jstree.js 271 KB

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