jstree.js 303 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600860186028603860486058606860786088609861086118612861386148615861686178618861986208621862286238624862586268627862886298630863186328633863486358636863786388639864086418642864386448645864686478648864986508651865286538654865586568657865886598660866186628663866486658666866786688669867086718672867386748675867686778678867986808681868286838684868586868687868886898690869186928693869486958696869786988699870087018702870387048705870687078708870987108711871287138714871587168717871887198720872187228723872487258726872787288729873087318732873387348735873687378738
  1. /*globals jQuery, define, module, exports, require, window, document, postMessage */
  2. (function (factory) {
  3. "use strict";
  4. if (typeof define === 'function' && define.amd) {
  5. define(['jquery'], factory);
  6. }
  7. else if(typeof module !== 'undefined' && module.exports) {
  8. module.exports = factory(require('jquery'));
  9. }
  10. else {
  11. factory(jQuery);
  12. }
  13. }(function ($, undefined) {
  14. "use strict";
  15. /*!
  16. * jsTree 3.3.15
  17. * http://jstree.com/
  18. *
  19. * Copyright (c) 2014 Ivan Bozhanov (http://vakata.com)
  20. *
  21. * Licensed same as jquery - under the terms of the MIT License
  22. * http://www.opensource.org/licenses/mit-license.php
  23. */
  24. /*!
  25. * if using jslint please allow for the jQuery global and use following options:
  26. * jslint: loopfunc: true, browser: true, ass: true, bitwise: true, continue: true, nomen: true, plusplus: true, regexp: true, unparam: true, todo: true, white: true
  27. */
  28. /*jshint -W083 */
  29. // prevent another load? maybe there is a better way?
  30. if($.jstree) {
  31. return;
  32. }
  33. /**
  34. * ### jsTree core functionality
  35. */
  36. // internal variables
  37. var instance_counter = 0,
  38. ccp_node = false,
  39. ccp_mode = false,
  40. ccp_inst = false,
  41. themes_loaded = [],
  42. src = $('script:last').attr('src'),
  43. document = window.document; // local variable is always faster to access then a global
  44. var setImmediate = window.setImmediate;
  45. var Promise = window.Promise;
  46. if (!setImmediate && Promise) {
  47. // Good enough approximation of setImmediate
  48. setImmediate = function (cb, arg) {
  49. Promise.resolve(arg).then(cb);
  50. };
  51. }
  52. /**
  53. * holds all jstree related functions and variables, including the actual class and methods to create, access and manipulate instances.
  54. * @name $.jstree
  55. */
  56. $.jstree = {
  57. /**
  58. * specifies the jstree version in use
  59. * @name $.jstree.version
  60. */
  61. version : '3.3.15',
  62. /**
  63. * holds all the default options used when creating new instances
  64. * @name $.jstree.defaults
  65. */
  66. defaults : {
  67. /**
  68. * configure which plugins will be active on an instance. Should be an array of strings, where each element is a plugin name. The default is `[]`
  69. * @name $.jstree.defaults.plugins
  70. */
  71. plugins : []
  72. },
  73. /**
  74. * stores all loaded jstree plugins (used internally)
  75. * @name $.jstree.plugins
  76. */
  77. plugins : {},
  78. path : src && src.indexOf('/') !== -1 ? src.replace(/\/[^\/]+$/,'') : '',
  79. idregex : /[\\:&!^|()\[\]<>@*'+~#";.,=\- \/${}%?`]/g,
  80. root : '#'
  81. };
  82. /**
  83. * creates a jstree instance
  84. * @name $.jstree.create(el [, options])
  85. * @param {DOMElement|jQuery|String} el the element to create the instance on, can be jQuery extended or a selector
  86. * @param {Object} options options for this instance (extends `$.jstree.defaults`)
  87. * @return {jsTree} the new instance
  88. */
  89. $.jstree.create = function (el, options) {
  90. var tmp = new $.jstree.core(++instance_counter),
  91. opt = options;
  92. options = $.extend(true, {}, $.jstree.defaults, options);
  93. if(opt && opt.plugins) {
  94. options.plugins = opt.plugins;
  95. }
  96. $.each(options.plugins, function (i, k) {
  97. if(i !== 'core') {
  98. tmp = tmp.plugin(k, options[k]);
  99. }
  100. });
  101. $(el).data('jstree', tmp);
  102. tmp.init(el, options);
  103. return tmp;
  104. };
  105. /**
  106. * remove all traces of jstree from the DOM and destroy all instances
  107. * @name $.jstree.destroy()
  108. */
  109. $.jstree.destroy = function () {
  110. $('.jstree:jstree').jstree('destroy');
  111. $(document).off('.jstree');
  112. };
  113. /**
  114. * the jstree class constructor, used only internally
  115. * @private
  116. * @name $.jstree.core(id)
  117. * @param {Number} id this instance's index
  118. */
  119. $.jstree.core = function (id) {
  120. this._id = id;
  121. this._cnt = 0;
  122. this._wrk = null;
  123. this._data = {
  124. core : {
  125. themes : {
  126. name : false,
  127. dots : false,
  128. icons : false,
  129. ellipsis : false
  130. },
  131. selected : [],
  132. last_error : {},
  133. working : false,
  134. worker_queue : [],
  135. focused : null
  136. }
  137. };
  138. };
  139. /**
  140. * get a reference to an existing instance
  141. *
  142. * __Examples__
  143. *
  144. * // provided a container with an ID of "tree", and a nested node with an ID of "branch"
  145. * // all of there will return the same instance
  146. * $.jstree.reference('tree');
  147. * $.jstree.reference('#tree');
  148. * $.jstree.reference($('#tree'));
  149. * $.jstree.reference(document.getElementByID('tree'));
  150. * $.jstree.reference('branch');
  151. * $.jstree.reference('#branch');
  152. * $.jstree.reference($('#branch'));
  153. * $.jstree.reference(document.getElementByID('branch'));
  154. *
  155. * @name $.jstree.reference(needle)
  156. * @param {DOMElement|jQuery|String} needle
  157. * @return {jsTree|null} the instance or `null` if not found
  158. */
  159. $.jstree.reference = function (needle) {
  160. var tmp = null,
  161. obj = null;
  162. if(needle && needle.id && (!needle.tagName || !needle.nodeType)) { needle = needle.id; }
  163. if(!obj || !obj.length) {
  164. try { obj = $(needle); } catch (ignore) { }
  165. }
  166. if(!obj || !obj.length) {
  167. try { obj = $('#' + needle.replace($.jstree.idregex,'\\$&')); } catch (ignore) { }
  168. }
  169. if(obj && obj.length && (obj = obj.closest('.jstree')).length && (obj = obj.data('jstree'))) {
  170. tmp = obj;
  171. }
  172. else {
  173. $('.jstree').each(function () {
  174. var inst = $(this).data('jstree');
  175. if(inst && inst._model.data[needle]) {
  176. tmp = inst;
  177. return false;
  178. }
  179. });
  180. }
  181. return tmp;
  182. };
  183. /**
  184. * Create an instance, get an instance or invoke a command on a instance.
  185. *
  186. * If there is no instance associated with the current node a new one is created and `arg` is used to extend `$.jstree.defaults` for this new instance. There would be no return value (chaining is not broken).
  187. *
  188. * If there is an existing instance and `arg` is a string the command specified by `arg` is executed on the instance, with any additional arguments passed to the function. If the function returns a value it will be returned (chaining could break depending on function).
  189. *
  190. * If there is an existing instance and `arg` is not a string the instance itself is returned (similar to `$.jstree.reference`).
  191. *
  192. * In any other case - nothing is returned and chaining is not broken.
  193. *
  194. * __Examples__
  195. *
  196. * $('#tree1').jstree(); // creates an instance
  197. * $('#tree2').jstree({ plugins : [] }); // create an instance with some options
  198. * $('#tree1').jstree('open_node', '#branch_1'); // call a method on an existing instance, passing additional arguments
  199. * $('#tree2').jstree(); // get an existing instance (or create an instance)
  200. * $('#tree2').jstree(true); // get an existing instance (will not create new instance)
  201. * $('#branch_1').jstree().select_node('#branch_1'); // get an instance (using a nested element and call a method)
  202. *
  203. * @name $().jstree([arg])
  204. * @param {String|Object} arg
  205. * @return {Mixed}
  206. */
  207. $.fn.jstree = function (arg) {
  208. // check for string argument
  209. var is_method = (typeof arg === 'string'),
  210. args = Array.prototype.slice.call(arguments, 1),
  211. result = null;
  212. if(arg === true && !this.length) { return false; }
  213. this.each(function () {
  214. // get the instance (if there is one) and method (if it exists)
  215. var instance = $.jstree.reference(this),
  216. method = is_method && instance ? instance[arg] : null;
  217. // if calling a method, and method is available - execute on the instance
  218. result = is_method && method ?
  219. method.apply(instance, args) :
  220. null;
  221. // if there is no instance and no method is being called - create one
  222. if(!instance && !is_method && (arg === undefined || $.isPlainObject(arg))) {
  223. $.jstree.create(this, arg);
  224. }
  225. // if there is an instance and no method is called - return the instance
  226. if( (instance && !is_method) || arg === true ) {
  227. result = instance || false;
  228. }
  229. // if there was a method call which returned a result - break and return the value
  230. if(result !== null && result !== undefined) {
  231. return false;
  232. }
  233. });
  234. // if there was a method call with a valid return value - return that, otherwise continue the chain
  235. return result !== null && result !== undefined ?
  236. result : this;
  237. };
  238. /**
  239. * used to find elements containing an instance
  240. *
  241. * __Examples__
  242. *
  243. * $('div:jstree').each(function () {
  244. * $(this).jstree('destroy');
  245. * });
  246. *
  247. * @name $(':jstree')
  248. * @return {jQuery}
  249. */
  250. $.expr.pseudos.jstree = $.expr.createPseudo(function(search) {
  251. return function(a) {
  252. return $(a).hasClass('jstree') &&
  253. $(a).data('jstree') !== undefined;
  254. };
  255. });
  256. /**
  257. * stores all defaults for the core
  258. * @name $.jstree.defaults.core
  259. */
  260. $.jstree.defaults.core = {
  261. /**
  262. * data configuration
  263. *
  264. * If left as `false` the HTML inside the jstree container element is used to populate the tree (that should be an unordered list with list items).
  265. *
  266. * You can also pass in a HTML string or a JSON array here.
  267. *
  268. * It is possible to pass in a standard jQuery-like AJAX config and jstree will automatically determine if the response is JSON or HTML and use that to populate the tree.
  269. * In addition to the standard jQuery ajax options here you can supply functions for `data` and `url`, the functions will be run in the current instance's scope and a param will be passed indicating which node is being loaded, the return value of those functions will be used.
  270. *
  271. * The last option is to specify a function, that function will receive the node being loaded as argument and a second param which is a function which should be called with the result.
  272. *
  273. * __Examples__
  274. *
  275. * // AJAX
  276. * $('#tree').jstree({
  277. * 'core' : {
  278. * 'data' : {
  279. * 'url' : '/get/children/',
  280. * 'data' : function (node) {
  281. * return { 'id' : node.id };
  282. * }
  283. * }
  284. * });
  285. *
  286. * // direct data
  287. * $('#tree').jstree({
  288. * 'core' : {
  289. * 'data' : [
  290. * 'Simple root node',
  291. * {
  292. * 'id' : 'node_2',
  293. * 'text' : 'Root node with options',
  294. * 'state' : { 'opened' : true, 'selected' : true },
  295. * 'children' : [ { 'text' : 'Child 1' }, 'Child 2']
  296. * }
  297. * ]
  298. * }
  299. * });
  300. *
  301. * // function
  302. * $('#tree').jstree({
  303. * 'core' : {
  304. * 'data' : function (obj, callback) {
  305. * callback.call(this, ['Root 1', 'Root 2']);
  306. * }
  307. * });
  308. *
  309. * @name $.jstree.defaults.core.data
  310. */
  311. data : false,
  312. /**
  313. * configure the various strings used throughout the tree
  314. *
  315. * You can use an object where the key is the string you need to replace and the value is your replacement.
  316. * Another option is to specify a function which will be called with an argument of the needed string and should return the replacement.
  317. * If left as `false` no replacement is made.
  318. *
  319. * __Examples__
  320. *
  321. * $('#tree').jstree({
  322. * 'core' : {
  323. * 'strings' : {
  324. * 'Loading ...' : 'Please wait ...'
  325. * }
  326. * }
  327. * });
  328. *
  329. * @name $.jstree.defaults.core.strings
  330. */
  331. strings : false,
  332. /**
  333. * determines what happens when a user tries to modify the structure of the tree
  334. * If left as `false` all operations like create, rename, delete, move or copy are prevented.
  335. * You can set this to `true` to allow all interactions or use a function to have better control.
  336. *
  337. * __Examples__
  338. *
  339. * $('#tree').jstree({
  340. * 'core' : {
  341. * 'check_callback' : function (operation, node, node_parent, node_position, more) {
  342. * // operation can be 'create_node', 'rename_node', 'delete_node', 'move_node', 'copy_node' or 'edit'
  343. * // in case of 'rename_node' node_position is filled with the new node name
  344. * return operation === 'rename_node' ? true : false;
  345. * }
  346. * }
  347. * });
  348. *
  349. * @name $.jstree.defaults.core.check_callback
  350. */
  351. check_callback : false,
  352. /**
  353. * a callback called with a single object parameter in the instance's scope when something goes wrong (operation prevented, ajax failed, etc)
  354. * @name $.jstree.defaults.core.error
  355. */
  356. error : $.noop,
  357. /**
  358. * the open / close animation duration in milliseconds - set this to `false` to disable the animation (default is `200`)
  359. * @name $.jstree.defaults.core.animation
  360. */
  361. animation : 200,
  362. /**
  363. * a boolean indicating if multiple nodes can be selected
  364. * @name $.jstree.defaults.core.multiple
  365. */
  366. multiple : true,
  367. /**
  368. * theme configuration object
  369. * @name $.jstree.defaults.core.themes
  370. */
  371. themes : {
  372. /**
  373. * the name of the theme to use (if left as `false` the default theme is used)
  374. * @name $.jstree.defaults.core.themes.name
  375. */
  376. name : false,
  377. /**
  378. * the URL of the theme's CSS file, leave this as `false` if you have manually included the theme CSS (recommended). You can set this to `true` too which will try to autoload the theme.
  379. * @name $.jstree.defaults.core.themes.url
  380. */
  381. url : false,
  382. /**
  383. * the location of all jstree themes - only used if `url` is set to `true`
  384. * @name $.jstree.defaults.core.themes.dir
  385. */
  386. dir : false,
  387. /**
  388. * a boolean indicating if connecting dots are shown
  389. * @name $.jstree.defaults.core.themes.dots
  390. */
  391. dots : true,
  392. /**
  393. * a boolean indicating if node icons are shown
  394. * @name $.jstree.defaults.core.themes.icons
  395. */
  396. icons : true,
  397. /**
  398. * a boolean indicating if node ellipsis should be shown - this only works with a fixed with on the container
  399. * @name $.jstree.defaults.core.themes.ellipsis
  400. */
  401. ellipsis : false,
  402. /**
  403. * a boolean indicating if the tree background is striped
  404. * @name $.jstree.defaults.core.themes.stripes
  405. */
  406. stripes : false,
  407. /**
  408. * a string (or boolean `false`) specifying the theme variant to use (if the theme supports variants)
  409. * @name $.jstree.defaults.core.themes.variant
  410. */
  411. variant : false,
  412. /**
  413. * a boolean specifying if a reponsive version of the theme should kick in on smaller screens (if the theme supports it). Defaults to `false`.
  414. * @name $.jstree.defaults.core.themes.responsive
  415. */
  416. responsive : false
  417. },
  418. /**
  419. * if left as `true` all parents of all selected nodes will be opened once the tree loads (so that all selected nodes are visible to the user)
  420. * @name $.jstree.defaults.core.expand_selected_onload
  421. */
  422. expand_selected_onload : true,
  423. /**
  424. * if left as `true` web workers will be used to parse incoming JSON data where possible, so that the UI will not be blocked by large requests. Workers are however about 30% slower. Defaults to `true`
  425. * @name $.jstree.defaults.core.worker
  426. */
  427. worker : true,
  428. /**
  429. * Force node text to plain text (and escape HTML). Defaults to `false`
  430. * @name $.jstree.defaults.core.force_text
  431. */
  432. force_text : false,
  433. /**
  434. * Should the node be toggled if the text is double clicked. Defaults to `true`
  435. * @name $.jstree.defaults.core.dblclick_toggle
  436. */
  437. dblclick_toggle : true,
  438. /**
  439. * Should the loaded nodes be part of the state. Defaults to `false`
  440. * @name $.jstree.defaults.core.loaded_state
  441. */
  442. loaded_state : false,
  443. /**
  444. * Should the last active node be focused when the tree container is blurred and the focused again. This helps working with screen readers. Defaults to `true`
  445. * @name $.jstree.defaults.core.restore_focus
  446. */
  447. restore_focus : true,
  448. /**
  449. * Force to compute and set "aria-setsize" and "aria-posinset" explicitly for each treeitem.
  450. * Some browsers may compute incorrect elements position and produce wrong announcements for screen readers. Defaults to `false`
  451. * @name $.jstree.defaults.core.compute_elements_positions
  452. */
  453. compute_elements_positions : false,
  454. /**
  455. * Default keyboard shortcuts (an object where each key is the button name or combo - like 'enter', 'ctrl-space', 'p', etc and the value is the function to execute in the instance's scope)
  456. * @name $.jstree.defaults.core.keyboard
  457. */
  458. keyboard : {
  459. 'ctrl-space': function (e) {
  460. // aria defines space only with Ctrl
  461. e.type = "click";
  462. $(e.currentTarget).trigger(e);
  463. },
  464. 'enter': function (e) {
  465. // enter
  466. e.type = "click";
  467. $(e.currentTarget).trigger(e);
  468. },
  469. 'left': function (e) {
  470. // left
  471. e.preventDefault();
  472. if(this.is_open(e.currentTarget)) {
  473. this.close_node(e.currentTarget);
  474. }
  475. else {
  476. var o = this.get_parent(e.currentTarget);
  477. if(o && o.id !== $.jstree.root) { this.get_node(o, true).children('.jstree-anchor').trigger('focus'); }
  478. }
  479. },
  480. 'up': function (e) {
  481. // up
  482. e.preventDefault();
  483. var o = this.get_prev_dom(e.currentTarget);
  484. if(o && o.length) { o.children('.jstree-anchor').trigger('focus'); }
  485. },
  486. 'right': function (e) {
  487. // right
  488. e.preventDefault();
  489. if(this.is_closed(e.currentTarget)) {
  490. this.open_node(e.currentTarget, function (o) { this.get_node(o, true).children('.jstree-anchor').trigger('focus'); });
  491. }
  492. else if (this.is_open(e.currentTarget)) {
  493. var o = this.get_node(e.currentTarget, true).children('.jstree-children')[0];
  494. if(o) { $(this._firstChild(o)).children('.jstree-anchor').trigger('focus'); }
  495. }
  496. },
  497. 'down': function (e) {
  498. // down
  499. e.preventDefault();
  500. var o = this.get_next_dom(e.currentTarget);
  501. if(o && o.length) { o.children('.jstree-anchor').trigger('focus'); }
  502. },
  503. '*': function (e) {
  504. // aria defines * on numpad as open_all - not very common
  505. this.open_all();
  506. },
  507. 'home': function (e) {
  508. // home
  509. e.preventDefault();
  510. var o = this._firstChild(this.get_container_ul()[0]);
  511. if(o) { $(o).children('.jstree-anchor').filter(':visible').trigger('focus'); }
  512. },
  513. 'end': function (e) {
  514. // end
  515. e.preventDefault();
  516. this.element.find('.jstree-anchor').filter(':visible').last().trigger('focus');
  517. },
  518. 'f2': function (e) {
  519. // f2 - safe to include - if check_callback is false it will fail
  520. e.preventDefault();
  521. this.edit(e.currentTarget);
  522. }
  523. }
  524. };
  525. $.jstree.core.prototype = {
  526. /**
  527. * used to decorate an instance with a plugin. Used internally.
  528. * @private
  529. * @name plugin(deco [, opts])
  530. * @param {String} deco the plugin to decorate with
  531. * @param {Object} opts options for the plugin
  532. * @return {jsTree}
  533. */
  534. plugin : function (deco, opts) {
  535. var Child = $.jstree.plugins[deco];
  536. if(Child) {
  537. this._data[deco] = {};
  538. Child.prototype = this;
  539. return new Child(opts, this);
  540. }
  541. return this;
  542. },
  543. /**
  544. * initialize the instance. Used internally.
  545. * @private
  546. * @name init(el, optons)
  547. * @param {DOMElement|jQuery|String} el the element we are transforming
  548. * @param {Object} options options for this instance
  549. * @trigger init.jstree, loading.jstree, loaded.jstree, ready.jstree, changed.jstree
  550. */
  551. init : function (el, options) {
  552. this._model = {
  553. data : {},
  554. changed : [],
  555. force_full_redraw : false,
  556. redraw_timeout : false,
  557. default_state : {
  558. loaded : true,
  559. opened : false,
  560. selected : false,
  561. disabled : false
  562. }
  563. };
  564. this._model.data[$.jstree.root] = {
  565. id : $.jstree.root,
  566. parent : null,
  567. parents : [],
  568. children : [],
  569. children_d : [],
  570. state : { loaded : false }
  571. };
  572. this.element = $(el).addClass('jstree jstree-' + this._id);
  573. this.settings = options;
  574. this._data.core.ready = false;
  575. this._data.core.loaded = false;
  576. this._data.core.rtl = (this.element.css("direction") === "rtl");
  577. this.element[this._data.core.rtl ? 'addClass' : 'removeClass']("jstree-rtl");
  578. this.element.attr('role','tree');
  579. if(this.settings.core.multiple) {
  580. this.element.attr('aria-multiselectable', true);
  581. }
  582. if(!this.element.attr('tabindex')) {
  583. this.element.attr('tabindex','0');
  584. }
  585. this.bind();
  586. /**
  587. * triggered after all events are bound
  588. * @event
  589. * @name init.jstree
  590. */
  591. this.trigger("init");
  592. this._data.core.original_container_html = this.element.find(" > ul > li").clone(true);
  593. this._data.core.original_container_html
  594. .find("li").addBack()
  595. .contents().filter(function() {
  596. return this.nodeType === 3 && (!this.nodeValue || /^\s+$/.test(this.nodeValue));
  597. })
  598. .remove();
  599. this.element.html("<"+"ul class='jstree-container-ul jstree-children' role='group'><"+"li id='j"+this._id+"_loading' class='jstree-initial-node jstree-loading jstree-leaf jstree-last' role='none'><i class='jstree-icon jstree-ocl'></i><"+"a class='jstree-anchor' role='treeitem' href='#'><i class='jstree-icon jstree-themeicon-hidden'></i>" + this.get_string("Loading ...") + "</a></li></ul>");
  600. this.element.attr('aria-activedescendant','j' + this._id + '_loading');
  601. this._data.core.li_height = this.get_container_ul().children("li").first().outerHeight() || 24;
  602. this._data.core.node = this._create_prototype_node();
  603. /**
  604. * triggered after the loading text is shown and before loading starts
  605. * @event
  606. * @name loading.jstree
  607. */
  608. this.trigger("loading");
  609. this.load_node($.jstree.root);
  610. },
  611. /**
  612. * destroy an instance
  613. * @name destroy()
  614. * @param {Boolean} keep_html if not set to `true` the container will be emptied, otherwise the current DOM elements will be kept intact
  615. */
  616. destroy : function (keep_html) {
  617. /**
  618. * triggered before the tree is destroyed
  619. * @event
  620. * @name destroy.jstree
  621. */
  622. this.trigger("destroy");
  623. if(this._wrk) {
  624. try {
  625. window.URL.revokeObjectURL(this._wrk);
  626. this._wrk = null;
  627. }
  628. catch (ignore) { }
  629. }
  630. if(!keep_html) { this.element.empty(); }
  631. this.teardown();
  632. },
  633. /**
  634. * Create a prototype node
  635. * @name _create_prototype_node()
  636. * @return {DOMElement}
  637. */
  638. _create_prototype_node : function () {
  639. var _node = document.createElement('LI'), _temp1, _temp2;
  640. _node.setAttribute('role', 'none');
  641. _temp1 = document.createElement('I');
  642. _temp1.className = 'jstree-icon jstree-ocl';
  643. _temp1.setAttribute('role', 'presentation');
  644. _node.appendChild(_temp1);
  645. _temp1 = document.createElement('A');
  646. _temp1.className = 'jstree-anchor';
  647. _temp1.setAttribute('href','#');
  648. _temp1.setAttribute('tabindex','-1');
  649. _temp1.setAttribute('role', 'treeitem');
  650. _temp2 = document.createElement('I');
  651. _temp2.className = 'jstree-icon jstree-themeicon';
  652. _temp2.setAttribute('role', 'presentation');
  653. _temp1.appendChild(_temp2);
  654. _node.appendChild(_temp1);
  655. _temp1 = _temp2 = null;
  656. return _node;
  657. },
  658. _kbevent_to_func : function (e) {
  659. var keys = {
  660. 8: "Backspace", 9: "Tab", 13: "Enter", 19: "Pause", 27: "Esc",
  661. 32: "Space", 33: "PageUp", 34: "PageDown", 35: "End", 36: "Home",
  662. 37: "Left", 38: "Up", 39: "Right", 40: "Down", 44: "Print", 45: "Insert",
  663. 46: "Delete", 96: "Numpad0", 97: "Numpad1", 98: "Numpad2", 99 : "Numpad3",
  664. 100: "Numpad4", 101: "Numpad5", 102: "Numpad6", 103: "Numpad7",
  665. 104: "Numpad8", 105: "Numpad9", '-13': "NumpadEnter", 112: "F1",
  666. 113: "F2", 114: "F3", 115: "F4", 116: "F5", 117: "F6", 118: "F7",
  667. 119: "F8", 120: "F9", 121: "F10", 122: "F11", 123: "F12", 144: "Numlock",
  668. 145: "Scrolllock", 16: 'Shift', 17: 'Ctrl', 18: 'Alt',
  669. 48: '0', 49: '1', 50: '2', 51: '3', 52: '4', 53: '5',
  670. 54: '6', 55: '7', 56: '8', 57: '9', 59: ';', 61: '=', 65: 'a',
  671. 66: 'b', 67: 'c', 68: 'd', 69: 'e', 70: 'f', 71: 'g', 72: 'h',
  672. 73: 'i', 74: 'j', 75: 'k', 76: 'l', 77: 'm', 78: 'n', 79: 'o',
  673. 80: 'p', 81: 'q', 82: 'r', 83: 's', 84: 't', 85: 'u', 86: 'v',
  674. 87: 'w', 88: 'x', 89: 'y', 90: 'z', 107: '+', 109: '-', 110: '.',
  675. 186: ';', 187: '=', 188: ',', 189: '-', 190: '.', 191: '/', 192: '`',
  676. 219: '[', 220: '\\',221: ']', 222: "'", 111: '/', 106: '*', 173: '-'
  677. };
  678. var parts = [];
  679. if (e.ctrlKey) { parts.push('ctrl'); }
  680. if (e.altKey) { parts.push('alt'); }
  681. if (e.shiftKey) { parts.push('shift'); }
  682. parts.push(keys[e.which] ? keys[e.which].toLowerCase() : e.which);
  683. parts = parts.sort().join('-').toLowerCase();
  684. if (parts === 'shift-shift' || parts === 'ctrl-ctrl' || parts === 'alt-alt') {
  685. return null;
  686. }
  687. var kb = this.settings.core.keyboard, i, tmp;
  688. for (i in kb) {
  689. if (kb.hasOwnProperty(i)) {
  690. tmp = i;
  691. if (tmp !== '-' && tmp !== '+') {
  692. tmp = tmp.replace('--', '-MINUS').replace('+-', '-MINUS').replace('++', '-PLUS').replace('-+', '-PLUS');
  693. tmp = tmp.split(/-|\+/).sort().join('-').replace('MINUS', '-').replace('PLUS', '+').toLowerCase();
  694. }
  695. if (tmp === parts) {
  696. return kb[i];
  697. }
  698. }
  699. }
  700. return null;
  701. },
  702. /**
  703. * part of the destroying of an instance. Used internally.
  704. * @private
  705. * @name teardown()
  706. */
  707. teardown : function () {
  708. this.unbind();
  709. this.element
  710. .removeClass('jstree')
  711. .removeData('jstree')
  712. .find("[class^='jstree']")
  713. .addBack()
  714. .attr("class", function () { return this.className.replace(/jstree[^ ]*|$/ig,''); });
  715. this.element = null;
  716. },
  717. /**
  718. * bind all events. Used internally.
  719. * @private
  720. * @name bind()
  721. */
  722. bind : function () {
  723. var word = '',
  724. tout = null,
  725. was_click = 0;
  726. this.element
  727. .on("dblclick.jstree", function (e) {
  728. if(e.target.tagName && e.target.tagName.toLowerCase() === "input") { return true; }
  729. if(document.selection && document.selection.empty) {
  730. document.selection.empty();
  731. }
  732. else {
  733. if(window.getSelection) {
  734. var sel = window.getSelection();
  735. try {
  736. sel.removeAllRanges();
  737. sel.collapse();
  738. } catch (ignore) { }
  739. }
  740. }
  741. })
  742. .on("mousedown.jstree", function (e) {
  743. if(e.target === this.element[0]) {
  744. e.preventDefault(); // prevent losing focus when clicking scroll arrows (FF, Chrome)
  745. was_click = +(new Date()); // ie does not allow to prevent losing focus
  746. }
  747. }.bind(this))
  748. .on("mousedown.jstree", ".jstree-ocl", function (e) {
  749. e.preventDefault(); // prevent any node inside from losing focus when clicking the open/close icon
  750. })
  751. .on("click.jstree", ".jstree-ocl", function (e) {
  752. this.toggle_node(e.target);
  753. }.bind(this))
  754. .on("dblclick.jstree", ".jstree-anchor", function (e) {
  755. if(e.target.tagName && e.target.tagName.toLowerCase() === "input") { return true; }
  756. if(this.settings.core.dblclick_toggle) {
  757. this.toggle_node(e.target);
  758. }
  759. }.bind(this))
  760. .on("click.jstree", ".jstree-anchor", function (e) {
  761. e.preventDefault();
  762. if(e.currentTarget !== document.activeElement) { $(e.currentTarget).trigger('focus'); }
  763. this.activate_node(e.currentTarget, e);
  764. }.bind(this))
  765. .on('keydown.jstree', '.jstree-anchor', function (e) {
  766. if(e.target.tagName && e.target.tagName.toLowerCase() === "input") { return true; }
  767. if(this._data.core.rtl) {
  768. if(e.which === 37) { e.which = 39; }
  769. else if(e.which === 39) { e.which = 37; }
  770. }
  771. var f = this._kbevent_to_func(e);
  772. if (f) {
  773. var r = f.call(this, e);
  774. if (r === false || r === true) {
  775. return r;
  776. }
  777. }
  778. }.bind(this))
  779. .on("load_node.jstree", function (e, data) {
  780. if(data.status) {
  781. if(data.node.id === $.jstree.root && !this._data.core.loaded) {
  782. this._data.core.loaded = true;
  783. if(this._firstChild(this.get_container_ul()[0])) {
  784. this.element.attr('aria-activedescendant',this._firstChild(this.get_container_ul()[0]).id);
  785. }
  786. /**
  787. * triggered after the root node is loaded for the first time
  788. * @event
  789. * @name loaded.jstree
  790. */
  791. this.trigger("loaded");
  792. }
  793. if(!this._data.core.ready) {
  794. setTimeout(function() {
  795. if(this.element && !this.get_container_ul().find('.jstree-loading').length) {
  796. this._data.core.ready = true;
  797. if(this._data.core.selected.length) {
  798. if(this.settings.core.expand_selected_onload) {
  799. var tmp = [], i, j;
  800. for(i = 0, j = this._data.core.selected.length; i < j; i++) {
  801. tmp = tmp.concat(this._model.data[this._data.core.selected[i]].parents);
  802. }
  803. tmp = $.vakata.array_unique(tmp);
  804. for(i = 0, j = tmp.length; i < j; i++) {
  805. this.open_node(tmp[i], false, 0);
  806. }
  807. }
  808. this.trigger('changed', { 'action' : 'ready', 'selected' : this._data.core.selected });
  809. }
  810. /**
  811. * triggered after all nodes are finished loading
  812. * @event
  813. * @name ready.jstree
  814. */
  815. this.trigger("ready");
  816. }
  817. }.bind(this), 0);
  818. }
  819. }
  820. }.bind(this))
  821. // quick searching when the tree is focused
  822. .on('keypress.jstree', function (e) {
  823. if(e.target.tagName && e.target.tagName.toLowerCase() === "input") { return true; }
  824. if(tout) { clearTimeout(tout); }
  825. tout = setTimeout(function () {
  826. word = '';
  827. }, 500);
  828. var chr = String.fromCharCode(e.which).toLowerCase(),
  829. col = this.element.find('.jstree-anchor').filter(':visible'),
  830. ind = col.index(document.activeElement) || 0,
  831. end = false;
  832. word += chr;
  833. // match for whole word from current node down (including the current node)
  834. if(word.length > 1) {
  835. col.slice(ind).each(function (i, v) {
  836. if($(v).text().toLowerCase().indexOf(word) === 0) {
  837. $(v).trigger('focus');
  838. end = true;
  839. return false;
  840. }
  841. }.bind(this));
  842. if(end) { return; }
  843. // match for whole word from the beginning of the tree
  844. col.slice(0, ind).each(function (i, v) {
  845. if($(v).text().toLowerCase().indexOf(word) === 0) {
  846. $(v).trigger('focus');
  847. end = true;
  848. return false;
  849. }
  850. }.bind(this));
  851. if(end) { return; }
  852. }
  853. // list nodes that start with that letter (only if word consists of a single char)
  854. if(new RegExp('^' + chr.replace(/[-\/\\^$*+?.()|[\]{}]/g, '\\$&') + '+$').test(word)) {
  855. // search for the next node starting with that letter
  856. col.slice(ind + 1).each(function (i, v) {
  857. if($(v).text().toLowerCase().charAt(0) === chr) {
  858. $(v).trigger('focus');
  859. end = true;
  860. return false;
  861. }
  862. }.bind(this));
  863. if(end) { return; }
  864. // search from the beginning
  865. col.slice(0, ind + 1).each(function (i, v) {
  866. if($(v).text().toLowerCase().charAt(0) === chr) {
  867. $(v).trigger('focus');
  868. end = true;
  869. return false;
  870. }
  871. }.bind(this));
  872. if(end) { return; }
  873. }
  874. }.bind(this))
  875. // THEME RELATED
  876. .on("init.jstree", function () {
  877. var s = this.settings.core.themes;
  878. this._data.core.themes.dots = s.dots;
  879. this._data.core.themes.stripes = s.stripes;
  880. this._data.core.themes.icons = s.icons;
  881. this._data.core.themes.ellipsis = s.ellipsis;
  882. this.set_theme(s.name || "default", s.url);
  883. this.set_theme_variant(s.variant);
  884. }.bind(this))
  885. .on("loading.jstree", function () {
  886. this[ this._data.core.themes.dots ? "show_dots" : "hide_dots" ]();
  887. this[ this._data.core.themes.icons ? "show_icons" : "hide_icons" ]();
  888. this[ this._data.core.themes.stripes ? "show_stripes" : "hide_stripes" ]();
  889. this[ this._data.core.themes.ellipsis ? "show_ellipsis" : "hide_ellipsis" ]();
  890. }.bind(this))
  891. .on('blur.jstree', '.jstree-anchor', function (e) {
  892. this._data.core.focused = null;
  893. $(e.currentTarget).filter('.jstree-hovered').trigger('mouseleave');
  894. this.element.attr('tabindex', '0');
  895. $(e.currentTarget).attr('tabindex', '-1');
  896. }.bind(this))
  897. .on('focus.jstree', '.jstree-anchor', function (e) {
  898. var tmp = this.get_node(e.currentTarget);
  899. if(tmp && (tmp.id || tmp.id === 0)) {
  900. this._data.core.focused = tmp.id;
  901. }
  902. this.element.find('.jstree-hovered').not(e.currentTarget).trigger('mouseleave');
  903. $(e.currentTarget).trigger('mouseenter');
  904. this.element.attr('tabindex', '-1');
  905. $(e.currentTarget).attr('tabindex', '0');
  906. }.bind(this))
  907. .on('focus.jstree', function () {
  908. if(+(new Date()) - was_click > 500 && !this._data.core.focused && this.settings.core.restore_focus) {
  909. was_click = 0;
  910. var act = this.get_node(this.element.attr('aria-activedescendant'), true);
  911. if(act) {
  912. act.find('> .jstree-anchor').trigger('focus');
  913. }
  914. }
  915. }.bind(this))
  916. .on('mouseenter.jstree', '.jstree-anchor', function (e) {
  917. this.hover_node(e.currentTarget);
  918. }.bind(this))
  919. .on('mouseleave.jstree', '.jstree-anchor', function (e) {
  920. this.dehover_node(e.currentTarget);
  921. }.bind(this));
  922. },
  923. /**
  924. * part of the destroying of an instance. Used internally.
  925. * @private
  926. * @name unbind()
  927. */
  928. unbind : function () {
  929. this.element.off('.jstree');
  930. $(document).off('.jstree-' + this._id);
  931. },
  932. /**
  933. * trigger an event. Used internally.
  934. * @private
  935. * @name trigger(ev [, data])
  936. * @param {String} ev the name of the event to trigger
  937. * @param {Object} data additional data to pass with the event
  938. */
  939. trigger : function (ev, data) {
  940. if(!data) {
  941. data = {};
  942. }
  943. data.instance = this;
  944. this.element.triggerHandler(ev.replace('.jstree','') + '.jstree', data);
  945. },
  946. /**
  947. * returns the jQuery extended instance container
  948. * @name get_container()
  949. * @return {jQuery}
  950. */
  951. get_container : function () {
  952. return this.element;
  953. },
  954. /**
  955. * returns the jQuery extended main UL node inside the instance container. Used internally.
  956. * @private
  957. * @name get_container_ul()
  958. * @return {jQuery}
  959. */
  960. get_container_ul : function () {
  961. return this.element.children(".jstree-children").first();
  962. },
  963. /**
  964. * gets string replacements (localization). Used internally.
  965. * @private
  966. * @name get_string(key)
  967. * @param {String} key
  968. * @return {String}
  969. */
  970. get_string : function (key) {
  971. var a = this.settings.core.strings;
  972. if($.vakata.is_function(a)) { return a.call(this, key); }
  973. if(a && a[key]) { return a[key]; }
  974. return key;
  975. },
  976. /**
  977. * gets the first child of a DOM node. Used internally.
  978. * @private
  979. * @name _firstChild(dom)
  980. * @param {DOMElement} dom
  981. * @return {DOMElement}
  982. */
  983. _firstChild : function (dom) {
  984. dom = dom ? dom.firstChild : null;
  985. while(dom !== null && dom.nodeType !== 1) {
  986. dom = dom.nextSibling;
  987. }
  988. return dom;
  989. },
  990. /**
  991. * gets the next sibling of a DOM node. Used internally.
  992. * @private
  993. * @name _nextSibling(dom)
  994. * @param {DOMElement} dom
  995. * @return {DOMElement}
  996. */
  997. _nextSibling : function (dom) {
  998. dom = dom ? dom.nextSibling : null;
  999. while(dom !== null && dom.nodeType !== 1) {
  1000. dom = dom.nextSibling;
  1001. }
  1002. return dom;
  1003. },
  1004. /**
  1005. * gets the previous sibling of a DOM node. Used internally.
  1006. * @private
  1007. * @name _previousSibling(dom)
  1008. * @param {DOMElement} dom
  1009. * @return {DOMElement}
  1010. */
  1011. _previousSibling : function (dom) {
  1012. dom = dom ? dom.previousSibling : null;
  1013. while(dom !== null && dom.nodeType !== 1) {
  1014. dom = dom.previousSibling;
  1015. }
  1016. return dom;
  1017. },
  1018. /**
  1019. * get the JSON representation of a node (or the actual jQuery extended DOM node) by using any input (child DOM element, ID string, selector, etc)
  1020. * @name get_node(obj [, as_dom])
  1021. * @param {mixed} obj
  1022. * @param {Boolean} as_dom
  1023. * @return {Object|jQuery}
  1024. */
  1025. get_node : function (obj, as_dom) {
  1026. if(obj && (obj.id || obj.id === 0)) {
  1027. obj = obj.id;
  1028. }
  1029. if (obj instanceof $ && obj.length && obj[0].id) {
  1030. obj = obj[0].id;
  1031. }
  1032. var dom;
  1033. try {
  1034. if(this._model.data[obj]) {
  1035. obj = this._model.data[obj];
  1036. }
  1037. else if(typeof obj === "string" && this._model.data[obj.replace(/^#/, '')]) {
  1038. obj = this._model.data[obj.replace(/^#/, '')];
  1039. }
  1040. else if(typeof obj === "string" && (dom = $('#' + obj.replace($.jstree.idregex,'\\$&'), this.element)).length && this._model.data[dom.closest('.jstree-node').attr('id')]) {
  1041. obj = this._model.data[dom.closest('.jstree-node').attr('id')];
  1042. }
  1043. else if((dom = this.element.find(obj)).length && this._model.data[dom.closest('.jstree-node').attr('id')]) {
  1044. obj = this._model.data[dom.closest('.jstree-node').attr('id')];
  1045. }
  1046. else if((dom = this.element.find(obj)).length && dom.hasClass('jstree')) {
  1047. obj = this._model.data[$.jstree.root];
  1048. }
  1049. else {
  1050. return false;
  1051. }
  1052. if(as_dom) {
  1053. obj = obj.id === $.jstree.root ? this.element : $('#' + obj.id.replace($.jstree.idregex,'\\$&'), this.element);
  1054. }
  1055. return obj;
  1056. } catch (ex) { return false; }
  1057. },
  1058. /**
  1059. * get the path to a node, either consisting of node texts, or of node IDs, optionally glued together (otherwise an array)
  1060. * @name get_path(obj [, glue, ids])
  1061. * @param {mixed} obj the node
  1062. * @param {String} glue if you want the path as a string - pass the glue here (for example '/'), if a falsy value is supplied here, an array is returned
  1063. * @param {Boolean} ids if set to true build the path using ID, otherwise node text is used
  1064. * @return {mixed}
  1065. */
  1066. get_path : function (obj, glue, ids) {
  1067. obj = obj.parents ? obj : this.get_node(obj);
  1068. if(!obj || obj.id === $.jstree.root || !obj.parents) {
  1069. return false;
  1070. }
  1071. var i, j, p = [];
  1072. p.push(ids ? obj.id : obj.text);
  1073. for(i = 0, j = obj.parents.length; i < j; i++) {
  1074. p.push(ids ? obj.parents[i] : this.get_text(obj.parents[i]));
  1075. }
  1076. p = p.reverse().slice(1);
  1077. return glue ? p.join(glue) : p;
  1078. },
  1079. /**
  1080. * get the next visible node that is below the `obj` node. If `strict` is set to `true` only sibling nodes are returned.
  1081. * @name get_next_dom(obj [, strict])
  1082. * @param {mixed} obj
  1083. * @param {Boolean} strict
  1084. * @return {jQuery}
  1085. */
  1086. get_next_dom : function (obj, strict) {
  1087. var tmp;
  1088. obj = this.get_node(obj, true);
  1089. if(obj[0] === this.element[0]) {
  1090. tmp = this._firstChild(this.get_container_ul()[0]);
  1091. while (tmp && tmp.offsetHeight === 0) {
  1092. tmp = this._nextSibling(tmp);
  1093. }
  1094. return tmp ? $(tmp) : false;
  1095. }
  1096. if(!obj || !obj.length) {
  1097. return false;
  1098. }
  1099. if(strict) {
  1100. tmp = obj[0];
  1101. do {
  1102. tmp = this._nextSibling(tmp);
  1103. } while (tmp && tmp.offsetHeight === 0);
  1104. return tmp ? $(tmp) : false;
  1105. }
  1106. if(obj.hasClass("jstree-open")) {
  1107. tmp = this._firstChild(obj.children('.jstree-children')[0]);
  1108. while (tmp && tmp.offsetHeight === 0) {
  1109. tmp = this._nextSibling(tmp);
  1110. }
  1111. if(tmp !== null) {
  1112. return $(tmp);
  1113. }
  1114. }
  1115. tmp = obj[0];
  1116. do {
  1117. tmp = this._nextSibling(tmp);
  1118. } while (tmp && tmp.offsetHeight === 0);
  1119. if(tmp !== null) {
  1120. return $(tmp);
  1121. }
  1122. return obj.parentsUntil(".jstree",".jstree-node").nextAll(".jstree-node:visible").first();
  1123. },
  1124. /**
  1125. * get the previous visible node that is above the `obj` node. If `strict` is set to `true` only sibling nodes are returned.
  1126. * @name get_prev_dom(obj [, strict])
  1127. * @param {mixed} obj
  1128. * @param {Boolean} strict
  1129. * @return {jQuery}
  1130. */
  1131. get_prev_dom : function (obj, strict) {
  1132. var tmp;
  1133. obj = this.get_node(obj, true);
  1134. if(obj[0] === this.element[0]) {
  1135. tmp = this.get_container_ul()[0].lastChild;
  1136. while (tmp && tmp.offsetHeight === 0) {
  1137. tmp = this._previousSibling(tmp);
  1138. }
  1139. return tmp ? $(tmp) : false;
  1140. }
  1141. if(!obj || !obj.length) {
  1142. return false;
  1143. }
  1144. if(strict) {
  1145. tmp = obj[0];
  1146. do {
  1147. tmp = this._previousSibling(tmp);
  1148. } while (tmp && tmp.offsetHeight === 0);
  1149. return tmp ? $(tmp) : false;
  1150. }
  1151. tmp = obj[0];
  1152. do {
  1153. tmp = this._previousSibling(tmp);
  1154. } while (tmp && tmp.offsetHeight === 0);
  1155. if(tmp !== null) {
  1156. obj = $(tmp);
  1157. while(obj.hasClass("jstree-open")) {
  1158. obj = obj.children(".jstree-children").first().children(".jstree-node:visible:last");
  1159. }
  1160. return obj;
  1161. }
  1162. tmp = obj[0].parentNode.parentNode;
  1163. return tmp && tmp.className && tmp.className.indexOf('jstree-node') !== -1 ? $(tmp) : false;
  1164. },
  1165. /**
  1166. * get the parent ID of a node
  1167. * @name get_parent(obj)
  1168. * @param {mixed} obj
  1169. * @return {String}
  1170. */
  1171. get_parent : function (obj) {
  1172. obj = this.get_node(obj);
  1173. if(!obj || obj.id === $.jstree.root) {
  1174. return false;
  1175. }
  1176. return obj.parent;
  1177. },
  1178. /**
  1179. * get a jQuery collection of all the children of a node (node must be rendered), returns false on error
  1180. * @name get_children_dom(obj)
  1181. * @param {mixed} obj
  1182. * @return {jQuery}
  1183. */
  1184. get_children_dom : function (obj) {
  1185. obj = this.get_node(obj, true);
  1186. if(obj[0] === this.element[0]) {
  1187. return this.get_container_ul().children(".jstree-node");
  1188. }
  1189. if(!obj || !obj.length) {
  1190. return false;
  1191. }
  1192. return obj.children(".jstree-children").children(".jstree-node");
  1193. },
  1194. /**
  1195. * checks if a node has children
  1196. * @name is_parent(obj)
  1197. * @param {mixed} obj
  1198. * @return {Boolean}
  1199. */
  1200. is_parent : function (obj) {
  1201. obj = this.get_node(obj);
  1202. return obj && (obj.state.loaded === false || obj.children.length > 0);
  1203. },
  1204. /**
  1205. * checks if a node is loaded (its children are available)
  1206. * @name is_loaded(obj)
  1207. * @param {mixed} obj
  1208. * @return {Boolean}
  1209. */
  1210. is_loaded : function (obj) {
  1211. obj = this.get_node(obj);
  1212. return obj && obj.state.loaded;
  1213. },
  1214. /**
  1215. * check if a node is currently loading (fetching children)
  1216. * @name is_loading(obj)
  1217. * @param {mixed} obj
  1218. * @return {Boolean}
  1219. */
  1220. is_loading : function (obj) {
  1221. obj = this.get_node(obj);
  1222. return obj && obj.state && obj.state.loading;
  1223. },
  1224. /**
  1225. * check if a node is opened
  1226. * @name is_open(obj)
  1227. * @param {mixed} obj
  1228. * @return {Boolean}
  1229. */
  1230. is_open : function (obj) {
  1231. obj = this.get_node(obj);
  1232. return obj && obj.state.opened;
  1233. },
  1234. /**
  1235. * check if a node is in a closed state
  1236. * @name is_closed(obj)
  1237. * @param {mixed} obj
  1238. * @return {Boolean}
  1239. */
  1240. is_closed : function (obj) {
  1241. obj = this.get_node(obj);
  1242. return obj && this.is_parent(obj) && !obj.state.opened;
  1243. },
  1244. /**
  1245. * check if a node has no children
  1246. * @name is_leaf(obj)
  1247. * @param {mixed} obj
  1248. * @return {Boolean}
  1249. */
  1250. is_leaf : function (obj) {
  1251. return !this.is_parent(obj);
  1252. },
  1253. /**
  1254. * loads a node (fetches its children using the `core.data` setting). Multiple nodes can be passed to by using an array.
  1255. * @name load_node(obj [, callback])
  1256. * @param {mixed} obj
  1257. * @param {function} callback a function to be executed once loading is complete, the function is executed in the instance's scope and receives two arguments - the node and a boolean status
  1258. * @return {Boolean}
  1259. * @trigger load_node.jstree
  1260. */
  1261. load_node : function (obj, callback) {
  1262. var dom = this.get_node(obj, true), k, l, i, j, c;
  1263. if($.vakata.is_array(obj)) {
  1264. this._load_nodes(obj.slice(), callback);
  1265. return true;
  1266. }
  1267. obj = this.get_node(obj);
  1268. if(!obj) {
  1269. if(callback) { callback.call(this, obj, false); }
  1270. return false;
  1271. }
  1272. // if(obj.state.loading) { } // the node is already loading - just wait for it to load and invoke callback? but if called implicitly it should be loaded again?
  1273. if(obj.state.loaded) {
  1274. obj.state.loaded = false;
  1275. for(i = 0, j = obj.parents.length; i < j; i++) {
  1276. this._model.data[obj.parents[i]].children_d = $.vakata.array_filter(this._model.data[obj.parents[i]].children_d, function (v) {
  1277. return $.inArray(v, obj.children_d) === -1;
  1278. });
  1279. }
  1280. for(k = 0, l = obj.children_d.length; k < l; k++) {
  1281. if(this._model.data[obj.children_d[k]].state.selected) {
  1282. c = true;
  1283. }
  1284. delete this._model.data[obj.children_d[k]];
  1285. }
  1286. if (c) {
  1287. this._data.core.selected = $.vakata.array_filter(this._data.core.selected, function (v) {
  1288. return $.inArray(v, obj.children_d) === -1;
  1289. });
  1290. }
  1291. obj.children = [];
  1292. obj.children_d = [];
  1293. if(c) {
  1294. this.trigger('changed', { 'action' : 'load_node', 'node' : obj, 'selected' : this._data.core.selected });
  1295. }
  1296. }
  1297. obj.state.failed = false;
  1298. obj.state.loading = true;
  1299. if (obj.id !== $.jstree.root) {
  1300. dom.children(".jstree-anchor").attr('aria-busy', true);
  1301. } else {
  1302. dom.attr('aria-busy', true);
  1303. }
  1304. dom.addClass("jstree-loading");
  1305. this._load_node(obj, function (status) {
  1306. obj = this._model.data[obj.id];
  1307. obj.state.loading = false;
  1308. obj.state.loaded = status;
  1309. obj.state.failed = !obj.state.loaded;
  1310. var dom = this.get_node(obj, true), i = 0, j = 0, m = this._model.data, has_children = false;
  1311. for(i = 0, j = obj.children.length; i < j; i++) {
  1312. if(m[obj.children[i]] && !m[obj.children[i]].state.hidden) {
  1313. has_children = true;
  1314. break;
  1315. }
  1316. }
  1317. if(obj.state.loaded && dom && dom.length) {
  1318. dom.removeClass('jstree-closed jstree-open jstree-leaf');
  1319. if (!has_children) {
  1320. dom.addClass('jstree-leaf');
  1321. }
  1322. else {
  1323. if (obj.id !== '#') {
  1324. dom.addClass(obj.state.opened ? 'jstree-open' : 'jstree-closed');
  1325. }
  1326. }
  1327. }
  1328. if (obj.id !== $.jstree.root) {
  1329. dom.children(".jstree-anchor").attr('aria-busy', false);
  1330. } else {
  1331. dom.attr('aria-busy', false);
  1332. }
  1333. dom.removeClass("jstree-loading");
  1334. /**
  1335. * triggered after a node is loaded
  1336. * @event
  1337. * @name load_node.jstree
  1338. * @param {Object} node the node that was loading
  1339. * @param {Boolean} status was the node loaded successfully
  1340. */
  1341. this.trigger('load_node', { "node" : obj, "status" : status });
  1342. if(callback) {
  1343. callback.call(this, obj, status);
  1344. }
  1345. }.bind(this));
  1346. return true;
  1347. },
  1348. /**
  1349. * load an array of nodes (will also load unavailable nodes as soon as they appear in the structure). Used internally.
  1350. * @private
  1351. * @name _load_nodes(nodes [, callback])
  1352. * @param {array} nodes
  1353. * @param {function} callback a function to be executed once loading is complete, the function is executed in the instance's scope and receives one argument - the array passed to _load_nodes
  1354. */
  1355. _load_nodes : function (nodes, callback, is_callback, force_reload) {
  1356. var r = true,
  1357. c = function () { this._load_nodes(nodes, callback, true); },
  1358. m = this._model.data, i, j, tmp = [];
  1359. for(i = 0, j = nodes.length; i < j; i++) {
  1360. if(m[nodes[i]] && ( (!m[nodes[i]].state.loaded && !m[nodes[i]].state.failed) || (!is_callback && force_reload) )) {
  1361. if(!this.is_loading(nodes[i])) {
  1362. this.load_node(nodes[i], c);
  1363. }
  1364. r = false;
  1365. }
  1366. }
  1367. if(r) {
  1368. for(i = 0, j = nodes.length; i < j; i++) {
  1369. if(m[nodes[i]] && m[nodes[i]].state.loaded) {
  1370. tmp.push(nodes[i]);
  1371. }
  1372. }
  1373. if(callback && !callback.done) {
  1374. callback.call(this, tmp);
  1375. callback.done = true;
  1376. }
  1377. }
  1378. },
  1379. /**
  1380. * loads all unloaded nodes
  1381. * @name load_all([obj, callback])
  1382. * @param {mixed} obj the node to load recursively, omit to load all nodes in the tree
  1383. * @param {function} callback a function to be executed once loading all the nodes is complete,
  1384. * @trigger load_all.jstree
  1385. */
  1386. load_all : function (obj, callback) {
  1387. if(!obj) { obj = $.jstree.root; }
  1388. obj = this.get_node(obj);
  1389. if(!obj) { return false; }
  1390. var to_load = [],
  1391. m = this._model.data,
  1392. c = m[obj.id].children_d,
  1393. i, j;
  1394. if(obj.state && !obj.state.loaded) {
  1395. to_load.push(obj.id);
  1396. }
  1397. for(i = 0, j = c.length; i < j; i++) {
  1398. if(m[c[i]] && m[c[i]].state && !m[c[i]].state.loaded) {
  1399. to_load.push(c[i]);
  1400. }
  1401. }
  1402. if(to_load.length) {
  1403. this._load_nodes(to_load, function () {
  1404. this.load_all(obj, callback);
  1405. });
  1406. }
  1407. else {
  1408. /**
  1409. * triggered after a load_all call completes
  1410. * @event
  1411. * @name load_all.jstree
  1412. * @param {Object} node the recursively loaded node
  1413. */
  1414. if(callback) { callback.call(this, obj); }
  1415. this.trigger('load_all', { "node" : obj });
  1416. }
  1417. },
  1418. /**
  1419. * handles the actual loading of a node. Used only internally.
  1420. * @private
  1421. * @name _load_node(obj [, callback])
  1422. * @param {mixed} obj
  1423. * @param {function} callback a function to be executed once loading is complete, the function is executed in the instance's scope and receives one argument - a boolean status
  1424. * @return {Boolean}
  1425. */
  1426. _load_node : function (obj, callback) {
  1427. var s = this.settings.core.data, t;
  1428. var notTextOrCommentNode = function notTextOrCommentNode () {
  1429. return this.nodeType !== 3 && this.nodeType !== 8;
  1430. };
  1431. // use original HTML
  1432. if(!s) {
  1433. if(obj.id === $.jstree.root) {
  1434. return this._append_html_data(obj, this._data.core.original_container_html.clone(true), function (status) {
  1435. callback.call(this, status);
  1436. });
  1437. }
  1438. else {
  1439. return callback.call(this, false);
  1440. }
  1441. // return callback.call(this, obj.id === $.jstree.root ? this._append_html_data(obj, this._data.core.original_container_html.clone(true)) : false);
  1442. }
  1443. if($.vakata.is_function(s)) {
  1444. return s.call(this, obj, function (d) {
  1445. if(d === false) {
  1446. callback.call(this, false);
  1447. }
  1448. else {
  1449. this[typeof d === 'string' ? '_append_html_data' : '_append_json_data'](obj, typeof d === 'string' ? $($.parseHTML(d)).filter(notTextOrCommentNode) : d, function (status) {
  1450. callback.call(this, status);
  1451. });
  1452. }
  1453. // return d === false ? callback.call(this, false) : callback.call(this, this[typeof d === 'string' ? '_append_html_data' : '_append_json_data'](obj, typeof d === 'string' ? $(d) : d));
  1454. }.bind(this));
  1455. }
  1456. if(typeof s === 'object') {
  1457. if(s.url) {
  1458. s = $.extend(true, {}, s);
  1459. if($.vakata.is_function(s.url)) {
  1460. s.url = s.url.call(this, obj);
  1461. }
  1462. if($.vakata.is_function(s.data)) {
  1463. s.data = s.data.call(this, obj);
  1464. }
  1465. return $.ajax(s)
  1466. .done(function (d,t,x) {
  1467. var type = x.getResponseHeader('Content-Type');
  1468. if((type && type.indexOf('json') !== -1) || typeof d === "object") {
  1469. return this._append_json_data(obj, d, function (status) { callback.call(this, status); });
  1470. //return callback.call(this, this._append_json_data(obj, d));
  1471. }
  1472. if((type && type.indexOf('html') !== -1) || typeof d === "string") {
  1473. return this._append_html_data(obj, $($.parseHTML(d)).filter(notTextOrCommentNode), function (status) { callback.call(this, status); });
  1474. // return callback.call(this, this._append_html_data(obj, $(d)));
  1475. }
  1476. this._data.core.last_error = { 'error' : 'ajax', 'plugin' : 'core', 'id' : 'core_04', 'reason' : 'Could not load node', 'data' : JSON.stringify({ 'id' : obj.id, 'xhr' : x }) };
  1477. this.settings.core.error.call(this, this._data.core.last_error);
  1478. return callback.call(this, false);
  1479. }.bind(this))
  1480. .fail(function (f) {
  1481. this._data.core.last_error = { 'error' : 'ajax', 'plugin' : 'core', 'id' : 'core_04', 'reason' : 'Could not load node', 'data' : JSON.stringify({ 'id' : obj.id, 'xhr' : f }) };
  1482. callback.call(this, false);
  1483. this.settings.core.error.call(this, this._data.core.last_error);
  1484. }.bind(this));
  1485. }
  1486. if ($.vakata.is_array(s)) {
  1487. t = $.extend(true, [], s);
  1488. } else if ($.isPlainObject(s)) {
  1489. t = $.extend(true, {}, s);
  1490. } else {
  1491. t = s;
  1492. }
  1493. if(obj.id === $.jstree.root) {
  1494. return this._append_json_data(obj, t, function (status) {
  1495. callback.call(this, status);
  1496. });
  1497. }
  1498. else {
  1499. this._data.core.last_error = { 'error' : 'nodata', 'plugin' : 'core', 'id' : 'core_05', 'reason' : 'Could not load node', 'data' : JSON.stringify({ 'id' : obj.id }) };
  1500. this.settings.core.error.call(this, this._data.core.last_error);
  1501. return callback.call(this, false);
  1502. }
  1503. //return callback.call(this, (obj.id === $.jstree.root ? this._append_json_data(obj, t) : false) );
  1504. }
  1505. if(typeof s === 'string') {
  1506. if(obj.id === $.jstree.root) {
  1507. return this._append_html_data(obj, $($.parseHTML(s)).filter(notTextOrCommentNode), function (status) {
  1508. callback.call(this, status);
  1509. });
  1510. }
  1511. else {
  1512. this._data.core.last_error = { 'error' : 'nodata', 'plugin' : 'core', 'id' : 'core_06', 'reason' : 'Could not load node', 'data' : JSON.stringify({ 'id' : obj.id }) };
  1513. this.settings.core.error.call(this, this._data.core.last_error);
  1514. return callback.call(this, false);
  1515. }
  1516. //return callback.call(this, (obj.id === $.jstree.root ? this._append_html_data(obj, $(s)) : false) );
  1517. }
  1518. return callback.call(this, false);
  1519. },
  1520. /**
  1521. * adds a node to the list of nodes to redraw. Used only internally.
  1522. * @private
  1523. * @name _node_changed(obj [, callback])
  1524. * @param {mixed} obj
  1525. */
  1526. _node_changed : function (obj) {
  1527. obj = this.get_node(obj);
  1528. if (obj && $.inArray(obj.id, this._model.changed) === -1) {
  1529. this._model.changed.push(obj.id);
  1530. }
  1531. },
  1532. /**
  1533. * appends HTML content to the tree. Used internally.
  1534. * @private
  1535. * @name _append_html_data(obj, data)
  1536. * @param {mixed} obj the node to append to
  1537. * @param {String} data the HTML string to parse and append
  1538. * @trigger model.jstree, changed.jstree
  1539. */
  1540. _append_html_data : function (dom, data, cb) {
  1541. dom = this.get_node(dom);
  1542. dom.children = [];
  1543. dom.children_d = [];
  1544. var dat = data.is('ul') ? data.children() : data,
  1545. par = dom.id,
  1546. chd = [],
  1547. dpc = [],
  1548. m = this._model.data,
  1549. p = m[par],
  1550. s = this._data.core.selected.length,
  1551. tmp, i, j;
  1552. dat.each(function (i, v) {
  1553. tmp = this._parse_model_from_html($(v), par, p.parents.concat());
  1554. if(tmp) {
  1555. chd.push(tmp);
  1556. dpc.push(tmp);
  1557. if(m[tmp].children_d.length) {
  1558. dpc = dpc.concat(m[tmp].children_d);
  1559. }
  1560. }
  1561. }.bind(this));
  1562. p.children = chd;
  1563. p.children_d = dpc;
  1564. for(i = 0, j = p.parents.length; i < j; i++) {
  1565. m[p.parents[i]].children_d = m[p.parents[i]].children_d.concat(dpc);
  1566. }
  1567. /**
  1568. * triggered when new data is inserted to the tree model
  1569. * @event
  1570. * @name model.jstree
  1571. * @param {Array} nodes an array of node IDs
  1572. * @param {String} parent the parent ID of the nodes
  1573. */
  1574. this.trigger('model', { "nodes" : dpc, 'parent' : par });
  1575. if(par !== $.jstree.root) {
  1576. this._node_changed(par);
  1577. this.redraw();
  1578. }
  1579. else {
  1580. this.get_container_ul().children('.jstree-initial-node').remove();
  1581. this.redraw(true);
  1582. }
  1583. if(this._data.core.selected.length !== s) {
  1584. this.trigger('changed', { 'action' : 'model', 'selected' : this._data.core.selected });
  1585. }
  1586. cb.call(this, true);
  1587. },
  1588. /**
  1589. * appends JSON content to the tree. Used internally.
  1590. * @private
  1591. * @name _append_json_data(obj, data)
  1592. * @param {mixed} obj the node to append to
  1593. * @param {String} data the JSON object to parse and append
  1594. * @param {Boolean} force_processing internal param - do not set
  1595. * @trigger model.jstree, changed.jstree
  1596. */
  1597. _append_json_data : function (dom, data, cb, force_processing) {
  1598. if(this.element === null) { return; }
  1599. dom = this.get_node(dom);
  1600. dom.children = [];
  1601. dom.children_d = [];
  1602. // *%$@!!!
  1603. if(data.d) {
  1604. data = data.d;
  1605. if(typeof data === "string") {
  1606. data = JSON.parse(data);
  1607. }
  1608. }
  1609. if(!$.vakata.is_array(data)) { data = [data]; }
  1610. var w = null,
  1611. args = {
  1612. 'df' : this._model.default_state,
  1613. 'dat' : data,
  1614. 'par' : dom.id,
  1615. 'm' : this._model.data,
  1616. 't_id' : this._id,
  1617. 't_cnt' : this._cnt,
  1618. 'sel' : this._data.core.selected
  1619. },
  1620. inst = this,
  1621. func = function (data, undefined) {
  1622. if(data.data) { data = data.data; }
  1623. var dat = data.dat,
  1624. par = data.par,
  1625. chd = [],
  1626. dpc = [],
  1627. add = [],
  1628. df = data.df,
  1629. t_id = data.t_id,
  1630. t_cnt = data.t_cnt,
  1631. m = data.m,
  1632. p = m[par],
  1633. sel = data.sel,
  1634. tmp, i, j, rslt,
  1635. parse_flat = function (d, p, ps) {
  1636. if(!ps) { ps = []; }
  1637. else { ps = ps.concat(); }
  1638. if(p) { ps.unshift(p); }
  1639. var tid = d.id.toString(),
  1640. i, j, c, e,
  1641. tmp = {
  1642. id : tid,
  1643. text : d.text || '',
  1644. icon : d.icon !== undefined ? d.icon : true,
  1645. parent : p,
  1646. parents : ps,
  1647. children : d.children || [],
  1648. children_d : d.children_d || [],
  1649. data : d.data,
  1650. state : { },
  1651. li_attr : { id : false },
  1652. a_attr : { href : '#' },
  1653. original : false
  1654. };
  1655. for(i in df) {
  1656. if(df.hasOwnProperty(i)) {
  1657. tmp.state[i] = df[i];
  1658. }
  1659. }
  1660. if(d && d.data && d.data.jstree && d.data.jstree.icon) {
  1661. tmp.icon = d.data.jstree.icon;
  1662. }
  1663. if(tmp.icon === undefined || tmp.icon === null || tmp.icon === "") {
  1664. tmp.icon = true;
  1665. }
  1666. if(d && d.data) {
  1667. tmp.data = d.data;
  1668. if(d.data.jstree) {
  1669. for(i in d.data.jstree) {
  1670. if(d.data.jstree.hasOwnProperty(i)) {
  1671. tmp.state[i] = d.data.jstree[i];
  1672. }
  1673. }
  1674. }
  1675. }
  1676. if(d && typeof d.state === 'object') {
  1677. for (i in d.state) {
  1678. if(d.state.hasOwnProperty(i)) {
  1679. tmp.state[i] = d.state[i];
  1680. }
  1681. }
  1682. }
  1683. if(d && typeof d.li_attr === 'object') {
  1684. for (i in d.li_attr) {
  1685. if(d.li_attr.hasOwnProperty(i)) {
  1686. tmp.li_attr[i] = d.li_attr[i];
  1687. }
  1688. }
  1689. }
  1690. if(!tmp.li_attr.id) {
  1691. tmp.li_attr.id = tid;
  1692. }
  1693. if(d && typeof d.a_attr === 'object') {
  1694. for (i in d.a_attr) {
  1695. if(d.a_attr.hasOwnProperty(i)) {
  1696. tmp.a_attr[i] = d.a_attr[i];
  1697. }
  1698. }
  1699. }
  1700. if(d && d.children && d.children === true) {
  1701. tmp.state.loaded = false;
  1702. tmp.children = [];
  1703. tmp.children_d = [];
  1704. }
  1705. m[tmp.id] = tmp;
  1706. for(i = 0, j = tmp.children.length; i < j; i++) {
  1707. c = parse_flat(m[tmp.children[i]], tmp.id, ps);
  1708. e = m[c];
  1709. tmp.children_d.push(c);
  1710. if(e.children_d.length) {
  1711. tmp.children_d = tmp.children_d.concat(e.children_d);
  1712. }
  1713. }
  1714. delete d.data;
  1715. delete d.children;
  1716. m[tmp.id].original = d;
  1717. if(tmp.state.selected) {
  1718. add.push(tmp.id);
  1719. }
  1720. return tmp.id;
  1721. },
  1722. parse_nest = function (d, p, ps) {
  1723. if(!ps) { ps = []; }
  1724. else { ps = ps.concat(); }
  1725. if(p) { ps.unshift(p); }
  1726. var tid = false, i, j, c, e, tmp;
  1727. do {
  1728. tid = 'j' + t_id + '_' + (++t_cnt);
  1729. } while(m[tid]);
  1730. tmp = {
  1731. id : false,
  1732. text : typeof d === 'string' ? d : '',
  1733. icon : typeof d === 'object' && d.icon !== undefined ? d.icon : true,
  1734. parent : p,
  1735. parents : ps,
  1736. children : [],
  1737. children_d : [],
  1738. data : null,
  1739. state : { },
  1740. li_attr : { id : false },
  1741. a_attr : { href : '#' },
  1742. original : false
  1743. };
  1744. for(i in df) {
  1745. if(df.hasOwnProperty(i)) {
  1746. tmp.state[i] = df[i];
  1747. }
  1748. }
  1749. if(d && (d.id || d.id === 0)) { tmp.id = d.id.toString(); }
  1750. if(d && d.text) { tmp.text = d.text; }
  1751. if(d && d.data && d.data.jstree && d.data.jstree.icon) {
  1752. tmp.icon = d.data.jstree.icon;
  1753. }
  1754. if(tmp.icon === undefined || tmp.icon === null || tmp.icon === "") {
  1755. tmp.icon = true;
  1756. }
  1757. if(d && d.data) {
  1758. tmp.data = d.data;
  1759. if(d.data.jstree) {
  1760. for(i in d.data.jstree) {
  1761. if(d.data.jstree.hasOwnProperty(i)) {
  1762. tmp.state[i] = d.data.jstree[i];
  1763. }
  1764. }
  1765. }
  1766. }
  1767. if(d && typeof d.state === 'object') {
  1768. for (i in d.state) {
  1769. if(d.state.hasOwnProperty(i)) {
  1770. tmp.state[i] = d.state[i];
  1771. }
  1772. }
  1773. }
  1774. if(d && typeof d.li_attr === 'object') {
  1775. for (i in d.li_attr) {
  1776. if(d.li_attr.hasOwnProperty(i)) {
  1777. tmp.li_attr[i] = d.li_attr[i];
  1778. }
  1779. }
  1780. }
  1781. if(tmp.li_attr.id && !(tmp.id || tmp.id === 0)) {
  1782. tmp.id = tmp.li_attr.id.toString();
  1783. }
  1784. if(!(tmp.id || tmp.id === 0)) {
  1785. tmp.id = tid;
  1786. }
  1787. if(!tmp.li_attr.id) {
  1788. tmp.li_attr.id = tmp.id;
  1789. }
  1790. if(d && typeof d.a_attr === 'object') {
  1791. for (i in d.a_attr) {
  1792. if(d.a_attr.hasOwnProperty(i)) {
  1793. tmp.a_attr[i] = d.a_attr[i];
  1794. }
  1795. }
  1796. }
  1797. if(d && d.children && d.children.length) {
  1798. for(i = 0, j = d.children.length; i < j; i++) {
  1799. c = parse_nest(d.children[i], tmp.id, ps);
  1800. e = m[c];
  1801. tmp.children.push(c);
  1802. if(e.children_d.length) {
  1803. tmp.children_d = tmp.children_d.concat(e.children_d);
  1804. }
  1805. }
  1806. tmp.children_d = tmp.children_d.concat(tmp.children);
  1807. }
  1808. if(d && d.children && d.children === true) {
  1809. tmp.state.loaded = false;
  1810. tmp.children = [];
  1811. tmp.children_d = [];
  1812. }
  1813. delete d.data;
  1814. delete d.children;
  1815. tmp.original = d;
  1816. m[tmp.id] = tmp;
  1817. if(tmp.state.selected) {
  1818. add.push(tmp.id);
  1819. }
  1820. return tmp.id;
  1821. };
  1822. if(dat.length && dat[0].id !== undefined && dat[0].parent !== undefined) {
  1823. // Flat JSON support (for easy import from DB):
  1824. // 1) convert to object (foreach)
  1825. for(i = 0, j = dat.length; i < j; i++) {
  1826. if(!dat[i].children) {
  1827. dat[i].children = [];
  1828. }
  1829. if(!dat[i].state) {
  1830. dat[i].state = {};
  1831. }
  1832. m[dat[i].id.toString()] = dat[i];
  1833. }
  1834. // 2) populate children (foreach)
  1835. for(i = 0, j = dat.length; i < j; i++) {
  1836. if (!m[dat[i].parent.toString()]) {
  1837. if (typeof inst !== "undefined") {
  1838. inst._data.core.last_error = { 'error' : 'parse', 'plugin' : 'core', 'id' : 'core_07', 'reason' : 'Node with invalid parent', 'data' : JSON.stringify({ 'id' : dat[i].id.toString(), 'parent' : dat[i].parent.toString() }) };
  1839. inst.settings.core.error.call(inst, inst._data.core.last_error);
  1840. }
  1841. continue;
  1842. }
  1843. m[dat[i].parent.toString()].children.push(dat[i].id.toString());
  1844. // populate parent.children_d
  1845. p.children_d.push(dat[i].id.toString());
  1846. }
  1847. // 3) normalize && populate parents and children_d with recursion
  1848. for(i = 0, j = p.children.length; i < j; i++) {
  1849. tmp = parse_flat(m[p.children[i]], par, p.parents.concat());
  1850. dpc.push(tmp);
  1851. if(m[tmp].children_d.length) {
  1852. dpc = dpc.concat(m[tmp].children_d);
  1853. }
  1854. }
  1855. for(i = 0, j = p.parents.length; i < j; i++) {
  1856. m[p.parents[i]].children_d = m[p.parents[i]].children_d.concat(dpc);
  1857. }
  1858. // ?) three_state selection - p.state.selected && t - (if three_state foreach(dat => ch) -> foreach(parents) if(parent.selected) child.selected = true;
  1859. rslt = {
  1860. 'cnt' : t_cnt,
  1861. 'mod' : m,
  1862. 'sel' : sel,
  1863. 'par' : par,
  1864. 'dpc' : dpc,
  1865. 'add' : add
  1866. };
  1867. }
  1868. else {
  1869. for(i = 0, j = dat.length; i < j; i++) {
  1870. tmp = parse_nest(dat[i], par, p.parents.concat());
  1871. if(tmp) {
  1872. chd.push(tmp);
  1873. dpc.push(tmp);
  1874. if(m[tmp].children_d.length) {
  1875. dpc = dpc.concat(m[tmp].children_d);
  1876. }
  1877. }
  1878. }
  1879. p.children = chd;
  1880. p.children_d = dpc;
  1881. for(i = 0, j = p.parents.length; i < j; i++) {
  1882. m[p.parents[i]].children_d = m[p.parents[i]].children_d.concat(dpc);
  1883. }
  1884. rslt = {
  1885. 'cnt' : t_cnt,
  1886. 'mod' : m,
  1887. 'sel' : sel,
  1888. 'par' : par,
  1889. 'dpc' : dpc,
  1890. 'add' : add
  1891. };
  1892. }
  1893. if(typeof window === 'undefined' || typeof window.document === 'undefined') {
  1894. postMessage(rslt);
  1895. }
  1896. else {
  1897. return rslt;
  1898. }
  1899. },
  1900. rslt = function (rslt, worker) {
  1901. if(this.element === null) { return; }
  1902. this._cnt = rslt.cnt;
  1903. var i, m = this._model.data;
  1904. for (i in m) {
  1905. if (m.hasOwnProperty(i) && m[i].state && m[i].state.loading && rslt.mod[i]) {
  1906. rslt.mod[i].state.loading = true;
  1907. }
  1908. }
  1909. this._model.data = rslt.mod; // breaks the reference in load_node - careful
  1910. if(worker) {
  1911. var j, a = rslt.add, r = rslt.sel, s = this._data.core.selected.slice();
  1912. m = this._model.data;
  1913. // if selection was changed while calculating in worker
  1914. if(r.length !== s.length || $.vakata.array_unique(r.concat(s)).length !== r.length) {
  1915. // deselect nodes that are no longer selected
  1916. for(i = 0, j = r.length; i < j; i++) {
  1917. if($.inArray(r[i], a) === -1 && $.inArray(r[i], s) === -1) {
  1918. m[r[i]].state.selected = false;
  1919. }
  1920. }
  1921. // select nodes that were selected in the mean time
  1922. for(i = 0, j = s.length; i < j; i++) {
  1923. if($.inArray(s[i], r) === -1) {
  1924. m[s[i]].state.selected = true;
  1925. }
  1926. }
  1927. }
  1928. }
  1929. if(rslt.add.length) {
  1930. this._data.core.selected = this._data.core.selected.concat(rslt.add);
  1931. }
  1932. this.trigger('model', { "nodes" : rslt.dpc, 'parent' : rslt.par });
  1933. if(rslt.par !== $.jstree.root) {
  1934. this._node_changed(rslt.par);
  1935. this.redraw();
  1936. }
  1937. else {
  1938. // this.get_container_ul().children('.jstree-initial-node').remove();
  1939. this.redraw(true);
  1940. }
  1941. if(rslt.add.length) {
  1942. this.trigger('changed', { 'action' : 'model', 'selected' : this._data.core.selected });
  1943. }
  1944. // If no worker, try to mimic worker behavioour, by invoking cb asynchronously
  1945. if (!worker && setImmediate) {
  1946. setImmediate(function(){
  1947. cb.call(inst, true);
  1948. });
  1949. }
  1950. else {
  1951. cb.call(inst, true);
  1952. }
  1953. };
  1954. if(this.settings.core.worker && window.Blob && window.URL && window.Worker) {
  1955. try {
  1956. if(this._wrk === null) {
  1957. this._wrk = window.URL.createObjectURL(
  1958. new window.Blob(
  1959. ['self.onmessage = ' + func.toString()],
  1960. {type:"text/javascript"}
  1961. )
  1962. );
  1963. }
  1964. if(!this._data.core.working || force_processing) {
  1965. this._data.core.working = true;
  1966. w = new window.Worker(this._wrk);
  1967. w.onmessage = function (e) {
  1968. rslt.call(this, e.data, true);
  1969. try { w.terminate(); w = null; } catch(ignore) { }
  1970. if(this._data.core.worker_queue.length) {
  1971. this._append_json_data.apply(this, this._data.core.worker_queue.shift());
  1972. }
  1973. else {
  1974. this._data.core.working = false;
  1975. }
  1976. }.bind(this);
  1977. w.onerror = function (e) {
  1978. rslt.call(this, func(args), false);
  1979. if(this._data.core.worker_queue.length) {
  1980. this._append_json_data.apply(this, this._data.core.worker_queue.shift());
  1981. }
  1982. else {
  1983. this._data.core.working = false;
  1984. }
  1985. }.bind(this);
  1986. if(!args.par) {
  1987. if(this._data.core.worker_queue.length) {
  1988. this._append_json_data.apply(this, this._data.core.worker_queue.shift());
  1989. }
  1990. else {
  1991. this._data.core.working = false;
  1992. }
  1993. }
  1994. else {
  1995. w.postMessage(args);
  1996. }
  1997. }
  1998. else {
  1999. this._data.core.worker_queue.push([dom, data, cb, true]);
  2000. }
  2001. }
  2002. catch(e) {
  2003. rslt.call(this, func(args), false);
  2004. if(this._data.core.worker_queue.length) {
  2005. this._append_json_data.apply(this, this._data.core.worker_queue.shift());
  2006. }
  2007. else {
  2008. this._data.core.working = false;
  2009. }
  2010. }
  2011. }
  2012. else {
  2013. rslt.call(this, func(args), false);
  2014. }
  2015. },
  2016. /**
  2017. * parses a node from a jQuery object and appends them to the in memory tree model. Used internally.
  2018. * @private
  2019. * @name _parse_model_from_html(d [, p, ps])
  2020. * @param {jQuery} d the jQuery object to parse
  2021. * @param {String} p the parent ID
  2022. * @param {Array} ps list of all parents
  2023. * @return {String} the ID of the object added to the model
  2024. */
  2025. _parse_model_from_html : function (d, p, ps) {
  2026. if(!ps) { ps = []; }
  2027. else { ps = [].concat(ps); }
  2028. if(p) { ps.unshift(p); }
  2029. var c, e, m = this._model.data,
  2030. data = {
  2031. id : false,
  2032. text : false,
  2033. icon : true,
  2034. parent : p,
  2035. parents : ps,
  2036. children : [],
  2037. children_d : [],
  2038. data : null,
  2039. state : { },
  2040. li_attr : { id : false },
  2041. a_attr : { href : '#' },
  2042. original : false
  2043. }, i, tmp, tid;
  2044. for(i in this._model.default_state) {
  2045. if(this._model.default_state.hasOwnProperty(i)) {
  2046. data.state[i] = this._model.default_state[i];
  2047. }
  2048. }
  2049. tmp = $.vakata.attributes(d, true);
  2050. $.each(tmp, function (i, v) {
  2051. v = $.vakata.trim(v);
  2052. if(!v.length) { return true; }
  2053. data.li_attr[i] = v;
  2054. if(i === 'id') {
  2055. data.id = v.toString();
  2056. }
  2057. });
  2058. tmp = d.children('a').first();
  2059. if(tmp.length) {
  2060. tmp = $.vakata.attributes(tmp, true);
  2061. $.each(tmp, function (i, v) {
  2062. v = $.vakata.trim(v);
  2063. if(v.length) {
  2064. data.a_attr[i] = v;
  2065. }
  2066. });
  2067. }
  2068. tmp = d.children("a").first().length ? d.children("a").first().clone() : d.clone();
  2069. tmp.children("ins, i, ul").remove();
  2070. tmp = tmp.html();
  2071. tmp = $('<div></div>').html(tmp);
  2072. data.text = this.settings.core.force_text ? tmp.text() : tmp.html();
  2073. tmp = d.data();
  2074. data.data = tmp ? $.extend(true, {}, tmp) : null;
  2075. data.state.opened = d.hasClass('jstree-open');
  2076. data.state.selected = d.children('a').hasClass('jstree-clicked');
  2077. data.state.disabled = d.children('a').hasClass('jstree-disabled');
  2078. if(data.data && data.data.jstree) {
  2079. for(i in data.data.jstree) {
  2080. if(data.data.jstree.hasOwnProperty(i)) {
  2081. data.state[i] = data.data.jstree[i];
  2082. }
  2083. }
  2084. }
  2085. tmp = d.children("a").children(".jstree-themeicon");
  2086. if(tmp.length) {
  2087. data.icon = tmp.hasClass('jstree-themeicon-hidden') ? false : tmp.attr('rel');
  2088. }
  2089. if(data.state.icon !== undefined) {
  2090. data.icon = data.state.icon;
  2091. }
  2092. if(data.icon === undefined || data.icon === null || data.icon === "") {
  2093. data.icon = true;
  2094. }
  2095. tmp = d.children("ul").children("li");
  2096. do {
  2097. tid = 'j' + this._id + '_' + (++this._cnt);
  2098. } while(m[tid]);
  2099. data.id = data.li_attr.id ? data.li_attr.id.toString() : tid;
  2100. if(tmp.length) {
  2101. tmp.each(function (i, v) {
  2102. c = this._parse_model_from_html($(v), data.id, ps);
  2103. e = this._model.data[c];
  2104. data.children.push(c);
  2105. if(e.children_d.length) {
  2106. data.children_d = data.children_d.concat(e.children_d);
  2107. }
  2108. }.bind(this));
  2109. data.children_d = data.children_d.concat(data.children);
  2110. }
  2111. else {
  2112. if(d.hasClass('jstree-closed')) {
  2113. data.state.loaded = false;
  2114. }
  2115. }
  2116. if(data.li_attr['class']) {
  2117. data.li_attr['class'] = data.li_attr['class'].replace('jstree-closed','').replace('jstree-open','');
  2118. }
  2119. if(data.a_attr['class']) {
  2120. data.a_attr['class'] = data.a_attr['class'].replace('jstree-clicked','').replace('jstree-disabled','');
  2121. }
  2122. m[data.id] = data;
  2123. if(data.state.selected) {
  2124. this._data.core.selected.push(data.id);
  2125. }
  2126. return data.id;
  2127. },
  2128. /**
  2129. * parses a node from a JSON object (used when dealing with flat data, which has no nesting of children, but has id and parent properties) and appends it to the in memory tree model. Used internally.
  2130. * @private
  2131. * @name _parse_model_from_flat_json(d [, p, ps])
  2132. * @param {Object} d the JSON object to parse
  2133. * @param {String} p the parent ID
  2134. * @param {Array} ps list of all parents
  2135. * @return {String} the ID of the object added to the model
  2136. */
  2137. _parse_model_from_flat_json : function (d, p, ps) {
  2138. if(!ps) { ps = []; }
  2139. else { ps = ps.concat(); }
  2140. if(p) { ps.unshift(p); }
  2141. var tid = d.id.toString(),
  2142. m = this._model.data,
  2143. df = this._model.default_state,
  2144. i, j, c, e,
  2145. tmp = {
  2146. id : tid,
  2147. text : d.text || '',
  2148. icon : d.icon !== undefined ? d.icon : true,
  2149. parent : p,
  2150. parents : ps,
  2151. children : d.children || [],
  2152. children_d : d.children_d || [],
  2153. data : d.data,
  2154. state : { },
  2155. li_attr : { id : false },
  2156. a_attr : { href : '#' },
  2157. original : false
  2158. };
  2159. for(i in df) {
  2160. if(df.hasOwnProperty(i)) {
  2161. tmp.state[i] = df[i];
  2162. }
  2163. }
  2164. if(d && d.data && d.data.jstree && d.data.jstree.icon) {
  2165. tmp.icon = d.data.jstree.icon;
  2166. }
  2167. if(tmp.icon === undefined || tmp.icon === null || tmp.icon === "") {
  2168. tmp.icon = true;
  2169. }
  2170. if(d && d.data) {
  2171. tmp.data = d.data;
  2172. if(d.data.jstree) {
  2173. for(i in d.data.jstree) {
  2174. if(d.data.jstree.hasOwnProperty(i)) {
  2175. tmp.state[i] = d.data.jstree[i];
  2176. }
  2177. }
  2178. }
  2179. }
  2180. if(d && typeof d.state === 'object') {
  2181. for (i in d.state) {
  2182. if(d.state.hasOwnProperty(i)) {
  2183. tmp.state[i] = d.state[i];
  2184. }
  2185. }
  2186. }
  2187. if(d && typeof d.li_attr === 'object') {
  2188. for (i in d.li_attr) {
  2189. if(d.li_attr.hasOwnProperty(i)) {
  2190. tmp.li_attr[i] = d.li_attr[i];
  2191. }
  2192. }
  2193. }
  2194. if(!tmp.li_attr.id) {
  2195. tmp.li_attr.id = tid;
  2196. }
  2197. if(d && typeof d.a_attr === 'object') {
  2198. for (i in d.a_attr) {
  2199. if(d.a_attr.hasOwnProperty(i)) {
  2200. tmp.a_attr[i] = d.a_attr[i];
  2201. }
  2202. }
  2203. }
  2204. if(d && d.children && d.children === true) {
  2205. tmp.state.loaded = false;
  2206. tmp.children = [];
  2207. tmp.children_d = [];
  2208. }
  2209. m[tmp.id] = tmp;
  2210. for(i = 0, j = tmp.children.length; i < j; i++) {
  2211. c = this._parse_model_from_flat_json(m[tmp.children[i]], tmp.id, ps);
  2212. e = m[c];
  2213. tmp.children_d.push(c);
  2214. if(e.children_d.length) {
  2215. tmp.children_d = tmp.children_d.concat(e.children_d);
  2216. }
  2217. }
  2218. delete d.data;
  2219. delete d.children;
  2220. m[tmp.id].original = d;
  2221. if(tmp.state.selected) {
  2222. this._data.core.selected.push(tmp.id);
  2223. }
  2224. return tmp.id;
  2225. },
  2226. /**
  2227. * parses a node from a JSON object and appends it to the in memory tree model. Used internally.
  2228. * @private
  2229. * @name _parse_model_from_json(d [, p, ps])
  2230. * @param {Object} d the JSON object to parse
  2231. * @param {String} p the parent ID
  2232. * @param {Array} ps list of all parents
  2233. * @return {String} the ID of the object added to the model
  2234. */
  2235. _parse_model_from_json : function (d, p, ps) {
  2236. if(!ps) { ps = []; }
  2237. else { ps = ps.concat(); }
  2238. if(p) { ps.unshift(p); }
  2239. var tid = false, i, j, c, e, m = this._model.data, df = this._model.default_state, tmp;
  2240. do {
  2241. tid = 'j' + this._id + '_' + (++this._cnt);
  2242. } while(m[tid]);
  2243. tmp = {
  2244. id : false,
  2245. text : typeof d === 'string' ? d : '',
  2246. icon : typeof d === 'object' && d.icon !== undefined ? d.icon : true,
  2247. parent : p,
  2248. parents : ps,
  2249. children : [],
  2250. children_d : [],
  2251. data : null,
  2252. state : { },
  2253. li_attr : { id : false },
  2254. a_attr : { href : '#' },
  2255. original : false
  2256. };
  2257. for(i in df) {
  2258. if(df.hasOwnProperty(i)) {
  2259. tmp.state[i] = df[i];
  2260. }
  2261. }
  2262. if(d && (d.id || d.id === 0)) { tmp.id = d.id.toString(); }
  2263. if(d && d.text) { tmp.text = d.text; }
  2264. if(d && d.data && d.data.jstree && d.data.jstree.icon) {
  2265. tmp.icon = d.data.jstree.icon;
  2266. }
  2267. if(tmp.icon === undefined || tmp.icon === null || tmp.icon === "") {
  2268. tmp.icon = true;
  2269. }
  2270. if(d && d.data) {
  2271. tmp.data = d.data;
  2272. if(d.data.jstree) {
  2273. for(i in d.data.jstree) {
  2274. if(d.data.jstree.hasOwnProperty(i)) {
  2275. tmp.state[i] = d.data.jstree[i];
  2276. }
  2277. }
  2278. }
  2279. }
  2280. if(d && typeof d.state === 'object') {
  2281. for (i in d.state) {
  2282. if(d.state.hasOwnProperty(i)) {
  2283. tmp.state[i] = d.state[i];
  2284. }
  2285. }
  2286. }
  2287. if(d && typeof d.li_attr === 'object') {
  2288. for (i in d.li_attr) {
  2289. if(d.li_attr.hasOwnProperty(i)) {
  2290. tmp.li_attr[i] = d.li_attr[i];
  2291. }
  2292. }
  2293. }
  2294. if(tmp.li_attr.id && !(tmp.id || tmp.id === 0)) {
  2295. tmp.id = tmp.li_attr.id.toString();
  2296. }
  2297. if(!(tmp.id || tmp.id === 0)) {
  2298. tmp.id = tid;
  2299. }
  2300. if(!tmp.li_attr.id) {
  2301. tmp.li_attr.id = tmp.id;
  2302. }
  2303. if(d && typeof d.a_attr === 'object') {
  2304. for (i in d.a_attr) {
  2305. if(d.a_attr.hasOwnProperty(i)) {
  2306. tmp.a_attr[i] = d.a_attr[i];
  2307. }
  2308. }
  2309. }
  2310. if(d && d.children && d.children.length) {
  2311. for(i = 0, j = d.children.length; i < j; i++) {
  2312. c = this._parse_model_from_json(d.children[i], tmp.id, ps);
  2313. e = m[c];
  2314. tmp.children.push(c);
  2315. if(e.children_d.length) {
  2316. tmp.children_d = tmp.children_d.concat(e.children_d);
  2317. }
  2318. }
  2319. tmp.children_d = tmp.children.concat(tmp.children_d);
  2320. }
  2321. if(d && d.children && d.children === true) {
  2322. tmp.state.loaded = false;
  2323. tmp.children = [];
  2324. tmp.children_d = [];
  2325. }
  2326. delete d.data;
  2327. delete d.children;
  2328. tmp.original = d;
  2329. m[tmp.id] = tmp;
  2330. if(tmp.state.selected) {
  2331. this._data.core.selected.push(tmp.id);
  2332. }
  2333. return tmp.id;
  2334. },
  2335. /**
  2336. * redraws all nodes that need to be redrawn. Used internally.
  2337. * @private
  2338. * @name _redraw()
  2339. * @trigger redraw.jstree
  2340. */
  2341. _redraw : function () {
  2342. var nodes = this._model.force_full_redraw ? this._model.data[$.jstree.root].children.concat([]) : this._model.changed.concat([]),
  2343. f = document.createElement('UL'), tmp, i, j, fe = this._data.core.focused;
  2344. for(i = 0, j = nodes.length; i < j; i++) {
  2345. tmp = this.redraw_node(nodes[i], true, this._model.force_full_redraw);
  2346. if(tmp && this._model.force_full_redraw) {
  2347. f.appendChild(tmp);
  2348. }
  2349. }
  2350. if(this._model.force_full_redraw) {
  2351. f.className = this.get_container_ul()[0].className;
  2352. f.setAttribute('role','presentation');
  2353. this.element.empty().append(f);
  2354. //this.get_container_ul()[0].appendChild(f);
  2355. }
  2356. if(fe !== null && this.settings.core.restore_focus) {
  2357. tmp = this.get_node(fe, true);
  2358. if(tmp && tmp.length && tmp.children('.jstree-anchor')[0] !== document.activeElement) {
  2359. tmp.children('.jstree-anchor').trigger('focus');
  2360. }
  2361. else {
  2362. this._data.core.focused = null;
  2363. }
  2364. }
  2365. this._model.force_full_redraw = false;
  2366. this._model.changed = [];
  2367. /**
  2368. * triggered after nodes are redrawn
  2369. * @event
  2370. * @name redraw.jstree
  2371. * @param {array} nodes the redrawn nodes
  2372. */
  2373. this.trigger('redraw', { "nodes" : nodes });
  2374. },
  2375. /**
  2376. * redraws all nodes that need to be redrawn or optionally - the whole tree
  2377. * @name redraw([full])
  2378. * @param {Boolean} full if set to `true` all nodes are redrawn.
  2379. */
  2380. redraw : function (full) {
  2381. if(full) {
  2382. this._model.force_full_redraw = true;
  2383. }
  2384. //if(this._model.redraw_timeout) {
  2385. // clearTimeout(this._model.redraw_timeout);
  2386. //}
  2387. //this._model.redraw_timeout = setTimeout($.proxy(this._redraw, this),0);
  2388. this._redraw();
  2389. },
  2390. /**
  2391. * redraws a single node's children. Used internally.
  2392. * @private
  2393. * @name draw_children(node)
  2394. * @param {mixed} node the node whose children will be redrawn
  2395. */
  2396. draw_children : function (node) {
  2397. var obj = this.get_node(node),
  2398. i = false,
  2399. j = false,
  2400. k = false,
  2401. d = document;
  2402. if(!obj) { return false; }
  2403. if(obj.id === $.jstree.root) { return this.redraw(true); }
  2404. node = this.get_node(node, true);
  2405. if(!node || !node.length) { return false; } // TODO: quick toggle
  2406. node.children('.jstree-children').remove();
  2407. node = node[0];
  2408. if(obj.children.length && obj.state.loaded) {
  2409. k = d.createElement('UL');
  2410. k.setAttribute('role', 'group');
  2411. k.className = 'jstree-children';
  2412. for(i = 0, j = obj.children.length; i < j; i++) {
  2413. k.appendChild(this.redraw_node(obj.children[i], true, true));
  2414. }
  2415. node.appendChild(k);
  2416. }
  2417. },
  2418. /**
  2419. * redraws a single node. Used internally.
  2420. * @private
  2421. * @name redraw_node(node, deep, is_callback, force_render)
  2422. * @param {mixed} node the node to redraw
  2423. * @param {Boolean} deep should child nodes be redrawn too
  2424. * @param {Boolean} is_callback is this a recursion call
  2425. * @param {Boolean} force_render should children of closed parents be drawn anyway
  2426. */
  2427. redraw_node : function (node, deep, is_callback, force_render) {
  2428. var obj = this.get_node(node),
  2429. par = false,
  2430. ind = false,
  2431. old = false,
  2432. i = false,
  2433. j = false,
  2434. k = false,
  2435. c = '',
  2436. d = document,
  2437. m = this._model.data,
  2438. f = false,
  2439. s = false,
  2440. tmp = null,
  2441. t = 0,
  2442. l = 0,
  2443. has_children = false,
  2444. last_sibling = false;
  2445. if(!obj) { return false; }
  2446. if(obj.id === $.jstree.root) { return this.redraw(true); }
  2447. deep = deep || obj.children.length === 0;
  2448. node = !document.querySelector ? document.getElementById(obj.id) : this.element[0].querySelector('#' + ("0123456789".indexOf(obj.id[0]) !== -1 ? '\\3' + obj.id[0] + ' ' + obj.id.substr(1).replace($.jstree.idregex,'\\$&') : obj.id.replace($.jstree.idregex,'\\$&')) ); //, this.element);
  2449. if(!node) {
  2450. deep = true;
  2451. //node = d.createElement('LI');
  2452. if(!is_callback) {
  2453. par = obj.parent !== $.jstree.root ? $('#' + obj.parent.replace($.jstree.idregex,'\\$&'), this.element)[0] : null;
  2454. if(par !== null && (!par || !m[obj.parent].state.opened)) {
  2455. return false;
  2456. }
  2457. ind = $.inArray(obj.id, par === null ? m[$.jstree.root].children : m[obj.parent].children);
  2458. }
  2459. }
  2460. else {
  2461. node = $(node);
  2462. if(!is_callback) {
  2463. par = node.parent().parent()[0];
  2464. if(par === this.element[0]) {
  2465. par = null;
  2466. }
  2467. ind = node.index();
  2468. }
  2469. // m[obj.id].data = node.data(); // use only node's data, no need to touch jquery storage
  2470. if(!deep && obj.children.length && !node.children('.jstree-children').length) {
  2471. deep = true;
  2472. }
  2473. if(!deep) {
  2474. old = node.children('.jstree-children')[0];
  2475. }
  2476. f = node.children('.jstree-anchor')[0] === document.activeElement;
  2477. node.remove();
  2478. //node = d.createElement('LI');
  2479. //node = node[0];
  2480. }
  2481. node = this._data.core.node.cloneNode(true);
  2482. // node is DOM, deep is boolean
  2483. c = 'jstree-node ';
  2484. for(i in obj.li_attr) {
  2485. if(obj.li_attr.hasOwnProperty(i)) {
  2486. if(i === 'id') { continue; }
  2487. if(i !== 'class') {
  2488. node.setAttribute(i, obj.li_attr[i]);
  2489. }
  2490. else {
  2491. c += obj.li_attr[i];
  2492. }
  2493. }
  2494. }
  2495. if(!obj.a_attr.id) {
  2496. obj.a_attr.id = obj.id + '_anchor';
  2497. }
  2498. node.childNodes[1].setAttribute('aria-selected', !!obj.state.selected);
  2499. node.childNodes[1].setAttribute('aria-level', obj.parents.length);
  2500. if(this.settings.core.compute_elements_positions) {
  2501. node.childNodes[1].setAttribute('aria-setsize', m[obj.parent].children.length);
  2502. node.childNodes[1].setAttribute('aria-posinset', m[obj.parent].children.indexOf(obj.id) + 1);
  2503. }
  2504. if(obj.state.disabled) {
  2505. node.childNodes[1].setAttribute('aria-disabled', true);
  2506. }
  2507. for(i = 0, j = obj.children.length; i < j; i++) {
  2508. if(!m[obj.children[i]].state.hidden) {
  2509. has_children = true;
  2510. break;
  2511. }
  2512. }
  2513. if(obj.parent !== null && m[obj.parent] && !obj.state.hidden) {
  2514. i = $.inArray(obj.id, m[obj.parent].children);
  2515. last_sibling = obj.id;
  2516. if(i !== -1) {
  2517. i++;
  2518. for(j = m[obj.parent].children.length; i < j; i++) {
  2519. if(!m[m[obj.parent].children[i]].state.hidden) {
  2520. last_sibling = m[obj.parent].children[i];
  2521. }
  2522. if(last_sibling !== obj.id) {
  2523. break;
  2524. }
  2525. }
  2526. }
  2527. }
  2528. if(obj.state.hidden) {
  2529. c += ' jstree-hidden';
  2530. }
  2531. if (obj.state.loading) {
  2532. c += ' jstree-loading';
  2533. }
  2534. if(obj.state.loaded && !has_children) {
  2535. c += ' jstree-leaf';
  2536. }
  2537. else {
  2538. c += obj.state.opened && obj.state.loaded ? ' jstree-open' : ' jstree-closed';
  2539. node.childNodes[1].setAttribute('aria-expanded', (obj.state.opened && obj.state.loaded) );
  2540. }
  2541. if(last_sibling === obj.id) {
  2542. c += ' jstree-last';
  2543. }
  2544. node.id = obj.id;
  2545. node.className = c;
  2546. c = ( obj.state.selected ? ' jstree-clicked' : '') + ( obj.state.disabled ? ' jstree-disabled' : '');
  2547. for(j in obj.a_attr) {
  2548. if(obj.a_attr.hasOwnProperty(j)) {
  2549. if(j === 'href' && obj.a_attr[j] === '#') { continue; }
  2550. if(j !== 'class') {
  2551. node.childNodes[1].setAttribute(j, obj.a_attr[j]);
  2552. }
  2553. else {
  2554. c += ' ' + obj.a_attr[j];
  2555. }
  2556. }
  2557. }
  2558. if(c.length) {
  2559. node.childNodes[1].className = 'jstree-anchor ' + c;
  2560. }
  2561. if((obj.icon && obj.icon !== true) || obj.icon === false) {
  2562. if(obj.icon === false) {
  2563. node.childNodes[1].childNodes[0].className += ' jstree-themeicon-hidden';
  2564. }
  2565. else if(obj.icon.indexOf('/') === -1 && obj.icon.indexOf('.') === -1) {
  2566. node.childNodes[1].childNodes[0].className += ' ' + obj.icon + ' jstree-themeicon-custom';
  2567. }
  2568. else {
  2569. node.childNodes[1].childNodes[0].style.backgroundImage = 'url("'+obj.icon+'")';
  2570. node.childNodes[1].childNodes[0].style.backgroundPosition = 'center center';
  2571. node.childNodes[1].childNodes[0].style.backgroundSize = 'auto';
  2572. node.childNodes[1].childNodes[0].className += ' jstree-themeicon-custom';
  2573. }
  2574. }
  2575. if(this.settings.core.force_text) {
  2576. node.childNodes[1].appendChild(d.createTextNode(obj.text));
  2577. }
  2578. else {
  2579. node.childNodes[1].innerHTML += obj.text;
  2580. }
  2581. if(deep && obj.children.length && (obj.state.opened || force_render) && obj.state.loaded) {
  2582. k = d.createElement('UL');
  2583. k.setAttribute('role', 'group');
  2584. k.className = 'jstree-children';
  2585. for(i = 0, j = obj.children.length; i < j; i++) {
  2586. k.appendChild(this.redraw_node(obj.children[i], deep, true));
  2587. }
  2588. node.appendChild(k);
  2589. }
  2590. if(old) {
  2591. node.appendChild(old);
  2592. }
  2593. if(!is_callback) {
  2594. // append back using par / ind
  2595. if(!par) {
  2596. par = this.element[0];
  2597. }
  2598. for(i = 0, j = par.childNodes.length; i < j; i++) {
  2599. if(par.childNodes[i] && par.childNodes[i].className && par.childNodes[i].className.indexOf('jstree-children') !== -1) {
  2600. tmp = par.childNodes[i];
  2601. break;
  2602. }
  2603. }
  2604. if(!tmp) {
  2605. tmp = d.createElement('UL');
  2606. tmp.setAttribute('role', 'group');
  2607. tmp.className = 'jstree-children';
  2608. par.appendChild(tmp);
  2609. }
  2610. par = tmp;
  2611. if(ind < par.childNodes.length) {
  2612. par.insertBefore(node, par.childNodes[ind]);
  2613. }
  2614. else {
  2615. par.appendChild(node);
  2616. }
  2617. if(f) {
  2618. t = this.element[0].scrollTop;
  2619. l = this.element[0].scrollLeft;
  2620. node.childNodes[1].focus();
  2621. this.element[0].scrollTop = t;
  2622. this.element[0].scrollLeft = l;
  2623. }
  2624. }
  2625. if(obj.state.opened && !obj.state.loaded) {
  2626. obj.state.opened = false;
  2627. setTimeout(function () {
  2628. this.open_node(obj.id, false, 0);
  2629. }.bind(this), 0);
  2630. }
  2631. return node;
  2632. },
  2633. /**
  2634. * opens a node, revealing its children. If the node is not loaded it will be loaded and opened once ready.
  2635. * @name open_node(obj [, callback, animation])
  2636. * @param {mixed} obj the node to open
  2637. * @param {Function} callback a function to execute once the node is opened
  2638. * @param {Number} animation the animation duration in milliseconds when opening the node (overrides the `core.animation` setting). Use `false` for no animation.
  2639. * @trigger open_node.jstree, after_open.jstree, before_open.jstree
  2640. */
  2641. open_node : function (obj, callback, animation) {
  2642. var t1, t2, d, t;
  2643. if($.vakata.is_array(obj)) {
  2644. obj = obj.slice();
  2645. for(t1 = 0, t2 = obj.length; t1 < t2; t1++) {
  2646. this.open_node(obj[t1], callback, animation);
  2647. }
  2648. return true;
  2649. }
  2650. obj = this.get_node(obj);
  2651. if(!obj || obj.id === $.jstree.root) {
  2652. return false;
  2653. }
  2654. animation = animation === undefined ? this.settings.core.animation : animation;
  2655. if(!this.is_closed(obj)) {
  2656. if(callback) {
  2657. callback.call(this, obj, false);
  2658. }
  2659. return false;
  2660. }
  2661. if(!this.is_loaded(obj)) {
  2662. if(this.is_loading(obj)) {
  2663. return setTimeout(function () {
  2664. this.open_node(obj, callback, animation);
  2665. }.bind(this), 500);
  2666. }
  2667. this.load_node(obj, function (o, ok) {
  2668. return ok ? this.open_node(o, callback, animation) : (callback ? callback.call(this, o, false) : false);
  2669. });
  2670. }
  2671. else {
  2672. d = this.get_node(obj, true);
  2673. t = this;
  2674. if(d.length) {
  2675. if(animation && d.children(".jstree-children").length) {
  2676. d.children(".jstree-children").stop(true, true);
  2677. }
  2678. if(obj.children.length && !this._firstChild(d.children('.jstree-children')[0])) {
  2679. this.draw_children(obj);
  2680. //d = this.get_node(obj, true);
  2681. }
  2682. if(!animation) {
  2683. this.trigger('before_open', { "node" : obj });
  2684. d[0].className = d[0].className.replace('jstree-closed', 'jstree-open');
  2685. d[0].childNodes[1].setAttribute("aria-expanded", true);
  2686. }
  2687. else {
  2688. this.trigger('before_open', { "node" : obj });
  2689. d
  2690. .children(".jstree-children").css("display","none").end()
  2691. .removeClass("jstree-closed").addClass("jstree-open")
  2692. .children('.jstree-anchor').attr("aria-expanded", true).end()
  2693. .children(".jstree-children").stop(true, true)
  2694. .slideDown(animation, function () {
  2695. this.style.display = "";
  2696. if (t.element) {
  2697. t.trigger("after_open", { "node" : obj });
  2698. }
  2699. });
  2700. }
  2701. }
  2702. obj.state.opened = true;
  2703. if(callback) {
  2704. callback.call(this, obj, true);
  2705. }
  2706. if(!d.length) {
  2707. /**
  2708. * triggered when a node is about to be opened (if the node is supposed to be in the DOM, it will be, but it won't be visible yet)
  2709. * @event
  2710. * @name before_open.jstree
  2711. * @param {Object} node the opened node
  2712. */
  2713. this.trigger('before_open', { "node" : obj });
  2714. }
  2715. /**
  2716. * triggered when a node is opened (if there is an animation it will not be completed yet)
  2717. * @event
  2718. * @name open_node.jstree
  2719. * @param {Object} node the opened node
  2720. */
  2721. this.trigger('open_node', { "node" : obj });
  2722. if(!animation || !d.length) {
  2723. /**
  2724. * triggered when a node is opened and the animation is complete
  2725. * @event
  2726. * @name after_open.jstree
  2727. * @param {Object} node the opened node
  2728. */
  2729. this.trigger("after_open", { "node" : obj });
  2730. }
  2731. return true;
  2732. }
  2733. },
  2734. /**
  2735. * opens every parent of a node (node should be loaded)
  2736. * @name _open_to(obj)
  2737. * @param {mixed} obj the node to reveal
  2738. * @private
  2739. */
  2740. _open_to : function (obj) {
  2741. obj = this.get_node(obj);
  2742. if(!obj || obj.id === $.jstree.root) {
  2743. return false;
  2744. }
  2745. var i, j, p = obj.parents;
  2746. for(i = 0, j = p.length; i < j; i+=1) {
  2747. if(i !== $.jstree.root) {
  2748. this.open_node(p[i], false, 0);
  2749. }
  2750. }
  2751. return $('#' + obj.id.replace($.jstree.idregex,'\\$&'), this.element);
  2752. },
  2753. /**
  2754. * closes a node, hiding its children
  2755. * @name close_node(obj [, animation])
  2756. * @param {mixed} obj the node to close
  2757. * @param {Number} animation the animation duration in milliseconds when closing the node (overrides the `core.animation` setting). Use `false` for no animation.
  2758. * @trigger close_node.jstree, after_close.jstree
  2759. */
  2760. close_node : function (obj, animation) {
  2761. var t1, t2, t, d;
  2762. if($.vakata.is_array(obj)) {
  2763. obj = obj.slice();
  2764. for(t1 = 0, t2 = obj.length; t1 < t2; t1++) {
  2765. this.close_node(obj[t1], animation);
  2766. }
  2767. return true;
  2768. }
  2769. obj = this.get_node(obj);
  2770. if(!obj || obj.id === $.jstree.root) {
  2771. return false;
  2772. }
  2773. if(this.is_closed(obj)) {
  2774. return false;
  2775. }
  2776. animation = animation === undefined ? this.settings.core.animation : animation;
  2777. t = this;
  2778. d = this.get_node(obj, true);
  2779. obj.state.opened = false;
  2780. /**
  2781. * triggered when a node is closed (if there is an animation it will not be complete yet)
  2782. * @event
  2783. * @name close_node.jstree
  2784. * @param {Object} node the closed node
  2785. */
  2786. this.trigger('close_node',{ "node" : obj });
  2787. if(!d.length) {
  2788. /**
  2789. * triggered when a node is closed and the animation is complete
  2790. * @event
  2791. * @name after_close.jstree
  2792. * @param {Object} node the closed node
  2793. */
  2794. this.trigger("after_close", { "node" : obj });
  2795. }
  2796. else {
  2797. if(!animation) {
  2798. d[0].className = d[0].className.replace('jstree-open', 'jstree-closed');
  2799. d.children('.jstree-anchor').attr("aria-expanded", false);
  2800. d.children('.jstree-children').remove();
  2801. this.trigger("after_close", { "node" : obj });
  2802. }
  2803. else {
  2804. d
  2805. .children(".jstree-children").attr("style","display:block !important").end()
  2806. .removeClass("jstree-open").addClass("jstree-closed")
  2807. .children('.jstree-anchor').attr("aria-expanded", false).end()
  2808. .children(".jstree-children").stop(true, true).slideUp(animation, function () {
  2809. this.style.display = "";
  2810. d.children('.jstree-children').remove();
  2811. if (t.element) {
  2812. t.trigger("after_close", { "node" : obj });
  2813. }
  2814. });
  2815. }
  2816. }
  2817. },
  2818. /**
  2819. * toggles a node - closing it if it is open, opening it if it is closed
  2820. * @name toggle_node(obj)
  2821. * @param {mixed} obj the node to toggle
  2822. */
  2823. toggle_node : function (obj) {
  2824. var t1, t2;
  2825. if($.vakata.is_array(obj)) {
  2826. obj = obj.slice();
  2827. for(t1 = 0, t2 = obj.length; t1 < t2; t1++) {
  2828. this.toggle_node(obj[t1]);
  2829. }
  2830. return true;
  2831. }
  2832. if(this.is_closed(obj)) {
  2833. return this.open_node(obj);
  2834. }
  2835. if(this.is_open(obj)) {
  2836. return this.close_node(obj);
  2837. }
  2838. },
  2839. /**
  2840. * opens all nodes within a node (or the tree), revealing their children. If the node is not loaded it will be loaded and opened once ready.
  2841. * @name open_all([obj, animation, original_obj])
  2842. * @param {mixed} obj the node to open recursively, omit to open all nodes in the tree
  2843. * @param {Number} animation the animation duration in milliseconds when opening the nodes, the default is no animation
  2844. * @param {jQuery} reference to the node that started the process (internal use)
  2845. * @trigger open_all.jstree
  2846. */
  2847. open_all : function (obj, animation, original_obj) {
  2848. if(!obj) { obj = $.jstree.root; }
  2849. obj = this.get_node(obj);
  2850. if(!obj) { return false; }
  2851. var dom = obj.id === $.jstree.root ? this.get_container_ul() : this.get_node(obj, true), i, j, _this;
  2852. if(!dom.length) {
  2853. for(i = 0, j = obj.children_d.length; i < j; i++) {
  2854. if(this.is_closed(this._model.data[obj.children_d[i]])) {
  2855. this._model.data[obj.children_d[i]].state.opened = true;
  2856. }
  2857. }
  2858. return this.trigger('open_all', { "node" : obj });
  2859. }
  2860. original_obj = original_obj || dom;
  2861. _this = this;
  2862. dom = this.is_closed(obj) ? dom.find('.jstree-closed').addBack() : dom.find('.jstree-closed');
  2863. dom.each(function () {
  2864. _this.open_node(
  2865. this,
  2866. function(node, status) { if(status && this.is_parent(node)) { this.open_all(node, animation, original_obj); } },
  2867. animation || 0
  2868. );
  2869. });
  2870. if(original_obj.find('.jstree-closed').length === 0) {
  2871. /**
  2872. * triggered when an `open_all` call completes
  2873. * @event
  2874. * @name open_all.jstree
  2875. * @param {Object} node the opened node
  2876. */
  2877. this.trigger('open_all', { "node" : this.get_node(original_obj) });
  2878. }
  2879. },
  2880. /**
  2881. * closes all nodes within a node (or the tree), revealing their children
  2882. * @name close_all([obj, animation])
  2883. * @param {mixed} obj the node to close recursively, omit to close all nodes in the tree
  2884. * @param {Number} animation the animation duration in milliseconds when closing the nodes, the default is no animation
  2885. * @trigger close_all.jstree
  2886. */
  2887. close_all : function (obj, animation) {
  2888. if(!obj) { obj = $.jstree.root; }
  2889. obj = this.get_node(obj);
  2890. if(!obj) { return false; }
  2891. var dom = obj.id === $.jstree.root ? this.get_container_ul() : this.get_node(obj, true),
  2892. _this = this, i, j;
  2893. if(dom.length) {
  2894. dom = this.is_open(obj) ? dom.find('.jstree-open').addBack() : dom.find('.jstree-open');
  2895. $(dom.get().reverse()).each(function () { _this.close_node(this, animation || 0); });
  2896. }
  2897. for(i = 0, j = obj.children_d.length; i < j; i++) {
  2898. this._model.data[obj.children_d[i]].state.opened = false;
  2899. }
  2900. /**
  2901. * triggered when an `close_all` call completes
  2902. * @event
  2903. * @name close_all.jstree
  2904. * @param {Object} node the closed node
  2905. */
  2906. this.trigger('close_all', { "node" : obj });
  2907. },
  2908. /**
  2909. * checks if a node is disabled (not selectable)
  2910. * @name is_disabled(obj)
  2911. * @param {mixed} obj
  2912. * @return {Boolean}
  2913. */
  2914. is_disabled : function (obj) {
  2915. obj = this.get_node(obj);
  2916. return obj && obj.state && obj.state.disabled;
  2917. },
  2918. /**
  2919. * enables a node - so that it can be selected
  2920. * @name enable_node(obj)
  2921. * @param {mixed} obj the node to enable
  2922. * @trigger enable_node.jstree
  2923. */
  2924. enable_node : function (obj) {
  2925. var t1, t2;
  2926. if($.vakata.is_array(obj)) {
  2927. obj = obj.slice();
  2928. for(t1 = 0, t2 = obj.length; t1 < t2; t1++) {
  2929. this.enable_node(obj[t1]);
  2930. }
  2931. return true;
  2932. }
  2933. obj = this.get_node(obj);
  2934. if(!obj || obj.id === $.jstree.root) {
  2935. return false;
  2936. }
  2937. obj.state.disabled = false;
  2938. this.get_node(obj,true).children('.jstree-anchor').removeClass('jstree-disabled').attr('aria-disabled', false);
  2939. /**
  2940. * triggered when an node is enabled
  2941. * @event
  2942. * @name enable_node.jstree
  2943. * @param {Object} node the enabled node
  2944. */
  2945. this.trigger('enable_node', { 'node' : obj });
  2946. },
  2947. /**
  2948. * disables a node - so that it can not be selected
  2949. * @name disable_node(obj)
  2950. * @param {mixed} obj the node to disable
  2951. * @trigger disable_node.jstree
  2952. */
  2953. disable_node : function (obj) {
  2954. var t1, t2;
  2955. if($.vakata.is_array(obj)) {
  2956. obj = obj.slice();
  2957. for(t1 = 0, t2 = obj.length; t1 < t2; t1++) {
  2958. this.disable_node(obj[t1]);
  2959. }
  2960. return true;
  2961. }
  2962. obj = this.get_node(obj);
  2963. if(!obj || obj.id === $.jstree.root) {
  2964. return false;
  2965. }
  2966. obj.state.disabled = true;
  2967. this.get_node(obj,true).children('.jstree-anchor').addClass('jstree-disabled').attr('aria-disabled', true);
  2968. /**
  2969. * triggered when an node is disabled
  2970. * @event
  2971. * @name disable_node.jstree
  2972. * @param {Object} node the disabled node
  2973. */
  2974. this.trigger('disable_node', { 'node' : obj });
  2975. },
  2976. /**
  2977. * determines if a node is hidden
  2978. * @name is_hidden(obj)
  2979. * @param {mixed} obj the node
  2980. */
  2981. is_hidden : function (obj) {
  2982. obj = this.get_node(obj);
  2983. return obj.state.hidden === true;
  2984. },
  2985. /**
  2986. * hides a node - it is still in the structure but will not be visible
  2987. * @name hide_node(obj)
  2988. * @param {mixed} obj the node to hide
  2989. * @param {Boolean} skip_redraw internal parameter controlling if redraw is called
  2990. * @trigger hide_node.jstree
  2991. */
  2992. hide_node : function (obj, skip_redraw) {
  2993. var t1, t2;
  2994. if($.vakata.is_array(obj)) {
  2995. obj = obj.slice();
  2996. for(t1 = 0, t2 = obj.length; t1 < t2; t1++) {
  2997. this.hide_node(obj[t1], true);
  2998. }
  2999. if (!skip_redraw) {
  3000. this.redraw();
  3001. }
  3002. return true;
  3003. }
  3004. obj = this.get_node(obj);
  3005. if(!obj || obj.id === $.jstree.root) {
  3006. return false;
  3007. }
  3008. if(!obj.state.hidden) {
  3009. obj.state.hidden = true;
  3010. this._node_changed(obj.parent);
  3011. if(!skip_redraw) {
  3012. this.redraw();
  3013. }
  3014. /**
  3015. * triggered when an node is hidden
  3016. * @event
  3017. * @name hide_node.jstree
  3018. * @param {Object} node the hidden node
  3019. */
  3020. this.trigger('hide_node', { 'node' : obj });
  3021. }
  3022. },
  3023. /**
  3024. * shows a node
  3025. * @name show_node(obj)
  3026. * @param {mixed} obj the node to show
  3027. * @param {Boolean} skip_redraw internal parameter controlling if redraw is called
  3028. * @trigger show_node.jstree
  3029. */
  3030. show_node : function (obj, skip_redraw) {
  3031. var t1, t2;
  3032. if($.vakata.is_array(obj)) {
  3033. obj = obj.slice();
  3034. for(t1 = 0, t2 = obj.length; t1 < t2; t1++) {
  3035. this.show_node(obj[t1], true);
  3036. }
  3037. if (!skip_redraw) {
  3038. this.redraw();
  3039. }
  3040. return true;
  3041. }
  3042. obj = this.get_node(obj);
  3043. if(!obj || obj.id === $.jstree.root) {
  3044. return false;
  3045. }
  3046. if(obj.state.hidden) {
  3047. obj.state.hidden = false;
  3048. this._node_changed(obj.parent);
  3049. if(!skip_redraw) {
  3050. this.redraw();
  3051. }
  3052. /**
  3053. * triggered when an node is shown
  3054. * @event
  3055. * @name show_node.jstree
  3056. * @param {Object} node the shown node
  3057. */
  3058. this.trigger('show_node', { 'node' : obj });
  3059. }
  3060. },
  3061. /**
  3062. * hides all nodes
  3063. * @name hide_all()
  3064. * @trigger hide_all.jstree
  3065. */
  3066. hide_all : function (skip_redraw) {
  3067. var i, m = this._model.data, ids = [];
  3068. for(i in m) {
  3069. if(m.hasOwnProperty(i) && i !== $.jstree.root && !m[i].state.hidden) {
  3070. m[i].state.hidden = true;
  3071. ids.push(i);
  3072. }
  3073. }
  3074. this._model.force_full_redraw = true;
  3075. if(!skip_redraw) {
  3076. this.redraw();
  3077. }
  3078. /**
  3079. * triggered when all nodes are hidden
  3080. * @event
  3081. * @name hide_all.jstree
  3082. * @param {Array} nodes the IDs of all hidden nodes
  3083. */
  3084. this.trigger('hide_all', { 'nodes' : ids });
  3085. return ids;
  3086. },
  3087. /**
  3088. * shows all nodes
  3089. * @name show_all()
  3090. * @trigger show_all.jstree
  3091. */
  3092. show_all : function (skip_redraw) {
  3093. var i, m = this._model.data, ids = [];
  3094. for(i in m) {
  3095. if(m.hasOwnProperty(i) && i !== $.jstree.root && m[i].state.hidden) {
  3096. m[i].state.hidden = false;
  3097. ids.push(i);
  3098. }
  3099. }
  3100. this._model.force_full_redraw = true;
  3101. if(!skip_redraw) {
  3102. this.redraw();
  3103. }
  3104. /**
  3105. * triggered when all nodes are shown
  3106. * @event
  3107. * @name show_all.jstree
  3108. * @param {Array} nodes the IDs of all shown nodes
  3109. */
  3110. this.trigger('show_all', { 'nodes' : ids });
  3111. return ids;
  3112. },
  3113. /**
  3114. * called when a node is selected by the user. Used internally.
  3115. * @private
  3116. * @name activate_node(obj, e)
  3117. * @param {mixed} obj the node
  3118. * @param {Object} e the related event
  3119. * @trigger activate_node.jstree, changed.jstree
  3120. */
  3121. activate_node : function (obj, e) {
  3122. if(this.is_disabled(obj)) {
  3123. return false;
  3124. }
  3125. if(!e || typeof e !== 'object') {
  3126. e = {};
  3127. }
  3128. // ensure last_clicked is still in the DOM, make it fresh (maybe it was moved?) and make sure it is still selected, if not - make last_clicked the last selected node
  3129. this._data.core.last_clicked = this._data.core.last_clicked && this._data.core.last_clicked.id !== undefined ? this.get_node(this._data.core.last_clicked.id) : null;
  3130. if(this._data.core.last_clicked && !this._data.core.last_clicked.state.selected) { this._data.core.last_clicked = null; }
  3131. if(!this._data.core.last_clicked && this._data.core.selected.length) { this._data.core.last_clicked = this.get_node(this._data.core.selected[this._data.core.selected.length - 1]); }
  3132. if(!this.settings.core.multiple || (!e.metaKey && !e.ctrlKey && !e.shiftKey) || (e.shiftKey && (!this._data.core.last_clicked || !this.get_parent(obj) || this.get_parent(obj) !== this._data.core.last_clicked.parent ) )) {
  3133. if(!this.settings.core.multiple && (e.metaKey || e.ctrlKey || e.shiftKey) && this.is_selected(obj)) {
  3134. this.deselect_node(obj, false, e);
  3135. }
  3136. else {
  3137. if (!this.is_selected(obj) || this._data.core.selected.length !== 1) {
  3138. this.deselect_all(true);
  3139. this.select_node(obj, false, false, e);
  3140. }
  3141. this._data.core.last_clicked = this.get_node(obj);
  3142. }
  3143. }
  3144. else {
  3145. if(e.shiftKey) {
  3146. var o = this.get_node(obj).id,
  3147. l = this._data.core.last_clicked.id,
  3148. p = this.get_node(this._data.core.last_clicked.parent).children,
  3149. c = false,
  3150. i, j;
  3151. for(i = 0, j = p.length; i < j; i += 1) {
  3152. // separate IFs work whem o and l are the same
  3153. if(p[i] === o) {
  3154. c = !c;
  3155. }
  3156. if(p[i] === l) {
  3157. c = !c;
  3158. }
  3159. if(!this.is_disabled(p[i]) && (c || p[i] === o || p[i] === l)) {
  3160. if (!this.is_hidden(p[i])) {
  3161. this.select_node(p[i], true, false, e);
  3162. }
  3163. }
  3164. else {
  3165. if (!e.ctrlKey) {
  3166. this.deselect_node(p[i], true, e);
  3167. }
  3168. }
  3169. }
  3170. this.trigger('changed', { 'action' : 'select_node', 'node' : this.get_node(obj), 'selected' : this._data.core.selected, 'event' : e });
  3171. }
  3172. else {
  3173. if(!this.is_selected(obj)) {
  3174. if (e.ctrlKey) {
  3175. this._data.core.last_clicked = this.get_node(obj);
  3176. }
  3177. this.select_node(obj, false, false, e);
  3178. }
  3179. else {
  3180. this.deselect_node(obj, false, e);
  3181. }
  3182. }
  3183. }
  3184. /**
  3185. * triggered when an node is clicked or intercated with by the user
  3186. * @event
  3187. * @name activate_node.jstree
  3188. * @param {Object} node
  3189. * @param {Object} event the ooriginal event (if any) which triggered the call (may be an empty object)
  3190. */
  3191. this.trigger('activate_node', { 'node' : this.get_node(obj), 'event' : e });
  3192. },
  3193. /**
  3194. * applies the hover state on a node, called when a node is hovered by the user. Used internally.
  3195. * @private
  3196. * @name hover_node(obj)
  3197. * @param {mixed} obj
  3198. * @trigger hover_node.jstree
  3199. */
  3200. hover_node : function (obj) {
  3201. obj = this.get_node(obj, true);
  3202. if(!obj || !obj.length || obj.children('.jstree-hovered').length) {
  3203. return false;
  3204. }
  3205. var o = this.element.find('.jstree-hovered'), t = this.element;
  3206. if(o && o.length) { this.dehover_node(o); }
  3207. obj.children('.jstree-anchor').addClass('jstree-hovered');
  3208. /**
  3209. * triggered when an node is hovered
  3210. * @event
  3211. * @name hover_node.jstree
  3212. * @param {Object} node
  3213. */
  3214. this.trigger('hover_node', { 'node' : this.get_node(obj) });
  3215. setTimeout(function () { t.attr('aria-activedescendant', obj[0].id); }, 0);
  3216. },
  3217. /**
  3218. * removes the hover state from a nodecalled when a node is no longer hovered by the user. Used internally.
  3219. * @private
  3220. * @name dehover_node(obj)
  3221. * @param {mixed} obj
  3222. * @trigger dehover_node.jstree
  3223. */
  3224. dehover_node : function (obj) {
  3225. obj = this.get_node(obj, true);
  3226. if(!obj || !obj.length || !obj.children('.jstree-hovered').length) {
  3227. return false;
  3228. }
  3229. obj.children('.jstree-anchor').removeClass('jstree-hovered');
  3230. /**
  3231. * triggered when an node is no longer hovered
  3232. * @event
  3233. * @name dehover_node.jstree
  3234. * @param {Object} node
  3235. */
  3236. this.trigger('dehover_node', { 'node' : this.get_node(obj) });
  3237. },
  3238. /**
  3239. * select a node
  3240. * @name select_node(obj [, supress_event, prevent_open])
  3241. * @param {mixed} obj an array can be used to select multiple nodes
  3242. * @param {Boolean} supress_event if set to `true` the `changed.jstree` event won't be triggered
  3243. * @param {Boolean} prevent_open if set to `true` parents of the selected node won't be opened
  3244. * @trigger select_node.jstree, changed.jstree
  3245. */
  3246. select_node : function (obj, supress_event, prevent_open, e) {
  3247. var dom, t1, t2, th;
  3248. if($.vakata.is_array(obj)) {
  3249. obj = obj.slice();
  3250. for(t1 = 0, t2 = obj.length; t1 < t2; t1++) {
  3251. this.select_node(obj[t1], supress_event, prevent_open, e);
  3252. }
  3253. return true;
  3254. }
  3255. obj = this.get_node(obj);
  3256. if(!obj || obj.id === $.jstree.root) {
  3257. return false;
  3258. }
  3259. dom = this.get_node(obj, true);
  3260. if(!obj.state.selected) {
  3261. obj.state.selected = true;
  3262. this._data.core.selected.push(obj.id);
  3263. if(!prevent_open) {
  3264. dom = this._open_to(obj);
  3265. }
  3266. if(dom && dom.length) {
  3267. dom.children('.jstree-anchor').addClass('jstree-clicked').attr('aria-selected', true);
  3268. }
  3269. /**
  3270. * triggered when an node is selected
  3271. * @event
  3272. * @name select_node.jstree
  3273. * @param {Object} node
  3274. * @param {Array} selected the current selection
  3275. * @param {Object} event the event (if any) that triggered this select_node
  3276. */
  3277. this.trigger('select_node', { 'node' : obj, 'selected' : this._data.core.selected, 'event' : e });
  3278. if(!supress_event) {
  3279. /**
  3280. * triggered when selection changes
  3281. * @event
  3282. * @name changed.jstree
  3283. * @param {Object} node
  3284. * @param {Object} action the action that caused the selection to change
  3285. * @param {Array} selected the current selection
  3286. * @param {Object} event the event (if any) that triggered this changed event
  3287. */
  3288. this.trigger('changed', { 'action' : 'select_node', 'node' : obj, 'selected' : this._data.core.selected, 'event' : e });
  3289. }
  3290. }
  3291. },
  3292. /**
  3293. * deselect a node
  3294. * @name deselect_node(obj [, supress_event])
  3295. * @param {mixed} obj an array can be used to deselect multiple nodes
  3296. * @param {Boolean} supress_event if set to `true` the `changed.jstree` event won't be triggered
  3297. * @trigger deselect_node.jstree, changed.jstree
  3298. */
  3299. deselect_node : function (obj, supress_event, e) {
  3300. var t1, t2, dom;
  3301. if($.vakata.is_array(obj)) {
  3302. obj = obj.slice();
  3303. for(t1 = 0, t2 = obj.length; t1 < t2; t1++) {
  3304. this.deselect_node(obj[t1], supress_event, e);
  3305. }
  3306. return true;
  3307. }
  3308. obj = this.get_node(obj);
  3309. if(!obj || obj.id === $.jstree.root) {
  3310. return false;
  3311. }
  3312. dom = this.get_node(obj, true);
  3313. if(obj.state.selected) {
  3314. obj.state.selected = false;
  3315. this._data.core.selected = $.vakata.array_remove_item(this._data.core.selected, obj.id);
  3316. if(dom.length) {
  3317. dom.children('.jstree-anchor').removeClass('jstree-clicked').attr('aria-selected', false);
  3318. }
  3319. /**
  3320. * triggered when an node is deselected
  3321. * @event
  3322. * @name deselect_node.jstree
  3323. * @param {Object} node
  3324. * @param {Array} selected the current selection
  3325. * @param {Object} event the event (if any) that triggered this deselect_node
  3326. */
  3327. this.trigger('deselect_node', { 'node' : obj, 'selected' : this._data.core.selected, 'event' : e });
  3328. if(!supress_event) {
  3329. this.trigger('changed', { 'action' : 'deselect_node', 'node' : obj, 'selected' : this._data.core.selected, 'event' : e });
  3330. }
  3331. }
  3332. },
  3333. /**
  3334. * select all nodes in the tree
  3335. * @name select_all([supress_event])
  3336. * @param {Boolean} supress_event if set to `true` the `changed.jstree` event won't be triggered
  3337. * @trigger select_all.jstree, changed.jstree
  3338. */
  3339. select_all : function (supress_event) {
  3340. var tmp = this._data.core.selected.concat([]), i, j;
  3341. this._data.core.selected = this._model.data[$.jstree.root].children_d.concat();
  3342. for(i = 0, j = this._data.core.selected.length; i < j; i++) {
  3343. if(this._model.data[this._data.core.selected[i]]) {
  3344. this._model.data[this._data.core.selected[i]].state.selected = true;
  3345. }
  3346. }
  3347. this.redraw(true);
  3348. /**
  3349. * triggered when all nodes are selected
  3350. * @event
  3351. * @name select_all.jstree
  3352. * @param {Array} selected the current selection
  3353. */
  3354. this.trigger('select_all', { 'selected' : this._data.core.selected });
  3355. if(!supress_event) {
  3356. this.trigger('changed', { 'action' : 'select_all', 'selected' : this._data.core.selected, 'old_selection' : tmp });
  3357. }
  3358. },
  3359. /**
  3360. * deselect all selected nodes
  3361. * @name deselect_all([supress_event])
  3362. * @param {Boolean} supress_event if set to `true` the `changed.jstree` event won't be triggered
  3363. * @trigger deselect_all.jstree, changed.jstree
  3364. */
  3365. deselect_all : function (supress_event) {
  3366. var tmp = this._data.core.selected.concat([]), i, j;
  3367. for(i = 0, j = this._data.core.selected.length; i < j; i++) {
  3368. if(this._model.data[this._data.core.selected[i]]) {
  3369. this._model.data[this._data.core.selected[i]].state.selected = false;
  3370. }
  3371. }
  3372. this._data.core.selected = [];
  3373. this.element.find('.jstree-clicked').removeClass('jstree-clicked').attr('aria-selected', false);
  3374. /**
  3375. * triggered when all nodes are deselected
  3376. * @event
  3377. * @name deselect_all.jstree
  3378. * @param {Object} node the previous selection
  3379. * @param {Array} selected the current selection
  3380. */
  3381. this.trigger('deselect_all', { 'selected' : this._data.core.selected, 'node' : tmp });
  3382. if(!supress_event) {
  3383. this.trigger('changed', { 'action' : 'deselect_all', 'selected' : this._data.core.selected, 'old_selection' : tmp });
  3384. }
  3385. },
  3386. /**
  3387. * checks if a node is selected
  3388. * @name is_selected(obj)
  3389. * @param {mixed} obj
  3390. * @return {Boolean}
  3391. */
  3392. is_selected : function (obj) {
  3393. obj = this.get_node(obj);
  3394. if(!obj || obj.id === $.jstree.root) {
  3395. return false;
  3396. }
  3397. return obj.state.selected;
  3398. },
  3399. /**
  3400. * get an array of all selected nodes
  3401. * @name get_selected([full])
  3402. * @param {mixed} full if set to `true` the returned array will consist of the full node objects, otherwise - only IDs will be returned
  3403. * @return {Array}
  3404. */
  3405. get_selected : function (full) {
  3406. return full ? $.map(this._data.core.selected, function (i) { return this.get_node(i); }.bind(this)) : this._data.core.selected.slice();
  3407. },
  3408. /**
  3409. * get an array of all top level selected nodes (ignoring children of selected nodes)
  3410. * @name get_top_selected([full])
  3411. * @param {mixed} full if set to `true` the returned array will consist of the full node objects, otherwise - only IDs will be returned
  3412. * @return {Array}
  3413. */
  3414. get_top_selected : function (full) {
  3415. var tmp = this.get_selected(true),
  3416. obj = {}, i, j, k, l;
  3417. for(i = 0, j = tmp.length; i < j; i++) {
  3418. obj[tmp[i].id] = tmp[i];
  3419. }
  3420. for(i = 0, j = tmp.length; i < j; i++) {
  3421. for(k = 0, l = tmp[i].children_d.length; k < l; k++) {
  3422. if(obj[tmp[i].children_d[k]]) {
  3423. delete obj[tmp[i].children_d[k]];
  3424. }
  3425. }
  3426. }
  3427. tmp = [];
  3428. for(i in obj) {
  3429. if(obj.hasOwnProperty(i)) {
  3430. tmp.push(i);
  3431. }
  3432. }
  3433. return full ? $.map(tmp, function (i) { return this.get_node(i); }.bind(this)) : tmp;
  3434. },
  3435. /**
  3436. * get an array of all bottom level selected nodes (ignoring selected parents)
  3437. * @name get_bottom_selected([full])
  3438. * @param {mixed} full if set to `true` the returned array will consist of the full node objects, otherwise - only IDs will be returned
  3439. * @return {Array}
  3440. */
  3441. get_bottom_selected : function (full) {
  3442. var tmp = this.get_selected(true),
  3443. obj = [], i, j;
  3444. for(i = 0, j = tmp.length; i < j; i++) {
  3445. if(!tmp[i].children.length) {
  3446. obj.push(tmp[i].id);
  3447. }
  3448. }
  3449. return full ? $.map(obj, function (i) { return this.get_node(i); }.bind(this)) : obj;
  3450. },
  3451. /**
  3452. * gets the current state of the tree so that it can be restored later with `set_state(state)`. Used internally.
  3453. * @name get_state()
  3454. * @private
  3455. * @return {Object}
  3456. */
  3457. get_state : function () {
  3458. var state = {
  3459. 'core' : {
  3460. 'open' : [],
  3461. 'loaded' : [],
  3462. 'scroll' : {
  3463. 'left' : this.element.scrollLeft(),
  3464. 'top' : this.element.scrollTop()
  3465. },
  3466. /*!
  3467. 'themes' : {
  3468. 'name' : this.get_theme(),
  3469. 'icons' : this._data.core.themes.icons,
  3470. 'dots' : this._data.core.themes.dots
  3471. },
  3472. */
  3473. 'selected' : []
  3474. }
  3475. }, i;
  3476. for(i in this._model.data) {
  3477. if(this._model.data.hasOwnProperty(i)) {
  3478. if(i !== $.jstree.root) {
  3479. if(this._model.data[i].state.loaded && this.settings.core.loaded_state) {
  3480. state.core.loaded.push(i);
  3481. }
  3482. if(this._model.data[i].state.opened) {
  3483. state.core.open.push(i);
  3484. }
  3485. if(this._model.data[i].state.selected) {
  3486. state.core.selected.push(i);
  3487. }
  3488. }
  3489. }
  3490. }
  3491. return state;
  3492. },
  3493. /**
  3494. * sets the state of the tree. Used internally.
  3495. * @name set_state(state [, callback])
  3496. * @private
  3497. * @param {Object} state the state to restore. Keep in mind this object is passed by reference and jstree will modify it.
  3498. * @param {Function} callback an optional function to execute once the state is restored.
  3499. * @trigger set_state.jstree
  3500. */
  3501. set_state : function (state, callback) {
  3502. if(state) {
  3503. if(state.core && state.core.selected && state.core.initial_selection === undefined) {
  3504. state.core.initial_selection = this._data.core.selected.concat([]).sort().join(',');
  3505. }
  3506. if(state.core) {
  3507. var res, n, t, _this, i;
  3508. if(state.core.loaded) {
  3509. if(!this.settings.core.loaded_state || !$.vakata.is_array(state.core.loaded) || !state.core.loaded.length) {
  3510. delete state.core.loaded;
  3511. this.set_state(state, callback);
  3512. }
  3513. else {
  3514. this._load_nodes(state.core.loaded, function (nodes) {
  3515. delete state.core.loaded;
  3516. this.set_state(state, callback);
  3517. });
  3518. }
  3519. return false;
  3520. }
  3521. if(state.core.open) {
  3522. if(!$.vakata.is_array(state.core.open) || !state.core.open.length) {
  3523. delete state.core.open;
  3524. this.set_state(state, callback);
  3525. }
  3526. else {
  3527. this._load_nodes(state.core.open, function (nodes) {
  3528. this.open_node(nodes, false, 0);
  3529. delete state.core.open;
  3530. this.set_state(state, callback);
  3531. });
  3532. }
  3533. return false;
  3534. }
  3535. if(state.core.scroll) {
  3536. if(state.core.scroll && state.core.scroll.left !== undefined) {
  3537. this.element.scrollLeft(state.core.scroll.left);
  3538. }
  3539. if(state.core.scroll && state.core.scroll.top !== undefined) {
  3540. this.element.scrollTop(state.core.scroll.top);
  3541. }
  3542. delete state.core.scroll;
  3543. this.set_state(state, callback);
  3544. return false;
  3545. }
  3546. if(state.core.selected) {
  3547. _this = this;
  3548. if (state.core.initial_selection === undefined ||
  3549. state.core.initial_selection === this._data.core.selected.concat([]).sort().join(',')
  3550. ) {
  3551. this.deselect_all();
  3552. $.each(state.core.selected, function (i, v) {
  3553. _this.select_node(v, false, true);
  3554. });
  3555. }
  3556. delete state.core.initial_selection;
  3557. delete state.core.selected;
  3558. this.set_state(state, callback);
  3559. return false;
  3560. }
  3561. for(i in state) {
  3562. if(state.hasOwnProperty(i) && i !== "core" && $.inArray(i, this.settings.plugins) === -1) {
  3563. delete state[i];
  3564. }
  3565. }
  3566. if($.isEmptyObject(state.core)) {
  3567. delete state.core;
  3568. this.set_state(state, callback);
  3569. return false;
  3570. }
  3571. }
  3572. if($.isEmptyObject(state)) {
  3573. state = null;
  3574. if(callback) { callback.call(this); }
  3575. /**
  3576. * triggered when a `set_state` call completes
  3577. * @event
  3578. * @name set_state.jstree
  3579. */
  3580. this.trigger('set_state');
  3581. return false;
  3582. }
  3583. return true;
  3584. }
  3585. return false;
  3586. },
  3587. /**
  3588. * refreshes the tree - all nodes are reloaded with calls to `load_node`.
  3589. * @name refresh()
  3590. * @param {Boolean} skip_loading an option to skip showing the loading indicator
  3591. * @param {Mixed} forget_state if set to `true` state will not be reapplied, if set to a function (receiving the current state as argument) the result of that function will be used as state
  3592. * @trigger refresh.jstree
  3593. */
  3594. refresh : function (skip_loading, forget_state) {
  3595. this._data.core.state = forget_state === true ? {} : this.get_state();
  3596. if(forget_state && $.vakata.is_function(forget_state)) { this._data.core.state = forget_state.call(this, this._data.core.state); }
  3597. this._cnt = 0;
  3598. this._model.data = {};
  3599. this._model.data[$.jstree.root] = {
  3600. id : $.jstree.root,
  3601. parent : null,
  3602. parents : [],
  3603. children : [],
  3604. children_d : [],
  3605. state : { loaded : false }
  3606. };
  3607. this._data.core.selected = [];
  3608. this._data.core.last_clicked = null;
  3609. this._data.core.focused = null;
  3610. var c = this.get_container_ul()[0].className;
  3611. if(!skip_loading) {
  3612. this.element.html("<"+"ul class='"+c+"' role='group'><"+"li class='jstree-initial-node jstree-loading jstree-leaf jstree-last' role='none' id='j"+this._id+"_loading'><i class='jstree-icon jstree-ocl'></i><"+"a class='jstree-anchor' role='treeitem' href='#'><i class='jstree-icon jstree-themeicon-hidden'></i>" + this.get_string("Loading ...") + "</a></li></ul>");
  3613. this.element.attr('aria-activedescendant','j'+this._id+'_loading');
  3614. }
  3615. this.load_node($.jstree.root, function (o, s) {
  3616. if(s) {
  3617. this.get_container_ul()[0].className = c;
  3618. if(this._firstChild(this.get_container_ul()[0])) {
  3619. this.element.attr('aria-activedescendant',this._firstChild(this.get_container_ul()[0]).id);
  3620. }
  3621. this.set_state($.extend(true, {}, this._data.core.state), function () {
  3622. /**
  3623. * triggered when a `refresh` call completes
  3624. * @event
  3625. * @name refresh.jstree
  3626. */
  3627. this.trigger('refresh');
  3628. });
  3629. }
  3630. this._data.core.state = null;
  3631. });
  3632. },
  3633. /**
  3634. * refreshes a node in the tree (reload its children) all opened nodes inside that node are reloaded with calls to `load_node`.
  3635. * @name refresh_node(obj)
  3636. * @param {mixed} obj the node
  3637. * @trigger refresh_node.jstree
  3638. */
  3639. refresh_node : function (obj) {
  3640. obj = this.get_node(obj);
  3641. if(!obj || obj.id === $.jstree.root) { return false; }
  3642. var opened = [], to_load = [], s = this._data.core.selected.concat([]);
  3643. to_load.push(obj.id);
  3644. if(obj.state.opened === true) { opened.push(obj.id); }
  3645. this.get_node(obj, true).find('.jstree-open').each(function() { to_load.push(this.id); opened.push(this.id); });
  3646. this._load_nodes(to_load, function (nodes) {
  3647. this.open_node(opened, false, 0);
  3648. this.select_node(s);
  3649. /**
  3650. * triggered when a node is refreshed
  3651. * @event
  3652. * @name refresh_node.jstree
  3653. * @param {Object} node - the refreshed node
  3654. * @param {Array} nodes - an array of the IDs of the nodes that were reloaded
  3655. */
  3656. this.trigger('refresh_node', { 'node' : obj, 'nodes' : nodes });
  3657. }.bind(this), false, true);
  3658. },
  3659. /**
  3660. * set (change) the ID of a node
  3661. * @name set_id(obj, id)
  3662. * @param {mixed} obj the node
  3663. * @param {String} id the new ID
  3664. * @return {Boolean}
  3665. * @trigger set_id.jstree
  3666. */
  3667. set_id : function (obj, id) {
  3668. obj = this.get_node(obj);
  3669. if(!obj || obj.id === $.jstree.root) { return false; }
  3670. var i, j, m = this._model.data, old = obj.id;
  3671. id = id.toString();
  3672. // update parents (replace current ID with new one in children and children_d)
  3673. m[obj.parent].children[$.inArray(obj.id, m[obj.parent].children)] = id;
  3674. for(i = 0, j = obj.parents.length; i < j; i++) {
  3675. m[obj.parents[i]].children_d[$.inArray(obj.id, m[obj.parents[i]].children_d)] = id;
  3676. }
  3677. // update children (replace current ID with new one in parent and parents)
  3678. for(i = 0, j = obj.children.length; i < j; i++) {
  3679. m[obj.children[i]].parent = id;
  3680. }
  3681. for(i = 0, j = obj.children_d.length; i < j; i++) {
  3682. m[obj.children_d[i]].parents[$.inArray(obj.id, m[obj.children_d[i]].parents)] = id;
  3683. }
  3684. i = $.inArray(obj.id, this._data.core.selected);
  3685. if(i !== -1) { this._data.core.selected[i] = id; }
  3686. // update model and obj itself (obj.id, this._model.data[KEY])
  3687. i = this.get_node(obj.id, true);
  3688. if(i) {
  3689. i.attr('id', id); //.children('.jstree-anchor').attr('id', id + '_anchor').end().attr('aria-labelledby', id + '_anchor');
  3690. if(this.element.attr('aria-activedescendant') === obj.id) {
  3691. this.element.attr('aria-activedescendant', id);
  3692. }
  3693. }
  3694. delete m[obj.id];
  3695. obj.id = id;
  3696. obj.li_attr.id = id;
  3697. m[id] = obj;
  3698. /**
  3699. * triggered when a node id value is changed
  3700. * @event
  3701. * @name set_id.jstree
  3702. * @param {Object} node
  3703. * @param {String} old the old id
  3704. */
  3705. this.trigger('set_id',{ "node" : obj, "new" : obj.id, "old" : old });
  3706. return true;
  3707. },
  3708. /**
  3709. * get the text value of a node
  3710. * @name get_text(obj)
  3711. * @param {mixed} obj the node
  3712. * @return {String}
  3713. */
  3714. get_text : function (obj) {
  3715. obj = this.get_node(obj);
  3716. return (!obj || obj.id === $.jstree.root) ? false : obj.text;
  3717. },
  3718. /**
  3719. * set the text value of a node. Used internally, please use `rename_node(obj, val)`.
  3720. * @private
  3721. * @name set_text(obj, val)
  3722. * @param {mixed} obj the node, you can pass an array to set the text on multiple nodes
  3723. * @param {String} val the new text value
  3724. * @return {Boolean}
  3725. * @trigger set_text.jstree
  3726. */
  3727. set_text : function (obj, val) {
  3728. var t1, t2;
  3729. if($.vakata.is_array(obj)) {
  3730. obj = obj.slice();
  3731. for(t1 = 0, t2 = obj.length; t1 < t2; t1++) {
  3732. this.set_text(obj[t1], val);
  3733. }
  3734. return true;
  3735. }
  3736. obj = this.get_node(obj);
  3737. if(!obj || obj.id === $.jstree.root) { return false; }
  3738. obj.text = val;
  3739. if(this.get_node(obj, true).length) {
  3740. this.redraw_node(obj.id);
  3741. }
  3742. /**
  3743. * triggered when a node text value is changed
  3744. * @event
  3745. * @name set_text.jstree
  3746. * @param {Object} obj
  3747. * @param {String} text the new value
  3748. */
  3749. this.trigger('set_text',{ "obj" : obj, "text" : val });
  3750. return true;
  3751. },
  3752. /**
  3753. * gets a JSON representation of a node (or the whole tree)
  3754. * @name get_json([obj, options])
  3755. * @param {mixed} obj
  3756. * @param {Object} options
  3757. * @param {Boolean} options.no_state do not return state information
  3758. * @param {Boolean} options.no_id do not return ID
  3759. * @param {Boolean} options.no_children do not include children
  3760. * @param {Boolean} options.no_data do not include node data
  3761. * @param {Boolean} options.no_li_attr do not include LI attributes
  3762. * @param {Boolean} options.no_a_attr do not include A attributes
  3763. * @param {Boolean} options.flat return flat JSON instead of nested
  3764. * @return {Object}
  3765. */
  3766. get_json : function (obj, options, flat) {
  3767. obj = this.get_node(obj || $.jstree.root);
  3768. if(!obj) { return false; }
  3769. if(options && options.flat && !flat) { flat = []; }
  3770. var tmp = {
  3771. 'id' : obj.id,
  3772. 'text' : obj.text,
  3773. 'icon' : this.get_icon(obj),
  3774. 'li_attr' : $.extend(true, {}, obj.li_attr),
  3775. 'a_attr' : $.extend(true, {}, obj.a_attr),
  3776. 'state' : {},
  3777. 'data' : options && options.no_data ? false : $.extend(true, $.vakata.is_array(obj.data)?[]:{}, obj.data)
  3778. //( this.get_node(obj, true).length ? this.get_node(obj, true).data() : obj.data ),
  3779. }, i, j;
  3780. if(options && options.flat) {
  3781. tmp.parent = obj.parent;
  3782. }
  3783. else {
  3784. tmp.children = [];
  3785. }
  3786. if(!options || !options.no_state) {
  3787. for(i in obj.state) {
  3788. if(obj.state.hasOwnProperty(i)) {
  3789. tmp.state[i] = obj.state[i];
  3790. }
  3791. }
  3792. } else {
  3793. delete tmp.state;
  3794. }
  3795. if(options && options.no_li_attr) {
  3796. delete tmp.li_attr;
  3797. }
  3798. if(options && options.no_a_attr) {
  3799. delete tmp.a_attr;
  3800. }
  3801. if(options && options.no_id) {
  3802. delete tmp.id;
  3803. if(tmp.li_attr && tmp.li_attr.id) {
  3804. delete tmp.li_attr.id;
  3805. }
  3806. if(tmp.a_attr && tmp.a_attr.id) {
  3807. delete tmp.a_attr.id;
  3808. }
  3809. }
  3810. if(options && options.flat && obj.id !== $.jstree.root) {
  3811. flat.push(tmp);
  3812. }
  3813. if(!options || !options.no_children) {
  3814. for(i = 0, j = obj.children.length; i < j; i++) {
  3815. if(options && options.flat) {
  3816. this.get_json(obj.children[i], options, flat);
  3817. }
  3818. else {
  3819. tmp.children.push(this.get_json(obj.children[i], options));
  3820. }
  3821. }
  3822. }
  3823. return options && options.flat ? flat : (obj.id === $.jstree.root ? tmp.children : tmp);
  3824. },
  3825. /**
  3826. * create a new node (do not confuse with load_node)
  3827. * @name create_node([par, node, pos, callback, is_loaded])
  3828. * @param {mixed} par the parent node (to create a root node use either "#" (string) or `null`)
  3829. * @param {mixed} node the data for the new node (a valid JSON object, or a simple string with the name)
  3830. * @param {mixed} pos the index at which to insert the node, "first" and "last" are also supported, default is "last"
  3831. * @param {Function} callback a function to be called once the node is created
  3832. * @param {Boolean} is_loaded internal argument indicating if the parent node was succesfully loaded
  3833. * @return {String} the ID of the newly create node
  3834. * @trigger model.jstree, create_node.jstree
  3835. */
  3836. create_node : function (par, node, pos, callback, is_loaded) {
  3837. if(par === null) { par = $.jstree.root; }
  3838. par = this.get_node(par);
  3839. if(!par) { return false; }
  3840. pos = pos === undefined ? "last" : pos;
  3841. if(!pos.toString().match(/^(before|after)$/) && !is_loaded && !this.is_loaded(par)) {
  3842. return this.load_node(par, function () { this.create_node(par, node, pos, callback, true); });
  3843. }
  3844. if(!node) { node = { "text" : this.get_string('New node') }; }
  3845. if(typeof node === "string") {
  3846. node = { "text" : node };
  3847. } else {
  3848. node = $.extend(true, {}, node);
  3849. }
  3850. if(node.text === undefined) { node.text = this.get_string('New node'); }
  3851. var tmp, dpc, i, j;
  3852. if(par.id === $.jstree.root) {
  3853. if(pos === "before") { pos = "first"; }
  3854. if(pos === "after") { pos = "last"; }
  3855. }
  3856. switch(pos) {
  3857. case "before":
  3858. tmp = this.get_node(par.parent);
  3859. pos = $.inArray(par.id, tmp.children);
  3860. par = tmp;
  3861. break;
  3862. case "after" :
  3863. tmp = this.get_node(par.parent);
  3864. pos = $.inArray(par.id, tmp.children) + 1;
  3865. par = tmp;
  3866. break;
  3867. case "inside":
  3868. case "first":
  3869. pos = 0;
  3870. break;
  3871. case "last":
  3872. pos = par.children.length;
  3873. break;
  3874. default:
  3875. if(!pos) { pos = 0; }
  3876. break;
  3877. }
  3878. if(pos > par.children.length) { pos = par.children.length; }
  3879. if(node.id === undefined) { node.id = true; }
  3880. if(!this.check("create_node", node, par, pos)) {
  3881. this.settings.core.error.call(this, this._data.core.last_error);
  3882. return false;
  3883. }
  3884. if(node.id === true) { delete node.id; }
  3885. node = this._parse_model_from_json(node, par.id, par.parents.concat());
  3886. if(!node) { return false; }
  3887. tmp = this.get_node(node);
  3888. dpc = [];
  3889. dpc.push(node);
  3890. dpc = dpc.concat(tmp.children_d);
  3891. this.trigger('model', { "nodes" : dpc, "parent" : par.id });
  3892. par.children_d = par.children_d.concat(dpc);
  3893. for(i = 0, j = par.parents.length; i < j; i++) {
  3894. this._model.data[par.parents[i]].children_d = this._model.data[par.parents[i]].children_d.concat(dpc);
  3895. }
  3896. node = tmp;
  3897. tmp = [];
  3898. for(i = 0, j = par.children.length; i < j; i++) {
  3899. tmp[i >= pos ? i+1 : i] = par.children[i];
  3900. }
  3901. tmp[pos] = node.id;
  3902. par.children = tmp;
  3903. this.redraw_node(par, true);
  3904. /**
  3905. * triggered when a node is created
  3906. * @event
  3907. * @name create_node.jstree
  3908. * @param {Object} node
  3909. * @param {String} parent the parent's ID
  3910. * @param {Number} position the position of the new node among the parent's children
  3911. */
  3912. this.trigger('create_node', { "node" : this.get_node(node), "parent" : par.id, "position" : pos });
  3913. if(callback) { callback.call(this, this.get_node(node)); }
  3914. return node.id;
  3915. },
  3916. /**
  3917. * set the text value of a node
  3918. * @name rename_node(obj, val)
  3919. * @param {mixed} obj the node, you can pass an array to rename multiple nodes to the same name
  3920. * @param {String} val the new text value
  3921. * @return {Boolean}
  3922. * @trigger rename_node.jstree
  3923. */
  3924. rename_node : function (obj, val) {
  3925. var t1, t2, old;
  3926. if($.vakata.is_array(obj)) {
  3927. obj = obj.slice();
  3928. for(t1 = 0, t2 = obj.length; t1 < t2; t1++) {
  3929. this.rename_node(obj[t1], val);
  3930. }
  3931. return true;
  3932. }
  3933. obj = this.get_node(obj);
  3934. if(!obj || obj.id === $.jstree.root) { return false; }
  3935. old = obj.text;
  3936. if(!this.check("rename_node", obj, this.get_parent(obj), val)) {
  3937. this.settings.core.error.call(this, this._data.core.last_error);
  3938. return false;
  3939. }
  3940. this.set_text(obj, val); // .apply(this, Array.prototype.slice.call(arguments))
  3941. /**
  3942. * triggered when a node is renamed
  3943. * @event
  3944. * @name rename_node.jstree
  3945. * @param {Object} node
  3946. * @param {String} text the new value
  3947. * @param {String} old the old value
  3948. */
  3949. this.trigger('rename_node', { "node" : obj, "text" : val, "old" : old });
  3950. return true;
  3951. },
  3952. /**
  3953. * remove a node
  3954. * @name delete_node(obj)
  3955. * @param {mixed} obj the node, you can pass an array to delete multiple nodes
  3956. * @return {Boolean}
  3957. * @trigger delete_node.jstree, changed.jstree
  3958. */
  3959. delete_node : function (obj) {
  3960. var t1, t2, par, pos, tmp, i, j, k, l, c, top, lft;
  3961. if($.vakata.is_array(obj)) {
  3962. obj = obj.slice();
  3963. for(t1 = 0, t2 = obj.length; t1 < t2; t1++) {
  3964. this.delete_node(obj[t1]);
  3965. }
  3966. return true;
  3967. }
  3968. obj = this.get_node(obj);
  3969. if(!obj || obj.id === $.jstree.root) { return false; }
  3970. par = this.get_node(obj.parent);
  3971. pos = $.inArray(obj.id, par.children);
  3972. c = false;
  3973. if(!this.check("delete_node", obj, par, pos)) {
  3974. this.settings.core.error.call(this, this._data.core.last_error);
  3975. return false;
  3976. }
  3977. if(pos !== -1) {
  3978. par.children = $.vakata.array_remove(par.children, pos);
  3979. }
  3980. tmp = obj.children_d.concat([]);
  3981. tmp.push(obj.id);
  3982. for(i = 0, j = obj.parents.length; i < j; i++) {
  3983. this._model.data[obj.parents[i]].children_d = $.vakata.array_filter(this._model.data[obj.parents[i]].children_d, function (v) {
  3984. return $.inArray(v, tmp) === -1;
  3985. });
  3986. }
  3987. for(k = 0, l = tmp.length; k < l; k++) {
  3988. if(this._model.data[tmp[k]].state.selected) {
  3989. c = true;
  3990. break;
  3991. }
  3992. }
  3993. if (c) {
  3994. this._data.core.selected = $.vakata.array_filter(this._data.core.selected, function (v) {
  3995. return $.inArray(v, tmp) === -1;
  3996. });
  3997. }
  3998. /**
  3999. * triggered when a node is deleted
  4000. * @event
  4001. * @name delete_node.jstree
  4002. * @param {Object} node
  4003. * @param {String} parent the parent's ID
  4004. */
  4005. this.trigger('delete_node', { "node" : obj, "parent" : par.id });
  4006. if(c) {
  4007. this.trigger('changed', { 'action' : 'delete_node', 'node' : obj, 'selected' : this._data.core.selected, 'parent' : par.id });
  4008. }
  4009. for(k = 0, l = tmp.length; k < l; k++) {
  4010. delete this._model.data[tmp[k]];
  4011. }
  4012. if($.inArray(this._data.core.focused, tmp) !== -1) {
  4013. this._data.core.focused = null;
  4014. top = this.element[0].scrollTop;
  4015. lft = this.element[0].scrollLeft;
  4016. if(par.id === $.jstree.root) {
  4017. if (this._model.data[$.jstree.root].children[0]) {
  4018. this.get_node(this._model.data[$.jstree.root].children[0], true).children('.jstree-anchor').trigger('focus');
  4019. }
  4020. }
  4021. else {
  4022. this.get_node(par, true).children('.jstree-anchor').trigger('focus');
  4023. }
  4024. this.element[0].scrollTop = top;
  4025. this.element[0].scrollLeft = lft;
  4026. }
  4027. this.redraw_node(par, true);
  4028. return true;
  4029. },
  4030. /**
  4031. * check if an operation is premitted on the tree. Used internally.
  4032. * @private
  4033. * @name check(chk, obj, par, pos)
  4034. * @param {String} chk the operation to check, can be "create_node", "rename_node", "delete_node", "copy_node" or "move_node"
  4035. * @param {mixed} obj the node
  4036. * @param {mixed} par the parent
  4037. * @param {mixed} pos the position to insert at, or if "rename_node" - the new name
  4038. * @param {mixed} more some various additional information, for example if a "move_node" operations is triggered by DND this will be the hovered node
  4039. * @return {Boolean}
  4040. */
  4041. check : function (chk, obj, par, pos, more) {
  4042. obj = obj && obj.id ? obj : this.get_node(obj);
  4043. par = par && par.id ? par : this.get_node(par);
  4044. var tmp = chk.match(/^(move_node|copy_node|create_node)$/i) ? par : obj,
  4045. chc = this.settings.core.check_callback;
  4046. if(chk === "move_node" || chk === "copy_node") {
  4047. if((!more || !more.is_multi) && (chk === "move_node" && $.inArray(obj.id, par.children) === pos)) {
  4048. this._data.core.last_error = { 'error' : 'check', 'plugin' : 'core', 'id' : 'core_08', 'reason' : 'Moving node to its current position', 'data' : JSON.stringify({ 'chk' : chk, 'pos' : pos, 'obj' : obj && obj.id ? obj.id : false, 'par' : par && par.id ? par.id : false }) };
  4049. return false;
  4050. }
  4051. if((!more || !more.is_multi) && (obj.id === par.id || (chk === "move_node" && $.inArray(obj.id, par.children) === pos) || $.inArray(par.id, obj.children_d) !== -1)) {
  4052. this._data.core.last_error = { 'error' : 'check', 'plugin' : 'core', 'id' : 'core_01', 'reason' : 'Moving parent inside child', 'data' : JSON.stringify({ 'chk' : chk, 'pos' : pos, 'obj' : obj && obj.id ? obj.id : false, 'par' : par && par.id ? par.id : false }) };
  4053. return false;
  4054. }
  4055. }
  4056. if(tmp && tmp.data) { tmp = tmp.data; }
  4057. if(tmp && tmp.functions && (tmp.functions[chk] === false || tmp.functions[chk] === true)) {
  4058. if(tmp.functions[chk] === false) {
  4059. this._data.core.last_error = { 'error' : 'check', 'plugin' : 'core', 'id' : 'core_02', 'reason' : 'Node data prevents function: ' + chk, 'data' : JSON.stringify({ 'chk' : chk, 'pos' : pos, 'obj' : obj && obj.id ? obj.id : false, 'par' : par && par.id ? par.id : false }) };
  4060. }
  4061. return tmp.functions[chk];
  4062. }
  4063. if(chc === false || ($.vakata.is_function(chc) && chc.call(this, chk, obj, par, pos, more) === false) || (chc && chc[chk] === false)) {
  4064. this._data.core.last_error = { 'error' : 'check', 'plugin' : 'core', 'id' : 'core_03', 'reason' : 'User config for core.check_callback prevents function: ' + chk, 'data' : JSON.stringify({ 'chk' : chk, 'pos' : pos, 'obj' : obj && obj.id ? obj.id : false, 'par' : par && par.id ? par.id : false }) };
  4065. return false;
  4066. }
  4067. return true;
  4068. },
  4069. /**
  4070. * get the last error
  4071. * @name last_error()
  4072. * @return {Object}
  4073. */
  4074. last_error : function () {
  4075. return this._data.core.last_error;
  4076. },
  4077. /**
  4078. * move a node to a new parent
  4079. * @name move_node(obj, par [, pos, callback, is_loaded])
  4080. * @param {mixed} obj the node to move, pass an array to move multiple nodes
  4081. * @param {mixed} par the new parent
  4082. * @param {mixed} pos the position to insert at (besides integer values, "first" and "last" are supported, as well as "before" and "after"), defaults to integer `0`
  4083. * @param {function} callback a function to call once the move is completed, receives 3 arguments - the node, the new parent and the position
  4084. * @param {Boolean} is_loaded internal parameter indicating if the parent node has been loaded
  4085. * @param {Boolean} skip_redraw internal parameter indicating if the tree should be redrawn
  4086. * @param {Boolean} instance internal parameter indicating if the node comes from another instance
  4087. * @trigger move_node.jstree
  4088. */
  4089. move_node : function (obj, par, pos, callback, is_loaded, skip_redraw, origin) {
  4090. var t1, t2, old_par, old_pos, new_par, old_ins, is_multi, dpc, tmp, i, j, k, l, p;
  4091. par = this.get_node(par);
  4092. pos = pos === undefined ? 0 : pos;
  4093. if(!par) { return false; }
  4094. if(!pos.toString().match(/^(before|after)$/) && !is_loaded && !this.is_loaded(par)) {
  4095. return this.load_node(par, function () { this.move_node(obj, par, pos, callback, true, false, origin); });
  4096. }
  4097. if($.vakata.is_array(obj)) {
  4098. if(obj.length === 1) {
  4099. obj = obj[0];
  4100. }
  4101. else {
  4102. //obj = obj.slice();
  4103. for(t1 = 0, t2 = obj.length; t1 < t2; t1++) {
  4104. if((tmp = this.move_node(obj[t1], par, pos, callback, is_loaded, false, origin))) {
  4105. par = tmp;
  4106. pos = "after";
  4107. }
  4108. }
  4109. this.redraw();
  4110. return true;
  4111. }
  4112. }
  4113. obj = obj && (obj.id !== undefined) ? obj : this.get_node(obj);
  4114. if(!obj || obj.id === $.jstree.root) { return false; }
  4115. old_par = (obj.parent || $.jstree.root).toString();
  4116. new_par = (!pos.toString().match(/^(before|after)$/) || par.id === $.jstree.root) ? par : this.get_node(par.parent);
  4117. old_ins = origin ? origin : (this._model.data[obj.id] ? this : $.jstree.reference(obj.id));
  4118. is_multi = !old_ins || !old_ins._id || (this._id !== old_ins._id);
  4119. old_pos = old_ins && old_ins._id && old_par && old_ins._model.data[old_par] && old_ins._model.data[old_par].children ? $.inArray(obj.id, old_ins._model.data[old_par].children) : -1;
  4120. if(old_ins && old_ins._id) {
  4121. obj = old_ins._model.data[obj.id];
  4122. }
  4123. if(is_multi) {
  4124. if((tmp = this.copy_node(obj, par, pos, callback, is_loaded, false, origin))) {
  4125. if(old_ins) { old_ins.delete_node(obj); }
  4126. return tmp;
  4127. }
  4128. return false;
  4129. }
  4130. //var m = this._model.data;
  4131. if(par.id === $.jstree.root) {
  4132. if(pos === "before") { pos = "first"; }
  4133. if(pos === "after") { pos = "last"; }
  4134. }
  4135. switch(pos) {
  4136. case "before":
  4137. pos = $.inArray(par.id, new_par.children);
  4138. break;
  4139. case "after" :
  4140. pos = $.inArray(par.id, new_par.children) + 1;
  4141. break;
  4142. case "inside":
  4143. case "first":
  4144. pos = 0;
  4145. break;
  4146. case "last":
  4147. pos = new_par.children.length;
  4148. break;
  4149. default:
  4150. if(!pos) { pos = 0; }
  4151. break;
  4152. }
  4153. if(pos > new_par.children.length) { pos = new_par.children.length; }
  4154. if(!this.check("move_node", obj, new_par, pos, { 'core' : true, 'origin' : origin, 'is_multi' : (old_ins && old_ins._id && old_ins._id !== this._id), 'is_foreign' : (!old_ins || !old_ins._id) })) {
  4155. this.settings.core.error.call(this, this._data.core.last_error);
  4156. return false;
  4157. }
  4158. if(obj.parent === new_par.id) {
  4159. dpc = new_par.children.concat();
  4160. tmp = $.inArray(obj.id, dpc);
  4161. if(tmp !== -1) {
  4162. dpc = $.vakata.array_remove(dpc, tmp);
  4163. if(pos > tmp) { pos--; }
  4164. }
  4165. tmp = [];
  4166. for(i = 0, j = dpc.length; i < j; i++) {
  4167. tmp[i >= pos ? i+1 : i] = dpc[i];
  4168. }
  4169. tmp[pos] = obj.id;
  4170. new_par.children = tmp;
  4171. this._node_changed(new_par.id);
  4172. this.redraw(new_par.id === $.jstree.root);
  4173. }
  4174. else {
  4175. // clean old parent and up
  4176. tmp = obj.children_d.concat();
  4177. tmp.push(obj.id);
  4178. for(i = 0, j = obj.parents.length; i < j; i++) {
  4179. dpc = [];
  4180. p = old_ins._model.data[obj.parents[i]].children_d;
  4181. for(k = 0, l = p.length; k < l; k++) {
  4182. if($.inArray(p[k], tmp) === -1) {
  4183. dpc.push(p[k]);
  4184. }
  4185. }
  4186. old_ins._model.data[obj.parents[i]].children_d = dpc;
  4187. }
  4188. old_ins._model.data[old_par].children = $.vakata.array_remove_item(old_ins._model.data[old_par].children, obj.id);
  4189. // insert into new parent and up
  4190. for(i = 0, j = new_par.parents.length; i < j; i++) {
  4191. this._model.data[new_par.parents[i]].children_d = this._model.data[new_par.parents[i]].children_d.concat(tmp);
  4192. }
  4193. dpc = [];
  4194. for(i = 0, j = new_par.children.length; i < j; i++) {
  4195. dpc[i >= pos ? i+1 : i] = new_par.children[i];
  4196. }
  4197. dpc[pos] = obj.id;
  4198. new_par.children = dpc;
  4199. new_par.children_d.push(obj.id);
  4200. new_par.children_d = new_par.children_d.concat(obj.children_d);
  4201. // update object
  4202. obj.parent = new_par.id;
  4203. tmp = new_par.parents.concat();
  4204. tmp.unshift(new_par.id);
  4205. p = obj.parents.length;
  4206. obj.parents = tmp;
  4207. // update object children
  4208. tmp = tmp.concat();
  4209. for(i = 0, j = obj.children_d.length; i < j; i++) {
  4210. this._model.data[obj.children_d[i]].parents = this._model.data[obj.children_d[i]].parents.slice(0,p*-1);
  4211. Array.prototype.push.apply(this._model.data[obj.children_d[i]].parents, tmp);
  4212. }
  4213. if(old_par === $.jstree.root || new_par.id === $.jstree.root) {
  4214. this._model.force_full_redraw = true;
  4215. }
  4216. if(!this._model.force_full_redraw) {
  4217. this._node_changed(old_par);
  4218. this._node_changed(new_par.id);
  4219. }
  4220. if(!skip_redraw) {
  4221. this.redraw();
  4222. }
  4223. }
  4224. if(callback) { callback.call(this, obj, new_par, pos); }
  4225. /**
  4226. * triggered when a node is moved
  4227. * @event
  4228. * @name move_node.jstree
  4229. * @param {Object} node
  4230. * @param {String} parent the parent's ID
  4231. * @param {Number} position the position of the node among the parent's children
  4232. * @param {String} old_parent the old parent of the node
  4233. * @param {Number} old_position the old position of the node
  4234. * @param {Boolean} is_multi do the node and new parent belong to different instances
  4235. * @param {jsTree} old_instance the instance the node came from
  4236. * @param {jsTree} new_instance the instance of the new parent
  4237. */
  4238. this.trigger('move_node', { "node" : obj, "parent" : new_par.id, "position" : pos, "old_parent" : old_par, "old_position" : old_pos, 'is_multi' : (old_ins && old_ins._id && old_ins._id !== this._id), 'is_foreign' : (!old_ins || !old_ins._id), 'old_instance' : old_ins, 'new_instance' : this });
  4239. return obj.id;
  4240. },
  4241. /**
  4242. * copy a node to a new parent
  4243. * @name copy_node(obj, par [, pos, callback, is_loaded])
  4244. * @param {mixed} obj the node to copy, pass an array to copy multiple nodes
  4245. * @param {mixed} par the new parent
  4246. * @param {mixed} pos the position to insert at (besides integer values, "first" and "last" are supported, as well as "before" and "after"), defaults to integer `0`
  4247. * @param {function} callback a function to call once the move is completed, receives 3 arguments - the node, the new parent and the position
  4248. * @param {Boolean} is_loaded internal parameter indicating if the parent node has been loaded
  4249. * @param {Boolean} skip_redraw internal parameter indicating if the tree should be redrawn
  4250. * @param {Boolean} instance internal parameter indicating if the node comes from another instance
  4251. * @trigger model.jstree copy_node.jstree
  4252. */
  4253. copy_node : function (obj, par, pos, callback, is_loaded, skip_redraw, origin) {
  4254. var t1, t2, dpc, tmp, i, j, node, old_par, new_par, old_ins, is_multi;
  4255. par = this.get_node(par);
  4256. pos = pos === undefined ? 0 : pos;
  4257. if(!par) { return false; }
  4258. if(!pos.toString().match(/^(before|after)$/) && !is_loaded && !this.is_loaded(par)) {
  4259. return this.load_node(par, function () { this.copy_node(obj, par, pos, callback, true, false, origin); });
  4260. }
  4261. if($.vakata.is_array(obj)) {
  4262. if(obj.length === 1) {
  4263. obj = obj[0];
  4264. }
  4265. else {
  4266. //obj = obj.slice();
  4267. for(t1 = 0, t2 = obj.length; t1 < t2; t1++) {
  4268. if((tmp = this.copy_node(obj[t1], par, pos, callback, is_loaded, true, origin))) {
  4269. par = tmp;
  4270. pos = "after";
  4271. }
  4272. }
  4273. this.redraw();
  4274. return true;
  4275. }
  4276. }
  4277. obj = obj && (obj.id !== undefined) ? obj : this.get_node(obj);
  4278. if(!obj || obj.id === $.jstree.root) { return false; }
  4279. old_par = (obj.parent || $.jstree.root).toString();
  4280. new_par = (!pos.toString().match(/^(before|after)$/) || par.id === $.jstree.root) ? par : this.get_node(par.parent);
  4281. old_ins = origin ? origin : (this._model.data[obj.id] ? this : $.jstree.reference(obj.id));
  4282. is_multi = !old_ins || !old_ins._id || (this._id !== old_ins._id);
  4283. if(old_ins && old_ins._id) {
  4284. obj = old_ins._model.data[obj.id];
  4285. }
  4286. if(par.id === $.jstree.root) {
  4287. if(pos === "before") { pos = "first"; }
  4288. if(pos === "after") { pos = "last"; }
  4289. }
  4290. switch(pos) {
  4291. case "before":
  4292. pos = $.inArray(par.id, new_par.children);
  4293. break;
  4294. case "after" :
  4295. pos = $.inArray(par.id, new_par.children) + 1;
  4296. break;
  4297. case "inside":
  4298. case "first":
  4299. pos = 0;
  4300. break;
  4301. case "last":
  4302. pos = new_par.children.length;
  4303. break;
  4304. default:
  4305. if(!pos) { pos = 0; }
  4306. break;
  4307. }
  4308. if(pos > new_par.children.length) { pos = new_par.children.length; }
  4309. if(!this.check("copy_node", obj, new_par, pos, { 'core' : true, 'origin' : origin, 'is_multi' : (old_ins && old_ins._id && old_ins._id !== this._id), 'is_foreign' : (!old_ins || !old_ins._id) })) {
  4310. this.settings.core.error.call(this, this._data.core.last_error);
  4311. return false;
  4312. }
  4313. node = old_ins ? old_ins.get_json(obj, { no_id : true, no_data : true, no_state : true }) : obj;
  4314. if(!node) { return false; }
  4315. if(node.id === true) { delete node.id; }
  4316. node = this._parse_model_from_json(node, new_par.id, new_par.parents.concat());
  4317. if(!node) { return false; }
  4318. tmp = this.get_node(node);
  4319. if(obj && obj.state && obj.state.loaded === false) { tmp.state.loaded = false; }
  4320. dpc = [];
  4321. dpc.push(node);
  4322. dpc = dpc.concat(tmp.children_d);
  4323. this.trigger('model', { "nodes" : dpc, "parent" : new_par.id });
  4324. // insert into new parent and up
  4325. for(i = 0, j = new_par.parents.length; i < j; i++) {
  4326. this._model.data[new_par.parents[i]].children_d = this._model.data[new_par.parents[i]].children_d.concat(dpc);
  4327. }
  4328. dpc = [];
  4329. for(i = 0, j = new_par.children.length; i < j; i++) {
  4330. dpc[i >= pos ? i+1 : i] = new_par.children[i];
  4331. }
  4332. dpc[pos] = tmp.id;
  4333. new_par.children = dpc;
  4334. new_par.children_d.push(tmp.id);
  4335. new_par.children_d = new_par.children_d.concat(tmp.children_d);
  4336. if(new_par.id === $.jstree.root) {
  4337. this._model.force_full_redraw = true;
  4338. }
  4339. if(!this._model.force_full_redraw) {
  4340. this._node_changed(new_par.id);
  4341. }
  4342. if(!skip_redraw) {
  4343. this.redraw(new_par.id === $.jstree.root);
  4344. }
  4345. if(callback) { callback.call(this, tmp, new_par, pos); }
  4346. /**
  4347. * triggered when a node is copied
  4348. * @event
  4349. * @name copy_node.jstree
  4350. * @param {Object} node the copied node
  4351. * @param {Object} original the original node
  4352. * @param {String} parent the parent's ID
  4353. * @param {Number} position the position of the node among the parent's children
  4354. * @param {String} old_parent the old parent of the node
  4355. * @param {Number} old_position the position of the original node
  4356. * @param {Boolean} is_multi do the node and new parent belong to different instances
  4357. * @param {jsTree} old_instance the instance the node came from
  4358. * @param {jsTree} new_instance the instance of the new parent
  4359. */
  4360. this.trigger('copy_node', { "node" : tmp, "original" : obj, "parent" : new_par.id, "position" : pos, "old_parent" : old_par, "old_position" : old_ins && old_ins._id && old_par && old_ins._model.data[old_par] && old_ins._model.data[old_par].children ? $.inArray(obj.id, old_ins._model.data[old_par].children) : -1,'is_multi' : (old_ins && old_ins._id && old_ins._id !== this._id), 'is_foreign' : (!old_ins || !old_ins._id), 'old_instance' : old_ins, 'new_instance' : this });
  4361. return tmp.id;
  4362. },
  4363. /**
  4364. * cut a node (a later call to `paste(obj)` would move the node)
  4365. * @name cut(obj)
  4366. * @param {mixed} obj multiple objects can be passed using an array
  4367. * @trigger cut.jstree
  4368. */
  4369. cut : function (obj) {
  4370. if(!obj) { obj = this._data.core.selected.concat(); }
  4371. if(!$.vakata.is_array(obj)) { obj = [obj]; }
  4372. if(!obj.length) { return false; }
  4373. var tmp = [], o, t1, t2;
  4374. for(t1 = 0, t2 = obj.length; t1 < t2; t1++) {
  4375. o = this.get_node(obj[t1]);
  4376. if(o && (o.id || o.id === 0) && o.id !== $.jstree.root) { tmp.push(o); }
  4377. }
  4378. if(!tmp.length) { return false; }
  4379. ccp_node = tmp;
  4380. ccp_inst = this;
  4381. ccp_mode = 'move_node';
  4382. /**
  4383. * triggered when nodes are added to the buffer for moving
  4384. * @event
  4385. * @name cut.jstree
  4386. * @param {Array} node
  4387. */
  4388. this.trigger('cut', { "node" : obj });
  4389. },
  4390. /**
  4391. * copy a node (a later call to `paste(obj)` would copy the node)
  4392. * @name copy(obj)
  4393. * @param {mixed} obj multiple objects can be passed using an array
  4394. * @trigger copy.jstree
  4395. */
  4396. copy : function (obj) {
  4397. if(!obj) { obj = this._data.core.selected.concat(); }
  4398. if(!$.vakata.is_array(obj)) { obj = [obj]; }
  4399. if(!obj.length) { return false; }
  4400. var tmp = [], o, t1, t2;
  4401. for(t1 = 0, t2 = obj.length; t1 < t2; t1++) {
  4402. o = this.get_node(obj[t1]);
  4403. if(o && (o.id !== undefined) && o.id !== $.jstree.root) { tmp.push(o); }
  4404. }
  4405. if(!tmp.length) { return false; }
  4406. ccp_node = tmp;
  4407. ccp_inst = this;
  4408. ccp_mode = 'copy_node';
  4409. /**
  4410. * triggered when nodes are added to the buffer for copying
  4411. * @event
  4412. * @name copy.jstree
  4413. * @param {Array} node
  4414. */
  4415. this.trigger('copy', { "node" : obj });
  4416. },
  4417. /**
  4418. * get the current buffer (any nodes that are waiting for a paste operation)
  4419. * @name get_buffer()
  4420. * @return {Object} an object consisting of `mode` ("copy_node" or "move_node"), `node` (an array of objects) and `inst` (the instance)
  4421. */
  4422. get_buffer : function () {
  4423. return { 'mode' : ccp_mode, 'node' : ccp_node, 'inst' : ccp_inst };
  4424. },
  4425. /**
  4426. * check if there is something in the buffer to paste
  4427. * @name can_paste()
  4428. * @return {Boolean}
  4429. */
  4430. can_paste : function () {
  4431. return ccp_mode !== false && ccp_node !== false; // && ccp_inst._model.data[ccp_node];
  4432. },
  4433. /**
  4434. * copy or move the previously cut or copied nodes to a new parent
  4435. * @name paste(obj [, pos])
  4436. * @param {mixed} obj the new parent
  4437. * @param {mixed} pos the position to insert at (besides integer, "first" and "last" are supported), defaults to integer `0`
  4438. * @trigger paste.jstree
  4439. */
  4440. paste : function (obj, pos) {
  4441. obj = this.get_node(obj);
  4442. if(!obj || !ccp_mode || !ccp_mode.match(/^(copy_node|move_node)$/) || !ccp_node) { return false; }
  4443. if(this[ccp_mode](ccp_node, obj, pos, false, false, false, ccp_inst)) {
  4444. /**
  4445. * triggered when paste is invoked
  4446. * @event
  4447. * @name paste.jstree
  4448. * @param {String} parent the ID of the receiving node
  4449. * @param {Array} node the nodes in the buffer
  4450. * @param {String} mode the performed operation - "copy_node" or "move_node"
  4451. */
  4452. this.trigger('paste', { "parent" : obj.id, "node" : ccp_node, "mode" : ccp_mode });
  4453. }
  4454. ccp_node = false;
  4455. ccp_mode = false;
  4456. ccp_inst = false;
  4457. },
  4458. /**
  4459. * clear the buffer of previously copied or cut nodes
  4460. * @name clear_buffer()
  4461. * @trigger clear_buffer.jstree
  4462. */
  4463. clear_buffer : function () {
  4464. ccp_node = false;
  4465. ccp_mode = false;
  4466. ccp_inst = false;
  4467. /**
  4468. * triggered when the copy / cut buffer is cleared
  4469. * @event
  4470. * @name clear_buffer.jstree
  4471. */
  4472. this.trigger('clear_buffer');
  4473. },
  4474. /**
  4475. * put a node in edit mode (input field to rename the node)
  4476. * @name edit(obj [, default_text, callback])
  4477. * @param {mixed} obj
  4478. * @param {String} default_text the text to populate the input with (if omitted or set to a non-string value the node's text value is used)
  4479. * @param {Function} callback a function to be called once the text box is blurred, it is called in the instance's scope and receives the node, a status parameter (true if the rename is successful, false otherwise), a boolean indicating if the user cancelled the edit and the original unescaped value provided by the user. You can also access the node's title using .text
  4480. */
  4481. edit : function (obj, default_text, callback) {
  4482. var rtl, w, a, s, t, h1, h2, fn, tmp, cancel = false;
  4483. obj = this.get_node(obj);
  4484. if(!obj) { return false; }
  4485. if(!this.check("edit", obj, this.get_parent(obj))) {
  4486. this.settings.core.error.call(this, this._data.core.last_error);
  4487. return false;
  4488. }
  4489. tmp = obj;
  4490. default_text = typeof default_text === 'string' ? default_text : obj.text;
  4491. this.set_text(obj, "");
  4492. obj = this._open_to(obj);
  4493. tmp.text = default_text;
  4494. rtl = this._data.core.rtl;
  4495. w = this.element.width();
  4496. this._data.core.focused = tmp.id;
  4497. a = obj.children('.jstree-anchor').trigger('focus');
  4498. s = $('<span></span>');
  4499. /*!
  4500. oi = obj.children("i:visible"),
  4501. ai = a.children("i:visible"),
  4502. w1 = oi.width() * oi.length,
  4503. w2 = ai.width() * ai.length,
  4504. */
  4505. t = default_text;
  4506. h1 = $("<"+"div></div>", { css : { "position" : "absolute", "top" : "-200px", "left" : (rtl ? "0px" : "-1000px"), "visibility" : "hidden" } }).appendTo(document.body);
  4507. h2 = $("<"+"input />", {
  4508. "value" : t,
  4509. "class" : "jstree-rename-input",
  4510. // "size" : t.length,
  4511. "css" : {
  4512. "padding" : "0",
  4513. "border" : "1px solid silver",
  4514. "box-sizing" : "border-box",
  4515. "display" : "inline-block",
  4516. "height" : (this._data.core.li_height) + "px",
  4517. "lineHeight" : (this._data.core.li_height) + "px",
  4518. "width" : "150px" // will be set a bit further down
  4519. },
  4520. "blur" : function (e) {
  4521. e.stopImmediatePropagation();
  4522. e.preventDefault();
  4523. var i = s.children(".jstree-rename-input"),
  4524. v = i.val(),
  4525. f = this.settings.core.force_text,
  4526. nv;
  4527. if(v === "") { v = t; }
  4528. h1.remove();
  4529. s.replaceWith(a);
  4530. s.remove();
  4531. t = f ? t : $('<div></div>').append($.parseHTML(t)).html();
  4532. obj = this.get_node(obj);
  4533. this.set_text(obj, t);
  4534. nv = !!this.rename_node(obj, f ? $('<div></div>').text(v).text() : $('<div></div>').append($.parseHTML(v)).html());
  4535. if(!nv) {
  4536. this.set_text(obj, t); // move this up? and fix #483
  4537. }
  4538. this._data.core.focused = tmp.id;
  4539. setTimeout(function () {
  4540. var node = this.get_node(tmp.id, true);
  4541. if(node.length) {
  4542. this._data.core.focused = tmp.id;
  4543. node.children('.jstree-anchor').trigger('focus');
  4544. }
  4545. }.bind(this), 0);
  4546. if(callback) {
  4547. callback.call(this, tmp, nv, cancel, v);
  4548. }
  4549. h2 = null;
  4550. }.bind(this),
  4551. "keydown" : function (e) {
  4552. var key = e.which;
  4553. if(key === 27) {
  4554. cancel = true;
  4555. this.value = t;
  4556. }
  4557. if(key === 27 || key === 13 || key === 37 || key === 38 || key === 39 || key === 40 || key === 32) {
  4558. e.stopImmediatePropagation();
  4559. }
  4560. if(key === 27 || key === 13) {
  4561. e.preventDefault();
  4562. this.blur();
  4563. }
  4564. },
  4565. "click" : function (e) { e.stopImmediatePropagation(); },
  4566. "mousedown" : function (e) { e.stopImmediatePropagation(); },
  4567. "keyup" : function (e) {
  4568. h2.width(Math.min(h1.text("pW" + this.value).width(),w));
  4569. },
  4570. "keypress" : function(e) {
  4571. if(e.which === 13) { return false; }
  4572. }
  4573. });
  4574. fn = {
  4575. fontFamily : a.css('fontFamily') || '',
  4576. fontSize : a.css('fontSize') || '',
  4577. fontWeight : a.css('fontWeight') || '',
  4578. fontStyle : a.css('fontStyle') || '',
  4579. fontStretch : a.css('fontStretch') || '',
  4580. fontVariant : a.css('fontVariant') || '',
  4581. letterSpacing : a.css('letterSpacing') || '',
  4582. wordSpacing : a.css('wordSpacing') || ''
  4583. };
  4584. s.attr('class', a.attr('class')).append(a.contents().clone()).append(h2);
  4585. a.replaceWith(s);
  4586. h1.css(fn);
  4587. h2.css(fn).width(Math.min(h1.text("pW" + h2[0].value).width(),w))[0].select();
  4588. $(document).one('mousedown.jstree touchstart.jstree dnd_start.vakata', function (e) {
  4589. if (h2 && e.target !== h2) {
  4590. $(h2).trigger('blur');
  4591. }
  4592. });
  4593. },
  4594. /**
  4595. * changes the theme
  4596. * @name set_theme(theme_name [, theme_url])
  4597. * @param {String} theme_name the name of the new theme to apply
  4598. * @param {mixed} theme_url the location of the CSS file for this theme. Omit or set to `false` if you manually included the file. Set to `true` to autoload from the `core.themes.dir` directory.
  4599. * @trigger set_theme.jstree
  4600. */
  4601. set_theme : function (theme_name, theme_url) {
  4602. if(!theme_name) { return false; }
  4603. if(theme_url === true) {
  4604. var dir = this.settings.core.themes.dir;
  4605. if(!dir) { dir = $.jstree.path + '/themes'; }
  4606. theme_url = dir + '/' + theme_name + '/style.css';
  4607. }
  4608. if(theme_url && $.inArray(theme_url, themes_loaded) === -1) {
  4609. $('head').append('<'+'link rel="stylesheet" href="' + theme_url + '" type="text/css" />');
  4610. themes_loaded.push(theme_url);
  4611. }
  4612. if(this._data.core.themes.name) {
  4613. this.element.removeClass('jstree-' + this._data.core.themes.name);
  4614. }
  4615. this._data.core.themes.name = theme_name;
  4616. this.element.addClass('jstree-' + theme_name);
  4617. this.element[this.settings.core.themes.responsive ? 'addClass' : 'removeClass' ]('jstree-' + theme_name + '-responsive');
  4618. /**
  4619. * triggered when a theme is set
  4620. * @event
  4621. * @name set_theme.jstree
  4622. * @param {String} theme the new theme
  4623. */
  4624. this.trigger('set_theme', { 'theme' : theme_name });
  4625. },
  4626. /**
  4627. * gets the name of the currently applied theme name
  4628. * @name get_theme()
  4629. * @return {String}
  4630. */
  4631. get_theme : function () { return this._data.core.themes.name; },
  4632. /**
  4633. * changes the theme variant (if the theme has variants)
  4634. * @name set_theme_variant(variant_name)
  4635. * @param {String|Boolean} variant_name the variant to apply (if `false` is used the current variant is removed)
  4636. */
  4637. set_theme_variant : function (variant_name) {
  4638. if(this._data.core.themes.variant) {
  4639. this.element.removeClass('jstree-' + this._data.core.themes.name + '-' + this._data.core.themes.variant);
  4640. }
  4641. this._data.core.themes.variant = variant_name;
  4642. if(variant_name) {
  4643. this.element.addClass('jstree-' + this._data.core.themes.name + '-' + this._data.core.themes.variant);
  4644. }
  4645. },
  4646. /**
  4647. * gets the name of the currently applied theme variant
  4648. * @name get_theme()
  4649. * @return {String}
  4650. */
  4651. get_theme_variant : function () { return this._data.core.themes.variant; },
  4652. /**
  4653. * shows a striped background on the container (if the theme supports it)
  4654. * @name show_stripes()
  4655. */
  4656. show_stripes : function () {
  4657. this._data.core.themes.stripes = true;
  4658. this.get_container_ul().addClass("jstree-striped");
  4659. /**
  4660. * triggered when stripes are shown
  4661. * @event
  4662. * @name show_stripes.jstree
  4663. */
  4664. this.trigger('show_stripes');
  4665. },
  4666. /**
  4667. * hides the striped background on the container
  4668. * @name hide_stripes()
  4669. */
  4670. hide_stripes : function () {
  4671. this._data.core.themes.stripes = false;
  4672. this.get_container_ul().removeClass("jstree-striped");
  4673. /**
  4674. * triggered when stripes are hidden
  4675. * @event
  4676. * @name hide_stripes.jstree
  4677. */
  4678. this.trigger('hide_stripes');
  4679. },
  4680. /**
  4681. * toggles the striped background on the container
  4682. * @name toggle_stripes()
  4683. */
  4684. toggle_stripes : function () { if(this._data.core.themes.stripes) { this.hide_stripes(); } else { this.show_stripes(); } },
  4685. /**
  4686. * shows the connecting dots (if the theme supports it)
  4687. * @name show_dots()
  4688. */
  4689. show_dots : function () {
  4690. this._data.core.themes.dots = true;
  4691. this.get_container_ul().removeClass("jstree-no-dots");
  4692. /**
  4693. * triggered when dots are shown
  4694. * @event
  4695. * @name show_dots.jstree
  4696. */
  4697. this.trigger('show_dots');
  4698. },
  4699. /**
  4700. * hides the connecting dots
  4701. * @name hide_dots()
  4702. */
  4703. hide_dots : function () {
  4704. this._data.core.themes.dots = false;
  4705. this.get_container_ul().addClass("jstree-no-dots");
  4706. /**
  4707. * triggered when dots are hidden
  4708. * @event
  4709. * @name hide_dots.jstree
  4710. */
  4711. this.trigger('hide_dots');
  4712. },
  4713. /**
  4714. * toggles the connecting dots
  4715. * @name toggle_dots()
  4716. */
  4717. toggle_dots : function () { if(this._data.core.themes.dots) { this.hide_dots(); } else { this.show_dots(); } },
  4718. /**
  4719. * show the node icons
  4720. * @name show_icons()
  4721. */
  4722. show_icons : function () {
  4723. this._data.core.themes.icons = true;
  4724. this.get_container_ul().removeClass("jstree-no-icons");
  4725. /**
  4726. * triggered when icons are shown
  4727. * @event
  4728. * @name show_icons.jstree
  4729. */
  4730. this.trigger('show_icons');
  4731. },
  4732. /**
  4733. * hide the node icons
  4734. * @name hide_icons()
  4735. */
  4736. hide_icons : function () {
  4737. this._data.core.themes.icons = false;
  4738. this.get_container_ul().addClass("jstree-no-icons");
  4739. /**
  4740. * triggered when icons are hidden
  4741. * @event
  4742. * @name hide_icons.jstree
  4743. */
  4744. this.trigger('hide_icons');
  4745. },
  4746. /**
  4747. * toggle the node icons
  4748. * @name toggle_icons()
  4749. */
  4750. toggle_icons : function () { if(this._data.core.themes.icons) { this.hide_icons(); } else { this.show_icons(); } },
  4751. /**
  4752. * show the node ellipsis
  4753. * @name show_icons()
  4754. */
  4755. show_ellipsis : function () {
  4756. this._data.core.themes.ellipsis = true;
  4757. this.get_container_ul().addClass("jstree-ellipsis");
  4758. /**
  4759. * triggered when ellisis is shown
  4760. * @event
  4761. * @name show_ellipsis.jstree
  4762. */
  4763. this.trigger('show_ellipsis');
  4764. },
  4765. /**
  4766. * hide the node ellipsis
  4767. * @name hide_ellipsis()
  4768. */
  4769. hide_ellipsis : function () {
  4770. this._data.core.themes.ellipsis = false;
  4771. this.get_container_ul().removeClass("jstree-ellipsis");
  4772. /**
  4773. * triggered when ellisis is hidden
  4774. * @event
  4775. * @name hide_ellipsis.jstree
  4776. */
  4777. this.trigger('hide_ellipsis');
  4778. },
  4779. /**
  4780. * toggle the node ellipsis
  4781. * @name toggle_icons()
  4782. */
  4783. toggle_ellipsis : function () { if(this._data.core.themes.ellipsis) { this.hide_ellipsis(); } else { this.show_ellipsis(); } },
  4784. /**
  4785. * set the node icon for a node
  4786. * @name set_icon(obj, icon)
  4787. * @param {mixed} obj
  4788. * @param {String} icon the new icon - can be a path to an icon or a className, if using an image that is in the current directory use a `./` prefix, otherwise it will be detected as a class
  4789. */
  4790. set_icon : function (obj, icon) {
  4791. var t1, t2, dom, old;
  4792. if($.vakata.is_array(obj)) {
  4793. obj = obj.slice();
  4794. for(t1 = 0, t2 = obj.length; t1 < t2; t1++) {
  4795. this.set_icon(obj[t1], icon);
  4796. }
  4797. return true;
  4798. }
  4799. obj = this.get_node(obj);
  4800. if(!obj || obj.id === $.jstree.root) { return false; }
  4801. old = obj.icon;
  4802. obj.icon = icon === true || icon === null || icon === undefined || icon === '' ? true : icon;
  4803. dom = this.get_node(obj, true).children(".jstree-anchor").children(".jstree-themeicon");
  4804. if(icon === false) {
  4805. dom.removeClass('jstree-themeicon-custom ' + old).css("background","").removeAttr("rel");
  4806. this.hide_icon(obj);
  4807. }
  4808. else if(icon === true || icon === null || icon === undefined || icon === '') {
  4809. dom.removeClass('jstree-themeicon-custom ' + old).css("background","").removeAttr("rel");
  4810. if(old === false) { this.show_icon(obj); }
  4811. }
  4812. else if(icon.indexOf("/") === -1 && icon.indexOf(".") === -1) {
  4813. dom.removeClass(old).css("background","");
  4814. dom.addClass(icon + ' jstree-themeicon-custom').attr("rel",icon);
  4815. if(old === false) { this.show_icon(obj); }
  4816. }
  4817. else {
  4818. dom.removeClass(old).css("background","");
  4819. dom.addClass('jstree-themeicon-custom').css("background", "url('" + icon + "') center center no-repeat").attr("rel",icon);
  4820. if(old === false) { this.show_icon(obj); }
  4821. }
  4822. return true;
  4823. },
  4824. /**
  4825. * get the node icon for a node
  4826. * @name get_icon(obj)
  4827. * @param {mixed} obj
  4828. * @return {String}
  4829. */
  4830. get_icon : function (obj) {
  4831. obj = this.get_node(obj);
  4832. return (!obj || obj.id === $.jstree.root) ? false : obj.icon;
  4833. },
  4834. /**
  4835. * hide the icon on an individual node
  4836. * @name hide_icon(obj)
  4837. * @param {mixed} obj
  4838. */
  4839. hide_icon : function (obj) {
  4840. var t1, t2;
  4841. if($.vakata.is_array(obj)) {
  4842. obj = obj.slice();
  4843. for(t1 = 0, t2 = obj.length; t1 < t2; t1++) {
  4844. this.hide_icon(obj[t1]);
  4845. }
  4846. return true;
  4847. }
  4848. obj = this.get_node(obj);
  4849. if(!obj || obj === $.jstree.root) { return false; }
  4850. obj.icon = false;
  4851. this.get_node(obj, true).children(".jstree-anchor").children(".jstree-themeicon").addClass('jstree-themeicon-hidden');
  4852. return true;
  4853. },
  4854. /**
  4855. * show the icon on an individual node
  4856. * @name show_icon(obj)
  4857. * @param {mixed} obj
  4858. */
  4859. show_icon : function (obj) {
  4860. var t1, t2, dom;
  4861. if($.vakata.is_array(obj)) {
  4862. obj = obj.slice();
  4863. for(t1 = 0, t2 = obj.length; t1 < t2; t1++) {
  4864. this.show_icon(obj[t1]);
  4865. }
  4866. return true;
  4867. }
  4868. obj = this.get_node(obj);
  4869. if(!obj || obj === $.jstree.root) { return false; }
  4870. dom = this.get_node(obj, true);
  4871. obj.icon = dom.length ? dom.children(".jstree-anchor").children(".jstree-themeicon").attr('rel') : true;
  4872. if(!obj.icon) { obj.icon = true; }
  4873. dom.children(".jstree-anchor").children(".jstree-themeicon").removeClass('jstree-themeicon-hidden');
  4874. return true;
  4875. }
  4876. };
  4877. // helpers
  4878. $.vakata = {};
  4879. // collect attributes
  4880. $.vakata.attributes = function(node, with_values) {
  4881. node = $(node)[0];
  4882. var attr = with_values ? {} : [];
  4883. if(node && node.attributes) {
  4884. $.each(node.attributes, function (i, v) {
  4885. if($.inArray(v.name.toLowerCase(),['style','contenteditable','hasfocus','tabindex']) !== -1) { return; }
  4886. if(v.value !== null && $.vakata.trim(v.value) !== '') {
  4887. if(with_values) { attr[v.name] = v.value; }
  4888. else { attr.push(v.name); }
  4889. }
  4890. });
  4891. }
  4892. return attr;
  4893. };
  4894. $.vakata.array_unique = function(array) {
  4895. var a = [], i, j, l, o = {};
  4896. for(i = 0, l = array.length; i < l; i++) {
  4897. if(o[array[i]] === undefined) {
  4898. a.push(array[i]);
  4899. o[array[i]] = true;
  4900. }
  4901. }
  4902. return a;
  4903. };
  4904. // remove item from array
  4905. $.vakata.array_remove = function(array, from) {
  4906. array.splice(from, 1);
  4907. return array;
  4908. //var rest = array.slice((to || from) + 1 || array.length);
  4909. //array.length = from < 0 ? array.length + from : from;
  4910. //array.push.apply(array, rest);
  4911. //return array;
  4912. };
  4913. // remove item from array
  4914. $.vakata.array_remove_item = function(array, item) {
  4915. var tmp = $.inArray(item, array);
  4916. return tmp !== -1 ? $.vakata.array_remove(array, tmp) : array;
  4917. };
  4918. $.vakata.array_filter = function(c,a,b,d,e) {
  4919. if (c.filter) {
  4920. return c.filter(a, b);
  4921. }
  4922. d=[];
  4923. for (e in c) {
  4924. if (~~e+''===e+'' && e>=0 && a.call(b,c[e],+e,c)) {
  4925. d.push(c[e]);
  4926. }
  4927. }
  4928. return d;
  4929. };
  4930. $.vakata.trim = function (text) {
  4931. return String.prototype.trim ?
  4932. String.prototype.trim.call(text.toString()) :
  4933. text.toString().replace(/^[\s\uFEFF\xA0]+|[\s\uFEFF\xA0]+$/g, '');
  4934. };
  4935. $.vakata.is_function = function(obj) {
  4936. return typeof obj === "function" && typeof obj.nodeType !== "number";
  4937. };
  4938. $.vakata.is_array = Array.isArray || function (obj) {
  4939. return Object.prototype.toString.call(obj) === "[object Array]";
  4940. };
  4941. // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_objects/Function/bind#polyfill
  4942. if (!Function.prototype.bind) {
  4943. Function.prototype.bind = function () {
  4944. var thatFunc = this, thatArg = arguments[0];
  4945. var args = Array.prototype.slice.call(arguments, 1);
  4946. if (typeof thatFunc !== 'function') {
  4947. // closest thing possible to the ECMAScript 5
  4948. // internal IsCallable function
  4949. throw new TypeError('Function.prototype.bind - what is trying to be bound is not callable');
  4950. }
  4951. return function(){
  4952. var funcArgs = args.concat(Array.prototype.slice.call(arguments));
  4953. return thatFunc.apply(thatArg, funcArgs);
  4954. };
  4955. };
  4956. }
  4957. /**
  4958. * ### Changed plugin
  4959. *
  4960. * 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.
  4961. */
  4962. $.jstree.plugins.changed = function (options, parent) {
  4963. var last = [];
  4964. this.trigger = function (ev, data) {
  4965. var i, j;
  4966. if(!data) {
  4967. data = {};
  4968. }
  4969. if(ev.replace('.jstree','') === 'changed') {
  4970. data.changed = { selected : [], deselected : [] };
  4971. var tmp = {};
  4972. for(i = 0, j = last.length; i < j; i++) {
  4973. tmp[last[i]] = 1;
  4974. }
  4975. for(i = 0, j = data.selected.length; i < j; i++) {
  4976. if(!tmp[data.selected[i]]) {
  4977. data.changed.selected.push(data.selected[i]);
  4978. }
  4979. else {
  4980. tmp[data.selected[i]] = 2;
  4981. }
  4982. }
  4983. for(i = 0, j = last.length; i < j; i++) {
  4984. if(tmp[last[i]] === 1) {
  4985. data.changed.deselected.push(last[i]);
  4986. }
  4987. }
  4988. last = data.selected.slice();
  4989. }
  4990. /**
  4991. * triggered when selection changes (the "changed" plugin enhances the original event with more data)
  4992. * @event
  4993. * @name changed.jstree
  4994. * @param {Object} node
  4995. * @param {Object} action the action that caused the selection to change
  4996. * @param {Array} selected the current selection
  4997. * @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
  4998. * @param {Object} event the event (if any) that triggered this changed event
  4999. * @plugin changed
  5000. */
  5001. parent.trigger.call(this, ev, data);
  5002. };
  5003. this.refresh = function (skip_loading, forget_state) {
  5004. last = [];
  5005. return parent.refresh.apply(this, arguments);
  5006. };
  5007. };
  5008. /**
  5009. * ### Checkbox plugin
  5010. *
  5011. * This plugin renders checkbox icons in front of each node, making multiple selection much easier.
  5012. * 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.
  5013. */
  5014. var _i = document.createElement('I');
  5015. _i.className = 'jstree-icon jstree-checkbox';
  5016. _i.setAttribute('role', 'presentation');
  5017. /**
  5018. * stores all defaults for the checkbox plugin
  5019. * @name $.jstree.defaults.checkbox
  5020. * @plugin checkbox
  5021. */
  5022. $.jstree.defaults.checkbox = {
  5023. /**
  5024. * a boolean indicating if checkboxes should be visible (can be changed at a later time using `show_checkboxes()` and `hide_checkboxes`). Defaults to `true`.
  5025. * @name $.jstree.defaults.checkbox.visible
  5026. * @plugin checkbox
  5027. */
  5028. visible : true,
  5029. /**
  5030. * a boolean indicating if checkboxes should cascade down and have an undetermined state. Defaults to `true`.
  5031. * @name $.jstree.defaults.checkbox.three_state
  5032. * @plugin checkbox
  5033. */
  5034. three_state : true,
  5035. /**
  5036. * a boolean indicating if clicking anywhere on the node should act as clicking on the checkbox. Defaults to `true`.
  5037. * @name $.jstree.defaults.checkbox.whole_node
  5038. * @plugin checkbox
  5039. */
  5040. whole_node : true,
  5041. /**
  5042. * a boolean indicating if the selected style of a node should be kept, or removed. Defaults to `true`.
  5043. * @name $.jstree.defaults.checkbox.keep_selected_style
  5044. * @plugin checkbox
  5045. */
  5046. keep_selected_style : true,
  5047. /**
  5048. * This setting controls how cascading and undetermined nodes are applied.
  5049. * 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.
  5050. * If `three_state` is set to `true` this setting is automatically set to 'up+down+undetermined'. Defaults to ''.
  5051. * @name $.jstree.defaults.checkbox.cascade
  5052. * @plugin checkbox
  5053. */
  5054. cascade : '',
  5055. /**
  5056. * 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.
  5057. * @name $.jstree.defaults.checkbox.tie_selection
  5058. * @plugin checkbox
  5059. */
  5060. tie_selection : true,
  5061. /**
  5062. * This setting controls if cascading down affects disabled checkboxes
  5063. * @name $.jstree.defaults.checkbox.cascade_to_disabled
  5064. * @plugin checkbox
  5065. */
  5066. cascade_to_disabled : true,
  5067. /**
  5068. * This setting controls if cascading down affects hidden checkboxes
  5069. * @name $.jstree.defaults.checkbox.cascade_to_hidden
  5070. * @plugin checkbox
  5071. */
  5072. cascade_to_hidden : true
  5073. };
  5074. $.jstree.plugins.checkbox = function (options, parent) {
  5075. this.bind = function () {
  5076. parent.bind.call(this);
  5077. this._data.checkbox.uto = false;
  5078. this._data.checkbox.selected = [];
  5079. if(this.settings.checkbox.three_state) {
  5080. this.settings.checkbox.cascade = 'up+down+undetermined';
  5081. }
  5082. this.element
  5083. .on("init.jstree", function () {
  5084. this._data.checkbox.visible = this.settings.checkbox.visible;
  5085. if(!this.settings.checkbox.keep_selected_style) {
  5086. this.element.addClass('jstree-checkbox-no-clicked');
  5087. }
  5088. if(this.settings.checkbox.tie_selection) {
  5089. this.element.addClass('jstree-checkbox-selection');
  5090. }
  5091. }.bind(this))
  5092. .on("loading.jstree", function () {
  5093. this[ this._data.checkbox.visible ? 'show_checkboxes' : 'hide_checkboxes' ]();
  5094. }.bind(this));
  5095. if(this.settings.checkbox.cascade.indexOf('undetermined') !== -1) {
  5096. this.element
  5097. .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', function () {
  5098. // only if undetermined is in setting
  5099. if(this._data.checkbox.uto) { clearTimeout(this._data.checkbox.uto); }
  5100. this._data.checkbox.uto = setTimeout(this._undetermined.bind(this), 50);
  5101. }.bind(this));
  5102. }
  5103. if(!this.settings.checkbox.tie_selection) {
  5104. this.element
  5105. .on('model.jstree', function (e, data) {
  5106. var m = this._model.data,
  5107. p = m[data.parent],
  5108. dpc = data.nodes,
  5109. i, j;
  5110. for(i = 0, j = dpc.length; i < j; i++) {
  5111. 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);
  5112. if(m[dpc[i]].state.checked) {
  5113. this._data.checkbox.selected.push(dpc[i]);
  5114. }
  5115. }
  5116. }.bind(this));
  5117. }
  5118. if(this.settings.checkbox.cascade.indexOf('up') !== -1 || this.settings.checkbox.cascade.indexOf('down') !== -1) {
  5119. this.element
  5120. .on('model.jstree', function (e, data) {
  5121. var m = this._model.data,
  5122. p = m[data.parent],
  5123. dpc = data.nodes,
  5124. chd = [],
  5125. c, i, j, k, l, tmp, s = this.settings.checkbox.cascade, t = this.settings.checkbox.tie_selection;
  5126. if(s.indexOf('down') !== -1) {
  5127. // apply down
  5128. if(p.state[ t ? 'selected' : 'checked' ]) {
  5129. for(i = 0, j = dpc.length; i < j; i++) {
  5130. m[dpc[i]].state[ t ? 'selected' : 'checked' ] = true;
  5131. }
  5132. this._data[ t ? 'core' : 'checkbox' ].selected = this._data[ t ? 'core' : 'checkbox' ].selected.concat(dpc);
  5133. }
  5134. else {
  5135. for(i = 0, j = dpc.length; i < j; i++) {
  5136. if(m[dpc[i]].state[ t ? 'selected' : 'checked' ]) {
  5137. for(k = 0, l = m[dpc[i]].children_d.length; k < l; k++) {
  5138. m[m[dpc[i]].children_d[k]].state[ t ? 'selected' : 'checked' ] = true;
  5139. }
  5140. this._data[ t ? 'core' : 'checkbox' ].selected = this._data[ t ? 'core' : 'checkbox' ].selected.concat(m[dpc[i]].children_d);
  5141. }
  5142. }
  5143. }
  5144. }
  5145. if(s.indexOf('up') !== -1) {
  5146. // apply up
  5147. for(i = 0, j = p.children_d.length; i < j; i++) {
  5148. if(!m[p.children_d[i]].children.length) {
  5149. chd.push(m[p.children_d[i]].parent);
  5150. }
  5151. }
  5152. chd = $.vakata.array_unique(chd);
  5153. for(k = 0, l = chd.length; k < l; k++) {
  5154. p = m[chd[k]];
  5155. while(p && p.id !== $.jstree.root) {
  5156. c = 0;
  5157. for(i = 0, j = p.children.length; i < j; i++) {
  5158. c += m[p.children[i]].state[ t ? 'selected' : 'checked' ];
  5159. }
  5160. if(c === j) {
  5161. p.state[ t ? 'selected' : 'checked' ] = true;
  5162. this._data[ t ? 'core' : 'checkbox' ].selected.push(p.id);
  5163. tmp = this.get_node(p, true);
  5164. if(tmp && tmp.length) {
  5165. tmp.children('.jstree-anchor').attr('aria-selected', true).addClass( t ? 'jstree-clicked' : 'jstree-checked');
  5166. }
  5167. }
  5168. else {
  5169. break;
  5170. }
  5171. p = this.get_node(p.parent);
  5172. }
  5173. }
  5174. }
  5175. this._data[ t ? 'core' : 'checkbox' ].selected = $.vakata.array_unique(this._data[ t ? 'core' : 'checkbox' ].selected);
  5176. }.bind(this))
  5177. .on(this.settings.checkbox.tie_selection ? 'select_node.jstree' : 'check_node.jstree', function (e, data) {
  5178. var self = this,
  5179. obj = data.node,
  5180. m = this._model.data,
  5181. par = this.get_node(obj.parent),
  5182. i, j, c, tmp, s = this.settings.checkbox.cascade, t = this.settings.checkbox.tie_selection,
  5183. sel = {}, cur = this._data[ t ? 'core' : 'checkbox' ].selected;
  5184. for (i = 0, j = cur.length; i < j; i++) {
  5185. sel[cur[i]] = true;
  5186. }
  5187. // apply down
  5188. if(s.indexOf('down') !== -1) {
  5189. //this._data[ t ? 'core' : 'checkbox' ].selected = $.vakata.array_unique(this._data[ t ? 'core' : 'checkbox' ].selected.concat(obj.children_d));
  5190. var selectedIds = this._cascade_new_checked_state(obj.id, true);
  5191. var temp = obj.children_d.concat(obj.id);
  5192. for (i = 0, j = temp.length; i < j; i++) {
  5193. if (selectedIds.indexOf(temp[i]) > -1) {
  5194. sel[temp[i]] = true;
  5195. }
  5196. else {
  5197. delete sel[temp[i]];
  5198. }
  5199. }
  5200. }
  5201. // apply up
  5202. if(s.indexOf('up') !== -1) {
  5203. while(par && par.id !== $.jstree.root) {
  5204. c = 0;
  5205. for(i = 0, j = par.children.length; i < j; i++) {
  5206. c += m[par.children[i]].state[ t ? 'selected' : 'checked' ];
  5207. }
  5208. if(c === j) {
  5209. par.state[ t ? 'selected' : 'checked' ] = true;
  5210. sel[par.id] = true;
  5211. //this._data[ t ? 'core' : 'checkbox' ].selected.push(par.id);
  5212. tmp = this.get_node(par, true);
  5213. if(tmp && tmp.length) {
  5214. tmp.children('.jstree-anchor').attr('aria-selected', true).addClass(t ? 'jstree-clicked' : 'jstree-checked');
  5215. }
  5216. }
  5217. else {
  5218. break;
  5219. }
  5220. par = this.get_node(par.parent);
  5221. }
  5222. }
  5223. cur = [];
  5224. for (i in sel) {
  5225. if (sel.hasOwnProperty(i)) {
  5226. cur.push(i);
  5227. }
  5228. }
  5229. this._data[ t ? 'core' : 'checkbox' ].selected = cur;
  5230. }.bind(this))
  5231. .on(this.settings.checkbox.tie_selection ? 'deselect_all.jstree' : 'uncheck_all.jstree', function (e, data) {
  5232. var obj = this.get_node($.jstree.root),
  5233. m = this._model.data,
  5234. i, j, tmp;
  5235. for(i = 0, j = obj.children_d.length; i < j; i++) {
  5236. tmp = m[obj.children_d[i]];
  5237. if(tmp && tmp.original && tmp.original.state && tmp.original.state.undetermined) {
  5238. tmp.original.state.undetermined = false;
  5239. }
  5240. }
  5241. }.bind(this))
  5242. .on(this.settings.checkbox.tie_selection ? 'deselect_node.jstree' : 'uncheck_node.jstree', function (e, data) {
  5243. var self = this,
  5244. obj = data.node,
  5245. dom = this.get_node(obj, true),
  5246. i, j, tmp, s = this.settings.checkbox.cascade, t = this.settings.checkbox.tie_selection,
  5247. cur = this._data[ t ? 'core' : 'checkbox' ].selected, sel = {},
  5248. stillSelectedIds = [],
  5249. allIds = obj.children_d.concat(obj.id);
  5250. // apply down
  5251. if(s.indexOf('down') !== -1) {
  5252. var selectedIds = this._cascade_new_checked_state(obj.id, false);
  5253. cur = $.vakata.array_filter(cur, function(id) {
  5254. return allIds.indexOf(id) === -1 || selectedIds.indexOf(id) > -1;
  5255. });
  5256. }
  5257. // only apply up if cascade up is enabled and if this node is not selected
  5258. // (if all child nodes are disabled and cascade_to_disabled === false then this node will till be selected).
  5259. if(s.indexOf('up') !== -1 && cur.indexOf(obj.id) === -1) {
  5260. for(i = 0, j = obj.parents.length; i < j; i++) {
  5261. tmp = this._model.data[obj.parents[i]];
  5262. tmp.state[ t ? 'selected' : 'checked' ] = false;
  5263. if(tmp && tmp.original && tmp.original.state && tmp.original.state.undetermined) {
  5264. tmp.original.state.undetermined = false;
  5265. }
  5266. tmp = this.get_node(obj.parents[i], true);
  5267. if(tmp && tmp.length) {
  5268. tmp.children('.jstree-anchor').attr('aria-selected', false).removeClass(t ? 'jstree-clicked' : 'jstree-checked');
  5269. }
  5270. }
  5271. cur = $.vakata.array_filter(cur, function(id) {
  5272. return obj.parents.indexOf(id) === -1;
  5273. });
  5274. }
  5275. this._data[ t ? 'core' : 'checkbox' ].selected = cur;
  5276. }.bind(this));
  5277. }
  5278. if(this.settings.checkbox.cascade.indexOf('up') !== -1) {
  5279. this.element
  5280. .on('delete_node.jstree', function (e, data) {
  5281. // apply up (whole handler)
  5282. var p = this.get_node(data.parent),
  5283. m = this._model.data,
  5284. i, j, c, tmp, t = this.settings.checkbox.tie_selection;
  5285. while(p && p.id !== $.jstree.root && !p.state[ t ? 'selected' : 'checked' ]) {
  5286. c = 0;
  5287. for(i = 0, j = p.children.length; i < j; i++) {
  5288. c += m[p.children[i]].state[ t ? 'selected' : 'checked' ];
  5289. }
  5290. if(j > 0 && c === j) {
  5291. p.state[ t ? 'selected' : 'checked' ] = true;
  5292. this._data[ t ? 'core' : 'checkbox' ].selected.push(p.id);
  5293. tmp = this.get_node(p, true);
  5294. if(tmp && tmp.length) {
  5295. tmp.children('.jstree-anchor').attr('aria-selected', true).addClass(t ? 'jstree-clicked' : 'jstree-checked');
  5296. }
  5297. }
  5298. else {
  5299. break;
  5300. }
  5301. p = this.get_node(p.parent);
  5302. }
  5303. }.bind(this))
  5304. .on('move_node.jstree', function (e, data) {
  5305. // apply up (whole handler)
  5306. var is_multi = data.is_multi,
  5307. old_par = data.old_parent,
  5308. new_par = this.get_node(data.parent),
  5309. m = this._model.data,
  5310. p, c, i, j, tmp, t = this.settings.checkbox.tie_selection;
  5311. if(!is_multi) {
  5312. p = this.get_node(old_par);
  5313. while(p && p.id !== $.jstree.root && !p.state[ t ? 'selected' : 'checked' ]) {
  5314. c = 0;
  5315. for(i = 0, j = p.children.length; i < j; i++) {
  5316. c += m[p.children[i]].state[ t ? 'selected' : 'checked' ];
  5317. }
  5318. if(j > 0 && c === j) {
  5319. p.state[ t ? 'selected' : 'checked' ] = true;
  5320. this._data[ t ? 'core' : 'checkbox' ].selected.push(p.id);
  5321. tmp = this.get_node(p, true);
  5322. if(tmp && tmp.length) {
  5323. tmp.children('.jstree-anchor').attr('aria-selected', true).addClass(t ? 'jstree-clicked' : 'jstree-checked');
  5324. }
  5325. }
  5326. else {
  5327. break;
  5328. }
  5329. p = this.get_node(p.parent);
  5330. }
  5331. }
  5332. p = new_par;
  5333. while(p && p.id !== $.jstree.root) {
  5334. c = 0;
  5335. for(i = 0, j = p.children.length; i < j; i++) {
  5336. c += m[p.children[i]].state[ t ? 'selected' : 'checked' ];
  5337. }
  5338. if(c === j) {
  5339. if(!p.state[ t ? 'selected' : 'checked' ]) {
  5340. p.state[ t ? 'selected' : 'checked' ] = true;
  5341. this._data[ t ? 'core' : 'checkbox' ].selected.push(p.id);
  5342. tmp = this.get_node(p, true);
  5343. if(tmp && tmp.length) {
  5344. tmp.children('.jstree-anchor').attr('aria-selected', true).addClass(t ? 'jstree-clicked' : 'jstree-checked');
  5345. }
  5346. }
  5347. }
  5348. else {
  5349. if(p.state[ t ? 'selected' : 'checked' ]) {
  5350. p.state[ t ? 'selected' : 'checked' ] = false;
  5351. this._data[ t ? 'core' : 'checkbox' ].selected = $.vakata.array_remove_item(this._data[ t ? 'core' : 'checkbox' ].selected, p.id);
  5352. tmp = this.get_node(p, true);
  5353. if(tmp && tmp.length) {
  5354. tmp.children('.jstree-anchor').attr('aria-selected', false).removeClass(t ? 'jstree-clicked' : 'jstree-checked');
  5355. }
  5356. }
  5357. else {
  5358. break;
  5359. }
  5360. }
  5361. p = this.get_node(p.parent);
  5362. }
  5363. }.bind(this));
  5364. }
  5365. };
  5366. /**
  5367. * get an array of all nodes whose state is "undetermined"
  5368. * @name get_undetermined([full])
  5369. * @param {boolean} full: if set to `true` the returned array will consist of the full node objects, otherwise - only IDs will be returned
  5370. * @return {Array}
  5371. * @plugin checkbox
  5372. */
  5373. this.get_undetermined = function (full) {
  5374. if (this.settings.checkbox.cascade.indexOf('undetermined') === -1) {
  5375. return [];
  5376. }
  5377. 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, r = [];
  5378. for(i = 0, j = s.length; i < j; i++) {
  5379. if(m[s[i]] && m[s[i]].parents) {
  5380. for(k = 0, l = m[s[i]].parents.length; k < l; k++) {
  5381. if(o[m[s[i]].parents[k]] !== undefined) {
  5382. break;
  5383. }
  5384. if(m[s[i]].parents[k] !== $.jstree.root) {
  5385. o[m[s[i]].parents[k]] = true;
  5386. p.push(m[s[i]].parents[k]);
  5387. }
  5388. }
  5389. }
  5390. }
  5391. // attempt for server side undetermined state
  5392. this.element.find('.jstree-closed').not(':has(.jstree-children)')
  5393. .each(function () {
  5394. var tmp = tt.get_node(this), tmp2;
  5395. if(!tmp) { return; }
  5396. if(!tmp.state.loaded) {
  5397. if(tmp.original && tmp.original.state && tmp.original.state.undetermined && tmp.original.state.undetermined === true) {
  5398. if(o[tmp.id] === undefined && tmp.id !== $.jstree.root) {
  5399. o[tmp.id] = true;
  5400. p.push(tmp.id);
  5401. }
  5402. for(k = 0, l = tmp.parents.length; k < l; k++) {
  5403. if(o[tmp.parents[k]] === undefined && tmp.parents[k] !== $.jstree.root) {
  5404. o[tmp.parents[k]] = true;
  5405. p.push(tmp.parents[k]);
  5406. }
  5407. }
  5408. }
  5409. }
  5410. else {
  5411. for(i = 0, j = tmp.children_d.length; i < j; i++) {
  5412. tmp2 = m[tmp.children_d[i]];
  5413. if(!tmp2.state.loaded && tmp2.original && tmp2.original.state && tmp2.original.state.undetermined && tmp2.original.state.undetermined === true) {
  5414. if(o[tmp2.id] === undefined && tmp2.id !== $.jstree.root) {
  5415. o[tmp2.id] = true;
  5416. p.push(tmp2.id);
  5417. }
  5418. for(k = 0, l = tmp2.parents.length; k < l; k++) {
  5419. if(o[tmp2.parents[k]] === undefined && tmp2.parents[k] !== $.jstree.root) {
  5420. o[tmp2.parents[k]] = true;
  5421. p.push(tmp2.parents[k]);
  5422. }
  5423. }
  5424. }
  5425. }
  5426. }
  5427. });
  5428. for (i = 0, j = p.length; i < j; i++) {
  5429. if(!m[p[i]].state[ t ? 'selected' : 'checked' ]) {
  5430. r.push(full ? m[p[i]] : p[i]);
  5431. }
  5432. }
  5433. return r;
  5434. };
  5435. /**
  5436. * set the undetermined state where and if necessary. Used internally.
  5437. * @private
  5438. * @name _undetermined()
  5439. * @plugin checkbox
  5440. */
  5441. this._undetermined = function () {
  5442. if(this.element === null) { return; }
  5443. var p = this.get_undetermined(false), i, j, s;
  5444. this.element.find('.jstree-undetermined').removeClass('jstree-undetermined');
  5445. for (i = 0, j = p.length; i < j; i++) {
  5446. s = this.get_node(p[i], true);
  5447. if(s && s.length) {
  5448. s.children('.jstree-anchor').children('.jstree-checkbox').addClass('jstree-undetermined');
  5449. }
  5450. }
  5451. };
  5452. this.redraw_node = function(obj, deep, is_callback, force_render) {
  5453. obj = parent.redraw_node.apply(this, arguments);
  5454. if(obj) {
  5455. var i, j, tmp = null, icon = null;
  5456. for(i = 0, j = obj.childNodes.length; i < j; i++) {
  5457. if(obj.childNodes[i] && obj.childNodes[i].className && obj.childNodes[i].className.indexOf("jstree-anchor") !== -1) {
  5458. tmp = obj.childNodes[i];
  5459. break;
  5460. }
  5461. }
  5462. if(tmp) {
  5463. if(!this.settings.checkbox.tie_selection && this._model.data[obj.id].state.checked) { tmp.className += ' jstree-checked'; }
  5464. icon = _i.cloneNode(false);
  5465. if(this._model.data[obj.id].state.checkbox_disabled) { icon.className += ' jstree-checkbox-disabled'; }
  5466. tmp.insertBefore(icon, tmp.childNodes[0]);
  5467. }
  5468. }
  5469. if(!is_callback && this.settings.checkbox.cascade.indexOf('undetermined') !== -1) {
  5470. if(this._data.checkbox.uto) { clearTimeout(this._data.checkbox.uto); }
  5471. this._data.checkbox.uto = setTimeout(this._undetermined.bind(this), 50);
  5472. }
  5473. return obj;
  5474. };
  5475. /**
  5476. * show the node checkbox icons
  5477. * @name show_checkboxes()
  5478. * @plugin checkbox
  5479. */
  5480. this.show_checkboxes = function () { this._data.core.themes.checkboxes = true; this.get_container_ul().removeClass("jstree-no-checkboxes"); };
  5481. /**
  5482. * hide the node checkbox icons
  5483. * @name hide_checkboxes()
  5484. * @plugin checkbox
  5485. */
  5486. this.hide_checkboxes = function () { this._data.core.themes.checkboxes = false; this.get_container_ul().addClass("jstree-no-checkboxes"); };
  5487. /**
  5488. * toggle the node icons
  5489. * @name toggle_checkboxes()
  5490. * @plugin checkbox
  5491. */
  5492. this.toggle_checkboxes = function () { if(this._data.core.themes.checkboxes) { this.hide_checkboxes(); } else { this.show_checkboxes(); } };
  5493. /**
  5494. * checks if a node is in an undetermined state
  5495. * @name is_undetermined(obj)
  5496. * @param {mixed} obj
  5497. * @return {Boolean}
  5498. */
  5499. this.is_undetermined = function (obj) {
  5500. obj = this.get_node(obj);
  5501. 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;
  5502. if(!obj || obj.state[ t ? 'selected' : 'checked' ] === true || s.indexOf('undetermined') === -1 || (s.indexOf('down') === -1 && s.indexOf('up') === -1)) {
  5503. return false;
  5504. }
  5505. if(!obj.state.loaded && obj.original.state.undetermined === true) {
  5506. return true;
  5507. }
  5508. for(i = 0, j = obj.children_d.length; i < j; i++) {
  5509. if($.inArray(obj.children_d[i], d) !== -1 || (!m[obj.children_d[i]].state.loaded && m[obj.children_d[i]].original.state.undetermined)) {
  5510. return true;
  5511. }
  5512. }
  5513. return false;
  5514. };
  5515. /**
  5516. * disable a node's checkbox
  5517. * @name disable_checkbox(obj)
  5518. * @param {mixed} obj an array can be used too
  5519. * @trigger disable_checkbox.jstree
  5520. * @plugin checkbox
  5521. */
  5522. this.disable_checkbox = function (obj) {
  5523. var t1, t2, dom;
  5524. if($.vakata.is_array(obj)) {
  5525. obj = obj.slice();
  5526. for(t1 = 0, t2 = obj.length; t1 < t2; t1++) {
  5527. this.disable_checkbox(obj[t1]);
  5528. }
  5529. return true;
  5530. }
  5531. obj = this.get_node(obj);
  5532. if(!obj || obj.id === $.jstree.root) {
  5533. return false;
  5534. }
  5535. dom = this.get_node(obj, true);
  5536. if(!obj.state.checkbox_disabled) {
  5537. obj.state.checkbox_disabled = true;
  5538. if(dom && dom.length) {
  5539. dom.children('.jstree-anchor').children('.jstree-checkbox').addClass('jstree-checkbox-disabled');
  5540. }
  5541. /**
  5542. * triggered when an node's checkbox is disabled
  5543. * @event
  5544. * @name disable_checkbox.jstree
  5545. * @param {Object} node
  5546. * @plugin checkbox
  5547. */
  5548. this.trigger('disable_checkbox', { 'node' : obj });
  5549. }
  5550. };
  5551. /**
  5552. * enable a node's checkbox
  5553. * @name enable_checkbox(obj)
  5554. * @param {mixed} obj an array can be used too
  5555. * @trigger enable_checkbox.jstree
  5556. * @plugin checkbox
  5557. */
  5558. this.enable_checkbox = function (obj) {
  5559. var t1, t2, dom;
  5560. if($.vakata.is_array(obj)) {
  5561. obj = obj.slice();
  5562. for(t1 = 0, t2 = obj.length; t1 < t2; t1++) {
  5563. this.enable_checkbox(obj[t1]);
  5564. }
  5565. return true;
  5566. }
  5567. obj = this.get_node(obj);
  5568. if(!obj || obj.id === $.jstree.root) {
  5569. return false;
  5570. }
  5571. dom = this.get_node(obj, true);
  5572. if(obj.state.checkbox_disabled) {
  5573. obj.state.checkbox_disabled = false;
  5574. if(dom && dom.length) {
  5575. dom.children('.jstree-anchor').children('.jstree-checkbox').removeClass('jstree-checkbox-disabled');
  5576. }
  5577. /**
  5578. * triggered when an node's checkbox is enabled
  5579. * @event
  5580. * @name enable_checkbox.jstree
  5581. * @param {Object} node
  5582. * @plugin checkbox
  5583. */
  5584. this.trigger('enable_checkbox', { 'node' : obj });
  5585. }
  5586. };
  5587. this.activate_node = function (obj, e) {
  5588. if($(e.target).hasClass('jstree-checkbox-disabled')) {
  5589. return false;
  5590. }
  5591. if(this.settings.checkbox.tie_selection && (this.settings.checkbox.whole_node || $(e.target).hasClass('jstree-checkbox'))) {
  5592. e.ctrlKey = true;
  5593. }
  5594. if(this.settings.checkbox.tie_selection || (!this.settings.checkbox.whole_node && !$(e.target).hasClass('jstree-checkbox'))) {
  5595. return parent.activate_node.call(this, obj, e);
  5596. }
  5597. if(this.is_disabled(obj)) {
  5598. return false;
  5599. }
  5600. if(this.is_checked(obj)) {
  5601. this.uncheck_node(obj, e);
  5602. }
  5603. else {
  5604. this.check_node(obj, e);
  5605. }
  5606. this.trigger('activate_node', { 'node' : this.get_node(obj) });
  5607. };
  5608. this.delete_node = function (obj) {
  5609. if(this.settings.checkbox.tie_selection || $.vakata.is_array(obj)) {
  5610. return parent.delete_node.call(this, obj);
  5611. }
  5612. var tmp, k, l, c = false;
  5613. obj = this.get_node(obj);
  5614. if(!obj || obj.id === $.jstree.root) { return false; }
  5615. tmp = obj.children_d.concat([]);
  5616. tmp.push(obj.id);
  5617. for(k = 0, l = tmp.length; k < l; k++) {
  5618. if(this._model.data[tmp[k]].state.checked) {
  5619. c = true;
  5620. break;
  5621. }
  5622. }
  5623. if (c) {
  5624. this._data.checkbox.selected = $.vakata.array_filter(this._data.checkbox.selected, function (v) {
  5625. return $.inArray(v, tmp) === -1;
  5626. });
  5627. }
  5628. return parent.delete_node.call(this, obj);
  5629. };
  5630. /**
  5631. * Cascades checked state to a node and all its descendants. This function does NOT affect hidden and disabled nodes (or their descendants).
  5632. * However if these unaffected nodes are already selected their ids will be included in the returned array.
  5633. * @private
  5634. * @name _cascade_new_checked_state(id, checkedState)
  5635. * @param {string} id the node ID
  5636. * @param {bool} checkedState should the nodes be checked or not
  5637. * @returns {Array} Array of all node id's (in this tree branch) that are checked.
  5638. */
  5639. this._cascade_new_checked_state = function (id, checkedState) {
  5640. var self = this;
  5641. var t = this.settings.checkbox.tie_selection;
  5642. var node = this._model.data[id];
  5643. var selectedNodeIds = [];
  5644. var selectedChildrenIds = [], i, j, selectedChildIds;
  5645. if (
  5646. (this.settings.checkbox.cascade_to_disabled || !node.state.disabled) &&
  5647. (this.settings.checkbox.cascade_to_hidden || !node.state.hidden)
  5648. ) {
  5649. //First try and check/uncheck the children
  5650. if (node.children) {
  5651. for (i = 0, j = node.children.length; i < j; i++) {
  5652. var childId = node.children[i];
  5653. selectedChildIds = self._cascade_new_checked_state(childId, checkedState);
  5654. selectedNodeIds = selectedNodeIds.concat(selectedChildIds);
  5655. if (selectedChildIds.indexOf(childId) > -1) {
  5656. selectedChildrenIds.push(childId);
  5657. }
  5658. }
  5659. }
  5660. var dom = self.get_node(node, true);
  5661. //A node's state is undetermined if some but not all of it's children are checked/selected .
  5662. var undetermined = selectedChildrenIds.length > 0 && selectedChildrenIds.length < node.children.length;
  5663. if(node.original && node.original.state && node.original.state.undetermined) {
  5664. node.original.state.undetermined = undetermined;
  5665. }
  5666. //If a node is undetermined then remove selected class
  5667. if (undetermined) {
  5668. node.state[ t ? 'selected' : 'checked' ] = false;
  5669. dom.children('.jstree-anchor').attr('aria-selected', false).removeClass(t ? 'jstree-clicked' : 'jstree-checked');
  5670. }
  5671. //Otherwise, if the checkedState === true (i.e. the node is being checked now) and all of the node's children are checked (if it has any children),
  5672. //check the node and style it correctly.
  5673. else if (checkedState && selectedChildrenIds.length === node.children.length) {
  5674. node.state[ t ? 'selected' : 'checked' ] = checkedState;
  5675. selectedNodeIds.push(node.id);
  5676. dom.children('.jstree-anchor').attr('aria-selected', true).addClass(t ? 'jstree-clicked' : 'jstree-checked');
  5677. }
  5678. else {
  5679. node.state[ t ? 'selected' : 'checked' ] = false;
  5680. dom.children('.jstree-anchor').attr('aria-selected', false).removeClass(t ? 'jstree-clicked' : 'jstree-checked');
  5681. }
  5682. }
  5683. else {
  5684. selectedChildIds = this.get_checked_descendants(id);
  5685. if (node.state[ t ? 'selected' : 'checked' ]) {
  5686. selectedChildIds.push(node.id);
  5687. }
  5688. selectedNodeIds = selectedNodeIds.concat(selectedChildIds);
  5689. }
  5690. return selectedNodeIds;
  5691. };
  5692. /**
  5693. * Gets ids of nodes selected in branch (of tree) specified by id (does not include the node specified by id)
  5694. * @name get_checked_descendants(obj)
  5695. * @param {string} id the node ID
  5696. * @return {Array} array of IDs
  5697. * @plugin checkbox
  5698. */
  5699. this.get_checked_descendants = function (id) {
  5700. var self = this;
  5701. var t = self.settings.checkbox.tie_selection;
  5702. var node = self._model.data[id];
  5703. return $.vakata.array_filter(node.children_d, function(_id) {
  5704. return self._model.data[_id].state[ t ? 'selected' : 'checked' ];
  5705. });
  5706. };
  5707. /**
  5708. * check a node (only if tie_selection in checkbox settings is false, otherwise select_node will be called internally)
  5709. * @name check_node(obj)
  5710. * @param {mixed} obj an array can be used to check multiple nodes
  5711. * @trigger check_node.jstree
  5712. * @plugin checkbox
  5713. */
  5714. this.check_node = function (obj, e) {
  5715. if(this.settings.checkbox.tie_selection) { return this.select_node(obj, false, true, e); }
  5716. var dom, t1, t2, th;
  5717. if($.vakata.is_array(obj)) {
  5718. obj = obj.slice();
  5719. for(t1 = 0, t2 = obj.length; t1 < t2; t1++) {
  5720. this.check_node(obj[t1], e);
  5721. }
  5722. return true;
  5723. }
  5724. obj = this.get_node(obj);
  5725. if(!obj || obj.id === $.jstree.root) {
  5726. return false;
  5727. }
  5728. dom = this.get_node(obj, true);
  5729. if(!obj.state.checked) {
  5730. obj.state.checked = true;
  5731. this._data.checkbox.selected.push(obj.id);
  5732. if(dom && dom.length) {
  5733. dom.children('.jstree-anchor').addClass('jstree-checked');
  5734. }
  5735. /**
  5736. * triggered when an node is checked (only if tie_selection in checkbox settings is false)
  5737. * @event
  5738. * @name check_node.jstree
  5739. * @param {Object} node
  5740. * @param {Array} selected the current selection
  5741. * @param {Object} event the event (if any) that triggered this check_node
  5742. * @plugin checkbox
  5743. */
  5744. this.trigger('check_node', { 'node' : obj, 'selected' : this._data.checkbox.selected, 'event' : e });
  5745. }
  5746. };
  5747. /**
  5748. * uncheck a node (only if tie_selection in checkbox settings is false, otherwise deselect_node will be called internally)
  5749. * @name uncheck_node(obj)
  5750. * @param {mixed} obj an array can be used to uncheck multiple nodes
  5751. * @trigger uncheck_node.jstree
  5752. * @plugin checkbox
  5753. */
  5754. this.uncheck_node = function (obj, e) {
  5755. if(this.settings.checkbox.tie_selection) { return this.deselect_node(obj, false, e); }
  5756. var t1, t2, dom;
  5757. if($.vakata.is_array(obj)) {
  5758. obj = obj.slice();
  5759. for(t1 = 0, t2 = obj.length; t1 < t2; t1++) {
  5760. this.uncheck_node(obj[t1], e);
  5761. }
  5762. return true;
  5763. }
  5764. obj = this.get_node(obj);
  5765. if(!obj || obj.id === $.jstree.root) {
  5766. return false;
  5767. }
  5768. dom = this.get_node(obj, true);
  5769. if(obj.state.checked) {
  5770. obj.state.checked = false;
  5771. this._data.checkbox.selected = $.vakata.array_remove_item(this._data.checkbox.selected, obj.id);
  5772. if(dom.length) {
  5773. dom.children('.jstree-anchor').removeClass('jstree-checked');
  5774. }
  5775. /**
  5776. * triggered when an node is unchecked (only if tie_selection in checkbox settings is false)
  5777. * @event
  5778. * @name uncheck_node.jstree
  5779. * @param {Object} node
  5780. * @param {Array} selected the current selection
  5781. * @param {Object} event the event (if any) that triggered this uncheck_node
  5782. * @plugin checkbox
  5783. */
  5784. this.trigger('uncheck_node', { 'node' : obj, 'selected' : this._data.checkbox.selected, 'event' : e });
  5785. }
  5786. };
  5787. /**
  5788. * checks all nodes in the tree (only if tie_selection in checkbox settings is false, otherwise select_all will be called internally)
  5789. * @name check_all()
  5790. * @trigger check_all.jstree, changed.jstree
  5791. * @plugin checkbox
  5792. */
  5793. this.check_all = function () {
  5794. if(this.settings.checkbox.tie_selection) { return this.select_all(); }
  5795. var tmp = this._data.checkbox.selected.concat([]), i, j;
  5796. this._data.checkbox.selected = this._model.data[$.jstree.root].children_d.concat();
  5797. for(i = 0, j = this._data.checkbox.selected.length; i < j; i++) {
  5798. if(this._model.data[this._data.checkbox.selected[i]]) {
  5799. this._model.data[this._data.checkbox.selected[i]].state.checked = true;
  5800. }
  5801. }
  5802. this.redraw(true);
  5803. /**
  5804. * triggered when all nodes are checked (only if tie_selection in checkbox settings is false)
  5805. * @event
  5806. * @name check_all.jstree
  5807. * @param {Array} selected the current selection
  5808. * @plugin checkbox
  5809. */
  5810. this.trigger('check_all', { 'selected' : this._data.checkbox.selected });
  5811. };
  5812. /**
  5813. * uncheck all checked nodes (only if tie_selection in checkbox settings is false, otherwise deselect_all will be called internally)
  5814. * @name uncheck_all()
  5815. * @trigger uncheck_all.jstree
  5816. * @plugin checkbox
  5817. */
  5818. this.uncheck_all = function () {
  5819. if(this.settings.checkbox.tie_selection) { return this.deselect_all(); }
  5820. var tmp = this._data.checkbox.selected.concat([]), i, j;
  5821. for(i = 0, j = this._data.checkbox.selected.length; i < j; i++) {
  5822. if(this._model.data[this._data.checkbox.selected[i]]) {
  5823. this._model.data[this._data.checkbox.selected[i]].state.checked = false;
  5824. }
  5825. }
  5826. this._data.checkbox.selected = [];
  5827. this.element.find('.jstree-checked').removeClass('jstree-checked');
  5828. /**
  5829. * triggered when all nodes are unchecked (only if tie_selection in checkbox settings is false)
  5830. * @event
  5831. * @name uncheck_all.jstree
  5832. * @param {Object} node the previous selection
  5833. * @param {Array} selected the current selection
  5834. * @plugin checkbox
  5835. */
  5836. this.trigger('uncheck_all', { 'selected' : this._data.checkbox.selected, 'node' : tmp });
  5837. };
  5838. /**
  5839. * checks if a node is checked (if tie_selection is on in the settings this function will return the same as is_selected)
  5840. * @name is_checked(obj)
  5841. * @param {mixed} obj
  5842. * @return {Boolean}
  5843. * @plugin checkbox
  5844. */
  5845. this.is_checked = function (obj) {
  5846. if(this.settings.checkbox.tie_selection) { return this.is_selected(obj); }
  5847. obj = this.get_node(obj);
  5848. if(!obj || obj.id === $.jstree.root) { return false; }
  5849. return obj.state.checked;
  5850. };
  5851. /**
  5852. * get an array of all checked nodes (if tie_selection is on in the settings this function will return the same as get_selected)
  5853. * @name get_checked([full])
  5854. * @param {mixed} full if set to `true` the returned array will consist of the full node objects, otherwise - only IDs will be returned
  5855. * @return {Array}
  5856. * @plugin checkbox
  5857. */
  5858. this.get_checked = function (full) {
  5859. if(this.settings.checkbox.tie_selection) { return this.get_selected(full); }
  5860. return full ? $.map(this._data.checkbox.selected, function (i) { return this.get_node(i); }.bind(this)) : this._data.checkbox.selected.slice();
  5861. };
  5862. /**
  5863. * 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)
  5864. * @name get_top_checked([full])
  5865. * @param {mixed} full if set to `true` the returned array will consist of the full node objects, otherwise - only IDs will be returned
  5866. * @return {Array}
  5867. * @plugin checkbox
  5868. */
  5869. this.get_top_checked = function (full) {
  5870. if(this.settings.checkbox.tie_selection) { return this.get_top_selected(full); }
  5871. var tmp = this.get_checked(true),
  5872. obj = {}, i, j, k, l;
  5873. for(i = 0, j = tmp.length; i < j; i++) {
  5874. obj[tmp[i].id] = tmp[i];
  5875. }
  5876. for(i = 0, j = tmp.length; i < j; i++) {
  5877. for(k = 0, l = tmp[i].children_d.length; k < l; k++) {
  5878. if(obj[tmp[i].children_d[k]]) {
  5879. delete obj[tmp[i].children_d[k]];
  5880. }
  5881. }
  5882. }
  5883. tmp = [];
  5884. for(i in obj) {
  5885. if(obj.hasOwnProperty(i)) {
  5886. tmp.push(i);
  5887. }
  5888. }
  5889. return full ? $.map(tmp, function (i) { return this.get_node(i); }.bind(this)) : tmp;
  5890. };
  5891. /**
  5892. * 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)
  5893. * @name get_bottom_checked([full])
  5894. * @param {mixed} full if set to `true` the returned array will consist of the full node objects, otherwise - only IDs will be returned
  5895. * @return {Array}
  5896. * @plugin checkbox
  5897. */
  5898. this.get_bottom_checked = function (full) {
  5899. if(this.settings.checkbox.tie_selection) { return this.get_bottom_selected(full); }
  5900. var tmp = this.get_checked(true),
  5901. obj = [], i, j;
  5902. for(i = 0, j = tmp.length; i < j; i++) {
  5903. if(!tmp[i].children.length) {
  5904. obj.push(tmp[i].id);
  5905. }
  5906. }
  5907. return full ? $.map(obj, function (i) { return this.get_node(i); }.bind(this)) : obj;
  5908. };
  5909. this.load_node = function (obj, callback) {
  5910. var k, l, i, j, c, tmp;
  5911. if(!$.vakata.is_array(obj) && !this.settings.checkbox.tie_selection) {
  5912. tmp = this.get_node(obj);
  5913. if(tmp && tmp.state.loaded) {
  5914. for(k = 0, l = tmp.children_d.length; k < l; k++) {
  5915. if(this._model.data[tmp.children_d[k]].state.checked) {
  5916. c = true;
  5917. this._data.checkbox.selected = $.vakata.array_remove_item(this._data.checkbox.selected, tmp.children_d[k]);
  5918. }
  5919. }
  5920. }
  5921. }
  5922. return parent.load_node.apply(this, arguments);
  5923. };
  5924. this.get_state = function () {
  5925. var state = parent.get_state.apply(this, arguments);
  5926. if(this.settings.checkbox.tie_selection) { return state; }
  5927. state.checkbox = this._data.checkbox.selected.slice();
  5928. return state;
  5929. };
  5930. this.set_state = function (state, callback) {
  5931. var res = parent.set_state.apply(this, arguments);
  5932. if(res && state.checkbox) {
  5933. if(!this.settings.checkbox.tie_selection) {
  5934. this.uncheck_all();
  5935. var _this = this;
  5936. $.each(state.checkbox, function (i, v) {
  5937. _this.check_node(v);
  5938. });
  5939. }
  5940. delete state.checkbox;
  5941. this.set_state(state, callback);
  5942. return false;
  5943. }
  5944. return res;
  5945. };
  5946. this.refresh = function (skip_loading, forget_state) {
  5947. if(this.settings.checkbox.tie_selection) {
  5948. this._data.checkbox.selected = [];
  5949. }
  5950. return parent.refresh.apply(this, arguments);
  5951. };
  5952. };
  5953. // include the checkbox plugin by default
  5954. // $.jstree.defaults.plugins.push("checkbox");
  5955. /**
  5956. * ### Conditionalselect plugin
  5957. *
  5958. * This plugin allows defining a callback to allow or deny node selection by user input (activate node method).
  5959. */
  5960. /**
  5961. * 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`.
  5962. * @name $.jstree.defaults.checkbox.visible
  5963. * @plugin checkbox
  5964. */
  5965. $.jstree.defaults.conditionalselect = function () { return true; };
  5966. $.jstree.plugins.conditionalselect = function (options, parent) {
  5967. // own function
  5968. this.activate_node = function (obj, e) {
  5969. if(this.settings.conditionalselect.call(this, this.get_node(obj), e)) {
  5970. return parent.activate_node.call(this, obj, e);
  5971. }
  5972. };
  5973. };
  5974. /**
  5975. * ### Contextmenu plugin
  5976. *
  5977. * Shows a context menu when a node is right-clicked.
  5978. */
  5979. /**
  5980. * stores all defaults for the contextmenu plugin
  5981. * @name $.jstree.defaults.contextmenu
  5982. * @plugin contextmenu
  5983. */
  5984. $.jstree.defaults.contextmenu = {
  5985. /**
  5986. * a boolean indicating if the node should be selected when the context menu is invoked on it. Defaults to `true`.
  5987. * @name $.jstree.defaults.contextmenu.select_node
  5988. * @plugin contextmenu
  5989. */
  5990. select_node : true,
  5991. /**
  5992. * a boolean indicating if the menu should be shown aligned with the node. Defaults to `true`, otherwise the mouse coordinates are used.
  5993. * @name $.jstree.defaults.contextmenu.show_at_node
  5994. * @plugin contextmenu
  5995. */
  5996. show_at_node : true,
  5997. /**
  5998. * 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).
  5999. *
  6000. * Each action consists of a key (a unique name) and a value which is an object with the following properties (only label and action are required). Once a menu item is activated the `action` function will be invoked with an object containing the following keys: item - the contextmenu item definition as seen below, reference - the DOM node that was used (the tree node), element - the contextmenu DOM element, position - an object with x/y properties indicating the position of the menu.
  6001. *
  6002. * * `separator_before` - a boolean indicating if there should be a separator before this item
  6003. * * `separator_after` - a boolean indicating if there should be a separator after this item
  6004. * * `_disabled` - a boolean indicating if this action should be disabled
  6005. * * `label` - a string - the name of the action (could be a function returning a string)
  6006. * * `title` - a string - an optional tooltip for the item
  6007. * * `action` - a function to be executed if this item is chosen, the function will receive
  6008. * * `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
  6009. * * `shortcut` - keyCode which will trigger the action if the menu is open (for example `113` for rename, which equals F2)
  6010. * * `shortcut_label` - shortcut label (like for example `F2` for rename)
  6011. * * `submenu` - an object with the same structure as $.jstree.defaults.contextmenu.items which can be used to create a submenu - each key will be rendered as a separate option in a submenu that will appear once the current item is hovered
  6012. *
  6013. * @name $.jstree.defaults.contextmenu.items
  6014. * @plugin contextmenu
  6015. */
  6016. items : function (o, cb) { // Could be an object directly
  6017. return {
  6018. "create" : {
  6019. "separator_before" : false,
  6020. "separator_after" : true,
  6021. "_disabled" : false, //(this.check("create_node", data.reference, {}, "last")),
  6022. "label" : "Create",
  6023. "action" : function (data) {
  6024. var inst = $.jstree.reference(data.reference),
  6025. obj = inst.get_node(data.reference);
  6026. inst.create_node(obj, {}, "last", function (new_node) {
  6027. try {
  6028. inst.edit(new_node);
  6029. } catch (ex) {
  6030. setTimeout(function () { inst.edit(new_node); },0);
  6031. }
  6032. });
  6033. }
  6034. },
  6035. "rename" : {
  6036. "separator_before" : false,
  6037. "separator_after" : false,
  6038. "_disabled" : false, //(this.check("rename_node", data.reference, this.get_parent(data.reference), "")),
  6039. "label" : "Rename",
  6040. /*!
  6041. "shortcut" : 113,
  6042. "shortcut_label" : 'F2',
  6043. "icon" : "glyphicon glyphicon-leaf",
  6044. */
  6045. "action" : function (data) {
  6046. var inst = $.jstree.reference(data.reference),
  6047. obj = inst.get_node(data.reference);
  6048. inst.edit(obj);
  6049. }
  6050. },
  6051. "remove" : {
  6052. "separator_before" : false,
  6053. "icon" : false,
  6054. "separator_after" : false,
  6055. "_disabled" : false, //(this.check("delete_node", data.reference, this.get_parent(data.reference), "")),
  6056. "label" : "Delete",
  6057. "action" : function (data) {
  6058. var inst = $.jstree.reference(data.reference),
  6059. obj = inst.get_node(data.reference);
  6060. if(inst.is_selected(obj)) {
  6061. inst.delete_node(inst.get_selected());
  6062. }
  6063. else {
  6064. inst.delete_node(obj);
  6065. }
  6066. }
  6067. },
  6068. "ccp" : {
  6069. "separator_before" : true,
  6070. "icon" : false,
  6071. "separator_after" : false,
  6072. "label" : "Edit",
  6073. "action" : false,
  6074. "submenu" : {
  6075. "cut" : {
  6076. "separator_before" : false,
  6077. "separator_after" : false,
  6078. "label" : "Cut",
  6079. "action" : function (data) {
  6080. var inst = $.jstree.reference(data.reference),
  6081. obj = inst.get_node(data.reference);
  6082. if(inst.is_selected(obj)) {
  6083. inst.cut(inst.get_top_selected());
  6084. }
  6085. else {
  6086. inst.cut(obj);
  6087. }
  6088. }
  6089. },
  6090. "copy" : {
  6091. "separator_before" : false,
  6092. "icon" : false,
  6093. "separator_after" : false,
  6094. "label" : "Copy",
  6095. "action" : function (data) {
  6096. var inst = $.jstree.reference(data.reference),
  6097. obj = inst.get_node(data.reference);
  6098. if(inst.is_selected(obj)) {
  6099. inst.copy(inst.get_top_selected());
  6100. }
  6101. else {
  6102. inst.copy(obj);
  6103. }
  6104. }
  6105. },
  6106. "paste" : {
  6107. "separator_before" : false,
  6108. "icon" : false,
  6109. "_disabled" : function (data) {
  6110. return !$.jstree.reference(data.reference).can_paste();
  6111. },
  6112. "separator_after" : false,
  6113. "label" : "Paste",
  6114. "action" : function (data) {
  6115. var inst = $.jstree.reference(data.reference),
  6116. obj = inst.get_node(data.reference);
  6117. inst.paste(obj);
  6118. }
  6119. }
  6120. }
  6121. }
  6122. };
  6123. }
  6124. };
  6125. $.jstree.plugins.contextmenu = function (options, parent) {
  6126. this.bind = function () {
  6127. parent.bind.call(this);
  6128. var last_ts = 0, cto = null, ex, ey;
  6129. this.element
  6130. .on("init.jstree loading.jstree ready.jstree", function () {
  6131. this.get_container_ul().addClass('jstree-contextmenu');
  6132. }.bind(this))
  6133. .on("contextmenu.jstree", ".jstree-anchor", function (e, data) {
  6134. if (e.target.tagName.toLowerCase() === 'input') {
  6135. return;
  6136. }
  6137. e.preventDefault();
  6138. last_ts = e.ctrlKey ? +new Date() : 0;
  6139. if(data || cto) {
  6140. last_ts = (+new Date()) + 10000;
  6141. }
  6142. if(cto) {
  6143. clearTimeout(cto);
  6144. }
  6145. if(!this.is_loading(e.currentTarget)) {
  6146. this.show_contextmenu(e.currentTarget, e.pageX, e.pageY, e);
  6147. }
  6148. }.bind(this))
  6149. .on("click.jstree", ".jstree-anchor", function (e) {
  6150. if(this._data.contextmenu.visible && (!last_ts || (+new Date()) - last_ts > 250)) { // work around safari & macOS ctrl+click
  6151. $.vakata.context.hide();
  6152. }
  6153. last_ts = 0;
  6154. }.bind(this))
  6155. .on("touchstart.jstree", ".jstree-anchor", function (e) {
  6156. if(!e.originalEvent || !e.originalEvent.changedTouches || !e.originalEvent.changedTouches[0]) {
  6157. return;
  6158. }
  6159. ex = e.originalEvent.changedTouches[0].clientX;
  6160. ey = e.originalEvent.changedTouches[0].clientY;
  6161. cto = setTimeout(function () {
  6162. $(e.currentTarget).trigger('contextmenu', true);
  6163. }, 750);
  6164. })
  6165. .on('touchmove.vakata.jstree', function (e) {
  6166. if(cto && e.originalEvent && e.originalEvent.changedTouches && e.originalEvent.changedTouches[0] && (Math.abs(ex - e.originalEvent.changedTouches[0].clientX) > 10 || Math.abs(ey - e.originalEvent.changedTouches[0].clientY) > 10)) {
  6167. clearTimeout(cto);
  6168. $.vakata.context.hide();
  6169. }
  6170. })
  6171. .on('touchend.vakata.jstree', function (e) {
  6172. if(cto) {
  6173. clearTimeout(cto);
  6174. }
  6175. });
  6176. /*!
  6177. if(!('oncontextmenu' in document.body) && ('ontouchstart' in document.body)) {
  6178. var el = null, tm = null;
  6179. this.element
  6180. .on("touchstart", ".jstree-anchor", function (e) {
  6181. el = e.currentTarget;
  6182. tm = +new Date();
  6183. $(document).one("touchend", function (e) {
  6184. e.target = document.elementFromPoint(e.originalEvent.targetTouches[0].pageX - window.pageXOffset, e.originalEvent.targetTouches[0].pageY - window.pageYOffset);
  6185. e.currentTarget = e.target;
  6186. tm = ((+(new Date())) - tm);
  6187. if(e.target === el && tm > 600 && tm < 1000) {
  6188. e.preventDefault();
  6189. $(el).trigger('contextmenu', e);
  6190. }
  6191. el = null;
  6192. tm = null;
  6193. });
  6194. });
  6195. }
  6196. */
  6197. $(document).on("context_hide.vakata.jstree", function (e, data) {
  6198. this._data.contextmenu.visible = false;
  6199. $(data.reference).removeClass('jstree-context');
  6200. }.bind(this));
  6201. };
  6202. this.teardown = function () {
  6203. if(this._data.contextmenu.visible) {
  6204. $.vakata.context.hide();
  6205. }
  6206. $(document).off("context_hide.vakata.jstree");
  6207. parent.teardown.call(this);
  6208. };
  6209. /**
  6210. * prepare and show the context menu for a node
  6211. * @name show_contextmenu(obj [, x, y])
  6212. * @param {mixed} obj the node
  6213. * @param {Number} x the x-coordinate relative to the document to show the menu at
  6214. * @param {Number} y the y-coordinate relative to the document to show the menu at
  6215. * @param {Object} e the event if available that triggered the contextmenu
  6216. * @plugin contextmenu
  6217. * @trigger show_contextmenu.jstree
  6218. */
  6219. this.show_contextmenu = function (obj, x, y, e) {
  6220. obj = this.get_node(obj);
  6221. if(!obj || obj.id === $.jstree.root) { return false; }
  6222. var s = this.settings.contextmenu,
  6223. d = this.get_node(obj, true),
  6224. a = d.children(".jstree-anchor"),
  6225. o = false,
  6226. i = false;
  6227. if(s.show_at_node || x === undefined || y === undefined) {
  6228. o = a.offset();
  6229. x = o.left;
  6230. y = o.top + this._data.core.li_height;
  6231. }
  6232. if(this.settings.contextmenu.select_node && !this.is_selected(obj)) {
  6233. this.activate_node(obj, e);
  6234. }
  6235. i = s.items;
  6236. if($.vakata.is_function(i)) {
  6237. i = i.call(this, obj, function (i) {
  6238. this._show_contextmenu(obj, x, y, i);
  6239. }.bind(this));
  6240. }
  6241. if($.isPlainObject(i)) {
  6242. this._show_contextmenu(obj, x, y, i);
  6243. }
  6244. };
  6245. /**
  6246. * show the prepared context menu for a node
  6247. * @name _show_contextmenu(obj, x, y, i)
  6248. * @param {mixed} obj the node
  6249. * @param {Number} x the x-coordinate relative to the document to show the menu at
  6250. * @param {Number} y the y-coordinate relative to the document to show the menu at
  6251. * @param {Number} i the object of items to show
  6252. * @plugin contextmenu
  6253. * @trigger show_contextmenu.jstree
  6254. * @private
  6255. */
  6256. this._show_contextmenu = function (obj, x, y, i) {
  6257. var d = this.get_node(obj, true),
  6258. a = d.children(".jstree-anchor");
  6259. $(document).one("context_show.vakata.jstree", function (e, data) {
  6260. var cls = 'jstree-contextmenu jstree-' + this.get_theme() + '-contextmenu';
  6261. $(data.element).addClass(cls);
  6262. a.addClass('jstree-context');
  6263. }.bind(this));
  6264. this._data.contextmenu.visible = true;
  6265. $.vakata.context.show(a, { 'x' : x, 'y' : y }, i);
  6266. /**
  6267. * triggered when the contextmenu is shown for a node
  6268. * @event
  6269. * @name show_contextmenu.jstree
  6270. * @param {Object} node the node
  6271. * @param {Number} x the x-coordinate of the menu relative to the document
  6272. * @param {Number} y the y-coordinate of the menu relative to the document
  6273. * @plugin contextmenu
  6274. */
  6275. this.trigger('show_contextmenu', { "node" : obj, "x" : x, "y" : y });
  6276. };
  6277. };
  6278. // contextmenu helper
  6279. (function ($) {
  6280. var right_to_left = false,
  6281. vakata_context = {
  6282. element : false,
  6283. reference : false,
  6284. position_x : 0,
  6285. position_y : 0,
  6286. items : [],
  6287. html : "",
  6288. is_visible : false
  6289. };
  6290. $.vakata.context = {
  6291. settings : {
  6292. hide_onmouseleave : 0,
  6293. icons : true
  6294. },
  6295. _trigger : function (event_name) {
  6296. $(document).triggerHandler("context_" + event_name + ".vakata", {
  6297. "reference" : vakata_context.reference,
  6298. "element" : vakata_context.element,
  6299. "position" : {
  6300. "x" : vakata_context.position_x,
  6301. "y" : vakata_context.position_y
  6302. }
  6303. });
  6304. },
  6305. _execute : function (i) {
  6306. i = vakata_context.items[i];
  6307. return i && (!i._disabled || ($.vakata.is_function(i._disabled) && !i._disabled({ "item" : i, "reference" : vakata_context.reference, "element" : vakata_context.element }))) && i.action ? i.action.call(null, {
  6308. "item" : i,
  6309. "reference" : vakata_context.reference,
  6310. "element" : vakata_context.element,
  6311. "position" : {
  6312. "x" : vakata_context.position_x,
  6313. "y" : vakata_context.position_y
  6314. }
  6315. }) : false;
  6316. },
  6317. _parse : function (o, is_callback) {
  6318. if(!o) { return false; }
  6319. if(!is_callback) {
  6320. vakata_context.html = "";
  6321. vakata_context.items = [];
  6322. }
  6323. var str = "",
  6324. sep = false,
  6325. tmp;
  6326. if(is_callback) { str += "<"+"ul>"; }
  6327. $.each(o, function (i, val) {
  6328. if(!val) { return true; }
  6329. vakata_context.items.push(val);
  6330. if(!sep && val.separator_before) {
  6331. str += "<"+"li class='vakata-context-separator'><"+"a href='#' " + ($.vakata.context.settings.icons ? '' : 'class="vakata-context-no-icons"') + ">&#160;<"+"/a><"+"/li>";
  6332. }
  6333. sep = false;
  6334. str += "<"+"li class='" + (val._class || "") + (val._disabled === true || ($.vakata.is_function(val._disabled) && val._disabled({ "item" : val, "reference" : vakata_context.reference, "element" : vakata_context.element })) ? " vakata-contextmenu-disabled " : "") + "' "+(val.shortcut?" data-shortcut='"+val.shortcut+"' ":'')+">";
  6335. str += "<"+"a href='#' rel='" + (vakata_context.items.length - 1) + "' " + (val.title ? "title='" + val.title + "'" : "") + ">";
  6336. if($.vakata.context.settings.icons) {
  6337. str += "<"+"i ";
  6338. if(val.icon) {
  6339. if(val.icon.indexOf("/") !== -1 || val.icon.indexOf(".") !== -1) { str += " style='background:url(\"" + val.icon + "\") center center no-repeat' "; }
  6340. else { str += " class='" + val.icon + "' "; }
  6341. }
  6342. str += "><"+"/i><"+"span class='vakata-contextmenu-sep'>&#160;<"+"/span>";
  6343. }
  6344. str += ($.vakata.is_function(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>";
  6345. if(val.submenu) {
  6346. tmp = $.vakata.context._parse(val.submenu, true);
  6347. if(tmp) { str += tmp; }
  6348. }
  6349. str += "<"+"/li>";
  6350. if(val.separator_after) {
  6351. str += "<"+"li class='vakata-context-separator'><"+"a href='#' " + ($.vakata.context.settings.icons ? '' : 'class="vakata-context-no-icons"') + ">&#160;<"+"/a><"+"/li>";
  6352. sep = true;
  6353. }
  6354. });
  6355. str = str.replace(/<li class\='vakata-context-separator'\><\/li\>$/,"");
  6356. if(is_callback) { str += "</ul>"; }
  6357. /**
  6358. * triggered on the document when the contextmenu is parsed (HTML is built)
  6359. * @event
  6360. * @plugin contextmenu
  6361. * @name context_parse.vakata
  6362. * @param {jQuery} reference the element that was right clicked
  6363. * @param {jQuery} element the DOM element of the menu itself
  6364. * @param {Object} position the x & y coordinates of the menu
  6365. */
  6366. if(!is_callback) { vakata_context.html = str; $.vakata.context._trigger("parse"); }
  6367. return str.length > 10 ? str : false;
  6368. },
  6369. _show_submenu : function (o) {
  6370. o = $(o);
  6371. if(!o.length || !o.children("ul").length) { return; }
  6372. var e = o.children("ul"),
  6373. xl = o.offset().left,
  6374. x = xl + o.outerWidth(),
  6375. y = o.offset().top,
  6376. w = e.width(),
  6377. h = e.height(),
  6378. dw = $(window).width() + $(window).scrollLeft(),
  6379. dh = $(window).height() + $(window).scrollTop();
  6380. // може да се спести е една проверка - дали няма някой от класовете вече нагоре
  6381. if(right_to_left) {
  6382. o[x - (w + 10 + o.outerWidth()) < 0 ? "addClass" : "removeClass"]("vakata-context-left");
  6383. }
  6384. else {
  6385. o[x + w > dw && xl > dw - x ? "addClass" : "removeClass"]("vakata-context-right");
  6386. }
  6387. if(y + h + 10 > dh) {
  6388. e.css("bottom","-1px");
  6389. }
  6390. //if does not fit - stick it to the side
  6391. if (o.hasClass('vakata-context-right')) {
  6392. if (xl < w) {
  6393. e.css("margin-right", xl - w);
  6394. }
  6395. } else {
  6396. if (dw - x < w) {
  6397. e.css("margin-left", dw - x - w);
  6398. }
  6399. }
  6400. e.show();
  6401. },
  6402. show : function (reference, position, data) {
  6403. var o, e, x, y, w, h, dw, dh, cond = true;
  6404. if(vakata_context.element && vakata_context.element.length) {
  6405. vakata_context.element.width('');
  6406. }
  6407. switch(cond) {
  6408. case (!position && !reference):
  6409. return false;
  6410. case (!!position && !!reference):
  6411. vakata_context.reference = reference;
  6412. vakata_context.position_x = position.x;
  6413. vakata_context.position_y = position.y;
  6414. break;
  6415. case (!position && !!reference):
  6416. vakata_context.reference = reference;
  6417. o = reference.offset();
  6418. vakata_context.position_x = o.left + reference.outerHeight();
  6419. vakata_context.position_y = o.top;
  6420. break;
  6421. case (!!position && !reference):
  6422. vakata_context.position_x = position.x;
  6423. vakata_context.position_y = position.y;
  6424. break;
  6425. }
  6426. if(!!reference && !data && $(reference).data('vakata_contextmenu')) {
  6427. data = $(reference).data('vakata_contextmenu');
  6428. }
  6429. if($.vakata.context._parse(data)) {
  6430. vakata_context.element.html(vakata_context.html);
  6431. }
  6432. if(vakata_context.items.length) {
  6433. vakata_context.element.appendTo(document.body);
  6434. e = vakata_context.element;
  6435. x = vakata_context.position_x;
  6436. y = vakata_context.position_y;
  6437. w = e.width();
  6438. h = e.height();
  6439. dw = $(window).width() + $(window).scrollLeft();
  6440. dh = $(window).height() + $(window).scrollTop();
  6441. if(right_to_left) {
  6442. x -= (e.outerWidth() - $(reference).outerWidth());
  6443. if(x < $(window).scrollLeft() + 20) {
  6444. x = $(window).scrollLeft() + 20;
  6445. }
  6446. }
  6447. if(x + w + 20 > dw) {
  6448. x = dw - (w + 20);
  6449. }
  6450. if(y + h + 20 > dh) {
  6451. y = dh - (h + 20);
  6452. }
  6453. vakata_context.element
  6454. .css({ "left" : x, "top" : y })
  6455. .show()
  6456. .find('a').first().trigger('focus').parent().addClass("vakata-context-hover");
  6457. vakata_context.is_visible = true;
  6458. /**
  6459. * triggered on the document when the contextmenu is shown
  6460. * @event
  6461. * @plugin contextmenu
  6462. * @name context_show.vakata
  6463. * @param {jQuery} reference the element that was right clicked
  6464. * @param {jQuery} element the DOM element of the menu itself
  6465. * @param {Object} position the x & y coordinates of the menu
  6466. */
  6467. $.vakata.context._trigger("show");
  6468. }
  6469. },
  6470. hide : function () {
  6471. if(vakata_context.is_visible) {
  6472. vakata_context.element.hide().find("ul").hide().end().find(':focus').trigger('blur').end().detach();
  6473. vakata_context.is_visible = false;
  6474. /**
  6475. * triggered on the document when the contextmenu is hidden
  6476. * @event
  6477. * @plugin contextmenu
  6478. * @name context_hide.vakata
  6479. * @param {jQuery} reference the element that was right clicked
  6480. * @param {jQuery} element the DOM element of the menu itself
  6481. * @param {Object} position the x & y coordinates of the menu
  6482. */
  6483. $.vakata.context._trigger("hide");
  6484. }
  6485. }
  6486. };
  6487. $(function () {
  6488. right_to_left = $(document.body).css("direction") === "rtl";
  6489. var to = false;
  6490. vakata_context.element = $("<ul class='vakata-context'></ul>");
  6491. vakata_context.element
  6492. .on("mouseenter", "li", function (e) {
  6493. e.stopImmediatePropagation();
  6494. if($.contains(this, e.relatedTarget)) {
  6495. // премахнато заради delegate mouseleave по-долу
  6496. // $(this).find(".vakata-context-hover").removeClass("vakata-context-hover");
  6497. return;
  6498. }
  6499. if(to) { clearTimeout(to); }
  6500. vakata_context.element.find(".vakata-context-hover").removeClass("vakata-context-hover").end();
  6501. $(this)
  6502. .siblings().find("ul").hide().end().end()
  6503. .parentsUntil(".vakata-context", "li").addBack().addClass("vakata-context-hover");
  6504. $.vakata.context._show_submenu(this);
  6505. })
  6506. // тестово - дали не натоварва?
  6507. .on("mouseleave", "li", function (e) {
  6508. if($.contains(this, e.relatedTarget)) { return; }
  6509. $(this).find(".vakata-context-hover").addBack().removeClass("vakata-context-hover");
  6510. })
  6511. .on("mouseleave", function (e) {
  6512. $(this).find(".vakata-context-hover").removeClass("vakata-context-hover");
  6513. if($.vakata.context.settings.hide_onmouseleave) {
  6514. to = setTimeout(
  6515. (function (t) {
  6516. return function () { $.vakata.context.hide(); };
  6517. }(this)), $.vakata.context.settings.hide_onmouseleave);
  6518. }
  6519. })
  6520. .on("click", "a", function (e) {
  6521. e.preventDefault();
  6522. //})
  6523. //.on("mouseup", "a", function (e) {
  6524. if(!$(this).trigger('blur').parent().hasClass("vakata-context-disabled") && $.vakata.context._execute($(this).attr("rel")) !== false) {
  6525. $.vakata.context.hide();
  6526. }
  6527. })
  6528. .on('keydown', 'a', function (e) {
  6529. var o = null;
  6530. switch(e.which) {
  6531. case 13:
  6532. case 32:
  6533. e.type = "click";
  6534. e.preventDefault();
  6535. $(e.currentTarget).trigger(e);
  6536. break;
  6537. case 37:
  6538. if(vakata_context.is_visible) {
  6539. 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').trigger('focus');
  6540. e.stopImmediatePropagation();
  6541. e.preventDefault();
  6542. }
  6543. break;
  6544. case 38:
  6545. if(vakata_context.is_visible) {
  6546. o = vakata_context.element.find("ul:visible").addBack().last().children(".vakata-context-hover").removeClass("vakata-context-hover").prevAll("li:not(.vakata-context-separator)").first();
  6547. if(!o.length) { o = vakata_context.element.find("ul:visible").addBack().last().children("li:not(.vakata-context-separator)").last(); }
  6548. o.addClass("vakata-context-hover").children('a').trigger('focus');
  6549. e.stopImmediatePropagation();
  6550. e.preventDefault();
  6551. }
  6552. break;
  6553. case 39:
  6554. if(vakata_context.is_visible) {
  6555. 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').trigger('focus');
  6556. e.stopImmediatePropagation();
  6557. e.preventDefault();
  6558. }
  6559. break;
  6560. case 40:
  6561. if(vakata_context.is_visible) {
  6562. o = vakata_context.element.find("ul:visible").addBack().last().children(".vakata-context-hover").removeClass("vakata-context-hover").nextAll("li:not(.vakata-context-separator)").first();
  6563. if(!o.length) { o = vakata_context.element.find("ul:visible").addBack().last().children("li:not(.vakata-context-separator)").first(); }
  6564. o.addClass("vakata-context-hover").children('a').trigger('focus');
  6565. e.stopImmediatePropagation();
  6566. e.preventDefault();
  6567. }
  6568. break;
  6569. case 27:
  6570. $.vakata.context.hide();
  6571. e.preventDefault();
  6572. break;
  6573. default:
  6574. //console.log(e.which);
  6575. break;
  6576. }
  6577. })
  6578. .on('keydown', function (e) {
  6579. e.preventDefault();
  6580. var a = vakata_context.element.find('.vakata-contextmenu-shortcut-' + e.which).parent();
  6581. if(a.parent().not('.vakata-context-disabled')) {
  6582. a.trigger('click');
  6583. }
  6584. });
  6585. $(document)
  6586. .on("mousedown.vakata.jstree", function (e) {
  6587. if(vakata_context.is_visible && vakata_context.element[0] !== e.target && !$.contains(vakata_context.element[0], e.target)) {
  6588. $.vakata.context.hide();
  6589. }
  6590. })
  6591. .on("context_show.vakata.jstree", function (e, data) {
  6592. vakata_context.element.find("li:has(ul)").children("a").addClass("vakata-context-parent");
  6593. if(right_to_left) {
  6594. vakata_context.element.addClass("vakata-context-rtl").css("direction", "rtl");
  6595. }
  6596. // also apply a RTL class?
  6597. vakata_context.element.find("ul").hide().end();
  6598. });
  6599. });
  6600. }($));
  6601. // $.jstree.defaults.plugins.push("contextmenu");
  6602. /**
  6603. * ### Drag'n'drop plugin
  6604. *
  6605. * Enables dragging and dropping of nodes in the tree, resulting in a move or copy operations.
  6606. */
  6607. /**
  6608. * stores all defaults for the drag'n'drop plugin
  6609. * @name $.jstree.defaults.dnd
  6610. * @plugin dnd
  6611. */
  6612. $.jstree.defaults.dnd = {
  6613. /**
  6614. * a boolean indicating if a copy should be possible while dragging (by pressint the meta key or Ctrl). Defaults to `true`.
  6615. * @name $.jstree.defaults.dnd.copy
  6616. * @plugin dnd
  6617. */
  6618. copy : true,
  6619. /**
  6620. * a number indicating how long a node should remain hovered while dragging to be opened. Defaults to `500`.
  6621. * @name $.jstree.defaults.dnd.open_timeout
  6622. * @plugin dnd
  6623. */
  6624. open_timeout : 500,
  6625. /**
  6626. * 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
  6627. * @name $.jstree.defaults.dnd.is_draggable
  6628. * @plugin dnd
  6629. */
  6630. is_draggable : true,
  6631. /**
  6632. * 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`
  6633. * @name $.jstree.defaults.dnd.check_while_dragging
  6634. * @plugin dnd
  6635. */
  6636. check_while_dragging : true,
  6637. /**
  6638. * a boolean indicating if nodes from this tree should only be copied with dnd (as opposed to moved), default is `false`
  6639. * @name $.jstree.defaults.dnd.always_copy
  6640. * @plugin dnd
  6641. */
  6642. always_copy : false,
  6643. /**
  6644. * 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`
  6645. * @name $.jstree.defaults.dnd.inside_pos
  6646. * @plugin dnd
  6647. */
  6648. inside_pos : 0,
  6649. /**
  6650. * 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
  6651. * @name $.jstree.defaults.dnd.drag_selection
  6652. * @plugin dnd
  6653. */
  6654. drag_selection : true,
  6655. /**
  6656. * 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.
  6657. * @name $.jstree.defaults.dnd.touch
  6658. * @plugin dnd
  6659. */
  6660. touch : true,
  6661. /**
  6662. * 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.
  6663. * @name $.jstree.defaults.dnd.large_drop_target
  6664. * @plugin dnd
  6665. */
  6666. large_drop_target : false,
  6667. /**
  6668. * 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".
  6669. * @name $.jstree.defaults.dnd.large_drag_target
  6670. * @plugin dnd
  6671. */
  6672. large_drag_target : false,
  6673. /**
  6674. * controls whether use HTML5 dnd api instead of classical. That will allow better integration of dnd events with other HTML5 controls.
  6675. * @reference http://caniuse.com/#feat=dragndrop
  6676. * @name $.jstree.defaults.dnd.use_html5
  6677. * @plugin dnd
  6678. */
  6679. use_html5: false,
  6680. /**
  6681. * controls whether items can be dropped anywhere on the tree.
  6682. * @name $.jstree.defaults.dnd.blank_space_drop
  6683. * @plugin dnd
  6684. */
  6685. blank_space_drop: false
  6686. };
  6687. var drg, elm;
  6688. // TODO: now check works by checking for each node individually, how about max_children, unique, etc?
  6689. $.jstree.plugins.dnd = function (options, parent) {
  6690. this.init = function (el, options) {
  6691. parent.init.call(this, el, options);
  6692. this.settings.dnd.use_html5 = this.settings.dnd.use_html5 && ('draggable' in document.createElement('span'));
  6693. };
  6694. this.bind = function () {
  6695. parent.bind.call(this);
  6696. this.element
  6697. .on(this.settings.dnd.use_html5 ? 'dragstart.jstree' : 'mousedown.jstree touchstart.jstree', this.settings.dnd.large_drag_target ? '.jstree-node' : '.jstree-anchor', function (e) {
  6698. if(this.settings.dnd.large_drag_target && $(e.target).closest('.jstree-node')[0] !== e.currentTarget) {
  6699. return true;
  6700. }
  6701. if(e.type === "touchstart" && (!this.settings.dnd.touch || (this.settings.dnd.touch === 'selected' && !$(e.currentTarget).closest('.jstree-node').children('.jstree-anchor').hasClass('jstree-clicked')))) {
  6702. return true;
  6703. }
  6704. var obj = this.get_node(e.target),
  6705. mlt = this.is_selected(obj) && this.settings.dnd.drag_selection ? this.get_top_selected().length : 1,
  6706. txt = (mlt > 1 ? mlt + ' ' + this.get_string('nodes') : this.get_text(e.currentTarget));
  6707. if(this.settings.core.force_text) {
  6708. txt = $.vakata.html.escape(txt);
  6709. }
  6710. if(obj && (obj.id || obj.id === 0) && obj.id !== $.jstree.root && (e.which === 1 || e.type === "touchstart" || e.type === "dragstart") &&
  6711. (this.settings.dnd.is_draggable === true || ($.vakata.is_function(this.settings.dnd.is_draggable) && this.settings.dnd.is_draggable.call(this, (mlt > 1 ? this.get_top_selected(true) : [obj]), e)))
  6712. ) {
  6713. drg = { 'jstree' : true, 'origin' : this, 'obj' : this.get_node(obj,true), 'nodes' : mlt > 1 ? this.get_top_selected() : [obj.id] };
  6714. elm = e.currentTarget;
  6715. if (this.settings.dnd.use_html5) {
  6716. $.vakata.dnd._trigger('start', e, { 'helper': $(), 'element': elm, 'data': drg });
  6717. } else {
  6718. this.element.trigger('mousedown.jstree');
  6719. return $.vakata.dnd.start(e, drg, '<div id="jstree-dnd" class="jstree-' + this.get_theme() + ' jstree-' + this.get_theme() + '-' + this.get_theme_variant() + ' ' + ( this.settings.core.themes.responsive ? ' jstree-dnd-responsive' : '' ) + '"><i class="jstree-icon jstree-er"></i>' + txt + '<ins class="jstree-copy">+</ins></div>');
  6720. }
  6721. }
  6722. }.bind(this));
  6723. if (this.settings.dnd.use_html5) {
  6724. this.element
  6725. .on('dragover.jstree', function (e) {
  6726. e.preventDefault();
  6727. $.vakata.dnd._trigger('move', e, { 'helper': $(), 'element': elm, 'data': drg });
  6728. return false;
  6729. })
  6730. //.on('dragenter.jstree', this.settings.dnd.large_drop_target ? '.jstree-node' : '.jstree-anchor', $.proxy(function (e) {
  6731. // e.preventDefault();
  6732. // $.vakata.dnd._trigger('move', e, { 'helper': $(), 'element': elm, 'data': drg });
  6733. // return false;
  6734. // }, this))
  6735. .on('drop.jstree', function (e) {
  6736. e.preventDefault();
  6737. $.vakata.dnd._trigger('stop', e, { 'helper': $(), 'element': elm, 'data': drg });
  6738. return false;
  6739. }.bind(this));
  6740. }
  6741. };
  6742. this.redraw_node = function(obj, deep, callback, force_render) {
  6743. obj = parent.redraw_node.apply(this, arguments);
  6744. if (obj && this.settings.dnd.use_html5) {
  6745. if (this.settings.dnd.large_drag_target) {
  6746. obj.setAttribute('draggable', true);
  6747. } else {
  6748. var i, j, tmp = null;
  6749. for(i = 0, j = obj.childNodes.length; i < j; i++) {
  6750. if(obj.childNodes[i] && obj.childNodes[i].className && obj.childNodes[i].className.indexOf("jstree-anchor") !== -1) {
  6751. tmp = obj.childNodes[i];
  6752. break;
  6753. }
  6754. }
  6755. if(tmp) {
  6756. tmp.setAttribute('draggable', true);
  6757. }
  6758. }
  6759. }
  6760. return obj;
  6761. };
  6762. };
  6763. $(function() {
  6764. // bind only once for all instances
  6765. var lastmv = false,
  6766. laster = false,
  6767. lastev = false,
  6768. opento = false,
  6769. marker = $('<div id="jstree-marker">&#160;</div>').hide(); //.appendTo('body');
  6770. $(document)
  6771. .on('dragover.vakata.jstree', function (e) {
  6772. if (elm) {
  6773. $.vakata.dnd._trigger('move', e, { 'helper': $(), 'element': elm, 'data': drg });
  6774. }
  6775. })
  6776. .on('drop.vakata.jstree', function (e) {
  6777. if (elm) {
  6778. $.vakata.dnd._trigger('stop', e, { 'helper': $(), 'element': elm, 'data': drg });
  6779. elm = null;
  6780. drg = null;
  6781. }
  6782. })
  6783. .on('dnd_start.vakata.jstree', function (e, data) {
  6784. lastmv = false;
  6785. lastev = false;
  6786. if(!data || !data.data || !data.data.jstree) { return; }
  6787. marker.appendTo(document.body); //.show();
  6788. })
  6789. .on('dnd_move.vakata.jstree', function (e, data) {
  6790. var isDifferentNode = data.event.target !== lastev.target;
  6791. if(opento) {
  6792. if (!data.event || data.event.type !== 'dragover' || isDifferentNode) {
  6793. clearTimeout(opento);
  6794. }
  6795. }
  6796. if(!data || !data.data || !data.data.jstree) { return; }
  6797. // if we are hovering the marker image do nothing (can happen on "inside" drags)
  6798. if(data.event.target.id && data.event.target.id === 'jstree-marker') {
  6799. return;
  6800. }
  6801. lastev = data.event;
  6802. var ins = $.jstree.reference(data.event.target),
  6803. ref = false,
  6804. off = false,
  6805. rel = false,
  6806. tmp, l, t, h, p, i, o, ok, t1, t2, op, ps, pr, ip, tm, is_copy, pn, c;
  6807. // if we are over an instance
  6808. if(ins && ins._data && ins._data.dnd) {
  6809. marker.attr('class', 'jstree-' + ins.get_theme() + ( ins.settings.core.themes.responsive ? ' jstree-dnd-responsive' : '' ));
  6810. is_copy = data.data.origin && (data.data.origin.settings.dnd.always_copy || (data.data.origin.settings.dnd.copy && (data.event.metaKey || data.event.ctrlKey)));
  6811. data.helper
  6812. .children().attr('class', 'jstree-' + ins.get_theme() + ' jstree-' + ins.get_theme() + '-' + ins.get_theme_variant() + ' ' + ( ins.settings.core.themes.responsive ? ' jstree-dnd-responsive' : '' ))
  6813. .find('.jstree-copy').first()[ is_copy ? 'show' : 'hide' ]();
  6814. // if are hovering the container itself add a new root node
  6815. //console.log(data.event);
  6816. if( (data.event.target === ins.element[0] || data.event.target === ins.get_container_ul()[0]) && (ins.get_container_ul().children().length === 0 || ins.settings.dnd.blank_space_drop)) {
  6817. ok = true;
  6818. for(t1 = 0, t2 = data.data.nodes.length; t1 < t2; t1++) {
  6819. 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) });
  6820. if(!ok) { break; }
  6821. }
  6822. if(ok) {
  6823. lastmv = { 'ins' : ins, 'par' : $.jstree.root, 'pos' : 'last' };
  6824. marker.hide();
  6825. data.helper.find('.jstree-icon').first().removeClass('jstree-er').addClass('jstree-ok');
  6826. if (data.event.originalEvent && data.event.originalEvent.dataTransfer) {
  6827. data.event.originalEvent.dataTransfer.dropEffect = is_copy ? 'copy' : 'move';
  6828. }
  6829. return;
  6830. }
  6831. }
  6832. else {
  6833. // if we are hovering a tree node
  6834. ref = ins.settings.dnd.large_drop_target ? $(data.event.target).closest('.jstree-node').children('.jstree-anchor') : $(data.event.target).closest('.jstree-anchor');
  6835. if(ref && ref.length && ref.parent().is('.jstree-closed, .jstree-open, .jstree-leaf')) {
  6836. off = ref.offset();
  6837. rel = (data.event.pageY !== undefined ? data.event.pageY : data.event.originalEvent.pageY) - off.top;
  6838. h = ref.outerHeight();
  6839. if(rel < h / 3) {
  6840. o = ['b', 'i', 'a'];
  6841. }
  6842. else if(rel > h - h / 3) {
  6843. o = ['a', 'i', 'b'];
  6844. }
  6845. else {
  6846. o = rel > h / 2 ? ['i', 'a', 'b'] : ['i', 'b', 'a'];
  6847. }
  6848. $.each(o, function (j, v) {
  6849. switch(v) {
  6850. case 'b':
  6851. l = off.left - 6;
  6852. t = off.top;
  6853. p = ins.get_parent(ref);
  6854. i = ref.parent().index();
  6855. c = 'jstree-below';
  6856. break;
  6857. case 'i':
  6858. ip = ins.settings.dnd.inside_pos;
  6859. tm = ins.get_node(ref.parent());
  6860. l = off.left - 2;
  6861. t = off.top + h / 2 + 1;
  6862. p = tm.id;
  6863. i = ip === 'first' ? 0 : (ip === 'last' ? tm.children.length : Math.min(ip, tm.children.length));
  6864. c = 'jstree-inside';
  6865. break;
  6866. case 'a':
  6867. l = off.left - 6;
  6868. t = off.top + h;
  6869. p = ins.get_parent(ref);
  6870. i = ref.parent().index() + 1;
  6871. c = 'jstree-above';
  6872. break;
  6873. }
  6874. ok = true;
  6875. for(t1 = 0, t2 = data.data.nodes.length; t1 < t2; t1++) {
  6876. 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";
  6877. ps = i;
  6878. if(op === "move_node" && v === 'a' && (data.data.origin && data.data.origin === ins) && p === ins.get_parent(data.data.nodes[t1])) {
  6879. pr = ins.get_node(p);
  6880. if(ps > $.inArray(data.data.nodes[t1], pr.children)) {
  6881. ps -= 1;
  6882. }
  6883. }
  6884. 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) }) );
  6885. if(!ok) {
  6886. if(ins && ins.last_error) { laster = ins.last_error(); }
  6887. break;
  6888. }
  6889. }
  6890. if(v === 'i' && ref.parent().is('.jstree-closed') && ins.settings.dnd.open_timeout) {
  6891. if (!data.event || data.event.type !== 'dragover' || isDifferentNode) {
  6892. if (opento) { clearTimeout(opento); }
  6893. opento = setTimeout((function (x, z) { return function () { x.open_node(z); }; }(ins, ref)), ins.settings.dnd.open_timeout);
  6894. }
  6895. }
  6896. if(ok) {
  6897. pn = ins.get_node(p, true);
  6898. if (!pn.hasClass('.jstree-dnd-parent')) {
  6899. $('.jstree-dnd-parent').removeClass('jstree-dnd-parent');
  6900. pn.addClass('jstree-dnd-parent');
  6901. }
  6902. lastmv = { 'ins' : ins, 'par' : p, 'pos' : v === 'i' && ip === 'last' && i === 0 && !ins.is_loaded(tm) ? 'last' : i };
  6903. marker.css({ 'left' : l + 'px', 'top' : t + 'px' }).show();
  6904. marker.removeClass('jstree-above jstree-inside jstree-below').addClass(c);
  6905. data.helper.find('.jstree-icon').first().removeClass('jstree-er').addClass('jstree-ok');
  6906. if (data.event.originalEvent && data.event.originalEvent.dataTransfer) {
  6907. data.event.originalEvent.dataTransfer.dropEffect = is_copy ? 'copy' : 'move';
  6908. }
  6909. laster = {};
  6910. o = true;
  6911. return false;
  6912. }
  6913. });
  6914. if(o === true) { return; }
  6915. }
  6916. }
  6917. }
  6918. $('.jstree-dnd-parent').removeClass('jstree-dnd-parent');
  6919. lastmv = false;
  6920. data.helper.find('.jstree-icon').removeClass('jstree-ok').addClass('jstree-er');
  6921. if (data.event.originalEvent && data.event.originalEvent.dataTransfer) {
  6922. //data.event.originalEvent.dataTransfer.dropEffect = 'none';
  6923. }
  6924. marker.hide();
  6925. })
  6926. .on('dnd_scroll.vakata.jstree', function (e, data) {
  6927. if(!data || !data.data || !data.data.jstree) { return; }
  6928. marker.hide();
  6929. lastmv = false;
  6930. lastev = false;
  6931. data.helper.find('.jstree-icon').first().removeClass('jstree-ok').addClass('jstree-er');
  6932. })
  6933. .on('dnd_stop.vakata.jstree', function (e, data) {
  6934. $('.jstree-dnd-parent').removeClass('jstree-dnd-parent');
  6935. if(opento) { clearTimeout(opento); }
  6936. if(!data || !data.data || !data.data.jstree) { return; }
  6937. marker.hide().detach();
  6938. var i, j, nodes = [];
  6939. if(lastmv) {
  6940. for(i = 0, j = data.data.nodes.length; i < j; i++) {
  6941. nodes[i] = data.data.origin ? data.data.origin.get_node(data.data.nodes[i]) : data.data.nodes[i];
  6942. }
  6943. 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);
  6944. }
  6945. else {
  6946. i = $(data.event.target).closest('.jstree');
  6947. if(i.length && laster && laster.error && laster.error === 'check') {
  6948. i = i.jstree(true);
  6949. if(i) {
  6950. i.settings.core.error.call(this, laster);
  6951. }
  6952. }
  6953. }
  6954. lastev = false;
  6955. lastmv = false;
  6956. })
  6957. .on('keyup.jstree keydown.jstree', function (e, data) {
  6958. data = $.vakata.dnd._get();
  6959. if(data && data.data && data.data.jstree) {
  6960. if (e.type === "keyup" && e.which === 27) {
  6961. if (opento) { clearTimeout(opento); }
  6962. lastmv = false;
  6963. laster = false;
  6964. lastev = false;
  6965. opento = false;
  6966. marker.hide().detach();
  6967. $.vakata.dnd._clean();
  6968. } else {
  6969. 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' ]();
  6970. if(lastev) {
  6971. lastev.metaKey = e.metaKey;
  6972. lastev.ctrlKey = e.ctrlKey;
  6973. $.vakata.dnd._trigger('move', lastev);
  6974. }
  6975. }
  6976. }
  6977. });
  6978. });
  6979. // helpers
  6980. (function ($) {
  6981. $.vakata.html = {
  6982. div : $('<div></div>'),
  6983. escape : function (str) {
  6984. return $.vakata.html.div.text(str).html();
  6985. },
  6986. strip : function (str) {
  6987. return $.vakata.html.div.empty().append($.parseHTML(str)).text();
  6988. }
  6989. };
  6990. // private variable
  6991. var vakata_dnd = {
  6992. element : false,
  6993. target : false,
  6994. is_down : false,
  6995. is_drag : false,
  6996. helper : false,
  6997. helper_w: 0,
  6998. data : false,
  6999. init_x : 0,
  7000. init_y : 0,
  7001. scroll_l: 0,
  7002. scroll_t: 0,
  7003. scroll_e: false,
  7004. scroll_i: false,
  7005. is_touch: false
  7006. };
  7007. $.vakata.dnd = {
  7008. settings : {
  7009. scroll_speed : 10,
  7010. scroll_proximity : 20,
  7011. helper_left : 5,
  7012. helper_top : 10,
  7013. threshold : 5,
  7014. threshold_touch : 10
  7015. },
  7016. _trigger : function (event_name, e, data) {
  7017. if (data === undefined) {
  7018. data = $.vakata.dnd._get();
  7019. }
  7020. data.event = e;
  7021. $(document).triggerHandler("dnd_" + event_name + ".vakata", data);
  7022. },
  7023. _get : function () {
  7024. return {
  7025. "data" : vakata_dnd.data,
  7026. "element" : vakata_dnd.element,
  7027. "helper" : vakata_dnd.helper
  7028. };
  7029. },
  7030. _clean : function () {
  7031. if(vakata_dnd.helper) { vakata_dnd.helper.remove(); }
  7032. if(vakata_dnd.scroll_i) { clearInterval(vakata_dnd.scroll_i); vakata_dnd.scroll_i = false; }
  7033. vakata_dnd = {
  7034. element : false,
  7035. target : false,
  7036. is_down : false,
  7037. is_drag : false,
  7038. helper : false,
  7039. helper_w: 0,
  7040. data : false,
  7041. init_x : 0,
  7042. init_y : 0,
  7043. scroll_l: 0,
  7044. scroll_t: 0,
  7045. scroll_e: false,
  7046. scroll_i: false,
  7047. is_touch: false
  7048. };
  7049. elm = null;
  7050. $(document).off("mousemove.vakata.jstree touchmove.vakata.jstree", $.vakata.dnd.drag);
  7051. $(document).off("mouseup.vakata.jstree touchend.vakata.jstree", $.vakata.dnd.stop);
  7052. },
  7053. _scroll : function (init_only) {
  7054. if(!vakata_dnd.scroll_e || (!vakata_dnd.scroll_l && !vakata_dnd.scroll_t)) {
  7055. if(vakata_dnd.scroll_i) { clearInterval(vakata_dnd.scroll_i); vakata_dnd.scroll_i = false; }
  7056. return false;
  7057. }
  7058. if(!vakata_dnd.scroll_i) {
  7059. vakata_dnd.scroll_i = setInterval($.vakata.dnd._scroll, 100);
  7060. return false;
  7061. }
  7062. if(init_only === true) { return false; }
  7063. var i = vakata_dnd.scroll_e.scrollTop(),
  7064. j = vakata_dnd.scroll_e.scrollLeft();
  7065. vakata_dnd.scroll_e.scrollTop(i + vakata_dnd.scroll_t * $.vakata.dnd.settings.scroll_speed);
  7066. vakata_dnd.scroll_e.scrollLeft(j + vakata_dnd.scroll_l * $.vakata.dnd.settings.scroll_speed);
  7067. if(i !== vakata_dnd.scroll_e.scrollTop() || j !== vakata_dnd.scroll_e.scrollLeft()) {
  7068. /**
  7069. * triggered on the document when a drag causes an element to scroll
  7070. * @event
  7071. * @plugin dnd
  7072. * @name dnd_scroll.vakata
  7073. * @param {Mixed} data any data supplied with the call to $.vakata.dnd.start
  7074. * @param {DOM} element the DOM element being dragged
  7075. * @param {jQuery} helper the helper shown next to the mouse
  7076. * @param {jQuery} event the element that is scrolling
  7077. */
  7078. $.vakata.dnd._trigger("scroll", vakata_dnd.scroll_e);
  7079. }
  7080. },
  7081. start : function (e, data, html) {
  7082. if(e.type === "touchstart" && e.originalEvent && e.originalEvent.changedTouches && e.originalEvent.changedTouches[0]) {
  7083. e.pageX = e.originalEvent.changedTouches[0].pageX;
  7084. e.pageY = e.originalEvent.changedTouches[0].pageY;
  7085. e.target = document.elementFromPoint(e.originalEvent.changedTouches[0].pageX - window.pageXOffset, e.originalEvent.changedTouches[0].pageY - window.pageYOffset);
  7086. }
  7087. if(vakata_dnd.is_drag) { $.vakata.dnd.stop({}); }
  7088. try {
  7089. e.currentTarget.unselectable = "on";
  7090. e.currentTarget.onselectstart = function() { return false; };
  7091. if(e.currentTarget.style) {
  7092. e.currentTarget.style.touchAction = "none";
  7093. e.currentTarget.style.msTouchAction = "none";
  7094. e.currentTarget.style.MozUserSelect = "none";
  7095. }
  7096. } catch(ignore) { }
  7097. vakata_dnd.init_x = e.pageX;
  7098. vakata_dnd.init_y = e.pageY;
  7099. vakata_dnd.data = data;
  7100. vakata_dnd.is_down = true;
  7101. vakata_dnd.element = e.currentTarget;
  7102. vakata_dnd.target = e.target;
  7103. vakata_dnd.is_touch = e.type === "touchstart";
  7104. if(html !== false) {
  7105. vakata_dnd.helper = $("<div id='vakata-dnd'></div>").html(html).css({
  7106. "display" : "block",
  7107. "margin" : "0",
  7108. "padding" : "0",
  7109. "position" : "absolute",
  7110. "top" : "-2000px",
  7111. "lineHeight" : "16px",
  7112. "zIndex" : "10000"
  7113. });
  7114. }
  7115. $(document).on("mousemove.vakata.jstree touchmove.vakata.jstree", $.vakata.dnd.drag);
  7116. $(document).on("mouseup.vakata.jstree touchend.vakata.jstree", $.vakata.dnd.stop);
  7117. return false;
  7118. },
  7119. drag : function (e) {
  7120. if(e.type === "touchmove" && e.originalEvent && e.originalEvent.changedTouches && e.originalEvent.changedTouches[0]) {
  7121. e.pageX = e.originalEvent.changedTouches[0].pageX;
  7122. e.pageY = e.originalEvent.changedTouches[0].pageY;
  7123. e.target = document.elementFromPoint(e.originalEvent.changedTouches[0].pageX - window.pageXOffset, e.originalEvent.changedTouches[0].pageY - window.pageYOffset);
  7124. }
  7125. if(!vakata_dnd.is_down) { return; }
  7126. if(!vakata_dnd.is_drag) {
  7127. if(
  7128. Math.abs(e.pageX - vakata_dnd.init_x) > (vakata_dnd.is_touch ? $.vakata.dnd.settings.threshold_touch : $.vakata.dnd.settings.threshold) ||
  7129. Math.abs(e.pageY - vakata_dnd.init_y) > (vakata_dnd.is_touch ? $.vakata.dnd.settings.threshold_touch : $.vakata.dnd.settings.threshold)
  7130. ) {
  7131. if(vakata_dnd.helper) {
  7132. vakata_dnd.helper.appendTo(document.body);
  7133. vakata_dnd.helper_w = vakata_dnd.helper.outerWidth();
  7134. }
  7135. vakata_dnd.is_drag = true;
  7136. $(vakata_dnd.target).one('click.vakata', false);
  7137. /**
  7138. * triggered on the document when a drag starts
  7139. * @event
  7140. * @plugin dnd
  7141. * @name dnd_start.vakata
  7142. * @param {Mixed} data any data supplied with the call to $.vakata.dnd.start
  7143. * @param {DOM} element the DOM element being dragged
  7144. * @param {jQuery} helper the helper shown next to the mouse
  7145. * @param {Object} event the event that caused the start (probably mousemove)
  7146. */
  7147. $.vakata.dnd._trigger("start", e);
  7148. }
  7149. else { return; }
  7150. }
  7151. var d = false, w = false,
  7152. dh = false, wh = false,
  7153. dw = false, ww = false,
  7154. dt = false, dl = false,
  7155. ht = false, hl = false;
  7156. vakata_dnd.scroll_t = 0;
  7157. vakata_dnd.scroll_l = 0;
  7158. vakata_dnd.scroll_e = false;
  7159. $($(e.target).parentsUntil("body").addBack().get().reverse())
  7160. .filter(function () {
  7161. return this.ownerDocument && (/^auto|scroll$/).test($(this).css("overflow")) &&
  7162. (this.scrollHeight > this.offsetHeight || this.scrollWidth > this.offsetWidth);
  7163. })
  7164. .each(function () {
  7165. var t = $(this), o = t.offset();
  7166. if(this.scrollHeight > this.offsetHeight) {
  7167. if(o.top + t.height() - e.pageY < $.vakata.dnd.settings.scroll_proximity) { vakata_dnd.scroll_t = 1; }
  7168. if(e.pageY - o.top < $.vakata.dnd.settings.scroll_proximity) { vakata_dnd.scroll_t = -1; }
  7169. }
  7170. if(this.scrollWidth > this.offsetWidth) {
  7171. if(o.left + t.width() - e.pageX < $.vakata.dnd.settings.scroll_proximity) { vakata_dnd.scroll_l = 1; }
  7172. if(e.pageX - o.left < $.vakata.dnd.settings.scroll_proximity) { vakata_dnd.scroll_l = -1; }
  7173. }
  7174. if(vakata_dnd.scroll_t || vakata_dnd.scroll_l) {
  7175. vakata_dnd.scroll_e = $(this);
  7176. return false;
  7177. }
  7178. });
  7179. if(!vakata_dnd.scroll_e) {
  7180. d = $(document); w = $(window);
  7181. dh = d.height(); wh = w.height();
  7182. dw = d.width(); ww = w.width();
  7183. dt = d.scrollTop(); dl = d.scrollLeft();
  7184. if(dh > wh && e.pageY - dt < $.vakata.dnd.settings.scroll_proximity) { vakata_dnd.scroll_t = -1; }
  7185. if(dh > wh && wh - (e.pageY - dt) < $.vakata.dnd.settings.scroll_proximity) { vakata_dnd.scroll_t = 1; }
  7186. if(dw > ww && e.pageX - dl < $.vakata.dnd.settings.scroll_proximity) { vakata_dnd.scroll_l = -1; }
  7187. if(dw > ww && ww - (e.pageX - dl) < $.vakata.dnd.settings.scroll_proximity) { vakata_dnd.scroll_l = 1; }
  7188. if(vakata_dnd.scroll_t || vakata_dnd.scroll_l) {
  7189. vakata_dnd.scroll_e = d;
  7190. }
  7191. }
  7192. if(vakata_dnd.scroll_e) { $.vakata.dnd._scroll(true); }
  7193. if(vakata_dnd.helper) {
  7194. ht = parseInt(e.pageY + $.vakata.dnd.settings.helper_top, 10);
  7195. hl = parseInt(e.pageX + $.vakata.dnd.settings.helper_left, 10);
  7196. if(dh && ht + 25 > dh) { ht = dh - 50; }
  7197. if(dw && hl + vakata_dnd.helper_w > dw) { hl = dw - (vakata_dnd.helper_w + 2); }
  7198. vakata_dnd.helper.css({
  7199. left : hl + "px",
  7200. top : ht + "px"
  7201. });
  7202. }
  7203. /**
  7204. * triggered on the document when a drag is in progress
  7205. * @event
  7206. * @plugin dnd
  7207. * @name dnd_move.vakata
  7208. * @param {Mixed} data any data supplied with the call to $.vakata.dnd.start
  7209. * @param {DOM} element the DOM element being dragged
  7210. * @param {jQuery} helper the helper shown next to the mouse
  7211. * @param {Object} event the event that caused this to trigger (most likely mousemove)
  7212. */
  7213. $.vakata.dnd._trigger("move", e);
  7214. return false;
  7215. },
  7216. stop : function (e) {
  7217. if(e.type === "touchend" && e.originalEvent && e.originalEvent.changedTouches && e.originalEvent.changedTouches[0]) {
  7218. e.pageX = e.originalEvent.changedTouches[0].pageX;
  7219. e.pageY = e.originalEvent.changedTouches[0].pageY;
  7220. e.target = document.elementFromPoint(e.originalEvent.changedTouches[0].pageX - window.pageXOffset, e.originalEvent.changedTouches[0].pageY - window.pageYOffset);
  7221. }
  7222. if(vakata_dnd.is_drag) {
  7223. /**
  7224. * triggered on the document when a drag stops (the dragged element is dropped)
  7225. * @event
  7226. * @plugin dnd
  7227. * @name dnd_stop.vakata
  7228. * @param {Mixed} data any data supplied with the call to $.vakata.dnd.start
  7229. * @param {DOM} element the DOM element being dragged
  7230. * @param {jQuery} helper the helper shown next to the mouse
  7231. * @param {Object} event the event that caused the stop
  7232. */
  7233. if (e.target !== vakata_dnd.target) {
  7234. $(vakata_dnd.target).off('click.vakata');
  7235. }
  7236. $.vakata.dnd._trigger("stop", e);
  7237. }
  7238. else {
  7239. if(e.type === "touchend" && e.target === vakata_dnd.target) {
  7240. var to = setTimeout(function () { $(e.target).trigger('click'); }, 100);
  7241. $(e.target).one('click', function() { if(to) { clearTimeout(to); } });
  7242. }
  7243. }
  7244. $.vakata.dnd._clean();
  7245. return false;
  7246. }
  7247. };
  7248. }($));
  7249. // include the dnd plugin by default
  7250. // $.jstree.defaults.plugins.push("dnd");
  7251. /**
  7252. * ### Massload plugin
  7253. *
  7254. * Adds massload functionality to jsTree, so that multiple nodes can be loaded in a single request (only useful with lazy loading).
  7255. */
  7256. /**
  7257. * massload configuration
  7258. *
  7259. * It is possible to set this to a standard jQuery-like AJAX config.
  7260. * 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.
  7261. *
  7262. * 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.
  7263. *
  7264. * 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.
  7265. *
  7266. * {
  7267. * "id1" : [{ "text" : "Child of ID1", "id" : "c1" }, { "text" : "Another child of ID1", "id" : "c2" }],
  7268. * "id2" : [{ "text" : "Child of ID2", "id" : "c3" }]
  7269. * }
  7270. *
  7271. * @name $.jstree.defaults.massload
  7272. * @plugin massload
  7273. */
  7274. $.jstree.defaults.massload = null;
  7275. $.jstree.plugins.massload = function (options, parent) {
  7276. this.init = function (el, options) {
  7277. this._data.massload = {};
  7278. parent.init.call(this, el, options);
  7279. };
  7280. this._load_nodes = function (nodes, callback, is_callback, force_reload) {
  7281. var s = this.settings.massload,
  7282. toLoad = [],
  7283. m = this._model.data,
  7284. i, j, dom;
  7285. if (!is_callback) {
  7286. for(i = 0, j = nodes.length; i < j; i++) {
  7287. if(!m[nodes[i]] || ( (!m[nodes[i]].state.loaded && !m[nodes[i]].state.failed) || force_reload) ) {
  7288. toLoad.push(nodes[i]);
  7289. dom = this.get_node(nodes[i], true);
  7290. if (dom && dom.length) {
  7291. dom.addClass("jstree-loading").attr('aria-busy',true);
  7292. }
  7293. }
  7294. }
  7295. this._data.massload = {};
  7296. if (toLoad.length) {
  7297. if($.vakata.is_function(s)) {
  7298. return s.call(this, toLoad, function (data) {
  7299. var i, j;
  7300. if(data) {
  7301. for(i in data) {
  7302. if(data.hasOwnProperty(i)) {
  7303. this._data.massload[i] = data[i];
  7304. }
  7305. }
  7306. }
  7307. for(i = 0, j = nodes.length; i < j; i++) {
  7308. dom = this.get_node(nodes[i], true);
  7309. if (dom && dom.length) {
  7310. dom.removeClass("jstree-loading").attr('aria-busy',false);
  7311. }
  7312. }
  7313. parent._load_nodes.call(this, nodes, callback, is_callback, force_reload);
  7314. }.bind(this));
  7315. }
  7316. if(typeof s === 'object' && s && s.url) {
  7317. s = $.extend(true, {}, s);
  7318. if($.vakata.is_function(s.url)) {
  7319. s.url = s.url.call(this, toLoad);
  7320. }
  7321. if($.vakata.is_function(s.data)) {
  7322. s.data = s.data.call(this, toLoad);
  7323. }
  7324. return $.ajax(s)
  7325. .done(function (data,t,x) {
  7326. var i, j;
  7327. if(data) {
  7328. for(i in data) {
  7329. if(data.hasOwnProperty(i)) {
  7330. this._data.massload[i] = data[i];
  7331. }
  7332. }
  7333. }
  7334. for(i = 0, j = nodes.length; i < j; i++) {
  7335. dom = this.get_node(nodes[i], true);
  7336. if (dom && dom.length) {
  7337. dom.removeClass("jstree-loading").attr('aria-busy',false);
  7338. }
  7339. }
  7340. parent._load_nodes.call(this, nodes, callback, is_callback, force_reload);
  7341. }.bind(this))
  7342. .fail(function (f) {
  7343. parent._load_nodes.call(this, nodes, callback, is_callback, force_reload);
  7344. }.bind(this));
  7345. }
  7346. }
  7347. }
  7348. return parent._load_nodes.call(this, nodes, callback, is_callback, force_reload);
  7349. };
  7350. this._load_node = function (obj, callback) {
  7351. var data = this._data.massload[obj.id],
  7352. rslt = null, dom;
  7353. if(data) {
  7354. rslt = this[typeof data === 'string' ? '_append_html_data' : '_append_json_data'](
  7355. obj,
  7356. typeof data === 'string' ? $($.parseHTML(data)).filter(function () { return this.nodeType !== 3; }) : data,
  7357. function (status) { callback.call(this, status); }
  7358. );
  7359. dom = this.get_node(obj.id, true);
  7360. if (dom && dom.length) {
  7361. dom.removeClass("jstree-loading").attr('aria-busy',false);
  7362. }
  7363. delete this._data.massload[obj.id];
  7364. return rslt;
  7365. }
  7366. return parent._load_node.call(this, obj, callback);
  7367. };
  7368. };
  7369. /**
  7370. * ### Search plugin
  7371. *
  7372. * Adds search functionality to jsTree.
  7373. */
  7374. /**
  7375. * stores all defaults for the search plugin
  7376. * @name $.jstree.defaults.search
  7377. * @plugin search
  7378. */
  7379. $.jstree.defaults.search = {
  7380. /**
  7381. * a jQuery-like AJAX config, which jstree uses if a server should be queried for results.
  7382. *
  7383. * 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.
  7384. * 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
  7385. * @name $.jstree.defaults.search.ajax
  7386. * @plugin search
  7387. */
  7388. ajax : false,
  7389. /**
  7390. * Indicates if the search should be fuzzy or not (should `chnd3` match `child node 3`). Default is `false`.
  7391. * @name $.jstree.defaults.search.fuzzy
  7392. * @plugin search
  7393. */
  7394. fuzzy : false,
  7395. /**
  7396. * Indicates if the search should be case sensitive. Default is `false`.
  7397. * @name $.jstree.defaults.search.case_sensitive
  7398. * @plugin search
  7399. */
  7400. case_sensitive : false,
  7401. /**
  7402. * 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).
  7403. * This setting can be changed at runtime when calling the search method. Default is `false`.
  7404. * @name $.jstree.defaults.search.show_only_matches
  7405. * @plugin search
  7406. */
  7407. show_only_matches : false,
  7408. /**
  7409. * Indicates if the children of matched element are shown (when show_only_matches is true)
  7410. * This setting can be changed at runtime when calling the search method. Default is `false`.
  7411. * @name $.jstree.defaults.search.show_only_matches_children
  7412. * @plugin search
  7413. */
  7414. show_only_matches_children : false,
  7415. /**
  7416. * 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`.
  7417. * @name $.jstree.defaults.search.close_opened_onclear
  7418. * @plugin search
  7419. */
  7420. close_opened_onclear : true,
  7421. /**
  7422. * Indicates if only leaf nodes should be included in search results. Default is `false`.
  7423. * @name $.jstree.defaults.search.search_leaves_only
  7424. * @plugin search
  7425. */
  7426. search_leaves_only : false,
  7427. /**
  7428. * 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).
  7429. * 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`.
  7430. * @name $.jstree.defaults.search.search_callback
  7431. * @plugin search
  7432. */
  7433. search_callback : false
  7434. };
  7435. $.jstree.plugins.search = function (options, parent) {
  7436. this.bind = function () {
  7437. parent.bind.call(this);
  7438. this._data.search.str = "";
  7439. this._data.search.dom = $();
  7440. this._data.search.res = [];
  7441. this._data.search.opn = [];
  7442. this._data.search.som = false;
  7443. this._data.search.smc = false;
  7444. this._data.search.hdn = [];
  7445. this.element
  7446. .on("search.jstree", function (e, data) {
  7447. if(this._data.search.som && data.res.length) {
  7448. var m = this._model.data, i, j, p = [], k, l;
  7449. for(i = 0, j = data.res.length; i < j; i++) {
  7450. if(m[data.res[i]] && !m[data.res[i]].state.hidden) {
  7451. p.push(data.res[i]);
  7452. p = p.concat(m[data.res[i]].parents);
  7453. if(this._data.search.smc) {
  7454. for (k = 0, l = m[data.res[i]].children_d.length; k < l; k++) {
  7455. if (m[m[data.res[i]].children_d[k]] && !m[m[data.res[i]].children_d[k]].state.hidden) {
  7456. p.push(m[data.res[i]].children_d[k]);
  7457. }
  7458. }
  7459. }
  7460. }
  7461. }
  7462. p = $.vakata.array_remove_item($.vakata.array_unique(p), $.jstree.root);
  7463. this._data.search.hdn = this.hide_all(true);
  7464. this.show_node(p, true);
  7465. this.redraw(true);
  7466. }
  7467. }.bind(this))
  7468. .on("clear_search.jstree", function (e, data) {
  7469. if(this._data.search.som && data.res.length) {
  7470. this.show_node(this._data.search.hdn, true);
  7471. this.redraw(true);
  7472. }
  7473. }.bind(this));
  7474. };
  7475. /**
  7476. * used to search the tree nodes for a given string
  7477. * @name search(str [, skip_async])
  7478. * @param {String} str the search string
  7479. * @param {Boolean} skip_async if set to true server will not be queried even if configured
  7480. * @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)
  7481. * @param {mixed} inside an optional node to whose children to limit the search
  7482. * @param {Boolean} append if set to true the results of this search are appended to the previous search
  7483. * @plugin search
  7484. * @trigger search.jstree
  7485. */
  7486. this.search = function (str, skip_async, show_only_matches, inside, append, show_only_matches_children) {
  7487. if(str === false || $.vakata.trim(str.toString()) === "") {
  7488. return this.clear_search();
  7489. }
  7490. inside = this.get_node(inside);
  7491. inside = inside && (inside.id || inside.id === 0) ? inside.id : null;
  7492. str = str.toString();
  7493. var s = this.settings.search,
  7494. a = s.ajax ? s.ajax : false,
  7495. m = this._model.data,
  7496. f = null,
  7497. r = [],
  7498. p = [], i, j;
  7499. if(this._data.search.res.length && !append) {
  7500. this.clear_search();
  7501. }
  7502. if(show_only_matches === undefined) {
  7503. show_only_matches = s.show_only_matches;
  7504. }
  7505. if(show_only_matches_children === undefined) {
  7506. show_only_matches_children = s.show_only_matches_children;
  7507. }
  7508. if(!skip_async && a !== false) {
  7509. if($.vakata.is_function(a)) {
  7510. return a.call(this, str, function (d) {
  7511. if(d && d.d) { d = d.d; }
  7512. this._load_nodes(!$.vakata.is_array(d) ? [] : $.vakata.array_unique(d), function () {
  7513. this.search(str, true, show_only_matches, inside, append, show_only_matches_children);
  7514. });
  7515. }.bind(this), inside);
  7516. }
  7517. else {
  7518. a = $.extend({}, a);
  7519. if(!a.data) { a.data = {}; }
  7520. a.data.str = str;
  7521. if(inside) {
  7522. a.data.inside = inside;
  7523. }
  7524. if (this._data.search.lastRequest) {
  7525. this._data.search.lastRequest.abort();
  7526. }
  7527. this._data.search.lastRequest = $.ajax(a)
  7528. .fail(function () {
  7529. this._data.core.last_error = { 'error' : 'ajax', 'plugin' : 'search', 'id' : 'search_01', 'reason' : 'Could not load search parents', 'data' : JSON.stringify(a) };
  7530. this.settings.core.error.call(this, this._data.core.last_error);
  7531. }.bind(this))
  7532. .done(function (d) {
  7533. if(d && d.d) { d = d.d; }
  7534. this._load_nodes(!$.vakata.is_array(d) ? [] : $.vakata.array_unique(d), function () {
  7535. this.search(str, true, show_only_matches, inside, append, show_only_matches_children);
  7536. });
  7537. }.bind(this));
  7538. return this._data.search.lastRequest;
  7539. }
  7540. }
  7541. if(!append) {
  7542. this._data.search.str = str;
  7543. this._data.search.dom = $();
  7544. this._data.search.res = [];
  7545. this._data.search.opn = [];
  7546. this._data.search.som = show_only_matches;
  7547. this._data.search.smc = show_only_matches_children;
  7548. }
  7549. f = new $.vakata.search(str, true, { caseSensitive : s.case_sensitive, fuzzy : s.fuzzy });
  7550. $.each(m[inside ? inside : $.jstree.root].children_d, function (ii, i) {
  7551. var v = m[i];
  7552. if(v.text && !v.state.hidden && (!s.search_leaves_only || (v.state.loaded && v.children.length === 0)) && ( (s.search_callback && s.search_callback.call(this, str, v)) || (!s.search_callback && f.search(v.text).isMatch) ) ) {
  7553. r.push(i);
  7554. p = p.concat(v.parents);
  7555. }
  7556. });
  7557. if(r.length) {
  7558. p = $.vakata.array_unique(p);
  7559. for(i = 0, j = p.length; i < j; i++) {
  7560. if(p[i] !== $.jstree.root && m[p[i]] && this.open_node(p[i], null, 0) === true) {
  7561. this._data.search.opn.push(p[i]);
  7562. }
  7563. }
  7564. if(!append) {
  7565. 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(', #')));
  7566. this._data.search.res = r;
  7567. }
  7568. else {
  7569. 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(', #'))));
  7570. this._data.search.res = $.vakata.array_unique(this._data.search.res.concat(r));
  7571. }
  7572. this._data.search.dom.children(".jstree-anchor").addClass('jstree-search');
  7573. }
  7574. /**
  7575. * triggered after search is complete
  7576. * @event
  7577. * @name search.jstree
  7578. * @param {jQuery} nodes a jQuery collection of matching nodes
  7579. * @param {String} str the search string
  7580. * @param {Array} res a collection of objects represeing the matching nodes
  7581. * @plugin search
  7582. */
  7583. this.trigger('search', { nodes : this._data.search.dom, str : str, res : this._data.search.res, show_only_matches : show_only_matches });
  7584. };
  7585. /**
  7586. * used to clear the last search (removes classes and shows all nodes if filtering is on)
  7587. * @name clear_search()
  7588. * @plugin search
  7589. * @trigger clear_search.jstree
  7590. */
  7591. this.clear_search = function () {
  7592. if(this.settings.search.close_opened_onclear) {
  7593. this.close_node(this._data.search.opn, 0);
  7594. }
  7595. /**
  7596. * triggered after search is complete
  7597. * @event
  7598. * @name clear_search.jstree
  7599. * @param {jQuery} nodes a jQuery collection of matching nodes (the result from the last search)
  7600. * @param {String} str the search string (the last search string)
  7601. * @param {Array} res a collection of objects represeing the matching nodes (the result from the last search)
  7602. * @plugin search
  7603. */
  7604. this.trigger('clear_search', { 'nodes' : this._data.search.dom, str : this._data.search.str, res : this._data.search.res });
  7605. if(this._data.search.res.length) {
  7606. this._data.search.dom = $(this.element[0].querySelectorAll('#' + $.map(this._data.search.res, function (v) {
  7607. return "0123456789".indexOf(v[0]) !== -1 ? '\\3' + v[0] + ' ' + v.substr(1).replace($.jstree.idregex,'\\$&') : v.replace($.jstree.idregex,'\\$&');
  7608. }).join(', #')));
  7609. this._data.search.dom.children(".jstree-anchor").removeClass("jstree-search");
  7610. }
  7611. this._data.search.str = "";
  7612. this._data.search.res = [];
  7613. this._data.search.opn = [];
  7614. this._data.search.dom = $();
  7615. };
  7616. this.redraw_node = function(obj, deep, callback, force_render) {
  7617. obj = parent.redraw_node.apply(this, arguments);
  7618. if(obj) {
  7619. if($.inArray(obj.id, this._data.search.res) !== -1) {
  7620. var i, j, tmp = null;
  7621. for(i = 0, j = obj.childNodes.length; i < j; i++) {
  7622. if(obj.childNodes[i] && obj.childNodes[i].className && obj.childNodes[i].className.indexOf("jstree-anchor") !== -1) {
  7623. tmp = obj.childNodes[i];
  7624. break;
  7625. }
  7626. }
  7627. if(tmp) {
  7628. tmp.className += ' jstree-search';
  7629. }
  7630. }
  7631. }
  7632. return obj;
  7633. };
  7634. };
  7635. // helpers
  7636. (function ($) {
  7637. // from http://kiro.me/projects/fuse.html
  7638. $.vakata.search = function(pattern, txt, options) {
  7639. options = options || {};
  7640. options = $.extend({}, $.vakata.search.defaults, options);
  7641. if(options.fuzzy !== false) {
  7642. options.fuzzy = true;
  7643. }
  7644. pattern = options.caseSensitive ? pattern : pattern.toLowerCase();
  7645. var MATCH_LOCATION = options.location,
  7646. MATCH_DISTANCE = options.distance,
  7647. MATCH_THRESHOLD = options.threshold,
  7648. patternLen = pattern.length,
  7649. matchmask, pattern_alphabet, match_bitapScore, search;
  7650. if(patternLen > 32) {
  7651. options.fuzzy = false;
  7652. }
  7653. if(options.fuzzy) {
  7654. matchmask = 1 << (patternLen - 1);
  7655. pattern_alphabet = (function () {
  7656. var mask = {},
  7657. i = 0;
  7658. for (i = 0; i < patternLen; i++) {
  7659. mask[pattern.charAt(i)] = 0;
  7660. }
  7661. for (i = 0; i < patternLen; i++) {
  7662. mask[pattern.charAt(i)] |= 1 << (patternLen - i - 1);
  7663. }
  7664. return mask;
  7665. }());
  7666. match_bitapScore = function (e, x) {
  7667. var accuracy = e / patternLen,
  7668. proximity = Math.abs(MATCH_LOCATION - x);
  7669. if(!MATCH_DISTANCE) {
  7670. return proximity ? 1.0 : accuracy;
  7671. }
  7672. return accuracy + (proximity / MATCH_DISTANCE);
  7673. };
  7674. }
  7675. search = function (text) {
  7676. text = options.caseSensitive ? text.toString() : text.toString().toLowerCase();
  7677. if(pattern === text || text.indexOf(pattern) !== -1) {
  7678. return {
  7679. isMatch: true,
  7680. score: 0
  7681. };
  7682. }
  7683. if(!options.fuzzy) {
  7684. return {
  7685. isMatch: false,
  7686. score: 1
  7687. };
  7688. }
  7689. var i, j,
  7690. textLen = text.length,
  7691. scoreThreshold = MATCH_THRESHOLD,
  7692. bestLoc = text.indexOf(pattern, MATCH_LOCATION),
  7693. binMin, binMid,
  7694. binMax = patternLen + textLen,
  7695. lastRd, start, finish, rd, charMatch,
  7696. score = 1,
  7697. locations = [];
  7698. if (bestLoc !== -1) {
  7699. scoreThreshold = Math.min(match_bitapScore(0, bestLoc), scoreThreshold);
  7700. bestLoc = text.lastIndexOf(pattern, MATCH_LOCATION + patternLen);
  7701. if (bestLoc !== -1) {
  7702. scoreThreshold = Math.min(match_bitapScore(0, bestLoc), scoreThreshold);
  7703. }
  7704. }
  7705. bestLoc = -1;
  7706. for (i = 0; i < patternLen; i++) {
  7707. binMin = 0;
  7708. binMid = binMax;
  7709. while (binMin < binMid) {
  7710. if (match_bitapScore(i, MATCH_LOCATION + binMid) <= scoreThreshold) {
  7711. binMin = binMid;
  7712. } else {
  7713. binMax = binMid;
  7714. }
  7715. binMid = Math.floor((binMax - binMin) / 2 + binMin);
  7716. }
  7717. binMax = binMid;
  7718. start = Math.max(1, MATCH_LOCATION - binMid + 1);
  7719. finish = Math.min(MATCH_LOCATION + binMid, textLen) + patternLen;
  7720. rd = new Array(finish + 2);
  7721. rd[finish + 1] = (1 << i) - 1;
  7722. for (j = finish; j >= start; j--) {
  7723. charMatch = pattern_alphabet[text.charAt(j - 1)];
  7724. if (i === 0) {
  7725. rd[j] = ((rd[j + 1] << 1) | 1) & charMatch;
  7726. } else {
  7727. rd[j] = ((rd[j + 1] << 1) | 1) & charMatch | (((lastRd[j + 1] | lastRd[j]) << 1) | 1) | lastRd[j + 1];
  7728. }
  7729. if (rd[j] & matchmask) {
  7730. score = match_bitapScore(i, j - 1);
  7731. if (score <= scoreThreshold) {
  7732. scoreThreshold = score;
  7733. bestLoc = j - 1;
  7734. locations.push(bestLoc);
  7735. if (bestLoc > MATCH_LOCATION) {
  7736. start = Math.max(1, 2 * MATCH_LOCATION - bestLoc);
  7737. } else {
  7738. break;
  7739. }
  7740. }
  7741. }
  7742. }
  7743. if (match_bitapScore(i + 1, MATCH_LOCATION) > scoreThreshold) {
  7744. break;
  7745. }
  7746. lastRd = rd;
  7747. }
  7748. return {
  7749. isMatch: bestLoc >= 0,
  7750. score: score
  7751. };
  7752. };
  7753. return txt === true ? { 'search' : search } : search(txt);
  7754. };
  7755. $.vakata.search.defaults = {
  7756. location : 0,
  7757. distance : 100,
  7758. threshold : 0.6,
  7759. fuzzy : false,
  7760. caseSensitive : false
  7761. };
  7762. }($));
  7763. // include the search plugin by default
  7764. // $.jstree.defaults.plugins.push("search");
  7765. /**
  7766. * ### Sort plugin
  7767. *
  7768. * Automatically sorts all siblings in the tree according to a sorting function.
  7769. */
  7770. /**
  7771. * the settings function used to sort the nodes.
  7772. * It is executed in the tree's context, accepts two nodes as arguments and should return `1` or `-1`.
  7773. * @name $.jstree.defaults.sort
  7774. * @plugin sort
  7775. */
  7776. $.jstree.defaults.sort = function (a, b) {
  7777. //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);
  7778. return this.get_text(a) > this.get_text(b) ? 1 : -1;
  7779. };
  7780. $.jstree.plugins.sort = function (options, parent) {
  7781. this.bind = function () {
  7782. parent.bind.call(this);
  7783. this.element
  7784. .on("model.jstree", function (e, data) {
  7785. this.sort(data.parent, true);
  7786. }.bind(this))
  7787. .on("rename_node.jstree create_node.jstree", function (e, data) {
  7788. this.sort(data.parent || data.node.parent, false);
  7789. this.redraw_node(data.parent || data.node.parent, true);
  7790. }.bind(this))
  7791. .on("move_node.jstree copy_node.jstree", function (e, data) {
  7792. this.sort(data.parent, false);
  7793. this.redraw_node(data.parent, true);
  7794. }.bind(this));
  7795. };
  7796. /**
  7797. * used to sort a node's children
  7798. * @private
  7799. * @name sort(obj [, deep])
  7800. * @param {mixed} obj the node
  7801. * @param {Boolean} deep if set to `true` nodes are sorted recursively.
  7802. * @plugin sort
  7803. * @trigger search.jstree
  7804. */
  7805. this.sort = function (obj, deep) {
  7806. var i, j;
  7807. obj = this.get_node(obj);
  7808. if(obj && obj.children && obj.children.length) {
  7809. obj.children.sort(this.settings.sort.bind(this));
  7810. if(deep) {
  7811. for(i = 0, j = obj.children_d.length; i < j; i++) {
  7812. this.sort(obj.children_d[i], false);
  7813. }
  7814. }
  7815. }
  7816. };
  7817. };
  7818. // include the sort plugin by default
  7819. // $.jstree.defaults.plugins.push("sort");
  7820. /**
  7821. * ### State plugin
  7822. *
  7823. * Saves the state of the tree (selected nodes, opened nodes) on the user's computer using available options (localStorage, cookies, etc)
  7824. */
  7825. var to = false;
  7826. /**
  7827. * stores all defaults for the state plugin
  7828. * @name $.jstree.defaults.state
  7829. * @plugin state
  7830. */
  7831. $.jstree.defaults.state = {
  7832. /**
  7833. * A string for the key to use when saving the current tree (change if using multiple trees in your project). Defaults to `jstree`.
  7834. * @name $.jstree.defaults.state.key
  7835. * @plugin state
  7836. */
  7837. key : 'jstree',
  7838. /**
  7839. * A space separated list of events that trigger a state save. Defaults to `changed.jstree open_node.jstree close_node.jstree`.
  7840. * @name $.jstree.defaults.state.events
  7841. * @plugin state
  7842. */
  7843. events : 'changed.jstree open_node.jstree close_node.jstree check_node.jstree uncheck_node.jstree',
  7844. /**
  7845. * Time in milliseconds after which the state will expire. Defaults to 'false' meaning - no expire.
  7846. * @name $.jstree.defaults.state.ttl
  7847. * @plugin state
  7848. */
  7849. ttl : false,
  7850. /**
  7851. * 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.
  7852. * @name $.jstree.defaults.state.filter
  7853. * @plugin state
  7854. */
  7855. filter : false,
  7856. /**
  7857. * Should loaded nodes be restored (setting this to true means that it is possible that the whole tree will be loaded for some users - use with caution). Defaults to `false`
  7858. * @name $.jstree.defaults.state.preserve_loaded
  7859. * @plugin state
  7860. */
  7861. preserve_loaded : false
  7862. };
  7863. $.jstree.plugins.state = function (options, parent) {
  7864. this.bind = function () {
  7865. parent.bind.call(this);
  7866. var bind = function () {
  7867. this.element.on(this.settings.state.events, function () {
  7868. if(to) { clearTimeout(to); }
  7869. to = setTimeout(function () { this.save_state(); }.bind(this), 100);
  7870. }.bind(this));
  7871. /**
  7872. * triggered when the state plugin is finished restoring the state (and immediately after ready if there is no state to restore).
  7873. * @event
  7874. * @name state_ready.jstree
  7875. * @plugin state
  7876. */
  7877. this.trigger('state_ready');
  7878. }.bind(this);
  7879. this.element
  7880. .on("ready.jstree", function (e, data) {
  7881. this.element.one("restore_state.jstree", bind);
  7882. if(!this.restore_state()) { bind(); }
  7883. }.bind(this));
  7884. };
  7885. /**
  7886. * save the state
  7887. * @name save_state()
  7888. * @plugin state
  7889. */
  7890. this.save_state = function () {
  7891. var tm = this.get_state();
  7892. if (!this.settings.state.preserve_loaded) {
  7893. delete tm.core.loaded;
  7894. }
  7895. var st = { 'state' : tm, 'ttl' : this.settings.state.ttl, 'sec' : +(new Date()) };
  7896. $.vakata.storage.set(this.settings.state.key, JSON.stringify(st));
  7897. };
  7898. /**
  7899. * restore the state from the user's computer
  7900. * @name restore_state()
  7901. * @plugin state
  7902. */
  7903. this.restore_state = function () {
  7904. var k = $.vakata.storage.get(this.settings.state.key);
  7905. if(!!k) { try { k = JSON.parse(k); } catch(ex) { return false; } }
  7906. if(!!k && k.ttl && k.sec && +(new Date()) - k.sec > k.ttl) { return false; }
  7907. if(!!k && k.state) { k = k.state; }
  7908. if(!!k && $.vakata.is_function(this.settings.state.filter)) { k = this.settings.state.filter.call(this, k); }
  7909. if(!!k) {
  7910. if (!this.settings.state.preserve_loaded) {
  7911. delete k.core.loaded;
  7912. }
  7913. this.element.one("set_state.jstree", function (e, data) { data.instance.trigger('restore_state', { 'state' : $.extend(true, {}, k) }); });
  7914. this.set_state(k);
  7915. return true;
  7916. }
  7917. return false;
  7918. };
  7919. /**
  7920. * clear the state on the user's computer
  7921. * @name clear_state()
  7922. * @plugin state
  7923. */
  7924. this.clear_state = function () {
  7925. return $.vakata.storage.del(this.settings.state.key);
  7926. };
  7927. };
  7928. (function ($, undefined) {
  7929. $.vakata.storage = {
  7930. // simply specifying the functions in FF throws an error
  7931. set : function (key, val) { return window.localStorage.setItem(key, val); },
  7932. get : function (key) { return window.localStorage.getItem(key); },
  7933. del : function (key) { return window.localStorage.removeItem(key); }
  7934. };
  7935. }($));
  7936. // include the state plugin by default
  7937. // $.jstree.defaults.plugins.push("state");
  7938. /**
  7939. * ### Types plugin
  7940. *
  7941. * 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.
  7942. */
  7943. /**
  7944. * 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).
  7945. *
  7946. * * `max_children` the maximum number of immediate children this node type can have. Do not specify or set to `-1` for unlimited.
  7947. * * `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.
  7948. * * `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.
  7949. * * `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.
  7950. * * `li_attr` an object of values which will be used to add HTML attributes on the resulting LI DOM node (merged with the node's own data)
  7951. * * `a_attr` an object of values which will be used to add HTML attributes on the resulting A DOM node (merged with the node's own data)
  7952. *
  7953. * There are two predefined types:
  7954. *
  7955. * * `#` represents the root of the tree, for example `max_children` would control the maximum number of root nodes.
  7956. * * `default` represents the default node - any settings here will be applied to all nodes that do not have a type specified.
  7957. *
  7958. * @name $.jstree.defaults.types
  7959. * @plugin types
  7960. */
  7961. $.jstree.defaults.types = {
  7962. 'default' : {}
  7963. };
  7964. $.jstree.defaults.types[$.jstree.root] = {};
  7965. $.jstree.plugins.types = function (options, parent) {
  7966. this.init = function (el, options) {
  7967. var i, j;
  7968. if(options && options.types && options.types['default']) {
  7969. for(i in options.types) {
  7970. if(i !== "default" && i !== $.jstree.root && options.types.hasOwnProperty(i)) {
  7971. for(j in options.types['default']) {
  7972. if(options.types['default'].hasOwnProperty(j) && options.types[i][j] === undefined) {
  7973. options.types[i][j] = options.types['default'][j];
  7974. }
  7975. }
  7976. }
  7977. }
  7978. }
  7979. parent.init.call(this, el, options);
  7980. this._model.data[$.jstree.root].type = $.jstree.root;
  7981. };
  7982. this.refresh = function (skip_loading, forget_state) {
  7983. parent.refresh.call(this, skip_loading, forget_state);
  7984. this._model.data[$.jstree.root].type = $.jstree.root;
  7985. };
  7986. this.bind = function () {
  7987. this.element
  7988. .on('model.jstree', function (e, data) {
  7989. var m = this._model.data,
  7990. dpc = data.nodes,
  7991. t = this.settings.types,
  7992. i, j, c = 'default', k;
  7993. for(i = 0, j = dpc.length; i < j; i++) {
  7994. c = 'default';
  7995. if(m[dpc[i]].original && m[dpc[i]].original.type && t[m[dpc[i]].original.type]) {
  7996. c = m[dpc[i]].original.type;
  7997. }
  7998. if(m[dpc[i]].data && m[dpc[i]].data.jstree && m[dpc[i]].data.jstree.type && t[m[dpc[i]].data.jstree.type]) {
  7999. c = m[dpc[i]].data.jstree.type;
  8000. }
  8001. m[dpc[i]].type = c;
  8002. if(m[dpc[i]].icon === true && t[c].icon !== undefined) {
  8003. m[dpc[i]].icon = t[c].icon;
  8004. }
  8005. if(t[c].li_attr !== undefined && typeof t[c].li_attr === 'object') {
  8006. for (k in t[c].li_attr) {
  8007. if (t[c].li_attr.hasOwnProperty(k)) {
  8008. if (k === 'id') {
  8009. continue;
  8010. }
  8011. else if (m[dpc[i]].li_attr[k] === undefined) {
  8012. m[dpc[i]].li_attr[k] = t[c].li_attr[k];
  8013. }
  8014. else if (k === 'class') {
  8015. m[dpc[i]].li_attr['class'] = t[c].li_attr['class'] + ' ' + m[dpc[i]].li_attr['class'];
  8016. }
  8017. }
  8018. }
  8019. }
  8020. if(t[c].a_attr !== undefined && typeof t[c].a_attr === 'object') {
  8021. for (k in t[c].a_attr) {
  8022. if (t[c].a_attr.hasOwnProperty(k)) {
  8023. if (k === 'id') {
  8024. continue;
  8025. }
  8026. else if (m[dpc[i]].a_attr[k] === undefined) {
  8027. m[dpc[i]].a_attr[k] = t[c].a_attr[k];
  8028. }
  8029. else if (k === 'href' && m[dpc[i]].a_attr[k] === '#') {
  8030. m[dpc[i]].a_attr['href'] = t[c].a_attr['href'];
  8031. }
  8032. else if (k === 'class') {
  8033. m[dpc[i]].a_attr['class'] = t[c].a_attr['class'] + ' ' + m[dpc[i]].a_attr['class'];
  8034. }
  8035. }
  8036. }
  8037. }
  8038. }
  8039. m[$.jstree.root].type = $.jstree.root;
  8040. }.bind(this));
  8041. parent.bind.call(this);
  8042. };
  8043. this.get_json = function (obj, options, flat) {
  8044. var i, j,
  8045. m = this._model.data,
  8046. opt = options ? $.extend(true, {}, options, {no_id:false}) : {},
  8047. tmp = parent.get_json.call(this, obj, opt, flat);
  8048. if(tmp === false) { return false; }
  8049. if($.vakata.is_array(tmp)) {
  8050. for(i = 0, j = tmp.length; i < j; i++) {
  8051. tmp[i].type = (tmp[i].id || tmp[i].id === 0) && m[tmp[i].id] && m[tmp[i].id].type ? m[tmp[i].id].type : "default";
  8052. if(options && options.no_id) {
  8053. delete tmp[i].id;
  8054. if(tmp[i].li_attr && tmp[i].li_attr.id) {
  8055. delete tmp[i].li_attr.id;
  8056. }
  8057. if(tmp[i].a_attr && tmp[i].a_attr.id) {
  8058. delete tmp[i].a_attr.id;
  8059. }
  8060. }
  8061. }
  8062. }
  8063. else {
  8064. tmp.type = (tmp.id || tmp.id === 0) && m[tmp.id] && m[tmp.id].type ? m[tmp.id].type : "default";
  8065. if(options && options.no_id) {
  8066. tmp = this._delete_ids(tmp);
  8067. }
  8068. }
  8069. return tmp;
  8070. };
  8071. this._delete_ids = function (tmp) {
  8072. if($.vakata.is_array(tmp)) {
  8073. for(var i = 0, j = tmp.length; i < j; i++) {
  8074. tmp[i] = this._delete_ids(tmp[i]);
  8075. }
  8076. return tmp;
  8077. }
  8078. delete tmp.id;
  8079. if(tmp.li_attr && tmp.li_attr.id) {
  8080. delete tmp.li_attr.id;
  8081. }
  8082. if(tmp.a_attr && tmp.a_attr.id) {
  8083. delete tmp.a_attr.id;
  8084. }
  8085. if(tmp.children && $.vakata.is_array(tmp.children)) {
  8086. tmp.children = this._delete_ids(tmp.children);
  8087. }
  8088. return tmp;
  8089. };
  8090. this.check = function (chk, obj, par, pos, more) {
  8091. if(parent.check.call(this, chk, obj, par, pos, more) === false) { return false; }
  8092. obj = obj && (obj.id || obj.id === 0) ? obj : this.get_node(obj);
  8093. par = par && (par.id || par.id === 0) ? par : this.get_node(par);
  8094. var m = obj && (obj.id || obj.id === 0) ? (more && more.origin ? more.origin : $.jstree.reference(obj.id)) : null, tmp, d, i, j;
  8095. m = m && m._model && m._model.data ? m._model.data : null;
  8096. switch(chk) {
  8097. case "create_node":
  8098. case "move_node":
  8099. case "copy_node":
  8100. if(chk !== 'move_node' || $.inArray(obj.id, par.children) === -1) {
  8101. tmp = this.get_rules(par);
  8102. if(tmp.max_children !== undefined && tmp.max_children !== -1 && tmp.max_children === par.children.length) {
  8103. 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 === 0) ? obj.id : false, 'par' : par && (par.id || par.id === 0) ? par.id : false }) };
  8104. return false;
  8105. }
  8106. if(tmp.valid_children !== undefined && tmp.valid_children !== -1 && $.inArray((obj.type || 'default'), tmp.valid_children) === -1) {
  8107. 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 === 0) ? obj.id : false, 'par' : par && (par.id || par.id === 0) ? par.id : false }) };
  8108. return false;
  8109. }
  8110. if(m && obj.children_d && obj.parents) {
  8111. d = 0;
  8112. for(i = 0, j = obj.children_d.length; i < j; i++) {
  8113. d = Math.max(d, m[obj.children_d[i]].parents.length);
  8114. }
  8115. d = d - obj.parents.length + 1;
  8116. }
  8117. if(d <= 0 || d === undefined) { d = 1; }
  8118. do {
  8119. if(tmp.max_depth !== undefined && tmp.max_depth !== -1 && tmp.max_depth < d) {
  8120. 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 === 0) ? obj.id : false, 'par' : par && (par.id || par.id === 0) ? par.id : false }) };
  8121. return false;
  8122. }
  8123. par = this.get_node(par.parent);
  8124. tmp = this.get_rules(par);
  8125. d++;
  8126. } while(par);
  8127. }
  8128. break;
  8129. }
  8130. return true;
  8131. };
  8132. /**
  8133. * used to retrieve the type settings object for a node
  8134. * @name get_rules(obj)
  8135. * @param {mixed} obj the node to find the rules for
  8136. * @return {Object}
  8137. * @plugin types
  8138. */
  8139. this.get_rules = function (obj) {
  8140. obj = this.get_node(obj);
  8141. if(!obj) { return false; }
  8142. var tmp = this.get_type(obj, true);
  8143. if(tmp.max_depth === undefined) { tmp.max_depth = -1; }
  8144. if(tmp.max_children === undefined) { tmp.max_children = -1; }
  8145. if(tmp.valid_children === undefined) { tmp.valid_children = -1; }
  8146. return tmp;
  8147. };
  8148. /**
  8149. * used to retrieve the type string or settings object for a node
  8150. * @name get_type(obj [, rules])
  8151. * @param {mixed} obj the node to find the rules for
  8152. * @param {Boolean} rules if set to `true` instead of a string the settings object will be returned
  8153. * @return {String|Object}
  8154. * @plugin types
  8155. */
  8156. this.get_type = function (obj, rules) {
  8157. obj = this.get_node(obj);
  8158. return (!obj) ? false : ( rules ? $.extend({ 'type' : obj.type }, this.settings.types[obj.type]) : obj.type);
  8159. };
  8160. /**
  8161. * used to change a node's type
  8162. * @name set_type(obj, type)
  8163. * @param {mixed} obj the node to change
  8164. * @param {String} type the new type
  8165. * @plugin types
  8166. */
  8167. this.set_type = function (obj, type) {
  8168. var m = this._model.data, t, t1, t2, old_type, old_icon, k, d, a;
  8169. if($.vakata.is_array(obj)) {
  8170. obj = obj.slice();
  8171. for(t1 = 0, t2 = obj.length; t1 < t2; t1++) {
  8172. this.set_type(obj[t1], type);
  8173. }
  8174. return true;
  8175. }
  8176. t = this.settings.types;
  8177. obj = this.get_node(obj);
  8178. if(!t[type] || !obj) { return false; }
  8179. d = this.get_node(obj, true);
  8180. if (d && d.length) {
  8181. a = d.children('.jstree-anchor');
  8182. }
  8183. old_type = obj.type;
  8184. old_icon = this.get_icon(obj);
  8185. obj.type = type;
  8186. if(old_icon === true || !t[old_type] || (t[old_type].icon !== undefined && old_icon === t[old_type].icon)) {
  8187. this.set_icon(obj, t[type].icon !== undefined ? t[type].icon : true);
  8188. }
  8189. // remove old type props
  8190. if(t[old_type] && t[old_type].li_attr !== undefined && typeof t[old_type].li_attr === 'object') {
  8191. for (k in t[old_type].li_attr) {
  8192. if (t[old_type].li_attr.hasOwnProperty(k)) {
  8193. if (k === 'id') {
  8194. continue;
  8195. }
  8196. else if (k === 'class') {
  8197. m[obj.id].li_attr['class'] = (m[obj.id].li_attr['class'] || '').replace(t[old_type].li_attr[k], '');
  8198. if (d) { d.removeClass(t[old_type].li_attr[k]); }
  8199. }
  8200. else if (m[obj.id].li_attr[k] === t[old_type].li_attr[k]) {
  8201. m[obj.id].li_attr[k] = null;
  8202. if (d) { d.removeAttr(k); }
  8203. }
  8204. }
  8205. }
  8206. }
  8207. if(t[old_type] && t[old_type].a_attr !== undefined && typeof t[old_type].a_attr === 'object') {
  8208. for (k in t[old_type].a_attr) {
  8209. if (t[old_type].a_attr.hasOwnProperty(k)) {
  8210. if (k === 'id') {
  8211. continue;
  8212. }
  8213. else if (k === 'class') {
  8214. m[obj.id].a_attr['class'] = (m[obj.id].a_attr['class'] || '').replace(t[old_type].a_attr[k], '');
  8215. if (a) { a.removeClass(t[old_type].a_attr[k]); }
  8216. }
  8217. else if (m[obj.id].a_attr[k] === t[old_type].a_attr[k]) {
  8218. if (k === 'href') {
  8219. m[obj.id].a_attr[k] = '#';
  8220. if (a) { a.attr('href', '#'); }
  8221. }
  8222. else {
  8223. delete m[obj.id].a_attr[k];
  8224. if (a) { a.removeAttr(k); }
  8225. }
  8226. }
  8227. }
  8228. }
  8229. }
  8230. // add new props
  8231. if(t[type].li_attr !== undefined && typeof t[type].li_attr === 'object') {
  8232. for (k in t[type].li_attr) {
  8233. if (t[type].li_attr.hasOwnProperty(k)) {
  8234. if (k === 'id') {
  8235. continue;
  8236. }
  8237. else if (m[obj.id].li_attr[k] === undefined) {
  8238. m[obj.id].li_attr[k] = t[type].li_attr[k];
  8239. if (d) {
  8240. if (k === 'class') {
  8241. d.addClass(t[type].li_attr[k]);
  8242. }
  8243. else {
  8244. d.attr(k, t[type].li_attr[k]);
  8245. }
  8246. }
  8247. }
  8248. else if (k === 'class') {
  8249. m[obj.id].li_attr['class'] = t[type].li_attr[k] + ' ' + m[obj.id].li_attr['class'];
  8250. if (d) { d.addClass(t[type].li_attr[k]); }
  8251. }
  8252. }
  8253. }
  8254. }
  8255. if(t[type].a_attr !== undefined && typeof t[type].a_attr === 'object') {
  8256. for (k in t[type].a_attr) {
  8257. if (t[type].a_attr.hasOwnProperty(k)) {
  8258. if (k === 'id') {
  8259. continue;
  8260. }
  8261. else if (m[obj.id].a_attr[k] === undefined) {
  8262. m[obj.id].a_attr[k] = t[type].a_attr[k];
  8263. if (a) {
  8264. if (k === 'class') {
  8265. a.addClass(t[type].a_attr[k]);
  8266. }
  8267. else {
  8268. a.attr(k, t[type].a_attr[k]);
  8269. }
  8270. }
  8271. }
  8272. else if (k === 'href' && m[obj.id].a_attr[k] === '#') {
  8273. m[obj.id].a_attr['href'] = t[type].a_attr['href'];
  8274. if (a) { a.attr('href', t[type].a_attr['href']); }
  8275. }
  8276. else if (k === 'class') {
  8277. m[obj.id].a_attr['class'] = t[type].a_attr['class'] + ' ' + m[obj.id].a_attr['class'];
  8278. if (a) { a.addClass(t[type].a_attr[k]); }
  8279. }
  8280. }
  8281. }
  8282. }
  8283. return true;
  8284. };
  8285. };
  8286. // include the types plugin by default
  8287. // $.jstree.defaults.plugins.push("types");
  8288. /**
  8289. * ### Unique plugin
  8290. *
  8291. * Enforces that no nodes with the same name can coexist as siblings.
  8292. */
  8293. /**
  8294. * stores all defaults for the unique plugin
  8295. * @name $.jstree.defaults.unique
  8296. * @plugin unique
  8297. */
  8298. $.jstree.defaults.unique = {
  8299. /**
  8300. * Indicates if the comparison should be case sensitive. Default is `false`.
  8301. * @name $.jstree.defaults.unique.case_sensitive
  8302. * @plugin unique
  8303. */
  8304. case_sensitive : false,
  8305. /**
  8306. * Indicates if white space should be trimmed before the comparison. Default is `false`.
  8307. * @name $.jstree.defaults.unique.trim_whitespace
  8308. * @plugin unique
  8309. */
  8310. trim_whitespace : false,
  8311. /**
  8312. * A callback executed in the instance's scope when a new node is created with no name and a node with the default name already exists, the two arguments are the conflicting name and the counter. The default will produce results like `New node (2)`.
  8313. * @name $.jstree.defaults.unique.duplicate
  8314. * @plugin unique
  8315. */
  8316. duplicate : function (name, counter) {
  8317. return name + ' (' + counter + ')';
  8318. }
  8319. };
  8320. $.jstree.plugins.unique = function (options, parent) {
  8321. this.check = function (chk, obj, par, pos, more) {
  8322. if(parent.check.call(this, chk, obj, par, pos, more) === false) { return false; }
  8323. obj = obj && (obj.id || obj.id === 0) ? obj : this.get_node(obj);
  8324. par = par && (par.id || par.id === 0) ? par : this.get_node(par);
  8325. if(!par || !par.children) { return true; }
  8326. var n = chk === "rename_node" ? pos : obj.text,
  8327. c = [],
  8328. s = this.settings.unique.case_sensitive,
  8329. w = this.settings.unique.trim_whitespace,
  8330. m = this._model.data, i, j, t;
  8331. for(i = 0, j = par.children.length; i < j; i++) {
  8332. t = m[par.children[i]].text;
  8333. if (!s) {
  8334. t = t.toLowerCase();
  8335. }
  8336. if (w) {
  8337. t = t.replace(/^[\s\uFEFF\xA0]+|[\s\uFEFF\xA0]+$/g, '');
  8338. }
  8339. c.push(t);
  8340. }
  8341. if(!s) { n = n.toLowerCase(); }
  8342. if (w) { n = n.replace(/^[\s\uFEFF\xA0]+|[\s\uFEFF\xA0]+$/g, ''); }
  8343. switch(chk) {
  8344. case "delete_node":
  8345. return true;
  8346. case "rename_node":
  8347. t = obj.text || '';
  8348. if (!s) {
  8349. t = t.toLowerCase();
  8350. }
  8351. if (w) {
  8352. t = t.replace(/^[\s\uFEFF\xA0]+|[\s\uFEFF\xA0]+$/g, '');
  8353. }
  8354. i = ($.inArray(n, c) === -1 || (obj.text && t === n));
  8355. if(!i) {
  8356. 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 === 0) ? obj.id : false, 'par' : par && (par.id || par.id === 0) ? par.id : false }) };
  8357. }
  8358. return i;
  8359. case "create_node":
  8360. i = ($.inArray(n, c) === -1);
  8361. if(!i) {
  8362. 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 === 0) ? obj.id : false, 'par' : par && (par.id || par.id === 0) ? par.id : false }) };
  8363. }
  8364. return i;
  8365. case "copy_node":
  8366. i = ($.inArray(n, c) === -1);
  8367. if(!i) {
  8368. 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 === 0) ? obj.id : false, 'par' : par && (par.id || par.id === 0) ? par.id : false }) };
  8369. }
  8370. return i;
  8371. case "move_node":
  8372. i = ( (obj.parent === par.id && (!more || !more.is_multi)) || $.inArray(n, c) === -1);
  8373. if(!i) {
  8374. 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 === 0) ? obj.id : false, 'par' : par && (par.id || par.id === 0) ? par.id : false }) };
  8375. }
  8376. return i;
  8377. }
  8378. return true;
  8379. };
  8380. this.create_node = function (par, node, pos, callback, is_loaded) {
  8381. if(!node || (typeof node === 'object' && node.text === undefined)) {
  8382. if(par === null) {
  8383. par = $.jstree.root;
  8384. }
  8385. par = this.get_node(par);
  8386. if(!par) {
  8387. return parent.create_node.call(this, par, node, pos, callback, is_loaded);
  8388. }
  8389. pos = pos === undefined ? "last" : pos;
  8390. if(!pos.toString().match(/^(before|after)$/) && !is_loaded && !this.is_loaded(par)) {
  8391. return parent.create_node.call(this, par, node, pos, callback, is_loaded);
  8392. }
  8393. if(!node) { node = {}; }
  8394. var tmp, n, dpc, i, j, m = this._model.data, s = this.settings.unique.case_sensitive, w = this.settings.unique.trim_whitespace, cb = this.settings.unique.duplicate, t;
  8395. n = tmp = this.get_string('New node');
  8396. dpc = [];
  8397. for(i = 0, j = par.children.length; i < j; i++) {
  8398. t = m[par.children[i]].text;
  8399. if (!s) {
  8400. t = t.toLowerCase();
  8401. }
  8402. if (w) {
  8403. t = t.replace(/^[\s\uFEFF\xA0]+|[\s\uFEFF\xA0]+$/g, '');
  8404. }
  8405. dpc.push(t);
  8406. }
  8407. i = 1;
  8408. t = n;
  8409. if (!s) {
  8410. t = t.toLowerCase();
  8411. }
  8412. if (w) {
  8413. t = t.replace(/^[\s\uFEFF\xA0]+|[\s\uFEFF\xA0]+$/g, '');
  8414. }
  8415. while($.inArray(t, dpc) !== -1) {
  8416. n = cb.call(this, tmp, (++i)).toString();
  8417. t = n;
  8418. if (!s) {
  8419. t = t.toLowerCase();
  8420. }
  8421. if (w) {
  8422. t = t.replace(/^[\s\uFEFF\xA0]+|[\s\uFEFF\xA0]+$/g, '');
  8423. }
  8424. }
  8425. node.text = n;
  8426. }
  8427. return parent.create_node.call(this, par, node, pos, callback, is_loaded);
  8428. };
  8429. };
  8430. // include the unique plugin by default
  8431. // $.jstree.defaults.plugins.push("unique");
  8432. /**
  8433. * ### Wholerow plugin
  8434. *
  8435. * Makes each node appear block level. Making selection easier. May cause slow down for large trees in old browsers.
  8436. */
  8437. var div = document.createElement('DIV');
  8438. div.setAttribute('unselectable','on');
  8439. div.setAttribute('role','presentation');
  8440. div.className = 'jstree-wholerow';
  8441. div.innerHTML = '&#160;';
  8442. $.jstree.plugins.wholerow = function (options, parent) {
  8443. this.bind = function () {
  8444. parent.bind.call(this);
  8445. this.element
  8446. .on('ready.jstree set_state.jstree', function () {
  8447. this.hide_dots();
  8448. }.bind(this))
  8449. .on("init.jstree loading.jstree ready.jstree", function () {
  8450. //div.style.height = this._data.core.li_height + 'px';
  8451. this.get_container_ul().addClass('jstree-wholerow-ul');
  8452. }.bind(this))
  8453. .on("deselect_all.jstree", function (e, data) {
  8454. this.element.find('.jstree-wholerow-clicked').removeClass('jstree-wholerow-clicked');
  8455. }.bind(this))
  8456. .on("changed.jstree", function (e, data) {
  8457. this.element.find('.jstree-wholerow-clicked').removeClass('jstree-wholerow-clicked');
  8458. var tmp = false, i, j;
  8459. for(i = 0, j = data.selected.length; i < j; i++) {
  8460. tmp = this.get_node(data.selected[i], true);
  8461. if(tmp && tmp.length) {
  8462. tmp.children('.jstree-wholerow').addClass('jstree-wholerow-clicked');
  8463. }
  8464. }
  8465. }.bind(this))
  8466. .on("open_node.jstree", function (e, data) {
  8467. this.get_node(data.node, true).find('.jstree-clicked').parent().children('.jstree-wholerow').addClass('jstree-wholerow-clicked');
  8468. }.bind(this))
  8469. .on("hover_node.jstree dehover_node.jstree", function (e, data) {
  8470. if(e.type === "hover_node" && this.is_disabled(data.node)) { return; }
  8471. this.get_node(data.node, true).children('.jstree-wholerow')[e.type === "hover_node"?"addClass":"removeClass"]('jstree-wholerow-hovered');
  8472. }.bind(this))
  8473. .on("contextmenu.jstree", ".jstree-wholerow", function (e) {
  8474. if (this._data.contextmenu) {
  8475. e.preventDefault();
  8476. var tmp = $.Event('contextmenu', { metaKey : e.metaKey, ctrlKey : e.ctrlKey, altKey : e.altKey, shiftKey : e.shiftKey, pageX : e.pageX, pageY : e.pageY });
  8477. $(e.currentTarget).closest(".jstree-node").children(".jstree-anchor").first().trigger(tmp);
  8478. }
  8479. }.bind(this))
  8480. /*!
  8481. .on("mousedown.jstree touchstart.jstree", ".jstree-wholerow", function (e) {
  8482. if(e.target === e.currentTarget) {
  8483. var a = $(e.currentTarget).closest(".jstree-node").children(".jstree-anchor");
  8484. e.target = a[0];
  8485. a.trigger(e);
  8486. }
  8487. })
  8488. */
  8489. .on("click.jstree", ".jstree-wholerow", function (e) {
  8490. e.stopImmediatePropagation();
  8491. var tmp = $.Event('click', { metaKey : e.metaKey, ctrlKey : e.ctrlKey, altKey : e.altKey, shiftKey : e.shiftKey });
  8492. $(e.currentTarget).closest(".jstree-node").children(".jstree-anchor").first().trigger(tmp).trigger('focus');
  8493. })
  8494. .on("dblclick.jstree", ".jstree-wholerow", function (e) {
  8495. e.stopImmediatePropagation();
  8496. var tmp = $.Event('dblclick', { metaKey : e.metaKey, ctrlKey : e.ctrlKey, altKey : e.altKey, shiftKey : e.shiftKey });
  8497. $(e.currentTarget).closest(".jstree-node").children(".jstree-anchor").first().trigger(tmp).trigger('focus');
  8498. })
  8499. .on("click.jstree", ".jstree-leaf > .jstree-ocl", function (e) {
  8500. e.stopImmediatePropagation();
  8501. var tmp = $.Event('click', { metaKey : e.metaKey, ctrlKey : e.ctrlKey, altKey : e.altKey, shiftKey : e.shiftKey });
  8502. $(e.currentTarget).closest(".jstree-node").children(".jstree-anchor").first().trigger(tmp).trigger('focus');
  8503. }.bind(this))
  8504. .on("mouseover.jstree", ".jstree-wholerow, .jstree-icon", function (e) {
  8505. e.stopImmediatePropagation();
  8506. if(!this.is_disabled(e.currentTarget)) {
  8507. this.hover_node(e.currentTarget);
  8508. }
  8509. return false;
  8510. }.bind(this))
  8511. .on("mouseleave.jstree", ".jstree-node", function (e) {
  8512. this.dehover_node(e.currentTarget);
  8513. }.bind(this));
  8514. };
  8515. this.teardown = function () {
  8516. if(this.settings.wholerow) {
  8517. this.element.find(".jstree-wholerow").remove();
  8518. }
  8519. parent.teardown.call(this);
  8520. };
  8521. this.redraw_node = function(obj, deep, callback, force_render) {
  8522. obj = parent.redraw_node.apply(this, arguments);
  8523. if(obj) {
  8524. var tmp = div.cloneNode(true);
  8525. //tmp.style.height = this._data.core.li_height + 'px';
  8526. if($.inArray(obj.id, this._data.core.selected) !== -1) { tmp.className += ' jstree-wholerow-clicked'; }
  8527. if(this._data.core.focused && this._data.core.focused === obj.id) { tmp.className += ' jstree-wholerow-hovered'; }
  8528. obj.insertBefore(tmp, obj.childNodes[0]);
  8529. }
  8530. return obj;
  8531. };
  8532. };
  8533. // include the wholerow plugin by default
  8534. // $.jstree.defaults.plugins.push("wholerow");
  8535. if(window.customElements && Object && Object.create) {
  8536. var proto = Object.create(HTMLElement.prototype);
  8537. proto.createdCallback = function () {
  8538. var c = { core : {}, plugins : [] }, i;
  8539. for(i in $.jstree.plugins) {
  8540. if($.jstree.plugins.hasOwnProperty(i) && this.attributes[i]) {
  8541. c.plugins.push(i);
  8542. if(this.getAttribute(i) && JSON.parse(this.getAttribute(i))) {
  8543. c[i] = JSON.parse(this.getAttribute(i));
  8544. }
  8545. }
  8546. }
  8547. for(i in $.jstree.defaults.core) {
  8548. if($.jstree.defaults.core.hasOwnProperty(i) && this.attributes[i]) {
  8549. c.core[i] = JSON.parse(this.getAttribute(i)) || this.getAttribute(i);
  8550. }
  8551. }
  8552. $(this).jstree(c);
  8553. };
  8554. // proto.attributeChangedCallback = function (name, previous, value) { };
  8555. try {
  8556. window.customElements.define("vakata-jstree", function() {}, { prototype: proto });
  8557. } catch (ignore) { }
  8558. }
  8559. }));