-
Notifications
You must be signed in to change notification settings - Fork 982
/
application.js
489 lines (431 loc) · 15.3 KB
/
application.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
//= require jquery
//= require jquery.turbolinks
//= require turbolinks
//= require i18n
//= require jquery_ujs
//= require jquery.ui.autocomplete
//= require scoped_search
//= require bootstrap
//= require multi-select
//= require charts
//= require topbar
//= require two-pane
//= require vendor
//= require about
//= require jquery.extentions
//= require jquery.multi-select
//= require settings
//= require jquery.gridster
//= require hidden_values
//= require select_on_click
//= require select2
$(document).on('ContentLoad', function(){onContentLoad()});
Turbolinks.enableProgressBar();
$(function() {
$(document).trigger('ContentLoad');
});
function onContentLoad(){
uninitialized_autocompletes = $.grep($('.autocomplete-input'), function(i){ return !$(i).next().hasClass('autocomplete-clear'); });
if (uninitialized_autocompletes.length > 0) {
$.each(uninitialized_autocompletes, function(i, input) {$(input).scopedSearch({'delay': 250})});
$('.ui-helper-hidden-accessible').remove();
}
$('.flash.error').each(function(index, item) {
if ($('.alert.alert-danger.base').length == 0) {
if ($('#host-conflicts-modal').length == 0) {
notify(item, 'error');
}
}
});
$('.flash.warning').each(function(index, item) {
notify(item, 'warning');
});
$('.flash.notice').each(function(index, item) {
notify(item, 'success');
});
// adds buttons classes to all links
$("#title_action a").addClass("btn btn-default");
$("#title_action li a").removeClass("btn btn-default").addClass("la");
$("#title_action span").removeClass("btn btn-default").addClass("btn-group");
$("#title_action a[href*='new']").removeClass('btn-default').addClass("btn-success");
if ($("input[focus_on_load=true]").length > 0) {
$("input[focus_on_load]").first().focus();
}
// highlight tabs with errors
var errorFields = $(".tab-content .has-error");
errorFields.parents(".tab-pane").each(function() {
$("a[href=#"+this.id+"]").addClass("tab-error");
})
$(".tab-error").first().click();
$('.nav-pills .tab-error').first().click();
errorFields.first().find('.form-control').focus();
//set the tooltips
$('a[rel="popover"]').popover({html: true});
$('[rel="twipsy"]').tooltip({ container: 'body' });
$('*[title]').not('*[rel]').tooltip();
$('[data-table=inline]').not('.dataTable').dataTable(
{
"sDom": "<'row'<'col-md-6'f>r>t<'row'<'col-md-6'i><'col-md-6'p>>",
"sPaginationType": "bootstrap",
"order": [[ 0, 'asc' ]] // Set default order for the first column
}
);
// Prevents all links with the disabled attribute set to "disabled"
// from being clicked.
$('a[disabled="disabled"]').click(function() {
return false;
});
// allow opening new window for selected links
$('a[rel="external"]').attr("target","_blank");
$('*[data-ajax-url]').each(function() {
var url = $(this).data('ajax-url');
$(this).removeAttr('data-ajax-url');
$(this).load(url, function(response, status, xhr) {
if (status == "error") {
$(this).closest(".tab-content").find("#spinner").html(__('Failed to fetch: ') + xhr.status + " " + xhr.statusText);
}
if ($(this).data('on-complete')){
window[$(this).data('on-complete')].call(null, this, status);
}
});
});
multiSelectOnLoad();
// Removes the value from fake password field.
$("#fakepassword").val("");
$('form').on('click', 'input[type="submit"]', function() {
$("#fakepassword").remove();
});
password_caps_lock_hint();
var tz = jstz.determine();
$.cookie('timezone', tz.name(), { path: '/', secure: location.protocol === 'https:' });
$('.full-value').SelectOnClick();
$('select').select2({ allowClear: true });
$("#new_lookup_keys_ select").select2('destroy');
// Interface selects are initiated separately
$("#interfaces select").select2('destroy');
$('input.remove_form_templates').closest('form').submit(function(event) {
$(this).find('.form_template').remove()
})
}
function preserve_selected_options(elem) {
// mark the selected values to preserve them for form hiding
elem.find('option:not(:selected)').removeAttr('selected');
elem.find('option:selected').attr('selected', 'selected');
}
function password_caps_lock_hint() {
$('[type=password]').keypress(function(e) {
var $addon = $(this).parent().children('.input-addon'),
key = String.fromCharCode(e.which);
if (check_caps_lock(key, e)){
if (!$addon.is(':visible'))
$addon.show();
} else if ($addon.is(':visible')) {
$addon.hide();
}
});
}
//Tests if letter is upper case and the shift key is NOT pressed.
function check_caps_lock(key, e) {
return key.toUpperCase() === key && key.toLowerCase() !== key && !e.shiftKey
}
function remove_fields(link) {
$(link).prev("input[type=hidden]").val("1");
$(link).closest(".fields").hide();
mark_params_override();
}
function mark_params_override(){
$('#inherited_parameters .override-param').removeClass('override-param');
$('#inherited_parameters [data-tag=override]').show();
$('#parameters').find('[id$=_name]:visible').each(function(){
var param_name = $(this);
$('#inherited_parameters').find('[id^=name_]').each(function(){
if (param_name.val() == $(this).text()){
$(this).addClass('override-param');
$(this).closest('tr').find('textarea').addClass('override-param')
$(this).closest('tr').find('[data-tag=override]').hide();
}
})
})
$('#inherited_puppetclasses_parameters .override-param').removeClass('override-param');
$('#inherited_puppetclasses_parameters [data-tag=override]').show();
$('#puppetclasses_parameters').find('[data-property=class]:visible').each(function(){
var klass = $(this).val();
var name = $(this).closest('tr').find('[data-property=name]').val();
$('#inherited_puppetclasses_parameters [id^="puppetclass_"][id*="_params\\["][id$="\\]"]').each(function(){
var param = $(this);
if (param.find('[data-property=class]').text() == klass && param.find('[data-property=name]').text() == name) {
param.find('.error').removeClass('error');
param.find('.warning').removeClass('warning');
param.closest('tr').find('[data-property=name]').addClass('override-param');
param.find('input, textarea').addClass('override-param');
param.find('[data-tag=override]').hide();
}
});
});
$('#params-tab').removeClass("tab-error");
if ($("#params").find('.form-group.error').length > 0) $('#params-tab').addClass('tab-error');
$('a[rel="popover"]').popover({html: true});
}
function add_fields(link, association, content) {
var new_id = new Date().getTime();
var regexp = new RegExp("new_" + association, "g");
$(link).before(content.replace(regexp, new_id));
}
$(document).ready(function() {
$("#check_all_roles").click(function(e) {
e.preventDefault();
$(".role_checkbox").prop('checked', true);
});
$("#uncheck_all_roles").click(function(e) {
e.preventDefault();
$(".role_checkbox").prop('checked', false);
});
$('#login-form').submit(function(e){
$(".jnotify-container").remove();
});
});
function toggleCheckboxesBySelector(selector) {
boxes = $(selector);
var all_checked = true;
for (i = 0; i < boxes.length; i++) { if (!boxes[i].checked) { all_checked = false; } }
for (i = 0; i < boxes.length; i++) { boxes[i].checked = !all_checked; }
}
function toggleRowGroup(el) {
var tr = $(el).closest('tr');
var n = tr.next();
tr.toggleClass('open');
while (n.length > 0 && !n.hasClass('group')) {
n.toggle();
n = n.next();
}
}
function template_info(div, url) {
// Ignore method as PUT redirects to host page if used on update
form = $("form :input[name!='_method']").serialize();
build = $('input:radio[name$="[provision_method]"]:checked').val();
$(div).html(spinner_placeholder());
// Use a post to avoid request URI too large issues with big forms
$.ajax({
type: "POST",
url: url + "?provisioning=" + build,
data: form,
success: function(response, status, xhr) {
$(div).html(response);
$('select').select2({ allowClear: true });
},
error: function(jqXHR, textStatus, errorThrown) {
$(div).html('<div class="alert alert-warning alert-dismissable">' +
'<button type="button" class="close" data-dismiss="alert" aria-hidden="true">×</button>' +
__('Sorry but no templates were configured.') + '</div>');
}
});
}
//add bookmark dialog
$(function() {
$('#bookmarks-modal .modal-footer .btn-primary').on('click', function(){
$('#bookmarks-modal .modal-body .btn-primary').click();
});
$("#bookmarks-modal").bind('shown.bs.modal', function () {
var query = encodeURI($("#search").val());
var url = $("#bookmark").attr('data-url');
$("#bookmarks-modal .modal-body").empty();
$("#bookmarks-modal .modal-body").append("<span id='loading'>" + __('Loading ...') + "</span>");
$("#bookmarks-modal .modal-body").load(url + '&query=' + query + ' form',
function(response, status, xhr) {
$("#loading").hide();
$("#bookmarks-modal .modal-body .btn").hide()
});
});
});
function filter_by_level(item){
var level = $(item).val();
// Note that class names don't map to log level names (label-info == notice)
if(level == 'info'){
$('.label-info').closest('tr').show();
$('.label-default').closest('tr').show();
$('.label-warning').closest('tr').show();
$('.label-danger').closest('tr').show();
}
if(level == 'notice'){
$('.label-info').closest('tr').show();
$('.label-default').closest('tr').hide();
$('.label-warning').closest('tr').show();
$('.label-danger').closest('tr').show();
}
if(level == 'warning'){
$('.label-info').closest('tr').hide();
$('.label-default').closest('tr').hide();
$('.label-warning').closest('tr').show();
$('.label-danger').closest('tr').show();
}
if(level == 'error'){
$('.label-info').closest('tr').hide();
$('.label-default').closest('tr').hide();
$('.label-warning').closest('tr').hide();
$('.label-danger').closest('tr').show();
}
if($("#report_log tr:visible ").length ==1 || $("#report_log tr:visible ").length ==2 && $('#ntsh:visible').length > 0 ){
$('#ntsh').show();
}
else{
$('#ntsh').hide();
}
}
function auth_source_selected(){
var auth_source_id = $('#user_auth_source_id').val();
if (auth_source_id == '') {
$("#password").hide();
} else {
$("#password").show();
}
}
function show_release(element){
var os_family = $(element).val();
if ($.inArray(os_family, ['Debian', 'Solaris', 'Coreos']) != -1) {
$("#release_name").show();
} else {
$("#release_name").hide();
}
}
// return a hash with values of all attributes
function attribute_hash(attributes){
var attrs = {};
for (i=0;i < attributes.length; i++) {
var attr = $('*[id$='+attributes[i]+']');
if (attr.length > 0) {
if(attr.attr("type")=="checkbox"){
attrs[attributes[i]] = [];
$("*[id*="+attributes[i]+"]:checked").each(function(index,item){
attrs[attributes[i]].push($(item).val());
})
} else {
if (attr.length > 1) {
// select2 adds a div, so now we have a select && div
attrs[attributes[i]] = $($.grep(attr, function(a) {
return $(a).is("select");
})).val();
} else {
if (attr.val() != null) attrs[attributes[i]] = attr.val();
}
}
}
}
return attrs;
}
function ignore_subnet(item){
$(item).tooltip('hide');
$(item).closest('.accordion-group').remove();
}
function show_rdoc(item){
var url = $(item).attr('data-url');
window.open(url);
}
// shows provisioning templates in a new window
$(function() {
$('[data-provisioning-template=true]').click(function(){
window.open(this.href, [width='300',height='400',scrollbars='yes']);
return false;
});
});
function update_puppetclasses(element) {
var host_id = $("form").data('id')
var env_id = $('select[name*=environment_id]').val();
var url = $(element).attr('data-url');
var data = $("form").serialize().replace('method=put', 'method=post');
if (url.match('hostgroups')) {
data = data + '&hostgroup_id=' + host_id
} else {
data = data + '&host_id=' + host_id
}
if (env_id == "") return;
$(element).indicator_show();
$.ajax({
type: 'post',
url: url,
data: data,
success: function(request) {
$('#puppet_klasses').html(request);
reload_puppetclass_params();
$('[rel="twipsy"]').tooltip();
},
complete: function() {
reloadOnAjaxComplete(element);
}
})
}
// generates an absolute, needed in case of running Foreman from a subpath
function foreman_url(path) {
return URL_PREFIX + path;
}
$.fn.indicator_show = function(){
$(this).parents('.form-group').find('img').show();
}
$.fn.indicator_hide = function(){
$(this).parents('.form-group').find('img').hide();
}
function spinner_placeholder(text){
if (text == undefined) text = "";
return "<div class='spinner-placeholder'>" + text + "</div>"
}
function notify(item, type) {
var options = { type: type, sticky: (type != 'success') };
$.jnotify($(item).text(), options);
$(item).remove();
}
function filter_permissions(item){
var term = $(item).val().trim();
if (term.length > 0) {
$(".form-group .collapse").parents('.form-group').hide();
$(".form-group .control-label:icontains('"+term+"')").parents('.form-group').show();
} else{
$(".form-group .collapse").parents('.form-group').show();
}
}
function setPowerState(item, status){
if(status=='success') {
var place_holder = $('#loading_power_state').parent('.btn-group');
var power_actions = $('#power_actions');
power_actions.find('.btn-sm').removeClass('btn-sm');
if (power_actions.find('.btn-group').exists()){
power_actions.contents().replaceAll(place_holder);
}else{
power_actions.contents().appendTo(place_holder);
$('#loading_power_state').remove();
}
}else{
$('#loading_power_state').text(_('Unknown power state'))
}
power_actions.hide();
$('[rel="twipsy"]').tooltip();
}
function reloadOnAjaxComplete(element) {
$(element).indicator_hide();
$('[rel="twipsy"]').tooltip();
$('select').select2({ allowClear: true });
}
function set_fullscreen(element){
var exit_button = $('<div class="exit-fullscreen"><a class="btn btn-default btn-lg" href="#" onclick="exit_fullscreen(); return false;" title="'+__('Exit Full Screen')+'"><i class="glyphicon glyphicon-resize-small"></i></a></div>');
element.data('origin',element.parent())
.data('position', $(window).scrollTop())
.addClass('fullscreen')
.appendTo($('#main'))
.resize()
.after(exit_button);
$('#content').addClass('hidden');
$('.navbar').addClass('hidden');
$(document).on('keyup', function(e) {
if (e.keyCode == 27) { // esc
exit_fullscreen();
}
});
}
function exit_fullscreen(){
var element = $('.fullscreen');
$('#content').removeClass('hidden');
$('.navbar').removeClass('hidden');
element.removeClass('fullscreen')
.prependTo(element.data('origin'))
.resize();
$('.exit-fullscreen').remove();
$(window).scrollTop(element.data('position'));
}