Web Development with jQuery®

(Elliott) #1

(^264) ❘ CHAPTER 9 PLUGINS
A mousedown event, again namespaced with the name contextMenu, is attached to the document.
Whenever a mousedown event occurs, every

element with the class name contextMenu is iterated.
$(document).bind(
'mousedown.contextMenu',
function()
{
$('div.contextMenu').each(
function()
{
if (!$(this).data('contextMenu'))
{
$(this).hide();
}
}
);
}
);
If any of those
elements doesn’t have its contextMenu data set to true, then that
element
is hidden. Remember earlier in the script that you attached a mouseover and mouseout event to the
context menu to track whether the context menu is active, which was done by setting the data con-
textMenu to a boolean value. This bit of code completes that implementation and makes it possible to
close the context menu just by clicking anywhere but on the context menu.
The last block of code is executed when the context menu is disabled, which is done when you click
the Disable Context Menu option.
$('span#applicationContextMenuDisable').click(
function(event)
{
$('div#applicationContextMenu').contextMenu('disable');
$('div#applicationContextMenu').hide();
}
);
The preceding code causes the last block of code, which disables the context menu, to execute.
}
else
{
this.find('li')
.not('li.contextMenuDisabled, li.contextMenuSeparator')
.unbind('mouseover.contextMenu')
.unbind('mouseout.contextMenu');
this.data('contextMenu', false)
.removeClass('contextMenu')
.unbind('mouseover.contextMenu')
.unbind('mouseout.contextMenu');
this.parents('.contextMenuContainer:first')
.unbind('contextmenu.contextMenu');
$('body').data('contextMenu', false);
http://www.it-ebooks.info