reports: Fix id of custom date selector
[ninja.git] / application / media / js / jquery.contextMenu.js
blob6273faa6353dce6b924223bc0e0c591dfdfe0c06
1 // jQuery Context Menu Plugin
2 //
3 // Version 1.01
4 //
5 // Cory S.N. LaViska
6 // A Beautiful Site (http://abeautifulsite.net/)
7 //
8 // More info: http://abeautifulsite.net/2008/09/jquery-context-menu-plugin/
9 //
10 // Terms of Use
12 // This plugin is dual-licensed under the GNU General Public License
13 //   and the MIT License and is copyright A Beautiful Site, LLC.
15 if(jQuery)( function() {
16         var show_context_menu = function(el, e, o, callback) {
17                 var offset = el.offset();
18                 // Add contextMenu class
19                 $('#' + o.menu).addClass('contextMenu');
20                 // Simulate a true right click
21                 var evt = e;
22                 evt.stopPropagation();
23                 el.mouseup( function(e) {
24                         e.stopPropagation();
25                         el.unbind('mouseup');
26                         if( evt.button == 2 ) {
27                                 // Hide context menus that may be showing
28                                 $(".contextMenu").hide();
29                                 // Get this context menu
30                                 var menu = $('#' + o.menu);
32                                 if( el.hasClass('disabled') ) return false;
34                                 // enable/disable menu items
35                                 // depending on obj_prop value
36                                 if (o.use_prop == true) { // added by op5
37                                         // start by showing all menu items
38                                         // in case they have been previously disabled
39                                         $('.contextMenu li').show();
40                                         var the_id = $(this).attr('id');
41                                         var parts = the_id.split('|');
42                                         var name = false;
43                                         var service = false;
44                                         switch(parts.length) {
45                                                 case 0: case 1: return false;
46                                                         break;
47                                                 case 2: // host or groups
48                                                         name = parts[1];
49                                                         break;
50                                                 case 4: // service
51                                                         name = parts[1];
52                                                         service = parts[2];
53                                                         break;
54                                         }
56                                         if (service != false) {
57                                                 name = name + '__' + service;
58                                         }
60                                         name = name.replace(/[^a-zA-Z0-9-_]/g, '_');
62                                         var obj_prop = $('._' + name).text();
64                                         var OK = 0;
65                                         var ACKNOWLEDGED = 1;
66                                         var NOTIFICATIONS_ENABLED = 2;
67                                         var CHECKS_ENABLED = 4;
68                                         var SCHEDULED_DT = 8;
69                                         if (obj_prop != '') {
70                                                 if (obj_prop & ACKNOWLEDGED || !(obj_prop & 16) || obj_prop & OK) {
71                                                         $('#_menu_acknowledge_host_problem').hide();
72                                                         $('#_menu_acknowledge_svc_problem').hide();
73                                                 }
74                                                 if (!(obj_prop & ACKNOWLEDGED)) { // || obj_prop & OK
75                                                         $('#_menu_remove_host_acknowledgement').hide();
76                                                         $('#_menu_remove_svc_acknowledgement').hide();
77                                                 }
78                                                 if (obj_prop & NOTIFICATIONS_ENABLED) {
79                                                         $('#_menu_disable_host_notifications').hide();
80                                                         $('#_menu_disable_svc_notifications').hide();
81                                                 }
82                                                 if (!(obj_prop & NOTIFICATIONS_ENABLED)) {
83                                                         $('#_menu_enable_host_notifications').hide();
84                                                         $('#_menu_enable_svc_notifications').hide();
85                                                 }
86                                                 if (obj_prop & CHECKS_ENABLED) {
87                                                         $('#_menu_disable_host_check').hide();
88                                                         $('#_menu_disable_svc_check').hide();
89                                                 }
90                                                 if (!(obj_prop & CHECKS_ENABLED)) {
91                                                         $('#_menu_enable_host_check').hide();
92                                                         $('#_menu_enable_svc_check').hide();
93                                                 }
94                                                 if (obj_prop & SCHEDULED_DT) {
95                                                         $('#_menu_schedule_host_downtime').hide();
96                                                         $('#_menu_schedule_svc_downtime').hide();
97                                                 }
98                                                 if (!(obj_prop & SCHEDULED_DT)) {
99                                                         $('#_menu_removeschedule_host_downtime').hide();
100                                                         $('#_menu_removeschedule_svc_downtime').hide();
101                                                 } else if(el.hasClass('svc_obj_properties')) {
102                                                         // Do not offer to cancel scheduled downtime if its host is in scheduled downtime. Look for that.
104                                                         // Traverse upwards, looking for a host-td (since it might not be to the immidiate left of this service's td)
105                                                         var tr_to_examine = el.parent();
106                                                         while(tr_to_examine.find('td').eq(1).hasClass('white')) {
107                                                                 tr_to_examine = tr_to_examine.prev();
108                                                         }
110                                                         if(tr_to_examine.find('.service_hostname img[title="Scheduled downtime"]').length > 0) {
111                                                                 $('#_menu_removeschedule_svc_downtime').hide();
112                                                         }
113                                                 }
114                                         }
115                                 }
117                                 // Detect mouse position
118                                 var d = {}, x, y;
119                                 if( self.innerHeight ) {
120                                         d.pageYOffset = self.pageYOffset;
121                                         d.pageXOffset = self.pageXOffset;
122                                         d.innerHeight = self.innerHeight;
123                                         d.innerWidth = self.innerWidth;
124                                 } else if( document.documentElement &&
125                                         document.documentElement.clientHeight ) {
126                                         d.pageYOffset = document.documentElement.scrollTop;
127                                         d.pageXOffset = document.documentElement.scrollLeft;
128                                         d.innerHeight = document.documentElement.clientHeight;
129                                         d.innerWidth = document.documentElement.clientWidth;
130                                 } else if( document.body ) {
131                                         d.pageYOffset = document.body.scrollTop;
132                                         d.pageXOffset = document.body.scrollLeft;
133                                         d.innerHeight = document.body.clientHeight;
134                                         d.innerWidth = document.body.clientWidth;
135                                 }
136                                 x = e.clientX;
137                                 y = e.clientY;
139                                 // Show the menu
140                                 $(document).unbind('click');
141                                 // Make sure menu doesn't extend outside viewport
142                                 if (y + $(menu).height() >= $(window).height()) {
143                                         y = y - $(menu).height();
144                                 }
145                                 $(menu).css({ top: y, left: x , position: 'fixed'}).fadeIn(o.inSpeed);
146                                 // Hover events
147                                 $(menu).find('A').mouseover( function() {
148                                         $(menu).find('LI.hover').removeClass('hover');
149                                         $(this).parent().addClass('hover');
150                                 }).mouseout( function() {
151                                         $(menu).find('LI.hover').removeClass('hover');
152                                 });
154                                 // Keyboard
155                                 $(document).keypress( function(e) {
156                                         switch( e.keyCode ) {
157                                                 case 38: // up
158                                                         if( $(menu).find('LI.hover').size() == 0 ) {
159                                                                 $(menu).find('LI:last').addClass('hover');
160                                                         } else {
161                                                                 $(menu).find('LI.hover').removeClass('hover').prevAll('LI:not(.disabled)').eq(0).addClass('hover');
162                                                                 if( $(menu).find('LI.hover').size() == 0 ) $(menu).find('LI:last').addClass('hover');
163                                                         }
164                                                 break;
165                                                 case 40: // down
166                                                         if( $(menu).find('LI.hover').size() == 0 ) {
167                                                                 $(menu).find('LI:first').addClass('hover');
168                                                         } else {
169                                                                 $(menu).find('LI.hover').removeClass('hover').nextAll('LI:not(.disabled)').eq(0).addClass('hover');
170                                                                 if( $(menu).find('LI.hover').size() == 0 ) $(menu).find('LI:first').addClass('hover');
171                                                         }
172                                                 break;
173                                                 case 13: // enter
174                                                         $(menu).find('LI.hover A').trigger('click');
175                                                 break;
176                                                 case 27: // esc
177                                                         $(document).trigger('click');
178                                                 break
179                                         }
180                                 });
182                                 // When items are selected
183                                 $('#' + o.menu).find('A').unbind('click');
184                                 $('#' + o.menu).find('LI:not(.disabled) A').click( function() {
185                                         $(document).unbind('click').unbind('keypress');
186                                         $(".contextMenu").hide();
187                                         // Callback
188                                         if( typeof callback === "function" ) callback( $(this).attr('href').substr(1), el, {x: x - offset.left, y: y - offset.top, docX: x, docY: y} );
189                                         return false;
190                                 });
192                                 // Hide bindings
193                                 setTimeout( function() { // Delay for Mozilla
194                                         $(document).click( function() {
195                                                 $(document).unbind('click').unbind('keypress');
196                                                 $(menu).fadeOut(o.outSpeed);
197                                                 return false;
198                                         });
199                                 }, 0);
200                         }
201                 });
203                 // Disable text selection
204                 if( $.browser.mozilla ) {
205                         $('#' + o.menu).each( function() { $(this).css({ 'MozUserSelect' : 'none' }); });
206                 } else if( $.browser.msie ) {
207                         $('#' + o.menu).each( function() { $(this).bind('selectstart.disableTextSelect', function() { return false; }); });
208                 } else {
209                         $('#' + o.menu).each(function() { $(this).bind('mousedown.disableTextSelect', function() { return false; }); });
210                 }
211                 // Disable browser context menu (requires both selectors to work in IE/Safari + FF/Chrome)
212                 el.add($('UL.contextMenu')).bind('contextmenu', function() { return false; });
213         };
214         $.extend($.fn, {
215                 contextMenu: function(o, callback, sel) {
216                         // Defaults
217                         if( o.menu == undefined ) return false;
218                         if( o.inSpeed == undefined ) o.inSpeed = 150;
219                         if( o.outSpeed == undefined ) o.outSpeed = 75;
220                         // 0 needs to be -1 for expected results (no fade)
221                         if( o.inSpeed == 0 ) o.inSpeed = -1;
222                         if( o.outSpeed == 0 ) o.outSpeed = -1;
223                         var el = $(this);
224                         el.on('mousedown', sel, function(e) {
225                                 e.stopPropagation();
226                                 if(3 !== e.which) {
227                                         return false;
228                                 }
229                                 show_context_menu($(this), e, o, callback);
230                         });
231                         return $(this);
232                 },
234                 // Disable context menu items on the fly
235                 disableContextMenuItems: function(o) {
236                         if( o == undefined ) {
237                                 // Disable all
238                                 $(this).find('LI').addClass('disabled');
239                                 return( $(this) );
240                         }
241                         $(this).each( function() {
242                                 if( o != undefined ) {
243                                         var d = o.split(',');
244                                         for( var i = 0; i < d.length; i++ ) {
245                                                 $(this).find('A[href="' + d[i] + '"]').parent().addClass('disabled');
247                                         }
248                                 }
249                         });
250                         return( $(this) );
251                 },
253                 // Enable context menu items on the fly
254                 enableContextMenuItems: function(o) {
255                         if( o == undefined ) {
256                                 // Enable all
257                                 $(this).find('LI.disabled').removeClass('disabled');
258                                 return( $(this) );
259                         }
260                         $(this).each( function() {
261                                 if( o != undefined ) {
262                                         var d = o.split(',');
263                                         for( var i = 0; i < d.length; i++ ) {
264                                                 $(this).find('A[href="' + d[i] + '"]').parent().removeClass('disabled');
266                                         }
267                                 }
268                         });
269                         return( $(this) );
270                 },
272                 // Disable context menu(s)
273                 disableContextMenu: function() {
274                         $(this).each( function() {
275                                 $(this).addClass('disabled');
276                         });
277                         return( $(this) );
278                 },
280                 // Enable context menu(s)
281                 enableContextMenu: function() {
282                         $(this).each( function() {
283                                 $(this).removeClass('disabled');
284                         });
285                         return( $(this) );
286                 },
288                 // Destroy context menu(s)
289                 destroyContextMenu: function() {
290                         // Destroy specified context menus
291                         $(this).each( function() {
292                                 // Disable action
293                                 $(this).unbind('mousedown').unbind('mouseup');
294                         });
295                         return( $(this) );
296                 }
298         });
299 })(jQuery);