tree_component.js 99 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315
  1. /*
  2. * jsTree 0.9.8
  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-05-15
  12. *
  13. */
  14. (function($) {
  15. // jQuery plugin
  16. $.fn.tree = function (opts) {
  17. return this.each(function() {
  18. var conf = $.extend({},opts);
  19. if(tree_component.inst && tree_component.inst[$(this).attr('id')]) tree_component.inst[$(this).attr('id')].destroy();
  20. if(conf !== false) new tree_component().init(this, conf);
  21. });
  22. };
  23. $.tree_create = function() {
  24. return new tree_component();
  25. };
  26. $.tree_focused = function() {
  27. return tree_component.inst[tree_component.focused];
  28. };
  29. $.tree_reference = function(id) {
  30. return tree_component.inst[id] || null;
  31. };
  32. // rollback
  33. $.tree_rollback = function(data) {
  34. for(var i in data) {
  35. if(typeof data[i] == "function") continue;
  36. var tmp = tree_component.inst[i];
  37. var lock = !tmp.locked;
  38. // if not locked - lock the tree
  39. if(lock) tmp.lock(true);
  40. // Cancel ongoing rename
  41. if(tmp.inp) tmp.inp.val("").blur();
  42. tmp.context.append = false;
  43. tmp.container.html(data[i].html).find(".dragged").removeClass("dragged").end().find("div.context").remove();
  44. if(data[i].selected) {
  45. tmp.selected = $("#" + data[i].selected);
  46. tmp.selected_arr = [];
  47. tmp.container
  48. .find("a.clicked").each( function () {
  49. tmp.selected_arr.push(tmp.get_node(this));
  50. });
  51. }
  52. // if this function set the lock - unlock
  53. if(lock) tmp.lock(false);
  54. delete lock;
  55. delete tmp;
  56. }
  57. };
  58. // core
  59. function tree_component () {
  60. // instance manager
  61. if(typeof tree_component.inst == "undefined") {
  62. tree_component.cntr = 0;
  63. tree_component.inst = {};
  64. // DRAG'N'DROP STUFF
  65. tree_component.drag_drop = {
  66. isdown : false, // Is there a drag
  67. drag_node : false, // The actual node
  68. drag_help : false, // The helper
  69. init_x : false,
  70. init_y : false,
  71. moving : false,
  72. origin_tree : false,
  73. marker : false,
  74. move_type : false, // before, after or inside
  75. ref_node : false, // reference node
  76. appended : false, // is helper appended
  77. foreign : false, // Is the dragged node a foreign one
  78. droppable : [], // Array of classes that can be dropped onto the tree
  79. open_time : false, // Timeout for opening nodes
  80. scroll_time : false // Timeout for scrolling
  81. };
  82. // listening for clicks on foreign nodes
  83. tree_component.mousedown = function(event) {
  84. var tmp = $(event.target);
  85. if(tree_component.drag_drop.droppable.length && tmp.is("." + tree_component.drag_drop.droppable.join(", .")) ) {
  86. tree_component.drag_drop.drag_help = $("<div id='jstree-dragged' class='tree tree-default'><ul><li class='last dragged foreign " + event.target.className + "'><a href='#'>" + tmp.text() + "</a></li></ul></div>");
  87. tree_component.drag_drop.drag_node = tree_component.drag_drop.drag_help.find("li:eq(0)");
  88. tree_component.drag_drop.isdown = true;
  89. tree_component.drag_drop.foreign = tmp;
  90. tmp.blur();
  91. event.preventDefault();
  92. event.stopPropagation();
  93. return false;
  94. }
  95. event.stopPropagation();
  96. return true;
  97. };
  98. tree_component.mouseup = function(event) {
  99. var tmp = tree_component.drag_drop;
  100. if(tmp.open_time) clearTimeout(tmp.open_time);
  101. if(tmp.scroll_time) clearTimeout(tmp.scroll_time);
  102. if(tmp.foreign === false && tmp.drag_node && tmp.drag_node.size()) {
  103. tmp.drag_help.remove();
  104. if(tmp.move_type) {
  105. var tree1 = tree_component.inst[tmp.ref_node.parents(".tree:eq(0)").attr("id")];
  106. if(tree1) tree1.moved(tmp.origin_tree.container.find("li.dragged"), tmp.ref_node, tmp.move_type, false, (tmp.origin_tree.settings.rules.drag_copy == "on" || (tmp.origin_tree.settings.rules.drag_copy == "ctrl" && event.ctrlKey) ) );
  107. }
  108. tmp.move_type = false;
  109. tmp.ref_node = false;
  110. }
  111. if(tmp.drag_node && tmp.foreign !== false) {
  112. tmp.drag_help.remove();
  113. if(tmp.move_type) {
  114. var tree1 = tree_component.inst[tmp.ref_node.parents(".tree:eq(0)").attr("id")];
  115. if(tree1) tree1.settings.callback.ondrop.call(null, tmp.foreign.get(0), tree1.get_node(tmp.ref_node).get(0), tmp.move_type, tree1);
  116. }
  117. tmp.foreign = false;
  118. tmp.move_type = false;
  119. tmp.ref_node = false;
  120. }
  121. // RESET EVERYTHING
  122. tree_component.drag_drop.marker.hide();
  123. tmp.drag_help = false;
  124. tmp.drag_node = false;
  125. tmp.isdown = false;
  126. tmp.init_x = false;
  127. tmp.init_y = false;
  128. tmp.moving = false;
  129. tmp.appended = false;
  130. $("li.dragged").removeClass("dragged");
  131. tmp.origin_tree = false;
  132. event.preventDefault();
  133. event.stopPropagation();
  134. return false;
  135. };
  136. tree_component.mousemove = function(event) {
  137. var tmp = tree_component.drag_drop;
  138. if(tmp.isdown) {
  139. if(!tmp.moving && Math.abs(tmp.init_x - event.pageX) < 5 && Math.abs(tmp.init_y - event.pageY) < 5) {
  140. event.preventDefault();
  141. event.stopPropagation();
  142. return false;
  143. }
  144. else tree_component.drag_drop.moving = true;
  145. if(tmp.open_time) clearTimeout(tmp.open_time);
  146. if(!tmp.appended) {
  147. if(tmp.foreign !== false) tmp.origin_tree = $.tree_focused();
  148. $("body").append(tmp.drag_help);
  149. tmp.w = tmp.drag_help.width();
  150. tmp.appended = true;
  151. }
  152. tmp.drag_help.css({ "left" : (event.pageX - (tmp.origin_tree.settings.ui.rtl ? tmp.w : -5 ) ), "top" : (event.pageY + 15) });
  153. if(event.target.tagName == "IMG" && event.target.id == "marker") return false;
  154. var et = $(event.target);
  155. var cnt = et.is(".tree") ? et : et.parents(".tree:eq(0)");
  156. // if not moving over a tree
  157. if(cnt.size() == 0 || !tree_component.inst[cnt.attr("id")]) {
  158. if(tmp.scroll_time) clearTimeout(tmp.scroll_time);
  159. if(tmp.drag_help.find("IMG").size() == 0) {
  160. tmp.drag_help.find("li:eq(0)").append("<img style='position:absolute; " + (tmp.origin_tree.settings.ui.rtl ? "right" : "left" ) + ":4px; top:0px; background:white; padding:2px;' src='" + tmp.origin_tree.settings.ui.theme_path + "remove.png' />");
  161. }
  162. tmp.move_type = false;
  163. tmp.ref_node = false;
  164. tree_component.drag_drop.marker.hide();
  165. return false;
  166. }
  167. var tree2 = tree_component.inst[cnt.attr("id")];
  168. tree2.off_height();
  169. // if moving over another tree and multitree is false
  170. if( tmp.foreign === false && tmp.origin_tree.container.get(0) != tree2.container.get(0) && (!tmp.origin_tree.settings.rules.multitree || !tree2.settings.rules.multitree) ) {
  171. if(tmp.drag_help.find("IMG").size() == 0) {
  172. tmp.drag_help.find("li:eq(0)").append("<img style='position:absolute; " + (tmp.origin_tree.settings.ui.rtl ? "right" : "left" ) + ":4px; top:0px; background:white; padding:2px;' src='" + tmp.origin_tree.settings.ui.theme_path + "remove.png' />");
  173. }
  174. tmp.move_type = false;
  175. tmp.ref_node = false;
  176. tree_component.drag_drop.marker.hide();
  177. return false;
  178. }
  179. if(tmp.scroll_time) clearTimeout(tmp.scroll_time);
  180. tmp.scroll_time = setTimeout( function() { tree2.scrollCheck(event.pageX,event.pageY); }, 50);
  181. var mov = false;
  182. var st = cnt.scrollTop();
  183. if(event.target.tagName == "A" ) {
  184. // just in case if hover is over the draggable
  185. if(et.is("#jstree-dragged")) return false;
  186. if(tree2.get_node(event.target).hasClass("closed")) {
  187. tmp.open_time = setTimeout( function () { tree2.open_branch(et); }, 500);
  188. }
  189. var et_off = et.offset();
  190. var goTo = {
  191. x : (et_off.left - 1),
  192. y : (event.pageY - et_off.top)
  193. };
  194. if(cnt.children("ul:eq(0)").hasClass("rtl")) goTo.x += et.width() - 8;
  195. var arr = [];
  196. if(goTo.y < tree2.li_height/3 + 1 ) arr = ["before","inside","after"];
  197. else if(goTo.y > tree2.li_height*2/3 - 1 ) arr = ["after","inside","before"];
  198. else {
  199. if(goTo.y < tree2.li_height/2) arr = ["inside","before","after"];
  200. else arr = ["inside","after","before"];
  201. }
  202. var ok = false;
  203. $.each(arr, function(i, val) {
  204. if(tree2.checkMove(tmp.origin_tree.container.find("li.dragged"), et, val)) {
  205. mov = val;
  206. ok = true;
  207. return false;
  208. }
  209. });
  210. if(ok) {
  211. switch(mov) {
  212. case "before":
  213. goTo.y = et_off.top - 2;
  214. if(cnt.children("ul:eq(0)").hasClass("rtl")) { tree_component.drag_drop.marker.attr("src", tree2.settings.ui.theme_path + "marker_rtl.gif").width(40); }
  215. else { tree_component.drag_drop.marker.attr("src", tree2.settings.ui.theme_path + "marker.gif").width(40); }
  216. break;
  217. case "after":
  218. goTo.y = et_off.top - 2 + tree2.li_height;
  219. if(cnt.children("ul:eq(0)").hasClass("rtl")) { tree_component.drag_drop.marker.attr("src", tree2.settings.ui.theme_path + "marker_rtl.gif").width(40); }
  220. else { tree_component.drag_drop.marker.attr("src", tree2.settings.ui.theme_path + "marker.gif").width(40); }
  221. break;
  222. case "inside":
  223. goTo.x -= 2;
  224. if(cnt.children("ul:eq(0)").hasClass("rtl")) {
  225. goTo.x += 36;
  226. }
  227. goTo.y = et_off.top - 2 + tree2.li_height/2;
  228. tree_component.drag_drop.marker.attr("src", tree2.settings.ui.theme_path + "plus.gif").width(11);
  229. break;
  230. }
  231. tmp.move_type = mov;
  232. tmp.ref_node = $(event.target);
  233. tmp.drag_help.find("IMG").remove();
  234. tree_component.drag_drop.marker.css({ "left" : goTo.x , "top" : goTo.y }).show();
  235. }
  236. }
  237. if( (et.is(".tree") || et.is("ul") ) && et.find("li:eq(0)").size() == 0) {
  238. var et_off = et.offset();
  239. tmp.move_type = "inside";
  240. tmp.ref_node = cnt.children("ul:eq(0)");
  241. tmp.drag_help.find("IMG").remove();
  242. tree_component.drag_drop.marker.attr("src", tree2.settings.ui.theme_path + "plus.gif").width(11);
  243. tree_component.drag_drop.marker.css({ "left" : et_off.left + ( cnt.children("ul:eq(0)").hasClass("rtl") ? (cnt.width() - 10) : 10 ) , "top" : et_off.top + 15 }).show();
  244. }
  245. else if(event.target.tagName != "A" || !ok) {
  246. if(tmp.drag_help.find("IMG").size() == 0) {
  247. tmp.drag_help.find("li:eq(0)").append("<img style='position:absolute; " + (tmp.origin_tree.settings.ui.rtl ? "right" : "left" ) + ":4px; top:0px; background:white; padding:2px;' src='" + tmp.origin_tree.settings.ui.theme_path + "remove.png' />");
  248. }
  249. tmp.move_type = false;
  250. tmp.ref_node = false;
  251. tree_component.drag_drop.marker.hide();
  252. }
  253. event.preventDefault();
  254. event.stopPropagation();
  255. return false;
  256. }
  257. return true;
  258. };
  259. };
  260. return {
  261. cntr : ++tree_component.cntr,
  262. settings : {
  263. data : {
  264. type : "predefined", // ENUM [json, xml_flat, xml_nested, predefined]
  265. method : "GET", // HOW TO REQUEST FILES
  266. async : false, // BOOL - async loading onopen
  267. async_data : function (NODE, TREE_OBJ) { return { id : $(NODE).attr("id") || 0 } }, // PARAMETERS PASSED TO SERVER
  268. url : false, // FALSE or STRING - url to document to be used (async or not)
  269. json : false, // FALSE or OBJECT if type is JSON and async is false - the tree dump as json
  270. xml : false // FALSE or STRING if type is XML_FLAT or XML_NESTED and async is false - a string to generate the tree from
  271. },
  272. selected : false, // FALSE or STRING or ARRAY
  273. opened : [], // ARRAY OF INITIALLY OPENED NODES
  274. languages : [], // ARRAY of string values (which will be used as CSS classes - so they must be valid)
  275. path : false, // FALSE or STRING (if false - will be autodetected)
  276. cookies : false, // FALSE or OBJECT (prefix, open, selected, opts - from jqCookie - expires, path, domain, secure)
  277. ui : {
  278. dots : true, // BOOL - dots or no dots
  279. rtl : false, // BOOL - is the tree right-to-left
  280. animation : 0, // INT - duration of open/close animations in miliseconds
  281. hover_mode : true, // SHOULD get_* functions chage focus or change hovered item
  282. scroll_spd : 4,
  283. theme_path : false, // Path to themes
  284. theme_name : "default",// Name of theme
  285. context : [
  286. {
  287. id : "create",
  288. label : "Create",
  289. icon : "create.png",
  290. visible : function (NODE, TREE_OBJ) { if(NODE.length != 1) return false; return TREE_OBJ.check("creatable", NODE); },
  291. action : function (NODE, TREE_OBJ) { TREE_OBJ.create(false, TREE_OBJ.get_node(NODE[0])); }
  292. },
  293. "separator",
  294. {
  295. id : "rename",
  296. label : "Rename",
  297. icon : "rename.png",
  298. visible : function (NODE, TREE_OBJ) { if(NODE.length != 1) return false; return TREE_OBJ.check("renameable", NODE); },
  299. action : function (NODE, TREE_OBJ) { TREE_OBJ.rename(NODE); }
  300. },
  301. {
  302. id : "delete",
  303. label : "Delete",
  304. icon : "remove.png",
  305. visible : function (NODE, TREE_OBJ) { var ok = true; $.each(NODE, function () { if(TREE_OBJ.check("deletable", this) == false) ok = false; return false; }); return ok; },
  306. action : function (NODE, TREE_OBJ) { $.each(NODE, function () { TREE_OBJ.remove(this); }); }
  307. }
  308. ]
  309. },
  310. rules : {
  311. multiple : false, // FALSE | CTRL | ON - multiple selection off/ with or without holding Ctrl
  312. metadata : false, // FALSE or STRING - attribute name (use metadata plugin)
  313. type_attr : "rel", // STRING attribute name (where is the type stored if no metadata)
  314. multitree : false, // BOOL - is drag n drop between trees allowed
  315. createat : "bottom", // STRING (top or bottom) new nodes get inserted at top or bottom
  316. use_inline : false, // CHECK FOR INLINE RULES - REQUIRES METADATA
  317. clickable : "all", // which node types can the user select | default - all
  318. renameable : "all", // which node types can the user select | default - all
  319. deletable : "all", // which node types can the user delete | default - all
  320. creatable : "all", // which node types can the user create in | default - all
  321. draggable : "none", // which node types can the user move | default - none | "all"
  322. dragrules : "all", // what move operations between nodes are allowed | default - none | "all"
  323. drag_copy : false, // FALSE | CTRL | ON - drag to copy off/ with or without holding Ctrl
  324. droppable : [],
  325. drag_button : "left" // left, right or both
  326. },
  327. lang : {
  328. new_node : "New folder",
  329. loading : "Loading ..."
  330. },
  331. callback : { // various callbacks to attach custom logic to
  332. // before focus - should return true | false
  333. beforechange: function(NODE,TREE_OBJ) { return true },
  334. beforeopen : function(NODE,TREE_OBJ) { return true },
  335. beforeclose : function(NODE,TREE_OBJ) { return true },
  336. // before move - should return true | false
  337. beforemove : function(NODE,REF_NODE,TYPE,TREE_OBJ) { return true },
  338. // before create - should return true | false
  339. beforecreate: function(NODE,REF_NODE,TYPE,TREE_OBJ) { return true },
  340. // before rename - should return true | false
  341. beforerename: function(NODE,LANG,TREE_OBJ) { return true },
  342. // before delete - should return true | false
  343. beforedelete: function(NODE,TREE_OBJ) { return true },
  344. onJSONdata : function(DATA,TREE_OBJ) { return DATA; },
  345. onselect : function(NODE,TREE_OBJ) { }, // node selected
  346. ondeselect : function(NODE,TREE_OBJ) { }, // node deselected
  347. onchange : function(NODE,TREE_OBJ) { }, // focus changed
  348. onrename : function(NODE,LANG,TREE_OBJ,RB) { }, // node renamed ISNEW - TRUE|FALSE, current language
  349. onmove : function(NODE,REF_NODE,TYPE,TREE_OBJ,RB) { }, // move completed (TYPE is BELOW|ABOVE|INSIDE)
  350. oncopy : function(NODE,REF_NODE,TYPE,TREE_OBJ,RB) { }, // copy completed (TYPE is BELOW|ABOVE|INSIDE)
  351. oncreate : function(NODE,REF_NODE,TYPE,TREE_OBJ,RB) { }, // node created, parent node (TYPE is createat)
  352. ondelete : function(NODE,TREE_OBJ,RB) { }, // node deleted
  353. onopen : function(NODE,TREE_OBJ) { }, // node opened
  354. onopen_all : function(TREE_OBJ) { }, // all nodes opened
  355. onclose : function(NODE,TREE_OBJ) { }, // node closed
  356. error : function(TEXT,TREE_OBJ) { }, // error occured
  357. // double click on node - defaults to open/close & select
  358. ondblclk : function(NODE,TREE_OBJ) { TREE_OBJ.toggle_branch.call(TREE_OBJ, NODE); TREE_OBJ.select_branch.call(TREE_OBJ, NODE); },
  359. // right click - to prevent use: EV.preventDefault(); EV.stopPropagation(); return false
  360. onrgtclk : function(NODE,TREE_OBJ,EV) { },
  361. onload : function(TREE_OBJ) { },
  362. onfocus : function(TREE_OBJ) { },
  363. ondrop : function(NODE,REF_NODE,TYPE,TREE_OBJ) {}
  364. }
  365. },
  366. // INITIALIZATION
  367. init : function(elem, conf) {
  368. var _this = this;
  369. this.container = $(elem);
  370. if(this.container.size == 0) { alert("Invalid container node!"); return }
  371. tree_component.inst[this.cntr] = this;
  372. if(!this.container.attr("id")) this.container.attr("id","jstree_" + this.cntr);
  373. tree_component.inst[this.container.attr("id")] = tree_component.inst[this.cntr];
  374. tree_component.focused = this.cntr;
  375. var opts = $.extend({},conf);
  376. // MERGE OPTIONS WITH DEFAULTS
  377. if(opts && opts.cookies) {
  378. this.settings.cookies = $.extend({},this.settings.cookies,opts.cookies);
  379. delete opts.cookies;
  380. if(!this.settings.cookies.opts) this.settings.cookies.opts = {};
  381. }
  382. if(opts && opts.callback) {
  383. this.settings.callback = $.extend({},this.settings.callback,opts.callback);
  384. delete opts.callback;
  385. }
  386. if(opts && opts.data) {
  387. this.settings.data = $.extend({},this.settings.data,opts.data);
  388. delete opts.data;
  389. }
  390. if(opts && opts.ui) {
  391. this.settings.ui = $.extend({},this.settings.ui,opts.ui);
  392. delete opts.ui;
  393. }
  394. if(opts && opts.rules) {
  395. this.settings.rules = $.extend({},this.settings.rules,opts.rules);
  396. delete opts.rules;
  397. }
  398. if(opts && opts.lang) {
  399. this.settings.lang = $.extend({},this.settings.lang,opts.lang);
  400. delete opts.lang;
  401. }
  402. this.settings = $.extend({},this.settings,opts);
  403. // PATH TO IMAGES AND XSL
  404. if(this.settings.path == false) {
  405. this.path = "";
  406. $("script").each( function () {
  407. if(this.src.toString().match(/tree_component.*?js$/)) {
  408. _this.path = this.src.toString().replace(/tree_component.*?js$/, "");
  409. }
  410. });
  411. }
  412. else this.path = this.settings.path;
  413. // DEAL WITH LANGUAGE VERSIONS
  414. this.current_lang = this.settings.languages && this.settings.languages.length ? this.settings.languages[0] : false;
  415. if(this.settings.languages && this.settings.languages.length) {
  416. this.sn = get_sheet_num("tree_component.css");
  417. if(this.sn === false && document.styleSheets.length) this.sn = document.styleSheets.length;
  418. var st = false;
  419. var id = this.container.attr("id") ? "#" + this.container.attr("id") : ".tree";
  420. for(var ln = 0; ln < this.settings.languages.length; ln++) {
  421. st = add_css(id + " ." + this.settings.languages[ln], this.sn);
  422. if(st !== false) {
  423. if(this.settings.languages[ln] == this.current_lang) st.style.display = "";
  424. else st.style.display = "none";
  425. }
  426. }
  427. }
  428. // DROPPABLES
  429. if(this.settings.rules.droppable.length) {
  430. for(var i in this.settings.rules.droppable) {
  431. if(typeof this.settings.rules.droppable[i] == "function") continue;
  432. tree_component.drag_drop.droppable.push(this.settings.rules.droppable[i]);
  433. }
  434. tree_component.drag_drop.droppable = $.unique(tree_component.drag_drop.droppable);
  435. }
  436. // THEMES
  437. if(this.settings.ui.theme_path == false) this.settings.ui.theme_path = this.path + "themes/";
  438. this.theme = this.settings.ui.theme_path;
  439. if(_this.settings.ui.theme_name) {
  440. this.theme += _this.settings.ui.theme_name + "/";
  441. if(_this.settings.ui.theme_name != "themeroller" && !tree_component.def_style) { add_sheet(_this.settings.ui.theme_path + "default/style.css"); tree_component.def_style = true; }
  442. add_sheet(_this.theme + "style.css");
  443. }
  444. this.container.addClass("tree");
  445. if(_this.settings.ui.theme_name != "themeroller") this.container.addClass("tree-default");
  446. if(this.settings.ui.theme_name && this.settings.ui.theme_name != "default") this.container.addClass("tree-" + _this.settings.ui.theme_name);
  447. if(this.settings.ui.theme_name == "themeroller") this.container.addClass("ui-widget ui-widget-content");
  448. if(this.settings.rules.multiple) this.selected_arr = [];
  449. this.offset = false;
  450. // CONTEXT MENU
  451. this.context_menu();
  452. this.hovered = false;
  453. this.locked = false;
  454. // CREATE DUMMY FOR MOVING
  455. if(this.settings.rules.draggable != "none" && tree_component.drag_drop.marker === false) {
  456. var _this = this;
  457. tree_component.drag_drop.marker = $("<img>")
  458. .attr({
  459. id : "marker",
  460. src : _this.settings.ui.theme_path + "marker.gif"
  461. })
  462. .css({
  463. height : "5px",
  464. width : "40px",
  465. display : "block",
  466. position : "absolute",
  467. left : "30px",
  468. top : "30px",
  469. zIndex : "1000"
  470. }).hide().appendTo("body");
  471. }
  472. this.refresh();
  473. this.attachEvents();
  474. this.focus();
  475. },
  476. off_height : function () {
  477. if(this.offset === false) {
  478. this.container.css({ position : "relative" });
  479. this.offset = this.container.offset();
  480. var tmp = 0;
  481. tmp = parseInt($.curCSS(this.container.get(0), "paddingTop", true),10);
  482. if(tmp) this.offset.top += tmp;
  483. tmp = parseInt($.curCSS(this.container.get(0), "borderTopWidth", true),10);
  484. if(tmp) this.offset.top += tmp;
  485. this.container.css({ position : "" });
  486. }
  487. if(!this.li_height) {
  488. var tmp = this.container.find("ul li.closed, ul li.leaf").eq(0);
  489. this.li_height = tmp.height();
  490. if(tmp.children("ul:eq(0)").size()) this.li_height -= tmp.children("ul:eq(0)").height();
  491. if(!this.li_height) this.li_height = 18;
  492. }
  493. },
  494. context_menu : function () {
  495. this.context = false;
  496. if(this.settings.ui.context != false) {
  497. var str = '<div class="tree-context tree-default-context tree-' + this.settings.ui.theme_name + '-context">';
  498. for(var i in this.settings.ui.context) {
  499. if(typeof this.settings.ui.context[i] == "function") continue;
  500. if(this.settings.ui.context[i] == "separator") {
  501. str += "<span class='separator'>&nbsp;</span>";
  502. continue;
  503. }
  504. var icn = "";
  505. if(this.settings.ui.context[i].icon) icn = 'background-image:url(\'' + ( this.settings.ui.context[i].icon.indexOf("/") == -1 ? this.theme + this.settings.ui.context[i].icon : this.settings.ui.context[i].icon ) + '\');';
  506. str += '<a rel="' + this.settings.ui.context[i].id + '" href="#" style="' + icn + '">' + this.settings.ui.context[i].label + '</a>';
  507. }
  508. str += '</div>';
  509. this.context = $(str);
  510. this.context.hide();
  511. this.context.append = false;
  512. }
  513. },
  514. // REPAINT TREE
  515. refresh : function (obj) {
  516. if(this.locked) return this.error("LOCKED");
  517. var _this = this;
  518. this.is_partial_refresh = obj ? true : false;
  519. // SAVE OPENED
  520. this.opened = Array();
  521. if(this.settings.cookies && $.cookie(this.settings.cookies.prefix + '_open')) {
  522. var str = $.cookie(this.settings.cookies.prefix + '_open');
  523. var tmp = str.split(",");
  524. $.each(tmp, function () {
  525. if(this.replace(/^#/,"").length > 0) { _this.opened.push("#" + this.replace(/^#/,"")); }
  526. });
  527. this.settings.opened = false;
  528. }
  529. else if(this.settings.opened != false) {
  530. $.each(this.settings.opened, function (i, item) {
  531. if(this.replace(/^#/,"").length > 0) { _this.opened.push("#" + this.replace(/^#/,"")); }
  532. });
  533. this.settings.opened = false;
  534. }
  535. else {
  536. this.container.find("li.open").each(function (i) { if(this.id) { _this.opened.push("#" + this.id); } });
  537. }
  538. // SAVE SELECTED
  539. if(this.selected) {
  540. this.settings.selected = Array();
  541. if(obj) {
  542. $(obj).find("li:has(a.clicked)").each(function () {
  543. $this = $(this);
  544. if($this.attr("id")) _this.settings.selected.push("#" + $this.attr("id"));
  545. });
  546. }
  547. else {
  548. if(this.selected_arr) {
  549. $.each(this.selected_arr, function () {
  550. if(this.attr("id")) _this.settings.selected.push("#" + this.attr("id"));
  551. });
  552. }
  553. else {
  554. if(this.selected.attr("id")) this.settings.selected.push("#" + this.selected.attr("id"));
  555. }
  556. }
  557. }
  558. else if(this.settings.cookies && $.cookie(this.settings.cookies.prefix + '_selected')) {
  559. this.settings.selected = Array();
  560. var str = $.cookie(this.settings.cookies.prefix + '_selected');
  561. var tmp = str.split(",");
  562. $.each(tmp, function () {
  563. if(this.replace(/^#/,"").length > 0) { _this.settings.selected.push("#" + this.replace(/^#/,"")); }
  564. });
  565. }
  566. else if(this.settings.selected !== false) {
  567. var tmp = Array();
  568. if((typeof this.settings.selected).toLowerCase() == "object") {
  569. $.each(this.settings.selected, function () {
  570. if(this.replace(/^#/,"").length > 0) tmp.push("#" + this.replace(/^#/,""));
  571. });
  572. }
  573. else {
  574. if(this.settings.selected.replace(/^#/,"").length > 0) tmp.push("#" + this.settings.selected.replace(/^#/,""));
  575. }
  576. this.settings.selected = tmp;
  577. }
  578. if(obj && this.settings.data.async) {
  579. this.opened = Array();
  580. obj = this.get_node(obj);
  581. obj.find("li.open").each(function (i) { _this.opened.push("#" + this.id); });
  582. if(obj.hasClass("open")) obj.removeClass("open").addClass("closed");
  583. if(obj.hasClass("leaf")) obj.removeClass("leaf");
  584. obj.children("ul:eq(0)").html("");
  585. return this.open_branch(obj, true, function () { _this.reselect.apply(_this); });
  586. }
  587. if(this.settings.data.type == "xml_flat" || this.settings.data.type == "xml_nested") {
  588. this.scrtop = this.container.get(0).scrollTop;
  589. var xsl = (this.settings.data.type == "xml_flat") ? "flat.xsl" : "nested.xsl";
  590. if(this.settings.data.xml) this.container.getTransform(this.path + xsl, this.settings.data.xml, { params : { theme_name : _this.settings.ui.theme_name, theme_path : _this.theme }, meth : _this.settings.data.method, dat : _this.settings.data.async_data.apply(_this,[obj, _this]), callback: function () { _this.context_menu.apply(_this); _this.reselect.apply(_this); } });
  591. else this.container.getTransform(this.path + xsl, this.settings.data.url, { params : { theme_name : _this.settings.ui.theme_name, theme_path : _this.theme }, meth : _this.settings.data.method, dat : _this.settings.data.async_data.apply(_this,[obj, _this]), callback: function () { _this.context_menu.apply(_this); _this.reselect.apply(_this); } });
  592. return;
  593. }
  594. else if(this.settings.data.type == "json") {
  595. if(this.settings.data.json) {
  596. var str = "";
  597. if(this.settings.data.json.length) {
  598. for(var i = 0; i < this.settings.data.json.length; i++) {
  599. str += this.parseJSON(this.settings.data.json[i]);
  600. }
  601. } else str = this.parseJSON(this.settings.data.json);
  602. this.container.html("<ul>" + str + "</ul>");
  603. this.container.find("li:last-child").addClass("last").end().find("li:has(ul)").not(".open").addClass("closed");
  604. this.container.find("li").not(".open").not(".closed").addClass("leaf");
  605. this.context_menu();
  606. this.reselect();
  607. }
  608. else {
  609. var _this = this;
  610. $.ajax({
  611. type : this.settings.data.method,
  612. url : this.settings.data.url,
  613. data : this.settings.data.async_data(false, this),
  614. dataType : "json",
  615. success : function (data) {
  616. data = _this.settings.callback.onJSONdata.call(null, data, _this);
  617. var str = "";
  618. if(data.length) {
  619. for(var i = 0; i < data.length; i++) {
  620. str += _this.parseJSON(data[i]);
  621. }
  622. } else str = _this.parseJSON(data);
  623. _this.container.html("<ul>" + str + "</ul>");
  624. _this.container.find("li:last-child").addClass("last").end().find("li:has(ul)").not(".open").addClass("closed");
  625. _this.container.find("li").not(".open").not(".closed").addClass("leaf");
  626. _this.context_menu.apply(_this);
  627. _this.reselect.apply(_this);
  628. },
  629. error : function (xhttp, textStatus, errorThrown) { _this.error(errorThrown + " " + textStatus); }
  630. });
  631. }
  632. }
  633. else {
  634. this.container.children("ul:eq(0)");
  635. this.container.find("li:last-child").addClass("last").end().find("li:has(ul)").not(".open").addClass("closed");
  636. this.container.find("li").not(".open").not(".closed").addClass("leaf");
  637. this.reselect();
  638. }
  639. },
  640. // CONVERT JSON TO HTML
  641. parseJSON : function (data) {
  642. if(!data || !data.data) return "";
  643. var str = "";
  644. str += "<li ";
  645. var cls = false;
  646. if(data.attributes) {
  647. for(var i in data.attributes) {
  648. if(typeof data.attributes[i] == "function") continue;
  649. if(i == "class") {
  650. str += " class='" + data.attributes[i] + " ";
  651. if(data.state == "closed" || data.state == "open") str += " " + data.state + " ";
  652. str += "' ";
  653. cls = true;
  654. }
  655. else str += " " + i + "='" + data.attributes[i] + "' ";
  656. }
  657. }
  658. if(!cls && (data.state == "closed" || data.state == "open")) str += " class='" + data.state + "' ";
  659. str += ">";
  660. if(this.settings.languages.length) {
  661. for(var i = 0; i < this.settings.languages.length; i++) {
  662. var attr = {};
  663. attr["href"] = "#";
  664. attr["style"] = "";
  665. attr["class"] = this.settings.languages[i];
  666. if(data.data[this.settings.languages[i]] && (typeof data.data[this.settings.languages[i]].attributes).toLowerCase() != "undefined") {
  667. for(var j in data.data[this.settings.languages[i]].attributes) {
  668. if(typeof data.data[this.settings.languages[i]].attributes[j] == "function") continue;
  669. if(j == "style" || j == "class") attr[j] += " " + data.data[this.settings.languages[i]].attributes[j];
  670. else attr[j] = data.data[this.settings.languages[i]].attributes[j];
  671. }
  672. }
  673. if(data.data[this.settings.languages[i]] && data.data[this.settings.languages[i]].icon && this.settings.theme_name != "themeroller") {
  674. var icn = data.data[this.settings.languages[i]].icon.indexOf("/") == -1 ? this.theme + data.data[this.settings.languages[i]].icon : data.data[this.settings.languages[i]].icon;
  675. attr["style"] += " ; background-image:url('" + icn + "'); ";
  676. }
  677. str += "<a";
  678. for(var j in attr) {
  679. if(typeof attr[j] == "function") continue;
  680. str += ' ' + j + '="' + attr[j] + '" ';
  681. }
  682. str += ">";
  683. if(data.data[this.settings.languages[i]] && data.data[this.settings.languages[i]].icon && this.settings.theme_name == "themeroller") {
  684. str += "<ins class='ui-icon " + data.data[this.settings.languages[i]].icon + "'>&nbsp;</ins>";
  685. }
  686. str += ( (typeof data.data[this.settings.languages[i]].title).toLowerCase() != "undefined" ? data.data[this.settings.languages[i]].title : data.data[this.settings.languages[i]] ) + "</a>";
  687. }
  688. }
  689. else {
  690. var attr = {};
  691. attr["href"] = "#";
  692. attr["style"] = "";
  693. attr["class"] = "";
  694. if((typeof data.data.attributes).toLowerCase() != "undefined") {
  695. for(var i in data.data.attributes) {
  696. if(typeof data.data.attributes[i] == "function") continue;
  697. if(i == "style" || i == "class") attr[i] += " " + data.data.attributes[i];
  698. else attr[i] = data.data.attributes[i];
  699. }
  700. }
  701. if(data.data.icon && this.settings.ui.theme_name != "themeroller") {
  702. var icn = data.data.icon.indexOf("/") == -1 ? this.theme + data.data.icon : data.data.icon;
  703. attr["style"] += " ; background-image:url('" + icn + "');";
  704. }
  705. str += "<a";
  706. for(var i in attr) {
  707. if(typeof attr[j] == "function") continue;
  708. str += ' ' + i + '="' + attr[i] + '" ';
  709. }
  710. str += ">";
  711. if(data.data.icon && this.settings.ui.theme_name == "themeroller") {
  712. str += "<ins class='ui-icon " + data.data.icon + "'>&nbsp;</ins>";
  713. }
  714. str += ( (typeof data.data.title).toLowerCase() != "undefined" ? data.data.title : data.data ) + "</a>";
  715. }
  716. if(data.children && data.children.length) {
  717. str += '<ul>';
  718. for(var i = 0; i < data.children.length; i++) {
  719. str += this.parseJSON(data.children[i]);
  720. }
  721. str += '</ul>';
  722. }
  723. str += "</li>";
  724. return str;
  725. },
  726. // getJSON from HTML
  727. getJSON : function (nod, outer_attrib, inner_attrib, force) {
  728. var _this = this;
  729. if(!nod || $(nod).size() == 0) {
  730. nod = this.container.children("ul").children("li");
  731. }
  732. else nod = $(nod);
  733. if(nod.size() > 1) {
  734. var arr = [];
  735. nod.each(function () {
  736. arr.push(_this.getJSON(this, outer_attrib, inner_attrib, force));
  737. });
  738. return arr;
  739. }
  740. if(!outer_attrib) outer_attrib = [ "id", "rel", "class" ];
  741. if(!inner_attrib) inner_attrib = [ ];
  742. var obj = { attributes : {}, data : false };
  743. for(var i in outer_attrib) {
  744. if(typeof outer_attrib[i] == "function") continue;
  745. var val = (outer_attrib[i] == "class") ? nod.attr(outer_attrib[i]).replace("last","").replace("leaf","").replace("closed","").replace("open","") : nod.attr(outer_attrib[i]);
  746. if(typeof val != "undefined" && val.replace(" ","").length > 0) obj.attributes[outer_attrib[i]] = val;
  747. delete val;
  748. }
  749. if(this.settings.languages.length) {
  750. obj.data = {};
  751. for(var i in this.settings.languages) {
  752. if(typeof this.settings.languages[i] == "function") continue;
  753. var a = nod.children("a." + this.settings.languages[i]);
  754. if(force || inner_attrib.length || a.get(0).style.backgroundImage.toString().length) {
  755. obj.data[this.settings.languages[i]] = {};
  756. obj.data[this.settings.languages[i]].title = a.text();
  757. if(a.get(0).style.backgroundImage.length) {
  758. obj.data[this.settings.languages[i]].icon = a.get(0).style.backgroundImage.replace("url(","").replace(")","");
  759. }
  760. if(this.settings.ui.theme_name == "themeroller" && a.children("ins").size()) {
  761. var tmp = a.children("ins").attr("class");
  762. var cls = false;
  763. $.each(tmp.split(" "), function (i, val) {
  764. if(val.indexOf("ui-icon-") == 0) {
  765. cls = val;
  766. return false;
  767. }
  768. });
  769. if(cls) obj.data[this.settings.languages[i]].icon = cls;
  770. }
  771. if(inner_attrib.length) {
  772. obj.data[this.settings.languages[i]].attributes = {};
  773. for(var j in inner_attrib) {
  774. if(typeof inner_attrib[j] == "function") continue;
  775. var val = a.attr(inner_attrib[j]);
  776. if(typeof val != "undefined" && val.replace(" ","").length > 0) obj.data[this.settings.languages[i]].attributes[inner_attrib[j]] = val;
  777. delete val;
  778. }
  779. }
  780. }
  781. else {
  782. obj.data[this.settings.languages[i]] = a.text();
  783. }
  784. }
  785. }
  786. else {
  787. var a = nod.children("a");
  788. if(force || inner_attrib.length || a.get(0).style.backgroundImage.toString().length) {
  789. obj.data = {};
  790. obj.data.title = a.text();
  791. if(a.get(0).style.backgroundImage.length) {
  792. obj.data.icon = a.get(0).style.backgroundImage.replace("url(","").replace(")","");
  793. }
  794. if(this.settings.ui.theme_name == "themeroller" && a.children("ins").size()) {
  795. var tmp = a.children("ins").attr("class");
  796. var cls = false;
  797. $.each(tmp.split(" "), function (i, val) {
  798. if(val.indexOf("ui-icon-") == 0) {
  799. cls = val;
  800. return false;
  801. }
  802. });
  803. if(cls) obj.data[this.settings.languages[i]].icon = cls;
  804. }
  805. if(inner_attrib.length) {
  806. obj.data.attributes = {};
  807. for(var j in inner_attrib) {
  808. if(typeof inner_attrib[j] == "function") continue;
  809. var val = a.attr(inner_attrib[j]);
  810. if(typeof val != "undefined" && val.replace(" ","").length > 0) obj.data.attributes[inner_attrib[j]] = val;
  811. delete val;
  812. }
  813. }
  814. }
  815. else {
  816. obj.data = a.text();
  817. }
  818. }
  819. if(nod.children("ul").size() > 0) {
  820. obj.children = [];
  821. nod.children("ul").children("li").each(function () {
  822. obj.children.push(_this.getJSON(this, outer_attrib, inner_attrib, force));
  823. });
  824. }
  825. return obj;
  826. },
  827. // getXML from HTML
  828. getXML : function (tp, nod, outer_attrib, inner_attrib, cb) {
  829. var _this = this;
  830. if(tp != "flat") tp = "nested";
  831. if(!nod || $(nod).size() == 0) {
  832. nod = this.container.children("ul").children("li");
  833. }
  834. else nod = $(nod);
  835. if(nod.size() > 1) {
  836. var obj = '<root>';
  837. nod.each(function () {
  838. obj += _this.getXML(tp, this, outer_attrib, inner_attrib, true);
  839. });
  840. obj += '</root>';
  841. return obj;
  842. }
  843. if(!outer_attrib) outer_attrib = [ "id", "rel", "class" ];
  844. if(!inner_attrib) inner_attrib = [ ];
  845. var obj = '';
  846. if(!cb) obj = '<root>';
  847. obj += '<item ';
  848. if(tp == "flat") {
  849. var tmp_id = nod.parents("li:eq(0)").size() ? nod.parents("li:eq(0)").attr("id") : 0;
  850. obj += ' parent_id="' + tmp_id + '" ';
  851. delete tmp_id;
  852. }
  853. for(var i in outer_attrib) {
  854. if(typeof outer_attrib[i] == "function") continue;
  855. var val = (outer_attrib[i] == "class") ? nod.attr(outer_attrib[i]).replace("last","").replace("leaf","").replace("closed","").replace("open","") : nod.attr(outer_attrib[i]);
  856. if(typeof val != "undefined" && val.replace(" ","").length > 0) obj += ' ' + outer_attrib[i] + '="' + val + '" ';
  857. delete val;
  858. }
  859. obj += '>';
  860. obj += '<content>';
  861. if(this.settings.languages.length) {
  862. for(var i in this.settings.languages) {
  863. if(typeof this.settings.languages[i] == "function") continue;
  864. var a = nod.children("a." + this.settings.languages[i]);
  865. obj += '<name ';
  866. if(inner_attrib.length || a.get(0).style.backgroundImage.toString().length || this.settings.ui.theme_name == "themeroller") {
  867. if(a.get(0).style.backgroundImage.length) {
  868. obj += ' icon="' + a.get(0).style.backgroundImage.replace("url(","").replace(")","") + '" ';
  869. }
  870. if(this.settings.ui.theme_name == "themeroller" && a.children("ins").size()) {
  871. var tmp = a.children("ins").attr("class");
  872. var cls = false;
  873. $.each(tmp.split(" "), function (i, val) {
  874. if(val.indexOf("ui-icon-") == 0) {
  875. cls = val;
  876. return false;
  877. }
  878. });
  879. if(cls) obj += ' icon="' + cls + '" ';
  880. }
  881. if(inner_attrib.length) {
  882. for(var j in inner_attrib) {
  883. if(typeof inner_attrib[j] == "function") continue;
  884. var val = a.attr(inner_attrib[j]);
  885. if(typeof val != "undefined" && val.replace(" ","").length > 0) obj += ' ' + inner_attrib[j] + '="' + val + '" ';
  886. delete val;
  887. }
  888. }
  889. }
  890. obj += '><![CDATA[' + a.text() + ']]></name>';
  891. }
  892. }
  893. else {
  894. var a = nod.children("a");
  895. obj += '<name ';
  896. if(inner_attrib.length || a.get(0).style.backgroundImage.toString().length || this.settings.ui.theme_name == "themeroller") {
  897. if(a.get(0).style.backgroundImage.length) {
  898. obj += ' icon="' + a.get(0).style.backgroundImage.replace("url(","").replace(")","") + '" ';
  899. }
  900. if(this.settings.ui.theme_name == "themeroller" && a.children("ins").size()) {
  901. var tmp = a.children("ins").attr("class");
  902. var cls = false;
  903. $.each(tmp.split(" "), function (i, val) {
  904. if(val.indexOf("ui-icon-") == 0) {
  905. cls = val;
  906. return false;
  907. }
  908. });
  909. if(cls) obj += ' icon="' + cls + '" ';
  910. }
  911. if(inner_attrib.length) {
  912. for(var j in inner_attrib) {
  913. if(typeof inner_attrib[j] == "function") continue;
  914. var val = a.attr(inner_attrib[j]);
  915. if(typeof val != "undefined" && val.replace(" ","").length > 0) obj += ' ' + inner_attrib[j] + '="' + val + '" ';
  916. delete val;
  917. }
  918. }
  919. }
  920. obj += '><![CDATA[' + a.text() + ']]></name>';
  921. }
  922. obj += '</content>';
  923. if(tp == "flat") obj += '</item>';
  924. if(nod.children("ul").size() > 0) {
  925. nod.children("ul").children("li").each(function () {
  926. obj += _this.getXML(tp, this, outer_attrib, inner_attrib, true);
  927. });
  928. }
  929. if(tp == "nested") obj += '</item>';
  930. if(!cb) obj += '</root>';
  931. return obj;
  932. },
  933. focus : function () {
  934. if(this.locked) return false;
  935. if(tree_component.focused != this.cntr) {
  936. tree_component.focused = this.cntr;
  937. this.settings.callback.onfocus.call(null, this);
  938. }
  939. },
  940. show_context : function (obj) {
  941. this.context.show();
  942. var tmp = $(obj).children("a:visible").offset();
  943. this.context.css({ "left" : (tmp.left), "top" : (tmp.top + parseInt(obj.children("a:visible").height()) + 2) });
  944. },
  945. hide_context : function () {
  946. if(this.context.to_remove && this.context.apply_to) this.context.apply_to.children("a").removeClass("clicked");
  947. this.context.apply_to = false;
  948. this.context.hide();
  949. },
  950. // ALL EVENTS
  951. attachEvents : function () {
  952. var _this = this;
  953. this.container
  954. .bind("mousedown.jstree", function (event) {
  955. if(tree_component.drag_drop.isdown) {
  956. tree_component.drag_drop.move_type = false;
  957. event.preventDefault();
  958. event.stopPropagation();
  959. event.stopImmediatePropagation();
  960. return false;
  961. }
  962. })
  963. .bind("mouseup.jstree", function (event) {
  964. setTimeout( function() { _this.focus.apply(_this); }, 5);
  965. })
  966. .bind("click.jstree", function (event) {
  967. //event.stopPropagation();
  968. return true;
  969. });
  970. $("#" + this.container.attr("id") + " li")
  971. .live("click", function(event) { // WHEN CLICK IS ON THE ARROW
  972. if(event.target.tagName != "LI") return true;
  973. _this.off_height();
  974. if(event.pageY - $(event.target).offset().top > _this.li_height) return true;
  975. _this.toggle_branch.apply(_this, [event.target]);
  976. event.stopPropagation();
  977. return false;
  978. });
  979. $("#" + this.container.attr("id") + " li a")
  980. .live("click.jstree", function (event) { // WHEN CLICK IS ON THE TEXT OR ICON
  981. if(event.which && event.which == 3) return true;
  982. if(_this.locked) {
  983. event.preventDefault();
  984. event.target.blur();
  985. return _this.error("LOCKED");
  986. }
  987. _this.select_branch.apply(_this, [event.target, event.ctrlKey || _this.settings.rules.multiple == "on"]);
  988. if(_this.inp) { _this.inp.blur(); }
  989. event.preventDefault();
  990. event.target.blur();
  991. return false;
  992. })
  993. .live("dblclick.jstree", function (event) { // WHEN DOUBLECLICK ON TEXT OR ICON
  994. if(_this.locked) {
  995. event.preventDefault();
  996. event.stopPropagation();
  997. event.target.blur();
  998. return _this.error("LOCKED");
  999. }
  1000. _this.settings.callback.ondblclk.call(null, _this.get_node(event.target).get(0), _this);
  1001. event.preventDefault();
  1002. event.stopPropagation();
  1003. event.target.blur();
  1004. })
  1005. .live("contextmenu.jstree", function (event) {
  1006. if(_this.locked) {
  1007. event.target.blur();
  1008. return _this.error("LOCKED");
  1009. }
  1010. var val = _this.settings.callback.onrgtclk.call(null, _this.get_node(event.target).get(0), _this, event);
  1011. if(_this.context) {
  1012. if(_this.context.append == false) {
  1013. $("body").append(_this.context);
  1014. _this.context.append = true;
  1015. for(var i in _this.settings.ui.context) {
  1016. if(typeof _this.settings.ui.context[i] == "function") continue;
  1017. if(_this.settings.ui.context[i] == "separator") continue;
  1018. (function () {
  1019. var func = _this.settings.ui.context[i].action;
  1020. _this.context.children("[rel=" + _this.settings.ui.context[i].id +"]")
  1021. .bind("click", function (event) {
  1022. if(!$(this).hasClass("disabled")) {
  1023. func.call(null, _this.context.apply_to || null, _this);
  1024. _this.hide_context();
  1025. }
  1026. event.stopPropagation();
  1027. event.preventDefault();
  1028. return false;
  1029. })
  1030. .bind("mouseup", function (event) {
  1031. this.blur();
  1032. if($(this).hasClass("disabled")) {
  1033. event.stopPropagation();
  1034. event.preventDefault();
  1035. return false;
  1036. }
  1037. })
  1038. .bind("mousedown", function (event) {
  1039. event.stopPropagation();
  1040. event.preventDefault();
  1041. });
  1042. })();
  1043. }
  1044. }
  1045. var obj = _this.get_node(event.target);
  1046. if(_this.inp) { _this.inp.blur(); }
  1047. if(obj) {
  1048. if(!obj.children("a:eq(0)").hasClass("clicked")) {
  1049. // _this.select_branch.apply(_this, [event.target, event.ctrlKey || _this.settings.rules.multiple == "on"]);
  1050. _this.context.apply_to = obj;
  1051. _this.context.to_remove = true;
  1052. _this.context.apply_to.children("a").addClass("clicked");
  1053. event.target.blur();
  1054. }
  1055. else {
  1056. _this.context.to_remove = false;
  1057. _this.context.apply_to = (_this.selected_arr && _this.selected_arr.length > 1) ? _this.selected_arr : _this.selected;
  1058. }
  1059. _this.context.children("a").removeClass("disabled").show();
  1060. var go = false;
  1061. for(var i in _this.settings.ui.context) {
  1062. if(typeof _this.settings.ui.context[i] == "function") continue;
  1063. if(_this.settings.ui.context[i] == "separator") continue;
  1064. var state = _this.settings.ui.context[i].visible.call(null, _this.context.apply_to, _this);
  1065. if(state === false) _this.context.children("[rel=" + _this.settings.ui.context[i].id +"]").addClass("disabled");
  1066. if(state === -1) _this.context.children("[rel=" + _this.settings.ui.context[i].id +"]").hide();
  1067. else go = true;
  1068. }
  1069. if(go == true) _this.show_context(obj);
  1070. event.preventDefault();
  1071. event.stopPropagation();
  1072. return false;
  1073. }
  1074. }
  1075. return val;
  1076. })
  1077. .live("mouseover.jstree", function (event) {
  1078. if(_this.locked) {
  1079. event.preventDefault();
  1080. event.stopPropagation();
  1081. return _this.error("LOCKED");
  1082. }
  1083. if( (_this.settings.ui.hover_mode || _this.settings.ui.theme_name == "themeroller" ) && _this.hovered !== false && event.target.tagName == "A") {
  1084. _this.hovered.children("a").removeClass("hover ui-state-hover");
  1085. _this.hovered = false;
  1086. }
  1087. if(_this.settings.ui.theme_name == "themeroller") {
  1088. _this.hover_branch.apply(_this, [event.target]);
  1089. }
  1090. });
  1091. if(_this.settings.ui.theme_name == "themeroller") {
  1092. $("#" + this.container.attr("id") + " li a").live("mouseout", function (event) {
  1093. if(_this.hovered) _this.hovered.children("a").removeClass("hover ui-state-hover");
  1094. });
  1095. }
  1096. // ATTACH DRAG & DROP ONLY IF NEEDED
  1097. if(this.settings.rules.draggable != "none") {
  1098. $("#" + this.container.attr("id") + " li a")
  1099. .live("mousedown.jstree", function (event) {
  1100. if(_this.settings.rules.drag_button == "left" && event.which && event.which != 1) return true;
  1101. if(_this.settings.rules.drag_button == "right" && event.which && event.which != 3) return true;
  1102. _this.focus.apply(_this);
  1103. if(_this.locked) return _this.error("LOCKED");
  1104. // SELECT LIST ITEM NODE
  1105. var obj = _this.get_node(event.target);
  1106. // IF ITEM IS DRAGGABLE
  1107. if(_this.settings.rules.multiple != false && _this.selected_arr.length > 1 && obj.children("a:eq(0)").hasClass("clicked")) {
  1108. var counter = 0;
  1109. for(var i in _this.selected_arr) {
  1110. if(typeof _this.selected_arr[i] == "function") continue;
  1111. if(_this.check("draggable", _this.selected_arr[i])) {
  1112. _this.selected_arr[i].addClass("dragged");
  1113. tree_component.drag_drop.origin_tree = _this;
  1114. counter ++;
  1115. }
  1116. }
  1117. if(counter > 0) {
  1118. if(_this.check("draggable", obj)) tree_component.drag_drop.drag_node = obj;
  1119. else tree_component.drag_drop.drag_node = _this.container.find("li.dragged:eq(0)");
  1120. tree_component.drag_drop.isdown = true;
  1121. tree_component.drag_drop.drag_help = $("<div id='jstree-dragged' class='tree " + (_this.container.hasClass("tree-default") ? " tree-default" : "" ) + (_this.settings.ui.theme_name && _this.settings.ui.theme_name != "default" ? " tree-" + _this.settings.ui.theme_name : "" ) + "' />").append("<ul class='" + _this.container.children("ul:eq(0)").get(0).className + "' />");
  1122. var tmp = $(tree_component.drag_drop.drag_node.get(0).cloneNode(true));
  1123. if(_this.settings.languages.length > 0) tmp.find("a").not("." + _this.current_lang).hide();
  1124. tree_component.drag_drop.drag_help.children("ul:eq(0)").append(tmp);
  1125. tree_component.drag_drop.drag_help.find("li:eq(0)").removeClass("last").addClass("last").children("a").html("Multiple selection").end().children("ul").remove();
  1126. }
  1127. }
  1128. else {
  1129. if(_this.check("draggable", obj)) {
  1130. tree_component.drag_drop.drag_node = obj;
  1131. tree_component.drag_drop.drag_help = $("<div id='jstree-dragged' class='tree " + (_this.container.hasClass("tree-default") ? " tree-default" : "" ) + (_this.settings.ui.theme_name && _this.settings.ui.theme_name != "default" ? " tree-" + _this.settings.ui.theme_name : "" ) + "' />").append("<ul class='" + _this.container.children("ul:eq(0)").get(0).className + "' />");
  1132. var tmp = $(obj.get(0).cloneNode(true));
  1133. if(_this.settings.languages.length > 0) tmp.find("a").not("." + _this.current_lang).hide();
  1134. tree_component.drag_drop.drag_help.children("ul:eq(0)").append(tmp);
  1135. tree_component.drag_drop.drag_help.find("li:eq(0)").removeClass("last").addClass("last");
  1136. tree_component.drag_drop.isdown = true;
  1137. tree_component.drag_drop.foreign = false;
  1138. tree_component.drag_drop.origin_tree = _this;
  1139. obj.addClass("dragged");
  1140. }
  1141. }
  1142. tree_component.drag_drop.init_x = event.pageX;
  1143. tree_component.drag_drop.init_y = event.pageY;
  1144. obj.blur();
  1145. event.preventDefault();
  1146. event.stopPropagation();
  1147. return false;
  1148. });
  1149. $(document)
  1150. .bind("mousedown.jstree", tree_component.mousedown)
  1151. .bind("mouseup.jstree", tree_component.mouseup)
  1152. .bind("mousemove.jstree", tree_component.mousemove);
  1153. }
  1154. // ENDIF OF DRAG & DROP FUNCTIONS
  1155. if(_this.context) $(document).bind("mousedown", function() { _this.hide_context(); });
  1156. },
  1157. checkMove : function (NODES, REF_NODE, TYPE) {
  1158. if(this.locked) return this.error("LOCKED");
  1159. var _this = this;
  1160. // OVER SELF OR CHILDREN
  1161. if(REF_NODE.parents("li.dragged").size() > 0 || REF_NODE.is(".dragged")) return this.error("MOVE: NODE OVER SELF");
  1162. // CHECK AGAINST DRAG_RULES
  1163. if(NODES.size() == 1) {
  1164. var NODE = NODES.eq(0);
  1165. if(tree_component.drag_drop.foreign) {
  1166. if(this.settings.rules.droppable.length == 0) return false;
  1167. if(!NODE.is("." + this.settings.rules.droppable.join(", ."))) return false;
  1168. var ok = false;
  1169. for(var i in this.settings.rules.droppable) {
  1170. if(typeof this.settings.rules.droppable[i] == "function") continue;
  1171. if(NODE.is("." + this.settings.rules.droppable[i])) {
  1172. if(this.settings.rules.metadata) {
  1173. $.metadata.setType("attr", this.settings.rules.metadata);
  1174. NODE.attr(this.settings.rules.metadata, "type: '" + this.settings.rules.droppable[i] + "'");
  1175. }
  1176. else {
  1177. NODE.attr(this.settings.rules.type_attr, this.settings.rules.droppable[i]);
  1178. }
  1179. ok = true;
  1180. break;
  1181. }
  1182. }
  1183. if(!ok) return false;
  1184. }
  1185. if(!this.check("dragrules", [NODE, TYPE, REF_NODE.parents("li:eq(0)")])) return this.error("MOVE: AGAINST DRAG RULES");
  1186. }
  1187. else {
  1188. var ok = true;
  1189. NODES.each(function (i) {
  1190. if(ok == false) return false;
  1191. //if(i > 0) {
  1192. // var ref = NODES.eq( (i - 1) );
  1193. // var mv = "after";
  1194. //}
  1195. //else {
  1196. var ref = REF_NODE;
  1197. var mv = TYPE;
  1198. //}
  1199. if(!_this.check.apply(_this,["dragrules", [$(this), mv, ref]])) ok = false;
  1200. });
  1201. if(ok == false) return this.error("MOVE: AGAINST DRAG RULES");
  1202. }
  1203. // CHECK AGAINST METADATA
  1204. if(this.settings.rules.use_inline && this.settings.rules.metadata) {
  1205. var nd = false;
  1206. if(TYPE == "inside") nd = REF_NODE.parents("li:eq(0)");
  1207. else nd = REF_NODE.parents("li:eq(1)");
  1208. if(nd.size()) {
  1209. // VALID CHILDREN CHECK
  1210. if(typeof nd.metadata()["valid_children"] != "undefined") {
  1211. var tmp = nd.metadata()["valid_children"];
  1212. var ok = true;
  1213. NODES.each(function (i) {
  1214. if(ok == false) return false;
  1215. if($.inArray(_this.get_type(this), tmp) == -1) ok = false;
  1216. });
  1217. if(ok == false) return this.error("MOVE: NOT A VALID CHILD");
  1218. }
  1219. // CHECK IF PARENT HAS FREE SLOTS FOR CHILDREN
  1220. if(typeof nd.metadata()["max_children"] != "undefined") {
  1221. if((nd.children("ul:eq(0)").children("li").not(".dragged").size() + NODES.size()) > nd.metadata().max_children) return this.error("MOVE: MAX CHILDREN REACHED");
  1222. }
  1223. // CHECK FOR MAXDEPTH UP THE CHAIN
  1224. var incr = 0;
  1225. NODES.each(function (j) {
  1226. var i = 1;
  1227. var t = $(this);
  1228. while(i < 100) {
  1229. t = t.children("ul").children("li");
  1230. if(t.size() == 0) break;
  1231. i ++
  1232. }
  1233. incr = Math.max(i,incr);
  1234. });
  1235. var ok = true;
  1236. if((typeof $(nd).metadata().max_depth).toLowerCase() != "undefined" && $(nd).metadata().max_depth < incr) ok = false;
  1237. else {
  1238. nd.parents("li").each(function(i) {
  1239. if(ok == false) return false;
  1240. if((typeof $(this).metadata().max_depth).toLowerCase() != "undefined") {
  1241. if( (i + incr) >= $(this).metadata().max_depth) ok = false;
  1242. }
  1243. });
  1244. }
  1245. if(ok == false) return this.error("MOVE: MAX_DEPTH REACHED");
  1246. }
  1247. }
  1248. return true;
  1249. },
  1250. // USED AFTER REFRESH
  1251. reselect : function (is_callback) {
  1252. var _this = this;
  1253. if(!is_callback) this.cl_count = 0;
  1254. else this.cl_count --;
  1255. // REOPEN BRANCHES
  1256. if(this.opened && this.opened.length) {
  1257. var opn = false;
  1258. for(var j = 0; this.opened && j < this.opened.length; j++) {
  1259. if(this.settings.data.async) {
  1260. if(this.get_node(this.opened[j]).size() > 0) {
  1261. opn = true;
  1262. var tmp = this.opened[j];
  1263. delete this.opened[j];
  1264. this.open_branch(tmp, true, function () { _this.reselect.apply(_this, [true]); } );
  1265. this.cl_count ++;
  1266. }
  1267. }
  1268. else this.open_branch(this.opened[j], true);
  1269. }
  1270. if(this.settings.data.async && opn) return;
  1271. delete this.opened;
  1272. }
  1273. if(this.cl_count > 0) return;
  1274. // DOTS and RIGHT TO LEFT
  1275. if(this.settings.ui.rtl) this.container.css("direction","rtl").children("ul:eq(0)").addClass("rtl");
  1276. else this.container.css("direction","ltr").children("ul:eq(0)").addClass("ltr");
  1277. if(this.settings.ui.dots == false) this.container.children("ul:eq(0)").addClass("no_dots");
  1278. // REPOSITION SCROLL
  1279. if(this.scrtop) {
  1280. this.container.scrollTop(_this.scrtop);
  1281. delete this.scrtop;
  1282. }
  1283. // RESELECT PREVIOUSLY SELECTED
  1284. if(this.settings.selected !== false) {
  1285. $.each(this.settings.selected, function (i) {
  1286. if(_this.is_partial_refresh) _this.select_branch($(_this.settings.selected[i], _this.container), (_this.settings.rules.multiple !== false) );
  1287. else _this.select_branch($(_this.settings.selected[i], _this.container), (_this.settings.rules.multiple !== false && i > 0) );
  1288. });
  1289. this.settings.selected = false;
  1290. }
  1291. if(this.settings.ui.theme_name == "themeroller") this.container.find("a").addClass("ui-state-default");
  1292. this.settings.callback.onload.call(null, _this);
  1293. },
  1294. // GET THE EXTENDED LI ELEMENT
  1295. get_node : function (obj) {
  1296. var obj = $(obj);
  1297. return obj.is("li") ? obj : obj.parents("li:eq(0)");
  1298. },
  1299. // GET THE TYPE OF THE NODE
  1300. get_type : function (obj) {
  1301. obj = !obj ? this.selected : this.get_node(obj);
  1302. if(!obj) return;
  1303. if(this.settings.rules.metadata) {
  1304. $.metadata.setType("attr", this.settings.rules.metadata);
  1305. var tmp = obj.metadata().type;
  1306. if(tmp) return tmp;
  1307. }
  1308. return obj.attr(this.settings.rules.type_attr);
  1309. },
  1310. // SCROLL CONTAINER WHILE DRAGGING
  1311. scrollCheck : function (x,y) {
  1312. var _this = this;
  1313. var cnt = _this.container;
  1314. var off = _this.container.offset();
  1315. var st = cnt.scrollTop();
  1316. var sl = cnt.scrollLeft();
  1317. // DETECT HORIZONTAL SCROLL
  1318. var h_cor = (cnt.get(0).scrollWidth > cnt.width()) ? 40 : 20;
  1319. if(y - off.top < 20) cnt.scrollTop(Math.max( (st - _this.settings.ui.scroll_spd) ,0)); // NEAR TOP
  1320. if(cnt.height() - (y - off.top) < h_cor) cnt.scrollTop(st + _this.settings.ui.scroll_spd); // NEAR BOTTOM
  1321. if(x - off.left < 20) cnt.scrollLeft(Math.max( (sl - _this.settings.ui.scroll_spd),0)); // NEAR LEFT
  1322. if(cnt.width() - (x - off.left) < 40) cnt.scrollLeft(sl + _this.settings.ui.scroll_spd); // NEAR RIGHT
  1323. if(cnt.scrollLeft() != sl || cnt.scrollTop() != st) {
  1324. _this.moveType = false;
  1325. _this.moveRef = false;
  1326. tree_component.drag_drop.marker.hide();
  1327. }
  1328. tree_component.drag_drop.scroll_time = setTimeout( function() { _this.scrollCheck(x,y); }, 50);
  1329. },
  1330. check : function (rule, nodes) {
  1331. if(this.locked) return this.error("LOCKED");
  1332. // CHECK LOCAL RULES IF METADATA
  1333. if(rule != "dragrules" && this.settings.rules.use_inline && this.settings.rules.metadata) {
  1334. $.metadata.setType("attr", this.settings.rules.metadata);
  1335. if(typeof this.get_node(nodes).metadata()[rule] != "undefined") return this.get_node(nodes).metadata()[rule];
  1336. }
  1337. if(!this.settings.rules[rule]) return false;
  1338. if(this.settings.rules[rule] == "none") return false;
  1339. if(this.settings.rules[rule] == "all") return true;
  1340. if(rule == "dragrules") {
  1341. var nds = new Array();
  1342. nds[0] = this.get_type(nodes[0]);
  1343. nds[1] = nodes[1];
  1344. nds[2] = this.get_type(nodes[2]);
  1345. for(var i = 0; i < this.settings.rules.dragrules.length; i++) {
  1346. var r = this.settings.rules.dragrules[i];
  1347. var n = (r.indexOf("!") === 0) ? false : true;
  1348. if(!n) r = r.replace("!","");
  1349. var tmp = r.split(" ");
  1350. for(var j = 0; j < 3; j++) {
  1351. if(tmp[j] == nds[j] || tmp[j] == "*") tmp[j] = true;
  1352. }
  1353. if(tmp[0] === true && tmp[1] === true && tmp[2] === true) return n;
  1354. }
  1355. return false;
  1356. }
  1357. else
  1358. return ($.inArray(this.get_type(nodes),this.settings.rules[rule]) != -1) ? true : false;
  1359. },
  1360. hover_branch : function (obj) {
  1361. if(this.locked) return this.error("LOCKED");
  1362. if(this.settings.ui.hover_mode == false && this.settings.ui.theme_name != "themeroller") return this.select_branch(obj);
  1363. var _this = this;
  1364. var obj = _this.get_node(obj);
  1365. if(!obj.size()) return this.error("HOVER: NOT A VALID NODE");
  1366. // CHECK AGAINST RULES FOR SELECTABLE NODES
  1367. if(!_this.check("clickable", obj)) return this.error("SELECT: NODE NOT SELECTABLE");
  1368. if(this.hovered) this.hovered.children("A").removeClass("hover ui-state-hover");
  1369. // SAVE NEWLY SELECTED
  1370. this.hovered = obj;
  1371. // FOCUS NEW NODE AND OPEN ALL PARENT NODES IF CLOSED
  1372. this.hovered.children("a").removeClass("hover ui-state-hover").addClass( this.settings.ui.theme_name == "themeroller" ? "hover ui-state-hover" : "hover");
  1373. // SCROLL SELECTED NODE INTO VIEW
  1374. var off_t = this.hovered.offset().top;
  1375. var beg_t = this.container.offset().top;
  1376. var end_t = beg_t + this.container.height();
  1377. var h_cor = (this.container.get(0).scrollWidth > this.container.width()) ? 40 : 20;
  1378. if(off_t + 5 < beg_t) this.container.scrollTop(this.container.scrollTop() - (beg_t - off_t + 5) );
  1379. if(off_t + h_cor > end_t) this.container.scrollTop(this.container.scrollTop() + (off_t + h_cor - end_t) );
  1380. },
  1381. select_branch : function (obj, multiple) {
  1382. if(this.locked) return this.error("LOCKED");
  1383. if(!obj && this.hovered !== false) obj = this.hovered;
  1384. var _this = this;
  1385. obj = _this.get_node(obj);
  1386. if(!obj.size()) return this.error("SELECT: NOT A VALID NODE");
  1387. obj.children("a").removeClass("hover ui-state-hover");
  1388. // CHECK AGAINST RULES FOR SELECTABLE NODES
  1389. if(!_this.check("clickable", obj)) return this.error("SELECT: NODE NOT SELECTABLE");
  1390. if(_this.settings.callback.beforechange.call(null,obj.get(0),_this) === false) return this.error("SELECT: STOPPED BY USER");
  1391. // IF multiple AND obj IS ALREADY SELECTED - DESELECT IT
  1392. if(this.settings.rules.multiple != false && multiple && obj.children("a.clicked").size() > 0) {
  1393. return this.deselect_branch(obj);
  1394. }
  1395. if(this.settings.rules.multiple != false && multiple) {
  1396. this.selected_arr.push(obj);
  1397. }
  1398. if(this.settings.rules.multiple != false && !multiple) {
  1399. for(var i in this.selected_arr) {
  1400. if(typeof this.selected_arr[i] == "function") continue;
  1401. this.selected_arr[i].children("A").removeClass("clicked ui-state-active");
  1402. this.settings.callback.ondeselect.call(null, this.selected_arr[i].get(0), _this);
  1403. }
  1404. this.selected_arr = [];
  1405. this.selected_arr.push(obj);
  1406. if(this.selected && this.selected.children("A").hasClass("clicked")) {
  1407. this.selected.children("A").removeClass("clicked ui-state-active");
  1408. this.settings.callback.ondeselect.call(null, this.selected.get(0), _this);
  1409. }
  1410. }
  1411. if(!this.settings.rules.multiple) {
  1412. if(this.selected) {
  1413. this.selected.children("A").removeClass("clicked ui-state-active");
  1414. this.settings.callback.ondeselect.call(null, this.selected.get(0), _this);
  1415. }
  1416. }
  1417. // SAVE NEWLY SELECTED
  1418. this.selected = obj;
  1419. if( (this.settings.ui.hover_mode || this.settings.ui.theme_name == "themeroller") && this.hovered !== false) {
  1420. this.hovered.children("A").removeClass("hover ui-state-hover");
  1421. this.hovered = obj;
  1422. }
  1423. // FOCUS NEW NODE AND OPEN ALL PARENT NODES IF CLOSED
  1424. this.selected.children("a").removeClass("clicked ui-state-active").addClass( this.settings.ui.theme_name == "themeroller" ? "clicked ui-state-active" : "clicked").end().parents("li.closed").each( function () { _this.open_branch(this, true); });
  1425. // SCROLL SELECTED NODE INTO VIEW
  1426. var off_t = this.selected.offset().top;
  1427. var beg_t = this.container.offset().top;
  1428. var end_t = beg_t + this.container.height();
  1429. var h_cor = (this.container.get(0).scrollWidth > this.container.width()) ? 40 : 20;
  1430. if(off_t + 5 < beg_t) this.container.scrollTop(this.container.scrollTop() - (beg_t - off_t + 5) );
  1431. if(off_t + h_cor > end_t) this.container.scrollTop(this.container.scrollTop() + (off_t + h_cor - end_t) );
  1432. this.set_cookie("selected");
  1433. this.settings.callback.onselect.call(null, this.selected.get(0), _this);
  1434. this.settings.callback.onchange.call(null, this.selected.get(0), _this);
  1435. },
  1436. deselect_branch : function (obj) {
  1437. if(this.locked) return this.error("LOCKED");
  1438. var _this = this;
  1439. var obj = this.get_node(obj);
  1440. obj.children("a").removeClass("clicked ui-state-active");
  1441. this.settings.callback.ondeselect.call(null, obj.get(0), _this);
  1442. if(this.settings.rules.multiple != false && this.selected_arr.length > 1) {
  1443. this.selected_arr = [];
  1444. this.container.find("a.clicked").filter(":first-child").parent().each(function () {
  1445. _this.selected_arr.push($(this));
  1446. });
  1447. if(obj.get(0) == this.selected.get(0)) {
  1448. this.selected = this.selected_arr[0];
  1449. this.set_cookie("selected");
  1450. }
  1451. }
  1452. else {
  1453. if(this.settings.rules.multiple != false) this.selected_arr = [];
  1454. this.selected = false;
  1455. this.set_cookie("selected");
  1456. }
  1457. if(this.selected) this.settings.callback.onchange.call(null, this.selected.get(0), _this);
  1458. else this.settings.callback.onchange.call(null, false, _this);
  1459. },
  1460. toggle_branch : function (obj) {
  1461. if(this.locked) return this.error("LOCKED");
  1462. var obj = this.get_node(obj);
  1463. if(obj.hasClass("closed")) return this.open_branch(obj);
  1464. if(obj.hasClass("open")) return this.close_branch(obj);
  1465. },
  1466. open_branch : function (obj, disable_animation, callback) {
  1467. if(this.locked) return this.error("LOCKED");
  1468. var obj = this.get_node(obj);
  1469. if(!obj.size()) return this.error("OPEN: NO SUCH NODE");
  1470. if(obj.hasClass("leaf")) return this.error("OPEN: OPENING LEAF NODE");
  1471. if(this.settings.data.async && obj.find("li").size() == 0) {
  1472. if(this.settings.callback.beforeopen.call(null,obj.get(0),this) === false) return this.error("OPEN: STOPPED BY USER");
  1473. var _this = this;
  1474. obj.children("ul:eq(0)").remove().end().append("<ul><li class='last'><a class='loading' href='#'>" + (_this.settings.lang.loading || "Loading ...") + "</a></li></ul>");
  1475. obj.removeClass("closed").addClass("open");
  1476. if(this.settings.data.type == "xml_flat" || this.settings.data.type == "xml_nested") {
  1477. var xsl = (this.settings.data.type == "xml_flat") ? "flat.xsl" : "nested.xsl";
  1478. obj.children("ul:eq(0)").getTransform(this.path + xsl, this.settings.data.url, { params : { theme_path : _this.theme }, meth : this.settings.data.method, dat : this.settings.data.async_data(obj, this), repl : true,
  1479. callback: function (str, json) {
  1480. if(str.length < 15) {
  1481. obj.removeClass("closed").removeClass("open").addClass("leaf").children("ul").remove();
  1482. if(callback) callback.call();
  1483. return;
  1484. }
  1485. _this.open_branch.apply(_this, [obj]);
  1486. if(callback) callback.call();
  1487. },
  1488. error : function () { obj.removeClass("open").addClass("closed").children("ul:eq(0)").remove(); }
  1489. });
  1490. }
  1491. else {
  1492. $.ajax({
  1493. type : this.settings.data.method,
  1494. url : this.settings.data.url,
  1495. data : this.settings.data.async_data(obj, this),
  1496. dataType : "json",
  1497. success : function (data, textStatus) {
  1498. data = _this.settings.callback.onJSONdata.call(null, data, _this);
  1499. if(!data || data.length == 0) {
  1500. obj.removeClass("closed").removeClass("open").addClass("leaf").children("ul").remove();
  1501. if(callback) callback.call();
  1502. return;
  1503. }
  1504. var str = "";
  1505. if(data.length) {
  1506. for(var i = 0; i < data.length; i++) {
  1507. str += _this.parseJSON(data[i]);
  1508. }
  1509. }
  1510. else str = _this.parseJSON(data);
  1511. if(str.length > 0) {
  1512. obj.children("ul:eq(0)").replaceWith("<ul>" + str + "</ul>");
  1513. obj.find("li:last-child").addClass("last").end().find("li:has(ul)").not(".open").addClass("closed");
  1514. obj.find("li").not(".open").not(".closed").addClass("leaf");
  1515. _this.open_branch.apply(_this, [obj]);
  1516. }
  1517. else obj.removeClass("closed").removeClass("open").addClass("leaf").children("ul").remove();
  1518. if(callback) callback.call();
  1519. },
  1520. error : function (xhttp, textStatus, errorThrown) { obj.removeClass("open").addClass("closed").children("ul:eq(0)").remove(); _this.error(errorThrown + " " + textStatus); }
  1521. });
  1522. }
  1523. return true;
  1524. }
  1525. else {
  1526. if(!this.settings.data.async) {
  1527. if(this.settings.callback.beforeopen.call(null,obj.get(0),this) === false) return this.error("OPEN: STOPPED BY USER");
  1528. }
  1529. if(this.settings.ui.theme_name == "themeroller") obj.find("a").not(".ui-state-default").addClass("ui-state-default");
  1530. if(parseInt(this.settings.ui.animation) > 0 && !disable_animation ) {
  1531. obj.children("ul:eq(0)").css("display","none");
  1532. obj.removeClass("closed").addClass("open");
  1533. obj.children("ul:eq(0)").slideDown(parseInt(this.settings.ui.animation), function() {
  1534. $(this).css("display","");
  1535. if(callback) callback.call();
  1536. });
  1537. } else {
  1538. obj.removeClass("closed").addClass("open");
  1539. if(callback) callback.call();
  1540. }
  1541. this.set_cookie("open");
  1542. this.settings.callback.onopen.call(null, obj.get(0), this);
  1543. return true;
  1544. }
  1545. },
  1546. close_branch : function (obj, disable_animation) {
  1547. if(this.locked) return this.error("LOCKED");
  1548. var _this = this;
  1549. var obj = this.get_node(obj);
  1550. if(!obj.size()) return this.error("CLOSE: NO SUCH NODE");
  1551. if(_this.settings.callback.beforeclose.call(null,obj.get(0),_this) === false) return this.error("CLOSE: STOPPED BY USER");
  1552. if(parseInt(this.settings.ui.animation) > 0 && !disable_animation && obj.children("ul:eq(0)").size() == 1) {
  1553. obj.children("ul:eq(0)").slideUp(parseInt(this.settings.ui.animation), function() {
  1554. if(obj.hasClass("open")) obj.removeClass("open").addClass("closed");
  1555. _this.set_cookie("open");
  1556. $(this).css("display","");
  1557. });
  1558. }
  1559. else {
  1560. if(obj.hasClass("open")) obj.removeClass("open").addClass("closed");
  1561. this.set_cookie("open");
  1562. }
  1563. if(this.selected && obj.children("ul:eq(0)").find("a.clicked").size() > 0) {
  1564. obj.find("li:has(a.clicked)").each(function() {
  1565. _this.deselect_branch(this);
  1566. });
  1567. if(obj.children("a.clicked").size() == 0) this.select_branch(obj, (this.settings.rules.multiple != false && this.selected_arr.length > 0) );
  1568. }
  1569. this.settings.callback.onclose.call(null, obj.get(0), this);
  1570. },
  1571. open_all : function (obj, callback) {
  1572. if(this.locked) return this.error("LOCKED");
  1573. var _this = this;
  1574. obj = obj ? this.get_node(obj).parent() : this.container;
  1575. var s = obj.find("li.closed").size();
  1576. if(!callback) this.cl_count = 0;
  1577. else this.cl_count --;
  1578. if(s > 0) {
  1579. this.cl_count += s;
  1580. obj.find("li.closed").each( function () { var __this = this; _this.open_branch.apply(_this, [this, true, function() { _this.open_all.apply(_this, [__this, true]); } ]); });
  1581. }
  1582. else if(this.cl_count == 0) this.settings.callback.onopen_all.call(null,this);
  1583. },
  1584. close_all : function () {
  1585. if(this.locked) return this.error("LOCKED");
  1586. var _this = this;
  1587. this.container.find("li.open").each( function () { _this.close_branch(this, true); });
  1588. },
  1589. show_lang : function (i) {
  1590. if(this.locked) return this.error("LOCKED");
  1591. if(this.settings.languages[i] == this.current_lang) return true;
  1592. var st = false;
  1593. var id = this.container.attr("id") ? "#" + this.container.attr("id") : ".tree";
  1594. st = get_css(id + " ." + this.current_lang, this.sn);
  1595. if(st !== false) st.style.display = "none";
  1596. st = get_css(id + " ." + this.settings.languages[i], this.sn);
  1597. if(st !== false) st.style.display = "";
  1598. this.current_lang = this.settings.languages[i];
  1599. return true;
  1600. },
  1601. cycle_lang : function() {
  1602. if(this.locked) return this.error("LOCKED");
  1603. var i = $.inArray(this.current_lang, this.settings.languages);
  1604. i ++;
  1605. if(i > this.settings.languages.length - 1) i = 0;
  1606. this.show_lang(i);
  1607. },
  1608. create : function (obj, ref_node, position) {
  1609. if(this.locked) return this.error("LOCKED");
  1610. var root = false;
  1611. if(ref_node == -1) { root = true; ref_node = this.container; }
  1612. else ref_node = ref_node ? this.get_node(ref_node) : this.selected;
  1613. if(!root && (!ref_node || !ref_node.size())) return this.error("CREATE: NO NODE SELECTED");
  1614. var pos = position;
  1615. var tmp = ref_node; // for type calculation
  1616. if(position == "before") {
  1617. position = ref_node.parent().children().index(ref_node);
  1618. ref_node = ref_node.parents("li:eq(0)");
  1619. }
  1620. if(position == "after") {
  1621. position = ref_node.parent().children().index(ref_node) + 1;
  1622. ref_node = ref_node.parents("li:eq(0)");
  1623. }
  1624. if(!root && ref_node.size() == 0) { root = true; ref_node = this.container; }
  1625. if(!root) {
  1626. if(!this.check("creatable", ref_node)) return this.error("CREATE: CANNOT CREATE IN NODE");
  1627. if(ref_node.hasClass("closed")) {
  1628. if(this.settings.data.async && ref_node.children("ul").size() == 0) {
  1629. var _this = this;
  1630. return this.open_branch(ref_node, true, function () { _this.create.apply(_this, [obj, ref_node, position]); } );
  1631. }
  1632. else this.open_branch(ref_node, true);
  1633. }
  1634. }
  1635. // creating new object to pass to parseJSON
  1636. var torename = false;
  1637. if(!obj) obj = {};
  1638. else obj = $.extend(true, {}, obj);
  1639. if(!obj.attributes) obj.attributes = {};
  1640. if(this.settings.rules.metadata) {
  1641. if(!obj.attributes[this.settings.rules.metadata]) obj.attributes[this.settings.rules.metadata] = '{ "type" : "' + (this.get_type(tmp) || "") + '" }';
  1642. }
  1643. else {
  1644. if(!obj.attributes[this.settings.rules.type_attr]) obj.attributes[this.settings.rules.type_attr] = this.get_type(tmp) || "";
  1645. }
  1646. if(this.settings.languages.length) {
  1647. if(!obj.data) { obj.data = {}; torename = true; }
  1648. for(var i = 0; i < this.settings.languages.length; i++) {
  1649. 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;
  1650. }
  1651. }
  1652. else {
  1653. if(!obj.data) { obj.data = this.settings.lang.new_node; torename = true; }
  1654. }
  1655. var $li = $(this.parseJSON(obj));
  1656. if($li.children("ul").size()) {
  1657. if(!$li.is(".open")) $li.addClass("closed");
  1658. }
  1659. else $li.addClass("leaf");
  1660. $li.find("li:last-child").addClass("last").end().find("li:has(ul)").not(".open").addClass("closed");
  1661. $li.find("li").not(".open").not(".closed").addClass("leaf");
  1662. if(!root && this.settings.rules.use_inline && this.settings.rules.metadata) {
  1663. var t = this.get_type($li) || "";
  1664. $.metadata.setType("attr", this.settings.rules.metadata);
  1665. if(typeof ref_node.metadata()["valid_children"] != "undefined") {
  1666. if($.inArray(t, ref_node.metadata()["valid_children"]) == -1) return this.error("CREATE: NODE NOT A VALID CHILD");
  1667. }
  1668. if(typeof ref_node.metadata()["max_children"] != "undefined") {
  1669. if( (ref_node.children("ul:eq(0)").children("li").size() + 1) > ref_node.metadata().max_children) return this.error("CREATE: MAX_CHILDREN REACHED");
  1670. }
  1671. var ok = true;
  1672. if((typeof $(ref_node).metadata().max_depth).toLowerCase() != "undefined" && $(ref_node).metadata().max_depth === 0) ok = false;
  1673. else {
  1674. ref_node.parents("li").each(function(i) {
  1675. if($(this).metadata().max_depth) {
  1676. if( (i + 1) >= $(this).metadata().max_depth) {
  1677. ok = false;
  1678. return false;
  1679. }
  1680. }
  1681. });
  1682. }
  1683. if(!ok) return this.error("CREATE: MAX_DEPTH REACHED");
  1684. }
  1685. if((typeof position).toLowerCase() == "undefined" || position == "inside")
  1686. position = (this.settings.rules.createat == "top") ? 0 : ref_node.children("ul:eq(0)").children("li").size();
  1687. if(ref_node.children("ul").size() == 0 || (root == true && ref_node.children("ul").children("li").size() == 0) ) {
  1688. if(!root) var a = this.moved($li,ref_node.children("a:eq(0)"),"inside", true);
  1689. else var a = this.moved($li,this.container.children("ul:eq(0)"),"inside", true);
  1690. }
  1691. else if(pos == "before" && ref_node.children("ul:eq(0)").children("li:nth-child(" + (position + 1) + ")").size())
  1692. var a = this.moved($li,ref_node.children("ul:eq(0)").children("li:nth-child(" + (position + 1) + ")").children("a:eq(0)"),"before", true);
  1693. else if(pos == "after" && ref_node.children("ul:eq(0)").children("li:nth-child(" + (position) + ")").size())
  1694. var a = this.moved($li,ref_node.children("ul:eq(0)").children("li:nth-child(" + (position) + ")").children("a:eq(0)"),"after", true);
  1695. else if(ref_node.children("ul:eq(0)").children("li:nth-child(" + (position + 1) + ")").size())
  1696. var a = this.moved($li,ref_node.children("ul:eq(0)").children("li:nth-child(" + (position + 1) + ")").children("a:eq(0)"),"before", true);
  1697. else
  1698. var a = this.moved($li,ref_node.children("ul:eq(0)").children("li:last").children("a:eq(0)"),"after",true);
  1699. if(a === false) return this.error("CREATE: ABORTED");
  1700. if(torename) {
  1701. this.select_branch($li.children("a:eq(0)"));
  1702. this.rename();
  1703. }
  1704. return $li;
  1705. },
  1706. rename : function (obj) {
  1707. if(this.locked) return this.error("LOCKED");
  1708. obj = obj ? this.get_node(obj) : this.selected;
  1709. var _this = this;
  1710. if(!obj || !obj.size()) return this.error("RENAME: NO NODE SELECTED");
  1711. if(!this.check("renameable", obj)) return this.error("RENAME: NODE NOT RENAMABLE");
  1712. if(!this.settings.callback.beforerename.call(null,obj.get(0), _this.current_lang, _this)) return this.error("RENAME: STOPPED BY USER");
  1713. obj.parents("li.closed").each(function () { _this.open_branch(this) });
  1714. if(this.current_lang) obj = obj.find("a." + this.current_lang).get(0);
  1715. else obj = obj.find("a:first").get(0);
  1716. last_value = obj.innerHTML;
  1717. _this.inp = $("<input type='text' autocomplete='off' />");
  1718. _this.inp
  1719. .val(last_value.replace(/&amp;/g,"&").replace(/&gt;/g,">").replace(/&lt;/g,"<"))
  1720. .bind("mousedown", function (event) { event.stopPropagation(); })
  1721. .bind("mouseup", function (event) { event.stopPropagation(); })
  1722. .bind("click", function (event) { event.stopPropagation(); })
  1723. .bind("keyup", function (event) {
  1724. var key = event.keyCode || event.which;
  1725. if(key == 27) { this.value = last_value; this.blur(); return }
  1726. if(key == 13) { this.blur(); return }
  1727. });
  1728. // Rollback
  1729. var rb = {};
  1730. rb[this.container.attr("id")] = this.get_rollback();
  1731. _this.inp.blur(function(event) {
  1732. if(this.value == "") this.value = last_value;
  1733. $(obj).text( $(obj).parent().find("input").eq(0).attr("value") ).get(0).style.display = "";
  1734. $(obj).prevAll("span").remove();
  1735. _this.settings.callback.onrename.call(null, _this.get_node(obj).get(0), _this.current_lang, _this, rb);
  1736. _this.inp = false;
  1737. });
  1738. var spn = $("<span />").addClass(obj.className).append(_this.inp);
  1739. spn.attr("style", $(obj).attr("style"));
  1740. obj.style.display = "none";
  1741. $(obj).parent().prepend(spn);
  1742. _this.inp.get(0).focus();
  1743. _this.inp.get(0).select();
  1744. },
  1745. // REMOVE NODES
  1746. remove : function(obj) {
  1747. if(this.locked) return this.error("LOCKED");
  1748. // Rollback
  1749. var rb = {};
  1750. rb[this.container.attr("id")] = this.get_rollback();
  1751. if(obj && (!this.selected || this.get_node(obj).get(0) != this.selected.get(0) )) {
  1752. obj = this.get_node(obj);
  1753. if(obj.size()) {
  1754. if(!this.check("deletable", obj)) return this.error("DELETE: NODE NOT DELETABLE");
  1755. if(!this.settings.callback.beforedelete.call(null,obj.get(0), _this)) return this.error("DELETE: STOPPED BY USER");
  1756. $parent = obj.parent();
  1757. obj = obj.remove();
  1758. $parent.children("li:last").addClass("last");
  1759. if($parent.children("li").size() == 0) {
  1760. $li = $parent.parents("li:eq(0)");
  1761. $li.removeClass("open").removeClass("closed").addClass("leaf").children("ul").remove();
  1762. this.set_cookie("open");
  1763. }
  1764. this.settings.callback.ondelete.call(null, obj.get(0), this, rb);
  1765. }
  1766. }
  1767. else if(this.selected) {
  1768. if(!this.check("deletable", this.selected)) return this.error("DELETE: NODE NOT DELETABLE");
  1769. if(!this.settings.callback.beforedelete.call(null,this.selected.get(0), _this)) return this.error("DELETE: STOPPED BY USER");
  1770. $parent = this.selected.parent();
  1771. var obj = this.selected;
  1772. if(this.settings.rules.multiple == false || this.selected_arr.length == 1) {
  1773. var stop = true;
  1774. var tmp = (this.selected.prev("li:eq(0)").size()) ? this.selected.prev("li:eq(0)") : this.selected.parents("li:eq(0)");
  1775. // this.get_prev(true);
  1776. }
  1777. obj = obj.remove();
  1778. $parent.children("li:last").addClass("last");
  1779. if($parent.children("li").size() == 0) {
  1780. $li = $parent.parents("li:eq(0)");
  1781. $li.removeClass("open").removeClass("closed").addClass("leaf").children("ul").remove();
  1782. this.set_cookie("open");
  1783. }
  1784. //this.selected = false;
  1785. this.settings.callback.ondelete.call(null, obj.get(0), this, rb);
  1786. if(stop && tmp) this.select_branch(tmp);
  1787. if(this.settings.rules.multiple != false && !stop) {
  1788. var _this = this;
  1789. this.selected_arr = [];
  1790. this.container.find("a.clicked").filter(":first-child").parent().each(function () {
  1791. _this.selected_arr.push($(this));
  1792. });
  1793. if(this.selected_arr.length > 0) {
  1794. this.selected = this.selected_arr[0];
  1795. this.remove();
  1796. }
  1797. }
  1798. }
  1799. else return this.error("DELETE: NO NODE SELECTED");
  1800. },
  1801. next : function (obj, strict) {
  1802. obj = this.get_node(obj);
  1803. if(!obj.size()) return false;
  1804. if(strict) return (obj.nextAll("li").size() > 0) ? obj.nextAll("li:eq(0)") : false;
  1805. if(obj.hasClass("open")) return obj.find("li:eq(0)");
  1806. else if(obj.nextAll("li").size() > 0) return obj.nextAll("li:eq(0)");
  1807. else return obj.parents("li").next("li").eq(0);
  1808. },
  1809. prev : function(obj, strict) {
  1810. obj = this.get_node(obj);
  1811. if(!obj.size()) return false;
  1812. if(strict) return (obj.prevAll("li").size() > 0) ? obj.prevAll("li:eq(0)") : false;
  1813. if(obj.prev("li").size()) {
  1814. var obj = obj.prev("li").eq(0);
  1815. while(obj.hasClass("open")) obj = obj.children("ul:eq(0)").children("li:last");
  1816. return obj;
  1817. }
  1818. else return obj.parents("li:eq(0)").size() ? obj.parents("li:eq(0)") : false;
  1819. },
  1820. parent : function(obj) {
  1821. obj = this.get_node(obj);
  1822. if(!obj.size()) return false;
  1823. return obj.parents("li:eq(0)").size() ? obj.parents("li:eq(0)") : false;
  1824. },
  1825. children : function(obj) {
  1826. obj = this.get_node(obj);
  1827. if(!obj.size()) return false;
  1828. return obj.children("ul:eq(0)").children("li");
  1829. },
  1830. // FOR EXPLORER-LIKE KEYBOARD SHORTCUTS
  1831. get_next : function(force) {
  1832. var obj = this.hovered || this.selected;
  1833. return force ? this.select_branch(this.next(obj)) : this.hover_branch(this.next(obj));
  1834. },
  1835. get_prev : function(force) {
  1836. var obj = this.hovered || this.selected;
  1837. return force ? this.select_branch(this.prev(obj)) : this.hover_branch(this.prev(obj));
  1838. },
  1839. get_left : function(force, rtl) {
  1840. if(this.settings.ui.rtl && !rtl) return this.get_right(force, true);
  1841. var obj = this.hovered || this.selected;
  1842. if(obj) {
  1843. if(obj.hasClass("open")) this.close_branch(obj);
  1844. else {
  1845. return force ? this.select_branch(this.parent(obj)) : this.hover_branch(this.parent(obj));
  1846. }
  1847. }
  1848. },
  1849. get_right : function(force, rtl) {
  1850. if(this.settings.ui.rtl && !rtl) return this.get_left(force, true);
  1851. var obj = this.hovered || this.selected;
  1852. if(obj) {
  1853. if(obj.hasClass("closed")) this.open_branch(obj);
  1854. else {
  1855. return force ? this.select_branch(obj.find("li:eq(0)")) : this.hover_branch(obj.find("li:eq(0)"));
  1856. }
  1857. }
  1858. },
  1859. toggleDots : function () {
  1860. if(this.settings.ui.dots) {
  1861. this.settings.ui.dots = false;
  1862. this.container.children("ul:eq(0)").addClass("no_dots");
  1863. }
  1864. else {
  1865. this.settings.ui.dots = true;
  1866. this.container.children("ul:eq(0)").removeClass("no_dots");
  1867. }
  1868. },
  1869. toggleRTL : function () {
  1870. if(this.settings.ui.rtl) {
  1871. this.settings.ui.rtl = false;
  1872. this.container.css("direction","ltr").children("ul:eq(0)").removeClass("rtl").addClass("ltr");
  1873. }
  1874. else {
  1875. this.settings.ui.rtl = true;
  1876. this.container.css("direction","rtl").children("ul:eq(0)").removeClass("ltr").addClass("rtl");
  1877. }
  1878. },
  1879. set_cookie : function (type) {
  1880. if(this.settings.cookies === false) return false;
  1881. if(this.settings.cookies[type] === false) return false;
  1882. switch(type) {
  1883. case "selected":
  1884. if(this.settings.rules.multiple != false && this.selected_arr.length > 1) {
  1885. var val = Array();
  1886. $.each(this.selected_arr, function () {
  1887. if(this.attr("id")) { val.push(this.attr("id")); }
  1888. });
  1889. val = val.join(",");
  1890. }
  1891. else var val = this.selected ? this.selected.attr("id") : false;
  1892. $.cookie(this.settings.cookies.prefix + '_selected',val,this.settings.cookies.opts);
  1893. break;
  1894. case "open":
  1895. var str = "";
  1896. this.container.find("li.open").each(function (i) { if(this.id) { str += this.id + ","; } });
  1897. $.cookie(this.settings.cookies.prefix + '_open',str.replace(/,$/ig,""),this.settings.cookies.opts);
  1898. break;
  1899. }
  1900. },
  1901. get_rollback : function () {
  1902. var rb = {};
  1903. if(this.context.to_remove && this.context.apply_to) this.context.apply_to.children("a").removeClass("clicked");
  1904. rb.html = this.container.html();
  1905. if(this.context.to_remove && this.context.apply_to) this.context.apply_to.children("a").addClass("clicked");
  1906. rb.selected = this.selected ? this.selected.attr("id") : false;
  1907. return rb;
  1908. },
  1909. moved : function (what, where, how, is_new, is_copy, rb) {
  1910. var what = $(what);
  1911. var $parent = $(what).parents("ul:eq(0)");
  1912. var $where = $(where);
  1913. // Rollback
  1914. if(!rb) {
  1915. var rb = {};
  1916. rb[this.container.attr("id")] = this.get_rollback();
  1917. if(!is_new) {
  1918. var tmp = what.size() > 1 ? what.eq(0).parents(".tree:eq(0)") : what.parents(".tree:eq(0)");
  1919. if(tmp.get(0) != this.container.get(0)) {
  1920. tmp = tree_component.inst[tmp.attr("id")];
  1921. rb[tmp.container.attr("id")] = tmp.get_rollback();
  1922. }
  1923. delete tmp;
  1924. }
  1925. }
  1926. if(how == "inside" && this.settings.data.async && this.get_node($where).hasClass("closed")) {
  1927. var _this = this;
  1928. return this.open_branch(this.get_node($where), true, function () { _this.moved.apply(_this, [what, where, how, is_new, is_copy, rb]); });
  1929. }
  1930. // IF MULTIPLE
  1931. if(what.size() > 1) {
  1932. var _this = this;
  1933. var tmp = this.moved(what.eq(0), where, how, false, is_copy, rb);
  1934. what.each(function (i) {
  1935. if(i == 0) return;
  1936. if(tmp) { // if tmp is false - the previous move was a no-go
  1937. tmp = _this.moved(this, tmp.children("a:eq(0)"), "after", false, is_copy, rb);
  1938. }
  1939. });
  1940. return;
  1941. }
  1942. if(is_copy) {
  1943. _what = what.clone();
  1944. _what.each(function (i) {
  1945. this.id = this.id + "_copy";
  1946. $(this).find("li").each(function () {
  1947. this.id = this.id + "_copy";
  1948. });
  1949. $(this).removeClass("dragged").find("a.clicked").removeClass("clicked ui-state-active").end().find("li.dragged").removeClass("dragged");
  1950. });
  1951. }
  1952. else _what = what;
  1953. if(is_new) {
  1954. if(!this.settings.callback.beforecreate.call(null,this.get_node(what).get(0), this.get_node(where).get(0),how,this)) return false;
  1955. }
  1956. else {
  1957. if(!this.settings.callback.beforemove.call(null,this.get_node(what).get(0), this.get_node(where).get(0),how,this)) return false;
  1958. }
  1959. if(!is_new) {
  1960. var tmp = what.parents(".tree:eq(0)");
  1961. // if different trees
  1962. if(tmp.get(0) != this.container.get(0)) {
  1963. tmp = tree_component.inst[tmp.attr("id")];
  1964. // if there are languages - otherwise - no cleanup needed
  1965. if(tmp.settings.languages.length) {
  1966. var res = [];
  1967. // if new tree has no languages - use current visible
  1968. if(this.settings.languages.length == 0) res.push("." + tmp.current_lang);
  1969. else {
  1970. for(var i in this.settings.languages) {
  1971. if(typeof this.settings.languages[i] == "function") continue;
  1972. for(var j in tmp.settings.languages) {
  1973. if(typeof tmp.settings.languages[j] == "function") continue;
  1974. if(this.settings.languages[i] == tmp.settings.languages[j]) res.push("." + this.settings.languages[i]);
  1975. }
  1976. }
  1977. }
  1978. if(res.length == 0) return this.error("MOVE: NO COMMON LANGUAGES");
  1979. what.find("a").not(res.join(",")).remove();
  1980. }
  1981. what.find("a.clicked").removeClass("clicked ui-state-active");
  1982. }
  1983. }
  1984. what = _what;
  1985. // ADD NODE TO NEW PLACE
  1986. switch(how) {
  1987. case "before":
  1988. $where.parents("ul:eq(0)").children("li.last").removeClass("last");
  1989. $where.parent().before(what.removeClass("last"));
  1990. $where.parents("ul:eq(0)").children("li:last").addClass("last");
  1991. break;
  1992. case "after":
  1993. $where.parents("ul:eq(0)").children("li.last").removeClass("last");
  1994. $where.parent().after(what.removeClass("last"));
  1995. $where.parents("ul:eq(0)").children("li:last").addClass("last");
  1996. break;
  1997. case "inside":
  1998. if($where.parent().children("ul:first").size()) {
  1999. if(this.settings.rules.createat == "top") $where.parent().children("ul:first").prepend(what.removeClass("last")).children("li:last").addClass("last");
  2000. else $where.parent().children("ul:first").children(".last").removeClass("last").end().append(what.removeClass("last")).children("li:last").addClass("last");
  2001. }
  2002. else {
  2003. what.addClass("last");
  2004. $where.parent().append("<ul/>").removeClass("leaf").addClass("closed");
  2005. $where.parent().children("ul:first").prepend(what);
  2006. }
  2007. if($where.parent().hasClass("closed")) { this.open_branch($where); }
  2008. break;
  2009. default:
  2010. break;
  2011. }
  2012. // CLEANUP OLD PARENT
  2013. if($parent.find("li").size() == 0) {
  2014. var $li = $parent.parent();
  2015. $li.removeClass("open").removeClass("closed").addClass("leaf");
  2016. if(!$li.is(".tree")) $li.children("ul").remove();
  2017. $li.parents("ul:eq(0)").children("li.last").removeClass("last").end().children("li:last").addClass("last");
  2018. this.set_cookie("open");
  2019. }
  2020. else {
  2021. $parent.children("li.last").removeClass("last");
  2022. $parent.children("li:last").addClass("last");
  2023. }
  2024. // NO LONGER CORRECT WITH position PARAM - if(is_new && how != "inside") where = this.get_node(where).parents("li:eq(0)");
  2025. if(is_copy) this.settings.callback.oncopy.call(null, this.get_node(what).get(0), this.get_node(where).get(0), how, this, rb);
  2026. else if(is_new) this.settings.callback.oncreate.call(null, this.get_node(what).get(0), ($where.is("ul") ? -1 : this.get_node(where).get(0) ), how, this, rb);
  2027. else this.settings.callback.onmove.call(null, this.get_node(what).get(0), this.get_node(where).get(0), how, this, rb);
  2028. return what;
  2029. },
  2030. error : function (code) {
  2031. this.settings.callback.error.call(null,code,this);
  2032. return false;
  2033. },
  2034. lock : function (state) {
  2035. this.locked = state;
  2036. if(this.locked) this.container.children("ul:eq(0)").addClass("locked");
  2037. else this.container.children("ul:eq(0)").removeClass("locked");
  2038. },
  2039. cut : function (obj) {
  2040. if(this.locked) return this.error("LOCKED");
  2041. obj = obj ? this.get_node(obj) : this.container.find("a.clicked").filter(":first-child").parent();
  2042. if(!obj || !obj.size()) return this.error("CUT: NO NODE SELECTED");
  2043. this.copy_nodes = false;
  2044. this.cut_nodes = obj;
  2045. },
  2046. copy : function (obj) {
  2047. if(this.locked) return this.error("LOCKED");
  2048. obj = obj ? this.get_node(obj) : this.container.find("a.clicked").filter(":first-child").parent();
  2049. if(!obj || !obj.size()) return this.error("COPY: NO NODE SELECTED");
  2050. this.copy_nodes = obj;
  2051. this.cut_nodes = false;
  2052. },
  2053. paste : function (obj, position) {
  2054. if(this.locked) return this.error("LOCKED");
  2055. var root = false;
  2056. if(obj == -1) { root = true; obj = this.container; }
  2057. else obj = obj ? this.get_node(obj) : this.selected;
  2058. if(!root && (!obj || !obj.size())) return this.error("PASTE: NO NODE SELECTED");
  2059. if(!this.copy_nodes && !this.cut_nodes) return this.error("PASTE: NOTHING TO DO");
  2060. var _this = this;
  2061. var pos = position;
  2062. if(position == "before") {
  2063. position = obj.parent().children().index(obj);
  2064. obj = obj.parents("li:eq(0)");
  2065. }
  2066. else if(position == "after") {
  2067. position = obj.parent().children().index(obj) + 1;
  2068. obj = obj.parents("li:eq(0)");
  2069. }
  2070. else if((typeof position).toLowerCase() == "undefined" || position == "inside") {
  2071. position = (this.settings.rules.createat == "top") ? 0 : obj.children("ul:eq(0)").children("li").size();
  2072. }
  2073. if(!root && obj.size() == 0) { root = true; obj = this.container; }
  2074. if(this.copy_nodes && this.copy_nodes.size()) {
  2075. var ok = true;
  2076. // This is copy - why forbid this?
  2077. //obj.parents().andSelf().each(function () {
  2078. // if(_this.copy_nodes.index(this) != -1) {
  2079. // ok = false;
  2080. // return false;
  2081. // }
  2082. //});
  2083. if(!ok) return this.error("Invalid paste");
  2084. if(!root && !this.checkMove(this.copy_nodes, obj.children("a:eq(0)"), "inside")) return false;
  2085. if(obj.children("ul").size() == 0 || (root == true && obj.children("ul").children("li").size() == 0) ) {
  2086. if(!root) var a = this.moved(this.copy_nodes,obj.children("a:eq(0)"),"inside", false, true);
  2087. else var a = this.moved(this.copy_nodes,this.container.children("ul:eq(0)"),"inside", false, true);
  2088. }
  2089. else if(pos == "before" && obj.children("ul:eq(0)").children("li:nth-child(" + (position + 1) + ")").size())
  2090. var a = this.moved(this.copy_nodes,obj.children("ul:eq(0)").children("li:nth-child(" + (position + 1) + ")").children("a:eq(0)"),"before", false, true);
  2091. else if(pos == "after" && obj.children("ul:eq(0)").children("li:nth-child(" + (position) + ")").size())
  2092. var a = this.moved(this.copy_nodes,obj.children("ul:eq(0)").children("li:nth-child(" + (position) + ")").children("a:eq(0)"),"after", false, true);
  2093. else if(obj.children("ul:eq(0)").children("li:nth-child(" + (position + 1) + ")").size())
  2094. var a = this.moved(this.copy_nodes,obj.children("ul:eq(0)").children("li:nth-child(" + (position + 1) + ")").children("a:eq(0)"),"before", false, true);
  2095. else
  2096. var a = this.moved(this.copy_nodes,obj.children("ul:eq(0)").children("li:last").children("a:eq(0)"),"after", false, true);
  2097. this.copy_nodes = false;
  2098. }
  2099. if(this.cut_nodes && this.cut_nodes.size()) {
  2100. var ok = true;
  2101. obj.parents().andSelf().each(function () {
  2102. if(_this.cut_nodes.index(this) != -1) {
  2103. ok = false;
  2104. return false;
  2105. }
  2106. });
  2107. if(!ok) return this.error("Invalid paste");
  2108. if(!root && !this.checkMove(this.cut_nodes, obj.children("a:eq(0)"), "inside")) return false;
  2109. if(obj.children("ul").size() == 0 || (root == true && obj.children("ul").children("li").size() == 0) ) {
  2110. if(!root) var a = this.moved(this.cut_nodes,obj.children("a:eq(0)"),"inside");
  2111. else var a = this.moved(this.cut_nodes,this.container.children("ul:eq(0)"),"inside");
  2112. }
  2113. else if(pos == "before" && obj.children("ul:eq(0)").children("li:nth-child(" + (position + 1) + ")").size())
  2114. var a = this.moved(this.cut_nodes,obj.children("ul:eq(0)").children("li:nth-child(" + (position + 1) + ")").children("a:eq(0)"),"before");
  2115. else if(pos == "after" && obj.children("ul:eq(0)").children("li:nth-child(" + (position) + ")").size())
  2116. var a = this.moved(this.cut_nodes,obj.children("ul:eq(0)").children("li:nth-child(" + (position) + ")").children("a:eq(0)"),"after");
  2117. else if(obj.children("ul:eq(0)").children("li:nth-child(" + (position + 1) + ")").size())
  2118. var a = this.moved(this.cut_nodes,obj.children("ul:eq(0)").children("li:nth-child(" + (position + 1) + ")").children("a:eq(0)"),"before");
  2119. else
  2120. var a = this.moved(this.cut_nodes,obj.children("ul:eq(0)").children("li:last").children("a:eq(0)"),"after");
  2121. this.cut_nodes = false;
  2122. }
  2123. },
  2124. search : function(str) {
  2125. var _this = this;
  2126. if(!str || (this.srch && str != this.srch) ) {
  2127. this.srch = "";
  2128. this.srch_opn = false;
  2129. this.container.find("a.search").removeClass("search ui-state-highlight");
  2130. }
  2131. this.srch = str;
  2132. if(!str) return;
  2133. if(this.settings.data.async) {
  2134. if(!this.srch_opn) {
  2135. var dd = $.extend( { "search" : str } , this.settings.data.async_data(false, this) );
  2136. $.ajax({
  2137. type : this.settings.data.method,
  2138. url : this.settings.data.url,
  2139. data : dd,
  2140. dataType : "text",
  2141. success : function (data) {
  2142. _this.srch_opn = $.unique(data.split(","));
  2143. _this.search.apply(_this,[str]);
  2144. }
  2145. });
  2146. }
  2147. else if(this.srch_opn.length) {
  2148. if(this.srch_opn && this.srch_opn.length) {
  2149. var opn = false;
  2150. for(var j = 0; j < this.srch_opn.length; j++) {
  2151. if(this.get_node("#" + this.srch_opn[j]).size() > 0) {
  2152. opn = true;
  2153. var tmp = "#" + this.srch_opn[j];
  2154. delete this.srch_opn[j];
  2155. this.open_branch(tmp, true, function () { _this.search.apply(_this,[str]); } );
  2156. }
  2157. }
  2158. if(!opn) {
  2159. this.srch_opn = [];
  2160. _this.search.apply(_this,[str]);
  2161. }
  2162. }
  2163. }
  2164. else {
  2165. var selector = "a";
  2166. // IF LANGUAGE VERSIONS
  2167. if(this.settings.languages.length) selector += "." + this.current_lang;
  2168. this.container.find(selector + ":contains('" + str + "')").addClass( this.settings.ui.theme_name == "themeroller" ? "search ui-state-highlight" : "search");
  2169. this.srch_opn = false;
  2170. }
  2171. }
  2172. else {
  2173. var selector = "a";
  2174. // IF LANGUAGE VERSIONS
  2175. if(this.settings.languages.length) selector += "." + this.current_lang;
  2176. this.container.find(selector + ":contains('" + str + "')").addClass( this.settings.ui.theme_name == "themeroller" ? "search ui-state-highlight" : "search").parents("li.closed").each( function () { _this.open_branch(this, true); });
  2177. }
  2178. },
  2179. destroy : function() {
  2180. this.hide_context();
  2181. this.container.unbind(".jstree");
  2182. $("#" + this.container.attr("id")).die("click.jstree").die("dblclick.jstree").die("contextmenu.jstree").die("mouseover.jstree").die("mouseout.jstree").die("mousedown.jstree");
  2183. this.container.removeClass("tree ui-widget ui-widget-content tree-default tree-" + this.settings.ui.theme_name).children("ul").removeClass("no_dots rtl ltr locked").find("li").removeClass("leaf").removeClass("open").removeClass("closed").removeClass("last").children("a").removeClass("clicked hover search ui-state-active ui-state-hover ui-state-highlight ui-state-default");
  2184. if(this.cntr == tree_component.focused) {
  2185. for(var i in tree_component.inst) {
  2186. if(i != this.cntr && i != this.container.attr("id")) {
  2187. tree_component.inst[i].focus();
  2188. break;
  2189. }
  2190. }
  2191. }
  2192. tree_component.inst[this.cntr] = false;
  2193. tree_component.inst[this.container.attr("id")] = false;
  2194. delete tree_component.inst[this.cntr];
  2195. delete tree_component.inst[this.container.attr("id")];
  2196. tree_component.cntr --;
  2197. }
  2198. }
  2199. };
  2200. })(jQuery);