jquery.contextMenu.js 89 KB

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