jquery.contextMenu.js 89 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122
  1. /**
  2. * jQuery contextMenu v2.7.0 - Plugin for simple contextMenu handling
  3. *
  4. * Version: v2.7.0
  5. *
  6. * Authors: Björn Brala (SWIS.nl), Rodney Rehm, Addy Osmani (patches for FF)
  7. * Web: http://swisnl.github.io/jQuery-contextMenu/
  8. *
  9. * Copyright (c) 2011-2018 SWIS BV and contributors
  10. *
  11. * Licensed under
  12. * MIT License http://www.opensource.org/licenses/mit-license
  13. *
  14. * Date: 2018-10-02T14:29:27.777Z
  15. */
  16. // jscs:disable
  17. /* jshint ignore:start */
  18. (function (factory) {
  19. if (typeof define === 'function' && define.amd) {
  20. // AMD. Register as anonymous module.
  21. define(['jquery'], factory);
  22. } else if (typeof exports === 'object') {
  23. // Node / CommonJS
  24. factory(require('jquery'));
  25. } else {
  26. // Browser globals.
  27. factory(jQuery);
  28. }
  29. })(function ($) {
  30. 'use strict';
  31. // TODO: -
  32. // ARIA stuff: menuitem, menuitemcheckbox und menuitemradio
  33. // create <menu> structure if $.support[htmlCommand || htmlMenuitem] and !opt.disableNative
  34. // determine html5 compatibility
  35. $.support.htmlMenuitem = ('HTMLMenuItemElement' in window);
  36. $.support.htmlCommand = ('HTMLCommandElement' in window);
  37. $.support.eventSelectstart = ('onselectstart' in document.documentElement);
  38. /* // should the need arise, test for css user-select
  39. $.support.cssUserSelect = (function(){
  40. var t = false,
  41. e = document.createElement('div');
  42. $.each('Moz|Webkit|Khtml|O|ms|Icab|'.split('|'), function(i, prefix) {
  43. var propCC = prefix + (prefix ? 'U' : 'u') + 'serSelect',
  44. prop = (prefix ? ('-' + prefix.toLowerCase() + '-') : '') + 'user-select';
  45. e.style.cssText = prop + ': text;';
  46. if (e.style[propCC] == 'text') {
  47. t = true;
  48. return false;
  49. }
  50. return true;
  51. });
  52. return t;
  53. })();
  54. */
  55. if (!$.ui || !$.widget) {
  56. // duck punch $.cleanData like jQueryUI does to get that remove event
  57. $.cleanData = (function (orig) {
  58. return function (elems) {
  59. var events, elem, i;
  60. for (i = 0; elems[i] != null; i++) {
  61. elem = elems[i];
  62. try {
  63. // Only trigger remove when necessary to save time
  64. events = $._data(elem, 'events');
  65. if (events && events.remove) {
  66. $(elem).triggerHandler('remove');
  67. }
  68. // Http://bugs.jquery.com/ticket/8235
  69. } catch (e) {
  70. }
  71. }
  72. orig(elems);
  73. };
  74. })($.cleanData);
  75. }
  76. /* jshint ignore:end */
  77. // jscs:enable
  78. var // currently active contextMenu trigger
  79. $currentTrigger = null,
  80. // is contextMenu initialized with at least one menu?
  81. initialized = false,
  82. // window handle
  83. $win = $(window),
  84. // number of registered menus
  85. counter = 0,
  86. // mapping selector to namespace
  87. namespaces = {},
  88. // mapping namespace to options
  89. menus = {},
  90. // custom command type handlers
  91. types = {},
  92. // default values
  93. defaults = {
  94. // selector of contextMenu trigger
  95. selector: null,
  96. // where to append the menu to
  97. appendTo: null,
  98. // method to trigger context menu ["right", "left", "hover"]
  99. trigger: 'right',
  100. // hide menu when mouse leaves trigger / menu elements
  101. autoHide: false,
  102. // ms to wait before showing a hover-triggered context menu
  103. delay: 200,
  104. // flag denoting if a second trigger should simply move (true) or rebuild (false) an open menu
  105. // as long as the trigger happened on one of the trigger-element's child nodes
  106. reposition: true,
  107. // Flag denoting if a second trigger should close the menu, as long as
  108. // the trigger happened on one of the trigger-element's child nodes.
  109. // This overrides the reposition option.
  110. hideOnSecondTrigger: false,
  111. //ability to select submenu
  112. selectableSubMenu: false,
  113. // Default classname configuration to be able avoid conflicts in frameworks
  114. classNames: {
  115. hover: 'context-menu-hover', // Item hover
  116. disabled: 'context-menu-disabled', // Item disabled
  117. visible: 'context-menu-visible', // Item visible
  118. notSelectable: 'context-menu-not-selectable', // Item not selectable
  119. icon: 'context-menu-icon',
  120. iconEdit: 'context-menu-icon-edit',
  121. iconCut: 'context-menu-icon-cut',
  122. iconCopy: 'context-menu-icon-copy',
  123. iconPaste: 'context-menu-icon-paste',
  124. iconDelete: 'context-menu-icon-delete',
  125. iconAdd: 'context-menu-icon-add',
  126. iconQuit: 'context-menu-icon-quit',
  127. iconLoadingClass: 'context-menu-icon-loading'
  128. },
  129. // determine position to show menu at
  130. determinePosition: function ($menu) {
  131. // position to the lower middle of the trigger element
  132. if ($.ui && $.ui.position) {
  133. // .position() is provided as a jQuery UI utility
  134. // (...and it won't work on hidden elements)
  135. $menu.css('display', 'block').position({
  136. my: 'center top',
  137. at: 'center bottom',
  138. of: this,
  139. offset: '0 5',
  140. collision: 'fit'
  141. }).css('display', 'none');
  142. } else {
  143. // determine contextMenu position
  144. var offset = this.offset();
  145. offset.top += this.outerHeight();
  146. offset.left += this.outerWidth() / 2 - $menu.outerWidth() / 2;
  147. $menu.css(offset);
  148. }
  149. },
  150. // position menu
  151. position: function (opt, x, y) {
  152. var offset;
  153. // determine contextMenu position
  154. if (!x && !y) {
  155. opt.determinePosition.call(this, opt.$menu);
  156. return;
  157. } else if (x === 'maintain' && y === 'maintain') {
  158. // x and y must not be changed (after re-show on command click)
  159. offset = opt.$menu.position();
  160. } else {
  161. // x and y are given (by mouse event)
  162. var offsetParentOffset = opt.$menu.offsetParent().offset();
  163. offset = {top: y - offsetParentOffset.top, left: x -offsetParentOffset.left};
  164. }
  165. // correct offset if viewport demands it
  166. var bottom = $win.scrollTop() + $win.height(),
  167. right = $win.scrollLeft() + $win.width(),
  168. height = opt.$menu.outerHeight(),
  169. width = opt.$menu.outerWidth();
  170. if (offset.top + height > bottom) {
  171. offset.top -= height;
  172. }
  173. if (offset.top < 0) {
  174. offset.top = 0;
  175. }
  176. if (offset.left + width > right) {
  177. offset.left -= width;
  178. }
  179. if (offset.left < 0) {
  180. offset.left = 0;
  181. }
  182. opt.$menu.css(offset);
  183. },
  184. // position the sub-menu
  185. positionSubmenu: function ($menu) {
  186. if (typeof $menu === 'undefined') {
  187. // When user hovers over item (which has sub items) handle.focusItem will call this.
  188. // but the submenu does not exist yet if opt.items is a promise. just return, will
  189. // call positionSubmenu after promise is completed.
  190. return;
  191. }
  192. if ($.ui && $.ui.position) {
  193. // .position() is provided as a jQuery UI utility
  194. // (...and it won't work on hidden elements)
  195. $menu.css('display', 'block').position({
  196. my: 'left top-5',
  197. at: 'right top',
  198. of: this,
  199. collision: 'flipfit fit'
  200. }).css('display', '');
  201. } else {
  202. // determine contextMenu position
  203. var offset = {
  204. top: -9,
  205. left: this.outerWidth() - 5
  206. };
  207. $menu.css(offset);
  208. }
  209. },
  210. // offset to add to zIndex
  211. zIndex: 1,
  212. // show hide animation settings
  213. animation: {
  214. duration: 50,
  215. show: 'slideDown',
  216. hide: 'slideUp'
  217. },
  218. // events
  219. events: {
  220. show: $.noop,
  221. hide: $.noop,
  222. activated: $.noop
  223. },
  224. // default callback
  225. callback: null,
  226. // list of contextMenu items
  227. items: {}
  228. },
  229. // mouse position for hover activation
  230. hoveract = {
  231. timer: null,
  232. pageX: null,
  233. pageY: null
  234. },
  235. // determine zIndex
  236. zindex = function ($t) {
  237. var zin = 0,
  238. $tt = $t;
  239. while (true) {
  240. zin = Math.max(zin, parseInt($tt.css('z-index'), 10) || 0);
  241. $tt = $tt.parent();
  242. if (!$tt || !$tt.length || 'html body'.indexOf($tt.prop('nodeName').toLowerCase()) > -1) {
  243. break;
  244. }
  245. }
  246. return zin;
  247. },
  248. // event handlers
  249. handle = {
  250. // abort anything
  251. abortevent: function (e) {
  252. e.preventDefault();
  253. e.stopImmediatePropagation();
  254. },
  255. // contextmenu show dispatcher
  256. contextmenu: function (e) {
  257. var $this = $(this);
  258. // disable actual context-menu if we are using the right mouse button as the trigger
  259. if (e.data.trigger === 'right') {
  260. e.preventDefault();
  261. e.stopImmediatePropagation();
  262. }
  263. // abort native-triggered events unless we're triggering on right click
  264. if ((e.data.trigger !== 'right' && e.data.trigger !== 'demand') && e.originalEvent) {
  265. return;
  266. }
  267. // Let the current contextmenu decide if it should show or not based on its own trigger settings
  268. if (typeof e.mouseButton !== 'undefined' && e.data) {
  269. if (!(e.data.trigger === 'left' && e.mouseButton === 0) && !(e.data.trigger === 'right' && e.mouseButton === 2)) {
  270. // Mouse click is not valid.
  271. return;
  272. }
  273. }
  274. // abort event if menu is visible for this trigger
  275. if ($this.hasClass('context-menu-active')) {
  276. return;
  277. }
  278. if (!$this.hasClass('context-menu-disabled')) {
  279. // theoretically need to fire a show event at <menu>
  280. // http://www.whatwg.org/specs/web-apps/current-work/multipage/interactive-elements.html#context-menus
  281. // var evt = jQuery.Event("show", { data: data, pageX: e.pageX, pageY: e.pageY, relatedTarget: this });
  282. // e.data.$menu.trigger(evt);
  283. $currentTrigger = $this;
  284. if (e.data.build) {
  285. var built = e.data.build($currentTrigger, e);
  286. // abort if build() returned false
  287. if (built === false) {
  288. return;
  289. }
  290. // dynamically build menu on invocation
  291. e.data = $.extend(true, {}, defaults, e.data, built || {});
  292. // abort if there are no items to display
  293. if (!e.data.items || $.isEmptyObject(e.data.items)) {
  294. // Note: jQuery captures and ignores errors from event handlers
  295. if (window.console) {
  296. (console.error || console.log).call(console, 'No items specified to show in contextMenu');
  297. }
  298. throw new Error('No Items specified');
  299. }
  300. // backreference for custom command type creation
  301. e.data.$trigger = $currentTrigger;
  302. op.create(e.data);
  303. }
  304. op.show.call($this, e.data, e.pageX, e.pageY);
  305. }
  306. },
  307. // contextMenu left-click trigger
  308. click: function (e) {
  309. e.preventDefault();
  310. e.stopImmediatePropagation();
  311. $(this).trigger($.Event('contextmenu', {data: e.data, pageX: e.pageX, pageY: e.pageY}));
  312. },
  313. // contextMenu right-click trigger
  314. mousedown: function (e) {
  315. // register mouse down
  316. var $this = $(this);
  317. // hide any previous menus
  318. if ($currentTrigger && $currentTrigger.length && !$currentTrigger.is($this)) {
  319. $currentTrigger.data('contextMenu').$menu.trigger('contextmenu:hide');
  320. }
  321. // activate on right click
  322. if (e.button === 2) {
  323. $currentTrigger = $this.data('contextMenuActive', true);
  324. }
  325. },
  326. // contextMenu right-click trigger
  327. mouseup: function (e) {
  328. // show menu
  329. var $this = $(this);
  330. if ($this.data('contextMenuActive') && $currentTrigger && $currentTrigger.length && $currentTrigger.is($this) && !$this.hasClass('context-menu-disabled')) {
  331. e.preventDefault();
  332. e.stopImmediatePropagation();
  333. $currentTrigger = $this;
  334. $this.trigger($.Event('contextmenu', {data: e.data, pageX: e.pageX, pageY: e.pageY}));
  335. }
  336. $this.removeData('contextMenuActive');
  337. },
  338. // contextMenu hover trigger
  339. mouseenter: function (e) {
  340. var $this = $(this),
  341. $related = $(e.relatedTarget),
  342. $document = $(document);
  343. // abort if we're coming from a menu
  344. if ($related.is('.context-menu-list') || $related.closest('.context-menu-list').length) {
  345. return;
  346. }
  347. // abort if a menu is shown
  348. if ($currentTrigger && $currentTrigger.length) {
  349. return;
  350. }
  351. hoveract.pageX = e.pageX;
  352. hoveract.pageY = e.pageY;
  353. hoveract.data = e.data;
  354. $document.on('mousemove.contextMenuShow', handle.mousemove);
  355. hoveract.timer = setTimeout(function () {
  356. hoveract.timer = null;
  357. $document.off('mousemove.contextMenuShow');
  358. $currentTrigger = $this;
  359. $this.trigger($.Event('contextmenu', {
  360. data: hoveract.data,
  361. pageX: hoveract.pageX,
  362. pageY: hoveract.pageY
  363. }));
  364. }, e.data.delay);
  365. },
  366. // contextMenu hover trigger
  367. mousemove: function (e) {
  368. hoveract.pageX = e.pageX;
  369. hoveract.pageY = e.pageY;
  370. },
  371. // contextMenu hover trigger
  372. mouseleave: function (e) {
  373. // abort if we're leaving for a menu
  374. var $related = $(e.relatedTarget);
  375. if ($related.is('.context-menu-list') || $related.closest('.context-menu-list').length) {
  376. return;
  377. }
  378. try {
  379. clearTimeout(hoveract.timer);
  380. } catch (e) {
  381. }
  382. hoveract.timer = null;
  383. },
  384. // click on layer to hide contextMenu
  385. layerClick: function (e) {
  386. var $this = $(this),
  387. root = $this.data('contextMenuRoot'),
  388. button = e.button,
  389. x = e.pageX,
  390. y = e.pageY,
  391. fakeClick = x === undefined,
  392. target,
  393. offset;
  394. e.preventDefault();
  395. setTimeout(function () {
  396. // If the click is not real, things break: https://github.com/swisnl/jQuery-contextMenu/issues/132
  397. if(fakeClick){
  398. if (root !== null && typeof root !== 'undefined' && root.$menu !== null && typeof root.$menu !== 'undefined') {
  399. root.$menu.trigger('contextmenu:hide');
  400. }
  401. return;
  402. }
  403. var $window;
  404. var triggerAction = ((root.trigger === 'left' && button === 0) || (root.trigger === 'right' && button === 2));
  405. // find the element that would've been clicked, wasn't the layer in the way
  406. if (document.elementFromPoint && root.$layer) {
  407. root.$layer.hide();
  408. target = document.elementFromPoint(x - $win.scrollLeft(), y - $win.scrollTop());
  409. // also need to try and focus this element if we're in a contenteditable area,
  410. // as the layer will prevent the browser mouse action we want
  411. if (target.isContentEditable) {
  412. var range = document.createRange(),
  413. sel = window.getSelection();
  414. range.selectNode(target);
  415. range.collapse(true);
  416. sel.removeAllRanges();
  417. sel.addRange(range);
  418. }
  419. $(target).trigger(e);
  420. root.$layer.show();
  421. }
  422. if (root.hideOnSecondTrigger && triggerAction && root.$menu !== null && typeof root.$menu !== 'undefined') {
  423. root.$menu.trigger('contextmenu:hide');
  424. return;
  425. }
  426. if (root.reposition && triggerAction) {
  427. if (document.elementFromPoint) {
  428. if (root.$trigger.is(target)) {
  429. root.position.call(root.$trigger, root, x, y);
  430. return;
  431. }
  432. } else {
  433. offset = root.$trigger.offset();
  434. $window = $(window);
  435. // while this looks kinda awful, it's the best way to avoid
  436. // unnecessarily calculating any positions
  437. offset.top += $window.scrollTop();
  438. if (offset.top <= e.pageY) {
  439. offset.left += $window.scrollLeft();
  440. if (offset.left <= e.pageX) {
  441. offset.bottom = offset.top + root.$trigger.outerHeight();
  442. if (offset.bottom >= e.pageY) {
  443. offset.right = offset.left + root.$trigger.outerWidth();
  444. if (offset.right >= e.pageX) {
  445. // reposition
  446. root.position.call(root.$trigger, root, x, y);
  447. return;
  448. }
  449. }
  450. }
  451. }
  452. }
  453. }
  454. if (target && triggerAction) {
  455. root.$trigger.one('contextmenu:hidden', function () {
  456. $(target).contextMenu({x: x, y: y, button: button});
  457. });
  458. }
  459. if (root !== null && typeof root !== 'undefined' && root.$menu !== null && typeof root.$menu !== 'undefined') {
  460. root.$menu.trigger('contextmenu:hide');
  461. }
  462. }, 50);
  463. },
  464. // key handled :hover
  465. keyStop: function (e, opt) {
  466. if (!opt.isInput) {
  467. e.preventDefault();
  468. }
  469. e.stopPropagation();
  470. },
  471. key: function (e) {
  472. var opt = {};
  473. // Only get the data from $currentTrigger if it exists
  474. if ($currentTrigger) {
  475. opt = $currentTrigger.data('contextMenu') || {};
  476. }
  477. // If the trigger happen on a element that are above the contextmenu do this
  478. if (typeof opt.zIndex === 'undefined') {
  479. opt.zIndex = 0;
  480. }
  481. var targetZIndex = 0;
  482. var getZIndexOfTriggerTarget = function (target) {
  483. if (target.style.zIndex !== '') {
  484. targetZIndex = target.style.zIndex;
  485. } else {
  486. if (target.offsetParent !== null && typeof target.offsetParent !== 'undefined') {
  487. getZIndexOfTriggerTarget(target.offsetParent);
  488. }
  489. else if (target.parentElement !== null && typeof target.parentElement !== 'undefined') {
  490. getZIndexOfTriggerTarget(target.parentElement);
  491. }
  492. }
  493. };
  494. getZIndexOfTriggerTarget(e.target);
  495. // If targetZIndex is heigher then opt.zIndex dont progress any futher.
  496. // This is used to make sure that if you are using a dialog with a input / textarea / contenteditable div
  497. // and its above the contextmenu it wont steal keys events
  498. if (opt.$menu && parseInt(targetZIndex,10) > parseInt(opt.$menu.css("zIndex"),10)) {
  499. return;
  500. }
  501. switch (e.keyCode) {
  502. case 9:
  503. case 38: // up
  504. handle.keyStop(e, opt);
  505. // if keyCode is [38 (up)] or [9 (tab) with shift]
  506. if (opt.isInput) {
  507. if (e.keyCode === 9 && e.shiftKey) {
  508. e.preventDefault();
  509. if (opt.$selected) {
  510. opt.$selected.find('input, textarea, select').blur();
  511. }
  512. if (opt.$menu !== null && typeof opt.$menu !== 'undefined') {
  513. opt.$menu.trigger('prevcommand');
  514. }
  515. return;
  516. } else if (e.keyCode === 38 && opt.$selected.find('input, textarea, select').prop('type') === 'checkbox') {
  517. // checkboxes don't capture this key
  518. e.preventDefault();
  519. return;
  520. }
  521. } else if (e.keyCode !== 9 || e.shiftKey) {
  522. if (opt.$menu !== null && typeof opt.$menu !== 'undefined') {
  523. opt.$menu.trigger('prevcommand');
  524. }
  525. return;
  526. }
  527. break;
  528. // omitting break;
  529. // case 9: // tab - reached through omitted break;
  530. case 40: // down
  531. handle.keyStop(e, opt);
  532. if (opt.isInput) {
  533. if (e.keyCode === 9) {
  534. e.preventDefault();
  535. if (opt.$selected) {
  536. opt.$selected.find('input, textarea, select').blur();
  537. }
  538. if (opt.$menu !== null && typeof opt.$menu !== 'undefined') {
  539. opt.$menu.trigger('nextcommand');
  540. }
  541. return;
  542. } else if (e.keyCode === 40 && opt.$selected.find('input, textarea, select').prop('type') === 'checkbox') {
  543. // checkboxes don't capture this key
  544. e.preventDefault();
  545. return;
  546. }
  547. } else {
  548. if (opt.$menu !== null && typeof opt.$menu !== 'undefined') {
  549. opt.$menu.trigger('nextcommand');
  550. }
  551. return;
  552. }
  553. break;
  554. case 37: // left
  555. handle.keyStop(e, opt);
  556. if (opt.isInput || !opt.$selected || !opt.$selected.length) {
  557. break;
  558. }
  559. if (!opt.$selected.parent().hasClass('context-menu-root')) {
  560. var $parent = opt.$selected.parent().parent();
  561. opt.$selected.trigger('contextmenu:blur');
  562. opt.$selected = $parent;
  563. return;
  564. }
  565. break;
  566. case 39: // right
  567. handle.keyStop(e, opt);
  568. if (opt.isInput || !opt.$selected || !opt.$selected.length) {
  569. break;
  570. }
  571. var itemdata = opt.$selected.data('contextMenu') || {};
  572. if (itemdata.$menu && opt.$selected.hasClass('context-menu-submenu')) {
  573. opt.$selected = null;
  574. itemdata.$selected = null;
  575. itemdata.$menu.trigger('nextcommand');
  576. return;
  577. }
  578. break;
  579. case 35: // end
  580. case 36: // home
  581. if (opt.$selected && opt.$selected.find('input, textarea, select').length) {
  582. return;
  583. } else {
  584. (opt.$selected && opt.$selected.parent() || opt.$menu)
  585. .children(':not(.' + opt.classNames.disabled + ', .' + opt.classNames.notSelectable + ')')[e.keyCode === 36 ? 'first' : 'last']()
  586. .trigger('contextmenu:focus');
  587. e.preventDefault();
  588. return;
  589. }
  590. break;
  591. case 13: // enter
  592. handle.keyStop(e, opt);
  593. if (opt.isInput) {
  594. if (opt.$selected && !opt.$selected.is('textarea, select')) {
  595. e.preventDefault();
  596. return;
  597. }
  598. break;
  599. }
  600. if (typeof opt.$selected !== 'undefined' && opt.$selected !== null) {
  601. opt.$selected.trigger('mouseup');
  602. }
  603. return;
  604. case 32: // space
  605. case 33: // page up
  606. case 34: // page down
  607. // prevent browser from scrolling down while menu is visible
  608. handle.keyStop(e, opt);
  609. return;
  610. case 27: // esc
  611. handle.keyStop(e, opt);
  612. if (opt.$menu !== null && typeof opt.$menu !== 'undefined') {
  613. opt.$menu.trigger('contextmenu:hide');
  614. }
  615. return;
  616. default: // 0-9, a-z
  617. var k = (String.fromCharCode(e.keyCode)).toUpperCase();
  618. if (opt.accesskeys && opt.accesskeys[k]) {
  619. // according to the specs accesskeys must be invoked immediately
  620. opt.accesskeys[k].$node.trigger(opt.accesskeys[k].$menu ? 'contextmenu:focus' : 'mouseup');
  621. return;
  622. }
  623. break;
  624. }
  625. // pass event to selected item,
  626. // stop propagation to avoid endless recursion
  627. e.stopPropagation();
  628. if (typeof opt.$selected !== 'undefined' && opt.$selected !== null) {
  629. opt.$selected.trigger(e);
  630. }
  631. },
  632. // select previous possible command in menu
  633. prevItem: function (e) {
  634. e.stopPropagation();
  635. var opt = $(this).data('contextMenu') || {};
  636. var root = $(this).data('contextMenuRoot') || {};
  637. // obtain currently selected menu
  638. if (opt.$selected) {
  639. var $s = opt.$selected;
  640. opt = opt.$selected.parent().data('contextMenu') || {};
  641. opt.$selected = $s;
  642. }
  643. var $children = opt.$menu.children(),
  644. $prev = !opt.$selected || !opt.$selected.prev().length ? $children.last() : opt.$selected.prev(),
  645. $round = $prev;
  646. // skip disabled or hidden elements
  647. while ($prev.hasClass(root.classNames.disabled) || $prev.hasClass(root.classNames.notSelectable) || $prev.is(':hidden')) {
  648. if ($prev.prev().length) {
  649. $prev = $prev.prev();
  650. } else {
  651. $prev = $children.last();
  652. }
  653. if ($prev.is($round)) {
  654. // break endless loop
  655. return;
  656. }
  657. }
  658. // leave current
  659. if (opt.$selected) {
  660. handle.itemMouseleave.call(opt.$selected.get(0), e);
  661. }
  662. // activate next
  663. handle.itemMouseenter.call($prev.get(0), e);
  664. // focus input
  665. var $input = $prev.find('input, textarea, select');
  666. if ($input.length) {
  667. $input.focus();
  668. }
  669. },
  670. // select next possible command in menu
  671. nextItem: function (e) {
  672. e.stopPropagation();
  673. var opt = $(this).data('contextMenu') || {};
  674. var root = $(this).data('contextMenuRoot') || {};
  675. // obtain currently selected menu
  676. if (opt.$selected) {
  677. var $s = opt.$selected;
  678. opt = opt.$selected.parent().data('contextMenu') || {};
  679. opt.$selected = $s;
  680. }
  681. var $children = opt.$menu.children(),
  682. $next = !opt.$selected || !opt.$selected.next().length ? $children.first() : opt.$selected.next(),
  683. $round = $next;
  684. // skip disabled
  685. while ($next.hasClass(root.classNames.disabled) || $next.hasClass(root.classNames.notSelectable) || $next.is(':hidden')) {
  686. if ($next.next().length) {
  687. $next = $next.next();
  688. } else {
  689. $next = $children.first();
  690. }
  691. if ($next.is($round)) {
  692. // break endless loop
  693. return;
  694. }
  695. }
  696. // leave current
  697. if (opt.$selected) {
  698. handle.itemMouseleave.call(opt.$selected.get(0), e);
  699. }
  700. // activate next
  701. handle.itemMouseenter.call($next.get(0), e);
  702. // focus input
  703. var $input = $next.find('input, textarea, select');
  704. if ($input.length) {
  705. $input.focus();
  706. }
  707. },
  708. // flag that we're inside an input so the key handler can act accordingly
  709. focusInput: function () {
  710. var $this = $(this).closest('.context-menu-item'),
  711. data = $this.data(),
  712. opt = data.contextMenu,
  713. root = data.contextMenuRoot;
  714. root.$selected = opt.$selected = $this;
  715. root.isInput = opt.isInput = true;
  716. },
  717. // flag that we're inside an input so the key handler can act accordingly
  718. blurInput: function () {
  719. var $this = $(this).closest('.context-menu-item'),
  720. data = $this.data(),
  721. opt = data.contextMenu,
  722. root = data.contextMenuRoot;
  723. root.isInput = opt.isInput = false;
  724. },
  725. // :hover on menu
  726. menuMouseenter: function () {
  727. var root = $(this).data().contextMenuRoot;
  728. root.hovering = true;
  729. },
  730. // :hover on menu
  731. menuMouseleave: function (e) {
  732. var root = $(this).data().contextMenuRoot;
  733. if (root.$layer && root.$layer.is(e.relatedTarget)) {
  734. root.hovering = false;
  735. }
  736. },
  737. // :hover done manually so key handling is possible
  738. itemMouseenter: function (e) {
  739. var $this = $(this),
  740. data = $this.data(),
  741. opt = data.contextMenu,
  742. root = data.contextMenuRoot;
  743. root.hovering = true;
  744. // abort if we're re-entering
  745. if (e && root.$layer && root.$layer.is(e.relatedTarget)) {
  746. e.preventDefault();
  747. e.stopImmediatePropagation();
  748. }
  749. // make sure only one item is selected
  750. (opt.$menu ? opt : root).$menu
  751. .children('.' + root.classNames.hover).trigger('contextmenu:blur')
  752. .children('.hover').trigger('contextmenu:blur');
  753. if ($this.hasClass(root.classNames.disabled) || $this.hasClass(root.classNames.notSelectable)) {
  754. opt.$selected = null;
  755. return;
  756. }
  757. $this.trigger('contextmenu:focus');
  758. },
  759. // :hover done manually so key handling is possible
  760. itemMouseleave: function (e) {
  761. var $this = $(this),
  762. data = $this.data(),
  763. opt = data.contextMenu,
  764. root = data.contextMenuRoot;
  765. if (root !== opt && root.$layer && root.$layer.is(e.relatedTarget)) {
  766. if (typeof root.$selected !== 'undefined' && root.$selected !== null) {
  767. root.$selected.trigger('contextmenu:blur');
  768. }
  769. e.preventDefault();
  770. e.stopImmediatePropagation();
  771. root.$selected = opt.$selected = opt.$node;
  772. return;
  773. }
  774. if(opt && opt.$menu && opt.$menu.hasClass('context-menu-visible')){
  775. return;
  776. }
  777. $this.trigger('contextmenu:blur');
  778. },
  779. // contextMenu item click
  780. itemClick: function (e) {
  781. var $this = $(this),
  782. data = $this.data(),
  783. opt = data.contextMenu,
  784. root = data.contextMenuRoot,
  785. key = data.contextMenuKey,
  786. callback;
  787. // abort if the key is unknown or disabled or is a menu
  788. if (!opt.items[key] || $this.is('.' + root.classNames.disabled + ', .context-menu-separator, .' + root.classNames.notSelectable) || ($this.is('.context-menu-submenu') && root.selectableSubMenu === false )) {
  789. return;
  790. }
  791. e.preventDefault();
  792. e.stopImmediatePropagation();
  793. if ($.isFunction(opt.callbacks[key]) && Object.prototype.hasOwnProperty.call(opt.callbacks, key)) {
  794. // item-specific callback
  795. callback = opt.callbacks[key];
  796. } else if ($.isFunction(root.callback)) {
  797. // default callback
  798. callback = root.callback;
  799. } else {
  800. // no callback, no action
  801. return;
  802. }
  803. // hide menu if callback doesn't stop that
  804. if (callback.call(root.$trigger, key, root, e) !== false) {
  805. root.$menu.trigger('contextmenu:hide');
  806. } else if (root.$menu.parent().length) {
  807. op.update.call(root.$trigger, root);
  808. }
  809. },
  810. // ignore click events on input elements
  811. inputClick: function (e) {
  812. e.stopImmediatePropagation();
  813. },
  814. // hide <menu>
  815. hideMenu: function (e, data) {
  816. var root = $(this).data('contextMenuRoot');
  817. op.hide.call(root.$trigger, root, data && data.force);
  818. },
  819. // focus <command>
  820. focusItem: function (e) {
  821. e.stopPropagation();
  822. var $this = $(this),
  823. data = $this.data(),
  824. opt = data.contextMenu,
  825. root = data.contextMenuRoot;
  826. if ($this.hasClass(root.classNames.disabled) || $this.hasClass(root.classNames.notSelectable)) {
  827. return;
  828. }
  829. $this
  830. .addClass([root.classNames.hover, root.classNames.visible].join(' '))
  831. // select other items and included items
  832. .parent().find('.context-menu-item').not($this)
  833. .removeClass(root.classNames.visible)
  834. .filter('.' + root.classNames.hover)
  835. .trigger('contextmenu:blur');
  836. // remember selected
  837. opt.$selected = root.$selected = $this;
  838. if(opt && opt.$node && opt.$node.hasClass('context-menu-submenu')){
  839. opt.$node.addClass(root.classNames.hover);
  840. }
  841. // position sub-menu - do after show so dumb $.ui.position can keep up
  842. if (opt.$node) {
  843. root.positionSubmenu.call(opt.$node, opt.$menu);
  844. }
  845. },
  846. // blur <command>
  847. blurItem: function (e) {
  848. e.stopPropagation();
  849. var $this = $(this),
  850. data = $this.data(),
  851. opt = data.contextMenu,
  852. root = data.contextMenuRoot;
  853. if (opt.autoHide) { // for tablets and touch screens this needs to remain
  854. $this.removeClass(root.classNames.visible);
  855. }
  856. $this.removeClass(root.classNames.hover);
  857. opt.$selected = null;
  858. }
  859. },
  860. // operations
  861. op = {
  862. show: function (opt, x, y) {
  863. var $trigger = $(this),
  864. css = {};
  865. // hide any open menus
  866. $('#context-menu-layer').trigger('mousedown');
  867. // backreference for callbacks
  868. opt.$trigger = $trigger;
  869. // show event
  870. if (opt.events.show.call($trigger, opt) === false) {
  871. $currentTrigger = null;
  872. return;
  873. }
  874. // create or update context menu
  875. var hasVisibleItems = op.update.call($trigger, opt);
  876. if (hasVisibleItems === false) {
  877. $currentTrigger = null;
  878. return;
  879. }
  880. // position menu
  881. opt.position.call($trigger, opt, x, y);
  882. // make sure we're in front
  883. if (opt.zIndex) {
  884. var additionalZValue = opt.zIndex;
  885. // If opt.zIndex is a function, call the function to get the right zIndex.
  886. if (typeof opt.zIndex === 'function') {
  887. additionalZValue = opt.zIndex.call($trigger, opt);
  888. }
  889. css.zIndex = zindex($trigger) + additionalZValue;
  890. }
  891. // add layer
  892. op.layer.call(opt.$menu, opt, css.zIndex);
  893. // adjust sub-menu zIndexes
  894. opt.$menu.find('ul').css('zIndex', css.zIndex + 1);
  895. // position and show context menu
  896. opt.$menu.css(css)[opt.animation.show](opt.animation.duration, function () {
  897. $trigger.trigger('contextmenu:visible');
  898. op.activated(opt);
  899. opt.events.activated(opt);
  900. });
  901. // make options available and set state
  902. $trigger
  903. .data('contextMenu', opt)
  904. .addClass('context-menu-active');
  905. // register key handler
  906. $(document).off('keydown.contextMenu').on('keydown.contextMenu', handle.key);
  907. // register autoHide handler
  908. if (opt.autoHide) {
  909. // mouse position handler
  910. $(document).on('mousemove.contextMenuAutoHide', function (e) {
  911. // need to capture the offset on mousemove,
  912. // since the page might've been scrolled since activation
  913. var pos = $trigger.offset();
  914. pos.right = pos.left + $trigger.outerWidth();
  915. pos.bottom = pos.top + $trigger.outerHeight();
  916. if (opt.$layer && !opt.hovering && (!(e.pageX >= pos.left && e.pageX <= pos.right) || !(e.pageY >= pos.top && e.pageY <= pos.bottom))) {
  917. /* Additional hover check after short time, you might just miss the edge of the menu */
  918. setTimeout(function () {
  919. if (!opt.hovering && opt.$menu !== null && typeof opt.$menu !== 'undefined') {
  920. opt.$menu.trigger('contextmenu:hide');
  921. }
  922. }, 50);
  923. }
  924. });
  925. }
  926. },
  927. hide: function (opt, force) {
  928. var $trigger = $(this);
  929. if (!opt) {
  930. opt = $trigger.data('contextMenu') || {};
  931. }
  932. // hide event
  933. if (!force && opt.events && opt.events.hide.call($trigger, opt) === false) {
  934. return;
  935. }
  936. // remove options and revert state
  937. $trigger
  938. .removeData('contextMenu')
  939. .removeClass('context-menu-active');
  940. if (opt.$layer) {
  941. // keep layer for a bit so the contextmenu event can be aborted properly by opera
  942. setTimeout((function ($layer) {
  943. return function () {
  944. $layer.remove();
  945. };
  946. })(opt.$layer), 10);
  947. try {
  948. delete opt.$layer;
  949. } catch (e) {
  950. opt.$layer = null;
  951. }
  952. }
  953. // remove handle
  954. $currentTrigger = null;
  955. // remove selected
  956. opt.$menu.find('.' + opt.classNames.hover).trigger('contextmenu:blur');
  957. opt.$selected = null;
  958. // collapse all submenus
  959. opt.$menu.find('.' + opt.classNames.visible).removeClass(opt.classNames.visible);
  960. // unregister key and mouse handlers
  961. // $(document).off('.contextMenuAutoHide keydown.contextMenu'); // http://bugs.jquery.com/ticket/10705
  962. $(document).off('.contextMenuAutoHide').off('keydown.contextMenu');
  963. // hide menu
  964. if (opt.$menu) {
  965. opt.$menu[opt.animation.hide](opt.animation.duration, function () {
  966. // tear down dynamically built menu after animation is completed.
  967. if (opt.build) {
  968. opt.$menu.remove();
  969. $.each(opt, function (key) {
  970. switch (key) {
  971. case 'ns':
  972. case 'selector':
  973. case 'build':
  974. case 'trigger':
  975. return true;
  976. default:
  977. opt[key] = undefined;
  978. try {
  979. delete opt[key];
  980. } catch (e) {
  981. }
  982. return true;
  983. }
  984. });
  985. }
  986. setTimeout(function () {
  987. $trigger.trigger('contextmenu:hidden');
  988. }, 10);
  989. });
  990. }
  991. },
  992. create: function (opt, root) {
  993. if (typeof root === 'undefined') {
  994. root = opt;
  995. }
  996. // create contextMenu
  997. opt.$menu = $('<ul class="context-menu-list"></ul>').addClass(opt.className || '').data({
  998. 'contextMenu': opt,
  999. 'contextMenuRoot': root
  1000. });
  1001. $.each(['callbacks', 'commands', 'inputs'], function (i, k) {
  1002. opt[k] = {};
  1003. if (!root[k]) {
  1004. root[k] = {};
  1005. }
  1006. });
  1007. if (!root.accesskeys) {
  1008. root.accesskeys = {};
  1009. }
  1010. function createNameNode(item) {
  1011. var $name = $('<span></span>');
  1012. if (item._accesskey) {
  1013. if (item._beforeAccesskey) {
  1014. $name.append(document.createTextNode(item._beforeAccesskey));
  1015. }
  1016. $('<span></span>')
  1017. .addClass('context-menu-accesskey')
  1018. .text(item._accesskey)
  1019. .appendTo($name);
  1020. if (item._afterAccesskey) {
  1021. $name.append(document.createTextNode(item._afterAccesskey));
  1022. }
  1023. } else {
  1024. if (item.isHtmlName) {
  1025. // restrict use with access keys
  1026. if (typeof item.accesskey !== 'undefined') {
  1027. throw new Error('accesskeys are not compatible with HTML names and cannot be used together in the same item');
  1028. }
  1029. $name.html(item.name);
  1030. } else {
  1031. $name.text(item.name);
  1032. }
  1033. }
  1034. return $name;
  1035. }
  1036. // create contextMenu items
  1037. $.each(opt.items, function (key, item) {
  1038. var $t = $('<li class="context-menu-item"></li>').addClass(item.className || ''),
  1039. $label = null,
  1040. $input = null;
  1041. // iOS needs to see a click-event bound to an element to actually
  1042. // have the TouchEvents infrastructure trigger the click event
  1043. $t.on('click', $.noop);
  1044. // Make old school string seperator a real item so checks wont be
  1045. // akward later.
  1046. // And normalize 'cm_separator' into 'cm_seperator'.
  1047. if (typeof item === 'string' || item.type === 'cm_separator') {
  1048. item = {type: 'cm_seperator'};
  1049. }
  1050. item.$node = $t.data({
  1051. 'contextMenu': opt,
  1052. 'contextMenuRoot': root,
  1053. 'contextMenuKey': key
  1054. });
  1055. // register accesskey
  1056. // NOTE: the accesskey attribute should be applicable to any element, but Safari5 and Chrome13 still can't do that
  1057. if (typeof item.accesskey !== 'undefined') {
  1058. var aks = splitAccesskey(item.accesskey);
  1059. for (var i = 0, ak; ak = aks[i]; i++) {
  1060. if (!root.accesskeys[ak]) {
  1061. root.accesskeys[ak] = item;
  1062. var matched = item.name.match(new RegExp('^(.*?)(' + ak + ')(.*)$', 'i'));
  1063. if (matched) {
  1064. item._beforeAccesskey = matched[1];
  1065. item._accesskey = matched[2];
  1066. item._afterAccesskey = matched[3];
  1067. }
  1068. break;
  1069. }
  1070. }
  1071. }
  1072. if (item.type && types[item.type]) {
  1073. // run custom type handler
  1074. types[item.type].call($t, item, opt, root);
  1075. // register commands
  1076. $.each([opt, root], function (i, k) {
  1077. k.commands[key] = item;
  1078. // Overwrite only if undefined or the item is appended to the root. This so it
  1079. // doesn't overwrite callbacks of root elements if the name is the same.
  1080. if ($.isFunction(item.callback) && (typeof k.callbacks[key] === 'undefined' || typeof opt.type === 'undefined')) {
  1081. k.callbacks[key] = item.callback;
  1082. }
  1083. });
  1084. } else {
  1085. // add label for input
  1086. if (item.type === 'cm_seperator') {
  1087. $t.addClass('context-menu-separator ' + root.classNames.notSelectable);
  1088. } else if (item.type === 'html') {
  1089. $t.addClass('context-menu-html ' + root.classNames.notSelectable);
  1090. } else if (item.type !== 'sub' && item.type) {
  1091. $label = $('<label></label>').appendTo($t);
  1092. createNameNode(item).appendTo($label);
  1093. $t.addClass('context-menu-input');
  1094. opt.hasTypes = true;
  1095. $.each([opt, root], function (i, k) {
  1096. k.commands[key] = item;
  1097. k.inputs[key] = item;
  1098. });
  1099. } else if (item.items) {
  1100. item.type = 'sub';
  1101. }
  1102. switch (item.type) {
  1103. case 'cm_seperator':
  1104. break;
  1105. case 'text':
  1106. $input = $('<input type="text" value="1" name="" />')
  1107. .attr('name', 'context-menu-input-' + key)
  1108. .val(item.value || '')
  1109. .appendTo($label);
  1110. break;
  1111. case 'textarea':
  1112. $input = $('<textarea name=""></textarea>')
  1113. .attr('name', 'context-menu-input-' + key)
  1114. .val(item.value || '')
  1115. .appendTo($label);
  1116. if (item.height) {
  1117. $input.height(item.height);
  1118. }
  1119. break;
  1120. case 'checkbox':
  1121. $input = $('<input type="checkbox" value="1" name="" />')
  1122. .attr('name', 'context-menu-input-' + key)
  1123. .val(item.value || '')
  1124. .prop('checked', !!item.selected)
  1125. .prependTo($label);
  1126. break;
  1127. case 'radio':
  1128. $input = $('<input type="radio" value="1" name="" />')
  1129. .attr('name', 'context-menu-input-' + item.radio)
  1130. .val(item.value || '')
  1131. .prop('checked', !!item.selected)
  1132. .prependTo($label);
  1133. break;
  1134. case 'select':
  1135. $input = $('<select name=""></select>')
  1136. .attr('name', 'context-menu-input-' + key)
  1137. .appendTo($label);
  1138. if (item.options) {
  1139. $.each(item.options, function (value, text) {
  1140. $('<option></option>').val(value).text(text).appendTo($input);
  1141. });
  1142. $input.val(item.selected);
  1143. }
  1144. break;
  1145. case 'sub':
  1146. createNameNode(item).appendTo($t);
  1147. item.appendTo = item.$node;
  1148. $t.data('contextMenu', item).addClass('context-menu-submenu');
  1149. item.callback = null;
  1150. // If item contains items, and this is a promise, we should create it later
  1151. // check if subitems is of type promise. If it is a promise we need to create
  1152. // it later, after promise has been resolved.
  1153. if ('function' === typeof item.items.then) {
  1154. // probably a promise, process it, when completed it will create the sub menu's.
  1155. op.processPromises(item, root, item.items);
  1156. } else {
  1157. // normal submenu.
  1158. op.create(item, root);
  1159. }
  1160. break;
  1161. case 'html':
  1162. $(item.html).appendTo($t);
  1163. break;
  1164. default:
  1165. $.each([opt, root], function (i, k) {
  1166. k.commands[key] = item;
  1167. // Overwrite only if undefined or the item is appended to the root. This so it
  1168. // doesn't overwrite callbacks of root elements if the name is the same.
  1169. if ($.isFunction(item.callback) && (typeof k.callbacks[key] === 'undefined' || typeof opt.type === 'undefined')) {
  1170. k.callbacks[key] = item.callback;
  1171. }
  1172. });
  1173. createNameNode(item).appendTo($t);
  1174. break;
  1175. }
  1176. // disable key listener in <input>
  1177. if (item.type && item.type !== 'sub' && item.type !== 'html' && item.type !== 'cm_seperator') {
  1178. $input
  1179. .on('focus', handle.focusInput)
  1180. .on('blur', handle.blurInput);
  1181. if (item.events) {
  1182. $input.on(item.events, opt);
  1183. }
  1184. }
  1185. // add icons
  1186. if (item.icon) {
  1187. if ($.isFunction(item.icon)) {
  1188. item._icon = item.icon.call(this, this, $t, key, item);
  1189. } else {
  1190. if (typeof(item.icon) === 'string' && (
  1191. item.icon.substring(0, 4) === 'fab '
  1192. || item.icon.substring(0, 4) === 'fas '
  1193. || item.icon.substring(0, 4) === 'far '
  1194. || item.icon.substring(0, 4) === 'fal ')
  1195. ) {
  1196. // to enable font awesome
  1197. $t.addClass(root.classNames.icon + ' ' + root.classNames.icon + '--fa5');
  1198. item._icon = $('<i class="' + item.icon + '"></i>');
  1199. } else if (typeof(item.icon) === 'string' && item.icon.substring(0, 3) === 'fa-') {
  1200. item._icon = root.classNames.icon + ' ' + root.classNames.icon + '--fa fa ' + item.icon;
  1201. } else {
  1202. item._icon = root.classNames.icon + ' ' + root.classNames.icon + '-' + item.icon;
  1203. }
  1204. }
  1205. if(typeof(item._icon) === "string"){
  1206. $t.addClass(item._icon);
  1207. } else {
  1208. $t.prepend(item._icon);
  1209. }
  1210. }
  1211. }
  1212. // cache contained elements
  1213. item.$input = $input;
  1214. item.$label = $label;
  1215. // attach item to menu
  1216. $t.appendTo(opt.$menu);
  1217. // Disable text selection
  1218. if (!opt.hasTypes && $.support.eventSelectstart) {
  1219. // browsers support user-select: none,
  1220. // IE has a special event for text-selection
  1221. // browsers supporting neither will not be preventing text-selection
  1222. $t.on('selectstart.disableTextSelect', handle.abortevent);
  1223. }
  1224. });
  1225. // attach contextMenu to <body> (to bypass any possible overflow:hidden issues on parents of the trigger element)
  1226. if (!opt.$node) {
  1227. opt.$menu.css('display', 'none').addClass('context-menu-root');
  1228. }
  1229. opt.$menu.appendTo(opt.appendTo || document.body);
  1230. },
  1231. resize: function ($menu, nested) {
  1232. var domMenu;
  1233. // determine widths of submenus, as CSS won't grow them automatically
  1234. // position:absolute within position:absolute; min-width:100; max-width:200; results in width: 100;
  1235. // kinda sucks hard...
  1236. // determine width of absolutely positioned element
  1237. $menu.css({position: 'absolute', display: 'block'});
  1238. // don't apply yet, because that would break nested elements' widths
  1239. $menu.data('width',
  1240. (domMenu = $menu.get(0)).getBoundingClientRect ?
  1241. Math.ceil(domMenu.getBoundingClientRect().width) :
  1242. $menu.outerWidth() + 1); // outerWidth() returns rounded pixels
  1243. // reset styles so they allow nested elements to grow/shrink naturally
  1244. $menu.css({
  1245. position: 'static',
  1246. minWidth: '0px',
  1247. maxWidth: '100000px'
  1248. });
  1249. // identify width of nested menus
  1250. $menu.find('> li > ul').each(function () {
  1251. op.resize($(this), true);
  1252. });
  1253. // reset and apply changes in the end because nested
  1254. // elements' widths wouldn't be calculatable otherwise
  1255. if (!nested) {
  1256. $menu.find('ul').addBack().css({
  1257. position: '',
  1258. display: '',
  1259. minWidth: '',
  1260. maxWidth: ''
  1261. }).outerWidth(function () {
  1262. return $(this).data('width');
  1263. });
  1264. }
  1265. },
  1266. update: function (opt, root) {
  1267. var $trigger = this;
  1268. if (typeof root === 'undefined') {
  1269. root = opt;
  1270. op.resize(opt.$menu);
  1271. }
  1272. var hasVisibleItems = false;
  1273. // re-check disabled for each item
  1274. opt.$menu.children().each(function () {
  1275. var $item = $(this),
  1276. key = $item.data('contextMenuKey'),
  1277. item = opt.items[key],
  1278. disabled = ($.isFunction(item.disabled) && item.disabled.call($trigger, key, root)) || item.disabled === true,
  1279. visible;
  1280. if ($.isFunction(item.visible)) {
  1281. visible = item.visible.call($trigger, key, root);
  1282. } else if (typeof item.visible !== 'undefined') {
  1283. visible = item.visible === true;
  1284. } else {
  1285. visible = true;
  1286. }
  1287. if (visible) {
  1288. hasVisibleItems = true;
  1289. }
  1290. $item[visible ? 'show' : 'hide']();
  1291. // dis- / enable item
  1292. $item[disabled ? 'addClass' : 'removeClass'](root.classNames.disabled);
  1293. if ($.isFunction(item.icon)) {
  1294. $item.removeClass(item._icon);
  1295. var iconResult = item.icon.call(this, $trigger, $item, key, item);
  1296. if(typeof(iconResult) === "string"){
  1297. $item.addClass(iconResult);
  1298. } else {
  1299. $item.prepend(iconResult);
  1300. }
  1301. }
  1302. if (item.type) {
  1303. // dis- / enable input elements
  1304. $item.find('input, select, textarea').prop('disabled', disabled);
  1305. // update input states
  1306. switch (item.type) {
  1307. case 'text':
  1308. case 'textarea':
  1309. item.$input.val(item.value || '');
  1310. break;
  1311. case 'checkbox':
  1312. case 'radio':
  1313. item.$input.val(item.value || '').prop('checked', !!item.selected);
  1314. break;
  1315. case 'select':
  1316. item.$input.val((item.selected === 0 ? "0" : item.selected) || '');
  1317. break;
  1318. }
  1319. }
  1320. if (item.$menu) {
  1321. // update sub-menu
  1322. var subMenuHasVisibleItems = op.update.call($trigger, item, root);
  1323. if (subMenuHasVisibleItems) {
  1324. hasVisibleItems = true;
  1325. }
  1326. }
  1327. });
  1328. return hasVisibleItems;
  1329. },
  1330. layer: function (opt, zIndex) {
  1331. // add transparent layer for click area
  1332. // filter and background for Internet Explorer, Issue #23
  1333. var $layer = opt.$layer = $('<div id="context-menu-layer"></div>')
  1334. .css({
  1335. height: $win.height(),
  1336. width: $win.width(),
  1337. display: 'block',
  1338. position: 'fixed',
  1339. 'z-index': zIndex,
  1340. top: 0,
  1341. left: 0,
  1342. opacity: 0,
  1343. filter: 'alpha(opacity=0)',
  1344. 'background-color': '#000'
  1345. })
  1346. .data('contextMenuRoot', opt)
  1347. .insertBefore(this)
  1348. .on('contextmenu', handle.abortevent)
  1349. .on('mousedown', handle.layerClick);
  1350. // IE6 doesn't know position:fixed;
  1351. if (typeof document.body.style.maxWidth === 'undefined') { // IE6 doesn't support maxWidth
  1352. $layer.css({
  1353. 'position': 'absolute',
  1354. 'height': $(document).height()
  1355. });
  1356. }
  1357. return $layer;
  1358. },
  1359. processPromises: function (opt, root, promise) {
  1360. // Start
  1361. opt.$node.addClass(root.classNames.iconLoadingClass);
  1362. function completedPromise(opt, root, items) {
  1363. // Completed promise (dev called promise.resolve). We now have a list of items which can
  1364. // be used to create the rest of the context menu.
  1365. if (typeof items === 'undefined') {
  1366. // Null result, dev should have checked
  1367. errorPromise(undefined);//own error object
  1368. }
  1369. finishPromiseProcess(opt, root, items);
  1370. }
  1371. function errorPromise(opt, root, errorItem) {
  1372. // User called promise.reject() with an error item, if not, provide own error item.
  1373. if (typeof errorItem === 'undefined') {
  1374. errorItem = {
  1375. "error": {
  1376. name: "No items and no error item",
  1377. icon: "context-menu-icon context-menu-icon-quit"
  1378. }
  1379. };
  1380. if (window.console) {
  1381. (console.error || console.log).call(console, 'When you reject a promise, provide an "items" object, equal to normal sub-menu items');
  1382. }
  1383. } else if (typeof errorItem === 'string') {
  1384. errorItem = {"error": {name: errorItem}};
  1385. }
  1386. finishPromiseProcess(opt, root, errorItem);
  1387. }
  1388. function finishPromiseProcess(opt, root, items) {
  1389. if (typeof root.$menu === 'undefined' || !root.$menu.is(':visible')) {
  1390. return;
  1391. }
  1392. opt.$node.removeClass(root.classNames.iconLoadingClass);
  1393. opt.items = items;
  1394. op.create(opt, root, true); // Create submenu
  1395. op.update(opt, root); // Correctly update position if user is already hovered over menu item
  1396. root.positionSubmenu.call(opt.$node, opt.$menu); // positionSubmenu, will only do anything if user already hovered over menu item that just got new subitems.
  1397. }
  1398. // Wait for promise completion. .then(success, error, notify) (we don't track notify). Bind the opt
  1399. // and root to avoid scope problems
  1400. promise.then(completedPromise.bind(this, opt, root), errorPromise.bind(this, opt, root));
  1401. },
  1402. // operation that will run after contextMenu showed on screen
  1403. activated: function(opt){
  1404. var $menu = opt.$menu;
  1405. var $menuOffset = $menu.offset();
  1406. var winHeight = $(window).height();
  1407. var winScrollTop = $(window).scrollTop();
  1408. var menuHeight = $menu.height();
  1409. if(menuHeight > winHeight){
  1410. $menu.css({
  1411. 'height' : winHeight + 'px',
  1412. 'overflow-x': 'hidden',
  1413. 'overflow-y': 'auto',
  1414. 'top': winScrollTop + 'px'
  1415. });
  1416. } else if(($menuOffset.top < winScrollTop) || ($menuOffset.top + menuHeight > winScrollTop + winHeight)){
  1417. $menu.css({
  1418. 'top': winScrollTop + 'px'
  1419. });
  1420. }
  1421. }
  1422. };
  1423. // split accesskey according to http://www.whatwg.org/specs/web-apps/current-work/multipage/editing.html#assigned-access-key
  1424. function splitAccesskey(val) {
  1425. var t = val.split(/\s+/);
  1426. var keys = [];
  1427. for (var i = 0, k; k = t[i]; i++) {
  1428. k = k.charAt(0).toUpperCase(); // first character only
  1429. // theoretically non-accessible characters should be ignored, but different systems, different keyboard layouts, ... screw it.
  1430. // a map to look up already used access keys would be nice
  1431. keys.push(k);
  1432. }
  1433. return keys;
  1434. }
  1435. // handle contextMenu triggers
  1436. $.fn.contextMenu = function (operation) {
  1437. var $t = this, $o = operation;
  1438. if (this.length > 0) { // this is not a build on demand menu
  1439. if (typeof operation === 'undefined') {
  1440. this.first().trigger('contextmenu');
  1441. } else if (typeof operation.x !== 'undefined' && typeof operation.y !== 'undefined') {
  1442. this.first().trigger($.Event('contextmenu', {
  1443. pageX: operation.x,
  1444. pageY: operation.y,
  1445. mouseButton: operation.button
  1446. }));
  1447. } else if (operation === 'hide') {
  1448. var $menu = this.first().data('contextMenu') ? this.first().data('contextMenu').$menu : null;
  1449. if ($menu) {
  1450. $menu.trigger('contextmenu:hide');
  1451. }
  1452. } else if (operation === 'destroy') {
  1453. $.contextMenu('destroy', {context: this});
  1454. } else if ($.isPlainObject(operation)) {
  1455. operation.context = this;
  1456. $.contextMenu('create', operation);
  1457. } else if (operation) {
  1458. this.removeClass('context-menu-disabled');
  1459. } else if (!operation) {
  1460. this.addClass('context-menu-disabled');
  1461. }
  1462. } else {
  1463. $.each(menus, function () {
  1464. if (this.selector === $t.selector) {
  1465. $o.data = this;
  1466. $.extend($o.data, {trigger: 'demand'});
  1467. }
  1468. });
  1469. handle.contextmenu.call($o.target, $o);
  1470. }
  1471. return this;
  1472. };
  1473. // manage contextMenu instances
  1474. $.contextMenu = function (operation, options) {
  1475. if (typeof operation !== 'string') {
  1476. options = operation;
  1477. operation = 'create';
  1478. }
  1479. if (typeof options === 'string') {
  1480. options = {selector: options};
  1481. } else if (typeof options === 'undefined') {
  1482. options = {};
  1483. }
  1484. // merge with default options
  1485. var o = $.extend(true, {}, defaults, options || {});
  1486. var $document = $(document);
  1487. var $context = $document;
  1488. var _hasContext = false;
  1489. if (!o.context || !o.context.length) {
  1490. o.context = document;
  1491. } else {
  1492. // you never know what they throw at you...
  1493. $context = $(o.context).first();
  1494. o.context = $context.get(0);
  1495. _hasContext = !$(o.context).is(document);
  1496. }
  1497. switch (operation) {
  1498. case 'update':
  1499. // Updates visibility and such
  1500. if(_hasContext){
  1501. op.update($context);
  1502. } else {
  1503. for(var menu in menus){
  1504. if(menus.hasOwnProperty(menu)){
  1505. op.update(menus[menu]);
  1506. }
  1507. }
  1508. }
  1509. break;
  1510. case 'create':
  1511. // no selector no joy
  1512. if (!o.selector) {
  1513. throw new Error('No selector specified');
  1514. }
  1515. // make sure internal classes are not bound to
  1516. if (o.selector.match(/.context-menu-(list|item|input)($|\s)/)) {
  1517. throw new Error('Cannot bind to selector "' + o.selector + '" as it contains a reserved className');
  1518. }
  1519. if (!o.build && (!o.items || $.isEmptyObject(o.items))) {
  1520. throw new Error('No Items specified');
  1521. }
  1522. counter++;
  1523. o.ns = '.contextMenu' + counter;
  1524. if (!_hasContext) {
  1525. namespaces[o.selector] = o.ns;
  1526. }
  1527. menus[o.ns] = o;
  1528. // default to right click
  1529. if (!o.trigger) {
  1530. o.trigger = 'right';
  1531. }
  1532. if (!initialized) {
  1533. var itemClick = o.itemClickEvent === 'click' ? 'click.contextMenu' : 'mouseup.contextMenu';
  1534. var contextMenuItemObj = {
  1535. // 'mouseup.contextMenu': handle.itemClick,
  1536. // 'click.contextMenu': handle.itemClick,
  1537. 'contextmenu:focus.contextMenu': handle.focusItem,
  1538. 'contextmenu:blur.contextMenu': handle.blurItem,
  1539. 'contextmenu.contextMenu': handle.abortevent,
  1540. 'mouseenter.contextMenu': handle.itemMouseenter,
  1541. 'mouseleave.contextMenu': handle.itemMouseleave
  1542. };
  1543. contextMenuItemObj[itemClick] = handle.itemClick;
  1544. // make sure item click is registered first
  1545. $document
  1546. .on({
  1547. 'contextmenu:hide.contextMenu': handle.hideMenu,
  1548. 'prevcommand.contextMenu': handle.prevItem,
  1549. 'nextcommand.contextMenu': handle.nextItem,
  1550. 'contextmenu.contextMenu': handle.abortevent,
  1551. 'mouseenter.contextMenu': handle.menuMouseenter,
  1552. 'mouseleave.contextMenu': handle.menuMouseleave
  1553. }, '.context-menu-list')
  1554. .on('mouseup.contextMenu', '.context-menu-input', handle.inputClick)
  1555. .on(contextMenuItemObj, '.context-menu-item');
  1556. initialized = true;
  1557. }
  1558. // engage native contextmenu event
  1559. $context
  1560. .on('contextmenu' + o.ns, o.selector, o, handle.contextmenu);
  1561. if (_hasContext) {
  1562. // add remove hook, just in case
  1563. $context.on('remove' + o.ns, function () {
  1564. $(this).contextMenu('destroy');
  1565. });
  1566. }
  1567. switch (o.trigger) {
  1568. case 'hover':
  1569. $context
  1570. .on('mouseenter' + o.ns, o.selector, o, handle.mouseenter)
  1571. .on('mouseleave' + o.ns, o.selector, o, handle.mouseleave);
  1572. break;
  1573. case 'left':
  1574. $context.on('click' + o.ns, o.selector, o, handle.click);
  1575. break;
  1576. case 'touchstart':
  1577. $context.on('touchstart' + o.ns, o.selector, o, handle.click);
  1578. break;
  1579. /*
  1580. default:
  1581. // http://www.quirksmode.org/dom/events/contextmenu.html
  1582. $document
  1583. .on('mousedown' + o.ns, o.selector, o, handle.mousedown)
  1584. .on('mouseup' + o.ns, o.selector, o, handle.mouseup);
  1585. break;
  1586. */
  1587. }
  1588. // create menu
  1589. if (!o.build) {
  1590. op.create(o);
  1591. }
  1592. break;
  1593. case 'destroy':
  1594. var $visibleMenu;
  1595. if (_hasContext) {
  1596. // get proper options
  1597. var context = o.context;
  1598. $.each(menus, function (ns, o) {
  1599. if (!o) {
  1600. return true;
  1601. }
  1602. // Is this menu equest to the context called from
  1603. if (!$(context).is(o.selector)) {
  1604. return true;
  1605. }
  1606. $visibleMenu = $('.context-menu-list').filter(':visible');
  1607. if ($visibleMenu.length && $visibleMenu.data().contextMenuRoot.$trigger.is($(o.context).find(o.selector))) {
  1608. $visibleMenu.trigger('contextmenu:hide', {force: true});
  1609. }
  1610. try {
  1611. if (menus[o.ns].$menu) {
  1612. menus[o.ns].$menu.remove();
  1613. }
  1614. delete menus[o.ns];
  1615. } catch (e) {
  1616. menus[o.ns] = null;
  1617. }
  1618. $(o.context).off(o.ns);
  1619. return true;
  1620. });
  1621. } else if (!o.selector) {
  1622. $document.off('.contextMenu .contextMenuAutoHide');
  1623. $.each(menus, function (ns, o) {
  1624. $(o.context).off(o.ns);
  1625. });
  1626. namespaces = {};
  1627. menus = {};
  1628. counter = 0;
  1629. initialized = false;
  1630. $('#context-menu-layer, .context-menu-list').remove();
  1631. } else if (namespaces[o.selector]) {
  1632. $visibleMenu = $('.context-menu-list').filter(':visible');
  1633. if ($visibleMenu.length && $visibleMenu.data().contextMenuRoot.$trigger.is(o.selector)) {
  1634. $visibleMenu.trigger('contextmenu:hide', {force: true});
  1635. }
  1636. try {
  1637. if (menus[namespaces[o.selector]].$menu) {
  1638. menus[namespaces[o.selector]].$menu.remove();
  1639. }
  1640. delete menus[namespaces[o.selector]];
  1641. } catch (e) {
  1642. menus[namespaces[o.selector]] = null;
  1643. }
  1644. $document.off(namespaces[o.selector]);
  1645. }
  1646. break;
  1647. case 'html5':
  1648. // if <command> and <menuitem> are not handled by the browser,
  1649. // or options was a bool true,
  1650. // initialize $.contextMenu for them
  1651. if ((!$.support.htmlCommand && !$.support.htmlMenuitem) || (typeof options === 'boolean' && options)) {
  1652. $('menu[type="context"]').each(function () {
  1653. if (this.id) {
  1654. $.contextMenu({
  1655. selector: '[contextmenu=' + this.id + ']',
  1656. items: $.contextMenu.fromMenu(this)
  1657. });
  1658. }
  1659. }).css('display', 'none');
  1660. }
  1661. break;
  1662. default:
  1663. throw new Error('Unknown operation "' + operation + '"');
  1664. }
  1665. return this;
  1666. };
  1667. // import values into <input> commands
  1668. $.contextMenu.setInputValues = function (opt, data) {
  1669. if (typeof data === 'undefined') {
  1670. data = {};
  1671. }
  1672. $.each(opt.inputs, function (key, item) {
  1673. switch (item.type) {
  1674. case 'text':
  1675. case 'textarea':
  1676. item.value = data[key] || '';
  1677. break;
  1678. case 'checkbox':
  1679. item.selected = data[key] ? true : false;
  1680. break;
  1681. case 'radio':
  1682. item.selected = (data[item.radio] || '') === item.value;
  1683. break;
  1684. case 'select':
  1685. item.selected = data[key] || '';
  1686. break;
  1687. }
  1688. });
  1689. };
  1690. // export values from <input> commands
  1691. $.contextMenu.getInputValues = function (opt, data) {
  1692. if (typeof data === 'undefined') {
  1693. data = {};
  1694. }
  1695. $.each(opt.inputs, function (key, item) {
  1696. switch (item.type) {
  1697. case 'text':
  1698. case 'textarea':
  1699. case 'select':
  1700. data[key] = item.$input.val();
  1701. break;
  1702. case 'checkbox':
  1703. data[key] = item.$input.prop('checked');
  1704. break;
  1705. case 'radio':
  1706. if (item.$input.prop('checked')) {
  1707. data[item.radio] = item.value;
  1708. }
  1709. break;
  1710. }
  1711. });
  1712. return data;
  1713. };
  1714. // find <label for="xyz">
  1715. function inputLabel(node) {
  1716. return (node.id && $('label[for="' + node.id + '"]').val()) || node.name;
  1717. }
  1718. // convert <menu> to items object
  1719. function menuChildren(items, $children, counter) {
  1720. if (!counter) {
  1721. counter = 0;
  1722. }
  1723. $children.each(function () {
  1724. var $node = $(this),
  1725. node = this,
  1726. nodeName = this.nodeName.toLowerCase(),
  1727. label,
  1728. item;
  1729. // extract <label><input>
  1730. if (nodeName === 'label' && $node.find('input, textarea, select').length) {
  1731. label = $node.text();
  1732. $node = $node.children().first();
  1733. node = $node.get(0);
  1734. nodeName = node.nodeName.toLowerCase();
  1735. }
  1736. /*
  1737. * <menu> accepts flow-content as children. that means <embed>, <canvas> and such are valid menu items.
  1738. * Not being the sadistic kind, $.contextMenu only accepts:
  1739. * <command>, <menuitem>, <hr>, <span>, <p> <input [text, radio, checkbox]>, <textarea>, <select> and of course <menu>.
  1740. * Everything else will be imported as an html node, which is not interfaced with contextMenu.
  1741. */
  1742. // http://www.whatwg.org/specs/web-apps/current-work/multipage/commands.html#concept-command
  1743. switch (nodeName) {
  1744. // http://www.whatwg.org/specs/web-apps/current-work/multipage/interactive-elements.html#the-menu-element
  1745. case 'menu':
  1746. item = {name: $node.attr('label'), items: {}};
  1747. counter = menuChildren(item.items, $node.children(), counter);
  1748. break;
  1749. // http://www.whatwg.org/specs/web-apps/current-work/multipage/commands.html#using-the-a-element-to-define-a-command
  1750. case 'a':
  1751. // http://www.whatwg.org/specs/web-apps/current-work/multipage/commands.html#using-the-button-element-to-define-a-command
  1752. case 'button':
  1753. item = {
  1754. name: $node.text(),
  1755. disabled: !!$node.attr('disabled'),
  1756. callback: (function () {
  1757. return function () {
  1758. $node.get(0).click();
  1759. };
  1760. })()
  1761. };
  1762. break;
  1763. // http://www.whatwg.org/specs/web-apps/current-work/multipage/commands.html#using-the-command-element-to-define-a-command
  1764. case 'menuitem':
  1765. case 'command':
  1766. switch ($node.attr('type')) {
  1767. case undefined:
  1768. case 'command':
  1769. case 'menuitem':
  1770. item = {
  1771. name: $node.attr('label'),
  1772. disabled: !!$node.attr('disabled'),
  1773. icon: $node.attr('icon'),
  1774. callback: (function () {
  1775. return function () {
  1776. $node.get(0).click();
  1777. };
  1778. })()
  1779. };
  1780. break;
  1781. case 'checkbox':
  1782. item = {
  1783. type: 'checkbox',
  1784. disabled: !!$node.attr('disabled'),
  1785. name: $node.attr('label'),
  1786. selected: !!$node.attr('checked')
  1787. };
  1788. break;
  1789. case 'radio':
  1790. item = {
  1791. type: 'radio',
  1792. disabled: !!$node.attr('disabled'),
  1793. name: $node.attr('label'),
  1794. radio: $node.attr('radiogroup'),
  1795. value: $node.attr('id'),
  1796. selected: !!$node.attr('checked')
  1797. };
  1798. break;
  1799. default:
  1800. item = undefined;
  1801. }
  1802. break;
  1803. case 'hr':
  1804. item = '-------';
  1805. break;
  1806. case 'input':
  1807. switch ($node.attr('type')) {
  1808. case 'text':
  1809. item = {
  1810. type: 'text',
  1811. name: label || inputLabel(node),
  1812. disabled: !!$node.attr('disabled'),
  1813. value: $node.val()
  1814. };
  1815. break;
  1816. case 'checkbox':
  1817. item = {
  1818. type: 'checkbox',
  1819. name: label || inputLabel(node),
  1820. disabled: !!$node.attr('disabled'),
  1821. selected: !!$node.attr('checked')
  1822. };
  1823. break;
  1824. case 'radio':
  1825. item = {
  1826. type: 'radio',
  1827. name: label || inputLabel(node),
  1828. disabled: !!$node.attr('disabled'),
  1829. radio: !!$node.attr('name'),
  1830. value: $node.val(),
  1831. selected: !!$node.attr('checked')
  1832. };
  1833. break;
  1834. default:
  1835. item = undefined;
  1836. break;
  1837. }
  1838. break;
  1839. case 'select':
  1840. item = {
  1841. type: 'select',
  1842. name: label || inputLabel(node),
  1843. disabled: !!$node.attr('disabled'),
  1844. selected: $node.val(),
  1845. options: {}
  1846. };
  1847. $node.children().each(function () {
  1848. item.options[this.value] = $(this).text();
  1849. });
  1850. break;
  1851. case 'textarea':
  1852. item = {
  1853. type: 'textarea',
  1854. name: label || inputLabel(node),
  1855. disabled: !!$node.attr('disabled'),
  1856. value: $node.val()
  1857. };
  1858. break;
  1859. case 'label':
  1860. break;
  1861. default:
  1862. item = {type: 'html', html: $node.clone(true)};
  1863. break;
  1864. }
  1865. if (item) {
  1866. counter++;
  1867. items['key' + counter] = item;
  1868. }
  1869. });
  1870. return counter;
  1871. }
  1872. // convert html5 menu
  1873. $.contextMenu.fromMenu = function (element) {
  1874. var $this = $(element),
  1875. items = {};
  1876. menuChildren(items, $this.children());
  1877. return items;
  1878. };
  1879. // make defaults accessible
  1880. $.contextMenu.defaults = defaults;
  1881. $.contextMenu.types = types;
  1882. // export internal functions - undocumented, for hacking only!
  1883. $.contextMenu.handle = handle;
  1884. $.contextMenu.op = op;
  1885. $.contextMenu.menus = menus;
  1886. });