jstree.js 271 KB

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