jquery.tree.js 105 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069
  1. /*
  2. * jsTree 0.9.9a
  3. * http://jstree.com/
  4. *
  5. * Copyright (c) 2009 Ivan Bozhanov (vakata.com)
  6. *
  7. * Dual licensed under the MIT and GPL licenses:
  8. * http://www.opensource.org/licenses/mit-license.php
  9. * http://www.gnu.org/licenses/gpl.html
  10. *
  11. * Date: 2009-10-06
  12. *
  13. */
  14. (function ($) {
  15. // jQuery plugin
  16. $.tree = {
  17. datastores: {},
  18. plugins: {},
  19. defaults: {
  20. data: {
  21. async: false, // Are async requests used to load open_branch contents
  22. type: "html", // One of included datastores
  23. opts: { method: "GET", url: false} // Options passed to datastore
  24. },
  25. idprev:'jt_',
  26. selected: false, // FALSE or STRING or ARRAY
  27. opened: [], // ARRAY OF INITIALLY OPENED NODES
  28. languages: [], // ARRAY of string values (which will be used as CSS classes - so they must be valid)
  29. ui: {
  30. dots: true, // BOOL - dots or no dots
  31. animation: 0, // INT - duration of open/close animations in miliseconds
  32. scroll_spd: 4,
  33. theme_path: false, // Path to the theme CSS file - if set to false and theme_name is not false - will lookup jstree-path-here/themes/theme-name-here/style.css
  34. theme_name: "default", // if set to false no theme will be loaded
  35. selected_parent_close: "select_parent", // false, "deselect", "select_parent"
  36. selected_delete: "select_previous" // false, "select_previous"
  37. },
  38. types: {
  39. "default": {
  40. clickable: true, // can be function
  41. renameable: true, // can be function
  42. deletable: true, // can be function
  43. creatable: true, // can be function
  44. draggable: true, // can be function
  45. max_children: -1, // -1 - not set, 0 - no children, 1 - one child, etc // can be function
  46. max_depth: -1, // -1 - not set, 0 - no children, 1 - one level of children, etc // can be function
  47. valid_children: "all", // all, none, array of values // can be function
  48. icon: {
  49. image: false,
  50. position: false
  51. }
  52. }
  53. },
  54. rules: {
  55. multiple: false, // FALSE | CTRL | ON - multiple selection off/ with or without holding Ctrl
  56. multitree: "none", // all, none, array of tree IDs to accept from
  57. type_attr: "rel", // STRING attribute name (where is the type stored as string)
  58. createat: "bottom", // STRING (top or bottom) new nodes get inserted at top or bottom
  59. drag_copy: "ctrl", // FALSE | CTRL | ON - drag to copy off/ with or without holding Ctrl
  60. drag_button: "left", // left, right or both
  61. use_max_children: true,
  62. use_max_depth: true,
  63. max_children: -1,
  64. max_depth: -1,
  65. valid_children: "all"
  66. },
  67. lang: {
  68. new_node: "New folder",
  69. loading: "Loading ..."
  70. },
  71. callback: {
  72. beforechange: function (NODE, TREE_OBJ) { return true },
  73. beforeopen: function (NODE, TREE_OBJ) { return true },
  74. beforeclose: function (NODE, TREE_OBJ) { return true },
  75. beforemove: function (NODE, REF_NODE, TYPE, TREE_OBJ) { return true },
  76. beforecreate: function (NODE, REF_NODE, TYPE, TREE_OBJ) { return true },
  77. beforerename: function (NODE, LANG, TREE_OBJ) { return true },
  78. beforedelete: function (NODE, TREE_OBJ) { return true },
  79. beforedata: function (NODE, TREE_OBJ) { return { id: $(NODE).attr("id") || 0} }, // PARAMETERS PASSED TO SERVER
  80. ondata: function (DATA, TREE_OBJ) { return DATA; }, // modify data before parsing it
  81. onparse: function (STR, TREE_OBJ) { return STR; }, // modify string before visualizing it
  82. onhover: function (NODE, TREE_OBJ) { }, // node hovered
  83. onselect: function (NODE, TREE_OBJ) { }, // node selected
  84. ondeselect: function (NODE, TREE_OBJ) { }, // node deselected
  85. onchange: function (NODE, TREE_OBJ) { }, // focus changed
  86. onrename: function (NODE, TREE_OBJ, RB) { }, // node renamed
  87. onmove: function (NODE, REF_NODE, TYPE, TREE_OBJ, RB) { }, // move completed
  88. oncopy: function (NODE, REF_NODE, TYPE, TREE_OBJ, RB) { }, // copy completed
  89. oncreate: function (NODE, REF_NODE, TYPE, TREE_OBJ, RB) { }, // node created
  90. ondelete: function (NODE, TREE_OBJ, RB) { }, // node deleted
  91. onopen: function (NODE, TREE_OBJ) { }, // node opened
  92. onopen_all: function (TREE_OBJ) { }, // all nodes opened
  93. onclose_all: function (TREE_OBJ) { }, // all nodes closed
  94. onclose: function (NODE, TREE_OBJ) { }, // node closed
  95. error: function (TEXT, TREE_OBJ) { }, // error occured
  96. ondblclk: function (NODE, TREE_OBJ) { TREE_OBJ.toggle_branch.call(TREE_OBJ, NODE); TREE_OBJ.select_branch.call(TREE_OBJ, NODE); },
  97. onrgtclk: function (NODE, TREE_OBJ, EV) { }, // right click - to prevent use: EV.preventDefault(); EV.stopPropagation(); return false
  98. onload: function (TREE_OBJ) { },
  99. oninit: function (TREE_OBJ) { },
  100. onfocus: function (TREE_OBJ) { },
  101. ondestroy: function (TREE_OBJ) { },
  102. onsearch: function (NODES, TREE_OBJ) { NODES.addClass("search"); },
  103. ondrop: function (NODE, REF_NODE, TYPE, TREE_OBJ) { },
  104. check: function (RULE, NODE, VALUE, TREE_OBJ) { return VALUE; },
  105. check_move: function (NODE, REF_NODE, TYPE, TREE_OBJ) { return true; }
  106. },
  107. plugins: {}
  108. },
  109. create: function () { return new tree_component(); },
  110. focused: function () { return tree_component.inst[tree_component.focused]; },
  111. reference: function (obj) {
  112. var o = $(obj);
  113. if (!o.size()) {
  114. obj = obj.replace("#", "");
  115. o = $("#" + obj);
  116. }
  117. if (!o.size()) return null;
  118. o = (o.is(".tree")) ? o.attr("id") : o.parents(".tree:eq(0)").attr("id");
  119. return tree_component.inst[o] || null;
  120. },
  121. rollback: function (data) {
  122. for (var i in data) {
  123. if (!data.hasOwnProperty(i)) continue;
  124. var tmp = tree_component.inst[i];
  125. var lock = !tmp.locked;
  126. // if not locked - lock the tree
  127. if (lock) tmp.lock(true);
  128. // Cancel ongoing rename
  129. tmp.inp = false;
  130. tmp.container.html(data[i].html).find(".dragged").removeClass("dragged").end().find(".hover").removeClass("hover");
  131. if (data[i].selected) {
  132. tmp.selected = $("#" + data[i].selected);
  133. tmp.selected_arr = [];
  134. tmp.container
  135. .find("a.clicked").each(function () {
  136. tmp.selected_arr.push(tmp.get_node(this));
  137. });
  138. }
  139. // if this function set the lock - unlock
  140. if (lock) tmp.lock(false);
  141. delete lock;
  142. delete tmp;
  143. }
  144. },
  145. drop_mode: function (opts) {
  146. opts = $.extend(opts, { show: false, type: "default", str: "Foreign node" });
  147. tree_component.drag_drop.foreign = true;
  148. tree_component.drag_drop.isdown = true;
  149. tree_component.drag_drop.moving = true;
  150. tree_component.drag_drop.appended = false;
  151. tree_component.drag_drop.f_type = opts.type;
  152. tree_component.drag_drop.f_data = opts;
  153. if (!opts.show) {
  154. tree_component.drag_drop.drag_help = false;
  155. tree_component.drag_drop.drag_node = false;
  156. }
  157. else {
  158. tree_component.drag_drop.drag_help = $("<div id='jstree-dragged' class='tree tree-default'><ul><li class='last dragged foreign'><a href='#'><ins>&nbsp;</ins>" + opts.str + "</a></li></ul></div>");
  159. tree_component.drag_drop.drag_node = tree_component.drag_drop.drag_help.find("li:eq(0)");
  160. }
  161. if ($.tree.drag_start !== false) $.tree.drag_start.call(null, false);
  162. },
  163. drag_start: false,
  164. drag: false,
  165. drag_end: false
  166. };
  167. $.fn.tree = function (opts) {
  168. return this.each(function () {
  169. var conf = $.extend({}, opts);
  170. if (tree_component.inst && tree_component.inst[$(this).attr('id')]) tree_component.inst[$(this).attr('id')].destroy();
  171. if (conf !== false) new tree_component().init(this, conf);
  172. });
  173. };
  174. // core
  175. function tree_component() {
  176. return {
  177. cntr: ++tree_component.cntr,
  178. settings: $.extend({}, $.tree.defaults),
  179. init: function (elem, conf) {
  180. var _this = this;
  181. this.container = $(elem);
  182. if (this.container.size == 0) return false;
  183. tree_component.inst[this.cntr] = this;
  184. if (!this.container.attr("id")) this.container.attr("id", "jstree_" + this.cntr);
  185. tree_component.inst[this.container.attr("id")] = tree_component.inst[this.cntr];
  186. tree_component.focused = this.cntr;
  187. this.settings = $.extend(true, {}, this.settings, conf);
  188. // DEAL WITH LANGUAGE VERSIONS
  189. if (this.settings.languages && this.settings.languages.length) {
  190. this.current_lang = this.settings.languages[0];
  191. var st = false;
  192. var id = "#" + this.container.attr("id");
  193. for (var ln = 0; ln < this.settings.languages.length; ln++) {
  194. st = tree_component.add_css(id + " ." + this.settings.languages[ln]);
  195. if (st !== false) st.style.display = (this.settings.languages[ln] == this.current_lang) ? "" : "none";
  196. }
  197. }
  198. else this.current_lang = false;
  199. // THEMES
  200. this.container.addClass("tree");
  201. if (this.settings.ui.theme_name !== false) {
  202. if (this.settings.ui.theme_path === false) {
  203. $("script").each(function () {
  204. if (this.src.toString().match(/jquery\.tree.*?js$/)) { _this.settings.ui.theme_path = this.src.toString().replace(/jquery\.tree.*?js$/, "") + "themes/" + _this.settings.ui.theme_name + "/style.css"; return false; }
  205. });
  206. }
  207. if (this.settings.ui.theme_path != "" && $.inArray(this.settings.ui.theme_path, tree_component.themes) == -1) {
  208. tree_component.add_sheet({ url: this.settings.ui.theme_path });
  209. tree_component.themes.push(this.settings.ui.theme_path);
  210. }
  211. this.container.addClass("tree-" + this.settings.ui.theme_name);
  212. }
  213. // TYPE ICONS
  214. var type_icons = "";
  215. for (var t in this.settings.types) {
  216. if (!this.settings.types.hasOwnProperty(t)) continue;
  217. if (!this.settings.types[t].icon) continue;
  218. if (this.settings.types[t].icon.image || this.settings.types[t].icon.position) {
  219. if (t == "default") type_icons += "#" + this.container.attr("id") + " li > a ins { ";
  220. else type_icons += "#" + this.container.attr("id") + " li[rel=" + t + "] > a ins { ";
  221. if (this.settings.types[t].icon.image) type_icons += " background-image:url(" + this.settings.types[t].icon.image + "); ";
  222. if (this.settings.types[t].icon.position) type_icons += " background-position:" + this.settings.types[t].icon.position + "; ";
  223. type_icons += "} ";
  224. }
  225. }
  226. if (type_icons != "") tree_component.add_sheet({ str: type_icons });
  227. if (this.settings.rules.multiple) this.selected_arr = [];
  228. this.offset = false;
  229. this.hovered = false;
  230. this.locked = false;
  231. if (tree_component.drag_drop.marker === false) tree_component.drag_drop.marker = $("<div>").attr({ id: "jstree-marker" }).hide().appendTo("body");
  232. this.callback("oninit", [this]);
  233. this.refresh();
  234. this.attach_events();
  235. this.focus();
  236. },
  237. refresh: function (obj) {
  238. if (this.locked) return this.error("LOCKED");
  239. var _this = this;
  240. if (obj && !this.settings.data.async) obj = false;
  241. this.is_partial_refresh = obj ? true : false;
  242. // SAVE OPENED
  243. this.opened = Array();
  244. if (this.settings.opened != false) {
  245. $.each(this.settings.opened, function (i, item) {
  246. if (this.replace(/^#/, "").length > 0) { _this.opened.push("#" + this.replace(/^#/, "")); }
  247. });
  248. this.settings.opened = false;
  249. }
  250. else {
  251. this.container.find("li.open").each(function (i) { if (this.id) { _this.opened.push("#" + this.id); } });
  252. }
  253. // SAVE SELECTED
  254. if (this.selected) {
  255. this.settings.selected = Array();
  256. if (obj) {
  257. $(obj).find("li:has(a.clicked)").each(function () {
  258. if (this.id) _this.settings.selected.push("#" + this.id);
  259. });
  260. }
  261. else {
  262. if (this.selected_arr) {
  263. $.each(this.selected_arr, function () {
  264. if (this.attr("id")) _this.settings.selected.push("#" + this.attr("id"));
  265. });
  266. }
  267. else {
  268. if (this.selected.attr("id")) this.settings.selected.push("#" + this.selected.attr("id"));
  269. }
  270. }
  271. }
  272. else if (this.settings.selected !== false) {
  273. var tmp = Array();
  274. if ((typeof this.settings.selected).toLowerCase() == "object") {
  275. $.each(this.settings.selected, function () {
  276. if (this.replace(/^#/, "").length > 0) tmp.push("#" + this.replace(/^#/, ""));
  277. });
  278. }
  279. else {
  280. if (this.settings.selected.replace(/^#/, "").length > 0) tmp.push("#" + this.settings.selected.replace(/^#/, ""));
  281. }
  282. this.settings.selected = tmp;
  283. }
  284. if (obj && this.settings.data.async) {
  285. this.opened = Array();
  286. obj = this.get_node(obj);
  287. obj.find("li.open").each(function (i) { _this.opened.push("#" + this.id); });
  288. if (obj.hasClass("open")) obj.removeClass("open").addClass("closed");
  289. if (obj.hasClass("leaf")) obj.removeClass("leaf");
  290. obj.children("ul:eq(0)").html("");
  291. return this.open_branch(obj, true, function () { _this.reselect.apply(_this); });
  292. }
  293. var _this = this;
  294. var _datastore = new $.tree.datastores[this.settings.data.type]();
  295. if (this.container.children("ul").size() == 0) {
  296. this.container.html("<ul class='ltr' style='direction:ltr;'><li class='last'><a class='loading' href='#'><ins>&nbsp;</ins>" + (this.settings.lang.loading || "Loading ...") + "</a></li></ul>");
  297. }
  298. _datastore.load(this.callback("beforedata", [false, this]), this, this.settings.data.opts, function (data) {
  299. data = _this.callback("ondata", [data, _this]);
  300. _datastore.parse(data, _this, _this.settings.data.opts, function (str) {
  301. str = _this.callback("onparse", [str, _this]);
  302. _this.container.empty().append($("<ul class='ltr'>").html(str));
  303. _this.container.find("li:last-child").addClass("last").end().find("li:has(ul)").not(".open").addClass("closed");
  304. _this.container.find("li").not(".open").not(".closed").addClass("leaf");
  305. _this.reselect();
  306. });
  307. });
  308. },
  309. reselect: function (is_callback) {
  310. var _this = this;
  311. if (!is_callback) this.cl_count = 0;
  312. else this.cl_count--;
  313. // REOPEN BRANCHES
  314. if (this.opened && this.opened.length) {
  315. var opn = false;
  316. for (var j = 0; this.opened && j < this.opened.length; j++) {
  317. if (this.settings.data.async) {
  318. var tmp = this.get_node(this.opened[j]);
  319. if (tmp.size() && tmp.hasClass("closed") > 0) {
  320. opn = true;
  321. var tmp = this.opened[j].toString().replace('/', '\\/');
  322. delete this.opened[j];
  323. this.open_branch(tmp, true, function () { _this.reselect.apply(_this, [true]); });
  324. this.cl_count++;
  325. }
  326. }
  327. else this.open_branch(this.opened[j], true);
  328. }
  329. if (this.settings.data.async && opn) return;
  330. if (this.cl_count > 0) return;
  331. delete this.opened;
  332. }
  333. if (this.cl_count > 0) return;
  334. // DOTS and RIGHT TO LEFT
  335. this.container.css("direction", "ltr").children("ul:eq(0)").addClass("ltr");
  336. if (this.settings.ui.dots == false) this.container.children("ul:eq(0)").addClass("no_dots");
  337. // REPOSITION SCROLL
  338. if (this.scrtop) {
  339. this.container.scrollTop(_this.scrtop);
  340. delete this.scrtop;
  341. }
  342. // RESELECT PREVIOUSLY SELECTED
  343. if (this.settings.selected !== false) {
  344. $.each(this.settings.selected, function (i) {
  345. if (_this.is_partial_refresh) _this.select_branch($(_this.settings.selected[i].toString().replace('/', '\\/'), _this.container), (_this.settings.rules.multiple !== false));
  346. else _this.select_branch($(_this.settings.selected[i].toString().replace('/', '\\/'), _this.container), (_this.settings.rules.multiple !== false && i > 0));
  347. });
  348. this.settings.selected = false;
  349. }
  350. this.callback("onload", [_this]);
  351. },
  352. get: function (obj, format, opts) {
  353. if (!format) format = this.settings.data.type;
  354. if (!opts) opts = this.settings.data.opts;
  355. return new $.tree.datastores[format]().get(obj, this, opts);
  356. },
  357. attach_events: function () {
  358. var _this = this;
  359. this.container
  360. .bind("mousedown.jstree", function (event) {
  361. if (tree_component.drag_drop.isdown) {
  362. tree_component.drag_drop.move_type = false;
  363. event.preventDefault();
  364. event.stopPropagation();
  365. event.stopImmediatePropagation();
  366. return false;
  367. }
  368. })
  369. .bind("mouseup.jstree", function (event) {
  370. setTimeout(function () { _this.focus.apply(_this); }, 5);
  371. })
  372. .bind("click.jstree", function (event) {
  373. //event.stopPropagation();
  374. return true;
  375. });
  376. $("li", this.container.get(0))
  377. .on("click", function (event) { // WHEN CLICK IS ON THE ARROW
  378. if (event.target.tagName != "LI") return true;
  379. _this.off_height();
  380. if (event.pageY - $(event.target).offset().top > _this.li_height) return true;
  381. _this.toggle_branch.apply(_this, [event.target]);
  382. event.stopPropagation();
  383. return false;
  384. });
  385. $("a", this.container.get(0))
  386. .on("click", function (event) { // WHEN CLICK IS ON THE TEXT OR ICON
  387. if (event.which && event.which == 3) return true;
  388. if (_this.locked) {
  389. event.preventDefault();
  390. event.target.blur();
  391. return _this.error("LOCKED");
  392. }
  393. _this.select_branch.apply(_this, [event.target, event.ctrlKey || _this.settings.rules.multiple == "on"]);
  394. if (_this.inp) { _this.inp.blur(); }
  395. event.preventDefault();
  396. event.target.blur();
  397. return false;
  398. })
  399. .on("dblclick", function (event) { // WHEN DOUBLECLICK ON TEXT OR ICON
  400. if (_this.locked) {
  401. event.preventDefault();
  402. event.stopPropagation();
  403. event.target.blur();
  404. return _this.error("LOCKED");
  405. }
  406. _this.callback("ondblclk", [_this.get_node(event.target).get(0), _this]);
  407. event.preventDefault();
  408. event.stopPropagation();
  409. event.target.blur();
  410. })
  411. .on("contextmenu", function (event) {
  412. if (_this.locked) {
  413. event.target.blur();
  414. return _this.error("LOCKED");
  415. }
  416. return _this.callback("onrgtclk", [_this.get_node(event.target).get(0), _this, event]);
  417. })
  418. .on("mouseover", function (event) {
  419. if (_this.locked) {
  420. event.preventDefault();
  421. event.stopPropagation();
  422. return _this.error("LOCKED");
  423. }
  424. if (_this.hovered !== false && (event.target.tagName == "A" || event.target.tagName == "INS")) {
  425. _this.hovered.children("a").removeClass("hover");
  426. _this.hovered = false;
  427. }
  428. _this.callback("onhover", [_this.get_node(event.target).get(0), _this]);
  429. })
  430. .on("mousedown", function (event) {
  431. if (_this.settings.rules.drag_button == "left" && event.which && event.which != 1) return true;
  432. if (_this.settings.rules.drag_button == "right" && event.which && event.which != 3) return true;
  433. _this.focus.apply(_this);
  434. if (_this.locked) return _this.error("LOCKED");
  435. // SELECT LIST ITEM NODE
  436. var obj = _this.get_node(event.target);
  437. // IF ITEM IS DRAGGABLE
  438. if (_this.settings.rules.multiple != false && _this.selected_arr.length > 1 && obj.children("a:eq(0)").hasClass("clicked")) {
  439. var counter = 0;
  440. for (var i in _this.selected_arr) {
  441. if (!_this.selected_arr.hasOwnProperty(i)) continue;
  442. if (_this.check("draggable", _this.selected_arr[i])) {
  443. _this.selected_arr[i].addClass("dragged");
  444. tree_component.drag_drop.origin_tree = _this;
  445. counter++;
  446. }
  447. }
  448. if (counter > 0) {
  449. if (_this.check("draggable", obj)) tree_component.drag_drop.drag_node = obj;
  450. else tree_component.drag_drop.drag_node = _this.container.find("li.dragged:eq(0)");
  451. tree_component.drag_drop.isdown = true;
  452. tree_component.drag_drop.drag_help = $("<div id='jstree-dragged' class='tree " + (_this.settings.ui.theme_name != "" ? " tree-" + _this.settings.ui.theme_name : "") + "' />").append("<ul class='" + _this.container.children("ul:eq(0)").get(0).className + "' />");
  453. var tmp = tree_component.drag_drop.drag_node.clone();
  454. if (_this.settings.languages.length > 0) tmp.find("a").not("." + _this.current_lang).hide();
  455. tree_component.drag_drop.drag_help.children("ul:eq(0)").append(tmp);
  456. tree_component.drag_drop.drag_help.find("li:eq(0)").removeClass("last").addClass("last").children("a").html("<ins>&nbsp;</ins>Multiple selection").end().children("ul").remove();
  457. tree_component.drag_drop.dragged = _this.container.find("li.dragged");
  458. }
  459. }
  460. else {
  461. if (_this.check("draggable", obj)) {
  462. tree_component.drag_drop.drag_node = obj;
  463. tree_component.drag_drop.drag_help = $("<div id='jstree-dragged' class='tree " + (_this.settings.ui.theme_name != "" ? " tree-" + _this.settings.ui.theme_name : "") + "' />").append("<ul class='" + _this.container.children("ul:eq(0)").get(0).className + "' />");
  464. var tmp = obj.clone();
  465. if (_this.settings.languages.length > 0) tmp.find("a").not("." + _this.current_lang).hide();
  466. tree_component.drag_drop.drag_help.children("ul:eq(0)").append(tmp);
  467. tree_component.drag_drop.drag_help.find("li:eq(0)").removeClass("last").addClass("last");
  468. tree_component.drag_drop.isdown = true;
  469. tree_component.drag_drop.foreign = false;
  470. tree_component.drag_drop.origin_tree = _this;
  471. obj.addClass("dragged");
  472. tree_component.drag_drop.dragged = _this.container.find("li.dragged");
  473. }
  474. }
  475. tree_component.drag_drop.init_x = event.pageX;
  476. tree_component.drag_drop.init_y = event.pageY;
  477. obj.blur();
  478. event.preventDefault();
  479. event.stopPropagation();
  480. return false;
  481. });
  482. },
  483. focus: function () {
  484. if (this.locked) return false;
  485. if (tree_component.focused != this.cntr) {
  486. tree_component.focused = this.cntr;
  487. this.callback("onfocus", [this]);
  488. }
  489. },
  490. off_height: function () {
  491. if (this.offset === false) {
  492. this.container.css({ position: "relative" });
  493. this.offset = this.container.offset();
  494. var tmp = 0;
  495. tmp = parseInt($.curCSS(this.container.get(0), "paddingTop", true), 10);
  496. if (tmp) this.offset.top += tmp;
  497. tmp = parseInt($.curCSS(this.container.get(0), "borderTopWidth", true), 10);
  498. if (tmp) this.offset.top += tmp;
  499. this.container.css({ position: "" });
  500. }
  501. if (!this.li_height) {
  502. var tmp = this.container.find("ul li.closed, ul li.leaf").eq(0);
  503. this.li_height = tmp.height();
  504. if (tmp.children("ul:eq(0)").size()) this.li_height -= tmp.children("ul:eq(0)").height();
  505. if (!this.li_height) this.li_height = 18;
  506. }
  507. },
  508. scroll_check: function (x, y) {
  509. var _this = this;
  510. var cnt = _this.container;
  511. var off = _this.container.offset();
  512. var st = cnt.scrollTop();
  513. var sl = cnt.scrollLeft();
  514. // DETECT HORIZONTAL SCROLL
  515. var h_cor = (cnt.get(0).scrollWidth > cnt.width()) ? 40 : 20;
  516. if (y - off.top < 20) cnt.scrollTop(Math.max((st - _this.settings.ui.scroll_spd), 0)); // NEAR TOP
  517. if (cnt.height() - (y - off.top) < h_cor) cnt.scrollTop(st + _this.settings.ui.scroll_spd); // NEAR BOTTOM
  518. if (x - off.left < 20) cnt.scrollLeft(Math.max((sl - _this.settings.ui.scroll_spd), 0)); // NEAR LEFT
  519. if (cnt.width() - (x - off.left) < 40) cnt.scrollLeft(sl + _this.settings.ui.scroll_spd); // NEAR RIGHT
  520. if (cnt.scrollLeft() != sl || cnt.scrollTop() != st) {
  521. tree_component.drag_drop.move_type = false;
  522. tree_component.drag_drop.ref_node = false;
  523. tree_component.drag_drop.marker.hide();
  524. }
  525. tree_component.drag_drop.scroll_time = setTimeout(function () { _this.scroll_check(x, y); }, 50);
  526. },
  527. scroll_into_view: function (obj) {
  528. obj = obj ? this.get_node(obj) : this.selected;
  529. if (!obj) return false;
  530. var off_t = obj.offset().top;
  531. var beg_t = this.container.offset().top;
  532. var end_t = beg_t + this.container.height();
  533. var h_cor = (this.container.get(0).scrollWidth > this.container.width()) ? 40 : 20;
  534. if (off_t + 5 < beg_t) this.container.scrollTop(this.container.scrollTop() - (beg_t - off_t + 5));
  535. if (off_t + h_cor > end_t) this.container.scrollTop(this.container.scrollTop() + (off_t + h_cor - end_t));
  536. },
  537. get_node: function (obj) {
  538. return $(obj).closest("li");
  539. },
  540. get_type: function (obj) {
  541. obj = !obj ? this.selected : this.get_node(obj);
  542. if (!obj) return;
  543. var tmp = obj.attr(this.settings.rules.type_attr);
  544. return tmp || "default";
  545. },
  546. set_type: function (str, obj) {
  547. obj = !obj ? this.selected : this.get_node(obj);
  548. if (!obj || !str) return;
  549. obj.attr(this.settings.rules.type_attr, str);
  550. },
  551. get_text: function (obj, lang) {
  552. obj = this.get_node(obj);
  553. if (!obj || obj.size() == 0) return "";
  554. if (this.settings.languages && this.settings.languages.length) {
  555. lang = lang ? lang : this.current_lang;
  556. obj = obj.children("a." + lang);
  557. }
  558. else obj = obj.children("a:visible");
  559. var val = "";
  560. obj.contents().each(function () {
  561. if (this.nodeType == 3) { val = this.data; return false; }
  562. });
  563. return val;
  564. },
  565. check: function (rule, obj) {
  566. if (this.locked) return false;
  567. var v = false;
  568. // if root node
  569. if (obj === -1) { if (typeof this.settings.rules[rule] != "undefined") v = this.settings.rules[rule]; }
  570. else {
  571. obj = !obj ? this.selected : this.get_node(obj);
  572. if (!obj) return;
  573. var t = this.get_type(obj);
  574. if (typeof this.settings.types[t] != "undefined" && typeof this.settings.types[t][rule] != "undefined") v = this.settings.types[t][rule];
  575. else if (typeof this.settings.types["default"] != "undefined" && typeof this.settings.types["default"][rule] != "undefined") v = this.settings.types["default"][rule];
  576. }
  577. if (typeof v == "function") v = v.call(null, obj, this);
  578. v = this.callback("check", [rule, obj, v, this]);
  579. return v;
  580. },
  581. check_move: function (nod, ref_node, how) {
  582. if (this.locked) return false;
  583. if ($(ref_node).closest("li.dragged").size()) return false;
  584. var tree1 = nod.parents(".tree:eq(0)").get(0);
  585. var tree2 = ref_node.parents(".tree:eq(0)").get(0);
  586. // if different trees
  587. if (tree1 && tree1 != tree2) {
  588. var m = $.tree.reference(tree2.id).settings.rules.multitree;
  589. if (m == "none" || ($.isArray(m) && $.inArray(tree1.id, m) == -1)) return false;
  590. }
  591. var p = (how != "inside") ? this.parent(ref_node) : this.get_node(ref_node);
  592. nod = this.get_node(nod);
  593. if (p == false) return false;
  594. var r = {
  595. max_depth: this.settings.rules.use_max_depth ? this.check("max_depth", p) : -1,
  596. max_children: this.settings.rules.use_max_children ? this.check("max_children", p) : -1,
  597. valid_children: this.check("valid_children", p)
  598. };
  599. var nod_type = (typeof nod == "string") ? nod : this.get_type(nod);
  600. if (typeof r.valid_children != "undefined" && (r.valid_children == "none" || (typeof r.valid_children == "object" && $.inArray(nod_type, $.makeArray(r.valid_children)) == -1))) return false;
  601. if (this.settings.rules.use_max_children) {
  602. if (typeof r.max_children != "undefined" && r.max_children != -1) {
  603. if (r.max_children == 0) return false;
  604. var c_count = 1;
  605. if (tree_component.drag_drop.moving == true && tree_component.drag_drop.foreign == false) {
  606. c_count = tree_component.drag_drop.dragged.size();
  607. c_count = c_count - p.find('> ul > li.dragged').size();
  608. }
  609. if (r.max_children < p.find('> ul > li').size() + c_count) return false;
  610. }
  611. }
  612. if (this.settings.rules.use_max_depth) {
  613. if (typeof r.max_depth != "undefined" && r.max_depth === 0) return this.error("MOVE: MAX-DEPTH REACHED");
  614. // check for max_depth up the chain
  615. var mx = (r.max_depth > 0) ? r.max_depth : false;
  616. var i = 0;
  617. var t = p;
  618. while (t !== -1) {
  619. t = this.parent(t);
  620. i++;
  621. var m = this.check("max_depth", t);
  622. if (m >= 0) {
  623. mx = (mx === false) ? (m - i) : Math.min(mx, m - i);
  624. }
  625. if (mx !== false && mx <= 0) return this.error("MOVE: MAX-DEPTH REACHED");
  626. }
  627. if (mx !== false && mx <= 0) return this.error("MOVE: MAX-DEPTH REACHED");
  628. if (mx !== false) {
  629. var incr = 1;
  630. if (typeof nod != "string") {
  631. var t = nod;
  632. // possible async problem - when nodes are not all loaded down the chain
  633. while (t.size() > 0) {
  634. if (mx - incr < 0) return this.error("MOVE: MAX-DEPTH REACHED");
  635. t = t.children("ul").children("li");
  636. incr++;
  637. }
  638. }
  639. }
  640. }
  641. if (this.callback("check_move", [nod, ref_node, how, this]) == false) return false;
  642. return true;
  643. },
  644. hover_branch: function (obj) {
  645. if (this.locked) return this.error("LOCKED");
  646. var _this = this;
  647. var obj = _this.get_node(obj);
  648. if (!obj.size()) return this.error("HOVER: NOT A VALID NODE");
  649. if (!_this.check("clickable", obj)) return this.error("SELECT: NODE NOT SELECTABLE");
  650. if (this.hovered) this.hovered.children("A").removeClass("hover");
  651. this.hovered = obj;
  652. this.hovered.children("a").addClass("hover");
  653. this.scroll_into_view(this.hovered);
  654. },
  655. select_branch: function (obj, multiple) {
  656. if (this.locked) return this.error("LOCKED");
  657. if (!obj && this.hovered !== false) obj = this.hovered;
  658. var _this = this;
  659. obj = _this.get_node(obj);
  660. if (!obj.size()) return this.error("SELECT: NOT A VALID NODE");
  661. obj.children("a").removeClass("hover");
  662. // CHECK AGAINST RULES FOR SELECTABLE NODES
  663. if (!_this.check("clickable", obj)) return this.error("SELECT: NODE NOT SELECTABLE");
  664. if (_this.callback("beforechange", [obj.get(0), _this]) === false) return this.error("SELECT: STOPPED BY USER");
  665. // IF multiple AND obj IS ALREADY SELECTED - DESELECT IT
  666. if (this.settings.rules.multiple != false && multiple && obj.children("a.clicked").size() > 0) {
  667. return this.deselect_branch(obj);
  668. }
  669. if (this.settings.rules.multiple != false && multiple) {
  670. this.selected_arr.push(obj);
  671. }
  672. if (this.settings.rules.multiple != false && !multiple) {
  673. for (var i in this.selected_arr) {
  674. if (!this.selected_arr.hasOwnProperty(i)) continue;
  675. this.selected_arr[i].children("A").removeClass("clicked");
  676. this.callback("ondeselect", [this.selected_arr[i].get(0), _this]);
  677. }
  678. this.selected_arr = [];
  679. this.selected_arr.push(obj);
  680. if (this.selected && this.selected.children("A").hasClass("clicked")) {
  681. this.selected.children("A").removeClass("clicked");
  682. this.callback("ondeselect", [this.selected.get(0), _this]);
  683. }
  684. }
  685. if (!this.settings.rules.multiple) {
  686. if (this.selected) {
  687. this.selected.children("A").removeClass("clicked");
  688. this.callback("ondeselect", [this.selected.get(0), _this]);
  689. }
  690. }
  691. // SAVE NEWLY SELECTED
  692. this.selected = obj;
  693. if (this.hovered !== false) {
  694. this.hovered.children("A").removeClass("hover");
  695. this.hovered = obj;
  696. }
  697. // FOCUS NEW NODE AND OPEN ALL PARENT NODES IF CLOSED
  698. this.selected.children("a").addClass("clicked").end().parents("li.closed").each(function () { _this.open_branch(this, true); });
  699. // SCROLL SELECTED NODE INTO VIEW
  700. this.scroll_into_view(this.selected);
  701. this.callback("onselect", [this.selected.get(0), _this]);
  702. this.callback("onchange", [this.selected.get(0), _this]);
  703. },
  704. deselect_branch: function (obj) {
  705. if (this.locked) return this.error("LOCKED");
  706. var _this = this;
  707. var obj = this.get_node(obj);
  708. if (obj.children("a.clicked").size() == 0) return this.error("DESELECT: NODE NOT SELECTED");
  709. obj.children("a").removeClass("clicked");
  710. this.callback("ondeselect", [obj.get(0), _this]);
  711. if (this.settings.rules.multiple != false && this.selected_arr.length > 1) {
  712. this.selected_arr = [];
  713. this.container.find("a.clicked").filter(":first-child").parent().each(function () {
  714. _this.selected_arr.push($(this));
  715. });
  716. if (obj.get(0) == this.selected.get(0)) {
  717. this.selected = this.selected_arr[0];
  718. }
  719. }
  720. else {
  721. if (this.settings.rules.multiple != false) this.selected_arr = [];
  722. this.selected = false;
  723. }
  724. this.callback("onchange", [obj.get(0), _this]);
  725. },
  726. toggle_branch: function (obj) {
  727. if (this.locked) return this.error("LOCKED");
  728. var obj = this.get_node(obj);
  729. if (obj.hasClass("closed")) return this.open_branch(obj);
  730. if (obj.hasClass("open")) return this.close_branch(obj);
  731. },
  732. open_branch: function (obj, disable_animation, callback) {
  733. var _this = this;
  734. if (this.locked) return this.error("LOCKED");
  735. var obj = this.get_node(obj);
  736. if (!obj.size()) return this.error("OPEN: NO SUCH NODE");
  737. if (obj.hasClass("leaf")) return this.error("OPEN: OPENING LEAF NODE");
  738. if (this.settings.data.async && obj.find("li").size() == 0) {
  739. if (this.callback("beforeopen", [obj.get(0), this]) === false) return this.error("OPEN: STOPPED BY USER");
  740. obj.children("ul:eq(0)").remove().end().append("<ul><li class='last'><a class='loading' href='#'><ins>&nbsp;</ins>" + (_this.settings.lang.loading || "Loading ...") + "</a></li></ul>");
  741. obj.removeClass("closed").addClass("open");
  742. var _datastore = new $.tree.datastores[this.settings.data.type]();
  743. _datastore.load(this.callback("beforedata", [obj, this]), this, this.settings.data.opts, function (data) {
  744. data = _this.callback("ondata", [data, _this]);
  745. if (!data || data.length == 0) {
  746. obj.removeClass("closed").removeClass("open").addClass("leaf").children("ul").remove();
  747. if (callback) callback.call();
  748. return;
  749. }
  750. _datastore.parse(data, _this, _this.settings.data.opts, function (str) {
  751. str = _this.callback("onparse", [str, _this]);
  752. // if(obj.children('ul:eq(0)').children('li').size() > 1) obj.children("ul").find('.loaading').parent().replaceWith(str); else
  753. obj.children("ul:eq(0)").replaceWith($("<ul>").html(str));
  754. obj.find("li:last-child").addClass("last").end().find("li:has(ul)").not(".open").addClass("closed");
  755. obj.find("li").not(".open").not(".closed").addClass("leaf");
  756. _this.open_branch.apply(_this, [obj]);
  757. if (callback) callback.call();
  758. });
  759. });
  760. return true;
  761. }
  762. else {
  763. if (!this.settings.data.async) {
  764. if (this.callback("beforeopen", [obj.get(0), this]) === false) return this.error("OPEN: STOPPED BY USER");
  765. }
  766. if (parseInt(this.settings.ui.animation) > 0 && !disable_animation) {
  767. obj.children("ul:eq(0)").css("display", "none");
  768. obj.removeClass("closed").addClass("open");
  769. obj.children("ul:eq(0)").slideDown(parseInt(this.settings.ui.animation), function () {
  770. $(this).css("display", "");
  771. if (callback) callback.call();
  772. });
  773. } else {
  774. obj.removeClass("closed").addClass("open");
  775. if (callback) callback.call();
  776. }
  777. this.callback("onopen", [obj.get(0), this]);
  778. return true;
  779. }
  780. },
  781. close_branch: function (obj, disable_animation) {
  782. if (this.locked) return this.error("LOCKED");
  783. var _this = this;
  784. var obj = this.get_node(obj);
  785. if (!obj.size()) return this.error("CLOSE: NO SUCH NODE");
  786. if (_this.callback("beforeclose", [obj.get(0), _this]) === false) return this.error("CLOSE: STOPPED BY USER");
  787. if (parseInt(this.settings.ui.animation) > 0 && !disable_animation && obj.children("ul:eq(0)").size() == 1) {
  788. obj.children("ul:eq(0)").slideUp(parseInt(this.settings.ui.animation), function () {
  789. if (obj.hasClass("open")) obj.removeClass("open").addClass("closed");
  790. $(this).css("display", "");
  791. });
  792. }
  793. else {
  794. if (obj.hasClass("open")) obj.removeClass("open").addClass("closed");
  795. }
  796. if (this.selected && this.settings.ui.selected_parent_close !== false && obj.children("ul:eq(0)").find("a.clicked").size() > 0) {
  797. obj.find("li:has(a.clicked)").each(function () {
  798. _this.deselect_branch(this);
  799. });
  800. if (this.settings.ui.selected_parent_close == "select_parent" && obj.children("a.clicked").size() == 0) this.select_branch(obj, (this.settings.rules.multiple != false && this.selected_arr.length > 0));
  801. }
  802. this.callback("onclose", [obj.get(0), this]);
  803. },
  804. open_all: function (obj, callback) {
  805. if (this.locked) return this.error("LOCKED");
  806. var _this = this;
  807. obj = obj ? this.get_node(obj) : this.container;
  808. var s = obj.find("li.closed").size();
  809. if (!callback) this.cl_count = 0;
  810. else this.cl_count--;
  811. if (s > 0) {
  812. this.cl_count += s;
  813. // maybe add .andSelf()
  814. obj.find("li.closed").each(function () { var __this = this; _this.open_branch.apply(_this, [this, true, function () { _this.open_all.apply(_this, [__this, true]); } ]); });
  815. }
  816. else if (this.cl_count == 0) this.callback("onopen_all", [this]);
  817. },
  818. close_all: function (obj) {
  819. if (this.locked) return this.error("LOCKED");
  820. var _this = this;
  821. obj = obj ? this.get_node(obj) : this.container;
  822. // maybe add .andSelf()
  823. obj.find("li.open").each(function () { _this.close_branch(this, true); });
  824. this.callback("onclose_all", [this]);
  825. },
  826. set_lang: function (i) {
  827. if (!$.isArray(this.settings.languages) || this.settings.languages.length == 0) return false;
  828. if (this.locked) return this.error("LOCKED");
  829. if (!$.inArray(i, this.settings.languages) && typeof this.settings.languages[i] != "undefined") i = this.settings.languages[i];
  830. if (typeof i == "undefined") return false;
  831. if (i == this.current_lang) return true;
  832. var st = false;
  833. var id = "#" + this.container.attr("id");
  834. st = tree_component.get_css(id + " ." + this.current_lang);
  835. if (st !== false) st.style.display = "none";
  836. st = tree_component.get_css(id + " ." + i);
  837. if (st !== false) st.style.display = "";
  838. this.current_lang = i;
  839. return true;
  840. },
  841. get_lang: function () {
  842. if (!$.isArray(this.settings.languages) || this.settings.languages.length == 0) return false;
  843. return this.current_lang;
  844. },
  845. create: function (obj, ref_node, position) {
  846. if (this.locked) return this.error("LOCKED");
  847. var root = false;
  848. if (ref_node == -1) { root = true; ref_node = this.container; }
  849. else ref_node = ref_node ? this.get_node(ref_node) : this.selected;
  850. if (!root && (!ref_node || !ref_node.size())) return this.error("CREATE: NO NODE SELECTED");
  851. var pos = position;
  852. var tmp = ref_node; // for type calculation
  853. if (position == "before") {
  854. position = ref_node.parent().children().index(ref_node);
  855. ref_node = ref_node.parents("li:eq(0)");
  856. }
  857. if (position == "after") {
  858. position = ref_node.parent().children().index(ref_node) + 1;
  859. ref_node = ref_node.parents("li:eq(0)");
  860. }
  861. if (!root && ref_node.size() == 0) { root = true; ref_node = this.container; }
  862. if (!root) {
  863. if (!this.check("creatable", ref_node)) return this.error("CREATE: CANNOT CREATE IN NODE");
  864. if (ref_node.hasClass("closed")) {
  865. if (this.settings.data.async && ref_node.children("ul").size() == 0) {
  866. var _this = this;
  867. return this.open_branch(ref_node, true, function () { _this.create.apply(_this, [obj, ref_node, position]); });
  868. }
  869. else this.open_branch(ref_node, true);
  870. }
  871. }
  872. // creating new object to pass to parseJSON
  873. var torename = false;
  874. if (!obj) obj = {};
  875. else obj = $.extend(true, {}, obj);
  876. if (!obj.attributes) obj.attributes = {};
  877. if (!obj.attributes[this.settings.rules.type_attr]) obj.attributes[this.settings.rules.type_attr] = this.get_type(tmp) || "default";
  878. if (this.settings.languages.length) {
  879. if (!obj.data) { obj.data = {}; torename = true; }
  880. for (var i = 0; i < this.settings.languages.length; i++) {
  881. if (!obj.data[this.settings.languages[i]]) obj.data[this.settings.languages[i]] = ((typeof this.settings.lang.new_node).toLowerCase() != "string" && this.settings.lang.new_node[i]) ? this.settings.lang.new_node[i] : this.settings.lang.new_node;
  882. }
  883. }
  884. else {
  885. if (!obj.data) { obj.data = this.settings.lang.new_node; torename = true; }
  886. }
  887. obj = this.callback("ondata", [obj, this]);
  888. var obj_s = $.tree.datastores.json().parse(obj, this);
  889. obj_s = this.callback("onparse", [obj_s, this]);
  890. var $li = $(obj_s);
  891. if ($li.children("ul").size()) {
  892. if (!$li.is(".open")) $li.addClass("closed");
  893. }
  894. else $li.addClass("leaf");
  895. $li.find("li:last-child").addClass("last").end().find("li:has(ul)").not(".open").addClass("closed");
  896. $li.find("li").not(".open").not(".closed").addClass("leaf");
  897. var r = {
  898. max_depth: this.settings.rules.use_max_depth ? this.check("max_depth", (root ? -1 : ref_node)) : -1,
  899. max_children: this.settings.rules.use_max_children ? this.check("max_children", (root ? -1 : ref_node)) : -1,
  900. valid_children: this.check("valid_children", (root ? -1 : ref_node))
  901. };
  902. var nod_type = this.get_type($li);
  903. if (typeof r.valid_children != "undefined" && (r.valid_children == "none" || ($.isArray(r.valid_children) && $.inArray(nod_type, r.valid_children) == -1))) return this.error("CREATE: NODE NOT A VALID CHILD");
  904. if (this.settings.rules.use_max_children) {
  905. if (typeof r.max_children != "undefined" && r.max_children != -1 && r.max_children >= this.children(ref_node).size()) return this.error("CREATE: MAX_CHILDREN REACHED");
  906. }
  907. if (this.settings.rules.use_max_depth) {
  908. if (typeof r.max_depth != "undefined" && r.max_depth === 0) return this.error("CREATE: MAX-DEPTH REACHED");
  909. // check for max_depth up the chain
  910. var mx = (r.max_depth > 0) ? r.max_depth : false;
  911. var i = 0;
  912. var t = ref_node;
  913. while (t !== -1 && !root) {
  914. t = this.parent(t);
  915. i++;
  916. var m = this.check("max_depth", t);
  917. if (m >= 0) {
  918. mx = (mx === false) ? (m - i) : Math.min(mx, m - i);
  919. }
  920. if (mx !== false && mx <= 0) return this.error("CREATE: MAX-DEPTH REACHED");
  921. }
  922. if (mx !== false && mx <= 0) return this.error("CREATE: MAX-DEPTH REACHED");
  923. if (mx !== false) {
  924. var incr = 1;
  925. var t = $li;
  926. while (t.size() > 0) {
  927. if (mx - incr < 0) return this.error("CREATE: MAX-DEPTH REACHED");
  928. t = t.children("ul").children("li");
  929. incr++;
  930. }
  931. }
  932. }
  933. if ((typeof position).toLowerCase() == "undefined" || position == "inside")
  934. position = (this.settings.rules.createat == "top") ? 0 : ref_node.children("ul:eq(0)").children("li").size();
  935. if (ref_node.children("ul").size() == 0 || (root == true && ref_node.children("ul").children("li").size() == 0)) {
  936. if (!root) var a = this.moved($li, ref_node.children("a:eq(0)"), "inside", true);
  937. else var a = this.moved($li, this.container.children("ul:eq(0)"), "inside", true);
  938. }
  939. else if (pos == "before" && ref_node.children("ul:eq(0)").children("li:nth-child(" + (position + 1) + ")").size())
  940. var a = this.moved($li, ref_node.children("ul:eq(0)").children("li:nth-child(" + (position + 1) + ")").children("a:eq(0)"), "before", true);
  941. else if (pos == "after" && ref_node.children("ul:eq(0)").children("li:nth-child(" + (position) + ")").size())
  942. var a = this.moved($li, ref_node.children("ul:eq(0)").children("li:nth-child(" + (position) + ")").children("a:eq(0)"), "after", true);
  943. else if (ref_node.children("ul:eq(0)").children("li:nth-child(" + (position + 1) + ")").size())
  944. var a = this.moved($li, ref_node.children("ul:eq(0)").children("li:nth-child(" + (position + 1) + ")").children("a:eq(0)"), "before", true);
  945. else
  946. var a = this.moved($li, ref_node.children("ul:eq(0)").children("li:last").children("a:eq(0)"), "after", true);
  947. if (a === false) return this.error("CREATE: ABORTED");
  948. if (torename) {
  949. this.select_branch($li.children("a:eq(0)"));
  950. this.rename();
  951. }
  952. return $li;
  953. },
  954. rename: function (obj, new_name) {
  955. if (this.locked) return this.error("LOCKED");
  956. obj = obj ? this.get_node(obj) : this.selected;
  957. var _this = this;
  958. if (!obj || !obj.size()) return this.error("RENAME: NO NODE SELECTED");
  959. if (!this.check("renameable", obj)) return this.error("RENAME: NODE NOT RENAMABLE");
  960. if (!this.callback("beforerename", [obj.get(0), _this.current_lang, _this])) return this.error("RENAME: STOPPED BY USER");
  961. obj.parents("li.closed").each(function () { _this.open_branch(this) });
  962. if (this.current_lang) obj = obj.find("a." + this.current_lang);
  963. else obj = obj.find("a:first");
  964. // Rollback
  965. var rb = {};
  966. rb[this.container.attr("id")] = this.get_rollback();
  967. var icn = obj.children("ins").clone();
  968. if ((typeof new_name).toLowerCase() == "string") {
  969. obj.text(new_name).prepend(icn);
  970. _this.callback("onrename", [_this.get_node(obj).get(0), _this, rb]);
  971. }
  972. else {
  973. var last_value = "";
  974. obj.contents().each(function () {
  975. if (this.nodeType == 3) { last_value = this.data; return false; }
  976. });
  977. _this.inp = $("<input type='text' autocomplete='off' />");
  978. _this.inp
  979. .val(last_value.replace(/&amp;/g, "&").replace(/&gt;/g, ">").replace(/&lt;/g, "<"))
  980. .bind("mousedown", function (event) { event.stopPropagation(); })
  981. .bind("mouseup", function (event) { event.stopPropagation(); })
  982. .bind("click", function (event) { event.stopPropagation(); })
  983. .bind("keyup", function (event) {
  984. var key = event.keyCode || event.which;
  985. if (key == 27) { this.value = last_value; this.blur(); return }
  986. if (key == 13) { this.blur(); return; }
  987. });
  988. _this.inp.blur(function (event) {
  989. if (this.value == "") this.value = last_value;
  990. obj.text(this.value).prepend(icn);
  991. obj.get(0).style.display = "";
  992. obj.prevAll("span").remove();
  993. _this.inp = false;
  994. _this.callback("onrename", [_this.get_node(obj).get(0), _this, rb]);
  995. });
  996. var spn = $("<span />").addClass(obj.attr("class")).append(icn).append(_this.inp);
  997. obj.get(0).style.display = "none";
  998. obj.parent().prepend(spn);
  999. _this.inp.get(0).focus();
  1000. _this.inp.get(0).select();
  1001. }
  1002. },
  1003. remove: function (obj) {
  1004. if (this.locked) return this.error("LOCKED");
  1005. var _this = this;
  1006. // Rollback
  1007. var rb = {};
  1008. rb[this.container.attr("id")] = this.get_rollback();
  1009. if (obj && (!this.selected || this.get_node(obj).get(0) != this.selected.get(0))) {
  1010. obj = this.get_node(obj);
  1011. if (obj.size()) {
  1012. if (!this.check("deletable", obj)) return this.error("DELETE: NODE NOT DELETABLE");
  1013. if (!this.callback("beforedelete", [obj.get(0), _this])) return this.error("DELETE: STOPPED BY USER");
  1014. $parent = obj.parent();
  1015. if (obj.find("a.clicked").size()) {
  1016. var reset_selected = false;
  1017. _this.selected_arr = [];
  1018. this.container.find("a.clicked").filter(":first-child").parent().each(function () {
  1019. if (!reset_selected && this == _this.selected.get(0)) reset_selected = true;
  1020. if ($(this).parents().index(obj) != -1) return true;
  1021. _this.selected_arr.push($(this));
  1022. });
  1023. if (reset_selected) this.selected = this.selected_arr[0] || false;
  1024. }
  1025. obj = obj.remove();
  1026. $parent.children("li:last").addClass("last");
  1027. if ($parent.children("li").size() == 0) {
  1028. $li = $parent.parents("li:eq(0)");
  1029. $li.removeClass("open").removeClass("closed").addClass("leaf").children("ul").remove();
  1030. }
  1031. this.callback("ondelete", [obj.get(0), this, rb]);
  1032. }
  1033. }
  1034. else if (this.selected) {
  1035. if (!this.check("deletable", this.selected)) return this.error("DELETE: NODE NOT DELETABLE");
  1036. if (!this.callback("beforedelete", [this.selected.get(0), _this])) return this.error("DELETE: STOPPED BY USER");
  1037. $parent = this.selected.parent();
  1038. var obj = this.selected;
  1039. if (this.settings.rules.multiple == false || this.selected_arr.length == 1) {
  1040. var stop = true;
  1041. var tmp = this.settings.ui.selected_delete == "select_previous" ? this.prev(this.selected) : false;
  1042. }
  1043. obj = obj.remove();
  1044. $parent.children("li:last").addClass("last");
  1045. if ($parent.children("li").size() == 0) {
  1046. $li = $parent.parents("li:eq(0)");
  1047. $li.removeClass("open").removeClass("closed").addClass("leaf").children("ul").remove();
  1048. }
  1049. if (!stop && this.settings.rules.multiple != false) {
  1050. var _this = this;
  1051. this.selected_arr = [];
  1052. this.container.find("a.clicked").filter(":first-child").parent().each(function () {
  1053. _this.selected_arr.push($(this));
  1054. });
  1055. if (this.selected_arr.length > 0) {
  1056. this.selected = this.selected_arr[0];
  1057. this.remove();
  1058. }
  1059. }
  1060. if (stop && tmp) this.select_branch(tmp);
  1061. this.callback("ondelete", [obj.get(0), this, rb]);
  1062. }
  1063. else return this.error("DELETE: NO NODE SELECTED");
  1064. },
  1065. next: function (obj, strict) {
  1066. obj = this.get_node(obj);
  1067. if (!obj.size()) return false;
  1068. if (strict) return (obj.nextAll("li").size() > 0) ? obj.nextAll("li:eq(0)") : false;
  1069. if (obj.hasClass("open")) return obj.find("li:eq(0)");
  1070. else if (obj.nextAll("li").size() > 0) return obj.nextAll("li:eq(0)");
  1071. else return obj.parents("li").next("li").eq(0);
  1072. },
  1073. prev: function (obj, strict) {
  1074. obj = this.get_node(obj);
  1075. if (!obj.size()) return false;
  1076. if (strict) return (obj.prevAll("li").size() > 0) ? obj.prevAll("li:eq(0)") : false;
  1077. if (obj.prev("li").size()) {
  1078. var obj = obj.prev("li").eq(0);
  1079. while (obj.hasClass("open")) obj = obj.children("ul:eq(0)").children("li:last");
  1080. return obj;
  1081. }
  1082. else return obj.parents("li:eq(0)").size() ? obj.parents("li:eq(0)") : false;
  1083. },
  1084. parent: function (obj) {
  1085. obj = this.get_node(obj);
  1086. if (!obj.size()) return false;
  1087. return obj.parents("li:eq(0)").size() ? obj.parents("li:eq(0)") : -1;
  1088. },
  1089. children: function (obj) {
  1090. if (obj === -1) return this.container.children("ul:eq(0)").children("li");
  1091. obj = this.get_node(obj);
  1092. if (!obj.size()) return false;
  1093. return obj.children("ul:eq(0)").children("li");
  1094. },
  1095. toggle_dots: function () {
  1096. if (this.settings.ui.dots) {
  1097. this.settings.ui.dots = false;
  1098. this.container.children("ul:eq(0)").addClass("no_dots");
  1099. }
  1100. else {
  1101. this.settings.ui.dots = true;
  1102. this.container.children("ul:eq(0)").removeClass("no_dots");
  1103. }
  1104. },
  1105. callback: function (cb, args) {
  1106. var p = false;
  1107. var r = null;
  1108. for (var i in this.settings.plugins) {
  1109. if (typeof $.tree.plugins[i] != "object") continue;
  1110. p = $.tree.plugins[i];
  1111. if (p.callbacks && typeof p.callbacks[cb] == "function") r = p.callbacks[cb].apply(this, args);
  1112. if (typeof r !== "undefined" && r !== null) {
  1113. if (cb == "ondata" || cb == "onparse") args[0] = r; // keep the chain if data or parse
  1114. else return r;
  1115. }
  1116. }
  1117. p = this.settings.callback[cb];
  1118. if (typeof p == "function") return p.apply(null, args);
  1119. },
  1120. get_rollback: function () {
  1121. var rb = {};
  1122. rb.html = this.container.html();
  1123. rb.selected = this.selected ? this.selected.attr("id") : false;
  1124. return rb;
  1125. },
  1126. moved: function (what, where, how, is_new, is_copy, rb) {
  1127. var what = $(what);
  1128. var $parent = $(what).parents("ul:eq(0)");
  1129. var $where = $(where);
  1130. if ($where.is("ins")) $where = $where.parent();
  1131. // Rollback
  1132. if (!rb) {
  1133. var rb = {};
  1134. rb[this.container.attr("id")] = this.get_rollback();
  1135. if (!is_new) {
  1136. var tmp = what.size() > 1 ? what.eq(0).parents(".tree:eq(0)") : what.parents(".tree:eq(0)");
  1137. if (tmp.get(0) != this.container.get(0)) {
  1138. tmp = tree_component.inst[tmp.attr("id")];
  1139. rb[tmp.container.attr("id")] = tmp.get_rollback();
  1140. }
  1141. delete tmp;
  1142. }
  1143. }
  1144. if (how == "inside" && this.settings.data.async) {
  1145. var _this = this;
  1146. if (this.get_node($where).hasClass("closed")) {
  1147. return this.open_branch(this.get_node($where), true, function () { _this.moved.apply(_this, [what, where, how, is_new, is_copy, rb]); });
  1148. }
  1149. if (this.get_node($where).find("> ul > li > a.loading").size() == 1) {
  1150. setTimeout(function () { _this.moved.apply(_this, [what, where, how, is_new, is_copy]); }, 200);
  1151. return;
  1152. }
  1153. }
  1154. // IF MULTIPLE
  1155. if (what.size() > 1) {
  1156. var _this = this;
  1157. var tmp = this.moved(what.eq(0), where, how, false, is_copy, rb);
  1158. what.each(function (i) {
  1159. if (i == 0) return;
  1160. if (tmp) { // if tmp is false - the previous move was a no-go
  1161. tmp = _this.moved(this, tmp.children("a:eq(0)"), "after", false, is_copy, rb);
  1162. }
  1163. });
  1164. return what;
  1165. }
  1166. if (is_copy) {
  1167. _what = what.clone();
  1168. _what.each(function (i) {
  1169. this.id = this.id + "_copy";
  1170. $(this).find("li").each(function () {
  1171. this.id = this.id + "_copy";
  1172. });
  1173. $(this).removeClass("dragged").find("a.clicked").removeClass("clicked").end().find("li.dragged").removeClass("dragged");
  1174. });
  1175. }
  1176. else _what = what;
  1177. if (is_new) {
  1178. if (!this.callback("beforecreate", [this.get_node(what).get(0), this.get_node(where).get(0), how, this])) return false;
  1179. }
  1180. else {
  1181. if (!this.callback("beforemove", [this.get_node(what).get(0), this.get_node(where).get(0), how, this])) return false;
  1182. }
  1183. if (!is_new) {
  1184. var tmp = what.parents(".tree:eq(0)");
  1185. // if different trees
  1186. if (tmp.get(0) != this.container.get(0)) {
  1187. tmp = tree_component.inst[tmp.attr("id")];
  1188. // if there are languages - otherwise - no cleanup needed
  1189. if (tmp.settings.languages.length) {
  1190. var res = [];
  1191. // if new tree has no languages - use current visible
  1192. if (this.settings.languages.length == 0) res.push("." + tmp.current_lang);
  1193. else {
  1194. for (var i in this.settings.languages) {
  1195. if (!this.settings.languages.hasOwnProperty(i)) continue;
  1196. for (var j in tmp.settings.languages) {
  1197. if (!tmp.settings.languages.hasOwnProperty(j)) continue;
  1198. if (this.settings.languages[i] == tmp.settings.languages[j]) res.push("." + this.settings.languages[i]);
  1199. }
  1200. }
  1201. }
  1202. if (res.length == 0) return this.error("MOVE: NO COMMON LANGUAGES");
  1203. _what.find("a").not(res.join(",")).remove();
  1204. }
  1205. _what.find("a.clicked").removeClass("clicked");
  1206. }
  1207. }
  1208. what = _what;
  1209. // ADD NODE TO NEW PLACE
  1210. switch (how) {
  1211. case "before":
  1212. $where.parents("ul:eq(0)").children("li.last").removeClass("last");
  1213. $where.parent().before(what.removeClass("last"));
  1214. $where.parents("ul:eq(0)").children("li:last").addClass("last");
  1215. break;
  1216. case "after":
  1217. $where.parents("ul:eq(0)").children("li.last").removeClass("last");
  1218. $where.parent().after(what.removeClass("last"));
  1219. $where.parents("ul:eq(0)").children("li:last").addClass("last");
  1220. break;
  1221. case "inside":
  1222. if ($where.parent().children("ul:first").size()) {
  1223. if (this.settings.rules.createat == "top") {
  1224. $where.parent().children("ul:first").prepend(what.removeClass("last")).children("li:last").addClass("last");
  1225. // restored this section
  1226. var tmp_node = $where.parent().children("ul:first").children("li:first");
  1227. if (tmp_node.size()) {
  1228. how = "before";
  1229. where = tmp_node;
  1230. }
  1231. }
  1232. else {
  1233. // restored this section
  1234. var tmp_node = $where.parent().children("ul:first").children(".last");
  1235. if (tmp_node.size()) {
  1236. how = "after";
  1237. where = tmp_node;
  1238. }
  1239. $where.parent().children("ul:first").children(".last").removeClass("last").end().append(what.removeClass("last")).children("li:last").addClass("last");
  1240. }
  1241. }
  1242. else {
  1243. what.addClass("last");
  1244. $where.parent().removeClass("leaf").append("<ul/>");
  1245. if (!$where.parent().hasClass("open")) $where.parent().addClass("closed");
  1246. $where.parent().children("ul:first").prepend(what);
  1247. }
  1248. if ($where.parent().hasClass("closed")) { this.open_branch($where); }
  1249. break;
  1250. default:
  1251. break;
  1252. }
  1253. // CLEANUP OLD PARENT
  1254. if ($parent.find("li").size() == 0) {
  1255. var $li = $parent.parent();
  1256. $li.removeClass("open").removeClass("closed").addClass("leaf");
  1257. if (!$li.is(".tree")) $li.children("ul").remove();
  1258. $li.parents("ul:eq(0)").children("li.last").removeClass("last").end().children("li:last").addClass("last");
  1259. }
  1260. else {
  1261. $parent.children("li.last").removeClass("last");
  1262. $parent.children("li:last").addClass("last");
  1263. }
  1264. // NO LONGER CORRECT WITH position PARAM - if(is_new && how != "inside") where = this.get_node(where).parents("li:eq(0)");
  1265. if (is_copy) this.callback("oncopy", [this.get_node(what).get(0), this.get_node(where).get(0), how, this, rb]);
  1266. else if (is_new) this.callback("oncreate", [this.get_node(what).get(0), ($where.is("ul") ? -1 : this.get_node(where).get(0)), how, this, rb]);
  1267. else this.callback("onmove", [this.get_node(what).get(0), this.get_node(where).get(0), how, this, rb]);
  1268. return what;
  1269. },
  1270. error: function (code) {
  1271. this.callback("error", [code, this]);
  1272. return false;
  1273. },
  1274. lock: function (state) {
  1275. this.locked = state;
  1276. if (this.locked) this.container.children("ul:eq(0)").addClass("locked");
  1277. else this.container.children("ul:eq(0)").removeClass("locked");
  1278. },
  1279. cut: function (obj) {
  1280. if (this.locked) return this.error("LOCKED");
  1281. obj = obj ? this.get_node(obj) : this.container.find("a.clicked").filter(":first-child").parent();
  1282. if (!obj || !obj.size()) return this.error("CUT: NO NODE SELECTED");
  1283. tree_component.cut_copy.copy_nodes = false;
  1284. tree_component.cut_copy.cut_nodes = obj;
  1285. },
  1286. copy: function (obj) {
  1287. if (this.locked) return this.error("LOCKED");
  1288. obj = obj ? this.get_node(obj) : this.container.find("a.clicked").filter(":first-child").parent();
  1289. if (!obj || !obj.size()) return this.error("COPY: NO NODE SELECTED");
  1290. tree_component.cut_copy.copy_nodes = obj;
  1291. tree_component.cut_copy.cut_nodes = false;
  1292. },
  1293. paste: function (obj, position) {
  1294. if (this.locked) return this.error("LOCKED");
  1295. var root = false;
  1296. if (obj == -1) { root = true; obj = this.container; }
  1297. else obj = obj ? this.get_node(obj) : this.selected;
  1298. if (!root && (!obj || !obj.size())) return this.error("PASTE: NO NODE SELECTED");
  1299. if (!tree_component.cut_copy.copy_nodes && !tree_component.cut_copy.cut_nodes) return this.error("PASTE: NOTHING TO DO");
  1300. var _this = this;
  1301. var pos = position;
  1302. if (position == "before") {
  1303. position = obj.parent().children().index(obj);
  1304. obj = obj.parents("li:eq(0)");
  1305. }
  1306. else if (position == "after") {
  1307. position = obj.parent().children().index(obj) + 1;
  1308. obj = obj.parents("li:eq(0)");
  1309. }
  1310. else if ((typeof position).toLowerCase() == "undefined" || position == "inside") {
  1311. position = (this.settings.rules.createat == "top") ? 0 : obj.children("ul:eq(0)").children("li").size();
  1312. }
  1313. if (!root && obj.size() == 0) { root = true; obj = this.container; }
  1314. if (tree_component.cut_copy.copy_nodes && tree_component.cut_copy.copy_nodes.size()) {
  1315. var ok = true;
  1316. if (!root && !this.check_move(tree_component.cut_copy.copy_nodes, obj.children("a:eq(0)"), "inside")) return false;
  1317. if (obj.children("ul").size() == 0 || (root == true && obj.children("ul").children("li").size() == 0)) {
  1318. if (!root) var a = this.moved(tree_component.cut_copy.copy_nodes, obj.children("a:eq(0)"), "inside", false, true);
  1319. else var a = this.moved(tree_component.cut_copy.copy_nodes, this.container.children("ul:eq(0)"), "inside", false, true);
  1320. }
  1321. else if (pos == "before" && obj.children("ul:eq(0)").children("li:nth-child(" + (position + 1) + ")").size())
  1322. var a = this.moved(tree_component.cut_copy.copy_nodes, obj.children("ul:eq(0)").children("li:nth-child(" + (position + 1) + ")").children("a:eq(0)"), "before", false, true);
  1323. else if (pos == "after" && obj.children("ul:eq(0)").children("li:nth-child(" + (position) + ")").size())
  1324. var a = this.moved(tree_component.cut_copy.copy_nodes, obj.children("ul:eq(0)").children("li:nth-child(" + (position) + ")").children("a:eq(0)"), "after", false, true);
  1325. else if (obj.children("ul:eq(0)").children("li:nth-child(" + (position + 1) + ")").size())
  1326. var a = this.moved(tree_component.cut_copy.copy_nodes, obj.children("ul:eq(0)").children("li:nth-child(" + (position + 1) + ")").children("a:eq(0)"), "before", false, true);
  1327. else
  1328. var a = this.moved(tree_component.cut_copy.copy_nodes, obj.children("ul:eq(0)").children("li:last").children("a:eq(0)"), "after", false, true);
  1329. tree_component.cut_copy.copy_nodes = false;
  1330. }
  1331. if (tree_component.cut_copy.cut_nodes && tree_component.cut_copy.cut_nodes.size()) {
  1332. var ok = true;
  1333. obj.parents().andSelf().each(function () {
  1334. if (tree_component.cut_copy.cut_nodes.index(this) != -1) {
  1335. ok = false;
  1336. return false;
  1337. }
  1338. });
  1339. if (!ok) return this.error("Invalid paste");
  1340. if (!root && !this.check_move(tree_component.cut_copy.cut_nodes, obj.children("a:eq(0)"), "inside")) return false;
  1341. if (obj.children("ul").size() == 0 || (root == true && obj.children("ul").children("li").size() == 0)) {
  1342. if (!root) var a = this.moved(tree_component.cut_copy.cut_nodes, obj.children("a:eq(0)"), "inside");
  1343. else var a = this.moved(tree_component.cut_copy.cut_nodes, this.container.children("ul:eq(0)"), "inside");
  1344. }
  1345. else if (pos == "before" && obj.children("ul:eq(0)").children("li:nth-child(" + (position + 1) + ")").size())
  1346. var a = this.moved(tree_component.cut_copy.cut_nodes, obj.children("ul:eq(0)").children("li:nth-child(" + (position + 1) + ")").children("a:eq(0)"), "before");
  1347. else if (pos == "after" && obj.children("ul:eq(0)").children("li:nth-child(" + (position) + ")").size())
  1348. var a = this.moved(tree_component.cut_copy.cut_nodes, obj.children("ul:eq(0)").children("li:nth-child(" + (position) + ")").children("a:eq(0)"), "after");
  1349. else if (obj.children("ul:eq(0)").children("li:nth-child(" + (position + 1) + ")").size())
  1350. var a = this.moved(tree_component.cut_copy.cut_nodes, obj.children("ul:eq(0)").children("li:nth-child(" + (position + 1) + ")").children("a:eq(0)"), "before");
  1351. else
  1352. var a = this.moved(tree_component.cut_copy.cut_nodes, obj.children("ul:eq(0)").children("li:last").children("a:eq(0)"), "after");
  1353. tree_component.cut_copy.cut_nodes = false;
  1354. }
  1355. },
  1356. search: function (str, func) {
  1357. var _this = this;
  1358. if (!str || (this.srch && str != this.srch)) {
  1359. this.srch = "";
  1360. this.srch_opn = false;
  1361. this.container.find("a.search").removeClass("search");
  1362. }
  1363. this.srch = str;
  1364. if (!str) return;
  1365. if (!func) func = "contains";
  1366. if (this.settings.data.async) {
  1367. if (!this.srch_opn) {
  1368. var dd = $.extend({ "search": str }, this.callback("beforedata", [false, this]));
  1369. $.ajax({
  1370. type: this.settings.data.opts.method,
  1371. url: this.settings.data.opts.url,
  1372. data: dd,
  1373. dataType: "text",
  1374. success: function (data) {
  1375. _this.srch_opn = $.unique(data.split(","));
  1376. _this.search.apply(_this, [str, func]);
  1377. }
  1378. });
  1379. }
  1380. else if (this.srch_opn.length) {
  1381. if (this.srch_opn && this.srch_opn.length) {
  1382. var opn = false;
  1383. for (var j = 0; j < this.srch_opn.length; j++) {
  1384. if (this.get_node("#" + this.srch_opn[j]).size() > 0) {
  1385. opn = true;
  1386. var tmp = "#" + this.srch_opn[j];
  1387. delete this.srch_opn[j];
  1388. this.open_branch(tmp, true, function () { _this.search.apply(_this, [str, func]); });
  1389. }
  1390. }
  1391. if (!opn) {
  1392. this.srch_opn = [];
  1393. _this.search.apply(_this, [str, func]);
  1394. }
  1395. }
  1396. }
  1397. else {
  1398. this.srch_opn = false;
  1399. var selector = "a";
  1400. // IF LANGUAGE VERSIONS
  1401. if (this.settings.languages.length) selector += "." + this.current_lang;
  1402. this.callback("onsearch", [this.container.find(selector + ":" + func + "('" + str + "')"), this]);
  1403. }
  1404. }
  1405. else {
  1406. var selector = "a";
  1407. // IF LANGUAGE VERSIONS
  1408. if (this.settings.languages.length) selector += "." + this.current_lang;
  1409. var nn = this.container.find(selector + ":" + func + "('" + str + "')");
  1410. nn.parents("li.closed").each(function () { _this.open_branch(this, true); });
  1411. this.callback("onsearch", [nn, this]);
  1412. }
  1413. },
  1414. add_sheet: tree_component.add_sheet,
  1415. destroy: function () {
  1416. this.callback("ondestroy", [this]);
  1417. this.container.unbind(".jstree");
  1418. $("#" + this.container.attr("id")).off("click.jstree").off("dblclick.jstree").off("mouseover.jstree").off("mouseout.jstree").off("mousedown.jstree");
  1419. this.container.removeClass("tree ui-widget ui-widget-content tree-default tree-" + this.settings.ui.theme_name).children("ul").removeClass("no_dots ltr locked").find("li").removeClass("leaf").removeClass("open").removeClass("closed").removeClass("last").children("a").removeClass("clicked hover search");
  1420. if (this.cntr == tree_component.focused) {
  1421. for (var i in tree_component.inst) {
  1422. if (i != this.cntr && i != this.container.attr("id")) {
  1423. tree_component.inst[i].focus();
  1424. break;
  1425. }
  1426. }
  1427. }
  1428. tree_component.inst[this.cntr] = false;
  1429. tree_component.inst[this.container.attr("id")] = false;
  1430. delete tree_component.inst[this.cntr];
  1431. delete tree_component.inst[this.container.attr("id")];
  1432. tree_component.cntr--;
  1433. }
  1434. }
  1435. };
  1436. // instance manager
  1437. tree_component.cntr = 0;
  1438. tree_component.inst = {};
  1439. // themes
  1440. tree_component.themes = [];
  1441. // drag'n'drop stuff
  1442. tree_component.drag_drop = {
  1443. isdown: false, // Is there a drag
  1444. drag_node: false, // The actual node
  1445. drag_help: false, // The helper
  1446. dragged: false,
  1447. init_x: false,
  1448. init_y: false,
  1449. moving: false,
  1450. origin_tree: false,
  1451. marker: false,
  1452. move_type: false, // before, after or inside
  1453. ref_node: false, // reference node
  1454. appended: false, // is helper appended
  1455. foreign: false, // Is the dragged node a foreign one
  1456. droppable: [], // Array of classes that can be dropped onto the tree
  1457. open_time: false, // Timeout for opening nodes
  1458. scroll_time: false // Timeout for scrolling
  1459. };
  1460. tree_component.mouseup = function (event) {
  1461. var tmp = tree_component.drag_drop;
  1462. if (tmp.open_time) clearTimeout(tmp.open_time);
  1463. if (tmp.scroll_time) clearTimeout(tmp.scroll_time);
  1464. if (tmp.moving && $.tree.drag_end !== false) $.tree.drag_end.call(null, event, tmp);
  1465. if (tmp.foreign === false && tmp.drag_node && tmp.drag_node.size()) {
  1466. tmp.drag_help.remove();
  1467. if (tmp.move_type) {
  1468. var tree1 = tree_component.inst[tmp.ref_node.parents(".tree:eq(0)").attr("id")];
  1469. if (tree1) tree1.moved(tmp.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)));
  1470. }
  1471. tmp.move_type = false;
  1472. tmp.ref_node = false;
  1473. }
  1474. if (tmp.foreign !== false) {
  1475. if (tmp.drag_help) tmp.drag_help.remove();
  1476. if (tmp.move_type) {
  1477. var tree1 = tree_component.inst[tmp.ref_node.parents(".tree:eq(0)").attr("id")];
  1478. if (tree1) tree1.callback("ondrop", [tmp.f_data, tree1.get_node(tmp.ref_node).get(0), tmp.move_type, tree1]);
  1479. }
  1480. tmp.foreign = false;
  1481. tmp.move_type = false;
  1482. tmp.ref_node = false;
  1483. }
  1484. // RESET EVERYTHING
  1485. if (tree_component.drag_drop.marker) tree_component.drag_drop.marker.hide();
  1486. if (tmp.dragged && tmp.dragged.size()) tmp.dragged.removeClass("dragged");
  1487. tmp.dragged = false;
  1488. tmp.drag_help = false;
  1489. tmp.drag_node = false;
  1490. tmp.f_type = false;
  1491. tmp.f_data = false;
  1492. tmp.init_x = false;
  1493. tmp.init_y = false;
  1494. tmp.moving = false;
  1495. tmp.appended = false;
  1496. tmp.origin_tree = false;
  1497. if (tmp.isdown) {
  1498. tmp.isdown = false;
  1499. event.preventDefault();
  1500. event.stopPropagation();
  1501. return false;
  1502. }
  1503. };
  1504. tree_component.mousemove = function (event) {
  1505. var tmp = tree_component.drag_drop;
  1506. var is_start = false;
  1507. if (tmp.isdown) {
  1508. if (!tmp.moving && Math.abs(tmp.init_x - event.pageX) < 5 && Math.abs(tmp.init_y - event.pageY) < 5) {
  1509. event.preventDefault();
  1510. event.stopPropagation();
  1511. return false;
  1512. }
  1513. else {
  1514. if (!tmp.moving) {
  1515. tree_component.drag_drop.moving = true;
  1516. is_start = true;
  1517. }
  1518. }
  1519. if (tmp.open_time) clearTimeout(tmp.open_time);
  1520. if (tmp.drag_help !== false) {
  1521. if (!tmp.appended) {
  1522. if (tmp.foreign !== false) tmp.origin_tree = $.tree.focused();
  1523. $("body").append(tmp.drag_help);
  1524. tmp.w = tmp.drag_help.width();
  1525. tmp.appended = true;
  1526. }
  1527. tmp.drag_help.css({ "left": (event.pageX + 5), "top": (event.pageY + 15) });
  1528. }
  1529. if (is_start && $.tree.drag_start !== false) $.tree.drag_start.call(null, event, tmp);
  1530. if ($.tree.drag !== false) $.tree.drag.call(null, event, tmp);
  1531. if (event.target.tagName == "DIV" && event.target.id == "jstree-marker") return false;
  1532. var et = $(event.target);
  1533. if (et.is("ins")) et = et.parent();
  1534. var cnt = et.is(".tree") ? et : et.parents(".tree:eq(0)");
  1535. // if not moving over a tree
  1536. if (cnt.size() == 0 || !tree_component.inst[cnt.attr("id")]) {
  1537. if (tmp.scroll_time) clearTimeout(tmp.scroll_time);
  1538. if (tmp.drag_help !== false) tmp.drag_help.find("li:eq(0) ins").addClass("forbidden");
  1539. tmp.move_type = false;
  1540. tmp.ref_node = false;
  1541. tree_component.drag_drop.marker.hide();
  1542. return false;
  1543. }
  1544. var tree2 = tree_component.inst[cnt.attr("id")];
  1545. tree2.off_height();
  1546. if (tmp.scroll_time) clearTimeout(tmp.scroll_time);
  1547. tmp.scroll_time = setTimeout(function () { tree2.scroll_check(event.pageX, event.pageY); }, 50);
  1548. var mov = false;
  1549. var st = cnt.scrollTop();
  1550. if (event.target.tagName == "A" || event.target.tagName == "INS") {
  1551. // just in case if hover is over the draggable
  1552. if (et.is("#jstree-dragged")) return false;
  1553. if (tree2.get_node(event.target).hasClass("closed")) {
  1554. tmp.open_time = setTimeout(function () { tree2.open_branch(et); }, 500);
  1555. }
  1556. var et_off = et.offset();
  1557. var goTo = {
  1558. x: (et_off.left - 1),
  1559. y: (event.pageY - et_off.top)
  1560. };
  1561. var arr = [];
  1562. if (goTo.y < tree2.li_height / 3 + 1) arr = ["before", "inside", "after"];
  1563. else if (goTo.y > tree2.li_height * 2 / 3 - 1) arr = ["after", "inside", "before"];
  1564. else {
  1565. if (goTo.y < tree2.li_height / 2) arr = ["inside", "before", "after"];
  1566. else arr = ["inside", "after", "before"];
  1567. }
  1568. var ok = false;
  1569. var nn = (tmp.foreign == false) ? tmp.origin_tree.container.find("li.dragged") : tmp.f_type;
  1570. $.each(arr, function (i, val) {
  1571. if (tree2.check_move(nn, et, val)) {
  1572. mov = val;
  1573. ok = true;
  1574. return false;
  1575. }
  1576. });
  1577. if (ok) {
  1578. switch (mov) {
  1579. case "before":
  1580. goTo.y = et_off.top - 2;
  1581. tree_component.drag_drop.marker.attr("class", "marker");
  1582. break;
  1583. case "after":
  1584. goTo.y = et_off.top - 2 + tree2.li_height;
  1585. tree_component.drag_drop.marker.attr("class", "marker");
  1586. break;
  1587. case "inside":
  1588. goTo.x -= 2;
  1589. goTo.y = et_off.top - 2 + tree2.li_height / 2;
  1590. tree_component.drag_drop.marker.attr("class", "marker_plus");
  1591. break;
  1592. }
  1593. tmp.move_type = mov;
  1594. tmp.ref_node = $(event.target);
  1595. if (tmp.drag_help !== false) tmp.drag_help.find(".forbidden").removeClass("forbidden");
  1596. tree_component.drag_drop.marker.css({ "left": goTo.x, "top": goTo.y }).show();
  1597. }
  1598. }
  1599. if ((et.is(".tree") || et.is("ul")) && et.find("li:eq(0)").size() == 0) {
  1600. var et_off = et.offset();
  1601. tmp.move_type = "inside";
  1602. tmp.ref_node = cnt.children("ul:eq(0)");
  1603. if (tmp.drag_help !== false) tmp.drag_help.find(".forbidden").removeClass("forbidden");
  1604. tree_component.drag_drop.marker.attr("class", "marker_plus");
  1605. tree_component.drag_drop.marker.css({ "left": (et_off.left + 10), "top": et_off.top + 15 }).show();
  1606. }
  1607. else if ((event.target.tagName != "A" && event.target.tagName != "INS") || !ok) {
  1608. if (tmp.drag_help !== false) tmp.drag_help.find("li:eq(0) ins").addClass("forbidden");
  1609. tmp.move_type = false;
  1610. tmp.ref_node = false;
  1611. tree_component.drag_drop.marker.hide();
  1612. }
  1613. event.preventDefault();
  1614. event.stopPropagation();
  1615. return false;
  1616. }
  1617. return true;
  1618. };
  1619. $(function () {
  1620. $(document).bind("mousemove.jstree", tree_component.mousemove);
  1621. $(document).bind("mouseup.jstree", tree_component.mouseup);
  1622. });
  1623. // cut, copy, paste stuff
  1624. tree_component.cut_copy = {
  1625. copy_nodes: false,
  1626. cut_nodes: false
  1627. };
  1628. // css stuff
  1629. tree_component.css = false;
  1630. tree_component.get_css = function (rule_name, delete_flag) {
  1631. rule_name = rule_name.toLowerCase();
  1632. var css_rules = tree_component.css.cssRules || tree_component.css.rules;
  1633. var j = 0;
  1634. do {
  1635. if (css_rules.length && j > css_rules.length + 5) return false;
  1636. if (css_rules[j].selectorText && css_rules[j].selectorText.toLowerCase() == rule_name) {
  1637. if (delete_flag == true) {
  1638. if (tree_component.css.removeRule) document.styleSheets[i].removeRule(j);
  1639. if (tree_component.css.deleteRule) document.styleSheets[i].deleteRule(j);
  1640. return true;
  1641. }
  1642. else return css_rules[j];
  1643. }
  1644. }
  1645. while (css_rules[++j]);
  1646. return false;
  1647. };
  1648. tree_component.add_css = function (rule_name) {
  1649. if (tree_component.get_css(rule_name)) return false;
  1650. (tree_component.css.insertRule) ? tree_component.css.insertRule(rule_name + ' { }', 0) : tree_component.css.addRule(rule_name, null, 0);
  1651. return tree_component.get_css(rule_name);
  1652. };
  1653. tree_component.remove_css = function (rule_name) {
  1654. return tree_component.get_css(rule_name, true);
  1655. };
  1656. tree_component.add_sheet = function (opts) {
  1657. if (opts.str) {
  1658. var tmp = document.createElement("style");
  1659. tmp.setAttribute('type', "text/css");
  1660. if (tmp.styleSheet) {
  1661. document.getElementsByTagName("head")[0].appendChild(tmp);
  1662. tmp.styleSheet.cssText = opts.str;
  1663. }
  1664. else {
  1665. tmp.appendChild(document.createTextNode(opts.str));
  1666. document.getElementsByTagName("head")[0].appendChild(tmp);
  1667. }
  1668. return tmp.sheet || tmp.styleSheet;
  1669. }
  1670. if (opts.url) {
  1671. if (document.createStyleSheet) {
  1672. try { document.createStyleSheet(opts.url); } catch (e) { };
  1673. }
  1674. else {
  1675. var newSS = document.createElement('link');
  1676. newSS.rel = 'stylesheet';
  1677. newSS.type = 'text/css';
  1678. newSS.media = "all";
  1679. newSS.href = opts.url;
  1680. // var styles = "@import url(' " + url + " ');";
  1681. // newSS.href ='data:text/css,'+escape(styles);
  1682. document.getElementsByTagName("head")[0].appendChild(newSS);
  1683. return newSS.styleSheet;
  1684. }
  1685. }
  1686. };
  1687. $(function () {
  1688. var u = navigator.userAgent.toLowerCase();
  1689. var v = (u.match(/.+(?:rv|it|ra|ie)[\/: ]([\d.]+)/) || [0, '0'])[1];
  1690. var css = '/* TREE LAYOUT */ .tree ul { margin:0 0 0 5px; padding:0 0 0 0; list-style-type:none; } .tree li { display:block; min-height:18px; line-height:18px; padding:0 0 0 15px; margin:0 0 0 0; /* Background fix */ clear:both; } .tree li ul { display:none; } .tree li a, .tree li span { display:inline-block;line-height:16px;height:16px;color:black;white-space:nowrap;text-decoration:none;padding:1px 4px 1px 4px;margin:0; } .tree li a:focus { outline: none; } .tree li a input, .tree li span input { margin:0;padding:0 0;display:inline-block;height:12px !important;border:1px solid white;background:white;font-size:10px;font-family:Verdana; } .tree li a input:not([class="xxx"]), .tree li span input:not([class="xxx"]) { padding:1px 0; } /* FOR DOTS */ .tree .ltr li.last { float:left; } .tree > ul li.last { overflow:visible; } /* OPEN OR CLOSE */ .tree li.open ul { display:block; } .tree li.closed ul { display:none !important; } /* FOR DRAGGING */ #jstree-dragged { position:absolute; top:-10px; left:-10px; margin:0; padding:0; } #jstree-dragged ul ul ul { display:none; } #jstree-marker { padding:0; margin:0; line-height:5px; font-size:1px; overflow:hidden; height:5px; position:absolute; left:-45px; top:-30px; z-index:1000; background-color:transparent; background-repeat:no-repeat; display:none; } #jstree-marker.marker { width:45px; background-position:-32px top; } #jstree-marker.marker_plus { width:5px; background-position:right top; } /* BACKGROUND DOTS */ .tree li li { overflow:hidden; } .tree > .ltr > li { display:table; } /* ICONS */ .tree ul ins { display:inline-block; text-decoration:none; width:16px; height:16px; } .tree .ltr ins { margin:0 4px 0 0px; } ';
  1691. var agent = navigator.userAgent.toLowerCase();
  1692. if (/msie 6.0/.test(agent)) css += '.tree li { height:18px; zoom:1; } .tree li li { overflow:visible; } .tree .ltr li.last { margin-top: expression( (this.previousSibling && /open/.test(this.previousSibling.className) ) ? "-2px" : "0"); } .marker { width:45px; background-position:-32px top; } .marker_plus { width:5px; background-position:right top; }';
  1693. if (/msie 7.0/.test(agent)) css += '.tree li li { overflow:visible; } .tree .ltr li.last { margin-top: expression( (this.previousSibling && /open/.test(this.previousSibling.className) ) ? "-2px" : "0"); }';
  1694. if (/opera/.test(agent)) css += '.tree > ul > li.last:after { content:"."; display: block; height:1px; clear:both; visibility:hidden; }';
  1695. if (/mozilla/.test(agent)) css += '.tree .ltr li a { display:inline; float:left; } .tree li ul { clear:both; }';
  1696. tree_component.css = tree_component.add_sheet({ str: css });
  1697. });
  1698. })(jQuery);
  1699. // Datastores
  1700. // HTML and JSON are included here by default
  1701. (function ($) {
  1702. $.extend($.tree.datastores, {
  1703. "html" : function () {
  1704. return {
  1705. get : function(obj, tree, opts) {
  1706. return obj && $(obj).size() ? $('<div>').append(tree.get_node(obj).clone()).html() : tree.container.children("ul:eq(0)").html();
  1707. },
  1708. parse : function(data, tree, opts, callback) {
  1709. if(callback) callback.call(null, data);
  1710. return data;
  1711. },
  1712. load : function(data, tree, opts, callback) {
  1713. if(opts.url) {
  1714. $.ajax({
  1715. 'type' : opts.method,
  1716. 'url' : opts.url,
  1717. 'data' : data,
  1718. 'dataType' : "html",
  1719. 'success' : function (d, textStatus) {
  1720. callback.call(null, d);
  1721. },
  1722. 'error' : function (xhttp, textStatus, errorThrown) {
  1723. callback.call(null, false);
  1724. tree.error(errorThrown + " " + textStatus);
  1725. }
  1726. });
  1727. }
  1728. else {
  1729. callback.call(null, opts.static || tree.container.children("ul:eq(0)").html());
  1730. }
  1731. }
  1732. };
  1733. },
  1734. "json" : function () {
  1735. return {
  1736. get : function(obj, tree, opts) {
  1737. var _this = this;
  1738. if(!obj || $(obj).size() == 0) obj = tree.container.children("ul").children("li");
  1739. else obj = $(obj);
  1740. if(!opts) opts = {};
  1741. if(!opts.outer_attrib) opts.outer_attrib = [ "id", "rel", "class" ];
  1742. if(!opts.inner_attrib) opts.inner_attrib = [ ];
  1743. if(obj.size() > 1) {
  1744. var arr = [];
  1745. obj.each(function () {
  1746. arr.push(_this.get(this, tree, opts));
  1747. });
  1748. return arr;
  1749. }
  1750. if(obj.size() == 0) return [];
  1751. var json = { attributes : {}, data : {} };
  1752. if(obj.hasClass("open")) json.data.state = "open";
  1753. if(obj.hasClass("closed")) json.data.state = "closed";
  1754. for(var i in opts.outer_attrib) {
  1755. if(!opts.outer_attrib.hasOwnProperty(i)) continue;
  1756. var val = (opts.outer_attrib[i] == "class") ? obj.attr(opts.outer_attrib[i]).replace(/(^| )last( |$)/ig," ").replace(/(^| )(leaf|closed|open)( |$)/ig," ") : obj.attr(opts.outer_attrib[i]);
  1757. if(typeof val != "undefined" && val.toString().replace(" ","").length > 0) json.attributes[opts.outer_attrib[i]] = val;
  1758. delete val;
  1759. }
  1760. if(tree.settings.languages.length) {
  1761. for(var i in tree.settings.languages) {
  1762. if(!tree.settings.languages.hasOwnProperty(i)) continue;
  1763. var a = obj.children("a." + tree.settings.languages[i]);
  1764. if(opts.force || opts.inner_attrib.length || a.children("ins").get(0).style.backgroundImage.toString().length || a.children("ins").get(0).className.length) {
  1765. json.data[tree.settings.languages[i]] = {};
  1766. json.data[tree.settings.languages[i]].title = tree.get_text(obj,tree.settings.languages[i]);
  1767. if(a.children("ins").get(0).style.className.length) {
  1768. json.data[tree.settings.languages[i]].icon = a.children("ins").get(0).style.className;
  1769. }
  1770. if(a.children("ins").get(0).style.backgroundImage.length) {
  1771. json.data[tree.settings.languages[i]].icon = a.children("ins").get(0).style.backgroundImage.replace("url(","").replace(")","");
  1772. }
  1773. if(opts.inner_attrib.length) {
  1774. json.data[tree.settings.languages[i]].attributes = {};
  1775. for(var j in opts.inner_attrib) {
  1776. if(!opts.inner_attrib.hasOwnProperty(j)) continue;
  1777. var val = a.attr(opts.inner_attrib[j]);
  1778. if(typeof val != "undefined" && val.toString().replace(" ","").length > 0) json.data[tree.settings.languages[i]].attributes[opts.inner_attrib[j]] = val;
  1779. delete val;
  1780. }
  1781. }
  1782. }
  1783. else {
  1784. json.data[tree.settings.languages[i]] = tree.get_text(obj,tree.settings.languages[i]);
  1785. }
  1786. }
  1787. }
  1788. else {
  1789. var a = obj.children("a");
  1790. json.data.title = tree.get_text(obj);
  1791. if(a.children("ins").size() && a.children("ins").get(0).className.length) {
  1792. json.data.icon = a.children("ins").get(0).className;
  1793. }
  1794. if(a.children("ins").size() && a.children("ins").get(0).style.backgroundImage.length) {
  1795. json.data.icon = a.children("ins").get(0).style.backgroundImage.replace("url(","").replace(")","");
  1796. }
  1797. if(opts.inner_attrib.length) {
  1798. json.data.attributes = {};
  1799. for(var j in opts.inner_attrib) {
  1800. if(!opts.inner_attrib.hasOwnProperty(j)) continue;
  1801. var val = a.attr(opts.inner_attrib[j]);
  1802. if(typeof val != "undefined" && val.toString().replace(" ","").length > 0) json.data.attributes[opts.inner_attrib[j]] = val;
  1803. delete val;
  1804. }
  1805. }
  1806. }
  1807. if(obj.children("ul").size() > 0) {
  1808. json.children = [];
  1809. obj.children("ul").children("li").each(function () {
  1810. json.children.push(_this.get(this, tree, opts));
  1811. });
  1812. }
  1813. return json;
  1814. },
  1815. parse : function(data, tree, opts, callback) {
  1816. if(Object.prototype.toString.apply(data) === "[object Array]") {
  1817. var str = '';
  1818. for(var i = 0; i < data.length; i ++) {
  1819. if(typeof data[i] == "function") continue;
  1820. str += this.parse(data[i], tree, opts);
  1821. }
  1822. if(callback) callback.call(null, str);
  1823. return str;
  1824. }
  1825. if(!data || !data.data) {
  1826. if(callback) callback.call(null, false);
  1827. return "";
  1828. }
  1829. var str = '';
  1830. str += "<li ";
  1831. var cls = false;
  1832. if(data.attributes) {
  1833. for(var i in data.attributes) {
  1834. if(!data.attributes.hasOwnProperty(i)) continue;
  1835. if(i == "class") {
  1836. str += " class='" + data.attributes[i] + " ";
  1837. if(data.state == "closed" || data.state == "open") str += " " + data.state + " ";
  1838. str += "' ";
  1839. cls = true;
  1840. }
  1841. else if(i=="id")
  1842. str += " " + i + "='"+tree.settings.idprev + data.attributes[i] + "' ";
  1843. else str += " " + i + "='" + data.attributes[i] + "' ";
  1844. }
  1845. }
  1846. if(!cls && (data.state == "closed" || data.state == "open")) str += " class='" + data.state + "' ";
  1847. str += ">";
  1848. if(tree.settings.languages.length) {
  1849. for(var i = 0; i < tree.settings.languages.length; i++) {
  1850. var attr = {};
  1851. attr["href"] = "";
  1852. attr["style"] = "";
  1853. attr["class"] = tree.settings.languages[i];
  1854. if(data.data[tree.settings.languages[i]] && (typeof data.data[tree.settings.languages[i]].attributes).toLowerCase() != "undefined") {
  1855. for(var j in data.data[tree.settings.languages[i]].attributes) {
  1856. if(!data.data[tree.settings.languages[i]].attributes.hasOwnProperty(j)) continue;
  1857. if(j == "style" || j == "class") attr[j] += " " + data.data[tree.settings.languages[i]].attributes[j];
  1858. else attr[j] = data.data[tree.settings.languages[i]].attributes[j];
  1859. }
  1860. }
  1861. str += "<a";
  1862. for(var j in attr) {
  1863. if(!attr.hasOwnProperty(j)) continue;
  1864. str += ' ' + j + '="' + attr[j] + '" ';
  1865. }
  1866. str += ">";
  1867. if(data.data[tree.settings.languages[i]] && data.data[tree.settings.languages[i]].icon) {
  1868. str += "<ins " + (data.data[tree.settings.languages[i]].icon.indexOf("/") == -1 ? " class='" + data.data[tree.settings.languages[i]].icon + "' " : " style='background-image:url(\"" + data.data[tree.settings.languages[i]].icon + "\");' " ) + ">&nbsp;</ins>";
  1869. }
  1870. else str += "<ins>&nbsp;</ins>";
  1871. str += ( (typeof data.data[tree.settings.languages[i]].title).toLowerCase() != "undefined" ? data.data[tree.settings.languages[i]].title : data.data[tree.settings.languages[i]] ) + "</a>";
  1872. }
  1873. }
  1874. else {
  1875. var attr = {};
  1876. attr["href"] = "";
  1877. attr["style"] = "";
  1878. attr["class"] = "";
  1879. if((typeof data.data.attributes).toLowerCase() != "undefined") {
  1880. for(var i in data.data.attributes) {
  1881. if(!data.data.attributes.hasOwnProperty(i)) continue;
  1882. if(i == "style" || i == "class") attr[i] += " " + data.data.attributes[i];
  1883. else attr[i] = data.data.attributes[i];
  1884. }
  1885. }
  1886. str += "<a";
  1887. for(var i in attr) {
  1888. if(!attr.hasOwnProperty(i)) continue;
  1889. str += ' ' + i + '="' + attr[i] + '" ';
  1890. }
  1891. str += ">";
  1892. if(data.data.icon) {
  1893. str += "<ins " + (data.data.icon.indexOf("/") == -1 ? " class='" + data.data.icon + "' " : " style='background-image:url(\"" + data.data.icon + "\");' " ) + ">&nbsp;</ins>";
  1894. }
  1895. else str += "<ins>&nbsp;</ins>";
  1896. str += ( (typeof data.data.title).toLowerCase() != "undefined" ? data.data.title : data.data ) + "</a>";
  1897. }
  1898. if(data.children && data.children.length) {
  1899. str += '<ul>';
  1900. for(var i = 0; i < data.children.length; i++) {
  1901. str += this.parse(data.children[i], tree, opts);
  1902. }
  1903. str += '</ul>';
  1904. }
  1905. if(callback) callback.call(null, str);
  1906. return str;
  1907. },
  1908. load : function(data, tree, opts, callback) {
  1909. if(opts.static) {
  1910. callback.call(null, opts.static);
  1911. }
  1912. else {
  1913. $.ajax({
  1914. 'type' : opts.method,
  1915. 'url' : opts.url,
  1916. 'data' : data,
  1917. 'dataType' : "json",
  1918. 'success' : function (d, textStatus) {
  1919. callback.call(null, d);
  1920. },
  1921. 'error' : function (xhttp, textStatus, errorThrown) {
  1922. callback.call(null, false);
  1923. tree.error(errorThrown + " " + textStatus);
  1924. }
  1925. });
  1926. }
  1927. }
  1928. }
  1929. }
  1930. });
  1931. })(jQuery);