jstree.js 237 KB

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