jquery.contextMenu.js 87 KB

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