/* * jsTree 0.9.8 * http://jstree.com/ * * Copyright (c) 2009 Ivan Bozhanov (vakata.com) * * Dual licensed under the MIT and GPL licenses: * http://www.opensource.org/licenses/mit-license.php * http://www.gnu.org/licenses/gpl.html * * Date: 2009-05-15 * */ (function($) { // jQuery plugin $.fn.tree = function (opts) { return this.each(function() { var conf = $.extend({},opts); if(tree_component.inst && tree_component.inst[$(this).attr('id')]) tree_component.inst[$(this).attr('id')].destroy(); if(conf !== false) new tree_component().init(this, conf); }); }; $.tree_create = function() { return new tree_component(); }; $.tree_focused = function() { return tree_component.inst[tree_component.focused]; }; $.tree_reference = function(id) { return tree_component.inst[id] || null; }; // rollback $.tree_rollback = function(data) { for(var i in data) { if(typeof data[i] == "function") continue; var tmp = tree_component.inst[i]; var lock = !tmp.locked; // if not locked - lock the tree if(lock) tmp.lock(true); // Cancel ongoing rename if(tmp.inp) tmp.inp.val("").blur(); tmp.context.append = false; tmp.container.html(data[i].html).find(".dragged").removeClass("dragged").end().find("div.context").remove(); if(data[i].selected) { tmp.selected = $("#" + data[i].selected); tmp.selected_arr = []; tmp.container .find("a.clicked").each( function () { tmp.selected_arr.push(tmp.get_node(this)); }); } // if this function set the lock - unlock if(lock) tmp.lock(false); delete lock; delete tmp; } }; // core function tree_component () { // instance manager if(typeof tree_component.inst == "undefined") { tree_component.cntr = 0; tree_component.inst = {}; // DRAG'N'DROP STUFF tree_component.drag_drop = { isdown : false, // Is there a drag drag_node : false, // The actual node drag_help : false, // The helper init_x : false, init_y : false, moving : false, origin_tree : false, marker : false, move_type : false, // before, after or inside ref_node : false, // reference node appended : false, // is helper appended foreign : false, // Is the dragged node a foreign one droppable : [], // Array of classes that can be dropped onto the tree open_time : false, // Timeout for opening nodes scroll_time : false // Timeout for scrolling }; // listening for clicks on foreign nodes tree_component.mousedown = function(event) { var tmp = $(event.target); if(tree_component.drag_drop.droppable.length && tmp.is("." + tree_component.drag_drop.droppable.join(", .")) ) { tree_component.drag_drop.drag_help = $("
"); tree_component.drag_drop.drag_node = tree_component.drag_drop.drag_help.find("li:eq(0)"); tree_component.drag_drop.isdown = true; tree_component.drag_drop.foreign = tmp; tmp.blur(); event.preventDefault(); event.stopPropagation(); return false; } event.stopPropagation(); return true; }; tree_component.mouseup = function(event) { var tmp = tree_component.drag_drop; if(tmp.open_time) clearTimeout(tmp.open_time); if(tmp.scroll_time) clearTimeout(tmp.scroll_time); if(tmp.foreign === false && tmp.drag_node && tmp.drag_node.size()) { tmp.drag_help.remove(); if(tmp.move_type) { var tree1 = tree_component.inst[tmp.ref_node.parents(".tree:eq(0)").attr("id")]; if(tree1) tree1.moved(tmp.origin_tree.container.find("li.dragged"), tmp.ref_node, tmp.move_type, false, (tmp.origin_tree.settings.rules.drag_copy == "on" || (tmp.origin_tree.settings.rules.drag_copy == "ctrl" && event.ctrlKey) ) ); } tmp.move_type = false; tmp.ref_node = false; } if(tmp.drag_node && tmp.foreign !== false) { tmp.drag_help.remove(); if(tmp.move_type) { var tree1 = tree_component.inst[tmp.ref_node.parents(".tree:eq(0)").attr("id")]; if(tree1) tree1.settings.callback.ondrop.call(null, tmp.foreign.get(0), tree1.get_node(tmp.ref_node).get(0), tmp.move_type, tree1); } tmp.foreign = false; tmp.move_type = false; tmp.ref_node = false; } // RESET EVERYTHING tree_component.drag_drop.marker.hide(); tmp.drag_help = false; tmp.drag_node = false; tmp.isdown = false; tmp.init_x = false; tmp.init_y = false; tmp.moving = false; tmp.appended = false; $("li.dragged").removeClass("dragged"); tmp.origin_tree = false; event.preventDefault(); event.stopPropagation(); return false; }; tree_component.mousemove = function(event) { var tmp = tree_component.drag_drop; if(tmp.isdown) { if(!tmp.moving && Math.abs(tmp.init_x - event.pageX) < 5 && Math.abs(tmp.init_y - event.pageY) < 5) { event.preventDefault(); event.stopPropagation(); return false; } else tree_component.drag_drop.moving = true; if(tmp.open_time) clearTimeout(tmp.open_time); if(!tmp.appended) { if(tmp.foreign !== false) tmp.origin_tree = $.tree_focused(); $("body").append(tmp.drag_help); tmp.w = tmp.drag_help.width(); tmp.appended = true; } tmp.drag_help.css({ "left" : (event.pageX - (tmp.origin_tree.settings.ui.rtl ? tmp.w : -5 ) ), "top" : (event.pageY + 15) }); if(event.target.tagName == "IMG" && event.target.id == "marker") return false; var et = $(event.target); var cnt = et.is(".tree") ? et : et.parents(".tree:eq(0)"); // if not moving over a tree if(cnt.size() == 0 || !tree_component.inst[cnt.attr("id")]) { if(tmp.scroll_time) clearTimeout(tmp.scroll_time); if(tmp.drag_help.find("IMG").size() == 0) { tmp.drag_help.find("li:eq(0)").append(""); } tmp.move_type = false; tmp.ref_node = false; tree_component.drag_drop.marker.hide(); return false; } var tree2 = tree_component.inst[cnt.attr("id")]; tree2.off_height(); // if moving over another tree and multitree is false if( tmp.foreign === false && tmp.origin_tree.container.get(0) != tree2.container.get(0) && (!tmp.origin_tree.settings.rules.multitree || !tree2.settings.rules.multitree) ) { if(tmp.drag_help.find("IMG").size() == 0) { tmp.drag_help.find("li:eq(0)").append(""); } tmp.move_type = false; tmp.ref_node = false; tree_component.drag_drop.marker.hide(); return false; } if(tmp.scroll_time) clearTimeout(tmp.scroll_time); tmp.scroll_time = setTimeout( function() { tree2.scrollCheck(event.pageX,event.pageY); }, 50); var mov = false; var st = cnt.scrollTop(); if(event.target.tagName == "A" ) { // just in case if hover is over the draggable if(et.is("#jstree-dragged")) return false; if(tree2.get_node(event.target).hasClass("closed")) { tmp.open_time = setTimeout( function () { tree2.open_branch(et); }, 500); } var et_off = et.offset(); var goTo = { x : (et_off.left - 1), y : (event.pageY - et_off.top) }; if(cnt.children("ul:eq(0)").hasClass("rtl")) goTo.x += et.width() - 8; var arr = []; if(goTo.y < tree2.li_height/3 + 1 ) arr = ["before","inside","after"]; else if(goTo.y > tree2.li_height*2/3 - 1 ) arr = ["after","inside","before"]; else { if(goTo.y < tree2.li_height/2) arr = ["inside","before","after"]; else arr = ["inside","after","before"]; } var ok = false; $.each(arr, function(i, val) { if(tree2.checkMove(tmp.origin_tree.container.find("li.dragged"), et, val)) { mov = val; ok = true; return false; } }); if(ok) { switch(mov) { case "before": goTo.y = et_off.top - 2; if(cnt.children("ul:eq(0)").hasClass("rtl")) { tree_component.drag_drop.marker.attr("src", tree2.settings.ui.theme_path + "marker_rtl.gif").width(40); } else { tree_component.drag_drop.marker.attr("src", tree2.settings.ui.theme_path + "marker.gif").width(40); } break; case "after": goTo.y = et_off.top - 2 + tree2.li_height; if(cnt.children("ul:eq(0)").hasClass("rtl")) { tree_component.drag_drop.marker.attr("src", tree2.settings.ui.theme_path + "marker_rtl.gif").width(40); } else { tree_component.drag_drop.marker.attr("src", tree2.settings.ui.theme_path + "marker.gif").width(40); } break; case "inside": goTo.x -= 2; if(cnt.children("ul:eq(0)").hasClass("rtl")) { goTo.x += 36; } goTo.y = et_off.top - 2 + tree2.li_height/2; tree_component.drag_drop.marker.attr("src", tree2.settings.ui.theme_path + "plus.gif").width(11); break; } tmp.move_type = mov; tmp.ref_node = $(event.target); tmp.drag_help.find("IMG").remove(); tree_component.drag_drop.marker.css({ "left" : goTo.x , "top" : goTo.y }).show(); } } if( (et.is(".tree") || et.is("ul") ) && et.find("li:eq(0)").size() == 0) { var et_off = et.offset(); tmp.move_type = "inside"; tmp.ref_node = cnt.children("ul:eq(0)"); tmp.drag_help.find("IMG").remove(); tree_component.drag_drop.marker.attr("src", tree2.settings.ui.theme_path + "plus.gif").width(11); tree_component.drag_drop.marker.css({ "left" : et_off.left + ( cnt.children("ul:eq(0)").hasClass("rtl") ? (cnt.width() - 10) : 10 ) , "top" : et_off.top + 15 }).show(); } else if(event.target.tagName != "A" || !ok) { if(tmp.drag_help.find("IMG").size() == 0) { tmp.drag_help.find("li:eq(0)").append(""); } tmp.move_type = false; tmp.ref_node = false; tree_component.drag_drop.marker.hide(); } event.preventDefault(); event.stopPropagation(); return false; } return true; }; }; return { cntr : ++tree_component.cntr, settings : { data : { type : "predefined", // ENUM [json, xml_flat, xml_nested, predefined] method : "GET", // HOW TO REQUEST FILES async : false, // BOOL - async loading onopen async_data : function (NODE, TREE_OBJ) { return { id : $(NODE).attr("id") || 0 } }, // PARAMETERS PASSED TO SERVER url : false, // FALSE or STRING - url to document to be used (async or not) json : false, // FALSE or OBJECT if type is JSON and async is false - the tree dump as json xml : false // FALSE or STRING if type is XML_FLAT or XML_NESTED and async is false - a string to generate the tree from }, selected : false, // FALSE or STRING or ARRAY opened : [], // ARRAY OF INITIALLY OPENED NODES languages : [], // ARRAY of string values (which will be used as CSS classes - so they must be valid) path : false, // FALSE or STRING (if false - will be autodetected) cookies : false, // FALSE or OBJECT (prefix, open, selected, opts - from jqCookie - expires, path, domain, secure) ui : { dots : true, // BOOL - dots or no dots rtl : false, // BOOL - is the tree right-to-left animation : 0, // INT - duration of open/close animations in miliseconds hover_mode : true, // SHOULD get_* functions chage focus or change hovered item scroll_spd : 4, theme_path : false, // Path to themes theme_name : "default",// Name of theme context : [ { id : "create", label : "Create", icon : "create.png", visible : function (NODE, TREE_OBJ) { if(NODE.length != 1) return false; return TREE_OBJ.check("creatable", NODE); }, action : function (NODE, TREE_OBJ) { TREE_OBJ.create(false, TREE_OBJ.get_node(NODE[0])); } }, "separator", // { // id : "rename", // label : "Rename", // icon : "rename.png", // visible : function (NODE, TREE_OBJ) { if(NODE.length != 1) return false; return TREE_OBJ.check("renameable", NODE); }, // action : function (NODE, TREE_OBJ) { TREE_OBJ.rename(NODE); } // }, { id : "edit", label : "Edit", icon : "rename.png", visible : function (NODE, TREE_OBJ) { var ok = true; $.each(NODE, function () { if(TREE_OBJ.check("editable", this) == false) ok = false; return false; }); return ok; }, action : function (NODE, TREE_OBJ) { $.each(NODE, function () { TREE_OBJ.edit(this); }); } }, { id : "delete", label : "Delete", icon : "remove.png", visible : function (NODE, TREE_OBJ) { var ok = true; $.each(NODE, function () { if(TREE_OBJ.check("deletable", this) == false) ok = false; return false; }); return ok; }, action : function (NODE, TREE_OBJ) { $.each(NODE, function () { TREE_OBJ.remove(this); }); } } ] }, rules : { multiple : false, // FALSE | CTRL | ON - multiple selection off/ with or without holding Ctrl metadata : false, // FALSE or STRING - attribute name (use metadata plugin) type_attr : "rel", // STRING attribute name (where is the type stored if no metadata) multitree : false, // BOOL - is drag n drop between trees allowed createat : "bottom", // STRING (top or bottom) new nodes get inserted at top or bottom use_inline : false, // CHECK FOR INLINE RULES - REQUIRES METADATA clickable : "all", // which node types can the user select | default - all renameable : "all", // which node types can the user select | default - all deletable : "all", // which node types can the user delete | default - all creatable : "all", // which node types can the user create in | default - all draggable : "none", // which node types can the user move | default - none | "all" dragrules : "all", // what move operations between nodes are allowed | default - none | "all" drag_copy : false, // FALSE | CTRL | ON - drag to copy off/ with or without holding Ctrl droppable : [], drag_button : "left", // left, right or both editable : "all" // which node types can the user create in | default - all }, lang : { new_node : "New folder", loading : "Loading ..." }, callback : { // various callbacks to attach custom logic to // before focus - should return true | false beforechange: function(NODE,TREE_OBJ) { return true }, beforeopen : function(NODE,TREE_OBJ) { return true }, beforeclose : function(NODE,TREE_OBJ) { return true }, // before move - should return true | false beforemove : function(NODE,REF_NODE,TYPE,TREE_OBJ) { return true }, // before create - should return true | false beforecreate: function(NODE,REF_NODE,TYPE,TREE_OBJ) { return true }, // before rename - should return true | false beforerename: function(NODE,LANG,TREE_OBJ) { return true }, // before delete - should return true | false beforedelete: function(NODE,TREE_OBJ) { return true }, onJSONdata : function(DATA,TREE_OBJ) { return DATA; }, onselect : function(NODE,TREE_OBJ) { }, // node selected ondeselect : function(NODE,TREE_OBJ) { }, // node deselected onchange : function(NODE,TREE_OBJ) { }, // focus changed onrename : function(NODE,LANG,TREE_OBJ,RB) { }, // node renamed ISNEW - TRUE|FALSE, current language onmove : function(NODE,REF_NODE,TYPE,TREE_OBJ,RB) { }, // move completed (TYPE is BELOW|ABOVE|INSIDE) oncopy : function(NODE,REF_NODE,TYPE,TREE_OBJ,RB) { }, // copy completed (TYPE is BELOW|ABOVE|INSIDE) oncreate : function(NODE,REF_NODE,TYPE,TREE_OBJ,RB) { }, // node created, parent node (TYPE is createat) ondelete : function(NODE,TREE_OBJ,RB) { }, // node deleted onopen : function(NODE,TREE_OBJ) { }, // node opened onopen_all : function(TREE_OBJ) { }, // all nodes opened onclose : function(NODE,TREE_OBJ) { }, // node closed error : function(TEXT,TREE_OBJ) { }, // error occured // double click on node - defaults to open/close & select ondblclk : function(NODE,TREE_OBJ) { TREE_OBJ.toggle_branch.call(TREE_OBJ, NODE); TREE_OBJ.select_branch.call(TREE_OBJ, NODE); }, // right click - to prevent use: EV.preventDefault(); EV.stopPropagation(); return false onrgtclk : function(NODE,TREE_OBJ,EV) { }, onload : function(TREE_OBJ) { }, onfocus : function(TREE_OBJ) { }, ondrop : function(NODE,REF_NODE,TYPE,TREE_OBJ) {} } }, // INITIALIZATION init : function(elem, conf) { var _this = this; this.container = $(elem); if(this.container.size == 0) { alert("Invalid container node!"); return } tree_component.inst[this.cntr] = this; if(!this.container.attr("id")) this.container.attr("id","jstree_" + this.cntr); tree_component.inst[this.container.attr("id")] = tree_component.inst[this.cntr]; tree_component.focused = this.cntr; var opts = $.extend({},conf); // MERGE OPTIONS WITH DEFAULTS if(opts && opts.cookies) { this.settings.cookies = $.extend({},this.settings.cookies,opts.cookies); delete opts.cookies; if(!this.settings.cookies.opts) this.settings.cookies.opts = {}; } if(opts && opts.callback) { this.settings.callback = $.extend({},this.settings.callback,opts.callback); delete opts.callback; } if(opts && opts.data) { this.settings.data = $.extend({},this.settings.data,opts.data); delete opts.data; } if(opts && opts.ui) { this.settings.ui = $.extend({},this.settings.ui,opts.ui); delete opts.ui; } if(opts && opts.rules) { this.settings.rules = $.extend({},this.settings.rules,opts.rules); delete opts.rules; } if(opts && opts.lang) { this.settings.lang = $.extend({},this.settings.lang,opts.lang); delete opts.lang; } this.settings = $.extend({},this.settings,opts); // PATH TO IMAGES AND XSL if(this.settings.path == false) { this.path = ""; $("script").each( function () { if(this.src.toString().match(/tree_component.*?js$/)) { _this.path = this.src.toString().replace(/tree_component.*?js$/, ""); } }); } else this.path = this.settings.path; // DEAL WITH LANGUAGE VERSIONS this.current_lang = this.settings.languages && this.settings.languages.length ? this.settings.languages[0] : false; if(this.settings.languages && this.settings.languages.length) { this.sn = get_sheet_num("tree_component.css"); if(this.sn === false && document.styleSheets.length) this.sn = document.styleSheets.length; var st = false; var id = this.container.attr("id") ? "#" + this.container.attr("id") : ".tree"; for(var ln = 0; ln < this.settings.languages.length; ln++) { st = add_css(id + " ." + this.settings.languages[ln], this.sn); if(st !== false) { if(this.settings.languages[ln] == this.current_lang) st.style.display = ""; else st.style.display = "none"; } } } // DROPPABLES if(this.settings.rules.droppable.length) { for(var i in this.settings.rules.droppable) { if(typeof this.settings.rules.droppable[i] == "function") continue; tree_component.drag_drop.droppable.push(this.settings.rules.droppable[i]); } tree_component.drag_drop.droppable = $.unique(tree_component.drag_drop.droppable); } // THEMES if(this.settings.ui.theme_path === false) this.settings.ui.theme_path = this.path + "themes/"; this.theme = this.settings.ui.theme_path; if(_this.settings.ui.theme_name) { this.theme += _this.settings.ui.theme_name + "/"; if(_this.settings.ui.theme_name != "themeroller" && !tree_component.def_style) { add_sheet(_this.settings.ui.theme_path + "default/style.css"); tree_component.def_style = true; } add_sheet(_this.theme + "style.css"); } this.container.addClass("tree"); if(_this.settings.ui.theme_name != "themeroller") this.container.addClass("tree-default"); if(this.settings.ui.theme_name && this.settings.ui.theme_name != "default") this.container.addClass("tree-" + _this.settings.ui.theme_name); if(this.settings.ui.theme_name == "themeroller") this.container.addClass("ui-widget ui-widget-content"); if(this.settings.rules.multiple) this.selected_arr = []; this.offset = false; // CONTEXT MENU this.context_menu(); this.hovered = false; this.locked = false; // CREATE DUMMY FOR MOVING if(this.settings.rules.draggable != "none" && tree_component.drag_drop.marker === false) { var _this = this; tree_component.drag_drop.marker = $("") .attr({ id : "marker", src : _this.settings.ui.theme_path + "marker.gif" }) .css({ height : "5px", width : "40px", display : "block", position : "absolute", left : "30px", top : "30px", zIndex : "1000" }).hide().appendTo("body"); } this.refresh(); this.attachEvents(); this.focus(); }, off_height : function () { if(this.offset === false) { this.container.css({ position : "relative" }); this.offset = this.container.offset(); var tmp = 0; tmp = parseInt($.curCSS(this.container.get(0), "paddingTop", true),10); if(tmp) this.offset.top += tmp; tmp = parseInt($.curCSS(this.container.get(0), "borderTopWidth", true),10); if(tmp) this.offset.top += tmp; this.container.css({ position : "" }); } if(!this.li_height) { var tmp = this.container.find("ul li.closed, ul li.leaf").eq(0); this.li_height = tmp.height(); if(tmp.children("ul:eq(0)").size()) this.li_height -= tmp.children("ul:eq(0)").height(); if(!this.li_height) this.li_height = 18; } }, context_menu : function () { this.context = false; if(this.settings.ui.context != false) { var str = '
'; for(var i in this.settings.ui.context) { if(typeof this.settings.ui.context[i] == "function") continue; if(this.settings.ui.context[i] == "separator") { str += " "; continue; } var icn = ""; if(this.settings.ui.context[i].icon) icn = 'background-image:url(\'' + ( this.settings.ui.context[i].icon.indexOf("/") == -1 ? this.theme + this.settings.ui.context[i].icon : this.settings.ui.context[i].icon ) + '\');'; str += '' + this.settings.ui.context[i].label + ''; } str += '
'; this.context = $(str); this.context.hide(); this.context.append = false; } }, // REPAINT TREE refresh : function (obj) { if(this.locked) return this.error("LOCKED"); var _this = this; this.is_partial_refresh = obj ? true : false; // SAVE OPENED this.opened = Array(); if(this.settings.cookies && $.cookie(this.settings.cookies.prefix + '_open')) { var str = $.cookie(this.settings.cookies.prefix + '_open'); var tmp = str.split(","); $.each(tmp, function () { if(this.replace(/^#/,"").length > 0) { _this.opened.push("#" + this.replace(/^#/,"")); } }); this.settings.opened = false; } else if(this.settings.opened != false) { $.each(this.settings.opened, function (i, item) { if(this.replace(/^#/,"").length > 0) { _this.opened.push("#" + this.replace(/^#/,"")); } }); this.settings.opened = false; } else { this.container.find("li.open").each(function (i) { if(this.id) { _this.opened.push("#" + this.id); } }); } // SAVE SELECTED if(this.selected) { this.settings.selected = Array(); if(obj) { $(obj).find("li:has(a.clicked)").each(function () { $this = $(this); if($this.attr("id")) _this.settings.selected.push("#" + $this.attr("id")); }); } else { if(this.selected_arr) { $.each(this.selected_arr, function () { if(this.attr("id")) _this.settings.selected.push("#" + this.attr("id")); }); } else { if(this.selected.attr("id")) this.settings.selected.push("#" + this.selected.attr("id")); } } } else if(this.settings.cookies && $.cookie(this.settings.cookies.prefix + '_selected')) { this.settings.selected = Array(); var str = $.cookie(this.settings.cookies.prefix + '_selected'); var tmp = str.split(","); $.each(tmp, function () { if(this.replace(/^#/,"").length > 0) { _this.settings.selected.push("#" + this.replace(/^#/,"")); } }); } else if(this.settings.selected !== false) { var tmp = Array(); if((typeof this.settings.selected).toLowerCase() == "object") { $.each(this.settings.selected, function () { if(this.replace(/^#/,"").length > 0) tmp.push("#" + this.replace(/^#/,"")); }); } else { if(this.settings.selected.replace(/^#/,"").length > 0) tmp.push("#" + this.settings.selected.replace(/^#/,"")); } this.settings.selected = tmp; } if(obj && this.settings.data.async) { this.opened = Array(); obj = this.get_node(obj); obj.find("li.open").each(function (i) { _this.opened.push("#" + this.id); }); if(obj.hasClass("open")) obj.removeClass("open").addClass("closed"); if(obj.hasClass("leaf")) obj.removeClass("leaf"); obj.children("ul:eq(0)").html(""); return this.open_branch(obj, true, function () { _this.reselect.apply(_this); }); } if(this.settings.data.type == "xml_flat" || this.settings.data.type == "xml_nested") { this.scrtop = this.container.get(0).scrollTop; var xsl = (this.settings.data.type == "xml_flat") ? "flat.xsl" : "nested.xsl"; if(this.settings.data.xml) this.container.getTransform(this.path + xsl, this.settings.data.xml, { params : { theme_name : _this.settings.ui.theme_name, theme_path : _this.theme }, meth : _this.settings.data.method, dat : _this.settings.data.async_data.apply(_this,[obj, _this]), callback: function () { _this.context_menu.apply(_this); _this.reselect.apply(_this); } }); else this.container.getTransform(this.path + xsl, this.settings.data.url, { params : { theme_name : _this.settings.ui.theme_name, theme_path : _this.theme }, meth : _this.settings.data.method, dat : _this.settings.data.async_data.apply(_this,[obj, _this]), callback: function () { _this.context_menu.apply(_this); _this.reselect.apply(_this); } }); return; } else if(this.settings.data.type == "json") { if(this.settings.data.json) { var str = ""; if(this.settings.data.json.length) { for(var i = 0; i < this.settings.data.json.length; i++) { str += this.parseJSON(this.settings.data.json[i]); } } else str = this.parseJSON(this.settings.data.json); this.container.html(""); this.container.find("li:last-child").addClass("last").end().find("li:has(ul)").not(".open").addClass("closed"); this.container.find("li").not(".open").not(".closed").addClass("leaf"); this.context_menu(); this.reselect(); } else { var _this = this; $.ajax({ type : this.settings.data.method, url : this.settings.data.url, data : this.settings.data.async_data(false, this), dataType : "json", success : function (data) { data = _this.settings.callback.onJSONdata.call(null, data, _this); var str = ""; if(data.length) { for(var i = 0; i < data.length; i++) { str += _this.parseJSON(data[i]); } } else str = _this.parseJSON(data); _this.container.html(""); _this.container.find("li:last-child").addClass("last").end().find("li:has(ul)").not(".open").addClass("closed"); _this.container.find("li").not(".open").not(".closed").addClass("leaf"); _this.context_menu.apply(_this); _this.reselect.apply(_this); }, error : function (xhttp, textStatus, errorThrown) { _this.error(errorThrown + " " + textStatus); } }); } } else { this.container.children("ul:eq(0)"); this.container.find("li:last-child").addClass("last").end().find("li:has(ul)").not(".open").addClass("closed"); this.container.find("li").not(".open").not(".closed").addClass("leaf"); this.reselect(); } }, // CONVERT JSON TO HTML parseJSON : function (data) { if(!data || !data.data) return ""; var str = ""; str += "
  •  "; } str += ( (typeof data.data[this.settings.languages[i]].title).toLowerCase() != "undefined" ? data.data[this.settings.languages[i]].title : data.data[this.settings.languages[i]] ) + ""; } } else { var attr = {}; attr["href"] = "#"; attr["style"] = ""; attr["class"] = ""; if((typeof data.data.attributes).toLowerCase() != "undefined") { for(var i in data.data.attributes) { if(typeof data.data.attributes[i] == "function") continue; if(i == "style" || i == "class") attr[i] += " " + data.data.attributes[i]; else attr[i] = data.data.attributes[i]; } } if(data.data.icon && this.settings.ui.theme_name != "themeroller") { var icn = data.data.icon.indexOf("/") == -1 ? this.theme + data.data.icon : data.data.icon; attr["style"] += " ; background-image:url('" + icn + "');"; } str += " "; } str += ( (typeof data.data.title).toLowerCase() != "undefined" ? data.data.title : data.data ) + ""; } if(data.children && data.children.length) { str += ''; } str += "
  • "; return str; }, // getJSON from HTML getJSON : function (nod, outer_attrib, inner_attrib, force) { var _this = this; if(!nod || $(nod).size() == 0) { nod = this.container.children("ul").children("li"); } else nod = $(nod); if(nod.size() > 1) { var arr = []; nod.each(function () { arr.push(_this.getJSON(this, outer_attrib, inner_attrib, force)); }); return arr; } if(!outer_attrib) outer_attrib = [ "id", "rel", "class" ]; if(!inner_attrib) inner_attrib = [ ]; var obj = { attributes : {}, data : false }; for(var i in outer_attrib) { if(typeof outer_attrib[i] == "function") continue; var val = (outer_attrib[i] == "class") ? nod.attr(outer_attrib[i]).replace("last","").replace("leaf","").replace("closed","").replace("open","") : nod.attr(outer_attrib[i]); if(typeof val != "undefined" && val.replace(" ","").length > 0) obj.attributes[outer_attrib[i]] = val; delete val; } if(this.settings.languages.length) { obj.data = {}; for(var i in this.settings.languages) { if(typeof this.settings.languages[i] == "function") continue; var a = nod.children("a." + this.settings.languages[i]); if(force || inner_attrib.length || a.get(0).style.backgroundImage.toString().length) { obj.data[this.settings.languages[i]] = {}; obj.data[this.settings.languages[i]].title = a.text(); if(a.get(0).style.backgroundImage.length) { obj.data[this.settings.languages[i]].icon = a.get(0).style.backgroundImage.replace("url(","").replace(")",""); } if(this.settings.ui.theme_name == "themeroller" && a.children("ins").size()) { var tmp = a.children("ins").attr("class"); var cls = false; $.each(tmp.split(" "), function (i, val) { if(val.indexOf("ui-icon-") == 0) { cls = val; return false; } }); if(cls) obj.data[this.settings.languages[i]].icon = cls; } if(inner_attrib.length) { obj.data[this.settings.languages[i]].attributes = {}; for(var j in inner_attrib) { if(typeof inner_attrib[j] == "function") continue; var val = a.attr(inner_attrib[j]); if(typeof val != "undefined" && val.replace(" ","").length > 0) obj.data[this.settings.languages[i]].attributes[inner_attrib[j]] = val; delete val; } } } else { obj.data[this.settings.languages[i]] = a.text(); } } } else { var a = nod.children("a"); if(force || inner_attrib.length || a.get(0).style.backgroundImage.toString().length) { obj.data = {}; obj.data.title = a.text(); if(a.get(0).style.backgroundImage.length) { obj.data.icon = a.get(0).style.backgroundImage.replace("url(","").replace(")",""); } if(this.settings.ui.theme_name == "themeroller" && a.children("ins").size()) { var tmp = a.children("ins").attr("class"); var cls = false; $.each(tmp.split(" "), function (i, val) { if(val.indexOf("ui-icon-") == 0) { cls = val; return false; } }); if(cls) obj.data[this.settings.languages[i]].icon = cls; } if(inner_attrib.length) { obj.data.attributes = {}; for(var j in inner_attrib) { if(typeof inner_attrib[j] == "function") continue; var val = a.attr(inner_attrib[j]); if(typeof val != "undefined" && val.replace(" ","").length > 0) obj.data.attributes[inner_attrib[j]] = val; delete val; } } } else { obj.data = a.text(); } } if(nod.children("ul").size() > 0) { obj.children = []; nod.children("ul").children("li").each(function () { obj.children.push(_this.getJSON(this, outer_attrib, inner_attrib, force)); }); } return obj; }, // getXML from HTML getXML : function (tp, nod, outer_attrib, inner_attrib, cb) { var _this = this; if(tp != "flat") tp = "nested"; if(!nod || $(nod).size() == 0) { nod = this.container.children("ul").children("li"); } else nod = $(nod); if(nod.size() > 1) { var obj = ''; nod.each(function () { obj += _this.getXML(tp, this, outer_attrib, inner_attrib, true); }); obj += ''; return obj; } if(!outer_attrib) outer_attrib = [ "id", "rel", "class" ]; if(!inner_attrib) inner_attrib = [ ]; var obj = ''; if(!cb) obj = ''; obj += ' 0) obj += ' ' + outer_attrib[i] + '="' + val + '" '; delete val; } obj += '>'; obj += ''; if(this.settings.languages.length) { for(var i in this.settings.languages) { if(typeof this.settings.languages[i] == "function") continue; var a = nod.children("a." + this.settings.languages[i]); obj += ' 0) obj += ' ' + inner_attrib[j] + '="' + val + '" '; delete val; } } } obj += '>'; } } else { var a = nod.children("a"); obj += ' 0) obj += ' ' + inner_attrib[j] + '="' + val + '" '; delete val; } } } obj += '>'; } obj += ''; if(tp == "flat") obj += ''; if(nod.children("ul").size() > 0) { nod.children("ul").children("li").each(function () { obj += _this.getXML(tp, this, outer_attrib, inner_attrib, true); }); } if(tp == "nested") obj += ''; if(!cb) obj += ''; return obj; }, focus : function () { if(this.locked) return false; if(tree_component.focused != this.cntr) { tree_component.focused = this.cntr; this.settings.callback.onfocus.call(null, this); } }, show_context : function (obj) { this.context.show(); var tmp = $(obj).children("a:visible").offset(); this.context.css({ "left" : (tmp.left), "top" : (tmp.top + parseInt(obj.children("a:visible").height()) + 2) }); }, hide_context : function () { if(this.context.to_remove && this.context.apply_to) this.context.apply_to.children("a").removeClass("clicked"); this.context.apply_to = false; this.context.hide(); }, // ALL EVENTS attachEvents : function () { var _this = this; this.container .bind("mousedown.jstree", function (event) { if(tree_component.drag_drop.isdown) { tree_component.drag_drop.move_type = false; event.preventDefault(); event.stopPropagation(); event.stopImmediatePropagation(); return false; } }) .bind("mouseup.jstree", function (event) { setTimeout( function() { _this.focus.apply(_this); }, 5); }) .bind("click.jstree", function (event) { //event.stopPropagation(); return true; }); $("#" + this.container.attr("id") + " li") .live("click", function(event) { // WHEN CLICK IS ON THE ARROW if(event.target.tagName != "LI") return true; _this.off_height(); if(event.pageY - $(event.target).offset().top > _this.li_height) return true; _this.toggle_branch.apply(_this, [event.target]); event.stopPropagation(); return false; }); $("#" + this.container.attr("id") + " li a") .live("click.jstree", function (event) { // WHEN CLICK IS ON THE TEXT OR ICON if(event.which && event.which == 3) return true; if(_this.locked) { event.preventDefault(); event.target.blur(); return _this.error("LOCKED"); } _this.select_branch.apply(_this, [event.target, event.ctrlKey || _this.settings.rules.multiple == "on"]); if(_this.inp) { _this.inp.blur(); } event.preventDefault(); event.target.blur(); return false; }) .live("dblclick.jstree", function (event) { // WHEN DOUBLECLICK ON TEXT OR ICON if(_this.locked) { event.preventDefault(); event.stopPropagation(); event.target.blur(); return _this.error("LOCKED"); } _this.settings.callback.ondblclk.call(null, _this.get_node(event.target).get(0), _this); event.preventDefault(); event.stopPropagation(); event.target.blur(); }) .live("contextmenu.jstree", function (event) { if(_this.locked) { event.target.blur(); return _this.error("LOCKED"); } var val = _this.settings.callback.onrgtclk.call(null, _this.get_node(event.target).get(0), _this, event); if(_this.context) { if(_this.context.append == false) { $("body").append(_this.context); _this.context.append = true; for(var i in _this.settings.ui.context) { if(typeof _this.settings.ui.context[i] == "function") continue; if(_this.settings.ui.context[i] == "separator") continue; (function () { var func = _this.settings.ui.context[i].action; _this.context.children("[rel=" + _this.settings.ui.context[i].id +"]") .bind("click", function (event) { if(!$(this).hasClass("disabled")) { func.call(null, _this.context.apply_to || null, _this); _this.hide_context(); } event.stopPropagation(); event.preventDefault(); return false; }) .bind("mouseup", function (event) { this.blur(); if($(this).hasClass("disabled")) { event.stopPropagation(); event.preventDefault(); return false; } }) .bind("mousedown", function (event) { event.stopPropagation(); event.preventDefault(); }); })(); } } var obj = _this.get_node(event.target); if(_this.inp) { _this.inp.blur(); } if(obj) { if(!obj.children("a:eq(0)").hasClass("clicked")) { // _this.select_branch.apply(_this, [event.target, event.ctrlKey || _this.settings.rules.multiple == "on"]); _this.context.apply_to = obj; _this.context.to_remove = true; _this.context.apply_to.children("a").addClass("clicked"); event.target.blur(); } else { _this.context.to_remove = false; _this.context.apply_to = (_this.selected_arr && _this.selected_arr.length > 1) ? _this.selected_arr : _this.selected; } _this.context.children("a").removeClass("disabled").show(); var go = false; for(var i in _this.settings.ui.context) { if(typeof _this.settings.ui.context[i] == "function") continue; if(_this.settings.ui.context[i] == "separator") continue; var state = _this.settings.ui.context[i].visible.call(null, _this.context.apply_to, _this); if(state === false) _this.context.children("[rel=" + _this.settings.ui.context[i].id +"]").addClass("disabled"); if(state === -1) _this.context.children("[rel=" + _this.settings.ui.context[i].id +"]").hide(); else go = true; } if(go == true) _this.show_context(obj); event.preventDefault(); event.stopPropagation(); return false; } } return val; }) .live("mouseover.jstree", function (event) { if(_this.locked) { event.preventDefault(); event.stopPropagation(); return _this.error("LOCKED"); } if( (_this.settings.ui.hover_mode || _this.settings.ui.theme_name == "themeroller" ) && _this.hovered !== false && event.target.tagName == "A") { _this.hovered.children("a").removeClass("hover ui-state-hover"); _this.hovered = false; } if(_this.settings.ui.theme_name == "themeroller") { _this.hover_branch.apply(_this, [event.target]); } }); if(_this.settings.ui.theme_name == "themeroller") { $("#" + this.container.attr("id") + " li a").live("mouseout", function (event) { if(_this.hovered) _this.hovered.children("a").removeClass("hover ui-state-hover"); }); } // ATTACH DRAG & DROP ONLY IF NEEDED if(this.settings.rules.draggable != "none") { $("#" + this.container.attr("id") + " li a") .live("mousedown.jstree", function (event) { if(_this.settings.rules.drag_button == "left" && event.which && event.which != 1) return true; if(_this.settings.rules.drag_button == "right" && event.which && event.which != 3) return true; _this.focus.apply(_this); if(_this.locked) return _this.error("LOCKED"); // SELECT LIST ITEM NODE var obj = _this.get_node(event.target); // IF ITEM IS DRAGGABLE if(_this.settings.rules.multiple != false && _this.selected_arr.length > 1 && obj.children("a:eq(0)").hasClass("clicked")) { var counter = 0; for(var i in _this.selected_arr) { if(typeof _this.selected_arr[i] == "function") continue; if(_this.check("draggable", _this.selected_arr[i])) { _this.selected_arr[i].addClass("dragged"); tree_component.drag_drop.origin_tree = _this; counter ++; } } if(counter > 0) { if(_this.check("draggable", obj)) tree_component.drag_drop.drag_node = obj; else tree_component.drag_drop.drag_node = _this.container.find("li.dragged:eq(0)"); tree_component.drag_drop.isdown = true; tree_component.drag_drop.drag_help = $("
    ").append("