jstree.js 164 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021
  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 supply 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').trigger('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').trigger('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').trigger('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').trigger('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').trigger('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').trigger('focus'); }
  512. },
  513. 'end': function (e) {
  514. // end
  515. e.preventDefault();
  516. this.element.find('.jstree-anchor').filter(':visible').last().trigger('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] ? keys[e.which].toLowerCase() : 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", 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. }.bind(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", function (e) {
  752. this.toggle_node(e.target);
  753. }.bind(this))
  754. .on("dblclick.jstree", ".jstree-anchor", 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. }.bind(this))
  760. .on("click.jstree", ".jstree-anchor", function (e) {
  761. e.preventDefault();
  762. if(e.currentTarget !== document.activeElement) { $(e.currentTarget).trigger('focus'); }
  763. this.activate_node(e.currentTarget, e);
  764. }.bind(this))
  765. .on('keydown.jstree', '.jstree-anchor', 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. }.bind(this))
  779. .on("load_node.jstree", 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(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. }.bind(this), 0);
  818. }
  819. }
  820. }.bind(this))
  821. // quick searching when the tree is focused
  822. .on('keypress.jstree', 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(function (i, v) {
  836. if($(v).text().toLowerCase().indexOf(word) === 0) {
  837. $(v).trigger('focus');
  838. end = true;
  839. return false;
  840. }
  841. }.bind(this));
  842. if(end) { return; }
  843. // match for whole word from the beginning of the tree
  844. col.slice(0, ind).each(function (i, v) {
  845. if($(v).text().toLowerCase().indexOf(word) === 0) {
  846. $(v).trigger('focus');
  847. end = true;
  848. return false;
  849. }
  850. }.bind(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(function (i, v) {
  857. if($(v).text().toLowerCase().charAt(0) === chr) {
  858. $(v).trigger('focus');
  859. end = true;
  860. return false;
  861. }
  862. }.bind(this));
  863. if(end) { return; }
  864. // search from the beginning
  865. col.slice(0, ind + 1).each(function (i, v) {
  866. if($(v).text().toLowerCase().charAt(0) === chr) {
  867. $(v).trigger('focus');
  868. end = true;
  869. return false;
  870. }
  871. }.bind(this));
  872. if(end) { return; }
  873. }
  874. }.bind(this))
  875. // THEME RELATED
  876. .on("init.jstree", 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. }.bind(this))
  885. .on("loading.jstree", 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. }.bind(this))
  891. .on('blur.jstree', '.jstree-anchor', function (e) {
  892. this._data.core.focused = null;
  893. $(e.currentTarget).filter('.jstree-hovered').trigger('mouseleave');
  894. this.element.attr('tabindex', '0');
  895. $(e.currentTarget).attr('tabindex', '-1');
  896. }.bind(this))
  897. .on('focus.jstree', '.jstree-anchor', function (e) {
  898. var tmp = this.get_node(e.currentTarget);
  899. if(tmp && (tmp.id || tmp.id === 0)) {
  900. this._data.core.focused = tmp.id;
  901. }
  902. this.element.find('.jstree-hovered').not(e.currentTarget).trigger('mouseleave');
  903. $(e.currentTarget).trigger('mouseenter');
  904. this.element.attr('tabindex', '-1');
  905. $(e.currentTarget).attr('tabindex', '0');
  906. }.bind(this))
  907. .on('focus.jstree', function () {
  908. if(+(new Date()) - was_click > 500 && !this._data.core.focused && this.settings.core.restore_focus) {
  909. was_click = 0;
  910. var act = this.get_node(this.element.attr('aria-activedescendant'), true);
  911. if(act) {
  912. act.find('> .jstree-anchor').trigger('focus');
  913. }
  914. }
  915. }.bind(this))
  916. .on('mouseenter.jstree', '.jstree-anchor', function (e) {
  917. this.hover_node(e.currentTarget);
  918. }.bind(this))
  919. .on('mouseleave.jstree', '.jstree-anchor', function (e) {
  920. this.dehover_node(e.currentTarget);
  921. }.bind(this));
  922. },
  923. /**
  924. * part of the destroying of an instance. Used internally.
  925. * @private
  926. * @name unbind()
  927. */
  928. unbind : function () {
  929. this.element.off('.jstree');
  930. $(document).off('.jstree-' + this._id);
  931. },
  932. /**
  933. * trigger an event. Used internally.
  934. * @private
  935. * @name trigger(ev [, data])
  936. * @param {String} ev the name of the event to trigger
  937. * @param {Object} data additional data to pass with the event
  938. */
  939. trigger : function (ev, data) {
  940. if(!data) {
  941. data = {};
  942. }
  943. data.instance = this;
  944. this.element.triggerHandler(ev.replace('.jstree','') + '.jstree', data);
  945. },
  946. /**
  947. * returns the jQuery extended instance container
  948. * @name get_container()
  949. * @return {jQuery}
  950. */
  951. get_container : function () {
  952. return this.element;
  953. },
  954. /**
  955. * returns the jQuery extended main UL node inside the instance container. Used internally.
  956. * @private
  957. * @name get_container_ul()
  958. * @return {jQuery}
  959. */
  960. get_container_ul : function () {
  961. return this.element.children(".jstree-children").first();
  962. },
  963. /**
  964. * gets string replacements (localization). Used internally.
  965. * @private
  966. * @name get_string(key)
  967. * @param {String} key
  968. * @return {String}
  969. */
  970. get_string : function (key) {
  971. var a = this.settings.core.strings;
  972. if($.vakata.is_function(a)) { return a.call(this, key); }
  973. if(a && a[key]) { return a[key]; }
  974. return key;
  975. },
  976. /**
  977. * gets the first child of a DOM node. Used internally.
  978. * @private
  979. * @name _firstChild(dom)
  980. * @param {DOMElement} dom
  981. * @return {DOMElement}
  982. */
  983. _firstChild : function (dom) {
  984. dom = dom ? dom.firstChild : null;
  985. while(dom !== null && dom.nodeType !== 1) {
  986. dom = dom.nextSibling;
  987. }
  988. return dom;
  989. },
  990. /**
  991. * gets the next sibling of a DOM node. Used internally.
  992. * @private
  993. * @name _nextSibling(dom)
  994. * @param {DOMElement} dom
  995. * @return {DOMElement}
  996. */
  997. _nextSibling : function (dom) {
  998. dom = dom ? dom.nextSibling : null;
  999. while(dom !== null && dom.nodeType !== 1) {
  1000. dom = dom.nextSibling;
  1001. }
  1002. return dom;
  1003. },
  1004. /**
  1005. * gets the previous sibling of a DOM node. Used internally.
  1006. * @private
  1007. * @name _previousSibling(dom)
  1008. * @param {DOMElement} dom
  1009. * @return {DOMElement}
  1010. */
  1011. _previousSibling : function (dom) {
  1012. dom = dom ? dom.previousSibling : null;
  1013. while(dom !== null && dom.nodeType !== 1) {
  1014. dom = dom.previousSibling;
  1015. }
  1016. return dom;
  1017. },
  1018. /**
  1019. * 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)
  1020. * @name get_node(obj [, as_dom])
  1021. * @param {mixed} obj
  1022. * @param {Boolean} as_dom
  1023. * @return {Object|jQuery}
  1024. */
  1025. get_node : function (obj, as_dom) {
  1026. if(obj && (obj.id || obj.id === 0)) {
  1027. obj = obj.id;
  1028. }
  1029. if (obj instanceof $ && obj.length && obj[0].id) {
  1030. obj = obj[0].id;
  1031. }
  1032. var dom;
  1033. try {
  1034. if(this._model.data[obj]) {
  1035. obj = this._model.data[obj];
  1036. }
  1037. else if(typeof obj === "string" && this._model.data[obj.replace(/^#/, '')]) {
  1038. obj = this._model.data[obj.replace(/^#/, '')];
  1039. }
  1040. else if(typeof obj === "string" && (dom = $('#' + obj.replace($.jstree.idregex,'\\$&'), this.element)).length && this._model.data[dom.closest('.jstree-node').attr('id')]) {
  1041. obj = this._model.data[dom.closest('.jstree-node').attr('id')];
  1042. }
  1043. else if((dom = this.element.find(obj)).length && this._model.data[dom.closest('.jstree-node').attr('id')]) {
  1044. obj = this._model.data[dom.closest('.jstree-node').attr('id')];
  1045. }
  1046. else if((dom = this.element.find(obj)).length && dom.hasClass('jstree')) {
  1047. obj = this._model.data[$.jstree.root];
  1048. }
  1049. else {
  1050. return false;
  1051. }
  1052. if(as_dom) {
  1053. obj = obj.id === $.jstree.root ? this.element : $('#' + obj.id.replace($.jstree.idregex,'\\$&'), this.element);
  1054. }
  1055. return obj;
  1056. } catch (ex) { return false; }
  1057. },
  1058. /**
  1059. * get the path to a node, either consisting of node texts, or of node IDs, optionally glued together (otherwise an array)
  1060. * @name get_path(obj [, glue, ids])
  1061. * @param {mixed} obj the node
  1062. * @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
  1063. * @param {Boolean} ids if set to true build the path using ID, otherwise node text is used
  1064. * @return {mixed}
  1065. */
  1066. get_path : function (obj, glue, ids) {
  1067. obj = obj.parents ? obj : this.get_node(obj);
  1068. if(!obj || obj.id === $.jstree.root || !obj.parents) {
  1069. return false;
  1070. }
  1071. var i, j, p = [];
  1072. p.push(ids ? obj.id : obj.text);
  1073. for(i = 0, j = obj.parents.length; i < j; i++) {
  1074. p.push(ids ? obj.parents[i] : this.get_text(obj.parents[i]));
  1075. }
  1076. p = p.reverse().slice(1);
  1077. return glue ? p.join(glue) : p;
  1078. },
  1079. /**
  1080. * get the next visible node that is below the `obj` node. If `strict` is set to `true` only sibling nodes are returned.
  1081. * @name get_next_dom(obj [, strict])
  1082. * @param {mixed} obj
  1083. * @param {Boolean} strict
  1084. * @return {jQuery}
  1085. */
  1086. get_next_dom : function (obj, strict) {
  1087. var tmp;
  1088. obj = this.get_node(obj, true);
  1089. if(obj[0] === this.element[0]) {
  1090. tmp = this._firstChild(this.get_container_ul()[0]);
  1091. while (tmp && tmp.offsetHeight === 0) {
  1092. tmp = this._nextSibling(tmp);
  1093. }
  1094. return tmp ? $(tmp) : false;
  1095. }
  1096. if(!obj || !obj.length) {
  1097. return false;
  1098. }
  1099. if(strict) {
  1100. tmp = obj[0];
  1101. do {
  1102. tmp = this._nextSibling(tmp);
  1103. } while (tmp && tmp.offsetHeight === 0);
  1104. return tmp ? $(tmp) : false;
  1105. }
  1106. if(obj.hasClass("jstree-open")) {
  1107. tmp = this._firstChild(obj.children('.jstree-children')[0]);
  1108. while (tmp && tmp.offsetHeight === 0) {
  1109. tmp = this._nextSibling(tmp);
  1110. }
  1111. if(tmp !== null) {
  1112. return $(tmp);
  1113. }
  1114. }
  1115. tmp = obj[0];
  1116. do {
  1117. tmp = this._nextSibling(tmp);
  1118. } while (tmp && tmp.offsetHeight === 0);
  1119. if(tmp !== null) {
  1120. return $(tmp);
  1121. }
  1122. return obj.parentsUntil(".jstree",".jstree-node").nextAll(".jstree-node:visible").first();
  1123. },
  1124. /**
  1125. * get the previous visible node that is above the `obj` node. If `strict` is set to `true` only sibling nodes are returned.
  1126. * @name get_prev_dom(obj [, strict])
  1127. * @param {mixed} obj
  1128. * @param {Boolean} strict
  1129. * @return {jQuery}
  1130. */
  1131. get_prev_dom : function (obj, strict) {
  1132. var tmp;
  1133. obj = this.get_node(obj, true);
  1134. if(obj[0] === this.element[0]) {
  1135. tmp = this.get_container_ul()[0].lastChild;
  1136. while (tmp && tmp.offsetHeight === 0) {
  1137. tmp = this._previousSibling(tmp);
  1138. }
  1139. return tmp ? $(tmp) : false;
  1140. }
  1141. if(!obj || !obj.length) {
  1142. return false;
  1143. }
  1144. if(strict) {
  1145. tmp = obj[0];
  1146. do {
  1147. tmp = this._previousSibling(tmp);
  1148. } while (tmp && tmp.offsetHeight === 0);
  1149. return tmp ? $(tmp) : false;
  1150. }
  1151. tmp = obj[0];
  1152. do {
  1153. tmp = this._previousSibling(tmp);
  1154. } while (tmp && tmp.offsetHeight === 0);
  1155. if(tmp !== null) {
  1156. obj = $(tmp);
  1157. while(obj.hasClass("jstree-open")) {
  1158. obj = obj.children(".jstree-children").first().children(".jstree-node:visible:last");
  1159. }
  1160. return obj;
  1161. }
  1162. tmp = obj[0].parentNode.parentNode;
  1163. return tmp && tmp.className && tmp.className.indexOf('jstree-node') !== -1 ? $(tmp) : false;
  1164. },
  1165. /**
  1166. * get the parent ID of a node
  1167. * @name get_parent(obj)
  1168. * @param {mixed} obj
  1169. * @return {String}
  1170. */
  1171. get_parent : function (obj) {
  1172. obj = this.get_node(obj);
  1173. if(!obj || obj.id === $.jstree.root) {
  1174. return false;
  1175. }
  1176. return obj.parent;
  1177. },
  1178. /**
  1179. * get a jQuery collection of all the children of a node (node must be rendered), returns false on error
  1180. * @name get_children_dom(obj)
  1181. * @param {mixed} obj
  1182. * @return {jQuery}
  1183. */
  1184. get_children_dom : function (obj) {
  1185. obj = this.get_node(obj, true);
  1186. if(obj[0] === this.element[0]) {
  1187. return this.get_container_ul().children(".jstree-node");
  1188. }
  1189. if(!obj || !obj.length) {
  1190. return false;
  1191. }
  1192. return obj.children(".jstree-children").children(".jstree-node");
  1193. },
  1194. /**
  1195. * checks if a node has children
  1196. * @name is_parent(obj)
  1197. * @param {mixed} obj
  1198. * @return {Boolean}
  1199. */
  1200. is_parent : function (obj) {
  1201. obj = this.get_node(obj);
  1202. return obj && (obj.state.loaded === false || obj.children.length > 0);
  1203. },
  1204. /**
  1205. * checks if a node is loaded (its children are available)
  1206. * @name is_loaded(obj)
  1207. * @param {mixed} obj
  1208. * @return {Boolean}
  1209. */
  1210. is_loaded : function (obj) {
  1211. obj = this.get_node(obj);
  1212. return obj && obj.state.loaded;
  1213. },
  1214. /**
  1215. * check if a node is currently loading (fetching children)
  1216. * @name is_loading(obj)
  1217. * @param {mixed} obj
  1218. * @return {Boolean}
  1219. */
  1220. is_loading : function (obj) {
  1221. obj = this.get_node(obj);
  1222. return obj && obj.state && obj.state.loading;
  1223. },
  1224. /**
  1225. * check if a node is opened
  1226. * @name is_open(obj)
  1227. * @param {mixed} obj
  1228. * @return {Boolean}
  1229. */
  1230. is_open : function (obj) {
  1231. obj = this.get_node(obj);
  1232. return obj && obj.state.opened;
  1233. },
  1234. /**
  1235. * check if a node is in a closed state
  1236. * @name is_closed(obj)
  1237. * @param {mixed} obj
  1238. * @return {Boolean}
  1239. */
  1240. is_closed : function (obj) {
  1241. obj = this.get_node(obj);
  1242. return obj && this.is_parent(obj) && !obj.state.opened;
  1243. },
  1244. /**
  1245. * check if a node has no children
  1246. * @name is_leaf(obj)
  1247. * @param {mixed} obj
  1248. * @return {Boolean}
  1249. */
  1250. is_leaf : function (obj) {
  1251. return !this.is_parent(obj);
  1252. },
  1253. /**
  1254. * loads a node (fetches its children using the `core.data` setting). Multiple nodes can be passed to by using an array.
  1255. * @name load_node(obj [, callback])
  1256. * @param {mixed} obj
  1257. * @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
  1258. * @return {Boolean}
  1259. * @trigger load_node.jstree
  1260. */
  1261. load_node : function (obj, callback) {
  1262. var dom = this.get_node(obj, true), k, l, i, j, c;
  1263. if($.vakata.is_array(obj)) {
  1264. this._load_nodes(obj.slice(), callback);
  1265. return true;
  1266. }
  1267. obj = this.get_node(obj);
  1268. if(!obj) {
  1269. if(callback) { callback.call(this, obj, false); }
  1270. return false;
  1271. }
  1272. // 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?
  1273. if(obj.state.loaded) {
  1274. obj.state.loaded = false;
  1275. for(i = 0, j = obj.parents.length; i < j; i++) {
  1276. this._model.data[obj.parents[i]].children_d = $.vakata.array_filter(this._model.data[obj.parents[i]].children_d, function (v) {
  1277. return $.inArray(v, obj.children_d) === -1;
  1278. });
  1279. }
  1280. for(k = 0, l = obj.children_d.length; k < l; k++) {
  1281. if(this._model.data[obj.children_d[k]].state.selected) {
  1282. c = true;
  1283. }
  1284. delete this._model.data[obj.children_d[k]];
  1285. }
  1286. if (c) {
  1287. this._data.core.selected = $.vakata.array_filter(this._data.core.selected, function (v) {
  1288. return $.inArray(v, obj.children_d) === -1;
  1289. });
  1290. }
  1291. obj.children = [];
  1292. obj.children_d = [];
  1293. if(c) {
  1294. this.trigger('changed', { 'action' : 'load_node', 'node' : obj, 'selected' : this._data.core.selected });
  1295. }
  1296. }
  1297. obj.state.failed = false;
  1298. obj.state.loading = true;
  1299. if (obj.id !== $.jstree.root) {
  1300. dom.children(".jstree-anchor").attr('aria-busy', true);
  1301. } else {
  1302. dom.attr('aria-busy', true);
  1303. }
  1304. dom.addClass("jstree-loading");
  1305. this._load_node(obj, function (status) {
  1306. obj = this._model.data[obj.id];
  1307. obj.state.loading = false;
  1308. obj.state.loaded = status;
  1309. obj.state.failed = !obj.state.loaded;
  1310. var dom = this.get_node(obj, true), i = 0, j = 0, m = this._model.data, has_children = false;
  1311. for(i = 0, j = obj.children.length; i < j; i++) {
  1312. if(m[obj.children[i]] && !m[obj.children[i]].state.hidden) {
  1313. has_children = true;
  1314. break;
  1315. }
  1316. }
  1317. if(obj.state.loaded && dom && dom.length) {
  1318. dom.removeClass('jstree-closed jstree-open jstree-leaf');
  1319. if (!has_children) {
  1320. dom.addClass('jstree-leaf');
  1321. }
  1322. else {
  1323. if (obj.id !== '#') {
  1324. dom.addClass(obj.state.opened ? 'jstree-open' : 'jstree-closed');
  1325. }
  1326. }
  1327. }
  1328. if (obj.id !== $.jstree.root) {
  1329. dom.children(".jstree-anchor").attr('aria-busy', false);
  1330. } else {
  1331. dom.attr('aria-busy', false);
  1332. }
  1333. dom.removeClass("jstree-loading");
  1334. /**
  1335. * triggered after a node is loaded
  1336. * @event
  1337. * @name load_node.jstree
  1338. * @param {Object} node the node that was loading
  1339. * @param {Boolean} status was the node loaded successfully
  1340. */
  1341. this.trigger('load_node', { "node" : obj, "status" : status });
  1342. if(callback) {
  1343. callback.call(this, obj, status);
  1344. }
  1345. }.bind(this));
  1346. return true;
  1347. },
  1348. /**
  1349. * load an array of nodes (will also load unavailable nodes as soon as they appear in the structure). Used internally.
  1350. * @private
  1351. * @name _load_nodes(nodes [, callback])
  1352. * @param {array} nodes
  1353. * @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
  1354. */
  1355. _load_nodes : function (nodes, callback, is_callback, force_reload) {
  1356. var r = true,
  1357. c = function () { this._load_nodes(nodes, callback, true); },
  1358. m = this._model.data, i, j, tmp = [];
  1359. for(i = 0, j = nodes.length; i < j; i++) {
  1360. if(m[nodes[i]] && ( (!m[nodes[i]].state.loaded && !m[nodes[i]].state.failed) || (!is_callback && force_reload) )) {
  1361. if(!this.is_loading(nodes[i])) {
  1362. this.load_node(nodes[i], c);
  1363. }
  1364. r = false;
  1365. }
  1366. }
  1367. if(r) {
  1368. for(i = 0, j = nodes.length; i < j; i++) {
  1369. if(m[nodes[i]] && m[nodes[i]].state.loaded) {
  1370. tmp.push(nodes[i]);
  1371. }
  1372. }
  1373. if(callback && !callback.done) {
  1374. callback.call(this, tmp);
  1375. callback.done = true;
  1376. }
  1377. }
  1378. },
  1379. /**
  1380. * loads all unloaded nodes
  1381. * @name load_all([obj, callback])
  1382. * @param {mixed} obj the node to load recursively, omit to load all nodes in the tree
  1383. * @param {function} callback a function to be executed once loading all the nodes is complete,
  1384. * @trigger load_all.jstree
  1385. */
  1386. load_all : function (obj, callback) {
  1387. if(!obj) { obj = $.jstree.root; }
  1388. obj = this.get_node(obj);
  1389. if(!obj) { return false; }
  1390. var to_load = [],
  1391. m = this._model.data,
  1392. c = m[obj.id].children_d,
  1393. i, j;
  1394. if(obj.state && !obj.state.loaded) {
  1395. to_load.push(obj.id);
  1396. }
  1397. for(i = 0, j = c.length; i < j; i++) {
  1398. if(m[c[i]] && m[c[i]].state && !m[c[i]].state.loaded) {
  1399. to_load.push(c[i]);
  1400. }
  1401. }
  1402. if(to_load.length) {
  1403. this._load_nodes(to_load, function () {
  1404. this.load_all(obj, callback);
  1405. });
  1406. }
  1407. else {
  1408. /**
  1409. * triggered after a load_all call completes
  1410. * @event
  1411. * @name load_all.jstree
  1412. * @param {Object} node the recursively loaded node
  1413. */
  1414. if(callback) { callback.call(this, obj); }
  1415. this.trigger('load_all', { "node" : obj });
  1416. }
  1417. },
  1418. /**
  1419. * handles the actual loading of a node. Used only internally.
  1420. * @private
  1421. * @name _load_node(obj [, callback])
  1422. * @param {mixed} obj
  1423. * @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
  1424. * @return {Boolean}
  1425. */
  1426. _load_node : function (obj, callback) {
  1427. var s = this.settings.core.data, t;
  1428. var notTextOrCommentNode = function notTextOrCommentNode () {
  1429. return this.nodeType !== 3 && this.nodeType !== 8;
  1430. };
  1431. // use original HTML
  1432. if(!s) {
  1433. if(obj.id === $.jstree.root) {
  1434. return this._append_html_data(obj, this._data.core.original_container_html.clone(true), function (status) {
  1435. callback.call(this, status);
  1436. });
  1437. }
  1438. else {
  1439. return callback.call(this, false);
  1440. }
  1441. // return callback.call(this, obj.id === $.jstree.root ? this._append_html_data(obj, this._data.core.original_container_html.clone(true)) : false);
  1442. }
  1443. if($.vakata.is_function(s)) {
  1444. return s.call(this, obj, function (d) {
  1445. if(d === false) {
  1446. callback.call(this, false);
  1447. }
  1448. else {
  1449. this[typeof d === 'string' ? '_append_html_data' : '_append_json_data'](obj, typeof d === 'string' ? $($.parseHTML(d)).filter(notTextOrCommentNode) : d, function (status) {
  1450. callback.call(this, status);
  1451. });
  1452. }
  1453. // 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));
  1454. }.bind(this));
  1455. }
  1456. if(typeof s === 'object') {
  1457. if(s.url) {
  1458. s = $.extend(true, {}, s);
  1459. if($.vakata.is_function(s.url)) {
  1460. s.url = s.url.call(this, obj);
  1461. }
  1462. if($.vakata.is_function(s.data)) {
  1463. s.data = s.data.call(this, obj);
  1464. }
  1465. return $.ajax(s)
  1466. .done(function (d,t,x) {
  1467. var type = x.getResponseHeader('Content-Type');
  1468. if((type && type.indexOf('json') !== -1) || typeof d === "object") {
  1469. return this._append_json_data(obj, d, function (status) { callback.call(this, status); });
  1470. //return callback.call(this, this._append_json_data(obj, d));
  1471. }
  1472. if((type && type.indexOf('html') !== -1) || typeof d === "string") {
  1473. return this._append_html_data(obj, $($.parseHTML(d)).filter(notTextOrCommentNode), function (status) { callback.call(this, status); });
  1474. // return callback.call(this, this._append_html_data(obj, $(d)));
  1475. }
  1476. 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 }) };
  1477. this.settings.core.error.call(this, this._data.core.last_error);
  1478. return callback.call(this, false);
  1479. }.bind(this))
  1480. .fail(function (f) {
  1481. 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 }) };
  1482. callback.call(this, false);
  1483. this.settings.core.error.call(this, this._data.core.last_error);
  1484. }.bind(this));
  1485. }
  1486. if ($.vakata.is_array(s)) {
  1487. t = $.extend(true, [], s);
  1488. } else if ($.isPlainObject(s)) {
  1489. t = $.extend(true, {}, s);
  1490. } else {
  1491. t = s;
  1492. }
  1493. if(obj.id === $.jstree.root) {
  1494. return this._append_json_data(obj, t, function (status) {
  1495. callback.call(this, status);
  1496. });
  1497. }
  1498. else {
  1499. this._data.core.last_error = { 'error' : 'nodata', 'plugin' : 'core', 'id' : 'core_05', 'reason' : 'Could not load node', 'data' : JSON.stringify({ 'id' : obj.id }) };
  1500. this.settings.core.error.call(this, this._data.core.last_error);
  1501. return callback.call(this, false);
  1502. }
  1503. //return callback.call(this, (obj.id === $.jstree.root ? this._append_json_data(obj, t) : false) );
  1504. }
  1505. if(typeof s === 'string') {
  1506. if(obj.id === $.jstree.root) {
  1507. return this._append_html_data(obj, $($.parseHTML(s)).filter(notTextOrCommentNode), function (status) {
  1508. callback.call(this, status);
  1509. });
  1510. }
  1511. else {
  1512. this._data.core.last_error = { 'error' : 'nodata', 'plugin' : 'core', 'id' : 'core_06', 'reason' : 'Could not load node', 'data' : JSON.stringify({ 'id' : obj.id }) };
  1513. this.settings.core.error.call(this, this._data.core.last_error);
  1514. return callback.call(this, false);
  1515. }
  1516. //return callback.call(this, (obj.id === $.jstree.root ? this._append_html_data(obj, $(s)) : false) );
  1517. }
  1518. return callback.call(this, false);
  1519. },
  1520. /**
  1521. * adds a node to the list of nodes to redraw. Used only internally.
  1522. * @private
  1523. * @name _node_changed(obj [, callback])
  1524. * @param {mixed} obj
  1525. */
  1526. _node_changed : function (obj) {
  1527. obj = this.get_node(obj);
  1528. if (obj && $.inArray(obj.id, this._model.changed) === -1) {
  1529. this._model.changed.push(obj.id);
  1530. }
  1531. },
  1532. /**
  1533. * appends HTML content to the tree. Used internally.
  1534. * @private
  1535. * @name _append_html_data(obj, data)
  1536. * @param {mixed} obj the node to append to
  1537. * @param {String} data the HTML string to parse and append
  1538. * @trigger model.jstree, changed.jstree
  1539. */
  1540. _append_html_data : function (dom, data, cb) {
  1541. dom = this.get_node(dom);
  1542. dom.children = [];
  1543. dom.children_d = [];
  1544. var dat = data.is('ul') ? data.children() : data,
  1545. par = dom.id,
  1546. chd = [],
  1547. dpc = [],
  1548. m = this._model.data,
  1549. p = m[par],
  1550. s = this._data.core.selected.length,
  1551. tmp, i, j;
  1552. dat.each(function (i, v) {
  1553. tmp = this._parse_model_from_html($(v), par, p.parents.concat());
  1554. if(tmp) {
  1555. chd.push(tmp);
  1556. dpc.push(tmp);
  1557. if(m[tmp].children_d.length) {
  1558. dpc = dpc.concat(m[tmp].children_d);
  1559. }
  1560. }
  1561. }.bind(this));
  1562. p.children = chd;
  1563. p.children_d = dpc;
  1564. for(i = 0, j = p.parents.length; i < j; i++) {
  1565. m[p.parents[i]].children_d = m[p.parents[i]].children_d.concat(dpc);
  1566. }
  1567. /**
  1568. * triggered when new data is inserted to the tree model
  1569. * @event
  1570. * @name model.jstree
  1571. * @param {Array} nodes an array of node IDs
  1572. * @param {String} parent the parent ID of the nodes
  1573. */
  1574. this.trigger('model', { "nodes" : dpc, 'parent' : par });
  1575. if(par !== $.jstree.root) {
  1576. this._node_changed(par);
  1577. this.redraw();
  1578. }
  1579. else {
  1580. this.get_container_ul().children('.jstree-initial-node').remove();
  1581. this.redraw(true);
  1582. }
  1583. if(this._data.core.selected.length !== s) {
  1584. this.trigger('changed', { 'action' : 'model', 'selected' : this._data.core.selected });
  1585. }
  1586. cb.call(this, true);
  1587. },
  1588. /**
  1589. * appends JSON content to the tree. Used internally.
  1590. * @private
  1591. * @name _append_json_data(obj, data)
  1592. * @param {mixed} obj the node to append to
  1593. * @param {String} data the JSON object to parse and append
  1594. * @param {Boolean} force_processing internal param - do not set
  1595. * @trigger model.jstree, changed.jstree
  1596. */
  1597. _append_json_data : function (dom, data, cb, force_processing) {
  1598. if(this.element === null) { return; }
  1599. dom = this.get_node(dom);
  1600. dom.children = [];
  1601. dom.children_d = [];
  1602. // *%$@!!!
  1603. if(data.d) {
  1604. data = data.d;
  1605. if(typeof data === "string") {
  1606. data = JSON.parse(data);
  1607. }
  1608. }
  1609. if(!$.vakata.is_array(data)) { data = [data]; }
  1610. var w = null,
  1611. args = {
  1612. 'df' : this._model.default_state,
  1613. 'dat' : data,
  1614. 'par' : dom.id,
  1615. 'm' : this._model.data,
  1616. 't_id' : this._id,
  1617. 't_cnt' : this._cnt,
  1618. 'sel' : this._data.core.selected
  1619. },
  1620. inst = this,
  1621. func = function (data, undefined) {
  1622. if(data.data) { data = data.data; }
  1623. var dat = data.dat,
  1624. par = data.par,
  1625. chd = [],
  1626. dpc = [],
  1627. add = [],
  1628. df = data.df,
  1629. t_id = data.t_id,
  1630. t_cnt = data.t_cnt,
  1631. m = data.m,
  1632. p = m[par],
  1633. sel = data.sel,
  1634. tmp, i, j, rslt,
  1635. parse_flat = function (d, p, ps) {
  1636. if(!ps) { ps = []; }
  1637. else { ps = ps.concat(); }
  1638. if(p) { ps.unshift(p); }
  1639. var tid = d.id.toString(),
  1640. i, j, c, e,
  1641. tmp = {
  1642. id : tid,
  1643. text : d.text || '',
  1644. icon : d.icon !== undefined ? d.icon : true,
  1645. parent : p,
  1646. parents : ps,
  1647. children : d.children || [],
  1648. children_d : d.children_d || [],
  1649. data : d.data,
  1650. state : { },
  1651. li_attr : { id : false },
  1652. a_attr : { href : '#' },
  1653. original : false
  1654. };
  1655. for(i in df) {
  1656. if(df.hasOwnProperty(i)) {
  1657. tmp.state[i] = df[i];
  1658. }
  1659. }
  1660. if(d && d.data && d.data.jstree && d.data.jstree.icon) {
  1661. tmp.icon = d.data.jstree.icon;
  1662. }
  1663. if(tmp.icon === undefined || tmp.icon === null || tmp.icon === "") {
  1664. tmp.icon = true;
  1665. }
  1666. if(d && d.data) {
  1667. tmp.data = d.data;
  1668. if(d.data.jstree) {
  1669. for(i in d.data.jstree) {
  1670. if(d.data.jstree.hasOwnProperty(i)) {
  1671. tmp.state[i] = d.data.jstree[i];
  1672. }
  1673. }
  1674. }
  1675. }
  1676. if(d && typeof d.state === 'object') {
  1677. for (i in d.state) {
  1678. if(d.state.hasOwnProperty(i)) {
  1679. tmp.state[i] = d.state[i];
  1680. }
  1681. }
  1682. }
  1683. if(d && typeof d.li_attr === 'object') {
  1684. for (i in d.li_attr) {
  1685. if(d.li_attr.hasOwnProperty(i)) {
  1686. tmp.li_attr[i] = d.li_attr[i];
  1687. }
  1688. }
  1689. }
  1690. if(!tmp.li_attr.id) {
  1691. tmp.li_attr.id = tid;
  1692. }
  1693. if(d && typeof d.a_attr === 'object') {
  1694. for (i in d.a_attr) {
  1695. if(d.a_attr.hasOwnProperty(i)) {
  1696. tmp.a_attr[i] = d.a_attr[i];
  1697. }
  1698. }
  1699. }
  1700. if(d && d.children && d.children === true) {
  1701. tmp.state.loaded = false;
  1702. tmp.children = [];
  1703. tmp.children_d = [];
  1704. }
  1705. m[tmp.id] = tmp;
  1706. for(i = 0, j = tmp.children.length; i < j; i++) {
  1707. c = parse_flat(m[tmp.children[i]], tmp.id, ps);
  1708. e = m[c];
  1709. tmp.children_d.push(c);
  1710. if(e.children_d.length) {
  1711. tmp.children_d = tmp.children_d.concat(e.children_d);
  1712. }
  1713. }
  1714. delete d.data;
  1715. delete d.children;
  1716. m[tmp.id].original = d;
  1717. if(tmp.state.selected) {
  1718. add.push(tmp.id);
  1719. }
  1720. return tmp.id;
  1721. },
  1722. parse_nest = function (d, p, ps) {
  1723. if(!ps) { ps = []; }
  1724. else { ps = ps.concat(); }
  1725. if(p) { ps.unshift(p); }
  1726. var tid = false, i, j, c, e, tmp;
  1727. do {
  1728. tid = 'j' + t_id + '_' + (++t_cnt);
  1729. } while(m[tid]);
  1730. tmp = {
  1731. id : false,
  1732. text : typeof d === 'string' ? d : '',
  1733. icon : typeof d === 'object' && d.icon !== undefined ? d.icon : true,
  1734. parent : p,
  1735. parents : ps,
  1736. children : [],
  1737. children_d : [],
  1738. data : null,
  1739. state : { },
  1740. li_attr : { id : false },
  1741. a_attr : { href : '#' },
  1742. original : false
  1743. };
  1744. for(i in df) {
  1745. if(df.hasOwnProperty(i)) {
  1746. tmp.state[i] = df[i];
  1747. }
  1748. }
  1749. if(d && (d.id || d.id === 0)) { tmp.id = d.id.toString(); }
  1750. if(d && d.text) { tmp.text = d.text; }
  1751. if(d && d.data && d.data.jstree && d.data.jstree.icon) {
  1752. tmp.icon = d.data.jstree.icon;
  1753. }
  1754. if(tmp.icon === undefined || tmp.icon === null || tmp.icon === "") {
  1755. tmp.icon = true;
  1756. }
  1757. if(d && d.data) {
  1758. tmp.data = d.data;
  1759. if(d.data.jstree) {
  1760. for(i in d.data.jstree) {
  1761. if(d.data.jstree.hasOwnProperty(i)) {
  1762. tmp.state[i] = d.data.jstree[i];
  1763. }
  1764. }
  1765. }
  1766. }
  1767. if(d && typeof d.state === 'object') {
  1768. for (i in d.state) {
  1769. if(d.state.hasOwnProperty(i)) {
  1770. tmp.state[i] = d.state[i];
  1771. }
  1772. }
  1773. }
  1774. if(d && typeof d.li_attr === 'object') {
  1775. for (i in d.li_attr) {
  1776. if(d.li_attr.hasOwnProperty(i)) {
  1777. tmp.li_attr[i] = d.li_attr[i];
  1778. }
  1779. }
  1780. }
  1781. if(tmp.li_attr.id && !(tmp.id || tmp.id === 0)) {
  1782. tmp.id = tmp.li_attr.id.toString();
  1783. }
  1784. if(!(tmp.id || tmp.id === 0)) {
  1785. tmp.id = tid;
  1786. }
  1787. if(!tmp.li_attr.id) {
  1788. tmp.li_attr.id = tmp.id;
  1789. }
  1790. if(d && typeof d.a_attr === 'object') {
  1791. for (i in d.a_attr) {
  1792. if(d.a_attr.hasOwnProperty(i)) {
  1793. tmp.a_attr[i] = d.a_attr[i];
  1794. }
  1795. }
  1796. }
  1797. if(d && d.children && d.children.length) {
  1798. for(i = 0, j = d.children.length; i < j; i++) {
  1799. c = parse_nest(d.children[i], tmp.id, ps);
  1800. e = m[c];
  1801. tmp.children.push(c);
  1802. if(e.children_d.length) {
  1803. tmp.children_d = tmp.children_d.concat(e.children_d);
  1804. }
  1805. }
  1806. tmp.children_d = tmp.children_d.concat(tmp.children);
  1807. }
  1808. if(d && d.children && d.children === true) {
  1809. tmp.state.loaded = false;
  1810. tmp.children = [];
  1811. tmp.children_d = [];
  1812. }
  1813. delete d.data;
  1814. delete d.children;
  1815. tmp.original = d;
  1816. m[tmp.id] = tmp;
  1817. if(tmp.state.selected) {
  1818. add.push(tmp.id);
  1819. }
  1820. return tmp.id;
  1821. };
  1822. if(dat.length && dat[0].id !== undefined && dat[0].parent !== undefined) {
  1823. // Flat JSON support (for easy import from DB):
  1824. // 1) convert to object (foreach)
  1825. for(i = 0, j = dat.length; i < j; i++) {
  1826. if(!dat[i].children) {
  1827. dat[i].children = [];
  1828. }
  1829. if(!dat[i].state) {
  1830. dat[i].state = {};
  1831. }
  1832. m[dat[i].id.toString()] = dat[i];
  1833. }
  1834. // 2) populate children (foreach)
  1835. for(i = 0, j = dat.length; i < j; i++) {
  1836. if (!m[dat[i].parent.toString()]) {
  1837. if (typeof inst !== "undefined") {
  1838. 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() }) };
  1839. inst.settings.core.error.call(inst, inst._data.core.last_error);
  1840. }
  1841. continue;
  1842. }
  1843. m[dat[i].parent.toString()].children.push(dat[i].id.toString());
  1844. // populate parent.children_d
  1845. p.children_d.push(dat[i].id.toString());
  1846. }
  1847. // 3) normalize && populate parents and children_d with recursion
  1848. for(i = 0, j = p.children.length; i < j; i++) {
  1849. tmp = parse_flat(m[p.children[i]], par, p.parents.concat());
  1850. dpc.push(tmp);
  1851. if(m[tmp].children_d.length) {
  1852. dpc = dpc.concat(m[tmp].children_d);
  1853. }
  1854. }
  1855. for(i = 0, j = p.parents.length; i < j; i++) {
  1856. m[p.parents[i]].children_d = m[p.parents[i]].children_d.concat(dpc);
  1857. }
  1858. // ?) three_state selection - p.state.selected && t - (if three_state foreach(dat => ch) -> foreach(parents) if(parent.selected) child.selected = true;
  1859. rslt = {
  1860. 'cnt' : t_cnt,
  1861. 'mod' : m,
  1862. 'sel' : sel,
  1863. 'par' : par,
  1864. 'dpc' : dpc,
  1865. 'add' : add
  1866. };
  1867. }
  1868. else {
  1869. for(i = 0, j = dat.length; i < j; i++) {
  1870. tmp = parse_nest(dat[i], par, p.parents.concat());
  1871. if(tmp) {
  1872. chd.push(tmp);
  1873. dpc.push(tmp);
  1874. if(m[tmp].children_d.length) {
  1875. dpc = dpc.concat(m[tmp].children_d);
  1876. }
  1877. }
  1878. }
  1879. p.children = chd;
  1880. p.children_d = dpc;
  1881. for(i = 0, j = p.parents.length; i < j; i++) {
  1882. m[p.parents[i]].children_d = m[p.parents[i]].children_d.concat(dpc);
  1883. }
  1884. rslt = {
  1885. 'cnt' : t_cnt,
  1886. 'mod' : m,
  1887. 'sel' : sel,
  1888. 'par' : par,
  1889. 'dpc' : dpc,
  1890. 'add' : add
  1891. };
  1892. }
  1893. if(typeof window === 'undefined' || typeof window.document === 'undefined') {
  1894. postMessage(rslt);
  1895. }
  1896. else {
  1897. return rslt;
  1898. }
  1899. },
  1900. rslt = function (rslt, worker) {
  1901. if(this.element === null) { return; }
  1902. this._cnt = rslt.cnt;
  1903. var i, m = this._model.data;
  1904. for (i in m) {
  1905. if (m.hasOwnProperty(i) && m[i].state && m[i].state.loading && rslt.mod[i]) {
  1906. rslt.mod[i].state.loading = true;
  1907. }
  1908. }
  1909. this._model.data = rslt.mod; // breaks the reference in load_node - careful
  1910. if(worker) {
  1911. var j, a = rslt.add, r = rslt.sel, s = this._data.core.selected.slice();
  1912. m = this._model.data;
  1913. // if selection was changed while calculating in worker
  1914. if(r.length !== s.length || $.vakata.array_unique(r.concat(s)).length !== r.length) {
  1915. // deselect nodes that are no longer selected
  1916. for(i = 0, j = r.length; i < j; i++) {
  1917. if($.inArray(r[i], a) === -1 && $.inArray(r[i], s) === -1) {
  1918. m[r[i]].state.selected = false;
  1919. }
  1920. }
  1921. // select nodes that were selected in the mean time
  1922. for(i = 0, j = s.length; i < j; i++) {
  1923. if($.inArray(s[i], r) === -1) {
  1924. m[s[i]].state.selected = true;
  1925. }
  1926. }
  1927. }
  1928. }
  1929. if(rslt.add.length) {
  1930. this._data.core.selected = this._data.core.selected.concat(rslt.add);
  1931. }
  1932. this.trigger('model', { "nodes" : rslt.dpc, 'parent' : rslt.par });
  1933. if(rslt.par !== $.jstree.root) {
  1934. this._node_changed(rslt.par);
  1935. this.redraw();
  1936. }
  1937. else {
  1938. // this.get_container_ul().children('.jstree-initial-node').remove();
  1939. this.redraw(true);
  1940. }
  1941. if(rslt.add.length) {
  1942. this.trigger('changed', { 'action' : 'model', 'selected' : this._data.core.selected });
  1943. }
  1944. // If no worker, try to mimic worker behavioour, by invoking cb asynchronously
  1945. if (!worker && setImmediate) {
  1946. setImmediate(function(){
  1947. cb.call(inst, true);
  1948. });
  1949. }
  1950. else {
  1951. cb.call(inst, true);
  1952. }
  1953. };
  1954. if(this.settings.core.worker && window.Blob && window.URL && window.Worker) {
  1955. try {
  1956. if(this._wrk === null) {
  1957. this._wrk = window.URL.createObjectURL(
  1958. new window.Blob(
  1959. ['self.onmessage = ' + func.toString()],
  1960. {type:"text/javascript"}
  1961. )
  1962. );
  1963. }
  1964. if(!this._data.core.working || force_processing) {
  1965. this._data.core.working = true;
  1966. w = new window.Worker(this._wrk);
  1967. w.onmessage = function (e) {
  1968. rslt.call(this, e.data, true);
  1969. try { w.terminate(); w = null; } catch(ignore) { }
  1970. if(this._data.core.worker_queue.length) {
  1971. this._append_json_data.apply(this, this._data.core.worker_queue.shift());
  1972. }
  1973. else {
  1974. this._data.core.working = false;
  1975. }
  1976. }.bind(this);
  1977. w.onerror = function (e) {
  1978. rslt.call(this, func(args), false);
  1979. if(this._data.core.worker_queue.length) {
  1980. this._append_json_data.apply(this, this._data.core.worker_queue.shift());
  1981. }
  1982. else {
  1983. this._data.core.working = false;
  1984. }
  1985. }.bind(this);
  1986. if(!args.par) {
  1987. if(this._data.core.worker_queue.length) {
  1988. this._append_json_data.apply(this, this._data.core.worker_queue.shift());
  1989. }
  1990. else {
  1991. this._data.core.working = false;
  1992. }
  1993. }
  1994. else {
  1995. w.postMessage(args);
  1996. }
  1997. }
  1998. else {
  1999. this._data.core.worker_queue.push([dom, data, cb, true]);
  2000. }
  2001. }
  2002. catch(e) {
  2003. rslt.call(this, func(args), false);
  2004. if(this._data.core.worker_queue.length) {
  2005. this._append_json_data.apply(this, this._data.core.worker_queue.shift());
  2006. }
  2007. else {
  2008. this._data.core.working = false;
  2009. }
  2010. }
  2011. }
  2012. else {
  2013. rslt.call(this, func(args), false);
  2014. }
  2015. },
  2016. /**
  2017. * parses a node from a jQuery object and appends them to the in memory tree model. Used internally.
  2018. * @private
  2019. * @name _parse_model_from_html(d [, p, ps])
  2020. * @param {jQuery} d the jQuery object to parse
  2021. * @param {String} p the parent ID
  2022. * @param {Array} ps list of all parents
  2023. * @return {String} the ID of the object added to the model
  2024. */
  2025. _parse_model_from_html : function (d, p, ps) {
  2026. if(!ps) { ps = []; }
  2027. else { ps = [].concat(ps); }
  2028. if(p) { ps.unshift(p); }
  2029. var c, e, m = this._model.data,
  2030. data = {
  2031. id : false,
  2032. text : false,
  2033. icon : true,
  2034. parent : p,
  2035. parents : ps,
  2036. children : [],
  2037. children_d : [],
  2038. data : null,
  2039. state : { },
  2040. li_attr : { id : false },
  2041. a_attr : { href : '#' },
  2042. original : false
  2043. }, i, tmp, tid;
  2044. for(i in this._model.default_state) {
  2045. if(this._model.default_state.hasOwnProperty(i)) {
  2046. data.state[i] = this._model.default_state[i];
  2047. }
  2048. }
  2049. tmp = $.vakata.attributes(d, true);
  2050. $.each(tmp, function (i, v) {
  2051. v = $.vakata.trim(v);
  2052. if(!v.length) { return true; }
  2053. data.li_attr[i] = v;
  2054. if(i === 'id') {
  2055. data.id = v.toString();
  2056. }
  2057. });
  2058. tmp = d.children('a').first();
  2059. if(tmp.length) {
  2060. tmp = $.vakata.attributes(tmp, true);
  2061. $.each(tmp, function (i, v) {
  2062. v = $.vakata.trim(v);
  2063. if(v.length) {
  2064. data.a_attr[i] = v;
  2065. }
  2066. });
  2067. }
  2068. tmp = d.children("a").first().length ? d.children("a").first().clone() : d.clone();
  2069. tmp.children("ins, i, ul").remove();
  2070. tmp = tmp.html();
  2071. tmp = $('<div></div>').html(tmp);
  2072. data.text = this.settings.core.force_text ? tmp.text() : tmp.html();
  2073. tmp = d.data();
  2074. data.data = tmp ? $.extend(true, {}, tmp) : null;
  2075. data.state.opened = d.hasClass('jstree-open');
  2076. data.state.selected = d.children('a').hasClass('jstree-clicked');
  2077. data.state.disabled = d.children('a').hasClass('jstree-disabled');
  2078. if(data.data && data.data.jstree) {
  2079. for(i in data.data.jstree) {
  2080. if(data.data.jstree.hasOwnProperty(i)) {
  2081. data.state[i] = data.data.jstree[i];
  2082. }
  2083. }
  2084. }
  2085. tmp = d.children("a").children(".jstree-themeicon");
  2086. if(tmp.length) {
  2087. data.icon = tmp.hasClass('jstree-themeicon-hidden') ? false : tmp.attr('rel');
  2088. }
  2089. if(data.state.icon !== undefined) {
  2090. data.icon = data.state.icon;
  2091. }
  2092. if(data.icon === undefined || data.icon === null || data.icon === "") {
  2093. data.icon = true;
  2094. }
  2095. tmp = d.children("ul").children("li");
  2096. do {
  2097. tid = 'j' + this._id + '_' + (++this._cnt);
  2098. } while(m[tid]);
  2099. data.id = data.li_attr.id ? data.li_attr.id.toString() : tid;
  2100. if(tmp.length) {
  2101. tmp.each(function (i, v) {
  2102. c = this._parse_model_from_html($(v), data.id, ps);
  2103. e = this._model.data[c];
  2104. data.children.push(c);
  2105. if(e.children_d.length) {
  2106. data.children_d = data.children_d.concat(e.children_d);
  2107. }
  2108. }.bind(this));
  2109. data.children_d = data.children_d.concat(data.children);
  2110. }
  2111. else {
  2112. if(d.hasClass('jstree-closed')) {
  2113. data.state.loaded = false;
  2114. }
  2115. }
  2116. if(data.li_attr['class']) {
  2117. data.li_attr['class'] = data.li_attr['class'].replace('jstree-closed','').replace('jstree-open','');
  2118. }
  2119. if(data.a_attr['class']) {
  2120. data.a_attr['class'] = data.a_attr['class'].replace('jstree-clicked','').replace('jstree-disabled','');
  2121. }
  2122. m[data.id] = data;
  2123. if(data.state.selected) {
  2124. this._data.core.selected.push(data.id);
  2125. }
  2126. return data.id;
  2127. },
  2128. /**
  2129. * 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.
  2130. * @private
  2131. * @name _parse_model_from_flat_json(d [, p, ps])
  2132. * @param {Object} d the JSON object to parse
  2133. * @param {String} p the parent ID
  2134. * @param {Array} ps list of all parents
  2135. * @return {String} the ID of the object added to the model
  2136. */
  2137. _parse_model_from_flat_json : function (d, p, ps) {
  2138. if(!ps) { ps = []; }
  2139. else { ps = ps.concat(); }
  2140. if(p) { ps.unshift(p); }
  2141. var tid = d.id.toString(),
  2142. m = this._model.data,
  2143. df = this._model.default_state,
  2144. i, j, c, e,
  2145. tmp = {
  2146. id : tid,
  2147. text : d.text || '',
  2148. icon : d.icon !== undefined ? d.icon : true,
  2149. parent : p,
  2150. parents : ps,
  2151. children : d.children || [],
  2152. children_d : d.children_d || [],
  2153. data : d.data,
  2154. state : { },
  2155. li_attr : { id : false },
  2156. a_attr : { href : '#' },
  2157. original : false
  2158. };
  2159. for(i in df) {
  2160. if(df.hasOwnProperty(i)) {
  2161. tmp.state[i] = df[i];
  2162. }
  2163. }
  2164. if(d && d.data && d.data.jstree && d.data.jstree.icon) {
  2165. tmp.icon = d.data.jstree.icon;
  2166. }
  2167. if(tmp.icon === undefined || tmp.icon === null || tmp.icon === "") {
  2168. tmp.icon = true;
  2169. }
  2170. if(d && d.data) {
  2171. tmp.data = d.data;
  2172. if(d.data.jstree) {
  2173. for(i in d.data.jstree) {
  2174. if(d.data.jstree.hasOwnProperty(i)) {
  2175. tmp.state[i] = d.data.jstree[i];
  2176. }
  2177. }
  2178. }
  2179. }
  2180. if(d && typeof d.state === 'object') {
  2181. for (i in d.state) {
  2182. if(d.state.hasOwnProperty(i)) {
  2183. tmp.state[i] = d.state[i];
  2184. }
  2185. }
  2186. }
  2187. if(d && typeof d.li_attr === 'object') {
  2188. for (i in d.li_attr) {
  2189. if(d.li_attr.hasOwnProperty(i)) {
  2190. tmp.li_attr[i] = d.li_attr[i];
  2191. }
  2192. }
  2193. }
  2194. if(!tmp.li_attr.id) {
  2195. tmp.li_attr.id = tid;
  2196. }
  2197. if(d && typeof d.a_attr === 'object') {
  2198. for (i in d.a_attr) {
  2199. if(d.a_attr.hasOwnProperty(i)) {
  2200. tmp.a_attr[i] = d.a_attr[i];
  2201. }
  2202. }
  2203. }
  2204. if(d && d.children && d.children === true) {
  2205. tmp.state.loaded = false;
  2206. tmp.children = [];
  2207. tmp.children_d = [];
  2208. }
  2209. m[tmp.id] = tmp;
  2210. for(i = 0, j = tmp.children.length; i < j; i++) {
  2211. c = this._parse_model_from_flat_json(m[tmp.children[i]], tmp.id, ps);
  2212. e = m[c];
  2213. tmp.children_d.push(c);
  2214. if(e.children_d.length) {
  2215. tmp.children_d = tmp.children_d.concat(e.children_d);
  2216. }
  2217. }
  2218. delete d.data;
  2219. delete d.children;
  2220. m[tmp.id].original = d;
  2221. if(tmp.state.selected) {
  2222. this._data.core.selected.push(tmp.id);
  2223. }
  2224. return tmp.id;
  2225. },
  2226. /**
  2227. * parses a node from a JSON object and appends it to the in memory tree model. Used internally.
  2228. * @private
  2229. * @name _parse_model_from_json(d [, p, ps])
  2230. * @param {Object} d the JSON object to parse
  2231. * @param {String} p the parent ID
  2232. * @param {Array} ps list of all parents
  2233. * @return {String} the ID of the object added to the model
  2234. */
  2235. _parse_model_from_json : function (d, p, ps) {
  2236. if(!ps) { ps = []; }
  2237. else { ps = ps.concat(); }
  2238. if(p) { ps.unshift(p); }
  2239. var tid = false, i, j, c, e, m = this._model.data, df = this._model.default_state, tmp;
  2240. do {
  2241. tid = 'j' + this._id + '_' + (++this._cnt);
  2242. } while(m[tid]);
  2243. tmp = {
  2244. id : false,
  2245. text : typeof d === 'string' ? d : '',
  2246. icon : typeof d === 'object' && d.icon !== undefined ? d.icon : true,
  2247. parent : p,
  2248. parents : ps,
  2249. children : [],
  2250. children_d : [],
  2251. data : null,
  2252. state : { },
  2253. li_attr : { id : false },
  2254. a_attr : { href : '#' },
  2255. original : false
  2256. };
  2257. for(i in df) {
  2258. if(df.hasOwnProperty(i)) {
  2259. tmp.state[i] = df[i];
  2260. }
  2261. }
  2262. if(d && (d.id || d.id === 0)) { tmp.id = d.id.toString(); }
  2263. if(d && d.text) { tmp.text = d.text; }
  2264. if(d && d.data && d.data.jstree && d.data.jstree.icon) {
  2265. tmp.icon = d.data.jstree.icon;
  2266. }
  2267. if(tmp.icon === undefined || tmp.icon === null || tmp.icon === "") {
  2268. tmp.icon = true;
  2269. }
  2270. if(d && d.data) {
  2271. tmp.data = d.data;
  2272. if(d.data.jstree) {
  2273. for(i in d.data.jstree) {
  2274. if(d.data.jstree.hasOwnProperty(i)) {
  2275. tmp.state[i] = d.data.jstree[i];
  2276. }
  2277. }
  2278. }
  2279. }
  2280. if(d && typeof d.state === 'object') {
  2281. for (i in d.state) {
  2282. if(d.state.hasOwnProperty(i)) {
  2283. tmp.state[i] = d.state[i];
  2284. }
  2285. }
  2286. }
  2287. if(d && typeof d.li_attr === 'object') {
  2288. for (i in d.li_attr) {
  2289. if(d.li_attr.hasOwnProperty(i)) {
  2290. tmp.li_attr[i] = d.li_attr[i];
  2291. }
  2292. }
  2293. }
  2294. if(tmp.li_attr.id && !(tmp.id || tmp.id === 0)) {
  2295. tmp.id = tmp.li_attr.id.toString();
  2296. }
  2297. if(!(tmp.id || tmp.id === 0)) {
  2298. tmp.id = tid;
  2299. }
  2300. if(!tmp.li_attr.id) {
  2301. tmp.li_attr.id = tmp.id;
  2302. }
  2303. if(d && typeof d.a_attr === 'object') {
  2304. for (i in d.a_attr) {
  2305. if(d.a_attr.hasOwnProperty(i)) {
  2306. tmp.a_attr[i] = d.a_attr[i];
  2307. }
  2308. }
  2309. }
  2310. if(d && d.children && d.children.length) {
  2311. for(i = 0, j = d.children.length; i < j; i++) {
  2312. c = this._parse_model_from_json(d.children[i], tmp.id, ps);
  2313. e = m[c];
  2314. tmp.children.push(c);
  2315. if(e.children_d.length) {
  2316. tmp.children_d = tmp.children_d.concat(e.children_d);
  2317. }
  2318. }
  2319. tmp.children_d = tmp.children.concat(tmp.children_d);
  2320. }
  2321. if(d && d.children && d.children === true) {
  2322. tmp.state.loaded = false;
  2323. tmp.children = [];
  2324. tmp.children_d = [];
  2325. }
  2326. delete d.data;
  2327. delete d.children;
  2328. tmp.original = d;
  2329. m[tmp.id] = tmp;
  2330. if(tmp.state.selected) {
  2331. this._data.core.selected.push(tmp.id);
  2332. }
  2333. return tmp.id;
  2334. },
  2335. /**
  2336. * redraws all nodes that need to be redrawn. Used internally.
  2337. * @private
  2338. * @name _redraw()
  2339. * @trigger redraw.jstree
  2340. */
  2341. _redraw : function () {
  2342. var nodes = this._model.force_full_redraw ? this._model.data[$.jstree.root].children.concat([]) : this._model.changed.concat([]),
  2343. f = document.createElement('UL'), tmp, i, j, fe = this._data.core.focused;
  2344. for(i = 0, j = nodes.length; i < j; i++) {
  2345. tmp = this.redraw_node(nodes[i], true, this._model.force_full_redraw);
  2346. if(tmp && this._model.force_full_redraw) {
  2347. f.appendChild(tmp);
  2348. }
  2349. }
  2350. if(this._model.force_full_redraw) {
  2351. f.className = this.get_container_ul()[0].className;
  2352. f.setAttribute('role','presentation');
  2353. this.element.empty().append(f);
  2354. //this.get_container_ul()[0].appendChild(f);
  2355. }
  2356. if(fe !== null && this.settings.core.restore_focus) {
  2357. tmp = this.get_node(fe, true);
  2358. if(tmp && tmp.length && tmp.children('.jstree-anchor')[0] !== document.activeElement) {
  2359. tmp.children('.jstree-anchor').trigger('focus');
  2360. }
  2361. else {
  2362. this._data.core.focused = null;
  2363. }
  2364. }
  2365. this._model.force_full_redraw = false;
  2366. this._model.changed = [];
  2367. /**
  2368. * triggered after nodes are redrawn
  2369. * @event
  2370. * @name redraw.jstree
  2371. * @param {array} nodes the redrawn nodes
  2372. */
  2373. this.trigger('redraw', { "nodes" : nodes });
  2374. },
  2375. /**
  2376. * redraws all nodes that need to be redrawn or optionally - the whole tree
  2377. * @name redraw([full])
  2378. * @param {Boolean} full if set to `true` all nodes are redrawn.
  2379. */
  2380. redraw : function (full) {
  2381. if(full) {
  2382. this._model.force_full_redraw = true;
  2383. }
  2384. //if(this._model.redraw_timeout) {
  2385. // clearTimeout(this._model.redraw_timeout);
  2386. //}
  2387. //this._model.redraw_timeout = setTimeout($.proxy(this._redraw, this),0);
  2388. this._redraw();
  2389. },
  2390. /**
  2391. * redraws a single node's children. Used internally.
  2392. * @private
  2393. * @name draw_children(node)
  2394. * @param {mixed} node the node whose children will be redrawn
  2395. */
  2396. draw_children : function (node) {
  2397. var obj = this.get_node(node),
  2398. i = false,
  2399. j = false,
  2400. k = false,
  2401. d = document;
  2402. if(!obj) { return false; }
  2403. if(obj.id === $.jstree.root) { return this.redraw(true); }
  2404. node = this.get_node(node, true);
  2405. if(!node || !node.length) { return false; } // TODO: quick toggle
  2406. node.children('.jstree-children').remove();
  2407. node = node[0];
  2408. if(obj.children.length && obj.state.loaded) {
  2409. k = d.createElement('UL');
  2410. k.setAttribute('role', 'group');
  2411. k.className = 'jstree-children';
  2412. for(i = 0, j = obj.children.length; i < j; i++) {
  2413. k.appendChild(this.redraw_node(obj.children[i], true, true));
  2414. }
  2415. node.appendChild(k);
  2416. }
  2417. },
  2418. /**
  2419. * redraws a single node. Used internally.
  2420. * @private
  2421. * @name redraw_node(node, deep, is_callback, force_render)
  2422. * @param {mixed} node the node to redraw
  2423. * @param {Boolean} deep should child nodes be redrawn too
  2424. * @param {Boolean} is_callback is this a recursion call
  2425. * @param {Boolean} force_render should children of closed parents be drawn anyway
  2426. */
  2427. redraw_node : function (node, deep, is_callback, force_render) {
  2428. var obj = this.get_node(node),
  2429. par = false,
  2430. ind = false,
  2431. old = false,
  2432. i = false,
  2433. j = false,
  2434. k = false,
  2435. c = '',
  2436. d = document,
  2437. m = this._model.data,
  2438. f = false,
  2439. s = false,
  2440. tmp = null,
  2441. t = 0,
  2442. l = 0,
  2443. has_children = false,
  2444. last_sibling = false;
  2445. if(!obj) { return false; }
  2446. if(obj.id === $.jstree.root) { return this.redraw(true); }
  2447. deep = deep || obj.children.length === 0;
  2448. 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);
  2449. if(!node) {
  2450. deep = true;
  2451. //node = d.createElement('LI');
  2452. if(!is_callback) {
  2453. par = obj.parent !== $.jstree.root ? $('#' + obj.parent.replace($.jstree.idregex,'\\$&'), this.element)[0] : null;
  2454. if(par !== null && (!par || !m[obj.parent].state.opened)) {
  2455. return false;
  2456. }
  2457. ind = $.inArray(obj.id, par === null ? m[$.jstree.root].children : m[obj.parent].children);
  2458. }
  2459. }
  2460. else {
  2461. node = $(node);
  2462. if(!is_callback) {
  2463. par = node.parent().parent()[0];
  2464. if(par === this.element[0]) {
  2465. par = null;
  2466. }
  2467. ind = node.index();
  2468. }
  2469. // m[obj.id].data = node.data(); // use only node's data, no need to touch jquery storage
  2470. if(!deep && obj.children.length && !node.children('.jstree-children').length) {
  2471. deep = true;
  2472. }
  2473. if(!deep) {
  2474. old = node.children('.jstree-children')[0];
  2475. }
  2476. f = node.children('.jstree-anchor')[0] === document.activeElement;
  2477. node.remove();
  2478. //node = d.createElement('LI');
  2479. //node = node[0];
  2480. }
  2481. node = this._data.core.node.cloneNode(true);
  2482. // node is DOM, deep is boolean
  2483. c = 'jstree-node ';
  2484. for(i in obj.li_attr) {
  2485. if(obj.li_attr.hasOwnProperty(i)) {
  2486. if(i === 'id') { continue; }
  2487. if(i !== 'class') {
  2488. node.setAttribute(i, obj.li_attr[i]);
  2489. }
  2490. else {
  2491. c += obj.li_attr[i];
  2492. }
  2493. }
  2494. }
  2495. if(!obj.a_attr.id) {
  2496. obj.a_attr.id = obj.id + '_anchor';
  2497. }
  2498. node.childNodes[1].setAttribute('aria-selected', !!obj.state.selected);
  2499. node.childNodes[1].setAttribute('aria-level', obj.parents.length);
  2500. if(this.settings.core.compute_elements_positions) {
  2501. node.childNodes[1].setAttribute('aria-setsize', m[obj.parent].children.length);
  2502. node.childNodes[1].setAttribute('aria-posinset', m[obj.parent].children.indexOf(obj.id) + 1);
  2503. }
  2504. if(obj.state.disabled) {
  2505. node.childNodes[1].setAttribute('aria-disabled', true);
  2506. }
  2507. for(i = 0, j = obj.children.length; i < j; i++) {
  2508. if(!m[obj.children[i]].state.hidden) {
  2509. has_children = true;
  2510. break;
  2511. }
  2512. }
  2513. if(obj.parent !== null && m[obj.parent] && !obj.state.hidden) {
  2514. i = $.inArray(obj.id, m[obj.parent].children);
  2515. last_sibling = obj.id;
  2516. if(i !== -1) {
  2517. i++;
  2518. for(j = m[obj.parent].children.length; i < j; i++) {
  2519. if(!m[m[obj.parent].children[i]].state.hidden) {
  2520. last_sibling = m[obj.parent].children[i];
  2521. }
  2522. if(last_sibling !== obj.id) {
  2523. break;
  2524. }
  2525. }
  2526. }
  2527. }
  2528. if(obj.state.hidden) {
  2529. c += ' jstree-hidden';
  2530. }
  2531. if (obj.state.loading) {
  2532. c += ' jstree-loading';
  2533. }
  2534. if(obj.state.loaded && !has_children) {
  2535. c += ' jstree-leaf';
  2536. }
  2537. else {
  2538. c += obj.state.opened && obj.state.loaded ? ' jstree-open' : ' jstree-closed';
  2539. node.childNodes[1].setAttribute('aria-expanded', (obj.state.opened && obj.state.loaded) );
  2540. }
  2541. if(last_sibling === obj.id) {
  2542. c += ' jstree-last';
  2543. }
  2544. node.id = obj.id;
  2545. node.className = c;
  2546. c = ( obj.state.selected ? ' jstree-clicked' : '') + ( obj.state.disabled ? ' jstree-disabled' : '');
  2547. for(j in obj.a_attr) {
  2548. if(obj.a_attr.hasOwnProperty(j)) {
  2549. if(j === 'href' && obj.a_attr[j] === '#') { continue; }
  2550. if(j !== 'class') {
  2551. node.childNodes[1].setAttribute(j, obj.a_attr[j]);
  2552. }
  2553. else {
  2554. c += ' ' + obj.a_attr[j];
  2555. }
  2556. }
  2557. }
  2558. if(c.length) {
  2559. node.childNodes[1].className = 'jstree-anchor ' + c;
  2560. }
  2561. if((obj.icon && obj.icon !== true) || obj.icon === false) {
  2562. if(obj.icon === false) {
  2563. node.childNodes[1].childNodes[0].className += ' jstree-themeicon-hidden';
  2564. }
  2565. else if(obj.icon.indexOf('/') === -1 && obj.icon.indexOf('.') === -1) {
  2566. node.childNodes[1].childNodes[0].className += ' ' + obj.icon + ' jstree-themeicon-custom';
  2567. }
  2568. else {
  2569. node.childNodes[1].childNodes[0].style.backgroundImage = 'url("'+obj.icon+'")';
  2570. node.childNodes[1].childNodes[0].style.backgroundPosition = 'center center';
  2571. node.childNodes[1].childNodes[0].style.backgroundSize = 'auto';
  2572. node.childNodes[1].childNodes[0].className += ' jstree-themeicon-custom';
  2573. }
  2574. }
  2575. if(this.settings.core.force_text) {
  2576. node.childNodes[1].appendChild(d.createTextNode(obj.text));
  2577. }
  2578. else {
  2579. node.childNodes[1].innerHTML += obj.text;
  2580. }
  2581. if(deep && obj.children.length && (obj.state.opened || force_render) && obj.state.loaded) {
  2582. k = d.createElement('UL');
  2583. k.setAttribute('role', 'group');
  2584. k.className = 'jstree-children';
  2585. for(i = 0, j = obj.children.length; i < j; i++) {
  2586. k.appendChild(this.redraw_node(obj.children[i], deep, true));
  2587. }
  2588. node.appendChild(k);
  2589. }
  2590. if(old) {
  2591. node.appendChild(old);
  2592. }
  2593. if(!is_callback) {
  2594. // append back using par / ind
  2595. if(!par) {
  2596. par = this.element[0];
  2597. }
  2598. for(i = 0, j = par.childNodes.length; i < j; i++) {
  2599. if(par.childNodes[i] && par.childNodes[i].className && par.childNodes[i].className.indexOf('jstree-children') !== -1) {
  2600. tmp = par.childNodes[i];
  2601. break;
  2602. }
  2603. }
  2604. if(!tmp) {
  2605. tmp = d.createElement('UL');
  2606. tmp.setAttribute('role', 'group');
  2607. tmp.className = 'jstree-children';
  2608. par.appendChild(tmp);
  2609. }
  2610. par = tmp;
  2611. if(ind < par.childNodes.length) {
  2612. par.insertBefore(node, par.childNodes[ind]);
  2613. }
  2614. else {
  2615. par.appendChild(node);
  2616. }
  2617. if(f) {
  2618. t = this.element[0].scrollTop;
  2619. l = this.element[0].scrollLeft;
  2620. node.childNodes[1].focus();
  2621. this.element[0].scrollTop = t;
  2622. this.element[0].scrollLeft = l;
  2623. }
  2624. }
  2625. if(obj.state.opened && !obj.state.loaded) {
  2626. obj.state.opened = false;
  2627. setTimeout(function () {
  2628. this.open_node(obj.id, false, 0);
  2629. }.bind(this), 0);
  2630. }
  2631. return node;
  2632. },
  2633. /**
  2634. * opens a node, revealing its children. If the node is not loaded it will be loaded and opened once ready.
  2635. * @name open_node(obj [, callback, animation])
  2636. * @param {mixed} obj the node to open
  2637. * @param {Function} callback a function to execute once the node is opened
  2638. * @param {Number} animation the animation duration in milliseconds when opening the node (overrides the `core.animation` setting). Use `false` for no animation.
  2639. * @trigger open_node.jstree, after_open.jstree, before_open.jstree
  2640. */
  2641. open_node : function (obj, callback, animation) {
  2642. var t1, t2, d, t;
  2643. if($.vakata.is_array(obj)) {
  2644. obj = obj.slice();
  2645. for(t1 = 0, t2 = obj.length; t1 < t2; t1++) {
  2646. this.open_node(obj[t1], callback, animation);
  2647. }
  2648. return true;
  2649. }
  2650. obj = this.get_node(obj);
  2651. if(!obj || obj.id === $.jstree.root) {
  2652. return false;
  2653. }
  2654. animation = animation === undefined ? this.settings.core.animation : animation;
  2655. if(!this.is_closed(obj)) {
  2656. if(callback) {
  2657. callback.call(this, obj, false);
  2658. }
  2659. return false;
  2660. }
  2661. if(!this.is_loaded(obj)) {
  2662. if(this.is_loading(obj)) {
  2663. return setTimeout(function () {
  2664. this.open_node(obj, callback, animation);
  2665. }.bind(this), 500);
  2666. }
  2667. this.load_node(obj, function (o, ok) {
  2668. return ok ? this.open_node(o, callback, animation) : (callback ? callback.call(this, o, false) : false);
  2669. });
  2670. }
  2671. else {
  2672. d = this.get_node(obj, true);
  2673. t = this;
  2674. if(d.length) {
  2675. if(animation && d.children(".jstree-children").length) {
  2676. d.children(".jstree-children").stop(true, true);
  2677. }
  2678. if(obj.children.length && !this._firstChild(d.children('.jstree-children')[0])) {
  2679. this.draw_children(obj);
  2680. //d = this.get_node(obj, true);
  2681. }
  2682. if(!animation) {
  2683. this.trigger('before_open', { "node" : obj });
  2684. d[0].className = d[0].className.replace('jstree-closed', 'jstree-open');
  2685. d[0].childNodes[1].setAttribute("aria-expanded", true);
  2686. }
  2687. else {
  2688. this.trigger('before_open', { "node" : obj });
  2689. d
  2690. .children(".jstree-children").css("display","none").end()
  2691. .removeClass("jstree-closed").addClass("jstree-open")
  2692. .children('.jstree-anchor').attr("aria-expanded", true).end()
  2693. .children(".jstree-children").stop(true, true)
  2694. .slideDown(animation, function () {
  2695. this.style.display = "";
  2696. if (t.element) {
  2697. t.trigger("after_open", { "node" : obj });
  2698. }
  2699. });
  2700. }
  2701. }
  2702. obj.state.opened = true;
  2703. if(callback) {
  2704. callback.call(this, obj, true);
  2705. }
  2706. if(!d.length) {
  2707. /**
  2708. * 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)
  2709. * @event
  2710. * @name before_open.jstree
  2711. * @param {Object} node the opened node
  2712. */
  2713. this.trigger('before_open', { "node" : obj });
  2714. }
  2715. /**
  2716. * triggered when a node is opened (if there is an animation it will not be completed yet)
  2717. * @event
  2718. * @name open_node.jstree
  2719. * @param {Object} node the opened node
  2720. */
  2721. this.trigger('open_node', { "node" : obj });
  2722. if(!animation || !d.length) {
  2723. /**
  2724. * triggered when a node is opened and the animation is complete
  2725. * @event
  2726. * @name after_open.jstree
  2727. * @param {Object} node the opened node
  2728. */
  2729. this.trigger("after_open", { "node" : obj });
  2730. }
  2731. return true;
  2732. }
  2733. },
  2734. /**
  2735. * opens every parent of a node (node should be loaded)
  2736. * @name _open_to(obj)
  2737. * @param {mixed} obj the node to reveal
  2738. * @private
  2739. */
  2740. _open_to : function (obj) {
  2741. obj = this.get_node(obj);
  2742. if(!obj || obj.id === $.jstree.root) {
  2743. return false;
  2744. }
  2745. var i, j, p = obj.parents;
  2746. for(i = 0, j = p.length; i < j; i+=1) {
  2747. if(i !== $.jstree.root) {
  2748. this.open_node(p[i], false, 0);
  2749. }
  2750. }
  2751. return $('#' + obj.id.replace($.jstree.idregex,'\\$&'), this.element);
  2752. },
  2753. /**
  2754. * closes a node, hiding its children
  2755. * @name close_node(obj [, animation])
  2756. * @param {mixed} obj the node to close
  2757. * @param {Number} animation the animation duration in milliseconds when closing the node (overrides the `core.animation` setting). Use `false` for no animation.
  2758. * @trigger close_node.jstree, after_close.jstree
  2759. */
  2760. close_node : function (obj, animation) {
  2761. var t1, t2, t, d;
  2762. if($.vakata.is_array(obj)) {
  2763. obj = obj.slice();
  2764. for(t1 = 0, t2 = obj.length; t1 < t2; t1++) {
  2765. this.close_node(obj[t1], animation);
  2766. }
  2767. return true;
  2768. }
  2769. obj = this.get_node(obj);
  2770. if(!obj || obj.id === $.jstree.root) {
  2771. return false;
  2772. }
  2773. if(this.is_closed(obj)) {
  2774. return false;
  2775. }
  2776. animation = animation === undefined ? this.settings.core.animation : animation;
  2777. t = this;
  2778. d = this.get_node(obj, true);
  2779. obj.state.opened = false;
  2780. /**
  2781. * triggered when a node is closed (if there is an animation it will not be complete yet)
  2782. * @event
  2783. * @name close_node.jstree
  2784. * @param {Object} node the closed node
  2785. */
  2786. this.trigger('close_node',{ "node" : obj });
  2787. if(!d.length) {
  2788. /**
  2789. * triggered when a node is closed and the animation is complete
  2790. * @event
  2791. * @name after_close.jstree
  2792. * @param {Object} node the closed node
  2793. */
  2794. this.trigger("after_close", { "node" : obj });
  2795. }
  2796. else {
  2797. if(!animation) {
  2798. d[0].className = d[0].className.replace('jstree-open', 'jstree-closed');
  2799. d.children('.jstree-anchor').attr("aria-expanded", false);
  2800. d.children('.jstree-children').remove();
  2801. this.trigger("after_close", { "node" : obj });
  2802. }
  2803. else {
  2804. d
  2805. .children(".jstree-children").attr("style","display:block !important").end()
  2806. .removeClass("jstree-open").addClass("jstree-closed")
  2807. .children('.jstree-anchor').attr("aria-expanded", false).end()
  2808. .children(".jstree-children").stop(true, true).slideUp(animation, function () {
  2809. this.style.display = "";
  2810. d.children('.jstree-children').remove();
  2811. if (t.element) {
  2812. t.trigger("after_close", { "node" : obj });
  2813. }
  2814. });
  2815. }
  2816. }
  2817. },
  2818. /**
  2819. * toggles a node - closing it if it is open, opening it if it is closed
  2820. * @name toggle_node(obj)
  2821. * @param {mixed} obj the node to toggle
  2822. */
  2823. toggle_node : function (obj) {
  2824. var t1, t2;
  2825. if($.vakata.is_array(obj)) {
  2826. obj = obj.slice();
  2827. for(t1 = 0, t2 = obj.length; t1 < t2; t1++) {
  2828. this.toggle_node(obj[t1]);
  2829. }
  2830. return true;
  2831. }
  2832. if(this.is_closed(obj)) {
  2833. return this.open_node(obj);
  2834. }
  2835. if(this.is_open(obj)) {
  2836. return this.close_node(obj);
  2837. }
  2838. },
  2839. /**
  2840. * 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.
  2841. * @name open_all([obj, animation, original_obj])
  2842. * @param {mixed} obj the node to open recursively, omit to open all nodes in the tree
  2843. * @param {Number} animation the animation duration in milliseconds when opening the nodes, the default is no animation
  2844. * @param {jQuery} reference to the node that started the process (internal use)
  2845. * @trigger open_all.jstree
  2846. */
  2847. open_all : function (obj, animation, original_obj) {
  2848. if(!obj) { obj = $.jstree.root; }
  2849. obj = this.get_node(obj);
  2850. if(!obj) { return false; }
  2851. var dom = obj.id === $.jstree.root ? this.get_container_ul() : this.get_node(obj, true), i, j, _this;
  2852. if(!dom.length) {
  2853. for(i = 0, j = obj.children_d.length; i < j; i++) {
  2854. if(this.is_closed(this._model.data[obj.children_d[i]])) {
  2855. this._model.data[obj.children_d[i]].state.opened = true;
  2856. }
  2857. }
  2858. return this.trigger('open_all', { "node" : obj });
  2859. }
  2860. original_obj = original_obj || dom;
  2861. _this = this;
  2862. dom = this.is_closed(obj) ? dom.find('.jstree-closed').addBack() : dom.find('.jstree-closed');
  2863. dom.each(function () {
  2864. _this.open_node(
  2865. this,
  2866. function(node, status) { if(status && this.is_parent(node)) { this.open_all(node, animation, original_obj); } },
  2867. animation || 0
  2868. );
  2869. });
  2870. if(original_obj.find('.jstree-closed').length === 0) {
  2871. /**
  2872. * triggered when an `open_all` call completes
  2873. * @event
  2874. * @name open_all.jstree
  2875. * @param {Object} node the opened node
  2876. */
  2877. this.trigger('open_all', { "node" : this.get_node(original_obj) });
  2878. }
  2879. },
  2880. /**
  2881. * closes all nodes within a node (or the tree), revealing their children
  2882. * @name close_all([obj, animation])
  2883. * @param {mixed} obj the node to close recursively, omit to close all nodes in the tree
  2884. * @param {Number} animation the animation duration in milliseconds when closing the nodes, the default is no animation
  2885. * @trigger close_all.jstree
  2886. */
  2887. close_all : function (obj, animation) {
  2888. if(!obj) { obj = $.jstree.root; }
  2889. obj = this.get_node(obj);
  2890. if(!obj) { return false; }
  2891. var dom = obj.id === $.jstree.root ? this.get_container_ul() : this.get_node(obj, true),
  2892. _this = this, i, j;
  2893. if(dom.length) {
  2894. dom = this.is_open(obj) ? dom.find('.jstree-open').addBack() : dom.find('.jstree-open');
  2895. $(dom.get().reverse()).each(function () { _this.close_node(this, animation || 0); });
  2896. }
  2897. for(i = 0, j = obj.children_d.length; i < j; i++) {
  2898. this._model.data[obj.children_d[i]].state.opened = false;
  2899. }
  2900. /**
  2901. * triggered when an `close_all` call completes
  2902. * @event
  2903. * @name close_all.jstree
  2904. * @param {Object} node the closed node
  2905. */
  2906. this.trigger('close_all', { "node" : obj });
  2907. },
  2908. /**
  2909. * checks if a node is disabled (not selectable)
  2910. * @name is_disabled(obj)
  2911. * @param {mixed} obj
  2912. * @return {Boolean}
  2913. */
  2914. is_disabled : function (obj) {
  2915. obj = this.get_node(obj);
  2916. return obj && obj.state && obj.state.disabled;
  2917. },
  2918. /**
  2919. * enables a node - so that it can be selected
  2920. * @name enable_node(obj)
  2921. * @param {mixed} obj the node to enable
  2922. * @trigger enable_node.jstree
  2923. */
  2924. enable_node : function (obj) {
  2925. var t1, t2;
  2926. if($.vakata.is_array(obj)) {
  2927. obj = obj.slice();
  2928. for(t1 = 0, t2 = obj.length; t1 < t2; t1++) {
  2929. this.enable_node(obj[t1]);
  2930. }
  2931. return true;
  2932. }
  2933. obj = this.get_node(obj);
  2934. if(!obj || obj.id === $.jstree.root) {
  2935. return false;
  2936. }
  2937. obj.state.disabled = false;
  2938. this.get_node(obj,true).children('.jstree-anchor').removeClass('jstree-disabled').attr('aria-disabled', false);
  2939. /**
  2940. * triggered when an node is enabled
  2941. * @event
  2942. * @name enable_node.jstree
  2943. * @param {Object} node the enabled node
  2944. */
  2945. this.trigger('enable_node', { 'node' : obj });
  2946. },
  2947. /**
  2948. * disables a node - so that it can not be selected
  2949. * @name disable_node(obj)
  2950. * @param {mixed} obj the node to disable
  2951. * @trigger disable_node.jstree
  2952. */
  2953. disable_node : function (obj) {
  2954. var t1, t2;
  2955. if($.vakata.is_array(obj)) {
  2956. obj = obj.slice();
  2957. for(t1 = 0, t2 = obj.length; t1 < t2; t1++) {
  2958. this.disable_node(obj[t1]);
  2959. }
  2960. return true;
  2961. }
  2962. obj = this.get_node(obj);
  2963. if(!obj || obj.id === $.jstree.root) {
  2964. return false;
  2965. }
  2966. obj.state.disabled = true;
  2967. this.get_node(obj,true).children('.jstree-anchor').addClass('jstree-disabled').attr('aria-disabled', true);
  2968. /**
  2969. * triggered when an node is disabled
  2970. * @event
  2971. * @name disable_node.jstree
  2972. * @param {Object} node the disabled node
  2973. */
  2974. this.trigger('disable_node', { 'node' : obj });
  2975. },
  2976. /**
  2977. * determines if a node is hidden
  2978. * @name is_hidden(obj)
  2979. * @param {mixed} obj the node
  2980. */
  2981. is_hidden : function (obj) {
  2982. obj = this.get_node(obj);
  2983. return obj.state.hidden === true;
  2984. },
  2985. /**
  2986. * hides a node - it is still in the structure but will not be visible
  2987. * @name hide_node(obj)
  2988. * @param {mixed} obj the node to hide
  2989. * @param {Boolean} skip_redraw internal parameter controlling if redraw is called
  2990. * @trigger hide_node.jstree
  2991. */
  2992. hide_node : function (obj, skip_redraw) {
  2993. var t1, t2;
  2994. if($.vakata.is_array(obj)) {
  2995. obj = obj.slice();
  2996. for(t1 = 0, t2 = obj.length; t1 < t2; t1++) {
  2997. this.hide_node(obj[t1], true);
  2998. }
  2999. if (!skip_redraw) {
  3000. this.redraw();
  3001. }
  3002. return true;
  3003. }
  3004. obj = this.get_node(obj);
  3005. if(!obj || obj.id === $.jstree.root) {
  3006. return false;
  3007. }
  3008. if(!obj.state.hidden) {
  3009. obj.state.hidden = true;
  3010. this._node_changed(obj.parent);
  3011. if(!skip_redraw) {
  3012. this.redraw();
  3013. }
  3014. /**
  3015. * triggered when an node is hidden
  3016. * @event
  3017. * @name hide_node.jstree
  3018. * @param {Object} node the hidden node
  3019. */
  3020. this.trigger('hide_node', { 'node' : obj });
  3021. }
  3022. },
  3023. /**
  3024. * shows a node
  3025. * @name show_node(obj)
  3026. * @param {mixed} obj the node to show
  3027. * @param {Boolean} skip_redraw internal parameter controlling if redraw is called
  3028. * @trigger show_node.jstree
  3029. */
  3030. show_node : function (obj, skip_redraw) {
  3031. var t1, t2;
  3032. if($.vakata.is_array(obj)) {
  3033. obj = obj.slice();
  3034. for(t1 = 0, t2 = obj.length; t1 < t2; t1++) {
  3035. this.show_node(obj[t1], true);
  3036. }
  3037. if (!skip_redraw) {
  3038. this.redraw();
  3039. }
  3040. return true;
  3041. }
  3042. obj = this.get_node(obj);
  3043. if(!obj || obj.id === $.jstree.root) {
  3044. return false;
  3045. }
  3046. if(obj.state.hidden) {
  3047. obj.state.hidden = false;
  3048. this._node_changed(obj.parent);
  3049. if(!skip_redraw) {
  3050. this.redraw();
  3051. }
  3052. /**
  3053. * triggered when an node is shown
  3054. * @event
  3055. * @name show_node.jstree
  3056. * @param {Object} node the shown node
  3057. */
  3058. this.trigger('show_node', { 'node' : obj });
  3059. }
  3060. },
  3061. /**
  3062. * hides all nodes
  3063. * @name hide_all()
  3064. * @trigger hide_all.jstree
  3065. */
  3066. hide_all : function (skip_redraw) {
  3067. var i, m = this._model.data, ids = [];
  3068. for(i in m) {
  3069. if(m.hasOwnProperty(i) && i !== $.jstree.root && !m[i].state.hidden) {
  3070. m[i].state.hidden = true;
  3071. ids.push(i);
  3072. }
  3073. }
  3074. this._model.force_full_redraw = true;
  3075. if(!skip_redraw) {
  3076. this.redraw();
  3077. }
  3078. /**
  3079. * triggered when all nodes are hidden
  3080. * @event
  3081. * @name hide_all.jstree
  3082. * @param {Array} nodes the IDs of all hidden nodes
  3083. */
  3084. this.trigger('hide_all', { 'nodes' : ids });
  3085. return ids;
  3086. },
  3087. /**
  3088. * shows all nodes
  3089. * @name show_all()
  3090. * @trigger show_all.jstree
  3091. */
  3092. show_all : function (skip_redraw) {
  3093. var i, m = this._model.data, ids = [];
  3094. for(i in m) {
  3095. if(m.hasOwnProperty(i) && i !== $.jstree.root && m[i].state.hidden) {
  3096. m[i].state.hidden = false;
  3097. ids.push(i);
  3098. }
  3099. }
  3100. this._model.force_full_redraw = true;
  3101. if(!skip_redraw) {
  3102. this.redraw();
  3103. }
  3104. /**
  3105. * triggered when all nodes are shown
  3106. * @event
  3107. * @name show_all.jstree
  3108. * @param {Array} nodes the IDs of all shown nodes
  3109. */
  3110. this.trigger('show_all', { 'nodes' : ids });
  3111. return ids;
  3112. },
  3113. /**
  3114. * called when a node is selected by the user. Used internally.
  3115. * @private
  3116. * @name activate_node(obj, e)
  3117. * @param {mixed} obj the node
  3118. * @param {Object} e the related event
  3119. * @trigger activate_node.jstree, changed.jstree
  3120. */
  3121. activate_node : function (obj, e) {
  3122. if(this.is_disabled(obj)) {
  3123. return false;
  3124. }
  3125. if(!e || typeof e !== 'object') {
  3126. e = {};
  3127. }
  3128. // 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
  3129. 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;
  3130. if(this._data.core.last_clicked && !this._data.core.last_clicked.state.selected) { this._data.core.last_clicked = null; }
  3131. 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]); }
  3132. 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 ) )) {
  3133. if(!this.settings.core.multiple && (e.metaKey || e.ctrlKey || e.shiftKey) && this.is_selected(obj)) {
  3134. this.deselect_node(obj, false, e);
  3135. }
  3136. else {
  3137. if (!this.is_selected(obj) || this._data.core.selected.length !== 1) {
  3138. this.deselect_all(true);
  3139. this.select_node(obj, false, false, e);
  3140. }
  3141. this._data.core.last_clicked = this.get_node(obj);
  3142. }
  3143. }
  3144. else {
  3145. if(e.shiftKey) {
  3146. var o = this.get_node(obj).id,
  3147. l = this._data.core.last_clicked.id,
  3148. p = this.get_node(this._data.core.last_clicked.parent).children,
  3149. c = false,
  3150. i, j;
  3151. for(i = 0, j = p.length; i < j; i += 1) {
  3152. // separate IFs work whem o and l are the same
  3153. if(p[i] === o) {
  3154. c = !c;
  3155. }
  3156. if(p[i] === l) {
  3157. c = !c;
  3158. }
  3159. if(!this.is_disabled(p[i]) && (c || p[i] === o || p[i] === l)) {
  3160. if (!this.is_hidden(p[i])) {
  3161. this.select_node(p[i], true, false, e);
  3162. }
  3163. }
  3164. else {
  3165. if (!e.ctrlKey) {
  3166. this.deselect_node(p[i], true, e);
  3167. }
  3168. }
  3169. }
  3170. this.trigger('changed', { 'action' : 'select_node', 'node' : this.get_node(obj), 'selected' : this._data.core.selected, 'event' : e });
  3171. }
  3172. else {
  3173. if(!this.is_selected(obj)) {
  3174. if (e.ctrlKey) {
  3175. this._data.core.last_clicked = this.get_node(obj);
  3176. }
  3177. this.select_node(obj, false, false, e);
  3178. }
  3179. else {
  3180. this.deselect_node(obj, false, e);
  3181. }
  3182. }
  3183. }
  3184. /**
  3185. * triggered when an node is clicked or intercated with by the user
  3186. * @event
  3187. * @name activate_node.jstree
  3188. * @param {Object} node
  3189. * @param {Object} event the ooriginal event (if any) which triggered the call (may be an empty object)
  3190. */
  3191. this.trigger('activate_node', { 'node' : this.get_node(obj), 'event' : e });
  3192. },
  3193. /**
  3194. * applies the hover state on a node, called when a node is hovered by the user. Used internally.
  3195. * @private
  3196. * @name hover_node(obj)
  3197. * @param {mixed} obj
  3198. * @trigger hover_node.jstree
  3199. */
  3200. hover_node : function (obj) {
  3201. obj = this.get_node(obj, true);
  3202. if(!obj || !obj.length || obj.children('.jstree-hovered').length) {
  3203. return false;
  3204. }
  3205. var o = this.element.find('.jstree-hovered'), t = this.element;
  3206. if(o && o.length) { this.dehover_node(o); }
  3207. obj.children('.jstree-anchor').addClass('jstree-hovered');
  3208. /**
  3209. * triggered when an node is hovered
  3210. * @event
  3211. * @name hover_node.jstree
  3212. * @param {Object} node
  3213. */
  3214. this.trigger('hover_node', { 'node' : this.get_node(obj) });
  3215. setTimeout(function () { t.attr('aria-activedescendant', obj[0].id); }, 0);
  3216. },
  3217. /**
  3218. * removes the hover state from a nodecalled when a node is no longer hovered by the user. Used internally.
  3219. * @private
  3220. * @name dehover_node(obj)
  3221. * @param {mixed} obj
  3222. * @trigger dehover_node.jstree
  3223. */
  3224. dehover_node : function (obj) {
  3225. obj = this.get_node(obj, true);
  3226. if(!obj || !obj.length || !obj.children('.jstree-hovered').length) {
  3227. return false;
  3228. }
  3229. obj.children('.jstree-anchor').removeClass('jstree-hovered');
  3230. /**
  3231. * triggered when an node is no longer hovered
  3232. * @event
  3233. * @name dehover_node.jstree
  3234. * @param {Object} node
  3235. */
  3236. this.trigger('dehover_node', { 'node' : this.get_node(obj) });
  3237. },
  3238. /**
  3239. * select a node
  3240. * @name select_node(obj [, supress_event, prevent_open])
  3241. * @param {mixed} obj an array can be used to select multiple nodes
  3242. * @param {Boolean} supress_event if set to `true` the `changed.jstree` event won't be triggered
  3243. * @param {Boolean} prevent_open if set to `true` parents of the selected node won't be opened
  3244. * @trigger select_node.jstree, changed.jstree
  3245. */
  3246. select_node : function (obj, supress_event, prevent_open, e) {
  3247. var dom, t1, t2, th;
  3248. if($.vakata.is_array(obj)) {
  3249. obj = obj.slice();
  3250. for(t1 = 0, t2 = obj.length; t1 < t2; t1++) {
  3251. this.select_node(obj[t1], supress_event, prevent_open, e);
  3252. }
  3253. return true;
  3254. }
  3255. obj = this.get_node(obj);
  3256. if(!obj || obj.id === $.jstree.root) {
  3257. return false;
  3258. }
  3259. dom = this.get_node(obj, true);
  3260. if(!obj.state.selected) {
  3261. obj.state.selected = true;
  3262. this._data.core.selected.push(obj.id);
  3263. if(!prevent_open) {
  3264. dom = this._open_to(obj);
  3265. }
  3266. if(dom && dom.length) {
  3267. dom.children('.jstree-anchor').addClass('jstree-clicked').attr('aria-selected', true);
  3268. }
  3269. /**
  3270. * triggered when an node is selected
  3271. * @event
  3272. * @name select_node.jstree
  3273. * @param {Object} node
  3274. * @param {Array} selected the current selection
  3275. * @param {Object} event the event (if any) that triggered this select_node
  3276. */
  3277. this.trigger('select_node', { 'node' : obj, 'selected' : this._data.core.selected, 'event' : e });
  3278. if(!supress_event) {
  3279. /**
  3280. * triggered when selection changes
  3281. * @event
  3282. * @name changed.jstree
  3283. * @param {Object} node
  3284. * @param {Object} action the action that caused the selection to change
  3285. * @param {Array} selected the current selection
  3286. * @param {Object} event the event (if any) that triggered this changed event
  3287. */
  3288. this.trigger('changed', { 'action' : 'select_node', 'node' : obj, 'selected' : this._data.core.selected, 'event' : e });
  3289. }
  3290. }
  3291. },
  3292. /**
  3293. * deselect a node
  3294. * @name deselect_node(obj [, supress_event])
  3295. * @param {mixed} obj an array can be used to deselect multiple nodes
  3296. * @param {Boolean} supress_event if set to `true` the `changed.jstree` event won't be triggered
  3297. * @trigger deselect_node.jstree, changed.jstree
  3298. */
  3299. deselect_node : function (obj, supress_event, e) {
  3300. var t1, t2, dom;
  3301. if($.vakata.is_array(obj)) {
  3302. obj = obj.slice();
  3303. for(t1 = 0, t2 = obj.length; t1 < t2; t1++) {
  3304. this.deselect_node(obj[t1], supress_event, e);
  3305. }
  3306. return true;
  3307. }
  3308. obj = this.get_node(obj);
  3309. if(!obj || obj.id === $.jstree.root) {
  3310. return false;
  3311. }
  3312. dom = this.get_node(obj, true);
  3313. if(obj.state.selected) {
  3314. obj.state.selected = false;
  3315. this._data.core.selected = $.vakata.array_remove_item(this._data.core.selected, obj.id);
  3316. if(dom.length) {
  3317. dom.children('.jstree-anchor').removeClass('jstree-clicked').attr('aria-selected', false);
  3318. }
  3319. /**
  3320. * triggered when an node is deselected
  3321. * @event
  3322. * @name deselect_node.jstree
  3323. * @param {Object} node
  3324. * @param {Array} selected the current selection
  3325. * @param {Object} event the event (if any) that triggered this deselect_node
  3326. */
  3327. this.trigger('deselect_node', { 'node' : obj, 'selected' : this._data.core.selected, 'event' : e });
  3328. if(!supress_event) {
  3329. this.trigger('changed', { 'action' : 'deselect_node', 'node' : obj, 'selected' : this._data.core.selected, 'event' : e });
  3330. }
  3331. }
  3332. },
  3333. /**
  3334. * select all nodes in the tree
  3335. * @name select_all([supress_event])
  3336. * @param {Boolean} supress_event if set to `true` the `changed.jstree` event won't be triggered
  3337. * @trigger select_all.jstree, changed.jstree
  3338. */
  3339. select_all : function (supress_event) {
  3340. var tmp = this._data.core.selected.concat([]), i, j;
  3341. this._data.core.selected = this._model.data[$.jstree.root].children_d.concat();
  3342. for(i = 0, j = this._data.core.selected.length; i < j; i++) {
  3343. if(this._model.data[this._data.core.selected[i]]) {
  3344. this._model.data[this._data.core.selected[i]].state.selected = true;
  3345. }
  3346. }
  3347. this.redraw(true);
  3348. /**
  3349. * triggered when all nodes are selected
  3350. * @event
  3351. * @name select_all.jstree
  3352. * @param {Array} selected the current selection
  3353. */
  3354. this.trigger('select_all', { 'selected' : this._data.core.selected });
  3355. if(!supress_event) {
  3356. this.trigger('changed', { 'action' : 'select_all', 'selected' : this._data.core.selected, 'old_selection' : tmp });
  3357. }
  3358. },
  3359. /**
  3360. * deselect all selected nodes
  3361. * @name deselect_all([supress_event])
  3362. * @param {Boolean} supress_event if set to `true` the `changed.jstree` event won't be triggered
  3363. * @trigger deselect_all.jstree, changed.jstree
  3364. */
  3365. deselect_all : function (supress_event) {
  3366. var tmp = this._data.core.selected.concat([]), i, j;
  3367. for(i = 0, j = this._data.core.selected.length; i < j; i++) {
  3368. if(this._model.data[this._data.core.selected[i]]) {
  3369. this._model.data[this._data.core.selected[i]].state.selected = false;
  3370. }
  3371. }
  3372. this._data.core.selected = [];
  3373. this.element.find('.jstree-clicked').removeClass('jstree-clicked').attr('aria-selected', false);
  3374. /**
  3375. * triggered when all nodes are deselected
  3376. * @event
  3377. * @name deselect_all.jstree
  3378. * @param {Object} node the previous selection
  3379. * @param {Array} selected the current selection
  3380. */
  3381. this.trigger('deselect_all', { 'selected' : this._data.core.selected, 'node' : tmp });
  3382. if(!supress_event) {
  3383. this.trigger('changed', { 'action' : 'deselect_all', 'selected' : this._data.core.selected, 'old_selection' : tmp });
  3384. }
  3385. },
  3386. /**
  3387. * checks if a node is selected
  3388. * @name is_selected(obj)
  3389. * @param {mixed} obj
  3390. * @return {Boolean}
  3391. */
  3392. is_selected : function (obj) {
  3393. obj = this.get_node(obj);
  3394. if(!obj || obj.id === $.jstree.root) {
  3395. return false;
  3396. }
  3397. return obj.state.selected;
  3398. },
  3399. /**
  3400. * get an array of all selected nodes
  3401. * @name get_selected([full])
  3402. * @param {mixed} full if set to `true` the returned array will consist of the full node objects, otherwise - only IDs will be returned
  3403. * @return {Array}
  3404. */
  3405. get_selected : function (full) {
  3406. return full ? $.map(this._data.core.selected, function (i) { return this.get_node(i); }.bind(this)) : this._data.core.selected.slice();
  3407. },
  3408. /**
  3409. * get an array of all top level selected nodes (ignoring children of selected nodes)
  3410. * @name get_top_selected([full])
  3411. * @param {mixed} full if set to `true` the returned array will consist of the full node objects, otherwise - only IDs will be returned
  3412. * @return {Array}
  3413. */
  3414. get_top_selected : function (full) {
  3415. var tmp = this.get_selected(true),
  3416. obj = {}, i, j, k, l;
  3417. for(i = 0, j = tmp.length; i < j; i++) {
  3418. obj[tmp[i].id] = tmp[i];
  3419. }
  3420. for(i = 0, j = tmp.length; i < j; i++) {
  3421. for(k = 0, l = tmp[i].children_d.length; k < l; k++) {
  3422. if(obj[tmp[i].children_d[k]]) {
  3423. delete obj[tmp[i].children_d[k]];
  3424. }
  3425. }
  3426. }
  3427. tmp = [];
  3428. for(i in obj) {
  3429. if(obj.hasOwnProperty(i)) {
  3430. tmp.push(i);
  3431. }
  3432. }
  3433. return full ? $.map(tmp, function (i) { return this.get_node(i); }.bind(this)) : tmp;
  3434. },
  3435. /**
  3436. * get an array of all bottom level selected nodes (ignoring selected parents)
  3437. * @name get_bottom_selected([full])
  3438. * @param {mixed} full if set to `true` the returned array will consist of the full node objects, otherwise - only IDs will be returned
  3439. * @return {Array}
  3440. */
  3441. get_bottom_selected : function (full) {
  3442. var tmp = this.get_selected(true),
  3443. obj = [], i, j;
  3444. for(i = 0, j = tmp.length; i < j; i++) {
  3445. if(!tmp[i].children.length) {
  3446. obj.push(tmp[i].id);
  3447. }
  3448. }
  3449. return full ? $.map(obj, function (i) { return this.get_node(i); }.bind(this)) : obj;
  3450. },
  3451. /**
  3452. * gets the current state of the tree so that it can be restored later with `set_state(state)`. Used internally.
  3453. * @name get_state()
  3454. * @private
  3455. * @return {Object}
  3456. */
  3457. get_state : function () {
  3458. var state = {
  3459. 'core' : {
  3460. 'open' : [],
  3461. 'loaded' : [],
  3462. 'scroll' : {
  3463. 'left' : this.element.scrollLeft(),
  3464. 'top' : this.element.scrollTop()
  3465. },
  3466. /*!
  3467. 'themes' : {
  3468. 'name' : this.get_theme(),
  3469. 'icons' : this._data.core.themes.icons,
  3470. 'dots' : this._data.core.themes.dots
  3471. },
  3472. */
  3473. 'selected' : []
  3474. }
  3475. }, i;
  3476. for(i in this._model.data) {
  3477. if(this._model.data.hasOwnProperty(i)) {
  3478. if(i !== $.jstree.root) {
  3479. if(this._model.data[i].state.loaded && this.settings.core.loaded_state) {
  3480. state.core.loaded.push(i);
  3481. }
  3482. if(this._model.data[i].state.opened) {
  3483. state.core.open.push(i);
  3484. }
  3485. if(this._model.data[i].state.selected) {
  3486. state.core.selected.push(i);
  3487. }
  3488. }
  3489. }
  3490. }
  3491. return state;
  3492. },
  3493. /**
  3494. * sets the state of the tree. Used internally.
  3495. * @name set_state(state [, callback])
  3496. * @private
  3497. * @param {Object} state the state to restore. Keep in mind this object is passed by reference and jstree will modify it.
  3498. * @param {Function} callback an optional function to execute once the state is restored.
  3499. * @trigger set_state.jstree
  3500. */
  3501. set_state : function (state, callback) {
  3502. if(state) {
  3503. if(state.core && state.core.selected && state.core.initial_selection === undefined) {
  3504. state.core.initial_selection = this._data.core.selected.concat([]).sort().join(',');
  3505. }
  3506. if(state.core) {
  3507. var res, n, t, _this, i;
  3508. if(state.core.loaded) {
  3509. if(!this.settings.core.loaded_state || !$.vakata.is_array(state.core.loaded) || !state.core.loaded.length) {
  3510. delete state.core.loaded;
  3511. this.set_state(state, callback);
  3512. }
  3513. else {
  3514. this._load_nodes(state.core.loaded, function (nodes) {
  3515. delete state.core.loaded;
  3516. this.set_state(state, callback);
  3517. });
  3518. }
  3519. return false;
  3520. }
  3521. if(state.core.open) {
  3522. if(!$.vakata.is_array(state.core.open) || !state.core.open.length) {
  3523. delete state.core.open;
  3524. this.set_state(state, callback);
  3525. }
  3526. else {
  3527. this._load_nodes(state.core.open, function (nodes) {
  3528. this.open_node(nodes, false, 0);
  3529. delete state.core.open;
  3530. this.set_state(state, callback);
  3531. });
  3532. }
  3533. return false;
  3534. }
  3535. if(state.core.scroll) {
  3536. if(state.core.scroll && state.core.scroll.left !== undefined) {
  3537. this.element.scrollLeft(state.core.scroll.left);
  3538. }
  3539. if(state.core.scroll && state.core.scroll.top !== undefined) {
  3540. this.element.scrollTop(state.core.scroll.top);
  3541. }
  3542. delete state.core.scroll;
  3543. this.set_state(state, callback);
  3544. return false;
  3545. }
  3546. if(state.core.selected) {
  3547. _this = this;
  3548. if (state.core.initial_selection === undefined ||
  3549. state.core.initial_selection === this._data.core.selected.concat([]).sort().join(',')
  3550. ) {
  3551. this.deselect_all();
  3552. $.each(state.core.selected, function (i, v) {
  3553. _this.select_node(v, false, true);
  3554. });
  3555. }
  3556. delete state.core.initial_selection;
  3557. delete state.core.selected;
  3558. this.set_state(state, callback);
  3559. return false;
  3560. }
  3561. for(i in state) {
  3562. if(state.hasOwnProperty(i) && i !== "core" && $.inArray(i, this.settings.plugins) === -1) {
  3563. delete state[i];
  3564. }
  3565. }
  3566. if($.isEmptyObject(state.core)) {
  3567. delete state.core;
  3568. this.set_state(state, callback);
  3569. return false;
  3570. }
  3571. }
  3572. if($.isEmptyObject(state)) {
  3573. state = null;
  3574. if(callback) { callback.call(this); }
  3575. /**
  3576. * triggered when a `set_state` call completes
  3577. * @event
  3578. * @name set_state.jstree
  3579. */
  3580. this.trigger('set_state');
  3581. return false;
  3582. }
  3583. return true;
  3584. }
  3585. return false;
  3586. },
  3587. /**
  3588. * refreshes the tree - all nodes are reloaded with calls to `load_node`.
  3589. * @name refresh()
  3590. * @param {Boolean} skip_loading an option to skip showing the loading indicator
  3591. * @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
  3592. * @trigger refresh.jstree
  3593. */
  3594. refresh : function (skip_loading, forget_state) {
  3595. this._data.core.state = forget_state === true ? {} : this.get_state();
  3596. if(forget_state && $.vakata.is_function(forget_state)) { this._data.core.state = forget_state.call(this, this._data.core.state); }
  3597. this._cnt = 0;
  3598. this._model.data = {};
  3599. this._model.data[$.jstree.root] = {
  3600. id : $.jstree.root,
  3601. parent : null,
  3602. parents : [],
  3603. children : [],
  3604. children_d : [],
  3605. state : { loaded : false }
  3606. };
  3607. this._data.core.selected = [];
  3608. this._data.core.last_clicked = null;
  3609. this._data.core.focused = null;
  3610. var c = this.get_container_ul()[0].className;
  3611. if(!skip_loading) {
  3612. 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>");
  3613. this.element.attr('aria-activedescendant','j'+this._id+'_loading');
  3614. }
  3615. this.load_node($.jstree.root, function (o, s) {
  3616. if(s) {
  3617. this.get_container_ul()[0].className = c;
  3618. if(this._firstChild(this.get_container_ul()[0])) {
  3619. this.element.attr('aria-activedescendant',this._firstChild(this.get_container_ul()[0]).id);
  3620. }
  3621. this.set_state($.extend(true, {}, this._data.core.state), function () {
  3622. /**
  3623. * triggered when a `refresh` call completes
  3624. * @event
  3625. * @name refresh.jstree
  3626. */
  3627. this.trigger('refresh');
  3628. });
  3629. }
  3630. this._data.core.state = null;
  3631. });
  3632. },
  3633. /**
  3634. * refreshes a node in the tree (reload its children) all opened nodes inside that node are reloaded with calls to `load_node`.
  3635. * @name refresh_node(obj)
  3636. * @param {mixed} obj the node
  3637. * @trigger refresh_node.jstree
  3638. */
  3639. refresh_node : function (obj) {
  3640. obj = this.get_node(obj);
  3641. if(!obj || obj.id === $.jstree.root) { return false; }
  3642. var opened = [], to_load = [], s = this._data.core.selected.concat([]);
  3643. to_load.push(obj.id);
  3644. if(obj.state.opened === true) { opened.push(obj.id); }
  3645. this.get_node(obj, true).find('.jstree-open').each(function() { to_load.push(this.id); opened.push(this.id); });
  3646. this._load_nodes(to_load, function (nodes) {
  3647. this.open_node(opened, false, 0);
  3648. this.select_node(s);
  3649. /**
  3650. * triggered when a node is refreshed
  3651. * @event
  3652. * @name refresh_node.jstree
  3653. * @param {Object} node - the refreshed node
  3654. * @param {Array} nodes - an array of the IDs of the nodes that were reloaded
  3655. */
  3656. this.trigger('refresh_node', { 'node' : obj, 'nodes' : nodes });
  3657. }.bind(this), false, true);
  3658. },
  3659. /**
  3660. * set (change) the ID of a node
  3661. * @name set_id(obj, id)
  3662. * @param {mixed} obj the node
  3663. * @param {String} id the new ID
  3664. * @return {Boolean}
  3665. * @trigger set_id.jstree
  3666. */
  3667. set_id : function (obj, id) {
  3668. obj = this.get_node(obj);
  3669. if(!obj || obj.id === $.jstree.root) { return false; }
  3670. var i, j, m = this._model.data, old = obj.id;
  3671. id = id.toString();
  3672. // update parents (replace current ID with new one in children and children_d)
  3673. m[obj.parent].children[$.inArray(obj.id, m[obj.parent].children)] = id;
  3674. for(i = 0, j = obj.parents.length; i < j; i++) {
  3675. m[obj.parents[i]].children_d[$.inArray(obj.id, m[obj.parents[i]].children_d)] = id;
  3676. }
  3677. // update children (replace current ID with new one in parent and parents)
  3678. for(i = 0, j = obj.children.length; i < j; i++) {
  3679. m[obj.children[i]].parent = id;
  3680. }
  3681. for(i = 0, j = obj.children_d.length; i < j; i++) {
  3682. m[obj.children_d[i]].parents[$.inArray(obj.id, m[obj.children_d[i]].parents)] = id;
  3683. }
  3684. i = $.inArray(obj.id, this._data.core.selected);
  3685. if(i !== -1) { this._data.core.selected[i] = id; }
  3686. // update model and obj itself (obj.id, this._model.data[KEY])
  3687. i = this.get_node(obj.id, true);
  3688. if(i) {
  3689. i.attr('id', id); //.children('.jstree-anchor').attr('id', id + '_anchor').end().attr('aria-labelledby', id + '_anchor');
  3690. if(this.element.attr('aria-activedescendant') === obj.id) {
  3691. this.element.attr('aria-activedescendant', id);
  3692. }
  3693. }
  3694. delete m[obj.id];
  3695. obj.id = id;
  3696. obj.li_attr.id = id;
  3697. m[id] = obj;
  3698. /**
  3699. * triggered when a node id value is changed
  3700. * @event
  3701. * @name set_id.jstree
  3702. * @param {Object} node
  3703. * @param {String} old the old id
  3704. */
  3705. this.trigger('set_id',{ "node" : obj, "new" : obj.id, "old" : old });
  3706. return true;
  3707. },
  3708. /**
  3709. * get the text value of a node
  3710. * @name get_text(obj)
  3711. * @param {mixed} obj the node
  3712. * @return {String}
  3713. */
  3714. get_text : function (obj) {
  3715. obj = this.get_node(obj);
  3716. return (!obj || obj.id === $.jstree.root) ? false : obj.text;
  3717. },
  3718. /**
  3719. * set the text value of a node. Used internally, please use `rename_node(obj, val)`.
  3720. * @private
  3721. * @name set_text(obj, val)
  3722. * @param {mixed} obj the node, you can pass an array to set the text on multiple nodes
  3723. * @param {String} val the new text value
  3724. * @return {Boolean}
  3725. * @trigger set_text.jstree
  3726. */
  3727. set_text : function (obj, val) {
  3728. var t1, t2;
  3729. if($.vakata.is_array(obj)) {
  3730. obj = obj.slice();
  3731. for(t1 = 0, t2 = obj.length; t1 < t2; t1++) {
  3732. this.set_text(obj[t1], val);
  3733. }
  3734. return true;
  3735. }
  3736. obj = this.get_node(obj);
  3737. if(!obj || obj.id === $.jstree.root) { return false; }
  3738. obj.text = val;
  3739. if(this.get_node(obj, true).length) {
  3740. this.redraw_node(obj.id);
  3741. }
  3742. /**
  3743. * triggered when a node text value is changed
  3744. * @event
  3745. * @name set_text.jstree
  3746. * @param {Object} obj
  3747. * @param {String} text the new value
  3748. */
  3749. this.trigger('set_text',{ "obj" : obj, "text" : val });
  3750. return true;
  3751. },
  3752. /**
  3753. * gets a JSON representation of a node (or the whole tree)
  3754. * @name get_json([obj, options])
  3755. * @param {mixed} obj
  3756. * @param {Object} options
  3757. * @param {Boolean} options.no_state do not return state information
  3758. * @param {Boolean} options.no_id do not return ID
  3759. * @param {Boolean} options.no_children do not include children
  3760. * @param {Boolean} options.no_data do not include node data
  3761. * @param {Boolean} options.no_li_attr do not include LI attributes
  3762. * @param {Boolean} options.no_a_attr do not include A attributes
  3763. * @param {Boolean} options.flat return flat JSON instead of nested
  3764. * @return {Object}
  3765. */
  3766. get_json : function (obj, options, flat) {
  3767. obj = this.get_node(obj || $.jstree.root);
  3768. if(!obj) { return false; }
  3769. if(options && options.flat && !flat) { flat = []; }
  3770. var tmp = {
  3771. 'id' : obj.id,
  3772. 'text' : obj.text,
  3773. 'icon' : this.get_icon(obj),
  3774. 'li_attr' : $.extend(true, {}, obj.li_attr),
  3775. 'a_attr' : $.extend(true, {}, obj.a_attr),
  3776. 'state' : {},
  3777. 'data' : options && options.no_data ? false : $.extend(true, $.vakata.is_array(obj.data)?[]:{}, obj.data)
  3778. //( this.get_node(obj, true).length ? this.get_node(obj, true).data() : obj.data ),
  3779. }, i, j;
  3780. if(options && options.flat) {
  3781. tmp.parent = obj.parent;
  3782. }
  3783. else {
  3784. tmp.children = [];
  3785. }
  3786. if(!options || !options.no_state) {
  3787. for(i in obj.state) {
  3788. if(obj.state.hasOwnProperty(i)) {
  3789. tmp.state[i] = obj.state[i];
  3790. }
  3791. }
  3792. } else {
  3793. delete tmp.state;
  3794. }
  3795. if(options && options.no_li_attr) {
  3796. delete tmp.li_attr;
  3797. }
  3798. if(options && options.no_a_attr) {
  3799. delete tmp.a_attr;
  3800. }
  3801. if(options && options.no_id) {
  3802. delete tmp.id;
  3803. if(tmp.li_attr && tmp.li_attr.id) {
  3804. delete tmp.li_attr.id;
  3805. }
  3806. if(tmp.a_attr && tmp.a_attr.id) {
  3807. delete tmp.a_attr.id;
  3808. }
  3809. }
  3810. if(options && options.flat && obj.id !== $.jstree.root) {
  3811. flat.push(tmp);
  3812. }
  3813. if(!options || !options.no_children) {
  3814. for(i = 0, j = obj.children.length; i < j; i++) {
  3815. if(options && options.flat) {
  3816. this.get_json(obj.children[i], options, flat);
  3817. }
  3818. else {
  3819. tmp.children.push(this.get_json(obj.children[i], options));
  3820. }
  3821. }
  3822. }
  3823. return options && options.flat ? flat : (obj.id === $.jstree.root ? tmp.children : tmp);
  3824. },
  3825. /**
  3826. * create a new node (do not confuse with load_node)
  3827. * @name create_node([par, node, pos, callback, is_loaded])
  3828. * @param {mixed} par the parent node (to create a root node use either "#" (string) or `null`)
  3829. * @param {mixed} node the data for the new node (a valid JSON object, or a simple string with the name)
  3830. * @param {mixed} pos the index at which to insert the node, "first" and "last" are also supported, default is "last"
  3831. * @param {Function} callback a function to be called once the node is created
  3832. * @param {Boolean} is_loaded internal argument indicating if the parent node was succesfully loaded
  3833. * @return {String} the ID of the newly create node
  3834. * @trigger model.jstree, create_node.jstree
  3835. */
  3836. create_node : function (par, node, pos, callback, is_loaded) {
  3837. if(par === null) { par = $.jstree.root; }
  3838. par = this.get_node(par);
  3839. if(!par) { return false; }
  3840. pos = pos === undefined ? "last" : pos;
  3841. if(!pos.toString().match(/^(before|after)$/) && !is_loaded && !this.is_loaded(par)) {
  3842. return this.load_node(par, function () { this.create_node(par, node, pos, callback, true); });
  3843. }
  3844. if(!node) { node = { "text" : this.get_string('New node') }; }
  3845. if(typeof node === "string") {
  3846. node = { "text" : node };
  3847. } else {
  3848. node = $.extend(true, {}, node);
  3849. }
  3850. if(node.text === undefined) { node.text = this.get_string('New node'); }
  3851. var tmp, dpc, i, j;
  3852. if(par.id === $.jstree.root) {
  3853. if(pos === "before") { pos = "first"; }
  3854. if(pos === "after") { pos = "last"; }
  3855. }
  3856. switch(pos) {
  3857. case "before":
  3858. tmp = this.get_node(par.parent);
  3859. pos = $.inArray(par.id, tmp.children);
  3860. par = tmp;
  3861. break;
  3862. case "after" :
  3863. tmp = this.get_node(par.parent);
  3864. pos = $.inArray(par.id, tmp.children) + 1;
  3865. par = tmp;
  3866. break;
  3867. case "inside":
  3868. case "first":
  3869. pos = 0;
  3870. break;
  3871. case "last":
  3872. pos = par.children.length;
  3873. break;
  3874. default:
  3875. if(!pos) { pos = 0; }
  3876. break;
  3877. }
  3878. if(pos > par.children.length) { pos = par.children.length; }
  3879. if(node.id === undefined) { node.id = true; }
  3880. if(!this.check("create_node", node, par, pos)) {
  3881. this.settings.core.error.call(this, this._data.core.last_error);
  3882. return false;
  3883. }
  3884. if(node.id === true) { delete node.id; }
  3885. node = this._parse_model_from_json(node, par.id, par.parents.concat());
  3886. if(!node) { return false; }
  3887. tmp = this.get_node(node);
  3888. dpc = [];
  3889. dpc.push(node);
  3890. dpc = dpc.concat(tmp.children_d);
  3891. this.trigger('model', { "nodes" : dpc, "parent" : par.id });
  3892. par.children_d = par.children_d.concat(dpc);
  3893. for(i = 0, j = par.parents.length; i < j; i++) {
  3894. this._model.data[par.parents[i]].children_d = this._model.data[par.parents[i]].children_d.concat(dpc);
  3895. }
  3896. node = tmp;
  3897. tmp = [];
  3898. for(i = 0, j = par.children.length; i < j; i++) {
  3899. tmp[i >= pos ? i+1 : i] = par.children[i];
  3900. }
  3901. tmp[pos] = node.id;
  3902. par.children = tmp;
  3903. this.redraw_node(par, true);
  3904. /**
  3905. * triggered when a node is created
  3906. * @event
  3907. * @name create_node.jstree
  3908. * @param {Object} node
  3909. * @param {String} parent the parent's ID
  3910. * @param {Number} position the position of the new node among the parent's children
  3911. */
  3912. this.trigger('create_node', { "node" : this.get_node(node), "parent" : par.id, "position" : pos });
  3913. if(callback) { callback.call(this, this.get_node(node)); }
  3914. return node.id;
  3915. },
  3916. /**
  3917. * set the text value of a node
  3918. * @name rename_node(obj, val)
  3919. * @param {mixed} obj the node, you can pass an array to rename multiple nodes to the same name
  3920. * @param {String} val the new text value
  3921. * @return {Boolean}
  3922. * @trigger rename_node.jstree
  3923. */
  3924. rename_node : function (obj, val) {
  3925. var t1, t2, old;
  3926. if($.vakata.is_array(obj)) {
  3927. obj = obj.slice();
  3928. for(t1 = 0, t2 = obj.length; t1 < t2; t1++) {
  3929. this.rename_node(obj[t1], val);
  3930. }
  3931. return true;
  3932. }
  3933. obj = this.get_node(obj);
  3934. if(!obj || obj.id === $.jstree.root) { return false; }
  3935. old = obj.text;
  3936. if(!this.check("rename_node", obj, this.get_parent(obj), val)) {
  3937. this.settings.core.error.call(this, this._data.core.last_error);
  3938. return false;
  3939. }
  3940. this.set_text(obj, val); // .apply(this, Array.prototype.slice.call(arguments))
  3941. /**
  3942. * triggered when a node is renamed
  3943. * @event
  3944. * @name rename_node.jstree
  3945. * @param {Object} node
  3946. * @param {String} text the new value
  3947. * @param {String} old the old value
  3948. */
  3949. this.trigger('rename_node', { "node" : obj, "text" : val, "old" : old });
  3950. return true;
  3951. },
  3952. /**
  3953. * remove a node
  3954. * @name delete_node(obj)
  3955. * @param {mixed} obj the node, you can pass an array to delete multiple nodes
  3956. * @return {Boolean}
  3957. * @trigger delete_node.jstree, changed.jstree
  3958. */
  3959. delete_node : function (obj) {
  3960. var t1, t2, par, pos, tmp, i, j, k, l, c, top, lft;
  3961. if($.vakata.is_array(obj)) {
  3962. obj = obj.slice();
  3963. for(t1 = 0, t2 = obj.length; t1 < t2; t1++) {
  3964. this.delete_node(obj[t1]);
  3965. }
  3966. return true;
  3967. }
  3968. obj = this.get_node(obj);
  3969. if(!obj || obj.id === $.jstree.root) { return false; }
  3970. par = this.get_node(obj.parent);
  3971. pos = $.inArray(obj.id, par.children);
  3972. c = false;
  3973. if(!this.check("delete_node", obj, par, pos)) {
  3974. this.settings.core.error.call(this, this._data.core.last_error);
  3975. return false;
  3976. }
  3977. if(pos !== -1) {
  3978. par.children = $.vakata.array_remove(par.children, pos);
  3979. }
  3980. tmp = obj.children_d.concat([]);
  3981. tmp.push(obj.id);
  3982. for(i = 0, j = obj.parents.length; i < j; i++) {
  3983. this._model.data[obj.parents[i]].children_d = $.vakata.array_filter(this._model.data[obj.parents[i]].children_d, function (v) {
  3984. return $.inArray(v, tmp) === -1;
  3985. });
  3986. }
  3987. for(k = 0, l = tmp.length; k < l; k++) {
  3988. if(this._model.data[tmp[k]].state.selected) {
  3989. c = true;
  3990. break;
  3991. }
  3992. }
  3993. if (c) {
  3994. this._data.core.selected = $.vakata.array_filter(this._data.core.selected, function (v) {
  3995. return $.inArray(v, tmp) === -1;
  3996. });
  3997. }
  3998. /**
  3999. * triggered when a node is deleted
  4000. * @event
  4001. * @name delete_node.jstree
  4002. * @param {Object} node
  4003. * @param {String} parent the parent's ID
  4004. */
  4005. this.trigger('delete_node', { "node" : obj, "parent" : par.id });
  4006. if(c) {
  4007. this.trigger('changed', { 'action' : 'delete_node', 'node' : obj, 'selected' : this._data.core.selected, 'parent' : par.id });
  4008. }
  4009. for(k = 0, l = tmp.length; k < l; k++) {
  4010. delete this._model.data[tmp[k]];
  4011. }
  4012. if($.inArray(this._data.core.focused, tmp) !== -1) {
  4013. this._data.core.focused = null;
  4014. top = this.element[0].scrollTop;
  4015. lft = this.element[0].scrollLeft;
  4016. if(par.id === $.jstree.root) {
  4017. if (this._model.data[$.jstree.root].children[0]) {
  4018. this.get_node(this._model.data[$.jstree.root].children[0], true).children('.jstree-anchor').trigger('focus');
  4019. }
  4020. }
  4021. else {
  4022. this.get_node(par, true).children('.jstree-anchor').trigger('focus');
  4023. }
  4024. this.element[0].scrollTop = top;
  4025. this.element[0].scrollLeft = lft;
  4026. }
  4027. this.redraw_node(par, true);
  4028. return true;
  4029. },
  4030. /**
  4031. * check if an operation is premitted on the tree. Used internally.
  4032. * @private
  4033. * @name check(chk, obj, par, pos)
  4034. * @param {String} chk the operation to check, can be "create_node", "rename_node", "delete_node", "copy_node" or "move_node"
  4035. * @param {mixed} obj the node
  4036. * @param {mixed} par the parent
  4037. * @param {mixed} pos the position to insert at, or if "rename_node" - the new name
  4038. * @param {mixed} more some various additional information, for example if a "move_node" operations is triggered by DND this will be the hovered node
  4039. * @return {Boolean}
  4040. */
  4041. check : function (chk, obj, par, pos, more) {
  4042. obj = obj && obj.id ? obj : this.get_node(obj);
  4043. par = par && par.id ? par : this.get_node(par);
  4044. var tmp = chk.match(/^(move_node|copy_node|create_node)$/i) ? par : obj,
  4045. chc = this.settings.core.check_callback;
  4046. if(chk === "move_node" || chk === "copy_node") {
  4047. if((!more || !more.is_multi) && (chk === "move_node" && $.inArray(obj.id, par.children) === pos)) {
  4048. 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 }) };
  4049. return false;
  4050. }
  4051. 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)) {
  4052. 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 }) };
  4053. return false;
  4054. }
  4055. }
  4056. if(tmp && tmp.data) { tmp = tmp.data; }
  4057. if(tmp && tmp.functions && (tmp.functions[chk] === false || tmp.functions[chk] === true)) {
  4058. if(tmp.functions[chk] === false) {
  4059. 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 }) };
  4060. }
  4061. return tmp.functions[chk];
  4062. }
  4063. if(chc === false || ($.vakata.is_function(chc) && chc.call(this, chk, obj, par, pos, more) === false) || (chc && chc[chk] === false)) {
  4064. 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 }) };
  4065. return false;
  4066. }
  4067. return true;
  4068. },
  4069. /**
  4070. * get the last error
  4071. * @name last_error()
  4072. * @return {Object}
  4073. */
  4074. last_error : function () {
  4075. return this._data.core.last_error;
  4076. },
  4077. /**
  4078. * move a node to a new parent
  4079. * @name move_node(obj, par [, pos, callback, is_loaded])
  4080. * @param {mixed} obj the node to move, pass an array to move multiple nodes
  4081. * @param {mixed} par the new parent
  4082. * @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`
  4083. * @param {function} callback a function to call once the move is completed, receives 3 arguments - the node, the new parent and the position
  4084. * @param {Boolean} is_loaded internal parameter indicating if the parent node has been loaded
  4085. * @param {Boolean} skip_redraw internal parameter indicating if the tree should be redrawn
  4086. * @param {Boolean} instance internal parameter indicating if the node comes from another instance
  4087. * @trigger move_node.jstree
  4088. */
  4089. move_node : function (obj, par, pos, callback, is_loaded, skip_redraw, origin) {
  4090. var t1, t2, old_par, old_pos, new_par, old_ins, is_multi, dpc, tmp, i, j, k, l, p;
  4091. par = this.get_node(par);
  4092. pos = pos === undefined ? 0 : pos;
  4093. if(!par) { return false; }
  4094. if(!pos.toString().match(/^(before|after)$/) && !is_loaded && !this.is_loaded(par)) {
  4095. return this.load_node(par, function () { this.move_node(obj, par, pos, callback, true, false, origin); });
  4096. }
  4097. if($.vakata.is_array(obj)) {
  4098. if(obj.length === 1) {
  4099. obj = obj[0];
  4100. }
  4101. else {
  4102. //obj = obj.slice();
  4103. for(t1 = 0, t2 = obj.length; t1 < t2; t1++) {
  4104. if((tmp = this.move_node(obj[t1], par, pos, callback, is_loaded, false, origin))) {
  4105. par = tmp;
  4106. pos = "after";
  4107. }
  4108. }
  4109. this.redraw();
  4110. return true;
  4111. }
  4112. }
  4113. obj = obj && (obj.id !== undefined) ? obj : this.get_node(obj);
  4114. if(!obj || obj.id === $.jstree.root) { return false; }
  4115. old_par = (obj.parent || $.jstree.root).toString();
  4116. new_par = (!pos.toString().match(/^(before|after)$/) || par.id === $.jstree.root) ? par : this.get_node(par.parent);
  4117. old_ins = origin ? origin : (this._model.data[obj.id] ? this : $.jstree.reference(obj.id));
  4118. is_multi = !old_ins || !old_ins._id || (this._id !== old_ins._id);
  4119. 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;
  4120. if(old_ins && old_ins._id) {
  4121. obj = old_ins._model.data[obj.id];
  4122. }
  4123. if(is_multi) {
  4124. if((tmp = this.copy_node(obj, par, pos, callback, is_loaded, false, origin))) {
  4125. if(old_ins) { old_ins.delete_node(obj); }
  4126. return tmp;
  4127. }
  4128. return false;
  4129. }
  4130. //var m = this._model.data;
  4131. if(par.id === $.jstree.root) {
  4132. if(pos === "before") { pos = "first"; }
  4133. if(pos === "after") { pos = "last"; }
  4134. }
  4135. switch(pos) {
  4136. case "before":
  4137. pos = $.inArray(par.id, new_par.children);
  4138. break;
  4139. case "after" :
  4140. pos = $.inArray(par.id, new_par.children) + 1;
  4141. break;
  4142. case "inside":
  4143. case "first":
  4144. pos = 0;
  4145. break;
  4146. case "last":
  4147. pos = new_par.children.length;
  4148. break;
  4149. default:
  4150. if(!pos) { pos = 0; }
  4151. break;
  4152. }
  4153. if(pos > new_par.children.length) { pos = new_par.children.length; }
  4154. 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) })) {
  4155. this.settings.core.error.call(this, this._data.core.last_error);
  4156. return false;
  4157. }
  4158. if(obj.parent === new_par.id) {
  4159. dpc = new_par.children.concat();
  4160. tmp = $.inArray(obj.id, dpc);
  4161. if(tmp !== -1) {
  4162. dpc = $.vakata.array_remove(dpc, tmp);
  4163. if(pos > tmp) { pos--; }
  4164. }
  4165. tmp = [];
  4166. for(i = 0, j = dpc.length; i < j; i++) {
  4167. tmp[i >= pos ? i+1 : i] = dpc[i];
  4168. }
  4169. tmp[pos] = obj.id;
  4170. new_par.children = tmp;
  4171. this._node_changed(new_par.id);
  4172. this.redraw(new_par.id === $.jstree.root);
  4173. }
  4174. else {
  4175. // clean old parent and up
  4176. tmp = obj.children_d.concat();
  4177. tmp.push(obj.id);
  4178. for(i = 0, j = obj.parents.length; i < j; i++) {
  4179. dpc = [];
  4180. p = old_ins._model.data[obj.parents[i]].children_d;
  4181. for(k = 0, l = p.length; k < l; k++) {
  4182. if($.inArray(p[k], tmp) === -1) {
  4183. dpc.push(p[k]);
  4184. }
  4185. }
  4186. old_ins._model.data[obj.parents[i]].children_d = dpc;
  4187. }
  4188. old_ins._model.data[old_par].children = $.vakata.array_remove_item(old_ins._model.data[old_par].children, obj.id);
  4189. // insert into new parent and up
  4190. for(i = 0, j = new_par.parents.length; i < j; i++) {
  4191. this._model.data[new_par.parents[i]].children_d = this._model.data[new_par.parents[i]].children_d.concat(tmp);
  4192. }
  4193. dpc = [];
  4194. for(i = 0, j = new_par.children.length; i < j; i++) {
  4195. dpc[i >= pos ? i+1 : i] = new_par.children[i];
  4196. }
  4197. dpc[pos] = obj.id;
  4198. new_par.children = dpc;
  4199. new_par.children_d.push(obj.id);
  4200. new_par.children_d = new_par.children_d.concat(obj.children_d);
  4201. // update object
  4202. obj.parent = new_par.id;
  4203. tmp = new_par.parents.concat();
  4204. tmp.unshift(new_par.id);
  4205. p = obj.parents.length;
  4206. obj.parents = tmp;
  4207. // update object children
  4208. tmp = tmp.concat();
  4209. for(i = 0, j = obj.children_d.length; i < j; i++) {
  4210. this._model.data[obj.children_d[i]].parents = this._model.data[obj.children_d[i]].parents.slice(0,p*-1);
  4211. Array.prototype.push.apply(this._model.data[obj.children_d[i]].parents, tmp);
  4212. }
  4213. if(old_par === $.jstree.root || new_par.id === $.jstree.root) {
  4214. this._model.force_full_redraw = true;
  4215. }
  4216. if(!this._model.force_full_redraw) {
  4217. this._node_changed(old_par);
  4218. this._node_changed(new_par.id);
  4219. }
  4220. if(!skip_redraw) {
  4221. this.redraw();
  4222. }
  4223. }
  4224. if(callback) { callback.call(this, obj, new_par, pos); }
  4225. /**
  4226. * triggered when a node is moved
  4227. * @event
  4228. * @name move_node.jstree
  4229. * @param {Object} node
  4230. * @param {String} parent the parent's ID
  4231. * @param {Number} position the position of the node among the parent's children
  4232. * @param {String} old_parent the old parent of the node
  4233. * @param {Number} old_position the old position of the node
  4234. * @param {Boolean} is_multi do the node and new parent belong to different instances
  4235. * @param {jsTree} old_instance the instance the node came from
  4236. * @param {jsTree} new_instance the instance of the new parent
  4237. */
  4238. 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 });
  4239. return obj.id;
  4240. },
  4241. /**
  4242. * copy a node to a new parent
  4243. * @name copy_node(obj, par [, pos, callback, is_loaded])
  4244. * @param {mixed} obj the node to copy, pass an array to copy multiple nodes
  4245. * @param {mixed} par the new parent
  4246. * @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`
  4247. * @param {function} callback a function to call once the move is completed, receives 3 arguments - the node, the new parent and the position
  4248. * @param {Boolean} is_loaded internal parameter indicating if the parent node has been loaded
  4249. * @param {Boolean} skip_redraw internal parameter indicating if the tree should be redrawn
  4250. * @param {Boolean} instance internal parameter indicating if the node comes from another instance
  4251. * @trigger model.jstree copy_node.jstree
  4252. */
  4253. copy_node : function (obj, par, pos, callback, is_loaded, skip_redraw, origin) {
  4254. var t1, t2, dpc, tmp, i, j, node, old_par, new_par, old_ins, is_multi;
  4255. par = this.get_node(par);
  4256. pos = pos === undefined ? 0 : pos;
  4257. if(!par) { return false; }
  4258. if(!pos.toString().match(/^(before|after)$/) && !is_loaded && !this.is_loaded(par)) {
  4259. return this.load_node(par, function () { this.copy_node(obj, par, pos, callback, true, false, origin); });
  4260. }
  4261. if($.vakata.is_array(obj)) {
  4262. if(obj.length === 1) {
  4263. obj = obj[0];
  4264. }
  4265. else {
  4266. //obj = obj.slice();
  4267. for(t1 = 0, t2 = obj.length; t1 < t2; t1++) {
  4268. if((tmp = this.copy_node(obj[t1], par, pos, callback, is_loaded, true, origin))) {
  4269. par = tmp;
  4270. pos = "after";
  4271. }
  4272. }
  4273. this.redraw();
  4274. return true;
  4275. }
  4276. }
  4277. obj = obj && (obj.id !== undefined) ? obj : this.get_node(obj);
  4278. if(!obj || obj.id === $.jstree.root) { return false; }
  4279. old_par = (obj.parent || $.jstree.root).toString();
  4280. new_par = (!pos.toString().match(/^(before|after)$/) || par.id === $.jstree.root) ? par : this.get_node(par.parent);
  4281. old_ins = origin ? origin : (this._model.data[obj.id] ? this : $.jstree.reference(obj.id));
  4282. is_multi = !old_ins || !old_ins._id || (this._id !== old_ins._id);
  4283. if(old_ins && old_ins._id) {
  4284. obj = old_ins._model.data[obj.id];
  4285. }
  4286. if(par.id === $.jstree.root) {
  4287. if(pos === "before") { pos = "first"; }
  4288. if(pos === "after") { pos = "last"; }
  4289. }
  4290. switch(pos) {
  4291. case "before":
  4292. pos = $.inArray(par.id, new_par.children);
  4293. break;
  4294. case "after" :
  4295. pos = $.inArray(par.id, new_par.children) + 1;
  4296. break;
  4297. case "inside":
  4298. case "first":
  4299. pos = 0;
  4300. break;
  4301. case "last":
  4302. pos = new_par.children.length;
  4303. break;
  4304. default:
  4305. if(!pos) { pos = 0; }
  4306. break;
  4307. }
  4308. if(pos > new_par.children.length) { pos = new_par.children.length; }
  4309. 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) })) {
  4310. this.settings.core.error.call(this, this._data.core.last_error);
  4311. return false;
  4312. }
  4313. node = old_ins ? old_ins.get_json(obj, { no_id : true, no_data : true, no_state : true }) : obj;
  4314. if(!node) { return false; }
  4315. if(node.id === true) { delete node.id; }
  4316. node = this._parse_model_from_json(node, new_par.id, new_par.parents.concat());
  4317. if(!node) { return false; }
  4318. tmp = this.get_node(node);
  4319. if(obj && obj.state && obj.state.loaded === false) { tmp.state.loaded = false; }
  4320. dpc = [];
  4321. dpc.push(node);
  4322. dpc = dpc.concat(tmp.children_d);
  4323. this.trigger('model', { "nodes" : dpc, "parent" : new_par.id });
  4324. // insert into new parent and up
  4325. for(i = 0, j = new_par.parents.length; i < j; i++) {
  4326. this._model.data[new_par.parents[i]].children_d = this._model.data[new_par.parents[i]].children_d.concat(dpc);
  4327. }
  4328. dpc = [];
  4329. for(i = 0, j = new_par.children.length; i < j; i++) {
  4330. dpc[i >= pos ? i+1 : i] = new_par.children[i];
  4331. }
  4332. dpc[pos] = tmp.id;
  4333. new_par.children = dpc;
  4334. new_par.children_d.push(tmp.id);
  4335. new_par.children_d = new_par.children_d.concat(tmp.children_d);
  4336. if(new_par.id === $.jstree.root) {
  4337. this._model.force_full_redraw = true;
  4338. }
  4339. if(!this._model.force_full_redraw) {
  4340. this._node_changed(new_par.id);
  4341. }
  4342. if(!skip_redraw) {
  4343. this.redraw(new_par.id === $.jstree.root);
  4344. }
  4345. if(callback) { callback.call(this, tmp, new_par, pos); }
  4346. /**
  4347. * triggered when a node is copied
  4348. * @event
  4349. * @name copy_node.jstree
  4350. * @param {Object} node the copied node
  4351. * @param {Object} original the original node
  4352. * @param {String} parent the parent's ID
  4353. * @param {Number} position the position of the node among the parent's children
  4354. * @param {String} old_parent the old parent of the node
  4355. * @param {Number} old_position the position of the original node
  4356. * @param {Boolean} is_multi do the node and new parent belong to different instances
  4357. * @param {jsTree} old_instance the instance the node came from
  4358. * @param {jsTree} new_instance the instance of the new parent
  4359. */
  4360. 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 });
  4361. return tmp.id;
  4362. },
  4363. /**
  4364. * cut a node (a later call to `paste(obj)` would move the node)
  4365. * @name cut(obj)
  4366. * @param {mixed} obj multiple objects can be passed using an array
  4367. * @trigger cut.jstree
  4368. */
  4369. cut : function (obj) {
  4370. if(!obj) { obj = this._data.core.selected.concat(); }
  4371. if(!$.vakata.is_array(obj)) { obj = [obj]; }
  4372. if(!obj.length) { return false; }
  4373. var tmp = [], o, t1, t2;
  4374. for(t1 = 0, t2 = obj.length; t1 < t2; t1++) {
  4375. o = this.get_node(obj[t1]);
  4376. if(o && (o.id || o.id === 0) && o.id !== $.jstree.root) { tmp.push(o); }
  4377. }
  4378. if(!tmp.length) { return false; }
  4379. ccp_node = tmp;
  4380. ccp_inst = this;
  4381. ccp_mode = 'move_node';
  4382. /**
  4383. * triggered when nodes are added to the buffer for moving
  4384. * @event
  4385. * @name cut.jstree
  4386. * @param {Array} node
  4387. */
  4388. this.trigger('cut', { "node" : obj });
  4389. },
  4390. /**
  4391. * copy a node (a later call to `paste(obj)` would copy the node)
  4392. * @name copy(obj)
  4393. * @param {mixed} obj multiple objects can be passed using an array
  4394. * @trigger copy.jstree
  4395. */
  4396. copy : function (obj) {
  4397. if(!obj) { obj = this._data.core.selected.concat(); }
  4398. if(!$.vakata.is_array(obj)) { obj = [obj]; }
  4399. if(!obj.length) { return false; }
  4400. var tmp = [], o, t1, t2;
  4401. for(t1 = 0, t2 = obj.length; t1 < t2; t1++) {
  4402. o = this.get_node(obj[t1]);
  4403. if(o && (o.id !== undefined) && o.id !== $.jstree.root) { tmp.push(o); }
  4404. }
  4405. if(!tmp.length) { return false; }
  4406. ccp_node = tmp;
  4407. ccp_inst = this;
  4408. ccp_mode = 'copy_node';
  4409. /**
  4410. * triggered when nodes are added to the buffer for copying
  4411. * @event
  4412. * @name copy.jstree
  4413. * @param {Array} node
  4414. */
  4415. this.trigger('copy', { "node" : obj });
  4416. },
  4417. /**
  4418. * get the current buffer (any nodes that are waiting for a paste operation)
  4419. * @name get_buffer()
  4420. * @return {Object} an object consisting of `mode` ("copy_node" or "move_node"), `node` (an array of objects) and `inst` (the instance)
  4421. */
  4422. get_buffer : function () {
  4423. return { 'mode' : ccp_mode, 'node' : ccp_node, 'inst' : ccp_inst };
  4424. },
  4425. /**
  4426. * check if there is something in the buffer to paste
  4427. * @name can_paste()
  4428. * @return {Boolean}
  4429. */
  4430. can_paste : function () {
  4431. return ccp_mode !== false && ccp_node !== false; // && ccp_inst._model.data[ccp_node];
  4432. },
  4433. /**
  4434. * copy or move the previously cut or copied nodes to a new parent
  4435. * @name paste(obj [, pos])
  4436. * @param {mixed} obj the new parent
  4437. * @param {mixed} pos the position to insert at (besides integer, "first" and "last" are supported), defaults to integer `0`
  4438. * @trigger paste.jstree
  4439. */
  4440. paste : function (obj, pos) {
  4441. obj = this.get_node(obj);
  4442. if(!obj || !ccp_mode || !ccp_mode.match(/^(copy_node|move_node)$/) || !ccp_node) { return false; }
  4443. if(this[ccp_mode](ccp_node, obj, pos, false, false, false, ccp_inst)) {
  4444. /**
  4445. * triggered when paste is invoked
  4446. * @event
  4447. * @name paste.jstree
  4448. * @param {String} parent the ID of the receiving node
  4449. * @param {Array} node the nodes in the buffer
  4450. * @param {String} mode the performed operation - "copy_node" or "move_node"
  4451. */
  4452. this.trigger('paste', { "parent" : obj.id, "node" : ccp_node, "mode" : ccp_mode });
  4453. }
  4454. ccp_node = false;
  4455. ccp_mode = false;
  4456. ccp_inst = false;
  4457. },
  4458. /**
  4459. * clear the buffer of previously copied or cut nodes
  4460. * @name clear_buffer()
  4461. * @trigger clear_buffer.jstree
  4462. */
  4463. clear_buffer : function () {
  4464. ccp_node = false;
  4465. ccp_mode = false;
  4466. ccp_inst = false;
  4467. /**
  4468. * triggered when the copy / cut buffer is cleared
  4469. * @event
  4470. * @name clear_buffer.jstree
  4471. */
  4472. this.trigger('clear_buffer');
  4473. },
  4474. /**
  4475. * put a node in edit mode (input field to rename the node)
  4476. * @name edit(obj [, default_text, callback])
  4477. * @param {mixed} obj
  4478. * @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)
  4479. * @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
  4480. */
  4481. edit : function (obj, default_text, callback) {
  4482. var rtl, w, a, s, t, h1, h2, fn, tmp, cancel = false;
  4483. obj = this.get_node(obj);
  4484. if(!obj) { return false; }
  4485. if(!this.check("edit", obj, this.get_parent(obj))) {
  4486. this.settings.core.error.call(this, this._data.core.last_error);
  4487. return false;
  4488. }
  4489. tmp = obj;
  4490. default_text = typeof default_text === 'string' ? default_text : obj.text;
  4491. this.set_text(obj, "");
  4492. obj = this._open_to(obj);
  4493. tmp.text = default_text;
  4494. rtl = this._data.core.rtl;
  4495. w = this.element.width();
  4496. this._data.core.focused = tmp.id;
  4497. a = obj.children('.jstree-anchor').trigger('focus');
  4498. s = $('<span></span>');
  4499. /*!
  4500. oi = obj.children("i:visible"),
  4501. ai = a.children("i:visible"),
  4502. w1 = oi.width() * oi.length,
  4503. w2 = ai.width() * ai.length,
  4504. */
  4505. t = default_text;
  4506. h1 = $("<"+"div></div>", { css : { "position" : "absolute", "top" : "-200px", "left" : (rtl ? "0px" : "-1000px"), "visibility" : "hidden" } }).appendTo(document.body);
  4507. h2 = $("<"+"input />", {
  4508. "value" : t,
  4509. "class" : "jstree-rename-input",
  4510. // "size" : t.length,
  4511. "css" : {
  4512. "padding" : "0",
  4513. "border" : "1px solid silver",
  4514. "box-sizing" : "border-box",
  4515. "display" : "inline-block",
  4516. "height" : (this._data.core.li_height) + "px",
  4517. "lineHeight" : (this._data.core.li_height) + "px",
  4518. "width" : "150px" // will be set a bit further down
  4519. },
  4520. "blur" : function (e) {
  4521. e.stopImmediatePropagation();
  4522. e.preventDefault();
  4523. var i = s.children(".jstree-rename-input"),
  4524. v = i.val(),
  4525. f = this.settings.core.force_text,
  4526. nv;
  4527. if(v === "") { v = t; }
  4528. h1.remove();
  4529. s.replaceWith(a);
  4530. s.remove();
  4531. t = f ? t : $('<div></div>').append($.parseHTML(t)).html();
  4532. obj = this.get_node(obj);
  4533. this.set_text(obj, t);
  4534. nv = !!this.rename_node(obj, f ? $('<div></div>').text(v).text() : $('<div></div>').append($.parseHTML(v)).html());
  4535. if(!nv) {
  4536. this.set_text(obj, t); // move this up? and fix #483
  4537. }
  4538. this._data.core.focused = tmp.id;
  4539. setTimeout(function () {
  4540. var node = this.get_node(tmp.id, true);
  4541. if(node.length) {
  4542. this._data.core.focused = tmp.id;
  4543. node.children('.jstree-anchor').trigger('focus');
  4544. }
  4545. }.bind(this), 0);
  4546. if(callback) {
  4547. callback.call(this, tmp, nv, cancel, v);
  4548. }
  4549. h2 = null;
  4550. }.bind(this),
  4551. "keydown" : function (e) {
  4552. var key = e.which;
  4553. if(key === 27) {
  4554. cancel = true;
  4555. this.value = t;
  4556. }
  4557. if(key === 27 || key === 13 || key === 37 || key === 38 || key === 39 || key === 40 || key === 32) {
  4558. e.stopImmediatePropagation();
  4559. }
  4560. if(key === 27 || key === 13) {
  4561. e.preventDefault();
  4562. this.blur();
  4563. }
  4564. },
  4565. "click" : function (e) { e.stopImmediatePropagation(); },
  4566. "mousedown" : function (e) { e.stopImmediatePropagation(); },
  4567. "keyup" : function (e) {
  4568. h2.width(Math.min(h1.text("pW" + this.value).width(),w));
  4569. },
  4570. "keypress" : function(e) {
  4571. if(e.which === 13) { return false; }
  4572. }
  4573. });
  4574. fn = {
  4575. fontFamily : a.css('fontFamily') || '',
  4576. fontSize : a.css('fontSize') || '',
  4577. fontWeight : a.css('fontWeight') || '',
  4578. fontStyle : a.css('fontStyle') || '',
  4579. fontStretch : a.css('fontStretch') || '',
  4580. fontVariant : a.css('fontVariant') || '',
  4581. letterSpacing : a.css('letterSpacing') || '',
  4582. wordSpacing : a.css('wordSpacing') || ''
  4583. };
  4584. s.attr('class', a.attr('class')).append(a.contents().clone()).append(h2);
  4585. a.replaceWith(s);
  4586. h1.css(fn);
  4587. h2.css(fn).width(Math.min(h1.text("pW" + h2[0].value).width(),w))[0].select();
  4588. $(document).one('mousedown.jstree touchstart.jstree dnd_start.vakata', function (e) {
  4589. if (h2 && e.target !== h2) {
  4590. $(h2).trigger('blur');
  4591. }
  4592. });
  4593. },
  4594. /**
  4595. * changes the theme
  4596. * @name set_theme(theme_name [, theme_url])
  4597. * @param {String} theme_name the name of the new theme to apply
  4598. * @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.
  4599. * @trigger set_theme.jstree
  4600. */
  4601. set_theme : function (theme_name, theme_url) {
  4602. if(!theme_name) { return false; }
  4603. if(theme_url === true) {
  4604. var dir = this.settings.core.themes.dir;
  4605. if(!dir) { dir = $.jstree.path + '/themes'; }
  4606. theme_url = dir + '/' + theme_name + '/style.css';
  4607. }
  4608. if(theme_url && $.inArray(theme_url, themes_loaded) === -1) {
  4609. $('head').append('<'+'link rel="stylesheet" href="' + theme_url + '" type="text/css" />');
  4610. themes_loaded.push(theme_url);
  4611. }
  4612. if(this._data.core.themes.name) {
  4613. this.element.removeClass('jstree-' + this._data.core.themes.name);
  4614. }
  4615. this._data.core.themes.name = theme_name;
  4616. this.element.addClass('jstree-' + theme_name);
  4617. this.element[this.settings.core.themes.responsive ? 'addClass' : 'removeClass' ]('jstree-' + theme_name + '-responsive');
  4618. /**
  4619. * triggered when a theme is set
  4620. * @event
  4621. * @name set_theme.jstree
  4622. * @param {String} theme the new theme
  4623. */
  4624. this.trigger('set_theme', { 'theme' : theme_name });
  4625. },
  4626. /**
  4627. * gets the name of the currently applied theme name
  4628. * @name get_theme()
  4629. * @return {String}
  4630. */
  4631. get_theme : function () { return this._data.core.themes.name; },
  4632. /**
  4633. * changes the theme variant (if the theme has variants)
  4634. * @name set_theme_variant(variant_name)
  4635. * @param {String|Boolean} variant_name the variant to apply (if `false` is used the current variant is removed)
  4636. */
  4637. set_theme_variant : function (variant_name) {
  4638. if(this._data.core.themes.variant) {
  4639. this.element.removeClass('jstree-' + this._data.core.themes.name + '-' + this._data.core.themes.variant);
  4640. }
  4641. this._data.core.themes.variant = variant_name;
  4642. if(variant_name) {
  4643. this.element.addClass('jstree-' + this._data.core.themes.name + '-' + this._data.core.themes.variant);
  4644. }
  4645. },
  4646. /**
  4647. * gets the name of the currently applied theme variant
  4648. * @name get_theme()
  4649. * @return {String}
  4650. */
  4651. get_theme_variant : function () { return this._data.core.themes.variant; },
  4652. /**
  4653. * shows a striped background on the container (if the theme supports it)
  4654. * @name show_stripes()
  4655. */
  4656. show_stripes : function () {
  4657. this._data.core.themes.stripes = true;
  4658. this.get_container_ul().addClass("jstree-striped");
  4659. /**
  4660. * triggered when stripes are shown
  4661. * @event
  4662. * @name show_stripes.jstree
  4663. */
  4664. this.trigger('show_stripes');
  4665. },
  4666. /**
  4667. * hides the striped background on the container
  4668. * @name hide_stripes()
  4669. */
  4670. hide_stripes : function () {
  4671. this._data.core.themes.stripes = false;
  4672. this.get_container_ul().removeClass("jstree-striped");
  4673. /**
  4674. * triggered when stripes are hidden
  4675. * @event
  4676. * @name hide_stripes.jstree
  4677. */
  4678. this.trigger('hide_stripes');
  4679. },
  4680. /**
  4681. * toggles the striped background on the container
  4682. * @name toggle_stripes()
  4683. */
  4684. toggle_stripes : function () { if(this._data.core.themes.stripes) { this.hide_stripes(); } else { this.show_stripes(); } },
  4685. /**
  4686. * shows the connecting dots (if the theme supports it)
  4687. * @name show_dots()
  4688. */
  4689. show_dots : function () {
  4690. this._data.core.themes.dots = true;
  4691. this.get_container_ul().removeClass("jstree-no-dots");
  4692. /**
  4693. * triggered when dots are shown
  4694. * @event
  4695. * @name show_dots.jstree
  4696. */
  4697. this.trigger('show_dots');
  4698. },
  4699. /**
  4700. * hides the connecting dots
  4701. * @name hide_dots()
  4702. */
  4703. hide_dots : function () {
  4704. this._data.core.themes.dots = false;
  4705. this.get_container_ul().addClass("jstree-no-dots");
  4706. /**
  4707. * triggered when dots are hidden
  4708. * @event
  4709. * @name hide_dots.jstree
  4710. */
  4711. this.trigger('hide_dots');
  4712. },
  4713. /**
  4714. * toggles the connecting dots
  4715. * @name toggle_dots()
  4716. */
  4717. toggle_dots : function () { if(this._data.core.themes.dots) { this.hide_dots(); } else { this.show_dots(); } },
  4718. /**
  4719. * show the node icons
  4720. * @name show_icons()
  4721. */
  4722. show_icons : function () {
  4723. this._data.core.themes.icons = true;
  4724. this.get_container_ul().removeClass("jstree-no-icons");
  4725. /**
  4726. * triggered when icons are shown
  4727. * @event
  4728. * @name show_icons.jstree
  4729. */
  4730. this.trigger('show_icons');
  4731. },
  4732. /**
  4733. * hide the node icons
  4734. * @name hide_icons()
  4735. */
  4736. hide_icons : function () {
  4737. this._data.core.themes.icons = false;
  4738. this.get_container_ul().addClass("jstree-no-icons");
  4739. /**
  4740. * triggered when icons are hidden
  4741. * @event
  4742. * @name hide_icons.jstree
  4743. */
  4744. this.trigger('hide_icons');
  4745. },
  4746. /**
  4747. * toggle the node icons
  4748. * @name toggle_icons()
  4749. */
  4750. toggle_icons : function () { if(this._data.core.themes.icons) { this.hide_icons(); } else { this.show_icons(); } },
  4751. /**
  4752. * show the node ellipsis
  4753. * @name show_icons()
  4754. */
  4755. show_ellipsis : function () {
  4756. this._data.core.themes.ellipsis = true;
  4757. this.get_container_ul().addClass("jstree-ellipsis");
  4758. /**
  4759. * triggered when ellisis is shown
  4760. * @event
  4761. * @name show_ellipsis.jstree
  4762. */
  4763. this.trigger('show_ellipsis');
  4764. },
  4765. /**
  4766. * hide the node ellipsis
  4767. * @name hide_ellipsis()
  4768. */
  4769. hide_ellipsis : function () {
  4770. this._data.core.themes.ellipsis = false;
  4771. this.get_container_ul().removeClass("jstree-ellipsis");
  4772. /**
  4773. * triggered when ellisis is hidden
  4774. * @event
  4775. * @name hide_ellipsis.jstree
  4776. */
  4777. this.trigger('hide_ellipsis');
  4778. },
  4779. /**
  4780. * toggle the node ellipsis
  4781. * @name toggle_icons()
  4782. */
  4783. toggle_ellipsis : function () { if(this._data.core.themes.ellipsis) { this.hide_ellipsis(); } else { this.show_ellipsis(); } },
  4784. /**
  4785. * set the node icon for a node
  4786. * @name set_icon(obj, icon)
  4787. * @param {mixed} obj
  4788. * @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
  4789. */
  4790. set_icon : function (obj, icon) {
  4791. var t1, t2, dom, old;
  4792. if($.vakata.is_array(obj)) {
  4793. obj = obj.slice();
  4794. for(t1 = 0, t2 = obj.length; t1 < t2; t1++) {
  4795. this.set_icon(obj[t1], icon);
  4796. }
  4797. return true;
  4798. }
  4799. obj = this.get_node(obj);
  4800. if(!obj || obj.id === $.jstree.root) { return false; }
  4801. old = obj.icon;
  4802. obj.icon = icon === true || icon === null || icon === undefined || icon === '' ? true : icon;
  4803. dom = this.get_node(obj, true).children(".jstree-anchor").children(".jstree-themeicon");
  4804. if(icon === false) {
  4805. dom.removeClass('jstree-themeicon-custom ' + old).css("background","").removeAttr("rel");
  4806. this.hide_icon(obj);
  4807. }
  4808. else if(icon === true || icon === null || icon === undefined || icon === '') {
  4809. dom.removeClass('jstree-themeicon-custom ' + old).css("background","").removeAttr("rel");
  4810. if(old === false) { this.show_icon(obj); }
  4811. }
  4812. else if(icon.indexOf("/") === -1 && icon.indexOf(".") === -1) {
  4813. dom.removeClass(old).css("background","");
  4814. dom.addClass(icon + ' jstree-themeicon-custom').attr("rel",icon);
  4815. if(old === false) { this.show_icon(obj); }
  4816. }
  4817. else {
  4818. dom.removeClass(old).css("background","");
  4819. dom.addClass('jstree-themeicon-custom').css("background", "url('" + icon + "') center center no-repeat").attr("rel",icon);
  4820. if(old === false) { this.show_icon(obj); }
  4821. }
  4822. return true;
  4823. },
  4824. /**
  4825. * get the node icon for a node
  4826. * @name get_icon(obj)
  4827. * @param {mixed} obj
  4828. * @return {String}
  4829. */
  4830. get_icon : function (obj) {
  4831. obj = this.get_node(obj);
  4832. return (!obj || obj.id === $.jstree.root) ? false : obj.icon;
  4833. },
  4834. /**
  4835. * hide the icon on an individual node
  4836. * @name hide_icon(obj)
  4837. * @param {mixed} obj
  4838. */
  4839. hide_icon : function (obj) {
  4840. var t1, t2;
  4841. if($.vakata.is_array(obj)) {
  4842. obj = obj.slice();
  4843. for(t1 = 0, t2 = obj.length; t1 < t2; t1++) {
  4844. this.hide_icon(obj[t1]);
  4845. }
  4846. return true;
  4847. }
  4848. obj = this.get_node(obj);
  4849. if(!obj || obj === $.jstree.root) { return false; }
  4850. obj.icon = false;
  4851. this.get_node(obj, true).children(".jstree-anchor").children(".jstree-themeicon").addClass('jstree-themeicon-hidden');
  4852. return true;
  4853. },
  4854. /**
  4855. * show the icon on an individual node
  4856. * @name show_icon(obj)
  4857. * @param {mixed} obj
  4858. */
  4859. show_icon : function (obj) {
  4860. var t1, t2, dom;
  4861. if($.vakata.is_array(obj)) {
  4862. obj = obj.slice();
  4863. for(t1 = 0, t2 = obj.length; t1 < t2; t1++) {
  4864. this.show_icon(obj[t1]);
  4865. }
  4866. return true;
  4867. }
  4868. obj = this.get_node(obj);
  4869. if(!obj || obj === $.jstree.root) { return false; }
  4870. dom = this.get_node(obj, true);
  4871. obj.icon = dom.length ? dom.children(".jstree-anchor").children(".jstree-themeicon").attr('rel') : true;
  4872. if(!obj.icon) { obj.icon = true; }
  4873. dom.children(".jstree-anchor").children(".jstree-themeicon").removeClass('jstree-themeicon-hidden');
  4874. return true;
  4875. }
  4876. };
  4877. // helpers
  4878. $.vakata = {};
  4879. // collect attributes
  4880. $.vakata.attributes = function(node, with_values) {
  4881. node = $(node)[0];
  4882. var attr = with_values ? {} : [];
  4883. if(node && node.attributes) {
  4884. $.each(node.attributes, function (i, v) {
  4885. if($.inArray(v.name.toLowerCase(),['style','contenteditable','hasfocus','tabindex']) !== -1) { return; }
  4886. if(v.value !== null && $.vakata.trim(v.value) !== '') {
  4887. if(with_values) { attr[v.name] = v.value; }
  4888. else { attr.push(v.name); }
  4889. }
  4890. });
  4891. }
  4892. return attr;
  4893. };
  4894. $.vakata.array_unique = function(array) {
  4895. var a = [], i, j, l, o = {};
  4896. for(i = 0, l = array.length; i < l; i++) {
  4897. if(o[array[i]] === undefined) {
  4898. a.push(array[i]);
  4899. o[array[i]] = true;
  4900. }
  4901. }
  4902. return a;
  4903. };
  4904. // remove item from array
  4905. $.vakata.array_remove = function(array, from) {
  4906. array.splice(from, 1);
  4907. return array;
  4908. //var rest = array.slice((to || from) + 1 || array.length);
  4909. //array.length = from < 0 ? array.length + from : from;
  4910. //array.push.apply(array, rest);
  4911. //return array;
  4912. };
  4913. // remove item from array
  4914. $.vakata.array_remove_item = function(array, item) {
  4915. var tmp = $.inArray(item, array);
  4916. return tmp !== -1 ? $.vakata.array_remove(array, tmp) : array;
  4917. };
  4918. $.vakata.array_filter = function(c,a,b,d,e) {
  4919. if (c.filter) {
  4920. return c.filter(a, b);
  4921. }
  4922. d=[];
  4923. for (e in c) {
  4924. if (~~e+''===e+'' && e>=0 && a.call(b,c[e],+e,c)) {
  4925. d.push(c[e]);
  4926. }
  4927. }
  4928. return d;
  4929. };
  4930. $.vakata.trim = function (text) {
  4931. return String.prototype.trim ?
  4932. String.prototype.trim.call(text.toString()) :
  4933. text.toString().replace(/^[\s\uFEFF\xA0]+|[\s\uFEFF\xA0]+$/g, '');
  4934. };
  4935. $.vakata.is_function = function(obj) {
  4936. return typeof obj === "function" && typeof obj.nodeType !== "number";
  4937. };
  4938. $.vakata.is_array = Array.isArray || function (obj) {
  4939. return Object.prototype.toString.call(obj) === "[object Array]";
  4940. };
  4941. // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_objects/Function/bind#polyfill
  4942. if (!Function.prototype.bind) {
  4943. Function.prototype.bind = function () {
  4944. var thatFunc = this, thatArg = arguments[0];
  4945. var args = Array.prototype.slice.call(arguments, 1);
  4946. if (typeof thatFunc !== 'function') {
  4947. // closest thing possible to the ECMAScript 5
  4948. // internal IsCallable function
  4949. throw new TypeError('Function.prototype.bind - what is trying to be bound is not callable');
  4950. }
  4951. return function(){
  4952. var funcArgs = args.concat(Array.prototype.slice.call(arguments));
  4953. return thatFunc.apply(thatArg, funcArgs);
  4954. };
  4955. };
  4956. }
  4957. }));