jstree.js 283 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169
  1. /*globals jQuery, define, module, exports, require, window, document, postMessage */
  2. (function (factory) {
  3. "use strict";
  4. if (typeof define === 'function' && define.amd) {
  5. define(['jquery'], factory);
  6. }
  7. else if(typeof module !== 'undefined' && module.exports) {
  8. module.exports = factory(require('jquery'));
  9. }
  10. else {
  11. factory(jQuery);
  12. }
  13. }(function ($, undefined) {
  14. "use strict";
  15. /*!
  16. * jsTree 3.3.1
  17. * http://jstree.com/
  18. *
  19. * Copyright (c) 2014 Ivan Bozhanov (http://vakata.com)
  20. *
  21. * Licensed same as jquery - under the terms of the MIT License
  22. * http://www.opensource.org/licenses/mit-license.php
  23. */
  24. /*!
  25. * if using jslint please allow for the jQuery global and use following options:
  26. * jslint: loopfunc: true, browser: true, ass: true, bitwise: true, continue: true, nomen: true, plusplus: true, regexp: true, unparam: true, todo: true, white: true
  27. */
  28. /*jshint -W083 */
  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. _node = document.createElement('LI'), _temp1, _temp2;
  45. _node.setAttribute('role', 'treeitem');
  46. _temp1 = document.createElement('I');
  47. _temp1.className = 'jstree-icon jstree-ocl';
  48. _temp1.setAttribute('role', 'presentation');
  49. _node.appendChild(_temp1);
  50. _temp1 = document.createElement('A');
  51. _temp1.className = 'jstree-anchor';
  52. _temp1.setAttribute('href','#');
  53. _temp1.setAttribute('tabindex','-1');
  54. _temp2 = document.createElement('I');
  55. _temp2.className = 'jstree-icon jstree-themeicon';
  56. _temp2.setAttribute('role', 'presentation');
  57. _temp1.appendChild(_temp2);
  58. _node.appendChild(_temp1);
  59. _temp1 = _temp2 = null;
  60. /**
  61. * holds all jstree related functions and variables, including the actual class and methods to create, access and manipulate instances.
  62. * @name $.jstree
  63. */
  64. $.jstree = {
  65. /**
  66. * specifies the jstree version in use
  67. * @name $.jstree.version
  68. */
  69. version : '3.3.1',
  70. /**
  71. * holds all the default options used when creating new instances
  72. * @name $.jstree.defaults
  73. */
  74. defaults : {
  75. /**
  76. * 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 `[]`
  77. * @name $.jstree.defaults.plugins
  78. */
  79. plugins : []
  80. },
  81. /**
  82. * stores all loaded jstree plugins (used internally)
  83. * @name $.jstree.plugins
  84. */
  85. plugins : {},
  86. path : src && src.indexOf('/') !== -1 ? src.replace(/\/[^\/]+$/,'') : '',
  87. idregex : /[\\:&!^|()\[\]<>@*'+~#";.,=\- \/${}%?`]/g,
  88. root : '#'
  89. };
  90. /**
  91. * creates a jstree instance
  92. * @name $.jstree.create(el [, options])
  93. * @param {DOMElement|jQuery|String} el the element to create the instance on, can be jQuery extended or a selector
  94. * @param {Object} options options for this instance (extends `$.jstree.defaults`)
  95. * @return {jsTree} the new instance
  96. */
  97. $.jstree.create = function (el, options) {
  98. var tmp = new $.jstree.core(++instance_counter),
  99. opt = options;
  100. options = $.extend(true, {}, $.jstree.defaults, options);
  101. if(opt && opt.plugins) {
  102. options.plugins = opt.plugins;
  103. }
  104. $.each(options.plugins, function (i, k) {
  105. if(i !== 'core') {
  106. tmp = tmp.plugin(k, options[k]);
  107. }
  108. });
  109. $(el).data('jstree', tmp);
  110. tmp.init(el, options);
  111. return tmp;
  112. };
  113. /**
  114. * remove all traces of jstree from the DOM and destroy all instances
  115. * @name $.jstree.destroy()
  116. */
  117. $.jstree.destroy = function () {
  118. $('.jstree:jstree').jstree('destroy');
  119. $(document).off('.jstree');
  120. };
  121. /**
  122. * the jstree class constructor, used only internally
  123. * @private
  124. * @name $.jstree.core(id)
  125. * @param {Number} id this instance's index
  126. */
  127. $.jstree.core = function (id) {
  128. this._id = id;
  129. this._cnt = 0;
  130. this._wrk = null;
  131. this._data = {
  132. core : {
  133. themes : {
  134. name : false,
  135. dots : false,
  136. icons : false
  137. },
  138. selected : [],
  139. last_error : {},
  140. working : false,
  141. worker_queue : [],
  142. focused : null
  143. }
  144. };
  145. };
  146. /**
  147. * get a reference to an existing instance
  148. *
  149. * __Examples__
  150. *
  151. * // provided a container with an ID of "tree", and a nested node with an ID of "branch"
  152. * // all of there will return the same instance
  153. * $.jstree.reference('tree');
  154. * $.jstree.reference('#tree');
  155. * $.jstree.reference($('#tree'));
  156. * $.jstree.reference(document.getElementByID('tree'));
  157. * $.jstree.reference('branch');
  158. * $.jstree.reference('#branch');
  159. * $.jstree.reference($('#branch'));
  160. * $.jstree.reference(document.getElementByID('branch'));
  161. *
  162. * @name $.jstree.reference(needle)
  163. * @param {DOMElement|jQuery|String} needle
  164. * @return {jsTree|null} the instance or `null` if not found
  165. */
  166. $.jstree.reference = function (needle) {
  167. var tmp = null,
  168. obj = null;
  169. if(needle && needle.id && (!needle.tagName || !needle.nodeType)) { needle = needle.id; }
  170. if(!obj || !obj.length) {
  171. try { obj = $(needle); } catch (ignore) { }
  172. }
  173. if(!obj || !obj.length) {
  174. try { obj = $('#' + needle.replace($.jstree.idregex,'\\$&')); } catch (ignore) { }
  175. }
  176. if(obj && obj.length && (obj = obj.closest('.jstree')).length && (obj = obj.data('jstree'))) {
  177. tmp = obj;
  178. }
  179. else {
  180. $('.jstree').each(function () {
  181. var inst = $(this).data('jstree');
  182. if(inst && inst._model.data[needle]) {
  183. tmp = inst;
  184. return false;
  185. }
  186. });
  187. }
  188. return tmp;
  189. };
  190. /**
  191. * Create an instance, get an instance or invoke a command on a instance.
  192. *
  193. * 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).
  194. *
  195. * 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).
  196. *
  197. * If there is an existing instance and `arg` is not a string the instance itself is returned (similar to `$.jstree.reference`).
  198. *
  199. * In any other case - nothing is returned and chaining is not broken.
  200. *
  201. * __Examples__
  202. *
  203. * $('#tree1').jstree(); // creates an instance
  204. * $('#tree2').jstree({ plugins : [] }); // create an instance with some options
  205. * $('#tree1').jstree('open_node', '#branch_1'); // call a method on an existing instance, passing additional arguments
  206. * $('#tree2').jstree(); // get an existing instance (or create an instance)
  207. * $('#tree2').jstree(true); // get an existing instance (will not create new instance)
  208. * $('#branch_1').jstree().select_node('#branch_1'); // get an instance (using a nested element and call a method)
  209. *
  210. * @name $().jstree([arg])
  211. * @param {String|Object} arg
  212. * @return {Mixed}
  213. */
  214. $.fn.jstree = function (arg) {
  215. // check for string argument
  216. var is_method = (typeof arg === 'string'),
  217. args = Array.prototype.slice.call(arguments, 1),
  218. result = null;
  219. if(arg === true && !this.length) { return false; }
  220. this.each(function () {
  221. // get the instance (if there is one) and method (if it exists)
  222. var instance = $.jstree.reference(this),
  223. method = is_method && instance ? instance[arg] : null;
  224. // if calling a method, and method is available - execute on the instance
  225. result = is_method && method ?
  226. method.apply(instance, args) :
  227. null;
  228. // if there is no instance and no method is being called - create one
  229. if(!instance && !is_method && (arg === undefined || $.isPlainObject(arg))) {
  230. $.jstree.create(this, arg);
  231. }
  232. // if there is an instance and no method is called - return the instance
  233. if( (instance && !is_method) || arg === true ) {
  234. result = instance || false;
  235. }
  236. // if there was a method call which returned a result - break and return the value
  237. if(result !== null && result !== undefined) {
  238. return false;
  239. }
  240. });
  241. // if there was a method call with a valid return value - return that, otherwise continue the chain
  242. return result !== null && result !== undefined ?
  243. result : this;
  244. };
  245. /**
  246. * used to find elements containing an instance
  247. *
  248. * __Examples__
  249. *
  250. * $('div:jstree').each(function () {
  251. * $(this).jstree('destroy');
  252. * });
  253. *
  254. * @name $(':jstree')
  255. * @return {jQuery}
  256. */
  257. $.expr[':'].jstree = $.expr.createPseudo(function(search) {
  258. return function(a) {
  259. return $(a).hasClass('jstree') &&
  260. $(a).data('jstree') !== undefined;
  261. };
  262. });
  263. /**
  264. * stores all defaults for the core
  265. * @name $.jstree.defaults.core
  266. */
  267. $.jstree.defaults.core = {
  268. /**
  269. * data configuration
  270. *
  271. * 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).
  272. *
  273. * You can also pass in a HTML string or a JSON array here.
  274. *
  275. * 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.
  276. * In addition to the standard jQuery ajax options here you can suppy functions for `data` and `url`, the functions will be run in the current instance's scope and a param will be passed indicating which node is being loaded, the return value of those functions will be used.
  277. *
  278. * 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.
  279. *
  280. * __Examples__
  281. *
  282. * // AJAX
  283. * $('#tree').jstree({
  284. * 'core' : {
  285. * 'data' : {
  286. * 'url' : '/get/children/',
  287. * 'data' : function (node) {
  288. * return { 'id' : node.id };
  289. * }
  290. * }
  291. * });
  292. *
  293. * // direct data
  294. * $('#tree').jstree({
  295. * 'core' : {
  296. * 'data' : [
  297. * 'Simple root node',
  298. * {
  299. * 'id' : 'node_2',
  300. * 'text' : 'Root node with options',
  301. * 'state' : { 'opened' : true, 'selected' : true },
  302. * 'children' : [ { 'text' : 'Child 1' }, 'Child 2']
  303. * }
  304. * ]
  305. * }
  306. * });
  307. *
  308. * // function
  309. * $('#tree').jstree({
  310. * 'core' : {
  311. * 'data' : function (obj, callback) {
  312. * callback.call(this, ['Root 1', 'Root 2']);
  313. * }
  314. * });
  315. *
  316. * @name $.jstree.defaults.core.data
  317. */
  318. data : false,
  319. /**
  320. * configure the various strings used throughout the tree
  321. *
  322. * You can use an object where the key is the string you need to replace and the value is your replacement.
  323. * Another option is to specify a function which will be called with an argument of the needed string and should return the replacement.
  324. * If left as `false` no replacement is made.
  325. *
  326. * __Examples__
  327. *
  328. * $('#tree').jstree({
  329. * 'core' : {
  330. * 'strings' : {
  331. * 'Loading ...' : 'Please wait ...'
  332. * }
  333. * }
  334. * });
  335. *
  336. * @name $.jstree.defaults.core.strings
  337. */
  338. strings : false,
  339. /**
  340. * determines what happens when a user tries to modify the structure of the tree
  341. * If left as `false` all operations like create, rename, delete, move or copy are prevented.
  342. * You can set this to `true` to allow all interactions or use a function to have better control.
  343. *
  344. * __Examples__
  345. *
  346. * $('#tree').jstree({
  347. * 'core' : {
  348. * 'check_callback' : function (operation, node, node_parent, node_position, more) {
  349. * // operation can be 'create_node', 'rename_node', 'delete_node', 'move_node' or 'copy_node'
  350. * // in case of 'rename_node' node_position is filled with the new node name
  351. * return operation === 'rename_node' ? true : false;
  352. * }
  353. * }
  354. * });
  355. *
  356. * @name $.jstree.defaults.core.check_callback
  357. */
  358. check_callback : false,
  359. /**
  360. * a callback called with a single object parameter in the instance's scope when something goes wrong (operation prevented, ajax failed, etc)
  361. * @name $.jstree.defaults.core.error
  362. */
  363. error : $.noop,
  364. /**
  365. * the open / close animation duration in milliseconds - set this to `false` to disable the animation (default is `200`)
  366. * @name $.jstree.defaults.core.animation
  367. */
  368. animation : 200,
  369. /**
  370. * a boolean indicating if multiple nodes can be selected
  371. * @name $.jstree.defaults.core.multiple
  372. */
  373. multiple : true,
  374. /**
  375. * theme configuration object
  376. * @name $.jstree.defaults.core.themes
  377. */
  378. themes : {
  379. /**
  380. * the name of the theme to use (if left as `false` the default theme is used)
  381. * @name $.jstree.defaults.core.themes.name
  382. */
  383. name : false,
  384. /**
  385. * 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.
  386. * @name $.jstree.defaults.core.themes.url
  387. */
  388. url : false,
  389. /**
  390. * the location of all jstree themes - only used if `url` is set to `true`
  391. * @name $.jstree.defaults.core.themes.dir
  392. */
  393. dir : false,
  394. /**
  395. * a boolean indicating if connecting dots are shown
  396. * @name $.jstree.defaults.core.themes.dots
  397. */
  398. dots : true,
  399. /**
  400. * a boolean indicating if node icons are shown
  401. * @name $.jstree.defaults.core.themes.icons
  402. */
  403. icons : true,
  404. /**
  405. * a boolean indicating if the tree background is striped
  406. * @name $.jstree.defaults.core.themes.stripes
  407. */
  408. stripes : false,
  409. /**
  410. * a string (or boolean `false`) specifying the theme variant to use (if the theme supports variants)
  411. * @name $.jstree.defaults.core.themes.variant
  412. */
  413. variant : false,
  414. /**
  415. * a boolean specifying if a reponsive version of the theme should kick in on smaller screens (if the theme supports it). Defaults to `false`.
  416. * @name $.jstree.defaults.core.themes.responsive
  417. */
  418. responsive : false
  419. },
  420. /**
  421. * 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)
  422. * @name $.jstree.defaults.core.expand_selected_onload
  423. */
  424. expand_selected_onload : true,
  425. /**
  426. * 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`
  427. * @name $.jstree.defaults.core.worker
  428. */
  429. worker : true,
  430. /**
  431. * Force node text to plain text (and escape HTML). Defaults to `false`
  432. * @name $.jstree.defaults.core.force_text
  433. */
  434. force_text : false,
  435. /**
  436. * Should the node should be toggled if the text is double clicked . Defaults to `true`
  437. * @name $.jstree.defaults.core.dblclick_toggle
  438. */
  439. dblclick_toggle : true
  440. };
  441. $.jstree.core.prototype = {
  442. /**
  443. * used to decorate an instance with a plugin. Used internally.
  444. * @private
  445. * @name plugin(deco [, opts])
  446. * @param {String} deco the plugin to decorate with
  447. * @param {Object} opts options for the plugin
  448. * @return {jsTree}
  449. */
  450. plugin : function (deco, opts) {
  451. var Child = $.jstree.plugins[deco];
  452. if(Child) {
  453. this._data[deco] = {};
  454. Child.prototype = this;
  455. return new Child(opts, this);
  456. }
  457. return this;
  458. },
  459. /**
  460. * initialize the instance. Used internally.
  461. * @private
  462. * @name init(el, optons)
  463. * @param {DOMElement|jQuery|String} el the element we are transforming
  464. * @param {Object} options options for this instance
  465. * @trigger init.jstree, loading.jstree, loaded.jstree, ready.jstree, changed.jstree
  466. */
  467. init : function (el, options) {
  468. this._model = {
  469. data : {},
  470. changed : [],
  471. force_full_redraw : false,
  472. redraw_timeout : false,
  473. default_state : {
  474. loaded : true,
  475. opened : false,
  476. selected : false,
  477. disabled : false
  478. }
  479. };
  480. this._model.data[$.jstree.root] = {
  481. id : $.jstree.root,
  482. parent : null,
  483. parents : [],
  484. children : [],
  485. children_d : [],
  486. state : { loaded : false }
  487. };
  488. this.element = $(el).addClass('jstree jstree-' + this._id);
  489. this.settings = options;
  490. this._data.core.ready = false;
  491. this._data.core.loaded = false;
  492. this._data.core.rtl = (this.element.css("direction") === "rtl");
  493. this.element[this._data.core.rtl ? 'addClass' : 'removeClass']("jstree-rtl");
  494. this.element.attr('role','tree');
  495. if(this.settings.core.multiple) {
  496. this.element.attr('aria-multiselectable', true);
  497. }
  498. if(!this.element.attr('tabindex')) {
  499. this.element.attr('tabindex','0');
  500. }
  501. this.bind();
  502. /**
  503. * triggered after all events are bound
  504. * @event
  505. * @name init.jstree
  506. */
  507. this.trigger("init");
  508. this._data.core.original_container_html = this.element.find(" > ul > li").clone(true);
  509. this._data.core.original_container_html
  510. .find("li").addBack()
  511. .contents().filter(function() {
  512. return this.nodeType === 3 && (!this.nodeValue || /^\s+$/.test(this.nodeValue));
  513. })
  514. .remove();
  515. 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='tree-item'><i class='jstree-icon jstree-ocl'></i><"+"a class='jstree-anchor' href='#'><i class='jstree-icon jstree-themeicon-hidden'></i>" + this.get_string("Loading ...") + "</a></li></ul>");
  516. this.element.attr('aria-activedescendant','j' + this._id + '_loading');
  517. this._data.core.li_height = this.get_container_ul().children("li").first().height() || 24;
  518. /**
  519. * triggered after the loading text is shown and before loading starts
  520. * @event
  521. * @name loading.jstree
  522. */
  523. this.trigger("loading");
  524. this.load_node($.jstree.root);
  525. },
  526. /**
  527. * destroy an instance
  528. * @name destroy()
  529. * @param {Boolean} keep_html if not set to `true` the container will be emptied, otherwise the current DOM elements will be kept intact
  530. */
  531. destroy : function (keep_html) {
  532. if(this._wrk) {
  533. try {
  534. window.URL.revokeObjectURL(this._wrk);
  535. this._wrk = null;
  536. }
  537. catch (ignore) { }
  538. }
  539. if(!keep_html) { this.element.empty(); }
  540. this.teardown();
  541. },
  542. /**
  543. * part of the destroying of an instance. Used internally.
  544. * @private
  545. * @name teardown()
  546. */
  547. teardown : function () {
  548. this.unbind();
  549. this.element
  550. .removeClass('jstree')
  551. .removeData('jstree')
  552. .find("[class^='jstree']")
  553. .addBack()
  554. .attr("class", function () { return this.className.replace(/jstree[^ ]*|$/ig,''); });
  555. this.element = null;
  556. },
  557. /**
  558. * bind all events. Used internally.
  559. * @private
  560. * @name bind()
  561. */
  562. bind : function () {
  563. var word = '',
  564. tout = null,
  565. was_click = 0;
  566. this.element
  567. .on("dblclick.jstree", function (e) {
  568. if(e.target.tagName && e.target.tagName.toLowerCase() === "input") { return true; }
  569. if(document.selection && document.selection.empty) {
  570. document.selection.empty();
  571. }
  572. else {
  573. if(window.getSelection) {
  574. var sel = window.getSelection();
  575. try {
  576. sel.removeAllRanges();
  577. sel.collapse();
  578. } catch (ignore) { }
  579. }
  580. }
  581. })
  582. .on("mousedown.jstree", $.proxy(function (e) {
  583. if(e.target === this.element[0]) {
  584. e.preventDefault(); // prevent losing focus when clicking scroll arrows (FF, Chrome)
  585. was_click = +(new Date()); // ie does not allow to prevent losing focus
  586. }
  587. }, this))
  588. .on("mousedown.jstree", ".jstree-ocl", function (e) {
  589. e.preventDefault(); // prevent any node inside from losing focus when clicking the open/close icon
  590. })
  591. .on("click.jstree", ".jstree-ocl", $.proxy(function (e) {
  592. this.toggle_node(e.target);
  593. }, this))
  594. .on("dblclick.jstree", ".jstree-anchor", $.proxy(function (e) {
  595. if(e.target.tagName && e.target.tagName.toLowerCase() === "input") { return true; }
  596. if(this.settings.core.dblclick_toggle) {
  597. this.toggle_node(e.target);
  598. }
  599. }, this))
  600. .on("click.jstree", ".jstree-anchor", $.proxy(function (e) {
  601. e.preventDefault();
  602. if(e.currentTarget !== document.activeElement) { $(e.currentTarget).focus(); }
  603. this.activate_node(e.currentTarget, e);
  604. }, this))
  605. .on('keydown.jstree', '.jstree-anchor', $.proxy(function (e) {
  606. if(e.target.tagName && e.target.tagName.toLowerCase() === "input") { return true; }
  607. if(e.which !== 32 && e.which !== 13 && (e.shiftKey || e.ctrlKey || e.altKey || e.metaKey)) { return true; }
  608. var o = null;
  609. if(this._data.core.rtl) {
  610. if(e.which === 37) { e.which = 39; }
  611. else if(e.which === 39) { e.which = 37; }
  612. }
  613. switch(e.which) {
  614. case 32: // aria defines space only with Ctrl
  615. if(e.ctrlKey) {
  616. e.type = "click";
  617. $(e.currentTarget).trigger(e);
  618. }
  619. break;
  620. case 13: // enter
  621. e.type = "click";
  622. $(e.currentTarget).trigger(e);
  623. break;
  624. case 37: // left
  625. e.preventDefault();
  626. if(this.is_open(e.currentTarget)) {
  627. this.close_node(e.currentTarget);
  628. }
  629. else {
  630. o = this.get_parent(e.currentTarget);
  631. if(o && o.id !== $.jstree.root) { this.get_node(o, true).children('.jstree-anchor').focus(); }
  632. }
  633. break;
  634. case 38: // up
  635. e.preventDefault();
  636. o = this.get_prev_dom(e.currentTarget);
  637. if(o && o.length) { o.children('.jstree-anchor').focus(); }
  638. break;
  639. case 39: // right
  640. e.preventDefault();
  641. if(this.is_closed(e.currentTarget)) {
  642. this.open_node(e.currentTarget, function (o) { this.get_node(o, true).children('.jstree-anchor').focus(); });
  643. }
  644. else if (this.is_open(e.currentTarget)) {
  645. o = this.get_node(e.currentTarget, true).children('.jstree-children')[0];
  646. if(o) { $(this._firstChild(o)).children('.jstree-anchor').focus(); }
  647. }
  648. break;
  649. case 40: // down
  650. e.preventDefault();
  651. o = this.get_next_dom(e.currentTarget);
  652. if(o && o.length) { o.children('.jstree-anchor').focus(); }
  653. break;
  654. case 106: // aria defines * on numpad as open_all - not very common
  655. this.open_all();
  656. break;
  657. case 36: // home
  658. e.preventDefault();
  659. o = this._firstChild(this.get_container_ul()[0]);
  660. if(o) { $(o).children('.jstree-anchor').filter(':visible').focus(); }
  661. break;
  662. case 35: // end
  663. e.preventDefault();
  664. this.element.find('.jstree-anchor').filter(':visible').last().focus();
  665. break;
  666. case 113: // f2 - safe to include - if check_callback is false it will fail
  667. e.preventDefault();
  668. this.edit(e.currentTarget);
  669. break;
  670. default:
  671. break;
  672. /*!
  673. // delete
  674. case 46:
  675. e.preventDefault();
  676. o = this.get_node(e.currentTarget);
  677. if(o && o.id && o.id !== $.jstree.root) {
  678. o = this.is_selected(o) ? this.get_selected() : o;
  679. this.delete_node(o);
  680. }
  681. break;
  682. */
  683. }
  684. }, this))
  685. .on("load_node.jstree", $.proxy(function (e, data) {
  686. if(data.status) {
  687. if(data.node.id === $.jstree.root && !this._data.core.loaded) {
  688. this._data.core.loaded = true;
  689. if(this._firstChild(this.get_container_ul()[0])) {
  690. this.element.attr('aria-activedescendant',this._firstChild(this.get_container_ul()[0]).id);
  691. }
  692. /**
  693. * triggered after the root node is loaded for the first time
  694. * @event
  695. * @name loaded.jstree
  696. */
  697. this.trigger("loaded");
  698. }
  699. if(!this._data.core.ready) {
  700. setTimeout($.proxy(function() {
  701. if(this.element && !this.get_container_ul().find('.jstree-loading').length) {
  702. this._data.core.ready = true;
  703. if(this._data.core.selected.length) {
  704. if(this.settings.core.expand_selected_onload) {
  705. var tmp = [], i, j;
  706. for(i = 0, j = this._data.core.selected.length; i < j; i++) {
  707. tmp = tmp.concat(this._model.data[this._data.core.selected[i]].parents);
  708. }
  709. tmp = $.vakata.array_unique(tmp);
  710. for(i = 0, j = tmp.length; i < j; i++) {
  711. this.open_node(tmp[i], false, 0);
  712. }
  713. }
  714. this.trigger('changed', { 'action' : 'ready', 'selected' : this._data.core.selected });
  715. }
  716. /**
  717. * triggered after all nodes are finished loading
  718. * @event
  719. * @name ready.jstree
  720. */
  721. this.trigger("ready");
  722. }
  723. }, this), 0);
  724. }
  725. }
  726. }, this))
  727. // quick searching when the tree is focused
  728. .on('keypress.jstree', $.proxy(function (e) {
  729. if(e.target.tagName && e.target.tagName.toLowerCase() === "input") { return true; }
  730. if(tout) { clearTimeout(tout); }
  731. tout = setTimeout(function () {
  732. word = '';
  733. }, 500);
  734. var chr = String.fromCharCode(e.which).toLowerCase(),
  735. col = this.element.find('.jstree-anchor').filter(':visible'),
  736. ind = col.index(document.activeElement) || 0,
  737. end = false;
  738. word += chr;
  739. // match for whole word from current node down (including the current node)
  740. if(word.length > 1) {
  741. col.slice(ind).each($.proxy(function (i, v) {
  742. if($(v).text().toLowerCase().indexOf(word) === 0) {
  743. $(v).focus();
  744. end = true;
  745. return false;
  746. }
  747. }, this));
  748. if(end) { return; }
  749. // match for whole word from the beginning of the tree
  750. col.slice(0, ind).each($.proxy(function (i, v) {
  751. if($(v).text().toLowerCase().indexOf(word) === 0) {
  752. $(v).focus();
  753. end = true;
  754. return false;
  755. }
  756. }, this));
  757. if(end) { return; }
  758. }
  759. // list nodes that start with that letter (only if word consists of a single char)
  760. if(new RegExp('^' + chr.replace(/[-\/\\^$*+?.()|[\]{}]/g, '\\$&') + '+$').test(word)) {
  761. // search for the next node starting with that letter
  762. col.slice(ind + 1).each($.proxy(function (i, v) {
  763. if($(v).text().toLowerCase().charAt(0) === chr) {
  764. $(v).focus();
  765. end = true;
  766. return false;
  767. }
  768. }, this));
  769. if(end) { return; }
  770. // search from the beginning
  771. col.slice(0, ind + 1).each($.proxy(function (i, v) {
  772. if($(v).text().toLowerCase().charAt(0) === chr) {
  773. $(v).focus();
  774. end = true;
  775. return false;
  776. }
  777. }, this));
  778. if(end) { return; }
  779. }
  780. }, this))
  781. // THEME RELATED
  782. .on("init.jstree", $.proxy(function () {
  783. var s = this.settings.core.themes;
  784. this._data.core.themes.dots = s.dots;
  785. this._data.core.themes.stripes = s.stripes;
  786. this._data.core.themes.icons = s.icons;
  787. this.set_theme(s.name || "default", s.url);
  788. this.set_theme_variant(s.variant);
  789. }, this))
  790. .on("loading.jstree", $.proxy(function () {
  791. this[ this._data.core.themes.dots ? "show_dots" : "hide_dots" ]();
  792. this[ this._data.core.themes.icons ? "show_icons" : "hide_icons" ]();
  793. this[ this._data.core.themes.stripes ? "show_stripes" : "hide_stripes" ]();
  794. }, this))
  795. .on('blur.jstree', '.jstree-anchor', $.proxy(function (e) {
  796. this._data.core.focused = null;
  797. $(e.currentTarget).filter('.jstree-hovered').mouseleave();
  798. this.element.attr('tabindex', '0');
  799. }, this))
  800. .on('focus.jstree', '.jstree-anchor', $.proxy(function (e) {
  801. var tmp = this.get_node(e.currentTarget);
  802. if(tmp && tmp.id) {
  803. this._data.core.focused = tmp.id;
  804. }
  805. this.element.find('.jstree-hovered').not(e.currentTarget).mouseleave();
  806. $(e.currentTarget).mouseenter();
  807. this.element.attr('tabindex', '-1');
  808. }, this))
  809. .on('focus.jstree', $.proxy(function () {
  810. if(+(new Date()) - was_click > 500 && !this._data.core.focused) {
  811. was_click = 0;
  812. var act = this.get_node(this.element.attr('aria-activedescendant'), true);
  813. if(act) {
  814. act.find('> .jstree-anchor').focus();
  815. }
  816. }
  817. }, this))
  818. .on('mouseenter.jstree', '.jstree-anchor', $.proxy(function (e) {
  819. this.hover_node(e.currentTarget);
  820. }, this))
  821. .on('mouseleave.jstree', '.jstree-anchor', $.proxy(function (e) {
  822. this.dehover_node(e.currentTarget);
  823. }, this));
  824. },
  825. /**
  826. * part of the destroying of an instance. Used internally.
  827. * @private
  828. * @name unbind()
  829. */
  830. unbind : function () {
  831. this.element.off('.jstree');
  832. $(document).off('.jstree-' + this._id);
  833. },
  834. /**
  835. * trigger an event. Used internally.
  836. * @private
  837. * @name trigger(ev [, data])
  838. * @param {String} ev the name of the event to trigger
  839. * @param {Object} data additional data to pass with the event
  840. */
  841. trigger : function (ev, data) {
  842. if(!data) {
  843. data = {};
  844. }
  845. data.instance = this;
  846. this.element.triggerHandler(ev.replace('.jstree','') + '.jstree', data);
  847. },
  848. /**
  849. * returns the jQuery extended instance container
  850. * @name get_container()
  851. * @return {jQuery}
  852. */
  853. get_container : function () {
  854. return this.element;
  855. },
  856. /**
  857. * returns the jQuery extended main UL node inside the instance container. Used internally.
  858. * @private
  859. * @name get_container_ul()
  860. * @return {jQuery}
  861. */
  862. get_container_ul : function () {
  863. return this.element.children(".jstree-children").first();
  864. },
  865. /**
  866. * gets string replacements (localization). Used internally.
  867. * @private
  868. * @name get_string(key)
  869. * @param {String} key
  870. * @return {String}
  871. */
  872. get_string : function (key) {
  873. var a = this.settings.core.strings;
  874. if($.isFunction(a)) { return a.call(this, key); }
  875. if(a && a[key]) { return a[key]; }
  876. return key;
  877. },
  878. /**
  879. * gets the first child of a DOM node. Used internally.
  880. * @private
  881. * @name _firstChild(dom)
  882. * @param {DOMElement} dom
  883. * @return {DOMElement}
  884. */
  885. _firstChild : function (dom) {
  886. dom = dom ? dom.firstChild : null;
  887. while(dom !== null && dom.nodeType !== 1) {
  888. dom = dom.nextSibling;
  889. }
  890. return dom;
  891. },
  892. /**
  893. * gets the next sibling of a DOM node. Used internally.
  894. * @private
  895. * @name _nextSibling(dom)
  896. * @param {DOMElement} dom
  897. * @return {DOMElement}
  898. */
  899. _nextSibling : function (dom) {
  900. dom = dom ? dom.nextSibling : null;
  901. while(dom !== null && dom.nodeType !== 1) {
  902. dom = dom.nextSibling;
  903. }
  904. return dom;
  905. },
  906. /**
  907. * gets the previous sibling of a DOM node. Used internally.
  908. * @private
  909. * @name _previousSibling(dom)
  910. * @param {DOMElement} dom
  911. * @return {DOMElement}
  912. */
  913. _previousSibling : function (dom) {
  914. dom = dom ? dom.previousSibling : null;
  915. while(dom !== null && dom.nodeType !== 1) {
  916. dom = dom.previousSibling;
  917. }
  918. return dom;
  919. },
  920. /**
  921. * 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)
  922. * @name get_node(obj [, as_dom])
  923. * @param {mixed} obj
  924. * @param {Boolean} as_dom
  925. * @return {Object|jQuery}
  926. */
  927. get_node : function (obj, as_dom) {
  928. if(obj && obj.id) {
  929. obj = obj.id;
  930. }
  931. var dom;
  932. try {
  933. if(this._model.data[obj]) {
  934. obj = this._model.data[obj];
  935. }
  936. else if(typeof obj === "string" && this._model.data[obj.replace(/^#/, '')]) {
  937. obj = this._model.data[obj.replace(/^#/, '')];
  938. }
  939. else if(typeof obj === "string" && (dom = $('#' + obj.replace($.jstree.idregex,'\\$&'), this.element)).length && this._model.data[dom.closest('.jstree-node').attr('id')]) {
  940. obj = this._model.data[dom.closest('.jstree-node').attr('id')];
  941. }
  942. else if((dom = $(obj, this.element)).length && this._model.data[dom.closest('.jstree-node').attr('id')]) {
  943. obj = this._model.data[dom.closest('.jstree-node').attr('id')];
  944. }
  945. else if((dom = $(obj, this.element)).length && dom.hasClass('jstree')) {
  946. obj = this._model.data[$.jstree.root];
  947. }
  948. else {
  949. return false;
  950. }
  951. if(as_dom) {
  952. obj = obj.id === $.jstree.root ? this.element : $('#' + obj.id.replace($.jstree.idregex,'\\$&'), this.element);
  953. }
  954. return obj;
  955. } catch (ex) { return false; }
  956. },
  957. /**
  958. * get the path to a node, either consisting of node texts, or of node IDs, optionally glued together (otherwise an array)
  959. * @name get_path(obj [, glue, ids])
  960. * @param {mixed} obj the node
  961. * @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
  962. * @param {Boolean} ids if set to true build the path using ID, otherwise node text is used
  963. * @return {mixed}
  964. */
  965. get_path : function (obj, glue, ids) {
  966. obj = obj.parents ? obj : this.get_node(obj);
  967. if(!obj || obj.id === $.jstree.root || !obj.parents) {
  968. return false;
  969. }
  970. var i, j, p = [];
  971. p.push(ids ? obj.id : obj.text);
  972. for(i = 0, j = obj.parents.length; i < j; i++) {
  973. p.push(ids ? obj.parents[i] : this.get_text(obj.parents[i]));
  974. }
  975. p = p.reverse().slice(1);
  976. return glue ? p.join(glue) : p;
  977. },
  978. /**
  979. * get the next visible node that is below the `obj` node. If `strict` is set to `true` only sibling nodes are returned.
  980. * @name get_next_dom(obj [, strict])
  981. * @param {mixed} obj
  982. * @param {Boolean} strict
  983. * @return {jQuery}
  984. */
  985. get_next_dom : function (obj, strict) {
  986. var tmp;
  987. obj = this.get_node(obj, true);
  988. if(obj[0] === this.element[0]) {
  989. tmp = this._firstChild(this.get_container_ul()[0]);
  990. while (tmp && tmp.offsetHeight === 0) {
  991. tmp = this._nextSibling(tmp);
  992. }
  993. return tmp ? $(tmp) : false;
  994. }
  995. if(!obj || !obj.length) {
  996. return false;
  997. }
  998. if(strict) {
  999. tmp = obj[0];
  1000. do {
  1001. tmp = this._nextSibling(tmp);
  1002. } while (tmp && tmp.offsetHeight === 0);
  1003. return tmp ? $(tmp) : false;
  1004. }
  1005. if(obj.hasClass("jstree-open")) {
  1006. tmp = this._firstChild(obj.children('.jstree-children')[0]);
  1007. while (tmp && tmp.offsetHeight === 0) {
  1008. tmp = this._nextSibling(tmp);
  1009. }
  1010. if(tmp !== null) {
  1011. return $(tmp);
  1012. }
  1013. }
  1014. tmp = obj[0];
  1015. do {
  1016. tmp = this._nextSibling(tmp);
  1017. } while (tmp && tmp.offsetHeight === 0);
  1018. if(tmp !== null) {
  1019. return $(tmp);
  1020. }
  1021. return obj.parentsUntil(".jstree",".jstree-node").nextAll(".jstree-node:visible").first();
  1022. },
  1023. /**
  1024. * get the previous visible node that is above the `obj` node. If `strict` is set to `true` only sibling nodes are returned.
  1025. * @name get_prev_dom(obj [, strict])
  1026. * @param {mixed} obj
  1027. * @param {Boolean} strict
  1028. * @return {jQuery}
  1029. */
  1030. get_prev_dom : function (obj, strict) {
  1031. var tmp;
  1032. obj = this.get_node(obj, true);
  1033. if(obj[0] === this.element[0]) {
  1034. tmp = this.get_container_ul()[0].lastChild;
  1035. while (tmp && tmp.offsetHeight === 0) {
  1036. tmp = this._previousSibling(tmp);
  1037. }
  1038. return tmp ? $(tmp) : false;
  1039. }
  1040. if(!obj || !obj.length) {
  1041. return false;
  1042. }
  1043. if(strict) {
  1044. tmp = obj[0];
  1045. do {
  1046. tmp = this._previousSibling(tmp);
  1047. } while (tmp && tmp.offsetHeight === 0);
  1048. return tmp ? $(tmp) : false;
  1049. }
  1050. tmp = obj[0];
  1051. do {
  1052. tmp = this._previousSibling(tmp);
  1053. } while (tmp && tmp.offsetHeight === 0);
  1054. if(tmp !== null) {
  1055. obj = $(tmp);
  1056. while(obj.hasClass("jstree-open")) {
  1057. obj = obj.children(".jstree-children").first().children(".jstree-node:visible:last");
  1058. }
  1059. return obj;
  1060. }
  1061. tmp = obj[0].parentNode.parentNode;
  1062. return tmp && tmp.className && tmp.className.indexOf('jstree-node') !== -1 ? $(tmp) : false;
  1063. },
  1064. /**
  1065. * get the parent ID of a node
  1066. * @name get_parent(obj)
  1067. * @param {mixed} obj
  1068. * @return {String}
  1069. */
  1070. get_parent : function (obj) {
  1071. obj = this.get_node(obj);
  1072. if(!obj || obj.id === $.jstree.root) {
  1073. return false;
  1074. }
  1075. return obj.parent;
  1076. },
  1077. /**
  1078. * get a jQuery collection of all the children of a node (node must be rendered)
  1079. * @name get_children_dom(obj)
  1080. * @param {mixed} obj
  1081. * @return {jQuery}
  1082. */
  1083. get_children_dom : function (obj) {
  1084. obj = this.get_node(obj, true);
  1085. if(obj[0] === this.element[0]) {
  1086. return this.get_container_ul().children(".jstree-node");
  1087. }
  1088. if(!obj || !obj.length) {
  1089. return false;
  1090. }
  1091. return obj.children(".jstree-children").children(".jstree-node");
  1092. },
  1093. /**
  1094. * checks if a node has children
  1095. * @name is_parent(obj)
  1096. * @param {mixed} obj
  1097. * @return {Boolean}
  1098. */
  1099. is_parent : function (obj) {
  1100. obj = this.get_node(obj);
  1101. return obj && (obj.state.loaded === false || obj.children.length > 0);
  1102. },
  1103. /**
  1104. * checks if a node is loaded (its children are available)
  1105. * @name is_loaded(obj)
  1106. * @param {mixed} obj
  1107. * @return {Boolean}
  1108. */
  1109. is_loaded : function (obj) {
  1110. obj = this.get_node(obj);
  1111. return obj && obj.state.loaded;
  1112. },
  1113. /**
  1114. * check if a node is currently loading (fetching children)
  1115. * @name is_loading(obj)
  1116. * @param {mixed} obj
  1117. * @return {Boolean}
  1118. */
  1119. is_loading : function (obj) {
  1120. obj = this.get_node(obj);
  1121. return obj && obj.state && obj.state.loading;
  1122. },
  1123. /**
  1124. * check if a node is opened
  1125. * @name is_open(obj)
  1126. * @param {mixed} obj
  1127. * @return {Boolean}
  1128. */
  1129. is_open : function (obj) {
  1130. obj = this.get_node(obj);
  1131. return obj && obj.state.opened;
  1132. },
  1133. /**
  1134. * check if a node is in a closed state
  1135. * @name is_closed(obj)
  1136. * @param {mixed} obj
  1137. * @return {Boolean}
  1138. */
  1139. is_closed : function (obj) {
  1140. obj = this.get_node(obj);
  1141. return obj && this.is_parent(obj) && !obj.state.opened;
  1142. },
  1143. /**
  1144. * check if a node has no children
  1145. * @name is_leaf(obj)
  1146. * @param {mixed} obj
  1147. * @return {Boolean}
  1148. */
  1149. is_leaf : function (obj) {
  1150. return !this.is_parent(obj);
  1151. },
  1152. /**
  1153. * loads a node (fetches its children using the `core.data` setting). Multiple nodes can be passed to by using an array.
  1154. * @name load_node(obj [, callback])
  1155. * @param {mixed} obj
  1156. * @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
  1157. * @return {Boolean}
  1158. * @trigger load_node.jstree
  1159. */
  1160. load_node : function (obj, callback) {
  1161. var k, l, i, j, c;
  1162. if($.isArray(obj)) {
  1163. this._load_nodes(obj.slice(), callback);
  1164. return true;
  1165. }
  1166. obj = this.get_node(obj);
  1167. if(!obj) {
  1168. if(callback) { callback.call(this, obj, false); }
  1169. return false;
  1170. }
  1171. // 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?
  1172. if(obj.state.loaded) {
  1173. obj.state.loaded = false;
  1174. for(i = 0, j = obj.parents.length; i < j; i++) {
  1175. this._model.data[obj.parents[i]].children_d = $.vakata.array_filter(this._model.data[obj.parents[i]].children_d, function (v) {
  1176. return $.inArray(v, obj.children_d) === -1;
  1177. });
  1178. }
  1179. for(k = 0, l = obj.children_d.length; k < l; k++) {
  1180. if(this._model.data[obj.children_d[k]].state.selected) {
  1181. c = true;
  1182. }
  1183. delete this._model.data[obj.children_d[k]];
  1184. }
  1185. if (c) {
  1186. this._data.core.selected = $.vakata.array_filter(this._data.core.selected, function (v) {
  1187. return $.inArray(v, obj.children_d) === -1;
  1188. });
  1189. }
  1190. obj.children = [];
  1191. obj.children_d = [];
  1192. if(c) {
  1193. this.trigger('changed', { 'action' : 'load_node', 'node' : obj, 'selected' : this._data.core.selected });
  1194. }
  1195. }
  1196. obj.state.failed = false;
  1197. obj.state.loading = true;
  1198. this.get_node(obj, true).addClass("jstree-loading").attr('aria-busy',true);
  1199. this._load_node(obj, $.proxy(function (status) {
  1200. obj = this._model.data[obj.id];
  1201. obj.state.loading = false;
  1202. obj.state.loaded = status;
  1203. obj.state.failed = !obj.state.loaded;
  1204. var dom = this.get_node(obj, true), i = 0, j = 0, m = this._model.data, has_children = false;
  1205. for(i = 0, j = obj.children.length; i < j; i++) {
  1206. if(m[obj.children[i]] && !m[obj.children[i]].state.hidden) {
  1207. has_children = true;
  1208. break;
  1209. }
  1210. }
  1211. if(obj.state.loaded && dom && dom.length) {
  1212. dom.removeClass('jstree-closed jstree-open jstree-leaf');
  1213. if (!has_children) {
  1214. dom.addClass('jstree-leaf');
  1215. }
  1216. else {
  1217. if (obj.id !== '#') {
  1218. dom.addClass(obj.state.opened ? 'jstree-open' : 'jstree-closed');
  1219. }
  1220. }
  1221. }
  1222. dom.removeClass("jstree-loading").attr('aria-busy',false);
  1223. /**
  1224. * triggered after a node is loaded
  1225. * @event
  1226. * @name load_node.jstree
  1227. * @param {Object} node the node that was loading
  1228. * @param {Boolean} status was the node loaded successfully
  1229. */
  1230. this.trigger('load_node', { "node" : obj, "status" : status });
  1231. if(callback) {
  1232. callback.call(this, obj, status);
  1233. }
  1234. }, this));
  1235. return true;
  1236. },
  1237. /**
  1238. * load an array of nodes (will also load unavailable nodes as soon as the appear in the structure). Used internally.
  1239. * @private
  1240. * @name _load_nodes(nodes [, callback])
  1241. * @param {array} nodes
  1242. * @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
  1243. */
  1244. _load_nodes : function (nodes, callback, is_callback, force_reload) {
  1245. var r = true,
  1246. c = function () { this._load_nodes(nodes, callback, true); },
  1247. m = this._model.data, i, j, tmp = [];
  1248. for(i = 0, j = nodes.length; i < j; i++) {
  1249. if(m[nodes[i]] && ( (!m[nodes[i]].state.loaded && !m[nodes[i]].state.failed) || (!is_callback && force_reload) )) {
  1250. if(!this.is_loading(nodes[i])) {
  1251. this.load_node(nodes[i], c);
  1252. }
  1253. r = false;
  1254. }
  1255. }
  1256. if(r) {
  1257. for(i = 0, j = nodes.length; i < j; i++) {
  1258. if(m[nodes[i]] && m[nodes[i]].state.loaded) {
  1259. tmp.push(nodes[i]);
  1260. }
  1261. }
  1262. if(callback && !callback.done) {
  1263. callback.call(this, tmp);
  1264. callback.done = true;
  1265. }
  1266. }
  1267. },
  1268. /**
  1269. * loads all unloaded nodes
  1270. * @name load_all([obj, callback])
  1271. * @param {mixed} obj the node to load recursively, omit to load all nodes in the tree
  1272. * @param {function} callback a function to be executed once loading all the nodes is complete,
  1273. * @trigger load_all.jstree
  1274. */
  1275. load_all : function (obj, callback) {
  1276. if(!obj) { obj = $.jstree.root; }
  1277. obj = this.get_node(obj);
  1278. if(!obj) { return false; }
  1279. var to_load = [],
  1280. m = this._model.data,
  1281. c = m[obj.id].children_d,
  1282. i, j;
  1283. if(obj.state && !obj.state.loaded) {
  1284. to_load.push(obj.id);
  1285. }
  1286. for(i = 0, j = c.length; i < j; i++) {
  1287. if(m[c[i]] && m[c[i]].state && !m[c[i]].state.loaded) {
  1288. to_load.push(c[i]);
  1289. }
  1290. }
  1291. if(to_load.length) {
  1292. this._load_nodes(to_load, function () {
  1293. this.load_all(obj, callback);
  1294. });
  1295. }
  1296. else {
  1297. /**
  1298. * triggered after a load_all call completes
  1299. * @event
  1300. * @name load_all.jstree
  1301. * @param {Object} node the recursively loaded node
  1302. */
  1303. if(callback) { callback.call(this, obj); }
  1304. this.trigger('load_all', { "node" : obj });
  1305. }
  1306. },
  1307. /**
  1308. * handles the actual loading of a node. Used only internally.
  1309. * @private
  1310. * @name _load_node(obj [, callback])
  1311. * @param {mixed} obj
  1312. * @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
  1313. * @return {Boolean}
  1314. */
  1315. _load_node : function (obj, callback) {
  1316. var s = this.settings.core.data, t;
  1317. // use original HTML
  1318. if(!s) {
  1319. if(obj.id === $.jstree.root) {
  1320. return this._append_html_data(obj, this._data.core.original_container_html.clone(true), function (status) {
  1321. callback.call(this, status);
  1322. });
  1323. }
  1324. else {
  1325. return callback.call(this, false);
  1326. }
  1327. // return callback.call(this, obj.id === $.jstree.root ? this._append_html_data(obj, this._data.core.original_container_html.clone(true)) : false);
  1328. }
  1329. if($.isFunction(s)) {
  1330. return s.call(this, obj, $.proxy(function (d) {
  1331. if(d === false) {
  1332. callback.call(this, false);
  1333. }
  1334. else {
  1335. this[typeof d === 'string' ? '_append_html_data' : '_append_json_data'](obj, typeof d === 'string' ? $($.parseHTML(d)).filter(function () { return this.nodeType !== 3; }) : d, function (status) {
  1336. callback.call(this, status);
  1337. });
  1338. }
  1339. // 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));
  1340. }, this));
  1341. }
  1342. if(typeof s === 'object') {
  1343. if(s.url) {
  1344. s = $.extend(true, {}, s);
  1345. if($.isFunction(s.url)) {
  1346. s.url = s.url.call(this, obj);
  1347. }
  1348. if($.isFunction(s.data)) {
  1349. s.data = s.data.call(this, obj);
  1350. }
  1351. return $.ajax(s)
  1352. .done($.proxy(function (d,t,x) {
  1353. var type = x.getResponseHeader('Content-Type');
  1354. if((type && type.indexOf('json') !== -1) || typeof d === "object") {
  1355. return this._append_json_data(obj, d, function (status) { callback.call(this, status); });
  1356. //return callback.call(this, this._append_json_data(obj, d));
  1357. }
  1358. if((type && type.indexOf('html') !== -1) || typeof d === "string") {
  1359. return this._append_html_data(obj, $($.parseHTML(d)).filter(function () { return this.nodeType !== 3; }), function (status) { callback.call(this, status); });
  1360. // return callback.call(this, this._append_html_data(obj, $(d)));
  1361. }
  1362. 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 }) };
  1363. this.settings.core.error.call(this, this._data.core.last_error);
  1364. return callback.call(this, false);
  1365. }, this))
  1366. .fail($.proxy(function (f) {
  1367. callback.call(this, false);
  1368. 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 }) };
  1369. this.settings.core.error.call(this, this._data.core.last_error);
  1370. }, this));
  1371. }
  1372. t = ($.isArray(s) || $.isPlainObject(s)) ? JSON.parse(JSON.stringify(s)) : s;
  1373. if(obj.id === $.jstree.root) {
  1374. return this._append_json_data(obj, t, function (status) {
  1375. callback.call(this, status);
  1376. });
  1377. }
  1378. else {
  1379. this._data.core.last_error = { 'error' : 'nodata', 'plugin' : 'core', 'id' : 'core_05', 'reason' : 'Could not load node', 'data' : JSON.stringify({ 'id' : obj.id }) };
  1380. this.settings.core.error.call(this, this._data.core.last_error);
  1381. return callback.call(this, false);
  1382. }
  1383. //return callback.call(this, (obj.id === $.jstree.root ? this._append_json_data(obj, t) : false) );
  1384. }
  1385. if(typeof s === 'string') {
  1386. if(obj.id === $.jstree.root) {
  1387. return this._append_html_data(obj, $($.parseHTML(s)).filter(function () { return this.nodeType !== 3; }), function (status) {
  1388. callback.call(this, status);
  1389. });
  1390. }
  1391. else {
  1392. this._data.core.last_error = { 'error' : 'nodata', 'plugin' : 'core', 'id' : 'core_06', 'reason' : 'Could not load node', 'data' : JSON.stringify({ 'id' : obj.id }) };
  1393. this.settings.core.error.call(this, this._data.core.last_error);
  1394. return callback.call(this, false);
  1395. }
  1396. //return callback.call(this, (obj.id === $.jstree.root ? this._append_html_data(obj, $(s)) : false) );
  1397. }
  1398. return callback.call(this, false);
  1399. },
  1400. /**
  1401. * adds a node to the list of nodes to redraw. Used only internally.
  1402. * @private
  1403. * @name _node_changed(obj [, callback])
  1404. * @param {mixed} obj
  1405. */
  1406. _node_changed : function (obj) {
  1407. obj = this.get_node(obj);
  1408. if(obj) {
  1409. this._model.changed.push(obj.id);
  1410. }
  1411. },
  1412. /**
  1413. * appends HTML content to the tree. Used internally.
  1414. * @private
  1415. * @name _append_html_data(obj, data)
  1416. * @param {mixed} obj the node to append to
  1417. * @param {String} data the HTML string to parse and append
  1418. * @trigger model.jstree, changed.jstree
  1419. */
  1420. _append_html_data : function (dom, data, cb) {
  1421. dom = this.get_node(dom);
  1422. dom.children = [];
  1423. dom.children_d = [];
  1424. var dat = data.is('ul') ? data.children() : data,
  1425. par = dom.id,
  1426. chd = [],
  1427. dpc = [],
  1428. m = this._model.data,
  1429. p = m[par],
  1430. s = this._data.core.selected.length,
  1431. tmp, i, j;
  1432. dat.each($.proxy(function (i, v) {
  1433. tmp = this._parse_model_from_html($(v), par, p.parents.concat());
  1434. if(tmp) {
  1435. chd.push(tmp);
  1436. dpc.push(tmp);
  1437. if(m[tmp].children_d.length) {
  1438. dpc = dpc.concat(m[tmp].children_d);
  1439. }
  1440. }
  1441. }, this));
  1442. p.children = chd;
  1443. p.children_d = dpc;
  1444. for(i = 0, j = p.parents.length; i < j; i++) {
  1445. m[p.parents[i]].children_d = m[p.parents[i]].children_d.concat(dpc);
  1446. }
  1447. /**
  1448. * triggered when new data is inserted to the tree model
  1449. * @event
  1450. * @name model.jstree
  1451. * @param {Array} nodes an array of node IDs
  1452. * @param {String} parent the parent ID of the nodes
  1453. */
  1454. this.trigger('model', { "nodes" : dpc, 'parent' : par });
  1455. if(par !== $.jstree.root) {
  1456. this._node_changed(par);
  1457. this.redraw();
  1458. }
  1459. else {
  1460. this.get_container_ul().children('.jstree-initial-node').remove();
  1461. this.redraw(true);
  1462. }
  1463. if(this._data.core.selected.length !== s) {
  1464. this.trigger('changed', { 'action' : 'model', 'selected' : this._data.core.selected });
  1465. }
  1466. cb.call(this, true);
  1467. },
  1468. /**
  1469. * appends JSON content to the tree. Used internally.
  1470. * @private
  1471. * @name _append_json_data(obj, data)
  1472. * @param {mixed} obj the node to append to
  1473. * @param {String} data the JSON object to parse and append
  1474. * @param {Boolean} force_processing internal param - do not set
  1475. * @trigger model.jstree, changed.jstree
  1476. */
  1477. _append_json_data : function (dom, data, cb, force_processing) {
  1478. if(this.element === null) { return; }
  1479. dom = this.get_node(dom);
  1480. dom.children = [];
  1481. dom.children_d = [];
  1482. // *%$@!!!
  1483. if(data.d) {
  1484. data = data.d;
  1485. if(typeof data === "string") {
  1486. data = JSON.parse(data);
  1487. }
  1488. }
  1489. if(!$.isArray(data)) { data = [data]; }
  1490. var w = null,
  1491. args = {
  1492. 'df' : this._model.default_state,
  1493. 'dat' : data,
  1494. 'par' : dom.id,
  1495. 'm' : this._model.data,
  1496. 't_id' : this._id,
  1497. 't_cnt' : this._cnt,
  1498. 'sel' : this._data.core.selected
  1499. },
  1500. func = function (data, undefined) {
  1501. if(data.data) { data = data.data; }
  1502. var dat = data.dat,
  1503. par = data.par,
  1504. chd = [],
  1505. dpc = [],
  1506. add = [],
  1507. df = data.df,
  1508. t_id = data.t_id,
  1509. t_cnt = data.t_cnt,
  1510. m = data.m,
  1511. p = m[par],
  1512. sel = data.sel,
  1513. tmp, i, j, rslt,
  1514. parse_flat = function (d, p, ps) {
  1515. if(!ps) { ps = []; }
  1516. else { ps = ps.concat(); }
  1517. if(p) { ps.unshift(p); }
  1518. var tid = d.id.toString(),
  1519. i, j, c, e,
  1520. tmp = {
  1521. id : tid,
  1522. text : d.text || '',
  1523. icon : d.icon !== undefined ? d.icon : true,
  1524. parent : p,
  1525. parents : ps,
  1526. children : d.children || [],
  1527. children_d : d.children_d || [],
  1528. data : d.data,
  1529. state : { },
  1530. li_attr : { id : false },
  1531. a_attr : { href : '#' },
  1532. original : false
  1533. };
  1534. for(i in df) {
  1535. if(df.hasOwnProperty(i)) {
  1536. tmp.state[i] = df[i];
  1537. }
  1538. }
  1539. if(d && d.data && d.data.jstree && d.data.jstree.icon) {
  1540. tmp.icon = d.data.jstree.icon;
  1541. }
  1542. if(tmp.icon === undefined || tmp.icon === null || tmp.icon === "") {
  1543. tmp.icon = true;
  1544. }
  1545. if(d && d.data) {
  1546. tmp.data = d.data;
  1547. if(d.data.jstree) {
  1548. for(i in d.data.jstree) {
  1549. if(d.data.jstree.hasOwnProperty(i)) {
  1550. tmp.state[i] = d.data.jstree[i];
  1551. }
  1552. }
  1553. }
  1554. }
  1555. if(d && typeof d.state === 'object') {
  1556. for (i in d.state) {
  1557. if(d.state.hasOwnProperty(i)) {
  1558. tmp.state[i] = d.state[i];
  1559. }
  1560. }
  1561. }
  1562. if(d && typeof d.li_attr === 'object') {
  1563. for (i in d.li_attr) {
  1564. if(d.li_attr.hasOwnProperty(i)) {
  1565. tmp.li_attr[i] = d.li_attr[i];
  1566. }
  1567. }
  1568. }
  1569. if(!tmp.li_attr.id) {
  1570. tmp.li_attr.id = tid;
  1571. }
  1572. if(d && typeof d.a_attr === 'object') {
  1573. for (i in d.a_attr) {
  1574. if(d.a_attr.hasOwnProperty(i)) {
  1575. tmp.a_attr[i] = d.a_attr[i];
  1576. }
  1577. }
  1578. }
  1579. if(d && d.children && d.children === true) {
  1580. tmp.state.loaded = false;
  1581. tmp.children = [];
  1582. tmp.children_d = [];
  1583. }
  1584. m[tmp.id] = tmp;
  1585. for(i = 0, j = tmp.children.length; i < j; i++) {
  1586. c = parse_flat(m[tmp.children[i]], tmp.id, ps);
  1587. e = m[c];
  1588. tmp.children_d.push(c);
  1589. if(e.children_d.length) {
  1590. tmp.children_d = tmp.children_d.concat(e.children_d);
  1591. }
  1592. }
  1593. delete d.data;
  1594. delete d.children;
  1595. m[tmp.id].original = d;
  1596. if(tmp.state.selected) {
  1597. add.push(tmp.id);
  1598. }
  1599. return tmp.id;
  1600. },
  1601. parse_nest = function (d, p, ps) {
  1602. if(!ps) { ps = []; }
  1603. else { ps = ps.concat(); }
  1604. if(p) { ps.unshift(p); }
  1605. var tid = false, i, j, c, e, tmp;
  1606. do {
  1607. tid = 'j' + t_id + '_' + (++t_cnt);
  1608. } while(m[tid]);
  1609. tmp = {
  1610. id : false,
  1611. text : typeof d === 'string' ? d : '',
  1612. icon : typeof d === 'object' && d.icon !== undefined ? d.icon : true,
  1613. parent : p,
  1614. parents : ps,
  1615. children : [],
  1616. children_d : [],
  1617. data : null,
  1618. state : { },
  1619. li_attr : { id : false },
  1620. a_attr : { href : '#' },
  1621. original : false
  1622. };
  1623. for(i in df) {
  1624. if(df.hasOwnProperty(i)) {
  1625. tmp.state[i] = df[i];
  1626. }
  1627. }
  1628. if(d && d.id) { tmp.id = d.id.toString(); }
  1629. if(d && d.text) { tmp.text = d.text; }
  1630. if(d && d.data && d.data.jstree && d.data.jstree.icon) {
  1631. tmp.icon = d.data.jstree.icon;
  1632. }
  1633. if(tmp.icon === undefined || tmp.icon === null || tmp.icon === "") {
  1634. tmp.icon = true;
  1635. }
  1636. if(d && d.data) {
  1637. tmp.data = d.data;
  1638. if(d.data.jstree) {
  1639. for(i in d.data.jstree) {
  1640. if(d.data.jstree.hasOwnProperty(i)) {
  1641. tmp.state[i] = d.data.jstree[i];
  1642. }
  1643. }
  1644. }
  1645. }
  1646. if(d && typeof d.state === 'object') {
  1647. for (i in d.state) {
  1648. if(d.state.hasOwnProperty(i)) {
  1649. tmp.state[i] = d.state[i];
  1650. }
  1651. }
  1652. }
  1653. if(d && typeof d.li_attr === 'object') {
  1654. for (i in d.li_attr) {
  1655. if(d.li_attr.hasOwnProperty(i)) {
  1656. tmp.li_attr[i] = d.li_attr[i];
  1657. }
  1658. }
  1659. }
  1660. if(tmp.li_attr.id && !tmp.id) {
  1661. tmp.id = tmp.li_attr.id.toString();
  1662. }
  1663. if(!tmp.id) {
  1664. tmp.id = tid;
  1665. }
  1666. if(!tmp.li_attr.id) {
  1667. tmp.li_attr.id = tmp.id;
  1668. }
  1669. if(d && typeof d.a_attr === 'object') {
  1670. for (i in d.a_attr) {
  1671. if(d.a_attr.hasOwnProperty(i)) {
  1672. tmp.a_attr[i] = d.a_attr[i];
  1673. }
  1674. }
  1675. }
  1676. if(d && d.children && d.children.length) {
  1677. for(i = 0, j = d.children.length; i < j; i++) {
  1678. c = parse_nest(d.children[i], tmp.id, ps);
  1679. e = m[c];
  1680. tmp.children.push(c);
  1681. if(e.children_d.length) {
  1682. tmp.children_d = tmp.children_d.concat(e.children_d);
  1683. }
  1684. }
  1685. tmp.children_d = tmp.children_d.concat(tmp.children);
  1686. }
  1687. if(d && d.children && d.children === true) {
  1688. tmp.state.loaded = false;
  1689. tmp.children = [];
  1690. tmp.children_d = [];
  1691. }
  1692. delete d.data;
  1693. delete d.children;
  1694. tmp.original = d;
  1695. m[tmp.id] = tmp;
  1696. if(tmp.state.selected) {
  1697. add.push(tmp.id);
  1698. }
  1699. return tmp.id;
  1700. };
  1701. if(dat.length && dat[0].id !== undefined && dat[0].parent !== undefined) {
  1702. // Flat JSON support (for easy import from DB):
  1703. // 1) convert to object (foreach)
  1704. for(i = 0, j = dat.length; i < j; i++) {
  1705. if(!dat[i].children) {
  1706. dat[i].children = [];
  1707. }
  1708. m[dat[i].id.toString()] = dat[i];
  1709. }
  1710. // 2) populate children (foreach)
  1711. for(i = 0, j = dat.length; i < j; i++) {
  1712. m[dat[i].parent.toString()].children.push(dat[i].id.toString());
  1713. // populate parent.children_d
  1714. p.children_d.push(dat[i].id.toString());
  1715. }
  1716. // 3) normalize && populate parents and children_d with recursion
  1717. for(i = 0, j = p.children.length; i < j; i++) {
  1718. tmp = parse_flat(m[p.children[i]], par, p.parents.concat());
  1719. dpc.push(tmp);
  1720. if(m[tmp].children_d.length) {
  1721. dpc = dpc.concat(m[tmp].children_d);
  1722. }
  1723. }
  1724. for(i = 0, j = p.parents.length; i < j; i++) {
  1725. m[p.parents[i]].children_d = m[p.parents[i]].children_d.concat(dpc);
  1726. }
  1727. // ?) three_state selection - p.state.selected && t - (if three_state foreach(dat => ch) -> foreach(parents) if(parent.selected) child.selected = true;
  1728. rslt = {
  1729. 'cnt' : t_cnt,
  1730. 'mod' : m,
  1731. 'sel' : sel,
  1732. 'par' : par,
  1733. 'dpc' : dpc,
  1734. 'add' : add
  1735. };
  1736. }
  1737. else {
  1738. for(i = 0, j = dat.length; i < j; i++) {
  1739. tmp = parse_nest(dat[i], par, p.parents.concat());
  1740. if(tmp) {
  1741. chd.push(tmp);
  1742. dpc.push(tmp);
  1743. if(m[tmp].children_d.length) {
  1744. dpc = dpc.concat(m[tmp].children_d);
  1745. }
  1746. }
  1747. }
  1748. p.children = chd;
  1749. p.children_d = dpc;
  1750. for(i = 0, j = p.parents.length; i < j; i++) {
  1751. m[p.parents[i]].children_d = m[p.parents[i]].children_d.concat(dpc);
  1752. }
  1753. rslt = {
  1754. 'cnt' : t_cnt,
  1755. 'mod' : m,
  1756. 'sel' : sel,
  1757. 'par' : par,
  1758. 'dpc' : dpc,
  1759. 'add' : add
  1760. };
  1761. }
  1762. if(typeof window === 'undefined' || typeof window.document === 'undefined') {
  1763. postMessage(rslt);
  1764. }
  1765. else {
  1766. return rslt;
  1767. }
  1768. },
  1769. rslt = function (rslt, worker) {
  1770. if(this.element === null) { return; }
  1771. this._cnt = rslt.cnt;
  1772. var i, m = this._model.data;
  1773. for (i in m) {
  1774. if (m.hasOwnProperty(i) && m[i].state && m[i].state.loading && rslt.mod[i]) {
  1775. rslt.mod[i].state.loading = true;
  1776. }
  1777. }
  1778. this._model.data = rslt.mod; // breaks the reference in load_node - careful
  1779. if(worker) {
  1780. var j, a = rslt.add, r = rslt.sel, s = this._data.core.selected.slice();
  1781. m = this._model.data;
  1782. // if selection was changed while calculating in worker
  1783. if(r.length !== s.length || $.vakata.array_unique(r.concat(s)).length !== r.length) {
  1784. // deselect nodes that are no longer selected
  1785. for(i = 0, j = r.length; i < j; i++) {
  1786. if($.inArray(r[i], a) === -1 && $.inArray(r[i], s) === -1) {
  1787. m[r[i]].state.selected = false;
  1788. }
  1789. }
  1790. // select nodes that were selected in the mean time
  1791. for(i = 0, j = s.length; i < j; i++) {
  1792. if($.inArray(s[i], r) === -1) {
  1793. m[s[i]].state.selected = true;
  1794. }
  1795. }
  1796. }
  1797. }
  1798. if(rslt.add.length) {
  1799. this._data.core.selected = this._data.core.selected.concat(rslt.add);
  1800. }
  1801. this.trigger('model', { "nodes" : rslt.dpc, 'parent' : rslt.par });
  1802. if(rslt.par !== $.jstree.root) {
  1803. this._node_changed(rslt.par);
  1804. this.redraw();
  1805. }
  1806. else {
  1807. // this.get_container_ul().children('.jstree-initial-node').remove();
  1808. this.redraw(true);
  1809. }
  1810. if(rslt.add.length) {
  1811. this.trigger('changed', { 'action' : 'model', 'selected' : this._data.core.selected });
  1812. }
  1813. cb.call(this, true);
  1814. };
  1815. if(this.settings.core.worker && window.Blob && window.URL && window.Worker) {
  1816. try {
  1817. if(this._wrk === null) {
  1818. this._wrk = window.URL.createObjectURL(
  1819. new window.Blob(
  1820. ['self.onmessage = ' + func.toString()],
  1821. {type:"text/javascript"}
  1822. )
  1823. );
  1824. }
  1825. if(!this._data.core.working || force_processing) {
  1826. this._data.core.working = true;
  1827. w = new window.Worker(this._wrk);
  1828. w.onmessage = $.proxy(function (e) {
  1829. rslt.call(this, e.data, true);
  1830. try { w.terminate(); w = null; } catch(ignore) { }
  1831. if(this._data.core.worker_queue.length) {
  1832. this._append_json_data.apply(this, this._data.core.worker_queue.shift());
  1833. }
  1834. else {
  1835. this._data.core.working = false;
  1836. }
  1837. }, this);
  1838. if(!args.par) {
  1839. if(this._data.core.worker_queue.length) {
  1840. this._append_json_data.apply(this, this._data.core.worker_queue.shift());
  1841. }
  1842. else {
  1843. this._data.core.working = false;
  1844. }
  1845. }
  1846. else {
  1847. w.postMessage(args);
  1848. }
  1849. }
  1850. else {
  1851. this._data.core.worker_queue.push([dom, data, cb, true]);
  1852. }
  1853. }
  1854. catch(e) {
  1855. rslt.call(this, func(args), false);
  1856. if(this._data.core.worker_queue.length) {
  1857. this._append_json_data.apply(this, this._data.core.worker_queue.shift());
  1858. }
  1859. else {
  1860. this._data.core.working = false;
  1861. }
  1862. }
  1863. }
  1864. else {
  1865. rslt.call(this, func(args), false);
  1866. }
  1867. },
  1868. /**
  1869. * parses a node from a jQuery object and appends them to the in memory tree model. Used internally.
  1870. * @private
  1871. * @name _parse_model_from_html(d [, p, ps])
  1872. * @param {jQuery} d the jQuery object to parse
  1873. * @param {String} p the parent ID
  1874. * @param {Array} ps list of all parents
  1875. * @return {String} the ID of the object added to the model
  1876. */
  1877. _parse_model_from_html : function (d, p, ps) {
  1878. if(!ps) { ps = []; }
  1879. else { ps = [].concat(ps); }
  1880. if(p) { ps.unshift(p); }
  1881. var c, e, m = this._model.data,
  1882. data = {
  1883. id : false,
  1884. text : false,
  1885. icon : true,
  1886. parent : p,
  1887. parents : ps,
  1888. children : [],
  1889. children_d : [],
  1890. data : null,
  1891. state : { },
  1892. li_attr : { id : false },
  1893. a_attr : { href : '#' },
  1894. original : false
  1895. }, i, tmp, tid;
  1896. for(i in this._model.default_state) {
  1897. if(this._model.default_state.hasOwnProperty(i)) {
  1898. data.state[i] = this._model.default_state[i];
  1899. }
  1900. }
  1901. tmp = $.vakata.attributes(d, true);
  1902. $.each(tmp, function (i, v) {
  1903. v = $.trim(v);
  1904. if(!v.length) { return true; }
  1905. data.li_attr[i] = v;
  1906. if(i === 'id') {
  1907. data.id = v.toString();
  1908. }
  1909. });
  1910. tmp = d.children('a').first();
  1911. if(tmp.length) {
  1912. tmp = $.vakata.attributes(tmp, true);
  1913. $.each(tmp, function (i, v) {
  1914. v = $.trim(v);
  1915. if(v.length) {
  1916. data.a_attr[i] = v;
  1917. }
  1918. });
  1919. }
  1920. tmp = d.children("a").first().length ? d.children("a").first().clone() : d.clone();
  1921. tmp.children("ins, i, ul").remove();
  1922. tmp = tmp.html();
  1923. tmp = $('<div />').html(tmp);
  1924. data.text = this.settings.core.force_text ? tmp.text() : tmp.html();
  1925. tmp = d.data();
  1926. data.data = tmp ? $.extend(true, {}, tmp) : null;
  1927. data.state.opened = d.hasClass('jstree-open');
  1928. data.state.selected = d.children('a').hasClass('jstree-clicked');
  1929. data.state.disabled = d.children('a').hasClass('jstree-disabled');
  1930. if(data.data && data.data.jstree) {
  1931. for(i in data.data.jstree) {
  1932. if(data.data.jstree.hasOwnProperty(i)) {
  1933. data.state[i] = data.data.jstree[i];
  1934. }
  1935. }
  1936. }
  1937. tmp = d.children("a").children(".jstree-themeicon");
  1938. if(tmp.length) {
  1939. data.icon = tmp.hasClass('jstree-themeicon-hidden') ? false : tmp.attr('rel');
  1940. }
  1941. if(data.state.icon !== undefined) {
  1942. data.icon = data.state.icon;
  1943. }
  1944. if(data.icon === undefined || data.icon === null || data.icon === "") {
  1945. data.icon = true;
  1946. }
  1947. tmp = d.children("ul").children("li");
  1948. do {
  1949. tid = 'j' + this._id + '_' + (++this._cnt);
  1950. } while(m[tid]);
  1951. data.id = data.li_attr.id ? data.li_attr.id.toString() : tid;
  1952. if(tmp.length) {
  1953. tmp.each($.proxy(function (i, v) {
  1954. c = this._parse_model_from_html($(v), data.id, ps);
  1955. e = this._model.data[c];
  1956. data.children.push(c);
  1957. if(e.children_d.length) {
  1958. data.children_d = data.children_d.concat(e.children_d);
  1959. }
  1960. }, this));
  1961. data.children_d = data.children_d.concat(data.children);
  1962. }
  1963. else {
  1964. if(d.hasClass('jstree-closed')) {
  1965. data.state.loaded = false;
  1966. }
  1967. }
  1968. if(data.li_attr['class']) {
  1969. data.li_attr['class'] = data.li_attr['class'].replace('jstree-closed','').replace('jstree-open','');
  1970. }
  1971. if(data.a_attr['class']) {
  1972. data.a_attr['class'] = data.a_attr['class'].replace('jstree-clicked','').replace('jstree-disabled','');
  1973. }
  1974. m[data.id] = data;
  1975. if(data.state.selected) {
  1976. this._data.core.selected.push(data.id);
  1977. }
  1978. return data.id;
  1979. },
  1980. /**
  1981. * 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.
  1982. * @private
  1983. * @name _parse_model_from_flat_json(d [, p, ps])
  1984. * @param {Object} d the JSON object to parse
  1985. * @param {String} p the parent ID
  1986. * @param {Array} ps list of all parents
  1987. * @return {String} the ID of the object added to the model
  1988. */
  1989. _parse_model_from_flat_json : function (d, p, ps) {
  1990. if(!ps) { ps = []; }
  1991. else { ps = ps.concat(); }
  1992. if(p) { ps.unshift(p); }
  1993. var tid = d.id.toString(),
  1994. m = this._model.data,
  1995. df = this._model.default_state,
  1996. i, j, c, e,
  1997. tmp = {
  1998. id : tid,
  1999. text : d.text || '',
  2000. icon : d.icon !== undefined ? d.icon : true,
  2001. parent : p,
  2002. parents : ps,
  2003. children : d.children || [],
  2004. children_d : d.children_d || [],
  2005. data : d.data,
  2006. state : { },
  2007. li_attr : { id : false },
  2008. a_attr : { href : '#' },
  2009. original : false
  2010. };
  2011. for(i in df) {
  2012. if(df.hasOwnProperty(i)) {
  2013. tmp.state[i] = df[i];
  2014. }
  2015. }
  2016. if(d && d.data && d.data.jstree && d.data.jstree.icon) {
  2017. tmp.icon = d.data.jstree.icon;
  2018. }
  2019. if(tmp.icon === undefined || tmp.icon === null || tmp.icon === "") {
  2020. tmp.icon = true;
  2021. }
  2022. if(d && d.data) {
  2023. tmp.data = d.data;
  2024. if(d.data.jstree) {
  2025. for(i in d.data.jstree) {
  2026. if(d.data.jstree.hasOwnProperty(i)) {
  2027. tmp.state[i] = d.data.jstree[i];
  2028. }
  2029. }
  2030. }
  2031. }
  2032. if(d && typeof d.state === 'object') {
  2033. for (i in d.state) {
  2034. if(d.state.hasOwnProperty(i)) {
  2035. tmp.state[i] = d.state[i];
  2036. }
  2037. }
  2038. }
  2039. if(d && typeof d.li_attr === 'object') {
  2040. for (i in d.li_attr) {
  2041. if(d.li_attr.hasOwnProperty(i)) {
  2042. tmp.li_attr[i] = d.li_attr[i];
  2043. }
  2044. }
  2045. }
  2046. if(!tmp.li_attr.id) {
  2047. tmp.li_attr.id = tid;
  2048. }
  2049. if(d && typeof d.a_attr === 'object') {
  2050. for (i in d.a_attr) {
  2051. if(d.a_attr.hasOwnProperty(i)) {
  2052. tmp.a_attr[i] = d.a_attr[i];
  2053. }
  2054. }
  2055. }
  2056. if(d && d.children && d.children === true) {
  2057. tmp.state.loaded = false;
  2058. tmp.children = [];
  2059. tmp.children_d = [];
  2060. }
  2061. m[tmp.id] = tmp;
  2062. for(i = 0, j = tmp.children.length; i < j; i++) {
  2063. c = this._parse_model_from_flat_json(m[tmp.children[i]], tmp.id, ps);
  2064. e = m[c];
  2065. tmp.children_d.push(c);
  2066. if(e.children_d.length) {
  2067. tmp.children_d = tmp.children_d.concat(e.children_d);
  2068. }
  2069. }
  2070. delete d.data;
  2071. delete d.children;
  2072. m[tmp.id].original = d;
  2073. if(tmp.state.selected) {
  2074. this._data.core.selected.push(tmp.id);
  2075. }
  2076. return tmp.id;
  2077. },
  2078. /**
  2079. * parses a node from a JSON object and appends it to the in memory tree model. Used internally.
  2080. * @private
  2081. * @name _parse_model_from_json(d [, p, ps])
  2082. * @param {Object} d the JSON object to parse
  2083. * @param {String} p the parent ID
  2084. * @param {Array} ps list of all parents
  2085. * @return {String} the ID of the object added to the model
  2086. */
  2087. _parse_model_from_json : function (d, p, ps) {
  2088. if(!ps) { ps = []; }
  2089. else { ps = ps.concat(); }
  2090. if(p) { ps.unshift(p); }
  2091. var tid = false, i, j, c, e, m = this._model.data, df = this._model.default_state, tmp;
  2092. do {
  2093. tid = 'j' + this._id + '_' + (++this._cnt);
  2094. } while(m[tid]);
  2095. tmp = {
  2096. id : false,
  2097. text : typeof d === 'string' ? d : '',
  2098. icon : typeof d === 'object' && d.icon !== undefined ? d.icon : true,
  2099. parent : p,
  2100. parents : ps,
  2101. children : [],
  2102. children_d : [],
  2103. data : null,
  2104. state : { },
  2105. li_attr : { id : false },
  2106. a_attr : { href : '#' },
  2107. original : false
  2108. };
  2109. for(i in df) {
  2110. if(df.hasOwnProperty(i)) {
  2111. tmp.state[i] = df[i];
  2112. }
  2113. }
  2114. if(d && d.id) { tmp.id = d.id.toString(); }
  2115. if(d && d.text) { tmp.text = d.text; }
  2116. if(d && d.data && d.data.jstree && d.data.jstree.icon) {
  2117. tmp.icon = d.data.jstree.icon;
  2118. }
  2119. if(tmp.icon === undefined || tmp.icon === null || tmp.icon === "") {
  2120. tmp.icon = true;
  2121. }
  2122. if(d && d.data) {
  2123. tmp.data = d.data;
  2124. if(d.data.jstree) {
  2125. for(i in d.data.jstree) {
  2126. if(d.data.jstree.hasOwnProperty(i)) {
  2127. tmp.state[i] = d.data.jstree[i];
  2128. }
  2129. }
  2130. }
  2131. }
  2132. if(d && typeof d.state === 'object') {
  2133. for (i in d.state) {
  2134. if(d.state.hasOwnProperty(i)) {
  2135. tmp.state[i] = d.state[i];
  2136. }
  2137. }
  2138. }
  2139. if(d && typeof d.li_attr === 'object') {
  2140. for (i in d.li_attr) {
  2141. if(d.li_attr.hasOwnProperty(i)) {
  2142. tmp.li_attr[i] = d.li_attr[i];
  2143. }
  2144. }
  2145. }
  2146. if(tmp.li_attr.id && !tmp.id) {
  2147. tmp.id = tmp.li_attr.id.toString();
  2148. }
  2149. if(!tmp.id) {
  2150. tmp.id = tid;
  2151. }
  2152. if(!tmp.li_attr.id) {
  2153. tmp.li_attr.id = tmp.id;
  2154. }
  2155. if(d && typeof d.a_attr === 'object') {
  2156. for (i in d.a_attr) {
  2157. if(d.a_attr.hasOwnProperty(i)) {
  2158. tmp.a_attr[i] = d.a_attr[i];
  2159. }
  2160. }
  2161. }
  2162. if(d && d.children && d.children.length) {
  2163. for(i = 0, j = d.children.length; i < j; i++) {
  2164. c = this._parse_model_from_json(d.children[i], tmp.id, ps);
  2165. e = m[c];
  2166. tmp.children.push(c);
  2167. if(e.children_d.length) {
  2168. tmp.children_d = tmp.children_d.concat(e.children_d);
  2169. }
  2170. }
  2171. tmp.children_d = tmp.children_d.concat(tmp.children);
  2172. }
  2173. if(d && d.children && d.children === true) {
  2174. tmp.state.loaded = false;
  2175. tmp.children = [];
  2176. tmp.children_d = [];
  2177. }
  2178. delete d.data;
  2179. delete d.children;
  2180. tmp.original = d;
  2181. m[tmp.id] = tmp;
  2182. if(tmp.state.selected) {
  2183. this._data.core.selected.push(tmp.id);
  2184. }
  2185. return tmp.id;
  2186. },
  2187. /**
  2188. * redraws all nodes that need to be redrawn. Used internally.
  2189. * @private
  2190. * @name _redraw()
  2191. * @trigger redraw.jstree
  2192. */
  2193. _redraw : function () {
  2194. var nodes = this._model.force_full_redraw ? this._model.data[$.jstree.root].children.concat([]) : this._model.changed.concat([]),
  2195. f = document.createElement('UL'), tmp, i, j, fe = this._data.core.focused;
  2196. for(i = 0, j = nodes.length; i < j; i++) {
  2197. tmp = this.redraw_node(nodes[i], true, this._model.force_full_redraw);
  2198. if(tmp && this._model.force_full_redraw) {
  2199. f.appendChild(tmp);
  2200. }
  2201. }
  2202. if(this._model.force_full_redraw) {
  2203. f.className = this.get_container_ul()[0].className;
  2204. f.setAttribute('role','group');
  2205. this.element.empty().append(f);
  2206. //this.get_container_ul()[0].appendChild(f);
  2207. }
  2208. if(fe !== null) {
  2209. tmp = this.get_node(fe, true);
  2210. if(tmp && tmp.length && tmp.children('.jstree-anchor')[0] !== document.activeElement) {
  2211. tmp.children('.jstree-anchor').focus();
  2212. }
  2213. else {
  2214. this._data.core.focused = null;
  2215. }
  2216. }
  2217. this._model.force_full_redraw = false;
  2218. this._model.changed = [];
  2219. /**
  2220. * triggered after nodes are redrawn
  2221. * @event
  2222. * @name redraw.jstree
  2223. * @param {array} nodes the redrawn nodes
  2224. */
  2225. this.trigger('redraw', { "nodes" : nodes });
  2226. },
  2227. /**
  2228. * redraws all nodes that need to be redrawn or optionally - the whole tree
  2229. * @name redraw([full])
  2230. * @param {Boolean} full if set to `true` all nodes are redrawn.
  2231. */
  2232. redraw : function (full) {
  2233. if(full) {
  2234. this._model.force_full_redraw = true;
  2235. }
  2236. //if(this._model.redraw_timeout) {
  2237. // clearTimeout(this._model.redraw_timeout);
  2238. //}
  2239. //this._model.redraw_timeout = setTimeout($.proxy(this._redraw, this),0);
  2240. this._redraw();
  2241. },
  2242. /**
  2243. * redraws a single node's children. Used internally.
  2244. * @private
  2245. * @name draw_children(node)
  2246. * @param {mixed} node the node whose children will be redrawn
  2247. */
  2248. draw_children : function (node) {
  2249. var obj = this.get_node(node),
  2250. i = false,
  2251. j = false,
  2252. k = false,
  2253. d = document;
  2254. if(!obj) { return false; }
  2255. if(obj.id === $.jstree.root) { return this.redraw(true); }
  2256. node = this.get_node(node, true);
  2257. if(!node || !node.length) { return false; } // TODO: quick toggle
  2258. node.children('.jstree-children').remove();
  2259. node = node[0];
  2260. if(obj.children.length && obj.state.loaded) {
  2261. k = d.createElement('UL');
  2262. k.setAttribute('role', 'group');
  2263. k.className = 'jstree-children';
  2264. for(i = 0, j = obj.children.length; i < j; i++) {
  2265. k.appendChild(this.redraw_node(obj.children[i], true, true));
  2266. }
  2267. node.appendChild(k);
  2268. }
  2269. },
  2270. /**
  2271. * redraws a single node. Used internally.
  2272. * @private
  2273. * @name redraw_node(node, deep, is_callback, force_render)
  2274. * @param {mixed} node the node to redraw
  2275. * @param {Boolean} deep should child nodes be redrawn too
  2276. * @param {Boolean} is_callback is this a recursion call
  2277. * @param {Boolean} force_render should children of closed parents be drawn anyway
  2278. */
  2279. redraw_node : function (node, deep, is_callback, force_render) {
  2280. var obj = this.get_node(node),
  2281. par = false,
  2282. ind = false,
  2283. old = false,
  2284. i = false,
  2285. j = false,
  2286. k = false,
  2287. c = '',
  2288. d = document,
  2289. m = this._model.data,
  2290. f = false,
  2291. s = false,
  2292. tmp = null,
  2293. t = 0,
  2294. l = 0,
  2295. has_children = false,
  2296. last_sibling = false;
  2297. if(!obj) { return false; }
  2298. if(obj.id === $.jstree.root) { return this.redraw(true); }
  2299. deep = deep || obj.children.length === 0;
  2300. 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);
  2301. if(!node) {
  2302. deep = true;
  2303. //node = d.createElement('LI');
  2304. if(!is_callback) {
  2305. par = obj.parent !== $.jstree.root ? $('#' + obj.parent.replace($.jstree.idregex,'\\$&'), this.element)[0] : null;
  2306. if(par !== null && (!par || !m[obj.parent].state.opened)) {
  2307. return false;
  2308. }
  2309. ind = $.inArray(obj.id, par === null ? m[$.jstree.root].children : m[obj.parent].children);
  2310. }
  2311. }
  2312. else {
  2313. node = $(node);
  2314. if(!is_callback) {
  2315. par = node.parent().parent()[0];
  2316. if(par === this.element[0]) {
  2317. par = null;
  2318. }
  2319. ind = node.index();
  2320. }
  2321. // m[obj.id].data = node.data(); // use only node's data, no need to touch jquery storage
  2322. if(!deep && obj.children.length && !node.children('.jstree-children').length) {
  2323. deep = true;
  2324. }
  2325. if(!deep) {
  2326. old = node.children('.jstree-children')[0];
  2327. }
  2328. f = node.children('.jstree-anchor')[0] === document.activeElement;
  2329. node.remove();
  2330. //node = d.createElement('LI');
  2331. //node = node[0];
  2332. }
  2333. node = _node.cloneNode(true);
  2334. // node is DOM, deep is boolean
  2335. c = 'jstree-node ';
  2336. for(i in obj.li_attr) {
  2337. if(obj.li_attr.hasOwnProperty(i)) {
  2338. if(i === 'id') { continue; }
  2339. if(i !== 'class') {
  2340. node.setAttribute(i, obj.li_attr[i]);
  2341. }
  2342. else {
  2343. c += obj.li_attr[i];
  2344. }
  2345. }
  2346. }
  2347. if(!obj.a_attr.id) {
  2348. obj.a_attr.id = obj.id + '_anchor';
  2349. }
  2350. node.setAttribute('aria-selected', !!obj.state.selected);
  2351. node.setAttribute('aria-level', obj.parents.length);
  2352. node.setAttribute('aria-labelledby', obj.a_attr.id);
  2353. if(obj.state.disabled) {
  2354. node.setAttribute('aria-disabled', true);
  2355. }
  2356. for(i = 0, j = obj.children.length; i < j; i++) {
  2357. if(!m[obj.children[i]].state.hidden) {
  2358. has_children = true;
  2359. break;
  2360. }
  2361. }
  2362. if(obj.parent !== null && m[obj.parent] && !obj.state.hidden) {
  2363. i = $.inArray(obj.id, m[obj.parent].children);
  2364. last_sibling = obj.id;
  2365. if(i !== -1) {
  2366. i++;
  2367. for(j = m[obj.parent].children.length; i < j; i++) {
  2368. if(!m[m[obj.parent].children[i]].state.hidden) {
  2369. last_sibling = m[obj.parent].children[i];
  2370. }
  2371. if(last_sibling !== obj.id) {
  2372. break;
  2373. }
  2374. }
  2375. }
  2376. }
  2377. if(obj.state.hidden) {
  2378. c += ' jstree-hidden';
  2379. }
  2380. if(obj.state.loaded && !has_children) {
  2381. c += ' jstree-leaf';
  2382. }
  2383. else {
  2384. c += obj.state.opened && obj.state.loaded ? ' jstree-open' : ' jstree-closed';
  2385. node.setAttribute('aria-expanded', (obj.state.opened && obj.state.loaded) );
  2386. }
  2387. if(last_sibling === obj.id) {
  2388. c += ' jstree-last';
  2389. }
  2390. node.id = obj.id;
  2391. node.className = c;
  2392. c = ( obj.state.selected ? ' jstree-clicked' : '') + ( obj.state.disabled ? ' jstree-disabled' : '');
  2393. for(j in obj.a_attr) {
  2394. if(obj.a_attr.hasOwnProperty(j)) {
  2395. if(j === 'href' && obj.a_attr[j] === '#') { continue; }
  2396. if(j !== 'class') {
  2397. node.childNodes[1].setAttribute(j, obj.a_attr[j]);
  2398. }
  2399. else {
  2400. c += ' ' + obj.a_attr[j];
  2401. }
  2402. }
  2403. }
  2404. if(c.length) {
  2405. node.childNodes[1].className = 'jstree-anchor ' + c;
  2406. }
  2407. if((obj.icon && obj.icon !== true) || obj.icon === false) {
  2408. if(obj.icon === false) {
  2409. node.childNodes[1].childNodes[0].className += ' jstree-themeicon-hidden';
  2410. }
  2411. else if(obj.icon.indexOf('/') === -1 && obj.icon.indexOf('.') === -1) {
  2412. node.childNodes[1].childNodes[0].className += ' ' + obj.icon + ' jstree-themeicon-custom';
  2413. }
  2414. else {
  2415. node.childNodes[1].childNodes[0].style.backgroundImage = 'url("'+obj.icon+'")';
  2416. node.childNodes[1].childNodes[0].style.backgroundPosition = 'center center';
  2417. node.childNodes[1].childNodes[0].style.backgroundSize = 'auto';
  2418. node.childNodes[1].childNodes[0].className += ' jstree-themeicon-custom';
  2419. }
  2420. }
  2421. if(this.settings.core.force_text) {
  2422. node.childNodes[1].appendChild(d.createTextNode(obj.text));
  2423. }
  2424. else {
  2425. node.childNodes[1].innerHTML += obj.text;
  2426. }
  2427. if(deep && obj.children.length && (obj.state.opened || force_render) && obj.state.loaded) {
  2428. k = d.createElement('UL');
  2429. k.setAttribute('role', 'group');
  2430. k.className = 'jstree-children';
  2431. for(i = 0, j = obj.children.length; i < j; i++) {
  2432. k.appendChild(this.redraw_node(obj.children[i], deep, true));
  2433. }
  2434. node.appendChild(k);
  2435. }
  2436. if(old) {
  2437. node.appendChild(old);
  2438. }
  2439. if(!is_callback) {
  2440. // append back using par / ind
  2441. if(!par) {
  2442. par = this.element[0];
  2443. }
  2444. for(i = 0, j = par.childNodes.length; i < j; i++) {
  2445. if(par.childNodes[i] && par.childNodes[i].className && par.childNodes[i].className.indexOf('jstree-children') !== -1) {
  2446. tmp = par.childNodes[i];
  2447. break;
  2448. }
  2449. }
  2450. if(!tmp) {
  2451. tmp = d.createElement('UL');
  2452. tmp.setAttribute('role', 'group');
  2453. tmp.className = 'jstree-children';
  2454. par.appendChild(tmp);
  2455. }
  2456. par = tmp;
  2457. if(ind < par.childNodes.length) {
  2458. par.insertBefore(node, par.childNodes[ind]);
  2459. }
  2460. else {
  2461. par.appendChild(node);
  2462. }
  2463. if(f) {
  2464. t = this.element[0].scrollTop;
  2465. l = this.element[0].scrollLeft;
  2466. node.childNodes[1].focus();
  2467. this.element[0].scrollTop = t;
  2468. this.element[0].scrollLeft = l;
  2469. }
  2470. }
  2471. if(obj.state.opened && !obj.state.loaded) {
  2472. obj.state.opened = false;
  2473. setTimeout($.proxy(function () {
  2474. this.open_node(obj.id, false, 0);
  2475. }, this), 0);
  2476. }
  2477. return node;
  2478. },
  2479. /**
  2480. * opens a node, revaling its children. If the node is not loaded it will be loaded and opened once ready.
  2481. * @name open_node(obj [, callback, animation])
  2482. * @param {mixed} obj the node to open
  2483. * @param {Function} callback a function to execute once the node is opened
  2484. * @param {Number} animation the animation duration in milliseconds when opening the node (overrides the `core.animation` setting). Use `false` for no animation.
  2485. * @trigger open_node.jstree, after_open.jstree, before_open.jstree
  2486. */
  2487. open_node : function (obj, callback, animation) {
  2488. var t1, t2, d, t;
  2489. if($.isArray(obj)) {
  2490. obj = obj.slice();
  2491. for(t1 = 0, t2 = obj.length; t1 < t2; t1++) {
  2492. this.open_node(obj[t1], callback, animation);
  2493. }
  2494. return true;
  2495. }
  2496. obj = this.get_node(obj);
  2497. if(!obj || obj.id === $.jstree.root) {
  2498. return false;
  2499. }
  2500. animation = animation === undefined ? this.settings.core.animation : animation;
  2501. if(!this.is_closed(obj)) {
  2502. if(callback) {
  2503. callback.call(this, obj, false);
  2504. }
  2505. return false;
  2506. }
  2507. if(!this.is_loaded(obj)) {
  2508. if(this.is_loading(obj)) {
  2509. return setTimeout($.proxy(function () {
  2510. this.open_node(obj, callback, animation);
  2511. }, this), 500);
  2512. }
  2513. this.load_node(obj, function (o, ok) {
  2514. return ok ? this.open_node(o, callback, animation) : (callback ? callback.call(this, o, false) : false);
  2515. });
  2516. }
  2517. else {
  2518. d = this.get_node(obj, true);
  2519. t = this;
  2520. if(d.length) {
  2521. if(animation && d.children(".jstree-children").length) {
  2522. d.children(".jstree-children").stop(true, true);
  2523. }
  2524. if(obj.children.length && !this._firstChild(d.children('.jstree-children')[0])) {
  2525. this.draw_children(obj);
  2526. //d = this.get_node(obj, true);
  2527. }
  2528. if(!animation) {
  2529. this.trigger('before_open', { "node" : obj });
  2530. d[0].className = d[0].className.replace('jstree-closed', 'jstree-open');
  2531. d[0].setAttribute("aria-expanded", true);
  2532. }
  2533. else {
  2534. this.trigger('before_open', { "node" : obj });
  2535. d
  2536. .children(".jstree-children").css("display","none").end()
  2537. .removeClass("jstree-closed").addClass("jstree-open").attr("aria-expanded", true)
  2538. .children(".jstree-children").stop(true, true)
  2539. .slideDown(animation, function () {
  2540. this.style.display = "";
  2541. if (t.element) {
  2542. t.trigger("after_open", { "node" : obj });
  2543. }
  2544. });
  2545. }
  2546. }
  2547. obj.state.opened = true;
  2548. if(callback) {
  2549. callback.call(this, obj, true);
  2550. }
  2551. if(!d.length) {
  2552. /**
  2553. * 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)
  2554. * @event
  2555. * @name before_open.jstree
  2556. * @param {Object} node the opened node
  2557. */
  2558. this.trigger('before_open', { "node" : obj });
  2559. }
  2560. /**
  2561. * triggered when a node is opened (if there is an animation it will not be completed yet)
  2562. * @event
  2563. * @name open_node.jstree
  2564. * @param {Object} node the opened node
  2565. */
  2566. this.trigger('open_node', { "node" : obj });
  2567. if(!animation || !d.length) {
  2568. /**
  2569. * triggered when a node is opened and the animation is complete
  2570. * @event
  2571. * @name after_open.jstree
  2572. * @param {Object} node the opened node
  2573. */
  2574. this.trigger("after_open", { "node" : obj });
  2575. }
  2576. return true;
  2577. }
  2578. },
  2579. /**
  2580. * opens every parent of a node (node should be loaded)
  2581. * @name _open_to(obj)
  2582. * @param {mixed} obj the node to reveal
  2583. * @private
  2584. */
  2585. _open_to : function (obj) {
  2586. obj = this.get_node(obj);
  2587. if(!obj || obj.id === $.jstree.root) {
  2588. return false;
  2589. }
  2590. var i, j, p = obj.parents;
  2591. for(i = 0, j = p.length; i < j; i+=1) {
  2592. if(i !== $.jstree.root) {
  2593. this.open_node(p[i], false, 0);
  2594. }
  2595. }
  2596. return $('#' + obj.id.replace($.jstree.idregex,'\\$&'), this.element);
  2597. },
  2598. /**
  2599. * closes a node, hiding its children
  2600. * @name close_node(obj [, animation])
  2601. * @param {mixed} obj the node to close
  2602. * @param {Number} animation the animation duration in milliseconds when closing the node (overrides the `core.animation` setting). Use `false` for no animation.
  2603. * @trigger close_node.jstree, after_close.jstree
  2604. */
  2605. close_node : function (obj, animation) {
  2606. var t1, t2, t, d;
  2607. if($.isArray(obj)) {
  2608. obj = obj.slice();
  2609. for(t1 = 0, t2 = obj.length; t1 < t2; t1++) {
  2610. this.close_node(obj[t1], animation);
  2611. }
  2612. return true;
  2613. }
  2614. obj = this.get_node(obj);
  2615. if(!obj || obj.id === $.jstree.root) {
  2616. return false;
  2617. }
  2618. if(this.is_closed(obj)) {
  2619. return false;
  2620. }
  2621. animation = animation === undefined ? this.settings.core.animation : animation;
  2622. t = this;
  2623. d = this.get_node(obj, true);
  2624. obj.state.opened = false;
  2625. /**
  2626. * triggered when a node is closed (if there is an animation it will not be complete yet)
  2627. * @event
  2628. * @name close_node.jstree
  2629. * @param {Object} node the closed node
  2630. */
  2631. this.trigger('close_node',{ "node" : obj });
  2632. if(!d.length) {
  2633. /**
  2634. * triggered when a node is closed and the animation is complete
  2635. * @event
  2636. * @name after_close.jstree
  2637. * @param {Object} node the closed node
  2638. */
  2639. this.trigger("after_close", { "node" : obj });
  2640. }
  2641. else {
  2642. if(!animation) {
  2643. d[0].className = d[0].className.replace('jstree-open', 'jstree-closed');
  2644. d.attr("aria-expanded", false).children('.jstree-children').remove();
  2645. this.trigger("after_close", { "node" : obj });
  2646. }
  2647. else {
  2648. d
  2649. .children(".jstree-children").attr("style","display:block !important").end()
  2650. .removeClass("jstree-open").addClass("jstree-closed").attr("aria-expanded", false)
  2651. .children(".jstree-children").stop(true, true).slideUp(animation, function () {
  2652. this.style.display = "";
  2653. d.children('.jstree-children').remove();
  2654. if (t.element) {
  2655. t.trigger("after_close", { "node" : obj });
  2656. }
  2657. });
  2658. }
  2659. }
  2660. },
  2661. /**
  2662. * toggles a node - closing it if it is open, opening it if it is closed
  2663. * @name toggle_node(obj)
  2664. * @param {mixed} obj the node to toggle
  2665. */
  2666. toggle_node : function (obj) {
  2667. var t1, t2;
  2668. if($.isArray(obj)) {
  2669. obj = obj.slice();
  2670. for(t1 = 0, t2 = obj.length; t1 < t2; t1++) {
  2671. this.toggle_node(obj[t1]);
  2672. }
  2673. return true;
  2674. }
  2675. if(this.is_closed(obj)) {
  2676. return this.open_node(obj);
  2677. }
  2678. if(this.is_open(obj)) {
  2679. return this.close_node(obj);
  2680. }
  2681. },
  2682. /**
  2683. * opens all nodes within a node (or the tree), revaling their children. If the node is not loaded it will be loaded and opened once ready.
  2684. * @name open_all([obj, animation, original_obj])
  2685. * @param {mixed} obj the node to open recursively, omit to open all nodes in the tree
  2686. * @param {Number} animation the animation duration in milliseconds when opening the nodes, the default is no animation
  2687. * @param {jQuery} reference to the node that started the process (internal use)
  2688. * @trigger open_all.jstree
  2689. */
  2690. open_all : function (obj, animation, original_obj) {
  2691. if(!obj) { obj = $.jstree.root; }
  2692. obj = this.get_node(obj);
  2693. if(!obj) { return false; }
  2694. var dom = obj.id === $.jstree.root ? this.get_container_ul() : this.get_node(obj, true), i, j, _this;
  2695. if(!dom.length) {
  2696. for(i = 0, j = obj.children_d.length; i < j; i++) {
  2697. if(this.is_closed(this._model.data[obj.children_d[i]])) {
  2698. this._model.data[obj.children_d[i]].state.opened = true;
  2699. }
  2700. }
  2701. return this.trigger('open_all', { "node" : obj });
  2702. }
  2703. original_obj = original_obj || dom;
  2704. _this = this;
  2705. dom = this.is_closed(obj) ? dom.find('.jstree-closed').addBack() : dom.find('.jstree-closed');
  2706. dom.each(function () {
  2707. _this.open_node(
  2708. this,
  2709. function(node, status) { if(status && this.is_parent(node)) { this.open_all(node, animation, original_obj); } },
  2710. animation || 0
  2711. );
  2712. });
  2713. if(original_obj.find('.jstree-closed').length === 0) {
  2714. /**
  2715. * triggered when an `open_all` call completes
  2716. * @event
  2717. * @name open_all.jstree
  2718. * @param {Object} node the opened node
  2719. */
  2720. this.trigger('open_all', { "node" : this.get_node(original_obj) });
  2721. }
  2722. },
  2723. /**
  2724. * closes all nodes within a node (or the tree), revaling their children
  2725. * @name close_all([obj, animation])
  2726. * @param {mixed} obj the node to close recursively, omit to close all nodes in the tree
  2727. * @param {Number} animation the animation duration in milliseconds when closing the nodes, the default is no animation
  2728. * @trigger close_all.jstree
  2729. */
  2730. close_all : function (obj, animation) {
  2731. if(!obj) { obj = $.jstree.root; }
  2732. obj = this.get_node(obj);
  2733. if(!obj) { return false; }
  2734. var dom = obj.id === $.jstree.root ? this.get_container_ul() : this.get_node(obj, true),
  2735. _this = this, i, j;
  2736. if(dom.length) {
  2737. dom = this.is_open(obj) ? dom.find('.jstree-open').addBack() : dom.find('.jstree-open');
  2738. $(dom.get().reverse()).each(function () { _this.close_node(this, animation || 0); });
  2739. }
  2740. for(i = 0, j = obj.children_d.length; i < j; i++) {
  2741. this._model.data[obj.children_d[i]].state.opened = false;
  2742. }
  2743. /**
  2744. * triggered when an `close_all` call completes
  2745. * @event
  2746. * @name close_all.jstree
  2747. * @param {Object} node the closed node
  2748. */
  2749. this.trigger('close_all', { "node" : obj });
  2750. },
  2751. /**
  2752. * checks if a node is disabled (not selectable)
  2753. * @name is_disabled(obj)
  2754. * @param {mixed} obj
  2755. * @return {Boolean}
  2756. */
  2757. is_disabled : function (obj) {
  2758. obj = this.get_node(obj);
  2759. return obj && obj.state && obj.state.disabled;
  2760. },
  2761. /**
  2762. * enables a node - so that it can be selected
  2763. * @name enable_node(obj)
  2764. * @param {mixed} obj the node to enable
  2765. * @trigger enable_node.jstree
  2766. */
  2767. enable_node : function (obj) {
  2768. var t1, t2;
  2769. if($.isArray(obj)) {
  2770. obj = obj.slice();
  2771. for(t1 = 0, t2 = obj.length; t1 < t2; t1++) {
  2772. this.enable_node(obj[t1]);
  2773. }
  2774. return true;
  2775. }
  2776. obj = this.get_node(obj);
  2777. if(!obj || obj.id === $.jstree.root) {
  2778. return false;
  2779. }
  2780. obj.state.disabled = false;
  2781. this.get_node(obj,true).children('.jstree-anchor').removeClass('jstree-disabled').attr('aria-disabled', false);
  2782. /**
  2783. * triggered when an node is enabled
  2784. * @event
  2785. * @name enable_node.jstree
  2786. * @param {Object} node the enabled node
  2787. */
  2788. this.trigger('enable_node', { 'node' : obj });
  2789. },
  2790. /**
  2791. * disables a node - so that it can not be selected
  2792. * @name disable_node(obj)
  2793. * @param {mixed} obj the node to disable
  2794. * @trigger disable_node.jstree
  2795. */
  2796. disable_node : function (obj) {
  2797. var t1, t2;
  2798. if($.isArray(obj)) {
  2799. obj = obj.slice();
  2800. for(t1 = 0, t2 = obj.length; t1 < t2; t1++) {
  2801. this.disable_node(obj[t1]);
  2802. }
  2803. return true;
  2804. }
  2805. obj = this.get_node(obj);
  2806. if(!obj || obj.id === $.jstree.root) {
  2807. return false;
  2808. }
  2809. obj.state.disabled = true;
  2810. this.get_node(obj,true).children('.jstree-anchor').addClass('jstree-disabled').attr('aria-disabled', true);
  2811. /**
  2812. * triggered when an node is disabled
  2813. * @event
  2814. * @name disable_node.jstree
  2815. * @param {Object} node the disabled node
  2816. */
  2817. this.trigger('disable_node', { 'node' : obj });
  2818. },
  2819. /**
  2820. * determines if a node is hidden
  2821. * @name is_hidden(obj)
  2822. * @param {mixed} obj the node
  2823. */
  2824. is_hidden : function (obj) {
  2825. obj = this.get_node(obj);
  2826. return obj.state.hidden === true;
  2827. },
  2828. /**
  2829. * hides a node - it is still in the structure but will not be visible
  2830. * @name hide_node(obj)
  2831. * @param {mixed} obj the node to hide
  2832. * @param {Boolean} redraw internal parameter controlling if redraw is called
  2833. * @trigger hide_node.jstree
  2834. */
  2835. hide_node : function (obj, skip_redraw) {
  2836. var t1, t2;
  2837. if($.isArray(obj)) {
  2838. obj = obj.slice();
  2839. for(t1 = 0, t2 = obj.length; t1 < t2; t1++) {
  2840. this.hide_node(obj[t1], true);
  2841. }
  2842. if (!skip_redraw) {
  2843. this.redraw();
  2844. }
  2845. return true;
  2846. }
  2847. obj = this.get_node(obj);
  2848. if(!obj || obj.id === $.jstree.root) {
  2849. return false;
  2850. }
  2851. if(!obj.state.hidden) {
  2852. obj.state.hidden = true;
  2853. this._node_changed(obj.parent);
  2854. if(!skip_redraw) {
  2855. this.redraw();
  2856. }
  2857. /**
  2858. * triggered when an node is hidden
  2859. * @event
  2860. * @name hide_node.jstree
  2861. * @param {Object} node the hidden node
  2862. */
  2863. this.trigger('hide_node', { 'node' : obj });
  2864. }
  2865. },
  2866. /**
  2867. * shows a node
  2868. * @name show_node(obj)
  2869. * @param {mixed} obj the node to show
  2870. * @param {Boolean} skip_redraw internal parameter controlling if redraw is called
  2871. * @trigger show_node.jstree
  2872. */
  2873. show_node : function (obj, skip_redraw) {
  2874. var t1, t2;
  2875. if($.isArray(obj)) {
  2876. obj = obj.slice();
  2877. for(t1 = 0, t2 = obj.length; t1 < t2; t1++) {
  2878. this.show_node(obj[t1], true);
  2879. }
  2880. if (!skip_redraw) {
  2881. this.redraw();
  2882. }
  2883. return true;
  2884. }
  2885. obj = this.get_node(obj);
  2886. if(!obj || obj.id === $.jstree.root) {
  2887. return false;
  2888. }
  2889. if(obj.state.hidden) {
  2890. obj.state.hidden = false;
  2891. this._node_changed(obj.parent);
  2892. if(!skip_redraw) {
  2893. this.redraw();
  2894. }
  2895. /**
  2896. * triggered when an node is shown
  2897. * @event
  2898. * @name show_node.jstree
  2899. * @param {Object} node the shown node
  2900. */
  2901. this.trigger('show_node', { 'node' : obj });
  2902. }
  2903. },
  2904. /**
  2905. * hides all nodes
  2906. * @name hide_all()
  2907. * @trigger hide_all.jstree
  2908. */
  2909. hide_all : function (skip_redraw) {
  2910. var i, m = this._model.data, ids = [];
  2911. for(i in m) {
  2912. if(m.hasOwnProperty(i) && i !== $.jstree.root && !m[i].state.hidden) {
  2913. m[i].state.hidden = true;
  2914. ids.push(i);
  2915. }
  2916. }
  2917. this._model.force_full_redraw = true;
  2918. if(!skip_redraw) {
  2919. this.redraw();
  2920. }
  2921. /**
  2922. * triggered when all nodes are hidden
  2923. * @event
  2924. * @name hide_all.jstree
  2925. * @param {Array} nodes the IDs of all hidden nodes
  2926. */
  2927. this.trigger('hide_all', { 'nodes' : ids });
  2928. return ids;
  2929. },
  2930. /**
  2931. * shows all nodes
  2932. * @name show_all()
  2933. * @trigger show_all.jstree
  2934. */
  2935. show_all : function (skip_redraw) {
  2936. var i, m = this._model.data, ids = [];
  2937. for(i in m) {
  2938. if(m.hasOwnProperty(i) && i !== $.jstree.root && m[i].state.hidden) {
  2939. m[i].state.hidden = false;
  2940. ids.push(i);
  2941. }
  2942. }
  2943. this._model.force_full_redraw = true;
  2944. if(!skip_redraw) {
  2945. this.redraw();
  2946. }
  2947. /**
  2948. * triggered when all nodes are shown
  2949. * @event
  2950. * @name show_all.jstree
  2951. * @param {Array} nodes the IDs of all shown nodes
  2952. */
  2953. this.trigger('show_all', { 'nodes' : ids });
  2954. return ids;
  2955. },
  2956. /**
  2957. * called when a node is selected by the user. Used internally.
  2958. * @private
  2959. * @name activate_node(obj, e)
  2960. * @param {mixed} obj the node
  2961. * @param {Object} e the related event
  2962. * @trigger activate_node.jstree, changed.jstree
  2963. */
  2964. activate_node : function (obj, e) {
  2965. if(this.is_disabled(obj)) {
  2966. return false;
  2967. }
  2968. if(!e || typeof e !== 'object') {
  2969. e = {};
  2970. }
  2971. // 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
  2972. 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;
  2973. if(this._data.core.last_clicked && !this._data.core.last_clicked.state.selected) { this._data.core.last_clicked = null; }
  2974. 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]); }
  2975. 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 ) )) {
  2976. if(!this.settings.core.multiple && (e.metaKey || e.ctrlKey || e.shiftKey) && this.is_selected(obj)) {
  2977. this.deselect_node(obj, false, e);
  2978. }
  2979. else {
  2980. this.deselect_all(true);
  2981. this.select_node(obj, false, false, e);
  2982. this._data.core.last_clicked = this.get_node(obj);
  2983. }
  2984. }
  2985. else {
  2986. if(e.shiftKey) {
  2987. var o = this.get_node(obj).id,
  2988. l = this._data.core.last_clicked.id,
  2989. p = this.get_node(this._data.core.last_clicked.parent).children,
  2990. c = false,
  2991. i, j;
  2992. for(i = 0, j = p.length; i < j; i += 1) {
  2993. // separate IFs work whem o and l are the same
  2994. if(p[i] === o) {
  2995. c = !c;
  2996. }
  2997. if(p[i] === l) {
  2998. c = !c;
  2999. }
  3000. if(!this.is_disabled(p[i]) && (c || p[i] === o || p[i] === l)) {
  3001. if (!this.is_hidden(p[i])) {
  3002. this.select_node(p[i], true, false, e);
  3003. }
  3004. }
  3005. else {
  3006. this.deselect_node(p[i], true, e);
  3007. }
  3008. }
  3009. this.trigger('changed', { 'action' : 'select_node', 'node' : this.get_node(obj), 'selected' : this._data.core.selected, 'event' : e });
  3010. }
  3011. else {
  3012. if(!this.is_selected(obj)) {
  3013. this.select_node(obj, false, false, e);
  3014. }
  3015. else {
  3016. this.deselect_node(obj, false, e);
  3017. }
  3018. }
  3019. }
  3020. /**
  3021. * triggered when an node is clicked or intercated with by the user
  3022. * @event
  3023. * @name activate_node.jstree
  3024. * @param {Object} node
  3025. * @param {Object} event the ooriginal event (if any) which triggered the call (may be an empty object)
  3026. */
  3027. this.trigger('activate_node', { 'node' : this.get_node(obj), 'event' : e });
  3028. },
  3029. /**
  3030. * applies the hover state on a node, called when a node is hovered by the user. Used internally.
  3031. * @private
  3032. * @name hover_node(obj)
  3033. * @param {mixed} obj
  3034. * @trigger hover_node.jstree
  3035. */
  3036. hover_node : function (obj) {
  3037. obj = this.get_node(obj, true);
  3038. if(!obj || !obj.length || obj.children('.jstree-hovered').length) {
  3039. return false;
  3040. }
  3041. var o = this.element.find('.jstree-hovered'), t = this.element;
  3042. if(o && o.length) { this.dehover_node(o); }
  3043. obj.children('.jstree-anchor').addClass('jstree-hovered');
  3044. /**
  3045. * triggered when an node is hovered
  3046. * @event
  3047. * @name hover_node.jstree
  3048. * @param {Object} node
  3049. */
  3050. this.trigger('hover_node', { 'node' : this.get_node(obj) });
  3051. setTimeout(function () { t.attr('aria-activedescendant', obj[0].id); }, 0);
  3052. },
  3053. /**
  3054. * removes the hover state from a nodecalled when a node is no longer hovered by the user. Used internally.
  3055. * @private
  3056. * @name dehover_node(obj)
  3057. * @param {mixed} obj
  3058. * @trigger dehover_node.jstree
  3059. */
  3060. dehover_node : function (obj) {
  3061. obj = this.get_node(obj, true);
  3062. if(!obj || !obj.length || !obj.children('.jstree-hovered').length) {
  3063. return false;
  3064. }
  3065. obj.children('.jstree-anchor').removeClass('jstree-hovered');
  3066. /**
  3067. * triggered when an node is no longer hovered
  3068. * @event
  3069. * @name dehover_node.jstree
  3070. * @param {Object} node
  3071. */
  3072. this.trigger('dehover_node', { 'node' : this.get_node(obj) });
  3073. },
  3074. /**
  3075. * select a node
  3076. * @name select_node(obj [, supress_event, prevent_open])
  3077. * @param {mixed} obj an array can be used to select multiple nodes
  3078. * @param {Boolean} supress_event if set to `true` the `changed.jstree` event won't be triggered
  3079. * @param {Boolean} prevent_open if set to `true` parents of the selected node won't be opened
  3080. * @trigger select_node.jstree, changed.jstree
  3081. */
  3082. select_node : function (obj, supress_event, prevent_open, e) {
  3083. var dom, t1, t2, th;
  3084. if($.isArray(obj)) {
  3085. obj = obj.slice();
  3086. for(t1 = 0, t2 = obj.length; t1 < t2; t1++) {
  3087. this.select_node(obj[t1], supress_event, prevent_open, e);
  3088. }
  3089. return true;
  3090. }
  3091. obj = this.get_node(obj);
  3092. if(!obj || obj.id === $.jstree.root) {
  3093. return false;
  3094. }
  3095. dom = this.get_node(obj, true);
  3096. if(!obj.state.selected) {
  3097. obj.state.selected = true;
  3098. this._data.core.selected.push(obj.id);
  3099. if(!prevent_open) {
  3100. dom = this._open_to(obj);
  3101. }
  3102. if(dom && dom.length) {
  3103. dom.attr('aria-selected', true).children('.jstree-anchor').addClass('jstree-clicked');
  3104. }
  3105. /**
  3106. * triggered when an node is selected
  3107. * @event
  3108. * @name select_node.jstree
  3109. * @param {Object} node
  3110. * @param {Array} selected the current selection
  3111. * @param {Object} event the event (if any) that triggered this select_node
  3112. */
  3113. this.trigger('select_node', { 'node' : obj, 'selected' : this._data.core.selected, 'event' : e });
  3114. if(!supress_event) {
  3115. /**
  3116. * triggered when selection changes
  3117. * @event
  3118. * @name changed.jstree
  3119. * @param {Object} node
  3120. * @param {Object} action the action that caused the selection to change
  3121. * @param {Array} selected the current selection
  3122. * @param {Object} event the event (if any) that triggered this changed event
  3123. */
  3124. this.trigger('changed', { 'action' : 'select_node', 'node' : obj, 'selected' : this._data.core.selected, 'event' : e });
  3125. }
  3126. }
  3127. },
  3128. /**
  3129. * deselect a node
  3130. * @name deselect_node(obj [, supress_event])
  3131. * @param {mixed} obj an array can be used to deselect multiple nodes
  3132. * @param {Boolean} supress_event if set to `true` the `changed.jstree` event won't be triggered
  3133. * @trigger deselect_node.jstree, changed.jstree
  3134. */
  3135. deselect_node : function (obj, supress_event, e) {
  3136. var t1, t2, dom;
  3137. if($.isArray(obj)) {
  3138. obj = obj.slice();
  3139. for(t1 = 0, t2 = obj.length; t1 < t2; t1++) {
  3140. this.deselect_node(obj[t1], supress_event, e);
  3141. }
  3142. return true;
  3143. }
  3144. obj = this.get_node(obj);
  3145. if(!obj || obj.id === $.jstree.root) {
  3146. return false;
  3147. }
  3148. dom = this.get_node(obj, true);
  3149. if(obj.state.selected) {
  3150. obj.state.selected = false;
  3151. this._data.core.selected = $.vakata.array_remove_item(this._data.core.selected, obj.id);
  3152. if(dom.length) {
  3153. dom.attr('aria-selected', false).children('.jstree-anchor').removeClass('jstree-clicked');
  3154. }
  3155. /**
  3156. * triggered when an node is deselected
  3157. * @event
  3158. * @name deselect_node.jstree
  3159. * @param {Object} node
  3160. * @param {Array} selected the current selection
  3161. * @param {Object} event the event (if any) that triggered this deselect_node
  3162. */
  3163. this.trigger('deselect_node', { 'node' : obj, 'selected' : this._data.core.selected, 'event' : e });
  3164. if(!supress_event) {
  3165. this.trigger('changed', { 'action' : 'deselect_node', 'node' : obj, 'selected' : this._data.core.selected, 'event' : e });
  3166. }
  3167. }
  3168. },
  3169. /**
  3170. * select all nodes in the tree
  3171. * @name select_all([supress_event])
  3172. * @param {Boolean} supress_event if set to `true` the `changed.jstree` event won't be triggered
  3173. * @trigger select_all.jstree, changed.jstree
  3174. */
  3175. select_all : function (supress_event) {
  3176. var tmp = this._data.core.selected.concat([]), i, j;
  3177. this._data.core.selected = this._model.data[$.jstree.root].children_d.concat();
  3178. for(i = 0, j = this._data.core.selected.length; i < j; i++) {
  3179. if(this._model.data[this._data.core.selected[i]]) {
  3180. this._model.data[this._data.core.selected[i]].state.selected = true;
  3181. }
  3182. }
  3183. this.redraw(true);
  3184. /**
  3185. * triggered when all nodes are selected
  3186. * @event
  3187. * @name select_all.jstree
  3188. * @param {Array} selected the current selection
  3189. */
  3190. this.trigger('select_all', { 'selected' : this._data.core.selected });
  3191. if(!supress_event) {
  3192. this.trigger('changed', { 'action' : 'select_all', 'selected' : this._data.core.selected, 'old_selection' : tmp });
  3193. }
  3194. },
  3195. /**
  3196. * deselect all selected nodes
  3197. * @name deselect_all([supress_event])
  3198. * @param {Boolean} supress_event if set to `true` the `changed.jstree` event won't be triggered
  3199. * @trigger deselect_all.jstree, changed.jstree
  3200. */
  3201. deselect_all : function (supress_event) {
  3202. var tmp = this._data.core.selected.concat([]), i, j;
  3203. for(i = 0, j = this._data.core.selected.length; i < j; i++) {
  3204. if(this._model.data[this._data.core.selected[i]]) {
  3205. this._model.data[this._data.core.selected[i]].state.selected = false;
  3206. }
  3207. }
  3208. this._data.core.selected = [];
  3209. this.element.find('.jstree-clicked').removeClass('jstree-clicked').parent().attr('aria-selected', false);
  3210. /**
  3211. * triggered when all nodes are deselected
  3212. * @event
  3213. * @name deselect_all.jstree
  3214. * @param {Object} node the previous selection
  3215. * @param {Array} selected the current selection
  3216. */
  3217. this.trigger('deselect_all', { 'selected' : this._data.core.selected, 'node' : tmp });
  3218. if(!supress_event) {
  3219. this.trigger('changed', { 'action' : 'deselect_all', 'selected' : this._data.core.selected, 'old_selection' : tmp });
  3220. }
  3221. },
  3222. /**
  3223. * checks if a node is selected
  3224. * @name is_selected(obj)
  3225. * @param {mixed} obj
  3226. * @return {Boolean}
  3227. */
  3228. is_selected : function (obj) {
  3229. obj = this.get_node(obj);
  3230. if(!obj || obj.id === $.jstree.root) {
  3231. return false;
  3232. }
  3233. return obj.state.selected;
  3234. },
  3235. /**
  3236. * get an array of all selected nodes
  3237. * @name get_selected([full])
  3238. * @param {mixed} full if set to `true` the returned array will consist of the full node objects, otherwise - only IDs will be returned
  3239. * @return {Array}
  3240. */
  3241. get_selected : function (full) {
  3242. return full ? $.map(this._data.core.selected, $.proxy(function (i) { return this.get_node(i); }, this)) : this._data.core.selected.slice();
  3243. },
  3244. /**
  3245. * get an array of all top level selected nodes (ignoring children of selected nodes)
  3246. * @name get_top_selected([full])
  3247. * @param {mixed} full if set to `true` the returned array will consist of the full node objects, otherwise - only IDs will be returned
  3248. * @return {Array}
  3249. */
  3250. get_top_selected : function (full) {
  3251. var tmp = this.get_selected(true),
  3252. obj = {}, i, j, k, l;
  3253. for(i = 0, j = tmp.length; i < j; i++) {
  3254. obj[tmp[i].id] = tmp[i];
  3255. }
  3256. for(i = 0, j = tmp.length; i < j; i++) {
  3257. for(k = 0, l = tmp[i].children_d.length; k < l; k++) {
  3258. if(obj[tmp[i].children_d[k]]) {
  3259. delete obj[tmp[i].children_d[k]];
  3260. }
  3261. }
  3262. }
  3263. tmp = [];
  3264. for(i in obj) {
  3265. if(obj.hasOwnProperty(i)) {
  3266. tmp.push(i);
  3267. }
  3268. }
  3269. return full ? $.map(tmp, $.proxy(function (i) { return this.get_node(i); }, this)) : tmp;
  3270. },
  3271. /**
  3272. * get an array of all bottom level selected nodes (ignoring selected parents)
  3273. * @name get_bottom_selected([full])
  3274. * @param {mixed} full if set to `true` the returned array will consist of the full node objects, otherwise - only IDs will be returned
  3275. * @return {Array}
  3276. */
  3277. get_bottom_selected : function (full) {
  3278. var tmp = this.get_selected(true),
  3279. obj = [], i, j;
  3280. for(i = 0, j = tmp.length; i < j; i++) {
  3281. if(!tmp[i].children.length) {
  3282. obj.push(tmp[i].id);
  3283. }
  3284. }
  3285. return full ? $.map(obj, $.proxy(function (i) { return this.get_node(i); }, this)) : obj;
  3286. },
  3287. /**
  3288. * gets the current state of the tree so that it can be restored later with `set_state(state)`. Used internally.
  3289. * @name get_state()
  3290. * @private
  3291. * @return {Object}
  3292. */
  3293. get_state : function () {
  3294. var state = {
  3295. 'core' : {
  3296. 'open' : [],
  3297. 'scroll' : {
  3298. 'left' : this.element.scrollLeft(),
  3299. 'top' : this.element.scrollTop()
  3300. },
  3301. /*!
  3302. 'themes' : {
  3303. 'name' : this.get_theme(),
  3304. 'icons' : this._data.core.themes.icons,
  3305. 'dots' : this._data.core.themes.dots
  3306. },
  3307. */
  3308. 'selected' : []
  3309. }
  3310. }, i;
  3311. for(i in this._model.data) {
  3312. if(this._model.data.hasOwnProperty(i)) {
  3313. if(i !== $.jstree.root) {
  3314. if(this._model.data[i].state.opened) {
  3315. state.core.open.push(i);
  3316. }
  3317. if(this._model.data[i].state.selected) {
  3318. state.core.selected.push(i);
  3319. }
  3320. }
  3321. }
  3322. }
  3323. return state;
  3324. },
  3325. /**
  3326. * sets the state of the tree. Used internally.
  3327. * @name set_state(state [, callback])
  3328. * @private
  3329. * @param {Object} state the state to restore. Keep in mind this object is passed by reference and jstree will modify it.
  3330. * @param {Function} callback an optional function to execute once the state is restored.
  3331. * @trigger set_state.jstree
  3332. */
  3333. set_state : function (state, callback) {
  3334. if(state) {
  3335. if(state.core) {
  3336. var res, n, t, _this, i;
  3337. if(state.core.open) {
  3338. if(!$.isArray(state.core.open) || !state.core.open.length) {
  3339. delete state.core.open;
  3340. this.set_state(state, callback);
  3341. }
  3342. else {
  3343. this._load_nodes(state.core.open, function (nodes) {
  3344. this.open_node(nodes, false, 0);
  3345. delete state.core.open;
  3346. this.set_state(state, callback);
  3347. }, true);
  3348. }
  3349. return false;
  3350. }
  3351. if(state.core.scroll) {
  3352. if(state.core.scroll && state.core.scroll.left !== undefined) {
  3353. this.element.scrollLeft(state.core.scroll.left);
  3354. }
  3355. if(state.core.scroll && state.core.scroll.top !== undefined) {
  3356. this.element.scrollTop(state.core.scroll.top);
  3357. }
  3358. delete state.core.scroll;
  3359. this.set_state(state, callback);
  3360. return false;
  3361. }
  3362. if(state.core.selected) {
  3363. _this = this;
  3364. this.deselect_all();
  3365. $.each(state.core.selected, function (i, v) {
  3366. _this.select_node(v, false, true);
  3367. });
  3368. delete state.core.selected;
  3369. this.set_state(state, callback);
  3370. return false;
  3371. }
  3372. for(i in state) {
  3373. if(state.hasOwnProperty(i) && i !== "core" && $.inArray(i, this.settings.plugins) === -1) {
  3374. delete state[i];
  3375. }
  3376. }
  3377. if($.isEmptyObject(state.core)) {
  3378. delete state.core;
  3379. this.set_state(state, callback);
  3380. return false;
  3381. }
  3382. }
  3383. if($.isEmptyObject(state)) {
  3384. state = null;
  3385. if(callback) { callback.call(this); }
  3386. /**
  3387. * triggered when a `set_state` call completes
  3388. * @event
  3389. * @name set_state.jstree
  3390. */
  3391. this.trigger('set_state');
  3392. return false;
  3393. }
  3394. return true;
  3395. }
  3396. return false;
  3397. },
  3398. /**
  3399. * refreshes the tree - all nodes are reloaded with calls to `load_node`.
  3400. * @name refresh()
  3401. * @param {Boolean} skip_loading an option to skip showing the loading indicator
  3402. * @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
  3403. * @trigger refresh.jstree
  3404. */
  3405. refresh : function (skip_loading, forget_state) {
  3406. this._data.core.state = forget_state === true ? {} : this.get_state();
  3407. if(forget_state && $.isFunction(forget_state)) { this._data.core.state = forget_state.call(this, this._data.core.state); }
  3408. this._cnt = 0;
  3409. this._model.data = {};
  3410. this._model.data[$.jstree.root] = {
  3411. id : $.jstree.root,
  3412. parent : null,
  3413. parents : [],
  3414. children : [],
  3415. children_d : [],
  3416. state : { loaded : false }
  3417. };
  3418. this._data.core.selected = [];
  3419. this._data.core.last_clicked = null;
  3420. this._data.core.focused = null;
  3421. var c = this.get_container_ul()[0].className;
  3422. if(!skip_loading) {
  3423. this.element.html("<"+"ul class='"+c+"' role='group'><"+"li class='jstree-initial-node jstree-loading jstree-leaf jstree-last' role='treeitem' id='j"+this._id+"_loading'><i class='jstree-icon jstree-ocl'></i><"+"a class='jstree-anchor' href='#'><i class='jstree-icon jstree-themeicon-hidden'></i>" + this.get_string("Loading ...") + "</a></li></ul>");
  3424. this.element.attr('aria-activedescendant','j'+this._id+'_loading');
  3425. }
  3426. this.load_node($.jstree.root, function (o, s) {
  3427. if(s) {
  3428. this.get_container_ul()[0].className = c;
  3429. if(this._firstChild(this.get_container_ul()[0])) {
  3430. this.element.attr('aria-activedescendant',this._firstChild(this.get_container_ul()[0]).id);
  3431. }
  3432. this.set_state($.extend(true, {}, this._data.core.state), function () {
  3433. /**
  3434. * triggered when a `refresh` call completes
  3435. * @event
  3436. * @name refresh.jstree
  3437. */
  3438. this.trigger('refresh');
  3439. });
  3440. }
  3441. this._data.core.state = null;
  3442. });
  3443. },
  3444. /**
  3445. * refreshes a node in the tree (reload its children) all opened nodes inside that node are reloaded with calls to `load_node`.
  3446. * @name refresh_node(obj)
  3447. * @param {mixed} obj the node
  3448. * @trigger refresh_node.jstree
  3449. */
  3450. refresh_node : function (obj) {
  3451. obj = this.get_node(obj);
  3452. if(!obj || obj.id === $.jstree.root) { return false; }
  3453. var opened = [], to_load = [], s = this._data.core.selected.concat([]);
  3454. to_load.push(obj.id);
  3455. if(obj.state.opened === true) { opened.push(obj.id); }
  3456. this.get_node(obj, true).find('.jstree-open').each(function() { to_load.push(this.id); opened.push(this.id); });
  3457. this._load_nodes(to_load, $.proxy(function (nodes) {
  3458. this.open_node(opened, false, 0);
  3459. this.select_node(s);
  3460. /**
  3461. * triggered when a node is refreshed
  3462. * @event
  3463. * @name refresh_node.jstree
  3464. * @param {Object} node - the refreshed node
  3465. * @param {Array} nodes - an array of the IDs of the nodes that were reloaded
  3466. */
  3467. this.trigger('refresh_node', { 'node' : obj, 'nodes' : nodes });
  3468. }, this), false, true);
  3469. },
  3470. /**
  3471. * set (change) the ID of a node
  3472. * @name set_id(obj, id)
  3473. * @param {mixed} obj the node
  3474. * @param {String} id the new ID
  3475. * @return {Boolean}
  3476. * @trigger set_id.jstree
  3477. */
  3478. set_id : function (obj, id) {
  3479. obj = this.get_node(obj);
  3480. if(!obj || obj.id === $.jstree.root) { return false; }
  3481. var i, j, m = this._model.data, old = obj.id;
  3482. id = id.toString();
  3483. // update parents (replace current ID with new one in children and children_d)
  3484. m[obj.parent].children[$.inArray(obj.id, m[obj.parent].children)] = id;
  3485. for(i = 0, j = obj.parents.length; i < j; i++) {
  3486. m[obj.parents[i]].children_d[$.inArray(obj.id, m[obj.parents[i]].children_d)] = id;
  3487. }
  3488. // update children (replace current ID with new one in parent and parents)
  3489. for(i = 0, j = obj.children.length; i < j; i++) {
  3490. m[obj.children[i]].parent = id;
  3491. }
  3492. for(i = 0, j = obj.children_d.length; i < j; i++) {
  3493. m[obj.children_d[i]].parents[$.inArray(obj.id, m[obj.children_d[i]].parents)] = id;
  3494. }
  3495. i = $.inArray(obj.id, this._data.core.selected);
  3496. if(i !== -1) { this._data.core.selected[i] = id; }
  3497. // update model and obj itself (obj.id, this._model.data[KEY])
  3498. i = this.get_node(obj.id, true);
  3499. if(i) {
  3500. i.attr('id', id); //.children('.jstree-anchor').attr('id', id + '_anchor').end().attr('aria-labelledby', id + '_anchor');
  3501. if(this.element.attr('aria-activedescendant') === obj.id) {
  3502. this.element.attr('aria-activedescendant', id);
  3503. }
  3504. }
  3505. delete m[obj.id];
  3506. obj.id = id;
  3507. obj.li_attr.id = id;
  3508. m[id] = obj;
  3509. /**
  3510. * triggered when a node id value is changed
  3511. * @event
  3512. * @name set_id.jstree
  3513. * @param {Object} node
  3514. * @param {String} old the old id
  3515. */
  3516. this.trigger('set_id',{ "node" : obj, "new" : obj.id, "old" : old });
  3517. return true;
  3518. },
  3519. /**
  3520. * get the text value of a node
  3521. * @name get_text(obj)
  3522. * @param {mixed} obj the node
  3523. * @return {String}
  3524. */
  3525. get_text : function (obj) {
  3526. obj = this.get_node(obj);
  3527. return (!obj || obj.id === $.jstree.root) ? false : obj.text;
  3528. },
  3529. /**
  3530. * set the text value of a node. Used internally, please use `rename_node(obj, val)`.
  3531. * @private
  3532. * @name set_text(obj, val)
  3533. * @param {mixed} obj the node, you can pass an array to set the text on multiple nodes
  3534. * @param {String} val the new text value
  3535. * @return {Boolean}
  3536. * @trigger set_text.jstree
  3537. */
  3538. set_text : function (obj, val) {
  3539. var t1, t2;
  3540. if($.isArray(obj)) {
  3541. obj = obj.slice();
  3542. for(t1 = 0, t2 = obj.length; t1 < t2; t1++) {
  3543. this.set_text(obj[t1], val);
  3544. }
  3545. return true;
  3546. }
  3547. obj = this.get_node(obj);
  3548. if(!obj || obj.id === $.jstree.root) { return false; }
  3549. obj.text = val;
  3550. if(this.get_node(obj, true).length) {
  3551. this.redraw_node(obj.id);
  3552. }
  3553. /**
  3554. * triggered when a node text value is changed
  3555. * @event
  3556. * @name set_text.jstree
  3557. * @param {Object} obj
  3558. * @param {String} text the new value
  3559. */
  3560. this.trigger('set_text',{ "obj" : obj, "text" : val });
  3561. return true;
  3562. },
  3563. /**
  3564. * gets a JSON representation of a node (or the whole tree)
  3565. * @name get_json([obj, options])
  3566. * @param {mixed} obj
  3567. * @param {Object} options
  3568. * @param {Boolean} options.no_state do not return state information
  3569. * @param {Boolean} options.no_id do not return ID
  3570. * @param {Boolean} options.no_children do not include children
  3571. * @param {Boolean} options.no_data do not include node data
  3572. * @param {Boolean} options.flat return flat JSON instead of nested
  3573. * @return {Object}
  3574. */
  3575. get_json : function (obj, options, flat) {
  3576. obj = this.get_node(obj || $.jstree.root);
  3577. if(!obj) { return false; }
  3578. if(options && options.flat && !flat) { flat = []; }
  3579. var tmp = {
  3580. 'id' : obj.id,
  3581. 'text' : obj.text,
  3582. 'icon' : this.get_icon(obj),
  3583. 'li_attr' : $.extend(true, {}, obj.li_attr),
  3584. 'a_attr' : $.extend(true, {}, obj.a_attr),
  3585. 'state' : {},
  3586. 'data' : options && options.no_data ? false : $.extend(true, {}, obj.data)
  3587. //( this.get_node(obj, true).length ? this.get_node(obj, true).data() : obj.data ),
  3588. }, i, j;
  3589. if(options && options.flat) {
  3590. tmp.parent = obj.parent;
  3591. }
  3592. else {
  3593. tmp.children = [];
  3594. }
  3595. if(!options || !options.no_state) {
  3596. for(i in obj.state) {
  3597. if(obj.state.hasOwnProperty(i)) {
  3598. tmp.state[i] = obj.state[i];
  3599. }
  3600. }
  3601. }
  3602. if(options && options.no_id) {
  3603. delete tmp.id;
  3604. if(tmp.li_attr && tmp.li_attr.id) {
  3605. delete tmp.li_attr.id;
  3606. }
  3607. if(tmp.a_attr && tmp.a_attr.id) {
  3608. delete tmp.a_attr.id;
  3609. }
  3610. }
  3611. if(options && options.flat && obj.id !== $.jstree.root) {
  3612. flat.push(tmp);
  3613. }
  3614. if(!options || !options.no_children) {
  3615. for(i = 0, j = obj.children.length; i < j; i++) {
  3616. if(options && options.flat) {
  3617. this.get_json(obj.children[i], options, flat);
  3618. }
  3619. else {
  3620. tmp.children.push(this.get_json(obj.children[i], options));
  3621. }
  3622. }
  3623. }
  3624. return options && options.flat ? flat : (obj.id === $.jstree.root ? tmp.children : tmp);
  3625. },
  3626. /**
  3627. * create a new node (do not confuse with load_node)
  3628. * @name create_node([par, node, pos, callback, is_loaded])
  3629. * @param {mixed} par the parent node (to create a root node use either "#" (string) or `null`)
  3630. * @param {mixed} node the data for the new node (a valid JSON object, or a simple string with the name)
  3631. * @param {mixed} pos the index at which to insert the node, "first" and "last" are also supported, default is "last"
  3632. * @param {Function} callback a function to be called once the node is created
  3633. * @param {Boolean} is_loaded internal argument indicating if the parent node was succesfully loaded
  3634. * @return {String} the ID of the newly create node
  3635. * @trigger model.jstree, create_node.jstree
  3636. */
  3637. create_node : function (par, node, pos, callback, is_loaded) {
  3638. if(par === null) { par = $.jstree.root; }
  3639. par = this.get_node(par);
  3640. if(!par) { return false; }
  3641. pos = pos === undefined ? "last" : pos;
  3642. if(!pos.toString().match(/^(before|after)$/) && !is_loaded && !this.is_loaded(par)) {
  3643. return this.load_node(par, function () { this.create_node(par, node, pos, callback, true); });
  3644. }
  3645. if(!node) { node = { "text" : this.get_string('New node') }; }
  3646. if(typeof node === "string") { node = { "text" : node }; }
  3647. if(node.text === undefined) { node.text = this.get_string('New node'); }
  3648. var tmp, dpc, i, j;
  3649. if(par.id === $.jstree.root) {
  3650. if(pos === "before") { pos = "first"; }
  3651. if(pos === "after") { pos = "last"; }
  3652. }
  3653. switch(pos) {
  3654. case "before":
  3655. tmp = this.get_node(par.parent);
  3656. pos = $.inArray(par.id, tmp.children);
  3657. par = tmp;
  3658. break;
  3659. case "after" :
  3660. tmp = this.get_node(par.parent);
  3661. pos = $.inArray(par.id, tmp.children) + 1;
  3662. par = tmp;
  3663. break;
  3664. case "inside":
  3665. case "first":
  3666. pos = 0;
  3667. break;
  3668. case "last":
  3669. pos = par.children.length;
  3670. break;
  3671. default:
  3672. if(!pos) { pos = 0; }
  3673. break;
  3674. }
  3675. if(pos > par.children.length) { pos = par.children.length; }
  3676. if(!node.id) { node.id = true; }
  3677. if(!this.check("create_node", node, par, pos)) {
  3678. this.settings.core.error.call(this, this._data.core.last_error);
  3679. return false;
  3680. }
  3681. if(node.id === true) { delete node.id; }
  3682. node = this._parse_model_from_json(node, par.id, par.parents.concat());
  3683. if(!node) { return false; }
  3684. tmp = this.get_node(node);
  3685. dpc = [];
  3686. dpc.push(node);
  3687. dpc = dpc.concat(tmp.children_d);
  3688. this.trigger('model', { "nodes" : dpc, "parent" : par.id });
  3689. par.children_d = par.children_d.concat(dpc);
  3690. for(i = 0, j = par.parents.length; i < j; i++) {
  3691. this._model.data[par.parents[i]].children_d = this._model.data[par.parents[i]].children_d.concat(dpc);
  3692. }
  3693. node = tmp;
  3694. tmp = [];
  3695. for(i = 0, j = par.children.length; i < j; i++) {
  3696. tmp[i >= pos ? i+1 : i] = par.children[i];
  3697. }
  3698. tmp[pos] = node.id;
  3699. par.children = tmp;
  3700. this.redraw_node(par, true);
  3701. if(callback) { callback.call(this, this.get_node(node)); }
  3702. /**
  3703. * triggered when a node is created
  3704. * @event
  3705. * @name create_node.jstree
  3706. * @param {Object} node
  3707. * @param {String} parent the parent's ID
  3708. * @param {Number} position the position of the new node among the parent's children
  3709. */
  3710. this.trigger('create_node', { "node" : this.get_node(node), "parent" : par.id, "position" : pos });
  3711. return node.id;
  3712. },
  3713. /**
  3714. * set the text value of a node
  3715. * @name rename_node(obj, val)
  3716. * @param {mixed} obj the node, you can pass an array to rename multiple nodes to the same name
  3717. * @param {String} val the new text value
  3718. * @return {Boolean}
  3719. * @trigger rename_node.jstree
  3720. */
  3721. rename_node : function (obj, val) {
  3722. var t1, t2, old;
  3723. if($.isArray(obj)) {
  3724. obj = obj.slice();
  3725. for(t1 = 0, t2 = obj.length; t1 < t2; t1++) {
  3726. this.rename_node(obj[t1], val);
  3727. }
  3728. return true;
  3729. }
  3730. obj = this.get_node(obj);
  3731. if(!obj || obj.id === $.jstree.root) { return false; }
  3732. old = obj.text;
  3733. if(!this.check("rename_node", obj, this.get_parent(obj), val)) {
  3734. this.settings.core.error.call(this, this._data.core.last_error);
  3735. return false;
  3736. }
  3737. this.set_text(obj, val); // .apply(this, Array.prototype.slice.call(arguments))
  3738. /**
  3739. * triggered when a node is renamed
  3740. * @event
  3741. * @name rename_node.jstree
  3742. * @param {Object} node
  3743. * @param {String} text the new value
  3744. * @param {String} old the old value
  3745. */
  3746. this.trigger('rename_node', { "node" : obj, "text" : val, "old" : old });
  3747. return true;
  3748. },
  3749. /**
  3750. * remove a node
  3751. * @name delete_node(obj)
  3752. * @param {mixed} obj the node, you can pass an array to delete multiple nodes
  3753. * @return {Boolean}
  3754. * @trigger delete_node.jstree, changed.jstree
  3755. */
  3756. delete_node : function (obj) {
  3757. var t1, t2, par, pos, tmp, i, j, k, l, c, top, lft;
  3758. if($.isArray(obj)) {
  3759. obj = obj.slice();
  3760. for(t1 = 0, t2 = obj.length; t1 < t2; t1++) {
  3761. this.delete_node(obj[t1]);
  3762. }
  3763. return true;
  3764. }
  3765. obj = this.get_node(obj);
  3766. if(!obj || obj.id === $.jstree.root) { return false; }
  3767. par = this.get_node(obj.parent);
  3768. pos = $.inArray(obj.id, par.children);
  3769. c = false;
  3770. if(!this.check("delete_node", obj, par, pos)) {
  3771. this.settings.core.error.call(this, this._data.core.last_error);
  3772. return false;
  3773. }
  3774. if(pos !== -1) {
  3775. par.children = $.vakata.array_remove(par.children, pos);
  3776. }
  3777. tmp = obj.children_d.concat([]);
  3778. tmp.push(obj.id);
  3779. for(i = 0, j = obj.parents.length; i < j; i++) {
  3780. this._model.data[obj.parents[i]].children_d = $.vakata.array_filter(this._model.data[obj.parents[i]].children_d, function (v) {
  3781. return $.inArray(v, tmp) === -1;
  3782. });
  3783. }
  3784. for(k = 0, l = tmp.length; k < l; k++) {
  3785. if(this._model.data[tmp[k]].state.selected) {
  3786. c = true;
  3787. break;
  3788. }
  3789. }
  3790. if (c) {
  3791. this._data.core.selected = $.vakata.array_filter(this._data.core.selected, function (v) {
  3792. return $.inArray(v, tmp) === -1;
  3793. });
  3794. }
  3795. /**
  3796. * triggered when a node is deleted
  3797. * @event
  3798. * @name delete_node.jstree
  3799. * @param {Object} node
  3800. * @param {String} parent the parent's ID
  3801. */
  3802. this.trigger('delete_node', { "node" : obj, "parent" : par.id });
  3803. if(c) {
  3804. this.trigger('changed', { 'action' : 'delete_node', 'node' : obj, 'selected' : this._data.core.selected, 'parent' : par.id });
  3805. }
  3806. for(k = 0, l = tmp.length; k < l; k++) {
  3807. delete this._model.data[tmp[k]];
  3808. }
  3809. if($.inArray(this._data.core.focused, tmp) !== -1) {
  3810. this._data.core.focused = null;
  3811. top = this.element[0].scrollTop;
  3812. lft = this.element[0].scrollLeft;
  3813. if(par.id === $.jstree.root) {
  3814. if (this._model.data[$.jstree.root].children[0]) {
  3815. this.get_node(this._model.data[$.jstree.root].children[0], true).children('.jstree-anchor').focus();
  3816. }
  3817. }
  3818. else {
  3819. this.get_node(par, true).children('.jstree-anchor').focus();
  3820. }
  3821. this.element[0].scrollTop = top;
  3822. this.element[0].scrollLeft = lft;
  3823. }
  3824. this.redraw_node(par, true);
  3825. return true;
  3826. },
  3827. /**
  3828. * check if an operation is premitted on the tree. Used internally.
  3829. * @private
  3830. * @name check(chk, obj, par, pos)
  3831. * @param {String} chk the operation to check, can be "create_node", "rename_node", "delete_node", "copy_node" or "move_node"
  3832. * @param {mixed} obj the node
  3833. * @param {mixed} par the parent
  3834. * @param {mixed} pos the position to insert at, or if "rename_node" - the new name
  3835. * @param {mixed} more some various additional information, for example if a "move_node" operations is triggered by DND this will be the hovered node
  3836. * @return {Boolean}
  3837. */
  3838. check : function (chk, obj, par, pos, more) {
  3839. obj = obj && obj.id ? obj : this.get_node(obj);
  3840. par = par && par.id ? par : this.get_node(par);
  3841. var tmp = chk.match(/^move_node|copy_node|create_node$/i) ? par : obj,
  3842. chc = this.settings.core.check_callback;
  3843. if(chk === "move_node" || chk === "copy_node") {
  3844. 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)) {
  3845. 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 }) };
  3846. return false;
  3847. }
  3848. }
  3849. if(tmp && tmp.data) { tmp = tmp.data; }
  3850. if(tmp && tmp.functions && (tmp.functions[chk] === false || tmp.functions[chk] === true)) {
  3851. if(tmp.functions[chk] === false) {
  3852. 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 }) };
  3853. }
  3854. return tmp.functions[chk];
  3855. }
  3856. if(chc === false || ($.isFunction(chc) && chc.call(this, chk, obj, par, pos, more) === false) || (chc && chc[chk] === false)) {
  3857. 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 }) };
  3858. return false;
  3859. }
  3860. return true;
  3861. },
  3862. /**
  3863. * get the last error
  3864. * @name last_error()
  3865. * @return {Object}
  3866. */
  3867. last_error : function () {
  3868. return this._data.core.last_error;
  3869. },
  3870. /**
  3871. * move a node to a new parent
  3872. * @name move_node(obj, par [, pos, callback, is_loaded])
  3873. * @param {mixed} obj the node to move, pass an array to move multiple nodes
  3874. * @param {mixed} par the new parent
  3875. * @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`
  3876. * @param {function} callback a function to call once the move is completed, receives 3 arguments - the node, the new parent and the position
  3877. * @param {Boolean} is_loaded internal parameter indicating if the parent node has been loaded
  3878. * @param {Boolean} skip_redraw internal parameter indicating if the tree should be redrawn
  3879. * @param {Boolean} instance internal parameter indicating if the node comes from another instance
  3880. * @trigger move_node.jstree
  3881. */
  3882. move_node : function (obj, par, pos, callback, is_loaded, skip_redraw, origin) {
  3883. var t1, t2, old_par, old_pos, new_par, old_ins, is_multi, dpc, tmp, i, j, k, l, p;
  3884. par = this.get_node(par);
  3885. pos = pos === undefined ? 0 : pos;
  3886. if(!par) { return false; }
  3887. if(!pos.toString().match(/^(before|after)$/) && !is_loaded && !this.is_loaded(par)) {
  3888. return this.load_node(par, function () { this.move_node(obj, par, pos, callback, true, false, origin); });
  3889. }
  3890. if($.isArray(obj)) {
  3891. if(obj.length === 1) {
  3892. obj = obj[0];
  3893. }
  3894. else {
  3895. //obj = obj.slice();
  3896. for(t1 = 0, t2 = obj.length; t1 < t2; t1++) {
  3897. if((tmp = this.move_node(obj[t1], par, pos, callback, is_loaded, false, origin))) {
  3898. par = tmp;
  3899. pos = "after";
  3900. }
  3901. }
  3902. this.redraw();
  3903. return true;
  3904. }
  3905. }
  3906. obj = obj && obj.id ? obj : this.get_node(obj);
  3907. if(!obj || obj.id === $.jstree.root) { return false; }
  3908. old_par = (obj.parent || $.jstree.root).toString();
  3909. new_par = (!pos.toString().match(/^(before|after)$/) || par.id === $.jstree.root) ? par : this.get_node(par.parent);
  3910. old_ins = origin ? origin : (this._model.data[obj.id] ? this : $.jstree.reference(obj.id));
  3911. is_multi = !old_ins || !old_ins._id || (this._id !== old_ins._id);
  3912. 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;
  3913. if(old_ins && old_ins._id) {
  3914. obj = old_ins._model.data[obj.id];
  3915. }
  3916. if(is_multi) {
  3917. if((tmp = this.copy_node(obj, par, pos, callback, is_loaded, false, origin))) {
  3918. if(old_ins) { old_ins.delete_node(obj); }
  3919. return tmp;
  3920. }
  3921. return false;
  3922. }
  3923. //var m = this._model.data;
  3924. if(par.id === $.jstree.root) {
  3925. if(pos === "before") { pos = "first"; }
  3926. if(pos === "after") { pos = "last"; }
  3927. }
  3928. switch(pos) {
  3929. case "before":
  3930. pos = $.inArray(par.id, new_par.children);
  3931. break;
  3932. case "after" :
  3933. pos = $.inArray(par.id, new_par.children) + 1;
  3934. break;
  3935. case "inside":
  3936. case "first":
  3937. pos = 0;
  3938. break;
  3939. case "last":
  3940. pos = new_par.children.length;
  3941. break;
  3942. default:
  3943. if(!pos) { pos = 0; }
  3944. break;
  3945. }
  3946. if(pos > new_par.children.length) { pos = new_par.children.length; }
  3947. 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) })) {
  3948. this.settings.core.error.call(this, this._data.core.last_error);
  3949. return false;
  3950. }
  3951. if(obj.parent === new_par.id) {
  3952. dpc = new_par.children.concat();
  3953. tmp = $.inArray(obj.id, dpc);
  3954. if(tmp !== -1) {
  3955. dpc = $.vakata.array_remove(dpc, tmp);
  3956. if(pos > tmp) { pos--; }
  3957. }
  3958. tmp = [];
  3959. for(i = 0, j = dpc.length; i < j; i++) {
  3960. tmp[i >= pos ? i+1 : i] = dpc[i];
  3961. }
  3962. tmp[pos] = obj.id;
  3963. new_par.children = tmp;
  3964. this._node_changed(new_par.id);
  3965. this.redraw(new_par.id === $.jstree.root);
  3966. }
  3967. else {
  3968. // clean old parent and up
  3969. tmp = obj.children_d.concat();
  3970. tmp.push(obj.id);
  3971. for(i = 0, j = obj.parents.length; i < j; i++) {
  3972. dpc = [];
  3973. p = old_ins._model.data[obj.parents[i]].children_d;
  3974. for(k = 0, l = p.length; k < l; k++) {
  3975. if($.inArray(p[k], tmp) === -1) {
  3976. dpc.push(p[k]);
  3977. }
  3978. }
  3979. old_ins._model.data[obj.parents[i]].children_d = dpc;
  3980. }
  3981. old_ins._model.data[old_par].children = $.vakata.array_remove_item(old_ins._model.data[old_par].children, obj.id);
  3982. // insert into new parent and up
  3983. for(i = 0, j = new_par.parents.length; i < j; i++) {
  3984. this._model.data[new_par.parents[i]].children_d = this._model.data[new_par.parents[i]].children_d.concat(tmp);
  3985. }
  3986. dpc = [];
  3987. for(i = 0, j = new_par.children.length; i < j; i++) {
  3988. dpc[i >= pos ? i+1 : i] = new_par.children[i];
  3989. }
  3990. dpc[pos] = obj.id;
  3991. new_par.children = dpc;
  3992. new_par.children_d.push(obj.id);
  3993. new_par.children_d = new_par.children_d.concat(obj.children_d);
  3994. // update object
  3995. obj.parent = new_par.id;
  3996. tmp = new_par.parents.concat();
  3997. tmp.unshift(new_par.id);
  3998. p = obj.parents.length;
  3999. obj.parents = tmp;
  4000. // update object children
  4001. tmp = tmp.concat();
  4002. for(i = 0, j = obj.children_d.length; i < j; i++) {
  4003. this._model.data[obj.children_d[i]].parents = this._model.data[obj.children_d[i]].parents.slice(0,p*-1);
  4004. Array.prototype.push.apply(this._model.data[obj.children_d[i]].parents, tmp);
  4005. }
  4006. if(old_par === $.jstree.root || new_par.id === $.jstree.root) {
  4007. this._model.force_full_redraw = true;
  4008. }
  4009. if(!this._model.force_full_redraw) {
  4010. this._node_changed(old_par);
  4011. this._node_changed(new_par.id);
  4012. }
  4013. if(!skip_redraw) {
  4014. this.redraw();
  4015. }
  4016. }
  4017. if(callback) { callback.call(this, obj, new_par, pos); }
  4018. /**
  4019. * triggered when a node is moved
  4020. * @event
  4021. * @name move_node.jstree
  4022. * @param {Object} node
  4023. * @param {String} parent the parent's ID
  4024. * @param {Number} position the position of the node among the parent's children
  4025. * @param {String} old_parent the old parent of the node
  4026. * @param {Number} old_position the old position of the node
  4027. * @param {Boolean} is_multi do the node and new parent belong to different instances
  4028. * @param {jsTree} old_instance the instance the node came from
  4029. * @param {jsTree} new_instance the instance of the new parent
  4030. */
  4031. 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 });
  4032. return obj.id;
  4033. },
  4034. /**
  4035. * copy a node to a new parent
  4036. * @name copy_node(obj, par [, pos, callback, is_loaded])
  4037. * @param {mixed} obj the node to copy, pass an array to copy multiple nodes
  4038. * @param {mixed} par the new parent
  4039. * @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`
  4040. * @param {function} callback a function to call once the move is completed, receives 3 arguments - the node, the new parent and the position
  4041. * @param {Boolean} is_loaded internal parameter indicating if the parent node has been loaded
  4042. * @param {Boolean} skip_redraw internal parameter indicating if the tree should be redrawn
  4043. * @param {Boolean} instance internal parameter indicating if the node comes from another instance
  4044. * @trigger model.jstree copy_node.jstree
  4045. */
  4046. copy_node : function (obj, par, pos, callback, is_loaded, skip_redraw, origin) {
  4047. var t1, t2, dpc, tmp, i, j, node, old_par, new_par, old_ins, is_multi;
  4048. par = this.get_node(par);
  4049. pos = pos === undefined ? 0 : pos;
  4050. if(!par) { return false; }
  4051. if(!pos.toString().match(/^(before|after)$/) && !is_loaded && !this.is_loaded(par)) {
  4052. return this.load_node(par, function () { this.copy_node(obj, par, pos, callback, true, false, origin); });
  4053. }
  4054. if($.isArray(obj)) {
  4055. if(obj.length === 1) {
  4056. obj = obj[0];
  4057. }
  4058. else {
  4059. //obj = obj.slice();
  4060. for(t1 = 0, t2 = obj.length; t1 < t2; t1++) {
  4061. if((tmp = this.copy_node(obj[t1], par, pos, callback, is_loaded, true, origin))) {
  4062. par = tmp;
  4063. pos = "after";
  4064. }
  4065. }
  4066. this.redraw();
  4067. return true;
  4068. }
  4069. }
  4070. obj = obj && obj.id ? obj : this.get_node(obj);
  4071. if(!obj || obj.id === $.jstree.root) { return false; }
  4072. old_par = (obj.parent || $.jstree.root).toString();
  4073. new_par = (!pos.toString().match(/^(before|after)$/) || par.id === $.jstree.root) ? par : this.get_node(par.parent);
  4074. old_ins = origin ? origin : (this._model.data[obj.id] ? this : $.jstree.reference(obj.id));
  4075. is_multi = !old_ins || !old_ins._id || (this._id !== old_ins._id);
  4076. if(old_ins && old_ins._id) {
  4077. obj = old_ins._model.data[obj.id];
  4078. }
  4079. if(par.id === $.jstree.root) {
  4080. if(pos === "before") { pos = "first"; }
  4081. if(pos === "after") { pos = "last"; }
  4082. }
  4083. switch(pos) {
  4084. case "before":
  4085. pos = $.inArray(par.id, new_par.children);
  4086. break;
  4087. case "after" :
  4088. pos = $.inArray(par.id, new_par.children) + 1;
  4089. break;
  4090. case "inside":
  4091. case "first":
  4092. pos = 0;
  4093. break;
  4094. case "last":
  4095. pos = new_par.children.length;
  4096. break;
  4097. default:
  4098. if(!pos) { pos = 0; }
  4099. break;
  4100. }
  4101. if(pos > new_par.children.length) { pos = new_par.children.length; }
  4102. 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) })) {
  4103. this.settings.core.error.call(this, this._data.core.last_error);
  4104. return false;
  4105. }
  4106. node = old_ins ? old_ins.get_json(obj, { no_id : true, no_data : true, no_state : true }) : obj;
  4107. if(!node) { return false; }
  4108. if(node.id === true) { delete node.id; }
  4109. node = this._parse_model_from_json(node, new_par.id, new_par.parents.concat());
  4110. if(!node) { return false; }
  4111. tmp = this.get_node(node);
  4112. if(obj && obj.state && obj.state.loaded === false) { tmp.state.loaded = false; }
  4113. dpc = [];
  4114. dpc.push(node);
  4115. dpc = dpc.concat(tmp.children_d);
  4116. this.trigger('model', { "nodes" : dpc, "parent" : new_par.id });
  4117. // insert into new parent and up
  4118. for(i = 0, j = new_par.parents.length; i < j; i++) {
  4119. this._model.data[new_par.parents[i]].children_d = this._model.data[new_par.parents[i]].children_d.concat(dpc);
  4120. }
  4121. dpc = [];
  4122. for(i = 0, j = new_par.children.length; i < j; i++) {
  4123. dpc[i >= pos ? i+1 : i] = new_par.children[i];
  4124. }
  4125. dpc[pos] = tmp.id;
  4126. new_par.children = dpc;
  4127. new_par.children_d.push(tmp.id);
  4128. new_par.children_d = new_par.children_d.concat(tmp.children_d);
  4129. if(new_par.id === $.jstree.root) {
  4130. this._model.force_full_redraw = true;
  4131. }
  4132. if(!this._model.force_full_redraw) {
  4133. this._node_changed(new_par.id);
  4134. }
  4135. if(!skip_redraw) {
  4136. this.redraw(new_par.id === $.jstree.root);
  4137. }
  4138. if(callback) { callback.call(this, tmp, new_par, pos); }
  4139. /**
  4140. * triggered when a node is copied
  4141. * @event
  4142. * @name copy_node.jstree
  4143. * @param {Object} node the copied node
  4144. * @param {Object} original the original node
  4145. * @param {String} parent the parent's ID
  4146. * @param {Number} position the position of the node among the parent's children
  4147. * @param {String} old_parent the old parent of the node
  4148. * @param {Number} old_position the position of the original node
  4149. * @param {Boolean} is_multi do the node and new parent belong to different instances
  4150. * @param {jsTree} old_instance the instance the node came from
  4151. * @param {jsTree} new_instance the instance of the new parent
  4152. */
  4153. 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 });
  4154. return tmp.id;
  4155. },
  4156. /**
  4157. * cut a node (a later call to `paste(obj)` would move the node)
  4158. * @name cut(obj)
  4159. * @param {mixed} obj multiple objects can be passed using an array
  4160. * @trigger cut.jstree
  4161. */
  4162. cut : function (obj) {
  4163. if(!obj) { obj = this._data.core.selected.concat(); }
  4164. if(!$.isArray(obj)) { obj = [obj]; }
  4165. if(!obj.length) { return false; }
  4166. var tmp = [], o, t1, t2;
  4167. for(t1 = 0, t2 = obj.length; t1 < t2; t1++) {
  4168. o = this.get_node(obj[t1]);
  4169. if(o && o.id && o.id !== $.jstree.root) { tmp.push(o); }
  4170. }
  4171. if(!tmp.length) { return false; }
  4172. ccp_node = tmp;
  4173. ccp_inst = this;
  4174. ccp_mode = 'move_node';
  4175. /**
  4176. * triggered when nodes are added to the buffer for moving
  4177. * @event
  4178. * @name cut.jstree
  4179. * @param {Array} node
  4180. */
  4181. this.trigger('cut', { "node" : obj });
  4182. },
  4183. /**
  4184. * copy a node (a later call to `paste(obj)` would copy the node)
  4185. * @name copy(obj)
  4186. * @param {mixed} obj multiple objects can be passed using an array
  4187. * @trigger copy.jstree
  4188. */
  4189. copy : function (obj) {
  4190. if(!obj) { obj = this._data.core.selected.concat(); }
  4191. if(!$.isArray(obj)) { obj = [obj]; }
  4192. if(!obj.length) { return false; }
  4193. var tmp = [], o, t1, t2;
  4194. for(t1 = 0, t2 = obj.length; t1 < t2; t1++) {
  4195. o = this.get_node(obj[t1]);
  4196. if(o && o.id && o.id !== $.jstree.root) { tmp.push(o); }
  4197. }
  4198. if(!tmp.length) { return false; }
  4199. ccp_node = tmp;
  4200. ccp_inst = this;
  4201. ccp_mode = 'copy_node';
  4202. /**
  4203. * triggered when nodes are added to the buffer for copying
  4204. * @event
  4205. * @name copy.jstree
  4206. * @param {Array} node
  4207. */
  4208. this.trigger('copy', { "node" : obj });
  4209. },
  4210. /**
  4211. * get the current buffer (any nodes that are waiting for a paste operation)
  4212. * @name get_buffer()
  4213. * @return {Object} an object consisting of `mode` ("copy_node" or "move_node"), `node` (an array of objects) and `inst` (the instance)
  4214. */
  4215. get_buffer : function () {
  4216. return { 'mode' : ccp_mode, 'node' : ccp_node, 'inst' : ccp_inst };
  4217. },
  4218. /**
  4219. * check if there is something in the buffer to paste
  4220. * @name can_paste()
  4221. * @return {Boolean}
  4222. */
  4223. can_paste : function () {
  4224. return ccp_mode !== false && ccp_node !== false; // && ccp_inst._model.data[ccp_node];
  4225. },
  4226. /**
  4227. * copy or move the previously cut or copied nodes to a new parent
  4228. * @name paste(obj [, pos])
  4229. * @param {mixed} obj the new parent
  4230. * @param {mixed} pos the position to insert at (besides integer, "first" and "last" are supported), defaults to integer `0`
  4231. * @trigger paste.jstree
  4232. */
  4233. paste : function (obj, pos) {
  4234. obj = this.get_node(obj);
  4235. if(!obj || !ccp_mode || !ccp_mode.match(/^(copy_node|move_node)$/) || !ccp_node) { return false; }
  4236. if(this[ccp_mode](ccp_node, obj, pos, false, false, false, ccp_inst)) {
  4237. /**
  4238. * triggered when paste is invoked
  4239. * @event
  4240. * @name paste.jstree
  4241. * @param {String} parent the ID of the receiving node
  4242. * @param {Array} node the nodes in the buffer
  4243. * @param {String} mode the performed operation - "copy_node" or "move_node"
  4244. */
  4245. this.trigger('paste', { "parent" : obj.id, "node" : ccp_node, "mode" : ccp_mode });
  4246. }
  4247. ccp_node = false;
  4248. ccp_mode = false;
  4249. ccp_inst = false;
  4250. },
  4251. /**
  4252. * clear the buffer of previously copied or cut nodes
  4253. * @name clear_buffer()
  4254. * @trigger clear_buffer.jstree
  4255. */
  4256. clear_buffer : function () {
  4257. ccp_node = false;
  4258. ccp_mode = false;
  4259. ccp_inst = false;
  4260. /**
  4261. * triggered when the copy / cut buffer is cleared
  4262. * @event
  4263. * @name clear_buffer.jstree
  4264. */
  4265. this.trigger('clear_buffer');
  4266. },
  4267. /**
  4268. * put a node in edit mode (input field to rename the node)
  4269. * @name edit(obj [, default_text, callback])
  4270. * @param {mixed} obj
  4271. * @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)
  4272. * @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) and a boolean indicating if the user cancelled the edit. You can access the node's title using .text
  4273. */
  4274. edit : function (obj, default_text, callback) {
  4275. var rtl, w, a, s, t, h1, h2, fn, tmp, cancel = false;
  4276. obj = this.get_node(obj);
  4277. if(!obj) { return false; }
  4278. if(this.settings.core.check_callback === false) {
  4279. this._data.core.last_error = { 'error' : 'check', 'plugin' : 'core', 'id' : 'core_07', 'reason' : 'Could not edit node because of check_callback' };
  4280. this.settings.core.error.call(this, this._data.core.last_error);
  4281. return false;
  4282. }
  4283. tmp = obj;
  4284. default_text = typeof default_text === 'string' ? default_text : obj.text;
  4285. this.set_text(obj, "");
  4286. obj = this._open_to(obj);
  4287. tmp.text = default_text;
  4288. rtl = this._data.core.rtl;
  4289. w = this.element.width();
  4290. this._data.core.focused = tmp.id;
  4291. a = obj.children('.jstree-anchor').focus();
  4292. s = $('<span>');
  4293. /*!
  4294. oi = obj.children("i:visible"),
  4295. ai = a.children("i:visible"),
  4296. w1 = oi.width() * oi.length,
  4297. w2 = ai.width() * ai.length,
  4298. */
  4299. t = default_text;
  4300. h1 = $("<"+"div />", { css : { "position" : "absolute", "top" : "-200px", "left" : (rtl ? "0px" : "-1000px"), "visibility" : "hidden" } }).appendTo("body");
  4301. h2 = $("<"+"input />", {
  4302. "value" : t,
  4303. "class" : "jstree-rename-input",
  4304. // "size" : t.length,
  4305. "css" : {
  4306. "padding" : "0",
  4307. "border" : "1px solid silver",
  4308. "box-sizing" : "border-box",
  4309. "display" : "inline-block",
  4310. "height" : (this._data.core.li_height) + "px",
  4311. "lineHeight" : (this._data.core.li_height) + "px",
  4312. "width" : "150px" // will be set a bit further down
  4313. },
  4314. "blur" : $.proxy(function (e) {
  4315. e.stopImmediatePropagation();
  4316. e.preventDefault();
  4317. var i = s.children(".jstree-rename-input"),
  4318. v = i.val(),
  4319. f = this.settings.core.force_text,
  4320. nv;
  4321. if(v === "") { v = t; }
  4322. h1.remove();
  4323. s.replaceWith(a);
  4324. s.remove();
  4325. t = f ? t : $('<div></div>').append($.parseHTML(t)).html();
  4326. this.set_text(obj, t);
  4327. nv = !!this.rename_node(obj, f ? $('<div></div>').text(v).text() : $('<div></div>').append($.parseHTML(v)).html());
  4328. if(!nv) {
  4329. this.set_text(obj, t); // move this up? and fix #483
  4330. }
  4331. this._data.core.focused = tmp.id;
  4332. setTimeout($.proxy(function () {
  4333. var node = this.get_node(tmp.id, true);
  4334. if(node.length) {
  4335. this._data.core.focused = tmp.id;
  4336. node.children('.jstree-anchor').focus();
  4337. }
  4338. }, this), 0);
  4339. if(callback) {
  4340. callback.call(this, tmp, nv, cancel);
  4341. }
  4342. h2 = null;
  4343. }, this),
  4344. "keydown" : function (e) {
  4345. var key = e.which;
  4346. if(key === 27) {
  4347. cancel = true;
  4348. this.value = t;
  4349. }
  4350. if(key === 27 || key === 13 || key === 37 || key === 38 || key === 39 || key === 40 || key === 32) {
  4351. e.stopImmediatePropagation();
  4352. }
  4353. if(key === 27 || key === 13) {
  4354. e.preventDefault();
  4355. this.blur();
  4356. }
  4357. },
  4358. "click" : function (e) { e.stopImmediatePropagation(); },
  4359. "mousedown" : function (e) { e.stopImmediatePropagation(); },
  4360. "keyup" : function (e) {
  4361. h2.width(Math.min(h1.text("pW" + this.value).width(),w));
  4362. },
  4363. "keypress" : function(e) {
  4364. if(e.which === 13) { return false; }
  4365. }
  4366. });
  4367. fn = {
  4368. fontFamily : a.css('fontFamily') || '',
  4369. fontSize : a.css('fontSize') || '',
  4370. fontWeight : a.css('fontWeight') || '',
  4371. fontStyle : a.css('fontStyle') || '',
  4372. fontStretch : a.css('fontStretch') || '',
  4373. fontVariant : a.css('fontVariant') || '',
  4374. letterSpacing : a.css('letterSpacing') || '',
  4375. wordSpacing : a.css('wordSpacing') || ''
  4376. };
  4377. s.attr('class', a.attr('class')).append(a.contents().clone()).append(h2);
  4378. a.replaceWith(s);
  4379. h1.css(fn);
  4380. h2.css(fn).width(Math.min(h1.text("pW" + h2[0].value).width(),w))[0].select();
  4381. $(document).one('mousedown.jstree touchstart.jstree dnd_start.vakata', function (e) {
  4382. if (h2 && e.target !== h2) {
  4383. $(h2).blur();
  4384. }
  4385. });
  4386. },
  4387. /**
  4388. * changes the theme
  4389. * @name set_theme(theme_name [, theme_url])
  4390. * @param {String} theme_name the name of the new theme to apply
  4391. * @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.
  4392. * @trigger set_theme.jstree
  4393. */
  4394. set_theme : function (theme_name, theme_url) {
  4395. if(!theme_name) { return false; }
  4396. if(theme_url === true) {
  4397. var dir = this.settings.core.themes.dir;
  4398. if(!dir) { dir = $.jstree.path + '/themes'; }
  4399. theme_url = dir + '/' + theme_name + '/style.css';
  4400. }
  4401. if(theme_url && $.inArray(theme_url, themes_loaded) === -1) {
  4402. $('head').append('<'+'link rel="stylesheet" href="' + theme_url + '" type="text/css" />');
  4403. themes_loaded.push(theme_url);
  4404. }
  4405. if(this._data.core.themes.name) {
  4406. this.element.removeClass('jstree-' + this._data.core.themes.name);
  4407. }
  4408. this._data.core.themes.name = theme_name;
  4409. this.element.addClass('jstree-' + theme_name);
  4410. this.element[this.settings.core.themes.responsive ? 'addClass' : 'removeClass' ]('jstree-' + theme_name + '-responsive');
  4411. /**
  4412. * triggered when a theme is set
  4413. * @event
  4414. * @name set_theme.jstree
  4415. * @param {String} theme the new theme
  4416. */
  4417. this.trigger('set_theme', { 'theme' : theme_name });
  4418. },
  4419. /**
  4420. * gets the name of the currently applied theme name
  4421. * @name get_theme()
  4422. * @return {String}
  4423. */
  4424. get_theme : function () { return this._data.core.themes.name; },
  4425. /**
  4426. * changes the theme variant (if the theme has variants)
  4427. * @name set_theme_variant(variant_name)
  4428. * @param {String|Boolean} variant_name the variant to apply (if `false` is used the current variant is removed)
  4429. */
  4430. set_theme_variant : function (variant_name) {
  4431. if(this._data.core.themes.variant) {
  4432. this.element.removeClass('jstree-' + this._data.core.themes.name + '-' + this._data.core.themes.variant);
  4433. }
  4434. this._data.core.themes.variant = variant_name;
  4435. if(variant_name) {
  4436. this.element.addClass('jstree-' + this._data.core.themes.name + '-' + this._data.core.themes.variant);
  4437. }
  4438. },
  4439. /**
  4440. * gets the name of the currently applied theme variant
  4441. * @name get_theme()
  4442. * @return {String}
  4443. */
  4444. get_theme_variant : function () { return this._data.core.themes.variant; },
  4445. /**
  4446. * shows a striped background on the container (if the theme supports it)
  4447. * @name show_stripes()
  4448. */
  4449. show_stripes : function () { this._data.core.themes.stripes = true; this.get_container_ul().addClass("jstree-striped"); },
  4450. /**
  4451. * hides the striped background on the container
  4452. * @name hide_stripes()
  4453. */
  4454. hide_stripes : function () { this._data.core.themes.stripes = false; this.get_container_ul().removeClass("jstree-striped"); },
  4455. /**
  4456. * toggles the striped background on the container
  4457. * @name toggle_stripes()
  4458. */
  4459. toggle_stripes : function () { if(this._data.core.themes.stripes) { this.hide_stripes(); } else { this.show_stripes(); } },
  4460. /**
  4461. * shows the connecting dots (if the theme supports it)
  4462. * @name show_dots()
  4463. */
  4464. show_dots : function () { this._data.core.themes.dots = true; this.get_container_ul().removeClass("jstree-no-dots"); },
  4465. /**
  4466. * hides the connecting dots
  4467. * @name hide_dots()
  4468. */
  4469. hide_dots : function () { this._data.core.themes.dots = false; this.get_container_ul().addClass("jstree-no-dots"); },
  4470. /**
  4471. * toggles the connecting dots
  4472. * @name toggle_dots()
  4473. */
  4474. toggle_dots : function () { if(this._data.core.themes.dots) { this.hide_dots(); } else { this.show_dots(); } },
  4475. /**
  4476. * show the node icons
  4477. * @name show_icons()
  4478. */
  4479. show_icons : function () { this._data.core.themes.icons = true; this.get_container_ul().removeClass("jstree-no-icons"); },
  4480. /**
  4481. * hide the node icons
  4482. * @name hide_icons()
  4483. */
  4484. hide_icons : function () { this._data.core.themes.icons = false; this.get_container_ul().addClass("jstree-no-icons"); },
  4485. /**
  4486. * toggle the node icons
  4487. * @name toggle_icons()
  4488. */
  4489. toggle_icons : function () { if(this._data.core.themes.icons) { this.hide_icons(); } else { this.show_icons(); } },
  4490. /**
  4491. * set the node icon for a node
  4492. * @name set_icon(obj, icon)
  4493. * @param {mixed} obj
  4494. * @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
  4495. */
  4496. set_icon : function (obj, icon) {
  4497. var t1, t2, dom, old;
  4498. if($.isArray(obj)) {
  4499. obj = obj.slice();
  4500. for(t1 = 0, t2 = obj.length; t1 < t2; t1++) {
  4501. this.set_icon(obj[t1], icon);
  4502. }
  4503. return true;
  4504. }
  4505. obj = this.get_node(obj);
  4506. if(!obj || obj.id === $.jstree.root) { return false; }
  4507. old = obj.icon;
  4508. obj.icon = icon === true || icon === null || icon === undefined || icon === '' ? true : icon;
  4509. dom = this.get_node(obj, true).children(".jstree-anchor").children(".jstree-themeicon");
  4510. if(icon === false) {
  4511. this.hide_icon(obj);
  4512. }
  4513. else if(icon === true || icon === null || icon === undefined || icon === '') {
  4514. dom.removeClass('jstree-themeicon-custom ' + old).css("background","").removeAttr("rel");
  4515. if(old === false) { this.show_icon(obj); }
  4516. }
  4517. else if(icon.indexOf("/") === -1 && icon.indexOf(".") === -1) {
  4518. dom.removeClass(old).css("background","");
  4519. dom.addClass(icon + ' jstree-themeicon-custom').attr("rel",icon);
  4520. if(old === false) { this.show_icon(obj); }
  4521. }
  4522. else {
  4523. dom.removeClass(old).css("background","");
  4524. dom.addClass('jstree-themeicon-custom').css("background", "url('" + icon + "') center center no-repeat").attr("rel",icon);
  4525. if(old === false) { this.show_icon(obj); }
  4526. }
  4527. return true;
  4528. },
  4529. /**
  4530. * get the node icon for a node
  4531. * @name get_icon(obj)
  4532. * @param {mixed} obj
  4533. * @return {String}
  4534. */
  4535. get_icon : function (obj) {
  4536. obj = this.get_node(obj);
  4537. return (!obj || obj.id === $.jstree.root) ? false : obj.icon;
  4538. },
  4539. /**
  4540. * hide the icon on an individual node
  4541. * @name hide_icon(obj)
  4542. * @param {mixed} obj
  4543. */
  4544. hide_icon : function (obj) {
  4545. var t1, t2;
  4546. if($.isArray(obj)) {
  4547. obj = obj.slice();
  4548. for(t1 = 0, t2 = obj.length; t1 < t2; t1++) {
  4549. this.hide_icon(obj[t1]);
  4550. }
  4551. return true;
  4552. }
  4553. obj = this.get_node(obj);
  4554. if(!obj || obj === $.jstree.root) { return false; }
  4555. obj.icon = false;
  4556. this.get_node(obj, true).children(".jstree-anchor").children(".jstree-themeicon").addClass('jstree-themeicon-hidden');
  4557. return true;
  4558. },
  4559. /**
  4560. * show the icon on an individual node
  4561. * @name show_icon(obj)
  4562. * @param {mixed} obj
  4563. */
  4564. show_icon : function (obj) {
  4565. var t1, t2, dom;
  4566. if($.isArray(obj)) {
  4567. obj = obj.slice();
  4568. for(t1 = 0, t2 = obj.length; t1 < t2; t1++) {
  4569. this.show_icon(obj[t1]);
  4570. }
  4571. return true;
  4572. }
  4573. obj = this.get_node(obj);
  4574. if(!obj || obj === $.jstree.root) { return false; }
  4575. dom = this.get_node(obj, true);
  4576. obj.icon = dom.length ? dom.children(".jstree-anchor").children(".jstree-themeicon").attr('rel') : true;
  4577. if(!obj.icon) { obj.icon = true; }
  4578. dom.children(".jstree-anchor").children(".jstree-themeicon").removeClass('jstree-themeicon-hidden');
  4579. return true;
  4580. }
  4581. };
  4582. // helpers
  4583. $.vakata = {};
  4584. // collect attributes
  4585. $.vakata.attributes = function(node, with_values) {
  4586. node = $(node)[0];
  4587. var attr = with_values ? {} : [];
  4588. if(node && node.attributes) {
  4589. $.each(node.attributes, function (i, v) {
  4590. if($.inArray(v.name.toLowerCase(),['style','contenteditable','hasfocus','tabindex']) !== -1) { return; }
  4591. if(v.value !== null && $.trim(v.value) !== '') {
  4592. if(with_values) { attr[v.name] = v.value; }
  4593. else { attr.push(v.name); }
  4594. }
  4595. });
  4596. }
  4597. return attr;
  4598. };
  4599. $.vakata.array_unique = function(array) {
  4600. var a = [], i, j, l, o = {};
  4601. for(i = 0, l = array.length; i < l; i++) {
  4602. if(o[array[i]] === undefined) {
  4603. a.push(array[i]);
  4604. o[array[i]] = true;
  4605. }
  4606. }
  4607. return a;
  4608. };
  4609. // remove item from array
  4610. $.vakata.array_remove = function(array, from) {
  4611. array.splice(from, 1);
  4612. return array;
  4613. //var rest = array.slice((to || from) + 1 || array.length);
  4614. //array.length = from < 0 ? array.length + from : from;
  4615. //array.push.apply(array, rest);
  4616. //return array;
  4617. };
  4618. // remove item from array
  4619. $.vakata.array_remove_item = function(array, item) {
  4620. var tmp = $.inArray(item, array);
  4621. return tmp !== -1 ? $.vakata.array_remove(array, tmp) : array;
  4622. };
  4623. $.vakata.array_filter = function(c,a,b,d,e) {
  4624. if (c.filter) {
  4625. return c.filter(a, b);
  4626. }
  4627. d=[];
  4628. for (e in c) {
  4629. if (~~e+''===e+'' && e>=0 && a.call(b,c[e],+e,c)) {
  4630. d.push(c[e]);
  4631. }
  4632. }
  4633. return d;
  4634. };
  4635. /**
  4636. * ### Changed plugin
  4637. *
  4638. * This plugin adds more information to the `changed.jstree` event. The new data is contained in the `changed` event data property, and contains a lists of `selected` and `deselected` nodes.
  4639. */
  4640. $.jstree.plugins.changed = function (options, parent) {
  4641. var last = [];
  4642. this.trigger = function (ev, data) {
  4643. var i, j;
  4644. if(!data) {
  4645. data = {};
  4646. }
  4647. if(ev.replace('.jstree','') === 'changed') {
  4648. data.changed = { selected : [], deselected : [] };
  4649. var tmp = {};
  4650. for(i = 0, j = last.length; i < j; i++) {
  4651. tmp[last[i]] = 1;
  4652. }
  4653. for(i = 0, j = data.selected.length; i < j; i++) {
  4654. if(!tmp[data.selected[i]]) {
  4655. data.changed.selected.push(data.selected[i]);
  4656. }
  4657. else {
  4658. tmp[data.selected[i]] = 2;
  4659. }
  4660. }
  4661. for(i = 0, j = last.length; i < j; i++) {
  4662. if(tmp[last[i]] === 1) {
  4663. data.changed.deselected.push(last[i]);
  4664. }
  4665. }
  4666. last = data.selected.slice();
  4667. }
  4668. /**
  4669. * triggered when selection changes (the "changed" plugin enhances the original event with more data)
  4670. * @event
  4671. * @name changed.jstree
  4672. * @param {Object} node
  4673. * @param {Object} action the action that caused the selection to change
  4674. * @param {Array} selected the current selection
  4675. * @param {Object} changed an object containing two properties `selected` and `deselected` - both arrays of node IDs, which were selected or deselected since the last changed event
  4676. * @param {Object} event the event (if any) that triggered this changed event
  4677. * @plugin changed
  4678. */
  4679. parent.trigger.call(this, ev, data);
  4680. };
  4681. this.refresh = function (skip_loading, forget_state) {
  4682. last = [];
  4683. return parent.refresh.apply(this, arguments);
  4684. };
  4685. };
  4686. /**
  4687. * ### Checkbox plugin
  4688. *
  4689. * This plugin renders checkbox icons in front of each node, making multiple selection much easier.
  4690. * It also supports tri-state behavior, meaning that if a node has a few of its children checked it will be rendered as undetermined, and state will be propagated up.
  4691. */
  4692. var _i = document.createElement('I');
  4693. _i.className = 'jstree-icon jstree-checkbox';
  4694. _i.setAttribute('role', 'presentation');
  4695. /**
  4696. * stores all defaults for the checkbox plugin
  4697. * @name $.jstree.defaults.checkbox
  4698. * @plugin checkbox
  4699. */
  4700. $.jstree.defaults.checkbox = {
  4701. /**
  4702. * a boolean indicating if checkboxes should be visible (can be changed at a later time using `show_checkboxes()` and `hide_checkboxes`). Defaults to `true`.
  4703. * @name $.jstree.defaults.checkbox.visible
  4704. * @plugin checkbox
  4705. */
  4706. visible : true,
  4707. /**
  4708. * a boolean indicating if checkboxes should cascade down and have an undetermined state. Defaults to `true`.
  4709. * @name $.jstree.defaults.checkbox.three_state
  4710. * @plugin checkbox
  4711. */
  4712. three_state : true,
  4713. /**
  4714. * a boolean indicating if clicking anywhere on the node should act as clicking on the checkbox. Defaults to `true`.
  4715. * @name $.jstree.defaults.checkbox.whole_node
  4716. * @plugin checkbox
  4717. */
  4718. whole_node : true,
  4719. /**
  4720. * a boolean indicating if the selected style of a node should be kept, or removed. Defaults to `true`.
  4721. * @name $.jstree.defaults.checkbox.keep_selected_style
  4722. * @plugin checkbox
  4723. */
  4724. keep_selected_style : true,
  4725. /**
  4726. * This setting controls how cascading and undetermined nodes are applied.
  4727. * If 'up' is in the string - cascading up is enabled, if 'down' is in the string - cascading down is enabled, if 'undetermined' is in the string - undetermined nodes will be used.
  4728. * If `three_state` is set to `true` this setting is automatically set to 'up+down+undetermined'. Defaults to ''.
  4729. * @name $.jstree.defaults.checkbox.cascade
  4730. * @plugin checkbox
  4731. */
  4732. cascade : '',
  4733. /**
  4734. * This setting controls if checkbox are bound to the general tree selection or to an internal array maintained by the checkbox plugin. Defaults to `true`, only set to `false` if you know exactly what you are doing.
  4735. * @name $.jstree.defaults.checkbox.tie_selection
  4736. * @plugin checkbox
  4737. */
  4738. tie_selection : true
  4739. };
  4740. $.jstree.plugins.checkbox = function (options, parent) {
  4741. this.bind = function () {
  4742. parent.bind.call(this);
  4743. this._data.checkbox.uto = false;
  4744. this._data.checkbox.selected = [];
  4745. if(this.settings.checkbox.three_state) {
  4746. this.settings.checkbox.cascade = 'up+down+undetermined';
  4747. }
  4748. this.element
  4749. .on("init.jstree", $.proxy(function () {
  4750. this._data.checkbox.visible = this.settings.checkbox.visible;
  4751. if(!this.settings.checkbox.keep_selected_style) {
  4752. this.element.addClass('jstree-checkbox-no-clicked');
  4753. }
  4754. if(this.settings.checkbox.tie_selection) {
  4755. this.element.addClass('jstree-checkbox-selection');
  4756. }
  4757. }, this))
  4758. .on("loading.jstree", $.proxy(function () {
  4759. this[ this._data.checkbox.visible ? 'show_checkboxes' : 'hide_checkboxes' ]();
  4760. }, this));
  4761. if(this.settings.checkbox.cascade.indexOf('undetermined') !== -1) {
  4762. this.element
  4763. .on('changed.jstree uncheck_node.jstree check_node.jstree uncheck_all.jstree check_all.jstree move_node.jstree copy_node.jstree redraw.jstree open_node.jstree', $.proxy(function () {
  4764. // only if undetermined is in setting
  4765. if(this._data.checkbox.uto) { clearTimeout(this._data.checkbox.uto); }
  4766. this._data.checkbox.uto = setTimeout($.proxy(this._undetermined, this), 50);
  4767. }, this));
  4768. }
  4769. if(!this.settings.checkbox.tie_selection) {
  4770. this.element
  4771. .on('model.jstree', $.proxy(function (e, data) {
  4772. var m = this._model.data,
  4773. p = m[data.parent],
  4774. dpc = data.nodes,
  4775. i, j;
  4776. for(i = 0, j = dpc.length; i < j; i++) {
  4777. m[dpc[i]].state.checked = m[dpc[i]].state.checked || (m[dpc[i]].original && m[dpc[i]].original.state && m[dpc[i]].original.state.checked);
  4778. if(m[dpc[i]].state.checked) {
  4779. this._data.checkbox.selected.push(dpc[i]);
  4780. }
  4781. }
  4782. }, this));
  4783. }
  4784. if(this.settings.checkbox.cascade.indexOf('up') !== -1 || this.settings.checkbox.cascade.indexOf('down') !== -1) {
  4785. this.element
  4786. .on('model.jstree', $.proxy(function (e, data) {
  4787. var m = this._model.data,
  4788. p = m[data.parent],
  4789. dpc = data.nodes,
  4790. chd = [],
  4791. c, i, j, k, l, tmp, s = this.settings.checkbox.cascade, t = this.settings.checkbox.tie_selection;
  4792. if(s.indexOf('down') !== -1) {
  4793. // apply down
  4794. if(p.state[ t ? 'selected' : 'checked' ]) {
  4795. for(i = 0, j = dpc.length; i < j; i++) {
  4796. m[dpc[i]].state[ t ? 'selected' : 'checked' ] = true;
  4797. }
  4798. this._data[ t ? 'core' : 'checkbox' ].selected = this._data[ t ? 'core' : 'checkbox' ].selected.concat(dpc);
  4799. }
  4800. else {
  4801. for(i = 0, j = dpc.length; i < j; i++) {
  4802. if(m[dpc[i]].state[ t ? 'selected' : 'checked' ]) {
  4803. for(k = 0, l = m[dpc[i]].children_d.length; k < l; k++) {
  4804. m[m[dpc[i]].children_d[k]].state[ t ? 'selected' : 'checked' ] = true;
  4805. }
  4806. this._data[ t ? 'core' : 'checkbox' ].selected = this._data[ t ? 'core' : 'checkbox' ].selected.concat(m[dpc[i]].children_d);
  4807. }
  4808. }
  4809. }
  4810. }
  4811. if(s.indexOf('up') !== -1) {
  4812. // apply up
  4813. for(i = 0, j = p.children_d.length; i < j; i++) {
  4814. if(!m[p.children_d[i]].children.length) {
  4815. chd.push(m[p.children_d[i]].parent);
  4816. }
  4817. }
  4818. chd = $.vakata.array_unique(chd);
  4819. for(k = 0, l = chd.length; k < l; k++) {
  4820. p = m[chd[k]];
  4821. while(p && p.id !== $.jstree.root) {
  4822. c = 0;
  4823. for(i = 0, j = p.children.length; i < j; i++) {
  4824. c += m[p.children[i]].state[ t ? 'selected' : 'checked' ];
  4825. }
  4826. if(c === j) {
  4827. p.state[ t ? 'selected' : 'checked' ] = true;
  4828. this._data[ t ? 'core' : 'checkbox' ].selected.push(p.id);
  4829. tmp = this.get_node(p, true);
  4830. if(tmp && tmp.length) {
  4831. tmp.attr('aria-selected', true).children('.jstree-anchor').addClass( t ? 'jstree-clicked' : 'jstree-checked');
  4832. }
  4833. }
  4834. else {
  4835. break;
  4836. }
  4837. p = this.get_node(p.parent);
  4838. }
  4839. }
  4840. }
  4841. this._data[ t ? 'core' : 'checkbox' ].selected = $.vakata.array_unique(this._data[ t ? 'core' : 'checkbox' ].selected);
  4842. }, this))
  4843. .on(this.settings.checkbox.tie_selection ? 'select_node.jstree' : 'check_node.jstree', $.proxy(function (e, data) {
  4844. var obj = data.node,
  4845. m = this._model.data,
  4846. par = this.get_node(obj.parent),
  4847. dom = this.get_node(obj, true),
  4848. i, j, c, tmp, s = this.settings.checkbox.cascade, t = this.settings.checkbox.tie_selection,
  4849. sel = {}, cur = this._data[ t ? 'core' : 'checkbox' ].selected;
  4850. for (i = 0, j = cur.length; i < j; i++) {
  4851. sel[cur[i]] = true;
  4852. }
  4853. // apply down
  4854. if(s.indexOf('down') !== -1) {
  4855. //this._data[ t ? 'core' : 'checkbox' ].selected = $.vakata.array_unique(this._data[ t ? 'core' : 'checkbox' ].selected.concat(obj.children_d));
  4856. for(i = 0, j = obj.children_d.length; i < j; i++) {
  4857. sel[obj.children_d[i]] = true;
  4858. tmp = m[obj.children_d[i]];
  4859. tmp.state[ t ? 'selected' : 'checked' ] = true;
  4860. if(tmp && tmp.original && tmp.original.state && tmp.original.state.undetermined) {
  4861. tmp.original.state.undetermined = false;
  4862. }
  4863. }
  4864. }
  4865. // apply up
  4866. if(s.indexOf('up') !== -1) {
  4867. while(par && par.id !== $.jstree.root) {
  4868. c = 0;
  4869. for(i = 0, j = par.children.length; i < j; i++) {
  4870. c += m[par.children[i]].state[ t ? 'selected' : 'checked' ];
  4871. }
  4872. if(c === j) {
  4873. par.state[ t ? 'selected' : 'checked' ] = true;
  4874. sel[par.id] = true;
  4875. //this._data[ t ? 'core' : 'checkbox' ].selected.push(par.id);
  4876. tmp = this.get_node(par, true);
  4877. if(tmp && tmp.length) {
  4878. tmp.attr('aria-selected', true).children('.jstree-anchor').addClass(t ? 'jstree-clicked' : 'jstree-checked');
  4879. }
  4880. }
  4881. else {
  4882. break;
  4883. }
  4884. par = this.get_node(par.parent);
  4885. }
  4886. }
  4887. cur = [];
  4888. for (i in sel) {
  4889. if (sel.hasOwnProperty(i)) {
  4890. cur.push(i);
  4891. }
  4892. }
  4893. this._data[ t ? 'core' : 'checkbox' ].selected = cur;
  4894. // apply down (process .children separately?)
  4895. if(s.indexOf('down') !== -1 && dom.length) {
  4896. dom.find('.jstree-anchor').addClass(t ? 'jstree-clicked' : 'jstree-checked').parent().attr('aria-selected', true);
  4897. }
  4898. }, this))
  4899. .on(this.settings.checkbox.tie_selection ? 'deselect_all.jstree' : 'uncheck_all.jstree', $.proxy(function (e, data) {
  4900. var obj = this.get_node($.jstree.root),
  4901. m = this._model.data,
  4902. i, j, tmp;
  4903. for(i = 0, j = obj.children_d.length; i < j; i++) {
  4904. tmp = m[obj.children_d[i]];
  4905. if(tmp && tmp.original && tmp.original.state && tmp.original.state.undetermined) {
  4906. tmp.original.state.undetermined = false;
  4907. }
  4908. }
  4909. }, this))
  4910. .on(this.settings.checkbox.tie_selection ? 'deselect_node.jstree' : 'uncheck_node.jstree', $.proxy(function (e, data) {
  4911. var obj = data.node,
  4912. dom = this.get_node(obj, true),
  4913. i, j, tmp, s = this.settings.checkbox.cascade, t = this.settings.checkbox.tie_selection,
  4914. cur = this._data[ t ? 'core' : 'checkbox' ].selected, sel = {};
  4915. if(obj && obj.original && obj.original.state && obj.original.state.undetermined) {
  4916. obj.original.state.undetermined = false;
  4917. }
  4918. // apply down
  4919. if(s.indexOf('down') !== -1) {
  4920. for(i = 0, j = obj.children_d.length; i < j; i++) {
  4921. tmp = this._model.data[obj.children_d[i]];
  4922. tmp.state[ t ? 'selected' : 'checked' ] = false;
  4923. if(tmp && tmp.original && tmp.original.state && tmp.original.state.undetermined) {
  4924. tmp.original.state.undetermined = false;
  4925. }
  4926. }
  4927. }
  4928. // apply up
  4929. if(s.indexOf('up') !== -1) {
  4930. for(i = 0, j = obj.parents.length; i < j; i++) {
  4931. tmp = this._model.data[obj.parents[i]];
  4932. tmp.state[ t ? 'selected' : 'checked' ] = false;
  4933. if(tmp && tmp.original && tmp.original.state && tmp.original.state.undetermined) {
  4934. tmp.original.state.undetermined = false;
  4935. }
  4936. tmp = this.get_node(obj.parents[i], true);
  4937. if(tmp && tmp.length) {
  4938. tmp.attr('aria-selected', false).children('.jstree-anchor').removeClass(t ? 'jstree-clicked' : 'jstree-checked');
  4939. }
  4940. }
  4941. }
  4942. sel = {};
  4943. for(i = 0, j = cur.length; i < j; i++) {
  4944. // apply down + apply up
  4945. if(
  4946. (s.indexOf('down') === -1 || $.inArray(cur[i], obj.children_d) === -1) &&
  4947. (s.indexOf('up') === -1 || $.inArray(cur[i], obj.parents) === -1)
  4948. ) {
  4949. sel[cur[i]] = true;
  4950. }
  4951. }
  4952. cur = [];
  4953. for (i in sel) {
  4954. if (sel.hasOwnProperty(i)) {
  4955. cur.push(i);
  4956. }
  4957. }
  4958. this._data[ t ? 'core' : 'checkbox' ].selected = cur;
  4959. // apply down (process .children separately?)
  4960. if(s.indexOf('down') !== -1 && dom.length) {
  4961. dom.find('.jstree-anchor').removeClass(t ? 'jstree-clicked' : 'jstree-checked').parent().attr('aria-selected', false);
  4962. }
  4963. }, this));
  4964. }
  4965. if(this.settings.checkbox.cascade.indexOf('up') !== -1) {
  4966. this.element
  4967. .on('delete_node.jstree', $.proxy(function (e, data) {
  4968. // apply up (whole handler)
  4969. var p = this.get_node(data.parent),
  4970. m = this._model.data,
  4971. i, j, c, tmp, t = this.settings.checkbox.tie_selection;
  4972. while(p && p.id !== $.jstree.root && !p.state[ t ? 'selected' : 'checked' ]) {
  4973. c = 0;
  4974. for(i = 0, j = p.children.length; i < j; i++) {
  4975. c += m[p.children[i]].state[ t ? 'selected' : 'checked' ];
  4976. }
  4977. if(j > 0 && c === j) {
  4978. p.state[ t ? 'selected' : 'checked' ] = true;
  4979. this._data[ t ? 'core' : 'checkbox' ].selected.push(p.id);
  4980. tmp = this.get_node(p, true);
  4981. if(tmp && tmp.length) {
  4982. tmp.attr('aria-selected', true).children('.jstree-anchor').addClass(t ? 'jstree-clicked' : 'jstree-checked');
  4983. }
  4984. }
  4985. else {
  4986. break;
  4987. }
  4988. p = this.get_node(p.parent);
  4989. }
  4990. }, this))
  4991. .on('move_node.jstree', $.proxy(function (e, data) {
  4992. // apply up (whole handler)
  4993. var is_multi = data.is_multi,
  4994. old_par = data.old_parent,
  4995. new_par = this.get_node(data.parent),
  4996. m = this._model.data,
  4997. p, c, i, j, tmp, t = this.settings.checkbox.tie_selection;
  4998. if(!is_multi) {
  4999. p = this.get_node(old_par);
  5000. while(p && p.id !== $.jstree.root && !p.state[ t ? 'selected' : 'checked' ]) {
  5001. c = 0;
  5002. for(i = 0, j = p.children.length; i < j; i++) {
  5003. c += m[p.children[i]].state[ t ? 'selected' : 'checked' ];
  5004. }
  5005. if(j > 0 && c === j) {
  5006. p.state[ t ? 'selected' : 'checked' ] = true;
  5007. this._data[ t ? 'core' : 'checkbox' ].selected.push(p.id);
  5008. tmp = this.get_node(p, true);
  5009. if(tmp && tmp.length) {
  5010. tmp.attr('aria-selected', true).children('.jstree-anchor').addClass(t ? 'jstree-clicked' : 'jstree-checked');
  5011. }
  5012. }
  5013. else {
  5014. break;
  5015. }
  5016. p = this.get_node(p.parent);
  5017. }
  5018. }
  5019. p = new_par;
  5020. while(p && p.id !== $.jstree.root) {
  5021. c = 0;
  5022. for(i = 0, j = p.children.length; i < j; i++) {
  5023. c += m[p.children[i]].state[ t ? 'selected' : 'checked' ];
  5024. }
  5025. if(c === j) {
  5026. if(!p.state[ t ? 'selected' : 'checked' ]) {
  5027. p.state[ t ? 'selected' : 'checked' ] = true;
  5028. this._data[ t ? 'core' : 'checkbox' ].selected.push(p.id);
  5029. tmp = this.get_node(p, true);
  5030. if(tmp && tmp.length) {
  5031. tmp.attr('aria-selected', true).children('.jstree-anchor').addClass(t ? 'jstree-clicked' : 'jstree-checked');
  5032. }
  5033. }
  5034. }
  5035. else {
  5036. if(p.state[ t ? 'selected' : 'checked' ]) {
  5037. p.state[ t ? 'selected' : 'checked' ] = false;
  5038. this._data[ t ? 'core' : 'checkbox' ].selected = $.vakata.array_remove_item(this._data[ t ? 'core' : 'checkbox' ].selected, p.id);
  5039. tmp = this.get_node(p, true);
  5040. if(tmp && tmp.length) {
  5041. tmp.attr('aria-selected', false).children('.jstree-anchor').removeClass(t ? 'jstree-clicked' : 'jstree-checked');
  5042. }
  5043. }
  5044. else {
  5045. break;
  5046. }
  5047. }
  5048. p = this.get_node(p.parent);
  5049. }
  5050. }, this));
  5051. }
  5052. };
  5053. /**
  5054. * set the undetermined state where and if necessary. Used internally.
  5055. * @private
  5056. * @name _undetermined()
  5057. * @plugin checkbox
  5058. */
  5059. this._undetermined = function () {
  5060. if(this.element === null) { return; }
  5061. var i, j, k, l, o = {}, m = this._model.data, t = this.settings.checkbox.tie_selection, s = this._data[ t ? 'core' : 'checkbox' ].selected, p = [], tt = this;
  5062. for(i = 0, j = s.length; i < j; i++) {
  5063. if(m[s[i]] && m[s[i]].parents) {
  5064. for(k = 0, l = m[s[i]].parents.length; k < l; k++) {
  5065. if(o[m[s[i]].parents[k]] !== undefined) {
  5066. break;
  5067. }
  5068. if(m[s[i]].parents[k] !== $.jstree.root) {
  5069. o[m[s[i]].parents[k]] = true;
  5070. p.push(m[s[i]].parents[k]);
  5071. }
  5072. }
  5073. }
  5074. }
  5075. // attempt for server side undetermined state
  5076. this.element.find('.jstree-closed').not(':has(.jstree-children)')
  5077. .each(function () {
  5078. var tmp = tt.get_node(this), tmp2;
  5079. if(!tmp.state.loaded) {
  5080. if(tmp.original && tmp.original.state && tmp.original.state.undetermined && tmp.original.state.undetermined === true) {
  5081. if(o[tmp.id] === undefined && tmp.id !== $.jstree.root) {
  5082. o[tmp.id] = true;
  5083. p.push(tmp.id);
  5084. }
  5085. for(k = 0, l = tmp.parents.length; k < l; k++) {
  5086. if(o[tmp.parents[k]] === undefined && tmp.parents[k] !== $.jstree.root) {
  5087. o[tmp.parents[k]] = true;
  5088. p.push(tmp.parents[k]);
  5089. }
  5090. }
  5091. }
  5092. }
  5093. else {
  5094. for(i = 0, j = tmp.children_d.length; i < j; i++) {
  5095. tmp2 = m[tmp.children_d[i]];
  5096. if(!tmp2.state.loaded && tmp2.original && tmp2.original.state && tmp2.original.state.undetermined && tmp2.original.state.undetermined === true) {
  5097. if(o[tmp2.id] === undefined && tmp2.id !== $.jstree.root) {
  5098. o[tmp2.id] = true;
  5099. p.push(tmp2.id);
  5100. }
  5101. for(k = 0, l = tmp2.parents.length; k < l; k++) {
  5102. if(o[tmp2.parents[k]] === undefined && tmp2.parents[k] !== $.jstree.root) {
  5103. o[tmp2.parents[k]] = true;
  5104. p.push(tmp2.parents[k]);
  5105. }
  5106. }
  5107. }
  5108. }
  5109. }
  5110. });
  5111. this.element.find('.jstree-undetermined').removeClass('jstree-undetermined');
  5112. for(i = 0, j = p.length; i < j; i++) {
  5113. if(!m[p[i]].state[ t ? 'selected' : 'checked' ]) {
  5114. s = this.get_node(p[i], true);
  5115. if(s && s.length) {
  5116. s.children('.jstree-anchor').children('.jstree-checkbox').addClass('jstree-undetermined');
  5117. }
  5118. }
  5119. }
  5120. };
  5121. this.redraw_node = function(obj, deep, is_callback, force_render) {
  5122. obj = parent.redraw_node.apply(this, arguments);
  5123. if(obj) {
  5124. var i, j, tmp = null, icon = null;
  5125. for(i = 0, j = obj.childNodes.length; i < j; i++) {
  5126. if(obj.childNodes[i] && obj.childNodes[i].className && obj.childNodes[i].className.indexOf("jstree-anchor") !== -1) {
  5127. tmp = obj.childNodes[i];
  5128. break;
  5129. }
  5130. }
  5131. if(tmp) {
  5132. if(!this.settings.checkbox.tie_selection && this._model.data[obj.id].state.checked) { tmp.className += ' jstree-checked'; }
  5133. icon = _i.cloneNode(false);
  5134. if(this._model.data[obj.id].state.checkbox_disabled) { icon.className += ' jstree-checkbox-disabled'; }
  5135. tmp.insertBefore(icon, tmp.childNodes[0]);
  5136. }
  5137. }
  5138. if(!is_callback && this.settings.checkbox.cascade.indexOf('undetermined') !== -1) {
  5139. if(this._data.checkbox.uto) { clearTimeout(this._data.checkbox.uto); }
  5140. this._data.checkbox.uto = setTimeout($.proxy(this._undetermined, this), 50);
  5141. }
  5142. return obj;
  5143. };
  5144. /**
  5145. * show the node checkbox icons
  5146. * @name show_checkboxes()
  5147. * @plugin checkbox
  5148. */
  5149. this.show_checkboxes = function () { this._data.core.themes.checkboxes = true; this.get_container_ul().removeClass("jstree-no-checkboxes"); };
  5150. /**
  5151. * hide the node checkbox icons
  5152. * @name hide_checkboxes()
  5153. * @plugin checkbox
  5154. */
  5155. this.hide_checkboxes = function () { this._data.core.themes.checkboxes = false; this.get_container_ul().addClass("jstree-no-checkboxes"); };
  5156. /**
  5157. * toggle the node icons
  5158. * @name toggle_checkboxes()
  5159. * @plugin checkbox
  5160. */
  5161. this.toggle_checkboxes = function () { if(this._data.core.themes.checkboxes) { this.hide_checkboxes(); } else { this.show_checkboxes(); } };
  5162. /**
  5163. * checks if a node is in an undetermined state
  5164. * @name is_undetermined(obj)
  5165. * @param {mixed} obj
  5166. * @return {Boolean}
  5167. */
  5168. this.is_undetermined = function (obj) {
  5169. obj = this.get_node(obj);
  5170. var s = this.settings.checkbox.cascade, i, j, t = this.settings.checkbox.tie_selection, d = this._data[ t ? 'core' : 'checkbox' ].selected, m = this._model.data;
  5171. if(!obj || obj.state[ t ? 'selected' : 'checked' ] === true || s.indexOf('undetermined') === -1 || (s.indexOf('down') === -1 && s.indexOf('up') === -1)) {
  5172. return false;
  5173. }
  5174. if(!obj.state.loaded && obj.original.state.undetermined === true) {
  5175. return true;
  5176. }
  5177. for(i = 0, j = obj.children_d.length; i < j; i++) {
  5178. if($.inArray(obj.children_d[i], d) !== -1 || (!m[obj.children_d[i]].state.loaded && m[obj.children_d[i]].original.state.undetermined)) {
  5179. return true;
  5180. }
  5181. }
  5182. return false;
  5183. };
  5184. /**
  5185. * disable a node's checkbox
  5186. * @name disable_checkbox(obj)
  5187. * @param {mixed} obj an array can be used too
  5188. * @trigger disable_checkbox.jstree
  5189. * @plugin checkbox
  5190. */
  5191. this.disable_checkbox = function (obj) {
  5192. var t1, t2, dom;
  5193. if($.isArray(obj)) {
  5194. obj = obj.slice();
  5195. for(t1 = 0, t2 = obj.length; t1 < t2; t1++) {
  5196. this.disable_checkbox(obj[t1]);
  5197. }
  5198. return true;
  5199. }
  5200. obj = this.get_node(obj);
  5201. if(!obj || obj.id === $.jstree.root) {
  5202. return false;
  5203. }
  5204. dom = this.get_node(obj, true);
  5205. if(!obj.state.checkbox_disabled) {
  5206. obj.state.checkbox_disabled = true;
  5207. if(dom && dom.length) {
  5208. dom.children('.jstree-anchor').children('.jstree-checkbox').addClass('jstree-checkbox-disabled');
  5209. }
  5210. /**
  5211. * triggered when an node's checkbox is disabled
  5212. * @event
  5213. * @name disable_checkbox.jstree
  5214. * @param {Object} node
  5215. * @plugin checkbox
  5216. */
  5217. this.trigger('disable_checkbox', { 'node' : obj });
  5218. }
  5219. };
  5220. /**
  5221. * enable a node's checkbox
  5222. * @name disable_checkbox(obj)
  5223. * @param {mixed} obj an array can be used too
  5224. * @trigger enable_checkbox.jstree
  5225. * @plugin checkbox
  5226. */
  5227. this.enable_checkbox = function (obj) {
  5228. var t1, t2, dom;
  5229. if($.isArray(obj)) {
  5230. obj = obj.slice();
  5231. for(t1 = 0, t2 = obj.length; t1 < t2; t1++) {
  5232. this.enable_checkbox(obj[t1]);
  5233. }
  5234. return true;
  5235. }
  5236. obj = this.get_node(obj);
  5237. if(!obj || obj.id === $.jstree.root) {
  5238. return false;
  5239. }
  5240. dom = this.get_node(obj, true);
  5241. if(obj.state.checkbox_disabled) {
  5242. obj.state.checkbox_disabled = false;
  5243. if(dom && dom.length) {
  5244. dom.children('.jstree-anchor').children('.jstree-checkbox').removeClass('jstree-checkbox-disabled');
  5245. }
  5246. /**
  5247. * triggered when an node's checkbox is enabled
  5248. * @event
  5249. * @name enable_checkbox.jstree
  5250. * @param {Object} node
  5251. * @plugin checkbox
  5252. */
  5253. this.trigger('enable_checkbox', { 'node' : obj });
  5254. }
  5255. };
  5256. this.activate_node = function (obj, e) {
  5257. if($(e.target).hasClass('jstree-checkbox-disabled')) {
  5258. return false;
  5259. }
  5260. if(this.settings.checkbox.tie_selection && (this.settings.checkbox.whole_node || $(e.target).hasClass('jstree-checkbox'))) {
  5261. e.ctrlKey = true;
  5262. }
  5263. if(this.settings.checkbox.tie_selection || (!this.settings.checkbox.whole_node && !$(e.target).hasClass('jstree-checkbox'))) {
  5264. return parent.activate_node.call(this, obj, e);
  5265. }
  5266. if(this.is_disabled(obj)) {
  5267. return false;
  5268. }
  5269. if(this.is_checked(obj)) {
  5270. this.uncheck_node(obj, e);
  5271. }
  5272. else {
  5273. this.check_node(obj, e);
  5274. }
  5275. this.trigger('activate_node', { 'node' : this.get_node(obj) });
  5276. };
  5277. /**
  5278. * check a node (only if tie_selection in checkbox settings is false, otherwise select_node will be called internally)
  5279. * @name check_node(obj)
  5280. * @param {mixed} obj an array can be used to check multiple nodes
  5281. * @trigger check_node.jstree
  5282. * @plugin checkbox
  5283. */
  5284. this.check_node = function (obj, e) {
  5285. if(this.settings.checkbox.tie_selection) { return this.select_node(obj, false, true, e); }
  5286. var dom, t1, t2, th;
  5287. if($.isArray(obj)) {
  5288. obj = obj.slice();
  5289. for(t1 = 0, t2 = obj.length; t1 < t2; t1++) {
  5290. this.check_node(obj[t1], e);
  5291. }
  5292. return true;
  5293. }
  5294. obj = this.get_node(obj);
  5295. if(!obj || obj.id === $.jstree.root) {
  5296. return false;
  5297. }
  5298. dom = this.get_node(obj, true);
  5299. if(!obj.state.checked) {
  5300. obj.state.checked = true;
  5301. this._data.checkbox.selected.push(obj.id);
  5302. if(dom && dom.length) {
  5303. dom.children('.jstree-anchor').addClass('jstree-checked');
  5304. }
  5305. /**
  5306. * triggered when an node is checked (only if tie_selection in checkbox settings is false)
  5307. * @event
  5308. * @name check_node.jstree
  5309. * @param {Object} node
  5310. * @param {Array} selected the current selection
  5311. * @param {Object} event the event (if any) that triggered this check_node
  5312. * @plugin checkbox
  5313. */
  5314. this.trigger('check_node', { 'node' : obj, 'selected' : this._data.checkbox.selected, 'event' : e });
  5315. }
  5316. };
  5317. /**
  5318. * uncheck a node (only if tie_selection in checkbox settings is false, otherwise deselect_node will be called internally)
  5319. * @name uncheck_node(obj)
  5320. * @param {mixed} obj an array can be used to uncheck multiple nodes
  5321. * @trigger uncheck_node.jstree
  5322. * @plugin checkbox
  5323. */
  5324. this.uncheck_node = function (obj, e) {
  5325. if(this.settings.checkbox.tie_selection) { return this.deselect_node(obj, false, e); }
  5326. var t1, t2, dom;
  5327. if($.isArray(obj)) {
  5328. obj = obj.slice();
  5329. for(t1 = 0, t2 = obj.length; t1 < t2; t1++) {
  5330. this.uncheck_node(obj[t1], e);
  5331. }
  5332. return true;
  5333. }
  5334. obj = this.get_node(obj);
  5335. if(!obj || obj.id === $.jstree.root) {
  5336. return false;
  5337. }
  5338. dom = this.get_node(obj, true);
  5339. if(obj.state.checked) {
  5340. obj.state.checked = false;
  5341. this._data.checkbox.selected = $.vakata.array_remove_item(this._data.checkbox.selected, obj.id);
  5342. if(dom.length) {
  5343. dom.children('.jstree-anchor').removeClass('jstree-checked');
  5344. }
  5345. /**
  5346. * triggered when an node is unchecked (only if tie_selection in checkbox settings is false)
  5347. * @event
  5348. * @name uncheck_node.jstree
  5349. * @param {Object} node
  5350. * @param {Array} selected the current selection
  5351. * @param {Object} event the event (if any) that triggered this uncheck_node
  5352. * @plugin checkbox
  5353. */
  5354. this.trigger('uncheck_node', { 'node' : obj, 'selected' : this._data.checkbox.selected, 'event' : e });
  5355. }
  5356. };
  5357. /**
  5358. * checks all nodes in the tree (only if tie_selection in checkbox settings is false, otherwise select_all will be called internally)
  5359. * @name check_all()
  5360. * @trigger check_all.jstree, changed.jstree
  5361. * @plugin checkbox
  5362. */
  5363. this.check_all = function () {
  5364. if(this.settings.checkbox.tie_selection) { return this.select_all(); }
  5365. var tmp = this._data.checkbox.selected.concat([]), i, j;
  5366. this._data.checkbox.selected = this._model.data[$.jstree.root].children_d.concat();
  5367. for(i = 0, j = this._data.checkbox.selected.length; i < j; i++) {
  5368. if(this._model.data[this._data.checkbox.selected[i]]) {
  5369. this._model.data[this._data.checkbox.selected[i]].state.checked = true;
  5370. }
  5371. }
  5372. this.redraw(true);
  5373. /**
  5374. * triggered when all nodes are checked (only if tie_selection in checkbox settings is false)
  5375. * @event
  5376. * @name check_all.jstree
  5377. * @param {Array} selected the current selection
  5378. * @plugin checkbox
  5379. */
  5380. this.trigger('check_all', { 'selected' : this._data.checkbox.selected });
  5381. };
  5382. /**
  5383. * uncheck all checked nodes (only if tie_selection in checkbox settings is false, otherwise deselect_all will be called internally)
  5384. * @name uncheck_all()
  5385. * @trigger uncheck_all.jstree
  5386. * @plugin checkbox
  5387. */
  5388. this.uncheck_all = function () {
  5389. if(this.settings.checkbox.tie_selection) { return this.deselect_all(); }
  5390. var tmp = this._data.checkbox.selected.concat([]), i, j;
  5391. for(i = 0, j = this._data.checkbox.selected.length; i < j; i++) {
  5392. if(this._model.data[this._data.checkbox.selected[i]]) {
  5393. this._model.data[this._data.checkbox.selected[i]].state.checked = false;
  5394. }
  5395. }
  5396. this._data.checkbox.selected = [];
  5397. this.element.find('.jstree-checked').removeClass('jstree-checked');
  5398. /**
  5399. * triggered when all nodes are unchecked (only if tie_selection in checkbox settings is false)
  5400. * @event
  5401. * @name uncheck_all.jstree
  5402. * @param {Object} node the previous selection
  5403. * @param {Array} selected the current selection
  5404. * @plugin checkbox
  5405. */
  5406. this.trigger('uncheck_all', { 'selected' : this._data.checkbox.selected, 'node' : tmp });
  5407. };
  5408. /**
  5409. * checks if a node is checked (if tie_selection is on in the settings this function will return the same as is_selected)
  5410. * @name is_checked(obj)
  5411. * @param {mixed} obj
  5412. * @return {Boolean}
  5413. * @plugin checkbox
  5414. */
  5415. this.is_checked = function (obj) {
  5416. if(this.settings.checkbox.tie_selection) { return this.is_selected(obj); }
  5417. obj = this.get_node(obj);
  5418. if(!obj || obj.id === $.jstree.root) { return false; }
  5419. return obj.state.checked;
  5420. };
  5421. /**
  5422. * get an array of all checked nodes (if tie_selection is on in the settings this function will return the same as get_selected)
  5423. * @name get_checked([full])
  5424. * @param {mixed} full if set to `true` the returned array will consist of the full node objects, otherwise - only IDs will be returned
  5425. * @return {Array}
  5426. * @plugin checkbox
  5427. */
  5428. this.get_checked = function (full) {
  5429. if(this.settings.checkbox.tie_selection) { return this.get_selected(full); }
  5430. return full ? $.map(this._data.checkbox.selected, $.proxy(function (i) { return this.get_node(i); }, this)) : this._data.checkbox.selected;
  5431. };
  5432. /**
  5433. * get an array of all top level checked nodes (ignoring children of checked nodes) (if tie_selection is on in the settings this function will return the same as get_top_selected)
  5434. * @name get_top_checked([full])
  5435. * @param {mixed} full if set to `true` the returned array will consist of the full node objects, otherwise - only IDs will be returned
  5436. * @return {Array}
  5437. * @plugin checkbox
  5438. */
  5439. this.get_top_checked = function (full) {
  5440. if(this.settings.checkbox.tie_selection) { return this.get_top_selected(full); }
  5441. var tmp = this.get_checked(true),
  5442. obj = {}, i, j, k, l;
  5443. for(i = 0, j = tmp.length; i < j; i++) {
  5444. obj[tmp[i].id] = tmp[i];
  5445. }
  5446. for(i = 0, j = tmp.length; i < j; i++) {
  5447. for(k = 0, l = tmp[i].children_d.length; k < l; k++) {
  5448. if(obj[tmp[i].children_d[k]]) {
  5449. delete obj[tmp[i].children_d[k]];
  5450. }
  5451. }
  5452. }
  5453. tmp = [];
  5454. for(i in obj) {
  5455. if(obj.hasOwnProperty(i)) {
  5456. tmp.push(i);
  5457. }
  5458. }
  5459. return full ? $.map(tmp, $.proxy(function (i) { return this.get_node(i); }, this)) : tmp;
  5460. };
  5461. /**
  5462. * get an array of all bottom level checked nodes (ignoring selected parents) (if tie_selection is on in the settings this function will return the same as get_bottom_selected)
  5463. * @name get_bottom_checked([full])
  5464. * @param {mixed} full if set to `true` the returned array will consist of the full node objects, otherwise - only IDs will be returned
  5465. * @return {Array}
  5466. * @plugin checkbox
  5467. */
  5468. this.get_bottom_checked = function (full) {
  5469. if(this.settings.checkbox.tie_selection) { return this.get_bottom_selected(full); }
  5470. var tmp = this.get_checked(true),
  5471. obj = [], i, j;
  5472. for(i = 0, j = tmp.length; i < j; i++) {
  5473. if(!tmp[i].children.length) {
  5474. obj.push(tmp[i].id);
  5475. }
  5476. }
  5477. return full ? $.map(obj, $.proxy(function (i) { return this.get_node(i); }, this)) : obj;
  5478. };
  5479. this.load_node = function (obj, callback) {
  5480. var k, l, i, j, c, tmp;
  5481. if(!$.isArray(obj) && !this.settings.checkbox.tie_selection) {
  5482. tmp = this.get_node(obj);
  5483. if(tmp && tmp.state.loaded) {
  5484. for(k = 0, l = tmp.children_d.length; k < l; k++) {
  5485. if(this._model.data[tmp.children_d[k]].state.checked) {
  5486. c = true;
  5487. this._data.checkbox.selected = $.vakata.array_remove_item(this._data.checkbox.selected, tmp.children_d[k]);
  5488. }
  5489. }
  5490. }
  5491. }
  5492. return parent.load_node.apply(this, arguments);
  5493. };
  5494. this.get_state = function () {
  5495. var state = parent.get_state.apply(this, arguments);
  5496. if(this.settings.checkbox.tie_selection) { return state; }
  5497. state.checkbox = this._data.checkbox.selected.slice();
  5498. return state;
  5499. };
  5500. this.set_state = function (state, callback) {
  5501. var res = parent.set_state.apply(this, arguments);
  5502. if(res && state.checkbox) {
  5503. if(!this.settings.checkbox.tie_selection) {
  5504. this.uncheck_all();
  5505. var _this = this;
  5506. $.each(state.checkbox, function (i, v) {
  5507. _this.check_node(v);
  5508. });
  5509. }
  5510. delete state.checkbox;
  5511. this.set_state(state, callback);
  5512. return false;
  5513. }
  5514. return res;
  5515. };
  5516. this.refresh = function (skip_loading, forget_state) {
  5517. if(!this.settings.checkbox.tie_selection) {
  5518. this._data.checkbox.selected = [];
  5519. }
  5520. return parent.refresh.apply(this, arguments);
  5521. };
  5522. };
  5523. // include the checkbox plugin by default
  5524. // $.jstree.defaults.plugins.push("checkbox");
  5525. /**
  5526. * ### Conditionalselect plugin
  5527. *
  5528. * This plugin allows defining a callback to allow or deny node selection by user input (activate node method).
  5529. */
  5530. /**
  5531. * a callback (function) which is invoked in the instance's scope and receives two arguments - the node and the event that triggered the `activate_node` call. Returning false prevents working with the node, returning true allows invoking activate_node. Defaults to returning `true`.
  5532. * @name $.jstree.defaults.checkbox.visible
  5533. * @plugin checkbox
  5534. */
  5535. $.jstree.defaults.conditionalselect = function () { return true; };
  5536. $.jstree.plugins.conditionalselect = function (options, parent) {
  5537. // own function
  5538. this.activate_node = function (obj, e) {
  5539. if(this.settings.conditionalselect.call(this, this.get_node(obj), e)) {
  5540. parent.activate_node.call(this, obj, e);
  5541. }
  5542. };
  5543. };
  5544. /**
  5545. * ### Contextmenu plugin
  5546. *
  5547. * Shows a context menu when a node is right-clicked.
  5548. */
  5549. /**
  5550. * stores all defaults for the contextmenu plugin
  5551. * @name $.jstree.defaults.contextmenu
  5552. * @plugin contextmenu
  5553. */
  5554. $.jstree.defaults.contextmenu = {
  5555. /**
  5556. * a boolean indicating if the node should be selected when the context menu is invoked on it. Defaults to `true`.
  5557. * @name $.jstree.defaults.contextmenu.select_node
  5558. * @plugin contextmenu
  5559. */
  5560. select_node : true,
  5561. /**
  5562. * a boolean indicating if the menu should be shown aligned with the node. Defaults to `true`, otherwise the mouse coordinates are used.
  5563. * @name $.jstree.defaults.contextmenu.show_at_node
  5564. * @plugin contextmenu
  5565. */
  5566. show_at_node : true,
  5567. /**
  5568. * an object of actions, or a function that accepts a node and a callback function and calls the callback function with an object of actions available for that node (you can also return the items too).
  5569. *
  5570. * Each action consists of a key (a unique name) and a value which is an object with the following properties (only label and action are required). Once a menu item is activated the `action` function will be invoked with an object containing the following keys: item - the contextmenu item definition as seen below, reference - the DOM node that was used (the tree node), element - the contextmenu DOM element, position - an object with x/y properties indicating the position of the menu.
  5571. *
  5572. * * `separator_before` - a boolean indicating if there should be a separator before this item
  5573. * * `separator_after` - a boolean indicating if there should be a separator after this item
  5574. * * `_disabled` - a boolean indicating if this action should be disabled
  5575. * * `label` - a string - the name of the action (could be a function returning a string)
  5576. * * `action` - a function to be executed if this item is chosen, the function will receive
  5577. * * `icon` - a string, 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
  5578. * * `shortcut` - keyCode which will trigger the action if the menu is open (for example `113` for rename, which equals F2)
  5579. * * `shortcut_label` - shortcut label (like for example `F2` for rename)
  5580. * * `submenu` - an object with the same structure as $.jstree.defaults.contextmenu.items which can be used to create a submenu - each key will be rendered as a separate option in a submenu that will appear once the current item is hovered
  5581. *
  5582. * @name $.jstree.defaults.contextmenu.items
  5583. * @plugin contextmenu
  5584. */
  5585. items : function (o, cb) { // Could be an object directly
  5586. return {
  5587. "create" : {
  5588. "separator_before" : false,
  5589. "separator_after" : true,
  5590. "_disabled" : false, //(this.check("create_node", data.reference, {}, "last")),
  5591. "label" : "Create",
  5592. "action" : function (data) {
  5593. var inst = $.jstree.reference(data.reference),
  5594. obj = inst.get_node(data.reference);
  5595. inst.create_node(obj, {}, "last", function (new_node) {
  5596. setTimeout(function () { inst.edit(new_node); },0);
  5597. });
  5598. }
  5599. },
  5600. "rename" : {
  5601. "separator_before" : false,
  5602. "separator_after" : false,
  5603. "_disabled" : false, //(this.check("rename_node", data.reference, this.get_parent(data.reference), "")),
  5604. "label" : "Rename",
  5605. /*!
  5606. "shortcut" : 113,
  5607. "shortcut_label" : 'F2',
  5608. "icon" : "glyphicon glyphicon-leaf",
  5609. */
  5610. "action" : function (data) {
  5611. var inst = $.jstree.reference(data.reference),
  5612. obj = inst.get_node(data.reference);
  5613. inst.edit(obj);
  5614. }
  5615. },
  5616. "remove" : {
  5617. "separator_before" : false,
  5618. "icon" : false,
  5619. "separator_after" : false,
  5620. "_disabled" : false, //(this.check("delete_node", data.reference, this.get_parent(data.reference), "")),
  5621. "label" : "Delete",
  5622. "action" : function (data) {
  5623. var inst = $.jstree.reference(data.reference),
  5624. obj = inst.get_node(data.reference);
  5625. if(inst.is_selected(obj)) {
  5626. inst.delete_node(inst.get_selected());
  5627. }
  5628. else {
  5629. inst.delete_node(obj);
  5630. }
  5631. }
  5632. },
  5633. "ccp" : {
  5634. "separator_before" : true,
  5635. "icon" : false,
  5636. "separator_after" : false,
  5637. "label" : "Edit",
  5638. "action" : false,
  5639. "submenu" : {
  5640. "cut" : {
  5641. "separator_before" : false,
  5642. "separator_after" : false,
  5643. "label" : "Cut",
  5644. "action" : function (data) {
  5645. var inst = $.jstree.reference(data.reference),
  5646. obj = inst.get_node(data.reference);
  5647. if(inst.is_selected(obj)) {
  5648. inst.cut(inst.get_top_selected());
  5649. }
  5650. else {
  5651. inst.cut(obj);
  5652. }
  5653. }
  5654. },
  5655. "copy" : {
  5656. "separator_before" : false,
  5657. "icon" : false,
  5658. "separator_after" : false,
  5659. "label" : "Copy",
  5660. "action" : function (data) {
  5661. var inst = $.jstree.reference(data.reference),
  5662. obj = inst.get_node(data.reference);
  5663. if(inst.is_selected(obj)) {
  5664. inst.copy(inst.get_top_selected());
  5665. }
  5666. else {
  5667. inst.copy(obj);
  5668. }
  5669. }
  5670. },
  5671. "paste" : {
  5672. "separator_before" : false,
  5673. "icon" : false,
  5674. "_disabled" : function (data) {
  5675. return !$.jstree.reference(data.reference).can_paste();
  5676. },
  5677. "separator_after" : false,
  5678. "label" : "Paste",
  5679. "action" : function (data) {
  5680. var inst = $.jstree.reference(data.reference),
  5681. obj = inst.get_node(data.reference);
  5682. inst.paste(obj);
  5683. }
  5684. }
  5685. }
  5686. }
  5687. };
  5688. }
  5689. };
  5690. $.jstree.plugins.contextmenu = function (options, parent) {
  5691. this.bind = function () {
  5692. parent.bind.call(this);
  5693. var last_ts = 0, cto = null, ex, ey;
  5694. this.element
  5695. .on("contextmenu.jstree", ".jstree-anchor", $.proxy(function (e, data) {
  5696. if (e.target.tagName.toLowerCase() === 'input') {
  5697. return;
  5698. }
  5699. e.preventDefault();
  5700. last_ts = e.ctrlKey ? +new Date() : 0;
  5701. if(data || cto) {
  5702. last_ts = (+new Date()) + 10000;
  5703. }
  5704. if(cto) {
  5705. clearTimeout(cto);
  5706. }
  5707. if(!this.is_loading(e.currentTarget)) {
  5708. this.show_contextmenu(e.currentTarget, e.pageX, e.pageY, e);
  5709. }
  5710. }, this))
  5711. .on("click.jstree", ".jstree-anchor", $.proxy(function (e) {
  5712. if(this._data.contextmenu.visible && (!last_ts || (+new Date()) - last_ts > 250)) { // work around safari & macOS ctrl+click
  5713. $.vakata.context.hide();
  5714. }
  5715. last_ts = 0;
  5716. }, this))
  5717. .on("touchstart.jstree", ".jstree-anchor", function (e) {
  5718. if(!e.originalEvent || !e.originalEvent.changedTouches || !e.originalEvent.changedTouches[0]) {
  5719. return;
  5720. }
  5721. ex = e.pageX;
  5722. ey = e.pageY;
  5723. cto = setTimeout(function () {
  5724. $(e.currentTarget).trigger('contextmenu', true);
  5725. }, 750);
  5726. })
  5727. .on('touchmove.vakata.jstree', function (e) {
  5728. if(cto && e.originalEvent && e.originalEvent.changedTouches && e.originalEvent.changedTouches[0] && (Math.abs(ex - e.pageX) > 50 || Math.abs(ey - e.pageY) > 50)) {
  5729. clearTimeout(cto);
  5730. }
  5731. })
  5732. .on('touchend.vakata.jstree', function (e) {
  5733. if(cto) {
  5734. clearTimeout(cto);
  5735. }
  5736. });
  5737. /*!
  5738. if(!('oncontextmenu' in document.body) && ('ontouchstart' in document.body)) {
  5739. var el = null, tm = null;
  5740. this.element
  5741. .on("touchstart", ".jstree-anchor", function (e) {
  5742. el = e.currentTarget;
  5743. tm = +new Date();
  5744. $(document).one("touchend", function (e) {
  5745. e.target = document.elementFromPoint(e.originalEvent.targetTouches[0].pageX - window.pageXOffset, e.originalEvent.targetTouches[0].pageY - window.pageYOffset);
  5746. e.currentTarget = e.target;
  5747. tm = ((+(new Date())) - tm);
  5748. if(e.target === el && tm > 600 && tm < 1000) {
  5749. e.preventDefault();
  5750. $(el).trigger('contextmenu', e);
  5751. }
  5752. el = null;
  5753. tm = null;
  5754. });
  5755. });
  5756. }
  5757. */
  5758. $(document).on("context_hide.vakata.jstree", $.proxy(function (e, data) {
  5759. this._data.contextmenu.visible = false;
  5760. data.reference.removeClass('jstree-context');
  5761. }, this));
  5762. };
  5763. this.teardown = function () {
  5764. if(this._data.contextmenu.visible) {
  5765. $.vakata.context.hide();
  5766. }
  5767. parent.teardown.call(this);
  5768. };
  5769. /**
  5770. * prepare and show the context menu for a node
  5771. * @name show_contextmenu(obj [, x, y])
  5772. * @param {mixed} obj the node
  5773. * @param {Number} x the x-coordinate relative to the document to show the menu at
  5774. * @param {Number} y the y-coordinate relative to the document to show the menu at
  5775. * @param {Object} e the event if available that triggered the contextmenu
  5776. * @plugin contextmenu
  5777. * @trigger show_contextmenu.jstree
  5778. */
  5779. this.show_contextmenu = function (obj, x, y, e) {
  5780. obj = this.get_node(obj);
  5781. if(!obj || obj.id === $.jstree.root) { return false; }
  5782. var s = this.settings.contextmenu,
  5783. d = this.get_node(obj, true),
  5784. a = d.children(".jstree-anchor"),
  5785. o = false,
  5786. i = false;
  5787. if(s.show_at_node || x === undefined || y === undefined) {
  5788. o = a.offset();
  5789. x = o.left;
  5790. y = o.top + this._data.core.li_height;
  5791. }
  5792. if(this.settings.contextmenu.select_node && !this.is_selected(obj)) {
  5793. this.activate_node(obj, e);
  5794. }
  5795. i = s.items;
  5796. if($.isFunction(i)) {
  5797. i = i.call(this, obj, $.proxy(function (i) {
  5798. this._show_contextmenu(obj, x, y, i);
  5799. }, this));
  5800. }
  5801. if($.isPlainObject(i)) {
  5802. this._show_contextmenu(obj, x, y, i);
  5803. }
  5804. };
  5805. /**
  5806. * show the prepared context menu for a node
  5807. * @name _show_contextmenu(obj, x, y, i)
  5808. * @param {mixed} obj the node
  5809. * @param {Number} x the x-coordinate relative to the document to show the menu at
  5810. * @param {Number} y the y-coordinate relative to the document to show the menu at
  5811. * @param {Number} i the object of items to show
  5812. * @plugin contextmenu
  5813. * @trigger show_contextmenu.jstree
  5814. * @private
  5815. */
  5816. this._show_contextmenu = function (obj, x, y, i) {
  5817. var d = this.get_node(obj, true),
  5818. a = d.children(".jstree-anchor");
  5819. $(document).one("context_show.vakata.jstree", $.proxy(function (e, data) {
  5820. var cls = 'jstree-contextmenu jstree-' + this.get_theme() + '-contextmenu';
  5821. $(data.element).addClass(cls);
  5822. a.addClass('jstree-context');
  5823. }, this));
  5824. this._data.contextmenu.visible = true;
  5825. $.vakata.context.show(a, { 'x' : x, 'y' : y }, i);
  5826. /**
  5827. * triggered when the contextmenu is shown for a node
  5828. * @event
  5829. * @name show_contextmenu.jstree
  5830. * @param {Object} node the node
  5831. * @param {Number} x the x-coordinate of the menu relative to the document
  5832. * @param {Number} y the y-coordinate of the menu relative to the document
  5833. * @plugin contextmenu
  5834. */
  5835. this.trigger('show_contextmenu', { "node" : obj, "x" : x, "y" : y });
  5836. };
  5837. };
  5838. // contextmenu helper
  5839. (function ($) {
  5840. var right_to_left = false,
  5841. vakata_context = {
  5842. element : false,
  5843. reference : false,
  5844. position_x : 0,
  5845. position_y : 0,
  5846. items : [],
  5847. html : "",
  5848. is_visible : false
  5849. };
  5850. $.vakata.context = {
  5851. settings : {
  5852. hide_onmouseleave : 0,
  5853. icons : true
  5854. },
  5855. _trigger : function (event_name) {
  5856. $(document).triggerHandler("context_" + event_name + ".vakata", {
  5857. "reference" : vakata_context.reference,
  5858. "element" : vakata_context.element,
  5859. "position" : {
  5860. "x" : vakata_context.position_x,
  5861. "y" : vakata_context.position_y
  5862. }
  5863. });
  5864. },
  5865. _execute : function (i) {
  5866. i = vakata_context.items[i];
  5867. return i && (!i._disabled || ($.isFunction(i._disabled) && !i._disabled({ "item" : i, "reference" : vakata_context.reference, "element" : vakata_context.element }))) && i.action ? i.action.call(null, {
  5868. "item" : i,
  5869. "reference" : vakata_context.reference,
  5870. "element" : vakata_context.element,
  5871. "position" : {
  5872. "x" : vakata_context.position_x,
  5873. "y" : vakata_context.position_y
  5874. }
  5875. }) : false;
  5876. },
  5877. _parse : function (o, is_callback) {
  5878. if(!o) { return false; }
  5879. if(!is_callback) {
  5880. vakata_context.html = "";
  5881. vakata_context.items = [];
  5882. }
  5883. var str = "",
  5884. sep = false,
  5885. tmp;
  5886. if(is_callback) { str += "<"+"ul>"; }
  5887. $.each(o, function (i, val) {
  5888. if(!val) { return true; }
  5889. vakata_context.items.push(val);
  5890. if(!sep && val.separator_before) {
  5891. str += "<"+"li class='vakata-context-separator'><"+"a href='#' " + ($.vakata.context.settings.icons ? '' : 'style="margin-left:0px;"') + ">&#160;<"+"/a><"+"/li>";
  5892. }
  5893. sep = false;
  5894. str += "<"+"li class='" + (val._class || "") + (val._disabled === true || ($.isFunction(val._disabled) && val._disabled({ "item" : val, "reference" : vakata_context.reference, "element" : vakata_context.element })) ? " vakata-contextmenu-disabled " : "") + "' "+(val.shortcut?" data-shortcut='"+val.shortcut+"' ":'')+">";
  5895. str += "<"+"a href='#' rel='" + (vakata_context.items.length - 1) + "'>";
  5896. if($.vakata.context.settings.icons) {
  5897. str += "<"+"i ";
  5898. if(val.icon) {
  5899. if(val.icon.indexOf("/") !== -1 || val.icon.indexOf(".") !== -1) { str += " style='background:url(\"" + val.icon + "\") center center no-repeat' "; }
  5900. else { str += " class='" + val.icon + "' "; }
  5901. }
  5902. str += "><"+"/i><"+"span class='vakata-contextmenu-sep'>&#160;<"+"/span>";
  5903. }
  5904. str += ($.isFunction(val.label) ? val.label({ "item" : i, "reference" : vakata_context.reference, "element" : vakata_context.element }) : val.label) + (val.shortcut?' <span class="vakata-contextmenu-shortcut vakata-contextmenu-shortcut-'+val.shortcut+'">'+ (val.shortcut_label || '') +'</span>':'') + "<"+"/a>";
  5905. if(val.submenu) {
  5906. tmp = $.vakata.context._parse(val.submenu, true);
  5907. if(tmp) { str += tmp; }
  5908. }
  5909. str += "<"+"/li>";
  5910. if(val.separator_after) {
  5911. str += "<"+"li class='vakata-context-separator'><"+"a href='#' " + ($.vakata.context.settings.icons ? '' : 'style="margin-left:0px;"') + ">&#160;<"+"/a><"+"/li>";
  5912. sep = true;
  5913. }
  5914. });
  5915. str = str.replace(/<li class\='vakata-context-separator'\><\/li\>$/,"");
  5916. if(is_callback) { str += "</ul>"; }
  5917. /**
  5918. * triggered on the document when the contextmenu is parsed (HTML is built)
  5919. * @event
  5920. * @plugin contextmenu
  5921. * @name context_parse.vakata
  5922. * @param {jQuery} reference the element that was right clicked
  5923. * @param {jQuery} element the DOM element of the menu itself
  5924. * @param {Object} position the x & y coordinates of the menu
  5925. */
  5926. if(!is_callback) { vakata_context.html = str; $.vakata.context._trigger("parse"); }
  5927. return str.length > 10 ? str : false;
  5928. },
  5929. _show_submenu : function (o) {
  5930. o = $(o);
  5931. if(!o.length || !o.children("ul").length) { return; }
  5932. var e = o.children("ul"),
  5933. xl = o.offset().left,
  5934. x = xl + o.outerWidth(),
  5935. y = o.offset().top,
  5936. w = e.width(),
  5937. h = e.height(),
  5938. dw = $(window).width() + $(window).scrollLeft(),
  5939. dh = $(window).height() + $(window).scrollTop();
  5940. // може да се спести е една проверка - дали няма някой от класовете вече нагоре
  5941. if(right_to_left) {
  5942. o[x - (w + 10 + o.outerWidth()) < 0 ? "addClass" : "removeClass"]("vakata-context-left");
  5943. }
  5944. else {
  5945. o[x + w > dw && xl > dw - x ? "addClass" : "removeClass"]("vakata-context-right");
  5946. }
  5947. if(y + h + 10 > dh) {
  5948. e.css("bottom","-1px");
  5949. }
  5950. //if does not fit - stick it to the side
  5951. if (o.hasClass('vakata-context-right')) {
  5952. if (xl < w) {
  5953. e.css("margin-right", xl - w);
  5954. }
  5955. } else {
  5956. if (dw - x < w) {
  5957. e.css("margin-left", dw - x - w);
  5958. }
  5959. }
  5960. e.show();
  5961. },
  5962. show : function (reference, position, data) {
  5963. var o, e, x, y, w, h, dw, dh, cond = true;
  5964. if(vakata_context.element && vakata_context.element.length) {
  5965. vakata_context.element.width('');
  5966. }
  5967. switch(cond) {
  5968. case (!position && !reference):
  5969. return false;
  5970. case (!!position && !!reference):
  5971. vakata_context.reference = reference;
  5972. vakata_context.position_x = position.x;
  5973. vakata_context.position_y = position.y;
  5974. break;
  5975. case (!position && !!reference):
  5976. vakata_context.reference = reference;
  5977. o = reference.offset();
  5978. vakata_context.position_x = o.left + reference.outerHeight();
  5979. vakata_context.position_y = o.top;
  5980. break;
  5981. case (!!position && !reference):
  5982. vakata_context.position_x = position.x;
  5983. vakata_context.position_y = position.y;
  5984. break;
  5985. }
  5986. if(!!reference && !data && $(reference).data('vakata_contextmenu')) {
  5987. data = $(reference).data('vakata_contextmenu');
  5988. }
  5989. if($.vakata.context._parse(data)) {
  5990. vakata_context.element.html(vakata_context.html);
  5991. }
  5992. if(vakata_context.items.length) {
  5993. vakata_context.element.appendTo("body");
  5994. e = vakata_context.element;
  5995. x = vakata_context.position_x;
  5996. y = vakata_context.position_y;
  5997. w = e.width();
  5998. h = e.height();
  5999. dw = $(window).width() + $(window).scrollLeft();
  6000. dh = $(window).height() + $(window).scrollTop();
  6001. if(right_to_left) {
  6002. x -= (e.outerWidth() - $(reference).outerWidth());
  6003. if(x < $(window).scrollLeft() + 20) {
  6004. x = $(window).scrollLeft() + 20;
  6005. }
  6006. }
  6007. if(x + w + 20 > dw) {
  6008. x = dw - (w + 20);
  6009. }
  6010. if(y + h + 20 > dh) {
  6011. y = dh - (h + 20);
  6012. }
  6013. vakata_context.element
  6014. .css({ "left" : x, "top" : y })
  6015. .show()
  6016. .find('a').first().focus().parent().addClass("vakata-context-hover");
  6017. vakata_context.is_visible = true;
  6018. /**
  6019. * triggered on the document when the contextmenu is shown
  6020. * @event
  6021. * @plugin contextmenu
  6022. * @name context_show.vakata
  6023. * @param {jQuery} reference the element that was right clicked
  6024. * @param {jQuery} element the DOM element of the menu itself
  6025. * @param {Object} position the x & y coordinates of the menu
  6026. */
  6027. $.vakata.context._trigger("show");
  6028. }
  6029. },
  6030. hide : function () {
  6031. if(vakata_context.is_visible) {
  6032. vakata_context.element.hide().find("ul").hide().end().find(':focus').blur().end().detach();
  6033. vakata_context.is_visible = false;
  6034. /**
  6035. * triggered on the document when the contextmenu is hidden
  6036. * @event
  6037. * @plugin contextmenu
  6038. * @name context_hide.vakata
  6039. * @param {jQuery} reference the element that was right clicked
  6040. * @param {jQuery} element the DOM element of the menu itself
  6041. * @param {Object} position the x & y coordinates of the menu
  6042. */
  6043. $.vakata.context._trigger("hide");
  6044. }
  6045. }
  6046. };
  6047. $(function () {
  6048. right_to_left = $("body").css("direction") === "rtl";
  6049. var to = false;
  6050. vakata_context.element = $("<ul class='vakata-context'></ul>");
  6051. vakata_context.element
  6052. .on("mouseenter", "li", function (e) {
  6053. e.stopImmediatePropagation();
  6054. if($.contains(this, e.relatedTarget)) {
  6055. // премахнато заради delegate mouseleave по-долу
  6056. // $(this).find(".vakata-context-hover").removeClass("vakata-context-hover");
  6057. return;
  6058. }
  6059. if(to) { clearTimeout(to); }
  6060. vakata_context.element.find(".vakata-context-hover").removeClass("vakata-context-hover").end();
  6061. $(this)
  6062. .siblings().find("ul").hide().end().end()
  6063. .parentsUntil(".vakata-context", "li").addBack().addClass("vakata-context-hover");
  6064. $.vakata.context._show_submenu(this);
  6065. })
  6066. // тестово - дали не натоварва?
  6067. .on("mouseleave", "li", function (e) {
  6068. if($.contains(this, e.relatedTarget)) { return; }
  6069. $(this).find(".vakata-context-hover").addBack().removeClass("vakata-context-hover");
  6070. })
  6071. .on("mouseleave", function (e) {
  6072. $(this).find(".vakata-context-hover").removeClass("vakata-context-hover");
  6073. if($.vakata.context.settings.hide_onmouseleave) {
  6074. to = setTimeout(
  6075. (function (t) {
  6076. return function () { $.vakata.context.hide(); };
  6077. }(this)), $.vakata.context.settings.hide_onmouseleave);
  6078. }
  6079. })
  6080. .on("click", "a", function (e) {
  6081. e.preventDefault();
  6082. //})
  6083. //.on("mouseup", "a", function (e) {
  6084. if(!$(this).blur().parent().hasClass("vakata-context-disabled") && $.vakata.context._execute($(this).attr("rel")) !== false) {
  6085. $.vakata.context.hide();
  6086. }
  6087. })
  6088. .on('keydown', 'a', function (e) {
  6089. var o = null;
  6090. switch(e.which) {
  6091. case 13:
  6092. case 32:
  6093. e.type = "mouseup";
  6094. e.preventDefault();
  6095. $(e.currentTarget).trigger(e);
  6096. break;
  6097. case 37:
  6098. if(vakata_context.is_visible) {
  6099. vakata_context.element.find(".vakata-context-hover").last().closest("li").first().find("ul").hide().find(".vakata-context-hover").removeClass("vakata-context-hover").end().end().children('a').focus();
  6100. e.stopImmediatePropagation();
  6101. e.preventDefault();
  6102. }
  6103. break;
  6104. case 38:
  6105. if(vakata_context.is_visible) {
  6106. o = vakata_context.element.find("ul:visible").addBack().last().children(".vakata-context-hover").removeClass("vakata-context-hover").prevAll("li:not(.vakata-context-separator)").first();
  6107. if(!o.length) { o = vakata_context.element.find("ul:visible").addBack().last().children("li:not(.vakata-context-separator)").last(); }
  6108. o.addClass("vakata-context-hover").children('a').focus();
  6109. e.stopImmediatePropagation();
  6110. e.preventDefault();
  6111. }
  6112. break;
  6113. case 39:
  6114. if(vakata_context.is_visible) {
  6115. vakata_context.element.find(".vakata-context-hover").last().children("ul").show().children("li:not(.vakata-context-separator)").removeClass("vakata-context-hover").first().addClass("vakata-context-hover").children('a').focus();
  6116. e.stopImmediatePropagation();
  6117. e.preventDefault();
  6118. }
  6119. break;
  6120. case 40:
  6121. if(vakata_context.is_visible) {
  6122. o = vakata_context.element.find("ul:visible").addBack().last().children(".vakata-context-hover").removeClass("vakata-context-hover").nextAll("li:not(.vakata-context-separator)").first();
  6123. if(!o.length) { o = vakata_context.element.find("ul:visible").addBack().last().children("li:not(.vakata-context-separator)").first(); }
  6124. o.addClass("vakata-context-hover").children('a').focus();
  6125. e.stopImmediatePropagation();
  6126. e.preventDefault();
  6127. }
  6128. break;
  6129. case 27:
  6130. $.vakata.context.hide();
  6131. e.preventDefault();
  6132. break;
  6133. default:
  6134. //console.log(e.which);
  6135. break;
  6136. }
  6137. })
  6138. .on('keydown', function (e) {
  6139. e.preventDefault();
  6140. var a = vakata_context.element.find('.vakata-contextmenu-shortcut-' + e.which).parent();
  6141. if(a.parent().not('.vakata-context-disabled')) {
  6142. a.click();
  6143. }
  6144. });
  6145. $(document)
  6146. .on("mousedown.vakata.jstree", function (e) {
  6147. if(vakata_context.is_visible && !$.contains(vakata_context.element[0], e.target)) {
  6148. $.vakata.context.hide();
  6149. }
  6150. })
  6151. .on("context_show.vakata.jstree", function (e, data) {
  6152. vakata_context.element.find("li:has(ul)").children("a").addClass("vakata-context-parent");
  6153. if(right_to_left) {
  6154. vakata_context.element.addClass("vakata-context-rtl").css("direction", "rtl");
  6155. }
  6156. // also apply a RTL class?
  6157. vakata_context.element.find("ul").hide().end();
  6158. });
  6159. });
  6160. }($));
  6161. // $.jstree.defaults.plugins.push("contextmenu");
  6162. /**
  6163. * ### Drag'n'drop plugin
  6164. *
  6165. * Enables dragging and dropping of nodes in the tree, resulting in a move or copy operations.
  6166. */
  6167. /**
  6168. * stores all defaults for the drag'n'drop plugin
  6169. * @name $.jstree.defaults.dnd
  6170. * @plugin dnd
  6171. */
  6172. $.jstree.defaults.dnd = {
  6173. /**
  6174. * a boolean indicating if a copy should be possible while dragging (by pressint the meta key or Ctrl). Defaults to `true`.
  6175. * @name $.jstree.defaults.dnd.copy
  6176. * @plugin dnd
  6177. */
  6178. copy : true,
  6179. /**
  6180. * a number indicating how long a node should remain hovered while dragging to be opened. Defaults to `500`.
  6181. * @name $.jstree.defaults.dnd.open_timeout
  6182. * @plugin dnd
  6183. */
  6184. open_timeout : 500,
  6185. /**
  6186. * a function invoked each time a node is about to be dragged, invoked in the tree's scope and receives the nodes about to be dragged as an argument (array) and the event that started the drag - return `false` to prevent dragging
  6187. * @name $.jstree.defaults.dnd.is_draggable
  6188. * @plugin dnd
  6189. */
  6190. is_draggable : true,
  6191. /**
  6192. * a boolean indicating if checks should constantly be made while the user is dragging the node (as opposed to checking only on drop), default is `true`
  6193. * @name $.jstree.defaults.dnd.check_while_dragging
  6194. * @plugin dnd
  6195. */
  6196. check_while_dragging : true,
  6197. /**
  6198. * a boolean indicating if nodes from this tree should only be copied with dnd (as opposed to moved), default is `false`
  6199. * @name $.jstree.defaults.dnd.always_copy
  6200. * @plugin dnd
  6201. */
  6202. always_copy : false,
  6203. /**
  6204. * when dropping a node "inside", this setting indicates the position the node should go to - it can be an integer or a string: "first" (same as 0) or "last", default is `0`
  6205. * @name $.jstree.defaults.dnd.inside_pos
  6206. * @plugin dnd
  6207. */
  6208. inside_pos : 0,
  6209. /**
  6210. * when starting the drag on a node that is selected this setting controls if all selected nodes are dragged or only the single node, default is `true`, which means all selected nodes are dragged when the drag is started on a selected node
  6211. * @name $.jstree.defaults.dnd.drag_selection
  6212. * @plugin dnd
  6213. */
  6214. drag_selection : true,
  6215. /**
  6216. * controls whether dnd works on touch devices. If left as boolean true dnd will work the same as in desktop browsers, which in some cases may impair scrolling. If set to boolean false dnd will not work on touch devices. There is a special third option - string "selected" which means only selected nodes can be dragged on touch devices.
  6217. * @name $.jstree.defaults.dnd.touch
  6218. * @plugin dnd
  6219. */
  6220. touch : true,
  6221. /**
  6222. * controls whether items can be dropped anywhere on the node, not just on the anchor, by default only the node anchor is a valid drop target. Works best with the wholerow plugin. If enabled on mobile depending on the interface it might be hard for the user to cancel the drop, since the whole tree container will be a valid drop target.
  6223. * @name $.jstree.defaults.dnd.large_drop_target
  6224. * @plugin dnd
  6225. */
  6226. large_drop_target : false,
  6227. /**
  6228. * controls whether a drag can be initiated from any part of the node and not just the text/icon part, works best with the wholerow plugin. Keep in mind it can cause problems with tree scrolling on mobile depending on the interface - in that case set the touch option to "selected".
  6229. * @name $.jstree.defaults.dnd.large_drag_target
  6230. * @plugin dnd
  6231. */
  6232. large_drag_target : false,
  6233. /**
  6234. * controls whether use HTML5 dnd api instead of classical. That will allow better integration of dnd events with other HTML5 controls.
  6235. * @reference http://caniuse.com/#feat=dragndrop
  6236. * @name $.jstree.defaults.dnd.use_html5
  6237. * @plugin dnd
  6238. */
  6239. use_html5: false
  6240. };
  6241. var drg, elm;
  6242. // TODO: now check works by checking for each node individually, how about max_children, unique, etc?
  6243. $.jstree.plugins.dnd = function (options, parent) {
  6244. this.init = function (el, options) {
  6245. parent.init.call(this, el, options);
  6246. this.settings.dnd.use_html5 = this.settings.dnd.use_html5 && ('draggable' in document.createElement('span'));
  6247. };
  6248. this.bind = function () {
  6249. parent.bind.call(this);
  6250. this.element
  6251. .on(this.settings.dnd.use_html5 ? 'dragstart.jstree' : 'mousedown.jstree touchstart.jstree', this.settings.dnd.large_drag_target ? '.jstree-node' : '.jstree-anchor', $.proxy(function (e) {
  6252. if(this.settings.dnd.large_drag_target && $(e.target).closest('.jstree-node')[0] !== e.currentTarget) {
  6253. return true;
  6254. }
  6255. if(e.type === "touchstart" && (!this.settings.dnd.touch || (this.settings.dnd.touch === 'selected' && !$(e.currentTarget).closest('.jstree-node').children('.jstree-anchor').hasClass('jstree-clicked')))) {
  6256. return true;
  6257. }
  6258. var obj = this.get_node(e.target),
  6259. mlt = this.is_selected(obj) && this.settings.dnd.drag_selection ? this.get_top_selected().length : 1,
  6260. txt = (mlt > 1 ? mlt + ' ' + this.get_string('nodes') : this.get_text(e.currentTarget));
  6261. if(this.settings.core.force_text) {
  6262. txt = $.vakata.html.escape(txt);
  6263. }
  6264. if(obj && obj.id && obj.id !== $.jstree.root && (e.which === 1 || e.type === "touchstart" || e.type === "dragstart") &&
  6265. (this.settings.dnd.is_draggable === true || ($.isFunction(this.settings.dnd.is_draggable) && this.settings.dnd.is_draggable.call(this, (mlt > 1 ? this.get_top_selected(true) : [obj]), e)))
  6266. ) {
  6267. drg = { 'jstree' : true, 'origin' : this, 'obj' : this.get_node(obj,true), 'nodes' : mlt > 1 ? this.get_top_selected() : [obj.id] };
  6268. elm = e.currentTarget;
  6269. if (this.settings.dnd.use_html5) {
  6270. $.vakata.dnd._trigger('start', e, { 'helper': $(), 'element': elm, 'data': drg });
  6271. } else {
  6272. this.element.trigger('mousedown.jstree');
  6273. return $.vakata.dnd.start(e, drg, '<div id="jstree-dnd" class="jstree-' + this.get_theme() + ' jstree-' + this.get_theme() + '-' + this.get_theme_variant() + ' ' + ( this.settings.core.themes.responsive ? ' jstree-dnd-responsive' : '' ) + '"><i class="jstree-icon jstree-er"></i>' + txt + '<ins class="jstree-copy" style="display:none;">+</ins></div>');
  6274. }
  6275. }
  6276. }, this));
  6277. if (this.settings.dnd.use_html5) {
  6278. this.element
  6279. .on('dragover.jstree', function (e) {
  6280. e.preventDefault();
  6281. $.vakata.dnd._trigger('move', e, { 'helper': $(), 'element': elm, 'data': drg });
  6282. return false;
  6283. })
  6284. //.on('dragenter.jstree', this.settings.dnd.large_drop_target ? '.jstree-node' : '.jstree-anchor', $.proxy(function (e) {
  6285. // e.preventDefault();
  6286. // $.vakata.dnd._trigger('move', e, { 'helper': $(), 'element': elm, 'data': drg });
  6287. // return false;
  6288. // }, this))
  6289. .on('drop.jstree', $.proxy(function (e) {
  6290. e.preventDefault();
  6291. $.vakata.dnd._trigger('stop', e, { 'helper': $(), 'element': elm, 'data': drg });
  6292. return false;
  6293. }, this));
  6294. }
  6295. };
  6296. this.redraw_node = function(obj, deep, callback, force_render) {
  6297. obj = parent.redraw_node.apply(this, arguments);
  6298. if (obj && this.settings.dnd.use_html5) {
  6299. if (this.settings.dnd.large_drag_target) {
  6300. obj.setAttribute('draggable', true);
  6301. } else {
  6302. var i, j, tmp = null;
  6303. for(i = 0, j = obj.childNodes.length; i < j; i++) {
  6304. if(obj.childNodes[i] && obj.childNodes[i].className && obj.childNodes[i].className.indexOf("jstree-anchor") !== -1) {
  6305. tmp = obj.childNodes[i];
  6306. break;
  6307. }
  6308. }
  6309. if(tmp) {
  6310. tmp.setAttribute('draggable', true);
  6311. }
  6312. }
  6313. }
  6314. return obj;
  6315. };
  6316. };
  6317. $(function() {
  6318. // bind only once for all instances
  6319. var lastmv = false,
  6320. laster = false,
  6321. lastev = false,
  6322. opento = false,
  6323. marker = $('<div id="jstree-marker">&#160;</div>').hide(); //.appendTo('body');
  6324. $(document)
  6325. .on('dnd_start.vakata.jstree', function (e, data) {
  6326. lastmv = false;
  6327. lastev = false;
  6328. if(!data || !data.data || !data.data.jstree) { return; }
  6329. marker.appendTo('body'); //.show();
  6330. })
  6331. .on('dnd_move.vakata.jstree', function (e, data) {
  6332. if(opento) {
  6333. if (!data.event || data.event.type !== 'dragover' || data.event.target !== lastev.target) {
  6334. clearTimeout(opento);
  6335. }
  6336. }
  6337. if(!data || !data.data || !data.data.jstree) { return; }
  6338. // if we are hovering the marker image do nothing (can happen on "inside" drags)
  6339. if(data.event.target.id && data.event.target.id === 'jstree-marker') {
  6340. return;
  6341. }
  6342. lastev = data.event;
  6343. var ins = $.jstree.reference(data.event.target),
  6344. ref = false,
  6345. off = false,
  6346. rel = false,
  6347. tmp, l, t, h, p, i, o, ok, t1, t2, op, ps, pr, ip, tm, is_copy;
  6348. // if we are over an instance
  6349. if(ins && ins._data && ins._data.dnd) {
  6350. marker.attr('class', 'jstree-' + ins.get_theme() + ( ins.settings.core.themes.responsive ? ' jstree-dnd-responsive' : '' ));
  6351. is_copy = data.data.origin && (data.data.origin.settings.dnd.always_copy || (data.data.origin.settings.dnd.copy && (data.event.metaKey || data.event.ctrlKey)));
  6352. data.helper
  6353. .children().attr('class', 'jstree-' + ins.get_theme() + ' jstree-' + ins.get_theme() + '-' + ins.get_theme_variant() + ' ' + ( ins.settings.core.themes.responsive ? ' jstree-dnd-responsive' : '' ))
  6354. .find('.jstree-copy').first()[ is_copy ? 'show' : 'hide' ]();
  6355. // if are hovering the container itself add a new root node
  6356. //console.log(data.event);
  6357. if( (data.event.target === ins.element[0] || data.event.target === ins.get_container_ul()[0]) && ins.get_container_ul().children().length === 0) {
  6358. ok = true;
  6359. for(t1 = 0, t2 = data.data.nodes.length; t1 < t2; t1++) {
  6360. ok = ok && ins.check( (data.data.origin && (data.data.origin.settings.dnd.always_copy || (data.data.origin.settings.dnd.copy && (data.event.metaKey || data.event.ctrlKey)) ) ? "copy_node" : "move_node"), (data.data.origin && data.data.origin !== ins ? data.data.origin.get_node(data.data.nodes[t1]) : data.data.nodes[t1]), $.jstree.root, 'last', { 'dnd' : true, 'ref' : ins.get_node($.jstree.root), 'pos' : 'i', 'origin' : data.data.origin, 'is_multi' : (data.data.origin && data.data.origin !== ins), 'is_foreign' : (!data.data.origin) });
  6361. if(!ok) { break; }
  6362. }
  6363. if(ok) {
  6364. lastmv = { 'ins' : ins, 'par' : $.jstree.root, 'pos' : 'last' };
  6365. marker.hide();
  6366. data.helper.find('.jstree-icon').first().removeClass('jstree-er').addClass('jstree-ok');
  6367. if (data.event.originalEvent && data.event.originalEvent.dataTransfer) {
  6368. data.event.originalEvent.dataTransfer.dropEffect = is_copy ? 'copy' : 'move';
  6369. }
  6370. return;
  6371. }
  6372. }
  6373. else {
  6374. // if we are hovering a tree node
  6375. ref = ins.settings.dnd.large_drop_target ? $(data.event.target).closest('.jstree-node').children('.jstree-anchor') : $(data.event.target).closest('.jstree-anchor');
  6376. if(ref && ref.length && ref.parent().is('.jstree-closed, .jstree-open, .jstree-leaf')) {
  6377. off = ref.offset();
  6378. rel = (data.event.pageY !== undefined ? data.event.pageY : data.event.originalEvent.pageY) - off.top;
  6379. h = ref.outerHeight();
  6380. if(rel < h / 3) {
  6381. o = ['b', 'i', 'a'];
  6382. }
  6383. else if(rel > h - h / 3) {
  6384. o = ['a', 'i', 'b'];
  6385. }
  6386. else {
  6387. o = rel > h / 2 ? ['i', 'a', 'b'] : ['i', 'b', 'a'];
  6388. }
  6389. $.each(o, function (j, v) {
  6390. switch(v) {
  6391. case 'b':
  6392. l = off.left - 6;
  6393. t = off.top;
  6394. p = ins.get_parent(ref);
  6395. i = ref.parent().index();
  6396. break;
  6397. case 'i':
  6398. ip = ins.settings.dnd.inside_pos;
  6399. tm = ins.get_node(ref.parent());
  6400. l = off.left - 2;
  6401. t = off.top + h / 2 + 1;
  6402. p = tm.id;
  6403. i = ip === 'first' ? 0 : (ip === 'last' ? tm.children.length : Math.min(ip, tm.children.length));
  6404. break;
  6405. case 'a':
  6406. l = off.left - 6;
  6407. t = off.top + h;
  6408. p = ins.get_parent(ref);
  6409. i = ref.parent().index() + 1;
  6410. break;
  6411. }
  6412. ok = true;
  6413. for(t1 = 0, t2 = data.data.nodes.length; t1 < t2; t1++) {
  6414. op = data.data.origin && (data.data.origin.settings.dnd.always_copy || (data.data.origin.settings.dnd.copy && (data.event.metaKey || data.event.ctrlKey))) ? "copy_node" : "move_node";
  6415. ps = i;
  6416. if(op === "move_node" && v === 'a' && (data.data.origin && data.data.origin === ins) && p === ins.get_parent(data.data.nodes[t1])) {
  6417. pr = ins.get_node(p);
  6418. if(ps > $.inArray(data.data.nodes[t1], pr.children)) {
  6419. ps -= 1;
  6420. }
  6421. }
  6422. ok = ok && ( (ins && ins.settings && ins.settings.dnd && ins.settings.dnd.check_while_dragging === false) || ins.check(op, (data.data.origin && data.data.origin !== ins ? data.data.origin.get_node(data.data.nodes[t1]) : data.data.nodes[t1]), p, ps, { 'dnd' : true, 'ref' : ins.get_node(ref.parent()), 'pos' : v, 'origin' : data.data.origin, 'is_multi' : (data.data.origin && data.data.origin !== ins), 'is_foreign' : (!data.data.origin) }) );
  6423. if(!ok) {
  6424. if(ins && ins.last_error) { laster = ins.last_error(); }
  6425. break;
  6426. }
  6427. }
  6428. if(v === 'i' && ref.parent().is('.jstree-closed') && ins.settings.dnd.open_timeout) {
  6429. opento = setTimeout((function (x, z) { return function () { x.open_node(z); }; }(ins, ref)), ins.settings.dnd.open_timeout);
  6430. }
  6431. if(ok) {
  6432. lastmv = { 'ins' : ins, 'par' : p, 'pos' : v === 'i' && ip === 'last' && i === 0 && !ins.is_loaded(tm) ? 'last' : i };
  6433. marker.css({ 'left' : l + 'px', 'top' : t + 'px' }).show();
  6434. data.helper.find('.jstree-icon').first().removeClass('jstree-er').addClass('jstree-ok');
  6435. if (data.event.originalEvent && data.event.originalEvent.dataTransfer) {
  6436. data.event.originalEvent.dataTransfer.dropEffect = is_copy ? 'copy' : 'move';
  6437. }
  6438. laster = {};
  6439. o = true;
  6440. return false;
  6441. }
  6442. });
  6443. if(o === true) { return; }
  6444. }
  6445. }
  6446. }
  6447. lastmv = false;
  6448. data.helper.find('.jstree-icon').removeClass('jstree-ok').addClass('jstree-er');
  6449. if (data.event.originalEvent && data.event.originalEvent.dataTransfer) {
  6450. data.event.originalEvent.dataTransfer.dropEffect = 'none';
  6451. }
  6452. marker.hide();
  6453. })
  6454. .on('dnd_scroll.vakata.jstree', function (e, data) {
  6455. if(!data || !data.data || !data.data.jstree) { return; }
  6456. marker.hide();
  6457. lastmv = false;
  6458. lastev = false;
  6459. data.helper.find('.jstree-icon').first().removeClass('jstree-ok').addClass('jstree-er');
  6460. })
  6461. .on('dnd_stop.vakata.jstree', function (e, data) {
  6462. if(opento) { clearTimeout(opento); }
  6463. if(!data || !data.data || !data.data.jstree) { return; }
  6464. marker.hide().detach();
  6465. var i, j, nodes = [];
  6466. if(lastmv) {
  6467. for(i = 0, j = data.data.nodes.length; i < j; i++) {
  6468. nodes[i] = data.data.origin ? data.data.origin.get_node(data.data.nodes[i]) : data.data.nodes[i];
  6469. }
  6470. lastmv.ins[ data.data.origin && (data.data.origin.settings.dnd.always_copy || (data.data.origin.settings.dnd.copy && (data.event.metaKey || data.event.ctrlKey))) ? 'copy_node' : 'move_node' ](nodes, lastmv.par, lastmv.pos, false, false, false, data.data.origin);
  6471. }
  6472. else {
  6473. i = $(data.event.target).closest('.jstree');
  6474. if(i.length && laster && laster.error && laster.error === 'check') {
  6475. i = i.jstree(true);
  6476. if(i) {
  6477. i.settings.core.error.call(this, laster);
  6478. }
  6479. }
  6480. }
  6481. lastev = false;
  6482. lastmv = false;
  6483. })
  6484. .on('keyup.jstree keydown.jstree', function (e, data) {
  6485. data = $.vakata.dnd._get();
  6486. if(data && data.data && data.data.jstree) {
  6487. if (e.type === "keyup" && e.which === 27) {
  6488. if (opento) { clearTimeout(opento); }
  6489. lastmv = false;
  6490. laster = false;
  6491. lastev = false;
  6492. opento = false;
  6493. marker.hide().detach();
  6494. $.vakata.dnd._clean();
  6495. } else {
  6496. data.helper.find('.jstree-copy').first()[ data.data.origin && (data.data.origin.settings.dnd.always_copy || (data.data.origin.settings.dnd.copy && (e.metaKey || e.ctrlKey))) ? 'show' : 'hide' ]();
  6497. if(lastev) {
  6498. lastev.metaKey = e.metaKey;
  6499. lastev.ctrlKey = e.ctrlKey;
  6500. $.vakata.dnd._trigger('move', lastev);
  6501. }
  6502. }
  6503. }
  6504. });
  6505. });
  6506. // helpers
  6507. (function ($) {
  6508. $.vakata.html = {
  6509. div : $('<div />'),
  6510. escape : function (str) {
  6511. return $.vakata.html.div.text(str).html();
  6512. },
  6513. strip : function (str) {
  6514. return $.vakata.html.div.empty().append($.parseHTML(str)).text();
  6515. }
  6516. };
  6517. // private variable
  6518. var vakata_dnd = {
  6519. element : false,
  6520. target : false,
  6521. is_down : false,
  6522. is_drag : false,
  6523. helper : false,
  6524. helper_w: 0,
  6525. data : false,
  6526. init_x : 0,
  6527. init_y : 0,
  6528. scroll_l: 0,
  6529. scroll_t: 0,
  6530. scroll_e: false,
  6531. scroll_i: false,
  6532. is_touch: false
  6533. };
  6534. $.vakata.dnd = {
  6535. settings : {
  6536. scroll_speed : 10,
  6537. scroll_proximity : 20,
  6538. helper_left : 5,
  6539. helper_top : 10,
  6540. threshold : 5,
  6541. threshold_touch : 50
  6542. },
  6543. _trigger : function (event_name, e, data) {
  6544. if (data === undefined) {
  6545. data = $.vakata.dnd._get();
  6546. }
  6547. data.event = e;
  6548. $(document).triggerHandler("dnd_" + event_name + ".vakata", data);
  6549. },
  6550. _get : function () {
  6551. return {
  6552. "data" : vakata_dnd.data,
  6553. "element" : vakata_dnd.element,
  6554. "helper" : vakata_dnd.helper
  6555. };
  6556. },
  6557. _clean : function () {
  6558. if(vakata_dnd.helper) { vakata_dnd.helper.remove(); }
  6559. if(vakata_dnd.scroll_i) { clearInterval(vakata_dnd.scroll_i); vakata_dnd.scroll_i = false; }
  6560. vakata_dnd = {
  6561. element : false,
  6562. target : false,
  6563. is_down : false,
  6564. is_drag : false,
  6565. helper : false,
  6566. helper_w: 0,
  6567. data : false,
  6568. init_x : 0,
  6569. init_y : 0,
  6570. scroll_l: 0,
  6571. scroll_t: 0,
  6572. scroll_e: false,
  6573. scroll_i: false,
  6574. is_touch: false
  6575. };
  6576. $(document).off("mousemove.vakata.jstree touchmove.vakata.jstree", $.vakata.dnd.drag);
  6577. $(document).off("mouseup.vakata.jstree touchend.vakata.jstree", $.vakata.dnd.stop);
  6578. },
  6579. _scroll : function (init_only) {
  6580. if(!vakata_dnd.scroll_e || (!vakata_dnd.scroll_l && !vakata_dnd.scroll_t)) {
  6581. if(vakata_dnd.scroll_i) { clearInterval(vakata_dnd.scroll_i); vakata_dnd.scroll_i = false; }
  6582. return false;
  6583. }
  6584. if(!vakata_dnd.scroll_i) {
  6585. vakata_dnd.scroll_i = setInterval($.vakata.dnd._scroll, 100);
  6586. return false;
  6587. }
  6588. if(init_only === true) { return false; }
  6589. var i = vakata_dnd.scroll_e.scrollTop(),
  6590. j = vakata_dnd.scroll_e.scrollLeft();
  6591. vakata_dnd.scroll_e.scrollTop(i + vakata_dnd.scroll_t * $.vakata.dnd.settings.scroll_speed);
  6592. vakata_dnd.scroll_e.scrollLeft(j + vakata_dnd.scroll_l * $.vakata.dnd.settings.scroll_speed);
  6593. if(i !== vakata_dnd.scroll_e.scrollTop() || j !== vakata_dnd.scroll_e.scrollLeft()) {
  6594. /**
  6595. * triggered on the document when a drag causes an element to scroll
  6596. * @event
  6597. * @plugin dnd
  6598. * @name dnd_scroll.vakata
  6599. * @param {Mixed} data any data supplied with the call to $.vakata.dnd.start
  6600. * @param {DOM} element the DOM element being dragged
  6601. * @param {jQuery} helper the helper shown next to the mouse
  6602. * @param {jQuery} event the element that is scrolling
  6603. */
  6604. $.vakata.dnd._trigger("scroll", vakata_dnd.scroll_e);
  6605. }
  6606. },
  6607. start : function (e, data, html) {
  6608. if(e.type === "touchstart" && e.originalEvent && e.originalEvent.changedTouches && e.originalEvent.changedTouches[0]) {
  6609. e.pageX = e.originalEvent.changedTouches[0].pageX;
  6610. e.pageY = e.originalEvent.changedTouches[0].pageY;
  6611. e.target = document.elementFromPoint(e.originalEvent.changedTouches[0].pageX - window.pageXOffset, e.originalEvent.changedTouches[0].pageY - window.pageYOffset);
  6612. }
  6613. if(vakata_dnd.is_drag) { $.vakata.dnd.stop({}); }
  6614. try {
  6615. e.currentTarget.unselectable = "on";
  6616. e.currentTarget.onselectstart = function() { return false; };
  6617. if(e.currentTarget.style) {
  6618. e.currentTarget.style.touchAction = "none";
  6619. e.currentTarget.style.msTouchAction = "none";
  6620. e.currentTarget.style.MozUserSelect = "none";
  6621. }
  6622. } catch(ignore) { }
  6623. vakata_dnd.init_x = e.pageX;
  6624. vakata_dnd.init_y = e.pageY;
  6625. vakata_dnd.data = data;
  6626. vakata_dnd.is_down = true;
  6627. vakata_dnd.element = e.currentTarget;
  6628. vakata_dnd.target = e.target;
  6629. vakata_dnd.is_touch = e.type === "touchstart";
  6630. if(html !== false) {
  6631. vakata_dnd.helper = $("<div id='vakata-dnd'></div>").html(html).css({
  6632. "display" : "block",
  6633. "margin" : "0",
  6634. "padding" : "0",
  6635. "position" : "absolute",
  6636. "top" : "-2000px",
  6637. "lineHeight" : "16px",
  6638. "zIndex" : "10000"
  6639. });
  6640. }
  6641. $(document).on("mousemove.vakata.jstree touchmove.vakata.jstree", $.vakata.dnd.drag);
  6642. $(document).on("mouseup.vakata.jstree touchend.vakata.jstree", $.vakata.dnd.stop);
  6643. return false;
  6644. },
  6645. drag : function (e) {
  6646. if(e.type === "touchmove" && e.originalEvent && e.originalEvent.changedTouches && e.originalEvent.changedTouches[0]) {
  6647. e.pageX = e.originalEvent.changedTouches[0].pageX;
  6648. e.pageY = e.originalEvent.changedTouches[0].pageY;
  6649. e.target = document.elementFromPoint(e.originalEvent.changedTouches[0].pageX - window.pageXOffset, e.originalEvent.changedTouches[0].pageY - window.pageYOffset);
  6650. }
  6651. if(!vakata_dnd.is_down) { return; }
  6652. if(!vakata_dnd.is_drag) {
  6653. if(
  6654. Math.abs(e.pageX - vakata_dnd.init_x) > (vakata_dnd.is_touch ? $.vakata.dnd.settings.threshold_touch : $.vakata.dnd.settings.threshold) ||
  6655. Math.abs(e.pageY - vakata_dnd.init_y) > (vakata_dnd.is_touch ? $.vakata.dnd.settings.threshold_touch : $.vakata.dnd.settings.threshold)
  6656. ) {
  6657. if(vakata_dnd.helper) {
  6658. vakata_dnd.helper.appendTo("body");
  6659. vakata_dnd.helper_w = vakata_dnd.helper.outerWidth();
  6660. }
  6661. vakata_dnd.is_drag = true;
  6662. /**
  6663. * triggered on the document when a drag starts
  6664. * @event
  6665. * @plugin dnd
  6666. * @name dnd_start.vakata
  6667. * @param {Mixed} data any data supplied with the call to $.vakata.dnd.start
  6668. * @param {DOM} element the DOM element being dragged
  6669. * @param {jQuery} helper the helper shown next to the mouse
  6670. * @param {Object} event the event that caused the start (probably mousemove)
  6671. */
  6672. $.vakata.dnd._trigger("start", e);
  6673. }
  6674. else { return; }
  6675. }
  6676. var d = false, w = false,
  6677. dh = false, wh = false,
  6678. dw = false, ww = false,
  6679. dt = false, dl = false,
  6680. ht = false, hl = false;
  6681. vakata_dnd.scroll_t = 0;
  6682. vakata_dnd.scroll_l = 0;
  6683. vakata_dnd.scroll_e = false;
  6684. $($(e.target).parentsUntil("body").addBack().get().reverse())
  6685. .filter(function () {
  6686. return (/^auto|scroll$/).test($(this).css("overflow")) &&
  6687. (this.scrollHeight > this.offsetHeight || this.scrollWidth > this.offsetWidth);
  6688. })
  6689. .each(function () {
  6690. var t = $(this), o = t.offset();
  6691. if(this.scrollHeight > this.offsetHeight) {
  6692. if(o.top + t.height() - e.pageY < $.vakata.dnd.settings.scroll_proximity) { vakata_dnd.scroll_t = 1; }
  6693. if(e.pageY - o.top < $.vakata.dnd.settings.scroll_proximity) { vakata_dnd.scroll_t = -1; }
  6694. }
  6695. if(this.scrollWidth > this.offsetWidth) {
  6696. if(o.left + t.width() - e.pageX < $.vakata.dnd.settings.scroll_proximity) { vakata_dnd.scroll_l = 1; }
  6697. if(e.pageX - o.left < $.vakata.dnd.settings.scroll_proximity) { vakata_dnd.scroll_l = -1; }
  6698. }
  6699. if(vakata_dnd.scroll_t || vakata_dnd.scroll_l) {
  6700. vakata_dnd.scroll_e = $(this);
  6701. return false;
  6702. }
  6703. });
  6704. if(!vakata_dnd.scroll_e) {
  6705. d = $(document); w = $(window);
  6706. dh = d.height(); wh = w.height();
  6707. dw = d.width(); ww = w.width();
  6708. dt = d.scrollTop(); dl = d.scrollLeft();
  6709. if(dh > wh && e.pageY - dt < $.vakata.dnd.settings.scroll_proximity) { vakata_dnd.scroll_t = -1; }
  6710. if(dh > wh && wh - (e.pageY - dt) < $.vakata.dnd.settings.scroll_proximity) { vakata_dnd.scroll_t = 1; }
  6711. if(dw > ww && e.pageX - dl < $.vakata.dnd.settings.scroll_proximity) { vakata_dnd.scroll_l = -1; }
  6712. if(dw > ww && ww - (e.pageX - dl) < $.vakata.dnd.settings.scroll_proximity) { vakata_dnd.scroll_l = 1; }
  6713. if(vakata_dnd.scroll_t || vakata_dnd.scroll_l) {
  6714. vakata_dnd.scroll_e = d;
  6715. }
  6716. }
  6717. if(vakata_dnd.scroll_e) { $.vakata.dnd._scroll(true); }
  6718. if(vakata_dnd.helper) {
  6719. ht = parseInt(e.pageY + $.vakata.dnd.settings.helper_top, 10);
  6720. hl = parseInt(e.pageX + $.vakata.dnd.settings.helper_left, 10);
  6721. if(dh && ht + 25 > dh) { ht = dh - 50; }
  6722. if(dw && hl + vakata_dnd.helper_w > dw) { hl = dw - (vakata_dnd.helper_w + 2); }
  6723. vakata_dnd.helper.css({
  6724. left : hl + "px",
  6725. top : ht + "px"
  6726. });
  6727. }
  6728. /**
  6729. * triggered on the document when a drag is in progress
  6730. * @event
  6731. * @plugin dnd
  6732. * @name dnd_move.vakata
  6733. * @param {Mixed} data any data supplied with the call to $.vakata.dnd.start
  6734. * @param {DOM} element the DOM element being dragged
  6735. * @param {jQuery} helper the helper shown next to the mouse
  6736. * @param {Object} event the event that caused this to trigger (most likely mousemove)
  6737. */
  6738. $.vakata.dnd._trigger("move", e);
  6739. return false;
  6740. },
  6741. stop : function (e) {
  6742. if(e.type === "touchend" && e.originalEvent && e.originalEvent.changedTouches && e.originalEvent.changedTouches[0]) {
  6743. e.pageX = e.originalEvent.changedTouches[0].pageX;
  6744. e.pageY = e.originalEvent.changedTouches[0].pageY;
  6745. e.target = document.elementFromPoint(e.originalEvent.changedTouches[0].pageX - window.pageXOffset, e.originalEvent.changedTouches[0].pageY - window.pageYOffset);
  6746. }
  6747. if(vakata_dnd.is_drag) {
  6748. /**
  6749. * triggered on the document when a drag stops (the dragged element is dropped)
  6750. * @event
  6751. * @plugin dnd
  6752. * @name dnd_stop.vakata
  6753. * @param {Mixed} data any data supplied with the call to $.vakata.dnd.start
  6754. * @param {DOM} element the DOM element being dragged
  6755. * @param {jQuery} helper the helper shown next to the mouse
  6756. * @param {Object} event the event that caused the stop
  6757. */
  6758. $.vakata.dnd._trigger("stop", e);
  6759. }
  6760. else {
  6761. if(e.type === "touchend" && e.target === vakata_dnd.target) {
  6762. var to = setTimeout(function () { $(e.target).click(); }, 100);
  6763. $(e.target).one('click', function() { if(to) { clearTimeout(to); } });
  6764. }
  6765. }
  6766. $.vakata.dnd._clean();
  6767. return false;
  6768. }
  6769. };
  6770. }($));
  6771. // include the dnd plugin by default
  6772. // $.jstree.defaults.plugins.push("dnd");
  6773. /**
  6774. * ### Massload plugin
  6775. *
  6776. * Adds massload functionality to jsTree, so that multiple nodes can be loaded in a single request (only useful with lazy loading).
  6777. */
  6778. /**
  6779. * massload configuration
  6780. *
  6781. * It is possible to set this to a standard jQuery-like AJAX config.
  6782. * 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 IDs need to be loaded, the return value of those functions will be used.
  6783. *
  6784. * You can also set this to a function, that function will receive the node IDs being loaded as argument and a second param which is a function (callback) which should be called with the result.
  6785. *
  6786. * Both the AJAX and the function approach rely on the same return value - an object where the keys are the node IDs, and the value is the children of that node as an array.
  6787. *
  6788. * {
  6789. * "id1" : [{ "text" : "Child of ID1", "id" : "c1" }, { "text" : "Another child of ID1", "id" : "c2" }],
  6790. * "id2" : [{ "text" : "Child of ID2", "id" : "c3" }]
  6791. * }
  6792. *
  6793. * @name $.jstree.defaults.massload
  6794. * @plugin massload
  6795. */
  6796. $.jstree.defaults.massload = null;
  6797. $.jstree.plugins.massload = function (options, parent) {
  6798. this.init = function (el, options) {
  6799. this._data.massload = {};
  6800. parent.init.call(this, el, options);
  6801. };
  6802. this._load_nodes = function (nodes, callback, is_callback, force_reload) {
  6803. var s = this.settings.massload,
  6804. nodesString = JSON.stringify(nodes),
  6805. toLoad = [],
  6806. m = this._model.data,
  6807. i, j, dom;
  6808. if (!is_callback) {
  6809. for(i = 0, j = nodes.length; i < j; i++) {
  6810. if(!m[nodes[i]] || ( (!m[nodes[i]].state.loaded && !m[nodes[i]].state.failed) || force_reload) ) {
  6811. toLoad.push(nodes[i]);
  6812. dom = this.get_node(nodes[i], true);
  6813. if (dom && dom.length) {
  6814. dom.addClass("jstree-loading").attr('aria-busy',true);
  6815. }
  6816. }
  6817. }
  6818. this._data.massload = {};
  6819. if (toLoad.length) {
  6820. if($.isFunction(s)) {
  6821. return s.call(this, toLoad, $.proxy(function (data) {
  6822. var i, j;
  6823. if(data) {
  6824. for(i in data) {
  6825. if(data.hasOwnProperty(i)) {
  6826. this._data.massload[i] = data[i];
  6827. }
  6828. }
  6829. }
  6830. for(i = 0, j = nodes.length; i < j; i++) {
  6831. dom = this.get_node(nodes[i], true);
  6832. if (dom && dom.length) {
  6833. dom.removeClass("jstree-loading").attr('aria-busy',false);
  6834. }
  6835. }
  6836. parent._load_nodes.call(this, nodes, callback, is_callback, force_reload);
  6837. }, this));
  6838. }
  6839. if(typeof s === 'object' && s && s.url) {
  6840. s = $.extend(true, {}, s);
  6841. if($.isFunction(s.url)) {
  6842. s.url = s.url.call(this, toLoad);
  6843. }
  6844. if($.isFunction(s.data)) {
  6845. s.data = s.data.call(this, toLoad);
  6846. }
  6847. return $.ajax(s)
  6848. .done($.proxy(function (data,t,x) {
  6849. var i, j;
  6850. if(data) {
  6851. for(i in data) {
  6852. if(data.hasOwnProperty(i)) {
  6853. this._data.massload[i] = data[i];
  6854. }
  6855. }
  6856. }
  6857. for(i = 0, j = nodes.length; i < j; i++) {
  6858. dom = this.get_node(nodes[i], true);
  6859. if (dom && dom.length) {
  6860. dom.removeClass("jstree-loading").attr('aria-busy',false);
  6861. }
  6862. }
  6863. parent._load_nodes.call(this, nodes, callback, is_callback, force_reload);
  6864. }, this))
  6865. .fail($.proxy(function (f) {
  6866. parent._load_nodes.call(this, nodes, callback, is_callback, force_reload);
  6867. }, this));
  6868. }
  6869. }
  6870. }
  6871. return parent._load_nodes.call(this, nodes, callback, is_callback, force_reload);
  6872. };
  6873. this._load_node = function (obj, callback) {
  6874. var data = this._data.massload[obj.id],
  6875. rslt = null, dom;
  6876. if(data) {
  6877. rslt = this[typeof data === 'string' ? '_append_html_data' : '_append_json_data'](
  6878. obj,
  6879. typeof data === 'string' ? $($.parseHTML(data)).filter(function () { return this.nodeType !== 3; }) : data,
  6880. function (status) { callback.call(this, status); }
  6881. );
  6882. dom = this.get_node(obj.id, true);
  6883. if (dom && dom.length) {
  6884. dom.removeClass("jstree-loading").attr('aria-busy',false);
  6885. }
  6886. delete this._data.massload[obj.id];
  6887. return rslt;
  6888. }
  6889. return parent._load_node.call(this, obj, callback);
  6890. };
  6891. };
  6892. /**
  6893. * ### Search plugin
  6894. *
  6895. * Adds search functionality to jsTree.
  6896. */
  6897. /**
  6898. * stores all defaults for the search plugin
  6899. * @name $.jstree.defaults.search
  6900. * @plugin search
  6901. */
  6902. $.jstree.defaults.search = {
  6903. /**
  6904. * a jQuery-like AJAX config, which jstree uses if a server should be queried for results.
  6905. *
  6906. * A `str` (which is the search string) parameter will be added with the request, an optional `inside` parameter will be added if the search is limited to a node id. The expected result is a JSON array with nodes that need to be opened so that matching nodes will be revealed.
  6907. * Leave this setting as `false` to not query the server. You can also set this to a function, which will be invoked in the instance's scope and receive 3 parameters - the search string, the callback to call with the array of nodes to load, and the optional node ID to limit the search to
  6908. * @name $.jstree.defaults.search.ajax
  6909. * @plugin search
  6910. */
  6911. ajax : false,
  6912. /**
  6913. * Indicates if the search should be fuzzy or not (should `chnd3` match `child node 3`). Default is `false`.
  6914. * @name $.jstree.defaults.search.fuzzy
  6915. * @plugin search
  6916. */
  6917. fuzzy : false,
  6918. /**
  6919. * Indicates if the search should be case sensitive. Default is `false`.
  6920. * @name $.jstree.defaults.search.case_sensitive
  6921. * @plugin search
  6922. */
  6923. case_sensitive : false,
  6924. /**
  6925. * Indicates if the tree should be filtered (by default) to show only matching nodes (keep in mind this can be a heavy on large trees in old browsers).
  6926. * This setting can be changed at runtime when calling the search method. Default is `false`.
  6927. * @name $.jstree.defaults.search.show_only_matches
  6928. * @plugin search
  6929. */
  6930. show_only_matches : false,
  6931. /**
  6932. * Indicates if the children of matched element are shown (when show_only_matches is true)
  6933. * This setting can be changed at runtime when calling the search method. Default is `false`.
  6934. * @name $.jstree.defaults.search.show_only_matches_children
  6935. * @plugin search
  6936. */
  6937. show_only_matches_children : false,
  6938. /**
  6939. * Indicates if all nodes opened to reveal the search result, should be closed when the search is cleared or a new search is performed. Default is `true`.
  6940. * @name $.jstree.defaults.search.close_opened_onclear
  6941. * @plugin search
  6942. */
  6943. close_opened_onclear : true,
  6944. /**
  6945. * Indicates if only leaf nodes should be included in search results. Default is `false`.
  6946. * @name $.jstree.defaults.search.search_leaves_only
  6947. * @plugin search
  6948. */
  6949. search_leaves_only : false,
  6950. /**
  6951. * If set to a function it wil be called in the instance's scope with two arguments - search string and node (where node will be every node in the structure, so use with caution).
  6952. * If the function returns a truthy value the node will be considered a match (it might not be displayed if search_only_leaves is set to true and the node is not a leaf). Default is `false`.
  6953. * @name $.jstree.defaults.search.search_callback
  6954. * @plugin search
  6955. */
  6956. search_callback : false
  6957. };
  6958. $.jstree.plugins.search = function (options, parent) {
  6959. this.bind = function () {
  6960. parent.bind.call(this);
  6961. this._data.search.str = "";
  6962. this._data.search.dom = $();
  6963. this._data.search.res = [];
  6964. this._data.search.opn = [];
  6965. this._data.search.som = false;
  6966. this._data.search.smc = false;
  6967. this._data.search.hdn = [];
  6968. this.element
  6969. .on("search.jstree", $.proxy(function (e, data) {
  6970. if(this._data.search.som && data.res.length) {
  6971. var m = this._model.data, i, j, p = [], k, l;
  6972. for(i = 0, j = data.res.length; i < j; i++) {
  6973. if(m[data.res[i]] && !m[data.res[i]].state.hidden) {
  6974. p.push(data.res[i]);
  6975. p = p.concat(m[data.res[i]].parents);
  6976. if(this._data.search.smc) {
  6977. for (k = 0, l = m[data.res[i]].children_d.length; k < l; k++) {
  6978. if (m[m[data.res[i]].children_d[k]] && !m[m[data.res[i]].children_d[k]].state.hidden) {
  6979. p.push(m[data.res[i]].children_d[k]);
  6980. }
  6981. }
  6982. }
  6983. }
  6984. }
  6985. p = $.vakata.array_remove_item($.vakata.array_unique(p), $.jstree.root);
  6986. this._data.search.hdn = this.hide_all(true);
  6987. this.show_node(p, true);
  6988. this.redraw(true);
  6989. }
  6990. }, this))
  6991. .on("clear_search.jstree", $.proxy(function (e, data) {
  6992. if(this._data.search.som && data.res.length) {
  6993. this.show_node(this._data.search.hdn, true);
  6994. this.redraw(true);
  6995. }
  6996. }, this));
  6997. };
  6998. /**
  6999. * used to search the tree nodes for a given string
  7000. * @name search(str [, skip_async])
  7001. * @param {String} str the search string
  7002. * @param {Boolean} skip_async if set to true server will not be queried even if configured
  7003. * @param {Boolean} show_only_matches if set to true only matching nodes will be shown (keep in mind this can be very slow on large trees or old browsers)
  7004. * @param {mixed} inside an optional node to whose children to limit the search
  7005. * @param {Boolean} append if set to true the results of this search are appended to the previous search
  7006. * @plugin search
  7007. * @trigger search.jstree
  7008. */
  7009. this.search = function (str, skip_async, show_only_matches, inside, append, show_only_matches_children) {
  7010. if(str === false || $.trim(str.toString()) === "") {
  7011. return this.clear_search();
  7012. }
  7013. inside = this.get_node(inside);
  7014. inside = inside && inside.id ? inside.id : null;
  7015. str = str.toString();
  7016. var s = this.settings.search,
  7017. a = s.ajax ? s.ajax : false,
  7018. m = this._model.data,
  7019. f = null,
  7020. r = [],
  7021. p = [], i, j;
  7022. if(this._data.search.res.length && !append) {
  7023. this.clear_search();
  7024. }
  7025. if(show_only_matches === undefined) {
  7026. show_only_matches = s.show_only_matches;
  7027. }
  7028. if(show_only_matches_children === undefined) {
  7029. show_only_matches_children = s.show_only_matches_children;
  7030. }
  7031. if(!skip_async && a !== false) {
  7032. if($.isFunction(a)) {
  7033. return a.call(this, str, $.proxy(function (d) {
  7034. if(d && d.d) { d = d.d; }
  7035. this._load_nodes(!$.isArray(d) ? [] : $.vakata.array_unique(d), function () {
  7036. this.search(str, true, show_only_matches, inside, append);
  7037. });
  7038. }, this), inside);
  7039. }
  7040. else {
  7041. a = $.extend({}, a);
  7042. if(!a.data) { a.data = {}; }
  7043. a.data.str = str;
  7044. if(inside) {
  7045. a.data.inside = inside;
  7046. }
  7047. return $.ajax(a)
  7048. .fail($.proxy(function () {
  7049. this._data.core.last_error = { 'error' : 'ajax', 'plugin' : 'search', 'id' : 'search_01', 'reason' : 'Could not load search parents', 'data' : JSON.stringify(a) };
  7050. this.settings.core.error.call(this, this._data.core.last_error);
  7051. }, this))
  7052. .done($.proxy(function (d) {
  7053. if(d && d.d) { d = d.d; }
  7054. this._load_nodes(!$.isArray(d) ? [] : $.vakata.array_unique(d), function () {
  7055. this.search(str, true, show_only_matches, inside, append);
  7056. });
  7057. }, this));
  7058. }
  7059. }
  7060. if(!append) {
  7061. this._data.search.str = str;
  7062. this._data.search.dom = $();
  7063. this._data.search.res = [];
  7064. this._data.search.opn = [];
  7065. this._data.search.som = show_only_matches;
  7066. this._data.search.smc = show_only_matches_children;
  7067. }
  7068. f = new $.vakata.search(str, true, { caseSensitive : s.case_sensitive, fuzzy : s.fuzzy });
  7069. $.each(m[inside ? inside : $.jstree.root].children_d, function (ii, i) {
  7070. var v = m[i];
  7071. if(v.text && !v.state.hidden && (!s.search_leaves_only || (v.state.loaded && v.children.length === 0)) && ( (s.search_callback && s.search_callback.call(this, str, v)) || (!s.search_callback && f.search(v.text).isMatch) ) ) {
  7072. r.push(i);
  7073. p = p.concat(v.parents);
  7074. }
  7075. });
  7076. if(r.length) {
  7077. p = $.vakata.array_unique(p);
  7078. for(i = 0, j = p.length; i < j; i++) {
  7079. if(p[i] !== $.jstree.root && m[p[i]] && this.open_node(p[i], null, 0) === true) {
  7080. this._data.search.opn.push(p[i]);
  7081. }
  7082. }
  7083. if(!append) {
  7084. this._data.search.dom = $(this.element[0].querySelectorAll('#' + $.map(r, function (v) { return "0123456789".indexOf(v[0]) !== -1 ? '\\3' + v[0] + ' ' + v.substr(1).replace($.jstree.idregex,'\\$&') : v.replace($.jstree.idregex,'\\$&'); }).join(', #')));
  7085. this._data.search.res = r;
  7086. }
  7087. else {
  7088. this._data.search.dom = this._data.search.dom.add($(this.element[0].querySelectorAll('#' + $.map(r, function (v) { return "0123456789".indexOf(v[0]) !== -1 ? '\\3' + v[0] + ' ' + v.substr(1).replace($.jstree.idregex,'\\$&') : v.replace($.jstree.idregex,'\\$&'); }).join(', #'))));
  7089. this._data.search.res = $.vakata.array_unique(this._data.search.res.concat(r));
  7090. }
  7091. this._data.search.dom.children(".jstree-anchor").addClass('jstree-search');
  7092. }
  7093. /**
  7094. * triggered after search is complete
  7095. * @event
  7096. * @name search.jstree
  7097. * @param {jQuery} nodes a jQuery collection of matching nodes
  7098. * @param {String} str the search string
  7099. * @param {Array} res a collection of objects represeing the matching nodes
  7100. * @plugin search
  7101. */
  7102. this.trigger('search', { nodes : this._data.search.dom, str : str, res : this._data.search.res, show_only_matches : show_only_matches });
  7103. };
  7104. /**
  7105. * used to clear the last search (removes classes and shows all nodes if filtering is on)
  7106. * @name clear_search()
  7107. * @plugin search
  7108. * @trigger clear_search.jstree
  7109. */
  7110. this.clear_search = function () {
  7111. if(this.settings.search.close_opened_onclear) {
  7112. this.close_node(this._data.search.opn, 0);
  7113. }
  7114. /**
  7115. * triggered after search is complete
  7116. * @event
  7117. * @name clear_search.jstree
  7118. * @param {jQuery} nodes a jQuery collection of matching nodes (the result from the last search)
  7119. * @param {String} str the search string (the last search string)
  7120. * @param {Array} res a collection of objects represeing the matching nodes (the result from the last search)
  7121. * @plugin search
  7122. */
  7123. this.trigger('clear_search', { 'nodes' : this._data.search.dom, str : this._data.search.str, res : this._data.search.res });
  7124. if(this._data.search.res.length) {
  7125. this._data.search.dom = $(this.element[0].querySelectorAll('#' + $.map(this._data.search.res, function (v) {
  7126. return "0123456789".indexOf(v[0]) !== -1 ? '\\3' + v[0] + ' ' + v.substr(1).replace($.jstree.idregex,'\\$&') : v.replace($.jstree.idregex,'\\$&');
  7127. }).join(', #')));
  7128. this._data.search.dom.children(".jstree-anchor").removeClass("jstree-search");
  7129. }
  7130. this._data.search.str = "";
  7131. this._data.search.res = [];
  7132. this._data.search.opn = [];
  7133. this._data.search.dom = $();
  7134. };
  7135. this.redraw_node = function(obj, deep, callback, force_render) {
  7136. obj = parent.redraw_node.apply(this, arguments);
  7137. if(obj) {
  7138. if($.inArray(obj.id, this._data.search.res) !== -1) {
  7139. var i, j, tmp = null;
  7140. for(i = 0, j = obj.childNodes.length; i < j; i++) {
  7141. if(obj.childNodes[i] && obj.childNodes[i].className && obj.childNodes[i].className.indexOf("jstree-anchor") !== -1) {
  7142. tmp = obj.childNodes[i];
  7143. break;
  7144. }
  7145. }
  7146. if(tmp) {
  7147. tmp.className += ' jstree-search';
  7148. }
  7149. }
  7150. }
  7151. return obj;
  7152. };
  7153. };
  7154. // helpers
  7155. (function ($) {
  7156. // from http://kiro.me/projects/fuse.html
  7157. $.vakata.search = function(pattern, txt, options) {
  7158. options = options || {};
  7159. options = $.extend({}, $.vakata.search.defaults, options);
  7160. if(options.fuzzy !== false) {
  7161. options.fuzzy = true;
  7162. }
  7163. pattern = options.caseSensitive ? pattern : pattern.toLowerCase();
  7164. var MATCH_LOCATION = options.location,
  7165. MATCH_DISTANCE = options.distance,
  7166. MATCH_THRESHOLD = options.threshold,
  7167. patternLen = pattern.length,
  7168. matchmask, pattern_alphabet, match_bitapScore, search;
  7169. if(patternLen > 32) {
  7170. options.fuzzy = false;
  7171. }
  7172. if(options.fuzzy) {
  7173. matchmask = 1 << (patternLen - 1);
  7174. pattern_alphabet = (function () {
  7175. var mask = {},
  7176. i = 0;
  7177. for (i = 0; i < patternLen; i++) {
  7178. mask[pattern.charAt(i)] = 0;
  7179. }
  7180. for (i = 0; i < patternLen; i++) {
  7181. mask[pattern.charAt(i)] |= 1 << (patternLen - i - 1);
  7182. }
  7183. return mask;
  7184. }());
  7185. match_bitapScore = function (e, x) {
  7186. var accuracy = e / patternLen,
  7187. proximity = Math.abs(MATCH_LOCATION - x);
  7188. if(!MATCH_DISTANCE) {
  7189. return proximity ? 1.0 : accuracy;
  7190. }
  7191. return accuracy + (proximity / MATCH_DISTANCE);
  7192. };
  7193. }
  7194. search = function (text) {
  7195. text = options.caseSensitive ? text : text.toLowerCase();
  7196. if(pattern === text || text.indexOf(pattern) !== -1) {
  7197. return {
  7198. isMatch: true,
  7199. score: 0
  7200. };
  7201. }
  7202. if(!options.fuzzy) {
  7203. return {
  7204. isMatch: false,
  7205. score: 1
  7206. };
  7207. }
  7208. var i, j,
  7209. textLen = text.length,
  7210. scoreThreshold = MATCH_THRESHOLD,
  7211. bestLoc = text.indexOf(pattern, MATCH_LOCATION),
  7212. binMin, binMid,
  7213. binMax = patternLen + textLen,
  7214. lastRd, start, finish, rd, charMatch,
  7215. score = 1,
  7216. locations = [];
  7217. if (bestLoc !== -1) {
  7218. scoreThreshold = Math.min(match_bitapScore(0, bestLoc), scoreThreshold);
  7219. bestLoc = text.lastIndexOf(pattern, MATCH_LOCATION + patternLen);
  7220. if (bestLoc !== -1) {
  7221. scoreThreshold = Math.min(match_bitapScore(0, bestLoc), scoreThreshold);
  7222. }
  7223. }
  7224. bestLoc = -1;
  7225. for (i = 0; i < patternLen; i++) {
  7226. binMin = 0;
  7227. binMid = binMax;
  7228. while (binMin < binMid) {
  7229. if (match_bitapScore(i, MATCH_LOCATION + binMid) <= scoreThreshold) {
  7230. binMin = binMid;
  7231. } else {
  7232. binMax = binMid;
  7233. }
  7234. binMid = Math.floor((binMax - binMin) / 2 + binMin);
  7235. }
  7236. binMax = binMid;
  7237. start = Math.max(1, MATCH_LOCATION - binMid + 1);
  7238. finish = Math.min(MATCH_LOCATION + binMid, textLen) + patternLen;
  7239. rd = new Array(finish + 2);
  7240. rd[finish + 1] = (1 << i) - 1;
  7241. for (j = finish; j >= start; j--) {
  7242. charMatch = pattern_alphabet[text.charAt(j - 1)];
  7243. if (i === 0) {
  7244. rd[j] = ((rd[j + 1] << 1) | 1) & charMatch;
  7245. } else {
  7246. rd[j] = ((rd[j + 1] << 1) | 1) & charMatch | (((lastRd[j + 1] | lastRd[j]) << 1) | 1) | lastRd[j + 1];
  7247. }
  7248. if (rd[j] & matchmask) {
  7249. score = match_bitapScore(i, j - 1);
  7250. if (score <= scoreThreshold) {
  7251. scoreThreshold = score;
  7252. bestLoc = j - 1;
  7253. locations.push(bestLoc);
  7254. if (bestLoc > MATCH_LOCATION) {
  7255. start = Math.max(1, 2 * MATCH_LOCATION - bestLoc);
  7256. } else {
  7257. break;
  7258. }
  7259. }
  7260. }
  7261. }
  7262. if (match_bitapScore(i + 1, MATCH_LOCATION) > scoreThreshold) {
  7263. break;
  7264. }
  7265. lastRd = rd;
  7266. }
  7267. return {
  7268. isMatch: bestLoc >= 0,
  7269. score: score
  7270. };
  7271. };
  7272. return txt === true ? { 'search' : search } : search(txt);
  7273. };
  7274. $.vakata.search.defaults = {
  7275. location : 0,
  7276. distance : 100,
  7277. threshold : 0.6,
  7278. fuzzy : false,
  7279. caseSensitive : false
  7280. };
  7281. }($));
  7282. // include the search plugin by default
  7283. // $.jstree.defaults.plugins.push("search");
  7284. /**
  7285. * ### Sort plugin
  7286. *
  7287. * Automatically sorts all siblings in the tree according to a sorting function.
  7288. */
  7289. /**
  7290. * the settings function used to sort the nodes.
  7291. * It is executed in the tree's context, accepts two nodes as arguments and should return `1` or `-1`.
  7292. * @name $.jstree.defaults.sort
  7293. * @plugin sort
  7294. */
  7295. $.jstree.defaults.sort = function (a, b) {
  7296. //return this.get_type(a) === this.get_type(b) ? (this.get_text(a) > this.get_text(b) ? 1 : -1) : this.get_type(a) >= this.get_type(b);
  7297. return this.get_text(a) > this.get_text(b) ? 1 : -1;
  7298. };
  7299. $.jstree.plugins.sort = function (options, parent) {
  7300. this.bind = function () {
  7301. parent.bind.call(this);
  7302. this.element
  7303. .on("model.jstree", $.proxy(function (e, data) {
  7304. this.sort(data.parent, true);
  7305. }, this))
  7306. .on("rename_node.jstree create_node.jstree", $.proxy(function (e, data) {
  7307. this.sort(data.parent || data.node.parent, false);
  7308. this.redraw_node(data.parent || data.node.parent, true);
  7309. }, this))
  7310. .on("move_node.jstree copy_node.jstree", $.proxy(function (e, data) {
  7311. this.sort(data.parent, false);
  7312. this.redraw_node(data.parent, true);
  7313. }, this));
  7314. };
  7315. /**
  7316. * used to sort a node's children
  7317. * @private
  7318. * @name sort(obj [, deep])
  7319. * @param {mixed} obj the node
  7320. * @param {Boolean} deep if set to `true` nodes are sorted recursively.
  7321. * @plugin sort
  7322. * @trigger search.jstree
  7323. */
  7324. this.sort = function (obj, deep) {
  7325. var i, j;
  7326. obj = this.get_node(obj);
  7327. if(obj && obj.children && obj.children.length) {
  7328. obj.children.sort($.proxy(this.settings.sort, this));
  7329. if(deep) {
  7330. for(i = 0, j = obj.children_d.length; i < j; i++) {
  7331. this.sort(obj.children_d[i], false);
  7332. }
  7333. }
  7334. }
  7335. };
  7336. };
  7337. // include the sort plugin by default
  7338. // $.jstree.defaults.plugins.push("sort");
  7339. /**
  7340. * ### State plugin
  7341. *
  7342. * Saves the state of the tree (selected nodes, opened nodes) on the user's computer using available options (localStorage, cookies, etc)
  7343. */
  7344. var to = false;
  7345. /**
  7346. * stores all defaults for the state plugin
  7347. * @name $.jstree.defaults.state
  7348. * @plugin state
  7349. */
  7350. $.jstree.defaults.state = {
  7351. /**
  7352. * A string for the key to use when saving the current tree (change if using multiple trees in your project). Defaults to `jstree`.
  7353. * @name $.jstree.defaults.state.key
  7354. * @plugin state
  7355. */
  7356. key : 'jstree',
  7357. /**
  7358. * A space separated list of events that trigger a state save. Defaults to `changed.jstree open_node.jstree close_node.jstree`.
  7359. * @name $.jstree.defaults.state.events
  7360. * @plugin state
  7361. */
  7362. events : 'changed.jstree open_node.jstree close_node.jstree check_node.jstree uncheck_node.jstree',
  7363. /**
  7364. * Time in milliseconds after which the state will expire. Defaults to 'false' meaning - no expire.
  7365. * @name $.jstree.defaults.state.ttl
  7366. * @plugin state
  7367. */
  7368. ttl : false,
  7369. /**
  7370. * A function that will be executed prior to restoring state with one argument - the state object. Can be used to clear unwanted parts of the state.
  7371. * @name $.jstree.defaults.state.filter
  7372. * @plugin state
  7373. */
  7374. filter : false
  7375. };
  7376. $.jstree.plugins.state = function (options, parent) {
  7377. this.bind = function () {
  7378. parent.bind.call(this);
  7379. var bind = $.proxy(function () {
  7380. this.element.on(this.settings.state.events, $.proxy(function () {
  7381. if(to) { clearTimeout(to); }
  7382. to = setTimeout($.proxy(function () { this.save_state(); }, this), 100);
  7383. }, this));
  7384. /**
  7385. * triggered when the state plugin is finished restoring the state (and immediately after ready if there is no state to restore).
  7386. * @event
  7387. * @name state_ready.jstree
  7388. * @plugin state
  7389. */
  7390. this.trigger('state_ready');
  7391. }, this);
  7392. this.element
  7393. .on("ready.jstree", $.proxy(function (e, data) {
  7394. this.element.one("restore_state.jstree", bind);
  7395. if(!this.restore_state()) { bind(); }
  7396. }, this));
  7397. };
  7398. /**
  7399. * save the state
  7400. * @name save_state()
  7401. * @plugin state
  7402. */
  7403. this.save_state = function () {
  7404. var st = { 'state' : this.get_state(), 'ttl' : this.settings.state.ttl, 'sec' : +(new Date()) };
  7405. $.vakata.storage.set(this.settings.state.key, JSON.stringify(st));
  7406. };
  7407. /**
  7408. * restore the state from the user's computer
  7409. * @name restore_state()
  7410. * @plugin state
  7411. */
  7412. this.restore_state = function () {
  7413. var k = $.vakata.storage.get(this.settings.state.key);
  7414. if(!!k) { try { k = JSON.parse(k); } catch(ex) { return false; } }
  7415. if(!!k && k.ttl && k.sec && +(new Date()) - k.sec > k.ttl) { return false; }
  7416. if(!!k && k.state) { k = k.state; }
  7417. if(!!k && $.isFunction(this.settings.state.filter)) { k = this.settings.state.filter.call(this, k); }
  7418. if(!!k) {
  7419. this.element.one("set_state.jstree", function (e, data) { data.instance.trigger('restore_state', { 'state' : $.extend(true, {}, k) }); });
  7420. this.set_state(k);
  7421. return true;
  7422. }
  7423. return false;
  7424. };
  7425. /**
  7426. * clear the state on the user's computer
  7427. * @name clear_state()
  7428. * @plugin state
  7429. */
  7430. this.clear_state = function () {
  7431. return $.vakata.storage.del(this.settings.state.key);
  7432. };
  7433. };
  7434. (function ($, undefined) {
  7435. $.vakata.storage = {
  7436. // simply specifying the functions in FF throws an error
  7437. set : function (key, val) { return window.localStorage.setItem(key, val); },
  7438. get : function (key) { return window.localStorage.getItem(key); },
  7439. del : function (key) { return window.localStorage.removeItem(key); }
  7440. };
  7441. }($));
  7442. // include the state plugin by default
  7443. // $.jstree.defaults.plugins.push("state");
  7444. /**
  7445. * ### Types plugin
  7446. *
  7447. * Makes it possible to add predefined types for groups of nodes, which make it possible to easily control nesting rules and icon for each group.
  7448. */
  7449. /**
  7450. * An object storing all types as key value pairs, where the key is the type name and the value is an object that could contain following keys (all optional).
  7451. *
  7452. * * `max_children` the maximum number of immediate children this node type can have. Do not specify or set to `-1` for unlimited.
  7453. * * `max_depth` the maximum number of nesting this node type can have. A value of `1` would mean that the node can have children, but no grandchildren. Do not specify or set to `-1` for unlimited.
  7454. * * `valid_children` an array of node type strings, that nodes of this type can have as children. Do not specify or set to `-1` for no limits.
  7455. * * `icon` a string - 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. Omit to use the default icon from your theme.
  7456. * * `li_attr` an object of values which will be used to add HTML attributes on the resulting LI DOM node (merged with the node's own data)
  7457. * * `a_attr` an object of values which will be used to add HTML attributes on the resulting A DOM node (merged with the node's own data)
  7458. *
  7459. * There are two predefined types:
  7460. *
  7461. * * `#` represents the root of the tree, for example `max_children` would control the maximum number of root nodes.
  7462. * * `default` represents the default node - any settings here will be applied to all nodes that do not have a type specified.
  7463. *
  7464. * @name $.jstree.defaults.types
  7465. * @plugin types
  7466. */
  7467. $.jstree.defaults.types = {
  7468. 'default' : {}
  7469. };
  7470. $.jstree.defaults.types[$.jstree.root] = {};
  7471. $.jstree.plugins.types = function (options, parent) {
  7472. this.init = function (el, options) {
  7473. var i, j;
  7474. if(options && options.types && options.types['default']) {
  7475. for(i in options.types) {
  7476. if(i !== "default" && i !== $.jstree.root && options.types.hasOwnProperty(i)) {
  7477. for(j in options.types['default']) {
  7478. if(options.types['default'].hasOwnProperty(j) && options.types[i][j] === undefined) {
  7479. options.types[i][j] = options.types['default'][j];
  7480. }
  7481. }
  7482. }
  7483. }
  7484. }
  7485. parent.init.call(this, el, options);
  7486. this._model.data[$.jstree.root].type = $.jstree.root;
  7487. };
  7488. this.refresh = function (skip_loading, forget_state) {
  7489. parent.refresh.call(this, skip_loading, forget_state);
  7490. this._model.data[$.jstree.root].type = $.jstree.root;
  7491. };
  7492. this.bind = function () {
  7493. this.element
  7494. .on('model.jstree', $.proxy(function (e, data) {
  7495. var m = this._model.data,
  7496. dpc = data.nodes,
  7497. t = this.settings.types,
  7498. i, j, c = 'default', k;
  7499. for(i = 0, j = dpc.length; i < j; i++) {
  7500. c = 'default';
  7501. if(m[dpc[i]].original && m[dpc[i]].original.type && t[m[dpc[i]].original.type]) {
  7502. c = m[dpc[i]].original.type;
  7503. }
  7504. if(m[dpc[i]].data && m[dpc[i]].data.jstree && m[dpc[i]].data.jstree.type && t[m[dpc[i]].data.jstree.type]) {
  7505. c = m[dpc[i]].data.jstree.type;
  7506. }
  7507. m[dpc[i]].type = c;
  7508. if(m[dpc[i]].icon === true && t[c].icon !== undefined) {
  7509. m[dpc[i]].icon = t[c].icon;
  7510. }
  7511. if(t[c].li_attr !== undefined && typeof t[c].li_attr === 'object') {
  7512. for (k in t[c].li_attr) {
  7513. if (t[c].li_attr.hasOwnProperty(k)) {
  7514. if (k === 'id') {
  7515. continue;
  7516. }
  7517. else if (m[dpc[i]].li_attr[k] === undefined) {
  7518. m[dpc[i]].li_attr[k] = t[c].li_attr[k];
  7519. }
  7520. else if (k === 'class') {
  7521. m[dpc[i]].li_attr['class'] = t[c].li_attr['class'] + ' ' + m[dpc[i]].li_attr['class'];
  7522. }
  7523. }
  7524. }
  7525. }
  7526. if(t[c].a_attr !== undefined && typeof t[c].a_attr === 'object') {
  7527. for (k in t[c].a_attr) {
  7528. if (t[c].a_attr.hasOwnProperty(k)) {
  7529. if (k === 'id') {
  7530. continue;
  7531. }
  7532. else if (m[dpc[i]].a_attr[k] === undefined) {
  7533. m[dpc[i]].a_attr[k] = t[c].a_attr[k];
  7534. }
  7535. else if (k === 'href' && m[dpc[i]].a_attr[k] === '#') {
  7536. m[dpc[i]].a_attr['href'] = t[c].a_attr['href'];
  7537. }
  7538. else if (k === 'class') {
  7539. m[dpc[i]].a_attr['class'] = t[c].a_attr['class'] + ' ' + m[dpc[i]].a_attr['class'];
  7540. }
  7541. }
  7542. }
  7543. }
  7544. }
  7545. m[$.jstree.root].type = $.jstree.root;
  7546. }, this));
  7547. parent.bind.call(this);
  7548. };
  7549. this.get_json = function (obj, options, flat) {
  7550. var i, j,
  7551. m = this._model.data,
  7552. opt = options ? $.extend(true, {}, options, {no_id:false}) : {},
  7553. tmp = parent.get_json.call(this, obj, opt, flat);
  7554. if(tmp === false) { return false; }
  7555. if($.isArray(tmp)) {
  7556. for(i = 0, j = tmp.length; i < j; i++) {
  7557. tmp[i].type = tmp[i].id && m[tmp[i].id] && m[tmp[i].id].type ? m[tmp[i].id].type : "default";
  7558. if(options && options.no_id) {
  7559. delete tmp[i].id;
  7560. if(tmp[i].li_attr && tmp[i].li_attr.id) {
  7561. delete tmp[i].li_attr.id;
  7562. }
  7563. if(tmp[i].a_attr && tmp[i].a_attr.id) {
  7564. delete tmp[i].a_attr.id;
  7565. }
  7566. }
  7567. }
  7568. }
  7569. else {
  7570. tmp.type = tmp.id && m[tmp.id] && m[tmp.id].type ? m[tmp.id].type : "default";
  7571. if(options && options.no_id) {
  7572. tmp = this._delete_ids(tmp);
  7573. }
  7574. }
  7575. return tmp;
  7576. };
  7577. this._delete_ids = function (tmp) {
  7578. if($.isArray(tmp)) {
  7579. for(var i = 0, j = tmp.length; i < j; i++) {
  7580. tmp[i] = this._delete_ids(tmp[i]);
  7581. }
  7582. return tmp;
  7583. }
  7584. delete tmp.id;
  7585. if(tmp.li_attr && tmp.li_attr.id) {
  7586. delete tmp.li_attr.id;
  7587. }
  7588. if(tmp.a_attr && tmp.a_attr.id) {
  7589. delete tmp.a_attr.id;
  7590. }
  7591. if(tmp.children && $.isArray(tmp.children)) {
  7592. tmp.children = this._delete_ids(tmp.children);
  7593. }
  7594. return tmp;
  7595. };
  7596. this.check = function (chk, obj, par, pos, more) {
  7597. if(parent.check.call(this, chk, obj, par, pos, more) === false) { return false; }
  7598. obj = obj && obj.id ? obj : this.get_node(obj);
  7599. par = par && par.id ? par : this.get_node(par);
  7600. var m = obj && obj.id ? (more && more.origin ? more.origin : $.jstree.reference(obj.id)) : null, tmp, d, i, j;
  7601. m = m && m._model && m._model.data ? m._model.data : null;
  7602. switch(chk) {
  7603. case "create_node":
  7604. case "move_node":
  7605. case "copy_node":
  7606. if(chk !== 'move_node' || $.inArray(obj.id, par.children) === -1) {
  7607. tmp = this.get_rules(par);
  7608. if(tmp.max_children !== undefined && tmp.max_children !== -1 && tmp.max_children === par.children.length) {
  7609. this._data.core.last_error = { 'error' : 'check', 'plugin' : 'types', 'id' : 'types_01', 'reason' : 'max_children prevents function: ' + chk, 'data' : JSON.stringify({ 'chk' : chk, 'pos' : pos, 'obj' : obj && obj.id ? obj.id : false, 'par' : par && par.id ? par.id : false }) };
  7610. return false;
  7611. }
  7612. if(tmp.valid_children !== undefined && tmp.valid_children !== -1 && $.inArray((obj.type || 'default'), tmp.valid_children) === -1) {
  7613. this._data.core.last_error = { 'error' : 'check', 'plugin' : 'types', 'id' : 'types_02', 'reason' : 'valid_children prevents function: ' + chk, 'data' : JSON.stringify({ 'chk' : chk, 'pos' : pos, 'obj' : obj && obj.id ? obj.id : false, 'par' : par && par.id ? par.id : false }) };
  7614. return false;
  7615. }
  7616. if(m && obj.children_d && obj.parents) {
  7617. d = 0;
  7618. for(i = 0, j = obj.children_d.length; i < j; i++) {
  7619. d = Math.max(d, m[obj.children_d[i]].parents.length);
  7620. }
  7621. d = d - obj.parents.length + 1;
  7622. }
  7623. if(d <= 0 || d === undefined) { d = 1; }
  7624. do {
  7625. if(tmp.max_depth !== undefined && tmp.max_depth !== -1 && tmp.max_depth < d) {
  7626. this._data.core.last_error = { 'error' : 'check', 'plugin' : 'types', 'id' : 'types_03', 'reason' : 'max_depth prevents function: ' + chk, 'data' : JSON.stringify({ 'chk' : chk, 'pos' : pos, 'obj' : obj && obj.id ? obj.id : false, 'par' : par && par.id ? par.id : false }) };
  7627. return false;
  7628. }
  7629. par = this.get_node(par.parent);
  7630. tmp = this.get_rules(par);
  7631. d++;
  7632. } while(par);
  7633. }
  7634. break;
  7635. }
  7636. return true;
  7637. };
  7638. /**
  7639. * used to retrieve the type settings object for a node
  7640. * @name get_rules(obj)
  7641. * @param {mixed} obj the node to find the rules for
  7642. * @return {Object}
  7643. * @plugin types
  7644. */
  7645. this.get_rules = function (obj) {
  7646. obj = this.get_node(obj);
  7647. if(!obj) { return false; }
  7648. var tmp = this.get_type(obj, true);
  7649. if(tmp.max_depth === undefined) { tmp.max_depth = -1; }
  7650. if(tmp.max_children === undefined) { tmp.max_children = -1; }
  7651. if(tmp.valid_children === undefined) { tmp.valid_children = -1; }
  7652. return tmp;
  7653. };
  7654. /**
  7655. * used to retrieve the type string or settings object for a node
  7656. * @name get_type(obj [, rules])
  7657. * @param {mixed} obj the node to find the rules for
  7658. * @param {Boolean} rules if set to `true` instead of a string the settings object will be returned
  7659. * @return {String|Object}
  7660. * @plugin types
  7661. */
  7662. this.get_type = function (obj, rules) {
  7663. obj = this.get_node(obj);
  7664. return (!obj) ? false : ( rules ? $.extend({ 'type' : obj.type }, this.settings.types[obj.type]) : obj.type);
  7665. };
  7666. /**
  7667. * used to change a node's type
  7668. * @name set_type(obj, type)
  7669. * @param {mixed} obj the node to change
  7670. * @param {String} type the new type
  7671. * @plugin types
  7672. */
  7673. this.set_type = function (obj, type) {
  7674. var m = this._model.data, t, t1, t2, old_type, old_icon, k, d, a;
  7675. if($.isArray(obj)) {
  7676. obj = obj.slice();
  7677. for(t1 = 0, t2 = obj.length; t1 < t2; t1++) {
  7678. this.set_type(obj[t1], type);
  7679. }
  7680. return true;
  7681. }
  7682. t = this.settings.types;
  7683. obj = this.get_node(obj);
  7684. if(!t[type] || !obj) { return false; }
  7685. d = this.get_node(obj, true);
  7686. if (d && d.length) {
  7687. a = d.children('.jstree-anchor');
  7688. }
  7689. old_type = obj.type;
  7690. old_icon = this.get_icon(obj);
  7691. obj.type = type;
  7692. if(old_icon === true || (t[old_type] && t[old_type].icon !== undefined && old_icon === t[old_type].icon)) {
  7693. this.set_icon(obj, t[type].icon !== undefined ? t[type].icon : true);
  7694. }
  7695. // remove old type props
  7696. if(t[old_type].li_attr !== undefined && typeof t[old_type].li_attr === 'object') {
  7697. for (k in t[old_type].li_attr) {
  7698. if (t[old_type].li_attr.hasOwnProperty(k)) {
  7699. if (k === 'id') {
  7700. continue;
  7701. }
  7702. else if (k === 'class') {
  7703. m[obj.id].li_attr['class'] = (m[obj.id].li_attr['class'] || '').replace(t[old_type].li_attr[k], '');
  7704. if (d) { d.removeClass(t[old_type].li_attr[k]); }
  7705. }
  7706. else if (m[obj.id].li_attr[k] === t[old_type].li_attr[k]) {
  7707. m[obj.id].li_attr[k] = null;
  7708. if (d) { d.removeAttr(k); }
  7709. }
  7710. }
  7711. }
  7712. }
  7713. if(t[old_type].a_attr !== undefined && typeof t[old_type].a_attr === 'object') {
  7714. for (k in t[old_type].a_attr) {
  7715. if (t[old_type].a_attr.hasOwnProperty(k)) {
  7716. if (k === 'id') {
  7717. continue;
  7718. }
  7719. else if (k === 'class') {
  7720. m[obj.id].a_attr['class'] = (m[obj.id].a_attr['class'] || '').replace(t[old_type].a_attr[k], '');
  7721. if (a) { a.removeClass(t[old_type].a_attr[k]); }
  7722. }
  7723. else if (m[obj.id].a_attr[k] === t[old_type].a_attr[k]) {
  7724. if (k === 'href') {
  7725. m[obj.id].a_attr[k] = '#';
  7726. if (a) { a.attr('href', '#'); }
  7727. }
  7728. else {
  7729. delete m[obj.id].a_attr[k];
  7730. if (a) { a.removeAttr(k); }
  7731. }
  7732. }
  7733. }
  7734. }
  7735. }
  7736. // add new props
  7737. if(t[type].li_attr !== undefined && typeof t[type].li_attr === 'object') {
  7738. for (k in t[type].li_attr) {
  7739. if (t[type].li_attr.hasOwnProperty(k)) {
  7740. if (k === 'id') {
  7741. continue;
  7742. }
  7743. else if (m[obj.id].li_attr[k] === undefined) {
  7744. m[obj.id].li_attr[k] = t[type].li_attr[k];
  7745. if (d) {
  7746. if (k === 'class') {
  7747. d.addClass(t[type].li_attr[k]);
  7748. }
  7749. else {
  7750. d.attr(k, t[type].li_attr[k]);
  7751. }
  7752. }
  7753. }
  7754. else if (k === 'class') {
  7755. m[obj.id].li_attr['class'] = t[type].li_attr[k] + ' ' + m[obj.id].li_attr['class'];
  7756. if (d) { d.addClass(t[type].li_attr[k]); }
  7757. }
  7758. }
  7759. }
  7760. }
  7761. if(t[type].a_attr !== undefined && typeof t[type].a_attr === 'object') {
  7762. for (k in t[type].a_attr) {
  7763. if (t[type].a_attr.hasOwnProperty(k)) {
  7764. if (k === 'id') {
  7765. continue;
  7766. }
  7767. else if (m[obj.id].a_attr[k] === undefined) {
  7768. m[obj.id].a_attr[k] = t[type].a_attr[k];
  7769. if (a) {
  7770. if (k === 'class') {
  7771. a.addClass(t[type].a_attr[k]);
  7772. }
  7773. else {
  7774. a.attr(k, t[type].a_attr[k]);
  7775. }
  7776. }
  7777. }
  7778. else if (k === 'href' && m[obj.id].a_attr[k] === '#') {
  7779. m[obj.id].a_attr['href'] = t[type].a_attr['href'];
  7780. if (a) { a.attr('href', t[type].a_attr['href']); }
  7781. }
  7782. else if (k === 'class') {
  7783. m[obj.id].a_attr['class'] = t[type].a_attr['class'] + ' ' + m[obj.id].a_attr['class'];
  7784. if (a) { a.addClass(t[type].a_attr[k]); }
  7785. }
  7786. }
  7787. }
  7788. }
  7789. return true;
  7790. };
  7791. };
  7792. // include the types plugin by default
  7793. // $.jstree.defaults.plugins.push("types");
  7794. /**
  7795. * ### Unique plugin
  7796. *
  7797. * Enforces that no nodes with the same name can coexist as siblings.
  7798. */
  7799. /**
  7800. * stores all defaults for the unique plugin
  7801. * @name $.jstree.defaults.unique
  7802. * @plugin unique
  7803. */
  7804. $.jstree.defaults.unique = {
  7805. /**
  7806. * Indicates if the comparison should be case sensitive. Default is `false`.
  7807. * @name $.jstree.defaults.unique.case_sensitive
  7808. * @plugin unique
  7809. */
  7810. case_sensitive : false,
  7811. /**
  7812. * A callback executed in the instance's scope when a new node is created and the name is already taken, the two arguments are the conflicting name and the counter. The default will produce results like `New node (2)`.
  7813. * @name $.jstree.defaults.unique.duplicate
  7814. * @plugin unique
  7815. */
  7816. duplicate : function (name, counter) {
  7817. return name + ' (' + counter + ')';
  7818. }
  7819. };
  7820. $.jstree.plugins.unique = function (options, parent) {
  7821. this.check = function (chk, obj, par, pos, more) {
  7822. if(parent.check.call(this, chk, obj, par, pos, more) === false) { return false; }
  7823. obj = obj && obj.id ? obj : this.get_node(obj);
  7824. par = par && par.id ? par : this.get_node(par);
  7825. if(!par || !par.children) { return true; }
  7826. var n = chk === "rename_node" ? pos : obj.text,
  7827. c = [],
  7828. s = this.settings.unique.case_sensitive,
  7829. m = this._model.data, i, j;
  7830. for(i = 0, j = par.children.length; i < j; i++) {
  7831. c.push(s ? m[par.children[i]].text : m[par.children[i]].text.toLowerCase());
  7832. }
  7833. if(!s) { n = n.toLowerCase(); }
  7834. switch(chk) {
  7835. case "delete_node":
  7836. return true;
  7837. case "rename_node":
  7838. i = ($.inArray(n, c) === -1 || (obj.text && obj.text[ s ? 'toString' : 'toLowerCase']() === n));
  7839. if(!i) {
  7840. this._data.core.last_error = { 'error' : 'check', 'plugin' : 'unique', 'id' : 'unique_01', 'reason' : 'Child with name ' + n + ' already exists. Preventing: ' + chk, 'data' : JSON.stringify({ 'chk' : chk, 'pos' : pos, 'obj' : obj && obj.id ? obj.id : false, 'par' : par && par.id ? par.id : false }) };
  7841. }
  7842. return i;
  7843. case "create_node":
  7844. i = ($.inArray(n, c) === -1);
  7845. if(!i) {
  7846. this._data.core.last_error = { 'error' : 'check', 'plugin' : 'unique', 'id' : 'unique_04', 'reason' : 'Child with name ' + n + ' already exists. Preventing: ' + chk, 'data' : JSON.stringify({ 'chk' : chk, 'pos' : pos, 'obj' : obj && obj.id ? obj.id : false, 'par' : par && par.id ? par.id : false }) };
  7847. }
  7848. return i;
  7849. case "copy_node":
  7850. i = ($.inArray(n, c) === -1);
  7851. if(!i) {
  7852. this._data.core.last_error = { 'error' : 'check', 'plugin' : 'unique', 'id' : 'unique_02', 'reason' : 'Child with name ' + n + ' already exists. Preventing: ' + chk, 'data' : JSON.stringify({ 'chk' : chk, 'pos' : pos, 'obj' : obj && obj.id ? obj.id : false, 'par' : par && par.id ? par.id : false }) };
  7853. }
  7854. return i;
  7855. case "move_node":
  7856. i = ( (obj.parent === par.id && (!more || !more.is_multi)) || $.inArray(n, c) === -1);
  7857. if(!i) {
  7858. this._data.core.last_error = { 'error' : 'check', 'plugin' : 'unique', 'id' : 'unique_03', 'reason' : 'Child with name ' + n + ' already exists. Preventing: ' + chk, 'data' : JSON.stringify({ 'chk' : chk, 'pos' : pos, 'obj' : obj && obj.id ? obj.id : false, 'par' : par && par.id ? par.id : false }) };
  7859. }
  7860. return i;
  7861. }
  7862. return true;
  7863. };
  7864. this.create_node = function (par, node, pos, callback, is_loaded) {
  7865. if(!node || node.text === undefined) {
  7866. if(par === null) {
  7867. par = $.jstree.root;
  7868. }
  7869. par = this.get_node(par);
  7870. if(!par) {
  7871. return parent.create_node.call(this, par, node, pos, callback, is_loaded);
  7872. }
  7873. pos = pos === undefined ? "last" : pos;
  7874. if(!pos.toString().match(/^(before|after)$/) && !is_loaded && !this.is_loaded(par)) {
  7875. return parent.create_node.call(this, par, node, pos, callback, is_loaded);
  7876. }
  7877. if(!node) { node = {}; }
  7878. var tmp, n, dpc, i, j, m = this._model.data, s = this.settings.unique.case_sensitive, cb = this.settings.unique.duplicate;
  7879. n = tmp = this.get_string('New node');
  7880. dpc = [];
  7881. for(i = 0, j = par.children.length; i < j; i++) {
  7882. dpc.push(s ? m[par.children[i]].text : m[par.children[i]].text.toLowerCase());
  7883. }
  7884. i = 1;
  7885. while($.inArray(s ? n : n.toLowerCase(), dpc) !== -1) {
  7886. n = cb.call(this, tmp, (++i)).toString();
  7887. }
  7888. node.text = n;
  7889. }
  7890. return parent.create_node.call(this, par, node, pos, callback, is_loaded);
  7891. };
  7892. };
  7893. // include the unique plugin by default
  7894. // $.jstree.defaults.plugins.push("unique");
  7895. /**
  7896. * ### Wholerow plugin
  7897. *
  7898. * Makes each node appear block level. Making selection easier. May cause slow down for large trees in old browsers.
  7899. */
  7900. var div = document.createElement('DIV');
  7901. div.setAttribute('unselectable','on');
  7902. div.setAttribute('role','presentation');
  7903. div.className = 'jstree-wholerow';
  7904. div.innerHTML = '&#160;';
  7905. $.jstree.plugins.wholerow = function (options, parent) {
  7906. this.bind = function () {
  7907. parent.bind.call(this);
  7908. this.element
  7909. .on('ready.jstree set_state.jstree', $.proxy(function () {
  7910. this.hide_dots();
  7911. }, this))
  7912. .on("init.jstree loading.jstree ready.jstree", $.proxy(function () {
  7913. //div.style.height = this._data.core.li_height + 'px';
  7914. this.get_container_ul().addClass('jstree-wholerow-ul');
  7915. }, this))
  7916. .on("deselect_all.jstree", $.proxy(function (e, data) {
  7917. this.element.find('.jstree-wholerow-clicked').removeClass('jstree-wholerow-clicked');
  7918. }, this))
  7919. .on("changed.jstree", $.proxy(function (e, data) {
  7920. this.element.find('.jstree-wholerow-clicked').removeClass('jstree-wholerow-clicked');
  7921. var tmp = false, i, j;
  7922. for(i = 0, j = data.selected.length; i < j; i++) {
  7923. tmp = this.get_node(data.selected[i], true);
  7924. if(tmp && tmp.length) {
  7925. tmp.children('.jstree-wholerow').addClass('jstree-wholerow-clicked');
  7926. }
  7927. }
  7928. }, this))
  7929. .on("open_node.jstree", $.proxy(function (e, data) {
  7930. this.get_node(data.node, true).find('.jstree-clicked').parent().children('.jstree-wholerow').addClass('jstree-wholerow-clicked');
  7931. }, this))
  7932. .on("hover_node.jstree dehover_node.jstree", $.proxy(function (e, data) {
  7933. if(e.type === "hover_node" && this.is_disabled(data.node)) { return; }
  7934. this.get_node(data.node, true).children('.jstree-wholerow')[e.type === "hover_node"?"addClass":"removeClass"]('jstree-wholerow-hovered');
  7935. }, this))
  7936. .on("contextmenu.jstree", ".jstree-wholerow", $.proxy(function (e) {
  7937. if (this._data.contextmenu) {
  7938. e.preventDefault();
  7939. var tmp = $.Event('contextmenu', { metaKey : e.metaKey, ctrlKey : e.ctrlKey, altKey : e.altKey, shiftKey : e.shiftKey, pageX : e.pageX, pageY : e.pageY });
  7940. $(e.currentTarget).closest(".jstree-node").children(".jstree-anchor").first().trigger(tmp);
  7941. }
  7942. }, this))
  7943. /*!
  7944. .on("mousedown.jstree touchstart.jstree", ".jstree-wholerow", function (e) {
  7945. if(e.target === e.currentTarget) {
  7946. var a = $(e.currentTarget).closest(".jstree-node").children(".jstree-anchor");
  7947. e.target = a[0];
  7948. a.trigger(e);
  7949. }
  7950. })
  7951. */
  7952. .on("click.jstree", ".jstree-wholerow", function (e) {
  7953. e.stopImmediatePropagation();
  7954. var tmp = $.Event('click', { metaKey : e.metaKey, ctrlKey : e.ctrlKey, altKey : e.altKey, shiftKey : e.shiftKey });
  7955. $(e.currentTarget).closest(".jstree-node").children(".jstree-anchor").first().trigger(tmp).focus();
  7956. })
  7957. .on("click.jstree", ".jstree-leaf > .jstree-ocl", $.proxy(function (e) {
  7958. e.stopImmediatePropagation();
  7959. var tmp = $.Event('click', { metaKey : e.metaKey, ctrlKey : e.ctrlKey, altKey : e.altKey, shiftKey : e.shiftKey });
  7960. $(e.currentTarget).closest(".jstree-node").children(".jstree-anchor").first().trigger(tmp).focus();
  7961. }, this))
  7962. .on("mouseover.jstree", ".jstree-wholerow, .jstree-icon", $.proxy(function (e) {
  7963. e.stopImmediatePropagation();
  7964. if(!this.is_disabled(e.currentTarget)) {
  7965. this.hover_node(e.currentTarget);
  7966. }
  7967. return false;
  7968. }, this))
  7969. .on("mouseleave.jstree", ".jstree-node", $.proxy(function (e) {
  7970. this.dehover_node(e.currentTarget);
  7971. }, this));
  7972. };
  7973. this.teardown = function () {
  7974. if(this.settings.wholerow) {
  7975. this.element.find(".jstree-wholerow").remove();
  7976. }
  7977. parent.teardown.call(this);
  7978. };
  7979. this.redraw_node = function(obj, deep, callback, force_render) {
  7980. obj = parent.redraw_node.apply(this, arguments);
  7981. if(obj) {
  7982. var tmp = div.cloneNode(true);
  7983. //tmp.style.height = this._data.core.li_height + 'px';
  7984. if($.inArray(obj.id, this._data.core.selected) !== -1) { tmp.className += ' jstree-wholerow-clicked'; }
  7985. if(this._data.core.focused && this._data.core.focused === obj.id) { tmp.className += ' jstree-wholerow-hovered'; }
  7986. obj.insertBefore(tmp, obj.childNodes[0]);
  7987. }
  7988. return obj;
  7989. };
  7990. };
  7991. // include the wholerow plugin by default
  7992. // $.jstree.defaults.plugins.push("wholerow");
  7993. if(document.registerElement && Object && Object.create) {
  7994. var proto = Object.create(HTMLElement.prototype);
  7995. proto.createdCallback = function () {
  7996. var c = { core : {}, plugins : [] }, i;
  7997. for(i in $.jstree.plugins) {
  7998. if($.jstree.plugins.hasOwnProperty(i) && this.attributes[i]) {
  7999. c.plugins.push(i);
  8000. if(this.getAttribute(i) && JSON.parse(this.getAttribute(i))) {
  8001. c[i] = JSON.parse(this.getAttribute(i));
  8002. }
  8003. }
  8004. }
  8005. for(i in $.jstree.defaults.core) {
  8006. if($.jstree.defaults.core.hasOwnProperty(i) && this.attributes[i]) {
  8007. c.core[i] = JSON.parse(this.getAttribute(i)) || this.getAttribute(i);
  8008. }
  8009. }
  8010. $(this).jstree(c);
  8011. };
  8012. // proto.attributeChangedCallback = function (name, previous, value) { };
  8013. try {
  8014. document.registerElement("vakata-jstree", { prototype: proto });
  8015. } catch(ignore) { }
  8016. }
  8017. }));