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.

8608 lines
297 KiB

6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
  1. /*globals jQuery, define, module, exports, require, window, document, postMessage */
  2. (function (factory) {
  3. "use strict";
  4. if (typeof define === 'function' && define.amd) {
  5. define(['jquery'], factory);
  6. }
  7. else if(typeof module !== 'undefined' && module.exports) {
  8. module.exports = factory(require('jquery'));
  9. }
  10. else {
  11. factory(jQuery);
  12. }
  13. }(function ($, undefined) {
  14. "use strict";
  15. /*!
  16. * jsTree 3.3.7
  17. * http://jstree.com/
  18. *
  19. * Copyright (c) 2014 Ivan Bozhanov (http://vakata.com)
  20. *
  21. * Licensed same as jquery - under the terms of the MIT License
  22. * http://www.opensource.org/licenses/mit-license.php
  23. */
  24. /*!
  25. * if using jslint please allow for the jQuery global and use following options:
  26. * jslint: loopfunc: true, browser: true, ass: true, bitwise: true, continue: true, nomen: true, plusplus: true, regexp: true, unparam: true, todo: true, white: true
  27. */
  28. /*jshint -W083 */
  29. // prevent another load? maybe there is a better way?
  30. if($.jstree) {
  31. return;
  32. }
  33. /**
  34. * ### jsTree core functionality
  35. */
  36. // internal variables
  37. var instance_counter = 0,
  38. ccp_node = false,
  39. ccp_mode = false,
  40. ccp_inst = false,
  41. themes_loaded = [],
  42. src = $('script:last').attr('src'),
  43. document = window.document; // local variable is always faster to access then a global
  44. /**
  45. * holds all jstree related functions and variables, including the actual class and methods to create, access and manipulate instances.
  46. * @name $.jstree
  47. */
  48. $.jstree = {
  49. /**
  50. * specifies the jstree version in use
  51. * @name $.jstree.version
  52. */
  53. version : '3.3.7',
  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: "Return", 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').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).mouseleave();
  884. $(e.currentTarget).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_d.concat(tmp.children);
  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) && (obj.id === par.id || (chk === "move_node" && $.inArray(obj.id, par.children) === pos) || $.inArray(par.id, obj.children_d) !== -1)) {
  3988. 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 }) };
  3989. return false;
  3990. }
  3991. }
  3992. if(tmp && tmp.data) { tmp = tmp.data; }
  3993. if(tmp && tmp.functions && (tmp.functions[chk] === false || tmp.functions[chk] === true)) {
  3994. if(tmp.functions[chk] === false) {
  3995. 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 }) };
  3996. }
  3997. return tmp.functions[chk];
  3998. }
  3999. if(chc === false || ($.isFunction(chc) && chc.call(this, chk, obj, par, pos, more) === false) || (chc && chc[chk] === false)) {
  4000. 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 }) };
  4001. return false;
  4002. }
  4003. return true;
  4004. },
  4005. /**
  4006. * get the last error
  4007. * @name last_error()
  4008. * @return {Object}
  4009. */
  4010. last_error : function () {
  4011. return this._data.core.last_error;
  4012. },
  4013. /**
  4014. * move a node to a new parent
  4015. * @name move_node(obj, par [, pos, callback, is_loaded])
  4016. * @param {mixed} obj the node to move, pass an array to move multiple nodes
  4017. * @param {mixed} par the new parent
  4018. * @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`
  4019. * @param {function} callback a function to call once the move is completed, receives 3 arguments - the node, the new parent and the position
  4020. * @param {Boolean} is_loaded internal parameter indicating if the parent node has been loaded
  4021. * @param {Boolean} skip_redraw internal parameter indicating if the tree should be redrawn
  4022. * @param {Boolean} instance internal parameter indicating if the node comes from another instance
  4023. * @trigger move_node.jstree
  4024. */
  4025. move_node : function (obj, par, pos, callback, is_loaded, skip_redraw, origin) {
  4026. var t1, t2, old_par, old_pos, new_par, old_ins, is_multi, dpc, tmp, i, j, k, l, p;
  4027. par = this.get_node(par);
  4028. pos = pos === undefined ? 0 : pos;
  4029. if(!par) { return false; }
  4030. if(!pos.toString().match(/^(before|after)$/) && !is_loaded && !this.is_loaded(par)) {
  4031. return this.load_node(par, function () { this.move_node(obj, par, pos, callback, true, false, origin); });
  4032. }
  4033. if($.isArray(obj)) {
  4034. if(obj.length === 1) {
  4035. obj = obj[0];
  4036. }
  4037. else {
  4038. //obj = obj.slice();
  4039. for(t1 = 0, t2 = obj.length; t1 < t2; t1++) {
  4040. if((tmp = this.move_node(obj[t1], par, pos, callback, is_loaded, false, origin))) {
  4041. par = tmp;
  4042. pos = "after";
  4043. }
  4044. }
  4045. this.redraw();
  4046. return true;
  4047. }
  4048. }
  4049. obj = obj && obj.id ? obj : this.get_node(obj);
  4050. if(!obj || obj.id === $.jstree.root) { return false; }
  4051. old_par = (obj.parent || $.jstree.root).toString();
  4052. new_par = (!pos.toString().match(/^(before|after)$/) || par.id === $.jstree.root) ? par : this.get_node(par.parent);
  4053. old_ins = origin ? origin : (this._model.data[obj.id] ? this : $.jstree.reference(obj.id));
  4054. is_multi = !old_ins || !old_ins._id || (this._id !== old_ins._id);
  4055. 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;
  4056. if(old_ins && old_ins._id) {
  4057. obj = old_ins._model.data[obj.id];
  4058. }
  4059. if(is_multi) {
  4060. if((tmp = this.copy_node(obj, par, pos, callback, is_loaded, false, origin))) {
  4061. if(old_ins) { old_ins.delete_node(obj); }
  4062. return tmp;
  4063. }
  4064. return false;
  4065. }
  4066. //var m = this._model.data;
  4067. if(par.id === $.jstree.root) {
  4068. if(pos === "before") { pos = "first"; }
  4069. if(pos === "after") { pos = "last"; }
  4070. }
  4071. switch(pos) {
  4072. case "before":
  4073. pos = $.inArray(par.id, new_par.children);
  4074. break;
  4075. case "after" :
  4076. pos = $.inArray(par.id, new_par.children) + 1;
  4077. break;
  4078. case "inside":
  4079. case "first":
  4080. pos = 0;
  4081. break;
  4082. case "last":
  4083. pos = new_par.children.length;
  4084. break;
  4085. default:
  4086. if(!pos) { pos = 0; }
  4087. break;
  4088. }
  4089. if(pos > new_par.children.length) { pos = new_par.children.length; }
  4090. 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) })) {
  4091. this.settings.core.error.call(this, this._data.core.last_error);
  4092. return false;
  4093. }
  4094. if(obj.parent === new_par.id) {
  4095. dpc = new_par.children.concat();
  4096. tmp = $.inArray(obj.id, dpc);
  4097. if(tmp !== -1) {
  4098. dpc = $.vakata.array_remove(dpc, tmp);
  4099. if(pos > tmp) { pos--; }
  4100. }
  4101. tmp = [];
  4102. for(i = 0, j = dpc.length; i < j; i++) {
  4103. tmp[i >= pos ? i+1 : i] = dpc[i];
  4104. }
  4105. tmp[pos] = obj.id;
  4106. new_par.children = tmp;
  4107. this._node_changed(new_par.id);
  4108. this.redraw(new_par.id === $.jstree.root);
  4109. }
  4110. else {
  4111. // clean old parent and up
  4112. tmp = obj.children_d.concat();
  4113. tmp.push(obj.id);
  4114. for(i = 0, j = obj.parents.length; i < j; i++) {
  4115. dpc = [];
  4116. p = old_ins._model.data[obj.parents[i]].children_d;
  4117. for(k = 0, l = p.length; k < l; k++) {
  4118. if($.inArray(p[k], tmp) === -1) {
  4119. dpc.push(p[k]);
  4120. }
  4121. }
  4122. old_ins._model.data[obj.parents[i]].children_d = dpc;
  4123. }
  4124. old_ins._model.data[old_par].children = $.vakata.array_remove_item(old_ins._model.data[old_par].children, obj.id);
  4125. // insert into new parent and up
  4126. for(i = 0, j = new_par.parents.length; i < j; i++) {
  4127. this._model.data[new_par.parents[i]].children_d = this._model.data[new_par.parents[i]].children_d.concat(tmp);
  4128. }
  4129. dpc = [];
  4130. for(i = 0, j = new_par.children.length; i < j; i++) {
  4131. dpc[i >= pos ? i+1 : i] = new_par.children[i];
  4132. }
  4133. dpc[pos] = obj.id;
  4134. new_par.children = dpc;
  4135. new_par.children_d.push(obj.id);
  4136. new_par.children_d = new_par.children_d.concat(obj.children_d);
  4137. // update object
  4138. obj.parent = new_par.id;
  4139. tmp = new_par.parents.concat();
  4140. tmp.unshift(new_par.id);
  4141. p = obj.parents.length;
  4142. obj.parents = tmp;
  4143. // update object children
  4144. tmp = tmp.concat();
  4145. for(i = 0, j = obj.children_d.length; i < j; i++) {
  4146. this._model.data[obj.children_d[i]].parents = this._model.data[obj.children_d[i]].parents.slice(0,p*-1);
  4147. Array.prototype.push.apply(this._model.data[obj.children_d[i]].parents, tmp);
  4148. }
  4149. if(old_par === $.jstree.root || new_par.id === $.jstree.root) {
  4150. this._model.force_full_redraw = true;
  4151. }
  4152. if(!this._model.force_full_redraw) {
  4153. this._node_changed(old_par);
  4154. this._node_changed(new_par.id);
  4155. }
  4156. if(!skip_redraw) {
  4157. this.redraw();
  4158. }
  4159. }
  4160. if(callback) { callback.call(this, obj, new_par, pos); }
  4161. /**
  4162. * triggered when a node is moved
  4163. * @event
  4164. * @name move_node.jstree
  4165. * @param {Object} node
  4166. * @param {String} parent the parent's ID
  4167. * @param {Number} position the position of the node among the parent's children
  4168. * @param {String} old_parent the old parent of the node
  4169. * @param {Number} old_position the old position of the node
  4170. * @param {Boolean} is_multi do the node and new parent belong to different instances
  4171. * @param {jsTree} old_instance the instance the node came from
  4172. * @param {jsTree} new_instance the instance of the new parent
  4173. */
  4174. 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 });
  4175. return obj.id;
  4176. },
  4177. /**
  4178. * copy a node to a new parent
  4179. * @name copy_node(obj, par [, pos, callback, is_loaded])
  4180. * @param {mixed} obj the node to copy, pass an array to copy multiple nodes
  4181. * @param {mixed} par the new parent
  4182. * @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`
  4183. * @param {function} callback a function to call once the move is completed, receives 3 arguments - the node, the new parent and the position
  4184. * @param {Boolean} is_loaded internal parameter indicating if the parent node has been loaded
  4185. * @param {Boolean} skip_redraw internal parameter indicating if the tree should be redrawn
  4186. * @param {Boolean} instance internal parameter indicating if the node comes from another instance
  4187. * @trigger model.jstree copy_node.jstree
  4188. */
  4189. copy_node : function (obj, par, pos, callback, is_loaded, skip_redraw, origin) {
  4190. var t1, t2, dpc, tmp, i, j, node, old_par, new_par, old_ins, is_multi;
  4191. par = this.get_node(par);
  4192. pos = pos === undefined ? 0 : pos;
  4193. if(!par) { return false; }
  4194. if(!pos.toString().match(/^(before|after)$/) && !is_loaded && !this.is_loaded(par)) {
  4195. return this.load_node(par, function () { this.copy_node(obj, par, pos, callback, true, false, origin); });
  4196. }
  4197. if($.isArray(obj)) {
  4198. if(obj.length === 1) {
  4199. obj = obj[0];
  4200. }
  4201. else {
  4202. //obj = obj.slice();
  4203. for(t1 = 0, t2 = obj.length; t1 < t2; t1++) {
  4204. if((tmp = this.copy_node(obj[t1], par, pos, callback, is_loaded, true, origin))) {
  4205. par = tmp;
  4206. pos = "after";
  4207. }
  4208. }
  4209. this.redraw();
  4210. return true;
  4211. }
  4212. }
  4213. obj = obj && obj.id ? obj : this.get_node(obj);
  4214. if(!obj || obj.id === $.jstree.root) { return false; }
  4215. old_par = (obj.parent || $.jstree.root).toString();
  4216. new_par = (!pos.toString().match(/^(before|after)$/) || par.id === $.jstree.root) ? par : this.get_node(par.parent);
  4217. old_ins = origin ? origin : (this._model.data[obj.id] ? this : $.jstree.reference(obj.id));
  4218. is_multi = !old_ins || !old_ins._id || (this._id !== old_ins._id);
  4219. if(old_ins && old_ins._id) {
  4220. obj = old_ins._model.data[obj.id];
  4221. }
  4222. if(par.id === $.jstree.root) {
  4223. if(pos === "before") { pos = "first"; }
  4224. if(pos === "after") { pos = "last"; }
  4225. }
  4226. switch(pos) {
  4227. case "before":
  4228. pos = $.inArray(par.id, new_par.children);
  4229. break;
  4230. case "after" :
  4231. pos = $.inArray(par.id, new_par.children) + 1;
  4232. break;
  4233. case "inside":
  4234. case "first":
  4235. pos = 0;
  4236. break;
  4237. case "last":
  4238. pos = new_par.children.length;
  4239. break;
  4240. default:
  4241. if(!pos) { pos = 0; }
  4242. break;
  4243. }
  4244. if(pos > new_par.children.length) { pos = new_par.children.length; }
  4245. 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) })) {
  4246. this.settings.core.error.call(this, this._data.core.last_error);
  4247. return false;
  4248. }
  4249. node = old_ins ? old_ins.get_json(obj, { no_id : true, no_data : true, no_state : true }) : obj;
  4250. if(!node) { return false; }
  4251. if(node.id === true) { delete node.id; }
  4252. node = this._parse_model_from_json(node, new_par.id, new_par.parents.concat());
  4253. if(!node) { return false; }
  4254. tmp = this.get_node(node);
  4255. if(obj && obj.state && obj.state.loaded === false) { tmp.state.loaded = false; }
  4256. dpc = [];
  4257. dpc.push(node);
  4258. dpc = dpc.concat(tmp.children_d);
  4259. this.trigger('model', { "nodes" : dpc, "parent" : new_par.id });
  4260. // insert into new parent and up
  4261. for(i = 0, j = new_par.parents.length; i < j; i++) {
  4262. this._model.data[new_par.parents[i]].children_d = this._model.data[new_par.parents[i]].children_d.concat(dpc);
  4263. }
  4264. dpc = [];
  4265. for(i = 0, j = new_par.children.length; i < j; i++) {
  4266. dpc[i >= pos ? i+1 : i] = new_par.children[i];
  4267. }
  4268. dpc[pos] = tmp.id;
  4269. new_par.children = dpc;
  4270. new_par.children_d.push(tmp.id);
  4271. new_par.children_d = new_par.children_d.concat(tmp.children_d);
  4272. if(new_par.id === $.jstree.root) {
  4273. this._model.force_full_redraw = true;
  4274. }
  4275. if(!this._model.force_full_redraw) {
  4276. this._node_changed(new_par.id);
  4277. }
  4278. if(!skip_redraw) {
  4279. this.redraw(new_par.id === $.jstree.root);
  4280. }
  4281. if(callback) { callback.call(this, tmp, new_par, pos); }
  4282. /**
  4283. * triggered when a node is copied
  4284. * @event
  4285. * @name copy_node.jstree
  4286. * @param {Object} node the copied node
  4287. * @param {Object} original the original node
  4288. * @param {String} parent the parent's ID
  4289. * @param {Number} position the position of the node among the parent's children
  4290. * @param {String} old_parent the old parent of the node
  4291. * @param {Number} old_position the position of the original node
  4292. * @param {Boolean} is_multi do the node and new parent belong to different instances
  4293. * @param {jsTree} old_instance the instance the node came from
  4294. * @param {jsTree} new_instance the instance of the new parent
  4295. */
  4296. 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 });
  4297. return tmp.id;
  4298. },
  4299. /**
  4300. * cut a node (a later call to `paste(obj)` would move the node)
  4301. * @name cut(obj)
  4302. * @param {mixed} obj multiple objects can be passed using an array
  4303. * @trigger cut.jstree
  4304. */
  4305. cut : function (obj) {
  4306. if(!obj) { obj = this._data.core.selected.concat(); }
  4307. if(!$.isArray(obj)) { obj = [obj]; }
  4308. if(!obj.length) { return false; }
  4309. var tmp = [], o, t1, t2;
  4310. for(t1 = 0, t2 = obj.length; t1 < t2; t1++) {
  4311. o = this.get_node(obj[t1]);
  4312. if(o && o.id && o.id !== $.jstree.root) { tmp.push(o); }
  4313. }
  4314. if(!tmp.length) { return false; }
  4315. ccp_node = tmp;
  4316. ccp_inst = this;
  4317. ccp_mode = 'move_node';
  4318. /**
  4319. * triggered when nodes are added to the buffer for moving
  4320. * @event
  4321. * @name cut.jstree
  4322. * @param {Array} node
  4323. */
  4324. this.trigger('cut', { "node" : obj });
  4325. },
  4326. /**
  4327. * copy a node (a later call to `paste(obj)` would copy the node)
  4328. * @name copy(obj)
  4329. * @param {mixed} obj multiple objects can be passed using an array
  4330. * @trigger copy.jstree
  4331. */
  4332. copy : function (obj) {
  4333. if(!obj) { obj = this._data.core.selected.concat(); }
  4334. if(!$.isArray(obj)) { obj = [obj]; }
  4335. if(!obj.length) { return false; }
  4336. var tmp = [], o, t1, t2;
  4337. for(t1 = 0, t2 = obj.length; t1 < t2; t1++) {
  4338. o = this.get_node(obj[t1]);
  4339. if(o && o.id && o.id !== $.jstree.root) { tmp.push(o); }
  4340. }
  4341. if(!tmp.length) { return false; }
  4342. ccp_node = tmp;
  4343. ccp_inst = this;
  4344. ccp_mode = 'copy_node';
  4345. /**
  4346. * triggered when nodes are added to the buffer for copying
  4347. * @event
  4348. * @name copy.jstree
  4349. * @param {Array} node
  4350. */
  4351. this.trigger('copy', { "node" : obj });
  4352. },
  4353. /**
  4354. * get the current buffer (any nodes that are waiting for a paste operation)
  4355. * @name get_buffer()
  4356. * @return {Object} an object consisting of `mode` ("copy_node" or "move_node"), `node` (an array of objects) and `inst` (the instance)
  4357. */
  4358. get_buffer : function () {
  4359. return { 'mode' : ccp_mode, 'node' : ccp_node, 'inst' : ccp_inst };
  4360. },
  4361. /**
  4362. * check if there is something in the buffer to paste
  4363. * @name can_paste()
  4364. * @return {Boolean}
  4365. */
  4366. can_paste : function () {
  4367. return ccp_mode !== false && ccp_node !== false; // && ccp_inst._model.data[ccp_node];
  4368. },
  4369. /**
  4370. * copy or move the previously cut or copied nodes to a new parent
  4371. * @name paste(obj [, pos])
  4372. * @param {mixed} obj the new parent
  4373. * @param {mixed} pos the position to insert at (besides integer, "first" and "last" are supported), defaults to integer `0`
  4374. * @trigger paste.jstree
  4375. */
  4376. paste : function (obj, pos) {
  4377. obj = this.get_node(obj);
  4378. if(!obj || !ccp_mode || !ccp_mode.match(/^(copy_node|move_node)$/) || !ccp_node) { return false; }
  4379. if(this[ccp_mode](ccp_node, obj, pos, false, false, false, ccp_inst)) {
  4380. /**
  4381. * triggered when paste is invoked
  4382. * @event
  4383. * @name paste.jstree
  4384. * @param {String} parent the ID of the receiving node
  4385. * @param {Array} node the nodes in the buffer
  4386. * @param {String} mode the performed operation - "copy_node" or "move_node"
  4387. */
  4388. this.trigger('paste', { "parent" : obj.id, "node" : ccp_node, "mode" : ccp_mode });
  4389. }
  4390. ccp_node = false;
  4391. ccp_mode = false;
  4392. ccp_inst = false;
  4393. },
  4394. /**
  4395. * clear the buffer of previously copied or cut nodes
  4396. * @name clear_buffer()
  4397. * @trigger clear_buffer.jstree
  4398. */
  4399. clear_buffer : function () {
  4400. ccp_node = false;
  4401. ccp_mode = false;
  4402. ccp_inst = false;
  4403. /**
  4404. * triggered when the copy / cut buffer is cleared
  4405. * @event
  4406. * @name clear_buffer.jstree
  4407. */
  4408. this.trigger('clear_buffer');
  4409. },
  4410. /**
  4411. * put a node in edit mode (input field to rename the node)
  4412. * @name edit(obj [, default_text, callback])
  4413. * @param {mixed} obj
  4414. * @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)
  4415. * @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
  4416. */
  4417. edit : function (obj, default_text, callback) {
  4418. var rtl, w, a, s, t, h1, h2, fn, tmp, cancel = false;
  4419. obj = this.get_node(obj);
  4420. if(!obj) { return false; }
  4421. if(!this.check("edit", obj, this.get_parent(obj))) {
  4422. this.settings.core.error.call(this, this._data.core.last_error);
  4423. return false;
  4424. }
  4425. tmp = obj;
  4426. default_text = typeof default_text === 'string' ? default_text : obj.text;
  4427. this.set_text(obj, "");
  4428. obj = this._open_to(obj);
  4429. tmp.text = default_text;
  4430. rtl = this._data.core.rtl;
  4431. w = this.element.width();
  4432. this._data.core.focused = tmp.id;
  4433. a = obj.children('.jstree-anchor').focus();
  4434. s = $('<span>');
  4435. /*!
  4436. oi = obj.children("i:visible"),
  4437. ai = a.children("i:visible"),
  4438. w1 = oi.width() * oi.length,
  4439. w2 = ai.width() * ai.length,
  4440. */
  4441. t = default_text;
  4442. h1 = $("<"+"div />", { css : { "position" : "absolute", "top" : "-200px", "left" : (rtl ? "0px" : "-1000px"), "visibility" : "hidden" } }).appendTo(document.body);
  4443. h2 = $("<"+"input />", {
  4444. "value" : t,
  4445. "class" : "jstree-rename-input",
  4446. // "size" : t.length,
  4447. "css" : {
  4448. "padding" : "0",
  4449. "border" : "1px solid silver",
  4450. "box-sizing" : "border-box",
  4451. "display" : "inline-block",
  4452. "height" : (this._data.core.li_height) + "px",
  4453. "lineHeight" : (this._data.core.li_height) + "px",
  4454. "width" : "150px" // will be set a bit further down
  4455. },
  4456. "blur" : $.proxy(function (e) {
  4457. e.stopImmediatePropagation();
  4458. e.preventDefault();
  4459. var i = s.children(".jstree-rename-input"),
  4460. v = i.val(),
  4461. f = this.settings.core.force_text,
  4462. nv;
  4463. if(v === "") { v = t; }
  4464. h1.remove();
  4465. s.replaceWith(a);
  4466. s.remove();
  4467. t = f ? t : $('<div></div>').append($.parseHTML(t)).html();
  4468. obj = this.get_node(obj);
  4469. this.set_text(obj, t);
  4470. nv = !!this.rename_node(obj, f ? $('<div></div>').text(v).text() : $('<div></div>').append($.parseHTML(v)).html());
  4471. if(!nv) {
  4472. this.set_text(obj, t); // move this up? and fix #483
  4473. }
  4474. this._data.core.focused = tmp.id;
  4475. setTimeout($.proxy(function () {
  4476. var node = this.get_node(tmp.id, true);
  4477. if(node.length) {
  4478. this._data.core.focused = tmp.id;
  4479. node.children('.jstree-anchor').focus();
  4480. }
  4481. }, this), 0);
  4482. if(callback) {
  4483. callback.call(this, tmp, nv, cancel);
  4484. }
  4485. h2 = null;
  4486. }, this),
  4487. "keydown" : function (e) {
  4488. var key = e.which;
  4489. if(key === 27) {
  4490. cancel = true;
  4491. this.value = t;
  4492. }
  4493. if(key === 27 || key === 13 || key === 37 || key === 38 || key === 39 || key === 40 || key === 32) {
  4494. e.stopImmediatePropagation();
  4495. }
  4496. if(key === 27 || key === 13) {
  4497. e.preventDefault();
  4498. this.blur();
  4499. }
  4500. },
  4501. "click" : function (e) { e.stopImmediatePropagation(); },
  4502. "mousedown" : function (e) { e.stopImmediatePropagation(); },
  4503. "keyup" : function (e) {
  4504. h2.width(Math.min(h1.text("pW" + this.value).width(),w));
  4505. },
  4506. "keypress" : function(e) {
  4507. if(e.which === 13) { return false; }
  4508. }
  4509. });
  4510. fn = {
  4511. fontFamily : a.css('fontFamily') || '',
  4512. fontSize : a.css('fontSize') || '',
  4513. fontWeight : a.css('fontWeight') || '',
  4514. fontStyle : a.css('fontStyle') || '',
  4515. fontStretch : a.css('fontStretch') || '',
  4516. fontVariant : a.css('fontVariant') || '',
  4517. letterSpacing : a.css('letterSpacing') || '',
  4518. wordSpacing : a.css('wordSpacing') || ''
  4519. };
  4520. s.attr('class', a.attr('class')).append(a.contents().clone()).append(h2);
  4521. a.replaceWith(s);
  4522. h1.css(fn);
  4523. h2.css(fn).width(Math.min(h1.text("pW" + h2[0].value).width(),w))[0].select();
  4524. $(document).one('mousedown.jstree touchstart.jstree dnd_start.vakata', function (e) {
  4525. if (h2 && e.target !== h2) {
  4526. $(h2).blur();
  4527. }
  4528. });
  4529. },
  4530. /**
  4531. * changes the theme
  4532. * @name set_theme(theme_name [, theme_url])
  4533. * @param {String} theme_name the name of the new theme to apply
  4534. * @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.
  4535. * @trigger set_theme.jstree
  4536. */
  4537. set_theme : function (theme_name, theme_url) {
  4538. if(!theme_name) { return false; }
  4539. if(theme_url === true) {
  4540. var dir = this.settings.core.themes.dir;
  4541. if(!dir) { dir = $.jstree.path + '/themes'; }
  4542. theme_url = dir + '/' + theme_name + '/style.css';
  4543. }
  4544. if(theme_url && $.inArray(theme_url, themes_loaded) === -1) {
  4545. $('head').append('<'+'link rel="stylesheet" href="' + theme_url + '" type="text/css" />');
  4546. themes_loaded.push(theme_url);
  4547. }
  4548. if(this._data.core.themes.name) {
  4549. this.element.removeClass('jstree-' + this._data.core.themes.name);
  4550. }
  4551. this._data.core.themes.name = theme_name;
  4552. this.element.addClass('jstree-' + theme_name);
  4553. this.element[this.settings.core.themes.responsive ? 'addClass' : 'removeClass' ]('jstree-' + theme_name + '-responsive');
  4554. /**
  4555. * triggered when a theme is set
  4556. * @event
  4557. * @name set_theme.jstree
  4558. * @param {String} theme the new theme
  4559. */
  4560. this.trigger('set_theme', { 'theme' : theme_name });
  4561. },
  4562. /**
  4563. * gets the name of the currently applied theme name
  4564. * @name get_theme()
  4565. * @return {String}
  4566. */
  4567. get_theme : function () { return this._data.core.themes.name; },
  4568. /**
  4569. * changes the theme variant (if the theme has variants)
  4570. * @name set_theme_variant(variant_name)
  4571. * @param {String|Boolean} variant_name the variant to apply (if `false` is used the current variant is removed)
  4572. */
  4573. set_theme_variant : function (variant_name) {
  4574. if(this._data.core.themes.variant) {
  4575. this.element.removeClass('jstree-' + this._data.core.themes.name + '-' + this._data.core.themes.variant);
  4576. }
  4577. this._data.core.themes.variant = variant_name;
  4578. if(variant_name) {
  4579. this.element.addClass('jstree-' + this._data.core.themes.name + '-' + this._data.core.themes.variant);
  4580. }
  4581. },
  4582. /**
  4583. * gets the name of the currently applied theme variant
  4584. * @name get_theme()
  4585. * @return {String}
  4586. */
  4587. get_theme_variant : function () { return this._data.core.themes.variant; },
  4588. /**
  4589. * shows a striped background on the container (if the theme supports it)
  4590. * @name show_stripes()
  4591. */
  4592. show_stripes : function () {
  4593. this._data.core.themes.stripes = true;
  4594. this.get_container_ul().addClass("jstree-striped");
  4595. /**
  4596. * triggered when stripes are shown
  4597. * @event
  4598. * @name show_stripes.jstree
  4599. */
  4600. this.trigger('show_stripes');
  4601. },
  4602. /**
  4603. * hides the striped background on the container
  4604. * @name hide_stripes()
  4605. */
  4606. hide_stripes : function () {
  4607. this._data.core.themes.stripes = false;
  4608. this.get_container_ul().removeClass("jstree-striped");
  4609. /**
  4610. * triggered when stripes are hidden
  4611. * @event
  4612. * @name hide_stripes.jstree
  4613. */
  4614. this.trigger('hide_stripes');
  4615. },
  4616. /**
  4617. * toggles the striped background on the container
  4618. * @name toggle_stripes()
  4619. */
  4620. toggle_stripes : function () { if(this._data.core.themes.stripes) { this.hide_stripes(); } else { this.show_stripes(); } },
  4621. /**
  4622. * shows the connecting dots (if the theme supports it)
  4623. * @name show_dots()
  4624. */
  4625. show_dots : function () {
  4626. this._data.core.themes.dots = true;
  4627. this.get_container_ul().removeClass("jstree-no-dots");
  4628. /**
  4629. * triggered when dots are shown
  4630. * @event
  4631. * @name show_dots.jstree
  4632. */
  4633. this.trigger('show_dots');
  4634. },
  4635. /**
  4636. * hides the connecting dots
  4637. * @name hide_dots()
  4638. */
  4639. hide_dots : function () {
  4640. this._data.core.themes.dots = false;
  4641. this.get_container_ul().addClass("jstree-no-dots");
  4642. /**
  4643. * triggered when dots are hidden
  4644. * @event
  4645. * @name hide_dots.jstree
  4646. */
  4647. this.trigger('hide_dots');
  4648. },
  4649. /**
  4650. * toggles the connecting dots
  4651. * @name toggle_dots()
  4652. */
  4653. toggle_dots : function () { if(this._data.core.themes.dots) { this.hide_dots(); } else { this.show_dots(); } },
  4654. /**
  4655. * show the node icons
  4656. * @name show_icons()
  4657. */
  4658. show_icons : function () {
  4659. this._data.core.themes.icons = true;
  4660. this.get_container_ul().removeClass("jstree-no-icons");
  4661. /**
  4662. * triggered when icons are shown
  4663. * @event
  4664. * @name show_icons.jstree
  4665. */
  4666. this.trigger('show_icons');
  4667. },
  4668. /**
  4669. * hide the node icons
  4670. * @name hide_icons()
  4671. */
  4672. hide_icons : function () {
  4673. this._data.core.themes.icons = false;
  4674. this.get_container_ul().addClass("jstree-no-icons");
  4675. /**
  4676. * triggered when icons are hidden
  4677. * @event
  4678. * @name hide_icons.jstree
  4679. */
  4680. this.trigger('hide_icons');
  4681. },
  4682. /**
  4683. * toggle the node icons
  4684. * @name toggle_icons()
  4685. */
  4686. toggle_icons : function () { if(this._data.core.themes.icons) { this.hide_icons(); } else { this.show_icons(); } },
  4687. /**
  4688. * show the node ellipsis
  4689. * @name show_icons()
  4690. */
  4691. show_ellipsis : function () {
  4692. this._data.core.themes.ellipsis = true;
  4693. this.get_container_ul().addClass("jstree-ellipsis");
  4694. /**
  4695. * triggered when ellisis is shown
  4696. * @event
  4697. * @name show_ellipsis.jstree
  4698. */
  4699. this.trigger('show_ellipsis');
  4700. },
  4701. /**
  4702. * hide the node ellipsis
  4703. * @name hide_ellipsis()
  4704. */
  4705. hide_ellipsis : function () {
  4706. this._data.core.themes.ellipsis = false;
  4707. this.get_container_ul().removeClass("jstree-ellipsis");
  4708. /**
  4709. * triggered when ellisis is hidden
  4710. * @event
  4711. * @name hide_ellipsis.jstree
  4712. */
  4713. this.trigger('hide_ellipsis');
  4714. },
  4715. /**
  4716. * toggle the node ellipsis
  4717. * @name toggle_icons()
  4718. */
  4719. toggle_ellipsis : function () { if(this._data.core.themes.ellipsis) { this.hide_ellipsis(); } else { this.show_ellipsis(); } },
  4720. /**
  4721. * set the node icon for a node
  4722. * @name set_icon(obj, icon)
  4723. * @param {mixed} obj
  4724. * @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
  4725. */
  4726. set_icon : function (obj, icon) {
  4727. var t1, t2, dom, old;
  4728. if($.isArray(obj)) {
  4729. obj = obj.slice();
  4730. for(t1 = 0, t2 = obj.length; t1 < t2; t1++) {
  4731. this.set_icon(obj[t1], icon);
  4732. }
  4733. return true;
  4734. }
  4735. obj = this.get_node(obj);
  4736. if(!obj || obj.id === $.jstree.root) { return false; }
  4737. old = obj.icon;
  4738. obj.icon = icon === true || icon === null || icon === undefined || icon === '' ? true : icon;
  4739. dom = this.get_node(obj, true).children(".jstree-anchor").children(".jstree-themeicon");
  4740. if(icon === false) {
  4741. dom.removeClass('jstree-themeicon-custom ' + old).css("background","").removeAttr("rel");
  4742. this.hide_icon(obj);
  4743. }
  4744. else if(icon === true || icon === null || icon === undefined || icon === '') {
  4745. dom.removeClass('jstree-themeicon-custom ' + old).css("background","").removeAttr("rel");
  4746. if(old === false) { this.show_icon(obj); }
  4747. }
  4748. else if(icon.indexOf("/") === -1 && icon.indexOf(".") === -1) {
  4749. dom.removeClass(old).css("background","");
  4750. dom.addClass(icon + ' jstree-themeicon-custom').attr("rel",icon);
  4751. if(old === false) { this.show_icon(obj); }
  4752. }
  4753. else {
  4754. dom.removeClass(old).css("background","");
  4755. dom.addClass('jstree-themeicon-custom').css("background", "url('" + icon + "') center center no-repeat").attr("rel",icon);
  4756. if(old === false) { this.show_icon(obj); }
  4757. }
  4758. return true;
  4759. },
  4760. /**
  4761. * get the node icon for a node
  4762. * @name get_icon(obj)
  4763. * @param {mixed} obj
  4764. * @return {String}
  4765. */
  4766. get_icon : function (obj) {
  4767. obj = this.get_node(obj);
  4768. return (!obj || obj.id === $.jstree.root) ? false : obj.icon;
  4769. },
  4770. /**
  4771. * hide the icon on an individual node
  4772. * @name hide_icon(obj)
  4773. * @param {mixed} obj
  4774. */
  4775. hide_icon : function (obj) {
  4776. var t1, t2;
  4777. if($.isArray(obj)) {
  4778. obj = obj.slice();
  4779. for(t1 = 0, t2 = obj.length; t1 < t2; t1++) {
  4780. this.hide_icon(obj[t1]);
  4781. }
  4782. return true;
  4783. }
  4784. obj = this.get_node(obj);
  4785. if(!obj || obj === $.jstree.root) { return false; }
  4786. obj.icon = false;
  4787. this.get_node(obj, true).children(".jstree-anchor").children(".jstree-themeicon").addClass('jstree-themeicon-hidden');
  4788. return true;
  4789. },
  4790. /**
  4791. * show the icon on an individual node
  4792. * @name show_icon(obj)
  4793. * @param {mixed} obj
  4794. */
  4795. show_icon : function (obj) {
  4796. var t1, t2, dom;
  4797. if($.isArray(obj)) {
  4798. obj = obj.slice();
  4799. for(t1 = 0, t2 = obj.length; t1 < t2; t1++) {
  4800. this.show_icon(obj[t1]);
  4801. }
  4802. return true;
  4803. }
  4804. obj = this.get_node(obj);
  4805. if(!obj || obj === $.jstree.root) { return false; }
  4806. dom = this.get_node(obj, true);
  4807. obj.icon = dom.length ? dom.children(".jstree-anchor").children(".jstree-themeicon").attr('rel') : true;
  4808. if(!obj.icon) { obj.icon = true; }
  4809. dom.children(".jstree-anchor").children(".jstree-themeicon").removeClass('jstree-themeicon-hidden');
  4810. return true;
  4811. }
  4812. };
  4813. // helpers
  4814. $.vakata = {};
  4815. // collect attributes
  4816. $.vakata.attributes = function(node, with_values) {
  4817. node = $(node)[0];
  4818. var attr = with_values ? {} : [];
  4819. if(node && node.attributes) {
  4820. $.each(node.attributes, function (i, v) {
  4821. if($.inArray(v.name.toLowerCase(),['style','contenteditable','hasfocus','tabindex']) !== -1) { return; }
  4822. if(v.value !== null && $.trim(v.value) !== '') {
  4823. if(with_values) { attr[v.name] = v.value; }
  4824. else { attr.push(v.name); }
  4825. }
  4826. });
  4827. }
  4828. return attr;
  4829. };
  4830. $.vakata.array_unique = function(array) {
  4831. var a = [], i, j, l, o = {};
  4832. for(i = 0, l = array.length; i < l; i++) {
  4833. if(o[array[i]] === undefined) {
  4834. a.push(array[i]);
  4835. o[array[i]] = true;
  4836. }
  4837. }
  4838. return a;
  4839. };
  4840. // remove item from array
  4841. $.vakata.array_remove = function(array, from) {
  4842. array.splice(from, 1);
  4843. return array;
  4844. //var rest = array.slice((to || from) + 1 || array.length);
  4845. //array.length = from < 0 ? array.length + from : from;
  4846. //array.push.apply(array, rest);
  4847. //return array;
  4848. };
  4849. // remove item from array
  4850. $.vakata.array_remove_item = function(array, item) {
  4851. var tmp = $.inArray(item, array);
  4852. return tmp !== -1 ? $.vakata.array_remove(array, tmp) : array;
  4853. };
  4854. $.vakata.array_filter = function(c,a,b,d,e) {
  4855. if (c.filter) {
  4856. return c.filter(a, b);
  4857. }
  4858. d=[];
  4859. for (e in c) {
  4860. if (~~e+''===e+'' && e>=0 && a.call(b,c[e],+e,c)) {
  4861. d.push(c[e]);
  4862. }
  4863. }
  4864. return d;
  4865. };
  4866. /**
  4867. * ### Changed plugin
  4868. *
  4869. * This plugin adds more information to the `changed.jstree` event. The new data is contained in the `changed` event data property, and contains a lists of `selected` and `deselected` nodes.
  4870. */
  4871. $.jstree.plugins.changed = function (options, parent) {
  4872. var last = [];
  4873. this.trigger = function (ev, data) {
  4874. var i, j;
  4875. if(!data) {
  4876. data = {};
  4877. }
  4878. if(ev.replace('.jstree','') === 'changed') {
  4879. data.changed = { selected : [], deselected : [] };
  4880. var tmp = {};
  4881. for(i = 0, j = last.length; i < j; i++) {
  4882. tmp[last[i]] = 1;
  4883. }
  4884. for(i = 0, j = data.selected.length; i < j; i++) {
  4885. if(!tmp[data.selected[i]]) {
  4886. data.changed.selected.push(data.selected[i]);
  4887. }
  4888. else {
  4889. tmp[data.selected[i]] = 2;
  4890. }
  4891. }
  4892. for(i = 0, j = last.length; i < j; i++) {
  4893. if(tmp[last[i]] === 1) {
  4894. data.changed.deselected.push(last[i]);
  4895. }
  4896. }
  4897. last = data.selected.slice();
  4898. }
  4899. /**
  4900. * triggered when selection changes (the "changed" plugin enhances the original event with more data)
  4901. * @event
  4902. * @name changed.jstree
  4903. * @param {Object} node
  4904. * @param {Object} action the action that caused the selection to change
  4905. * @param {Array} selected the current selection
  4906. * @param {Object} changed an object containing two properties `selected` and `deselected` - both arrays of node IDs, which were selected or deselected since the last changed event
  4907. * @param {Object} event the event (if any) that triggered this changed event
  4908. * @plugin changed
  4909. */
  4910. parent.trigger.call(this, ev, data);
  4911. };
  4912. this.refresh = function (skip_loading, forget_state) {
  4913. last = [];
  4914. return parent.refresh.apply(this, arguments);
  4915. };
  4916. };
  4917. /**
  4918. * ### Checkbox plugin
  4919. *
  4920. * This plugin renders checkbox icons in front of each node, making multiple selection much easier.
  4921. * It also supports tri-state behavior, meaning that if a node has a few of its children checked it will be rendered as undetermined, and state will be propagated up.
  4922. */
  4923. var _i = document.createElement('I');
  4924. _i.className = 'jstree-icon jstree-checkbox';
  4925. _i.setAttribute('role', 'presentation');
  4926. /**
  4927. * stores all defaults for the checkbox plugin
  4928. * @name $.jstree.defaults.checkbox
  4929. * @plugin checkbox
  4930. */
  4931. $.jstree.defaults.checkbox = {
  4932. /**
  4933. * a boolean indicating if checkboxes should be visible (can be changed at a later time using `show_checkboxes()` and `hide_checkboxes`). Defaults to `true`.
  4934. * @name $.jstree.defaults.checkbox.visible
  4935. * @plugin checkbox
  4936. */
  4937. visible : true,
  4938. /**
  4939. * a boolean indicating if checkboxes should cascade down and have an undetermined state. Defaults to `true`.
  4940. * @name $.jstree.defaults.checkbox.three_state
  4941. * @plugin checkbox
  4942. */
  4943. three_state : true,
  4944. /**
  4945. * a boolean indicating if clicking anywhere on the node should act as clicking on the checkbox. Defaults to `true`.
  4946. * @name $.jstree.defaults.checkbox.whole_node
  4947. * @plugin checkbox
  4948. */
  4949. whole_node : true,
  4950. /**
  4951. * a boolean indicating if the selected style of a node should be kept, or removed. Defaults to `true`.
  4952. * @name $.jstree.defaults.checkbox.keep_selected_style
  4953. * @plugin checkbox
  4954. */
  4955. keep_selected_style : true,
  4956. /**
  4957. * This setting controls how cascading and undetermined nodes are applied.
  4958. * If 'up' is in the string - cascading up is enabled, if 'down' is in the string - cascading down is enabled, if 'undetermined' is in the string - undetermined nodes will be used.
  4959. * If `three_state` is set to `true` this setting is automatically set to 'up+down+undetermined'. Defaults to ''.
  4960. * @name $.jstree.defaults.checkbox.cascade
  4961. * @plugin checkbox
  4962. */
  4963. cascade : '',
  4964. /**
  4965. * This setting controls if checkbox are bound to the general tree selection or to an internal array maintained by the checkbox plugin. Defaults to `true`, only set to `false` if you know exactly what you are doing.
  4966. * @name $.jstree.defaults.checkbox.tie_selection
  4967. * @plugin checkbox
  4968. */
  4969. tie_selection : true,
  4970. /**
  4971. * This setting controls if cascading down affects disabled checkboxes
  4972. * @name $.jstree.defaults.checkbox.cascade_to_disabled
  4973. * @plugin checkbox
  4974. */
  4975. cascade_to_disabled : true,
  4976. /**
  4977. * This setting controls if cascading down affects hidden checkboxes
  4978. * @name $.jstree.defaults.checkbox.cascade_to_hidden
  4979. * @plugin checkbox
  4980. */
  4981. cascade_to_hidden : true
  4982. };
  4983. $.jstree.plugins.checkbox = function (options, parent) {
  4984. this.bind = function () {
  4985. parent.bind.call(this);
  4986. this._data.checkbox.uto = false;
  4987. this._data.checkbox.selected = [];
  4988. if(this.settings.checkbox.three_state) {
  4989. this.settings.checkbox.cascade = 'up+down+undetermined';
  4990. }
  4991. this.element
  4992. .on("init.jstree", $.proxy(function () {
  4993. this._data.checkbox.visible = this.settings.checkbox.visible;
  4994. if(!this.settings.checkbox.keep_selected_style) {
  4995. this.element.addClass('jstree-checkbox-no-clicked');
  4996. }
  4997. if(this.settings.checkbox.tie_selection) {
  4998. this.element.addClass('jstree-checkbox-selection');
  4999. }
  5000. }, this))
  5001. .on("loading.jstree", $.proxy(function () {
  5002. this[ this._data.checkbox.visible ? 'show_checkboxes' : 'hide_checkboxes' ]();
  5003. }, this));
  5004. if(this.settings.checkbox.cascade.indexOf('undetermined') !== -1) {
  5005. this.element
  5006. .on('changed.jstree uncheck_node.jstree check_node.jstree uncheck_all.jstree check_all.jstree move_node.jstree copy_node.jstree redraw.jstree open_node.jstree', $.proxy(function () {
  5007. // only if undetermined is in setting
  5008. if(this._data.checkbox.uto) { clearTimeout(this._data.checkbox.uto); }
  5009. this._data.checkbox.uto = setTimeout($.proxy(this._undetermined, this), 50);
  5010. }, this));
  5011. }
  5012. if(!this.settings.checkbox.tie_selection) {
  5013. this.element
  5014. .on('model.jstree', $.proxy(function (e, data) {
  5015. var m = this._model.data,
  5016. p = m[data.parent],
  5017. dpc = data.nodes,
  5018. i, j;
  5019. for(i = 0, j = dpc.length; i < j; i++) {
  5020. m[dpc[i]].state.checked = m[dpc[i]].state.checked || (m[dpc[i]].original && m[dpc[i]].original.state && m[dpc[i]].original.state.checked);
  5021. if(m[dpc[i]].state.checked) {
  5022. this._data.checkbox.selected.push(dpc[i]);
  5023. }
  5024. }
  5025. }, this));
  5026. }
  5027. if(this.settings.checkbox.cascade.indexOf('up') !== -1 || this.settings.checkbox.cascade.indexOf('down') !== -1) {
  5028. this.element
  5029. .on('model.jstree', $.proxy(function (e, data) {
  5030. var m = this._model.data,
  5031. p = m[data.parent],
  5032. dpc = data.nodes,
  5033. chd = [],
  5034. c, i, j, k, l, tmp, s = this.settings.checkbox.cascade, t = this.settings.checkbox.tie_selection;
  5035. if(s.indexOf('down') !== -1) {
  5036. // apply down
  5037. if(p.state[ t ? 'selected' : 'checked' ]) {
  5038. for(i = 0, j = dpc.length; i < j; i++) {
  5039. m[dpc[i]].state[ t ? 'selected' : 'checked' ] = true;
  5040. }
  5041. this._data[ t ? 'core' : 'checkbox' ].selected = this._data[ t ? 'core' : 'checkbox' ].selected.concat(dpc);
  5042. }
  5043. else {
  5044. for(i = 0, j = dpc.length; i < j; i++) {
  5045. if(m[dpc[i]].state[ t ? 'selected' : 'checked' ]) {
  5046. for(k = 0, l = m[dpc[i]].children_d.length; k < l; k++) {
  5047. m[m[dpc[i]].children_d[k]].state[ t ? 'selected' : 'checked' ] = true;
  5048. }
  5049. this._data[ t ? 'core' : 'checkbox' ].selected = this._data[ t ? 'core' : 'checkbox' ].selected.concat(m[dpc[i]].children_d);
  5050. }
  5051. }
  5052. }
  5053. }
  5054. if(s.indexOf('up') !== -1) {
  5055. // apply up
  5056. for(i = 0, j = p.children_d.length; i < j; i++) {
  5057. if(!m[p.children_d[i]].children.length) {
  5058. chd.push(m[p.children_d[i]].parent);
  5059. }
  5060. }
  5061. chd = $.vakata.array_unique(chd);
  5062. for(k = 0, l = chd.length; k < l; k++) {
  5063. p = m[chd[k]];
  5064. while(p && p.id !== $.jstree.root) {
  5065. c = 0;
  5066. for(i = 0, j = p.children.length; i < j; i++) {
  5067. c += m[p.children[i]].state[ t ? 'selected' : 'checked' ];
  5068. }
  5069. if(c === j) {
  5070. p.state[ t ? 'selected' : 'checked' ] = true;
  5071. this._data[ t ? 'core' : 'checkbox' ].selected.push(p.id);
  5072. tmp = this.get_node(p, true);
  5073. if(tmp && tmp.length) {
  5074. tmp.attr('aria-selected', true).children('.jstree-anchor').addClass( t ? 'jstree-clicked' : 'jstree-checked');
  5075. }
  5076. }
  5077. else {
  5078. break;
  5079. }
  5080. p = this.get_node(p.parent);
  5081. }
  5082. }
  5083. }
  5084. this._data[ t ? 'core' : 'checkbox' ].selected = $.vakata.array_unique(this._data[ t ? 'core' : 'checkbox' ].selected);
  5085. }, this))
  5086. .on(this.settings.checkbox.tie_selection ? 'select_node.jstree' : 'check_node.jstree', $.proxy(function (e, data) {
  5087. var self = this,
  5088. obj = data.node,
  5089. m = this._model.data,
  5090. par = this.get_node(obj.parent),
  5091. i, j, c, tmp, s = this.settings.checkbox.cascade, t = this.settings.checkbox.tie_selection,
  5092. sel = {}, cur = this._data[ t ? 'core' : 'checkbox' ].selected;
  5093. for (i = 0, j = cur.length; i < j; i++) {
  5094. sel[cur[i]] = true;
  5095. }
  5096. // apply down
  5097. if(s.indexOf('down') !== -1) {
  5098. //this._data[ t ? 'core' : 'checkbox' ].selected = $.vakata.array_unique(this._data[ t ? 'core' : 'checkbox' ].selected.concat(obj.children_d));
  5099. var selectedIds = this._cascade_new_checked_state(obj.id, true);
  5100. var temp = obj.children_d.concat(obj.id);
  5101. for (i = 0, j = temp.length; i < j; i++) {
  5102. if (selectedIds.indexOf(temp[i]) > -1) {
  5103. sel[temp[i]] = true;
  5104. }
  5105. else {
  5106. delete sel[temp[i]];
  5107. }
  5108. }
  5109. }
  5110. // apply up
  5111. if(s.indexOf('up') !== -1) {
  5112. while(par && par.id !== $.jstree.root) {
  5113. c = 0;
  5114. for(i = 0, j = par.children.length; i < j; i++) {
  5115. c += m[par.children[i]].state[ t ? 'selected' : 'checked' ];
  5116. }
  5117. if(c === j) {
  5118. par.state[ t ? 'selected' : 'checked' ] = true;
  5119. sel[par.id] = true;
  5120. //this._data[ t ? 'core' : 'checkbox' ].selected.push(par.id);
  5121. tmp = this.get_node(par, true);
  5122. if(tmp && tmp.length) {
  5123. tmp.attr('aria-selected', true).children('.jstree-anchor').addClass(t ? 'jstree-clicked' : 'jstree-checked');
  5124. }
  5125. }
  5126. else {
  5127. break;
  5128. }
  5129. par = this.get_node(par.parent);
  5130. }
  5131. }
  5132. cur = [];
  5133. for (i in sel) {
  5134. if (sel.hasOwnProperty(i)) {
  5135. cur.push(i);
  5136. }
  5137. }
  5138. this._data[ t ? 'core' : 'checkbox' ].selected = cur;
  5139. }, this))
  5140. .on(this.settings.checkbox.tie_selection ? 'deselect_all.jstree' : 'uncheck_all.jstree', $.proxy(function (e, data) {
  5141. var obj = this.get_node($.jstree.root),
  5142. m = this._model.data,
  5143. i, j, tmp;
  5144. for(i = 0, j = obj.children_d.length; i < j; i++) {
  5145. tmp = m[obj.children_d[i]];
  5146. if(tmp && tmp.original && tmp.original.state && tmp.original.state.undetermined) {
  5147. tmp.original.state.undetermined = false;
  5148. }
  5149. }
  5150. }, this))
  5151. .on(this.settings.checkbox.tie_selection ? 'deselect_node.jstree' : 'uncheck_node.jstree', $.proxy(function (e, data) {
  5152. var self = this,
  5153. obj = data.node,
  5154. dom = this.get_node(obj, true),
  5155. i, j, tmp, s = this.settings.checkbox.cascade, t = this.settings.checkbox.tie_selection,
  5156. cur = this._data[ t ? 'core' : 'checkbox' ].selected, sel = {},
  5157. stillSelectedIds = [],
  5158. allIds = obj.children_d.concat(obj.id);
  5159. // apply down
  5160. if(s.indexOf('down') !== -1) {
  5161. var selectedIds = this._cascade_new_checked_state(obj.id, false);
  5162. cur = cur.filter(function(id) {
  5163. return allIds.indexOf(id) === -1 || selectedIds.indexOf(id) > -1;
  5164. });
  5165. }
  5166. // only apply up if cascade up is enabled and if this node is not selected
  5167. // (if all child nodes are disabled and cascade_to_disabled === false then this node will till be selected).
  5168. if(s.indexOf('up') !== -1 && cur.indexOf(obj.id) === -1) {
  5169. for(i = 0, j = obj.parents.length; i < j; i++) {
  5170. tmp = this._model.data[obj.parents[i]];
  5171. tmp.state[ t ? 'selected' : 'checked' ] = false;
  5172. if(tmp && tmp.original && tmp.original.state && tmp.original.state.undetermined) {
  5173. tmp.original.state.undetermined = false;
  5174. }
  5175. tmp = this.get_node(obj.parents[i], true);
  5176. if(tmp && tmp.length) {
  5177. tmp.attr('aria-selected', false).children('.jstree-anchor').removeClass(t ? 'jstree-clicked' : 'jstree-checked');
  5178. }
  5179. }
  5180. cur = cur.filter(function(id) {
  5181. return obj.parents.indexOf(id) === -1;
  5182. });
  5183. }
  5184. this._data[ t ? 'core' : 'checkbox' ].selected = cur;
  5185. }, this));
  5186. }
  5187. if(this.settings.checkbox.cascade.indexOf('up') !== -1) {
  5188. this.element
  5189. .on('delete_node.jstree', $.proxy(function (e, data) {
  5190. // apply up (whole handler)
  5191. var p = this.get_node(data.parent),
  5192. m = this._model.data,
  5193. i, j, c, tmp, t = this.settings.checkbox.tie_selection;
  5194. while(p && p.id !== $.jstree.root && !p.state[ t ? 'selected' : 'checked' ]) {
  5195. c = 0;
  5196. for(i = 0, j = p.children.length; i < j; i++) {
  5197. c += m[p.children[i]].state[ t ? 'selected' : 'checked' ];
  5198. }
  5199. if(j > 0 && c === j) {
  5200. p.state[ t ? 'selected' : 'checked' ] = true;
  5201. this._data[ t ? 'core' : 'checkbox' ].selected.push(p.id);
  5202. tmp = this.get_node(p, true);
  5203. if(tmp && tmp.length) {
  5204. tmp.attr('aria-selected', true).children('.jstree-anchor').addClass(t ? 'jstree-clicked' : 'jstree-checked');
  5205. }
  5206. }
  5207. else {
  5208. break;
  5209. }
  5210. p = this.get_node(p.parent);
  5211. }
  5212. }, this))
  5213. .on('move_node.jstree', $.proxy(function (e, data) {
  5214. // apply up (whole handler)
  5215. var is_multi = data.is_multi,
  5216. old_par = data.old_parent,
  5217. new_par = this.get_node(data.parent),
  5218. m = this._model.data,
  5219. p, c, i, j, tmp, t = this.settings.checkbox.tie_selection;
  5220. if(!is_multi) {
  5221. p = this.get_node(old_par);
  5222. while(p && p.id !== $.jstree.root && !p.state[ t ? 'selected' : 'checked' ]) {
  5223. c = 0;
  5224. for(i = 0, j = p.children.length; i < j; i++) {
  5225. c += m[p.children[i]].state[ t ? 'selected' : 'checked' ];
  5226. }
  5227. if(j > 0 && c === j) {
  5228. p.state[ t ? 'selected' : 'checked' ] = true;
  5229. this._data[ t ? 'core' : 'checkbox' ].selected.push(p.id);
  5230. tmp = this.get_node(p, true);
  5231. if(tmp && tmp.length) {
  5232. tmp.attr('aria-selected', true).children('.jstree-anchor').addClass(t ? 'jstree-clicked' : 'jstree-checked');
  5233. }
  5234. }
  5235. else {
  5236. break;
  5237. }
  5238. p = this.get_node(p.parent);
  5239. }
  5240. }
  5241. p = new_par;
  5242. while(p && p.id !== $.jstree.root) {
  5243. c = 0;
  5244. for(i = 0, j = p.children.length; i < j; i++) {
  5245. c += m[p.children[i]].state[ t ? 'selected' : 'checked' ];
  5246. }
  5247. if(c === j) {
  5248. if(!p.state[ t ? 'selected' : 'checked' ]) {
  5249. p.state[ t ? 'selected' : 'checked' ] = true;
  5250. this._data[ t ? 'core' : 'checkbox' ].selected.push(p.id);
  5251. tmp = this.get_node(p, true);
  5252. if(tmp && tmp.length) {
  5253. tmp.attr('aria-selected', true).children('.jstree-anchor').addClass(t ? 'jstree-clicked' : 'jstree-checked');
  5254. }
  5255. }
  5256. }
  5257. else {
  5258. if(p.state[ t ? 'selected' : 'checked' ]) {
  5259. p.state[ t ? 'selected' : 'checked' ] = false;
  5260. this._data[ t ? 'core' : 'checkbox' ].selected = $.vakata.array_remove_item(this._data[ t ? 'core' : 'checkbox' ].selected, p.id);
  5261. tmp = this.get_node(p, true);
  5262. if(tmp && tmp.length) {
  5263. tmp.attr('aria-selected', false).children('.jstree-anchor').removeClass(t ? 'jstree-clicked' : 'jstree-checked');
  5264. }
  5265. }
  5266. else {
  5267. break;
  5268. }
  5269. }
  5270. p = this.get_node(p.parent);
  5271. }
  5272. }, this));
  5273. }
  5274. };
  5275. /**
  5276. * get an array of all nodes whose state is "undetermined"
  5277. * @name get_undetermined([full])
  5278. * @param {boolean} full: if set to `true` the returned array will consist of the full node objects, otherwise - only IDs will be returned
  5279. * @return {Array}
  5280. * @plugin checkbox
  5281. */
  5282. this.get_undetermined = function (full) {
  5283. if (this.settings.checkbox.cascade.indexOf('undetermined') === -1) {
  5284. return [];
  5285. }
  5286. var i, j, k, l, o = {}, m = this._model.data, t = this.settings.checkbox.tie_selection, s = this._data[ t ? 'core' : 'checkbox' ].selected, p = [], tt = this, r = [];
  5287. for(i = 0, j = s.length; i < j; i++) {
  5288. if(m[s[i]] && m[s[i]].parents) {
  5289. for(k = 0, l = m[s[i]].parents.length; k < l; k++) {
  5290. if(o[m[s[i]].parents[k]] !== undefined) {
  5291. break;
  5292. }
  5293. if(m[s[i]].parents[k] !== $.jstree.root) {
  5294. o[m[s[i]].parents[k]] = true;
  5295. p.push(m[s[i]].parents[k]);
  5296. }
  5297. }
  5298. }
  5299. }
  5300. // attempt for server side undetermined state
  5301. this.element.find('.jstree-closed').not(':has(.jstree-children)')
  5302. .each(function () {
  5303. var tmp = tt.get_node(this), tmp2;
  5304. if(!tmp) { return; }
  5305. if(!tmp.state.loaded) {
  5306. if(tmp.original && tmp.original.state && tmp.original.state.undetermined && tmp.original.state.undetermined === true) {
  5307. if(o[tmp.id] === undefined && tmp.id !== $.jstree.root) {
  5308. o[tmp.id] = true;
  5309. p.push(tmp.id);
  5310. }
  5311. for(k = 0, l = tmp.parents.length; k < l; k++) {
  5312. if(o[tmp.parents[k]] === undefined && tmp.parents[k] !== $.jstree.root) {
  5313. o[tmp.parents[k]] = true;
  5314. p.push(tmp.parents[k]);
  5315. }
  5316. }
  5317. }
  5318. }
  5319. else {
  5320. for(i = 0, j = tmp.children_d.length; i < j; i++) {
  5321. tmp2 = m[tmp.children_d[i]];
  5322. if(!tmp2.state.loaded && tmp2.original && tmp2.original.state && tmp2.original.state.undetermined && tmp2.original.state.undetermined === true) {
  5323. if(o[tmp2.id] === undefined && tmp2.id !== $.jstree.root) {
  5324. o[tmp2.id] = true;
  5325. p.push(tmp2.id);
  5326. }
  5327. for(k = 0, l = tmp2.parents.length; k < l; k++) {
  5328. if(o[tmp2.parents[k]] === undefined && tmp2.parents[k] !== $.jstree.root) {
  5329. o[tmp2.parents[k]] = true;
  5330. p.push(tmp2.parents[k]);
  5331. }
  5332. }
  5333. }
  5334. }
  5335. }
  5336. });
  5337. for (i = 0, j = p.length; i < j; i++) {
  5338. if(!m[p[i]].state[ t ? 'selected' : 'checked' ]) {
  5339. r.push(full ? m[p[i]] : p[i]);
  5340. }
  5341. }
  5342. return r;
  5343. };
  5344. /**
  5345. * set the undetermined state where and if necessary. Used internally.
  5346. * @private
  5347. * @name _undetermined()
  5348. * @plugin checkbox
  5349. */
  5350. this._undetermined = function () {
  5351. if(this.element === null) { return; }
  5352. var p = this.get_undetermined(false), i, j, s;
  5353. this.element.find('.jstree-undetermined').removeClass('jstree-undetermined');
  5354. for (i = 0, j = p.length; i < j; i++) {
  5355. s = this.get_node(p[i], true);
  5356. if(s && s.length) {
  5357. s.children('.jstree-anchor').children('.jstree-checkbox').addClass('jstree-undetermined');
  5358. }
  5359. }
  5360. };
  5361. this.redraw_node = function(obj, deep, is_callback, force_render) {
  5362. obj = parent.redraw_node.apply(this, arguments);
  5363. if(obj) {
  5364. var i, j, tmp = null, icon = null;
  5365. for(i = 0, j = obj.childNodes.length; i < j; i++) {
  5366. if(obj.childNodes[i] && obj.childNodes[i].className && obj.childNodes[i].className.indexOf("jstree-anchor") !== -1) {
  5367. tmp = obj.childNodes[i];
  5368. break;
  5369. }
  5370. }
  5371. if(tmp) {
  5372. if(!this.settings.checkbox.tie_selection && this._model.data[obj.id].state.checked) { tmp.className += ' jstree-checked'; }
  5373. icon = _i.cloneNode(false);
  5374. if(this._model.data[obj.id].state.checkbox_disabled) { icon.className += ' jstree-checkbox-disabled'; }
  5375. tmp.insertBefore(icon, tmp.childNodes[0]);
  5376. }
  5377. }
  5378. if(!is_callback && this.settings.checkbox.cascade.indexOf('undetermined') !== -1) {
  5379. if(this._data.checkbox.uto) { clearTimeout(this._data.checkbox.uto); }
  5380. this._data.checkbox.uto = setTimeout($.proxy(this._undetermined, this), 50);
  5381. }
  5382. return obj;
  5383. };
  5384. /**
  5385. * show the node checkbox icons
  5386. * @name show_checkboxes()
  5387. * @plugin checkbox
  5388. */
  5389. this.show_checkboxes = function () { this._data.core.themes.checkboxes = true; this.get_container_ul().removeClass("jstree-no-checkboxes"); };
  5390. /**
  5391. * hide the node checkbox icons
  5392. * @name hide_checkboxes()
  5393. * @plugin checkbox
  5394. */
  5395. this.hide_checkboxes = function () { this._data.core.themes.checkboxes = false; this.get_container_ul().addClass("jstree-no-checkboxes"); };
  5396. /**
  5397. * toggle the node icons
  5398. * @name toggle_checkboxes()
  5399. * @plugin checkbox
  5400. */
  5401. this.toggle_checkboxes = function () { if(this._data.core.themes.checkboxes) { this.hide_checkboxes(); } else { this.show_checkboxes(); } };
  5402. /**
  5403. * checks if a node is in an undetermined state
  5404. * @name is_undetermined(obj)
  5405. * @param {mixed} obj
  5406. * @return {Boolean}
  5407. */
  5408. this.is_undetermined = function (obj) {
  5409. obj = this.get_node(obj);
  5410. var s = this.settings.checkbox.cascade, i, j, t = this.settings.checkbox.tie_selection, d = this._data[ t ? 'core' : 'checkbox' ].selected, m = this._model.data;
  5411. if(!obj || obj.state[ t ? 'selected' : 'checked' ] === true || s.indexOf('undetermined') === -1 || (s.indexOf('down') === -1 && s.indexOf('up') === -1)) {
  5412. return false;
  5413. }
  5414. if(!obj.state.loaded && obj.original.state.undetermined === true) {
  5415. return true;
  5416. }
  5417. for(i = 0, j = obj.children_d.length; i < j; i++) {
  5418. if($.inArray(obj.children_d[i], d) !== -1 || (!m[obj.children_d[i]].state.loaded && m[obj.children_d[i]].original.state.undetermined)) {
  5419. return true;
  5420. }
  5421. }
  5422. return false;
  5423. };
  5424. /**
  5425. * disable a node's checkbox
  5426. * @name disable_checkbox(obj)
  5427. * @param {mixed} obj an array can be used too
  5428. * @trigger disable_checkbox.jstree
  5429. * @plugin checkbox
  5430. */
  5431. this.disable_checkbox = function (obj) {
  5432. var t1, t2, dom;
  5433. if($.isArray(obj)) {
  5434. obj = obj.slice();
  5435. for(t1 = 0, t2 = obj.length; t1 < t2; t1++) {
  5436. this.disable_checkbox(obj[t1]);
  5437. }
  5438. return true;
  5439. }
  5440. obj = this.get_node(obj);
  5441. if(!obj || obj.id === $.jstree.root) {
  5442. return false;
  5443. }
  5444. dom = this.get_node(obj, true);
  5445. if(!obj.state.checkbox_disabled) {
  5446. obj.state.checkbox_disabled = true;
  5447. if(dom && dom.length) {
  5448. dom.children('.jstree-anchor').children('.jstree-checkbox').addClass('jstree-checkbox-disabled');
  5449. }
  5450. /**
  5451. * triggered when an node's checkbox is disabled
  5452. * @event
  5453. * @name disable_checkbox.jstree
  5454. * @param {Object} node
  5455. * @plugin checkbox
  5456. */
  5457. this.trigger('disable_checkbox', { 'node' : obj });
  5458. }
  5459. };
  5460. /**
  5461. * enable a node's checkbox
  5462. * @name disable_checkbox(obj)
  5463. * @param {mixed} obj an array can be used too
  5464. * @trigger enable_checkbox.jstree
  5465. * @plugin checkbox
  5466. */
  5467. this.enable_checkbox = function (obj) {
  5468. var t1, t2, dom;
  5469. if($.isArray(obj)) {
  5470. obj = obj.slice();
  5471. for(t1 = 0, t2 = obj.length; t1 < t2; t1++) {
  5472. this.enable_checkbox(obj[t1]);
  5473. }
  5474. return true;
  5475. }
  5476. obj = this.get_node(obj);
  5477. if(!obj || obj.id === $.jstree.root) {
  5478. return false;
  5479. }
  5480. dom = this.get_node(obj, true);
  5481. if(obj.state.checkbox_disabled) {
  5482. obj.state.checkbox_disabled = false;
  5483. if(dom && dom.length) {
  5484. dom.children('.jstree-anchor').children('.jstree-checkbox').removeClass('jstree-checkbox-disabled');
  5485. }
  5486. /**
  5487. * triggered when an node's checkbox is enabled
  5488. * @event
  5489. * @name enable_checkbox.jstree
  5490. * @param {Object} node
  5491. * @plugin checkbox
  5492. */
  5493. this.trigger('enable_checkbox', { 'node' : obj });
  5494. }
  5495. };
  5496. this.activate_node = function (obj, e) {
  5497. if($(e.target).hasClass('jstree-checkbox-disabled')) {
  5498. return false;
  5499. }
  5500. if(this.settings.checkbox.tie_selection && (this.settings.checkbox.whole_node || $(e.target).hasClass('jstree-checkbox'))) {
  5501. e.ctrlKey = true;
  5502. }
  5503. if(this.settings.checkbox.tie_selection || (!this.settings.checkbox.whole_node && !$(e.target).hasClass('jstree-checkbox'))) {
  5504. return parent.activate_node.call(this, obj, e);
  5505. }
  5506. if(this.is_disabled(obj)) {
  5507. return false;
  5508. }
  5509. if(this.is_checked(obj)) {
  5510. this.uncheck_node(obj, e);
  5511. }
  5512. else {
  5513. this.check_node(obj, e);
  5514. }
  5515. this.trigger('activate_node', { 'node' : this.get_node(obj) });
  5516. };
  5517. /**
  5518. * Cascades checked state to a node and all its descendants. This function does NOT affect hidden and disabled nodes (or their descendants).
  5519. * However if these unaffected nodes are already selected their ids will be included in the returned array.
  5520. * @private
  5521. * @param {string} id the node ID
  5522. * @param {bool} checkedState should the nodes be checked or not
  5523. * @returns {Array} Array of all node id's (in this tree branch) that are checked.
  5524. */
  5525. this._cascade_new_checked_state = function (id, checkedState) {
  5526. var self = this;
  5527. var t = this.settings.checkbox.tie_selection;
  5528. var node = this._model.data[id];
  5529. var selectedNodeIds = [];
  5530. var selectedChildrenIds = [], i, j, selectedChildIds;
  5531. if (
  5532. (this.settings.checkbox.cascade_to_disabled || !node.state.disabled) &&
  5533. (this.settings.checkbox.cascade_to_hidden || !node.state.hidden)
  5534. ) {
  5535. //First try and check/uncheck the children
  5536. if (node.children) {
  5537. for (i = 0, j = node.children.length; i < j; i++) {
  5538. var childId = node.children[i];
  5539. selectedChildIds = self._cascade_new_checked_state(childId, checkedState);
  5540. selectedNodeIds = selectedNodeIds.concat(selectedChildIds);
  5541. if (selectedChildIds.indexOf(childId) > -1) {
  5542. selectedChildrenIds.push(childId);
  5543. }
  5544. }
  5545. }
  5546. var dom = self.get_node(node, true);
  5547. //A node's state is undetermined if some but not all of it's children are checked/selected .
  5548. var undetermined = selectedChildrenIds.length > 0 && selectedChildrenIds.length < node.children.length;
  5549. if(node.original && node.original.state && node.original.state.undetermined) {
  5550. node.original.state.undetermined = undetermined;
  5551. }
  5552. //If a node is undetermined then remove selected class
  5553. if (undetermined) {
  5554. node.state[ t ? 'selected' : 'checked' ] = false;
  5555. dom.attr('aria-selected', false).children('.jstree-anchor').removeClass(t ? 'jstree-clicked' : 'jstree-checked');
  5556. }
  5557. //Otherwise, if the checkedState === true (i.e. the node is being checked now) and all of the node's children are checked (if it has any children),
  5558. //check the node and style it correctly.
  5559. else if (checkedState && selectedChildrenIds.length === node.children.length) {
  5560. node.state[ t ? 'selected' : 'checked' ] = checkedState;
  5561. selectedNodeIds.push(node.id);
  5562. dom.attr('aria-selected', true).children('.jstree-anchor').addClass(t ? 'jstree-clicked' : 'jstree-checked');
  5563. }
  5564. else {
  5565. node.state[ t ? 'selected' : 'checked' ] = false;
  5566. dom.attr('aria-selected', false).children('.jstree-anchor').removeClass(t ? 'jstree-clicked' : 'jstree-checked');
  5567. }
  5568. }
  5569. else {
  5570. selectedChildIds = this.get_checked_descendants(id);
  5571. if (node.state[ t ? 'selected' : 'checked' ]) {
  5572. selectedChildIds.push(node.id);
  5573. }
  5574. selectedNodeIds = selectedNodeIds.concat(selectedChildIds);
  5575. }
  5576. return selectedNodeIds;
  5577. };
  5578. /**
  5579. * Gets ids of nodes selected in branch (of tree) specified by id (does not include the node specified by id)
  5580. * @name get_checked_descendants(obj)
  5581. * @param {string} id the node ID
  5582. * @return {Array} array of IDs
  5583. * @plugin checkbox
  5584. */
  5585. this.get_checked_descendants = function (id) {
  5586. var self = this;
  5587. var t = self.settings.checkbox.tie_selection;
  5588. var node = self._model.data[id];
  5589. return node.children_d.filter(function(_id) {
  5590. return self._model.data[_id].state[ t ? 'selected' : 'checked' ];
  5591. });
  5592. };
  5593. /**
  5594. * check a node (only if tie_selection in checkbox settings is false, otherwise select_node will be called internally)
  5595. * @name check_node(obj)
  5596. * @param {mixed} obj an array can be used to check multiple nodes
  5597. * @trigger check_node.jstree
  5598. * @plugin checkbox
  5599. */
  5600. this.check_node = function (obj, e) {
  5601. if(this.settings.checkbox.tie_selection) { return this.select_node(obj, false, true, e); }
  5602. var dom, t1, t2, th;
  5603. if($.isArray(obj)) {
  5604. obj = obj.slice();
  5605. for(t1 = 0, t2 = obj.length; t1 < t2; t1++) {
  5606. this.check_node(obj[t1], e);
  5607. }
  5608. return true;
  5609. }
  5610. obj = this.get_node(obj);
  5611. if(!obj || obj.id === $.jstree.root) {
  5612. return false;
  5613. }
  5614. dom = this.get_node(obj, true);
  5615. if(!obj.state.checked) {
  5616. obj.state.checked = true;
  5617. this._data.checkbox.selected.push(obj.id);
  5618. if(dom && dom.length) {
  5619. dom.children('.jstree-anchor').addClass('jstree-checked');
  5620. }
  5621. /**
  5622. * triggered when an node is checked (only if tie_selection in checkbox settings is false)
  5623. * @event
  5624. * @name check_node.jstree
  5625. * @param {Object} node
  5626. * @param {Array} selected the current selection
  5627. * @param {Object} event the event (if any) that triggered this check_node
  5628. * @plugin checkbox
  5629. */
  5630. this.trigger('check_node', { 'node' : obj, 'selected' : this._data.checkbox.selected, 'event' : e });
  5631. }
  5632. };
  5633. /**
  5634. * uncheck a node (only if tie_selection in checkbox settings is false, otherwise deselect_node will be called internally)
  5635. * @name uncheck_node(obj)
  5636. * @param {mixed} obj an array can be used to uncheck multiple nodes
  5637. * @trigger uncheck_node.jstree
  5638. * @plugin checkbox
  5639. */
  5640. this.uncheck_node = function (obj, e) {
  5641. if(this.settings.checkbox.tie_selection) { return this.deselect_node(obj, false, e); }
  5642. var t1, t2, dom;
  5643. if($.isArray(obj)) {
  5644. obj = obj.slice();
  5645. for(t1 = 0, t2 = obj.length; t1 < t2; t1++) {
  5646. this.uncheck_node(obj[t1], e);
  5647. }
  5648. return true;
  5649. }
  5650. obj = this.get_node(obj);
  5651. if(!obj || obj.id === $.jstree.root) {
  5652. return false;
  5653. }
  5654. dom = this.get_node(obj, true);
  5655. if(obj.state.checked) {
  5656. obj.state.checked = false;
  5657. this._data.checkbox.selected = $.vakata.array_remove_item(this._data.checkbox.selected, obj.id);
  5658. if(dom.length) {
  5659. dom.children('.jstree-anchor').removeClass('jstree-checked');
  5660. }
  5661. /**
  5662. * triggered when an node is unchecked (only if tie_selection in checkbox settings is false)
  5663. * @event
  5664. * @name uncheck_node.jstree
  5665. * @param {Object} node
  5666. * @param {Array} selected the current selection
  5667. * @param {Object} event the event (if any) that triggered this uncheck_node
  5668. * @plugin checkbox
  5669. */
  5670. this.trigger('uncheck_node', { 'node' : obj, 'selected' : this._data.checkbox.selected, 'event' : e });
  5671. }
  5672. };
  5673. /**
  5674. * checks all nodes in the tree (only if tie_selection in checkbox settings is false, otherwise select_all will be called internally)
  5675. * @name check_all()
  5676. * @trigger check_all.jstree, changed.jstree
  5677. * @plugin checkbox
  5678. */
  5679. this.check_all = function () {
  5680. if(this.settings.checkbox.tie_selection) { return this.select_all(); }
  5681. var tmp = this._data.checkbox.selected.concat([]), i, j;
  5682. this._data.checkbox.selected = this._model.data[$.jstree.root].children_d.concat();
  5683. for(i = 0, j = this._data.checkbox.selected.length; i < j; i++) {
  5684. if(this._model.data[this._data.checkbox.selected[i]]) {
  5685. this._model.data[this._data.checkbox.selected[i]].state.checked = true;
  5686. }
  5687. }
  5688. this.redraw(true);
  5689. /**
  5690. * triggered when all nodes are checked (only if tie_selection in checkbox settings is false)
  5691. * @event
  5692. * @name check_all.jstree
  5693. * @param {Array} selected the current selection
  5694. * @plugin checkbox
  5695. */
  5696. this.trigger('check_all', { 'selected' : this._data.checkbox.selected });
  5697. };
  5698. /**
  5699. * uncheck all checked nodes (only if tie_selection in checkbox settings is false, otherwise deselect_all will be called internally)
  5700. * @name uncheck_all()
  5701. * @trigger uncheck_all.jstree
  5702. * @plugin checkbox
  5703. */
  5704. this.uncheck_all = function () {
  5705. if(this.settings.checkbox.tie_selection) { return this.deselect_all(); }
  5706. var tmp = this._data.checkbox.selected.concat([]), i, j;
  5707. for(i = 0, j = this._data.checkbox.selected.length; i < j; i++) {
  5708. if(this._model.data[this._data.checkbox.selected[i]]) {
  5709. this._model.data[this._data.checkbox.selected[i]].state.checked = false;
  5710. }
  5711. }
  5712. this._data.checkbox.selected = [];
  5713. this.element.find('.jstree-checked').removeClass('jstree-checked');
  5714. /**
  5715. * triggered when all nodes are unchecked (only if tie_selection in checkbox settings is false)
  5716. * @event
  5717. * @name uncheck_all.jstree
  5718. * @param {Object} node the previous selection
  5719. * @param {Array} selected the current selection
  5720. * @plugin checkbox
  5721. */
  5722. this.trigger('uncheck_all', { 'selected' : this._data.checkbox.selected, 'node' : tmp });
  5723. };
  5724. /**
  5725. * checks if a node is checked (if tie_selection is on in the settings this function will return the same as is_selected)
  5726. * @name is_checked(obj)
  5727. * @param {mixed} obj
  5728. * @return {Boolean}
  5729. * @plugin checkbox
  5730. */
  5731. this.is_checked = function (obj) {
  5732. if(this.settings.checkbox.tie_selection) { return this.is_selected(obj); }
  5733. obj = this.get_node(obj);
  5734. if(!obj || obj.id === $.jstree.root) { return false; }
  5735. return obj.state.checked;
  5736. };
  5737. /**
  5738. * get an array of all checked nodes (if tie_selection is on in the settings this function will return the same as get_selected)
  5739. * @name get_checked([full])
  5740. * @param {mixed} full if set to `true` the returned array will consist of the full node objects, otherwise - only IDs will be returned
  5741. * @return {Array}
  5742. * @plugin checkbox
  5743. */
  5744. this.get_checked = function (full) {
  5745. if(this.settings.checkbox.tie_selection) { return this.get_selected(full); }
  5746. return full ? $.map(this._data.checkbox.selected, $.proxy(function (i) { return this.get_node(i); }, this)) : this._data.checkbox.selected;
  5747. };
  5748. /**
  5749. * get an array of all top level checked nodes (ignoring children of checked nodes) (if tie_selection is on in the settings this function will return the same as get_top_selected)
  5750. * @name get_top_checked([full])
  5751. * @param {mixed} full if set to `true` the returned array will consist of the full node objects, otherwise - only IDs will be returned
  5752. * @return {Array}
  5753. * @plugin checkbox
  5754. */
  5755. this.get_top_checked = function (full) {
  5756. if(this.settings.checkbox.tie_selection) { return this.get_top_selected(full); }
  5757. var tmp = this.get_checked(true),
  5758. obj = {}, i, j, k, l;
  5759. for(i = 0, j = tmp.length; i < j; i++) {
  5760. obj[tmp[i].id] = tmp[i];
  5761. }
  5762. for(i = 0, j = tmp.length; i < j; i++) {
  5763. for(k = 0, l = tmp[i].children_d.length; k < l; k++) {
  5764. if(obj[tmp[i].children_d[k]]) {
  5765. delete obj[tmp[i].children_d[k]];
  5766. }
  5767. }
  5768. }
  5769. tmp = [];
  5770. for(i in obj) {
  5771. if(obj.hasOwnProperty(i)) {
  5772. tmp.push(i);
  5773. }
  5774. }
  5775. return full ? $.map(tmp, $.proxy(function (i) { return this.get_node(i); }, this)) : tmp;
  5776. };
  5777. /**
  5778. * get an array of all bottom level checked nodes (ignoring selected parents) (if tie_selection is on in the settings this function will return the same as get_bottom_selected)
  5779. * @name get_bottom_checked([full])
  5780. * @param {mixed} full if set to `true` the returned array will consist of the full node objects, otherwise - only IDs will be returned
  5781. * @return {Array}
  5782. * @plugin checkbox
  5783. */
  5784. this.get_bottom_checked = function (full) {
  5785. if(this.settings.checkbox.tie_selection) { return this.get_bottom_selected(full); }
  5786. var tmp = this.get_checked(true),
  5787. obj = [], i, j;
  5788. for(i = 0, j = tmp.length; i < j; i++) {
  5789. if(!tmp[i].children.length) {
  5790. obj.push(tmp[i].id);
  5791. }
  5792. }
  5793. return full ? $.map(obj, $.proxy(function (i) { return this.get_node(i); }, this)) : obj;
  5794. };
  5795. this.load_node = function (obj, callback) {
  5796. var k, l, i, j, c, tmp;
  5797. if(!$.isArray(obj) && !this.settings.checkbox.tie_selection) {
  5798. tmp = this.get_node(obj);
  5799. if(tmp && tmp.state.loaded) {
  5800. for(k = 0, l = tmp.children_d.length; k < l; k++) {
  5801. if(this._model.data[tmp.children_d[k]].state.checked) {
  5802. c = true;
  5803. this._data.checkbox.selected = $.vakata.array_remove_item(this._data.checkbox.selected, tmp.children_d[k]);
  5804. }
  5805. }
  5806. }
  5807. }
  5808. return parent.load_node.apply(this, arguments);
  5809. };
  5810. this.get_state = function () {
  5811. var state = parent.get_state.apply(this, arguments);
  5812. if(this.settings.checkbox.tie_selection) { return state; }
  5813. state.checkbox = this._data.checkbox.selected.slice();
  5814. return state;
  5815. };
  5816. this.set_state = function (state, callback) {
  5817. var res = parent.set_state.apply(this, arguments);
  5818. if(res && state.checkbox) {
  5819. if(!this.settings.checkbox.tie_selection) {
  5820. this.uncheck_all();
  5821. var _this = this;
  5822. $.each(state.checkbox, function (i, v) {
  5823. _this.check_node(v);
  5824. });
  5825. }
  5826. delete state.checkbox;
  5827. this.set_state(state, callback);
  5828. return false;
  5829. }
  5830. return res;
  5831. };
  5832. this.refresh = function (skip_loading, forget_state) {
  5833. if(this.settings.checkbox.tie_selection) {
  5834. this._data.checkbox.selected = [];
  5835. }
  5836. return parent.refresh.apply(this, arguments);
  5837. };
  5838. };
  5839. // include the checkbox plugin by default
  5840. // $.jstree.defaults.plugins.push("checkbox");
  5841. /**
  5842. * ### Conditionalselect plugin
  5843. *
  5844. * This plugin allows defining a callback to allow or deny node selection by user input (activate node method).
  5845. */
  5846. /**
  5847. * a callback (function) which is invoked in the instance's scope and receives two arguments - the node and the event that triggered the `activate_node` call. Returning false prevents working with the node, returning true allows invoking activate_node. Defaults to returning `true`.
  5848. * @name $.jstree.defaults.checkbox.visible
  5849. * @plugin checkbox
  5850. */
  5851. $.jstree.defaults.conditionalselect = function () { return true; };
  5852. $.jstree.plugins.conditionalselect = function (options, parent) {
  5853. // own function
  5854. this.activate_node = function (obj, e) {
  5855. if(this.settings.conditionalselect.call(this, this.get_node(obj), e)) {
  5856. return parent.activate_node.call(this, obj, e);
  5857. }
  5858. };
  5859. };
  5860. /**
  5861. * ### Contextmenu plugin
  5862. *
  5863. * Shows a context menu when a node is right-clicked.
  5864. */
  5865. /**
  5866. * stores all defaults for the contextmenu plugin
  5867. * @name $.jstree.defaults.contextmenu
  5868. * @plugin contextmenu
  5869. */
  5870. $.jstree.defaults.contextmenu = {
  5871. /**
  5872. * a boolean indicating if the node should be selected when the context menu is invoked on it. Defaults to `true`.
  5873. * @name $.jstree.defaults.contextmenu.select_node
  5874. * @plugin contextmenu
  5875. */
  5876. select_node : true,
  5877. /**
  5878. * a boolean indicating if the menu should be shown aligned with the node. Defaults to `true`, otherwise the mouse coordinates are used.
  5879. * @name $.jstree.defaults.contextmenu.show_at_node
  5880. * @plugin contextmenu
  5881. */
  5882. show_at_node : true,
  5883. /**
  5884. * an object of actions, or a function that accepts a node and a callback function and calls the callback function with an object of actions available for that node (you can also return the items too).
  5885. *
  5886. * Each action consists of a key (a unique name) and a value which is an object with the following properties (only label and action are required). Once a menu item is activated the `action` function will be invoked with an object containing the following keys: item - the contextmenu item definition as seen below, reference - the DOM node that was used (the tree node), element - the contextmenu DOM element, position - an object with x/y properties indicating the position of the menu.
  5887. *
  5888. * * `separator_before` - a boolean indicating if there should be a separator before this item
  5889. * * `separator_after` - a boolean indicating if there should be a separator after this item
  5890. * * `_disabled` - a boolean indicating if this action should be disabled
  5891. * * `label` - a string - the name of the action (could be a function returning a string)
  5892. * * `title` - a string - an optional tooltip for the item
  5893. * * `action` - a function to be executed if this item is chosen, the function will receive
  5894. * * `icon` - a string, can be a path to an icon or a className, if using an image that is in the current directory use a `./` prefix, otherwise it will be detected as a class
  5895. * * `shortcut` - keyCode which will trigger the action if the menu is open (for example `113` for rename, which equals F2)
  5896. * * `shortcut_label` - shortcut label (like for example `F2` for rename)
  5897. * * `submenu` - an object with the same structure as $.jstree.defaults.contextmenu.items which can be used to create a submenu - each key will be rendered as a separate option in a submenu that will appear once the current item is hovered
  5898. *
  5899. * @name $.jstree.defaults.contextmenu.items
  5900. * @plugin contextmenu
  5901. */
  5902. items : function (o, cb) { // Could be an object directly
  5903. return {
  5904. "create" : {
  5905. "separator_before" : false,
  5906. "separator_after" : true,
  5907. "_disabled" : false, //(this.check("create_node", data.reference, {}, "last")),
  5908. "label" : "Create",
  5909. "action" : function (data) {
  5910. var inst = $.jstree.reference(data.reference),
  5911. obj = inst.get_node(data.reference);
  5912. inst.create_node(obj, {}, "last", function (new_node) {
  5913. try {
  5914. inst.edit(new_node);
  5915. } catch (ex) {
  5916. setTimeout(function () { inst.edit(new_node); },0);
  5917. }
  5918. });
  5919. }
  5920. },
  5921. "rename" : {
  5922. "separator_before" : false,
  5923. "separator_after" : false,
  5924. "_disabled" : false, //(this.check("rename_node", data.reference, this.get_parent(data.reference), "")),
  5925. "label" : "Rename",
  5926. /*!
  5927. "shortcut" : 113,
  5928. "shortcut_label" : 'F2',
  5929. "icon" : "glyphicon glyphicon-leaf",
  5930. */
  5931. "action" : function (data) {
  5932. var inst = $.jstree.reference(data.reference),
  5933. obj = inst.get_node(data.reference);
  5934. inst.edit(obj);
  5935. }
  5936. },
  5937. "remove" : {
  5938. "separator_before" : false,
  5939. "icon" : false,
  5940. "separator_after" : false,
  5941. "_disabled" : false, //(this.check("delete_node", data.reference, this.get_parent(data.reference), "")),
  5942. "label" : "Delete",
  5943. "action" : function (data) {
  5944. var inst = $.jstree.reference(data.reference),
  5945. obj = inst.get_node(data.reference);
  5946. if(inst.is_selected(obj)) {
  5947. inst.delete_node(inst.get_selected());
  5948. }
  5949. else {
  5950. inst.delete_node(obj);
  5951. }
  5952. }
  5953. },
  5954. "ccp" : {
  5955. "separator_before" : true,
  5956. "icon" : false,
  5957. "separator_after" : false,
  5958. "label" : "Edit",
  5959. "action" : false,
  5960. "submenu" : {
  5961. "cut" : {
  5962. "separator_before" : false,
  5963. "separator_after" : false,
  5964. "label" : "Cut",
  5965. "action" : function (data) {
  5966. var inst = $.jstree.reference(data.reference),
  5967. obj = inst.get_node(data.reference);
  5968. if(inst.is_selected(obj)) {
  5969. inst.cut(inst.get_top_selected());
  5970. }
  5971. else {
  5972. inst.cut(obj);
  5973. }
  5974. }
  5975. },
  5976. "copy" : {
  5977. "separator_before" : false,
  5978. "icon" : false,
  5979. "separator_after" : false,
  5980. "label" : "Copy",
  5981. "action" : function (data) {
  5982. var inst = $.jstree.reference(data.reference),
  5983. obj = inst.get_node(data.reference);
  5984. if(inst.is_selected(obj)) {
  5985. inst.copy(inst.get_top_selected());
  5986. }
  5987. else {
  5988. inst.copy(obj);
  5989. }
  5990. }
  5991. },
  5992. "paste" : {
  5993. "separator_before" : false,
  5994. "icon" : false,
  5995. "_disabled" : function (data) {
  5996. return !$.jstree.reference(data.reference).can_paste();
  5997. },
  5998. "separator_after" : false,
  5999. "label" : "Paste",
  6000. "action" : function (data) {
  6001. var inst = $.jstree.reference(data.reference),
  6002. obj = inst.get_node(data.reference);
  6003. inst.paste(obj);
  6004. }
  6005. }
  6006. }
  6007. }
  6008. };
  6009. }
  6010. };
  6011. $.jstree.plugins.contextmenu = function (options, parent) {
  6012. this.bind = function () {
  6013. parent.bind.call(this);
  6014. var last_ts = 0, cto = null, ex, ey;
  6015. this.element
  6016. .on("init.jstree loading.jstree ready.jstree", $.proxy(function () {
  6017. this.get_container_ul().addClass('jstree-contextmenu');
  6018. }, this))
  6019. .on("contextmenu.jstree", ".jstree-anchor", $.proxy(function (e, data) {
  6020. if (e.target.tagName.toLowerCase() === 'input') {
  6021. return;
  6022. }
  6023. e.preventDefault();
  6024. last_ts = e.ctrlKey ? +new Date() : 0;
  6025. if(data || cto) {
  6026. last_ts = (+new Date()) + 10000;
  6027. }
  6028. if(cto) {
  6029. clearTimeout(cto);
  6030. }
  6031. if(!this.is_loading(e.currentTarget)) {
  6032. this.show_contextmenu(e.currentTarget, e.pageX, e.pageY, e);
  6033. }
  6034. }, this))
  6035. .on("click.jstree", ".jstree-anchor", $.proxy(function (e) {
  6036. if(this._data.contextmenu.visible && (!last_ts || (+new Date()) - last_ts > 250)) { // work around safari & macOS ctrl+click
  6037. $.vakata.context.hide();
  6038. }
  6039. last_ts = 0;
  6040. }, this))
  6041. .on("touchstart.jstree", ".jstree-anchor", function (e) {
  6042. if(!e.originalEvent || !e.originalEvent.changedTouches || !e.originalEvent.changedTouches[0]) {
  6043. return;
  6044. }
  6045. ex = e.originalEvent.changedTouches[0].clientX;
  6046. ey = e.originalEvent.changedTouches[0].clientY;
  6047. cto = setTimeout(function () {
  6048. $(e.currentTarget).trigger('contextmenu', true);
  6049. }, 750);
  6050. })
  6051. .on('touchmove.vakata.jstree', function (e) {
  6052. if(cto && e.originalEvent && e.originalEvent.changedTouches && e.originalEvent.changedTouches[0] && (Math.abs(ex - e.originalEvent.changedTouches[0].clientX) > 10 || Math.abs(ey - e.originalEvent.changedTouches[0].clientY) > 10)) {
  6053. clearTimeout(cto);
  6054. $.vakata.context.hide();
  6055. }
  6056. })
  6057. .on('touchend.vakata.jstree', function (e) {
  6058. if(cto) {
  6059. clearTimeout(cto);
  6060. }
  6061. });
  6062. /*!
  6063. if(!('oncontextmenu' in document.body) && ('ontouchstart' in document.body)) {
  6064. var el = null, tm = null;
  6065. this.element
  6066. .on("touchstart", ".jstree-anchor", function (e) {
  6067. el = e.currentTarget;
  6068. tm = +new Date();
  6069. $(document).one("touchend", function (e) {
  6070. e.target = document.elementFromPoint(e.originalEvent.targetTouches[0].pageX - window.pageXOffset, e.originalEvent.targetTouches[0].pageY - window.pageYOffset);
  6071. e.currentTarget = e.target;
  6072. tm = ((+(new Date())) - tm);
  6073. if(e.target === el && tm > 600 && tm < 1000) {
  6074. e.preventDefault();
  6075. $(el).trigger('contextmenu', e);
  6076. }
  6077. el = null;
  6078. tm = null;
  6079. });
  6080. });
  6081. }
  6082. */
  6083. $(document).on("context_hide.vakata.jstree", $.proxy(function (e, data) {
  6084. this._data.contextmenu.visible = false;
  6085. $(data.reference).removeClass('jstree-context');
  6086. }, this));
  6087. };
  6088. this.teardown = function () {
  6089. if(this._data.contextmenu.visible) {
  6090. $.vakata.context.hide();
  6091. }
  6092. parent.teardown.call(this);
  6093. };
  6094. /**
  6095. * prepare and show the context menu for a node
  6096. * @name show_contextmenu(obj [, x, y])
  6097. * @param {mixed} obj the node
  6098. * @param {Number} x the x-coordinate relative to the document to show the menu at
  6099. * @param {Number} y the y-coordinate relative to the document to show the menu at
  6100. * @param {Object} e the event if available that triggered the contextmenu
  6101. * @plugin contextmenu
  6102. * @trigger show_contextmenu.jstree
  6103. */
  6104. this.show_contextmenu = function (obj, x, y, e) {
  6105. obj = this.get_node(obj);
  6106. if(!obj || obj.id === $.jstree.root) { return false; }
  6107. var s = this.settings.contextmenu,
  6108. d = this.get_node(obj, true),
  6109. a = d.children(".jstree-anchor"),
  6110. o = false,
  6111. i = false;
  6112. if(s.show_at_node || x === undefined || y === undefined) {
  6113. o = a.offset();
  6114. x = o.left;
  6115. y = o.top + this._data.core.li_height;
  6116. }
  6117. if(this.settings.contextmenu.select_node && !this.is_selected(obj)) {
  6118. this.activate_node(obj, e);
  6119. }
  6120. i = s.items;
  6121. if($.isFunction(i)) {
  6122. i = i.call(this, obj, $.proxy(function (i) {
  6123. this._show_contextmenu(obj, x, y, i);
  6124. }, this));
  6125. }
  6126. if($.isPlainObject(i)) {
  6127. this._show_contextmenu(obj, x, y, i);
  6128. }
  6129. };
  6130. /**
  6131. * show the prepared context menu for a node
  6132. * @name _show_contextmenu(obj, x, y, i)
  6133. * @param {mixed} obj the node
  6134. * @param {Number} x the x-coordinate relative to the document to show the menu at
  6135. * @param {Number} y the y-coordinate relative to the document to show the menu at
  6136. * @param {Number} i the object of items to show
  6137. * @plugin contextmenu
  6138. * @trigger show_contextmenu.jstree
  6139. * @private
  6140. */
  6141. this._show_contextmenu = function (obj, x, y, i) {
  6142. var d = this.get_node(obj, true),
  6143. a = d.children(".jstree-anchor");
  6144. $(document).one("context_show.vakata.jstree", $.proxy(function (e, data) {
  6145. var cls = 'jstree-contextmenu jstree-' + this.get_theme() + '-contextmenu';
  6146. $(data.element).addClass(cls);
  6147. a.addClass('jstree-context');
  6148. }, this));
  6149. this._data.contextmenu.visible = true;
  6150. $.vakata.context.show(a, { 'x' : x, 'y' : y }, i);
  6151. /**
  6152. * triggered when the contextmenu is shown for a node
  6153. * @event
  6154. * @name show_contextmenu.jstree
  6155. * @param {Object} node the node
  6156. * @param {Number} x the x-coordinate of the menu relative to the document
  6157. * @param {Number} y the y-coordinate of the menu relative to the document
  6158. * @plugin contextmenu
  6159. */
  6160. this.trigger('show_contextmenu', { "node" : obj, "x" : x, "y" : y });
  6161. };
  6162. };
  6163. // contextmenu helper
  6164. (function ($) {
  6165. var right_to_left = false,
  6166. vakata_context = {
  6167. element : false,
  6168. reference : false,
  6169. position_x : 0,
  6170. position_y : 0,
  6171. items : [],
  6172. html : "",
  6173. is_visible : false
  6174. };
  6175. $.vakata.context = {
  6176. settings : {
  6177. hide_onmouseleave : 0,
  6178. icons : true
  6179. },
  6180. _trigger : function (event_name) {
  6181. $(document).triggerHandler("context_" + event_name + ".vakata", {
  6182. "reference" : vakata_context.reference,
  6183. "element" : vakata_context.element,
  6184. "position" : {
  6185. "x" : vakata_context.position_x,
  6186. "y" : vakata_context.position_y
  6187. }
  6188. });
  6189. },
  6190. _execute : function (i) {
  6191. i = vakata_context.items[i];
  6192. return i && (!i._disabled || ($.isFunction(i._disabled) && !i._disabled({ "item" : i, "reference" : vakata_context.reference, "element" : vakata_context.element }))) && i.action ? i.action.call(null, {
  6193. "item" : i,
  6194. "reference" : vakata_context.reference,
  6195. "element" : vakata_context.element,
  6196. "position" : {
  6197. "x" : vakata_context.position_x,
  6198. "y" : vakata_context.position_y
  6199. }
  6200. }) : false;
  6201. },
  6202. _parse : function (o, is_callback) {
  6203. if(!o) { return false; }
  6204. if(!is_callback) {
  6205. vakata_context.html = "";
  6206. vakata_context.items = [];
  6207. }
  6208. var str = "",
  6209. sep = false,
  6210. tmp;
  6211. if(is_callback) { str += "<"+"ul>"; }
  6212. $.each(o, function (i, val) {
  6213. if(!val) { return true; }
  6214. vakata_context.items.push(val);
  6215. if(!sep && val.separator_before) {
  6216. str += "<"+"li class='vakata-context-separator'><"+"a href='#' " + ($.vakata.context.settings.icons ? '' : 'style="margin-left:0px;"') + ">&#160;<"+"/a><"+"/li>";
  6217. }
  6218. sep = false;
  6219. str += "<"+"li class='" + (val._class || "") + (val._disabled === true || ($.isFunction(val._disabled) && val._disabled({ "item" : val, "reference" : vakata_context.reference, "element" : vakata_context.element })) ? " vakata-contextmenu-disabled " : "") + "' "+(val.shortcut?" data-shortcut='"+val.shortcut+"' ":'')+">";
  6220. str += "<"+"a href='#' rel='" + (vakata_context.items.length - 1) + "' " + (val.title ? "title='" + val.title + "'" : "") + ">";
  6221. if($.vakata.context.settings.icons) {
  6222. str += "<"+"i ";
  6223. if(val.icon) {
  6224. if(val.icon.indexOf("/") !== -1 || val.icon.indexOf(".") !== -1) { str += " style='background:url(\"" + val.icon + "\") center center no-repeat' "; }
  6225. else { str += " class='" + val.icon + "' "; }
  6226. }
  6227. str += "><"+"/i><"+"span class='vakata-contextmenu-sep'>&#160;<"+"/span>";
  6228. }
  6229. str += ($.isFunction(val.label) ? val.label({ "item" : i, "reference" : vakata_context.reference, "element" : vakata_context.element }) : val.label) + (val.shortcut?' <span class="vakata-contextmenu-shortcut vakata-contextmenu-shortcut-'+val.shortcut+'">'+ (val.shortcut_label || '') +'</span>':'') + "<"+"/a>";
  6230. if(val.submenu) {
  6231. tmp = $.vakata.context._parse(val.submenu, true);
  6232. if(tmp) { str += tmp; }
  6233. }
  6234. str += "<"+"/li>";
  6235. if(val.separator_after) {
  6236. str += "<"+"li class='vakata-context-separator'><"+"a href='#' " + ($.vakata.context.settings.icons ? '' : 'style="margin-left:0px;"') + ">&#160;<"+"/a><"+"/li>";
  6237. sep = true;
  6238. }
  6239. });
  6240. str = str.replace(/<li class\='vakata-context-separator'\><\/li\>$/,"");
  6241. if(is_callback) { str += "</ul>"; }
  6242. /**
  6243. * triggered on the document when the contextmenu is parsed (HTML is built)
  6244. * @event
  6245. * @plugin contextmenu
  6246. * @name context_parse.vakata
  6247. * @param {jQuery} reference the element that was right clicked
  6248. * @param {jQuery} element the DOM element of the menu itself
  6249. * @param {Object} position the x & y coordinates of the menu
  6250. */
  6251. if(!is_callback) { vakata_context.html = str; $.vakata.context._trigger("parse"); }
  6252. return str.length > 10 ? str : false;
  6253. },
  6254. _show_submenu : function (o) {
  6255. o = $(o);
  6256. if(!o.length || !o.children("ul").length) { return; }
  6257. var e = o.children("ul"),
  6258. xl = o.offset().left,
  6259. x = xl + o.outerWidth(),
  6260. y = o.offset().top,
  6261. w = e.width(),
  6262. h = e.height(),
  6263. dw = $(window).width() + $(window).scrollLeft(),
  6264. dh = $(window).height() + $(window).scrollTop();
  6265. // може да се спести е една проверка - дали няма някой от класовете вече нагоре
  6266. if(right_to_left) {
  6267. o[x - (w + 10 + o.outerWidth()) < 0 ? "addClass" : "removeClass"]("vakata-context-left");
  6268. }
  6269. else {
  6270. o[x + w > dw && xl > dw - x ? "addClass" : "removeClass"]("vakata-context-right");
  6271. }
  6272. if(y + h + 10 > dh) {
  6273. e.css("bottom","-1px");
  6274. }
  6275. //if does not fit - stick it to the side
  6276. if (o.hasClass('vakata-context-right')) {
  6277. if (xl < w) {
  6278. e.css("margin-right", xl - w);
  6279. }
  6280. } else {
  6281. if (dw - x < w) {
  6282. e.css("margin-left", dw - x - w);
  6283. }
  6284. }
  6285. e.show();
  6286. },
  6287. show : function (reference, position, data) {
  6288. var o, e, x, y, w, h, dw, dh, cond = true;
  6289. if(vakata_context.element && vakata_context.element.length) {
  6290. vakata_context.element.width('');
  6291. }
  6292. switch(cond) {
  6293. case (!position && !reference):
  6294. return false;
  6295. case (!!position && !!reference):
  6296. vakata_context.reference = reference;
  6297. vakata_context.position_x = position.x;
  6298. vakata_context.position_y = position.y;
  6299. break;
  6300. case (!position && !!reference):
  6301. vakata_context.reference = reference;
  6302. o = reference.offset();
  6303. vakata_context.position_x = o.left + reference.outerHeight();
  6304. vakata_context.position_y = o.top;
  6305. break;
  6306. case (!!position && !reference):
  6307. vakata_context.position_x = position.x;
  6308. vakata_context.position_y = position.y;
  6309. break;
  6310. }
  6311. if(!!reference && !data && $(reference).data('vakata_contextmenu')) {
  6312. data = $(reference).data('vakata_contextmenu');
  6313. }
  6314. if($.vakata.context._parse(data)) {
  6315. vakata_context.element.html(vakata_context.html);
  6316. }
  6317. if(vakata_context.items.length) {
  6318. vakata_context.element.appendTo(document.body);
  6319. e = vakata_context.element;
  6320. x = vakata_context.position_x;
  6321. y = vakata_context.position_y;
  6322. w = e.width();
  6323. h = e.height();
  6324. dw = $(window).width() + $(window).scrollLeft();
  6325. dh = $(window).height() + $(window).scrollTop();
  6326. if(right_to_left) {
  6327. x -= (e.outerWidth() - $(reference).outerWidth());
  6328. if(x < $(window).scrollLeft() + 20) {
  6329. x = $(window).scrollLeft() + 20;
  6330. }
  6331. }
  6332. if(x + w + 20 > dw) {
  6333. x = dw - (w + 20);
  6334. }
  6335. if(y + h + 20 > dh) {
  6336. y = dh - (h + 20);
  6337. }
  6338. vakata_context.element
  6339. .css({ "left" : x, "top" : y })
  6340. .show()
  6341. .find('a').first().focus().parent().addClass("vakata-context-hover");
  6342. vakata_context.is_visible = true;
  6343. /**
  6344. * triggered on the document when the contextmenu is shown
  6345. * @event
  6346. * @plugin contextmenu
  6347. * @name context_show.vakata
  6348. * @param {jQuery} reference the element that was right clicked
  6349. * @param {jQuery} element the DOM element of the menu itself
  6350. * @param {Object} position the x & y coordinates of the menu
  6351. */
  6352. $.vakata.context._trigger("show");
  6353. }
  6354. },
  6355. hide : function () {
  6356. if(vakata_context.is_visible) {
  6357. vakata_context.element.hide().find("ul").hide().end().find(':focus').blur().end().detach();
  6358. vakata_context.is_visible = false;
  6359. /**
  6360. * triggered on the document when the contextmenu is hidden
  6361. * @event
  6362. * @plugin contextmenu
  6363. * @name context_hide.vakata
  6364. * @param {jQuery} reference the element that was right clicked
  6365. * @param {jQuery} element the DOM element of the menu itself
  6366. * @param {Object} position the x & y coordinates of the menu
  6367. */
  6368. $.vakata.context._trigger("hide");
  6369. }
  6370. }
  6371. };
  6372. $(function () {
  6373. right_to_left = $(document.body).css("direction") === "rtl";
  6374. var to = false;
  6375. vakata_context.element = $("<ul class='vakata-context'></ul>");
  6376. vakata_context.element
  6377. .on("mouseenter", "li", function (e) {
  6378. e.stopImmediatePropagation();
  6379. if($.contains(this, e.relatedTarget)) {
  6380. // премахнато заради delegate mouseleave по-долу
  6381. // $(this).find(".vakata-context-hover").removeClass("vakata-context-hover");
  6382. return;
  6383. }
  6384. if(to) { clearTimeout(to); }
  6385. vakata_context.element.find(".vakata-context-hover").removeClass("vakata-context-hover").end();
  6386. $(this)
  6387. .siblings().find("ul").hide().end().end()
  6388. .parentsUntil(".vakata-context", "li").addBack().addClass("vakata-context-hover");
  6389. $.vakata.context._show_submenu(this);
  6390. })
  6391. // тестово - дали не натоварва?
  6392. .on("mouseleave", "li", function (e) {
  6393. if($.contains(this, e.relatedTarget)) { return; }
  6394. $(this).find(".vakata-context-hover").addBack().removeClass("vakata-context-hover");
  6395. })
  6396. .on("mouseleave", function (e) {
  6397. $(this).find(".vakata-context-hover").removeClass("vakata-context-hover");
  6398. if($.vakata.context.settings.hide_onmouseleave) {
  6399. to = setTimeout(
  6400. (function (t) {
  6401. return function () { $.vakata.context.hide(); };
  6402. }(this)), $.vakata.context.settings.hide_onmouseleave);
  6403. }
  6404. })
  6405. .on("click", "a", function (e) {
  6406. e.preventDefault();
  6407. //})
  6408. //.on("mouseup", "a", function (e) {
  6409. if(!$(this).blur().parent().hasClass("vakata-context-disabled") && $.vakata.context._execute($(this).attr("rel")) !== false) {
  6410. $.vakata.context.hide();
  6411. }
  6412. })
  6413. .on('keydown', 'a', function (e) {
  6414. var o = null;
  6415. switch(e.which) {
  6416. case 13:
  6417. case 32:
  6418. e.type = "click";
  6419. e.preventDefault();
  6420. $(e.currentTarget).trigger(e);
  6421. break;
  6422. case 37:
  6423. if(vakata_context.is_visible) {
  6424. vakata_context.element.find(".vakata-context-hover").last().closest("li").first().find("ul").hide().find(".vakata-context-hover").removeClass("vakata-context-hover").end().end().children('a').focus();
  6425. e.stopImmediatePropagation();
  6426. e.preventDefault();
  6427. }
  6428. break;
  6429. case 38:
  6430. if(vakata_context.is_visible) {
  6431. o = vakata_context.element.find("ul:visible").addBack().last().children(".vakata-context-hover").removeClass("vakata-context-hover").prevAll("li:not(.vakata-context-separator)").first();
  6432. if(!o.length) { o = vakata_context.element.find("ul:visible").addBack().last().children("li:not(.vakata-context-separator)").last(); }
  6433. o.addClass("vakata-context-hover").children('a').focus();
  6434. e.stopImmediatePropagation();
  6435. e.preventDefault();
  6436. }
  6437. break;
  6438. case 39:
  6439. if(vakata_context.is_visible) {
  6440. vakata_context.element.find(".vakata-context-hover").last().children("ul").show().children("li:not(.vakata-context-separator)").removeClass("vakata-context-hover").first().addClass("vakata-context-hover").children('a').focus();
  6441. e.stopImmediatePropagation();
  6442. e.preventDefault();
  6443. }
  6444. break;
  6445. case 40:
  6446. if(vakata_context.is_visible) {
  6447. o = vakata_context.element.find("ul:visible").addBack().last().children(".vakata-context-hover").removeClass("vakata-context-hover").nextAll("li:not(.vakata-context-separator)").first();
  6448. if(!o.length) { o = vakata_context.element.find("ul:visible").addBack().last().children("li:not(.vakata-context-separator)").first(); }
  6449. o.addClass("vakata-context-hover").children('a').focus();
  6450. e.stopImmediatePropagation();
  6451. e.preventDefault();
  6452. }
  6453. break;
  6454. case 27:
  6455. $.vakata.context.hide();
  6456. e.preventDefault();
  6457. break;
  6458. default:
  6459. //console.log(e.which);
  6460. break;
  6461. }
  6462. })
  6463. .on('keydown', function (e) {
  6464. e.preventDefault();
  6465. var a = vakata_context.element.find('.vakata-contextmenu-shortcut-' + e.which).parent();
  6466. if(a.parent().not('.vakata-context-disabled')) {
  6467. a.click();
  6468. }
  6469. });
  6470. $(document)
  6471. .on("mousedown.vakata.jstree", function (e) {
  6472. if(vakata_context.is_visible && vakata_context.element[0] !== e.target && !$.contains(vakata_context.element[0], e.target)) {
  6473. $.vakata.context.hide();
  6474. }
  6475. })
  6476. .on("context_show.vakata.jstree", function (e, data) {
  6477. vakata_context.element.find("li:has(ul)").children("a").addClass("vakata-context-parent");
  6478. if(right_to_left) {
  6479. vakata_context.element.addClass("vakata-context-rtl").css("direction", "rtl");
  6480. }
  6481. // also apply a RTL class?
  6482. vakata_context.element.find("ul").hide().end();
  6483. });
  6484. });
  6485. }($));
  6486. // $.jstree.defaults.plugins.push("contextmenu");
  6487. /**
  6488. * ### Drag'n'drop plugin
  6489. *
  6490. * Enables dragging and dropping of nodes in the tree, resulting in a move or copy operations.
  6491. */
  6492. /**
  6493. * stores all defaults for the drag'n'drop plugin
  6494. * @name $.jstree.defaults.dnd
  6495. * @plugin dnd
  6496. */
  6497. $.jstree.defaults.dnd = {
  6498. /**
  6499. * a boolean indicating if a copy should be possible while dragging (by pressint the meta key or Ctrl). Defaults to `true`.
  6500. * @name $.jstree.defaults.dnd.copy
  6501. * @plugin dnd
  6502. */
  6503. copy : true,
  6504. /**
  6505. * a number indicating how long a node should remain hovered while dragging to be opened. Defaults to `500`.
  6506. * @name $.jstree.defaults.dnd.open_timeout
  6507. * @plugin dnd
  6508. */
  6509. open_timeout : 500,
  6510. /**
  6511. * a function invoked each time a node is about to be dragged, invoked in the tree's scope and receives the nodes about to be dragged as an argument (array) and the event that started the drag - return `false` to prevent dragging
  6512. * @name $.jstree.defaults.dnd.is_draggable
  6513. * @plugin dnd
  6514. */
  6515. is_draggable : true,
  6516. /**
  6517. * a boolean indicating if checks should constantly be made while the user is dragging the node (as opposed to checking only on drop), default is `true`
  6518. * @name $.jstree.defaults.dnd.check_while_dragging
  6519. * @plugin dnd
  6520. */
  6521. check_while_dragging : true,
  6522. /**
  6523. * a boolean indicating if nodes from this tree should only be copied with dnd (as opposed to moved), default is `false`
  6524. * @name $.jstree.defaults.dnd.always_copy
  6525. * @plugin dnd
  6526. */
  6527. always_copy : false,
  6528. /**
  6529. * when dropping a node "inside", this setting indicates the position the node should go to - it can be an integer or a string: "first" (same as 0) or "last", default is `0`
  6530. * @name $.jstree.defaults.dnd.inside_pos
  6531. * @plugin dnd
  6532. */
  6533. inside_pos : 0,
  6534. /**
  6535. * when starting the drag on a node that is selected this setting controls if all selected nodes are dragged or only the single node, default is `true`, which means all selected nodes are dragged when the drag is started on a selected node
  6536. * @name $.jstree.defaults.dnd.drag_selection
  6537. * @plugin dnd
  6538. */
  6539. drag_selection : true,
  6540. /**
  6541. * controls whether dnd works on touch devices. If left as boolean true dnd will work the same as in desktop browsers, which in some cases may impair scrolling. If set to boolean false dnd will not work on touch devices. There is a special third option - string "selected" which means only selected nodes can be dragged on touch devices.
  6542. * @name $.jstree.defaults.dnd.touch
  6543. * @plugin dnd
  6544. */
  6545. touch : true,
  6546. /**
  6547. * controls whether items can be dropped anywhere on the node, not just on the anchor, by default only the node anchor is a valid drop target. Works best with the wholerow plugin. If enabled on mobile depending on the interface it might be hard for the user to cancel the drop, since the whole tree container will be a valid drop target.
  6548. * @name $.jstree.defaults.dnd.large_drop_target
  6549. * @plugin dnd
  6550. */
  6551. large_drop_target : false,
  6552. /**
  6553. * controls whether a drag can be initiated from any part of the node and not just the text/icon part, works best with the wholerow plugin. Keep in mind it can cause problems with tree scrolling on mobile depending on the interface - in that case set the touch option to "selected".
  6554. * @name $.jstree.defaults.dnd.large_drag_target
  6555. * @plugin dnd
  6556. */
  6557. large_drag_target : false,
  6558. /**
  6559. * controls whether use HTML5 dnd api instead of classical. That will allow better integration of dnd events with other HTML5 controls.
  6560. * @reference http://caniuse.com/#feat=dragndrop
  6561. * @name $.jstree.defaults.dnd.use_html5
  6562. * @plugin dnd
  6563. */
  6564. use_html5: false
  6565. };
  6566. var drg, elm;
  6567. // TODO: now check works by checking for each node individually, how about max_children, unique, etc?
  6568. $.jstree.plugins.dnd = function (options, parent) {
  6569. this.init = function (el, options) {
  6570. parent.init.call(this, el, options);
  6571. this.settings.dnd.use_html5 = this.settings.dnd.use_html5 && ('draggable' in document.createElement('span'));
  6572. };
  6573. this.bind = function () {
  6574. parent.bind.call(this);
  6575. this.element
  6576. .on(this.settings.dnd.use_html5 ? 'dragstart.jstree' : 'mousedown.jstree touchstart.jstree', this.settings.dnd.large_drag_target ? '.jstree-node' : '.jstree-anchor', $.proxy(function (e) {
  6577. if(this.settings.dnd.large_drag_target && $(e.target).closest('.jstree-node')[0] !== e.currentTarget) {
  6578. return true;
  6579. }
  6580. if(e.type === "touchstart" && (!this.settings.dnd.touch || (this.settings.dnd.touch === 'selected' && !$(e.currentTarget).closest('.jstree-node').children('.jstree-anchor').hasClass('jstree-clicked')))) {
  6581. return true;
  6582. }
  6583. var obj = this.get_node(e.target),
  6584. mlt = this.is_selected(obj) && this.settings.dnd.drag_selection ? this.get_top_selected().length : 1,
  6585. txt = (mlt > 1 ? mlt + ' ' + this.get_string('nodes') : this.get_text(e.currentTarget));
  6586. if(this.settings.core.force_text) {
  6587. txt = $.vakata.html.escape(txt);
  6588. }
  6589. if(obj && obj.id && obj.id !== $.jstree.root && (e.which === 1 || e.type === "touchstart" || e.type === "dragstart") &&
  6590. (this.settings.dnd.is_draggable === true || ($.isFunction(this.settings.dnd.is_draggable) && this.settings.dnd.is_draggable.call(this, (mlt > 1 ? this.get_top_selected(true) : [obj]), e)))
  6591. ) {
  6592. drg = { 'jstree' : true, 'origin' : this, 'obj' : this.get_node(obj,true), 'nodes' : mlt > 1 ? this.get_top_selected() : [obj.id] };
  6593. elm = e.currentTarget;
  6594. if (this.settings.dnd.use_html5) {
  6595. $.vakata.dnd._trigger('start', e, { 'helper': $(), 'element': elm, 'data': drg });
  6596. } else {
  6597. this.element.trigger('mousedown.jstree');
  6598. return $.vakata.dnd.start(e, drg, '<div id="jstree-dnd" class="jstree-' + this.get_theme() + ' jstree-' + this.get_theme() + '-' + this.get_theme_variant() + ' ' + ( this.settings.core.themes.responsive ? ' jstree-dnd-responsive' : '' ) + '"><i class="jstree-icon jstree-er"></i>' + txt + '<ins class="jstree-copy" style="display:none;">+</ins></div>');
  6599. }
  6600. }
  6601. }, this));
  6602. if (this.settings.dnd.use_html5) {
  6603. this.element
  6604. .on('dragover.jstree', function (e) {
  6605. e.preventDefault();
  6606. $.vakata.dnd._trigger('move', e, { 'helper': $(), 'element': elm, 'data': drg });
  6607. return false;
  6608. })
  6609. //.on('dragenter.jstree', this.settings.dnd.large_drop_target ? '.jstree-node' : '.jstree-anchor', $.proxy(function (e) {
  6610. // e.preventDefault();
  6611. // $.vakata.dnd._trigger('move', e, { 'helper': $(), 'element': elm, 'data': drg });
  6612. // return false;
  6613. // }, this))
  6614. .on('drop.jstree', $.proxy(function (e) {
  6615. e.preventDefault();
  6616. $.vakata.dnd._trigger('stop', e, { 'helper': $(), 'element': elm, 'data': drg });
  6617. return false;
  6618. }, this));
  6619. }
  6620. };
  6621. this.redraw_node = function(obj, deep, callback, force_render) {
  6622. obj = parent.redraw_node.apply(this, arguments);
  6623. if (obj && this.settings.dnd.use_html5) {
  6624. if (this.settings.dnd.large_drag_target) {
  6625. obj.setAttribute('draggable', true);
  6626. } else {
  6627. var i, j, tmp = null;
  6628. for(i = 0, j = obj.childNodes.length; i < j; i++) {
  6629. if(obj.childNodes[i] && obj.childNodes[i].className && obj.childNodes[i].className.indexOf("jstree-anchor") !== -1) {
  6630. tmp = obj.childNodes[i];
  6631. break;
  6632. }
  6633. }
  6634. if(tmp) {
  6635. tmp.setAttribute('draggable', true);
  6636. }
  6637. }
  6638. }
  6639. return obj;
  6640. };
  6641. };
  6642. $(function() {
  6643. // bind only once for all instances
  6644. var lastmv = false,
  6645. laster = false,
  6646. lastev = false,
  6647. opento = false,
  6648. marker = $('<div id="jstree-marker">&#160;</div>').hide(); //.appendTo('body');
  6649. $(document)
  6650. .on('dragover.vakata.jstree', function (e) {
  6651. if (elm) {
  6652. $.vakata.dnd._trigger('move', e, { 'helper': $(), 'element': elm, 'data': drg });
  6653. }
  6654. })
  6655. .on('drop.vakata.jstree', function (e) {
  6656. if (elm) {
  6657. $.vakata.dnd._trigger('stop', e, { 'helper': $(), 'element': elm, 'data': drg });
  6658. elm = null;
  6659. drg = null;
  6660. }
  6661. })
  6662. .on('dnd_start.vakata.jstree', function (e, data) {
  6663. lastmv = false;
  6664. lastev = false;
  6665. if(!data || !data.data || !data.data.jstree) { return; }
  6666. marker.appendTo(document.body); //.show();
  6667. })
  6668. .on('dnd_move.vakata.jstree', function (e, data) {
  6669. var isDifferentNode = data.event.target !== lastev.target;
  6670. if(opento) {
  6671. if (!data.event || data.event.type !== 'dragover' || isDifferentNode) {
  6672. clearTimeout(opento);
  6673. }
  6674. }
  6675. if(!data || !data.data || !data.data.jstree) { return; }
  6676. // if we are hovering the marker image do nothing (can happen on "inside" drags)
  6677. if(data.event.target.id && data.event.target.id === 'jstree-marker') {
  6678. return;
  6679. }
  6680. lastev = data.event;
  6681. var ins = $.jstree.reference(data.event.target),
  6682. ref = false,
  6683. off = false,
  6684. rel = false,
  6685. tmp, l, t, h, p, i, o, ok, t1, t2, op, ps, pr, ip, tm, is_copy, pn;
  6686. // if we are over an instance
  6687. if(ins && ins._data && ins._data.dnd) {
  6688. marker.attr('class', 'jstree-' + ins.get_theme() + ( ins.settings.core.themes.responsive ? ' jstree-dnd-responsive' : '' ));
  6689. is_copy = data.data.origin && (data.data.origin.settings.dnd.always_copy || (data.data.origin.settings.dnd.copy && (data.event.metaKey || data.event.ctrlKey)));
  6690. data.helper
  6691. .children().attr('class', 'jstree-' + ins.get_theme() + ' jstree-' + ins.get_theme() + '-' + ins.get_theme_variant() + ' ' + ( ins.settings.core.themes.responsive ? ' jstree-dnd-responsive' : '' ))
  6692. .find('.jstree-copy').first()[ is_copy ? 'show' : 'hide' ]();
  6693. // if are hovering the container itself add a new root node
  6694. //console.log(data.event);
  6695. if( (data.event.target === ins.element[0] || data.event.target === ins.get_container_ul()[0]) && ins.get_container_ul().children().length === 0) {
  6696. ok = true;
  6697. for(t1 = 0, t2 = data.data.nodes.length; t1 < t2; t1++) {
  6698. ok = ok && ins.check( (data.data.origin && (data.data.origin.settings.dnd.always_copy || (data.data.origin.settings.dnd.copy && (data.event.metaKey || data.event.ctrlKey)) ) ? "copy_node" : "move_node"), (data.data.origin && data.data.origin !== ins ? data.data.origin.get_node(data.data.nodes[t1]) : data.data.nodes[t1]), $.jstree.root, 'last', { 'dnd' : true, 'ref' : ins.get_node($.jstree.root), 'pos' : 'i', 'origin' : data.data.origin, 'is_multi' : (data.data.origin && data.data.origin !== ins), 'is_foreign' : (!data.data.origin) });
  6699. if(!ok) { break; }
  6700. }
  6701. if(ok) {
  6702. lastmv = { 'ins' : ins, 'par' : $.jstree.root, 'pos' : 'last' };
  6703. marker.hide();
  6704. data.helper.find('.jstree-icon').first().removeClass('jstree-er').addClass('jstree-ok');
  6705. if (data.event.originalEvent && data.event.originalEvent.dataTransfer) {
  6706. data.event.originalEvent.dataTransfer.dropEffect = is_copy ? 'copy' : 'move';
  6707. }
  6708. return;
  6709. }
  6710. }
  6711. else {
  6712. // if we are hovering a tree node
  6713. ref = ins.settings.dnd.large_drop_target ? $(data.event.target).closest('.jstree-node').children('.jstree-anchor') : $(data.event.target).closest('.jstree-anchor');
  6714. if(ref && ref.length && ref.parent().is('.jstree-closed, .jstree-open, .jstree-leaf')) {
  6715. off = ref.offset();
  6716. rel = (data.event.pageY !== undefined ? data.event.pageY : data.event.originalEvent.pageY) - off.top;
  6717. h = ref.outerHeight();
  6718. if(rel < h / 3) {
  6719. o = ['b', 'i', 'a'];
  6720. }
  6721. else if(rel > h - h / 3) {
  6722. o = ['a', 'i', 'b'];
  6723. }
  6724. else {
  6725. o = rel > h / 2 ? ['i', 'a', 'b'] : ['i', 'b', 'a'];
  6726. }
  6727. $.each(o, function (j, v) {
  6728. switch(v) {
  6729. case 'b':
  6730. l = off.left - 6;
  6731. t = off.top;
  6732. p = ins.get_parent(ref);
  6733. i = ref.parent().index();
  6734. break;
  6735. case 'i':
  6736. ip = ins.settings.dnd.inside_pos;
  6737. tm = ins.get_node(ref.parent());
  6738. l = off.left - 2;
  6739. t = off.top + h / 2 + 1;
  6740. p = tm.id;
  6741. i = ip === 'first' ? 0 : (ip === 'last' ? tm.children.length : Math.min(ip, tm.children.length));
  6742. break;
  6743. case 'a':
  6744. l = off.left - 6;
  6745. t = off.top + h;
  6746. p = ins.get_parent(ref);
  6747. i = ref.parent().index() + 1;
  6748. break;
  6749. }
  6750. ok = true;
  6751. for(t1 = 0, t2 = data.data.nodes.length; t1 < t2; t1++) {
  6752. op = data.data.origin && (data.data.origin.settings.dnd.always_copy || (data.data.origin.settings.dnd.copy && (data.event.metaKey || data.event.ctrlKey))) ? "copy_node" : "move_node";
  6753. ps = i;
  6754. if(op === "move_node" && v === 'a' && (data.data.origin && data.data.origin === ins) && p === ins.get_parent(data.data.nodes[t1])) {
  6755. pr = ins.get_node(p);
  6756. if(ps > $.inArray(data.data.nodes[t1], pr.children)) {
  6757. ps -= 1;
  6758. }
  6759. }
  6760. ok = ok && ( (ins && ins.settings && ins.settings.dnd && ins.settings.dnd.check_while_dragging === false) || ins.check(op, (data.data.origin && data.data.origin !== ins ? data.data.origin.get_node(data.data.nodes[t1]) : data.data.nodes[t1]), p, ps, { 'dnd' : true, 'ref' : ins.get_node(ref.parent()), 'pos' : v, 'origin' : data.data.origin, 'is_multi' : (data.data.origin && data.data.origin !== ins), 'is_foreign' : (!data.data.origin) }) );
  6761. if(!ok) {
  6762. if(ins && ins.last_error) { laster = ins.last_error(); }
  6763. break;
  6764. }
  6765. }
  6766. if(v === 'i' && ref.parent().is('.jstree-closed') && ins.settings.dnd.open_timeout) {
  6767. if (!data.event || data.event.type !== 'dragover' || isDifferentNode) {
  6768. if (opento) { clearTimeout(opento); }
  6769. opento = setTimeout((function (x, z) { return function () { x.open_node(z); }; }(ins, ref)), ins.settings.dnd.open_timeout);
  6770. }
  6771. }
  6772. if(ok) {
  6773. pn = ins.get_node(p, true);
  6774. if (!pn.hasClass('.jstree-dnd-parent')) {
  6775. $('.jstree-dnd-parent').removeClass('jstree-dnd-parent');
  6776. pn.addClass('jstree-dnd-parent');
  6777. }
  6778. lastmv = { 'ins' : ins, 'par' : p, 'pos' : v === 'i' && ip === 'last' && i === 0 && !ins.is_loaded(tm) ? 'last' : i };
  6779. marker.css({ 'left' : l + 'px', 'top' : t + 'px' }).show();
  6780. data.helper.find('.jstree-icon').first().removeClass('jstree-er').addClass('jstree-ok');
  6781. if (data.event.originalEvent && data.event.originalEvent.dataTransfer) {
  6782. data.event.originalEvent.dataTransfer.dropEffect = is_copy ? 'copy' : 'move';
  6783. }
  6784. laster = {};
  6785. o = true;
  6786. return false;
  6787. }
  6788. });
  6789. if(o === true) { return; }
  6790. }
  6791. }
  6792. }
  6793. $('.jstree-dnd-parent').removeClass('jstree-dnd-parent');
  6794. lastmv = false;
  6795. data.helper.find('.jstree-icon').removeClass('jstree-ok').addClass('jstree-er');
  6796. if (data.event.originalEvent && data.event.originalEvent.dataTransfer) {
  6797. //data.event.originalEvent.dataTransfer.dropEffect = 'none';
  6798. }
  6799. marker.hide();
  6800. })
  6801. .on('dnd_scroll.vakata.jstree', function (e, data) {
  6802. if(!data || !data.data || !data.data.jstree) { return; }
  6803. marker.hide();
  6804. lastmv = false;
  6805. lastev = false;
  6806. data.helper.find('.jstree-icon').first().removeClass('jstree-ok').addClass('jstree-er');
  6807. })
  6808. .on('dnd_stop.vakata.jstree', function (e, data) {
  6809. $('.jstree-dnd-parent').removeClass('jstree-dnd-parent');
  6810. if(opento) { clearTimeout(opento); }
  6811. if(!data || !data.data || !data.data.jstree) { return; }
  6812. marker.hide().detach();
  6813. var i, j, nodes = [];
  6814. if(lastmv) {
  6815. for(i = 0, j = data.data.nodes.length; i < j; i++) {
  6816. nodes[i] = data.data.origin ? data.data.origin.get_node(data.data.nodes[i]) : data.data.nodes[i];
  6817. }
  6818. lastmv.ins[ data.data.origin && (data.data.origin.settings.dnd.always_copy || (data.data.origin.settings.dnd.copy && (data.event.metaKey || data.event.ctrlKey))) ? 'copy_node' : 'move_node' ](nodes, lastmv.par, lastmv.pos, false, false, false, data.data.origin);
  6819. }
  6820. else {
  6821. i = $(data.event.target).closest('.jstree');
  6822. if(i.length && laster && laster.error && laster.error === 'check') {
  6823. i = i.jstree(true);
  6824. if(i) {
  6825. i.settings.core.error.call(this, laster);
  6826. }
  6827. }
  6828. }
  6829. lastev = false;
  6830. lastmv = false;
  6831. })
  6832. .on('keyup.jstree keydown.jstree', function (e, data) {
  6833. data = $.vakata.dnd._get();
  6834. if(data && data.data && data.data.jstree) {
  6835. if (e.type === "keyup" && e.which === 27) {
  6836. if (opento) { clearTimeout(opento); }
  6837. lastmv = false;
  6838. laster = false;
  6839. lastev = false;
  6840. opento = false;
  6841. marker.hide().detach();
  6842. $.vakata.dnd._clean();
  6843. } else {
  6844. data.helper.find('.jstree-copy').first()[ data.data.origin && (data.data.origin.settings.dnd.always_copy || (data.data.origin.settings.dnd.copy && (e.metaKey || e.ctrlKey))) ? 'show' : 'hide' ]();
  6845. if(lastev) {
  6846. lastev.metaKey = e.metaKey;
  6847. lastev.ctrlKey = e.ctrlKey;
  6848. $.vakata.dnd._trigger('move', lastev);
  6849. }
  6850. }
  6851. }
  6852. });
  6853. });
  6854. // helpers
  6855. (function ($) {
  6856. $.vakata.html = {
  6857. div : $('<div />'),
  6858. escape : function (str) {
  6859. return $.vakata.html.div.text(str).html();
  6860. },
  6861. strip : function (str) {
  6862. return $.vakata.html.div.empty().append($.parseHTML(str)).text();
  6863. }
  6864. };
  6865. // private variable
  6866. var vakata_dnd = {
  6867. element : false,
  6868. target : false,
  6869. is_down : false,
  6870. is_drag : false,
  6871. helper : false,
  6872. helper_w: 0,
  6873. data : false,
  6874. init_x : 0,
  6875. init_y : 0,
  6876. scroll_l: 0,
  6877. scroll_t: 0,
  6878. scroll_e: false,
  6879. scroll_i: false,
  6880. is_touch: false
  6881. };
  6882. $.vakata.dnd = {
  6883. settings : {
  6884. scroll_speed : 10,
  6885. scroll_proximity : 20,
  6886. helper_left : 5,
  6887. helper_top : 10,
  6888. threshold : 5,
  6889. threshold_touch : 10
  6890. },
  6891. _trigger : function (event_name, e, data) {
  6892. if (data === undefined) {
  6893. data = $.vakata.dnd._get();
  6894. }
  6895. data.event = e;
  6896. $(document).triggerHandler("dnd_" + event_name + ".vakata", data);
  6897. },
  6898. _get : function () {
  6899. return {
  6900. "data" : vakata_dnd.data,
  6901. "element" : vakata_dnd.element,
  6902. "helper" : vakata_dnd.helper
  6903. };
  6904. },
  6905. _clean : function () {
  6906. if(vakata_dnd.helper) { vakata_dnd.helper.remove(); }
  6907. if(vakata_dnd.scroll_i) { clearInterval(vakata_dnd.scroll_i); vakata_dnd.scroll_i = false; }
  6908. vakata_dnd = {
  6909. element : false,
  6910. target : false,
  6911. is_down : false,
  6912. is_drag : false,
  6913. helper : false,
  6914. helper_w: 0,
  6915. data : false,
  6916. init_x : 0,
  6917. init_y : 0,
  6918. scroll_l: 0,
  6919. scroll_t: 0,
  6920. scroll_e: false,
  6921. scroll_i: false,
  6922. is_touch: false
  6923. };
  6924. $(document).off("mousemove.vakata.jstree touchmove.vakata.jstree", $.vakata.dnd.drag);
  6925. $(document).off("mouseup.vakata.jstree touchend.vakata.jstree", $.vakata.dnd.stop);
  6926. },
  6927. _scroll : function (init_only) {
  6928. if(!vakata_dnd.scroll_e || (!vakata_dnd.scroll_l && !vakata_dnd.scroll_t)) {
  6929. if(vakata_dnd.scroll_i) { clearInterval(vakata_dnd.scroll_i); vakata_dnd.scroll_i = false; }
  6930. return false;
  6931. }
  6932. if(!vakata_dnd.scroll_i) {
  6933. vakata_dnd.scroll_i = setInterval($.vakata.dnd._scroll, 100);
  6934. return false;
  6935. }
  6936. if(init_only === true) { return false; }
  6937. var i = vakata_dnd.scroll_e.scrollTop(),
  6938. j = vakata_dnd.scroll_e.scrollLeft();
  6939. vakata_dnd.scroll_e.scrollTop(i + vakata_dnd.scroll_t * $.vakata.dnd.settings.scroll_speed);
  6940. vakata_dnd.scroll_e.scrollLeft(j + vakata_dnd.scroll_l * $.vakata.dnd.settings.scroll_speed);
  6941. if(i !== vakata_dnd.scroll_e.scrollTop() || j !== vakata_dnd.scroll_e.scrollLeft()) {
  6942. /**
  6943. * triggered on the document when a drag causes an element to scroll
  6944. * @event
  6945. * @plugin dnd
  6946. * @name dnd_scroll.vakata
  6947. * @param {Mixed} data any data supplied with the call to $.vakata.dnd.start
  6948. * @param {DOM} element the DOM element being dragged
  6949. * @param {jQuery} helper the helper shown next to the mouse
  6950. * @param {jQuery} event the element that is scrolling
  6951. */
  6952. $.vakata.dnd._trigger("scroll", vakata_dnd.scroll_e);
  6953. }
  6954. },
  6955. start : function (e, data, html) {
  6956. if(e.type === "touchstart" && e.originalEvent && e.originalEvent.changedTouches && e.originalEvent.changedTouches[0]) {
  6957. e.pageX = e.originalEvent.changedTouches[0].pageX;
  6958. e.pageY = e.originalEvent.changedTouches[0].pageY;
  6959. e.target = document.elementFromPoint(e.originalEvent.changedTouches[0].pageX - window.pageXOffset, e.originalEvent.changedTouches[0].pageY - window.pageYOffset);
  6960. }
  6961. if(vakata_dnd.is_drag) { $.vakata.dnd.stop({}); }
  6962. try {
  6963. e.currentTarget.unselectable = "on";
  6964. e.currentTarget.onselectstart = function() { return false; };
  6965. if(e.currentTarget.style) {
  6966. e.currentTarget.style.touchAction = "none";
  6967. e.currentTarget.style.msTouchAction = "none";
  6968. e.currentTarget.style.MozUserSelect = "none";
  6969. }
  6970. } catch(ignore) { }
  6971. vakata_dnd.init_x = e.pageX;
  6972. vakata_dnd.init_y = e.pageY;
  6973. vakata_dnd.data = data;
  6974. vakata_dnd.is_down = true;
  6975. vakata_dnd.element = e.currentTarget;
  6976. vakata_dnd.target = e.target;
  6977. vakata_dnd.is_touch = e.type === "touchstart";
  6978. if(html !== false) {
  6979. vakata_dnd.helper = $("<div id='vakata-dnd'></div>").html(html).css({
  6980. "display" : "block",
  6981. "margin" : "0",
  6982. "padding" : "0",
  6983. "position" : "absolute",
  6984. "top" : "-2000px",
  6985. "lineHeight" : "16px",
  6986. "zIndex" : "10000"
  6987. });
  6988. }
  6989. $(document).on("mousemove.vakata.jstree touchmove.vakata.jstree", $.vakata.dnd.drag);
  6990. $(document).on("mouseup.vakata.jstree touchend.vakata.jstree", $.vakata.dnd.stop);
  6991. return false;
  6992. },
  6993. drag : function (e) {
  6994. if(e.type === "touchmove" && e.originalEvent && e.originalEvent.changedTouches && e.originalEvent.changedTouches[0]) {
  6995. e.pageX = e.originalEvent.changedTouches[0].pageX;
  6996. e.pageY = e.originalEvent.changedTouches[0].pageY;
  6997. e.target = document.elementFromPoint(e.originalEvent.changedTouches[0].pageX - window.pageXOffset, e.originalEvent.changedTouches[0].pageY - window.pageYOffset);
  6998. }
  6999. if(!vakata_dnd.is_down) { return; }
  7000. if(!vakata_dnd.is_drag) {
  7001. if(
  7002. Math.abs(e.pageX - vakata_dnd.init_x) > (vakata_dnd.is_touch ? $.vakata.dnd.settings.threshold_touch : $.vakata.dnd.settings.threshold) ||
  7003. Math.abs(e.pageY - vakata_dnd.init_y) > (vakata_dnd.is_touch ? $.vakata.dnd.settings.threshold_touch : $.vakata.dnd.settings.threshold)
  7004. ) {
  7005. if(vakata_dnd.helper) {
  7006. vakata_dnd.helper.appendTo(document.body);
  7007. vakata_dnd.helper_w = vakata_dnd.helper.outerWidth();
  7008. }
  7009. vakata_dnd.is_drag = true;
  7010. $(vakata_dnd.target).one('click.vakata', false);
  7011. /**
  7012. * triggered on the document when a drag starts
  7013. * @event
  7014. * @plugin dnd
  7015. * @name dnd_start.vakata
  7016. * @param {Mixed} data any data supplied with the call to $.vakata.dnd.start
  7017. * @param {DOM} element the DOM element being dragged
  7018. * @param {jQuery} helper the helper shown next to the mouse
  7019. * @param {Object} event the event that caused the start (probably mousemove)
  7020. */
  7021. $.vakata.dnd._trigger("start", e);
  7022. }
  7023. else { return; }
  7024. }
  7025. var d = false, w = false,
  7026. dh = false, wh = false,
  7027. dw = false, ww = false,
  7028. dt = false, dl = false,
  7029. ht = false, hl = false;
  7030. vakata_dnd.scroll_t = 0;
  7031. vakata_dnd.scroll_l = 0;
  7032. vakata_dnd.scroll_e = false;
  7033. $($(e.target).parentsUntil("body").addBack().get().reverse())
  7034. .filter(function () {
  7035. return (/^auto|scroll$/).test($(this).css("overflow")) &&
  7036. (this.scrollHeight > this.offsetHeight || this.scrollWidth > this.offsetWidth);
  7037. })
  7038. .each(function () {
  7039. var t = $(this), o = t.offset();
  7040. if(this.scrollHeight > this.offsetHeight) {
  7041. if(o.top + t.height() - e.pageY < $.vakata.dnd.settings.scroll_proximity) { vakata_dnd.scroll_t = 1; }
  7042. if(e.pageY - o.top < $.vakata.dnd.settings.scroll_proximity) { vakata_dnd.scroll_t = -1; }
  7043. }
  7044. if(this.scrollWidth > this.offsetWidth) {
  7045. if(o.left + t.width() - e.pageX < $.vakata.dnd.settings.scroll_proximity) { vakata_dnd.scroll_l = 1; }
  7046. if(e.pageX - o.left < $.vakata.dnd.settings.scroll_proximity) { vakata_dnd.scroll_l = -1; }
  7047. }
  7048. if(vakata_dnd.scroll_t || vakata_dnd.scroll_l) {
  7049. vakata_dnd.scroll_e = $(this);
  7050. return false;
  7051. }
  7052. });
  7053. if(!vakata_dnd.scroll_e) {
  7054. d = $(document); w = $(window);
  7055. dh = d.height(); wh = w.height();
  7056. dw = d.width(); ww = w.width();
  7057. dt = d.scrollTop(); dl = d.scrollLeft();
  7058. if(dh > wh && e.pageY - dt < $.vakata.dnd.settings.scroll_proximity) { vakata_dnd.scroll_t = -1; }
  7059. if(dh > wh && wh - (e.pageY - dt) < $.vakata.dnd.settings.scroll_proximity) { vakata_dnd.scroll_t = 1; }
  7060. if(dw > ww && e.pageX - dl < $.vakata.dnd.settings.scroll_proximity) { vakata_dnd.scroll_l = -1; }
  7061. if(dw > ww && ww - (e.pageX - dl) < $.vakata.dnd.settings.scroll_proximity) { vakata_dnd.scroll_l = 1; }
  7062. if(vakata_dnd.scroll_t || vakata_dnd.scroll_l) {
  7063. vakata_dnd.scroll_e = d;
  7064. }
  7065. }
  7066. if(vakata_dnd.scroll_e) { $.vakata.dnd._scroll(true); }
  7067. if(vakata_dnd.helper) {
  7068. ht = parseInt(e.pageY + $.vakata.dnd.settings.helper_top, 10);
  7069. hl = parseInt(e.pageX + $.vakata.dnd.settings.helper_left, 10);
  7070. if(dh && ht + 25 > dh) { ht = dh - 50; }
  7071. if(dw && hl + vakata_dnd.helper_w > dw) { hl = dw - (vakata_dnd.helper_w + 2); }
  7072. vakata_dnd.helper.css({
  7073. left : hl + "px",
  7074. top : ht + "px"
  7075. });
  7076. }
  7077. /**
  7078. * triggered on the document when a drag is in progress
  7079. * @event
  7080. * @plugin dnd
  7081. * @name dnd_move.vakata
  7082. * @param {Mixed} data any data supplied with the call to $.vakata.dnd.start
  7083. * @param {DOM} element the DOM element being dragged
  7084. * @param {jQuery} helper the helper shown next to the mouse
  7085. * @param {Object} event the event that caused this to trigger (most likely mousemove)
  7086. */
  7087. $.vakata.dnd._trigger("move", e);
  7088. return false;
  7089. },
  7090. stop : function (e) {
  7091. if(e.type === "touchend" && e.originalEvent && e.originalEvent.changedTouches && e.originalEvent.changedTouches[0]) {
  7092. e.pageX = e.originalEvent.changedTouches[0].pageX;
  7093. e.pageY = e.originalEvent.changedTouches[0].pageY;
  7094. e.target = document.elementFromPoint(e.originalEvent.changedTouches[0].pageX - window.pageXOffset, e.originalEvent.changedTouches[0].pageY - window.pageYOffset);
  7095. }
  7096. if(vakata_dnd.is_drag) {
  7097. /**
  7098. * triggered on the document when a drag stops (the dragged element is dropped)
  7099. * @event
  7100. * @plugin dnd
  7101. * @name dnd_stop.vakata
  7102. * @param {Mixed} data any data supplied with the call to $.vakata.dnd.start
  7103. * @param {DOM} element the DOM element being dragged
  7104. * @param {jQuery} helper the helper shown next to the mouse
  7105. * @param {Object} event the event that caused the stop
  7106. */
  7107. if (e.target !== vakata_dnd.target) {
  7108. $(vakata_dnd.target).off('click.vakata');
  7109. }
  7110. $.vakata.dnd._trigger("stop", e);
  7111. }
  7112. else {
  7113. if(e.type === "touchend" && e.target === vakata_dnd.target) {
  7114. var to = setTimeout(function () { $(e.target).click(); }, 100);
  7115. $(e.target).one('click', function() { if(to) { clearTimeout(to); } });
  7116. }
  7117. }
  7118. $.vakata.dnd._clean();
  7119. return false;
  7120. }
  7121. };
  7122. }($));
  7123. // include the dnd plugin by default
  7124. // $.jstree.defaults.plugins.push("dnd");
  7125. /**
  7126. * ### Massload plugin
  7127. *
  7128. * Adds massload functionality to jsTree, so that multiple nodes can be loaded in a single request (only useful with lazy loading).
  7129. */
  7130. /**
  7131. * massload configuration
  7132. *
  7133. * It is possible to set this to a standard jQuery-like AJAX config.
  7134. * In addition to the standard jQuery ajax options here you can supply functions for `data` and `url`, the functions will be run in the current instance's scope and a param will be passed indicating which node IDs need to be loaded, the return value of those functions will be used.
  7135. *
  7136. * You can also set this to a function, that function will receive the node IDs being loaded as argument and a second param which is a function (callback) which should be called with the result.
  7137. *
  7138. * Both the AJAX and the function approach rely on the same return value - an object where the keys are the node IDs, and the value is the children of that node as an array.
  7139. *
  7140. * {
  7141. * "id1" : [{ "text" : "Child of ID1", "id" : "c1" }, { "text" : "Another child of ID1", "id" : "c2" }],
  7142. * "id2" : [{ "text" : "Child of ID2", "id" : "c3" }]
  7143. * }
  7144. *
  7145. * @name $.jstree.defaults.massload
  7146. * @plugin massload
  7147. */
  7148. $.jstree.defaults.massload = null;
  7149. $.jstree.plugins.massload = function (options, parent) {
  7150. this.init = function (el, options) {
  7151. this._data.massload = {};
  7152. parent.init.call(this, el, options);
  7153. };
  7154. this._load_nodes = function (nodes, callback, is_callback, force_reload) {
  7155. var s = this.settings.massload,
  7156. nodesString = JSON.stringify(nodes),
  7157. toLoad = [],
  7158. m = this._model.data,
  7159. i, j, dom;
  7160. if (!is_callback) {
  7161. for(i = 0, j = nodes.length; i < j; i++) {
  7162. if(!m[nodes[i]] || ( (!m[nodes[i]].state.loaded && !m[nodes[i]].state.failed) || force_reload) ) {
  7163. toLoad.push(nodes[i]);
  7164. dom = this.get_node(nodes[i], true);
  7165. if (dom && dom.length) {
  7166. dom.addClass("jstree-loading").attr('aria-busy',true);
  7167. }
  7168. }
  7169. }
  7170. this._data.massload = {};
  7171. if (toLoad.length) {
  7172. if($.isFunction(s)) {
  7173. return s.call(this, toLoad, $.proxy(function (data) {
  7174. var i, j;
  7175. if(data) {
  7176. for(i in data) {
  7177. if(data.hasOwnProperty(i)) {
  7178. this._data.massload[i] = data[i];
  7179. }
  7180. }
  7181. }
  7182. for(i = 0, j = nodes.length; i < j; i++) {
  7183. dom = this.get_node(nodes[i], true);
  7184. if (dom && dom.length) {
  7185. dom.removeClass("jstree-loading").attr('aria-busy',false);
  7186. }
  7187. }
  7188. parent._load_nodes.call(this, nodes, callback, is_callback, force_reload);
  7189. }, this));
  7190. }
  7191. if(typeof s === 'object' && s && s.url) {
  7192. s = $.extend(true, {}, s);
  7193. if($.isFunction(s.url)) {
  7194. s.url = s.url.call(this, toLoad);
  7195. }
  7196. if($.isFunction(s.data)) {
  7197. s.data = s.data.call(this, toLoad);
  7198. }
  7199. return $.ajax(s)
  7200. .done($.proxy(function (data,t,x) {
  7201. var i, j;
  7202. if(data) {
  7203. for(i in data) {
  7204. if(data.hasOwnProperty(i)) {
  7205. this._data.massload[i] = data[i];
  7206. }
  7207. }
  7208. }
  7209. for(i = 0, j = nodes.length; i < j; i++) {
  7210. dom = this.get_node(nodes[i], true);
  7211. if (dom && dom.length) {
  7212. dom.removeClass("jstree-loading").attr('aria-busy',false);
  7213. }
  7214. }
  7215. parent._load_nodes.call(this, nodes, callback, is_callback, force_reload);
  7216. }, this))
  7217. .fail($.proxy(function (f) {
  7218. parent._load_nodes.call(this, nodes, callback, is_callback, force_reload);
  7219. }, this));
  7220. }
  7221. }
  7222. }
  7223. return parent._load_nodes.call(this, nodes, callback, is_callback, force_reload);
  7224. };
  7225. this._load_node = function (obj, callback) {
  7226. var data = this._data.massload[obj.id],
  7227. rslt = null, dom;
  7228. if(data) {
  7229. rslt = this[typeof data === 'string' ? '_append_html_data' : '_append_json_data'](
  7230. obj,
  7231. typeof data === 'string' ? $($.parseHTML(data)).filter(function () { return this.nodeType !== 3; }) : data,
  7232. function (status) { callback.call(this, status); }
  7233. );
  7234. dom = this.get_node(obj.id, true);
  7235. if (dom && dom.length) {
  7236. dom.removeClass("jstree-loading").attr('aria-busy',false);
  7237. }
  7238. delete this._data.massload[obj.id];
  7239. return rslt;
  7240. }
  7241. return parent._load_node.call(this, obj, callback);
  7242. };
  7243. };
  7244. /**
  7245. * ### Search plugin
  7246. *
  7247. * Adds search functionality to jsTree.
  7248. */
  7249. /**
  7250. * stores all defaults for the search plugin
  7251. * @name $.jstree.defaults.search
  7252. * @plugin search
  7253. */
  7254. $.jstree.defaults.search = {
  7255. /**
  7256. * a jQuery-like AJAX config, which jstree uses if a server should be queried for results.
  7257. *
  7258. * A `str` (which is the search string) parameter will be added with the request, an optional `inside` parameter will be added if the search is limited to a node id. The expected result is a JSON array with nodes that need to be opened so that matching nodes will be revealed.
  7259. * Leave this setting as `false` to not query the server. You can also set this to a function, which will be invoked in the instance's scope and receive 3 parameters - the search string, the callback to call with the array of nodes to load, and the optional node ID to limit the search to
  7260. * @name $.jstree.defaults.search.ajax
  7261. * @plugin search
  7262. */
  7263. ajax : false,
  7264. /**
  7265. * Indicates if the search should be fuzzy or not (should `chnd3` match `child node 3`). Default is `false`.
  7266. * @name $.jstree.defaults.search.fuzzy
  7267. * @plugin search
  7268. */
  7269. fuzzy : false,
  7270. /**
  7271. * Indicates if the search should be case sensitive. Default is `false`.
  7272. * @name $.jstree.defaults.search.case_sensitive
  7273. * @plugin search
  7274. */
  7275. case_sensitive : false,
  7276. /**
  7277. * Indicates if the tree should be filtered (by default) to show only matching nodes (keep in mind this can be a heavy on large trees in old browsers).
  7278. * This setting can be changed at runtime when calling the search method. Default is `false`.
  7279. * @name $.jstree.defaults.search.show_only_matches
  7280. * @plugin search
  7281. */
  7282. show_only_matches : false,
  7283. /**
  7284. * Indicates if the children of matched element are shown (when show_only_matches is true)
  7285. * This setting can be changed at runtime when calling the search method. Default is `false`.
  7286. * @name $.jstree.defaults.search.show_only_matches_children
  7287. * @plugin search
  7288. */
  7289. show_only_matches_children : false,
  7290. /**
  7291. * Indicates if all nodes opened to reveal the search result, should be closed when the search is cleared or a new search is performed. Default is `true`.
  7292. * @name $.jstree.defaults.search.close_opened_onclear
  7293. * @plugin search
  7294. */
  7295. close_opened_onclear : true,
  7296. /**
  7297. * Indicates if only leaf nodes should be included in search results. Default is `false`.
  7298. * @name $.jstree.defaults.search.search_leaves_only
  7299. * @plugin search
  7300. */
  7301. search_leaves_only : false,
  7302. /**
  7303. * If set to a function it wil be called in the instance's scope with two arguments - search string and node (where node will be every node in the structure, so use with caution).
  7304. * If the function returns a truthy value the node will be considered a match (it might not be displayed if search_only_leaves is set to true and the node is not a leaf). Default is `false`.
  7305. * @name $.jstree.defaults.search.search_callback
  7306. * @plugin search
  7307. */
  7308. search_callback : false
  7309. };
  7310. $.jstree.plugins.search = function (options, parent) {
  7311. this.bind = function () {
  7312. parent.bind.call(this);
  7313. this._data.search.str = "";
  7314. this._data.search.dom = $();
  7315. this._data.search.res = [];
  7316. this._data.search.opn = [];
  7317. this._data.search.som = false;
  7318. this._data.search.smc = false;
  7319. this._data.search.hdn = [];
  7320. this.element
  7321. .on("search.jstree", $.proxy(function (e, data) {
  7322. if(this._data.search.som && data.res.length) {
  7323. var m = this._model.data, i, j, p = [], k, l;
  7324. for(i = 0, j = data.res.length; i < j; i++) {
  7325. if(m[data.res[i]] && !m[data.res[i]].state.hidden) {
  7326. p.push(data.res[i]);
  7327. p = p.concat(m[data.res[i]].parents);
  7328. if(this._data.search.smc) {
  7329. for (k = 0, l = m[data.res[i]].children_d.length; k < l; k++) {
  7330. if (m[m[data.res[i]].children_d[k]] && !m[m[data.res[i]].children_d[k]].state.hidden) {
  7331. p.push(m[data.res[i]].children_d[k]);
  7332. }
  7333. }
  7334. }
  7335. }
  7336. }
  7337. p = $.vakata.array_remove_item($.vakata.array_unique(p), $.jstree.root);
  7338. this._data.search.hdn = this.hide_all(true);
  7339. this.show_node(p, true);
  7340. this.redraw(true);
  7341. }
  7342. }, this))
  7343. .on("clear_search.jstree", $.proxy(function (e, data) {
  7344. if(this._data.search.som && data.res.length) {
  7345. this.show_node(this._data.search.hdn, true);
  7346. this.redraw(true);
  7347. }
  7348. }, this));
  7349. };
  7350. /**
  7351. * used to search the tree nodes for a given string
  7352. * @name search(str [, skip_async])
  7353. * @param {String} str the search string
  7354. * @param {Boolean} skip_async if set to true server will not be queried even if configured
  7355. * @param {Boolean} show_only_matches if set to true only matching nodes will be shown (keep in mind this can be very slow on large trees or old browsers)
  7356. * @param {mixed} inside an optional node to whose children to limit the search
  7357. * @param {Boolean} append if set to true the results of this search are appended to the previous search
  7358. * @plugin search
  7359. * @trigger search.jstree
  7360. */
  7361. this.search = function (str, skip_async, show_only_matches, inside, append, show_only_matches_children) {
  7362. if(str === false || $.trim(str.toString()) === "") {
  7363. return this.clear_search();
  7364. }
  7365. inside = this.get_node(inside);
  7366. inside = inside && inside.id ? inside.id : null;
  7367. str = str.toString();
  7368. var s = this.settings.search,
  7369. a = s.ajax ? s.ajax : false,
  7370. m = this._model.data,
  7371. f = null,
  7372. r = [],
  7373. p = [], i, j;
  7374. if(this._data.search.res.length && !append) {
  7375. this.clear_search();
  7376. }
  7377. if(show_only_matches === undefined) {
  7378. show_only_matches = s.show_only_matches;
  7379. }
  7380. if(show_only_matches_children === undefined) {
  7381. show_only_matches_children = s.show_only_matches_children;
  7382. }
  7383. if(!skip_async && a !== false) {
  7384. if($.isFunction(a)) {
  7385. return a.call(this, str, $.proxy(function (d) {
  7386. if(d && d.d) { d = d.d; }
  7387. this._load_nodes(!$.isArray(d) ? [] : $.vakata.array_unique(d), function () {
  7388. this.search(str, true, show_only_matches, inside, append, show_only_matches_children);
  7389. });
  7390. }, this), inside);
  7391. }
  7392. else {
  7393. a = $.extend({}, a);
  7394. if(!a.data) { a.data = {}; }
  7395. a.data.str = str;
  7396. if(inside) {
  7397. a.data.inside = inside;
  7398. }
  7399. if (this._data.search.lastRequest) {
  7400. this._data.search.lastRequest.abort();
  7401. }
  7402. this._data.search.lastRequest = $.ajax(a)
  7403. .fail($.proxy(function () {
  7404. this._data.core.last_error = { 'error' : 'ajax', 'plugin' : 'search', 'id' : 'search_01', 'reason' : 'Could not load search parents', 'data' : JSON.stringify(a) };
  7405. this.settings.core.error.call(this, this._data.core.last_error);
  7406. }, this))
  7407. .done($.proxy(function (d) {
  7408. if(d && d.d) { d = d.d; }
  7409. this._load_nodes(!$.isArray(d) ? [] : $.vakata.array_unique(d), function () {
  7410. this.search(str, true, show_only_matches, inside, append, show_only_matches_children);
  7411. });
  7412. }, this));
  7413. return this._data.search.lastRequest;
  7414. }
  7415. }
  7416. if(!append) {
  7417. this._data.search.str = str;
  7418. this._data.search.dom = $();
  7419. this._data.search.res = [];
  7420. this._data.search.opn = [];
  7421. this._data.search.som = show_only_matches;
  7422. this._data.search.smc = show_only_matches_children;
  7423. }
  7424. f = new $.vakata.search(str, true, { caseSensitive : s.case_sensitive, fuzzy : s.fuzzy });
  7425. $.each(m[inside ? inside : $.jstree.root].children_d, function (ii, i) {
  7426. var v = m[i];
  7427. if(v.text && !v.state.hidden && (!s.search_leaves_only || (v.state.loaded && v.children.length === 0)) && ( (s.search_callback && s.search_callback.call(this, str, v)) || (!s.search_callback && f.search(v.text).isMatch) ) ) {
  7428. r.push(i);
  7429. p = p.concat(v.parents);
  7430. }
  7431. });
  7432. if(r.length) {
  7433. p = $.vakata.array_unique(p);
  7434. for(i = 0, j = p.length; i < j; i++) {
  7435. if(p[i] !== $.jstree.root && m[p[i]] && this.open_node(p[i], null, 0) === true) {
  7436. this._data.search.opn.push(p[i]);
  7437. }
  7438. }
  7439. if(!append) {
  7440. this._data.search.dom = $(this.element[0].querySelectorAll('#' + $.map(r, function (v) { return "0123456789".indexOf(v[0]) !== -1 ? '\\3' + v[0] + ' ' + v.substr(1).replace($.jstree.idregex,'\\$&') : v.replace($.jstree.idregex,'\\$&'); }).join(', #')));
  7441. this._data.search.res = r;
  7442. }
  7443. else {
  7444. this._data.search.dom = this._data.search.dom.add($(this.element[0].querySelectorAll('#' + $.map(r, function (v) { return "0123456789".indexOf(v[0]) !== -1 ? '\\3' + v[0] + ' ' + v.substr(1).replace($.jstree.idregex,'\\$&') : v.replace($.jstree.idregex,'\\$&'); }).join(', #'))));
  7445. this._data.search.res = $.vakata.array_unique(this._data.search.res.concat(r));
  7446. }
  7447. this._data.search.dom.children(".jstree-anchor").addClass('jstree-search');
  7448. }
  7449. /**
  7450. * triggered after search is complete
  7451. * @event
  7452. * @name search.jstree
  7453. * @param {jQuery} nodes a jQuery collection of matching nodes
  7454. * @param {String} str the search string
  7455. * @param {Array} res a collection of objects represeing the matching nodes
  7456. * @plugin search
  7457. */
  7458. this.trigger('search', { nodes : this._data.search.dom, str : str, res : this._data.search.res, show_only_matches : show_only_matches });
  7459. };
  7460. /**
  7461. * used to clear the last search (removes classes and shows all nodes if filtering is on)
  7462. * @name clear_search()
  7463. * @plugin search
  7464. * @trigger clear_search.jstree
  7465. */
  7466. this.clear_search = function () {
  7467. if(this.settings.search.close_opened_onclear) {
  7468. this.close_node(this._data.search.opn, 0);
  7469. }
  7470. /**
  7471. * triggered after search is complete
  7472. * @event
  7473. * @name clear_search.jstree
  7474. * @param {jQuery} nodes a jQuery collection of matching nodes (the result from the last search)
  7475. * @param {String} str the search string (the last search string)
  7476. * @param {Array} res a collection of objects represeing the matching nodes (the result from the last search)
  7477. * @plugin search
  7478. */
  7479. this.trigger('clear_search', { 'nodes' : this._data.search.dom, str : this._data.search.str, res : this._data.search.res });
  7480. if(this._data.search.res.length) {
  7481. this._data.search.dom = $(this.element[0].querySelectorAll('#' + $.map(this._data.search.res, function (v) {
  7482. return "0123456789".indexOf(v[0]) !== -1 ? '\\3' + v[0] + ' ' + v.substr(1).replace($.jstree.idregex,'\\$&') : v.replace($.jstree.idregex,'\\$&');
  7483. }).join(', #')));
  7484. this._data.search.dom.children(".jstree-anchor").removeClass("jstree-search");
  7485. }
  7486. this._data.search.str = "";
  7487. this._data.search.res = [];
  7488. this._data.search.opn = [];
  7489. this._data.search.dom = $();
  7490. };
  7491. this.redraw_node = function(obj, deep, callback, force_render) {
  7492. obj = parent.redraw_node.apply(this, arguments);
  7493. if(obj) {
  7494. if($.inArray(obj.id, this._data.search.res) !== -1) {
  7495. var i, j, tmp = null;
  7496. for(i = 0, j = obj.childNodes.length; i < j; i++) {
  7497. if(obj.childNodes[i] && obj.childNodes[i].className && obj.childNodes[i].className.indexOf("jstree-anchor") !== -1) {
  7498. tmp = obj.childNodes[i];
  7499. break;
  7500. }
  7501. }
  7502. if(tmp) {
  7503. tmp.className += ' jstree-search';
  7504. }
  7505. }
  7506. }
  7507. return obj;
  7508. };
  7509. };
  7510. // helpers
  7511. (function ($) {
  7512. // from http://kiro.me/projects/fuse.html
  7513. $.vakata.search = function(pattern, txt, options) {
  7514. options = options || {};
  7515. options = $.extend({}, $.vakata.search.defaults, options);
  7516. if(options.fuzzy !== false) {
  7517. options.fuzzy = true;
  7518. }
  7519. pattern = options.caseSensitive ? pattern : pattern.toLowerCase();
  7520. var MATCH_LOCATION = options.location,
  7521. MATCH_DISTANCE = options.distance,
  7522. MATCH_THRESHOLD = options.threshold,
  7523. patternLen = pattern.length,
  7524. matchmask, pattern_alphabet, match_bitapScore, search;
  7525. if(patternLen > 32) {
  7526. options.fuzzy = false;
  7527. }
  7528. if(options.fuzzy) {
  7529. matchmask = 1 << (patternLen - 1);
  7530. pattern_alphabet = (function () {
  7531. var mask = {},
  7532. i = 0;
  7533. for (i = 0; i < patternLen; i++) {
  7534. mask[pattern.charAt(i)] = 0;
  7535. }
  7536. for (i = 0; i < patternLen; i++) {
  7537. mask[pattern.charAt(i)] |= 1 << (patternLen - i - 1);
  7538. }
  7539. return mask;
  7540. }());
  7541. match_bitapScore = function (e, x) {
  7542. var accuracy = e / patternLen,
  7543. proximity = Math.abs(MATCH_LOCATION - x);
  7544. if(!MATCH_DISTANCE) {
  7545. return proximity ? 1.0 : accuracy;
  7546. }
  7547. return accuracy + (proximity / MATCH_DISTANCE);
  7548. };
  7549. }
  7550. search = function (text) {
  7551. text = options.caseSensitive ? text : text.toLowerCase();
  7552. if(pattern === text || text.indexOf(pattern) !== -1) {
  7553. return {
  7554. isMatch: true,
  7555. score: 0
  7556. };
  7557. }
  7558. if(!options.fuzzy) {
  7559. return {
  7560. isMatch: false,
  7561. score: 1
  7562. };
  7563. }
  7564. var i, j,
  7565. textLen = text.length,
  7566. scoreThreshold = MATCH_THRESHOLD,
  7567. bestLoc = text.indexOf(pattern, MATCH_LOCATION),
  7568. binMin, binMid,
  7569. binMax = patternLen + textLen,
  7570. lastRd, start, finish, rd, charMatch,
  7571. score = 1,
  7572. locations = [];
  7573. if (bestLoc !== -1) {
  7574. scoreThreshold = Math.min(match_bitapScore(0, bestLoc), scoreThreshold);
  7575. bestLoc = text.lastIndexOf(pattern, MATCH_LOCATION + patternLen);
  7576. if (bestLoc !== -1) {
  7577. scoreThreshold = Math.min(match_bitapScore(0, bestLoc), scoreThreshold);
  7578. }
  7579. }
  7580. bestLoc = -1;
  7581. for (i = 0; i < patternLen; i++) {
  7582. binMin = 0;
  7583. binMid = binMax;
  7584. while (binMin < binMid) {
  7585. if (match_bitapScore(i, MATCH_LOCATION + binMid) <= scoreThreshold) {
  7586. binMin = binMid;
  7587. } else {
  7588. binMax = binMid;
  7589. }
  7590. binMid = Math.floor((binMax - binMin) / 2 + binMin);
  7591. }
  7592. binMax = binMid;
  7593. start = Math.max(1, MATCH_LOCATION - binMid + 1);
  7594. finish = Math.min(MATCH_LOCATION + binMid, textLen) + patternLen;
  7595. rd = new Array(finish + 2);
  7596. rd[finish + 1] = (1 << i) - 1;
  7597. for (j = finish; j >= start; j--) {
  7598. charMatch = pattern_alphabet[text.charAt(j - 1)];
  7599. if (i === 0) {
  7600. rd[j] = ((rd[j + 1] << 1) | 1) & charMatch;
  7601. } else {
  7602. rd[j] = ((rd[j + 1] << 1) | 1) & charMatch | (((lastRd[j + 1] | lastRd[j]) << 1) | 1) | lastRd[j + 1];
  7603. }
  7604. if (rd[j] & matchmask) {
  7605. score = match_bitapScore(i, j - 1);
  7606. if (score <= scoreThreshold) {
  7607. scoreThreshold = score;
  7608. bestLoc = j - 1;
  7609. locations.push(bestLoc);
  7610. if (bestLoc > MATCH_LOCATION) {
  7611. start = Math.max(1, 2 * MATCH_LOCATION - bestLoc);
  7612. } else {
  7613. break;
  7614. }
  7615. }
  7616. }
  7617. }
  7618. if (match_bitapScore(i + 1, MATCH_LOCATION) > scoreThreshold) {
  7619. break;
  7620. }
  7621. lastRd = rd;
  7622. }
  7623. return {
  7624. isMatch: bestLoc >= 0,
  7625. score: score
  7626. };
  7627. };
  7628. return txt === true ? { 'search' : search } : search(txt);
  7629. };
  7630. $.vakata.search.defaults = {
  7631. location : 0,
  7632. distance : 100,
  7633. threshold : 0.6,
  7634. fuzzy : false,
  7635. caseSensitive : false
  7636. };
  7637. }($));
  7638. // include the search plugin by default
  7639. // $.jstree.defaults.plugins.push("search");
  7640. /**
  7641. * ### Sort plugin
  7642. *
  7643. * Automatically sorts all siblings in the tree according to a sorting function.
  7644. */
  7645. /**
  7646. * the settings function used to sort the nodes.
  7647. * It is executed in the tree's context, accepts two nodes as arguments and should return `1` or `-1`.
  7648. * @name $.jstree.defaults.sort
  7649. * @plugin sort
  7650. */
  7651. $.jstree.defaults.sort = function (a, b) {
  7652. //return this.get_type(a) === this.get_type(b) ? (this.get_text(a) > this.get_text(b) ? 1 : -1) : this.get_type(a) >= this.get_type(b);
  7653. return this.get_text(a) > this.get_text(b) ? 1 : -1;
  7654. };
  7655. $.jstree.plugins.sort = function (options, parent) {
  7656. this.bind = function () {
  7657. parent.bind.call(this);
  7658. this.element
  7659. .on("model.jstree", $.proxy(function (e, data) {
  7660. this.sort(data.parent, true);
  7661. }, this))
  7662. .on("rename_node.jstree create_node.jstree", $.proxy(function (e, data) {
  7663. this.sort(data.parent || data.node.parent, false);
  7664. this.redraw_node(data.parent || data.node.parent, true);
  7665. }, this))
  7666. .on("move_node.jstree copy_node.jstree", $.proxy(function (e, data) {
  7667. this.sort(data.parent, false);
  7668. this.redraw_node(data.parent, true);
  7669. }, this));
  7670. };
  7671. /**
  7672. * used to sort a node's children
  7673. * @private
  7674. * @name sort(obj [, deep])
  7675. * @param {mixed} obj the node
  7676. * @param {Boolean} deep if set to `true` nodes are sorted recursively.
  7677. * @plugin sort
  7678. * @trigger search.jstree
  7679. */
  7680. this.sort = function (obj, deep) {
  7681. var i, j;
  7682. obj = this.get_node(obj);
  7683. if(obj && obj.children && obj.children.length) {
  7684. obj.children.sort($.proxy(this.settings.sort, this));
  7685. if(deep) {
  7686. for(i = 0, j = obj.children_d.length; i < j; i++) {
  7687. this.sort(obj.children_d[i], false);
  7688. }
  7689. }
  7690. }
  7691. };
  7692. };
  7693. // include the sort plugin by default
  7694. // $.jstree.defaults.plugins.push("sort");
  7695. /**
  7696. * ### State plugin
  7697. *
  7698. * Saves the state of the tree (selected nodes, opened nodes) on the user's computer using available options (localStorage, cookies, etc)
  7699. */
  7700. var to = false;
  7701. /**
  7702. * stores all defaults for the state plugin
  7703. * @name $.jstree.defaults.state
  7704. * @plugin state
  7705. */
  7706. $.jstree.defaults.state = {
  7707. /**
  7708. * A string for the key to use when saving the current tree (change if using multiple trees in your project). Defaults to `jstree`.
  7709. * @name $.jstree.defaults.state.key
  7710. * @plugin state
  7711. */
  7712. key : 'jstree',
  7713. /**
  7714. * A space separated list of events that trigger a state save. Defaults to `changed.jstree open_node.jstree close_node.jstree`.
  7715. * @name $.jstree.defaults.state.events
  7716. * @plugin state
  7717. */
  7718. events : 'changed.jstree open_node.jstree close_node.jstree check_node.jstree uncheck_node.jstree',
  7719. /**
  7720. * Time in milliseconds after which the state will expire. Defaults to 'false' meaning - no expire.
  7721. * @name $.jstree.defaults.state.ttl
  7722. * @plugin state
  7723. */
  7724. ttl : false,
  7725. /**
  7726. * A function that will be executed prior to restoring state with one argument - the state object. Can be used to clear unwanted parts of the state.
  7727. * @name $.jstree.defaults.state.filter
  7728. * @plugin state
  7729. */
  7730. filter : false,
  7731. /**
  7732. * Should loaded nodes be restored (setting this to true means that it is possible that the whole tree will be loaded for some users - use with caution). Defaults to `false`
  7733. * @name $.jstree.defaults.state.preserve_loaded
  7734. * @plugin state
  7735. */
  7736. preserve_loaded : false
  7737. };
  7738. $.jstree.plugins.state = function (options, parent) {
  7739. this.bind = function () {
  7740. parent.bind.call(this);
  7741. var bind = $.proxy(function () {
  7742. this.element.on(this.settings.state.events, $.proxy(function () {
  7743. if(to) { clearTimeout(to); }
  7744. to = setTimeout($.proxy(function () { this.save_state(); }, this), 100);
  7745. }, this));
  7746. /**
  7747. * triggered when the state plugin is finished restoring the state (and immediately after ready if there is no state to restore).
  7748. * @event
  7749. * @name state_ready.jstree
  7750. * @plugin state
  7751. */
  7752. this.trigger('state_ready');
  7753. }, this);
  7754. this.element
  7755. .on("ready.jstree", $.proxy(function (e, data) {
  7756. this.element.one("restore_state.jstree", bind);
  7757. if(!this.restore_state()) { bind(); }
  7758. }, this));
  7759. };
  7760. /**
  7761. * save the state
  7762. * @name save_state()
  7763. * @plugin state
  7764. */
  7765. this.save_state = function () {
  7766. var tm = this.get_state();
  7767. if (!this.settings.state.preserve_loaded) {
  7768. delete tm.core.loaded;
  7769. }
  7770. var st = { 'state' : tm, 'ttl' : this.settings.state.ttl, 'sec' : +(new Date()) };
  7771. $.vakata.storage.set(this.settings.state.key, JSON.stringify(st));
  7772. };
  7773. /**
  7774. * restore the state from the user's computer
  7775. * @name restore_state()
  7776. * @plugin state
  7777. */
  7778. this.restore_state = function () {
  7779. var k = $.vakata.storage.get(this.settings.state.key);
  7780. if(!!k) { try { k = JSON.parse(k); } catch(ex) { return false; } }
  7781. if(!!k && k.ttl && k.sec && +(new Date()) - k.sec > k.ttl) { return false; }
  7782. if(!!k && k.state) { k = k.state; }
  7783. if(!!k && $.isFunction(this.settings.state.filter)) { k = this.settings.state.filter.call(this, k); }
  7784. if(!!k) {
  7785. if (!this.settings.state.preserve_loaded) {
  7786. delete k.core.loaded;
  7787. }
  7788. this.element.one("set_state.jstree", function (e, data) { data.instance.trigger('restore_state', { 'state' : $.extend(true, {}, k) }); });
  7789. this.set_state(k);
  7790. return true;
  7791. }
  7792. return false;
  7793. };
  7794. /**
  7795. * clear the state on the user's computer
  7796. * @name clear_state()
  7797. * @plugin state
  7798. */
  7799. this.clear_state = function () {
  7800. return $.vakata.storage.del(this.settings.state.key);
  7801. };
  7802. };
  7803. (function ($, undefined) {
  7804. $.vakata.storage = {
  7805. // simply specifying the functions in FF throws an error
  7806. set : function (key, val) { return window.localStorage.setItem(key, val); },
  7807. get : function (key) { return window.localStorage.getItem(key); },
  7808. del : function (key) { return window.localStorage.removeItem(key); }
  7809. };
  7810. }($));
  7811. // include the state plugin by default
  7812. // $.jstree.defaults.plugins.push("state");
  7813. /**
  7814. * ### Types plugin
  7815. *
  7816. * Makes it possible to add predefined types for groups of nodes, which make it possible to easily control nesting rules and icon for each group.
  7817. */
  7818. /**
  7819. * An object storing all types as key value pairs, where the key is the type name and the value is an object that could contain following keys (all optional).
  7820. *
  7821. * * `max_children` the maximum number of immediate children this node type can have. Do not specify or set to `-1` for unlimited.
  7822. * * `max_depth` the maximum number of nesting this node type can have. A value of `1` would mean that the node can have children, but no grandchildren. Do not specify or set to `-1` for unlimited.
  7823. * * `valid_children` an array of node type strings, that nodes of this type can have as children. Do not specify or set to `-1` for no limits.
  7824. * * `icon` a string - can be a path to an icon or a className, if using an image that is in the current directory use a `./` prefix, otherwise it will be detected as a class. Omit to use the default icon from your theme.
  7825. * * `li_attr` an object of values which will be used to add HTML attributes on the resulting LI DOM node (merged with the node's own data)
  7826. * * `a_attr` an object of values which will be used to add HTML attributes on the resulting A DOM node (merged with the node's own data)
  7827. *
  7828. * There are two predefined types:
  7829. *
  7830. * * `#` represents the root of the tree, for example `max_children` would control the maximum number of root nodes.
  7831. * * `default` represents the default node - any settings here will be applied to all nodes that do not have a type specified.
  7832. *
  7833. * @name $.jstree.defaults.types
  7834. * @plugin types
  7835. */
  7836. $.jstree.defaults.types = {
  7837. 'default' : {}
  7838. };
  7839. $.jstree.defaults.types[$.jstree.root] = {};
  7840. $.jstree.plugins.types = function (options, parent) {
  7841. this.init = function (el, options) {
  7842. var i, j;
  7843. if(options && options.types && options.types['default']) {
  7844. for(i in options.types) {
  7845. if(i !== "default" && i !== $.jstree.root && options.types.hasOwnProperty(i)) {
  7846. for(j in options.types['default']) {
  7847. if(options.types['default'].hasOwnProperty(j) && options.types[i][j] === undefined) {
  7848. options.types[i][j] = options.types['default'][j];
  7849. }
  7850. }
  7851. }
  7852. }
  7853. }
  7854. parent.init.call(this, el, options);
  7855. this._model.data[$.jstree.root].type = $.jstree.root;
  7856. };
  7857. this.refresh = function (skip_loading, forget_state) {
  7858. parent.refresh.call(this, skip_loading, forget_state);
  7859. this._model.data[$.jstree.root].type = $.jstree.root;
  7860. };
  7861. this.bind = function () {
  7862. this.element
  7863. .on('model.jstree', $.proxy(function (e, data) {
  7864. var m = this._model.data,
  7865. dpc = data.nodes,
  7866. t = this.settings.types,
  7867. i, j, c = 'default', k;
  7868. for(i = 0, j = dpc.length; i < j; i++) {
  7869. c = 'default';
  7870. if(m[dpc[i]].original && m[dpc[i]].original.type && t[m[dpc[i]].original.type]) {
  7871. c = m[dpc[i]].original.type;
  7872. }
  7873. if(m[dpc[i]].data && m[dpc[i]].data.jstree && m[dpc[i]].data.jstree.type && t[m[dpc[i]].data.jstree.type]) {
  7874. c = m[dpc[i]].data.jstree.type;
  7875. }
  7876. m[dpc[i]].type = c;
  7877. if(m[dpc[i]].icon === true && t[c].icon !== undefined) {
  7878. m[dpc[i]].icon = t[c].icon;
  7879. }
  7880. if(t[c].li_attr !== undefined && typeof t[c].li_attr === 'object') {
  7881. for (k in t[c].li_attr) {
  7882. if (t[c].li_attr.hasOwnProperty(k)) {
  7883. if (k === 'id') {
  7884. continue;
  7885. }
  7886. else if (m[dpc[i]].li_attr[k] === undefined) {
  7887. m[dpc[i]].li_attr[k] = t[c].li_attr[k];
  7888. }
  7889. else if (k === 'class') {
  7890. m[dpc[i]].li_attr['class'] = t[c].li_attr['class'] + ' ' + m[dpc[i]].li_attr['class'];
  7891. }
  7892. }
  7893. }
  7894. }
  7895. if(t[c].a_attr !== undefined && typeof t[c].a_attr === 'object') {
  7896. for (k in t[c].a_attr) {
  7897. if (t[c].a_attr.hasOwnProperty(k)) {
  7898. if (k === 'id') {
  7899. continue;
  7900. }
  7901. else if (m[dpc[i]].a_attr[k] === undefined) {
  7902. m[dpc[i]].a_attr[k] = t[c].a_attr[k];
  7903. }
  7904. else if (k === 'href' && m[dpc[i]].a_attr[k] === '#') {
  7905. m[dpc[i]].a_attr['href'] = t[c].a_attr['href'];
  7906. }
  7907. else if (k === 'class') {
  7908. m[dpc[i]].a_attr['class'] = t[c].a_attr['class'] + ' ' + m[dpc[i]].a_attr['class'];
  7909. }
  7910. }
  7911. }
  7912. }
  7913. }
  7914. m[$.jstree.root].type = $.jstree.root;
  7915. }, this));
  7916. parent.bind.call(this);
  7917. };
  7918. this.get_json = function (obj, options, flat) {
  7919. var i, j,
  7920. m = this._model.data,
  7921. opt = options ? $.extend(true, {}, options, {no_id:false}) : {},
  7922. tmp = parent.get_json.call(this, obj, opt, flat);
  7923. if(tmp === false) { return false; }
  7924. if($.isArray(tmp)) {
  7925. for(i = 0, j = tmp.length; i < j; i++) {
  7926. tmp[i].type = tmp[i].id && m[tmp[i].id] && m[tmp[i].id].type ? m[tmp[i].id].type : "default";
  7927. if(options && options.no_id) {
  7928. delete tmp[i].id;
  7929. if(tmp[i].li_attr && tmp[i].li_attr.id) {
  7930. delete tmp[i].li_attr.id;
  7931. }
  7932. if(tmp[i].a_attr && tmp[i].a_attr.id) {
  7933. delete tmp[i].a_attr.id;
  7934. }
  7935. }
  7936. }
  7937. }
  7938. else {
  7939. tmp.type = tmp.id && m[tmp.id] && m[tmp.id].type ? m[tmp.id].type : "default";
  7940. if(options && options.no_id) {
  7941. tmp = this._delete_ids(tmp);
  7942. }
  7943. }
  7944. return tmp;
  7945. };
  7946. this._delete_ids = function (tmp) {
  7947. if($.isArray(tmp)) {
  7948. for(var i = 0, j = tmp.length; i < j; i++) {
  7949. tmp[i] = this._delete_ids(tmp[i]);
  7950. }
  7951. return tmp;
  7952. }
  7953. delete tmp.id;
  7954. if(tmp.li_attr && tmp.li_attr.id) {
  7955. delete tmp.li_attr.id;
  7956. }
  7957. if(tmp.a_attr && tmp.a_attr.id) {
  7958. delete tmp.a_attr.id;
  7959. }
  7960. if(tmp.children && $.isArray(tmp.children)) {
  7961. tmp.children = this._delete_ids(tmp.children);
  7962. }
  7963. return tmp;
  7964. };
  7965. this.check = function (chk, obj, par, pos, more) {
  7966. if(parent.check.call(this, chk, obj, par, pos, more) === false) { return false; }
  7967. obj = obj && obj.id ? obj : this.get_node(obj);
  7968. par = par && par.id ? par : this.get_node(par);
  7969. var m = obj && obj.id ? (more && more.origin ? more.origin : $.jstree.reference(obj.id)) : null, tmp, d, i, j;
  7970. m = m && m._model && m._model.data ? m._model.data : null;
  7971. switch(chk) {
  7972. case "create_node":
  7973. case "move_node":
  7974. case "copy_node":
  7975. if(chk !== 'move_node' || $.inArray(obj.id, par.children) === -1) {
  7976. tmp = this.get_rules(par);
  7977. if(tmp.max_children !== undefined && tmp.max_children !== -1 && tmp.max_children === par.children.length) {
  7978. this._data.core.last_error = { 'error' : 'check', 'plugin' : 'types', 'id' : 'types_01', 'reason' : 'max_children prevents function: ' + chk, 'data' : JSON.stringify({ 'chk' : chk, 'pos' : pos, 'obj' : obj && obj.id ? obj.id : false, 'par' : par && par.id ? par.id : false }) };
  7979. return false;
  7980. }
  7981. if(tmp.valid_children !== undefined && tmp.valid_children !== -1 && $.inArray((obj.type || 'default'), tmp.valid_children) === -1) {
  7982. this._data.core.last_error = { 'error' : 'check', 'plugin' : 'types', 'id' : 'types_02', 'reason' : 'valid_children prevents function: ' + chk, 'data' : JSON.stringify({ 'chk' : chk, 'pos' : pos, 'obj' : obj && obj.id ? obj.id : false, 'par' : par && par.id ? par.id : false }) };
  7983. return false;
  7984. }
  7985. if(m && obj.children_d && obj.parents) {
  7986. d = 0;
  7987. for(i = 0, j = obj.children_d.length; i < j; i++) {
  7988. d = Math.max(d, m[obj.children_d[i]].parents.length);
  7989. }
  7990. d = d - obj.parents.length + 1;
  7991. }
  7992. if(d <= 0 || d === undefined) { d = 1; }
  7993. do {
  7994. if(tmp.max_depth !== undefined && tmp.max_depth !== -1 && tmp.max_depth < d) {
  7995. this._data.core.last_error = { 'error' : 'check', 'plugin' : 'types', 'id' : 'types_03', 'reason' : 'max_depth prevents function: ' + chk, 'data' : JSON.stringify({ 'chk' : chk, 'pos' : pos, 'obj' : obj && obj.id ? obj.id : false, 'par' : par && par.id ? par.id : false }) };
  7996. return false;
  7997. }
  7998. par = this.get_node(par.parent);
  7999. tmp = this.get_rules(par);
  8000. d++;
  8001. } while(par);
  8002. }
  8003. break;
  8004. }
  8005. return true;
  8006. };
  8007. /**
  8008. * used to retrieve the type settings object for a node
  8009. * @name get_rules(obj)
  8010. * @param {mixed} obj the node to find the rules for
  8011. * @return {Object}
  8012. * @plugin types
  8013. */
  8014. this.get_rules = function (obj) {
  8015. obj = this.get_node(obj);
  8016. if(!obj) { return false; }
  8017. var tmp = this.get_type(obj, true);
  8018. if(tmp.max_depth === undefined) { tmp.max_depth = -1; }
  8019. if(tmp.max_children === undefined) { tmp.max_children = -1; }
  8020. if(tmp.valid_children === undefined) { tmp.valid_children = -1; }
  8021. return tmp;
  8022. };
  8023. /**
  8024. * used to retrieve the type string or settings object for a node
  8025. * @name get_type(obj [, rules])
  8026. * @param {mixed} obj the node to find the rules for
  8027. * @param {Boolean} rules if set to `true` instead of a string the settings object will be returned
  8028. * @return {String|Object}
  8029. * @plugin types
  8030. */
  8031. this.get_type = function (obj, rules) {
  8032. obj = this.get_node(obj);
  8033. return (!obj) ? false : ( rules ? $.extend({ 'type' : obj.type }, this.settings.types[obj.type]) : obj.type);
  8034. };
  8035. /**
  8036. * used to change a node's type
  8037. * @name set_type(obj, type)
  8038. * @param {mixed} obj the node to change
  8039. * @param {String} type the new type
  8040. * @plugin types
  8041. */
  8042. this.set_type = function (obj, type) {
  8043. var m = this._model.data, t, t1, t2, old_type, old_icon, k, d, a;
  8044. if($.isArray(obj)) {
  8045. obj = obj.slice();
  8046. for(t1 = 0, t2 = obj.length; t1 < t2; t1++) {
  8047. this.set_type(obj[t1], type);
  8048. }
  8049. return true;
  8050. }
  8051. t = this.settings.types;
  8052. obj = this.get_node(obj);
  8053. if(!t[type] || !obj) { return false; }
  8054. d = this.get_node(obj, true);
  8055. if (d && d.length) {
  8056. a = d.children('.jstree-anchor');
  8057. }
  8058. old_type = obj.type;
  8059. old_icon = this.get_icon(obj);
  8060. obj.type = type;
  8061. if(old_icon === true || !t[old_type] || (t[old_type].icon !== undefined && old_icon === t[old_type].icon)) {
  8062. this.set_icon(obj, t[type].icon !== undefined ? t[type].icon : true);
  8063. }
  8064. // remove old type props
  8065. if(t[old_type] && t[old_type].li_attr !== undefined && typeof t[old_type].li_attr === 'object') {
  8066. for (k in t[old_type].li_attr) {
  8067. if (t[old_type].li_attr.hasOwnProperty(k)) {
  8068. if (k === 'id') {
  8069. continue;
  8070. }
  8071. else if (k === 'class') {
  8072. m[obj.id].li_attr['class'] = (m[obj.id].li_attr['class'] || '').replace(t[old_type].li_attr[k], '');
  8073. if (d) { d.removeClass(t[old_type].li_attr[k]); }
  8074. }
  8075. else if (m[obj.id].li_attr[k] === t[old_type].li_attr[k]) {
  8076. m[obj.id].li_attr[k] = null;
  8077. if (d) { d.removeAttr(k); }
  8078. }
  8079. }
  8080. }
  8081. }
  8082. if(t[old_type] && t[old_type].a_attr !== undefined && typeof t[old_type].a_attr === 'object') {
  8083. for (k in t[old_type].a_attr) {
  8084. if (t[old_type].a_attr.hasOwnProperty(k)) {
  8085. if (k === 'id') {
  8086. continue;
  8087. }
  8088. else if (k === 'class') {
  8089. m[obj.id].a_attr['class'] = (m[obj.id].a_attr['class'] || '').replace(t[old_type].a_attr[k], '');
  8090. if (a) { a.removeClass(t[old_type].a_attr[k]); }
  8091. }
  8092. else if (m[obj.id].a_attr[k] === t[old_type].a_attr[k]) {
  8093. if (k === 'href') {
  8094. m[obj.id].a_attr[k] = '#';
  8095. if (a) { a.attr('href', '#'); }
  8096. }
  8097. else {
  8098. delete m[obj.id].a_attr[k];
  8099. if (a) { a.removeAttr(k); }
  8100. }
  8101. }
  8102. }
  8103. }
  8104. }
  8105. // add new props
  8106. if(t[type].li_attr !== undefined && typeof t[type].li_attr === 'object') {
  8107. for (k in t[type].li_attr) {
  8108. if (t[type].li_attr.hasOwnProperty(k)) {
  8109. if (k === 'id') {
  8110. continue;
  8111. }
  8112. else if (m[obj.id].li_attr[k] === undefined) {
  8113. m[obj.id].li_attr[k] = t[type].li_attr[k];
  8114. if (d) {
  8115. if (k === 'class') {
  8116. d.addClass(t[type].li_attr[k]);
  8117. }
  8118. else {
  8119. d.attr(k, t[type].li_attr[k]);
  8120. }
  8121. }
  8122. }
  8123. else if (k === 'class') {
  8124. m[obj.id].li_attr['class'] = t[type].li_attr[k] + ' ' + m[obj.id].li_attr['class'];
  8125. if (d) { d.addClass(t[type].li_attr[k]); }
  8126. }
  8127. }
  8128. }
  8129. }
  8130. if(t[type].a_attr !== undefined && typeof t[type].a_attr === 'object') {
  8131. for (k in t[type].a_attr) {
  8132. if (t[type].a_attr.hasOwnProperty(k)) {
  8133. if (k === 'id') {
  8134. continue;
  8135. }
  8136. else if (m[obj.id].a_attr[k] === undefined) {
  8137. m[obj.id].a_attr[k] = t[type].a_attr[k];
  8138. if (a) {
  8139. if (k === 'class') {
  8140. a.addClass(t[type].a_attr[k]);
  8141. }
  8142. else {
  8143. a.attr(k, t[type].a_attr[k]);
  8144. }
  8145. }
  8146. }
  8147. else if (k === 'href' && m[obj.id].a_attr[k] === '#') {
  8148. m[obj.id].a_attr['href'] = t[type].a_attr['href'];
  8149. if (a) { a.attr('href', t[type].a_attr['href']); }
  8150. }
  8151. else if (k === 'class') {
  8152. m[obj.id].a_attr['class'] = t[type].a_attr['class'] + ' ' + m[obj.id].a_attr['class'];
  8153. if (a) { a.addClass(t[type].a_attr[k]); }
  8154. }
  8155. }
  8156. }
  8157. }
  8158. return true;
  8159. };
  8160. };
  8161. // include the types plugin by default
  8162. // $.jstree.defaults.plugins.push("types");
  8163. /**
  8164. * ### Unique plugin
  8165. *
  8166. * Enforces that no nodes with the same name can coexist as siblings.
  8167. */
  8168. /**
  8169. * stores all defaults for the unique plugin
  8170. * @name $.jstree.defaults.unique
  8171. * @plugin unique
  8172. */
  8173. $.jstree.defaults.unique = {
  8174. /**
  8175. * Indicates if the comparison should be case sensitive. Default is `false`.
  8176. * @name $.jstree.defaults.unique.case_sensitive
  8177. * @plugin unique
  8178. */
  8179. case_sensitive : false,
  8180. /**
  8181. * Indicates if white space should be trimmed before the comparison. Default is `false`.
  8182. * @name $.jstree.defaults.unique.trim_whitespace
  8183. * @plugin unique
  8184. */
  8185. trim_whitespace : false,
  8186. /**
  8187. * A callback executed in the instance's scope when a new node is created and the name is already taken, the two arguments are the conflicting name and the counter. The default will produce results like `New node (2)`.
  8188. * @name $.jstree.defaults.unique.duplicate
  8189. * @plugin unique
  8190. */
  8191. duplicate : function (name, counter) {
  8192. return name + ' (' + counter + ')';
  8193. }
  8194. };
  8195. $.jstree.plugins.unique = function (options, parent) {
  8196. this.check = function (chk, obj, par, pos, more) {
  8197. if(parent.check.call(this, chk, obj, par, pos, more) === false) { return false; }
  8198. obj = obj && obj.id ? obj : this.get_node(obj);
  8199. par = par && par.id ? par : this.get_node(par);
  8200. if(!par || !par.children) { return true; }
  8201. var n = chk === "rename_node" ? pos : obj.text,
  8202. c = [],
  8203. s = this.settings.unique.case_sensitive,
  8204. w = this.settings.unique.trim_whitespace,
  8205. m = this._model.data, i, j, t;
  8206. for(i = 0, j = par.children.length; i < j; i++) {
  8207. t = m[par.children[i]].text;
  8208. if (!s) {
  8209. t = t.toLowerCase();
  8210. }
  8211. if (w) {
  8212. t = t.replace(/^[\s\uFEFF\xA0]+|[\s\uFEFF\xA0]+$/g, '');
  8213. }
  8214. c.push(t);
  8215. }
  8216. if(!s) { n = n.toLowerCase(); }
  8217. if (w) { n = n.replace(/^[\s\uFEFF\xA0]+|[\s\uFEFF\xA0]+$/g, ''); }
  8218. switch(chk) {
  8219. case "delete_node":
  8220. return true;
  8221. case "rename_node":
  8222. t = obj.text || '';
  8223. if (!s) {
  8224. t = t.toLowerCase();
  8225. }
  8226. if (w) {
  8227. t = t.replace(/^[\s\uFEFF\xA0]+|[\s\uFEFF\xA0]+$/g, '');
  8228. }
  8229. i = ($.inArray(n, c) === -1 || (obj.text && t === n));
  8230. if(!i) {
  8231. this._data.core.last_error = { 'error' : 'check', 'plugin' : 'unique', 'id' : 'unique_01', 'reason' : 'Child with name ' + n + ' already exists. Preventing: ' + chk, 'data' : JSON.stringify({ 'chk' : chk, 'pos' : pos, 'obj' : obj && obj.id ? obj.id : false, 'par' : par && par.id ? par.id : false }) };
  8232. }
  8233. return i;
  8234. case "create_node":
  8235. i = ($.inArray(n, c) === -1);
  8236. if(!i) {
  8237. this._data.core.last_error = { 'error' : 'check', 'plugin' : 'unique', 'id' : 'unique_04', 'reason' : 'Child with name ' + n + ' already exists. Preventing: ' + chk, 'data' : JSON.stringify({ 'chk' : chk, 'pos' : pos, 'obj' : obj && obj.id ? obj.id : false, 'par' : par && par.id ? par.id : false }) };
  8238. }
  8239. return i;
  8240. case "copy_node":
  8241. i = ($.inArray(n, c) === -1);
  8242. if(!i) {
  8243. this._data.core.last_error = { 'error' : 'check', 'plugin' : 'unique', 'id' : 'unique_02', 'reason' : 'Child with name ' + n + ' already exists. Preventing: ' + chk, 'data' : JSON.stringify({ 'chk' : chk, 'pos' : pos, 'obj' : obj && obj.id ? obj.id : false, 'par' : par && par.id ? par.id : false }) };
  8244. }
  8245. return i;
  8246. case "move_node":
  8247. i = ( (obj.parent === par.id && (!more || !more.is_multi)) || $.inArray(n, c) === -1);
  8248. if(!i) {
  8249. this._data.core.last_error = { 'error' : 'check', 'plugin' : 'unique', 'id' : 'unique_03', 'reason' : 'Child with name ' + n + ' already exists. Preventing: ' + chk, 'data' : JSON.stringify({ 'chk' : chk, 'pos' : pos, 'obj' : obj && obj.id ? obj.id : false, 'par' : par && par.id ? par.id : false }) };
  8250. }
  8251. return i;
  8252. }
  8253. return true;
  8254. };
  8255. this.create_node = function (par, node, pos, callback, is_loaded) {
  8256. if(!node || node.text === undefined) {
  8257. if(par === null) {
  8258. par = $.jstree.root;
  8259. }
  8260. par = this.get_node(par);
  8261. if(!par) {
  8262. return parent.create_node.call(this, par, node, pos, callback, is_loaded);
  8263. }
  8264. pos = pos === undefined ? "last" : pos;
  8265. if(!pos.toString().match(/^(before|after)$/) && !is_loaded && !this.is_loaded(par)) {
  8266. return parent.create_node.call(this, par, node, pos, callback, is_loaded);
  8267. }
  8268. if(!node) { node = {}; }
  8269. var tmp, n, dpc, i, j, m = this._model.data, s = this.settings.unique.case_sensitive, w = this.settings.unique.trim_whitespace, cb = this.settings.unique.duplicate, t;
  8270. n = tmp = this.get_string('New node');
  8271. dpc = [];
  8272. for(i = 0, j = par.children.length; i < j; i++) {
  8273. t = m[par.children[i]].text;
  8274. if (!s) {
  8275. t = t.toLowerCase();
  8276. }
  8277. if (w) {
  8278. t = t.replace(/^[\s\uFEFF\xA0]+|[\s\uFEFF\xA0]+$/g, '');
  8279. }
  8280. dpc.push(t);
  8281. }
  8282. i = 1;
  8283. t = n;
  8284. if (!s) {
  8285. t = t.toLowerCase();
  8286. }
  8287. if (w) {
  8288. t = t.replace(/^[\s\uFEFF\xA0]+|[\s\uFEFF\xA0]+$/g, '');
  8289. }
  8290. while($.inArray(t, dpc) !== -1) {
  8291. n = cb.call(this, tmp, (++i)).toString();
  8292. t = n;
  8293. if (!s) {
  8294. t = t.toLowerCase();
  8295. }
  8296. if (w) {
  8297. t = t.replace(/^[\s\uFEFF\xA0]+|[\s\uFEFF\xA0]+$/g, '');
  8298. }
  8299. }
  8300. node.text = n;
  8301. }
  8302. return parent.create_node.call(this, par, node, pos, callback, is_loaded);
  8303. };
  8304. };
  8305. // include the unique plugin by default
  8306. // $.jstree.defaults.plugins.push("unique");
  8307. /**
  8308. * ### Wholerow plugin
  8309. *
  8310. * Makes each node appear block level. Making selection easier. May cause slow down for large trees in old browsers.
  8311. */
  8312. var div = document.createElement('DIV');
  8313. div.setAttribute('unselectable','on');
  8314. div.setAttribute('role','presentation');
  8315. div.className = 'jstree-wholerow';
  8316. div.innerHTML = '&#160;';
  8317. $.jstree.plugins.wholerow = function (options, parent) {
  8318. this.bind = function () {
  8319. parent.bind.call(this);
  8320. this.element
  8321. .on('ready.jstree set_state.jstree', $.proxy(function () {
  8322. this.hide_dots();
  8323. }, this))
  8324. .on("init.jstree loading.jstree ready.jstree", $.proxy(function () {
  8325. //div.style.height = this._data.core.li_height + 'px';
  8326. this.get_container_ul().addClass('jstree-wholerow-ul');
  8327. }, this))
  8328. .on("deselect_all.jstree", $.proxy(function (e, data) {
  8329. this.element.find('.jstree-wholerow-clicked').removeClass('jstree-wholerow-clicked');
  8330. }, this))
  8331. .on("changed.jstree", $.proxy(function (e, data) {
  8332. this.element.find('.jstree-wholerow-clicked').removeClass('jstree-wholerow-clicked');
  8333. var tmp = false, i, j;
  8334. for(i = 0, j = data.selected.length; i < j; i++) {
  8335. tmp = this.get_node(data.selected[i], true);
  8336. if(tmp && tmp.length) {
  8337. tmp.children('.jstree-wholerow').addClass('jstree-wholerow-clicked');
  8338. }
  8339. }
  8340. }, this))
  8341. .on("open_node.jstree", $.proxy(function (e, data) {
  8342. this.get_node(data.node, true).find('.jstree-clicked').parent().children('.jstree-wholerow').addClass('jstree-wholerow-clicked');
  8343. }, this))
  8344. .on("hover_node.jstree dehover_node.jstree", $.proxy(function (e, data) {
  8345. if(e.type === "hover_node" && this.is_disabled(data.node)) { return; }
  8346. this.get_node(data.node, true).children('.jstree-wholerow')[e.type === "hover_node"?"addClass":"removeClass"]('jstree-wholerow-hovered');
  8347. }, this))
  8348. .on("contextmenu.jstree", ".jstree-wholerow", $.proxy(function (e) {
  8349. if (this._data.contextmenu) {
  8350. e.preventDefault();
  8351. var tmp = $.Event('contextmenu', { metaKey : e.metaKey, ctrlKey : e.ctrlKey, altKey : e.altKey, shiftKey : e.shiftKey, pageX : e.pageX, pageY : e.pageY });
  8352. $(e.currentTarget).closest(".jstree-node").children(".jstree-anchor").first().trigger(tmp);
  8353. }
  8354. }, this))
  8355. /*!
  8356. .on("mousedown.jstree touchstart.jstree", ".jstree-wholerow", function (e) {
  8357. if(e.target === e.currentTarget) {
  8358. var a = $(e.currentTarget).closest(".jstree-node").children(".jstree-anchor");
  8359. e.target = a[0];
  8360. a.trigger(e);
  8361. }
  8362. })
  8363. */
  8364. .on("click.jstree", ".jstree-wholerow", function (e) {
  8365. e.stopImmediatePropagation();
  8366. var tmp = $.Event('click', { metaKey : e.metaKey, ctrlKey : e.ctrlKey, altKey : e.altKey, shiftKey : e.shiftKey });
  8367. $(e.currentTarget).closest(".jstree-node").children(".jstree-anchor").first().trigger(tmp).focus();
  8368. })
  8369. .on("dblclick.jstree", ".jstree-wholerow", function (e) {
  8370. e.stopImmediatePropagation();
  8371. var tmp = $.Event('dblclick', { metaKey : e.metaKey, ctrlKey : e.ctrlKey, altKey : e.altKey, shiftKey : e.shiftKey });
  8372. $(e.currentTarget).closest(".jstree-node").children(".jstree-anchor").first().trigger(tmp).focus();
  8373. })
  8374. .on("click.jstree", ".jstree-leaf > .jstree-ocl", $.proxy(function (e) {
  8375. e.stopImmediatePropagation();
  8376. var tmp = $.Event('click', { metaKey : e.metaKey, ctrlKey : e.ctrlKey, altKey : e.altKey, shiftKey : e.shiftKey });
  8377. $(e.currentTarget).closest(".jstree-node").children(".jstree-anchor").first().trigger(tmp).focus();
  8378. }, this))
  8379. .on("mouseover.jstree", ".jstree-wholerow, .jstree-icon", $.proxy(function (e) {
  8380. e.stopImmediatePropagation();
  8381. if(!this.is_disabled(e.currentTarget)) {
  8382. this.hover_node(e.currentTarget);
  8383. }
  8384. return false;
  8385. }, this))
  8386. .on("mouseleave.jstree", ".jstree-node", $.proxy(function (e) {
  8387. this.dehover_node(e.currentTarget);
  8388. }, this));
  8389. };
  8390. this.teardown = function () {
  8391. if(this.settings.wholerow) {
  8392. this.element.find(".jstree-wholerow").remove();
  8393. }
  8394. parent.teardown.call(this);
  8395. };
  8396. this.redraw_node = function(obj, deep, callback, force_render) {
  8397. obj = parent.redraw_node.apply(this, arguments);
  8398. if(obj) {
  8399. var tmp = div.cloneNode(true);
  8400. //tmp.style.height = this._data.core.li_height + 'px';
  8401. if($.inArray(obj.id, this._data.core.selected) !== -1) { tmp.className += ' jstree-wholerow-clicked'; }
  8402. if(this._data.core.focused && this._data.core.focused === obj.id) { tmp.className += ' jstree-wholerow-hovered'; }
  8403. obj.insertBefore(tmp, obj.childNodes[0]);
  8404. }
  8405. return obj;
  8406. };
  8407. };
  8408. // include the wholerow plugin by default
  8409. // $.jstree.defaults.plugins.push("wholerow");
  8410. if(window.customElements && Object && Object.create) {
  8411. var proto = Object.create(HTMLElement.prototype);
  8412. proto.createdCallback = function () {
  8413. var c = { core : {}, plugins : [] }, i;
  8414. for(i in $.jstree.plugins) {
  8415. if($.jstree.plugins.hasOwnProperty(i) && this.attributes[i]) {
  8416. c.plugins.push(i);
  8417. if(this.getAttribute(i) && JSON.parse(this.getAttribute(i))) {
  8418. c[i] = JSON.parse(this.getAttribute(i));
  8419. }
  8420. }
  8421. }
  8422. for(i in $.jstree.defaults.core) {
  8423. if($.jstree.defaults.core.hasOwnProperty(i) && this.attributes[i]) {
  8424. c.core[i] = JSON.parse(this.getAttribute(i)) || this.getAttribute(i);
  8425. }
  8426. }
  8427. $(this).jstree(c);
  8428. };
  8429. // proto.attributeChangedCallback = function (name, previous, value) { };
  8430. try {
  8431. window.customElements.define("vakata-jstree", function() {}, { prototype: proto });
  8432. } catch (ignore) { }
  8433. }
  8434. }));