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.
 
 
 
 
 
 

4932 lines
160 KiB

  1. /*!
  2. * jsTree {{VERSION}}
  3. * http://jstree.com/
  4. *
  5. * Copyright (c) 2014 Ivan Bozhanov (http://vakata.com)
  6. *
  7. * Licensed same as jquery - under the terms of the MIT License
  8. * http://www.opensource.org/licenses/mit-license.php
  9. */
  10. /*!
  11. * if using jslint please allow for the jQuery global and use following options:
  12. * jslint: loopfunc: true, browser: true, ass: true, bitwise: true, continue: true, nomen: true, plusplus: true, regexp: true, unparam: true, todo: true, white: true
  13. */
  14. /*jshint -W083 */
  15. /*globals jQuery, define, module, exports, require, window, document, postMessage */
  16. (function (factory) {
  17. "use strict";
  18. if (typeof define === 'function' && define.amd) {
  19. define(['jquery'], factory);
  20. }
  21. else if(typeof module !== 'undefined' && module.exports) {
  22. module.exports = factory(require('jquery'));
  23. }
  24. else {
  25. factory(jQuery);
  26. }
  27. }(function ($, undefined) {
  28. "use strict";
  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 : '{{VERSION}}',
  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. }));