{"version":3,"file":"jquery.contextMenu.min.js","sources":["jquery.contextMenu.min.js"],"sourcesContent":["/**\r\n * jQuery contextMenu v2.9.2 - Plugin for simple contextMenu handling\r\n *\r\n * Version: v2.9.2\r\n *\r\n * Authors: Björn Brala (SWIS.nl), Rodney Rehm, Addy Osmani (patches for FF)\r\n * Web: http://swisnl.github.io/jQuery-contextMenu/\r\n *\r\n * Copyright (c) 2011-2020 SWIS BV and contributors\r\n *\r\n * Licensed under\r\n * MIT License http://www.opensource.org/licenses/mit-license\r\n *\r\n * Date: 2020-05-13T13:55:36.983Z\r\n */\r\n\r\n// jscs:disable\r\n/* jshint ignore:start */\r\n(function (factory) {\r\n if (typeof define === 'function' && define.amd) {\r\n // AMD. Register as anonymous module.\r\n define(['jquery'], factory);\r\n } else if (typeof exports === 'object') {\r\n // Node / CommonJS\r\n factory(require('jquery'));\r\n } else {\r\n // Browser globals.\r\n factory(jQuery);\r\n }\r\n})(function ($) {\r\n\r\n 'use strict';\r\n\r\n // TODO: -\r\n // ARIA stuff: menuitem, menuitemcheckbox und menuitemradio\r\n // create structure if $.support[htmlCommand || htmlMenuitem] and !opt.disableNative\r\n\r\n // determine html5 compatibility\r\n $.support.htmlMenuitem = ('HTMLMenuItemElement' in window);\r\n $.support.htmlCommand = ('HTMLCommandElement' in window);\r\n $.support.eventSelectstart = ('onselectstart' in document.documentElement);\r\n /* // should the need arise, test for css user-select\r\n $.support.cssUserSelect = (function(){\r\n var t = false,\r\n e = document.createElement('div');\r\n\r\n $.each('Moz|Webkit|Khtml|O|ms|Icab|'.split('|'), function(i, prefix) {\r\n var propCC = prefix + (prefix ? 'U' : 'u') + 'serSelect',\r\n prop = (prefix ? ('-' + prefix.toLowerCase() + '-') : '') + 'user-select';\r\n\r\n e.style.cssText = prop + ': text;';\r\n if (e.style[propCC] == 'text') {\r\n t = true;\r\n return false;\r\n }\r\n\r\n return true;\r\n });\r\n\r\n return t;\r\n })();\r\n */\r\n\r\n\r\n if (!$.ui || !$.widget) {\r\n // duck punch $.cleanData like jQueryUI does to get that remove event\r\n $.cleanData = (function (orig) {\r\n return function (elems) {\r\n var events, elem, i;\r\n for (i = 0; elems[i] != null; i++) {\r\n elem = elems[i];\r\n try {\r\n // Only trigger remove when necessary to save time\r\n events = $._data(elem, 'events');\r\n if (events && events.remove) {\r\n $(elem).triggerHandler('remove');\r\n }\r\n\r\n // Http://bugs.jquery.com/ticket/8235\r\n } catch (e) {\r\n }\r\n }\r\n orig(elems);\r\n };\r\n })($.cleanData);\r\n }\r\n /* jshint ignore:end */\r\n // jscs:enable\r\n\r\n var // currently active contextMenu trigger\r\n $currentTrigger = null,\r\n // is contextMenu initialized with at least one menu?\r\n initialized = false,\r\n // window handle\r\n $win = $(window),\r\n // number of registered menus\r\n counter = 0,\r\n // mapping selector to namespace\r\n namespaces = {},\r\n // mapping namespace to options\r\n menus = {},\r\n // custom command type handlers\r\n types = {},\r\n // default values\r\n defaults = {\r\n // selector of contextMenu trigger\r\n selector: null,\r\n // where to append the menu to\r\n appendTo: null,\r\n // method to trigger context menu [\"right\", \"left\", \"hover\"]\r\n trigger: 'right',\r\n // hide menu when mouse leaves trigger / menu elements\r\n autoHide: false,\r\n // ms to wait before showing a hover-triggered context menu\r\n delay: 200,\r\n // flag denoting if a second trigger should simply move (true) or rebuild (false) an open menu\r\n // as long as the trigger happened on one of the trigger-element's child nodes\r\n reposition: true,\r\n // Flag denoting if a second trigger should close the menu, as long as\r\n // the trigger happened on one of the trigger-element's child nodes.\r\n // This overrides the reposition option.\r\n hideOnSecondTrigger: false,\r\n\r\n //ability to select submenu\r\n selectableSubMenu: false,\r\n\r\n // Default classname configuration to be able avoid conflicts in frameworks\r\n classNames: {\r\n hover: 'context-menu-hover', // Item hover\r\n disabled: 'context-menu-disabled', // Item disabled\r\n visible: 'context-menu-visible', // Item visible\r\n notSelectable: 'context-menu-not-selectable', // Item not selectable\r\n\r\n icon: 'context-menu-icon',\r\n iconEdit: 'context-menu-icon-edit',\r\n iconCut: 'context-menu-icon-cut',\r\n iconCopy: 'context-menu-icon-copy',\r\n iconPaste: 'context-menu-icon-paste',\r\n iconDelete: 'context-menu-icon-delete',\r\n iconAdd: 'context-menu-icon-add',\r\n iconQuit: 'context-menu-icon-quit',\r\n iconLoadingClass: 'context-menu-icon-loading'\r\n },\r\n\r\n // determine position to show menu at\r\n determinePosition: function ($menu) {\r\n // position to the lower middle of the trigger element\r\n if ($.ui && $.ui.position) {\r\n // .position() is provided as a jQuery UI utility\r\n // (...and it won't work on hidden elements)\r\n $menu.css('display', 'block').position({\r\n my: 'center top',\r\n at: 'center bottom',\r\n of: this,\r\n offset: '0 5',\r\n collision: 'fit'\r\n }).css('display', 'none');\r\n } else {\r\n // determine contextMenu position\r\n var offset = this.offset();\r\n offset.top += this.outerHeight();\r\n offset.left += this.outerWidth() / 2 - $menu.outerWidth() / 2;\r\n $menu.css(offset);\r\n }\r\n },\r\n // position menu\r\n position: function (opt, x, y) {\r\n var offset;\r\n // determine contextMenu position\r\n if (!x && !y) {\r\n opt.determinePosition.call(this, opt.$menu);\r\n return;\r\n } else if (x === 'maintain' && y === 'maintain') {\r\n // x and y must not be changed (after re-show on command click)\r\n offset = opt.$menu.position();\r\n } else {\r\n // x and y are given (by mouse event)\r\n var offsetParentOffset = opt.$menu.offsetParent().offset();\r\n offset = {top: y - offsetParentOffset.top, left: x -offsetParentOffset.left};\r\n }\r\n\r\n // correct offset if viewport demands it\r\n var bottom = $win.scrollTop() + $win.height(),\r\n right = $win.scrollLeft() + $win.width(),\r\n height = opt.$menu.outerHeight(),\r\n width = opt.$menu.outerWidth();\r\n\r\n if (offset.top + height > bottom) {\r\n offset.top -= height;\r\n }\r\n\r\n if (offset.top < 0) {\r\n offset.top = 0;\r\n }\r\n\r\n if (offset.left + width > right) {\r\n offset.left -= width;\r\n }\r\n\r\n if (offset.left < 0) {\r\n offset.left = 0;\r\n }\r\n\r\n opt.$menu.css(offset);\r\n },\r\n // position the sub-menu\r\n positionSubmenu: function ($menu) {\r\n if (typeof $menu === 'undefined') {\r\n // When user hovers over item (which has sub items) handle.focusItem will call this.\r\n // but the submenu does not exist yet if opt.items is a promise. just return, will\r\n // call positionSubmenu after promise is completed.\r\n return;\r\n }\r\n if ($.ui && $.ui.position) {\r\n // .position() is provided as a jQuery UI utility\r\n // (...and it won't work on hidden elements)\r\n $menu.css('display', 'block').position({\r\n my: 'left top-5',\r\n at: 'right top',\r\n of: this,\r\n collision: 'flipfit fit'\r\n }).css('display', '');\r\n } else {\r\n // determine contextMenu position\r\n var offset = {\r\n top: -9,\r\n left: this.outerWidth() - 5\r\n };\r\n $menu.css(offset);\r\n }\r\n },\r\n // offset to add to zIndex\r\n zIndex: 1,\r\n // show hide animation settings\r\n animation: {\r\n duration: 50,\r\n show: 'slideDown',\r\n hide: 'slideUp'\r\n },\r\n // events\r\n events: {\r\n preShow: $.noop,\r\n show: $.noop,\r\n hide: $.noop,\r\n activated: $.noop\r\n },\r\n // default callback\r\n callback: null,\r\n // list of contextMenu items\r\n items: {}\r\n },\r\n // mouse position for hover activation\r\n hoveract = {\r\n timer: null,\r\n pageX: null,\r\n pageY: null\r\n },\r\n // determine zIndex\r\n zindex = function ($t) {\r\n var zin = 0,\r\n $tt = $t;\r\n\r\n while (true) {\r\n zin = Math.max(zin, parseInt($tt.css('z-index'), 10) || 0);\r\n $tt = $tt.parent();\r\n if (!$tt || !$tt.length || 'html body'.indexOf($tt.prop('nodeName').toLowerCase()) > -1) {\r\n break;\r\n }\r\n }\r\n return zin;\r\n },\r\n // event handlers\r\n handle = {\r\n // abort anything\r\n abortevent: function (e) {\r\n e.preventDefault();\r\n e.stopImmediatePropagation();\r\n },\r\n // contextmenu show dispatcher\r\n contextmenu: function (e) {\r\n var $this = $(this);\r\n\r\n //Show browser context-menu when preShow returns false\r\n if (e.data.events.preShow($this,e) === false) {\r\n return;\r\n }\r\n\r\n // disable actual context-menu if we are using the right mouse button as the trigger\r\n if (e.data.trigger === 'right') {\r\n e.preventDefault();\r\n e.stopImmediatePropagation();\r\n }\r\n\r\n // abort native-triggered events unless we're triggering on right click\r\n if ((e.data.trigger !== 'right' && e.data.trigger !== 'demand') && e.originalEvent) {\r\n return;\r\n }\r\n\r\n // Let the current contextmenu decide if it should show or not based on its own trigger settings\r\n if (typeof e.mouseButton !== 'undefined' && e.data) {\r\n if (!(e.data.trigger === 'left' && e.mouseButton === 0) && !(e.data.trigger === 'right' && e.mouseButton === 2)) {\r\n // Mouse click is not valid.\r\n return;\r\n }\r\n }\r\n\r\n // abort event if menu is visible for this trigger\r\n if ($this.hasClass('context-menu-active')) {\r\n return;\r\n }\r\n\r\n if (!$this.hasClass('context-menu-disabled')) {\r\n // theoretically need to fire a show event at \r\n // http://www.whatwg.org/specs/web-apps/current-work/multipage/interactive-elements.html#context-menus\r\n // var evt = jQuery.Event(\"show\", { data: data, pageX: e.pageX, pageY: e.pageY, relatedTarget: this });\r\n // e.data.$menu.trigger(evt);\r\n\r\n $currentTrigger = $this;\r\n if (e.data.build) {\r\n var built = e.data.build($currentTrigger, e);\r\n // abort if build() returned false\r\n if (built === false) {\r\n return;\r\n }\r\n\r\n // dynamically build menu on invocation\r\n e.data = $.extend(true, {}, defaults, e.data, built || {});\r\n\r\n // abort if there are no items to display\r\n if (!e.data.items || $.isEmptyObject(e.data.items)) {\r\n // Note: jQuery captures and ignores errors from event handlers\r\n if (window.console) {\r\n (console.error || console.log).call(console, 'No items specified to show in contextMenu');\r\n }\r\n\r\n throw new Error('No Items specified');\r\n }\r\n\r\n // backreference for custom command type creation\r\n e.data.$trigger = $currentTrigger;\r\n\r\n op.create(e.data);\r\n }\r\n op.show.call($this, e.data, e.pageX, e.pageY);\r\n }\r\n },\r\n // contextMenu left-click trigger\r\n click: function (e) {\r\n e.preventDefault();\r\n e.stopImmediatePropagation();\r\n $(this).trigger($.Event('contextmenu', {data: e.data, pageX: e.pageX, pageY: e.pageY}));\r\n },\r\n // contextMenu right-click trigger\r\n mousedown: function (e) {\r\n // register mouse down\r\n var $this = $(this);\r\n\r\n // hide any previous menus\r\n if ($currentTrigger && $currentTrigger.length && !$currentTrigger.is($this)) {\r\n $currentTrigger.data('contextMenu').$menu.trigger('contextmenu:hide');\r\n }\r\n\r\n // activate on right click\r\n if (e.button === 2) {\r\n $currentTrigger = $this.data('contextMenuActive', true);\r\n }\r\n },\r\n // contextMenu right-click trigger\r\n mouseup: function (e) {\r\n // show menu\r\n var $this = $(this);\r\n if ($this.data('contextMenuActive') && $currentTrigger && $currentTrigger.length && $currentTrigger.is($this) && !$this.hasClass('context-menu-disabled')) {\r\n e.preventDefault();\r\n e.stopImmediatePropagation();\r\n $currentTrigger = $this;\r\n $this.trigger($.Event('contextmenu', {data: e.data, pageX: e.pageX, pageY: e.pageY}));\r\n }\r\n\r\n $this.removeData('contextMenuActive');\r\n },\r\n // contextMenu hover trigger\r\n mouseenter: function (e) {\r\n var $this = $(this),\r\n $related = $(e.relatedTarget),\r\n $document = $(document);\r\n\r\n // abort if we're coming from a menu\r\n if ($related.is('.context-menu-list') || $related.closest('.context-menu-list').length) {\r\n return;\r\n }\r\n\r\n // abort if a menu is shown\r\n if ($currentTrigger && $currentTrigger.length) {\r\n return;\r\n }\r\n\r\n hoveract.pageX = e.pageX;\r\n hoveract.pageY = e.pageY;\r\n hoveract.data = e.data;\r\n $document.on('mousemove.contextMenuShow', handle.mousemove);\r\n hoveract.timer = setTimeout(function () {\r\n hoveract.timer = null;\r\n $document.off('mousemove.contextMenuShow');\r\n $currentTrigger = $this;\r\n $this.trigger($.Event('contextmenu', {\r\n data: hoveract.data,\r\n pageX: hoveract.pageX,\r\n pageY: hoveract.pageY\r\n }));\r\n }, e.data.delay);\r\n },\r\n // contextMenu hover trigger\r\n mousemove: function (e) {\r\n hoveract.pageX = e.pageX;\r\n hoveract.pageY = e.pageY;\r\n },\r\n // contextMenu hover trigger\r\n mouseleave: function (e) {\r\n // abort if we're leaving for a menu\r\n var $related = $(e.relatedTarget);\r\n if ($related.is('.context-menu-list') || $related.closest('.context-menu-list').length) {\r\n return;\r\n }\r\n\r\n try {\r\n clearTimeout(hoveract.timer);\r\n } catch (e) {\r\n }\r\n\r\n hoveract.timer = null;\r\n },\r\n // click on layer to hide contextMenu\r\n layerClick: function (e) {\r\n var $this = $(this),\r\n root = $this.data('contextMenuRoot'),\r\n button = e.button,\r\n x = e.pageX,\r\n y = e.pageY,\r\n fakeClick = x === undefined,\r\n target,\r\n offset;\r\n\r\n e.preventDefault();\r\n\r\n setTimeout(function () {\r\n // If the click is not real, things break: https://github.com/swisnl/jQuery-contextMenu/issues/132\r\n if(fakeClick){\r\n if (root !== null && typeof root !== 'undefined' && root.$menu !== null && typeof root.$menu !== 'undefined') {\r\n root.$menu.trigger('contextmenu:hide');\r\n }\r\n return;\r\n }\r\n\r\n var $window;\r\n var triggerAction = ((root.trigger === 'left' && button === 0) || (root.trigger === 'right' && button === 2));\r\n\r\n // find the element that would've been clicked, wasn't the layer in the way\r\n if (document.elementFromPoint && root.$layer) {\r\n root.$layer.hide();\r\n target = document.elementFromPoint(x - $win.scrollLeft(), y - $win.scrollTop());\r\n\r\n // also need to try and focus this element if we're in a contenteditable area,\r\n // as the layer will prevent the browser mouse action we want\r\n if (target !== null && target.isContentEditable) {\r\n var range = document.createRange(),\r\n sel = window.getSelection();\r\n range.selectNode(target);\r\n range.collapse(true);\r\n sel.removeAllRanges();\r\n sel.addRange(range);\r\n }\r\n $(target).trigger(e);\r\n root.$layer.show();\r\n }\r\n\r\n if (root.hideOnSecondTrigger && triggerAction && root.$menu !== null && typeof root.$menu !== 'undefined') {\r\n root.$menu.trigger('contextmenu:hide');\r\n return;\r\n }\r\n\r\n if (root.reposition && triggerAction) {\r\n if (document.elementFromPoint) {\r\n if (root.$trigger.is(target)) {\r\n root.position.call(root.$trigger, root, x, y);\r\n return;\r\n }\r\n } else {\r\n offset = root.$trigger.offset();\r\n $window = $(window);\r\n // while this looks kinda awful, it's the best way to avoid\r\n // unnecessarily calculating any positions\r\n offset.top += $window.scrollTop();\r\n if (offset.top <= e.pageY) {\r\n offset.left += $window.scrollLeft();\r\n if (offset.left <= e.pageX) {\r\n offset.bottom = offset.top + root.$trigger.outerHeight();\r\n if (offset.bottom >= e.pageY) {\r\n offset.right = offset.left + root.$trigger.outerWidth();\r\n if (offset.right >= e.pageX) {\r\n // reposition\r\n root.position.call(root.$trigger, root, x, y);\r\n return;\r\n }\r\n }\r\n }\r\n }\r\n }\r\n }\r\n\r\n if (target && triggerAction) {\r\n root.$trigger.one('contextmenu:hidden', function () {\r\n $(target).contextMenu({x: x, y: y, button: button});\r\n });\r\n }\r\n\r\n if (root !== null && typeof root !== 'undefined' && root.$menu !== null && typeof root.$menu !== 'undefined') {\r\n root.$menu.trigger('contextmenu:hide');\r\n }\r\n }, 50);\r\n },\r\n // key handled :hover\r\n keyStop: function (e, opt) {\r\n if (!opt.isInput) {\r\n e.preventDefault();\r\n }\r\n\r\n e.stopPropagation();\r\n },\r\n key: function (e) {\r\n\r\n var opt = {};\r\n\r\n // Only get the data from $currentTrigger if it exists\r\n if ($currentTrigger) {\r\n opt = $currentTrigger.data('contextMenu') || {};\r\n }\r\n // If the trigger happen on a element that are above the contextmenu do this\r\n if (typeof opt.zIndex === 'undefined') {\r\n opt.zIndex = 0;\r\n }\r\n var targetZIndex = 0;\r\n var getZIndexOfTriggerTarget = function (target) {\r\n if (target.style.zIndex !== '') {\r\n targetZIndex = target.style.zIndex;\r\n } else {\r\n if (target.offsetParent !== null && typeof target.offsetParent !== 'undefined') {\r\n getZIndexOfTriggerTarget(target.offsetParent);\r\n }\r\n else if (target.parentElement !== null && typeof target.parentElement !== 'undefined') {\r\n getZIndexOfTriggerTarget(target.parentElement);\r\n }\r\n }\r\n };\r\n getZIndexOfTriggerTarget(e.target);\r\n // If targetZIndex is heigher then opt.zIndex dont progress any futher.\r\n // This is used to make sure that if you are using a dialog with a input / textarea / contenteditable div\r\n // and its above the contextmenu it wont steal keys events\r\n if (opt.$menu && parseInt(targetZIndex,10) > parseInt(opt.$menu.css(\"zIndex\"),10)) {\r\n return;\r\n }\r\n switch (e.keyCode) {\r\n case 9:\r\n case 38: // up\r\n handle.keyStop(e, opt);\r\n // if keyCode is [38 (up)] or [9 (tab) with shift]\r\n if (opt.isInput) {\r\n if (e.keyCode === 9 && e.shiftKey) {\r\n e.preventDefault();\r\n if (opt.$selected) {\r\n opt.$selected.find('input, textarea, select').blur();\r\n }\r\n if (opt.$menu !== null && typeof opt.$menu !== 'undefined') {\r\n opt.$menu.trigger('prevcommand');\r\n }\r\n return;\r\n } else if (e.keyCode === 38 && opt.$selected.find('input, textarea, select').prop('type') === 'checkbox') {\r\n // checkboxes don't capture this key\r\n e.preventDefault();\r\n return;\r\n }\r\n } else if (e.keyCode !== 9 || e.shiftKey) {\r\n if (opt.$menu !== null && typeof opt.$menu !== 'undefined') {\r\n opt.$menu.trigger('prevcommand');\r\n }\r\n return;\r\n }\r\n break;\r\n // omitting break;\r\n // case 9: // tab - reached through omitted break;\r\n case 40: // down\r\n handle.keyStop(e, opt);\r\n if (opt.isInput) {\r\n if (e.keyCode === 9) {\r\n e.preventDefault();\r\n if (opt.$selected) {\r\n opt.$selected.find('input, textarea, select').blur();\r\n }\r\n if (opt.$menu !== null && typeof opt.$menu !== 'undefined') {\r\n opt.$menu.trigger('nextcommand');\r\n }\r\n return;\r\n } else if (e.keyCode === 40 && opt.$selected.find('input, textarea, select').prop('type') === 'checkbox') {\r\n // checkboxes don't capture this key\r\n e.preventDefault();\r\n return;\r\n }\r\n } else {\r\n if (opt.$menu !== null && typeof opt.$menu !== 'undefined') {\r\n opt.$menu.trigger('nextcommand');\r\n }\r\n return;\r\n }\r\n break;\r\n\r\n case 37: // left\r\n handle.keyStop(e, opt);\r\n if (opt.isInput || !opt.$selected || !opt.$selected.length) {\r\n break;\r\n }\r\n\r\n if (!opt.$selected.parent().hasClass('context-menu-root')) {\r\n var $parent = opt.$selected.parent().parent();\r\n opt.$selected.trigger('contextmenu:blur');\r\n opt.$selected = $parent;\r\n return;\r\n }\r\n break;\r\n\r\n case 39: // right\r\n handle.keyStop(e, opt);\r\n if (opt.isInput || !opt.$selected || !opt.$selected.length) {\r\n break;\r\n }\r\n\r\n var itemdata = opt.$selected.data('contextMenu') || {};\r\n if (itemdata.$menu && opt.$selected.hasClass('context-menu-submenu')) {\r\n opt.$selected = null;\r\n itemdata.$selected = null;\r\n itemdata.$menu.trigger('nextcommand');\r\n return;\r\n }\r\n break;\r\n\r\n case 35: // end\r\n case 36: // home\r\n if (opt.$selected && opt.$selected.find('input, textarea, select').length) {\r\n return;\r\n } else {\r\n (opt.$selected && opt.$selected.parent() || opt.$menu)\r\n .children(':not(.' + opt.classNames.disabled + ', .' + opt.classNames.notSelectable + ')')[e.keyCode === 36 ? 'first' : 'last']()\r\n .trigger('contextmenu:focus');\r\n e.preventDefault();\r\n return;\r\n }\r\n break;\r\n\r\n case 13: // enter\r\n handle.keyStop(e, opt);\r\n if (opt.isInput) {\r\n if (opt.$selected && !opt.$selected.is('textarea, select')) {\r\n e.preventDefault();\r\n return;\r\n }\r\n break;\r\n }\r\n if (typeof opt.$selected !== 'undefined' && opt.$selected !== null) {\r\n opt.$selected.trigger('mouseup');\r\n }\r\n return;\r\n\r\n case 32: // space\r\n case 33: // page up\r\n case 34: // page down\r\n // prevent browser from scrolling down while menu is visible\r\n handle.keyStop(e, opt);\r\n return;\r\n\r\n case 27: // esc\r\n handle.keyStop(e, opt);\r\n if (opt.$menu !== null && typeof opt.$menu !== 'undefined') {\r\n opt.$menu.trigger('contextmenu:hide');\r\n }\r\n return;\r\n\r\n default: // 0-9, a-z\r\n var k = (String.fromCharCode(e.keyCode)).toUpperCase();\r\n if (opt.accesskeys && opt.accesskeys[k]) {\r\n // according to the specs accesskeys must be invoked immediately\r\n opt.accesskeys[k].$node.trigger(opt.accesskeys[k].$menu ? 'contextmenu:focus' : 'mouseup');\r\n return;\r\n }\r\n break;\r\n }\r\n // pass event to selected item,\r\n // stop propagation to avoid endless recursion\r\n e.stopPropagation();\r\n if (typeof opt.$selected !== 'undefined' && opt.$selected !== null) {\r\n opt.$selected.trigger(e);\r\n }\r\n },\r\n // select previous possible command in menu\r\n prevItem: function (e) {\r\n e.stopPropagation();\r\n var opt = $(this).data('contextMenu') || {};\r\n var root = $(this).data('contextMenuRoot') || {};\r\n\r\n // obtain currently selected menu\r\n if (opt.$selected) {\r\n var $s = opt.$selected;\r\n opt = opt.$selected.parent().data('contextMenu') || {};\r\n opt.$selected = $s;\r\n }\r\n\r\n var $children = opt.$menu.children(),\r\n $prev = !opt.$selected || !opt.$selected.prev().length ? $children.last() : opt.$selected.prev(),\r\n $round = $prev;\r\n\r\n // skip disabled or hidden elements\r\n while ($prev.hasClass(root.classNames.disabled) || $prev.hasClass(root.classNames.notSelectable) || $prev.is(':hidden')) {\r\n if ($prev.prev().length) {\r\n $prev = $prev.prev();\r\n } else {\r\n $prev = $children.last();\r\n }\r\n if ($prev.is($round)) {\r\n // break endless loop\r\n return;\r\n }\r\n }\r\n\r\n // leave current\r\n if (opt.$selected) {\r\n handle.itemMouseleave.call(opt.$selected.get(0), e);\r\n }\r\n\r\n // activate next\r\n handle.itemMouseenter.call($prev.get(0), e);\r\n\r\n // focus input\r\n var $input = $prev.find('input, textarea, select');\r\n if ($input.length) {\r\n $input.focus();\r\n }\r\n },\r\n // select next possible command in menu\r\n nextItem: function (e) {\r\n e.stopPropagation();\r\n var opt = $(this).data('contextMenu') || {};\r\n var root = $(this).data('contextMenuRoot') || {};\r\n\r\n // obtain currently selected menu\r\n if (opt.$selected) {\r\n var $s = opt.$selected;\r\n opt = opt.$selected.parent().data('contextMenu') || {};\r\n opt.$selected = $s;\r\n }\r\n\r\n var $children = opt.$menu.children(),\r\n $next = !opt.$selected || !opt.$selected.next().length ? $children.first() : opt.$selected.next(),\r\n $round = $next;\r\n\r\n // skip disabled\r\n while ($next.hasClass(root.classNames.disabled) || $next.hasClass(root.classNames.notSelectable) || $next.is(':hidden')) {\r\n if ($next.next().length) {\r\n $next = $next.next();\r\n } else {\r\n $next = $children.first();\r\n }\r\n if ($next.is($round)) {\r\n // break endless loop\r\n return;\r\n }\r\n }\r\n\r\n // leave current\r\n if (opt.$selected) {\r\n handle.itemMouseleave.call(opt.$selected.get(0), e);\r\n }\r\n\r\n // activate next\r\n handle.itemMouseenter.call($next.get(0), e);\r\n\r\n // focus input\r\n var $input = $next.find('input, textarea, select');\r\n if ($input.length) {\r\n $input.focus();\r\n }\r\n },\r\n // flag that we're inside an input so the key handler can act accordingly\r\n focusInput: function () {\r\n var $this = $(this).closest('.context-menu-item'),\r\n data = $this.data(),\r\n opt = data.contextMenu,\r\n root = data.contextMenuRoot;\r\n\r\n root.$selected = opt.$selected = $this;\r\n root.isInput = opt.isInput = true;\r\n },\r\n // flag that we're inside an input so the key handler can act accordingly\r\n blurInput: function () {\r\n var $this = $(this).closest('.context-menu-item'),\r\n data = $this.data(),\r\n opt = data.contextMenu,\r\n root = data.contextMenuRoot;\r\n\r\n root.isInput = opt.isInput = false;\r\n },\r\n // :hover on menu\r\n menuMouseenter: function () {\r\n var root = $(this).data().contextMenuRoot;\r\n root.hovering = true;\r\n },\r\n // :hover on menu\r\n menuMouseleave: function (e) {\r\n var root = $(this).data().contextMenuRoot;\r\n if (root.$layer && root.$layer.is(e.relatedTarget)) {\r\n root.hovering = false;\r\n }\r\n },\r\n // :hover done manually so key handling is possible\r\n itemMouseenter: function (e) {\r\n var $this = $(this),\r\n data = $this.data(),\r\n opt = data.contextMenu,\r\n root = data.contextMenuRoot;\r\n\r\n root.hovering = true;\r\n\r\n // abort if we're re-entering\r\n if (e && root.$layer && root.$layer.is(e.relatedTarget)) {\r\n e.preventDefault();\r\n e.stopImmediatePropagation();\r\n }\r\n\r\n // make sure only one item is selected\r\n (opt.$menu ? opt : root).$menu\r\n .children('.' + root.classNames.hover).trigger('contextmenu:blur')\r\n .children('.hover').trigger('contextmenu:blur');\r\n\r\n if ($this.hasClass(root.classNames.disabled) || $this.hasClass(root.classNames.notSelectable)) {\r\n opt.$selected = null;\r\n return;\r\n }\r\n\r\n\r\n $this.trigger('contextmenu:focus');\r\n },\r\n // :hover done manually so key handling is possible\r\n itemMouseleave: function (e) {\r\n var $this = $(this),\r\n data = $this.data(),\r\n opt = data.contextMenu,\r\n root = data.contextMenuRoot;\r\n\r\n if (root !== opt && root.$layer && root.$layer.is(e.relatedTarget)) {\r\n if (typeof root.$selected !== 'undefined' && root.$selected !== null) {\r\n root.$selected.trigger('contextmenu:blur');\r\n }\r\n e.preventDefault();\r\n e.stopImmediatePropagation();\r\n root.$selected = opt.$selected = opt.$node;\r\n return;\r\n }\r\n\r\n if(opt && opt.$menu && opt.$menu.hasClass('context-menu-visible')){\r\n return;\r\n }\r\n\r\n $this.trigger('contextmenu:blur');\r\n },\r\n // contextMenu item click\r\n itemClick: function (e) {\r\n var $this = $(this),\r\n data = $this.data(),\r\n opt = data.contextMenu,\r\n root = data.contextMenuRoot,\r\n key = data.contextMenuKey,\r\n callback;\r\n\r\n // abort if the key is unknown or disabled or is a menu\r\n if (!opt.items[key] || $this.is('.' + root.classNames.disabled + ', .context-menu-separator, .' + root.classNames.notSelectable) || ($this.is('.context-menu-submenu') && root.selectableSubMenu === false )) {\r\n return;\r\n }\r\n\r\n e.preventDefault();\r\n e.stopImmediatePropagation();\r\n\r\n if ($.isFunction(opt.callbacks[key]) && Object.prototype.hasOwnProperty.call(opt.callbacks, key)) {\r\n // item-specific callback\r\n callback = opt.callbacks[key];\r\n } else if ($.isFunction(root.callback)) {\r\n // default callback\r\n callback = root.callback;\r\n } else {\r\n // no callback, no action\r\n return;\r\n }\r\n\r\n // hide menu if callback doesn't stop that\r\n if (callback.call(root.$trigger, key, root, e) !== false) {\r\n root.$menu.trigger('contextmenu:hide');\r\n } else if (root.$menu.parent().length) {\r\n op.update.call(root.$trigger, root);\r\n }\r\n },\r\n // ignore click events on input elements\r\n inputClick: function (e) {\r\n e.stopImmediatePropagation();\r\n },\r\n // hide \r\n hideMenu: function (e, data) {\r\n var root = $(this).data('contextMenuRoot');\r\n op.hide.call(root.$trigger, root, data && data.force);\r\n },\r\n // focus \r\n focusItem: function (e) {\r\n e.stopPropagation();\r\n var $this = $(this),\r\n data = $this.data(),\r\n opt = data.contextMenu,\r\n root = data.contextMenuRoot;\r\n\r\n if ($this.hasClass(root.classNames.disabled) || $this.hasClass(root.classNames.notSelectable)) {\r\n return;\r\n }\r\n\r\n $this\r\n .addClass([root.classNames.hover, root.classNames.visible].join(' '))\r\n // select other items and included items\r\n .parent().find('.context-menu-item').not($this)\r\n .removeClass(root.classNames.visible)\r\n .filter('.' + root.classNames.hover)\r\n .trigger('contextmenu:blur');\r\n\r\n // remember selected\r\n opt.$selected = root.$selected = $this;\r\n\r\n\r\n if(opt && opt.$node && opt.$node.hasClass('context-menu-submenu')){\r\n opt.$node.addClass(root.classNames.hover);\r\n }\r\n\r\n // position sub-menu - do after show so dumb $.ui.position can keep up\r\n if (opt.$node) {\r\n root.positionSubmenu.call(opt.$node, opt.$menu);\r\n }\r\n },\r\n // blur \r\n blurItem: function (e) {\r\n e.stopPropagation();\r\n var $this = $(this),\r\n data = $this.data(),\r\n opt = data.contextMenu,\r\n root = data.contextMenuRoot;\r\n\r\n if (opt.autoHide) { // for tablets and touch screens this needs to remain\r\n $this.removeClass(root.classNames.visible);\r\n }\r\n $this.removeClass(root.classNames.hover);\r\n opt.$selected = null;\r\n }\r\n },\r\n // operations\r\n op = {\r\n show: function (opt, x, y) {\r\n var $trigger = $(this),\r\n css = {};\r\n\r\n // hide any open menus\r\n $('#context-menu-layer').trigger('mousedown');\r\n\r\n // backreference for callbacks\r\n opt.$trigger = $trigger;\r\n\r\n // show event\r\n if (opt.events.show.call($trigger, opt) === false) {\r\n $currentTrigger = null;\r\n return;\r\n }\r\n\r\n // create or update context menu\r\n var hasVisibleItems = op.update.call($trigger, opt);\r\n if (hasVisibleItems === false) {\r\n $currentTrigger = null;\r\n return;\r\n }\r\n\r\n // position menu\r\n opt.position.call($trigger, opt, x, y);\r\n\r\n // make sure we're in front\r\n if (opt.zIndex) {\r\n var additionalZValue = opt.zIndex;\r\n // If opt.zIndex is a function, call the function to get the right zIndex.\r\n if (typeof opt.zIndex === 'function') {\r\n additionalZValue = opt.zIndex.call($trigger, opt);\r\n }\r\n css.zIndex = zindex($trigger) + additionalZValue;\r\n }\r\n\r\n // add layer\r\n op.layer.call(opt.$menu, opt, css.zIndex);\r\n\r\n // adjust sub-menu zIndexes\r\n opt.$menu.find('ul').css('zIndex', css.zIndex + 1);\r\n\r\n // position and show context menu\r\n opt.$menu.css(css)[opt.animation.show](opt.animation.duration, function () {\r\n $trigger.trigger('contextmenu:visible');\r\n\r\n op.activated(opt);\r\n opt.events.activated(opt);\r\n });\r\n // make options available and set state\r\n $trigger\r\n .data('contextMenu', opt)\r\n .addClass('context-menu-active');\r\n\r\n // register key handler\r\n $(document).off('keydown.contextMenu').on('keydown.contextMenu', handle.key);\r\n // register autoHide handler\r\n if (opt.autoHide) {\r\n // mouse position handler\r\n $(document).on('mousemove.contextMenuAutoHide', function (e) {\r\n // need to capture the offset on mousemove,\r\n // since the page might've been scrolled since activation\r\n var pos = $trigger.offset();\r\n pos.right = pos.left + $trigger.outerWidth();\r\n pos.bottom = pos.top + $trigger.outerHeight();\r\n\r\n if (opt.$layer && !opt.hovering && (!(e.pageX >= pos.left && e.pageX <= pos.right) || !(e.pageY >= pos.top && e.pageY <= pos.bottom))) {\r\n /* Additional hover check after short time, you might just miss the edge of the menu */\r\n setTimeout(function () {\r\n if (!opt.hovering && opt.$menu !== null && typeof opt.$menu !== 'undefined') {\r\n opt.$menu.trigger('contextmenu:hide');\r\n }\r\n }, 50);\r\n }\r\n });\r\n }\r\n },\r\n hide: function (opt, force) {\r\n var $trigger = $(this);\r\n if (!opt) {\r\n opt = $trigger.data('contextMenu') || {};\r\n }\r\n\r\n // hide event\r\n if (!force && opt.events && opt.events.hide.call($trigger, opt) === false) {\r\n return;\r\n }\r\n\r\n // remove options and revert state\r\n $trigger\r\n .removeData('contextMenu')\r\n .removeClass('context-menu-active');\r\n\r\n if (opt.$layer) {\r\n // keep layer for a bit so the contextmenu event can be aborted properly by opera\r\n setTimeout((function ($layer) {\r\n return function () {\r\n $layer.remove();\r\n };\r\n })(opt.$layer), 10);\r\n\r\n try {\r\n delete opt.$layer;\r\n } catch (e) {\r\n opt.$layer = null;\r\n }\r\n }\r\n\r\n // remove handle\r\n $currentTrigger = null;\r\n // remove selected\r\n opt.$menu.find('.' + opt.classNames.hover).trigger('contextmenu:blur');\r\n opt.$selected = null;\r\n // collapse all submenus\r\n opt.$menu.find('.' + opt.classNames.visible).removeClass(opt.classNames.visible);\r\n // unregister key and mouse handlers\r\n // $(document).off('.contextMenuAutoHide keydown.contextMenu'); // http://bugs.jquery.com/ticket/10705\r\n $(document).off('.contextMenuAutoHide').off('keydown.contextMenu');\r\n // hide menu\r\n if (opt.$menu) {\r\n opt.$menu[opt.animation.hide](opt.animation.duration, function () {\r\n // tear down dynamically built menu after animation is completed.\r\n if (opt.build) {\r\n opt.$menu.remove();\r\n $.each(opt, function (key) {\r\n switch (key) {\r\n case 'ns':\r\n case 'selector':\r\n case 'build':\r\n case 'trigger':\r\n return true;\r\n\r\n default:\r\n opt[key] = undefined;\r\n try {\r\n delete opt[key];\r\n } catch (e) {\r\n }\r\n return true;\r\n }\r\n });\r\n }\r\n\r\n setTimeout(function () {\r\n $trigger.trigger('contextmenu:hidden');\r\n }, 10);\r\n });\r\n }\r\n },\r\n create: function (opt, root) {\r\n if (typeof root === 'undefined') {\r\n root = opt;\r\n }\r\n\r\n // create contextMenu\r\n opt.$menu = $('
    ').addClass(opt.className || '').data({\r\n 'contextMenu': opt,\r\n 'contextMenuRoot': root\r\n });\r\n if(opt.dataAttr){\r\n $.each(opt.dataAttr, function (key, item) {\r\n opt.$menu.attr('data-' + opt.key, item);\r\n });\r\n }\r\n\r\n $.each(['callbacks', 'commands', 'inputs'], function (i, k) {\r\n opt[k] = {};\r\n if (!root[k]) {\r\n root[k] = {};\r\n }\r\n });\r\n\r\n if (!root.accesskeys) {\r\n root.accesskeys = {};\r\n }\r\n\r\n function createNameNode(item) {\r\n var $name = $('');\r\n if (item._accesskey) {\r\n if (item._beforeAccesskey) {\r\n $name.append(document.createTextNode(item._beforeAccesskey));\r\n }\r\n $('')\r\n .addClass('context-menu-accesskey')\r\n .text(item._accesskey)\r\n .appendTo($name);\r\n if (item._afterAccesskey) {\r\n $name.append(document.createTextNode(item._afterAccesskey));\r\n }\r\n } else {\r\n if (item.isHtmlName) {\r\n // restrict use with access keys\r\n if (typeof item.accesskey !== 'undefined') {\r\n throw new Error('accesskeys are not compatible with HTML names and cannot be used together in the same item');\r\n }\r\n $name.html(item.name);\r\n } else {\r\n $name.text(item.name);\r\n }\r\n }\r\n return $name;\r\n }\r\n\r\n // create contextMenu items\r\n $.each(opt.items, function (key, item) {\r\n var $t = $('
  • ').addClass(item.className || ''),\r\n $label = null,\r\n $input = null;\r\n\r\n // iOS needs to see a click-event bound to an element to actually\r\n // have the TouchEvents infrastructure trigger the click event\r\n $t.on('click', $.noop);\r\n\r\n // Make old school string seperator a real item so checks wont be\r\n // akward later.\r\n // And normalize 'cm_separator' into 'cm_seperator'.\r\n if (typeof item === 'string' || item.type === 'cm_separator') {\r\n item = {type: 'cm_seperator'};\r\n }\r\n\r\n item.$node = $t.data({\r\n 'contextMenu': opt,\r\n 'contextMenuRoot': root,\r\n 'contextMenuKey': key\r\n });\r\n\r\n // register accesskey\r\n // NOTE: the accesskey attribute should be applicable to any element, but Safari5 and Chrome13 still can't do that\r\n if (typeof item.accesskey !== 'undefined') {\r\n var aks = splitAccesskey(item.accesskey);\r\n for (var i = 0, ak; ak = aks[i]; i++) {\r\n if (!root.accesskeys[ak]) {\r\n root.accesskeys[ak] = item;\r\n var matched = item.name.match(new RegExp('^(.*?)(' + ak + ')(.*)$', 'i'));\r\n if (matched) {\r\n item._beforeAccesskey = matched[1];\r\n item._accesskey = matched[2];\r\n item._afterAccesskey = matched[3];\r\n }\r\n break;\r\n }\r\n }\r\n }\r\n\r\n if (item.type && types[item.type]) {\r\n // run custom type handler\r\n types[item.type].call($t, item, opt, root);\r\n // register commands\r\n $.each([opt, root], function (i, k) {\r\n k.commands[key] = item;\r\n // Overwrite only if undefined or the item is appended to the root. This so it\r\n // doesn't overwrite callbacks of root elements if the name is the same.\r\n if ($.isFunction(item.callback) && (typeof k.callbacks[key] === 'undefined' || typeof opt.type === 'undefined')) {\r\n k.callbacks[key] = item.callback;\r\n }\r\n });\r\n } else {\r\n // add label for input\r\n if (item.type === 'cm_seperator') {\r\n $t.addClass('context-menu-separator ' + root.classNames.notSelectable);\r\n } else if (item.type === 'html') {\r\n $t.addClass('context-menu-html ' + root.classNames.notSelectable);\r\n } else if (item.type !== 'sub' && item.type) {\r\n $label = $('').appendTo($t);\r\n createNameNode(item).appendTo($label);\r\n\r\n $t.addClass('context-menu-input');\r\n opt.hasTypes = true;\r\n $.each([opt, root], function (i, k) {\r\n k.commands[key] = item;\r\n k.inputs[key] = item;\r\n });\r\n } else if (item.items) {\r\n item.type = 'sub';\r\n }\r\n\r\n switch (item.type) {\r\n case 'cm_seperator':\r\n break;\r\n\r\n case 'text':\r\n $input = $('')\r\n .attr('name', 'context-menu-input-' + key)\r\n .val(item.value || '')\r\n .appendTo($label);\r\n break;\r\n\r\n case 'textarea':\r\n $input = $('')\r\n .attr('name', 'context-menu-input-' + key)\r\n .val(item.value || '')\r\n .appendTo($label);\r\n\r\n if (item.height) {\r\n $input.height(item.height);\r\n }\r\n break;\r\n\r\n case 'checkbox':\r\n $input = $('')\r\n .attr('name', 'context-menu-input-' + key)\r\n .val(item.value || '')\r\n .prop('checked', !!item.selected)\r\n .prependTo($label);\r\n break;\r\n\r\n case 'radio':\r\n $input = $('')\r\n .attr('name', 'context-menu-input-' + item.radio)\r\n .val(item.value || '')\r\n .prop('checked', !!item.selected)\r\n .prependTo($label);\r\n break;\r\n\r\n case 'select':\r\n $input = $('')\r\n .attr('name', 'context-menu-input-' + key)\r\n .appendTo($label);\r\n if (item.options) {\r\n $.each(item.options, function (value, text) {\r\n $('').val(value).text(text).appendTo($input);\r\n });\r\n $input.val(item.selected);\r\n }\r\n break;\r\n\r\n case 'sub':\r\n createNameNode(item).appendTo($t);\r\n item.appendTo = item.$node;\r\n $t.data('contextMenu', item).addClass('context-menu-submenu');\r\n item.callback = null;\r\n\r\n // If item contains items, and this is a promise, we should create it later\r\n // check if subitems is of type promise. If it is a promise we need to create\r\n // it later, after promise has been resolved.\r\n if ('function' === typeof item.items.then) {\r\n // probably a promise, process it, when completed it will create the sub menu's.\r\n op.processPromises(item, root, item.items);\r\n } else {\r\n // normal submenu.\r\n op.create(item, root);\r\n }\r\n break;\r\n\r\n case 'html':\r\n $(item.html).appendTo($t);\r\n break;\r\n\r\n default:\r\n $.each([opt, root], function (i, k) {\r\n k.commands[key] = item;\r\n // Overwrite only if undefined or the item is appended to the root. This so it\r\n // doesn't overwrite callbacks of root elements if the name is the same.\r\n if ($.isFunction(item.callback) && (typeof k.callbacks[key] === 'undefined' || typeof opt.type === 'undefined')) {\r\n k.callbacks[key] = item.callback;\r\n }\r\n });\r\n createNameNode(item).appendTo($t);\r\n break;\r\n }\r\n\r\n // disable key listener in \r\n if (item.type && item.type !== 'sub' && item.type !== 'html' && item.type !== 'cm_seperator') {\r\n $input\r\n .on('focus', handle.focusInput)\r\n .on('blur', handle.blurInput);\r\n\r\n if (item.events) {\r\n $input.on(item.events, opt);\r\n }\r\n }\r\n\r\n // add icons\r\n if (item.icon) {\r\n if ($.isFunction(item.icon)) {\r\n item._icon = item.icon.call(this, this, $t, key, item);\r\n } else {\r\n if (typeof(item.icon) === 'string' && (\r\n item.icon.substring(0, 4) === 'fab '\r\n || item.icon.substring(0, 4) === 'fas '\r\n || item.icon.substring(0, 4) === 'fad '\r\n || item.icon.substring(0, 4) === 'far '\r\n || item.icon.substring(0, 4) === 'fal ')\r\n ) {\r\n // to enable font awesome\r\n $t.addClass(root.classNames.icon + ' ' + root.classNames.icon + '--fa5');\r\n item._icon = $('');\r\n } else if (typeof(item.icon) === 'string' && item.icon.substring(0, 3) === 'fa-') {\r\n item._icon = root.classNames.icon + ' ' + root.classNames.icon + '--fa fa ' + item.icon;\r\n } else {\r\n item._icon = root.classNames.icon + ' ' + root.classNames.icon + '-' + item.icon;\r\n }\r\n }\r\n\r\n if(typeof(item._icon) === \"string\"){\r\n $t.addClass(item._icon);\r\n } else {\r\n $t.prepend(item._icon);\r\n }\r\n }\r\n }\r\n\r\n // cache contained elements\r\n item.$input = $input;\r\n item.$label = $label;\r\n\r\n // attach item to menu\r\n $t.appendTo(opt.$menu);\r\n\r\n // Disable text selection\r\n if (!opt.hasTypes && $.support.eventSelectstart) {\r\n // browsers support user-select: none,\r\n // IE has a special event for text-selection\r\n // browsers supporting neither will not be preventing text-selection\r\n $t.on('selectstart.disableTextSelect', handle.abortevent);\r\n }\r\n });\r\n // attach contextMenu to (to bypass any possible overflow:hidden issues on parents of the trigger element)\r\n if (!opt.$node) {\r\n opt.$menu.css('display', 'none').addClass('context-menu-root');\r\n }\r\n opt.$menu.appendTo(opt.appendTo || document.body);\r\n },\r\n resize: function ($menu, nested) {\r\n var domMenu;\r\n // determine widths of submenus, as CSS won't grow them automatically\r\n // position:absolute within position:absolute; min-width:100; max-width:200; results in width: 100;\r\n // kinda sucks hard...\r\n\r\n // determine width of absolutely positioned element\r\n $menu.css({position: 'absolute', display: 'block'});\r\n // don't apply yet, because that would break nested elements' widths\r\n $menu.data('width',\r\n (domMenu = $menu.get(0)).getBoundingClientRect ?\r\n Math.ceil(domMenu.getBoundingClientRect().width) :\r\n $menu.outerWidth() + 1); // outerWidth() returns rounded pixels\r\n // reset styles so they allow nested elements to grow/shrink naturally\r\n $menu.css({\r\n position: 'static',\r\n minWidth: '0px',\r\n maxWidth: '100000px'\r\n });\r\n // identify width of nested menus\r\n $menu.find('> li > ul').each(function () {\r\n op.resize($(this), true);\r\n });\r\n // reset and apply changes in the end because nested\r\n // elements' widths wouldn't be calculatable otherwise\r\n if (!nested) {\r\n $menu.find('ul').addBack().css({\r\n position: '',\r\n display: '',\r\n minWidth: '',\r\n maxWidth: ''\r\n }).outerWidth(function () {\r\n return $(this).data('width');\r\n });\r\n }\r\n },\r\n update: function (opt, root) {\r\n var $trigger = this;\r\n if (typeof root === 'undefined') {\r\n root = opt;\r\n op.resize(opt.$menu);\r\n }\r\n\r\n var hasVisibleItems = false;\r\n\r\n // re-check disabled for each item\r\n opt.$menu.children().each(function () {\r\n var $item = $(this),\r\n key = $item.data('contextMenuKey'),\r\n item = opt.items[key],\r\n disabled = ($.isFunction(item.disabled) && item.disabled.call($trigger, key, root)) || item.disabled === true,\r\n visible;\r\n if ($.isFunction(item.visible)) {\r\n visible = item.visible.call($trigger, key, root);\r\n } else if (typeof item.visible !== 'undefined') {\r\n visible = item.visible === true;\r\n } else {\r\n visible = true;\r\n }\r\n\r\n if (visible) {\r\n hasVisibleItems = true;\r\n }\r\n\r\n $item[visible ? 'show' : 'hide']();\r\n\r\n // dis- / enable item\r\n $item[disabled ? 'addClass' : 'removeClass'](root.classNames.disabled);\r\n\r\n if ($.isFunction(item.icon)) {\r\n $item.removeClass(item._icon);\r\n var iconResult = item.icon.call(this, $trigger, $item, key, item);\r\n if(typeof(iconResult) === \"string\"){\r\n $item.addClass(iconResult);\r\n } else {\r\n $item.prepend(iconResult);\r\n }\r\n }\r\n\r\n if (item.type) {\r\n // dis- / enable input elements\r\n $item.find('input, select, textarea').prop('disabled', disabled);\r\n\r\n // update input states\r\n switch (item.type) {\r\n case 'text':\r\n case 'textarea':\r\n item.$input.val(item.value || '');\r\n break;\r\n\r\n case 'checkbox':\r\n case 'radio':\r\n item.$input.val(item.value || '').prop('checked', !!item.selected);\r\n break;\r\n\r\n case 'select':\r\n item.$input.val((item.selected === 0 ? \"0\" : item.selected) || '');\r\n break;\r\n }\r\n }\r\n\r\n if (item.$menu) {\r\n // update sub-menu\r\n var subMenuHasVisibleItems = op.update.call($trigger, item, root);\r\n if (subMenuHasVisibleItems) {\r\n hasVisibleItems = true;\r\n }\r\n }\r\n });\r\n return hasVisibleItems;\r\n },\r\n layer: function (opt, zIndex) {\r\n // add transparent layer for click area\r\n // filter and background for Internet Explorer, Issue #23\r\n var $layer = opt.$layer = $('
    ')\r\n .css({\r\n height: $win.height(),\r\n width: $win.width(),\r\n display: 'block',\r\n position: 'fixed',\r\n 'z-index': zIndex - 1,\r\n top: 0,\r\n left: 0,\r\n opacity: 0,\r\n filter: 'alpha(opacity=0)',\r\n 'background-color': '#000'\r\n })\r\n .data('contextMenuRoot', opt)\r\n .appendTo(document.body)\r\n .on('contextmenu', handle.abortevent)\r\n .on('mousedown', handle.layerClick);\r\n\r\n // IE6 doesn't know position:fixed;\r\n if (typeof document.body.style.maxWidth === 'undefined') { // IE6 doesn't support maxWidth\r\n $layer.css({\r\n 'position': 'absolute',\r\n 'height': $(document).height()\r\n });\r\n }\r\n\r\n return $layer;\r\n },\r\n processPromises: function (opt, root, promise) {\r\n // Start\r\n opt.$node.addClass(root.classNames.iconLoadingClass);\r\n\r\n function completedPromise(opt, root, items) {\r\n // Completed promise (dev called promise.resolve). We now have a list of items which can\r\n // be used to create the rest of the context menu.\r\n if (typeof items === 'undefined') {\r\n // Null result, dev should have checked\r\n errorPromise(undefined);//own error object\r\n }\r\n finishPromiseProcess(opt, root, items);\r\n }\r\n\r\n function errorPromise(opt, root, errorItem) {\r\n // User called promise.reject() with an error item, if not, provide own error item.\r\n if (typeof errorItem === 'undefined') {\r\n errorItem = {\r\n \"error\": {\r\n name: \"No items and no error item\",\r\n icon: \"context-menu-icon context-menu-icon-quit\"\r\n }\r\n };\r\n if (window.console) {\r\n (console.error || console.log).call(console, 'When you reject a promise, provide an \"items\" object, equal to normal sub-menu items');\r\n }\r\n } else if (typeof errorItem === 'string') {\r\n errorItem = {\"error\": {name: errorItem}};\r\n }\r\n finishPromiseProcess(opt, root, errorItem);\r\n }\r\n\r\n function finishPromiseProcess(opt, root, items) {\r\n if (typeof root.$menu === 'undefined' || !root.$menu.is(':visible')) {\r\n return;\r\n }\r\n opt.$node.removeClass(root.classNames.iconLoadingClass);\r\n opt.items = items;\r\n op.create(opt, root, true); // Create submenu\r\n op.update(opt, root); // Correctly update position if user is already hovered over menu item\r\n root.positionSubmenu.call(opt.$node, opt.$menu); // positionSubmenu, will only do anything if user already hovered over menu item that just got new subitems.\r\n }\r\n\r\n // Wait for promise completion. .then(success, error, notify) (we don't track notify). Bind the opt\r\n // and root to avoid scope problems\r\n promise.then(completedPromise.bind(this, opt, root), errorPromise.bind(this, opt, root));\r\n },\r\n // operation that will run after contextMenu showed on screen\r\n activated: function(opt){\r\n var $menu = opt.$menu;\r\n var $menuOffset = $menu.offset();\r\n var winHeight = $(window).height();\r\n var winScrollTop = $(window).scrollTop();\r\n var menuHeight = $menu.height();\r\n if(menuHeight > winHeight){\r\n $menu.css({\r\n 'height' : winHeight + 'px',\r\n 'overflow-x': 'hidden',\r\n 'overflow-y': 'auto',\r\n 'top': winScrollTop + 'px'\r\n });\r\n } else if(($menuOffset.top < winScrollTop) || ($menuOffset.top + menuHeight > winScrollTop + winHeight)){\r\n $menu.css({\r\n 'top': winScrollTop + 'px'\r\n });\r\n }\r\n }\r\n };\r\n\r\n // split accesskey according to http://www.whatwg.org/specs/web-apps/current-work/multipage/editing.html#assigned-access-key\r\n function splitAccesskey(val) {\r\n var t = val.split(/\\s+/);\r\n var keys = [];\r\n\r\n for (var i = 0, k; k = t[i]; i++) {\r\n k = k.charAt(0).toUpperCase(); // first character only\r\n // theoretically non-accessible characters should be ignored, but different systems, different keyboard layouts, ... screw it.\r\n // a map to look up already used access keys would be nice\r\n keys.push(k);\r\n }\r\n\r\n return keys;\r\n }\r\n\r\n// handle contextMenu triggers\r\n $.fn.contextMenu = function (operation) {\r\n var $t = this, $o = operation;\r\n if (this.length > 0) { // this is not a build on demand menu\r\n if (typeof operation === 'undefined') {\r\n this.first().trigger('contextmenu');\r\n } else if (typeof operation.x !== 'undefined' && typeof operation.y !== 'undefined') {\r\n this.first().trigger($.Event('contextmenu', {\r\n pageX: operation.x,\r\n pageY: operation.y,\r\n mouseButton: operation.button\r\n }));\r\n } else if (operation === 'hide') {\r\n var $menu = this.first().data('contextMenu') ? this.first().data('contextMenu').$menu : null;\r\n if ($menu) {\r\n $menu.trigger('contextmenu:hide');\r\n }\r\n } else if (operation === 'destroy') {\r\n $.contextMenu('destroy', {context: this});\r\n } else if ($.isPlainObject(operation)) {\r\n operation.context = this;\r\n $.contextMenu('create', operation);\r\n } else if (operation) {\r\n this.removeClass('context-menu-disabled');\r\n } else if (!operation) {\r\n this.addClass('context-menu-disabled');\r\n }\r\n } else {\r\n $.each(menus, function () {\r\n if (this.selector === $t.selector) {\r\n $o.data = this;\r\n\r\n $.extend($o.data, {trigger: 'demand'});\r\n }\r\n });\r\n\r\n handle.contextmenu.call($o.target, $o);\r\n }\r\n\r\n return this;\r\n };\r\n\r\n // manage contextMenu instances\r\n $.contextMenu = function (operation, options) {\r\n if (typeof operation !== 'string') {\r\n options = operation;\r\n operation = 'create';\r\n }\r\n\r\n if (typeof options === 'string') {\r\n options = {selector: options};\r\n } else if (typeof options === 'undefined') {\r\n options = {};\r\n }\r\n\r\n // merge with default options\r\n var o = $.extend(true, {}, defaults, options || {});\r\n var $document = $(document);\r\n var $context = $document;\r\n var _hasContext = false;\r\n\r\n if (!o.context || !o.context.length) {\r\n o.context = document;\r\n } else {\r\n // you never know what they throw at you...\r\n $context = $(o.context).first();\r\n o.context = $context.get(0);\r\n _hasContext = !$(o.context).is(document);\r\n }\r\n\r\n switch (operation) {\r\n\r\n case 'update':\r\n // Updates visibility and such\r\n if(_hasContext){\r\n op.update($context);\r\n } else {\r\n for(var menu in menus){\r\n if(menus.hasOwnProperty(menu)){\r\n op.update(menus[menu]);\r\n }\r\n }\r\n }\r\n break;\r\n\r\n case 'create':\r\n // no selector no joy\r\n if (!o.selector) {\r\n throw new Error('No selector specified');\r\n }\r\n // make sure internal classes are not bound to\r\n if (o.selector.match(/.context-menu-(list|item|input)($|\\s)/)) {\r\n throw new Error('Cannot bind to selector \"' + o.selector + '\" as it contains a reserved className');\r\n }\r\n if (!o.build && (!o.items || $.isEmptyObject(o.items))) {\r\n throw new Error('No Items specified');\r\n }\r\n counter++;\r\n o.ns = '.contextMenu' + counter;\r\n if (!_hasContext) {\r\n namespaces[o.selector] = o.ns;\r\n }\r\n menus[o.ns] = o;\r\n\r\n // default to right click\r\n if (!o.trigger) {\r\n o.trigger = 'right';\r\n }\r\n\r\n if (!initialized) {\r\n var itemClick = o.itemClickEvent === 'click' ? 'click.contextMenu' : 'mouseup.contextMenu';\r\n var contextMenuItemObj = {\r\n // 'mouseup.contextMenu': handle.itemClick,\r\n // 'click.contextMenu': handle.itemClick,\r\n 'contextmenu:focus.contextMenu': handle.focusItem,\r\n 'contextmenu:blur.contextMenu': handle.blurItem,\r\n 'contextmenu.contextMenu': handle.abortevent,\r\n 'mouseenter.contextMenu': handle.itemMouseenter,\r\n 'mouseleave.contextMenu': handle.itemMouseleave\r\n };\r\n contextMenuItemObj[itemClick] = handle.itemClick;\r\n // make sure item click is registered first\r\n $document\r\n .on({\r\n 'contextmenu:hide.contextMenu': handle.hideMenu,\r\n 'prevcommand.contextMenu': handle.prevItem,\r\n 'nextcommand.contextMenu': handle.nextItem,\r\n 'contextmenu.contextMenu': handle.abortevent,\r\n 'mouseenter.contextMenu': handle.menuMouseenter,\r\n 'mouseleave.contextMenu': handle.menuMouseleave\r\n }, '.context-menu-list')\r\n .on('mouseup.contextMenu', '.context-menu-input', handle.inputClick)\r\n .on(contextMenuItemObj, '.context-menu-item');\r\n\r\n initialized = true;\r\n }\r\n\r\n // engage native contextmenu event\r\n $context\r\n .on('contextmenu' + o.ns, o.selector, o, handle.contextmenu);\r\n\r\n if (_hasContext) {\r\n // add remove hook, just in case\r\n $context.on('remove' + o.ns, function () {\r\n $(this).contextMenu('destroy');\r\n });\r\n }\r\n\r\n switch (o.trigger) {\r\n case 'hover':\r\n $context\r\n .on('mouseenter' + o.ns, o.selector, o, handle.mouseenter)\r\n .on('mouseleave' + o.ns, o.selector, o, handle.mouseleave);\r\n break;\r\n\r\n case 'left':\r\n $context.on('click' + o.ns, o.selector, o, handle.click);\r\n break;\r\n\t\t\t\t case 'touchstart':\r\n $context.on('touchstart' + o.ns, o.selector, o, handle.click);\r\n break;\r\n /*\r\n default:\r\n // http://www.quirksmode.org/dom/events/contextmenu.html\r\n $document\r\n .on('mousedown' + o.ns, o.selector, o, handle.mousedown)\r\n .on('mouseup' + o.ns, o.selector, o, handle.mouseup);\r\n break;\r\n */\r\n }\r\n\r\n // create menu\r\n if (!o.build) {\r\n op.create(o);\r\n }\r\n break;\r\n\r\n case 'destroy':\r\n var $visibleMenu;\r\n if (_hasContext) {\r\n // get proper options\r\n var context = o.context;\r\n $.each(menus, function (ns, o) {\r\n\r\n if (!o) {\r\n return true;\r\n }\r\n\r\n // Is this menu equest to the context called from\r\n if (!$(context).is(o.selector)) {\r\n return true;\r\n }\r\n\r\n $visibleMenu = $('.context-menu-list').filter(':visible');\r\n if ($visibleMenu.length && $visibleMenu.data().contextMenuRoot.$trigger.is($(o.context).find(o.selector))) {\r\n $visibleMenu.trigger('contextmenu:hide', {force: true});\r\n }\r\n\r\n try {\r\n if (menus[o.ns].$menu) {\r\n menus[o.ns].$menu.remove();\r\n }\r\n\r\n delete menus[o.ns];\r\n } catch (e) {\r\n menus[o.ns] = null;\r\n }\r\n\r\n $(o.context).off(o.ns);\r\n\r\n return true;\r\n });\r\n } else if (!o.selector) {\r\n $document.off('.contextMenu .contextMenuAutoHide');\r\n $.each(menus, function (ns, o) {\r\n $(o.context).off(o.ns);\r\n });\r\n\r\n namespaces = {};\r\n menus = {};\r\n counter = 0;\r\n initialized = false;\r\n\r\n $('#context-menu-layer, .context-menu-list').remove();\r\n } else if (namespaces[o.selector]) {\r\n $visibleMenu = $('.context-menu-list').filter(':visible');\r\n if ($visibleMenu.length && $visibleMenu.data().contextMenuRoot.$trigger.is(o.selector)) {\r\n $visibleMenu.trigger('contextmenu:hide', {force: true});\r\n }\r\n\r\n try {\r\n if (menus[namespaces[o.selector]].$menu) {\r\n menus[namespaces[o.selector]].$menu.remove();\r\n }\r\n\r\n delete menus[namespaces[o.selector]];\r\n } catch (e) {\r\n menus[namespaces[o.selector]] = null;\r\n }\r\n\r\n $document.off(namespaces[o.selector]);\r\n }\r\n break;\r\n\r\n case 'html5':\r\n // if and are not handled by the browser,\r\n // or options was a bool true,\r\n // initialize $.contextMenu for them\r\n if ((!$.support.htmlCommand && !$.support.htmlMenuitem) || (typeof options === 'boolean' && options)) {\r\n $('menu[type=\"context\"]').each(function () {\r\n if (this.id) {\r\n $.contextMenu({\r\n selector: '[contextmenu=' + this.id + ']',\r\n items: $.contextMenu.fromMenu(this)\r\n });\r\n }\r\n }).css('display', 'none');\r\n }\r\n break;\r\n\r\n default:\r\n throw new Error('Unknown operation \"' + operation + '\"');\r\n }\r\n\r\n return this;\r\n };\r\n\r\n// import values into commands\r\n $.contextMenu.setInputValues = function (opt, data) {\r\n if (typeof data === 'undefined') {\r\n data = {};\r\n }\r\n\r\n $.each(opt.inputs, function (key, item) {\r\n switch (item.type) {\r\n case 'text':\r\n case 'textarea':\r\n item.value = data[key] || '';\r\n break;\r\n\r\n case 'checkbox':\r\n item.selected = data[key] ? true : false;\r\n break;\r\n\r\n case 'radio':\r\n item.selected = (data[item.radio] || '') === item.value;\r\n break;\r\n\r\n case 'select':\r\n item.selected = data[key] || '';\r\n break;\r\n }\r\n });\r\n };\r\n\r\n// export values from commands\r\n $.contextMenu.getInputValues = function (opt, data) {\r\n if (typeof data === 'undefined') {\r\n data = {};\r\n }\r\n\r\n $.each(opt.inputs, function (key, item) {\r\n switch (item.type) {\r\n case 'text':\r\n case 'textarea':\r\n case 'select':\r\n data[key] = item.$input.val();\r\n break;\r\n\r\n case 'checkbox':\r\n data[key] = item.$input.prop('checked');\r\n break;\r\n\r\n case 'radio':\r\n if (item.$input.prop('checked')) {\r\n data[item.radio] = item.value;\r\n }\r\n break;\r\n }\r\n });\r\n\r\n return data;\r\n };\r\n\r\n// find