You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

662 lines
24 KiB

  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'], factory);
  11. }
  12. else if(typeof exports === 'object') {
  13. factory(require('jquery'), require('jstree'));
  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", $.proxy(function () {
  173. this.get_container_ul().addClass('jstree-contextmenu');
  174. }, this))
  175. .on("contextmenu.jstree", ".jstree-anchor", $.proxy(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. }, this))
  191. .on("click.jstree", ".jstree-anchor", $.proxy(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. }, 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", $.proxy(function (e, data) {
  240. this._data.contextmenu.visible = false;
  241. $(data.reference).removeClass('jstree-context');
  242. }, this));
  243. };
  244. this.teardown = function () {
  245. if(this._data.contextmenu.visible) {
  246. $.vakata.context.hide();
  247. }
  248. parent.teardown.call(this);
  249. };
  250. /**
  251. * prepare and show the context menu for a node
  252. * @name show_contextmenu(obj [, x, y])
  253. * @param {mixed} obj the node
  254. * @param {Number} x the x-coordinate relative to the document to show the menu at
  255. * @param {Number} y the y-coordinate relative to the document to show the menu at
  256. * @param {Object} e the event if available that triggered the contextmenu
  257. * @plugin contextmenu
  258. * @trigger show_contextmenu.jstree
  259. */
  260. this.show_contextmenu = function (obj, x, y, e) {
  261. obj = this.get_node(obj);
  262. if(!obj || obj.id === $.jstree.root) { return false; }
  263. var s = this.settings.contextmenu,
  264. d = this.get_node(obj, true),
  265. a = d.children(".jstree-anchor"),
  266. o = false,
  267. i = false;
  268. if(s.show_at_node || x === undefined || y === undefined) {
  269. o = a.offset();
  270. x = o.left;
  271. y = o.top + this._data.core.li_height;
  272. }
  273. if(this.settings.contextmenu.select_node && !this.is_selected(obj)) {
  274. this.activate_node(obj, e);
  275. }
  276. i = s.items;
  277. if($.isFunction(i)) {
  278. i = i.call(this, obj, $.proxy(function (i) {
  279. this._show_contextmenu(obj, x, y, i);
  280. }, this));
  281. }
  282. if($.isPlainObject(i)) {
  283. this._show_contextmenu(obj, x, y, i);
  284. }
  285. };
  286. /**
  287. * show the prepared context menu for a node
  288. * @name _show_contextmenu(obj, x, y, i)
  289. * @param {mixed} obj the node
  290. * @param {Number} x the x-coordinate relative to the document to show the menu at
  291. * @param {Number} y the y-coordinate relative to the document to show the menu at
  292. * @param {Number} i the object of items to show
  293. * @plugin contextmenu
  294. * @trigger show_contextmenu.jstree
  295. * @private
  296. */
  297. this._show_contextmenu = function (obj, x, y, i) {
  298. var d = this.get_node(obj, true),
  299. a = d.children(".jstree-anchor");
  300. $(document).one("context_show.vakata.jstree", $.proxy(function (e, data) {
  301. var cls = 'jstree-contextmenu jstree-' + this.get_theme() + '-contextmenu';
  302. $(data.element).addClass(cls);
  303. a.addClass('jstree-context');
  304. }, this));
  305. this._data.contextmenu.visible = true;
  306. $.vakata.context.show(a, { 'x' : x, 'y' : y }, i);
  307. /**
  308. * triggered when the contextmenu is shown for a node
  309. * @event
  310. * @name show_contextmenu.jstree
  311. * @param {Object} node the node
  312. * @param {Number} x the x-coordinate of the menu relative to the document
  313. * @param {Number} y the y-coordinate of the menu relative to the document
  314. * @plugin contextmenu
  315. */
  316. this.trigger('show_contextmenu', { "node" : obj, "x" : x, "y" : y });
  317. };
  318. };
  319. // contextmenu helper
  320. (function ($) {
  321. var right_to_left = false,
  322. vakata_context = {
  323. element : false,
  324. reference : false,
  325. position_x : 0,
  326. position_y : 0,
  327. items : [],
  328. html : "",
  329. is_visible : false
  330. };
  331. $.vakata.context = {
  332. settings : {
  333. hide_onmouseleave : 0,
  334. icons : true
  335. },
  336. _trigger : function (event_name) {
  337. $(document).triggerHandler("context_" + event_name + ".vakata", {
  338. "reference" : vakata_context.reference,
  339. "element" : vakata_context.element,
  340. "position" : {
  341. "x" : vakata_context.position_x,
  342. "y" : vakata_context.position_y
  343. }
  344. });
  345. },
  346. _execute : function (i) {
  347. i = vakata_context.items[i];
  348. return i && (!i._disabled || ($.isFunction(i._disabled) && !i._disabled({ "item" : i, "reference" : vakata_context.reference, "element" : vakata_context.element }))) && i.action ? i.action.call(null, {
  349. "item" : i,
  350. "reference" : vakata_context.reference,
  351. "element" : vakata_context.element,
  352. "position" : {
  353. "x" : vakata_context.position_x,
  354. "y" : vakata_context.position_y
  355. }
  356. }) : false;
  357. },
  358. _parse : function (o, is_callback) {
  359. if(!o) { return false; }
  360. if(!is_callback) {
  361. vakata_context.html = "";
  362. vakata_context.items = [];
  363. }
  364. var str = "",
  365. sep = false,
  366. tmp;
  367. if(is_callback) { str += "<"+"ul>"; }
  368. $.each(o, function (i, val) {
  369. if(!val) { return true; }
  370. vakata_context.items.push(val);
  371. if(!sep && val.separator_before) {
  372. str += "<"+"li class='vakata-context-separator'><"+"a href='#' " + ($.vakata.context.settings.icons ? '' : 'style="margin-left:0px;"') + ">&#160;<"+"/a><"+"/li>";
  373. }
  374. sep = false;
  375. str += "<"+"li class='" + (val._class || "") + (val._disabled === true || ($.isFunction(val._disabled) && val._disabled({ "item" : val, "reference" : vakata_context.reference, "element" : vakata_context.element })) ? " vakata-contextmenu-disabled " : "") + "' "+(val.shortcut?" data-shortcut='"+val.shortcut+"' ":'')+">";
  376. str += "<"+"a href='#' rel='" + (vakata_context.items.length - 1) + "' " + (val.title ? "title='" + val.title + "'" : "") + ">";
  377. if($.vakata.context.settings.icons) {
  378. str += "<"+"i ";
  379. if(val.icon) {
  380. if(val.icon.indexOf("/") !== -1 || val.icon.indexOf(".") !== -1) { str += " style='background:url(\"" + val.icon + "\") center center no-repeat' "; }
  381. else { str += " class='" + val.icon + "' "; }
  382. }
  383. str += "><"+"/i><"+"span class='vakata-contextmenu-sep'>&#160;<"+"/span>";
  384. }
  385. str += ($.isFunction(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>";
  386. if(val.submenu) {
  387. tmp = $.vakata.context._parse(val.submenu, true);
  388. if(tmp) { str += tmp; }
  389. }
  390. str += "<"+"/li>";
  391. if(val.separator_after) {
  392. str += "<"+"li class='vakata-context-separator'><"+"a href='#' " + ($.vakata.context.settings.icons ? '' : 'style="margin-left:0px;"') + ">&#160;<"+"/a><"+"/li>";
  393. sep = true;
  394. }
  395. });
  396. str = str.replace(/<li class\='vakata-context-separator'\><\/li\>$/,"");
  397. if(is_callback) { str += "</ul>"; }
  398. /**
  399. * triggered on the document when the contextmenu is parsed (HTML is built)
  400. * @event
  401. * @plugin contextmenu
  402. * @name context_parse.vakata
  403. * @param {jQuery} reference the element that was right clicked
  404. * @param {jQuery} element the DOM element of the menu itself
  405. * @param {Object} position the x & y coordinates of the menu
  406. */
  407. if(!is_callback) { vakata_context.html = str; $.vakata.context._trigger("parse"); }
  408. return str.length > 10 ? str : false;
  409. },
  410. _show_submenu : function (o) {
  411. o = $(o);
  412. if(!o.length || !o.children("ul").length) { return; }
  413. var e = o.children("ul"),
  414. xl = o.offset().left,
  415. x = xl + o.outerWidth(),
  416. y = o.offset().top,
  417. w = e.width(),
  418. h = e.height(),
  419. dw = $(window).width() + $(window).scrollLeft(),
  420. dh = $(window).height() + $(window).scrollTop();
  421. // може да се спести е една проверка - дали няма някой от класовете вече нагоре
  422. if(right_to_left) {
  423. o[x - (w + 10 + o.outerWidth()) < 0 ? "addClass" : "removeClass"]("vakata-context-left");
  424. }
  425. else {
  426. o[x + w > dw && xl > dw - x ? "addClass" : "removeClass"]("vakata-context-right");
  427. }
  428. if(y + h + 10 > dh) {
  429. e.css("bottom","-1px");
  430. }
  431. //if does not fit - stick it to the side
  432. if (o.hasClass('vakata-context-right')) {
  433. if (xl < w) {
  434. e.css("margin-right", xl - w);
  435. }
  436. } else {
  437. if (dw - x < w) {
  438. e.css("margin-left", dw - x - w);
  439. }
  440. }
  441. e.show();
  442. },
  443. show : function (reference, position, data) {
  444. var o, e, x, y, w, h, dw, dh, cond = true;
  445. if(vakata_context.element && vakata_context.element.length) {
  446. vakata_context.element.width('');
  447. }
  448. switch(cond) {
  449. case (!position && !reference):
  450. return false;
  451. case (!!position && !!reference):
  452. vakata_context.reference = reference;
  453. vakata_context.position_x = position.x;
  454. vakata_context.position_y = position.y;
  455. break;
  456. case (!position && !!reference):
  457. vakata_context.reference = reference;
  458. o = reference.offset();
  459. vakata_context.position_x = o.left + reference.outerHeight();
  460. vakata_context.position_y = o.top;
  461. break;
  462. case (!!position && !reference):
  463. vakata_context.position_x = position.x;
  464. vakata_context.position_y = position.y;
  465. break;
  466. }
  467. if(!!reference && !data && $(reference).data('vakata_contextmenu')) {
  468. data = $(reference).data('vakata_contextmenu');
  469. }
  470. if($.vakata.context._parse(data)) {
  471. vakata_context.element.html(vakata_context.html);
  472. }
  473. if(vakata_context.items.length) {
  474. vakata_context.element.appendTo(document.body);
  475. e = vakata_context.element;
  476. x = vakata_context.position_x;
  477. y = vakata_context.position_y;
  478. w = e.width();
  479. h = e.height();
  480. dw = $(window).width() + $(window).scrollLeft();
  481. dh = $(window).height() + $(window).scrollTop();
  482. if(right_to_left) {
  483. x -= (e.outerWidth() - $(reference).outerWidth());
  484. if(x < $(window).scrollLeft() + 20) {
  485. x = $(window).scrollLeft() + 20;
  486. }
  487. }
  488. if(x + w + 20 > dw) {
  489. x = dw - (w + 20);
  490. }
  491. if(y + h + 20 > dh) {
  492. y = dh - (h + 20);
  493. }
  494. vakata_context.element
  495. .css({ "left" : x, "top" : y })
  496. .show()
  497. .find('a').first().focus().parent().addClass("vakata-context-hover");
  498. vakata_context.is_visible = true;
  499. /**
  500. * triggered on the document when the contextmenu is shown
  501. * @event
  502. * @plugin contextmenu
  503. * @name context_show.vakata
  504. * @param {jQuery} reference the element that was right clicked
  505. * @param {jQuery} element the DOM element of the menu itself
  506. * @param {Object} position the x & y coordinates of the menu
  507. */
  508. $.vakata.context._trigger("show");
  509. }
  510. },
  511. hide : function () {
  512. if(vakata_context.is_visible) {
  513. vakata_context.element.hide().find("ul").hide().end().find(':focus').blur().end().detach();
  514. vakata_context.is_visible = false;
  515. /**
  516. * triggered on the document when the contextmenu is hidden
  517. * @event
  518. * @plugin contextmenu
  519. * @name context_hide.vakata
  520. * @param {jQuery} reference the element that was right clicked
  521. * @param {jQuery} element the DOM element of the menu itself
  522. * @param {Object} position the x & y coordinates of the menu
  523. */
  524. $.vakata.context._trigger("hide");
  525. }
  526. }
  527. };
  528. $(function () {
  529. right_to_left = $(document.body).css("direction") === "rtl";
  530. var to = false;
  531. vakata_context.element = $("<ul class='vakata-context'></ul>");
  532. vakata_context.element
  533. .on("mouseenter", "li", function (e) {
  534. e.stopImmediatePropagation();
  535. if($.contains(this, e.relatedTarget)) {
  536. // премахнато заради delegate mouseleave по-долу
  537. // $(this).find(".vakata-context-hover").removeClass("vakata-context-hover");
  538. return;
  539. }
  540. if(to) { clearTimeout(to); }
  541. vakata_context.element.find(".vakata-context-hover").removeClass("vakata-context-hover").end();
  542. $(this)
  543. .siblings().find("ul").hide().end().end()
  544. .parentsUntil(".vakata-context", "li").addBack().addClass("vakata-context-hover");
  545. $.vakata.context._show_submenu(this);
  546. })
  547. // тестово - дали не натоварва?
  548. .on("mouseleave", "li", function (e) {
  549. if($.contains(this, e.relatedTarget)) { return; }
  550. $(this).find(".vakata-context-hover").addBack().removeClass("vakata-context-hover");
  551. })
  552. .on("mouseleave", function (e) {
  553. $(this).find(".vakata-context-hover").removeClass("vakata-context-hover");
  554. if($.vakata.context.settings.hide_onmouseleave) {
  555. to = setTimeout(
  556. (function (t) {
  557. return function () { $.vakata.context.hide(); };
  558. }(this)), $.vakata.context.settings.hide_onmouseleave);
  559. }
  560. })
  561. .on("click", "a", function (e) {
  562. e.preventDefault();
  563. //})
  564. //.on("mouseup", "a", function (e) {
  565. if(!$(this).blur().parent().hasClass("vakata-context-disabled") && $.vakata.context._execute($(this).attr("rel")) !== false) {
  566. $.vakata.context.hide();
  567. }
  568. })
  569. .on('keydown', 'a', function (e) {
  570. var o = null;
  571. switch(e.which) {
  572. case 13:
  573. case 32:
  574. e.type = "click";
  575. e.preventDefault();
  576. $(e.currentTarget).trigger(e);
  577. break;
  578. case 37:
  579. if(vakata_context.is_visible) {
  580. 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').focus();
  581. e.stopImmediatePropagation();
  582. e.preventDefault();
  583. }
  584. break;
  585. case 38:
  586. if(vakata_context.is_visible) {
  587. o = vakata_context.element.find("ul:visible").addBack().last().children(".vakata-context-hover").removeClass("vakata-context-hover").prevAll("li:not(.vakata-context-separator)").first();
  588. if(!o.length) { o = vakata_context.element.find("ul:visible").addBack().last().children("li:not(.vakata-context-separator)").last(); }
  589. o.addClass("vakata-context-hover").children('a').focus();
  590. e.stopImmediatePropagation();
  591. e.preventDefault();
  592. }
  593. break;
  594. case 39:
  595. if(vakata_context.is_visible) {
  596. 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').focus();
  597. e.stopImmediatePropagation();
  598. e.preventDefault();
  599. }
  600. break;
  601. case 40:
  602. if(vakata_context.is_visible) {
  603. o = vakata_context.element.find("ul:visible").addBack().last().children(".vakata-context-hover").removeClass("vakata-context-hover").nextAll("li:not(.vakata-context-separator)").first();
  604. if(!o.length) { o = vakata_context.element.find("ul:visible").addBack().last().children("li:not(.vakata-context-separator)").first(); }
  605. o.addClass("vakata-context-hover").children('a').focus();
  606. e.stopImmediatePropagation();
  607. e.preventDefault();
  608. }
  609. break;
  610. case 27:
  611. $.vakata.context.hide();
  612. e.preventDefault();
  613. break;
  614. default:
  615. //console.log(e.which);
  616. break;
  617. }
  618. })
  619. .on('keydown', function (e) {
  620. e.preventDefault();
  621. var a = vakata_context.element.find('.vakata-contextmenu-shortcut-' + e.which).parent();
  622. if(a.parent().not('.vakata-context-disabled')) {
  623. a.click();
  624. }
  625. });
  626. $(document)
  627. .on("mousedown.vakata.jstree", function (e) {
  628. if(vakata_context.is_visible && vakata_context.element[0] !== e.target && !$.contains(vakata_context.element[0], e.target)) {
  629. $.vakata.context.hide();
  630. }
  631. })
  632. .on("context_show.vakata.jstree", function (e, data) {
  633. vakata_context.element.find("li:has(ul)").children("a").addClass("vakata-context-parent");
  634. if(right_to_left) {
  635. vakata_context.element.addClass("vakata-context-rtl").css("direction", "rtl");
  636. }
  637. // also apply a RTL class?
  638. vakata_context.element.find("ul").hide().end();
  639. });
  640. });
  641. }($));
  642. // $.jstree.defaults.plugins.push("contextmenu");
  643. }));