jstree.js 301 KB

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