jstree.js 162 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970
  1. /*!
  2. * jsTree {{VERSION}}
  3. * http://jstree.com/
  4. *
  5. * Copyright (c) 2014 Ivan Bozhanov (http://vakata.com)
  6. *
  7. * Licensed same as jquery - under the terms of the MIT License
  8. * http://www.opensource.org/licenses/mit-license.php
  9. */
  10. /*!
  11. * if using jslint please allow for the jQuery global and use following options:
  12. * jslint: loopfunc: true, browser: true, ass: true, bitwise: true, continue: true, nomen: true, plusplus: true, regexp: true, unparam: true, todo: true, white: true
  13. */
  14. /*jshint -W083 */
  15. /*globals jQuery, define, module, exports, require, window, document, postMessage */
  16. (function (factory) {
  17. "use strict";
  18. if (typeof define === 'function' && define.amd) {
  19. define(['jquery'], factory);
  20. }
  21. else if(typeof module !== 'undefined' && module.exports) {
  22. module.exports = factory(require('jquery'));
  23. }
  24. else {
  25. factory(jQuery);
  26. }
  27. }(function ($, undefined) {
  28. "use strict";
  29. // prevent another load? maybe there is a better way?
  30. if($.jstree) {
  31. return;
  32. }
  33. /**
  34. * ### jsTree core functionality
  35. */
  36. // internal variables
  37. var instance_counter = 0,
  38. ccp_node = false,
  39. ccp_mode = false,
  40. ccp_inst = false,
  41. themes_loaded = [],
  42. src = $('script:last').attr('src'),
  43. document = window.document; // local variable is always faster to access then a global
  44. var setImmediate = window.setImmediate;
  45. var Promise = window.Promise;
  46. if (!setImmediate && Promise) {
  47. // Good enough approximation of setImmediate
  48. setImmediate = function (cb, arg) {
  49. Promise.resolve(arg).then(cb);
  50. };
  51. }
  52. /**
  53. * holds all jstree related functions and variables, including the actual class and methods to create, access and manipulate instances.
  54. * @name $.jstree
  55. */
  56. $.jstree = {
  57. /**
  58. * specifies the jstree version in use
  59. * @name $.jstree.version
  60. */
  61. version : '{{VERSION}}',
  62. /**
  63. * holds all the default options used when creating new instances
  64. * @name $.jstree.defaults
  65. */
  66. defaults : {
  67. /**
  68. * configure which plugins will be active on an instance. Should be an array of strings, where each element is a plugin name. The default is `[]`
  69. * @name $.jstree.defaults.plugins
  70. */
  71. plugins : []
  72. },
  73. /**
  74. * stores all loaded jstree plugins (used internally)
  75. * @name $.jstree.plugins
  76. */
  77. plugins : {},
  78. path : src && src.indexOf('/') !== -1 ? src.replace(/\/[^\/]+$/,'') : '',
  79. idregex : /[\\:&!^|()\[\]<>@*'+~#";.,=\- \/${}%?`]/g,
  80. root : '#'
  81. };
  82. /**
  83. * creates a jstree instance
  84. * @name $.jstree.create(el [, options])
  85. * @param {DOMElement|jQuery|String} el the element to create the instance on, can be jQuery extended or a selector
  86. * @param {Object} options options for this instance (extends `$.jstree.defaults`)
  87. * @return {jsTree} the new instance
  88. */
  89. $.jstree.create = function (el, options) {
  90. var tmp = new $.jstree.core(++instance_counter),
  91. opt = options;
  92. options = $.extend(true, {}, $.jstree.defaults, options);
  93. if(opt && opt.plugins) {
  94. options.plugins = opt.plugins;
  95. }
  96. $.each(options.plugins, function (i, k) {
  97. if(i !== 'core') {
  98. tmp = tmp.plugin(k, options[k]);
  99. }
  100. });
  101. $(el).data('jstree', tmp);
  102. tmp.init(el, options);
  103. return tmp;
  104. };
  105. /**
  106. * remove all traces of jstree from the DOM and destroy all instances
  107. * @name $.jstree.destroy()
  108. */
  109. $.jstree.destroy = function () {
  110. $('.jstree:jstree').jstree('destroy');
  111. $(document).off('.jstree');
  112. };
  113. /**
  114. * the jstree class constructor, used only internally
  115. * @private
  116. * @name $.jstree.core(id)
  117. * @param {Number} id this instance's index
  118. */
  119. $.jstree.core = function (id) {
  120. this._id = id;
  121. this._cnt = 0;
  122. this._wrk = null;
  123. this._data = {
  124. core : {
  125. themes : {
  126. name : false,
  127. dots : false,
  128. icons : false,
  129. ellipsis : false
  130. },
  131. selected : [],
  132. last_error : {},
  133. working : false,
  134. worker_queue : [],
  135. focused : null
  136. }
  137. };
  138. };
  139. /**
  140. * get a reference to an existing instance
  141. *
  142. * __Examples__
  143. *
  144. * // provided a container with an ID of "tree", and a nested node with an ID of "branch"
  145. * // all of there will return the same instance
  146. * $.jstree.reference('tree');
  147. * $.jstree.reference('#tree');
  148. * $.jstree.reference($('#tree'));
  149. * $.jstree.reference(document.getElementByID('tree'));
  150. * $.jstree.reference('branch');
  151. * $.jstree.reference('#branch');
  152. * $.jstree.reference($('#branch'));
  153. * $.jstree.reference(document.getElementByID('branch'));
  154. *
  155. * @name $.jstree.reference(needle)
  156. * @param {DOMElement|jQuery|String} needle
  157. * @return {jsTree|null} the instance or `null` if not found
  158. */
  159. $.jstree.reference = function (needle) {
  160. var tmp = null,
  161. obj = null;
  162. if(needle && needle.id && (!needle.tagName || !needle.nodeType)) { needle = needle.id; }
  163. if(!obj || !obj.length) {
  164. try { obj = $(needle); } catch (ignore) { }
  165. }
  166. if(!obj || !obj.length) {
  167. try { obj = $('#' + needle.replace($.jstree.idregex,'\\$&')); } catch (ignore) { }
  168. }
  169. if(obj && obj.length && (obj = obj.closest('.jstree')).length && (obj = obj.data('jstree'))) {
  170. tmp = obj;
  171. }
  172. else {
  173. $('.jstree').each(function () {
  174. var inst = $(this).data('jstree');
  175. if(inst && inst._model.data[needle]) {
  176. tmp = inst;
  177. return false;
  178. }
  179. });
  180. }
  181. return tmp;
  182. };
  183. /**
  184. * Create an instance, get an instance or invoke a command on a instance.
  185. *
  186. * If there is no instance associated with the current node a new one is created and `arg` is used to extend `$.jstree.defaults` for this new instance. There would be no return value (chaining is not broken).
  187. *
  188. * If there is an existing instance and `arg` is a string the command specified by `arg` is executed on the instance, with any additional arguments passed to the function. If the function returns a value it will be returned (chaining could break depending on function).
  189. *
  190. * If there is an existing instance and `arg` is not a string the instance itself is returned (similar to `$.jstree.reference`).
  191. *
  192. * In any other case - nothing is returned and chaining is not broken.
  193. *
  194. * __Examples__
  195. *
  196. * $('#tree1').jstree(); // creates an instance
  197. * $('#tree2').jstree({ plugins : [] }); // create an instance with some options
  198. * $('#tree1').jstree('open_node', '#branch_1'); // call a method on an existing instance, passing additional arguments
  199. * $('#tree2').jstree(); // get an existing instance (or create an instance)
  200. * $('#tree2').jstree(true); // get an existing instance (will not create new instance)
  201. * $('#branch_1').jstree().select_node('#branch_1'); // get an instance (using a nested element and call a method)
  202. *
  203. * @name $().jstree([arg])
  204. * @param {String|Object} arg
  205. * @return {Mixed}
  206. */
  207. $.fn.jstree = function (arg) {
  208. // check for string argument
  209. var is_method = (typeof arg === 'string'),
  210. args = Array.prototype.slice.call(arguments, 1),
  211. result = null;
  212. if(arg === true && !this.length) { return false; }
  213. this.each(function () {
  214. // get the instance (if there is one) and method (if it exists)
  215. var instance = $.jstree.reference(this),
  216. method = is_method && instance ? instance[arg] : null;
  217. // if calling a method, and method is available - execute on the instance
  218. result = is_method && method ?
  219. method.apply(instance, args) :
  220. null;
  221. // if there is no instance and no method is being called - create one
  222. if(!instance && !is_method && (arg === undefined || $.isPlainObject(arg))) {
  223. $.jstree.create(this, arg);
  224. }
  225. // if there is an instance and no method is called - return the instance
  226. if( (instance && !is_method) || arg === true ) {
  227. result = instance || false;
  228. }
  229. // if there was a method call which returned a result - break and return the value
  230. if(result !== null && result !== undefined) {
  231. return false;
  232. }
  233. });
  234. // if there was a method call with a valid return value - return that, otherwise continue the chain
  235. return result !== null && result !== undefined ?
  236. result : this;
  237. };
  238. /**
  239. * used to find elements containing an instance
  240. *
  241. * __Examples__
  242. *
  243. * $('div:jstree').each(function () {
  244. * $(this).jstree('destroy');
  245. * });
  246. *
  247. * @name $(':jstree')
  248. * @return {jQuery}
  249. */
  250. $.expr.pseudos.jstree = $.expr.createPseudo(function(search) {
  251. return function(a) {
  252. return $(a).hasClass('jstree') &&
  253. $(a).data('jstree') !== undefined;
  254. };
  255. });
  256. /**
  257. * stores all defaults for the core
  258. * @name $.jstree.defaults.core
  259. */
  260. $.jstree.defaults.core = {
  261. /**
  262. * data configuration
  263. *
  264. * If left as `false` the HTML inside the jstree container element is used to populate the tree (that should be an unordered list with list items).
  265. *
  266. * You can also pass in a HTML string or a JSON array here.
  267. *
  268. * It is possible to pass in a standard jQuery-like AJAX config and jstree will automatically determine if the response is JSON or HTML and use that to populate the tree.
  269. * In addition to the standard jQuery ajax options here you can suppy functions for `data` and `url`, the functions will be run in the current instance's scope and a param will be passed indicating which node is being loaded, the return value of those functions will be used.
  270. *
  271. * The last option is to specify a function, that function will receive the node being loaded as argument and a second param which is a function which should be called with the result.
  272. *
  273. * __Examples__
  274. *
  275. * // AJAX
  276. * $('#tree').jstree({
  277. * 'core' : {
  278. * 'data' : {
  279. * 'url' : '/get/children/',
  280. * 'data' : function (node) {
  281. * return { 'id' : node.id };
  282. * }
  283. * }
  284. * });
  285. *
  286. * // direct data
  287. * $('#tree').jstree({
  288. * 'core' : {
  289. * 'data' : [
  290. * 'Simple root node',
  291. * {
  292. * 'id' : 'node_2',
  293. * 'text' : 'Root node with options',
  294. * 'state' : { 'opened' : true, 'selected' : true },
  295. * 'children' : [ { 'text' : 'Child 1' }, 'Child 2']
  296. * }
  297. * ]
  298. * }
  299. * });
  300. *
  301. * // function
  302. * $('#tree').jstree({
  303. * 'core' : {
  304. * 'data' : function (obj, callback) {
  305. * callback.call(this, ['Root 1', 'Root 2']);
  306. * }
  307. * });
  308. *
  309. * @name $.jstree.defaults.core.data
  310. */
  311. data : false,
  312. /**
  313. * configure the various strings used throughout the tree
  314. *
  315. * You can use an object where the key is the string you need to replace and the value is your replacement.
  316. * Another option is to specify a function which will be called with an argument of the needed string and should return the replacement.
  317. * If left as `false` no replacement is made.
  318. *
  319. * __Examples__
  320. *
  321. * $('#tree').jstree({
  322. * 'core' : {
  323. * 'strings' : {
  324. * 'Loading ...' : 'Please wait ...'
  325. * }
  326. * }
  327. * });
  328. *
  329. * @name $.jstree.defaults.core.strings
  330. */
  331. strings : false,
  332. /**
  333. * determines what happens when a user tries to modify the structure of the tree
  334. * If left as `false` all operations like create, rename, delete, move or copy are prevented.
  335. * You can set this to `true` to allow all interactions or use a function to have better control.
  336. *
  337. * __Examples__
  338. *
  339. * $('#tree').jstree({
  340. * 'core' : {
  341. * 'check_callback' : function (operation, node, node_parent, node_position, more) {
  342. * // operation can be 'create_node', 'rename_node', 'delete_node', 'move_node', 'copy_node' or 'edit'
  343. * // in case of 'rename_node' node_position is filled with the new node name
  344. * return operation === 'rename_node' ? true : false;
  345. * }
  346. * }
  347. * });
  348. *
  349. * @name $.jstree.defaults.core.check_callback
  350. */
  351. check_callback : false,
  352. /**
  353. * a callback called with a single object parameter in the instance's scope when something goes wrong (operation prevented, ajax failed, etc)
  354. * @name $.jstree.defaults.core.error
  355. */
  356. error : $.noop,
  357. /**
  358. * the open / close animation duration in milliseconds - set this to `false` to disable the animation (default is `200`)
  359. * @name $.jstree.defaults.core.animation
  360. */
  361. animation : 200,
  362. /**
  363. * a boolean indicating if multiple nodes can be selected
  364. * @name $.jstree.defaults.core.multiple
  365. */
  366. multiple : true,
  367. /**
  368. * theme configuration object
  369. * @name $.jstree.defaults.core.themes
  370. */
  371. themes : {
  372. /**
  373. * the name of the theme to use (if left as `false` the default theme is used)
  374. * @name $.jstree.defaults.core.themes.name
  375. */
  376. name : false,
  377. /**
  378. * the URL of the theme's CSS file, leave this as `false` if you have manually included the theme CSS (recommended). You can set this to `true` too which will try to autoload the theme.
  379. * @name $.jstree.defaults.core.themes.url
  380. */
  381. url : false,
  382. /**
  383. * the location of all jstree themes - only used if `url` is set to `true`
  384. * @name $.jstree.defaults.core.themes.dir
  385. */
  386. dir : false,
  387. /**
  388. * a boolean indicating if connecting dots are shown
  389. * @name $.jstree.defaults.core.themes.dots
  390. */
  391. dots : true,
  392. /**
  393. * a boolean indicating if node icons are shown
  394. * @name $.jstree.defaults.core.themes.icons
  395. */
  396. icons : true,
  397. /**
  398. * a boolean indicating if node ellipsis should be shown - this only works with a fixed with on the container
  399. * @name $.jstree.defaults.core.themes.ellipsis
  400. */
  401. ellipsis : false,
  402. /**
  403. * a boolean indicating if the tree background is striped
  404. * @name $.jstree.defaults.core.themes.stripes
  405. */
  406. stripes : false,
  407. /**
  408. * a string (or boolean `false`) specifying the theme variant to use (if the theme supports variants)
  409. * @name $.jstree.defaults.core.themes.variant
  410. */
  411. variant : false,
  412. /**
  413. * a boolean specifying if a reponsive version of the theme should kick in on smaller screens (if the theme supports it). Defaults to `false`.
  414. * @name $.jstree.defaults.core.themes.responsive
  415. */
  416. responsive : false
  417. },
  418. /**
  419. * if left as `true` all parents of all selected nodes will be opened once the tree loads (so that all selected nodes are visible to the user)
  420. * @name $.jstree.defaults.core.expand_selected_onload
  421. */
  422. expand_selected_onload : true,
  423. /**
  424. * if left as `true` web workers will be used to parse incoming JSON data where possible, so that the UI will not be blocked by large requests. Workers are however about 30% slower. Defaults to `true`
  425. * @name $.jstree.defaults.core.worker
  426. */
  427. worker : true,
  428. /**
  429. * Force node text to plain text (and escape HTML). Defaults to `false`
  430. * @name $.jstree.defaults.core.force_text
  431. */
  432. force_text : false,
  433. /**
  434. * Should the node be toggled if the text is double clicked. Defaults to `true`
  435. * @name $.jstree.defaults.core.dblclick_toggle
  436. */
  437. dblclick_toggle : true,
  438. /**
  439. * Should the loaded nodes be part of the state. Defaults to `false`
  440. * @name $.jstree.defaults.core.loaded_state
  441. */
  442. loaded_state : false,
  443. /**
  444. * Should the last active node be focused when the tree container is blurred and the focused again. This helps working with screen readers. Defaults to `true`
  445. * @name $.jstree.defaults.core.restore_focus
  446. */
  447. restore_focus : true,
  448. /**
  449. * Force to compute and set "aria-setsize" and "aria-posinset" explicitly for each treeitem.
  450. * Some browsers may compute incorrect elements position and produce wrong announcements for screen readers. Defaults to `false`
  451. * @name $.jstree.defaults.core.compute_elements_positions
  452. */
  453. compute_elements_positions : false,
  454. /**
  455. * Default keyboard shortcuts (an object where each key is the button name or combo - like 'enter', 'ctrl-space', 'p', etc and the value is the function to execute in the instance's scope)
  456. * @name $.jstree.defaults.core.keyboard
  457. */
  458. keyboard : {
  459. 'ctrl-space': function (e) {
  460. // aria defines space only with Ctrl
  461. e.type = "click";
  462. $(e.currentTarget).trigger(e);
  463. },
  464. 'enter': function (e) {
  465. // enter
  466. e.type = "click";
  467. $(e.currentTarget).trigger(e);
  468. },
  469. 'left': function (e) {
  470. // left
  471. e.preventDefault();
  472. if(this.is_open(e.currentTarget)) {
  473. this.close_node(e.currentTarget);
  474. }
  475. else {
  476. var o = this.get_parent(e.currentTarget);
  477. if(o && o.id !== $.jstree.root) { this.get_node(o, true).children('.jstree-anchor').focus(); }
  478. }
  479. },
  480. 'up': function (e) {
  481. // up
  482. e.preventDefault();
  483. var o = this.get_prev_dom(e.currentTarget);
  484. if(o && o.length) { o.children('.jstree-anchor').focus(); }
  485. },
  486. 'right': function (e) {
  487. // right
  488. e.preventDefault();
  489. if(this.is_closed(e.currentTarget)) {
  490. this.open_node(e.currentTarget, function (o) { this.get_node(o, true).children('.jstree-anchor').focus(); });
  491. }
  492. else if (this.is_open(e.currentTarget)) {
  493. var o = this.get_node(e.currentTarget, true).children('.jstree-children')[0];
  494. if(o) { $(this._firstChild(o)).children('.jstree-anchor').focus(); }
  495. }
  496. },
  497. 'down': function (e) {
  498. // down
  499. e.preventDefault();
  500. var o = this.get_next_dom(e.currentTarget);
  501. if(o && o.length) { o.children('.jstree-anchor').focus(); }
  502. },
  503. '*': function (e) {
  504. // aria defines * on numpad as open_all - not very common
  505. this.open_all();
  506. },
  507. 'home': function (e) {
  508. // home
  509. e.preventDefault();
  510. var o = this._firstChild(this.get_container_ul()[0]);
  511. if(o) { $(o).children('.jstree-anchor').filter(':visible').focus(); }
  512. },
  513. 'end': function (e) {
  514. // end
  515. e.preventDefault();
  516. this.element.find('.jstree-anchor').filter(':visible').last().focus();
  517. },
  518. 'f2': function (e) {
  519. // f2 - safe to include - if check_callback is false it will fail
  520. e.preventDefault();
  521. this.edit(e.currentTarget);
  522. }
  523. }
  524. };
  525. $.jstree.core.prototype = {
  526. /**
  527. * used to decorate an instance with a plugin. Used internally.
  528. * @private
  529. * @name plugin(deco [, opts])
  530. * @param {String} deco the plugin to decorate with
  531. * @param {Object} opts options for the plugin
  532. * @return {jsTree}
  533. */
  534. plugin : function (deco, opts) {
  535. var Child = $.jstree.plugins[deco];
  536. if(Child) {
  537. this._data[deco] = {};
  538. Child.prototype = this;
  539. return new Child(opts, this);
  540. }
  541. return this;
  542. },
  543. /**
  544. * initialize the instance. Used internally.
  545. * @private
  546. * @name init(el, optons)
  547. * @param {DOMElement|jQuery|String} el the element we are transforming
  548. * @param {Object} options options for this instance
  549. * @trigger init.jstree, loading.jstree, loaded.jstree, ready.jstree, changed.jstree
  550. */
  551. init : function (el, options) {
  552. this._model = {
  553. data : {},
  554. changed : [],
  555. force_full_redraw : false,
  556. redraw_timeout : false,
  557. default_state : {
  558. loaded : true,
  559. opened : false,
  560. selected : false,
  561. disabled : false
  562. }
  563. };
  564. this._model.data[$.jstree.root] = {
  565. id : $.jstree.root,
  566. parent : null,
  567. parents : [],
  568. children : [],
  569. children_d : [],
  570. state : { loaded : false }
  571. };
  572. this.element = $(el).addClass('jstree jstree-' + this._id);
  573. this.settings = options;
  574. this._data.core.ready = false;
  575. this._data.core.loaded = false;
  576. this._data.core.rtl = (this.element.css("direction") === "rtl");
  577. this.element[this._data.core.rtl ? 'addClass' : 'removeClass']("jstree-rtl");
  578. this.element.attr('role','tree');
  579. if(this.settings.core.multiple) {
  580. this.element.attr('aria-multiselectable', true);
  581. }
  582. if(!this.element.attr('tabindex')) {
  583. this.element.attr('tabindex','0');
  584. }
  585. this.bind();
  586. /**
  587. * triggered after all events are bound
  588. * @event
  589. * @name init.jstree
  590. */
  591. this.trigger("init");
  592. this._data.core.original_container_html = this.element.find(" > ul > li").clone(true);
  593. this._data.core.original_container_html
  594. .find("li").addBack()
  595. .contents().filter(function() {
  596. return this.nodeType === 3 && (!this.nodeValue || /^\s+$/.test(this.nodeValue));
  597. })
  598. .remove();
  599. this.element.html("<"+"ul class='jstree-container-ul jstree-children' role='group'><"+"li id='j"+this._id+"_loading' class='jstree-initial-node jstree-loading jstree-leaf jstree-last' role='none'><i class='jstree-icon jstree-ocl'></i><"+"a class='jstree-anchor' role='treeitem' href='#'><i class='jstree-icon jstree-themeicon-hidden'></i>" + this.get_string("Loading ...") + "</a></li></ul>");
  600. this.element.attr('aria-activedescendant','j' + this._id + '_loading');
  601. this._data.core.li_height = this.get_container_ul().children("li").first().outerHeight() || 24;
  602. this._data.core.node = this._create_prototype_node();
  603. /**
  604. * triggered after the loading text is shown and before loading starts
  605. * @event
  606. * @name loading.jstree
  607. */
  608. this.trigger("loading");
  609. this.load_node($.jstree.root);
  610. },
  611. /**
  612. * destroy an instance
  613. * @name destroy()
  614. * @param {Boolean} keep_html if not set to `true` the container will be emptied, otherwise the current DOM elements will be kept intact
  615. */
  616. destroy : function (keep_html) {
  617. /**
  618. * triggered before the tree is destroyed
  619. * @event
  620. * @name destroy.jstree
  621. */
  622. this.trigger("destroy");
  623. if(this._wrk) {
  624. try {
  625. window.URL.revokeObjectURL(this._wrk);
  626. this._wrk = null;
  627. }
  628. catch (ignore) { }
  629. }
  630. if(!keep_html) { this.element.empty(); }
  631. this.teardown();
  632. },
  633. /**
  634. * Create a prototype node
  635. * @name _create_prototype_node()
  636. * @return {DOMElement}
  637. */
  638. _create_prototype_node : function () {
  639. var _node = document.createElement('LI'), _temp1, _temp2;
  640. _node.setAttribute('role', 'none');
  641. _temp1 = document.createElement('I');
  642. _temp1.className = 'jstree-icon jstree-ocl';
  643. _temp1.setAttribute('role', 'presentation');
  644. _node.appendChild(_temp1);
  645. _temp1 = document.createElement('A');
  646. _temp1.className = 'jstree-anchor';
  647. _temp1.setAttribute('href','#');
  648. _temp1.setAttribute('tabindex','-1');
  649. _temp1.setAttribute('role', 'treeitem');
  650. _temp2 = document.createElement('I');
  651. _temp2.className = 'jstree-icon jstree-themeicon';
  652. _temp2.setAttribute('role', 'presentation');
  653. _temp1.appendChild(_temp2);
  654. _node.appendChild(_temp1);
  655. _temp1 = _temp2 = null;
  656. return _node;
  657. },
  658. _kbevent_to_func : function (e) {
  659. var keys = {
  660. 8: "Backspace", 9: "Tab", 13: "Enter", 19: "Pause", 27: "Esc",
  661. 32: "Space", 33: "PageUp", 34: "PageDown", 35: "End", 36: "Home",
  662. 37: "Left", 38: "Up", 39: "Right", 40: "Down", 44: "Print", 45: "Insert",
  663. 46: "Delete", 96: "Numpad0", 97: "Numpad1", 98: "Numpad2", 99 : "Numpad3",
  664. 100: "Numpad4", 101: "Numpad5", 102: "Numpad6", 103: "Numpad7",
  665. 104: "Numpad8", 105: "Numpad9", '-13': "NumpadEnter", 112: "F1",
  666. 113: "F2", 114: "F3", 115: "F4", 116: "F5", 117: "F6", 118: "F7",
  667. 119: "F8", 120: "F9", 121: "F10", 122: "F11", 123: "F12", 144: "Numlock",
  668. 145: "Scrolllock", 16: 'Shift', 17: 'Ctrl', 18: 'Alt',
  669. 48: '0', 49: '1', 50: '2', 51: '3', 52: '4', 53: '5',
  670. 54: '6', 55: '7', 56: '8', 57: '9', 59: ';', 61: '=', 65: 'a',
  671. 66: 'b', 67: 'c', 68: 'd', 69: 'e', 70: 'f', 71: 'g', 72: 'h',
  672. 73: 'i', 74: 'j', 75: 'k', 76: 'l', 77: 'm', 78: 'n', 79: 'o',
  673. 80: 'p', 81: 'q', 82: 'r', 83: 's', 84: 't', 85: 'u', 86: 'v',
  674. 87: 'w', 88: 'x', 89: 'y', 90: 'z', 107: '+', 109: '-', 110: '.',
  675. 186: ';', 187: '=', 188: ',', 189: '-', 190: '.', 191: '/', 192: '`',
  676. 219: '[', 220: '\\',221: ']', 222: "'", 111: '/', 106: '*', 173: '-'
  677. };
  678. var parts = [];
  679. if (e.ctrlKey) { parts.push('ctrl'); }
  680. if (e.altKey) { parts.push('alt'); }
  681. if (e.shiftKey) { parts.push('shift'); }
  682. parts.push(keys[e.which] || e.which);
  683. parts = parts.sort().join('-').toLowerCase();
  684. if (parts === 'shift-shift' || parts === 'ctrl-ctrl' || parts === 'alt-alt') {
  685. return null;
  686. }
  687. var kb = this.settings.core.keyboard, i, tmp;
  688. for (i in kb) {
  689. if (kb.hasOwnProperty(i)) {
  690. tmp = i;
  691. if (tmp !== '-' && tmp !== '+') {
  692. tmp = tmp.replace('--', '-MINUS').replace('+-', '-MINUS').replace('++', '-PLUS').replace('-+', '-PLUS');
  693. tmp = tmp.split(/-|\+/).sort().join('-').replace('MINUS', '-').replace('PLUS', '+').toLowerCase();
  694. }
  695. if (tmp === parts) {
  696. return kb[i];
  697. }
  698. }
  699. }
  700. return null;
  701. },
  702. /**
  703. * part of the destroying of an instance. Used internally.
  704. * @private
  705. * @name teardown()
  706. */
  707. teardown : function () {
  708. this.unbind();
  709. this.element
  710. .removeClass('jstree')
  711. .removeData('jstree')
  712. .find("[class^='jstree']")
  713. .addBack()
  714. .attr("class", function () { return this.className.replace(/jstree[^ ]*|$/ig,''); });
  715. this.element = null;
  716. },
  717. /**
  718. * bind all events. Used internally.
  719. * @private
  720. * @name bind()
  721. */
  722. bind : function () {
  723. var word = '',
  724. tout = null,
  725. was_click = 0;
  726. this.element
  727. .on("dblclick.jstree", function (e) {
  728. if(e.target.tagName && e.target.tagName.toLowerCase() === "input") { return true; }
  729. if(document.selection && document.selection.empty) {
  730. document.selection.empty();
  731. }
  732. else {
  733. if(window.getSelection) {
  734. var sel = window.getSelection();
  735. try {
  736. sel.removeAllRanges();
  737. sel.collapse();
  738. } catch (ignore) { }
  739. }
  740. }
  741. })
  742. .on("mousedown.jstree", $.proxy(function (e) {
  743. if(e.target === this.element[0]) {
  744. e.preventDefault(); // prevent losing focus when clicking scroll arrows (FF, Chrome)
  745. was_click = +(new Date()); // ie does not allow to prevent losing focus
  746. }
  747. }, this))
  748. .on("mousedown.jstree", ".jstree-ocl", function (e) {
  749. e.preventDefault(); // prevent any node inside from losing focus when clicking the open/close icon
  750. })
  751. .on("click.jstree", ".jstree-ocl", $.proxy(function (e) {
  752. this.toggle_node(e.target);
  753. }, this))
  754. .on("dblclick.jstree", ".jstree-anchor", $.proxy(function (e) {
  755. if(e.target.tagName && e.target.tagName.toLowerCase() === "input") { return true; }
  756. if(this.settings.core.dblclick_toggle) {
  757. this.toggle_node(e.target);
  758. }
  759. }, this))
  760. .on("click.jstree", ".jstree-anchor", $.proxy(function (e) {
  761. e.preventDefault();
  762. if(e.currentTarget !== document.activeElement) { $(e.currentTarget).focus(); }
  763. this.activate_node(e.currentTarget, e);
  764. }, this))
  765. .on('keydown.jstree', '.jstree-anchor', $.proxy(function (e) {
  766. if(e.target.tagName && e.target.tagName.toLowerCase() === "input") { return true; }
  767. if(this._data.core.rtl) {
  768. if(e.which === 37) { e.which = 39; }
  769. else if(e.which === 39) { e.which = 37; }
  770. }
  771. var f = this._kbevent_to_func(e);
  772. if (f) {
  773. var r = f.call(this, e);
  774. if (r === false || r === true) {
  775. return r;
  776. }
  777. }
  778. }, this))
  779. .on("load_node.jstree", $.proxy(function (e, data) {
  780. if(data.status) {
  781. if(data.node.id === $.jstree.root && !this._data.core.loaded) {
  782. this._data.core.loaded = true;
  783. if(this._firstChild(this.get_container_ul()[0])) {
  784. this.element.attr('aria-activedescendant',this._firstChild(this.get_container_ul()[0]).id);
  785. }
  786. /**
  787. * triggered after the root node is loaded for the first time
  788. * @event
  789. * @name loaded.jstree
  790. */
  791. this.trigger("loaded");
  792. }
  793. if(!this._data.core.ready) {
  794. setTimeout($.proxy(function() {
  795. if(this.element && !this.get_container_ul().find('.jstree-loading').length) {
  796. this._data.core.ready = true;
  797. if(this._data.core.selected.length) {
  798. if(this.settings.core.expand_selected_onload) {
  799. var tmp = [], i, j;
  800. for(i = 0, j = this._data.core.selected.length; i < j; i++) {
  801. tmp = tmp.concat(this._model.data[this._data.core.selected[i]].parents);
  802. }
  803. tmp = $.vakata.array_unique(tmp);
  804. for(i = 0, j = tmp.length; i < j; i++) {
  805. this.open_node(tmp[i], false, 0);
  806. }
  807. }
  808. this.trigger('changed', { 'action' : 'ready', 'selected' : this._data.core.selected });
  809. }
  810. /**
  811. * triggered after all nodes are finished loading
  812. * @event
  813. * @name ready.jstree
  814. */
  815. this.trigger("ready");
  816. }
  817. }, this), 0);
  818. }
  819. }
  820. }, this))
  821. // quick searching when the tree is focused
  822. .on('keypress.jstree', $.proxy(function (e) {
  823. if(e.target.tagName && e.target.tagName.toLowerCase() === "input") { return true; }
  824. if(tout) { clearTimeout(tout); }
  825. tout = setTimeout(function () {
  826. word = '';
  827. }, 500);
  828. var chr = String.fromCharCode(e.which).toLowerCase(),
  829. col = this.element.find('.jstree-anchor').filter(':visible'),
  830. ind = col.index(document.activeElement) || 0,
  831. end = false;
  832. word += chr;
  833. // match for whole word from current node down (including the current node)
  834. if(word.length > 1) {
  835. col.slice(ind).each($.proxy(function (i, v) {
  836. if($(v).text().toLowerCase().indexOf(word) === 0) {
  837. $(v).focus();
  838. end = true;
  839. return false;
  840. }
  841. }, this));
  842. if(end) { return; }
  843. // match for whole word from the beginning of the tree
  844. col.slice(0, ind).each($.proxy(function (i, v) {
  845. if($(v).text().toLowerCase().indexOf(word) === 0) {
  846. $(v).focus();
  847. end = true;
  848. return false;
  849. }
  850. }, this));
  851. if(end) { return; }
  852. }
  853. // list nodes that start with that letter (only if word consists of a single char)
  854. if(new RegExp('^' + chr.replace(/[-\/\\^$*+?.()|[\]{}]/g, '\\$&') + '+$').test(word)) {
  855. // search for the next node starting with that letter
  856. col.slice(ind + 1).each($.proxy(function (i, v) {
  857. if($(v).text().toLowerCase().charAt(0) === chr) {
  858. $(v).focus();
  859. end = true;
  860. return false;
  861. }
  862. }, this));
  863. if(end) { return; }
  864. // search from the beginning
  865. col.slice(0, ind + 1).each($.proxy(function (i, v) {
  866. if($(v).text().toLowerCase().charAt(0) === chr) {
  867. $(v).focus();
  868. end = true;
  869. return false;
  870. }
  871. }, this));
  872. if(end) { return; }
  873. }
  874. }, this))
  875. // THEME RELATED
  876. .on("init.jstree", $.proxy(function () {
  877. var s = this.settings.core.themes;
  878. this._data.core.themes.dots = s.dots;
  879. this._data.core.themes.stripes = s.stripes;
  880. this._data.core.themes.icons = s.icons;
  881. this._data.core.themes.ellipsis = s.ellipsis;
  882. this.set_theme(s.name || "default", s.url);
  883. this.set_theme_variant(s.variant);
  884. }, this))
  885. .on("loading.jstree", $.proxy(function () {
  886. this[ this._data.core.themes.dots ? "show_dots" : "hide_dots" ]();
  887. this[ this._data.core.themes.icons ? "show_icons" : "hide_icons" ]();
  888. this[ this._data.core.themes.stripes ? "show_stripes" : "hide_stripes" ]();
  889. this[ this._data.core.themes.ellipsis ? "show_ellipsis" : "hide_ellipsis" ]();
  890. }, this))
  891. .on('blur.jstree', '.jstree-anchor', $.proxy(function (e) {
  892. this._data.core.focused = null;
  893. $(e.currentTarget).filter('.jstree-hovered').trigger('mouseleave');
  894. this.element.attr('tabindex', '0');
  895. }, this))
  896. .on('focus.jstree', '.jstree-anchor', $.proxy(function (e) {
  897. var tmp = this.get_node(e.currentTarget);
  898. if(tmp && tmp.id) {
  899. this._data.core.focused = tmp.id;
  900. }
  901. this.element.find('.jstree-hovered').not(e.currentTarget).trigger('mouseleave');
  902. $(e.currentTarget).trigger('mouseenter');
  903. this.element.attr('tabindex', '-1');
  904. }, this))
  905. .on('focus.jstree', $.proxy(function () {
  906. if(+(new Date()) - was_click > 500 && !this._data.core.focused && this.settings.core.restore_focus) {
  907. was_click = 0;
  908. var act = this.get_node(this.element.attr('aria-activedescendant'), true);
  909. if(act) {
  910. act.find('> .jstree-anchor').focus();
  911. }
  912. }
  913. }, this))
  914. .on('mouseenter.jstree', '.jstree-anchor', $.proxy(function (e) {
  915. this.hover_node(e.currentTarget);
  916. }, this))
  917. .on('mouseleave.jstree', '.jstree-anchor', $.proxy(function (e) {
  918. this.dehover_node(e.currentTarget);
  919. }, this));
  920. },
  921. /**
  922. * part of the destroying of an instance. Used internally.
  923. * @private
  924. * @name unbind()
  925. */
  926. unbind : function () {
  927. this.element.off('.jstree');
  928. $(document).off('.jstree-' + this._id);
  929. },
  930. /**
  931. * trigger an event. Used internally.
  932. * @private
  933. * @name trigger(ev [, data])
  934. * @param {String} ev the name of the event to trigger
  935. * @param {Object} data additional data to pass with the event
  936. */
  937. trigger : function (ev, data) {
  938. if(!data) {
  939. data = {};
  940. }
  941. data.instance = this;
  942. this.element.triggerHandler(ev.replace('.jstree','') + '.jstree', data);
  943. },
  944. /**
  945. * returns the jQuery extended instance container
  946. * @name get_container()
  947. * @return {jQuery}
  948. */
  949. get_container : function () {
  950. return this.element;
  951. },
  952. /**
  953. * returns the jQuery extended main UL node inside the instance container. Used internally.
  954. * @private
  955. * @name get_container_ul()
  956. * @return {jQuery}
  957. */
  958. get_container_ul : function () {
  959. return this.element.children(".jstree-children").first();
  960. },
  961. /**
  962. * gets string replacements (localization). Used internally.
  963. * @private
  964. * @name get_string(key)
  965. * @param {String} key
  966. * @return {String}
  967. */
  968. get_string : function (key) {
  969. var a = this.settings.core.strings;
  970. if($.isFunction(a)) { return a.call(this, key); }
  971. if(a && a[key]) { return a[key]; }
  972. return key;
  973. },
  974. /**
  975. * gets the first child of a DOM node. Used internally.
  976. * @private
  977. * @name _firstChild(dom)
  978. * @param {DOMElement} dom
  979. * @return {DOMElement}
  980. */
  981. _firstChild : function (dom) {
  982. dom = dom ? dom.firstChild : null;
  983. while(dom !== null && dom.nodeType !== 1) {
  984. dom = dom.nextSibling;
  985. }
  986. return dom;
  987. },
  988. /**
  989. * gets the next sibling of a DOM node. Used internally.
  990. * @private
  991. * @name _nextSibling(dom)
  992. * @param {DOMElement} dom
  993. * @return {DOMElement}
  994. */
  995. _nextSibling : function (dom) {
  996. dom = dom ? dom.nextSibling : null;
  997. while(dom !== null && dom.nodeType !== 1) {
  998. dom = dom.nextSibling;
  999. }
  1000. return dom;
  1001. },
  1002. /**
  1003. * gets the previous sibling of a DOM node. Used internally.
  1004. * @private
  1005. * @name _previousSibling(dom)
  1006. * @param {DOMElement} dom
  1007. * @return {DOMElement}
  1008. */
  1009. _previousSibling : function (dom) {
  1010. dom = dom ? dom.previousSibling : null;
  1011. while(dom !== null && dom.nodeType !== 1) {
  1012. dom = dom.previousSibling;
  1013. }
  1014. return dom;
  1015. },
  1016. /**
  1017. * get the JSON representation of a node (or the actual jQuery extended DOM node) by using any input (child DOM element, ID string, selector, etc)
  1018. * @name get_node(obj [, as_dom])
  1019. * @param {mixed} obj
  1020. * @param {Boolean} as_dom
  1021. * @return {Object|jQuery}
  1022. */
  1023. get_node : function (obj, as_dom) {
  1024. if(obj && obj.id) {
  1025. obj = obj.id;
  1026. }
  1027. if (obj instanceof $ && obj.length && obj[0].id) {
  1028. obj = obj[0].id;
  1029. }
  1030. var dom;
  1031. try {
  1032. if(this._model.data[obj]) {
  1033. obj = this._model.data[obj];
  1034. }
  1035. else if(typeof obj === "string" && this._model.data[obj.replace(/^#/, '')]) {
  1036. obj = this._model.data[obj.replace(/^#/, '')];
  1037. }
  1038. else if(typeof obj === "string" && (dom = $('#' + obj.replace($.jstree.idregex,'\\$&'), this.element)).length && this._model.data[dom.closest('.jstree-node').attr('id')]) {
  1039. obj = this._model.data[dom.closest('.jstree-node').attr('id')];
  1040. }
  1041. else if((dom = this.element.find(obj)).length && this._model.data[dom.closest('.jstree-node').attr('id')]) {
  1042. obj = this._model.data[dom.closest('.jstree-node').attr('id')];
  1043. }
  1044. else if((dom = this.element.find(obj)).length && dom.hasClass('jstree')) {
  1045. obj = this._model.data[$.jstree.root];
  1046. }
  1047. else {
  1048. return false;
  1049. }
  1050. if(as_dom) {
  1051. obj = obj.id === $.jstree.root ? this.element : $('#' + obj.id.replace($.jstree.idregex,'\\$&'), this.element);
  1052. }
  1053. return obj;
  1054. } catch (ex) { return false; }
  1055. },
  1056. /**
  1057. * get the path to a node, either consisting of node texts, or of node IDs, optionally glued together (otherwise an array)
  1058. * @name get_path(obj [, glue, ids])
  1059. * @param {mixed} obj the node
  1060. * @param {String} glue if you want the path as a string - pass the glue here (for example '/'), if a falsy value is supplied here, an array is returned
  1061. * @param {Boolean} ids if set to true build the path using ID, otherwise node text is used
  1062. * @return {mixed}
  1063. */
  1064. get_path : function (obj, glue, ids) {
  1065. obj = obj.parents ? obj : this.get_node(obj);
  1066. if(!obj || obj.id === $.jstree.root || !obj.parents) {
  1067. return false;
  1068. }
  1069. var i, j, p = [];
  1070. p.push(ids ? obj.id : obj.text);
  1071. for(i = 0, j = obj.parents.length; i < j; i++) {
  1072. p.push(ids ? obj.parents[i] : this.get_text(obj.parents[i]));
  1073. }
  1074. p = p.reverse().slice(1);
  1075. return glue ? p.join(glue) : p;
  1076. },
  1077. /**
  1078. * get the next visible node that is below the `obj` node. If `strict` is set to `true` only sibling nodes are returned.
  1079. * @name get_next_dom(obj [, strict])
  1080. * @param {mixed} obj
  1081. * @param {Boolean} strict
  1082. * @return {jQuery}
  1083. */
  1084. get_next_dom : function (obj, strict) {
  1085. var tmp;
  1086. obj = this.get_node(obj, true);
  1087. if(obj[0] === this.element[0]) {
  1088. tmp = this._firstChild(this.get_container_ul()[0]);
  1089. while (tmp && tmp.offsetHeight === 0) {
  1090. tmp = this._nextSibling(tmp);
  1091. }
  1092. return tmp ? $(tmp) : false;
  1093. }
  1094. if(!obj || !obj.length) {
  1095. return false;
  1096. }
  1097. if(strict) {
  1098. tmp = obj[0];
  1099. do {
  1100. tmp = this._nextSibling(tmp);
  1101. } while (tmp && tmp.offsetHeight === 0);
  1102. return tmp ? $(tmp) : false;
  1103. }
  1104. if(obj.hasClass("jstree-open")) {
  1105. tmp = this._firstChild(obj.children('.jstree-children')[0]);
  1106. while (tmp && tmp.offsetHeight === 0) {
  1107. tmp = this._nextSibling(tmp);
  1108. }
  1109. if(tmp !== null) {
  1110. return $(tmp);
  1111. }
  1112. }
  1113. tmp = obj[0];
  1114. do {
  1115. tmp = this._nextSibling(tmp);
  1116. } while (tmp && tmp.offsetHeight === 0);
  1117. if(tmp !== null) {
  1118. return $(tmp);
  1119. }
  1120. return obj.parentsUntil(".jstree",".jstree-node").nextAll(".jstree-node:visible").first();
  1121. },
  1122. /**
  1123. * get the previous visible node that is above the `obj` node. If `strict` is set to `true` only sibling nodes are returned.
  1124. * @name get_prev_dom(obj [, strict])
  1125. * @param {mixed} obj
  1126. * @param {Boolean} strict
  1127. * @return {jQuery}
  1128. */
  1129. get_prev_dom : function (obj, strict) {
  1130. var tmp;
  1131. obj = this.get_node(obj, true);
  1132. if(obj[0] === this.element[0]) {
  1133. tmp = this.get_container_ul()[0].lastChild;
  1134. while (tmp && tmp.offsetHeight === 0) {
  1135. tmp = this._previousSibling(tmp);
  1136. }
  1137. return tmp ? $(tmp) : false;
  1138. }
  1139. if(!obj || !obj.length) {
  1140. return false;
  1141. }
  1142. if(strict) {
  1143. tmp = obj[0];
  1144. do {
  1145. tmp = this._previousSibling(tmp);
  1146. } while (tmp && tmp.offsetHeight === 0);
  1147. return tmp ? $(tmp) : false;
  1148. }
  1149. tmp = obj[0];
  1150. do {
  1151. tmp = this._previousSibling(tmp);
  1152. } while (tmp && tmp.offsetHeight === 0);
  1153. if(tmp !== null) {
  1154. obj = $(tmp);
  1155. while(obj.hasClass("jstree-open")) {
  1156. obj = obj.children(".jstree-children").first().children(".jstree-node:visible:last");
  1157. }
  1158. return obj;
  1159. }
  1160. tmp = obj[0].parentNode.parentNode;
  1161. return tmp && tmp.className && tmp.className.indexOf('jstree-node') !== -1 ? $(tmp) : false;
  1162. },
  1163. /**
  1164. * get the parent ID of a node
  1165. * @name get_parent(obj)
  1166. * @param {mixed} obj
  1167. * @return {String}
  1168. */
  1169. get_parent : function (obj) {
  1170. obj = this.get_node(obj);
  1171. if(!obj || obj.id === $.jstree.root) {
  1172. return false;
  1173. }
  1174. return obj.parent;
  1175. },
  1176. /**
  1177. * get a jQuery collection of all the children of a node (node must be rendered), returns false on error
  1178. * @name get_children_dom(obj)
  1179. * @param {mixed} obj
  1180. * @return {jQuery}
  1181. */
  1182. get_children_dom : function (obj) {
  1183. obj = this.get_node(obj, true);
  1184. if(obj[0] === this.element[0]) {
  1185. return this.get_container_ul().children(".jstree-node");
  1186. }
  1187. if(!obj || !obj.length) {
  1188. return false;
  1189. }
  1190. return obj.children(".jstree-children").children(".jstree-node");
  1191. },
  1192. /**
  1193. * checks if a node has children
  1194. * @name is_parent(obj)
  1195. * @param {mixed} obj
  1196. * @return {Boolean}
  1197. */
  1198. is_parent : function (obj) {
  1199. obj = this.get_node(obj);
  1200. return obj && (obj.state.loaded === false || obj.children.length > 0);
  1201. },
  1202. /**
  1203. * checks if a node is loaded (its children are available)
  1204. * @name is_loaded(obj)
  1205. * @param {mixed} obj
  1206. * @return {Boolean}
  1207. */
  1208. is_loaded : function (obj) {
  1209. obj = this.get_node(obj);
  1210. return obj && obj.state.loaded;
  1211. },
  1212. /**
  1213. * check if a node is currently loading (fetching children)
  1214. * @name is_loading(obj)
  1215. * @param {mixed} obj
  1216. * @return {Boolean}
  1217. */
  1218. is_loading : function (obj) {
  1219. obj = this.get_node(obj);
  1220. return obj && obj.state && obj.state.loading;
  1221. },
  1222. /**
  1223. * check if a node is opened
  1224. * @name is_open(obj)
  1225. * @param {mixed} obj
  1226. * @return {Boolean}
  1227. */
  1228. is_open : function (obj) {
  1229. obj = this.get_node(obj);
  1230. return obj && obj.state.opened;
  1231. },
  1232. /**
  1233. * check if a node is in a closed state
  1234. * @name is_closed(obj)
  1235. * @param {mixed} obj
  1236. * @return {Boolean}
  1237. */
  1238. is_closed : function (obj) {
  1239. obj = this.get_node(obj);
  1240. return obj && this.is_parent(obj) && !obj.state.opened;
  1241. },
  1242. /**
  1243. * check if a node has no children
  1244. * @name is_leaf(obj)
  1245. * @param {mixed} obj
  1246. * @return {Boolean}
  1247. */
  1248. is_leaf : function (obj) {
  1249. return !this.is_parent(obj);
  1250. },
  1251. /**
  1252. * loads a node (fetches its children using the `core.data` setting). Multiple nodes can be passed to by using an array.
  1253. * @name load_node(obj [, callback])
  1254. * @param {mixed} obj
  1255. * @param {function} callback a function to be executed once loading is complete, the function is executed in the instance's scope and receives two arguments - the node and a boolean status
  1256. * @return {Boolean}
  1257. * @trigger load_node.jstree
  1258. */
  1259. load_node : function (obj, callback) {
  1260. var k, l, i, j, c;
  1261. if($.isArray(obj)) {
  1262. this._load_nodes(obj.slice(), callback);
  1263. return true;
  1264. }
  1265. obj = this.get_node(obj);
  1266. if(!obj) {
  1267. if(callback) { callback.call(this, obj, false); }
  1268. return false;
  1269. }
  1270. // if(obj.state.loading) { } // the node is already loading - just wait for it to load and invoke callback? but if called implicitly it should be loaded again?
  1271. if(obj.state.loaded) {
  1272. obj.state.loaded = false;
  1273. for(i = 0, j = obj.parents.length; i < j; i++) {
  1274. this._model.data[obj.parents[i]].children_d = $.vakata.array_filter(this._model.data[obj.parents[i]].children_d, function (v) {
  1275. return $.inArray(v, obj.children_d) === -1;
  1276. });
  1277. }
  1278. for(k = 0, l = obj.children_d.length; k < l; k++) {
  1279. if(this._model.data[obj.children_d[k]].state.selected) {
  1280. c = true;
  1281. }
  1282. delete this._model.data[obj.children_d[k]];
  1283. }
  1284. if (c) {
  1285. this._data.core.selected = $.vakata.array_filter(this._data.core.selected, function (v) {
  1286. return $.inArray(v, obj.children_d) === -1;
  1287. });
  1288. }
  1289. obj.children = [];
  1290. obj.children_d = [];
  1291. if(c) {
  1292. this.trigger('changed', { 'action' : 'load_node', 'node' : obj, 'selected' : this._data.core.selected });
  1293. }
  1294. }
  1295. obj.state.failed = false;
  1296. obj.state.loading = true;
  1297. this.get_node(obj, true).addClass("jstree-loading").attr('aria-busy',true);
  1298. this._load_node(obj, $.proxy(function (status) {
  1299. obj = this._model.data[obj.id];
  1300. obj.state.loading = false;
  1301. obj.state.loaded = status;
  1302. obj.state.failed = !obj.state.loaded;
  1303. var dom = this.get_node(obj, true), i = 0, j = 0, m = this._model.data, has_children = false;
  1304. for(i = 0, j = obj.children.length; i < j; i++) {
  1305. if(m[obj.children[i]] && !m[obj.children[i]].state.hidden) {
  1306. has_children = true;
  1307. break;
  1308. }
  1309. }
  1310. if(obj.state.loaded && dom && dom.length) {
  1311. dom.removeClass('jstree-closed jstree-open jstree-leaf');
  1312. if (!has_children) {
  1313. dom.addClass('jstree-leaf');
  1314. }
  1315. else {
  1316. if (obj.id !== '#') {
  1317. dom.addClass(obj.state.opened ? 'jstree-open' : 'jstree-closed');
  1318. }
  1319. }
  1320. }
  1321. dom.removeClass("jstree-loading").attr('aria-busy',false);
  1322. /**
  1323. * triggered after a node is loaded
  1324. * @event
  1325. * @name load_node.jstree
  1326. * @param {Object} node the node that was loading
  1327. * @param {Boolean} status was the node loaded successfully
  1328. */
  1329. this.trigger('load_node', { "node" : obj, "status" : status });
  1330. if(callback) {
  1331. callback.call(this, obj, status);
  1332. }
  1333. }, this));
  1334. return true;
  1335. },
  1336. /**
  1337. * load an array of nodes (will also load unavailable nodes as soon as they appear in the structure). Used internally.
  1338. * @private
  1339. * @name _load_nodes(nodes [, callback])
  1340. * @param {array} nodes
  1341. * @param {function} callback a function to be executed once loading is complete, the function is executed in the instance's scope and receives one argument - the array passed to _load_nodes
  1342. */
  1343. _load_nodes : function (nodes, callback, is_callback, force_reload) {
  1344. var r = true,
  1345. c = function () { this._load_nodes(nodes, callback, true); },
  1346. m = this._model.data, i, j, tmp = [];
  1347. for(i = 0, j = nodes.length; i < j; i++) {
  1348. if(m[nodes[i]] && ( (!m[nodes[i]].state.loaded && !m[nodes[i]].state.failed) || (!is_callback && force_reload) )) {
  1349. if(!this.is_loading(nodes[i])) {
  1350. this.load_node(nodes[i], c);
  1351. }
  1352. r = false;
  1353. }
  1354. }
  1355. if(r) {
  1356. for(i = 0, j = nodes.length; i < j; i++) {
  1357. if(m[nodes[i]] && m[nodes[i]].state.loaded) {
  1358. tmp.push(nodes[i]);
  1359. }
  1360. }
  1361. if(callback && !callback.done) {
  1362. callback.call(this, tmp);
  1363. callback.done = true;
  1364. }
  1365. }
  1366. },
  1367. /**
  1368. * loads all unloaded nodes
  1369. * @name load_all([obj, callback])
  1370. * @param {mixed} obj the node to load recursively, omit to load all nodes in the tree
  1371. * @param {function} callback a function to be executed once loading all the nodes is complete,
  1372. * @trigger load_all.jstree
  1373. */
  1374. load_all : function (obj, callback) {
  1375. if(!obj) { obj = $.jstree.root; }
  1376. obj = this.get_node(obj);
  1377. if(!obj) { return false; }
  1378. var to_load = [],
  1379. m = this._model.data,
  1380. c = m[obj.id].children_d,
  1381. i, j;
  1382. if(obj.state && !obj.state.loaded) {
  1383. to_load.push(obj.id);
  1384. }
  1385. for(i = 0, j = c.length; i < j; i++) {
  1386. if(m[c[i]] && m[c[i]].state && !m[c[i]].state.loaded) {
  1387. to_load.push(c[i]);
  1388. }
  1389. }
  1390. if(to_load.length) {
  1391. this._load_nodes(to_load, function () {
  1392. this.load_all(obj, callback);
  1393. });
  1394. }
  1395. else {
  1396. /**
  1397. * triggered after a load_all call completes
  1398. * @event
  1399. * @name load_all.jstree
  1400. * @param {Object} node the recursively loaded node
  1401. */
  1402. if(callback) { callback.call(this, obj); }
  1403. this.trigger('load_all', { "node" : obj });
  1404. }
  1405. },
  1406. /**
  1407. * handles the actual loading of a node. Used only internally.
  1408. * @private
  1409. * @name _load_node(obj [, callback])
  1410. * @param {mixed} obj
  1411. * @param {function} callback a function to be executed once loading is complete, the function is executed in the instance's scope and receives one argument - a boolean status
  1412. * @return {Boolean}
  1413. */
  1414. _load_node : function (obj, callback) {
  1415. var s = this.settings.core.data, t;
  1416. var notTextOrCommentNode = function notTextOrCommentNode () {
  1417. return this.nodeType !== 3 && this.nodeType !== 8;
  1418. };
  1419. // use original HTML
  1420. if(!s) {
  1421. if(obj.id === $.jstree.root) {
  1422. return this._append_html_data(obj, this._data.core.original_container_html.clone(true), function (status) {
  1423. callback.call(this, status);
  1424. });
  1425. }
  1426. else {
  1427. return callback.call(this, false);
  1428. }
  1429. // return callback.call(this, obj.id === $.jstree.root ? this._append_html_data(obj, this._data.core.original_container_html.clone(true)) : false);
  1430. }
  1431. if($.isFunction(s)) {
  1432. return s.call(this, obj, $.proxy(function (d) {
  1433. if(d === false) {
  1434. callback.call(this, false);
  1435. }
  1436. else {
  1437. this[typeof d === 'string' ? '_append_html_data' : '_append_json_data'](obj, typeof d === 'string' ? $($.parseHTML(d)).filter(notTextOrCommentNode) : d, function (status) {
  1438. callback.call(this, status);
  1439. });
  1440. }
  1441. // return d === false ? callback.call(this, false) : callback.call(this, this[typeof d === 'string' ? '_append_html_data' : '_append_json_data'](obj, typeof d === 'string' ? $(d) : d));
  1442. }, this));
  1443. }
  1444. if(typeof s === 'object') {
  1445. if(s.url) {
  1446. s = $.extend(true, {}, s);
  1447. if($.isFunction(s.url)) {
  1448. s.url = s.url.call(this, obj);
  1449. }
  1450. if($.isFunction(s.data)) {
  1451. s.data = s.data.call(this, obj);
  1452. }
  1453. return $.ajax(s)
  1454. .done($.proxy(function (d,t,x) {
  1455. var type = x.getResponseHeader('Content-Type');
  1456. if((type && type.indexOf('json') !== -1) || typeof d === "object") {
  1457. return this._append_json_data(obj, d, function (status) { callback.call(this, status); });
  1458. //return callback.call(this, this._append_json_data(obj, d));
  1459. }
  1460. if((type && type.indexOf('html') !== -1) || typeof d === "string") {
  1461. return this._append_html_data(obj, $($.parseHTML(d)).filter(notTextOrCommentNode), function (status) { callback.call(this, status); });
  1462. // return callback.call(this, this._append_html_data(obj, $(d)));
  1463. }
  1464. this._data.core.last_error = { 'error' : 'ajax', 'plugin' : 'core', 'id' : 'core_04', 'reason' : 'Could not load node', 'data' : JSON.stringify({ 'id' : obj.id, 'xhr' : x }) };
  1465. this.settings.core.error.call(this, this._data.core.last_error);
  1466. return callback.call(this, false);
  1467. }, this))
  1468. .fail($.proxy(function (f) {
  1469. this._data.core.last_error = { 'error' : 'ajax', 'plugin' : 'core', 'id' : 'core_04', 'reason' : 'Could not load node', 'data' : JSON.stringify({ 'id' : obj.id, 'xhr' : f }) };
  1470. callback.call(this, false);
  1471. this.settings.core.error.call(this, this._data.core.last_error);
  1472. }, this));
  1473. }
  1474. if ($.isArray(s)) {
  1475. t = $.extend(true, [], s);
  1476. } else if ($.isPlainObject(s)) {
  1477. t = $.extend(true, {}, s);
  1478. } else {
  1479. t = s;
  1480. }
  1481. if(obj.id === $.jstree.root) {
  1482. return this._append_json_data(obj, t, function (status) {
  1483. callback.call(this, status);
  1484. });
  1485. }
  1486. else {
  1487. this._data.core.last_error = { 'error' : 'nodata', 'plugin' : 'core', 'id' : 'core_05', 'reason' : 'Could not load node', 'data' : JSON.stringify({ 'id' : obj.id }) };
  1488. this.settings.core.error.call(this, this._data.core.last_error);
  1489. return callback.call(this, false);
  1490. }
  1491. //return callback.call(this, (obj.id === $.jstree.root ? this._append_json_data(obj, t) : false) );
  1492. }
  1493. if(typeof s === 'string') {
  1494. if(obj.id === $.jstree.root) {
  1495. return this._append_html_data(obj, $($.parseHTML(s)).filter(notTextOrCommentNode), function (status) {
  1496. callback.call(this, status);
  1497. });
  1498. }
  1499. else {
  1500. this._data.core.last_error = { 'error' : 'nodata', 'plugin' : 'core', 'id' : 'core_06', 'reason' : 'Could not load node', 'data' : JSON.stringify({ 'id' : obj.id }) };
  1501. this.settings.core.error.call(this, this._data.core.last_error);
  1502. return callback.call(this, false);
  1503. }
  1504. //return callback.call(this, (obj.id === $.jstree.root ? this._append_html_data(obj, $(s)) : false) );
  1505. }
  1506. return callback.call(this, false);
  1507. },
  1508. /**
  1509. * adds a node to the list of nodes to redraw. Used only internally.
  1510. * @private
  1511. * @name _node_changed(obj [, callback])
  1512. * @param {mixed} obj
  1513. */
  1514. _node_changed : function (obj) {
  1515. obj = this.get_node(obj);
  1516. if (obj && $.inArray(obj.id, this._model.changed) === -1) {
  1517. this._model.changed.push(obj.id);
  1518. }
  1519. },
  1520. /**
  1521. * appends HTML content to the tree. Used internally.
  1522. * @private
  1523. * @name _append_html_data(obj, data)
  1524. * @param {mixed} obj the node to append to
  1525. * @param {String} data the HTML string to parse and append
  1526. * @trigger model.jstree, changed.jstree
  1527. */
  1528. _append_html_data : function (dom, data, cb) {
  1529. dom = this.get_node(dom);
  1530. dom.children = [];
  1531. dom.children_d = [];
  1532. var dat = data.is('ul') ? data.children() : data,
  1533. par = dom.id,
  1534. chd = [],
  1535. dpc = [],
  1536. m = this._model.data,
  1537. p = m[par],
  1538. s = this._data.core.selected.length,
  1539. tmp, i, j;
  1540. dat.each($.proxy(function (i, v) {
  1541. tmp = this._parse_model_from_html($(v), par, p.parents.concat());
  1542. if(tmp) {
  1543. chd.push(tmp);
  1544. dpc.push(tmp);
  1545. if(m[tmp].children_d.length) {
  1546. dpc = dpc.concat(m[tmp].children_d);
  1547. }
  1548. }
  1549. }, this));
  1550. p.children = chd;
  1551. p.children_d = dpc;
  1552. for(i = 0, j = p.parents.length; i < j; i++) {
  1553. m[p.parents[i]].children_d = m[p.parents[i]].children_d.concat(dpc);
  1554. }
  1555. /**
  1556. * triggered when new data is inserted to the tree model
  1557. * @event
  1558. * @name model.jstree
  1559. * @param {Array} nodes an array of node IDs
  1560. * @param {String} parent the parent ID of the nodes
  1561. */
  1562. this.trigger('model', { "nodes" : dpc, 'parent' : par });
  1563. if(par !== $.jstree.root) {
  1564. this._node_changed(par);
  1565. this.redraw();
  1566. }
  1567. else {
  1568. this.get_container_ul().children('.jstree-initial-node').remove();
  1569. this.redraw(true);
  1570. }
  1571. if(this._data.core.selected.length !== s) {
  1572. this.trigger('changed', { 'action' : 'model', 'selected' : this._data.core.selected });
  1573. }
  1574. cb.call(this, true);
  1575. },
  1576. /**
  1577. * appends JSON content to the tree. Used internally.
  1578. * @private
  1579. * @name _append_json_data(obj, data)
  1580. * @param {mixed} obj the node to append to
  1581. * @param {String} data the JSON object to parse and append
  1582. * @param {Boolean} force_processing internal param - do not set
  1583. * @trigger model.jstree, changed.jstree
  1584. */
  1585. _append_json_data : function (dom, data, cb, force_processing) {
  1586. if(this.element === null) { return; }
  1587. dom = this.get_node(dom);
  1588. dom.children = [];
  1589. dom.children_d = [];
  1590. // *%$@!!!
  1591. if(data.d) {
  1592. data = data.d;
  1593. if(typeof data === "string") {
  1594. data = JSON.parse(data);
  1595. }
  1596. }
  1597. if(!$.isArray(data)) { data = [data]; }
  1598. var w = null,
  1599. args = {
  1600. 'df' : this._model.default_state,
  1601. 'dat' : data,
  1602. 'par' : dom.id,
  1603. 'm' : this._model.data,
  1604. 't_id' : this._id,
  1605. 't_cnt' : this._cnt,
  1606. 'sel' : this._data.core.selected
  1607. },
  1608. inst = this,
  1609. func = function (data, undefined) {
  1610. if(data.data) { data = data.data; }
  1611. var dat = data.dat,
  1612. par = data.par,
  1613. chd = [],
  1614. dpc = [],
  1615. add = [],
  1616. df = data.df,
  1617. t_id = data.t_id,
  1618. t_cnt = data.t_cnt,
  1619. m = data.m,
  1620. p = m[par],
  1621. sel = data.sel,
  1622. tmp, i, j, rslt,
  1623. parse_flat = function (d, p, ps) {
  1624. if(!ps) { ps = []; }
  1625. else { ps = ps.concat(); }
  1626. if(p) { ps.unshift(p); }
  1627. var tid = d.id.toString(),
  1628. i, j, c, e,
  1629. tmp = {
  1630. id : tid,
  1631. text : d.text || '',
  1632. icon : d.icon !== undefined ? d.icon : true,
  1633. parent : p,
  1634. parents : ps,
  1635. children : d.children || [],
  1636. children_d : d.children_d || [],
  1637. data : d.data,
  1638. state : { },
  1639. li_attr : { id : false },
  1640. a_attr : { href : '#' },
  1641. original : false
  1642. };
  1643. for(i in df) {
  1644. if(df.hasOwnProperty(i)) {
  1645. tmp.state[i] = df[i];
  1646. }
  1647. }
  1648. if(d && d.data && d.data.jstree && d.data.jstree.icon) {
  1649. tmp.icon = d.data.jstree.icon;
  1650. }
  1651. if(tmp.icon === undefined || tmp.icon === null || tmp.icon === "") {
  1652. tmp.icon = true;
  1653. }
  1654. if(d && d.data) {
  1655. tmp.data = d.data;
  1656. if(d.data.jstree) {
  1657. for(i in d.data.jstree) {
  1658. if(d.data.jstree.hasOwnProperty(i)) {
  1659. tmp.state[i] = d.data.jstree[i];
  1660. }
  1661. }
  1662. }
  1663. }
  1664. if(d && typeof d.state === 'object') {
  1665. for (i in d.state) {
  1666. if(d.state.hasOwnProperty(i)) {
  1667. tmp.state[i] = d.state[i];
  1668. }
  1669. }
  1670. }
  1671. if(d && typeof d.li_attr === 'object') {
  1672. for (i in d.li_attr) {
  1673. if(d.li_attr.hasOwnProperty(i)) {
  1674. tmp.li_attr[i] = d.li_attr[i];
  1675. }
  1676. }
  1677. }
  1678. if(!tmp.li_attr.id) {
  1679. tmp.li_attr.id = tid;
  1680. }
  1681. if(d && typeof d.a_attr === 'object') {
  1682. for (i in d.a_attr) {
  1683. if(d.a_attr.hasOwnProperty(i)) {
  1684. tmp.a_attr[i] = d.a_attr[i];
  1685. }
  1686. }
  1687. }
  1688. if(d && d.children && d.children === true) {
  1689. tmp.state.loaded = false;
  1690. tmp.children = [];
  1691. tmp.children_d = [];
  1692. }
  1693. m[tmp.id] = tmp;
  1694. for(i = 0, j = tmp.children.length; i < j; i++) {
  1695. c = parse_flat(m[tmp.children[i]], tmp.id, ps);
  1696. e = m[c];
  1697. tmp.children_d.push(c);
  1698. if(e.children_d.length) {
  1699. tmp.children_d = tmp.children_d.concat(e.children_d);
  1700. }
  1701. }
  1702. delete d.data;
  1703. delete d.children;
  1704. m[tmp.id].original = d;
  1705. if(tmp.state.selected) {
  1706. add.push(tmp.id);
  1707. }
  1708. return tmp.id;
  1709. },
  1710. parse_nest = function (d, p, ps) {
  1711. if(!ps) { ps = []; }
  1712. else { ps = ps.concat(); }
  1713. if(p) { ps.unshift(p); }
  1714. var tid = false, i, j, c, e, tmp;
  1715. do {
  1716. tid = 'j' + t_id + '_' + (++t_cnt);
  1717. } while(m[tid]);
  1718. tmp = {
  1719. id : false,
  1720. text : typeof d === 'string' ? d : '',
  1721. icon : typeof d === 'object' && d.icon !== undefined ? d.icon : true,
  1722. parent : p,
  1723. parents : ps,
  1724. children : [],
  1725. children_d : [],
  1726. data : null,
  1727. state : { },
  1728. li_attr : { id : false },
  1729. a_attr : { href : '#' },
  1730. original : false
  1731. };
  1732. for(i in df) {
  1733. if(df.hasOwnProperty(i)) {
  1734. tmp.state[i] = df[i];
  1735. }
  1736. }
  1737. if(d && d.id) { tmp.id = d.id.toString(); }
  1738. if(d && d.text) { tmp.text = d.text; }
  1739. if(d && d.data && d.data.jstree && d.data.jstree.icon) {
  1740. tmp.icon = d.data.jstree.icon;
  1741. }
  1742. if(tmp.icon === undefined || tmp.icon === null || tmp.icon === "") {
  1743. tmp.icon = true;
  1744. }
  1745. if(d && d.data) {
  1746. tmp.data = d.data;
  1747. if(d.data.jstree) {
  1748. for(i in d.data.jstree) {
  1749. if(d.data.jstree.hasOwnProperty(i)) {
  1750. tmp.state[i] = d.data.jstree[i];
  1751. }
  1752. }
  1753. }
  1754. }
  1755. if(d && typeof d.state === 'object') {
  1756. for (i in d.state) {
  1757. if(d.state.hasOwnProperty(i)) {
  1758. tmp.state[i] = d.state[i];
  1759. }
  1760. }
  1761. }
  1762. if(d && typeof d.li_attr === 'object') {
  1763. for (i in d.li_attr) {
  1764. if(d.li_attr.hasOwnProperty(i)) {
  1765. tmp.li_attr[i] = d.li_attr[i];
  1766. }
  1767. }
  1768. }
  1769. if(tmp.li_attr.id && !tmp.id) {
  1770. tmp.id = tmp.li_attr.id.toString();
  1771. }
  1772. if(!tmp.id) {
  1773. tmp.id = tid;
  1774. }
  1775. if(!tmp.li_attr.id) {
  1776. tmp.li_attr.id = tmp.id;
  1777. }
  1778. if(d && typeof d.a_attr === 'object') {
  1779. for (i in d.a_attr) {
  1780. if(d.a_attr.hasOwnProperty(i)) {
  1781. tmp.a_attr[i] = d.a_attr[i];
  1782. }
  1783. }
  1784. }
  1785. if(d && d.children && d.children.length) {
  1786. for(i = 0, j = d.children.length; i < j; i++) {
  1787. c = parse_nest(d.children[i], tmp.id, ps);
  1788. e = m[c];
  1789. tmp.children.push(c);
  1790. if(e.children_d.length) {
  1791. tmp.children_d = tmp.children_d.concat(e.children_d);
  1792. }
  1793. }
  1794. tmp.children_d = tmp.children_d.concat(tmp.children);
  1795. }
  1796. if(d && d.children && d.children === true) {
  1797. tmp.state.loaded = false;
  1798. tmp.children = [];
  1799. tmp.children_d = [];
  1800. }
  1801. delete d.data;
  1802. delete d.children;
  1803. tmp.original = d;
  1804. m[tmp.id] = tmp;
  1805. if(tmp.state.selected) {
  1806. add.push(tmp.id);
  1807. }
  1808. return tmp.id;
  1809. };
  1810. if(dat.length && dat[0].id !== undefined && dat[0].parent !== undefined) {
  1811. // Flat JSON support (for easy import from DB):
  1812. // 1) convert to object (foreach)
  1813. for(i = 0, j = dat.length; i < j; i++) {
  1814. if(!dat[i].children) {
  1815. dat[i].children = [];
  1816. }
  1817. if(!dat[i].state) {
  1818. dat[i].state = {};
  1819. }
  1820. m[dat[i].id.toString()] = dat[i];
  1821. }
  1822. // 2) populate children (foreach)
  1823. for(i = 0, j = dat.length; i < j; i++) {
  1824. if (!m[dat[i].parent.toString()]) {
  1825. if (typeof inst !== "undefined") {
  1826. inst._data.core.last_error = { 'error' : 'parse', 'plugin' : 'core', 'id' : 'core_07', 'reason' : 'Node with invalid parent', 'data' : JSON.stringify({ 'id' : dat[i].id.toString(), 'parent' : dat[i].parent.toString() }) };
  1827. inst.settings.core.error.call(inst, inst._data.core.last_error);
  1828. }
  1829. continue;
  1830. }
  1831. m[dat[i].parent.toString()].children.push(dat[i].id.toString());
  1832. // populate parent.children_d
  1833. p.children_d.push(dat[i].id.toString());
  1834. }
  1835. // 3) normalize && populate parents and children_d with recursion
  1836. for(i = 0, j = p.children.length; i < j; i++) {
  1837. tmp = parse_flat(m[p.children[i]], par, p.parents.concat());
  1838. dpc.push(tmp);
  1839. if(m[tmp].children_d.length) {
  1840. dpc = dpc.concat(m[tmp].children_d);
  1841. }
  1842. }
  1843. for(i = 0, j = p.parents.length; i < j; i++) {
  1844. m[p.parents[i]].children_d = m[p.parents[i]].children_d.concat(dpc);
  1845. }
  1846. // ?) three_state selection - p.state.selected && t - (if three_state foreach(dat => ch) -> foreach(parents) if(parent.selected) child.selected = true;
  1847. rslt = {
  1848. 'cnt' : t_cnt,
  1849. 'mod' : m,
  1850. 'sel' : sel,
  1851. 'par' : par,
  1852. 'dpc' : dpc,
  1853. 'add' : add
  1854. };
  1855. }
  1856. else {
  1857. for(i = 0, j = dat.length; i < j; i++) {
  1858. tmp = parse_nest(dat[i], par, p.parents.concat());
  1859. if(tmp) {
  1860. chd.push(tmp);
  1861. dpc.push(tmp);
  1862. if(m[tmp].children_d.length) {
  1863. dpc = dpc.concat(m[tmp].children_d);
  1864. }
  1865. }
  1866. }
  1867. p.children = chd;
  1868. p.children_d = dpc;
  1869. for(i = 0, j = p.parents.length; i < j; i++) {
  1870. m[p.parents[i]].children_d = m[p.parents[i]].children_d.concat(dpc);
  1871. }
  1872. rslt = {
  1873. 'cnt' : t_cnt,
  1874. 'mod' : m,
  1875. 'sel' : sel,
  1876. 'par' : par,
  1877. 'dpc' : dpc,
  1878. 'add' : add
  1879. };
  1880. }
  1881. if(typeof window === 'undefined' || typeof window.document === 'undefined') {
  1882. postMessage(rslt);
  1883. }
  1884. else {
  1885. return rslt;
  1886. }
  1887. },
  1888. rslt = function (rslt, worker) {
  1889. if(this.element === null) { return; }
  1890. this._cnt = rslt.cnt;
  1891. var i, m = this._model.data;
  1892. for (i in m) {
  1893. if (m.hasOwnProperty(i) && m[i].state && m[i].state.loading && rslt.mod[i]) {
  1894. rslt.mod[i].state.loading = true;
  1895. }
  1896. }
  1897. this._model.data = rslt.mod; // breaks the reference in load_node - careful
  1898. if(worker) {
  1899. var j, a = rslt.add, r = rslt.sel, s = this._data.core.selected.slice();
  1900. m = this._model.data;
  1901. // if selection was changed while calculating in worker
  1902. if(r.length !== s.length || $.vakata.array_unique(r.concat(s)).length !== r.length) {
  1903. // deselect nodes that are no longer selected
  1904. for(i = 0, j = r.length; i < j; i++) {
  1905. if($.inArray(r[i], a) === -1 && $.inArray(r[i], s) === -1) {
  1906. m[r[i]].state.selected = false;
  1907. }
  1908. }
  1909. // select nodes that were selected in the mean time
  1910. for(i = 0, j = s.length; i < j; i++) {
  1911. if($.inArray(s[i], r) === -1) {
  1912. m[s[i]].state.selected = true;
  1913. }
  1914. }
  1915. }
  1916. }
  1917. if(rslt.add.length) {
  1918. this._data.core.selected = this._data.core.selected.concat(rslt.add);
  1919. }
  1920. this.trigger('model', { "nodes" : rslt.dpc, 'parent' : rslt.par });
  1921. if(rslt.par !== $.jstree.root) {
  1922. this._node_changed(rslt.par);
  1923. this.redraw();
  1924. }
  1925. else {
  1926. // this.get_container_ul().children('.jstree-initial-node').remove();
  1927. this.redraw(true);
  1928. }
  1929. if(rslt.add.length) {
  1930. this.trigger('changed', { 'action' : 'model', 'selected' : this._data.core.selected });
  1931. }
  1932. // If no worker, try to mimic worker behavioour, by invoking cb asynchronously
  1933. if (!worker && setImmediate) {
  1934. setImmediate(function(){
  1935. cb.call(inst, true);
  1936. });
  1937. }
  1938. else {
  1939. cb.call(inst, true);
  1940. }
  1941. };
  1942. if(this.settings.core.worker && window.Blob && window.URL && window.Worker) {
  1943. try {
  1944. if(this._wrk === null) {
  1945. this._wrk = window.URL.createObjectURL(
  1946. new window.Blob(
  1947. ['self.onmessage = ' + func.toString()],
  1948. {type:"text/javascript"}
  1949. )
  1950. );
  1951. }
  1952. if(!this._data.core.working || force_processing) {
  1953. this._data.core.working = true;
  1954. w = new window.Worker(this._wrk);
  1955. w.onmessage = $.proxy(function (e) {
  1956. rslt.call(this, e.data, true);
  1957. try { w.terminate(); w = null; } catch(ignore) { }
  1958. if(this._data.core.worker_queue.length) {
  1959. this._append_json_data.apply(this, this._data.core.worker_queue.shift());
  1960. }
  1961. else {
  1962. this._data.core.working = false;
  1963. }
  1964. }, this);
  1965. if(!args.par) {
  1966. if(this._data.core.worker_queue.length) {
  1967. this._append_json_data.apply(this, this._data.core.worker_queue.shift());
  1968. }
  1969. else {
  1970. this._data.core.working = false;
  1971. }
  1972. }
  1973. else {
  1974. w.postMessage(args);
  1975. }
  1976. }
  1977. else {
  1978. this._data.core.worker_queue.push([dom, data, cb, true]);
  1979. }
  1980. }
  1981. catch(e) {
  1982. rslt.call(this, func(args), false);
  1983. if(this._data.core.worker_queue.length) {
  1984. this._append_json_data.apply(this, this._data.core.worker_queue.shift());
  1985. }
  1986. else {
  1987. this._data.core.working = false;
  1988. }
  1989. }
  1990. }
  1991. else {
  1992. rslt.call(this, func(args), false);
  1993. }
  1994. },
  1995. /**
  1996. * parses a node from a jQuery object and appends them to the in memory tree model. Used internally.
  1997. * @private
  1998. * @name _parse_model_from_html(d [, p, ps])
  1999. * @param {jQuery} d the jQuery object to parse
  2000. * @param {String} p the parent ID
  2001. * @param {Array} ps list of all parents
  2002. * @return {String} the ID of the object added to the model
  2003. */
  2004. _parse_model_from_html : function (d, p, ps) {
  2005. if(!ps) { ps = []; }
  2006. else { ps = [].concat(ps); }
  2007. if(p) { ps.unshift(p); }
  2008. var c, e, m = this._model.data,
  2009. data = {
  2010. id : false,
  2011. text : false,
  2012. icon : true,
  2013. parent : p,
  2014. parents : ps,
  2015. children : [],
  2016. children_d : [],
  2017. data : null,
  2018. state : { },
  2019. li_attr : { id : false },
  2020. a_attr : { href : '#' },
  2021. original : false
  2022. }, i, tmp, tid;
  2023. for(i in this._model.default_state) {
  2024. if(this._model.default_state.hasOwnProperty(i)) {
  2025. data.state[i] = this._model.default_state[i];
  2026. }
  2027. }
  2028. tmp = $.vakata.attributes(d, true);
  2029. $.each(tmp, function (i, v) {
  2030. v = $.vakata.trim(v);
  2031. if(!v.length) { return true; }
  2032. data.li_attr[i] = v;
  2033. if(i === 'id') {
  2034. data.id = v.toString();
  2035. }
  2036. });
  2037. tmp = d.children('a').first();
  2038. if(tmp.length) {
  2039. tmp = $.vakata.attributes(tmp, true);
  2040. $.each(tmp, function (i, v) {
  2041. v = $.vakata.trim(v);
  2042. if(v.length) {
  2043. data.a_attr[i] = v;
  2044. }
  2045. });
  2046. }
  2047. tmp = d.children("a").first().length ? d.children("a").first().clone() : d.clone();
  2048. tmp.children("ins, i, ul").remove();
  2049. tmp = tmp.html();
  2050. tmp = $('<div></div>').html(tmp);
  2051. data.text = this.settings.core.force_text ? tmp.text() : tmp.html();
  2052. tmp = d.data();
  2053. data.data = tmp ? $.extend(true, {}, tmp) : null;
  2054. data.state.opened = d.hasClass('jstree-open');
  2055. data.state.selected = d.children('a').hasClass('jstree-clicked');
  2056. data.state.disabled = d.children('a').hasClass('jstree-disabled');
  2057. if(data.data && data.data.jstree) {
  2058. for(i in data.data.jstree) {
  2059. if(data.data.jstree.hasOwnProperty(i)) {
  2060. data.state[i] = data.data.jstree[i];
  2061. }
  2062. }
  2063. }
  2064. tmp = d.children("a").children(".jstree-themeicon");
  2065. if(tmp.length) {
  2066. data.icon = tmp.hasClass('jstree-themeicon-hidden') ? false : tmp.attr('rel');
  2067. }
  2068. if(data.state.icon !== undefined) {
  2069. data.icon = data.state.icon;
  2070. }
  2071. if(data.icon === undefined || data.icon === null || data.icon === "") {
  2072. data.icon = true;
  2073. }
  2074. tmp = d.children("ul").children("li");
  2075. do {
  2076. tid = 'j' + this._id + '_' + (++this._cnt);
  2077. } while(m[tid]);
  2078. data.id = data.li_attr.id ? data.li_attr.id.toString() : tid;
  2079. if(tmp.length) {
  2080. tmp.each($.proxy(function (i, v) {
  2081. c = this._parse_model_from_html($(v), data.id, ps);
  2082. e = this._model.data[c];
  2083. data.children.push(c);
  2084. if(e.children_d.length) {
  2085. data.children_d = data.children_d.concat(e.children_d);
  2086. }
  2087. }, this));
  2088. data.children_d = data.children_d.concat(data.children);
  2089. }
  2090. else {
  2091. if(d.hasClass('jstree-closed')) {
  2092. data.state.loaded = false;
  2093. }
  2094. }
  2095. if(data.li_attr['class']) {
  2096. data.li_attr['class'] = data.li_attr['class'].replace('jstree-closed','').replace('jstree-open','');
  2097. }
  2098. if(data.a_attr['class']) {
  2099. data.a_attr['class'] = data.a_attr['class'].replace('jstree-clicked','').replace('jstree-disabled','');
  2100. }
  2101. m[data.id] = data;
  2102. if(data.state.selected) {
  2103. this._data.core.selected.push(data.id);
  2104. }
  2105. return data.id;
  2106. },
  2107. /**
  2108. * parses a node from a JSON object (used when dealing with flat data, which has no nesting of children, but has id and parent properties) and appends it to the in memory tree model. Used internally.
  2109. * @private
  2110. * @name _parse_model_from_flat_json(d [, p, ps])
  2111. * @param {Object} d the JSON object to parse
  2112. * @param {String} p the parent ID
  2113. * @param {Array} ps list of all parents
  2114. * @return {String} the ID of the object added to the model
  2115. */
  2116. _parse_model_from_flat_json : function (d, p, ps) {
  2117. if(!ps) { ps = []; }
  2118. else { ps = ps.concat(); }
  2119. if(p) { ps.unshift(p); }
  2120. var tid = d.id.toString(),
  2121. m = this._model.data,
  2122. df = this._model.default_state,
  2123. i, j, c, e,
  2124. tmp = {
  2125. id : tid,
  2126. text : d.text || '',
  2127. icon : d.icon !== undefined ? d.icon : true,
  2128. parent : p,
  2129. parents : ps,
  2130. children : d.children || [],
  2131. children_d : d.children_d || [],
  2132. data : d.data,
  2133. state : { },
  2134. li_attr : { id : false },
  2135. a_attr : { href : '#' },
  2136. original : false
  2137. };
  2138. for(i in df) {
  2139. if(df.hasOwnProperty(i)) {
  2140. tmp.state[i] = df[i];
  2141. }
  2142. }
  2143. if(d && d.data && d.data.jstree && d.data.jstree.icon) {
  2144. tmp.icon = d.data.jstree.icon;
  2145. }
  2146. if(tmp.icon === undefined || tmp.icon === null || tmp.icon === "") {
  2147. tmp.icon = true;
  2148. }
  2149. if(d && d.data) {
  2150. tmp.data = d.data;
  2151. if(d.data.jstree) {
  2152. for(i in d.data.jstree) {
  2153. if(d.data.jstree.hasOwnProperty(i)) {
  2154. tmp.state[i] = d.data.jstree[i];
  2155. }
  2156. }
  2157. }
  2158. }
  2159. if(d && typeof d.state === 'object') {
  2160. for (i in d.state) {
  2161. if(d.state.hasOwnProperty(i)) {
  2162. tmp.state[i] = d.state[i];
  2163. }
  2164. }
  2165. }
  2166. if(d && typeof d.li_attr === 'object') {
  2167. for (i in d.li_attr) {
  2168. if(d.li_attr.hasOwnProperty(i)) {
  2169. tmp.li_attr[i] = d.li_attr[i];
  2170. }
  2171. }
  2172. }
  2173. if(!tmp.li_attr.id) {
  2174. tmp.li_attr.id = tid;
  2175. }
  2176. if(d && typeof d.a_attr === 'object') {
  2177. for (i in d.a_attr) {
  2178. if(d.a_attr.hasOwnProperty(i)) {
  2179. tmp.a_attr[i] = d.a_attr[i];
  2180. }
  2181. }
  2182. }
  2183. if(d && d.children && d.children === true) {
  2184. tmp.state.loaded = false;
  2185. tmp.children = [];
  2186. tmp.children_d = [];
  2187. }
  2188. m[tmp.id] = tmp;
  2189. for(i = 0, j = tmp.children.length; i < j; i++) {
  2190. c = this._parse_model_from_flat_json(m[tmp.children[i]], tmp.id, ps);
  2191. e = m[c];
  2192. tmp.children_d.push(c);
  2193. if(e.children_d.length) {
  2194. tmp.children_d = tmp.children_d.concat(e.children_d);
  2195. }
  2196. }
  2197. delete d.data;
  2198. delete d.children;
  2199. m[tmp.id].original = d;
  2200. if(tmp.state.selected) {
  2201. this._data.core.selected.push(tmp.id);
  2202. }
  2203. return tmp.id;
  2204. },
  2205. /**
  2206. * parses a node from a JSON object and appends it to the in memory tree model. Used internally.
  2207. * @private
  2208. * @name _parse_model_from_json(d [, p, ps])
  2209. * @param {Object} d the JSON object to parse
  2210. * @param {String} p the parent ID
  2211. * @param {Array} ps list of all parents
  2212. * @return {String} the ID of the object added to the model
  2213. */
  2214. _parse_model_from_json : function (d, p, ps) {
  2215. if(!ps) { ps = []; }
  2216. else { ps = ps.concat(); }
  2217. if(p) { ps.unshift(p); }
  2218. var tid = false, i, j, c, e, m = this._model.data, df = this._model.default_state, tmp;
  2219. do {
  2220. tid = 'j' + this._id + '_' + (++this._cnt);
  2221. } while(m[tid]);
  2222. tmp = {
  2223. id : false,
  2224. text : typeof d === 'string' ? d : '',
  2225. icon : typeof d === 'object' && d.icon !== undefined ? d.icon : true,
  2226. parent : p,
  2227. parents : ps,
  2228. children : [],
  2229. children_d : [],
  2230. data : null,
  2231. state : { },
  2232. li_attr : { id : false },
  2233. a_attr : { href : '#' },
  2234. original : false
  2235. };
  2236. for(i in df) {
  2237. if(df.hasOwnProperty(i)) {
  2238. tmp.state[i] = df[i];
  2239. }
  2240. }
  2241. if(d && d.id) { tmp.id = d.id.toString(); }
  2242. if(d && d.text) { tmp.text = d.text; }
  2243. if(d && d.data && d.data.jstree && d.data.jstree.icon) {
  2244. tmp.icon = d.data.jstree.icon;
  2245. }
  2246. if(tmp.icon === undefined || tmp.icon === null || tmp.icon === "") {
  2247. tmp.icon = true;
  2248. }
  2249. if(d && d.data) {
  2250. tmp.data = d.data;
  2251. if(d.data.jstree) {
  2252. for(i in d.data.jstree) {
  2253. if(d.data.jstree.hasOwnProperty(i)) {
  2254. tmp.state[i] = d.data.jstree[i];
  2255. }
  2256. }
  2257. }
  2258. }
  2259. if(d && typeof d.state === 'object') {
  2260. for (i in d.state) {
  2261. if(d.state.hasOwnProperty(i)) {
  2262. tmp.state[i] = d.state[i];
  2263. }
  2264. }
  2265. }
  2266. if(d && typeof d.li_attr === 'object') {
  2267. for (i in d.li_attr) {
  2268. if(d.li_attr.hasOwnProperty(i)) {
  2269. tmp.li_attr[i] = d.li_attr[i];
  2270. }
  2271. }
  2272. }
  2273. if(tmp.li_attr.id && !tmp.id) {
  2274. tmp.id = tmp.li_attr.id.toString();
  2275. }
  2276. if(!tmp.id) {
  2277. tmp.id = tid;
  2278. }
  2279. if(!tmp.li_attr.id) {
  2280. tmp.li_attr.id = tmp.id;
  2281. }
  2282. if(d && typeof d.a_attr === 'object') {
  2283. for (i in d.a_attr) {
  2284. if(d.a_attr.hasOwnProperty(i)) {
  2285. tmp.a_attr[i] = d.a_attr[i];
  2286. }
  2287. }
  2288. }
  2289. if(d && d.children && d.children.length) {
  2290. for(i = 0, j = d.children.length; i < j; i++) {
  2291. c = this._parse_model_from_json(d.children[i], tmp.id, ps);
  2292. e = m[c];
  2293. tmp.children.push(c);
  2294. if(e.children_d.length) {
  2295. tmp.children_d = tmp.children_d.concat(e.children_d);
  2296. }
  2297. }
  2298. tmp.children_d = tmp.children.concat(tmp.children_d);
  2299. }
  2300. if(d && d.children && d.children === true) {
  2301. tmp.state.loaded = false;
  2302. tmp.children = [];
  2303. tmp.children_d = [];
  2304. }
  2305. delete d.data;
  2306. delete d.children;
  2307. tmp.original = d;
  2308. m[tmp.id] = tmp;
  2309. if(tmp.state.selected) {
  2310. this._data.core.selected.push(tmp.id);
  2311. }
  2312. return tmp.id;
  2313. },
  2314. /**
  2315. * redraws all nodes that need to be redrawn. Used internally.
  2316. * @private
  2317. * @name _redraw()
  2318. * @trigger redraw.jstree
  2319. */
  2320. _redraw : function () {
  2321. var nodes = this._model.force_full_redraw ? this._model.data[$.jstree.root].children.concat([]) : this._model.changed.concat([]),
  2322. f = document.createElement('UL'), tmp, i, j, fe = this._data.core.focused;
  2323. for(i = 0, j = nodes.length; i < j; i++) {
  2324. tmp = this.redraw_node(nodes[i], true, this._model.force_full_redraw);
  2325. if(tmp && this._model.force_full_redraw) {
  2326. f.appendChild(tmp);
  2327. }
  2328. }
  2329. if(this._model.force_full_redraw) {
  2330. f.className = this.get_container_ul()[0].className;
  2331. f.setAttribute('role','group');
  2332. this.element.empty().append(f);
  2333. //this.get_container_ul()[0].appendChild(f);
  2334. }
  2335. if(fe !== null && this.settings.core.restore_focus) {
  2336. tmp = this.get_node(fe, true);
  2337. if(tmp && tmp.length && tmp.children('.jstree-anchor')[0] !== document.activeElement) {
  2338. tmp.children('.jstree-anchor').focus();
  2339. }
  2340. else {
  2341. this._data.core.focused = null;
  2342. }
  2343. }
  2344. this._model.force_full_redraw = false;
  2345. this._model.changed = [];
  2346. /**
  2347. * triggered after nodes are redrawn
  2348. * @event
  2349. * @name redraw.jstree
  2350. * @param {array} nodes the redrawn nodes
  2351. */
  2352. this.trigger('redraw', { "nodes" : nodes });
  2353. },
  2354. /**
  2355. * redraws all nodes that need to be redrawn or optionally - the whole tree
  2356. * @name redraw([full])
  2357. * @param {Boolean} full if set to `true` all nodes are redrawn.
  2358. */
  2359. redraw : function (full) {
  2360. if(full) {
  2361. this._model.force_full_redraw = true;
  2362. }
  2363. //if(this._model.redraw_timeout) {
  2364. // clearTimeout(this._model.redraw_timeout);
  2365. //}
  2366. //this._model.redraw_timeout = setTimeout($.proxy(this._redraw, this),0);
  2367. this._redraw();
  2368. },
  2369. /**
  2370. * redraws a single node's children. Used internally.
  2371. * @private
  2372. * @name draw_children(node)
  2373. * @param {mixed} node the node whose children will be redrawn
  2374. */
  2375. draw_children : function (node) {
  2376. var obj = this.get_node(node),
  2377. i = false,
  2378. j = false,
  2379. k = false,
  2380. d = document;
  2381. if(!obj) { return false; }
  2382. if(obj.id === $.jstree.root) { return this.redraw(true); }
  2383. node = this.get_node(node, true);
  2384. if(!node || !node.length) { return false; } // TODO: quick toggle
  2385. node.children('.jstree-children').remove();
  2386. node = node[0];
  2387. if(obj.children.length && obj.state.loaded) {
  2388. k = d.createElement('UL');
  2389. k.setAttribute('role', 'group');
  2390. k.className = 'jstree-children';
  2391. for(i = 0, j = obj.children.length; i < j; i++) {
  2392. k.appendChild(this.redraw_node(obj.children[i], true, true));
  2393. }
  2394. node.appendChild(k);
  2395. }
  2396. },
  2397. /**
  2398. * redraws a single node. Used internally.
  2399. * @private
  2400. * @name redraw_node(node, deep, is_callback, force_render)
  2401. * @param {mixed} node the node to redraw
  2402. * @param {Boolean} deep should child nodes be redrawn too
  2403. * @param {Boolean} is_callback is this a recursion call
  2404. * @param {Boolean} force_render should children of closed parents be drawn anyway
  2405. */
  2406. redraw_node : function (node, deep, is_callback, force_render) {
  2407. var obj = this.get_node(node),
  2408. par = false,
  2409. ind = false,
  2410. old = false,
  2411. i = false,
  2412. j = false,
  2413. k = false,
  2414. c = '',
  2415. d = document,
  2416. m = this._model.data,
  2417. f = false,
  2418. s = false,
  2419. tmp = null,
  2420. t = 0,
  2421. l = 0,
  2422. has_children = false,
  2423. last_sibling = false;
  2424. if(!obj) { return false; }
  2425. if(obj.id === $.jstree.root) { return this.redraw(true); }
  2426. deep = deep || obj.children.length === 0;
  2427. node = !document.querySelector ? document.getElementById(obj.id) : this.element[0].querySelector('#' + ("0123456789".indexOf(obj.id[0]) !== -1 ? '\\3' + obj.id[0] + ' ' + obj.id.substr(1).replace($.jstree.idregex,'\\$&') : obj.id.replace($.jstree.idregex,'\\$&')) ); //, this.element);
  2428. if(!node) {
  2429. deep = true;
  2430. //node = d.createElement('LI');
  2431. if(!is_callback) {
  2432. par = obj.parent !== $.jstree.root ? $('#' + obj.parent.replace($.jstree.idregex,'\\$&'), this.element)[0] : null;
  2433. if(par !== null && (!par || !m[obj.parent].state.opened)) {
  2434. return false;
  2435. }
  2436. ind = $.inArray(obj.id, par === null ? m[$.jstree.root].children : m[obj.parent].children);
  2437. }
  2438. }
  2439. else {
  2440. node = $(node);
  2441. if(!is_callback) {
  2442. par = node.parent().parent()[0];
  2443. if(par === this.element[0]) {
  2444. par = null;
  2445. }
  2446. ind = node.index();
  2447. }
  2448. // m[obj.id].data = node.data(); // use only node's data, no need to touch jquery storage
  2449. if(!deep && obj.children.length && !node.children('.jstree-children').length) {
  2450. deep = true;
  2451. }
  2452. if(!deep) {
  2453. old = node.children('.jstree-children')[0];
  2454. }
  2455. f = node.children('.jstree-anchor')[0] === document.activeElement;
  2456. node.remove();
  2457. //node = d.createElement('LI');
  2458. //node = node[0];
  2459. }
  2460. node = this._data.core.node.cloneNode(true);
  2461. // node is DOM, deep is boolean
  2462. c = 'jstree-node ';
  2463. for(i in obj.li_attr) {
  2464. if(obj.li_attr.hasOwnProperty(i)) {
  2465. if(i === 'id') { continue; }
  2466. if(i !== 'class') {
  2467. node.setAttribute(i, obj.li_attr[i]);
  2468. }
  2469. else {
  2470. c += obj.li_attr[i];
  2471. }
  2472. }
  2473. }
  2474. if(!obj.a_attr.id) {
  2475. obj.a_attr.id = obj.id + '_anchor';
  2476. }
  2477. node.childNodes[1].setAttribute('aria-selected', !!obj.state.selected);
  2478. node.childNodes[1].setAttribute('aria-level', obj.parents.length);
  2479. if(this.settings.core.compute_elements_positions) {
  2480. node.childNodes[1].setAttribute('aria-setsize', m[obj.parent].children.length);
  2481. node.childNodes[1].setAttribute('aria-posinset', m[obj.parent].children.indexOf(obj.id) + 1);
  2482. }
  2483. if(obj.state.disabled) {
  2484. node.childNodes[1].setAttribute('aria-disabled', true);
  2485. }
  2486. for(i = 0, j = obj.children.length; i < j; i++) {
  2487. if(!m[obj.children[i]].state.hidden) {
  2488. has_children = true;
  2489. break;
  2490. }
  2491. }
  2492. if(obj.parent !== null && m[obj.parent] && !obj.state.hidden) {
  2493. i = $.inArray(obj.id, m[obj.parent].children);
  2494. last_sibling = obj.id;
  2495. if(i !== -1) {
  2496. i++;
  2497. for(j = m[obj.parent].children.length; i < j; i++) {
  2498. if(!m[m[obj.parent].children[i]].state.hidden) {
  2499. last_sibling = m[obj.parent].children[i];
  2500. }
  2501. if(last_sibling !== obj.id) {
  2502. break;
  2503. }
  2504. }
  2505. }
  2506. }
  2507. if(obj.state.hidden) {
  2508. c += ' jstree-hidden';
  2509. }
  2510. if (obj.state.loading) {
  2511. c += ' jstree-loading';
  2512. }
  2513. if(obj.state.loaded && !has_children) {
  2514. c += ' jstree-leaf';
  2515. }
  2516. else {
  2517. c += obj.state.opened && obj.state.loaded ? ' jstree-open' : ' jstree-closed';
  2518. node.childNodes[1].setAttribute('aria-expanded', (obj.state.opened && obj.state.loaded) );
  2519. }
  2520. if(last_sibling === obj.id) {
  2521. c += ' jstree-last';
  2522. }
  2523. node.id = obj.id;
  2524. node.className = c;
  2525. c = ( obj.state.selected ? ' jstree-clicked' : '') + ( obj.state.disabled ? ' jstree-disabled' : '');
  2526. for(j in obj.a_attr) {
  2527. if(obj.a_attr.hasOwnProperty(j)) {
  2528. if(j === 'href' && obj.a_attr[j] === '#') { continue; }
  2529. if(j !== 'class') {
  2530. node.childNodes[1].setAttribute(j, obj.a_attr[j]);
  2531. }
  2532. else {
  2533. c += ' ' + obj.a_attr[j];
  2534. }
  2535. }
  2536. }
  2537. if(c.length) {
  2538. node.childNodes[1].className = 'jstree-anchor ' + c;
  2539. }
  2540. if((obj.icon && obj.icon !== true) || obj.icon === false) {
  2541. if(obj.icon === false) {
  2542. node.childNodes[1].childNodes[0].className += ' jstree-themeicon-hidden';
  2543. }
  2544. else if(obj.icon.indexOf('/') === -1 && obj.icon.indexOf('.') === -1) {
  2545. node.childNodes[1].childNodes[0].className += ' ' + obj.icon + ' jstree-themeicon-custom';
  2546. }
  2547. else {
  2548. node.childNodes[1].childNodes[0].style.backgroundImage = 'url("'+obj.icon+'")';
  2549. node.childNodes[1].childNodes[0].style.backgroundPosition = 'center center';
  2550. node.childNodes[1].childNodes[0].style.backgroundSize = 'auto';
  2551. node.childNodes[1].childNodes[0].className += ' jstree-themeicon-custom';
  2552. }
  2553. }
  2554. if(this.settings.core.force_text) {
  2555. node.childNodes[1].appendChild(d.createTextNode(obj.text));
  2556. }
  2557. else {
  2558. node.childNodes[1].innerHTML += obj.text;
  2559. }
  2560. if(deep && obj.children.length && (obj.state.opened || force_render) && obj.state.loaded) {
  2561. k = d.createElement('UL');
  2562. k.setAttribute('role', 'group');
  2563. k.className = 'jstree-children';
  2564. for(i = 0, j = obj.children.length; i < j; i++) {
  2565. k.appendChild(this.redraw_node(obj.children[i], deep, true));
  2566. }
  2567. node.appendChild(k);
  2568. }
  2569. if(old) {
  2570. node.appendChild(old);
  2571. }
  2572. if(!is_callback) {
  2573. // append back using par / ind
  2574. if(!par) {
  2575. par = this.element[0];
  2576. }
  2577. for(i = 0, j = par.childNodes.length; i < j; i++) {
  2578. if(par.childNodes[i] && par.childNodes[i].className && par.childNodes[i].className.indexOf('jstree-children') !== -1) {
  2579. tmp = par.childNodes[i];
  2580. break;
  2581. }
  2582. }
  2583. if(!tmp) {
  2584. tmp = d.createElement('UL');
  2585. tmp.setAttribute('role', 'group');
  2586. tmp.className = 'jstree-children';
  2587. par.appendChild(tmp);
  2588. }
  2589. par = tmp;
  2590. if(ind < par.childNodes.length) {
  2591. par.insertBefore(node, par.childNodes[ind]);
  2592. }
  2593. else {
  2594. par.appendChild(node);
  2595. }
  2596. if(f) {
  2597. t = this.element[0].scrollTop;
  2598. l = this.element[0].scrollLeft;
  2599. node.childNodes[1].focus();
  2600. this.element[0].scrollTop = t;
  2601. this.element[0].scrollLeft = l;
  2602. }
  2603. }
  2604. if(obj.state.opened && !obj.state.loaded) {
  2605. obj.state.opened = false;
  2606. setTimeout($.proxy(function () {
  2607. this.open_node(obj.id, false, 0);
  2608. }, this), 0);
  2609. }
  2610. return node;
  2611. },
  2612. /**
  2613. * opens a node, revealing its children. If the node is not loaded it will be loaded and opened once ready.
  2614. * @name open_node(obj [, callback, animation])
  2615. * @param {mixed} obj the node to open
  2616. * @param {Function} callback a function to execute once the node is opened
  2617. * @param {Number} animation the animation duration in milliseconds when opening the node (overrides the `core.animation` setting). Use `false` for no animation.
  2618. * @trigger open_node.jstree, after_open.jstree, before_open.jstree
  2619. */
  2620. open_node : function (obj, callback, animation) {
  2621. var t1, t2, d, t;
  2622. if($.isArray(obj)) {
  2623. obj = obj.slice();
  2624. for(t1 = 0, t2 = obj.length; t1 < t2; t1++) {
  2625. this.open_node(obj[t1], callback, animation);
  2626. }
  2627. return true;
  2628. }
  2629. obj = this.get_node(obj);
  2630. if(!obj || obj.id === $.jstree.root) {
  2631. return false;
  2632. }
  2633. animation = animation === undefined ? this.settings.core.animation : animation;
  2634. if(!this.is_closed(obj)) {
  2635. if(callback) {
  2636. callback.call(this, obj, false);
  2637. }
  2638. return false;
  2639. }
  2640. if(!this.is_loaded(obj)) {
  2641. if(this.is_loading(obj)) {
  2642. return setTimeout($.proxy(function () {
  2643. this.open_node(obj, callback, animation);
  2644. }, this), 500);
  2645. }
  2646. this.load_node(obj, function (o, ok) {
  2647. return ok ? this.open_node(o, callback, animation) : (callback ? callback.call(this, o, false) : false);
  2648. });
  2649. }
  2650. else {
  2651. d = this.get_node(obj, true);
  2652. t = this;
  2653. if(d.length) {
  2654. if(animation && d.children(".jstree-children").length) {
  2655. d.children(".jstree-children").stop(true, true);
  2656. }
  2657. if(obj.children.length && !this._firstChild(d.children('.jstree-children')[0])) {
  2658. this.draw_children(obj);
  2659. //d = this.get_node(obj, true);
  2660. }
  2661. if(!animation) {
  2662. this.trigger('before_open', { "node" : obj });
  2663. d[0].className = d[0].className.replace('jstree-closed', 'jstree-open');
  2664. d[0].childNodes[1].setAttribute("aria-expanded", true);
  2665. }
  2666. else {
  2667. this.trigger('before_open', { "node" : obj });
  2668. d
  2669. .children(".jstree-children").css("display","none").end()
  2670. .removeClass("jstree-closed").addClass("jstree-open")
  2671. .children('.jstree-anchor').attr("aria-expanded", true).end()
  2672. .children(".jstree-children").stop(true, true)
  2673. .slideDown(animation, function () {
  2674. this.style.display = "";
  2675. if (t.element) {
  2676. t.trigger("after_open", { "node" : obj });
  2677. }
  2678. });
  2679. }
  2680. }
  2681. obj.state.opened = true;
  2682. if(callback) {
  2683. callback.call(this, obj, true);
  2684. }
  2685. if(!d.length) {
  2686. /**
  2687. * triggered when a node is about to be opened (if the node is supposed to be in the DOM, it will be, but it won't be visible yet)
  2688. * @event
  2689. * @name before_open.jstree
  2690. * @param {Object} node the opened node
  2691. */
  2692. this.trigger('before_open', { "node" : obj });
  2693. }
  2694. /**
  2695. * triggered when a node is opened (if there is an animation it will not be completed yet)
  2696. * @event
  2697. * @name open_node.jstree
  2698. * @param {Object} node the opened node
  2699. */
  2700. this.trigger('open_node', { "node" : obj });
  2701. if(!animation || !d.length) {
  2702. /**
  2703. * triggered when a node is opened and the animation is complete
  2704. * @event
  2705. * @name after_open.jstree
  2706. * @param {Object} node the opened node
  2707. */
  2708. this.trigger("after_open", { "node" : obj });
  2709. }
  2710. return true;
  2711. }
  2712. },
  2713. /**
  2714. * opens every parent of a node (node should be loaded)
  2715. * @name _open_to(obj)
  2716. * @param {mixed} obj the node to reveal
  2717. * @private
  2718. */
  2719. _open_to : function (obj) {
  2720. obj = this.get_node(obj);
  2721. if(!obj || obj.id === $.jstree.root) {
  2722. return false;
  2723. }
  2724. var i, j, p = obj.parents;
  2725. for(i = 0, j = p.length; i < j; i+=1) {
  2726. if(i !== $.jstree.root) {
  2727. this.open_node(p[i], false, 0);
  2728. }
  2729. }
  2730. return $('#' + obj.id.replace($.jstree.idregex,'\\$&'), this.element);
  2731. },
  2732. /**
  2733. * closes a node, hiding its children
  2734. * @name close_node(obj [, animation])
  2735. * @param {mixed} obj the node to close
  2736. * @param {Number} animation the animation duration in milliseconds when closing the node (overrides the `core.animation` setting). Use `false` for no animation.
  2737. * @trigger close_node.jstree, after_close.jstree
  2738. */
  2739. close_node : function (obj, animation) {
  2740. var t1, t2, t, d;
  2741. if($.isArray(obj)) {
  2742. obj = obj.slice();
  2743. for(t1 = 0, t2 = obj.length; t1 < t2; t1++) {
  2744. this.close_node(obj[t1], animation);
  2745. }
  2746. return true;
  2747. }
  2748. obj = this.get_node(obj);
  2749. if(!obj || obj.id === $.jstree.root) {
  2750. return false;
  2751. }
  2752. if(this.is_closed(obj)) {
  2753. return false;
  2754. }
  2755. animation = animation === undefined ? this.settings.core.animation : animation;
  2756. t = this;
  2757. d = this.get_node(obj, true);
  2758. obj.state.opened = false;
  2759. /**
  2760. * triggered when a node is closed (if there is an animation it will not be complete yet)
  2761. * @event
  2762. * @name close_node.jstree
  2763. * @param {Object} node the closed node
  2764. */
  2765. this.trigger('close_node',{ "node" : obj });
  2766. if(!d.length) {
  2767. /**
  2768. * triggered when a node is closed and the animation is complete
  2769. * @event
  2770. * @name after_close.jstree
  2771. * @param {Object} node the closed node
  2772. */
  2773. this.trigger("after_close", { "node" : obj });
  2774. }
  2775. else {
  2776. if(!animation) {
  2777. d[0].className = d[0].className.replace('jstree-open', 'jstree-closed');
  2778. d.children('.jstree-anchor').attr("aria-expanded", false);
  2779. d.children('.jstree-children').remove();
  2780. this.trigger("after_close", { "node" : obj });
  2781. }
  2782. else {
  2783. d
  2784. .children(".jstree-children").attr("style","display:block !important").end()
  2785. .removeClass("jstree-open").addClass("jstree-closed")
  2786. .children('.jstree-anchor').attr("aria-expanded", false).end()
  2787. .children(".jstree-children").stop(true, true).slideUp(animation, function () {
  2788. this.style.display = "";
  2789. d.children('.jstree-children').remove();
  2790. if (t.element) {
  2791. t.trigger("after_close", { "node" : obj });
  2792. }
  2793. });
  2794. }
  2795. }
  2796. },
  2797. /**
  2798. * toggles a node - closing it if it is open, opening it if it is closed
  2799. * @name toggle_node(obj)
  2800. * @param {mixed} obj the node to toggle
  2801. */
  2802. toggle_node : function (obj) {
  2803. var t1, t2;
  2804. if($.isArray(obj)) {
  2805. obj = obj.slice();
  2806. for(t1 = 0, t2 = obj.length; t1 < t2; t1++) {
  2807. this.toggle_node(obj[t1]);
  2808. }
  2809. return true;
  2810. }
  2811. if(this.is_closed(obj)) {
  2812. return this.open_node(obj);
  2813. }
  2814. if(this.is_open(obj)) {
  2815. return this.close_node(obj);
  2816. }
  2817. },
  2818. /**
  2819. * opens all nodes within a node (or the tree), revealing their children. If the node is not loaded it will be loaded and opened once ready.
  2820. * @name open_all([obj, animation, original_obj])
  2821. * @param {mixed} obj the node to open recursively, omit to open all nodes in the tree
  2822. * @param {Number} animation the animation duration in milliseconds when opening the nodes, the default is no animation
  2823. * @param {jQuery} reference to the node that started the process (internal use)
  2824. * @trigger open_all.jstree
  2825. */
  2826. open_all : function (obj, animation, original_obj) {
  2827. if(!obj) { obj = $.jstree.root; }
  2828. obj = this.get_node(obj);
  2829. if(!obj) { return false; }
  2830. var dom = obj.id === $.jstree.root ? this.get_container_ul() : this.get_node(obj, true), i, j, _this;
  2831. if(!dom.length) {
  2832. for(i = 0, j = obj.children_d.length; i < j; i++) {
  2833. if(this.is_closed(this._model.data[obj.children_d[i]])) {
  2834. this._model.data[obj.children_d[i]].state.opened = true;
  2835. }
  2836. }
  2837. return this.trigger('open_all', { "node" : obj });
  2838. }
  2839. original_obj = original_obj || dom;
  2840. _this = this;
  2841. dom = this.is_closed(obj) ? dom.find('.jstree-closed').addBack() : dom.find('.jstree-closed');
  2842. dom.each(function () {
  2843. _this.open_node(
  2844. this,
  2845. function(node, status) { if(status && this.is_parent(node)) { this.open_all(node, animation, original_obj); } },
  2846. animation || 0
  2847. );
  2848. });
  2849. if(original_obj.find('.jstree-closed').length === 0) {
  2850. /**
  2851. * triggered when an `open_all` call completes
  2852. * @event
  2853. * @name open_all.jstree
  2854. * @param {Object} node the opened node
  2855. */
  2856. this.trigger('open_all', { "node" : this.get_node(original_obj) });
  2857. }
  2858. },
  2859. /**
  2860. * closes all nodes within a node (or the tree), revealing their children
  2861. * @name close_all([obj, animation])
  2862. * @param {mixed} obj the node to close recursively, omit to close all nodes in the tree
  2863. * @param {Number} animation the animation duration in milliseconds when closing the nodes, the default is no animation
  2864. * @trigger close_all.jstree
  2865. */
  2866. close_all : function (obj, animation) {
  2867. if(!obj) { obj = $.jstree.root; }
  2868. obj = this.get_node(obj);
  2869. if(!obj) { return false; }
  2870. var dom = obj.id === $.jstree.root ? this.get_container_ul() : this.get_node(obj, true),
  2871. _this = this, i, j;
  2872. if(dom.length) {
  2873. dom = this.is_open(obj) ? dom.find('.jstree-open').addBack() : dom.find('.jstree-open');
  2874. $(dom.get().reverse()).each(function () { _this.close_node(this, animation || 0); });
  2875. }
  2876. for(i = 0, j = obj.children_d.length; i < j; i++) {
  2877. this._model.data[obj.children_d[i]].state.opened = false;
  2878. }
  2879. /**
  2880. * triggered when an `close_all` call completes
  2881. * @event
  2882. * @name close_all.jstree
  2883. * @param {Object} node the closed node
  2884. */
  2885. this.trigger('close_all', { "node" : obj });
  2886. },
  2887. /**
  2888. * checks if a node is disabled (not selectable)
  2889. * @name is_disabled(obj)
  2890. * @param {mixed} obj
  2891. * @return {Boolean}
  2892. */
  2893. is_disabled : function (obj) {
  2894. obj = this.get_node(obj);
  2895. return obj && obj.state && obj.state.disabled;
  2896. },
  2897. /**
  2898. * enables a node - so that it can be selected
  2899. * @name enable_node(obj)
  2900. * @param {mixed} obj the node to enable
  2901. * @trigger enable_node.jstree
  2902. */
  2903. enable_node : function (obj) {
  2904. var t1, t2;
  2905. if($.isArray(obj)) {
  2906. obj = obj.slice();
  2907. for(t1 = 0, t2 = obj.length; t1 < t2; t1++) {
  2908. this.enable_node(obj[t1]);
  2909. }
  2910. return true;
  2911. }
  2912. obj = this.get_node(obj);
  2913. if(!obj || obj.id === $.jstree.root) {
  2914. return false;
  2915. }
  2916. obj.state.disabled = false;
  2917. this.get_node(obj,true).children('.jstree-anchor').removeClass('jstree-disabled').attr('aria-disabled', false);
  2918. /**
  2919. * triggered when an node is enabled
  2920. * @event
  2921. * @name enable_node.jstree
  2922. * @param {Object} node the enabled node
  2923. */
  2924. this.trigger('enable_node', { 'node' : obj });
  2925. },
  2926. /**
  2927. * disables a node - so that it can not be selected
  2928. * @name disable_node(obj)
  2929. * @param {mixed} obj the node to disable
  2930. * @trigger disable_node.jstree
  2931. */
  2932. disable_node : function (obj) {
  2933. var t1, t2;
  2934. if($.isArray(obj)) {
  2935. obj = obj.slice();
  2936. for(t1 = 0, t2 = obj.length; t1 < t2; t1++) {
  2937. this.disable_node(obj[t1]);
  2938. }
  2939. return true;
  2940. }
  2941. obj = this.get_node(obj);
  2942. if(!obj || obj.id === $.jstree.root) {
  2943. return false;
  2944. }
  2945. obj.state.disabled = true;
  2946. this.get_node(obj,true).children('.jstree-anchor').addClass('jstree-disabled').attr('aria-disabled', true);
  2947. /**
  2948. * triggered when an node is disabled
  2949. * @event
  2950. * @name disable_node.jstree
  2951. * @param {Object} node the disabled node
  2952. */
  2953. this.trigger('disable_node', { 'node' : obj });
  2954. },
  2955. /**
  2956. * determines if a node is hidden
  2957. * @name is_hidden(obj)
  2958. * @param {mixed} obj the node
  2959. */
  2960. is_hidden : function (obj) {
  2961. obj = this.get_node(obj);
  2962. return obj.state.hidden === true;
  2963. },
  2964. /**
  2965. * hides a node - it is still in the structure but will not be visible
  2966. * @name hide_node(obj)
  2967. * @param {mixed} obj the node to hide
  2968. * @param {Boolean} skip_redraw internal parameter controlling if redraw is called
  2969. * @trigger hide_node.jstree
  2970. */
  2971. hide_node : function (obj, skip_redraw) {
  2972. var t1, t2;
  2973. if($.isArray(obj)) {
  2974. obj = obj.slice();
  2975. for(t1 = 0, t2 = obj.length; t1 < t2; t1++) {
  2976. this.hide_node(obj[t1], true);
  2977. }
  2978. if (!skip_redraw) {
  2979. this.redraw();
  2980. }
  2981. return true;
  2982. }
  2983. obj = this.get_node(obj);
  2984. if(!obj || obj.id === $.jstree.root) {
  2985. return false;
  2986. }
  2987. if(!obj.state.hidden) {
  2988. obj.state.hidden = true;
  2989. this._node_changed(obj.parent);
  2990. if(!skip_redraw) {
  2991. this.redraw();
  2992. }
  2993. /**
  2994. * triggered when an node is hidden
  2995. * @event
  2996. * @name hide_node.jstree
  2997. * @param {Object} node the hidden node
  2998. */
  2999. this.trigger('hide_node', { 'node' : obj });
  3000. }
  3001. },
  3002. /**
  3003. * shows a node
  3004. * @name show_node(obj)
  3005. * @param {mixed} obj the node to show
  3006. * @param {Boolean} skip_redraw internal parameter controlling if redraw is called
  3007. * @trigger show_node.jstree
  3008. */
  3009. show_node : function (obj, skip_redraw) {
  3010. var t1, t2;
  3011. if($.isArray(obj)) {
  3012. obj = obj.slice();
  3013. for(t1 = 0, t2 = obj.length; t1 < t2; t1++) {
  3014. this.show_node(obj[t1], true);
  3015. }
  3016. if (!skip_redraw) {
  3017. this.redraw();
  3018. }
  3019. return true;
  3020. }
  3021. obj = this.get_node(obj);
  3022. if(!obj || obj.id === $.jstree.root) {
  3023. return false;
  3024. }
  3025. if(obj.state.hidden) {
  3026. obj.state.hidden = false;
  3027. this._node_changed(obj.parent);
  3028. if(!skip_redraw) {
  3029. this.redraw();
  3030. }
  3031. /**
  3032. * triggered when an node is shown
  3033. * @event
  3034. * @name show_node.jstree
  3035. * @param {Object} node the shown node
  3036. */
  3037. this.trigger('show_node', { 'node' : obj });
  3038. }
  3039. },
  3040. /**
  3041. * hides all nodes
  3042. * @name hide_all()
  3043. * @trigger hide_all.jstree
  3044. */
  3045. hide_all : function (skip_redraw) {
  3046. var i, m = this._model.data, ids = [];
  3047. for(i in m) {
  3048. if(m.hasOwnProperty(i) && i !== $.jstree.root && !m[i].state.hidden) {
  3049. m[i].state.hidden = true;
  3050. ids.push(i);
  3051. }
  3052. }
  3053. this._model.force_full_redraw = true;
  3054. if(!skip_redraw) {
  3055. this.redraw();
  3056. }
  3057. /**
  3058. * triggered when all nodes are hidden
  3059. * @event
  3060. * @name hide_all.jstree
  3061. * @param {Array} nodes the IDs of all hidden nodes
  3062. */
  3063. this.trigger('hide_all', { 'nodes' : ids });
  3064. return ids;
  3065. },
  3066. /**
  3067. * shows all nodes
  3068. * @name show_all()
  3069. * @trigger show_all.jstree
  3070. */
  3071. show_all : function (skip_redraw) {
  3072. var i, m = this._model.data, ids = [];
  3073. for(i in m) {
  3074. if(m.hasOwnProperty(i) && i !== $.jstree.root && m[i].state.hidden) {
  3075. m[i].state.hidden = false;
  3076. ids.push(i);
  3077. }
  3078. }
  3079. this._model.force_full_redraw = true;
  3080. if(!skip_redraw) {
  3081. this.redraw();
  3082. }
  3083. /**
  3084. * triggered when all nodes are shown
  3085. * @event
  3086. * @name show_all.jstree
  3087. * @param {Array} nodes the IDs of all shown nodes
  3088. */
  3089. this.trigger('show_all', { 'nodes' : ids });
  3090. return ids;
  3091. },
  3092. /**
  3093. * called when a node is selected by the user. Used internally.
  3094. * @private
  3095. * @name activate_node(obj, e)
  3096. * @param {mixed} obj the node
  3097. * @param {Object} e the related event
  3098. * @trigger activate_node.jstree, changed.jstree
  3099. */
  3100. activate_node : function (obj, e) {
  3101. if(this.is_disabled(obj)) {
  3102. return false;
  3103. }
  3104. if(!e || typeof e !== 'object') {
  3105. e = {};
  3106. }
  3107. // ensure last_clicked is still in the DOM, make it fresh (maybe it was moved?) and make sure it is still selected, if not - make last_clicked the last selected node
  3108. this._data.core.last_clicked = this._data.core.last_clicked && this._data.core.last_clicked.id !== undefined ? this.get_node(this._data.core.last_clicked.id) : null;
  3109. if(this._data.core.last_clicked && !this._data.core.last_clicked.state.selected) { this._data.core.last_clicked = null; }
  3110. if(!this._data.core.last_clicked && this._data.core.selected.length) { this._data.core.last_clicked = this.get_node(this._data.core.selected[this._data.core.selected.length - 1]); }
  3111. if(!this.settings.core.multiple || (!e.metaKey && !e.ctrlKey && !e.shiftKey) || (e.shiftKey && (!this._data.core.last_clicked || !this.get_parent(obj) || this.get_parent(obj) !== this._data.core.last_clicked.parent ) )) {
  3112. if(!this.settings.core.multiple && (e.metaKey || e.ctrlKey || e.shiftKey) && this.is_selected(obj)) {
  3113. this.deselect_node(obj, false, e);
  3114. }
  3115. else {
  3116. this.deselect_all(true);
  3117. this.select_node(obj, false, false, e);
  3118. this._data.core.last_clicked = this.get_node(obj);
  3119. }
  3120. }
  3121. else {
  3122. if(e.shiftKey) {
  3123. var o = this.get_node(obj).id,
  3124. l = this._data.core.last_clicked.id,
  3125. p = this.get_node(this._data.core.last_clicked.parent).children,
  3126. c = false,
  3127. i, j;
  3128. for(i = 0, j = p.length; i < j; i += 1) {
  3129. // separate IFs work whem o and l are the same
  3130. if(p[i] === o) {
  3131. c = !c;
  3132. }
  3133. if(p[i] === l) {
  3134. c = !c;
  3135. }
  3136. if(!this.is_disabled(p[i]) && (c || p[i] === o || p[i] === l)) {
  3137. if (!this.is_hidden(p[i])) {
  3138. this.select_node(p[i], true, false, e);
  3139. }
  3140. }
  3141. else {
  3142. this.deselect_node(p[i], true, e);
  3143. }
  3144. }
  3145. this.trigger('changed', { 'action' : 'select_node', 'node' : this.get_node(obj), 'selected' : this._data.core.selected, 'event' : e });
  3146. }
  3147. else {
  3148. if(!this.is_selected(obj)) {
  3149. this.select_node(obj, false, false, e);
  3150. }
  3151. else {
  3152. this.deselect_node(obj, false, e);
  3153. }
  3154. }
  3155. }
  3156. /**
  3157. * triggered when an node is clicked or intercated with by the user
  3158. * @event
  3159. * @name activate_node.jstree
  3160. * @param {Object} node
  3161. * @param {Object} event the ooriginal event (if any) which triggered the call (may be an empty object)
  3162. */
  3163. this.trigger('activate_node', { 'node' : this.get_node(obj), 'event' : e });
  3164. },
  3165. /**
  3166. * applies the hover state on a node, called when a node is hovered by the user. Used internally.
  3167. * @private
  3168. * @name hover_node(obj)
  3169. * @param {mixed} obj
  3170. * @trigger hover_node.jstree
  3171. */
  3172. hover_node : function (obj) {
  3173. obj = this.get_node(obj, true);
  3174. if(!obj || !obj.length || obj.children('.jstree-hovered').length) {
  3175. return false;
  3176. }
  3177. var o = this.element.find('.jstree-hovered'), t = this.element;
  3178. if(o && o.length) { this.dehover_node(o); }
  3179. obj.children('.jstree-anchor').addClass('jstree-hovered');
  3180. /**
  3181. * triggered when an node is hovered
  3182. * @event
  3183. * @name hover_node.jstree
  3184. * @param {Object} node
  3185. */
  3186. this.trigger('hover_node', { 'node' : this.get_node(obj) });
  3187. setTimeout(function () { t.attr('aria-activedescendant', obj[0].id); }, 0);
  3188. },
  3189. /**
  3190. * removes the hover state from a nodecalled when a node is no longer hovered by the user. Used internally.
  3191. * @private
  3192. * @name dehover_node(obj)
  3193. * @param {mixed} obj
  3194. * @trigger dehover_node.jstree
  3195. */
  3196. dehover_node : function (obj) {
  3197. obj = this.get_node(obj, true);
  3198. if(!obj || !obj.length || !obj.children('.jstree-hovered').length) {
  3199. return false;
  3200. }
  3201. obj.children('.jstree-anchor').removeClass('jstree-hovered');
  3202. /**
  3203. * triggered when an node is no longer hovered
  3204. * @event
  3205. * @name dehover_node.jstree
  3206. * @param {Object} node
  3207. */
  3208. this.trigger('dehover_node', { 'node' : this.get_node(obj) });
  3209. },
  3210. /**
  3211. * select a node
  3212. * @name select_node(obj [, supress_event, prevent_open])
  3213. * @param {mixed} obj an array can be used to select multiple nodes
  3214. * @param {Boolean} supress_event if set to `true` the `changed.jstree` event won't be triggered
  3215. * @param {Boolean} prevent_open if set to `true` parents of the selected node won't be opened
  3216. * @trigger select_node.jstree, changed.jstree
  3217. */
  3218. select_node : function (obj, supress_event, prevent_open, e) {
  3219. var dom, t1, t2, th;
  3220. if($.isArray(obj)) {
  3221. obj = obj.slice();
  3222. for(t1 = 0, t2 = obj.length; t1 < t2; t1++) {
  3223. this.select_node(obj[t1], supress_event, prevent_open, e);
  3224. }
  3225. return true;
  3226. }
  3227. obj = this.get_node(obj);
  3228. if(!obj || obj.id === $.jstree.root) {
  3229. return false;
  3230. }
  3231. dom = this.get_node(obj, true);
  3232. if(!obj.state.selected) {
  3233. obj.state.selected = true;
  3234. this._data.core.selected.push(obj.id);
  3235. if(!prevent_open) {
  3236. dom = this._open_to(obj);
  3237. }
  3238. if(dom && dom.length) {
  3239. dom.children('.jstree-anchor').addClass('jstree-clicked').attr('aria-selected', true);
  3240. }
  3241. /**
  3242. * triggered when an node is selected
  3243. * @event
  3244. * @name select_node.jstree
  3245. * @param {Object} node
  3246. * @param {Array} selected the current selection
  3247. * @param {Object} event the event (if any) that triggered this select_node
  3248. */
  3249. this.trigger('select_node', { 'node' : obj, 'selected' : this._data.core.selected, 'event' : e });
  3250. if(!supress_event) {
  3251. /**
  3252. * triggered when selection changes
  3253. * @event
  3254. * @name changed.jstree
  3255. * @param {Object} node
  3256. * @param {Object} action the action that caused the selection to change
  3257. * @param {Array} selected the current selection
  3258. * @param {Object} event the event (if any) that triggered this changed event
  3259. */
  3260. this.trigger('changed', { 'action' : 'select_node', 'node' : obj, 'selected' : this._data.core.selected, 'event' : e });
  3261. }
  3262. }
  3263. },
  3264. /**
  3265. * deselect a node
  3266. * @name deselect_node(obj [, supress_event])
  3267. * @param {mixed} obj an array can be used to deselect multiple nodes
  3268. * @param {Boolean} supress_event if set to `true` the `changed.jstree` event won't be triggered
  3269. * @trigger deselect_node.jstree, changed.jstree
  3270. */
  3271. deselect_node : function (obj, supress_event, e) {
  3272. var t1, t2, dom;
  3273. if($.isArray(obj)) {
  3274. obj = obj.slice();
  3275. for(t1 = 0, t2 = obj.length; t1 < t2; t1++) {
  3276. this.deselect_node(obj[t1], supress_event, e);
  3277. }
  3278. return true;
  3279. }
  3280. obj = this.get_node(obj);
  3281. if(!obj || obj.id === $.jstree.root) {
  3282. return false;
  3283. }
  3284. dom = this.get_node(obj, true);
  3285. if(obj.state.selected) {
  3286. obj.state.selected = false;
  3287. this._data.core.selected = $.vakata.array_remove_item(this._data.core.selected, obj.id);
  3288. if(dom.length) {
  3289. dom.children('.jstree-anchor').removeClass('jstree-clicked').attr('aria-selected', false);
  3290. }
  3291. /**
  3292. * triggered when an node is deselected
  3293. * @event
  3294. * @name deselect_node.jstree
  3295. * @param {Object} node
  3296. * @param {Array} selected the current selection
  3297. * @param {Object} event the event (if any) that triggered this deselect_node
  3298. */
  3299. this.trigger('deselect_node', { 'node' : obj, 'selected' : this._data.core.selected, 'event' : e });
  3300. if(!supress_event) {
  3301. this.trigger('changed', { 'action' : 'deselect_node', 'node' : obj, 'selected' : this._data.core.selected, 'event' : e });
  3302. }
  3303. }
  3304. },
  3305. /**
  3306. * select all nodes in the tree
  3307. * @name select_all([supress_event])
  3308. * @param {Boolean} supress_event if set to `true` the `changed.jstree` event won't be triggered
  3309. * @trigger select_all.jstree, changed.jstree
  3310. */
  3311. select_all : function (supress_event) {
  3312. var tmp = this._data.core.selected.concat([]), i, j;
  3313. this._data.core.selected = this._model.data[$.jstree.root].children_d.concat();
  3314. for(i = 0, j = this._data.core.selected.length; i < j; i++) {
  3315. if(this._model.data[this._data.core.selected[i]]) {
  3316. this._model.data[this._data.core.selected[i]].state.selected = true;
  3317. }
  3318. }
  3319. this.redraw(true);
  3320. /**
  3321. * triggered when all nodes are selected
  3322. * @event
  3323. * @name select_all.jstree
  3324. * @param {Array} selected the current selection
  3325. */
  3326. this.trigger('select_all', { 'selected' : this._data.core.selected });
  3327. if(!supress_event) {
  3328. this.trigger('changed', { 'action' : 'select_all', 'selected' : this._data.core.selected, 'old_selection' : tmp });
  3329. }
  3330. },
  3331. /**
  3332. * deselect all selected nodes
  3333. * @name deselect_all([supress_event])
  3334. * @param {Boolean} supress_event if set to `true` the `changed.jstree` event won't be triggered
  3335. * @trigger deselect_all.jstree, changed.jstree
  3336. */
  3337. deselect_all : function (supress_event) {
  3338. var tmp = this._data.core.selected.concat([]), i, j;
  3339. for(i = 0, j = this._data.core.selected.length; i < j; i++) {
  3340. if(this._model.data[this._data.core.selected[i]]) {
  3341. this._model.data[this._data.core.selected[i]].state.selected = false;
  3342. }
  3343. }
  3344. this._data.core.selected = [];
  3345. this.element.find('.jstree-clicked').removeClass('jstree-clicked').attr('aria-selected', false);
  3346. /**
  3347. * triggered when all nodes are deselected
  3348. * @event
  3349. * @name deselect_all.jstree
  3350. * @param {Object} node the previous selection
  3351. * @param {Array} selected the current selection
  3352. */
  3353. this.trigger('deselect_all', { 'selected' : this._data.core.selected, 'node' : tmp });
  3354. if(!supress_event) {
  3355. this.trigger('changed', { 'action' : 'deselect_all', 'selected' : this._data.core.selected, 'old_selection' : tmp });
  3356. }
  3357. },
  3358. /**
  3359. * checks if a node is selected
  3360. * @name is_selected(obj)
  3361. * @param {mixed} obj
  3362. * @return {Boolean}
  3363. */
  3364. is_selected : function (obj) {
  3365. obj = this.get_node(obj);
  3366. if(!obj || obj.id === $.jstree.root) {
  3367. return false;
  3368. }
  3369. return obj.state.selected;
  3370. },
  3371. /**
  3372. * get an array of all selected nodes
  3373. * @name get_selected([full])
  3374. * @param {mixed} full if set to `true` the returned array will consist of the full node objects, otherwise - only IDs will be returned
  3375. * @return {Array}
  3376. */
  3377. get_selected : function (full) {
  3378. return full ? $.map(this._data.core.selected, $.proxy(function (i) { return this.get_node(i); }, this)) : this._data.core.selected.slice();
  3379. },
  3380. /**
  3381. * get an array of all top level selected nodes (ignoring children of selected nodes)
  3382. * @name get_top_selected([full])
  3383. * @param {mixed} full if set to `true` the returned array will consist of the full node objects, otherwise - only IDs will be returned
  3384. * @return {Array}
  3385. */
  3386. get_top_selected : function (full) {
  3387. var tmp = this.get_selected(true),
  3388. obj = {}, i, j, k, l;
  3389. for(i = 0, j = tmp.length; i < j; i++) {
  3390. obj[tmp[i].id] = tmp[i];
  3391. }
  3392. for(i = 0, j = tmp.length; i < j; i++) {
  3393. for(k = 0, l = tmp[i].children_d.length; k < l; k++) {
  3394. if(obj[tmp[i].children_d[k]]) {
  3395. delete obj[tmp[i].children_d[k]];
  3396. }
  3397. }
  3398. }
  3399. tmp = [];
  3400. for(i in obj) {
  3401. if(obj.hasOwnProperty(i)) {
  3402. tmp.push(i);
  3403. }
  3404. }
  3405. return full ? $.map(tmp, $.proxy(function (i) { return this.get_node(i); }, this)) : tmp;
  3406. },
  3407. /**
  3408. * get an array of all bottom level selected nodes (ignoring selected parents)
  3409. * @name get_bottom_selected([full])
  3410. * @param {mixed} full if set to `true` the returned array will consist of the full node objects, otherwise - only IDs will be returned
  3411. * @return {Array}
  3412. */
  3413. get_bottom_selected : function (full) {
  3414. var tmp = this.get_selected(true),
  3415. obj = [], i, j;
  3416. for(i = 0, j = tmp.length; i < j; i++) {
  3417. if(!tmp[i].children.length) {
  3418. obj.push(tmp[i].id);
  3419. }
  3420. }
  3421. return full ? $.map(obj, $.proxy(function (i) { return this.get_node(i); }, this)) : obj;
  3422. },
  3423. /**
  3424. * gets the current state of the tree so that it can be restored later with `set_state(state)`. Used internally.
  3425. * @name get_state()
  3426. * @private
  3427. * @return {Object}
  3428. */
  3429. get_state : function () {
  3430. var state = {
  3431. 'core' : {
  3432. 'open' : [],
  3433. 'loaded' : [],
  3434. 'scroll' : {
  3435. 'left' : this.element.scrollLeft(),
  3436. 'top' : this.element.scrollTop()
  3437. },
  3438. /*!
  3439. 'themes' : {
  3440. 'name' : this.get_theme(),
  3441. 'icons' : this._data.core.themes.icons,
  3442. 'dots' : this._data.core.themes.dots
  3443. },
  3444. */
  3445. 'selected' : []
  3446. }
  3447. }, i;
  3448. for(i in this._model.data) {
  3449. if(this._model.data.hasOwnProperty(i)) {
  3450. if(i !== $.jstree.root) {
  3451. if(this._model.data[i].state.loaded && this.settings.core.loaded_state) {
  3452. state.core.loaded.push(i);
  3453. }
  3454. if(this._model.data[i].state.opened) {
  3455. state.core.open.push(i);
  3456. }
  3457. if(this._model.data[i].state.selected) {
  3458. state.core.selected.push(i);
  3459. }
  3460. }
  3461. }
  3462. }
  3463. return state;
  3464. },
  3465. /**
  3466. * sets the state of the tree. Used internally.
  3467. * @name set_state(state [, callback])
  3468. * @private
  3469. * @param {Object} state the state to restore. Keep in mind this object is passed by reference and jstree will modify it.
  3470. * @param {Function} callback an optional function to execute once the state is restored.
  3471. * @trigger set_state.jstree
  3472. */
  3473. set_state : function (state, callback) {
  3474. if(state) {
  3475. if(state.core && state.core.selected && state.core.initial_selection === undefined) {
  3476. state.core.initial_selection = this._data.core.selected.concat([]).sort().join(',');
  3477. }
  3478. if(state.core) {
  3479. var res, n, t, _this, i;
  3480. if(state.core.loaded) {
  3481. if(!this.settings.core.loaded_state || !$.isArray(state.core.loaded) || !state.core.loaded.length) {
  3482. delete state.core.loaded;
  3483. this.set_state(state, callback);
  3484. }
  3485. else {
  3486. this._load_nodes(state.core.loaded, function (nodes) {
  3487. delete state.core.loaded;
  3488. this.set_state(state, callback);
  3489. });
  3490. }
  3491. return false;
  3492. }
  3493. if(state.core.open) {
  3494. if(!$.isArray(state.core.open) || !state.core.open.length) {
  3495. delete state.core.open;
  3496. this.set_state(state, callback);
  3497. }
  3498. else {
  3499. this._load_nodes(state.core.open, function (nodes) {
  3500. this.open_node(nodes, false, 0);
  3501. delete state.core.open;
  3502. this.set_state(state, callback);
  3503. });
  3504. }
  3505. return false;
  3506. }
  3507. if(state.core.scroll) {
  3508. if(state.core.scroll && state.core.scroll.left !== undefined) {
  3509. this.element.scrollLeft(state.core.scroll.left);
  3510. }
  3511. if(state.core.scroll && state.core.scroll.top !== undefined) {
  3512. this.element.scrollTop(state.core.scroll.top);
  3513. }
  3514. delete state.core.scroll;
  3515. this.set_state(state, callback);
  3516. return false;
  3517. }
  3518. if(state.core.selected) {
  3519. _this = this;
  3520. if (state.core.initial_selection === undefined ||
  3521. state.core.initial_selection === this._data.core.selected.concat([]).sort().join(',')
  3522. ) {
  3523. this.deselect_all();
  3524. $.each(state.core.selected, function (i, v) {
  3525. _this.select_node(v, false, true);
  3526. });
  3527. }
  3528. delete state.core.initial_selection;
  3529. delete state.core.selected;
  3530. this.set_state(state, callback);
  3531. return false;
  3532. }
  3533. for(i in state) {
  3534. if(state.hasOwnProperty(i) && i !== "core" && $.inArray(i, this.settings.plugins) === -1) {
  3535. delete state[i];
  3536. }
  3537. }
  3538. if($.isEmptyObject(state.core)) {
  3539. delete state.core;
  3540. this.set_state(state, callback);
  3541. return false;
  3542. }
  3543. }
  3544. if($.isEmptyObject(state)) {
  3545. state = null;
  3546. if(callback) { callback.call(this); }
  3547. /**
  3548. * triggered when a `set_state` call completes
  3549. * @event
  3550. * @name set_state.jstree
  3551. */
  3552. this.trigger('set_state');
  3553. return false;
  3554. }
  3555. return true;
  3556. }
  3557. return false;
  3558. },
  3559. /**
  3560. * refreshes the tree - all nodes are reloaded with calls to `load_node`.
  3561. * @name refresh()
  3562. * @param {Boolean} skip_loading an option to skip showing the loading indicator
  3563. * @param {Mixed} forget_state if set to `true` state will not be reapplied, if set to a function (receiving the current state as argument) the result of that function will be used as state
  3564. * @trigger refresh.jstree
  3565. */
  3566. refresh : function (skip_loading, forget_state) {
  3567. this._data.core.state = forget_state === true ? {} : this.get_state();
  3568. if(forget_state && $.isFunction(forget_state)) { this._data.core.state = forget_state.call(this, this._data.core.state); }
  3569. this._cnt = 0;
  3570. this._model.data = {};
  3571. this._model.data[$.jstree.root] = {
  3572. id : $.jstree.root,
  3573. parent : null,
  3574. parents : [],
  3575. children : [],
  3576. children_d : [],
  3577. state : { loaded : false }
  3578. };
  3579. this._data.core.selected = [];
  3580. this._data.core.last_clicked = null;
  3581. this._data.core.focused = null;
  3582. var c = this.get_container_ul()[0].className;
  3583. if(!skip_loading) {
  3584. this.element.html("<"+"ul class='"+c+"' role='group'><"+"li class='jstree-initial-node jstree-loading jstree-leaf jstree-last' role='none' id='j"+this._id+"_loading'><i class='jstree-icon jstree-ocl'></i><"+"a class='jstree-anchor' role='treeitem' href='#'><i class='jstree-icon jstree-themeicon-hidden'></i>" + this.get_string("Loading ...") + "</a></li></ul>");
  3585. this.element.attr('aria-activedescendant','j'+this._id+'_loading');
  3586. }
  3587. this.load_node($.jstree.root, function (o, s) {
  3588. if(s) {
  3589. this.get_container_ul()[0].className = c;
  3590. if(this._firstChild(this.get_container_ul()[0])) {
  3591. this.element.attr('aria-activedescendant',this._firstChild(this.get_container_ul()[0]).id);
  3592. }
  3593. this.set_state($.extend(true, {}, this._data.core.state), function () {
  3594. /**
  3595. * triggered when a `refresh` call completes
  3596. * @event
  3597. * @name refresh.jstree
  3598. */
  3599. this.trigger('refresh');
  3600. });
  3601. }
  3602. this._data.core.state = null;
  3603. });
  3604. },
  3605. /**
  3606. * refreshes a node in the tree (reload its children) all opened nodes inside that node are reloaded with calls to `load_node`.
  3607. * @name refresh_node(obj)
  3608. * @param {mixed} obj the node
  3609. * @trigger refresh_node.jstree
  3610. */
  3611. refresh_node : function (obj) {
  3612. obj = this.get_node(obj);
  3613. if(!obj || obj.id === $.jstree.root) { return false; }
  3614. var opened = [], to_load = [], s = this._data.core.selected.concat([]);
  3615. to_load.push(obj.id);
  3616. if(obj.state.opened === true) { opened.push(obj.id); }
  3617. this.get_node(obj, true).find('.jstree-open').each(function() { to_load.push(this.id); opened.push(this.id); });
  3618. this._load_nodes(to_load, $.proxy(function (nodes) {
  3619. this.open_node(opened, false, 0);
  3620. this.select_node(s);
  3621. /**
  3622. * triggered when a node is refreshed
  3623. * @event
  3624. * @name refresh_node.jstree
  3625. * @param {Object} node - the refreshed node
  3626. * @param {Array} nodes - an array of the IDs of the nodes that were reloaded
  3627. */
  3628. this.trigger('refresh_node', { 'node' : obj, 'nodes' : nodes });
  3629. }, this), false, true);
  3630. },
  3631. /**
  3632. * set (change) the ID of a node
  3633. * @name set_id(obj, id)
  3634. * @param {mixed} obj the node
  3635. * @param {String} id the new ID
  3636. * @return {Boolean}
  3637. * @trigger set_id.jstree
  3638. */
  3639. set_id : function (obj, id) {
  3640. obj = this.get_node(obj);
  3641. if(!obj || obj.id === $.jstree.root) { return false; }
  3642. var i, j, m = this._model.data, old = obj.id;
  3643. id = id.toString();
  3644. // update parents (replace current ID with new one in children and children_d)
  3645. m[obj.parent].children[$.inArray(obj.id, m[obj.parent].children)] = id;
  3646. for(i = 0, j = obj.parents.length; i < j; i++) {
  3647. m[obj.parents[i]].children_d[$.inArray(obj.id, m[obj.parents[i]].children_d)] = id;
  3648. }
  3649. // update children (replace current ID with new one in parent and parents)
  3650. for(i = 0, j = obj.children.length; i < j; i++) {
  3651. m[obj.children[i]].parent = id;
  3652. }
  3653. for(i = 0, j = obj.children_d.length; i < j; i++) {
  3654. m[obj.children_d[i]].parents[$.inArray(obj.id, m[obj.children_d[i]].parents)] = id;
  3655. }
  3656. i = $.inArray(obj.id, this._data.core.selected);
  3657. if(i !== -1) { this._data.core.selected[i] = id; }
  3658. // update model and obj itself (obj.id, this._model.data[KEY])
  3659. i = this.get_node(obj.id, true);
  3660. if(i) {
  3661. i.attr('id', id); //.children('.jstree-anchor').attr('id', id + '_anchor').end().attr('aria-labelledby', id + '_anchor');
  3662. if(this.element.attr('aria-activedescendant') === obj.id) {
  3663. this.element.attr('aria-activedescendant', id);
  3664. }
  3665. }
  3666. delete m[obj.id];
  3667. obj.id = id;
  3668. obj.li_attr.id = id;
  3669. m[id] = obj;
  3670. /**
  3671. * triggered when a node id value is changed
  3672. * @event
  3673. * @name set_id.jstree
  3674. * @param {Object} node
  3675. * @param {String} old the old id
  3676. */
  3677. this.trigger('set_id',{ "node" : obj, "new" : obj.id, "old" : old });
  3678. return true;
  3679. },
  3680. /**
  3681. * get the text value of a node
  3682. * @name get_text(obj)
  3683. * @param {mixed} obj the node
  3684. * @return {String}
  3685. */
  3686. get_text : function (obj) {
  3687. obj = this.get_node(obj);
  3688. return (!obj || obj.id === $.jstree.root) ? false : obj.text;
  3689. },
  3690. /**
  3691. * set the text value of a node. Used internally, please use `rename_node(obj, val)`.
  3692. * @private
  3693. * @name set_text(obj, val)
  3694. * @param {mixed} obj the node, you can pass an array to set the text on multiple nodes
  3695. * @param {String} val the new text value
  3696. * @return {Boolean}
  3697. * @trigger set_text.jstree
  3698. */
  3699. set_text : function (obj, val) {
  3700. var t1, t2;
  3701. if($.isArray(obj)) {
  3702. obj = obj.slice();
  3703. for(t1 = 0, t2 = obj.length; t1 < t2; t1++) {
  3704. this.set_text(obj[t1], val);
  3705. }
  3706. return true;
  3707. }
  3708. obj = this.get_node(obj);
  3709. if(!obj || obj.id === $.jstree.root) { return false; }
  3710. obj.text = val;
  3711. if(this.get_node(obj, true).length) {
  3712. this.redraw_node(obj.id);
  3713. }
  3714. /**
  3715. * triggered when a node text value is changed
  3716. * @event
  3717. * @name set_text.jstree
  3718. * @param {Object} obj
  3719. * @param {String} text the new value
  3720. */
  3721. this.trigger('set_text',{ "obj" : obj, "text" : val });
  3722. return true;
  3723. },
  3724. /**
  3725. * gets a JSON representation of a node (or the whole tree)
  3726. * @name get_json([obj, options])
  3727. * @param {mixed} obj
  3728. * @param {Object} options
  3729. * @param {Boolean} options.no_state do not return state information
  3730. * @param {Boolean} options.no_id do not return ID
  3731. * @param {Boolean} options.no_children do not include children
  3732. * @param {Boolean} options.no_data do not include node data
  3733. * @param {Boolean} options.no_li_attr do not include LI attributes
  3734. * @param {Boolean} options.no_a_attr do not include A attributes
  3735. * @param {Boolean} options.flat return flat JSON instead of nested
  3736. * @return {Object}
  3737. */
  3738. get_json : function (obj, options, flat) {
  3739. obj = this.get_node(obj || $.jstree.root);
  3740. if(!obj) { return false; }
  3741. if(options && options.flat && !flat) { flat = []; }
  3742. var tmp = {
  3743. 'id' : obj.id,
  3744. 'text' : obj.text,
  3745. 'icon' : this.get_icon(obj),
  3746. 'li_attr' : $.extend(true, {}, obj.li_attr),
  3747. 'a_attr' : $.extend(true, {}, obj.a_attr),
  3748. 'state' : {},
  3749. 'data' : options && options.no_data ? false : $.extend(true, $.isArray(obj.data)?[]:{}, obj.data)
  3750. //( this.get_node(obj, true).length ? this.get_node(obj, true).data() : obj.data ),
  3751. }, i, j;
  3752. if(options && options.flat) {
  3753. tmp.parent = obj.parent;
  3754. }
  3755. else {
  3756. tmp.children = [];
  3757. }
  3758. if(!options || !options.no_state) {
  3759. for(i in obj.state) {
  3760. if(obj.state.hasOwnProperty(i)) {
  3761. tmp.state[i] = obj.state[i];
  3762. }
  3763. }
  3764. } else {
  3765. delete tmp.state;
  3766. }
  3767. if(options && options.no_li_attr) {
  3768. delete tmp.li_attr;
  3769. }
  3770. if(options && options.no_a_attr) {
  3771. delete tmp.a_attr;
  3772. }
  3773. if(options && options.no_id) {
  3774. delete tmp.id;
  3775. if(tmp.li_attr && tmp.li_attr.id) {
  3776. delete tmp.li_attr.id;
  3777. }
  3778. if(tmp.a_attr && tmp.a_attr.id) {
  3779. delete tmp.a_attr.id;
  3780. }
  3781. }
  3782. if(options && options.flat && obj.id !== $.jstree.root) {
  3783. flat.push(tmp);
  3784. }
  3785. if(!options || !options.no_children) {
  3786. for(i = 0, j = obj.children.length; i < j; i++) {
  3787. if(options && options.flat) {
  3788. this.get_json(obj.children[i], options, flat);
  3789. }
  3790. else {
  3791. tmp.children.push(this.get_json(obj.children[i], options));
  3792. }
  3793. }
  3794. }
  3795. return options && options.flat ? flat : (obj.id === $.jstree.root ? tmp.children : tmp);
  3796. },
  3797. /**
  3798. * create a new node (do not confuse with load_node)
  3799. * @name create_node([par, node, pos, callback, is_loaded])
  3800. * @param {mixed} par the parent node (to create a root node use either "#" (string) or `null`)
  3801. * @param {mixed} node the data for the new node (a valid JSON object, or a simple string with the name)
  3802. * @param {mixed} pos the index at which to insert the node, "first" and "last" are also supported, default is "last"
  3803. * @param {Function} callback a function to be called once the node is created
  3804. * @param {Boolean} is_loaded internal argument indicating if the parent node was succesfully loaded
  3805. * @return {String} the ID of the newly create node
  3806. * @trigger model.jstree, create_node.jstree
  3807. */
  3808. create_node : function (par, node, pos, callback, is_loaded) {
  3809. if(par === null) { par = $.jstree.root; }
  3810. par = this.get_node(par);
  3811. if(!par) { return false; }
  3812. pos = pos === undefined ? "last" : pos;
  3813. if(!pos.toString().match(/^(before|after)$/) && !is_loaded && !this.is_loaded(par)) {
  3814. return this.load_node(par, function () { this.create_node(par, node, pos, callback, true); });
  3815. }
  3816. if(!node) { node = { "text" : this.get_string('New node') }; }
  3817. if(typeof node === "string") {
  3818. node = { "text" : node };
  3819. } else {
  3820. node = $.extend(true, {}, node);
  3821. }
  3822. if(node.text === undefined) { node.text = this.get_string('New node'); }
  3823. var tmp, dpc, i, j;
  3824. if(par.id === $.jstree.root) {
  3825. if(pos === "before") { pos = "first"; }
  3826. if(pos === "after") { pos = "last"; }
  3827. }
  3828. switch(pos) {
  3829. case "before":
  3830. tmp = this.get_node(par.parent);
  3831. pos = $.inArray(par.id, tmp.children);
  3832. par = tmp;
  3833. break;
  3834. case "after" :
  3835. tmp = this.get_node(par.parent);
  3836. pos = $.inArray(par.id, tmp.children) + 1;
  3837. par = tmp;
  3838. break;
  3839. case "inside":
  3840. case "first":
  3841. pos = 0;
  3842. break;
  3843. case "last":
  3844. pos = par.children.length;
  3845. break;
  3846. default:
  3847. if(!pos) { pos = 0; }
  3848. break;
  3849. }
  3850. if(pos > par.children.length) { pos = par.children.length; }
  3851. if(!node.id) { node.id = true; }
  3852. if(!this.check("create_node", node, par, pos)) {
  3853. this.settings.core.error.call(this, this._data.core.last_error);
  3854. return false;
  3855. }
  3856. if(node.id === true) { delete node.id; }
  3857. node = this._parse_model_from_json(node, par.id, par.parents.concat());
  3858. if(!node) { return false; }
  3859. tmp = this.get_node(node);
  3860. dpc = [];
  3861. dpc.push(node);
  3862. dpc = dpc.concat(tmp.children_d);
  3863. this.trigger('model', { "nodes" : dpc, "parent" : par.id });
  3864. par.children_d = par.children_d.concat(dpc);
  3865. for(i = 0, j = par.parents.length; i < j; i++) {
  3866. this._model.data[par.parents[i]].children_d = this._model.data[par.parents[i]].children_d.concat(dpc);
  3867. }
  3868. node = tmp;
  3869. tmp = [];
  3870. for(i = 0, j = par.children.length; i < j; i++) {
  3871. tmp[i >= pos ? i+1 : i] = par.children[i];
  3872. }
  3873. tmp[pos] = node.id;
  3874. par.children = tmp;
  3875. this.redraw_node(par, true);
  3876. /**
  3877. * triggered when a node is created
  3878. * @event
  3879. * @name create_node.jstree
  3880. * @param {Object} node
  3881. * @param {String} parent the parent's ID
  3882. * @param {Number} position the position of the new node among the parent's children
  3883. */
  3884. this.trigger('create_node', { "node" : this.get_node(node), "parent" : par.id, "position" : pos });
  3885. if(callback) { callback.call(this, this.get_node(node)); }
  3886. return node.id;
  3887. },
  3888. /**
  3889. * set the text value of a node
  3890. * @name rename_node(obj, val)
  3891. * @param {mixed} obj the node, you can pass an array to rename multiple nodes to the same name
  3892. * @param {String} val the new text value
  3893. * @return {Boolean}
  3894. * @trigger rename_node.jstree
  3895. */
  3896. rename_node : function (obj, val) {
  3897. var t1, t2, old;
  3898. if($.isArray(obj)) {
  3899. obj = obj.slice();
  3900. for(t1 = 0, t2 = obj.length; t1 < t2; t1++) {
  3901. this.rename_node(obj[t1], val);
  3902. }
  3903. return true;
  3904. }
  3905. obj = this.get_node(obj);
  3906. if(!obj || obj.id === $.jstree.root) { return false; }
  3907. old = obj.text;
  3908. if(!this.check("rename_node", obj, this.get_parent(obj), val)) {
  3909. this.settings.core.error.call(this, this._data.core.last_error);
  3910. return false;
  3911. }
  3912. this.set_text(obj, val); // .apply(this, Array.prototype.slice.call(arguments))
  3913. /**
  3914. * triggered when a node is renamed
  3915. * @event
  3916. * @name rename_node.jstree
  3917. * @param {Object} node
  3918. * @param {String} text the new value
  3919. * @param {String} old the old value
  3920. */
  3921. this.trigger('rename_node', { "node" : obj, "text" : val, "old" : old });
  3922. return true;
  3923. },
  3924. /**
  3925. * remove a node
  3926. * @name delete_node(obj)
  3927. * @param {mixed} obj the node, you can pass an array to delete multiple nodes
  3928. * @return {Boolean}
  3929. * @trigger delete_node.jstree, changed.jstree
  3930. */
  3931. delete_node : function (obj) {
  3932. var t1, t2, par, pos, tmp, i, j, k, l, c, top, lft;
  3933. if($.isArray(obj)) {
  3934. obj = obj.slice();
  3935. for(t1 = 0, t2 = obj.length; t1 < t2; t1++) {
  3936. this.delete_node(obj[t1]);
  3937. }
  3938. return true;
  3939. }
  3940. obj = this.get_node(obj);
  3941. if(!obj || obj.id === $.jstree.root) { return false; }
  3942. par = this.get_node(obj.parent);
  3943. pos = $.inArray(obj.id, par.children);
  3944. c = false;
  3945. if(!this.check("delete_node", obj, par, pos)) {
  3946. this.settings.core.error.call(this, this._data.core.last_error);
  3947. return false;
  3948. }
  3949. if(pos !== -1) {
  3950. par.children = $.vakata.array_remove(par.children, pos);
  3951. }
  3952. tmp = obj.children_d.concat([]);
  3953. tmp.push(obj.id);
  3954. for(i = 0, j = obj.parents.length; i < j; i++) {
  3955. this._model.data[obj.parents[i]].children_d = $.vakata.array_filter(this._model.data[obj.parents[i]].children_d, function (v) {
  3956. return $.inArray(v, tmp) === -1;
  3957. });
  3958. }
  3959. for(k = 0, l = tmp.length; k < l; k++) {
  3960. if(this._model.data[tmp[k]].state.selected) {
  3961. c = true;
  3962. break;
  3963. }
  3964. }
  3965. if (c) {
  3966. this._data.core.selected = $.vakata.array_filter(this._data.core.selected, function (v) {
  3967. return $.inArray(v, tmp) === -1;
  3968. });
  3969. }
  3970. /**
  3971. * triggered when a node is deleted
  3972. * @event
  3973. * @name delete_node.jstree
  3974. * @param {Object} node
  3975. * @param {String} parent the parent's ID
  3976. */
  3977. this.trigger('delete_node', { "node" : obj, "parent" : par.id });
  3978. if(c) {
  3979. this.trigger('changed', { 'action' : 'delete_node', 'node' : obj, 'selected' : this._data.core.selected, 'parent' : par.id });
  3980. }
  3981. for(k = 0, l = tmp.length; k < l; k++) {
  3982. delete this._model.data[tmp[k]];
  3983. }
  3984. if($.inArray(this._data.core.focused, tmp) !== -1) {
  3985. this._data.core.focused = null;
  3986. top = this.element[0].scrollTop;
  3987. lft = this.element[0].scrollLeft;
  3988. if(par.id === $.jstree.root) {
  3989. if (this._model.data[$.jstree.root].children[0]) {
  3990. this.get_node(this._model.data[$.jstree.root].children[0], true).children('.jstree-anchor').focus();
  3991. }
  3992. }
  3993. else {
  3994. this.get_node(par, true).children('.jstree-anchor').focus();
  3995. }
  3996. this.element[0].scrollTop = top;
  3997. this.element[0].scrollLeft = lft;
  3998. }
  3999. this.redraw_node(par, true);
  4000. return true;
  4001. },
  4002. /**
  4003. * check if an operation is premitted on the tree. Used internally.
  4004. * @private
  4005. * @name check(chk, obj, par, pos)
  4006. * @param {String} chk the operation to check, can be "create_node", "rename_node", "delete_node", "copy_node" or "move_node"
  4007. * @param {mixed} obj the node
  4008. * @param {mixed} par the parent
  4009. * @param {mixed} pos the position to insert at, or if "rename_node" - the new name
  4010. * @param {mixed} more some various additional information, for example if a "move_node" operations is triggered by DND this will be the hovered node
  4011. * @return {Boolean}
  4012. */
  4013. check : function (chk, obj, par, pos, more) {
  4014. obj = obj && obj.id ? obj : this.get_node(obj);
  4015. par = par && par.id ? par : this.get_node(par);
  4016. var tmp = chk.match(/^move_node|copy_node|create_node$/i) ? par : obj,
  4017. chc = this.settings.core.check_callback;
  4018. if(chk === "move_node" || chk === "copy_node") {
  4019. if((!more || !more.is_multi) && (chk === "move_node" && $.inArray(obj.id, par.children) === pos)) {
  4020. this._data.core.last_error = { 'error' : 'check', 'plugin' : 'core', 'id' : 'core_08', 'reason' : 'Moving node to its current position', 'data' : JSON.stringify({ 'chk' : chk, 'pos' : pos, 'obj' : obj && obj.id ? obj.id : false, 'par' : par && par.id ? par.id : false }) };
  4021. return false;
  4022. }
  4023. if((!more || !more.is_multi) && (obj.id === par.id || (chk === "move_node" && $.inArray(obj.id, par.children) === pos) || $.inArray(par.id, obj.children_d) !== -1)) {
  4024. this._data.core.last_error = { 'error' : 'check', 'plugin' : 'core', 'id' : 'core_01', 'reason' : 'Moving parent inside child', 'data' : JSON.stringify({ 'chk' : chk, 'pos' : pos, 'obj' : obj && obj.id ? obj.id : false, 'par' : par && par.id ? par.id : false }) };
  4025. return false;
  4026. }
  4027. }
  4028. if(tmp && tmp.data) { tmp = tmp.data; }
  4029. if(tmp && tmp.functions && (tmp.functions[chk] === false || tmp.functions[chk] === true)) {
  4030. if(tmp.functions[chk] === false) {
  4031. this._data.core.last_error = { 'error' : 'check', 'plugin' : 'core', 'id' : 'core_02', 'reason' : 'Node data prevents function: ' + chk, 'data' : JSON.stringify({ 'chk' : chk, 'pos' : pos, 'obj' : obj && obj.id ? obj.id : false, 'par' : par && par.id ? par.id : false }) };
  4032. }
  4033. return tmp.functions[chk];
  4034. }
  4035. if(chc === false || ($.isFunction(chc) && chc.call(this, chk, obj, par, pos, more) === false) || (chc && chc[chk] === false)) {
  4036. this._data.core.last_error = { 'error' : 'check', 'plugin' : 'core', 'id' : 'core_03', 'reason' : 'User config for core.check_callback prevents function: ' + chk, 'data' : JSON.stringify({ 'chk' : chk, 'pos' : pos, 'obj' : obj && obj.id ? obj.id : false, 'par' : par && par.id ? par.id : false }) };
  4037. return false;
  4038. }
  4039. return true;
  4040. },
  4041. /**
  4042. * get the last error
  4043. * @name last_error()
  4044. * @return {Object}
  4045. */
  4046. last_error : function () {
  4047. return this._data.core.last_error;
  4048. },
  4049. /**
  4050. * move a node to a new parent
  4051. * @name move_node(obj, par [, pos, callback, is_loaded])
  4052. * @param {mixed} obj the node to move, pass an array to move multiple nodes
  4053. * @param {mixed} par the new parent
  4054. * @param {mixed} pos the position to insert at (besides integer values, "first" and "last" are supported, as well as "before" and "after"), defaults to integer `0`
  4055. * @param {function} callback a function to call once the move is completed, receives 3 arguments - the node, the new parent and the position
  4056. * @param {Boolean} is_loaded internal parameter indicating if the parent node has been loaded
  4057. * @param {Boolean} skip_redraw internal parameter indicating if the tree should be redrawn
  4058. * @param {Boolean} instance internal parameter indicating if the node comes from another instance
  4059. * @trigger move_node.jstree
  4060. */
  4061. move_node : function (obj, par, pos, callback, is_loaded, skip_redraw, origin) {
  4062. var t1, t2, old_par, old_pos, new_par, old_ins, is_multi, dpc, tmp, i, j, k, l, p;
  4063. par = this.get_node(par);
  4064. pos = pos === undefined ? 0 : pos;
  4065. if(!par) { return false; }
  4066. if(!pos.toString().match(/^(before|after)$/) && !is_loaded && !this.is_loaded(par)) {
  4067. return this.load_node(par, function () { this.move_node(obj, par, pos, callback, true, false, origin); });
  4068. }
  4069. if($.isArray(obj)) {
  4070. if(obj.length === 1) {
  4071. obj = obj[0];
  4072. }
  4073. else {
  4074. //obj = obj.slice();
  4075. for(t1 = 0, t2 = obj.length; t1 < t2; t1++) {
  4076. if((tmp = this.move_node(obj[t1], par, pos, callback, is_loaded, false, origin))) {
  4077. par = tmp;
  4078. pos = "after";
  4079. }
  4080. }
  4081. this.redraw();
  4082. return true;
  4083. }
  4084. }
  4085. obj = obj && obj.id ? obj : this.get_node(obj);
  4086. if(!obj || obj.id === $.jstree.root) { return false; }
  4087. old_par = (obj.parent || $.jstree.root).toString();
  4088. new_par = (!pos.toString().match(/^(before|after)$/) || par.id === $.jstree.root) ? par : this.get_node(par.parent);
  4089. old_ins = origin ? origin : (this._model.data[obj.id] ? this : $.jstree.reference(obj.id));
  4090. is_multi = !old_ins || !old_ins._id || (this._id !== old_ins._id);
  4091. old_pos = old_ins && old_ins._id && old_par && old_ins._model.data[old_par] && old_ins._model.data[old_par].children ? $.inArray(obj.id, old_ins._model.data[old_par].children) : -1;
  4092. if(old_ins && old_ins._id) {
  4093. obj = old_ins._model.data[obj.id];
  4094. }
  4095. if(is_multi) {
  4096. if((tmp = this.copy_node(obj, par, pos, callback, is_loaded, false, origin))) {
  4097. if(old_ins) { old_ins.delete_node(obj); }
  4098. return tmp;
  4099. }
  4100. return false;
  4101. }
  4102. //var m = this._model.data;
  4103. if(par.id === $.jstree.root) {
  4104. if(pos === "before") { pos = "first"; }
  4105. if(pos === "after") { pos = "last"; }
  4106. }
  4107. switch(pos) {
  4108. case "before":
  4109. pos = $.inArray(par.id, new_par.children);
  4110. break;
  4111. case "after" :
  4112. pos = $.inArray(par.id, new_par.children) + 1;
  4113. break;
  4114. case "inside":
  4115. case "first":
  4116. pos = 0;
  4117. break;
  4118. case "last":
  4119. pos = new_par.children.length;
  4120. break;
  4121. default:
  4122. if(!pos) { pos = 0; }
  4123. break;
  4124. }
  4125. if(pos > new_par.children.length) { pos = new_par.children.length; }
  4126. if(!this.check("move_node", obj, new_par, pos, { 'core' : true, 'origin' : origin, 'is_multi' : (old_ins && old_ins._id && old_ins._id !== this._id), 'is_foreign' : (!old_ins || !old_ins._id) })) {
  4127. this.settings.core.error.call(this, this._data.core.last_error);
  4128. return false;
  4129. }
  4130. if(obj.parent === new_par.id) {
  4131. dpc = new_par.children.concat();
  4132. tmp = $.inArray(obj.id, dpc);
  4133. if(tmp !== -1) {
  4134. dpc = $.vakata.array_remove(dpc, tmp);
  4135. if(pos > tmp) { pos--; }
  4136. }
  4137. tmp = [];
  4138. for(i = 0, j = dpc.length; i < j; i++) {
  4139. tmp[i >= pos ? i+1 : i] = dpc[i];
  4140. }
  4141. tmp[pos] = obj.id;
  4142. new_par.children = tmp;
  4143. this._node_changed(new_par.id);
  4144. this.redraw(new_par.id === $.jstree.root);
  4145. }
  4146. else {
  4147. // clean old parent and up
  4148. tmp = obj.children_d.concat();
  4149. tmp.push(obj.id);
  4150. for(i = 0, j = obj.parents.length; i < j; i++) {
  4151. dpc = [];
  4152. p = old_ins._model.data[obj.parents[i]].children_d;
  4153. for(k = 0, l = p.length; k < l; k++) {
  4154. if($.inArray(p[k], tmp) === -1) {
  4155. dpc.push(p[k]);
  4156. }
  4157. }
  4158. old_ins._model.data[obj.parents[i]].children_d = dpc;
  4159. }
  4160. old_ins._model.data[old_par].children = $.vakata.array_remove_item(old_ins._model.data[old_par].children, obj.id);
  4161. // insert into new parent and up
  4162. for(i = 0, j = new_par.parents.length; i < j; i++) {
  4163. this._model.data[new_par.parents[i]].children_d = this._model.data[new_par.parents[i]].children_d.concat(tmp);
  4164. }
  4165. dpc = [];
  4166. for(i = 0, j = new_par.children.length; i < j; i++) {
  4167. dpc[i >= pos ? i+1 : i] = new_par.children[i];
  4168. }
  4169. dpc[pos] = obj.id;
  4170. new_par.children = dpc;
  4171. new_par.children_d.push(obj.id);
  4172. new_par.children_d = new_par.children_d.concat(obj.children_d);
  4173. // update object
  4174. obj.parent = new_par.id;
  4175. tmp = new_par.parents.concat();
  4176. tmp.unshift(new_par.id);
  4177. p = obj.parents.length;
  4178. obj.parents = tmp;
  4179. // update object children
  4180. tmp = tmp.concat();
  4181. for(i = 0, j = obj.children_d.length; i < j; i++) {
  4182. this._model.data[obj.children_d[i]].parents = this._model.data[obj.children_d[i]].parents.slice(0,p*-1);
  4183. Array.prototype.push.apply(this._model.data[obj.children_d[i]].parents, tmp);
  4184. }
  4185. if(old_par === $.jstree.root || new_par.id === $.jstree.root) {
  4186. this._model.force_full_redraw = true;
  4187. }
  4188. if(!this._model.force_full_redraw) {
  4189. this._node_changed(old_par);
  4190. this._node_changed(new_par.id);
  4191. }
  4192. if(!skip_redraw) {
  4193. this.redraw();
  4194. }
  4195. }
  4196. if(callback) { callback.call(this, obj, new_par, pos); }
  4197. /**
  4198. * triggered when a node is moved
  4199. * @event
  4200. * @name move_node.jstree
  4201. * @param {Object} node
  4202. * @param {String} parent the parent's ID
  4203. * @param {Number} position the position of the node among the parent's children
  4204. * @param {String} old_parent the old parent of the node
  4205. * @param {Number} old_position the old position of the node
  4206. * @param {Boolean} is_multi do the node and new parent belong to different instances
  4207. * @param {jsTree} old_instance the instance the node came from
  4208. * @param {jsTree} new_instance the instance of the new parent
  4209. */
  4210. this.trigger('move_node', { "node" : obj, "parent" : new_par.id, "position" : pos, "old_parent" : old_par, "old_position" : old_pos, 'is_multi' : (old_ins && old_ins._id && old_ins._id !== this._id), 'is_foreign' : (!old_ins || !old_ins._id), 'old_instance' : old_ins, 'new_instance' : this });
  4211. return obj.id;
  4212. },
  4213. /**
  4214. * copy a node to a new parent
  4215. * @name copy_node(obj, par [, pos, callback, is_loaded])
  4216. * @param {mixed} obj the node to copy, pass an array to copy multiple nodes
  4217. * @param {mixed} par the new parent
  4218. * @param {mixed} pos the position to insert at (besides integer values, "first" and "last" are supported, as well as "before" and "after"), defaults to integer `0`
  4219. * @param {function} callback a function to call once the move is completed, receives 3 arguments - the node, the new parent and the position
  4220. * @param {Boolean} is_loaded internal parameter indicating if the parent node has been loaded
  4221. * @param {Boolean} skip_redraw internal parameter indicating if the tree should be redrawn
  4222. * @param {Boolean} instance internal parameter indicating if the node comes from another instance
  4223. * @trigger model.jstree copy_node.jstree
  4224. */
  4225. copy_node : function (obj, par, pos, callback, is_loaded, skip_redraw, origin) {
  4226. var t1, t2, dpc, tmp, i, j, node, old_par, new_par, old_ins, is_multi;
  4227. par = this.get_node(par);
  4228. pos = pos === undefined ? 0 : pos;
  4229. if(!par) { return false; }
  4230. if(!pos.toString().match(/^(before|after)$/) && !is_loaded && !this.is_loaded(par)) {
  4231. return this.load_node(par, function () { this.copy_node(obj, par, pos, callback, true, false, origin); });
  4232. }
  4233. if($.isArray(obj)) {
  4234. if(obj.length === 1) {
  4235. obj = obj[0];
  4236. }
  4237. else {
  4238. //obj = obj.slice();
  4239. for(t1 = 0, t2 = obj.length; t1 < t2; t1++) {
  4240. if((tmp = this.copy_node(obj[t1], par, pos, callback, is_loaded, true, origin))) {
  4241. par = tmp;
  4242. pos = "after";
  4243. }
  4244. }
  4245. this.redraw();
  4246. return true;
  4247. }
  4248. }
  4249. obj = obj && obj.id ? obj : this.get_node(obj);
  4250. if(!obj || obj.id === $.jstree.root) { return false; }
  4251. old_par = (obj.parent || $.jstree.root).toString();
  4252. new_par = (!pos.toString().match(/^(before|after)$/) || par.id === $.jstree.root) ? par : this.get_node(par.parent);
  4253. old_ins = origin ? origin : (this._model.data[obj.id] ? this : $.jstree.reference(obj.id));
  4254. is_multi = !old_ins || !old_ins._id || (this._id !== old_ins._id);
  4255. if(old_ins && old_ins._id) {
  4256. obj = old_ins._model.data[obj.id];
  4257. }
  4258. if(par.id === $.jstree.root) {
  4259. if(pos === "before") { pos = "first"; }
  4260. if(pos === "after") { pos = "last"; }
  4261. }
  4262. switch(pos) {
  4263. case "before":
  4264. pos = $.inArray(par.id, new_par.children);
  4265. break;
  4266. case "after" :
  4267. pos = $.inArray(par.id, new_par.children) + 1;
  4268. break;
  4269. case "inside":
  4270. case "first":
  4271. pos = 0;
  4272. break;
  4273. case "last":
  4274. pos = new_par.children.length;
  4275. break;
  4276. default:
  4277. if(!pos) { pos = 0; }
  4278. break;
  4279. }
  4280. if(pos > new_par.children.length) { pos = new_par.children.length; }
  4281. if(!this.check("copy_node", obj, new_par, pos, { 'core' : true, 'origin' : origin, 'is_multi' : (old_ins && old_ins._id && old_ins._id !== this._id), 'is_foreign' : (!old_ins || !old_ins._id) })) {
  4282. this.settings.core.error.call(this, this._data.core.last_error);
  4283. return false;
  4284. }
  4285. node = old_ins ? old_ins.get_json(obj, { no_id : true, no_data : true, no_state : true }) : obj;
  4286. if(!node) { return false; }
  4287. if(node.id === true) { delete node.id; }
  4288. node = this._parse_model_from_json(node, new_par.id, new_par.parents.concat());
  4289. if(!node) { return false; }
  4290. tmp = this.get_node(node);
  4291. if(obj && obj.state && obj.state.loaded === false) { tmp.state.loaded = false; }
  4292. dpc = [];
  4293. dpc.push(node);
  4294. dpc = dpc.concat(tmp.children_d);
  4295. this.trigger('model', { "nodes" : dpc, "parent" : new_par.id });
  4296. // insert into new parent and up
  4297. for(i = 0, j = new_par.parents.length; i < j; i++) {
  4298. this._model.data[new_par.parents[i]].children_d = this._model.data[new_par.parents[i]].children_d.concat(dpc);
  4299. }
  4300. dpc = [];
  4301. for(i = 0, j = new_par.children.length; i < j; i++) {
  4302. dpc[i >= pos ? i+1 : i] = new_par.children[i];
  4303. }
  4304. dpc[pos] = tmp.id;
  4305. new_par.children = dpc;
  4306. new_par.children_d.push(tmp.id);
  4307. new_par.children_d = new_par.children_d.concat(tmp.children_d);
  4308. if(new_par.id === $.jstree.root) {
  4309. this._model.force_full_redraw = true;
  4310. }
  4311. if(!this._model.force_full_redraw) {
  4312. this._node_changed(new_par.id);
  4313. }
  4314. if(!skip_redraw) {
  4315. this.redraw(new_par.id === $.jstree.root);
  4316. }
  4317. if(callback) { callback.call(this, tmp, new_par, pos); }
  4318. /**
  4319. * triggered when a node is copied
  4320. * @event
  4321. * @name copy_node.jstree
  4322. * @param {Object} node the copied node
  4323. * @param {Object} original the original node
  4324. * @param {String} parent the parent's ID
  4325. * @param {Number} position the position of the node among the parent's children
  4326. * @param {String} old_parent the old parent of the node
  4327. * @param {Number} old_position the position of the original node
  4328. * @param {Boolean} is_multi do the node and new parent belong to different instances
  4329. * @param {jsTree} old_instance the instance the node came from
  4330. * @param {jsTree} new_instance the instance of the new parent
  4331. */
  4332. this.trigger('copy_node', { "node" : tmp, "original" : obj, "parent" : new_par.id, "position" : pos, "old_parent" : old_par, "old_position" : old_ins && old_ins._id && old_par && old_ins._model.data[old_par] && old_ins._model.data[old_par].children ? $.inArray(obj.id, old_ins._model.data[old_par].children) : -1,'is_multi' : (old_ins && old_ins._id && old_ins._id !== this._id), 'is_foreign' : (!old_ins || !old_ins._id), 'old_instance' : old_ins, 'new_instance' : this });
  4333. return tmp.id;
  4334. },
  4335. /**
  4336. * cut a node (a later call to `paste(obj)` would move the node)
  4337. * @name cut(obj)
  4338. * @param {mixed} obj multiple objects can be passed using an array
  4339. * @trigger cut.jstree
  4340. */
  4341. cut : function (obj) {
  4342. if(!obj) { obj = this._data.core.selected.concat(); }
  4343. if(!$.isArray(obj)) { obj = [obj]; }
  4344. if(!obj.length) { return false; }
  4345. var tmp = [], o, t1, t2;
  4346. for(t1 = 0, t2 = obj.length; t1 < t2; t1++) {
  4347. o = this.get_node(obj[t1]);
  4348. if(o && o.id && o.id !== $.jstree.root) { tmp.push(o); }
  4349. }
  4350. if(!tmp.length) { return false; }
  4351. ccp_node = tmp;
  4352. ccp_inst = this;
  4353. ccp_mode = 'move_node';
  4354. /**
  4355. * triggered when nodes are added to the buffer for moving
  4356. * @event
  4357. * @name cut.jstree
  4358. * @param {Array} node
  4359. */
  4360. this.trigger('cut', { "node" : obj });
  4361. },
  4362. /**
  4363. * copy a node (a later call to `paste(obj)` would copy the node)
  4364. * @name copy(obj)
  4365. * @param {mixed} obj multiple objects can be passed using an array
  4366. * @trigger copy.jstree
  4367. */
  4368. copy : function (obj) {
  4369. if(!obj) { obj = this._data.core.selected.concat(); }
  4370. if(!$.isArray(obj)) { obj = [obj]; }
  4371. if(!obj.length) { return false; }
  4372. var tmp = [], o, t1, t2;
  4373. for(t1 = 0, t2 = obj.length; t1 < t2; t1++) {
  4374. o = this.get_node(obj[t1]);
  4375. if(o && o.id && o.id !== $.jstree.root) { tmp.push(o); }
  4376. }
  4377. if(!tmp.length) { return false; }
  4378. ccp_node = tmp;
  4379. ccp_inst = this;
  4380. ccp_mode = 'copy_node';
  4381. /**
  4382. * triggered when nodes are added to the buffer for copying
  4383. * @event
  4384. * @name copy.jstree
  4385. * @param {Array} node
  4386. */
  4387. this.trigger('copy', { "node" : obj });
  4388. },
  4389. /**
  4390. * get the current buffer (any nodes that are waiting for a paste operation)
  4391. * @name get_buffer()
  4392. * @return {Object} an object consisting of `mode` ("copy_node" or "move_node"), `node` (an array of objects) and `inst` (the instance)
  4393. */
  4394. get_buffer : function () {
  4395. return { 'mode' : ccp_mode, 'node' : ccp_node, 'inst' : ccp_inst };
  4396. },
  4397. /**
  4398. * check if there is something in the buffer to paste
  4399. * @name can_paste()
  4400. * @return {Boolean}
  4401. */
  4402. can_paste : function () {
  4403. return ccp_mode !== false && ccp_node !== false; // && ccp_inst._model.data[ccp_node];
  4404. },
  4405. /**
  4406. * copy or move the previously cut or copied nodes to a new parent
  4407. * @name paste(obj [, pos])
  4408. * @param {mixed} obj the new parent
  4409. * @param {mixed} pos the position to insert at (besides integer, "first" and "last" are supported), defaults to integer `0`
  4410. * @trigger paste.jstree
  4411. */
  4412. paste : function (obj, pos) {
  4413. obj = this.get_node(obj);
  4414. if(!obj || !ccp_mode || !ccp_mode.match(/^(copy_node|move_node)$/) || !ccp_node) { return false; }
  4415. if(this[ccp_mode](ccp_node, obj, pos, false, false, false, ccp_inst)) {
  4416. /**
  4417. * triggered when paste is invoked
  4418. * @event
  4419. * @name paste.jstree
  4420. * @param {String} parent the ID of the receiving node
  4421. * @param {Array} node the nodes in the buffer
  4422. * @param {String} mode the performed operation - "copy_node" or "move_node"
  4423. */
  4424. this.trigger('paste', { "parent" : obj.id, "node" : ccp_node, "mode" : ccp_mode });
  4425. }
  4426. ccp_node = false;
  4427. ccp_mode = false;
  4428. ccp_inst = false;
  4429. },
  4430. /**
  4431. * clear the buffer of previously copied or cut nodes
  4432. * @name clear_buffer()
  4433. * @trigger clear_buffer.jstree
  4434. */
  4435. clear_buffer : function () {
  4436. ccp_node = false;
  4437. ccp_mode = false;
  4438. ccp_inst = false;
  4439. /**
  4440. * triggered when the copy / cut buffer is cleared
  4441. * @event
  4442. * @name clear_buffer.jstree
  4443. */
  4444. this.trigger('clear_buffer');
  4445. },
  4446. /**
  4447. * put a node in edit mode (input field to rename the node)
  4448. * @name edit(obj [, default_text, callback])
  4449. * @param {mixed} obj
  4450. * @param {String} default_text the text to populate the input with (if omitted or set to a non-string value the node's text value is used)
  4451. * @param {Function} callback a function to be called once the text box is blurred, it is called in the instance's scope and receives the node, a status parameter (true if the rename is successful, false otherwise), a boolean indicating if the user cancelled the edit and the original unescaped value provided by the user. You can also access the node's title using .text
  4452. */
  4453. edit : function (obj, default_text, callback) {
  4454. var rtl, w, a, s, t, h1, h2, fn, tmp, cancel = false;
  4455. obj = this.get_node(obj);
  4456. if(!obj) { return false; }
  4457. if(!this.check("edit", obj, this.get_parent(obj))) {
  4458. this.settings.core.error.call(this, this._data.core.last_error);
  4459. return false;
  4460. }
  4461. tmp = obj;
  4462. default_text = typeof default_text === 'string' ? default_text : obj.text;
  4463. this.set_text(obj, "");
  4464. obj = this._open_to(obj);
  4465. tmp.text = default_text;
  4466. rtl = this._data.core.rtl;
  4467. w = this.element.width();
  4468. this._data.core.focused = tmp.id;
  4469. a = obj.children('.jstree-anchor').focus();
  4470. s = $('<span></span>');
  4471. /*!
  4472. oi = obj.children("i:visible"),
  4473. ai = a.children("i:visible"),
  4474. w1 = oi.width() * oi.length,
  4475. w2 = ai.width() * ai.length,
  4476. */
  4477. t = default_text;
  4478. h1 = $("<"+"div></div>", { css : { "position" : "absolute", "top" : "-200px", "left" : (rtl ? "0px" : "-1000px"), "visibility" : "hidden" } }).appendTo(document.body);
  4479. h2 = $("<"+"input />", {
  4480. "value" : t,
  4481. "class" : "jstree-rename-input",
  4482. // "size" : t.length,
  4483. "css" : {
  4484. "padding" : "0",
  4485. "border" : "1px solid silver",
  4486. "box-sizing" : "border-box",
  4487. "display" : "inline-block",
  4488. "height" : (this._data.core.li_height) + "px",
  4489. "lineHeight" : (this._data.core.li_height) + "px",
  4490. "width" : "150px" // will be set a bit further down
  4491. },
  4492. "blur" : $.proxy(function (e) {
  4493. e.stopImmediatePropagation();
  4494. e.preventDefault();
  4495. var i = s.children(".jstree-rename-input"),
  4496. v = i.val(),
  4497. f = this.settings.core.force_text,
  4498. nv;
  4499. if(v === "") { v = t; }
  4500. h1.remove();
  4501. s.replaceWith(a);
  4502. s.remove();
  4503. t = f ? t : $('<div></div>').append($.parseHTML(t)).html();
  4504. obj = this.get_node(obj);
  4505. this.set_text(obj, t);
  4506. nv = !!this.rename_node(obj, f ? $('<div></div>').text(v).text() : $('<div></div>').append($.parseHTML(v)).html());
  4507. if(!nv) {
  4508. this.set_text(obj, t); // move this up? and fix #483
  4509. }
  4510. this._data.core.focused = tmp.id;
  4511. setTimeout($.proxy(function () {
  4512. var node = this.get_node(tmp.id, true);
  4513. if(node.length) {
  4514. this._data.core.focused = tmp.id;
  4515. node.children('.jstree-anchor').focus();
  4516. }
  4517. }, this), 0);
  4518. if(callback) {
  4519. callback.call(this, tmp, nv, cancel, v);
  4520. }
  4521. h2 = null;
  4522. }, this),
  4523. "keydown" : function (e) {
  4524. var key = e.which;
  4525. if(key === 27) {
  4526. cancel = true;
  4527. this.value = t;
  4528. }
  4529. if(key === 27 || key === 13 || key === 37 || key === 38 || key === 39 || key === 40 || key === 32) {
  4530. e.stopImmediatePropagation();
  4531. }
  4532. if(key === 27 || key === 13) {
  4533. e.preventDefault();
  4534. this.blur();
  4535. }
  4536. },
  4537. "click" : function (e) { e.stopImmediatePropagation(); },
  4538. "mousedown" : function (e) { e.stopImmediatePropagation(); },
  4539. "keyup" : function (e) {
  4540. h2.width(Math.min(h1.text("pW" + this.value).width(),w));
  4541. },
  4542. "keypress" : function(e) {
  4543. if(e.which === 13) { return false; }
  4544. }
  4545. });
  4546. fn = {
  4547. fontFamily : a.css('fontFamily') || '',
  4548. fontSize : a.css('fontSize') || '',
  4549. fontWeight : a.css('fontWeight') || '',
  4550. fontStyle : a.css('fontStyle') || '',
  4551. fontStretch : a.css('fontStretch') || '',
  4552. fontVariant : a.css('fontVariant') || '',
  4553. letterSpacing : a.css('letterSpacing') || '',
  4554. wordSpacing : a.css('wordSpacing') || ''
  4555. };
  4556. s.attr('class', a.attr('class')).append(a.contents().clone()).append(h2);
  4557. a.replaceWith(s);
  4558. h1.css(fn);
  4559. h2.css(fn).width(Math.min(h1.text("pW" + h2[0].value).width(),w))[0].select();
  4560. $(document).one('mousedown.jstree touchstart.jstree dnd_start.vakata', function (e) {
  4561. if (h2 && e.target !== h2) {
  4562. $(h2).blur();
  4563. }
  4564. });
  4565. },
  4566. /**
  4567. * changes the theme
  4568. * @name set_theme(theme_name [, theme_url])
  4569. * @param {String} theme_name the name of the new theme to apply
  4570. * @param {mixed} theme_url the location of the CSS file for this theme. Omit or set to `false` if you manually included the file. Set to `true` to autoload from the `core.themes.dir` directory.
  4571. * @trigger set_theme.jstree
  4572. */
  4573. set_theme : function (theme_name, theme_url) {
  4574. if(!theme_name) { return false; }
  4575. if(theme_url === true) {
  4576. var dir = this.settings.core.themes.dir;
  4577. if(!dir) { dir = $.jstree.path + '/themes'; }
  4578. theme_url = dir + '/' + theme_name + '/style.css';
  4579. }
  4580. if(theme_url && $.inArray(theme_url, themes_loaded) === -1) {
  4581. $('head').append('<'+'link rel="stylesheet" href="' + theme_url + '" type="text/css" />');
  4582. themes_loaded.push(theme_url);
  4583. }
  4584. if(this._data.core.themes.name) {
  4585. this.element.removeClass('jstree-' + this._data.core.themes.name);
  4586. }
  4587. this._data.core.themes.name = theme_name;
  4588. this.element.addClass('jstree-' + theme_name);
  4589. this.element[this.settings.core.themes.responsive ? 'addClass' : 'removeClass' ]('jstree-' + theme_name + '-responsive');
  4590. /**
  4591. * triggered when a theme is set
  4592. * @event
  4593. * @name set_theme.jstree
  4594. * @param {String} theme the new theme
  4595. */
  4596. this.trigger('set_theme', { 'theme' : theme_name });
  4597. },
  4598. /**
  4599. * gets the name of the currently applied theme name
  4600. * @name get_theme()
  4601. * @return {String}
  4602. */
  4603. get_theme : function () { return this._data.core.themes.name; },
  4604. /**
  4605. * changes the theme variant (if the theme has variants)
  4606. * @name set_theme_variant(variant_name)
  4607. * @param {String|Boolean} variant_name the variant to apply (if `false` is used the current variant is removed)
  4608. */
  4609. set_theme_variant : function (variant_name) {
  4610. if(this._data.core.themes.variant) {
  4611. this.element.removeClass('jstree-' + this._data.core.themes.name + '-' + this._data.core.themes.variant);
  4612. }
  4613. this._data.core.themes.variant = variant_name;
  4614. if(variant_name) {
  4615. this.element.addClass('jstree-' + this._data.core.themes.name + '-' + this._data.core.themes.variant);
  4616. }
  4617. },
  4618. /**
  4619. * gets the name of the currently applied theme variant
  4620. * @name get_theme()
  4621. * @return {String}
  4622. */
  4623. get_theme_variant : function () { return this._data.core.themes.variant; },
  4624. /**
  4625. * shows a striped background on the container (if the theme supports it)
  4626. * @name show_stripes()
  4627. */
  4628. show_stripes : function () {
  4629. this._data.core.themes.stripes = true;
  4630. this.get_container_ul().addClass("jstree-striped");
  4631. /**
  4632. * triggered when stripes are shown
  4633. * @event
  4634. * @name show_stripes.jstree
  4635. */
  4636. this.trigger('show_stripes');
  4637. },
  4638. /**
  4639. * hides the striped background on the container
  4640. * @name hide_stripes()
  4641. */
  4642. hide_stripes : function () {
  4643. this._data.core.themes.stripes = false;
  4644. this.get_container_ul().removeClass("jstree-striped");
  4645. /**
  4646. * triggered when stripes are hidden
  4647. * @event
  4648. * @name hide_stripes.jstree
  4649. */
  4650. this.trigger('hide_stripes');
  4651. },
  4652. /**
  4653. * toggles the striped background on the container
  4654. * @name toggle_stripes()
  4655. */
  4656. toggle_stripes : function () { if(this._data.core.themes.stripes) { this.hide_stripes(); } else { this.show_stripes(); } },
  4657. /**
  4658. * shows the connecting dots (if the theme supports it)
  4659. * @name show_dots()
  4660. */
  4661. show_dots : function () {
  4662. this._data.core.themes.dots = true;
  4663. this.get_container_ul().removeClass("jstree-no-dots");
  4664. /**
  4665. * triggered when dots are shown
  4666. * @event
  4667. * @name show_dots.jstree
  4668. */
  4669. this.trigger('show_dots');
  4670. },
  4671. /**
  4672. * hides the connecting dots
  4673. * @name hide_dots()
  4674. */
  4675. hide_dots : function () {
  4676. this._data.core.themes.dots = false;
  4677. this.get_container_ul().addClass("jstree-no-dots");
  4678. /**
  4679. * triggered when dots are hidden
  4680. * @event
  4681. * @name hide_dots.jstree
  4682. */
  4683. this.trigger('hide_dots');
  4684. },
  4685. /**
  4686. * toggles the connecting dots
  4687. * @name toggle_dots()
  4688. */
  4689. toggle_dots : function () { if(this._data.core.themes.dots) { this.hide_dots(); } else { this.show_dots(); } },
  4690. /**
  4691. * show the node icons
  4692. * @name show_icons()
  4693. */
  4694. show_icons : function () {
  4695. this._data.core.themes.icons = true;
  4696. this.get_container_ul().removeClass("jstree-no-icons");
  4697. /**
  4698. * triggered when icons are shown
  4699. * @event
  4700. * @name show_icons.jstree
  4701. */
  4702. this.trigger('show_icons');
  4703. },
  4704. /**
  4705. * hide the node icons
  4706. * @name hide_icons()
  4707. */
  4708. hide_icons : function () {
  4709. this._data.core.themes.icons = false;
  4710. this.get_container_ul().addClass("jstree-no-icons");
  4711. /**
  4712. * triggered when icons are hidden
  4713. * @event
  4714. * @name hide_icons.jstree
  4715. */
  4716. this.trigger('hide_icons');
  4717. },
  4718. /**
  4719. * toggle the node icons
  4720. * @name toggle_icons()
  4721. */
  4722. toggle_icons : function () { if(this._data.core.themes.icons) { this.hide_icons(); } else { this.show_icons(); } },
  4723. /**
  4724. * show the node ellipsis
  4725. * @name show_icons()
  4726. */
  4727. show_ellipsis : function () {
  4728. this._data.core.themes.ellipsis = true;
  4729. this.get_container_ul().addClass("jstree-ellipsis");
  4730. /**
  4731. * triggered when ellisis is shown
  4732. * @event
  4733. * @name show_ellipsis.jstree
  4734. */
  4735. this.trigger('show_ellipsis');
  4736. },
  4737. /**
  4738. * hide the node ellipsis
  4739. * @name hide_ellipsis()
  4740. */
  4741. hide_ellipsis : function () {
  4742. this._data.core.themes.ellipsis = false;
  4743. this.get_container_ul().removeClass("jstree-ellipsis");
  4744. /**
  4745. * triggered when ellisis is hidden
  4746. * @event
  4747. * @name hide_ellipsis.jstree
  4748. */
  4749. this.trigger('hide_ellipsis');
  4750. },
  4751. /**
  4752. * toggle the node ellipsis
  4753. * @name toggle_icons()
  4754. */
  4755. toggle_ellipsis : function () { if(this._data.core.themes.ellipsis) { this.hide_ellipsis(); } else { this.show_ellipsis(); } },
  4756. /**
  4757. * set the node icon for a node
  4758. * @name set_icon(obj, icon)
  4759. * @param {mixed} obj
  4760. * @param {String} icon the new icon - can be a path to an icon or a className, if using an image that is in the current directory use a `./` prefix, otherwise it will be detected as a class
  4761. */
  4762. set_icon : function (obj, icon) {
  4763. var t1, t2, dom, old;
  4764. if($.isArray(obj)) {
  4765. obj = obj.slice();
  4766. for(t1 = 0, t2 = obj.length; t1 < t2; t1++) {
  4767. this.set_icon(obj[t1], icon);
  4768. }
  4769. return true;
  4770. }
  4771. obj = this.get_node(obj);
  4772. if(!obj || obj.id === $.jstree.root) { return false; }
  4773. old = obj.icon;
  4774. obj.icon = icon === true || icon === null || icon === undefined || icon === '' ? true : icon;
  4775. dom = this.get_node(obj, true).children(".jstree-anchor").children(".jstree-themeicon");
  4776. if(icon === false) {
  4777. dom.removeClass('jstree-themeicon-custom ' + old).css("background","").removeAttr("rel");
  4778. this.hide_icon(obj);
  4779. }
  4780. else if(icon === true || icon === null || icon === undefined || icon === '') {
  4781. dom.removeClass('jstree-themeicon-custom ' + old).css("background","").removeAttr("rel");
  4782. if(old === false) { this.show_icon(obj); }
  4783. }
  4784. else if(icon.indexOf("/") === -1 && icon.indexOf(".") === -1) {
  4785. dom.removeClass(old).css("background","");
  4786. dom.addClass(icon + ' jstree-themeicon-custom').attr("rel",icon);
  4787. if(old === false) { this.show_icon(obj); }
  4788. }
  4789. else {
  4790. dom.removeClass(old).css("background","");
  4791. dom.addClass('jstree-themeicon-custom').css("background", "url('" + icon + "') center center no-repeat").attr("rel",icon);
  4792. if(old === false) { this.show_icon(obj); }
  4793. }
  4794. return true;
  4795. },
  4796. /**
  4797. * get the node icon for a node
  4798. * @name get_icon(obj)
  4799. * @param {mixed} obj
  4800. * @return {String}
  4801. */
  4802. get_icon : function (obj) {
  4803. obj = this.get_node(obj);
  4804. return (!obj || obj.id === $.jstree.root) ? false : obj.icon;
  4805. },
  4806. /**
  4807. * hide the icon on an individual node
  4808. * @name hide_icon(obj)
  4809. * @param {mixed} obj
  4810. */
  4811. hide_icon : function (obj) {
  4812. var t1, t2;
  4813. if($.isArray(obj)) {
  4814. obj = obj.slice();
  4815. for(t1 = 0, t2 = obj.length; t1 < t2; t1++) {
  4816. this.hide_icon(obj[t1]);
  4817. }
  4818. return true;
  4819. }
  4820. obj = this.get_node(obj);
  4821. if(!obj || obj === $.jstree.root) { return false; }
  4822. obj.icon = false;
  4823. this.get_node(obj, true).children(".jstree-anchor").children(".jstree-themeicon").addClass('jstree-themeicon-hidden');
  4824. return true;
  4825. },
  4826. /**
  4827. * show the icon on an individual node
  4828. * @name show_icon(obj)
  4829. * @param {mixed} obj
  4830. */
  4831. show_icon : function (obj) {
  4832. var t1, t2, dom;
  4833. if($.isArray(obj)) {
  4834. obj = obj.slice();
  4835. for(t1 = 0, t2 = obj.length; t1 < t2; t1++) {
  4836. this.show_icon(obj[t1]);
  4837. }
  4838. return true;
  4839. }
  4840. obj = this.get_node(obj);
  4841. if(!obj || obj === $.jstree.root) { return false; }
  4842. dom = this.get_node(obj, true);
  4843. obj.icon = dom.length ? dom.children(".jstree-anchor").children(".jstree-themeicon").attr('rel') : true;
  4844. if(!obj.icon) { obj.icon = true; }
  4845. dom.children(".jstree-anchor").children(".jstree-themeicon").removeClass('jstree-themeicon-hidden');
  4846. return true;
  4847. }
  4848. };
  4849. // helpers
  4850. $.vakata = {};
  4851. // collect attributes
  4852. $.vakata.attributes = function(node, with_values) {
  4853. node = $(node)[0];
  4854. var attr = with_values ? {} : [];
  4855. if(node && node.attributes) {
  4856. $.each(node.attributes, function (i, v) {
  4857. if($.inArray(v.name.toLowerCase(),['style','contenteditable','hasfocus','tabindex']) !== -1) { return; }
  4858. if(v.value !== null && $.vakata.trim(v.value) !== '') {
  4859. if(with_values) { attr[v.name] = v.value; }
  4860. else { attr.push(v.name); }
  4861. }
  4862. });
  4863. }
  4864. return attr;
  4865. };
  4866. $.vakata.array_unique = function(array) {
  4867. var a = [], i, j, l, o = {};
  4868. for(i = 0, l = array.length; i < l; i++) {
  4869. if(o[array[i]] === undefined) {
  4870. a.push(array[i]);
  4871. o[array[i]] = true;
  4872. }
  4873. }
  4874. return a;
  4875. };
  4876. // remove item from array
  4877. $.vakata.array_remove = function(array, from) {
  4878. array.splice(from, 1);
  4879. return array;
  4880. //var rest = array.slice((to || from) + 1 || array.length);
  4881. //array.length = from < 0 ? array.length + from : from;
  4882. //array.push.apply(array, rest);
  4883. //return array;
  4884. };
  4885. // remove item from array
  4886. $.vakata.array_remove_item = function(array, item) {
  4887. var tmp = $.inArray(item, array);
  4888. return tmp !== -1 ? $.vakata.array_remove(array, tmp) : array;
  4889. };
  4890. $.vakata.array_filter = function(c,a,b,d,e) {
  4891. if (c.filter) {
  4892. return c.filter(a, b);
  4893. }
  4894. d=[];
  4895. for (e in c) {
  4896. if (~~e+''===e+'' && e>=0 && a.call(b,c[e],+e,c)) {
  4897. d.push(c[e]);
  4898. }
  4899. }
  4900. return d;
  4901. };
  4902. $.vakata.trim = function (text) {
  4903. return String.prototype.trim ?
  4904. String.prototype.trim.call(text.toString()) :
  4905. text.toString().replace(/^[\s\uFEFF\xA0]+|[\s\uFEFF\xA0]+$/g, '');
  4906. };
  4907. }));