jquery.dynatree.js 98 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278
  1. /*************************************************************************
  2. jquery.dynatree.js
  3. Dynamic tree view control, with support for lazy loading of branches.
  4. Copyright (c) 2008-2011, Martin Wendt (http://wwWendt.de)
  5. Dual licensed under the MIT or GPL Version 2 licenses.
  6. http://code.google.com/p/dynatree/wiki/LicenseInfo
  7. A current version and some documentation is available at
  8. http://dynatree.googlecode.com/
  9. $Version: 1.2.0$
  10. $Revision: 528, 2011-09-17 18:58:59$
  11. @depends: jquery.js
  12. @depends: jquery.ui.core.js
  13. @depends: jquery.cookie.js
  14. *************************************************************************/
  15. // Note: We currently allow eval() to parse the 'data' attribtes, when initializing from HTML.
  16. /*jslint laxbreak: true, browser: true, evil: true, indent: 0, white: false, onevar: false */
  17. /*************************************************************************
  18. * Debug functions
  19. */
  20. var _canLog = true;
  21. function _log(mode, msg) {
  22. /**
  23. * Usage: logMsg("%o was toggled", this);
  24. */
  25. if( !_canLog ){
  26. return;
  27. }
  28. // Remove first argument
  29. var args = Array.prototype.slice.apply(arguments, [1]);
  30. // Prepend timestamp
  31. var dt = new Date();
  32. var tag = dt.getHours()+":"+dt.getMinutes()+":"+dt.getSeconds()+"."+dt.getMilliseconds();
  33. args[0] = tag + " - " + args[0];
  34. try {
  35. switch( mode ) {
  36. case "info":
  37. window.console.info.apply(window.console, args);
  38. break;
  39. case "warn":
  40. window.console.warn.apply(window.console, args);
  41. break;
  42. default:
  43. //window.console.log.apply(window.console, args);
  44. break;
  45. }
  46. } catch(e) {
  47. if( !window.console ){
  48. _canLog = false; // Permanently disable, when logging is not supported by the browser
  49. }
  50. }
  51. }
  52. function logMsg(msg) {
  53. Array.prototype.unshift.apply(arguments, ["debug"]);
  54. _log.apply(this, arguments);
  55. }
  56. // Forward declaration
  57. var getDynaTreePersistData = null;
  58. /*************************************************************************
  59. * Constants
  60. */
  61. var DTNodeStatus_Error = -1;
  62. var DTNodeStatus_Loading = 1;
  63. var DTNodeStatus_Ok = 0;
  64. // Start of local namespace
  65. (function($) {
  66. /*************************************************************************
  67. * Common tool functions.
  68. */
  69. var Class = {
  70. create: function() {
  71. return function() {
  72. this.initialize.apply(this, arguments);
  73. };
  74. }
  75. };
  76. // Tool function to get dtnode from the event target:
  77. function getDtNodeFromElement(el) {
  78. var iMax = 5;
  79. while( el && iMax-- ) {
  80. if(el.dtnode) { return el.dtnode; }
  81. el = el.parentNode;
  82. }
  83. return null;
  84. }
  85. function noop() {
  86. }
  87. /*************************************************************************
  88. * Class DynaTreeNode
  89. */
  90. var DynaTreeNode = Class.create();
  91. DynaTreeNode.prototype = {
  92. initialize: function(parent, tree, data) {
  93. /**
  94. * @constructor
  95. */
  96. this.parent = parent;
  97. this.tree = tree;
  98. if ( typeof data === "string" ){
  99. data = { title: data };
  100. }
  101. if( data.key === undefined ){
  102. data.key = "_" + tree._nodeCount++;
  103. }
  104. this.data = $.extend({}, $.ui.dynatree.nodedatadefaults, data);
  105. this.li = null; // not yet created
  106. this.span = null; // not yet created
  107. this.ul = null; // not yet created
  108. this.childList = null; // no subnodes yet
  109. this.isLoading = false; // Lazy content is being loaded
  110. this.hasSubSel = false;
  111. this.bExpanded = false;
  112. this.bSelected = false;
  113. },
  114. toString: function() {
  115. return "DynaTreeNode<" + this.data.key + ">: '" + this.data.title + "'";
  116. },
  117. toDict: function(recursive, callback) {
  118. var dict = $.extend({}, this.data);
  119. dict.activate = ( this.tree.activeNode === this );
  120. dict.focus = ( this.tree.focusNode === this );
  121. dict.expand = this.bExpanded;
  122. dict.select = this.bSelected;
  123. if( callback ){
  124. callback(dict);
  125. }
  126. if( recursive && this.childList ) {
  127. dict.children = [];
  128. for(var i=0, l=this.childList.length; i<l; i++ ){
  129. dict.children.push(this.childList[i].toDict(true, callback));
  130. }
  131. } else {
  132. delete dict.children;
  133. }
  134. return dict;
  135. },
  136. fromDict: function(dict) {
  137. /**
  138. * Update node data. If dict contains 'children', then also replace
  139. * the hole sub tree.
  140. */
  141. var children = dict.children;
  142. if(children === undefined){
  143. this.data = $.extend(this.data, dict);
  144. this.render();
  145. return;
  146. }
  147. dict = $.extend({}, dict);
  148. dict.children = undefined;
  149. this.data = $.extend(this.data, dict);
  150. this.removeChildren();
  151. this.addChild(children);
  152. },
  153. _getInnerHtml: function() {
  154. var tree = this.tree,
  155. opts = tree.options,
  156. cache = tree.cache,
  157. level = this.getLevel(),
  158. data = this.data,
  159. res = "";
  160. // connector (expanded, expandable or simple)
  161. if( level < opts.minExpandLevel ) {
  162. if(level > 1){
  163. res += cache.tagConnector;
  164. }
  165. // .. else (i.e. for root level) skip expander/connector altogether
  166. } else if( this.hasChildren() !== false ) {
  167. res += cache.tagExpander;
  168. } else {
  169. res += cache.tagConnector;
  170. }
  171. // Checkbox mode
  172. if( opts.checkbox && data.hideCheckbox !== true && !data.isStatusNode ) {
  173. res += cache.tagCheckbox;
  174. }
  175. // folder or doctype icon
  176. if ( data.icon ) {
  177. res += "<img src='" + opts.imagePath + data.icon + "' alt='' />";
  178. } else if ( data.icon === false ) {
  179. // icon == false means 'no icon'
  180. noop(); // keep JSLint happy
  181. } else {
  182. // icon == null means 'default icon'
  183. res += cache.tagNodeIcon;
  184. }
  185. // node title
  186. var nodeTitle = "";
  187. if ( opts.onCustomRender ){
  188. nodeTitle = opts.onCustomRender.call(tree, this) || "";
  189. }
  190. if(!nodeTitle){
  191. var tooltip = data.tooltip ? " title='" + data.tooltip + "'" : "";
  192. if( opts.noLink || data.noLink ) {
  193. nodeTitle = "<span style='display: inline-block;' class='" + opts.classNames.title + "'" + tooltip + ">" + data.title + "</span>";
  194. // this.tree.logDebug("nodeTitle: " + nodeTitle);
  195. }else{
  196. //nodeTitle = "<a href='#' class='" + opts.classNames.title + "'" + tooltip + ">" + data.title + "</a>";
  197. nodeTitle = "<a href=" + data.href + " id='" + data.key + "' class='" + opts.classNames.title + "'" + tooltip + ">" + data.title + "</a>";
  198. }
  199. }
  200. res += nodeTitle;
  201. return res;
  202. },
  203. _fixOrder: function() {
  204. /**
  205. * Make sure, that <li> order matches childList order.
  206. */
  207. var cl = this.childList;
  208. if( !cl || !this.ul ){
  209. return;
  210. }
  211. var childLI = this.ul.firstChild;
  212. for(var i=0, l=cl.length-1; i<l; i++) {
  213. var childNode1 = cl[i];
  214. var childNode2 = childLI.dtnode;
  215. if( childNode1 !== childNode2 ) {
  216. this.tree.logDebug("_fixOrder: mismatch at index " + i + ": " + childNode1 + " != " + childNode2);
  217. this.ul.insertBefore(childNode1.li, childNode2.li);
  218. } else {
  219. childLI = childLI.nextSibling;
  220. }
  221. }
  222. },
  223. render: function(useEffects, includeInvisible) {
  224. /**
  225. * Create <li><span>..</span> .. </li> tags for this node.
  226. *
  227. * <li id='KEY' dtnode=NODE> // This div contains the node's span and list of child div's.
  228. * <span class='title'>S S S A</span> // Span contains graphic spans and title <a> tag
  229. * <ul> // only present, when node has children
  230. * <li id='KEY' dtnode=NODE>child1</li>
  231. * <li id='KEY' dtnode=NODE>child2</li>
  232. * </ul>
  233. * </li>
  234. */
  235. //this.tree.logDebug("%s.render(%s)", this, useEffects);
  236. // ---
  237. var tree = this.tree,
  238. parent = this.parent,
  239. data = this.data,
  240. opts = tree.options,
  241. cn = opts.classNames,
  242. isLastSib = this.isLastSibling(),
  243. firstTime = false;
  244. if( !parent && !this.ul ) {
  245. // Root node has only a <ul>
  246. this.li = this.span = null;
  247. this.ul = document.createElement("ul");
  248. if( opts.minExpandLevel > 1 ){
  249. this.ul.className = cn.container + " " + cn.noConnector;
  250. }else{
  251. this.ul.className = cn.container;
  252. }
  253. } else if( parent ) {
  254. // Create <li><span /> </li>
  255. if( ! this.li ) {
  256. firstTime = true;
  257. this.li = document.createElement("li");
  258. this.li.dtnode = this;
  259. if( data.key && opts.generateIds ){
  260. this.li.id = opts.idPrefix + data.key;
  261. }
  262. this.span = document.createElement("span");
  263. this.span.className = cn.title;
  264. this.li.appendChild(this.span);
  265. if( !parent.ul ) {
  266. // This is the parent's first child: create UL tag
  267. // (Hidden, because it will be
  268. parent.ul = document.createElement("ul");
  269. parent.ul.style.display = "none";
  270. parent.li.appendChild(parent.ul);
  271. // if( opts.minExpandLevel > this.getLevel() ){
  272. // parent.ul.className = cn.noConnector;
  273. // }
  274. }
  275. // set node connector images, links and text
  276. // this.span.innerHTML = this._getInnerHtml();
  277. parent.ul.appendChild(this.li);
  278. }
  279. // set node connector images, links and text
  280. this.span.innerHTML = this._getInnerHtml();
  281. // Set classes for current status
  282. var cnList = [];
  283. cnList.push(cn.node);
  284. if( data.isFolder ){
  285. cnList.push(cn.folder);
  286. }
  287. if( this.bExpanded ){
  288. cnList.push(cn.expanded);
  289. }
  290. if( this.hasChildren() !== false ){
  291. cnList.push(cn.hasChildren);
  292. }
  293. if( data.isLazy && this.childList === null ){
  294. cnList.push(cn.lazy);
  295. }
  296. if( isLastSib ){
  297. cnList.push(cn.lastsib);
  298. }
  299. if( this.bSelected ){
  300. cnList.push(cn.selected);
  301. }
  302. if( this.hasSubSel ){
  303. cnList.push(cn.partsel);
  304. }
  305. if( tree.activeNode === this ){
  306. cnList.push(cn.active);
  307. }
  308. if( data.addClass ){
  309. cnList.push(data.addClass);
  310. }
  311. // IE6 doesn't correctly evaluate multiple class names,
  312. // so we create combined class names that can be used in the CSS
  313. cnList.push(cn.combinedExpanderPrefix
  314. + (this.bExpanded ? "e" : "c")
  315. + (data.isLazy && this.childList === null ? "d" : "")
  316. + (isLastSib ? "l" : "")
  317. );
  318. cnList.push(cn.combinedIconPrefix
  319. + (this.bExpanded ? "e" : "c")
  320. + (data.isFolder ? "f" : "")
  321. );
  322. this.span.className = cnList.join(" ");
  323. // TODO: we should not set this in the <span> tag also, if we set it here:
  324. this.li.className = isLastSib ? cn.lastsib : "";
  325. // Allow tweaking, binding, after node was created for the first time
  326. if(firstTime && opts.onCreate){
  327. opts.onCreate.call(tree, this, this.span);
  328. }
  329. // Hide children, if node is collapsed
  330. // this.ul.style.display = ( this.bExpanded || !parent ) ? "" : "none";
  331. // Allow tweaking after node state was rendered
  332. if(opts.onRender){
  333. opts.onRender.call(tree, this, this.span);
  334. }
  335. }
  336. // Visit child nodes
  337. if( (this.bExpanded || includeInvisible === true) && this.childList ) {
  338. for(var i=0, l=this.childList.length; i<l; i++) {
  339. this.childList[i].render(false, includeInvisible);
  340. }
  341. // Make sure the tag order matches the child array
  342. this._fixOrder();
  343. }
  344. // Hide children, if node is collapsed
  345. if( this.ul ) {
  346. var isHidden = (this.ul.style.display === "none");
  347. var isExpanded = !!this.bExpanded;
  348. // logMsg("isHidden:%s", isHidden);
  349. if( useEffects && opts.fx && (isHidden === isExpanded) ) {
  350. var duration = opts.fx.duration || 200;
  351. $(this.ul).animate(opts.fx, duration);
  352. } else {
  353. this.ul.style.display = ( this.bExpanded || !parent ) ? "" : "none";
  354. }
  355. }
  356. },
  357. /** Return '/id1/id2/id3'. */
  358. getKeyPath: function(excludeSelf) {
  359. var path = [];
  360. this.visitParents(function(node){
  361. if(node.parent){
  362. path.unshift(node.data.key);
  363. }
  364. }, !excludeSelf);
  365. return "/" + path.join(this.tree.options.keyPathSeparator);
  366. },
  367. getParent: function() {
  368. return this.parent;
  369. },
  370. getChildren: function() {
  371. if(this.hasChildren() === undefined){
  372. return undefined; // Lazy node: unloaded, currently loading, or load error
  373. }
  374. return this.childList;
  375. },
  376. /** Check if node has children (returns undefined, if not sure). */
  377. hasChildren: function() {
  378. if(this.data.isLazy){
  379. if(this.childList === null || this.childList === undefined){
  380. // Not yet loaded
  381. return undefined;
  382. }else if(this.childList.length === 0){
  383. // Loaded, but response was empty
  384. return false;
  385. }else if(this.childList.length === 1 && this.childList[0].isStatusNode()){
  386. // Currently loading or load error
  387. return undefined;
  388. }
  389. return true;
  390. }
  391. return !!this.childList;
  392. },
  393. isFirstSibling: function() {
  394. var p = this.parent;
  395. return !p || p.childList[0] === this;
  396. },
  397. isLastSibling: function() {
  398. var p = this.parent;
  399. return !p || p.childList[p.childList.length-1] === this;
  400. },
  401. getPrevSibling: function() {
  402. if( !this.parent ){
  403. return null;
  404. }
  405. var ac = this.parent.childList;
  406. for(var i=1, l=ac.length; i<l; i++){ // start with 1, so prev(first) = null
  407. if( ac[i] === this ){
  408. return ac[i-1];
  409. }
  410. }
  411. return null;
  412. },
  413. getNextSibling: function() {
  414. if( !this.parent ){
  415. return null;
  416. }
  417. var ac = this.parent.childList;
  418. for(var i=0, l=ac.length-1; i<l; i++){ // up to length-2, so next(last) = null
  419. if( ac[i] === this ){
  420. return ac[i+1];
  421. }
  422. }
  423. return null;
  424. },
  425. isStatusNode: function() {
  426. return (this.data.isStatusNode === true);
  427. },
  428. isChildOf: function(otherNode) {
  429. return (this.parent && this.parent === otherNode);
  430. },
  431. isDescendantOf: function(otherNode) {
  432. if(!otherNode){
  433. return false;
  434. }
  435. var p = this.parent;
  436. while( p ) {
  437. if( p === otherNode ){
  438. return true;
  439. }
  440. p = p.parent;
  441. }
  442. return false;
  443. },
  444. countChildren: function() {
  445. var cl = this.childList;
  446. if( !cl ){
  447. return 0;
  448. }
  449. var n = cl.length;
  450. for(var i=0, l=n; i<l; i++){
  451. var child = cl[i];
  452. n += child.countChildren();
  453. }
  454. return n;
  455. },
  456. /**Sort child list by title.
  457. * cmd: optional compare function.
  458. * deep: optional: pass true to sort all descendant nodes.
  459. */
  460. sortChildren: function(cmp, deep) {
  461. var cl = this.childList;
  462. if( !cl ){
  463. return;
  464. }
  465. cmp = cmp || function(a, b) {
  466. // return a.data.title === b.data.title ? 0 : a.data.title > b.data.title ? 1 : -1;
  467. var x = a.data.title.toLowerCase(),
  468. y = b.data.title.toLowerCase();
  469. return x === y ? 0 : x > y ? 1 : -1;
  470. };
  471. cl.sort(cmp);
  472. if( deep ){
  473. for(var i=0, l=cl.length; i<l; i++){
  474. if( cl[i].childList ){
  475. cl[i].sortChildren(cmp, "$norender$");
  476. }
  477. }
  478. }
  479. if( deep !== "$norender$" ){
  480. this.render();
  481. }
  482. },
  483. _setStatusNode: function(data) {
  484. // Create, modify or remove the status child node (pass 'null', to remove it).
  485. var firstChild = ( this.childList ? this.childList[0] : null );
  486. if( !data ) {
  487. if ( firstChild && firstChild.isStatusNode()) {
  488. try{
  489. // I've seen exceptions here with loadKeyPath...
  490. if(this.ul){
  491. this.ul.removeChild(firstChild.li);
  492. firstChild.li = null; // avoid leaks (issue 215)
  493. }
  494. }catch(e){}
  495. if( this.childList.length === 1 ){
  496. this.childList = [];
  497. }else{
  498. this.childList.shift();
  499. }
  500. }
  501. } else if ( firstChild ) {
  502. data.isStatusNode = true;
  503. data.key = "_statusNode";
  504. firstChild.data = data;
  505. firstChild.render();
  506. } else {
  507. data.isStatusNode = true;
  508. data.key = "_statusNode";
  509. firstChild = this.addChild(data);
  510. }
  511. },
  512. setLazyNodeStatus: function(lts, opts) {
  513. var tooltip = (opts && opts.tooltip) ? opts.tooltip : null;
  514. var info = (opts && opts.info) ? " (" + opts.info + ")" : "";
  515. switch( lts ) {
  516. case DTNodeStatus_Ok:
  517. this._setStatusNode(null);
  518. $(this.span).removeClass(this.tree.options.classNames.nodeLoading);
  519. this.isLoading = false;
  520. // this.render();
  521. if( this.tree.options.autoFocus ) {
  522. if( this === this.tree.tnRoot && this.childList && this.childList.length > 0) {
  523. // special case: using ajaxInit
  524. this.childList[0].focus();
  525. } else {
  526. this.focus();
  527. }
  528. }
  529. break;
  530. case DTNodeStatus_Loading:
  531. this.isLoading = true;
  532. $(this.span).addClass(this.tree.options.classNames.nodeLoading);
  533. // The root is hidden, so we set a temporary status child
  534. if(!this.parent){
  535. this._setStatusNode({
  536. title: this.tree.options.strings.loading + info,
  537. tooltip: tooltip,
  538. addClass: this.tree.options.classNames.nodeWait
  539. });
  540. }
  541. break;
  542. case DTNodeStatus_Error:
  543. this.isLoading = false;
  544. // $(this.span).addClass(this.tree.options.classNames.nodeError);
  545. this._setStatusNode({
  546. title: this.tree.options.strings.loadError + info,
  547. tooltip: tooltip,
  548. addClass: this.tree.options.classNames.nodeError
  549. });
  550. break;
  551. default:
  552. throw "Bad LazyNodeStatus: '" + lts + "'.";
  553. }
  554. },
  555. _parentList: function(includeRoot, includeSelf) {
  556. var l = [];
  557. var dtn = includeSelf ? this : this.parent;
  558. while( dtn ) {
  559. if( includeRoot || dtn.parent ){
  560. l.unshift(dtn);
  561. }
  562. dtn = dtn.parent;
  563. }
  564. return l;
  565. },
  566. getLevel: function() {
  567. /**
  568. * Return node depth. 0: System root node, 1: visible top-level node.
  569. */
  570. var level = 0;
  571. var dtn = this.parent;
  572. while( dtn ) {
  573. level++;
  574. dtn = dtn.parent;
  575. }
  576. return level;
  577. },
  578. _getTypeForOuterNodeEvent: function(event) {
  579. /** Return the inner node span (title, checkbox or expander) if
  580. * event.target points to the outer span.
  581. * This function should fix issue #93:
  582. * FF2 ignores empty spans, when generating events (returning the parent instead).
  583. */
  584. var cns = this.tree.options.classNames;
  585. var target = event.target;
  586. // Only process clicks on an outer node span (probably due to a FF2 event handling bug)
  587. if( target.className.indexOf(cns.node) < 0 ) {
  588. return null;
  589. }
  590. // Event coordinates, relative to outer node span:
  591. var eventX = event.pageX - target.offsetLeft;
  592. var eventY = event.pageY - target.offsetTop;
  593. for(var i=0, l=target.childNodes.length; i<l; i++) {
  594. var cn = target.childNodes[i];
  595. var x = cn.offsetLeft - target.offsetLeft;
  596. var y = cn.offsetTop - target.offsetTop;
  597. var nx = cn.clientWidth, ny = cn.clientHeight;
  598. // alert (cn.className + ": " + x + ", " + y + ", s:" + nx + ", " + ny);
  599. if( eventX >= x && eventX <= (x+nx) && eventY >= y && eventY <= (y+ny) ) {
  600. // alert("HIT "+ cn.className);
  601. if( cn.className==cns.title ){
  602. return "title";
  603. }else if( cn.className==cns.expander ){
  604. return "expander";
  605. }else if( cn.className==cns.checkbox ){
  606. return "checkbox";
  607. }else if( cn.className==cns.nodeIcon ){
  608. return "icon";
  609. }
  610. }
  611. }
  612. return "prefix";
  613. },
  614. getEventTargetType: function(event) {
  615. // Return the part of a node, that a click event occured on.
  616. // Note: there is no check, if the event was fired on TIHS node.
  617. var tcn = event && event.target ? event.target.className : "";
  618. var cns = this.tree.options.classNames;
  619. if( tcn === cns.title ){
  620. return "title";
  621. }else if( tcn === cns.expander ){
  622. return "expander";
  623. }else if( tcn === cns.checkbox ){
  624. return "checkbox";
  625. }else if( tcn === cns.nodeIcon ){
  626. return "icon";
  627. }else if( tcn === cns.empty || tcn === cns.vline || tcn === cns.connector ){
  628. return "prefix";
  629. }else if( tcn.indexOf(cns.node) >= 0 ){
  630. // FIX issue #93
  631. return this._getTypeForOuterNodeEvent(event);
  632. }
  633. return null;
  634. },
  635. isVisible: function() {
  636. // Return true, if all parents are expanded.
  637. var parents = this._parentList(true, false);
  638. for(var i=0, l=parents.length; i<l; i++){
  639. if( ! parents[i].bExpanded ){ return false; }
  640. }
  641. return true;
  642. },
  643. makeVisible: function() {
  644. // Make sure, all parents are expanded
  645. var parents = this._parentList(true, false);
  646. for(var i=0, l=parents.length; i<l; i++){
  647. parents[i]._expand(true);
  648. }
  649. },
  650. focus: function() {
  651. // TODO: check, if we already have focus
  652. // this.tree.logDebug("dtnode.focus(): %o", this);
  653. this.makeVisible();
  654. try {
  655. $(this.span).find(">a").focus();
  656. } catch(e) { }
  657. },
  658. isFocused: function() {
  659. return (this.tree.tnFocused === this);
  660. },
  661. _activate: function(flag, fireEvents) {
  662. // (De)Activate - but not focus - this node.
  663. this.tree.logDebug("dtnode._activate(%o, fireEvents=%o) - %o", flag, fireEvents, this);
  664. var opts = this.tree.options;
  665. if( this.data.isStatusNode ){
  666. return;
  667. }
  668. if ( fireEvents && opts.onQueryActivate && opts.onQueryActivate.call(this.tree, flag, this) === false ){
  669. return; // Callback returned false
  670. }
  671. if( flag ) {
  672. // Activate
  673. if( this.tree.activeNode ) {
  674. if( this.tree.activeNode === this ){
  675. return;
  676. }
  677. this.tree.activeNode.deactivate();
  678. }
  679. if( opts.activeVisible ){
  680. this.makeVisible();
  681. }
  682. this.tree.activeNode = this;
  683. if( opts.persist ){
  684. $.cookie(opts.cookieId+"-active", this.data.key, opts.cookie);
  685. }
  686. this.tree.persistence.activeKey = this.data.key;
  687. $(this.span).addClass(opts.classNames.active);
  688. if ( fireEvents && opts.onActivate ){
  689. opts.onActivate.call(this.tree, this);
  690. }
  691. } else {
  692. // Deactivate
  693. if( this.tree.activeNode === this ) {
  694. if ( opts.onQueryActivate && opts.onQueryActivate.call(this.tree, false, this) === false ){
  695. return; // Callback returned false
  696. }
  697. $(this.span).removeClass(opts.classNames.active);
  698. if( opts.persist ) {
  699. // Note: we don't pass null, but ''. So the cookie is not deleted.
  700. // If we pass null, we also have to pass a COPY of opts, because $cookie will override opts.expires (issue 84)
  701. $.cookie(opts.cookieId+"-active", "", opts.cookie);
  702. }
  703. this.tree.persistence.activeKey = null;
  704. this.tree.activeNode = null;
  705. if ( fireEvents && opts.onDeactivate ){
  706. opts.onDeactivate.call(this.tree, this);
  707. }
  708. }
  709. }
  710. },
  711. activate: function() {
  712. // Select - but not focus - this node.
  713. // this.tree.logDebug("dtnode.activate(): %o", this);
  714. this._activate(true, true);
  715. },
  716. activateSilently: function() {
  717. this._activate(true, false);
  718. },
  719. deactivate: function() {
  720. // this.tree.logDebug("dtnode.deactivate(): %o", this);
  721. this._activate(false, true);
  722. },
  723. isActive: function() {
  724. return (this.tree.activeNode === this);
  725. },
  726. _userActivate: function() {
  727. // Handle user click / [space] / [enter], according to clickFolderMode.
  728. var activate = true;
  729. var expand = false;
  730. if ( this.data.isFolder ) {
  731. switch( this.tree.options.clickFolderMode ) {
  732. case 2:
  733. activate = false;
  734. expand = true;
  735. break;
  736. case 3:
  737. activate = expand = true;
  738. break;
  739. }
  740. }
  741. if( this.parent === null ) {
  742. expand = false;
  743. }
  744. if( expand ) {
  745. this.toggleExpand();
  746. this.focus();
  747. }
  748. if( activate ) {
  749. this.activate();
  750. }
  751. },
  752. _setSubSel: function(hasSubSel) {
  753. if( hasSubSel ) {
  754. this.hasSubSel = true;
  755. $(this.span).addClass(this.tree.options.classNames.partsel);
  756. } else {
  757. this.hasSubSel = false;
  758. $(this.span).removeClass(this.tree.options.classNames.partsel);
  759. }
  760. },
  761. /**
  762. * Fix selection and partsel status, of parent nodes, according to current status of
  763. * end nodes.
  764. */
  765. _updatePartSelectionState: function() {
  766. // alert("_updatePartSelectionState " + this);
  767. // this.tree.logDebug("_updatePartSelectionState() - %o", this);
  768. var sel;
  769. // Return `true` or `false` for end nodes and remove part-sel flag
  770. if( ! this.hasChildren() ){
  771. sel = (this.bSelected && !this.data.unselectable && !this.data.isStatusNode);
  772. this._setSubSel(false);
  773. return sel;
  774. }
  775. // Return `true`, `false`, or `undefined` for parent nodes
  776. var i, l,
  777. cl = this.childList,
  778. allSelected = true,
  779. allDeselected = true;
  780. for(i=0, l=cl.length; i<l; i++) {
  781. var n = cl[i],
  782. s = n._updatePartSelectionState();
  783. if( s !== false){
  784. allDeselected = false;
  785. }
  786. if( s !== true){
  787. allSelected = false;
  788. }
  789. }
  790. if( allSelected ){
  791. sel = true;
  792. } else if ( allDeselected ){
  793. sel = false;
  794. } else {
  795. sel = undefined;
  796. }
  797. this._setSubSel(sel === undefined);
  798. this.bSelected = (sel === true);
  799. return sel;
  800. },
  801. /**
  802. * Fix selection status, after this node was (de)selected in multi-hier mode.
  803. * This includes (de)selecting all children.
  804. */
  805. _fixSelectionState: function() {
  806. // alert("_fixSelectionState " + this);
  807. // this.tree.logDebug("_fixSelectionState(%s) - %o", this.bSelected, this);
  808. var p, i, l;
  809. if( this.bSelected ) {
  810. // Select all children
  811. this.visit(function(node){
  812. node.parent._setSubSel(true);
  813. if(!node.data.unselectable){
  814. node._select(true, false, false);
  815. }
  816. });
  817. // Select parents, if all children are selected
  818. p = this.parent;
  819. while( p ) {
  820. p._setSubSel(true);
  821. var allChildsSelected = true;
  822. for(i=0, l=p.childList.length; i<l; i++) {
  823. var n = p.childList[i];
  824. if( !n.bSelected && !n.data.isStatusNode && !n.data.unselectable) {
  825. allChildsSelected = false;
  826. break;
  827. }
  828. }
  829. if( allChildsSelected ){
  830. p._select(true, false, false);
  831. }
  832. p = p.parent;
  833. }
  834. } else {
  835. // Deselect all children
  836. this._setSubSel(false);
  837. this.visit(function(node){
  838. node._setSubSel(false);
  839. node._select(false, false, false);
  840. });
  841. // Deselect parents, and recalc hasSubSel
  842. p = this.parent;
  843. while( p ) {
  844. p._select(false, false, false);
  845. var isPartSel = false;
  846. for(i=0, l=p.childList.length; i<l; i++) {
  847. if( p.childList[i].bSelected || p.childList[i].hasSubSel ) {
  848. isPartSel = true;
  849. break;
  850. }
  851. }
  852. p._setSubSel(isPartSel);
  853. p = p.parent;
  854. }
  855. }
  856. },
  857. _select: function(sel, fireEvents, deep) {
  858. // Select - but not focus - this node.
  859. // this.tree.logDebug("dtnode._select(%o) - %o", sel, this);
  860. var opts = this.tree.options;
  861. if( this.data.isStatusNode ){
  862. return;
  863. }
  864. //
  865. if( this.bSelected === sel ) {
  866. // this.tree.logDebug("dtnode._select(%o) IGNORED - %o", sel, this);
  867. return;
  868. }
  869. // Allow event listener to abort selection
  870. if ( fireEvents && opts.onQuerySelect && opts.onQuerySelect.call(this.tree, sel, this) === false ){
  871. return; // Callback returned false
  872. }
  873. // Force single-selection
  874. if( opts.selectMode==1 && sel ) {
  875. this.tree.visit(function(node){
  876. if( node.bSelected ) {
  877. // Deselect; assuming that in selectMode:1 there's max. one other selected node
  878. node._select(false, false, false);
  879. return false;
  880. }
  881. });
  882. }
  883. this.bSelected = sel;
  884. // this.tree._changeNodeList("select", this, sel);
  885. if( sel ) {
  886. if( opts.persist ){
  887. this.tree.persistence.addSelect(this.data.key);
  888. }
  889. $(this.span).addClass(opts.classNames.selected);
  890. if( deep && opts.selectMode === 3 ){
  891. this._fixSelectionState();
  892. }
  893. if ( fireEvents && opts.onSelect ){
  894. opts.onSelect.call(this.tree, true, this);
  895. }
  896. } else {
  897. if( opts.persist ){
  898. this.tree.persistence.clearSelect(this.data.key);
  899. }
  900. $(this.span).removeClass(opts.classNames.selected);
  901. if( deep && opts.selectMode === 3 ){
  902. this._fixSelectionState();
  903. }
  904. if ( fireEvents && opts.onSelect ){
  905. opts.onSelect.call(this.tree, false, this);
  906. }
  907. }
  908. },
  909. select: function(sel) {
  910. // Select - but not focus - this node.
  911. // this.tree.logDebug("dtnode.select(%o) - %o", sel, this);
  912. if( this.data.unselectable ){
  913. return this.bSelected;
  914. }
  915. return this._select(sel!==false, true, true);
  916. },
  917. toggleSelect: function() {
  918. // this.tree.logDebug("dtnode.toggleSelect() - %o", this);
  919. return this.select(!this.bSelected);
  920. },
  921. isSelected: function() {
  922. return this.bSelected;
  923. },
  924. isLazy: function() {
  925. return !!this.data.isLazy;
  926. },
  927. _loadContent: function() {
  928. try {
  929. var opts = this.tree.options;
  930. this.tree.logDebug("_loadContent: start - %o", this);
  931. this.setLazyNodeStatus(DTNodeStatus_Loading);
  932. if( true === opts.onLazyRead.call(this.tree, this) ) {
  933. // If function returns 'true', we assume that the loading is done:
  934. this.setLazyNodeStatus(DTNodeStatus_Ok);
  935. // Otherwise (i.e. if the loading was started as an asynchronous process)
  936. // the onLazyRead(dtnode) handler is expected to call dtnode.setLazyNodeStatus(DTNodeStatus_Ok/_Error) when done.
  937. this.tree.logDebug("_loadContent: succeeded - %o", this);
  938. }
  939. } catch(e) {
  940. this.tree.logWarning("_loadContent: failed - %o", e);
  941. this.setLazyNodeStatus(DTNodeStatus_Error, {tooltip: ""+e});
  942. }
  943. },
  944. _expand: function(bExpand, forceSync) {
  945. if( this.bExpanded === bExpand ) {
  946. this.tree.logDebug("dtnode._expand(%o) IGNORED - %o", bExpand, this);
  947. return;
  948. }
  949. this.tree.logDebug("dtnode._expand(%o) - %o", bExpand, this);
  950. var opts = this.tree.options;
  951. if( !bExpand && this.getLevel() < opts.minExpandLevel ) {
  952. this.tree.logDebug("dtnode._expand(%o) prevented collapse - %o", bExpand, this);
  953. return;
  954. }
  955. if ( opts.onQueryExpand && opts.onQueryExpand.call(this.tree, bExpand, this) === false ){
  956. return; // Callback returned false
  957. }
  958. this.bExpanded = bExpand;
  959. // Persist expand state
  960. if( opts.persist ) {
  961. if( bExpand ){
  962. this.tree.persistence.addExpand(this.data.key);
  963. }else{
  964. this.tree.persistence.clearExpand(this.data.key);
  965. }
  966. }
  967. // Do not apply animations in init phase, or before lazy-loading
  968. var allowEffects = !(this.data.isLazy && this.childList === null)
  969. && !this.isLoading
  970. && !forceSync;
  971. this.render(allowEffects);
  972. // Auto-collapse mode: collapse all siblings
  973. if( this.bExpanded && this.parent && opts.autoCollapse ) {
  974. var parents = this._parentList(false, true);
  975. for(var i=0, l=parents.length; i<l; i++){
  976. parents[i].collapseSiblings();
  977. }
  978. }
  979. // If the currently active node is now hidden, deactivate it
  980. if( opts.activeVisible && this.tree.activeNode && ! this.tree.activeNode.isVisible() ) {
  981. this.tree.activeNode.deactivate();
  982. }
  983. // Expanding a lazy node: set 'loading...' and call callback
  984. if( bExpand && this.data.isLazy && this.childList === null && !this.isLoading ) {
  985. this._loadContent();
  986. return;
  987. }
  988. if ( opts.onExpand ){
  989. opts.onExpand.call(this.tree, bExpand, this);
  990. }
  991. },
  992. isExpanded: function() {
  993. return this.bExpanded;
  994. },
  995. expand: function(flag) {
  996. flag = (flag !== false);
  997. if( !this.childList && !this.data.isLazy && flag ){
  998. return; // Prevent expanding empty nodes
  999. } else if( this.parent === null && !flag ){
  1000. return; // Prevent collapsing the root
  1001. }
  1002. this._expand(flag);
  1003. },
  1004. scheduleAction: function(mode, ms) {
  1005. /** Schedule activity for delayed execution (cancel any pending request).
  1006. * scheduleAction('cancel') will cancel the request.
  1007. */
  1008. if( this.tree.timer ) {
  1009. clearTimeout(this.tree.timer);
  1010. this.tree.logDebug("clearTimeout(%o)", this.tree.timer);
  1011. }
  1012. var self = this; // required for closures
  1013. switch (mode) {
  1014. case "cancel":
  1015. // Simply made sure that timer was cleared
  1016. break;
  1017. case "expand":
  1018. this.tree.timer = setTimeout(function(){
  1019. self.tree.logDebug("setTimeout: trigger expand");
  1020. self.expand(true);
  1021. }, ms);
  1022. break;
  1023. case "activate":
  1024. this.tree.timer = setTimeout(function(){
  1025. self.tree.logDebug("setTimeout: trigger activate");
  1026. self.activate();
  1027. }, ms);
  1028. break;
  1029. default:
  1030. throw "Invalid mode " + mode;
  1031. }
  1032. this.tree.logDebug("setTimeout(%s, %s): %s", mode, ms, this.tree.timer);
  1033. },
  1034. toggleExpand: function() {
  1035. this.expand(!this.bExpanded);
  1036. },
  1037. collapseSiblings: function() {
  1038. if( this.parent === null ){
  1039. return;
  1040. }
  1041. var ac = this.parent.childList;
  1042. for (var i=0, l=ac.length; i<l; i++) {
  1043. if ( ac[i] !== this && ac[i].bExpanded ){
  1044. ac[i]._expand(false);
  1045. }
  1046. }
  1047. },
  1048. _onClick: function(event) {
  1049. // this.tree.logDebug("dtnode.onClick(" + event.type + "): dtnode:" + this + ", button:" + event.button + ", which: " + event.which);
  1050. var targetType = this.getEventTargetType(event);
  1051. if( targetType === "expander" ) {
  1052. // Clicking the expander icon always expands/collapses
  1053. this.toggleExpand();
  1054. //this.focus(); // OXY
  1055. } else if( targetType === "checkbox" ) {
  1056. // Clicking the checkbox always (de)selects
  1057. this.toggleSelect();
  1058. this.focus(); // issue 95
  1059. } else {
  1060. this._userActivate();
  1061. var aTag = this.span.getElementsByTagName("a");
  1062. if(aTag[0]){
  1063. // issue 154
  1064. // TODO: check if still required on IE 9:
  1065. // Chrome and Safari don't focus the a-tag on click,
  1066. // but calling focus() seem to have problems on IE:
  1067. // http://code.google.com/p/dynatree/issues/detail?id=154
  1068. if(!$.browser.msie){
  1069. aTag[0].focus();
  1070. }
  1071. }else{
  1072. // 'noLink' option was set
  1073. return true;
  1074. }
  1075. }
  1076. // Make sure that clicks stop, otherwise <a href='#'> jumps to the top
  1077. event.preventDefault();
  1078. },
  1079. _onDblClick: function(event) {
  1080. // this.tree.logDebug("dtnode.onDblClick(" + event.type + "): dtnode:" + this + ", button:" + event.button + ", which: " + event.which);
  1081. },
  1082. _onKeydown: function(event) {
  1083. // this.tree.logDebug("dtnode.onKeydown(" + event.type + "): dtnode:" + this + ", charCode:" + event.charCode + ", keyCode: " + event.keyCode + ", which: " + event.which);
  1084. var handled = true,
  1085. sib;
  1086. // alert("keyDown" + event.which);
  1087. switch( event.which ) {
  1088. // charCodes:
  1089. // case 43: // '+'
  1090. case 107: // '+'
  1091. case 187: // '+' @ Chrome, Safari
  1092. if( !this.bExpanded ){ this.toggleExpand(); }
  1093. break;
  1094. // case 45: // '-'
  1095. case 109: // '-'
  1096. case 189: // '+' @ Chrome, Safari
  1097. if( this.bExpanded ){ this.toggleExpand(); }
  1098. break;
  1099. //~ case 42: // '*'
  1100. //~ break;
  1101. //~ case 47: // '/'
  1102. //~ break;
  1103. // case 13: // <enter>
  1104. // <enter> on a focused <a> tag seems to generate a click-event.
  1105. // this._userActivate();
  1106. // break;
  1107. case 32: // <space>
  1108. this._userActivate();
  1109. break;
  1110. case 8: // <backspace>
  1111. if( this.parent ){
  1112. this.parent.focus();
  1113. }
  1114. break;
  1115. case 37: // <left>
  1116. if( this.bExpanded ) {
  1117. this.toggleExpand();
  1118. this.focus();
  1119. // } else if( this.parent && (this.tree.options.rootVisible || this.parent.parent) ) {
  1120. } else if( this.parent && this.parent.parent ) {
  1121. this.parent.focus();
  1122. }
  1123. break;
  1124. case 39: // <right>
  1125. if( !this.bExpanded && (this.childList || this.data.isLazy) ) {
  1126. this.toggleExpand();
  1127. this.focus();
  1128. } else if( this.childList ) {
  1129. this.childList[0].focus();
  1130. }
  1131. break;
  1132. case 38: // <up>
  1133. sib = this.getPrevSibling();
  1134. while( sib && sib.bExpanded && sib.childList ){
  1135. sib = sib.childList[sib.childList.length-1];
  1136. }
  1137. // if( !sib && this.parent && (this.tree.options.rootVisible || this.parent.parent) )
  1138. if( !sib && this.parent && this.parent.parent ){
  1139. sib = this.parent;
  1140. }
  1141. if( sib ){
  1142. sib.focus();
  1143. }
  1144. break;
  1145. case 40: // <down>
  1146. if( this.bExpanded && this.childList ) {
  1147. sib = this.childList[0];
  1148. } else {
  1149. var parents = this._parentList(false, true);
  1150. for(var i=parents.length-1; i>=0; i--) {
  1151. sib = parents[i].getNextSibling();
  1152. if( sib ){ break; }
  1153. }
  1154. }
  1155. if( sib ){
  1156. sib.focus();
  1157. }
  1158. break;
  1159. default:
  1160. handled = false;
  1161. }
  1162. // Return false, if handled, to prevent default processing
  1163. // return !handled;
  1164. if(handled){
  1165. event.preventDefault();
  1166. }
  1167. },
  1168. _onKeypress: function(event) {
  1169. // onKeypress is only hooked to allow user callbacks.
  1170. // We don't process it, because IE and Safari don't fire keypress for cursor keys.
  1171. // this.tree.logDebug("dtnode.onKeypress(" + event.type + "): dtnode:" + this + ", charCode:" + event.charCode + ", keyCode: " + event.keyCode + ", which: " + event.which);
  1172. },
  1173. _onFocus: function(event) {
  1174. // Handles blur and focus events.
  1175. // this.tree.logDebug("dtnode.onFocus(%o): %o", event, this);
  1176. var opts = this.tree.options;
  1177. if ( event.type == "blur" || event.type == "focusout" ) {
  1178. if ( opts.onBlur ){
  1179. opts.onBlur.call(this.tree, this);
  1180. }
  1181. if( this.tree.tnFocused ){
  1182. $(this.tree.tnFocused.span).removeClass(opts.classNames.focused);
  1183. }
  1184. this.tree.tnFocused = null;
  1185. if( opts.persist ){
  1186. $.cookie(opts.cookieId+"-focus", "", opts.cookie);
  1187. }
  1188. } else if ( event.type=="focus" || event.type=="focusin") {
  1189. // Fix: sometimes the blur event is not generated
  1190. if( this.tree.tnFocused && this.tree.tnFocused !== this ) {
  1191. this.tree.logDebug("dtnode.onFocus: out of sync: curFocus: %o", this.tree.tnFocused);
  1192. $(this.tree.tnFocused.span).removeClass(opts.classNames.focused);
  1193. }
  1194. this.tree.tnFocused = this;
  1195. if ( opts.onFocus ){
  1196. opts.onFocus.call(this.tree, this);
  1197. }
  1198. $(this.tree.tnFocused.span).addClass(opts.classNames.focused);
  1199. if( opts.persist ){
  1200. $.cookie(opts.cookieId+"-focus", this.data.key, opts.cookie);
  1201. }
  1202. }
  1203. // TODO: return anything?
  1204. // return false;
  1205. },
  1206. visit: function(fn, includeSelf) {
  1207. // Call fn(node) for all child nodes. Stop iteration, if fn() returns false.
  1208. var res = true;
  1209. if( includeSelf === true ) {
  1210. res = fn(this);
  1211. if( res === false || res == "skip" ){
  1212. return res;
  1213. }
  1214. }
  1215. if(this.childList){
  1216. for(var i=0, l=this.childList.length; i<l; i++){
  1217. res = this.childList[i].visit(fn, true);
  1218. if( res === false ){
  1219. break;
  1220. }
  1221. }
  1222. }
  1223. return res;
  1224. },
  1225. visitParents: function(fn, includeSelf) {
  1226. // Visit parent nodes (bottom up)
  1227. if(includeSelf && fn(this) === false){
  1228. return false;
  1229. }
  1230. var p = this.parent;
  1231. while( p ) {
  1232. if(fn(p) === false){
  1233. return false;
  1234. }
  1235. p = p.parent;
  1236. }
  1237. return true;
  1238. },
  1239. remove: function() {
  1240. // Remove this node
  1241. // this.tree.logDebug ("%s.remove()", this);
  1242. if ( this === this.tree.root ){
  1243. throw "Cannot remove system root";
  1244. }
  1245. return this.parent.removeChild(this);
  1246. },
  1247. removeChild: function(tn) {
  1248. // Remove tn from list of direct children.
  1249. var ac = this.childList;
  1250. if( ac.length == 1 ) {
  1251. if( tn !== ac[0] ){
  1252. throw "removeChild: invalid child";
  1253. }
  1254. return this.removeChildren();
  1255. }
  1256. if( tn === this.tree.activeNode ){
  1257. tn.deactivate();
  1258. }
  1259. if( this.tree.options.persist ) {
  1260. if( tn.bSelected ){
  1261. this.tree.persistence.clearSelect(tn.data.key);
  1262. }
  1263. if ( tn.bExpanded ){
  1264. this.tree.persistence.clearExpand(tn.data.key);
  1265. }
  1266. }
  1267. tn.removeChildren(true);
  1268. // this.div.removeChild(tn.div);
  1269. this.ul.removeChild(tn.li);
  1270. for(var i=0, l=ac.length; i<l; i++) {
  1271. if( ac[i] === tn ) {
  1272. this.childList.splice(i, 1);
  1273. // delete tn; // JSLint complained
  1274. break;
  1275. }
  1276. }
  1277. },
  1278. removeChildren: function(isRecursiveCall, retainPersistence) {
  1279. // Remove all child nodes (more efficiently than recursive remove())
  1280. this.tree.logDebug("%s.removeChildren(%o)", this, isRecursiveCall);
  1281. var tree = this.tree;
  1282. var ac = this.childList;
  1283. if( ac ) {
  1284. for(var i=0, l=ac.length; i<l; i++) {
  1285. var tn = ac[i];
  1286. if ( tn === tree.activeNode && !retainPersistence ){
  1287. tn.deactivate();
  1288. }
  1289. if( this.tree.options.persist && !retainPersistence ) {
  1290. if( tn.bSelected ){
  1291. this.tree.persistence.clearSelect(tn.data.key);
  1292. }
  1293. if ( tn.bExpanded ){
  1294. this.tree.persistence.clearExpand(tn.data.key);
  1295. }
  1296. }
  1297. tn.removeChildren(true, retainPersistence);
  1298. if(this.ul){
  1299. this.ul.removeChild(tn.li);
  1300. }
  1301. /*
  1302. try{
  1303. this.ul.removeChild(tn.li);
  1304. }catch(e){
  1305. this.tree.logDebug("%s.removeChildren: couldnt remove LI", this, e);
  1306. }
  1307. */
  1308. // delete tn; JSLint complained
  1309. }
  1310. // Set to 'null' which is interpreted as 'not yet loaded' for lazy
  1311. // nodes
  1312. this.childList = null;
  1313. }
  1314. if( ! isRecursiveCall ) {
  1315. // this._expand(false);
  1316. // this.isRead = false;
  1317. this.isLoading = false;
  1318. this.render();
  1319. }
  1320. },
  1321. setTitle: function(title) {
  1322. this.fromDict({title: title});
  1323. },
  1324. reload: function(force) {
  1325. throw "Use reloadChildren() instead";
  1326. },
  1327. reloadChildren: function(callback) {
  1328. // Reload lazy content (expansion state is maintained).
  1329. if( this.parent === null ){
  1330. throw "Use tree.reload() instead";
  1331. }else if( ! this.data.isLazy ){
  1332. throw "node.reloadChildren() requires lazy nodes.";
  1333. }
  1334. // appendAjax triggers 'nodeLoaded' event.
  1335. // We listen to this, if a callback was passed to reloadChildren
  1336. if(callback){
  1337. var self = this;
  1338. var eventType = "nodeLoaded.dynatree." + this.tree.$tree.attr("id")
  1339. + "." + this.data.key;
  1340. this.tree.$tree.bind(eventType, function(e, node, isOk){
  1341. self.tree.$tree.unbind(eventType);
  1342. self.tree.logDebug("loaded %o, %o, %o", e, node, isOk);
  1343. if(node !== self){
  1344. throw "got invalid load event";
  1345. }
  1346. callback.call(self.tree, node, isOk);
  1347. });
  1348. }
  1349. // The expansion state is maintained
  1350. this.removeChildren();
  1351. this._loadContent();
  1352. // if( this.bExpanded ) {
  1353. // // Remove children first, to prevent effects being applied
  1354. // this.removeChildren();
  1355. // // then force re-expand to trigger lazy loading
  1356. //// this.expand(false);
  1357. //// this.expand(true);
  1358. // this._loadContent();
  1359. // } else {
  1360. // this.removeChildren();
  1361. // this._loadContent();
  1362. // }
  1363. },
  1364. /**
  1365. * Make sure the node with a given key path is available in the tree.
  1366. */
  1367. _loadKeyPath: function(keyPath, callback) {
  1368. var tree = this.tree;
  1369. tree.logDebug("%s._loadKeyPath(%s)", this, keyPath);
  1370. if(keyPath === ""){
  1371. throw "Key path must not be empty";
  1372. }
  1373. var segList = keyPath.split(tree.options.keyPathSeparator);
  1374. if(segList[0] === ""){
  1375. throw "Key path must be relative (don't start with '/')";
  1376. }
  1377. var seg = segList.shift();
  1378. for(var i=0, l=this.childList.length; i < l; i++){
  1379. var child = this.childList[i];
  1380. if( child.data.key === seg ){
  1381. if(segList.length === 0) {
  1382. // Found the end node
  1383. callback.call(tree, child, "ok");
  1384. }else if(child.data.isLazy && (child.childList === null || child.childList === undefined)){
  1385. tree.logDebug("%s._loadKeyPath(%s) -> reloading %s...", this, keyPath, child);
  1386. var self = this;
  1387. child.reloadChildren(function(node, isOk){
  1388. // After loading, look for direct child with that key
  1389. if(isOk){
  1390. tree.logDebug("%s._loadKeyPath(%s) -> reloaded %s.", node, keyPath, node);
  1391. callback.call(tree, child, "loaded");
  1392. node._loadKeyPath(segList.join(tree.options.keyPathSeparator), callback);
  1393. }else{
  1394. tree.logWarning("%s._loadKeyPath(%s) -> reloadChildren() failed.", self, keyPath);
  1395. callback.call(tree, child, "error");
  1396. }
  1397. }); // Note: this line gives a JSLint warning (Don't make functions within a loop)
  1398. // we can ignore it, since it will only be exectuted once, the the loop is ended
  1399. // See also http://stackoverflow.com/questions/3037598/how-to-get-around-the-jslint-error-dont-make-functions-within-a-loop
  1400. } else {
  1401. callback.call(tree, child, "loaded");
  1402. // Look for direct child with that key
  1403. child._loadKeyPath(segList.join(tree.options.keyPathSeparator), callback);
  1404. }
  1405. return;
  1406. }
  1407. }
  1408. // Could not find key
  1409. tree.logWarning("Node not found: " + seg);
  1410. return;
  1411. },
  1412. resetLazy: function() {
  1413. // Discard lazy content.
  1414. if( this.parent === null ){
  1415. throw "Use tree.reload() instead";
  1416. }else if( ! this.data.isLazy ){
  1417. throw "node.resetLazy() requires lazy nodes.";
  1418. }
  1419. this.expand(false);
  1420. this.removeChildren();
  1421. },
  1422. _addChildNode: function(dtnode, beforeNode) {
  1423. /**
  1424. * Internal function to add one single DynatreeNode as a child.
  1425. *
  1426. */
  1427. var tree = this.tree,
  1428. opts = tree.options,
  1429. pers = tree.persistence;
  1430. // tree.logDebug("%s._addChildNode(%o)", this, dtnode);
  1431. // --- Update and fix dtnode attributes if necessary
  1432. dtnode.parent = this;
  1433. // if( beforeNode && (beforeNode.parent !== this || beforeNode === dtnode ) )
  1434. // throw "<beforeNode> must be another child of <this>";
  1435. // --- Add dtnode as a child
  1436. if ( this.childList === null ) {
  1437. this.childList = [];
  1438. } else if( ! beforeNode ) {
  1439. // Fix 'lastsib'
  1440. if(this.childList.length > 0) {
  1441. $(this.childList[this.childList.length-1].span).removeClass(opts.classNames.lastsib);
  1442. }
  1443. }
  1444. if( beforeNode ) {
  1445. var iBefore = $.inArray(beforeNode, this.childList);
  1446. if( iBefore < 0 ){
  1447. throw "<beforeNode> must be a child of <this>";
  1448. }
  1449. this.childList.splice(iBefore, 0, dtnode);
  1450. } else {
  1451. // Append node
  1452. this.childList.push(dtnode);
  1453. }
  1454. // --- Handle persistence
  1455. // Initial status is read from cookies, if persistence is active and
  1456. // cookies are already present.
  1457. // Otherwise the status is read from the data attributes and then persisted.
  1458. var isInitializing = tree.isInitializing();
  1459. if( opts.persist && pers.cookiesFound && isInitializing ) {
  1460. // Init status from cookies
  1461. // tree.logDebug("init from cookie, pa=%o, dk=%o", pers.activeKey, dtnode.data.key);
  1462. if( pers.activeKey === dtnode.data.key ){
  1463. tree.activeNode = dtnode;
  1464. }
  1465. if( pers.focusedKey === dtnode.data.key ){
  1466. tree.focusNode = dtnode;
  1467. }
  1468. dtnode.bExpanded = ($.inArray(dtnode.data.key, pers.expandedKeyList) >= 0);
  1469. dtnode.bSelected = ($.inArray(dtnode.data.key, pers.selectedKeyList) >= 0);
  1470. // tree.logDebug(" key=%o, bSelected=%o", dtnode.data.key, dtnode.bSelected);
  1471. } else {
  1472. // Init status from data (Note: we write the cookies after the init phase)
  1473. // tree.logDebug("init from data");
  1474. if( dtnode.data.activate ) {
  1475. tree.activeNode = dtnode;
  1476. if( opts.persist ){
  1477. pers.activeKey = dtnode.data.key;
  1478. }
  1479. }
  1480. if( dtnode.data.focus ) {
  1481. tree.focusNode = dtnode;
  1482. if( opts.persist ){
  1483. pers.focusedKey = dtnode.data.key;
  1484. }
  1485. }
  1486. dtnode.bExpanded = ( dtnode.data.expand === true ); // Collapsed by default
  1487. if( dtnode.bExpanded && opts.persist ){
  1488. pers.addExpand(dtnode.data.key);
  1489. }
  1490. dtnode.bSelected = ( dtnode.data.select === true ); // Deselected by default
  1491. /*
  1492. Doesn't work, cause pers.selectedKeyList may be null
  1493. if( dtnode.bSelected && opts.selectMode==1
  1494. && pers.selectedKeyList && pers.selectedKeyList.length>0 ) {
  1495. tree.logWarning("Ignored multi-selection in single-mode for %o", dtnode);
  1496. dtnode.bSelected = false; // Fixing bad input data (multi selection for mode:1)
  1497. }
  1498. */
  1499. if( dtnode.bSelected && opts.persist ){
  1500. pers.addSelect(dtnode.data.key);
  1501. }
  1502. }
  1503. // Always expand, if it's below minExpandLevel
  1504. // tree.logDebug ("%s._addChildNode(%o), l=%o", this, dtnode, dtnode.getLevel());
  1505. if ( opts.minExpandLevel >= dtnode.getLevel() ) {
  1506. // tree.logDebug ("Force expand for %o", dtnode);
  1507. this.bExpanded = true;
  1508. }
  1509. // In multi-hier mode, update the parents selection state
  1510. // issue #82: only if not initializing, because the children may not exist yet
  1511. // if( !dtnode.data.isStatusNode && opts.selectMode==3 && !isInitializing )
  1512. // dtnode._fixSelectionState();
  1513. // In multi-hier mode, update the parents selection state
  1514. if( dtnode.bSelected && opts.selectMode==3 ) {
  1515. var p = this;
  1516. while( p ) {
  1517. if( !p.hasSubSel ){
  1518. p._setSubSel(true);
  1519. }
  1520. p = p.parent;
  1521. }
  1522. }
  1523. // render this node and the new child
  1524. if ( tree.bEnableUpdate ){
  1525. this.render();
  1526. }
  1527. return dtnode;
  1528. },
  1529. addChild: function(obj, beforeNode) {
  1530. /**
  1531. * Add a node object as child.
  1532. *
  1533. * This should be the only place, where a DynaTreeNode is constructed!
  1534. * (Except for the root node creation in the tree constructor)
  1535. *
  1536. * @param obj A JS object (may be recursive) or an array of those.
  1537. * @param {DynaTreeNode} beforeNode (optional) sibling node.
  1538. *
  1539. * Data format: array of node objects, with optional 'children' attributes.
  1540. * [
  1541. * { title: "t1", isFolder: true, ... }
  1542. * { title: "t2", isFolder: true, ...,
  1543. * children: [
  1544. * {title: "t2.1", ..},
  1545. * {..}
  1546. * ]
  1547. * }
  1548. * ]
  1549. * A simple object is also accepted instead of an array.
  1550. *
  1551. */
  1552. // this.tree.logDebug("%s.addChild(%o, %o)", this, obj, beforeNode);
  1553. if(typeof(obj) == "string"){
  1554. throw "Invalid data type for " + obj;
  1555. }else if( !obj || obj.length === 0 ){ // Passed null or undefined or empty array
  1556. return;
  1557. }else if( obj instanceof DynaTreeNode ){
  1558. return this._addChildNode(obj, beforeNode);
  1559. }
  1560. if( !obj.length ){ // Passed a single data object
  1561. obj = [ obj ];
  1562. }
  1563. var prevFlag = this.tree.enableUpdate(false);
  1564. var tnFirst = null;
  1565. for (var i=0, l=obj.length; i<l; i++) {
  1566. var data = obj[i];
  1567. var dtnode = this._addChildNode(new DynaTreeNode(this, this.tree, data), beforeNode);
  1568. if( !tnFirst ){
  1569. tnFirst = dtnode;
  1570. }
  1571. // Add child nodes recursively
  1572. if( data.children ){
  1573. dtnode.addChild(data.children, null);
  1574. }
  1575. }
  1576. this.tree.enableUpdate(prevFlag);
  1577. return tnFirst;
  1578. },
  1579. append: function(obj) {
  1580. this.tree.logWarning("node.append() is deprecated (use node.addChild() instead).");
  1581. return this.addChild(obj, null);
  1582. },
  1583. appendAjax: function(ajaxOptions) {
  1584. var self = this;
  1585. this.removeChildren(false, true);
  1586. this.setLazyNodeStatus(DTNodeStatus_Loading);
  1587. // Debug feature: force a delay, to simulate slow loading...
  1588. if(ajaxOptions.debugLazyDelay){
  1589. var ms = ajaxOptions.debugLazyDelay;
  1590. ajaxOptions.debugLazyDelay = 0;
  1591. this.tree.logInfo("appendAjax: waiting for debugLazyDelay " + ms);
  1592. setTimeout(function(){self.appendAjax(ajaxOptions);}, ms);
  1593. return;
  1594. }
  1595. // Ajax option inheritance: $.ajaxSetup < $.ui.dynatree.prototype.options.ajaxDefaults < tree.options.ajaxDefaults < ajaxOptions
  1596. var orgSuccess = ajaxOptions.success;
  1597. var orgError = ajaxOptions.error;
  1598. var eventType = "nodeLoaded.dynatree." + this.tree.$tree.attr("id")
  1599. + "." + this.data.key;
  1600. var options = $.extend({}, this.tree.options.ajaxDefaults, ajaxOptions, {
  1601. success: function(data, textStatus){
  1602. // <this> is the request options
  1603. // self.tree.logDebug("appendAjax().success");
  1604. var prevPhase = self.tree.phase;
  1605. self.tree.phase = "init";
  1606. // postProcess is similar to the standard dataFilter hook,
  1607. // but it is also called for JSONP
  1608. if( options.postProcess ){
  1609. data = options.postProcess.call(this, data, this.dataType);
  1610. }
  1611. // Process ASPX WebMethod JSON object inside "d" property
  1612. // http://code.google.com/p/dynatree/issues/detail?id=202
  1613. else if (data && data.hasOwnProperty("d")) {
  1614. data = data.d;
  1615. }
  1616. if(!$.isArray(data) || data.length !== 0){
  1617. self.addChild(data, null);
  1618. }
  1619. self.tree.phase = "postInit";
  1620. if( orgSuccess ){
  1621. orgSuccess.call(options, self, data, textStatus);
  1622. }
  1623. self.tree.logDebug("trigger " + eventType);
  1624. self.tree.$tree.trigger(eventType, [self, true]);
  1625. self.tree.phase = prevPhase;
  1626. // This should be the last command, so node.isLoading is true
  1627. // while the callbacks run
  1628. self.setLazyNodeStatus(DTNodeStatus_Ok);
  1629. if($.isArray(data) && data.length === 0){
  1630. // Set to [] which is interpreted as 'no children' for lazy
  1631. // nodes
  1632. self.childList = [];
  1633. self.render();
  1634. }
  1635. },
  1636. error: function(XMLHttpRequest, textStatus, errorThrown){
  1637. // <this> is the request options
  1638. self.tree.logWarning("appendAjax failed:", textStatus, ":\n", XMLHttpRequest, "\n", errorThrown);
  1639. if( orgError ){
  1640. orgError.call(options, self, XMLHttpRequest, textStatus, errorThrown);
  1641. }
  1642. self.tree.$tree.trigger(eventType, [self, false]);
  1643. self.setLazyNodeStatus(DTNodeStatus_Error, {info: textStatus, tooltip: ""+errorThrown});
  1644. }
  1645. });
  1646. $.ajax(options);
  1647. },
  1648. move: function(targetNode, mode) {
  1649. /**Move this node to targetNode.
  1650. * mode 'child': append this node as last child of targetNode.
  1651. * This is the default. To be compatble with the D'n'd
  1652. * hitMode, we also accept 'over'.
  1653. * mode 'before': add this node as sibling before targetNode.
  1654. * mode 'after': add this node as sibling after targetNode.
  1655. */
  1656. var pos;
  1657. if(this === targetNode){
  1658. return;
  1659. }
  1660. if( !this.parent ){
  1661. throw "Cannot move system root";
  1662. }
  1663. if(mode === undefined || mode == "over"){
  1664. mode = "child";
  1665. }
  1666. var prevParent = this.parent;
  1667. var targetParent = (mode === "child") ? targetNode : targetNode.parent;
  1668. if( targetParent.isDescendantOf(this) ){
  1669. throw "Cannot move a node to it's own descendant";
  1670. }
  1671. // Unlink this node from current parent
  1672. if( this.parent.childList.length == 1 ) {
  1673. this.parent.childList = null;
  1674. this.parent.bExpanded = false;
  1675. } else {
  1676. pos = $.inArray(this, this.parent.childList);
  1677. if( pos < 0 ){
  1678. throw "Internal error";
  1679. }
  1680. this.parent.childList.splice(pos, 1);
  1681. }
  1682. // Remove from source DOM parent
  1683. this.parent.ul.removeChild(this.li);
  1684. // Insert this node to target parent's child list
  1685. this.parent = targetParent;
  1686. if( targetParent.hasChildren() ) {
  1687. switch(mode) {
  1688. case "child":
  1689. // Append to existing target children
  1690. targetParent.childList.push(this);
  1691. break;
  1692. case "before":
  1693. // Insert this node before target node
  1694. pos = $.inArray(targetNode, targetParent.childList);
  1695. if( pos < 0 ){
  1696. throw "Internal error";
  1697. }
  1698. targetParent.childList.splice(pos, 0, this);
  1699. break;
  1700. case "after":
  1701. // Insert this node after target node
  1702. pos = $.inArray(targetNode, targetParent.childList);
  1703. if( pos < 0 ){
  1704. throw "Internal error";
  1705. }
  1706. targetParent.childList.splice(pos+1, 0, this);
  1707. break;
  1708. default:
  1709. throw "Invalid mode " + mode;
  1710. }
  1711. } else {
  1712. targetParent.childList = [ this ];
  1713. }
  1714. // Parent has no <ul> tag yet:
  1715. if( !targetParent.ul ) {
  1716. // This is the parent's first child: create UL tag
  1717. // (Hidden, because it will be
  1718. targetParent.ul = document.createElement("ul");
  1719. targetParent.ul.style.display = "none";
  1720. targetParent.li.appendChild(targetParent.ul);
  1721. }
  1722. // Add to target DOM parent
  1723. targetParent.ul.appendChild(this.li);
  1724. if( this.tree !== targetNode.tree ) {
  1725. // Fix node.tree for all source nodes
  1726. this.visit(function(node){
  1727. node.tree = targetNode.tree;
  1728. }, null, true);
  1729. throw "Not yet implemented.";
  1730. }
  1731. // TODO: fix selection state
  1732. // TODO: fix active state
  1733. if( !prevParent.isDescendantOf(targetParent)) {
  1734. prevParent.render();
  1735. }
  1736. if( !targetParent.isDescendantOf(prevParent) ) {
  1737. targetParent.render();
  1738. }
  1739. // this.tree.redraw();
  1740. /*
  1741. var tree = this.tree;
  1742. var opts = tree.options;
  1743. var pers = tree.persistence;
  1744. // Always expand, if it's below minExpandLevel
  1745. // tree.logDebug ("%s._addChildNode(%o), l=%o", this, dtnode, dtnode.getLevel());
  1746. if ( opts.minExpandLevel >= dtnode.getLevel() ) {
  1747. // tree.logDebug ("Force expand for %o", dtnode);
  1748. this.bExpanded = true;
  1749. }
  1750. // In multi-hier mode, update the parents selection state
  1751. // issue #82: only if not initializing, because the children may not exist yet
  1752. // if( !dtnode.data.isStatusNode && opts.selectMode==3 && !isInitializing )
  1753. // dtnode._fixSelectionState();
  1754. // In multi-hier mode, update the parents selection state
  1755. if( dtnode.bSelected && opts.selectMode==3 ) {
  1756. var p = this;
  1757. while( p ) {
  1758. if( !p.hasSubSel )
  1759. p._setSubSel(true);
  1760. p = p.parent;
  1761. }
  1762. }
  1763. // render this node and the new child
  1764. if ( tree.bEnableUpdate )
  1765. this.render();
  1766. return dtnode;
  1767. */
  1768. },
  1769. // --- end of class
  1770. lastentry: undefined
  1771. };
  1772. /*************************************************************************
  1773. * class DynaTreeStatus
  1774. */
  1775. var DynaTreeStatus = Class.create();
  1776. DynaTreeStatus._getTreePersistData = function(cookieId, cookieOpts) {
  1777. // Static member: Return persistence information from cookies
  1778. var ts = new DynaTreeStatus(cookieId, cookieOpts);
  1779. ts.read();
  1780. return ts.toDict();
  1781. };
  1782. // Make available in global scope
  1783. getDynaTreePersistData = DynaTreeStatus._getTreePersistData; // TODO: deprecated
  1784. DynaTreeStatus.prototype = {
  1785. // Constructor
  1786. initialize: function(cookieId, cookieOpts) {
  1787. // this._log("DynaTreeStatus: initialize");
  1788. if( cookieId === undefined ){
  1789. cookieId = $.ui.dynatree.prototype.options.cookieId;
  1790. }
  1791. cookieOpts = $.extend({}, $.ui.dynatree.prototype.options.cookie, cookieOpts);
  1792. this.cookieId = cookieId;
  1793. this.cookieOpts = cookieOpts;
  1794. this.cookiesFound = undefined;
  1795. this.activeKey = null;
  1796. this.focusedKey = null;
  1797. this.expandedKeyList = null;
  1798. this.selectedKeyList = null;
  1799. },
  1800. // member functions
  1801. _log: function(msg) {
  1802. // this.logDebug("_changeNodeList(%o): nodeList:%o, idx:%o", mode, nodeList, idx);
  1803. Array.prototype.unshift.apply(arguments, ["debug"]);
  1804. _log.apply(this, arguments);
  1805. },
  1806. read: function() {
  1807. // this._log("DynaTreeStatus: read");
  1808. // Read or init cookies.
  1809. this.cookiesFound = false;
  1810. var cookie = $.cookie(this.cookieId + "-active");
  1811. this.activeKey = ( cookie === null ) ? "" : cookie;
  1812. if( cookie !== null ){
  1813. this.cookiesFound = true;
  1814. }
  1815. cookie = $.cookie(this.cookieId + "-focus");
  1816. this.focusedKey = ( cookie === null ) ? "" : cookie;
  1817. if( cookie !== null ){
  1818. this.cookiesFound = true;
  1819. }
  1820. cookie = $.cookie(this.cookieId + "-expand");
  1821. this.expandedKeyList = ( cookie === null ) ? [] : cookie.split(",");
  1822. if( cookie !== null ){
  1823. this.cookiesFound = true;
  1824. }
  1825. cookie = $.cookie(this.cookieId + "-select");
  1826. this.selectedKeyList = ( cookie === null ) ? [] : cookie.split(",");
  1827. if( cookie !== null ){
  1828. this.cookiesFound = true;
  1829. }
  1830. },
  1831. write: function() {
  1832. // this._log("DynaTreeStatus: write");
  1833. $.cookie(this.cookieId + "-active", ( this.activeKey === null ) ? "" : this.activeKey, this.cookieOpts);
  1834. $.cookie(this.cookieId + "-focus", ( this.focusedKey === null ) ? "" : this.focusedKey, this.cookieOpts);
  1835. $.cookie(this.cookieId + "-expand", ( this.expandedKeyList === null ) ? "" : this.expandedKeyList.join(","), this.cookieOpts);
  1836. $.cookie(this.cookieId + "-select", ( this.selectedKeyList === null ) ? "" : this.selectedKeyList.join(","), this.cookieOpts);
  1837. },
  1838. addExpand: function(key) {
  1839. // this._log("addExpand(%o)", key);
  1840. if( $.inArray(key, this.expandedKeyList) < 0 ) {
  1841. this.expandedKeyList.push(key);
  1842. $.cookie(this.cookieId + "-expand", this.expandedKeyList.join(","), this.cookieOpts);
  1843. }
  1844. },
  1845. clearExpand: function(key) {
  1846. // this._log("clearExpand(%o)", key);
  1847. var idx = $.inArray(key, this.expandedKeyList);
  1848. if( idx >= 0 ) {
  1849. this.expandedKeyList.splice(idx, 1);
  1850. $.cookie(this.cookieId + "-expand", this.expandedKeyList.join(","), this.cookieOpts);
  1851. }
  1852. },
  1853. addSelect: function(key) {
  1854. // this._log("addSelect(%o)", key);
  1855. if( $.inArray(key, this.selectedKeyList) < 0 ) {
  1856. this.selectedKeyList.push(key);
  1857. $.cookie(this.cookieId + "-select", this.selectedKeyList.join(","), this.cookieOpts);
  1858. }
  1859. },
  1860. clearSelect: function(key) {
  1861. // this._log("clearSelect(%o)", key);
  1862. var idx = $.inArray(key, this.selectedKeyList);
  1863. if( idx >= 0 ) {
  1864. this.selectedKeyList.splice(idx, 1);
  1865. $.cookie(this.cookieId + "-select", this.selectedKeyList.join(","), this.cookieOpts);
  1866. }
  1867. },
  1868. isReloading: function() {
  1869. return this.cookiesFound === true;
  1870. },
  1871. toDict: function() {
  1872. return {
  1873. cookiesFound: this.cookiesFound,
  1874. activeKey: this.activeKey,
  1875. focusedKey: this.activeKey,
  1876. expandedKeyList: this.expandedKeyList,
  1877. selectedKeyList: this.selectedKeyList
  1878. };
  1879. },
  1880. // --- end of class
  1881. lastentry: undefined
  1882. };
  1883. /*************************************************************************
  1884. * class DynaTree
  1885. */
  1886. var DynaTree = Class.create();
  1887. // --- Static members ----------------------------------------------------------
  1888. DynaTree.version = "$Version: 1.2.0$";
  1889. /*
  1890. DynaTree._initTree = function() {
  1891. };
  1892. DynaTree._bind = function() {
  1893. };
  1894. */
  1895. //--- Class members ------------------------------------------------------------
  1896. DynaTree.prototype = {
  1897. // Constructor
  1898. // initialize: function(divContainer, options) {
  1899. initialize: function($widget) {
  1900. // instance members
  1901. this.phase = "init";
  1902. this.$widget = $widget;
  1903. this.options = $widget.options;
  1904. this.$tree = $widget.element;
  1905. this.timer = null;
  1906. // find container element
  1907. this.divTree = this.$tree.get(0);
  1908. // var parentPos = $(this.divTree).parent().offset();
  1909. // this.parentTop = parentPos.top;
  1910. // this.parentLeft = parentPos.left;
  1911. _initDragAndDrop(this);
  1912. },
  1913. // member functions
  1914. _load: function(callback) {
  1915. var $widget = this.$widget;
  1916. var opts = this.options,
  1917. self = this;
  1918. this.bEnableUpdate = true;
  1919. this._nodeCount = 1;
  1920. this.activeNode = null;
  1921. this.focusNode = null;
  1922. // Some deprecation warnings to help with migration
  1923. if( opts.rootVisible !== undefined ){
  1924. this.logWarning("Option 'rootVisible' is no longer supported.");
  1925. }
  1926. if( opts.minExpandLevel < 1 ) {
  1927. this.logWarning("Option 'minExpandLevel' must be >= 1.");
  1928. opts.minExpandLevel = 1;
  1929. }
  1930. // _log("warn", "jQuery.support.boxModel " + jQuery.support.boxModel);
  1931. // If a 'options.classNames' dictionary was passed, still use defaults
  1932. // for undefined classes:
  1933. if( opts.classNames !== $.ui.dynatree.prototype.options.classNames ) {
  1934. opts.classNames = $.extend({}, $.ui.dynatree.prototype.options.classNames, opts.classNames);
  1935. }
  1936. if( opts.ajaxDefaults !== $.ui.dynatree.prototype.options.ajaxDefaults ) {
  1937. opts.ajaxDefaults = $.extend({}, $.ui.dynatree.prototype.options.ajaxDefaults, opts.ajaxDefaults);
  1938. }
  1939. if( opts.dnd !== $.ui.dynatree.prototype.options.dnd ) {
  1940. opts.dnd = $.extend({}, $.ui.dynatree.prototype.options.dnd, opts.dnd);
  1941. }
  1942. // Guess skin path, if not specified
  1943. if(!opts.imagePath) {
  1944. $("script").each( function () {
  1945. var _rexDtLibName = /.*dynatree[^\/]*\.js$/i;
  1946. if( this.src.search(_rexDtLibName) >= 0 ) {
  1947. if( this.src.indexOf("/")>=0 ){ // issue #47
  1948. opts.imagePath = this.src.slice(0, this.src.lastIndexOf("/")) + "/skin/";
  1949. }else{
  1950. opts.imagePath = "skin/";
  1951. }
  1952. self.logDebug("Guessing imagePath from '%s': '%s'", this.src, opts.imagePath);
  1953. return false; // first match
  1954. }
  1955. });
  1956. }
  1957. this.persistence = new DynaTreeStatus(opts.cookieId, opts.cookie);
  1958. if( opts.persist ) {
  1959. if( !$.cookie ){
  1960. _log("warn", "Please include jquery.cookie.js to use persistence.");
  1961. }
  1962. this.persistence.read();
  1963. }
  1964. this.logDebug("DynaTree.persistence: %o", this.persistence.toDict());
  1965. // Cached tag strings
  1966. this.cache = {
  1967. tagEmpty: "<span class='" + opts.classNames.empty + "'></span>",
  1968. tagVline: "<span class='" + opts.classNames.vline + "'></span>",
  1969. tagExpander: "<span class='" + opts.classNames.expander + "'></span>",
  1970. tagConnector: "<span class='" + opts.classNames.connector + "'></span>",
  1971. //tagNodeIcon: "<span class='" + opts.classNames.nodeIcon + "'></span>",
  1972. tagNodeIcon: "",
  1973. tagCheckbox: "<span class='" + opts.classNames.checkbox + "'></span>",
  1974. lastentry: undefined
  1975. };
  1976. // Clear container, in case it contained some 'waiting' or 'error' text
  1977. // for clients that don't support JS.
  1978. // We don't do this however, if we try to load from an embedded UL element.
  1979. if( opts.children || (opts.initAjax && opts.initAjax.url) || opts.initId ){
  1980. $(this.divTree).empty();
  1981. }
  1982. var $ulInitialize = this.$tree.find(">ul:first").hide();
  1983. // Create the root element
  1984. this.tnRoot = new DynaTreeNode(null, this, {});
  1985. this.tnRoot.bExpanded = true;
  1986. this.tnRoot.render();
  1987. this.divTree.appendChild(this.tnRoot.ul);
  1988. var root = this.tnRoot;
  1989. var isReloading = ( opts.persist && this.persistence.isReloading() );
  1990. var isLazy = false;
  1991. var prevFlag = this.enableUpdate(false);
  1992. this.logDebug("Dynatree._load(): read tree structure...");
  1993. // Init tree structure
  1994. if( opts.children ) {
  1995. // Read structure from node array
  1996. root.addChild(opts.children);
  1997. } else if( opts.initAjax && opts.initAjax.url ) {
  1998. // Init tree from AJAX request
  1999. isLazy = true;
  2000. root.data.isLazy = true;
  2001. this._reloadAjax(callback);
  2002. } else if( opts.initId ) {
  2003. // Init tree from another UL element
  2004. this._createFromTag(root, $("#"+opts.initId));
  2005. } else {
  2006. // Init tree from the first UL element inside the container <div>
  2007. // var $ul = this.$tree.find(">ul:first").hide();
  2008. this._createFromTag(root, $ulInitialize);
  2009. $ulInitialize.remove();
  2010. }
  2011. this._checkConsistency();
  2012. // Fix part-sel flags
  2013. if(!isLazy && opts.selectMode == 3){
  2014. root._updatePartSelectionState();
  2015. }
  2016. // Render html markup
  2017. this.logDebug("Dynatree._load(): render nodes...");
  2018. this.enableUpdate(prevFlag);
  2019. // bind event handlers
  2020. this.logDebug("Dynatree._load(): bind events...");
  2021. this.$widget.bind();
  2022. // --- Post-load processing
  2023. this.logDebug("Dynatree._load(): postInit...");
  2024. this.phase = "postInit";
  2025. // In persist mode, make sure that cookies are written, even if they are empty
  2026. if( opts.persist ) {
  2027. this.persistence.write();
  2028. }
  2029. // Set focus, if possible (this will also fire an event and write a cookie)
  2030. if( this.focusNode && this.focusNode.isVisible() ) {
  2031. this.logDebug("Focus on init: %o", this.focusNode);
  2032. this.focusNode.focus();
  2033. }
  2034. if( !isLazy ) {
  2035. if( opts.onPostInit ) {
  2036. opts.onPostInit.call(this, isReloading, false);
  2037. }
  2038. if( callback ){
  2039. callback.call(this, "ok");
  2040. }
  2041. }
  2042. this.phase = "idle";
  2043. },
  2044. _reloadAjax: function(callback) {
  2045. // Reload
  2046. var opts = this.options;
  2047. if( ! opts.initAjax || ! opts.initAjax.url ){
  2048. throw "tree.reload() requires 'initAjax' mode.";
  2049. }
  2050. var pers = this.persistence;
  2051. var ajaxOpts = $.extend({}, opts.initAjax);
  2052. // Append cookie info to the request
  2053. // this.logDebug("reloadAjax: key=%o, an.key:%o", pers.activeKey, this.activeNode?this.activeNode.data.key:"?");
  2054. if( ajaxOpts.addActiveKey ){
  2055. ajaxOpts.data.activeKey = pers.activeKey;
  2056. }
  2057. if( ajaxOpts.addFocusedKey ){
  2058. ajaxOpts.data.focusedKey = pers.focusedKey;
  2059. }
  2060. if( ajaxOpts.addExpandedKeyList ){
  2061. ajaxOpts.data.expandedKeyList = pers.expandedKeyList.join(",");
  2062. }
  2063. if( ajaxOpts.addSelectedKeyList ){
  2064. ajaxOpts.data.selectedKeyList = pers.selectedKeyList.join(",");
  2065. }
  2066. // Set up onPostInit callback to be called when Ajax returns
  2067. if( ajaxOpts.success ){
  2068. this.logWarning("initAjax: success callback is ignored; use onPostInit instead.");
  2069. }
  2070. if( ajaxOpts.error ){
  2071. this.logWarning("initAjax: error callback is ignored; use onPostInit instead.");
  2072. }
  2073. var isReloading = pers.isReloading();
  2074. ajaxOpts.success = function(dtnode, data, textStatus) {
  2075. if(opts.selectMode == 3){
  2076. dtnode.tree.tnRoot._updatePartSelectionState();
  2077. }
  2078. if(opts.onPostInit){
  2079. opts.onPostInit.call(dtnode.tree, isReloading, false);
  2080. }
  2081. if(callback){
  2082. callback.call(dtnode.tree, "ok");
  2083. }
  2084. };
  2085. ajaxOpts.error = function(dtnode, XMLHttpRequest, textStatus, errorThrown) {
  2086. if(opts.onPostInit){
  2087. opts.onPostInit.call(dtnode.tree, isReloading, true, XMLHttpRequest, textStatus, errorThrown);
  2088. }
  2089. if(callback){
  2090. callback.call(dtnode.tree, "error", XMLHttpRequest, textStatus, errorThrown);
  2091. }
  2092. };
  2093. // }
  2094. this.logDebug("Dynatree._init(): send Ajax request...");
  2095. this.tnRoot.appendAjax(ajaxOpts);
  2096. },
  2097. toString: function() {
  2098. // return "DynaTree '" + this.options.title + "'";
  2099. return "Dynatree '" + this.$tree.attr("id") + "'";
  2100. },
  2101. toDict: function() {
  2102. return this.tnRoot.toDict(true);
  2103. },
  2104. serializeArray: function(stopOnParents) {
  2105. // Return a JavaScript array of objects, ready to be encoded as a JSON
  2106. // string for selected nodes
  2107. var nodeList = this.getSelectedNodes(stopOnParents),
  2108. name = this.$tree.attr("name") || this.$tree.attr("id"),
  2109. arr = [];
  2110. for(var i=0, l=nodeList.length; i<l; i++){
  2111. arr.push({name: name, value: nodeList[i].data.key});
  2112. }
  2113. return arr;
  2114. },
  2115. getPersistData: function() {
  2116. return this.persistence.toDict();
  2117. },
  2118. logDebug: function(msg) {
  2119. if( this.options.debugLevel >= 2 ) {
  2120. Array.prototype.unshift.apply(arguments, ["debug"]);
  2121. _log.apply(this, arguments);
  2122. }
  2123. },
  2124. logInfo: function(msg) {
  2125. if( this.options.debugLevel >= 1 ) {
  2126. Array.prototype.unshift.apply(arguments, ["info"]);
  2127. _log.apply(this, arguments);
  2128. }
  2129. },
  2130. logWarning: function(msg) {
  2131. Array.prototype.unshift.apply(arguments, ["warn"]);
  2132. _log.apply(this, arguments);
  2133. },
  2134. isInitializing: function() {
  2135. return ( this.phase=="init" || this.phase=="postInit" );
  2136. },
  2137. isReloading: function() {
  2138. return ( this.phase=="init" || this.phase=="postInit" ) && this.options.persist && this.persistence.cookiesFound;
  2139. },
  2140. isUserEvent: function() {
  2141. return ( this.phase=="userEvent" );
  2142. },
  2143. redraw: function() {
  2144. // this.logDebug("dynatree.redraw()...");
  2145. this.tnRoot.render(false, false);
  2146. // this.logDebug("dynatree.redraw() done.");
  2147. },
  2148. renderInvisibleNodes: function() {
  2149. this.tnRoot.render(false, true);
  2150. },
  2151. reload: function(callback) {
  2152. this._load(callback);
  2153. },
  2154. getRoot: function() {
  2155. return this.tnRoot;
  2156. },
  2157. enable: function() {
  2158. this.$widget.enable();
  2159. },
  2160. disable: function() {
  2161. this.$widget.disable();
  2162. },
  2163. getNodeByKey: function(key) {
  2164. // Search the DOM by element ID (assuming this is faster than traversing all nodes).
  2165. // $("#...") has problems, if the key contains '.', so we use getElementById()
  2166. var el = document.getElementById(this.options.idPrefix + key);
  2167. if( el ){
  2168. return el.dtnode ? el.dtnode : null;
  2169. }
  2170. // Not found in the DOM, but still may be in an unrendered part of tree
  2171. var match = null;
  2172. this.visit(function(node){
  2173. if(node.data.key == key) {
  2174. match = node;
  2175. return false;
  2176. }
  2177. }, true);
  2178. return match;
  2179. },
  2180. getActiveNode: function() {
  2181. return this.activeNode;
  2182. },
  2183. reactivate: function(setFocus) {
  2184. // Re-fire onQueryActivate and onActivate events.
  2185. var node = this.activeNode;
  2186. // this.logDebug("reactivate %o", node);
  2187. if( node ) {
  2188. this.activeNode = null; // Force re-activating
  2189. node.activate();
  2190. if( setFocus ){
  2191. node.focus();
  2192. }
  2193. }
  2194. },
  2195. getSelectedNodes: function(stopOnParents) {
  2196. var nodeList = [];
  2197. this.tnRoot.visit(function(node){
  2198. if( node.bSelected ) {
  2199. nodeList.push(node);
  2200. if( stopOnParents === true ){
  2201. return "skip"; // stop processing this branch
  2202. }
  2203. }
  2204. });
  2205. return nodeList;
  2206. },
  2207. activateKey: function(key) {
  2208. var dtnode = (key === null) ? null : this.getNodeByKey(key);
  2209. if( !dtnode ) {
  2210. if( this.activeNode ){
  2211. this.activeNode.deactivate();
  2212. }
  2213. this.activeNode = null;
  2214. return null;
  2215. }
  2216. dtnode.focus();
  2217. dtnode.activate();
  2218. return dtnode;
  2219. },
  2220. loadKeyPath: function(keyPath, callback) {
  2221. var segList = keyPath.split(this.options.keyPathSeparator);
  2222. // Remove leading '/'
  2223. if(segList[0] === ""){
  2224. segList.shift();
  2225. }
  2226. // Remove leading system root key
  2227. if(segList[0] == this.tnRoot.data.key){
  2228. this.logDebug("Removed leading root key.");
  2229. segList.shift();
  2230. }
  2231. keyPath = segList.join(this.options.keyPathSeparator);
  2232. return this.tnRoot._loadKeyPath(keyPath, callback);
  2233. },
  2234. selectKey: function(key, select) {
  2235. var dtnode = this.getNodeByKey(key);
  2236. if( !dtnode ){
  2237. return null;
  2238. }
  2239. dtnode.select(select);
  2240. return dtnode;
  2241. },
  2242. enableUpdate: function(bEnable) {
  2243. if ( this.bEnableUpdate==bEnable ){
  2244. return bEnable;
  2245. }
  2246. this.bEnableUpdate = bEnable;
  2247. if ( bEnable ){
  2248. this.redraw();
  2249. }
  2250. return !bEnable; // return previous value
  2251. },
  2252. count: function() {
  2253. return this.tnRoot.countChildren();
  2254. },
  2255. visit: function(fn, includeRoot) {
  2256. return this.tnRoot.visit(fn, includeRoot);
  2257. },
  2258. _createFromTag: function(parentTreeNode, $ulParent) {
  2259. // Convert a <UL>...</UL> list into children of the parent tree node.
  2260. var self = this;
  2261. /*
  2262. TODO: better?
  2263. this.$lis = $("li:has(a[href])", this.element);
  2264. this.$tabs = this.$lis.map(function() { return $("a", this)[0]; });
  2265. */
  2266. $ulParent.find(">li").each(function() {
  2267. var $li = $(this),
  2268. $liSpan = $li.find(">span:first"),
  2269. $liA = $li.find(">a:first"),
  2270. title,
  2271. href = null,
  2272. target = null,
  2273. tooltip;
  2274. if( $liSpan.length ) {
  2275. // If a <li><span> tag is specified, use it literally.
  2276. title = $liSpan.html();
  2277. } else if( $liA.length ) {
  2278. title = $liA.html();
  2279. href = $liA.attr("href");
  2280. target = $liA.attr("target");
  2281. tooltip = $liA.attr("title");
  2282. } else {
  2283. // If only a <li> tag is specified, use the trimmed string up to
  2284. // the next child <ul> tag.
  2285. title = $li.html();
  2286. var iPos = title.search(/<ul/i);
  2287. if( iPos >= 0 ){
  2288. title = $.trim(title.substring(0, iPos));
  2289. }else{
  2290. title = $.trim(title);
  2291. }
  2292. // self.logDebug("%o", title);
  2293. }
  2294. // Parse node options from ID, title and class attributes
  2295. var data = {
  2296. title: title,
  2297. tooltip: tooltip,
  2298. isFolder: $li.hasClass("folder"),
  2299. isLazy: $li.hasClass("lazy"),
  2300. expand: $li.hasClass("expanded"),
  2301. select: $li.hasClass("selected"),
  2302. activate: $li.hasClass("active"),
  2303. focus: $li.hasClass("focused"),
  2304. noLink: $li.hasClass("noLink")
  2305. };
  2306. if( href ){
  2307. data.href = href;
  2308. data.target = target;
  2309. }
  2310. if( $li.attr("title") ){
  2311. data.tooltip = $li.attr("title"); // overrides <a title='...'>
  2312. }
  2313. if( $li.attr("id") ){
  2314. data.key = $li.attr("id");
  2315. }
  2316. // If a data attribute is present, evaluate as a JavaScript object
  2317. if( $li.attr("data") ) {
  2318. var dataAttr = $.trim($li.attr("data"));
  2319. if( dataAttr ) {
  2320. if( dataAttr.charAt(0) != "{" ){
  2321. dataAttr = "{" + dataAttr + "}";
  2322. }
  2323. try {
  2324. $.extend(data, eval("(" + dataAttr + ")"));
  2325. } catch(e) {
  2326. throw ("Error parsing node data: " + e + "\ndata:\n'" + dataAttr + "'");
  2327. }
  2328. }
  2329. }
  2330. var childNode = parentTreeNode.addChild(data);
  2331. // Recursive reading of child nodes, if LI tag contains an UL tag
  2332. var $ul = $li.find(">ul:first");
  2333. if( $ul.length ) {
  2334. self._createFromTag(childNode, $ul); // must use 'self', because 'this' is the each() context
  2335. }
  2336. });
  2337. },
  2338. _checkConsistency: function() {
  2339. // this.logDebug("tree._checkConsistency() NOT IMPLEMENTED - %o", this);
  2340. },
  2341. _setDndStatus: function(sourceNode, targetNode, helper, hitMode, accept) {
  2342. // hitMode: 'after', 'before', 'over', 'out', 'start', 'stop'
  2343. var $source = sourceNode ? $(sourceNode.span) : null,
  2344. $target = $(targetNode.span);
  2345. if( !this.$dndMarker ) {
  2346. this.$dndMarker = $("<div id='dt-drop-marker'></div>")
  2347. .hide()
  2348. .prependTo($(this.divTree).parent());
  2349. // .prependTo("body");
  2350. // logMsg("Creating marker: %o", this.$dndMarker);
  2351. }
  2352. /*
  2353. if(hitMode === "start"){
  2354. }
  2355. if(hitMode === "stop"){
  2356. // sourceNode.removeClass("dt-drop-target");
  2357. }
  2358. */
  2359. // this.$dndMarker.attr("class", hitMode);
  2360. if(hitMode === "after" || hitMode === "before" || hitMode === "over"){
  2361. // $source && $source.addClass("dt-drag-source");
  2362. var pos = $target.offset();
  2363. switch(hitMode){
  2364. case "before":
  2365. this.$dndMarker.removeClass("dt-drop-after dt-drop-over");
  2366. this.$dndMarker.addClass("dt-drop-before");
  2367. pos.top -= 8;
  2368. break;
  2369. case "after":
  2370. this.$dndMarker.removeClass("dt-drop-before dt-drop-over");
  2371. this.$dndMarker.addClass("dt-drop-after");
  2372. pos.top += 8;
  2373. break;
  2374. default:
  2375. this.$dndMarker.removeClass("dt-drop-after dt-drop-before");
  2376. this.$dndMarker.addClass("dt-drop-over");
  2377. $target.addClass("dt-drop-target");
  2378. pos.left += 8;
  2379. }
  2380. // logMsg("Creating marker: %o", this.$dndMarker);
  2381. // logMsg(" $target.offset=%o", $target);
  2382. // logMsg(" pos/$target.offset=%o", pos);
  2383. // logMsg(" $target.position=%o", $target.position());
  2384. // logMsg(" $target.offsetParent=%o, ot:%o", $target.offsetParent(), $target.offsetParent().offset());
  2385. // logMsg(" $(this.divTree).offset=%o", $(this.divTree).offset());
  2386. // logMsg(" $(this.divTree).parent=%o", $(this.divTree).parent());
  2387. this.$dndMarker.offset({left: pos.left, top: pos.top})
  2388. .css({
  2389. "z-index": 1000
  2390. })
  2391. .show();
  2392. // helper.addClass("dt-drop-hover");
  2393. } else {
  2394. // $source && $source.removeClass("dt-drag-source");
  2395. $target.removeClass("dt-drop-target");
  2396. this.$dndMarker.hide();
  2397. // helper.removeClass("dt-drop-hover");
  2398. }
  2399. if(hitMode === "after"){
  2400. $target.addClass("dt-drop-after");
  2401. } else {
  2402. $target.removeClass("dt-drop-after");
  2403. }
  2404. if(hitMode === "before"){
  2405. $target.addClass("dt-drop-before");
  2406. } else {
  2407. $target.removeClass("dt-drop-before");
  2408. }
  2409. if(accept === true){
  2410. if($source){
  2411. $source.addClass("dt-drop-accept");
  2412. }
  2413. $target.addClass("dt-drop-accept");
  2414. helper.addClass("dt-drop-accept");
  2415. }else{
  2416. if($source){
  2417. $source.removeClass("dt-drop-accept");
  2418. }
  2419. $target.removeClass("dt-drop-accept");
  2420. helper.removeClass("dt-drop-accept");
  2421. }
  2422. if(accept === false){
  2423. if($source){
  2424. $source.addClass("dt-drop-reject");
  2425. }
  2426. $target.addClass("dt-drop-reject");
  2427. helper.addClass("dt-drop-reject");
  2428. }else{
  2429. if($source){
  2430. $source.removeClass("dt-drop-reject");
  2431. }
  2432. $target.removeClass("dt-drop-reject");
  2433. helper.removeClass("dt-drop-reject");
  2434. }
  2435. },
  2436. _onDragEvent: function(eventName, node, otherNode, event, ui, draggable) {
  2437. /**
  2438. * Handles drag'n'drop functionality.
  2439. *
  2440. * A standard jQuery drag-and-drop process may generate these calls:
  2441. *
  2442. * draggable helper():
  2443. * _onDragEvent("helper", sourceNode, null, event, null, null);
  2444. * start:
  2445. * _onDragEvent("start", sourceNode, null, event, ui, draggable);
  2446. * drag:
  2447. * _onDragEvent("leave", prevTargetNode, sourceNode, event, ui, draggable);
  2448. * _onDragEvent("over", targetNode, sourceNode, event, ui, draggable);
  2449. * _onDragEvent("enter", targetNode, sourceNode, event, ui, draggable);
  2450. * stop:
  2451. * _onDragEvent("drop", targetNode, sourceNode, event, ui, draggable);
  2452. * _onDragEvent("leave", targetNode, sourceNode, event, ui, draggable);
  2453. * _onDragEvent("stop", sourceNode, null, event, ui, draggable);
  2454. */
  2455. // if(eventName !== "over"){
  2456. // this.logDebug("tree._onDragEvent(%s, %o, %o) - %o", eventName, node, otherNode, this);
  2457. // }
  2458. var opts = this.options;
  2459. var dnd = this.options.dnd;
  2460. var res = null;
  2461. var nodeTag = $(node.span);
  2462. var hitMode;
  2463. switch (eventName) {
  2464. case "helper":
  2465. // Only event and node argument is available
  2466. var helper = $("<div class='dt-drag-helper'><span class='dt-drag-helper-img' /></div>")
  2467. .append($(event.target).closest('a').clone());
  2468. // Attach node reference to helper object
  2469. helper.data("dtSourceNode", node);
  2470. // this.logDebug("helper.sourceNode=%o", helper.data("dtSourceNode"));
  2471. res = helper;
  2472. break;
  2473. case "start":
  2474. if(node.isStatusNode()) {
  2475. res = false;
  2476. } else if(dnd.onDragStart) {
  2477. res = dnd.onDragStart(node);
  2478. }
  2479. if(res === false) {
  2480. this.logDebug("tree.onDragStart() cancelled");
  2481. //draggable._clear();
  2482. // NOTE: the return value seems to be ignored (drag is not canceled, when false is returned)
  2483. ui.helper.trigger("mouseup");
  2484. ui.helper.hide();
  2485. } else {
  2486. nodeTag.addClass("dt-drag-source");
  2487. }
  2488. break;
  2489. case "enter":
  2490. res = dnd.onDragEnter ? dnd.onDragEnter(node, otherNode) : null;
  2491. res = {
  2492. over: (res !== false) && ((res === true) || (res === "over") || $.inArray("over", res) >= 0),
  2493. before: (res !== false) && ((res === true) || (res === "before") || $.inArray("before", res) >= 0),
  2494. after: (res !== false) && ((res === true) || (res === "after") || $.inArray("after", res) >= 0)
  2495. };
  2496. ui.helper.data("enterResponse", res);
  2497. // this.logDebug("helper.enterResponse: %o", res);
  2498. break;
  2499. case "over":
  2500. var enterResponse = ui.helper.data("enterResponse");
  2501. hitMode = null;
  2502. if(enterResponse === false){
  2503. // Don't call onDragOver if onEnter returned false.
  2504. break;
  2505. } else if(typeof enterResponse === "string") {
  2506. // Use hitMode from onEnter if provided.
  2507. hitMode = enterResponse;
  2508. } else {
  2509. // Calculate hitMode from relative cursor position.
  2510. var nodeOfs = nodeTag.offset();
  2511. // var relPos = { x: event.clientX - nodeOfs.left,
  2512. // y: event.clientY - nodeOfs.top };
  2513. // nodeOfs.top += this.parentTop;
  2514. // nodeOfs.left += this.parentLeft;
  2515. var relPos = { x: event.pageX - nodeOfs.left,
  2516. y: event.pageY - nodeOfs.top };
  2517. var relPos2 = { x: relPos.x / nodeTag.width(),
  2518. y: relPos.y / nodeTag.height() };
  2519. // this.logDebug("event.page: %s/%s", event.pageX, event.pageY);
  2520. // this.logDebug("event.client: %s/%s", event.clientX, event.clientY);
  2521. // this.logDebug("nodeOfs: %s/%s", nodeOfs.left, nodeOfs.top);
  2522. //// this.logDebug("parent: %s/%s", this.parentLeft, this.parentTop);
  2523. // this.logDebug("relPos: %s/%s", relPos.x, relPos.y);
  2524. // this.logDebug("relPos2: %s/%s", relPos2.x, relPos2.y);
  2525. if( enterResponse.after && relPos2.y > 0.75 ){
  2526. hitMode = "after";
  2527. } else if(!enterResponse.over && enterResponse.after && relPos2.y > 0.5 ){
  2528. hitMode = "after";
  2529. } else if(enterResponse.before && relPos2.y <= 0.25) {
  2530. hitMode = "before";
  2531. } else if(!enterResponse.over && enterResponse.before && relPos2.y <= 0.5) {
  2532. hitMode = "before";
  2533. } else if(enterResponse.over) {
  2534. hitMode = "over";
  2535. }
  2536. // Prevent no-ops like 'before source node'
  2537. // TODO: these are no-ops when moving nodes, but not in copy mode
  2538. if( dnd.preventVoidMoves ){
  2539. if(node === otherNode){
  2540. // this.logDebug(" drop over source node prevented");
  2541. hitMode = null;
  2542. }else if(hitMode === "before" && otherNode && node === otherNode.getNextSibling()){
  2543. // this.logDebug(" drop after source node prevented");
  2544. hitMode = null;
  2545. }else if(hitMode === "after" && otherNode && node === otherNode.getPrevSibling()){
  2546. // this.logDebug(" drop before source node prevented");
  2547. hitMode = null;
  2548. }else if(hitMode === "over" && otherNode
  2549. && otherNode.parent === node && otherNode.isLastSibling() ){
  2550. // this.logDebug(" drop last child over own parent prevented");
  2551. hitMode = null;
  2552. }
  2553. }
  2554. // this.logDebug("hitMode: %s - %s - %s", hitMode, (node.parent === otherNode), node.isLastSibling());
  2555. ui.helper.data("hitMode", hitMode);
  2556. }
  2557. // Auto-expand node (only when 'over' the node, not 'before', or 'after')
  2558. if(hitMode === "over"
  2559. && dnd.autoExpandMS && node.hasChildren() !== false && !node.bExpanded) {
  2560. node.scheduleAction("expand", dnd.autoExpandMS);
  2561. }
  2562. if(hitMode && dnd.onDragOver){
  2563. res = dnd.onDragOver(node, otherNode, hitMode);
  2564. }
  2565. this._setDndStatus(otherNode, node, ui.helper, hitMode, res!==false);
  2566. break;
  2567. case "drop":
  2568. hitMode = ui.helper.data("hitMode");
  2569. if(hitMode && dnd.onDrop){
  2570. dnd.onDrop(node, otherNode, hitMode, ui, draggable);
  2571. }
  2572. break;
  2573. case "leave":
  2574. // Cancel pending expand request
  2575. node.scheduleAction("cancel");
  2576. ui.helper.data("enterResponse", null);
  2577. ui.helper.data("hitMode", null);
  2578. this._setDndStatus(otherNode, node, ui.helper, "out", undefined);
  2579. if(dnd.onDragLeave){
  2580. dnd.onDragLeave(node, otherNode);
  2581. }
  2582. break;
  2583. case "stop":
  2584. nodeTag.removeClass("dt-drag-source");
  2585. if(dnd.onDragStop){
  2586. dnd.onDragStop(node);
  2587. }
  2588. break;
  2589. default:
  2590. throw "Unsupported drag event: " + eventName;
  2591. }
  2592. return res;
  2593. },
  2594. cancelDrag: function() {
  2595. var dd = $.ui.ddmanager.current;
  2596. if(dd){
  2597. dd.cancel();
  2598. }
  2599. },
  2600. // --- end of class
  2601. lastentry: undefined
  2602. };
  2603. /*************************************************************************
  2604. * Widget $(..).dynatree
  2605. */
  2606. $.widget("ui.dynatree", {
  2607. /*
  2608. init: function() {
  2609. // ui.core 1.6 renamed init() to _init(): this stub assures backward compatibility
  2610. _log("warn", "ui.dynatree.init() was called; you should upgrade to jquery.ui.core.js v1.8 or higher.");
  2611. return this._init();
  2612. },
  2613. */
  2614. _init: function() {
  2615. if( parseFloat($.ui.version) < 1.8 ) {
  2616. // jquery.ui.core 1.8 renamed _init() to _create(): this stub assures backward compatibility
  2617. if(this.options.debugLevel >= 0){
  2618. _log("warn", "ui.dynatree._init() was called; you should upgrade to jquery.ui.core.js v1.8 or higher.");
  2619. }
  2620. return this._create();
  2621. }
  2622. // jquery.ui.core 1.8 still uses _init() to perform "default functionality"
  2623. if(this.options.debugLevel >= 2){
  2624. _log("debug", "ui.dynatree._init() was called; no current default functionality.");
  2625. }
  2626. },
  2627. _create: function() {
  2628. var opts = this.options;
  2629. if(opts.debugLevel >= 1){
  2630. //logMsg("Dynatree._create(): version='%s', debugLevel=%o.", $.ui.dynatree.version, this.options.debugLevel);
  2631. }
  2632. // The widget framework supplies this.element and this.options.
  2633. this.options.event += ".dynatree"; // namespace event
  2634. var divTree = this.element.get(0);
  2635. /* // Clear container, in case it contained some 'waiting' or 'error' text
  2636. // for clients that don't support JS
  2637. if( opts.children || (opts.initAjax && opts.initAjax.url) || opts.initId )
  2638. $(divTree).empty();
  2639. */
  2640. // Create the DynaTree object
  2641. this.tree = new DynaTree(this);
  2642. this.tree._load();
  2643. this.tree.logDebug("Dynatree._init(): done.");
  2644. },
  2645. bind: function() {
  2646. // Prevent duplicate binding
  2647. this.unbind();
  2648. var eventNames = "click.dynatree dblclick.dynatree";
  2649. if( this.options.keyboard ){
  2650. // Note: leading ' '!
  2651. eventNames += " keypress.dynatree keydown.dynatree";
  2652. }
  2653. this.element.bind(eventNames, function(event){
  2654. var dtnode = getDtNodeFromElement(event.target);
  2655. if( !dtnode ){
  2656. return true; // Allow bubbling of other events
  2657. }
  2658. var tree = dtnode.tree;
  2659. var o = tree.options;
  2660. tree.logDebug("event(%s): dtnode: %s", event.type, dtnode);
  2661. var prevPhase = tree.phase;
  2662. tree.phase = "userEvent";
  2663. try {
  2664. switch(event.type) {
  2665. case "click":
  2666. return ( o.onClick && o.onClick.call(tree, dtnode, event)===false ) ? false : dtnode._onClick(event);
  2667. case "dblclick":
  2668. return ( o.onDblClick && o.onDblClick.call(tree, dtnode, event)===false ) ? false : dtnode._onDblClick(event);
  2669. case "keydown":
  2670. return ( o.onKeydown && o.onKeydown.call(tree, dtnode, event)===false ) ? false : dtnode._onKeydown(event);
  2671. case "keypress":
  2672. return ( o.onKeypress && o.onKeypress.call(tree, dtnode, event)===false ) ? false : dtnode._onKeypress(event);
  2673. }
  2674. } catch(e) {
  2675. var _ = null; // issue 117
  2676. tree.logWarning("bind(%o): dtnode: %o, error: %o", event, dtnode, e);
  2677. } finally {
  2678. tree.phase = prevPhase;
  2679. }
  2680. });
  2681. // focus/blur don't bubble, i.e. are not delegated to parent <div> tags,
  2682. // so we use the addEventListener capturing phase.
  2683. // See http://www.howtocreate.co.uk/tutorials/javascript/domevents
  2684. function __focusHandler(event) {
  2685. // Handles blur and focus.
  2686. // Fix event for IE:
  2687. // doesn't pass JSLint:
  2688. // event = arguments[0] = $.event.fix( event || window.event );
  2689. // what jQuery does:
  2690. // var args = jQuery.makeArray( arguments );
  2691. // event = args[0] = jQuery.event.fix( event || window.event );
  2692. event = $.event.fix( event || window.event );
  2693. var dtnode = getDtNodeFromElement(event.target);
  2694. return dtnode ? dtnode._onFocus(event) : false;
  2695. }
  2696. var div = this.tree.divTree;
  2697. if( div.addEventListener ) {
  2698. div.addEventListener("focus", __focusHandler, true);
  2699. div.addEventListener("blur", __focusHandler, true);
  2700. } else {
  2701. div.onfocusin = div.onfocusout = __focusHandler;
  2702. }
  2703. // EVENTS
  2704. // disable click if event is configured to something else
  2705. // if (!(/^click/).test(o.event))
  2706. // this.$tabs.bind("click.tabs", function() { return false; });
  2707. },
  2708. unbind: function() {
  2709. this.element.unbind(".dynatree");
  2710. },
  2711. /* TODO: we could handle option changes during runtime here (maybe to re-render, ...)
  2712. setData: function(key, value) {
  2713. this.tree.logDebug("dynatree.setData('" + key + "', '" + value + "')");
  2714. },
  2715. */
  2716. enable: function() {
  2717. this.bind();
  2718. // Call default disable(): remove -disabled from css:
  2719. $.Widget.prototype.enable.apply(this, arguments);
  2720. },
  2721. disable: function() {
  2722. this.unbind();
  2723. // Call default disable(): add -disabled to css:
  2724. $.Widget.prototype.disable.apply(this, arguments);
  2725. },
  2726. // --- getter methods (i.e. NOT returning a reference to $)
  2727. getTree: function() {
  2728. return this.tree;
  2729. },
  2730. getRoot: function() {
  2731. return this.tree.getRoot();
  2732. },
  2733. getActiveNode: function() {
  2734. return this.tree.getActiveNode();
  2735. },
  2736. getSelectedNodes: function() {
  2737. return this.tree.getSelectedNodes();
  2738. },
  2739. // ------------------------------------------------------------------------
  2740. lastentry: undefined
  2741. });
  2742. // The following methods return a value (thus breaking the jQuery call chain):
  2743. if( parseFloat($.ui.version) < 1.8 ) {
  2744. $.ui.dynatree.getter = "getTree getRoot getActiveNode getSelectedNodes";
  2745. }
  2746. /*******************************************************************************
  2747. * Tools in ui.dynatree namespace
  2748. */
  2749. $.ui.dynatree.version = "$Version: 1.2.0$";
  2750. /**
  2751. * Return a DynaTreeNode object for a given DOM element
  2752. */
  2753. $.ui.dynatree.getNode = function(el) {
  2754. if(el instanceof DynaTreeNode){
  2755. return el; // el already was a DynaTreeNode
  2756. }
  2757. // TODO: for some reason $el.parents("[dtnode]") does not work (jQuery 1.6.1)
  2758. // maybe, because dtnode is a property, not an attribute
  2759. var $el = el.selector === undefined ? $(el) : el,
  2760. // parent = $el.closest("[dtnode]"),
  2761. parent = $el.parents("[dtnode]").first(),
  2762. node;
  2763. if(typeof parent.prop == "function"){
  2764. node = parent.prop("dtnode");
  2765. }else{ // pre jQuery 1.6
  2766. node = parent.attr("dtnode");
  2767. }
  2768. return node;
  2769. }
  2770. /**Return persistence information from cookies.*/
  2771. $.ui.dynatree.getPersistData = DynaTreeStatus._getTreePersistData;
  2772. /*******************************************************************************
  2773. * Plugin default options:
  2774. */
  2775. $.ui.dynatree.prototype.options = {
  2776. title: "Dynatree", // Tree's name (only used for debug outpu)
  2777. minExpandLevel: 1, // 1: root node is not collapsible
  2778. imagePath: null, // Path to a folder containing icons. Defaults to 'skin/' subdirectory.
  2779. children: null, // Init tree structure from this object array.
  2780. initId: null, // Init tree structure from a <ul> element with this ID.
  2781. initAjax: null, // Ajax options used to initialize the tree strucuture.
  2782. autoFocus: true, // Set focus to first child, when expanding or lazy-loading.
  2783. keyboard: true, // Support keyboard navigation.
  2784. persist: false, // Persist expand-status to a cookie
  2785. autoCollapse: false, // Automatically collapse all siblings, when a node is expanded.
  2786. clickFolderMode: 3, // 1:activate, 2:expand, 3:activate and expand
  2787. activeVisible: true, // Make sure, active nodes are visible (expanded).
  2788. checkbox: false, // Show checkboxes.
  2789. selectMode: 2, // 1:single, 2:multi, 3:multi-hier
  2790. fx: null, // Animations, e.g. null or { height: "toggle", duration: 200 }
  2791. noLink: false, // Use <span> instead of <a> tags for all nodes
  2792. // Low level event handlers: onEvent(dtnode, event): return false, to stop default processing
  2793. onClick: null, // null: generate focus, expand, activate, select events.
  2794. onDblClick: null, // (No default actions.)
  2795. onKeydown: null, // null: generate keyboard navigation (focus, expand, activate).
  2796. onKeypress: null, // (No default actions.)
  2797. onFocus: null, // null: set focus to node.
  2798. onBlur: null, // null: remove focus from node.
  2799. // Pre-event handlers onQueryEvent(flag, dtnode): return false, to stop processing
  2800. onQueryActivate: null, // Callback(flag, dtnode) before a node is (de)activated.
  2801. onQuerySelect: null, // Callback(flag, dtnode) before a node is (de)selected.
  2802. onQueryExpand: null, // Callback(flag, dtnode) before a node is expanded/collpsed.
  2803. // High level event handlers
  2804. onPostInit: null, // Callback(isReloading, isError) when tree was (re)loaded.
  2805. onActivate: null, // Callback(dtnode) when a node is activated.
  2806. onDeactivate: null, // Callback(dtnode) when a node is deactivated.
  2807. onSelect: null, // Callback(flag, dtnode) when a node is (de)selected.
  2808. onExpand: null, // Callback(flag, dtnode) when a node is expanded/collapsed.
  2809. onLazyRead: null, // Callback(dtnode) when a lazy node is expanded for the first time.
  2810. onCustomRender: null, // Callback(dtnode) before a node is rendered. Return a HTML string to override.
  2811. // onCustomRender:function(node) {
  2812. // return "<a href='" + node.data.href + "' id='" + node.data.key + "' class='dt-title'>" + node.data.title + "</a>"
  2813. // },
  2814. //onCreate: function(node, nodeSpan) {
  2815. //node.expand(true);
  2816. //},
  2817. onCreate: null, // Callback(dtnode, nodeSpan) after a node was rendered for the first time.
  2818. //onRender: null, // Callback(dtnode, nodeSpan) after a node was rendered.
  2819. onRender: null,
  2820. // Drag'n'drop support
  2821. dnd: {
  2822. // Make tree nodes draggable:
  2823. onDragStart: null, // Callback(sourceNode), return true, to enable dnd
  2824. onDragStop: null, // Callback(sourceNode)
  2825. // helper: null,
  2826. // Make tree nodes accept draggables
  2827. autoExpandMS: 1000, // Expand nodes after n milliseconds of hovering.
  2828. preventVoidMoves: true, // Prevent dropping nodes 'before self', etc.
  2829. onDragEnter: null, // Callback(targetNode, sourceNode)
  2830. onDragOver: null, // Callback(targetNode, sourceNode, hitMode)
  2831. onDrop: null, // Callback(targetNode, sourceNode, hitMode)
  2832. onDragLeave: null // Callback(targetNode, sourceNode)
  2833. },
  2834. ajaxDefaults: { // Used by initAjax option
  2835. cache: false, // false: Append random '_' argument to the request url to prevent caching.
  2836. dataType: "json" // Expect json format and pass json object to callbacks.
  2837. },
  2838. strings: {
  2839. loading: "Loading&#8230;",
  2840. loadError: "Load error!"
  2841. },
  2842. generateIds: true, // Generate id attributes like <span id='dt-id-KEY'>
  2843. idPrefix: "", // Used to generate node id's like <span id="dt-id-<key>">.
  2844. keyPathSeparator: "/", // Used by node.getKeyPath() and tree.loadKeyPath().
  2845. // cookieId: "dt-cookie", // Choose a more unique name, to allow multiple trees.
  2846. cookieId: "dynatree", // Choose a more unique name, to allow multiple trees.
  2847. cookie: {
  2848. expires: null //7, // Days or Date; null: session cookie
  2849. // path: "/", // Defaults to current page
  2850. // domain: "jquery.com",
  2851. // secure: true
  2852. },
  2853. // Class names used, when rendering the HTML markup.
  2854. // Note: if only single entries are passed for options.classNames, all other
  2855. // values are still set to default.
  2856. classNames: {
  2857. container: "dt-container",
  2858. node: "dt-node",
  2859. folder: "dt-folder",
  2860. // document: "dt-document",
  2861. empty: "dt-empty",
  2862. vline: "dt-vline",
  2863. expander: "dt-expander",
  2864. // connector: "dt-connector",
  2865. connector: "dt-icon",
  2866. checkbox: "dt-checkbox",
  2867. nodeIcon: false,
  2868. //nodeIcon: "dt-icon",
  2869. title: "dt-title",
  2870. noConnector: "dt-no-connector",
  2871. nodeError: "dt-statusnode-error",
  2872. nodeWait: "dt-statusnode-wait",
  2873. hidden: "dt-hidden",
  2874. combinedExpanderPrefix: "dt-exp-",
  2875. combinedIconPrefix: "dt-ico-",
  2876. nodeLoading: "dt-loading",
  2877. // disabled: "dt-disabled",
  2878. hasChildren: "dt-has-children",
  2879. active: "dt-active",
  2880. selected: "dt-selected",
  2881. expanded: "dt-expanded",
  2882. lazy: "dt-lazy",
  2883. focused: "dt-focused",
  2884. partsel: "dt-partsel",
  2885. lastsib: "dt-lastsib"
  2886. },
  2887. debugLevel: 1,
  2888. // ------------------------------------------------------------------------
  2889. lastentry: undefined
  2890. };
  2891. //
  2892. if( parseFloat($.ui.version) < 1.8 ) {
  2893. $.ui.dynatree.defaults = $.ui.dynatree.prototype.options;
  2894. }
  2895. /*******************************************************************************
  2896. * Reserved data attributes for a tree node.
  2897. */
  2898. $.ui.dynatree.nodedatadefaults = {
  2899. title: null, // (required) Displayed name of the node (html is allowed here)
  2900. key: null, // May be used with activate(), select(), find(), ...
  2901. isFolder: true, // Use a folder icon. Also the node is expandable but not selectable.
  2902. isLazy: true, // Call onLazyRead(), when the node is expanded for the first time to allow for delayed creation of children.
  2903. tooltip: null, // Show this popup text.
  2904. icon: null, // Use a custom image (filename relative to tree.options.imagePath). 'null' for default icon, 'false' for no icon.
  2905. addClass: null, // Class name added to the node's span tag.
  2906. noLink: false, // Use <span> instead of <a> tag for this node
  2907. activate: false, // Initial active status.
  2908. focus: false, // Initial focused status.
  2909. expand: false, // Initial expanded status.
  2910. select: false, // Initial selected status.
  2911. hideCheckbox: false, // Suppress checkbox display for this node.
  2912. unselectable: false, // Prevent selection.
  2913. // disabled: false,
  2914. // The following attributes are only valid if passed to some functions:
  2915. children: null, // Array of child nodes.
  2916. // NOTE: we can also add custom attributes here.
  2917. // This may then also be used in the onActivate(), onSelect() or onLazyTree() callbacks.
  2918. // ------------------------------------------------------------------------
  2919. lastentry: undefined
  2920. };
  2921. /*******************************************************************************
  2922. * Drag and drop support
  2923. */
  2924. function _initDragAndDrop(tree) {
  2925. var dnd = tree.options.dnd || null;
  2926. // Register 'connectToDynatree' option with ui.draggable
  2927. if(dnd && (dnd.onDragStart || dnd.onDrop)) {
  2928. _registerDnd();
  2929. }
  2930. // Attach ui.draggable to this Dynatree instance
  2931. if(dnd && dnd.onDragStart ) {
  2932. tree.$tree.draggable({
  2933. addClasses: false,
  2934. appendTo: "body",
  2935. containment: false,
  2936. delay: 0,
  2937. distance: 4,
  2938. revert: false,
  2939. // Delegate draggable.start, drag, and stop events to our handler
  2940. connectToDynatree: true,
  2941. // Let source tree create the helper element
  2942. helper: function(event) {
  2943. var sourceNode = getDtNodeFromElement(event.target);
  2944. return sourceNode.tree._onDragEvent("helper", sourceNode, null, event, null, null);
  2945. },
  2946. _last: null
  2947. });
  2948. }
  2949. // Attach ui.droppable to this Dynatree instance
  2950. if(dnd && dnd.onDrop) {
  2951. tree.$tree.droppable({
  2952. addClasses: false,
  2953. tolerance: "intersect",
  2954. greedy: false,
  2955. _last: null
  2956. });
  2957. }
  2958. }
  2959. //--- Extend ui.draggable event handling --------------------------------------
  2960. var didRegisterDnd = false;
  2961. var _registerDnd = function() {
  2962. if(didRegisterDnd){
  2963. return;
  2964. }
  2965. $.ui.plugin.add("draggable", "connectToDynatree", {
  2966. start: function(event, ui) {
  2967. var draggable = $(this).data("draggable");
  2968. var sourceNode = ui.helper.data("dtSourceNode") || null;
  2969. // logMsg("draggable-connectToDynatree.start, %s", sourceNode);
  2970. // logMsg(" this: %o", this);
  2971. // logMsg(" event: %o", event);
  2972. // logMsg(" draggable: %o", draggable);
  2973. // logMsg(" ui: %o", ui);
  2974. if(sourceNode) {
  2975. // Adjust helper offset, so cursor is slightly outside top/left corner
  2976. // draggable.offset.click.top -= event.target.offsetTop;
  2977. // draggable.offset.click.left -= event.target.offsetLeft;
  2978. draggable.offset.click.top = -2;
  2979. draggable.offset.click.left = + 16;
  2980. // logMsg(" draggable.offset.click FIXED: %s/%s", draggable.offset.click.left, draggable.offset.click.top);
  2981. // Trigger onDragStart event
  2982. // TODO: when called as connectTo..., the return value is ignored(?)
  2983. return sourceNode.tree._onDragEvent("start", sourceNode, null, event, ui, draggable);
  2984. }
  2985. },
  2986. drag: function(event, ui) {
  2987. var draggable = $(this).data("draggable");
  2988. var sourceNode = ui.helper.data("dtSourceNode") || null;
  2989. var prevTargetNode = ui.helper.data("dtTargetNode") || null;
  2990. var targetNode = getDtNodeFromElement(event.target);
  2991. // logMsg("getDtNodeFromElement(%o): %s", event.target, targetNode);
  2992. if(event.target && !targetNode){
  2993. // We got a drag event, but the targetNode could not be found
  2994. // at the event location. This may happen, if the mouse
  2995. // jumped over the drag helper, in which case we ignore it:
  2996. var isHelper = $(event.target).closest("div.dt-drag-helper,#dt-drop-marker").length > 0;
  2997. if(isHelper){
  2998. // logMsg("Drag event over helper: ignored.");
  2999. return;
  3000. }
  3001. }
  3002. // logMsg("draggable-connectToDynatree.drag: targetNode(from event): %s, dtTargetNode: %s", targetNode, ui.helper.data("dtTargetNode"));
  3003. ui.helper.data("dtTargetNode", targetNode);
  3004. // Leaving a tree node
  3005. if(prevTargetNode && prevTargetNode !== targetNode ) {
  3006. prevTargetNode.tree._onDragEvent("leave", prevTargetNode, sourceNode, event, ui, draggable);
  3007. }
  3008. if(targetNode){
  3009. if(!targetNode.tree.options.dnd.onDrop) {
  3010. // not enabled as drop target
  3011. noop(); // Keep JSLint happy
  3012. } else if(targetNode === prevTargetNode) {
  3013. // Moving over same node
  3014. targetNode.tree._onDragEvent("over", targetNode, sourceNode, event, ui, draggable);
  3015. }else{
  3016. // Entering this node first time
  3017. targetNode.tree._onDragEvent("enter", targetNode, sourceNode, event, ui, draggable);
  3018. }
  3019. }
  3020. // else go ahead with standard event handling
  3021. },
  3022. stop: function(event, ui) {
  3023. var draggable = $(this).data("draggable");
  3024. var sourceNode = ui.helper.data("dtSourceNode") || null;
  3025. var targetNode = ui.helper.data("dtTargetNode") || null;
  3026. // logMsg("draggable-connectToDynatree.stop: targetNode(from event): %s, dtTargetNode: %s", targetNode, ui.helper.data("dtTargetNode"));
  3027. // logMsg("draggable-connectToDynatree.stop, %s", sourceNode);
  3028. var mouseDownEvent = draggable._mouseDownEvent;
  3029. var eventType = event.type;
  3030. // logMsg(" type: %o, downEvent: %o, upEvent: %o", eventType, mouseDownEvent, event);
  3031. // logMsg(" targetNode: %o", targetNode);
  3032. var dropped = (eventType == "mouseup" && event.which == 1);
  3033. if(!dropped){
  3034. logMsg("Drag was cancelled");
  3035. }
  3036. if(targetNode) {
  3037. if(dropped){
  3038. targetNode.tree._onDragEvent("drop", targetNode, sourceNode, event, ui, draggable);
  3039. }
  3040. targetNode.tree._onDragEvent("leave", targetNode, sourceNode, event, ui, draggable);
  3041. }
  3042. if(sourceNode){
  3043. sourceNode.tree._onDragEvent("stop", sourceNode, null, event, ui, draggable);
  3044. }
  3045. }
  3046. });
  3047. didRegisterDnd = true;
  3048. };
  3049. // ---------------------------------------------------------------------------
  3050. })(jQuery);