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.

8422 lines
291 KiB

6 years ago
  1. /*globals jQuery, define, module, exports, require, window, document, postMessage */
  2. (function (factory) {
  3. "use strict";
  4. if (typeof define === 'function' && define.amd) {
  5. define(['jquery'], factory);
  6. }
  7. else if(typeof module !== 'undefined' && module.exports) {
  8. module.exports = factory(require('jquery'));
  9. }
  10. else {
  11. factory(jQuery);
  12. }
  13. }(function ($, undefined) {
  14. "use strict";
  15. /*!
  16. * jsTree 3.3.4
  17. * http://jstree.com/
  18. *
  19. * Copyright (c) 2014 Ivan Bozhanov (http://vakata.com)
  20. *
  21. * Licensed same as jquery - under the terms of the MIT License
  22. * http://www.opensource.org/licenses/mit-license.php
  23. */
  24. /*!
  25. * if using jslint please allow for the jQuery global and use following options:
  26. * jslint: loopfunc: true, browser: true, ass: true, bitwise: true, continue: true, nomen: true, plusplus: true, regexp: true, unparam: true, todo: true, white: true
  27. */
  28. /*jshint -W083 */
  29. // prevent another load? maybe there is a better way?
  30. if($.jstree) {
  31. return;
  32. }
  33. /**
  34. * ### jsTree core functionality
  35. */
  36. // internal variables
  37. var instance_counter = 0,
  38. ccp_node = false,
  39. ccp_mode = false,
  40. ccp_inst = false,
  41. themes_loaded = [],
  42. src = $('script:last').attr('src'),
  43. document = window.document; // local variable is always faster to access then a global
  44. /**
  45. * holds all jstree related functions and variables, including the actual class and methods to create, access and manipulate instances.
  46. * @name $.jstree
  47. */
  48. $.jstree = {
  49. /**
  50. * specifies the jstree version in use
  51. * @name $.jstree.version
  52. */
  53. version : '3.3.4',
  54. /**
  55. * holds all the default options used when creating new instances
  56. * @name $.jstree.defaults
  57. */
  58. defaults : {
  59. /**
  60. * configure which plugins will be active on an instance. Should be an array of strings, where each element is a plugin name. The default is `[]`
  61. * @name $.jstree.defaults.plugins
  62. */
  63. plugins : []
  64. },
  65. /**
  66. * stores all loaded jstree plugins (used internally)
  67. * @name $.jstree.plugins
  68. */
  69. plugins : {},
  70. path : src && src.indexOf('/') !== -1 ? src.replace(/\/[^\/]+$/,'') : '',
  71. idregex : /[\\:&!^|()\[\]<>@*'+~#";.,=\- \/${}%?`]/g,
  72. root : '#'
  73. };
  74. /**
  75. * creates a jstree instance
  76. * @name $.jstree.create(el [, options])
  77. * @param {DOMElement|jQuery|String} el the element to create the instance on, can be jQuery extended or a selector
  78. * @param {Object} options options for this instance (extends `$.jstree.defaults`)
  79. * @return {jsTree} the new instance
  80. */
  81. $.jstree.create = function (el, options) {
  82. var tmp = new $.jstree.core(++instance_counter),
  83. opt = options;
  84. options = $.extend(true, {}, $.jstree.defaults, options);
  85. if(opt && opt.plugins) {
  86. options.plugins = opt.plugins;
  87. }
  88. $.each(options.plugins, function (i, k) {
  89. if(i !== 'core') {
  90. tmp = tmp.plugin(k, options[k]);
  91. }
  92. });
  93. $(el).data('jstree', tmp);
  94. tmp.init(el, options);
  95. return tmp;
  96. };
  97. /**
  98. * remove all traces of jstree from the DOM and destroy all instances
  99. * @name $.jstree.destroy()
  100. */
  101. $.jstree.destroy = function () {
  102. $('.jstree:jstree').jstree('destroy');
  103. $(document).off('.jstree');
  104. };
  105. /**
  106. * the jstree class constructor, used only internally
  107. * @private
  108. * @name $.jstree.core(id)
  109. * @param {Number} id this instance's index
  110. */
  111. $.jstree.core = function (id) {
  112. this._id = id;
  113. this._cnt = 0;
  114. this._wrk = null;
  115. this._data = {
  116. core : {
  117. themes : {
  118. name : false,
  119. dots : false,
  120. icons : false,
  121. ellipsis : false
  122. },
  123. selected : [],
  124. last_error : {},
  125. working : false,
  126. worker_queue : [],
  127. focused : null
  128. }
  129. };
  130. };
  131. /**
  132. * get a reference to an existing instance
  133. *
  134. * __Examples__
  135. *
  136. * // provided a container with an ID of "tree", and a nested node with an ID of "branch"
  137. * // all of there will return the same instance
  138. * $.jstree.reference('tree');
  139. * $.jstree.reference('#tree');
  140. * $.jstree.reference($('#tree'));
  141. * $.jstree.reference(document.getElementByID('tree'));
  142. * $.jstree.reference('branch');
  143. * $.jstree.reference('#branch');
  144. * $.jstree.reference($('#branch'));
  145. * $.jstree.reference(document.getElementByID('branch'));
  146. *
  147. * @name $.jstree.reference(needle)
  148. * @param {DOMElement|jQuery|String} needle
  149. * @return {jsTree|null} the instance or `null` if not found
  150. */
  151. $.jstree.reference = function (needle) {
  152. var tmp = null,
  153. obj = null;
  154. if(needle && needle.id && (!needle.tagName || !needle.nodeType)) { needle = needle.id; }
  155. if(!obj || !obj.length) {
  156. try { obj = $(needle); } catch (ignore) { }
  157. }
  158. if(!obj || !obj.length) {
  159. try { obj = $('#' + needle.replace($.jstree.idregex,'\\$&')); } catch (ignore) { }
  160. }
  161. if(obj && obj.length && (obj = obj.closest('.jstree')).length && (obj = obj.data('jstree'))) {
  162. tmp = obj;
  163. }
  164. else {
  165. $('.jstree').each(function () {
  166. var inst = $(this).data('jstree');
  167. if(inst && inst._model.data[needle]) {
  168. tmp = inst;
  169. return false;
  170. }
  171. });
  172. }
  173. return tmp;
  174. };
  175. /**
  176. * Create an instance, get an instance or invoke a command on a instance.
  177. *
  178. * If there is no instance associated with the current node a new one is created and `arg` is used to extend `$.jstree.defaults` for this new instance. There would be no return value (chaining is not broken).
  179. *
  180. * If there is an existing instance and `arg` is a string the command specified by `arg` is executed on the instance, with any additional arguments passed to the function. If the function returns a value it will be returned (chaining could break depending on function).
  181. *
  182. * If there is an existing instance and `arg` is not a string the instance itself is returned (similar to `$.jstree.reference`).
  183. *
  184. * In any other case - nothing is returned and chaining is not broken.
  185. *
  186. * __Examples__
  187. *
  188. * $('#tree1').jstree(); // creates an instance
  189. * $('#tree2').jstree({ plugins : [] }); // create an instance with some options
  190. * $('#tree1').jstree('open_node', '#branch_1'); // call a method on an existing instance, passing additional arguments
  191. * $('#tree2').jstree(); // get an existing instance (or create an instance)
  192. * $('#tree2').jstree(true); // get an existing instance (will not create new instance)
  193. * $('#branch_1').jstree().select_node('#branch_1'); // get an instance (using a nested element and call a method)
  194. *
  195. * @name $().jstree([arg])
  196. * @param {String|Object} arg
  197. * @return {Mixed}
  198. */
  199. $.fn.jstree = function (arg) {
  200. // check for string argument
  201. var is_method = (typeof arg === 'string'),
  202. args = Array.prototype.slice.call(arguments, 1),
  203. result = null;
  204. if(arg === true && !this.length) { return false; }
  205. this.each(function () {
  206. // get the instance (if there is one) and method (if it exists)
  207. var instance = $.jstree.reference(this),
  208. method = is_method && instance ? instance[arg] : null;
  209. // if calling a method, and method is available - execute on the instance
  210. result = is_method && method ?
  211. method.apply(instance, args) :
  212. null;
  213. // if there is no instance and no method is being called - create one
  214. if(!instance && !is_method && (arg === undefined || $.isPlainObject(arg))) {
  215. $.jstree.create(this, arg);
  216. }
  217. // if there is an instance and no method is called - return the instance
  218. if( (instance && !is_method) || arg === true ) {
  219. result = instance || false;
  220. }
  221. // if there was a method call which returned a result - break and return the value
  222. if(result !== null && result !== undefined) {
  223. return false;
  224. }
  225. });
  226. // if there was a method call with a valid return value - return that, otherwise continue the chain
  227. return result !== null && result !== undefined ?
  228. result : this;
  229. };
  230. /**
  231. * used to find elements containing an instance
  232. *
  233. * __Examples__
  234. *
  235. * $('div:jstree').each(function () {
  236. * $(this).jstree('destroy');
  237. * });
  238. *
  239. * @name $(':jstree')
  240. * @return {jQuery}
  241. */
  242. $.expr.pseudos.jstree = $.expr.createPseudo(function(search) {
  243. return function(a) {
  244. return $(a).hasClass('jstree') &&
  245. $(a).data('jstree') !== undefined;
  246. };
  247. });
  248. /**
  249. * stores all defaults for the core
  250. * @name $.jstree.defaults.core
  251. */
  252. $.jstree.defaults.core = {
  253. /**
  254. * data configuration
  255. *
  256. * If left as `false` the HTML inside the jstree container element is used to populate the tree (that should be an unordered list with list items).
  257. *
  258. * You can also pass in a HTML string or a JSON array here.
  259. *
  260. * It is possible to pass in a standard jQuery-like AJAX config and jstree will automatically determine if the response is JSON or HTML and use that to populate the tree.
  261. * In addition to the standard jQuery ajax options here you can suppy functions for `data` and `url`, the functions will be run in the current instance's scope and a param will be passed indicating which node is being loaded, the return value of those functions will be used.
  262. *
  263. * The last option is to specify a function, that function will receive the node being loaded as argument and a second param which is a function which should be called with the result.
  264. *
  265. * __Examples__
  266. *
  267. * // AJAX
  268. * $('#tree').jstree({
  269. * 'core' : {
  270. * 'data' : {
  271. * 'url' : '/get/children/',
  272. * 'data' : function (node) {
  273. * return { 'id' : node.id };
  274. * }
  275. * }
  276. * });
  277. *
  278. * // direct data
  279. * $('#tree').jstree({
  280. * 'core' : {
  281. * 'data' : [
  282. * 'Simple root node',
  283. * {
  284. * 'id' : 'node_2',
  285. * 'text' : 'Root node with options',
  286. * 'state' : { 'opened' : true, 'selected' : true },
  287. * 'children' : [ { 'text' : 'Child 1' }, 'Child 2']
  288. * }
  289. * ]
  290. * }
  291. * });
  292. *
  293. * // function
  294. * $('#tree').jstree({
  295. * 'core' : {
  296. * 'data' : function (obj, callback) {
  297. * callback.call(this, ['Root 1', 'Root 2']);
  298. * }
  299. * });
  300. *
  301. * @name $.jstree.defaults.core.data
  302. */
  303. data : false,
  304. /**
  305. * configure the various strings used throughout the tree
  306. *
  307. * You can use an object where the key is the string you need to replace and the value is your replacement.
  308. * Another option is to specify a function which will be called with an argument of the needed string and should return the replacement.
  309. * If left as `false` no replacement is made.
  310. *
  311. * __Examples__
  312. *
  313. * $('#tree').jstree({
  314. * 'core' : {
  315. * 'strings' : {
  316. * 'Loading ...' : 'Please wait ...'
  317. * }
  318. * }
  319. * });
  320. *
  321. * @name $.jstree.defaults.core.strings
  322. */
  323. strings : false,
  324. /**
  325. * determines what happens when a user tries to modify the structure of the tree
  326. * If left as `false` all operations like create, rename, delete, move or copy are prevented.
  327. * You can set this to `true` to allow all interactions or use a function to have better control.
  328. *
  329. * __Examples__
  330. *
  331. * $('#tree').jstree({
  332. * 'core' : {
  333. * 'check_callback' : function (operation, node, node_parent, node_position, more) {
  334. * // operation can be 'create_node', 'rename_node', 'delete_node', 'move_node', 'copy_node' or 'edit'
  335. * // in case of 'rename_node' node_position is filled with the new node name
  336. * return operation === 'rename_node' ? true : false;
  337. * }
  338. * }
  339. * });
  340. *
  341. * @name $.jstree.defaults.core.check_callback
  342. */
  343. check_callback : false,
  344. /**
  345. * a callback called with a single object parameter in the instance's scope when something goes wrong (operation prevented, ajax failed, etc)
  346. * @name $.jstree.defaults.core.error
  347. */
  348. error : $.noop,
  349. /**
  350. * the open / close animation duration in milliseconds - set this to `false` to disable the animation (default is `200`)
  351. * @name $.jstree.defaults.core.animation
  352. */
  353. animation : 200,
  354. /**
  355. * a boolean indicating if multiple nodes can be selected
  356. * @name $.jstree.defaults.core.multiple
  357. */
  358. multiple : true,
  359. /**
  360. * theme configuration object
  361. * @name $.jstree.defaults.core.themes
  362. */
  363. themes : {
  364. /**
  365. * the name of the theme to use (if left as `false` the default theme is used)
  366. * @name $.jstree.defaults.core.themes.name
  367. */
  368. name : false,
  369. /**
  370. * the URL of the theme's CSS file, leave this as `false` if you have manually included the theme CSS (recommended). You can set this to `true` too which will try to autoload the theme.
  371. * @name $.jstree.defaults.core.themes.url
  372. */
  373. url : false,
  374. /**
  375. * the location of all jstree themes - only used if `url` is set to `true`
  376. * @name $.jstree.defaults.core.themes.dir
  377. */
  378. dir : false,
  379. /**
  380. * a boolean indicating if connecting dots are shown
  381. * @name $.jstree.defaults.core.themes.dots
  382. */
  383. dots : true,
  384. /**
  385. * a boolean indicating if node icons are shown
  386. * @name $.jstree.defaults.core.themes.icons
  387. */
  388. icons : true,
  389. /**
  390. * a boolean indicating if node ellipsis should be shown - this only works with a fixed with on the container
  391. * @name $.jstree.defaults.core.themes.ellipsis
  392. */
  393. ellipsis : false,
  394. /**
  395. * a boolean indicating if the tree background is striped
  396. * @name $.jstree.defaults.core.themes.stripes
  397. */
  398. stripes : false,
  399. /**
  400. * a string (or boolean `false`) specifying the theme variant to use (if the theme supports variants)
  401. * @name $.jstree.defaults.core.themes.variant
  402. */
  403. variant : false,
  404. /**
  405. * a boolean specifying if a reponsive version of the theme should kick in on smaller screens (if the theme supports it). Defaults to `false`.
  406. * @name $.jstree.defaults.core.themes.responsive
  407. */
  408. responsive : false
  409. },
  410. /**
  411. * if left as `true` all parents of all selected nodes will be opened once the tree loads (so that all selected nodes are visible to the user)
  412. * @name $.jstree.defaults.core.expand_selected_onload
  413. */
  414. expand_selected_onload : true,
  415. /**
  416. * if left as `true` web workers will be used to parse incoming JSON data where possible, so that the UI will not be blocked by large requests. Workers are however about 30% slower. Defaults to `true`
  417. * @name $.jstree.defaults.core.worker
  418. */
  419. worker : true,
  420. /**
  421. * Force node text to plain text (and escape HTML). Defaults to `false`
  422. * @name $.jstree.defaults.core.force_text
  423. */
  424. force_text : false,
  425. /**
  426. * Should the node should be toggled if the text is double clicked . Defaults to `true`
  427. * @name $.jstree.defaults.core.dblclick_toggle
  428. */
  429. dblclick_toggle : true
  430. };
  431. $.jstree.core.prototype = {
  432. /**
  433. * used to decorate an instance with a plugin. Used internally.
  434. * @private
  435. * @name plugin(deco [, opts])
  436. * @param {String} deco the plugin to decorate with
  437. * @param {Object} opts options for the plugin
  438. * @return {jsTree}
  439. */
  440. plugin : function (deco, opts) {
  441. var Child = $.jstree.plugins[deco];
  442. if(Child) {
  443. this._data[deco] = {};
  444. Child.prototype = this;
  445. return new Child(opts, this);
  446. }
  447. return this;
  448. },
  449. /**
  450. * initialize the instance. Used internally.
  451. * @private
  452. * @name init(el, optons)
  453. * @param {DOMElement|jQuery|String} el the element we are transforming
  454. * @param {Object} options options for this instance
  455. * @trigger init.jstree, loading.jstree, loaded.jstree, ready.jstree, changed.jstree
  456. */
  457. init : function (el, options) {
  458. this._model = {
  459. data : {},
  460. changed : [],
  461. force_full_redraw : false,
  462. redraw_timeout : false,
  463. default_state : {
  464. loaded : true,
  465. opened : false,
  466. selected : false,
  467. disabled : false
  468. }
  469. };
  470. this._model.data[$.jstree.root] = {
  471. id : $.jstree.root,
  472. parent : null,
  473. parents : [],
  474. children : [],
  475. children_d : [],
  476. state : { loaded : false }
  477. };
  478. this.element = $(el).addClass('jstree jstree-' + this._id);
  479. this.settings = options;
  480. this._data.core.ready = false;
  481. this._data.core.loaded = false;
  482. this._data.core.rtl = (this.element.css("direction") === "rtl");
  483. this.element[this._data.core.rtl ? 'addClass' : 'removeClass']("jstree-rtl");
  484. this.element.attr('role','tree');
  485. if(this.settings.core.multiple) {
  486. this.element.attr('aria-multiselectable', true);
  487. }
  488. if(!this.element.attr('tabindex')) {
  489. this.element.attr('tabindex','0');
  490. }
  491. this.bind();
  492. /**
  493. * triggered after all events are bound
  494. * @event
  495. * @name init.jstree
  496. */
  497. this.trigger("init");
  498. this._data.core.original_container_html = this.element.find(" > ul > li").clone(true);
  499. this._data.core.original_container_html
  500. .find("li").addBack()
  501. .contents().filter(function() {
  502. return this.nodeType === 3 && (!this.nodeValue || /^\s+$/.test(this.nodeValue));
  503. })
  504. .remove();
  505. this.element.html("<"+"ul class='jstree-container-ul jstree-children' role='group'><"+"li id='j"+this._id+"_loading' class='jstree-initial-node jstree-loading jstree-leaf jstree-last' role='tree-item'><i class='jstree-icon jstree-ocl'></i><"+"a class='jstree-anchor' href='#'><i class='jstree-icon jstree-themeicon-hidden'></i>" + this.get_string("Loading ...") + "</a></li></ul>");
  506. this.element.attr('aria-activedescendant','j' + this._id + '_loading');
  507. this._data.core.li_height = this.get_container_ul().children("li").first().outerHeight() || 24;
  508. this._data.core.node = this._create_prototype_node();
  509. /**
  510. * triggered after the loading text is shown and before loading starts
  511. * @event
  512. * @name loading.jstree
  513. */
  514. this.trigger("loading");
  515. this.load_node($.jstree.root);
  516. },
  517. /**
  518. * destroy an instance
  519. * @name destroy()
  520. * @param {Boolean} keep_html if not set to `true` the container will be emptied, otherwise the current DOM elements will be kept intact
  521. */
  522. destroy : function (keep_html) {
  523. /**
  524. * triggered before the tree is destroyed
  525. * @event
  526. * @name destroy.jstree
  527. */
  528. this.trigger("destroy");
  529. if(this._wrk) {
  530. try {
  531. window.URL.revokeObjectURL(this._wrk);
  532. this._wrk = null;
  533. }
  534. catch (ignore) { }
  535. }
  536. if(!keep_html) { this.element.empty(); }
  537. this.teardown();
  538. },
  539. /**
  540. * Create prototype node
  541. */
  542. _create_prototype_node : function () {
  543. var _node = document.createElement('LI'), _temp1, _temp2;
  544. _node.setAttribute('role', 'treeitem');
  545. _temp1 = document.createElement('I');
  546. _temp1.className = 'jstree-icon jstree-ocl';
  547. _temp1.setAttribute('role', 'presentation');
  548. _node.appendChild(_temp1);
  549. _temp1 = document.createElement('A');
  550. _temp1.className = 'jstree-anchor';
  551. _temp1.setAttribute('href','#');
  552. _temp1.setAttribute('tabindex','-1');
  553. _temp2 = document.createElement('I');
  554. _temp2.className = 'jstree-icon jstree-themeicon';
  555. _temp2.setAttribute('role', 'presentation');
  556. _temp1.appendChild(_temp2);
  557. _node.appendChild(_temp1);
  558. _temp1 = _temp2 = null;
  559. return _node;
  560. },
  561. /**
  562. * part of the destroying of an instance. Used internally.
  563. * @private
  564. * @name teardown()
  565. */
  566. teardown : function () {
  567. this.unbind();
  568. this.element
  569. .removeClass('jstree')
  570. .removeData('jstree')
  571. .find("[class^='jstree']")
  572. .addBack()
  573. .attr("class", function () { return this.className.replace(/jstree[^ ]*|$/ig,''); });
  574. this.element = null;
  575. },
  576. /**
  577. * bind all events. Used internally.
  578. * @private
  579. * @name bind()
  580. */
  581. bind : function () {
  582. var word = '',
  583. tout = null,
  584. was_click = 0;
  585. this.element
  586. .on("dblclick.jstree", function (e) {
  587. if(e.target.tagName && e.target.tagName.toLowerCase() === "input") { return true; }
  588. if(document.selection && document.selection.empty) {
  589. document.selection.empty();
  590. }
  591. else {
  592. if(window.getSelection) {
  593. var sel = window.getSelection();
  594. try {
  595. sel.removeAllRanges();
  596. sel.collapse();
  597. } catch (ignore) { }
  598. }
  599. }
  600. })
  601. .on("mousedown.jstree", $.proxy(function (e) {
  602. if(e.target === this.element[0]) {
  603. e.preventDefault(); // prevent losing focus when clicking scroll arrows (FF, Chrome)
  604. was_click = +(new Date()); // ie does not allow to prevent losing focus
  605. }
  606. }, this))
  607. .on("mousedown.jstree", ".jstree-ocl", function (e) {
  608. e.preventDefault(); // prevent any node inside from losing focus when clicking the open/close icon
  609. })
  610. .on("click.jstree", ".jstree-ocl", $.proxy(function (e) {
  611. this.toggle_node(e.target);
  612. }, this))
  613. .on("dblclick.jstree", ".jstree-anchor", $.proxy(function (e) {
  614. if(e.target.tagName && e.target.tagName.toLowerCase() === "input") { return true; }
  615. if(this.settings.core.dblclick_toggle) {
  616. this.toggle_node(e.target);
  617. }
  618. }, this))
  619. .on("click.jstree", ".jstree-anchor", $.proxy(function (e) {
  620. e.preventDefault();
  621. if(e.currentTarget !== document.activeElement) { $(e.currentTarget).focus(); }
  622. this.activate_node(e.currentTarget, e);
  623. }, this))
  624. .on('keydown.jstree', '.jstree-anchor', $.proxy(function (e) {
  625. if(e.target.tagName && e.target.tagName.toLowerCase() === "input") { return true; }
  626. if(e.which !== 32 && e.which !== 13 && (e.shiftKey || e.ctrlKey || e.altKey || e.metaKey)) { return true; }
  627. var o = null;
  628. if(this._data.core.rtl) {
  629. if(e.which === 37) { e.which = 39; }
  630. else if(e.which === 39) { e.which = 37; }
  631. }
  632. switch(e.which) {
  633. case 32: // aria defines space only with Ctrl
  634. if(e.ctrlKey) {
  635. e.type = "click";
  636. $(e.currentTarget).trigger(e);
  637. }
  638. break;
  639. case 13: // enter
  640. e.type = "click";
  641. $(e.currentTarget).trigger(e);
  642. break;
  643. case 37: // left
  644. e.preventDefault();
  645. if(this.is_open(e.currentTarget)) {
  646. this.close_node(e.currentTarget);
  647. }
  648. else {
  649. o = this.get_parent(e.currentTarget);
  650. if(o && o.id !== $.jstree.root) { this.get_node(o, true).children('.jstree-anchor').focus(); }
  651. }
  652. break;
  653. case 38: // up
  654. e.preventDefault();
  655. o = this.get_prev_dom(e.currentTarget);
  656. if(o && o.length) { o.children('.jstree-anchor').focus(); }
  657. break;
  658. case 39: // right
  659. e.preventDefault();
  660. if(this.is_closed(e.currentTarget)) {
  661. this.open_node(e.currentTarget, function (o) { this.get_node(o, true).children('.jstree-anchor').focus(); });
  662. }
  663. else if (this.is_open(e.currentTarget)) {
  664. o = this.get_node(e.currentTarget, true).children('.jstree-children')[0];
  665. if(o) { $(this._firstChild(o)).children('.jstree-anchor').focus(); }
  666. }
  667. break;
  668. case 40: // down
  669. e.preventDefault();
  670. o = this.get_next_dom(e.currentTarget);
  671. if(o && o.length) { o.children('.jstree-anchor').focus(); }
  672. break;
  673. case 106: // aria defines * on numpad as open_all - not very common
  674. this.open_all();
  675. break;
  676. case 36: // home
  677. e.preventDefault();
  678. o = this._firstChild(this.get_container_ul()[0]);
  679. if(o) { $(o).children('.jstree-anchor').filter(':visible').focus(); }
  680. break;
  681. case 35: // end
  682. e.preventDefault();
  683. this.element.find('.jstree-anchor').filter(':visible').last().focus();
  684. break;
  685. case 113: // f2 - safe to include - if check_callback is false it will fail
  686. e.preventDefault();
  687. this.edit(e.currentTarget);
  688. break;
  689. default:
  690. break;
  691. /*!
  692. // delete
  693. case 46:
  694. e.preventDefault();
  695. o = this.get_node(e.currentTarget);
  696. if(o && o.id && o.id !== $.jstree.root) {
  697. o = this.is_selected(o) ? this.get_selected() : o;
  698. this.delete_node(o);
  699. }
  700. break;
  701. */
  702. }
  703. }, this))
  704. .on("load_node.jstree", $.proxy(function (e, data) {
  705. if(data.status) {
  706. if(data.node.id === $.jstree.root && !this._data.core.loaded) {
  707. this._data.core.loaded = true;
  708. if(this._firstChild(this.get_container_ul()[0])) {
  709. this.element.attr('aria-activedescendant',this._firstChild(this.get_container_ul()[0]).id);
  710. }
  711. /**
  712. * triggered after the root node is loaded for the first time
  713. * @event
  714. * @name loaded.jstree
  715. */
  716. this.trigger("loaded");
  717. }
  718. if(!this._data.core.ready) {
  719. setTimeout($.proxy(function() {
  720. if(this.element && !this.get_container_ul().find('.jstree-loading').length) {
  721. this._data.core.ready = true;
  722. if(this._data.core.selected.length) {
  723. if(this.settings.core.expand_selected_onload) {
  724. var tmp = [], i, j;
  725. for(i = 0, j = this._data.core.selected.length; i < j; i++) {
  726. tmp = tmp.concat(this._model.data[this._data.core.selected[i]].parents);
  727. }
  728. tmp = $.vakata.array_unique(tmp);
  729. for(i = 0, j = tmp.length; i < j; i++) {
  730. this.open_node(tmp[i], false, 0);
  731. }
  732. }
  733. this.trigger('changed', { 'action' : 'ready', 'selected' : this._data.core.selected });
  734. }
  735. /**
  736. * triggered after all nodes are finished loading
  737. * @event
  738. * @name ready.jstree
  739. */
  740. this.trigger("ready");
  741. }
  742. }, this), 0);
  743. }
  744. }
  745. }, this))
  746. // quick searching when the tree is focused
  747. .on('keypress.jstree', $.proxy(function (e) {
  748. if(e.target.tagName && e.target.tagName.toLowerCase() === "input") { return true; }
  749. if(tout) { clearTimeout(tout); }
  750. tout = setTimeout(function () {
  751. word = '';
  752. }, 500);
  753. var chr = String.fromCharCode(e.which).toLowerCase(),
  754. col = this.element.find('.jstree-anchor').filter(':visible'),
  755. ind = col.index(document.activeElement) || 0,
  756. end = false;
  757. word += chr;
  758. // match for whole word from current node down (including the current node)
  759. if(word.length > 1) {
  760. col.slice(ind).each($.proxy(function (i, v) {
  761. if($(v).text().toLowerCase().indexOf(word) === 0) {
  762. $(v).focus();
  763. end = true;
  764. return false;
  765. }
  766. }, this));
  767. if(end) { return; }
  768. // match for whole word from the beginning of the tree
  769. col.slice(0, ind).each($.proxy(function (i, v) {
  770. if($(v).text().toLowerCase().indexOf(word) === 0) {
  771. $(v).focus();
  772. end = true;
  773. return false;
  774. }
  775. }, this));
  776. if(end) { return; }
  777. }
  778. // list nodes that start with that letter (only if word consists of a single char)
  779. if(new RegExp('^' + chr.replace(/[-\/\\^$*+?.()|[\]{}]/g, '\\$&') + '+$').test(word)) {
  780. // search for the next node starting with that letter
  781. col.slice(ind + 1).each($.proxy(function (i, v) {
  782. if($(v).text().toLowerCase().charAt(0) === chr) {
  783. $(v).focus();
  784. end = true;
  785. return false;
  786. }
  787. }, this));
  788. if(end) { return; }
  789. // search from the beginning
  790. col.slice(0, ind + 1).each($.proxy(function (i, v) {
  791. if($(v).text().toLowerCase().charAt(0) === chr) {
  792. $(v).focus();
  793. end = true;
  794. return false;
  795. }
  796. }, this));
  797. if(end) { return; }
  798. }
  799. }, this))
  800. // THEME RELATED
  801. .on("init.jstree", $.proxy(function () {
  802. var s = this.settings.core.themes;
  803. this._data.core.themes.dots = s.dots;
  804. this._data.core.themes.stripes = s.stripes;
  805. this._data.core.themes.icons = s.icons;
  806. this._data.core.themes.ellipsis = s.ellipsis;
  807. this.set_theme(s.name || "default", s.url);
  808. this.set_theme_variant(s.variant);
  809. }, this))
  810. .on("loading.jstree", $.proxy(function () {
  811. this[ this._data.core.themes.dots ? "show_dots" : "hide_dots" ]();
  812. this[ this._data.core.themes.icons ? "show_icons" : "hide_icons" ]();
  813. this[ this._data.core.themes.stripes ? "show_stripes" : "hide_stripes" ]();
  814. this[ this._data.core.themes.ellipsis ? "show_ellipsis" : "hide_ellipsis" ]();
  815. }, this))
  816. .on('blur.jstree', '.jstree-anchor', $.proxy(function (e) {
  817. this._data.core.focused = null;
  818. $(e.currentTarget).filter('.jstree-hovered').mouseleave();
  819. this.element.attr('tabindex', '0');
  820. }, this))
  821. .on('focus.jstree', '.jstree-anchor', $.proxy(function (e) {
  822. var tmp = this.get_node(e.currentTarget);
  823. if(tmp && tmp.id) {
  824. this._data.core.focused = tmp.id;
  825. }
  826. this.element.find('.jstree-hovered').not(e.currentTarget).mouseleave();
  827. $(e.currentTarget).mouseenter();
  828. this.element.attr('tabindex', '-1');
  829. }, this))
  830. .on('focus.jstree', $.proxy(function () {
  831. if(+(new Date()) - was_click > 500 && !this._data.core.focused) {
  832. was_click = 0;
  833. var act = this.get_node(this.element.attr('aria-activedescendant'), true);
  834. if(act) {
  835. act.find('> .jstree-anchor').focus();
  836. }
  837. }
  838. }, this))
  839. .on('mouseenter.jstree', '.jstree-anchor', $.proxy(function (e) {
  840. this.hover_node(e.currentTarget);
  841. }, this))
  842. .on('mouseleave.jstree', '.jstree-anchor', $.proxy(function (e) {
  843. this.dehover_node(e.currentTarget);
  844. }, this));
  845. },
  846. /**
  847. * part of the destroying of an instance. Used internally.
  848. * @private
  849. * @name unbind()
  850. */
  851. unbind : function () {
  852. this.element.off('.jstree');
  853. $(document).off('.jstree-' + this._id);
  854. },
  855. /**
  856. * trigger an event. Used internally.
  857. * @private
  858. * @name trigger(ev [, data])
  859. * @param {String} ev the name of the event to trigger
  860. * @param {Object} data additional data to pass with the event
  861. */
  862. trigger : function (ev, data) {
  863. if(!data) {
  864. data = {};
  865. }
  866. data.instance = this;
  867. this.element.triggerHandler(ev.replace('.jstree','') + '.jstree', data);
  868. },
  869. /**
  870. * returns the jQuery extended instance container
  871. * @name get_container()
  872. * @return {jQuery}
  873. */
  874. get_container : function () {
  875. return this.element;
  876. },
  877. /**
  878. * returns the jQuery extended main UL node inside the instance container. Used internally.
  879. * @private
  880. * @name get_container_ul()
  881. * @return {jQuery}
  882. */
  883. get_container_ul : function () {
  884. return this.element.children(".jstree-children").first();
  885. },
  886. /**
  887. * gets string replacements (localization). Used internally.
  888. * @private
  889. * @name get_string(key)
  890. * @param {String} key
  891. * @return {String}
  892. */
  893. get_string : function (key) {
  894. var a = this.settings.core.strings;
  895. if($.isFunction(a)) { return a.call(this, key); }
  896. if(a && a[key]) { return a[key]; }
  897. return key;
  898. },
  899. /**
  900. * gets the first child of a DOM node. Used internally.
  901. * @private
  902. * @name _firstChild(dom)
  903. * @param {DOMElement} dom
  904. * @return {DOMElement}
  905. */
  906. _firstChild : function (dom) {
  907. dom = dom ? dom.firstChild : null;
  908. while(dom !== null && dom.nodeType !== 1) {
  909. dom = dom.nextSibling;
  910. }
  911. return dom;
  912. },
  913. /**
  914. * gets the next sibling of a DOM node. Used internally.
  915. * @private
  916. * @name _nextSibling(dom)
  917. * @param {DOMElement} dom
  918. * @return {DOMElement}
  919. */
  920. _nextSibling : function (dom) {
  921. dom = dom ? dom.nextSibling : null;
  922. while(dom !== null && dom.nodeType !== 1) {
  923. dom = dom.nextSibling;
  924. }
  925. return dom;
  926. },
  927. /**
  928. * gets the previous sibling of a DOM node. Used internally.
  929. * @private
  930. * @name _previousSibling(dom)
  931. * @param {DOMElement} dom
  932. * @return {DOMElement}
  933. */
  934. _previousSibling : function (dom) {
  935. dom = dom ? dom.previousSibling : null;
  936. while(dom !== null && dom.nodeType !== 1) {
  937. dom = dom.previousSibling;
  938. }
  939. return dom;
  940. },
  941. /**
  942. * get the JSON representation of a node (or the actual jQuery extended DOM node) by using any input (child DOM element, ID string, selector, etc)
  943. * @name get_node(obj [, as_dom])
  944. * @param {mixed} obj
  945. * @param {Boolean} as_dom
  946. * @return {Object|jQuery}
  947. */
  948. get_node : function (obj, as_dom) {
  949. if(obj && obj.id) {
  950. obj = obj.id;
  951. }
  952. var dom;
  953. try {
  954. if(this._model.data[obj]) {
  955. obj = this._model.data[obj];
  956. }
  957. else if(typeof obj === "string" && this._model.data[obj.replace(/^#/, '')]) {
  958. obj = this._model.data[obj.replace(/^#/, '')];
  959. }
  960. else if(typeof obj === "string" && (dom = $('#' + obj.replace($.jstree.idregex,'\\$&'), this.element)).length && this._model.data[dom.closest('.jstree-node').attr('id')]) {
  961. obj = this._model.data[dom.closest('.jstree-node').attr('id')];
  962. }
  963. else if((dom = $(obj, this.element)).length && this._model.data[dom.closest('.jstree-node').attr('id')]) {
  964. obj = this._model.data[dom.closest('.jstree-node').attr('id')];
  965. }
  966. else if((dom = $(obj, this.element)).length && dom.hasClass('jstree')) {
  967. obj = this._model.data[$.jstree.root];
  968. }
  969. else {
  970. return false;
  971. }
  972. if(as_dom) {
  973. obj = obj.id === $.jstree.root ? this.element : $('#' + obj.id.replace($.jstree.idregex,'\\$&'), this.element);
  974. }
  975. return obj;
  976. } catch (ex) { return false; }
  977. },
  978. /**
  979. * get the path to a node, either consisting of node texts, or of node IDs, optionally glued together (otherwise an array)
  980. * @name get_path(obj [, glue, ids])
  981. * @param {mixed} obj the node
  982. * @param {String} glue if you want the path as a string - pass the glue here (for example '/'), if a falsy value is supplied here, an array is returned
  983. * @param {Boolean} ids if set to true build the path using ID, otherwise node text is used
  984. * @return {mixed}
  985. */
  986. get_path : function (obj, glue, ids) {
  987. obj = obj.parents ? obj : this.get_node(obj);
  988. if(!obj || obj.id === $.jstree.root || !obj.parents) {
  989. return false;
  990. }
  991. var i, j, p = [];
  992. p.push(ids ? obj.id : obj.text);
  993. for(i = 0, j = obj.parents.length; i < j; i++) {
  994. p.push(ids ? obj.parents[i] : this.get_text(obj.parents[i]));
  995. }
  996. p = p.reverse().slice(1);
  997. return glue ? p.join(glue) : p;
  998. },
  999. /**
  1000. * get the next visible node that is below the `obj` node. If `strict` is set to `true` only sibling nodes are returned.
  1001. * @name get_next_dom(obj [, strict])
  1002. * @param {mixed} obj
  1003. * @param {Boolean} strict
  1004. * @return {jQuery}
  1005. */
  1006. get_next_dom : function (obj, strict) {
  1007. var tmp;
  1008. obj = this.get_node(obj, true);
  1009. if(obj[0] === this.element[0]) {
  1010. tmp = this._firstChild(this.get_container_ul()[0]);
  1011. while (tmp && tmp.offsetHeight === 0) {
  1012. tmp = this._nextSibling(tmp);
  1013. }
  1014. return tmp ? $(tmp) : false;
  1015. }
  1016. if(!obj || !obj.length) {
  1017. return false;
  1018. }
  1019. if(strict) {
  1020. tmp = obj[0];
  1021. do {
  1022. tmp = this._nextSibling(tmp);
  1023. } while (tmp && tmp.offsetHeight === 0);
  1024. return tmp ? $(tmp) : false;
  1025. }
  1026. if(obj.hasClass("jstree-open")) {
  1027. tmp = this._firstChild(obj.children('.jstree-children')[0]);
  1028. while (tmp && tmp.offsetHeight === 0) {
  1029. tmp = this._nextSibling(tmp);
  1030. }
  1031. if(tmp !== null) {
  1032. return $(tmp);
  1033. }
  1034. }
  1035. tmp = obj[0];
  1036. do {
  1037. tmp = this._nextSibling(tmp);
  1038. } while (tmp && tmp.offsetHeight === 0);
  1039. if(tmp !== null) {
  1040. return $(tmp);
  1041. }
  1042. return obj.parentsUntil(".jstree",".jstree-node").nextAll(".jstree-node:visible").first();
  1043. },
  1044. /**
  1045. * get the previous visible node that is above the `obj` node. If `strict` is set to `true` only sibling nodes are returned.
  1046. * @name get_prev_dom(obj [, strict])
  1047. * @param {mixed} obj
  1048. * @param {Boolean} strict
  1049. * @return {jQuery}
  1050. */
  1051. get_prev_dom : function (obj, strict) {
  1052. var tmp;
  1053. obj = this.get_node(obj, true);
  1054. if(obj[0] === this.element[0]) {
  1055. tmp = this.get_container_ul()[0].lastChild;
  1056. while (tmp && tmp.offsetHeight === 0) {
  1057. tmp = this._previousSibling(tmp);
  1058. }
  1059. return tmp ? $(tmp) : false;
  1060. }
  1061. if(!obj || !obj.length) {
  1062. return false;
  1063. }
  1064. if(strict) {
  1065. tmp = obj[0];
  1066. do {
  1067. tmp = this._previousSibling(tmp);
  1068. } while (tmp && tmp.offsetHeight === 0);
  1069. return tmp ? $(tmp) : false;
  1070. }
  1071. tmp = obj[0];
  1072. do {
  1073. tmp = this._previousSibling(tmp);
  1074. } while (tmp && tmp.offsetHeight === 0);
  1075. if(tmp !== null) {
  1076. obj = $(tmp);
  1077. while(obj.hasClass("jstree-open")) {
  1078. obj = obj.children(".jstree-children").first().children(".jstree-node:visible:last");
  1079. }
  1080. return obj;
  1081. }
  1082. tmp = obj[0].parentNode.parentNode;
  1083. return tmp && tmp.className && tmp.className.indexOf('jstree-node') !== -1 ? $(tmp) : false;
  1084. },
  1085. /**
  1086. * get the parent ID of a node
  1087. * @name get_parent(obj)
  1088. * @param {mixed} obj
  1089. * @return {String}
  1090. */
  1091. get_parent : function (obj) {
  1092. obj = this.get_node(obj);
  1093. if(!obj || obj.id === $.jstree.root) {
  1094. return false;
  1095. }
  1096. return obj.parent;
  1097. },
  1098. /**
  1099. * get a jQuery collection of all the children of a node (node must be rendered)
  1100. * @name get_children_dom(obj)
  1101. * @param {mixed} obj
  1102. * @return {jQuery}
  1103. */
  1104. get_children_dom : function (obj) {
  1105. obj = this.get_node(obj, true);
  1106. if(obj[0] === this.element[0]) {
  1107. return this.get_container_ul().children(".jstree-node");
  1108. }
  1109. if(!obj || !obj.length) {
  1110. return false;
  1111. }
  1112. return obj.children(".jstree-children").children(".jstree-node");
  1113. },
  1114. /**
  1115. * checks if a node has children
  1116. * @name is_parent(obj)
  1117. * @param {mixed} obj
  1118. * @return {Boolean}
  1119. */
  1120. is_parent : function (obj) {
  1121. obj = this.get_node(obj);
  1122. return obj && (obj.state.loaded === false || obj.children.length > 0);
  1123. },
  1124. /**
  1125. * checks if a node is loaded (its children are available)
  1126. * @name is_loaded(obj)
  1127. * @param {mixed} obj
  1128. * @return {Boolean}
  1129. */
  1130. is_loaded : function (obj) {
  1131. obj = this.get_node(obj);
  1132. return obj && obj.state.loaded;
  1133. },
  1134. /**
  1135. * check if a node is currently loading (fetching children)
  1136. * @name is_loading(obj)
  1137. * @param {mixed} obj
  1138. * @return {Boolean}
  1139. */
  1140. is_loading : function (obj) {
  1141. obj = this.get_node(obj);
  1142. return obj && obj.state && obj.state.loading;
  1143. },
  1144. /**
  1145. * check if a node is opened
  1146. * @name is_open(obj)
  1147. * @param {mixed} obj
  1148. * @return {Boolean}
  1149. */
  1150. is_open : function (obj) {
  1151. obj = this.get_node(obj);
  1152. return obj && obj.state.opened;
  1153. },
  1154. /**
  1155. * check if a node is in a closed state
  1156. * @name is_closed(obj)
  1157. * @param {mixed} obj
  1158. * @return {Boolean}
  1159. */
  1160. is_closed : function (obj) {
  1161. obj = this.get_node(obj);
  1162. return obj && this.is_parent(obj) && !obj.state.opened;
  1163. },
  1164. /**
  1165. * check if a node has no children
  1166. * @name is_leaf(obj)
  1167. * @param {mixed} obj
  1168. * @return {Boolean}
  1169. */
  1170. is_leaf : function (obj) {
  1171. return !this.is_parent(obj);
  1172. },
  1173. /**
  1174. * loads a node (fetches its children using the `core.data` setting). Multiple nodes can be passed to by using an array.
  1175. * @name load_node(obj [, callback])
  1176. * @param {mixed} obj
  1177. * @param {function} callback a function to be executed once loading is complete, the function is executed in the instance's scope and receives two arguments - the node and a boolean status
  1178. * @return {Boolean}
  1179. * @trigger load_node.jstree
  1180. */
  1181. load_node : function (obj, callback) {
  1182. var k, l, i, j, c;
  1183. if($.isArray(obj)) {
  1184. this._load_nodes(obj.slice(), callback);
  1185. return true;
  1186. }
  1187. obj = this.get_node(obj);
  1188. if(!obj) {
  1189. if(callback) { callback.call(this, obj, false); }
  1190. return false;
  1191. }
  1192. // if(obj.state.loading) { } // the node is already loading - just wait for it to load and invoke callback? but if called implicitly it should be loaded again?
  1193. if(obj.state.loaded) {
  1194. obj.state.loaded = false;
  1195. for(i = 0, j = obj.parents.length; i < j; i++) {
  1196. this._model.data[obj.parents[i]].children_d = $.vakata.array_filter(this._model.data[obj.parents[i]].children_d, function (v) {
  1197. return $.inArray(v, obj.children_d) === -1;
  1198. });
  1199. }
  1200. for(k = 0, l = obj.children_d.length; k < l; k++) {
  1201. if(this._model.data[obj.children_d[k]].state.selected) {
  1202. c = true;
  1203. }
  1204. delete this._model.data[obj.children_d[k]];
  1205. }
  1206. if (c) {
  1207. this._data.core.selected = $.vakata.array_filter(this._data.core.selected, function (v) {
  1208. return $.inArray(v, obj.children_d) === -1;
  1209. });
  1210. }
  1211. obj.children = [];
  1212. obj.children_d = [];
  1213. if(c) {
  1214. this.trigger('changed', { 'action' : 'load_node', 'node' : obj, 'selected' : this._data.core.selected });
  1215. }
  1216. }
  1217. obj.state.failed = false;
  1218. obj.state.loading = true;
  1219. this.get_node(obj, true).addClass("jstree-loading").attr('aria-busy',true);
  1220. this._load_node(obj, $.proxy(function (status) {
  1221. obj = this._model.data[obj.id];
  1222. obj.state.loading = false;
  1223. obj.state.loaded = status;
  1224. obj.state.failed = !obj.state.loaded;
  1225. var dom = this.get_node(obj, true), i = 0, j = 0, m = this._model.data, has_children = false;
  1226. for(i = 0, j = obj.children.length; i < j; i++) {
  1227. if(m[obj.children[i]] && !m[obj.children[i]].state.hidden) {
  1228. has_children = true;
  1229. break;
  1230. }
  1231. }
  1232. if(obj.state.loaded && dom && dom.length) {
  1233. dom.removeClass('jstree-closed jstree-open jstree-leaf');
  1234. if (!has_children) {
  1235. dom.addClass('jstree-leaf');
  1236. }
  1237. else {
  1238. if (obj.id !== '#') {
  1239. dom.addClass(obj.state.opened ? 'jstree-open' : 'jstree-closed');
  1240. }
  1241. }
  1242. }
  1243. dom.removeClass("jstree-loading").attr('aria-busy',false);
  1244. /**
  1245. * triggered after a node is loaded
  1246. * @event
  1247. * @name load_node.jstree
  1248. * @param {Object} node the node that was loading
  1249. * @param {Boolean} status was the node loaded successfully
  1250. */
  1251. this.trigger('load_node', { "node" : obj, "status" : status });
  1252. if(callback) {
  1253. callback.call(this, obj, status);
  1254. }
  1255. }, this));
  1256. return true;
  1257. },
  1258. /**
  1259. * load an array of nodes (will also load unavailable nodes as soon as the appear in the structure). Used internally.
  1260. * @private
  1261. * @name _load_nodes(nodes [, callback])
  1262. * @param {array} nodes
  1263. * @param {function} callback a function to be executed once loading is complete, the function is executed in the instance's scope and receives one argument - the array passed to _load_nodes
  1264. */
  1265. _load_nodes : function (nodes, callback, is_callback, force_reload) {
  1266. var r = true,
  1267. c = function () { this._load_nodes(nodes, callback, true); },
  1268. m = this._model.data, i, j, tmp = [];
  1269. for(i = 0, j = nodes.length; i < j; i++) {
  1270. if(m[nodes[i]] && ( (!m[nodes[i]].state.loaded && !m[nodes[i]].state.failed) || (!is_callback && force_reload) )) {
  1271. if(!this.is_loading(nodes[i])) {
  1272. this.load_node(nodes[i], c);
  1273. }
  1274. r = false;
  1275. }
  1276. }
  1277. if(r) {
  1278. for(i = 0, j = nodes.length; i < j; i++) {
  1279. if(m[nodes[i]] && m[nodes[i]].state.loaded) {
  1280. tmp.push(nodes[i]);
  1281. }
  1282. }
  1283. if(callback && !callback.done) {
  1284. callback.call(this, tmp);
  1285. callback.done = true;
  1286. }
  1287. }
  1288. },
  1289. /**
  1290. * loads all unloaded nodes
  1291. * @name load_all([obj, callback])
  1292. * @param {mixed} obj the node to load recursively, omit to load all nodes in the tree
  1293. * @param {function} callback a function to be executed once loading all the nodes is complete,
  1294. * @trigger load_all.jstree
  1295. */
  1296. load_all : function (obj, callback) {
  1297. if(!obj) { obj = $.jstree.root; }
  1298. obj = this.get_node(obj);
  1299. if(!obj) { return false; }
  1300. var to_load = [],
  1301. m = this._model.data,
  1302. c = m[obj.id].children_d,
  1303. i, j;
  1304. if(obj.state && !obj.state.loaded) {
  1305. to_load.push(obj.id);
  1306. }
  1307. for(i = 0, j = c.length; i < j; i++) {
  1308. if(m[c[i]] && m[c[i]].state && !m[c[i]].state.loaded) {
  1309. to_load.push(c[i]);
  1310. }
  1311. }
  1312. if(to_load.length) {
  1313. this._load_nodes(to_load, function () {
  1314. this.load_all(obj, callback);
  1315. });
  1316. }
  1317. else {
  1318. /**
  1319. * triggered after a load_all call completes
  1320. * @event
  1321. * @name load_all.jstree
  1322. * @param {Object} node the recursively loaded node
  1323. */
  1324. if(callback) { callback.call(this, obj); }
  1325. this.trigger('load_all', { "node" : obj });
  1326. }
  1327. },
  1328. /**
  1329. * handles the actual loading of a node. Used only internally.
  1330. * @private
  1331. * @name _load_node(obj [, callback])
  1332. * @param {mixed} obj
  1333. * @param {function} callback a function to be executed once loading is complete, the function is executed in the instance's scope and receives one argument - a boolean status
  1334. * @return {Boolean}
  1335. */
  1336. _load_node : function (obj, callback) {
  1337. var s = this.settings.core.data, t;
  1338. var notTextOrCommentNode = function notTextOrCommentNode () {
  1339. return this.nodeType !== 3 && this.nodeType !== 8;
  1340. };
  1341. // use original HTML
  1342. if(!s) {
  1343. if(obj.id === $.jstree.root) {
  1344. return this._append_html_data(obj, this._data.core.original_container_html.clone(true), function (status) {
  1345. callback.call(this, status);
  1346. });
  1347. }
  1348. else {
  1349. return callback.call(this, false);
  1350. }
  1351. // return callback.call(this, obj.id === $.jstree.root ? this._append_html_data(obj, this._data.core.original_container_html.clone(true)) : false);
  1352. }
  1353. if($.isFunction(s)) {
  1354. return s.call(this, obj, $.proxy(function (d) {
  1355. if(d === false) {
  1356. callback.call(this, false);
  1357. }
  1358. else {
  1359. this[typeof d === 'string' ? '_append_html_data' : '_append_json_data'](obj, typeof d === 'string' ? $($.parseHTML(d)).filter(notTextOrCommentNode) : d, function (status) {
  1360. callback.call(this, status);
  1361. });
  1362. }
  1363. // return d === false ? callback.call(this, false) : callback.call(this, this[typeof d === 'string' ? '_append_html_data' : '_append_json_data'](obj, typeof d === 'string' ? $(d) : d));
  1364. }, this));
  1365. }
  1366. if(typeof s === 'object') {
  1367. if(s.url) {
  1368. s = $.extend(true, {}, s);
  1369. if($.isFunction(s.url)) {
  1370. s.url = s.url.call(this, obj);
  1371. }
  1372. if($.isFunction(s.data)) {
  1373. s.data = s.data.call(this, obj);
  1374. }
  1375. return $.ajax(s)
  1376. .done($.proxy(function (d,t,x) {
  1377. var type = x.getResponseHeader('Content-Type');
  1378. if((type && type.indexOf('json') !== -1) || typeof d === "object") {
  1379. return this._append_json_data(obj, d, function (status) { callback.call(this, status); });
  1380. //return callback.call(this, this._append_json_data(obj, d));
  1381. }
  1382. if((type && type.indexOf('html') !== -1) || typeof d === "string") {
  1383. return this._append_html_data(obj, $($.parseHTML(d)).filter(notTextOrCommentNode), function (status) { callback.call(this, status); });
  1384. // return callback.call(this, this._append_html_data(obj, $(d)));
  1385. }
  1386. this._data.core.last_error = { 'error' : 'ajax', 'plugin' : 'core', 'id' : 'core_04', 'reason' : 'Could not load node', 'data' : JSON.stringify({ 'id' : obj.id, 'xhr' : x }) };
  1387. this.settings.core.error.call(this, this._data.core.last_error);
  1388. return callback.call(this, false);
  1389. }, this))
  1390. .fail($.proxy(function (f) {
  1391. this._data.core.last_error = { 'error' : 'ajax', 'plugin' : 'core', 'id' : 'core_04', 'reason' : 'Could not load node', 'data' : JSON.stringify({ 'id' : obj.id, 'xhr' : f }) };
  1392. callback.call(this, false);
  1393. this.settings.core.error.call(this, this._data.core.last_error);
  1394. }, this));
  1395. }
  1396. if ($.isArray(s)) {
  1397. t = $.extend(true, [], s);
  1398. } else if ($.isPlainObject(s)) {
  1399. t = $.extend(true, {}, s);
  1400. } else {
  1401. t = s;
  1402. }
  1403. if(obj.id === $.jstree.root) {
  1404. return this._append_json_data(obj, t, function (status) {
  1405. callback.call(this, status);
  1406. });
  1407. }
  1408. else {
  1409. this._data.core.last_error = { 'error' : 'nodata', 'plugin' : 'core', 'id' : 'core_05', 'reason' : 'Could not load node', 'data' : JSON.stringify({ 'id' : obj.id }) };
  1410. this.settings.core.error.call(this, this._data.core.last_error);
  1411. return callback.call(this, false);
  1412. }
  1413. //return callback.call(this, (obj.id === $.jstree.root ? this._append_json_data(obj, t) : false) );
  1414. }
  1415. if(typeof s === 'string') {
  1416. if(obj.id === $.jstree.root) {
  1417. return this._append_html_data(obj, $($.parseHTML(s)).filter(notTextOrCommentNode), function (status) {
  1418. callback.call(this, status);
  1419. });
  1420. }
  1421. else {
  1422. this._data.core.last_error = { 'error' : 'nodata', 'plugin' : 'core', 'id' : 'core_06', 'reason' : 'Could not load node', 'data' : JSON.stringify({ 'id' : obj.id }) };
  1423. this.settings.core.error.call(this, this._data.core.last_error);
  1424. return callback.call(this, false);
  1425. }
  1426. //return callback.call(this, (obj.id === $.jstree.root ? this._append_html_data(obj, $(s)) : false) );
  1427. }
  1428. return callback.call(this, false);
  1429. },
  1430. /**
  1431. * adds a node to the list of nodes to redraw. Used only internally.
  1432. * @private
  1433. * @name _node_changed(obj [, callback])
  1434. * @param {mixed} obj
  1435. */
  1436. _node_changed : function (obj) {
  1437. obj = this.get_node(obj);
  1438. if(obj) {
  1439. this._model.changed.push(obj.id);
  1440. }
  1441. },
  1442. /**
  1443. * appends HTML content to the tree. Used internally.
  1444. * @private
  1445. * @name _append_html_data(obj, data)
  1446. * @param {mixed} obj the node to append to
  1447. * @param {String} data the HTML string to parse and append
  1448. * @trigger model.jstree, changed.jstree
  1449. */
  1450. _append_html_data : function (dom, data, cb) {
  1451. dom = this.get_node(dom);
  1452. dom.children = [];
  1453. dom.children_d = [];
  1454. var dat = data.is('ul') ? data.children() : data,
  1455. par = dom.id,
  1456. chd = [],
  1457. dpc = [],
  1458. m = this._model.data,
  1459. p = m[par],
  1460. s = this._data.core.selected.length,
  1461. tmp, i, j;
  1462. dat.each($.proxy(function (i, v) {
  1463. tmp = this._parse_model_from_html($(v), par, p.parents.concat());
  1464. if(tmp) {
  1465. chd.push(tmp);
  1466. dpc.push(tmp);
  1467. if(m[tmp].children_d.length) {
  1468. dpc = dpc.concat(m[tmp].children_d);
  1469. }
  1470. }
  1471. }, this));
  1472. p.children = chd;
  1473. p.children_d = dpc;
  1474. for(i = 0, j = p.parents.length; i < j; i++) {
  1475. m[p.parents[i]].children_d = m[p.parents[i]].children_d.concat(dpc);
  1476. }
  1477. /**
  1478. * triggered when new data is inserted to the tree model
  1479. * @event
  1480. * @name model.jstree
  1481. * @param {Array} nodes an array of node IDs
  1482. * @param {String} parent the parent ID of the nodes
  1483. */
  1484. this.trigger('model', { "nodes" : dpc, 'parent' : par });
  1485. if(par !== $.jstree.root) {
  1486. this._node_changed(par);
  1487. this.redraw();
  1488. }
  1489. else {
  1490. this.get_container_ul().children('.jstree-initial-node').remove();
  1491. this.redraw(true);
  1492. }
  1493. if(this._data.core.selected.length !== s) {
  1494. this.trigger('changed', { 'action' : 'model', 'selected' : this._data.core.selected });
  1495. }
  1496. cb.call(this, true);
  1497. },
  1498. /**
  1499. * appends JSON content to the tree. Used internally.
  1500. * @private
  1501. * @name _append_json_data(obj, data)
  1502. * @param {mixed} obj the node to append to
  1503. * @param {String} data the JSON object to parse and append
  1504. * @param {Boolean} force_processing internal param - do not set
  1505. * @trigger model.jstree, changed.jstree
  1506. */
  1507. _append_json_data : function (dom, data, cb, force_processing) {
  1508. if(this.element === null) { return; }
  1509. dom = this.get_node(dom);
  1510. dom.children = [];
  1511. dom.children_d = [];
  1512. // *%$@!!!
  1513. if(data.d) {
  1514. data = data.d;
  1515. if(typeof data === "string") {
  1516. data = JSON.parse(data);
  1517. }
  1518. }
  1519. if(!$.isArray(data)) { data = [data]; }
  1520. var w = null,
  1521. args = {
  1522. 'df' : this._model.default_state,
  1523. 'dat' : data,
  1524. 'par' : dom.id,
  1525. 'm' : this._model.data,
  1526. 't_id' : this._id,
  1527. 't_cnt' : this._cnt,
  1528. 'sel' : this._data.core.selected
  1529. },
  1530. func = function (data, undefined) {
  1531. if(data.data) { data = data.data; }
  1532. var dat = data.dat,
  1533. par = data.par,
  1534. chd = [],
  1535. dpc = [],
  1536. add = [],
  1537. df = data.df,
  1538. t_id = data.t_id,
  1539. t_cnt = data.t_cnt,
  1540. m = data.m,
  1541. p = m[par],
  1542. sel = data.sel,
  1543. tmp, i, j, rslt,
  1544. parse_flat = function (d, p, ps) {
  1545. if(!ps) { ps = []; }
  1546. else { ps = ps.concat(); }
  1547. if(p) { ps.unshift(p); }
  1548. var tid = d.id.toString(),
  1549. i, j, c, e,
  1550. tmp = {
  1551. id : tid,
  1552. text : d.text || '',
  1553. icon : d.icon !== undefined ? d.icon : true,
  1554. parent : p,
  1555. parents : ps,
  1556. children : d.children || [],
  1557. children_d : d.children_d || [],
  1558. data : d.data,
  1559. state : { },
  1560. li_attr : { id : false },
  1561. a_attr : { href : '#' },
  1562. original : false
  1563. };
  1564. for(i in df) {
  1565. if(df.hasOwnProperty(i)) {
  1566. tmp.state[i] = df[i];
  1567. }
  1568. }
  1569. if(d && d.data && d.data.jstree && d.data.jstree.icon) {
  1570. tmp.icon = d.data.jstree.icon;
  1571. }
  1572. if(tmp.icon === undefined || tmp.icon === null || tmp.icon === "") {
  1573. tmp.icon = true;
  1574. }
  1575. if(d && d.data) {
  1576. tmp.data = d.data;
  1577. if(d.data.jstree) {
  1578. for(i in d.data.jstree) {
  1579. if(d.data.jstree.hasOwnProperty(i)) {
  1580. tmp.state[i] = d.data.jstree[i];
  1581. }
  1582. }
  1583. }
  1584. }
  1585. if(d && typeof d.state === 'object') {
  1586. for (i in d.state) {
  1587. if(d.state.hasOwnProperty(i)) {
  1588. tmp.state[i] = d.state[i];
  1589. }
  1590. }
  1591. }
  1592. if(d && typeof d.li_attr === 'object') {
  1593. for (i in d.li_attr) {
  1594. if(d.li_attr.hasOwnProperty(i)) {
  1595. tmp.li_attr[i] = d.li_attr[i];
  1596. }
  1597. }
  1598. }
  1599. if(!tmp.li_attr.id) {
  1600. tmp.li_attr.id = tid;
  1601. }
  1602. if(d && typeof d.a_attr === 'object') {
  1603. for (i in d.a_attr) {
  1604. if(d.a_attr.hasOwnProperty(i)) {
  1605. tmp.a_attr[i] = d.a_attr[i];
  1606. }
  1607. }
  1608. }
  1609. if(d && d.children && d.children === true) {
  1610. tmp.state.loaded = false;
  1611. tmp.children = [];
  1612. tmp.children_d = [];
  1613. }
  1614. m[tmp.id] = tmp;
  1615. for(i = 0, j = tmp.children.length; i < j; i++) {
  1616. c = parse_flat(m[tmp.children[i]], tmp.id, ps);
  1617. e = m[c];
  1618. tmp.children_d.push(c);
  1619. if(e.children_d.length) {
  1620. tmp.children_d = tmp.children_d.concat(e.children_d);
  1621. }
  1622. }
  1623. delete d.data;
  1624. delete d.children;
  1625. m[tmp.id].original = d;
  1626. if(tmp.state.selected) {
  1627. add.push(tmp.id);
  1628. }
  1629. return tmp.id;
  1630. },
  1631. parse_nest = function (d, p, ps) {
  1632. if(!ps) { ps = []; }
  1633. else { ps = ps.concat(); }
  1634. if(p) { ps.unshift(p); }
  1635. var tid = false, i, j, c, e, tmp;
  1636. do {
  1637. tid = 'j' + t_id + '_' + (++t_cnt);
  1638. } while(m[tid]);
  1639. tmp = {
  1640. id : false,
  1641. text : typeof d === 'string' ? d : '',
  1642. icon : typeof d === 'object' && d.icon !== undefined ? d.icon : true,
  1643. parent : p,
  1644. parents : ps,
  1645. children : [],
  1646. children_d : [],
  1647. data : null,
  1648. state : { },
  1649. li_attr : { id : false },
  1650. a_attr : { href : '#' },
  1651. original : false
  1652. };
  1653. for(i in df) {
  1654. if(df.hasOwnProperty(i)) {
  1655. tmp.state[i] = df[i];
  1656. }
  1657. }
  1658. if(d && d.id) { tmp.id = d.id.toString(); }
  1659. if(d && d.text) { tmp.text = d.text; }
  1660. if(d && d.data && d.data.jstree && d.data.jstree.icon) {
  1661. tmp.icon = d.data.jstree.icon;
  1662. }
  1663. if(tmp.icon === undefined || tmp.icon === null || tmp.icon === "") {
  1664. tmp.icon = true;
  1665. }
  1666. if(d && d.data) {
  1667. tmp.data = d.data;
  1668. if(d.data.jstree) {
  1669. for(i in d.data.jstree) {
  1670. if(d.data.jstree.hasOwnProperty(i)) {
  1671. tmp.state[i] = d.data.jstree[i];
  1672. }
  1673. }
  1674. }
  1675. }
  1676. if(d && typeof d.state === 'object') {
  1677. for (i in d.state) {
  1678. if(d.state.hasOwnProperty(i)) {
  1679. tmp.state[i] = d.state[i];
  1680. }
  1681. }
  1682. }
  1683. if(d && typeof d.li_attr === 'object') {
  1684. for (i in d.li_attr) {
  1685. if(d.li_attr.hasOwnProperty(i)) {
  1686. tmp.li_attr[i] = d.li_attr[i];
  1687. }
  1688. }
  1689. }
  1690. if(tmp.li_attr.id && !tmp.id) {
  1691. tmp.id = tmp.li_attr.id.toString();
  1692. }
  1693. if(!tmp.id) {
  1694. tmp.id = tid;
  1695. }
  1696. if(!tmp.li_attr.id) {
  1697. tmp.li_attr.id = tmp.id;
  1698. }
  1699. if(d && typeof d.a_attr === 'object') {
  1700. for (i in d.a_attr) {
  1701. if(d.a_attr.hasOwnProperty(i)) {
  1702. tmp.a_attr[i] = d.a_attr[i];
  1703. }
  1704. }
  1705. }
  1706. if(d && d.children && d.children.length) {
  1707. for(i = 0, j = d.children.length; i < j; i++) {
  1708. c = parse_nest(d.children[i], tmp.id, ps);
  1709. e = m[c];
  1710. tmp.children.push(c);
  1711. if(e.children_d.length) {
  1712. tmp.children_d = tmp.children_d.concat(e.children_d);
  1713. }
  1714. }
  1715. tmp.children_d = tmp.children_d.concat(tmp.children);
  1716. }
  1717. if(d && d.children && d.children === true) {
  1718. tmp.state.loaded = false;
  1719. tmp.children = [];
  1720. tmp.children_d = [];
  1721. }
  1722. delete d.data;
  1723. delete d.children;
  1724. tmp.original = d;
  1725. m[tmp.id] = tmp;
  1726. if(tmp.state.selected) {
  1727. add.push(tmp.id);
  1728. }
  1729. return tmp.id;
  1730. };
  1731. if(dat.length && dat[0].id !== undefined && dat[0].parent !== undefined) {
  1732. // Flat JSON support (for easy import from DB):
  1733. // 1) convert to object (foreach)
  1734. for(i = 0, j = dat.length; i < j; i++) {
  1735. if(!dat[i].children) {
  1736. dat[i].children = [];
  1737. }
  1738. m[dat[i].id.toString()] = dat[i];
  1739. }
  1740. // 2) populate children (foreach)
  1741. for(i = 0, j = dat.length; i < j; i++) {
  1742. m[dat[i].parent.toString()].children.push(dat[i].id.toString());
  1743. // populate parent.children_d
  1744. p.children_d.push(dat[i].id.toString());
  1745. }
  1746. // 3) normalize && populate parents and children_d with recursion
  1747. for(i = 0, j = p.children.length; i < j; i++) {
  1748. tmp = parse_flat(m[p.children[i]], par, p.parents.concat());
  1749. dpc.push(tmp);
  1750. if(m[tmp].children_d.length) {
  1751. dpc = dpc.concat(m[tmp].children_d);
  1752. }
  1753. }
  1754. for(i = 0, j = p.parents.length; i < j; i++) {
  1755. m[p.parents[i]].children_d = m[p.parents[i]].children_d.concat(dpc);
  1756. }
  1757. // ?) three_state selection - p.state.selected && t - (if three_state foreach(dat => ch) -> foreach(parents) if(parent.selected) child.selected = true;
  1758. rslt = {
  1759. 'cnt' : t_cnt,
  1760. 'mod' : m,
  1761. 'sel' : sel,
  1762. 'par' : par,
  1763. 'dpc' : dpc,
  1764. 'add' : add
  1765. };
  1766. }
  1767. else {
  1768. for(i = 0, j = dat.length; i < j; i++) {
  1769. tmp = parse_nest(dat[i], par, p.parents.concat());
  1770. if(tmp) {
  1771. chd.push(tmp);
  1772. dpc.push(tmp);
  1773. if(m[tmp].children_d.length) {
  1774. dpc = dpc.concat(m[tmp].children_d);
  1775. }
  1776. }
  1777. }
  1778. p.children = chd;
  1779. p.children_d = dpc;
  1780. for(i = 0, j = p.parents.length; i < j; i++) {
  1781. m[p.parents[i]].children_d = m[p.parents[i]].children_d.concat(dpc);
  1782. }
  1783. rslt = {
  1784. 'cnt' : t_cnt,
  1785. 'mod' : m,
  1786. 'sel' : sel,
  1787. 'par' : par,
  1788. 'dpc' : dpc,
  1789. 'add' : add
  1790. };
  1791. }
  1792. if(typeof window === 'undefined' || typeof window.document === 'undefined') {
  1793. postMessage(rslt);
  1794. }
  1795. else {
  1796. return rslt;
  1797. }
  1798. },
  1799. rslt = function (rslt, worker) {
  1800. if(this.element === null) { return; }
  1801. this._cnt = rslt.cnt;
  1802. var i, m = this._model.data;
  1803. for (i in m) {
  1804. if (m.hasOwnProperty(i) && m[i].state && m[i].state.loading && rslt.mod[i]) {
  1805. rslt.mod[i].state.loading = true;
  1806. }
  1807. }
  1808. this._model.data = rslt.mod; // breaks the reference in load_node - careful
  1809. if(worker) {
  1810. var j, a = rslt.add, r = rslt.sel, s = this._data.core.selected.slice();
  1811. m = this._model.data;
  1812. // if selection was changed while calculating in worker
  1813. if(r.length !== s.length || $.vakata.array_unique(r.concat(s)).length !== r.length) {
  1814. // deselect nodes that are no longer selected
  1815. for(i = 0, j = r.length; i < j; i++) {
  1816. if($.inArray(r[i], a) === -1 && $.inArray(r[i], s) === -1) {
  1817. m[r[i]].state.selected = false;
  1818. }
  1819. }
  1820. // select nodes that were selected in the mean time
  1821. for(i = 0, j = s.length; i < j; i++) {
  1822. if($.inArray(s[i], r) === -1) {
  1823. m[s[i]].state.selected = true;
  1824. }
  1825. }
  1826. }
  1827. }
  1828. if(rslt.add.length) {
  1829. this._data.core.selected = this._data.core.selected.concat(rslt.add);
  1830. }
  1831. this.trigger('model', { "nodes" : rslt.dpc, 'parent' : rslt.par });
  1832. if(rslt.par !== $.jstree.root) {
  1833. this._node_changed(rslt.par);
  1834. this.redraw();
  1835. }
  1836. else {
  1837. // this.get_container_ul().children('.jstree-initial-node').remove();
  1838. this.redraw(true);
  1839. }
  1840. if(rslt.add.length) {
  1841. this.trigger('changed', { 'action' : 'model', 'selected' : this._data.core.selected });
  1842. }
  1843. cb.call(this, true);
  1844. };
  1845. if(this.settings.core.worker && window.Blob && window.URL && window.Worker) {
  1846. try {
  1847. if(this._wrk === null) {
  1848. this._wrk = window.URL.createObjectURL(
  1849. new window.Blob(
  1850. ['self.onmessage = ' + func.toString()],
  1851. {type:"text/javascript"}
  1852. )
  1853. );
  1854. }
  1855. if(!this._data.core.working || force_processing) {
  1856. this._data.core.working = true;
  1857. w = new window.Worker(this._wrk);
  1858. w.onmessage = $.proxy(function (e) {
  1859. rslt.call(this, e.data, true);
  1860. try { w.terminate(); w = null; } catch(ignore) { }
  1861. if(this._data.core.worker_queue.length) {
  1862. this._append_json_data.apply(this, this._data.core.worker_queue.shift());
  1863. }
  1864. else {
  1865. this._data.core.working = false;
  1866. }
  1867. }, this);
  1868. if(!args.par) {
  1869. if(this._data.core.worker_queue.length) {
  1870. this._append_json_data.apply(this, this._data.core.worker_queue.shift());
  1871. }
  1872. else {
  1873. this._data.core.working = false;
  1874. }
  1875. }
  1876. else {
  1877. w.postMessage(args);
  1878. }
  1879. }
  1880. else {
  1881. this._data.core.worker_queue.push([dom, data, cb, true]);
  1882. }
  1883. }
  1884. catch(e) {
  1885. rslt.call(this, func(args), false);
  1886. if(this._data.core.worker_queue.length) {
  1887. this._append_json_data.apply(this, this._data.core.worker_queue.shift());
  1888. }
  1889. else {
  1890. this._data.core.working = false;
  1891. }
  1892. }
  1893. }
  1894. else {
  1895. rslt.call(this, func(args), false);
  1896. }
  1897. },
  1898. /**
  1899. * parses a node from a jQuery object and appends them to the in memory tree model. Used internally.
  1900. * @private
  1901. * @name _parse_model_from_html(d [, p, ps])
  1902. * @param {jQuery} d the jQuery object to parse
  1903. * @param {String} p the parent ID
  1904. * @param {Array} ps list of all parents
  1905. * @return {String} the ID of the object added to the model
  1906. */
  1907. _parse_model_from_html : function (d, p, ps) {
  1908. if(!ps) { ps = []; }
  1909. else { ps = [].concat(ps); }
  1910. if(p) { ps.unshift(p); }
  1911. var c, e, m = this._model.data,
  1912. data = {
  1913. id : false,
  1914. text : false,
  1915. icon : true,
  1916. parent : p,
  1917. parents : ps,
  1918. children : [],
  1919. children_d : [],
  1920. data : null,
  1921. state : { },
  1922. li_attr : { id : false },
  1923. a_attr : { href : '#' },
  1924. original : false
  1925. }, i, tmp, tid;
  1926. for(i in this._model.default_state) {
  1927. if(this._model.default_state.hasOwnProperty(i)) {
  1928. data.state[i] = this._model.default_state[i];
  1929. }
  1930. }
  1931. tmp = $.vakata.attributes(d, true);
  1932. $.each(tmp, function (i, v) {
  1933. v = $.trim(v);
  1934. if(!v.length) { return true; }
  1935. data.li_attr[i] = v;
  1936. if(i === 'id') {
  1937. data.id = v.toString();
  1938. }
  1939. });
  1940. tmp = d.children('a').first();
  1941. if(tmp.length) {
  1942. tmp = $.vakata.attributes(tmp, true);
  1943. $.each(tmp, function (i, v) {
  1944. v = $.trim(v);
  1945. if(v.length) {
  1946. data.a_attr[i] = v;
  1947. }
  1948. });
  1949. }
  1950. tmp = d.children("a").first().length ? d.children("a").first().clone() : d.clone();
  1951. tmp.children("ins, i, ul").remove();
  1952. tmp = tmp.html();
  1953. tmp = $('<div />').html(tmp);
  1954. data.text = this.settings.core.force_text ? tmp.text() : tmp.html();
  1955. tmp = d.data();
  1956. data.data = tmp ? $.extend(true, {}, tmp) : null;
  1957. data.state.opened = d.hasClass('jstree-open');
  1958. data.state.selected = d.children('a').hasClass('jstree-clicked');
  1959. data.state.disabled = d.children('a').hasClass('jstree-disabled');
  1960. if(data.data && data.data.jstree) {
  1961. for(i in data.data.jstree) {
  1962. if(data.data.jstree.hasOwnProperty(i)) {
  1963. data.state[i] = data.data.jstree[i];
  1964. }
  1965. }
  1966. }
  1967. tmp = d.children("a").children(".jstree-themeicon");
  1968. if(tmp.length) {
  1969. data.icon = tmp.hasClass('jstree-themeicon-hidden') ? false : tmp.attr('rel');
  1970. }
  1971. if(data.state.icon !== undefined) {
  1972. data.icon = data.state.icon;
  1973. }
  1974. if(data.icon === undefined || data.icon === null || data.icon === "") {
  1975. data.icon = true;
  1976. }
  1977. tmp = d.children("ul").children("li");
  1978. do {
  1979. tid = 'j' + this._id + '_' + (++this._cnt);
  1980. } while(m[tid]);
  1981. data.id = data.li_attr.id ? data.li_attr.id.toString() : tid;
  1982. if(tmp.length) {
  1983. tmp.each($.proxy(function (i, v) {
  1984. c = this._parse_model_from_html($(v), data.id, ps);
  1985. e = this._model.data[c];
  1986. data.children.push(c);
  1987. if(e.children_d.length) {
  1988. data.children_d = data.children_d.concat(e.children_d);
  1989. }
  1990. }, this));
  1991. data.children_d = data.children_d.concat(data.children);
  1992. }
  1993. else {
  1994. if(d.hasClass('jstree-closed')) {
  1995. data.state.loaded = false;
  1996. }
  1997. }
  1998. if(data.li_attr['class']) {
  1999. data.li_attr['class'] = data.li_attr['class'].replace('jstree-closed','').replace('jstree-open','');
  2000. }
  2001. if(data.a_attr['class']) {
  2002. data.a_attr['class'] = data.a_attr['class'].replace('jstree-clicked','').replace('jstree-disabled','');
  2003. }
  2004. m[data.id] = data;
  2005. if(data.state.selected) {
  2006. this._data.core.selected.push(data.id);
  2007. }
  2008. return data.id;
  2009. },
  2010. /**
  2011. * parses a node from a JSON object (used when dealing with flat data, which has no nesting of children, but has id and parent properties) and appends it to the in memory tree model. Used internally.
  2012. * @private
  2013. * @name _parse_model_from_flat_json(d [, p, ps])
  2014. * @param {Object} d the JSON object to parse
  2015. * @param {String} p the parent ID
  2016. * @param {Array} ps list of all parents
  2017. * @return {String} the ID of the object added to the model
  2018. */
  2019. _parse_model_from_flat_json : function (d, p, ps) {
  2020. if(!ps) { ps = []; }
  2021. else { ps = ps.concat(); }
  2022. if(p) { ps.unshift(p); }
  2023. var tid = d.id.toString(),
  2024. m = this._model.data,
  2025. df = this._model.default_state,
  2026. i, j, c, e,
  2027. tmp = {
  2028. id : tid,
  2029. text : d.text || '',
  2030. icon : d.icon !== undefined ? d.icon : true,
  2031. parent : p,
  2032. parents : ps,
  2033. children : d.children || [],
  2034. children_d : d.children_d || [],
  2035. data : d.data,
  2036. state : { },
  2037. li_attr : { id : false },
  2038. a_attr : { href : '#' },
  2039. original : false
  2040. };
  2041. for(i in df) {
  2042. if(df.hasOwnProperty(i)) {
  2043. tmp.state[i] = df[i];
  2044. }
  2045. }
  2046. if(d && d.data && d.data.jstree && d.data.jstree.icon) {
  2047. tmp.icon = d.data.jstree.icon;
  2048. }
  2049. if(tmp.icon === undefined || tmp.icon === null || tmp.icon === "") {
  2050. tmp.icon = true;
  2051. }
  2052. if(d && d.data) {
  2053. tmp.data = d.data;
  2054. if(d.data.jstree) {
  2055. for(i in d.data.jstree) {
  2056. if(d.data.jstree.hasOwnProperty(i)) {
  2057. tmp.state[i] = d.data.jstree[i];
  2058. }
  2059. }
  2060. }
  2061. }
  2062. if(d && typeof d.state === 'object') {
  2063. for (i in d.state) {
  2064. if(d.state.hasOwnProperty(i)) {
  2065. tmp.state[i] = d.state[i];
  2066. }
  2067. }
  2068. }
  2069. if(d && typeof d.li_attr === 'object') {
  2070. for (i in d.li_attr) {
  2071. if(d.li_attr.hasOwnProperty(i)) {
  2072. tmp.li_attr[i] = d.li_attr[i];
  2073. }
  2074. }
  2075. }
  2076. if(!tmp.li_attr.id) {
  2077. tmp.li_attr.id = tid;
  2078. }
  2079. if(d && typeof d.a_attr === 'object') {
  2080. for (i in d.a_attr) {
  2081. if(d.a_attr.hasOwnProperty(i)) {
  2082. tmp.a_attr[i] = d.a_attr[i];
  2083. }
  2084. }
  2085. }
  2086. if(d && d.children && d.children === true) {
  2087. tmp.state.loaded = false;
  2088. tmp.children = [];
  2089. tmp.children_d = [];
  2090. }
  2091. m[tmp.id] = tmp;
  2092. for(i = 0, j = tmp.children.length; i < j; i++) {
  2093. c = this._parse_model_from_flat_json(m[tmp.children[i]], tmp.id, ps);
  2094. e = m[c];
  2095. tmp.children_d.push(c);
  2096. if(e.children_d.length) {
  2097. tmp.children_d = tmp.children_d.concat(e.children_d);
  2098. }
  2099. }
  2100. delete d.data;
  2101. delete d.children;
  2102. m[tmp.id].original = d;
  2103. if(tmp.state.selected) {
  2104. this._data.core.selected.push(tmp.id);
  2105. }
  2106. return tmp.id;
  2107. },
  2108. /**
  2109. * parses a node from a JSON object and appends it to the in memory tree model. Used internally.
  2110. * @private
  2111. * @name _parse_model_from_json(d [, p, ps])
  2112. * @param {Object} d the JSON object to parse
  2113. * @param {String} p the parent ID
  2114. * @param {Array} ps list of all parents
  2115. * @return {String} the ID of the object added to the model
  2116. */
  2117. _parse_model_from_json : function (d, p, ps) {
  2118. if(!ps) { ps = []; }
  2119. else { ps = ps.concat(); }
  2120. if(p) { ps.unshift(p); }
  2121. var tid = false, i, j, c, e, m = this._model.data, df = this._model.default_state, tmp;
  2122. do {
  2123. tid = 'j' + this._id + '_' + (++this._cnt);
  2124. } while(m[tid]);
  2125. tmp = {
  2126. id : false,
  2127. text : typeof d === 'string' ? d : '',
  2128. icon : typeof d === 'object' && d.icon !== undefined ? d.icon : true,
  2129. parent : p,
  2130. parents : ps,
  2131. children : [],
  2132. children_d : [],
  2133. data : null,
  2134. state : { },
  2135. li_attr : { id : false },
  2136. a_attr : { href : '#' },
  2137. original : false
  2138. };
  2139. for(i in df) {
  2140. if(df.hasOwnProperty(i)) {
  2141. tmp.state[i] = df[i];
  2142. }
  2143. }
  2144. if(d && d.id) { tmp.id = d.id.toString(); }
  2145. if(d && d.text) { tmp.text = d.text; }
  2146. if(d && d.data && d.data.jstree && d.data.jstree.icon) {
  2147. tmp.icon = d.data.jstree.icon;
  2148. }
  2149. if(tmp.icon === undefined || tmp.icon === null || tmp.icon === "") {
  2150. tmp.icon = true;
  2151. }
  2152. if(d && d.data) {
  2153. tmp.data = d.data;
  2154. if(d.data.jstree) {
  2155. for(i in d.data.jstree) {
  2156. if(d.data.jstree.hasOwnProperty(i)) {
  2157. tmp.state[i] = d.data.jstree[i];
  2158. }
  2159. }
  2160. }
  2161. }
  2162. if(d && typeof d.state === 'object') {
  2163. for (i in d.state) {
  2164. if(d.state.hasOwnProperty(i)) {
  2165. tmp.state[i] = d.state[i];
  2166. }
  2167. }
  2168. }
  2169. if(d && typeof d.li_attr === 'object') {
  2170. for (i in d.li_attr) {
  2171. if(d.li_attr.hasOwnProperty(i)) {
  2172. tmp.li_attr[i] = d.li_attr[i];
  2173. }
  2174. }
  2175. }
  2176. if(tmp.li_attr.id && !tmp.id) {
  2177. tmp.id = tmp.li_attr.id.toString();
  2178. }
  2179. if(!tmp.id) {
  2180. tmp.id = tid;
  2181. }
  2182. if(!tmp.li_attr.id) {
  2183. tmp.li_attr.id = tmp.id;
  2184. }
  2185. if(d && typeof d.a_attr === 'object') {
  2186. for (i in d.a_attr) {
  2187. if(d.a_attr.hasOwnProperty(i)) {
  2188. tmp.a_attr[i] = d.a_attr[i];
  2189. }
  2190. }
  2191. }
  2192. if(d && d.children && d.children.length) {
  2193. for(i = 0, j = d.children.length; i < j; i++) {
  2194. c = this._parse_model_from_json(d.children[i], tmp.id, ps);
  2195. e = m[c];
  2196. tmp.children.push(c);
  2197. if(e.children_d.length) {
  2198. tmp.children_d = tmp.children_d.concat(e.children_d);
  2199. }
  2200. }
  2201. tmp.children_d = tmp.children_d.concat(tmp.children);
  2202. }
  2203. if(d && d.children && d.children === true) {
  2204. tmp.state.loaded = false;
  2205. tmp.children = [];
  2206. tmp.children_d = [];
  2207. }
  2208. delete d.data;
  2209. delete d.children;
  2210. tmp.original = d;
  2211. m[tmp.id] = tmp;
  2212. if(tmp.state.selected) {
  2213. this._data.core.selected.push(tmp.id);
  2214. }
  2215. return tmp.id;
  2216. },
  2217. /**
  2218. * redraws all nodes that need to be redrawn. Used internally.
  2219. * @private
  2220. * @name _redraw()
  2221. * @trigger redraw.jstree
  2222. */
  2223. _redraw : function () {
  2224. var nodes = this._model.force_full_redraw ? this._model.data[$.jstree.root].children.concat([]) : this._model.changed.concat([]),
  2225. f = document.createElement('UL'), tmp, i, j, fe = this._data.core.focused;
  2226. for(i = 0, j = nodes.length; i < j; i++) {
  2227. tmp = this.redraw_node(nodes[i], true, this._model.force_full_redraw);
  2228. if(tmp && this._model.force_full_redraw) {
  2229. f.appendChild(tmp);
  2230. }
  2231. }
  2232. if(this._model.force_full_redraw) {
  2233. f.className = this.get_container_ul()[0].className;
  2234. f.setAttribute('role','group');
  2235. this.element.empty().append(f);
  2236. //this.get_container_ul()[0].appendChild(f);
  2237. }
  2238. if(fe !== null) {
  2239. tmp = this.get_node(fe, true);
  2240. if(tmp && tmp.length && tmp.children('.jstree-anchor')[0] !== document.activeElement) {
  2241. tmp.children('.jstree-anchor').focus();
  2242. }
  2243. else {
  2244. this._data.core.focused = null;
  2245. }
  2246. }
  2247. this._model.force_full_redraw = false;
  2248. this._model.changed = [];
  2249. /**
  2250. * triggered after nodes are redrawn
  2251. * @event
  2252. * @name redraw.jstree
  2253. * @param {array} nodes the redrawn nodes
  2254. */
  2255. this.trigger('redraw', { "nodes" : nodes });
  2256. },
  2257. /**
  2258. * redraws all nodes that need to be redrawn or optionally - the whole tree
  2259. * @name redraw([full])
  2260. * @param {Boolean} full if set to `true` all nodes are redrawn.
  2261. */
  2262. redraw : function (full) {
  2263. if(full) {
  2264. this._model.force_full_redraw = true;
  2265. }
  2266. //if(this._model.redraw_timeout) {
  2267. // clearTimeout(this._model.redraw_timeout);
  2268. //}
  2269. //this._model.redraw_timeout = setTimeout($.proxy(this._redraw, this),0);
  2270. this._redraw();
  2271. },
  2272. /**
  2273. * redraws a single node's children. Used internally.
  2274. * @private
  2275. * @name draw_children(node)
  2276. * @param {mixed} node the node whose children will be redrawn
  2277. */
  2278. draw_children : function (node) {
  2279. var obj = this.get_node(node),
  2280. i = false,
  2281. j = false,
  2282. k = false,
  2283. d = document;
  2284. if(!obj) { return false; }
  2285. if(obj.id === $.jstree.root) { return this.redraw(true); }
  2286. node = this.get_node(node, true);
  2287. if(!node || !node.length) { return false; } // TODO: quick toggle
  2288. node.children('.jstree-children').remove();
  2289. node = node[0];
  2290. if(obj.children.length && obj.state.loaded) {
  2291. k = d.createElement('UL');
  2292. k.setAttribute('role', 'group');
  2293. k.className = 'jstree-children';
  2294. for(i = 0, j = obj.children.length; i < j; i++) {
  2295. k.appendChild(this.redraw_node(obj.children[i], true, true));
  2296. }
  2297. node.appendChild(k);
  2298. }
  2299. },
  2300. /**
  2301. * redraws a single node. Used internally.
  2302. * @private
  2303. * @name redraw_node(node, deep, is_callback, force_render)
  2304. * @param {mixed} node the node to redraw
  2305. * @param {Boolean} deep should child nodes be redrawn too
  2306. * @param {Boolean} is_callback is this a recursion call
  2307. * @param {Boolean} force_render should children of closed parents be drawn anyway
  2308. */
  2309. redraw_node : function (node, deep, is_callback, force_render) {
  2310. var obj = this.get_node(node),
  2311. par = false,
  2312. ind = false,
  2313. old = false,
  2314. i = false,
  2315. j = false,
  2316. k = false,
  2317. c = '',
  2318. d = document,
  2319. m = this._model.data,
  2320. f = false,
  2321. s = false,
  2322. tmp = null,
  2323. t = 0,
  2324. l = 0,
  2325. has_children = false,
  2326. last_sibling = false;
  2327. if(!obj) { return false; }
  2328. if(obj.id === $.jstree.root) { return this.redraw(true); }
  2329. deep = deep || obj.children.length === 0;
  2330. node = !document.querySelector ? document.getElementById(obj.id) : this.element[0].querySelector('#' + ("0123456789".indexOf(obj.id[0]) !== -1 ? '\\3' + obj.id[0] + ' ' + obj.id.substr(1).replace($.jstree.idregex,'\\$&') : obj.id.replace($.jstree.idregex,'\\$&')) ); //, this.element);
  2331. if(!node) {
  2332. deep = true;
  2333. //node = d.createElement('LI');
  2334. if(!is_callback) {
  2335. par = obj.parent !== $.jstree.root ? $('#' + obj.parent.replace($.jstree.idregex,'\\$&'), this.element)[0] : null;
  2336. if(par !== null && (!par || !m[obj.parent].state.opened)) {
  2337. return false;
  2338. }
  2339. ind = $.inArray(obj.id, par === null ? m[$.jstree.root].children : m[obj.parent].children);
  2340. }
  2341. }
  2342. else {
  2343. node = $(node);
  2344. if(!is_callback) {
  2345. par = node.parent().parent()[0];
  2346. if(par === this.element[0]) {
  2347. par = null;
  2348. }
  2349. ind = node.index();
  2350. }
  2351. // m[obj.id].data = node.data(); // use only node's data, no need to touch jquery storage
  2352. if(!deep && obj.children.length && !node.children('.jstree-children').length) {
  2353. deep = true;
  2354. }
  2355. if(!deep) {
  2356. old = node.children('.jstree-children')[0];
  2357. }
  2358. f = node.children('.jstree-anchor')[0] === document.activeElement;
  2359. node.remove();
  2360. //node = d.createElement('LI');
  2361. //node = node[0];
  2362. }
  2363. node = this._data.core.node.cloneNode(true);
  2364. // node is DOM, deep is boolean
  2365. c = 'jstree-node ';
  2366. for(i in obj.li_attr) {
  2367. if(obj.li_attr.hasOwnProperty(i)) {
  2368. if(i === 'id') { continue; }
  2369. if(i !== 'class') {
  2370. node.setAttribute(i, obj.li_attr[i]);
  2371. }
  2372. else {
  2373. c += obj.li_attr[i];
  2374. }
  2375. }
  2376. }
  2377. if(!obj.a_attr.id) {
  2378. obj.a_attr.id = obj.id + '_anchor';
  2379. }
  2380. node.setAttribute('aria-selected', !!obj.state.selected);
  2381. node.setAttribute('aria-level', obj.parents.length);
  2382. node.setAttribute('aria-labelledby', obj.a_attr.id);
  2383. if(obj.state.disabled) {
  2384. node.setAttribute('aria-disabled', true);
  2385. }
  2386. for(i = 0, j = obj.children.length; i < j; i++) {
  2387. if(!m[obj.children[i]].state.hidden) {
  2388. has_children = true;
  2389. break;
  2390. }
  2391. }
  2392. if(obj.parent !== null && m[obj.parent] && !obj.state.hidden) {
  2393. i = $.inArray(obj.id, m[obj.parent].children);
  2394. last_sibling = obj.id;
  2395. if(i !== -1) {
  2396. i++;
  2397. for(j = m[obj.parent].children.length; i < j; i++) {
  2398. if(!m[m[obj.parent].children[i]].state.hidden) {
  2399. last_sibling = m[obj.parent].children[i];
  2400. }
  2401. if(last_sibling !== obj.id) {
  2402. break;
  2403. }
  2404. }
  2405. }
  2406. }
  2407. if(obj.state.hidden) {
  2408. c += ' jstree-hidden';
  2409. }
  2410. if(obj.state.loaded && !has_children) {
  2411. c += ' jstree-leaf';
  2412. }
  2413. else {
  2414. c += obj.state.opened && obj.state.loaded ? ' jstree-open' : ' jstree-closed';
  2415. node.setAttribute('aria-expanded', (obj.state.opened && obj.state.loaded) );
  2416. }
  2417. if(last_sibling === obj.id) {
  2418. c += ' jstree-last';
  2419. }
  2420. node.id = obj.id;
  2421. node.className = c;
  2422. c = ( obj.state.selected ? ' jstree-clicked' : '') + ( obj.state.disabled ? ' jstree-disabled' : '');
  2423. for(j in obj.a_attr) {
  2424. if(obj.a_attr.hasOwnProperty(j)) {
  2425. if(j === 'href' && obj.a_attr[j] === '#') { continue; }
  2426. if(j !== 'class') {
  2427. node.childNodes[1].setAttribute(j, obj.a_attr[j]);
  2428. }
  2429. else {
  2430. c += ' ' + obj.a_attr[j];
  2431. }
  2432. }
  2433. }
  2434. if(c.length) {
  2435. node.childNodes[1].className = 'jstree-anchor ' + c;
  2436. }
  2437. if((obj.icon && obj.icon !== true) || obj.icon === false) {
  2438. if(obj.icon === false) {
  2439. node.childNodes[1].childNodes[0].className += ' jstree-themeicon-hidden';
  2440. }
  2441. else if(obj.icon.indexOf('/') === -1 && obj.icon.indexOf('.') === -1) {
  2442. node.childNodes[1].childNodes[0].className += ' ' + obj.icon + ' jstree-themeicon-custom';
  2443. }
  2444. else {
  2445. node.childNodes[1].childNodes[0].style.backgroundImage = 'url("'+obj.icon+'")';
  2446. node.childNodes[1].childNodes[0].style.backgroundPosition = 'center center';
  2447. node.childNodes[1].childNodes[0].style.backgroundSize = 'auto';
  2448. node.childNodes[1].childNodes[0].className += ' jstree-themeicon-custom';
  2449. }
  2450. }
  2451. if(this.settings.core.force_text) {
  2452. node.childNodes[1].appendChild(d.createTextNode(obj.text));
  2453. }
  2454. else {
  2455. node.childNodes[1].innerHTML += obj.text;
  2456. }
  2457. if(deep && obj.children.length && (obj.state.opened || force_render) && obj.state.loaded) {
  2458. k = d.createElement('UL');
  2459. k.setAttribute('role', 'group');
  2460. k.className = 'jstree-children';
  2461. for(i = 0, j = obj.children.length; i < j; i++) {
  2462. k.appendChild(this.redraw_node(obj.children[i], deep, true));
  2463. }
  2464. node.appendChild(k);
  2465. }
  2466. if(old) {
  2467. node.appendChild(old);
  2468. }
  2469. if(!is_callback) {
  2470. // append back using par / ind
  2471. if(!par) {
  2472. par = this.element[0];
  2473. }
  2474. for(i = 0, j = par.childNodes.length; i < j; i++) {
  2475. if(par.childNodes[i] && par.childNodes[i].className && par.childNodes[i].className.indexOf('jstree-children') !== -1) {
  2476. tmp = par.childNodes[i];
  2477. break;
  2478. }
  2479. }
  2480. if(!tmp) {
  2481. tmp = d.createElement('UL');
  2482. tmp.setAttribute('role', 'group');
  2483. tmp.className = 'jstree-children';
  2484. par.appendChild(tmp);
  2485. }
  2486. par = tmp;
  2487. if(ind < par.childNodes.length) {
  2488. par.insertBefore(node, par.childNodes[ind]);
  2489. }
  2490. else {
  2491. par.appendChild(node);
  2492. }
  2493. if(f) {
  2494. t = this.element[0].scrollTop;
  2495. l = this.element[0].scrollLeft;
  2496. node.childNodes[1].focus();
  2497. this.element[0].scrollTop = t;
  2498. this.element[0].scrollLeft = l;
  2499. }
  2500. }
  2501. if(obj.state.opened && !obj.state.loaded) {
  2502. obj.state.opened = false;
  2503. setTimeout($.proxy(function () {
  2504. this.open_node(obj.id, false, 0);
  2505. }, this), 0);
  2506. }
  2507. return node;
  2508. },
  2509. /**
  2510. * opens a node, revaling its children. If the node is not loaded it will be loaded and opened once ready.
  2511. * @name open_node(obj [, callback, animation])
  2512. * @param {mixed} obj the node to open
  2513. * @param {Function} callback a function to execute once the node is opened
  2514. * @param {Number} animation the animation duration in milliseconds when opening the node (overrides the `core.animation` setting). Use `false` for no animation.
  2515. * @trigger open_node.jstree, after_open.jstree, before_open.jstree
  2516. */
  2517. open_node : function (obj, callback, animation) {
  2518. var t1, t2, d, t;
  2519. if($.isArray(obj)) {
  2520. obj = obj.slice();
  2521. for(t1 = 0, t2 = obj.length; t1 < t2; t1++) {
  2522. this.open_node(obj[t1], callback, animation);
  2523. }
  2524. return true;
  2525. }
  2526. obj = this.get_node(obj);
  2527. if(!obj || obj.id === $.jstree.root) {
  2528. return false;
  2529. }
  2530. animation = animation === undefined ? this.settings.core.animation : animation;
  2531. if(!this.is_closed(obj)) {
  2532. if(callback) {
  2533. callback.call(this, obj, false);
  2534. }
  2535. return false;
  2536. }
  2537. if(!this.is_loaded(obj)) {
  2538. if(this.is_loading(obj)) {
  2539. return setTimeout($.proxy(function () {
  2540. this.open_node(obj, callback, animation);
  2541. }, this), 500);
  2542. }
  2543. this.load_node(obj, function (o, ok) {
  2544. return ok ? this.open_node(o, callback, animation) : (callback ? callback.call(this, o, false) : false);
  2545. });
  2546. }
  2547. else {
  2548. d = this.get_node(obj, true);
  2549. t = this;
  2550. if(d.length) {
  2551. if(animation && d.children(".jstree-children").length) {
  2552. d.children(".jstree-children").stop(true, true);
  2553. }
  2554. if(obj.children.length && !this._firstChild(d.children('.jstree-children')[0])) {
  2555. this.draw_children(obj);
  2556. //d = this.get_node(obj, true);
  2557. }
  2558. if(!animation) {
  2559. this.trigger('before_open', { "node" : obj });
  2560. d[0].className = d[0].className.replace('jstree-closed', 'jstree-open');
  2561. d[0].setAttribute("aria-expanded", true);
  2562. }
  2563. else {
  2564. this.trigger('before_open', { "node" : obj });
  2565. d
  2566. .children(".jstree-children").css("display","none").end()
  2567. .removeClass("jstree-closed").addClass("jstree-open").attr("aria-expanded", true)
  2568. .children(".jstree-children").stop(true, true)
  2569. .slideDown(animation, function () {
  2570. this.style.display = "";
  2571. if (t.element) {
  2572. t.trigger("after_open", { "node" : obj });
  2573. }
  2574. });
  2575. }
  2576. }
  2577. obj.state.opened = true;
  2578. if(callback) {
  2579. callback.call(this, obj, true);
  2580. }
  2581. if(!d.length) {
  2582. /**
  2583. * triggered when a node is about to be opened (if the node is supposed to be in the DOM, it will be, but it won't be visible yet)
  2584. * @event
  2585. * @name before_open.jstree
  2586. * @param {Object} node the opened node
  2587. */
  2588. this.trigger('before_open', { "node" : obj });
  2589. }
  2590. /**
  2591. * triggered when a node is opened (if there is an animation it will not be completed yet)
  2592. * @event
  2593. * @name open_node.jstree
  2594. * @param {Object} node the opened node
  2595. */
  2596. this.trigger('open_node', { "node" : obj });
  2597. if(!animation || !d.length) {
  2598. /**
  2599. * triggered when a node is opened and the animation is complete
  2600. * @event
  2601. * @name after_open.jstree
  2602. * @param {Object} node the opened node
  2603. */
  2604. this.trigger("after_open", { "node" : obj });
  2605. }
  2606. return true;
  2607. }
  2608. },
  2609. /**
  2610. * opens every parent of a node (node should be loaded)
  2611. * @name _open_to(obj)
  2612. * @param {mixed} obj the node to reveal
  2613. * @private
  2614. */
  2615. _open_to : function (obj) {
  2616. obj = this.get_node(obj);
  2617. if(!obj || obj.id === $.jstree.root) {
  2618. return false;
  2619. }
  2620. var i, j, p = obj.parents;
  2621. for(i = 0, j = p.length; i < j; i+=1) {
  2622. if(i !== $.jstree.root) {
  2623. this.open_node(p[i], false, 0);
  2624. }
  2625. }
  2626. return $('#' + obj.id.replace($.jstree.idregex,'\\$&'), this.element);
  2627. },
  2628. /**
  2629. * closes a node, hiding its children
  2630. * @name close_node(obj [, animation])
  2631. * @param {mixed} obj the node to close
  2632. * @param {Number} animation the animation duration in milliseconds when closing the node (overrides the `core.animation` setting). Use `false` for no animation.
  2633. * @trigger close_node.jstree, after_close.jstree
  2634. */
  2635. close_node : function (obj, animation) {
  2636. var t1, t2, t, d;
  2637. if($.isArray(obj)) {
  2638. obj = obj.slice();
  2639. for(t1 = 0, t2 = obj.length; t1 < t2; t1++) {
  2640. this.close_node(obj[t1], animation);
  2641. }
  2642. return true;
  2643. }
  2644. obj = this.get_node(obj);
  2645. if(!obj || obj.id === $.jstree.root) {
  2646. return false;
  2647. }
  2648. if(this.is_closed(obj)) {
  2649. return false;
  2650. }
  2651. animation = animation === undefined ? this.settings.core.animation : animation;
  2652. t = this;
  2653. d = this.get_node(obj, true);
  2654. obj.state.opened = false;
  2655. /**
  2656. * triggered when a node is closed (if there is an animation it will not be complete yet)
  2657. * @event
  2658. * @name close_node.jstree
  2659. * @param {Object} node the closed node
  2660. */
  2661. this.trigger('close_node',{ "node" : obj });
  2662. if(!d.length) {
  2663. /**
  2664. * triggered when a node is closed and the animation is complete
  2665. * @event
  2666. * @name after_close.jstree
  2667. * @param {Object} node the closed node
  2668. */
  2669. this.trigger("after_close", { "node" : obj });
  2670. }
  2671. else {
  2672. if(!animation) {
  2673. d[0].className = d[0].className.replace('jstree-open', 'jstree-closed');
  2674. d.attr("aria-expanded", false).children('.jstree-children').remove();
  2675. this.trigger("after_close", { "node" : obj });
  2676. }
  2677. else {
  2678. d
  2679. .children(".jstree-children").attr("style","display:block !important").end()
  2680. .removeClass("jstree-open").addClass("jstree-closed").attr("aria-expanded", false)
  2681. .children(".jstree-children").stop(true, true).slideUp(animation, function () {
  2682. this.style.display = "";
  2683. d.children('.jstree-children').remove();
  2684. if (t.element) {
  2685. t.trigger("after_close", { "node" : obj });
  2686. }
  2687. });
  2688. }
  2689. }
  2690. },
  2691. /**
  2692. * toggles a node - closing it if it is open, opening it if it is closed
  2693. * @name toggle_node(obj)
  2694. * @param {mixed} obj the node to toggle
  2695. */
  2696. toggle_node : function (obj) {
  2697. var t1, t2;
  2698. if($.isArray(obj)) {
  2699. obj = obj.slice();
  2700. for(t1 = 0, t2 = obj.length; t1 < t2; t1++) {
  2701. this.toggle_node(obj[t1]);
  2702. }
  2703. return true;
  2704. }
  2705. if(this.is_closed(obj)) {
  2706. return this.open_node(obj);
  2707. }
  2708. if(this.is_open(obj)) {
  2709. return this.close_node(obj);
  2710. }
  2711. },
  2712. /**
  2713. * opens all nodes within a node (or the tree), revaling their children. If the node is not loaded it will be loaded and opened once ready.
  2714. * @name open_all([obj, animation, original_obj])
  2715. * @param {mixed} obj the node to open recursively, omit to open all nodes in the tree
  2716. * @param {Number} animation the animation duration in milliseconds when opening the nodes, the default is no animation
  2717. * @param {jQuery} reference to the node that started the process (internal use)
  2718. * @trigger open_all.jstree
  2719. */
  2720. open_all : function (obj, animation, original_obj) {
  2721. if(!obj) { obj = $.jstree.root; }
  2722. obj = this.get_node(obj);
  2723. if(!obj) { return false; }
  2724. var dom = obj.id === $.jstree.root ? this.get_container_ul() : this.get_node(obj, true), i, j, _this;
  2725. if(!dom.length) {
  2726. for(i = 0, j = obj.children_d.length; i < j; i++) {
  2727. if(this.is_closed(this._model.data[obj.children_d[i]])) {
  2728. this._model.data[obj.children_d[i]].state.opened = true;
  2729. }
  2730. }
  2731. return this.trigger('open_all', { "node" : obj });
  2732. }
  2733. original_obj = original_obj || dom;
  2734. _this = this;
  2735. dom = this.is_closed(obj) ? dom.find('.jstree-closed').addBack() : dom.find('.jstree-closed');
  2736. dom.each(function () {
  2737. _this.open_node(
  2738. this,
  2739. function(node, status) { if(status && this.is_parent(node)) { this.open_all(node, animation, original_obj); } },
  2740. animation || 0
  2741. );
  2742. });
  2743. if(original_obj.find('.jstree-closed').length === 0) {
  2744. /**
  2745. * triggered when an `open_all` call completes
  2746. * @event
  2747. * @name open_all.jstree
  2748. * @param {Object} node the opened node
  2749. */
  2750. this.trigger('open_all', { "node" : this.get_node(original_obj) });
  2751. }
  2752. },
  2753. /**
  2754. * closes all nodes within a node (or the tree), revaling their children
  2755. * @name close_all([obj, animation])
  2756. * @param {mixed} obj the node to close recursively, omit to close all nodes in the tree
  2757. * @param {Number} animation the animation duration in milliseconds when closing the nodes, the default is no animation
  2758. * @trigger close_all.jstree
  2759. */
  2760. close_all : function (obj, animation) {
  2761. if(!obj) { obj = $.jstree.root; }
  2762. obj = this.get_node(obj);
  2763. if(!obj) { return false; }
  2764. var dom = obj.id === $.jstree.root ? this.get_container_ul() : this.get_node(obj, true),
  2765. _this = this, i, j;
  2766. if(dom.length) {
  2767. dom = this.is_open(obj) ? dom.find('.jstree-open').addBack() : dom.find('.jstree-open');
  2768. $(dom.get().reverse()).each(function () { _this.close_node(this, animation || 0); });
  2769. }
  2770. for(i = 0, j = obj.children_d.length; i < j; i++) {
  2771. this._model.data[obj.children_d[i]].state.opened = false;
  2772. }
  2773. /**
  2774. * triggered when an `close_all` call completes
  2775. * @event
  2776. * @name close_all.jstree
  2777. * @param {Object} node the closed node
  2778. */
  2779. this.trigger('close_all', { "node" : obj });
  2780. },
  2781. /**
  2782. * checks if a node is disabled (not selectable)
  2783. * @name is_disabled(obj)
  2784. * @param {mixed} obj
  2785. * @return {Boolean}
  2786. */
  2787. is_disabled : function (obj) {
  2788. obj = this.get_node(obj);
  2789. return obj && obj.state && obj.state.disabled;
  2790. },
  2791. /**
  2792. * enables a node - so that it can be selected
  2793. * @name enable_node(obj)
  2794. * @param {mixed} obj the node to enable
  2795. * @trigger enable_node.jstree
  2796. */
  2797. enable_node : function (obj) {
  2798. var t1, t2;
  2799. if($.isArray(obj)) {
  2800. obj = obj.slice();
  2801. for(t1 = 0, t2 = obj.length; t1 < t2; t1++) {
  2802. this.enable_node(obj[t1]);
  2803. }
  2804. return true;
  2805. }
  2806. obj = this.get_node(obj);
  2807. if(!obj || obj.id === $.jstree.root) {
  2808. return false;
  2809. }
  2810. obj.state.disabled = false;
  2811. this.get_node(obj,true).children('.jstree-anchor').removeClass('jstree-disabled').attr('aria-disabled', false);
  2812. /**
  2813. * triggered when an node is enabled
  2814. * @event
  2815. * @name enable_node.jstree
  2816. * @param {Object} node the enabled node
  2817. */
  2818. this.trigger('enable_node', { 'node' : obj });
  2819. },
  2820. /**
  2821. * disables a node - so that it can not be selected
  2822. * @name disable_node(obj)
  2823. * @param {mixed} obj the node to disable
  2824. * @trigger disable_node.jstree
  2825. */
  2826. disable_node : function (obj) {
  2827. var t1, t2;
  2828. if($.isArray(obj)) {
  2829. obj = obj.slice();
  2830. for(t1 = 0, t2 = obj.length; t1 < t2; t1++) {
  2831. this.disable_node(obj[t1]);
  2832. }
  2833. return true;
  2834. }
  2835. obj = this.get_node(obj);
  2836. if(!obj || obj.id === $.jstree.root) {
  2837. return false;
  2838. }
  2839. obj.state.disabled = true;
  2840. this.get_node(obj,true).children('.jstree-anchor').addClass('jstree-disabled').attr('aria-disabled', true);
  2841. /**
  2842. * triggered when an node is disabled
  2843. * @event
  2844. * @name disable_node.jstree
  2845. * @param {Object} node the disabled node
  2846. */
  2847. this.trigger('disable_node', { 'node' : obj });
  2848. },
  2849. /**
  2850. * determines if a node is hidden
  2851. * @name is_hidden(obj)
  2852. * @param {mixed} obj the node
  2853. */
  2854. is_hidden : function (obj) {
  2855. obj = this.get_node(obj);
  2856. return obj.state.hidden === true;
  2857. },
  2858. /**
  2859. * hides a node - it is still in the structure but will not be visible
  2860. * @name hide_node(obj)
  2861. * @param {mixed} obj the node to hide
  2862. * @param {Boolean} skip_redraw internal parameter controlling if redraw is called
  2863. * @trigger hide_node.jstree
  2864. */
  2865. hide_node : function (obj, skip_redraw) {
  2866. var t1, t2;
  2867. if($.isArray(obj)) {
  2868. obj = obj.slice();
  2869. for(t1 = 0, t2 = obj.length; t1 < t2; t1++) {
  2870. this.hide_node(obj[t1], true);
  2871. }
  2872. if (!skip_redraw) {
  2873. this.redraw();
  2874. }
  2875. return true;
  2876. }
  2877. obj = this.get_node(obj);
  2878. if(!obj || obj.id === $.jstree.root) {
  2879. return false;
  2880. }
  2881. if(!obj.state.hidden) {
  2882. obj.state.hidden = true;
  2883. this._node_changed(obj.parent);
  2884. if(!skip_redraw) {
  2885. this.redraw();
  2886. }
  2887. /**
  2888. * triggered when an node is hidden
  2889. * @event
  2890. * @name hide_node.jstree
  2891. * @param {Object} node the hidden node
  2892. */
  2893. this.trigger('hide_node', { 'node' : obj });
  2894. }
  2895. },
  2896. /**
  2897. * shows a node
  2898. * @name show_node(obj)
  2899. * @param {mixed} obj the node to show
  2900. * @param {Boolean} skip_redraw internal parameter controlling if redraw is called
  2901. * @trigger show_node.jstree
  2902. */
  2903. show_node : function (obj, skip_redraw) {
  2904. var t1, t2;
  2905. if($.isArray(obj)) {
  2906. obj = obj.slice();
  2907. for(t1 = 0, t2 = obj.length; t1 < t2; t1++) {
  2908. this.show_node(obj[t1], true);
  2909. }
  2910. if (!skip_redraw) {
  2911. this.redraw();
  2912. }
  2913. return true;
  2914. }
  2915. obj = this.get_node(obj);
  2916. if(!obj || obj.id === $.jstree.root) {
  2917. return false;
  2918. }
  2919. if(obj.state.hidden) {
  2920. obj.state.hidden = false;
  2921. this._node_changed(obj.parent);
  2922. if(!skip_redraw) {
  2923. this.redraw();
  2924. }
  2925. /**
  2926. * triggered when an node is shown
  2927. * @event
  2928. * @name show_node.jstree
  2929. * @param {Object} node the shown node
  2930. */
  2931. this.trigger('show_node', { 'node' : obj });
  2932. }
  2933. },
  2934. /**
  2935. * hides all nodes
  2936. * @name hide_all()
  2937. * @trigger hide_all.jstree
  2938. */
  2939. hide_all : function (skip_redraw) {
  2940. var i, m = this._model.data, ids = [];
  2941. for(i in m) {
  2942. if(m.hasOwnProperty(i) && i !== $.jstree.root && !m[i].state.hidden) {
  2943. m[i].state.hidden = true;
  2944. ids.push(i);
  2945. }
  2946. }
  2947. this._model.force_full_redraw = true;
  2948. if(!skip_redraw) {
  2949. this.redraw();
  2950. }
  2951. /**
  2952. * triggered when all nodes are hidden
  2953. * @event
  2954. * @name hide_all.jstree
  2955. * @param {Array} nodes the IDs of all hidden nodes
  2956. */
  2957. this.trigger('hide_all', { 'nodes' : ids });
  2958. return ids;
  2959. },
  2960. /**
  2961. * shows all nodes
  2962. * @name show_all()
  2963. * @trigger show_all.jstree
  2964. */
  2965. show_all : function (skip_redraw) {
  2966. var i, m = this._model.data, ids = [];
  2967. for(i in m) {
  2968. if(m.hasOwnProperty(i) && i !== $.jstree.root && m[i].state.hidden) {
  2969. m[i].state.hidden = false;
  2970. ids.push(i);
  2971. }
  2972. }
  2973. this._model.force_full_redraw = true;
  2974. if(!skip_redraw) {
  2975. this.redraw();
  2976. }
  2977. /**
  2978. * triggered when all nodes are shown
  2979. * @event
  2980. * @name show_all.jstree
  2981. * @param {Array} nodes the IDs of all shown nodes
  2982. */
  2983. this.trigger('show_all', { 'nodes' : ids });
  2984. return ids;
  2985. },
  2986. /**
  2987. * called when a node is selected by the user. Used internally.
  2988. * @private
  2989. * @name activate_node(obj, e)
  2990. * @param {mixed} obj the node
  2991. * @param {Object} e the related event
  2992. * @trigger activate_node.jstree, changed.jstree
  2993. */
  2994. activate_node : function (obj, e) {
  2995. if(this.is_disabled(obj)) {
  2996. return false;
  2997. }
  2998. if(!e || typeof e !== 'object') {
  2999. e = {};
  3000. }
  3001. // ensure last_clicked is still in the DOM, make it fresh (maybe it was moved?) and make sure it is still selected, if not - make last_clicked the last selected node
  3002. this._data.core.last_clicked = this._data.core.last_clicked && this._data.core.last_clicked.id !== undefined ? this.get_node(this._data.core.last_clicked.id) : null;
  3003. if(this._data.core.last_clicked && !this._data.core.last_clicked.state.selected) { this._data.core.last_clicked = null; }
  3004. if(!this._data.core.last_clicked && this._data.core.selected.length) { this._data.core.last_clicked = this.get_node(this._data.core.selected[this._data.core.selected.length - 1]); }
  3005. if(!this.settings.core.multiple || (!e.metaKey && !e.ctrlKey && !e.shiftKey) || (e.shiftKey && (!this._data.core.last_clicked || !this.get_parent(obj) || this.get_parent(obj) !== this._data.core.last_clicked.parent ) )) {
  3006. if(!this.settings.core.multiple && (e.metaKey || e.ctrlKey || e.shiftKey) && this.is_selected(obj)) {
  3007. this.deselect_node(obj, false, e);
  3008. }
  3009. else {
  3010. this.deselect_all(true);
  3011. this.select_node(obj, false, false, e);
  3012. this._data.core.last_clicked = this.get_node(obj);
  3013. }
  3014. }
  3015. else {
  3016. if(e.shiftKey) {
  3017. var o = this.get_node(obj).id,
  3018. l = this._data.core.last_clicked.id,
  3019. p = this.get_node(this._data.core.last_clicked.parent).children,
  3020. c = false,
  3021. i, j;
  3022. for(i = 0, j = p.length; i < j; i += 1) {
  3023. // separate IFs work whem o and l are the same
  3024. if(p[i] === o) {
  3025. c = !c;
  3026. }
  3027. if(p[i] === l) {
  3028. c = !c;
  3029. }
  3030. if(!this.is_disabled(p[i]) && (c || p[i] === o || p[i] === l)) {
  3031. if (!this.is_hidden(p[i])) {
  3032. this.select_node(p[i], true, false, e);
  3033. }
  3034. }
  3035. else {
  3036. this.deselect_node(p[i], true, e);
  3037. }
  3038. }
  3039. this.trigger('changed', { 'action' : 'select_node', 'node' : this.get_node(obj), 'selected' : this._data.core.selected, 'event' : e });
  3040. }
  3041. else {
  3042. if(!this.is_selected(obj)) {
  3043. this.select_node(obj, false, false, e);
  3044. }
  3045. else {
  3046. this.deselect_node(obj, false, e);
  3047. }
  3048. }
  3049. }
  3050. /**
  3051. * triggered when an node is clicked or intercated with by the user
  3052. * @event
  3053. * @name activate_node.jstree
  3054. * @param {Object} node
  3055. * @param {Object} event the ooriginal event (if any) which triggered the call (may be an empty object)
  3056. */
  3057. this.trigger('activate_node', { 'node' : this.get_node(obj), 'event' : e });
  3058. },
  3059. /**
  3060. * applies the hover state on a node, called when a node is hovered by the user. Used internally.
  3061. * @private
  3062. * @name hover_node(obj)
  3063. * @param {mixed} obj
  3064. * @trigger hover_node.jstree
  3065. */
  3066. hover_node : function (obj) {
  3067. obj = this.get_node(obj, true);
  3068. if(!obj || !obj.length || obj.children('.jstree-hovered').length) {
  3069. return false;
  3070. }
  3071. var o = this.element.find('.jstree-hovered'), t = this.element;
  3072. if(o && o.length) { this.dehover_node(o); }
  3073. obj.children('.jstree-anchor').addClass('jstree-hovered');
  3074. /**
  3075. * triggered when an node is hovered
  3076. * @event
  3077. * @name hover_node.jstree
  3078. * @param {Object} node
  3079. */
  3080. this.trigger('hover_node', { 'node' : this.get_node(obj) });
  3081. setTimeout(function () { t.attr('aria-activedescendant', obj[0].id); }, 0);
  3082. },
  3083. /**
  3084. * removes the hover state from a nodecalled when a node is no longer hovered by the user. Used internally.
  3085. * @private
  3086. * @name dehover_node(obj)
  3087. * @param {mixed} obj
  3088. * @trigger dehover_node.jstree
  3089. */
  3090. dehover_node : function (obj) {
  3091. obj = this.get_node(obj, true);
  3092. if(!obj || !obj.length || !obj.children('.jstree-hovered').length) {
  3093. return false;
  3094. }
  3095. obj.children('.jstree-anchor').removeClass('jstree-hovered');
  3096. /**
  3097. * triggered when an node is no longer hovered
  3098. * @event
  3099. * @name dehover_node.jstree
  3100. * @param {Object} node
  3101. */
  3102. this.trigger('dehover_node', { 'node' : this.get_node(obj) });
  3103. },
  3104. /**
  3105. * select a node
  3106. * @name select_node(obj [, supress_event, prevent_open])
  3107. * @param {mixed} obj an array can be used to select multiple nodes
  3108. * @param {Boolean} supress_event if set to `true` the `changed.jstree` event won't be triggered
  3109. * @param {Boolean} prevent_open if set to `true` parents of the selected node won't be opened
  3110. * @trigger select_node.jstree, changed.jstree
  3111. */
  3112. select_node : function (obj, supress_event, prevent_open, e) {
  3113. var dom, t1, t2, th;
  3114. if($.isArray(obj)) {
  3115. obj = obj.slice();
  3116. for(t1 = 0, t2 = obj.length; t1 < t2; t1++) {
  3117. this.select_node(obj[t1], supress_event, prevent_open, e);
  3118. }
  3119. return true;
  3120. }
  3121. obj = this.get_node(obj);
  3122. if(!obj || obj.id === $.jstree.root) {
  3123. return false;
  3124. }
  3125. dom = this.get_node(obj, true);
  3126. if(!obj.state.selected) {
  3127. obj.state.selected = true;
  3128. this._data.core.selected.push(obj.id);
  3129. if(!prevent_open) {
  3130. dom = this._open_to(obj);
  3131. }
  3132. if(dom && dom.length) {
  3133. dom.attr('aria-selected', true).children('.jstree-anchor').addClass('jstree-clicked');
  3134. }
  3135. /**
  3136. * triggered when an node is selected
  3137. * @event
  3138. * @name select_node.jstree
  3139. * @param {Object} node
  3140. * @param {Array} selected the current selection
  3141. * @param {Object} event the event (if any) that triggered this select_node
  3142. */
  3143. this.trigger('select_node', { 'node' : obj, 'selected' : this._data.core.selected, 'event' : e });
  3144. if(!supress_event) {
  3145. /**
  3146. * triggered when selection changes
  3147. * @event
  3148. * @name changed.jstree
  3149. * @param {Object} node
  3150. * @param {Object} action the action that caused the selection to change
  3151. * @param {Array} selected the current selection
  3152. * @param {Object} event the event (if any) that triggered this changed event
  3153. */
  3154. this.trigger('changed', { 'action' : 'select_node', 'node' : obj, 'selected' : this._data.core.selected, 'event' : e });
  3155. }
  3156. }
  3157. },
  3158. /**
  3159. * deselect a node
  3160. * @name deselect_node(obj [, supress_event])
  3161. * @param {mixed} obj an array can be used to deselect multiple nodes
  3162. * @param {Boolean} supress_event if set to `true` the `changed.jstree` event won't be triggered
  3163. * @trigger deselect_node.jstree, changed.jstree
  3164. */
  3165. deselect_node : function (obj, supress_event, e) {
  3166. var t1, t2, dom;
  3167. if($.isArray(obj)) {
  3168. obj = obj.slice();
  3169. for(t1 = 0, t2 = obj.length; t1 < t2; t1++) {
  3170. this.deselect_node(obj[t1], supress_event, e);
  3171. }
  3172. return true;
  3173. }
  3174. obj = this.get_node(obj);
  3175. if(!obj || obj.id === $.jstree.root) {
  3176. return false;
  3177. }
  3178. dom = this.get_node(obj, true);
  3179. if(obj.state.selected) {
  3180. obj.state.selected = false;
  3181. this._data.core.selected = $.vakata.array_remove_item(this._data.core.selected, obj.id);
  3182. if(dom.length) {
  3183. dom.attr('aria-selected', false).children('.jstree-anchor').removeClass('jstree-clicked');
  3184. }
  3185. /**
  3186. * triggered when an node is deselected
  3187. * @event
  3188. * @name deselect_node.jstree
  3189. * @param {Object} node
  3190. * @param {Array} selected the current selection
  3191. * @param {Object} event the event (if any) that triggered this deselect_node
  3192. */
  3193. this.trigger('deselect_node', { 'node' : obj, 'selected' : this._data.core.selected, 'event' : e });
  3194. if(!supress_event) {
  3195. this.trigger('changed', { 'action' : 'deselect_node', 'node' : obj, 'selected' : this._data.core.selected, 'event' : e });
  3196. }
  3197. }
  3198. },
  3199. /**
  3200. * select all nodes in the tree
  3201. * @name select_all([supress_event])
  3202. * @param {Boolean} supress_event if set to `true` the `changed.jstree` event won't be triggered
  3203. * @trigger select_all.jstree, changed.jstree
  3204. */
  3205. select_all : function (supress_event) {
  3206. var tmp = this._data.core.selected.concat([]), i, j;
  3207. this._data.core.selected = this._model.data[$.jstree.root].children_d.concat();
  3208. for(i = 0, j = this._data.core.selected.length; i < j; i++) {
  3209. if(this._model.data[this._data.core.selected[i]]) {
  3210. this._model.data[this._data.core.selected[i]].state.selected = true;
  3211. }
  3212. }
  3213. this.redraw(true);
  3214. /**
  3215. * triggered when all nodes are selected
  3216. * @event
  3217. * @name select_all.jstree
  3218. * @param {Array} selected the current selection
  3219. */
  3220. this.trigger('select_all', { 'selected' : this._data.core.selected });
  3221. if(!supress_event) {
  3222. this.trigger('changed', { 'action' : 'select_all', 'selected' : this._data.core.selected, 'old_selection' : tmp });
  3223. }
  3224. },
  3225. /**
  3226. * deselect all selected nodes
  3227. * @name deselect_all([supress_event])
  3228. * @param {Boolean} supress_event if set to `true` the `changed.jstree` event won't be triggered
  3229. * @trigger deselect_all.jstree, changed.jstree
  3230. */
  3231. deselect_all : function (supress_event) {
  3232. var tmp = this._data.core.selected.concat([]), i, j;
  3233. for(i = 0, j = this._data.core.selected.length; i < j; i++) {
  3234. if(this._model.data[this._data.core.selected[i]]) {
  3235. this._model.data[this._data.core.selected[i]].state.selected = false;
  3236. }
  3237. }
  3238. this._data.core.selected = [];
  3239. this.element.find('.jstree-clicked').removeClass('jstree-clicked').parent().attr('aria-selected', false);
  3240. /**
  3241. * triggered when all nodes are deselected
  3242. * @event
  3243. * @name deselect_all.jstree
  3244. * @param {Object} node the previous selection
  3245. * @param {Array} selected the current selection
  3246. */
  3247. this.trigger('deselect_all', { 'selected' : this._data.core.selected, 'node' : tmp });
  3248. if(!supress_event) {
  3249. this.trigger('changed', { 'action' : 'deselect_all', 'selected' : this._data.core.selected, 'old_selection' : tmp });
  3250. }
  3251. },
  3252. /**
  3253. * checks if a node is selected
  3254. * @name is_selected(obj)
  3255. * @param {mixed} obj
  3256. * @return {Boolean}
  3257. */
  3258. is_selected : function (obj) {
  3259. obj = this.get_node(obj);
  3260. if(!obj || obj.id === $.jstree.root) {
  3261. return false;
  3262. }
  3263. return obj.state.selected;
  3264. },
  3265. /**
  3266. * get an array of all selected nodes
  3267. * @name get_selected([full])
  3268. * @param {mixed} full if set to `true` the returned array will consist of the full node objects, otherwise - only IDs will be returned
  3269. * @return {Array}
  3270. */
  3271. get_selected : function (full) {
  3272. return full ? $.map(this._data.core.selected, $.proxy(function (i) { return this.get_node(i); }, this)) : this._data.core.selected.slice();
  3273. },
  3274. /**
  3275. * get an array of all top level selected nodes (ignoring children of selected nodes)
  3276. * @name get_top_selected([full])
  3277. * @param {mixed} full if set to `true` the returned array will consist of the full node objects, otherwise - only IDs will be returned
  3278. * @return {Array}
  3279. */
  3280. get_top_selected : function (full) {
  3281. var tmp = this.get_selected(true),
  3282. obj = {}, i, j, k, l;
  3283. for(i = 0, j = tmp.length; i < j; i++) {
  3284. obj[tmp[i].id] = tmp[i];
  3285. }
  3286. for(i = 0, j = tmp.length; i < j; i++) {
  3287. for(k = 0, l = tmp[i].children_d.length; k < l; k++) {
  3288. if(obj[tmp[i].children_d[k]]) {
  3289. delete obj[tmp[i].children_d[k]];
  3290. }
  3291. }
  3292. }
  3293. tmp = [];
  3294. for(i in obj) {
  3295. if(obj.hasOwnProperty(i)) {
  3296. tmp.push(i);
  3297. }
  3298. }
  3299. return full ? $.map(tmp, $.proxy(function (i) { return this.get_node(i); }, this)) : tmp;
  3300. },
  3301. /**
  3302. * get an array of all bottom level selected nodes (ignoring selected parents)
  3303. * @name get_bottom_selected([full])
  3304. * @param {mixed} full if set to `true` the returned array will consist of the full node objects, otherwise - only IDs will be returned
  3305. * @return {Array}
  3306. */
  3307. get_bottom_selected : function (full) {
  3308. var tmp = this.get_selected(true),
  3309. obj = [], i, j;
  3310. for(i = 0, j = tmp.length; i < j; i++) {
  3311. if(!tmp[i].children.length) {
  3312. obj.push(tmp[i].id);
  3313. }
  3314. }
  3315. return full ? $.map(obj, $.proxy(function (i) { return this.get_node(i); }, this)) : obj;
  3316. },
  3317. /**
  3318. * gets the current state of the tree so that it can be restored later with `set_state(state)`. Used internally.
  3319. * @name get_state()
  3320. * @private
  3321. * @return {Object}
  3322. */
  3323. get_state : function () {
  3324. var state = {
  3325. 'core' : {
  3326. 'open' : [],
  3327. 'scroll' : {
  3328. 'left' : this.element.scrollLeft(),
  3329. 'top' : this.element.scrollTop()
  3330. },
  3331. /*!
  3332. 'themes' : {
  3333. 'name' : this.get_theme(),
  3334. 'icons' : this._data.core.themes.icons,
  3335. 'dots' : this._data.core.themes.dots
  3336. },
  3337. */
  3338. 'selected' : []
  3339. }
  3340. }, i;
  3341. for(i in this._model.data) {
  3342. if(this._model.data.hasOwnProperty(i)) {
  3343. if(i !== $.jstree.root) {
  3344. if(this._model.data[i].state.opened) {
  3345. state.core.open.push(i);
  3346. }
  3347. if(this._model.data[i].state.selected) {
  3348. state.core.selected.push(i);
  3349. }
  3350. }
  3351. }
  3352. }
  3353. return state;
  3354. },
  3355. /**
  3356. * sets the state of the tree. Used internally.
  3357. * @name set_state(state [, callback])
  3358. * @private
  3359. * @param {Object} state the state to restore. Keep in mind this object is passed by reference and jstree will modify it.
  3360. * @param {Function} callback an optional function to execute once the state is restored.
  3361. * @trigger set_state.jstree
  3362. */
  3363. set_state : function (state, callback) {
  3364. if(state) {
  3365. if(state.core && state.core.selected && state.core.initial_selection === undefined) {
  3366. state.core.initial_selection = this._data.core.selected.concat([]).sort().join(',');
  3367. }
  3368. if(state.core) {
  3369. var res, n, t, _this, i;
  3370. if(state.core.open) {
  3371. if(!$.isArray(state.core.open) || !state.core.open.length) {
  3372. delete state.core.open;
  3373. this.set_state(state, callback);
  3374. }
  3375. else {
  3376. this._load_nodes(state.core.open, function (nodes) {
  3377. this.open_node(nodes, false, 0);
  3378. delete state.core.open;
  3379. this.set_state(state, callback);
  3380. });
  3381. }
  3382. return false;
  3383. }
  3384. if(state.core.scroll) {
  3385. if(state.core.scroll && state.core.scroll.left !== undefined) {
  3386. this.element.scrollLeft(state.core.scroll.left);
  3387. }
  3388. if(state.core.scroll && state.core.scroll.top !== undefined) {
  3389. this.element.scrollTop(state.core.scroll.top);
  3390. }
  3391. delete state.core.scroll;
  3392. this.set_state(state, callback);
  3393. return false;
  3394. }
  3395. if(state.core.selected) {
  3396. _this = this;
  3397. if (state.core.initial_selection === undefined ||
  3398. state.core.initial_selection === this._data.core.selected.concat([]).sort().join(',')
  3399. ) {
  3400. this.deselect_all();
  3401. $.each(state.core.selected, function (i, v) {
  3402. _this.select_node(v, false, true);
  3403. });
  3404. }
  3405. delete state.core.initial_selection;
  3406. delete state.core.selected;
  3407. this.set_state(state, callback);
  3408. return false;
  3409. }
  3410. for(i in state) {
  3411. if(state.hasOwnProperty(i) && i !== "core" && $.inArray(i, this.settings.plugins) === -1) {
  3412. delete state[i];
  3413. }
  3414. }
  3415. if($.isEmptyObject(state.core)) {
  3416. delete state.core;
  3417. this.set_state(state, callback);
  3418. return false;
  3419. }
  3420. }
  3421. if($.isEmptyObject(state)) {
  3422. state = null;
  3423. if(callback) { callback.call(this); }
  3424. /**
  3425. * triggered when a `set_state` call completes
  3426. * @event
  3427. * @name set_state.jstree
  3428. */
  3429. this.trigger('set_state');
  3430. return false;
  3431. }
  3432. return true;
  3433. }
  3434. return false;
  3435. },
  3436. /**
  3437. * refreshes the tree - all nodes are reloaded with calls to `load_node`.
  3438. * @name refresh()
  3439. * @param {Boolean} skip_loading an option to skip showing the loading indicator
  3440. * @param {Mixed} forget_state if set to `true` state will not be reapplied, if set to a function (receiving the current state as argument) the result of that function will be used as state
  3441. * @trigger refresh.jstree
  3442. */
  3443. refresh : function (skip_loading, forget_state) {
  3444. this._data.core.state = forget_state === true ? {} : this.get_state();
  3445. if(forget_state && $.isFunction(forget_state)) { this._data.core.state = forget_state.call(this, this._data.core.state); }
  3446. this._cnt = 0;
  3447. this._model.data = {};
  3448. this._model.data[$.jstree.root] = {
  3449. id : $.jstree.root,
  3450. parent : null,
  3451. parents : [],
  3452. children : [],
  3453. children_d : [],
  3454. state : { loaded : false }
  3455. };
  3456. this._data.core.selected = [];
  3457. this._data.core.last_clicked = null;
  3458. this._data.core.focused = null;
  3459. var c = this.get_container_ul()[0].className;
  3460. if(!skip_loading) {
  3461. this.element.html("<"+"ul class='"+c+"' role='group'><"+"li class='jstree-initial-node jstree-loading jstree-leaf jstree-last' role='treeitem' id='j"+this._id+"_loading'><i class='jstree-icon jstree-ocl'></i><"+"a class='jstree-anchor' href='#'><i class='jstree-icon jstree-themeicon-hidden'></i>" + this.get_string("Loading ...") + "</a></li></ul>");
  3462. this.element.attr('aria-activedescendant','j'+this._id+'_loading');
  3463. }
  3464. this.load_node($.jstree.root, function (o, s) {
  3465. if(s) {
  3466. this.get_container_ul()[0].className = c;
  3467. if(this._firstChild(this.get_container_ul()[0])) {
  3468. this.element.attr('aria-activedescendant',this._firstChild(this.get_container_ul()[0]).id);
  3469. }
  3470. this.set_state($.extend(true, {}, this._data.core.state), function () {
  3471. /**
  3472. * triggered when a `refresh` call completes
  3473. * @event
  3474. * @name refresh.jstree
  3475. */
  3476. this.trigger('refresh');
  3477. });
  3478. }
  3479. this._data.core.state = null;
  3480. });
  3481. },
  3482. /**
  3483. * refreshes a node in the tree (reload its children) all opened nodes inside that node are reloaded with calls to `load_node`.
  3484. * @name refresh_node(obj)
  3485. * @param {mixed} obj the node
  3486. * @trigger refresh_node.jstree
  3487. */
  3488. refresh_node : function (obj) {
  3489. obj = this.get_node(obj);
  3490. if(!obj || obj.id === $.jstree.root) { return false; }
  3491. var opened = [], to_load = [], s = this._data.core.selected.concat([]);
  3492. to_load.push(obj.id);
  3493. if(obj.state.opened === true) { opened.push(obj.id); }
  3494. this.get_node(obj, true).find('.jstree-open').each(function() { to_load.push(this.id); opened.push(this.id); });
  3495. this._load_nodes(to_load, $.proxy(function (nodes) {
  3496. this.open_node(opened, false, 0);
  3497. this.select_node(s);
  3498. /**
  3499. * triggered when a node is refreshed
  3500. * @event
  3501. * @name refresh_node.jstree
  3502. * @param {Object} node - the refreshed node
  3503. * @param {Array} nodes - an array of the IDs of the nodes that were reloaded
  3504. */
  3505. this.trigger('refresh_node', { 'node' : obj, 'nodes' : nodes });
  3506. }, this), false, true);
  3507. },
  3508. /**
  3509. * set (change) the ID of a node
  3510. * @name set_id(obj, id)
  3511. * @param {mixed} obj the node
  3512. * @param {String} id the new ID
  3513. * @return {Boolean}
  3514. * @trigger set_id.jstree
  3515. */
  3516. set_id : function (obj, id) {
  3517. obj = this.get_node(obj);
  3518. if(!obj || obj.id === $.jstree.root) { return false; }
  3519. var i, j, m = this._model.data, old = obj.id;
  3520. id = id.toString();
  3521. // update parents (replace current ID with new one in children and children_d)
  3522. m[obj.parent].children[$.inArray(obj.id, m[obj.parent].children)] = id;
  3523. for(i = 0, j = obj.parents.length; i < j; i++) {
  3524. m[obj.parents[i]].children_d[$.inArray(obj.id, m[obj.parents[i]].children_d)] = id;
  3525. }
  3526. // update children (replace current ID with new one in parent and parents)
  3527. for(i = 0, j = obj.children.length; i < j; i++) {
  3528. m[obj.children[i]].parent = id;
  3529. }
  3530. for(i = 0, j = obj.children_d.length; i < j; i++) {
  3531. m[obj.children_d[i]].parents[$.inArray(obj.id, m[obj.children_d[i]].parents)] = id;
  3532. }
  3533. i = $.inArray(obj.id, this._data.core.selected);
  3534. if(i !== -1) { this._data.core.selected[i] = id; }
  3535. // update model and obj itself (obj.id, this._model.data[KEY])
  3536. i = this.get_node(obj.id, true);
  3537. if(i) {
  3538. i.attr('id', id); //.children('.jstree-anchor').attr('id', id + '_anchor').end().attr('aria-labelledby', id + '_anchor');
  3539. if(this.element.attr('aria-activedescendant') === obj.id) {
  3540. this.element.attr('aria-activedescendant', id);
  3541. }
  3542. }
  3543. delete m[obj.id];
  3544. obj.id = id;
  3545. obj.li_attr.id = id;
  3546. m[id] = obj;
  3547. /**
  3548. * triggered when a node id value is changed
  3549. * @event
  3550. * @name set_id.jstree
  3551. * @param {Object} node
  3552. * @param {String} old the old id
  3553. */
  3554. this.trigger('set_id',{ "node" : obj, "new" : obj.id, "old" : old });
  3555. return true;
  3556. },
  3557. /**
  3558. * get the text value of a node
  3559. * @name get_text(obj)
  3560. * @param {mixed} obj the node
  3561. * @return {String}
  3562. */
  3563. get_text : function (obj) {
  3564. obj = this.get_node(obj);
  3565. return (!obj || obj.id === $.jstree.root) ? false : obj.text;
  3566. },
  3567. /**
  3568. * set the text value of a node. Used internally, please use `rename_node(obj, val)`.
  3569. * @private
  3570. * @name set_text(obj, val)
  3571. * @param {mixed} obj the node, you can pass an array to set the text on multiple nodes
  3572. * @param {String} val the new text value
  3573. * @return {Boolean}
  3574. * @trigger set_text.jstree
  3575. */
  3576. set_text : function (obj, val) {
  3577. var t1, t2;
  3578. if($.isArray(obj)) {
  3579. obj = obj.slice();
  3580. for(t1 = 0, t2 = obj.length; t1 < t2; t1++) {
  3581. this.set_text(obj[t1], val);
  3582. }
  3583. return true;
  3584. }
  3585. obj = this.get_node(obj);
  3586. if(!obj || obj.id === $.jstree.root) { return false; }
  3587. obj.text = val;
  3588. if(this.get_node(obj, true).length) {
  3589. this.redraw_node(obj.id);
  3590. }
  3591. /**
  3592. * triggered when a node text value is changed
  3593. * @event
  3594. * @name set_text.jstree
  3595. * @param {Object} obj
  3596. * @param {String} text the new value
  3597. */
  3598. this.trigger('set_text',{ "obj" : obj, "text" : val });
  3599. return true;
  3600. },
  3601. /**
  3602. * gets a JSON representation of a node (or the whole tree)
  3603. * @name get_json([obj, options])
  3604. * @param {mixed} obj
  3605. * @param {Object} options
  3606. * @param {Boolean} options.no_state do not return state information
  3607. * @param {Boolean} options.no_id do not return ID
  3608. * @param {Boolean} options.no_children do not include children
  3609. * @param {Boolean} options.no_data do not include node data
  3610. * @param {Boolean} options.no_li_attr do not include LI attributes
  3611. * @param {Boolean} options.no_a_attr do not include A attributes
  3612. * @param {Boolean} options.flat return flat JSON instead of nested
  3613. * @return {Object}
  3614. */
  3615. get_json : function (obj, options, flat) {
  3616. obj = this.get_node(obj || $.jstree.root);
  3617. if(!obj) { return false; }
  3618. if(options && options.flat && !flat) { flat = []; }
  3619. var tmp = {
  3620. 'id' : obj.id,
  3621. 'text' : obj.text,
  3622. 'icon' : this.get_icon(obj),
  3623. 'li_attr' : $.extend(true, {}, obj.li_attr),
  3624. 'a_attr' : $.extend(true, {}, obj.a_attr),
  3625. 'state' : {},
  3626. 'data' : options && options.no_data ? false : $.extend(true, $.isArray(obj.data)?[]:{}, obj.data)
  3627. //( this.get_node(obj, true).length ? this.get_node(obj, true).data() : obj.data ),
  3628. }, i, j;
  3629. if(options && options.flat) {
  3630. tmp.parent = obj.parent;
  3631. }
  3632. else {
  3633. tmp.children = [];
  3634. }
  3635. if(!options || !options.no_state) {
  3636. for(i in obj.state) {
  3637. if(obj.state.hasOwnProperty(i)) {
  3638. tmp.state[i] = obj.state[i];
  3639. }
  3640. }
  3641. } else {
  3642. delete tmp.state;
  3643. }
  3644. if(options && options.no_li_attr) {
  3645. delete tmp.li_attr;
  3646. }
  3647. if(options && options.no_a_attr) {
  3648. delete tmp.a_attr;
  3649. }
  3650. if(options && options.no_id) {
  3651. delete tmp.id;
  3652. if(tmp.li_attr && tmp.li_attr.id) {
  3653. delete tmp.li_attr.id;
  3654. }
  3655. if(tmp.a_attr && tmp.a_attr.id) {
  3656. delete tmp.a_attr.id;
  3657. }
  3658. }
  3659. if(options && options.flat && obj.id !== $.jstree.root) {
  3660. flat.push(tmp);
  3661. }
  3662. if(!options || !options.no_children) {
  3663. for(i = 0, j = obj.children.length; i < j; i++) {
  3664. if(options && options.flat) {
  3665. this.get_json(obj.children[i], options, flat);
  3666. }
  3667. else {
  3668. tmp.children.push(this.get_json(obj.children[i], options));
  3669. }
  3670. }
  3671. }
  3672. return options && options.flat ? flat : (obj.id === $.jstree.root ? tmp.children : tmp);
  3673. },
  3674. /**
  3675. * create a new node (do not confuse with load_node)
  3676. * @name create_node([par, node, pos, callback, is_loaded])
  3677. * @param {mixed} par the parent node (to create a root node use either "#" (string) or `null`)
  3678. * @param {mixed} node the data for the new node (a valid JSON object, or a simple string with the name)
  3679. * @param {mixed} pos the index at which to insert the node, "first" and "last" are also supported, default is "last"
  3680. * @param {Function} callback a function to be called once the node is created
  3681. * @param {Boolean} is_loaded internal argument indicating if the parent node was succesfully loaded
  3682. * @return {String} the ID of the newly create node
  3683. * @trigger model.jstree, create_node.jstree
  3684. */
  3685. create_node : function (par, node, pos, callback, is_loaded) {
  3686. if(par === null) { par = $.jstree.root; }
  3687. par = this.get_node(par);
  3688. if(!par) { return false; }
  3689. pos = pos === undefined ? "last" : pos;
  3690. if(!pos.toString().match(/^(before|after)$/) && !is_loaded && !this.is_loaded(par)) {
  3691. return this.load_node(par, function () { this.create_node(par, node, pos, callback, true); });
  3692. }
  3693. if(!node) { node = { "text" : this.get_string('New node') }; }
  3694. if(typeof node === "string") {
  3695. node = { "text" : node };
  3696. } else {
  3697. node = $.extend(true, {}, node);
  3698. }
  3699. if(node.text === undefined) { node.text = this.get_string('New node'); }
  3700. var tmp, dpc, i, j;
  3701. if(par.id === $.jstree.root) {
  3702. if(pos === "before") { pos = "first"; }
  3703. if(pos === "after") { pos = "last"; }
  3704. }
  3705. switch(pos) {
  3706. case "before":
  3707. tmp = this.get_node(par.parent);
  3708. pos = $.inArray(par.id, tmp.children);
  3709. par = tmp;
  3710. break;
  3711. case "after" :
  3712. tmp = this.get_node(par.parent);
  3713. pos = $.inArray(par.id, tmp.children) + 1;
  3714. par = tmp;
  3715. break;
  3716. case "inside":
  3717. case "first":
  3718. pos = 0;
  3719. break;
  3720. case "last":
  3721. pos = par.children.length;
  3722. break;
  3723. default:
  3724. if(!pos) { pos = 0; }
  3725. break;
  3726. }
  3727. if(pos > par.children.length) { pos = par.children.length; }
  3728. if(!node.id) { node.id = true; }
  3729. if(!this.check("create_node", node, par, pos)) {
  3730. this.settings.core.error.call(this, this._data.core.last_error);
  3731. return false;
  3732. }
  3733. if(node.id === true) { delete node.id; }
  3734. node = this._parse_model_from_json(node, par.id, par.parents.concat());
  3735. if(!node) { return false; }
  3736. tmp = this.get_node(node);
  3737. dpc = [];
  3738. dpc.push(node);
  3739. dpc = dpc.concat(tmp.children_d);
  3740. this.trigger('model', { "nodes" : dpc, "parent" : par.id });
  3741. par.children_d = par.children_d.concat(dpc);
  3742. for(i = 0, j = par.parents.length; i < j; i++) {
  3743. this._model.data[par.parents[i]].children_d = this._model.data[par.parents[i]].children_d.concat(dpc);
  3744. }
  3745. node = tmp;
  3746. tmp = [];
  3747. for(i = 0, j = par.children.length; i < j; i++) {
  3748. tmp[i >= pos ? i+1 : i] = par.children[i];
  3749. }
  3750. tmp[pos] = node.id;
  3751. par.children = tmp;
  3752. this.redraw_node(par, true);
  3753. /**
  3754. * triggered when a node is created
  3755. * @event
  3756. * @name create_node.jstree
  3757. * @param {Object} node
  3758. * @param {String} parent the parent's ID
  3759. * @param {Number} position the position of the new node among the parent's children
  3760. */
  3761. this.trigger('create_node', { "node" : this.get_node(node), "parent" : par.id, "position" : pos });
  3762. if(callback) { callback.call(this, this.get_node(node)); }
  3763. return node.id;
  3764. },
  3765. /**
  3766. * set the text value of a node
  3767. * @name rename_node(obj, val)
  3768. * @param {mixed} obj the node, you can pass an array to rename multiple nodes to the same name
  3769. * @param {String} val the new text value
  3770. * @return {Boolean}
  3771. * @trigger rename_node.jstree
  3772. */
  3773. rename_node : function (obj, val) {
  3774. var t1, t2, old;
  3775. if($.isArray(obj)) {
  3776. obj = obj.slice();
  3777. for(t1 = 0, t2 = obj.length; t1 < t2; t1++) {
  3778. this.rename_node(obj[t1], val);
  3779. }
  3780. return true;
  3781. }
  3782. obj = this.get_node(obj);
  3783. if(!obj || obj.id === $.jstree.root) { return false; }
  3784. old = obj.text;
  3785. if(!this.check("rename_node", obj, this.get_parent(obj), val)) {
  3786. this.settings.core.error.call(this, this._data.core.last_error);
  3787. return false;
  3788. }
  3789. this.set_text(obj, val); // .apply(this, Array.prototype.slice.call(arguments))
  3790. /**
  3791. * triggered when a node is renamed
  3792. * @event
  3793. * @name rename_node.jstree
  3794. * @param {Object} node
  3795. * @param {String} text the new value
  3796. * @param {String} old the old value
  3797. */
  3798. this.trigger('rename_node', { "node" : obj, "text" : val, "old" : old });
  3799. return true;
  3800. },
  3801. /**
  3802. * remove a node
  3803. * @name delete_node(obj)
  3804. * @param {mixed} obj the node, you can pass an array to delete multiple nodes
  3805. * @return {Boolean}
  3806. * @trigger delete_node.jstree, changed.jstree
  3807. */
  3808. delete_node : function (obj) {
  3809. var t1, t2, par, pos, tmp, i, j, k, l, c, top, lft;
  3810. if($.isArray(obj)) {
  3811. obj = obj.slice();
  3812. for(t1 = 0, t2 = obj.length; t1 < t2; t1++) {
  3813. this.delete_node(obj[t1]);
  3814. }
  3815. return true;
  3816. }
  3817. obj = this.get_node(obj);
  3818. if(!obj || obj.id === $.jstree.root) { return false; }
  3819. par = this.get_node(obj.parent);
  3820. pos = $.inArray(obj.id, par.children);
  3821. c = false;
  3822. if(!this.check("delete_node", obj, par, pos)) {
  3823. this.settings.core.error.call(this, this._data.core.last_error);
  3824. return false;
  3825. }
  3826. if(pos !== -1) {
  3827. par.children = $.vakata.array_remove(par.children, pos);
  3828. }
  3829. tmp = obj.children_d.concat([]);
  3830. tmp.push(obj.id);
  3831. for(i = 0, j = obj.parents.length; i < j; i++) {
  3832. this._model.data[obj.parents[i]].children_d = $.vakata.array_filter(this._model.data[obj.parents[i]].children_d, function (v) {
  3833. return $.inArray(v, tmp) === -1;
  3834. });
  3835. }
  3836. for(k = 0, l = tmp.length; k < l; k++) {
  3837. if(this._model.data[tmp[k]].state.selected) {
  3838. c = true;
  3839. break;
  3840. }
  3841. }
  3842. if (c) {
  3843. this._data.core.selected = $.vakata.array_filter(this._data.core.selected, function (v) {
  3844. return $.inArray(v, tmp) === -1;
  3845. });
  3846. }
  3847. /**
  3848. * triggered when a node is deleted
  3849. * @event
  3850. * @name delete_node.jstree
  3851. * @param {Object} node
  3852. * @param {String} parent the parent's ID
  3853. */
  3854. this.trigger('delete_node', { "node" : obj, "parent" : par.id });
  3855. if(c) {
  3856. this.trigger('changed', { 'action' : 'delete_node', 'node' : obj, 'selected' : this._data.core.selected, 'parent' : par.id });
  3857. }
  3858. for(k = 0, l = tmp.length; k < l; k++) {
  3859. delete this._model.data[tmp[k]];
  3860. }
  3861. if($.inArray(this._data.core.focused, tmp) !== -1) {
  3862. this._data.core.focused = null;
  3863. top = this.element[0].scrollTop;
  3864. lft = this.element[0].scrollLeft;
  3865. if(par.id === $.jstree.root) {
  3866. if (this._model.data[$.jstree.root].children[0]) {
  3867. this.get_node(this._model.data[$.jstree.root].children[0], true).children('.jstree-anchor').focus();
  3868. }
  3869. }
  3870. else {
  3871. this.get_node(par, true).children('.jstree-anchor').focus();
  3872. }
  3873. this.element[0].scrollTop = top;
  3874. this.element[0].scrollLeft = lft;
  3875. }
  3876. this.redraw_node(par, true);
  3877. return true;
  3878. },
  3879. /**
  3880. * check if an operation is premitted on the tree. Used internally.
  3881. * @private
  3882. * @name check(chk, obj, par, pos)
  3883. * @param {String} chk the operation to check, can be "create_node", "rename_node", "delete_node", "copy_node" or "move_node"
  3884. * @param {mixed} obj the node
  3885. * @param {mixed} par the parent
  3886. * @param {mixed} pos the position to insert at, or if "rename_node" - the new name
  3887. * @param {mixed} more some various additional information, for example if a "move_node" operations is triggered by DND this will be the hovered node
  3888. * @return {Boolean}
  3889. */
  3890. check : function (chk, obj, par, pos, more) {
  3891. obj = obj && obj.id ? obj : this.get_node(obj);
  3892. par = par && par.id ? par : this.get_node(par);
  3893. var tmp = chk.match(/^move_node|copy_node|create_node$/i) ? par : obj,
  3894. chc = this.settings.core.check_callback;
  3895. if(chk === "move_node" || chk === "copy_node") {
  3896. if((!more || !more.is_multi) && (obj.id === par.id || (chk === "move_node" && $.inArray(obj.id, par.children) === pos) || $.inArray(par.id, obj.children_d) !== -1)) {
  3897. this._data.core.last_error = { 'error' : 'check', 'plugin' : 'core', 'id' : 'core_01', 'reason' : 'Moving parent inside child', 'data' : JSON.stringify({ 'chk' : chk, 'pos' : pos, 'obj' : obj && obj.id ? obj.id : false, 'par' : par && par.id ? par.id : false }) };
  3898. return false;
  3899. }
  3900. }
  3901. if(tmp && tmp.data) { tmp = tmp.data; }
  3902. if(tmp && tmp.functions && (tmp.functions[chk] === false || tmp.functions[chk] === true)) {
  3903. if(tmp.functions[chk] === false) {
  3904. this._data.core.last_error = { 'error' : 'check', 'plugin' : 'core', 'id' : 'core_02', 'reason' : 'Node data prevents function: ' + chk, 'data' : JSON.stringify({ 'chk' : chk, 'pos' : pos, 'obj' : obj && obj.id ? obj.id : false, 'par' : par && par.id ? par.id : false }) };
  3905. }
  3906. return tmp.functions[chk];
  3907. }
  3908. if(chc === false || ($.isFunction(chc) && chc.call(this, chk, obj, par, pos, more) === false) || (chc && chc[chk] === false)) {
  3909. this._data.core.last_error = { 'error' : 'check', 'plugin' : 'core', 'id' : 'core_03', 'reason' : 'User config for core.check_callback prevents function: ' + chk, 'data' : JSON.stringify({ 'chk' : chk, 'pos' : pos, 'obj' : obj && obj.id ? obj.id : false, 'par' : par && par.id ? par.id : false }) };
  3910. return false;
  3911. }
  3912. return true;
  3913. },
  3914. /**
  3915. * get the last error
  3916. * @name last_error()
  3917. * @return {Object}
  3918. */
  3919. last_error : function () {
  3920. return this._data.core.last_error;
  3921. },
  3922. /**
  3923. * move a node to a new parent
  3924. * @name move_node(obj, par [, pos, callback, is_loaded])
  3925. * @param {mixed} obj the node to move, pass an array to move multiple nodes
  3926. * @param {mixed} par the new parent
  3927. * @param {mixed} pos the position to insert at (besides integer values, "first" and "last" are supported, as well as "before" and "after"), defaults to integer `0`
  3928. * @param {function} callback a function to call once the move is completed, receives 3 arguments - the node, the new parent and the position
  3929. * @param {Boolean} is_loaded internal parameter indicating if the parent node has been loaded
  3930. * @param {Boolean} skip_redraw internal parameter indicating if the tree should be redrawn
  3931. * @param {Boolean} instance internal parameter indicating if the node comes from another instance
  3932. * @trigger move_node.jstree
  3933. */
  3934. move_node : function (obj, par, pos, callback, is_loaded, skip_redraw, origin) {
  3935. var t1, t2, old_par, old_pos, new_par, old_ins, is_multi, dpc, tmp, i, j, k, l, p;
  3936. par = this.get_node(par);
  3937. pos = pos === undefined ? 0 : pos;
  3938. if(!par) { return false; }
  3939. if(!pos.toString().match(/^(before|after)$/) && !is_loaded && !this.is_loaded(par)) {
  3940. return this.load_node(par, function () { this.move_node(obj, par, pos, callback, true, false, origin); });
  3941. }
  3942. if($.isArray(obj)) {
  3943. if(obj.length === 1) {
  3944. obj = obj[0];
  3945. }
  3946. else {
  3947. //obj = obj.slice();
  3948. for(t1 = 0, t2 = obj.length; t1 < t2; t1++) {
  3949. if((tmp = this.move_node(obj[t1], par, pos, callback, is_loaded, false, origin))) {
  3950. par = tmp;
  3951. pos = "after";
  3952. }
  3953. }
  3954. this.redraw();
  3955. return true;
  3956. }
  3957. }
  3958. obj = obj && obj.id ? obj : this.get_node(obj);
  3959. if(!obj || obj.id === $.jstree.root) { return false; }
  3960. old_par = (obj.parent || $.jstree.root).toString();
  3961. new_par = (!pos.toString().match(/^(before|after)$/) || par.id === $.jstree.root) ? par : this.get_node(par.parent);
  3962. old_ins = origin ? origin : (this._model.data[obj.id] ? this : $.jstree.reference(obj.id));
  3963. is_multi = !old_ins || !old_ins._id || (this._id !== old_ins._id);
  3964. old_pos = old_ins && old_ins._id && old_par && old_ins._model.data[old_par] && old_ins._model.data[old_par].children ? $.inArray(obj.id, old_ins._model.data[old_par].children) : -1;
  3965. if(old_ins && old_ins._id) {
  3966. obj = old_ins._model.data[obj.id];
  3967. }
  3968. if(is_multi) {
  3969. if((tmp = this.copy_node(obj, par, pos, callback, is_loaded, false, origin))) {
  3970. if(old_ins) { old_ins.delete_node(obj); }
  3971. return tmp;
  3972. }
  3973. return false;
  3974. }
  3975. //var m = this._model.data;
  3976. if(par.id === $.jstree.root) {
  3977. if(pos === "before") { pos = "first"; }
  3978. if(pos === "after") { pos = "last"; }
  3979. }
  3980. switch(pos) {
  3981. case "before":
  3982. pos = $.inArray(par.id, new_par.children);
  3983. break;
  3984. case "after" :
  3985. pos = $.inArray(par.id, new_par.children) + 1;
  3986. break;
  3987. case "inside":
  3988. case "first":
  3989. pos = 0;
  3990. break;
  3991. case "last":
  3992. pos = new_par.children.length;
  3993. break;
  3994. default:
  3995. if(!pos) { pos = 0; }
  3996. break;
  3997. }
  3998. if(pos > new_par.children.length) { pos = new_par.children.length; }
  3999. if(!this.check("move_node", obj, new_par, pos, { 'core' : true, 'origin' : origin, 'is_multi' : (old_ins && old_ins._id && old_ins._id !== this._id), 'is_foreign' : (!old_ins || !old_ins._id) })) {
  4000. this.settings.core.error.call(this, this._data.core.last_error);
  4001. return false;
  4002. }
  4003. if(obj.parent === new_par.id) {
  4004. dpc = new_par.children.concat();
  4005. tmp = $.inArray(obj.id, dpc);
  4006. if(tmp !== -1) {
  4007. dpc = $.vakata.array_remove(dpc, tmp);
  4008. if(pos > tmp) { pos--; }
  4009. }
  4010. tmp = [];
  4011. for(i = 0, j = dpc.length; i < j; i++) {
  4012. tmp[i >= pos ? i+1 : i] = dpc[i];
  4013. }
  4014. tmp[pos] = obj.id;
  4015. new_par.children = tmp;
  4016. this._node_changed(new_par.id);
  4017. this.redraw(new_par.id === $.jstree.root);
  4018. }
  4019. else {
  4020. // clean old parent and up
  4021. tmp = obj.children_d.concat();
  4022. tmp.push(obj.id);
  4023. for(i = 0, j = obj.parents.length; i < j; i++) {
  4024. dpc = [];
  4025. p = old_ins._model.data[obj.parents[i]].children_d;
  4026. for(k = 0, l = p.length; k < l; k++) {
  4027. if($.inArray(p[k], tmp) === -1) {
  4028. dpc.push(p[k]);
  4029. }
  4030. }
  4031. old_ins._model.data[obj.parents[i]].children_d = dpc;
  4032. }
  4033. old_ins._model.data[old_par].children = $.vakata.array_remove_item(old_ins._model.data[old_par].children, obj.id);
  4034. // insert into new parent and up
  4035. for(i = 0, j = new_par.parents.length; i < j; i++) {
  4036. this._model.data[new_par.parents[i]].children_d = this._model.data[new_par.parents[i]].children_d.concat(tmp);
  4037. }
  4038. dpc = [];
  4039. for(i = 0, j = new_par.children.length; i < j; i++) {
  4040. dpc[i >= pos ? i+1 : i] = new_par.children[i];
  4041. }
  4042. dpc[pos] = obj.id;
  4043. new_par.children = dpc;
  4044. new_par.children_d.push(obj.id);
  4045. new_par.children_d = new_par.children_d.concat(obj.children_d);
  4046. // update object
  4047. obj.parent = new_par.id;
  4048. tmp = new_par.parents.concat();
  4049. tmp.unshift(new_par.id);
  4050. p = obj.parents.length;
  4051. obj.parents = tmp;
  4052. // update object children
  4053. tmp = tmp.concat();
  4054. for(i = 0, j = obj.children_d.length; i < j; i++) {
  4055. this._model.data[obj.children_d[i]].parents = this._model.data[obj.children_d[i]].parents.slice(0,p*-1);
  4056. Array.prototype.push.apply(this._model.data[obj.children_d[i]].parents, tmp);
  4057. }
  4058. if(old_par === $.jstree.root || new_par.id === $.jstree.root) {
  4059. this._model.force_full_redraw = true;
  4060. }
  4061. if(!this._model.force_full_redraw) {
  4062. this._node_changed(old_par);
  4063. this._node_changed(new_par.id);
  4064. }
  4065. if(!skip_redraw) {
  4066. this.redraw();
  4067. }
  4068. }
  4069. if(callback) { callback.call(this, obj, new_par, pos); }
  4070. /**
  4071. * triggered when a node is moved
  4072. * @event
  4073. * @name move_node.jstree
  4074. * @param {Object} node
  4075. * @param {String} parent the parent's ID
  4076. * @param {Number} position the position of the node among the parent's children
  4077. * @param {String} old_parent the old parent of the node
  4078. * @param {Number} old_position the old position of the node
  4079. * @param {Boolean} is_multi do the node and new parent belong to different instances
  4080. * @param {jsTree} old_instance the instance the node came from
  4081. * @param {jsTree} new_instance the instance of the new parent
  4082. */
  4083. this.trigger('move_node', { "node" : obj, "parent" : new_par.id, "position" : pos, "old_parent" : old_par, "old_position" : old_pos, 'is_multi' : (old_ins && old_ins._id && old_ins._id !== this._id), 'is_foreign' : (!old_ins || !old_ins._id), 'old_instance' : old_ins, 'new_instance' : this });
  4084. return obj.id;
  4085. },
  4086. /**
  4087. * copy a node to a new parent
  4088. * @name copy_node(obj, par [, pos, callback, is_loaded])
  4089. * @param {mixed} obj the node to copy, pass an array to copy multiple nodes
  4090. * @param {mixed} par the new parent
  4091. * @param {mixed} pos the position to insert at (besides integer values, "first" and "last" are supported, as well as "before" and "after"), defaults to integer `0`
  4092. * @param {function} callback a function to call once the move is completed, receives 3 arguments - the node, the new parent and the position
  4093. * @param {Boolean} is_loaded internal parameter indicating if the parent node has been loaded
  4094. * @param {Boolean} skip_redraw internal parameter indicating if the tree should be redrawn
  4095. * @param {Boolean} instance internal parameter indicating if the node comes from another instance
  4096. * @trigger model.jstree copy_node.jstree
  4097. */
  4098. copy_node : function (obj, par, pos, callback, is_loaded, skip_redraw, origin) {
  4099. var t1, t2, dpc, tmp, i, j, node, old_par, new_par, old_ins, is_multi;
  4100. par = this.get_node(par);
  4101. pos = pos === undefined ? 0 : pos;
  4102. if(!par) { return false; }
  4103. if(!pos.toString().match(/^(before|after)$/) && !is_loaded && !this.is_loaded(par)) {
  4104. return this.load_node(par, function () { this.copy_node(obj, par, pos, callback, true, false, origin); });
  4105. }
  4106. if($.isArray(obj)) {
  4107. if(obj.length === 1) {
  4108. obj = obj[0];
  4109. }
  4110. else {
  4111. //obj = obj.slice();
  4112. for(t1 = 0, t2 = obj.length; t1 < t2; t1++) {
  4113. if((tmp = this.copy_node(obj[t1], par, pos, callback, is_loaded, true, origin))) {
  4114. par = tmp;
  4115. pos = "after";
  4116. }
  4117. }
  4118. this.redraw();
  4119. return true;
  4120. }
  4121. }
  4122. obj = obj && obj.id ? obj : this.get_node(obj);
  4123. if(!obj || obj.id === $.jstree.root) { return false; }
  4124. old_par = (obj.parent || $.jstree.root).toString();
  4125. new_par = (!pos.toString().match(/^(before|after)$/) || par.id === $.jstree.root) ? par : this.get_node(par.parent);
  4126. old_ins = origin ? origin : (this._model.data[obj.id] ? this : $.jstree.reference(obj.id));
  4127. is_multi = !old_ins || !old_ins._id || (this._id !== old_ins._id);
  4128. if(old_ins && old_ins._id) {
  4129. obj = old_ins._model.data[obj.id];
  4130. }
  4131. if(par.id === $.jstree.root) {
  4132. if(pos === "before") { pos = "first"; }
  4133. if(pos === "after") { pos = "last"; }
  4134. }
  4135. switch(pos) {
  4136. case "before":
  4137. pos = $.inArray(par.id, new_par.children);
  4138. break;
  4139. case "after" :
  4140. pos = $.inArray(par.id, new_par.children) + 1;
  4141. break;
  4142. case "inside":
  4143. case "first":
  4144. pos = 0;
  4145. break;
  4146. case "last":
  4147. pos = new_par.children.length;
  4148. break;
  4149. default:
  4150. if(!pos) { pos = 0; }
  4151. break;
  4152. }
  4153. if(pos > new_par.children.length) { pos = new_par.children.length; }
  4154. if(!this.check("copy_node", obj, new_par, pos, { 'core' : true, 'origin' : origin, 'is_multi' : (old_ins && old_ins._id && old_ins._id !== this._id), 'is_foreign' : (!old_ins || !old_ins._id) })) {
  4155. this.settings.core.error.call(this, this._data.core.last_error);
  4156. return false;
  4157. }
  4158. node = old_ins ? old_ins.get_json(obj, { no_id : true, no_data : true, no_state : true }) : obj;
  4159. if(!node) { return false; }
  4160. if(node.id === true) { delete node.id; }
  4161. node = this._parse_model_from_json(node, new_par.id, new_par.parents.concat());
  4162. if(!node) { return false; }
  4163. tmp = this.get_node(node);
  4164. if(obj && obj.state && obj.state.loaded === false) { tmp.state.loaded = false; }
  4165. dpc = [];
  4166. dpc.push(node);
  4167. dpc = dpc.concat(tmp.children_d);
  4168. this.trigger('model', { "nodes" : dpc, "parent" : new_par.id });
  4169. // insert into new parent and up
  4170. for(i = 0, j = new_par.parents.length; i < j; i++) {
  4171. this._model.data[new_par.parents[i]].children_d = this._model.data[new_par.parents[i]].children_d.concat(dpc);
  4172. }
  4173. dpc = [];
  4174. for(i = 0, j = new_par.children.length; i < j; i++) {
  4175. dpc[i >= pos ? i+1 : i] = new_par.children[i];
  4176. }
  4177. dpc[pos] = tmp.id;
  4178. new_par.children = dpc;
  4179. new_par.children_d.push(tmp.id);
  4180. new_par.children_d = new_par.children_d.concat(tmp.children_d);
  4181. if(new_par.id === $.jstree.root) {
  4182. this._model.force_full_redraw = true;
  4183. }
  4184. if(!this._model.force_full_redraw) {
  4185. this._node_changed(new_par.id);
  4186. }
  4187. if(!skip_redraw) {
  4188. this.redraw(new_par.id === $.jstree.root);
  4189. }
  4190. if(callback) { callback.call(this, tmp, new_par, pos); }
  4191. /**
  4192. * triggered when a node is copied
  4193. * @event
  4194. * @name copy_node.jstree
  4195. * @param {Object} node the copied node
  4196. * @param {Object} original the original node
  4197. * @param {String} parent the parent's ID
  4198. * @param {Number} position the position of the node among the parent's children
  4199. * @param {String} old_parent the old parent of the node
  4200. * @param {Number} old_position the position of the original node
  4201. * @param {Boolean} is_multi do the node and new parent belong to different instances
  4202. * @param {jsTree} old_instance the instance the node came from
  4203. * @param {jsTree} new_instance the instance of the new parent
  4204. */
  4205. this.trigger('copy_node', { "node" : tmp, "original" : obj, "parent" : new_par.id, "position" : pos, "old_parent" : old_par, "old_position" : old_ins && old_ins._id && old_par && old_ins._model.data[old_par] && old_ins._model.data[old_par].children ? $.inArray(obj.id, old_ins._model.data[old_par].children) : -1,'is_multi' : (old_ins && old_ins._id && old_ins._id !== this._id), 'is_foreign' : (!old_ins || !old_ins._id), 'old_instance' : old_ins, 'new_instance' : this });
  4206. return tmp.id;
  4207. },
  4208. /**
  4209. * cut a node (a later call to `paste(obj)` would move the node)
  4210. * @name cut(obj)
  4211. * @param {mixed} obj multiple objects can be passed using an array
  4212. * @trigger cut.jstree
  4213. */
  4214. cut : function (obj) {
  4215. if(!obj) { obj = this._data.core.selected.concat(); }
  4216. if(!$.isArray(obj)) { obj = [obj]; }
  4217. if(!obj.length) { return false; }
  4218. var tmp = [], o, t1, t2;
  4219. for(t1 = 0, t2 = obj.length; t1 < t2; t1++) {
  4220. o = this.get_node(obj[t1]);
  4221. if(o && o.id && o.id !== $.jstree.root) { tmp.push(o); }
  4222. }
  4223. if(!tmp.length) { return false; }
  4224. ccp_node = tmp;
  4225. ccp_inst = this;
  4226. ccp_mode = 'move_node';
  4227. /**
  4228. * triggered when nodes are added to the buffer for moving
  4229. * @event
  4230. * @name cut.jstree
  4231. * @param {Array} node
  4232. */
  4233. this.trigger('cut', { "node" : obj });
  4234. },
  4235. /**
  4236. * copy a node (a later call to `paste(obj)` would copy the node)
  4237. * @name copy(obj)
  4238. * @param {mixed} obj multiple objects can be passed using an array
  4239. * @trigger copy.jstree
  4240. */
  4241. copy : function (obj) {
  4242. if(!obj) { obj = this._data.core.selected.concat(); }
  4243. if(!$.isArray(obj)) { obj = [obj]; }
  4244. if(!obj.length) { return false; }
  4245. var tmp = [], o, t1, t2;
  4246. for(t1 = 0, t2 = obj.length; t1 < t2; t1++) {
  4247. o = this.get_node(obj[t1]);
  4248. if(o && o.id && o.id !== $.jstree.root) { tmp.push(o); }
  4249. }
  4250. if(!tmp.length) { return false; }
  4251. ccp_node = tmp;
  4252. ccp_inst = this;
  4253. ccp_mode = 'copy_node';
  4254. /**
  4255. * triggered when nodes are added to the buffer for copying
  4256. * @event
  4257. * @name copy.jstree
  4258. * @param {Array} node
  4259. */
  4260. this.trigger('copy', { "node" : obj });
  4261. },
  4262. /**
  4263. * get the current buffer (any nodes that are waiting for a paste operation)
  4264. * @name get_buffer()
  4265. * @return {Object} an object consisting of `mode` ("copy_node" or "move_node"), `node` (an array of objects) and `inst` (the instance)
  4266. */
  4267. get_buffer : function () {
  4268. return { 'mode' : ccp_mode, 'node' : ccp_node, 'inst' : ccp_inst };
  4269. },
  4270. /**
  4271. * check if there is something in the buffer to paste
  4272. * @name can_paste()
  4273. * @return {Boolean}
  4274. */
  4275. can_paste : function () {
  4276. return ccp_mode !== false && ccp_node !== false; // && ccp_inst._model.data[ccp_node];
  4277. },
  4278. /**
  4279. * copy or move the previously cut or copied nodes to a new parent
  4280. * @name paste(obj [, pos])
  4281. * @param {mixed} obj the new parent
  4282. * @param {mixed} pos the position to insert at (besides integer, "first" and "last" are supported), defaults to integer `0`
  4283. * @trigger paste.jstree
  4284. */
  4285. paste : function (obj, pos) {
  4286. obj = this.get_node(obj);
  4287. if(!obj || !ccp_mode || !ccp_mode.match(/^(copy_node|move_node)$/) || !ccp_node) { return false; }
  4288. if(this[ccp_mode](ccp_node, obj, pos, false, false, false, ccp_inst)) {
  4289. /**
  4290. * triggered when paste is invoked
  4291. * @event
  4292. * @name paste.jstree
  4293. * @param {String} parent the ID of the receiving node
  4294. * @param {Array} node the nodes in the buffer
  4295. * @param {String} mode the performed operation - "copy_node" or "move_node"
  4296. */
  4297. this.trigger('paste', { "parent" : obj.id, "node" : ccp_node, "mode" : ccp_mode });
  4298. }
  4299. ccp_node = false;
  4300. ccp_mode = false;
  4301. ccp_inst = false;
  4302. },
  4303. /**
  4304. * clear the buffer of previously copied or cut nodes
  4305. * @name clear_buffer()
  4306. * @trigger clear_buffer.jstree
  4307. */
  4308. clear_buffer : function () {
  4309. ccp_node = false;
  4310. ccp_mode = false;
  4311. ccp_inst = false;
  4312. /**
  4313. * triggered when the copy / cut buffer is cleared
  4314. * @event
  4315. * @name clear_buffer.jstree
  4316. */
  4317. this.trigger('clear_buffer');
  4318. },
  4319. /**
  4320. * put a node in edit mode (input field to rename the node)
  4321. * @name edit(obj [, default_text, callback])
  4322. * @param {mixed} obj
  4323. * @param {String} default_text the text to populate the input with (if omitted or set to a non-string value the node's text value is used)
  4324. * @param {Function} callback a function to be called once the text box is blurred, it is called in the instance's scope and receives the node, a status parameter (true if the rename is successful, false otherwise) and a boolean indicating if the user cancelled the edit. You can access the node's title using .text
  4325. */
  4326. edit : function (obj, default_text, callback) {
  4327. var rtl, w, a, s, t, h1, h2, fn, tmp, cancel = false;
  4328. obj = this.get_node(obj);
  4329. if(!obj) { return false; }
  4330. if(!this.check("edit", obj, this.get_parent(obj))) {
  4331. this.settings.core.error.call(this, this._data.core.last_error);
  4332. return false;
  4333. }
  4334. tmp = obj;
  4335. default_text = typeof default_text === 'string' ? default_text : obj.text;
  4336. this.set_text(obj, "");
  4337. obj = this._open_to(obj);
  4338. tmp.text = default_text;
  4339. rtl = this._data.core.rtl;
  4340. w = this.element.width();
  4341. this._data.core.focused = tmp.id;
  4342. a = obj.children('.jstree-anchor').focus();
  4343. s = $('<span>');
  4344. /*!
  4345. oi = obj.children("i:visible"),
  4346. ai = a.children("i:visible"),
  4347. w1 = oi.width() * oi.length,
  4348. w2 = ai.width() * ai.length,
  4349. */
  4350. t = default_text;
  4351. h1 = $("<"+"div />", { css : { "position" : "absolute", "top" : "-200px", "left" : (rtl ? "0px" : "-1000px"), "visibility" : "hidden" } }).appendTo("body");
  4352. h2 = $("<"+"input />", {
  4353. "value" : t,
  4354. "class" : "jstree-rename-input",
  4355. // "size" : t.length,
  4356. "css" : {
  4357. "padding" : "0",
  4358. "border" : "1px solid silver",
  4359. "box-sizing" : "border-box",
  4360. "display" : "inline-block",
  4361. "height" : (this._data.core.li_height) + "px",
  4362. "lineHeight" : (this._data.core.li_height) + "px",
  4363. "width" : "150px" // will be set a bit further down
  4364. },
  4365. "blur" : $.proxy(function (e) {
  4366. e.stopImmediatePropagation();
  4367. e.preventDefault();
  4368. var i = s.children(".jstree-rename-input"),
  4369. v = i.val(),
  4370. f = this.settings.core.force_text,
  4371. nv;
  4372. if(v === "") { v = t; }
  4373. h1.remove();
  4374. s.replaceWith(a);
  4375. s.remove();
  4376. t = f ? t : $('<div></div>').append($.parseHTML(t)).html();
  4377. this.set_text(obj, t);
  4378. nv = !!this.rename_node(obj, f ? $('<div></div>').text(v).text() : $('<div></div>').append($.parseHTML(v)).html());
  4379. if(!nv) {
  4380. this.set_text(obj, t); // move this up? and fix #483
  4381. }
  4382. this._data.core.focused = tmp.id;
  4383. setTimeout($.proxy(function () {
  4384. var node = this.get_node(tmp.id, true);
  4385. if(node.length) {
  4386. this._data.core.focused = tmp.id;
  4387. node.children('.jstree-anchor').focus();
  4388. }
  4389. }, this), 0);
  4390. if(callback) {
  4391. callback.call(this, tmp, nv, cancel);
  4392. }
  4393. h2 = null;
  4394. }, this),
  4395. "keydown" : function (e) {
  4396. var key = e.which;
  4397. if(key === 27) {
  4398. cancel = true;
  4399. this.value = t;
  4400. }
  4401. if(key === 27 || key === 13 || key === 37 || key === 38 || key === 39 || key === 40 || key === 32) {
  4402. e.stopImmediatePropagation();
  4403. }
  4404. if(key === 27 || key === 13) {
  4405. e.preventDefault();
  4406. this.blur();
  4407. }
  4408. },
  4409. "click" : function (e) { e.stopImmediatePropagation(); },
  4410. "mousedown" : function (e) { e.stopImmediatePropagation(); },
  4411. "keyup" : function (e) {
  4412. h2.width(Math.min(h1.text("pW" + this.value).width(),w));
  4413. },
  4414. "keypress" : function(e) {
  4415. if(e.which === 13) { return false; }
  4416. }
  4417. });
  4418. fn = {
  4419. fontFamily : a.css('fontFamily') || '',
  4420. fontSize : a.css('fontSize') || '',
  4421. fontWeight : a.css('fontWeight') || '',
  4422. fontStyle : a.css('fontStyle') || '',
  4423. fontStretch : a.css('fontStretch') || '',
  4424. fontVariant : a.css('fontVariant') || '',
  4425. letterSpacing : a.css('letterSpacing') || '',
  4426. wordSpacing : a.css('wordSpacing') || ''
  4427. };
  4428. s.attr('class', a.attr('class')).append(a.contents().clone()).append(h2);
  4429. a.replaceWith(s);
  4430. h1.css(fn);
  4431. h2.css(fn).width(Math.min(h1.text("pW" + h2[0].value).width(),w))[0].select();
  4432. $(document).one('mousedown.jstree touchstart.jstree dnd_start.vakata', function (e) {
  4433. if (h2 && e.target !== h2) {
  4434. $(h2).blur();
  4435. }
  4436. });
  4437. },
  4438. /**
  4439. * changes the theme
  4440. * @name set_theme(theme_name [, theme_url])
  4441. * @param {String} theme_name the name of the new theme to apply
  4442. * @param {mixed} theme_url the location of the CSS file for this theme. Omit or set to `false` if you manually included the file. Set to `true` to autoload from the `core.themes.dir` directory.
  4443. * @trigger set_theme.jstree
  4444. */
  4445. set_theme : function (theme_name, theme_url) {
  4446. if(!theme_name) { return false; }
  4447. if(theme_url === true) {
  4448. var dir = this.settings.core.themes.dir;
  4449. if(!dir) { dir = $.jstree.path + '/themes'; }
  4450. theme_url = dir + '/' + theme_name + '/style.css';
  4451. }
  4452. if(theme_url && $.inArray(theme_url, themes_loaded) === -1) {
  4453. $('head').append('<'+'link rel="stylesheet" href="' + theme_url + '" type="text/css" />');
  4454. themes_loaded.push(theme_url);
  4455. }
  4456. if(this._data.core.themes.name) {
  4457. this.element.removeClass('jstree-' + this._data.core.themes.name);
  4458. }
  4459. this._data.core.themes.name = theme_name;
  4460. this.element.addClass('jstree-' + theme_name);
  4461. this.element[this.settings.core.themes.responsive ? 'addClass' : 'removeClass' ]('jstree-' + theme_name + '-responsive');
  4462. /**
  4463. * triggered when a theme is set
  4464. * @event
  4465. * @name set_theme.jstree
  4466. * @param {String} theme the new theme
  4467. */
  4468. this.trigger('set_theme', { 'theme' : theme_name });
  4469. },
  4470. /**
  4471. * gets the name of the currently applied theme name
  4472. * @name get_theme()
  4473. * @return {String}
  4474. */
  4475. get_theme : function () { return this._data.core.themes.name; },
  4476. /**
  4477. * changes the theme variant (if the theme has variants)
  4478. * @name set_theme_variant(variant_name)
  4479. * @param {String|Boolean} variant_name the variant to apply (if `false` is used the current variant is removed)
  4480. */
  4481. set_theme_variant : function (variant_name) {
  4482. if(this._data.core.themes.variant) {
  4483. this.element.removeClass('jstree-' + this._data.core.themes.name + '-' + this._data.core.themes.variant);
  4484. }
  4485. this._data.core.themes.variant = variant_name;
  4486. if(variant_name) {
  4487. this.element.addClass('jstree-' + this._data.core.themes.name + '-' + this._data.core.themes.variant);
  4488. }
  4489. },
  4490. /**
  4491. * gets the name of the currently applied theme variant
  4492. * @name get_theme()
  4493. * @return {String}
  4494. */
  4495. get_theme_variant : function () { return this._data.core.themes.variant; },
  4496. /**
  4497. * shows a striped background on the container (if the theme supports it)
  4498. * @name show_stripes()
  4499. */
  4500. show_stripes : function () {
  4501. this._data.core.themes.stripes = true;
  4502. this.get_container_ul().addClass("jstree-striped");
  4503. /**
  4504. * triggered when stripes are shown
  4505. * @event
  4506. * @name show_stripes.jstree
  4507. */
  4508. this.trigger('show_stripes');
  4509. },
  4510. /**
  4511. * hides the striped background on the container
  4512. * @name hide_stripes()
  4513. */
  4514. hide_stripes : function () {
  4515. this._data.core.themes.stripes = false;
  4516. this.get_container_ul().removeClass("jstree-striped");
  4517. /**
  4518. * triggered when stripes are hidden
  4519. * @event
  4520. * @name hide_stripes.jstree
  4521. */
  4522. this.trigger('hide_stripes');
  4523. },
  4524. /**
  4525. * toggles the striped background on the container
  4526. * @name toggle_stripes()
  4527. */
  4528. toggle_stripes : function () { if(this._data.core.themes.stripes) { this.hide_stripes(); } else { this.show_stripes(); } },
  4529. /**
  4530. * shows the connecting dots (if the theme supports it)
  4531. * @name show_dots()
  4532. */
  4533. show_dots : function () {
  4534. this._data.core.themes.dots = true;
  4535. this.get_container_ul().removeClass("jstree-no-dots");
  4536. /**
  4537. * triggered when dots are shown
  4538. * @event
  4539. * @name show_dots.jstree
  4540. */
  4541. this.trigger('show_dots');
  4542. },
  4543. /**
  4544. * hides the connecting dots
  4545. * @name hide_dots()
  4546. */
  4547. hide_dots : function () {
  4548. this._data.core.themes.dots = false;
  4549. this.get_container_ul().addClass("jstree-no-dots");
  4550. /**
  4551. * triggered when dots are hidden
  4552. * @event
  4553. * @name hide_dots.jstree
  4554. */
  4555. this.trigger('hide_dots');
  4556. },
  4557. /**
  4558. * toggles the connecting dots
  4559. * @name toggle_dots()
  4560. */
  4561. toggle_dots : function () { if(this._data.core.themes.dots) { this.hide_dots(); } else { this.show_dots(); } },
  4562. /**
  4563. * show the node icons
  4564. * @name show_icons()
  4565. */
  4566. show_icons : function () {
  4567. this._data.core.themes.icons = true;
  4568. this.get_container_ul().removeClass("jstree-no-icons");
  4569. /**
  4570. * triggered when icons are shown
  4571. * @event
  4572. * @name show_icons.jstree
  4573. */
  4574. this.trigger('show_icons');
  4575. },
  4576. /**
  4577. * hide the node icons
  4578. * @name hide_icons()
  4579. */
  4580. hide_icons : function () {
  4581. this._data.core.themes.icons = false;
  4582. this.get_container_ul().addClass("jstree-no-icons");
  4583. /**
  4584. * triggered when icons are hidden
  4585. * @event
  4586. * @name hide_icons.jstree
  4587. */
  4588. this.trigger('hide_icons');
  4589. },
  4590. /**
  4591. * toggle the node icons
  4592. * @name toggle_icons()
  4593. */
  4594. toggle_icons : function () { if(this._data.core.themes.icons) { this.hide_icons(); } else { this.show_icons(); } },
  4595. /**
  4596. * show the node ellipsis
  4597. * @name show_icons()
  4598. */
  4599. show_ellipsis : function () {
  4600. this._data.core.themes.ellipsis = true;
  4601. this.get_container_ul().addClass("jstree-ellipsis");
  4602. /**
  4603. * triggered when ellisis is shown
  4604. * @event
  4605. * @name show_ellipsis.jstree
  4606. */
  4607. this.trigger('show_ellipsis');
  4608. },
  4609. /**
  4610. * hide the node ellipsis
  4611. * @name hide_ellipsis()
  4612. */
  4613. hide_ellipsis : function () {
  4614. this._data.core.themes.ellipsis = false;
  4615. this.get_container_ul().removeClass("jstree-ellipsis");
  4616. /**
  4617. * triggered when ellisis is hidden
  4618. * @event
  4619. * @name hide_ellipsis.jstree
  4620. */
  4621. this.trigger('hide_ellipsis');
  4622. },
  4623. /**
  4624. * toggle the node ellipsis
  4625. * @name toggle_icons()
  4626. */
  4627. toggle_ellipsis : function () { if(this._data.core.themes.ellipsis) { this.hide_ellipsis(); } else { this.show_ellipsis(); } },
  4628. /**
  4629. * set the node icon for a node
  4630. * @name set_icon(obj, icon)
  4631. * @param {mixed} obj
  4632. * @param {String} icon the new icon - 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
  4633. */
  4634. set_icon : function (obj, icon) {
  4635. var t1, t2, dom, old;
  4636. if($.isArray(obj)) {
  4637. obj = obj.slice();
  4638. for(t1 = 0, t2 = obj.length; t1 < t2; t1++) {
  4639. this.set_icon(obj[t1], icon);
  4640. }
  4641. return true;
  4642. }
  4643. obj = this.get_node(obj);
  4644. if(!obj || obj.id === $.jstree.root) { return false; }
  4645. old = obj.icon;
  4646. obj.icon = icon === true || icon === null || icon === undefined || icon === '' ? true : icon;
  4647. dom = this.get_node(obj, true).children(".jstree-anchor").children(".jstree-themeicon");
  4648. if(icon === false) {
  4649. this.hide_icon(obj);
  4650. }
  4651. else if(icon === true || icon === null || icon === undefined || icon === '') {
  4652. dom.removeClass('jstree-themeicon-custom ' + old).css("background","").removeAttr("rel");
  4653. if(old === false) { this.show_icon(obj); }
  4654. }
  4655. else if(icon.indexOf("/") === -1 && icon.indexOf(".") === -1) {
  4656. dom.removeClass(old).css("background","");
  4657. dom.addClass(icon + ' jstree-themeicon-custom').attr("rel",icon);
  4658. if(old === false) { this.show_icon(obj); }
  4659. }
  4660. else {
  4661. dom.removeClass(old).css("background","");
  4662. dom.addClass('jstree-themeicon-custom').css("background", "url('" + icon + "') center center no-repeat").attr("rel",icon);
  4663. if(old === false) { this.show_icon(obj); }
  4664. }
  4665. return true;
  4666. },
  4667. /**
  4668. * get the node icon for a node
  4669. * @name get_icon(obj)
  4670. * @param {mixed} obj
  4671. * @return {String}
  4672. */
  4673. get_icon : function (obj) {
  4674. obj = this.get_node(obj);
  4675. return (!obj || obj.id === $.jstree.root) ? false : obj.icon;
  4676. },
  4677. /**
  4678. * hide the icon on an individual node
  4679. * @name hide_icon(obj)
  4680. * @param {mixed} obj
  4681. */
  4682. hide_icon : function (obj) {
  4683. var t1, t2;
  4684. if($.isArray(obj)) {
  4685. obj = obj.slice();
  4686. for(t1 = 0, t2 = obj.length; t1 < t2; t1++) {
  4687. this.hide_icon(obj[t1]);
  4688. }
  4689. return true;
  4690. }
  4691. obj = this.get_node(obj);
  4692. if(!obj || obj === $.jstree.root) { return false; }
  4693. obj.icon = false;
  4694. this.get_node(obj, true).children(".jstree-anchor").children(".jstree-themeicon").addClass('jstree-themeicon-hidden');
  4695. return true;
  4696. },
  4697. /**
  4698. * show the icon on an individual node
  4699. * @name show_icon(obj)
  4700. * @param {mixed} obj
  4701. */
  4702. show_icon : function (obj) {
  4703. var t1, t2, dom;
  4704. if($.isArray(obj)) {
  4705. obj = obj.slice();
  4706. for(t1 = 0, t2 = obj.length; t1 < t2; t1++) {
  4707. this.show_icon(obj[t1]);
  4708. }
  4709. return true;
  4710. }
  4711. obj = this.get_node(obj);
  4712. if(!obj || obj === $.jstree.root) { return false; }
  4713. dom = this.get_node(obj, true);
  4714. obj.icon = dom.length ? dom.children(".jstree-anchor").children(".jstree-themeicon").attr('rel') : true;
  4715. if(!obj.icon) { obj.icon = true; }
  4716. dom.children(".jstree-anchor").children(".jstree-themeicon").removeClass('jstree-themeicon-hidden');
  4717. return true;
  4718. }
  4719. };
  4720. // helpers
  4721. $.vakata = {};
  4722. // collect attributes
  4723. $.vakata.attributes = function(node, with_values) {
  4724. node = $(node)[0];
  4725. var attr = with_values ? {} : [];
  4726. if(node && node.attributes) {
  4727. $.each(node.attributes, function (i, v) {
  4728. if($.inArray(v.name.toLowerCase(),['style','contenteditable','hasfocus','tabindex']) !== -1) { return; }
  4729. if(v.value !== null && $.trim(v.value) !== '') {
  4730. if(with_values) { attr[v.name] = v.value; }
  4731. else { attr.push(v.name); }
  4732. }
  4733. });
  4734. }
  4735. return attr;
  4736. };
  4737. $.vakata.array_unique = function(array) {
  4738. var a = [], i, j, l, o = {};
  4739. for(i = 0, l = array.length; i < l; i++) {
  4740. if(o[array[i]] === undefined) {
  4741. a.push(array[i]);
  4742. o[array[i]] = true;
  4743. }
  4744. }
  4745. return a;
  4746. };
  4747. // remove item from array
  4748. $.vakata.array_remove = function(array, from) {
  4749. array.splice(from, 1);
  4750. return array;
  4751. //var rest = array.slice((to || from) + 1 || array.length);
  4752. //array.length = from < 0 ? array.length + from : from;
  4753. //array.push.apply(array, rest);
  4754. //return array;
  4755. };
  4756. // remove item from array
  4757. $.vakata.array_remove_item = function(array, item) {
  4758. var tmp = $.inArray(item, array);
  4759. return tmp !== -1 ? $.vakata.array_remove(array, tmp) : array;
  4760. };
  4761. $.vakata.array_filter = function(c,a,b,d,e) {
  4762. if (c.filter) {
  4763. return c.filter(a, b);
  4764. }
  4765. d=[];
  4766. for (e in c) {
  4767. if (~~e+''===e+'' && e>=0 && a.call(b,c[e],+e,c)) {
  4768. d.push(c[e]);
  4769. }
  4770. }
  4771. return d;
  4772. };
  4773. /**
  4774. * ### Changed plugin
  4775. *
  4776. * This plugin adds more information to the `changed.jstree` event. The new data is contained in the `changed` event data property, and contains a lists of `selected` and `deselected` nodes.
  4777. */
  4778. $.jstree.plugins.changed = function (options, parent) {
  4779. var last = [];
  4780. this.trigger = function (ev, data) {
  4781. var i, j;
  4782. if(!data) {
  4783. data = {};
  4784. }
  4785. if(ev.replace('.jstree','') === 'changed') {
  4786. data.changed = { selected : [], deselected : [] };
  4787. var tmp = {};
  4788. for(i = 0, j = last.length; i < j; i++) {
  4789. tmp[last[i]] = 1;
  4790. }
  4791. for(i = 0, j = data.selected.length; i < j; i++) {
  4792. if(!tmp[data.selected[i]]) {
  4793. data.changed.selected.push(data.selected[i]);
  4794. }
  4795. else {
  4796. tmp[data.selected[i]] = 2;
  4797. }
  4798. }
  4799. for(i = 0, j = last.length; i < j; i++) {
  4800. if(tmp[last[i]] === 1) {
  4801. data.changed.deselected.push(last[i]);
  4802. }
  4803. }
  4804. last = data.selected.slice();
  4805. }
  4806. /**
  4807. * triggered when selection changes (the "changed" plugin enhances the original event with more data)
  4808. * @event
  4809. * @name changed.jstree
  4810. * @param {Object} node
  4811. * @param {Object} action the action that caused the selection to change
  4812. * @param {Array} selected the current selection
  4813. * @param {Object} changed an object containing two properties `selected` and `deselected` - both arrays of node IDs, which were selected or deselected since the last changed event
  4814. * @param {Object} event the event (if any) that triggered this changed event
  4815. * @plugin changed
  4816. */
  4817. parent.trigger.call(this, ev, data);
  4818. };
  4819. this.refresh = function (skip_loading, forget_state) {
  4820. last = [];
  4821. return parent.refresh.apply(this, arguments);
  4822. };
  4823. };
  4824. /**
  4825. * ### Checkbox plugin
  4826. *
  4827. * This plugin renders checkbox icons in front of each node, making multiple selection much easier.
  4828. * It also supports tri-state behavior, meaning that if a node has a few of its children checked it will be rendered as undetermined, and state will be propagated up.
  4829. */
  4830. var _i = document.createElement('I');
  4831. _i.className = 'jstree-icon jstree-checkbox';
  4832. _i.setAttribute('role', 'presentation');
  4833. /**
  4834. * stores all defaults for the checkbox plugin
  4835. * @name $.jstree.defaults.checkbox
  4836. * @plugin checkbox
  4837. */
  4838. $.jstree.defaults.checkbox = {
  4839. /**
  4840. * a boolean indicating if checkboxes should be visible (can be changed at a later time using `show_checkboxes()` and `hide_checkboxes`). Defaults to `true`.
  4841. * @name $.jstree.defaults.checkbox.visible
  4842. * @plugin checkbox
  4843. */
  4844. visible : true,
  4845. /**
  4846. * a boolean indicating if checkboxes should cascade down and have an undetermined state. Defaults to `true`.
  4847. * @name $.jstree.defaults.checkbox.three_state
  4848. * @plugin checkbox
  4849. */
  4850. three_state : true,
  4851. /**
  4852. * a boolean indicating if clicking anywhere on the node should act as clicking on the checkbox. Defaults to `true`.
  4853. * @name $.jstree.defaults.checkbox.whole_node
  4854. * @plugin checkbox
  4855. */
  4856. whole_node : true,
  4857. /**
  4858. * a boolean indicating if the selected style of a node should be kept, or removed. Defaults to `true`.
  4859. * @name $.jstree.defaults.checkbox.keep_selected_style
  4860. * @plugin checkbox
  4861. */
  4862. keep_selected_style : true,
  4863. /**
  4864. * This setting controls how cascading and undetermined nodes are applied.
  4865. * If 'up' is in the string - cascading up is enabled, if 'down' is in the string - cascading down is enabled, if 'undetermined' is in the string - undetermined nodes will be used.
  4866. * If `three_state` is set to `true` this setting is automatically set to 'up+down+undetermined'. Defaults to ''.
  4867. * @name $.jstree.defaults.checkbox.cascade
  4868. * @plugin checkbox
  4869. */
  4870. cascade : '',
  4871. /**
  4872. * This setting controls if checkbox are bound to the general tree selection or to an internal array maintained by the checkbox plugin. Defaults to `true`, only set to `false` if you know exactly what you are doing.
  4873. * @name $.jstree.defaults.checkbox.tie_selection
  4874. * @plugin checkbox
  4875. */
  4876. tie_selection : true,
  4877. /**
  4878. * This setting controls if cascading down affects disabled checkboxes
  4879. * @name $.jstree.defaults.checkbox.cascade_to_disabled
  4880. * @plugin checkbox
  4881. */
  4882. cascade_to_disabled : true,
  4883. /**
  4884. * This setting controls if cascading down affects hidden checkboxes
  4885. * @name $.jstree.defaults.checkbox.cascade_to_hidden
  4886. * @plugin checkbox
  4887. */
  4888. cascade_to_hidden : true
  4889. };
  4890. $.jstree.plugins.checkbox = function (options, parent) {
  4891. this.bind = function () {
  4892. parent.bind.call(this);
  4893. this._data.checkbox.uto = false;
  4894. this._data.checkbox.selected = [];
  4895. if(this.settings.checkbox.three_state) {
  4896. this.settings.checkbox.cascade = 'up+down+undetermined';
  4897. }
  4898. this.element
  4899. .on("init.jstree", $.proxy(function () {
  4900. this._data.checkbox.visible = this.settings.checkbox.visible;
  4901. if(!this.settings.checkbox.keep_selected_style) {
  4902. this.element.addClass('jstree-checkbox-no-clicked');
  4903. }
  4904. if(this.settings.checkbox.tie_selection) {
  4905. this.element.addClass('jstree-checkbox-selection');
  4906. }
  4907. }, this))
  4908. .on("loading.jstree", $.proxy(function () {
  4909. this[ this._data.checkbox.visible ? 'show_checkboxes' : 'hide_checkboxes' ]();
  4910. }, this));
  4911. if(this.settings.checkbox.cascade.indexOf('undetermined') !== -1) {
  4912. this.element
  4913. .on('changed.jstree uncheck_node.jstree check_node.jstree uncheck_all.jstree check_all.jstree move_node.jstree copy_node.jstree redraw.jstree open_node.jstree', $.proxy(function () {
  4914. // only if undetermined is in setting
  4915. if(this._data.checkbox.uto) { clearTimeout(this._data.checkbox.uto); }
  4916. this._data.checkbox.uto = setTimeout($.proxy(this._undetermined, this), 50);
  4917. }, this));
  4918. }
  4919. if(!this.settings.checkbox.tie_selection) {
  4920. this.element
  4921. .on('model.jstree', $.proxy(function (e, data) {
  4922. var m = this._model.data,
  4923. p = m[data.parent],
  4924. dpc = data.nodes,
  4925. i, j;
  4926. for(i = 0, j = dpc.length; i < j; i++) {
  4927. m[dpc[i]].state.checked = m[dpc[i]].state.checked || (m[dpc[i]].original && m[dpc[i]].original.state && m[dpc[i]].original.state.checked);
  4928. if(m[dpc[i]].state.checked) {
  4929. this._data.checkbox.selected.push(dpc[i]);
  4930. }
  4931. }
  4932. }, this));
  4933. }
  4934. if(this.settings.checkbox.cascade.indexOf('up') !== -1 || this.settings.checkbox.cascade.indexOf('down') !== -1) {
  4935. this.element
  4936. .on('model.jstree', $.proxy(function (e, data) {
  4937. var m = this._model.data,
  4938. p = m[data.parent],
  4939. dpc = data.nodes,
  4940. chd = [],
  4941. c, i, j, k, l, tmp, s = this.settings.checkbox.cascade, t = this.settings.checkbox.tie_selection;
  4942. if(s.indexOf('down') !== -1) {
  4943. // apply down
  4944. if(p.state[ t ? 'selected' : 'checked' ]) {
  4945. for(i = 0, j = dpc.length; i < j; i++) {
  4946. m[dpc[i]].state[ t ? 'selected' : 'checked' ] = true;
  4947. }
  4948. this._data[ t ? 'core' : 'checkbox' ].selected = this._data[ t ? 'core' : 'checkbox' ].selected.concat(dpc);
  4949. }
  4950. else {
  4951. for(i = 0, j = dpc.length; i < j; i++) {
  4952. if(m[dpc[i]].state[ t ? 'selected' : 'checked' ]) {
  4953. for(k = 0, l = m[dpc[i]].children_d.length; k < l; k++) {
  4954. m[m[dpc[i]].children_d[k]].state[ t ? 'selected' : 'checked' ] = true;
  4955. }
  4956. this._data[ t ? 'core' : 'checkbox' ].selected = this._data[ t ? 'core' : 'checkbox' ].selected.concat(m[dpc[i]].children_d);
  4957. }
  4958. }
  4959. }
  4960. }
  4961. if(s.indexOf('up') !== -1) {
  4962. // apply up
  4963. for(i = 0, j = p.children_d.length; i < j; i++) {
  4964. if(!m[p.children_d[i]].children.length) {
  4965. chd.push(m[p.children_d[i]].parent);
  4966. }
  4967. }
  4968. chd = $.vakata.array_unique(chd);
  4969. for(k = 0, l = chd.length; k < l; k++) {
  4970. p = m[chd[k]];
  4971. while(p && p.id !== $.jstree.root) {
  4972. c = 0;
  4973. for(i = 0, j = p.children.length; i < j; i++) {
  4974. c += m[p.children[i]].state[ t ? 'selected' : 'checked' ];
  4975. }
  4976. if(c === j) {
  4977. p.state[ t ? 'selected' : 'checked' ] = true;
  4978. this._data[ t ? 'core' : 'checkbox' ].selected.push(p.id);
  4979. tmp = this.get_node(p, true);
  4980. if(tmp && tmp.length) {
  4981. tmp.attr('aria-selected', true).children('.jstree-anchor').addClass( t ? 'jstree-clicked' : 'jstree-checked');
  4982. }
  4983. }
  4984. else {
  4985. break;
  4986. }
  4987. p = this.get_node(p.parent);
  4988. }
  4989. }
  4990. }
  4991. this._data[ t ? 'core' : 'checkbox' ].selected = $.vakata.array_unique(this._data[ t ? 'core' : 'checkbox' ].selected);
  4992. }, this))
  4993. .on(this.settings.checkbox.tie_selection ? 'select_node.jstree' : 'check_node.jstree', $.proxy(function (e, data) {
  4994. var self = this,
  4995. obj = data.node,
  4996. m = this._model.data,
  4997. par = this.get_node(obj.parent),
  4998. i, j, c, tmp, s = this.settings.checkbox.cascade, t = this.settings.checkbox.tie_selection,
  4999. sel = {}, cur = this._data[ t ? 'core' : 'checkbox' ].selected;
  5000. for (i = 0, j = cur.length; i < j; i++) {
  5001. sel[cur[i]] = true;
  5002. }
  5003. // apply down
  5004. if(s.indexOf('down') !== -1) {
  5005. //this._data[ t ? 'core' : 'checkbox' ].selected = $.vakata.array_unique(this._data[ t ? 'core' : 'checkbox' ].selected.concat(obj.children_d));
  5006. var selectedIds = this._cascade_new_checked_state(obj.id, true);
  5007. obj.children_d.concat(obj.id).forEach(function(id) {
  5008. if (selectedIds.indexOf(id) > -1) {
  5009. sel[id] = true;
  5010. }
  5011. else {
  5012. delete sel[id];
  5013. }
  5014. });
  5015. }
  5016. // apply up
  5017. if(s.indexOf('up') !== -1) {
  5018. while(par && par.id !== $.jstree.root) {
  5019. c = 0;
  5020. for(i = 0, j = par.children.length; i < j; i++) {
  5021. c += m[par.children[i]].state[ t ? 'selected' : 'checked' ];
  5022. }
  5023. if(c === j) {
  5024. par.state[ t ? 'selected' : 'checked' ] = true;
  5025. sel[par.id] = true;
  5026. //this._data[ t ? 'core' : 'checkbox' ].selected.push(par.id);
  5027. tmp = this.get_node(par, true);
  5028. if(tmp && tmp.length) {
  5029. tmp.attr('aria-selected', true).children('.jstree-anchor').addClass(t ? 'jstree-clicked' : 'jstree-checked');
  5030. }
  5031. }
  5032. else {
  5033. break;
  5034. }
  5035. par = this.get_node(par.parent);
  5036. }
  5037. }
  5038. cur = [];
  5039. for (i in sel) {
  5040. if (sel.hasOwnProperty(i)) {
  5041. cur.push(i);
  5042. }
  5043. }
  5044. this._data[ t ? 'core' : 'checkbox' ].selected = cur;
  5045. }, this))
  5046. .on(this.settings.checkbox.tie_selection ? 'deselect_all.jstree' : 'uncheck_all.jstree', $.proxy(function (e, data) {
  5047. var obj = this.get_node($.jstree.root),
  5048. m = this._model.data,
  5049. i, j, tmp;
  5050. for(i = 0, j = obj.children_d.length; i < j; i++) {
  5051. tmp = m[obj.children_d[i]];
  5052. if(tmp && tmp.original && tmp.original.state && tmp.original.state.undetermined) {
  5053. tmp.original.state.undetermined = false;
  5054. }
  5055. }
  5056. }, this))
  5057. .on(this.settings.checkbox.tie_selection ? 'deselect_node.jstree' : 'uncheck_node.jstree', $.proxy(function (e, data) {
  5058. var self = this,
  5059. obj = data.node,
  5060. dom = this.get_node(obj, true),
  5061. i, j, tmp, s = this.settings.checkbox.cascade, t = this.settings.checkbox.tie_selection,
  5062. cur = this._data[ t ? 'core' : 'checkbox' ].selected, sel = {},
  5063. stillSelectedIds = [],
  5064. allIds = obj.children_d.concat(obj.id);
  5065. // apply down
  5066. if(s.indexOf('down') !== -1) {
  5067. var selectedIds = this._cascade_new_checked_state(obj.id, false);
  5068. cur = cur.filter(function(id) {
  5069. return allIds.indexOf(id) === -1 || selectedIds.indexOf(id) > -1;
  5070. });
  5071. }
  5072. // only apply up if cascade up is enabled and if this node is not selected
  5073. // (if all child nodes are disabled and cascade_to_disabled === false then this node will till be selected).
  5074. if(s.indexOf('up') !== -1 && cur.indexOf(obj.id) === -1) {
  5075. for(i = 0, j = obj.parents.length; i < j; i++) {
  5076. tmp = this._model.data[obj.parents[i]];
  5077. tmp.state[ t ? 'selected' : 'checked' ] = false;
  5078. if(tmp && tmp.original && tmp.original.state && tmp.original.state.undetermined) {
  5079. tmp.original.state.undetermined = false;
  5080. }
  5081. tmp = this.get_node(obj.parents[i], true);
  5082. if(tmp && tmp.length) {
  5083. tmp.attr('aria-selected', false).children('.jstree-anchor').removeClass(t ? 'jstree-clicked' : 'jstree-checked');
  5084. }
  5085. }
  5086. cur = cur.filter(function(id) {
  5087. return obj.parents.indexOf(id) === -1;
  5088. });
  5089. }
  5090. this._data[ t ? 'core' : 'checkbox' ].selected = cur;
  5091. }, this));
  5092. }
  5093. if(this.settings.checkbox.cascade.indexOf('up') !== -1) {
  5094. this.element
  5095. .on('delete_node.jstree', $.proxy(function (e, data) {
  5096. // apply up (whole handler)
  5097. var p = this.get_node(data.parent),
  5098. m = this._model.data,
  5099. i, j, c, tmp, t = this.settings.checkbox.tie_selection;
  5100. while(p && p.id !== $.jstree.root && !p.state[ t ? 'selected' : 'checked' ]) {
  5101. c = 0;
  5102. for(i = 0, j = p.children.length; i < j; i++) {
  5103. c += m[p.children[i]].state[ t ? 'selected' : 'checked' ];
  5104. }
  5105. if(j > 0 && c === j) {
  5106. p.state[ t ? 'selected' : 'checked' ] = true;
  5107. this._data[ t ? 'core' : 'checkbox' ].selected.push(p.id);
  5108. tmp = this.get_node(p, true);
  5109. if(tmp && tmp.length) {
  5110. tmp.attr('aria-selected', true).children('.jstree-anchor').addClass(t ? 'jstree-clicked' : 'jstree-checked');
  5111. }
  5112. }
  5113. else {
  5114. break;
  5115. }
  5116. p = this.get_node(p.parent);
  5117. }
  5118. }, this))
  5119. .on('move_node.jstree', $.proxy(function (e, data) {
  5120. // apply up (whole handler)
  5121. var is_multi = data.is_multi,
  5122. old_par = data.old_parent,
  5123. new_par = this.get_node(data.parent),
  5124. m = this._model.data,
  5125. p, c, i, j, tmp, t = this.settings.checkbox.tie_selection;
  5126. if(!is_multi) {
  5127. p = this.get_node(old_par);
  5128. while(p && p.id !== $.jstree.root && !p.state[ t ? 'selected' : 'checked' ]) {
  5129. c = 0;
  5130. for(i = 0, j = p.children.length; i < j; i++) {
  5131. c += m[p.children[i]].state[ t ? 'selected' : 'checked' ];
  5132. }
  5133. if(j > 0 && c === j) {
  5134. p.state[ t ? 'selected' : 'checked' ] = true;
  5135. this._data[ t ? 'core' : 'checkbox' ].selected.push(p.id);
  5136. tmp = this.get_node(p, true);
  5137. if(tmp && tmp.length) {
  5138. tmp.attr('aria-selected', true).children('.jstree-anchor').addClass(t ? 'jstree-clicked' : 'jstree-checked');
  5139. }
  5140. }
  5141. else {
  5142. break;
  5143. }
  5144. p = this.get_node(p.parent);
  5145. }
  5146. }
  5147. p = new_par;
  5148. while(p && p.id !== $.jstree.root) {
  5149. c = 0;
  5150. for(i = 0, j = p.children.length; i < j; i++) {
  5151. c += m[p.children[i]].state[ t ? 'selected' : 'checked' ];
  5152. }
  5153. if(c === j) {
  5154. if(!p.state[ t ? 'selected' : 'checked' ]) {
  5155. p.state[ t ? 'selected' : 'checked' ] = true;
  5156. this._data[ t ? 'core' : 'checkbox' ].selected.push(p.id);
  5157. tmp = this.get_node(p, true);
  5158. if(tmp && tmp.length) {
  5159. tmp.attr('aria-selected', true).children('.jstree-anchor').addClass(t ? 'jstree-clicked' : 'jstree-checked');
  5160. }
  5161. }
  5162. }
  5163. else {
  5164. if(p.state[ t ? 'selected' : 'checked' ]) {
  5165. p.state[ t ? 'selected' : 'checked' ] = false;
  5166. this._data[ t ? 'core' : 'checkbox' ].selected = $.vakata.array_remove_item(this._data[ t ? 'core' : 'checkbox' ].selected, p.id);
  5167. tmp = this.get_node(p, true);
  5168. if(tmp && tmp.length) {
  5169. tmp.attr('aria-selected', false).children('.jstree-anchor').removeClass(t ? 'jstree-clicked' : 'jstree-checked');
  5170. }
  5171. }
  5172. else {
  5173. break;
  5174. }
  5175. }
  5176. p = this.get_node(p.parent);
  5177. }
  5178. }, this));
  5179. }
  5180. };
  5181. /**
  5182. * set the undetermined state where and if necessary. Used internally.
  5183. * @private
  5184. * @name _undetermined()
  5185. * @plugin checkbox
  5186. */
  5187. this._undetermined = function () {
  5188. if(this.element === null) { return; }
  5189. var i, j, k, l, o = {}, m = this._model.data, t = this.settings.checkbox.tie_selection, s = this._data[ t ? 'core' : 'checkbox' ].selected, p = [], tt = this;
  5190. for(i = 0, j = s.length; i < j; i++) {
  5191. if(m[s[i]] && m[s[i]].parents) {
  5192. for(k = 0, l = m[s[i]].parents.length; k < l; k++) {
  5193. if(o[m[s[i]].parents[k]] !== undefined) {
  5194. break;
  5195. }
  5196. if(m[s[i]].parents[k] !== $.jstree.root) {
  5197. o[m[s[i]].parents[k]] = true;
  5198. p.push(m[s[i]].parents[k]);
  5199. }
  5200. }
  5201. }
  5202. }
  5203. // attempt for server side undetermined state
  5204. this.element.find('.jstree-closed').not(':has(.jstree-children)')
  5205. .each(function () {
  5206. var tmp = tt.get_node(this), tmp2;
  5207. if(!tmp) { return; }
  5208. if(!tmp.state.loaded) {
  5209. if(tmp.original && tmp.original.state && tmp.original.state.undetermined && tmp.original.state.undetermined === true) {
  5210. if(o[tmp.id] === undefined && tmp.id !== $.jstree.root) {
  5211. o[tmp.id] = true;
  5212. p.push(tmp.id);
  5213. }
  5214. for(k = 0, l = tmp.parents.length; k < l; k++) {
  5215. if(o[tmp.parents[k]] === undefined && tmp.parents[k] !== $.jstree.root) {
  5216. o[tmp.parents[k]] = true;
  5217. p.push(tmp.parents[k]);
  5218. }
  5219. }
  5220. }
  5221. }
  5222. else {
  5223. for(i = 0, j = tmp.children_d.length; i < j; i++) {
  5224. tmp2 = m[tmp.children_d[i]];
  5225. if(!tmp2.state.loaded && tmp2.original && tmp2.original.state && tmp2.original.state.undetermined && tmp2.original.state.undetermined === true) {
  5226. if(o[tmp2.id] === undefined && tmp2.id !== $.jstree.root) {
  5227. o[tmp2.id] = true;
  5228. p.push(tmp2.id);
  5229. }
  5230. for(k = 0, l = tmp2.parents.length; k < l; k++) {
  5231. if(o[tmp2.parents[k]] === undefined && tmp2.parents[k] !== $.jstree.root) {
  5232. o[tmp2.parents[k]] = true;
  5233. p.push(tmp2.parents[k]);
  5234. }
  5235. }
  5236. }
  5237. }
  5238. }
  5239. });
  5240. this.element.find('.jstree-undetermined').removeClass('jstree-undetermined');
  5241. for(i = 0, j = p.length; i < j; i++) {
  5242. if(!m[p[i]].state[ t ? 'selected' : 'checked' ]) {
  5243. s = this.get_node(p[i], true);
  5244. if(s && s.length) {
  5245. s.children('.jstree-anchor').children('.jstree-checkbox').addClass('jstree-undetermined');
  5246. }
  5247. }
  5248. }
  5249. };
  5250. this.redraw_node = function(obj, deep, is_callback, force_render) {
  5251. obj = parent.redraw_node.apply(this, arguments);
  5252. if(obj) {
  5253. var i, j, tmp = null, icon = null;
  5254. for(i = 0, j = obj.childNodes.length; i < j; i++) {
  5255. if(obj.childNodes[i] && obj.childNodes[i].className && obj.childNodes[i].className.indexOf("jstree-anchor") !== -1) {
  5256. tmp = obj.childNodes[i];
  5257. break;
  5258. }
  5259. }
  5260. if(tmp) {
  5261. if(!this.settings.checkbox.tie_selection && this._model.data[obj.id].state.checked) { tmp.className += ' jstree-checked'; }
  5262. icon = _i.cloneNode(false);
  5263. if(this._model.data[obj.id].state.checkbox_disabled) { icon.className += ' jstree-checkbox-disabled'; }
  5264. tmp.insertBefore(icon, tmp.childNodes[0]);
  5265. }
  5266. }
  5267. if(!is_callback && this.settings.checkbox.cascade.indexOf('undetermined') !== -1) {
  5268. if(this._data.checkbox.uto) { clearTimeout(this._data.checkbox.uto); }
  5269. this._data.checkbox.uto = setTimeout($.proxy(this._undetermined, this), 50);
  5270. }
  5271. return obj;
  5272. };
  5273. /**
  5274. * show the node checkbox icons
  5275. * @name show_checkboxes()
  5276. * @plugin checkbox
  5277. */
  5278. this.show_checkboxes = function () { this._data.core.themes.checkboxes = true; this.get_container_ul().removeClass("jstree-no-checkboxes"); };
  5279. /**
  5280. * hide the node checkbox icons
  5281. * @name hide_checkboxes()
  5282. * @plugin checkbox
  5283. */
  5284. this.hide_checkboxes = function () { this._data.core.themes.checkboxes = false; this.get_container_ul().addClass("jstree-no-checkboxes"); };
  5285. /**
  5286. * toggle the node icons
  5287. * @name toggle_checkboxes()
  5288. * @plugin checkbox
  5289. */
  5290. this.toggle_checkboxes = function () { if(this._data.core.themes.checkboxes) { this.hide_checkboxes(); } else { this.show_checkboxes(); } };
  5291. /**
  5292. * checks if a node is in an undetermined state
  5293. * @name is_undetermined(obj)
  5294. * @param {mixed} obj
  5295. * @return {Boolean}
  5296. */
  5297. this.is_undetermined = function (obj) {
  5298. obj = this.get_node(obj);
  5299. var s = this.settings.checkbox.cascade, i, j, t = this.settings.checkbox.tie_selection, d = this._data[ t ? 'core' : 'checkbox' ].selected, m = this._model.data;
  5300. if(!obj || obj.state[ t ? 'selected' : 'checked' ] === true || s.indexOf('undetermined') === -1 || (s.indexOf('down') === -1 && s.indexOf('up') === -1)) {
  5301. return false;
  5302. }
  5303. if(!obj.state.loaded && obj.original.state.undetermined === true) {
  5304. return true;
  5305. }
  5306. for(i = 0, j = obj.children_d.length; i < j; i++) {
  5307. if($.inArray(obj.children_d[i], d) !== -1 || (!m[obj.children_d[i]].state.loaded && m[obj.children_d[i]].original.state.undetermined)) {
  5308. return true;
  5309. }
  5310. }
  5311. return false;
  5312. };
  5313. /**
  5314. * disable a node's checkbox
  5315. * @name disable_checkbox(obj)
  5316. * @param {mixed} obj an array can be used too
  5317. * @trigger disable_checkbox.jstree
  5318. * @plugin checkbox
  5319. */
  5320. this.disable_checkbox = function (obj) {
  5321. var t1, t2, dom;
  5322. if($.isArray(obj)) {
  5323. obj = obj.slice();
  5324. for(t1 = 0, t2 = obj.length; t1 < t2; t1++) {
  5325. this.disable_checkbox(obj[t1]);
  5326. }
  5327. return true;
  5328. }
  5329. obj = this.get_node(obj);
  5330. if(!obj || obj.id === $.jstree.root) {
  5331. return false;
  5332. }
  5333. dom = this.get_node(obj, true);
  5334. if(!obj.state.checkbox_disabled) {
  5335. obj.state.checkbox_disabled = true;
  5336. if(dom && dom.length) {
  5337. dom.children('.jstree-anchor').children('.jstree-checkbox').addClass('jstree-checkbox-disabled');
  5338. }
  5339. /**
  5340. * triggered when an node's checkbox is disabled
  5341. * @event
  5342. * @name disable_checkbox.jstree
  5343. * @param {Object} node
  5344. * @plugin checkbox
  5345. */
  5346. this.trigger('disable_checkbox', { 'node' : obj });
  5347. }
  5348. };
  5349. /**
  5350. * enable a node's checkbox
  5351. * @name disable_checkbox(obj)
  5352. * @param {mixed} obj an array can be used too
  5353. * @trigger enable_checkbox.jstree
  5354. * @plugin checkbox
  5355. */
  5356. this.enable_checkbox = function (obj) {
  5357. var t1, t2, dom;
  5358. if($.isArray(obj)) {
  5359. obj = obj.slice();
  5360. for(t1 = 0, t2 = obj.length; t1 < t2; t1++) {
  5361. this.enable_checkbox(obj[t1]);
  5362. }
  5363. return true;
  5364. }
  5365. obj = this.get_node(obj);
  5366. if(!obj || obj.id === $.jstree.root) {
  5367. return false;
  5368. }
  5369. dom = this.get_node(obj, true);
  5370. if(obj.state.checkbox_disabled) {
  5371. obj.state.checkbox_disabled = false;
  5372. if(dom && dom.length) {
  5373. dom.children('.jstree-anchor').children('.jstree-checkbox').removeClass('jstree-checkbox-disabled');
  5374. }
  5375. /**
  5376. * triggered when an node's checkbox is enabled
  5377. * @event
  5378. * @name enable_checkbox.jstree
  5379. * @param {Object} node
  5380. * @plugin checkbox
  5381. */
  5382. this.trigger('enable_checkbox', { 'node' : obj });
  5383. }
  5384. };
  5385. this.activate_node = function (obj, e) {
  5386. if($(e.target).hasClass('jstree-checkbox-disabled')) {
  5387. return false;
  5388. }
  5389. if(this.settings.checkbox.tie_selection && (this.settings.checkbox.whole_node || $(e.target).hasClass('jstree-checkbox'))) {
  5390. e.ctrlKey = true;
  5391. }
  5392. if(this.settings.checkbox.tie_selection || (!this.settings.checkbox.whole_node && !$(e.target).hasClass('jstree-checkbox'))) {
  5393. return parent.activate_node.call(this, obj, e);
  5394. }
  5395. if(this.is_disabled(obj)) {
  5396. return false;
  5397. }
  5398. if(this.is_checked(obj)) {
  5399. this.uncheck_node(obj, e);
  5400. }
  5401. else {
  5402. this.check_node(obj, e);
  5403. }
  5404. this.trigger('activate_node', { 'node' : this.get_node(obj) });
  5405. };
  5406. /**
  5407. * Unchecks a node and all its descendants. This function does NOT affect hidden and disabled nodes (or their descendants).
  5408. * However if these unaffected nodes are already selected their ids will be included in the returned array.
  5409. * @param id
  5410. * @param checkedState
  5411. * @returns {Array} Array of all node id's (in this tree branch) that are checked.
  5412. */
  5413. this._cascade_new_checked_state = function(id, checkedState) {
  5414. var self = this;
  5415. var t = this.settings.checkbox.tie_selection;
  5416. var node = this._model.data[id];
  5417. var selectedNodeIds = [];
  5418. var selectedChildrenIds = [];
  5419. if (
  5420. (this.settings.checkbox.cascade_to_disabled || !node.state.disabled) &&
  5421. (this.settings.checkbox.cascade_to_hidden || !node.state.hidden)
  5422. ) {
  5423. //First try and check/uncheck the children
  5424. if (node.children) {
  5425. node.children.forEach(function(childId) {
  5426. var selectedChildIds = self._cascade_new_checked_state(childId, checkedState);
  5427. selectedNodeIds = selectedNodeIds.concat(selectedChildIds);
  5428. if (selectedChildIds.indexOf(childId) > -1) {
  5429. selectedChildrenIds.push(childId);
  5430. }
  5431. });
  5432. }
  5433. var dom = self.get_node(node, true);
  5434. //A node's state is undetermined if some but not all of it's children are checked/selected .
  5435. var undetermined = selectedChildrenIds.length > 0 && selectedChildrenIds.length < node.children.length;
  5436. if(node.original && node.original.state && node.original.state.undetermined) {
  5437. node.original.state.undetermined = undetermined;
  5438. }
  5439. //If a node is undetermined then remove selected class
  5440. if (undetermined) {
  5441. node.state[ t ? 'selected' : 'checked' ] = false;
  5442. dom.attr('aria-selected', false).children('.jstree-anchor').removeClass(t ? 'jstree-clicked' : 'jstree-checked');
  5443. }
  5444. //Otherwise, if the checkedState === true (i.e. the node is being checked now) and all of the node's children are checked (if it has any children),
  5445. //check the node and style it correctly.
  5446. else if (checkedState && selectedChildrenIds.length === node.children.length) {
  5447. node.state[ t ? 'selected' : 'checked' ] = checkedState;
  5448. selectedNodeIds.push(node.id);
  5449. dom.attr('aria-selected', true).children('.jstree-anchor').addClass(t ? 'jstree-clicked' : 'jstree-checked');
  5450. }
  5451. else {
  5452. node.state[ t ? 'selected' : 'checked' ] = false;
  5453. dom.attr('aria-selected', false).children('.jstree-anchor').removeClass(t ? 'jstree-clicked' : 'jstree-checked');
  5454. }
  5455. }
  5456. else {
  5457. var selectedChildIds = this.get_checked_descendants(id);
  5458. if (node.state[ t ? 'selected' : 'checked' ]) {
  5459. selectedChildIds.push(node.id);
  5460. }
  5461. selectedNodeIds = selectedNodeIds.concat(selectedChildIds);
  5462. }
  5463. return selectedNodeIds;
  5464. };
  5465. /**
  5466. * Gets ids of nodes selected in branch (of tree) specified by id (does not include the node specified by id)
  5467. * @param id
  5468. */
  5469. this.get_checked_descendants = function(id) {
  5470. var self = this;
  5471. var t = self.settings.checkbox.tie_selection;
  5472. var node = self._model.data[id];
  5473. return node.children_d.filter(function(_id) {
  5474. return self._model.data[_id].state[ t ? 'selected' : 'checked' ];
  5475. });
  5476. };
  5477. /**
  5478. * check a node (only if tie_selection in checkbox settings is false, otherwise select_node will be called internally)
  5479. * @name check_node(obj)
  5480. * @param {mixed} obj an array can be used to check multiple nodes
  5481. * @trigger check_node.jstree
  5482. * @plugin checkbox
  5483. */
  5484. this.check_node = function (obj, e) {
  5485. if(this.settings.checkbox.tie_selection) { return this.select_node(obj, false, true, e); }
  5486. var dom, t1, t2, th;
  5487. if($.isArray(obj)) {
  5488. obj = obj.slice();
  5489. for(t1 = 0, t2 = obj.length; t1 < t2; t1++) {
  5490. this.check_node(obj[t1], e);
  5491. }
  5492. return true;
  5493. }
  5494. obj = this.get_node(obj);
  5495. if(!obj || obj.id === $.jstree.root) {
  5496. return false;
  5497. }
  5498. dom = this.get_node(obj, true);
  5499. if(!obj.state.checked) {
  5500. obj.state.checked = true;
  5501. this._data.checkbox.selected.push(obj.id);
  5502. if(dom && dom.length) {
  5503. dom.children('.jstree-anchor').addClass('jstree-checked');
  5504. }
  5505. /**
  5506. * triggered when an node is checked (only if tie_selection in checkbox settings is false)
  5507. * @event
  5508. * @name check_node.jstree
  5509. * @param {Object} node
  5510. * @param {Array} selected the current selection
  5511. * @param {Object} event the event (if any) that triggered this check_node
  5512. * @plugin checkbox
  5513. */
  5514. this.trigger('check_node', { 'node' : obj, 'selected' : this._data.checkbox.selected, 'event' : e });
  5515. }
  5516. };
  5517. /**
  5518. * uncheck a node (only if tie_selection in checkbox settings is false, otherwise deselect_node will be called internally)
  5519. * @name uncheck_node(obj)
  5520. * @param {mixed} obj an array can be used to uncheck multiple nodes
  5521. * @trigger uncheck_node.jstree
  5522. * @plugin checkbox
  5523. */
  5524. this.uncheck_node = function (obj, e) {
  5525. if(this.settings.checkbox.tie_selection) { return this.deselect_node(obj, false, e); }
  5526. var t1, t2, dom;
  5527. if($.isArray(obj)) {
  5528. obj = obj.slice();
  5529. for(t1 = 0, t2 = obj.length; t1 < t2; t1++) {
  5530. this.uncheck_node(obj[t1], e);
  5531. }
  5532. return true;
  5533. }
  5534. obj = this.get_node(obj);
  5535. if(!obj || obj.id === $.jstree.root) {
  5536. return false;
  5537. }
  5538. dom = this.get_node(obj, true);
  5539. if(obj.state.checked) {
  5540. obj.state.checked = false;
  5541. this._data.checkbox.selected = $.vakata.array_remove_item(this._data.checkbox.selected, obj.id);
  5542. if(dom.length) {
  5543. dom.children('.jstree-anchor').removeClass('jstree-checked');
  5544. }
  5545. /**
  5546. * triggered when an node is unchecked (only if tie_selection in checkbox settings is false)
  5547. * @event
  5548. * @name uncheck_node.jstree
  5549. * @param {Object} node
  5550. * @param {Array} selected the current selection
  5551. * @param {Object} event the event (if any) that triggered this uncheck_node
  5552. * @plugin checkbox
  5553. */
  5554. this.trigger('uncheck_node', { 'node' : obj, 'selected' : this._data.checkbox.selected, 'event' : e });
  5555. }
  5556. };
  5557. /**
  5558. * checks all nodes in the tree (only if tie_selection in checkbox settings is false, otherwise select_all will be called internally)
  5559. * @name check_all()
  5560. * @trigger check_all.jstree, changed.jstree
  5561. * @plugin checkbox
  5562. */
  5563. this.check_all = function () {
  5564. if(this.settings.checkbox.tie_selection) { return this.select_all(); }
  5565. var tmp = this._data.checkbox.selected.concat([]), i, j;
  5566. this._data.checkbox.selected = this._model.data[$.jstree.root].children_d.concat();
  5567. for(i = 0, j = this._data.checkbox.selected.length; i < j; i++) {
  5568. if(this._model.data[this._data.checkbox.selected[i]]) {
  5569. this._model.data[this._data.checkbox.selected[i]].state.checked = true;
  5570. }
  5571. }
  5572. this.redraw(true);
  5573. /**
  5574. * triggered when all nodes are checked (only if tie_selection in checkbox settings is false)
  5575. * @event
  5576. * @name check_all.jstree
  5577. * @param {Array} selected the current selection
  5578. * @plugin checkbox
  5579. */
  5580. this.trigger('check_all', { 'selected' : this._data.checkbox.selected });
  5581. };
  5582. /**
  5583. * uncheck all checked nodes (only if tie_selection in checkbox settings is false, otherwise deselect_all will be called internally)
  5584. * @name uncheck_all()
  5585. * @trigger uncheck_all.jstree
  5586. * @plugin checkbox
  5587. */
  5588. this.uncheck_all = function () {
  5589. if(this.settings.checkbox.tie_selection) { return this.deselect_all(); }
  5590. var tmp = this._data.checkbox.selected.concat([]), i, j;
  5591. for(i = 0, j = this._data.checkbox.selected.length; i < j; i++) {
  5592. if(this._model.data[this._data.checkbox.selected[i]]) {
  5593. this._model.data[this._data.checkbox.selected[i]].state.checked = false;
  5594. }
  5595. }
  5596. this._data.checkbox.selected = [];
  5597. this.element.find('.jstree-checked').removeClass('jstree-checked');
  5598. /**
  5599. * triggered when all nodes are unchecked (only if tie_selection in checkbox settings is false)
  5600. * @event
  5601. * @name uncheck_all.jstree
  5602. * @param {Object} node the previous selection
  5603. * @param {Array} selected the current selection
  5604. * @plugin checkbox
  5605. */
  5606. this.trigger('uncheck_all', { 'selected' : this._data.checkbox.selected, 'node' : tmp });
  5607. };
  5608. /**
  5609. * checks if a node is checked (if tie_selection is on in the settings this function will return the same as is_selected)
  5610. * @name is_checked(obj)
  5611. * @param {mixed} obj
  5612. * @return {Boolean}
  5613. * @plugin checkbox
  5614. */
  5615. this.is_checked = function (obj) {
  5616. if(this.settings.checkbox.tie_selection) { return this.is_selected(obj); }
  5617. obj = this.get_node(obj);
  5618. if(!obj || obj.id === $.jstree.root) { return false; }
  5619. return obj.state.checked;
  5620. };
  5621. /**
  5622. * get an array of all checked nodes (if tie_selection is on in the settings this function will return the same as get_selected)
  5623. * @name get_checked([full])
  5624. * @param {mixed} full if set to `true` the returned array will consist of the full node objects, otherwise - only IDs will be returned
  5625. * @return {Array}
  5626. * @plugin checkbox
  5627. */
  5628. this.get_checked = function (full) {
  5629. if(this.settings.checkbox.tie_selection) { return this.get_selected(full); }
  5630. return full ? $.map(this._data.checkbox.selected, $.proxy(function (i) { return this.get_node(i); }, this)) : this._data.checkbox.selected;
  5631. };
  5632. /**
  5633. * get an array of all top level checked nodes (ignoring children of checked nodes) (if tie_selection is on in the settings this function will return the same as get_top_selected)
  5634. * @name get_top_checked([full])
  5635. * @param {mixed} full if set to `true` the returned array will consist of the full node objects, otherwise - only IDs will be returned
  5636. * @return {Array}
  5637. * @plugin checkbox
  5638. */
  5639. this.get_top_checked = function (full) {
  5640. if(this.settings.checkbox.tie_selection) { return this.get_top_selected(full); }
  5641. var tmp = this.get_checked(true),
  5642. obj = {}, i, j, k, l;
  5643. for(i = 0, j = tmp.length; i < j; i++) {
  5644. obj[tmp[i].id] = tmp[i];
  5645. }
  5646. for(i = 0, j = tmp.length; i < j; i++) {
  5647. for(k = 0, l = tmp[i].children_d.length; k < l; k++) {
  5648. if(obj[tmp[i].children_d[k]]) {
  5649. delete obj[tmp[i].children_d[k]];
  5650. }
  5651. }
  5652. }
  5653. tmp = [];
  5654. for(i in obj) {
  5655. if(obj.hasOwnProperty(i)) {
  5656. tmp.push(i);
  5657. }
  5658. }
  5659. return full ? $.map(tmp, $.proxy(function (i) { return this.get_node(i); }, this)) : tmp;
  5660. };
  5661. /**
  5662. * get an array of all bottom level checked nodes (ignoring selected parents) (if tie_selection is on in the settings this function will return the same as get_bottom_selected)
  5663. * @name get_bottom_checked([full])
  5664. * @param {mixed} full if set to `true` the returned array will consist of the full node objects, otherwise - only IDs will be returned
  5665. * @return {Array}
  5666. * @plugin checkbox
  5667. */
  5668. this.get_bottom_checked = function (full) {
  5669. if(this.settings.checkbox.tie_selection) { return this.get_bottom_selected(full); }
  5670. var tmp = this.get_checked(true),
  5671. obj = [], i, j;
  5672. for(i = 0, j = tmp.length; i < j; i++) {
  5673. if(!tmp[i].children.length) {
  5674. obj.push(tmp[i].id);
  5675. }
  5676. }
  5677. return full ? $.map(obj, $.proxy(function (i) { return this.get_node(i); }, this)) : obj;
  5678. };
  5679. this.load_node = function (obj, callback) {
  5680. var k, l, i, j, c, tmp;
  5681. if(!$.isArray(obj) && !this.settings.checkbox.tie_selection) {
  5682. tmp = this.get_node(obj);
  5683. if(tmp && tmp.state.loaded) {
  5684. for(k = 0, l = tmp.children_d.length; k < l; k++) {
  5685. if(this._model.data[tmp.children_d[k]].state.checked) {
  5686. c = true;
  5687. this._data.checkbox.selected = $.vakata.array_remove_item(this._data.checkbox.selected, tmp.children_d[k]);
  5688. }
  5689. }
  5690. }
  5691. }
  5692. return parent.load_node.apply(this, arguments);
  5693. };
  5694. this.get_state = function () {
  5695. var state = parent.get_state.apply(this, arguments);
  5696. if(this.settings.checkbox.tie_selection) { return state; }
  5697. state.checkbox = this._data.checkbox.selected.slice();
  5698. return state;
  5699. };
  5700. this.set_state = function (state, callback) {
  5701. var res = parent.set_state.apply(this, arguments);
  5702. if(res && state.checkbox) {
  5703. if(!this.settings.checkbox.tie_selection) {
  5704. this.uncheck_all();
  5705. var _this = this;
  5706. $.each(state.checkbox, function (i, v) {
  5707. _this.check_node(v);
  5708. });
  5709. }
  5710. delete state.checkbox;
  5711. this.set_state(state, callback);
  5712. return false;
  5713. }
  5714. return res;
  5715. };
  5716. this.refresh = function (skip_loading, forget_state) {
  5717. if(!this.settings.checkbox.tie_selection) {
  5718. this._data.checkbox.selected = [];
  5719. }
  5720. return parent.refresh.apply(this, arguments);
  5721. };
  5722. };
  5723. // include the checkbox plugin by default
  5724. // $.jstree.defaults.plugins.push("checkbox");
  5725. /**
  5726. * ### Conditionalselect plugin
  5727. *
  5728. * This plugin allows defining a callback to allow or deny node selection by user input (activate node method).
  5729. */
  5730. /**
  5731. * a callback (function) which is invoked in the instance's scope and receives two arguments - the node and the event that triggered the `activate_node` call. Returning false prevents working with the node, returning true allows invoking activate_node. Defaults to returning `true`.
  5732. * @name $.jstree.defaults.checkbox.visible
  5733. * @plugin checkbox
  5734. */
  5735. $.jstree.defaults.conditionalselect = function () { return true; };
  5736. $.jstree.plugins.conditionalselect = function (options, parent) {
  5737. // own function
  5738. this.activate_node = function (obj, e) {
  5739. if(this.settings.conditionalselect.call(this, this.get_node(obj), e)) {
  5740. parent.activate_node.call(this, obj, e);
  5741. }
  5742. };
  5743. };
  5744. /**
  5745. * ### Contextmenu plugin
  5746. *
  5747. * Shows a context menu when a node is right-clicked.
  5748. */
  5749. /**
  5750. * stores all defaults for the contextmenu plugin
  5751. * @name $.jstree.defaults.contextmenu
  5752. * @plugin contextmenu
  5753. */
  5754. $.jstree.defaults.contextmenu = {
  5755. /**
  5756. * a boolean indicating if the node should be selected when the context menu is invoked on it. Defaults to `true`.
  5757. * @name $.jstree.defaults.contextmenu.select_node
  5758. * @plugin contextmenu
  5759. */
  5760. select_node : true,
  5761. /**
  5762. * a boolean indicating if the menu should be shown aligned with the node. Defaults to `true`, otherwise the mouse coordinates are used.
  5763. * @name $.jstree.defaults.contextmenu.show_at_node
  5764. * @plugin contextmenu
  5765. */
  5766. show_at_node : true,
  5767. /**
  5768. * 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).
  5769. *
  5770. * 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.
  5771. *
  5772. * * `separator_before` - a boolean indicating if there should be a separator before this item
  5773. * * `separator_after` - a boolean indicating if there should be a separator after this item
  5774. * * `_disabled` - a boolean indicating if this action should be disabled
  5775. * * `label` - a string - the name of the action (could be a function returning a string)
  5776. * * `title` - a string - an optional tooltip for the item
  5777. * * `action` - a function to be executed if this item is chosen, the function will receive
  5778. * * `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
  5779. * * `shortcut` - keyCode which will trigger the action if the menu is open (for example `113` for rename, which equals F2)
  5780. * * `shortcut_label` - shortcut label (like for example `F2` for rename)
  5781. * * `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
  5782. *
  5783. * @name $.jstree.defaults.contextmenu.items
  5784. * @plugin contextmenu
  5785. */
  5786. items : function (o, cb) { // Could be an object directly
  5787. return {
  5788. "create" : {
  5789. "separator_before" : false,
  5790. "separator_after" : true,
  5791. "_disabled" : false, //(this.check("create_node", data.reference, {}, "last")),
  5792. "label" : "Create",
  5793. "action" : function (data) {
  5794. var inst = $.jstree.reference(data.reference),
  5795. obj = inst.get_node(data.reference);
  5796. inst.create_node(obj, {}, "last", function (new_node) {
  5797. try {
  5798. inst.edit(new_node);
  5799. } catch (ex) {
  5800. setTimeout(function () { inst.edit(new_node); },0);
  5801. }
  5802. });
  5803. }
  5804. },
  5805. "rename" : {
  5806. "separator_before" : false,
  5807. "separator_after" : false,
  5808. "_disabled" : false, //(this.check("rename_node", data.reference, this.get_parent(data.reference), "")),
  5809. "label" : "Rename",
  5810. /*!
  5811. "shortcut" : 113,
  5812. "shortcut_label" : 'F2',
  5813. "icon" : "glyphicon glyphicon-leaf",
  5814. */
  5815. "action" : function (data) {
  5816. var inst = $.jstree.reference(data.reference),
  5817. obj = inst.get_node(data.reference);
  5818. inst.edit(obj);
  5819. }
  5820. },
  5821. "remove" : {
  5822. "separator_before" : false,
  5823. "icon" : false,
  5824. "separator_after" : false,
  5825. "_disabled" : false, //(this.check("delete_node", data.reference, this.get_parent(data.reference), "")),
  5826. "label" : "Delete",
  5827. "action" : function (data) {
  5828. var inst = $.jstree.reference(data.reference),
  5829. obj = inst.get_node(data.reference);
  5830. if(inst.is_selected(obj)) {
  5831. inst.delete_node(inst.get_selected());
  5832. }
  5833. else {
  5834. inst.delete_node(obj);
  5835. }
  5836. }
  5837. },
  5838. "ccp" : {
  5839. "separator_before" : true,
  5840. "icon" : false,
  5841. "separator_after" : false,
  5842. "label" : "Edit",
  5843. "action" : false,
  5844. "submenu" : {
  5845. "cut" : {
  5846. "separator_before" : false,
  5847. "separator_after" : false,
  5848. "label" : "Cut",
  5849. "action" : function (data) {
  5850. var inst = $.jstree.reference(data.reference),
  5851. obj = inst.get_node(data.reference);
  5852. if(inst.is_selected(obj)) {
  5853. inst.cut(inst.get_top_selected());
  5854. }
  5855. else {
  5856. inst.cut(obj);
  5857. }
  5858. }
  5859. },
  5860. "copy" : {
  5861. "separator_before" : false,
  5862. "icon" : false,
  5863. "separator_after" : false,
  5864. "label" : "Copy",
  5865. "action" : function (data) {
  5866. var inst = $.jstree.reference(data.reference),
  5867. obj = inst.get_node(data.reference);
  5868. if(inst.is_selected(obj)) {
  5869. inst.copy(inst.get_top_selected());
  5870. }
  5871. else {
  5872. inst.copy(obj);
  5873. }
  5874. }
  5875. },
  5876. "paste" : {
  5877. "separator_before" : false,
  5878. "icon" : false,
  5879. "_disabled" : function (data) {
  5880. return !$.jstree.reference(data.reference).can_paste();
  5881. },
  5882. "separator_after" : false,
  5883. "label" : "Paste",
  5884. "action" : function (data) {
  5885. var inst = $.jstree.reference(data.reference),
  5886. obj = inst.get_node(data.reference);
  5887. inst.paste(obj);
  5888. }
  5889. }
  5890. }
  5891. }
  5892. };
  5893. }
  5894. };
  5895. $.jstree.plugins.contextmenu = function (options, parent) {
  5896. this.bind = function () {
  5897. parent.bind.call(this);
  5898. var last_ts = 0, cto = null, ex, ey;
  5899. this.element
  5900. .on("init.jstree loading.jstree ready.jstree", $.proxy(function () {
  5901. this.get_container_ul().addClass('jstree-contextmenu');
  5902. }, this))
  5903. .on("contextmenu.jstree", ".jstree-anchor", $.proxy(function (e, data) {
  5904. if (e.target.tagName.toLowerCase() === 'input') {
  5905. return;
  5906. }
  5907. e.preventDefault();
  5908. last_ts = e.ctrlKey ? +new Date() : 0;
  5909. if(data || cto) {
  5910. last_ts = (+new Date()) + 10000;
  5911. }
  5912. if(cto) {
  5913. clearTimeout(cto);
  5914. }
  5915. if(!this.is_loading(e.currentTarget)) {
  5916. this.show_contextmenu(e.currentTarget, e.pageX, e.pageY, e);
  5917. }
  5918. }, this))
  5919. .on("click.jstree", ".jstree-anchor", $.proxy(function (e) {
  5920. if(this._data.contextmenu.visible && (!last_ts || (+new Date()) - last_ts > 250)) { // work around safari & macOS ctrl+click
  5921. $.vakata.context.hide();
  5922. }
  5923. last_ts = 0;
  5924. }, this))
  5925. .on("touchstart.jstree", ".jstree-anchor", function (e) {
  5926. if(!e.originalEvent || !e.originalEvent.changedTouches || !e.originalEvent.changedTouches[0]) {
  5927. return;
  5928. }
  5929. ex = e.originalEvent.changedTouches[0].clientX;
  5930. ey = e.originalEvent.changedTouches[0].clientY;
  5931. cto = setTimeout(function () {
  5932. $(e.currentTarget).trigger('contextmenu', true);
  5933. }, 750);
  5934. })
  5935. .on('touchmove.vakata.jstree', function (e) {
  5936. if(cto && e.originalEvent && e.originalEvent.changedTouches && e.originalEvent.changedTouches[0] && (Math.abs(ex - e.originalEvent.changedTouches[0].clientX) > 50 || Math.abs(ey - e.originalEvent.changedTouches[0].clientY) > 50)) {
  5937. clearTimeout(cto);
  5938. }
  5939. })
  5940. .on('touchend.vakata.jstree', function (e) {
  5941. if(cto) {
  5942. clearTimeout(cto);
  5943. }
  5944. });
  5945. /*!
  5946. if(!('oncontextmenu' in document.body) && ('ontouchstart' in document.body)) {
  5947. var el = null, tm = null;
  5948. this.element
  5949. .on("touchstart", ".jstree-anchor", function (e) {
  5950. el = e.currentTarget;
  5951. tm = +new Date();
  5952. $(document).one("touchend", function (e) {
  5953. e.target = document.elementFromPoint(e.originalEvent.targetTouches[0].pageX - window.pageXOffset, e.originalEvent.targetTouches[0].pageY - window.pageYOffset);
  5954. e.currentTarget = e.target;
  5955. tm = ((+(new Date())) - tm);
  5956. if(e.target === el && tm > 600 && tm < 1000) {
  5957. e.preventDefault();
  5958. $(el).trigger('contextmenu', e);
  5959. }
  5960. el = null;
  5961. tm = null;
  5962. });
  5963. });
  5964. }
  5965. */
  5966. $(document).on("context_hide.vakata.jstree", $.proxy(function (e, data) {
  5967. this._data.contextmenu.visible = false;
  5968. $(data.reference).removeClass('jstree-context');
  5969. }, this));
  5970. };
  5971. this.teardown = function () {
  5972. if(this._data.contextmenu.visible) {
  5973. $.vakata.context.hide();
  5974. }
  5975. parent.teardown.call(this);
  5976. };
  5977. /**
  5978. * prepare and show the context menu for a node
  5979. * @name show_contextmenu(obj [, x, y])
  5980. * @param {mixed} obj the node
  5981. * @param {Number} x the x-coordinate relative to the document to show the menu at
  5982. * @param {Number} y the y-coordinate relative to the document to show the menu at
  5983. * @param {Object} e the event if available that triggered the contextmenu
  5984. * @plugin contextmenu
  5985. * @trigger show_contextmenu.jstree
  5986. */
  5987. this.show_contextmenu = function (obj, x, y, e) {
  5988. obj = this.get_node(obj);
  5989. if(!obj || obj.id === $.jstree.root) { return false; }
  5990. var s = this.settings.contextmenu,
  5991. d = this.get_node(obj, true),
  5992. a = d.children(".jstree-anchor"),
  5993. o = false,
  5994. i = false;
  5995. if(s.show_at_node || x === undefined || y === undefined) {
  5996. o = a.offset();
  5997. x = o.left;
  5998. y = o.top + this._data.core.li_height;
  5999. }
  6000. if(this.settings.contextmenu.select_node && !this.is_selected(obj)) {
  6001. this.activate_node(obj, e);
  6002. }
  6003. i = s.items;
  6004. if($.isFunction(i)) {
  6005. i = i.call(this, obj, $.proxy(function (i) {
  6006. this._show_contextmenu(obj, x, y, i);
  6007. }, this));
  6008. }
  6009. if($.isPlainObject(i)) {
  6010. this._show_contextmenu(obj, x, y, i);
  6011. }
  6012. };
  6013. /**
  6014. * show the prepared context menu for a node
  6015. * @name _show_contextmenu(obj, x, y, i)
  6016. * @param {mixed} obj the node
  6017. * @param {Number} x the x-coordinate relative to the document to show the menu at
  6018. * @param {Number} y the y-coordinate relative to the document to show the menu at
  6019. * @param {Number} i the object of items to show
  6020. * @plugin contextmenu
  6021. * @trigger show_contextmenu.jstree
  6022. * @private
  6023. */
  6024. this._show_contextmenu = function (obj, x, y, i) {
  6025. var d = this.get_node(obj, true),
  6026. a = d.children(".jstree-anchor");
  6027. $(document).one("context_show.vakata.jstree", $.proxy(function (e, data) {
  6028. var cls = 'jstree-contextmenu jstree-' + this.get_theme() + '-contextmenu';
  6029. $(data.element).addClass(cls);
  6030. a.addClass('jstree-context');
  6031. }, this));
  6032. this._data.contextmenu.visible = true;
  6033. $.vakata.context.show(a, { 'x' : x, 'y' : y }, i);
  6034. /**
  6035. * triggered when the contextmenu is shown for a node
  6036. * @event
  6037. * @name show_contextmenu.jstree
  6038. * @param {Object} node the node
  6039. * @param {Number} x the x-coordinate of the menu relative to the document
  6040. * @param {Number} y the y-coordinate of the menu relative to the document
  6041. * @plugin contextmenu
  6042. */
  6043. this.trigger('show_contextmenu', { "node" : obj, "x" : x, "y" : y });
  6044. };
  6045. };
  6046. // contextmenu helper
  6047. (function ($) {
  6048. var right_to_left = false,
  6049. vakata_context = {
  6050. element : false,
  6051. reference : false,
  6052. position_x : 0,
  6053. position_y : 0,
  6054. items : [],
  6055. html : "",
  6056. is_visible : false
  6057. };
  6058. $.vakata.context = {
  6059. settings : {
  6060. hide_onmouseleave : 0,
  6061. icons : true
  6062. },
  6063. _trigger : function (event_name) {
  6064. $(document).triggerHandler("context_" + event_name + ".vakata", {
  6065. "reference" : vakata_context.reference,
  6066. "element" : vakata_context.element,
  6067. "position" : {
  6068. "x" : vakata_context.position_x,
  6069. "y" : vakata_context.position_y
  6070. }
  6071. });
  6072. },
  6073. _execute : function (i) {
  6074. i = vakata_context.items[i];
  6075. 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, {
  6076. "item" : i,
  6077. "reference" : vakata_context.reference,
  6078. "element" : vakata_context.element,
  6079. "position" : {
  6080. "x" : vakata_context.position_x,
  6081. "y" : vakata_context.position_y
  6082. }
  6083. }) : false;
  6084. },
  6085. _parse : function (o, is_callback) {
  6086. if(!o) { return false; }
  6087. if(!is_callback) {
  6088. vakata_context.html = "";
  6089. vakata_context.items = [];
  6090. }
  6091. var str = "",
  6092. sep = false,
  6093. tmp;
  6094. if(is_callback) { str += "<"+"ul>"; }
  6095. $.each(o, function (i, val) {
  6096. if(!val) { return true; }
  6097. vakata_context.items.push(val);
  6098. if(!sep && val.separator_before) {
  6099. str += "<"+"li class='vakata-context-separator'><"+"a href='#' " + ($.vakata.context.settings.icons ? '' : 'style="margin-left:0px;"') + ">&#160;<"+"/a><"+"/li>";
  6100. }
  6101. sep = false;
  6102. 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+"' ":'')+">";
  6103. str += "<"+"a href='#' rel='" + (vakata_context.items.length - 1) + "' " + (val.title ? "title='" + val.title + "'" : "") + ">";
  6104. if($.vakata.context.settings.icons) {
  6105. str += "<"+"i ";
  6106. if(val.icon) {
  6107. if(val.icon.indexOf("/") !== -1 || val.icon.indexOf(".") !== -1) { str += " style='background:url(\"" + val.icon + "\") center center no-repeat' "; }
  6108. else { str += " class='" + val.icon + "' "; }
  6109. }
  6110. str += "><"+"/i><"+"span class='vakata-contextmenu-sep'>&#160;<"+"/span>";
  6111. }
  6112. 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>";
  6113. if(val.submenu) {
  6114. tmp = $.vakata.context._parse(val.submenu, true);
  6115. if(tmp) { str += tmp; }
  6116. }
  6117. str += "<"+"/li>";
  6118. if(val.separator_after) {
  6119. str += "<"+"li class='vakata-context-separator'><"+"a href='#' " + ($.vakata.context.settings.icons ? '' : 'style="margin-left:0px;"') + ">&#160;<"+"/a><"+"/li>";
  6120. sep = true;
  6121. }
  6122. });
  6123. str = str.replace(/<li class\='vakata-context-separator'\><\/li\>$/,"");
  6124. if(is_callback) { str += "</ul>"; }
  6125. /**
  6126. * triggered on the document when the contextmenu is parsed (HTML is built)
  6127. * @event
  6128. * @plugin contextmenu
  6129. * @name context_parse.vakata
  6130. * @param {jQuery} reference the element that was right clicked
  6131. * @param {jQuery} element the DOM element of the menu itself
  6132. * @param {Object} position the x & y coordinates of the menu
  6133. */
  6134. if(!is_callback) { vakata_context.html = str; $.vakata.context._trigger("parse"); }
  6135. return str.length > 10 ? str : false;
  6136. },
  6137. _show_submenu : function (o) {
  6138. o = $(o);
  6139. if(!o.length || !o.children("ul").length) { return; }
  6140. var e = o.children("ul"),
  6141. xl = o.offset().left,
  6142. x = xl + o.outerWidth(),
  6143. y = o.offset().top,
  6144. w = e.width(),
  6145. h = e.height(),
  6146. dw = $(window).width() + $(window).scrollLeft(),
  6147. dh = $(window).height() + $(window).scrollTop();
  6148. // може да се спести е една проверка - дали няма някой от класовете вече нагоре
  6149. if(right_to_left) {
  6150. o[x - (w + 10 + o.outerWidth()) < 0 ? "addClass" : "removeClass"]("vakata-context-left");
  6151. }
  6152. else {
  6153. o[x + w > dw && xl > dw - x ? "addClass" : "removeClass"]("vakata-context-right");
  6154. }
  6155. if(y + h + 10 > dh) {
  6156. e.css("bottom","-1px");
  6157. }
  6158. //if does not fit - stick it to the side
  6159. if (o.hasClass('vakata-context-right')) {
  6160. if (xl < w) {
  6161. e.css("margin-right", xl - w);
  6162. }
  6163. } else {
  6164. if (dw - x < w) {
  6165. e.css("margin-left", dw - x - w);
  6166. }
  6167. }
  6168. e.show();
  6169. },
  6170. show : function (reference, position, data) {
  6171. var o, e, x, y, w, h, dw, dh, cond = true;
  6172. if(vakata_context.element && vakata_context.element.length) {
  6173. vakata_context.element.width('');
  6174. }
  6175. switch(cond) {
  6176. case (!position && !reference):
  6177. return false;
  6178. case (!!position && !!reference):
  6179. vakata_context.reference = reference;
  6180. vakata_context.position_x = position.x;
  6181. vakata_context.position_y = position.y;
  6182. break;
  6183. case (!position && !!reference):
  6184. vakata_context.reference = reference;
  6185. o = reference.offset();
  6186. vakata_context.position_x = o.left + reference.outerHeight();
  6187. vakata_context.position_y = o.top;
  6188. break;
  6189. case (!!position && !reference):
  6190. vakata_context.position_x = position.x;
  6191. vakata_context.position_y = position.y;
  6192. break;
  6193. }
  6194. if(!!reference && !data && $(reference).data('vakata_contextmenu')) {
  6195. data = $(reference).data('vakata_contextmenu');
  6196. }
  6197. if($.vakata.context._parse(data)) {
  6198. vakata_context.element.html(vakata_context.html);
  6199. }
  6200. if(vakata_context.items.length) {
  6201. vakata_context.element.appendTo("body");
  6202. e = vakata_context.element;
  6203. x = vakata_context.position_x;
  6204. y = vakata_context.position_y;
  6205. w = e.width();
  6206. h = e.height();
  6207. dw = $(window).width() + $(window).scrollLeft();
  6208. dh = $(window).height() + $(window).scrollTop();
  6209. if(right_to_left) {
  6210. x -= (e.outerWidth() - $(reference).outerWidth());
  6211. if(x < $(window).scrollLeft() + 20) {
  6212. x = $(window).scrollLeft() + 20;
  6213. }
  6214. }
  6215. if(x + w + 20 > dw) {
  6216. x = dw - (w + 20);
  6217. }
  6218. if(y + h + 20 > dh) {
  6219. y = dh - (h + 20);
  6220. }
  6221. vakata_context.element
  6222. .css({ "left" : x, "top" : y })
  6223. .show()
  6224. .find('a').first().focus().parent().addClass("vakata-context-hover");
  6225. vakata_context.is_visible = true;
  6226. /**
  6227. * triggered on the document when the contextmenu is shown
  6228. * @event
  6229. * @plugin contextmenu
  6230. * @name context_show.vakata
  6231. * @param {jQuery} reference the element that was right clicked
  6232. * @param {jQuery} element the DOM element of the menu itself
  6233. * @param {Object} position the x & y coordinates of the menu
  6234. */
  6235. $.vakata.context._trigger("show");
  6236. }
  6237. },
  6238. hide : function () {
  6239. if(vakata_context.is_visible) {
  6240. vakata_context.element.hide().find("ul").hide().end().find(':focus').blur().end().detach();
  6241. vakata_context.is_visible = false;
  6242. /**
  6243. * triggered on the document when the contextmenu is hidden
  6244. * @event
  6245. * @plugin contextmenu
  6246. * @name context_hide.vakata
  6247. * @param {jQuery} reference the element that was right clicked
  6248. * @param {jQuery} element the DOM element of the menu itself
  6249. * @param {Object} position the x & y coordinates of the menu
  6250. */
  6251. $.vakata.context._trigger("hide");
  6252. }
  6253. }
  6254. };
  6255. $(function () {
  6256. right_to_left = $("body").css("direction") === "rtl";
  6257. var to = false;
  6258. vakata_context.element = $("<ul class='vakata-context'></ul>");
  6259. vakata_context.element
  6260. .on("mouseenter", "li", function (e) {
  6261. e.stopImmediatePropagation();
  6262. if($.contains(this, e.relatedTarget)) {
  6263. // премахнато заради delegate mouseleave по-долу
  6264. // $(this).find(".vakata-context-hover").removeClass("vakata-context-hover");
  6265. return;
  6266. }
  6267. if(to) { clearTimeout(to); }
  6268. vakata_context.element.find(".vakata-context-hover").removeClass("vakata-context-hover").end();
  6269. $(this)
  6270. .siblings().find("ul").hide().end().end()
  6271. .parentsUntil(".vakata-context", "li").addBack().addClass("vakata-context-hover");
  6272. $.vakata.context._show_submenu(this);
  6273. })
  6274. // тестово - дали не натоварва?
  6275. .on("mouseleave", "li", function (e) {
  6276. if($.contains(this, e.relatedTarget)) { return; }
  6277. $(this).find(".vakata-context-hover").addBack().removeClass("vakata-context-hover");
  6278. })
  6279. .on("mouseleave", function (e) {
  6280. $(this).find(".vakata-context-hover").removeClass("vakata-context-hover");
  6281. if($.vakata.context.settings.hide_onmouseleave) {
  6282. to = setTimeout(
  6283. (function (t) {
  6284. return function () { $.vakata.context.hide(); };
  6285. }(this)), $.vakata.context.settings.hide_onmouseleave);
  6286. }
  6287. })
  6288. .on("click", "a", function (e) {
  6289. e.preventDefault();
  6290. //})
  6291. //.on("mouseup", "a", function (e) {
  6292. if(!$(this).blur().parent().hasClass("vakata-context-disabled") && $.vakata.context._execute($(this).attr("rel")) !== false) {
  6293. $.vakata.context.hide();
  6294. }
  6295. })
  6296. .on('keydown', 'a', function (e) {
  6297. var o = null;
  6298. switch(e.which) {
  6299. case 13:
  6300. case 32:
  6301. e.type = "click";
  6302. e.preventDefault();
  6303. $(e.currentTarget).trigger(e);
  6304. break;
  6305. case 37:
  6306. if(vakata_context.is_visible) {
  6307. 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();
  6308. e.stopImmediatePropagation();
  6309. e.preventDefault();
  6310. }
  6311. break;
  6312. case 38:
  6313. if(vakata_context.is_visible) {
  6314. o = vakata_context.element.find("ul:visible").addBack().last().children(".vakata-context-hover").removeClass("vakata-context-hover").prevAll("li:not(.vakata-context-separator)").first();
  6315. if(!o.length) { o = vakata_context.element.find("ul:visible").addBack().last().children("li:not(.vakata-context-separator)").last(); }
  6316. o.addClass("vakata-context-hover").children('a').focus();
  6317. e.stopImmediatePropagation();
  6318. e.preventDefault();
  6319. }
  6320. break;
  6321. case 39:
  6322. if(vakata_context.is_visible) {
  6323. 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();
  6324. e.stopImmediatePropagation();
  6325. e.preventDefault();
  6326. }
  6327. break;
  6328. case 40:
  6329. if(vakata_context.is_visible) {
  6330. o = vakata_context.element.find("ul:visible").addBack().last().children(".vakata-context-hover").removeClass("vakata-context-hover").nextAll("li:not(.vakata-context-separator)").first();
  6331. if(!o.length) { o = vakata_context.element.find("ul:visible").addBack().last().children("li:not(.vakata-context-separator)").first(); }
  6332. o.addClass("vakata-context-hover").children('a').focus();
  6333. e.stopImmediatePropagation();
  6334. e.preventDefault();
  6335. }
  6336. break;
  6337. case 27:
  6338. $.vakata.context.hide();
  6339. e.preventDefault();
  6340. break;
  6341. default:
  6342. //console.log(e.which);
  6343. break;
  6344. }
  6345. })
  6346. .on('keydown', function (e) {
  6347. e.preventDefault();
  6348. var a = vakata_context.element.find('.vakata-contextmenu-shortcut-' + e.which).parent();
  6349. if(a.parent().not('.vakata-context-disabled')) {
  6350. a.click();
  6351. }
  6352. });
  6353. $(document)
  6354. .on("mousedown.vakata.jstree", function (e) {
  6355. if(vakata_context.is_visible && vakata_context.element[0] !== e.target && !$.contains(vakata_context.element[0], e.target)) {
  6356. $.vakata.context.hide();
  6357. }
  6358. })
  6359. .on("context_show.vakata.jstree", function (e, data) {
  6360. vakata_context.element.find("li:has(ul)").children("a").addClass("vakata-context-parent");
  6361. if(right_to_left) {
  6362. vakata_context.element.addClass("vakata-context-rtl").css("direction", "rtl");
  6363. }
  6364. // also apply a RTL class?
  6365. vakata_context.element.find("ul").hide().end();
  6366. });
  6367. });
  6368. }($));
  6369. // $.jstree.defaults.plugins.push("contextmenu");
  6370. /**
  6371. * ### Drag'n'drop plugin
  6372. *
  6373. * Enables dragging and dropping of nodes in the tree, resulting in a move or copy operations.
  6374. */
  6375. /**
  6376. * stores all defaults for the drag'n'drop plugin
  6377. * @name $.jstree.defaults.dnd
  6378. * @plugin dnd
  6379. */
  6380. $.jstree.defaults.dnd = {
  6381. /**
  6382. * a boolean indicating if a copy should be possible while dragging (by pressint the meta key or Ctrl). Defaults to `true`.
  6383. * @name $.jstree.defaults.dnd.copy
  6384. * @plugin dnd
  6385. */
  6386. copy : true,
  6387. /**
  6388. * a number indicating how long a node should remain hovered while dragging to be opened. Defaults to `500`.
  6389. * @name $.jstree.defaults.dnd.open_timeout
  6390. * @plugin dnd
  6391. */
  6392. open_timeout : 500,
  6393. /**
  6394. * a function invoked each time a node is about to be dragged, invoked in the tree's scope and receives the nodes about to be dragged as an argument (array) and the event that started the drag - return `false` to prevent dragging
  6395. * @name $.jstree.defaults.dnd.is_draggable
  6396. * @plugin dnd
  6397. */
  6398. is_draggable : true,
  6399. /**
  6400. * a boolean indicating if checks should constantly be made while the user is dragging the node (as opposed to checking only on drop), default is `true`
  6401. * @name $.jstree.defaults.dnd.check_while_dragging
  6402. * @plugin dnd
  6403. */
  6404. check_while_dragging : true,
  6405. /**
  6406. * a boolean indicating if nodes from this tree should only be copied with dnd (as opposed to moved), default is `false`
  6407. * @name $.jstree.defaults.dnd.always_copy
  6408. * @plugin dnd
  6409. */
  6410. always_copy : false,
  6411. /**
  6412. * when dropping a node "inside", this setting indicates the position the node should go to - it can be an integer or a string: "first" (same as 0) or "last", default is `0`
  6413. * @name $.jstree.defaults.dnd.inside_pos
  6414. * @plugin dnd
  6415. */
  6416. inside_pos : 0,
  6417. /**
  6418. * when starting the drag on a node that is selected this setting controls if all selected nodes are dragged or only the single node, default is `true`, which means all selected nodes are dragged when the drag is started on a selected node
  6419. * @name $.jstree.defaults.dnd.drag_selection
  6420. * @plugin dnd
  6421. */
  6422. drag_selection : true,
  6423. /**
  6424. * controls whether dnd works on touch devices. If left as boolean true dnd will work the same as in desktop browsers, which in some cases may impair scrolling. If set to boolean false dnd will not work on touch devices. There is a special third option - string "selected" which means only selected nodes can be dragged on touch devices.
  6425. * @name $.jstree.defaults.dnd.touch
  6426. * @plugin dnd
  6427. */
  6428. touch : true,
  6429. /**
  6430. * controls whether items can be dropped anywhere on the node, not just on the anchor, by default only the node anchor is a valid drop target. Works best with the wholerow plugin. If enabled on mobile depending on the interface it might be hard for the user to cancel the drop, since the whole tree container will be a valid drop target.
  6431. * @name $.jstree.defaults.dnd.large_drop_target
  6432. * @plugin dnd
  6433. */
  6434. large_drop_target : false,
  6435. /**
  6436. * controls whether a drag can be initiated from any part of the node and not just the text/icon part, works best with the wholerow plugin. Keep in mind it can cause problems with tree scrolling on mobile depending on the interface - in that case set the touch option to "selected".
  6437. * @name $.jstree.defaults.dnd.large_drag_target
  6438. * @plugin dnd
  6439. */
  6440. large_drag_target : false,
  6441. /**
  6442. * controls whether use HTML5 dnd api instead of classical. That will allow better integration of dnd events with other HTML5 controls.
  6443. * @reference http://caniuse.com/#feat=dragndrop
  6444. * @name $.jstree.defaults.dnd.use_html5
  6445. * @plugin dnd
  6446. */
  6447. use_html5: false
  6448. };
  6449. var drg, elm;
  6450. // TODO: now check works by checking for each node individually, how about max_children, unique, etc?
  6451. $.jstree.plugins.dnd = function (options, parent) {
  6452. this.init = function (el, options) {
  6453. parent.init.call(this, el, options);
  6454. this.settings.dnd.use_html5 = this.settings.dnd.use_html5 && ('draggable' in document.createElement('span'));
  6455. };
  6456. this.bind = function () {
  6457. parent.bind.call(this);
  6458. this.element
  6459. .on(this.settings.dnd.use_html5 ? 'dragstart.jstree' : 'mousedown.jstree touchstart.jstree', this.settings.dnd.large_drag_target ? '.jstree-node' : '.jstree-anchor', $.proxy(function (e) {
  6460. if(this.settings.dnd.large_drag_target && $(e.target).closest('.jstree-node')[0] !== e.currentTarget) {
  6461. return true;
  6462. }
  6463. if(e.type === "touchstart" && (!this.settings.dnd.touch || (this.settings.dnd.touch === 'selected' && !$(e.currentTarget).closest('.jstree-node').children('.jstree-anchor').hasClass('jstree-clicked')))) {
  6464. return true;
  6465. }
  6466. var obj = this.get_node(e.target),
  6467. mlt = this.is_selected(obj) && this.settings.dnd.drag_selection ? this.get_top_selected().length : 1,
  6468. txt = (mlt > 1 ? mlt + ' ' + this.get_string('nodes') : this.get_text(e.currentTarget));
  6469. if(this.settings.core.force_text) {
  6470. txt = $.vakata.html.escape(txt);
  6471. }
  6472. if(obj && obj.id && obj.id !== $.jstree.root && (e.which === 1 || e.type === "touchstart" || e.type === "dragstart") &&
  6473. (this.settings.dnd.is_draggable === true || ($.isFunction(this.settings.dnd.is_draggable) && this.settings.dnd.is_draggable.call(this, (mlt > 1 ? this.get_top_selected(true) : [obj]), e)))
  6474. ) {
  6475. drg = { 'jstree' : true, 'origin' : this, 'obj' : this.get_node(obj,true), 'nodes' : mlt > 1 ? this.get_top_selected() : [obj.id] };
  6476. elm = e.currentTarget;
  6477. if (this.settings.dnd.use_html5) {
  6478. $.vakata.dnd._trigger('start', e, { 'helper': $(), 'element': elm, 'data': drg });
  6479. } else {
  6480. this.element.trigger('mousedown.jstree');
  6481. return $.vakata.dnd.start(e, drg, '<div id="jstree-dnd" class="jstree-' + this.get_theme() + ' jstree-' + this.get_theme() + '-' + this.get_theme_variant() + ' ' + ( this.settings.core.themes.responsive ? ' jstree-dnd-responsive' : '' ) + '"><i class="jstree-icon jstree-er"></i>' + txt + '<ins class="jstree-copy" style="display:none;">+</ins></div>');
  6482. }
  6483. }
  6484. }, this));
  6485. if (this.settings.dnd.use_html5) {
  6486. this.element
  6487. .on('dragover.jstree', function (e) {
  6488. e.preventDefault();
  6489. $.vakata.dnd._trigger('move', e, { 'helper': $(), 'element': elm, 'data': drg });
  6490. return false;
  6491. })
  6492. //.on('dragenter.jstree', this.settings.dnd.large_drop_target ? '.jstree-node' : '.jstree-anchor', $.proxy(function (e) {
  6493. // e.preventDefault();
  6494. // $.vakata.dnd._trigger('move', e, { 'helper': $(), 'element': elm, 'data': drg });
  6495. // return false;
  6496. // }, this))
  6497. .on('drop.jstree', $.proxy(function (e) {
  6498. e.preventDefault();
  6499. $.vakata.dnd._trigger('stop', e, { 'helper': $(), 'element': elm, 'data': drg });
  6500. return false;
  6501. }, this));
  6502. }
  6503. };
  6504. this.redraw_node = function(obj, deep, callback, force_render) {
  6505. obj = parent.redraw_node.apply(this, arguments);
  6506. if (obj && this.settings.dnd.use_html5) {
  6507. if (this.settings.dnd.large_drag_target) {
  6508. obj.setAttribute('draggable', true);
  6509. } else {
  6510. var i, j, tmp = null;
  6511. for(i = 0, j = obj.childNodes.length; i < j; i++) {
  6512. if(obj.childNodes[i] && obj.childNodes[i].className && obj.childNodes[i].className.indexOf("jstree-anchor") !== -1) {
  6513. tmp = obj.childNodes[i];
  6514. break;
  6515. }
  6516. }
  6517. if(tmp) {
  6518. tmp.setAttribute('draggable', true);
  6519. }
  6520. }
  6521. }
  6522. return obj;
  6523. };
  6524. };
  6525. $(function() {
  6526. // bind only once for all instances
  6527. var lastmv = false,
  6528. laster = false,
  6529. lastev = false,
  6530. opento = false,
  6531. marker = $('<div id="jstree-marker">&#160;</div>').hide(); //.appendTo('body');
  6532. $(document)
  6533. .on('dnd_start.vakata.jstree', function (e, data) {
  6534. lastmv = false;
  6535. lastev = false;
  6536. if(!data || !data.data || !data.data.jstree) { return; }
  6537. marker.appendTo('body'); //.show();
  6538. })
  6539. .on('dnd_move.vakata.jstree', function (e, data) {
  6540. var isDifferentNode = data.event.target !== lastev.target;
  6541. if(opento) {
  6542. if (!data.event || data.event.type !== 'dragover' || isDifferentNode) {
  6543. clearTimeout(opento);
  6544. }
  6545. }
  6546. if(!data || !data.data || !data.data.jstree) { return; }
  6547. // if we are hovering the marker image do nothing (can happen on "inside" drags)
  6548. if(data.event.target.id && data.event.target.id === 'jstree-marker') {
  6549. return;
  6550. }
  6551. lastev = data.event;
  6552. var ins = $.jstree.reference(data.event.target),
  6553. ref = false,
  6554. off = false,
  6555. rel = false,
  6556. tmp, l, t, h, p, i, o, ok, t1, t2, op, ps, pr, ip, tm, is_copy, pn;
  6557. // if we are over an instance
  6558. if(ins && ins._data && ins._data.dnd) {
  6559. marker.attr('class', 'jstree-' + ins.get_theme() + ( ins.settings.core.themes.responsive ? ' jstree-dnd-responsive' : '' ));
  6560. is_copy = data.data.origin && (data.data.origin.settings.dnd.always_copy || (data.data.origin.settings.dnd.copy && (data.event.metaKey || data.event.ctrlKey)));
  6561. data.helper
  6562. .children().attr('class', 'jstree-' + ins.get_theme() + ' jstree-' + ins.get_theme() + '-' + ins.get_theme_variant() + ' ' + ( ins.settings.core.themes.responsive ? ' jstree-dnd-responsive' : '' ))
  6563. .find('.jstree-copy').first()[ is_copy ? 'show' : 'hide' ]();
  6564. // if are hovering the container itself add a new root node
  6565. //console.log(data.event);
  6566. if( (data.event.target === ins.element[0] || data.event.target === ins.get_container_ul()[0]) && ins.get_container_ul().children().length === 0) {
  6567. ok = true;
  6568. for(t1 = 0, t2 = data.data.nodes.length; t1 < t2; t1++) {
  6569. ok = ok && ins.check( (data.data.origin && (data.data.origin.settings.dnd.always_copy || (data.data.origin.settings.dnd.copy && (data.event.metaKey || data.event.ctrlKey)) ) ? "copy_node" : "move_node"), (data.data.origin && data.data.origin !== ins ? data.data.origin.get_node(data.data.nodes[t1]) : data.data.nodes[t1]), $.jstree.root, 'last', { 'dnd' : true, 'ref' : ins.get_node($.jstree.root), 'pos' : 'i', 'origin' : data.data.origin, 'is_multi' : (data.data.origin && data.data.origin !== ins), 'is_foreign' : (!data.data.origin) });
  6570. if(!ok) { break; }
  6571. }
  6572. if(ok) {
  6573. lastmv = { 'ins' : ins, 'par' : $.jstree.root, 'pos' : 'last' };
  6574. marker.hide();
  6575. data.helper.find('.jstree-icon').first().removeClass('jstree-er').addClass('jstree-ok');
  6576. if (data.event.originalEvent && data.event.originalEvent.dataTransfer) {
  6577. data.event.originalEvent.dataTransfer.dropEffect = is_copy ? 'copy' : 'move';
  6578. }
  6579. return;
  6580. }
  6581. }
  6582. else {
  6583. // if we are hovering a tree node
  6584. ref = ins.settings.dnd.large_drop_target ? $(data.event.target).closest('.jstree-node').children('.jstree-anchor') : $(data.event.target).closest('.jstree-anchor');
  6585. if(ref && ref.length && ref.parent().is('.jstree-closed, .jstree-open, .jstree-leaf')) {
  6586. off = ref.offset();
  6587. rel = (data.event.pageY !== undefined ? data.event.pageY : data.event.originalEvent.pageY) - off.top;
  6588. h = ref.outerHeight();
  6589. if(rel < h / 3) {
  6590. o = ['b', 'i', 'a'];
  6591. }
  6592. else if(rel > h - h / 3) {
  6593. o = ['a', 'i', 'b'];
  6594. }
  6595. else {
  6596. o = rel > h / 2 ? ['i', 'a', 'b'] : ['i', 'b', 'a'];
  6597. }
  6598. $.each(o, function (j, v) {
  6599. switch(v) {
  6600. case 'b':
  6601. l = off.left - 6;
  6602. t = off.top;
  6603. p = ins.get_parent(ref);
  6604. i = ref.parent().index();
  6605. break;
  6606. case 'i':
  6607. ip = ins.settings.dnd.inside_pos;
  6608. tm = ins.get_node(ref.parent());
  6609. l = off.left - 2;
  6610. t = off.top + h / 2 + 1;
  6611. p = tm.id;
  6612. i = ip === 'first' ? 0 : (ip === 'last' ? tm.children.length : Math.min(ip, tm.children.length));
  6613. break;
  6614. case 'a':
  6615. l = off.left - 6;
  6616. t = off.top + h;
  6617. p = ins.get_parent(ref);
  6618. i = ref.parent().index() + 1;
  6619. break;
  6620. }
  6621. ok = true;
  6622. for(t1 = 0, t2 = data.data.nodes.length; t1 < t2; t1++) {
  6623. op = data.data.origin && (data.data.origin.settings.dnd.always_copy || (data.data.origin.settings.dnd.copy && (data.event.metaKey || data.event.ctrlKey))) ? "copy_node" : "move_node";
  6624. ps = i;
  6625. if(op === "move_node" && v === 'a' && (data.data.origin && data.data.origin === ins) && p === ins.get_parent(data.data.nodes[t1])) {
  6626. pr = ins.get_node(p);
  6627. if(ps > $.inArray(data.data.nodes[t1], pr.children)) {
  6628. ps -= 1;
  6629. }
  6630. }
  6631. ok = ok && ( (ins && ins.settings && ins.settings.dnd && ins.settings.dnd.check_while_dragging === false) || ins.check(op, (data.data.origin && data.data.origin !== ins ? data.data.origin.get_node(data.data.nodes[t1]) : data.data.nodes[t1]), p, ps, { 'dnd' : true, 'ref' : ins.get_node(ref.parent()), 'pos' : v, 'origin' : data.data.origin, 'is_multi' : (data.data.origin && data.data.origin !== ins), 'is_foreign' : (!data.data.origin) }) );
  6632. if(!ok) {
  6633. if(ins && ins.last_error) { laster = ins.last_error(); }
  6634. break;
  6635. }
  6636. }
  6637. if(v === 'i' && ref.parent().is('.jstree-closed') && ins.settings.dnd.open_timeout) {
  6638. if (!data.event || data.event.type !== 'dragover' || isDifferentNode) {
  6639. if (opento) { clearTimeout(opento); }
  6640. opento = setTimeout((function (x, z) { return function () { x.open_node(z); }; }(ins, ref)), ins.settings.dnd.open_timeout);
  6641. }
  6642. }
  6643. if(ok) {
  6644. pn = ins.get_node(p, true);
  6645. if (!pn.hasClass('.jstree-dnd-parent')) {
  6646. $('.jstree-dnd-parent').removeClass('jstree-dnd-parent');
  6647. pn.addClass('jstree-dnd-parent');
  6648. }
  6649. lastmv = { 'ins' : ins, 'par' : p, 'pos' : v === 'i' && ip === 'last' && i === 0 && !ins.is_loaded(tm) ? 'last' : i };
  6650. marker.css({ 'left' : l + 'px', 'top' : t + 'px' }).show();
  6651. data.helper.find('.jstree-icon').first().removeClass('jstree-er').addClass('jstree-ok');
  6652. if (data.event.originalEvent && data.event.originalEvent.dataTransfer) {
  6653. data.event.originalEvent.dataTransfer.dropEffect = is_copy ? 'copy' : 'move';
  6654. }
  6655. laster = {};
  6656. o = true;
  6657. return false;
  6658. }
  6659. });
  6660. if(o === true) { return; }
  6661. }
  6662. }
  6663. }
  6664. $('.jstree-dnd-parent').removeClass('jstree-dnd-parent');
  6665. lastmv = false;
  6666. data.helper.find('.jstree-icon').removeClass('jstree-ok').addClass('jstree-er');
  6667. if (data.event.originalEvent && data.event.originalEvent.dataTransfer) {
  6668. data.event.originalEvent.dataTransfer.dropEffect = 'none';
  6669. }
  6670. marker.hide();
  6671. })
  6672. .on('dnd_scroll.vakata.jstree', function (e, data) {
  6673. if(!data || !data.data || !data.data.jstree) { return; }
  6674. marker.hide();
  6675. lastmv = false;
  6676. lastev = false;
  6677. data.helper.find('.jstree-icon').first().removeClass('jstree-ok').addClass('jstree-er');
  6678. })
  6679. .on('dnd_stop.vakata.jstree', function (e, data) {
  6680. $('.jstree-dnd-parent').removeClass('jstree-dnd-parent');
  6681. if(opento) { clearTimeout(opento); }
  6682. if(!data || !data.data || !data.data.jstree) { return; }
  6683. marker.hide().detach();
  6684. var i, j, nodes = [];
  6685. if(lastmv) {
  6686. for(i = 0, j = data.data.nodes.length; i < j; i++) {
  6687. nodes[i] = data.data.origin ? data.data.origin.get_node(data.data.nodes[i]) : data.data.nodes[i];
  6688. }
  6689. lastmv.ins[ data.data.origin && (data.data.origin.settings.dnd.always_copy || (data.data.origin.settings.dnd.copy && (data.event.metaKey || data.event.ctrlKey))) ? 'copy_node' : 'move_node' ](nodes, lastmv.par, lastmv.pos, false, false, false, data.data.origin);
  6690. }
  6691. else {
  6692. i = $(data.event.target).closest('.jstree');
  6693. if(i.length && laster && laster.error && laster.error === 'check') {
  6694. i = i.jstree(true);
  6695. if(i) {
  6696. i.settings.core.error.call(this, laster);
  6697. }
  6698. }
  6699. }
  6700. lastev = false;
  6701. lastmv = false;
  6702. })
  6703. .on('keyup.jstree keydown.jstree', function (e, data) {
  6704. data = $.vakata.dnd._get();
  6705. if(data && data.data && data.data.jstree) {
  6706. if (e.type === "keyup" && e.which === 27) {
  6707. if (opento) { clearTimeout(opento); }
  6708. lastmv = false;
  6709. laster = false;
  6710. lastev = false;
  6711. opento = false;
  6712. marker.hide().detach();
  6713. $.vakata.dnd._clean();
  6714. } else {
  6715. data.helper.find('.jstree-copy').first()[ data.data.origin && (data.data.origin.settings.dnd.always_copy || (data.data.origin.settings.dnd.copy && (e.metaKey || e.ctrlKey))) ? 'show' : 'hide' ]();
  6716. if(lastev) {
  6717. lastev.metaKey = e.metaKey;
  6718. lastev.ctrlKey = e.ctrlKey;
  6719. $.vakata.dnd._trigger('move', lastev);
  6720. }
  6721. }
  6722. }
  6723. });
  6724. });
  6725. // helpers
  6726. (function ($) {
  6727. $.vakata.html = {
  6728. div : $('<div />'),
  6729. escape : function (str) {
  6730. return $.vakata.html.div.text(str).html();
  6731. },
  6732. strip : function (str) {
  6733. return $.vakata.html.div.empty().append($.parseHTML(str)).text();
  6734. }
  6735. };
  6736. // private variable
  6737. var vakata_dnd = {
  6738. element : false,
  6739. target : false,
  6740. is_down : false,
  6741. is_drag : false,
  6742. helper : false,
  6743. helper_w: 0,
  6744. data : false,
  6745. init_x : 0,
  6746. init_y : 0,
  6747. scroll_l: 0,
  6748. scroll_t: 0,
  6749. scroll_e: false,
  6750. scroll_i: false,
  6751. is_touch: false
  6752. };
  6753. $.vakata.dnd = {
  6754. settings : {
  6755. scroll_speed : 10,
  6756. scroll_proximity : 20,
  6757. helper_left : 5,
  6758. helper_top : 10,
  6759. threshold : 5,
  6760. threshold_touch : 50
  6761. },
  6762. _trigger : function (event_name, e, data) {
  6763. if (data === undefined) {
  6764. data = $.vakata.dnd._get();
  6765. }
  6766. data.event = e;
  6767. $(document).triggerHandler("dnd_" + event_name + ".vakata", data);
  6768. },
  6769. _get : function () {
  6770. return {
  6771. "data" : vakata_dnd.data,
  6772. "element" : vakata_dnd.element,
  6773. "helper" : vakata_dnd.helper
  6774. };
  6775. },
  6776. _clean : function () {
  6777. if(vakata_dnd.helper) { vakata_dnd.helper.remove(); }
  6778. if(vakata_dnd.scroll_i) { clearInterval(vakata_dnd.scroll_i); vakata_dnd.scroll_i = false; }
  6779. vakata_dnd = {
  6780. element : false,
  6781. target : false,
  6782. is_down : false,
  6783. is_drag : false,
  6784. helper : false,
  6785. helper_w: 0,
  6786. data : false,
  6787. init_x : 0,
  6788. init_y : 0,
  6789. scroll_l: 0,
  6790. scroll_t: 0,
  6791. scroll_e: false,
  6792. scroll_i: false,
  6793. is_touch: false
  6794. };
  6795. $(document).off("mousemove.vakata.jstree touchmove.vakata.jstree", $.vakata.dnd.drag);
  6796. $(document).off("mouseup.vakata.jstree touchend.vakata.jstree", $.vakata.dnd.stop);
  6797. },
  6798. _scroll : function (init_only) {
  6799. if(!vakata_dnd.scroll_e || (!vakata_dnd.scroll_l && !vakata_dnd.scroll_t)) {
  6800. if(vakata_dnd.scroll_i) { clearInterval(vakata_dnd.scroll_i); vakata_dnd.scroll_i = false; }
  6801. return false;
  6802. }
  6803. if(!vakata_dnd.scroll_i) {
  6804. vakata_dnd.scroll_i = setInterval($.vakata.dnd._scroll, 100);
  6805. return false;
  6806. }
  6807. if(init_only === true) { return false; }
  6808. var i = vakata_dnd.scroll_e.scrollTop(),
  6809. j = vakata_dnd.scroll_e.scrollLeft();
  6810. vakata_dnd.scroll_e.scrollTop(i + vakata_dnd.scroll_t * $.vakata.dnd.settings.scroll_speed);
  6811. vakata_dnd.scroll_e.scrollLeft(j + vakata_dnd.scroll_l * $.vakata.dnd.settings.scroll_speed);
  6812. if(i !== vakata_dnd.scroll_e.scrollTop() || j !== vakata_dnd.scroll_e.scrollLeft()) {
  6813. /**
  6814. * triggered on the document when a drag causes an element to scroll
  6815. * @event
  6816. * @plugin dnd
  6817. * @name dnd_scroll.vakata
  6818. * @param {Mixed} data any data supplied with the call to $.vakata.dnd.start
  6819. * @param {DOM} element the DOM element being dragged
  6820. * @param {jQuery} helper the helper shown next to the mouse
  6821. * @param {jQuery} event the element that is scrolling
  6822. */
  6823. $.vakata.dnd._trigger("scroll", vakata_dnd.scroll_e);
  6824. }
  6825. },
  6826. start : function (e, data, html) {
  6827. if(e.type === "touchstart" && e.originalEvent && e.originalEvent.changedTouches && e.originalEvent.changedTouches[0]) {
  6828. e.pageX = e.originalEvent.changedTouches[0].pageX;
  6829. e.pageY = e.originalEvent.changedTouches[0].pageY;
  6830. e.target = document.elementFromPoint(e.originalEvent.changedTouches[0].pageX - window.pageXOffset, e.originalEvent.changedTouches[0].pageY - window.pageYOffset);
  6831. }
  6832. if(vakata_dnd.is_drag) { $.vakata.dnd.stop({}); }
  6833. try {
  6834. e.currentTarget.unselectable = "on";
  6835. e.currentTarget.onselectstart = function() { return false; };
  6836. if(e.currentTarget.style) {
  6837. e.currentTarget.style.touchAction = "none";
  6838. e.currentTarget.style.msTouchAction = "none";
  6839. e.currentTarget.style.MozUserSelect = "none";
  6840. }
  6841. } catch(ignore) { }
  6842. vakata_dnd.init_x = e.pageX;
  6843. vakata_dnd.init_y = e.pageY;
  6844. vakata_dnd.data = data;
  6845. vakata_dnd.is_down = true;
  6846. vakata_dnd.element = e.currentTarget;
  6847. vakata_dnd.target = e.target;
  6848. vakata_dnd.is_touch = e.type === "touchstart";
  6849. if(html !== false) {
  6850. vakata_dnd.helper = $("<div id='vakata-dnd'></div>").html(html).css({
  6851. "display" : "block",
  6852. "margin" : "0",
  6853. "padding" : "0",
  6854. "position" : "absolute",
  6855. "top" : "-2000px",
  6856. "lineHeight" : "16px",
  6857. "zIndex" : "10000"
  6858. });
  6859. }
  6860. $(document).on("mousemove.vakata.jstree touchmove.vakata.jstree", $.vakata.dnd.drag);
  6861. $(document).on("mouseup.vakata.jstree touchend.vakata.jstree", $.vakata.dnd.stop);
  6862. return false;
  6863. },
  6864. drag : function (e) {
  6865. if(e.type === "touchmove" && e.originalEvent && e.originalEvent.changedTouches && e.originalEvent.changedTouches[0]) {
  6866. e.pageX = e.originalEvent.changedTouches[0].pageX;
  6867. e.pageY = e.originalEvent.changedTouches[0].pageY;
  6868. e.target = document.elementFromPoint(e.originalEvent.changedTouches[0].pageX - window.pageXOffset, e.originalEvent.changedTouches[0].pageY - window.pageYOffset);
  6869. }
  6870. if(!vakata_dnd.is_down) { return; }
  6871. if(!vakata_dnd.is_drag) {
  6872. if(
  6873. Math.abs(e.pageX - vakata_dnd.init_x) > (vakata_dnd.is_touch ? $.vakata.dnd.settings.threshold_touch : $.vakata.dnd.settings.threshold) ||
  6874. Math.abs(e.pageY - vakata_dnd.init_y) > (vakata_dnd.is_touch ? $.vakata.dnd.settings.threshold_touch : $.vakata.dnd.settings.threshold)
  6875. ) {
  6876. if(vakata_dnd.helper) {
  6877. vakata_dnd.helper.appendTo("body");
  6878. vakata_dnd.helper_w = vakata_dnd.helper.outerWidth();
  6879. }
  6880. vakata_dnd.is_drag = true;
  6881. $(vakata_dnd.target).one('click.vakata', false);
  6882. /**
  6883. * triggered on the document when a drag starts
  6884. * @event
  6885. * @plugin dnd
  6886. * @name dnd_start.vakata
  6887. * @param {Mixed} data any data supplied with the call to $.vakata.dnd.start
  6888. * @param {DOM} element the DOM element being dragged
  6889. * @param {jQuery} helper the helper shown next to the mouse
  6890. * @param {Object} event the event that caused the start (probably mousemove)
  6891. */
  6892. $.vakata.dnd._trigger("start", e);
  6893. }
  6894. else { return; }
  6895. }
  6896. var d = false, w = false,
  6897. dh = false, wh = false,
  6898. dw = false, ww = false,
  6899. dt = false, dl = false,
  6900. ht = false, hl = false;
  6901. vakata_dnd.scroll_t = 0;
  6902. vakata_dnd.scroll_l = 0;
  6903. vakata_dnd.scroll_e = false;
  6904. $($(e.target).parentsUntil("body").addBack().get().reverse())
  6905. .filter(function () {
  6906. return (/^auto|scroll$/).test($(this).css("overflow")) &&
  6907. (this.scrollHeight > this.offsetHeight || this.scrollWidth > this.offsetWidth);
  6908. })
  6909. .each(function () {
  6910. var t = $(this), o = t.offset();
  6911. if(this.scrollHeight > this.offsetHeight) {
  6912. if(o.top + t.height() - e.pageY < $.vakata.dnd.settings.scroll_proximity) { vakata_dnd.scroll_t = 1; }
  6913. if(e.pageY - o.top < $.vakata.dnd.settings.scroll_proximity) { vakata_dnd.scroll_t = -1; }
  6914. }
  6915. if(this.scrollWidth > this.offsetWidth) {
  6916. if(o.left + t.width() - e.pageX < $.vakata.dnd.settings.scroll_proximity) { vakata_dnd.scroll_l = 1; }
  6917. if(e.pageX - o.left < $.vakata.dnd.settings.scroll_proximity) { vakata_dnd.scroll_l = -1; }
  6918. }
  6919. if(vakata_dnd.scroll_t || vakata_dnd.scroll_l) {
  6920. vakata_dnd.scroll_e = $(this);
  6921. return false;
  6922. }
  6923. });
  6924. if(!vakata_dnd.scroll_e) {
  6925. d = $(document); w = $(window);
  6926. dh = d.height(); wh = w.height();
  6927. dw = d.width(); ww = w.width();
  6928. dt = d.scrollTop(); dl = d.scrollLeft();
  6929. if(dh > wh && e.pageY - dt < $.vakata.dnd.settings.scroll_proximity) { vakata_dnd.scroll_t = -1; }
  6930. if(dh > wh && wh - (e.pageY - dt) < $.vakata.dnd.settings.scroll_proximity) { vakata_dnd.scroll_t = 1; }
  6931. if(dw > ww && e.pageX - dl < $.vakata.dnd.settings.scroll_proximity) { vakata_dnd.scroll_l = -1; }
  6932. if(dw > ww && ww - (e.pageX - dl) < $.vakata.dnd.settings.scroll_proximity) { vakata_dnd.scroll_l = 1; }
  6933. if(vakata_dnd.scroll_t || vakata_dnd.scroll_l) {
  6934. vakata_dnd.scroll_e = d;
  6935. }
  6936. }
  6937. if(vakata_dnd.scroll_e) { $.vakata.dnd._scroll(true); }
  6938. if(vakata_dnd.helper) {
  6939. ht = parseInt(e.pageY + $.vakata.dnd.settings.helper_top, 10);
  6940. hl = parseInt(e.pageX + $.vakata.dnd.settings.helper_left, 10);
  6941. if(dh && ht + 25 > dh) { ht = dh - 50; }
  6942. if(dw && hl + vakata_dnd.helper_w > dw) { hl = dw - (vakata_dnd.helper_w + 2); }
  6943. vakata_dnd.helper.css({
  6944. left : hl + "px",
  6945. top : ht + "px"
  6946. });
  6947. }
  6948. /**
  6949. * triggered on the document when a drag is in progress
  6950. * @event
  6951. * @plugin dnd
  6952. * @name dnd_move.vakata
  6953. * @param {Mixed} data any data supplied with the call to $.vakata.dnd.start
  6954. * @param {DOM} element the DOM element being dragged
  6955. * @param {jQuery} helper the helper shown next to the mouse
  6956. * @param {Object} event the event that caused this to trigger (most likely mousemove)
  6957. */
  6958. $.vakata.dnd._trigger("move", e);
  6959. return false;
  6960. },
  6961. stop : function (e) {
  6962. if(e.type === "touchend" && e.originalEvent && e.originalEvent.changedTouches && e.originalEvent.changedTouches[0]) {
  6963. e.pageX = e.originalEvent.changedTouches[0].pageX;
  6964. e.pageY = e.originalEvent.changedTouches[0].pageY;
  6965. e.target = document.elementFromPoint(e.originalEvent.changedTouches[0].pageX - window.pageXOffset, e.originalEvent.changedTouches[0].pageY - window.pageYOffset);
  6966. }
  6967. if(vakata_dnd.is_drag) {
  6968. /**
  6969. * triggered on the document when a drag stops (the dragged element is dropped)
  6970. * @event
  6971. * @plugin dnd
  6972. * @name dnd_stop.vakata
  6973. * @param {Mixed} data any data supplied with the call to $.vakata.dnd.start
  6974. * @param {DOM} element the DOM element being dragged
  6975. * @param {jQuery} helper the helper shown next to the mouse
  6976. * @param {Object} event the event that caused the stop
  6977. */
  6978. if (e.target !== vakata_dnd.target) {
  6979. $(vakata_dnd.target).off('click.vakata');
  6980. }
  6981. $.vakata.dnd._trigger("stop", e);
  6982. }
  6983. else {
  6984. if(e.type === "touchend" && e.target === vakata_dnd.target) {
  6985. var to = setTimeout(function () { $(e.target).click(); }, 100);
  6986. $(e.target).one('click', function() { if(to) { clearTimeout(to); } });
  6987. }
  6988. }
  6989. $.vakata.dnd._clean();
  6990. return false;
  6991. }
  6992. };
  6993. }($));
  6994. // include the dnd plugin by default
  6995. // $.jstree.defaults.plugins.push("dnd");
  6996. /**
  6997. * ### Massload plugin
  6998. *
  6999. * Adds massload functionality to jsTree, so that multiple nodes can be loaded in a single request (only useful with lazy loading).
  7000. */
  7001. /**
  7002. * massload configuration
  7003. *
  7004. * It is possible to set this to a standard jQuery-like AJAX config.
  7005. * In addition to the standard jQuery ajax options here you can supply functions for `data` and `url`, the functions will be run in the current instance's scope and a param will be passed indicating which node IDs need to be loaded, the return value of those functions will be used.
  7006. *
  7007. * You can also set this to a function, that function will receive the node IDs being loaded as argument and a second param which is a function (callback) which should be called with the result.
  7008. *
  7009. * Both the AJAX and the function approach rely on the same return value - an object where the keys are the node IDs, and the value is the children of that node as an array.
  7010. *
  7011. * {
  7012. * "id1" : [{ "text" : "Child of ID1", "id" : "c1" }, { "text" : "Another child of ID1", "id" : "c2" }],
  7013. * "id2" : [{ "text" : "Child of ID2", "id" : "c3" }]
  7014. * }
  7015. *
  7016. * @name $.jstree.defaults.massload
  7017. * @plugin massload
  7018. */
  7019. $.jstree.defaults.massload = null;
  7020. $.jstree.plugins.massload = function (options, parent) {
  7021. this.init = function (el, options) {
  7022. this._data.massload = {};
  7023. parent.init.call(this, el, options);
  7024. };
  7025. this._load_nodes = function (nodes, callback, is_callback, force_reload) {
  7026. var s = this.settings.massload,
  7027. nodesString = JSON.stringify(nodes),
  7028. toLoad = [],
  7029. m = this._model.data,
  7030. i, j, dom;
  7031. if (!is_callback) {
  7032. for(i = 0, j = nodes.length; i < j; i++) {
  7033. if(!m[nodes[i]] || ( (!m[nodes[i]].state.loaded && !m[nodes[i]].state.failed) || force_reload) ) {
  7034. toLoad.push(nodes[i]);
  7035. dom = this.get_node(nodes[i], true);
  7036. if (dom && dom.length) {
  7037. dom.addClass("jstree-loading").attr('aria-busy',true);
  7038. }
  7039. }
  7040. }
  7041. this._data.massload = {};
  7042. if (toLoad.length) {
  7043. if($.isFunction(s)) {
  7044. return s.call(this, toLoad, $.proxy(function (data) {
  7045. var i, j;
  7046. if(data) {
  7047. for(i in data) {
  7048. if(data.hasOwnProperty(i)) {
  7049. this._data.massload[i] = data[i];
  7050. }
  7051. }
  7052. }
  7053. for(i = 0, j = nodes.length; i < j; i++) {
  7054. dom = this.get_node(nodes[i], true);
  7055. if (dom && dom.length) {
  7056. dom.removeClass("jstree-loading").attr('aria-busy',false);
  7057. }
  7058. }
  7059. parent._load_nodes.call(this, nodes, callback, is_callback, force_reload);
  7060. }, this));
  7061. }
  7062. if(typeof s === 'object' && s && s.url) {
  7063. s = $.extend(true, {}, s);
  7064. if($.isFunction(s.url)) {
  7065. s.url = s.url.call(this, toLoad);
  7066. }
  7067. if($.isFunction(s.data)) {
  7068. s.data = s.data.call(this, toLoad);
  7069. }
  7070. return $.ajax(s)
  7071. .done($.proxy(function (data,t,x) {
  7072. var i, j;
  7073. if(data) {
  7074. for(i in data) {
  7075. if(data.hasOwnProperty(i)) {
  7076. this._data.massload[i] = data[i];
  7077. }
  7078. }
  7079. }
  7080. for(i = 0, j = nodes.length; i < j; i++) {
  7081. dom = this.get_node(nodes[i], true);
  7082. if (dom && dom.length) {
  7083. dom.removeClass("jstree-loading").attr('aria-busy',false);
  7084. }
  7085. }
  7086. parent._load_nodes.call(this, nodes, callback, is_callback, force_reload);
  7087. }, this))
  7088. .fail($.proxy(function (f) {
  7089. parent._load_nodes.call(this, nodes, callback, is_callback, force_reload);
  7090. }, this));
  7091. }
  7092. }
  7093. }
  7094. return parent._load_nodes.call(this, nodes, callback, is_callback, force_reload);
  7095. };
  7096. this._load_node = function (obj, callback) {
  7097. var data = this._data.massload[obj.id],
  7098. rslt = null, dom;
  7099. if(data) {
  7100. rslt = this[typeof data === 'string' ? '_append_html_data' : '_append_json_data'](
  7101. obj,
  7102. typeof data === 'string' ? $($.parseHTML(data)).filter(function () { return this.nodeType !== 3; }) : data,
  7103. function (status) { callback.call(this, status); }
  7104. );
  7105. dom = this.get_node(obj.id, true);
  7106. if (dom && dom.length) {
  7107. dom.removeClass("jstree-loading").attr('aria-busy',false);
  7108. }
  7109. delete this._data.massload[obj.id];
  7110. return rslt;
  7111. }
  7112. return parent._load_node.call(this, obj, callback);
  7113. };
  7114. };
  7115. /**
  7116. * ### Search plugin
  7117. *
  7118. * Adds search functionality to jsTree.
  7119. */
  7120. /**
  7121. * stores all defaults for the search plugin
  7122. * @name $.jstree.defaults.search
  7123. * @plugin search
  7124. */
  7125. $.jstree.defaults.search = {
  7126. /**
  7127. * a jQuery-like AJAX config, which jstree uses if a server should be queried for results.
  7128. *
  7129. * A `str` (which is the search string) parameter will be added with the request, an optional `inside` parameter will be added if the search is limited to a node id. The expected result is a JSON array with nodes that need to be opened so that matching nodes will be revealed.
  7130. * Leave this setting as `false` to not query the server. You can also set this to a function, which will be invoked in the instance's scope and receive 3 parameters - the search string, the callback to call with the array of nodes to load, and the optional node ID to limit the search to
  7131. * @name $.jstree.defaults.search.ajax
  7132. * @plugin search
  7133. */
  7134. ajax : false,
  7135. /**
  7136. * Indicates if the search should be fuzzy or not (should `chnd3` match `child node 3`). Default is `false`.
  7137. * @name $.jstree.defaults.search.fuzzy
  7138. * @plugin search
  7139. */
  7140. fuzzy : false,
  7141. /**
  7142. * Indicates if the search should be case sensitive. Default is `false`.
  7143. * @name $.jstree.defaults.search.case_sensitive
  7144. * @plugin search
  7145. */
  7146. case_sensitive : false,
  7147. /**
  7148. * Indicates if the tree should be filtered (by default) to show only matching nodes (keep in mind this can be a heavy on large trees in old browsers).
  7149. * This setting can be changed at runtime when calling the search method. Default is `false`.
  7150. * @name $.jstree.defaults.search.show_only_matches
  7151. * @plugin search
  7152. */
  7153. show_only_matches : false,
  7154. /**
  7155. * Indicates if the children of matched element are shown (when show_only_matches is true)
  7156. * This setting can be changed at runtime when calling the search method. Default is `false`.
  7157. * @name $.jstree.defaults.search.show_only_matches_children
  7158. * @plugin search
  7159. */
  7160. show_only_matches_children : false,
  7161. /**
  7162. * Indicates if all nodes opened to reveal the search result, should be closed when the search is cleared or a new search is performed. Default is `true`.
  7163. * @name $.jstree.defaults.search.close_opened_onclear
  7164. * @plugin search
  7165. */
  7166. close_opened_onclear : true,
  7167. /**
  7168. * Indicates if only leaf nodes should be included in search results. Default is `false`.
  7169. * @name $.jstree.defaults.search.search_leaves_only
  7170. * @plugin search
  7171. */
  7172. search_leaves_only : false,
  7173. /**
  7174. * If set to a function it wil be called in the instance's scope with two arguments - search string and node (where node will be every node in the structure, so use with caution).
  7175. * If the function returns a truthy value the node will be considered a match (it might not be displayed if search_only_leaves is set to true and the node is not a leaf). Default is `false`.
  7176. * @name $.jstree.defaults.search.search_callback
  7177. * @plugin search
  7178. */
  7179. search_callback : false
  7180. };
  7181. $.jstree.plugins.search = function (options, parent) {
  7182. this.bind = function () {
  7183. parent.bind.call(this);
  7184. this._data.search.str = "";
  7185. this._data.search.dom = $();
  7186. this._data.search.res = [];
  7187. this._data.search.opn = [];
  7188. this._data.search.som = false;
  7189. this._data.search.smc = false;
  7190. this._data.search.hdn = [];
  7191. this.element
  7192. .on("search.jstree", $.proxy(function (e, data) {
  7193. if(this._data.search.som && data.res.length) {
  7194. var m = this._model.data, i, j, p = [], k, l;
  7195. for(i = 0, j = data.res.length; i < j; i++) {
  7196. if(m[data.res[i]] && !m[data.res[i]].state.hidden) {
  7197. p.push(data.res[i]);
  7198. p = p.concat(m[data.res[i]].parents);
  7199. if(this._data.search.smc) {
  7200. for (k = 0, l = m[data.res[i]].children_d.length; k < l; k++) {
  7201. if (m[m[data.res[i]].children_d[k]] && !m[m[data.res[i]].children_d[k]].state.hidden) {
  7202. p.push(m[data.res[i]].children_d[k]);
  7203. }
  7204. }
  7205. }
  7206. }
  7207. }
  7208. p = $.vakata.array_remove_item($.vakata.array_unique(p), $.jstree.root);
  7209. this._data.search.hdn = this.hide_all(true);
  7210. this.show_node(p, true);
  7211. this.redraw(true);
  7212. }
  7213. }, this))
  7214. .on("clear_search.jstree", $.proxy(function (e, data) {
  7215. if(this._data.search.som && data.res.length) {
  7216. this.show_node(this._data.search.hdn, true);
  7217. this.redraw(true);
  7218. }
  7219. }, this));
  7220. };
  7221. /**
  7222. * used to search the tree nodes for a given string
  7223. * @name search(str [, skip_async])
  7224. * @param {String} str the search string
  7225. * @param {Boolean} skip_async if set to true server will not be queried even if configured
  7226. * @param {Boolean} show_only_matches if set to true only matching nodes will be shown (keep in mind this can be very slow on large trees or old browsers)
  7227. * @param {mixed} inside an optional node to whose children to limit the search
  7228. * @param {Boolean} append if set to true the results of this search are appended to the previous search
  7229. * @plugin search
  7230. * @trigger search.jstree
  7231. */
  7232. this.search = function (str, skip_async, show_only_matches, inside, append, show_only_matches_children) {
  7233. if(str === false || $.trim(str.toString()) === "") {
  7234. return this.clear_search();
  7235. }
  7236. inside = this.get_node(inside);
  7237. inside = inside && inside.id ? inside.id : null;
  7238. str = str.toString();
  7239. var s = this.settings.search,
  7240. a = s.ajax ? s.ajax : false,
  7241. m = this._model.data,
  7242. f = null,
  7243. r = [],
  7244. p = [], i, j;
  7245. if(this._data.search.res.length && !append) {
  7246. this.clear_search();
  7247. }
  7248. if(show_only_matches === undefined) {
  7249. show_only_matches = s.show_only_matches;
  7250. }
  7251. if(show_only_matches_children === undefined) {
  7252. show_only_matches_children = s.show_only_matches_children;
  7253. }
  7254. if(!skip_async && a !== false) {
  7255. if($.isFunction(a)) {
  7256. return a.call(this, str, inside, $.proxy(function (d) { // CHANGE: added "inside" as argument
  7257. if(d && d.d) { d = d.d; }
  7258. this._load_nodes(!$.isArray(d) ? [] : $.vakata.array_unique(d), function () {
  7259. this.search(str, true, show_only_matches, inside, append, show_only_matches_children);
  7260. });
  7261. }, this), inside);
  7262. }
  7263. else {
  7264. a = $.extend({}, a);
  7265. if(!a.data) { a.data = {}; }
  7266. a.data.str = str;
  7267. if(inside) {
  7268. a.data.inside = inside;
  7269. }
  7270. if (this._data.search.lastRequest) {
  7271. this._data.search.lastRequest.abort();
  7272. }
  7273. this._data.search.lastRequest = $.ajax(a)
  7274. .fail($.proxy(function () {
  7275. this._data.core.last_error = { 'error' : 'ajax', 'plugin' : 'search', 'id' : 'search_01', 'reason' : 'Could not load search parents', 'data' : JSON.stringify(a) };
  7276. this.settings.core.error.call(this, this._data.core.last_error);
  7277. }, this))
  7278. .done($.proxy(function (d) {
  7279. if(d && d.d) { d = d.d; }
  7280. this._load_nodes(!$.isArray(d) ? [] : $.vakata.array_unique(d), function () {
  7281. this.search(str, true, show_only_matches, inside, append, show_only_matches_children);
  7282. });
  7283. }, this));
  7284. return this._data.search.lastRequest;
  7285. }
  7286. }
  7287. if(!append) {
  7288. this._data.search.str = str;
  7289. this._data.search.dom = $();
  7290. this._data.search.res = [];
  7291. this._data.search.opn = [];
  7292. this._data.search.som = show_only_matches;
  7293. this._data.search.smc = show_only_matches_children;
  7294. }
  7295. f = new $.vakata.search(str, true, { caseSensitive : s.case_sensitive, fuzzy : s.fuzzy });
  7296. $.each(m[inside ? inside : $.jstree.root].children_d, function (ii, i) {
  7297. var v = m[i];
  7298. if(v.text && !v.state.hidden && (!s.search_leaves_only || (v.state.loaded && v.children.length === 0)) && ( (s.search_callback && s.search_callback.call(this, str, v)) || (!s.search_callback && f.search(v.text).isMatch) ) ) {
  7299. r.push(i);
  7300. p = p.concat(v.parents);
  7301. }
  7302. });
  7303. if(r.length) {
  7304. p = $.vakata.array_unique(p);
  7305. for(i = 0, j = p.length; i < j; i++) {
  7306. if(p[i] !== $.jstree.root && m[p[i]] && this.open_node(p[i], null, 0) === true) {
  7307. this._data.search.opn.push(p[i]);
  7308. }
  7309. }
  7310. if(!append) {
  7311. this._data.search.dom = $(this.element[0].querySelectorAll('#' + $.map(r, function (v) { return "0123456789".indexOf(v[0]) !== -1 ? '\\3' + v[0] + ' ' + v.substr(1).replace($.jstree.idregex,'\\$&') : v.replace($.jstree.idregex,'\\$&'); }).join(', #')));
  7312. this._data.search.res = r;
  7313. }
  7314. else {
  7315. this._data.search.dom = this._data.search.dom.add($(this.element[0].querySelectorAll('#' + $.map(r, function (v) { return "0123456789".indexOf(v[0]) !== -1 ? '\\3' + v[0] + ' ' + v.substr(1).replace($.jstree.idregex,'\\$&') : v.replace($.jstree.idregex,'\\$&'); }).join(', #'))));
  7316. this._data.search.res = $.vakata.array_unique(this._data.search.res.concat(r));
  7317. }
  7318. this._data.search.dom.children(".jstree-anchor").addClass('jstree-search');
  7319. }
  7320. /**
  7321. * triggered after search is complete
  7322. * @event
  7323. * @name search.jstree
  7324. * @param {jQuery} nodes a jQuery collection of matching nodes
  7325. * @param {String} str the search string
  7326. * @param {Array} res a collection of objects represeing the matching nodes
  7327. * @plugin search
  7328. */
  7329. this.trigger('search', { nodes : this._data.search.dom, str : str, res : this._data.search.res, show_only_matches : show_only_matches });
  7330. };
  7331. /**
  7332. * used to clear the last search (removes classes and shows all nodes if filtering is on)
  7333. * @name clear_search()
  7334. * @plugin search
  7335. * @trigger clear_search.jstree
  7336. */
  7337. this.clear_search = function () {
  7338. if(this.settings.search.close_opened_onclear) {
  7339. this.close_node(this._data.search.opn, 0);
  7340. }
  7341. /**
  7342. * triggered after search is complete
  7343. * @event
  7344. * @name clear_search.jstree
  7345. * @param {jQuery} nodes a jQuery collection of matching nodes (the result from the last search)
  7346. * @param {String} str the search string (the last search string)
  7347. * @param {Array} res a collection of objects represeing the matching nodes (the result from the last search)
  7348. * @plugin search
  7349. */
  7350. this.trigger('clear_search', { 'nodes' : this._data.search.dom, str : this._data.search.str, res : this._data.search.res });
  7351. if(this._data.search.res.length) {
  7352. this._data.search.dom = $(this.element[0].querySelectorAll('#' + $.map(this._data.search.res, function (v) {
  7353. return "0123456789".indexOf(v[0]) !== -1 ? '\\3' + v[0] + ' ' + v.substr(1).replace($.jstree.idregex,'\\$&') : v.replace($.jstree.idregex,'\\$&');
  7354. }).join(', #')));
  7355. this._data.search.dom.children(".jstree-anchor").removeClass("jstree-search");
  7356. }
  7357. this._data.search.str = "";
  7358. this._data.search.res = [];
  7359. this._data.search.opn = [];
  7360. this._data.search.dom = $();
  7361. };
  7362. this.redraw_node = function(obj, deep, callback, force_render) {
  7363. obj = parent.redraw_node.apply(this, arguments);
  7364. if(obj) {
  7365. if($.inArray(obj.id, this._data.search.res) !== -1) {
  7366. var i, j, tmp = null;
  7367. for(i = 0, j = obj.childNodes.length; i < j; i++) {
  7368. if(obj.childNodes[i] && obj.childNodes[i].className && obj.childNodes[i].className.indexOf("jstree-anchor") !== -1) {
  7369. tmp = obj.childNodes[i];
  7370. break;
  7371. }
  7372. }
  7373. if(tmp) {
  7374. tmp.className += ' jstree-search';
  7375. }
  7376. }
  7377. }
  7378. return obj;
  7379. };
  7380. };
  7381. // helpers
  7382. (function ($) {
  7383. // from http://kiro.me/projects/fuse.html
  7384. $.vakata.search = function(pattern, txt, options) {
  7385. options = options || {};
  7386. options = $.extend({}, $.vakata.search.defaults, options);
  7387. if(options.fuzzy !== false) {
  7388. options.fuzzy = true;
  7389. }
  7390. pattern = options.caseSensitive ? pattern : pattern.toLowerCase();
  7391. var MATCH_LOCATION = options.location,
  7392. MATCH_DISTANCE = options.distance,
  7393. MATCH_THRESHOLD = options.threshold,
  7394. patternLen = pattern.length,
  7395. matchmask, pattern_alphabet, match_bitapScore, search;
  7396. if(patternLen > 32) {
  7397. options.fuzzy = false;
  7398. }
  7399. if(options.fuzzy) {
  7400. matchmask = 1 << (patternLen - 1);
  7401. pattern_alphabet = (function () {
  7402. var mask = {},
  7403. i = 0;
  7404. for (i = 0; i < patternLen; i++) {
  7405. mask[pattern.charAt(i)] = 0;
  7406. }
  7407. for (i = 0; i < patternLen; i++) {
  7408. mask[pattern.charAt(i)] |= 1 << (patternLen - i - 1);
  7409. }
  7410. return mask;
  7411. }());
  7412. match_bitapScore = function (e, x) {
  7413. var accuracy = e / patternLen,
  7414. proximity = Math.abs(MATCH_LOCATION - x);
  7415. if(!MATCH_DISTANCE) {
  7416. return proximity ? 1.0 : accuracy;
  7417. }
  7418. return accuracy + (proximity / MATCH_DISTANCE);
  7419. };
  7420. }
  7421. search = function (text) {
  7422. text = options.caseSensitive ? text : text.toLowerCase();
  7423. if(pattern === text || text.indexOf(pattern) !== -1) {
  7424. return {
  7425. isMatch: true,
  7426. score: 0
  7427. };
  7428. }
  7429. if(!options.fuzzy) {
  7430. return {
  7431. isMatch: false,
  7432. score: 1
  7433. };
  7434. }
  7435. var i, j,
  7436. textLen = text.length,
  7437. scoreThreshold = MATCH_THRESHOLD,
  7438. bestLoc = text.indexOf(pattern, MATCH_LOCATION),
  7439. binMin, binMid,
  7440. binMax = patternLen + textLen,
  7441. lastRd, start, finish, rd, charMatch,
  7442. score = 1,
  7443. locations = [];
  7444. if (bestLoc !== -1) {
  7445. scoreThreshold = Math.min(match_bitapScore(0, bestLoc), scoreThreshold);
  7446. bestLoc = text.lastIndexOf(pattern, MATCH_LOCATION + patternLen);
  7447. if (bestLoc !== -1) {
  7448. scoreThreshold = Math.min(match_bitapScore(0, bestLoc), scoreThreshold);
  7449. }
  7450. }
  7451. bestLoc = -1;
  7452. for (i = 0; i < patternLen; i++) {
  7453. binMin = 0;
  7454. binMid = binMax;
  7455. while (binMin < binMid) {
  7456. if (match_bitapScore(i, MATCH_LOCATION + binMid) <= scoreThreshold) {
  7457. binMin = binMid;
  7458. } else {
  7459. binMax = binMid;
  7460. }
  7461. binMid = Math.floor((binMax - binMin) / 2 + binMin);
  7462. }
  7463. binMax = binMid;
  7464. start = Math.max(1, MATCH_LOCATION - binMid + 1);
  7465. finish = Math.min(MATCH_LOCATION + binMid, textLen) + patternLen;
  7466. rd = new Array(finish + 2);
  7467. rd[finish + 1] = (1 << i) - 1;
  7468. for (j = finish; j >= start; j--) {
  7469. charMatch = pattern_alphabet[text.charAt(j - 1)];
  7470. if (i === 0) {
  7471. rd[j] = ((rd[j + 1] << 1) | 1) & charMatch;
  7472. } else {
  7473. rd[j] = ((rd[j + 1] << 1) | 1) & charMatch | (((lastRd[j + 1] | lastRd[j]) << 1) | 1) | lastRd[j + 1];
  7474. }
  7475. if (rd[j] & matchmask) {
  7476. score = match_bitapScore(i, j - 1);
  7477. if (score <= scoreThreshold) {
  7478. scoreThreshold = score;
  7479. bestLoc = j - 1;
  7480. locations.push(bestLoc);
  7481. if (bestLoc > MATCH_LOCATION) {
  7482. start = Math.max(1, 2 * MATCH_LOCATION - bestLoc);
  7483. } else {
  7484. break;
  7485. }
  7486. }
  7487. }
  7488. }
  7489. if (match_bitapScore(i + 1, MATCH_LOCATION) > scoreThreshold) {
  7490. break;
  7491. }
  7492. lastRd = rd;
  7493. }
  7494. return {
  7495. isMatch: bestLoc >= 0,
  7496. score: score
  7497. };
  7498. };
  7499. return txt === true ? { 'search' : search } : search(txt);
  7500. };
  7501. $.vakata.search.defaults = {
  7502. location : 0,
  7503. distance : 100,
  7504. threshold : 0.6,
  7505. fuzzy : false,
  7506. caseSensitive : false
  7507. };
  7508. }($));
  7509. // include the search plugin by default
  7510. // $.jstree.defaults.plugins.push("search");
  7511. /**
  7512. * ### Sort plugin
  7513. *
  7514. * Automatically sorts all siblings in the tree according to a sorting function.
  7515. */
  7516. /**
  7517. * the settings function used to sort the nodes.
  7518. * It is executed in the tree's context, accepts two nodes as arguments and should return `1` or `-1`.
  7519. * @name $.jstree.defaults.sort
  7520. * @plugin sort
  7521. */
  7522. $.jstree.defaults.sort = function (a, b) {
  7523. //return this.get_type(a) === this.get_type(b) ? (this.get_text(a) > this.get_text(b) ? 1 : -1) : this.get_type(a) >= this.get_type(b);
  7524. return this.get_text(a) > this.get_text(b) ? 1 : -1;
  7525. };
  7526. $.jstree.plugins.sort = function (options, parent) {
  7527. this.bind = function () {
  7528. parent.bind.call(this);
  7529. this.element
  7530. .on("model.jstree", $.proxy(function (e, data) {
  7531. this.sort(data.parent, true);
  7532. }, this))
  7533. .on("rename_node.jstree create_node.jstree", $.proxy(function (e, data) {
  7534. this.sort(data.parent || data.node.parent, false);
  7535. this.redraw_node(data.parent || data.node.parent, true);
  7536. }, this))
  7537. .on("move_node.jstree copy_node.jstree", $.proxy(function (e, data) {
  7538. this.sort(data.parent, false);
  7539. this.redraw_node(data.parent, true);
  7540. }, this));
  7541. };
  7542. /**
  7543. * used to sort a node's children
  7544. * @private
  7545. * @name sort(obj [, deep])
  7546. * @param {mixed} obj the node
  7547. * @param {Boolean} deep if set to `true` nodes are sorted recursively.
  7548. * @plugin sort
  7549. * @trigger search.jstree
  7550. */
  7551. this.sort = function (obj, deep) {
  7552. var i, j;
  7553. obj = this.get_node(obj);
  7554. if(obj && obj.children && obj.children.length) {
  7555. obj.children.sort($.proxy(this.settings.sort, this));
  7556. if(deep) {
  7557. for(i = 0, j = obj.children_d.length; i < j; i++) {
  7558. this.sort(obj.children_d[i], false);
  7559. }
  7560. }
  7561. }
  7562. };
  7563. };
  7564. // include the sort plugin by default
  7565. // $.jstree.defaults.plugins.push("sort");
  7566. /**
  7567. * ### State plugin
  7568. *
  7569. * Saves the state of the tree (selected nodes, opened nodes) on the user's computer using available options (localStorage, cookies, etc)
  7570. */
  7571. var to = false;
  7572. /**
  7573. * stores all defaults for the state plugin
  7574. * @name $.jstree.defaults.state
  7575. * @plugin state
  7576. */
  7577. $.jstree.defaults.state = {
  7578. /**
  7579. * A string for the key to use when saving the current tree (change if using multiple trees in your project). Defaults to `jstree`.
  7580. * @name $.jstree.defaults.state.key
  7581. * @plugin state
  7582. */
  7583. key : 'jstree',
  7584. /**
  7585. * A space separated list of events that trigger a state save. Defaults to `changed.jstree open_node.jstree close_node.jstree`.
  7586. * @name $.jstree.defaults.state.events
  7587. * @plugin state
  7588. */
  7589. events : 'changed.jstree open_node.jstree close_node.jstree check_node.jstree uncheck_node.jstree',
  7590. /**
  7591. * Time in milliseconds after which the state will expire. Defaults to 'false' meaning - no expire.
  7592. * @name $.jstree.defaults.state.ttl
  7593. * @plugin state
  7594. */
  7595. ttl : false,
  7596. /**
  7597. * A function that will be executed prior to restoring state with one argument - the state object. Can be used to clear unwanted parts of the state.
  7598. * @name $.jstree.defaults.state.filter
  7599. * @plugin state
  7600. */
  7601. filter : false
  7602. };
  7603. $.jstree.plugins.state = function (options, parent) {
  7604. this.bind = function () {
  7605. parent.bind.call(this);
  7606. var bind = $.proxy(function () {
  7607. this.element.on(this.settings.state.events, $.proxy(function () {
  7608. if(to) { clearTimeout(to); }
  7609. to = setTimeout($.proxy(function () { this.save_state(); }, this), 100);
  7610. }, this));
  7611. /**
  7612. * triggered when the state plugin is finished restoring the state (and immediately after ready if there is no state to restore).
  7613. * @event
  7614. * @name state_ready.jstree
  7615. * @plugin state
  7616. */
  7617. this.trigger('state_ready');
  7618. }, this);
  7619. this.element
  7620. .on("ready.jstree", $.proxy(function (e, data) {
  7621. this.element.one("restore_state.jstree", bind);
  7622. if(!this.restore_state()) { bind(); }
  7623. }, this));
  7624. };
  7625. /**
  7626. * save the state
  7627. * @name save_state()
  7628. * @plugin state
  7629. */
  7630. this.save_state = function () {
  7631. var st = { 'state' : this.get_state(), 'ttl' : this.settings.state.ttl, 'sec' : +(new Date()) };
  7632. $.vakata.storage.set(this.settings.state.key, JSON.stringify(st));
  7633. };
  7634. /**
  7635. * restore the state from the user's computer
  7636. * @name restore_state()
  7637. * @plugin state
  7638. */
  7639. this.restore_state = function () {
  7640. var k = $.vakata.storage.get(this.settings.state.key);
  7641. if(!!k) { try { k = JSON.parse(k); } catch(ex) { return false; } }
  7642. if(!!k && k.ttl && k.sec && +(new Date()) - k.sec > k.ttl) { return false; }
  7643. if(!!k && k.state) { k = k.state; }
  7644. if(!!k && $.isFunction(this.settings.state.filter)) { k = this.settings.state.filter.call(this, k); }
  7645. if(!!k) {
  7646. this.element.one("set_state.jstree", function (e, data) { data.instance.trigger('restore_state', { 'state' : $.extend(true, {}, k) }); });
  7647. this.set_state(k);
  7648. return true;
  7649. }
  7650. return false;
  7651. };
  7652. /**
  7653. * clear the state on the user's computer
  7654. * @name clear_state()
  7655. * @plugin state
  7656. */
  7657. this.clear_state = function () {
  7658. return $.vakata.storage.del(this.settings.state.key);
  7659. };
  7660. };
  7661. (function ($, undefined) {
  7662. $.vakata.storage = {
  7663. // simply specifying the functions in FF throws an error
  7664. set : function (key, val) { return window.localStorage.setItem(key, val); },
  7665. get : function (key) { return window.localStorage.getItem(key); },
  7666. del : function (key) { return window.localStorage.removeItem(key); }
  7667. };
  7668. }($));
  7669. // include the state plugin by default
  7670. // $.jstree.defaults.plugins.push("state");
  7671. /**
  7672. * ### Types plugin
  7673. *
  7674. * Makes it possible to add predefined types for groups of nodes, which make it possible to easily control nesting rules and icon for each group.
  7675. */
  7676. /**
  7677. * An object storing all types as key value pairs, where the key is the type name and the value is an object that could contain following keys (all optional).
  7678. *
  7679. * * `max_children` the maximum number of immediate children this node type can have. Do not specify or set to `-1` for unlimited.
  7680. * * `max_depth` the maximum number of nesting this node type can have. A value of `1` would mean that the node can have children, but no grandchildren. Do not specify or set to `-1` for unlimited.
  7681. * * `valid_children` an array of node type strings, that nodes of this type can have as children. Do not specify or set to `-1` for no limits.
  7682. * * `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. Omit to use the default icon from your theme.
  7683. * * `li_attr` an object of values which will be used to add HTML attributes on the resulting LI DOM node (merged with the node's own data)
  7684. * * `a_attr` an object of values which will be used to add HTML attributes on the resulting A DOM node (merged with the node's own data)
  7685. *
  7686. * There are two predefined types:
  7687. *
  7688. * * `#` represents the root of the tree, for example `max_children` would control the maximum number of root nodes.
  7689. * * `default` represents the default node - any settings here will be applied to all nodes that do not have a type specified.
  7690. *
  7691. * @name $.jstree.defaults.types
  7692. * @plugin types
  7693. */
  7694. $.jstree.defaults.types = {
  7695. 'default' : {}
  7696. };
  7697. $.jstree.defaults.types[$.jstree.root] = {};
  7698. $.jstree.plugins.types = function (options, parent) {
  7699. this.init = function (el, options) {
  7700. var i, j;
  7701. if(options && options.types && options.types['default']) {
  7702. for(i in options.types) {
  7703. if(i !== "default" && i !== $.jstree.root && options.types.hasOwnProperty(i)) {
  7704. for(j in options.types['default']) {
  7705. if(options.types['default'].hasOwnProperty(j) && options.types[i][j] === undefined) {
  7706. options.types[i][j] = options.types['default'][j];
  7707. }
  7708. }
  7709. }
  7710. }
  7711. }
  7712. parent.init.call(this, el, options);
  7713. this._model.data[$.jstree.root].type = $.jstree.root;
  7714. };
  7715. this.refresh = function (skip_loading, forget_state) {
  7716. parent.refresh.call(this, skip_loading, forget_state);
  7717. this._model.data[$.jstree.root].type = $.jstree.root;
  7718. };
  7719. this.bind = function () {
  7720. this.element
  7721. .on('model.jstree', $.proxy(function (e, data) {
  7722. var m = this._model.data,
  7723. dpc = data.nodes,
  7724. t = this.settings.types,
  7725. i, j, c = 'default', k;
  7726. for(i = 0, j = dpc.length; i < j; i++) {
  7727. c = 'default';
  7728. if(m[dpc[i]].original && m[dpc[i]].original.type && t[m[dpc[i]].original.type]) {
  7729. c = m[dpc[i]].original.type;
  7730. }
  7731. if(m[dpc[i]].data && m[dpc[i]].data.jstree && m[dpc[i]].data.jstree.type && t[m[dpc[i]].data.jstree.type]) {
  7732. c = m[dpc[i]].data.jstree.type;
  7733. }
  7734. m[dpc[i]].type = c;
  7735. if(m[dpc[i]].icon === true && t[c].icon !== undefined) {
  7736. m[dpc[i]].icon = t[c].icon;
  7737. }
  7738. if(t[c].li_attr !== undefined && typeof t[c].li_attr === 'object') {
  7739. for (k in t[c].li_attr) {
  7740. if (t[c].li_attr.hasOwnProperty(k)) {
  7741. if (k === 'id') {
  7742. continue;
  7743. }
  7744. else if (m[dpc[i]].li_attr[k] === undefined) {
  7745. m[dpc[i]].li_attr[k] = t[c].li_attr[k];
  7746. }
  7747. else if (k === 'class') {
  7748. m[dpc[i]].li_attr['class'] = t[c].li_attr['class'] + ' ' + m[dpc[i]].li_attr['class'];
  7749. }
  7750. }
  7751. }
  7752. }
  7753. if(t[c].a_attr !== undefined && typeof t[c].a_attr === 'object') {
  7754. for (k in t[c].a_attr) {
  7755. if (t[c].a_attr.hasOwnProperty(k)) {
  7756. if (k === 'id') {
  7757. continue;
  7758. }
  7759. else if (m[dpc[i]].a_attr[k] === undefined) {
  7760. m[dpc[i]].a_attr[k] = t[c].a_attr[k];
  7761. }
  7762. else if (k === 'href' && m[dpc[i]].a_attr[k] === '#') {
  7763. m[dpc[i]].a_attr['href'] = t[c].a_attr['href'];
  7764. }
  7765. else if (k === 'class') {
  7766. m[dpc[i]].a_attr['class'] = t[c].a_attr['class'] + ' ' + m[dpc[i]].a_attr['class'];
  7767. }
  7768. }
  7769. }
  7770. }
  7771. }
  7772. m[$.jstree.root].type = $.jstree.root;
  7773. }, this));
  7774. parent.bind.call(this);
  7775. };
  7776. this.get_json = function (obj, options, flat) {
  7777. var i, j,
  7778. m = this._model.data,
  7779. opt = options ? $.extend(true, {}, options, {no_id:false}) : {},
  7780. tmp = parent.get_json.call(this, obj, opt, flat);
  7781. if(tmp === false) { return false; }
  7782. if($.isArray(tmp)) {
  7783. for(i = 0, j = tmp.length; i < j; i++) {
  7784. tmp[i].type = tmp[i].id && m[tmp[i].id] && m[tmp[i].id].type ? m[tmp[i].id].type : "default";
  7785. if(options && options.no_id) {
  7786. delete tmp[i].id;
  7787. if(tmp[i].li_attr && tmp[i].li_attr.id) {
  7788. delete tmp[i].li_attr.id;
  7789. }
  7790. if(tmp[i].a_attr && tmp[i].a_attr.id) {
  7791. delete tmp[i].a_attr.id;
  7792. }
  7793. }
  7794. }
  7795. }
  7796. else {
  7797. tmp.type = tmp.id && m[tmp.id] && m[tmp.id].type ? m[tmp.id].type : "default";
  7798. if(options && options.no_id) {
  7799. tmp = this._delete_ids(tmp);
  7800. }
  7801. }
  7802. return tmp;
  7803. };
  7804. this._delete_ids = function (tmp) {
  7805. if($.isArray(tmp)) {
  7806. for(var i = 0, j = tmp.length; i < j; i++) {
  7807. tmp[i] = this._delete_ids(tmp[i]);
  7808. }
  7809. return tmp;
  7810. }
  7811. delete tmp.id;
  7812. if(tmp.li_attr && tmp.li_attr.id) {
  7813. delete tmp.li_attr.id;
  7814. }
  7815. if(tmp.a_attr && tmp.a_attr.id) {
  7816. delete tmp.a_attr.id;
  7817. }
  7818. if(tmp.children && $.isArray(tmp.children)) {
  7819. tmp.children = this._delete_ids(tmp.children);
  7820. }
  7821. return tmp;
  7822. };
  7823. this.check = function (chk, obj, par, pos, more) {
  7824. if(parent.check.call(this, chk, obj, par, pos, more) === false) { return false; }
  7825. obj = obj && obj.id ? obj : this.get_node(obj);
  7826. par = par && par.id ? par : this.get_node(par);
  7827. var m = obj && obj.id ? (more && more.origin ? more.origin : $.jstree.reference(obj.id)) : null, tmp, d, i, j;
  7828. m = m && m._model && m._model.data ? m._model.data : null;
  7829. switch(chk) {
  7830. case "create_node":
  7831. case "move_node":
  7832. case "copy_node":
  7833. if(chk !== 'move_node' || $.inArray(obj.id, par.children) === -1) {
  7834. tmp = this.get_rules(par);
  7835. if(tmp.max_children !== undefined && tmp.max_children !== -1 && tmp.max_children === par.children.length) {
  7836. this._data.core.last_error = { 'error' : 'check', 'plugin' : 'types', 'id' : 'types_01', 'reason' : 'max_children prevents function: ' + chk, 'data' : JSON.stringify({ 'chk' : chk, 'pos' : pos, 'obj' : obj && obj.id ? obj.id : false, 'par' : par && par.id ? par.id : false }) };
  7837. return false;
  7838. }
  7839. if(tmp.valid_children !== undefined && tmp.valid_children !== -1 && $.inArray((obj.type || 'default'), tmp.valid_children) === -1) {
  7840. this._data.core.last_error = { 'error' : 'check', 'plugin' : 'types', 'id' : 'types_02', 'reason' : 'valid_children prevents function: ' + chk, 'data' : JSON.stringify({ 'chk' : chk, 'pos' : pos, 'obj' : obj && obj.id ? obj.id : false, 'par' : par && par.id ? par.id : false }) };
  7841. return false;
  7842. }
  7843. if(m && obj.children_d && obj.parents) {
  7844. d = 0;
  7845. for(i = 0, j = obj.children_d.length; i < j; i++) {
  7846. d = Math.max(d, m[obj.children_d[i]].parents.length);
  7847. }
  7848. d = d - obj.parents.length + 1;
  7849. }
  7850. if(d <= 0 || d === undefined) { d = 1; }
  7851. do {
  7852. if(tmp.max_depth !== undefined && tmp.max_depth !== -1 && tmp.max_depth < d) {
  7853. this._data.core.last_error = { 'error' : 'check', 'plugin' : 'types', 'id' : 'types_03', 'reason' : 'max_depth prevents function: ' + chk, 'data' : JSON.stringify({ 'chk' : chk, 'pos' : pos, 'obj' : obj && obj.id ? obj.id : false, 'par' : par && par.id ? par.id : false }) };
  7854. return false;
  7855. }
  7856. par = this.get_node(par.parent);
  7857. tmp = this.get_rules(par);
  7858. d++;
  7859. } while(par);
  7860. }
  7861. break;
  7862. }
  7863. return true;
  7864. };
  7865. /**
  7866. * used to retrieve the type settings object for a node
  7867. * @name get_rules(obj)
  7868. * @param {mixed} obj the node to find the rules for
  7869. * @return {Object}
  7870. * @plugin types
  7871. */
  7872. this.get_rules = function (obj) {
  7873. obj = this.get_node(obj);
  7874. if(!obj) { return false; }
  7875. var tmp = this.get_type(obj, true);
  7876. if(tmp.max_depth === undefined) { tmp.max_depth = -1; }
  7877. if(tmp.max_children === undefined) { tmp.max_children = -1; }
  7878. if(tmp.valid_children === undefined) { tmp.valid_children = -1; }
  7879. return tmp;
  7880. };
  7881. /**
  7882. * used to retrieve the type string or settings object for a node
  7883. * @name get_type(obj [, rules])
  7884. * @param {mixed} obj the node to find the rules for
  7885. * @param {Boolean} rules if set to `true` instead of a string the settings object will be returned
  7886. * @return {String|Object}
  7887. * @plugin types
  7888. */
  7889. this.get_type = function (obj, rules) {
  7890. obj = this.get_node(obj);
  7891. return (!obj) ? false : ( rules ? $.extend({ 'type' : obj.type }, this.settings.types[obj.type]) : obj.type);
  7892. };
  7893. /**
  7894. * used to change a node's type
  7895. * @name set_type(obj, type)
  7896. * @param {mixed} obj the node to change
  7897. * @param {String} type the new type
  7898. * @plugin types
  7899. */
  7900. this.set_type = function (obj, type) {
  7901. var m = this._model.data, t, t1, t2, old_type, old_icon, k, d, a;
  7902. if($.isArray(obj)) {
  7903. obj = obj.slice();
  7904. for(t1 = 0, t2 = obj.length; t1 < t2; t1++) {
  7905. this.set_type(obj[t1], type);
  7906. }
  7907. return true;
  7908. }
  7909. t = this.settings.types;
  7910. obj = this.get_node(obj);
  7911. if(!t[type] || !obj) { return false; }
  7912. d = this.get_node(obj, true);
  7913. if (d && d.length) {
  7914. a = d.children('.jstree-anchor');
  7915. }
  7916. old_type = obj.type;
  7917. old_icon = this.get_icon(obj);
  7918. obj.type = type;
  7919. if(old_icon === true || !t[old_type] || (t[old_type].icon !== undefined && old_icon === t[old_type].icon)) {
  7920. this.set_icon(obj, t[type].icon !== undefined ? t[type].icon : true);
  7921. }
  7922. // remove old type props
  7923. if(t[old_type] && t[old_type].li_attr !== undefined && typeof t[old_type].li_attr === 'object') {
  7924. for (k in t[old_type].li_attr) {
  7925. if (t[old_type].li_attr.hasOwnProperty(k)) {
  7926. if (k === 'id') {
  7927. continue;
  7928. }
  7929. else if (k === 'class') {
  7930. m[obj.id].li_attr['class'] = (m[obj.id].li_attr['class'] || '').replace(t[old_type].li_attr[k], '');
  7931. if (d) { d.removeClass(t[old_type].li_attr[k]); }
  7932. }
  7933. else if (m[obj.id].li_attr[k] === t[old_type].li_attr[k]) {
  7934. m[obj.id].li_attr[k] = null;
  7935. if (d) { d.removeAttr(k); }
  7936. }
  7937. }
  7938. }
  7939. }
  7940. if(t[old_type] && t[old_type].a_attr !== undefined && typeof t[old_type].a_attr === 'object') {
  7941. for (k in t[old_type].a_attr) {
  7942. if (t[old_type].a_attr.hasOwnProperty(k)) {
  7943. if (k === 'id') {
  7944. continue;
  7945. }
  7946. else if (k === 'class') {
  7947. m[obj.id].a_attr['class'] = (m[obj.id].a_attr['class'] || '').replace(t[old_type].a_attr[k], '');
  7948. if (a) { a.removeClass(t[old_type].a_attr[k]); }
  7949. }
  7950. else if (m[obj.id].a_attr[k] === t[old_type].a_attr[k]) {
  7951. if (k === 'href') {
  7952. m[obj.id].a_attr[k] = '#';
  7953. if (a) { a.attr('href', '#'); }
  7954. }
  7955. else {
  7956. delete m[obj.id].a_attr[k];
  7957. if (a) { a.removeAttr(k); }
  7958. }
  7959. }
  7960. }
  7961. }
  7962. }
  7963. // add new props
  7964. if(t[type].li_attr !== undefined && typeof t[type].li_attr === 'object') {
  7965. for (k in t[type].li_attr) {
  7966. if (t[type].li_attr.hasOwnProperty(k)) {
  7967. if (k === 'id') {
  7968. continue;
  7969. }
  7970. else if (m[obj.id].li_attr[k] === undefined) {
  7971. m[obj.id].li_attr[k] = t[type].li_attr[k];
  7972. if (d) {
  7973. if (k === 'class') {
  7974. d.addClass(t[type].li_attr[k]);
  7975. }
  7976. else {
  7977. d.attr(k, t[type].li_attr[k]);
  7978. }
  7979. }
  7980. }
  7981. else if (k === 'class') {
  7982. m[obj.id].li_attr['class'] = t[type].li_attr[k] + ' ' + m[obj.id].li_attr['class'];
  7983. if (d) { d.addClass(t[type].li_attr[k]); }
  7984. }
  7985. }
  7986. }
  7987. }
  7988. if(t[type].a_attr !== undefined && typeof t[type].a_attr === 'object') {
  7989. for (k in t[type].a_attr) {
  7990. if (t[type].a_attr.hasOwnProperty(k)) {
  7991. if (k === 'id') {
  7992. continue;
  7993. }
  7994. else if (m[obj.id].a_attr[k] === undefined) {
  7995. m[obj.id].a_attr[k] = t[type].a_attr[k];
  7996. if (a) {
  7997. if (k === 'class') {
  7998. a.addClass(t[type].a_attr[k]);
  7999. }
  8000. else {
  8001. a.attr(k, t[type].a_attr[k]);
  8002. }
  8003. }
  8004. }
  8005. else if (k === 'href' && m[obj.id].a_attr[k] === '#') {
  8006. m[obj.id].a_attr['href'] = t[type].a_attr['href'];
  8007. if (a) { a.attr('href', t[type].a_attr['href']); }
  8008. }
  8009. else if (k === 'class') {
  8010. m[obj.id].a_attr['class'] = t[type].a_attr['class'] + ' ' + m[obj.id].a_attr['class'];
  8011. if (a) { a.addClass(t[type].a_attr[k]); }
  8012. }
  8013. }
  8014. }
  8015. }
  8016. return true;
  8017. };
  8018. };
  8019. // include the types plugin by default
  8020. // $.jstree.defaults.plugins.push("types");
  8021. /**
  8022. * ### Unique plugin
  8023. *
  8024. * Enforces that no nodes with the same name can coexist as siblings.
  8025. */
  8026. /**
  8027. * stores all defaults for the unique plugin
  8028. * @name $.jstree.defaults.unique
  8029. * @plugin unique
  8030. */
  8031. $.jstree.defaults.unique = {
  8032. /**
  8033. * Indicates if the comparison should be case sensitive. Default is `false`.
  8034. * @name $.jstree.defaults.unique.case_sensitive
  8035. * @plugin unique
  8036. */
  8037. case_sensitive : false,
  8038. /**
  8039. * A callback executed in the instance's scope when a new node is created and the name is already taken, the two arguments are the conflicting name and the counter. The default will produce results like `New node (2)`.
  8040. * @name $.jstree.defaults.unique.duplicate
  8041. * @plugin unique
  8042. */
  8043. duplicate : function (name, counter) {
  8044. return name + ' (' + counter + ')';
  8045. }
  8046. };
  8047. $.jstree.plugins.unique = function (options, parent) {
  8048. this.check = function (chk, obj, par, pos, more) {
  8049. if(parent.check.call(this, chk, obj, par, pos, more) === false) { return false; }
  8050. obj = obj && obj.id ? obj : this.get_node(obj);
  8051. par = par && par.id ? par : this.get_node(par);
  8052. if(!par || !par.children) { return true; }
  8053. var n = chk === "rename_node" ? pos : obj.text,
  8054. c = [],
  8055. s = this.settings.unique.case_sensitive,
  8056. m = this._model.data, i, j;
  8057. for(i = 0, j = par.children.length; i < j; i++) {
  8058. c.push(s ? m[par.children[i]].text : m[par.children[i]].text.toLowerCase());
  8059. }
  8060. if(!s) { n = n.toLowerCase(); }
  8061. switch(chk) {
  8062. case "delete_node":
  8063. return true;
  8064. case "rename_node":
  8065. i = ($.inArray(n, c) === -1 || (obj.text && obj.text[ s ? 'toString' : 'toLowerCase']() === n));
  8066. if(!i) {
  8067. this._data.core.last_error = { 'error' : 'check', 'plugin' : 'unique', 'id' : 'unique_01', 'reason' : 'Child with name ' + n + ' already exists. Preventing: ' + chk, 'data' : JSON.stringify({ 'chk' : chk, 'pos' : pos, 'obj' : obj && obj.id ? obj.id : false, 'par' : par && par.id ? par.id : false }) };
  8068. }
  8069. return i;
  8070. case "create_node":
  8071. i = ($.inArray(n, c) === -1);
  8072. if(!i) {
  8073. this._data.core.last_error = { 'error' : 'check', 'plugin' : 'unique', 'id' : 'unique_04', 'reason' : 'Child with name ' + n + ' already exists. Preventing: ' + chk, 'data' : JSON.stringify({ 'chk' : chk, 'pos' : pos, 'obj' : obj && obj.id ? obj.id : false, 'par' : par && par.id ? par.id : false }) };
  8074. }
  8075. return i;
  8076. case "copy_node":
  8077. i = ($.inArray(n, c) === -1);
  8078. if(!i) {
  8079. this._data.core.last_error = { 'error' : 'check', 'plugin' : 'unique', 'id' : 'unique_02', 'reason' : 'Child with name ' + n + ' already exists. Preventing: ' + chk, 'data' : JSON.stringify({ 'chk' : chk, 'pos' : pos, 'obj' : obj && obj.id ? obj.id : false, 'par' : par && par.id ? par.id : false }) };
  8080. }
  8081. return i;
  8082. case "move_node":
  8083. i = ( (obj.parent === par.id && (!more || !more.is_multi)) || $.inArray(n, c) === -1);
  8084. if(!i) {
  8085. this._data.core.last_error = { 'error' : 'check', 'plugin' : 'unique', 'id' : 'unique_03', 'reason' : 'Child with name ' + n + ' already exists. Preventing: ' + chk, 'data' : JSON.stringify({ 'chk' : chk, 'pos' : pos, 'obj' : obj && obj.id ? obj.id : false, 'par' : par && par.id ? par.id : false }) };
  8086. }
  8087. return i;
  8088. }
  8089. return true;
  8090. };
  8091. this.create_node = function (par, node, pos, callback, is_loaded) {
  8092. if(!node || node.text === undefined) {
  8093. if(par === null) {
  8094. par = $.jstree.root;
  8095. }
  8096. par = this.get_node(par);
  8097. if(!par) {
  8098. return parent.create_node.call(this, par, node, pos, callback, is_loaded);
  8099. }
  8100. pos = pos === undefined ? "last" : pos;
  8101. if(!pos.toString().match(/^(before|after)$/) && !is_loaded && !this.is_loaded(par)) {
  8102. return parent.create_node.call(this, par, node, pos, callback, is_loaded);
  8103. }
  8104. if(!node) { node = {}; }
  8105. var tmp, n, dpc, i, j, m = this._model.data, s = this.settings.unique.case_sensitive, cb = this.settings.unique.duplicate;
  8106. n = tmp = this.get_string('New node');
  8107. dpc = [];
  8108. for(i = 0, j = par.children.length; i < j; i++) {
  8109. dpc.push(s ? m[par.children[i]].text : m[par.children[i]].text.toLowerCase());
  8110. }
  8111. i = 1;
  8112. while($.inArray(s ? n : n.toLowerCase(), dpc) !== -1) {
  8113. n = cb.call(this, tmp, (++i)).toString();
  8114. }
  8115. node.text = n;
  8116. }
  8117. return parent.create_node.call(this, par, node, pos, callback, is_loaded);
  8118. };
  8119. };
  8120. // include the unique plugin by default
  8121. // $.jstree.defaults.plugins.push("unique");
  8122. /**
  8123. * ### Wholerow plugin
  8124. *
  8125. * Makes each node appear block level. Making selection easier. May cause slow down for large trees in old browsers.
  8126. */
  8127. var div = document.createElement('DIV');
  8128. div.setAttribute('unselectable','on');
  8129. div.setAttribute('role','presentation');
  8130. div.className = 'jstree-wholerow';
  8131. div.innerHTML = '&#160;';
  8132. $.jstree.plugins.wholerow = function (options, parent) {
  8133. this.bind = function () {
  8134. parent.bind.call(this);
  8135. this.element
  8136. .on('ready.jstree set_state.jstree', $.proxy(function () {
  8137. this.hide_dots();
  8138. }, this))
  8139. .on("init.jstree loading.jstree ready.jstree", $.proxy(function () {
  8140. //div.style.height = this._data.core.li_height + 'px';
  8141. this.get_container_ul().addClass('jstree-wholerow-ul');
  8142. }, this))
  8143. .on("deselect_all.jstree", $.proxy(function (e, data) {
  8144. this.element.find('.jstree-wholerow-clicked').removeClass('jstree-wholerow-clicked');
  8145. }, this))
  8146. .on("changed.jstree", $.proxy(function (e, data) {
  8147. this.element.find('.jstree-wholerow-clicked').removeClass('jstree-wholerow-clicked');
  8148. var tmp = false, i, j;
  8149. for(i = 0, j = data.selected.length; i < j; i++) {
  8150. tmp = this.get_node(data.selected[i], true);
  8151. if(tmp && tmp.length) {
  8152. tmp.children('.jstree-wholerow').addClass('jstree-wholerow-clicked');
  8153. }
  8154. }
  8155. }, this))
  8156. .on("open_node.jstree", $.proxy(function (e, data) {
  8157. this.get_node(data.node, true).find('.jstree-clicked').parent().children('.jstree-wholerow').addClass('jstree-wholerow-clicked');
  8158. }, this))
  8159. .on("hover_node.jstree dehover_node.jstree", $.proxy(function (e, data) {
  8160. if(e.type === "hover_node" && this.is_disabled(data.node)) { return; }
  8161. this.get_node(data.node, true).children('.jstree-wholerow')[e.type === "hover_node"?"addClass":"removeClass"]('jstree-wholerow-hovered');
  8162. }, this))
  8163. .on("contextmenu.jstree", ".jstree-wholerow", $.proxy(function (e) {
  8164. if (this._data.contextmenu) {
  8165. e.preventDefault();
  8166. var tmp = $.Event('contextmenu', { metaKey : e.metaKey, ctrlKey : e.ctrlKey, altKey : e.altKey, shiftKey : e.shiftKey, pageX : e.pageX, pageY : e.pageY });
  8167. $(e.currentTarget).closest(".jstree-node").children(".jstree-anchor").first().trigger(tmp);
  8168. }
  8169. }, this))
  8170. /*!
  8171. .on("mousedown.jstree touchstart.jstree", ".jstree-wholerow", function (e) {
  8172. if(e.target === e.currentTarget) {
  8173. var a = $(e.currentTarget).closest(".jstree-node").children(".jstree-anchor");
  8174. e.target = a[0];
  8175. a.trigger(e);
  8176. }
  8177. })
  8178. */
  8179. .on("click.jstree", ".jstree-wholerow", function (e) {
  8180. e.stopImmediatePropagation();
  8181. var tmp = $.Event('click', { metaKey : e.metaKey, ctrlKey : e.ctrlKey, altKey : e.altKey, shiftKey : e.shiftKey });
  8182. $(e.currentTarget).closest(".jstree-node").children(".jstree-anchor").first().trigger(tmp).focus();
  8183. })
  8184. .on("dblclick.jstree", ".jstree-wholerow", function (e) {
  8185. e.stopImmediatePropagation();
  8186. var tmp = $.Event('dblclick', { metaKey : e.metaKey, ctrlKey : e.ctrlKey, altKey : e.altKey, shiftKey : e.shiftKey });
  8187. $(e.currentTarget).closest(".jstree-node").children(".jstree-anchor").first().trigger(tmp).focus();
  8188. })
  8189. .on("click.jstree", ".jstree-leaf > .jstree-ocl", $.proxy(function (e) {
  8190. e.stopImmediatePropagation();
  8191. var tmp = $.Event('click', { metaKey : e.metaKey, ctrlKey : e.ctrlKey, altKey : e.altKey, shiftKey : e.shiftKey });
  8192. $(e.currentTarget).closest(".jstree-node").children(".jstree-anchor").first().trigger(tmp).focus();
  8193. }, this))
  8194. .on("mouseover.jstree", ".jstree-wholerow, .jstree-icon", $.proxy(function (e) {
  8195. e.stopImmediatePropagation();
  8196. if(!this.is_disabled(e.currentTarget)) {
  8197. this.hover_node(e.currentTarget);
  8198. }
  8199. return false;
  8200. }, this))
  8201. .on("mouseleave.jstree", ".jstree-node", $.proxy(function (e) {
  8202. this.dehover_node(e.currentTarget);
  8203. }, this));
  8204. };
  8205. this.teardown = function () {
  8206. if(this.settings.wholerow) {
  8207. this.element.find(".jstree-wholerow").remove();
  8208. }
  8209. parent.teardown.call(this);
  8210. };
  8211. this.redraw_node = function(obj, deep, callback, force_render) {
  8212. obj = parent.redraw_node.apply(this, arguments);
  8213. if(obj) {
  8214. var tmp = div.cloneNode(true);
  8215. //tmp.style.height = this._data.core.li_height + 'px';
  8216. if($.inArray(obj.id, this._data.core.selected) !== -1) { tmp.className += ' jstree-wholerow-clicked'; }
  8217. if(this._data.core.focused && this._data.core.focused === obj.id) { tmp.className += ' jstree-wholerow-hovered'; }
  8218. obj.insertBefore(tmp, obj.childNodes[0]);
  8219. }
  8220. return obj;
  8221. };
  8222. };
  8223. // include the wholerow plugin by default
  8224. // $.jstree.defaults.plugins.push("wholerow");
  8225. if(document.registerElement && Object && Object.create) {
  8226. var proto = Object.create(HTMLElement.prototype);
  8227. proto.createdCallback = function () {
  8228. var c = { core : {}, plugins : [] }, i;
  8229. for(i in $.jstree.plugins) {
  8230. if($.jstree.plugins.hasOwnProperty(i) && this.attributes[i]) {
  8231. c.plugins.push(i);
  8232. if(this.getAttribute(i) && JSON.parse(this.getAttribute(i))) {
  8233. c[i] = JSON.parse(this.getAttribute(i));
  8234. }
  8235. }
  8236. }
  8237. for(i in $.jstree.defaults.core) {
  8238. if($.jstree.defaults.core.hasOwnProperty(i) && this.attributes[i]) {
  8239. c.core[i] = JSON.parse(this.getAttribute(i)) || this.getAttribute(i);
  8240. }
  8241. }
  8242. $(this).jstree(c);
  8243. };
  8244. // proto.attributeChangedCallback = function (name, previous, value) { };
  8245. try {
  8246. document.registerElement("vakata-jstree", { prototype: proto });
  8247. } catch(ignore) { }
  8248. }
  8249. }));