1 // jQuery Context Menu Plugin
6 // A Beautiful Site (http://abeautifulsite.net/)
8 // More info: http://abeautifulsite.net/2008/09/jquery-context-menu-plugin/
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
22 evt
.stopPropagation();
23 el
.mouseup( function(e
) {
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('|');
44 switch(parts
.length
) {
45 case 0: case 1: return false;
47 case 2: // host or groups
56 if (service
!= false) {
57 name
= name
+ '__' + service
;
60 name
= name
.replace(/[^a-zA-Z0-9-_]/g, '_');
62 var obj_prop
= $('._' + name
).text();
66 var NOTIFICATIONS_ENABLED
= 2;
67 var CHECKS_ENABLED
= 4;
70 if (obj_prop
& ACKNOWLEDGED
|| !(obj_prop
& 16) || obj_prop
& OK
) {
71 $('#_menu_acknowledge_host_problem').hide();
72 $('#_menu_acknowledge_svc_problem').hide();
74 if (!(obj_prop
& ACKNOWLEDGED
)) { // || obj_prop & OK
75 $('#_menu_remove_host_acknowledgement').hide();
76 $('#_menu_remove_svc_acknowledgement').hide();
78 if (obj_prop
& NOTIFICATIONS_ENABLED
) {
79 $('#_menu_disable_host_notifications').hide();
80 $('#_menu_disable_svc_notifications').hide();
82 if (!(obj_prop
& NOTIFICATIONS_ENABLED
)) {
83 $('#_menu_enable_host_notifications').hide();
84 $('#_menu_enable_svc_notifications').hide();
86 if (obj_prop
& CHECKS_ENABLED
) {
87 $('#_menu_disable_host_check').hide();
88 $('#_menu_disable_svc_check').hide();
90 if (!(obj_prop
& CHECKS_ENABLED
)) {
91 $('#_menu_enable_host_check').hide();
92 $('#_menu_enable_svc_check').hide();
94 if (obj_prop
& SCHEDULED_DT
) {
95 $('#_menu_schedule_host_downtime').hide();
96 $('#_menu_schedule_svc_downtime').hide();
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();
110 if(tr_to_examine
.find('.service_hostname img[title="Scheduled downtime"]').length
> 0) {
111 $('#_menu_removeschedule_svc_downtime').hide();
117 // Detect mouse position
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
;
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();
145 $(menu
).css({ top
: y
, left
: x
, position
: 'fixed'}).fadeIn(o
.inSpeed
);
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');
155 $(document
).keypress( function(e
) {
156 switch( e
.keyCode
) {
158 if( $(menu
).find('LI.hover').size() == 0 ) {
159 $(menu
).find('LI:last').addClass('hover');
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');
166 if( $(menu
).find('LI.hover').size() == 0 ) {
167 $(menu
).find('LI:first').addClass('hover');
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');
174 $(menu
).find('LI.hover A').trigger('click');
177 $(document
).trigger('click');
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();
188 if( typeof callback
=== "function" ) callback( $(this).attr('href').substr(1), el
, {x
: x
- offset
.left
, y
: y
- offset
.top
, docX
: x
, docY
: y
} );
193 setTimeout( function() { // Delay for Mozilla
194 $(document
).click( function() {
195 $(document
).unbind('click').unbind('keypress');
196 $(menu
).fadeOut(o
.outSpeed
);
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; }); });
209 $('#' + o
.menu
).each(function() { $(this).bind('mousedown.disableTextSelect', function() { return false; }); });
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; });
215 contextMenu: function(o
, callback
, sel
) {
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;
224 el
.on('mousedown', sel
, function(e
) {
229 show_context_menu($(this), e
, o
, callback
);
234 // Disable context menu items on the fly
235 disableContextMenuItems: function(o
) {
236 if( o
== undefined ) {
238 $(this).find('LI').addClass('disabled');
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');
253 // Enable context menu items on the fly
254 enableContextMenuItems: function(o
) {
255 if( o
== undefined ) {
257 $(this).find('LI.disabled').removeClass('disabled');
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');
272 // Disable context menu(s)
273 disableContextMenu: function() {
274 $(this).each( function() {
275 $(this).addClass('disabled');
280 // Enable context menu(s)
281 enableContextMenu: function() {
282 $(this).each( function() {
283 $(this).removeClass('disabled');
288 // Destroy context menu(s)
289 destroyContextMenu: function() {
290 // Destroy specified context menus
291 $(this).each( function() {
293 $(this).unbind('mousedown').unbind('mouseup');