jstree.js 237 KB

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