jquery.contextMenu.js 86 KB

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