jstree.contextmenu.js 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662
  1. /**
  2. * ### Contextmenu plugin
  3. *
  4. * Shows a context menu when a node is right-clicked.
  5. */
  6. /*globals jQuery, define, exports, require, document */
  7. (function (factory) {
  8. "use strict";
  9. if (typeof define === 'function' && define.amd) {
  10. define('jstree.contextmenu', ['jquery','./jstree.js'], factory);
  11. }
  12. else if(typeof exports === 'object') {
  13. factory(require('jquery'), require('./jstree.js'));
  14. }
  15. else {
  16. factory(jQuery, jQuery.jstree);
  17. }
  18. }(function ($, jstree, undefined) {
  19. "use strict";
  20. if($.jstree.plugins.contextmenu) { return; }
  21. /**
  22. * stores all defaults for the contextmenu plugin
  23. * @name $.jstree.defaults.contextmenu
  24. * @plugin contextmenu
  25. */
  26. $.jstree.defaults.contextmenu = {
  27. /**
  28. * a boolean indicating if the node should be selected when the context menu is invoked on it. Defaults to `true`.
  29. * @name $.jstree.defaults.contextmenu.select_node
  30. * @plugin contextmenu
  31. */
  32. select_node : true,
  33. /**
  34. * a boolean indicating if the menu should be shown aligned with the node. Defaults to `true`, otherwise the mouse coordinates are used.
  35. * @name $.jstree.defaults.contextmenu.show_at_node
  36. * @plugin contextmenu
  37. */
  38. show_at_node : true,
  39. /**
  40. * an object of actions, or a function that accepts a node and a callback function and calls the callback function with an object of actions available for that node (you can also return the items too).
  41. *
  42. * Each action consists of a key (a unique name) and a value which is an object with the following properties (only label and action are required). Once a menu item is activated the `action` function will be invoked with an object containing the following keys: item - the contextmenu item definition as seen below, reference - the DOM node that was used (the tree node), element - the contextmenu DOM element, position - an object with x/y properties indicating the position of the menu.
  43. *
  44. * * `separator_before` - a boolean indicating if there should be a separator before this item
  45. * * `separator_after` - a boolean indicating if there should be a separator after this item
  46. * * `_disabled` - a boolean indicating if this action should be disabled
  47. * * `label` - a string - the name of the action (could be a function returning a string)
  48. * * `title` - a string - an optional tooltip for the item
  49. * * `action` - a function to be executed if this item is chosen, the function will receive
  50. * * `icon` - a string, can be a path to an icon or a className, if using an image that is in the current directory use a `./` prefix, otherwise it will be detected as a class
  51. * * `shortcut` - keyCode which will trigger the action if the menu is open (for example `113` for rename, which equals F2)
  52. * * `shortcut_label` - shortcut label (like for example `F2` for rename)
  53. * * `submenu` - an object with the same structure as $.jstree.defaults.contextmenu.items which can be used to create a submenu - each key will be rendered as a separate option in a submenu that will appear once the current item is hovered
  54. *
  55. * @name $.jstree.defaults.contextmenu.items
  56. * @plugin contextmenu
  57. */
  58. items : function (o, cb) { // Could be an object directly
  59. return {
  60. "create" : {
  61. "separator_before" : false,
  62. "separator_after" : true,
  63. "_disabled" : false, //(this.check("create_node", data.reference, {}, "last")),
  64. "label" : "Create",
  65. "action" : function (data) {
  66. var inst = $.jstree.reference(data.reference),
  67. obj = inst.get_node(data.reference);
  68. inst.create_node(obj, {}, "last", function (new_node) {
  69. try {
  70. inst.edit(new_node);
  71. } catch (ex) {
  72. setTimeout(function () { inst.edit(new_node); },0);
  73. }
  74. });
  75. }
  76. },
  77. "rename" : {
  78. "separator_before" : false,
  79. "separator_after" : false,
  80. "_disabled" : false, //(this.check("rename_node", data.reference, this.get_parent(data.reference), "")),
  81. "label" : "Rename",
  82. /*!
  83. "shortcut" : 113,
  84. "shortcut_label" : 'F2',
  85. "icon" : "glyphicon glyphicon-leaf",
  86. */
  87. "action" : function (data) {
  88. var inst = $.jstree.reference(data.reference),
  89. obj = inst.get_node(data.reference);
  90. inst.edit(obj);
  91. }
  92. },
  93. "remove" : {
  94. "separator_before" : false,
  95. "icon" : false,
  96. "separator_after" : false,
  97. "_disabled" : false, //(this.check("delete_node", data.reference, this.get_parent(data.reference), "")),
  98. "label" : "Delete",
  99. "action" : function (data) {
  100. var inst = $.jstree.reference(data.reference),
  101. obj = inst.get_node(data.reference);
  102. if(inst.is_selected(obj)) {
  103. inst.delete_node(inst.get_selected());
  104. }
  105. else {
  106. inst.delete_node(obj);
  107. }
  108. }
  109. },
  110. "ccp" : {
  111. "separator_before" : true,
  112. "icon" : false,
  113. "separator_after" : false,
  114. "label" : "Edit",
  115. "action" : false,
  116. "submenu" : {
  117. "cut" : {
  118. "separator_before" : false,
  119. "separator_after" : false,
  120. "label" : "Cut",
  121. "action" : function (data) {
  122. var inst = $.jstree.reference(data.reference),
  123. obj = inst.get_node(data.reference);
  124. if(inst.is_selected(obj)) {
  125. inst.cut(inst.get_top_selected());
  126. }
  127. else {
  128. inst.cut(obj);
  129. }
  130. }
  131. },
  132. "copy" : {
  133. "separator_before" : false,
  134. "icon" : false,
  135. "separator_after" : false,
  136. "label" : "Copy",
  137. "action" : function (data) {
  138. var inst = $.jstree.reference(data.reference),
  139. obj = inst.get_node(data.reference);
  140. if(inst.is_selected(obj)) {
  141. inst.copy(inst.get_top_selected());
  142. }
  143. else {
  144. inst.copy(obj);
  145. }
  146. }
  147. },
  148. "paste" : {
  149. "separator_before" : false,
  150. "icon" : false,
  151. "_disabled" : function (data) {
  152. return !$.jstree.reference(data.reference).can_paste();
  153. },
  154. "separator_after" : false,
  155. "label" : "Paste",
  156. "action" : function (data) {
  157. var inst = $.jstree.reference(data.reference),
  158. obj = inst.get_node(data.reference);
  159. inst.paste(obj);
  160. }
  161. }
  162. }
  163. }
  164. };
  165. }
  166. };
  167. $.jstree.plugins.contextmenu = function (options, parent) {
  168. this.bind = function () {
  169. parent.bind.call(this);
  170. var last_ts = 0, cto = null, ex, ey;
  171. this.element
  172. .on("init.jstree loading.jstree ready.jstree", function () {
  173. this.get_container_ul().addClass('jstree-contextmenu');
  174. }.bind(this))
  175. .on("contextmenu.jstree", ".jstree-anchor", function (e, data) {
  176. if (e.target.tagName.toLowerCase() === 'input') {
  177. return;
  178. }
  179. e.preventDefault();
  180. last_ts = e.ctrlKey ? +new Date() : 0;
  181. if(data || cto) {
  182. last_ts = (+new Date()) + 10000;
  183. }
  184. if(cto) {
  185. clearTimeout(cto);
  186. }
  187. if(!this.is_loading(e.currentTarget)) {
  188. this.show_contextmenu(e.currentTarget, e.pageX, e.pageY, e);
  189. }
  190. }.bind(this))
  191. .on("click.jstree", ".jstree-anchor", function (e) {
  192. if(this._data.contextmenu.visible && (!last_ts || (+new Date()) - last_ts > 250)) { // work around safari & macOS ctrl+click
  193. $.vakata.context.hide();
  194. }
  195. last_ts = 0;
  196. }.bind(this))
  197. .on("touchstart.jstree", ".jstree-anchor", function (e) {
  198. if(!e.originalEvent || !e.originalEvent.changedTouches || !e.originalEvent.changedTouches[0]) {
  199. return;
  200. }
  201. ex = e.originalEvent.changedTouches[0].clientX;
  202. ey = e.originalEvent.changedTouches[0].clientY;
  203. cto = setTimeout(function () {
  204. $(e.currentTarget).trigger('contextmenu', true);
  205. }, 750);
  206. })
  207. .on('touchmove.vakata.jstree', function (e) {
  208. if(cto && e.originalEvent && e.originalEvent.changedTouches && e.originalEvent.changedTouches[0] && (Math.abs(ex - e.originalEvent.changedTouches[0].clientX) > 10 || Math.abs(ey - e.originalEvent.changedTouches[0].clientY) > 10)) {
  209. clearTimeout(cto);
  210. $.vakata.context.hide();
  211. }
  212. })
  213. .on('touchend.vakata.jstree', function (e) {
  214. if(cto) {
  215. clearTimeout(cto);
  216. }
  217. });
  218. /*!
  219. if(!('oncontextmenu' in document.body) && ('ontouchstart' in document.body)) {
  220. var el = null, tm = null;
  221. this.element
  222. .on("touchstart", ".jstree-anchor", function (e) {
  223. el = e.currentTarget;
  224. tm = +new Date();
  225. $(document).one("touchend", function (e) {
  226. e.target = document.elementFromPoint(e.originalEvent.targetTouches[0].pageX - window.pageXOffset, e.originalEvent.targetTouches[0].pageY - window.pageYOffset);
  227. e.currentTarget = e.target;
  228. tm = ((+(new Date())) - tm);
  229. if(e.target === el && tm > 600 && tm < 1000) {
  230. e.preventDefault();
  231. $(el).trigger('contextmenu', e);
  232. }
  233. el = null;
  234. tm = null;
  235. });
  236. });
  237. }
  238. */
  239. $(document).on("context_hide.vakata.jstree", function (e, data) {
  240. this._data.contextmenu.visible = false;
  241. $(data.reference).removeClass('jstree-context');
  242. }.bind(this));
  243. };
  244. this.teardown = function () {
  245. if(this._data.contextmenu.visible) {
  246. $.vakata.context.hide();
  247. }
  248. $(document).off("context_hide.vakata.jstree");
  249. parent.teardown.call(this);
  250. };
  251. /**
  252. * prepare and show the context menu for a node
  253. * @name show_contextmenu(obj [, x, y])
  254. * @param {mixed} obj the node
  255. * @param {Number} x the x-coordinate relative to the document to show the menu at
  256. * @param {Number} y the y-coordinate relative to the document to show the menu at
  257. * @param {Object} e the event if available that triggered the contextmenu
  258. * @plugin contextmenu
  259. * @trigger show_contextmenu.jstree
  260. */
  261. this.show_contextmenu = function (obj, x, y, e) {
  262. obj = this.get_node(obj);
  263. if(!obj || obj.id === $.jstree.root) { return false; }
  264. var s = this.settings.contextmenu,
  265. d = this.get_node(obj, true),
  266. a = d.children(".jstree-anchor"),
  267. o = false,
  268. i = false;
  269. if(s.show_at_node || x === undefined || y === undefined) {
  270. o = a.offset();
  271. x = o.left;
  272. y = o.top + this._data.core.li_height;
  273. }
  274. if(this.settings.contextmenu.select_node && !this.is_selected(obj)) {
  275. this.activate_node(obj, e);
  276. }
  277. i = s.items;
  278. if($.vakata.is_function(i)) {
  279. i = i.call(this, obj, function (i) {
  280. this._show_contextmenu(obj, x, y, i);
  281. }.bind(this));
  282. }
  283. if($.isPlainObject(i)) {
  284. this._show_contextmenu(obj, x, y, i);
  285. }
  286. };
  287. /**
  288. * show the prepared context menu for a node
  289. * @name _show_contextmenu(obj, x, y, i)
  290. * @param {mixed} obj the node
  291. * @param {Number} x the x-coordinate relative to the document to show the menu at
  292. * @param {Number} y the y-coordinate relative to the document to show the menu at
  293. * @param {Number} i the object of items to show
  294. * @plugin contextmenu
  295. * @trigger show_contextmenu.jstree
  296. * @private
  297. */
  298. this._show_contextmenu = function (obj, x, y, i) {
  299. var d = this.get_node(obj, true),
  300. a = d.children(".jstree-anchor");
  301. $(document).one("context_show.vakata.jstree", function (e, data) {
  302. var cls = 'jstree-contextmenu jstree-' + this.get_theme() + '-contextmenu';
  303. $(data.element).addClass(cls);
  304. a.addClass('jstree-context');
  305. }.bind(this));
  306. this._data.contextmenu.visible = true;
  307. $.vakata.context.show(a, { 'x' : x, 'y' : y }, i);
  308. /**
  309. * triggered when the contextmenu is shown for a node
  310. * @event
  311. * @name show_contextmenu.jstree
  312. * @param {Object} node the node
  313. * @param {Number} x the x-coordinate of the menu relative to the document
  314. * @param {Number} y the y-coordinate of the menu relative to the document
  315. * @plugin contextmenu
  316. */
  317. this.trigger('show_contextmenu', { "node" : obj, "x" : x, "y" : y });
  318. };
  319. };
  320. // contextmenu helper
  321. (function ($) {
  322. var right_to_left = false,
  323. vakata_context = {
  324. element : false,
  325. reference : false,
  326. position_x : 0,
  327. position_y : 0,
  328. items : [],
  329. html : "",
  330. is_visible : false
  331. };
  332. $.vakata.context = {
  333. settings : {
  334. hide_onmouseleave : 0,
  335. icons : true
  336. },
  337. _trigger : function (event_name) {
  338. $(document).triggerHandler("context_" + event_name + ".vakata", {
  339. "reference" : vakata_context.reference,
  340. "element" : vakata_context.element,
  341. "position" : {
  342. "x" : vakata_context.position_x,
  343. "y" : vakata_context.position_y
  344. }
  345. });
  346. },
  347. _execute : function (i) {
  348. i = vakata_context.items[i];
  349. return i && (!i._disabled || ($.vakata.is_function(i._disabled) && !i._disabled({ "item" : i, "reference" : vakata_context.reference, "element" : vakata_context.element }))) && i.action ? i.action.call(null, {
  350. "item" : i,
  351. "reference" : vakata_context.reference,
  352. "element" : vakata_context.element,
  353. "position" : {
  354. "x" : vakata_context.position_x,
  355. "y" : vakata_context.position_y
  356. }
  357. }) : false;
  358. },
  359. _parse : function (o, is_callback) {
  360. if(!o) { return false; }
  361. if(!is_callback) {
  362. vakata_context.html = "";
  363. vakata_context.items = [];
  364. }
  365. var str = "",
  366. sep = false,
  367. tmp;
  368. if(is_callback) { str += "<"+"ul>"; }
  369. $.each(o, function (i, val) {
  370. if(!val) { return true; }
  371. vakata_context.items.push(val);
  372. if(!sep && val.separator_before) {
  373. str += "<"+"li class='vakata-context-separator'><"+"a href='#' " + ($.vakata.context.settings.icons ? '' : 'class="vakata-context-no-icons"') + ">&#160;<"+"/a><"+"/li>";
  374. }
  375. sep = false;
  376. str += "<"+"li class='" + (val._class || "") + (val._disabled === true || ($.vakata.is_function(val._disabled) && val._disabled({ "item" : val, "reference" : vakata_context.reference, "element" : vakata_context.element })) ? " vakata-contextmenu-disabled " : "") + "' "+(val.shortcut?" data-shortcut='"+val.shortcut+"' ":'')+">";
  377. str += "<"+"a href='#' rel='" + (vakata_context.items.length - 1) + "' " + (val.title ? "title='" + val.title + "'" : "") + ">";
  378. if($.vakata.context.settings.icons) {
  379. str += "<"+"i ";
  380. if(val.icon) {
  381. if(val.icon.indexOf("/") !== -1 || val.icon.indexOf(".") !== -1) { str += " style='background:url(\"" + val.icon + "\") center center no-repeat' "; }
  382. else { str += " class='" + val.icon + "' "; }
  383. }
  384. str += "><"+"/i><"+"span class='vakata-contextmenu-sep'>&#160;<"+"/span>";
  385. }
  386. str += ($.vakata.is_function(val.label) ? val.label({ "item" : i, "reference" : vakata_context.reference, "element" : vakata_context.element }) : val.label) + (val.shortcut?' <span class="vakata-contextmenu-shortcut vakata-contextmenu-shortcut-'+val.shortcut+'">'+ (val.shortcut_label || '') +'</span>':'') + "<"+"/a>";
  387. if(val.submenu) {
  388. tmp = $.vakata.context._parse(val.submenu, true);
  389. if(tmp) { str += tmp; }
  390. }
  391. str += "<"+"/li>";
  392. if(val.separator_after) {
  393. str += "<"+"li class='vakata-context-separator'><"+"a href='#' " + ($.vakata.context.settings.icons ? '' : 'class="vakata-context-no-icons"') + ">&#160;<"+"/a><"+"/li>";
  394. sep = true;
  395. }
  396. });
  397. str = str.replace(/<li class\='vakata-context-separator'\><\/li\>$/,"");
  398. if(is_callback) { str += "</ul>"; }
  399. /**
  400. * triggered on the document when the contextmenu is parsed (HTML is built)
  401. * @event
  402. * @plugin contextmenu
  403. * @name context_parse.vakata
  404. * @param {jQuery} reference the element that was right clicked
  405. * @param {jQuery} element the DOM element of the menu itself
  406. * @param {Object} position the x & y coordinates of the menu
  407. */
  408. if(!is_callback) { vakata_context.html = str; $.vakata.context._trigger("parse"); }
  409. return str.length > 10 ? str : false;
  410. },
  411. _show_submenu : function (o) {
  412. o = $(o);
  413. if(!o.length || !o.children("ul").length) { return; }
  414. var e = o.children("ul"),
  415. xl = o.offset().left,
  416. x = xl + o.outerWidth(),
  417. y = o.offset().top,
  418. w = e.width(),
  419. h = e.height(),
  420. dw = $(window).width() + $(window).scrollLeft(),
  421. dh = $(window).height() + $(window).scrollTop();
  422. // може да се спести е една проверка - дали няма някой от класовете вече нагоре
  423. if(right_to_left) {
  424. o[x - (w + 10 + o.outerWidth()) < 0 ? "addClass" : "removeClass"]("vakata-context-left");
  425. }
  426. else {
  427. o[x + w > dw && xl > dw - x ? "addClass" : "removeClass"]("vakata-context-right");
  428. }
  429. if(y + h + 10 > dh) {
  430. e.css("bottom","-1px");
  431. }
  432. //if does not fit - stick it to the side
  433. if (o.hasClass('vakata-context-right')) {
  434. if (xl < w) {
  435. e.css("margin-right", xl - w);
  436. }
  437. } else {
  438. if (dw - x < w) {
  439. e.css("margin-left", dw - x - w);
  440. }
  441. }
  442. e.show();
  443. },
  444. show : function (reference, position, data) {
  445. var o, e, x, y, w, h, dw, dh, cond = true;
  446. if(vakata_context.element && vakata_context.element.length) {
  447. vakata_context.element.width('');
  448. }
  449. switch(cond) {
  450. case (!position && !reference):
  451. return false;
  452. case (!!position && !!reference):
  453. vakata_context.reference = reference;
  454. vakata_context.position_x = position.x;
  455. vakata_context.position_y = position.y;
  456. break;
  457. case (!position && !!reference):
  458. vakata_context.reference = reference;
  459. o = reference.offset();
  460. vakata_context.position_x = o.left + reference.outerHeight();
  461. vakata_context.position_y = o.top;
  462. break;
  463. case (!!position && !reference):
  464. vakata_context.position_x = position.x;
  465. vakata_context.position_y = position.y;
  466. break;
  467. }
  468. if(!!reference && !data && $(reference).data('vakata_contextmenu')) {
  469. data = $(reference).data('vakata_contextmenu');
  470. }
  471. if($.vakata.context._parse(data)) {
  472. vakata_context.element.html(vakata_context.html);
  473. }
  474. if(vakata_context.items.length) {
  475. vakata_context.element.appendTo(document.body);
  476. e = vakata_context.element;
  477. x = vakata_context.position_x;
  478. y = vakata_context.position_y;
  479. w = e.width();
  480. h = e.height();
  481. dw = $(window).width() + $(window).scrollLeft();
  482. dh = $(window).height() + $(window).scrollTop();
  483. if(right_to_left) {
  484. x -= (e.outerWidth() - $(reference).outerWidth());
  485. if(x < $(window).scrollLeft() + 20) {
  486. x = $(window).scrollLeft() + 20;
  487. }
  488. }
  489. if(x + w + 20 > dw) {
  490. x = dw - (w + 20);
  491. }
  492. if(y + h + 20 > dh) {
  493. y = dh - (h + 20);
  494. }
  495. vakata_context.element
  496. .css({ "left" : x, "top" : y })
  497. .show()
  498. .find('a').first().trigger('focus').parent().addClass("vakata-context-hover");
  499. vakata_context.is_visible = true;
  500. /**
  501. * triggered on the document when the contextmenu is shown
  502. * @event
  503. * @plugin contextmenu
  504. * @name context_show.vakata
  505. * @param {jQuery} reference the element that was right clicked
  506. * @param {jQuery} element the DOM element of the menu itself
  507. * @param {Object} position the x & y coordinates of the menu
  508. */
  509. $.vakata.context._trigger("show");
  510. }
  511. },
  512. hide : function () {
  513. if(vakata_context.is_visible) {
  514. vakata_context.element.hide().find("ul").hide().end().find(':focus').trigger('blur').end().detach();
  515. vakata_context.is_visible = false;
  516. /**
  517. * triggered on the document when the contextmenu is hidden
  518. * @event
  519. * @plugin contextmenu
  520. * @name context_hide.vakata
  521. * @param {jQuery} reference the element that was right clicked
  522. * @param {jQuery} element the DOM element of the menu itself
  523. * @param {Object} position the x & y coordinates of the menu
  524. */
  525. $.vakata.context._trigger("hide");
  526. }
  527. }
  528. };
  529. $(function () {
  530. right_to_left = $(document.body).css("direction") === "rtl";
  531. var to = false;
  532. vakata_context.element = $("<ul class='vakata-context'></ul>");
  533. vakata_context.element
  534. .on("mouseenter", "li", function (e) {
  535. e.stopImmediatePropagation();
  536. if($.contains(this, e.relatedTarget)) {
  537. // премахнато заради delegate mouseleave по-долу
  538. // $(this).find(".vakata-context-hover").removeClass("vakata-context-hover");
  539. return;
  540. }
  541. if(to) { clearTimeout(to); }
  542. vakata_context.element.find(".vakata-context-hover").removeClass("vakata-context-hover").end();
  543. $(this)
  544. .siblings().find("ul").hide().end().end()
  545. .parentsUntil(".vakata-context", "li").addBack().addClass("vakata-context-hover");
  546. $.vakata.context._show_submenu(this);
  547. })
  548. // тестово - дали не натоварва?
  549. .on("mouseleave", "li", function (e) {
  550. if($.contains(this, e.relatedTarget)) { return; }
  551. $(this).find(".vakata-context-hover").addBack().removeClass("vakata-context-hover");
  552. })
  553. .on("mouseleave", function (e) {
  554. $(this).find(".vakata-context-hover").removeClass("vakata-context-hover");
  555. if($.vakata.context.settings.hide_onmouseleave) {
  556. to = setTimeout(
  557. (function (t) {
  558. return function () { $.vakata.context.hide(); };
  559. }(this)), $.vakata.context.settings.hide_onmouseleave);
  560. }
  561. })
  562. .on("click", "a", function (e) {
  563. e.preventDefault();
  564. //})
  565. //.on("mouseup", "a", function (e) {
  566. if(!$(this).trigger('blur').parent().hasClass("vakata-context-disabled") && $.vakata.context._execute($(this).attr("rel")) !== false) {
  567. $.vakata.context.hide();
  568. }
  569. })
  570. .on('keydown', 'a', function (e) {
  571. var o = null;
  572. switch(e.which) {
  573. case 13:
  574. case 32:
  575. e.type = "click";
  576. e.preventDefault();
  577. $(e.currentTarget).trigger(e);
  578. break;
  579. case 37:
  580. if(vakata_context.is_visible) {
  581. vakata_context.element.find(".vakata-context-hover").last().closest("li").first().find("ul").hide().find(".vakata-context-hover").removeClass("vakata-context-hover").end().end().children('a').trigger('focus');
  582. e.stopImmediatePropagation();
  583. e.preventDefault();
  584. }
  585. break;
  586. case 38:
  587. if(vakata_context.is_visible) {
  588. o = vakata_context.element.find("ul:visible").addBack().last().children(".vakata-context-hover").removeClass("vakata-context-hover").prevAll("li:not(.vakata-context-separator)").first();
  589. if(!o.length) { o = vakata_context.element.find("ul:visible").addBack().last().children("li:not(.vakata-context-separator)").last(); }
  590. o.addClass("vakata-context-hover").children('a').trigger('focus');
  591. e.stopImmediatePropagation();
  592. e.preventDefault();
  593. }
  594. break;
  595. case 39:
  596. if(vakata_context.is_visible) {
  597. vakata_context.element.find(".vakata-context-hover").last().children("ul").show().children("li:not(.vakata-context-separator)").removeClass("vakata-context-hover").first().addClass("vakata-context-hover").children('a').trigger('focus');
  598. e.stopImmediatePropagation();
  599. e.preventDefault();
  600. }
  601. break;
  602. case 40:
  603. if(vakata_context.is_visible) {
  604. o = vakata_context.element.find("ul:visible").addBack().last().children(".vakata-context-hover").removeClass("vakata-context-hover").nextAll("li:not(.vakata-context-separator)").first();
  605. if(!o.length) { o = vakata_context.element.find("ul:visible").addBack().last().children("li:not(.vakata-context-separator)").first(); }
  606. o.addClass("vakata-context-hover").children('a').trigger('focus');
  607. e.stopImmediatePropagation();
  608. e.preventDefault();
  609. }
  610. break;
  611. case 27:
  612. $.vakata.context.hide();
  613. e.preventDefault();
  614. break;
  615. default:
  616. //console.log(e.which);
  617. break;
  618. }
  619. })
  620. .on('keydown', function (e) {
  621. e.preventDefault();
  622. var a = vakata_context.element.find('.vakata-contextmenu-shortcut-' + e.which).parent();
  623. if(a.parent().not('.vakata-context-disabled')) {
  624. a.trigger('click');
  625. }
  626. });
  627. $(document)
  628. .on("mousedown.vakata.jstree", function (e) {
  629. if(vakata_context.is_visible && vakata_context.element[0] !== e.target && !$.contains(vakata_context.element[0], e.target)) {
  630. $.vakata.context.hide();
  631. }
  632. })
  633. .on("context_show.vakata.jstree", function (e, data) {
  634. vakata_context.element.find("li:has(ul)").children("a").addClass("vakata-context-parent");
  635. if(right_to_left) {
  636. vakata_context.element.addClass("vakata-context-rtl").css("direction", "rtl");
  637. }
  638. // also apply a RTL class?
  639. vakata_context.element.find("ul").hide().end();
  640. });
  641. });
  642. }($));
  643. // $.jstree.defaults.plugins.push("contextmenu");
  644. }));