noheader: Remove leftovers from "noheader"
[ninja.git] / application / views / js / common.js
blob100822feb9ab0f3ca226880a63d3771679b7729d
1 var sURL = window.location.pathname + location.search;
2 var _interval = 0;
3 var _save_page_interval = 0;
4 var current_interval = 0;
5 var edit_visible = 0;
6 var _save_scroll = true;
8 $(document).ready(function() {
10         var content_div = $( "body > .container > #content" ),
11                 header_div = $( "body > .container >#header" ),
12                 body = $( "body" );
14         function fit_content () {
15                 var height = body.height() - header_div.outerHeight();
16                 content_div.css( "height", height + "px" );
17         }
19         $(window).bind( "resize", fit_content );
20         fit_content();
22         // make scroll memory cookie to be reset
23         // when actively clicking on a link.
24         $('body').on('click', 'a', function() {
25                 _save_scroll = false;
26         });
28         if ( content_div ) {
29                 content_div.click();
30                 content_div.focus();
31         }
33         // stop widgets from trying to reload once user clicked
34         // on a menu
35         $('#menu a').click(function() {_is_refreshing = true;});
37         if ($.fn.contextMenu) {
38                 $("body").contextMenu({
39                                 menu: 'property_menu', use_prop:true
40                         },
41                         function(action, elem){
42                                 object_action(action, elem.attr('id'));
43                         }, ".obj_properties:not(.white)");
45                 $("body").contextMenu({
46                                 menu: 'svc_property_menu', use_prop:true
47                         },
48                         function(action, elem){
49                                 object_action(action, elem.attr('id'));
50                         },".svc_obj_properties");
51         }
53         // refresh helper code
54         var old_refresh = 0;
55         var refresh_is_paused = false;
56         $("#ninja_refresh_control").bind('change', function() {
57                 if ($("#ninja_refresh_control").attr('checked')) {
58                         // save previous refresh rate
59                         // to be able to restore it later
60                         old_refresh = current_interval;
61                         $('#ninja_refresh_lable').css('font-weight', 'bold');
62                         ninja_refresh(0);
63                         refresh_is_paused = true;
64                 } else {
65                         // restore previous refresh rate
66                         ninja_refresh(old_refresh);
67                         refresh_is_paused = false;
68                         $('#ninja_refresh_lable').css('font-weight', '');
69                 }
70         });
71         if ($('#ninja_refresh_edit').text()!=='') {
72                 create_slider('ninja_page_refresh');
73                 $('#ninja_page_refresh_slider').on('slidechange', function() {
74                         var delay = parseInt($('#ninja_page_refresh_value').val(), 10);
75                         $.jGrowl(sprintf(_page_refresh_msg, delay), { header: _success_header });
76                         ninja_refresh(delay);
77                 });
78         }
79         $('#ninja_refresh_edit').bind('click', function() {
80                 if (!edit_visible) {
81                         $('#ninja_page_refresh_slider').show();
82                         edit_visible = 1;
83                 } else {
84                         $('#ninja_page_refresh_slider').hide();
85                         edit_visible = 0;
86                 }
87         });
88         // -- end refresh helper code
90         // listview refresh helper code
91         $("#listview_refresh_control").bind('change', function() {
92                 if ($("#listview_refresh_control").attr('checked')) {
93                         // save previous refresh rate
94                         // to be able to restore it later
95                         $('#listview_refresh_lable').css('font-weight', 'bold');
96                         clearTimeout(lsfilter_storage.list.autorefresh_timer);
97                         $.jGrowl(_listview_refresh_paused_msg, { header: _success_header });
98                 } else {
99                         // restore previous refresh rate
100                         $('#listview_refresh_lable').css('font-weight', '');
101                         lsfilter_storage.list.start_autorefresh_timer();
102                         $.jGrowl(_listview_refresh_unpaused_msg, { header: _success_header });
103                 }
104         });
105         $("#listview_refresh_value").bind('change', function() {
106                 $("#listview_refresh_slider").slider("value", this.value);
107         });
108         if ($('#listview_refresh_edit').text()!=='') {
109                 create_slider('listview_refresh');
110                 $('#listview_refresh_slider').on('slidechange', function() {
111                         var delay = parseInt($('#listview_refresh_value').val(), 10);
112                         $.jGrowl(sprintf(_listview_refresh_msg, delay), { header: _success_header });
113                         clearTimeout(lsfilter_storage.list.autorefresh_timer);
114                         if (delay > 0) {
115                                 lsfilter_storage.list.config.autorefresh_enabled = true;
116                         } else {
117                                 lsfilter_storage.list.config.autorefresh_enabled = false;
118                         }
119                         lsfilter_storage.list.config.autorefresh_delay = delay * 1000;
120                         lsfilter_storage.list.start_autorefresh_timer();
121                 });
122         }
123         // -- end listview refresh helper code
125         $('.host_comment').each(function() {
126                 var anchor = $(this);
127                 var obj_name = anchor.data('obj_name');
128                 if (!obj_name) {
129                         return false;
130                 }
133                 // Remove the tooltip of the inner element since it overlays qtip
134                 anchor.find('span').attr('title', '');
136                 anchor.qtip($.extend(true, {}, qtip_default, {
137                         content: {
138                                 text: function(ev, api) {
139                                         $.ajax({
140                                                 url: _site_domain + _index_page + "/ajax/fetch_comments/",
141                                                 data: {host: obj_name}
142                                         })
143                                         .done(function(html) {
144                                                 api.set('content.text', html);
145                                         })
146                                         .fail(function(xhr, status, error) {
147                                                 api.set('content.text', status + ': ' + error);
148                                         });
150                                         return '<img src="' + _site_domain + loading_img + '" alt="' + _loading_str + '" />';
151                                 }
152                         }
153                 }));
154         });
156         $(".helptext_target").each(function(){
158                 // split the id into controller, key
159                 var the_id = $(this).attr('id');
160                 var part = the_id.split('|');
161                 if (!part.length) {
162                         return false;
163                 }
164                 var controller = part[1];
165                 var key = part[2];
166                 var elem_id = the_id;
168                 $(this).qtip($.extend(true, {}, qtip_default, {
169                         content: {
170                                 text: function(ev, api) {
171                                         $.ajax({
172                                                 url: _site_domain + _index_page + "/ajax/get_translation/",
173                                                 data: {controller: controller, key: key},
174                                                 type: 'POST',
175                                         })
176                                         .done(function(html) {
177                                                 api.set('content.text', html);
178                                         })
179                                         .fail(function(xhr, status, error) {
180                                                 api.set('content.text', status + ': ' + error);
181                                         });
183                                         return '<img src="' + _site_domain + loading_img + '" alt="' + _loading_str + '" />';
184                                 }
185                         }
186                 }));
187         });
188         $(".helptext_target").click(function() {return false;});
190         $('#multi_action_select').bind('change', function() {
191                 multi_action_select($(this).find('option:selected').val());
192         });
194         $('.select_all_items_service').live('click', function() {
195                 if ($(this).attr('checked')) {
196                         $(this).parents('table').find(".item_select_service input[type='checkbox']").not('.select_all_items_service').each(function() {
197                                 if (!$(this).attr('disabled') && !$(this).is(':hidden')) {
198                                         $(this).attr('checked', true);
199                                 }
200                                 else if ($(this).is(':hidden')) {
201                                         $(this).attr('checked', false);
202                                 }
203                         });
204                 } else {
205                         $(this).parents('table').find(".item_select_service input[type='checkbox']").not('.select_all_items_service').each(function() {
206                                 $(this).attr('checked', false);
207                         });
208                 }
209         });
210         // Toggle visibility for quick menu items
211         $("#page_settings_icon, #global_notifications_icon").click(function() {
212                 var menu_item = $(this);
213                 var li = menu_item.parents('li');
214                 var submenu = $('#'+menu_item[0].id.replace(/_icon$/, ''));
215                 if (submenu.is(':hidden')) {
216                         li.addClass("selected");
217                         submenu
218                                 .show()
219                                 .css('top', '49px')
220                                 .css('left', (menu_item.offset().left - 10) + 'px');
222                 } else {
223                         li.removeClass("selected");
224                         submenu.hide();
225                 }
226                 return false;
227         });
229         // are we using keyboard commands or not
230         if (_keycommands_active) {
231                 if (typeof _keycommand_forward !== 'undefined' && _keycommand_forward !== '') {
232                         jQuery(document).bind('keydown', _keycommand_forward, function (evt){
233                                 if (typeof $('.nextpage').attr('href') != 'undefined') {
234                                         // reset scroll memory to start at top for next page
235                                         _save_scroll = false;
236                                         self.location.href=$('.nextpage').attr('href');
237                                 }
238                                 return false;
239                         });
240                 }
242                 if (typeof _keycommand_back !== 'undefined' && _keycommand_back !== '') {
243                         jQuery(document).bind('keydown', _keycommand_back, function (evt){
244                                 if (typeof $('.prevpage').attr('href') != 'undefined') {
245                                         // reset scroll memory to start at top for previous page
246                                         _save_scroll = false;
247                                         self.location.href=$('.prevpage').attr('href');
248                                 }
249                                 return false;
250                         });
251                 }
253                 if (typeof _keycommand_search !== 'undefined' && _keycommand_search !== '') {
254                         jQuery(document).bind('keydown', _keycommand_search, function (evt){$('#query').focus(); return false; });
255                 }
257                 if (typeof _keycommand_pause !== 'undefined' && _keycommand_pause !== '') {
258                         jQuery(document).bind('keydown', _keycommand_pause, function (evt){
259                                 toggle_refresh();
260                                 return false;
261                         });
262                 }
263         }
265         /**
266         *       Toggle page refresh and show a jGrowl message to user about state
267         */
268         function toggle_refresh()
269         {
270                 if ($("#ninja_refresh_control").attr('checked')) {
271                         // restore previous refresh rate
272                         ninja_refresh(old_refresh);
273                         refresh_is_paused = false;
274                         $('#ninja_refresh_lable').css('font-weight', '');
275                         $("#ninja_refresh_control").attr('checked', false);
277                         // inform user
278                         $.jGrowl(_refresh_unpaused_msg, { header: _success_header });
279                 } else {
280                         // Prevent message from showing up when no pause is available
281                         if ($("#ninja_page_refresh").html() === null) {
282                                 return false;
283                         }
285                         $("#ninja_refresh_control").attr('checked', true);
286                         // save previous refresh rate
287                         // to be able to restore it later
288                         old_refresh = current_interval;
289                         $('#ninja_refresh_lable').css('font-weight', 'bold');
290                         ninja_refresh(0);
291                         refresh_is_paused = true;
293                         // inform user
294                         $.jGrowl(_refresh_paused_msg, { header: _success_header });
295                 }
296         }
298         $('#multi_object_submit_service').click(function() {
299                 // check that we have any selected items
300                 if (!$('.item_select_service input[name=object_select\\[\\]]').is(':checked')) {
301                         show_message("multi_object_submit_progress_service", _nothing_selected_error);
302                         return false;
303                 }
305                 // Check if we actually set an action
306                 if ($('#multi_action_select_service').val() === "") {
307                         show_message("multi_object_submit_progress_service", _no_action_error);
308                         return false;
309                 }
311                 show_progress("multi_object_submit_progress_service", _wait_str);
312         });
314         $('#multi_object_submit').click(function() {
315                 // check that we have any selected items
316                 if (!$('.item_select input[name=object_select\\[\\]]').is(':checked')) {
317                         show_message("multi_object_submit_progress", _nothing_selected_error);
318                         return false;
319                 }
321                 // Check if we actually set an action
322                 if ($('#multi_action_select').val() === "") {
323                         show_message("multi_object_submit_progress", _no_action_error);
324                         return false;
325                 }
327                 show_progress("multi_object_submit_progress", _wait_str);
328         });
332 function _(text)
334         // console.log('To translate: '+ text);
335         return text;
338 var loadimg_sml = new Image(16,16);
339 loadimg_sml.src = _site_domain + 'application/media/images/loading_small.gif';
342 *       cache the progress indicator image to show faster...
344 var Image1 = new Image(16,16);
345 Image1.src = _site_domain + 'application/media/images/loading.gif';
348 *       Show a progress indicator to inform user that something
349 *       is happening...
351 function show_progress(the_id, info_str, size_str) {
352         switch (size_str) {
353                 case "small": case "tiny":
354                         size_str = loadimg_sml.src;
355                         break;
356                 case "large": case "big":
357                         size_str = Image1.src;
358                         break;
359                 default:
360                         size_str = loadimg_sml.src;
361                         break;
362         }
363         $("#" + the_id).html('<img id="progress_image_id" src="' + size_str + '"> <em>' + info_str +'</em>').show();
366 function show_message(the_id, info_str) {
367         $("#" + the_id).html('<em>' + info_str +'</em>').show();
370 function switch_image(html_id, src)
372         $('#' + html_id).attr('src', src);
375 function object_action(action,the_id)
377         var parts = the_id.split('|');
378         var type = false;
379         var name = false;
380         var service = false;
381         switch(parts.length) {
382                 case 0: case 1:
383                         return false;
384                 case 2: // host or groups
385                         name = parts[1];
386                         break;
387                 case 3: // service
388                         name = parts[1];
389                         service = parts[2];
390                         break;
391                 case 4: // service
392                         name = parts[1];
393                         service = parts[3];
394                         break;
395         }
397         type = parts[0];
399         var cmd = false;
400         switch(action) {
401                 case 'schedule_host_downtime':
402                 case 'schedule_svc_downtime':
403                 case 'del_host_downtime':
404                 case 'del_svc_downtime':
405                 case 'acknowledge_host_problem':
406                 case 'acknowledge_svc_problem':
407                 case 'disable_host_svc_notifications':
408                 case 'disable_host_check':
409                 case 'disable_svc_check':
410                 case 'enable_host_check':
411                 case 'enable_svc_check':
412                 case 'schedule_host_check':
413                 case 'schedule_host_svc_checks':
414                 case 'schedule_svc_check':
415                 case 'add_host_comment':
416                 case 'add_svc_comment':
417                         cmd = action.toUpperCase();
418                         break;
419                 case 'remove_acknowledgement':
420                         cmd = type == 'host' ? 'REMOVE_HOST_ACKNOWLEDGEMENT' : 'REMOVE_SVC_ACKNOWLEDGEMENT';
421                         break;
422                 case 'disable_notifications':
423                         cmd = type == 'host' ? 'DISABLE_HOST_NOTIFICATIONS' : 'DISABLE_SVC_NOTIFICATIONS';
424                         break;
425                 case 'enable_notifications':
426                         cmd = type == 'host' ? 'ENABLE_HOST_NOTIFICATIONS' : 'ENABLE_SVC_NOTIFICATIONS';
427                         break;
428         }
430         // return if we couldn't figure out what command to run
431         if (cmd === false) {
432                 return false;
433         }
435         var target = _site_domain + _index_page + '/command/submit?cmd_typ=' + cmd + '&host_name=' + name;
436         if (service !== false) {
437                 target += '&service=' + service;
438         }
439         self.location.href = target;
443 *       Handle multi select of different actions
445 function multi_action_select(action, type)
447         // start by enabling all checkboxes in case
448         // they have been previously disabled
449         var field = 'item_select';
450         var prop_field = 'obj_prop';
451         if (type == 'service') {
452                 $(".item_select_service input[type='checkbox']").attr('disabled', false);
453                 field = 'item_select_service';
454                 prop_field = 'obj_prop_service';
455         } else {
456                 $(".item_select input[type='checkbox']").attr('disabled', false);
457         }
459         if (action === '')
460                 return false;
462         var ACKNOWLEDGED = 1;
463         var NOTIFICATIONS_ENABLED = 2;
464         var CHECKS_ENABLED = 4;
465         var SCHEDULED_DT = 8;
467         $('.' + prop_field).each(function() {
468                 var that = $(this);
469                 var test = false;
470                 switch (action) {
471                         case 'ACKNOWLEDGE_HOST_PROBLEM':
472                         case 'ACKNOWLEDGE_SVC_PROBLEM':
473                                 test = that.text() & ACKNOWLEDGED || !(that.text() & 16);
474                                 break;
475                         case 'REMOVE_HOST_ACKNOWLEDGEMENT':
476                         case 'REMOVE_SVC_ACKNOWLEDGEMENT':
477                                 test = !(that.text() & ACKNOWLEDGED);
478                                 break;
479                         case 'DISABLE_HOST_NOTIFICATIONS':
480                         case 'DISABLE_SVC_NOTIFICATIONS':
481                                 test = that.text() & NOTIFICATIONS_ENABLED;
482                                 break;
483                         case 'ENABLE_HOST_NOTIFICATIONS':
484                         case 'ENABLE_SVC_NOTIFICATIONS':
485                                 test = !(that.text() & NOTIFICATIONS_ENABLED);
486                                 break;
487                         case 'ENABLE_HOST_CHECK':
488                         case 'ENABLE_SVC_CHECK':
489                                 test = !(that.text() & CHECKS_ENABLED);
490                                 break;
491                         case 'DISABLE_HOST_CHECK':
492                         case 'DISABLE_SVC_CHECK':
493                                 test = that.text() & CHECKS_ENABLED;
494                                 break;
495                         case 'DEL_HOST_DOWNTIME':
496                         case 'DEL_SVC_DOWNTIME':
497                                 test = !(that.text() & SCHEDULED_DT);
498                                 break;
499                 }
500                 if (test) {
501                         that.closest('tr').find("." + field + " input[type='checkbox']").attr('disabled', true).attr('checked', false);
502                 }
503         });
506 function create_slider(the_id)
508         var last_update_request = false;
509         var id = $('#' + the_id + '_value');
510         var key = id.data('key');
511         var interval = id.val();
512         $("#" + the_id + "_slider").slider({
513                 value: interval,
514                 min: 0,
515                 max: 500,
516                 step: 10,
517                 slide: function(event, ui) {
518                         $("#" + the_id + "_value").val(ui.value);
519                 },
520                 stop: function(event, ui) {
521                         interval = ui.value;
522                         if(last_update_request !== false) {
523                                 last_update_request.abort();
524                         }
525                         last_update_request = $.ajax(
526                                 _site_domain + _index_page + "/ajax/save_page_setting/",
527                                 {
528                                         data: {
529                                                 page: '*',
530                                                 setting: interval,
531                                                 type: key
532                                         },
533                                         complete: function() {
534                                                 last_update_request = false;
535                                                 id.val(interval);
536                                         },
537                                         type: 'POST'
538                                 }
539                         );
540                 }
541         });
542         id.val($("#" + the_id + "_slider").slider("value"));
545 function ninja_refresh(val)
547         if (_interval) {
548                 clearInterval(_interval);
549         }
550         var refresh_val = (val === null) ? _refresh : val;
551         current_interval = refresh_val;
552         if (val>0) {
553                 _interval = setInterval( "refresh()", refresh_val*1000 );
554         }
557 function jgrowl_message(message_str, header_str)
559         if (message_str!=='') {
560                 $.jGrowl(message_str, { header: header_str });
561         }
565 // ===========================================================
566 // code for remembering scroll position between page reloads
567 // adapted from http://www.huntingground.freeserve.co.uk/main/mainfram.htm?../scripts/cookies/scrollpos.htm
568 // ===========================================================
570 cookieName = "page_scroll";
571 expdays = 5;
573 function setCookie(name, value, expires, path, domain, secure) {
574         if (!expires) {
575                 expires = new Date();
576         }
577         document.cookie = name + "=" + escape(value) +
578         ((expires === null) ? "" : "; expires=" + expires.toGMTString()) +
579         ((path === null) ? "" : "; path=" + path) +
580         ((domain === null) ? "" : "; domain=" + domain) +
581         ((secure === null) ? "" : "; secure");
584 function getCookie(name) {
585         var arg = name + "=";
586         var alen = arg.length;
587         var clen = document.cookie.length;
588         var i = 0;
589         while (i < clen) {
590                 var j = i + alen;
591                 if (document.cookie.substring(i, j) == arg){
592                         return getCookieVal(j);
593                 }
594                 i = document.cookie.indexOf(" ", i) + 1;
595                 if (i === 0) {
596                         break;
597                 }
598         }
599         return null;
602 function getCookieVal(offset) {
603         var endstr = document.cookie.indexOf (";", offset);
604         if (endstr == -1) {
605                 endstr = document.cookie.length;
606         }
607         return unescape(document.cookie.substring(offset, endstr));
610 function deleteCookie(name,path,domain) {
611         document.cookie = name + "=" + ((path === null) ? "" : "; path=" + path) + ((domain === null) ? "" : "; domain=" + domain) + "; expires=Thu, 01-Jan-00 00:00:01 GMT";
614 function saveScroll() {
615         var expdate = new Date();
616         expdate.setTime (expdate.getTime() + (expdays*24*60*60*1000)); // expiry date
618         if (!_save_scroll) {
619                 // reset scroll memory to top
620                 setCookie(cookieName,'0_0',expdate);
621                 return;
622         }
624         var x = $(window).scrollLeft();
625         var y = $(window).scrollTop();
626         Data = x + "_" + y;
628         setCookie(cookieName,Data,expdate);
631 $(window).bind('beforeunload', saveScroll);
633 function loadScroll() { // added function
634         inf = getCookie(cookieName);
635         if(!inf) {
636                 return;
637         }
638         var ar = inf.split("_");
639         if (ar.length == 2) {
640                 $(window).scrollLeft(parseInt(ar[0], 10));
641                 $(window).scrollTop(parseInt(ar[1], 10));
642         }
645 $(window).bind('load', loadScroll);
647 function trigger_cb_on_nth_call(cb, n) {
648         return function() {
649                 if (--n <= 0)
650                         cb();
651         };