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.
 
 
 
 
 
 

8612 line
298 KiB

  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.8
  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.8',
  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 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. * Should the loaded nodes be part of the state. Defaults to `false`
  432. * @name $.jstree.defaults.core.loaded_state
  433. */
  434. loaded_state : false,
  435. /**
  436. * Should the last active node be focused when the tree container is blurred and the focused again. This helps working with screen readers. Defaults to `true`
  437. * @name $.jstree.defaults.core.restore_focus
  438. */
  439. restore_focus : true,
  440. /**
  441. * Default keyboard shortcuts (an object where each key is the button name or combo - like 'enter', 'ctrl-space', 'p', etc and the value is the function to execute in the instance's scope)
  442. * @name $.jstree.defaults.core.keyboard
  443. */
  444. keyboard : {
  445. 'ctrl-space': function (e) {
  446. // aria defines space only with Ctrl
  447. e.type = "click";
  448. $(e.currentTarget).trigger(e);
  449. },
  450. 'enter': function (e) {
  451. // enter
  452. e.type = "click";
  453. $(e.currentTarget).trigger(e);
  454. },
  455. 'left': function (e) {
  456. // left
  457. e.preventDefault();
  458. if(this.is_open(e.currentTarget)) {
  459. this.close_node(e.currentTarget);
  460. }
  461. else {
  462. var o = this.get_parent(e.currentTarget);
  463. if(o && o.id !== $.jstree.root) { this.get_node(o, true).children('.jstree-anchor').focus(); }
  464. }
  465. },
  466. 'up': function (e) {
  467. // up
  468. e.preventDefault();
  469. var o = this.get_prev_dom(e.currentTarget);
  470. if(o && o.length) { o.children('.jstree-anchor').focus(); }
  471. },
  472. 'right': function (e) {
  473. // right
  474. e.preventDefault();
  475. if(this.is_closed(e.currentTarget)) {
  476. this.open_node(e.currentTarget, function (o) { this.get_node(o, true).children('.jstree-anchor').focus(); });
  477. }
  478. else if (this.is_open(e.currentTarget)) {
  479. var o = this.get_node(e.currentTarget, true).children('.jstree-children')[0];
  480. if(o) { $(this._firstChild(o)).children('.jstree-anchor').focus(); }
  481. }
  482. },
  483. 'down': function (e) {
  484. // down
  485. e.preventDefault();
  486. var o = this.get_next_dom(e.currentTarget);
  487. if(o && o.length) { o.children('.jstree-anchor').focus(); }
  488. },
  489. '*': function (e) {
  490. // aria defines * on numpad as open_all - not very common
  491. this.open_all();
  492. },
  493. 'home': function (e) {
  494. // home
  495. e.preventDefault();
  496. var o = this._firstChild(this.get_container_ul()[0]);
  497. if(o) { $(o).children('.jstree-anchor').filter(':visible').focus(); }
  498. },
  499. 'end': function (e) {
  500. // end
  501. e.preventDefault();
  502. this.element.find('.jstree-anchor').filter(':visible').last().focus();
  503. },
  504. 'f2': function (e) {
  505. // f2 - safe to include - if check_callback is false it will fail
  506. e.preventDefault();
  507. this.edit(e.currentTarget);
  508. }
  509. }
  510. };
  511. $.jstree.core.prototype = {
  512. /**
  513. * used to decorate an instance with a plugin. Used internally.
  514. * @private
  515. * @name plugin(deco [, opts])
  516. * @param {String} deco the plugin to decorate with
  517. * @param {Object} opts options for the plugin
  518. * @return {jsTree}
  519. */
  520. plugin : function (deco, opts) {
  521. var Child = $.jstree.plugins[deco];
  522. if(Child) {
  523. this._data[deco] = {};
  524. Child.prototype = this;
  525. return new Child(opts, this);
  526. }
  527. return this;
  528. },
  529. /**
  530. * initialize the instance. Used internally.
  531. * @private
  532. * @name init(el, optons)
  533. * @param {DOMElement|jQuery|String} el the element we are transforming
  534. * @param {Object} options options for this instance
  535. * @trigger init.jstree, loading.jstree, loaded.jstree, ready.jstree, changed.jstree
  536. */
  537. init : function (el, options) {
  538. this._model = {
  539. data : {},
  540. changed : [],
  541. force_full_redraw : false,
  542. redraw_timeout : false,
  543. default_state : {
  544. loaded : true,
  545. opened : false,
  546. selected : false,
  547. disabled : false
  548. }
  549. };
  550. this._model.data[$.jstree.root] = {
  551. id : $.jstree.root,
  552. parent : null,
  553. parents : [],
  554. children : [],
  555. children_d : [],
  556. state : { loaded : false }
  557. };
  558. this.element = $(el).addClass('jstree jstree-' + this._id);
  559. this.settings = options;
  560. this._data.core.ready = false;
  561. this._data.core.loaded = false;
  562. this._data.core.rtl = (this.element.css("direction") === "rtl");
  563. this.element[this._data.core.rtl ? 'addClass' : 'removeClass']("jstree-rtl");
  564. this.element.attr('role','tree');
  565. if(this.settings.core.multiple) {
  566. this.element.attr('aria-multiselectable', true);
  567. }
  568. if(!this.element.attr('tabindex')) {
  569. this.element.attr('tabindex','0');
  570. }
  571. this.bind();
  572. /**
  573. * triggered after all events are bound
  574. * @event
  575. * @name init.jstree
  576. */
  577. this.trigger("init");
  578. this._data.core.original_container_html = this.element.find(" > ul > li").clone(true);
  579. this._data.core.original_container_html
  580. .find("li").addBack()
  581. .contents().filter(function() {
  582. return this.nodeType === 3 && (!this.nodeValue || /^\s+$/.test(this.nodeValue));
  583. })
  584. .remove();
  585. 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>");
  586. this.element.attr('aria-activedescendant','j' + this._id + '_loading');
  587. this._data.core.li_height = this.get_container_ul().children("li").first().outerHeight() || 24;
  588. this._data.core.node = this._create_prototype_node();
  589. /**
  590. * triggered after the loading text is shown and before loading starts
  591. * @event
  592. * @name loading.jstree
  593. */
  594. this.trigger("loading");
  595. this.load_node($.jstree.root);
  596. },
  597. /**
  598. * destroy an instance
  599. * @name destroy()
  600. * @param {Boolean} keep_html if not set to `true` the container will be emptied, otherwise the current DOM elements will be kept intact
  601. */
  602. destroy : function (keep_html) {
  603. /**
  604. * triggered before the tree is destroyed
  605. * @event
  606. * @name destroy.jstree
  607. */
  608. this.trigger("destroy");
  609. if(this._wrk) {
  610. try {
  611. window.URL.revokeObjectURL(this._wrk);
  612. this._wrk = null;
  613. }
  614. catch (ignore) { }
  615. }
  616. if(!keep_html) { this.element.empty(); }
  617. this.teardown();
  618. },
  619. /**
  620. * Create a prototype node
  621. * @name _create_prototype_node()
  622. * @return {DOMElement}
  623. */
  624. _create_prototype_node : function () {
  625. var _node = document.createElement('LI'), _temp1, _temp2;
  626. _node.setAttribute('role', 'treeitem');
  627. _temp1 = document.createElement('I');
  628. _temp1.className = 'jstree-icon jstree-ocl';
  629. _temp1.setAttribute('role', 'presentation');
  630. _node.appendChild(_temp1);
  631. _temp1 = document.createElement('A');
  632. _temp1.className = 'jstree-anchor';
  633. _temp1.setAttribute('href','#');
  634. _temp1.setAttribute('tabindex','-1');
  635. _temp2 = document.createElement('I');
  636. _temp2.className = 'jstree-icon jstree-themeicon';
  637. _temp2.setAttribute('role', 'presentation');
  638. _temp1.appendChild(_temp2);
  639. _node.appendChild(_temp1);
  640. _temp1 = _temp2 = null;
  641. return _node;
  642. },
  643. _kbevent_to_func : function (e) {
  644. var keys = {
  645. 8: "Backspace", 9: "Tab", 13: "Enter", 19: "Pause", 27: "Esc",
  646. 32: "Space", 33: "PageUp", 34: "PageDown", 35: "End", 36: "Home",
  647. 37: "Left", 38: "Up", 39: "Right", 40: "Down", 44: "Print", 45: "Insert",
  648. 46: "Delete", 96: "Numpad0", 97: "Numpad1", 98: "Numpad2", 99 : "Numpad3",
  649. 100: "Numpad4", 101: "Numpad5", 102: "Numpad6", 103: "Numpad7",
  650. 104: "Numpad8", 105: "Numpad9", '-13': "NumpadEnter", 112: "F1",
  651. 113: "F2", 114: "F3", 115: "F4", 116: "F5", 117: "F6", 118: "F7",
  652. 119: "F8", 120: "F9", 121: "F10", 122: "F11", 123: "F12", 144: "Numlock",
  653. 145: "Scrolllock", 16: 'Shift', 17: 'Ctrl', 18: 'Alt',
  654. 48: '0', 49: '1', 50: '2', 51: '3', 52: '4', 53: '5',
  655. 54: '6', 55: '7', 56: '8', 57: '9', 59: ';', 61: '=', 65: 'a',
  656. 66: 'b', 67: 'c', 68: 'd', 69: 'e', 70: 'f', 71: 'g', 72: 'h',
  657. 73: 'i', 74: 'j', 75: 'k', 76: 'l', 77: 'm', 78: 'n', 79: 'o',
  658. 80: 'p', 81: 'q', 82: 'r', 83: 's', 84: 't', 85: 'u', 86: 'v',
  659. 87: 'w', 88: 'x', 89: 'y', 90: 'z', 107: '+', 109: '-', 110: '.',
  660. 186: ';', 187: '=', 188: ',', 189: '-', 190: '.', 191: '/', 192: '`',
  661. 219: '[', 220: '\\',221: ']', 222: "'", 111: '/', 106: '*', 173: '-'
  662. };
  663. var parts = [];
  664. if (e.ctrlKey) { parts.push('ctrl'); }
  665. if (e.altKey) { parts.push('alt'); }
  666. if (e.shiftKey) { parts.push('shift'); }
  667. parts.push(keys[e.which] || e.which);
  668. parts = parts.sort().join('-').toLowerCase();
  669. var kb = this.settings.core.keyboard, i, tmp;
  670. for (i in kb) {
  671. if (kb.hasOwnProperty(i)) {
  672. tmp = i;
  673. if (tmp !== '-' && tmp !== '+') {
  674. tmp = tmp.replace('--', '-MINUS').replace('+-', '-MINUS').replace('++', '-PLUS').replace('-+', '-PLUS');
  675. tmp = tmp.split(/-|\+/).sort().join('-').replace('MINUS', '-').replace('PLUS', '+').toLowerCase();
  676. }
  677. if (tmp === parts) {
  678. return kb[i];
  679. }
  680. }
  681. }
  682. return null;
  683. },
  684. /**
  685. * part of the destroying of an instance. Used internally.
  686. * @private
  687. * @name teardown()
  688. */
  689. teardown : function () {
  690. this.unbind();
  691. this.element
  692. .removeClass('jstree')
  693. .removeData('jstree')
  694. .find("[class^='jstree']")
  695. .addBack()
  696. .attr("class", function () { return this.className.replace(/jstree[^ ]*|$/ig,''); });
  697. this.element = null;
  698. },
  699. /**
  700. * bind all events. Used internally.
  701. * @private
  702. * @name bind()
  703. */
  704. bind : function () {
  705. var word = '',
  706. tout = null,
  707. was_click = 0;
  708. this.element
  709. .on("dblclick.jstree", function (e) {
  710. if(e.target.tagName && e.target.tagName.toLowerCase() === "input") { return true; }
  711. if(document.selection && document.selection.empty) {
  712. document.selection.empty();
  713. }
  714. else {
  715. if(window.getSelection) {
  716. var sel = window.getSelection();
  717. try {
  718. sel.removeAllRanges();
  719. sel.collapse();
  720. } catch (ignore) { }
  721. }
  722. }
  723. })
  724. .on("mousedown.jstree", $.proxy(function (e) {
  725. if(e.target === this.element[0]) {
  726. e.preventDefault(); // prevent losing focus when clicking scroll arrows (FF, Chrome)
  727. was_click = +(new Date()); // ie does not allow to prevent losing focus
  728. }
  729. }, this))
  730. .on("mousedown.jstree", ".jstree-ocl", function (e) {
  731. e.preventDefault(); // prevent any node inside from losing focus when clicking the open/close icon
  732. })
  733. .on("click.jstree", ".jstree-ocl", $.proxy(function (e) {
  734. this.toggle_node(e.target);
  735. }, this))
  736. .on("dblclick.jstree", ".jstree-anchor", $.proxy(function (e) {
  737. if(e.target.tagName && e.target.tagName.toLowerCase() === "input") { return true; }
  738. if(this.settings.core.dblclick_toggle) {
  739. this.toggle_node(e.target);
  740. }
  741. }, this))
  742. .on("click.jstree", ".jstree-anchor", $.proxy(function (e) {
  743. e.preventDefault();
  744. if(e.currentTarget !== document.activeElement) { $(e.currentTarget).focus(); }
  745. this.activate_node(e.currentTarget, e);
  746. }, this))
  747. .on('keydown.jstree', '.jstree-anchor', $.proxy(function (e) {
  748. if(e.target.tagName && e.target.tagName.toLowerCase() === "input") { return true; }
  749. if(this._data.core.rtl) {
  750. if(e.which === 37) { e.which = 39; }
  751. else if(e.which === 39) { e.which = 37; }
  752. }
  753. var f = this._kbevent_to_func(e);
  754. if (f) {
  755. var r = f.call(this, e);
  756. if (r === false || r === true) {
  757. return r;
  758. }
  759. }
  760. }, this))
  761. .on("load_node.jstree", $.proxy(function (e, data) {
  762. if(data.status) {
  763. if(data.node.id === $.jstree.root && !this._data.core.loaded) {
  764. this._data.core.loaded = true;
  765. if(this._firstChild(this.get_container_ul()[0])) {
  766. this.element.attr('aria-activedescendant',this._firstChild(this.get_container_ul()[0]).id);
  767. }
  768. /**
  769. * triggered after the root node is loaded for the first time
  770. * @event
  771. * @name loaded.jstree
  772. */
  773. this.trigger("loaded");
  774. }
  775. if(!this._data.core.ready) {
  776. setTimeout($.proxy(function() {
  777. if(this.element && !this.get_container_ul().find('.jstree-loading').length) {
  778. this._data.core.ready = true;
  779. if(this._data.core.selected.length) {
  780. if(this.settings.core.expand_selected_onload) {
  781. var tmp = [], i, j;
  782. for(i = 0, j = this._data.core.selected.length; i < j; i++) {
  783. tmp = tmp.concat(this._model.data[this._data.core.selected[i]].parents);
  784. }
  785. tmp = $.vakata.array_unique(tmp);
  786. for(i = 0, j = tmp.length; i < j; i++) {
  787. this.open_node(tmp[i], false, 0);
  788. }
  789. }
  790. this.trigger('changed', { 'action' : 'ready', 'selected' : this._data.core.selected });
  791. }
  792. /**
  793. * triggered after all nodes are finished loading
  794. * @event
  795. * @name ready.jstree
  796. */
  797. this.trigger("ready");
  798. }
  799. }, this), 0);
  800. }
  801. }
  802. }, this))
  803. // quick searching when the tree is focused
  804. .on('keypress.jstree', $.proxy(function (e) {
  805. if(e.target.tagName && e.target.tagName.toLowerCase() === "input") { return true; }
  806. if(tout) { clearTimeout(tout); }
  807. tout = setTimeout(function () {
  808. word = '';
  809. }, 500);
  810. var chr = String.fromCharCode(e.which).toLowerCase(),
  811. col = this.element.find('.jstree-anchor').filter(':visible'),
  812. ind = col.index(document.activeElement) || 0,
  813. end = false;
  814. word += chr;
  815. // match for whole word from current node down (including the current node)
  816. if(word.length > 1) {
  817. col.slice(ind).each($.proxy(function (i, v) {
  818. if($(v).text().toLowerCase().indexOf(word) === 0) {
  819. $(v).focus();
  820. end = true;
  821. return false;
  822. }
  823. }, this));
  824. if(end) { return; }
  825. // match for whole word from the beginning of the tree
  826. col.slice(0, ind).each($.proxy(function (i, v) {
  827. if($(v).text().toLowerCase().indexOf(word) === 0) {
  828. $(v).focus();
  829. end = true;
  830. return false;
  831. }
  832. }, this));
  833. if(end) { return; }
  834. }
  835. // list nodes that start with that letter (only if word consists of a single char)
  836. if(new RegExp('^' + chr.replace(/[-\/\\^$*+?.()|[\]{}]/g, '\\$&') + '+$').test(word)) {
  837. // search for the next node starting with that letter
  838. col.slice(ind + 1).each($.proxy(function (i, v) {
  839. if($(v).text().toLowerCase().charAt(0) === chr) {
  840. $(v).focus();
  841. end = true;
  842. return false;
  843. }
  844. }, this));
  845. if(end) { return; }
  846. // search from the beginning
  847. col.slice(0, ind + 1).each($.proxy(function (i, v) {
  848. if($(v).text().toLowerCase().charAt(0) === chr) {
  849. $(v).focus();
  850. end = true;
  851. return false;
  852. }
  853. }, this));
  854. if(end) { return; }
  855. }
  856. }, this))
  857. // THEME RELATED
  858. .on("init.jstree", $.proxy(function () {
  859. var s = this.settings.core.themes;
  860. this._data.core.themes.dots = s.dots;
  861. this._data.core.themes.stripes = s.stripes;
  862. this._data.core.themes.icons = s.icons;
  863. this._data.core.themes.ellipsis = s.ellipsis;
  864. this.set_theme(s.name || "default", s.url);
  865. this.set_theme_variant(s.variant);
  866. }, this))
  867. .on("loading.jstree", $.proxy(function () {
  868. this[ this._data.core.themes.dots ? "show_dots" : "hide_dots" ]();
  869. this[ this._data.core.themes.icons ? "show_icons" : "hide_icons" ]();
  870. this[ this._data.core.themes.stripes ? "show_stripes" : "hide_stripes" ]();
  871. this[ this._data.core.themes.ellipsis ? "show_ellipsis" : "hide_ellipsis" ]();
  872. }, this))
  873. .on('blur.jstree', '.jstree-anchor', $.proxy(function (e) {
  874. this._data.core.focused = null;
  875. $(e.currentTarget).filter('.jstree-hovered').trigger('mouseleave');
  876. this.element.attr('tabindex', '0');
  877. }, this))
  878. .on('focus.jstree', '.jstree-anchor', $.proxy(function (e) {
  879. var tmp = this.get_node(e.currentTarget);
  880. if(tmp && tmp.id) {
  881. this._data.core.focused = tmp.id;
  882. }
  883. this.element.find('.jstree-hovered').not(e.currentTarget).trigger('mouseleave');
  884. $(e.currentTarget).trigger('mouseenter');
  885. this.element.attr('tabindex', '-1');
  886. }, this))
  887. .on('focus.jstree', $.proxy(function () {
  888. if(+(new Date()) - was_click > 500 && !this._data.core.focused && this.settings.core.restore_focus) {
  889. was_click = 0;
  890. var act = this.get_node(this.element.attr('aria-activedescendant'), true);
  891. if(act) {
  892. act.find('> .jstree-anchor').focus();
  893. }
  894. }
  895. }, this))
  896. .on('mouseenter.jstree', '.jstree-anchor', $.proxy(function (e) {
  897. this.hover_node(e.currentTarget);
  898. }, this))
  899. .on('mouseleave.jstree', '.jstree-anchor', $.proxy(function (e) {
  900. this.dehover_node(e.currentTarget);
  901. }, this));
  902. },
  903. /**
  904. * part of the destroying of an instance. Used internally.
  905. * @private
  906. * @name unbind()
  907. */
  908. unbind : function () {
  909. this.element.off('.jstree');
  910. $(document).off('.jstree-' + this._id);
  911. },
  912. /**
  913. * trigger an event. Used internally.
  914. * @private
  915. * @name trigger(ev [, data])
  916. * @param {String} ev the name of the event to trigger
  917. * @param {Object} data additional data to pass with the event
  918. */
  919. trigger : function (ev, data) {
  920. if(!data) {
  921. data = {};
  922. }
  923. data.instance = this;
  924. this.element.triggerHandler(ev.replace('.jstree','') + '.jstree', data);
  925. },
  926. /**
  927. * returns the jQuery extended instance container
  928. * @name get_container()
  929. * @return {jQuery}
  930. */
  931. get_container : function () {
  932. return this.element;
  933. },
  934. /**
  935. * returns the jQuery extended main UL node inside the instance container. Used internally.
  936. * @private
  937. * @name get_container_ul()
  938. * @return {jQuery}
  939. */
  940. get_container_ul : function () {
  941. return this.element.children(".jstree-children").first();
  942. },
  943. /**
  944. * gets string replacements (localization). Used internally.
  945. * @private
  946. * @name get_string(key)
  947. * @param {String} key
  948. * @return {String}
  949. */
  950. get_string : function (key) {
  951. var a = this.settings.core.strings;
  952. if($.isFunction(a)) { return a.call(this, key); }
  953. if(a && a[key]) { return a[key]; }
  954. return key;
  955. },
  956. /**
  957. * gets the first child of a DOM node. Used internally.
  958. * @private
  959. * @name _firstChild(dom)
  960. * @param {DOMElement} dom
  961. * @return {DOMElement}
  962. */
  963. _firstChild : function (dom) {
  964. dom = dom ? dom.firstChild : null;
  965. while(dom !== null && dom.nodeType !== 1) {
  966. dom = dom.nextSibling;
  967. }
  968. return dom;
  969. },
  970. /**
  971. * gets the next sibling of a DOM node. Used internally.
  972. * @private
  973. * @name _nextSibling(dom)
  974. * @param {DOMElement} dom
  975. * @return {DOMElement}
  976. */
  977. _nextSibling : function (dom) {
  978. dom = dom ? dom.nextSibling : null;
  979. while(dom !== null && dom.nodeType !== 1) {
  980. dom = dom.nextSibling;
  981. }
  982. return dom;
  983. },
  984. /**
  985. * gets the previous sibling of a DOM node. Used internally.
  986. * @private
  987. * @name _previousSibling(dom)
  988. * @param {DOMElement} dom
  989. * @return {DOMElement}
  990. */
  991. _previousSibling : function (dom) {
  992. dom = dom ? dom.previousSibling : null;
  993. while(dom !== null && dom.nodeType !== 1) {
  994. dom = dom.previousSibling;
  995. }
  996. return dom;
  997. },
  998. /**
  999. * 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)
  1000. * @name get_node(obj [, as_dom])
  1001. * @param {mixed} obj
  1002. * @param {Boolean} as_dom
  1003. * @return {Object|jQuery}
  1004. */
  1005. get_node : function (obj, as_dom) {
  1006. if(obj && obj.id) {
  1007. obj = obj.id;
  1008. }
  1009. if (obj instanceof $ && obj.length && obj[0].id) {
  1010. obj = obj[0].id;
  1011. }
  1012. var dom;
  1013. try {
  1014. if(this._model.data[obj]) {
  1015. obj = this._model.data[obj];
  1016. }
  1017. else if(typeof obj === "string" && this._model.data[obj.replace(/^#/, '')]) {
  1018. obj = this._model.data[obj.replace(/^#/, '')];
  1019. }
  1020. else if(typeof obj === "string" && (dom = $('#' + obj.replace($.jstree.idregex,'\\$&'), this.element)).length && this._model.data[dom.closest('.jstree-node').attr('id')]) {
  1021. obj = this._model.data[dom.closest('.jstree-node').attr('id')];
  1022. }
  1023. else if((dom = this.element.find(obj)).length && this._model.data[dom.closest('.jstree-node').attr('id')]) {
  1024. obj = this._model.data[dom.closest('.jstree-node').attr('id')];
  1025. }
  1026. else if((dom = this.element.find(obj)).length && dom.hasClass('jstree')) {
  1027. obj = this._model.data[$.jstree.root];
  1028. }
  1029. else {
  1030. return false;
  1031. }
  1032. if(as_dom) {
  1033. obj = obj.id === $.jstree.root ? this.element : $('#' + obj.id.replace($.jstree.idregex,'\\$&'), this.element);
  1034. }
  1035. return obj;
  1036. } catch (ex) { return false; }
  1037. },
  1038. /**
  1039. * get the path to a node, either consisting of node texts, or of node IDs, optionally glued together (otherwise an array)
  1040. * @name get_path(obj [, glue, ids])
  1041. * @param {mixed} obj the node
  1042. * @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
  1043. * @param {Boolean} ids if set to true build the path using ID, otherwise node text is used
  1044. * @return {mixed}
  1045. */
  1046. get_path : function (obj, glue, ids) {
  1047. obj = obj.parents ? obj : this.get_node(obj);
  1048. if(!obj || obj.id === $.jstree.root || !obj.parents) {
  1049. return false;
  1050. }
  1051. var i, j, p = [];
  1052. p.push(ids ? obj.id : obj.text);
  1053. for(i = 0, j = obj.parents.length; i < j; i++) {
  1054. p.push(ids ? obj.parents[i] : this.get_text(obj.parents[i]));
  1055. }
  1056. p = p.reverse().slice(1);
  1057. return glue ? p.join(glue) : p;
  1058. },
  1059. /**
  1060. * get the next visible node that is below the `obj` node. If `strict` is set to `true` only sibling nodes are returned.
  1061. * @name get_next_dom(obj [, strict])
  1062. * @param {mixed} obj
  1063. * @param {Boolean} strict
  1064. * @return {jQuery}
  1065. */
  1066. get_next_dom : function (obj, strict) {
  1067. var tmp;
  1068. obj = this.get_node(obj, true);
  1069. if(obj[0] === this.element[0]) {
  1070. tmp = this._firstChild(this.get_container_ul()[0]);
  1071. while (tmp && tmp.offsetHeight === 0) {
  1072. tmp = this._nextSibling(tmp);
  1073. }
  1074. return tmp ? $(tmp) : false;
  1075. }
  1076. if(!obj || !obj.length) {
  1077. return false;
  1078. }
  1079. if(strict) {
  1080. tmp = obj[0];
  1081. do {
  1082. tmp = this._nextSibling(tmp);
  1083. } while (tmp && tmp.offsetHeight === 0);
  1084. return tmp ? $(tmp) : false;
  1085. }
  1086. if(obj.hasClass("jstree-open")) {
  1087. tmp = this._firstChild(obj.children('.jstree-children')[0]);
  1088. while (tmp && tmp.offsetHeight === 0) {
  1089. tmp = this._nextSibling(tmp);
  1090. }
  1091. if(tmp !== null) {
  1092. return $(tmp);
  1093. }
  1094. }
  1095. tmp = obj[0];
  1096. do {
  1097. tmp = this._nextSibling(tmp);
  1098. } while (tmp && tmp.offsetHeight === 0);
  1099. if(tmp !== null) {
  1100. return $(tmp);
  1101. }
  1102. return obj.parentsUntil(".jstree",".jstree-node").nextAll(".jstree-node:visible").first();
  1103. },
  1104. /**
  1105. * get the previous visible node that is above the `obj` node. If `strict` is set to `true` only sibling nodes are returned.
  1106. * @name get_prev_dom(obj [, strict])
  1107. * @param {mixed} obj
  1108. * @param {Boolean} strict
  1109. * @return {jQuery}
  1110. */
  1111. get_prev_dom : function (obj, strict) {
  1112. var tmp;
  1113. obj = this.get_node(obj, true);
  1114. if(obj[0] === this.element[0]) {
  1115. tmp = this.get_container_ul()[0].lastChild;
  1116. while (tmp && tmp.offsetHeight === 0) {
  1117. tmp = this._previousSibling(tmp);
  1118. }
  1119. return tmp ? $(tmp) : false;
  1120. }
  1121. if(!obj || !obj.length) {
  1122. return false;
  1123. }
  1124. if(strict) {
  1125. tmp = obj[0];
  1126. do {
  1127. tmp = this._previousSibling(tmp);
  1128. } while (tmp && tmp.offsetHeight === 0);
  1129. return tmp ? $(tmp) : false;
  1130. }
  1131. tmp = obj[0];
  1132. do {
  1133. tmp = this._previousSibling(tmp);
  1134. } while (tmp && tmp.offsetHeight === 0);
  1135. if(tmp !== null) {
  1136. obj = $(tmp);
  1137. while(obj.hasClass("jstree-open")) {
  1138. obj = obj.children(".jstree-children").first().children(".jstree-node:visible:last");
  1139. }
  1140. return obj;
  1141. }
  1142. tmp = obj[0].parentNode.parentNode;
  1143. return tmp && tmp.className && tmp.className.indexOf('jstree-node') !== -1 ? $(tmp) : false;
  1144. },
  1145. /**
  1146. * get the parent ID of a node
  1147. * @name get_parent(obj)
  1148. * @param {mixed} obj
  1149. * @return {String}
  1150. */
  1151. get_parent : function (obj) {
  1152. obj = this.get_node(obj);
  1153. if(!obj || obj.id === $.jstree.root) {
  1154. return false;
  1155. }
  1156. return obj.parent;
  1157. },
  1158. /**
  1159. * get a jQuery collection of all the children of a node (node must be rendered), returns false on error
  1160. * @name get_children_dom(obj)
  1161. * @param {mixed} obj
  1162. * @return {jQuery}
  1163. */
  1164. get_children_dom : function (obj) {
  1165. obj = this.get_node(obj, true);
  1166. if(obj[0] === this.element[0]) {
  1167. return this.get_container_ul().children(".jstree-node");
  1168. }
  1169. if(!obj || !obj.length) {
  1170. return false;
  1171. }
  1172. return obj.children(".jstree-children").children(".jstree-node");
  1173. },
  1174. /**
  1175. * checks if a node has children
  1176. * @name is_parent(obj)
  1177. * @param {mixed} obj
  1178. * @return {Boolean}
  1179. */
  1180. is_parent : function (obj) {
  1181. obj = this.get_node(obj);
  1182. return obj && (obj.state.loaded === false || obj.children.length > 0);
  1183. },
  1184. /**
  1185. * checks if a node is loaded (its children are available)
  1186. * @name is_loaded(obj)
  1187. * @param {mixed} obj
  1188. * @return {Boolean}
  1189. */
  1190. is_loaded : function (obj) {
  1191. obj = this.get_node(obj);
  1192. return obj && obj.state.loaded;
  1193. },
  1194. /**
  1195. * check if a node is currently loading (fetching children)
  1196. * @name is_loading(obj)
  1197. * @param {mixed} obj
  1198. * @return {Boolean}
  1199. */
  1200. is_loading : function (obj) {
  1201. obj = this.get_node(obj);
  1202. return obj && obj.state && obj.state.loading;
  1203. },
  1204. /**
  1205. * check if a node is opened
  1206. * @name is_open(obj)
  1207. * @param {mixed} obj
  1208. * @return {Boolean}
  1209. */
  1210. is_open : function (obj) {
  1211. obj = this.get_node(obj);
  1212. return obj && obj.state.opened;
  1213. },
  1214. /**
  1215. * check if a node is in a closed state
  1216. * @name is_closed(obj)
  1217. * @param {mixed} obj
  1218. * @return {Boolean}
  1219. */
  1220. is_closed : function (obj) {
  1221. obj = this.get_node(obj);
  1222. return obj && this.is_parent(obj) && !obj.state.opened;
  1223. },
  1224. /**
  1225. * check if a node has no children
  1226. * @name is_leaf(obj)
  1227. * @param {mixed} obj
  1228. * @return {Boolean}
  1229. */
  1230. is_leaf : function (obj) {
  1231. return !this.is_parent(obj);
  1232. },
  1233. /**
  1234. * loads a node (fetches its children using the `core.data` setting). Multiple nodes can be passed to by using an array.
  1235. * @name load_node(obj [, callback])
  1236. * @param {mixed} obj
  1237. * @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
  1238. * @return {Boolean}
  1239. * @trigger load_node.jstree
  1240. */
  1241. load_node : function (obj, callback) {
  1242. var k, l, i, j, c;
  1243. if($.isArray(obj)) {
  1244. this._load_nodes(obj.slice(), callback);
  1245. return true;
  1246. }
  1247. obj = this.get_node(obj);
  1248. if(!obj) {
  1249. if(callback) { callback.call(this, obj, false); }
  1250. return false;
  1251. }
  1252. // 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?
  1253. if(obj.state.loaded) {
  1254. obj.state.loaded = false;
  1255. for(i = 0, j = obj.parents.length; i < j; i++) {
  1256. this._model.data[obj.parents[i]].children_d = $.vakata.array_filter(this._model.data[obj.parents[i]].children_d, function (v) {
  1257. return $.inArray(v, obj.children_d) === -1;
  1258. });
  1259. }
  1260. for(k = 0, l = obj.children_d.length; k < l; k++) {
  1261. if(this._model.data[obj.children_d[k]].state.selected) {
  1262. c = true;
  1263. }
  1264. delete this._model.data[obj.children_d[k]];
  1265. }
  1266. if (c) {
  1267. this._data.core.selected = $.vakata.array_filter(this._data.core.selected, function (v) {
  1268. return $.inArray(v, obj.children_d) === -1;
  1269. });
  1270. }
  1271. obj.children = [];
  1272. obj.children_d = [];
  1273. if(c) {
  1274. this.trigger('changed', { 'action' : 'load_node', 'node' : obj, 'selected' : this._data.core.selected });
  1275. }
  1276. }
  1277. obj.state.failed = false;
  1278. obj.state.loading = true;
  1279. this.get_node(obj, true).addClass("jstree-loading").attr('aria-busy',true);
  1280. this._load_node(obj, $.proxy(function (status) {
  1281. obj = this._model.data[obj.id];
  1282. obj.state.loading = false;
  1283. obj.state.loaded = status;
  1284. obj.state.failed = !obj.state.loaded;
  1285. var dom = this.get_node(obj, true), i = 0, j = 0, m = this._model.data, has_children = false;
  1286. for(i = 0, j = obj.children.length; i < j; i++) {
  1287. if(m[obj.children[i]] && !m[obj.children[i]].state.hidden) {
  1288. has_children = true;
  1289. break;
  1290. }
  1291. }
  1292. if(obj.state.loaded && dom && dom.length) {
  1293. dom.removeClass('jstree-closed jstree-open jstree-leaf');
  1294. if (!has_children) {
  1295. dom.addClass('jstree-leaf');
  1296. }
  1297. else {
  1298. if (obj.id !== '#') {
  1299. dom.addClass(obj.state.opened ? 'jstree-open' : 'jstree-closed');
  1300. }
  1301. }
  1302. }
  1303. dom.removeClass("jstree-loading").attr('aria-busy',false);
  1304. /**
  1305. * triggered after a node is loaded
  1306. * @event
  1307. * @name load_node.jstree
  1308. * @param {Object} node the node that was loading
  1309. * @param {Boolean} status was the node loaded successfully
  1310. */
  1311. this.trigger('load_node', { "node" : obj, "status" : status });
  1312. if(callback) {
  1313. callback.call(this, obj, status);
  1314. }
  1315. }, this));
  1316. return true;
  1317. },
  1318. /**
  1319. * load an array of nodes (will also load unavailable nodes as soon as they appear in the structure). Used internally.
  1320. * @private
  1321. * @name _load_nodes(nodes [, callback])
  1322. * @param {array} nodes
  1323. * @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
  1324. */
  1325. _load_nodes : function (nodes, callback, is_callback, force_reload) {
  1326. var r = true,
  1327. c = function () { this._load_nodes(nodes, callback, true); },
  1328. m = this._model.data, i, j, tmp = [];
  1329. for(i = 0, j = nodes.length; i < j; i++) {
  1330. if(m[nodes[i]] && ( (!m[nodes[i]].state.loaded && !m[nodes[i]].state.failed) || (!is_callback && force_reload) )) {
  1331. if(!this.is_loading(nodes[i])) {
  1332. this.load_node(nodes[i], c);
  1333. }
  1334. r = false;
  1335. }
  1336. }
  1337. if(r) {
  1338. for(i = 0, j = nodes.length; i < j; i++) {
  1339. if(m[nodes[i]] && m[nodes[i]].state.loaded) {
  1340. tmp.push(nodes[i]);
  1341. }
  1342. }
  1343. if(callback && !callback.done) {
  1344. callback.call(this, tmp);
  1345. callback.done = true;
  1346. }
  1347. }
  1348. },
  1349. /**
  1350. * loads all unloaded nodes
  1351. * @name load_all([obj, callback])
  1352. * @param {mixed} obj the node to load recursively, omit to load all nodes in the tree
  1353. * @param {function} callback a function to be executed once loading all the nodes is complete,
  1354. * @trigger load_all.jstree
  1355. */
  1356. load_all : function (obj, callback) {
  1357. if(!obj) { obj = $.jstree.root; }
  1358. obj = this.get_node(obj);
  1359. if(!obj) { return false; }
  1360. var to_load = [],
  1361. m = this._model.data,
  1362. c = m[obj.id].children_d,
  1363. i, j;
  1364. if(obj.state && !obj.state.loaded) {
  1365. to_load.push(obj.id);
  1366. }
  1367. for(i = 0, j = c.length; i < j; i++) {
  1368. if(m[c[i]] && m[c[i]].state && !m[c[i]].state.loaded) {
  1369. to_load.push(c[i]);
  1370. }
  1371. }
  1372. if(to_load.length) {
  1373. this._load_nodes(to_load, function () {
  1374. this.load_all(obj, callback);
  1375. });
  1376. }
  1377. else {
  1378. /**
  1379. * triggered after a load_all call completes
  1380. * @event
  1381. * @name load_all.jstree
  1382. * @param {Object} node the recursively loaded node
  1383. */
  1384. if(callback) { callback.call(this, obj); }
  1385. this.trigger('load_all', { "node" : obj });
  1386. }
  1387. },
  1388. /**
  1389. * handles the actual loading of a node. Used only internally.
  1390. * @private
  1391. * @name _load_node(obj [, callback])
  1392. * @param {mixed} obj
  1393. * @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
  1394. * @return {Boolean}
  1395. */
  1396. _load_node : function (obj, callback) {
  1397. var s = this.settings.core.data, t;
  1398. var notTextOrCommentNode = function notTextOrCommentNode () {
  1399. return this.nodeType !== 3 && this.nodeType !== 8;
  1400. };
  1401. // use original HTML
  1402. if(!s) {
  1403. if(obj.id === $.jstree.root) {
  1404. return this._append_html_data(obj, this._data.core.original_container_html.clone(true), function (status) {
  1405. callback.call(this, status);
  1406. });
  1407. }
  1408. else {
  1409. return callback.call(this, false);
  1410. }
  1411. // return callback.call(this, obj.id === $.jstree.root ? this._append_html_data(obj, this._data.core.original_container_html.clone(true)) : false);
  1412. }
  1413. if($.isFunction(s)) {
  1414. return s.call(this, obj, $.proxy(function (d) {
  1415. if(d === false) {
  1416. callback.call(this, false);
  1417. }
  1418. else {
  1419. this[typeof d === 'string' ? '_append_html_data' : '_append_json_data'](obj, typeof d === 'string' ? $($.parseHTML(d)).filter(notTextOrCommentNode) : d, function (status) {
  1420. callback.call(this, status);
  1421. });
  1422. }
  1423. // 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));
  1424. }, this));
  1425. }
  1426. if(typeof s === 'object') {
  1427. if(s.url) {
  1428. s = $.extend(true, {}, s);
  1429. if($.isFunction(s.url)) {
  1430. s.url = s.url.call(this, obj);
  1431. }
  1432. if($.isFunction(s.data)) {
  1433. s.data = s.data.call(this, obj);
  1434. }
  1435. return $.ajax(s)
  1436. .done($.proxy(function (d,t,x) {
  1437. var type = x.getResponseHeader('Content-Type');
  1438. if((type && type.indexOf('json') !== -1) || typeof d === "object") {
  1439. return this._append_json_data(obj, d, function (status) { callback.call(this, status); });
  1440. //return callback.call(this, this._append_json_data(obj, d));
  1441. }
  1442. if((type && type.indexOf('html') !== -1) || typeof d === "string") {
  1443. return this._append_html_data(obj, $($.parseHTML(d)).filter(notTextOrCommentNode), function (status) { callback.call(this, status); });
  1444. // return callback.call(this, this._append_html_data(obj, $(d)));
  1445. }
  1446. 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 }) };
  1447. this.settings.core.error.call(this, this._data.core.last_error);
  1448. return callback.call(this, false);
  1449. }, this))
  1450. .fail($.proxy(function (f) {
  1451. 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 }) };
  1452. callback.call(this, false);
  1453. this.settings.core.error.call(this, this._data.core.last_error);
  1454. }, this));
  1455. }
  1456. if ($.isArray(s)) {
  1457. t = $.extend(true, [], s);
  1458. } else if ($.isPlainObject(s)) {
  1459. t = $.extend(true, {}, s);
  1460. } else {
  1461. t = s;
  1462. }
  1463. if(obj.id === $.jstree.root) {
  1464. return this._append_json_data(obj, t, function (status) {
  1465. callback.call(this, status);
  1466. });
  1467. }
  1468. else {
  1469. this._data.core.last_error = { 'error' : 'nodata', 'plugin' : 'core', 'id' : 'core_05', 'reason' : 'Could not load node', 'data' : JSON.stringify({ 'id' : obj.id }) };
  1470. this.settings.core.error.call(this, this._data.core.last_error);
  1471. return callback.call(this, false);
  1472. }
  1473. //return callback.call(this, (obj.id === $.jstree.root ? this._append_json_data(obj, t) : false) );
  1474. }
  1475. if(typeof s === 'string') {
  1476. if(obj.id === $.jstree.root) {
  1477. return this._append_html_data(obj, $($.parseHTML(s)).filter(notTextOrCommentNode), function (status) {
  1478. callback.call(this, status);
  1479. });
  1480. }
  1481. else {
  1482. this._data.core.last_error = { 'error' : 'nodata', 'plugin' : 'core', 'id' : 'core_06', 'reason' : 'Could not load node', 'data' : JSON.stringify({ 'id' : obj.id }) };
  1483. this.settings.core.error.call(this, this._data.core.last_error);
  1484. return callback.call(this, false);
  1485. }
  1486. //return callback.call(this, (obj.id === $.jstree.root ? this._append_html_data(obj, $(s)) : false) );
  1487. }
  1488. return callback.call(this, false);
  1489. },
  1490. /**
  1491. * adds a node to the list of nodes to redraw. Used only internally.
  1492. * @private
  1493. * @name _node_changed(obj [, callback])
  1494. * @param {mixed} obj
  1495. */
  1496. _node_changed : function (obj) {
  1497. obj = this.get_node(obj);
  1498. if (obj && $.inArray(obj.id, this._model.changed) === -1) {
  1499. this._model.changed.push(obj.id);
  1500. }
  1501. },
  1502. /**
  1503. * appends HTML content to the tree. Used internally.
  1504. * @private
  1505. * @name _append_html_data(obj, data)
  1506. * @param {mixed} obj the node to append to
  1507. * @param {String} data the HTML string to parse and append
  1508. * @trigger model.jstree, changed.jstree
  1509. */
  1510. _append_html_data : function (dom, data, cb) {
  1511. dom = this.get_node(dom);
  1512. dom.children = [];
  1513. dom.children_d = [];
  1514. var dat = data.is('ul') ? data.children() : data,
  1515. par = dom.id,
  1516. chd = [],
  1517. dpc = [],
  1518. m = this._model.data,
  1519. p = m[par],
  1520. s = this._data.core.selected.length,
  1521. tmp, i, j;
  1522. dat.each($.proxy(function (i, v) {
  1523. tmp = this._parse_model_from_html($(v), par, p.parents.concat());
  1524. if(tmp) {
  1525. chd.push(tmp);
  1526. dpc.push(tmp);
  1527. if(m[tmp].children_d.length) {
  1528. dpc = dpc.concat(m[tmp].children_d);
  1529. }
  1530. }
  1531. }, this));
  1532. p.children = chd;
  1533. p.children_d = dpc;
  1534. for(i = 0, j = p.parents.length; i < j; i++) {
  1535. m[p.parents[i]].children_d = m[p.parents[i]].children_d.concat(dpc);
  1536. }
  1537. /**
  1538. * triggered when new data is inserted to the tree model
  1539. * @event
  1540. * @name model.jstree
  1541. * @param {Array} nodes an array of node IDs
  1542. * @param {String} parent the parent ID of the nodes
  1543. */
  1544. this.trigger('model', { "nodes" : dpc, 'parent' : par });
  1545. if(par !== $.jstree.root) {
  1546. this._node_changed(par);
  1547. this.redraw();
  1548. }
  1549. else {
  1550. this.get_container_ul().children('.jstree-initial-node').remove();
  1551. this.redraw(true);
  1552. }
  1553. if(this._data.core.selected.length !== s) {
  1554. this.trigger('changed', { 'action' : 'model', 'selected' : this._data.core.selected });
  1555. }
  1556. cb.call(this, true);
  1557. },
  1558. /**
  1559. * appends JSON content to the tree. Used internally.
  1560. * @private
  1561. * @name _append_json_data(obj, data)
  1562. * @param {mixed} obj the node to append to
  1563. * @param {String} data the JSON object to parse and append
  1564. * @param {Boolean} force_processing internal param - do not set
  1565. * @trigger model.jstree, changed.jstree
  1566. */
  1567. _append_json_data : function (dom, data, cb, force_processing) {
  1568. if(this.element === null) { return; }
  1569. dom = this.get_node(dom);
  1570. dom.children = [];
  1571. dom.children_d = [];
  1572. // *%$@!!!
  1573. if(data.d) {
  1574. data = data.d;
  1575. if(typeof data === "string") {
  1576. data = JSON.parse(data);
  1577. }
  1578. }
  1579. if(!$.isArray(data)) { data = [data]; }
  1580. var w = null,
  1581. args = {
  1582. 'df' : this._model.default_state,
  1583. 'dat' : data,
  1584. 'par' : dom.id,
  1585. 'm' : this._model.data,
  1586. 't_id' : this._id,
  1587. 't_cnt' : this._cnt,
  1588. 'sel' : this._data.core.selected
  1589. },
  1590. inst = this,
  1591. func = function (data, undefined) {
  1592. if(data.data) { data = data.data; }
  1593. var dat = data.dat,
  1594. par = data.par,
  1595. chd = [],
  1596. dpc = [],
  1597. add = [],
  1598. df = data.df,
  1599. t_id = data.t_id,
  1600. t_cnt = data.t_cnt,
  1601. m = data.m,
  1602. p = m[par],
  1603. sel = data.sel,
  1604. tmp, i, j, rslt,
  1605. parse_flat = function (d, p, ps) {
  1606. if(!ps) { ps = []; }
  1607. else { ps = ps.concat(); }
  1608. if(p) { ps.unshift(p); }
  1609. var tid = d.id.toString(),
  1610. i, j, c, e,
  1611. tmp = {
  1612. id : tid,
  1613. text : d.text || '',
  1614. icon : d.icon !== undefined ? d.icon : true,
  1615. parent : p,
  1616. parents : ps,
  1617. children : d.children || [],
  1618. children_d : d.children_d || [],
  1619. data : d.data,
  1620. state : { },
  1621. li_attr : { id : false },
  1622. a_attr : { href : '#' },
  1623. original : false
  1624. };
  1625. for(i in df) {
  1626. if(df.hasOwnProperty(i)) {
  1627. tmp.state[i] = df[i];
  1628. }
  1629. }
  1630. if(d && d.data && d.data.jstree && d.data.jstree.icon) {
  1631. tmp.icon = d.data.jstree.icon;
  1632. }
  1633. if(tmp.icon === undefined || tmp.icon === null || tmp.icon === "") {
  1634. tmp.icon = true;
  1635. }
  1636. if(d && d.data) {
  1637. tmp.data = d.data;
  1638. if(d.data.jstree) {
  1639. for(i in d.data.jstree) {
  1640. if(d.data.jstree.hasOwnProperty(i)) {
  1641. tmp.state[i] = d.data.jstree[i];
  1642. }
  1643. }
  1644. }
  1645. }
  1646. if(d && typeof d.state === 'object') {
  1647. for (i in d.state) {
  1648. if(d.state.hasOwnProperty(i)) {
  1649. tmp.state[i] = d.state[i];
  1650. }
  1651. }
  1652. }
  1653. if(d && typeof d.li_attr === 'object') {
  1654. for (i in d.li_attr) {
  1655. if(d.li_attr.hasOwnProperty(i)) {
  1656. tmp.li_attr[i] = d.li_attr[i];
  1657. }
  1658. }
  1659. }
  1660. if(!tmp.li_attr.id) {
  1661. tmp.li_attr.id = tid;
  1662. }
  1663. if(d && typeof d.a_attr === 'object') {
  1664. for (i in d.a_attr) {
  1665. if(d.a_attr.hasOwnProperty(i)) {
  1666. tmp.a_attr[i] = d.a_attr[i];
  1667. }
  1668. }
  1669. }
  1670. if(d && d.children && d.children === true) {
  1671. tmp.state.loaded = false;
  1672. tmp.children = [];
  1673. tmp.children_d = [];
  1674. }
  1675. m[tmp.id] = tmp;
  1676. for(i = 0, j = tmp.children.length; i < j; i++) {
  1677. c = parse_flat(m[tmp.children[i]], tmp.id, ps);
  1678. e = m[c];
  1679. tmp.children_d.push(c);
  1680. if(e.children_d.length) {
  1681. tmp.children_d = tmp.children_d.concat(e.children_d);
  1682. }
  1683. }
  1684. delete d.data;
  1685. delete d.children;
  1686. m[tmp.id].original = d;
  1687. if(tmp.state.selected) {
  1688. add.push(tmp.id);
  1689. }
  1690. return tmp.id;
  1691. },
  1692. parse_nest = function (d, p, ps) {
  1693. if(!ps) { ps = []; }
  1694. else { ps = ps.concat(); }
  1695. if(p) { ps.unshift(p); }
  1696. var tid = false, i, j, c, e, tmp;
  1697. do {
  1698. tid = 'j' + t_id + '_' + (++t_cnt);
  1699. } while(m[tid]);
  1700. tmp = {
  1701. id : false,
  1702. text : typeof d === 'string' ? d : '',
  1703. icon : typeof d === 'object' && d.icon !== undefined ? d.icon : true,
  1704. parent : p,
  1705. parents : ps,
  1706. children : [],
  1707. children_d : [],
  1708. data : null,
  1709. state : { },
  1710. li_attr : { id : false },
  1711. a_attr : { href : '#' },
  1712. original : false
  1713. };
  1714. for(i in df) {
  1715. if(df.hasOwnProperty(i)) {
  1716. tmp.state[i] = df[i];
  1717. }
  1718. }
  1719. if(d && d.id) { tmp.id = d.id.toString(); }
  1720. if(d && d.text) { tmp.text = d.text; }
  1721. if(d && d.data && d.data.jstree && d.data.jstree.icon) {
  1722. tmp.icon = d.data.jstree.icon;
  1723. }
  1724. if(tmp.icon === undefined || tmp.icon === null || tmp.icon === "") {
  1725. tmp.icon = true;
  1726. }
  1727. if(d && d.data) {
  1728. tmp.data = d.data;
  1729. if(d.data.jstree) {
  1730. for(i in d.data.jstree) {
  1731. if(d.data.jstree.hasOwnProperty(i)) {
  1732. tmp.state[i] = d.data.jstree[i];
  1733. }
  1734. }
  1735. }
  1736. }
  1737. if(d && typeof d.state === 'object') {
  1738. for (i in d.state) {
  1739. if(d.state.hasOwnProperty(i)) {
  1740. tmp.state[i] = d.state[i];
  1741. }
  1742. }
  1743. }
  1744. if(d && typeof d.li_attr === 'object') {
  1745. for (i in d.li_attr) {
  1746. if(d.li_attr.hasOwnProperty(i)) {
  1747. tmp.li_attr[i] = d.li_attr[i];
  1748. }
  1749. }
  1750. }
  1751. if(tmp.li_attr.id && !tmp.id) {
  1752. tmp.id = tmp.li_attr.id.toString();
  1753. }
  1754. if(!tmp.id) {
  1755. tmp.id = tid;
  1756. }
  1757. if(!tmp.li_attr.id) {
  1758. tmp.li_attr.id = tmp.id;
  1759. }
  1760. if(d && typeof d.a_attr === 'object') {
  1761. for (i in d.a_attr) {
  1762. if(d.a_attr.hasOwnProperty(i)) {
  1763. tmp.a_attr[i] = d.a_attr[i];
  1764. }
  1765. }
  1766. }
  1767. if(d && d.children && d.children.length) {
  1768. for(i = 0, j = d.children.length; i < j; i++) {
  1769. c = parse_nest(d.children[i], tmp.id, ps);
  1770. e = m[c];
  1771. tmp.children.push(c);
  1772. if(e.children_d.length) {
  1773. tmp.children_d = tmp.children_d.concat(e.children_d);
  1774. }
  1775. }
  1776. tmp.children_d = tmp.children_d.concat(tmp.children);
  1777. }
  1778. if(d && d.children && d.children === true) {
  1779. tmp.state.loaded = false;
  1780. tmp.children = [];
  1781. tmp.children_d = [];
  1782. }
  1783. delete d.data;
  1784. delete d.children;
  1785. tmp.original = d;
  1786. m[tmp.id] = tmp;
  1787. if(tmp.state.selected) {
  1788. add.push(tmp.id);
  1789. }
  1790. return tmp.id;
  1791. };
  1792. if(dat.length && dat[0].id !== undefined && dat[0].parent !== undefined) {
  1793. // Flat JSON support (for easy import from DB):
  1794. // 1) convert to object (foreach)
  1795. for(i = 0, j = dat.length; i < j; i++) {
  1796. if(!dat[i].children) {
  1797. dat[i].children = [];
  1798. }
  1799. if(!dat[i].state) {
  1800. dat[i].state = {};
  1801. }
  1802. m[dat[i].id.toString()] = dat[i];
  1803. }
  1804. // 2) populate children (foreach)
  1805. for(i = 0, j = dat.length; i < j; i++) {
  1806. if (!m[dat[i].parent.toString()]) {
  1807. if (typeof inst !== "undefined") {
  1808. inst._data.core.last_error = { 'error' : 'parse', 'plugin' : 'core', 'id' : 'core_07', 'reason' : 'Node with invalid parent', 'data' : JSON.stringify({ 'id' : dat[i].id.toString(), 'parent' : dat[i].parent.toString() }) };
  1809. inst.settings.core.error.call(inst, inst._data.core.last_error);
  1810. }
  1811. continue;
  1812. }
  1813. m[dat[i].parent.toString()].children.push(dat[i].id.toString());
  1814. // populate parent.children_d
  1815. p.children_d.push(dat[i].id.toString());
  1816. }
  1817. // 3) normalize && populate parents and children_d with recursion
  1818. for(i = 0, j = p.children.length; i < j; i++) {
  1819. tmp = parse_flat(m[p.children[i]], par, p.parents.concat());
  1820. dpc.push(tmp);
  1821. if(m[tmp].children_d.length) {
  1822. dpc = dpc.concat(m[tmp].children_d);
  1823. }
  1824. }
  1825. for(i = 0, j = p.parents.length; i < j; i++) {
  1826. m[p.parents[i]].children_d = m[p.parents[i]].children_d.concat(dpc);
  1827. }
  1828. // ?) three_state selection - p.state.selected && t - (if three_state foreach(dat => ch) -> foreach(parents) if(parent.selected) child.selected = true;
  1829. rslt = {
  1830. 'cnt' : t_cnt,
  1831. 'mod' : m,
  1832. 'sel' : sel,
  1833. 'par' : par,
  1834. 'dpc' : dpc,
  1835. 'add' : add
  1836. };
  1837. }
  1838. else {
  1839. for(i = 0, j = dat.length; i < j; i++) {
  1840. tmp = parse_nest(dat[i], par, p.parents.concat());
  1841. if(tmp) {
  1842. chd.push(tmp);
  1843. dpc.push(tmp);
  1844. if(m[tmp].children_d.length) {
  1845. dpc = dpc.concat(m[tmp].children_d);
  1846. }
  1847. }
  1848. }
  1849. p.children = chd;
  1850. p.children_d = dpc;
  1851. for(i = 0, j = p.parents.length; i < j; i++) {
  1852. m[p.parents[i]].children_d = m[p.parents[i]].children_d.concat(dpc);
  1853. }
  1854. rslt = {
  1855. 'cnt' : t_cnt,
  1856. 'mod' : m,
  1857. 'sel' : sel,
  1858. 'par' : par,
  1859. 'dpc' : dpc,
  1860. 'add' : add
  1861. };
  1862. }
  1863. if(typeof window === 'undefined' || typeof window.document === 'undefined') {
  1864. postMessage(rslt);
  1865. }
  1866. else {
  1867. return rslt;
  1868. }
  1869. },
  1870. rslt = function (rslt, worker) {
  1871. if(this.element === null) { return; }
  1872. this._cnt = rslt.cnt;
  1873. var i, m = this._model.data;
  1874. for (i in m) {
  1875. if (m.hasOwnProperty(i) && m[i].state && m[i].state.loading && rslt.mod[i]) {
  1876. rslt.mod[i].state.loading = true;
  1877. }
  1878. }
  1879. this._model.data = rslt.mod; // breaks the reference in load_node - careful
  1880. if(worker) {
  1881. var j, a = rslt.add, r = rslt.sel, s = this._data.core.selected.slice();
  1882. m = this._model.data;
  1883. // if selection was changed while calculating in worker
  1884. if(r.length !== s.length || $.vakata.array_unique(r.concat(s)).length !== r.length) {
  1885. // deselect nodes that are no longer selected
  1886. for(i = 0, j = r.length; i < j; i++) {
  1887. if($.inArray(r[i], a) === -1 && $.inArray(r[i], s) === -1) {
  1888. m[r[i]].state.selected = false;
  1889. }
  1890. }
  1891. // select nodes that were selected in the mean time
  1892. for(i = 0, j = s.length; i < j; i++) {
  1893. if($.inArray(s[i], r) === -1) {
  1894. m[s[i]].state.selected = true;
  1895. }
  1896. }
  1897. }
  1898. }
  1899. if(rslt.add.length) {
  1900. this._data.core.selected = this._data.core.selected.concat(rslt.add);
  1901. }
  1902. this.trigger('model', { "nodes" : rslt.dpc, 'parent' : rslt.par });
  1903. if(rslt.par !== $.jstree.root) {
  1904. this._node_changed(rslt.par);
  1905. this.redraw();
  1906. }
  1907. else {
  1908. // this.get_container_ul().children('.jstree-initial-node').remove();
  1909. this.redraw(true);
  1910. }
  1911. if(rslt.add.length) {
  1912. this.trigger('changed', { 'action' : 'model', 'selected' : this._data.core.selected });
  1913. }
  1914. cb.call(this, true);
  1915. };
  1916. if(this.settings.core.worker && window.Blob && window.URL && window.Worker) {
  1917. try {
  1918. if(this._wrk === null) {
  1919. this._wrk = window.URL.createObjectURL(
  1920. new window.Blob(
  1921. ['self.onmessage = ' + func.toString()],
  1922. {type:"text/javascript"}
  1923. )
  1924. );
  1925. }
  1926. if(!this._data.core.working || force_processing) {
  1927. this._data.core.working = true;
  1928. w = new window.Worker(this._wrk);
  1929. w.onmessage = $.proxy(function (e) {
  1930. rslt.call(this, e.data, true);
  1931. try { w.terminate(); w = null; } catch(ignore) { }
  1932. if(this._data.core.worker_queue.length) {
  1933. this._append_json_data.apply(this, this._data.core.worker_queue.shift());
  1934. }
  1935. else {
  1936. this._data.core.working = false;
  1937. }
  1938. }, this);
  1939. if(!args.par) {
  1940. if(this._data.core.worker_queue.length) {
  1941. this._append_json_data.apply(this, this._data.core.worker_queue.shift());
  1942. }
  1943. else {
  1944. this._data.core.working = false;
  1945. }
  1946. }
  1947. else {
  1948. w.postMessage(args);
  1949. }
  1950. }
  1951. else {
  1952. this._data.core.worker_queue.push([dom, data, cb, true]);
  1953. }
  1954. }
  1955. catch(e) {
  1956. rslt.call(this, func(args), false);
  1957. if(this._data.core.worker_queue.length) {
  1958. this._append_json_data.apply(this, this._data.core.worker_queue.shift());
  1959. }
  1960. else {
  1961. this._data.core.working = false;
  1962. }
  1963. }
  1964. }
  1965. else {
  1966. rslt.call(this, func(args), false);
  1967. }
  1968. },
  1969. /**
  1970. * parses a node from a jQuery object and appends them to the in memory tree model. Used internally.
  1971. * @private
  1972. * @name _parse_model_from_html(d [, p, ps])
  1973. * @param {jQuery} d the jQuery object to parse
  1974. * @param {String} p the parent ID
  1975. * @param {Array} ps list of all parents
  1976. * @return {String} the ID of the object added to the model
  1977. */
  1978. _parse_model_from_html : function (d, p, ps) {
  1979. if(!ps) { ps = []; }
  1980. else { ps = [].concat(ps); }
  1981. if(p) { ps.unshift(p); }
  1982. var c, e, m = this._model.data,
  1983. data = {
  1984. id : false,
  1985. text : false,
  1986. icon : true,
  1987. parent : p,
  1988. parents : ps,
  1989. children : [],
  1990. children_d : [],
  1991. data : null,
  1992. state : { },
  1993. li_attr : { id : false },
  1994. a_attr : { href : '#' },
  1995. original : false
  1996. }, i, tmp, tid;
  1997. for(i in this._model.default_state) {
  1998. if(this._model.default_state.hasOwnProperty(i)) {
  1999. data.state[i] = this._model.default_state[i];
  2000. }
  2001. }
  2002. tmp = $.vakata.attributes(d, true);
  2003. $.each(tmp, function (i, v) {
  2004. v = $.trim(v);
  2005. if(!v.length) { return true; }
  2006. data.li_attr[i] = v;
  2007. if(i === 'id') {
  2008. data.id = v.toString();
  2009. }
  2010. });
  2011. tmp = d.children('a').first();
  2012. if(tmp.length) {
  2013. tmp = $.vakata.attributes(tmp, true);
  2014. $.each(tmp, function (i, v) {
  2015. v = $.trim(v);
  2016. if(v.length) {
  2017. data.a_attr[i] = v;
  2018. }
  2019. });
  2020. }
  2021. tmp = d.children("a").first().length ? d.children("a").first().clone() : d.clone();
  2022. tmp.children("ins, i, ul").remove();
  2023. tmp = tmp.html();
  2024. tmp = $('<div />').html(tmp);
  2025. data.text = this.settings.core.force_text ? tmp.text() : tmp.html();
  2026. tmp = d.data();
  2027. data.data = tmp ? $.extend(true, {}, tmp) : null;
  2028. data.state.opened = d.hasClass('jstree-open');
  2029. data.state.selected = d.children('a').hasClass('jstree-clicked');
  2030. data.state.disabled = d.children('a').hasClass('jstree-disabled');
  2031. if(data.data && data.data.jstree) {
  2032. for(i in data.data.jstree) {
  2033. if(data.data.jstree.hasOwnProperty(i)) {
  2034. data.state[i] = data.data.jstree[i];
  2035. }
  2036. }
  2037. }
  2038. tmp = d.children("a").children(".jstree-themeicon");
  2039. if(tmp.length) {
  2040. data.icon = tmp.hasClass('jstree-themeicon-hidden') ? false : tmp.attr('rel');
  2041. }
  2042. if(data.state.icon !== undefined) {
  2043. data.icon = data.state.icon;
  2044. }
  2045. if(data.icon === undefined || data.icon === null || data.icon === "") {
  2046. data.icon = true;
  2047. }
  2048. tmp = d.children("ul").children("li");
  2049. do {
  2050. tid = 'j' + this._id + '_' + (++this._cnt);
  2051. } while(m[tid]);
  2052. data.id = data.li_attr.id ? data.li_attr.id.toString() : tid;
  2053. if(tmp.length) {
  2054. tmp.each($.proxy(function (i, v) {
  2055. c = this._parse_model_from_html($(v), data.id, ps);
  2056. e = this._model.data[c];
  2057. data.children.push(c);
  2058. if(e.children_d.length) {
  2059. data.children_d = data.children_d.concat(e.children_d);
  2060. }
  2061. }, this));
  2062. data.children_d = data.children_d.concat(data.children);
  2063. }
  2064. else {
  2065. if(d.hasClass('jstree-closed')) {
  2066. data.state.loaded = false;
  2067. }
  2068. }
  2069. if(data.li_attr['class']) {
  2070. data.li_attr['class'] = data.li_attr['class'].replace('jstree-closed','').replace('jstree-open','');
  2071. }
  2072. if(data.a_attr['class']) {
  2073. data.a_attr['class'] = data.a_attr['class'].replace('jstree-clicked','').replace('jstree-disabled','');
  2074. }
  2075. m[data.id] = data;
  2076. if(data.state.selected) {
  2077. this._data.core.selected.push(data.id);
  2078. }
  2079. return data.id;
  2080. },
  2081. /**
  2082. * 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.
  2083. * @private
  2084. * @name _parse_model_from_flat_json(d [, p, ps])
  2085. * @param {Object} d the JSON object to parse
  2086. * @param {String} p the parent ID
  2087. * @param {Array} ps list of all parents
  2088. * @return {String} the ID of the object added to the model
  2089. */
  2090. _parse_model_from_flat_json : function (d, p, ps) {
  2091. if(!ps) { ps = []; }
  2092. else { ps = ps.concat(); }
  2093. if(p) { ps.unshift(p); }
  2094. var tid = d.id.toString(),
  2095. m = this._model.data,
  2096. df = this._model.default_state,
  2097. i, j, c, e,
  2098. tmp = {
  2099. id : tid,
  2100. text : d.text || '',
  2101. icon : d.icon !== undefined ? d.icon : true,
  2102. parent : p,
  2103. parents : ps,
  2104. children : d.children || [],
  2105. children_d : d.children_d || [],
  2106. data : d.data,
  2107. state : { },
  2108. li_attr : { id : false },
  2109. a_attr : { href : '#' },
  2110. original : false
  2111. };
  2112. for(i in df) {
  2113. if(df.hasOwnProperty(i)) {
  2114. tmp.state[i] = df[i];
  2115. }
  2116. }
  2117. if(d && d.data && d.data.jstree && d.data.jstree.icon) {
  2118. tmp.icon = d.data.jstree.icon;
  2119. }
  2120. if(tmp.icon === undefined || tmp.icon === null || tmp.icon === "") {
  2121. tmp.icon = true;
  2122. }
  2123. if(d && d.data) {
  2124. tmp.data = d.data;
  2125. if(d.data.jstree) {
  2126. for(i in d.data.jstree) {
  2127. if(d.data.jstree.hasOwnProperty(i)) {
  2128. tmp.state[i] = d.data.jstree[i];
  2129. }
  2130. }
  2131. }
  2132. }
  2133. if(d && typeof d.state === 'object') {
  2134. for (i in d.state) {
  2135. if(d.state.hasOwnProperty(i)) {
  2136. tmp.state[i] = d.state[i];
  2137. }
  2138. }
  2139. }
  2140. if(d && typeof d.li_attr === 'object') {
  2141. for (i in d.li_attr) {
  2142. if(d.li_attr.hasOwnProperty(i)) {
  2143. tmp.li_attr[i] = d.li_attr[i];
  2144. }
  2145. }
  2146. }
  2147. if(!tmp.li_attr.id) {
  2148. tmp.li_attr.id = tid;
  2149. }
  2150. if(d && typeof d.a_attr === 'object') {
  2151. for (i in d.a_attr) {
  2152. if(d.a_attr.hasOwnProperty(i)) {
  2153. tmp.a_attr[i] = d.a_attr[i];
  2154. }
  2155. }
  2156. }
  2157. if(d && d.children && d.children === true) {
  2158. tmp.state.loaded = false;
  2159. tmp.children = [];
  2160. tmp.children_d = [];
  2161. }
  2162. m[tmp.id] = tmp;
  2163. for(i = 0, j = tmp.children.length; i < j; i++) {
  2164. c = this._parse_model_from_flat_json(m[tmp.children[i]], tmp.id, ps);
  2165. e = m[c];
  2166. tmp.children_d.push(c);
  2167. if(e.children_d.length) {
  2168. tmp.children_d = tmp.children_d.concat(e.children_d);
  2169. }
  2170. }
  2171. delete d.data;
  2172. delete d.children;
  2173. m[tmp.id].original = d;
  2174. if(tmp.state.selected) {
  2175. this._data.core.selected.push(tmp.id);
  2176. }
  2177. return tmp.id;
  2178. },
  2179. /**
  2180. * parses a node from a JSON object and appends it to the in memory tree model. Used internally.
  2181. * @private
  2182. * @name _parse_model_from_json(d [, p, ps])
  2183. * @param {Object} d the JSON object to parse
  2184. * @param {String} p the parent ID
  2185. * @param {Array} ps list of all parents
  2186. * @return {String} the ID of the object added to the model
  2187. */
  2188. _parse_model_from_json : function (d, p, ps) {
  2189. if(!ps) { ps = []; }
  2190. else { ps = ps.concat(); }
  2191. if(p) { ps.unshift(p); }
  2192. var tid = false, i, j, c, e, m = this._model.data, df = this._model.default_state, tmp;
  2193. do {
  2194. tid = 'j' + this._id + '_' + (++this._cnt);
  2195. } while(m[tid]);
  2196. tmp = {
  2197. id : false,
  2198. text : typeof d === 'string' ? d : '',
  2199. icon : typeof d === 'object' && d.icon !== undefined ? d.icon : true,
  2200. parent : p,
  2201. parents : ps,
  2202. children : [],
  2203. children_d : [],
  2204. data : null,
  2205. state : { },
  2206. li_attr : { id : false },
  2207. a_attr : { href : '#' },
  2208. original : false
  2209. };
  2210. for(i in df) {
  2211. if(df.hasOwnProperty(i)) {
  2212. tmp.state[i] = df[i];
  2213. }
  2214. }
  2215. if(d && d.id) { tmp.id = d.id.toString(); }
  2216. if(d && d.text) { tmp.text = d.text; }
  2217. if(d && d.data && d.data.jstree && d.data.jstree.icon) {
  2218. tmp.icon = d.data.jstree.icon;
  2219. }
  2220. if(tmp.icon === undefined || tmp.icon === null || tmp.icon === "") {
  2221. tmp.icon = true;
  2222. }
  2223. if(d && d.data) {
  2224. tmp.data = d.data;
  2225. if(d.data.jstree) {
  2226. for(i in d.data.jstree) {
  2227. if(d.data.jstree.hasOwnProperty(i)) {
  2228. tmp.state[i] = d.data.jstree[i];
  2229. }
  2230. }
  2231. }
  2232. }
  2233. if(d && typeof d.state === 'object') {
  2234. for (i in d.state) {
  2235. if(d.state.hasOwnProperty(i)) {
  2236. tmp.state[i] = d.state[i];
  2237. }
  2238. }
  2239. }
  2240. if(d && typeof d.li_attr === 'object') {
  2241. for (i in d.li_attr) {
  2242. if(d.li_attr.hasOwnProperty(i)) {
  2243. tmp.li_attr[i] = d.li_attr[i];
  2244. }
  2245. }
  2246. }
  2247. if(tmp.li_attr.id && !tmp.id) {
  2248. tmp.id = tmp.li_attr.id.toString();
  2249. }
  2250. if(!tmp.id) {
  2251. tmp.id = tid;
  2252. }
  2253. if(!tmp.li_attr.id) {
  2254. tmp.li_attr.id = tmp.id;
  2255. }
  2256. if(d && typeof d.a_attr === 'object') {
  2257. for (i in d.a_attr) {
  2258. if(d.a_attr.hasOwnProperty(i)) {
  2259. tmp.a_attr[i] = d.a_attr[i];
  2260. }
  2261. }
  2262. }
  2263. if(d && d.children && d.children.length) {
  2264. for(i = 0, j = d.children.length; i < j; i++) {
  2265. c = this._parse_model_from_json(d.children[i], tmp.id, ps);
  2266. e = m[c];
  2267. tmp.children.push(c);
  2268. if(e.children_d.length) {
  2269. tmp.children_d = tmp.children_d.concat(e.children_d);
  2270. }
  2271. }
  2272. tmp.children_d = tmp.children.concat(tmp.children_d);
  2273. }
  2274. if(d && d.children && d.children === true) {
  2275. tmp.state.loaded = false;
  2276. tmp.children = [];
  2277. tmp.children_d = [];
  2278. }
  2279. delete d.data;
  2280. delete d.children;
  2281. tmp.original = d;
  2282. m[tmp.id] = tmp;
  2283. if(tmp.state.selected) {
  2284. this._data.core.selected.push(tmp.id);
  2285. }
  2286. return tmp.id;
  2287. },
  2288. /**
  2289. * redraws all nodes that need to be redrawn. Used internally.
  2290. * @private
  2291. * @name _redraw()
  2292. * @trigger redraw.jstree
  2293. */
  2294. _redraw : function () {
  2295. var nodes = this._model.force_full_redraw ? this._model.data[$.jstree.root].children.concat([]) : this._model.changed.concat([]),
  2296. f = document.createElement('UL'), tmp, i, j, fe = this._data.core.focused;
  2297. for(i = 0, j = nodes.length; i < j; i++) {
  2298. tmp = this.redraw_node(nodes[i], true, this._model.force_full_redraw);
  2299. if(tmp && this._model.force_full_redraw) {
  2300. f.appendChild(tmp);
  2301. }
  2302. }
  2303. if(this._model.force_full_redraw) {
  2304. f.className = this.get_container_ul()[0].className;
  2305. f.setAttribute('role','group');
  2306. this.element.empty().append(f);
  2307. //this.get_container_ul()[0].appendChild(f);
  2308. }
  2309. if(fe !== null && this.settings.core.restore_focus) {
  2310. tmp = this.get_node(fe, true);
  2311. if(tmp && tmp.length && tmp.children('.jstree-anchor')[0] !== document.activeElement) {
  2312. tmp.children('.jstree-anchor').focus();
  2313. }
  2314. else {
  2315. this._data.core.focused = null;
  2316. }
  2317. }
  2318. this._model.force_full_redraw = false;
  2319. this._model.changed = [];
  2320. /**
  2321. * triggered after nodes are redrawn
  2322. * @event
  2323. * @name redraw.jstree
  2324. * @param {array} nodes the redrawn nodes
  2325. */
  2326. this.trigger('redraw', { "nodes" : nodes });
  2327. },
  2328. /**
  2329. * redraws all nodes that need to be redrawn or optionally - the whole tree
  2330. * @name redraw([full])
  2331. * @param {Boolean} full if set to `true` all nodes are redrawn.
  2332. */
  2333. redraw : function (full) {
  2334. if(full) {
  2335. this._model.force_full_redraw = true;
  2336. }
  2337. //if(this._model.redraw_timeout) {
  2338. // clearTimeout(this._model.redraw_timeout);
  2339. //}
  2340. //this._model.redraw_timeout = setTimeout($.proxy(this._redraw, this),0);
  2341. this._redraw();
  2342. },
  2343. /**
  2344. * redraws a single node's children. Used internally.
  2345. * @private
  2346. * @name draw_children(node)
  2347. * @param {mixed} node the node whose children will be redrawn
  2348. */
  2349. draw_children : function (node) {
  2350. var obj = this.get_node(node),
  2351. i = false,
  2352. j = false,
  2353. k = false,
  2354. d = document;
  2355. if(!obj) { return false; }
  2356. if(obj.id === $.jstree.root) { return this.redraw(true); }
  2357. node = this.get_node(node, true);
  2358. if(!node || !node.length) { return false; } // TODO: quick toggle
  2359. node.children('.jstree-children').remove();
  2360. node = node[0];
  2361. if(obj.children.length && obj.state.loaded) {
  2362. k = d.createElement('UL');
  2363. k.setAttribute('role', 'group');
  2364. k.className = 'jstree-children';
  2365. for(i = 0, j = obj.children.length; i < j; i++) {
  2366. k.appendChild(this.redraw_node(obj.children[i], true, true));
  2367. }
  2368. node.appendChild(k);
  2369. }
  2370. },
  2371. /**
  2372. * redraws a single node. Used internally.
  2373. * @private
  2374. * @name redraw_node(node, deep, is_callback, force_render)
  2375. * @param {mixed} node the node to redraw
  2376. * @param {Boolean} deep should child nodes be redrawn too
  2377. * @param {Boolean} is_callback is this a recursion call
  2378. * @param {Boolean} force_render should children of closed parents be drawn anyway
  2379. */
  2380. redraw_node : function (node, deep, is_callback, force_render) {
  2381. var obj = this.get_node(node),
  2382. par = false,
  2383. ind = false,
  2384. old = false,
  2385. i = false,
  2386. j = false,
  2387. k = false,
  2388. c = '',
  2389. d = document,
  2390. m = this._model.data,
  2391. f = false,
  2392. s = false,
  2393. tmp = null,
  2394. t = 0,
  2395. l = 0,
  2396. has_children = false,
  2397. last_sibling = false;
  2398. if(!obj) { return false; }
  2399. if(obj.id === $.jstree.root) { return this.redraw(true); }
  2400. deep = deep || obj.children.length === 0;
  2401. 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);
  2402. if(!node) {
  2403. deep = true;
  2404. //node = d.createElement('LI');
  2405. if(!is_callback) {
  2406. par = obj.parent !== $.jstree.root ? $('#' + obj.parent.replace($.jstree.idregex,'\\$&'), this.element)[0] : null;
  2407. if(par !== null && (!par || !m[obj.parent].state.opened)) {
  2408. return false;
  2409. }
  2410. ind = $.inArray(obj.id, par === null ? m[$.jstree.root].children : m[obj.parent].children);
  2411. }
  2412. }
  2413. else {
  2414. node = $(node);
  2415. if(!is_callback) {
  2416. par = node.parent().parent()[0];
  2417. if(par === this.element[0]) {
  2418. par = null;
  2419. }
  2420. ind = node.index();
  2421. }
  2422. // m[obj.id].data = node.data(); // use only node's data, no need to touch jquery storage
  2423. if(!deep && obj.children.length && !node.children('.jstree-children').length) {
  2424. deep = true;
  2425. }
  2426. if(!deep) {
  2427. old = node.children('.jstree-children')[0];
  2428. }
  2429. f = node.children('.jstree-anchor')[0] === document.activeElement;
  2430. node.remove();
  2431. //node = d.createElement('LI');
  2432. //node = node[0];
  2433. }
  2434. node = this._data.core.node.cloneNode(true);
  2435. // node is DOM, deep is boolean
  2436. c = 'jstree-node ';
  2437. for(i in obj.li_attr) {
  2438. if(obj.li_attr.hasOwnProperty(i)) {
  2439. if(i === 'id') { continue; }
  2440. if(i !== 'class') {
  2441. node.setAttribute(i, obj.li_attr[i]);
  2442. }
  2443. else {
  2444. c += obj.li_attr[i];
  2445. }
  2446. }
  2447. }
  2448. if(!obj.a_attr.id) {
  2449. obj.a_attr.id = obj.id + '_anchor';
  2450. }
  2451. node.setAttribute('aria-selected', !!obj.state.selected);
  2452. node.setAttribute('aria-level', obj.parents.length);
  2453. node.setAttribute('aria-labelledby', obj.a_attr.id);
  2454. if(obj.state.disabled) {
  2455. node.setAttribute('aria-disabled', true);
  2456. }
  2457. for(i = 0, j = obj.children.length; i < j; i++) {
  2458. if(!m[obj.children[i]].state.hidden) {
  2459. has_children = true;
  2460. break;
  2461. }
  2462. }
  2463. if(obj.parent !== null && m[obj.parent] && !obj.state.hidden) {
  2464. i = $.inArray(obj.id, m[obj.parent].children);
  2465. last_sibling = obj.id;
  2466. if(i !== -1) {
  2467. i++;
  2468. for(j = m[obj.parent].children.length; i < j; i++) {
  2469. if(!m[m[obj.parent].children[i]].state.hidden) {
  2470. last_sibling = m[obj.parent].children[i];
  2471. }
  2472. if(last_sibling !== obj.id) {
  2473. break;
  2474. }
  2475. }
  2476. }
  2477. }
  2478. if(obj.state.hidden) {
  2479. c += ' jstree-hidden';
  2480. }
  2481. if (obj.state.loading) {
  2482. c += ' jstree-loading';
  2483. }
  2484. if(obj.state.loaded && !has_children) {
  2485. c += ' jstree-leaf';
  2486. }
  2487. else {
  2488. c += obj.state.opened && obj.state.loaded ? ' jstree-open' : ' jstree-closed';
  2489. node.setAttribute('aria-expanded', (obj.state.opened && obj.state.loaded) );
  2490. }
  2491. if(last_sibling === obj.id) {
  2492. c += ' jstree-last';
  2493. }
  2494. node.id = obj.id;
  2495. node.className = c;
  2496. c = ( obj.state.selected ? ' jstree-clicked' : '') + ( obj.state.disabled ? ' jstree-disabled' : '');
  2497. for(j in obj.a_attr) {
  2498. if(obj.a_attr.hasOwnProperty(j)) {
  2499. if(j === 'href' && obj.a_attr[j] === '#') { continue; }
  2500. if(j !== 'class') {
  2501. node.childNodes[1].setAttribute(j, obj.a_attr[j]);
  2502. }
  2503. else {
  2504. c += ' ' + obj.a_attr[j];
  2505. }
  2506. }
  2507. }
  2508. if(c.length) {
  2509. node.childNodes[1].className = 'jstree-anchor ' + c;
  2510. }
  2511. if((obj.icon && obj.icon !== true) || obj.icon === false) {
  2512. if(obj.icon === false) {
  2513. node.childNodes[1].childNodes[0].className += ' jstree-themeicon-hidden';
  2514. }
  2515. else if(obj.icon.indexOf('/') === -1 && obj.icon.indexOf('.') === -1) {
  2516. node.childNodes[1].childNodes[0].className += ' ' + obj.icon + ' jstree-themeicon-custom';
  2517. }
  2518. else {
  2519. node.childNodes[1].childNodes[0].style.backgroundImage = 'url("'+obj.icon+'")';
  2520. node.childNodes[1].childNodes[0].style.backgroundPosition = 'center center';
  2521. node.childNodes[1].childNodes[0].style.backgroundSize = 'auto';
  2522. node.childNodes[1].childNodes[0].className += ' jstree-themeicon-custom';
  2523. }
  2524. }
  2525. if(this.settings.core.force_text) {
  2526. node.childNodes[1].appendChild(d.createTextNode(obj.text));
  2527. }
  2528. else {
  2529. node.childNodes[1].innerHTML += obj.text;
  2530. }
  2531. if(deep && obj.children.length && (obj.state.opened || force_render) && obj.state.loaded) {
  2532. k = d.createElement('UL');
  2533. k.setAttribute('role', 'group');
  2534. k.className = 'jstree-children';
  2535. for(i = 0, j = obj.children.length; i < j; i++) {
  2536. k.appendChild(this.redraw_node(obj.children[i], deep, true));
  2537. }
  2538. node.appendChild(k);
  2539. }
  2540. if(old) {
  2541. node.appendChild(old);
  2542. }
  2543. if(!is_callback) {
  2544. // append back using par / ind
  2545. if(!par) {
  2546. par = this.element[0];
  2547. }
  2548. for(i = 0, j = par.childNodes.length; i < j; i++) {
  2549. if(par.childNodes[i] && par.childNodes[i].className && par.childNodes[i].className.indexOf('jstree-children') !== -1) {
  2550. tmp = par.childNodes[i];
  2551. break;
  2552. }
  2553. }
  2554. if(!tmp) {
  2555. tmp = d.createElement('UL');
  2556. tmp.setAttribute('role', 'group');
  2557. tmp.className = 'jstree-children';
  2558. par.appendChild(tmp);
  2559. }
  2560. par = tmp;
  2561. if(ind < par.childNodes.length) {
  2562. par.insertBefore(node, par.childNodes[ind]);
  2563. }
  2564. else {
  2565. par.appendChild(node);
  2566. }
  2567. if(f) {
  2568. t = this.element[0].scrollTop;
  2569. l = this.element[0].scrollLeft;
  2570. node.childNodes[1].focus();
  2571. this.element[0].scrollTop = t;
  2572. this.element[0].scrollLeft = l;
  2573. }
  2574. }
  2575. if(obj.state.opened && !obj.state.loaded) {
  2576. obj.state.opened = false;
  2577. setTimeout($.proxy(function () {
  2578. this.open_node(obj.id, false, 0);
  2579. }, this), 0);
  2580. }
  2581. return node;
  2582. },
  2583. /**
  2584. * opens a node, revealing its children. If the node is not loaded it will be loaded and opened once ready.
  2585. * @name open_node(obj [, callback, animation])
  2586. * @param {mixed} obj the node to open
  2587. * @param {Function} callback a function to execute once the node is opened
  2588. * @param {Number} animation the animation duration in milliseconds when opening the node (overrides the `core.animation` setting). Use `false` for no animation.
  2589. * @trigger open_node.jstree, after_open.jstree, before_open.jstree
  2590. */
  2591. open_node : function (obj, callback, animation) {
  2592. var t1, t2, d, t;
  2593. if($.isArray(obj)) {
  2594. obj = obj.slice();
  2595. for(t1 = 0, t2 = obj.length; t1 < t2; t1++) {
  2596. this.open_node(obj[t1], callback, animation);
  2597. }
  2598. return true;
  2599. }
  2600. obj = this.get_node(obj);
  2601. if(!obj || obj.id === $.jstree.root) {
  2602. return false;
  2603. }
  2604. animation = animation === undefined ? this.settings.core.animation : animation;
  2605. if(!this.is_closed(obj)) {
  2606. if(callback) {
  2607. callback.call(this, obj, false);
  2608. }
  2609. return false;
  2610. }
  2611. if(!this.is_loaded(obj)) {
  2612. if(this.is_loading(obj)) {
  2613. return setTimeout($.proxy(function () {
  2614. this.open_node(obj, callback, animation);
  2615. }, this), 500);
  2616. }
  2617. this.load_node(obj, function (o, ok) {
  2618. return ok ? this.open_node(o, callback, animation) : (callback ? callback.call(this, o, false) : false);
  2619. });
  2620. }
  2621. else {
  2622. d = this.get_node(obj, true);
  2623. t = this;
  2624. if(d.length) {
  2625. if(animation && d.children(".jstree-children").length) {
  2626. d.children(".jstree-children").stop(true, true);
  2627. }
  2628. if(obj.children.length && !this._firstChild(d.children('.jstree-children')[0])) {
  2629. this.draw_children(obj);
  2630. //d = this.get_node(obj, true);
  2631. }
  2632. if(!animation) {
  2633. this.trigger('before_open', { "node" : obj });
  2634. d[0].className = d[0].className.replace('jstree-closed', 'jstree-open');
  2635. d[0].setAttribute("aria-expanded", true);
  2636. }
  2637. else {
  2638. this.trigger('before_open', { "node" : obj });
  2639. d
  2640. .children(".jstree-children").css("display","none").end()
  2641. .removeClass("jstree-closed").addClass("jstree-open").attr("aria-expanded", true)
  2642. .children(".jstree-children").stop(true, true)
  2643. .slideDown(animation, function () {
  2644. this.style.display = "";
  2645. if (t.element) {
  2646. t.trigger("after_open", { "node" : obj });
  2647. }
  2648. });
  2649. }
  2650. }
  2651. obj.state.opened = true;
  2652. if(callback) {
  2653. callback.call(this, obj, true);
  2654. }
  2655. if(!d.length) {
  2656. /**
  2657. * 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)
  2658. * @event
  2659. * @name before_open.jstree
  2660. * @param {Object} node the opened node
  2661. */
  2662. this.trigger('before_open', { "node" : obj });
  2663. }
  2664. /**
  2665. * triggered when a node is opened (if there is an animation it will not be completed yet)
  2666. * @event
  2667. * @name open_node.jstree
  2668. * @param {Object} node the opened node
  2669. */
  2670. this.trigger('open_node', { "node" : obj });
  2671. if(!animation || !d.length) {
  2672. /**
  2673. * triggered when a node is opened and the animation is complete
  2674. * @event
  2675. * @name after_open.jstree
  2676. * @param {Object} node the opened node
  2677. */
  2678. this.trigger("after_open", { "node" : obj });
  2679. }
  2680. return true;
  2681. }
  2682. },
  2683. /**
  2684. * opens every parent of a node (node should be loaded)
  2685. * @name _open_to(obj)
  2686. * @param {mixed} obj the node to reveal
  2687. * @private
  2688. */
  2689. _open_to : function (obj) {
  2690. obj = this.get_node(obj);
  2691. if(!obj || obj.id === $.jstree.root) {
  2692. return false;
  2693. }
  2694. var i, j, p = obj.parents;
  2695. for(i = 0, j = p.length; i < j; i+=1) {
  2696. if(i !== $.jstree.root) {
  2697. this.open_node(p[i], false, 0);
  2698. }
  2699. }
  2700. return $('#' + obj.id.replace($.jstree.idregex,'\\$&'), this.element);
  2701. },
  2702. /**
  2703. * closes a node, hiding its children
  2704. * @name close_node(obj [, animation])
  2705. * @param {mixed} obj the node to close
  2706. * @param {Number} animation the animation duration in milliseconds when closing the node (overrides the `core.animation` setting). Use `false` for no animation.
  2707. * @trigger close_node.jstree, after_close.jstree
  2708. */
  2709. close_node : function (obj, animation) {
  2710. var t1, t2, t, d;
  2711. if($.isArray(obj)) {
  2712. obj = obj.slice();
  2713. for(t1 = 0, t2 = obj.length; t1 < t2; t1++) {
  2714. this.close_node(obj[t1], animation);
  2715. }
  2716. return true;
  2717. }
  2718. obj = this.get_node(obj);
  2719. if(!obj || obj.id === $.jstree.root) {
  2720. return false;
  2721. }
  2722. if(this.is_closed(obj)) {
  2723. return false;
  2724. }
  2725. animation = animation === undefined ? this.settings.core.animation : animation;
  2726. t = this;
  2727. d = this.get_node(obj, true);
  2728. obj.state.opened = false;
  2729. /**
  2730. * triggered when a node is closed (if there is an animation it will not be complete yet)
  2731. * @event
  2732. * @name close_node.jstree
  2733. * @param {Object} node the closed node
  2734. */
  2735. this.trigger('close_node',{ "node" : obj });
  2736. if(!d.length) {
  2737. /**
  2738. * triggered when a node is closed and the animation is complete
  2739. * @event
  2740. * @name after_close.jstree
  2741. * @param {Object} node the closed node
  2742. */
  2743. this.trigger("after_close", { "node" : obj });
  2744. }
  2745. else {
  2746. if(!animation) {
  2747. d[0].className = d[0].className.replace('jstree-open', 'jstree-closed');
  2748. d.attr("aria-expanded", false).children('.jstree-children').remove();
  2749. this.trigger("after_close", { "node" : obj });
  2750. }
  2751. else {
  2752. d
  2753. .children(".jstree-children").attr("style","display:block !important").end()
  2754. .removeClass("jstree-open").addClass("jstree-closed").attr("aria-expanded", false)
  2755. .children(".jstree-children").stop(true, true).slideUp(animation, function () {
  2756. this.style.display = "";
  2757. d.children('.jstree-children').remove();
  2758. if (t.element) {
  2759. t.trigger("after_close", { "node" : obj });
  2760. }
  2761. });
  2762. }
  2763. }
  2764. },
  2765. /**
  2766. * toggles a node - closing it if it is open, opening it if it is closed
  2767. * @name toggle_node(obj)
  2768. * @param {mixed} obj the node to toggle
  2769. */
  2770. toggle_node : function (obj) {
  2771. var t1, t2;
  2772. if($.isArray(obj)) {
  2773. obj = obj.slice();
  2774. for(t1 = 0, t2 = obj.length; t1 < t2; t1++) {
  2775. this.toggle_node(obj[t1]);
  2776. }
  2777. return true;
  2778. }
  2779. if(this.is_closed(obj)) {
  2780. return this.open_node(obj);
  2781. }
  2782. if(this.is_open(obj)) {
  2783. return this.close_node(obj);
  2784. }
  2785. },
  2786. /**
  2787. * opens all nodes within a node (or the tree), revealing their children. If the node is not loaded it will be loaded and opened once ready.
  2788. * @name open_all([obj, animation, original_obj])
  2789. * @param {mixed} obj the node to open recursively, omit to open all nodes in the tree
  2790. * @param {Number} animation the animation duration in milliseconds when opening the nodes, the default is no animation
  2791. * @param {jQuery} reference to the node that started the process (internal use)
  2792. * @trigger open_all.jstree
  2793. */
  2794. open_all : function (obj, animation, original_obj) {
  2795. if(!obj) { obj = $.jstree.root; }
  2796. obj = this.get_node(obj);
  2797. if(!obj) { return false; }
  2798. var dom = obj.id === $.jstree.root ? this.get_container_ul() : this.get_node(obj, true), i, j, _this;
  2799. if(!dom.length) {
  2800. for(i = 0, j = obj.children_d.length; i < j; i++) {
  2801. if(this.is_closed(this._model.data[obj.children_d[i]])) {
  2802. this._model.data[obj.children_d[i]].state.opened = true;
  2803. }
  2804. }
  2805. return this.trigger('open_all', { "node" : obj });
  2806. }
  2807. original_obj = original_obj || dom;
  2808. _this = this;
  2809. dom = this.is_closed(obj) ? dom.find('.jstree-closed').addBack() : dom.find('.jstree-closed');
  2810. dom.each(function () {
  2811. _this.open_node(
  2812. this,
  2813. function(node, status) { if(status && this.is_parent(node)) { this.open_all(node, animation, original_obj); } },
  2814. animation || 0
  2815. );
  2816. });
  2817. if(original_obj.find('.jstree-closed').length === 0) {
  2818. /**
  2819. * triggered when an `open_all` call completes
  2820. * @event
  2821. * @name open_all.jstree
  2822. * @param {Object} node the opened node
  2823. */
  2824. this.trigger('open_all', { "node" : this.get_node(original_obj) });
  2825. }
  2826. },
  2827. /**
  2828. * closes all nodes within a node (or the tree), revealing their children
  2829. * @name close_all([obj, animation])
  2830. * @param {mixed} obj the node to close recursively, omit to close all nodes in the tree
  2831. * @param {Number} animation the animation duration in milliseconds when closing the nodes, the default is no animation
  2832. * @trigger close_all.jstree
  2833. */
  2834. close_all : function (obj, animation) {
  2835. if(!obj) { obj = $.jstree.root; }
  2836. obj = this.get_node(obj);
  2837. if(!obj) { return false; }
  2838. var dom = obj.id === $.jstree.root ? this.get_container_ul() : this.get_node(obj, true),
  2839. _this = this, i, j;
  2840. if(dom.length) {
  2841. dom = this.is_open(obj) ? dom.find('.jstree-open').addBack() : dom.find('.jstree-open');
  2842. $(dom.get().reverse()).each(function () { _this.close_node(this, animation || 0); });
  2843. }
  2844. for(i = 0, j = obj.children_d.length; i < j; i++) {
  2845. this._model.data[obj.children_d[i]].state.opened = false;
  2846. }
  2847. /**
  2848. * triggered when an `close_all` call completes
  2849. * @event
  2850. * @name close_all.jstree
  2851. * @param {Object} node the closed node
  2852. */
  2853. this.trigger('close_all', { "node" : obj });
  2854. },
  2855. /**
  2856. * checks if a node is disabled (not selectable)
  2857. * @name is_disabled(obj)
  2858. * @param {mixed} obj
  2859. * @return {Boolean}
  2860. */
  2861. is_disabled : function (obj) {
  2862. obj = this.get_node(obj);
  2863. return obj && obj.state && obj.state.disabled;
  2864. },
  2865. /**
  2866. * enables a node - so that it can be selected
  2867. * @name enable_node(obj)
  2868. * @param {mixed} obj the node to enable
  2869. * @trigger enable_node.jstree
  2870. */
  2871. enable_node : function (obj) {
  2872. var t1, t2;
  2873. if($.isArray(obj)) {
  2874. obj = obj.slice();
  2875. for(t1 = 0, t2 = obj.length; t1 < t2; t1++) {
  2876. this.enable_node(obj[t1]);
  2877. }
  2878. return true;
  2879. }
  2880. obj = this.get_node(obj);
  2881. if(!obj || obj.id === $.jstree.root) {
  2882. return false;
  2883. }
  2884. obj.state.disabled = false;
  2885. this.get_node(obj,true).children('.jstree-anchor').removeClass('jstree-disabled').attr('aria-disabled', false);
  2886. /**
  2887. * triggered when an node is enabled
  2888. * @event
  2889. * @name enable_node.jstree
  2890. * @param {Object} node the enabled node
  2891. */
  2892. this.trigger('enable_node', { 'node' : obj });
  2893. },
  2894. /**
  2895. * disables a node - so that it can not be selected
  2896. * @name disable_node(obj)
  2897. * @param {mixed} obj the node to disable
  2898. * @trigger disable_node.jstree
  2899. */
  2900. disable_node : function (obj) {
  2901. var t1, t2;
  2902. if($.isArray(obj)) {
  2903. obj = obj.slice();
  2904. for(t1 = 0, t2 = obj.length; t1 < t2; t1++) {
  2905. this.disable_node(obj[t1]);
  2906. }
  2907. return true;
  2908. }
  2909. obj = this.get_node(obj);
  2910. if(!obj || obj.id === $.jstree.root) {
  2911. return false;
  2912. }
  2913. obj.state.disabled = true;
  2914. this.get_node(obj,true).children('.jstree-anchor').addClass('jstree-disabled').attr('aria-disabled', true);
  2915. /**
  2916. * triggered when an node is disabled
  2917. * @event
  2918. * @name disable_node.jstree
  2919. * @param {Object} node the disabled node
  2920. */
  2921. this.trigger('disable_node', { 'node' : obj });
  2922. },
  2923. /**
  2924. * determines if a node is hidden
  2925. * @name is_hidden(obj)
  2926. * @param {mixed} obj the node
  2927. */
  2928. is_hidden : function (obj) {
  2929. obj = this.get_node(obj);
  2930. return obj.state.hidden === true;
  2931. },
  2932. /**
  2933. * hides a node - it is still in the structure but will not be visible
  2934. * @name hide_node(obj)
  2935. * @param {mixed} obj the node to hide
  2936. * @param {Boolean} skip_redraw internal parameter controlling if redraw is called
  2937. * @trigger hide_node.jstree
  2938. */
  2939. hide_node : function (obj, skip_redraw) {
  2940. var t1, t2;
  2941. if($.isArray(obj)) {
  2942. obj = obj.slice();
  2943. for(t1 = 0, t2 = obj.length; t1 < t2; t1++) {
  2944. this.hide_node(obj[t1], true);
  2945. }
  2946. if (!skip_redraw) {
  2947. this.redraw();
  2948. }
  2949. return true;
  2950. }
  2951. obj = this.get_node(obj);
  2952. if(!obj || obj.id === $.jstree.root) {
  2953. return false;
  2954. }
  2955. if(!obj.state.hidden) {
  2956. obj.state.hidden = true;
  2957. this._node_changed(obj.parent);
  2958. if(!skip_redraw) {
  2959. this.redraw();
  2960. }
  2961. /**
  2962. * triggered when an node is hidden
  2963. * @event
  2964. * @name hide_node.jstree
  2965. * @param {Object} node the hidden node
  2966. */
  2967. this.trigger('hide_node', { 'node' : obj });
  2968. }
  2969. },
  2970. /**
  2971. * shows a node
  2972. * @name show_node(obj)
  2973. * @param {mixed} obj the node to show
  2974. * @param {Boolean} skip_redraw internal parameter controlling if redraw is called
  2975. * @trigger show_node.jstree
  2976. */
  2977. show_node : function (obj, skip_redraw) {
  2978. var t1, t2;
  2979. if($.isArray(obj)) {
  2980. obj = obj.slice();
  2981. for(t1 = 0, t2 = obj.length; t1 < t2; t1++) {
  2982. this.show_node(obj[t1], true);
  2983. }
  2984. if (!skip_redraw) {
  2985. this.redraw();
  2986. }
  2987. return true;
  2988. }
  2989. obj = this.get_node(obj);
  2990. if(!obj || obj.id === $.jstree.root) {
  2991. return false;
  2992. }
  2993. if(obj.state.hidden) {
  2994. obj.state.hidden = false;
  2995. this._node_changed(obj.parent);
  2996. if(!skip_redraw) {
  2997. this.redraw();
  2998. }
  2999. /**
  3000. * triggered when an node is shown
  3001. * @event
  3002. * @name show_node.jstree
  3003. * @param {Object} node the shown node
  3004. */
  3005. this.trigger('show_node', { 'node' : obj });
  3006. }
  3007. },
  3008. /**
  3009. * hides all nodes
  3010. * @name hide_all()
  3011. * @trigger hide_all.jstree
  3012. */
  3013. hide_all : function (skip_redraw) {
  3014. var i, m = this._model.data, ids = [];
  3015. for(i in m) {
  3016. if(m.hasOwnProperty(i) && i !== $.jstree.root && !m[i].state.hidden) {
  3017. m[i].state.hidden = true;
  3018. ids.push(i);
  3019. }
  3020. }
  3021. this._model.force_full_redraw = true;
  3022. if(!skip_redraw) {
  3023. this.redraw();
  3024. }
  3025. /**
  3026. * triggered when all nodes are hidden
  3027. * @event
  3028. * @name hide_all.jstree
  3029. * @param {Array} nodes the IDs of all hidden nodes
  3030. */
  3031. this.trigger('hide_all', { 'nodes' : ids });
  3032. return ids;
  3033. },
  3034. /**
  3035. * shows all nodes
  3036. * @name show_all()
  3037. * @trigger show_all.jstree
  3038. */
  3039. show_all : function (skip_redraw) {
  3040. var i, m = this._model.data, ids = [];
  3041. for(i in m) {
  3042. if(m.hasOwnProperty(i) && i !== $.jstree.root && m[i].state.hidden) {
  3043. m[i].state.hidden = false;
  3044. ids.push(i);
  3045. }
  3046. }
  3047. this._model.force_full_redraw = true;
  3048. if(!skip_redraw) {
  3049. this.redraw();
  3050. }
  3051. /**
  3052. * triggered when all nodes are shown
  3053. * @event
  3054. * @name show_all.jstree
  3055. * @param {Array} nodes the IDs of all shown nodes
  3056. */
  3057. this.trigger('show_all', { 'nodes' : ids });
  3058. return ids;
  3059. },
  3060. /**
  3061. * called when a node is selected by the user. Used internally.
  3062. * @private
  3063. * @name activate_node(obj, e)
  3064. * @param {mixed} obj the node
  3065. * @param {Object} e the related event
  3066. * @trigger activate_node.jstree, changed.jstree
  3067. */
  3068. activate_node : function (obj, e) {
  3069. if(this.is_disabled(obj)) {
  3070. return false;
  3071. }
  3072. if(!e || typeof e !== 'object') {
  3073. e = {};
  3074. }
  3075. // 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
  3076. 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;
  3077. if(this._data.core.last_clicked && !this._data.core.last_clicked.state.selected) { this._data.core.last_clicked = null; }
  3078. 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]); }
  3079. 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 ) )) {
  3080. if(!this.settings.core.multiple && (e.metaKey || e.ctrlKey || e.shiftKey) && this.is_selected(obj)) {
  3081. this.deselect_node(obj, false, e);
  3082. }
  3083. else {
  3084. this.deselect_all(true);
  3085. this.select_node(obj, false, false, e);
  3086. this._data.core.last_clicked = this.get_node(obj);
  3087. }
  3088. }
  3089. else {
  3090. if(e.shiftKey) {
  3091. var o = this.get_node(obj).id,
  3092. l = this._data.core.last_clicked.id,
  3093. p = this.get_node(this._data.core.last_clicked.parent).children,
  3094. c = false,
  3095. i, j;
  3096. for(i = 0, j = p.length; i < j; i += 1) {
  3097. // separate IFs work whem o and l are the same
  3098. if(p[i] === o) {
  3099. c = !c;
  3100. }
  3101. if(p[i] === l) {
  3102. c = !c;
  3103. }
  3104. if(!this.is_disabled(p[i]) && (c || p[i] === o || p[i] === l)) {
  3105. if (!this.is_hidden(p[i])) {
  3106. this.select_node(p[i], true, false, e);
  3107. }
  3108. }
  3109. else {
  3110. this.deselect_node(p[i], true, e);
  3111. }
  3112. }
  3113. this.trigger('changed', { 'action' : 'select_node', 'node' : this.get_node(obj), 'selected' : this._data.core.selected, 'event' : e });
  3114. }
  3115. else {
  3116. if(!this.is_selected(obj)) {
  3117. this.select_node(obj, false, false, e);
  3118. }
  3119. else {
  3120. this.deselect_node(obj, false, e);
  3121. }
  3122. }
  3123. }
  3124. /**
  3125. * triggered when an node is clicked or intercated with by the user
  3126. * @event
  3127. * @name activate_node.jstree
  3128. * @param {Object} node
  3129. * @param {Object} event the ooriginal event (if any) which triggered the call (may be an empty object)
  3130. */
  3131. this.trigger('activate_node', { 'node' : this.get_node(obj), 'event' : e });
  3132. },
  3133. /**
  3134. * applies the hover state on a node, called when a node is hovered by the user. Used internally.
  3135. * @private
  3136. * @name hover_node(obj)
  3137. * @param {mixed} obj
  3138. * @trigger hover_node.jstree
  3139. */
  3140. hover_node : function (obj) {
  3141. obj = this.get_node(obj, true);
  3142. if(!obj || !obj.length || obj.children('.jstree-hovered').length) {
  3143. return false;
  3144. }
  3145. var o = this.element.find('.jstree-hovered'), t = this.element;
  3146. if(o && o.length) { this.dehover_node(o); }
  3147. obj.children('.jstree-anchor').addClass('jstree-hovered');
  3148. /**
  3149. * triggered when an node is hovered
  3150. * @event
  3151. * @name hover_node.jstree
  3152. * @param {Object} node
  3153. */
  3154. this.trigger('hover_node', { 'node' : this.get_node(obj) });
  3155. setTimeout(function () { t.attr('aria-activedescendant', obj[0].id); }, 0);
  3156. },
  3157. /**
  3158. * removes the hover state from a nodecalled when a node is no longer hovered by the user. Used internally.
  3159. * @private
  3160. * @name dehover_node(obj)
  3161. * @param {mixed} obj
  3162. * @trigger dehover_node.jstree
  3163. */
  3164. dehover_node : function (obj) {
  3165. obj = this.get_node(obj, true);
  3166. if(!obj || !obj.length || !obj.children('.jstree-hovered').length) {
  3167. return false;
  3168. }
  3169. obj.children('.jstree-anchor').removeClass('jstree-hovered');
  3170. /**
  3171. * triggered when an node is no longer hovered
  3172. * @event
  3173. * @name dehover_node.jstree
  3174. * @param {Object} node
  3175. */
  3176. this.trigger('dehover_node', { 'node' : this.get_node(obj) });
  3177. },
  3178. /**
  3179. * select a node
  3180. * @name select_node(obj [, supress_event, prevent_open])
  3181. * @param {mixed} obj an array can be used to select multiple nodes
  3182. * @param {Boolean} supress_event if set to `true` the `changed.jstree` event won't be triggered
  3183. * @param {Boolean} prevent_open if set to `true` parents of the selected node won't be opened
  3184. * @trigger select_node.jstree, changed.jstree
  3185. */
  3186. select_node : function (obj, supress_event, prevent_open, e) {
  3187. var dom, t1, t2, th;
  3188. if($.isArray(obj)) {
  3189. obj = obj.slice();
  3190. for(t1 = 0, t2 = obj.length; t1 < t2; t1++) {
  3191. this.select_node(obj[t1], supress_event, prevent_open, e);
  3192. }
  3193. return true;
  3194. }
  3195. obj = this.get_node(obj);
  3196. if(!obj || obj.id === $.jstree.root) {
  3197. return false;
  3198. }
  3199. dom = this.get_node(obj, true);
  3200. if(!obj.state.selected) {
  3201. obj.state.selected = true;
  3202. this._data.core.selected.push(obj.id);
  3203. if(!prevent_open) {
  3204. dom = this._open_to(obj);
  3205. }
  3206. if(dom && dom.length) {
  3207. dom.attr('aria-selected', true).children('.jstree-anchor').addClass('jstree-clicked');
  3208. }
  3209. /**
  3210. * triggered when an node is selected
  3211. * @event
  3212. * @name select_node.jstree
  3213. * @param {Object} node
  3214. * @param {Array} selected the current selection
  3215. * @param {Object} event the event (if any) that triggered this select_node
  3216. */
  3217. this.trigger('select_node', { 'node' : obj, 'selected' : this._data.core.selected, 'event' : e });
  3218. if(!supress_event) {
  3219. /**
  3220. * triggered when selection changes
  3221. * @event
  3222. * @name changed.jstree
  3223. * @param {Object} node
  3224. * @param {Object} action the action that caused the selection to change
  3225. * @param {Array} selected the current selection
  3226. * @param {Object} event the event (if any) that triggered this changed event
  3227. */
  3228. this.trigger('changed', { 'action' : 'select_node', 'node' : obj, 'selected' : this._data.core.selected, 'event' : e });
  3229. }
  3230. }
  3231. },
  3232. /**
  3233. * deselect a node
  3234. * @name deselect_node(obj [, supress_event])
  3235. * @param {mixed} obj an array can be used to deselect multiple nodes
  3236. * @param {Boolean} supress_event if set to `true` the `changed.jstree` event won't be triggered
  3237. * @trigger deselect_node.jstree, changed.jstree
  3238. */
  3239. deselect_node : function (obj, supress_event, e) {
  3240. var t1, t2, dom;
  3241. if($.isArray(obj)) {
  3242. obj = obj.slice();
  3243. for(t1 = 0, t2 = obj.length; t1 < t2; t1++) {
  3244. this.deselect_node(obj[t1], supress_event, e);
  3245. }
  3246. return true;
  3247. }
  3248. obj = this.get_node(obj);
  3249. if(!obj || obj.id === $.jstree.root) {
  3250. return false;
  3251. }
  3252. dom = this.get_node(obj, true);
  3253. if(obj.state.selected) {
  3254. obj.state.selected = false;
  3255. this._data.core.selected = $.vakata.array_remove_item(this._data.core.selected, obj.id);
  3256. if(dom.length) {
  3257. dom.attr('aria-selected', false).children('.jstree-anchor').removeClass('jstree-clicked');
  3258. }
  3259. /**
  3260. * triggered when an node is deselected
  3261. * @event
  3262. * @name deselect_node.jstree
  3263. * @param {Object} node
  3264. * @param {Array} selected the current selection
  3265. * @param {Object} event the event (if any) that triggered this deselect_node
  3266. */
  3267. this.trigger('deselect_node', { 'node' : obj, 'selected' : this._data.core.selected, 'event' : e });
  3268. if(!supress_event) {
  3269. this.trigger('changed', { 'action' : 'deselect_node', 'node' : obj, 'selected' : this._data.core.selected, 'event' : e });
  3270. }
  3271. }
  3272. },
  3273. /**
  3274. * select all nodes in the tree
  3275. * @name select_all([supress_event])
  3276. * @param {Boolean} supress_event if set to `true` the `changed.jstree` event won't be triggered
  3277. * @trigger select_all.jstree, changed.jstree
  3278. */
  3279. select_all : function (supress_event) {
  3280. var tmp = this._data.core.selected.concat([]), i, j;
  3281. this._data.core.selected = this._model.data[$.jstree.root].children_d.concat();
  3282. for(i = 0, j = this._data.core.selected.length; i < j; i++) {
  3283. if(this._model.data[this._data.core.selected[i]]) {
  3284. this._model.data[this._data.core.selected[i]].state.selected = true;
  3285. }
  3286. }
  3287. this.redraw(true);
  3288. /**
  3289. * triggered when all nodes are selected
  3290. * @event
  3291. * @name select_all.jstree
  3292. * @param {Array} selected the current selection
  3293. */
  3294. this.trigger('select_all', { 'selected' : this._data.core.selected });
  3295. if(!supress_event) {
  3296. this.trigger('changed', { 'action' : 'select_all', 'selected' : this._data.core.selected, 'old_selection' : tmp });
  3297. }
  3298. },
  3299. /**
  3300. * deselect all selected nodes
  3301. * @name deselect_all([supress_event])
  3302. * @param {Boolean} supress_event if set to `true` the `changed.jstree` event won't be triggered
  3303. * @trigger deselect_all.jstree, changed.jstree
  3304. */
  3305. deselect_all : function (supress_event) {
  3306. var tmp = this._data.core.selected.concat([]), i, j;
  3307. for(i = 0, j = this._data.core.selected.length; i < j; i++) {
  3308. if(this._model.data[this._data.core.selected[i]]) {
  3309. this._model.data[this._data.core.selected[i]].state.selected = false;
  3310. }
  3311. }
  3312. this._data.core.selected = [];
  3313. this.element.find('.jstree-clicked').removeClass('jstree-clicked').parent().attr('aria-selected', false);
  3314. /**
  3315. * triggered when all nodes are deselected
  3316. * @event
  3317. * @name deselect_all.jstree
  3318. * @param {Object} node the previous selection
  3319. * @param {Array} selected the current selection
  3320. */
  3321. this.trigger('deselect_all', { 'selected' : this._data.core.selected, 'node' : tmp });
  3322. if(!supress_event) {
  3323. this.trigger('changed', { 'action' : 'deselect_all', 'selected' : this._data.core.selected, 'old_selection' : tmp });
  3324. }
  3325. },
  3326. /**
  3327. * checks if a node is selected
  3328. * @name is_selected(obj)
  3329. * @param {mixed} obj
  3330. * @return {Boolean}
  3331. */
  3332. is_selected : function (obj) {
  3333. obj = this.get_node(obj);
  3334. if(!obj || obj.id === $.jstree.root) {
  3335. return false;
  3336. }
  3337. return obj.state.selected;
  3338. },
  3339. /**
  3340. * get an array of all selected nodes
  3341. * @name get_selected([full])
  3342. * @param {mixed} full if set to `true` the returned array will consist of the full node objects, otherwise - only IDs will be returned
  3343. * @return {Array}
  3344. */
  3345. get_selected : function (full) {
  3346. return full ? $.map(this._data.core.selected, $.proxy(function (i) { return this.get_node(i); }, this)) : this._data.core.selected.slice();
  3347. },
  3348. /**
  3349. * get an array of all top level selected nodes (ignoring children of selected nodes)
  3350. * @name get_top_selected([full])
  3351. * @param {mixed} full if set to `true` the returned array will consist of the full node objects, otherwise - only IDs will be returned
  3352. * @return {Array}
  3353. */
  3354. get_top_selected : function (full) {
  3355. var tmp = this.get_selected(true),
  3356. obj = {}, i, j, k, l;
  3357. for(i = 0, j = tmp.length; i < j; i++) {
  3358. obj[tmp[i].id] = tmp[i];
  3359. }
  3360. for(i = 0, j = tmp.length; i < j; i++) {
  3361. for(k = 0, l = tmp[i].children_d.length; k < l; k++) {
  3362. if(obj[tmp[i].children_d[k]]) {
  3363. delete obj[tmp[i].children_d[k]];
  3364. }
  3365. }
  3366. }
  3367. tmp = [];
  3368. for(i in obj) {
  3369. if(obj.hasOwnProperty(i)) {
  3370. tmp.push(i);
  3371. }
  3372. }
  3373. return full ? $.map(tmp, $.proxy(function (i) { return this.get_node(i); }, this)) : tmp;
  3374. },
  3375. /**
  3376. * get an array of all bottom level selected nodes (ignoring selected parents)
  3377. * @name get_bottom_selected([full])
  3378. * @param {mixed} full if set to `true` the returned array will consist of the full node objects, otherwise - only IDs will be returned
  3379. * @return {Array}
  3380. */
  3381. get_bottom_selected : function (full) {
  3382. var tmp = this.get_selected(true),
  3383. obj = [], i, j;
  3384. for(i = 0, j = tmp.length; i < j; i++) {
  3385. if(!tmp[i].children.length) {
  3386. obj.push(tmp[i].id);
  3387. }
  3388. }
  3389. return full ? $.map(obj, $.proxy(function (i) { return this.get_node(i); }, this)) : obj;
  3390. },
  3391. /**
  3392. * gets the current state of the tree so that it can be restored later with `set_state(state)`. Used internally.
  3393. * @name get_state()
  3394. * @private
  3395. * @return {Object}
  3396. */
  3397. get_state : function () {
  3398. var state = {
  3399. 'core' : {
  3400. 'open' : [],
  3401. 'loaded' : [],
  3402. 'scroll' : {
  3403. 'left' : this.element.scrollLeft(),
  3404. 'top' : this.element.scrollTop()
  3405. },
  3406. /*!
  3407. 'themes' : {
  3408. 'name' : this.get_theme(),
  3409. 'icons' : this._data.core.themes.icons,
  3410. 'dots' : this._data.core.themes.dots
  3411. },
  3412. */
  3413. 'selected' : []
  3414. }
  3415. }, i;
  3416. for(i in this._model.data) {
  3417. if(this._model.data.hasOwnProperty(i)) {
  3418. if(i !== $.jstree.root) {
  3419. if(this._model.data[i].state.loaded && this.settings.core.loaded_state) {
  3420. state.core.loaded.push(i);
  3421. }
  3422. if(this._model.data[i].state.opened) {
  3423. state.core.open.push(i);
  3424. }
  3425. if(this._model.data[i].state.selected) {
  3426. state.core.selected.push(i);
  3427. }
  3428. }
  3429. }
  3430. }
  3431. return state;
  3432. },
  3433. /**
  3434. * sets the state of the tree. Used internally.
  3435. * @name set_state(state [, callback])
  3436. * @private
  3437. * @param {Object} state the state to restore. Keep in mind this object is passed by reference and jstree will modify it.
  3438. * @param {Function} callback an optional function to execute once the state is restored.
  3439. * @trigger set_state.jstree
  3440. */
  3441. set_state : function (state, callback) {
  3442. if(state) {
  3443. if(state.core && state.core.selected && state.core.initial_selection === undefined) {
  3444. state.core.initial_selection = this._data.core.selected.concat([]).sort().join(',');
  3445. }
  3446. if(state.core) {
  3447. var res, n, t, _this, i;
  3448. if(state.core.loaded) {
  3449. if(!this.settings.core.loaded_state || !$.isArray(state.core.loaded) || !state.core.loaded.length) {
  3450. delete state.core.loaded;
  3451. this.set_state(state, callback);
  3452. }
  3453. else {
  3454. this._load_nodes(state.core.loaded, function (nodes) {
  3455. delete state.core.loaded;
  3456. this.set_state(state, callback);
  3457. });
  3458. }
  3459. return false;
  3460. }
  3461. if(state.core.open) {
  3462. if(!$.isArray(state.core.open) || !state.core.open.length) {
  3463. delete state.core.open;
  3464. this.set_state(state, callback);
  3465. }
  3466. else {
  3467. this._load_nodes(state.core.open, function (nodes) {
  3468. this.open_node(nodes, false, 0);
  3469. delete state.core.open;
  3470. this.set_state(state, callback);
  3471. });
  3472. }
  3473. return false;
  3474. }
  3475. if(state.core.scroll) {
  3476. if(state.core.scroll && state.core.scroll.left !== undefined) {
  3477. this.element.scrollLeft(state.core.scroll.left);
  3478. }
  3479. if(state.core.scroll && state.core.scroll.top !== undefined) {
  3480. this.element.scrollTop(state.core.scroll.top);
  3481. }
  3482. delete state.core.scroll;
  3483. this.set_state(state, callback);
  3484. return false;
  3485. }
  3486. if(state.core.selected) {
  3487. _this = this;
  3488. if (state.core.initial_selection === undefined ||
  3489. state.core.initial_selection === this._data.core.selected.concat([]).sort().join(',')
  3490. ) {
  3491. this.deselect_all();
  3492. $.each(state.core.selected, function (i, v) {
  3493. _this.select_node(v, false, true);
  3494. });
  3495. }
  3496. delete state.core.initial_selection;
  3497. delete state.core.selected;
  3498. this.set_state(state, callback);
  3499. return false;
  3500. }
  3501. for(i in state) {
  3502. if(state.hasOwnProperty(i) && i !== "core" && $.inArray(i, this.settings.plugins) === -1) {
  3503. delete state[i];
  3504. }
  3505. }
  3506. if($.isEmptyObject(state.core)) {
  3507. delete state.core;
  3508. this.set_state(state, callback);
  3509. return false;
  3510. }
  3511. }
  3512. if($.isEmptyObject(state)) {
  3513. state = null;
  3514. if(callback) { callback.call(this); }
  3515. /**
  3516. * triggered when a `set_state` call completes
  3517. * @event
  3518. * @name set_state.jstree
  3519. */
  3520. this.trigger('set_state');
  3521. return false;
  3522. }
  3523. return true;
  3524. }
  3525. return false;
  3526. },
  3527. /**
  3528. * refreshes the tree - all nodes are reloaded with calls to `load_node`.
  3529. * @name refresh()
  3530. * @param {Boolean} skip_loading an option to skip showing the loading indicator
  3531. * @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
  3532. * @trigger refresh.jstree
  3533. */
  3534. refresh : function (skip_loading, forget_state) {
  3535. this._data.core.state = forget_state === true ? {} : this.get_state();
  3536. if(forget_state && $.isFunction(forget_state)) { this._data.core.state = forget_state.call(this, this._data.core.state); }
  3537. this._cnt = 0;
  3538. this._model.data = {};
  3539. this._model.data[$.jstree.root] = {
  3540. id : $.jstree.root,
  3541. parent : null,
  3542. parents : [],
  3543. children : [],
  3544. children_d : [],
  3545. state : { loaded : false }
  3546. };
  3547. this._data.core.selected = [];
  3548. this._data.core.last_clicked = null;
  3549. this._data.core.focused = null;
  3550. var c = this.get_container_ul()[0].className;
  3551. if(!skip_loading) {
  3552. 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>");
  3553. this.element.attr('aria-activedescendant','j'+this._id+'_loading');
  3554. }
  3555. this.load_node($.jstree.root, function (o, s) {
  3556. if(s) {
  3557. this.get_container_ul()[0].className = c;
  3558. if(this._firstChild(this.get_container_ul()[0])) {
  3559. this.element.attr('aria-activedescendant',this._firstChild(this.get_container_ul()[0]).id);
  3560. }
  3561. this.set_state($.extend(true, {}, this._data.core.state), function () {
  3562. /**
  3563. * triggered when a `refresh` call completes
  3564. * @event
  3565. * @name refresh.jstree
  3566. */
  3567. this.trigger('refresh');
  3568. });
  3569. }
  3570. this._data.core.state = null;
  3571. });
  3572. },
  3573. /**
  3574. * refreshes a node in the tree (reload its children) all opened nodes inside that node are reloaded with calls to `load_node`.
  3575. * @name refresh_node(obj)
  3576. * @param {mixed} obj the node
  3577. * @trigger refresh_node.jstree
  3578. */
  3579. refresh_node : function (obj) {
  3580. obj = this.get_node(obj);
  3581. if(!obj || obj.id === $.jstree.root) { return false; }
  3582. var opened = [], to_load = [], s = this._data.core.selected.concat([]);
  3583. to_load.push(obj.id);
  3584. if(obj.state.opened === true) { opened.push(obj.id); }
  3585. this.get_node(obj, true).find('.jstree-open').each(function() { to_load.push(this.id); opened.push(this.id); });
  3586. this._load_nodes(to_load, $.proxy(function (nodes) {
  3587. this.open_node(opened, false, 0);
  3588. this.select_node(s);
  3589. /**
  3590. * triggered when a node is refreshed
  3591. * @event
  3592. * @name refresh_node.jstree
  3593. * @param {Object} node - the refreshed node
  3594. * @param {Array} nodes - an array of the IDs of the nodes that were reloaded
  3595. */
  3596. this.trigger('refresh_node', { 'node' : obj, 'nodes' : nodes });
  3597. }, this), false, true);
  3598. },
  3599. /**
  3600. * set (change) the ID of a node
  3601. * @name set_id(obj, id)
  3602. * @param {mixed} obj the node
  3603. * @param {String} id the new ID
  3604. * @return {Boolean}
  3605. * @trigger set_id.jstree
  3606. */
  3607. set_id : function (obj, id) {
  3608. obj = this.get_node(obj);
  3609. if(!obj || obj.id === $.jstree.root) { return false; }
  3610. var i, j, m = this._model.data, old = obj.id;
  3611. id = id.toString();
  3612. // update parents (replace current ID with new one in children and children_d)
  3613. m[obj.parent].children[$.inArray(obj.id, m[obj.parent].children)] = id;
  3614. for(i = 0, j = obj.parents.length; i < j; i++) {
  3615. m[obj.parents[i]].children_d[$.inArray(obj.id, m[obj.parents[i]].children_d)] = id;
  3616. }
  3617. // update children (replace current ID with new one in parent and parents)
  3618. for(i = 0, j = obj.children.length; i < j; i++) {
  3619. m[obj.children[i]].parent = id;
  3620. }
  3621. for(i = 0, j = obj.children_d.length; i < j; i++) {
  3622. m[obj.children_d[i]].parents[$.inArray(obj.id, m[obj.children_d[i]].parents)] = id;
  3623. }
  3624. i = $.inArray(obj.id, this._data.core.selected);
  3625. if(i !== -1) { this._data.core.selected[i] = id; }
  3626. // update model and obj itself (obj.id, this._model.data[KEY])
  3627. i = this.get_node(obj.id, true);
  3628. if(i) {
  3629. i.attr('id', id); //.children('.jstree-anchor').attr('id', id + '_anchor').end().attr('aria-labelledby', id + '_anchor');
  3630. if(this.element.attr('aria-activedescendant') === obj.id) {
  3631. this.element.attr('aria-activedescendant', id);
  3632. }
  3633. }
  3634. delete m[obj.id];
  3635. obj.id = id;
  3636. obj.li_attr.id = id;
  3637. m[id] = obj;
  3638. /**
  3639. * triggered when a node id value is changed
  3640. * @event
  3641. * @name set_id.jstree
  3642. * @param {Object} node
  3643. * @param {String} old the old id
  3644. */
  3645. this.trigger('set_id',{ "node" : obj, "new" : obj.id, "old" : old });
  3646. return true;
  3647. },
  3648. /**
  3649. * get the text value of a node
  3650. * @name get_text(obj)
  3651. * @param {mixed} obj the node
  3652. * @return {String}
  3653. */
  3654. get_text : function (obj) {
  3655. obj = this.get_node(obj);
  3656. return (!obj || obj.id === $.jstree.root) ? false : obj.text;
  3657. },
  3658. /**
  3659. * set the text value of a node. Used internally, please use `rename_node(obj, val)`.
  3660. * @private
  3661. * @name set_text(obj, val)
  3662. * @param {mixed} obj the node, you can pass an array to set the text on multiple nodes
  3663. * @param {String} val the new text value
  3664. * @return {Boolean}
  3665. * @trigger set_text.jstree
  3666. */
  3667. set_text : function (obj, val) {
  3668. var t1, t2;
  3669. if($.isArray(obj)) {
  3670. obj = obj.slice();
  3671. for(t1 = 0, t2 = obj.length; t1 < t2; t1++) {
  3672. this.set_text(obj[t1], val);
  3673. }
  3674. return true;
  3675. }
  3676. obj = this.get_node(obj);
  3677. if(!obj || obj.id === $.jstree.root) { return false; }
  3678. obj.text = val;
  3679. if(this.get_node(obj, true).length) {
  3680. this.redraw_node(obj.id);
  3681. }
  3682. /**
  3683. * triggered when a node text value is changed
  3684. * @event
  3685. * @name set_text.jstree
  3686. * @param {Object} obj
  3687. * @param {String} text the new value
  3688. */
  3689. this.trigger('set_text',{ "obj" : obj, "text" : val });
  3690. return true;
  3691. },
  3692. /**
  3693. * gets a JSON representation of a node (or the whole tree)
  3694. * @name get_json([obj, options])
  3695. * @param {mixed} obj
  3696. * @param {Object} options
  3697. * @param {Boolean} options.no_state do not return state information
  3698. * @param {Boolean} options.no_id do not return ID
  3699. * @param {Boolean} options.no_children do not include children
  3700. * @param {Boolean} options.no_data do not include node data
  3701. * @param {Boolean} options.no_li_attr do not include LI attributes
  3702. * @param {Boolean} options.no_a_attr do not include A attributes
  3703. * @param {Boolean} options.flat return flat JSON instead of nested
  3704. * @return {Object}
  3705. */
  3706. get_json : function (obj, options, flat) {
  3707. obj = this.get_node(obj || $.jstree.root);
  3708. if(!obj) { return false; }
  3709. if(options && options.flat && !flat) { flat = []; }
  3710. var tmp = {
  3711. 'id' : obj.id,
  3712. 'text' : obj.text,
  3713. 'icon' : this.get_icon(obj),
  3714. 'li_attr' : $.extend(true, {}, obj.li_attr),
  3715. 'a_attr' : $.extend(true, {}, obj.a_attr),
  3716. 'state' : {},
  3717. 'data' : options && options.no_data ? false : $.extend(true, $.isArray(obj.data)?[]:{}, obj.data)
  3718. //( this.get_node(obj, true).length ? this.get_node(obj, true).data() : obj.data ),
  3719. }, i, j;
  3720. if(options && options.flat) {
  3721. tmp.parent = obj.parent;
  3722. }
  3723. else {
  3724. tmp.children = [];
  3725. }
  3726. if(!options || !options.no_state) {
  3727. for(i in obj.state) {
  3728. if(obj.state.hasOwnProperty(i)) {
  3729. tmp.state[i] = obj.state[i];
  3730. }
  3731. }
  3732. } else {
  3733. delete tmp.state;
  3734. }
  3735. if(options && options.no_li_attr) {
  3736. delete tmp.li_attr;
  3737. }
  3738. if(options && options.no_a_attr) {
  3739. delete tmp.a_attr;
  3740. }
  3741. if(options && options.no_id) {
  3742. delete tmp.id;
  3743. if(tmp.li_attr && tmp.li_attr.id) {
  3744. delete tmp.li_attr.id;
  3745. }
  3746. if(tmp.a_attr && tmp.a_attr.id) {
  3747. delete tmp.a_attr.id;
  3748. }
  3749. }
  3750. if(options && options.flat && obj.id !== $.jstree.root) {
  3751. flat.push(tmp);
  3752. }
  3753. if(!options || !options.no_children) {
  3754. for(i = 0, j = obj.children.length; i < j; i++) {
  3755. if(options && options.flat) {
  3756. this.get_json(obj.children[i], options, flat);
  3757. }
  3758. else {
  3759. tmp.children.push(this.get_json(obj.children[i], options));
  3760. }
  3761. }
  3762. }
  3763. return options && options.flat ? flat : (obj.id === $.jstree.root ? tmp.children : tmp);
  3764. },
  3765. /**
  3766. * create a new node (do not confuse with load_node)
  3767. * @name create_node([par, node, pos, callback, is_loaded])
  3768. * @param {mixed} par the parent node (to create a root node use either "#" (string) or `null`)
  3769. * @param {mixed} node the data for the new node (a valid JSON object, or a simple string with the name)
  3770. * @param {mixed} pos the index at which to insert the node, "first" and "last" are also supported, default is "last"
  3771. * @param {Function} callback a function to be called once the node is created
  3772. * @param {Boolean} is_loaded internal argument indicating if the parent node was succesfully loaded
  3773. * @return {String} the ID of the newly create node
  3774. * @trigger model.jstree, create_node.jstree
  3775. */
  3776. create_node : function (par, node, pos, callback, is_loaded) {
  3777. if(par === null) { par = $.jstree.root; }
  3778. par = this.get_node(par);
  3779. if(!par) { return false; }
  3780. pos = pos === undefined ? "last" : pos;
  3781. if(!pos.toString().match(/^(before|after)$/) && !is_loaded && !this.is_loaded(par)) {
  3782. return this.load_node(par, function () { this.create_node(par, node, pos, callback, true); });
  3783. }
  3784. if(!node) { node = { "text" : this.get_string('New node') }; }
  3785. if(typeof node === "string") {
  3786. node = { "text" : node };
  3787. } else {
  3788. node = $.extend(true, {}, node);
  3789. }
  3790. if(node.text === undefined) { node.text = this.get_string('New node'); }
  3791. var tmp, dpc, i, j;
  3792. if(par.id === $.jstree.root) {
  3793. if(pos === "before") { pos = "first"; }
  3794. if(pos === "after") { pos = "last"; }
  3795. }
  3796. switch(pos) {
  3797. case "before":
  3798. tmp = this.get_node(par.parent);
  3799. pos = $.inArray(par.id, tmp.children);
  3800. par = tmp;
  3801. break;
  3802. case "after" :
  3803. tmp = this.get_node(par.parent);
  3804. pos = $.inArray(par.id, tmp.children) + 1;
  3805. par = tmp;
  3806. break;
  3807. case "inside":
  3808. case "first":
  3809. pos = 0;
  3810. break;
  3811. case "last":
  3812. pos = par.children.length;
  3813. break;
  3814. default:
  3815. if(!pos) { pos = 0; }
  3816. break;
  3817. }
  3818. if(pos > par.children.length) { pos = par.children.length; }
  3819. if(!node.id) { node.id = true; }
  3820. if(!this.check("create_node", node, par, pos)) {
  3821. this.settings.core.error.call(this, this._data.core.last_error);
  3822. return false;
  3823. }
  3824. if(node.id === true) { delete node.id; }
  3825. node = this._parse_model_from_json(node, par.id, par.parents.concat());
  3826. if(!node) { return false; }
  3827. tmp = this.get_node(node);
  3828. dpc = [];
  3829. dpc.push(node);
  3830. dpc = dpc.concat(tmp.children_d);
  3831. this.trigger('model', { "nodes" : dpc, "parent" : par.id });
  3832. par.children_d = par.children_d.concat(dpc);
  3833. for(i = 0, j = par.parents.length; i < j; i++) {
  3834. this._model.data[par.parents[i]].children_d = this._model.data[par.parents[i]].children_d.concat(dpc);
  3835. }
  3836. node = tmp;
  3837. tmp = [];
  3838. for(i = 0, j = par.children.length; i < j; i++) {
  3839. tmp[i >= pos ? i+1 : i] = par.children[i];
  3840. }
  3841. tmp[pos] = node.id;
  3842. par.children = tmp;
  3843. this.redraw_node(par, true);
  3844. /**
  3845. * triggered when a node is created
  3846. * @event
  3847. * @name create_node.jstree
  3848. * @param {Object} node
  3849. * @param {String} parent the parent's ID
  3850. * @param {Number} position the position of the new node among the parent's children
  3851. */
  3852. this.trigger('create_node', { "node" : this.get_node(node), "parent" : par.id, "position" : pos });
  3853. if(callback) { callback.call(this, this.get_node(node)); }
  3854. return node.id;
  3855. },
  3856. /**
  3857. * set the text value of a node
  3858. * @name rename_node(obj, val)
  3859. * @param {mixed} obj the node, you can pass an array to rename multiple nodes to the same name
  3860. * @param {String} val the new text value
  3861. * @return {Boolean}
  3862. * @trigger rename_node.jstree
  3863. */
  3864. rename_node : function (obj, val) {
  3865. var t1, t2, old;
  3866. if($.isArray(obj)) {
  3867. obj = obj.slice();
  3868. for(t1 = 0, t2 = obj.length; t1 < t2; t1++) {
  3869. this.rename_node(obj[t1], val);
  3870. }
  3871. return true;
  3872. }
  3873. obj = this.get_node(obj);
  3874. if(!obj || obj.id === $.jstree.root) { return false; }
  3875. old = obj.text;
  3876. if(!this.check("rename_node", obj, this.get_parent(obj), val)) {
  3877. this.settings.core.error.call(this, this._data.core.last_error);
  3878. return false;
  3879. }
  3880. this.set_text(obj, val); // .apply(this, Array.prototype.slice.call(arguments))
  3881. /**
  3882. * triggered when a node is renamed
  3883. * @event
  3884. * @name rename_node.jstree
  3885. * @param {Object} node
  3886. * @param {String} text the new value
  3887. * @param {String} old the old value
  3888. */
  3889. this.trigger('rename_node', { "node" : obj, "text" : val, "old" : old });
  3890. return true;
  3891. },
  3892. /**
  3893. * remove a node
  3894. * @name delete_node(obj)
  3895. * @param {mixed} obj the node, you can pass an array to delete multiple nodes
  3896. * @return {Boolean}
  3897. * @trigger delete_node.jstree, changed.jstree
  3898. */
  3899. delete_node : function (obj) {
  3900. var t1, t2, par, pos, tmp, i, j, k, l, c, top, lft;
  3901. if($.isArray(obj)) {
  3902. obj = obj.slice();
  3903. for(t1 = 0, t2 = obj.length; t1 < t2; t1++) {
  3904. this.delete_node(obj[t1]);
  3905. }
  3906. return true;
  3907. }
  3908. obj = this.get_node(obj);
  3909. if(!obj || obj.id === $.jstree.root) { return false; }
  3910. par = this.get_node(obj.parent);
  3911. pos = $.inArray(obj.id, par.children);
  3912. c = false;
  3913. if(!this.check("delete_node", obj, par, pos)) {
  3914. this.settings.core.error.call(this, this._data.core.last_error);
  3915. return false;
  3916. }
  3917. if(pos !== -1) {
  3918. par.children = $.vakata.array_remove(par.children, pos);
  3919. }
  3920. tmp = obj.children_d.concat([]);
  3921. tmp.push(obj.id);
  3922. for(i = 0, j = obj.parents.length; i < j; i++) {
  3923. this._model.data[obj.parents[i]].children_d = $.vakata.array_filter(this._model.data[obj.parents[i]].children_d, function (v) {
  3924. return $.inArray(v, tmp) === -1;
  3925. });
  3926. }
  3927. for(k = 0, l = tmp.length; k < l; k++) {
  3928. if(this._model.data[tmp[k]].state.selected) {
  3929. c = true;
  3930. break;
  3931. }
  3932. }
  3933. if (c) {
  3934. this._data.core.selected = $.vakata.array_filter(this._data.core.selected, function (v) {
  3935. return $.inArray(v, tmp) === -1;
  3936. });
  3937. }
  3938. /**
  3939. * triggered when a node is deleted
  3940. * @event
  3941. * @name delete_node.jstree
  3942. * @param {Object} node
  3943. * @param {String} parent the parent's ID
  3944. */
  3945. this.trigger('delete_node', { "node" : obj, "parent" : par.id });
  3946. if(c) {
  3947. this.trigger('changed', { 'action' : 'delete_node', 'node' : obj, 'selected' : this._data.core.selected, 'parent' : par.id });
  3948. }
  3949. for(k = 0, l = tmp.length; k < l; k++) {
  3950. delete this._model.data[tmp[k]];
  3951. }
  3952. if($.inArray(this._data.core.focused, tmp) !== -1) {
  3953. this._data.core.focused = null;
  3954. top = this.element[0].scrollTop;
  3955. lft = this.element[0].scrollLeft;
  3956. if(par.id === $.jstree.root) {
  3957. if (this._model.data[$.jstree.root].children[0]) {
  3958. this.get_node(this._model.data[$.jstree.root].children[0], true).children('.jstree-anchor').focus();
  3959. }
  3960. }
  3961. else {
  3962. this.get_node(par, true).children('.jstree-anchor').focus();
  3963. }
  3964. this.element[0].scrollTop = top;
  3965. this.element[0].scrollLeft = lft;
  3966. }
  3967. this.redraw_node(par, true);
  3968. return true;
  3969. },
  3970. /**
  3971. * check if an operation is premitted on the tree. Used internally.
  3972. * @private
  3973. * @name check(chk, obj, par, pos)
  3974. * @param {String} chk the operation to check, can be "create_node", "rename_node", "delete_node", "copy_node" or "move_node"
  3975. * @param {mixed} obj the node
  3976. * @param {mixed} par the parent
  3977. * @param {mixed} pos the position to insert at, or if "rename_node" - the new name
  3978. * @param {mixed} more some various additional information, for example if a "move_node" operations is triggered by DND this will be the hovered node
  3979. * @return {Boolean}
  3980. */
  3981. check : function (chk, obj, par, pos, more) {
  3982. obj = obj && obj.id ? obj : this.get_node(obj);
  3983. par = par && par.id ? par : this.get_node(par);
  3984. var tmp = chk.match(/^move_node|copy_node|create_node$/i) ? par : obj,
  3985. chc = this.settings.core.check_callback;
  3986. if(chk === "move_node" || chk === "copy_node") {
  3987. if((!more || !more.is_multi) && (chk === "move_node" && $.inArray(obj.id, par.children) === pos)) {
  3988. this._data.core.last_error = { 'error' : 'check', 'plugin' : 'core', 'id' : 'core_08', 'reason' : 'Moving node to its current position', 'data' : JSON.stringify({ 'chk' : chk, 'pos' : pos, 'obj' : obj && obj.id ? obj.id : false, 'par' : par && par.id ? par.id : false }) };
  3989. return false;
  3990. }
  3991. 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)) {
  3992. 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 }) };
  3993. return false;
  3994. }
  3995. }
  3996. if(tmp && tmp.data) { tmp = tmp.data; }
  3997. if(tmp && tmp.functions && (tmp.functions[chk] === false || tmp.functions[chk] === true)) {
  3998. if(tmp.functions[chk] === false) {
  3999. 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 }) };
  4000. }
  4001. return tmp.functions[chk];
  4002. }
  4003. if(chc === false || ($.isFunction(chc) && chc.call(this, chk, obj, par, pos, more) === false) || (chc && chc[chk] === false)) {
  4004. 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 }) };
  4005. return false;
  4006. }
  4007. return true;
  4008. },
  4009. /**
  4010. * get the last error
  4011. * @name last_error()
  4012. * @return {Object}
  4013. */
  4014. last_error : function () {
  4015. return this._data.core.last_error;
  4016. },
  4017. /**
  4018. * move a node to a new parent
  4019. * @name move_node(obj, par [, pos, callback, is_loaded])
  4020. * @param {mixed} obj the node to move, pass an array to move multiple nodes
  4021. * @param {mixed} par the new parent
  4022. * @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`
  4023. * @param {function} callback a function to call once the move is completed, receives 3 arguments - the node, the new parent and the position
  4024. * @param {Boolean} is_loaded internal parameter indicating if the parent node has been loaded
  4025. * @param {Boolean} skip_redraw internal parameter indicating if the tree should be redrawn
  4026. * @param {Boolean} instance internal parameter indicating if the node comes from another instance
  4027. * @trigger move_node.jstree
  4028. */
  4029. move_node : function (obj, par, pos, callback, is_loaded, skip_redraw, origin) {
  4030. var t1, t2, old_par, old_pos, new_par, old_ins, is_multi, dpc, tmp, i, j, k, l, p;
  4031. par = this.get_node(par);
  4032. pos = pos === undefined ? 0 : pos;
  4033. if(!par) { return false; }
  4034. if(!pos.toString().match(/^(before|after)$/) && !is_loaded && !this.is_loaded(par)) {
  4035. return this.load_node(par, function () { this.move_node(obj, par, pos, callback, true, false, origin); });
  4036. }
  4037. if($.isArray(obj)) {
  4038. if(obj.length === 1) {
  4039. obj = obj[0];
  4040. }
  4041. else {
  4042. //obj = obj.slice();
  4043. for(t1 = 0, t2 = obj.length; t1 < t2; t1++) {
  4044. if((tmp = this.move_node(obj[t1], par, pos, callback, is_loaded, false, origin))) {
  4045. par = tmp;
  4046. pos = "after";
  4047. }
  4048. }
  4049. this.redraw();
  4050. return true;
  4051. }
  4052. }
  4053. obj = obj && obj.id ? obj : this.get_node(obj);
  4054. if(!obj || obj.id === $.jstree.root) { return false; }
  4055. old_par = (obj.parent || $.jstree.root).toString();
  4056. new_par = (!pos.toString().match(/^(before|after)$/) || par.id === $.jstree.root) ? par : this.get_node(par.parent);
  4057. old_ins = origin ? origin : (this._model.data[obj.id] ? this : $.jstree.reference(obj.id));
  4058. is_multi = !old_ins || !old_ins._id || (this._id !== old_ins._id);
  4059. 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;
  4060. if(old_ins && old_ins._id) {
  4061. obj = old_ins._model.data[obj.id];
  4062. }
  4063. if(is_multi) {
  4064. if((tmp = this.copy_node(obj, par, pos, callback, is_loaded, false, origin))) {
  4065. if(old_ins) { old_ins.delete_node(obj); }
  4066. return tmp;
  4067. }
  4068. return false;
  4069. }
  4070. //var m = this._model.data;
  4071. if(par.id === $.jstree.root) {
  4072. if(pos === "before") { pos = "first"; }
  4073. if(pos === "after") { pos = "last"; }
  4074. }
  4075. switch(pos) {
  4076. case "before":
  4077. pos = $.inArray(par.id, new_par.children);
  4078. break;
  4079. case "after" :
  4080. pos = $.inArray(par.id, new_par.children) + 1;
  4081. break;
  4082. case "inside":
  4083. case "first":
  4084. pos = 0;
  4085. break;
  4086. case "last":
  4087. pos = new_par.children.length;
  4088. break;
  4089. default:
  4090. if(!pos) { pos = 0; }
  4091. break;
  4092. }
  4093. if(pos > new_par.children.length) { pos = new_par.children.length; }
  4094. 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) })) {
  4095. this.settings.core.error.call(this, this._data.core.last_error);
  4096. return false;
  4097. }
  4098. if(obj.parent === new_par.id) {
  4099. dpc = new_par.children.concat();
  4100. tmp = $.inArray(obj.id, dpc);
  4101. if(tmp !== -1) {
  4102. dpc = $.vakata.array_remove(dpc, tmp);
  4103. if(pos > tmp) { pos--; }
  4104. }
  4105. tmp = [];
  4106. for(i = 0, j = dpc.length; i < j; i++) {
  4107. tmp[i >= pos ? i+1 : i] = dpc[i];
  4108. }
  4109. tmp[pos] = obj.id;
  4110. new_par.children = tmp;
  4111. this._node_changed(new_par.id);
  4112. this.redraw(new_par.id === $.jstree.root);
  4113. }
  4114. else {
  4115. // clean old parent and up
  4116. tmp = obj.children_d.concat();
  4117. tmp.push(obj.id);
  4118. for(i = 0, j = obj.parents.length; i < j; i++) {
  4119. dpc = [];
  4120. p = old_ins._model.data[obj.parents[i]].children_d;
  4121. for(k = 0, l = p.length; k < l; k++) {
  4122. if($.inArray(p[k], tmp) === -1) {
  4123. dpc.push(p[k]);
  4124. }
  4125. }
  4126. old_ins._model.data[obj.parents[i]].children_d = dpc;
  4127. }
  4128. old_ins._model.data[old_par].children = $.vakata.array_remove_item(old_ins._model.data[old_par].children, obj.id);
  4129. // insert into new parent and up
  4130. for(i = 0, j = new_par.parents.length; i < j; i++) {
  4131. this._model.data[new_par.parents[i]].children_d = this._model.data[new_par.parents[i]].children_d.concat(tmp);
  4132. }
  4133. dpc = [];
  4134. for(i = 0, j = new_par.children.length; i < j; i++) {
  4135. dpc[i >= pos ? i+1 : i] = new_par.children[i];
  4136. }
  4137. dpc[pos] = obj.id;
  4138. new_par.children = dpc;
  4139. new_par.children_d.push(obj.id);
  4140. new_par.children_d = new_par.children_d.concat(obj.children_d);
  4141. // update object
  4142. obj.parent = new_par.id;
  4143. tmp = new_par.parents.concat();
  4144. tmp.unshift(new_par.id);
  4145. p = obj.parents.length;
  4146. obj.parents = tmp;
  4147. // update object children
  4148. tmp = tmp.concat();
  4149. for(i = 0, j = obj.children_d.length; i < j; i++) {
  4150. this._model.data[obj.children_d[i]].parents = this._model.data[obj.children_d[i]].parents.slice(0,p*-1);
  4151. Array.prototype.push.apply(this._model.data[obj.children_d[i]].parents, tmp);
  4152. }
  4153. if(old_par === $.jstree.root || new_par.id === $.jstree.root) {
  4154. this._model.force_full_redraw = true;
  4155. }
  4156. if(!this._model.force_full_redraw) {
  4157. this._node_changed(old_par);
  4158. this._node_changed(new_par.id);
  4159. }
  4160. if(!skip_redraw) {
  4161. this.redraw();
  4162. }
  4163. }
  4164. if(callback) { callback.call(this, obj, new_par, pos); }
  4165. /**
  4166. * triggered when a node is moved
  4167. * @event
  4168. * @name move_node.jstree
  4169. * @param {Object} node
  4170. * @param {String} parent the parent's ID
  4171. * @param {Number} position the position of the node among the parent's children
  4172. * @param {String} old_parent the old parent of the node
  4173. * @param {Number} old_position the old position of the node
  4174. * @param {Boolean} is_multi do the node and new parent belong to different instances
  4175. * @param {jsTree} old_instance the instance the node came from
  4176. * @param {jsTree} new_instance the instance of the new parent
  4177. */
  4178. 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 });
  4179. return obj.id;
  4180. },
  4181. /**
  4182. * copy a node to a new parent
  4183. * @name copy_node(obj, par [, pos, callback, is_loaded])
  4184. * @param {mixed} obj the node to copy, pass an array to copy multiple nodes
  4185. * @param {mixed} par the new parent
  4186. * @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`
  4187. * @param {function} callback a function to call once the move is completed, receives 3 arguments - the node, the new parent and the position
  4188. * @param {Boolean} is_loaded internal parameter indicating if the parent node has been loaded
  4189. * @param {Boolean} skip_redraw internal parameter indicating if the tree should be redrawn
  4190. * @param {Boolean} instance internal parameter indicating if the node comes from another instance
  4191. * @trigger model.jstree copy_node.jstree
  4192. */
  4193. copy_node : function (obj, par, pos, callback, is_loaded, skip_redraw, origin) {
  4194. var t1, t2, dpc, tmp, i, j, node, old_par, new_par, old_ins, is_multi;
  4195. par = this.get_node(par);
  4196. pos = pos === undefined ? 0 : pos;
  4197. if(!par) { return false; }
  4198. if(!pos.toString().match(/^(before|after)$/) && !is_loaded && !this.is_loaded(par)) {
  4199. return this.load_node(par, function () { this.copy_node(obj, par, pos, callback, true, false, origin); });
  4200. }
  4201. if($.isArray(obj)) {
  4202. if(obj.length === 1) {
  4203. obj = obj[0];
  4204. }
  4205. else {
  4206. //obj = obj.slice();
  4207. for(t1 = 0, t2 = obj.length; t1 < t2; t1++) {
  4208. if((tmp = this.copy_node(obj[t1], par, pos, callback, is_loaded, true, origin))) {
  4209. par = tmp;
  4210. pos = "after";
  4211. }
  4212. }
  4213. this.redraw();
  4214. return true;
  4215. }
  4216. }
  4217. obj = obj && obj.id ? obj : this.get_node(obj);
  4218. if(!obj || obj.id === $.jstree.root) { return false; }
  4219. old_par = (obj.parent || $.jstree.root).toString();
  4220. new_par = (!pos.toString().match(/^(before|after)$/) || par.id === $.jstree.root) ? par : this.get_node(par.parent);
  4221. old_ins = origin ? origin : (this._model.data[obj.id] ? this : $.jstree.reference(obj.id));
  4222. is_multi = !old_ins || !old_ins._id || (this._id !== old_ins._id);
  4223. if(old_ins && old_ins._id) {
  4224. obj = old_ins._model.data[obj.id];
  4225. }
  4226. if(par.id === $.jstree.root) {
  4227. if(pos === "before") { pos = "first"; }
  4228. if(pos === "after") { pos = "last"; }
  4229. }
  4230. switch(pos) {
  4231. case "before":
  4232. pos = $.inArray(par.id, new_par.children);
  4233. break;
  4234. case "after" :
  4235. pos = $.inArray(par.id, new_par.children) + 1;
  4236. break;
  4237. case "inside":
  4238. case "first":
  4239. pos = 0;
  4240. break;
  4241. case "last":
  4242. pos = new_par.children.length;
  4243. break;
  4244. default:
  4245. if(!pos) { pos = 0; }
  4246. break;
  4247. }
  4248. if(pos > new_par.children.length) { pos = new_par.children.length; }
  4249. 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) })) {
  4250. this.settings.core.error.call(this, this._data.core.last_error);
  4251. return false;
  4252. }
  4253. node = old_ins ? old_ins.get_json(obj, { no_id : true, no_data : true, no_state : true }) : obj;
  4254. if(!node) { return false; }
  4255. if(node.id === true) { delete node.id; }
  4256. node = this._parse_model_from_json(node, new_par.id, new_par.parents.concat());
  4257. if(!node) { return false; }
  4258. tmp = this.get_node(node);
  4259. if(obj && obj.state && obj.state.loaded === false) { tmp.state.loaded = false; }
  4260. dpc = [];
  4261. dpc.push(node);
  4262. dpc = dpc.concat(tmp.children_d);
  4263. this.trigger('model', { "nodes" : dpc, "parent" : new_par.id });
  4264. // insert into new parent and up
  4265. for(i = 0, j = new_par.parents.length; i < j; i++) {
  4266. this._model.data[new_par.parents[i]].children_d = this._model.data[new_par.parents[i]].children_d.concat(dpc);
  4267. }
  4268. dpc = [];
  4269. for(i = 0, j = new_par.children.length; i < j; i++) {
  4270. dpc[i >= pos ? i+1 : i] = new_par.children[i];
  4271. }
  4272. dpc[pos] = tmp.id;
  4273. new_par.children = dpc;
  4274. new_par.children_d.push(tmp.id);
  4275. new_par.children_d = new_par.children_d.concat(tmp.children_d);
  4276. if(new_par.id === $.jstree.root) {
  4277. this._model.force_full_redraw = true;
  4278. }
  4279. if(!this._model.force_full_redraw) {
  4280. this._node_changed(new_par.id);
  4281. }
  4282. if(!skip_redraw) {
  4283. this.redraw(new_par.id === $.jstree.root);
  4284. }
  4285. if(callback) { callback.call(this, tmp, new_par, pos); }
  4286. /**
  4287. * triggered when a node is copied
  4288. * @event
  4289. * @name copy_node.jstree
  4290. * @param {Object} node the copied node
  4291. * @param {Object} original the original node
  4292. * @param {String} parent the parent's ID
  4293. * @param {Number} position the position of the node among the parent's children
  4294. * @param {String} old_parent the old parent of the node
  4295. * @param {Number} old_position the position of the original node
  4296. * @param {Boolean} is_multi do the node and new parent belong to different instances
  4297. * @param {jsTree} old_instance the instance the node came from
  4298. * @param {jsTree} new_instance the instance of the new parent
  4299. */
  4300. 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 });
  4301. return tmp.id;
  4302. },
  4303. /**
  4304. * cut a node (a later call to `paste(obj)` would move the node)
  4305. * @name cut(obj)
  4306. * @param {mixed} obj multiple objects can be passed using an array
  4307. * @trigger cut.jstree
  4308. */
  4309. cut : function (obj) {
  4310. if(!obj) { obj = this._data.core.selected.concat(); }
  4311. if(!$.isArray(obj)) { obj = [obj]; }
  4312. if(!obj.length) { return false; }
  4313. var tmp = [], o, t1, t2;
  4314. for(t1 = 0, t2 = obj.length; t1 < t2; t1++) {
  4315. o = this.get_node(obj[t1]);
  4316. if(o && o.id && o.id !== $.jstree.root) { tmp.push(o); }
  4317. }
  4318. if(!tmp.length) { return false; }
  4319. ccp_node = tmp;
  4320. ccp_inst = this;
  4321. ccp_mode = 'move_node';
  4322. /**
  4323. * triggered when nodes are added to the buffer for moving
  4324. * @event
  4325. * @name cut.jstree
  4326. * @param {Array} node
  4327. */
  4328. this.trigger('cut', { "node" : obj });
  4329. },
  4330. /**
  4331. * copy a node (a later call to `paste(obj)` would copy the node)
  4332. * @name copy(obj)
  4333. * @param {mixed} obj multiple objects can be passed using an array
  4334. * @trigger copy.jstree
  4335. */
  4336. copy : function (obj) {
  4337. if(!obj) { obj = this._data.core.selected.concat(); }
  4338. if(!$.isArray(obj)) { obj = [obj]; }
  4339. if(!obj.length) { return false; }
  4340. var tmp = [], o, t1, t2;
  4341. for(t1 = 0, t2 = obj.length; t1 < t2; t1++) {
  4342. o = this.get_node(obj[t1]);
  4343. if(o && o.id && o.id !== $.jstree.root) { tmp.push(o); }
  4344. }
  4345. if(!tmp.length) { return false; }
  4346. ccp_node = tmp;
  4347. ccp_inst = this;
  4348. ccp_mode = 'copy_node';
  4349. /**
  4350. * triggered when nodes are added to the buffer for copying
  4351. * @event
  4352. * @name copy.jstree
  4353. * @param {Array} node
  4354. */
  4355. this.trigger('copy', { "node" : obj });
  4356. },
  4357. /**
  4358. * get the current buffer (any nodes that are waiting for a paste operation)
  4359. * @name get_buffer()
  4360. * @return {Object} an object consisting of `mode` ("copy_node" or "move_node"), `node` (an array of objects) and `inst` (the instance)
  4361. */
  4362. get_buffer : function () {
  4363. return { 'mode' : ccp_mode, 'node' : ccp_node, 'inst' : ccp_inst };
  4364. },
  4365. /**
  4366. * check if there is something in the buffer to paste
  4367. * @name can_paste()
  4368. * @return {Boolean}
  4369. */
  4370. can_paste : function () {
  4371. return ccp_mode !== false && ccp_node !== false; // && ccp_inst._model.data[ccp_node];
  4372. },
  4373. /**
  4374. * copy or move the previously cut or copied nodes to a new parent
  4375. * @name paste(obj [, pos])
  4376. * @param {mixed} obj the new parent
  4377. * @param {mixed} pos the position to insert at (besides integer, "first" and "last" are supported), defaults to integer `0`
  4378. * @trigger paste.jstree
  4379. */
  4380. paste : function (obj, pos) {
  4381. obj = this.get_node(obj);
  4382. if(!obj || !ccp_mode || !ccp_mode.match(/^(copy_node|move_node)$/) || !ccp_node) { return false; }
  4383. if(this[ccp_mode](ccp_node, obj, pos, false, false, false, ccp_inst)) {
  4384. /**
  4385. * triggered when paste is invoked
  4386. * @event
  4387. * @name paste.jstree
  4388. * @param {String} parent the ID of the receiving node
  4389. * @param {Array} node the nodes in the buffer
  4390. * @param {String} mode the performed operation - "copy_node" or "move_node"
  4391. */
  4392. this.trigger('paste', { "parent" : obj.id, "node" : ccp_node, "mode" : ccp_mode });
  4393. }
  4394. ccp_node = false;
  4395. ccp_mode = false;
  4396. ccp_inst = false;
  4397. },
  4398. /**
  4399. * clear the buffer of previously copied or cut nodes
  4400. * @name clear_buffer()
  4401. * @trigger clear_buffer.jstree
  4402. */
  4403. clear_buffer : function () {
  4404. ccp_node = false;
  4405. ccp_mode = false;
  4406. ccp_inst = false;
  4407. /**
  4408. * triggered when the copy / cut buffer is cleared
  4409. * @event
  4410. * @name clear_buffer.jstree
  4411. */
  4412. this.trigger('clear_buffer');
  4413. },
  4414. /**
  4415. * put a node in edit mode (input field to rename the node)
  4416. * @name edit(obj [, default_text, callback])
  4417. * @param {mixed} obj
  4418. * @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)
  4419. * @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
  4420. */
  4421. edit : function (obj, default_text, callback) {
  4422. var rtl, w, a, s, t, h1, h2, fn, tmp, cancel = false;
  4423. obj = this.get_node(obj);
  4424. if(!obj) { return false; }
  4425. if(!this.check("edit", obj, this.get_parent(obj))) {
  4426. this.settings.core.error.call(this, this._data.core.last_error);
  4427. return false;
  4428. }
  4429. tmp = obj;
  4430. default_text = typeof default_text === 'string' ? default_text : obj.text;
  4431. this.set_text(obj, "");
  4432. obj = this._open_to(obj);
  4433. tmp.text = default_text;
  4434. rtl = this._data.core.rtl;
  4435. w = this.element.width();
  4436. this._data.core.focused = tmp.id;
  4437. a = obj.children('.jstree-anchor').focus();
  4438. s = $('<span>');
  4439. /*!
  4440. oi = obj.children("i:visible"),
  4441. ai = a.children("i:visible"),
  4442. w1 = oi.width() * oi.length,
  4443. w2 = ai.width() * ai.length,
  4444. */
  4445. t = default_text;
  4446. h1 = $("<"+"div />", { css : { "position" : "absolute", "top" : "-200px", "left" : (rtl ? "0px" : "-1000px"), "visibility" : "hidden" } }).appendTo(document.body);
  4447. h2 = $("<"+"input />", {
  4448. "value" : t,
  4449. "class" : "jstree-rename-input",
  4450. // "size" : t.length,
  4451. "css" : {
  4452. "padding" : "0",
  4453. "border" : "1px solid silver",
  4454. "box-sizing" : "border-box",
  4455. "display" : "inline-block",
  4456. "height" : (this._data.core.li_height) + "px",
  4457. "lineHeight" : (this._data.core.li_height) + "px",
  4458. "width" : "150px" // will be set a bit further down
  4459. },
  4460. "blur" : $.proxy(function (e) {
  4461. e.stopImmediatePropagation();
  4462. e.preventDefault();
  4463. var i = s.children(".jstree-rename-input"),
  4464. v = i.val(),
  4465. f = this.settings.core.force_text,
  4466. nv;
  4467. if(v === "") { v = t; }
  4468. h1.remove();
  4469. s.replaceWith(a);
  4470. s.remove();
  4471. t = f ? t : $('<div></div>').append($.parseHTML(t)).html();
  4472. obj = this.get_node(obj);
  4473. this.set_text(obj, t);
  4474. nv = !!this.rename_node(obj, f ? $('<div></div>').text(v).text() : $('<div></div>').append($.parseHTML(v)).html());
  4475. if(!nv) {
  4476. this.set_text(obj, t); // move this up? and fix #483
  4477. }
  4478. this._data.core.focused = tmp.id;
  4479. setTimeout($.proxy(function () {
  4480. var node = this.get_node(tmp.id, true);
  4481. if(node.length) {
  4482. this._data.core.focused = tmp.id;
  4483. node.children('.jstree-anchor').focus();
  4484. }
  4485. }, this), 0);
  4486. if(callback) {
  4487. callback.call(this, tmp, nv, cancel);
  4488. }
  4489. h2 = null;
  4490. }, this),
  4491. "keydown" : function (e) {
  4492. var key = e.which;
  4493. if(key === 27) {
  4494. cancel = true;
  4495. this.value = t;
  4496. }
  4497. if(key === 27 || key === 13 || key === 37 || key === 38 || key === 39 || key === 40 || key === 32) {
  4498. e.stopImmediatePropagation();
  4499. }
  4500. if(key === 27 || key === 13) {
  4501. e.preventDefault();
  4502. this.blur();
  4503. }
  4504. },
  4505. "click" : function (e) { e.stopImmediatePropagation(); },
  4506. "mousedown" : function (e) { e.stopImmediatePropagation(); },
  4507. "keyup" : function (e) {
  4508. h2.width(Math.min(h1.text("pW" + this.value).width(),w));
  4509. },
  4510. "keypress" : function(e) {
  4511. if(e.which === 13) { return false; }
  4512. }
  4513. });
  4514. fn = {
  4515. fontFamily : a.css('fontFamily') || '',
  4516. fontSize : a.css('fontSize') || '',
  4517. fontWeight : a.css('fontWeight') || '',
  4518. fontStyle : a.css('fontStyle') || '',
  4519. fontStretch : a.css('fontStretch') || '',
  4520. fontVariant : a.css('fontVariant') || '',
  4521. letterSpacing : a.css('letterSpacing') || '',
  4522. wordSpacing : a.css('wordSpacing') || ''
  4523. };
  4524. s.attr('class', a.attr('class')).append(a.contents().clone()).append(h2);
  4525. a.replaceWith(s);
  4526. h1.css(fn);
  4527. h2.css(fn).width(Math.min(h1.text("pW" + h2[0].value).width(),w))[0].select();
  4528. $(document).one('mousedown.jstree touchstart.jstree dnd_start.vakata', function (e) {
  4529. if (h2 && e.target !== h2) {
  4530. $(h2).blur();
  4531. }
  4532. });
  4533. },
  4534. /**
  4535. * changes the theme
  4536. * @name set_theme(theme_name [, theme_url])
  4537. * @param {String} theme_name the name of the new theme to apply
  4538. * @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.
  4539. * @trigger set_theme.jstree
  4540. */
  4541. set_theme : function (theme_name, theme_url) {
  4542. if(!theme_name) { return false; }
  4543. if(theme_url === true) {
  4544. var dir = this.settings.core.themes.dir;
  4545. if(!dir) { dir = $.jstree.path + '/themes'; }
  4546. theme_url = dir + '/' + theme_name + '/style.css';
  4547. }
  4548. if(theme_url && $.inArray(theme_url, themes_loaded) === -1) {
  4549. $('head').append('<'+'link rel="stylesheet" href="' + theme_url + '" type="text/css" />');
  4550. themes_loaded.push(theme_url);
  4551. }
  4552. if(this._data.core.themes.name) {
  4553. this.element.removeClass('jstree-' + this._data.core.themes.name);
  4554. }
  4555. this._data.core.themes.name = theme_name;
  4556. this.element.addClass('jstree-' + theme_name);
  4557. this.element[this.settings.core.themes.responsive ? 'addClass' : 'removeClass' ]('jstree-' + theme_name + '-responsive');
  4558. /**
  4559. * triggered when a theme is set
  4560. * @event
  4561. * @name set_theme.jstree
  4562. * @param {String} theme the new theme
  4563. */
  4564. this.trigger('set_theme', { 'theme' : theme_name });
  4565. },
  4566. /**
  4567. * gets the name of the currently applied theme name
  4568. * @name get_theme()
  4569. * @return {String}
  4570. */
  4571. get_theme : function () { return this._data.core.themes.name; },
  4572. /**
  4573. * changes the theme variant (if the theme has variants)
  4574. * @name set_theme_variant(variant_name)
  4575. * @param {String|Boolean} variant_name the variant to apply (if `false` is used the current variant is removed)
  4576. */
  4577. set_theme_variant : function (variant_name) {
  4578. if(this._data.core.themes.variant) {
  4579. this.element.removeClass('jstree-' + this._data.core.themes.name + '-' + this._data.core.themes.variant);
  4580. }
  4581. this._data.core.themes.variant = variant_name;
  4582. if(variant_name) {
  4583. this.element.addClass('jstree-' + this._data.core.themes.name + '-' + this._data.core.themes.variant);
  4584. }
  4585. },
  4586. /**
  4587. * gets the name of the currently applied theme variant
  4588. * @name get_theme()
  4589. * @return {String}
  4590. */
  4591. get_theme_variant : function () { return this._data.core.themes.variant; },
  4592. /**
  4593. * shows a striped background on the container (if the theme supports it)
  4594. * @name show_stripes()
  4595. */
  4596. show_stripes : function () {
  4597. this._data.core.themes.stripes = true;
  4598. this.get_container_ul().addClass("jstree-striped");
  4599. /**
  4600. * triggered when stripes are shown
  4601. * @event
  4602. * @name show_stripes.jstree
  4603. */
  4604. this.trigger('show_stripes');
  4605. },
  4606. /**
  4607. * hides the striped background on the container
  4608. * @name hide_stripes()
  4609. */
  4610. hide_stripes : function () {
  4611. this._data.core.themes.stripes = false;
  4612. this.get_container_ul().removeClass("jstree-striped");
  4613. /**
  4614. * triggered when stripes are hidden
  4615. * @event
  4616. * @name hide_stripes.jstree
  4617. */
  4618. this.trigger('hide_stripes');
  4619. },
  4620. /**
  4621. * toggles the striped background on the container
  4622. * @name toggle_stripes()
  4623. */
  4624. toggle_stripes : function () { if(this._data.core.themes.stripes) { this.hide_stripes(); } else { this.show_stripes(); } },
  4625. /**
  4626. * shows the connecting dots (if the theme supports it)
  4627. * @name show_dots()
  4628. */
  4629. show_dots : function () {
  4630. this._data.core.themes.dots = true;
  4631. this.get_container_ul().removeClass("jstree-no-dots");
  4632. /**
  4633. * triggered when dots are shown
  4634. * @event
  4635. * @name show_dots.jstree
  4636. */
  4637. this.trigger('show_dots');
  4638. },
  4639. /**
  4640. * hides the connecting dots
  4641. * @name hide_dots()
  4642. */
  4643. hide_dots : function () {
  4644. this._data.core.themes.dots = false;
  4645. this.get_container_ul().addClass("jstree-no-dots");
  4646. /**
  4647. * triggered when dots are hidden
  4648. * @event
  4649. * @name hide_dots.jstree
  4650. */
  4651. this.trigger('hide_dots');
  4652. },
  4653. /**
  4654. * toggles the connecting dots
  4655. * @name toggle_dots()
  4656. */
  4657. toggle_dots : function () { if(this._data.core.themes.dots) { this.hide_dots(); } else { this.show_dots(); } },
  4658. /**
  4659. * show the node icons
  4660. * @name show_icons()
  4661. */
  4662. show_icons : function () {
  4663. this._data.core.themes.icons = true;
  4664. this.get_container_ul().removeClass("jstree-no-icons");
  4665. /**
  4666. * triggered when icons are shown
  4667. * @event
  4668. * @name show_icons.jstree
  4669. */
  4670. this.trigger('show_icons');
  4671. },
  4672. /**
  4673. * hide the node icons
  4674. * @name hide_icons()
  4675. */
  4676. hide_icons : function () {
  4677. this._data.core.themes.icons = false;
  4678. this.get_container_ul().addClass("jstree-no-icons");
  4679. /**
  4680. * triggered when icons are hidden
  4681. * @event
  4682. * @name hide_icons.jstree
  4683. */
  4684. this.trigger('hide_icons');
  4685. },
  4686. /**
  4687. * toggle the node icons
  4688. * @name toggle_icons()
  4689. */
  4690. toggle_icons : function () { if(this._data.core.themes.icons) { this.hide_icons(); } else { this.show_icons(); } },
  4691. /**
  4692. * show the node ellipsis
  4693. * @name show_icons()
  4694. */
  4695. show_ellipsis : function () {
  4696. this._data.core.themes.ellipsis = true;
  4697. this.get_container_ul().addClass("jstree-ellipsis");
  4698. /**
  4699. * triggered when ellisis is shown
  4700. * @event
  4701. * @name show_ellipsis.jstree
  4702. */
  4703. this.trigger('show_ellipsis');
  4704. },
  4705. /**
  4706. * hide the node ellipsis
  4707. * @name hide_ellipsis()
  4708. */
  4709. hide_ellipsis : function () {
  4710. this._data.core.themes.ellipsis = false;
  4711. this.get_container_ul().removeClass("jstree-ellipsis");
  4712. /**
  4713. * triggered when ellisis is hidden
  4714. * @event
  4715. * @name hide_ellipsis.jstree
  4716. */
  4717. this.trigger('hide_ellipsis');
  4718. },
  4719. /**
  4720. * toggle the node ellipsis
  4721. * @name toggle_icons()
  4722. */
  4723. toggle_ellipsis : function () { if(this._data.core.themes.ellipsis) { this.hide_ellipsis(); } else { this.show_ellipsis(); } },
  4724. /**
  4725. * set the node icon for a node
  4726. * @name set_icon(obj, icon)
  4727. * @param {mixed} obj
  4728. * @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
  4729. */
  4730. set_icon : function (obj, icon) {
  4731. var t1, t2, dom, old;
  4732. if($.isArray(obj)) {
  4733. obj = obj.slice();
  4734. for(t1 = 0, t2 = obj.length; t1 < t2; t1++) {
  4735. this.set_icon(obj[t1], icon);
  4736. }
  4737. return true;
  4738. }
  4739. obj = this.get_node(obj);
  4740. if(!obj || obj.id === $.jstree.root) { return false; }
  4741. old = obj.icon;
  4742. obj.icon = icon === true || icon === null || icon === undefined || icon === '' ? true : icon;
  4743. dom = this.get_node(obj, true).children(".jstree-anchor").children(".jstree-themeicon");
  4744. if(icon === false) {
  4745. dom.removeClass('jstree-themeicon-custom ' + old).css("background","").removeAttr("rel");
  4746. this.hide_icon(obj);
  4747. }
  4748. else if(icon === true || icon === null || icon === undefined || icon === '') {
  4749. dom.removeClass('jstree-themeicon-custom ' + old).css("background","").removeAttr("rel");
  4750. if(old === false) { this.show_icon(obj); }
  4751. }
  4752. else if(icon.indexOf("/") === -1 && icon.indexOf(".") === -1) {
  4753. dom.removeClass(old).css("background","");
  4754. dom.addClass(icon + ' jstree-themeicon-custom').attr("rel",icon);
  4755. if(old === false) { this.show_icon(obj); }
  4756. }
  4757. else {
  4758. dom.removeClass(old).css("background","");
  4759. dom.addClass('jstree-themeicon-custom').css("background", "url('" + icon + "') center center no-repeat").attr("rel",icon);
  4760. if(old === false) { this.show_icon(obj); }
  4761. }
  4762. return true;
  4763. },
  4764. /**
  4765. * get the node icon for a node
  4766. * @name get_icon(obj)
  4767. * @param {mixed} obj
  4768. * @return {String}
  4769. */
  4770. get_icon : function (obj) {
  4771. obj = this.get_node(obj);
  4772. return (!obj || obj.id === $.jstree.root) ? false : obj.icon;
  4773. },
  4774. /**
  4775. * hide the icon on an individual node
  4776. * @name hide_icon(obj)
  4777. * @param {mixed} obj
  4778. */
  4779. hide_icon : function (obj) {
  4780. var t1, t2;
  4781. if($.isArray(obj)) {
  4782. obj = obj.slice();
  4783. for(t1 = 0, t2 = obj.length; t1 < t2; t1++) {
  4784. this.hide_icon(obj[t1]);
  4785. }
  4786. return true;
  4787. }
  4788. obj = this.get_node(obj);
  4789. if(!obj || obj === $.jstree.root) { return false; }
  4790. obj.icon = false;
  4791. this.get_node(obj, true).children(".jstree-anchor").children(".jstree-themeicon").addClass('jstree-themeicon-hidden');
  4792. return true;
  4793. },
  4794. /**
  4795. * show the icon on an individual node
  4796. * @name show_icon(obj)
  4797. * @param {mixed} obj
  4798. */
  4799. show_icon : function (obj) {
  4800. var t1, t2, dom;
  4801. if($.isArray(obj)) {
  4802. obj = obj.slice();
  4803. for(t1 = 0, t2 = obj.length; t1 < t2; t1++) {
  4804. this.show_icon(obj[t1]);
  4805. }
  4806. return true;
  4807. }
  4808. obj = this.get_node(obj);
  4809. if(!obj || obj === $.jstree.root) { return false; }
  4810. dom = this.get_node(obj, true);
  4811. obj.icon = dom.length ? dom.children(".jstree-anchor").children(".jstree-themeicon").attr('rel') : true;
  4812. if(!obj.icon) { obj.icon = true; }
  4813. dom.children(".jstree-anchor").children(".jstree-themeicon").removeClass('jstree-themeicon-hidden');
  4814. return true;
  4815. }
  4816. };
  4817. // helpers
  4818. $.vakata = {};
  4819. // collect attributes
  4820. $.vakata.attributes = function(node, with_values) {
  4821. node = $(node)[0];
  4822. var attr = with_values ? {} : [];
  4823. if(node && node.attributes) {
  4824. $.each(node.attributes, function (i, v) {
  4825. if($.inArray(v.name.toLowerCase(),['style','contenteditable','hasfocus','tabindex']) !== -1) { return; }
  4826. if(v.value !== null && $.trim(v.value) !== '') {
  4827. if(with_values) { attr[v.name] = v.value; }
  4828. else { attr.push(v.name); }
  4829. }
  4830. });
  4831. }
  4832. return attr;
  4833. };
  4834. $.vakata.array_unique = function(array) {
  4835. var a = [], i, j, l, o = {};
  4836. for(i = 0, l = array.length; i < l; i++) {
  4837. if(o[array[i]] === undefined) {
  4838. a.push(array[i]);
  4839. o[array[i]] = true;
  4840. }
  4841. }
  4842. return a;
  4843. };
  4844. // remove item from array
  4845. $.vakata.array_remove = function(array, from) {
  4846. array.splice(from, 1);
  4847. return array;
  4848. //var rest = array.slice((to || from) + 1 || array.length);
  4849. //array.length = from < 0 ? array.length + from : from;
  4850. //array.push.apply(array, rest);
  4851. //return array;
  4852. };
  4853. // remove item from array
  4854. $.vakata.array_remove_item = function(array, item) {
  4855. var tmp = $.inArray(item, array);
  4856. return tmp !== -1 ? $.vakata.array_remove(array, tmp) : array;
  4857. };
  4858. $.vakata.array_filter = function(c,a,b,d,e) {
  4859. if (c.filter) {
  4860. return c.filter(a, b);
  4861. }
  4862. d=[];
  4863. for (e in c) {
  4864. if (~~e+''===e+'' && e>=0 && a.call(b,c[e],+e,c)) {
  4865. d.push(c[e]);
  4866. }
  4867. }
  4868. return d;
  4869. };
  4870. /**
  4871. * ### Changed plugin
  4872. *
  4873. * 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.
  4874. */
  4875. $.jstree.plugins.changed = function (options, parent) {
  4876. var last = [];
  4877. this.trigger = function (ev, data) {
  4878. var i, j;
  4879. if(!data) {
  4880. data = {};
  4881. }
  4882. if(ev.replace('.jstree','') === 'changed') {
  4883. data.changed = { selected : [], deselected : [] };
  4884. var tmp = {};
  4885. for(i = 0, j = last.length; i < j; i++) {
  4886. tmp[last[i]] = 1;
  4887. }
  4888. for(i = 0, j = data.selected.length; i < j; i++) {
  4889. if(!tmp[data.selected[i]]) {
  4890. data.changed.selected.push(data.selected[i]);
  4891. }
  4892. else {
  4893. tmp[data.selected[i]] = 2;
  4894. }
  4895. }
  4896. for(i = 0, j = last.length; i < j; i++) {
  4897. if(tmp[last[i]] === 1) {
  4898. data.changed.deselected.push(last[i]);
  4899. }
  4900. }
  4901. last = data.selected.slice();
  4902. }
  4903. /**
  4904. * triggered when selection changes (the "changed" plugin enhances the original event with more data)
  4905. * @event
  4906. * @name changed.jstree
  4907. * @param {Object} node
  4908. * @param {Object} action the action that caused the selection to change
  4909. * @param {Array} selected the current selection
  4910. * @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
  4911. * @param {Object} event the event (if any) that triggered this changed event
  4912. * @plugin changed
  4913. */
  4914. parent.trigger.call(this, ev, data);
  4915. };
  4916. this.refresh = function (skip_loading, forget_state) {
  4917. last = [];
  4918. return parent.refresh.apply(this, arguments);
  4919. };
  4920. };
  4921. /**
  4922. * ### Checkbox plugin
  4923. *
  4924. * This plugin renders checkbox icons in front of each node, making multiple selection much easier.
  4925. * 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.
  4926. */
  4927. var _i = document.createElement('I');
  4928. _i.className = 'jstree-icon jstree-checkbox';
  4929. _i.setAttribute('role', 'presentation');
  4930. /**
  4931. * stores all defaults for the checkbox plugin
  4932. * @name $.jstree.defaults.checkbox
  4933. * @plugin checkbox
  4934. */
  4935. $.jstree.defaults.checkbox = {
  4936. /**
  4937. * a boolean indicating if checkboxes should be visible (can be changed at a later time using `show_checkboxes()` and `hide_checkboxes`). Defaults to `true`.
  4938. * @name $.jstree.defaults.checkbox.visible
  4939. * @plugin checkbox
  4940. */
  4941. visible : true,
  4942. /**
  4943. * a boolean indicating if checkboxes should cascade down and have an undetermined state. Defaults to `true`.
  4944. * @name $.jstree.defaults.checkbox.three_state
  4945. * @plugin checkbox
  4946. */
  4947. three_state : true,
  4948. /**
  4949. * a boolean indicating if clicking anywhere on the node should act as clicking on the checkbox. Defaults to `true`.
  4950. * @name $.jstree.defaults.checkbox.whole_node
  4951. * @plugin checkbox
  4952. */
  4953. whole_node : true,
  4954. /**
  4955. * a boolean indicating if the selected style of a node should be kept, or removed. Defaults to `true`.
  4956. * @name $.jstree.defaults.checkbox.keep_selected_style
  4957. * @plugin checkbox
  4958. */
  4959. keep_selected_style : true,
  4960. /**
  4961. * This setting controls how cascading and undetermined nodes are applied.
  4962. * 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.
  4963. * If `three_state` is set to `true` this setting is automatically set to 'up+down+undetermined'. Defaults to ''.
  4964. * @name $.jstree.defaults.checkbox.cascade
  4965. * @plugin checkbox
  4966. */
  4967. cascade : '',
  4968. /**
  4969. * 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.
  4970. * @name $.jstree.defaults.checkbox.tie_selection
  4971. * @plugin checkbox
  4972. */
  4973. tie_selection : true,
  4974. /**
  4975. * This setting controls if cascading down affects disabled checkboxes
  4976. * @name $.jstree.defaults.checkbox.cascade_to_disabled
  4977. * @plugin checkbox
  4978. */
  4979. cascade_to_disabled : true,
  4980. /**
  4981. * This setting controls if cascading down affects hidden checkboxes
  4982. * @name $.jstree.defaults.checkbox.cascade_to_hidden
  4983. * @plugin checkbox
  4984. */
  4985. cascade_to_hidden : true
  4986. };
  4987. $.jstree.plugins.checkbox = function (options, parent) {
  4988. this.bind = function () {
  4989. parent.bind.call(this);
  4990. this._data.checkbox.uto = false;
  4991. this._data.checkbox.selected = [];
  4992. if(this.settings.checkbox.three_state) {
  4993. this.settings.checkbox.cascade = 'up+down+undetermined';
  4994. }
  4995. this.element
  4996. .on("init.jstree", $.proxy(function () {
  4997. this._data.checkbox.visible = this.settings.checkbox.visible;
  4998. if(!this.settings.checkbox.keep_selected_style) {
  4999. this.element.addClass('jstree-checkbox-no-clicked');
  5000. }
  5001. if(this.settings.checkbox.tie_selection) {
  5002. this.element.addClass('jstree-checkbox-selection');
  5003. }
  5004. }, this))
  5005. .on("loading.jstree", $.proxy(function () {
  5006. this[ this._data.checkbox.visible ? 'show_checkboxes' : 'hide_checkboxes' ]();
  5007. }, this));
  5008. if(this.settings.checkbox.cascade.indexOf('undetermined') !== -1) {
  5009. this.element
  5010. .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 () {
  5011. // only if undetermined is in setting
  5012. if(this._data.checkbox.uto) { clearTimeout(this._data.checkbox.uto); }
  5013. this._data.checkbox.uto = setTimeout($.proxy(this._undetermined, this), 50);
  5014. }, this));
  5015. }
  5016. if(!this.settings.checkbox.tie_selection) {
  5017. this.element
  5018. .on('model.jstree', $.proxy(function (e, data) {
  5019. var m = this._model.data,
  5020. p = m[data.parent],
  5021. dpc = data.nodes,
  5022. i, j;
  5023. for(i = 0, j = dpc.length; i < j; i++) {
  5024. 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);
  5025. if(m[dpc[i]].state.checked) {
  5026. this._data.checkbox.selected.push(dpc[i]);
  5027. }
  5028. }
  5029. }, this));
  5030. }
  5031. if(this.settings.checkbox.cascade.indexOf('up') !== -1 || this.settings.checkbox.cascade.indexOf('down') !== -1) {
  5032. this.element
  5033. .on('model.jstree', $.proxy(function (e, data) {
  5034. var m = this._model.data,
  5035. p = m[data.parent],
  5036. dpc = data.nodes,
  5037. chd = [],
  5038. c, i, j, k, l, tmp, s = this.settings.checkbox.cascade, t = this.settings.checkbox.tie_selection;
  5039. if(s.indexOf('down') !== -1) {
  5040. // apply down
  5041. if(p.state[ t ? 'selected' : 'checked' ]) {
  5042. for(i = 0, j = dpc.length; i < j; i++) {
  5043. m[dpc[i]].state[ t ? 'selected' : 'checked' ] = true;
  5044. }
  5045. this._data[ t ? 'core' : 'checkbox' ].selected = this._data[ t ? 'core' : 'checkbox' ].selected.concat(dpc);
  5046. }
  5047. else {
  5048. for(i = 0, j = dpc.length; i < j; i++) {
  5049. if(m[dpc[i]].state[ t ? 'selected' : 'checked' ]) {
  5050. for(k = 0, l = m[dpc[i]].children_d.length; k < l; k++) {
  5051. m[m[dpc[i]].children_d[k]].state[ t ? 'selected' : 'checked' ] = true;
  5052. }
  5053. this._data[ t ? 'core' : 'checkbox' ].selected = this._data[ t ? 'core' : 'checkbox' ].selected.concat(m[dpc[i]].children_d);
  5054. }
  5055. }
  5056. }
  5057. }
  5058. if(s.indexOf('up') !== -1) {
  5059. // apply up
  5060. for(i = 0, j = p.children_d.length; i < j; i++) {
  5061. if(!m[p.children_d[i]].children.length) {
  5062. chd.push(m[p.children_d[i]].parent);
  5063. }
  5064. }
  5065. chd = $.vakata.array_unique(chd);
  5066. for(k = 0, l = chd.length; k < l; k++) {
  5067. p = m[chd[k]];
  5068. while(p && p.id !== $.jstree.root) {
  5069. c = 0;
  5070. for(i = 0, j = p.children.length; i < j; i++) {
  5071. c += m[p.children[i]].state[ t ? 'selected' : 'checked' ];
  5072. }
  5073. if(c === j) {
  5074. p.state[ t ? 'selected' : 'checked' ] = true;
  5075. this._data[ t ? 'core' : 'checkbox' ].selected.push(p.id);
  5076. tmp = this.get_node(p, true);
  5077. if(tmp && tmp.length) {
  5078. tmp.attr('aria-selected', true).children('.jstree-anchor').addClass( t ? 'jstree-clicked' : 'jstree-checked');
  5079. }
  5080. }
  5081. else {
  5082. break;
  5083. }
  5084. p = this.get_node(p.parent);
  5085. }
  5086. }
  5087. }
  5088. this._data[ t ? 'core' : 'checkbox' ].selected = $.vakata.array_unique(this._data[ t ? 'core' : 'checkbox' ].selected);
  5089. }, this))
  5090. .on(this.settings.checkbox.tie_selection ? 'select_node.jstree' : 'check_node.jstree', $.proxy(function (e, data) {
  5091. var self = this,
  5092. obj = data.node,
  5093. m = this._model.data,
  5094. par = this.get_node(obj.parent),
  5095. i, j, c, tmp, s = this.settings.checkbox.cascade, t = this.settings.checkbox.tie_selection,
  5096. sel = {}, cur = this._data[ t ? 'core' : 'checkbox' ].selected;
  5097. for (i = 0, j = cur.length; i < j; i++) {
  5098. sel[cur[i]] = true;
  5099. }
  5100. // apply down
  5101. if(s.indexOf('down') !== -1) {
  5102. //this._data[ t ? 'core' : 'checkbox' ].selected = $.vakata.array_unique(this._data[ t ? 'core' : 'checkbox' ].selected.concat(obj.children_d));
  5103. var selectedIds = this._cascade_new_checked_state(obj.id, true);
  5104. var temp = obj.children_d.concat(obj.id);
  5105. for (i = 0, j = temp.length; i < j; i++) {
  5106. if (selectedIds.indexOf(temp[i]) > -1) {
  5107. sel[temp[i]] = true;
  5108. }
  5109. else {
  5110. delete sel[temp[i]];
  5111. }
  5112. }
  5113. }
  5114. // apply up
  5115. if(s.indexOf('up') !== -1) {
  5116. while(par && par.id !== $.jstree.root) {
  5117. c = 0;
  5118. for(i = 0, j = par.children.length; i < j; i++) {
  5119. c += m[par.children[i]].state[ t ? 'selected' : 'checked' ];
  5120. }
  5121. if(c === j) {
  5122. par.state[ t ? 'selected' : 'checked' ] = true;
  5123. sel[par.id] = true;
  5124. //this._data[ t ? 'core' : 'checkbox' ].selected.push(par.id);
  5125. tmp = this.get_node(par, true);
  5126. if(tmp && tmp.length) {
  5127. tmp.attr('aria-selected', true).children('.jstree-anchor').addClass(t ? 'jstree-clicked' : 'jstree-checked');
  5128. }
  5129. }
  5130. else {
  5131. break;
  5132. }
  5133. par = this.get_node(par.parent);
  5134. }
  5135. }
  5136. cur = [];
  5137. for (i in sel) {
  5138. if (sel.hasOwnProperty(i)) {
  5139. cur.push(i);
  5140. }
  5141. }
  5142. this._data[ t ? 'core' : 'checkbox' ].selected = cur;
  5143. }, this))
  5144. .on(this.settings.checkbox.tie_selection ? 'deselect_all.jstree' : 'uncheck_all.jstree', $.proxy(function (e, data) {
  5145. var obj = this.get_node($.jstree.root),
  5146. m = this._model.data,
  5147. i, j, tmp;
  5148. for(i = 0, j = obj.children_d.length; i < j; i++) {
  5149. tmp = m[obj.children_d[i]];
  5150. if(tmp && tmp.original && tmp.original.state && tmp.original.state.undetermined) {
  5151. tmp.original.state.undetermined = false;
  5152. }
  5153. }
  5154. }, this))
  5155. .on(this.settings.checkbox.tie_selection ? 'deselect_node.jstree' : 'uncheck_node.jstree', $.proxy(function (e, data) {
  5156. var self = this,
  5157. obj = data.node,
  5158. dom = this.get_node(obj, true),
  5159. i, j, tmp, s = this.settings.checkbox.cascade, t = this.settings.checkbox.tie_selection,
  5160. cur = this._data[ t ? 'core' : 'checkbox' ].selected, sel = {},
  5161. stillSelectedIds = [],
  5162. allIds = obj.children_d.concat(obj.id);
  5163. // apply down
  5164. if(s.indexOf('down') !== -1) {
  5165. var selectedIds = this._cascade_new_checked_state(obj.id, false);
  5166. cur = $.vakata.array_filter(cur, function(id) {
  5167. return allIds.indexOf(id) === -1 || selectedIds.indexOf(id) > -1;
  5168. });
  5169. }
  5170. // only apply up if cascade up is enabled and if this node is not selected
  5171. // (if all child nodes are disabled and cascade_to_disabled === false then this node will till be selected).
  5172. if(s.indexOf('up') !== -1 && cur.indexOf(obj.id) === -1) {
  5173. for(i = 0, j = obj.parents.length; i < j; i++) {
  5174. tmp = this._model.data[obj.parents[i]];
  5175. tmp.state[ t ? 'selected' : 'checked' ] = false;
  5176. if(tmp && tmp.original && tmp.original.state && tmp.original.state.undetermined) {
  5177. tmp.original.state.undetermined = false;
  5178. }
  5179. tmp = this.get_node(obj.parents[i], true);
  5180. if(tmp && tmp.length) {
  5181. tmp.attr('aria-selected', false).children('.jstree-anchor').removeClass(t ? 'jstree-clicked' : 'jstree-checked');
  5182. }
  5183. }
  5184. cur = $.vakata.array_filter(cur, function(id) {
  5185. return obj.parents.indexOf(id) === -1;
  5186. });
  5187. }
  5188. this._data[ t ? 'core' : 'checkbox' ].selected = cur;
  5189. }, this));
  5190. }
  5191. if(this.settings.checkbox.cascade.indexOf('up') !== -1) {
  5192. this.element
  5193. .on('delete_node.jstree', $.proxy(function (e, data) {
  5194. // apply up (whole handler)
  5195. var p = this.get_node(data.parent),
  5196. m = this._model.data,
  5197. i, j, c, tmp, t = this.settings.checkbox.tie_selection;
  5198. while(p && p.id !== $.jstree.root && !p.state[ t ? 'selected' : 'checked' ]) {
  5199. c = 0;
  5200. for(i = 0, j = p.children.length; i < j; i++) {
  5201. c += m[p.children[i]].state[ t ? 'selected' : 'checked' ];
  5202. }
  5203. if(j > 0 && c === j) {
  5204. p.state[ t ? 'selected' : 'checked' ] = true;
  5205. this._data[ t ? 'core' : 'checkbox' ].selected.push(p.id);
  5206. tmp = this.get_node(p, true);
  5207. if(tmp && tmp.length) {
  5208. tmp.attr('aria-selected', true).children('.jstree-anchor').addClass(t ? 'jstree-clicked' : 'jstree-checked');
  5209. }
  5210. }
  5211. else {
  5212. break;
  5213. }
  5214. p = this.get_node(p.parent);
  5215. }
  5216. }, this))
  5217. .on('move_node.jstree', $.proxy(function (e, data) {
  5218. // apply up (whole handler)
  5219. var is_multi = data.is_multi,
  5220. old_par = data.old_parent,
  5221. new_par = this.get_node(data.parent),
  5222. m = this._model.data,
  5223. p, c, i, j, tmp, t = this.settings.checkbox.tie_selection;
  5224. if(!is_multi) {
  5225. p = this.get_node(old_par);
  5226. while(p && p.id !== $.jstree.root && !p.state[ t ? 'selected' : 'checked' ]) {
  5227. c = 0;
  5228. for(i = 0, j = p.children.length; i < j; i++) {
  5229. c += m[p.children[i]].state[ t ? 'selected' : 'checked' ];
  5230. }
  5231. if(j > 0 && c === j) {
  5232. p.state[ t ? 'selected' : 'checked' ] = true;
  5233. this._data[ t ? 'core' : 'checkbox' ].selected.push(p.id);
  5234. tmp = this.get_node(p, true);
  5235. if(tmp && tmp.length) {
  5236. tmp.attr('aria-selected', true).children('.jstree-anchor').addClass(t ? 'jstree-clicked' : 'jstree-checked');
  5237. }
  5238. }
  5239. else {
  5240. break;
  5241. }
  5242. p = this.get_node(p.parent);
  5243. }
  5244. }
  5245. p = new_par;
  5246. while(p && p.id !== $.jstree.root) {
  5247. c = 0;
  5248. for(i = 0, j = p.children.length; i < j; i++) {
  5249. c += m[p.children[i]].state[ t ? 'selected' : 'checked' ];
  5250. }
  5251. if(c === j) {
  5252. if(!p.state[ t ? 'selected' : 'checked' ]) {
  5253. p.state[ t ? 'selected' : 'checked' ] = true;
  5254. this._data[ t ? 'core' : 'checkbox' ].selected.push(p.id);
  5255. tmp = this.get_node(p, true);
  5256. if(tmp && tmp.length) {
  5257. tmp.attr('aria-selected', true).children('.jstree-anchor').addClass(t ? 'jstree-clicked' : 'jstree-checked');
  5258. }
  5259. }
  5260. }
  5261. else {
  5262. if(p.state[ t ? 'selected' : 'checked' ]) {
  5263. p.state[ t ? 'selected' : 'checked' ] = false;
  5264. this._data[ t ? 'core' : 'checkbox' ].selected = $.vakata.array_remove_item(this._data[ t ? 'core' : 'checkbox' ].selected, p.id);
  5265. tmp = this.get_node(p, true);
  5266. if(tmp && tmp.length) {
  5267. tmp.attr('aria-selected', false).children('.jstree-anchor').removeClass(t ? 'jstree-clicked' : 'jstree-checked');
  5268. }
  5269. }
  5270. else {
  5271. break;
  5272. }
  5273. }
  5274. p = this.get_node(p.parent);
  5275. }
  5276. }, this));
  5277. }
  5278. };
  5279. /**
  5280. * get an array of all nodes whose state is "undetermined"
  5281. * @name get_undetermined([full])
  5282. * @param {boolean} full: if set to `true` the returned array will consist of the full node objects, otherwise - only IDs will be returned
  5283. * @return {Array}
  5284. * @plugin checkbox
  5285. */
  5286. this.get_undetermined = function (full) {
  5287. if (this.settings.checkbox.cascade.indexOf('undetermined') === -1) {
  5288. return [];
  5289. }
  5290. 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, r = [];
  5291. for(i = 0, j = s.length; i < j; i++) {
  5292. if(m[s[i]] && m[s[i]].parents) {
  5293. for(k = 0, l = m[s[i]].parents.length; k < l; k++) {
  5294. if(o[m[s[i]].parents[k]] !== undefined) {
  5295. break;
  5296. }
  5297. if(m[s[i]].parents[k] !== $.jstree.root) {
  5298. o[m[s[i]].parents[k]] = true;
  5299. p.push(m[s[i]].parents[k]);
  5300. }
  5301. }
  5302. }
  5303. }
  5304. // attempt for server side undetermined state
  5305. this.element.find('.jstree-closed').not(':has(.jstree-children)')
  5306. .each(function () {
  5307. var tmp = tt.get_node(this), tmp2;
  5308. if(!tmp) { return; }
  5309. if(!tmp.state.loaded) {
  5310. if(tmp.original && tmp.original.state && tmp.original.state.undetermined && tmp.original.state.undetermined === true) {
  5311. if(o[tmp.id] === undefined && tmp.id !== $.jstree.root) {
  5312. o[tmp.id] = true;
  5313. p.push(tmp.id);
  5314. }
  5315. for(k = 0, l = tmp.parents.length; k < l; k++) {
  5316. if(o[tmp.parents[k]] === undefined && tmp.parents[k] !== $.jstree.root) {
  5317. o[tmp.parents[k]] = true;
  5318. p.push(tmp.parents[k]);
  5319. }
  5320. }
  5321. }
  5322. }
  5323. else {
  5324. for(i = 0, j = tmp.children_d.length; i < j; i++) {
  5325. tmp2 = m[tmp.children_d[i]];
  5326. if(!tmp2.state.loaded && tmp2.original && tmp2.original.state && tmp2.original.state.undetermined && tmp2.original.state.undetermined === true) {
  5327. if(o[tmp2.id] === undefined && tmp2.id !== $.jstree.root) {
  5328. o[tmp2.id] = true;
  5329. p.push(tmp2.id);
  5330. }
  5331. for(k = 0, l = tmp2.parents.length; k < l; k++) {
  5332. if(o[tmp2.parents[k]] === undefined && tmp2.parents[k] !== $.jstree.root) {
  5333. o[tmp2.parents[k]] = true;
  5334. p.push(tmp2.parents[k]);
  5335. }
  5336. }
  5337. }
  5338. }
  5339. }
  5340. });
  5341. for (i = 0, j = p.length; i < j; i++) {
  5342. if(!m[p[i]].state[ t ? 'selected' : 'checked' ]) {
  5343. r.push(full ? m[p[i]] : p[i]);
  5344. }
  5345. }
  5346. return r;
  5347. };
  5348. /**
  5349. * set the undetermined state where and if necessary. Used internally.
  5350. * @private
  5351. * @name _undetermined()
  5352. * @plugin checkbox
  5353. */
  5354. this._undetermined = function () {
  5355. if(this.element === null) { return; }
  5356. var p = this.get_undetermined(false), i, j, s;
  5357. this.element.find('.jstree-undetermined').removeClass('jstree-undetermined');
  5358. for (i = 0, j = p.length; i < j; i++) {
  5359. s = this.get_node(p[i], true);
  5360. if(s && s.length) {
  5361. s.children('.jstree-anchor').children('.jstree-checkbox').addClass('jstree-undetermined');
  5362. }
  5363. }
  5364. };
  5365. this.redraw_node = function(obj, deep, is_callback, force_render) {
  5366. obj = parent.redraw_node.apply(this, arguments);
  5367. if(obj) {
  5368. var i, j, tmp = null, icon = null;
  5369. for(i = 0, j = obj.childNodes.length; i < j; i++) {
  5370. if(obj.childNodes[i] && obj.childNodes[i].className && obj.childNodes[i].className.indexOf("jstree-anchor") !== -1) {
  5371. tmp = obj.childNodes[i];
  5372. break;
  5373. }
  5374. }
  5375. if(tmp) {
  5376. if(!this.settings.checkbox.tie_selection && this._model.data[obj.id].state.checked) { tmp.className += ' jstree-checked'; }
  5377. icon = _i.cloneNode(false);
  5378. if(this._model.data[obj.id].state.checkbox_disabled) { icon.className += ' jstree-checkbox-disabled'; }
  5379. tmp.insertBefore(icon, tmp.childNodes[0]);
  5380. }
  5381. }
  5382. if(!is_callback && this.settings.checkbox.cascade.indexOf('undetermined') !== -1) {
  5383. if(this._data.checkbox.uto) { clearTimeout(this._data.checkbox.uto); }
  5384. this._data.checkbox.uto = setTimeout($.proxy(this._undetermined, this), 50);
  5385. }
  5386. return obj;
  5387. };
  5388. /**
  5389. * show the node checkbox icons
  5390. * @name show_checkboxes()
  5391. * @plugin checkbox
  5392. */
  5393. this.show_checkboxes = function () { this._data.core.themes.checkboxes = true; this.get_container_ul().removeClass("jstree-no-checkboxes"); };
  5394. /**
  5395. * hide the node checkbox icons
  5396. * @name hide_checkboxes()
  5397. * @plugin checkbox
  5398. */
  5399. this.hide_checkboxes = function () { this._data.core.themes.checkboxes = false; this.get_container_ul().addClass("jstree-no-checkboxes"); };
  5400. /**
  5401. * toggle the node icons
  5402. * @name toggle_checkboxes()
  5403. * @plugin checkbox
  5404. */
  5405. this.toggle_checkboxes = function () { if(this._data.core.themes.checkboxes) { this.hide_checkboxes(); } else { this.show_checkboxes(); } };
  5406. /**
  5407. * checks if a node is in an undetermined state
  5408. * @name is_undetermined(obj)
  5409. * @param {mixed} obj
  5410. * @return {Boolean}
  5411. */
  5412. this.is_undetermined = function (obj) {
  5413. obj = this.get_node(obj);
  5414. 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;
  5415. if(!obj || obj.state[ t ? 'selected' : 'checked' ] === true || s.indexOf('undetermined') === -1 || (s.indexOf('down') === -1 && s.indexOf('up') === -1)) {
  5416. return false;
  5417. }
  5418. if(!obj.state.loaded && obj.original.state.undetermined === true) {
  5419. return true;
  5420. }
  5421. for(i = 0, j = obj.children_d.length; i < j; i++) {
  5422. if($.inArray(obj.children_d[i], d) !== -1 || (!m[obj.children_d[i]].state.loaded && m[obj.children_d[i]].original.state.undetermined)) {
  5423. return true;
  5424. }
  5425. }
  5426. return false;
  5427. };
  5428. /**
  5429. * disable a node's checkbox
  5430. * @name disable_checkbox(obj)
  5431. * @param {mixed} obj an array can be used too
  5432. * @trigger disable_checkbox.jstree
  5433. * @plugin checkbox
  5434. */
  5435. this.disable_checkbox = function (obj) {
  5436. var t1, t2, dom;
  5437. if($.isArray(obj)) {
  5438. obj = obj.slice();
  5439. for(t1 = 0, t2 = obj.length; t1 < t2; t1++) {
  5440. this.disable_checkbox(obj[t1]);
  5441. }
  5442. return true;
  5443. }
  5444. obj = this.get_node(obj);
  5445. if(!obj || obj.id === $.jstree.root) {
  5446. return false;
  5447. }
  5448. dom = this.get_node(obj, true);
  5449. if(!obj.state.checkbox_disabled) {
  5450. obj.state.checkbox_disabled = true;
  5451. if(dom && dom.length) {
  5452. dom.children('.jstree-anchor').children('.jstree-checkbox').addClass('jstree-checkbox-disabled');
  5453. }
  5454. /**
  5455. * triggered when an node's checkbox is disabled
  5456. * @event
  5457. * @name disable_checkbox.jstree
  5458. * @param {Object} node
  5459. * @plugin checkbox
  5460. */
  5461. this.trigger('disable_checkbox', { 'node' : obj });
  5462. }
  5463. };
  5464. /**
  5465. * enable a node's checkbox
  5466. * @name enable_checkbox(obj)
  5467. * @param {mixed} obj an array can be used too
  5468. * @trigger enable_checkbox.jstree
  5469. * @plugin checkbox
  5470. */
  5471. this.enable_checkbox = function (obj) {
  5472. var t1, t2, dom;
  5473. if($.isArray(obj)) {
  5474. obj = obj.slice();
  5475. for(t1 = 0, t2 = obj.length; t1 < t2; t1++) {
  5476. this.enable_checkbox(obj[t1]);
  5477. }
  5478. return true;
  5479. }
  5480. obj = this.get_node(obj);
  5481. if(!obj || obj.id === $.jstree.root) {
  5482. return false;
  5483. }
  5484. dom = this.get_node(obj, true);
  5485. if(obj.state.checkbox_disabled) {
  5486. obj.state.checkbox_disabled = false;
  5487. if(dom && dom.length) {
  5488. dom.children('.jstree-anchor').children('.jstree-checkbox').removeClass('jstree-checkbox-disabled');
  5489. }
  5490. /**
  5491. * triggered when an node's checkbox is enabled
  5492. * @event
  5493. * @name enable_checkbox.jstree
  5494. * @param {Object} node
  5495. * @plugin checkbox
  5496. */
  5497. this.trigger('enable_checkbox', { 'node' : obj });
  5498. }
  5499. };
  5500. this.activate_node = function (obj, e) {
  5501. if($(e.target).hasClass('jstree-checkbox-disabled')) {
  5502. return false;
  5503. }
  5504. if(this.settings.checkbox.tie_selection && (this.settings.checkbox.whole_node || $(e.target).hasClass('jstree-checkbox'))) {
  5505. e.ctrlKey = true;
  5506. }
  5507. if(this.settings.checkbox.tie_selection || (!this.settings.checkbox.whole_node && !$(e.target).hasClass('jstree-checkbox'))) {
  5508. return parent.activate_node.call(this, obj, e);
  5509. }
  5510. if(this.is_disabled(obj)) {
  5511. return false;
  5512. }
  5513. if(this.is_checked(obj)) {
  5514. this.uncheck_node(obj, e);
  5515. }
  5516. else {
  5517. this.check_node(obj, e);
  5518. }
  5519. this.trigger('activate_node', { 'node' : this.get_node(obj) });
  5520. };
  5521. /**
  5522. * Cascades checked state to a node and all its descendants. This function does NOT affect hidden and disabled nodes (or their descendants).
  5523. * However if these unaffected nodes are already selected their ids will be included in the returned array.
  5524. * @private
  5525. * @param {string} id the node ID
  5526. * @param {bool} checkedState should the nodes be checked or not
  5527. * @returns {Array} Array of all node id's (in this tree branch) that are checked.
  5528. */
  5529. this._cascade_new_checked_state = function (id, checkedState) {
  5530. var self = this;
  5531. var t = this.settings.checkbox.tie_selection;
  5532. var node = this._model.data[id];
  5533. var selectedNodeIds = [];
  5534. var selectedChildrenIds = [], i, j, selectedChildIds;
  5535. if (
  5536. (this.settings.checkbox.cascade_to_disabled || !node.state.disabled) &&
  5537. (this.settings.checkbox.cascade_to_hidden || !node.state.hidden)
  5538. ) {
  5539. //First try and check/uncheck the children
  5540. if (node.children) {
  5541. for (i = 0, j = node.children.length; i < j; i++) {
  5542. var childId = node.children[i];
  5543. selectedChildIds = self._cascade_new_checked_state(childId, checkedState);
  5544. selectedNodeIds = selectedNodeIds.concat(selectedChildIds);
  5545. if (selectedChildIds.indexOf(childId) > -1) {
  5546. selectedChildrenIds.push(childId);
  5547. }
  5548. }
  5549. }
  5550. var dom = self.get_node(node, true);
  5551. //A node's state is undetermined if some but not all of it's children are checked/selected .
  5552. var undetermined = selectedChildrenIds.length > 0 && selectedChildrenIds.length < node.children.length;
  5553. if(node.original && node.original.state && node.original.state.undetermined) {
  5554. node.original.state.undetermined = undetermined;
  5555. }
  5556. //If a node is undetermined then remove selected class
  5557. if (undetermined) {
  5558. node.state[ t ? 'selected' : 'checked' ] = false;
  5559. dom.attr('aria-selected', false).children('.jstree-anchor').removeClass(t ? 'jstree-clicked' : 'jstree-checked');
  5560. }
  5561. //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),
  5562. //check the node and style it correctly.
  5563. else if (checkedState && selectedChildrenIds.length === node.children.length) {
  5564. node.state[ t ? 'selected' : 'checked' ] = checkedState;
  5565. selectedNodeIds.push(node.id);
  5566. dom.attr('aria-selected', true).children('.jstree-anchor').addClass(t ? 'jstree-clicked' : 'jstree-checked');
  5567. }
  5568. else {
  5569. node.state[ t ? 'selected' : 'checked' ] = false;
  5570. dom.attr('aria-selected', false).children('.jstree-anchor').removeClass(t ? 'jstree-clicked' : 'jstree-checked');
  5571. }
  5572. }
  5573. else {
  5574. selectedChildIds = this.get_checked_descendants(id);
  5575. if (node.state[ t ? 'selected' : 'checked' ]) {
  5576. selectedChildIds.push(node.id);
  5577. }
  5578. selectedNodeIds = selectedNodeIds.concat(selectedChildIds);
  5579. }
  5580. return selectedNodeIds;
  5581. };
  5582. /**
  5583. * Gets ids of nodes selected in branch (of tree) specified by id (does not include the node specified by id)
  5584. * @name get_checked_descendants(obj)
  5585. * @param {string} id the node ID
  5586. * @return {Array} array of IDs
  5587. * @plugin checkbox
  5588. */
  5589. this.get_checked_descendants = function (id) {
  5590. var self = this;
  5591. var t = self.settings.checkbox.tie_selection;
  5592. var node = self._model.data[id];
  5593. return $.vakata.array_filter(node.children_d, function(_id) {
  5594. return self._model.data[_id].state[ t ? 'selected' : 'checked' ];
  5595. });
  5596. };
  5597. /**
  5598. * check a node (only if tie_selection in checkbox settings is false, otherwise select_node will be called internally)
  5599. * @name check_node(obj)
  5600. * @param {mixed} obj an array can be used to check multiple nodes
  5601. * @trigger check_node.jstree
  5602. * @plugin checkbox
  5603. */
  5604. this.check_node = function (obj, e) {
  5605. if(this.settings.checkbox.tie_selection) { return this.select_node(obj, false, true, e); }
  5606. var dom, t1, t2, th;
  5607. if($.isArray(obj)) {
  5608. obj = obj.slice();
  5609. for(t1 = 0, t2 = obj.length; t1 < t2; t1++) {
  5610. this.check_node(obj[t1], e);
  5611. }
  5612. return true;
  5613. }
  5614. obj = this.get_node(obj);
  5615. if(!obj || obj.id === $.jstree.root) {
  5616. return false;
  5617. }
  5618. dom = this.get_node(obj, true);
  5619. if(!obj.state.checked) {
  5620. obj.state.checked = true;
  5621. this._data.checkbox.selected.push(obj.id);
  5622. if(dom && dom.length) {
  5623. dom.children('.jstree-anchor').addClass('jstree-checked');
  5624. }
  5625. /**
  5626. * triggered when an node is checked (only if tie_selection in checkbox settings is false)
  5627. * @event
  5628. * @name check_node.jstree
  5629. * @param {Object} node
  5630. * @param {Array} selected the current selection
  5631. * @param {Object} event the event (if any) that triggered this check_node
  5632. * @plugin checkbox
  5633. */
  5634. this.trigger('check_node', { 'node' : obj, 'selected' : this._data.checkbox.selected, 'event' : e });
  5635. }
  5636. };
  5637. /**
  5638. * uncheck a node (only if tie_selection in checkbox settings is false, otherwise deselect_node will be called internally)
  5639. * @name uncheck_node(obj)
  5640. * @param {mixed} obj an array can be used to uncheck multiple nodes
  5641. * @trigger uncheck_node.jstree
  5642. * @plugin checkbox
  5643. */
  5644. this.uncheck_node = function (obj, e) {
  5645. if(this.settings.checkbox.tie_selection) { return this.deselect_node(obj, false, e); }
  5646. var t1, t2, dom;
  5647. if($.isArray(obj)) {
  5648. obj = obj.slice();
  5649. for(t1 = 0, t2 = obj.length; t1 < t2; t1++) {
  5650. this.uncheck_node(obj[t1], e);
  5651. }
  5652. return true;
  5653. }
  5654. obj = this.get_node(obj);
  5655. if(!obj || obj.id === $.jstree.root) {
  5656. return false;
  5657. }
  5658. dom = this.get_node(obj, true);
  5659. if(obj.state.checked) {
  5660. obj.state.checked = false;
  5661. this._data.checkbox.selected = $.vakata.array_remove_item(this._data.checkbox.selected, obj.id);
  5662. if(dom.length) {
  5663. dom.children('.jstree-anchor').removeClass('jstree-checked');
  5664. }
  5665. /**
  5666. * triggered when an node is unchecked (only if tie_selection in checkbox settings is false)
  5667. * @event
  5668. * @name uncheck_node.jstree
  5669. * @param {Object} node
  5670. * @param {Array} selected the current selection
  5671. * @param {Object} event the event (if any) that triggered this uncheck_node
  5672. * @plugin checkbox
  5673. */
  5674. this.trigger('uncheck_node', { 'node' : obj, 'selected' : this._data.checkbox.selected, 'event' : e });
  5675. }
  5676. };
  5677. /**
  5678. * checks all nodes in the tree (only if tie_selection in checkbox settings is false, otherwise select_all will be called internally)
  5679. * @name check_all()
  5680. * @trigger check_all.jstree, changed.jstree
  5681. * @plugin checkbox
  5682. */
  5683. this.check_all = function () {
  5684. if(this.settings.checkbox.tie_selection) { return this.select_all(); }
  5685. var tmp = this._data.checkbox.selected.concat([]), i, j;
  5686. this._data.checkbox.selected = this._model.data[$.jstree.root].children_d.concat();
  5687. for(i = 0, j = this._data.checkbox.selected.length; i < j; i++) {
  5688. if(this._model.data[this._data.checkbox.selected[i]]) {
  5689. this._model.data[this._data.checkbox.selected[i]].state.checked = true;
  5690. }
  5691. }
  5692. this.redraw(true);
  5693. /**
  5694. * triggered when all nodes are checked (only if tie_selection in checkbox settings is false)
  5695. * @event
  5696. * @name check_all.jstree
  5697. * @param {Array} selected the current selection
  5698. * @plugin checkbox
  5699. */
  5700. this.trigger('check_all', { 'selected' : this._data.checkbox.selected });
  5701. };
  5702. /**
  5703. * uncheck all checked nodes (only if tie_selection in checkbox settings is false, otherwise deselect_all will be called internally)
  5704. * @name uncheck_all()
  5705. * @trigger uncheck_all.jstree
  5706. * @plugin checkbox
  5707. */
  5708. this.uncheck_all = function () {
  5709. if(this.settings.checkbox.tie_selection) { return this.deselect_all(); }
  5710. var tmp = this._data.checkbox.selected.concat([]), i, j;
  5711. for(i = 0, j = this._data.checkbox.selected.length; i < j; i++) {
  5712. if(this._model.data[this._data.checkbox.selected[i]]) {
  5713. this._model.data[this._data.checkbox.selected[i]].state.checked = false;
  5714. }
  5715. }
  5716. this._data.checkbox.selected = [];
  5717. this.element.find('.jstree-checked').removeClass('jstree-checked');
  5718. /**
  5719. * triggered when all nodes are unchecked (only if tie_selection in checkbox settings is false)
  5720. * @event
  5721. * @name uncheck_all.jstree
  5722. * @param {Object} node the previous selection
  5723. * @param {Array} selected the current selection
  5724. * @plugin checkbox
  5725. */
  5726. this.trigger('uncheck_all', { 'selected' : this._data.checkbox.selected, 'node' : tmp });
  5727. };
  5728. /**
  5729. * checks if a node is checked (if tie_selection is on in the settings this function will return the same as is_selected)
  5730. * @name is_checked(obj)
  5731. * @param {mixed} obj
  5732. * @return {Boolean}
  5733. * @plugin checkbox
  5734. */
  5735. this.is_checked = function (obj) {
  5736. if(this.settings.checkbox.tie_selection) { return this.is_selected(obj); }
  5737. obj = this.get_node(obj);
  5738. if(!obj || obj.id === $.jstree.root) { return false; }
  5739. return obj.state.checked;
  5740. };
  5741. /**
  5742. * get an array of all checked nodes (if tie_selection is on in the settings this function will return the same as get_selected)
  5743. * @name get_checked([full])
  5744. * @param {mixed} full if set to `true` the returned array will consist of the full node objects, otherwise - only IDs will be returned
  5745. * @return {Array}
  5746. * @plugin checkbox
  5747. */
  5748. this.get_checked = function (full) {
  5749. if(this.settings.checkbox.tie_selection) { return this.get_selected(full); }
  5750. return full ? $.map(this._data.checkbox.selected, $.proxy(function (i) { return this.get_node(i); }, this)) : this._data.checkbox.selected.slice();
  5751. };
  5752. /**
  5753. * 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)
  5754. * @name get_top_checked([full])
  5755. * @param {mixed} full if set to `true` the returned array will consist of the full node objects, otherwise - only IDs will be returned
  5756. * @return {Array}
  5757. * @plugin checkbox
  5758. */
  5759. this.get_top_checked = function (full) {
  5760. if(this.settings.checkbox.tie_selection) { return this.get_top_selected(full); }
  5761. var tmp = this.get_checked(true),
  5762. obj = {}, i, j, k, l;
  5763. for(i = 0, j = tmp.length; i < j; i++) {
  5764. obj[tmp[i].id] = tmp[i];
  5765. }
  5766. for(i = 0, j = tmp.length; i < j; i++) {
  5767. for(k = 0, l = tmp[i].children_d.length; k < l; k++) {
  5768. if(obj[tmp[i].children_d[k]]) {
  5769. delete obj[tmp[i].children_d[k]];
  5770. }
  5771. }
  5772. }
  5773. tmp = [];
  5774. for(i in obj) {
  5775. if(obj.hasOwnProperty(i)) {
  5776. tmp.push(i);
  5777. }
  5778. }
  5779. return full ? $.map(tmp, $.proxy(function (i) { return this.get_node(i); }, this)) : tmp;
  5780. };
  5781. /**
  5782. * 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)
  5783. * @name get_bottom_checked([full])
  5784. * @param {mixed} full if set to `true` the returned array will consist of the full node objects, otherwise - only IDs will be returned
  5785. * @return {Array}
  5786. * @plugin checkbox
  5787. */
  5788. this.get_bottom_checked = function (full) {
  5789. if(this.settings.checkbox.tie_selection) { return this.get_bottom_selected(full); }
  5790. var tmp = this.get_checked(true),
  5791. obj = [], i, j;
  5792. for(i = 0, j = tmp.length; i < j; i++) {
  5793. if(!tmp[i].children.length) {
  5794. obj.push(tmp[i].id);
  5795. }
  5796. }
  5797. return full ? $.map(obj, $.proxy(function (i) { return this.get_node(i); }, this)) : obj;
  5798. };
  5799. this.load_node = function (obj, callback) {
  5800. var k, l, i, j, c, tmp;
  5801. if(!$.isArray(obj) && !this.settings.checkbox.tie_selection) {
  5802. tmp = this.get_node(obj);
  5803. if(tmp && tmp.state.loaded) {
  5804. for(k = 0, l = tmp.children_d.length; k < l; k++) {
  5805. if(this._model.data[tmp.children_d[k]].state.checked) {
  5806. c = true;
  5807. this._data.checkbox.selected = $.vakata.array_remove_item(this._data.checkbox.selected, tmp.children_d[k]);
  5808. }
  5809. }
  5810. }
  5811. }
  5812. return parent.load_node.apply(this, arguments);
  5813. };
  5814. this.get_state = function () {
  5815. var state = parent.get_state.apply(this, arguments);
  5816. if(this.settings.checkbox.tie_selection) { return state; }
  5817. state.checkbox = this._data.checkbox.selected.slice();
  5818. return state;
  5819. };
  5820. this.set_state = function (state, callback) {
  5821. var res = parent.set_state.apply(this, arguments);
  5822. if(res && state.checkbox) {
  5823. if(!this.settings.checkbox.tie_selection) {
  5824. this.uncheck_all();
  5825. var _this = this;
  5826. $.each(state.checkbox, function (i, v) {
  5827. _this.check_node(v);
  5828. });
  5829. }
  5830. delete state.checkbox;
  5831. this.set_state(state, callback);
  5832. return false;
  5833. }
  5834. return res;
  5835. };
  5836. this.refresh = function (skip_loading, forget_state) {
  5837. if(this.settings.checkbox.tie_selection) {
  5838. this._data.checkbox.selected = [];
  5839. }
  5840. return parent.refresh.apply(this, arguments);
  5841. };
  5842. };
  5843. // include the checkbox plugin by default
  5844. // $.jstree.defaults.plugins.push("checkbox");
  5845. /**
  5846. * ### Conditionalselect plugin
  5847. *
  5848. * This plugin allows defining a callback to allow or deny node selection by user input (activate node method).
  5849. */
  5850. /**
  5851. * 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`.
  5852. * @name $.jstree.defaults.checkbox.visible
  5853. * @plugin checkbox
  5854. */
  5855. $.jstree.defaults.conditionalselect = function () { return true; };
  5856. $.jstree.plugins.conditionalselect = function (options, parent) {
  5857. // own function
  5858. this.activate_node = function (obj, e) {
  5859. if(this.settings.conditionalselect.call(this, this.get_node(obj), e)) {
  5860. return parent.activate_node.call(this, obj, e);
  5861. }
  5862. };
  5863. };
  5864. /**
  5865. * ### Contextmenu plugin
  5866. *
  5867. * Shows a context menu when a node is right-clicked.
  5868. */
  5869. /**
  5870. * stores all defaults for the contextmenu plugin
  5871. * @name $.jstree.defaults.contextmenu
  5872. * @plugin contextmenu
  5873. */
  5874. $.jstree.defaults.contextmenu = {
  5875. /**
  5876. * a boolean indicating if the node should be selected when the context menu is invoked on it. Defaults to `true`.
  5877. * @name $.jstree.defaults.contextmenu.select_node
  5878. * @plugin contextmenu
  5879. */
  5880. select_node : true,
  5881. /**
  5882. * a boolean indicating if the menu should be shown aligned with the node. Defaults to `true`, otherwise the mouse coordinates are used.
  5883. * @name $.jstree.defaults.contextmenu.show_at_node
  5884. * @plugin contextmenu
  5885. */
  5886. show_at_node : true,
  5887. /**
  5888. * 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).
  5889. *
  5890. * 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.
  5891. *
  5892. * * `separator_before` - a boolean indicating if there should be a separator before this item
  5893. * * `separator_after` - a boolean indicating if there should be a separator after this item
  5894. * * `_disabled` - a boolean indicating if this action should be disabled
  5895. * * `label` - a string - the name of the action (could be a function returning a string)
  5896. * * `title` - a string - an optional tooltip for the item
  5897. * * `action` - a function to be executed if this item is chosen, the function will receive
  5898. * * `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
  5899. * * `shortcut` - keyCode which will trigger the action if the menu is open (for example `113` for rename, which equals F2)
  5900. * * `shortcut_label` - shortcut label (like for example `F2` for rename)
  5901. * * `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
  5902. *
  5903. * @name $.jstree.defaults.contextmenu.items
  5904. * @plugin contextmenu
  5905. */
  5906. items : function (o, cb) { // Could be an object directly
  5907. return {
  5908. "create" : {
  5909. "separator_before" : false,
  5910. "separator_after" : true,
  5911. "_disabled" : false, //(this.check("create_node", data.reference, {}, "last")),
  5912. "label" : "Create",
  5913. "action" : function (data) {
  5914. var inst = $.jstree.reference(data.reference),
  5915. obj = inst.get_node(data.reference);
  5916. inst.create_node(obj, {}, "last", function (new_node) {
  5917. try {
  5918. inst.edit(new_node);
  5919. } catch (ex) {
  5920. setTimeout(function () { inst.edit(new_node); },0);
  5921. }
  5922. });
  5923. }
  5924. },
  5925. "rename" : {
  5926. "separator_before" : false,
  5927. "separator_after" : false,
  5928. "_disabled" : false, //(this.check("rename_node", data.reference, this.get_parent(data.reference), "")),
  5929. "label" : "Rename",
  5930. /*!
  5931. "shortcut" : 113,
  5932. "shortcut_label" : 'F2',
  5933. "icon" : "glyphicon glyphicon-leaf",
  5934. */
  5935. "action" : function (data) {
  5936. var inst = $.jstree.reference(data.reference),
  5937. obj = inst.get_node(data.reference);
  5938. inst.edit(obj);
  5939. }
  5940. },
  5941. "remove" : {
  5942. "separator_before" : false,
  5943. "icon" : false,
  5944. "separator_after" : false,
  5945. "_disabled" : false, //(this.check("delete_node", data.reference, this.get_parent(data.reference), "")),
  5946. "label" : "Delete",
  5947. "action" : function (data) {
  5948. var inst = $.jstree.reference(data.reference),
  5949. obj = inst.get_node(data.reference);
  5950. if(inst.is_selected(obj)) {
  5951. inst.delete_node(inst.get_selected());
  5952. }
  5953. else {
  5954. inst.delete_node(obj);
  5955. }
  5956. }
  5957. },
  5958. "ccp" : {
  5959. "separator_before" : true,
  5960. "icon" : false,
  5961. "separator_after" : false,
  5962. "label" : "Edit",
  5963. "action" : false,
  5964. "submenu" : {
  5965. "cut" : {
  5966. "separator_before" : false,
  5967. "separator_after" : false,
  5968. "label" : "Cut",
  5969. "action" : function (data) {
  5970. var inst = $.jstree.reference(data.reference),
  5971. obj = inst.get_node(data.reference);
  5972. if(inst.is_selected(obj)) {
  5973. inst.cut(inst.get_top_selected());
  5974. }
  5975. else {
  5976. inst.cut(obj);
  5977. }
  5978. }
  5979. },
  5980. "copy" : {
  5981. "separator_before" : false,
  5982. "icon" : false,
  5983. "separator_after" : false,
  5984. "label" : "Copy",
  5985. "action" : function (data) {
  5986. var inst = $.jstree.reference(data.reference),
  5987. obj = inst.get_node(data.reference);
  5988. if(inst.is_selected(obj)) {
  5989. inst.copy(inst.get_top_selected());
  5990. }
  5991. else {
  5992. inst.copy(obj);
  5993. }
  5994. }
  5995. },
  5996. "paste" : {
  5997. "separator_before" : false,
  5998. "icon" : false,
  5999. "_disabled" : function (data) {
  6000. return !$.jstree.reference(data.reference).can_paste();
  6001. },
  6002. "separator_after" : false,
  6003. "label" : "Paste",
  6004. "action" : function (data) {
  6005. var inst = $.jstree.reference(data.reference),
  6006. obj = inst.get_node(data.reference);
  6007. inst.paste(obj);
  6008. }
  6009. }
  6010. }
  6011. }
  6012. };
  6013. }
  6014. };
  6015. $.jstree.plugins.contextmenu = function (options, parent) {
  6016. this.bind = function () {
  6017. parent.bind.call(this);
  6018. var last_ts = 0, cto = null, ex, ey;
  6019. this.element
  6020. .on("init.jstree loading.jstree ready.jstree", $.proxy(function () {
  6021. this.get_container_ul().addClass('jstree-contextmenu');
  6022. }, this))
  6023. .on("contextmenu.jstree", ".jstree-anchor", $.proxy(function (e, data) {
  6024. if (e.target.tagName.toLowerCase() === 'input') {
  6025. return;
  6026. }
  6027. e.preventDefault();
  6028. last_ts = e.ctrlKey ? +new Date() : 0;
  6029. if(data || cto) {
  6030. last_ts = (+new Date()) + 10000;
  6031. }
  6032. if(cto) {
  6033. clearTimeout(cto);
  6034. }
  6035. if(!this.is_loading(e.currentTarget)) {
  6036. this.show_contextmenu(e.currentTarget, e.pageX, e.pageY, e);
  6037. }
  6038. }, this))
  6039. .on("click.jstree", ".jstree-anchor", $.proxy(function (e) {
  6040. if(this._data.contextmenu.visible && (!last_ts || (+new Date()) - last_ts > 250)) { // work around safari & macOS ctrl+click
  6041. $.vakata.context.hide();
  6042. }
  6043. last_ts = 0;
  6044. }, this))
  6045. .on("touchstart.jstree", ".jstree-anchor", function (e) {
  6046. if(!e.originalEvent || !e.originalEvent.changedTouches || !e.originalEvent.changedTouches[0]) {
  6047. return;
  6048. }
  6049. ex = e.originalEvent.changedTouches[0].clientX;
  6050. ey = e.originalEvent.changedTouches[0].clientY;
  6051. cto = setTimeout(function () {
  6052. $(e.currentTarget).trigger('contextmenu', true);
  6053. }, 750);
  6054. })
  6055. .on('touchmove.vakata.jstree', function (e) {
  6056. if(cto && e.originalEvent && e.originalEvent.changedTouches && e.originalEvent.changedTouches[0] && (Math.abs(ex - e.originalEvent.changedTouches[0].clientX) > 10 || Math.abs(ey - e.originalEvent.changedTouches[0].clientY) > 10)) {
  6057. clearTimeout(cto);
  6058. $.vakata.context.hide();
  6059. }
  6060. })
  6061. .on('touchend.vakata.jstree', function (e) {
  6062. if(cto) {
  6063. clearTimeout(cto);
  6064. }
  6065. });
  6066. /*!
  6067. if(!('oncontextmenu' in document.body) && ('ontouchstart' in document.body)) {
  6068. var el = null, tm = null;
  6069. this.element
  6070. .on("touchstart", ".jstree-anchor", function (e) {
  6071. el = e.currentTarget;
  6072. tm = +new Date();
  6073. $(document).one("touchend", function (e) {
  6074. e.target = document.elementFromPoint(e.originalEvent.targetTouches[0].pageX - window.pageXOffset, e.originalEvent.targetTouches[0].pageY - window.pageYOffset);
  6075. e.currentTarget = e.target;
  6076. tm = ((+(new Date())) - tm);
  6077. if(e.target === el && tm > 600 && tm < 1000) {
  6078. e.preventDefault();
  6079. $(el).trigger('contextmenu', e);
  6080. }
  6081. el = null;
  6082. tm = null;
  6083. });
  6084. });
  6085. }
  6086. */
  6087. $(document).on("context_hide.vakata.jstree", $.proxy(function (e, data) {
  6088. this._data.contextmenu.visible = false;
  6089. $(data.reference).removeClass('jstree-context');
  6090. }, this));
  6091. };
  6092. this.teardown = function () {
  6093. if(this._data.contextmenu.visible) {
  6094. $.vakata.context.hide();
  6095. }
  6096. parent.teardown.call(this);
  6097. };
  6098. /**
  6099. * prepare and show the context menu for a node
  6100. * @name show_contextmenu(obj [, x, y])
  6101. * @param {mixed} obj the node
  6102. * @param {Number} x the x-coordinate relative to the document to show the menu at
  6103. * @param {Number} y the y-coordinate relative to the document to show the menu at
  6104. * @param {Object} e the event if available that triggered the contextmenu
  6105. * @plugin contextmenu
  6106. * @trigger show_contextmenu.jstree
  6107. */
  6108. this.show_contextmenu = function (obj, x, y, e) {
  6109. obj = this.get_node(obj);
  6110. if(!obj || obj.id === $.jstree.root) { return false; }
  6111. var s = this.settings.contextmenu,
  6112. d = this.get_node(obj, true),
  6113. a = d.children(".jstree-anchor"),
  6114. o = false,
  6115. i = false;
  6116. if(s.show_at_node || x === undefined || y === undefined) {
  6117. o = a.offset();
  6118. x = o.left;
  6119. y = o.top + this._data.core.li_height;
  6120. }
  6121. if(this.settings.contextmenu.select_node && !this.is_selected(obj)) {
  6122. this.activate_node(obj, e);
  6123. }
  6124. i = s.items;
  6125. if($.isFunction(i)) {
  6126. i = i.call(this, obj, $.proxy(function (i) {
  6127. this._show_contextmenu(obj, x, y, i);
  6128. }, this));
  6129. }
  6130. if($.isPlainObject(i)) {
  6131. this._show_contextmenu(obj, x, y, i);
  6132. }
  6133. };
  6134. /**
  6135. * show the prepared context menu for a node
  6136. * @name _show_contextmenu(obj, x, y, i)
  6137. * @param {mixed} obj the node
  6138. * @param {Number} x the x-coordinate relative to the document to show the menu at
  6139. * @param {Number} y the y-coordinate relative to the document to show the menu at
  6140. * @param {Number} i the object of items to show
  6141. * @plugin contextmenu
  6142. * @trigger show_contextmenu.jstree
  6143. * @private
  6144. */
  6145. this._show_contextmenu = function (obj, x, y, i) {
  6146. var d = this.get_node(obj, true),
  6147. a = d.children(".jstree-anchor");
  6148. $(document).one("context_show.vakata.jstree", $.proxy(function (e, data) {
  6149. var cls = 'jstree-contextmenu jstree-' + this.get_theme() + '-contextmenu';
  6150. $(data.element).addClass(cls);
  6151. a.addClass('jstree-context');
  6152. }, this));
  6153. this._data.contextmenu.visible = true;
  6154. $.vakata.context.show(a, { 'x' : x, 'y' : y }, i);
  6155. /**
  6156. * triggered when the contextmenu is shown for a node
  6157. * @event
  6158. * @name show_contextmenu.jstree
  6159. * @param {Object} node the node
  6160. * @param {Number} x the x-coordinate of the menu relative to the document
  6161. * @param {Number} y the y-coordinate of the menu relative to the document
  6162. * @plugin contextmenu
  6163. */
  6164. this.trigger('show_contextmenu', { "node" : obj, "x" : x, "y" : y });
  6165. };
  6166. };
  6167. // contextmenu helper
  6168. (function ($) {
  6169. var right_to_left = false,
  6170. vakata_context = {
  6171. element : false,
  6172. reference : false,
  6173. position_x : 0,
  6174. position_y : 0,
  6175. items : [],
  6176. html : "",
  6177. is_visible : false
  6178. };
  6179. $.vakata.context = {
  6180. settings : {
  6181. hide_onmouseleave : 0,
  6182. icons : true
  6183. },
  6184. _trigger : function (event_name) {
  6185. $(document).triggerHandler("context_" + event_name + ".vakata", {
  6186. "reference" : vakata_context.reference,
  6187. "element" : vakata_context.element,
  6188. "position" : {
  6189. "x" : vakata_context.position_x,
  6190. "y" : vakata_context.position_y
  6191. }
  6192. });
  6193. },
  6194. _execute : function (i) {
  6195. i = vakata_context.items[i];
  6196. 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, {
  6197. "item" : i,
  6198. "reference" : vakata_context.reference,
  6199. "element" : vakata_context.element,
  6200. "position" : {
  6201. "x" : vakata_context.position_x,
  6202. "y" : vakata_context.position_y
  6203. }
  6204. }) : false;
  6205. },
  6206. _parse : function (o, is_callback) {
  6207. if(!o) { return false; }
  6208. if(!is_callback) {
  6209. vakata_context.html = "";
  6210. vakata_context.items = [];
  6211. }
  6212. var str = "",
  6213. sep = false,
  6214. tmp;
  6215. if(is_callback) { str += "<"+"ul>"; }
  6216. $.each(o, function (i, val) {
  6217. if(!val) { return true; }
  6218. vakata_context.items.push(val);
  6219. if(!sep && val.separator_before) {
  6220. str += "<"+"li class='vakata-context-separator'><"+"a href='#' " + ($.vakata.context.settings.icons ? '' : 'style="margin-left:0px;"') + ">&#160;<"+"/a><"+"/li>";
  6221. }
  6222. sep = false;
  6223. 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+"' ":'')+">";
  6224. str += "<"+"a href='#' rel='" + (vakata_context.items.length - 1) + "' " + (val.title ? "title='" + val.title + "'" : "") + ">";
  6225. if($.vakata.context.settings.icons) {
  6226. str += "<"+"i ";
  6227. if(val.icon) {
  6228. if(val.icon.indexOf("/") !== -1 || val.icon.indexOf(".") !== -1) { str += " style='background:url(\"" + val.icon + "\") center center no-repeat' "; }
  6229. else { str += " class='" + val.icon + "' "; }
  6230. }
  6231. str += "><"+"/i><"+"span class='vakata-contextmenu-sep'>&#160;<"+"/span>";
  6232. }
  6233. 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>";
  6234. if(val.submenu) {
  6235. tmp = $.vakata.context._parse(val.submenu, true);
  6236. if(tmp) { str += tmp; }
  6237. }
  6238. str += "<"+"/li>";
  6239. if(val.separator_after) {
  6240. str += "<"+"li class='vakata-context-separator'><"+"a href='#' " + ($.vakata.context.settings.icons ? '' : 'style="margin-left:0px;"') + ">&#160;<"+"/a><"+"/li>";
  6241. sep = true;
  6242. }
  6243. });
  6244. str = str.replace(/<li class\='vakata-context-separator'\><\/li\>$/,"");
  6245. if(is_callback) { str += "</ul>"; }
  6246. /**
  6247. * triggered on the document when the contextmenu is parsed (HTML is built)
  6248. * @event
  6249. * @plugin contextmenu
  6250. * @name context_parse.vakata
  6251. * @param {jQuery} reference the element that was right clicked
  6252. * @param {jQuery} element the DOM element of the menu itself
  6253. * @param {Object} position the x & y coordinates of the menu
  6254. */
  6255. if(!is_callback) { vakata_context.html = str; $.vakata.context._trigger("parse"); }
  6256. return str.length > 10 ? str : false;
  6257. },
  6258. _show_submenu : function (o) {
  6259. o = $(o);
  6260. if(!o.length || !o.children("ul").length) { return; }
  6261. var e = o.children("ul"),
  6262. xl = o.offset().left,
  6263. x = xl + o.outerWidth(),
  6264. y = o.offset().top,
  6265. w = e.width(),
  6266. h = e.height(),
  6267. dw = $(window).width() + $(window).scrollLeft(),
  6268. dh = $(window).height() + $(window).scrollTop();
  6269. // може да се спести е една проверка - дали няма някой от класовете вече нагоре
  6270. if(right_to_left) {
  6271. o[x - (w + 10 + o.outerWidth()) < 0 ? "addClass" : "removeClass"]("vakata-context-left");
  6272. }
  6273. else {
  6274. o[x + w > dw && xl > dw - x ? "addClass" : "removeClass"]("vakata-context-right");
  6275. }
  6276. if(y + h + 10 > dh) {
  6277. e.css("bottom","-1px");
  6278. }
  6279. //if does not fit - stick it to the side
  6280. if (o.hasClass('vakata-context-right')) {
  6281. if (xl < w) {
  6282. e.css("margin-right", xl - w);
  6283. }
  6284. } else {
  6285. if (dw - x < w) {
  6286. e.css("margin-left", dw - x - w);
  6287. }
  6288. }
  6289. e.show();
  6290. },
  6291. show : function (reference, position, data) {
  6292. var o, e, x, y, w, h, dw, dh, cond = true;
  6293. if(vakata_context.element && vakata_context.element.length) {
  6294. vakata_context.element.width('');
  6295. }
  6296. switch(cond) {
  6297. case (!position && !reference):
  6298. return false;
  6299. case (!!position && !!reference):
  6300. vakata_context.reference = reference;
  6301. vakata_context.position_x = position.x;
  6302. vakata_context.position_y = position.y;
  6303. break;
  6304. case (!position && !!reference):
  6305. vakata_context.reference = reference;
  6306. o = reference.offset();
  6307. vakata_context.position_x = o.left + reference.outerHeight();
  6308. vakata_context.position_y = o.top;
  6309. break;
  6310. case (!!position && !reference):
  6311. vakata_context.position_x = position.x;
  6312. vakata_context.position_y = position.y;
  6313. break;
  6314. }
  6315. if(!!reference && !data && $(reference).data('vakata_contextmenu')) {
  6316. data = $(reference).data('vakata_contextmenu');
  6317. }
  6318. if($.vakata.context._parse(data)) {
  6319. vakata_context.element.html(vakata_context.html);
  6320. }
  6321. if(vakata_context.items.length) {
  6322. vakata_context.element.appendTo(document.body);
  6323. e = vakata_context.element;
  6324. x = vakata_context.position_x;
  6325. y = vakata_context.position_y;
  6326. w = e.width();
  6327. h = e.height();
  6328. dw = $(window).width() + $(window).scrollLeft();
  6329. dh = $(window).height() + $(window).scrollTop();
  6330. if(right_to_left) {
  6331. x -= (e.outerWidth() - $(reference).outerWidth());
  6332. if(x < $(window).scrollLeft() + 20) {
  6333. x = $(window).scrollLeft() + 20;
  6334. }
  6335. }
  6336. if(x + w + 20 > dw) {
  6337. x = dw - (w + 20);
  6338. }
  6339. if(y + h + 20 > dh) {
  6340. y = dh - (h + 20);
  6341. }
  6342. vakata_context.element
  6343. .css({ "left" : x, "top" : y })
  6344. .show()
  6345. .find('a').first().focus().parent().addClass("vakata-context-hover");
  6346. vakata_context.is_visible = true;
  6347. /**
  6348. * triggered on the document when the contextmenu is shown
  6349. * @event
  6350. * @plugin contextmenu
  6351. * @name context_show.vakata
  6352. * @param {jQuery} reference the element that was right clicked
  6353. * @param {jQuery} element the DOM element of the menu itself
  6354. * @param {Object} position the x & y coordinates of the menu
  6355. */
  6356. $.vakata.context._trigger("show");
  6357. }
  6358. },
  6359. hide : function () {
  6360. if(vakata_context.is_visible) {
  6361. vakata_context.element.hide().find("ul").hide().end().find(':focus').blur().end().detach();
  6362. vakata_context.is_visible = false;
  6363. /**
  6364. * triggered on the document when the contextmenu is hidden
  6365. * @event
  6366. * @plugin contextmenu
  6367. * @name context_hide.vakata
  6368. * @param {jQuery} reference the element that was right clicked
  6369. * @param {jQuery} element the DOM element of the menu itself
  6370. * @param {Object} position the x & y coordinates of the menu
  6371. */
  6372. $.vakata.context._trigger("hide");
  6373. }
  6374. }
  6375. };
  6376. $(function () {
  6377. right_to_left = $(document.body).css("direction") === "rtl";
  6378. var to = false;
  6379. vakata_context.element = $("<ul class='vakata-context'></ul>");
  6380. vakata_context.element
  6381. .on("mouseenter", "li", function (e) {
  6382. e.stopImmediatePropagation();
  6383. if($.contains(this, e.relatedTarget)) {
  6384. // премахнато заради delegate mouseleave по-долу
  6385. // $(this).find(".vakata-context-hover").removeClass("vakata-context-hover");
  6386. return;
  6387. }
  6388. if(to) { clearTimeout(to); }
  6389. vakata_context.element.find(".vakata-context-hover").removeClass("vakata-context-hover").end();
  6390. $(this)
  6391. .siblings().find("ul").hide().end().end()
  6392. .parentsUntil(".vakata-context", "li").addBack().addClass("vakata-context-hover");
  6393. $.vakata.context._show_submenu(this);
  6394. })
  6395. // тестово - дали не натоварва?
  6396. .on("mouseleave", "li", function (e) {
  6397. if($.contains(this, e.relatedTarget)) { return; }
  6398. $(this).find(".vakata-context-hover").addBack().removeClass("vakata-context-hover");
  6399. })
  6400. .on("mouseleave", function (e) {
  6401. $(this).find(".vakata-context-hover").removeClass("vakata-context-hover");
  6402. if($.vakata.context.settings.hide_onmouseleave) {
  6403. to = setTimeout(
  6404. (function (t) {
  6405. return function () { $.vakata.context.hide(); };
  6406. }(this)), $.vakata.context.settings.hide_onmouseleave);
  6407. }
  6408. })
  6409. .on("click", "a", function (e) {
  6410. e.preventDefault();
  6411. //})
  6412. //.on("mouseup", "a", function (e) {
  6413. if(!$(this).blur().parent().hasClass("vakata-context-disabled") && $.vakata.context._execute($(this).attr("rel")) !== false) {
  6414. $.vakata.context.hide();
  6415. }
  6416. })
  6417. .on('keydown', 'a', function (e) {
  6418. var o = null;
  6419. switch(e.which) {
  6420. case 13:
  6421. case 32:
  6422. e.type = "click";
  6423. e.preventDefault();
  6424. $(e.currentTarget).trigger(e);
  6425. break;
  6426. case 37:
  6427. if(vakata_context.is_visible) {
  6428. 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();
  6429. e.stopImmediatePropagation();
  6430. e.preventDefault();
  6431. }
  6432. break;
  6433. case 38:
  6434. if(vakata_context.is_visible) {
  6435. o = vakata_context.element.find("ul:visible").addBack().last().children(".vakata-context-hover").removeClass("vakata-context-hover").prevAll("li:not(.vakata-context-separator)").first();
  6436. if(!o.length) { o = vakata_context.element.find("ul:visible").addBack().last().children("li:not(.vakata-context-separator)").last(); }
  6437. o.addClass("vakata-context-hover").children('a').focus();
  6438. e.stopImmediatePropagation();
  6439. e.preventDefault();
  6440. }
  6441. break;
  6442. case 39:
  6443. if(vakata_context.is_visible) {
  6444. 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();
  6445. e.stopImmediatePropagation();
  6446. e.preventDefault();
  6447. }
  6448. break;
  6449. case 40:
  6450. if(vakata_context.is_visible) {
  6451. o = vakata_context.element.find("ul:visible").addBack().last().children(".vakata-context-hover").removeClass("vakata-context-hover").nextAll("li:not(.vakata-context-separator)").first();
  6452. if(!o.length) { o = vakata_context.element.find("ul:visible").addBack().last().children("li:not(.vakata-context-separator)").first(); }
  6453. o.addClass("vakata-context-hover").children('a').focus();
  6454. e.stopImmediatePropagation();
  6455. e.preventDefault();
  6456. }
  6457. break;
  6458. case 27:
  6459. $.vakata.context.hide();
  6460. e.preventDefault();
  6461. break;
  6462. default:
  6463. //console.log(e.which);
  6464. break;
  6465. }
  6466. })
  6467. .on('keydown', function (e) {
  6468. e.preventDefault();
  6469. var a = vakata_context.element.find('.vakata-contextmenu-shortcut-' + e.which).parent();
  6470. if(a.parent().not('.vakata-context-disabled')) {
  6471. a.click();
  6472. }
  6473. });
  6474. $(document)
  6475. .on("mousedown.vakata.jstree", function (e) {
  6476. if(vakata_context.is_visible && vakata_context.element[0] !== e.target && !$.contains(vakata_context.element[0], e.target)) {
  6477. $.vakata.context.hide();
  6478. }
  6479. })
  6480. .on("context_show.vakata.jstree", function (e, data) {
  6481. vakata_context.element.find("li:has(ul)").children("a").addClass("vakata-context-parent");
  6482. if(right_to_left) {
  6483. vakata_context.element.addClass("vakata-context-rtl").css("direction", "rtl");
  6484. }
  6485. // also apply a RTL class?
  6486. vakata_context.element.find("ul").hide().end();
  6487. });
  6488. });
  6489. }($));
  6490. // $.jstree.defaults.plugins.push("contextmenu");
  6491. /**
  6492. * ### Drag'n'drop plugin
  6493. *
  6494. * Enables dragging and dropping of nodes in the tree, resulting in a move or copy operations.
  6495. */
  6496. /**
  6497. * stores all defaults for the drag'n'drop plugin
  6498. * @name $.jstree.defaults.dnd
  6499. * @plugin dnd
  6500. */
  6501. $.jstree.defaults.dnd = {
  6502. /**
  6503. * a boolean indicating if a copy should be possible while dragging (by pressint the meta key or Ctrl). Defaults to `true`.
  6504. * @name $.jstree.defaults.dnd.copy
  6505. * @plugin dnd
  6506. */
  6507. copy : true,
  6508. /**
  6509. * a number indicating how long a node should remain hovered while dragging to be opened. Defaults to `500`.
  6510. * @name $.jstree.defaults.dnd.open_timeout
  6511. * @plugin dnd
  6512. */
  6513. open_timeout : 500,
  6514. /**
  6515. * 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
  6516. * @name $.jstree.defaults.dnd.is_draggable
  6517. * @plugin dnd
  6518. */
  6519. is_draggable : true,
  6520. /**
  6521. * 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`
  6522. * @name $.jstree.defaults.dnd.check_while_dragging
  6523. * @plugin dnd
  6524. */
  6525. check_while_dragging : true,
  6526. /**
  6527. * a boolean indicating if nodes from this tree should only be copied with dnd (as opposed to moved), default is `false`
  6528. * @name $.jstree.defaults.dnd.always_copy
  6529. * @plugin dnd
  6530. */
  6531. always_copy : false,
  6532. /**
  6533. * 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`
  6534. * @name $.jstree.defaults.dnd.inside_pos
  6535. * @plugin dnd
  6536. */
  6537. inside_pos : 0,
  6538. /**
  6539. * 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
  6540. * @name $.jstree.defaults.dnd.drag_selection
  6541. * @plugin dnd
  6542. */
  6543. drag_selection : true,
  6544. /**
  6545. * 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.
  6546. * @name $.jstree.defaults.dnd.touch
  6547. * @plugin dnd
  6548. */
  6549. touch : true,
  6550. /**
  6551. * 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.
  6552. * @name $.jstree.defaults.dnd.large_drop_target
  6553. * @plugin dnd
  6554. */
  6555. large_drop_target : false,
  6556. /**
  6557. * 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".
  6558. * @name $.jstree.defaults.dnd.large_drag_target
  6559. * @plugin dnd
  6560. */
  6561. large_drag_target : false,
  6562. /**
  6563. * controls whether use HTML5 dnd api instead of classical. That will allow better integration of dnd events with other HTML5 controls.
  6564. * @reference http://caniuse.com/#feat=dragndrop
  6565. * @name $.jstree.defaults.dnd.use_html5
  6566. * @plugin dnd
  6567. */
  6568. use_html5: false
  6569. };
  6570. var drg, elm;
  6571. // TODO: now check works by checking for each node individually, how about max_children, unique, etc?
  6572. $.jstree.plugins.dnd = function (options, parent) {
  6573. this.init = function (el, options) {
  6574. parent.init.call(this, el, options);
  6575. this.settings.dnd.use_html5 = this.settings.dnd.use_html5 && ('draggable' in document.createElement('span'));
  6576. };
  6577. this.bind = function () {
  6578. parent.bind.call(this);
  6579. this.element
  6580. .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) {
  6581. if(this.settings.dnd.large_drag_target && $(e.target).closest('.jstree-node')[0] !== e.currentTarget) {
  6582. return true;
  6583. }
  6584. if(e.type === "touchstart" && (!this.settings.dnd.touch || (this.settings.dnd.touch === 'selected' && !$(e.currentTarget).closest('.jstree-node').children('.jstree-anchor').hasClass('jstree-clicked')))) {
  6585. return true;
  6586. }
  6587. var obj = this.get_node(e.target),
  6588. mlt = this.is_selected(obj) && this.settings.dnd.drag_selection ? this.get_top_selected().length : 1,
  6589. txt = (mlt > 1 ? mlt + ' ' + this.get_string('nodes') : this.get_text(e.currentTarget));
  6590. if(this.settings.core.force_text) {
  6591. txt = $.vakata.html.escape(txt);
  6592. }
  6593. if(obj && obj.id && obj.id !== $.jstree.root && (e.which === 1 || e.type === "touchstart" || e.type === "dragstart") &&
  6594. (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)))
  6595. ) {
  6596. drg = { 'jstree' : true, 'origin' : this, 'obj' : this.get_node(obj,true), 'nodes' : mlt > 1 ? this.get_top_selected() : [obj.id] };
  6597. elm = e.currentTarget;
  6598. if (this.settings.dnd.use_html5) {
  6599. $.vakata.dnd._trigger('start', e, { 'helper': $(), 'element': elm, 'data': drg });
  6600. } else {
  6601. this.element.trigger('mousedown.jstree');
  6602. 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>');
  6603. }
  6604. }
  6605. }, this));
  6606. if (this.settings.dnd.use_html5) {
  6607. this.element
  6608. .on('dragover.jstree', function (e) {
  6609. e.preventDefault();
  6610. $.vakata.dnd._trigger('move', e, { 'helper': $(), 'element': elm, 'data': drg });
  6611. return false;
  6612. })
  6613. //.on('dragenter.jstree', this.settings.dnd.large_drop_target ? '.jstree-node' : '.jstree-anchor', $.proxy(function (e) {
  6614. // e.preventDefault();
  6615. // $.vakata.dnd._trigger('move', e, { 'helper': $(), 'element': elm, 'data': drg });
  6616. // return false;
  6617. // }, this))
  6618. .on('drop.jstree', $.proxy(function (e) {
  6619. e.preventDefault();
  6620. $.vakata.dnd._trigger('stop', e, { 'helper': $(), 'element': elm, 'data': drg });
  6621. return false;
  6622. }, this));
  6623. }
  6624. };
  6625. this.redraw_node = function(obj, deep, callback, force_render) {
  6626. obj = parent.redraw_node.apply(this, arguments);
  6627. if (obj && this.settings.dnd.use_html5) {
  6628. if (this.settings.dnd.large_drag_target) {
  6629. obj.setAttribute('draggable', true);
  6630. } else {
  6631. var i, j, tmp = null;
  6632. for(i = 0, j = obj.childNodes.length; i < j; i++) {
  6633. if(obj.childNodes[i] && obj.childNodes[i].className && obj.childNodes[i].className.indexOf("jstree-anchor") !== -1) {
  6634. tmp = obj.childNodes[i];
  6635. break;
  6636. }
  6637. }
  6638. if(tmp) {
  6639. tmp.setAttribute('draggable', true);
  6640. }
  6641. }
  6642. }
  6643. return obj;
  6644. };
  6645. };
  6646. $(function() {
  6647. // bind only once for all instances
  6648. var lastmv = false,
  6649. laster = false,
  6650. lastev = false,
  6651. opento = false,
  6652. marker = $('<div id="jstree-marker">&#160;</div>').hide(); //.appendTo('body');
  6653. $(document)
  6654. .on('dragover.vakata.jstree', function (e) {
  6655. if (elm) {
  6656. $.vakata.dnd._trigger('move', e, { 'helper': $(), 'element': elm, 'data': drg });
  6657. }
  6658. })
  6659. .on('drop.vakata.jstree', function (e) {
  6660. if (elm) {
  6661. $.vakata.dnd._trigger('stop', e, { 'helper': $(), 'element': elm, 'data': drg });
  6662. elm = null;
  6663. drg = null;
  6664. }
  6665. })
  6666. .on('dnd_start.vakata.jstree', function (e, data) {
  6667. lastmv = false;
  6668. lastev = false;
  6669. if(!data || !data.data || !data.data.jstree) { return; }
  6670. marker.appendTo(document.body); //.show();
  6671. })
  6672. .on('dnd_move.vakata.jstree', function (e, data) {
  6673. var isDifferentNode = data.event.target !== lastev.target;
  6674. if(opento) {
  6675. if (!data.event || data.event.type !== 'dragover' || isDifferentNode) {
  6676. clearTimeout(opento);
  6677. }
  6678. }
  6679. if(!data || !data.data || !data.data.jstree) { return; }
  6680. // if we are hovering the marker image do nothing (can happen on "inside" drags)
  6681. if(data.event.target.id && data.event.target.id === 'jstree-marker') {
  6682. return;
  6683. }
  6684. lastev = data.event;
  6685. var ins = $.jstree.reference(data.event.target),
  6686. ref = false,
  6687. off = false,
  6688. rel = false,
  6689. tmp, l, t, h, p, i, o, ok, t1, t2, op, ps, pr, ip, tm, is_copy, pn;
  6690. // if we are over an instance
  6691. if(ins && ins._data && ins._data.dnd) {
  6692. marker.attr('class', 'jstree-' + ins.get_theme() + ( ins.settings.core.themes.responsive ? ' jstree-dnd-responsive' : '' ));
  6693. is_copy = data.data.origin && (data.data.origin.settings.dnd.always_copy || (data.data.origin.settings.dnd.copy && (data.event.metaKey || data.event.ctrlKey)));
  6694. data.helper
  6695. .children().attr('class', 'jstree-' + ins.get_theme() + ' jstree-' + ins.get_theme() + '-' + ins.get_theme_variant() + ' ' + ( ins.settings.core.themes.responsive ? ' jstree-dnd-responsive' : '' ))
  6696. .find('.jstree-copy').first()[ is_copy ? 'show' : 'hide' ]();
  6697. // if are hovering the container itself add a new root node
  6698. //console.log(data.event);
  6699. if( (data.event.target === ins.element[0] || data.event.target === ins.get_container_ul()[0]) && ins.get_container_ul().children().length === 0) {
  6700. ok = true;
  6701. for(t1 = 0, t2 = data.data.nodes.length; t1 < t2; t1++) {
  6702. 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) });
  6703. if(!ok) { break; }
  6704. }
  6705. if(ok) {
  6706. lastmv = { 'ins' : ins, 'par' : $.jstree.root, 'pos' : 'last' };
  6707. marker.hide();
  6708. data.helper.find('.jstree-icon').first().removeClass('jstree-er').addClass('jstree-ok');
  6709. if (data.event.originalEvent && data.event.originalEvent.dataTransfer) {
  6710. data.event.originalEvent.dataTransfer.dropEffect = is_copy ? 'copy' : 'move';
  6711. }
  6712. return;
  6713. }
  6714. }
  6715. else {
  6716. // if we are hovering a tree node
  6717. ref = ins.settings.dnd.large_drop_target ? $(data.event.target).closest('.jstree-node').children('.jstree-anchor') : $(data.event.target).closest('.jstree-anchor');
  6718. if(ref && ref.length && ref.parent().is('.jstree-closed, .jstree-open, .jstree-leaf')) {
  6719. off = ref.offset();
  6720. rel = (data.event.pageY !== undefined ? data.event.pageY : data.event.originalEvent.pageY) - off.top;
  6721. h = ref.outerHeight();
  6722. if(rel < h / 3) {
  6723. o = ['b', 'i', 'a'];
  6724. }
  6725. else if(rel > h - h / 3) {
  6726. o = ['a', 'i', 'b'];
  6727. }
  6728. else {
  6729. o = rel > h / 2 ? ['i', 'a', 'b'] : ['i', 'b', 'a'];
  6730. }
  6731. $.each(o, function (j, v) {
  6732. switch(v) {
  6733. case 'b':
  6734. l = off.left - 6;
  6735. t = off.top;
  6736. p = ins.get_parent(ref);
  6737. i = ref.parent().index();
  6738. break;
  6739. case 'i':
  6740. ip = ins.settings.dnd.inside_pos;
  6741. tm = ins.get_node(ref.parent());
  6742. l = off.left - 2;
  6743. t = off.top + h / 2 + 1;
  6744. p = tm.id;
  6745. i = ip === 'first' ? 0 : (ip === 'last' ? tm.children.length : Math.min(ip, tm.children.length));
  6746. break;
  6747. case 'a':
  6748. l = off.left - 6;
  6749. t = off.top + h;
  6750. p = ins.get_parent(ref);
  6751. i = ref.parent().index() + 1;
  6752. break;
  6753. }
  6754. ok = true;
  6755. for(t1 = 0, t2 = data.data.nodes.length; t1 < t2; t1++) {
  6756. 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";
  6757. ps = i;
  6758. if(op === "move_node" && v === 'a' && (data.data.origin && data.data.origin === ins) && p === ins.get_parent(data.data.nodes[t1])) {
  6759. pr = ins.get_node(p);
  6760. if(ps > $.inArray(data.data.nodes[t1], pr.children)) {
  6761. ps -= 1;
  6762. }
  6763. }
  6764. 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) }) );
  6765. if(!ok) {
  6766. if(ins && ins.last_error) { laster = ins.last_error(); }
  6767. break;
  6768. }
  6769. }
  6770. if(v === 'i' && ref.parent().is('.jstree-closed') && ins.settings.dnd.open_timeout) {
  6771. if (!data.event || data.event.type !== 'dragover' || isDifferentNode) {
  6772. if (opento) { clearTimeout(opento); }
  6773. opento = setTimeout((function (x, z) { return function () { x.open_node(z); }; }(ins, ref)), ins.settings.dnd.open_timeout);
  6774. }
  6775. }
  6776. if(ok) {
  6777. pn = ins.get_node(p, true);
  6778. if (!pn.hasClass('.jstree-dnd-parent')) {
  6779. $('.jstree-dnd-parent').removeClass('jstree-dnd-parent');
  6780. pn.addClass('jstree-dnd-parent');
  6781. }
  6782. lastmv = { 'ins' : ins, 'par' : p, 'pos' : v === 'i' && ip === 'last' && i === 0 && !ins.is_loaded(tm) ? 'last' : i };
  6783. marker.css({ 'left' : l + 'px', 'top' : t + 'px' }).show();
  6784. data.helper.find('.jstree-icon').first().removeClass('jstree-er').addClass('jstree-ok');
  6785. if (data.event.originalEvent && data.event.originalEvent.dataTransfer) {
  6786. data.event.originalEvent.dataTransfer.dropEffect = is_copy ? 'copy' : 'move';
  6787. }
  6788. laster = {};
  6789. o = true;
  6790. return false;
  6791. }
  6792. });
  6793. if(o === true) { return; }
  6794. }
  6795. }
  6796. }
  6797. $('.jstree-dnd-parent').removeClass('jstree-dnd-parent');
  6798. lastmv = false;
  6799. data.helper.find('.jstree-icon').removeClass('jstree-ok').addClass('jstree-er');
  6800. if (data.event.originalEvent && data.event.originalEvent.dataTransfer) {
  6801. //data.event.originalEvent.dataTransfer.dropEffect = 'none';
  6802. }
  6803. marker.hide();
  6804. })
  6805. .on('dnd_scroll.vakata.jstree', function (e, data) {
  6806. if(!data || !data.data || !data.data.jstree) { return; }
  6807. marker.hide();
  6808. lastmv = false;
  6809. lastev = false;
  6810. data.helper.find('.jstree-icon').first().removeClass('jstree-ok').addClass('jstree-er');
  6811. })
  6812. .on('dnd_stop.vakata.jstree', function (e, data) {
  6813. $('.jstree-dnd-parent').removeClass('jstree-dnd-parent');
  6814. if(opento) { clearTimeout(opento); }
  6815. if(!data || !data.data || !data.data.jstree) { return; }
  6816. marker.hide().detach();
  6817. var i, j, nodes = [];
  6818. if(lastmv) {
  6819. for(i = 0, j = data.data.nodes.length; i < j; i++) {
  6820. nodes[i] = data.data.origin ? data.data.origin.get_node(data.data.nodes[i]) : data.data.nodes[i];
  6821. }
  6822. 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);
  6823. }
  6824. else {
  6825. i = $(data.event.target).closest('.jstree');
  6826. if(i.length && laster && laster.error && laster.error === 'check') {
  6827. i = i.jstree(true);
  6828. if(i) {
  6829. i.settings.core.error.call(this, laster);
  6830. }
  6831. }
  6832. }
  6833. lastev = false;
  6834. lastmv = false;
  6835. })
  6836. .on('keyup.jstree keydown.jstree', function (e, data) {
  6837. data = $.vakata.dnd._get();
  6838. if(data && data.data && data.data.jstree) {
  6839. if (e.type === "keyup" && e.which === 27) {
  6840. if (opento) { clearTimeout(opento); }
  6841. lastmv = false;
  6842. laster = false;
  6843. lastev = false;
  6844. opento = false;
  6845. marker.hide().detach();
  6846. $.vakata.dnd._clean();
  6847. } else {
  6848. 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' ]();
  6849. if(lastev) {
  6850. lastev.metaKey = e.metaKey;
  6851. lastev.ctrlKey = e.ctrlKey;
  6852. $.vakata.dnd._trigger('move', lastev);
  6853. }
  6854. }
  6855. }
  6856. });
  6857. });
  6858. // helpers
  6859. (function ($) {
  6860. $.vakata.html = {
  6861. div : $('<div />'),
  6862. escape : function (str) {
  6863. return $.vakata.html.div.text(str).html();
  6864. },
  6865. strip : function (str) {
  6866. return $.vakata.html.div.empty().append($.parseHTML(str)).text();
  6867. }
  6868. };
  6869. // private variable
  6870. var vakata_dnd = {
  6871. element : false,
  6872. target : false,
  6873. is_down : false,
  6874. is_drag : false,
  6875. helper : false,
  6876. helper_w: 0,
  6877. data : false,
  6878. init_x : 0,
  6879. init_y : 0,
  6880. scroll_l: 0,
  6881. scroll_t: 0,
  6882. scroll_e: false,
  6883. scroll_i: false,
  6884. is_touch: false
  6885. };
  6886. $.vakata.dnd = {
  6887. settings : {
  6888. scroll_speed : 10,
  6889. scroll_proximity : 20,
  6890. helper_left : 5,
  6891. helper_top : 10,
  6892. threshold : 5,
  6893. threshold_touch : 10
  6894. },
  6895. _trigger : function (event_name, e, data) {
  6896. if (data === undefined) {
  6897. data = $.vakata.dnd._get();
  6898. }
  6899. data.event = e;
  6900. $(document).triggerHandler("dnd_" + event_name + ".vakata", data);
  6901. },
  6902. _get : function () {
  6903. return {
  6904. "data" : vakata_dnd.data,
  6905. "element" : vakata_dnd.element,
  6906. "helper" : vakata_dnd.helper
  6907. };
  6908. },
  6909. _clean : function () {
  6910. if(vakata_dnd.helper) { vakata_dnd.helper.remove(); }
  6911. if(vakata_dnd.scroll_i) { clearInterval(vakata_dnd.scroll_i); vakata_dnd.scroll_i = false; }
  6912. vakata_dnd = {
  6913. element : false,
  6914. target : false,
  6915. is_down : false,
  6916. is_drag : false,
  6917. helper : false,
  6918. helper_w: 0,
  6919. data : false,
  6920. init_x : 0,
  6921. init_y : 0,
  6922. scroll_l: 0,
  6923. scroll_t: 0,
  6924. scroll_e: false,
  6925. scroll_i: false,
  6926. is_touch: false
  6927. };
  6928. $(document).off("mousemove.vakata.jstree touchmove.vakata.jstree", $.vakata.dnd.drag);
  6929. $(document).off("mouseup.vakata.jstree touchend.vakata.jstree", $.vakata.dnd.stop);
  6930. },
  6931. _scroll : function (init_only) {
  6932. if(!vakata_dnd.scroll_e || (!vakata_dnd.scroll_l && !vakata_dnd.scroll_t)) {
  6933. if(vakata_dnd.scroll_i) { clearInterval(vakata_dnd.scroll_i); vakata_dnd.scroll_i = false; }
  6934. return false;
  6935. }
  6936. if(!vakata_dnd.scroll_i) {
  6937. vakata_dnd.scroll_i = setInterval($.vakata.dnd._scroll, 100);
  6938. return false;
  6939. }
  6940. if(init_only === true) { return false; }
  6941. var i = vakata_dnd.scroll_e.scrollTop(),
  6942. j = vakata_dnd.scroll_e.scrollLeft();
  6943. vakata_dnd.scroll_e.scrollTop(i + vakata_dnd.scroll_t * $.vakata.dnd.settings.scroll_speed);
  6944. vakata_dnd.scroll_e.scrollLeft(j + vakata_dnd.scroll_l * $.vakata.dnd.settings.scroll_speed);
  6945. if(i !== vakata_dnd.scroll_e.scrollTop() || j !== vakata_dnd.scroll_e.scrollLeft()) {
  6946. /**
  6947. * triggered on the document when a drag causes an element to scroll
  6948. * @event
  6949. * @plugin dnd
  6950. * @name dnd_scroll.vakata
  6951. * @param {Mixed} data any data supplied with the call to $.vakata.dnd.start
  6952. * @param {DOM} element the DOM element being dragged
  6953. * @param {jQuery} helper the helper shown next to the mouse
  6954. * @param {jQuery} event the element that is scrolling
  6955. */
  6956. $.vakata.dnd._trigger("scroll", vakata_dnd.scroll_e);
  6957. }
  6958. },
  6959. start : function (e, data, html) {
  6960. if(e.type === "touchstart" && e.originalEvent && e.originalEvent.changedTouches && e.originalEvent.changedTouches[0]) {
  6961. e.pageX = e.originalEvent.changedTouches[0].pageX;
  6962. e.pageY = e.originalEvent.changedTouches[0].pageY;
  6963. e.target = document.elementFromPoint(e.originalEvent.changedTouches[0].pageX - window.pageXOffset, e.originalEvent.changedTouches[0].pageY - window.pageYOffset);
  6964. }
  6965. if(vakata_dnd.is_drag) { $.vakata.dnd.stop({}); }
  6966. try {
  6967. e.currentTarget.unselectable = "on";
  6968. e.currentTarget.onselectstart = function() { return false; };
  6969. if(e.currentTarget.style) {
  6970. e.currentTarget.style.touchAction = "none";
  6971. e.currentTarget.style.msTouchAction = "none";
  6972. e.currentTarget.style.MozUserSelect = "none";
  6973. }
  6974. } catch(ignore) { }
  6975. vakata_dnd.init_x = e.pageX;
  6976. vakata_dnd.init_y = e.pageY;
  6977. vakata_dnd.data = data;
  6978. vakata_dnd.is_down = true;
  6979. vakata_dnd.element = e.currentTarget;
  6980. vakata_dnd.target = e.target;
  6981. vakata_dnd.is_touch = e.type === "touchstart";
  6982. if(html !== false) {
  6983. vakata_dnd.helper = $("<div id='vakata-dnd'></div>").html(html).css({
  6984. "display" : "block",
  6985. "margin" : "0",
  6986. "padding" : "0",
  6987. "position" : "absolute",
  6988. "top" : "-2000px",
  6989. "lineHeight" : "16px",
  6990. "zIndex" : "10000"
  6991. });
  6992. }
  6993. $(document).on("mousemove.vakata.jstree touchmove.vakata.jstree", $.vakata.dnd.drag);
  6994. $(document).on("mouseup.vakata.jstree touchend.vakata.jstree", $.vakata.dnd.stop);
  6995. return false;
  6996. },
  6997. drag : function (e) {
  6998. if(e.type === "touchmove" && e.originalEvent && e.originalEvent.changedTouches && e.originalEvent.changedTouches[0]) {
  6999. e.pageX = e.originalEvent.changedTouches[0].pageX;
  7000. e.pageY = e.originalEvent.changedTouches[0].pageY;
  7001. e.target = document.elementFromPoint(e.originalEvent.changedTouches[0].pageX - window.pageXOffset, e.originalEvent.changedTouches[0].pageY - window.pageYOffset);
  7002. }
  7003. if(!vakata_dnd.is_down) { return; }
  7004. if(!vakata_dnd.is_drag) {
  7005. if(
  7006. Math.abs(e.pageX - vakata_dnd.init_x) > (vakata_dnd.is_touch ? $.vakata.dnd.settings.threshold_touch : $.vakata.dnd.settings.threshold) ||
  7007. Math.abs(e.pageY - vakata_dnd.init_y) > (vakata_dnd.is_touch ? $.vakata.dnd.settings.threshold_touch : $.vakata.dnd.settings.threshold)
  7008. ) {
  7009. if(vakata_dnd.helper) {
  7010. vakata_dnd.helper.appendTo(document.body);
  7011. vakata_dnd.helper_w = vakata_dnd.helper.outerWidth();
  7012. }
  7013. vakata_dnd.is_drag = true;
  7014. $(vakata_dnd.target).one('click.vakata', false);
  7015. /**
  7016. * triggered on the document when a drag starts
  7017. * @event
  7018. * @plugin dnd
  7019. * @name dnd_start.vakata
  7020. * @param {Mixed} data any data supplied with the call to $.vakata.dnd.start
  7021. * @param {DOM} element the DOM element being dragged
  7022. * @param {jQuery} helper the helper shown next to the mouse
  7023. * @param {Object} event the event that caused the start (probably mousemove)
  7024. */
  7025. $.vakata.dnd._trigger("start", e);
  7026. }
  7027. else { return; }
  7028. }
  7029. var d = false, w = false,
  7030. dh = false, wh = false,
  7031. dw = false, ww = false,
  7032. dt = false, dl = false,
  7033. ht = false, hl = false;
  7034. vakata_dnd.scroll_t = 0;
  7035. vakata_dnd.scroll_l = 0;
  7036. vakata_dnd.scroll_e = false;
  7037. $($(e.target).parentsUntil("body").addBack().get().reverse())
  7038. .filter(function () {
  7039. return (/^auto|scroll$/).test($(this).css("overflow")) &&
  7040. (this.scrollHeight > this.offsetHeight || this.scrollWidth > this.offsetWidth);
  7041. })
  7042. .each(function () {
  7043. var t = $(this), o = t.offset();
  7044. if(this.scrollHeight > this.offsetHeight) {
  7045. if(o.top + t.height() - e.pageY < $.vakata.dnd.settings.scroll_proximity) { vakata_dnd.scroll_t = 1; }
  7046. if(e.pageY - o.top < $.vakata.dnd.settings.scroll_proximity) { vakata_dnd.scroll_t = -1; }
  7047. }
  7048. if(this.scrollWidth > this.offsetWidth) {
  7049. if(o.left + t.width() - e.pageX < $.vakata.dnd.settings.scroll_proximity) { vakata_dnd.scroll_l = 1; }
  7050. if(e.pageX - o.left < $.vakata.dnd.settings.scroll_proximity) { vakata_dnd.scroll_l = -1; }
  7051. }
  7052. if(vakata_dnd.scroll_t || vakata_dnd.scroll_l) {
  7053. vakata_dnd.scroll_e = $(this);
  7054. return false;
  7055. }
  7056. });
  7057. if(!vakata_dnd.scroll_e) {
  7058. d = $(document); w = $(window);
  7059. dh = d.height(); wh = w.height();
  7060. dw = d.width(); ww = w.width();
  7061. dt = d.scrollTop(); dl = d.scrollLeft();
  7062. if(dh > wh && e.pageY - dt < $.vakata.dnd.settings.scroll_proximity) { vakata_dnd.scroll_t = -1; }
  7063. if(dh > wh && wh - (e.pageY - dt) < $.vakata.dnd.settings.scroll_proximity) { vakata_dnd.scroll_t = 1; }
  7064. if(dw > ww && e.pageX - dl < $.vakata.dnd.settings.scroll_proximity) { vakata_dnd.scroll_l = -1; }
  7065. if(dw > ww && ww - (e.pageX - dl) < $.vakata.dnd.settings.scroll_proximity) { vakata_dnd.scroll_l = 1; }
  7066. if(vakata_dnd.scroll_t || vakata_dnd.scroll_l) {
  7067. vakata_dnd.scroll_e = d;
  7068. }
  7069. }
  7070. if(vakata_dnd.scroll_e) { $.vakata.dnd._scroll(true); }
  7071. if(vakata_dnd.helper) {
  7072. ht = parseInt(e.pageY + $.vakata.dnd.settings.helper_top, 10);
  7073. hl = parseInt(e.pageX + $.vakata.dnd.settings.helper_left, 10);
  7074. if(dh && ht + 25 > dh) { ht = dh - 50; }
  7075. if(dw && hl + vakata_dnd.helper_w > dw) { hl = dw - (vakata_dnd.helper_w + 2); }
  7076. vakata_dnd.helper.css({
  7077. left : hl + "px",
  7078. top : ht + "px"
  7079. });
  7080. }
  7081. /**
  7082. * triggered on the document when a drag is in progress
  7083. * @event
  7084. * @plugin dnd
  7085. * @name dnd_move.vakata
  7086. * @param {Mixed} data any data supplied with the call to $.vakata.dnd.start
  7087. * @param {DOM} element the DOM element being dragged
  7088. * @param {jQuery} helper the helper shown next to the mouse
  7089. * @param {Object} event the event that caused this to trigger (most likely mousemove)
  7090. */
  7091. $.vakata.dnd._trigger("move", e);
  7092. return false;
  7093. },
  7094. stop : function (e) {
  7095. if(e.type === "touchend" && e.originalEvent && e.originalEvent.changedTouches && e.originalEvent.changedTouches[0]) {
  7096. e.pageX = e.originalEvent.changedTouches[0].pageX;
  7097. e.pageY = e.originalEvent.changedTouches[0].pageY;
  7098. e.target = document.elementFromPoint(e.originalEvent.changedTouches[0].pageX - window.pageXOffset, e.originalEvent.changedTouches[0].pageY - window.pageYOffset);
  7099. }
  7100. if(vakata_dnd.is_drag) {
  7101. /**
  7102. * triggered on the document when a drag stops (the dragged element is dropped)
  7103. * @event
  7104. * @plugin dnd
  7105. * @name dnd_stop.vakata
  7106. * @param {Mixed} data any data supplied with the call to $.vakata.dnd.start
  7107. * @param {DOM} element the DOM element being dragged
  7108. * @param {jQuery} helper the helper shown next to the mouse
  7109. * @param {Object} event the event that caused the stop
  7110. */
  7111. if (e.target !== vakata_dnd.target) {
  7112. $(vakata_dnd.target).off('click.vakata');
  7113. }
  7114. $.vakata.dnd._trigger("stop", e);
  7115. }
  7116. else {
  7117. if(e.type === "touchend" && e.target === vakata_dnd.target) {
  7118. var to = setTimeout(function () { $(e.target).click(); }, 100);
  7119. $(e.target).one('click', function() { if(to) { clearTimeout(to); } });
  7120. }
  7121. }
  7122. $.vakata.dnd._clean();
  7123. return false;
  7124. }
  7125. };
  7126. }($));
  7127. // include the dnd plugin by default
  7128. // $.jstree.defaults.plugins.push("dnd");
  7129. /**
  7130. * ### Massload plugin
  7131. *
  7132. * Adds massload functionality to jsTree, so that multiple nodes can be loaded in a single request (only useful with lazy loading).
  7133. */
  7134. /**
  7135. * massload configuration
  7136. *
  7137. * It is possible to set this to a standard jQuery-like AJAX config.
  7138. * 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.
  7139. *
  7140. * 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.
  7141. *
  7142. * 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.
  7143. *
  7144. * {
  7145. * "id1" : [{ "text" : "Child of ID1", "id" : "c1" }, { "text" : "Another child of ID1", "id" : "c2" }],
  7146. * "id2" : [{ "text" : "Child of ID2", "id" : "c3" }]
  7147. * }
  7148. *
  7149. * @name $.jstree.defaults.massload
  7150. * @plugin massload
  7151. */
  7152. $.jstree.defaults.massload = null;
  7153. $.jstree.plugins.massload = function (options, parent) {
  7154. this.init = function (el, options) {
  7155. this._data.massload = {};
  7156. parent.init.call(this, el, options);
  7157. };
  7158. this._load_nodes = function (nodes, callback, is_callback, force_reload) {
  7159. var s = this.settings.massload,
  7160. nodesString = JSON.stringify(nodes),
  7161. toLoad = [],
  7162. m = this._model.data,
  7163. i, j, dom;
  7164. if (!is_callback) {
  7165. for(i = 0, j = nodes.length; i < j; i++) {
  7166. if(!m[nodes[i]] || ( (!m[nodes[i]].state.loaded && !m[nodes[i]].state.failed) || force_reload) ) {
  7167. toLoad.push(nodes[i]);
  7168. dom = this.get_node(nodes[i], true);
  7169. if (dom && dom.length) {
  7170. dom.addClass("jstree-loading").attr('aria-busy',true);
  7171. }
  7172. }
  7173. }
  7174. this._data.massload = {};
  7175. if (toLoad.length) {
  7176. if($.isFunction(s)) {
  7177. return s.call(this, toLoad, $.proxy(function (data) {
  7178. var i, j;
  7179. if(data) {
  7180. for(i in data) {
  7181. if(data.hasOwnProperty(i)) {
  7182. this._data.massload[i] = data[i];
  7183. }
  7184. }
  7185. }
  7186. for(i = 0, j = nodes.length; i < j; i++) {
  7187. dom = this.get_node(nodes[i], true);
  7188. if (dom && dom.length) {
  7189. dom.removeClass("jstree-loading").attr('aria-busy',false);
  7190. }
  7191. }
  7192. parent._load_nodes.call(this, nodes, callback, is_callback, force_reload);
  7193. }, this));
  7194. }
  7195. if(typeof s === 'object' && s && s.url) {
  7196. s = $.extend(true, {}, s);
  7197. if($.isFunction(s.url)) {
  7198. s.url = s.url.call(this, toLoad);
  7199. }
  7200. if($.isFunction(s.data)) {
  7201. s.data = s.data.call(this, toLoad);
  7202. }
  7203. return $.ajax(s)
  7204. .done($.proxy(function (data,t,x) {
  7205. var i, j;
  7206. if(data) {
  7207. for(i in data) {
  7208. if(data.hasOwnProperty(i)) {
  7209. this._data.massload[i] = data[i];
  7210. }
  7211. }
  7212. }
  7213. for(i = 0, j = nodes.length; i < j; i++) {
  7214. dom = this.get_node(nodes[i], true);
  7215. if (dom && dom.length) {
  7216. dom.removeClass("jstree-loading").attr('aria-busy',false);
  7217. }
  7218. }
  7219. parent._load_nodes.call(this, nodes, callback, is_callback, force_reload);
  7220. }, this))
  7221. .fail($.proxy(function (f) {
  7222. parent._load_nodes.call(this, nodes, callback, is_callback, force_reload);
  7223. }, this));
  7224. }
  7225. }
  7226. }
  7227. return parent._load_nodes.call(this, nodes, callback, is_callback, force_reload);
  7228. };
  7229. this._load_node = function (obj, callback) {
  7230. var data = this._data.massload[obj.id],
  7231. rslt = null, dom;
  7232. if(data) {
  7233. rslt = this[typeof data === 'string' ? '_append_html_data' : '_append_json_data'](
  7234. obj,
  7235. typeof data === 'string' ? $($.parseHTML(data)).filter(function () { return this.nodeType !== 3; }) : data,
  7236. function (status) { callback.call(this, status); }
  7237. );
  7238. dom = this.get_node(obj.id, true);
  7239. if (dom && dom.length) {
  7240. dom.removeClass("jstree-loading").attr('aria-busy',false);
  7241. }
  7242. delete this._data.massload[obj.id];
  7243. return rslt;
  7244. }
  7245. return parent._load_node.call(this, obj, callback);
  7246. };
  7247. };
  7248. /**
  7249. * ### Search plugin
  7250. *
  7251. * Adds search functionality to jsTree.
  7252. */
  7253. /**
  7254. * stores all defaults for the search plugin
  7255. * @name $.jstree.defaults.search
  7256. * @plugin search
  7257. */
  7258. $.jstree.defaults.search = {
  7259. /**
  7260. * a jQuery-like AJAX config, which jstree uses if a server should be queried for results.
  7261. *
  7262. * 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.
  7263. * 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
  7264. * @name $.jstree.defaults.search.ajax
  7265. * @plugin search
  7266. */
  7267. ajax : false,
  7268. /**
  7269. * Indicates if the search should be fuzzy or not (should `chnd3` match `child node 3`). Default is `false`.
  7270. * @name $.jstree.defaults.search.fuzzy
  7271. * @plugin search
  7272. */
  7273. fuzzy : false,
  7274. /**
  7275. * Indicates if the search should be case sensitive. Default is `false`.
  7276. * @name $.jstree.defaults.search.case_sensitive
  7277. * @plugin search
  7278. */
  7279. case_sensitive : false,
  7280. /**
  7281. * 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).
  7282. * This setting can be changed at runtime when calling the search method. Default is `false`.
  7283. * @name $.jstree.defaults.search.show_only_matches
  7284. * @plugin search
  7285. */
  7286. show_only_matches : false,
  7287. /**
  7288. * Indicates if the children of matched element are shown (when show_only_matches is true)
  7289. * This setting can be changed at runtime when calling the search method. Default is `false`.
  7290. * @name $.jstree.defaults.search.show_only_matches_children
  7291. * @plugin search
  7292. */
  7293. show_only_matches_children : false,
  7294. /**
  7295. * 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`.
  7296. * @name $.jstree.defaults.search.close_opened_onclear
  7297. * @plugin search
  7298. */
  7299. close_opened_onclear : true,
  7300. /**
  7301. * Indicates if only leaf nodes should be included in search results. Default is `false`.
  7302. * @name $.jstree.defaults.search.search_leaves_only
  7303. * @plugin search
  7304. */
  7305. search_leaves_only : false,
  7306. /**
  7307. * 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).
  7308. * 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`.
  7309. * @name $.jstree.defaults.search.search_callback
  7310. * @plugin search
  7311. */
  7312. search_callback : false
  7313. };
  7314. $.jstree.plugins.search = function (options, parent) {
  7315. this.bind = function () {
  7316. parent.bind.call(this);
  7317. this._data.search.str = "";
  7318. this._data.search.dom = $();
  7319. this._data.search.res = [];
  7320. this._data.search.opn = [];
  7321. this._data.search.som = false;
  7322. this._data.search.smc = false;
  7323. this._data.search.hdn = [];
  7324. this.element
  7325. .on("search.jstree", $.proxy(function (e, data) {
  7326. if(this._data.search.som && data.res.length) {
  7327. var m = this._model.data, i, j, p = [], k, l;
  7328. for(i = 0, j = data.res.length; i < j; i++) {
  7329. if(m[data.res[i]] && !m[data.res[i]].state.hidden) {
  7330. p.push(data.res[i]);
  7331. p = p.concat(m[data.res[i]].parents);
  7332. if(this._data.search.smc) {
  7333. for (k = 0, l = m[data.res[i]].children_d.length; k < l; k++) {
  7334. if (m[m[data.res[i]].children_d[k]] && !m[m[data.res[i]].children_d[k]].state.hidden) {
  7335. p.push(m[data.res[i]].children_d[k]);
  7336. }
  7337. }
  7338. }
  7339. }
  7340. }
  7341. p = $.vakata.array_remove_item($.vakata.array_unique(p), $.jstree.root);
  7342. this._data.search.hdn = this.hide_all(true);
  7343. this.show_node(p, true);
  7344. this.redraw(true);
  7345. }
  7346. }, this))
  7347. .on("clear_search.jstree", $.proxy(function (e, data) {
  7348. if(this._data.search.som && data.res.length) {
  7349. this.show_node(this._data.search.hdn, true);
  7350. this.redraw(true);
  7351. }
  7352. }, this));
  7353. };
  7354. /**
  7355. * used to search the tree nodes for a given string
  7356. * @name search(str [, skip_async])
  7357. * @param {String} str the search string
  7358. * @param {Boolean} skip_async if set to true server will not be queried even if configured
  7359. * @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)
  7360. * @param {mixed} inside an optional node to whose children to limit the search
  7361. * @param {Boolean} append if set to true the results of this search are appended to the previous search
  7362. * @plugin search
  7363. * @trigger search.jstree
  7364. */
  7365. this.search = function (str, skip_async, show_only_matches, inside, append, show_only_matches_children) {
  7366. if(str === false || $.trim(str.toString()) === "") {
  7367. return this.clear_search();
  7368. }
  7369. inside = this.get_node(inside);
  7370. inside = inside && inside.id ? inside.id : null;
  7371. str = str.toString();
  7372. var s = this.settings.search,
  7373. a = s.ajax ? s.ajax : false,
  7374. m = this._model.data,
  7375. f = null,
  7376. r = [],
  7377. p = [], i, j;
  7378. if(this._data.search.res.length && !append) {
  7379. this.clear_search();
  7380. }
  7381. if(show_only_matches === undefined) {
  7382. show_only_matches = s.show_only_matches;
  7383. }
  7384. if(show_only_matches_children === undefined) {
  7385. show_only_matches_children = s.show_only_matches_children;
  7386. }
  7387. if(!skip_async && a !== false) {
  7388. if($.isFunction(a)) {
  7389. return a.call(this, str, $.proxy(function (d) {
  7390. if(d && d.d) { d = d.d; }
  7391. this._load_nodes(!$.isArray(d) ? [] : $.vakata.array_unique(d), function () {
  7392. this.search(str, true, show_only_matches, inside, append, show_only_matches_children);
  7393. });
  7394. }, this), inside);
  7395. }
  7396. else {
  7397. a = $.extend({}, a);
  7398. if(!a.data) { a.data = {}; }
  7399. a.data.str = str;
  7400. if(inside) {
  7401. a.data.inside = inside;
  7402. }
  7403. if (this._data.search.lastRequest) {
  7404. this._data.search.lastRequest.abort();
  7405. }
  7406. this._data.search.lastRequest = $.ajax(a)
  7407. .fail($.proxy(function () {
  7408. this._data.core.last_error = { 'error' : 'ajax', 'plugin' : 'search', 'id' : 'search_01', 'reason' : 'Could not load search parents', 'data' : JSON.stringify(a) };
  7409. this.settings.core.error.call(this, this._data.core.last_error);
  7410. }, this))
  7411. .done($.proxy(function (d) {
  7412. if(d && d.d) { d = d.d; }
  7413. this._load_nodes(!$.isArray(d) ? [] : $.vakata.array_unique(d), function () {
  7414. this.search(str, true, show_only_matches, inside, append, show_only_matches_children);
  7415. });
  7416. }, this));
  7417. return this._data.search.lastRequest;
  7418. }
  7419. }
  7420. if(!append) {
  7421. this._data.search.str = str;
  7422. this._data.search.dom = $();
  7423. this._data.search.res = [];
  7424. this._data.search.opn = [];
  7425. this._data.search.som = show_only_matches;
  7426. this._data.search.smc = show_only_matches_children;
  7427. }
  7428. f = new $.vakata.search(str, true, { caseSensitive : s.case_sensitive, fuzzy : s.fuzzy });
  7429. $.each(m[inside ? inside : $.jstree.root].children_d, function (ii, i) {
  7430. var v = m[i];
  7431. 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) ) ) {
  7432. r.push(i);
  7433. p = p.concat(v.parents);
  7434. }
  7435. });
  7436. if(r.length) {
  7437. p = $.vakata.array_unique(p);
  7438. for(i = 0, j = p.length; i < j; i++) {
  7439. if(p[i] !== $.jstree.root && m[p[i]] && this.open_node(p[i], null, 0) === true) {
  7440. this._data.search.opn.push(p[i]);
  7441. }
  7442. }
  7443. if(!append) {
  7444. 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(', #')));
  7445. this._data.search.res = r;
  7446. }
  7447. else {
  7448. 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(', #'))));
  7449. this._data.search.res = $.vakata.array_unique(this._data.search.res.concat(r));
  7450. }
  7451. this._data.search.dom.children(".jstree-anchor").addClass('jstree-search');
  7452. }
  7453. /**
  7454. * triggered after search is complete
  7455. * @event
  7456. * @name search.jstree
  7457. * @param {jQuery} nodes a jQuery collection of matching nodes
  7458. * @param {String} str the search string
  7459. * @param {Array} res a collection of objects represeing the matching nodes
  7460. * @plugin search
  7461. */
  7462. this.trigger('search', { nodes : this._data.search.dom, str : str, res : this._data.search.res, show_only_matches : show_only_matches });
  7463. };
  7464. /**
  7465. * used to clear the last search (removes classes and shows all nodes if filtering is on)
  7466. * @name clear_search()
  7467. * @plugin search
  7468. * @trigger clear_search.jstree
  7469. */
  7470. this.clear_search = function () {
  7471. if(this.settings.search.close_opened_onclear) {
  7472. this.close_node(this._data.search.opn, 0);
  7473. }
  7474. /**
  7475. * triggered after search is complete
  7476. * @event
  7477. * @name clear_search.jstree
  7478. * @param {jQuery} nodes a jQuery collection of matching nodes (the result from the last search)
  7479. * @param {String} str the search string (the last search string)
  7480. * @param {Array} res a collection of objects represeing the matching nodes (the result from the last search)
  7481. * @plugin search
  7482. */
  7483. this.trigger('clear_search', { 'nodes' : this._data.search.dom, str : this._data.search.str, res : this._data.search.res });
  7484. if(this._data.search.res.length) {
  7485. this._data.search.dom = $(this.element[0].querySelectorAll('#' + $.map(this._data.search.res, function (v) {
  7486. return "0123456789".indexOf(v[0]) !== -1 ? '\\3' + v[0] + ' ' + v.substr(1).replace($.jstree.idregex,'\\$&') : v.replace($.jstree.idregex,'\\$&');
  7487. }).join(', #')));
  7488. this._data.search.dom.children(".jstree-anchor").removeClass("jstree-search");
  7489. }
  7490. this._data.search.str = "";
  7491. this._data.search.res = [];
  7492. this._data.search.opn = [];
  7493. this._data.search.dom = $();
  7494. };
  7495. this.redraw_node = function(obj, deep, callback, force_render) {
  7496. obj = parent.redraw_node.apply(this, arguments);
  7497. if(obj) {
  7498. if($.inArray(obj.id, this._data.search.res) !== -1) {
  7499. var i, j, tmp = null;
  7500. for(i = 0, j = obj.childNodes.length; i < j; i++) {
  7501. if(obj.childNodes[i] && obj.childNodes[i].className && obj.childNodes[i].className.indexOf("jstree-anchor") !== -1) {
  7502. tmp = obj.childNodes[i];
  7503. break;
  7504. }
  7505. }
  7506. if(tmp) {
  7507. tmp.className += ' jstree-search';
  7508. }
  7509. }
  7510. }
  7511. return obj;
  7512. };
  7513. };
  7514. // helpers
  7515. (function ($) {
  7516. // from http://kiro.me/projects/fuse.html
  7517. $.vakata.search = function(pattern, txt, options) {
  7518. options = options || {};
  7519. options = $.extend({}, $.vakata.search.defaults, options);
  7520. if(options.fuzzy !== false) {
  7521. options.fuzzy = true;
  7522. }
  7523. pattern = options.caseSensitive ? pattern : pattern.toLowerCase();
  7524. var MATCH_LOCATION = options.location,
  7525. MATCH_DISTANCE = options.distance,
  7526. MATCH_THRESHOLD = options.threshold,
  7527. patternLen = pattern.length,
  7528. matchmask, pattern_alphabet, match_bitapScore, search;
  7529. if(patternLen > 32) {
  7530. options.fuzzy = false;
  7531. }
  7532. if(options.fuzzy) {
  7533. matchmask = 1 << (patternLen - 1);
  7534. pattern_alphabet = (function () {
  7535. var mask = {},
  7536. i = 0;
  7537. for (i = 0; i < patternLen; i++) {
  7538. mask[pattern.charAt(i)] = 0;
  7539. }
  7540. for (i = 0; i < patternLen; i++) {
  7541. mask[pattern.charAt(i)] |= 1 << (patternLen - i - 1);
  7542. }
  7543. return mask;
  7544. }());
  7545. match_bitapScore = function (e, x) {
  7546. var accuracy = e / patternLen,
  7547. proximity = Math.abs(MATCH_LOCATION - x);
  7548. if(!MATCH_DISTANCE) {
  7549. return proximity ? 1.0 : accuracy;
  7550. }
  7551. return accuracy + (proximity / MATCH_DISTANCE);
  7552. };
  7553. }
  7554. search = function (text) {
  7555. text = options.caseSensitive ? text : text.toLowerCase();
  7556. if(pattern === text || text.indexOf(pattern) !== -1) {
  7557. return {
  7558. isMatch: true,
  7559. score: 0
  7560. };
  7561. }
  7562. if(!options.fuzzy) {
  7563. return {
  7564. isMatch: false,
  7565. score: 1
  7566. };
  7567. }
  7568. var i, j,
  7569. textLen = text.length,
  7570. scoreThreshold = MATCH_THRESHOLD,
  7571. bestLoc = text.indexOf(pattern, MATCH_LOCATION),
  7572. binMin, binMid,
  7573. binMax = patternLen + textLen,
  7574. lastRd, start, finish, rd, charMatch,
  7575. score = 1,
  7576. locations = [];
  7577. if (bestLoc !== -1) {
  7578. scoreThreshold = Math.min(match_bitapScore(0, bestLoc), scoreThreshold);
  7579. bestLoc = text.lastIndexOf(pattern, MATCH_LOCATION + patternLen);
  7580. if (bestLoc !== -1) {
  7581. scoreThreshold = Math.min(match_bitapScore(0, bestLoc), scoreThreshold);
  7582. }
  7583. }
  7584. bestLoc = -1;
  7585. for (i = 0; i < patternLen; i++) {
  7586. binMin = 0;
  7587. binMid = binMax;
  7588. while (binMin < binMid) {
  7589. if (match_bitapScore(i, MATCH_LOCATION + binMid) <= scoreThreshold) {
  7590. binMin = binMid;
  7591. } else {
  7592. binMax = binMid;
  7593. }
  7594. binMid = Math.floor((binMax - binMin) / 2 + binMin);
  7595. }
  7596. binMax = binMid;
  7597. start = Math.max(1, MATCH_LOCATION - binMid + 1);
  7598. finish = Math.min(MATCH_LOCATION + binMid, textLen) + patternLen;
  7599. rd = new Array(finish + 2);
  7600. rd[finish + 1] = (1 << i) - 1;
  7601. for (j = finish; j >= start; j--) {
  7602. charMatch = pattern_alphabet[text.charAt(j - 1)];
  7603. if (i === 0) {
  7604. rd[j] = ((rd[j + 1] << 1) | 1) & charMatch;
  7605. } else {
  7606. rd[j] = ((rd[j + 1] << 1) | 1) & charMatch | (((lastRd[j + 1] | lastRd[j]) << 1) | 1) | lastRd[j + 1];
  7607. }
  7608. if (rd[j] & matchmask) {
  7609. score = match_bitapScore(i, j - 1);
  7610. if (score <= scoreThreshold) {
  7611. scoreThreshold = score;
  7612. bestLoc = j - 1;
  7613. locations.push(bestLoc);
  7614. if (bestLoc > MATCH_LOCATION) {
  7615. start = Math.max(1, 2 * MATCH_LOCATION - bestLoc);
  7616. } else {
  7617. break;
  7618. }
  7619. }
  7620. }
  7621. }
  7622. if (match_bitapScore(i + 1, MATCH_LOCATION) > scoreThreshold) {
  7623. break;
  7624. }
  7625. lastRd = rd;
  7626. }
  7627. return {
  7628. isMatch: bestLoc >= 0,
  7629. score: score
  7630. };
  7631. };
  7632. return txt === true ? { 'search' : search } : search(txt);
  7633. };
  7634. $.vakata.search.defaults = {
  7635. location : 0,
  7636. distance : 100,
  7637. threshold : 0.6,
  7638. fuzzy : false,
  7639. caseSensitive : false
  7640. };
  7641. }($));
  7642. // include the search plugin by default
  7643. // $.jstree.defaults.plugins.push("search");
  7644. /**
  7645. * ### Sort plugin
  7646. *
  7647. * Automatically sorts all siblings in the tree according to a sorting function.
  7648. */
  7649. /**
  7650. * the settings function used to sort the nodes.
  7651. * It is executed in the tree's context, accepts two nodes as arguments and should return `1` or `-1`.
  7652. * @name $.jstree.defaults.sort
  7653. * @plugin sort
  7654. */
  7655. $.jstree.defaults.sort = function (a, b) {
  7656. //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);
  7657. return this.get_text(a) > this.get_text(b) ? 1 : -1;
  7658. };
  7659. $.jstree.plugins.sort = function (options, parent) {
  7660. this.bind = function () {
  7661. parent.bind.call(this);
  7662. this.element
  7663. .on("model.jstree", $.proxy(function (e, data) {
  7664. this.sort(data.parent, true);
  7665. }, this))
  7666. .on("rename_node.jstree create_node.jstree", $.proxy(function (e, data) {
  7667. this.sort(data.parent || data.node.parent, false);
  7668. this.redraw_node(data.parent || data.node.parent, true);
  7669. }, this))
  7670. .on("move_node.jstree copy_node.jstree", $.proxy(function (e, data) {
  7671. this.sort(data.parent, false);
  7672. this.redraw_node(data.parent, true);
  7673. }, this));
  7674. };
  7675. /**
  7676. * used to sort a node's children
  7677. * @private
  7678. * @name sort(obj [, deep])
  7679. * @param {mixed} obj the node
  7680. * @param {Boolean} deep if set to `true` nodes are sorted recursively.
  7681. * @plugin sort
  7682. * @trigger search.jstree
  7683. */
  7684. this.sort = function (obj, deep) {
  7685. var i, j;
  7686. obj = this.get_node(obj);
  7687. if(obj && obj.children && obj.children.length) {
  7688. obj.children.sort($.proxy(this.settings.sort, this));
  7689. if(deep) {
  7690. for(i = 0, j = obj.children_d.length; i < j; i++) {
  7691. this.sort(obj.children_d[i], false);
  7692. }
  7693. }
  7694. }
  7695. };
  7696. };
  7697. // include the sort plugin by default
  7698. // $.jstree.defaults.plugins.push("sort");
  7699. /**
  7700. * ### State plugin
  7701. *
  7702. * Saves the state of the tree (selected nodes, opened nodes) on the user's computer using available options (localStorage, cookies, etc)
  7703. */
  7704. var to = false;
  7705. /**
  7706. * stores all defaults for the state plugin
  7707. * @name $.jstree.defaults.state
  7708. * @plugin state
  7709. */
  7710. $.jstree.defaults.state = {
  7711. /**
  7712. * A string for the key to use when saving the current tree (change if using multiple trees in your project). Defaults to `jstree`.
  7713. * @name $.jstree.defaults.state.key
  7714. * @plugin state
  7715. */
  7716. key : 'jstree',
  7717. /**
  7718. * A space separated list of events that trigger a state save. Defaults to `changed.jstree open_node.jstree close_node.jstree`.
  7719. * @name $.jstree.defaults.state.events
  7720. * @plugin state
  7721. */
  7722. events : 'changed.jstree open_node.jstree close_node.jstree check_node.jstree uncheck_node.jstree',
  7723. /**
  7724. * Time in milliseconds after which the state will expire. Defaults to 'false' meaning - no expire.
  7725. * @name $.jstree.defaults.state.ttl
  7726. * @plugin state
  7727. */
  7728. ttl : false,
  7729. /**
  7730. * 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.
  7731. * @name $.jstree.defaults.state.filter
  7732. * @plugin state
  7733. */
  7734. filter : false,
  7735. /**
  7736. * Should loaded nodes be restored (setting this to true means that it is possible that the whole tree will be loaded for some users - use with caution). Defaults to `false`
  7737. * @name $.jstree.defaults.state.preserve_loaded
  7738. * @plugin state
  7739. */
  7740. preserve_loaded : false
  7741. };
  7742. $.jstree.plugins.state = function (options, parent) {
  7743. this.bind = function () {
  7744. parent.bind.call(this);
  7745. var bind = $.proxy(function () {
  7746. this.element.on(this.settings.state.events, $.proxy(function () {
  7747. if(to) { clearTimeout(to); }
  7748. to = setTimeout($.proxy(function () { this.save_state(); }, this), 100);
  7749. }, this));
  7750. /**
  7751. * triggered when the state plugin is finished restoring the state (and immediately after ready if there is no state to restore).
  7752. * @event
  7753. * @name state_ready.jstree
  7754. * @plugin state
  7755. */
  7756. this.trigger('state_ready');
  7757. }, this);
  7758. this.element
  7759. .on("ready.jstree", $.proxy(function (e, data) {
  7760. this.element.one("restore_state.jstree", bind);
  7761. if(!this.restore_state()) { bind(); }
  7762. }, this));
  7763. };
  7764. /**
  7765. * save the state
  7766. * @name save_state()
  7767. * @plugin state
  7768. */
  7769. this.save_state = function () {
  7770. var tm = this.get_state();
  7771. if (!this.settings.state.preserve_loaded) {
  7772. delete tm.core.loaded;
  7773. }
  7774. var st = { 'state' : tm, 'ttl' : this.settings.state.ttl, 'sec' : +(new Date()) };
  7775. $.vakata.storage.set(this.settings.state.key, JSON.stringify(st));
  7776. };
  7777. /**
  7778. * restore the state from the user's computer
  7779. * @name restore_state()
  7780. * @plugin state
  7781. */
  7782. this.restore_state = function () {
  7783. var k = $.vakata.storage.get(this.settings.state.key);
  7784. if(!!k) { try { k = JSON.parse(k); } catch(ex) { return false; } }
  7785. if(!!k && k.ttl && k.sec && +(new Date()) - k.sec > k.ttl) { return false; }
  7786. if(!!k && k.state) { k = k.state; }
  7787. if(!!k && $.isFunction(this.settings.state.filter)) { k = this.settings.state.filter.call(this, k); }
  7788. if(!!k) {
  7789. if (!this.settings.state.preserve_loaded) {
  7790. delete k.core.loaded;
  7791. }
  7792. this.element.one("set_state.jstree", function (e, data) { data.instance.trigger('restore_state', { 'state' : $.extend(true, {}, k) }); });
  7793. this.set_state(k);
  7794. return true;
  7795. }
  7796. return false;
  7797. };
  7798. /**
  7799. * clear the state on the user's computer
  7800. * @name clear_state()
  7801. * @plugin state
  7802. */
  7803. this.clear_state = function () {
  7804. return $.vakata.storage.del(this.settings.state.key);
  7805. };
  7806. };
  7807. (function ($, undefined) {
  7808. $.vakata.storage = {
  7809. // simply specifying the functions in FF throws an error
  7810. set : function (key, val) { return window.localStorage.setItem(key, val); },
  7811. get : function (key) { return window.localStorage.getItem(key); },
  7812. del : function (key) { return window.localStorage.removeItem(key); }
  7813. };
  7814. }($));
  7815. // include the state plugin by default
  7816. // $.jstree.defaults.plugins.push("state");
  7817. /**
  7818. * ### Types plugin
  7819. *
  7820. * 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.
  7821. */
  7822. /**
  7823. * 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).
  7824. *
  7825. * * `max_children` the maximum number of immediate children this node type can have. Do not specify or set to `-1` for unlimited.
  7826. * * `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.
  7827. * * `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.
  7828. * * `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.
  7829. * * `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)
  7830. * * `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)
  7831. *
  7832. * There are two predefined types:
  7833. *
  7834. * * `#` represents the root of the tree, for example `max_children` would control the maximum number of root nodes.
  7835. * * `default` represents the default node - any settings here will be applied to all nodes that do not have a type specified.
  7836. *
  7837. * @name $.jstree.defaults.types
  7838. * @plugin types
  7839. */
  7840. $.jstree.defaults.types = {
  7841. 'default' : {}
  7842. };
  7843. $.jstree.defaults.types[$.jstree.root] = {};
  7844. $.jstree.plugins.types = function (options, parent) {
  7845. this.init = function (el, options) {
  7846. var i, j;
  7847. if(options && options.types && options.types['default']) {
  7848. for(i in options.types) {
  7849. if(i !== "default" && i !== $.jstree.root && options.types.hasOwnProperty(i)) {
  7850. for(j in options.types['default']) {
  7851. if(options.types['default'].hasOwnProperty(j) && options.types[i][j] === undefined) {
  7852. options.types[i][j] = options.types['default'][j];
  7853. }
  7854. }
  7855. }
  7856. }
  7857. }
  7858. parent.init.call(this, el, options);
  7859. this._model.data[$.jstree.root].type = $.jstree.root;
  7860. };
  7861. this.refresh = function (skip_loading, forget_state) {
  7862. parent.refresh.call(this, skip_loading, forget_state);
  7863. this._model.data[$.jstree.root].type = $.jstree.root;
  7864. };
  7865. this.bind = function () {
  7866. this.element
  7867. .on('model.jstree', $.proxy(function (e, data) {
  7868. var m = this._model.data,
  7869. dpc = data.nodes,
  7870. t = this.settings.types,
  7871. i, j, c = 'default', k;
  7872. for(i = 0, j = dpc.length; i < j; i++) {
  7873. c = 'default';
  7874. if(m[dpc[i]].original && m[dpc[i]].original.type && t[m[dpc[i]].original.type]) {
  7875. c = m[dpc[i]].original.type;
  7876. }
  7877. if(m[dpc[i]].data && m[dpc[i]].data.jstree && m[dpc[i]].data.jstree.type && t[m[dpc[i]].data.jstree.type]) {
  7878. c = m[dpc[i]].data.jstree.type;
  7879. }
  7880. m[dpc[i]].type = c;
  7881. if(m[dpc[i]].icon === true && t[c].icon !== undefined) {
  7882. m[dpc[i]].icon = t[c].icon;
  7883. }
  7884. if(t[c].li_attr !== undefined && typeof t[c].li_attr === 'object') {
  7885. for (k in t[c].li_attr) {
  7886. if (t[c].li_attr.hasOwnProperty(k)) {
  7887. if (k === 'id') {
  7888. continue;
  7889. }
  7890. else if (m[dpc[i]].li_attr[k] === undefined) {
  7891. m[dpc[i]].li_attr[k] = t[c].li_attr[k];
  7892. }
  7893. else if (k === 'class') {
  7894. m[dpc[i]].li_attr['class'] = t[c].li_attr['class'] + ' ' + m[dpc[i]].li_attr['class'];
  7895. }
  7896. }
  7897. }
  7898. }
  7899. if(t[c].a_attr !== undefined && typeof t[c].a_attr === 'object') {
  7900. for (k in t[c].a_attr) {
  7901. if (t[c].a_attr.hasOwnProperty(k)) {
  7902. if (k === 'id') {
  7903. continue;
  7904. }
  7905. else if (m[dpc[i]].a_attr[k] === undefined) {
  7906. m[dpc[i]].a_attr[k] = t[c].a_attr[k];
  7907. }
  7908. else if (k === 'href' && m[dpc[i]].a_attr[k] === '#') {
  7909. m[dpc[i]].a_attr['href'] = t[c].a_attr['href'];
  7910. }
  7911. else if (k === 'class') {
  7912. m[dpc[i]].a_attr['class'] = t[c].a_attr['class'] + ' ' + m[dpc[i]].a_attr['class'];
  7913. }
  7914. }
  7915. }
  7916. }
  7917. }
  7918. m[$.jstree.root].type = $.jstree.root;
  7919. }, this));
  7920. parent.bind.call(this);
  7921. };
  7922. this.get_json = function (obj, options, flat) {
  7923. var i, j,
  7924. m = this._model.data,
  7925. opt = options ? $.extend(true, {}, options, {no_id:false}) : {},
  7926. tmp = parent.get_json.call(this, obj, opt, flat);
  7927. if(tmp === false) { return false; }
  7928. if($.isArray(tmp)) {
  7929. for(i = 0, j = tmp.length; i < j; i++) {
  7930. tmp[i].type = tmp[i].id && m[tmp[i].id] && m[tmp[i].id].type ? m[tmp[i].id].type : "default";
  7931. if(options && options.no_id) {
  7932. delete tmp[i].id;
  7933. if(tmp[i].li_attr && tmp[i].li_attr.id) {
  7934. delete tmp[i].li_attr.id;
  7935. }
  7936. if(tmp[i].a_attr && tmp[i].a_attr.id) {
  7937. delete tmp[i].a_attr.id;
  7938. }
  7939. }
  7940. }
  7941. }
  7942. else {
  7943. tmp.type = tmp.id && m[tmp.id] && m[tmp.id].type ? m[tmp.id].type : "default";
  7944. if(options && options.no_id) {
  7945. tmp = this._delete_ids(tmp);
  7946. }
  7947. }
  7948. return tmp;
  7949. };
  7950. this._delete_ids = function (tmp) {
  7951. if($.isArray(tmp)) {
  7952. for(var i = 0, j = tmp.length; i < j; i++) {
  7953. tmp[i] = this._delete_ids(tmp[i]);
  7954. }
  7955. return tmp;
  7956. }
  7957. delete tmp.id;
  7958. if(tmp.li_attr && tmp.li_attr.id) {
  7959. delete tmp.li_attr.id;
  7960. }
  7961. if(tmp.a_attr && tmp.a_attr.id) {
  7962. delete tmp.a_attr.id;
  7963. }
  7964. if(tmp.children && $.isArray(tmp.children)) {
  7965. tmp.children = this._delete_ids(tmp.children);
  7966. }
  7967. return tmp;
  7968. };
  7969. this.check = function (chk, obj, par, pos, more) {
  7970. if(parent.check.call(this, chk, obj, par, pos, more) === false) { return false; }
  7971. obj = obj && obj.id ? obj : this.get_node(obj);
  7972. par = par && par.id ? par : this.get_node(par);
  7973. var m = obj && obj.id ? (more && more.origin ? more.origin : $.jstree.reference(obj.id)) : null, tmp, d, i, j;
  7974. m = m && m._model && m._model.data ? m._model.data : null;
  7975. switch(chk) {
  7976. case "create_node":
  7977. case "move_node":
  7978. case "copy_node":
  7979. if(chk !== 'move_node' || $.inArray(obj.id, par.children) === -1) {
  7980. tmp = this.get_rules(par);
  7981. if(tmp.max_children !== undefined && tmp.max_children !== -1 && tmp.max_children === par.children.length) {
  7982. 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 }) };
  7983. return false;
  7984. }
  7985. if(tmp.valid_children !== undefined && tmp.valid_children !== -1 && $.inArray((obj.type || 'default'), tmp.valid_children) === -1) {
  7986. 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 }) };
  7987. return false;
  7988. }
  7989. if(m && obj.children_d && obj.parents) {
  7990. d = 0;
  7991. for(i = 0, j = obj.children_d.length; i < j; i++) {
  7992. d = Math.max(d, m[obj.children_d[i]].parents.length);
  7993. }
  7994. d = d - obj.parents.length + 1;
  7995. }
  7996. if(d <= 0 || d === undefined) { d = 1; }
  7997. do {
  7998. if(tmp.max_depth !== undefined && tmp.max_depth !== -1 && tmp.max_depth < d) {
  7999. 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 }) };
  8000. return false;
  8001. }
  8002. par = this.get_node(par.parent);
  8003. tmp = this.get_rules(par);
  8004. d++;
  8005. } while(par);
  8006. }
  8007. break;
  8008. }
  8009. return true;
  8010. };
  8011. /**
  8012. * used to retrieve the type settings object for a node
  8013. * @name get_rules(obj)
  8014. * @param {mixed} obj the node to find the rules for
  8015. * @return {Object}
  8016. * @plugin types
  8017. */
  8018. this.get_rules = function (obj) {
  8019. obj = this.get_node(obj);
  8020. if(!obj) { return false; }
  8021. var tmp = this.get_type(obj, true);
  8022. if(tmp.max_depth === undefined) { tmp.max_depth = -1; }
  8023. if(tmp.max_children === undefined) { tmp.max_children = -1; }
  8024. if(tmp.valid_children === undefined) { tmp.valid_children = -1; }
  8025. return tmp;
  8026. };
  8027. /**
  8028. * used to retrieve the type string or settings object for a node
  8029. * @name get_type(obj [, rules])
  8030. * @param {mixed} obj the node to find the rules for
  8031. * @param {Boolean} rules if set to `true` instead of a string the settings object will be returned
  8032. * @return {String|Object}
  8033. * @plugin types
  8034. */
  8035. this.get_type = function (obj, rules) {
  8036. obj = this.get_node(obj);
  8037. return (!obj) ? false : ( rules ? $.extend({ 'type' : obj.type }, this.settings.types[obj.type]) : obj.type);
  8038. };
  8039. /**
  8040. * used to change a node's type
  8041. * @name set_type(obj, type)
  8042. * @param {mixed} obj the node to change
  8043. * @param {String} type the new type
  8044. * @plugin types
  8045. */
  8046. this.set_type = function (obj, type) {
  8047. var m = this._model.data, t, t1, t2, old_type, old_icon, k, d, a;
  8048. if($.isArray(obj)) {
  8049. obj = obj.slice();
  8050. for(t1 = 0, t2 = obj.length; t1 < t2; t1++) {
  8051. this.set_type(obj[t1], type);
  8052. }
  8053. return true;
  8054. }
  8055. t = this.settings.types;
  8056. obj = this.get_node(obj);
  8057. if(!t[type] || !obj) { return false; }
  8058. d = this.get_node(obj, true);
  8059. if (d && d.length) {
  8060. a = d.children('.jstree-anchor');
  8061. }
  8062. old_type = obj.type;
  8063. old_icon = this.get_icon(obj);
  8064. obj.type = type;
  8065. if(old_icon === true || !t[old_type] || (t[old_type].icon !== undefined && old_icon === t[old_type].icon)) {
  8066. this.set_icon(obj, t[type].icon !== undefined ? t[type].icon : true);
  8067. }
  8068. // remove old type props
  8069. if(t[old_type] && t[old_type].li_attr !== undefined && typeof t[old_type].li_attr === 'object') {
  8070. for (k in t[old_type].li_attr) {
  8071. if (t[old_type].li_attr.hasOwnProperty(k)) {
  8072. if (k === 'id') {
  8073. continue;
  8074. }
  8075. else if (k === 'class') {
  8076. m[obj.id].li_attr['class'] = (m[obj.id].li_attr['class'] || '').replace(t[old_type].li_attr[k], '');
  8077. if (d) { d.removeClass(t[old_type].li_attr[k]); }
  8078. }
  8079. else if (m[obj.id].li_attr[k] === t[old_type].li_attr[k]) {
  8080. m[obj.id].li_attr[k] = null;
  8081. if (d) { d.removeAttr(k); }
  8082. }
  8083. }
  8084. }
  8085. }
  8086. if(t[old_type] && t[old_type].a_attr !== undefined && typeof t[old_type].a_attr === 'object') {
  8087. for (k in t[old_type].a_attr) {
  8088. if (t[old_type].a_attr.hasOwnProperty(k)) {
  8089. if (k === 'id') {
  8090. continue;
  8091. }
  8092. else if (k === 'class') {
  8093. m[obj.id].a_attr['class'] = (m[obj.id].a_attr['class'] || '').replace(t[old_type].a_attr[k], '');
  8094. if (a) { a.removeClass(t[old_type].a_attr[k]); }
  8095. }
  8096. else if (m[obj.id].a_attr[k] === t[old_type].a_attr[k]) {
  8097. if (k === 'href') {
  8098. m[obj.id].a_attr[k] = '#';
  8099. if (a) { a.attr('href', '#'); }
  8100. }
  8101. else {
  8102. delete m[obj.id].a_attr[k];
  8103. if (a) { a.removeAttr(k); }
  8104. }
  8105. }
  8106. }
  8107. }
  8108. }
  8109. // add new props
  8110. if(t[type].li_attr !== undefined && typeof t[type].li_attr === 'object') {
  8111. for (k in t[type].li_attr) {
  8112. if (t[type].li_attr.hasOwnProperty(k)) {
  8113. if (k === 'id') {
  8114. continue;
  8115. }
  8116. else if (m[obj.id].li_attr[k] === undefined) {
  8117. m[obj.id].li_attr[k] = t[type].li_attr[k];
  8118. if (d) {
  8119. if (k === 'class') {
  8120. d.addClass(t[type].li_attr[k]);
  8121. }
  8122. else {
  8123. d.attr(k, t[type].li_attr[k]);
  8124. }
  8125. }
  8126. }
  8127. else if (k === 'class') {
  8128. m[obj.id].li_attr['class'] = t[type].li_attr[k] + ' ' + m[obj.id].li_attr['class'];
  8129. if (d) { d.addClass(t[type].li_attr[k]); }
  8130. }
  8131. }
  8132. }
  8133. }
  8134. if(t[type].a_attr !== undefined && typeof t[type].a_attr === 'object') {
  8135. for (k in t[type].a_attr) {
  8136. if (t[type].a_attr.hasOwnProperty(k)) {
  8137. if (k === 'id') {
  8138. continue;
  8139. }
  8140. else if (m[obj.id].a_attr[k] === undefined) {
  8141. m[obj.id].a_attr[k] = t[type].a_attr[k];
  8142. if (a) {
  8143. if (k === 'class') {
  8144. a.addClass(t[type].a_attr[k]);
  8145. }
  8146. else {
  8147. a.attr(k, t[type].a_attr[k]);
  8148. }
  8149. }
  8150. }
  8151. else if (k === 'href' && m[obj.id].a_attr[k] === '#') {
  8152. m[obj.id].a_attr['href'] = t[type].a_attr['href'];
  8153. if (a) { a.attr('href', t[type].a_attr['href']); }
  8154. }
  8155. else if (k === 'class') {
  8156. m[obj.id].a_attr['class'] = t[type].a_attr['class'] + ' ' + m[obj.id].a_attr['class'];
  8157. if (a) { a.addClass(t[type].a_attr[k]); }
  8158. }
  8159. }
  8160. }
  8161. }
  8162. return true;
  8163. };
  8164. };
  8165. // include the types plugin by default
  8166. // $.jstree.defaults.plugins.push("types");
  8167. /**
  8168. * ### Unique plugin
  8169. *
  8170. * Enforces that no nodes with the same name can coexist as siblings.
  8171. */
  8172. /**
  8173. * stores all defaults for the unique plugin
  8174. * @name $.jstree.defaults.unique
  8175. * @plugin unique
  8176. */
  8177. $.jstree.defaults.unique = {
  8178. /**
  8179. * Indicates if the comparison should be case sensitive. Default is `false`.
  8180. * @name $.jstree.defaults.unique.case_sensitive
  8181. * @plugin unique
  8182. */
  8183. case_sensitive : false,
  8184. /**
  8185. * Indicates if white space should be trimmed before the comparison. Default is `false`.
  8186. * @name $.jstree.defaults.unique.trim_whitespace
  8187. * @plugin unique
  8188. */
  8189. trim_whitespace : false,
  8190. /**
  8191. * 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)`.
  8192. * @name $.jstree.defaults.unique.duplicate
  8193. * @plugin unique
  8194. */
  8195. duplicate : function (name, counter) {
  8196. return name + ' (' + counter + ')';
  8197. }
  8198. };
  8199. $.jstree.plugins.unique = function (options, parent) {
  8200. this.check = function (chk, obj, par, pos, more) {
  8201. if(parent.check.call(this, chk, obj, par, pos, more) === false) { return false; }
  8202. obj = obj && obj.id ? obj : this.get_node(obj);
  8203. par = par && par.id ? par : this.get_node(par);
  8204. if(!par || !par.children) { return true; }
  8205. var n = chk === "rename_node" ? pos : obj.text,
  8206. c = [],
  8207. s = this.settings.unique.case_sensitive,
  8208. w = this.settings.unique.trim_whitespace,
  8209. m = this._model.data, i, j, t;
  8210. for(i = 0, j = par.children.length; i < j; i++) {
  8211. t = m[par.children[i]].text;
  8212. if (!s) {
  8213. t = t.toLowerCase();
  8214. }
  8215. if (w) {
  8216. t = t.replace(/^[\s\uFEFF\xA0]+|[\s\uFEFF\xA0]+$/g, '');
  8217. }
  8218. c.push(t);
  8219. }
  8220. if(!s) { n = n.toLowerCase(); }
  8221. if (w) { n = n.replace(/^[\s\uFEFF\xA0]+|[\s\uFEFF\xA0]+$/g, ''); }
  8222. switch(chk) {
  8223. case "delete_node":
  8224. return true;
  8225. case "rename_node":
  8226. t = obj.text || '';
  8227. if (!s) {
  8228. t = t.toLowerCase();
  8229. }
  8230. if (w) {
  8231. t = t.replace(/^[\s\uFEFF\xA0]+|[\s\uFEFF\xA0]+$/g, '');
  8232. }
  8233. i = ($.inArray(n, c) === -1 || (obj.text && t === n));
  8234. if(!i) {
  8235. 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 }) };
  8236. }
  8237. return i;
  8238. case "create_node":
  8239. i = ($.inArray(n, c) === -1);
  8240. if(!i) {
  8241. 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 }) };
  8242. }
  8243. return i;
  8244. case "copy_node":
  8245. i = ($.inArray(n, c) === -1);
  8246. if(!i) {
  8247. 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 }) };
  8248. }
  8249. return i;
  8250. case "move_node":
  8251. i = ( (obj.parent === par.id && (!more || !more.is_multi)) || $.inArray(n, c) === -1);
  8252. if(!i) {
  8253. 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 }) };
  8254. }
  8255. return i;
  8256. }
  8257. return true;
  8258. };
  8259. this.create_node = function (par, node, pos, callback, is_loaded) {
  8260. if(!node || node.text === undefined) {
  8261. if(par === null) {
  8262. par = $.jstree.root;
  8263. }
  8264. par = this.get_node(par);
  8265. if(!par) {
  8266. return parent.create_node.call(this, par, node, pos, callback, is_loaded);
  8267. }
  8268. pos = pos === undefined ? "last" : pos;
  8269. if(!pos.toString().match(/^(before|after)$/) && !is_loaded && !this.is_loaded(par)) {
  8270. return parent.create_node.call(this, par, node, pos, callback, is_loaded);
  8271. }
  8272. if(!node) { node = {}; }
  8273. var tmp, n, dpc, i, j, m = this._model.data, s = this.settings.unique.case_sensitive, w = this.settings.unique.trim_whitespace, cb = this.settings.unique.duplicate, t;
  8274. n = tmp = this.get_string('New node');
  8275. dpc = [];
  8276. for(i = 0, j = par.children.length; i < j; i++) {
  8277. t = m[par.children[i]].text;
  8278. if (!s) {
  8279. t = t.toLowerCase();
  8280. }
  8281. if (w) {
  8282. t = t.replace(/^[\s\uFEFF\xA0]+|[\s\uFEFF\xA0]+$/g, '');
  8283. }
  8284. dpc.push(t);
  8285. }
  8286. i = 1;
  8287. t = n;
  8288. if (!s) {
  8289. t = t.toLowerCase();
  8290. }
  8291. if (w) {
  8292. t = t.replace(/^[\s\uFEFF\xA0]+|[\s\uFEFF\xA0]+$/g, '');
  8293. }
  8294. while($.inArray(t, dpc) !== -1) {
  8295. n = cb.call(this, tmp, (++i)).toString();
  8296. t = n;
  8297. if (!s) {
  8298. t = t.toLowerCase();
  8299. }
  8300. if (w) {
  8301. t = t.replace(/^[\s\uFEFF\xA0]+|[\s\uFEFF\xA0]+$/g, '');
  8302. }
  8303. }
  8304. node.text = n;
  8305. }
  8306. return parent.create_node.call(this, par, node, pos, callback, is_loaded);
  8307. };
  8308. };
  8309. // include the unique plugin by default
  8310. // $.jstree.defaults.plugins.push("unique");
  8311. /**
  8312. * ### Wholerow plugin
  8313. *
  8314. * Makes each node appear block level. Making selection easier. May cause slow down for large trees in old browsers.
  8315. */
  8316. var div = document.createElement('DIV');
  8317. div.setAttribute('unselectable','on');
  8318. div.setAttribute('role','presentation');
  8319. div.className = 'jstree-wholerow';
  8320. div.innerHTML = '&#160;';
  8321. $.jstree.plugins.wholerow = function (options, parent) {
  8322. this.bind = function () {
  8323. parent.bind.call(this);
  8324. this.element
  8325. .on('ready.jstree set_state.jstree', $.proxy(function () {
  8326. this.hide_dots();
  8327. }, this))
  8328. .on("init.jstree loading.jstree ready.jstree", $.proxy(function () {
  8329. //div.style.height = this._data.core.li_height + 'px';
  8330. this.get_container_ul().addClass('jstree-wholerow-ul');
  8331. }, this))
  8332. .on("deselect_all.jstree", $.proxy(function (e, data) {
  8333. this.element.find('.jstree-wholerow-clicked').removeClass('jstree-wholerow-clicked');
  8334. }, this))
  8335. .on("changed.jstree", $.proxy(function (e, data) {
  8336. this.element.find('.jstree-wholerow-clicked').removeClass('jstree-wholerow-clicked');
  8337. var tmp = false, i, j;
  8338. for(i = 0, j = data.selected.length; i < j; i++) {
  8339. tmp = this.get_node(data.selected[i], true);
  8340. if(tmp && tmp.length) {
  8341. tmp.children('.jstree-wholerow').addClass('jstree-wholerow-clicked');
  8342. }
  8343. }
  8344. }, this))
  8345. .on("open_node.jstree", $.proxy(function (e, data) {
  8346. this.get_node(data.node, true).find('.jstree-clicked').parent().children('.jstree-wholerow').addClass('jstree-wholerow-clicked');
  8347. }, this))
  8348. .on("hover_node.jstree dehover_node.jstree", $.proxy(function (e, data) {
  8349. if(e.type === "hover_node" && this.is_disabled(data.node)) { return; }
  8350. this.get_node(data.node, true).children('.jstree-wholerow')[e.type === "hover_node"?"addClass":"removeClass"]('jstree-wholerow-hovered');
  8351. }, this))
  8352. .on("contextmenu.jstree", ".jstree-wholerow", $.proxy(function (e) {
  8353. if (this._data.contextmenu) {
  8354. e.preventDefault();
  8355. var tmp = $.Event('contextmenu', { metaKey : e.metaKey, ctrlKey : e.ctrlKey, altKey : e.altKey, shiftKey : e.shiftKey, pageX : e.pageX, pageY : e.pageY });
  8356. $(e.currentTarget).closest(".jstree-node").children(".jstree-anchor").first().trigger(tmp);
  8357. }
  8358. }, this))
  8359. /*!
  8360. .on("mousedown.jstree touchstart.jstree", ".jstree-wholerow", function (e) {
  8361. if(e.target === e.currentTarget) {
  8362. var a = $(e.currentTarget).closest(".jstree-node").children(".jstree-anchor");
  8363. e.target = a[0];
  8364. a.trigger(e);
  8365. }
  8366. })
  8367. */
  8368. .on("click.jstree", ".jstree-wholerow", function (e) {
  8369. e.stopImmediatePropagation();
  8370. var tmp = $.Event('click', { metaKey : e.metaKey, ctrlKey : e.ctrlKey, altKey : e.altKey, shiftKey : e.shiftKey });
  8371. $(e.currentTarget).closest(".jstree-node").children(".jstree-anchor").first().trigger(tmp).focus();
  8372. })
  8373. .on("dblclick.jstree", ".jstree-wholerow", function (e) {
  8374. e.stopImmediatePropagation();
  8375. var tmp = $.Event('dblclick', { metaKey : e.metaKey, ctrlKey : e.ctrlKey, altKey : e.altKey, shiftKey : e.shiftKey });
  8376. $(e.currentTarget).closest(".jstree-node").children(".jstree-anchor").first().trigger(tmp).focus();
  8377. })
  8378. .on("click.jstree", ".jstree-leaf > .jstree-ocl", $.proxy(function (e) {
  8379. e.stopImmediatePropagation();
  8380. var tmp = $.Event('click', { metaKey : e.metaKey, ctrlKey : e.ctrlKey, altKey : e.altKey, shiftKey : e.shiftKey });
  8381. $(e.currentTarget).closest(".jstree-node").children(".jstree-anchor").first().trigger(tmp).focus();
  8382. }, this))
  8383. .on("mouseover.jstree", ".jstree-wholerow, .jstree-icon", $.proxy(function (e) {
  8384. e.stopImmediatePropagation();
  8385. if(!this.is_disabled(e.currentTarget)) {
  8386. this.hover_node(e.currentTarget);
  8387. }
  8388. return false;
  8389. }, this))
  8390. .on("mouseleave.jstree", ".jstree-node", $.proxy(function (e) {
  8391. this.dehover_node(e.currentTarget);
  8392. }, this));
  8393. };
  8394. this.teardown = function () {
  8395. if(this.settings.wholerow) {
  8396. this.element.find(".jstree-wholerow").remove();
  8397. }
  8398. parent.teardown.call(this);
  8399. };
  8400. this.redraw_node = function(obj, deep, callback, force_render) {
  8401. obj = parent.redraw_node.apply(this, arguments);
  8402. if(obj) {
  8403. var tmp = div.cloneNode(true);
  8404. //tmp.style.height = this._data.core.li_height + 'px';
  8405. if($.inArray(obj.id, this._data.core.selected) !== -1) { tmp.className += ' jstree-wholerow-clicked'; }
  8406. if(this._data.core.focused && this._data.core.focused === obj.id) { tmp.className += ' jstree-wholerow-hovered'; }
  8407. obj.insertBefore(tmp, obj.childNodes[0]);
  8408. }
  8409. return obj;
  8410. };
  8411. };
  8412. // include the wholerow plugin by default
  8413. // $.jstree.defaults.plugins.push("wholerow");
  8414. if(window.customElements && Object && Object.create) {
  8415. var proto = Object.create(HTMLElement.prototype);
  8416. proto.createdCallback = function () {
  8417. var c = { core : {}, plugins : [] }, i;
  8418. for(i in $.jstree.plugins) {
  8419. if($.jstree.plugins.hasOwnProperty(i) && this.attributes[i]) {
  8420. c.plugins.push(i);
  8421. if(this.getAttribute(i) && JSON.parse(this.getAttribute(i))) {
  8422. c[i] = JSON.parse(this.getAttribute(i));
  8423. }
  8424. }
  8425. }
  8426. for(i in $.jstree.defaults.core) {
  8427. if($.jstree.defaults.core.hasOwnProperty(i) && this.attributes[i]) {
  8428. c.core[i] = JSON.parse(this.getAttribute(i)) || this.getAttribute(i);
  8429. }
  8430. }
  8431. $(this).jstree(c);
  8432. };
  8433. // proto.attributeChangedCallback = function (name, previous, value) { };
  8434. try {
  8435. window.customElements.define("vakata-jstree", function() {}, { prototype: proto });
  8436. } catch (ignore) { }
  8437. }
  8438. }));