Subversion Repositories oidplus

Rev

Go to most recent revision | View as "text/javascript" | Blame | Last modification | View Log | RSS feed

  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.  
  30.         // prevent another load? maybe there is a better way?
  31.         if($.jstree) {
  32.                 return;
  33.         }
  34.  
  35.         /**
  36.          * ### jsTree core functionality
  37.          */
  38.  
  39.         // internal variables
  40.         var instance_counter = 0,
  41.                 ccp_node = false,
  42.                 ccp_mode = false,
  43.                 ccp_inst = false,
  44.                 themes_loaded = [],
  45.                 src = $('script:last').attr('src'),
  46.                 document = window.document; // local variable is always faster to access then a global
  47.  
  48.         /**
  49.          * holds all jstree related functions and variables, including the actual class and methods to create, access and manipulate instances.
  50.          * @name $.jstree
  51.          */
  52.         $.jstree = {
  53.                 /**
  54.                  * specifies the jstree version in use
  55.                  * @name $.jstree.version
  56.                  */
  57.                 version : '3.3.7',
  58.                 /**
  59.                  * holds all the default options used when creating new instances
  60.                  * @name $.jstree.defaults
  61.                  */
  62.                 defaults : {
  63.                         /**
  64.                          * 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 `[]`
  65.                          * @name $.jstree.defaults.plugins
  66.                          */
  67.                         plugins : []
  68.                 },
  69.                 /**
  70.                  * stores all loaded jstree plugins (used internally)
  71.                  * @name $.jstree.plugins
  72.                  */
  73.                 plugins : {},
  74.                 path : src && src.indexOf('/') !== -1 ? src.replace(/\/[^\/]+$/,'') : '',
  75.                 idregex : /[\\:&!^|()\[\]<>@*'+~#";.,=\- \/${}%?`]/g,
  76.                 root : '#'
  77.         };
  78.        
  79.         /**
  80.          * creates a jstree instance
  81.          * @name $.jstree.create(el [, options])
  82.          * @param {DOMElement|jQuery|String} el the element to create the instance on, can be jQuery extended or a selector
  83.          * @param {Object} options options for this instance (extends `$.jstree.defaults`)
  84.          * @return {jsTree} the new instance
  85.          */
  86.         $.jstree.create = function (el, options) {
  87.                 var tmp = new $.jstree.core(++instance_counter),
  88.                         opt = options;
  89.                 options = $.extend(true, {}, $.jstree.defaults, options);
  90.                 if(opt && opt.plugins) {
  91.                         options.plugins = opt.plugins;
  92.                 }
  93.                 $.each(options.plugins, function (i, k) {
  94.                         if(i !== 'core') {
  95.                                 tmp = tmp.plugin(k, options[k]);
  96.                         }
  97.                 });
  98.                 $(el).data('jstree', tmp);
  99.                 tmp.init(el, options);
  100.                 return tmp;
  101.         };
  102.         /**
  103.          * remove all traces of jstree from the DOM and destroy all instances
  104.          * @name $.jstree.destroy()
  105.          */
  106.         $.jstree.destroy = function () {
  107.                 $('.jstree:jstree').jstree('destroy');
  108.                 $(document).off('.jstree');
  109.         };
  110.         /**
  111.          * the jstree class constructor, used only internally
  112.          * @private
  113.          * @name $.jstree.core(id)
  114.          * @param {Number} id this instance's index
  115.          */
  116.         $.jstree.core = function (id) {
  117.                 this._id = id;
  118.                 this._cnt = 0;
  119.                 this._wrk = null;
  120.                 this._data = {
  121.                         core : {
  122.                                 themes : {
  123.                                         name : false,
  124.                                         dots : false,
  125.                                         icons : false,
  126.                                         ellipsis : false
  127.                                 },
  128.                                 selected : [],
  129.                                 last_error : {},
  130.                                 working : false,
  131.                                 worker_queue : [],
  132.                                 focused : null
  133.                         }
  134.                 };
  135.         };
  136.         /**
  137.          * get a reference to an existing instance
  138.          *
  139.          * __Examples__
  140.          *
  141.          *      // provided a container with an ID of "tree", and a nested node with an ID of "branch"
  142.          *      // all of there will return the same instance
  143.          *      $.jstree.reference('tree');
  144.          *      $.jstree.reference('#tree');
  145.          *      $.jstree.reference($('#tree'));
  146.          *      $.jstree.reference(document.getElementByID('tree'));
  147.          *      $.jstree.reference('branch');
  148.          *      $.jstree.reference('#branch');
  149.          *      $.jstree.reference($('#branch'));
  150.          *      $.jstree.reference(document.getElementByID('branch'));
  151.          *
  152.          * @name $.jstree.reference(needle)
  153.          * @param {DOMElement|jQuery|String} needle
  154.          * @return {jsTree|null} the instance or `null` if not found
  155.          */
  156.         $.jstree.reference = function (needle) {
  157.                 var tmp = null,
  158.                         obj = null;
  159.                 if(needle && needle.id && (!needle.tagName || !needle.nodeType)) { needle = needle.id; }
  160.  
  161.                 if(!obj || !obj.length) {
  162.                         try { obj = $(needle); } catch (ignore) { }
  163.                 }
  164.                 if(!obj || !obj.length) {
  165.                         try { obj = $('#' + needle.replace($.jstree.idregex,'\\$&')); } catch (ignore) { }
  166.                 }
  167.                 if(obj && obj.length && (obj = obj.closest('.jstree')).length && (obj = obj.data('jstree'))) {
  168.                         tmp = obj;
  169.                 }
  170.                 else {
  171.                         $('.jstree').each(function () {
  172.                                 var inst = $(this).data('jstree');
  173.                                 if(inst && inst._model.data[needle]) {
  174.                                         tmp = inst;
  175.                                         return false;
  176.                                 }
  177.                         });
  178.                 }
  179.                 return tmp;
  180.         };
  181.         /**
  182.          * Create an instance, get an instance or invoke a command on a instance.
  183.          *
  184.          * 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).
  185.          *
  186.          * 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).
  187.          *
  188.          * If there is an existing instance and `arg` is not a string the instance itself is returned (similar to `$.jstree.reference`).
  189.          *
  190.          * In any other case - nothing is returned and chaining is not broken.
  191.          *
  192.          * __Examples__
  193.          *
  194.          *      $('#tree1').jstree(); // creates an instance
  195.          *      $('#tree2').jstree({ plugins : [] }); // create an instance with some options
  196.          *      $('#tree1').jstree('open_node', '#branch_1'); // call a method on an existing instance, passing additional arguments
  197.          *      $('#tree2').jstree(); // get an existing instance (or create an instance)
  198.          *      $('#tree2').jstree(true); // get an existing instance (will not create new instance)
  199.          *      $('#branch_1').jstree().select_node('#branch_1'); // get an instance (using a nested element and call a method)
  200.          *
  201.          * @name $().jstree([arg])
  202.          * @param {String|Object} arg
  203.          * @return {Mixed}
  204.          */
  205.         $.fn.jstree = function (arg) {
  206.                 // check for string argument
  207.                 var is_method   = (typeof arg === 'string'),
  208.                         args            = Array.prototype.slice.call(arguments, 1),
  209.                         result          = null;
  210.                 if(arg === true && !this.length) { return false; }
  211.                 this.each(function () {
  212.                         // get the instance (if there is one) and method (if it exists)
  213.                         var instance = $.jstree.reference(this),
  214.                                 method = is_method && instance ? instance[arg] : null;
  215.                         // if calling a method, and method is available - execute on the instance
  216.                         result = is_method && method ?
  217.                                 method.apply(instance, args) :
  218.                                 null;
  219.                         // if there is no instance and no method is being called - create one
  220.                         if(!instance && !is_method && (arg === undefined || $.isPlainObject(arg))) {
  221.                                 $.jstree.create(this, arg);
  222.                         }
  223.                         // if there is an instance and no method is called - return the instance
  224.                         if( (instance && !is_method) || arg === true ) {
  225.                                 result = instance || false;
  226.                         }
  227.                         // if there was a method call which returned a result - break and return the value
  228.                         if(result !== null && result !== undefined) {
  229.                                 return false;
  230.                         }
  231.                 });
  232.                 // if there was a method call with a valid return value - return that, otherwise continue the chain
  233.                 return result !== null && result !== undefined ?
  234.                         result : this;
  235.         };
  236.         /**
  237.          * used to find elements containing an instance
  238.          *
  239.          * __Examples__
  240.          *
  241.          *      $('div:jstree').each(function () {
  242.          *              $(this).jstree('destroy');
  243.          *      });
  244.          *
  245.          * @name $(':jstree')
  246.          * @return {jQuery}
  247.          */
  248.         $.expr.pseudos.jstree = $.expr.createPseudo(function(search) {
  249.                 return function(a) {
  250.                         return $(a).hasClass('jstree') &&
  251.                                 $(a).data('jstree') !== undefined;
  252.                 };
  253.         });
  254.  
  255.         /**
  256.          * stores all defaults for the core
  257.          * @name $.jstree.defaults.core
  258.          */
  259.         $.jstree.defaults.core = {
  260.                 /**
  261.                  * data configuration
  262.                  *
  263.                  * 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).
  264.                  *
  265.                  * You can also pass in a HTML string or a JSON array here.
  266.                  *
  267.                  * 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.
  268.                  * 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.
  269.                  *
  270.                  * 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.
  271.                  *
  272.                  * __Examples__
  273.                  *
  274.                  *      // AJAX
  275.                  *      $('#tree').jstree({
  276.                  *              'core' : {
  277.                  *                      'data' : {
  278.                  *                              'url' : '/get/children/',
  279.                  *                              'data' : function (node) {
  280.                  *                                      return { 'id' : node.id };
  281.                  *                              }
  282.                  *                      }
  283.                  *              });
  284.                  *
  285.                  *      // direct data
  286.                  *      $('#tree').jstree({
  287.                  *              'core' : {
  288.                  *                      'data' : [
  289.                  *                              'Simple root node',
  290.                  *                              {
  291.                  *                                      'id' : 'node_2',
  292.                  *                                      'text' : 'Root node with options',
  293.                  *                                      'state' : { 'opened' : true, 'selected' : true },
  294.                  *                                      'children' : [ { 'text' : 'Child 1' }, 'Child 2']
  295.                  *                              }
  296.                  *                      ]
  297.                  *              }
  298.                  *      });
  299.                  *
  300.                  *      // function
  301.                  *      $('#tree').jstree({
  302.                  *              'core' : {
  303.                  *                      'data' : function (obj, callback) {
  304.                  *                              callback.call(this, ['Root 1', 'Root 2']);
  305.                  *                      }
  306.                  *              });
  307.                  *
  308.                  * @name $.jstree.defaults.core.data
  309.                  */
  310.                 data                    : false,
  311.                 /**
  312.                  * configure the various strings used throughout the tree
  313.                  *
  314.                  * You can use an object where the key is the string you need to replace and the value is your replacement.
  315.                  * Another option is to specify a function which will be called with an argument of the needed string and should return the replacement.
  316.                  * If left as `false` no replacement is made.
  317.                  *
  318.                  * __Examples__
  319.                  *
  320.                  *      $('#tree').jstree({
  321.                  *              'core' : {
  322.                  *                      'strings' : {
  323.                  *                              'Loading ...' : 'Please wait ...'
  324.                  *                      }
  325.                  *              }
  326.                  *      });
  327.                  *
  328.                  * @name $.jstree.defaults.core.strings
  329.                  */
  330.                 strings                 : false,
  331.                 /**
  332.                  * determines what happens when a user tries to modify the structure of the tree
  333.                  * If left as `false` all operations like create, rename, delete, move or copy are prevented.
  334.                  * You can set this to `true` to allow all interactions or use a function to have better control.
  335.                  *
  336.                  * __Examples__
  337.                  *
  338.                  *      $('#tree').jstree({
  339.                  *              'core' : {
  340.                  *                      'check_callback' : function (operation, node, node_parent, node_position, more) {
  341.                  *                              // operation can be 'create_node', 'rename_node', 'delete_node', 'move_node', 'copy_node' or 'edit'
  342.                  *                              // in case of 'rename_node' node_position is filled with the new node name
  343.                  *                              return operation === 'rename_node' ? true : false;
  344.                  *                      }
  345.                  *              }
  346.                  *      });
  347.                  *
  348.                  * @name $.jstree.defaults.core.check_callback
  349.                  */
  350.                 check_callback  : false,
  351.                 /**
  352.                  * a callback called with a single object parameter in the instance's scope when something goes wrong (operation prevented, ajax failed, etc)
  353.                  * @name $.jstree.defaults.core.error
  354.                  */
  355.                 error                   : $.noop,
  356.                 /**
  357.                  * the open / close animation duration in milliseconds - set this to `false` to disable the animation (default is `200`)
  358.                  * @name $.jstree.defaults.core.animation
  359.                  */
  360.                 animation               : 200,
  361.                 /**
  362.                  * a boolean indicating if multiple nodes can be selected
  363.                  * @name $.jstree.defaults.core.multiple
  364.                  */
  365.                 multiple                : true,
  366.                 /**
  367.                  * theme configuration object
  368.                  * @name $.jstree.defaults.core.themes
  369.                  */
  370.                 themes                  : {
  371.                         /**
  372.                          * the name of the theme to use (if left as `false` the default theme is used)
  373.                          * @name $.jstree.defaults.core.themes.name
  374.                          */
  375.                         name                    : false,
  376.                         /**
  377.                          * 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.
  378.                          * @name $.jstree.defaults.core.themes.url
  379.                          */
  380.                         url                             : false,
  381.                         /**
  382.                          * the location of all jstree themes - only used if `url` is set to `true`
  383.                          * @name $.jstree.defaults.core.themes.dir
  384.                          */
  385.                         dir                             : false,
  386.                         /**
  387.                          * a boolean indicating if connecting dots are shown
  388.                          * @name $.jstree.defaults.core.themes.dots
  389.                          */
  390.                         dots                    : true,
  391.                         /**
  392.                          * a boolean indicating if node icons are shown
  393.                          * @name $.jstree.defaults.core.themes.icons
  394.                          */
  395.                         icons                   : true,
  396.                         /**
  397.                          * a boolean indicating if node ellipsis should be shown - this only works with a fixed with on the container
  398.                          * @name $.jstree.defaults.core.themes.ellipsis
  399.                          */
  400.                         ellipsis                : false,
  401.                         /**
  402.                          * a boolean indicating if the tree background is striped
  403.                          * @name $.jstree.defaults.core.themes.stripes
  404.                          */
  405.                         stripes                 : false,
  406.                         /**
  407.                          * a string (or boolean `false`) specifying the theme variant to use (if the theme supports variants)
  408.                          * @name $.jstree.defaults.core.themes.variant
  409.                          */
  410.                         variant                 : false,
  411.                         /**
  412.                          * a boolean specifying if a reponsive version of the theme should kick in on smaller screens (if the theme supports it). Defaults to `false`.
  413.                          * @name $.jstree.defaults.core.themes.responsive
  414.                          */
  415.                         responsive              : false
  416.                 },
  417.                 /**
  418.                  * 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)
  419.                  * @name $.jstree.defaults.core.expand_selected_onload
  420.                  */
  421.                 expand_selected_onload : true,
  422.                 /**
  423.                  * 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`
  424.                  * @name $.jstree.defaults.core.worker
  425.                  */
  426.                 worker : true,
  427.                 /**
  428.                  * Force node text to plain text (and escape HTML). Defaults to `false`
  429.                  * @name $.jstree.defaults.core.force_text
  430.                  */
  431.                 force_text : false,
  432.                 /**
  433.                  * Should the node be toggled if the text is double clicked. Defaults to `true`
  434.                  * @name $.jstree.defaults.core.dblclick_toggle
  435.                  */
  436.                 dblclick_toggle : true,
  437.                 /**
  438.                  * Should the loaded nodes be part of the state. Defaults to `false`
  439.                  * @name $.jstree.defaults.core.loaded_state
  440.                  */
  441.                 loaded_state : false,
  442.                 /**
  443.                  * 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`
  444.                  * @name $.jstree.defaults.core.restore_focus
  445.                  */
  446.                 restore_focus : true,
  447.                 /**
  448.                  * 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)
  449.                  * @name $.jstree.defaults.core.keyboard
  450.                  */
  451.                 keyboard : {
  452.                         'ctrl-space': function (e) {
  453.                                 // aria defines space only with Ctrl
  454.                                 e.type = "click";
  455.                                 $(e.currentTarget).trigger(e);
  456.                         },
  457.                         'enter': function (e) {
  458.                                 // enter
  459.                                 e.type = "click";
  460.                                 $(e.currentTarget).trigger(e);
  461.                         },
  462.                         'left': function (e) {
  463.                                 // left
  464.                                 e.preventDefault();
  465.                                 if(this.is_open(e.currentTarget)) {
  466.                                         this.close_node(e.currentTarget);
  467.                                 }
  468.                                 else {
  469.                                         var o = this.get_parent(e.currentTarget);
  470.                                         if(o && o.id !== $.jstree.root) { this.get_node(o, true).children('.jstree-anchor').focus(); }
  471.                                 }
  472.                         },
  473.                         'up': function (e) {
  474.                                 // up
  475.                                 e.preventDefault();
  476.                                 var o = this.get_prev_dom(e.currentTarget);
  477.                                 if(o && o.length) { o.children('.jstree-anchor').focus(); }
  478.                         },
  479.                         'right': function (e) {
  480.                                 // right
  481.                                 e.preventDefault();
  482.                                 if(this.is_closed(e.currentTarget)) {
  483.                                         this.open_node(e.currentTarget, function (o) { this.get_node(o, true).children('.jstree-anchor').focus(); });
  484.                                 }
  485.                                 else if (this.is_open(e.currentTarget)) {
  486.                                         var o = this.get_node(e.currentTarget, true).children('.jstree-children')[0];
  487.                                         if(o) { $(this._firstChild(o)).children('.jstree-anchor').focus(); }
  488.                                 }
  489.                         },
  490.                         'down': function (e) {
  491.                                 // down
  492.                                 e.preventDefault();
  493.                                 var o = this.get_next_dom(e.currentTarget);
  494.                                 if(o && o.length) { o.children('.jstree-anchor').focus(); }
  495.                         },
  496.                         '*': function (e) {
  497.                                 // aria defines * on numpad as open_all - not very common
  498.                                 this.open_all();
  499.                         },
  500.                         'home': function (e) {
  501.                                 // home
  502.                                 e.preventDefault();
  503.                                 var o = this._firstChild(this.get_container_ul()[0]);
  504.                                 if(o) { $(o).children('.jstree-anchor').filter(':visible').focus(); }
  505.                         },
  506.                         'end': function (e) {
  507.                                 // end
  508.                                 e.preventDefault();
  509.                                 this.element.find('.jstree-anchor').filter(':visible').last().focus();
  510.                         },
  511.                         'f2': function (e) {
  512.                                 // f2 - safe to include - if check_callback is false it will fail
  513.                                 e.preventDefault();
  514.                                 this.edit(e.currentTarget);
  515.                         }
  516.                 }
  517.         };
  518.         $.jstree.core.prototype = {
  519.                 /**
  520.                  * used to decorate an instance with a plugin. Used internally.
  521.                  * @private
  522.                  * @name plugin(deco [, opts])
  523.                  * @param  {String} deco the plugin to decorate with
  524.                  * @param  {Object} opts options for the plugin
  525.                  * @return {jsTree}
  526.                  */
  527.                 plugin : function (deco, opts) {
  528.                         var Child = $.jstree.plugins[deco];
  529.                         if(Child) {
  530.                                 this._data[deco] = {};
  531.                                 Child.prototype = this;
  532.                                 return new Child(opts, this);
  533.                         }
  534.                         return this;
  535.                 },
  536.                 /**
  537.                  * initialize the instance. Used internally.
  538.                  * @private
  539.                  * @name init(el, optons)
  540.                  * @param {DOMElement|jQuery|String} el the element we are transforming
  541.                  * @param {Object} options options for this instance
  542.                  * @trigger init.jstree, loading.jstree, loaded.jstree, ready.jstree, changed.jstree
  543.                  */
  544.                 init : function (el, options) {
  545.                         this._model = {
  546.                                 data : {},
  547.                                 changed : [],
  548.                                 force_full_redraw : false,
  549.                                 redraw_timeout : false,
  550.                                 default_state : {
  551.                                         loaded : true,
  552.                                         opened : false,
  553.                                         selected : false,
  554.                                         disabled : false
  555.                                 }
  556.                         };
  557.                         this._model.data[$.jstree.root] = {
  558.                                 id : $.jstree.root,
  559.                                 parent : null,
  560.                                 parents : [],
  561.                                 children : [],
  562.                                 children_d : [],
  563.                                 state : { loaded : false }
  564.                         };
  565.  
  566.                         this.element = $(el).addClass('jstree jstree-' + this._id);
  567.                         this.settings = options;
  568.  
  569.                         this._data.core.ready = false;
  570.                         this._data.core.loaded = false;
  571.                         this._data.core.rtl = (this.element.css("direction") === "rtl");
  572.                         this.element[this._data.core.rtl ? 'addClass' : 'removeClass']("jstree-rtl");
  573.                         this.element.attr('role','tree');
  574.                         if(this.settings.core.multiple) {
  575.                                 this.element.attr('aria-multiselectable', true);
  576.                         }
  577.                         if(!this.element.attr('tabindex')) {
  578.                                 this.element.attr('tabindex','0');
  579.                         }
  580.  
  581.                         this.bind();
  582.                         /**
  583.                          * triggered after all events are bound
  584.                          * @event
  585.                          * @name init.jstree
  586.                          */
  587.                         this.trigger("init");
  588.  
  589.                         this._data.core.original_container_html = this.element.find(" > ul > li").clone(true);
  590.                         this._data.core.original_container_html
  591.                                 .find("li").addBack()
  592.                                 .contents().filter(function() {
  593.                                         return this.nodeType === 3 && (!this.nodeValue || /^\s+$/.test(this.nodeValue));
  594.                                 })
  595.                                 .remove();
  596.                         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>");
  597.                         this.element.attr('aria-activedescendant','j' + this._id + '_loading');
  598.                         this._data.core.li_height = this.get_container_ul().children("li").first().outerHeight() || 24;
  599.                         this._data.core.node = this._create_prototype_node();
  600.                         /**
  601.                          * triggered after the loading text is shown and before loading starts
  602.                          * @event
  603.                          * @name loading.jstree
  604.                          */
  605.                         this.trigger("loading");
  606.                         this.load_node($.jstree.root);
  607.                 },
  608.                 /**
  609.                  * destroy an instance
  610.                  * @name destroy()
  611.                  * @param  {Boolean} keep_html if not set to `true` the container will be emptied, otherwise the current DOM elements will be kept intact
  612.                  */
  613.                 destroy : function (keep_html) {
  614.                         /**
  615.                          * triggered before the tree is destroyed
  616.                          * @event
  617.                          * @name destroy.jstree
  618.                          */
  619.                         this.trigger("destroy");
  620.                         if(this._wrk) {
  621.                                 try {
  622.                                         window.URL.revokeObjectURL(this._wrk);
  623.                                         this._wrk = null;
  624.                                 }
  625.                                 catch (ignore) { }
  626.                         }
  627.                         if(!keep_html) { this.element.empty(); }
  628.                         this.teardown();
  629.                 },
  630.                 /**
  631.                  * Create a prototype node
  632.                  * @name _create_prototype_node()
  633.                  * @return {DOMElement}
  634.                  */
  635.                 _create_prototype_node : function () {
  636.                         var _node = document.createElement('LI'), _temp1, _temp2;
  637.                         _node.setAttribute('role', 'treeitem');
  638.                         _temp1 = document.createElement('I');
  639.                         _temp1.className = 'jstree-icon jstree-ocl';
  640.                         _temp1.setAttribute('role', 'presentation');
  641.                         _node.appendChild(_temp1);
  642.                         _temp1 = document.createElement('A');
  643.                         _temp1.className = 'jstree-anchor';
  644.                         _temp1.setAttribute('href','#');
  645.                         _temp1.setAttribute('tabindex','-1');
  646.                         _temp2 = document.createElement('I');
  647.                         _temp2.className = 'jstree-icon jstree-themeicon';
  648.                         _temp2.setAttribute('role', 'presentation');
  649.                         _temp1.appendChild(_temp2);
  650.                         _node.appendChild(_temp1);
  651.                         _temp1 = _temp2 = null;
  652.  
  653.                         return _node;
  654.                 },
  655.                 _kbevent_to_func : function (e) {
  656.                         var keys = {
  657.                                 8: "Backspace", 9: "Tab", 13: "Return", 19: "Pause", 27: "Esc",
  658.                                 32: "Space", 33: "PageUp", 34: "PageDown", 35: "End", 36: "Home",
  659.                                 37: "Left", 38: "Up", 39: "Right", 40: "Down", 44: "Print", 45: "Insert",
  660.                                 46: "Delete", 96: "Numpad0", 97: "Numpad1", 98: "Numpad2", 99 : "Numpad3",
  661.                                 100: "Numpad4", 101: "Numpad5", 102: "Numpad6", 103: "Numpad7",
  662.                                 104: "Numpad8", 105: "Numpad9", '-13': "NumpadEnter", 112: "F1",
  663.                                 113: "F2", 114: "F3", 115: "F4", 116: "F5", 117: "F6", 118: "F7",
  664.                                 119: "F8", 120: "F9", 121: "F10", 122: "F11", 123: "F12", 144: "Numlock",
  665.                                 145: "Scrolllock", 16: 'Shift', 17: 'Ctrl', 18: 'Alt',
  666.                                 48: '0',  49: '1',  50: '2',  51: '3',  52: '4', 53:  '5',
  667.                                 54: '6',  55: '7',  56: '8',  57: '9',  59: ';',  61: '=', 65:  'a',
  668.                                 66: 'b',  67: 'c',  68: 'd',  69: 'e',  70: 'f',  71: 'g', 72:  'h',
  669.                                 73: 'i',  74: 'j',  75: 'k',  76: 'l',  77: 'm',  78: 'n', 79:  'o',
  670.                                 80: 'p',  81: 'q',  82: 'r',  83: 's',  84: 't',  85: 'u', 86:  'v',
  671.                                 87: 'w',  88: 'x',  89: 'y',  90: 'z', 107: '+', 109: '-', 110: '.',
  672.                                 186: ';', 187: '=', 188: ',', 189: '-', 190: '.', 191: '/', 192: '`',
  673.                                 219: '[', 220: '\\',221: ']', 222: "'", 111: '/', 106: '*', 173: '-'
  674.                         };
  675.                         var parts = [];
  676.                         if (e.ctrlKey) { parts.push('ctrl'); }
  677.                         if (e.altKey) { parts.push('alt'); }
  678.                         if (e.shiftKey) { parts.push('shift'); }
  679.                         parts.push(keys[e.which] || e.which);
  680.                         parts = parts.sort().join('-').toLowerCase();
  681.  
  682.                         var kb = this.settings.core.keyboard, i, tmp;
  683.                         for (i in kb) {
  684.                                 if (kb.hasOwnProperty(i)) {
  685.                                         tmp = i;
  686.                                         if (tmp !== '-' && tmp !== '+') {
  687.                                                 tmp = tmp.replace('--', '-MINUS').replace('+-', '-MINUS').replace('++', '-PLUS').replace('-+', '-PLUS');
  688.                                                 tmp = tmp.split(/-|\+/).sort().join('-').replace('MINUS', '-').replace('PLUS', '+').toLowerCase();
  689.                                         }
  690.                                         if (tmp === parts) {
  691.                                                 return kb[i];
  692.                                         }
  693.                                 }
  694.                         }
  695.                         return null;
  696.                 },
  697.                 /**
  698.                  * part of the destroying of an instance. Used internally.
  699.                  * @private
  700.                  * @name teardown()
  701.                  */
  702.                 teardown : function () {
  703.                         this.unbind();
  704.                         this.element
  705.                                 .removeClass('jstree')
  706.                                 .removeData('jstree')
  707.                                 .find("[class^='jstree']")
  708.                                         .addBack()
  709.                                         .attr("class", function () { return this.className.replace(/jstree[^ ]*|$/ig,''); });
  710.                         this.element = null;
  711.                 },
  712.                 /**
  713.                  * bind all events. Used internally.
  714.                  * @private
  715.                  * @name bind()
  716.                  */
  717.                 bind : function () {
  718.                         var word = '',
  719.                                 tout = null,
  720.                                 was_click = 0;
  721.                         this.element
  722.                                 .on("dblclick.jstree", function (e) {
  723.                                                 if(e.target.tagName && e.target.tagName.toLowerCase() === "input") { return true; }
  724.                                                 if(document.selection && document.selection.empty) {
  725.                                                         document.selection.empty();
  726.                                                 }
  727.                                                 else {
  728.                                                         if(window.getSelection) {
  729.                                                                 var sel = window.getSelection();
  730.                                                                 try {
  731.                                                                         sel.removeAllRanges();
  732.                                                                         sel.collapse();
  733.                                                                 } catch (ignore) { }
  734.                                                         }
  735.                                                 }
  736.                                         })
  737.                                 .on("mousedown.jstree", $.proxy(function (e) {
  738.                                                 if(e.target === this.element[0]) {
  739.                                                         e.preventDefault(); // prevent losing focus when clicking scroll arrows (FF, Chrome)
  740.                                                         was_click = +(new Date()); // ie does not allow to prevent losing focus
  741.                                                 }
  742.                                         }, this))
  743.                                 .on("mousedown.jstree", ".jstree-ocl", function (e) {
  744.                                                 e.preventDefault(); // prevent any node inside from losing focus when clicking the open/close icon
  745.                                         })
  746.                                 .on("click.jstree", ".jstree-ocl", $.proxy(function (e) {
  747.                                                 this.toggle_node(e.target);
  748.                                         }, this))
  749.                                 .on("dblclick.jstree", ".jstree-anchor", $.proxy(function (e) {
  750.                                                 if(e.target.tagName && e.target.tagName.toLowerCase() === "input") { return true; }
  751.                                                 if(this.settings.core.dblclick_toggle) {
  752.                                                         this.toggle_node(e.target);
  753.                                                 }
  754.                                         }, this))
  755.                                 .on("click.jstree", ".jstree-anchor", $.proxy(function (e) {
  756.                                                 e.preventDefault();
  757.                                                 if(e.currentTarget !== document.activeElement) { $(e.currentTarget).focus(); }
  758.                                                 this.activate_node(e.currentTarget, e);
  759.                                         }, this))
  760.                                 .on('keydown.jstree', '.jstree-anchor', $.proxy(function (e) {
  761.                                                 if(e.target.tagName && e.target.tagName.toLowerCase() === "input") { return true; }
  762.                                                 if(this._data.core.rtl) {
  763.                                                         if(e.which === 37) { e.which = 39; }
  764.                                                         else if(e.which === 39) { e.which = 37; }
  765.                                                 }
  766.                                                 var f = this._kbevent_to_func(e);
  767.                                                 if (f) {
  768.                                                         var r = f.call(this, e);
  769.                                                         if (r === false || r === true) {
  770.                                                                 return r;
  771.                                                         }
  772.                                                 }
  773.                                         }, this))
  774.                                 .on("load_node.jstree", $.proxy(function (e, data) {
  775.                                                 if(data.status) {
  776.                                                         if(data.node.id === $.jstree.root && !this._data.core.loaded) {
  777.                                                                 this._data.core.loaded = true;
  778.                                                                 if(this._firstChild(this.get_container_ul()[0])) {
  779.                                                                         this.element.attr('aria-activedescendant',this._firstChild(this.get_container_ul()[0]).id);
  780.                                                                 }
  781.                                                                 /**
  782.                                                                  * triggered after the root node is loaded for the first time
  783.                                                                  * @event
  784.                                                                  * @name loaded.jstree
  785.                                                                  */
  786.                                                                 this.trigger("loaded");
  787.                                                         }
  788.                                                         if(!this._data.core.ready) {
  789.                                                                 setTimeout($.proxy(function() {
  790.                                                                         if(this.element && !this.get_container_ul().find('.jstree-loading').length) {
  791.                                                                                 this._data.core.ready = true;
  792.                                                                                 if(this._data.core.selected.length) {
  793.                                                                                         if(this.settings.core.expand_selected_onload) {
  794.                                                                                                 var tmp = [], i, j;
  795.                                                                                                 for(i = 0, j = this._data.core.selected.length; i < j; i++) {
  796.                                                                                                         tmp = tmp.concat(this._model.data[this._data.core.selected[i]].parents);
  797.                                                                                                 }
  798.                                                                                                 tmp = $.vakata.array_unique(tmp);
  799.                                                                                                 for(i = 0, j = tmp.length; i < j; i++) {
  800.                                                                                                         this.open_node(tmp[i], false, 0);
  801.                                                                                                 }
  802.                                                                                         }
  803.                                                                                         this.trigger('changed', { 'action' : 'ready', 'selected' : this._data.core.selected });
  804.                                                                                 }
  805.                                                                                 /**
  806.                                                                                  * triggered after all nodes are finished loading
  807.                                                                                  * @event
  808.                                                                                  * @name ready.jstree
  809.                                                                                  */
  810.                                                                                 this.trigger("ready");
  811.                                                                         }
  812.                                                                 }, this), 0);
  813.                                                         }
  814.                                                 }
  815.                                         }, this))
  816.                                 // quick searching when the tree is focused
  817.                                 .on('keypress.jstree', $.proxy(function (e) {
  818.                                                 if(e.target.tagName && e.target.tagName.toLowerCase() === "input") { return true; }
  819.                                                 if(tout) { clearTimeout(tout); }
  820.                                                 tout = setTimeout(function () {
  821.                                                         word = '';
  822.                                                 }, 500);
  823.  
  824.                                                 var chr = String.fromCharCode(e.which).toLowerCase(),
  825.                                                         col = this.element.find('.jstree-anchor').filter(':visible'),
  826.                                                         ind = col.index(document.activeElement) || 0,
  827.                                                         end = false;
  828.                                                 word += chr;
  829.  
  830.                                                 // match for whole word from current node down (including the current node)
  831.                                                 if(word.length > 1) {
  832.                                                         col.slice(ind).each($.proxy(function (i, v) {
  833.                                                                 if($(v).text().toLowerCase().indexOf(word) === 0) {
  834.                                                                         $(v).focus();
  835.                                                                         end = true;
  836.                                                                         return false;
  837.                                                                 }
  838.                                                         }, this));
  839.                                                         if(end) { return; }
  840.  
  841.                                                         // match for whole word from the beginning of the tree
  842.                                                         col.slice(0, ind).each($.proxy(function (i, v) {
  843.                                                                 if($(v).text().toLowerCase().indexOf(word) === 0) {
  844.                                                                         $(v).focus();
  845.                                                                         end = true;
  846.                                                                         return false;
  847.                                                                 }
  848.                                                         }, this));
  849.                                                         if(end) { return; }
  850.                                                 }
  851.                                                 // list nodes that start with that letter (only if word consists of a single char)
  852.                                                 if(new RegExp('^' + chr.replace(/[-\/\\^$*+?.()|[\]{}]/g, '\\$&') + '+$').test(word)) {
  853.                                                         // search for the next node starting with that letter
  854.                                                         col.slice(ind + 1).each($.proxy(function (i, v) {
  855.                                                                 if($(v).text().toLowerCase().charAt(0) === chr) {
  856.                                                                         $(v).focus();
  857.                                                                         end = true;
  858.                                                                         return false;
  859.                                                                 }
  860.                                                         }, this));
  861.                                                         if(end) { return; }
  862.  
  863.                                                         // search from the beginning
  864.                                                         col.slice(0, ind + 1).each($.proxy(function (i, v) {
  865.                                                                 if($(v).text().toLowerCase().charAt(0) === chr) {
  866.                                                                         $(v).focus();
  867.                                                                         end = true;
  868.                                                                         return false;
  869.                                                                 }
  870.                                                         }, this));
  871.                                                         if(end) { return; }
  872.                                                 }
  873.                                         }, this))
  874.                                 // THEME RELATED
  875.                                 .on("init.jstree", $.proxy(function () {
  876.                                                 var s = this.settings.core.themes;
  877.                                                 this._data.core.themes.dots                     = s.dots;
  878.                                                 this._data.core.themes.stripes          = s.stripes;
  879.                                                 this._data.core.themes.icons            = s.icons;
  880.                                                 this._data.core.themes.ellipsis         = s.ellipsis;
  881.                                                 this.set_theme(s.name || "default", s.url);
  882.                                                 this.set_theme_variant(s.variant);
  883.                                         }, this))
  884.                                 .on("loading.jstree", $.proxy(function () {
  885.                                                 this[ this._data.core.themes.dots ? "show_dots" : "hide_dots" ]();
  886.                                                 this[ this._data.core.themes.icons ? "show_icons" : "hide_icons" ]();
  887.                                                 this[ this._data.core.themes.stripes ? "show_stripes" : "hide_stripes" ]();
  888.                                                 this[ this._data.core.themes.ellipsis ? "show_ellipsis" : "hide_ellipsis" ]();
  889.                                         }, this))
  890.                                 .on('blur.jstree', '.jstree-anchor', $.proxy(function (e) {
  891.                                                 this._data.core.focused = null;
  892.                                                 $(e.currentTarget).filter('.jstree-hovered').mouseleave();
  893.                                                 this.element.attr('tabindex', '0');
  894.                                         }, this))
  895.                                 .on('focus.jstree', '.jstree-anchor', $.proxy(function (e) {
  896.                                                 var tmp = this.get_node(e.currentTarget);
  897.                                                 if(tmp && tmp.id) {
  898.                                                         this._data.core.focused = tmp.id;
  899.                                                 }
  900.                                                 this.element.find('.jstree-hovered').not(e.currentTarget).mouseleave();
  901.                                                 $(e.currentTarget).mouseenter();
  902.                                                 this.element.attr('tabindex', '-1');
  903.                                         }, this))
  904.                                 .on('focus.jstree', $.proxy(function () {
  905.                                                 if(+(new Date()) - was_click > 500 && !this._data.core.focused && this.settings.core.restore_focus) {
  906.                                                         was_click = 0;
  907.                                                         var act = this.get_node(this.element.attr('aria-activedescendant'), true);
  908.                                                         if(act) {
  909.                                                                 act.find('> .jstree-anchor').focus();
  910.                                                         }
  911.                                                 }
  912.                                         }, this))
  913.                                 .on('mouseenter.jstree', '.jstree-anchor', $.proxy(function (e) {
  914.                                                 this.hover_node(e.currentTarget);
  915.                                         }, this))
  916.                                 .on('mouseleave.jstree', '.jstree-anchor', $.proxy(function (e) {
  917.                                                 this.dehover_node(e.currentTarget);
  918.                                         }, this));
  919.                 },
  920.                 /**
  921.                  * part of the destroying of an instance. Used internally.
  922.                  * @private
  923.                  * @name unbind()
  924.                  */
  925.                 unbind : function () {
  926.                         this.element.off('.jstree');
  927.                         $(document).off('.jstree-' + this._id);
  928.                 },
  929.                 /**
  930.                  * trigger an event. Used internally.
  931.                  * @private
  932.                  * @name trigger(ev [, data])
  933.                  * @param  {String} ev the name of the event to trigger
  934.                  * @param  {Object} data additional data to pass with the event
  935.                  */
  936.                 trigger : function (ev, data) {
  937.                         if(!data) {
  938.                                 data = {};
  939.                         }
  940.                         data.instance = this;
  941.                         this.element.triggerHandler(ev.replace('.jstree','') + '.jstree', data);
  942.                 },
  943.                 /**
  944.                  * returns the jQuery extended instance container
  945.                  * @name get_container()
  946.                  * @return {jQuery}
  947.                  */
  948.                 get_container : function () {
  949.                         return this.element;
  950.                 },
  951.                 /**
  952.                  * returns the jQuery extended main UL node inside the instance container. Used internally.
  953.                  * @private
  954.                  * @name get_container_ul()
  955.                  * @return {jQuery}
  956.                  */
  957.                 get_container_ul : function () {
  958.                         return this.element.children(".jstree-children").first();
  959.                 },
  960.                 /**
  961.                  * gets string replacements (localization). Used internally.
  962.                  * @private
  963.                  * @name get_string(key)
  964.                  * @param  {String} key
  965.                  * @return {String}
  966.                  */
  967.                 get_string : function (key) {
  968.                         var a = this.settings.core.strings;
  969.                         if($.isFunction(a)) { return a.call(this, key); }
  970.                         if(a && a[key]) { return a[key]; }
  971.                         return key;
  972.                 },
  973.                 /**
  974.                  * gets the first child of a DOM node. Used internally.
  975.                  * @private
  976.                  * @name _firstChild(dom)
  977.                  * @param  {DOMElement} dom
  978.                  * @return {DOMElement}
  979.                  */
  980.                 _firstChild : function (dom) {
  981.                         dom = dom ? dom.firstChild : null;
  982.                         while(dom !== null && dom.nodeType !== 1) {
  983.                                 dom = dom.nextSibling;
  984.                         }
  985.                         return dom;
  986.                 },
  987.                 /**
  988.                  * gets the next sibling of a DOM node. Used internally.
  989.                  * @private
  990.                  * @name _nextSibling(dom)
  991.                  * @param  {DOMElement} dom
  992.                  * @return {DOMElement}
  993.                  */
  994.                 _nextSibling : function (dom) {
  995.                         dom = dom ? dom.nextSibling : null;
  996.                         while(dom !== null && dom.nodeType !== 1) {
  997.                                 dom = dom.nextSibling;
  998.                         }
  999.                         return dom;
  1000.                 },
  1001.                 /**
  1002.                  * gets the previous sibling of a DOM node. Used internally.
  1003.                  * @private
  1004.                  * @name _previousSibling(dom)
  1005.                  * @param  {DOMElement} dom
  1006.                  * @return {DOMElement}
  1007.                  */
  1008.                 _previousSibling : function (dom) {
  1009.                         dom = dom ? dom.previousSibling : null;
  1010.                         while(dom !== null && dom.nodeType !== 1) {
  1011.                                 dom = dom.previousSibling;
  1012.                         }
  1013.                         return dom;
  1014.                 },
  1015.                 /**
  1016.                  * 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)
  1017.                  * @name get_node(obj [, as_dom])
  1018.                  * @param  {mixed} obj
  1019.                  * @param  {Boolean} as_dom
  1020.                  * @return {Object|jQuery}
  1021.                  */
  1022.                 get_node : function (obj, as_dom) {
  1023.                         if(obj && obj.id) {
  1024.                                 obj = obj.id;
  1025.                         }
  1026.                         if (obj instanceof jQuery && obj.length && obj[0].id) {
  1027.                                 obj = obj[0].id;
  1028.                         }
  1029.                         var dom;
  1030.                         try {
  1031.                                 if(this._model.data[obj]) {
  1032.                                         obj = this._model.data[obj];
  1033.                                 }
  1034.                                 else if(typeof obj === "string" && this._model.data[obj.replace(/^#/, '')]) {
  1035.                                         obj = this._model.data[obj.replace(/^#/, '')];
  1036.                                 }
  1037.                                 else if(typeof obj === "string" && (dom = $('#' + obj.replace($.jstree.idregex,'\\$&'), this.element)).length && this._model.data[dom.closest('.jstree-node').attr('id')]) {
  1038.                                         obj = this._model.data[dom.closest('.jstree-node').attr('id')];
  1039.                                 }
  1040.                                 else if((dom = this.element.find(obj)).length && this._model.data[dom.closest('.jstree-node').attr('id')]) {
  1041.                                         obj = this._model.data[dom.closest('.jstree-node').attr('id')];
  1042.                                 }
  1043.                                 else if((dom = this.element.find(obj)).length && dom.hasClass('jstree')) {
  1044.                                         obj = this._model.data[$.jstree.root];
  1045.                                 }
  1046.                                 else {
  1047.                                         return false;
  1048.                                 }
  1049.  
  1050.                                 if(as_dom) {
  1051.                                         obj = obj.id === $.jstree.root ? this.element : $('#' + obj.id.replace($.jstree.idregex,'\\$&'), this.element);
  1052.                                 }
  1053.                                 return obj;
  1054.                         } catch (ex) { return false; }
  1055.                 },
  1056.                 /**
  1057.                  * get the path to a node, either consisting of node texts, or of node IDs, optionally glued together (otherwise an array)
  1058.                  * @name get_path(obj [, glue, ids])
  1059.                  * @param  {mixed} obj the node
  1060.                  * @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
  1061.                  * @param  {Boolean} ids if set to true build the path using ID, otherwise node text is used
  1062.                  * @return {mixed}
  1063.                  */
  1064.                 get_path : function (obj, glue, ids) {
  1065.                         obj = obj.parents ? obj : this.get_node(obj);
  1066.                         if(!obj || obj.id === $.jstree.root || !obj.parents) {
  1067.                                 return false;
  1068.                         }
  1069.                         var i, j, p = [];
  1070.                         p.push(ids ? obj.id : obj.text);
  1071.                         for(i = 0, j = obj.parents.length; i < j; i++) {
  1072.                                 p.push(ids ? obj.parents[i] : this.get_text(obj.parents[i]));
  1073.                         }
  1074.                         p = p.reverse().slice(1);
  1075.                         return glue ? p.join(glue) : p;
  1076.                 },
  1077.                 /**
  1078.                  * get the next visible node that is below the `obj` node. If `strict` is set to `true` only sibling nodes are returned.
  1079.                  * @name get_next_dom(obj [, strict])
  1080.                  * @param  {mixed} obj
  1081.                  * @param  {Boolean} strict
  1082.                  * @return {jQuery}
  1083.                  */
  1084.                 get_next_dom : function (obj, strict) {
  1085.                         var tmp;
  1086.                         obj = this.get_node(obj, true);
  1087.                         if(obj[0] === this.element[0]) {
  1088.                                 tmp = this._firstChild(this.get_container_ul()[0]);
  1089.                                 while (tmp && tmp.offsetHeight === 0) {
  1090.                                         tmp = this._nextSibling(tmp);
  1091.                                 }
  1092.                                 return tmp ? $(tmp) : false;
  1093.                         }
  1094.                         if(!obj || !obj.length) {
  1095.                                 return false;
  1096.                         }
  1097.                         if(strict) {
  1098.                                 tmp = obj[0];
  1099.                                 do {
  1100.                                         tmp = this._nextSibling(tmp);
  1101.                                 } while (tmp && tmp.offsetHeight === 0);
  1102.                                 return tmp ? $(tmp) : false;
  1103.                         }
  1104.                         if(obj.hasClass("jstree-open")) {
  1105.                                 tmp = this._firstChild(obj.children('.jstree-children')[0]);
  1106.                                 while (tmp && tmp.offsetHeight === 0) {
  1107.                                         tmp = this._nextSibling(tmp);
  1108.                                 }
  1109.                                 if(tmp !== null) {
  1110.                                         return $(tmp);
  1111.                                 }
  1112.                         }
  1113.                         tmp = obj[0];
  1114.                         do {
  1115.                                 tmp = this._nextSibling(tmp);
  1116.                         } while (tmp && tmp.offsetHeight === 0);
  1117.                         if(tmp !== null) {
  1118.                                 return $(tmp);
  1119.                         }
  1120.                         return obj.parentsUntil(".jstree",".jstree-node").nextAll(".jstree-node:visible").first();
  1121.                 },
  1122.                 /**
  1123.                  * get the previous visible node that is above the `obj` node. If `strict` is set to `true` only sibling nodes are returned.
  1124.                  * @name get_prev_dom(obj [, strict])
  1125.                  * @param  {mixed} obj
  1126.                  * @param  {Boolean} strict
  1127.                  * @return {jQuery}
  1128.                  */
  1129.                 get_prev_dom : function (obj, strict) {
  1130.                         var tmp;
  1131.                         obj = this.get_node(obj, true);
  1132.                         if(obj[0] === this.element[0]) {
  1133.                                 tmp = this.get_container_ul()[0].lastChild;
  1134.                                 while (tmp && tmp.offsetHeight === 0) {
  1135.                                         tmp = this._previousSibling(tmp);
  1136.                                 }
  1137.                                 return tmp ? $(tmp) : false;
  1138.                         }
  1139.                         if(!obj || !obj.length) {
  1140.                                 return false;
  1141.                         }
  1142.                         if(strict) {
  1143.                                 tmp = obj[0];
  1144.                                 do {
  1145.                                         tmp = this._previousSibling(tmp);
  1146.                                 } while (tmp && tmp.offsetHeight === 0);
  1147.                                 return tmp ? $(tmp) : false;
  1148.                         }
  1149.                         tmp = obj[0];
  1150.                         do {
  1151.                                 tmp = this._previousSibling(tmp);
  1152.                         } while (tmp && tmp.offsetHeight === 0);
  1153.                         if(tmp !== null) {
  1154.                                 obj = $(tmp);
  1155.                                 while(obj.hasClass("jstree-open")) {
  1156.                                         obj = obj.children(".jstree-children").first().children(".jstree-node:visible:last");
  1157.                                 }
  1158.                                 return obj;
  1159.                         }
  1160.                         tmp = obj[0].parentNode.parentNode;
  1161.                         return tmp && tmp.className && tmp.className.indexOf('jstree-node') !== -1 ? $(tmp) : false;
  1162.                 },
  1163.                 /**
  1164.                  * get the parent ID of a node
  1165.                  * @name get_parent(obj)
  1166.                  * @param  {mixed} obj
  1167.                  * @return {String}
  1168.                  */
  1169.                 get_parent : function (obj) {
  1170.                         obj = this.get_node(obj);
  1171.                         if(!obj || obj.id === $.jstree.root) {
  1172.                                 return false;
  1173.                         }
  1174.                         return obj.parent;
  1175.                 },
  1176.                 /**
  1177.                  * get a jQuery collection of all the children of a node (node must be rendered), returns false on error
  1178.                  * @name get_children_dom(obj)
  1179.                  * @param  {mixed} obj
  1180.                  * @return {jQuery}
  1181.                  */
  1182.                 get_children_dom : function (obj) {
  1183.                         obj = this.get_node(obj, true);
  1184.                         if(obj[0] === this.element[0]) {
  1185.                                 return this.get_container_ul().children(".jstree-node");
  1186.                         }
  1187.                         if(!obj || !obj.length) {
  1188.                                 return false;
  1189.                         }
  1190.                         return obj.children(".jstree-children").children(".jstree-node");
  1191.                 },
  1192.                 /**
  1193.                  * checks if a node has children
  1194.                  * @name is_parent(obj)
  1195.                  * @param  {mixed} obj
  1196.                  * @return {Boolean}
  1197.                  */
  1198.                 is_parent : function (obj) {
  1199.                         obj = this.get_node(obj);
  1200.                         return obj && (obj.state.loaded === false || obj.children.length > 0);
  1201.                 },
  1202.                 /**
  1203.                  * checks if a node is loaded (its children are available)
  1204.                  * @name is_loaded(obj)
  1205.                  * @param  {mixed} obj
  1206.                  * @return {Boolean}
  1207.                  */
  1208.                 is_loaded : function (obj) {
  1209.                         obj = this.get_node(obj);
  1210.                         return obj && obj.state.loaded;
  1211.                 },
  1212.                 /**
  1213.                  * check if a node is currently loading (fetching children)
  1214.                  * @name is_loading(obj)
  1215.                  * @param  {mixed} obj
  1216.                  * @return {Boolean}
  1217.                  */
  1218.                 is_loading : function (obj) {
  1219.                         obj = this.get_node(obj);
  1220.                         return obj && obj.state && obj.state.loading;
  1221.                 },
  1222.                 /**
  1223.                  * check if a node is opened
  1224.                  * @name is_open(obj)
  1225.                  * @param  {mixed} obj
  1226.                  * @return {Boolean}
  1227.                  */
  1228.                 is_open : function (obj) {
  1229.                         obj = this.get_node(obj);
  1230.                         return obj && obj.state.opened;
  1231.                 },
  1232.                 /**
  1233.                  * check if a node is in a closed state
  1234.                  * @name is_closed(obj)
  1235.                  * @param  {mixed} obj
  1236.                  * @return {Boolean}
  1237.                  */
  1238.                 is_closed : function (obj) {
  1239.                         obj = this.get_node(obj);
  1240.                         return obj && this.is_parent(obj) && !obj.state.opened;
  1241.                 },
  1242.                 /**
  1243.                  * check if a node has no children
  1244.                  * @name is_leaf(obj)
  1245.                  * @param  {mixed} obj
  1246.                  * @return {Boolean}
  1247.                  */
  1248.                 is_leaf : function (obj) {
  1249.                         return !this.is_parent(obj);
  1250.                 },
  1251.                 /**
  1252.                  * loads a node (fetches its children using the `core.data` setting). Multiple nodes can be passed to by using an array.
  1253.                  * @name load_node(obj [, callback])
  1254.                  * @param  {mixed} obj
  1255.                  * @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
  1256.                  * @return {Boolean}
  1257.                  * @trigger load_node.jstree
  1258.                  */
  1259.                 load_node : function (obj, callback) {
  1260.                         var k, l, i, j, c;
  1261.                         if($.isArray(obj)) {
  1262.                                 this._load_nodes(obj.slice(), callback);
  1263.                                 return true;
  1264.                         }
  1265.                         obj = this.get_node(obj);
  1266.                         if(!obj) {
  1267.                                 if(callback) { callback.call(this, obj, false); }
  1268.                                 return false;
  1269.                         }
  1270.                         // 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?
  1271.                         if(obj.state.loaded) {
  1272.                                 obj.state.loaded = false;
  1273.                                 for(i = 0, j = obj.parents.length; i < j; i++) {
  1274.                                         this._model.data[obj.parents[i]].children_d = $.vakata.array_filter(this._model.data[obj.parents[i]].children_d, function (v) {
  1275.                                                 return $.inArray(v, obj.children_d) === -1;
  1276.                                         });
  1277.                                 }
  1278.                                 for(k = 0, l = obj.children_d.length; k < l; k++) {
  1279.                                         if(this._model.data[obj.children_d[k]].state.selected) {
  1280.                                                 c = true;
  1281.                                         }
  1282.                                         delete this._model.data[obj.children_d[k]];
  1283.                                 }
  1284.                                 if (c) {
  1285.                                         this._data.core.selected = $.vakata.array_filter(this._data.core.selected, function (v) {
  1286.                                                 return $.inArray(v, obj.children_d) === -1;
  1287.                                         });
  1288.                                 }
  1289.                                 obj.children = [];
  1290.                                 obj.children_d = [];
  1291.                                 if(c) {
  1292.                                         this.trigger('changed', { 'action' : 'load_node', 'node' : obj, 'selected' : this._data.core.selected });
  1293.                                 }
  1294.                         }
  1295.                         obj.state.failed = false;
  1296.                         obj.state.loading = true;
  1297.                         this.get_node(obj, true).addClass("jstree-loading").attr('aria-busy',true);
  1298.                         this._load_node(obj, $.proxy(function (status) {
  1299.                                 obj = this._model.data[obj.id];
  1300.                                 obj.state.loading = false;
  1301.                                 obj.state.loaded = status;
  1302.                                 obj.state.failed = !obj.state.loaded;
  1303.                                 var dom = this.get_node(obj, true), i = 0, j = 0, m = this._model.data, has_children = false;
  1304.                                 for(i = 0, j = obj.children.length; i < j; i++) {
  1305.                                         if(m[obj.children[i]] && !m[obj.children[i]].state.hidden) {
  1306.                                                 has_children = true;
  1307.                                                 break;
  1308.                                         }
  1309.                                 }
  1310.                                 if(obj.state.loaded && dom && dom.length) {
  1311.                                         dom.removeClass('jstree-closed jstree-open jstree-leaf');
  1312.                                         if (!has_children) {
  1313.                                                 dom.addClass('jstree-leaf');
  1314.                                         }
  1315.                                         else {
  1316.                                                 if (obj.id !== '#') {
  1317.                                                         dom.addClass(obj.state.opened ? 'jstree-open' : 'jstree-closed');
  1318.                                                 }
  1319.                                         }
  1320.                                 }
  1321.                                 dom.removeClass("jstree-loading").attr('aria-busy',false);
  1322.                                 /**
  1323.                                  * triggered after a node is loaded
  1324.                                  * @event
  1325.                                  * @name load_node.jstree
  1326.                                  * @param {Object} node the node that was loading
  1327.                                  * @param {Boolean} status was the node loaded successfully
  1328.                                  */
  1329.                                 this.trigger('load_node', { "node" : obj, "status" : status });
  1330.                                 if(callback) {
  1331.                                         callback.call(this, obj, status);
  1332.                                 }
  1333.                         }, this));
  1334.                         return true;
  1335.                 },
  1336.                 /**
  1337.                  * load an array of nodes (will also load unavailable nodes as soon as they appear in the structure). Used internally.
  1338.                  * @private
  1339.                  * @name _load_nodes(nodes [, callback])
  1340.                  * @param  {array} nodes
  1341.                  * @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
  1342.                  */
  1343.                 _load_nodes : function (nodes, callback, is_callback, force_reload) {
  1344.                         var r = true,
  1345.                                 c = function () { this._load_nodes(nodes, callback, true); },
  1346.                                 m = this._model.data, i, j, tmp = [];
  1347.                         for(i = 0, j = nodes.length; i < j; i++) {
  1348.                                 if(m[nodes[i]] && ( (!m[nodes[i]].state.loaded && !m[nodes[i]].state.failed) || (!is_callback && force_reload) )) {
  1349.                                         if(!this.is_loading(nodes[i])) {
  1350.                                                 this.load_node(nodes[i], c);
  1351.                                         }
  1352.                                         r = false;
  1353.                                 }
  1354.                         }
  1355.                         if(r) {
  1356.                                 for(i = 0, j = nodes.length; i < j; i++) {
  1357.                                         if(m[nodes[i]] && m[nodes[i]].state.loaded) {
  1358.                                                 tmp.push(nodes[i]);
  1359.                                         }
  1360.                                 }
  1361.                                 if(callback && !callback.done) {
  1362.                                         callback.call(this, tmp);
  1363.                                         callback.done = true;
  1364.                                 }
  1365.                         }
  1366.                 },
  1367.                 /**
  1368.                  * loads all unloaded nodes
  1369.                  * @name load_all([obj, callback])
  1370.                  * @param {mixed} obj the node to load recursively, omit to load all nodes in the tree
  1371.                  * @param {function} callback a function to be executed once loading all the nodes is complete,
  1372.                  * @trigger load_all.jstree
  1373.                  */
  1374.                 load_all : function (obj, callback) {
  1375.                         if(!obj) { obj = $.jstree.root; }
  1376.                         obj = this.get_node(obj);
  1377.                         if(!obj) { return false; }
  1378.                         var to_load = [],
  1379.                                 m = this._model.data,
  1380.                                 c = m[obj.id].children_d,
  1381.                                 i, j;
  1382.                         if(obj.state && !obj.state.loaded) {
  1383.                                 to_load.push(obj.id);
  1384.                         }
  1385.                         for(i = 0, j = c.length; i < j; i++) {
  1386.                                 if(m[c[i]] && m[c[i]].state && !m[c[i]].state.loaded) {
  1387.                                         to_load.push(c[i]);
  1388.                                 }
  1389.                         }
  1390.                         if(to_load.length) {
  1391.                                 this._load_nodes(to_load, function () {
  1392.                                         this.load_all(obj, callback);
  1393.                                 });
  1394.                         }
  1395.                         else {
  1396.                                 /**
  1397.                                  * triggered after a load_all call completes
  1398.                                  * @event
  1399.                                  * @name load_all.jstree
  1400.                                  * @param {Object} node the recursively loaded node
  1401.                                  */
  1402.                                 if(callback) { callback.call(this, obj); }
  1403.                                 this.trigger('load_all', { "node" : obj });
  1404.                         }
  1405.                 },
  1406.                 /**
  1407.                  * handles the actual loading of a node. Used only internally.
  1408.                  * @private
  1409.                  * @name _load_node(obj [, callback])
  1410.                  * @param  {mixed} obj
  1411.                  * @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
  1412.                  * @return {Boolean}
  1413.                  */
  1414.                 _load_node : function (obj, callback) {
  1415.                         var s = this.settings.core.data, t;
  1416.                         var notTextOrCommentNode = function notTextOrCommentNode () {
  1417.                                 return this.nodeType !== 3 && this.nodeType !== 8;
  1418.                         };
  1419.                         // use original HTML
  1420.                         if(!s) {
  1421.                                 if(obj.id === $.jstree.root) {
  1422.                                         return this._append_html_data(obj, this._data.core.original_container_html.clone(true), function (status) {
  1423.                                                 callback.call(this, status);
  1424.                                         });
  1425.                                 }
  1426.                                 else {
  1427.                                         return callback.call(this, false);
  1428.                                 }
  1429.                                 // return callback.call(this, obj.id === $.jstree.root ? this._append_html_data(obj, this._data.core.original_container_html.clone(true)) : false);
  1430.                         }
  1431.                         if($.isFunction(s)) {
  1432.                                 return s.call(this, obj, $.proxy(function (d) {
  1433.                                         if(d === false) {
  1434.                                                 callback.call(this, false);
  1435.                                         }
  1436.                                         else {
  1437.                                                 this[typeof d === 'string' ? '_append_html_data' : '_append_json_data'](obj, typeof d === 'string' ? $($.parseHTML(d)).filter(notTextOrCommentNode) : d, function (status) {
  1438.                                                         callback.call(this, status);
  1439.                                                 });
  1440.                                         }
  1441.                                         // 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));
  1442.                                 }, this));
  1443.                         }
  1444.                         if(typeof s === 'object') {
  1445.                                 if(s.url) {
  1446.                                         s = $.extend(true, {}, s);
  1447.                                         if($.isFunction(s.url)) {
  1448.                                                 s.url = s.url.call(this, obj);
  1449.                                         }
  1450.                                         if($.isFunction(s.data)) {
  1451.                                                 s.data = s.data.call(this, obj);
  1452.                                         }
  1453.                                         return $.ajax(s)
  1454.                                                 .done($.proxy(function (d,t,x) {
  1455.                                                                 var type = x.getResponseHeader('Content-Type');
  1456.                                                                 if((type && type.indexOf('json') !== -1) || typeof d === "object") {
  1457.                                                                         return this._append_json_data(obj, d, function (status) { callback.call(this, status); });
  1458.                                                                         //return callback.call(this, this._append_json_data(obj, d));
  1459.                                                                 }
  1460.                                                                 if((type && type.indexOf('html') !== -1) || typeof d === "string") {
  1461.                                                                         return this._append_html_data(obj, $($.parseHTML(d)).filter(notTextOrCommentNode), function (status) { callback.call(this, status); });
  1462.                                                                         // return callback.call(this, this._append_html_data(obj, $(d)));
  1463.                                                                 }
  1464.                                                                 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 }) };
  1465.                                                                 this.settings.core.error.call(this, this._data.core.last_error);
  1466.                                                                 return callback.call(this, false);
  1467.                                                         }, this))
  1468.                                                 .fail($.proxy(function (f) {
  1469.                                                                 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 }) };
  1470.                                                                 callback.call(this, false);
  1471.                                                                 this.settings.core.error.call(this, this._data.core.last_error);
  1472.                                                         }, this));
  1473.                                 }
  1474.                                 if ($.isArray(s)) {
  1475.                                         t = $.extend(true, [], s);
  1476.                                 } else if ($.isPlainObject(s)) {
  1477.                                         t = $.extend(true, {}, s);
  1478.                                 } else {
  1479.                                         t = s;
  1480.                                 }
  1481.                                 if(obj.id === $.jstree.root) {
  1482.                                         return this._append_json_data(obj, t, function (status) {
  1483.                                                 callback.call(this, status);
  1484.                                         });
  1485.                                 }
  1486.                                 else {
  1487.                                         this._data.core.last_error = { 'error' : 'nodata', 'plugin' : 'core', 'id' : 'core_05', 'reason' : 'Could not load node', 'data' : JSON.stringify({ 'id' : obj.id }) };
  1488.                                         this.settings.core.error.call(this, this._data.core.last_error);
  1489.                                         return callback.call(this, false);
  1490.                                 }
  1491.                                 //return callback.call(this, (obj.id === $.jstree.root ? this._append_json_data(obj, t) : false) );
  1492.                         }
  1493.                         if(typeof s === 'string') {
  1494.                                 if(obj.id === $.jstree.root) {
  1495.                                         return this._append_html_data(obj, $($.parseHTML(s)).filter(notTextOrCommentNode), function (status) {
  1496.                                                 callback.call(this, status);
  1497.                                         });
  1498.                                 }
  1499.                                 else {
  1500.                                         this._data.core.last_error = { 'error' : 'nodata', 'plugin' : 'core', 'id' : 'core_06', 'reason' : 'Could not load node', 'data' : JSON.stringify({ 'id' : obj.id }) };
  1501.                                         this.settings.core.error.call(this, this._data.core.last_error);
  1502.                                         return callback.call(this, false);
  1503.                                 }
  1504.                                 //return callback.call(this, (obj.id === $.jstree.root ? this._append_html_data(obj, $(s)) : false) );
  1505.                         }
  1506.                         return callback.call(this, false);
  1507.                 },
  1508.                 /**
  1509.                  * adds a node to the list of nodes to redraw. Used only internally.
  1510.                  * @private
  1511.                  * @name _node_changed(obj [, callback])
  1512.                  * @param  {mixed} obj
  1513.                  */
  1514.                 _node_changed : function (obj) {
  1515.                         obj = this.get_node(obj);
  1516.       if (obj && $.inArray(obj.id, this._model.changed) === -1) {
  1517.                                 this._model.changed.push(obj.id);
  1518.                         }
  1519.                 },
  1520.                 /**
  1521.                  * appends HTML content to the tree. Used internally.
  1522.                  * @private
  1523.                  * @name _append_html_data(obj, data)
  1524.                  * @param  {mixed} obj the node to append to
  1525.                  * @param  {String} data the HTML string to parse and append
  1526.                  * @trigger model.jstree, changed.jstree
  1527.                  */
  1528.                 _append_html_data : function (dom, data, cb) {
  1529.                         dom = this.get_node(dom);
  1530.                         dom.children = [];
  1531.                         dom.children_d = [];
  1532.                         var dat = data.is('ul') ? data.children() : data,
  1533.                                 par = dom.id,
  1534.                                 chd = [],
  1535.                                 dpc = [],
  1536.                                 m = this._model.data,
  1537.                                 p = m[par],
  1538.                                 s = this._data.core.selected.length,
  1539.                                 tmp, i, j;
  1540.                         dat.each($.proxy(function (i, v) {
  1541.                                 tmp = this._parse_model_from_html($(v), par, p.parents.concat());
  1542.                                 if(tmp) {
  1543.                                         chd.push(tmp);
  1544.                                         dpc.push(tmp);
  1545.                                         if(m[tmp].children_d.length) {
  1546.                                                 dpc = dpc.concat(m[tmp].children_d);
  1547.                                         }
  1548.                                 }
  1549.                         }, this));
  1550.                         p.children = chd;
  1551.                         p.children_d = dpc;
  1552.                         for(i = 0, j = p.parents.length; i < j; i++) {
  1553.                                 m[p.parents[i]].children_d = m[p.parents[i]].children_d.concat(dpc);
  1554.                         }
  1555.                         /**
  1556.                          * triggered when new data is inserted to the tree model
  1557.                          * @event
  1558.                          * @name model.jstree
  1559.                          * @param {Array} nodes an array of node IDs
  1560.                          * @param {String} parent the parent ID of the nodes
  1561.                          */
  1562.                         this.trigger('model', { "nodes" : dpc, 'parent' : par });
  1563.                         if(par !== $.jstree.root) {
  1564.                                 this._node_changed(par);
  1565.                                 this.redraw();
  1566.                         }
  1567.                         else {
  1568.                                 this.get_container_ul().children('.jstree-initial-node').remove();
  1569.                                 this.redraw(true);
  1570.                         }
  1571.                         if(this._data.core.selected.length !== s) {
  1572.                                 this.trigger('changed', { 'action' : 'model', 'selected' : this._data.core.selected });
  1573.                         }
  1574.                         cb.call(this, true);
  1575.                 },
  1576.                 /**
  1577.                  * appends JSON content to the tree. Used internally.
  1578.                  * @private
  1579.                  * @name _append_json_data(obj, data)
  1580.                  * @param  {mixed} obj the node to append to
  1581.                  * @param  {String} data the JSON object to parse and append
  1582.                  * @param  {Boolean} force_processing internal param - do not set
  1583.                  * @trigger model.jstree, changed.jstree
  1584.                  */
  1585.                 _append_json_data : function (dom, data, cb, force_processing) {
  1586.                         if(this.element === null) { return; }
  1587.                         dom = this.get_node(dom);
  1588.                         dom.children = [];
  1589.                         dom.children_d = [];
  1590.                         // *%$@!!!
  1591.                         if(data.d) {
  1592.                                 data = data.d;
  1593.                                 if(typeof data === "string") {
  1594.                                         data = JSON.parse(data);
  1595.                                 }
  1596.                         }
  1597.                         if(!$.isArray(data)) { data = [data]; }
  1598.                         var w = null,
  1599.                                 args = {
  1600.                                         'df'    : this._model.default_state,
  1601.                                         'dat'   : data,
  1602.                                         'par'   : dom.id,
  1603.                                         'm'             : this._model.data,
  1604.                                         't_id'  : this._id,
  1605.                                         't_cnt' : this._cnt,
  1606.                                         'sel'   : this._data.core.selected
  1607.                                 },
  1608.                                 func = function (data, undefined) {
  1609.                                         if(data.data) { data = data.data; }
  1610.                                         var dat = data.dat,
  1611.                                                 par = data.par,
  1612.                                                 chd = [],
  1613.                                                 dpc = [],
  1614.                                                 add = [],
  1615.                                                 df = data.df,
  1616.                                                 t_id = data.t_id,
  1617.                                                 t_cnt = data.t_cnt,
  1618.                                                 m = data.m,
  1619.                                                 p = m[par],
  1620.                                                 sel = data.sel,
  1621.                                                 tmp, i, j, rslt,
  1622.                                                 parse_flat = function (d, p, ps) {
  1623.                                                         if(!ps) { ps = []; }
  1624.                                                         else { ps = ps.concat(); }
  1625.                                                         if(p) { ps.unshift(p); }
  1626.                                                         var tid = d.id.toString(),
  1627.                                                                 i, j, c, e,
  1628.                                                                 tmp = {
  1629.                                                                         id                      : tid,
  1630.                                                                         text            : d.text || '',
  1631.                                                                         icon            : d.icon !== undefined ? d.icon : true,
  1632.                                                                         parent          : p,
  1633.                                                                         parents         : ps,
  1634.                                                                         children        : d.children || [],
  1635.                                                                         children_d      : d.children_d || [],
  1636.                                                                         data            : d.data,
  1637.                                                                         state           : { },
  1638.                                                                         li_attr         : { id : false },
  1639.                                                                         a_attr          : { href : '#' },
  1640.                                                                         original        : false
  1641.                                                                 };
  1642.                                                         for(i in df) {
  1643.                                                                 if(df.hasOwnProperty(i)) {
  1644.                                                                         tmp.state[i] = df[i];
  1645.                                                                 }
  1646.                                                         }
  1647.                                                         if(d && d.data && d.data.jstree && d.data.jstree.icon) {
  1648.                                                                 tmp.icon = d.data.jstree.icon;
  1649.                                                         }
  1650.                                                         if(tmp.icon === undefined || tmp.icon === null || tmp.icon === "") {
  1651.                                                                 tmp.icon = true;
  1652.                                                         }
  1653.                                                         if(d && d.data) {
  1654.                                                                 tmp.data = d.data;
  1655.                                                                 if(d.data.jstree) {
  1656.                                                                         for(i in d.data.jstree) {
  1657.                                                                                 if(d.data.jstree.hasOwnProperty(i)) {
  1658.                                                                                         tmp.state[i] = d.data.jstree[i];
  1659.                                                                                 }
  1660.                                                                         }
  1661.                                                                 }
  1662.                                                         }
  1663.                                                         if(d && typeof d.state === 'object') {
  1664.                                                                 for (i in d.state) {
  1665.                                                                         if(d.state.hasOwnProperty(i)) {
  1666.                                                                                 tmp.state[i] = d.state[i];
  1667.                                                                         }
  1668.                                                                 }
  1669.                                                         }
  1670.                                                         if(d && typeof d.li_attr === 'object') {
  1671.                                                                 for (i in d.li_attr) {
  1672.                                                                         if(d.li_attr.hasOwnProperty(i)) {
  1673.                                                                                 tmp.li_attr[i] = d.li_attr[i];
  1674.                                                                         }
  1675.                                                                 }
  1676.                                                         }
  1677.                                                         if(!tmp.li_attr.id) {
  1678.                                                                 tmp.li_attr.id = tid;
  1679.                                                         }
  1680.                                                         if(d && typeof d.a_attr === 'object') {
  1681.                                                                 for (i in d.a_attr) {
  1682.                                                                         if(d.a_attr.hasOwnProperty(i)) {
  1683.                                                                                 tmp.a_attr[i] = d.a_attr[i];
  1684.                                                                         }
  1685.                                                                 }
  1686.                                                         }
  1687.                                                         if(d && d.children && d.children === true) {
  1688.                                                                 tmp.state.loaded = false;
  1689.                                                                 tmp.children = [];
  1690.                                                                 tmp.children_d = [];
  1691.                                                         }
  1692.                                                         m[tmp.id] = tmp;
  1693.                                                         for(i = 0, j = tmp.children.length; i < j; i++) {
  1694.                                                                 c = parse_flat(m[tmp.children[i]], tmp.id, ps);
  1695.                                                                 e = m[c];
  1696.                                                                 tmp.children_d.push(c);
  1697.                                                                 if(e.children_d.length) {
  1698.                                                                         tmp.children_d = tmp.children_d.concat(e.children_d);
  1699.                                                                 }
  1700.                                                         }
  1701.                                                         delete d.data;
  1702.                                                         delete d.children;
  1703.                                                         m[tmp.id].original = d;
  1704.                                                         if(tmp.state.selected) {
  1705.                                                                 add.push(tmp.id);
  1706.                                                         }
  1707.                                                         return tmp.id;
  1708.                                                 },
  1709.                                                 parse_nest = function (d, p, ps) {
  1710.                                                         if(!ps) { ps = []; }
  1711.                                                         else { ps = ps.concat(); }
  1712.                                                         if(p) { ps.unshift(p); }
  1713.                                                         var tid = false, i, j, c, e, tmp;
  1714.                                                         do {
  1715.                                                                 tid = 'j' + t_id + '_' + (++t_cnt);
  1716.                                                         } while(m[tid]);
  1717.  
  1718.                                                         tmp = {
  1719.                                                                 id                      : false,
  1720.                                                                 text            : typeof d === 'string' ? d : '',
  1721.                                                                 icon            : typeof d === 'object' && d.icon !== undefined ? d.icon : true,
  1722.                                                                 parent          : p,
  1723.                                                                 parents         : ps,
  1724.                                                                 children        : [],
  1725.                                                                 children_d      : [],
  1726.                                                                 data            : null,
  1727.                                                                 state           : { },
  1728.                                                                 li_attr         : { id : false },
  1729.                                                                 a_attr          : { href : '#' },
  1730.                                                                 original        : false
  1731.                                                         };
  1732.                                                         for(i in df) {
  1733.                                                                 if(df.hasOwnProperty(i)) {
  1734.                                                                         tmp.state[i] = df[i];
  1735.                                                                 }
  1736.                                                         }
  1737.                                                         if(d && d.id) { tmp.id = d.id.toString(); }
  1738.                                                         if(d && d.text) { tmp.text = d.text; }
  1739.                                                         if(d && d.data && d.data.jstree && d.data.jstree.icon) {
  1740.                                                                 tmp.icon = d.data.jstree.icon;
  1741.                                                         }
  1742.                                                         if(tmp.icon === undefined || tmp.icon === null || tmp.icon === "") {
  1743.                                                                 tmp.icon = true;
  1744.                                                         }
  1745.                                                         if(d && d.data) {
  1746.                                                                 tmp.data = d.data;
  1747.                                                                 if(d.data.jstree) {
  1748.                                                                         for(i in d.data.jstree) {
  1749.                                                                                 if(d.data.jstree.hasOwnProperty(i)) {
  1750.                                                                                         tmp.state[i] = d.data.jstree[i];
  1751.                                                                                 }
  1752.                                                                         }
  1753.                                                                 }
  1754.                                                         }
  1755.                                                         if(d && typeof d.state === 'object') {
  1756.                                                                 for (i in d.state) {
  1757.                                                                         if(d.state.hasOwnProperty(i)) {
  1758.                                                                                 tmp.state[i] = d.state[i];
  1759.                                                                         }
  1760.                                                                 }
  1761.                                                         }
  1762.                                                         if(d && typeof d.li_attr === 'object') {
  1763.                                                                 for (i in d.li_attr) {
  1764.                                                                         if(d.li_attr.hasOwnProperty(i)) {
  1765.                                                                                 tmp.li_attr[i] = d.li_attr[i];
  1766.                                                                         }
  1767.                                                                 }
  1768.                                                         }
  1769.                                                         if(tmp.li_attr.id && !tmp.id) {
  1770.                                                                 tmp.id = tmp.li_attr.id.toString();
  1771.                                                         }
  1772.                                                         if(!tmp.id) {
  1773.                                                                 tmp.id = tid;
  1774.                                                         }
  1775.                                                         if(!tmp.li_attr.id) {
  1776.                                                                 tmp.li_attr.id = tmp.id;
  1777.                                                         }
  1778.                                                         if(d && typeof d.a_attr === 'object') {
  1779.                                                                 for (i in d.a_attr) {
  1780.                                                                         if(d.a_attr.hasOwnProperty(i)) {
  1781.                                                                                 tmp.a_attr[i] = d.a_attr[i];
  1782.                                                                         }
  1783.                                                                 }
  1784.                                                         }
  1785.                                                         if(d && d.children && d.children.length) {
  1786.                                                                 for(i = 0, j = d.children.length; i < j; i++) {
  1787.                                                                         c = parse_nest(d.children[i], tmp.id, ps);
  1788.                                                                         e = m[c];
  1789.                                                                         tmp.children.push(c);
  1790.                                                                         if(e.children_d.length) {
  1791.                                                                                 tmp.children_d = tmp.children_d.concat(e.children_d);
  1792.                                                                         }
  1793.                                                                 }
  1794.                                                                 tmp.children_d = tmp.children_d.concat(tmp.children);
  1795.                                                         }
  1796.                                                         if(d && d.children && d.children === true) {
  1797.                                                                 tmp.state.loaded = false;
  1798.                                                                 tmp.children = [];
  1799.                                                                 tmp.children_d = [];
  1800.                                                         }
  1801.                                                         delete d.data;
  1802.                                                         delete d.children;
  1803.                                                         tmp.original = d;
  1804.                                                         m[tmp.id] = tmp;
  1805.                                                         if(tmp.state.selected) {
  1806.                                                                 add.push(tmp.id);
  1807.                                                         }
  1808.                                                         return tmp.id;
  1809.                                                 };
  1810.  
  1811.                                         if(dat.length && dat[0].id !== undefined && dat[0].parent !== undefined) {
  1812.                                                 // Flat JSON support (for easy import from DB):
  1813.                                                 // 1) convert to object (foreach)
  1814.                                                 for(i = 0, j = dat.length; i < j; i++) {
  1815.                                                         if(!dat[i].children) {
  1816.                                                                 dat[i].children = [];
  1817.                                                         }
  1818.                                                         if(!dat[i].state) {
  1819.                                                                 dat[i].state = {};
  1820.                                                         }
  1821.                                                         m[dat[i].id.toString()] = dat[i];
  1822.                                                 }
  1823.                                                 // 2) populate children (foreach)
  1824.                                                 for(i = 0, j = dat.length; i < j; i++) {
  1825.                                                         if (!m[dat[i].parent.toString()]) {
  1826.                                                                 this._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() }) };
  1827.                                                                 this.settings.core.error.call(this, this._data.core.last_error);
  1828.                                                                 continue;
  1829.                                                         }
  1830.  
  1831.                                                         m[dat[i].parent.toString()].children.push(dat[i].id.toString());
  1832.                                                         // populate parent.children_d
  1833.                                                         p.children_d.push(dat[i].id.toString());
  1834.                                                 }
  1835.                                                 // 3) normalize && populate parents and children_d with recursion
  1836.                                                 for(i = 0, j = p.children.length; i < j; i++) {
  1837.                                                         tmp = parse_flat(m[p.children[i]], par, p.parents.concat());
  1838.                                                         dpc.push(tmp);
  1839.                                                         if(m[tmp].children_d.length) {
  1840.                                                                 dpc = dpc.concat(m[tmp].children_d);
  1841.                                                         }
  1842.                                                 }
  1843.                                                 for(i = 0, j = p.parents.length; i < j; i++) {
  1844.                                                         m[p.parents[i]].children_d = m[p.parents[i]].children_d.concat(dpc);
  1845.                                                 }
  1846.                                                 // ?) three_state selection - p.state.selected && t - (if three_state foreach(dat => ch) -> foreach(parents) if(parent.selected) child.selected = true;
  1847.                                                 rslt = {
  1848.                                                         'cnt' : t_cnt,
  1849.                                                         'mod' : m,
  1850.                                                         'sel' : sel,
  1851.                                                         'par' : par,
  1852.                                                         'dpc' : dpc,
  1853.                                                         'add' : add
  1854.                                                 };
  1855.                                         }
  1856.                                         else {
  1857.                                                 for(i = 0, j = dat.length; i < j; i++) {
  1858.                                                         tmp = parse_nest(dat[i], par, p.parents.concat());
  1859.                                                         if(tmp) {
  1860.                                                                 chd.push(tmp);
  1861.                                                                 dpc.push(tmp);
  1862.                                                                 if(m[tmp].children_d.length) {
  1863.                                                                         dpc = dpc.concat(m[tmp].children_d);
  1864.                                                                 }
  1865.                                                         }
  1866.                                                 }
  1867.                                                 p.children = chd;
  1868.                                                 p.children_d = dpc;
  1869.                                                 for(i = 0, j = p.parents.length; i < j; i++) {
  1870.                                                         m[p.parents[i]].children_d = m[p.parents[i]].children_d.concat(dpc);
  1871.                                                 }
  1872.                                                 rslt = {
  1873.                                                         'cnt' : t_cnt,
  1874.                                                         'mod' : m,
  1875.                                                         'sel' : sel,
  1876.                                                         'par' : par,
  1877.                                                         'dpc' : dpc,
  1878.                                                         'add' : add
  1879.                                                 };
  1880.                                         }
  1881.                                         if(typeof window === 'undefined' || typeof window.document === 'undefined') {
  1882.                                                 postMessage(rslt);
  1883.                                         }
  1884.                                         else {
  1885.                                                 return rslt;
  1886.                                         }
  1887.                                 },
  1888.                                 rslt = function (rslt, worker) {
  1889.                                         if(this.element === null) { return; }
  1890.                                         this._cnt = rslt.cnt;
  1891.                                         var i, m = this._model.data;
  1892.                                         for (i in m) {
  1893.                                                 if (m.hasOwnProperty(i) && m[i].state && m[i].state.loading && rslt.mod[i]) {
  1894.                                                         rslt.mod[i].state.loading = true;
  1895.                                                 }
  1896.                                         }
  1897.                                         this._model.data = rslt.mod; // breaks the reference in load_node - careful
  1898.  
  1899.                                         if(worker) {
  1900.                                                 var j, a = rslt.add, r = rslt.sel, s = this._data.core.selected.slice();
  1901.                                                 m = this._model.data;
  1902.                                                 // if selection was changed while calculating in worker
  1903.                                                 if(r.length !== s.length || $.vakata.array_unique(r.concat(s)).length !== r.length) {
  1904.                                                         // deselect nodes that are no longer selected
  1905.                                                         for(i = 0, j = r.length; i < j; i++) {
  1906.                                                                 if($.inArray(r[i], a) === -1 && $.inArray(r[i], s) === -1) {
  1907.                                                                         m[r[i]].state.selected = false;
  1908.                                                                 }
  1909.                                                         }
  1910.                                                         // select nodes that were selected in the mean time
  1911.                                                         for(i = 0, j = s.length; i < j; i++) {
  1912.                                                                 if($.inArray(s[i], r) === -1) {
  1913.                                                                         m[s[i]].state.selected = true;
  1914.                                                                 }
  1915.                                                         }
  1916.                                                 }
  1917.                                         }
  1918.                                         if(rslt.add.length) {
  1919.                                                 this._data.core.selected = this._data.core.selected.concat(rslt.add);
  1920.                                         }
  1921.  
  1922.                                         this.trigger('model', { "nodes" : rslt.dpc, 'parent' : rslt.par });
  1923.  
  1924.                                         if(rslt.par !== $.jstree.root) {
  1925.                                                 this._node_changed(rslt.par);
  1926.                                                 this.redraw();
  1927.                                         }
  1928.                                         else {
  1929.                                                 // this.get_container_ul().children('.jstree-initial-node').remove();
  1930.                                                 this.redraw(true);
  1931.                                         }
  1932.                                         if(rslt.add.length) {
  1933.                                                 this.trigger('changed', { 'action' : 'model', 'selected' : this._data.core.selected });
  1934.                                         }
  1935.                                         cb.call(this, true);
  1936.                                 };
  1937.                         if(this.settings.core.worker && window.Blob && window.URL && window.Worker) {
  1938.                                 try {
  1939.                                         if(this._wrk === null) {
  1940.                                                 this._wrk = window.URL.createObjectURL(
  1941.                                                         new window.Blob(
  1942.                                                                 ['self.onmessage = ' + func.toString()],
  1943.                                                                 {type:"text/javascript"}
  1944.                                                         )
  1945.                                                 );
  1946.                                         }
  1947.                                         if(!this._data.core.working || force_processing) {
  1948.                                                 this._data.core.working = true;
  1949.                                                 w = new window.Worker(this._wrk);
  1950.                                                 w.onmessage = $.proxy(function (e) {
  1951.                                                         rslt.call(this, e.data, true);
  1952.                                                         try { w.terminate(); w = null; } catch(ignore) { }
  1953.                                                         if(this._data.core.worker_queue.length) {
  1954.                                                                 this._append_json_data.apply(this, this._data.core.worker_queue.shift());
  1955.                                                         }
  1956.                                                         else {
  1957.                                                                 this._data.core.working = false;
  1958.                                                         }
  1959.                                                 }, this);
  1960.                                                 if(!args.par) {
  1961.                                                         if(this._data.core.worker_queue.length) {
  1962.                                                                 this._append_json_data.apply(this, this._data.core.worker_queue.shift());
  1963.                                                         }
  1964.                                                         else {
  1965.                                                                 this._data.core.working = false;
  1966.                                                         }
  1967.                                                 }
  1968.                                                 else {
  1969.                                                         w.postMessage(args);
  1970.                                                 }
  1971.                                         }
  1972.                                         else {
  1973.                                                 this._data.core.worker_queue.push([dom, data, cb, true]);
  1974.                                         }
  1975.                                 }
  1976.                                 catch(e) {
  1977.                                         rslt.call(this, func(args), false);
  1978.                                         if(this._data.core.worker_queue.length) {
  1979.                                                 this._append_json_data.apply(this, this._data.core.worker_queue.shift());
  1980.                                         }
  1981.                                         else {
  1982.                                                 this._data.core.working = false;
  1983.                                         }
  1984.                                 }
  1985.                         }
  1986.                         else {
  1987.                                 rslt.call(this, func(args), false);
  1988.                         }
  1989.                 },
  1990.                 /**
  1991.                  * parses a node from a jQuery object and appends them to the in memory tree model. Used internally.
  1992.                  * @private
  1993.                  * @name _parse_model_from_html(d [, p, ps])
  1994.                  * @param  {jQuery} d the jQuery object to parse
  1995.                  * @param  {String} p the parent ID
  1996.                  * @param  {Array} ps list of all parents
  1997.                  * @return {String} the ID of the object added to the model
  1998.                  */
  1999.                 _parse_model_from_html : function (d, p, ps) {
  2000.                         if(!ps) { ps = []; }
  2001.                         else { ps = [].concat(ps); }
  2002.                         if(p) { ps.unshift(p); }
  2003.                         var c, e, m = this._model.data,
  2004.                                 data = {
  2005.                                         id                      : false,
  2006.                                         text            : false,
  2007.                                         icon            : true,
  2008.                                         parent          : p,
  2009.                                         parents         : ps,
  2010.                                         children        : [],
  2011.                                         children_d      : [],
  2012.                                         data            : null,
  2013.                                         state           : { },
  2014.                                         li_attr         : { id : false },
  2015.                                         a_attr          : { href : '#' },
  2016.                                         original        : false
  2017.                                 }, i, tmp, tid;
  2018.                         for(i in this._model.default_state) {
  2019.                                 if(this._model.default_state.hasOwnProperty(i)) {
  2020.                                         data.state[i] = this._model.default_state[i];
  2021.                                 }
  2022.                         }
  2023.                         tmp = $.vakata.attributes(d, true);
  2024.                         $.each(tmp, function (i, v) {
  2025.                                 v = $.trim(v);
  2026.                                 if(!v.length) { return true; }
  2027.                                 data.li_attr[i] = v;
  2028.                                 if(i === 'id') {
  2029.                                         data.id = v.toString();
  2030.                                 }
  2031.                         });
  2032.                         tmp = d.children('a').first();
  2033.                         if(tmp.length) {
  2034.                                 tmp = $.vakata.attributes(tmp, true);
  2035.                                 $.each(tmp, function (i, v) {
  2036.                                         v = $.trim(v);
  2037.                                         if(v.length) {
  2038.                                                 data.a_attr[i] = v;
  2039.                                         }
  2040.                                 });
  2041.                         }
  2042.                         tmp = d.children("a").first().length ? d.children("a").first().clone() : d.clone();
  2043.                         tmp.children("ins, i, ul").remove();
  2044.                         tmp = tmp.html();
  2045.                         tmp = $('<div />').html(tmp);
  2046.                         data.text = this.settings.core.force_text ? tmp.text() : tmp.html();
  2047.                         tmp = d.data();
  2048.                         data.data = tmp ? $.extend(true, {}, tmp) : null;
  2049.                         data.state.opened = d.hasClass('jstree-open');
  2050.                         data.state.selected = d.children('a').hasClass('jstree-clicked');
  2051.                         data.state.disabled = d.children('a').hasClass('jstree-disabled');
  2052.                         if(data.data && data.data.jstree) {
  2053.                                 for(i in data.data.jstree) {
  2054.                                         if(data.data.jstree.hasOwnProperty(i)) {
  2055.                                                 data.state[i] = data.data.jstree[i];
  2056.                                         }
  2057.                                 }
  2058.                         }
  2059.                         tmp = d.children("a").children(".jstree-themeicon");
  2060.                         if(tmp.length) {
  2061.                                 data.icon = tmp.hasClass('jstree-themeicon-hidden') ? false : tmp.attr('rel');
  2062.                         }
  2063.                         if(data.state.icon !== undefined) {
  2064.                                 data.icon = data.state.icon;
  2065.                         }
  2066.                         if(data.icon === undefined || data.icon === null || data.icon === "") {
  2067.                                 data.icon = true;
  2068.                         }
  2069.                         tmp = d.children("ul").children("li");
  2070.                         do {
  2071.                                 tid = 'j' + this._id + '_' + (++this._cnt);
  2072.                         } while(m[tid]);
  2073.                         data.id = data.li_attr.id ? data.li_attr.id.toString() : tid;
  2074.                         if(tmp.length) {
  2075.                                 tmp.each($.proxy(function (i, v) {
  2076.                                         c = this._parse_model_from_html($(v), data.id, ps);
  2077.                                         e = this._model.data[c];
  2078.                                         data.children.push(c);
  2079.                                         if(e.children_d.length) {
  2080.                                                 data.children_d = data.children_d.concat(e.children_d);
  2081.                                         }
  2082.                                 }, this));
  2083.                                 data.children_d = data.children_d.concat(data.children);
  2084.                         }
  2085.                         else {
  2086.                                 if(d.hasClass('jstree-closed')) {
  2087.                                         data.state.loaded = false;
  2088.                                 }
  2089.                         }
  2090.                         if(data.li_attr['class']) {
  2091.                                 data.li_attr['class'] = data.li_attr['class'].replace('jstree-closed','').replace('jstree-open','');
  2092.                         }
  2093.                         if(data.a_attr['class']) {
  2094.                                 data.a_attr['class'] = data.a_attr['class'].replace('jstree-clicked','').replace('jstree-disabled','');
  2095.                         }
  2096.                         m[data.id] = data;
  2097.                         if(data.state.selected) {
  2098.                                 this._data.core.selected.push(data.id);
  2099.                         }
  2100.                         return data.id;
  2101.                 },
  2102.                 /**
  2103.                  * 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.
  2104.                  * @private
  2105.                  * @name _parse_model_from_flat_json(d [, p, ps])
  2106.                  * @param  {Object} d the JSON object to parse
  2107.                  * @param  {String} p the parent ID
  2108.                  * @param  {Array} ps list of all parents
  2109.                  * @return {String} the ID of the object added to the model
  2110.                  */
  2111.                 _parse_model_from_flat_json : function (d, p, ps) {
  2112.                         if(!ps) { ps = []; }
  2113.                         else { ps = ps.concat(); }
  2114.                         if(p) { ps.unshift(p); }
  2115.                         var tid = d.id.toString(),
  2116.                                 m = this._model.data,
  2117.                                 df = this._model.default_state,
  2118.                                 i, j, c, e,
  2119.                                 tmp = {
  2120.                                         id                      : tid,
  2121.                                         text            : d.text || '',
  2122.                                         icon            : d.icon !== undefined ? d.icon : true,
  2123.                                         parent          : p,
  2124.                                         parents         : ps,
  2125.                                         children        : d.children || [],
  2126.                                         children_d      : d.children_d || [],
  2127.                                         data            : d.data,
  2128.                                         state           : { },
  2129.                                         li_attr         : { id : false },
  2130.                                         a_attr          : { href : '#' },
  2131.                                         original        : false
  2132.                                 };
  2133.                         for(i in df) {
  2134.                                 if(df.hasOwnProperty(i)) {
  2135.                                         tmp.state[i] = df[i];
  2136.                                 }
  2137.                         }
  2138.                         if(d && d.data && d.data.jstree && d.data.jstree.icon) {
  2139.                                 tmp.icon = d.data.jstree.icon;
  2140.                         }
  2141.                         if(tmp.icon === undefined || tmp.icon === null || tmp.icon === "") {
  2142.                                 tmp.icon = true;
  2143.                         }
  2144.                         if(d && d.data) {
  2145.                                 tmp.data = d.data;
  2146.                                 if(d.data.jstree) {
  2147.                                         for(i in d.data.jstree) {
  2148.                                                 if(d.data.jstree.hasOwnProperty(i)) {
  2149.                                                         tmp.state[i] = d.data.jstree[i];
  2150.                                                 }
  2151.                                         }
  2152.                                 }
  2153.                         }
  2154.                         if(d && typeof d.state === 'object') {
  2155.                                 for (i in d.state) {
  2156.                                         if(d.state.hasOwnProperty(i)) {
  2157.                                                 tmp.state[i] = d.state[i];
  2158.                                         }
  2159.                                 }
  2160.                         }
  2161.                         if(d && typeof d.li_attr === 'object') {
  2162.                                 for (i in d.li_attr) {
  2163.                                         if(d.li_attr.hasOwnProperty(i)) {
  2164.                                                 tmp.li_attr[i] = d.li_attr[i];
  2165.                                         }
  2166.                                 }
  2167.                         }
  2168.                         if(!tmp.li_attr.id) {
  2169.                                 tmp.li_attr.id = tid;
  2170.                         }
  2171.                         if(d && typeof d.a_attr === 'object') {
  2172.                                 for (i in d.a_attr) {
  2173.                                         if(d.a_attr.hasOwnProperty(i)) {
  2174.                                                 tmp.a_attr[i] = d.a_attr[i];
  2175.                                         }
  2176.                                 }
  2177.                         }
  2178.                         if(d && d.children && d.children === true) {
  2179.                                 tmp.state.loaded = false;
  2180.                                 tmp.children = [];
  2181.                                 tmp.children_d = [];
  2182.                         }
  2183.                         m[tmp.id] = tmp;
  2184.                         for(i = 0, j = tmp.children.length; i < j; i++) {
  2185.                                 c = this._parse_model_from_flat_json(m[tmp.children[i]], tmp.id, ps);
  2186.                                 e = m[c];
  2187.                                 tmp.children_d.push(c);
  2188.                                 if(e.children_d.length) {
  2189.                                         tmp.children_d = tmp.children_d.concat(e.children_d);
  2190.                                 }
  2191.                         }
  2192.                         delete d.data;
  2193.                         delete d.children;
  2194.                         m[tmp.id].original = d;
  2195.                         if(tmp.state.selected) {
  2196.                                 this._data.core.selected.push(tmp.id);
  2197.                         }
  2198.                         return tmp.id;
  2199.                 },
  2200.                 /**
  2201.                  * parses a node from a JSON object and appends it to the in memory tree model. Used internally.
  2202.                  * @private
  2203.                  * @name _parse_model_from_json(d [, p, ps])
  2204.                  * @param  {Object} d the JSON object to parse
  2205.                  * @param  {String} p the parent ID
  2206.                  * @param  {Array} ps list of all parents
  2207.                  * @return {String} the ID of the object added to the model
  2208.                  */
  2209.                 _parse_model_from_json : function (d, p, ps) {
  2210.                         if(!ps) { ps = []; }
  2211.                         else { ps = ps.concat(); }
  2212.                         if(p) { ps.unshift(p); }
  2213.                         var tid = false, i, j, c, e, m = this._model.data, df = this._model.default_state, tmp;
  2214.                         do {
  2215.                                 tid = 'j' + this._id + '_' + (++this._cnt);
  2216.                         } while(m[tid]);
  2217.  
  2218.                         tmp = {
  2219.                                 id                      : false,
  2220.                                 text            : typeof d === 'string' ? d : '',
  2221.                                 icon            : typeof d === 'object' && d.icon !== undefined ? d.icon : true,
  2222.                                 parent          : p,
  2223.                                 parents         : ps,
  2224.                                 children        : [],
  2225.                                 children_d      : [],
  2226.                                 data            : null,
  2227.                                 state           : { },
  2228.                                 li_attr         : { id : false },
  2229.                                 a_attr          : { href : '#' },
  2230.                                 original        : false
  2231.                         };
  2232.                         for(i in df) {
  2233.                                 if(df.hasOwnProperty(i)) {
  2234.                                         tmp.state[i] = df[i];
  2235.                                 }
  2236.                         }
  2237.                         if(d && d.id) { tmp.id = d.id.toString(); }
  2238.                         if(d && d.text) { tmp.text = d.text; }
  2239.                         if(d && d.data && d.data.jstree && d.data.jstree.icon) {
  2240.                                 tmp.icon = d.data.jstree.icon;
  2241.                         }
  2242.                         if(tmp.icon === undefined || tmp.icon === null || tmp.icon === "") {
  2243.                                 tmp.icon = true;
  2244.                         }
  2245.                         if(d && d.data) {
  2246.                                 tmp.data = d.data;
  2247.                                 if(d.data.jstree) {
  2248.                                         for(i in d.data.jstree) {
  2249.                                                 if(d.data.jstree.hasOwnProperty(i)) {
  2250.                                                         tmp.state[i] = d.data.jstree[i];
  2251.                                                 }
  2252.                                         }
  2253.                                 }
  2254.                         }
  2255.                         if(d && typeof d.state === 'object') {
  2256.                                 for (i in d.state) {
  2257.                                         if(d.state.hasOwnProperty(i)) {
  2258.                                                 tmp.state[i] = d.state[i];
  2259.                                         }
  2260.                                 }
  2261.                         }
  2262.                         if(d && typeof d.li_attr === 'object') {
  2263.                                 for (i in d.li_attr) {
  2264.                                         if(d.li_attr.hasOwnProperty(i)) {
  2265.                                                 tmp.li_attr[i] = d.li_attr[i];
  2266.                                         }
  2267.                                 }
  2268.                         }
  2269.                         if(tmp.li_attr.id && !tmp.id) {
  2270.                                 tmp.id = tmp.li_attr.id.toString();
  2271.                         }
  2272.                         if(!tmp.id) {
  2273.                                 tmp.id = tid;
  2274.                         }
  2275.                         if(!tmp.li_attr.id) {
  2276.                                 tmp.li_attr.id = tmp.id;
  2277.                         }
  2278.                         if(d && typeof d.a_attr === 'object') {
  2279.                                 for (i in d.a_attr) {
  2280.                                         if(d.a_attr.hasOwnProperty(i)) {
  2281.                                                 tmp.a_attr[i] = d.a_attr[i];
  2282.                                         }
  2283.                                 }
  2284.                         }
  2285.                         if(d && d.children && d.children.length) {
  2286.                                 for(i = 0, j = d.children.length; i < j; i++) {
  2287.                                         c = this._parse_model_from_json(d.children[i], tmp.id, ps);
  2288.                                         e = m[c];
  2289.                                         tmp.children.push(c);
  2290.                                         if(e.children_d.length) {
  2291.                                                 tmp.children_d = tmp.children_d.concat(e.children_d);
  2292.                                         }
  2293.                                 }
  2294.                                 tmp.children_d = tmp.children_d.concat(tmp.children);
  2295.                         }
  2296.                         if(d && d.children && d.children === true) {
  2297.                                 tmp.state.loaded = false;
  2298.                                 tmp.children = [];
  2299.                                 tmp.children_d = [];
  2300.                         }
  2301.                         delete d.data;
  2302.                         delete d.children;
  2303.                         tmp.original = d;
  2304.                         m[tmp.id] = tmp;
  2305.                         if(tmp.state.selected) {
  2306.                                 this._data.core.selected.push(tmp.id);
  2307.                         }
  2308.                         return tmp.id;
  2309.                 },
  2310.                 /**
  2311.                  * redraws all nodes that need to be redrawn. Used internally.
  2312.                  * @private
  2313.                  * @name _redraw()
  2314.                  * @trigger redraw.jstree
  2315.                  */
  2316.                 _redraw : function () {
  2317.                         var nodes = this._model.force_full_redraw ? this._model.data[$.jstree.root].children.concat([]) : this._model.changed.concat([]),
  2318.                                 f = document.createElement('UL'), tmp, i, j, fe = this._data.core.focused;
  2319.                         for(i = 0, j = nodes.length; i < j; i++) {
  2320.                                 tmp = this.redraw_node(nodes[i], true, this._model.force_full_redraw);
  2321.                                 if(tmp && this._model.force_full_redraw) {
  2322.                                         f.appendChild(tmp);
  2323.                                 }
  2324.                         }
  2325.                         if(this._model.force_full_redraw) {
  2326.                                 f.className = this.get_container_ul()[0].className;
  2327.                                 f.setAttribute('role','group');
  2328.                                 this.element.empty().append(f);
  2329.                                 //this.get_container_ul()[0].appendChild(f);
  2330.                         }
  2331.                         if(fe !== null && this.settings.core.restore_focus) {
  2332.                                 tmp = this.get_node(fe, true);
  2333.                                 if(tmp && tmp.length && tmp.children('.jstree-anchor')[0] !== document.activeElement) {
  2334.                                         tmp.children('.jstree-anchor').focus();
  2335.                                 }
  2336.                                 else {
  2337.                                         this._data.core.focused = null;
  2338.                                 }
  2339.                         }
  2340.                         this._model.force_full_redraw = false;
  2341.                         this._model.changed = [];
  2342.                         /**
  2343.                          * triggered after nodes are redrawn
  2344.                          * @event
  2345.                          * @name redraw.jstree
  2346.                          * @param {array} nodes the redrawn nodes
  2347.                          */
  2348.                         this.trigger('redraw', { "nodes" : nodes });
  2349.                 },
  2350.                 /**
  2351.                  * redraws all nodes that need to be redrawn or optionally - the whole tree
  2352.                  * @name redraw([full])
  2353.                  * @param {Boolean} full if set to `true` all nodes are redrawn.
  2354.                  */
  2355.                 redraw : function (full) {
  2356.                         if(full) {
  2357.                                 this._model.force_full_redraw = true;
  2358.                         }
  2359.                         //if(this._model.redraw_timeout) {
  2360.                         //      clearTimeout(this._model.redraw_timeout);
  2361.                         //}
  2362.                         //this._model.redraw_timeout = setTimeout($.proxy(this._redraw, this),0);
  2363.                         this._redraw();
  2364.                 },
  2365.                 /**
  2366.                  * redraws a single node's children. Used internally.
  2367.                  * @private
  2368.                  * @name draw_children(node)
  2369.                  * @param {mixed} node the node whose children will be redrawn
  2370.                  */
  2371.                 draw_children : function (node) {
  2372.                         var obj = this.get_node(node),
  2373.                                 i = false,
  2374.                                 j = false,
  2375.                                 k = false,
  2376.                                 d = document;
  2377.                         if(!obj) { return false; }
  2378.                         if(obj.id === $.jstree.root) { return this.redraw(true); }
  2379.                         node = this.get_node(node, true);
  2380.                         if(!node || !node.length) { return false; } // TODO: quick toggle
  2381.  
  2382.                         node.children('.jstree-children').remove();
  2383.                         node = node[0];
  2384.                         if(obj.children.length && obj.state.loaded) {
  2385.                                 k = d.createElement('UL');
  2386.                                 k.setAttribute('role', 'group');
  2387.                                 k.className = 'jstree-children';
  2388.                                 for(i = 0, j = obj.children.length; i < j; i++) {
  2389.                                         k.appendChild(this.redraw_node(obj.children[i], true, true));
  2390.                                 }
  2391.                                 node.appendChild(k);
  2392.                         }
  2393.                 },
  2394.                 /**
  2395.                  * redraws a single node. Used internally.
  2396.                  * @private
  2397.                  * @name redraw_node(node, deep, is_callback, force_render)
  2398.                  * @param {mixed} node the node to redraw
  2399.                  * @param {Boolean} deep should child nodes be redrawn too
  2400.                  * @param {Boolean} is_callback is this a recursion call
  2401.                  * @param {Boolean} force_render should children of closed parents be drawn anyway
  2402.                  */
  2403.                 redraw_node : function (node, deep, is_callback, force_render) {
  2404.                         var obj = this.get_node(node),
  2405.                                 par = false,
  2406.                                 ind = false,
  2407.                                 old = false,
  2408.                                 i = false,
  2409.                                 j = false,
  2410.                                 k = false,
  2411.                                 c = '',
  2412.                                 d = document,
  2413.                                 m = this._model.data,
  2414.                                 f = false,
  2415.                                 s = false,
  2416.                                 tmp = null,
  2417.                                 t = 0,
  2418.                                 l = 0,
  2419.                                 has_children = false,
  2420.                                 last_sibling = false;
  2421.                         if(!obj) { return false; }
  2422.                         if(obj.id === $.jstree.root) {  return this.redraw(true); }
  2423.                         deep = deep || obj.children.length === 0;
  2424.                         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);
  2425.                         if(!node) {
  2426.                                 deep = true;
  2427.                                 //node = d.createElement('LI');
  2428.                                 if(!is_callback) {
  2429.                                         par = obj.parent !== $.jstree.root ? $('#' + obj.parent.replace($.jstree.idregex,'\\$&'), this.element)[0] : null;
  2430.                                         if(par !== null && (!par || !m[obj.parent].state.opened)) {
  2431.                                                 return false;
  2432.                                         }
  2433.                                         ind = $.inArray(obj.id, par === null ? m[$.jstree.root].children : m[obj.parent].children);
  2434.                                 }
  2435.                         }
  2436.                         else {
  2437.                                 node = $(node);
  2438.                                 if(!is_callback) {
  2439.                                         par = node.parent().parent()[0];
  2440.                                         if(par === this.element[0]) {
  2441.                                                 par = null;
  2442.                                         }
  2443.                                         ind = node.index();
  2444.                                 }
  2445.                                 // m[obj.id].data = node.data(); // use only node's data, no need to touch jquery storage
  2446.                                 if(!deep && obj.children.length && !node.children('.jstree-children').length) {
  2447.                                         deep = true;
  2448.                                 }
  2449.                                 if(!deep) {
  2450.                                         old = node.children('.jstree-children')[0];
  2451.                                 }
  2452.                                 f = node.children('.jstree-anchor')[0] === document.activeElement;
  2453.                                 node.remove();
  2454.                                 //node = d.createElement('LI');
  2455.                                 //node = node[0];
  2456.                         }
  2457.                         node = this._data.core.node.cloneNode(true);
  2458.                         // node is DOM, deep is boolean
  2459.  
  2460.                         c = 'jstree-node ';
  2461.                         for(i in obj.li_attr) {
  2462.                                 if(obj.li_attr.hasOwnProperty(i)) {
  2463.                                         if(i === 'id') { continue; }
  2464.                                         if(i !== 'class') {
  2465.                                                 node.setAttribute(i, obj.li_attr[i]);
  2466.                                         }
  2467.                                         else {
  2468.                                                 c += obj.li_attr[i];
  2469.                                         }
  2470.                                 }
  2471.                         }
  2472.                         if(!obj.a_attr.id) {
  2473.                                 obj.a_attr.id = obj.id + '_anchor';
  2474.                         }
  2475.                         node.setAttribute('aria-selected', !!obj.state.selected);
  2476.                         node.setAttribute('aria-level', obj.parents.length);
  2477.                         node.setAttribute('aria-labelledby', obj.a_attr.id);
  2478.                         if(obj.state.disabled) {
  2479.                                 node.setAttribute('aria-disabled', true);
  2480.                         }
  2481.  
  2482.                         for(i = 0, j = obj.children.length; i < j; i++) {
  2483.                                 if(!m[obj.children[i]].state.hidden) {
  2484.                                         has_children = true;
  2485.                                         break;
  2486.                                 }
  2487.                         }
  2488.                         if(obj.parent !== null && m[obj.parent] && !obj.state.hidden) {
  2489.                                 i = $.inArray(obj.id, m[obj.parent].children);
  2490.                                 last_sibling = obj.id;
  2491.                                 if(i !== -1) {
  2492.                                         i++;
  2493.                                         for(j = m[obj.parent].children.length; i < j; i++) {
  2494.                                                 if(!m[m[obj.parent].children[i]].state.hidden) {
  2495.                                                         last_sibling = m[obj.parent].children[i];
  2496.                                                 }
  2497.                                                 if(last_sibling !== obj.id) {
  2498.                                                         break;
  2499.                                                 }
  2500.                                         }
  2501.                                 }
  2502.                         }
  2503.  
  2504.                         if(obj.state.hidden) {
  2505.                                 c += ' jstree-hidden';
  2506.                         }
  2507.                         if (obj.state.loading) {
  2508.                                 c += ' jstree-loading';
  2509.                         }
  2510.                         if(obj.state.loaded && !has_children) {
  2511.                                 c += ' jstree-leaf';
  2512.                         }
  2513.                         else {
  2514.                                 c += obj.state.opened && obj.state.loaded ? ' jstree-open' : ' jstree-closed';
  2515.                                 node.setAttribute('aria-expanded', (obj.state.opened && obj.state.loaded) );
  2516.                         }
  2517.                         if(last_sibling === obj.id) {
  2518.                                 c += ' jstree-last';
  2519.                         }
  2520.                         node.id = obj.id;
  2521.                         node.className = c;
  2522.                         c = ( obj.state.selected ? ' jstree-clicked' : '') + ( obj.state.disabled ? ' jstree-disabled' : '');
  2523.                         for(j in obj.a_attr) {
  2524.                                 if(obj.a_attr.hasOwnProperty(j)) {
  2525.                                         if(j === 'href' && obj.a_attr[j] === '#') { continue; }
  2526.                                         if(j !== 'class') {
  2527.                                                 node.childNodes[1].setAttribute(j, obj.a_attr[j]);
  2528.                                         }
  2529.                                         else {
  2530.                                                 c += ' ' + obj.a_attr[j];
  2531.                                         }
  2532.                                 }
  2533.                         }
  2534.                         if(c.length) {
  2535.                                 node.childNodes[1].className = 'jstree-anchor ' + c;
  2536.                         }
  2537.                         if((obj.icon && obj.icon !== true) || obj.icon === false) {
  2538.                                 if(obj.icon === false) {
  2539.                                         node.childNodes[1].childNodes[0].className += ' jstree-themeicon-hidden';
  2540.                                 }
  2541.                                 else if(obj.icon.indexOf('/') === -1 && obj.icon.indexOf('.') === -1) {
  2542.                                         node.childNodes[1].childNodes[0].className += ' ' + obj.icon + ' jstree-themeicon-custom';
  2543.                                 }
  2544.                                 else {
  2545.                                         node.childNodes[1].childNodes[0].style.backgroundImage = 'url("'+obj.icon+'")';
  2546.                                         node.childNodes[1].childNodes[0].style.backgroundPosition = 'center center';
  2547.                                         node.childNodes[1].childNodes[0].style.backgroundSize = 'auto';
  2548.                                         node.childNodes[1].childNodes[0].className += ' jstree-themeicon-custom';
  2549.                                 }
  2550.                         }
  2551.  
  2552.                         if(this.settings.core.force_text) {
  2553.                                 node.childNodes[1].appendChild(d.createTextNode(obj.text));
  2554.                         }
  2555.                         else {
  2556.                                 node.childNodes[1].innerHTML += obj.text;
  2557.                         }
  2558.  
  2559.  
  2560.                         if(deep && obj.children.length && (obj.state.opened || force_render) && obj.state.loaded) {
  2561.                                 k = d.createElement('UL');
  2562.                                 k.setAttribute('role', 'group');
  2563.                                 k.className = 'jstree-children';
  2564.                                 for(i = 0, j = obj.children.length; i < j; i++) {
  2565.                                         k.appendChild(this.redraw_node(obj.children[i], deep, true));
  2566.                                 }
  2567.                                 node.appendChild(k);
  2568.                         }
  2569.                         if(old) {
  2570.                                 node.appendChild(old);
  2571.                         }
  2572.                         if(!is_callback) {
  2573.                                 // append back using par / ind
  2574.                                 if(!par) {
  2575.                                         par = this.element[0];
  2576.                                 }
  2577.                                 for(i = 0, j = par.childNodes.length; i < j; i++) {
  2578.                                         if(par.childNodes[i] && par.childNodes[i].className && par.childNodes[i].className.indexOf('jstree-children') !== -1) {
  2579.                                                 tmp = par.childNodes[i];
  2580.                                                 break;
  2581.                                         }
  2582.                                 }
  2583.                                 if(!tmp) {
  2584.                                         tmp = d.createElement('UL');
  2585.                                         tmp.setAttribute('role', 'group');
  2586.                                         tmp.className = 'jstree-children';
  2587.                                         par.appendChild(tmp);
  2588.                                 }
  2589.                                 par = tmp;
  2590.  
  2591.                                 if(ind < par.childNodes.length) {
  2592.                                         par.insertBefore(node, par.childNodes[ind]);
  2593.                                 }
  2594.                                 else {
  2595.                                         par.appendChild(node);
  2596.                                 }
  2597.                                 if(f) {
  2598.                                         t = this.element[0].scrollTop;
  2599.                                         l = this.element[0].scrollLeft;
  2600.                                         node.childNodes[1].focus();
  2601.                                         this.element[0].scrollTop = t;
  2602.                                         this.element[0].scrollLeft = l;
  2603.                                 }
  2604.                         }
  2605.                         if(obj.state.opened && !obj.state.loaded) {
  2606.                                 obj.state.opened = false;
  2607.                                 setTimeout($.proxy(function () {
  2608.                                         this.open_node(obj.id, false, 0);
  2609.                                 }, this), 0);
  2610.                         }
  2611.                         return node;
  2612.                 },
  2613.                 /**
  2614.                  * opens a node, revealing its children. If the node is not loaded it will be loaded and opened once ready.
  2615.                  * @name open_node(obj [, callback, animation])
  2616.                  * @param {mixed} obj the node to open
  2617.                  * @param {Function} callback a function to execute once the node is opened
  2618.                  * @param {Number} animation the animation duration in milliseconds when opening the node (overrides the `core.animation` setting). Use `false` for no animation.
  2619.                  * @trigger open_node.jstree, after_open.jstree, before_open.jstree
  2620.                  */
  2621.                 open_node : function (obj, callback, animation) {
  2622.                         var t1, t2, d, t;
  2623.                         if($.isArray(obj)) {
  2624.                                 obj = obj.slice();
  2625.                                 for(t1 = 0, t2 = obj.length; t1 < t2; t1++) {
  2626.                                         this.open_node(obj[t1], callback, animation);
  2627.                                 }
  2628.                                 return true;
  2629.                         }
  2630.                         obj = this.get_node(obj);
  2631.                         if(!obj || obj.id === $.jstree.root) {
  2632.                                 return false;
  2633.                         }
  2634.                         animation = animation === undefined ? this.settings.core.animation : animation;
  2635.                         if(!this.is_closed(obj)) {
  2636.                                 if(callback) {
  2637.                                         callback.call(this, obj, false);
  2638.                                 }
  2639.                                 return false;
  2640.                         }
  2641.                         if(!this.is_loaded(obj)) {
  2642.                                 if(this.is_loading(obj)) {
  2643.                                         return setTimeout($.proxy(function () {
  2644.                                                 this.open_node(obj, callback, animation);
  2645.                                         }, this), 500);
  2646.                                 }
  2647.                                 this.load_node(obj, function (o, ok) {
  2648.                                         return ok ? this.open_node(o, callback, animation) : (callback ? callback.call(this, o, false) : false);
  2649.                                 });
  2650.                         }
  2651.                         else {
  2652.                                 d = this.get_node(obj, true);
  2653.                                 t = this;
  2654.                                 if(d.length) {
  2655.                                         if(animation && d.children(".jstree-children").length) {
  2656.                                                 d.children(".jstree-children").stop(true, true);
  2657.                                         }
  2658.                                         if(obj.children.length && !this._firstChild(d.children('.jstree-children')[0])) {
  2659.                                                 this.draw_children(obj);
  2660.                                                 //d = this.get_node(obj, true);
  2661.                                         }
  2662.                                         if(!animation) {
  2663.                                                 this.trigger('before_open', { "node" : obj });
  2664.                                                 d[0].className = d[0].className.replace('jstree-closed', 'jstree-open');
  2665.                                                 d[0].setAttribute("aria-expanded", true);
  2666.                                         }
  2667.                                         else {
  2668.                                                 this.trigger('before_open', { "node" : obj });
  2669.                                                 d
  2670.                                                         .children(".jstree-children").css("display","none").end()
  2671.                                                         .removeClass("jstree-closed").addClass("jstree-open").attr("aria-expanded", true)
  2672.                                                         .children(".jstree-children").stop(true, true)
  2673.                                                                 .slideDown(animation, function () {
  2674.                                                                         this.style.display = "";
  2675.                                                                         if (t.element) {
  2676.                                                                                 t.trigger("after_open", { "node" : obj });
  2677.                                                                         }
  2678.                                                                 });
  2679.                                         }
  2680.                                 }
  2681.                                 obj.state.opened = true;
  2682.                                 if(callback) {
  2683.                                         callback.call(this, obj, true);
  2684.                                 }
  2685.                                 if(!d.length) {
  2686.                                         /**
  2687.                                          * 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)
  2688.                                          * @event
  2689.                                          * @name before_open.jstree
  2690.                                          * @param {Object} node the opened node
  2691.                                          */
  2692.                                         this.trigger('before_open', { "node" : obj });
  2693.                                 }
  2694.                                 /**
  2695.                                  * triggered when a node is opened (if there is an animation it will not be completed yet)
  2696.                                  * @event
  2697.                                  * @name open_node.jstree
  2698.                                  * @param {Object} node the opened node
  2699.                                  */
  2700.                                 this.trigger('open_node', { "node" : obj });
  2701.                                 if(!animation || !d.length) {
  2702.                                         /**
  2703.                                          * triggered when a node is opened and the animation is complete
  2704.                                          * @event
  2705.                                          * @name after_open.jstree
  2706.                                          * @param {Object} node the opened node
  2707.                                          */
  2708.                                         this.trigger("after_open", { "node" : obj });
  2709.                                 }
  2710.                                 return true;
  2711.                         }
  2712.                 },
  2713.                 /**
  2714.                  * opens every parent of a node (node should be loaded)
  2715.                  * @name _open_to(obj)
  2716.                  * @param {mixed} obj the node to reveal
  2717.                  * @private
  2718.                  */
  2719.                 _open_to : function (obj) {
  2720.                         obj = this.get_node(obj);
  2721.                         if(!obj || obj.id === $.jstree.root) {
  2722.                                 return false;
  2723.                         }
  2724.                         var i, j, p = obj.parents;
  2725.                         for(i = 0, j = p.length; i < j; i+=1) {
  2726.                                 if(i !== $.jstree.root) {
  2727.                                         this.open_node(p[i], false, 0);
  2728.                                 }
  2729.                         }
  2730.                         return $('#' + obj.id.replace($.jstree.idregex,'\\$&'), this.element);
  2731.                 },
  2732.                 /**
  2733.                  * closes a node, hiding its children
  2734.                  * @name close_node(obj [, animation])
  2735.                  * @param {mixed} obj the node to close
  2736.                  * @param {Number} animation the animation duration in milliseconds when closing the node (overrides the `core.animation` setting). Use `false` for no animation.
  2737.                  * @trigger close_node.jstree, after_close.jstree
  2738.                  */
  2739.                 close_node : function (obj, animation) {
  2740.                         var t1, t2, t, d;
  2741.                         if($.isArray(obj)) {
  2742.                                 obj = obj.slice();
  2743.                                 for(t1 = 0, t2 = obj.length; t1 < t2; t1++) {
  2744.                                         this.close_node(obj[t1], animation);
  2745.                                 }
  2746.                                 return true;
  2747.                         }
  2748.                         obj = this.get_node(obj);
  2749.                         if(!obj || obj.id === $.jstree.root) {
  2750.                                 return false;
  2751.                         }
  2752.                         if(this.is_closed(obj)) {
  2753.                                 return false;
  2754.                         }
  2755.                         animation = animation === undefined ? this.settings.core.animation : animation;
  2756.                         t = this;
  2757.                         d = this.get_node(obj, true);
  2758.  
  2759.                         obj.state.opened = false;
  2760.                         /**
  2761.                          * triggered when a node is closed (if there is an animation it will not be complete yet)
  2762.                          * @event
  2763.                          * @name close_node.jstree
  2764.                          * @param {Object} node the closed node
  2765.                          */
  2766.                         this.trigger('close_node',{ "node" : obj });
  2767.                         if(!d.length) {
  2768.                                 /**
  2769.                                  * triggered when a node is closed and the animation is complete
  2770.                                  * @event
  2771.                                  * @name after_close.jstree
  2772.                                  * @param {Object} node the closed node
  2773.                                  */
  2774.                                 this.trigger("after_close", { "node" : obj });
  2775.                         }
  2776.                         else {
  2777.                                 if(!animation) {
  2778.                                         d[0].className = d[0].className.replace('jstree-open', 'jstree-closed');
  2779.                                         d.attr("aria-expanded", false).children('.jstree-children').remove();
  2780.                                         this.trigger("after_close", { "node" : obj });
  2781.                                 }
  2782.                                 else {
  2783.                                         d
  2784.                                                 .children(".jstree-children").attr("style","display:block !important").end()
  2785.                                                 .removeClass("jstree-open").addClass("jstree-closed").attr("aria-expanded", false)
  2786.                                                 .children(".jstree-children").stop(true, true).slideUp(animation, function () {
  2787.                                                         this.style.display = "";
  2788.                                                         d.children('.jstree-children').remove();
  2789.                                                         if (t.element) {
  2790.                                                                 t.trigger("after_close", { "node" : obj });
  2791.                                                         }
  2792.                                                 });
  2793.                                 }
  2794.                         }
  2795.                 },
  2796.                 /**
  2797.                  * toggles a node - closing it if it is open, opening it if it is closed
  2798.                  * @name toggle_node(obj)
  2799.                  * @param {mixed} obj the node to toggle
  2800.                  */
  2801.                 toggle_node : function (obj) {
  2802.                         var t1, t2;
  2803.                         if($.isArray(obj)) {
  2804.                                 obj = obj.slice();
  2805.                                 for(t1 = 0, t2 = obj.length; t1 < t2; t1++) {
  2806.                                         this.toggle_node(obj[t1]);
  2807.                                 }
  2808.                                 return true;
  2809.                         }
  2810.                         if(this.is_closed(obj)) {
  2811.                                 return this.open_node(obj);
  2812.                         }
  2813.                         if(this.is_open(obj)) {
  2814.                                 return this.close_node(obj);
  2815.                         }
  2816.                 },
  2817.                 /**
  2818.                  * 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.
  2819.                  * @name open_all([obj, animation, original_obj])
  2820.                  * @param {mixed} obj the node to open recursively, omit to open all nodes in the tree
  2821.                  * @param {Number} animation the animation duration in milliseconds when opening the nodes, the default is no animation
  2822.                  * @param {jQuery} reference to the node that started the process (internal use)
  2823.                  * @trigger open_all.jstree
  2824.                  */
  2825.                 open_all : function (obj, animation, original_obj) {
  2826.                         if(!obj) { obj = $.jstree.root; }
  2827.                         obj = this.get_node(obj);
  2828.                         if(!obj) { return false; }
  2829.                         var dom = obj.id === $.jstree.root ? this.get_container_ul() : this.get_node(obj, true), i, j, _this;
  2830.                         if(!dom.length) {
  2831.                                 for(i = 0, j = obj.children_d.length; i < j; i++) {
  2832.                                         if(this.is_closed(this._model.data[obj.children_d[i]])) {
  2833.                                                 this._model.data[obj.children_d[i]].state.opened = true;
  2834.                                         }
  2835.                                 }
  2836.                                 return this.trigger('open_all', { "node" : obj });
  2837.                         }
  2838.                         original_obj = original_obj || dom;
  2839.                         _this = this;
  2840.                         dom = this.is_closed(obj) ? dom.find('.jstree-closed').addBack() : dom.find('.jstree-closed');
  2841.                         dom.each(function () {
  2842.                                 _this.open_node(
  2843.                                         this,
  2844.                                         function(node, status) { if(status && this.is_parent(node)) { this.open_all(node, animation, original_obj); } },
  2845.                                         animation || 0
  2846.                                 );
  2847.                         });
  2848.                         if(original_obj.find('.jstree-closed').length === 0) {
  2849.                                 /**
  2850.                                  * triggered when an `open_all` call completes
  2851.                                  * @event
  2852.                                  * @name open_all.jstree
  2853.                                  * @param {Object} node the opened node
  2854.                                  */
  2855.                                 this.trigger('open_all', { "node" : this.get_node(original_obj) });
  2856.                         }
  2857.                 },
  2858.                 /**
  2859.                  * closes all nodes within a node (or the tree), revealing their children
  2860.                  * @name close_all([obj, animation])
  2861.                  * @param {mixed} obj the node to close recursively, omit to close all nodes in the tree
  2862.                  * @param {Number} animation the animation duration in milliseconds when closing the nodes, the default is no animation
  2863.                  * @trigger close_all.jstree
  2864.                  */
  2865.                 close_all : function (obj, animation) {
  2866.                         if(!obj) { obj = $.jstree.root; }
  2867.                         obj = this.get_node(obj);
  2868.                         if(!obj) { return false; }
  2869.                         var dom = obj.id === $.jstree.root ? this.get_container_ul() : this.get_node(obj, true),
  2870.                                 _this = this, i, j;
  2871.                         if(dom.length) {
  2872.                                 dom = this.is_open(obj) ? dom.find('.jstree-open').addBack() : dom.find('.jstree-open');
  2873.                                 $(dom.get().reverse()).each(function () { _this.close_node(this, animation || 0); });
  2874.                         }
  2875.                         for(i = 0, j = obj.children_d.length; i < j; i++) {
  2876.                                 this._model.data[obj.children_d[i]].state.opened = false;
  2877.                         }
  2878.                         /**
  2879.                          * triggered when an `close_all` call completes
  2880.                          * @event
  2881.                          * @name close_all.jstree
  2882.                          * @param {Object} node the closed node
  2883.                          */
  2884.                         this.trigger('close_all', { "node" : obj });
  2885.                 },
  2886.                 /**
  2887.                  * checks if a node is disabled (not selectable)
  2888.                  * @name is_disabled(obj)
  2889.                  * @param  {mixed} obj
  2890.                  * @return {Boolean}
  2891.                  */
  2892.                 is_disabled : function (obj) {
  2893.                         obj = this.get_node(obj);
  2894.                         return obj && obj.state && obj.state.disabled;
  2895.                 },
  2896.                 /**
  2897.                  * enables a node - so that it can be selected
  2898.                  * @name enable_node(obj)
  2899.                  * @param {mixed} obj the node to enable
  2900.                  * @trigger enable_node.jstree
  2901.                  */
  2902.                 enable_node : function (obj) {
  2903.                         var t1, t2;
  2904.                         if($.isArray(obj)) {
  2905.                                 obj = obj.slice();
  2906.                                 for(t1 = 0, t2 = obj.length; t1 < t2; t1++) {
  2907.                                         this.enable_node(obj[t1]);
  2908.                                 }
  2909.                                 return true;
  2910.                         }
  2911.                         obj = this.get_node(obj);
  2912.                         if(!obj || obj.id === $.jstree.root) {
  2913.                                 return false;
  2914.                         }
  2915.                         obj.state.disabled = false;
  2916.                         this.get_node(obj,true).children('.jstree-anchor').removeClass('jstree-disabled').attr('aria-disabled', false);
  2917.                         /**
  2918.                          * triggered when an node is enabled
  2919.                          * @event
  2920.                          * @name enable_node.jstree
  2921.                          * @param {Object} node the enabled node
  2922.                          */
  2923.                         this.trigger('enable_node', { 'node' : obj });
  2924.                 },
  2925.                 /**
  2926.                  * disables a node - so that it can not be selected
  2927.                  * @name disable_node(obj)
  2928.                  * @param {mixed} obj the node to disable
  2929.                  * @trigger disable_node.jstree
  2930.                  */
  2931.                 disable_node : function (obj) {
  2932.                         var t1, t2;
  2933.                         if($.isArray(obj)) {
  2934.                                 obj = obj.slice();
  2935.                                 for(t1 = 0, t2 = obj.length; t1 < t2; t1++) {
  2936.                                         this.disable_node(obj[t1]);
  2937.                                 }
  2938.                                 return true;
  2939.                         }
  2940.                         obj = this.get_node(obj);
  2941.                         if(!obj || obj.id === $.jstree.root) {
  2942.                                 return false;
  2943.                         }
  2944.                         obj.state.disabled = true;
  2945.                         this.get_node(obj,true).children('.jstree-anchor').addClass('jstree-disabled').attr('aria-disabled', true);
  2946.                         /**
  2947.                          * triggered when an node is disabled
  2948.                          * @event
  2949.                          * @name disable_node.jstree
  2950.                          * @param {Object} node the disabled node
  2951.                          */
  2952.                         this.trigger('disable_node', { 'node' : obj });
  2953.                 },
  2954.                 /**
  2955.                  * determines if a node is hidden
  2956.                  * @name is_hidden(obj)
  2957.                  * @param {mixed} obj the node
  2958.                  */
  2959.                 is_hidden : function (obj) {
  2960.                         obj = this.get_node(obj);
  2961.                         return obj.state.hidden === true;
  2962.                 },
  2963.                 /**
  2964.                  * hides a node - it is still in the structure but will not be visible
  2965.                  * @name hide_node(obj)
  2966.                  * @param {mixed} obj the node to hide
  2967.                  * @param {Boolean} skip_redraw internal parameter controlling if redraw is called
  2968.                  * @trigger hide_node.jstree
  2969.                  */
  2970.                 hide_node : function (obj, skip_redraw) {
  2971.                         var t1, t2;
  2972.                         if($.isArray(obj)) {
  2973.                                 obj = obj.slice();
  2974.                                 for(t1 = 0, t2 = obj.length; t1 < t2; t1++) {
  2975.                                         this.hide_node(obj[t1], true);
  2976.                                 }
  2977.                                 if (!skip_redraw) {
  2978.                                         this.redraw();
  2979.                                 }
  2980.                                 return true;
  2981.                         }
  2982.                         obj = this.get_node(obj);
  2983.                         if(!obj || obj.id === $.jstree.root) {
  2984.                                 return false;
  2985.                         }
  2986.                         if(!obj.state.hidden) {
  2987.                                 obj.state.hidden = true;
  2988.                                 this._node_changed(obj.parent);
  2989.                                 if(!skip_redraw) {
  2990.                                         this.redraw();
  2991.                                 }
  2992.                                 /**
  2993.                                  * triggered when an node is hidden
  2994.                                  * @event
  2995.                                  * @name hide_node.jstree
  2996.                                  * @param {Object} node the hidden node
  2997.                                  */
  2998.                                 this.trigger('hide_node', { 'node' : obj });
  2999.                         }
  3000.                 },
  3001.                 /**
  3002.                  * shows a node
  3003.                  * @name show_node(obj)
  3004.                  * @param {mixed} obj the node to show
  3005.                  * @param {Boolean} skip_redraw internal parameter controlling if redraw is called
  3006.                  * @trigger show_node.jstree
  3007.                  */
  3008.                 show_node : function (obj, skip_redraw) {
  3009.                         var t1, t2;
  3010.                         if($.isArray(obj)) {
  3011.                                 obj = obj.slice();
  3012.                                 for(t1 = 0, t2 = obj.length; t1 < t2; t1++) {
  3013.                                         this.show_node(obj[t1], true);
  3014.                                 }
  3015.                                 if (!skip_redraw) {
  3016.                                         this.redraw();
  3017.                                 }
  3018.                                 return true;
  3019.                         }
  3020.                         obj = this.get_node(obj);
  3021.                         if(!obj || obj.id === $.jstree.root) {
  3022.                                 return false;
  3023.                         }
  3024.                         if(obj.state.hidden) {
  3025.                                 obj.state.hidden = false;
  3026.                                 this._node_changed(obj.parent);
  3027.                                 if(!skip_redraw) {
  3028.                                         this.redraw();
  3029.                                 }
  3030.                                 /**
  3031.                                  * triggered when an node is shown
  3032.                                  * @event
  3033.                                  * @name show_node.jstree
  3034.                                  * @param {Object} node the shown node
  3035.                                  */
  3036.                                 this.trigger('show_node', { 'node' : obj });
  3037.                         }
  3038.                 },
  3039.                 /**
  3040.                  * hides all nodes
  3041.                  * @name hide_all()
  3042.                  * @trigger hide_all.jstree
  3043.                  */
  3044.                 hide_all : function (skip_redraw) {
  3045.                         var i, m = this._model.data, ids = [];
  3046.                         for(i in m) {
  3047.                                 if(m.hasOwnProperty(i) && i !== $.jstree.root && !m[i].state.hidden) {
  3048.                                         m[i].state.hidden = true;
  3049.                                         ids.push(i);
  3050.                                 }
  3051.                         }
  3052.                         this._model.force_full_redraw = true;
  3053.                         if(!skip_redraw) {
  3054.                                 this.redraw();
  3055.                         }
  3056.                         /**
  3057.                          * triggered when all nodes are hidden
  3058.                          * @event
  3059.                          * @name hide_all.jstree
  3060.                          * @param {Array} nodes the IDs of all hidden nodes
  3061.                          */
  3062.                         this.trigger('hide_all', { 'nodes' : ids });
  3063.                         return ids;
  3064.                 },
  3065.                 /**
  3066.                  * shows all nodes
  3067.                  * @name show_all()
  3068.                  * @trigger show_all.jstree
  3069.                  */
  3070.                 show_all : function (skip_redraw) {
  3071.                         var i, m = this._model.data, ids = [];
  3072.                         for(i in m) {
  3073.                                 if(m.hasOwnProperty(i) && i !== $.jstree.root && m[i].state.hidden) {
  3074.                                         m[i].state.hidden = false;
  3075.                                         ids.push(i);
  3076.                                 }
  3077.                         }
  3078.                         this._model.force_full_redraw = true;
  3079.                         if(!skip_redraw) {
  3080.                                 this.redraw();
  3081.                         }
  3082.                         /**
  3083.                          * triggered when all nodes are shown
  3084.                          * @event
  3085.                          * @name show_all.jstree
  3086.                          * @param {Array} nodes the IDs of all shown nodes
  3087.                          */
  3088.                         this.trigger('show_all', { 'nodes' : ids });
  3089.                         return ids;
  3090.                 },
  3091.                 /**
  3092.                  * called when a node is selected by the user. Used internally.
  3093.                  * @private
  3094.                  * @name activate_node(obj, e)
  3095.                  * @param {mixed} obj the node
  3096.                  * @param {Object} e the related event
  3097.                  * @trigger activate_node.jstree, changed.jstree
  3098.                  */
  3099.                 activate_node : function (obj, e) {
  3100.                         if(this.is_disabled(obj)) {
  3101.                                 return false;
  3102.                         }
  3103.                         if(!e || typeof e !== 'object') {
  3104.                                 e = {};
  3105.                         }
  3106.  
  3107.                         // 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
  3108.                         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;
  3109.                         if(this._data.core.last_clicked && !this._data.core.last_clicked.state.selected) { this._data.core.last_clicked = null; }
  3110.                         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]); }
  3111.  
  3112.                         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 ) )) {
  3113.                                 if(!this.settings.core.multiple && (e.metaKey || e.ctrlKey || e.shiftKey) && this.is_selected(obj)) {
  3114.                                         this.deselect_node(obj, false, e);
  3115.                                 }
  3116.                                 else {
  3117.                                         this.deselect_all(true);
  3118.                                         this.select_node(obj, false, false, e);
  3119.                                         this._data.core.last_clicked = this.get_node(obj);
  3120.                                 }
  3121.                         }
  3122.                         else {
  3123.                                 if(e.shiftKey) {
  3124.                                         var o = this.get_node(obj).id,
  3125.                                                 l = this._data.core.last_clicked.id,
  3126.                                                 p = this.get_node(this._data.core.last_clicked.parent).children,
  3127.                                                 c = false,
  3128.                                                 i, j;
  3129.                                         for(i = 0, j = p.length; i < j; i += 1) {
  3130.                                                 // separate IFs work whem o and l are the same
  3131.                                                 if(p[i] === o) {
  3132.                                                         c = !c;
  3133.                                                 }
  3134.                                                 if(p[i] === l) {
  3135.                                                         c = !c;
  3136.                                                 }
  3137.                                                 if(!this.is_disabled(p[i]) && (c || p[i] === o || p[i] === l)) {
  3138.                                                         if (!this.is_hidden(p[i])) {
  3139.                                                                 this.select_node(p[i], true, false, e);
  3140.                                                         }
  3141.                                                 }
  3142.                                                 else {
  3143.                                                         this.deselect_node(p[i], true, e);
  3144.                                                 }
  3145.                                         }
  3146.                                         this.trigger('changed', { 'action' : 'select_node', 'node' : this.get_node(obj), 'selected' : this._data.core.selected, 'event' : e });
  3147.                                 }
  3148.                                 else {
  3149.                                         if(!this.is_selected(obj)) {
  3150.                                                 this.select_node(obj, false, false, e);
  3151.                                         }
  3152.                                         else {
  3153.                                                 this.deselect_node(obj, false, e);
  3154.                                         }
  3155.                                 }
  3156.                         }
  3157.                         /**
  3158.                          * triggered when an node is clicked or intercated with by the user
  3159.                          * @event
  3160.                          * @name activate_node.jstree
  3161.                          * @param {Object} node
  3162.                          * @param {Object} event the ooriginal event (if any) which triggered the call (may be an empty object)
  3163.                          */
  3164.                         this.trigger('activate_node', { 'node' : this.get_node(obj), 'event' : e });
  3165.                 },
  3166.                 /**
  3167.                  * applies the hover state on a node, called when a node is hovered by the user. Used internally.
  3168.                  * @private
  3169.                  * @name hover_node(obj)
  3170.                  * @param {mixed} obj
  3171.                  * @trigger hover_node.jstree
  3172.                  */
  3173.                 hover_node : function (obj) {
  3174.                         obj = this.get_node(obj, true);
  3175.                         if(!obj || !obj.length || obj.children('.jstree-hovered').length) {
  3176.                                 return false;
  3177.                         }
  3178.                         var o = this.element.find('.jstree-hovered'), t = this.element;
  3179.                         if(o && o.length) { this.dehover_node(o); }
  3180.  
  3181.                         obj.children('.jstree-anchor').addClass('jstree-hovered');
  3182.                         /**
  3183.                          * triggered when an node is hovered
  3184.                          * @event
  3185.                          * @name hover_node.jstree
  3186.                          * @param {Object} node
  3187.                          */
  3188.                         this.trigger('hover_node', { 'node' : this.get_node(obj) });
  3189.                         setTimeout(function () { t.attr('aria-activedescendant', obj[0].id); }, 0);
  3190.                 },
  3191.                 /**
  3192.                  * removes the hover state from a nodecalled when a node is no longer hovered by the user. Used internally.
  3193.                  * @private
  3194.                  * @name dehover_node(obj)
  3195.                  * @param {mixed} obj
  3196.                  * @trigger dehover_node.jstree
  3197.                  */
  3198.                 dehover_node : function (obj) {
  3199.                         obj = this.get_node(obj, true);
  3200.                         if(!obj || !obj.length || !obj.children('.jstree-hovered').length) {
  3201.                                 return false;
  3202.                         }
  3203.                         obj.children('.jstree-anchor').removeClass('jstree-hovered');
  3204.                         /**
  3205.                          * triggered when an node is no longer hovered
  3206.                          * @event
  3207.                          * @name dehover_node.jstree
  3208.                          * @param {Object} node
  3209.                          */
  3210.                         this.trigger('dehover_node', { 'node' : this.get_node(obj) });
  3211.                 },
  3212.                 /**
  3213.                  * select a node
  3214.                  * @name select_node(obj [, supress_event, prevent_open])
  3215.                  * @param {mixed} obj an array can be used to select multiple nodes
  3216.                  * @param {Boolean} supress_event if set to `true` the `changed.jstree` event won't be triggered
  3217.                  * @param {Boolean} prevent_open if set to `true` parents of the selected node won't be opened
  3218.                  * @trigger select_node.jstree, changed.jstree
  3219.                  */
  3220.                 select_node : function (obj, supress_event, prevent_open, e) {
  3221.                         var dom, t1, t2, th;
  3222.                         if($.isArray(obj)) {
  3223.                                 obj = obj.slice();
  3224.                                 for(t1 = 0, t2 = obj.length; t1 < t2; t1++) {
  3225.                                         this.select_node(obj[t1], supress_event, prevent_open, e);
  3226.                                 }
  3227.                                 return true;
  3228.                         }
  3229.                         obj = this.get_node(obj);
  3230.                         if(!obj || obj.id === $.jstree.root) {
  3231.                                 return false;
  3232.                         }
  3233.                         dom = this.get_node(obj, true);
  3234.                         if(!obj.state.selected) {
  3235.                                 obj.state.selected = true;
  3236.                                 this._data.core.selected.push(obj.id);
  3237.                                 if(!prevent_open) {
  3238.                                         dom = this._open_to(obj);
  3239.                                 }
  3240.                                 if(dom && dom.length) {
  3241.                                         dom.attr('aria-selected', true).children('.jstree-anchor').addClass('jstree-clicked');
  3242.                                 }
  3243.                                 /**
  3244.                                  * triggered when an node is selected
  3245.                                  * @event
  3246.                                  * @name select_node.jstree
  3247.                                  * @param {Object} node
  3248.                                  * @param {Array} selected the current selection
  3249.                                  * @param {Object} event the event (if any) that triggered this select_node
  3250.                                  */
  3251.                                 this.trigger('select_node', { 'node' : obj, 'selected' : this._data.core.selected, 'event' : e });
  3252.                                 if(!supress_event) {
  3253.                                         /**
  3254.                                          * triggered when selection changes
  3255.                                          * @event
  3256.                                          * @name changed.jstree
  3257.                                          * @param {Object} node
  3258.                                          * @param {Object} action the action that caused the selection to change
  3259.                                          * @param {Array} selected the current selection
  3260.                                          * @param {Object} event the event (if any) that triggered this changed event
  3261.                                          */
  3262.                                         this.trigger('changed', { 'action' : 'select_node', 'node' : obj, 'selected' : this._data.core.selected, 'event' : e });
  3263.                                 }
  3264.                         }
  3265.                 },
  3266.                 /**
  3267.                  * deselect a node
  3268.                  * @name deselect_node(obj [, supress_event])
  3269.                  * @param {mixed} obj an array can be used to deselect multiple nodes
  3270.                  * @param {Boolean} supress_event if set to `true` the `changed.jstree` event won't be triggered
  3271.                  * @trigger deselect_node.jstree, changed.jstree
  3272.                  */
  3273.                 deselect_node : function (obj, supress_event, e) {
  3274.                         var t1, t2, dom;
  3275.                         if($.isArray(obj)) {
  3276.                                 obj = obj.slice();
  3277.                                 for(t1 = 0, t2 = obj.length; t1 < t2; t1++) {
  3278.                                         this.deselect_node(obj[t1], supress_event, e);
  3279.                                 }
  3280.                                 return true;
  3281.                         }
  3282.                         obj = this.get_node(obj);
  3283.                         if(!obj || obj.id === $.jstree.root) {
  3284.                                 return false;
  3285.                         }
  3286.                         dom = this.get_node(obj, true);
  3287.                         if(obj.state.selected) {
  3288.                                 obj.state.selected = false;
  3289.                                 this._data.core.selected = $.vakata.array_remove_item(this._data.core.selected, obj.id);
  3290.                                 if(dom.length) {
  3291.                                         dom.attr('aria-selected', false).children('.jstree-anchor').removeClass('jstree-clicked');
  3292.                                 }
  3293.                                 /**
  3294.                                  * triggered when an node is deselected
  3295.                                  * @event
  3296.                                  * @name deselect_node.jstree
  3297.                                  * @param {Object} node
  3298.                                  * @param {Array} selected the current selection
  3299.                                  * @param {Object} event the event (if any) that triggered this deselect_node
  3300.                                  */
  3301.                                 this.trigger('deselect_node', { 'node' : obj, 'selected' : this._data.core.selected, 'event' : e });
  3302.                                 if(!supress_event) {
  3303.                                         this.trigger('changed', { 'action' : 'deselect_node', 'node' : obj, 'selected' : this._data.core.selected, 'event' : e });
  3304.                                 }
  3305.                         }
  3306.                 },
  3307.                 /**
  3308.                  * select all nodes in the tree
  3309.                  * @name select_all([supress_event])
  3310.                  * @param {Boolean} supress_event if set to `true` the `changed.jstree` event won't be triggered
  3311.                  * @trigger select_all.jstree, changed.jstree
  3312.                  */
  3313.                 select_all : function (supress_event) {
  3314.                         var tmp = this._data.core.selected.concat([]), i, j;
  3315.                         this._data.core.selected = this._model.data[$.jstree.root].children_d.concat();
  3316.                         for(i = 0, j = this._data.core.selected.length; i < j; i++) {
  3317.                                 if(this._model.data[this._data.core.selected[i]]) {
  3318.                                         this._model.data[this._data.core.selected[i]].state.selected = true;
  3319.                                 }
  3320.                         }
  3321.                         this.redraw(true);
  3322.                         /**
  3323.                          * triggered when all nodes are selected
  3324.                          * @event
  3325.                          * @name select_all.jstree
  3326.                          * @param {Array} selected the current selection
  3327.                          */
  3328.                         this.trigger('select_all', { 'selected' : this._data.core.selected });
  3329.                         if(!supress_event) {
  3330.                                 this.trigger('changed', { 'action' : 'select_all', 'selected' : this._data.core.selected, 'old_selection' : tmp });
  3331.                         }
  3332.                 },
  3333.                 /**
  3334.                  * deselect all selected nodes
  3335.                  * @name deselect_all([supress_event])
  3336.                  * @param {Boolean} supress_event if set to `true` the `changed.jstree` event won't be triggered
  3337.                  * @trigger deselect_all.jstree, changed.jstree
  3338.                  */
  3339.                 deselect_all : function (supress_event) {
  3340.                         var tmp = this._data.core.selected.concat([]), i, j;
  3341.                         for(i = 0, j = this._data.core.selected.length; i < j; i++) {
  3342.                                 if(this._model.data[this._data.core.selected[i]]) {
  3343.                                         this._model.data[this._data.core.selected[i]].state.selected = false;
  3344.                                 }
  3345.                         }
  3346.                         this._data.core.selected = [];
  3347.                         this.element.find('.jstree-clicked').removeClass('jstree-clicked').parent().attr('aria-selected', false);
  3348.                         /**
  3349.                          * triggered when all nodes are deselected
  3350.                          * @event
  3351.                          * @name deselect_all.jstree
  3352.                          * @param {Object} node the previous selection
  3353.                          * @param {Array} selected the current selection
  3354.                          */
  3355.                         this.trigger('deselect_all', { 'selected' : this._data.core.selected, 'node' : tmp });
  3356.                         if(!supress_event) {
  3357.                                 this.trigger('changed', { 'action' : 'deselect_all', 'selected' : this._data.core.selected, 'old_selection' : tmp });
  3358.                         }
  3359.                 },
  3360.                 /**
  3361.                  * checks if a node is selected
  3362.                  * @name is_selected(obj)
  3363.                  * @param  {mixed}  obj
  3364.                  * @return {Boolean}
  3365.                  */
  3366.                 is_selected : function (obj) {
  3367.                         obj = this.get_node(obj);
  3368.                         if(!obj || obj.id === $.jstree.root) {
  3369.                                 return false;
  3370.                         }
  3371.                         return obj.state.selected;
  3372.                 },
  3373.                 /**
  3374.                  * get an array of all selected nodes
  3375.                  * @name get_selected([full])
  3376.                  * @param  {mixed}  full if set to `true` the returned array will consist of the full node objects, otherwise - only IDs will be returned
  3377.                  * @return {Array}
  3378.                  */
  3379.                 get_selected : function (full) {
  3380.                         return full ? $.map(this._data.core.selected, $.proxy(function (i) { return this.get_node(i); }, this)) : this._data.core.selected.slice();
  3381.                 },
  3382.                 /**
  3383.                  * get an array of all top level selected nodes (ignoring children of selected nodes)
  3384.                  * @name get_top_selected([full])
  3385.                  * @param  {mixed}  full if set to `true` the returned array will consist of the full node objects, otherwise - only IDs will be returned
  3386.                  * @return {Array}
  3387.                  */
  3388.                 get_top_selected : function (full) {
  3389.                         var tmp = this.get_selected(true),
  3390.                                 obj = {}, i, j, k, l;
  3391.                         for(i = 0, j = tmp.length; i < j; i++) {
  3392.                                 obj[tmp[i].id] = tmp[i];
  3393.                         }
  3394.                         for(i = 0, j = tmp.length; i < j; i++) {
  3395.                                 for(k = 0, l = tmp[i].children_d.length; k < l; k++) {
  3396.                                         if(obj[tmp[i].children_d[k]]) {
  3397.                                                 delete obj[tmp[i].children_d[k]];
  3398.                                         }
  3399.                                 }
  3400.                         }
  3401.                         tmp = [];
  3402.                         for(i in obj) {
  3403.                                 if(obj.hasOwnProperty(i)) {
  3404.                                         tmp.push(i);
  3405.                                 }
  3406.                         }
  3407.                         return full ? $.map(tmp, $.proxy(function (i) { return this.get_node(i); }, this)) : tmp;
  3408.                 },
  3409.                 /**
  3410.                  * get an array of all bottom level selected nodes (ignoring selected parents)
  3411.                  * @name get_bottom_selected([full])
  3412.                  * @param  {mixed}  full if set to `true` the returned array will consist of the full node objects, otherwise - only IDs will be returned
  3413.                  * @return {Array}
  3414.                  */
  3415.                 get_bottom_selected : function (full) {
  3416.                         var tmp = this.get_selected(true),
  3417.                                 obj = [], i, j;
  3418.                         for(i = 0, j = tmp.length; i < j; i++) {
  3419.                                 if(!tmp[i].children.length) {
  3420.                                         obj.push(tmp[i].id);
  3421.                                 }
  3422.                         }
  3423.                         return full ? $.map(obj, $.proxy(function (i) { return this.get_node(i); }, this)) : obj;
  3424.                 },
  3425.                 /**
  3426.                  * gets the current state of the tree so that it can be restored later with `set_state(state)`. Used internally.
  3427.                  * @name get_state()
  3428.                  * @private
  3429.                  * @return {Object}
  3430.                  */
  3431.                 get_state : function () {
  3432.                         var state       = {
  3433.                                 'core' : {
  3434.                                         'open' : [],
  3435.                                         'loaded' : [],
  3436.                                         'scroll' : {
  3437.                                                 'left' : this.element.scrollLeft(),
  3438.                                                 'top' : this.element.scrollTop()
  3439.                                         },
  3440.                                         /*!
  3441.                                         'themes' : {
  3442.                                                 'name' : this.get_theme(),
  3443.                                                 'icons' : this._data.core.themes.icons,
  3444.                                                 'dots' : this._data.core.themes.dots
  3445.                                         },
  3446.                                         */
  3447.                                         'selected' : []
  3448.                                 }
  3449.                         }, i;
  3450.                         for(i in this._model.data) {
  3451.                                 if(this._model.data.hasOwnProperty(i)) {
  3452.                                         if(i !== $.jstree.root) {
  3453.                                                 if(this._model.data[i].state.loaded && this.settings.core.loaded_state) {
  3454.                                                         state.core.loaded.push(i);
  3455.                                                 }
  3456.                                                 if(this._model.data[i].state.opened) {
  3457.                                                         state.core.open.push(i);
  3458.                                                 }
  3459.                                                 if(this._model.data[i].state.selected) {
  3460.                                                         state.core.selected.push(i);
  3461.                                                 }
  3462.                                         }
  3463.                                 }
  3464.                         }
  3465.                         return state;
  3466.                 },
  3467.                 /**
  3468.                  * sets the state of the tree. Used internally.
  3469.                  * @name set_state(state [, callback])
  3470.                  * @private
  3471.                  * @param {Object} state the state to restore. Keep in mind this object is passed by reference and jstree will modify it.
  3472.                  * @param {Function} callback an optional function to execute once the state is restored.
  3473.                  * @trigger set_state.jstree
  3474.                  */
  3475.                 set_state : function (state, callback) {
  3476.                         if(state) {
  3477.                                 if(state.core && state.core.selected && state.core.initial_selection === undefined) {
  3478.                                         state.core.initial_selection = this._data.core.selected.concat([]).sort().join(',');
  3479.                                 }
  3480.                                 if(state.core) {
  3481.                                         var res, n, t, _this, i;
  3482.                                         if(state.core.loaded) {
  3483.                                                 if(!this.settings.core.loaded_state || !$.isArray(state.core.loaded) || !state.core.loaded.length) {
  3484.                                                         delete state.core.loaded;
  3485.                                                         this.set_state(state, callback);
  3486.                                                 }
  3487.                                                 else {
  3488.                                                         this._load_nodes(state.core.loaded, function (nodes) {
  3489.                                                                 delete state.core.loaded;
  3490.                                                                 this.set_state(state, callback);
  3491.                                                         });
  3492.                                                 }
  3493.                                                 return false;
  3494.                                         }
  3495.                                         if(state.core.open) {
  3496.                                                 if(!$.isArray(state.core.open) || !state.core.open.length) {
  3497.                                                         delete state.core.open;
  3498.                                                         this.set_state(state, callback);
  3499.                                                 }
  3500.                                                 else {
  3501.                                                         this._load_nodes(state.core.open, function (nodes) {
  3502.                                                                 this.open_node(nodes, false, 0);
  3503.                                                                 delete state.core.open;
  3504.                                                                 this.set_state(state, callback);
  3505.                                                         });
  3506.                                                 }
  3507.                                                 return false;
  3508.                                         }
  3509.                                         if(state.core.scroll) {
  3510.                                                 if(state.core.scroll && state.core.scroll.left !== undefined) {
  3511.                                                         this.element.scrollLeft(state.core.scroll.left);
  3512.                                                 }
  3513.                                                 if(state.core.scroll && state.core.scroll.top !== undefined) {
  3514.                                                         this.element.scrollTop(state.core.scroll.top);
  3515.                                                 }
  3516.                                                 delete state.core.scroll;
  3517.                                                 this.set_state(state, callback);
  3518.                                                 return false;
  3519.                                         }
  3520.                                         if(state.core.selected) {
  3521.                                                 _this = this;
  3522.                                                 if (state.core.initial_selection === undefined ||
  3523.                                                         state.core.initial_selection === this._data.core.selected.concat([]).sort().join(',')
  3524.                                                 ) {
  3525.                                                         this.deselect_all();
  3526.                                                         $.each(state.core.selected, function (i, v) {
  3527.                                                                 _this.select_node(v, false, true);
  3528.                                                         });
  3529.                                                 }
  3530.                                                 delete state.core.initial_selection;
  3531.                                                 delete state.core.selected;
  3532.                                                 this.set_state(state, callback);
  3533.                                                 return false;
  3534.                                         }
  3535.                                         for(i in state) {
  3536.                                                 if(state.hasOwnProperty(i) && i !== "core" && $.inArray(i, this.settings.plugins) === -1) {
  3537.                                                         delete state[i];
  3538.                                                 }
  3539.                                         }
  3540.                                         if($.isEmptyObject(state.core)) {
  3541.                                                 delete state.core;
  3542.                                                 this.set_state(state, callback);
  3543.                                                 return false;
  3544.                                         }
  3545.                                 }
  3546.                                 if($.isEmptyObject(state)) {
  3547.                                         state = null;
  3548.                                         if(callback) { callback.call(this); }
  3549.                                         /**
  3550.                                          * triggered when a `set_state` call completes
  3551.                                          * @event
  3552.                                          * @name set_state.jstree
  3553.                                          */
  3554.                                         this.trigger('set_state');
  3555.                                         return false;
  3556.                                 }
  3557.                                 return true;
  3558.                         }
  3559.                         return false;
  3560.                 },
  3561.                 /**
  3562.                  * refreshes the tree - all nodes are reloaded with calls to `load_node`.
  3563.                  * @name refresh()
  3564.                  * @param {Boolean} skip_loading an option to skip showing the loading indicator
  3565.                  * @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
  3566.                  * @trigger refresh.jstree
  3567.                  */
  3568.                 refresh : function (skip_loading, forget_state) {
  3569.                         this._data.core.state = forget_state === true ? {} : this.get_state();
  3570.                         if(forget_state && $.isFunction(forget_state)) { this._data.core.state = forget_state.call(this, this._data.core.state); }
  3571.                         this._cnt = 0;
  3572.                         this._model.data = {};
  3573.                         this._model.data[$.jstree.root] = {
  3574.                                 id : $.jstree.root,
  3575.                                 parent : null,
  3576.                                 parents : [],
  3577.                                 children : [],
  3578.                                 children_d : [],
  3579.                                 state : { loaded : false }
  3580.                         };
  3581.                         this._data.core.selected = [];
  3582.                         this._data.core.last_clicked = null;
  3583.                         this._data.core.focused = null;
  3584.  
  3585.                         var c = this.get_container_ul()[0].className;
  3586.                         if(!skip_loading) {
  3587.                                 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>");
  3588.                                 this.element.attr('aria-activedescendant','j'+this._id+'_loading');
  3589.                         }
  3590.                         this.load_node($.jstree.root, function (o, s) {
  3591.                                 if(s) {
  3592.                                         this.get_container_ul()[0].className = c;
  3593.                                         if(this._firstChild(this.get_container_ul()[0])) {
  3594.                                                 this.element.attr('aria-activedescendant',this._firstChild(this.get_container_ul()[0]).id);
  3595.                                         }
  3596.                                         this.set_state($.extend(true, {}, this._data.core.state), function () {
  3597.                                                 /**
  3598.                                                  * triggered when a `refresh` call completes
  3599.                                                  * @event
  3600.                                                  * @name refresh.jstree
  3601.                                                  */
  3602.                                                 this.trigger('refresh');
  3603.                                         });
  3604.                                 }
  3605.                                 this._data.core.state = null;
  3606.                         });
  3607.                 },
  3608.                 /**
  3609.                  * refreshes a node in the tree (reload its children) all opened nodes inside that node are reloaded with calls to `load_node`.
  3610.                  * @name refresh_node(obj)
  3611.                  * @param  {mixed} obj the node
  3612.                  * @trigger refresh_node.jstree
  3613.                  */
  3614.                 refresh_node : function (obj) {
  3615.                         obj = this.get_node(obj);
  3616.                         if(!obj || obj.id === $.jstree.root) { return false; }
  3617.                         var opened = [], to_load = [], s = this._data.core.selected.concat([]);
  3618.                         to_load.push(obj.id);
  3619.                         if(obj.state.opened === true) { opened.push(obj.id); }
  3620.                         this.get_node(obj, true).find('.jstree-open').each(function() { to_load.push(this.id); opened.push(this.id); });
  3621.                         this._load_nodes(to_load, $.proxy(function (nodes) {
  3622.                                 this.open_node(opened, false, 0);
  3623.                                 this.select_node(s);
  3624.                                 /**
  3625.                                  * triggered when a node is refreshed
  3626.                                  * @event
  3627.                                  * @name refresh_node.jstree
  3628.                                  * @param {Object} node - the refreshed node
  3629.                                  * @param {Array} nodes - an array of the IDs of the nodes that were reloaded
  3630.                                  */
  3631.                                 this.trigger('refresh_node', { 'node' : obj, 'nodes' : nodes });
  3632.                         }, this), false, true);
  3633.                 },
  3634.                 /**
  3635.                  * set (change) the ID of a node
  3636.                  * @name set_id(obj, id)
  3637.                  * @param  {mixed} obj the node
  3638.                  * @param  {String} id the new ID
  3639.                  * @return {Boolean}
  3640.                  * @trigger set_id.jstree
  3641.                  */
  3642.                 set_id : function (obj, id) {
  3643.                         obj = this.get_node(obj);
  3644.                         if(!obj || obj.id === $.jstree.root) { return false; }
  3645.                         var i, j, m = this._model.data, old = obj.id;
  3646.                         id = id.toString();
  3647.                         // update parents (replace current ID with new one in children and children_d)
  3648.                         m[obj.parent].children[$.inArray(obj.id, m[obj.parent].children)] = id;
  3649.                         for(i = 0, j = obj.parents.length; i < j; i++) {
  3650.                                 m[obj.parents[i]].children_d[$.inArray(obj.id, m[obj.parents[i]].children_d)] = id;
  3651.                         }
  3652.                         // update children (replace current ID with new one in parent and parents)
  3653.                         for(i = 0, j = obj.children.length; i < j; i++) {
  3654.                                 m[obj.children[i]].parent = id;
  3655.                         }
  3656.                         for(i = 0, j = obj.children_d.length; i < j; i++) {
  3657.                                 m[obj.children_d[i]].parents[$.inArray(obj.id, m[obj.children_d[i]].parents)] = id;
  3658.                         }
  3659.                         i = $.inArray(obj.id, this._data.core.selected);
  3660.                         if(i !== -1) { this._data.core.selected[i] = id; }
  3661.                         // update model and obj itself (obj.id, this._model.data[KEY])
  3662.                         i = this.get_node(obj.id, true);
  3663.                         if(i) {
  3664.                                 i.attr('id', id); //.children('.jstree-anchor').attr('id', id + '_anchor').end().attr('aria-labelledby', id + '_anchor');
  3665.                                 if(this.element.attr('aria-activedescendant') === obj.id) {
  3666.                                         this.element.attr('aria-activedescendant', id);
  3667.                                 }
  3668.                         }
  3669.                         delete m[obj.id];
  3670.                         obj.id = id;
  3671.                         obj.li_attr.id = id;
  3672.                         m[id] = obj;
  3673.                         /**
  3674.                          * triggered when a node id value is changed
  3675.                          * @event
  3676.                          * @name set_id.jstree
  3677.                          * @param {Object} node
  3678.                          * @param {String} old the old id
  3679.                          */
  3680.                         this.trigger('set_id',{ "node" : obj, "new" : obj.id, "old" : old });
  3681.                         return true;
  3682.                 },
  3683.                 /**
  3684.                  * get the text value of a node
  3685.                  * @name get_text(obj)
  3686.                  * @param  {mixed} obj the node
  3687.                  * @return {String}
  3688.                  */
  3689.                 get_text : function (obj) {
  3690.                         obj = this.get_node(obj);
  3691.                         return (!obj || obj.id === $.jstree.root) ? false : obj.text;
  3692.                 },
  3693.                 /**
  3694.                  * set the text value of a node. Used internally, please use `rename_node(obj, val)`.
  3695.                  * @private
  3696.                  * @name set_text(obj, val)
  3697.                  * @param  {mixed} obj the node, you can pass an array to set the text on multiple nodes
  3698.                  * @param  {String} val the new text value
  3699.                  * @return {Boolean}
  3700.                  * @trigger set_text.jstree
  3701.                  */
  3702.                 set_text : function (obj, val) {
  3703.                         var t1, t2;
  3704.                         if($.isArray(obj)) {
  3705.                                 obj = obj.slice();
  3706.                                 for(t1 = 0, t2 = obj.length; t1 < t2; t1++) {
  3707.                                         this.set_text(obj[t1], val);
  3708.                                 }
  3709.                                 return true;
  3710.                         }
  3711.                         obj = this.get_node(obj);
  3712.                         if(!obj || obj.id === $.jstree.root) { return false; }
  3713.                         obj.text = val;
  3714.                         if(this.get_node(obj, true).length) {
  3715.                                 this.redraw_node(obj.id);
  3716.                         }
  3717.                         /**
  3718.                          * triggered when a node text value is changed
  3719.                          * @event
  3720.                          * @name set_text.jstree
  3721.                          * @param {Object} obj
  3722.                          * @param {String} text the new value
  3723.                          */
  3724.                         this.trigger('set_text',{ "obj" : obj, "text" : val });
  3725.                         return true;
  3726.                 },
  3727.                 /**
  3728.                  * gets a JSON representation of a node (or the whole tree)
  3729.                  * @name get_json([obj, options])
  3730.                  * @param  {mixed} obj
  3731.                  * @param  {Object} options
  3732.                  * @param  {Boolean} options.no_state do not return state information
  3733.                  * @param  {Boolean} options.no_id do not return ID
  3734.                  * @param  {Boolean} options.no_children do not include children
  3735.                  * @param  {Boolean} options.no_data do not include node data
  3736.                  * @param  {Boolean} options.no_li_attr do not include LI attributes
  3737.                  * @param  {Boolean} options.no_a_attr do not include A attributes
  3738.                  * @param  {Boolean} options.flat return flat JSON instead of nested
  3739.                  * @return {Object}
  3740.                  */
  3741.                 get_json : function (obj, options, flat) {
  3742.                         obj = this.get_node(obj || $.jstree.root);
  3743.                         if(!obj) { return false; }
  3744.                         if(options && options.flat && !flat) { flat = []; }
  3745.                         var tmp = {
  3746.                                 'id' : obj.id,
  3747.                                 'text' : obj.text,
  3748.                                 'icon' : this.get_icon(obj),
  3749.                                 'li_attr' : $.extend(true, {}, obj.li_attr),
  3750.                                 'a_attr' : $.extend(true, {}, obj.a_attr),
  3751.                                 'state' : {},
  3752.                                 'data' : options && options.no_data ? false : $.extend(true, $.isArray(obj.data)?[]:{}, obj.data)
  3753.                                 //( this.get_node(obj, true).length ? this.get_node(obj, true).data() : obj.data ),
  3754.                         }, i, j;
  3755.                         if(options && options.flat) {
  3756.                                 tmp.parent = obj.parent;
  3757.                         }
  3758.                         else {
  3759.                                 tmp.children = [];
  3760.                         }
  3761.                         if(!options || !options.no_state) {
  3762.                                 for(i in obj.state) {
  3763.                                         if(obj.state.hasOwnProperty(i)) {
  3764.                                                 tmp.state[i] = obj.state[i];
  3765.                                         }
  3766.                                 }
  3767.                         } else {
  3768.                                 delete tmp.state;
  3769.                         }
  3770.                         if(options && options.no_li_attr) {
  3771.                                 delete tmp.li_attr;
  3772.                         }
  3773.                         if(options && options.no_a_attr) {
  3774.                                 delete tmp.a_attr;
  3775.                         }
  3776.                         if(options && options.no_id) {
  3777.                                 delete tmp.id;
  3778.                                 if(tmp.li_attr && tmp.li_attr.id) {
  3779.                                         delete tmp.li_attr.id;
  3780.                                 }
  3781.                                 if(tmp.a_attr && tmp.a_attr.id) {
  3782.                                         delete tmp.a_attr.id;
  3783.                                 }
  3784.                         }
  3785.                         if(options && options.flat && obj.id !== $.jstree.root) {
  3786.                                 flat.push(tmp);
  3787.                         }
  3788.                         if(!options || !options.no_children) {
  3789.                                 for(i = 0, j = obj.children.length; i < j; i++) {
  3790.                                         if(options && options.flat) {
  3791.                                                 this.get_json(obj.children[i], options, flat);
  3792.                                         }
  3793.                                         else {
  3794.                                                 tmp.children.push(this.get_json(obj.children[i], options));
  3795.                                         }
  3796.                                 }
  3797.                         }
  3798.                         return options && options.flat ? flat : (obj.id === $.jstree.root ? tmp.children : tmp);
  3799.                 },
  3800.                 /**
  3801.                  * create a new node (do not confuse with load_node)
  3802.                  * @name create_node([par, node, pos, callback, is_loaded])
  3803.                  * @param  {mixed}   par       the parent node (to create a root node use either "#" (string) or `null`)
  3804.                  * @param  {mixed}   node      the data for the new node (a valid JSON object, or a simple string with the name)
  3805.                  * @param  {mixed}   pos       the index at which to insert the node, "first" and "last" are also supported, default is "last"
  3806.                  * @param  {Function} callback a function to be called once the node is created
  3807.                  * @param  {Boolean} is_loaded internal argument indicating if the parent node was succesfully loaded
  3808.                  * @return {String}            the ID of the newly create node
  3809.                  * @trigger model.jstree, create_node.jstree
  3810.                  */
  3811.                 create_node : function (par, node, pos, callback, is_loaded) {
  3812.                         if(par === null) { par = $.jstree.root; }
  3813.                         par = this.get_node(par);
  3814.                         if(!par) { return false; }
  3815.                         pos = pos === undefined ? "last" : pos;
  3816.                         if(!pos.toString().match(/^(before|after)$/) && !is_loaded && !this.is_loaded(par)) {
  3817.                                 return this.load_node(par, function () { this.create_node(par, node, pos, callback, true); });
  3818.                         }
  3819.                         if(!node) { node = { "text" : this.get_string('New node') }; }
  3820.                         if(typeof node === "string") {
  3821.                                 node = { "text" : node };
  3822.                         } else {
  3823.                                 node = $.extend(true, {}, node);
  3824.                         }
  3825.                         if(node.text === undefined) { node.text = this.get_string('New node'); }
  3826.                         var tmp, dpc, i, j;
  3827.  
  3828.                         if(par.id === $.jstree.root) {
  3829.                                 if(pos === "before") { pos = "first"; }
  3830.                                 if(pos === "after") { pos = "last"; }
  3831.                         }
  3832.                         switch(pos) {
  3833.                                 case "before":
  3834.                                         tmp = this.get_node(par.parent);
  3835.                                         pos = $.inArray(par.id, tmp.children);
  3836.                                         par = tmp;
  3837.                                         break;
  3838.                                 case "after" :
  3839.                                         tmp = this.get_node(par.parent);
  3840.                                         pos = $.inArray(par.id, tmp.children) + 1;
  3841.                                         par = tmp;
  3842.                                         break;
  3843.                                 case "inside":
  3844.                                 case "first":
  3845.                                         pos = 0;
  3846.                                         break;
  3847.                                 case "last":
  3848.                                         pos = par.children.length;
  3849.                                         break;
  3850.                                 default:
  3851.                                         if(!pos) { pos = 0; }
  3852.                                         break;
  3853.                         }
  3854.                         if(pos > par.children.length) { pos = par.children.length; }
  3855.                         if(!node.id) { node.id = true; }
  3856.                         if(!this.check("create_node", node, par, pos)) {
  3857.                                 this.settings.core.error.call(this, this._data.core.last_error);
  3858.                                 return false;
  3859.                         }
  3860.                         if(node.id === true) { delete node.id; }
  3861.                         node = this._parse_model_from_json(node, par.id, par.parents.concat());
  3862.                         if(!node) { return false; }
  3863.                         tmp = this.get_node(node);
  3864.                         dpc = [];
  3865.                         dpc.push(node);
  3866.                         dpc = dpc.concat(tmp.children_d);
  3867.                         this.trigger('model', { "nodes" : dpc, "parent" : par.id });
  3868.  
  3869.                         par.children_d = par.children_d.concat(dpc);
  3870.                         for(i = 0, j = par.parents.length; i < j; i++) {
  3871.                                 this._model.data[par.parents[i]].children_d = this._model.data[par.parents[i]].children_d.concat(dpc);
  3872.                         }
  3873.                         node = tmp;
  3874.                         tmp = [];
  3875.                         for(i = 0, j = par.children.length; i < j; i++) {
  3876.                                 tmp[i >= pos ? i+1 : i] = par.children[i];
  3877.                         }
  3878.                         tmp[pos] = node.id;
  3879.                         par.children = tmp;
  3880.  
  3881.                         this.redraw_node(par, true);
  3882.                         /**
  3883.                          * triggered when a node is created
  3884.                          * @event
  3885.                          * @name create_node.jstree
  3886.                          * @param {Object} node
  3887.                          * @param {String} parent the parent's ID
  3888.                          * @param {Number} position the position of the new node among the parent's children
  3889.                          */
  3890.                         this.trigger('create_node', { "node" : this.get_node(node), "parent" : par.id, "position" : pos });
  3891.                         if(callback) { callback.call(this, this.get_node(node)); }
  3892.                         return node.id;
  3893.                 },
  3894.                 /**
  3895.                  * set the text value of a node
  3896.                  * @name rename_node(obj, val)
  3897.                  * @param  {mixed} obj the node, you can pass an array to rename multiple nodes to the same name
  3898.                  * @param  {String} val the new text value
  3899.                  * @return {Boolean}
  3900.                  * @trigger rename_node.jstree
  3901.                  */
  3902.                 rename_node : function (obj, val) {
  3903.                         var t1, t2, old;
  3904.                         if($.isArray(obj)) {
  3905.                                 obj = obj.slice();
  3906.                                 for(t1 = 0, t2 = obj.length; t1 < t2; t1++) {
  3907.                                         this.rename_node(obj[t1], val);
  3908.                                 }
  3909.                                 return true;
  3910.                         }
  3911.                         obj = this.get_node(obj);
  3912.                         if(!obj || obj.id === $.jstree.root) { return false; }
  3913.                         old = obj.text;
  3914.                         if(!this.check("rename_node", obj, this.get_parent(obj), val)) {
  3915.                                 this.settings.core.error.call(this, this._data.core.last_error);
  3916.                                 return false;
  3917.                         }
  3918.                         this.set_text(obj, val); // .apply(this, Array.prototype.slice.call(arguments))
  3919.                         /**
  3920.                          * triggered when a node is renamed
  3921.                          * @event
  3922.                          * @name rename_node.jstree
  3923.                          * @param {Object} node
  3924.                          * @param {String} text the new value
  3925.                          * @param {String} old the old value
  3926.                          */
  3927.                         this.trigger('rename_node', { "node" : obj, "text" : val, "old" : old });
  3928.                         return true;
  3929.                 },
  3930.                 /**
  3931.                  * remove a node
  3932.                  * @name delete_node(obj)
  3933.                  * @param  {mixed} obj the node, you can pass an array to delete multiple nodes
  3934.                  * @return {Boolean}
  3935.                  * @trigger delete_node.jstree, changed.jstree
  3936.                  */
  3937.                 delete_node : function (obj) {
  3938.                         var t1, t2, par, pos, tmp, i, j, k, l, c, top, lft;
  3939.                         if($.isArray(obj)) {
  3940.                                 obj = obj.slice();
  3941.                                 for(t1 = 0, t2 = obj.length; t1 < t2; t1++) {
  3942.                                         this.delete_node(obj[t1]);
  3943.                                 }
  3944.                                 return true;
  3945.                         }
  3946.                         obj = this.get_node(obj);
  3947.                         if(!obj || obj.id === $.jstree.root) { return false; }
  3948.                         par = this.get_node(obj.parent);
  3949.                         pos = $.inArray(obj.id, par.children);
  3950.                         c = false;
  3951.                         if(!this.check("delete_node", obj, par, pos)) {
  3952.                                 this.settings.core.error.call(this, this._data.core.last_error);
  3953.                                 return false;
  3954.                         }
  3955.                         if(pos !== -1) {
  3956.                                 par.children = $.vakata.array_remove(par.children, pos);
  3957.                         }
  3958.                         tmp = obj.children_d.concat([]);
  3959.                         tmp.push(obj.id);
  3960.                         for(i = 0, j = obj.parents.length; i < j; i++) {
  3961.                                 this._model.data[obj.parents[i]].children_d = $.vakata.array_filter(this._model.data[obj.parents[i]].children_d, function (v) {
  3962.                                         return $.inArray(v, tmp) === -1;
  3963.                                 });
  3964.                         }
  3965.                         for(k = 0, l = tmp.length; k < l; k++) {
  3966.                                 if(this._model.data[tmp[k]].state.selected) {
  3967.                                         c = true;
  3968.                                         break;
  3969.                                 }
  3970.                         }
  3971.                         if (c) {
  3972.                                 this._data.core.selected = $.vakata.array_filter(this._data.core.selected, function (v) {
  3973.                                         return $.inArray(v, tmp) === -1;
  3974.                                 });
  3975.                         }
  3976.                         /**
  3977.                          * triggered when a node is deleted
  3978.                          * @event
  3979.                          * @name delete_node.jstree
  3980.                          * @param {Object} node
  3981.                          * @param {String} parent the parent's ID
  3982.                          */
  3983.                         this.trigger('delete_node', { "node" : obj, "parent" : par.id });
  3984.                         if(c) {
  3985.                                 this.trigger('changed', { 'action' : 'delete_node', 'node' : obj, 'selected' : this._data.core.selected, 'parent' : par.id });
  3986.                         }
  3987.                         for(k = 0, l = tmp.length; k < l; k++) {
  3988.                                 delete this._model.data[tmp[k]];
  3989.                         }
  3990.                         if($.inArray(this._data.core.focused, tmp) !== -1) {
  3991.                                 this._data.core.focused = null;
  3992.                                 top = this.element[0].scrollTop;
  3993.                                 lft = this.element[0].scrollLeft;
  3994.                                 if(par.id === $.jstree.root) {
  3995.                                         if (this._model.data[$.jstree.root].children[0]) {
  3996.                                                 this.get_node(this._model.data[$.jstree.root].children[0], true).children('.jstree-anchor').focus();
  3997.                                         }
  3998.                                 }
  3999.                                 else {
  4000.                                         this.get_node(par, true).children('.jstree-anchor').focus();
  4001.                                 }
  4002.                                 this.element[0].scrollTop  = top;
  4003.                                 this.element[0].scrollLeft = lft;
  4004.                         }
  4005.                         this.redraw_node(par, true);
  4006.                         return true;
  4007.                 },
  4008.                 /**
  4009.                  * check if an operation is premitted on the tree. Used internally.
  4010.                  * @private
  4011.                  * @name check(chk, obj, par, pos)
  4012.                  * @param  {String} chk the operation to check, can be "create_node", "rename_node", "delete_node", "copy_node" or "move_node"
  4013.                  * @param  {mixed} obj the node
  4014.                  * @param  {mixed} par the parent
  4015.                  * @param  {mixed} pos the position to insert at, or if "rename_node" - the new name
  4016.                  * @param  {mixed} more some various additional information, for example if a "move_node" operations is triggered by DND this will be the hovered node
  4017.                  * @return {Boolean}
  4018.                  */
  4019.                 check : function (chk, obj, par, pos, more) {
  4020.                         obj = obj && obj.id ? obj : this.get_node(obj);
  4021.                         par = par && par.id ? par : this.get_node(par);
  4022.                         var tmp = chk.match(/^move_node|copy_node|create_node$/i) ? par : obj,
  4023.                                 chc = this.settings.core.check_callback;
  4024.                         if(chk === "move_node" || chk === "copy_node") {
  4025.                                 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)) {
  4026.                                         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 }) };
  4027.                                         return false;
  4028.                                 }
  4029.                         }
  4030.                         if(tmp && tmp.data) { tmp = tmp.data; }
  4031.                         if(tmp && tmp.functions && (tmp.functions[chk] === false || tmp.functions[chk] === true)) {
  4032.                                 if(tmp.functions[chk] === false) {
  4033.                                         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 }) };
  4034.                                 }
  4035.                                 return tmp.functions[chk];
  4036.                         }
  4037.                         if(chc === false || ($.isFunction(chc) && chc.call(this, chk, obj, par, pos, more) === false) || (chc && chc[chk] === false)) {
  4038.                                 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 }) };
  4039.                                 return false;
  4040.                         }
  4041.                         return true;
  4042.                 },
  4043.                 /**
  4044.                  * get the last error
  4045.                  * @name last_error()
  4046.                  * @return {Object}
  4047.                  */
  4048.                 last_error : function () {
  4049.                         return this._data.core.last_error;
  4050.                 },
  4051.                 /**
  4052.                  * move a node to a new parent
  4053.                  * @name move_node(obj, par [, pos, callback, is_loaded])
  4054.                  * @param  {mixed} obj the node to move, pass an array to move multiple nodes
  4055.                  * @param  {mixed} par the new parent
  4056.                  * @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`
  4057.                  * @param  {function} callback a function to call once the move is completed, receives 3 arguments - the node, the new parent and the position
  4058.                  * @param  {Boolean} is_loaded internal parameter indicating if the parent node has been loaded
  4059.                  * @param  {Boolean} skip_redraw internal parameter indicating if the tree should be redrawn
  4060.                  * @param  {Boolean} instance internal parameter indicating if the node comes from another instance
  4061.                  * @trigger move_node.jstree
  4062.                  */
  4063.                 move_node : function (obj, par, pos, callback, is_loaded, skip_redraw, origin) {
  4064.                         var t1, t2, old_par, old_pos, new_par, old_ins, is_multi, dpc, tmp, i, j, k, l, p;
  4065.  
  4066.                         par = this.get_node(par);
  4067.                         pos = pos === undefined ? 0 : pos;
  4068.                         if(!par) { return false; }
  4069.                         if(!pos.toString().match(/^(before|after)$/) && !is_loaded && !this.is_loaded(par)) {
  4070.                                 return this.load_node(par, function () { this.move_node(obj, par, pos, callback, true, false, origin); });
  4071.                         }
  4072.  
  4073.                         if($.isArray(obj)) {
  4074.                                 if(obj.length === 1) {
  4075.                                         obj = obj[0];
  4076.                                 }
  4077.                                 else {
  4078.                                         //obj = obj.slice();
  4079.                                         for(t1 = 0, t2 = obj.length; t1 < t2; t1++) {
  4080.                                                 if((tmp = this.move_node(obj[t1], par, pos, callback, is_loaded, false, origin))) {
  4081.                                                         par = tmp;
  4082.                                                         pos = "after";
  4083.                                                 }
  4084.                                         }
  4085.                                         this.redraw();
  4086.                                         return true;
  4087.                                 }
  4088.                         }
  4089.                         obj = obj && obj.id ? obj : this.get_node(obj);
  4090.  
  4091.                         if(!obj || obj.id === $.jstree.root) { return false; }
  4092.  
  4093.                         old_par = (obj.parent || $.jstree.root).toString();
  4094.                         new_par = (!pos.toString().match(/^(before|after)$/) || par.id === $.jstree.root) ? par : this.get_node(par.parent);
  4095.                         old_ins = origin ? origin : (this._model.data[obj.id] ? this : $.jstree.reference(obj.id));
  4096.                         is_multi = !old_ins || !old_ins._id || (this._id !== old_ins._id);
  4097.                         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;
  4098.                         if(old_ins && old_ins._id) {
  4099.                                 obj = old_ins._model.data[obj.id];
  4100.                         }
  4101.  
  4102.                         if(is_multi) {
  4103.                                 if((tmp = this.copy_node(obj, par, pos, callback, is_loaded, false, origin))) {
  4104.                                         if(old_ins) { old_ins.delete_node(obj); }
  4105.                                         return tmp;
  4106.                                 }
  4107.                                 return false;
  4108.                         }
  4109.                         //var m = this._model.data;
  4110.                         if(par.id === $.jstree.root) {
  4111.                                 if(pos === "before") { pos = "first"; }
  4112.                                 if(pos === "after") { pos = "last"; }
  4113.                         }
  4114.                         switch(pos) {
  4115.                                 case "before":
  4116.                                         pos = $.inArray(par.id, new_par.children);
  4117.                                         break;
  4118.                                 case "after" :
  4119.                                         pos = $.inArray(par.id, new_par.children) + 1;
  4120.                                         break;
  4121.                                 case "inside":
  4122.                                 case "first":
  4123.                                         pos = 0;
  4124.                                         break;
  4125.                                 case "last":
  4126.                                         pos = new_par.children.length;
  4127.                                         break;
  4128.                                 default:
  4129.                                         if(!pos) { pos = 0; }
  4130.                                         break;
  4131.                         }
  4132.                         if(pos > new_par.children.length) { pos = new_par.children.length; }
  4133.                         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) })) {
  4134.                                 this.settings.core.error.call(this, this._data.core.last_error);
  4135.                                 return false;
  4136.                         }
  4137.                         if(obj.parent === new_par.id) {
  4138.                                 dpc = new_par.children.concat();
  4139.                                 tmp = $.inArray(obj.id, dpc);
  4140.                                 if(tmp !== -1) {
  4141.                                         dpc = $.vakata.array_remove(dpc, tmp);
  4142.                                         if(pos > tmp) { pos--; }
  4143.                                 }
  4144.                                 tmp = [];
  4145.                                 for(i = 0, j = dpc.length; i < j; i++) {
  4146.                                         tmp[i >= pos ? i+1 : i] = dpc[i];
  4147.                                 }
  4148.                                 tmp[pos] = obj.id;
  4149.                                 new_par.children = tmp;
  4150.                                 this._node_changed(new_par.id);
  4151.                                 this.redraw(new_par.id === $.jstree.root);
  4152.                         }
  4153.                         else {
  4154.                                 // clean old parent and up
  4155.                                 tmp = obj.children_d.concat();
  4156.                                 tmp.push(obj.id);
  4157.                                 for(i = 0, j = obj.parents.length; i < j; i++) {
  4158.                                         dpc = [];
  4159.                                         p = old_ins._model.data[obj.parents[i]].children_d;
  4160.                                         for(k = 0, l = p.length; k < l; k++) {
  4161.                                                 if($.inArray(p[k], tmp) === -1) {
  4162.                                                         dpc.push(p[k]);
  4163.                                                 }
  4164.                                         }
  4165.                                         old_ins._model.data[obj.parents[i]].children_d = dpc;
  4166.                                 }
  4167.                                 old_ins._model.data[old_par].children = $.vakata.array_remove_item(old_ins._model.data[old_par].children, obj.id);
  4168.  
  4169.                                 // insert into new parent and up
  4170.                                 for(i = 0, j = new_par.parents.length; i < j; i++) {
  4171.                                         this._model.data[new_par.parents[i]].children_d = this._model.data[new_par.parents[i]].children_d.concat(tmp);
  4172.                                 }
  4173.                                 dpc = [];
  4174.                                 for(i = 0, j = new_par.children.length; i < j; i++) {
  4175.                                         dpc[i >= pos ? i+1 : i] = new_par.children[i];
  4176.                                 }
  4177.                                 dpc[pos] = obj.id;
  4178.                                 new_par.children = dpc;
  4179.                                 new_par.children_d.push(obj.id);
  4180.                                 new_par.children_d = new_par.children_d.concat(obj.children_d);
  4181.  
  4182.                                 // update object
  4183.                                 obj.parent = new_par.id;
  4184.                                 tmp = new_par.parents.concat();
  4185.                                 tmp.unshift(new_par.id);
  4186.                                 p = obj.parents.length;
  4187.                                 obj.parents = tmp;
  4188.  
  4189.                                 // update object children
  4190.                                 tmp = tmp.concat();
  4191.                                 for(i = 0, j = obj.children_d.length; i < j; i++) {
  4192.                                         this._model.data[obj.children_d[i]].parents = this._model.data[obj.children_d[i]].parents.slice(0,p*-1);
  4193.                                         Array.prototype.push.apply(this._model.data[obj.children_d[i]].parents, tmp);
  4194.                                 }
  4195.  
  4196.                                 if(old_par === $.jstree.root || new_par.id === $.jstree.root) {
  4197.                                         this._model.force_full_redraw = true;
  4198.                                 }
  4199.                                 if(!this._model.force_full_redraw) {
  4200.                                         this._node_changed(old_par);
  4201.                                         this._node_changed(new_par.id);
  4202.                                 }
  4203.                                 if(!skip_redraw) {
  4204.                                         this.redraw();
  4205.                                 }
  4206.                         }
  4207.                         if(callback) { callback.call(this, obj, new_par, pos); }
  4208.                         /**
  4209.                          * triggered when a node is moved
  4210.                          * @event
  4211.                          * @name move_node.jstree
  4212.                          * @param {Object} node
  4213.                          * @param {String} parent the parent's ID
  4214.                          * @param {Number} position the position of the node among the parent's children
  4215.                          * @param {String} old_parent the old parent of the node
  4216.                          * @param {Number} old_position the old position of the node
  4217.                          * @param {Boolean} is_multi do the node and new parent belong to different instances
  4218.                          * @param {jsTree} old_instance the instance the node came from
  4219.                          * @param {jsTree} new_instance the instance of the new parent
  4220.                          */
  4221.                         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 });
  4222.                         return obj.id;
  4223.                 },
  4224.                 /**
  4225.                  * copy a node to a new parent
  4226.                  * @name copy_node(obj, par [, pos, callback, is_loaded])
  4227.                  * @param  {mixed} obj the node to copy, pass an array to copy multiple nodes
  4228.                  * @param  {mixed} par the new parent
  4229.                  * @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`
  4230.                  * @param  {function} callback a function to call once the move is completed, receives 3 arguments - the node, the new parent and the position
  4231.                  * @param  {Boolean} is_loaded internal parameter indicating if the parent node has been loaded
  4232.                  * @param  {Boolean} skip_redraw internal parameter indicating if the tree should be redrawn
  4233.                  * @param  {Boolean} instance internal parameter indicating if the node comes from another instance
  4234.                  * @trigger model.jstree copy_node.jstree
  4235.                  */
  4236.                 copy_node : function (obj, par, pos, callback, is_loaded, skip_redraw, origin) {
  4237.                         var t1, t2, dpc, tmp, i, j, node, old_par, new_par, old_ins, is_multi;
  4238.  
  4239.                         par = this.get_node(par);
  4240.                         pos = pos === undefined ? 0 : pos;
  4241.                         if(!par) { return false; }
  4242.                         if(!pos.toString().match(/^(before|after)$/) && !is_loaded && !this.is_loaded(par)) {
  4243.                                 return this.load_node(par, function () { this.copy_node(obj, par, pos, callback, true, false, origin); });
  4244.                         }
  4245.  
  4246.                         if($.isArray(obj)) {
  4247.                                 if(obj.length === 1) {
  4248.                                         obj = obj[0];
  4249.                                 }
  4250.                                 else {
  4251.                                         //obj = obj.slice();
  4252.                                         for(t1 = 0, t2 = obj.length; t1 < t2; t1++) {
  4253.                                                 if((tmp = this.copy_node(obj[t1], par, pos, callback, is_loaded, true, origin))) {
  4254.                                                         par = tmp;
  4255.                                                         pos = "after";
  4256.                                                 }
  4257.                                         }
  4258.                                         this.redraw();
  4259.                                         return true;
  4260.                                 }
  4261.                         }
  4262.                         obj = obj && obj.id ? obj : this.get_node(obj);
  4263.                         if(!obj || obj.id === $.jstree.root) { return false; }
  4264.  
  4265.                         old_par = (obj.parent || $.jstree.root).toString();
  4266.                         new_par = (!pos.toString().match(/^(before|after)$/) || par.id === $.jstree.root) ? par : this.get_node(par.parent);
  4267.                         old_ins = origin ? origin : (this._model.data[obj.id] ? this : $.jstree.reference(obj.id));
  4268.                         is_multi = !old_ins || !old_ins._id || (this._id !== old_ins._id);
  4269.  
  4270.                         if(old_ins && old_ins._id) {
  4271.                                 obj = old_ins._model.data[obj.id];
  4272.                         }
  4273.  
  4274.                         if(par.id === $.jstree.root) {
  4275.                                 if(pos === "before") { pos = "first"; }
  4276.                                 if(pos === "after") { pos = "last"; }
  4277.                         }
  4278.                         switch(pos) {
  4279.                                 case "before":
  4280.                                         pos = $.inArray(par.id, new_par.children);
  4281.                                         break;
  4282.                                 case "after" :
  4283.                                         pos = $.inArray(par.id, new_par.children) + 1;
  4284.                                         break;
  4285.                                 case "inside":
  4286.                                 case "first":
  4287.                                         pos = 0;
  4288.                                         break;
  4289.                                 case "last":
  4290.                                         pos = new_par.children.length;
  4291.                                         break;
  4292.                                 default:
  4293.                                         if(!pos) { pos = 0; }
  4294.                                         break;
  4295.                         }
  4296.                         if(pos > new_par.children.length) { pos = new_par.children.length; }
  4297.                         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) })) {
  4298.                                 this.settings.core.error.call(this, this._data.core.last_error);
  4299.                                 return false;
  4300.                         }
  4301.                         node = old_ins ? old_ins.get_json(obj, { no_id : true, no_data : true, no_state : true }) : obj;
  4302.                         if(!node) { return false; }
  4303.                         if(node.id === true) { delete node.id; }
  4304.                         node = this._parse_model_from_json(node, new_par.id, new_par.parents.concat());
  4305.                         if(!node) { return false; }
  4306.                         tmp = this.get_node(node);
  4307.                         if(obj && obj.state && obj.state.loaded === false) { tmp.state.loaded = false; }
  4308.                         dpc = [];
  4309.                         dpc.push(node);
  4310.                         dpc = dpc.concat(tmp.children_d);
  4311.                         this.trigger('model', { "nodes" : dpc, "parent" : new_par.id });
  4312.  
  4313.                         // insert into new parent and up
  4314.                         for(i = 0, j = new_par.parents.length; i < j; i++) {
  4315.                                 this._model.data[new_par.parents[i]].children_d = this._model.data[new_par.parents[i]].children_d.concat(dpc);
  4316.                         }
  4317.                         dpc = [];
  4318.                         for(i = 0, j = new_par.children.length; i < j; i++) {
  4319.                                 dpc[i >= pos ? i+1 : i] = new_par.children[i];
  4320.                         }
  4321.                         dpc[pos] = tmp.id;
  4322.                         new_par.children = dpc;
  4323.                         new_par.children_d.push(tmp.id);
  4324.                         new_par.children_d = new_par.children_d.concat(tmp.children_d);
  4325.  
  4326.                         if(new_par.id === $.jstree.root) {
  4327.                                 this._model.force_full_redraw = true;
  4328.                         }
  4329.                         if(!this._model.force_full_redraw) {
  4330.                                 this._node_changed(new_par.id);
  4331.                         }
  4332.                         if(!skip_redraw) {
  4333.                                 this.redraw(new_par.id === $.jstree.root);
  4334.                         }
  4335.                         if(callback) { callback.call(this, tmp, new_par, pos); }
  4336.                         /**
  4337.                          * triggered when a node is copied
  4338.                          * @event
  4339.                          * @name copy_node.jstree
  4340.                          * @param {Object} node the copied node
  4341.                          * @param {Object} original the original node
  4342.                          * @param {String} parent the parent's ID
  4343.                          * @param {Number} position the position of the node among the parent's children
  4344.                          * @param {String} old_parent the old parent of the node
  4345.                          * @param {Number} old_position the position of the original node
  4346.                          * @param {Boolean} is_multi do the node and new parent belong to different instances
  4347.                          * @param {jsTree} old_instance the instance the node came from
  4348.                          * @param {jsTree} new_instance the instance of the new parent
  4349.                          */
  4350.                         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 });
  4351.                         return tmp.id;
  4352.                 },
  4353.                 /**
  4354.                  * cut a node (a later call to `paste(obj)` would move the node)
  4355.                  * @name cut(obj)
  4356.                  * @param  {mixed} obj multiple objects can be passed using an array
  4357.                  * @trigger cut.jstree
  4358.                  */
  4359.                 cut : function (obj) {
  4360.                         if(!obj) { obj = this._data.core.selected.concat(); }
  4361.                         if(!$.isArray(obj)) { obj = [obj]; }
  4362.                         if(!obj.length) { return false; }
  4363.                         var tmp = [], o, t1, t2;
  4364.                         for(t1 = 0, t2 = obj.length; t1 < t2; t1++) {
  4365.                                 o = this.get_node(obj[t1]);
  4366.                                 if(o && o.id && o.id !== $.jstree.root) { tmp.push(o); }
  4367.                         }
  4368.                         if(!tmp.length) { return false; }
  4369.                         ccp_node = tmp;
  4370.                         ccp_inst = this;
  4371.                         ccp_mode = 'move_node';
  4372.                         /**
  4373.                          * triggered when nodes are added to the buffer for moving
  4374.                          * @event
  4375.                          * @name cut.jstree
  4376.                          * @param {Array} node
  4377.                          */
  4378.                         this.trigger('cut', { "node" : obj });
  4379.                 },
  4380.                 /**
  4381.                  * copy a node (a later call to `paste(obj)` would copy the node)
  4382.                  * @name copy(obj)
  4383.                  * @param  {mixed} obj multiple objects can be passed using an array
  4384.                  * @trigger copy.jstree
  4385.                  */
  4386.                 copy : function (obj) {
  4387.                         if(!obj) { obj = this._data.core.selected.concat(); }
  4388.                         if(!$.isArray(obj)) { obj = [obj]; }
  4389.                         if(!obj.length) { return false; }
  4390.                         var tmp = [], o, t1, t2;
  4391.                         for(t1 = 0, t2 = obj.length; t1 < t2; t1++) {
  4392.                                 o = this.get_node(obj[t1]);
  4393.                                 if(o && o.id && o.id !== $.jstree.root) { tmp.push(o); }
  4394.                         }
  4395.                         if(!tmp.length) { return false; }
  4396.                         ccp_node = tmp;
  4397.                         ccp_inst = this;
  4398.                         ccp_mode = 'copy_node';
  4399.                         /**
  4400.                          * triggered when nodes are added to the buffer for copying
  4401.                          * @event
  4402.                          * @name copy.jstree
  4403.                          * @param {Array} node
  4404.                          */
  4405.                         this.trigger('copy', { "node" : obj });
  4406.                 },
  4407.                 /**
  4408.                  * get the current buffer (any nodes that are waiting for a paste operation)
  4409.                  * @name get_buffer()
  4410.                  * @return {Object} an object consisting of `mode` ("copy_node" or "move_node"), `node` (an array of objects) and `inst` (the instance)
  4411.                  */
  4412.                 get_buffer : function () {
  4413.                         return { 'mode' : ccp_mode, 'node' : ccp_node, 'inst' : ccp_inst };
  4414.                 },
  4415.                 /**
  4416.                  * check if there is something in the buffer to paste
  4417.                  * @name can_paste()
  4418.                  * @return {Boolean}
  4419.                  */
  4420.                 can_paste : function () {
  4421.                         return ccp_mode !== false && ccp_node !== false; // && ccp_inst._model.data[ccp_node];
  4422.                 },
  4423.                 /**
  4424.                  * copy or move the previously cut or copied nodes to a new parent
  4425.                  * @name paste(obj [, pos])
  4426.                  * @param  {mixed} obj the new parent
  4427.                  * @param  {mixed} pos the position to insert at (besides integer, "first" and "last" are supported), defaults to integer `0`
  4428.                  * @trigger paste.jstree
  4429.                  */
  4430.                 paste : function (obj, pos) {
  4431.                         obj = this.get_node(obj);
  4432.                         if(!obj || !ccp_mode || !ccp_mode.match(/^(copy_node|move_node)$/) || !ccp_node) { return false; }
  4433.                         if(this[ccp_mode](ccp_node, obj, pos, false, false, false, ccp_inst)) {
  4434.                                 /**
  4435.                                  * triggered when paste is invoked
  4436.                                  * @event
  4437.                                  * @name paste.jstree
  4438.                                  * @param {String} parent the ID of the receiving node
  4439.                                  * @param {Array} node the nodes in the buffer
  4440.                                  * @param {String} mode the performed operation - "copy_node" or "move_node"
  4441.                                  */
  4442.                                 this.trigger('paste', { "parent" : obj.id, "node" : ccp_node, "mode" : ccp_mode });
  4443.                         }
  4444.                         ccp_node = false;
  4445.                         ccp_mode = false;
  4446.                         ccp_inst = false;
  4447.                 },
  4448.                 /**
  4449.                  * clear the buffer of previously copied or cut nodes
  4450.                  * @name clear_buffer()
  4451.                  * @trigger clear_buffer.jstree
  4452.                  */
  4453.                 clear_buffer : function () {
  4454.                         ccp_node = false;
  4455.                         ccp_mode = false;
  4456.                         ccp_inst = false;
  4457.                         /**
  4458.                          * triggered when the copy / cut buffer is cleared
  4459.                          * @event
  4460.                          * @name clear_buffer.jstree
  4461.                          */
  4462.                         this.trigger('clear_buffer');
  4463.                 },
  4464.                 /**
  4465.                  * put a node in edit mode (input field to rename the node)
  4466.                  * @name edit(obj [, default_text, callback])
  4467.                  * @param  {mixed} obj
  4468.                  * @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)
  4469.                  * @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
  4470.                  */
  4471.                 edit : function (obj, default_text, callback) {
  4472.                         var rtl, w, a, s, t, h1, h2, fn, tmp, cancel = false;
  4473.                         obj = this.get_node(obj);
  4474.                         if(!obj) { return false; }
  4475.                         if(!this.check("edit", obj, this.get_parent(obj))) {
  4476.                                 this.settings.core.error.call(this, this._data.core.last_error);
  4477.                                 return false;
  4478.                         }
  4479.                         tmp = obj;
  4480.                         default_text = typeof default_text === 'string' ? default_text : obj.text;
  4481.                         this.set_text(obj, "");
  4482.                         obj = this._open_to(obj);
  4483.                         tmp.text = default_text;
  4484.  
  4485.                         rtl = this._data.core.rtl;
  4486.                         w  = this.element.width();
  4487.                         this._data.core.focused = tmp.id;
  4488.                         a  = obj.children('.jstree-anchor').focus();
  4489.                         s  = $('<span>');
  4490.                         /*!
  4491.                         oi = obj.children("i:visible"),
  4492.                         ai = a.children("i:visible"),
  4493.                         w1 = oi.width() * oi.length,
  4494.                         w2 = ai.width() * ai.length,
  4495.                         */
  4496.                         t  = default_text;
  4497.                         h1 = $("<"+"div />", { css : { "position" : "absolute", "top" : "-200px", "left" : (rtl ? "0px" : "-1000px"), "visibility" : "hidden" } }).appendTo(document.body);
  4498.                         h2 = $("<"+"input />", {
  4499.                                                 "value" : t,
  4500.                                                 "class" : "jstree-rename-input",
  4501.                                                 // "size" : t.length,
  4502.                                                 "css" : {
  4503.                                                         "padding" : "0",
  4504.                                                         "border" : "1px solid silver",
  4505.                                                         "box-sizing" : "border-box",
  4506.                                                         "display" : "inline-block",
  4507.                                                         "height" : (this._data.core.li_height) + "px",
  4508.                                                         "lineHeight" : (this._data.core.li_height) + "px",
  4509.                                                         "width" : "150px" // will be set a bit further down
  4510.                                                 },
  4511.                                                 "blur" : $.proxy(function (e) {
  4512.                                                         e.stopImmediatePropagation();
  4513.                                                         e.preventDefault();
  4514.                                                         var i = s.children(".jstree-rename-input"),
  4515.                                                                 v = i.val(),
  4516.                                                                 f = this.settings.core.force_text,
  4517.                                                                 nv;
  4518.                                                         if(v === "") { v = t; }
  4519.                                                         h1.remove();
  4520.                                                         s.replaceWith(a);
  4521.                                                         s.remove();
  4522.                                                         t = f ? t : $('<div></div>').append($.parseHTML(t)).html();
  4523.                                                         obj = this.get_node(obj);
  4524.                                                         this.set_text(obj, t);
  4525.                                                         nv = !!this.rename_node(obj, f ? $('<div></div>').text(v).text() : $('<div></div>').append($.parseHTML(v)).html());
  4526.                                                         if(!nv) {
  4527.                                                                 this.set_text(obj, t); // move this up? and fix #483
  4528.                                                         }
  4529.                                                         this._data.core.focused = tmp.id;
  4530.                                                         setTimeout($.proxy(function () {
  4531.                                                                 var node = this.get_node(tmp.id, true);
  4532.                                                                 if(node.length) {
  4533.                                                                         this._data.core.focused = tmp.id;
  4534.                                                                         node.children('.jstree-anchor').focus();
  4535.                                                                 }
  4536.                                                         }, this), 0);
  4537.                                                         if(callback) {
  4538.                                                                 callback.call(this, tmp, nv, cancel);
  4539.                                                         }
  4540.                                                         h2 = null;
  4541.                                                 }, this),
  4542.                                                 "keydown" : function (e) {
  4543.                                                         var key = e.which;
  4544.                                                         if(key === 27) {
  4545.                                                                 cancel = true;
  4546.                                                                 this.value = t;
  4547.                                                         }
  4548.                                                         if(key === 27 || key === 13 || key === 37 || key === 38 || key === 39 || key === 40 || key === 32) {
  4549.                                                                 e.stopImmediatePropagation();
  4550.                                                         }
  4551.                                                         if(key === 27 || key === 13) {
  4552.                                                                 e.preventDefault();
  4553.                                                                 this.blur();
  4554.                                                         }
  4555.                                                 },
  4556.                                                 "click" : function (e) { e.stopImmediatePropagation(); },
  4557.                                                 "mousedown" : function (e) { e.stopImmediatePropagation(); },
  4558.                                                 "keyup" : function (e) {
  4559.                                                         h2.width(Math.min(h1.text("pW" + this.value).width(),w));
  4560.                                                 },
  4561.                                                 "keypress" : function(e) {
  4562.                                                         if(e.which === 13) { return false; }
  4563.                                                 }
  4564.                                         });
  4565.                                 fn = {
  4566.                                                 fontFamily              : a.css('fontFamily')           || '',
  4567.                                                 fontSize                : a.css('fontSize')                     || '',
  4568.                                                 fontWeight              : a.css('fontWeight')           || '',
  4569.                                                 fontStyle               : a.css('fontStyle')            || '',
  4570.                                                 fontStretch             : a.css('fontStretch')          || '',
  4571.                                                 fontVariant             : a.css('fontVariant')          || '',
  4572.                                                 letterSpacing   : a.css('letterSpacing')        || '',
  4573.                                                 wordSpacing             : a.css('wordSpacing')          || ''
  4574.                                 };
  4575.                         s.attr('class', a.attr('class')).append(a.contents().clone()).append(h2);
  4576.                         a.replaceWith(s);
  4577.                         h1.css(fn);
  4578.                         h2.css(fn).width(Math.min(h1.text("pW" + h2[0].value).width(),w))[0].select();
  4579.                         $(document).one('mousedown.jstree touchstart.jstree dnd_start.vakata', function (e) {
  4580.                                 if (h2 && e.target !== h2) {
  4581.                                         $(h2).blur();
  4582.                                 }
  4583.                         });
  4584.                 },
  4585.  
  4586.  
  4587.                 /**
  4588.                  * changes the theme
  4589.                  * @name set_theme(theme_name [, theme_url])
  4590.                  * @param {String} theme_name the name of the new theme to apply
  4591.                  * @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.
  4592.                  * @trigger set_theme.jstree
  4593.                  */
  4594.                 set_theme : function (theme_name, theme_url) {
  4595.                         if(!theme_name) { return false; }
  4596.                         if(theme_url === true) {
  4597.                                 var dir = this.settings.core.themes.dir;
  4598.                                 if(!dir) { dir = $.jstree.path + '/themes'; }
  4599.                                 theme_url = dir + '/' + theme_name + '/style.css';
  4600.                         }
  4601.                         if(theme_url && $.inArray(theme_url, themes_loaded) === -1) {
  4602.                                 $('head').append('<'+'link rel="stylesheet" href="' + theme_url + '" type="text/css" />');
  4603.                                 themes_loaded.push(theme_url);
  4604.                         }
  4605.                         if(this._data.core.themes.name) {
  4606.                                 this.element.removeClass('jstree-' + this._data.core.themes.name);
  4607.                         }
  4608.                         this._data.core.themes.name = theme_name;
  4609.                         this.element.addClass('jstree-' + theme_name);
  4610.                         this.element[this.settings.core.themes.responsive ? 'addClass' : 'removeClass' ]('jstree-' + theme_name + '-responsive');
  4611.                         /**
  4612.                          * triggered when a theme is set
  4613.                          * @event
  4614.                          * @name set_theme.jstree
  4615.                          * @param {String} theme the new theme
  4616.                          */
  4617.                         this.trigger('set_theme', { 'theme' : theme_name });
  4618.                 },
  4619.                 /**
  4620.                  * gets the name of the currently applied theme name
  4621.                  * @name get_theme()
  4622.                  * @return {String}
  4623.                  */
  4624.                 get_theme : function () { return this._data.core.themes.name; },
  4625.                 /**
  4626.                  * changes the theme variant (if the theme has variants)
  4627.                  * @name set_theme_variant(variant_name)
  4628.                  * @param {String|Boolean} variant_name the variant to apply (if `false` is used the current variant is removed)
  4629.                  */
  4630.                 set_theme_variant : function (variant_name) {
  4631.                         if(this._data.core.themes.variant) {
  4632.                                 this.element.removeClass('jstree-' + this._data.core.themes.name + '-' + this._data.core.themes.variant);
  4633.                         }
  4634.                         this._data.core.themes.variant = variant_name;
  4635.                         if(variant_name) {
  4636.                                 this.element.addClass('jstree-' + this._data.core.themes.name + '-' + this._data.core.themes.variant);
  4637.                         }
  4638.                 },
  4639.                 /**
  4640.                  * gets the name of the currently applied theme variant
  4641.                  * @name get_theme()
  4642.                  * @return {String}
  4643.                  */
  4644.                 get_theme_variant : function () { return this._data.core.themes.variant; },
  4645.                 /**
  4646.                  * shows a striped background on the container (if the theme supports it)
  4647.                  * @name show_stripes()
  4648.                  */
  4649.                 show_stripes : function () {
  4650.                         this._data.core.themes.stripes = true;
  4651.                         this.get_container_ul().addClass("jstree-striped");
  4652.                         /**
  4653.                          * triggered when stripes are shown
  4654.                          * @event
  4655.                          * @name show_stripes.jstree
  4656.                          */
  4657.                         this.trigger('show_stripes');
  4658.                 },
  4659.                 /**
  4660.                  * hides the striped background on the container
  4661.                  * @name hide_stripes()
  4662.                  */
  4663.                 hide_stripes : function () {
  4664.                         this._data.core.themes.stripes = false;
  4665.                         this.get_container_ul().removeClass("jstree-striped");
  4666.                         /**
  4667.                          * triggered when stripes are hidden
  4668.                          * @event
  4669.                          * @name hide_stripes.jstree
  4670.                          */
  4671.                         this.trigger('hide_stripes');
  4672.                 },
  4673.                 /**
  4674.                  * toggles the striped background on the container
  4675.                  * @name toggle_stripes()
  4676.                  */
  4677.                 toggle_stripes : function () { if(this._data.core.themes.stripes) { this.hide_stripes(); } else { this.show_stripes(); } },
  4678.                 /**
  4679.                  * shows the connecting dots (if the theme supports it)
  4680.                  * @name show_dots()
  4681.                  */
  4682.                 show_dots : function () {
  4683.                         this._data.core.themes.dots = true;
  4684.                         this.get_container_ul().removeClass("jstree-no-dots");
  4685.                         /**
  4686.                          * triggered when dots are shown
  4687.                          * @event
  4688.                          * @name show_dots.jstree
  4689.                          */
  4690.                         this.trigger('show_dots');
  4691.                 },
  4692.                 /**
  4693.                  * hides the connecting dots
  4694.                  * @name hide_dots()
  4695.                  */
  4696.                 hide_dots : function () {
  4697.                         this._data.core.themes.dots = false;
  4698.                         this.get_container_ul().addClass("jstree-no-dots");
  4699.                         /**
  4700.                          * triggered when dots are hidden
  4701.                          * @event
  4702.                          * @name hide_dots.jstree
  4703.                          */
  4704.                         this.trigger('hide_dots');
  4705.                 },
  4706.                 /**
  4707.                  * toggles the connecting dots
  4708.                  * @name toggle_dots()
  4709.                  */
  4710.                 toggle_dots : function () { if(this._data.core.themes.dots) { this.hide_dots(); } else { this.show_dots(); } },
  4711.                 /**
  4712.                  * show the node icons
  4713.                  * @name show_icons()
  4714.                  */
  4715.                 show_icons : function () {
  4716.                         this._data.core.themes.icons = true;
  4717.                         this.get_container_ul().removeClass("jstree-no-icons");
  4718.                         /**
  4719.                          * triggered when icons are shown
  4720.                          * @event
  4721.                          * @name show_icons.jstree
  4722.                          */
  4723.                         this.trigger('show_icons');
  4724.                 },
  4725.                 /**
  4726.                  * hide the node icons
  4727.                  * @name hide_icons()
  4728.                  */
  4729.                 hide_icons : function () {
  4730.                         this._data.core.themes.icons = false;
  4731.                         this.get_container_ul().addClass("jstree-no-icons");
  4732.                         /**
  4733.                          * triggered when icons are hidden
  4734.                          * @event
  4735.                          * @name hide_icons.jstree
  4736.                          */
  4737.                         this.trigger('hide_icons');
  4738.                 },
  4739.                 /**
  4740.                  * toggle the node icons
  4741.                  * @name toggle_icons()
  4742.                  */
  4743.                 toggle_icons : function () { if(this._data.core.themes.icons) { this.hide_icons(); } else { this.show_icons(); } },
  4744.                 /**
  4745.                  * show the node ellipsis
  4746.                  * @name show_icons()
  4747.                  */
  4748.                 show_ellipsis : function () {
  4749.                         this._data.core.themes.ellipsis = true;
  4750.                         this.get_container_ul().addClass("jstree-ellipsis");
  4751.                         /**
  4752.                          * triggered when ellisis is shown
  4753.                          * @event
  4754.                          * @name show_ellipsis.jstree
  4755.                          */
  4756.                         this.trigger('show_ellipsis');
  4757.                 },
  4758.                 /**
  4759.                  * hide the node ellipsis
  4760.                  * @name hide_ellipsis()
  4761.                  */
  4762.                 hide_ellipsis : function () {
  4763.                         this._data.core.themes.ellipsis = false;
  4764.                         this.get_container_ul().removeClass("jstree-ellipsis");
  4765.                         /**
  4766.                          * triggered when ellisis is hidden
  4767.                          * @event
  4768.                          * @name hide_ellipsis.jstree
  4769.                          */
  4770.                         this.trigger('hide_ellipsis');
  4771.                 },
  4772.                 /**
  4773.                  * toggle the node ellipsis
  4774.                  * @name toggle_icons()
  4775.                  */
  4776.                 toggle_ellipsis : function () { if(this._data.core.themes.ellipsis) { this.hide_ellipsis(); } else { this.show_ellipsis(); } },
  4777.                 /**
  4778.                  * set the node icon for a node
  4779.                  * @name set_icon(obj, icon)
  4780.                  * @param {mixed} obj
  4781.                  * @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
  4782.                  */
  4783.                 set_icon : function (obj, icon) {
  4784.                         var t1, t2, dom, old;
  4785.                         if($.isArray(obj)) {
  4786.                                 obj = obj.slice();
  4787.                                 for(t1 = 0, t2 = obj.length; t1 < t2; t1++) {
  4788.                                         this.set_icon(obj[t1], icon);
  4789.                                 }
  4790.                                 return true;
  4791.                         }
  4792.                         obj = this.get_node(obj);
  4793.                         if(!obj || obj.id === $.jstree.root) { return false; }
  4794.                         old = obj.icon;
  4795.                         obj.icon = icon === true || icon === null || icon === undefined || icon === '' ? true : icon;
  4796.                         dom = this.get_node(obj, true).children(".jstree-anchor").children(".jstree-themeicon");
  4797.                         if(icon === false) {
  4798.                                 dom.removeClass('jstree-themeicon-custom ' + old).css("background","").removeAttr("rel");
  4799.                                 this.hide_icon(obj);
  4800.                         }
  4801.                         else if(icon === true || icon === null || icon === undefined || icon === '') {
  4802.                                 dom.removeClass('jstree-themeicon-custom ' + old).css("background","").removeAttr("rel");
  4803.                                 if(old === false) { this.show_icon(obj); }
  4804.                         }
  4805.                         else if(icon.indexOf("/") === -1 && icon.indexOf(".") === -1) {
  4806.                                 dom.removeClass(old).css("background","");
  4807.                                 dom.addClass(icon + ' jstree-themeicon-custom').attr("rel",icon);
  4808.                                 if(old === false) { this.show_icon(obj); }
  4809.                         }
  4810.                         else {
  4811.                                 dom.removeClass(old).css("background","");
  4812.                                 dom.addClass('jstree-themeicon-custom').css("background", "url('" + icon + "') center center no-repeat").attr("rel",icon);
  4813.                                 if(old === false) { this.show_icon(obj); }
  4814.                         }
  4815.                         return true;
  4816.                 },
  4817.                 /**
  4818.                  * get the node icon for a node
  4819.                  * @name get_icon(obj)
  4820.                  * @param {mixed} obj
  4821.                  * @return {String}
  4822.                  */
  4823.                 get_icon : function (obj) {
  4824.                         obj = this.get_node(obj);
  4825.                         return (!obj || obj.id === $.jstree.root) ? false : obj.icon;
  4826.                 },
  4827.                 /**
  4828.                  * hide the icon on an individual node
  4829.                  * @name hide_icon(obj)
  4830.                  * @param {mixed} obj
  4831.                  */
  4832.                 hide_icon : function (obj) {
  4833.                         var t1, t2;
  4834.                         if($.isArray(obj)) {
  4835.                                 obj = obj.slice();
  4836.                                 for(t1 = 0, t2 = obj.length; t1 < t2; t1++) {
  4837.                                         this.hide_icon(obj[t1]);
  4838.                                 }
  4839.                                 return true;
  4840.                         }
  4841.                         obj = this.get_node(obj);
  4842.                         if(!obj || obj === $.jstree.root) { return false; }
  4843.                         obj.icon = false;
  4844.                         this.get_node(obj, true).children(".jstree-anchor").children(".jstree-themeicon").addClass('jstree-themeicon-hidden');
  4845.                         return true;
  4846.                 },
  4847.                 /**
  4848.                  * show the icon on an individual node
  4849.                  * @name show_icon(obj)
  4850.                  * @param {mixed} obj
  4851.                  */
  4852.                 show_icon : function (obj) {
  4853.                         var t1, t2, dom;
  4854.                         if($.isArray(obj)) {
  4855.                                 obj = obj.slice();
  4856.                                 for(t1 = 0, t2 = obj.length; t1 < t2; t1++) {
  4857.                                         this.show_icon(obj[t1]);
  4858.                                 }
  4859.                                 return true;
  4860.                         }
  4861.                         obj = this.get_node(obj);
  4862.                         if(!obj || obj === $.jstree.root) { return false; }
  4863.                         dom = this.get_node(obj, true);
  4864.                         obj.icon = dom.length ? dom.children(".jstree-anchor").children(".jstree-themeicon").attr('rel') : true;
  4865.                         if(!obj.icon) { obj.icon = true; }
  4866.                         dom.children(".jstree-anchor").children(".jstree-themeicon").removeClass('jstree-themeicon-hidden');
  4867.                         return true;
  4868.                 }
  4869.         };
  4870.  
  4871.         // helpers
  4872.         $.vakata = {};
  4873.         // collect attributes
  4874.         $.vakata.attributes = function(node, with_values) {
  4875.                 node = $(node)[0];
  4876.                 var attr = with_values ? {} : [];
  4877.                 if(node && node.attributes) {
  4878.                         $.each(node.attributes, function (i, v) {
  4879.                                 if($.inArray(v.name.toLowerCase(),['style','contenteditable','hasfocus','tabindex']) !== -1) { return; }
  4880.                                 if(v.value !== null && $.trim(v.value) !== '') {
  4881.                                         if(with_values) { attr[v.name] = v.value; }
  4882.                                         else { attr.push(v.name); }
  4883.                                 }
  4884.                         });
  4885.                 }
  4886.                 return attr;
  4887.         };
  4888.         $.vakata.array_unique = function(array) {
  4889.                 var a = [], i, j, l, o = {};
  4890.                 for(i = 0, l = array.length; i < l; i++) {
  4891.                         if(o[array[i]] === undefined) {
  4892.                                 a.push(array[i]);
  4893.                                 o[array[i]] = true;
  4894.                         }
  4895.                 }
  4896.                 return a;
  4897.         };
  4898.         // remove item from array
  4899.         $.vakata.array_remove = function(array, from) {
  4900.                 array.splice(from, 1);
  4901.                 return array;
  4902.                 //var rest = array.slice((to || from) + 1 || array.length);
  4903.                 //array.length = from < 0 ? array.length + from : from;
  4904.                 //array.push.apply(array, rest);
  4905.                 //return array;
  4906.         };
  4907.         // remove item from array
  4908.         $.vakata.array_remove_item = function(array, item) {
  4909.                 var tmp = $.inArray(item, array);
  4910.                 return tmp !== -1 ? $.vakata.array_remove(array, tmp) : array;
  4911.         };
  4912.         $.vakata.array_filter = function(c,a,b,d,e) {
  4913.                 if (c.filter) {
  4914.                         return c.filter(a, b);
  4915.                 }
  4916.                 d=[];
  4917.                 for (e in c) {
  4918.                         if (~~e+''===e+'' && e>=0 && a.call(b,c[e],+e,c)) {
  4919.                                 d.push(c[e]);
  4920.                         }
  4921.                 }
  4922.                 return d;
  4923.         };
  4924.  
  4925.  
  4926. /**
  4927.  * ### Changed plugin
  4928.  *
  4929.  * 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.
  4930.  */
  4931.  
  4932.         $.jstree.plugins.changed = function (options, parent) {
  4933.                 var last = [];
  4934.                 this.trigger = function (ev, data) {
  4935.                         var i, j;
  4936.                         if(!data) {
  4937.                                 data = {};
  4938.                         }
  4939.                         if(ev.replace('.jstree','') === 'changed') {
  4940.                                 data.changed = { selected : [], deselected : [] };
  4941.                                 var tmp = {};
  4942.                                 for(i = 0, j = last.length; i < j; i++) {
  4943.                                         tmp[last[i]] = 1;
  4944.                                 }
  4945.                                 for(i = 0, j = data.selected.length; i < j; i++) {
  4946.                                         if(!tmp[data.selected[i]]) {
  4947.                                                 data.changed.selected.push(data.selected[i]);
  4948.                                         }
  4949.                                         else {
  4950.                                                 tmp[data.selected[i]] = 2;
  4951.                                         }
  4952.                                 }
  4953.                                 for(i = 0, j = last.length; i < j; i++) {
  4954.                                         if(tmp[last[i]] === 1) {
  4955.                                                 data.changed.deselected.push(last[i]);
  4956.                                         }
  4957.                                 }
  4958.                                 last = data.selected.slice();
  4959.                         }
  4960.                         /**
  4961.                          * triggered when selection changes (the "changed" plugin enhances the original event with more data)
  4962.                          * @event
  4963.                          * @name changed.jstree
  4964.                          * @param {Object} node
  4965.                          * @param {Object} action the action that caused the selection to change
  4966.                          * @param {Array} selected the current selection
  4967.                          * @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
  4968.                          * @param {Object} event the event (if any) that triggered this changed event
  4969.                          * @plugin changed
  4970.                          */
  4971.                         parent.trigger.call(this, ev, data);
  4972.                 };
  4973.                 this.refresh = function (skip_loading, forget_state) {
  4974.                         last = [];
  4975.                         return parent.refresh.apply(this, arguments);
  4976.                 };
  4977.         };
  4978.  
  4979. /**
  4980.  * ### Checkbox plugin
  4981.  *
  4982.  * This plugin renders checkbox icons in front of each node, making multiple selection much easier.
  4983.  * 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.
  4984.  */
  4985.  
  4986.         var _i = document.createElement('I');
  4987.         _i.className = 'jstree-icon jstree-checkbox';
  4988.         _i.setAttribute('role', 'presentation');
  4989.         /**
  4990.          * stores all defaults for the checkbox plugin
  4991.          * @name $.jstree.defaults.checkbox
  4992.          * @plugin checkbox
  4993.          */
  4994.         $.jstree.defaults.checkbox = {
  4995.                 /**
  4996.                  * a boolean indicating if checkboxes should be visible (can be changed at a later time using `show_checkboxes()` and `hide_checkboxes`). Defaults to `true`.
  4997.                  * @name $.jstree.defaults.checkbox.visible
  4998.                  * @plugin checkbox
  4999.                  */
  5000.                 visible                         : true,
  5001.                 /**
  5002.                  * a boolean indicating if checkboxes should cascade down and have an undetermined state. Defaults to `true`.
  5003.                  * @name $.jstree.defaults.checkbox.three_state
  5004.                  * @plugin checkbox
  5005.                  */
  5006.                 three_state                     : true,
  5007.                 /**
  5008.                  * a boolean indicating if clicking anywhere on the node should act as clicking on the checkbox. Defaults to `true`.
  5009.                  * @name $.jstree.defaults.checkbox.whole_node
  5010.                  * @plugin checkbox
  5011.                  */
  5012.                 whole_node                      : true,
  5013.                 /**
  5014.                  * a boolean indicating if the selected style of a node should be kept, or removed. Defaults to `true`.
  5015.                  * @name $.jstree.defaults.checkbox.keep_selected_style
  5016.                  * @plugin checkbox
  5017.                  */
  5018.                 keep_selected_style     : true,
  5019.                 /**
  5020.                  * This setting controls how cascading and undetermined nodes are applied.
  5021.                  * 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.
  5022.                  * If `three_state` is set to `true` this setting is automatically set to 'up+down+undetermined'. Defaults to ''.
  5023.                  * @name $.jstree.defaults.checkbox.cascade
  5024.                  * @plugin checkbox
  5025.                  */
  5026.                 cascade                         : '',
  5027.                 /**
  5028.                  * 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.
  5029.                  * @name $.jstree.defaults.checkbox.tie_selection
  5030.                  * @plugin checkbox
  5031.                  */
  5032.                 tie_selection           : true,
  5033.  
  5034.                 /**
  5035.                  * This setting controls if cascading down affects disabled checkboxes
  5036.                  * @name $.jstree.defaults.checkbox.cascade_to_disabled
  5037.                  * @plugin checkbox
  5038.                  */
  5039.                 cascade_to_disabled : true,
  5040.  
  5041.                 /**
  5042.                  * This setting controls if cascading down affects hidden checkboxes
  5043.                  * @name $.jstree.defaults.checkbox.cascade_to_hidden
  5044.                  * @plugin checkbox
  5045.                  */
  5046.                 cascade_to_hidden : true
  5047.         };
  5048.         $.jstree.plugins.checkbox = function (options, parent) {
  5049.                 this.bind = function () {
  5050.                         parent.bind.call(this);
  5051.                         this._data.checkbox.uto = false;
  5052.                         this._data.checkbox.selected = [];
  5053.                         if(this.settings.checkbox.three_state) {
  5054.                                 this.settings.checkbox.cascade = 'up+down+undetermined';
  5055.                         }
  5056.                         this.element
  5057.                                 .on("init.jstree", $.proxy(function () {
  5058.                                                 this._data.checkbox.visible = this.settings.checkbox.visible;
  5059.                                                 if(!this.settings.checkbox.keep_selected_style) {
  5060.                                                         this.element.addClass('jstree-checkbox-no-clicked');
  5061.                                                 }
  5062.                                                 if(this.settings.checkbox.tie_selection) {
  5063.                                                         this.element.addClass('jstree-checkbox-selection');
  5064.                                                 }
  5065.                                         }, this))
  5066.                                 .on("loading.jstree", $.proxy(function () {
  5067.                                                 this[ this._data.checkbox.visible ? 'show_checkboxes' : 'hide_checkboxes' ]();
  5068.                                         }, this));
  5069.                         if(this.settings.checkbox.cascade.indexOf('undetermined') !== -1) {
  5070.                                 this.element
  5071.                                         .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 () {
  5072.                                                         // only if undetermined is in setting
  5073.                                                         if(this._data.checkbox.uto) { clearTimeout(this._data.checkbox.uto); }
  5074.                                                         this._data.checkbox.uto = setTimeout($.proxy(this._undetermined, this), 50);
  5075.                                                 }, this));
  5076.                         }
  5077.                         if(!this.settings.checkbox.tie_selection) {
  5078.                                 this.element
  5079.                                         .on('model.jstree', $.proxy(function (e, data) {
  5080.                                                 var m = this._model.data,
  5081.                                                         p = m[data.parent],
  5082.                                                         dpc = data.nodes,
  5083.                                                         i, j;
  5084.                                                 for(i = 0, j = dpc.length; i < j; i++) {
  5085.                                                         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);
  5086.                                                         if(m[dpc[i]].state.checked) {
  5087.                                                                 this._data.checkbox.selected.push(dpc[i]);
  5088.                                                         }
  5089.                                                 }
  5090.                                         }, this));
  5091.                         }
  5092.                         if(this.settings.checkbox.cascade.indexOf('up') !== -1 || this.settings.checkbox.cascade.indexOf('down') !== -1) {
  5093.                                 this.element
  5094.                                         .on('model.jstree', $.proxy(function (e, data) {
  5095.                                                         var m = this._model.data,
  5096.                                                                 p = m[data.parent],
  5097.                                                                 dpc = data.nodes,
  5098.                                                                 chd = [],
  5099.                                                                 c, i, j, k, l, tmp, s = this.settings.checkbox.cascade, t = this.settings.checkbox.tie_selection;
  5100.  
  5101.                                                         if(s.indexOf('down') !== -1) {
  5102.                                                                 // apply down
  5103.                                                                 if(p.state[ t ? 'selected' : 'checked' ]) {
  5104.                                                                         for(i = 0, j = dpc.length; i < j; i++) {
  5105.                                                                                 m[dpc[i]].state[ t ? 'selected' : 'checked' ] = true;
  5106.                                                                         }
  5107.  
  5108.                                                                         this._data[ t ? 'core' : 'checkbox' ].selected = this._data[ t ? 'core' : 'checkbox' ].selected.concat(dpc);
  5109.                                                                 }
  5110.                                                                 else {
  5111.                                                                         for(i = 0, j = dpc.length; i < j; i++) {
  5112.                                                                                 if(m[dpc[i]].state[ t ? 'selected' : 'checked' ]) {
  5113.                                                                                         for(k = 0, l = m[dpc[i]].children_d.length; k < l; k++) {
  5114.                                                                                                 m[m[dpc[i]].children_d[k]].state[ t ? 'selected' : 'checked' ] = true;
  5115.                                                                                         }
  5116.                                                                                         this._data[ t ? 'core' : 'checkbox' ].selected = this._data[ t ? 'core' : 'checkbox' ].selected.concat(m[dpc[i]].children_d);
  5117.                                                                                 }
  5118.                                                                         }
  5119.                                                                 }
  5120.                                                         }
  5121.  
  5122.                                                         if(s.indexOf('up') !== -1) {
  5123.                                                                 // apply up
  5124.                                                                 for(i = 0, j = p.children_d.length; i < j; i++) {
  5125.                                                                         if(!m[p.children_d[i]].children.length) {
  5126.                                                                                 chd.push(m[p.children_d[i]].parent);
  5127.                                                                         }
  5128.                                                                 }
  5129.                                                                 chd = $.vakata.array_unique(chd);
  5130.                                                                 for(k = 0, l = chd.length; k < l; k++) {
  5131.                                                                         p = m[chd[k]];
  5132.                                                                         while(p && p.id !== $.jstree.root) {
  5133.                                                                                 c = 0;
  5134.                                                                                 for(i = 0, j = p.children.length; i < j; i++) {
  5135.                                                                                         c += m[p.children[i]].state[ t ? 'selected' : 'checked' ];
  5136.                                                                                 }
  5137.                                                                                 if(c === j) {
  5138.                                                                                         p.state[ t ? 'selected' : 'checked' ] = true;
  5139.                                                                                         this._data[ t ? 'core' : 'checkbox' ].selected.push(p.id);
  5140.                                                                                         tmp = this.get_node(p, true);
  5141.                                                                                         if(tmp && tmp.length) {
  5142.                                                                                                 tmp.attr('aria-selected', true).children('.jstree-anchor').addClass( t ? 'jstree-clicked' : 'jstree-checked');
  5143.                                                                                         }
  5144.                                                                                 }
  5145.                                                                                 else {
  5146.                                                                                         break;
  5147.                                                                                 }
  5148.                                                                                 p = this.get_node(p.parent);
  5149.                                                                         }
  5150.                                                                 }
  5151.                                                         }
  5152.  
  5153.                                                         this._data[ t ? 'core' : 'checkbox' ].selected = $.vakata.array_unique(this._data[ t ? 'core' : 'checkbox' ].selected);
  5154.                                                 }, this))
  5155.                                         .on(this.settings.checkbox.tie_selection ? 'select_node.jstree' : 'check_node.jstree', $.proxy(function (e, data) {
  5156.                                                         var self = this,
  5157.                                                                 obj = data.node,
  5158.                                                                 m = this._model.data,
  5159.                                                                 par = this.get_node(obj.parent),
  5160.                                                                 i, j, c, tmp, s = this.settings.checkbox.cascade, t = this.settings.checkbox.tie_selection,
  5161.                                                                 sel = {}, cur = this._data[ t ? 'core' : 'checkbox' ].selected;
  5162.  
  5163.                                                         for (i = 0, j = cur.length; i < j; i++) {
  5164.                                                                 sel[cur[i]] = true;
  5165.                                                         }
  5166.  
  5167.                                                         // apply down
  5168.                                                         if(s.indexOf('down') !== -1) {
  5169.                                                                 //this._data[ t ? 'core' : 'checkbox' ].selected = $.vakata.array_unique(this._data[ t ? 'core' : 'checkbox' ].selected.concat(obj.children_d));
  5170.                                                                 var selectedIds = this._cascade_new_checked_state(obj.id, true);
  5171.                                                                 var temp = obj.children_d.concat(obj.id);
  5172.                                                                 for (i = 0, j = temp.length; i < j; i++) {
  5173.                                                                         if (selectedIds.indexOf(temp[i]) > -1) {
  5174.                                                                                 sel[temp[i]] = true;
  5175.                                                                         }
  5176.                                                                         else {
  5177.                                                                                 delete sel[temp[i]];
  5178.                                                                         }
  5179.                                                                 }
  5180.                                                         }
  5181.  
  5182.                                                         // apply up
  5183.                                                         if(s.indexOf('up') !== -1) {
  5184.                                                                 while(par && par.id !== $.jstree.root) {
  5185.                                                                         c = 0;
  5186.                                                                         for(i = 0, j = par.children.length; i < j; i++) {
  5187.                                                                                 c += m[par.children[i]].state[ t ? 'selected' : 'checked' ];
  5188.                                                                         }
  5189.                                                                         if(c === j) {
  5190.                                                                                 par.state[ t ? 'selected' : 'checked' ] = true;
  5191.                                                                                 sel[par.id] = true;
  5192.                                                                                 //this._data[ t ? 'core' : 'checkbox' ].selected.push(par.id);
  5193.                                                                                 tmp = this.get_node(par, true);
  5194.                                                                                 if(tmp && tmp.length) {
  5195.                                                                                         tmp.attr('aria-selected', true).children('.jstree-anchor').addClass(t ? 'jstree-clicked' : 'jstree-checked');
  5196.                                                                                 }
  5197.                                                                         }
  5198.                                                                         else {
  5199.                                                                                 break;
  5200.                                                                         }
  5201.                                                                         par = this.get_node(par.parent);
  5202.                                                                 }
  5203.                                                         }
  5204.  
  5205.                                                         cur = [];
  5206.                                                         for (i in sel) {
  5207.                                                                 if (sel.hasOwnProperty(i)) {
  5208.                                                                         cur.push(i);
  5209.                                                                 }
  5210.                                                         }
  5211.                                                         this._data[ t ? 'core' : 'checkbox' ].selected = cur;
  5212.                                                 }, this))
  5213.                                         .on(this.settings.checkbox.tie_selection ? 'deselect_all.jstree' : 'uncheck_all.jstree', $.proxy(function (e, data) {
  5214.                                                         var obj = this.get_node($.jstree.root),
  5215.                                                                 m = this._model.data,
  5216.                                                                 i, j, tmp;
  5217.                                                         for(i = 0, j = obj.children_d.length; i < j; i++) {
  5218.                                                                 tmp = m[obj.children_d[i]];
  5219.                                                                 if(tmp && tmp.original && tmp.original.state && tmp.original.state.undetermined) {
  5220.                                                                         tmp.original.state.undetermined = false;
  5221.                                                                 }
  5222.                                                         }
  5223.                                                 }, this))
  5224.                                         .on(this.settings.checkbox.tie_selection ? 'deselect_node.jstree' : 'uncheck_node.jstree', $.proxy(function (e, data) {
  5225.                                                         var self = this,
  5226.                                                                 obj = data.node,
  5227.                                                                 dom = this.get_node(obj, true),
  5228.                                                                 i, j, tmp, s = this.settings.checkbox.cascade, t = this.settings.checkbox.tie_selection,
  5229.                                                                 cur = this._data[ t ? 'core' : 'checkbox' ].selected, sel = {},
  5230.                                                                 stillSelectedIds = [],
  5231.                                                                 allIds = obj.children_d.concat(obj.id);
  5232.  
  5233.                                                         // apply down
  5234.                                                         if(s.indexOf('down') !== -1) {
  5235.                                                                 var selectedIds = this._cascade_new_checked_state(obj.id, false);
  5236.  
  5237.                                                                 cur = cur.filter(function(id) {
  5238.                                                                         return allIds.indexOf(id) === -1 || selectedIds.indexOf(id) > -1;
  5239.                                                                 });
  5240.                                                         }
  5241.  
  5242.                                                         // only apply up if cascade up is enabled and if this node is not selected
  5243.                                                         // (if all child nodes are disabled and cascade_to_disabled === false then this node will till be selected).
  5244.                                                         if(s.indexOf('up') !== -1 && cur.indexOf(obj.id) === -1) {
  5245.                                                                 for(i = 0, j = obj.parents.length; i < j; i++) {
  5246.                                                                         tmp = this._model.data[obj.parents[i]];
  5247.                                                                         tmp.state[ t ? 'selected' : 'checked' ] = false;
  5248.                                                                         if(tmp && tmp.original && tmp.original.state && tmp.original.state.undetermined) {
  5249.                                                                                 tmp.original.state.undetermined = false;
  5250.                                                                         }
  5251.                                                                         tmp = this.get_node(obj.parents[i], true);
  5252.                                                                         if(tmp && tmp.length) {
  5253.                                                                                 tmp.attr('aria-selected', false).children('.jstree-anchor').removeClass(t ? 'jstree-clicked' : 'jstree-checked');
  5254.                                                                         }
  5255.                                                                 }
  5256.  
  5257.                                                                 cur = cur.filter(function(id) {
  5258.                                                                         return obj.parents.indexOf(id) === -1;
  5259.                                                                 });
  5260.                                                         }
  5261.  
  5262.                                                         this._data[ t ? 'core' : 'checkbox' ].selected = cur;
  5263.                                                 }, this));
  5264.                         }
  5265.                         if(this.settings.checkbox.cascade.indexOf('up') !== -1) {
  5266.                                 this.element
  5267.                                         .on('delete_node.jstree', $.proxy(function (e, data) {
  5268.                                                         // apply up (whole handler)
  5269.                                                         var p = this.get_node(data.parent),
  5270.                                                                 m = this._model.data,
  5271.                                                                 i, j, c, tmp, t = this.settings.checkbox.tie_selection;
  5272.                                                         while(p && p.id !== $.jstree.root && !p.state[ t ? 'selected' : 'checked' ]) {
  5273.                                                                 c = 0;
  5274.                                                                 for(i = 0, j = p.children.length; i < j; i++) {
  5275.                                                                         c += m[p.children[i]].state[ t ? 'selected' : 'checked' ];
  5276.                                                                 }
  5277.                                                                 if(j > 0 && c === j) {
  5278.                                                                         p.state[ t ? 'selected' : 'checked' ] = true;
  5279.                                                                         this._data[ t ? 'core' : 'checkbox' ].selected.push(p.id);
  5280.                                                                         tmp = this.get_node(p, true);
  5281.                                                                         if(tmp && tmp.length) {
  5282.                                                                                 tmp.attr('aria-selected', true).children('.jstree-anchor').addClass(t ? 'jstree-clicked' : 'jstree-checked');
  5283.                                                                         }
  5284.                                                                 }
  5285.                                                                 else {
  5286.                                                                         break;
  5287.                                                                 }
  5288.                                                                 p = this.get_node(p.parent);
  5289.                                                         }
  5290.                                                 }, this))
  5291.                                         .on('move_node.jstree', $.proxy(function (e, data) {
  5292.                                                         // apply up (whole handler)
  5293.                                                         var is_multi = data.is_multi,
  5294.                                                                 old_par = data.old_parent,
  5295.                                                                 new_par = this.get_node(data.parent),
  5296.                                                                 m = this._model.data,
  5297.                                                                 p, c, i, j, tmp, t = this.settings.checkbox.tie_selection;
  5298.                                                         if(!is_multi) {
  5299.                                                                 p = this.get_node(old_par);
  5300.                                                                 while(p && p.id !== $.jstree.root && !p.state[ t ? 'selected' : 'checked' ]) {
  5301.                                                                         c = 0;
  5302.                                                                         for(i = 0, j = p.children.length; i < j; i++) {
  5303.                                                                                 c += m[p.children[i]].state[ t ? 'selected' : 'checked' ];
  5304.                                                                         }
  5305.                                                                         if(j > 0 && c === j) {
  5306.                                                                                 p.state[ t ? 'selected' : 'checked' ] = true;
  5307.                                                                                 this._data[ t ? 'core' : 'checkbox' ].selected.push(p.id);
  5308.                                                                                 tmp = this.get_node(p, true);
  5309.                                                                                 if(tmp && tmp.length) {
  5310.                                                                                         tmp.attr('aria-selected', true).children('.jstree-anchor').addClass(t ? 'jstree-clicked' : 'jstree-checked');
  5311.                                                                                 }
  5312.                                                                         }
  5313.                                                                         else {
  5314.                                                                                 break;
  5315.                                                                         }
  5316.                                                                         p = this.get_node(p.parent);
  5317.                                                                 }
  5318.                                                         }
  5319.                                                         p = new_par;
  5320.                                                         while(p && p.id !== $.jstree.root) {
  5321.                                                                 c = 0;
  5322.                                                                 for(i = 0, j = p.children.length; i < j; i++) {
  5323.                                                                         c += m[p.children[i]].state[ t ? 'selected' : 'checked' ];
  5324.                                                                 }
  5325.                                                                 if(c === j) {
  5326.                                                                         if(!p.state[ t ? 'selected' : 'checked' ]) {
  5327.                                                                                 p.state[ t ? 'selected' : 'checked' ] = true;
  5328.                                                                                 this._data[ t ? 'core' : 'checkbox' ].selected.push(p.id);
  5329.                                                                                 tmp = this.get_node(p, true);
  5330.                                                                                 if(tmp && tmp.length) {
  5331.                                                                                         tmp.attr('aria-selected', true).children('.jstree-anchor').addClass(t ? 'jstree-clicked' : 'jstree-checked');
  5332.                                                                                 }
  5333.                                                                         }
  5334.                                                                 }
  5335.                                                                 else {
  5336.                                                                         if(p.state[ t ? 'selected' : 'checked' ]) {
  5337.                                                                                 p.state[ t ? 'selected' : 'checked' ] = false;
  5338.                                                                                 this._data[ t ? 'core' : 'checkbox' ].selected = $.vakata.array_remove_item(this._data[ t ? 'core' : 'checkbox' ].selected, p.id);
  5339.                                                                                 tmp = this.get_node(p, true);
  5340.                                                                                 if(tmp && tmp.length) {
  5341.                                                                                         tmp.attr('aria-selected', false).children('.jstree-anchor').removeClass(t ? 'jstree-clicked' : 'jstree-checked');
  5342.                                                                                 }
  5343.                                                                         }
  5344.                                                                         else {
  5345.                                                                                 break;
  5346.                                                                         }
  5347.                                                                 }
  5348.                                                                 p = this.get_node(p.parent);
  5349.                                                         }
  5350.                                                 }, this));
  5351.                         }
  5352.                 };
  5353.                 /**
  5354.                  * get an array of all nodes whose state is "undetermined"
  5355.                  * @name get_undetermined([full])
  5356.                  * @param  {boolean} full: if set to `true` the returned array will consist of the full node objects, otherwise - only IDs will be returned
  5357.                  * @return {Array}
  5358.                  * @plugin checkbox
  5359.                  */
  5360.                 this.get_undetermined = function (full) {
  5361.                         if (this.settings.checkbox.cascade.indexOf('undetermined') === -1) {
  5362.                                 return [];
  5363.                         }
  5364.                         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 = [];
  5365.                         for(i = 0, j = s.length; i < j; i++) {
  5366.                                 if(m[s[i]] && m[s[i]].parents) {
  5367.                                         for(k = 0, l = m[s[i]].parents.length; k < l; k++) {
  5368.                                                 if(o[m[s[i]].parents[k]] !== undefined) {
  5369.                                                         break;
  5370.                                                 }
  5371.                                                 if(m[s[i]].parents[k] !== $.jstree.root) {
  5372.                                                         o[m[s[i]].parents[k]] = true;
  5373.                                                         p.push(m[s[i]].parents[k]);
  5374.                                                 }
  5375.                                         }
  5376.                                 }
  5377.                         }
  5378.                         // attempt for server side undetermined state
  5379.                         this.element.find('.jstree-closed').not(':has(.jstree-children)')
  5380.                                 .each(function () {
  5381.                                         var tmp = tt.get_node(this), tmp2;
  5382.                                        
  5383.                                         if(!tmp) { return; }
  5384.                                        
  5385.                                         if(!tmp.state.loaded) {
  5386.                                                 if(tmp.original && tmp.original.state && tmp.original.state.undetermined && tmp.original.state.undetermined === true) {
  5387.                                                         if(o[tmp.id] === undefined && tmp.id !== $.jstree.root) {
  5388.                                                                 o[tmp.id] = true;
  5389.                                                                 p.push(tmp.id);
  5390.                                                         }
  5391.                                                         for(k = 0, l = tmp.parents.length; k < l; k++) {
  5392.                                                                 if(o[tmp.parents[k]] === undefined && tmp.parents[k] !== $.jstree.root) {
  5393.                                                                         o[tmp.parents[k]] = true;
  5394.                                                                         p.push(tmp.parents[k]);
  5395.                                                                 }
  5396.                                                         }
  5397.                                                 }
  5398.                                         }
  5399.                                         else {
  5400.                                                 for(i = 0, j = tmp.children_d.length; i < j; i++) {
  5401.                                                         tmp2 = m[tmp.children_d[i]];
  5402.                                                         if(!tmp2.state.loaded && tmp2.original && tmp2.original.state && tmp2.original.state.undetermined && tmp2.original.state.undetermined === true) {
  5403.                                                                 if(o[tmp2.id] === undefined && tmp2.id !== $.jstree.root) {
  5404.                                                                         o[tmp2.id] = true;
  5405.                                                                         p.push(tmp2.id);
  5406.                                                                 }
  5407.                                                                 for(k = 0, l = tmp2.parents.length; k < l; k++) {
  5408.                                                                         if(o[tmp2.parents[k]] === undefined && tmp2.parents[k] !== $.jstree.root) {
  5409.                                                                                 o[tmp2.parents[k]] = true;
  5410.                                                                                 p.push(tmp2.parents[k]);
  5411.                                                                         }
  5412.                                                                 }
  5413.                                                         }
  5414.                                                 }
  5415.                                         }
  5416.                                 });
  5417.                         for (i = 0, j = p.length; i < j; i++) {
  5418.                                 if(!m[p[i]].state[ t ? 'selected' : 'checked' ]) {
  5419.                                         r.push(full ? m[p[i]] : p[i]);
  5420.                                 }
  5421.                         }
  5422.                         return r;
  5423.                 };
  5424.                 /**
  5425.                  * set the undetermined state where and if necessary. Used internally.
  5426.                  * @private
  5427.                  * @name _undetermined()
  5428.                  * @plugin checkbox
  5429.                  */
  5430.                 this._undetermined = function () {
  5431.                         if(this.element === null) { return; }
  5432.                         var p = this.get_undetermined(false), i, j, s;
  5433.  
  5434.                         this.element.find('.jstree-undetermined').removeClass('jstree-undetermined');
  5435.                         for (i = 0, j = p.length; i < j; i++) {
  5436.                                 s = this.get_node(p[i], true);
  5437.                                 if(s && s.length) {
  5438.                                         s.children('.jstree-anchor').children('.jstree-checkbox').addClass('jstree-undetermined');
  5439.                                 }
  5440.                         }
  5441.                 };
  5442.                 this.redraw_node = function(obj, deep, is_callback, force_render) {
  5443.                         obj = parent.redraw_node.apply(this, arguments);
  5444.                         if(obj) {
  5445.                                 var i, j, tmp = null, icon = null;
  5446.                                 for(i = 0, j = obj.childNodes.length; i < j; i++) {
  5447.                                         if(obj.childNodes[i] && obj.childNodes[i].className && obj.childNodes[i].className.indexOf("jstree-anchor") !== -1) {
  5448.                                                 tmp = obj.childNodes[i];
  5449.                                                 break;
  5450.                                         }
  5451.                                 }
  5452.                                 if(tmp) {
  5453.                                         if(!this.settings.checkbox.tie_selection && this._model.data[obj.id].state.checked) { tmp.className += ' jstree-checked'; }
  5454.                                         icon = _i.cloneNode(false);
  5455.                                         if(this._model.data[obj.id].state.checkbox_disabled) { icon.className += ' jstree-checkbox-disabled'; }
  5456.                                         tmp.insertBefore(icon, tmp.childNodes[0]);
  5457.                                 }
  5458.                         }
  5459.                         if(!is_callback && this.settings.checkbox.cascade.indexOf('undetermined') !== -1) {
  5460.                                 if(this._data.checkbox.uto) { clearTimeout(this._data.checkbox.uto); }
  5461.                                 this._data.checkbox.uto = setTimeout($.proxy(this._undetermined, this), 50);
  5462.                         }
  5463.                         return obj;
  5464.                 };
  5465.                 /**
  5466.                  * show the node checkbox icons
  5467.                  * @name show_checkboxes()
  5468.                  * @plugin checkbox
  5469.                  */
  5470.                 this.show_checkboxes = function () { this._data.core.themes.checkboxes = true; this.get_container_ul().removeClass("jstree-no-checkboxes"); };
  5471.                 /**
  5472.                  * hide the node checkbox icons
  5473.                  * @name hide_checkboxes()
  5474.                  * @plugin checkbox
  5475.                  */
  5476.                 this.hide_checkboxes = function () { this._data.core.themes.checkboxes = false; this.get_container_ul().addClass("jstree-no-checkboxes"); };
  5477.                 /**
  5478.                  * toggle the node icons
  5479.                  * @name toggle_checkboxes()
  5480.                  * @plugin checkbox
  5481.                  */
  5482.                 this.toggle_checkboxes = function () { if(this._data.core.themes.checkboxes) { this.hide_checkboxes(); } else { this.show_checkboxes(); } };
  5483.                 /**
  5484.                  * checks if a node is in an undetermined state
  5485.                  * @name is_undetermined(obj)
  5486.                  * @param  {mixed} obj
  5487.                  * @return {Boolean}
  5488.                  */
  5489.                 this.is_undetermined = function (obj) {
  5490.                         obj = this.get_node(obj);
  5491.                         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;
  5492.                         if(!obj || obj.state[ t ? 'selected' : 'checked' ] === true || s.indexOf('undetermined') === -1 || (s.indexOf('down') === -1 && s.indexOf('up') === -1)) {
  5493.                                 return false;
  5494.                         }
  5495.                         if(!obj.state.loaded && obj.original.state.undetermined === true) {
  5496.                                 return true;
  5497.                         }
  5498.                         for(i = 0, j = obj.children_d.length; i < j; i++) {
  5499.                                 if($.inArray(obj.children_d[i], d) !== -1 || (!m[obj.children_d[i]].state.loaded && m[obj.children_d[i]].original.state.undetermined)) {
  5500.                                         return true;
  5501.                                 }
  5502.                         }
  5503.                         return false;
  5504.                 };
  5505.                 /**
  5506.                  * disable a node's checkbox
  5507.                  * @name disable_checkbox(obj)
  5508.                  * @param {mixed} obj an array can be used too
  5509.                  * @trigger disable_checkbox.jstree
  5510.                  * @plugin checkbox
  5511.                  */
  5512.                 this.disable_checkbox = function (obj) {
  5513.                         var t1, t2, dom;
  5514.                         if($.isArray(obj)) {
  5515.                                 obj = obj.slice();
  5516.                                 for(t1 = 0, t2 = obj.length; t1 < t2; t1++) {
  5517.                                         this.disable_checkbox(obj[t1]);
  5518.                                 }
  5519.                                 return true;
  5520.                         }
  5521.                         obj = this.get_node(obj);
  5522.                         if(!obj || obj.id === $.jstree.root) {
  5523.                                 return false;
  5524.                         }
  5525.                         dom = this.get_node(obj, true);
  5526.                         if(!obj.state.checkbox_disabled) {
  5527.                                 obj.state.checkbox_disabled = true;
  5528.                                 if(dom && dom.length) {
  5529.                                         dom.children('.jstree-anchor').children('.jstree-checkbox').addClass('jstree-checkbox-disabled');
  5530.                                 }
  5531.                                 /**
  5532.                                  * triggered when an node's checkbox is disabled
  5533.                                  * @event
  5534.                                  * @name disable_checkbox.jstree
  5535.                                  * @param {Object} node
  5536.                                  * @plugin checkbox
  5537.                                  */
  5538.                                 this.trigger('disable_checkbox', { 'node' : obj });
  5539.                         }
  5540.                 };
  5541.                 /**
  5542.                  * enable a node's checkbox
  5543.                  * @name disable_checkbox(obj)
  5544.                  * @param {mixed} obj an array can be used too
  5545.                  * @trigger enable_checkbox.jstree
  5546.                  * @plugin checkbox
  5547.                  */
  5548.                 this.enable_checkbox = function (obj) {
  5549.                         var t1, t2, dom;
  5550.                         if($.isArray(obj)) {
  5551.                                 obj = obj.slice();
  5552.                                 for(t1 = 0, t2 = obj.length; t1 < t2; t1++) {
  5553.                                         this.enable_checkbox(obj[t1]);
  5554.                                 }
  5555.                                 return true;
  5556.                         }
  5557.                         obj = this.get_node(obj);
  5558.                         if(!obj || obj.id === $.jstree.root) {
  5559.                                 return false;
  5560.                         }
  5561.                         dom = this.get_node(obj, true);
  5562.                         if(obj.state.checkbox_disabled) {
  5563.                                 obj.state.checkbox_disabled = false;
  5564.                                 if(dom && dom.length) {
  5565.                                         dom.children('.jstree-anchor').children('.jstree-checkbox').removeClass('jstree-checkbox-disabled');
  5566.                                 }
  5567.                                 /**
  5568.                                  * triggered when an node's checkbox is enabled
  5569.                                  * @event
  5570.                                  * @name enable_checkbox.jstree
  5571.                                  * @param {Object} node
  5572.                                  * @plugin checkbox
  5573.                                  */
  5574.                                 this.trigger('enable_checkbox', { 'node' : obj });
  5575.                         }
  5576.                 };
  5577.  
  5578.                 this.activate_node = function (obj, e) {
  5579.                         if($(e.target).hasClass('jstree-checkbox-disabled')) {
  5580.                                 return false;
  5581.                         }
  5582.                         if(this.settings.checkbox.tie_selection && (this.settings.checkbox.whole_node || $(e.target).hasClass('jstree-checkbox'))) {
  5583.                                 e.ctrlKey = true;
  5584.                         }
  5585.                         if(this.settings.checkbox.tie_selection || (!this.settings.checkbox.whole_node && !$(e.target).hasClass('jstree-checkbox'))) {
  5586.                                 return parent.activate_node.call(this, obj, e);
  5587.                         }
  5588.                         if(this.is_disabled(obj)) {
  5589.                                 return false;
  5590.                         }
  5591.                         if(this.is_checked(obj)) {
  5592.                                 this.uncheck_node(obj, e);
  5593.                         }
  5594.                         else {
  5595.                                 this.check_node(obj, e);
  5596.                         }
  5597.                         this.trigger('activate_node', { 'node' : this.get_node(obj) });
  5598.                 };
  5599.  
  5600.                 /**
  5601.                  * Cascades checked state to a node and all its descendants. This function does NOT affect hidden and disabled nodes (or their descendants).
  5602.                  * However if these unaffected nodes are already selected their ids will be included in the returned array.
  5603.                  * @private
  5604.                  * @param {string} id the node ID
  5605.                  * @param {bool} checkedState should the nodes be checked or not
  5606.                  * @returns {Array} Array of all node id's (in this tree branch) that are checked.
  5607.                  */
  5608.                 this._cascade_new_checked_state = function (id, checkedState) {
  5609.                         var self = this;
  5610.                         var t = this.settings.checkbox.tie_selection;
  5611.                         var node = this._model.data[id];
  5612.                         var selectedNodeIds = [];
  5613.                         var selectedChildrenIds = [], i, j, selectedChildIds;
  5614.  
  5615.                         if (
  5616.                                 (this.settings.checkbox.cascade_to_disabled || !node.state.disabled) &&
  5617.                                 (this.settings.checkbox.cascade_to_hidden || !node.state.hidden)
  5618.                         ) {
  5619.                                 //First try and check/uncheck the children
  5620.                                 if (node.children) {
  5621.                                         for (i = 0, j = node.children.length; i < j; i++) {
  5622.                                                 var childId = node.children[i];
  5623.                                                 selectedChildIds = self._cascade_new_checked_state(childId, checkedState);
  5624.                                                 selectedNodeIds = selectedNodeIds.concat(selectedChildIds);
  5625.                                                 if (selectedChildIds.indexOf(childId) > -1) {
  5626.                                                         selectedChildrenIds.push(childId);
  5627.                                                 }
  5628.                                         }
  5629.                                 }
  5630.  
  5631.                                 var dom = self.get_node(node, true);
  5632.  
  5633.                                 //A node's state is undetermined if some but not all of it's children are checked/selected .
  5634.                                 var undetermined = selectedChildrenIds.length > 0 && selectedChildrenIds.length < node.children.length;
  5635.  
  5636.                                 if(node.original && node.original.state && node.original.state.undetermined) {
  5637.                                         node.original.state.undetermined = undetermined;
  5638.                                 }
  5639.  
  5640.                                 //If a node is undetermined then remove selected class
  5641.                                 if (undetermined) {
  5642.                                         node.state[ t ? 'selected' : 'checked' ] = false;
  5643.                                         dom.attr('aria-selected', false).children('.jstree-anchor').removeClass(t ? 'jstree-clicked' : 'jstree-checked');
  5644.                                 }
  5645.                                 //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),
  5646.                                 //check the node and style it correctly.
  5647.                                 else if (checkedState && selectedChildrenIds.length === node.children.length) {
  5648.                                         node.state[ t ? 'selected' : 'checked' ] = checkedState;
  5649.                                         selectedNodeIds.push(node.id);
  5650.  
  5651.                                         dom.attr('aria-selected', true).children('.jstree-anchor').addClass(t ? 'jstree-clicked' : 'jstree-checked');
  5652.                                 }
  5653.                                 else {
  5654.                                         node.state[ t ? 'selected' : 'checked' ] = false;
  5655.                                         dom.attr('aria-selected', false).children('.jstree-anchor').removeClass(t ? 'jstree-clicked' : 'jstree-checked');
  5656.                                 }
  5657.                         }
  5658.                         else {
  5659.                                 selectedChildIds = this.get_checked_descendants(id);
  5660.  
  5661.                                 if (node.state[ t ? 'selected' : 'checked' ]) {
  5662.                                         selectedChildIds.push(node.id);
  5663.                                 }
  5664.  
  5665.                                 selectedNodeIds = selectedNodeIds.concat(selectedChildIds);
  5666.                         }
  5667.  
  5668.                         return selectedNodeIds;
  5669.                 };
  5670.  
  5671.                 /**
  5672.                  * Gets ids of nodes selected in branch (of tree) specified by id (does not include the node specified by id)
  5673.                  * @name get_checked_descendants(obj)
  5674.                  * @param {string} id the node ID
  5675.                  * @return {Array} array of IDs
  5676.                  * @plugin checkbox
  5677.                  */
  5678.                 this.get_checked_descendants = function (id) {
  5679.                         var self = this;
  5680.                         var t = self.settings.checkbox.tie_selection;
  5681.                         var node = self._model.data[id];
  5682.  
  5683.                         return node.children_d.filter(function(_id) {
  5684.                                 return self._model.data[_id].state[ t ? 'selected' : 'checked' ];
  5685.                         });
  5686.                 };
  5687.  
  5688.                 /**
  5689.                  * check a node (only if tie_selection in checkbox settings is false, otherwise select_node will be called internally)
  5690.                  * @name check_node(obj)
  5691.                  * @param {mixed} obj an array can be used to check multiple nodes
  5692.                  * @trigger check_node.jstree
  5693.                  * @plugin checkbox
  5694.                  */
  5695.                 this.check_node = function (obj, e) {
  5696.                         if(this.settings.checkbox.tie_selection) { return this.select_node(obj, false, true, e); }
  5697.                         var dom, t1, t2, th;
  5698.                         if($.isArray(obj)) {
  5699.                                 obj = obj.slice();
  5700.                                 for(t1 = 0, t2 = obj.length; t1 < t2; t1++) {
  5701.                                         this.check_node(obj[t1], e);
  5702.                                 }
  5703.                                 return true;
  5704.                         }
  5705.                         obj = this.get_node(obj);
  5706.                         if(!obj || obj.id === $.jstree.root) {
  5707.                                 return false;
  5708.                         }
  5709.                         dom = this.get_node(obj, true);
  5710.                         if(!obj.state.checked) {
  5711.                                 obj.state.checked = true;
  5712.                                 this._data.checkbox.selected.push(obj.id);
  5713.                                 if(dom && dom.length) {
  5714.                                         dom.children('.jstree-anchor').addClass('jstree-checked');
  5715.                                 }
  5716.                                 /**
  5717.                                  * triggered when an node is checked (only if tie_selection in checkbox settings is false)
  5718.                                  * @event
  5719.                                  * @name check_node.jstree
  5720.                                  * @param {Object} node
  5721.                                  * @param {Array} selected the current selection
  5722.                                  * @param {Object} event the event (if any) that triggered this check_node
  5723.                                  * @plugin checkbox
  5724.                                  */
  5725.                                 this.trigger('check_node', { 'node' : obj, 'selected' : this._data.checkbox.selected, 'event' : e });
  5726.                         }
  5727.                 };
  5728.                 /**
  5729.                  * uncheck a node (only if tie_selection in checkbox settings is false, otherwise deselect_node will be called internally)
  5730.                  * @name uncheck_node(obj)
  5731.                  * @param {mixed} obj an array can be used to uncheck multiple nodes
  5732.                  * @trigger uncheck_node.jstree
  5733.                  * @plugin checkbox
  5734.                  */
  5735.                 this.uncheck_node = function (obj, e) {
  5736.                         if(this.settings.checkbox.tie_selection) { return this.deselect_node(obj, false, e); }
  5737.                         var t1, t2, dom;
  5738.                         if($.isArray(obj)) {
  5739.                                 obj = obj.slice();
  5740.                                 for(t1 = 0, t2 = obj.length; t1 < t2; t1++) {
  5741.                                         this.uncheck_node(obj[t1], e);
  5742.                                 }
  5743.                                 return true;
  5744.                         }
  5745.                         obj = this.get_node(obj);
  5746.                         if(!obj || obj.id === $.jstree.root) {
  5747.                                 return false;
  5748.                         }
  5749.                         dom = this.get_node(obj, true);
  5750.                         if(obj.state.checked) {
  5751.                                 obj.state.checked = false;
  5752.                                 this._data.checkbox.selected = $.vakata.array_remove_item(this._data.checkbox.selected, obj.id);
  5753.                                 if(dom.length) {
  5754.                                         dom.children('.jstree-anchor').removeClass('jstree-checked');
  5755.                                 }
  5756.                                 /**
  5757.                                  * triggered when an node is unchecked (only if tie_selection in checkbox settings is false)
  5758.                                  * @event
  5759.                                  * @name uncheck_node.jstree
  5760.                                  * @param {Object} node
  5761.                                  * @param {Array} selected the current selection
  5762.                                  * @param {Object} event the event (if any) that triggered this uncheck_node
  5763.                                  * @plugin checkbox
  5764.                                  */
  5765.                                 this.trigger('uncheck_node', { 'node' : obj, 'selected' : this._data.checkbox.selected, 'event' : e });
  5766.                         }
  5767.                 };
  5768.                
  5769.                 /**
  5770.                  * checks all nodes in the tree (only if tie_selection in checkbox settings is false, otherwise select_all will be called internally)
  5771.                  * @name check_all()
  5772.                  * @trigger check_all.jstree, changed.jstree
  5773.                  * @plugin checkbox
  5774.                  */
  5775.                 this.check_all = function () {
  5776.                         if(this.settings.checkbox.tie_selection) { return this.select_all(); }
  5777.                         var tmp = this._data.checkbox.selected.concat([]), i, j;
  5778.                         this._data.checkbox.selected = this._model.data[$.jstree.root].children_d.concat();
  5779.                         for(i = 0, j = this._data.checkbox.selected.length; i < j; i++) {
  5780.                                 if(this._model.data[this._data.checkbox.selected[i]]) {
  5781.                                         this._model.data[this._data.checkbox.selected[i]].state.checked = true;
  5782.                                 }
  5783.                         }
  5784.                         this.redraw(true);
  5785.                         /**
  5786.                          * triggered when all nodes are checked (only if tie_selection in checkbox settings is false)
  5787.                          * @event
  5788.                          * @name check_all.jstree
  5789.                          * @param {Array} selected the current selection
  5790.                          * @plugin checkbox
  5791.                          */
  5792.                         this.trigger('check_all', { 'selected' : this._data.checkbox.selected });
  5793.                 };
  5794.                 /**
  5795.                  * uncheck all checked nodes (only if tie_selection in checkbox settings is false, otherwise deselect_all will be called internally)
  5796.                  * @name uncheck_all()
  5797.                  * @trigger uncheck_all.jstree
  5798.                  * @plugin checkbox
  5799.                  */
  5800.                 this.uncheck_all = function () {
  5801.                         if(this.settings.checkbox.tie_selection) { return this.deselect_all(); }
  5802.                         var tmp = this._data.checkbox.selected.concat([]), i, j;
  5803.                         for(i = 0, j = this._data.checkbox.selected.length; i < j; i++) {
  5804.                                 if(this._model.data[this._data.checkbox.selected[i]]) {
  5805.                                         this._model.data[this._data.checkbox.selected[i]].state.checked = false;
  5806.                                 }
  5807.                         }
  5808.                         this._data.checkbox.selected = [];
  5809.                         this.element.find('.jstree-checked').removeClass('jstree-checked');
  5810.                         /**
  5811.                          * triggered when all nodes are unchecked (only if tie_selection in checkbox settings is false)
  5812.                          * @event
  5813.                          * @name uncheck_all.jstree
  5814.                          * @param {Object} node the previous selection
  5815.                          * @param {Array} selected the current selection
  5816.                          * @plugin checkbox
  5817.                          */
  5818.                         this.trigger('uncheck_all', { 'selected' : this._data.checkbox.selected, 'node' : tmp });
  5819.                 };
  5820.                 /**
  5821.                  * checks if a node is checked (if tie_selection is on in the settings this function will return the same as is_selected)
  5822.                  * @name is_checked(obj)
  5823.                  * @param  {mixed}  obj
  5824.                  * @return {Boolean}
  5825.                  * @plugin checkbox
  5826.                  */
  5827.                 this.is_checked = function (obj) {
  5828.                         if(this.settings.checkbox.tie_selection) { return this.is_selected(obj); }
  5829.                         obj = this.get_node(obj);
  5830.                         if(!obj || obj.id === $.jstree.root) { return false; }
  5831.                         return obj.state.checked;
  5832.                 };
  5833.                 /**
  5834.                  * get an array of all checked nodes (if tie_selection is on in the settings this function will return the same as get_selected)
  5835.                  * @name get_checked([full])
  5836.                  * @param  {mixed}  full if set to `true` the returned array will consist of the full node objects, otherwise - only IDs will be returned
  5837.                  * @return {Array}
  5838.                  * @plugin checkbox
  5839.                  */
  5840.                 this.get_checked = function (full) {
  5841.                         if(this.settings.checkbox.tie_selection) { return this.get_selected(full); }
  5842.                         return full ? $.map(this._data.checkbox.selected, $.proxy(function (i) { return this.get_node(i); }, this)) : this._data.checkbox.selected;
  5843.                 };
  5844.                 /**
  5845.                  * 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)
  5846.                  * @name get_top_checked([full])
  5847.                  * @param  {mixed}  full if set to `true` the returned array will consist of the full node objects, otherwise - only IDs will be returned
  5848.                  * @return {Array}
  5849.                  * @plugin checkbox
  5850.                  */
  5851.                 this.get_top_checked = function (full) {
  5852.                         if(this.settings.checkbox.tie_selection) { return this.get_top_selected(full); }
  5853.                         var tmp = this.get_checked(true),
  5854.                                 obj = {}, i, j, k, l;
  5855.                         for(i = 0, j = tmp.length; i < j; i++) {
  5856.                                 obj[tmp[i].id] = tmp[i];
  5857.                         }
  5858.                         for(i = 0, j = tmp.length; i < j; i++) {
  5859.                                 for(k = 0, l = tmp[i].children_d.length; k < l; k++) {
  5860.                                         if(obj[tmp[i].children_d[k]]) {
  5861.                                                 delete obj[tmp[i].children_d[k]];
  5862.                                         }
  5863.                                 }
  5864.                         }
  5865.                         tmp = [];
  5866.                         for(i in obj) {
  5867.                                 if(obj.hasOwnProperty(i)) {
  5868.                                         tmp.push(i);
  5869.                                 }
  5870.                         }
  5871.                         return full ? $.map(tmp, $.proxy(function (i) { return this.get_node(i); }, this)) : tmp;
  5872.                 };
  5873.                 /**
  5874.                  * 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)
  5875.                  * @name get_bottom_checked([full])
  5876.                  * @param  {mixed}  full if set to `true` the returned array will consist of the full node objects, otherwise - only IDs will be returned
  5877.                  * @return {Array}
  5878.                  * @plugin checkbox
  5879.                  */
  5880.                 this.get_bottom_checked = function (full) {
  5881.                         if(this.settings.checkbox.tie_selection) { return this.get_bottom_selected(full); }
  5882.                         var tmp = this.get_checked(true),
  5883.                                 obj = [], i, j;
  5884.                         for(i = 0, j = tmp.length; i < j; i++) {
  5885.                                 if(!tmp[i].children.length) {
  5886.                                         obj.push(tmp[i].id);
  5887.                                 }
  5888.                         }
  5889.                         return full ? $.map(obj, $.proxy(function (i) { return this.get_node(i); }, this)) : obj;
  5890.                 };
  5891.                 this.load_node = function (obj, callback) {
  5892.                         var k, l, i, j, c, tmp;
  5893.                         if(!$.isArray(obj) && !this.settings.checkbox.tie_selection) {
  5894.                                 tmp = this.get_node(obj);
  5895.                                 if(tmp && tmp.state.loaded) {
  5896.                                         for(k = 0, l = tmp.children_d.length; k < l; k++) {
  5897.                                                 if(this._model.data[tmp.children_d[k]].state.checked) {
  5898.                                                         c = true;
  5899.                                                         this._data.checkbox.selected = $.vakata.array_remove_item(this._data.checkbox.selected, tmp.children_d[k]);
  5900.                                                 }
  5901.                                         }
  5902.                                 }
  5903.                         }
  5904.                         return parent.load_node.apply(this, arguments);
  5905.                 };
  5906.                 this.get_state = function () {
  5907.                         var state = parent.get_state.apply(this, arguments);
  5908.                         if(this.settings.checkbox.tie_selection) { return state; }
  5909.                         state.checkbox = this._data.checkbox.selected.slice();
  5910.                         return state;
  5911.                 };
  5912.                 this.set_state = function (state, callback) {
  5913.                         var res = parent.set_state.apply(this, arguments);
  5914.                         if(res && state.checkbox) {
  5915.                                 if(!this.settings.checkbox.tie_selection) {
  5916.                                         this.uncheck_all();
  5917.                                         var _this = this;
  5918.                                         $.each(state.checkbox, function (i, v) {
  5919.                                                 _this.check_node(v);
  5920.                                         });
  5921.                                 }
  5922.                                 delete state.checkbox;
  5923.                                 this.set_state(state, callback);
  5924.                                 return false;
  5925.                         }
  5926.                         return res;
  5927.                 };
  5928.                 this.refresh = function (skip_loading, forget_state) {
  5929.                         if(this.settings.checkbox.tie_selection) {
  5930.                                 this._data.checkbox.selected = [];
  5931.                         }
  5932.                         return parent.refresh.apply(this, arguments);
  5933.                 };
  5934.         };
  5935.  
  5936.         // include the checkbox plugin by default
  5937.         // $.jstree.defaults.plugins.push("checkbox");
  5938.  
  5939.  
  5940. /**
  5941.  * ### Conditionalselect plugin
  5942.  *
  5943.  * This plugin allows defining a callback to allow or deny node selection by user input (activate node method).
  5944.  */
  5945.  
  5946.         /**
  5947.          * 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`.
  5948.          * @name $.jstree.defaults.checkbox.visible
  5949.          * @plugin checkbox
  5950.          */
  5951.         $.jstree.defaults.conditionalselect = function () { return true; };
  5952.         $.jstree.plugins.conditionalselect = function (options, parent) {
  5953.                 // own function
  5954.                 this.activate_node = function (obj, e) {
  5955.                         if(this.settings.conditionalselect.call(this, this.get_node(obj), e)) {
  5956.                                 return parent.activate_node.call(this, obj, e);
  5957.                         }
  5958.                 };
  5959.         };
  5960.  
  5961.  
  5962. /**
  5963.  * ### Contextmenu plugin
  5964.  *
  5965.  * Shows a context menu when a node is right-clicked.
  5966.  */
  5967.  
  5968.         /**
  5969.          * stores all defaults for the contextmenu plugin
  5970.          * @name $.jstree.defaults.contextmenu
  5971.          * @plugin contextmenu
  5972.          */
  5973.         $.jstree.defaults.contextmenu = {
  5974.                 /**
  5975.                  * a boolean indicating if the node should be selected when the context menu is invoked on it. Defaults to `true`.
  5976.                  * @name $.jstree.defaults.contextmenu.select_node
  5977.                  * @plugin contextmenu
  5978.                  */
  5979.                 select_node : true,
  5980.                 /**
  5981.                  * a boolean indicating if the menu should be shown aligned with the node. Defaults to `true`, otherwise the mouse coordinates are used.
  5982.                  * @name $.jstree.defaults.contextmenu.show_at_node
  5983.                  * @plugin contextmenu
  5984.                  */
  5985.                 show_at_node : true,
  5986.                 /**
  5987.                  * 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).
  5988.                  *
  5989.                  * 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.
  5990.                  *
  5991.                  * * `separator_before` - a boolean indicating if there should be a separator before this item
  5992.                  * * `separator_after` - a boolean indicating if there should be a separator after this item
  5993.                  * * `_disabled` - a boolean indicating if this action should be disabled
  5994.                  * * `label` - a string - the name of the action (could be a function returning a string)
  5995.                  * * `title` - a string - an optional tooltip for the item
  5996.                  * * `action` - a function to be executed if this item is chosen, the function will receive
  5997.                  * * `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
  5998.                  * * `shortcut` - keyCode which will trigger the action if the menu is open (for example `113` for rename, which equals F2)
  5999.                  * * `shortcut_label` - shortcut label (like for example `F2` for rename)
  6000.                  * * `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
  6001.                  *
  6002.                  * @name $.jstree.defaults.contextmenu.items
  6003.                  * @plugin contextmenu
  6004.                  */
  6005.                 items : function (o, cb) { // Could be an object directly
  6006.                         return {
  6007.                                 "create" : {
  6008.                                         "separator_before"      : false,
  6009.                                         "separator_after"       : true,
  6010.                                         "_disabled"                     : false, //(this.check("create_node", data.reference, {}, "last")),
  6011.                                         "label"                         : "Create",
  6012.                                         "action"                        : function (data) {
  6013.                                                 var inst = $.jstree.reference(data.reference),
  6014.                                                         obj = inst.get_node(data.reference);
  6015.                                                 inst.create_node(obj, {}, "last", function (new_node) {
  6016.                                                         try {
  6017.                                                                 inst.edit(new_node);
  6018.                                                         } catch (ex) {
  6019.                                                                 setTimeout(function () { inst.edit(new_node); },0);
  6020.                                                         }
  6021.                                                 });
  6022.                                         }
  6023.                                 },
  6024.                                 "rename" : {
  6025.                                         "separator_before"      : false,
  6026.                                         "separator_after"       : false,
  6027.                                         "_disabled"                     : false, //(this.check("rename_node", data.reference, this.get_parent(data.reference), "")),
  6028.                                         "label"                         : "Rename",
  6029.                                         /*!
  6030.                                         "shortcut"                      : 113,
  6031.                                         "shortcut_label"        : 'F2',
  6032.                                         "icon"                          : "glyphicon glyphicon-leaf",
  6033.                                         */
  6034.                                         "action"                        : function (data) {
  6035.                                                 var inst = $.jstree.reference(data.reference),
  6036.                                                         obj = inst.get_node(data.reference);
  6037.                                                 inst.edit(obj);
  6038.                                         }
  6039.                                 },
  6040.                                 "remove" : {
  6041.                                         "separator_before"      : false,
  6042.                                         "icon"                          : false,
  6043.                                         "separator_after"       : false,
  6044.                                         "_disabled"                     : false, //(this.check("delete_node", data.reference, this.get_parent(data.reference), "")),
  6045.                                         "label"                         : "Delete",
  6046.                                         "action"                        : function (data) {
  6047.                                                 var inst = $.jstree.reference(data.reference),
  6048.                                                         obj = inst.get_node(data.reference);
  6049.                                                 if(inst.is_selected(obj)) {
  6050.                                                         inst.delete_node(inst.get_selected());
  6051.                                                 }
  6052.                                                 else {
  6053.                                                         inst.delete_node(obj);
  6054.                                                 }
  6055.                                         }
  6056.                                 },
  6057.                                 "ccp" : {
  6058.                                         "separator_before"      : true,
  6059.                                         "icon"                          : false,
  6060.                                         "separator_after"       : false,
  6061.                                         "label"                         : "Edit",
  6062.                                         "action"                        : false,
  6063.                                         "submenu" : {
  6064.                                                 "cut" : {
  6065.                                                         "separator_before"      : false,
  6066.                                                         "separator_after"       : false,
  6067.                                                         "label"                         : "Cut",
  6068.                                                         "action"                        : function (data) {
  6069.                                                                 var inst = $.jstree.reference(data.reference),
  6070.                                                                         obj = inst.get_node(data.reference);
  6071.                                                                 if(inst.is_selected(obj)) {
  6072.                                                                         inst.cut(inst.get_top_selected());
  6073.                                                                 }
  6074.                                                                 else {
  6075.                                                                         inst.cut(obj);
  6076.                                                                 }
  6077.                                                         }
  6078.                                                 },
  6079.                                                 "copy" : {
  6080.                                                         "separator_before"      : false,
  6081.                                                         "icon"                          : false,
  6082.                                                         "separator_after"       : false,
  6083.                                                         "label"                         : "Copy",
  6084.                                                         "action"                        : function (data) {
  6085.                                                                 var inst = $.jstree.reference(data.reference),
  6086.                                                                         obj = inst.get_node(data.reference);
  6087.                                                                 if(inst.is_selected(obj)) {
  6088.                                                                         inst.copy(inst.get_top_selected());
  6089.                                                                 }
  6090.                                                                 else {
  6091.                                                                         inst.copy(obj);
  6092.                                                                 }
  6093.                                                         }
  6094.                                                 },
  6095.                                                 "paste" : {
  6096.                                                         "separator_before"      : false,
  6097.                                                         "icon"                          : false,
  6098.                                                         "_disabled"                     : function (data) {
  6099.                                                                 return !$.jstree.reference(data.reference).can_paste();
  6100.                                                         },
  6101.                                                         "separator_after"       : false,
  6102.                                                         "label"                         : "Paste",
  6103.                                                         "action"                        : function (data) {
  6104.                                                                 var inst = $.jstree.reference(data.reference),
  6105.                                                                         obj = inst.get_node(data.reference);
  6106.                                                                 inst.paste(obj);
  6107.                                                         }
  6108.                                                 }
  6109.                                         }
  6110.                                 }
  6111.                         };
  6112.                 }
  6113.         };
  6114.  
  6115.         $.jstree.plugins.contextmenu = function (options, parent) {
  6116.                 this.bind = function () {
  6117.                         parent.bind.call(this);
  6118.  
  6119.                         var last_ts = 0, cto = null, ex, ey;
  6120.                         this.element
  6121.                                 .on("init.jstree loading.jstree ready.jstree", $.proxy(function () {
  6122.                                                 this.get_container_ul().addClass('jstree-contextmenu');
  6123.                                         }, this))
  6124.                                 .on("contextmenu.jstree", ".jstree-anchor", $.proxy(function (e, data) {
  6125.                                                 if (e.target.tagName.toLowerCase() === 'input') {
  6126.                                                         return;
  6127.                                                 }
  6128.                                                 e.preventDefault();
  6129.                                                 last_ts = e.ctrlKey ? +new Date() : 0;
  6130.                                                 if(data || cto) {
  6131.                                                         last_ts = (+new Date()) + 10000;
  6132.                                                 }
  6133.                                                 if(cto) {
  6134.                                                         clearTimeout(cto);
  6135.                                                 }
  6136.                                                 if(!this.is_loading(e.currentTarget)) {
  6137.                                                         this.show_contextmenu(e.currentTarget, e.pageX, e.pageY, e);
  6138.                                                 }
  6139.                                         }, this))
  6140.                                 .on("click.jstree", ".jstree-anchor", $.proxy(function (e) {
  6141.                                                 if(this._data.contextmenu.visible && (!last_ts || (+new Date()) - last_ts > 250)) { // work around safari & macOS ctrl+click
  6142.                                                         $.vakata.context.hide();
  6143.                                                 }
  6144.                                                 last_ts = 0;
  6145.                                         }, this))
  6146.                                 .on("touchstart.jstree", ".jstree-anchor", function (e) {
  6147.                                                 if(!e.originalEvent || !e.originalEvent.changedTouches || !e.originalEvent.changedTouches[0]) {
  6148.                                                         return;
  6149.                                                 }
  6150.                                                 ex = e.originalEvent.changedTouches[0].clientX;
  6151.                                                 ey = e.originalEvent.changedTouches[0].clientY;
  6152.                                                 cto = setTimeout(function () {
  6153.                                                         $(e.currentTarget).trigger('contextmenu', true);
  6154.                                                 }, 750);
  6155.                                         })
  6156.                                 .on('touchmove.vakata.jstree', function (e) {
  6157.                                                 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)) {
  6158.                                                         clearTimeout(cto);
  6159.                                                         $.vakata.context.hide();
  6160.                                                 }
  6161.                                         })
  6162.                                 .on('touchend.vakata.jstree', function (e) {
  6163.                                                 if(cto) {
  6164.                                                         clearTimeout(cto);
  6165.                                                 }
  6166.                                         });
  6167.  
  6168.                         /*!
  6169.                         if(!('oncontextmenu' in document.body) && ('ontouchstart' in document.body)) {
  6170.                                 var el = null, tm = null;
  6171.                                 this.element
  6172.                                         .on("touchstart", ".jstree-anchor", function (e) {
  6173.                                                 el = e.currentTarget;
  6174.                                                 tm = +new Date();
  6175.                                                 $(document).one("touchend", function (e) {
  6176.                                                         e.target = document.elementFromPoint(e.originalEvent.targetTouches[0].pageX - window.pageXOffset, e.originalEvent.targetTouches[0].pageY - window.pageYOffset);
  6177.                                                         e.currentTarget = e.target;
  6178.                                                         tm = ((+(new Date())) - tm);
  6179.                                                         if(e.target === el && tm > 600 && tm < 1000) {
  6180.                                                                 e.preventDefault();
  6181.                                                                 $(el).trigger('contextmenu', e);
  6182.                                                         }
  6183.                                                         el = null;
  6184.                                                         tm = null;
  6185.                                                 });
  6186.                                         });
  6187.                         }
  6188.                         */
  6189.                         $(document).on("context_hide.vakata.jstree", $.proxy(function (e, data) {
  6190.                                 this._data.contextmenu.visible = false;
  6191.                                 $(data.reference).removeClass('jstree-context');
  6192.                         }, this));
  6193.                 };
  6194.                 this.teardown = function () {
  6195.                         if(this._data.contextmenu.visible) {
  6196.                                 $.vakata.context.hide();
  6197.                         }
  6198.                         parent.teardown.call(this);
  6199.                 };
  6200.  
  6201.                 /**
  6202.                  * prepare and show the context menu for a node
  6203.                  * @name show_contextmenu(obj [, x, y])
  6204.                  * @param {mixed} obj the node
  6205.                  * @param {Number} x the x-coordinate relative to the document to show the menu at
  6206.                  * @param {Number} y the y-coordinate relative to the document to show the menu at
  6207.                  * @param {Object} e the event if available that triggered the contextmenu
  6208.                  * @plugin contextmenu
  6209.                  * @trigger show_contextmenu.jstree
  6210.                  */
  6211.                 this.show_contextmenu = function (obj, x, y, e) {
  6212.                         obj = this.get_node(obj);
  6213.                         if(!obj || obj.id === $.jstree.root) { return false; }
  6214.                         var s = this.settings.contextmenu,
  6215.                                 d = this.get_node(obj, true),
  6216.                                 a = d.children(".jstree-anchor"),
  6217.                                 o = false,
  6218.                                 i = false;
  6219.                         if(s.show_at_node || x === undefined || y === undefined) {
  6220.                                 o = a.offset();
  6221.                                 x = o.left;
  6222.                                 y = o.top + this._data.core.li_height;
  6223.                         }
  6224.                         if(this.settings.contextmenu.select_node && !this.is_selected(obj)) {
  6225.                                 this.activate_node(obj, e);
  6226.                         }
  6227.  
  6228.                         i = s.items;
  6229.                         if($.isFunction(i)) {
  6230.                                 i = i.call(this, obj, $.proxy(function (i) {
  6231.                                         this._show_contextmenu(obj, x, y, i);
  6232.                                 }, this));
  6233.                         }
  6234.                         if($.isPlainObject(i)) {
  6235.                                 this._show_contextmenu(obj, x, y, i);
  6236.                         }
  6237.                 };
  6238.                 /**
  6239.                  * show the prepared context menu for a node
  6240.                  * @name _show_contextmenu(obj, x, y, i)
  6241.                  * @param {mixed} obj the node
  6242.                  * @param {Number} x the x-coordinate relative to the document to show the menu at
  6243.                  * @param {Number} y the y-coordinate relative to the document to show the menu at
  6244.                  * @param {Number} i the object of items to show
  6245.                  * @plugin contextmenu
  6246.                  * @trigger show_contextmenu.jstree
  6247.                  * @private
  6248.                  */
  6249.                 this._show_contextmenu = function (obj, x, y, i) {
  6250.                         var d = this.get_node(obj, true),
  6251.                                 a = d.children(".jstree-anchor");
  6252.                         $(document).one("context_show.vakata.jstree", $.proxy(function (e, data) {
  6253.                                 var cls = 'jstree-contextmenu jstree-' + this.get_theme() + '-contextmenu';
  6254.                                 $(data.element).addClass(cls);
  6255.                                 a.addClass('jstree-context');
  6256.                         }, this));
  6257.                         this._data.contextmenu.visible = true;
  6258.                         $.vakata.context.show(a, { 'x' : x, 'y' : y }, i);
  6259.                         /**
  6260.                          * triggered when the contextmenu is shown for a node
  6261.                          * @event
  6262.                          * @name show_contextmenu.jstree
  6263.                          * @param {Object} node the node
  6264.                          * @param {Number} x the x-coordinate of the menu relative to the document
  6265.                          * @param {Number} y the y-coordinate of the menu relative to the document
  6266.                          * @plugin contextmenu
  6267.                          */
  6268.                         this.trigger('show_contextmenu', { "node" : obj, "x" : x, "y" : y });
  6269.                 };
  6270.         };
  6271.  
  6272.         // contextmenu helper
  6273.         (function ($) {
  6274.                 var right_to_left = false,
  6275.                         vakata_context = {
  6276.                                 element         : false,
  6277.                                 reference       : false,
  6278.                                 position_x      : 0,
  6279.                                 position_y      : 0,
  6280.                                 items           : [],
  6281.                                 html            : "",
  6282.                                 is_visible      : false
  6283.                         };
  6284.  
  6285.                 $.vakata.context = {
  6286.                         settings : {
  6287.                                 hide_onmouseleave       : 0,
  6288.                                 icons                           : true
  6289.                         },
  6290.                         _trigger : function (event_name) {
  6291.                                 $(document).triggerHandler("context_" + event_name + ".vakata", {
  6292.                                         "reference"     : vakata_context.reference,
  6293.                                         "element"       : vakata_context.element,
  6294.                                         "position"      : {
  6295.                                                 "x" : vakata_context.position_x,
  6296.                                                 "y" : vakata_context.position_y
  6297.                                         }
  6298.                                 });
  6299.                         },
  6300.                         _execute : function (i) {
  6301.                                 i = vakata_context.items[i];
  6302.                                 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, {
  6303.                                                         "item"          : i,
  6304.                                                         "reference"     : vakata_context.reference,
  6305.                                                         "element"       : vakata_context.element,
  6306.                                                         "position"      : {
  6307.                                                                 "x" : vakata_context.position_x,
  6308.                                                                 "y" : vakata_context.position_y
  6309.                                                         }
  6310.                                                 }) : false;
  6311.                         },
  6312.                         _parse : function (o, is_callback) {
  6313.                                 if(!o) { return false; }
  6314.                                 if(!is_callback) {
  6315.                                         vakata_context.html             = "";
  6316.                                         vakata_context.items    = [];
  6317.                                 }
  6318.                                 var str = "",
  6319.                                         sep = false,
  6320.                                         tmp;
  6321.  
  6322.                                 if(is_callback) { str += "<"+"ul>"; }
  6323.                                 $.each(o, function (i, val) {
  6324.                                         if(!val) { return true; }
  6325.                                         vakata_context.items.push(val);
  6326.                                         if(!sep && val.separator_before) {
  6327.                                                 str += "<"+"li class='vakata-context-separator'><"+"a href='#' " + ($.vakata.context.settings.icons ? '' : 'style="margin-left:0px;"') + ">&#160;<"+"/a><"+"/li>";
  6328.                                         }
  6329.                                         sep = false;
  6330.                                         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+"' ":'')+">";
  6331.                                         str += "<"+"a href='#' rel='" + (vakata_context.items.length - 1) + "' " + (val.title ? "title='" + val.title + "'" : "") + ">";
  6332.                                         if($.vakata.context.settings.icons) {
  6333.                                                 str += "<"+"i ";
  6334.                                                 if(val.icon) {
  6335.                                                         if(val.icon.indexOf("/") !== -1 || val.icon.indexOf(".") !== -1) { str += " style='background:url(\"" + val.icon + "\") center center no-repeat' "; }
  6336.                                                         else { str += " class='" + val.icon + "' "; }
  6337.                                                 }
  6338.                                                 str += "><"+"/i><"+"span class='vakata-contextmenu-sep'>&#160;<"+"/span>";
  6339.                                         }
  6340.                                         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>";
  6341.                                         if(val.submenu) {
  6342.                                                 tmp = $.vakata.context._parse(val.submenu, true);
  6343.                                                 if(tmp) { str += tmp; }
  6344.                                         }
  6345.                                         str += "<"+"/li>";
  6346.                                         if(val.separator_after) {
  6347.                                                 str += "<"+"li class='vakata-context-separator'><"+"a href='#' " + ($.vakata.context.settings.icons ? '' : 'style="margin-left:0px;"') + ">&#160;<"+"/a><"+"/li>";
  6348.                                                 sep = true;
  6349.                                         }
  6350.                                 });
  6351.                                 str  = str.replace(/<li class\='vakata-context-separator'\><\/li\>$/,"");
  6352.                                 if(is_callback) { str += "</ul>"; }
  6353.                                 /**
  6354.                                  * triggered on the document when the contextmenu is parsed (HTML is built)
  6355.                                  * @event
  6356.                                  * @plugin contextmenu
  6357.                                  * @name context_parse.vakata
  6358.                                  * @param {jQuery} reference the element that was right clicked
  6359.                                  * @param {jQuery} element the DOM element of the menu itself
  6360.                                  * @param {Object} position the x & y coordinates of the menu
  6361.                                  */
  6362.                                 if(!is_callback) { vakata_context.html = str; $.vakata.context._trigger("parse"); }
  6363.                                 return str.length > 10 ? str : false;
  6364.                         },
  6365.                         _show_submenu : function (o) {
  6366.                                 o = $(o);
  6367.                                 if(!o.length || !o.children("ul").length) { return; }
  6368.                                 var e = o.children("ul"),
  6369.                                         xl = o.offset().left,
  6370.                                         x = xl + o.outerWidth(),
  6371.                                         y = o.offset().top,
  6372.                                         w = e.width(),
  6373.                                         h = e.height(),
  6374.                                         dw = $(window).width() + $(window).scrollLeft(),
  6375.                                         dh = $(window).height() + $(window).scrollTop();
  6376.                                 // може да се спести е една проверка - дали няма някой от класовете вече нагоре
  6377.                                 if(right_to_left) {
  6378.                                         o[x - (w + 10 + o.outerWidth()) < 0 ? "addClass" : "removeClass"]("vakata-context-left");
  6379.                                 }
  6380.                                 else {
  6381.                                         o[x + w > dw  && xl > dw - x ? "addClass" : "removeClass"]("vakata-context-right");
  6382.                                 }
  6383.                                 if(y + h + 10 > dh) {
  6384.                                         e.css("bottom","-1px");
  6385.                                 }
  6386.  
  6387.                                 //if does not fit - stick it to the side
  6388.                                 if (o.hasClass('vakata-context-right')) {
  6389.                                         if (xl < w) {
  6390.                                                 e.css("margin-right", xl - w);
  6391.                                         }
  6392.                                 } else {
  6393.                                         if (dw - x < w) {
  6394.                                                 e.css("margin-left", dw - x - w);
  6395.                                         }
  6396.                                 }
  6397.  
  6398.                                 e.show();
  6399.                         },
  6400.                         show : function (reference, position, data) {
  6401.                                 var o, e, x, y, w, h, dw, dh, cond = true;
  6402.                                 if(vakata_context.element && vakata_context.element.length) {
  6403.                                         vakata_context.element.width('');
  6404.                                 }
  6405.                                 switch(cond) {
  6406.                                         case (!position && !reference):
  6407.                                                 return false;
  6408.                                         case (!!position && !!reference):
  6409.                                                 vakata_context.reference        = reference;
  6410.                                                 vakata_context.position_x       = position.x;
  6411.                                                 vakata_context.position_y       = position.y;
  6412.                                                 break;
  6413.                                         case (!position && !!reference):
  6414.                                                 vakata_context.reference        = reference;
  6415.                                                 o = reference.offset();
  6416.                                                 vakata_context.position_x       = o.left + reference.outerHeight();
  6417.                                                 vakata_context.position_y       = o.top;
  6418.                                                 break;
  6419.                                         case (!!position && !reference):
  6420.                                                 vakata_context.position_x       = position.x;
  6421.                                                 vakata_context.position_y       = position.y;
  6422.                                                 break;
  6423.                                 }
  6424.                                 if(!!reference && !data && $(reference).data('vakata_contextmenu')) {
  6425.                                         data = $(reference).data('vakata_contextmenu');
  6426.                                 }
  6427.                                 if($.vakata.context._parse(data)) {
  6428.                                         vakata_context.element.html(vakata_context.html);
  6429.                                 }
  6430.                                 if(vakata_context.items.length) {
  6431.                                         vakata_context.element.appendTo(document.body);
  6432.                                         e = vakata_context.element;
  6433.                                         x = vakata_context.position_x;
  6434.                                         y = vakata_context.position_y;
  6435.                                         w = e.width();
  6436.                                         h = e.height();
  6437.                                         dw = $(window).width() + $(window).scrollLeft();
  6438.                                         dh = $(window).height() + $(window).scrollTop();
  6439.                                         if(right_to_left) {
  6440.                                                 x -= (e.outerWidth() - $(reference).outerWidth());
  6441.                                                 if(x < $(window).scrollLeft() + 20) {
  6442.                                                         x = $(window).scrollLeft() + 20;
  6443.                                                 }
  6444.                                         }
  6445.                                         if(x + w + 20 > dw) {
  6446.                                                 x = dw - (w + 20);
  6447.                                         }
  6448.                                         if(y + h + 20 > dh) {
  6449.                                                 y = dh - (h + 20);
  6450.                                         }
  6451.  
  6452.                                         vakata_context.element
  6453.                                                 .css({ "left" : x, "top" : y })
  6454.                                                 .show()
  6455.                                                 .find('a').first().focus().parent().addClass("vakata-context-hover");
  6456.                                         vakata_context.is_visible = true;
  6457.                                         /**
  6458.                                          * triggered on the document when the contextmenu is shown
  6459.                                          * @event
  6460.                                          * @plugin contextmenu
  6461.                                          * @name context_show.vakata
  6462.                                          * @param {jQuery} reference the element that was right clicked
  6463.                                          * @param {jQuery} element the DOM element of the menu itself
  6464.                                          * @param {Object} position the x & y coordinates of the menu
  6465.                                          */
  6466.                                         $.vakata.context._trigger("show");
  6467.                                 }
  6468.                         },
  6469.                         hide : function () {
  6470.                                 if(vakata_context.is_visible) {
  6471.                                         vakata_context.element.hide().find("ul").hide().end().find(':focus').blur().end().detach();
  6472.                                         vakata_context.is_visible = false;
  6473.                                         /**
  6474.                                          * triggered on the document when the contextmenu is hidden
  6475.                                          * @event
  6476.                                          * @plugin contextmenu
  6477.                                          * @name context_hide.vakata
  6478.                                          * @param {jQuery} reference the element that was right clicked
  6479.                                          * @param {jQuery} element the DOM element of the menu itself
  6480.                                          * @param {Object} position the x & y coordinates of the menu
  6481.                                          */
  6482.                                         $.vakata.context._trigger("hide");
  6483.                                 }
  6484.                         }
  6485.                 };
  6486.                 $(function () {
  6487.                         right_to_left = $(document.body).css("direction") === "rtl";
  6488.                         var to = false;
  6489.  
  6490.                         vakata_context.element = $("<ul class='vakata-context'></ul>");
  6491.                         vakata_context.element
  6492.                                 .on("mouseenter", "li", function (e) {
  6493.                                         e.stopImmediatePropagation();
  6494.  
  6495.                                         if($.contains(this, e.relatedTarget)) {
  6496.                                                 // премахнато заради delegate mouseleave по-долу
  6497.                                                 // $(this).find(".vakata-context-hover").removeClass("vakata-context-hover");
  6498.                                                 return;
  6499.                                         }
  6500.  
  6501.                                         if(to) { clearTimeout(to); }
  6502.                                         vakata_context.element.find(".vakata-context-hover").removeClass("vakata-context-hover").end();
  6503.  
  6504.                                         $(this)
  6505.                                                 .siblings().find("ul").hide().end().end()
  6506.                                                 .parentsUntil(".vakata-context", "li").addBack().addClass("vakata-context-hover");
  6507.                                         $.vakata.context._show_submenu(this);
  6508.                                 })
  6509.                                 // тестово - дали не натоварва?
  6510.                                 .on("mouseleave", "li", function (e) {
  6511.                                         if($.contains(this, e.relatedTarget)) { return; }
  6512.                                         $(this).find(".vakata-context-hover").addBack().removeClass("vakata-context-hover");
  6513.                                 })
  6514.                                 .on("mouseleave", function (e) {
  6515.                                         $(this).find(".vakata-context-hover").removeClass("vakata-context-hover");
  6516.                                         if($.vakata.context.settings.hide_onmouseleave) {
  6517.                                                 to = setTimeout(
  6518.                                                         (function (t) {
  6519.                                                                 return function () { $.vakata.context.hide(); };
  6520.                                                         }(this)), $.vakata.context.settings.hide_onmouseleave);
  6521.                                         }
  6522.                                 })
  6523.                                 .on("click", "a", function (e) {
  6524.                                         e.preventDefault();
  6525.                                 //})
  6526.                                 //.on("mouseup", "a", function (e) {
  6527.                                         if(!$(this).blur().parent().hasClass("vakata-context-disabled") && $.vakata.context._execute($(this).attr("rel")) !== false) {
  6528.                                                 $.vakata.context.hide();
  6529.                                         }
  6530.                                 })
  6531.                                 .on('keydown', 'a', function (e) {
  6532.                                                 var o = null;
  6533.                                                 switch(e.which) {
  6534.                                                         case 13:
  6535.                                                         case 32:
  6536.                                                                 e.type = "click";
  6537.                                                                 e.preventDefault();
  6538.                                                                 $(e.currentTarget).trigger(e);
  6539.                                                                 break;
  6540.                                                         case 37:
  6541.                                                                 if(vakata_context.is_visible) {
  6542.                                                                         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();
  6543.                                                                         e.stopImmediatePropagation();
  6544.                                                                         e.preventDefault();
  6545.                                                                 }
  6546.                                                                 break;
  6547.                                                         case 38:
  6548.                                                                 if(vakata_context.is_visible) {
  6549.                                                                         o = vakata_context.element.find("ul:visible").addBack().last().children(".vakata-context-hover").removeClass("vakata-context-hover").prevAll("li:not(.vakata-context-separator)").first();
  6550.                                                                         if(!o.length) { o = vakata_context.element.find("ul:visible").addBack().last().children("li:not(.vakata-context-separator)").last(); }
  6551.                                                                         o.addClass("vakata-context-hover").children('a').focus();
  6552.                                                                         e.stopImmediatePropagation();
  6553.                                                                         e.preventDefault();
  6554.                                                                 }
  6555.                                                                 break;
  6556.                                                         case 39:
  6557.                                                                 if(vakata_context.is_visible) {
  6558.                                                                         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();
  6559.                                                                         e.stopImmediatePropagation();
  6560.                                                                         e.preventDefault();
  6561.                                                                 }
  6562.                                                                 break;
  6563.                                                         case 40:
  6564.                                                                 if(vakata_context.is_visible) {
  6565.                                                                         o = vakata_context.element.find("ul:visible").addBack().last().children(".vakata-context-hover").removeClass("vakata-context-hover").nextAll("li:not(.vakata-context-separator)").first();
  6566.                                                                         if(!o.length) { o = vakata_context.element.find("ul:visible").addBack().last().children("li:not(.vakata-context-separator)").first(); }
  6567.                                                                         o.addClass("vakata-context-hover").children('a').focus();
  6568.                                                                         e.stopImmediatePropagation();
  6569.                                                                         e.preventDefault();
  6570.                                                                 }
  6571.                                                                 break;
  6572.                                                         case 27:
  6573.                                                                 $.vakata.context.hide();
  6574.                                                                 e.preventDefault();
  6575.                                                                 break;
  6576.                                                         default:
  6577.                                                                 //console.log(e.which);
  6578.                                                                 break;
  6579.                                                 }
  6580.                                         })
  6581.                                 .on('keydown', function (e) {
  6582.                                         e.preventDefault();
  6583.                                         var a = vakata_context.element.find('.vakata-contextmenu-shortcut-' + e.which).parent();
  6584.                                         if(a.parent().not('.vakata-context-disabled')) {
  6585.                                                 a.click();
  6586.                                         }
  6587.                                 });
  6588.  
  6589.                         $(document)
  6590.                                 .on("mousedown.vakata.jstree", function (e) {
  6591.                                         if(vakata_context.is_visible && vakata_context.element[0] !== e.target  && !$.contains(vakata_context.element[0], e.target)) {
  6592.                                                 $.vakata.context.hide();
  6593.                                         }
  6594.                                 })
  6595.                                 .on("context_show.vakata.jstree", function (e, data) {
  6596.                                         vakata_context.element.find("li:has(ul)").children("a").addClass("vakata-context-parent");
  6597.                                         if(right_to_left) {
  6598.                                                 vakata_context.element.addClass("vakata-context-rtl").css("direction", "rtl");
  6599.                                         }
  6600.                                         // also apply a RTL class?
  6601.                                         vakata_context.element.find("ul").hide().end();
  6602.                                 });
  6603.                 });
  6604.         }($));
  6605.         // $.jstree.defaults.plugins.push("contextmenu");
  6606.  
  6607.  
  6608. /**
  6609.  * ### Drag'n'drop plugin
  6610.  *
  6611.  * Enables dragging and dropping of nodes in the tree, resulting in a move or copy operations.
  6612.  */
  6613.  
  6614.         /**
  6615.          * stores all defaults for the drag'n'drop plugin
  6616.          * @name $.jstree.defaults.dnd
  6617.          * @plugin dnd
  6618.          */
  6619.         $.jstree.defaults.dnd = {
  6620.                 /**
  6621.                  * a boolean indicating if a copy should be possible while dragging (by pressint the meta key or Ctrl). Defaults to `true`.
  6622.                  * @name $.jstree.defaults.dnd.copy
  6623.                  * @plugin dnd
  6624.                  */
  6625.                 copy : true,
  6626.                 /**
  6627.                  * a number indicating how long a node should remain hovered while dragging to be opened. Defaults to `500`.
  6628.                  * @name $.jstree.defaults.dnd.open_timeout
  6629.                  * @plugin dnd
  6630.                  */
  6631.                 open_timeout : 500,
  6632.                 /**
  6633.                  * 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
  6634.                  * @name $.jstree.defaults.dnd.is_draggable
  6635.                  * @plugin dnd
  6636.                  */
  6637.                 is_draggable : true,
  6638.                 /**
  6639.                  * 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`
  6640.                  * @name $.jstree.defaults.dnd.check_while_dragging
  6641.                  * @plugin dnd
  6642.                  */
  6643.                 check_while_dragging : true,
  6644.                 /**
  6645.                  * a boolean indicating if nodes from this tree should only be copied with dnd (as opposed to moved), default is `false`
  6646.                  * @name $.jstree.defaults.dnd.always_copy
  6647.                  * @plugin dnd
  6648.                  */
  6649.                 always_copy : false,
  6650.                 /**
  6651.                  * 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`
  6652.                  * @name $.jstree.defaults.dnd.inside_pos
  6653.                  * @plugin dnd
  6654.                  */
  6655.                 inside_pos : 0,
  6656.                 /**
  6657.                  * 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
  6658.                  * @name $.jstree.defaults.dnd.drag_selection
  6659.                  * @plugin dnd
  6660.                  */
  6661.                 drag_selection : true,
  6662.                 /**
  6663.                  * 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.
  6664.                  * @name $.jstree.defaults.dnd.touch
  6665.                  * @plugin dnd
  6666.                  */
  6667.                 touch : true,
  6668.                 /**
  6669.                  * 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.
  6670.                  * @name $.jstree.defaults.dnd.large_drop_target
  6671.                  * @plugin dnd
  6672.                  */
  6673.                 large_drop_target : false,
  6674.                 /**
  6675.                  * 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".
  6676.                  * @name $.jstree.defaults.dnd.large_drag_target
  6677.                  * @plugin dnd
  6678.                  */
  6679.                 large_drag_target : false,
  6680.                 /**
  6681.                  * controls whether use HTML5 dnd api instead of classical. That will allow better integration of dnd events with other HTML5 controls.
  6682.                  * @reference http://caniuse.com/#feat=dragndrop
  6683.                  * @name $.jstree.defaults.dnd.use_html5
  6684.                  * @plugin dnd
  6685.                  */
  6686.                 use_html5: false
  6687.         };
  6688.         var drg, elm;
  6689.         // TODO: now check works by checking for each node individually, how about max_children, unique, etc?
  6690.         $.jstree.plugins.dnd = function (options, parent) {
  6691.                 this.init = function (el, options) {
  6692.                         parent.init.call(this, el, options);
  6693.                         this.settings.dnd.use_html5 = this.settings.dnd.use_html5 && ('draggable' in document.createElement('span'));
  6694.                 };
  6695.                 this.bind = function () {
  6696.                         parent.bind.call(this);
  6697.  
  6698.                         this.element
  6699.                                 .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) {
  6700.                                                 if(this.settings.dnd.large_drag_target && $(e.target).closest('.jstree-node')[0] !== e.currentTarget) {
  6701.                                                         return true;
  6702.                                                 }
  6703.                                                 if(e.type === "touchstart" && (!this.settings.dnd.touch || (this.settings.dnd.touch === 'selected' && !$(e.currentTarget).closest('.jstree-node').children('.jstree-anchor').hasClass('jstree-clicked')))) {
  6704.                                                         return true;
  6705.                                                 }
  6706.                                                 var obj = this.get_node(e.target),
  6707.                                                         mlt = this.is_selected(obj) && this.settings.dnd.drag_selection ? this.get_top_selected().length : 1,
  6708.                                                         txt = (mlt > 1 ? mlt + ' ' + this.get_string('nodes') : this.get_text(e.currentTarget));
  6709.                                                 if(this.settings.core.force_text) {
  6710.                                                         txt = $.vakata.html.escape(txt);
  6711.                                                 }
  6712.                                                 if(obj && obj.id && obj.id !== $.jstree.root && (e.which === 1 || e.type === "touchstart" || e.type === "dragstart") &&
  6713.                                                         (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)))
  6714.                                                 ) {
  6715.                                                         drg = { 'jstree' : true, 'origin' : this, 'obj' : this.get_node(obj,true), 'nodes' : mlt > 1 ? this.get_top_selected() : [obj.id] };
  6716.                                                         elm = e.currentTarget;
  6717.                                                         if (this.settings.dnd.use_html5) {
  6718.                                                                 $.vakata.dnd._trigger('start', e, { 'helper': $(), 'element': elm, 'data': drg });
  6719.                                                         } else {
  6720.                                                                 this.element.trigger('mousedown.jstree');
  6721.                                                                 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>');
  6722.                                                         }
  6723.                                                 }
  6724.                                         }, this));
  6725.                         if (this.settings.dnd.use_html5) {
  6726.                                 this.element
  6727.                                         .on('dragover.jstree', function (e) {
  6728.                                                         e.preventDefault();
  6729.                                                         $.vakata.dnd._trigger('move', e, { 'helper': $(), 'element': elm, 'data': drg });
  6730.                                                         return false;
  6731.                                                 })
  6732.                                         //.on('dragenter.jstree', this.settings.dnd.large_drop_target ? '.jstree-node' : '.jstree-anchor', $.proxy(function (e) {
  6733.                                         //              e.preventDefault();
  6734.                                         //              $.vakata.dnd._trigger('move', e, { 'helper': $(), 'element': elm, 'data': drg });
  6735.                                         //              return false;
  6736.                                         //      }, this))
  6737.                                         .on('drop.jstree', $.proxy(function (e) {
  6738.                                                         e.preventDefault();
  6739.                                                         $.vakata.dnd._trigger('stop', e, { 'helper': $(), 'element': elm, 'data': drg });
  6740.                                                         return false;
  6741.                                                 }, this));
  6742.                         }
  6743.                 };
  6744.                 this.redraw_node = function(obj, deep, callback, force_render) {
  6745.                         obj = parent.redraw_node.apply(this, arguments);
  6746.                         if (obj && this.settings.dnd.use_html5) {
  6747.                                 if (this.settings.dnd.large_drag_target) {
  6748.                                         obj.setAttribute('draggable', true);
  6749.                                 } else {
  6750.                                         var i, j, tmp = null;
  6751.                                         for(i = 0, j = obj.childNodes.length; i < j; i++) {
  6752.                                                 if(obj.childNodes[i] && obj.childNodes[i].className && obj.childNodes[i].className.indexOf("jstree-anchor") !== -1) {
  6753.                                                         tmp = obj.childNodes[i];
  6754.                                                         break;
  6755.                                                 }
  6756.                                         }
  6757.                                         if(tmp) {
  6758.                                                 tmp.setAttribute('draggable', true);
  6759.                                         }
  6760.                                 }
  6761.                         }
  6762.                         return obj;
  6763.                 };
  6764.         };
  6765.  
  6766.         $(function() {
  6767.                 // bind only once for all instances
  6768.                 var lastmv = false,
  6769.                         laster = false,
  6770.                         lastev = false,
  6771.                         opento = false,
  6772.                         marker = $('<div id="jstree-marker">&#160;</div>').hide(); //.appendTo('body');
  6773.  
  6774.                 $(document)
  6775.                         .on('dragover.vakata.jstree', function (e) {
  6776.                                 if (elm) {
  6777.                                         $.vakata.dnd._trigger('move', e, { 'helper': $(), 'element': elm, 'data': drg });
  6778.                                 }
  6779.                         })
  6780.                         .on('drop.vakata.jstree', function (e) {
  6781.                                 if (elm) {
  6782.                                         $.vakata.dnd._trigger('stop', e, { 'helper': $(), 'element': elm, 'data': drg });
  6783.                                         elm = null;
  6784.                                         drg = null;
  6785.                                 }
  6786.                         })
  6787.                         .on('dnd_start.vakata.jstree', function (e, data) {
  6788.                                 lastmv = false;
  6789.                                 lastev = false;
  6790.                                 if(!data || !data.data || !data.data.jstree) { return; }
  6791.                                 marker.appendTo(document.body); //.show();
  6792.                         })
  6793.                         .on('dnd_move.vakata.jstree', function (e, data) {
  6794.                                 var isDifferentNode = data.event.target !== lastev.target;
  6795.                                 if(opento) {
  6796.                                         if (!data.event || data.event.type !== 'dragover' || isDifferentNode) {
  6797.                                                 clearTimeout(opento);
  6798.                                         }
  6799.                                 }
  6800.                                 if(!data || !data.data || !data.data.jstree) { return; }
  6801.  
  6802.                                 // if we are hovering the marker image do nothing (can happen on "inside" drags)
  6803.                                 if(data.event.target.id && data.event.target.id === 'jstree-marker') {
  6804.                                         return;
  6805.                                 }
  6806.                                 lastev = data.event;
  6807.  
  6808.                                 var ins = $.jstree.reference(data.event.target),
  6809.                                         ref = false,
  6810.                                         off = false,
  6811.                                         rel = false,
  6812.                                         tmp, l, t, h, p, i, o, ok, t1, t2, op, ps, pr, ip, tm, is_copy, pn;
  6813.                                 // if we are over an instance
  6814.                                 if(ins && ins._data && ins._data.dnd) {
  6815.                                         marker.attr('class', 'jstree-' + ins.get_theme() + ( ins.settings.core.themes.responsive ? ' jstree-dnd-responsive' : '' ));
  6816.                                         is_copy = data.data.origin && (data.data.origin.settings.dnd.always_copy || (data.data.origin.settings.dnd.copy && (data.event.metaKey || data.event.ctrlKey)));
  6817.                                         data.helper
  6818.                                                 .children().attr('class', 'jstree-' + ins.get_theme() + ' jstree-' + ins.get_theme() + '-' + ins.get_theme_variant() + ' ' + ( ins.settings.core.themes.responsive ? ' jstree-dnd-responsive' : '' ))
  6819.                                                 .find('.jstree-copy').first()[ is_copy ? 'show' : 'hide' ]();
  6820.  
  6821.                                         // if are hovering the container itself add a new root node
  6822.                                         //console.log(data.event);
  6823.                                         if( (data.event.target === ins.element[0] || data.event.target === ins.get_container_ul()[0]) && ins.get_container_ul().children().length === 0) {
  6824.                                                 ok = true;
  6825.                                                 for(t1 = 0, t2 = data.data.nodes.length; t1 < t2; t1++) {
  6826.                                                         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) });
  6827.                                                         if(!ok) { break; }
  6828.                                                 }
  6829.                                                 if(ok) {
  6830.                                                         lastmv = { 'ins' : ins, 'par' : $.jstree.root, 'pos' : 'last' };
  6831.                                                         marker.hide();
  6832.                                                         data.helper.find('.jstree-icon').first().removeClass('jstree-er').addClass('jstree-ok');
  6833.                                                         if (data.event.originalEvent && data.event.originalEvent.dataTransfer) {
  6834.                                                                 data.event.originalEvent.dataTransfer.dropEffect = is_copy ? 'copy' : 'move';
  6835.                                                         }
  6836.                                                         return;
  6837.                                                 }
  6838.                                         }
  6839.                                         else {
  6840.                                                 // if we are hovering a tree node
  6841.                                                 ref = ins.settings.dnd.large_drop_target ? $(data.event.target).closest('.jstree-node').children('.jstree-anchor') : $(data.event.target).closest('.jstree-anchor');
  6842.                                                 if(ref && ref.length && ref.parent().is('.jstree-closed, .jstree-open, .jstree-leaf')) {
  6843.                                                         off = ref.offset();
  6844.                                                         rel = (data.event.pageY !== undefined ? data.event.pageY : data.event.originalEvent.pageY) - off.top;
  6845.                                                         h = ref.outerHeight();
  6846.                                                         if(rel < h / 3) {
  6847.                                                                 o = ['b', 'i', 'a'];
  6848.                                                         }
  6849.                                                         else if(rel > h - h / 3) {
  6850.                                                                 o = ['a', 'i', 'b'];
  6851.                                                         }
  6852.                                                         else {
  6853.                                                                 o = rel > h / 2 ? ['i', 'a', 'b'] : ['i', 'b', 'a'];
  6854.                                                         }
  6855.                                                         $.each(o, function (j, v) {
  6856.                                                                 switch(v) {
  6857.                                                                         case 'b':
  6858.                                                                                 l = off.left - 6;
  6859.                                                                                 t = off.top;
  6860.                                                                                 p = ins.get_parent(ref);
  6861.                                                                                 i = ref.parent().index();
  6862.                                                                                 break;
  6863.                                                                         case 'i':
  6864.                                                                                 ip = ins.settings.dnd.inside_pos;
  6865.                                                                                 tm = ins.get_node(ref.parent());
  6866.                                                                                 l = off.left - 2;
  6867.                                                                                 t = off.top + h / 2 + 1;
  6868.                                                                                 p = tm.id;
  6869.                                                                                 i = ip === 'first' ? 0 : (ip === 'last' ? tm.children.length : Math.min(ip, tm.children.length));
  6870.                                                                                 break;
  6871.                                                                         case 'a':
  6872.                                                                                 l = off.left - 6;
  6873.                                                                                 t = off.top + h;
  6874.                                                                                 p = ins.get_parent(ref);
  6875.                                                                                 i = ref.parent().index() + 1;
  6876.                                                                                 break;
  6877.                                                                 }
  6878.                                                                 ok = true;
  6879.                                                                 for(t1 = 0, t2 = data.data.nodes.length; t1 < t2; t1++) {
  6880.                                                                         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";
  6881.                                                                         ps = i;
  6882.                                                                         if(op === "move_node" && v === 'a' && (data.data.origin && data.data.origin === ins) && p === ins.get_parent(data.data.nodes[t1])) {
  6883.                                                                                 pr = ins.get_node(p);
  6884.                                                                                 if(ps > $.inArray(data.data.nodes[t1], pr.children)) {
  6885.                                                                                         ps -= 1;
  6886.                                                                                 }
  6887.                                                                         }
  6888.                                                                         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) }) );
  6889.                                                                         if(!ok) {
  6890.                                                                                 if(ins && ins.last_error) { laster = ins.last_error(); }
  6891.                                                                                 break;
  6892.                                                                         }
  6893.                                                                 }
  6894.                                                                 if(v === 'i' && ref.parent().is('.jstree-closed') && ins.settings.dnd.open_timeout) {
  6895.                                                                         if (!data.event || data.event.type !== 'dragover' || isDifferentNode) {
  6896.                                                                                 if (opento) { clearTimeout(opento); }
  6897.                                                                                 opento = setTimeout((function (x, z) { return function () { x.open_node(z); }; }(ins, ref)), ins.settings.dnd.open_timeout);
  6898.                                                                         }
  6899.                                                                 }
  6900.                                                                 if(ok) {
  6901.                                                                         pn = ins.get_node(p, true);
  6902.                                                                         if (!pn.hasClass('.jstree-dnd-parent')) {
  6903.                                                                                 $('.jstree-dnd-parent').removeClass('jstree-dnd-parent');
  6904.                                                                                 pn.addClass('jstree-dnd-parent');
  6905.                                                                         }
  6906.                                                                         lastmv = { 'ins' : ins, 'par' : p, 'pos' : v === 'i' && ip === 'last' && i === 0 && !ins.is_loaded(tm) ? 'last' : i };
  6907.                                                                         marker.css({ 'left' : l + 'px', 'top' : t + 'px' }).show();
  6908.                                                                         data.helper.find('.jstree-icon').first().removeClass('jstree-er').addClass('jstree-ok');
  6909.                                                                         if (data.event.originalEvent && data.event.originalEvent.dataTransfer) {
  6910.                                                                                 data.event.originalEvent.dataTransfer.dropEffect = is_copy ? 'copy' : 'move';
  6911.                                                                         }
  6912.                                                                         laster = {};
  6913.                                                                         o = true;
  6914.                                                                         return false;
  6915.                                                                 }
  6916.                                                         });
  6917.                                                         if(o === true) { return; }
  6918.                                                 }
  6919.                                         }
  6920.                                 }
  6921.                                 $('.jstree-dnd-parent').removeClass('jstree-dnd-parent');
  6922.                                 lastmv = false;
  6923.                                 data.helper.find('.jstree-icon').removeClass('jstree-ok').addClass('jstree-er');
  6924.                                 if (data.event.originalEvent && data.event.originalEvent.dataTransfer) {
  6925.                                         //data.event.originalEvent.dataTransfer.dropEffect = 'none';
  6926.                                 }
  6927.                                 marker.hide();
  6928.                         })
  6929.                         .on('dnd_scroll.vakata.jstree', function (e, data) {
  6930.                                 if(!data || !data.data || !data.data.jstree) { return; }
  6931.                                 marker.hide();
  6932.                                 lastmv = false;
  6933.                                 lastev = false;
  6934.                                 data.helper.find('.jstree-icon').first().removeClass('jstree-ok').addClass('jstree-er');
  6935.                         })
  6936.                         .on('dnd_stop.vakata.jstree', function (e, data) {
  6937.                                 $('.jstree-dnd-parent').removeClass('jstree-dnd-parent');
  6938.                                 if(opento) { clearTimeout(opento); }
  6939.                                 if(!data || !data.data || !data.data.jstree) { return; }
  6940.                                 marker.hide().detach();
  6941.                                 var i, j, nodes = [];
  6942.                                 if(lastmv) {
  6943.                                         for(i = 0, j = data.data.nodes.length; i < j; i++) {
  6944.                                                 nodes[i] = data.data.origin ? data.data.origin.get_node(data.data.nodes[i]) : data.data.nodes[i];
  6945.                                         }
  6946.                                         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);
  6947.                                 }
  6948.                                 else {
  6949.                                         i = $(data.event.target).closest('.jstree');
  6950.                                         if(i.length && laster && laster.error && laster.error === 'check') {
  6951.                                                 i = i.jstree(true);
  6952.                                                 if(i) {
  6953.                                                         i.settings.core.error.call(this, laster);
  6954.                                                 }
  6955.                                         }
  6956.                                 }
  6957.                                 lastev = false;
  6958.                                 lastmv = false;
  6959.                         })
  6960.                         .on('keyup.jstree keydown.jstree', function (e, data) {
  6961.                                 data = $.vakata.dnd._get();
  6962.                                 if(data && data.data && data.data.jstree) {
  6963.                                         if (e.type === "keyup" && e.which === 27) {
  6964.                                                 if (opento) { clearTimeout(opento); }
  6965.                                                 lastmv = false;
  6966.                                                 laster = false;
  6967.                                                 lastev = false;
  6968.                                                 opento = false;
  6969.                                                 marker.hide().detach();
  6970.                                                 $.vakata.dnd._clean();
  6971.                                         } else {
  6972.                                                 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' ]();
  6973.                                                 if(lastev) {
  6974.                                                         lastev.metaKey = e.metaKey;
  6975.                                                         lastev.ctrlKey = e.ctrlKey;
  6976.                                                         $.vakata.dnd._trigger('move', lastev);
  6977.                                                 }
  6978.                                         }
  6979.                                 }
  6980.                         });
  6981.         });
  6982.  
  6983.         // helpers
  6984.         (function ($) {
  6985.                 $.vakata.html = {
  6986.                         div : $('<div />'),
  6987.                         escape : function (str) {
  6988.                                 return $.vakata.html.div.text(str).html();
  6989.                         },
  6990.                         strip : function (str) {
  6991.                                 return $.vakata.html.div.empty().append($.parseHTML(str)).text();
  6992.                         }
  6993.                 };
  6994.                 // private variable
  6995.                 var vakata_dnd = {
  6996.                         element : false,
  6997.                         target  : false,
  6998.                         is_down : false,
  6999.                         is_drag : false,
  7000.                         helper  : false,
  7001.                         helper_w: 0,
  7002.                         data    : false,
  7003.                         init_x  : 0,
  7004.                         init_y  : 0,
  7005.                         scroll_l: 0,
  7006.                         scroll_t: 0,
  7007.                         scroll_e: false,
  7008.                         scroll_i: false,
  7009.                         is_touch: false
  7010.                 };
  7011.                 $.vakata.dnd = {
  7012.                         settings : {
  7013.                                 scroll_speed            : 10,
  7014.                                 scroll_proximity        : 20,
  7015.                                 helper_left                     : 5,
  7016.                                 helper_top                      : 10,
  7017.                                 threshold                       : 5,
  7018.                                 threshold_touch         : 10
  7019.                         },
  7020.                         _trigger : function (event_name, e, data) {
  7021.                                 if (data === undefined) {
  7022.                                         data = $.vakata.dnd._get();
  7023.                                 }
  7024.                                 data.event = e;
  7025.                                 $(document).triggerHandler("dnd_" + event_name + ".vakata", data);
  7026.                         },
  7027.                         _get : function () {
  7028.                                 return {
  7029.                                         "data"          : vakata_dnd.data,
  7030.                                         "element"       : vakata_dnd.element,
  7031.                                         "helper"        : vakata_dnd.helper
  7032.                                 };
  7033.                         },
  7034.                         _clean : function () {
  7035.                                 if(vakata_dnd.helper) { vakata_dnd.helper.remove(); }
  7036.                                 if(vakata_dnd.scroll_i) { clearInterval(vakata_dnd.scroll_i); vakata_dnd.scroll_i = false; }
  7037.                                 vakata_dnd = {
  7038.                                         element : false,
  7039.                                         target  : false,
  7040.                                         is_down : false,
  7041.                                         is_drag : false,
  7042.                                         helper  : false,
  7043.                                         helper_w: 0,
  7044.                                         data    : false,
  7045.                                         init_x  : 0,
  7046.                                         init_y  : 0,
  7047.                                         scroll_l: 0,
  7048.                                         scroll_t: 0,
  7049.                                         scroll_e: false,
  7050.                                         scroll_i: false,
  7051.                                         is_touch: false
  7052.                                 };
  7053.                                 $(document).off("mousemove.vakata.jstree touchmove.vakata.jstree", $.vakata.dnd.drag);
  7054.                                 $(document).off("mouseup.vakata.jstree touchend.vakata.jstree", $.vakata.dnd.stop);
  7055.                         },
  7056.                         _scroll : function (init_only) {
  7057.                                 if(!vakata_dnd.scroll_e || (!vakata_dnd.scroll_l && !vakata_dnd.scroll_t)) {
  7058.                                         if(vakata_dnd.scroll_i) { clearInterval(vakata_dnd.scroll_i); vakata_dnd.scroll_i = false; }
  7059.                                         return false;
  7060.                                 }
  7061.                                 if(!vakata_dnd.scroll_i) {
  7062.                                         vakata_dnd.scroll_i = setInterval($.vakata.dnd._scroll, 100);
  7063.                                         return false;
  7064.                                 }
  7065.                                 if(init_only === true) { return false; }
  7066.  
  7067.                                 var i = vakata_dnd.scroll_e.scrollTop(),
  7068.                                         j = vakata_dnd.scroll_e.scrollLeft();
  7069.                                 vakata_dnd.scroll_e.scrollTop(i + vakata_dnd.scroll_t * $.vakata.dnd.settings.scroll_speed);
  7070.                                 vakata_dnd.scroll_e.scrollLeft(j + vakata_dnd.scroll_l * $.vakata.dnd.settings.scroll_speed);
  7071.                                 if(i !== vakata_dnd.scroll_e.scrollTop() || j !== vakata_dnd.scroll_e.scrollLeft()) {
  7072.                                         /**
  7073.                                          * triggered on the document when a drag causes an element to scroll
  7074.                                          * @event
  7075.                                          * @plugin dnd
  7076.                                          * @name dnd_scroll.vakata
  7077.                                          * @param {Mixed} data any data supplied with the call to $.vakata.dnd.start
  7078.                                          * @param {DOM} element the DOM element being dragged
  7079.                                          * @param {jQuery} helper the helper shown next to the mouse
  7080.                                          * @param {jQuery} event the element that is scrolling
  7081.                                          */
  7082.                                         $.vakata.dnd._trigger("scroll", vakata_dnd.scroll_e);
  7083.                                 }
  7084.                         },
  7085.                         start : function (e, data, html) {
  7086.                                 if(e.type === "touchstart" && e.originalEvent && e.originalEvent.changedTouches && e.originalEvent.changedTouches[0]) {
  7087.                                         e.pageX = e.originalEvent.changedTouches[0].pageX;
  7088.                                         e.pageY = e.originalEvent.changedTouches[0].pageY;
  7089.                                         e.target = document.elementFromPoint(e.originalEvent.changedTouches[0].pageX - window.pageXOffset, e.originalEvent.changedTouches[0].pageY - window.pageYOffset);
  7090.                                 }
  7091.                                 if(vakata_dnd.is_drag) { $.vakata.dnd.stop({}); }
  7092.                                 try {
  7093.                                         e.currentTarget.unselectable = "on";
  7094.                                         e.currentTarget.onselectstart = function() { return false; };
  7095.                                         if(e.currentTarget.style) {
  7096.                                                 e.currentTarget.style.touchAction = "none";
  7097.                                                 e.currentTarget.style.msTouchAction = "none";
  7098.                                                 e.currentTarget.style.MozUserSelect = "none";
  7099.                                         }
  7100.                                 } catch(ignore) { }
  7101.                                 vakata_dnd.init_x       = e.pageX;
  7102.                                 vakata_dnd.init_y       = e.pageY;
  7103.                                 vakata_dnd.data         = data;
  7104.                                 vakata_dnd.is_down      = true;
  7105.                                 vakata_dnd.element      = e.currentTarget;
  7106.                                 vakata_dnd.target       = e.target;
  7107.                                 vakata_dnd.is_touch     = e.type === "touchstart";
  7108.                                 if(html !== false) {
  7109.                                         vakata_dnd.helper = $("<div id='vakata-dnd'></div>").html(html).css({
  7110.                                                 "display"               : "block",
  7111.                                                 "margin"                : "0",
  7112.                                                 "padding"               : "0",
  7113.                                                 "position"              : "absolute",
  7114.                                                 "top"                   : "-2000px",
  7115.                                                 "lineHeight"    : "16px",
  7116.                                                 "zIndex"                : "10000"
  7117.                                         });
  7118.                                 }
  7119.                                 $(document).on("mousemove.vakata.jstree touchmove.vakata.jstree", $.vakata.dnd.drag);
  7120.                                 $(document).on("mouseup.vakata.jstree touchend.vakata.jstree", $.vakata.dnd.stop);
  7121.                                 return false;
  7122.                         },
  7123.                         drag : function (e) {
  7124.                                 if(e.type === "touchmove" && e.originalEvent && e.originalEvent.changedTouches && e.originalEvent.changedTouches[0]) {
  7125.                                         e.pageX = e.originalEvent.changedTouches[0].pageX;
  7126.                                         e.pageY = e.originalEvent.changedTouches[0].pageY;
  7127.                                         e.target = document.elementFromPoint(e.originalEvent.changedTouches[0].pageX - window.pageXOffset, e.originalEvent.changedTouches[0].pageY - window.pageYOffset);
  7128.                                 }
  7129.                                 if(!vakata_dnd.is_down) { return; }
  7130.                                 if(!vakata_dnd.is_drag) {
  7131.                                         if(
  7132.                                                 Math.abs(e.pageX - vakata_dnd.init_x) > (vakata_dnd.is_touch ? $.vakata.dnd.settings.threshold_touch : $.vakata.dnd.settings.threshold) ||
  7133.                                                 Math.abs(e.pageY - vakata_dnd.init_y) > (vakata_dnd.is_touch ? $.vakata.dnd.settings.threshold_touch : $.vakata.dnd.settings.threshold)
  7134.                                         ) {
  7135.                                                 if(vakata_dnd.helper) {
  7136.                                                         vakata_dnd.helper.appendTo(document.body);
  7137.                                                         vakata_dnd.helper_w = vakata_dnd.helper.outerWidth();
  7138.                                                 }
  7139.                                                 vakata_dnd.is_drag = true;
  7140.                                                 $(vakata_dnd.target).one('click.vakata', false);
  7141.                                                 /**
  7142.                                                  * triggered on the document when a drag starts
  7143.                                                  * @event
  7144.                                                  * @plugin dnd
  7145.                                                  * @name dnd_start.vakata
  7146.                                                  * @param {Mixed} data any data supplied with the call to $.vakata.dnd.start
  7147.                                                  * @param {DOM} element the DOM element being dragged
  7148.                                                  * @param {jQuery} helper the helper shown next to the mouse
  7149.                                                  * @param {Object} event the event that caused the start (probably mousemove)
  7150.                                                  */
  7151.                                                 $.vakata.dnd._trigger("start", e);
  7152.                                         }
  7153.                                         else { return; }
  7154.                                 }
  7155.  
  7156.                                 var d  = false, w  = false,
  7157.                                         dh = false, wh = false,
  7158.                                         dw = false, ww = false,
  7159.                                         dt = false, dl = false,
  7160.                                         ht = false, hl = false;
  7161.  
  7162.                                 vakata_dnd.scroll_t = 0;
  7163.                                 vakata_dnd.scroll_l = 0;
  7164.                                 vakata_dnd.scroll_e = false;
  7165.                                 $($(e.target).parentsUntil("body").addBack().get().reverse())
  7166.                                         .filter(function () {
  7167.                                                 return  (/^auto|scroll$/).test($(this).css("overflow")) &&
  7168.                                                                 (this.scrollHeight > this.offsetHeight || this.scrollWidth > this.offsetWidth);
  7169.                                         })
  7170.                                         .each(function () {
  7171.                                                 var t = $(this), o = t.offset();
  7172.                                                 if(this.scrollHeight > this.offsetHeight) {
  7173.                                                         if(o.top + t.height() - e.pageY < $.vakata.dnd.settings.scroll_proximity)       { vakata_dnd.scroll_t = 1; }
  7174.                                                         if(e.pageY - o.top < $.vakata.dnd.settings.scroll_proximity)                            { vakata_dnd.scroll_t = -1; }
  7175.                                                 }
  7176.                                                 if(this.scrollWidth > this.offsetWidth) {
  7177.                                                         if(o.left + t.width() - e.pageX < $.vakata.dnd.settings.scroll_proximity)       { vakata_dnd.scroll_l = 1; }
  7178.                                                         if(e.pageX - o.left < $.vakata.dnd.settings.scroll_proximity)                           { vakata_dnd.scroll_l = -1; }
  7179.                                                 }
  7180.                                                 if(vakata_dnd.scroll_t || vakata_dnd.scroll_l) {
  7181.                                                         vakata_dnd.scroll_e = $(this);
  7182.                                                         return false;
  7183.                                                 }
  7184.                                         });
  7185.  
  7186.                                 if(!vakata_dnd.scroll_e) {
  7187.                                         d  = $(document); w = $(window);
  7188.                                         dh = d.height(); wh = w.height();
  7189.                                         dw = d.width(); ww = w.width();
  7190.                                         dt = d.scrollTop(); dl = d.scrollLeft();
  7191.                                         if(dh > wh && e.pageY - dt < $.vakata.dnd.settings.scroll_proximity)            { vakata_dnd.scroll_t = -1;  }
  7192.                                         if(dh > wh && wh - (e.pageY - dt) < $.vakata.dnd.settings.scroll_proximity)     { vakata_dnd.scroll_t = 1; }
  7193.                                         if(dw > ww && e.pageX - dl < $.vakata.dnd.settings.scroll_proximity)            { vakata_dnd.scroll_l = -1; }
  7194.                                         if(dw > ww && ww - (e.pageX - dl) < $.vakata.dnd.settings.scroll_proximity)     { vakata_dnd.scroll_l = 1; }
  7195.                                         if(vakata_dnd.scroll_t || vakata_dnd.scroll_l) {
  7196.                                                 vakata_dnd.scroll_e = d;
  7197.                                         }
  7198.                                 }
  7199.                                 if(vakata_dnd.scroll_e) { $.vakata.dnd._scroll(true); }
  7200.  
  7201.                                 if(vakata_dnd.helper) {
  7202.                                         ht = parseInt(e.pageY + $.vakata.dnd.settings.helper_top, 10);
  7203.                                         hl = parseInt(e.pageX + $.vakata.dnd.settings.helper_left, 10);
  7204.                                         if(dh && ht + 25 > dh) { ht = dh - 50; }
  7205.                                         if(dw && hl + vakata_dnd.helper_w > dw) { hl = dw - (vakata_dnd.helper_w + 2); }
  7206.                                         vakata_dnd.helper.css({
  7207.                                                 left    : hl + "px",
  7208.                                                 top             : ht + "px"
  7209.                                         });
  7210.                                 }
  7211.                                 /**
  7212.                                  * triggered on the document when a drag is in progress
  7213.                                  * @event
  7214.                                  * @plugin dnd
  7215.                                  * @name dnd_move.vakata
  7216.                                  * @param {Mixed} data any data supplied with the call to $.vakata.dnd.start
  7217.                                  * @param {DOM} element the DOM element being dragged
  7218.                                  * @param {jQuery} helper the helper shown next to the mouse
  7219.                                  * @param {Object} event the event that caused this to trigger (most likely mousemove)
  7220.                                  */
  7221.                                 $.vakata.dnd._trigger("move", e);
  7222.                                 return false;
  7223.                         },
  7224.                         stop : function (e) {
  7225.                                 if(e.type === "touchend" && e.originalEvent && e.originalEvent.changedTouches && e.originalEvent.changedTouches[0]) {
  7226.                                         e.pageX = e.originalEvent.changedTouches[0].pageX;
  7227.                                         e.pageY = e.originalEvent.changedTouches[0].pageY;
  7228.                                         e.target = document.elementFromPoint(e.originalEvent.changedTouches[0].pageX - window.pageXOffset, e.originalEvent.changedTouches[0].pageY - window.pageYOffset);
  7229.                                 }
  7230.                                 if(vakata_dnd.is_drag) {
  7231.                                         /**
  7232.                                          * triggered on the document when a drag stops (the dragged element is dropped)
  7233.                                          * @event
  7234.                                          * @plugin dnd
  7235.                                          * @name dnd_stop.vakata
  7236.                                          * @param {Mixed} data any data supplied with the call to $.vakata.dnd.start
  7237.                                          * @param {DOM} element the DOM element being dragged
  7238.                                          * @param {jQuery} helper the helper shown next to the mouse
  7239.                                          * @param {Object} event the event that caused the stop
  7240.                                          */
  7241.                                         if (e.target !== vakata_dnd.target) {
  7242.                                                 $(vakata_dnd.target).off('click.vakata');
  7243.                                         }
  7244.                                         $.vakata.dnd._trigger("stop", e);
  7245.                                 }
  7246.                                 else {
  7247.                                         if(e.type === "touchend" && e.target === vakata_dnd.target) {
  7248.                                                 var to = setTimeout(function () { $(e.target).click(); }, 100);
  7249.                                                 $(e.target).one('click', function() { if(to) { clearTimeout(to); } });
  7250.                                         }
  7251.                                 }
  7252.                                 $.vakata.dnd._clean();
  7253.                                 return false;
  7254.                         }
  7255.                 };
  7256.         }($));
  7257.  
  7258.         // include the dnd plugin by default
  7259.         // $.jstree.defaults.plugins.push("dnd");
  7260.  
  7261.  
  7262. /**
  7263.  * ### Massload plugin
  7264.  *
  7265.  * Adds massload functionality to jsTree, so that multiple nodes can be loaded in a single request (only useful with lazy loading).
  7266.  */
  7267.  
  7268.         /**
  7269.          * massload configuration
  7270.          *
  7271.          * It is possible to set this to a standard jQuery-like AJAX config.
  7272.          * 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.
  7273.          *
  7274.          * 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.
  7275.          *
  7276.          * 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.
  7277.          *
  7278.          *      {
  7279.          *              "id1" : [{ "text" : "Child of ID1", "id" : "c1" }, { "text" : "Another child of ID1", "id" : "c2" }],
  7280.          *              "id2" : [{ "text" : "Child of ID2", "id" : "c3" }]
  7281.          *      }
  7282.          *
  7283.          * @name $.jstree.defaults.massload
  7284.          * @plugin massload
  7285.          */
  7286.         $.jstree.defaults.massload = null;
  7287.         $.jstree.plugins.massload = function (options, parent) {
  7288.                 this.init = function (el, options) {
  7289.                         this._data.massload = {};
  7290.                         parent.init.call(this, el, options);
  7291.                 };
  7292.                 this._load_nodes = function (nodes, callback, is_callback, force_reload) {
  7293.                         var s = this.settings.massload,
  7294.                                 nodesString = JSON.stringify(nodes),
  7295.                                 toLoad = [],
  7296.                                 m = this._model.data,
  7297.                                 i, j, dom;
  7298.                         if (!is_callback) {
  7299.                                 for(i = 0, j = nodes.length; i < j; i++) {
  7300.                                         if(!m[nodes[i]] || ( (!m[nodes[i]].state.loaded && !m[nodes[i]].state.failed) || force_reload) ) {
  7301.                                                 toLoad.push(nodes[i]);
  7302.                                                 dom = this.get_node(nodes[i], true);
  7303.                                                 if (dom && dom.length) {
  7304.                                                         dom.addClass("jstree-loading").attr('aria-busy',true);
  7305.                                                 }
  7306.                                         }
  7307.                                 }
  7308.                                 this._data.massload = {};
  7309.                                 if (toLoad.length) {
  7310.                                         if($.isFunction(s)) {
  7311.                                                 return s.call(this, toLoad, $.proxy(function (data) {
  7312.                                                         var i, j;
  7313.                                                         if(data) {
  7314.                                                                 for(i in data) {
  7315.                                                                         if(data.hasOwnProperty(i)) {
  7316.                                                                                 this._data.massload[i] = data[i];
  7317.                                                                         }
  7318.                                                                 }
  7319.                                                         }
  7320.                                                         for(i = 0, j = nodes.length; i < j; i++) {
  7321.                                                                 dom = this.get_node(nodes[i], true);
  7322.                                                                 if (dom && dom.length) {
  7323.                                                                         dom.removeClass("jstree-loading").attr('aria-busy',false);
  7324.                                                                 }
  7325.                                                         }
  7326.                                                         parent._load_nodes.call(this, nodes, callback, is_callback, force_reload);
  7327.                                                 }, this));
  7328.                                         }
  7329.                                         if(typeof s === 'object' && s && s.url) {
  7330.                                                 s = $.extend(true, {}, s);
  7331.                                                 if($.isFunction(s.url)) {
  7332.                                                         s.url = s.url.call(this, toLoad);
  7333.                                                 }
  7334.                                                 if($.isFunction(s.data)) {
  7335.                                                         s.data = s.data.call(this, toLoad);
  7336.                                                 }
  7337.                                                 return $.ajax(s)
  7338.                                                         .done($.proxy(function (data,t,x) {
  7339.                                                                         var i, j;
  7340.                                                                         if(data) {
  7341.                                                                                 for(i in data) {
  7342.                                                                                         if(data.hasOwnProperty(i)) {
  7343.                                                                                                 this._data.massload[i] = data[i];
  7344.                                                                                         }
  7345.                                                                                 }
  7346.                                                                         }
  7347.                                                                         for(i = 0, j = nodes.length; i < j; i++) {
  7348.                                                                                 dom = this.get_node(nodes[i], true);
  7349.                                                                                 if (dom && dom.length) {
  7350.                                                                                         dom.removeClass("jstree-loading").attr('aria-busy',false);
  7351.                                                                                 }
  7352.                                                                         }
  7353.                                                                         parent._load_nodes.call(this, nodes, callback, is_callback, force_reload);
  7354.                                                                 }, this))
  7355.                                                         .fail($.proxy(function (f) {
  7356.                                                                         parent._load_nodes.call(this, nodes, callback, is_callback, force_reload);
  7357.                                                                 }, this));
  7358.                                         }
  7359.                                 }
  7360.                         }
  7361.                         return parent._load_nodes.call(this, nodes, callback, is_callback, force_reload);
  7362.                 };
  7363.                 this._load_node = function (obj, callback) {
  7364.                         var data = this._data.massload[obj.id],
  7365.                                 rslt = null, dom;
  7366.                         if(data) {
  7367.                                 rslt = this[typeof data === 'string' ? '_append_html_data' : '_append_json_data'](
  7368.                                         obj,
  7369.                                         typeof data === 'string' ? $($.parseHTML(data)).filter(function () { return this.nodeType !== 3; }) : data,
  7370.                                         function (status) { callback.call(this, status); }
  7371.                                 );
  7372.                                 dom = this.get_node(obj.id, true);
  7373.                                 if (dom && dom.length) {
  7374.                                         dom.removeClass("jstree-loading").attr('aria-busy',false);
  7375.                                 }
  7376.                                 delete this._data.massload[obj.id];
  7377.                                 return rslt;
  7378.                         }
  7379.                         return parent._load_node.call(this, obj, callback);
  7380.                 };
  7381.         };
  7382.  
  7383. /**
  7384.  * ### Search plugin
  7385.  *
  7386.  * Adds search functionality to jsTree.
  7387.  */
  7388.  
  7389.         /**
  7390.          * stores all defaults for the search plugin
  7391.          * @name $.jstree.defaults.search
  7392.          * @plugin search
  7393.          */
  7394.         $.jstree.defaults.search = {
  7395.                 /**
  7396.                  * a jQuery-like AJAX config, which jstree uses if a server should be queried for results.
  7397.                  *
  7398.                  * 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.
  7399.                  * 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
  7400.                  * @name $.jstree.defaults.search.ajax
  7401.                  * @plugin search
  7402.                  */
  7403.                 ajax : false,
  7404.                 /**
  7405.                  * Indicates if the search should be fuzzy or not (should `chnd3` match `child node 3`). Default is `false`.
  7406.                  * @name $.jstree.defaults.search.fuzzy
  7407.                  * @plugin search
  7408.                  */
  7409.                 fuzzy : false,
  7410.                 /**
  7411.                  * Indicates if the search should be case sensitive. Default is `false`.
  7412.                  * @name $.jstree.defaults.search.case_sensitive
  7413.                  * @plugin search
  7414.                  */
  7415.                 case_sensitive : false,
  7416.                 /**
  7417.                  * 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).
  7418.                  * This setting can be changed at runtime when calling the search method. Default is `false`.
  7419.                  * @name $.jstree.defaults.search.show_only_matches
  7420.                  * @plugin search
  7421.                  */
  7422.                 show_only_matches : false,
  7423.                 /**
  7424.                  * Indicates if the children of matched element are shown (when show_only_matches is true)
  7425.                  * This setting can be changed at runtime when calling the search method. Default is `false`.
  7426.                  * @name $.jstree.defaults.search.show_only_matches_children
  7427.                  * @plugin search
  7428.                  */
  7429.                 show_only_matches_children : false,
  7430.                 /**
  7431.                  * 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`.
  7432.                  * @name $.jstree.defaults.search.close_opened_onclear
  7433.                  * @plugin search
  7434.                  */
  7435.                 close_opened_onclear : true,
  7436.                 /**
  7437.                  * Indicates if only leaf nodes should be included in search results. Default is `false`.
  7438.                  * @name $.jstree.defaults.search.search_leaves_only
  7439.                  * @plugin search
  7440.                  */
  7441.                 search_leaves_only : false,
  7442.                 /**
  7443.                  * 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).
  7444.                  * 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`.
  7445.                  * @name $.jstree.defaults.search.search_callback
  7446.                  * @plugin search
  7447.                  */
  7448.                 search_callback : false
  7449.         };
  7450.  
  7451.         $.jstree.plugins.search = function (options, parent) {
  7452.                 this.bind = function () {
  7453.                         parent.bind.call(this);
  7454.  
  7455.                         this._data.search.str = "";
  7456.                         this._data.search.dom = $();
  7457.                         this._data.search.res = [];
  7458.                         this._data.search.opn = [];
  7459.                         this._data.search.som = false;
  7460.                         this._data.search.smc = false;
  7461.                         this._data.search.hdn = [];
  7462.  
  7463.                         this.element
  7464.                                 .on("search.jstree", $.proxy(function (e, data) {
  7465.                                                 if(this._data.search.som && data.res.length) {
  7466.                                                         var m = this._model.data, i, j, p = [], k, l;
  7467.                                                         for(i = 0, j = data.res.length; i < j; i++) {
  7468.                                                                 if(m[data.res[i]] && !m[data.res[i]].state.hidden) {
  7469.                                                                         p.push(data.res[i]);
  7470.                                                                         p = p.concat(m[data.res[i]].parents);
  7471.                                                                         if(this._data.search.smc) {
  7472.                                                                                 for (k = 0, l = m[data.res[i]].children_d.length; k < l; k++) {
  7473.                                                                                         if (m[m[data.res[i]].children_d[k]] && !m[m[data.res[i]].children_d[k]].state.hidden) {
  7474.                                                                                                 p.push(m[data.res[i]].children_d[k]);
  7475.                                                                                         }
  7476.                                                                                 }
  7477.                                                                         }
  7478.                                                                 }
  7479.                                                         }
  7480.                                                         p = $.vakata.array_remove_item($.vakata.array_unique(p), $.jstree.root);
  7481.                                                         this._data.search.hdn = this.hide_all(true);
  7482.                                                         this.show_node(p, true);
  7483.                                                         this.redraw(true);
  7484.                                                 }
  7485.                                         }, this))
  7486.                                 .on("clear_search.jstree", $.proxy(function (e, data) {
  7487.                                                 if(this._data.search.som && data.res.length) {
  7488.                                                         this.show_node(this._data.search.hdn, true);
  7489.                                                         this.redraw(true);
  7490.                                                 }
  7491.                                         }, this));
  7492.                 };
  7493.                 /**
  7494.                  * used to search the tree nodes for a given string
  7495.                  * @name search(str [, skip_async])
  7496.                  * @param {String} str the search string
  7497.                  * @param {Boolean} skip_async if set to true server will not be queried even if configured
  7498.                  * @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)
  7499.                  * @param {mixed} inside an optional node to whose children to limit the search
  7500.                  * @param {Boolean} append if set to true the results of this search are appended to the previous search
  7501.                  * @plugin search
  7502.                  * @trigger search.jstree
  7503.                  */
  7504.                 this.search = function (str, skip_async, show_only_matches, inside, append, show_only_matches_children) {
  7505.                         if(str === false || $.trim(str.toString()) === "") {
  7506.                                 return this.clear_search();
  7507.                         }
  7508.                         inside = this.get_node(inside);
  7509.                         inside = inside && inside.id ? inside.id : null;
  7510.                         str = str.toString();
  7511.                         var s = this.settings.search,
  7512.                                 a = s.ajax ? s.ajax : false,
  7513.                                 m = this._model.data,
  7514.                                 f = null,
  7515.                                 r = [],
  7516.                                 p = [], i, j;
  7517.                         if(this._data.search.res.length && !append) {
  7518.                                 this.clear_search();
  7519.                         }
  7520.                         if(show_only_matches === undefined) {
  7521.                                 show_only_matches = s.show_only_matches;
  7522.                         }
  7523.                         if(show_only_matches_children === undefined) {
  7524.                                 show_only_matches_children = s.show_only_matches_children;
  7525.                         }
  7526.                         if(!skip_async && a !== false) {
  7527.                                 if($.isFunction(a)) {
  7528.                                         return a.call(this, str, $.proxy(function (d) {
  7529.                                                         if(d && d.d) { d = d.d; }
  7530.                                                         this._load_nodes(!$.isArray(d) ? [] : $.vakata.array_unique(d), function () {
  7531.                                                                 this.search(str, true, show_only_matches, inside, append, show_only_matches_children);
  7532.                                                         });
  7533.                                                 }, this), inside);
  7534.                                 }
  7535.                                 else {
  7536.                                         a = $.extend({}, a);
  7537.                                         if(!a.data) { a.data = {}; }
  7538.                                         a.data.str = str;
  7539.                                         if(inside) {
  7540.                                                 a.data.inside = inside;
  7541.                                         }
  7542.                                         if (this._data.search.lastRequest) {
  7543.                                                 this._data.search.lastRequest.abort();
  7544.                                         }
  7545.                                         this._data.search.lastRequest = $.ajax(a)
  7546.                                                 .fail($.proxy(function () {
  7547.                                                         this._data.core.last_error = { 'error' : 'ajax', 'plugin' : 'search', 'id' : 'search_01', 'reason' : 'Could not load search parents', 'data' : JSON.stringify(a) };
  7548.                                                         this.settings.core.error.call(this, this._data.core.last_error);
  7549.                                                 }, this))
  7550.                                                 .done($.proxy(function (d) {
  7551.                                                         if(d && d.d) { d = d.d; }
  7552.                                                         this._load_nodes(!$.isArray(d) ? [] : $.vakata.array_unique(d), function () {
  7553.                                                                 this.search(str, true, show_only_matches, inside, append, show_only_matches_children);
  7554.                                                         });
  7555.                                                 }, this));
  7556.                                         return this._data.search.lastRequest;
  7557.                                 }
  7558.                         }
  7559.                         if(!append) {
  7560.                                 this._data.search.str = str;
  7561.                                 this._data.search.dom = $();
  7562.                                 this._data.search.res = [];
  7563.                                 this._data.search.opn = [];
  7564.                                 this._data.search.som = show_only_matches;
  7565.                                 this._data.search.smc = show_only_matches_children;
  7566.                         }
  7567.  
  7568.                         f = new $.vakata.search(str, true, { caseSensitive : s.case_sensitive, fuzzy : s.fuzzy });
  7569.                         $.each(m[inside ? inside : $.jstree.root].children_d, function (ii, i) {
  7570.                                 var v = m[i];
  7571.                                 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) ) ) {
  7572.                                         r.push(i);
  7573.                                         p = p.concat(v.parents);
  7574.                                 }
  7575.                         });
  7576.                         if(r.length) {
  7577.                                 p = $.vakata.array_unique(p);
  7578.                                 for(i = 0, j = p.length; i < j; i++) {
  7579.                                         if(p[i] !== $.jstree.root && m[p[i]] && this.open_node(p[i], null, 0) === true) {
  7580.                                                 this._data.search.opn.push(p[i]);
  7581.                                         }
  7582.                                 }
  7583.                                 if(!append) {
  7584.                                         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(', #')));
  7585.                                         this._data.search.res = r;
  7586.                                 }
  7587.                                 else {
  7588.                                         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(', #'))));
  7589.                                         this._data.search.res = $.vakata.array_unique(this._data.search.res.concat(r));
  7590.                                 }
  7591.                                 this._data.search.dom.children(".jstree-anchor").addClass('jstree-search');
  7592.                         }
  7593.                         /**
  7594.                          * triggered after search is complete
  7595.                          * @event
  7596.                          * @name search.jstree
  7597.                          * @param {jQuery} nodes a jQuery collection of matching nodes
  7598.                          * @param {String} str the search string
  7599.                          * @param {Array} res a collection of objects represeing the matching nodes
  7600.                          * @plugin search
  7601.                          */
  7602.                         this.trigger('search', { nodes : this._data.search.dom, str : str, res : this._data.search.res, show_only_matches : show_only_matches });
  7603.                 };
  7604.                 /**
  7605.                  * used to clear the last search (removes classes and shows all nodes if filtering is on)
  7606.                  * @name clear_search()
  7607.                  * @plugin search
  7608.                  * @trigger clear_search.jstree
  7609.                  */
  7610.                 this.clear_search = function () {
  7611.                         if(this.settings.search.close_opened_onclear) {
  7612.                                 this.close_node(this._data.search.opn, 0);
  7613.                         }
  7614.                         /**
  7615.                          * triggered after search is complete
  7616.                          * @event
  7617.                          * @name clear_search.jstree
  7618.                          * @param {jQuery} nodes a jQuery collection of matching nodes (the result from the last search)
  7619.                          * @param {String} str the search string (the last search string)
  7620.                          * @param {Array} res a collection of objects represeing the matching nodes (the result from the last search)
  7621.                          * @plugin search
  7622.                          */
  7623.                         this.trigger('clear_search', { 'nodes' : this._data.search.dom, str : this._data.search.str, res : this._data.search.res });
  7624.                         if(this._data.search.res.length) {
  7625.                                 this._data.search.dom = $(this.element[0].querySelectorAll('#' + $.map(this._data.search.res, function (v) {
  7626.                                         return "0123456789".indexOf(v[0]) !== -1 ? '\\3' + v[0] + ' ' + v.substr(1).replace($.jstree.idregex,'\\$&') : v.replace($.jstree.idregex,'\\$&');
  7627.                                 }).join(', #')));
  7628.                                 this._data.search.dom.children(".jstree-anchor").removeClass("jstree-search");
  7629.                         }
  7630.                         this._data.search.str = "";
  7631.                         this._data.search.res = [];
  7632.                         this._data.search.opn = [];
  7633.                         this._data.search.dom = $();
  7634.                 };
  7635.  
  7636.                 this.redraw_node = function(obj, deep, callback, force_render) {
  7637.                         obj = parent.redraw_node.apply(this, arguments);
  7638.                         if(obj) {
  7639.                                 if($.inArray(obj.id, this._data.search.res) !== -1) {
  7640.                                         var i, j, tmp = null;
  7641.                                         for(i = 0, j = obj.childNodes.length; i < j; i++) {
  7642.                                                 if(obj.childNodes[i] && obj.childNodes[i].className && obj.childNodes[i].className.indexOf("jstree-anchor") !== -1) {
  7643.                                                         tmp = obj.childNodes[i];
  7644.                                                         break;
  7645.                                                 }
  7646.                                         }
  7647.                                         if(tmp) {
  7648.                                                 tmp.className += ' jstree-search';
  7649.                                         }
  7650.                                 }
  7651.                         }
  7652.                         return obj;
  7653.                 };
  7654.         };
  7655.  
  7656.         // helpers
  7657.         (function ($) {
  7658.                 // from http://kiro.me/projects/fuse.html
  7659.                 $.vakata.search = function(pattern, txt, options) {
  7660.                         options = options || {};
  7661.                         options = $.extend({}, $.vakata.search.defaults, options);
  7662.                         if(options.fuzzy !== false) {
  7663.                                 options.fuzzy = true;
  7664.                         }
  7665.                         pattern = options.caseSensitive ? pattern : pattern.toLowerCase();
  7666.                         var MATCH_LOCATION      = options.location,
  7667.                                 MATCH_DISTANCE  = options.distance,
  7668.                                 MATCH_THRESHOLD = options.threshold,
  7669.                                 patternLen = pattern.length,
  7670.                                 matchmask, pattern_alphabet, match_bitapScore, search;
  7671.                         if(patternLen > 32) {
  7672.                                 options.fuzzy = false;
  7673.                         }
  7674.                         if(options.fuzzy) {
  7675.                                 matchmask = 1 << (patternLen - 1);
  7676.                                 pattern_alphabet = (function () {
  7677.                                         var mask = {},
  7678.                                                 i = 0;
  7679.                                         for (i = 0; i < patternLen; i++) {
  7680.                                                 mask[pattern.charAt(i)] = 0;
  7681.                                         }
  7682.                                         for (i = 0; i < patternLen; i++) {
  7683.                                                 mask[pattern.charAt(i)] |= 1 << (patternLen - i - 1);
  7684.                                         }
  7685.                                         return mask;
  7686.                                 }());
  7687.                                 match_bitapScore = function (e, x) {
  7688.                                         var accuracy = e / patternLen,
  7689.                                                 proximity = Math.abs(MATCH_LOCATION - x);
  7690.                                         if(!MATCH_DISTANCE) {
  7691.                                                 return proximity ? 1.0 : accuracy;
  7692.                                         }
  7693.                                         return accuracy + (proximity / MATCH_DISTANCE);
  7694.                                 };
  7695.                         }
  7696.                         search = function (text) {
  7697.                                 text = options.caseSensitive ? text : text.toLowerCase();
  7698.                                 if(pattern === text || text.indexOf(pattern) !== -1) {
  7699.                                         return {
  7700.                                                 isMatch: true,
  7701.                                                 score: 0
  7702.                                         };
  7703.                                 }
  7704.                                 if(!options.fuzzy) {
  7705.                                         return {
  7706.                                                 isMatch: false,
  7707.                                                 score: 1
  7708.                                         };
  7709.                                 }
  7710.                                 var i, j,
  7711.                                         textLen = text.length,
  7712.                                         scoreThreshold = MATCH_THRESHOLD,
  7713.                                         bestLoc = text.indexOf(pattern, MATCH_LOCATION),
  7714.                                         binMin, binMid,
  7715.                                         binMax = patternLen + textLen,
  7716.                                         lastRd, start, finish, rd, charMatch,
  7717.                                         score = 1,
  7718.                                         locations = [];
  7719.                                 if (bestLoc !== -1) {
  7720.                                         scoreThreshold = Math.min(match_bitapScore(0, bestLoc), scoreThreshold);
  7721.                                         bestLoc = text.lastIndexOf(pattern, MATCH_LOCATION + patternLen);
  7722.                                         if (bestLoc !== -1) {
  7723.                                                 scoreThreshold = Math.min(match_bitapScore(0, bestLoc), scoreThreshold);
  7724.                                         }
  7725.                                 }
  7726.                                 bestLoc = -1;
  7727.                                 for (i = 0; i < patternLen; i++) {
  7728.                                         binMin = 0;
  7729.                                         binMid = binMax;
  7730.                                         while (binMin < binMid) {
  7731.                                                 if (match_bitapScore(i, MATCH_LOCATION + binMid) <= scoreThreshold) {
  7732.                                                         binMin = binMid;
  7733.                                                 } else {
  7734.                                                         binMax = binMid;
  7735.                                                 }
  7736.                                                 binMid = Math.floor((binMax - binMin) / 2 + binMin);
  7737.                                         }
  7738.                                         binMax = binMid;
  7739.                                         start = Math.max(1, MATCH_LOCATION - binMid + 1);
  7740.                                         finish = Math.min(MATCH_LOCATION + binMid, textLen) + patternLen;
  7741.                                         rd = new Array(finish + 2);
  7742.                                         rd[finish + 1] = (1 << i) - 1;
  7743.                                         for (j = finish; j >= start; j--) {
  7744.                                                 charMatch = pattern_alphabet[text.charAt(j - 1)];
  7745.                                                 if (i === 0) {
  7746.                                                         rd[j] = ((rd[j + 1] << 1) | 1) & charMatch;
  7747.                                                 } else {
  7748.                                                         rd[j] = ((rd[j + 1] << 1) | 1) & charMatch | (((lastRd[j + 1] | lastRd[j]) << 1) | 1) | lastRd[j + 1];
  7749.                                                 }
  7750.                                                 if (rd[j] & matchmask) {
  7751.                                                         score = match_bitapScore(i, j - 1);
  7752.                                                         if (score <= scoreThreshold) {
  7753.                                                                 scoreThreshold = score;
  7754.                                                                 bestLoc = j - 1;
  7755.                                                                 locations.push(bestLoc);
  7756.                                                                 if (bestLoc > MATCH_LOCATION) {
  7757.                                                                         start = Math.max(1, 2 * MATCH_LOCATION - bestLoc);
  7758.                                                                 } else {
  7759.                                                                         break;
  7760.                                                                 }
  7761.                                                         }
  7762.                                                 }
  7763.                                         }
  7764.                                         if (match_bitapScore(i + 1, MATCH_LOCATION) > scoreThreshold) {
  7765.                                                 break;
  7766.                                         }
  7767.                                         lastRd = rd;
  7768.                                 }
  7769.                                 return {
  7770.                                         isMatch: bestLoc >= 0,
  7771.                                         score: score
  7772.                                 };
  7773.                         };
  7774.                         return txt === true ? { 'search' : search } : search(txt);
  7775.                 };
  7776.                 $.vakata.search.defaults = {
  7777.                         location : 0,
  7778.                         distance : 100,
  7779.                         threshold : 0.6,
  7780.                         fuzzy : false,
  7781.                         caseSensitive : false
  7782.                 };
  7783.         }($));
  7784.  
  7785.         // include the search plugin by default
  7786.         // $.jstree.defaults.plugins.push("search");
  7787.  
  7788.  
  7789. /**
  7790.  * ### Sort plugin
  7791.  *
  7792.  * Automatically sorts all siblings in the tree according to a sorting function.
  7793.  */
  7794.  
  7795.         /**
  7796.          * the settings function used to sort the nodes.
  7797.          * It is executed in the tree's context, accepts two nodes as arguments and should return `1` or `-1`.
  7798.          * @name $.jstree.defaults.sort
  7799.          * @plugin sort
  7800.          */
  7801.         $.jstree.defaults.sort = function (a, b) {
  7802.                 //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);
  7803.                 return this.get_text(a) > this.get_text(b) ? 1 : -1;
  7804.         };
  7805.         $.jstree.plugins.sort = function (options, parent) {
  7806.                 this.bind = function () {
  7807.                         parent.bind.call(this);
  7808.                         this.element
  7809.                                 .on("model.jstree", $.proxy(function (e, data) {
  7810.                                                 this.sort(data.parent, true);
  7811.                                         }, this))
  7812.                                 .on("rename_node.jstree create_node.jstree", $.proxy(function (e, data) {
  7813.                                                 this.sort(data.parent || data.node.parent, false);
  7814.                                                 this.redraw_node(data.parent || data.node.parent, true);
  7815.                                         }, this))
  7816.                                 .on("move_node.jstree copy_node.jstree", $.proxy(function (e, data) {
  7817.                                                 this.sort(data.parent, false);
  7818.                                                 this.redraw_node(data.parent, true);
  7819.                                         }, this));
  7820.                 };
  7821.                 /**
  7822.                  * used to sort a node's children
  7823.                  * @private
  7824.                  * @name sort(obj [, deep])
  7825.                  * @param  {mixed} obj the node
  7826.                  * @param {Boolean} deep if set to `true` nodes are sorted recursively.
  7827.                  * @plugin sort
  7828.                  * @trigger search.jstree
  7829.                  */
  7830.                 this.sort = function (obj, deep) {
  7831.                         var i, j;
  7832.                         obj = this.get_node(obj);
  7833.                         if(obj && obj.children && obj.children.length) {
  7834.                                 obj.children.sort($.proxy(this.settings.sort, this));
  7835.                                 if(deep) {
  7836.                                         for(i = 0, j = obj.children_d.length; i < j; i++) {
  7837.                                                 this.sort(obj.children_d[i], false);
  7838.                                         }
  7839.                                 }
  7840.                         }
  7841.                 };
  7842.         };
  7843.  
  7844.         // include the sort plugin by default
  7845.         // $.jstree.defaults.plugins.push("sort");
  7846.  
  7847. /**
  7848.  * ### State plugin
  7849.  *
  7850.  * Saves the state of the tree (selected nodes, opened nodes) on the user's computer using available options (localStorage, cookies, etc)
  7851.  */
  7852.  
  7853.         var to = false;
  7854.         /**
  7855.          * stores all defaults for the state plugin
  7856.          * @name $.jstree.defaults.state
  7857.          * @plugin state
  7858.          */
  7859.         $.jstree.defaults.state = {
  7860.                 /**
  7861.                  * A string for the key to use when saving the current tree (change if using multiple trees in your project). Defaults to `jstree`.
  7862.                  * @name $.jstree.defaults.state.key
  7863.                  * @plugin state
  7864.                  */
  7865.                 key             : 'jstree',
  7866.                 /**
  7867.                  * A space separated list of events that trigger a state save. Defaults to `changed.jstree open_node.jstree close_node.jstree`.
  7868.                  * @name $.jstree.defaults.state.events
  7869.                  * @plugin state
  7870.                  */
  7871.                 events  : 'changed.jstree open_node.jstree close_node.jstree check_node.jstree uncheck_node.jstree',
  7872.                 /**
  7873.                  * Time in milliseconds after which the state will expire. Defaults to 'false' meaning - no expire.
  7874.                  * @name $.jstree.defaults.state.ttl
  7875.                  * @plugin state
  7876.                  */
  7877.                 ttl             : false,
  7878.                 /**
  7879.                  * 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.
  7880.                  * @name $.jstree.defaults.state.filter
  7881.                  * @plugin state
  7882.                  */
  7883.                 filter  : false,
  7884.                 /**
  7885.                  * 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`
  7886.                  * @name $.jstree.defaults.state.preserve_loaded
  7887.                  * @plugin state
  7888.                  */
  7889.                 preserve_loaded : false
  7890.         };
  7891.         $.jstree.plugins.state = function (options, parent) {
  7892.                 this.bind = function () {
  7893.                         parent.bind.call(this);
  7894.                         var bind = $.proxy(function () {
  7895.                                 this.element.on(this.settings.state.events, $.proxy(function () {
  7896.                                         if(to) { clearTimeout(to); }
  7897.                                         to = setTimeout($.proxy(function () { this.save_state(); }, this), 100);
  7898.                                 }, this));
  7899.                                 /**
  7900.                                  * triggered when the state plugin is finished restoring the state (and immediately after ready if there is no state to restore).
  7901.                                  * @event
  7902.                                  * @name state_ready.jstree
  7903.                                  * @plugin state
  7904.                                  */
  7905.                                 this.trigger('state_ready');
  7906.                         }, this);
  7907.                         this.element
  7908.                                 .on("ready.jstree", $.proxy(function (e, data) {
  7909.                                                 this.element.one("restore_state.jstree", bind);
  7910.                                                 if(!this.restore_state()) { bind(); }
  7911.                                         }, this));
  7912.                 };
  7913.                 /**
  7914.                  * save the state
  7915.                  * @name save_state()
  7916.                  * @plugin state
  7917.                  */
  7918.                 this.save_state = function () {
  7919.                         var tm = this.get_state();
  7920.                         if (!this.settings.state.preserve_loaded) {
  7921.                                 delete tm.core.loaded;
  7922.                         }
  7923.                         var st = { 'state' : tm, 'ttl' : this.settings.state.ttl, 'sec' : +(new Date()) };
  7924.                         $.vakata.storage.set(this.settings.state.key, JSON.stringify(st));
  7925.                 };
  7926.                 /**
  7927.                  * restore the state from the user's computer
  7928.                  * @name restore_state()
  7929.                  * @plugin state
  7930.                  */
  7931.                 this.restore_state = function () {
  7932.                         var k = $.vakata.storage.get(this.settings.state.key);
  7933.                         if(!!k) { try { k = JSON.parse(k); } catch(ex) { return false; } }
  7934.                         if(!!k && k.ttl && k.sec && +(new Date()) - k.sec > k.ttl) { return false; }
  7935.                         if(!!k && k.state) { k = k.state; }
  7936.                         if(!!k && $.isFunction(this.settings.state.filter)) { k = this.settings.state.filter.call(this, k); }
  7937.                         if(!!k) {
  7938.                                 if (!this.settings.state.preserve_loaded) {
  7939.                                         delete k.core.loaded;
  7940.                                 }
  7941.                                 this.element.one("set_state.jstree", function (e, data) { data.instance.trigger('restore_state', { 'state' : $.extend(true, {}, k) }); });
  7942.                                 this.set_state(k);
  7943.                                 return true;
  7944.                         }
  7945.                         return false;
  7946.                 };
  7947.                 /**
  7948.                  * clear the state on the user's computer
  7949.                  * @name clear_state()
  7950.                  * @plugin state
  7951.                  */
  7952.                 this.clear_state = function () {
  7953.                         return $.vakata.storage.del(this.settings.state.key);
  7954.                 };
  7955.         };
  7956.  
  7957.         (function ($, undefined) {
  7958.                 $.vakata.storage = {
  7959.                         // simply specifying the functions in FF throws an error
  7960.                         set : function (key, val) { return window.localStorage.setItem(key, val); },
  7961.                         get : function (key) { return window.localStorage.getItem(key); },
  7962.                         del : function (key) { return window.localStorage.removeItem(key); }
  7963.                 };
  7964.         }($));
  7965.  
  7966.         // include the state plugin by default
  7967.         // $.jstree.defaults.plugins.push("state");
  7968.  
  7969. /**
  7970.  * ### Types plugin
  7971.  *
  7972.  * 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.
  7973.  */
  7974.  
  7975.         /**
  7976.          * 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).
  7977.          *
  7978.          * * `max_children` the maximum number of immediate children this node type can have. Do not specify or set to `-1` for unlimited.
  7979.          * * `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.
  7980.          * * `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.
  7981.          * * `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.
  7982.          * * `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)
  7983.          * * `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)
  7984.          *
  7985.          * There are two predefined types:
  7986.          *
  7987.          * * `#` represents the root of the tree, for example `max_children` would control the maximum number of root nodes.
  7988.          * * `default` represents the default node - any settings here will be applied to all nodes that do not have a type specified.
  7989.          *
  7990.          * @name $.jstree.defaults.types
  7991.          * @plugin types
  7992.          */
  7993.         $.jstree.defaults.types = {
  7994.                 'default' : {}
  7995.         };
  7996.         $.jstree.defaults.types[$.jstree.root] = {};
  7997.  
  7998.         $.jstree.plugins.types = function (options, parent) {
  7999.                 this.init = function (el, options) {
  8000.                         var i, j;
  8001.                         if(options && options.types && options.types['default']) {
  8002.                                 for(i in options.types) {
  8003.                                         if(i !== "default" && i !== $.jstree.root && options.types.hasOwnProperty(i)) {
  8004.                                                 for(j in options.types['default']) {
  8005.                                                         if(options.types['default'].hasOwnProperty(j) && options.types[i][j] === undefined) {
  8006.                                                                 options.types[i][j] = options.types['default'][j];
  8007.                                                         }
  8008.                                                 }
  8009.                                         }
  8010.                                 }
  8011.                         }
  8012.                         parent.init.call(this, el, options);
  8013.                         this._model.data[$.jstree.root].type = $.jstree.root;
  8014.                 };
  8015.                 this.refresh = function (skip_loading, forget_state) {
  8016.                         parent.refresh.call(this, skip_loading, forget_state);
  8017.                         this._model.data[$.jstree.root].type = $.jstree.root;
  8018.                 };
  8019.                 this.bind = function () {
  8020.                         this.element
  8021.                                 .on('model.jstree', $.proxy(function (e, data) {
  8022.                                                 var m = this._model.data,
  8023.                                                         dpc = data.nodes,
  8024.                                                         t = this.settings.types,
  8025.                                                         i, j, c = 'default', k;
  8026.                                                 for(i = 0, j = dpc.length; i < j; i++) {
  8027.                                                         c = 'default';
  8028.                                                         if(m[dpc[i]].original && m[dpc[i]].original.type && t[m[dpc[i]].original.type]) {
  8029.                                                                 c = m[dpc[i]].original.type;
  8030.                                                         }
  8031.                                                         if(m[dpc[i]].data && m[dpc[i]].data.jstree && m[dpc[i]].data.jstree.type && t[m[dpc[i]].data.jstree.type]) {
  8032.                                                                 c = m[dpc[i]].data.jstree.type;
  8033.                                                         }
  8034.                                                         m[dpc[i]].type = c;
  8035.                                                         if(m[dpc[i]].icon === true && t[c].icon !== undefined) {
  8036.                                                                 m[dpc[i]].icon = t[c].icon;
  8037.                                                         }
  8038.                                                         if(t[c].li_attr !== undefined && typeof t[c].li_attr === 'object') {
  8039.                                                                 for (k in t[c].li_attr) {
  8040.                                                                         if (t[c].li_attr.hasOwnProperty(k)) {
  8041.                                                                                 if (k === 'id') {
  8042.                                                                                         continue;
  8043.                                                                                 }
  8044.                                                                                 else if (m[dpc[i]].li_attr[k] === undefined) {
  8045.                                                                                         m[dpc[i]].li_attr[k] = t[c].li_attr[k];
  8046.                                                                                 }
  8047.                                                                                 else if (k === 'class') {
  8048.                                                                                         m[dpc[i]].li_attr['class'] = t[c].li_attr['class'] + ' ' + m[dpc[i]].li_attr['class'];
  8049.                                                                                 }
  8050.                                                                         }
  8051.                                                                 }
  8052.                                                         }
  8053.                                                         if(t[c].a_attr !== undefined && typeof t[c].a_attr === 'object') {
  8054.                                                                 for (k in t[c].a_attr) {
  8055.                                                                         if (t[c].a_attr.hasOwnProperty(k)) {
  8056.                                                                                 if (k === 'id') {
  8057.                                                                                         continue;
  8058.                                                                                 }
  8059.                                                                                 else if (m[dpc[i]].a_attr[k] === undefined) {
  8060.                                                                                         m[dpc[i]].a_attr[k] = t[c].a_attr[k];
  8061.                                                                                 }
  8062.                                                                                 else if (k === 'href' && m[dpc[i]].a_attr[k] === '#') {
  8063.                                                                                         m[dpc[i]].a_attr['href'] = t[c].a_attr['href'];
  8064.                                                                                 }
  8065.                                                                                 else if (k === 'class') {
  8066.                                                                                         m[dpc[i]].a_attr['class'] = t[c].a_attr['class'] + ' ' + m[dpc[i]].a_attr['class'];
  8067.                                                                                 }
  8068.                                                                         }
  8069.                                                                 }
  8070.                                                         }
  8071.                                                 }
  8072.                                                 m[$.jstree.root].type = $.jstree.root;
  8073.                                         }, this));
  8074.                         parent.bind.call(this);
  8075.                 };
  8076.                 this.get_json = function (obj, options, flat) {
  8077.                         var i, j,
  8078.                                 m = this._model.data,
  8079.                                 opt = options ? $.extend(true, {}, options, {no_id:false}) : {},
  8080.                                 tmp = parent.get_json.call(this, obj, opt, flat);
  8081.                         if(tmp === false) { return false; }
  8082.                         if($.isArray(tmp)) {
  8083.                                 for(i = 0, j = tmp.length; i < j; i++) {
  8084.                                         tmp[i].type = tmp[i].id && m[tmp[i].id] && m[tmp[i].id].type ? m[tmp[i].id].type : "default";
  8085.                                         if(options && options.no_id) {
  8086.                                                 delete tmp[i].id;
  8087.                                                 if(tmp[i].li_attr && tmp[i].li_attr.id) {
  8088.                                                         delete tmp[i].li_attr.id;
  8089.                                                 }
  8090.                                                 if(tmp[i].a_attr && tmp[i].a_attr.id) {
  8091.                                                         delete tmp[i].a_attr.id;
  8092.                                                 }
  8093.                                         }
  8094.                                 }
  8095.                         }
  8096.                         else {
  8097.                                 tmp.type = tmp.id && m[tmp.id] && m[tmp.id].type ? m[tmp.id].type : "default";
  8098.                                 if(options && options.no_id) {
  8099.                                         tmp = this._delete_ids(tmp);
  8100.                                 }
  8101.                         }
  8102.                         return tmp;
  8103.                 };
  8104.                 this._delete_ids = function (tmp) {
  8105.                         if($.isArray(tmp)) {
  8106.                                 for(var i = 0, j = tmp.length; i < j; i++) {
  8107.                                         tmp[i] = this._delete_ids(tmp[i]);
  8108.                                 }
  8109.                                 return tmp;
  8110.                         }
  8111.                         delete tmp.id;
  8112.                         if(tmp.li_attr && tmp.li_attr.id) {
  8113.                                 delete tmp.li_attr.id;
  8114.                         }
  8115.                         if(tmp.a_attr && tmp.a_attr.id) {
  8116.                                 delete tmp.a_attr.id;
  8117.                         }
  8118.                         if(tmp.children && $.isArray(tmp.children)) {
  8119.                                 tmp.children = this._delete_ids(tmp.children);
  8120.                         }
  8121.                         return tmp;
  8122.                 };
  8123.                 this.check = function (chk, obj, par, pos, more) {
  8124.                         if(parent.check.call(this, chk, obj, par, pos, more) === false) { return false; }
  8125.                         obj = obj && obj.id ? obj : this.get_node(obj);
  8126.                         par = par && par.id ? par : this.get_node(par);
  8127.                         var m = obj && obj.id ? (more && more.origin ? more.origin : $.jstree.reference(obj.id)) : null, tmp, d, i, j;
  8128.                         m = m && m._model && m._model.data ? m._model.data : null;
  8129.                         switch(chk) {
  8130.                                 case "create_node":
  8131.                                 case "move_node":
  8132.                                 case "copy_node":
  8133.                                         if(chk !== 'move_node' || $.inArray(obj.id, par.children) === -1) {
  8134.                                                 tmp = this.get_rules(par);
  8135.                                                 if(tmp.max_children !== undefined && tmp.max_children !== -1 && tmp.max_children === par.children.length) {
  8136.                                                         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 }) };
  8137.                                                         return false;
  8138.                                                 }
  8139.                                                 if(tmp.valid_children !== undefined && tmp.valid_children !== -1 && $.inArray((obj.type || 'default'), tmp.valid_children) === -1) {
  8140.                                                         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 }) };
  8141.                                                         return false;
  8142.                                                 }
  8143.                                                 if(m && obj.children_d && obj.parents) {
  8144.                                                         d = 0;
  8145.                                                         for(i = 0, j = obj.children_d.length; i < j; i++) {
  8146.                                                                 d = Math.max(d, m[obj.children_d[i]].parents.length);
  8147.                                                         }
  8148.                                                         d = d - obj.parents.length + 1;
  8149.                                                 }
  8150.                                                 if(d <= 0 || d === undefined) { d = 1; }
  8151.                                                 do {
  8152.                                                         if(tmp.max_depth !== undefined && tmp.max_depth !== -1 && tmp.max_depth < d) {
  8153.                                                                 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 }) };
  8154.                                                                 return false;
  8155.                                                         }
  8156.                                                         par = this.get_node(par.parent);
  8157.                                                         tmp = this.get_rules(par);
  8158.                                                         d++;
  8159.                                                 } while(par);
  8160.                                         }
  8161.                                         break;
  8162.                         }
  8163.                         return true;
  8164.                 };
  8165.                 /**
  8166.                  * used to retrieve the type settings object for a node
  8167.                  * @name get_rules(obj)
  8168.                  * @param {mixed} obj the node to find the rules for
  8169.                  * @return {Object}
  8170.                  * @plugin types
  8171.                  */
  8172.                 this.get_rules = function (obj) {
  8173.                         obj = this.get_node(obj);
  8174.                         if(!obj) { return false; }
  8175.                         var tmp = this.get_type(obj, true);
  8176.                         if(tmp.max_depth === undefined) { tmp.max_depth = -1; }
  8177.                         if(tmp.max_children === undefined) { tmp.max_children = -1; }
  8178.                         if(tmp.valid_children === undefined) { tmp.valid_children = -1; }
  8179.                         return tmp;
  8180.                 };
  8181.                 /**
  8182.                  * used to retrieve the type string or settings object for a node
  8183.                  * @name get_type(obj [, rules])
  8184.                  * @param {mixed} obj the node to find the rules for
  8185.                  * @param {Boolean} rules if set to `true` instead of a string the settings object will be returned
  8186.                  * @return {String|Object}
  8187.                  * @plugin types
  8188.                  */
  8189.                 this.get_type = function (obj, rules) {
  8190.                         obj = this.get_node(obj);
  8191.                         return (!obj) ? false : ( rules ? $.extend({ 'type' : obj.type }, this.settings.types[obj.type]) : obj.type);
  8192.                 };
  8193.                 /**
  8194.                  * used to change a node's type
  8195.                  * @name set_type(obj, type)
  8196.                  * @param {mixed} obj the node to change
  8197.                  * @param {String} type the new type
  8198.                  * @plugin types
  8199.                  */
  8200.                 this.set_type = function (obj, type) {
  8201.                         var m = this._model.data, t, t1, t2, old_type, old_icon, k, d, a;
  8202.                         if($.isArray(obj)) {
  8203.                                 obj = obj.slice();
  8204.                                 for(t1 = 0, t2 = obj.length; t1 < t2; t1++) {
  8205.                                         this.set_type(obj[t1], type);
  8206.                                 }
  8207.                                 return true;
  8208.                         }
  8209.                         t = this.settings.types;
  8210.                         obj = this.get_node(obj);
  8211.                         if(!t[type] || !obj) { return false; }
  8212.                         d = this.get_node(obj, true);
  8213.                         if (d && d.length) {
  8214.                                 a = d.children('.jstree-anchor');
  8215.                         }
  8216.                         old_type = obj.type;
  8217.                         old_icon = this.get_icon(obj);
  8218.                         obj.type = type;
  8219.                         if(old_icon === true || !t[old_type] || (t[old_type].icon !== undefined && old_icon === t[old_type].icon)) {
  8220.                                 this.set_icon(obj, t[type].icon !== undefined ? t[type].icon : true);
  8221.                         }
  8222.  
  8223.                         // remove old type props
  8224.                         if(t[old_type] && t[old_type].li_attr !== undefined && typeof t[old_type].li_attr === 'object') {
  8225.                                 for (k in t[old_type].li_attr) {
  8226.                                         if (t[old_type].li_attr.hasOwnProperty(k)) {
  8227.                                                 if (k === 'id') {
  8228.                                                         continue;
  8229.                                                 }
  8230.                                                 else if (k === 'class') {
  8231.                                                         m[obj.id].li_attr['class'] = (m[obj.id].li_attr['class'] || '').replace(t[old_type].li_attr[k], '');
  8232.                                                         if (d) { d.removeClass(t[old_type].li_attr[k]); }
  8233.                                                 }
  8234.                                                 else if (m[obj.id].li_attr[k] === t[old_type].li_attr[k]) {
  8235.                                                         m[obj.id].li_attr[k] = null;
  8236.                                                         if (d) { d.removeAttr(k); }
  8237.                                                 }
  8238.                                         }
  8239.                                 }
  8240.                         }
  8241.                         if(t[old_type] && t[old_type].a_attr !== undefined && typeof t[old_type].a_attr === 'object') {
  8242.                                 for (k in t[old_type].a_attr) {
  8243.                                         if (t[old_type].a_attr.hasOwnProperty(k)) {
  8244.                                                 if (k === 'id') {
  8245.                                                         continue;
  8246.                                                 }
  8247.                                                 else if (k === 'class') {
  8248.                                                         m[obj.id].a_attr['class'] = (m[obj.id].a_attr['class'] || '').replace(t[old_type].a_attr[k], '');
  8249.                                                         if (a) { a.removeClass(t[old_type].a_attr[k]); }
  8250.                                                 }
  8251.                                                 else if (m[obj.id].a_attr[k] === t[old_type].a_attr[k]) {
  8252.                                                         if (k === 'href') {
  8253.                                                                 m[obj.id].a_attr[k] = '#';
  8254.                                                                 if (a) { a.attr('href', '#'); }
  8255.                                                         }
  8256.                                                         else {
  8257.                                                                 delete m[obj.id].a_attr[k];
  8258.                                                                 if (a) { a.removeAttr(k); }
  8259.                                                         }
  8260.                                                 }
  8261.                                         }
  8262.                                 }
  8263.                         }
  8264.  
  8265.                         // add new props
  8266.                         if(t[type].li_attr !== undefined && typeof t[type].li_attr === 'object') {
  8267.                                 for (k in t[type].li_attr) {
  8268.                                         if (t[type].li_attr.hasOwnProperty(k)) {
  8269.                                                 if (k === 'id') {
  8270.                                                         continue;
  8271.                                                 }
  8272.                                                 else if (m[obj.id].li_attr[k] === undefined) {
  8273.                                                         m[obj.id].li_attr[k] = t[type].li_attr[k];
  8274.                                                         if (d) {
  8275.                                                                 if (k === 'class') {
  8276.                                                                         d.addClass(t[type].li_attr[k]);
  8277.                                                                 }
  8278.                                                                 else {
  8279.                                                                         d.attr(k, t[type].li_attr[k]);
  8280.                                                                 }
  8281.                                                         }
  8282.                                                 }
  8283.                                                 else if (k === 'class') {
  8284.                                                         m[obj.id].li_attr['class'] = t[type].li_attr[k] + ' ' + m[obj.id].li_attr['class'];
  8285.                                                         if (d) { d.addClass(t[type].li_attr[k]); }
  8286.                                                 }
  8287.                                         }
  8288.                                 }
  8289.                         }
  8290.                         if(t[type].a_attr !== undefined && typeof t[type].a_attr === 'object') {
  8291.                                 for (k in t[type].a_attr) {
  8292.                                         if (t[type].a_attr.hasOwnProperty(k)) {
  8293.                                                 if (k === 'id') {
  8294.                                                         continue;
  8295.                                                 }
  8296.                                                 else if (m[obj.id].a_attr[k] === undefined) {
  8297.                                                         m[obj.id].a_attr[k] = t[type].a_attr[k];
  8298.                                                         if (a) {
  8299.                                                                 if (k === 'class') {
  8300.                                                                         a.addClass(t[type].a_attr[k]);
  8301.                                                                 }
  8302.                                                                 else {
  8303.                                                                         a.attr(k, t[type].a_attr[k]);
  8304.                                                                 }
  8305.                                                         }
  8306.                                                 }
  8307.                                                 else if (k === 'href' && m[obj.id].a_attr[k] === '#') {
  8308.                                                         m[obj.id].a_attr['href'] = t[type].a_attr['href'];
  8309.                                                         if (a) { a.attr('href', t[type].a_attr['href']); }
  8310.                                                 }
  8311.                                                 else if (k === 'class') {
  8312.                                                         m[obj.id].a_attr['class'] = t[type].a_attr['class'] + ' ' + m[obj.id].a_attr['class'];
  8313.                                                         if (a) { a.addClass(t[type].a_attr[k]); }
  8314.                                                 }
  8315.                                         }
  8316.                                 }
  8317.                         }
  8318.  
  8319.                         return true;
  8320.                 };
  8321.         };
  8322.         // include the types plugin by default
  8323.         // $.jstree.defaults.plugins.push("types");
  8324.  
  8325.  
  8326. /**
  8327.  * ### Unique plugin
  8328.  *
  8329.  * Enforces that no nodes with the same name can coexist as siblings.
  8330.  */
  8331.  
  8332.         /**
  8333.          * stores all defaults for the unique plugin
  8334.          * @name $.jstree.defaults.unique
  8335.          * @plugin unique
  8336.          */
  8337.         $.jstree.defaults.unique = {
  8338.                 /**
  8339.                  * Indicates if the comparison should be case sensitive. Default is `false`.
  8340.                  * @name $.jstree.defaults.unique.case_sensitive
  8341.                  * @plugin unique
  8342.                  */
  8343.                 case_sensitive : false,
  8344.                 /**
  8345.                  * Indicates if white space should be trimmed before the comparison. Default is `false`.
  8346.                  * @name $.jstree.defaults.unique.trim_whitespace
  8347.                  * @plugin unique
  8348.                  */
  8349.                 trim_whitespace : false,
  8350.                 /**
  8351.                  * 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)`.
  8352.                  * @name $.jstree.defaults.unique.duplicate
  8353.                  * @plugin unique
  8354.                  */
  8355.                 duplicate : function (name, counter) {
  8356.                         return name + ' (' + counter + ')';
  8357.                 }
  8358.         };
  8359.  
  8360.         $.jstree.plugins.unique = function (options, parent) {
  8361.                 this.check = function (chk, obj, par, pos, more) {
  8362.                         if(parent.check.call(this, chk, obj, par, pos, more) === false) { return false; }
  8363.                         obj = obj && obj.id ? obj : this.get_node(obj);
  8364.                         par = par && par.id ? par : this.get_node(par);
  8365.                         if(!par || !par.children) { return true; }
  8366.                         var n = chk === "rename_node" ? pos : obj.text,
  8367.                                 c = [],
  8368.                                 s = this.settings.unique.case_sensitive,
  8369.                                 w = this.settings.unique.trim_whitespace,
  8370.                                 m = this._model.data, i, j, t;
  8371.                         for(i = 0, j = par.children.length; i < j; i++) {
  8372.                                 t = m[par.children[i]].text;
  8373.                                 if (!s) {
  8374.                                         t = t.toLowerCase();
  8375.                                 }
  8376.                                 if (w) {
  8377.                                         t = t.replace(/^[\s\uFEFF\xA0]+|[\s\uFEFF\xA0]+$/g, '');
  8378.                                 }
  8379.                                 c.push(t);
  8380.                         }
  8381.                         if(!s) { n = n.toLowerCase(); }
  8382.                         if (w) { n = n.replace(/^[\s\uFEFF\xA0]+|[\s\uFEFF\xA0]+$/g, ''); }
  8383.                         switch(chk) {
  8384.                                 case "delete_node":
  8385.                                         return true;
  8386.                                 case "rename_node":
  8387.                                         t = obj.text || '';
  8388.                                         if (!s) {
  8389.                                                 t = t.toLowerCase();
  8390.                                         }
  8391.                                         if (w) {
  8392.                                                 t = t.replace(/^[\s\uFEFF\xA0]+|[\s\uFEFF\xA0]+$/g, '');
  8393.                                         }
  8394.                                         i = ($.inArray(n, c) === -1 || (obj.text && t === n));
  8395.                                         if(!i) {
  8396.                                                 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 }) };
  8397.                                         }
  8398.                                         return i;
  8399.                                 case "create_node":
  8400.                                         i = ($.inArray(n, c) === -1);
  8401.                                         if(!i) {
  8402.                                                 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 }) };
  8403.                                         }
  8404.                                         return i;
  8405.                                 case "copy_node":
  8406.                                         i = ($.inArray(n, c) === -1);
  8407.                                         if(!i) {
  8408.                                                 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 }) };
  8409.                                         }
  8410.                                         return i;
  8411.                                 case "move_node":
  8412.                                         i = ( (obj.parent === par.id && (!more || !more.is_multi)) || $.inArray(n, c) === -1);
  8413.                                         if(!i) {
  8414.                                                 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 }) };
  8415.                                         }
  8416.                                         return i;
  8417.                         }
  8418.                         return true;
  8419.                 };
  8420.                 this.create_node = function (par, node, pos, callback, is_loaded) {
  8421.                         if(!node || node.text === undefined) {
  8422.                                 if(par === null) {
  8423.                                         par = $.jstree.root;
  8424.                                 }
  8425.                                 par = this.get_node(par);
  8426.                                 if(!par) {
  8427.                                         return parent.create_node.call(this, par, node, pos, callback, is_loaded);
  8428.                                 }
  8429.                                 pos = pos === undefined ? "last" : pos;
  8430.                                 if(!pos.toString().match(/^(before|after)$/) && !is_loaded && !this.is_loaded(par)) {
  8431.                                         return parent.create_node.call(this, par, node, pos, callback, is_loaded);
  8432.                                 }
  8433.                                 if(!node) { node = {}; }
  8434.                                 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;
  8435.                                 n = tmp = this.get_string('New node');
  8436.                                 dpc = [];
  8437.                                 for(i = 0, j = par.children.length; i < j; i++) {
  8438.                                         t = m[par.children[i]].text;
  8439.                                         if (!s) {
  8440.                                                 t = t.toLowerCase();
  8441.                                         }
  8442.                                         if (w) {
  8443.                                                 t = t.replace(/^[\s\uFEFF\xA0]+|[\s\uFEFF\xA0]+$/g, '');
  8444.                                         }
  8445.                                         dpc.push(t);
  8446.                                 }
  8447.                                 i = 1;
  8448.                                 t = n;
  8449.                                 if (!s) {
  8450.                                         t = t.toLowerCase();
  8451.                                 }
  8452.                                 if (w) {
  8453.                                         t = t.replace(/^[\s\uFEFF\xA0]+|[\s\uFEFF\xA0]+$/g, '');
  8454.                                 }
  8455.                                 while($.inArray(t, dpc) !== -1) {
  8456.                                         n = cb.call(this, tmp, (++i)).toString();
  8457.                                         t = n;
  8458.                                         if (!s) {
  8459.                                                 t = t.toLowerCase();
  8460.                                         }
  8461.                                         if (w) {
  8462.                                                 t = t.replace(/^[\s\uFEFF\xA0]+|[\s\uFEFF\xA0]+$/g, '');
  8463.                                         }
  8464.                                 }
  8465.                                 node.text = n;
  8466.                         }
  8467.                         return parent.create_node.call(this, par, node, pos, callback, is_loaded);
  8468.                 };
  8469.         };
  8470.  
  8471.         // include the unique plugin by default
  8472.         // $.jstree.defaults.plugins.push("unique");
  8473.  
  8474.  
  8475. /**
  8476.  * ### Wholerow plugin
  8477.  *
  8478.  * Makes each node appear block level. Making selection easier. May cause slow down for large trees in old browsers.
  8479.  */
  8480.  
  8481.         var div = document.createElement('DIV');
  8482.         div.setAttribute('unselectable','on');
  8483.         div.setAttribute('role','presentation');
  8484.         div.className = 'jstree-wholerow';
  8485.         div.innerHTML = '&#160;';
  8486.         $.jstree.plugins.wholerow = function (options, parent) {
  8487.                 this.bind = function () {
  8488.                         parent.bind.call(this);
  8489.  
  8490.                         this.element
  8491.                                 .on('ready.jstree set_state.jstree', $.proxy(function () {
  8492.                                                 this.hide_dots();
  8493.                                         }, this))
  8494.                                 .on("init.jstree loading.jstree ready.jstree", $.proxy(function () {
  8495.                                                 //div.style.height = this._data.core.li_height + 'px';
  8496.                                                 this.get_container_ul().addClass('jstree-wholerow-ul');
  8497.                                         }, this))
  8498.                                 .on("deselect_all.jstree", $.proxy(function (e, data) {
  8499.                                                 this.element.find('.jstree-wholerow-clicked').removeClass('jstree-wholerow-clicked');
  8500.                                         }, this))
  8501.                                 .on("changed.jstree", $.proxy(function (e, data) {
  8502.                                                 this.element.find('.jstree-wholerow-clicked').removeClass('jstree-wholerow-clicked');
  8503.                                                 var tmp = false, i, j;
  8504.                                                 for(i = 0, j = data.selected.length; i < j; i++) {
  8505.                                                         tmp = this.get_node(data.selected[i], true);
  8506.                                                         if(tmp && tmp.length) {
  8507.                                                                 tmp.children('.jstree-wholerow').addClass('jstree-wholerow-clicked');
  8508.                                                         }
  8509.                                                 }
  8510.                                         }, this))
  8511.                                 .on("open_node.jstree", $.proxy(function (e, data) {
  8512.                                                 this.get_node(data.node, true).find('.jstree-clicked').parent().children('.jstree-wholerow').addClass('jstree-wholerow-clicked');
  8513.                                         }, this))
  8514.                                 .on("hover_node.jstree dehover_node.jstree", $.proxy(function (e, data) {
  8515.                                                 if(e.type === "hover_node" && this.is_disabled(data.node)) { return; }
  8516.                                                 this.get_node(data.node, true).children('.jstree-wholerow')[e.type === "hover_node"?"addClass":"removeClass"]('jstree-wholerow-hovered');
  8517.                                         }, this))
  8518.                                 .on("contextmenu.jstree", ".jstree-wholerow", $.proxy(function (e) {
  8519.                                                 if (this._data.contextmenu) {
  8520.                                                         e.preventDefault();
  8521.                                                         var tmp = $.Event('contextmenu', { metaKey : e.metaKey, ctrlKey : e.ctrlKey, altKey : e.altKey, shiftKey : e.shiftKey, pageX : e.pageX, pageY : e.pageY });
  8522.                                                         $(e.currentTarget).closest(".jstree-node").children(".jstree-anchor").first().trigger(tmp);
  8523.                                                 }
  8524.                                         }, this))
  8525.                                 /*!
  8526.                                 .on("mousedown.jstree touchstart.jstree", ".jstree-wholerow", function (e) {
  8527.                                                 if(e.target === e.currentTarget) {
  8528.                                                         var a = $(e.currentTarget).closest(".jstree-node").children(".jstree-anchor");
  8529.                                                         e.target = a[0];
  8530.                                                         a.trigger(e);
  8531.                                                 }
  8532.                                         })
  8533.                                 */
  8534.                                 .on("click.jstree", ".jstree-wholerow", function (e) {
  8535.                                                 e.stopImmediatePropagation();
  8536.                                                 var tmp = $.Event('click', { metaKey : e.metaKey, ctrlKey : e.ctrlKey, altKey : e.altKey, shiftKey : e.shiftKey });
  8537.                                                 $(e.currentTarget).closest(".jstree-node").children(".jstree-anchor").first().trigger(tmp).focus();
  8538.                                         })
  8539.                                 .on("dblclick.jstree", ".jstree-wholerow", function (e) {
  8540.                                                 e.stopImmediatePropagation();
  8541.                                                 var tmp = $.Event('dblclick', { metaKey : e.metaKey, ctrlKey : e.ctrlKey, altKey : e.altKey, shiftKey : e.shiftKey });
  8542.                                                 $(e.currentTarget).closest(".jstree-node").children(".jstree-anchor").first().trigger(tmp).focus();
  8543.                                         })
  8544.                                 .on("click.jstree", ".jstree-leaf > .jstree-ocl", $.proxy(function (e) {
  8545.                                                 e.stopImmediatePropagation();
  8546.                                                 var tmp = $.Event('click', { metaKey : e.metaKey, ctrlKey : e.ctrlKey, altKey : e.altKey, shiftKey : e.shiftKey });
  8547.                                                 $(e.currentTarget).closest(".jstree-node").children(".jstree-anchor").first().trigger(tmp).focus();
  8548.                                         }, this))
  8549.                                 .on("mouseover.jstree", ".jstree-wholerow, .jstree-icon", $.proxy(function (e) {
  8550.                                                 e.stopImmediatePropagation();
  8551.                                                 if(!this.is_disabled(e.currentTarget)) {
  8552.                                                         this.hover_node(e.currentTarget);
  8553.                                                 }
  8554.                                                 return false;
  8555.                                         }, this))
  8556.                                 .on("mouseleave.jstree", ".jstree-node", $.proxy(function (e) {
  8557.                                                 this.dehover_node(e.currentTarget);
  8558.                                         }, this));
  8559.                 };
  8560.                 this.teardown = function () {
  8561.                         if(this.settings.wholerow) {
  8562.                                 this.element.find(".jstree-wholerow").remove();
  8563.                         }
  8564.                         parent.teardown.call(this);
  8565.                 };
  8566.                 this.redraw_node = function(obj, deep, callback, force_render) {
  8567.                         obj = parent.redraw_node.apply(this, arguments);
  8568.                         if(obj) {
  8569.                                 var tmp = div.cloneNode(true);
  8570.                                 //tmp.style.height = this._data.core.li_height + 'px';
  8571.                                 if($.inArray(obj.id, this._data.core.selected) !== -1) { tmp.className += ' jstree-wholerow-clicked'; }
  8572.                                 if(this._data.core.focused && this._data.core.focused === obj.id) { tmp.className += ' jstree-wholerow-hovered'; }
  8573.                                 obj.insertBefore(tmp, obj.childNodes[0]);
  8574.                         }
  8575.                         return obj;
  8576.                 };
  8577.         };
  8578.         // include the wholerow plugin by default
  8579.         // $.jstree.defaults.plugins.push("wholerow");
  8580.         if(window.customElements && Object && Object.create) {
  8581.                 var proto = Object.create(HTMLElement.prototype);
  8582.                 proto.createdCallback = function () {
  8583.                         var c = { core : {}, plugins : [] }, i;
  8584.                         for(i in $.jstree.plugins) {
  8585.                                 if($.jstree.plugins.hasOwnProperty(i) && this.attributes[i]) {
  8586.                                         c.plugins.push(i);
  8587.                                         if(this.getAttribute(i) && JSON.parse(this.getAttribute(i))) {
  8588.                                                 c[i] = JSON.parse(this.getAttribute(i));
  8589.                                         }
  8590.                                 }
  8591.                         }
  8592.                         for(i in $.jstree.defaults.core) {
  8593.                                 if($.jstree.defaults.core.hasOwnProperty(i) && this.attributes[i]) {
  8594.                                         c.core[i] = JSON.parse(this.getAttribute(i)) || this.getAttribute(i);
  8595.                                 }
  8596.                         }
  8597.                         $(this).jstree(c);
  8598.                 };
  8599.                 // proto.attributeChangedCallback = function (name, previous, value) { };
  8600.                 try {
  8601.                         window.customElements.define("vakata-jstree", function() {}, { prototype: proto });
  8602.                 } catch (ignore) { }
  8603.         }
  8604.  
  8605. }));