diff --git a/.gitignore b/.gitignore index 425af745..6f0bf15f 100644 --- a/.gitignore +++ b/.gitignore @@ -3,4 +3,4 @@ /jstree.sublime-project /jstree.sublime-workspace /node_modules -/documentation.bat \ No newline at end of file +/favicon.ico \ No newline at end of file diff --git a/dist/jstree.js b/dist/jstree.js index ba4215a0..4e4cad7f 100644 --- a/dist/jstree.js +++ b/dist/jstree.js @@ -633,7 +633,8 @@ if(obj === -1 || !obj || !obj.length) { return false; } - var o = obj.parentNode.parentNode; + var o = obj[0].parentNode.parentNode; + return o.tagName === 'LI' ? $(o) : -1; }, /** @@ -2437,7 +2438,7 @@ var t = $(v), d = t.data('jstree'); if(d && d.children && t.children('ul').length === 0) { - this._progressive_data_clean(d.children); + this._progressive_data_clean(d.children, e.type === 'select_node'); } }, this)); }, this)); @@ -2524,7 +2525,7 @@ }, this)); } }; - this._progressive_data_clean = function (data) { + this._progressive_data_clean = function (data, is_select) { if(!this.settings.checkbox.three_state) { return false; } for(var i = 0, j = data.length; i < j; i++) { if(data[i].data && data[i].data.jstree) { @@ -2535,7 +2536,7 @@ delete data[i].data.jstree.undetermined; } if(data[i].children) { - this._progressive_data_clean(data[i].children); + this._progressive_data_clean(data[i].children, is_select); } } } @@ -3609,7 +3610,7 @@ this.element .bind("after_close.jstree", $.proxy(function (e, data) { var t = $(data.node); - if(this.settings.json.progressive_unload) { + if(this.settings.json.progressive_unload && t.find('.jstree-clicked:eq(0)').length === 0) { t.data('jstree').children = this.get_json(t)[0].children; t.children("ul").remove(); } @@ -3618,24 +3619,22 @@ }; this.parse_json = function (node) { var s = this.settings.json; - if($.isArray(node.children) && (this._data.core.ready || !this.settings.core.expand_selected_onload || !this._has_selected(node.children))) { - if(s.progressive_render) { - if(!node.data) { node.data = {}; } - if(!node.data.jstree) { node.data.jstree = {}; } - node.data.jstree.children = node.children; - node.children = true; - } + if(s.progressive_render && $.isArray(node.children) && !this._json_has_selected(node.children)) { + if(!node.data) { node.data = {}; } + if(!node.data.jstree) { node.data.jstree = {}; } + node.data.jstree.children = node.children; + node.children = true; } return parent.parse_json.call(this, node); }; - this._has_selected = function (data) { + this._json_has_selected = function (data) { var r = false; for(var i = 0, j = data.length; i < j; i++) { if(data[i].data && data[i].data.jstree && data[i].data.jstree.selected) { r = true; } else if(data[i].children) { - r = r || this._has_selected(data[i].children); + r = r || this._json_has_selected(data[i].children); } else { r = false; diff --git a/dist/jstree.min.js b/dist/jstree.min.js index 060c0ffa..2b4cfdb2 100644 --- a/dist/jstree.min.js +++ b/dist/jstree.min.js @@ -1,4 +1,4 @@ -/*! jsTree - v2.0.0-alpha - 2013-03-19 - (MIT) */ -(function(e){"use strict";if(!e.jstree){var t=0,n=!1,s=!1,a=[];e.jstree={version:"2.0.0-alpha",defaults:{plugins:[]},plugins:{}},e.jstree.create=function(n,s){var a=new e.jstree.core(++t),r=s;return s=e.extend(!0,{},e.jstree.defaults,s),r&&r.plugins&&(s.plugins=r.plugins),e.each(s.plugins,function(e,t){"core"!==e&&(a=a.plugin(t,s[t]))}),a.init(n,s),a},e.jstree.core=function(e){this._id=e,this._data={core:{themes:{}}}},e.jstree.reference=function(t){return e(t).closest(".jstree").data("jstree")},e.fn.jstree=function(t){var n="string"==typeof t,s=Array.prototype.slice.call(arguments,1),a=null;return this.each(function(){var r=e(this).data("jstree"),i=n&&r?r[t]:null;return a=n&&i?i.apply(r,s):null,r||e(this).data("jstree",new e.jstree.create(this,t)),null!==a&&a!==void 0?!1:void 0}),null!==a&&a!==void 0?a:this},e.expr[":"].jstree=e.expr.createPseudo(function(){return function(t){return e(t).hasClass("jstree")&&e(t).data("jstree")!==void 0}}),e.jstree.defaults.core={strings:!1,check_callback:!0,animation:100,aria_roles:!1,multiple:!0,themes:{name:!1,url:!0,dots:!0,icons:!0,dir:!1},base_height:!1,clean_loaded:!0,correct_loaded:!0,expand_selected_onload:!0},e.jstree.core.prototype={plugin:function(t,n){var s=e.jstree.plugins[t];return s?(this._data[t]={},s.prototype=this,new s(n,this)):this},init:function(t,n){this.element=e(t).addClass("jstree jstree-"+this._id),this.settings=n,this.element.bind("destroyed",e.proxy(this.teardown,this)),this._data.core.ready=!1,this._data.core.loaded=!1,this._data.core.rtl="rtl"===this.element.css("direction"),this.element[this._data.core.rtl?"addClass":"removeClass"]("jstree-rtl"),this.settings.core.aria_roles&&this.element.attr("role","tree"),this._data.core.selected=e(),this.bind(),this.trigger("init"),this._data.core.original_container_html=this.element.find(" > ul > li").clone(!0),this._data.core.original_container_html.find("li").addBack().contents().filter(function(){return 3===this.nodeType&&(!this.nodeValue||/^\s+$/.test(this.nodeValue))}).remove(),this.element.html(""),this._data.core.li_height=this.settings.base_height||this.get_container_ul().children("li:eq(0)").height()||18,this.load_node(-1)},destroy:function(){this.element.unbind("destroyed",this.teardown),this.teardown()},teardown:function(){this.unbind(),this.element.removeClass("jstree").removeData("jstree").find("[class^='jstree']").addBack().attr("class",function(){return this.className.replace(/jstree[^ ]*|$/gi,"")}),this.element=null},bind:function(){e.support.touch&&this.element.addTouch(),this.element.on("dblclick.jstree",function(){if(document.selection&&document.selection.empty)document.selection.empty();else if(window.getSelection){var e=window.getSelection();try{e.removeAllRanges(),e.collapse()}catch(t){}}}).on("click.jstree",".jstree-ocl",e.proxy(function(e){this.toggle_node(e.target)},this)).on("click.jstree",".jstree-anchor",e.proxy(function(e){e.preventDefault(),this.activate_node(e.currentTarget,e)},this)).on("keydown.jstree",".jstree-anchor",e.proxy(function(t){var n=null;switch(t.which){case 13:case 32:t.type="click",e(t.currentTarget).trigger(t);break;case 37:t.preventDefault(),this.is_open(t.currentTarget)?this.close_node(t.currentTarget):(n=this.get_prev(t.currentTarget),n&&n.length&&n.children(".jstree-anchor").focus());break;case 38:t.preventDefault(),n=this.get_prev(t.currentTarget),n&&n.length&&n.children(".jstree-anchor").focus();break;case 39:t.preventDefault(),this.is_closed(t.currentTarget)?this.open_node(t.currentTarget):(n=this.get_next(t.currentTarget),n&&n.length&&n.children(".jstree-anchor").focus());break;case 40:t.preventDefault(),n=this.get_next(t.currentTarget),n&&n.length&&n.children(".jstree-anchor").focus();break;default:}},this)).on("create_node.jstree",e.proxy(function(e,t){this.clean_node(t.node)},this)).on("load_node.jstree",e.proxy(function(t,n){if(n.status){if(this.settings.core.clean_loaded)if(-1===n.node){var s=this.get_container_ul().detach();s.children("li").length&&this.clean_node(s.children("li")),this.element.prepend(s)}else{var a=this._data.core.selected.length;n.node.children("ul").children("li").length&&this.clean_node(n.node.children("ul").children("li")),this._data.core.ready&&a!==this._data.core.selected.length&&this.trigger("changed",{action:"clean_node",selected:this._data.core.selected})}-1!==n.node||this._data.core.loaded||(this._data.core.loaded=!0,this.trigger("loaded")),this._data.core.ready||this.get_container_ul().find(".jstree-loading:eq(0)").length||(this._data.core.ready=!0,this._data.core.selected.length&&(this.settings.core.expand_selected_onload&&this._data.core.selected.parents(".jstree-closed").each(e.proxy(function(e,t){this.open_node(t,!1,0)},this)),this.trigger("changed",{action:"ready",selected:this._data.core.selected})),this.trigger("ready"))}},this)).on("loaded.jstree",e.proxy(function(e,t){this.settings.core.correct_loaded&&t.instance.get_container_ul().children("li").each(function(){t.instance.correct_node(this)})},this)).on("open_node.jstree",e.proxy(function(e,t){this.settings.core.correct_loaded&&t.node.children("li").each(function(){t.instance.correct_node(this)})},this)).on("init.jstree",e.proxy(function(){var e=this.settings.core.themes;this._data.core.themes.dots=e.dots,this._data.core.themes.icons=e.icons,e.name===!1&&(e.name="default"),this.set_theme(e.name,e.url)},this)).on("loaded.jstree",e.proxy(function(){this[this._data.core.themes.dots?"show_dots":"hide_dots"](),this[this._data.core.themes.icons?"show_icons":"hide_icons"]()},this)).on("focus.jstree",".jstree-anchor",e.proxy(function(t){e(t.currentTarget).mouseenter()},this)).on("blur.jstree",".jstree-anchor",e.proxy(function(t){e(t.currentTarget).mouseleave()},this)).on("mouseenter.jstree",".jstree-anchor",e.proxy(function(e){var t=this.element.find(".jstree-anchor:focus").not(".jstree-clicked");t&&t.length&&t[0]!==e.currentTarget&&t.blur(),this.hover_node(e.currentTarget)},this)).on("mouseleave.jstree",".jstree-anchor",e.proxy(function(e){this.dehover_node(e.currentTarget)},this))},unbind:function(){this.element.off(".jstree"),e(document).off(".jstree-"+this._id)},trigger:function(t,n,s){n||(n={}),n.instance=this,"number"===(typeof s).toLowerCase()?setTimeout(e.proxy(function(){this.element.triggerHandler(t.replace(".jstree","")+".jstree",n)},this),s):this.element.triggerHandler(t.replace(".jstree","")+".jstree",n)},get_container:function(){return this.element},get_container_ul:function(){return this.element.children("ul:eq(0)")},get_string:function(t){var n=this.settings.core.strings;return e.isFunction(n)?n.call(this,t):n&&n[t]?n[t]:t},_firstChild:function(e){for(e=e?e.firstChild:null;null!==e&&1!==e.nodeType;)e=e.nextSibling;return e},_nextSibling:function(e){for(e=e?e.nextSibling:null;null!==e&&1!==e.nodeType;)e=e.nextSibling;return e},_previousSibling:function(e){for(e=e?e.previousSibling:null;null!==e&&1!==e.nodeType;)e=e.previousSibling;return e},get_node:function(t){return-1===t?-1:(t=e(t,this.element),t.hasClass(".jstree")?-1:(t=t.closest("li",this.element),t.length?t:!1))},get_next:function(t,n){var s;return t=this.get_node(t),-1===t?(s=this._firstChild(this.get_container_ul()[0]),s?e(s):!1):t&&t.length?n?(s=this._nextSibling(t[0]),s?e(s):!1):t.hasClass("jstree-open")?(s=this._firstChild(t.children("ul")[0]),s?e(s):!1):(s=this._nextSibling(t[0]))?e(s):t.parentsUntil(".jstree","li").next("li").eq(0):!1},get_prev:function(t,n){var s;if(t=this.get_node(t),-1===t)return s=this.get_container_ul()[0].lastChild,s?e(s):!1;if(!t||!t.length)return!1;if(n)return s=this._previousSibling(t[0]),s?e(s):!1;if(s=this._previousSibling(t[0])){for(t=e(s);t.hasClass("jstree-open");)t=t.children("ul:eq(0)").children("li:last");return t}return s=t[0].parentNode.parentNode,s&&"LI"===s.tagName?e(s):!1},get_parent:function(t){if(t=this.get_node(t),-1===t||!t||!t.length)return!1;var n=t.parentNode.parentNode;return"LI"===n.tagName?e(n):-1},get_children:function(e){return e=this.get_node(e),-1===e?this.get_container_ul().children("li"):e&&e.length?e.children("ul").children("li"):!1},is_parent:function(e){return e=this.get_node(e),e&&-1!==e&&(this._firstChild(e.children("ul")[0])||e.hasClass("jstree-closed"))},is_loaded:function(e){return e=this.get_node(e),e&&(-1===e&&!this.element.children("ul").children("li.jstree-loading").length||-1!==e&&!e.hasClass("jstree-loading")&&(this._firstChild(e.children("ul")[0])||e.hasClass("jstree-leaf")))},is_loading:function(e){return e=this.get_node(e),e&&(-1===e&&this.element.children("ul").children("li.jstree-loading").length||-1!==e&&e.hasClass("jstree-loading"))},is_open:function(e){return e=this.get_node(e),e&&-1!==e&&e.hasClass("jstree-open")},is_closed:function(e){return e=this.get_node(e),e&&-1!==e&&e.hasClass("jstree-closed")},is_leaf:function(e){return e=this.get_node(e),e&&-1!==e&&e.hasClass("jstree-leaf")},load_node:function(t,n){return(t=this.get_node(t))?(-1!==t&&t.addClass("jstree-loading"),this._load_node(t,e.proxy(function(e){-1!==t&&t.removeClass("jstree-loading"),this.trigger("load_node",{node:t,status:e}),n&&n.call(this,t,e)},this)),!0):(n.call(this,t,!1),!1)},_load_node:function(e,t){-1===e?(this.get_container_ul().empty().append(this._data.core.original_container_html.clone(!0)),t.call(this,!0)):t.call(this,!1)},open_node:function(e,t,n){if(e=this.get_node(e),-1===e||!e||!e.length)return!1;if(n="undefined"===(typeof n).toLowerCase()?this.settings.core.animation:n,!this.is_closed(e))return t&&t.call(this,e,!1),!1;if(this.is_loaded(e)){var s=this;n?e.children("ul").css("display","none").end().removeClass("jstree-closed").addClass("jstree-open").children("ul").stop(!0,!0).slideDown(n,function(){this.style.display="",s.trigger("after_open",{node:e})}):e[0].className=e[0].className.replace("jstree-closed","jstree-open"),t&&t.call(this,e,!0),this.trigger("open_node",{node:e}),n||this.trigger("after_open",{node:e})}else this.load_node(e,function(e,s){return s?this.open_node(e,t,n):t?t.call(this,e,!1):!1})},close_node:function(e,t){if(e=this.get_node(e),!e||!e.length||!this.is_open(e))return!1;t="undefined"===(typeof t).toLowerCase()?this.settings.core.animation:t;var n=this;t?e.children("ul").attr("style","display:block !important").end().removeClass("jstree-open").addClass("jstree-closed").children("ul").stop(!0,!0).slideUp(t,function(){this.style.display="",n.trigger("after_close",{node:e})}):e[0].className=e[0].className.replace("jstree-open","jstree-closed"),this.trigger("close_node",{node:e}),t||this.trigger("after_close",{node:e})},toggle_node:function(e){return this.is_closed(e)?this.open_node(e):this.is_open(e)?this.close_node(e):void 0},open_all:function(e,t,n){e=e?this.get_node(e):-1,e=e&&-1!==e?e:this.get_container_ul(),n=n||e;var s=this;e=this.is_closed(e)?e.find("li.jstree-closed").addBack():e.find("li.jstree-closed"),e.each(function(){s.open_node(this,s.is_loaded(this)?!1:function(e){this.open_all(e,t,n)},t||0)}),0===n.find("li.jstree-closed").length&&this.trigger("open_all",{node:n})},close_all:function(e,t){e=e?this.get_node(e):-1;var n=e&&-1!==e?e:this.get_container_ul(),s=this;n=this.is_open(n)?n.find("li.jstree-open").addBack():n.find("li.jstree-open"),n.each(function(){s.close_node(this,t||0)}),this.trigger("close_all",{node:e})},is_disabled:function(e){return e=this.get_node(e),e&&-1!==e&&e.children(".jstree-anchor").hasClass("jstree-disabled")},enable_node:function(e){return e=this.get_node(e),e&&e.length?(e.children(".jstree-anchor").removeClass("jstree-disabled"),this.trigger("enable_node",{node:e}),void 0):!1},disable_node:function(e){return e=this.get_node(e),e&&e.length?(e.children(".jstree-anchor").addClass("jstree-disabled"),this.trigger("disable_node",{node:e}),void 0):!1},activate_node:function(e,t){return this.is_disabled(e)?!1:(this.settings.core.multiple&&(t.metaKey||t.ctrlKey)?this.is_selected(e)?this.deselect_node(e):this.select_node(e):(this.deselect_all(!0),this.select_node(e)),this.trigger("activate_node",{node:e}),void 0)},hover_node:function(e){return e=this.get_node(e),e&&e.length?(e.children(".jstree-anchor").addClass("jstree-hovered"),this.trigger("hover_node",{node:e}),void 0):!1},dehover_node:function(e){return e=this.get_node(e),e&&e.length?(e.children(".jstree-anchor").removeClass("jstree-hovered"),this.trigger("dehover_node",{node:e}),void 0):!1},select_node:function(e,t,n){if(e=this.get_node(e),!e||!e.length)return!1;if(this._data.core.selected=this._data.core.selected.add(e),this.element.find(".jstree-clicked").removeClass("jstree-clicked"),this._data.core.selected.children(".jstree-anchor").addClass("jstree-clicked"),!n){var s=this;e.parents(".jstree-closed").each(function(){s.open_node(this,!1,0)})}this.trigger("select_node",{node:e,selected:this._data.core.selected}),t||this.trigger("changed",{action:"select_node",node:e,selected:this._data.core.selected})},deselect_node:function(e,t){return e=this.get_node(e),e&&e.length?(this._data.core.selected=this._data.core.selected.not(e),this.element.find(".jstree-clicked").removeClass("jstree-clicked"),this._data.core.selected.children(".jstree-anchor").addClass("jstree-clicked"),this.trigger("deselect_node",{node:e,selected:this._data.core.selected}),t||this.trigger("changed",{action:"deselect_node",node:e,selected:this._data.core.selected}),void 0):!1},deselect_all:function(t){this._data.core.selected=e();var n=this.element.find(".jstree-clicked").removeClass("jstree-clicked");this.trigger("deselect_all",{selected:this._data.core.selected,node:n}),t||this.trigger("changed",{action:"deselect_all",selected:this._data.core.selected,node:n})},is_selected:function(e){return e=this.get_node(e),e&&e.length?this._data.core.selected.index(e)>=0:!1},get_selected:function(){return this._data.core.selected},clean_node:function(t){t=this.get_node(t),t=t&&-1!==t?t.find("li").addBack():this.element.find("li"),t.find("li.jstree-clicked").removeClass("jstree-clicked"),this.settings.core.aria_roles&&t.attr("role","treeitem").parent().attr("role","group");var n=this;return t.each(function(){var t=e(this),s=t.children("a"),a=t.data("jstree"),r=a&&a.opened||t.hasClass("jstree-open")?"open":a&&(a.closed||a.children)||t.children("ul").length?"closed":"leaf";switch(a&&a.opened&&delete a.opened,a&&a.closed&&delete a.closed,t.removeClass("jstree-open jstree-closed jstree-leaf jstree-last"),s.length?e.trim(s.attr("href"))||s.attr("href","#"):(t.contents().filter(function(){return 3===this.nodeType||"UL"!==this.tagName}).wrapAll(''),s=t.children("a"),s.html(t.children("a").html().replace(/[\s\t\n]+$/,""))),s.addClass("jstree-anchor"),t.children("i.jstree-ocl").length||t.prepend(" "),t.next().length||t.addClass("jstree-last"),r){case"leaf":t.addClass("jstree-leaf");break;case"closed":t.addClass("jstree-closed"),n.close_node(t,0);break;case"open":t.addClass("jstree-closed"),n.open_node(t,!1,0)}s.children("i.jstree-themeicon").length||s.prepend(" "),a&&a.icon!==void 0&&(n.set_icon(t,a.icon),delete a.icon),a&&a.selected&&(n.select_node(t,!0,!0),delete a.selected),a&&a.disabled&&(n.disable_node(t),delete a.disabled)})},correct_node:function(t,n){return t=this.get_node(t),!t||-1===t&&!n?!1:(t=-1===t?this.element.find("li"):n?t.find("li").addBack():t,t.each(function(){var t=e(this);switch(!0){case t.hasClass("jstree-open")&&!t.children("ul").children("li").length:t.removeClass("jstree-open").addClass("jstree-leaf").children("ul").remove();break;case t.hasClass("jstree-leaf")&&!!t.children("ul").children("li").length:t.removeClass("jstree-leaf").addClass("jstree-closed")}t[0===t.next().length?"addClass":"removeClass"]("jstree-last")}),t)},get_state:function(){var e={core:{open:[],scroll:{left:this.element.scrollLeft(),top:this.element.scrollTop()},themes:{name:this.get_theme(),icons:this._data.core.themes.icons,dots:this._data.core.themes.dots},selected:[]}};return this.get_container_ul().find(".jstree-open").each(function(){this.id&&e.core.open.push(this.id)}),this._data.core.selected.each(function(){this.id&&e.core.selected.push(this.id)}),e},set_state:function(t,n){if(t){if(t.core){if(e.isArray(t.core.open)){var s=!0,a=this;return e.each(t.core.open.concat([]),function(n,r){r=document.getElementById(r),r&&(a.is_loaded(r)?(a.is_closed(r)&&a.open_node(r,!1,0),e.vakata.array_remove(t.core.open,n)):(a.is_loading(r)||a.open_node(r,e.proxy(function(){this.set_state(t)},a),0),s=!1))}),s&&(delete t.core.open,this.set_state(t,n)),!1}if(t.core.scroll)return t.core.scroll&&t.core.scroll.left!==void 0&&this.element.scrollLeft(t.core.scroll.left),t.core.scroll&&t.core.scroll.top!==void 0&&this.element.scrollTop(t.core.scroll.top),delete t.core.scroll,delete t.core.open,this.set_state(t,n),!1;if(t.core.themes)return t.core.themes.name&&this.set_theme(t.core.themes.name),t.core.themes.dots!==void 0&&this[t.core.themes.dots?"show_dots":"hide_dots"](),t.core.themes.icons!==void 0&&this[t.core.themes.icons?"show_icons":"hide_icons"](),delete t.core.themes,delete t.core.open,this.set_state(t,n),!1;if(t.core.selected){var r=this;return this.deselect_all(),e.each(t.core.selected,function(e,t){r.select_node(document.getElementById(t))}),delete t.core.selected,this.set_state(t,n),!1}return e.isEmptyObject(t)?(n&&n.call(this),this.trigger("set_state"),!1):!0}return!0}return!1},refresh:function(){this._data.core.state=this.get_state(),this.load_node(-1,function(t,n){n&&this.set_state(e.extend(!0,{},this._data.core.state),function(){this.trigger("refresh")}),this._data.core.state=null})},get_text:function(t,n){return t=this.get_node(t),t&&-1!==t&&t.length?(t=t.children("a:eq(0)").clone(),t.children(".jstree-icon").remove(),t=t[n?"text":"html"](),t=e("
")[n?"text":"html"](t),t.html()):!1},set_text:function(e,t){if(e=this.get_node(e),!e||-1===e||!e.length)return!1;e=e.children("a:eq(0)");var n=e.children("I").clone();return e.html(t).prepend(n),this.trigger("set_text",{obj:e,text:t}),!0},parse_json:function(t){var n,s,a,r;return null===t||e.isArray(t)&&0===t.length?!1:e.isArray(t)?(a=e("