-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathcommon.js
More file actions
479 lines (436 loc) · 13.2 KB
/
common.js
File metadata and controls
479 lines (436 loc) · 13.2 KB
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
var sla_month_error_color = 'red';
var sla_month_disabled_color = '#cdcdcd';
var sla_month_enabled_color = '#fafafa';
$(document).ready(function() {
$('.filter-status').on('change', filter_mapping_mapping).each(filter_mapping_mapping);
var direct_link_visible = false;
$('#current_report_params').click(function() {
// make sure we always empty the field
$('#link_container').html('');
if (!direct_link_visible) {
$('#link_container')
.html('<form>'+_label_direct_link+' <input class="wide" type="text" value="'
+ document.location.protocol + '//'
+ document.location.host
+ $('#current_report_params').prop('href')
+ '"></form>')
.css('position', 'absolute')
.css('top', this.offsetHeight + this.offsetTop + 5)
.css('right', '0')
.show();
direct_link_visible = true;
} else {
$('#link_container').hide();
direct_link_visible = false;
}
return false;
});
$('#save_report').click(function() {
if (!direct_link_visible) {
$('#save_report_form')
.css('position', 'absolute')
.css('top', this.offsetHeight + this.offsetTop + 5)
.css('right', '0')
.show()
.find('input[name=report_name]')
.map(function() {
var input = this;
if(input.value == "") {
input.focus();
}
});
direct_link_visible = true;
} else {
$('#save_report_form').hide();
direct_link_visible = false;
}
return false;
});
$("#report_id").bind('change', function() {
$("#saved_report_form").trigger('submit');
});
$('.save_report_btn').parents('form').submit(function(ev) {
ev.preventDefault();
var form = $(this);
var btn = form.find('.save_report_btn');
btn.after(loadimg_sml);
$.ajax({
url: form[0].action,
type: form[0].method,
data: form.serialize(),
complete: function() {
btn.parent().find('img:last').remove();
},
success: function(data, status_msg, xhr) {
if (data == null) {
$.notify(_reports_error + ": " + xhr.responseText, {'sticky': true});
return;
}
Notify.message(data.status_msg, {type: "success"});
if (!btn[0].form.report_id)
$('form').append('<input type="hidden" name="report_id" value="'+data.report_id+'"/>');
$('#save_report_form').hide();
},
error: function(data) {
var resp;
try {
resp = $.parseJSON(data.responseText).error;
} catch (ex) {
resp = "Unknown error";
}
$.notify(_reports_error + ": " + resp, {'sticky': true});
btn.parent().find('img:last').remove();
},
dataType: 'json'
});
});
$(document).on('change', '#report_type', function( e ) {
set_selection();
var filterable = jQuery.fn.filterable.find( $('select[name="objects[]"]') ),
type = e.target.value.replace( /s$/, "" );
var url = _site_domain + _index_page;
url += '/listview/fetch_ajax?query=[' + type + 's] all&columns[]=key&limit=1000000';
if ( filterable ) {
$.ajax({
url: url,
dataType: 'json',
error: function( xhr ) {
console.log( xhr.responseText );
},
success: function( data ) {
var names = [];
for ( var i = 0; i < data.data.length; i++ ) {
names.push( data.data[ i ].key );
}
filterable.data = new Set( names );
filterable.reset();
}
});
}
});
$(document).on('change', '#start_year, #end_year',function () {
var start = 0;
var end = 11;
if (check_custom_months.start_date == undefined || check_custom_months.end_date == undefined) {
return;
}
if (this.value == check_custom_months.start_date.getFullYear()) {
start = check_custom_months.start_date.getMonth();
}
if (this.value == check_custom_months.end_date.getFullYear()) {
end = check_custom_months.end_date.getMonth();
}
var html = '<option></option>';
for (i = start; i <= end; i++) {
html += '<option value="' + (i+1) + '">' + Date.monthNames[i] + '</option>';
}
if (this.id == 'start_year')
$('#start_month').html(html);
else
$('#end_month').html(html);
});
$('#start_year, #end_year, #start_month, #end_month').on('change', check_custom_months);
$("#delete_report").click(confirm_delete_report);
$(document).on('submit', '.report_form', function() {
$('.filter-status:visible:checked', this).each(function() {
$('#' + $(this).data('which')).find('input, select').attr('name', '');
});
$('.filter-status:not(:visible)', this).each(function() {
$('#' + $(this).data('which')).find('input, select').attr('value', '-2');
});
return check_form_values();
});
$('#report_type').on('change', set_selection);
set_selection();
});
function init_datepicker()
{
// datePicker Jquery plugin
var datepicker_enddate = (new Date()).asString();
$('.date-pick').datePicker({clickInput:true, startDate:_start_date, endDate:datepicker_enddate});
$('#cal_start').on(
'dpClosed',
function(e, selectedDates)
{
var d = selectedDates[0];
if (d) {
d = new Date(d);
$('#cal_end').dpSetStartDate(d.asString());
}
}
);
$('#cal_end').on(
'dpClosed',
function(e, selectedDates)
{
var d = selectedDates[0];
if (d) {
d = new Date(d);
$('#cal_start').dpSetEndDate(d.asString());
}
}
);
}
function show_calendar(val, update) {
if (val=='custom') {
$("#custom_time").show();
init_datepicker();
init_timepicker();
if (update == '') {
$('input[name=start_time]').attr('value', '');
$('input[name=end_time]').attr('value', '');
}
if ($('#month_1').length) {
check_custom_months();
}
} else {
$("#custom_time").hide();
}
}
function set_selection() {
var val = $('#report_type').val();
if ($.inArray(val, ['servicegroups', 'hostgroups', 'services', 'hosts']) === -1)
val = 'hostgroups'; // Why? Because I found it like this
$('.object-list-type').text(val);
$('*[data-show-for]').hide();
$('*[data-show-for~='+val+']').show();
}
function check_form_values(form)
{
if (!form)
form = document.documentElement;
var errors = 0;
var err_str = '';
var cur_start = '';
var cur_end = '';
if ($("#report_period", form).val() == 'custom') {
if ($('input[name=type]', form).val() != 'sla') {
// date validation
cur_start = Date.fromString($("input[name=cal_start]", form).val());
var time = $(".time_start", form).val().split(':');
cur_start.addHours(time[0]);
cur_start.addMinutes(time[1]);
cur_end = Date.fromString($("input[name=cal_end]", form).val());
time = $(".time_end", form).val().split(':');
cur_end.addHours(time[0]);
cur_end.addMinutes(time[1]);
var now = new Date();
if (!cur_start || !cur_end) {
if (!cur_start) {
errors++;
err_str += "<li>" + _reports_invalid_startdate + ".</li>";
}
if (!cur_end) {
errors++;
err_str += "<li>" + _reports_invalid_enddate + ".</li>";
}
} else {
if (cur_end > now) {
if (!confirm(_reports_enddate_infuture)) {
return false;
} else {
cur_end = now;
}
}
}
if (cur_end < cur_start) {
errors++;
err_str += "<li>" + _reports_enddate_lessthan_startdate + ".</li>";
$(".datepick-start", form).addClass("time_error");
$(".datepick-end", form).addClass("time_error");
} else {
$(".datepick-start", form).removeClass("time_error");
$(".datepick-end", form).removeClass("time_error");
}
} else {
// verify that we have years and month fields
if ($('#start_year', form).val() == '' || $('#start_month', form).val() == ''
|| $('#end_year', form).val() == '' || $('#end_month', form).val() == '') {
errors++;
//@@@Fixme: Add translated string
err_str += "<li>Please select year and month for both start and end. ";
err_str += "<br />Please note that SLA reports can only be generated for previous months</li>";
}
else {
// remember: our months are 1-indexed
cur_start = new Date(0);
cur_start.setYear($("select[name=start_year]", form).val());
cur_start.addMonths(Number($("select[name=start_month]", form).val()) - 1);
cur_end = new Date(0);
cur_end.setYear($("select[name=end_year]", form).val());
cur_end.addMonths(Number($("select[name=end_month]", form).val()));
}
if (cur_end < cur_start) {
errors++;
err_str += "<li>" + _reports_enddate_lessthan_startdate + ".</li>";
$(".datepick-start", form).addClass("time_error");
$(".datepick-end", form).addClass("time_error");
} else {
$(".datepick-start", form).removeClass("time_error");
$(".datepick-end", form).removeClass("time_error");
}
}
}
if ($('input[name=report_mode]:checked', form).val() != 'standard' && !$('#show_all', form).is(':checked') && $("[name=objects\\[\\]]", form).is('select') && $('[name=objects\\[\\]] option', form).length == 0) {
errors++;
err_str += "<li>" + _reports_err_str_noobjects + ".</li>";
}
if ($("#enter_sla", form).is(":visible")) {
// check for sane SLA values
var red_error = false;
var max_val = 100;
var nr_of_slas = 0;
for (i=1;i<=12;i++) {
var field_name = 'month_' + i;
var input = $('input[id="' + field_name + '"]', form);
var value = input.prop('value');
value = value.replace(',', '.');
if (value > max_val || isNaN(value)) {
input.css('background', sla_month_error_color);
errors++;
red_error = true;
}
if (value != '') {
nr_of_slas++;
}
}
if (red_error) {
err_str += '<li>' + _reports_sla_err_str + '</li>';
}
if (nr_of_slas == 0 && !red_error) {
errors++;
err_str += "<li>" + _reports_no_sla_str + "</li>";
}
}
// display err_str if any
if (!errors) {
$('#response', form).html('');
$('#response', form).hide();
return true;
}
// clear all style info from progress
var resp = $('#response', form);
if (!resp.length)
resp = $('#response');
resp.attr("style", "");
resp.html("<ul class='alert error'>" + err_str + "</ul>");
window.scrollTo(0,0); // make sure user sees the error message
return false;
}
// init timepicker once it it is shown
function init_timepicker()
{
$("#time_start, #time_end").timePicker();
}
function check_custom_months()
{
var f = $('.report_form').get(0);
// not SLA?
if (!f['start_month'])
return;
if (check_custom_months.start_date == undefined) {
check_custom_months.start_date = new Date(0);
check_custom_months.end_date = new Date();
$.ajax({
url: _site_domain + _index_page + '/sla/custom_start/',
type: 'GET',
dataType: 'json',
success: function(data) {
if (!data.timestamp) {
$.notify("Unable to fetch oldest report timestamp: " + data.responseText, {'sticky': true});
}
check_custom_months.start_date.setTime(data.timestamp * 1000);
var html = '<option></option>';
for (i = check_custom_months.start_date.getFullYear(); i <= check_custom_months.end_date.getFullYear(); i++) {
html += '<option>' + i + '</option>';
}
$('#start_year').html(html);
$('#end_year').html(html);
}
});
}
$('#progress').hide();
}
function confirm_delete_report()
{
var id = $("#report_id").prop('value')
var is_scheduled = $('#is_scheduled').text()!='' ? true : false;
var msg = _reports_confirm_delete + "\n";
var type = $('input[name=type]').prop('value');
if (!id)
return;
if (is_scheduled) {
msg += _reports_confirm_delete_warning;
}
msg = msg.replace("this saved report", "the saved report '"+$('#report_id option[selected=selected]').text()+"'");
if (confirm(msg)) {
$(this).after(loadimg_sml);
$.ajax({
url: _site_domain + _index_page + '/' + _controller_name + '/delete/',
type: 'POST',
data: {'report_id': id, csrf_token: _csrf_token},
complete: function() {
$(loadimg_sml).remove();
},
success: function(data) {
var a = document.createElement("a");
a.href = window.location.href;
if(a.search && a.search.indexOf("report_id="+id) !== -1) {
window.location.href = a.search.replace(new RegExp("report_id="+id+"&?"), "");
}
},
error: function(data) {
var msg;
try {
msg = $.parseJSON(data.responseText).error;
} catch (ex) {
msg = "Unknown error";
}
$.notify("Failed to delete report: " + msg, {'sticky': true});
},
dataType: 'json'
});
}
}
function filter_mapping_mapping()
{
if ($(this).is(':checked'))
$('#' + $(this).data('which')).hide();
else
$('#' + $(this).data('which')).show();
// when checking if the child is visible, the container must be visible
// or we'd be checking the wrong thing.
$(this).siblings('.configure_mapping').show();
if (!$(this).siblings('.configure_mapping').find('.filter_map:visible').length)
$(this).siblings('.configure_mapping').hide();
}
function run_generation_pdf()
{
var htmlString = document.getElementById('report-page').innerHTML;
var parser = new DOMParser();
var doc = parser.parseFromString(htmlString, 'text/html');
// remove div that contains links
var divToRemove = doc.getElementById('report-links-internal');
if (divToRemove) {
divToRemove.parentNode.removeChild(divToRemove);
}
var source = doc.documentElement.innerHTML;
$.ajax({
url: _site_domain + _index_page + '/' + _controller_name + '/generatepdf',
type: 'post',
data: {content:source},
success: function(response) {
console.log('Request Successful! :', response);
},
error: function(xhr, status, error) {
var msg;
if (xhr.status === 403) {
msg ='You do not have permission to access this resource.';
} else {
msg ='An error occurred. '+error;
}
$.notify("Failed to generate report: " + msg, {'sticky': true});
},
dataType: 'json'
});
}