jstree.js 303 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563856485658566856785688569857085718572857385748575857685778578857985808581858285838584858585868587858885898590859185928593859485958596859785988599860086018602860386048605860686078608860986108611861286138614861586168617861886198620862186228623862486258626862786288629863086318632863386348635863686378638863986408641864286438644864586468647864886498650865186528653865486558656865786588659866086618662866386648665866686678668866986708671867286738674867586768677867886798680868186828683868486858686868786888689869086918692869386948695869686978698869987008701870287038704870587068707870887098710871187128713871487158716871787188719872087218722872387248725872687278728872987308731873287338734873587368737873887398740874187428743
  1. /*globals jQuery, define, module, exports, require, window, document, postMessage */
  2. (function (factory) {
  3. "use strict";
  4. if (typeof define === 'function' && define.amd) {
  5. define(['jquery'], factory);
  6. }
  7. else if(typeof module !== 'undefined' && module.exports) {
  8. module.exports = factory(require('jquery'));
  9. }
  10. else {
  11. factory(jQuery);
  12. }
  13. }(function ($, undefined) {
  14. "use strict";
  15. /*!
  16. * jsTree 3.3.16
  17. * http://jstree.com/
  18. *
  19. * Copyright (c) 2014 Ivan Bozhanov (http://vakata.com)
  20. *
  21. * Licensed same as jquery - under the terms of the MIT License
  22. * http://www.opensource.org/licenses/mit-license.php
  23. */
  24. /*!
  25. * if using jslint please allow for the jQuery global and use following options:
  26. * jslint: loopfunc: true, browser: true, ass: true, bitwise: true, continue: true, nomen: true, plusplus: true, regexp: true, unparam: true, todo: true, white: true
  27. */
  28. /*jshint -W083 */
  29. // prevent another load? maybe there is a better way?
  30. if($.jstree) {
  31. return;
  32. }
  33. /**
  34. * ### jsTree core functionality
  35. */
  36. // internal variables
  37. var instance_counter = 0,
  38. ccp_node = false,
  39. ccp_mode = false,
  40. ccp_inst = false,
  41. themes_loaded = [],
  42. src = $('script:last').attr('src'),
  43. document = window.document; // local variable is always faster to access then a global
  44. var setImmediate = window.setImmediate;
  45. var Promise = window.Promise;
  46. if (!setImmediate && Promise) {
  47. // Good enough approximation of setImmediate
  48. setImmediate = function (cb, arg) {
  49. Promise.resolve(arg).then(cb);
  50. };
  51. }
  52. /**
  53. * holds all jstree related functions and variables, including the actual class and methods to create, access and manipulate instances.
  54. * @name $.jstree
  55. */
  56. $.jstree = {
  57. /**
  58. * specifies the jstree version in use
  59. * @name $.jstree.version
  60. */
  61. version : '3.3.16',
  62. /**
  63. * holds all the default options used when creating new instances
  64. * @name $.jstree.defaults
  65. */
  66. defaults : {
  67. /**
  68. * configure which plugins will be active on an instance. Should be an array of strings, where each element is a plugin name. The default is `[]`
  69. * @name $.jstree.defaults.plugins
  70. */
  71. plugins : []
  72. },
  73. /**
  74. * stores all loaded jstree plugins (used internally)
  75. * @name $.jstree.plugins
  76. */
  77. plugins : {},
  78. path : src && src.indexOf('/') !== -1 ? src.replace(/\/[^\/]+$/,'') : '',
  79. idregex : /[\\:&!^|()\[\]<>@*'+~#";.,=\- \/${}%?`]/g,
  80. root : '#'
  81. };
  82. /**
  83. * creates a jstree instance
  84. * @name $.jstree.create(el [, options])
  85. * @param {DOMElement|jQuery|String} el the element to create the instance on, can be jQuery extended or a selector
  86. * @param {Object} options options for this instance (extends `$.jstree.defaults`)
  87. * @return {jsTree} the new instance
  88. */
  89. $.jstree.create = function (el, options) {
  90. var tmp = new $.jstree.core(++instance_counter),
  91. opt = options;
  92. options = $.extend(true, {}, $.jstree.defaults, options);
  93. if(opt && opt.plugins) {
  94. options.plugins = opt.plugins;
  95. }
  96. $.each(options.plugins, function (i, k) {
  97. if(i !== 'core') {
  98. tmp = tmp.plugin(k, options[k]);
  99. }
  100. });
  101. $(el).data('jstree', tmp);
  102. tmp.init(el, options);
  103. return tmp;
  104. };
  105. /**
  106. * remove all traces of jstree from the DOM and destroy all instances
  107. * @name $.jstree.destroy()
  108. */
  109. $.jstree.destroy = function () {
  110. $('.jstree:jstree').jstree('destroy');
  111. $(document).off('.jstree');
  112. };
  113. /**
  114. * the jstree class constructor, used only internally
  115. * @private
  116. * @name $.jstree.core(id)
  117. * @param {Number} id this instance's index
  118. */
  119. $.jstree.core = function (id) {
  120. this._id = id;
  121. this._cnt = 0;
  122. this._wrk = null;
  123. this._data = {
  124. core : {
  125. themes : {
  126. name : false,
  127. dots : false,
  128. icons : false,
  129. ellipsis : false
  130. },
  131. selected : [],
  132. last_error : {},
  133. working : false,
  134. worker_queue : [],
  135. focused : null
  136. }
  137. };
  138. };
  139. /**
  140. * get a reference to an existing instance
  141. *
  142. * __Examples__
  143. *
  144. * // provided a container with an ID of "tree", and a nested node with an ID of "branch"
  145. * // all of there will return the same instance
  146. * $.jstree.reference('tree');
  147. * $.jstree.reference('#tree');
  148. * $.jstree.reference($('#tree'));
  149. * $.jstree.reference(document.getElementByID('tree'));
  150. * $.jstree.reference('branch');
  151. * $.jstree.reference('#branch');
  152. * $.jstree.reference($('#branch'));
  153. * $.jstree.reference(document.getElementByID('branch'));
  154. *
  155. * @name $.jstree.reference(needle)
  156. * @param {DOMElement|jQuery|String} needle
  157. * @return {jsTree|null} the instance or `null` if not found
  158. */
  159. $.jstree.reference = function (needle) {
  160. var tmp = null,
  161. obj = null;
  162. if(needle && needle.id && (!needle.tagName || !needle.nodeType)) { needle = needle.id; }
  163. if(!obj || !obj.length) {
  164. try { obj = $(needle); } catch (ignore) { }
  165. }
  166. if(!obj || !obj.length) {
  167. try { obj = $('#' + needle.replace($.jstree.idregex,'\\$&')); } catch (ignore) { }
  168. }
  169. if(obj && obj.length && (obj = obj.closest('.jstree')).length && (obj = obj.data('jstree'))) {
  170. tmp = obj;
  171. }
  172. else {
  173. $('.jstree').each(function () {
  174. var inst = $(this).data('jstree');
  175. if(inst && inst._model.data[needle]) {
  176. tmp = inst;
  177. return false;
  178. }
  179. });
  180. }
  181. return tmp;
  182. };
  183. /**
  184. * Create an instance, get an instance or invoke a command on a instance.
  185. *
  186. * If there is no instance associated with the current node a new one is created and `arg` is used to extend `$.jstree.defaults` for this new instance. There would be no return value (chaining is not broken).
  187. *
  188. * If there is an existing instance and `arg` is a string the command specified by `arg` is executed on the instance, with any additional arguments passed to the function. If the function returns a value it will be returned (chaining could break depending on function).
  189. *
  190. * If there is an existing instance and `arg` is not a string the instance itself is returned (similar to `$.jstree.reference`).
  191. *
  192. * In any other case - nothing is returned and chaining is not broken.
  193. *
  194. * __Examples__
  195. *
  196. * $('#tree1').jstree(); // creates an instance
  197. * $('#tree2').jstree({ plugins : [] }); // create an instance with some options
  198. * $('#tree1').jstree('open_node', '#branch_1'); // call a method on an existing instance, passing additional arguments
  199. * $('#tree2').jstree(); // get an existing instance (or create an instance)
  200. * $('#tree2').jstree(true); // get an existing instance (will not create new instance)
  201. * $('#branch_1').jstree().select_node('#branch_1'); // get an instance (using a nested element and call a method)
  202. *
  203. * @name $().jstree([arg])
  204. * @param {String|Object} arg
  205. * @return {Mixed}
  206. */
  207. $.fn.jstree = function (arg) {
  208. // check for string argument
  209. var is_method = (typeof arg === 'string'),
  210. args = Array.prototype.slice.call(arguments, 1),
  211. result = null;
  212. if(arg === true && !this.length) { return false; }
  213. this.each(function () {
  214. // get the instance (if there is one) and method (if it exists)
  215. var instance = $.jstree.reference(this),
  216. method = is_method && instance ? instance[arg] : null;
  217. // if calling a method, and method is available - execute on the instance
  218. result = is_method && method ?
  219. method.apply(instance, args) :
  220. null;
  221. // if there is no instance and no method is being called - create one
  222. if(!instance && !is_method && (arg === undefined || $.isPlainObject(arg))) {
  223. $.jstree.create(this, arg);
  224. }
  225. // if there is an instance and no method is called - return the instance
  226. if( (instance && !is_method) || arg === true ) {
  227. result = instance || false;
  228. }
  229. // if there was a method call which returned a result - break and return the value
  230. if(result !== null && result !== undefined) {
  231. return false;
  232. }
  233. });
  234. // if there was a method call with a valid return value - return that, otherwise continue the chain
  235. return result !== null && result !== undefined ?
  236. result : this;
  237. };
  238. /**
  239. * used to find elements containing an instance
  240. *
  241. * __Examples__
  242. *
  243. * $('div:jstree').each(function () {
  244. * $(this).jstree('destroy');
  245. * });
  246. *
  247. * @name $(':jstree')
  248. * @return {jQuery}
  249. */
  250. $.expr.pseudos.jstree = $.expr.createPseudo(function(search) {
  251. return function(a) {
  252. return $(a).hasClass('jstree') &&
  253. $(a).data('jstree') !== undefined;
  254. };
  255. });
  256. /**
  257. * stores all defaults for the core
  258. * @name $.jstree.defaults.core
  259. */
  260. $.jstree.defaults.core = {
  261. /**
  262. * data configuration
  263. *
  264. * If left as `false` the HTML inside the jstree container element is used to populate the tree (that should be an unordered list with list items).
  265. *
  266. * You can also pass in a HTML string or a JSON array here.
  267. *
  268. * It is possible to pass in a standard jQuery-like AJAX config and jstree will automatically determine if the response is JSON or HTML and use that to populate the tree.
  269. * In addition to the standard jQuery ajax options here you can supply functions for `data` and `url`, the functions will be run in the current instance's scope and a param will be passed indicating which node is being loaded, the return value of those functions will be used.
  270. *
  271. * The last option is to specify a function, that function will receive the node being loaded as argument and a second param which is a function which should be called with the result.
  272. *
  273. * __Examples__
  274. *
  275. * // AJAX
  276. * $('#tree').jstree({
  277. * 'core' : {
  278. * 'data' : {
  279. * 'url' : '/get/children/',
  280. * 'data' : function (node) {
  281. * return { 'id' : node.id };
  282. * }
  283. * }
  284. * });
  285. *
  286. * // direct data
  287. * $('#tree').jstree({
  288. * 'core' : {
  289. * 'data' : [
  290. * 'Simple root node',
  291. * {
  292. * 'id' : 'node_2',
  293. * 'text' : 'Root node with options',
  294. * 'state' : { 'opened' : true, 'selected' : true },
  295. * 'children' : [ { 'text' : 'Child 1' }, 'Child 2']
  296. * }
  297. * ]
  298. * }
  299. * });
  300. *
  301. * // function
  302. * $('#tree').jstree({
  303. * 'core' : {
  304. * 'data' : function (obj, callback) {
  305. * callback.call(this, ['Root 1', 'Root 2']);
  306. * }
  307. * });
  308. *
  309. * @name $.jstree.defaults.core.data
  310. */
  311. data : false,
  312. /**
  313. * configure the various strings used throughout the tree
  314. *
  315. * You can use an object where the key is the string you need to replace and the value is your replacement.
  316. * Another option is to specify a function which will be called with an argument of the needed string and should return the replacement.
  317. * If left as `false` no replacement is made.
  318. *
  319. * __Examples__
  320. *
  321. * $('#tree').jstree({
  322. * 'core' : {
  323. * 'strings' : {
  324. * 'Loading ...' : 'Please wait ...'
  325. * }
  326. * }
  327. * });
  328. *
  329. * @name $.jstree.defaults.core.strings
  330. */
  331. strings : false,
  332. /**
  333. * determines what happens when a user tries to modify the structure of the tree
  334. * If left as `false` all operations like create, rename, delete, move or copy are prevented.
  335. * You can set this to `true` to allow all interactions or use a function to have better control.
  336. *
  337. * __Examples__
  338. *
  339. * $('#tree').jstree({
  340. * 'core' : {
  341. * 'check_callback' : function (operation, node, node_parent, node_position, more) {
  342. * // operation can be 'create_node', 'rename_node', 'delete_node', 'move_node', 'copy_node' or 'edit'
  343. * // in case of 'rename_node' node_position is filled with the new node name
  344. * return operation === 'rename_node' ? true : false;
  345. * }
  346. * }
  347. * });
  348. *
  349. * @name $.jstree.defaults.core.check_callback
  350. */
  351. check_callback : false,
  352. /**
  353. * a callback called with a single object parameter in the instance's scope when something goes wrong (operation prevented, ajax failed, etc)
  354. * @name $.jstree.defaults.core.error
  355. */
  356. error : $.noop,
  357. /**
  358. * the open / close animation duration in milliseconds - set this to `false` to disable the animation (default is `200`)
  359. * @name $.jstree.defaults.core.animation
  360. */
  361. animation : 200,
  362. /**
  363. * a boolean indicating if multiple nodes can be selected
  364. * @name $.jstree.defaults.core.multiple
  365. */
  366. multiple : true,
  367. /**
  368. * theme configuration object
  369. * @name $.jstree.defaults.core.themes
  370. */
  371. themes : {
  372. /**
  373. * the name of the theme to use (if left as `false` the default theme is used)
  374. * @name $.jstree.defaults.core.themes.name
  375. */
  376. name : false,
  377. /**
  378. * the URL of the theme's CSS file, leave this as `false` if you have manually included the theme CSS (recommended). You can set this to `true` too which will try to autoload the theme.
  379. * @name $.jstree.defaults.core.themes.url
  380. */
  381. url : false,
  382. /**
  383. * the location of all jstree themes - only used if `url` is set to `true`
  384. * @name $.jstree.defaults.core.themes.dir
  385. */
  386. dir : false,
  387. /**
  388. * a boolean indicating if connecting dots are shown
  389. * @name $.jstree.defaults.core.themes.dots
  390. */
  391. dots : true,
  392. /**
  393. * a boolean indicating if node icons are shown
  394. * @name $.jstree.defaults.core.themes.icons
  395. */
  396. icons : true,
  397. /**
  398. * a boolean indicating if node ellipsis should be shown - this only works with a fixed with on the container
  399. * @name $.jstree.defaults.core.themes.ellipsis
  400. */
  401. ellipsis : false,
  402. /**
  403. * a boolean indicating if the tree background is striped
  404. * @name $.jstree.defaults.core.themes.stripes
  405. */
  406. stripes : false,
  407. /**
  408. * a string (or boolean `false`) specifying the theme variant to use (if the theme supports variants)
  409. * @name $.jstree.defaults.core.themes.variant
  410. */
  411. variant : false,
  412. /**
  413. * a boolean specifying if a reponsive version of the theme should kick in on smaller screens (if the theme supports it). Defaults to `false`.
  414. * @name $.jstree.defaults.core.themes.responsive
  415. */
  416. responsive : false
  417. },
  418. /**
  419. * if left as `true` all parents of all selected nodes will be opened once the tree loads (so that all selected nodes are visible to the user)
  420. * @name $.jstree.defaults.core.expand_selected_onload
  421. */
  422. expand_selected_onload : true,
  423. /**
  424. * if left as `true` web workers will be used to parse incoming JSON data where possible, so that the UI will not be blocked by large requests. Workers are however about 30% slower. Defaults to `true`
  425. * @name $.jstree.defaults.core.worker
  426. */
  427. worker : true,
  428. /**
  429. * Force node text to plain text (and escape HTML). Defaults to `false`
  430. * @name $.jstree.defaults.core.force_text
  431. */
  432. force_text : false,
  433. /**
  434. * Should the node be toggled if the text is double clicked. Defaults to `true`
  435. * @name $.jstree.defaults.core.dblclick_toggle
  436. */
  437. dblclick_toggle : true,
  438. /**
  439. * Should the loaded nodes be part of the state. Defaults to `false`
  440. * @name $.jstree.defaults.core.loaded_state
  441. */
  442. loaded_state : false,
  443. /**
  444. * Should the last active node be focused when the tree container is blurred and the focused again. This helps working with screen readers. Defaults to `true`
  445. * @name $.jstree.defaults.core.restore_focus
  446. */
  447. restore_focus : true,
  448. /**
  449. * Force to compute and set "aria-setsize" and "aria-posinset" explicitly for each treeitem.
  450. * Some browsers may compute incorrect elements position and produce wrong announcements for screen readers. Defaults to `false`
  451. * @name $.jstree.defaults.core.compute_elements_positions
  452. */
  453. compute_elements_positions : false,
  454. /**
  455. * Default keyboard shortcuts (an object where each key is the button name or combo - like 'enter', 'ctrl-space', 'p', etc and the value is the function to execute in the instance's scope)
  456. * @name $.jstree.defaults.core.keyboard
  457. */
  458. keyboard : {
  459. 'ctrl-space': function (e) {
  460. // aria defines space only with Ctrl
  461. e.type = "click";
  462. $(e.currentTarget).trigger(e);
  463. },
  464. 'enter': function (e) {
  465. // enter
  466. e.type = "click";
  467. $(e.currentTarget).trigger(e);
  468. },
  469. 'left': function (e) {
  470. // left
  471. e.preventDefault();
  472. if(this.is_open(e.currentTarget)) {
  473. this.close_node(e.currentTarget);
  474. }
  475. else {
  476. var o = this.get_parent(e.currentTarget);
  477. if(o && o.id !== $.jstree.root) { this.get_node(o, true).children('.jstree-anchor').trigger('focus'); }
  478. }
  479. },
  480. 'up': function (e) {
  481. // up
  482. e.preventDefault();
  483. var o = this.get_prev_dom(e.currentTarget);
  484. if(o && o.length) { o.children('.jstree-anchor').trigger('focus'); }
  485. },
  486. 'right': function (e) {
  487. // right
  488. e.preventDefault();
  489. if(this.is_closed(e.currentTarget)) {
  490. this.open_node(e.currentTarget, function (o) { this.get_node(o, true).children('.jstree-anchor').trigger('focus'); });
  491. }
  492. else if (this.is_open(e.currentTarget)) {
  493. var o = this.get_node(e.currentTarget, true).children('.jstree-children')[0];
  494. if(o) { $(this._firstChild(o)).children('.jstree-anchor').trigger('focus'); }
  495. }
  496. },
  497. 'down': function (e) {
  498. // down
  499. e.preventDefault();
  500. var o = this.get_next_dom(e.currentTarget);
  501. if(o && o.length) { o.children('.jstree-anchor').trigger('focus'); }
  502. },
  503. '*': function (e) {
  504. // aria defines * on numpad as open_all - not very common
  505. this.open_all();
  506. },
  507. 'home': function (e) {
  508. // home
  509. e.preventDefault();
  510. var o = this._firstChild(this.get_container_ul()[0]);
  511. if(o) { $(o).children('.jstree-anchor').filter(':visible').trigger('focus'); }
  512. },
  513. 'end': function (e) {
  514. // end
  515. e.preventDefault();
  516. this.element.find('.jstree-anchor').filter(':visible').last().trigger('focus');
  517. },
  518. 'f2': function (e) {
  519. // f2 - safe to include - if check_callback is false it will fail
  520. e.preventDefault();
  521. this.edit(e.currentTarget);
  522. }
  523. },
  524. /**
  525. * Should reselecting an already selected node trigger the select and changed callbacks
  526. * @name $.jstree.defaults.core.allow_reselect
  527. */
  528. allow_reselect : false
  529. };
  530. $.jstree.core.prototype = {
  531. /**
  532. * used to decorate an instance with a plugin. Used internally.
  533. * @private
  534. * @name plugin(deco [, opts])
  535. * @param {String} deco the plugin to decorate with
  536. * @param {Object} opts options for the plugin
  537. * @return {jsTree}
  538. */
  539. plugin : function (deco, opts) {
  540. var Child = $.jstree.plugins[deco];
  541. if(Child) {
  542. this._data[deco] = {};
  543. Child.prototype = this;
  544. return new Child(opts, this);
  545. }
  546. return this;
  547. },
  548. /**
  549. * initialize the instance. Used internally.
  550. * @private
  551. * @name init(el, optons)
  552. * @param {DOMElement|jQuery|String} el the element we are transforming
  553. * @param {Object} options options for this instance
  554. * @trigger init.jstree, loading.jstree, loaded.jstree, ready.jstree, changed.jstree
  555. */
  556. init : function (el, options) {
  557. this._model = {
  558. data : {},
  559. changed : [],
  560. force_full_redraw : false,
  561. redraw_timeout : false,
  562. default_state : {
  563. loaded : true,
  564. opened : false,
  565. selected : false,
  566. disabled : false
  567. }
  568. };
  569. this._model.data[$.jstree.root] = {
  570. id : $.jstree.root,
  571. parent : null,
  572. parents : [],
  573. children : [],
  574. children_d : [],
  575. state : { loaded : false }
  576. };
  577. this.element = $(el).addClass('jstree jstree-' + this._id);
  578. this.settings = options;
  579. this._data.core.ready = false;
  580. this._data.core.loaded = false;
  581. this._data.core.rtl = (this.element.css("direction") === "rtl");
  582. this.element[this._data.core.rtl ? 'addClass' : 'removeClass']("jstree-rtl");
  583. this.element.attr('role','tree');
  584. if(this.settings.core.multiple) {
  585. this.element.attr('aria-multiselectable', true);
  586. }
  587. if(!this.element.attr('tabindex')) {
  588. this.element.attr('tabindex','0');
  589. }
  590. this.bind();
  591. /**
  592. * triggered after all events are bound
  593. * @event
  594. * @name init.jstree
  595. */
  596. this.trigger("init");
  597. this._data.core.original_container_html = this.element.find(" > ul > li").clone(true);
  598. this._data.core.original_container_html
  599. .find("li").addBack()
  600. .contents().filter(function() {
  601. return this.nodeType === 3 && (!this.nodeValue || /^\s+$/.test(this.nodeValue));
  602. })
  603. .remove();
  604. this.element.html("<"+"ul class='jstree-container-ul jstree-children' role='group'><"+"li id='j"+this._id+"_loading' class='jstree-initial-node jstree-loading jstree-leaf jstree-last' role='none'><i class='jstree-icon jstree-ocl'></i><"+"a class='jstree-anchor' role='treeitem' href='#'><i class='jstree-icon jstree-themeicon-hidden'></i>" + this.get_string("Loading ...") + "</a></li></ul>");
  605. this.element.attr('aria-activedescendant','j' + this._id + '_loading');
  606. this._data.core.li_height = this.get_container_ul().children("li").first().outerHeight() || 24;
  607. this._data.core.node = this._create_prototype_node();
  608. /**
  609. * triggered after the loading text is shown and before loading starts
  610. * @event
  611. * @name loading.jstree
  612. */
  613. this.trigger("loading");
  614. this.load_node($.jstree.root);
  615. },
  616. /**
  617. * destroy an instance
  618. * @name destroy()
  619. * @param {Boolean} keep_html if not set to `true` the container will be emptied, otherwise the current DOM elements will be kept intact
  620. */
  621. destroy : function (keep_html) {
  622. /**
  623. * triggered before the tree is destroyed
  624. * @event
  625. * @name destroy.jstree
  626. */
  627. this.trigger("destroy");
  628. if(this._wrk) {
  629. try {
  630. window.URL.revokeObjectURL(this._wrk);
  631. this._wrk = null;
  632. }
  633. catch (ignore) { }
  634. }
  635. if(!keep_html) { this.element.empty(); }
  636. this.teardown();
  637. },
  638. /**
  639. * Create a prototype node
  640. * @name _create_prototype_node()
  641. * @return {DOMElement}
  642. */
  643. _create_prototype_node : function () {
  644. var _node = document.createElement('LI'), _temp1, _temp2;
  645. _node.setAttribute('role', 'none');
  646. _temp1 = document.createElement('I');
  647. _temp1.className = 'jstree-icon jstree-ocl';
  648. _temp1.setAttribute('role', 'presentation');
  649. _node.appendChild(_temp1);
  650. _temp1 = document.createElement('A');
  651. _temp1.className = 'jstree-anchor';
  652. _temp1.setAttribute('href','#');
  653. _temp1.setAttribute('tabindex','-1');
  654. _temp1.setAttribute('role', 'treeitem');
  655. _temp2 = document.createElement('I');
  656. _temp2.className = 'jstree-icon jstree-themeicon';
  657. _temp2.setAttribute('role', 'presentation');
  658. _temp1.appendChild(_temp2);
  659. _node.appendChild(_temp1);
  660. _temp1 = _temp2 = null;
  661. return _node;
  662. },
  663. _kbevent_to_func : function (e) {
  664. var keys = {
  665. 8: "Backspace", 9: "Tab", 13: "Enter", 19: "Pause", 27: "Esc",
  666. 32: "Space", 33: "PageUp", 34: "PageDown", 35: "End", 36: "Home",
  667. 37: "Left", 38: "Up", 39: "Right", 40: "Down", 44: "Print", 45: "Insert",
  668. 46: "Delete", 96: "Numpad0", 97: "Numpad1", 98: "Numpad2", 99 : "Numpad3",
  669. 100: "Numpad4", 101: "Numpad5", 102: "Numpad6", 103: "Numpad7",
  670. 104: "Numpad8", 105: "Numpad9", '-13': "NumpadEnter", 112: "F1",
  671. 113: "F2", 114: "F3", 115: "F4", 116: "F5", 117: "F6", 118: "F7",
  672. 119: "F8", 120: "F9", 121: "F10", 122: "F11", 123: "F12", 144: "Numlock",
  673. 145: "Scrolllock", 16: 'Shift', 17: 'Ctrl', 18: 'Alt',
  674. 48: '0', 49: '1', 50: '2', 51: '3', 52: '4', 53: '5',
  675. 54: '6', 55: '7', 56: '8', 57: '9', 59: ';', 61: '=', 65: 'a',
  676. 66: 'b', 67: 'c', 68: 'd', 69: 'e', 70: 'f', 71: 'g', 72: 'h',
  677. 73: 'i', 74: 'j', 75: 'k', 76: 'l', 77: 'm', 78: 'n', 79: 'o',
  678. 80: 'p', 81: 'q', 82: 'r', 83: 's', 84: 't', 85: 'u', 86: 'v',
  679. 87: 'w', 88: 'x', 89: 'y', 90: 'z', 107: '+', 109: '-', 110: '.',
  680. 186: ';', 187: '=', 188: ',', 189: '-', 190: '.', 191: '/', 192: '`',
  681. 219: '[', 220: '\\',221: ']', 222: "'", 111: '/', 106: '*', 173: '-'
  682. };
  683. var parts = [];
  684. if (e.ctrlKey) { parts.push('ctrl'); }
  685. if (e.altKey) { parts.push('alt'); }
  686. if (e.shiftKey) { parts.push('shift'); }
  687. parts.push(keys[e.which] ? keys[e.which].toLowerCase() : e.which);
  688. parts = parts.sort().join('-').toLowerCase();
  689. if (parts === 'shift-shift' || parts === 'ctrl-ctrl' || parts === 'alt-alt') {
  690. return null;
  691. }
  692. var kb = this.settings.core.keyboard, i, tmp;
  693. for (i in kb) {
  694. if (kb.hasOwnProperty(i)) {
  695. tmp = i;
  696. if (tmp !== '-' && tmp !== '+') {
  697. tmp = tmp.replace('--', '-MINUS').replace('+-', '-MINUS').replace('++', '-PLUS').replace('-+', '-PLUS');
  698. tmp = tmp.split(/-|\+/).sort().join('-').replace('MINUS', '-').replace('PLUS', '+').toLowerCase();
  699. }
  700. if (tmp === parts) {
  701. return kb[i];
  702. }
  703. }
  704. }
  705. return null;
  706. },
  707. /**
  708. * part of the destroying of an instance. Used internally.
  709. * @private
  710. * @name teardown()
  711. */
  712. teardown : function () {
  713. this.unbind();
  714. this.element
  715. .removeClass('jstree')
  716. .removeData('jstree')
  717. .find("[class^='jstree']")
  718. .addBack()
  719. .attr("class", function () { return this.className.replace(/jstree[^ ]*|$/ig,''); });
  720. this.element = null;
  721. },
  722. /**
  723. * bind all events. Used internally.
  724. * @private
  725. * @name bind()
  726. */
  727. bind : function () {
  728. var word = '',
  729. tout = null,
  730. was_click = 0;
  731. this.element
  732. .on("dblclick.jstree", function (e) {
  733. if(e.target.tagName && e.target.tagName.toLowerCase() === "input") { return true; }
  734. if(document.selection && document.selection.empty) {
  735. document.selection.empty();
  736. }
  737. else {
  738. if(window.getSelection) {
  739. var sel = window.getSelection();
  740. try {
  741. sel.removeAllRanges();
  742. sel.collapse();
  743. } catch (ignore) { }
  744. }
  745. }
  746. })
  747. .on("mousedown.jstree", function (e) {
  748. if(e.target === this.element[0]) {
  749. e.preventDefault(); // prevent losing focus when clicking scroll arrows (FF, Chrome)
  750. was_click = +(new Date()); // ie does not allow to prevent losing focus
  751. }
  752. }.bind(this))
  753. .on("mousedown.jstree", ".jstree-ocl", function (e) {
  754. e.preventDefault(); // prevent any node inside from losing focus when clicking the open/close icon
  755. })
  756. .on("click.jstree", ".jstree-ocl", function (e) {
  757. this.toggle_node(e.target);
  758. }.bind(this))
  759. .on("dblclick.jstree", ".jstree-anchor", function (e) {
  760. if(e.target.tagName && e.target.tagName.toLowerCase() === "input") { return true; }
  761. if(this.settings.core.dblclick_toggle) {
  762. this.toggle_node(e.target);
  763. }
  764. }.bind(this))
  765. .on("click.jstree", ".jstree-anchor", function (e) {
  766. e.preventDefault();
  767. if(e.currentTarget !== document.activeElement) { $(e.currentTarget).trigger('focus'); }
  768. this.activate_node(e.currentTarget, e);
  769. }.bind(this))
  770. .on('keydown.jstree', '.jstree-anchor', function (e) {
  771. if(e.target.tagName && e.target.tagName.toLowerCase() === "input") { return true; }
  772. if(this._data.core.rtl) {
  773. if(e.which === 37) { e.which = 39; }
  774. else if(e.which === 39) { e.which = 37; }
  775. }
  776. var f = this._kbevent_to_func(e);
  777. if (f) {
  778. var r = f.call(this, e);
  779. if (r === false || r === true) {
  780. return r;
  781. }
  782. }
  783. }.bind(this))
  784. .on("load_node.jstree", function (e, data) {
  785. if(data.status) {
  786. if(data.node.id === $.jstree.root && !this._data.core.loaded) {
  787. this._data.core.loaded = true;
  788. if(this._firstChild(this.get_container_ul()[0])) {
  789. this.element.attr('aria-activedescendant',this._firstChild(this.get_container_ul()[0]).id);
  790. }
  791. /**
  792. * triggered after the root node is loaded for the first time
  793. * @event
  794. * @name loaded.jstree
  795. */
  796. this.trigger("loaded");
  797. }
  798. if(!this._data.core.ready) {
  799. setTimeout(function() {
  800. if(this.element && !this.get_container_ul().find('.jstree-loading').length) {
  801. this._data.core.ready = true;
  802. if(this._data.core.selected.length) {
  803. if(this.settings.core.expand_selected_onload) {
  804. var tmp = [], i, j;
  805. for(i = 0, j = this._data.core.selected.length; i < j; i++) {
  806. tmp = tmp.concat(this._model.data[this._data.core.selected[i]].parents);
  807. }
  808. tmp = $.vakata.array_unique(tmp);
  809. for(i = 0, j = tmp.length; i < j; i++) {
  810. this.open_node(tmp[i], false, 0);
  811. }
  812. }
  813. this.trigger('changed', { 'action' : 'ready', 'selected' : this._data.core.selected });
  814. }
  815. /**
  816. * triggered after all nodes are finished loading
  817. * @event
  818. * @name ready.jstree
  819. */
  820. this.trigger("ready");
  821. }
  822. }.bind(this), 0);
  823. }
  824. }
  825. }.bind(this))
  826. // quick searching when the tree is focused
  827. .on('keypress.jstree', function (e) {
  828. if(e.target.tagName && e.target.tagName.toLowerCase() === "input") { return true; }
  829. if(tout) { clearTimeout(tout); }
  830. tout = setTimeout(function () {
  831. word = '';
  832. }, 500);
  833. var chr = String.fromCharCode(e.which).toLowerCase(),
  834. col = this.element.find('.jstree-anchor').filter(':visible'),
  835. ind = col.index(document.activeElement) || 0,
  836. end = false;
  837. word += chr;
  838. // match for whole word from current node down (including the current node)
  839. if(word.length > 1) {
  840. col.slice(ind).each(function (i, v) {
  841. if($(v).text().toLowerCase().indexOf(word) === 0) {
  842. $(v).trigger('focus');
  843. end = true;
  844. return false;
  845. }
  846. }.bind(this));
  847. if(end) { return; }
  848. // match for whole word from the beginning of the tree
  849. col.slice(0, ind).each(function (i, v) {
  850. if($(v).text().toLowerCase().indexOf(word) === 0) {
  851. $(v).trigger('focus');
  852. end = true;
  853. return false;
  854. }
  855. }.bind(this));
  856. if(end) { return; }
  857. }
  858. // list nodes that start with that letter (only if word consists of a single char)
  859. if(new RegExp('^' + chr.replace(/[-\/\\^$*+?.()|[\]{}]/g, '\\$&') + '+$').test(word)) {
  860. // search for the next node starting with that letter
  861. col.slice(ind + 1).each(function (i, v) {
  862. if($(v).text().toLowerCase().charAt(0) === chr) {
  863. $(v).trigger('focus');
  864. end = true;
  865. return false;
  866. }
  867. }.bind(this));
  868. if(end) { return; }
  869. // search from the beginning
  870. col.slice(0, ind + 1).each(function (i, v) {
  871. if($(v).text().toLowerCase().charAt(0) === chr) {
  872. $(v).trigger('focus');
  873. end = true;
  874. return false;
  875. }
  876. }.bind(this));
  877. if(end) { return; }
  878. }
  879. }.bind(this))
  880. // THEME RELATED
  881. .on("init.jstree", function () {
  882. var s = this.settings.core.themes;
  883. this._data.core.themes.dots = s.dots;
  884. this._data.core.themes.stripes = s.stripes;
  885. this._data.core.themes.icons = s.icons;
  886. this._data.core.themes.ellipsis = s.ellipsis;
  887. this.set_theme(s.name || "default", s.url);
  888. this.set_theme_variant(s.variant);
  889. }.bind(this))
  890. .on("loading.jstree", function () {
  891. this[ this._data.core.themes.dots ? "show_dots" : "hide_dots" ]();
  892. this[ this._data.core.themes.icons ? "show_icons" : "hide_icons" ]();
  893. this[ this._data.core.themes.stripes ? "show_stripes" : "hide_stripes" ]();
  894. this[ this._data.core.themes.ellipsis ? "show_ellipsis" : "hide_ellipsis" ]();
  895. }.bind(this))
  896. .on('blur.jstree', '.jstree-anchor', function (e) {
  897. this._data.core.focused = null;
  898. $(e.currentTarget).filter('.jstree-hovered').trigger('mouseleave');
  899. this.element.attr('tabindex', '0');
  900. $(e.currentTarget).attr('tabindex', '-1');
  901. }.bind(this))
  902. .on('focus.jstree', '.jstree-anchor', function (e) {
  903. var tmp = this.get_node(e.currentTarget);
  904. if(tmp && (tmp.id || tmp.id === 0)) {
  905. this._data.core.focused = tmp.id;
  906. }
  907. this.element.find('.jstree-hovered').not(e.currentTarget).trigger('mouseleave');
  908. $(e.currentTarget).trigger('mouseenter');
  909. this.element.attr('tabindex', '-1');
  910. $(e.currentTarget).attr('tabindex', '0');
  911. }.bind(this))
  912. .on('focus.jstree', function () {
  913. if(+(new Date()) - was_click > 500 && !this._data.core.focused && this.settings.core.restore_focus) {
  914. was_click = 0;
  915. var act = this.get_node(this.element.attr('aria-activedescendant'), true);
  916. if(act) {
  917. act.find('> .jstree-anchor').trigger('focus');
  918. }
  919. }
  920. }.bind(this))
  921. .on('mouseenter.jstree', '.jstree-anchor', function (e) {
  922. this.hover_node(e.currentTarget);
  923. }.bind(this))
  924. .on('mouseleave.jstree', '.jstree-anchor', function (e) {
  925. this.dehover_node(e.currentTarget);
  926. }.bind(this));
  927. },
  928. /**
  929. * part of the destroying of an instance. Used internally.
  930. * @private
  931. * @name unbind()
  932. */
  933. unbind : function () {
  934. this.element.off('.jstree');
  935. $(document).off('.jstree-' + this._id);
  936. },
  937. /**
  938. * trigger an event. Used internally.
  939. * @private
  940. * @name trigger(ev [, data])
  941. * @param {String} ev the name of the event to trigger
  942. * @param {Object} data additional data to pass with the event
  943. */
  944. trigger : function (ev, data) {
  945. if(!data) {
  946. data = {};
  947. }
  948. data.instance = this;
  949. this.element.triggerHandler(ev.replace('.jstree','') + '.jstree', data);
  950. },
  951. /**
  952. * returns the jQuery extended instance container
  953. * @name get_container()
  954. * @return {jQuery}
  955. */
  956. get_container : function () {
  957. return this.element;
  958. },
  959. /**
  960. * returns the jQuery extended main UL node inside the instance container. Used internally.
  961. * @private
  962. * @name get_container_ul()
  963. * @return {jQuery}
  964. */
  965. get_container_ul : function () {
  966. return this.element.children(".jstree-children").first();
  967. },
  968. /**
  969. * gets string replacements (localization). Used internally.
  970. * @private
  971. * @name get_string(key)
  972. * @param {String} key
  973. * @return {String}
  974. */
  975. get_string : function (key) {
  976. var a = this.settings.core.strings;
  977. if($.vakata.is_function(a)) { return a.call(this, key); }
  978. if(a && a[key]) { return a[key]; }
  979. return key;
  980. },
  981. /**
  982. * gets the first child of a DOM node. Used internally.
  983. * @private
  984. * @name _firstChild(dom)
  985. * @param {DOMElement} dom
  986. * @return {DOMElement}
  987. */
  988. _firstChild : function (dom) {
  989. dom = dom ? dom.firstChild : null;
  990. while(dom !== null && dom.nodeType !== 1) {
  991. dom = dom.nextSibling;
  992. }
  993. return dom;
  994. },
  995. /**
  996. * gets the next sibling of a DOM node. Used internally.
  997. * @private
  998. * @name _nextSibling(dom)
  999. * @param {DOMElement} dom
  1000. * @return {DOMElement}
  1001. */
  1002. _nextSibling : function (dom) {
  1003. dom = dom ? dom.nextSibling : null;
  1004. while(dom !== null && dom.nodeType !== 1) {
  1005. dom = dom.nextSibling;
  1006. }
  1007. return dom;
  1008. },
  1009. /**
  1010. * gets the previous sibling of a DOM node. Used internally.
  1011. * @private
  1012. * @name _previousSibling(dom)
  1013. * @param {DOMElement} dom
  1014. * @return {DOMElement}
  1015. */
  1016. _previousSibling : function (dom) {
  1017. dom = dom ? dom.previousSibling : null;
  1018. while(dom !== null && dom.nodeType !== 1) {
  1019. dom = dom.previousSibling;
  1020. }
  1021. return dom;
  1022. },
  1023. /**
  1024. * 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)
  1025. * @name get_node(obj [, as_dom])
  1026. * @param {mixed} obj
  1027. * @param {Boolean} as_dom
  1028. * @return {Object|jQuery}
  1029. */
  1030. get_node : function (obj, as_dom) {
  1031. if(obj && (obj.id || obj.id === 0)) {
  1032. obj = obj.id;
  1033. }
  1034. if (obj instanceof $ && obj.length && obj[0].id) {
  1035. obj = obj[0].id;
  1036. }
  1037. var dom;
  1038. try {
  1039. if(this._model.data[obj]) {
  1040. obj = this._model.data[obj];
  1041. }
  1042. else if(typeof obj === "string" && this._model.data[obj.replace(/^#/, '')]) {
  1043. obj = this._model.data[obj.replace(/^#/, '')];
  1044. }
  1045. else if(typeof obj === "string" && (dom = $('#' + obj.replace($.jstree.idregex,'\\$&'), this.element)).length && this._model.data[dom.closest('.jstree-node').attr('id')]) {
  1046. obj = this._model.data[dom.closest('.jstree-node').attr('id')];
  1047. }
  1048. else if((dom = this.element.find(obj)).length && this._model.data[dom.closest('.jstree-node').attr('id')]) {
  1049. obj = this._model.data[dom.closest('.jstree-node').attr('id')];
  1050. }
  1051. else if((dom = this.element.find(obj)).length && dom.hasClass('jstree')) {
  1052. obj = this._model.data[$.jstree.root];
  1053. }
  1054. else {
  1055. return false;
  1056. }
  1057. if(as_dom) {
  1058. obj = obj.id === $.jstree.root ? this.element : $('#' + obj.id.replace($.jstree.idregex,'\\$&'), this.element);
  1059. }
  1060. return obj;
  1061. } catch (ex) { return false; }
  1062. },
  1063. /**
  1064. * get the path to a node, either consisting of node texts, or of node IDs, optionally glued together (otherwise an array)
  1065. * @name get_path(obj [, glue, ids])
  1066. * @param {mixed} obj the node
  1067. * @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
  1068. * @param {Boolean} ids if set to true build the path using ID, otherwise node text is used
  1069. * @return {mixed}
  1070. */
  1071. get_path : function (obj, glue, ids) {
  1072. obj = obj.parents ? obj : this.get_node(obj);
  1073. if(!obj || obj.id === $.jstree.root || !obj.parents) {
  1074. return false;
  1075. }
  1076. var i, j, p = [];
  1077. p.push(ids ? obj.id : obj.text);
  1078. for(i = 0, j = obj.parents.length; i < j; i++) {
  1079. p.push(ids ? obj.parents[i] : this.get_text(obj.parents[i]));
  1080. }
  1081. p = p.reverse().slice(1);
  1082. return glue ? p.join(glue) : p;
  1083. },
  1084. /**
  1085. * get the next visible node that is below the `obj` node. If `strict` is set to `true` only sibling nodes are returned.
  1086. * @name get_next_dom(obj [, strict])
  1087. * @param {mixed} obj
  1088. * @param {Boolean} strict
  1089. * @return {jQuery}
  1090. */
  1091. get_next_dom : function (obj, strict) {
  1092. var tmp;
  1093. obj = this.get_node(obj, true);
  1094. if(obj[0] === this.element[0]) {
  1095. tmp = this._firstChild(this.get_container_ul()[0]);
  1096. while (tmp && tmp.offsetHeight === 0) {
  1097. tmp = this._nextSibling(tmp);
  1098. }
  1099. return tmp ? $(tmp) : false;
  1100. }
  1101. if(!obj || !obj.length) {
  1102. return false;
  1103. }
  1104. if(strict) {
  1105. tmp = obj[0];
  1106. do {
  1107. tmp = this._nextSibling(tmp);
  1108. } while (tmp && tmp.offsetHeight === 0);
  1109. return tmp ? $(tmp) : false;
  1110. }
  1111. if(obj.hasClass("jstree-open")) {
  1112. tmp = this._firstChild(obj.children('.jstree-children')[0]);
  1113. while (tmp && tmp.offsetHeight === 0) {
  1114. tmp = this._nextSibling(tmp);
  1115. }
  1116. if(tmp !== null) {
  1117. return $(tmp);
  1118. }
  1119. }
  1120. tmp = obj[0];
  1121. do {
  1122. tmp = this._nextSibling(tmp);
  1123. } while (tmp && tmp.offsetHeight === 0);
  1124. if(tmp !== null) {
  1125. return $(tmp);
  1126. }
  1127. return obj.parentsUntil(".jstree",".jstree-node").nextAll(".jstree-node:visible").first();
  1128. },
  1129. /**
  1130. * get the previous visible node that is above the `obj` node. If `strict` is set to `true` only sibling nodes are returned.
  1131. * @name get_prev_dom(obj [, strict])
  1132. * @param {mixed} obj
  1133. * @param {Boolean} strict
  1134. * @return {jQuery}
  1135. */
  1136. get_prev_dom : function (obj, strict) {
  1137. var tmp;
  1138. obj = this.get_node(obj, true);
  1139. if(obj[0] === this.element[0]) {
  1140. tmp = this.get_container_ul()[0].lastChild;
  1141. while (tmp && tmp.offsetHeight === 0) {
  1142. tmp = this._previousSibling(tmp);
  1143. }
  1144. return tmp ? $(tmp) : false;
  1145. }
  1146. if(!obj || !obj.length) {
  1147. return false;
  1148. }
  1149. if(strict) {
  1150. tmp = obj[0];
  1151. do {
  1152. tmp = this._previousSibling(tmp);
  1153. } while (tmp && tmp.offsetHeight === 0);
  1154. return tmp ? $(tmp) : false;
  1155. }
  1156. tmp = obj[0];
  1157. do {
  1158. tmp = this._previousSibling(tmp);
  1159. } while (tmp && tmp.offsetHeight === 0);
  1160. if(tmp !== null) {
  1161. obj = $(tmp);
  1162. while(obj.hasClass("jstree-open")) {
  1163. obj = obj.children(".jstree-children").first().children(".jstree-node:visible:last");
  1164. }
  1165. return obj;
  1166. }
  1167. tmp = obj[0].parentNode.parentNode;
  1168. return tmp && tmp.className && tmp.className.indexOf('jstree-node') !== -1 ? $(tmp) : false;
  1169. },
  1170. /**
  1171. * get the parent ID of a node
  1172. * @name get_parent(obj)
  1173. * @param {mixed} obj
  1174. * @return {String}
  1175. */
  1176. get_parent : function (obj) {
  1177. obj = this.get_node(obj);
  1178. if(!obj || obj.id === $.jstree.root) {
  1179. return false;
  1180. }
  1181. return obj.parent;
  1182. },
  1183. /**
  1184. * get a jQuery collection of all the children of a node (node must be rendered), returns false on error
  1185. * @name get_children_dom(obj)
  1186. * @param {mixed} obj
  1187. * @return {jQuery}
  1188. */
  1189. get_children_dom : function (obj) {
  1190. obj = this.get_node(obj, true);
  1191. if(obj[0] === this.element[0]) {
  1192. return this.get_container_ul().children(".jstree-node");
  1193. }
  1194. if(!obj || !obj.length) {
  1195. return false;
  1196. }
  1197. return obj.children(".jstree-children").children(".jstree-node");
  1198. },
  1199. /**
  1200. * checks if a node has children
  1201. * @name is_parent(obj)
  1202. * @param {mixed} obj
  1203. * @return {Boolean}
  1204. */
  1205. is_parent : function (obj) {
  1206. obj = this.get_node(obj);
  1207. return obj && (obj.state.loaded === false || obj.children.length > 0);
  1208. },
  1209. /**
  1210. * checks if a node is loaded (its children are available)
  1211. * @name is_loaded(obj)
  1212. * @param {mixed} obj
  1213. * @return {Boolean}
  1214. */
  1215. is_loaded : function (obj) {
  1216. obj = this.get_node(obj);
  1217. return obj && obj.state.loaded;
  1218. },
  1219. /**
  1220. * check if a node is currently loading (fetching children)
  1221. * @name is_loading(obj)
  1222. * @param {mixed} obj
  1223. * @return {Boolean}
  1224. */
  1225. is_loading : function (obj) {
  1226. obj = this.get_node(obj);
  1227. return obj && obj.state && obj.state.loading;
  1228. },
  1229. /**
  1230. * check if a node is opened
  1231. * @name is_open(obj)
  1232. * @param {mixed} obj
  1233. * @return {Boolean}
  1234. */
  1235. is_open : function (obj) {
  1236. obj = this.get_node(obj);
  1237. return obj && obj.state.opened;
  1238. },
  1239. /**
  1240. * check if a node is in a closed state
  1241. * @name is_closed(obj)
  1242. * @param {mixed} obj
  1243. * @return {Boolean}
  1244. */
  1245. is_closed : function (obj) {
  1246. obj = this.get_node(obj);
  1247. return obj && this.is_parent(obj) && !obj.state.opened;
  1248. },
  1249. /**
  1250. * check if a node has no children
  1251. * @name is_leaf(obj)
  1252. * @param {mixed} obj
  1253. * @return {Boolean}
  1254. */
  1255. is_leaf : function (obj) {
  1256. return !this.is_parent(obj);
  1257. },
  1258. /**
  1259. * loads a node (fetches its children using the `core.data` setting). Multiple nodes can be passed to by using an array.
  1260. * @name load_node(obj [, callback])
  1261. * @param {mixed} obj
  1262. * @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
  1263. * @return {Boolean}
  1264. * @trigger load_node.jstree
  1265. */
  1266. load_node : function (obj, callback) {
  1267. var dom = this.get_node(obj, true), k, l, i, j, c;
  1268. if($.vakata.is_array(obj)) {
  1269. this._load_nodes(obj.slice(), callback);
  1270. return true;
  1271. }
  1272. obj = this.get_node(obj);
  1273. if(!obj) {
  1274. if(callback) { callback.call(this, obj, false); }
  1275. return false;
  1276. }
  1277. // 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?
  1278. if(obj.state.loaded) {
  1279. obj.state.loaded = false;
  1280. for(i = 0, j = obj.parents.length; i < j; i++) {
  1281. this._model.data[obj.parents[i]].children_d = $.vakata.array_filter(this._model.data[obj.parents[i]].children_d, function (v) {
  1282. return $.inArray(v, obj.children_d) === -1;
  1283. });
  1284. }
  1285. for(k = 0, l = obj.children_d.length; k < l; k++) {
  1286. if(this._model.data[obj.children_d[k]].state.selected) {
  1287. c = true;
  1288. }
  1289. delete this._model.data[obj.children_d[k]];
  1290. }
  1291. if (c) {
  1292. this._data.core.selected = $.vakata.array_filter(this._data.core.selected, function (v) {
  1293. return $.inArray(v, obj.children_d) === -1;
  1294. });
  1295. }
  1296. obj.children = [];
  1297. obj.children_d = [];
  1298. if(c) {
  1299. this.trigger('changed', { 'action' : 'load_node', 'node' : obj, 'selected' : this._data.core.selected });
  1300. }
  1301. }
  1302. obj.state.failed = false;
  1303. obj.state.loading = true;
  1304. if (obj.id !== $.jstree.root) {
  1305. dom.children(".jstree-anchor").attr('aria-busy', true);
  1306. } else {
  1307. dom.attr('aria-busy', true);
  1308. }
  1309. dom.addClass("jstree-loading");
  1310. this._load_node(obj, function (status) {
  1311. obj = this._model.data[obj.id];
  1312. obj.state.loading = false;
  1313. obj.state.loaded = status;
  1314. obj.state.failed = !obj.state.loaded;
  1315. var dom = this.get_node(obj, true), i = 0, j = 0, m = this._model.data, has_children = false;
  1316. for(i = 0, j = obj.children.length; i < j; i++) {
  1317. if(m[obj.children[i]] && !m[obj.children[i]].state.hidden) {
  1318. has_children = true;
  1319. break;
  1320. }
  1321. }
  1322. if(obj.state.loaded && dom && dom.length) {
  1323. dom.removeClass('jstree-closed jstree-open jstree-leaf');
  1324. if (!has_children) {
  1325. dom.addClass('jstree-leaf');
  1326. }
  1327. else {
  1328. if (obj.id !== '#') {
  1329. dom.addClass(obj.state.opened ? 'jstree-open' : 'jstree-closed');
  1330. }
  1331. }
  1332. }
  1333. if (obj.id !== $.jstree.root) {
  1334. dom.children(".jstree-anchor").attr('aria-busy', false);
  1335. } else {
  1336. dom.attr('aria-busy', false);
  1337. }
  1338. dom.removeClass("jstree-loading");
  1339. /**
  1340. * triggered after a node is loaded
  1341. * @event
  1342. * @name load_node.jstree
  1343. * @param {Object} node the node that was loading
  1344. * @param {Boolean} status was the node loaded successfully
  1345. */
  1346. this.trigger('load_node', { "node" : obj, "status" : status });
  1347. if(callback) {
  1348. callback.call(this, obj, status);
  1349. }
  1350. }.bind(this));
  1351. return true;
  1352. },
  1353. /**
  1354. * load an array of nodes (will also load unavailable nodes as soon as they appear in the structure). Used internally.
  1355. * @private
  1356. * @name _load_nodes(nodes [, callback])
  1357. * @param {array} nodes
  1358. * @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
  1359. */
  1360. _load_nodes : function (nodes, callback, is_callback, force_reload) {
  1361. var r = true,
  1362. c = function () { this._load_nodes(nodes, callback, true); },
  1363. m = this._model.data, i, j, tmp = [];
  1364. for(i = 0, j = nodes.length; i < j; i++) {
  1365. if(m[nodes[i]] && ( (!m[nodes[i]].state.loaded && !m[nodes[i]].state.failed) || (!is_callback && force_reload) )) {
  1366. if(!this.is_loading(nodes[i])) {
  1367. this.load_node(nodes[i], c);
  1368. }
  1369. r = false;
  1370. }
  1371. }
  1372. if(r) {
  1373. for(i = 0, j = nodes.length; i < j; i++) {
  1374. if(m[nodes[i]] && m[nodes[i]].state.loaded) {
  1375. tmp.push(nodes[i]);
  1376. }
  1377. }
  1378. if(callback && !callback.done) {
  1379. callback.call(this, tmp);
  1380. callback.done = true;
  1381. }
  1382. }
  1383. },
  1384. /**
  1385. * loads all unloaded nodes
  1386. * @name load_all([obj, callback])
  1387. * @param {mixed} obj the node to load recursively, omit to load all nodes in the tree
  1388. * @param {function} callback a function to be executed once loading all the nodes is complete,
  1389. * @trigger load_all.jstree
  1390. */
  1391. load_all : function (obj, callback) {
  1392. if(!obj) { obj = $.jstree.root; }
  1393. obj = this.get_node(obj);
  1394. if(!obj) { return false; }
  1395. var to_load = [],
  1396. m = this._model.data,
  1397. c = m[obj.id].children_d,
  1398. i, j;
  1399. if(obj.state && !obj.state.loaded) {
  1400. to_load.push(obj.id);
  1401. }
  1402. for(i = 0, j = c.length; i < j; i++) {
  1403. if(m[c[i]] && m[c[i]].state && !m[c[i]].state.loaded) {
  1404. to_load.push(c[i]);
  1405. }
  1406. }
  1407. if(to_load.length) {
  1408. this._load_nodes(to_load, function () {
  1409. this.load_all(obj, callback);
  1410. });
  1411. }
  1412. else {
  1413. /**
  1414. * triggered after a load_all call completes
  1415. * @event
  1416. * @name load_all.jstree
  1417. * @param {Object} node the recursively loaded node
  1418. */
  1419. if(callback) { callback.call(this, obj); }
  1420. this.trigger('load_all', { "node" : obj });
  1421. }
  1422. },
  1423. /**
  1424. * handles the actual loading of a node. Used only internally.
  1425. * @private
  1426. * @name _load_node(obj [, callback])
  1427. * @param {mixed} obj
  1428. * @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
  1429. * @return {Boolean}
  1430. */
  1431. _load_node : function (obj, callback) {
  1432. var s = this.settings.core.data, t;
  1433. var notTextOrCommentNode = function notTextOrCommentNode () {
  1434. return this.nodeType !== 3 && this.nodeType !== 8;
  1435. };
  1436. // use original HTML
  1437. if(!s) {
  1438. if(obj.id === $.jstree.root) {
  1439. return this._append_html_data(obj, this._data.core.original_container_html.clone(true), function (status) {
  1440. callback.call(this, status);
  1441. });
  1442. }
  1443. else {
  1444. return callback.call(this, false);
  1445. }
  1446. // return callback.call(this, obj.id === $.jstree.root ? this._append_html_data(obj, this._data.core.original_container_html.clone(true)) : false);
  1447. }
  1448. if($.vakata.is_function(s)) {
  1449. return s.call(this, obj, function (d) {
  1450. if(d === false) {
  1451. callback.call(this, false);
  1452. }
  1453. else {
  1454. this[typeof d === 'string' ? '_append_html_data' : '_append_json_data'](obj, typeof d === 'string' ? $($.parseHTML(d)).filter(notTextOrCommentNode) : d, function (status) {
  1455. callback.call(this, status);
  1456. });
  1457. }
  1458. // 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));
  1459. }.bind(this));
  1460. }
  1461. if(typeof s === 'object') {
  1462. if(s.url) {
  1463. s = $.extend(true, {}, s);
  1464. if($.vakata.is_function(s.url)) {
  1465. s.url = s.url.call(this, obj);
  1466. }
  1467. if($.vakata.is_function(s.data)) {
  1468. s.data = s.data.call(this, obj);
  1469. }
  1470. return $.ajax(s)
  1471. .done(function (d,t,x) {
  1472. var type = x.getResponseHeader('Content-Type');
  1473. if((type && type.indexOf('json') !== -1) || typeof d === "object") {
  1474. return this._append_json_data(obj, d, function (status) { callback.call(this, status); });
  1475. //return callback.call(this, this._append_json_data(obj, d));
  1476. }
  1477. if((type && type.indexOf('html') !== -1) || typeof d === "string") {
  1478. return this._append_html_data(obj, $($.parseHTML(d)).filter(notTextOrCommentNode), function (status) { callback.call(this, status); });
  1479. // return callback.call(this, this._append_html_data(obj, $(d)));
  1480. }
  1481. this._data.core.last_error = { 'error' : 'ajax', 'plugin' : 'core', 'id' : 'core_04', 'reason' : 'Could not load node', 'data' : JSON.stringify({ 'id' : obj.id, 'xhr' : x }) };
  1482. this.settings.core.error.call(this, this._data.core.last_error);
  1483. return callback.call(this, false);
  1484. }.bind(this))
  1485. .fail(function (f) {
  1486. 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 }) };
  1487. callback.call(this, false);
  1488. this.settings.core.error.call(this, this._data.core.last_error);
  1489. }.bind(this));
  1490. }
  1491. if ($.vakata.is_array(s)) {
  1492. t = $.extend(true, [], s);
  1493. } else if ($.isPlainObject(s)) {
  1494. t = $.extend(true, {}, s);
  1495. } else {
  1496. t = s;
  1497. }
  1498. if(obj.id === $.jstree.root) {
  1499. return this._append_json_data(obj, t, function (status) {
  1500. callback.call(this, status);
  1501. });
  1502. }
  1503. else {
  1504. this._data.core.last_error = { 'error' : 'nodata', 'plugin' : 'core', 'id' : 'core_05', 'reason' : 'Could not load node', 'data' : JSON.stringify({ 'id' : obj.id }) };
  1505. this.settings.core.error.call(this, this._data.core.last_error);
  1506. return callback.call(this, false);
  1507. }
  1508. //return callback.call(this, (obj.id === $.jstree.root ? this._append_json_data(obj, t) : false) );
  1509. }
  1510. if(typeof s === 'string') {
  1511. if(obj.id === $.jstree.root) {
  1512. return this._append_html_data(obj, $($.parseHTML(s)).filter(notTextOrCommentNode), function (status) {
  1513. callback.call(this, status);
  1514. });
  1515. }
  1516. else {
  1517. this._data.core.last_error = { 'error' : 'nodata', 'plugin' : 'core', 'id' : 'core_06', 'reason' : 'Could not load node', 'data' : JSON.stringify({ 'id' : obj.id }) };
  1518. this.settings.core.error.call(this, this._data.core.last_error);
  1519. return callback.call(this, false);
  1520. }
  1521. //return callback.call(this, (obj.id === $.jstree.root ? this._append_html_data(obj, $(s)) : false) );
  1522. }
  1523. return callback.call(this, false);
  1524. },
  1525. /**
  1526. * adds a node to the list of nodes to redraw. Used only internally.
  1527. * @private
  1528. * @name _node_changed(obj [, callback])
  1529. * @param {mixed} obj
  1530. */
  1531. _node_changed : function (obj) {
  1532. obj = this.get_node(obj);
  1533. if (obj && $.inArray(obj.id, this._model.changed) === -1) {
  1534. this._model.changed.push(obj.id);
  1535. }
  1536. },
  1537. /**
  1538. * appends HTML content to the tree. Used internally.
  1539. * @private
  1540. * @name _append_html_data(obj, data)
  1541. * @param {mixed} obj the node to append to
  1542. * @param {String} data the HTML string to parse and append
  1543. * @trigger model.jstree, changed.jstree
  1544. */
  1545. _append_html_data : function (dom, data, cb) {
  1546. dom = this.get_node(dom);
  1547. dom.children = [];
  1548. dom.children_d = [];
  1549. var dat = data.is('ul') ? data.children() : data,
  1550. par = dom.id,
  1551. chd = [],
  1552. dpc = [],
  1553. m = this._model.data,
  1554. p = m[par],
  1555. s = this._data.core.selected.length,
  1556. tmp, i, j;
  1557. dat.each(function (i, v) {
  1558. tmp = this._parse_model_from_html($(v), par, p.parents.concat());
  1559. if(tmp) {
  1560. chd.push(tmp);
  1561. dpc.push(tmp);
  1562. if(m[tmp].children_d.length) {
  1563. dpc = dpc.concat(m[tmp].children_d);
  1564. }
  1565. }
  1566. }.bind(this));
  1567. p.children = chd;
  1568. p.children_d = dpc;
  1569. for(i = 0, j = p.parents.length; i < j; i++) {
  1570. m[p.parents[i]].children_d = m[p.parents[i]].children_d.concat(dpc);
  1571. }
  1572. /**
  1573. * triggered when new data is inserted to the tree model
  1574. * @event
  1575. * @name model.jstree
  1576. * @param {Array} nodes an array of node IDs
  1577. * @param {String} parent the parent ID of the nodes
  1578. */
  1579. this.trigger('model', { "nodes" : dpc, 'parent' : par });
  1580. if(par !== $.jstree.root) {
  1581. this._node_changed(par);
  1582. this.redraw();
  1583. }
  1584. else {
  1585. this.get_container_ul().children('.jstree-initial-node').remove();
  1586. this.redraw(true);
  1587. }
  1588. if(this._data.core.selected.length !== s) {
  1589. this.trigger('changed', { 'action' : 'model', 'selected' : this._data.core.selected });
  1590. }
  1591. cb.call(this, true);
  1592. },
  1593. /**
  1594. * appends JSON content to the tree. Used internally.
  1595. * @private
  1596. * @name _append_json_data(obj, data)
  1597. * @param {mixed} obj the node to append to
  1598. * @param {String} data the JSON object to parse and append
  1599. * @param {Boolean} force_processing internal param - do not set
  1600. * @trigger model.jstree, changed.jstree
  1601. */
  1602. _append_json_data : function (dom, data, cb, force_processing) {
  1603. if(this.element === null) { return; }
  1604. dom = this.get_node(dom);
  1605. dom.children = [];
  1606. dom.children_d = [];
  1607. // *%$@!!!
  1608. if(data.d) {
  1609. data = data.d;
  1610. if(typeof data === "string") {
  1611. data = JSON.parse(data);
  1612. }
  1613. }
  1614. if(!$.vakata.is_array(data)) { data = [data]; }
  1615. var w = null,
  1616. args = {
  1617. 'df' : this._model.default_state,
  1618. 'dat' : data,
  1619. 'par' : dom.id,
  1620. 'm' : this._model.data,
  1621. 't_id' : this._id,
  1622. 't_cnt' : this._cnt,
  1623. 'sel' : this._data.core.selected
  1624. },
  1625. inst = this,
  1626. func = function (data, undefined) {
  1627. if(data.data) { data = data.data; }
  1628. var dat = data.dat,
  1629. par = data.par,
  1630. chd = [],
  1631. dpc = [],
  1632. add = [],
  1633. df = data.df,
  1634. t_id = data.t_id,
  1635. t_cnt = data.t_cnt,
  1636. m = data.m,
  1637. p = m[par],
  1638. sel = data.sel,
  1639. tmp, i, j, rslt,
  1640. parse_flat = function (d, p, ps) {
  1641. if(!ps) { ps = []; }
  1642. else { ps = ps.concat(); }
  1643. if(p) { ps.unshift(p); }
  1644. var tid = d.id.toString(),
  1645. i, j, c, e,
  1646. tmp = {
  1647. id : tid,
  1648. text : d.text || '',
  1649. icon : d.icon !== undefined ? d.icon : true,
  1650. parent : p,
  1651. parents : ps,
  1652. children : d.children || [],
  1653. children_d : d.children_d || [],
  1654. data : d.data,
  1655. state : { },
  1656. li_attr : { id : false },
  1657. a_attr : { href : '#' },
  1658. original : false
  1659. };
  1660. for(i in df) {
  1661. if(df.hasOwnProperty(i)) {
  1662. tmp.state[i] = df[i];
  1663. }
  1664. }
  1665. if(d && d.data && d.data.jstree && d.data.jstree.icon) {
  1666. tmp.icon = d.data.jstree.icon;
  1667. }
  1668. if(tmp.icon === undefined || tmp.icon === null || tmp.icon === "") {
  1669. tmp.icon = true;
  1670. }
  1671. if(d && d.data) {
  1672. tmp.data = d.data;
  1673. if(d.data.jstree) {
  1674. for(i in d.data.jstree) {
  1675. if(d.data.jstree.hasOwnProperty(i)) {
  1676. tmp.state[i] = d.data.jstree[i];
  1677. }
  1678. }
  1679. }
  1680. }
  1681. if(d && typeof d.state === 'object') {
  1682. for (i in d.state) {
  1683. if(d.state.hasOwnProperty(i)) {
  1684. tmp.state[i] = d.state[i];
  1685. }
  1686. }
  1687. }
  1688. if(d && typeof d.li_attr === 'object') {
  1689. for (i in d.li_attr) {
  1690. if(d.li_attr.hasOwnProperty(i)) {
  1691. tmp.li_attr[i] = d.li_attr[i];
  1692. }
  1693. }
  1694. }
  1695. if(!tmp.li_attr.id) {
  1696. tmp.li_attr.id = tid;
  1697. }
  1698. if(d && typeof d.a_attr === 'object') {
  1699. for (i in d.a_attr) {
  1700. if(d.a_attr.hasOwnProperty(i)) {
  1701. tmp.a_attr[i] = d.a_attr[i];
  1702. }
  1703. }
  1704. }
  1705. if(d && d.children && d.children === true) {
  1706. tmp.state.loaded = false;
  1707. tmp.children = [];
  1708. tmp.children_d = [];
  1709. }
  1710. m[tmp.id] = tmp;
  1711. for(i = 0, j = tmp.children.length; i < j; i++) {
  1712. c = parse_flat(m[tmp.children[i]], tmp.id, ps);
  1713. e = m[c];
  1714. tmp.children_d.push(c);
  1715. if(e.children_d.length) {
  1716. tmp.children_d = tmp.children_d.concat(e.children_d);
  1717. }
  1718. }
  1719. delete d.data;
  1720. delete d.children;
  1721. m[tmp.id].original = d;
  1722. if(tmp.state.selected) {
  1723. add.push(tmp.id);
  1724. }
  1725. return tmp.id;
  1726. },
  1727. parse_nest = function (d, p, ps) {
  1728. if(!ps) { ps = []; }
  1729. else { ps = ps.concat(); }
  1730. if(p) { ps.unshift(p); }
  1731. var tid = false, i, j, c, e, tmp;
  1732. do {
  1733. tid = 'j' + t_id + '_' + (++t_cnt);
  1734. } while(m[tid]);
  1735. tmp = {
  1736. id : false,
  1737. text : typeof d === 'string' ? d : '',
  1738. icon : typeof d === 'object' && d.icon !== undefined ? d.icon : true,
  1739. parent : p,
  1740. parents : ps,
  1741. children : [],
  1742. children_d : [],
  1743. data : null,
  1744. state : { },
  1745. li_attr : { id : false },
  1746. a_attr : { href : '#' },
  1747. original : false
  1748. };
  1749. for(i in df) {
  1750. if(df.hasOwnProperty(i)) {
  1751. tmp.state[i] = df[i];
  1752. }
  1753. }
  1754. if(d && (d.id || d.id === 0)) { tmp.id = d.id.toString(); }
  1755. if(d && d.text) { tmp.text = d.text; }
  1756. if(d && d.data && d.data.jstree && d.data.jstree.icon) {
  1757. tmp.icon = d.data.jstree.icon;
  1758. }
  1759. if(tmp.icon === undefined || tmp.icon === null || tmp.icon === "") {
  1760. tmp.icon = true;
  1761. }
  1762. if(d && d.data) {
  1763. tmp.data = d.data;
  1764. if(d.data.jstree) {
  1765. for(i in d.data.jstree) {
  1766. if(d.data.jstree.hasOwnProperty(i)) {
  1767. tmp.state[i] = d.data.jstree[i];
  1768. }
  1769. }
  1770. }
  1771. }
  1772. if(d && typeof d.state === 'object') {
  1773. for (i in d.state) {
  1774. if(d.state.hasOwnProperty(i)) {
  1775. tmp.state[i] = d.state[i];
  1776. }
  1777. }
  1778. }
  1779. if(d && typeof d.li_attr === 'object') {
  1780. for (i in d.li_attr) {
  1781. if(d.li_attr.hasOwnProperty(i)) {
  1782. tmp.li_attr[i] = d.li_attr[i];
  1783. }
  1784. }
  1785. }
  1786. if(tmp.li_attr.id && !(tmp.id || tmp.id === 0)) {
  1787. tmp.id = tmp.li_attr.id.toString();
  1788. }
  1789. if(!(tmp.id || tmp.id === 0)) {
  1790. tmp.id = tid;
  1791. }
  1792. if(!tmp.li_attr.id) {
  1793. tmp.li_attr.id = tmp.id;
  1794. }
  1795. if(d && typeof d.a_attr === 'object') {
  1796. for (i in d.a_attr) {
  1797. if(d.a_attr.hasOwnProperty(i)) {
  1798. tmp.a_attr[i] = d.a_attr[i];
  1799. }
  1800. }
  1801. }
  1802. if(d && d.children && d.children.length) {
  1803. for(i = 0, j = d.children.length; i < j; i++) {
  1804. c = parse_nest(d.children[i], tmp.id, ps);
  1805. e = m[c];
  1806. tmp.children.push(c);
  1807. if(e.children_d.length) {
  1808. tmp.children_d = tmp.children_d.concat(e.children_d);
  1809. }
  1810. }
  1811. tmp.children_d = tmp.children_d.concat(tmp.children);
  1812. }
  1813. if(d && d.children && d.children === true) {
  1814. tmp.state.loaded = false;
  1815. tmp.children = [];
  1816. tmp.children_d = [];
  1817. }
  1818. delete d.data;
  1819. delete d.children;
  1820. tmp.original = d;
  1821. m[tmp.id] = tmp;
  1822. if(tmp.state.selected) {
  1823. add.push(tmp.id);
  1824. }
  1825. return tmp.id;
  1826. };
  1827. if(dat.length && dat[0].id !== undefined && dat[0].parent !== undefined) {
  1828. // Flat JSON support (for easy import from DB):
  1829. // 1) convert to object (foreach)
  1830. for(i = 0, j = dat.length; i < j; i++) {
  1831. if(!dat[i].children) {
  1832. dat[i].children = [];
  1833. }
  1834. if(!dat[i].state) {
  1835. dat[i].state = {};
  1836. }
  1837. m[dat[i].id.toString()] = dat[i];
  1838. }
  1839. // 2) populate children (foreach)
  1840. for(i = 0, j = dat.length; i < j; i++) {
  1841. if (!m[dat[i].parent.toString()]) {
  1842. if (typeof inst !== "undefined") {
  1843. inst._data.core.last_error = { 'error' : 'parse', 'plugin' : 'core', 'id' : 'core_07', 'reason' : 'Node with invalid parent', 'data' : JSON.stringify({ 'id' : dat[i].id.toString(), 'parent' : dat[i].parent.toString() }) };
  1844. inst.settings.core.error.call(inst, inst._data.core.last_error);
  1845. }
  1846. continue;
  1847. }
  1848. m[dat[i].parent.toString()].children.push(dat[i].id.toString());
  1849. // populate parent.children_d
  1850. p.children_d.push(dat[i].id.toString());
  1851. }
  1852. // 3) normalize && populate parents and children_d with recursion
  1853. for(i = 0, j = p.children.length; i < j; i++) {
  1854. tmp = parse_flat(m[p.children[i]], par, p.parents.concat());
  1855. dpc.push(tmp);
  1856. if(m[tmp].children_d.length) {
  1857. dpc = dpc.concat(m[tmp].children_d);
  1858. }
  1859. }
  1860. for(i = 0, j = p.parents.length; i < j; i++) {
  1861. m[p.parents[i]].children_d = m[p.parents[i]].children_d.concat(dpc);
  1862. }
  1863. // ?) three_state selection - p.state.selected && t - (if three_state foreach(dat => ch) -> foreach(parents) if(parent.selected) child.selected = true;
  1864. rslt = {
  1865. 'cnt' : t_cnt,
  1866. 'mod' : m,
  1867. 'sel' : sel,
  1868. 'par' : par,
  1869. 'dpc' : dpc,
  1870. 'add' : add
  1871. };
  1872. }
  1873. else {
  1874. for(i = 0, j = dat.length; i < j; i++) {
  1875. tmp = parse_nest(dat[i], par, p.parents.concat());
  1876. if(tmp) {
  1877. chd.push(tmp);
  1878. dpc.push(tmp);
  1879. if(m[tmp].children_d.length) {
  1880. dpc = dpc.concat(m[tmp].children_d);
  1881. }
  1882. }
  1883. }
  1884. p.children = chd;
  1885. p.children_d = dpc;
  1886. for(i = 0, j = p.parents.length; i < j; i++) {
  1887. m[p.parents[i]].children_d = m[p.parents[i]].children_d.concat(dpc);
  1888. }
  1889. rslt = {
  1890. 'cnt' : t_cnt,
  1891. 'mod' : m,
  1892. 'sel' : sel,
  1893. 'par' : par,
  1894. 'dpc' : dpc,
  1895. 'add' : add
  1896. };
  1897. }
  1898. if(typeof window === 'undefined' || typeof window.document === 'undefined') {
  1899. postMessage(rslt);
  1900. }
  1901. else {
  1902. return rslt;
  1903. }
  1904. },
  1905. rslt = function (rslt, worker) {
  1906. if(this.element === null) { return; }
  1907. this._cnt = rslt.cnt;
  1908. var i, m = this._model.data;
  1909. for (i in m) {
  1910. if (m.hasOwnProperty(i) && m[i].state && m[i].state.loading && rslt.mod[i]) {
  1911. rslt.mod[i].state.loading = true;
  1912. }
  1913. }
  1914. this._model.data = rslt.mod; // breaks the reference in load_node - careful
  1915. if(worker) {
  1916. var j, a = rslt.add, r = rslt.sel, s = this._data.core.selected.slice();
  1917. m = this._model.data;
  1918. // if selection was changed while calculating in worker
  1919. if(r.length !== s.length || $.vakata.array_unique(r.concat(s)).length !== r.length) {
  1920. // deselect nodes that are no longer selected
  1921. for(i = 0, j = r.length; i < j; i++) {
  1922. if($.inArray(r[i], a) === -1 && $.inArray(r[i], s) === -1) {
  1923. m[r[i]].state.selected = false;
  1924. }
  1925. }
  1926. // select nodes that were selected in the mean time
  1927. for(i = 0, j = s.length; i < j; i++) {
  1928. if($.inArray(s[i], r) === -1) {
  1929. m[s[i]].state.selected = true;
  1930. }
  1931. }
  1932. }
  1933. }
  1934. if(rslt.add.length) {
  1935. this._data.core.selected = this._data.core.selected.concat(rslt.add);
  1936. }
  1937. this.trigger('model', { "nodes" : rslt.dpc, 'parent' : rslt.par });
  1938. if(rslt.par !== $.jstree.root) {
  1939. this._node_changed(rslt.par);
  1940. this.redraw();
  1941. }
  1942. else {
  1943. // this.get_container_ul().children('.jstree-initial-node').remove();
  1944. this.redraw(true);
  1945. }
  1946. if(rslt.add.length) {
  1947. this.trigger('changed', { 'action' : 'model', 'selected' : this._data.core.selected });
  1948. }
  1949. // If no worker, try to mimic worker behavioour, by invoking cb asynchronously
  1950. if (!worker && setImmediate) {
  1951. setImmediate(function(){
  1952. cb.call(inst, true);
  1953. });
  1954. }
  1955. else {
  1956. cb.call(inst, true);
  1957. }
  1958. };
  1959. if(this.settings.core.worker && window.Blob && window.URL && window.Worker) {
  1960. try {
  1961. if(this._wrk === null) {
  1962. this._wrk = window.URL.createObjectURL(
  1963. new window.Blob(
  1964. ['self.onmessage = ' + func.toString()],
  1965. {type:"text/javascript"}
  1966. )
  1967. );
  1968. }
  1969. if(!this._data.core.working || force_processing) {
  1970. this._data.core.working = true;
  1971. w = new window.Worker(this._wrk);
  1972. w.onmessage = function (e) {
  1973. rslt.call(this, e.data, true);
  1974. try { w.terminate(); w = null; } catch(ignore) { }
  1975. if(this._data.core.worker_queue.length) {
  1976. this._append_json_data.apply(this, this._data.core.worker_queue.shift());
  1977. }
  1978. else {
  1979. this._data.core.working = false;
  1980. }
  1981. }.bind(this);
  1982. w.onerror = function (e) {
  1983. rslt.call(this, func(args), false);
  1984. if(this._data.core.worker_queue.length) {
  1985. this._append_json_data.apply(this, this._data.core.worker_queue.shift());
  1986. }
  1987. else {
  1988. this._data.core.working = false;
  1989. }
  1990. }.bind(this);
  1991. if(!args.par) {
  1992. if(this._data.core.worker_queue.length) {
  1993. this._append_json_data.apply(this, this._data.core.worker_queue.shift());
  1994. }
  1995. else {
  1996. this._data.core.working = false;
  1997. }
  1998. }
  1999. else {
  2000. w.postMessage(args);
  2001. }
  2002. }
  2003. else {
  2004. this._data.core.worker_queue.push([dom, data, cb, true]);
  2005. }
  2006. }
  2007. catch(e) {
  2008. rslt.call(this, func(args), false);
  2009. if(this._data.core.worker_queue.length) {
  2010. this._append_json_data.apply(this, this._data.core.worker_queue.shift());
  2011. }
  2012. else {
  2013. this._data.core.working = false;
  2014. }
  2015. }
  2016. }
  2017. else {
  2018. rslt.call(this, func(args), false);
  2019. }
  2020. },
  2021. /**
  2022. * parses a node from a jQuery object and appends them to the in memory tree model. Used internally.
  2023. * @private
  2024. * @name _parse_model_from_html(d [, p, ps])
  2025. * @param {jQuery} d the jQuery object to parse
  2026. * @param {String} p the parent ID
  2027. * @param {Array} ps list of all parents
  2028. * @return {String} the ID of the object added to the model
  2029. */
  2030. _parse_model_from_html : function (d, p, ps) {
  2031. if(!ps) { ps = []; }
  2032. else { ps = [].concat(ps); }
  2033. if(p) { ps.unshift(p); }
  2034. var c, e, m = this._model.data,
  2035. data = {
  2036. id : false,
  2037. text : false,
  2038. icon : true,
  2039. parent : p,
  2040. parents : ps,
  2041. children : [],
  2042. children_d : [],
  2043. data : null,
  2044. state : { },
  2045. li_attr : { id : false },
  2046. a_attr : { href : '#' },
  2047. original : false
  2048. }, i, tmp, tid;
  2049. for(i in this._model.default_state) {
  2050. if(this._model.default_state.hasOwnProperty(i)) {
  2051. data.state[i] = this._model.default_state[i];
  2052. }
  2053. }
  2054. tmp = $.vakata.attributes(d, true);
  2055. $.each(tmp, function (i, v) {
  2056. v = $.vakata.trim(v);
  2057. if(!v.length) { return true; }
  2058. data.li_attr[i] = v;
  2059. if(i === 'id') {
  2060. data.id = v.toString();
  2061. }
  2062. });
  2063. tmp = d.children('a').first();
  2064. if(tmp.length) {
  2065. tmp = $.vakata.attributes(tmp, true);
  2066. $.each(tmp, function (i, v) {
  2067. v = $.vakata.trim(v);
  2068. if(v.length) {
  2069. data.a_attr[i] = v;
  2070. }
  2071. });
  2072. }
  2073. tmp = d.children("a").first().length ? d.children("a").first().clone() : d.clone();
  2074. tmp.children("ins, i, ul").remove();
  2075. tmp = tmp.html();
  2076. tmp = $('<div></div>').html(tmp);
  2077. data.text = this.settings.core.force_text ? tmp.text() : tmp.html();
  2078. tmp = d.data();
  2079. data.data = tmp ? $.extend(true, {}, tmp) : null;
  2080. data.state.opened = d.hasClass('jstree-open');
  2081. data.state.selected = d.children('a').hasClass('jstree-clicked');
  2082. data.state.disabled = d.children('a').hasClass('jstree-disabled');
  2083. if(data.data && data.data.jstree) {
  2084. for(i in data.data.jstree) {
  2085. if(data.data.jstree.hasOwnProperty(i)) {
  2086. data.state[i] = data.data.jstree[i];
  2087. }
  2088. }
  2089. }
  2090. tmp = d.children("a").children(".jstree-themeicon");
  2091. if(tmp.length) {
  2092. data.icon = tmp.hasClass('jstree-themeicon-hidden') ? false : tmp.attr('rel');
  2093. }
  2094. if(data.state.icon !== undefined) {
  2095. data.icon = data.state.icon;
  2096. }
  2097. if(data.icon === undefined || data.icon === null || data.icon === "") {
  2098. data.icon = true;
  2099. }
  2100. tmp = d.children("ul").children("li");
  2101. do {
  2102. tid = 'j' + this._id + '_' + (++this._cnt);
  2103. } while(m[tid]);
  2104. data.id = data.li_attr.id ? data.li_attr.id.toString() : tid;
  2105. if(tmp.length) {
  2106. tmp.each(function (i, v) {
  2107. c = this._parse_model_from_html($(v), data.id, ps);
  2108. e = this._model.data[c];
  2109. data.children.push(c);
  2110. if(e.children_d.length) {
  2111. data.children_d = data.children_d.concat(e.children_d);
  2112. }
  2113. }.bind(this));
  2114. data.children_d = data.children_d.concat(data.children);
  2115. }
  2116. else {
  2117. if(d.hasClass('jstree-closed')) {
  2118. data.state.loaded = false;
  2119. }
  2120. }
  2121. if(data.li_attr['class']) {
  2122. data.li_attr['class'] = data.li_attr['class'].replace('jstree-closed','').replace('jstree-open','');
  2123. }
  2124. if(data.a_attr['class']) {
  2125. data.a_attr['class'] = data.a_attr['class'].replace('jstree-clicked','').replace('jstree-disabled','');
  2126. }
  2127. m[data.id] = data;
  2128. if(data.state.selected) {
  2129. this._data.core.selected.push(data.id);
  2130. }
  2131. return data.id;
  2132. },
  2133. /**
  2134. * 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.
  2135. * @private
  2136. * @name _parse_model_from_flat_json(d [, p, ps])
  2137. * @param {Object} d the JSON object to parse
  2138. * @param {String} p the parent ID
  2139. * @param {Array} ps list of all parents
  2140. * @return {String} the ID of the object added to the model
  2141. */
  2142. _parse_model_from_flat_json : function (d, p, ps) {
  2143. if(!ps) { ps = []; }
  2144. else { ps = ps.concat(); }
  2145. if(p) { ps.unshift(p); }
  2146. var tid = d.id.toString(),
  2147. m = this._model.data,
  2148. df = this._model.default_state,
  2149. i, j, c, e,
  2150. tmp = {
  2151. id : tid,
  2152. text : d.text || '',
  2153. icon : d.icon !== undefined ? d.icon : true,
  2154. parent : p,
  2155. parents : ps,
  2156. children : d.children || [],
  2157. children_d : d.children_d || [],
  2158. data : d.data,
  2159. state : { },
  2160. li_attr : { id : false },
  2161. a_attr : { href : '#' },
  2162. original : false
  2163. };
  2164. for(i in df) {
  2165. if(df.hasOwnProperty(i)) {
  2166. tmp.state[i] = df[i];
  2167. }
  2168. }
  2169. if(d && d.data && d.data.jstree && d.data.jstree.icon) {
  2170. tmp.icon = d.data.jstree.icon;
  2171. }
  2172. if(tmp.icon === undefined || tmp.icon === null || tmp.icon === "") {
  2173. tmp.icon = true;
  2174. }
  2175. if(d && d.data) {
  2176. tmp.data = d.data;
  2177. if(d.data.jstree) {
  2178. for(i in d.data.jstree) {
  2179. if(d.data.jstree.hasOwnProperty(i)) {
  2180. tmp.state[i] = d.data.jstree[i];
  2181. }
  2182. }
  2183. }
  2184. }
  2185. if(d && typeof d.state === 'object') {
  2186. for (i in d.state) {
  2187. if(d.state.hasOwnProperty(i)) {
  2188. tmp.state[i] = d.state[i];
  2189. }
  2190. }
  2191. }
  2192. if(d && typeof d.li_attr === 'object') {
  2193. for (i in d.li_attr) {
  2194. if(d.li_attr.hasOwnProperty(i)) {
  2195. tmp.li_attr[i] = d.li_attr[i];
  2196. }
  2197. }
  2198. }
  2199. if(!tmp.li_attr.id) {
  2200. tmp.li_attr.id = tid;
  2201. }
  2202. if(d && typeof d.a_attr === 'object') {
  2203. for (i in d.a_attr) {
  2204. if(d.a_attr.hasOwnProperty(i)) {
  2205. tmp.a_attr[i] = d.a_attr[i];
  2206. }
  2207. }
  2208. }
  2209. if(d && d.children && d.children === true) {
  2210. tmp.state.loaded = false;
  2211. tmp.children = [];
  2212. tmp.children_d = [];
  2213. }
  2214. m[tmp.id] = tmp;
  2215. for(i = 0, j = tmp.children.length; i < j; i++) {
  2216. c = this._parse_model_from_flat_json(m[tmp.children[i]], tmp.id, ps);
  2217. e = m[c];
  2218. tmp.children_d.push(c);
  2219. if(e.children_d.length) {
  2220. tmp.children_d = tmp.children_d.concat(e.children_d);
  2221. }
  2222. }
  2223. delete d.data;
  2224. delete d.children;
  2225. m[tmp.id].original = d;
  2226. if(tmp.state.selected) {
  2227. this._data.core.selected.push(tmp.id);
  2228. }
  2229. return tmp.id;
  2230. },
  2231. /**
  2232. * parses a node from a JSON object and appends it to the in memory tree model. Used internally.
  2233. * @private
  2234. * @name _parse_model_from_json(d [, p, ps])
  2235. * @param {Object} d the JSON object to parse
  2236. * @param {String} p the parent ID
  2237. * @param {Array} ps list of all parents
  2238. * @return {String} the ID of the object added to the model
  2239. */
  2240. _parse_model_from_json : function (d, p, ps) {
  2241. if(!ps) { ps = []; }
  2242. else { ps = ps.concat(); }
  2243. if(p) { ps.unshift(p); }
  2244. var tid = false, i, j, c, e, m = this._model.data, df = this._model.default_state, tmp;
  2245. do {
  2246. tid = 'j' + this._id + '_' + (++this._cnt);
  2247. } while(m[tid]);
  2248. tmp = {
  2249. id : false,
  2250. text : typeof d === 'string' ? d : '',
  2251. icon : typeof d === 'object' && d.icon !== undefined ? d.icon : true,
  2252. parent : p,
  2253. parents : ps,
  2254. children : [],
  2255. children_d : [],
  2256. data : null,
  2257. state : { },
  2258. li_attr : { id : false },
  2259. a_attr : { href : '#' },
  2260. original : false
  2261. };
  2262. for(i in df) {
  2263. if(df.hasOwnProperty(i)) {
  2264. tmp.state[i] = df[i];
  2265. }
  2266. }
  2267. if(d && (d.id || d.id === 0)) { tmp.id = d.id.toString(); }
  2268. if(d && d.text) { tmp.text = d.text; }
  2269. if(d && d.data && d.data.jstree && d.data.jstree.icon) {
  2270. tmp.icon = d.data.jstree.icon;
  2271. }
  2272. if(tmp.icon === undefined || tmp.icon === null || tmp.icon === "") {
  2273. tmp.icon = true;
  2274. }
  2275. if(d && d.data) {
  2276. tmp.data = d.data;
  2277. if(d.data.jstree) {
  2278. for(i in d.data.jstree) {
  2279. if(d.data.jstree.hasOwnProperty(i)) {
  2280. tmp.state[i] = d.data.jstree[i];
  2281. }
  2282. }
  2283. }
  2284. }
  2285. if(d && typeof d.state === 'object') {
  2286. for (i in d.state) {
  2287. if(d.state.hasOwnProperty(i)) {
  2288. tmp.state[i] = d.state[i];
  2289. }
  2290. }
  2291. }
  2292. if(d && typeof d.li_attr === 'object') {
  2293. for (i in d.li_attr) {
  2294. if(d.li_attr.hasOwnProperty(i)) {
  2295. tmp.li_attr[i] = d.li_attr[i];
  2296. }
  2297. }
  2298. }
  2299. if(tmp.li_attr.id && !(tmp.id || tmp.id === 0)) {
  2300. tmp.id = tmp.li_attr.id.toString();
  2301. }
  2302. if(!(tmp.id || tmp.id === 0)) {
  2303. tmp.id = tid;
  2304. }
  2305. if(!tmp.li_attr.id) {
  2306. tmp.li_attr.id = tmp.id;
  2307. }
  2308. if(d && typeof d.a_attr === 'object') {
  2309. for (i in d.a_attr) {
  2310. if(d.a_attr.hasOwnProperty(i)) {
  2311. tmp.a_attr[i] = d.a_attr[i];
  2312. }
  2313. }
  2314. }
  2315. if(d && d.children && d.children.length) {
  2316. for(i = 0, j = d.children.length; i < j; i++) {
  2317. c = this._parse_model_from_json(d.children[i], tmp.id, ps);
  2318. e = m[c];
  2319. tmp.children.push(c);
  2320. if(e.children_d.length) {
  2321. tmp.children_d = tmp.children_d.concat(e.children_d);
  2322. }
  2323. }
  2324. tmp.children_d = tmp.children.concat(tmp.children_d);
  2325. }
  2326. if(d && d.children && d.children === true) {
  2327. tmp.state.loaded = false;
  2328. tmp.children = [];
  2329. tmp.children_d = [];
  2330. }
  2331. delete d.data;
  2332. delete d.children;
  2333. tmp.original = d;
  2334. m[tmp.id] = tmp;
  2335. if(tmp.state.selected) {
  2336. this._data.core.selected.push(tmp.id);
  2337. }
  2338. return tmp.id;
  2339. },
  2340. /**
  2341. * redraws all nodes that need to be redrawn. Used internally.
  2342. * @private
  2343. * @name _redraw()
  2344. * @trigger redraw.jstree
  2345. */
  2346. _redraw : function () {
  2347. var nodes = this._model.force_full_redraw ? this._model.data[$.jstree.root].children.concat([]) : this._model.changed.concat([]),
  2348. f = document.createElement('UL'), tmp, i, j, fe = this._data.core.focused;
  2349. for(i = 0, j = nodes.length; i < j; i++) {
  2350. tmp = this.redraw_node(nodes[i], true, this._model.force_full_redraw);
  2351. if(tmp && this._model.force_full_redraw) {
  2352. f.appendChild(tmp);
  2353. }
  2354. }
  2355. if(this._model.force_full_redraw) {
  2356. f.className = this.get_container_ul()[0].className;
  2357. f.setAttribute('role','presentation');
  2358. this.element.empty().append(f);
  2359. //this.get_container_ul()[0].appendChild(f);
  2360. }
  2361. if(fe !== null && this.settings.core.restore_focus) {
  2362. tmp = this.get_node(fe, true);
  2363. if(tmp && tmp.length && tmp.children('.jstree-anchor')[0] !== document.activeElement) {
  2364. tmp.children('.jstree-anchor').trigger('focus');
  2365. }
  2366. else {
  2367. this._data.core.focused = null;
  2368. }
  2369. }
  2370. this._model.force_full_redraw = false;
  2371. this._model.changed = [];
  2372. /**
  2373. * triggered after nodes are redrawn
  2374. * @event
  2375. * @name redraw.jstree
  2376. * @param {array} nodes the redrawn nodes
  2377. */
  2378. this.trigger('redraw', { "nodes" : nodes });
  2379. },
  2380. /**
  2381. * redraws all nodes that need to be redrawn or optionally - the whole tree
  2382. * @name redraw([full])
  2383. * @param {Boolean} full if set to `true` all nodes are redrawn.
  2384. */
  2385. redraw : function (full) {
  2386. if(full) {
  2387. this._model.force_full_redraw = true;
  2388. }
  2389. //if(this._model.redraw_timeout) {
  2390. // clearTimeout(this._model.redraw_timeout);
  2391. //}
  2392. //this._model.redraw_timeout = setTimeout($.proxy(this._redraw, this),0);
  2393. this._redraw();
  2394. },
  2395. /**
  2396. * redraws a single node's children. Used internally.
  2397. * @private
  2398. * @name draw_children(node)
  2399. * @param {mixed} node the node whose children will be redrawn
  2400. */
  2401. draw_children : function (node) {
  2402. var obj = this.get_node(node),
  2403. i = false,
  2404. j = false,
  2405. k = false,
  2406. d = document;
  2407. if(!obj) { return false; }
  2408. if(obj.id === $.jstree.root) { return this.redraw(true); }
  2409. node = this.get_node(node, true);
  2410. if(!node || !node.length) { return false; } // TODO: quick toggle
  2411. node.children('.jstree-children').remove();
  2412. node = node[0];
  2413. if(obj.children.length && obj.state.loaded) {
  2414. k = d.createElement('UL');
  2415. k.setAttribute('role', 'group');
  2416. k.className = 'jstree-children';
  2417. for(i = 0, j = obj.children.length; i < j; i++) {
  2418. k.appendChild(this.redraw_node(obj.children[i], true, true));
  2419. }
  2420. node.appendChild(k);
  2421. }
  2422. },
  2423. /**
  2424. * redraws a single node. Used internally.
  2425. * @private
  2426. * @name redraw_node(node, deep, is_callback, force_render)
  2427. * @param {mixed} node the node to redraw
  2428. * @param {Boolean} deep should child nodes be redrawn too
  2429. * @param {Boolean} is_callback is this a recursion call
  2430. * @param {Boolean} force_render should children of closed parents be drawn anyway
  2431. */
  2432. redraw_node : function (node, deep, is_callback, force_render) {
  2433. var obj = this.get_node(node),
  2434. par = false,
  2435. ind = false,
  2436. old = false,
  2437. i = false,
  2438. j = false,
  2439. k = false,
  2440. c = '',
  2441. d = document,
  2442. m = this._model.data,
  2443. f = false,
  2444. s = false,
  2445. tmp = null,
  2446. t = 0,
  2447. l = 0,
  2448. has_children = false,
  2449. last_sibling = false;
  2450. if(!obj) { return false; }
  2451. if(obj.id === $.jstree.root) { return this.redraw(true); }
  2452. deep = deep || obj.children.length === 0;
  2453. 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);
  2454. if(!node) {
  2455. deep = true;
  2456. //node = d.createElement('LI');
  2457. if(!is_callback) {
  2458. par = obj.parent !== $.jstree.root ? $('#' + obj.parent.replace($.jstree.idregex,'\\$&'), this.element)[0] : null;
  2459. if(par !== null && (!par || !m[obj.parent].state.opened)) {
  2460. return false;
  2461. }
  2462. ind = $.inArray(obj.id, par === null ? m[$.jstree.root].children : m[obj.parent].children);
  2463. }
  2464. }
  2465. else {
  2466. node = $(node);
  2467. if(!is_callback) {
  2468. par = node.parent().parent()[0];
  2469. if(par === this.element[0]) {
  2470. par = null;
  2471. }
  2472. ind = node.index();
  2473. }
  2474. // m[obj.id].data = node.data(); // use only node's data, no need to touch jquery storage
  2475. if(!deep && obj.children.length && !node.children('.jstree-children').length) {
  2476. deep = true;
  2477. }
  2478. if(!deep) {
  2479. old = node.children('.jstree-children')[0];
  2480. }
  2481. f = node.children('.jstree-anchor')[0] === document.activeElement;
  2482. node.remove();
  2483. //node = d.createElement('LI');
  2484. //node = node[0];
  2485. }
  2486. node = this._data.core.node.cloneNode(true);
  2487. // node is DOM, deep is boolean
  2488. c = 'jstree-node ';
  2489. for(i in obj.li_attr) {
  2490. if(obj.li_attr.hasOwnProperty(i)) {
  2491. if(i === 'id') { continue; }
  2492. if(i !== 'class') {
  2493. node.setAttribute(i, obj.li_attr[i]);
  2494. }
  2495. else {
  2496. c += obj.li_attr[i];
  2497. }
  2498. }
  2499. }
  2500. if(!obj.a_attr.id) {
  2501. obj.a_attr.id = obj.id + '_anchor';
  2502. }
  2503. node.childNodes[1].setAttribute('aria-selected', !!obj.state.selected);
  2504. node.childNodes[1].setAttribute('aria-level', obj.parents.length);
  2505. if(this.settings.core.compute_elements_positions) {
  2506. node.childNodes[1].setAttribute('aria-setsize', m[obj.parent].children.length);
  2507. node.childNodes[1].setAttribute('aria-posinset', m[obj.parent].children.indexOf(obj.id) + 1);
  2508. }
  2509. if(obj.state.disabled) {
  2510. node.childNodes[1].setAttribute('aria-disabled', true);
  2511. }
  2512. for(i = 0, j = obj.children.length; i < j; i++) {
  2513. if(!m[obj.children[i]].state.hidden) {
  2514. has_children = true;
  2515. break;
  2516. }
  2517. }
  2518. if(obj.parent !== null && m[obj.parent] && !obj.state.hidden) {
  2519. i = $.inArray(obj.id, m[obj.parent].children);
  2520. last_sibling = obj.id;
  2521. if(i !== -1) {
  2522. i++;
  2523. for(j = m[obj.parent].children.length; i < j; i++) {
  2524. if(!m[m[obj.parent].children[i]].state.hidden) {
  2525. last_sibling = m[obj.parent].children[i];
  2526. }
  2527. if(last_sibling !== obj.id) {
  2528. break;
  2529. }
  2530. }
  2531. }
  2532. }
  2533. if(obj.state.hidden) {
  2534. c += ' jstree-hidden';
  2535. }
  2536. if (obj.state.loading) {
  2537. c += ' jstree-loading';
  2538. }
  2539. if(obj.state.loaded && !has_children) {
  2540. c += ' jstree-leaf';
  2541. }
  2542. else {
  2543. c += obj.state.opened && obj.state.loaded ? ' jstree-open' : ' jstree-closed';
  2544. node.childNodes[1].setAttribute('aria-expanded', (obj.state.opened && obj.state.loaded) );
  2545. }
  2546. if(last_sibling === obj.id) {
  2547. c += ' jstree-last';
  2548. }
  2549. node.id = obj.id;
  2550. node.className = c;
  2551. c = ( obj.state.selected ? ' jstree-clicked' : '') + ( obj.state.disabled ? ' jstree-disabled' : '');
  2552. for(j in obj.a_attr) {
  2553. if(obj.a_attr.hasOwnProperty(j)) {
  2554. if(j === 'href' && obj.a_attr[j] === '#') { continue; }
  2555. if(j !== 'class') {
  2556. node.childNodes[1].setAttribute(j, obj.a_attr[j]);
  2557. }
  2558. else {
  2559. c += ' ' + obj.a_attr[j];
  2560. }
  2561. }
  2562. }
  2563. if(c.length) {
  2564. node.childNodes[1].className = 'jstree-anchor ' + c;
  2565. }
  2566. if((obj.icon && obj.icon !== true) || obj.icon === false) {
  2567. if(obj.icon === false) {
  2568. node.childNodes[1].childNodes[0].className += ' jstree-themeicon-hidden';
  2569. }
  2570. else if(obj.icon.indexOf('/') === -1 && obj.icon.indexOf('.') === -1) {
  2571. node.childNodes[1].childNodes[0].className += ' ' + obj.icon + ' jstree-themeicon-custom';
  2572. }
  2573. else {
  2574. node.childNodes[1].childNodes[0].style.backgroundImage = 'url("'+obj.icon+'")';
  2575. node.childNodes[1].childNodes[0].style.backgroundPosition = 'center center';
  2576. node.childNodes[1].childNodes[0].style.backgroundSize = 'auto';
  2577. node.childNodes[1].childNodes[0].className += ' jstree-themeicon-custom';
  2578. }
  2579. }
  2580. if(this.settings.core.force_text) {
  2581. node.childNodes[1].appendChild(d.createTextNode(obj.text));
  2582. }
  2583. else {
  2584. node.childNodes[1].innerHTML += obj.text;
  2585. }
  2586. if(deep && obj.children.length && (obj.state.opened || force_render) && obj.state.loaded) {
  2587. k = d.createElement('UL');
  2588. k.setAttribute('role', 'group');
  2589. k.className = 'jstree-children';
  2590. for(i = 0, j = obj.children.length; i < j; i++) {
  2591. k.appendChild(this.redraw_node(obj.children[i], deep, true));
  2592. }
  2593. node.appendChild(k);
  2594. }
  2595. if(old) {
  2596. node.appendChild(old);
  2597. }
  2598. if(!is_callback) {
  2599. // append back using par / ind
  2600. if(!par) {
  2601. par = this.element[0];
  2602. }
  2603. for(i = 0, j = par.childNodes.length; i < j; i++) {
  2604. if(par.childNodes[i] && par.childNodes[i].className && par.childNodes[i].className.indexOf('jstree-children') !== -1) {
  2605. tmp = par.childNodes[i];
  2606. break;
  2607. }
  2608. }
  2609. if(!tmp) {
  2610. tmp = d.createElement('UL');
  2611. tmp.setAttribute('role', 'group');
  2612. tmp.className = 'jstree-children';
  2613. par.appendChild(tmp);
  2614. }
  2615. par = tmp;
  2616. if(ind < par.childNodes.length) {
  2617. par.insertBefore(node, par.childNodes[ind]);
  2618. }
  2619. else {
  2620. par.appendChild(node);
  2621. }
  2622. if(f) {
  2623. t = this.element[0].scrollTop;
  2624. l = this.element[0].scrollLeft;
  2625. node.childNodes[1].focus();
  2626. this.element[0].scrollTop = t;
  2627. this.element[0].scrollLeft = l;
  2628. }
  2629. }
  2630. if(obj.state.opened && !obj.state.loaded) {
  2631. obj.state.opened = false;
  2632. setTimeout(function () {
  2633. this.open_node(obj.id, false, 0);
  2634. }.bind(this), 0);
  2635. }
  2636. return node;
  2637. },
  2638. /**
  2639. * opens a node, revealing its children. If the node is not loaded it will be loaded and opened once ready.
  2640. * @name open_node(obj [, callback, animation])
  2641. * @param {mixed} obj the node to open
  2642. * @param {Function} callback a function to execute once the node is opened
  2643. * @param {Number} animation the animation duration in milliseconds when opening the node (overrides the `core.animation` setting). Use `false` for no animation.
  2644. * @trigger open_node.jstree, after_open.jstree, before_open.jstree
  2645. */
  2646. open_node : function (obj, callback, animation) {
  2647. var t1, t2, d, t;
  2648. if($.vakata.is_array(obj)) {
  2649. obj = obj.slice();
  2650. for(t1 = 0, t2 = obj.length; t1 < t2; t1++) {
  2651. this.open_node(obj[t1], callback, animation);
  2652. }
  2653. return true;
  2654. }
  2655. obj = this.get_node(obj);
  2656. if(!obj || obj.id === $.jstree.root) {
  2657. return false;
  2658. }
  2659. animation = animation === undefined ? this.settings.core.animation : animation;
  2660. if(!this.is_closed(obj)) {
  2661. if(callback) {
  2662. callback.call(this, obj, false);
  2663. }
  2664. return false;
  2665. }
  2666. if(!this.is_loaded(obj)) {
  2667. if(this.is_loading(obj)) {
  2668. return setTimeout(function () {
  2669. this.open_node(obj, callback, animation);
  2670. }.bind(this), 500);
  2671. }
  2672. this.load_node(obj, function (o, ok) {
  2673. return ok ? this.open_node(o, callback, animation) : (callback ? callback.call(this, o, false) : false);
  2674. });
  2675. }
  2676. else {
  2677. d = this.get_node(obj, true);
  2678. t = this;
  2679. if(d.length) {
  2680. if(animation && d.children(".jstree-children").length) {
  2681. d.children(".jstree-children").stop(true, true);
  2682. }
  2683. if(obj.children.length && !this._firstChild(d.children('.jstree-children')[0])) {
  2684. this.draw_children(obj);
  2685. //d = this.get_node(obj, true);
  2686. }
  2687. if(!animation) {
  2688. this.trigger('before_open', { "node" : obj });
  2689. d[0].className = d[0].className.replace('jstree-closed', 'jstree-open');
  2690. d[0].childNodes[1].setAttribute("aria-expanded", true);
  2691. }
  2692. else {
  2693. this.trigger('before_open', { "node" : obj });
  2694. d
  2695. .children(".jstree-children").css("display","none").end()
  2696. .removeClass("jstree-closed").addClass("jstree-open")
  2697. .children('.jstree-anchor').attr("aria-expanded", true).end()
  2698. .children(".jstree-children").stop(true, true)
  2699. .slideDown(animation, function () {
  2700. this.style.display = "";
  2701. if (t.element) {
  2702. t.trigger("after_open", { "node" : obj });
  2703. }
  2704. });
  2705. }
  2706. }
  2707. obj.state.opened = true;
  2708. if(callback) {
  2709. callback.call(this, obj, true);
  2710. }
  2711. if(!d.length) {
  2712. /**
  2713. * 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)
  2714. * @event
  2715. * @name before_open.jstree
  2716. * @param {Object} node the opened node
  2717. */
  2718. this.trigger('before_open', { "node" : obj });
  2719. }
  2720. /**
  2721. * triggered when a node is opened (if there is an animation it will not be completed yet)
  2722. * @event
  2723. * @name open_node.jstree
  2724. * @param {Object} node the opened node
  2725. */
  2726. this.trigger('open_node', { "node" : obj });
  2727. if(!animation || !d.length) {
  2728. /**
  2729. * triggered when a node is opened and the animation is complete
  2730. * @event
  2731. * @name after_open.jstree
  2732. * @param {Object} node the opened node
  2733. */
  2734. this.trigger("after_open", { "node" : obj });
  2735. }
  2736. return true;
  2737. }
  2738. },
  2739. /**
  2740. * opens every parent of a node (node should be loaded)
  2741. * @name _open_to(obj)
  2742. * @param {mixed} obj the node to reveal
  2743. * @private
  2744. */
  2745. _open_to : function (obj) {
  2746. obj = this.get_node(obj);
  2747. if(!obj || obj.id === $.jstree.root) {
  2748. return false;
  2749. }
  2750. var i, j, p = obj.parents;
  2751. for(i = 0, j = p.length; i < j; i+=1) {
  2752. if(i !== $.jstree.root) {
  2753. this.open_node(p[i], false, 0);
  2754. }
  2755. }
  2756. return $('#' + obj.id.replace($.jstree.idregex,'\\$&'), this.element);
  2757. },
  2758. /**
  2759. * closes a node, hiding its children
  2760. * @name close_node(obj [, animation])
  2761. * @param {mixed} obj the node to close
  2762. * @param {Number} animation the animation duration in milliseconds when closing the node (overrides the `core.animation` setting). Use `false` for no animation.
  2763. * @trigger close_node.jstree, after_close.jstree
  2764. */
  2765. close_node : function (obj, animation) {
  2766. var t1, t2, t, d;
  2767. if($.vakata.is_array(obj)) {
  2768. obj = obj.slice();
  2769. for(t1 = 0, t2 = obj.length; t1 < t2; t1++) {
  2770. this.close_node(obj[t1], animation);
  2771. }
  2772. return true;
  2773. }
  2774. obj = this.get_node(obj);
  2775. if(!obj || obj.id === $.jstree.root) {
  2776. return false;
  2777. }
  2778. if(this.is_closed(obj)) {
  2779. return false;
  2780. }
  2781. animation = animation === undefined ? this.settings.core.animation : animation;
  2782. t = this;
  2783. d = this.get_node(obj, true);
  2784. obj.state.opened = false;
  2785. /**
  2786. * triggered when a node is closed (if there is an animation it will not be complete yet)
  2787. * @event
  2788. * @name close_node.jstree
  2789. * @param {Object} node the closed node
  2790. */
  2791. this.trigger('close_node',{ "node" : obj });
  2792. if(!d.length) {
  2793. /**
  2794. * triggered when a node is closed and the animation is complete
  2795. * @event
  2796. * @name after_close.jstree
  2797. * @param {Object} node the closed node
  2798. */
  2799. this.trigger("after_close", { "node" : obj });
  2800. }
  2801. else {
  2802. if(!animation) {
  2803. d[0].className = d[0].className.replace('jstree-open', 'jstree-closed');
  2804. d.children('.jstree-anchor').attr("aria-expanded", false);
  2805. d.children('.jstree-children').remove();
  2806. this.trigger("after_close", { "node" : obj });
  2807. }
  2808. else {
  2809. d
  2810. .children(".jstree-children").attr("style","display:block !important").end()
  2811. .removeClass("jstree-open").addClass("jstree-closed")
  2812. .children('.jstree-anchor').attr("aria-expanded", false).end()
  2813. .children(".jstree-children").stop(true, true).slideUp(animation, function () {
  2814. this.style.display = "";
  2815. d.children('.jstree-children').remove();
  2816. if (t.element) {
  2817. t.trigger("after_close", { "node" : obj });
  2818. }
  2819. });
  2820. }
  2821. }
  2822. },
  2823. /**
  2824. * toggles a node - closing it if it is open, opening it if it is closed
  2825. * @name toggle_node(obj)
  2826. * @param {mixed} obj the node to toggle
  2827. */
  2828. toggle_node : function (obj) {
  2829. var t1, t2;
  2830. if($.vakata.is_array(obj)) {
  2831. obj = obj.slice();
  2832. for(t1 = 0, t2 = obj.length; t1 < t2; t1++) {
  2833. this.toggle_node(obj[t1]);
  2834. }
  2835. return true;
  2836. }
  2837. if(this.is_closed(obj)) {
  2838. return this.open_node(obj);
  2839. }
  2840. if(this.is_open(obj)) {
  2841. return this.close_node(obj);
  2842. }
  2843. },
  2844. /**
  2845. * opens all nodes within a node (or the tree), revealing their children. If the node is not loaded it will be loaded and opened once ready.
  2846. * @name open_all([obj, animation, original_obj])
  2847. * @param {mixed} obj the node to open recursively, omit to open all nodes in the tree
  2848. * @param {Number} animation the animation duration in milliseconds when opening the nodes, the default is no animation
  2849. * @param {jQuery} reference to the node that started the process (internal use)
  2850. * @trigger open_all.jstree
  2851. */
  2852. open_all : function (obj, animation, original_obj) {
  2853. if(!obj) { obj = $.jstree.root; }
  2854. obj = this.get_node(obj);
  2855. if(!obj) { return false; }
  2856. var dom = obj.id === $.jstree.root ? this.get_container_ul() : this.get_node(obj, true), i, j, _this;
  2857. if(!dom.length) {
  2858. for(i = 0, j = obj.children_d.length; i < j; i++) {
  2859. if(this.is_closed(this._model.data[obj.children_d[i]])) {
  2860. this._model.data[obj.children_d[i]].state.opened = true;
  2861. }
  2862. }
  2863. return this.trigger('open_all', { "node" : obj });
  2864. }
  2865. original_obj = original_obj || dom;
  2866. _this = this;
  2867. dom = this.is_closed(obj) ? dom.find('.jstree-closed').addBack() : dom.find('.jstree-closed');
  2868. dom.each(function () {
  2869. _this.open_node(
  2870. this,
  2871. function(node, status) { if(status && this.is_parent(node)) { this.open_all(node, animation, original_obj); } },
  2872. animation || 0
  2873. );
  2874. });
  2875. if(original_obj.find('.jstree-closed').length === 0) {
  2876. /**
  2877. * triggered when an `open_all` call completes
  2878. * @event
  2879. * @name open_all.jstree
  2880. * @param {Object} node the opened node
  2881. */
  2882. this.trigger('open_all', { "node" : this.get_node(original_obj) });
  2883. }
  2884. },
  2885. /**
  2886. * closes all nodes within a node (or the tree), revealing their children
  2887. * @name close_all([obj, animation])
  2888. * @param {mixed} obj the node to close recursively, omit to close all nodes in the tree
  2889. * @param {Number} animation the animation duration in milliseconds when closing the nodes, the default is no animation
  2890. * @trigger close_all.jstree
  2891. */
  2892. close_all : function (obj, animation) {
  2893. if(!obj) { obj = $.jstree.root; }
  2894. obj = this.get_node(obj);
  2895. if(!obj) { return false; }
  2896. var dom = obj.id === $.jstree.root ? this.get_container_ul() : this.get_node(obj, true),
  2897. _this = this, i, j;
  2898. if(dom.length) {
  2899. dom = this.is_open(obj) ? dom.find('.jstree-open').addBack() : dom.find('.jstree-open');
  2900. $(dom.get().reverse()).each(function () { _this.close_node(this, animation || 0); });
  2901. }
  2902. for(i = 0, j = obj.children_d.length; i < j; i++) {
  2903. this._model.data[obj.children_d[i]].state.opened = false;
  2904. }
  2905. /**
  2906. * triggered when an `close_all` call completes
  2907. * @event
  2908. * @name close_all.jstree
  2909. * @param {Object} node the closed node
  2910. */
  2911. this.trigger('close_all', { "node" : obj });
  2912. },
  2913. /**
  2914. * checks if a node is disabled (not selectable)
  2915. * @name is_disabled(obj)
  2916. * @param {mixed} obj
  2917. * @return {Boolean}
  2918. */
  2919. is_disabled : function (obj) {
  2920. obj = this.get_node(obj);
  2921. return obj && obj.state && obj.state.disabled;
  2922. },
  2923. /**
  2924. * enables a node - so that it can be selected
  2925. * @name enable_node(obj)
  2926. * @param {mixed} obj the node to enable
  2927. * @trigger enable_node.jstree
  2928. */
  2929. enable_node : function (obj) {
  2930. var t1, t2;
  2931. if($.vakata.is_array(obj)) {
  2932. obj = obj.slice();
  2933. for(t1 = 0, t2 = obj.length; t1 < t2; t1++) {
  2934. this.enable_node(obj[t1]);
  2935. }
  2936. return true;
  2937. }
  2938. obj = this.get_node(obj);
  2939. if(!obj || obj.id === $.jstree.root) {
  2940. return false;
  2941. }
  2942. obj.state.disabled = false;
  2943. this.get_node(obj,true).children('.jstree-anchor').removeClass('jstree-disabled').attr('aria-disabled', false);
  2944. /**
  2945. * triggered when an node is enabled
  2946. * @event
  2947. * @name enable_node.jstree
  2948. * @param {Object} node the enabled node
  2949. */
  2950. this.trigger('enable_node', { 'node' : obj });
  2951. },
  2952. /**
  2953. * disables a node - so that it can not be selected
  2954. * @name disable_node(obj)
  2955. * @param {mixed} obj the node to disable
  2956. * @trigger disable_node.jstree
  2957. */
  2958. disable_node : function (obj) {
  2959. var t1, t2;
  2960. if($.vakata.is_array(obj)) {
  2961. obj = obj.slice();
  2962. for(t1 = 0, t2 = obj.length; t1 < t2; t1++) {
  2963. this.disable_node(obj[t1]);
  2964. }
  2965. return true;
  2966. }
  2967. obj = this.get_node(obj);
  2968. if(!obj || obj.id === $.jstree.root) {
  2969. return false;
  2970. }
  2971. obj.state.disabled = true;
  2972. this.get_node(obj,true).children('.jstree-anchor').addClass('jstree-disabled').attr('aria-disabled', true);
  2973. /**
  2974. * triggered when an node is disabled
  2975. * @event
  2976. * @name disable_node.jstree
  2977. * @param {Object} node the disabled node
  2978. */
  2979. this.trigger('disable_node', { 'node' : obj });
  2980. },
  2981. /**
  2982. * determines if a node is hidden
  2983. * @name is_hidden(obj)
  2984. * @param {mixed} obj the node
  2985. */
  2986. is_hidden : function (obj) {
  2987. obj = this.get_node(obj);
  2988. return obj.state.hidden === true;
  2989. },
  2990. /**
  2991. * hides a node - it is still in the structure but will not be visible
  2992. * @name hide_node(obj)
  2993. * @param {mixed} obj the node to hide
  2994. * @param {Boolean} skip_redraw internal parameter controlling if redraw is called
  2995. * @trigger hide_node.jstree
  2996. */
  2997. hide_node : function (obj, skip_redraw) {
  2998. var t1, t2;
  2999. if($.vakata.is_array(obj)) {
  3000. obj = obj.slice();
  3001. for(t1 = 0, t2 = obj.length; t1 < t2; t1++) {
  3002. this.hide_node(obj[t1], true);
  3003. }
  3004. if (!skip_redraw) {
  3005. this.redraw();
  3006. }
  3007. return true;
  3008. }
  3009. obj = this.get_node(obj);
  3010. if(!obj || obj.id === $.jstree.root) {
  3011. return false;
  3012. }
  3013. if(!obj.state.hidden) {
  3014. obj.state.hidden = true;
  3015. this._node_changed(obj.parent);
  3016. if(!skip_redraw) {
  3017. this.redraw();
  3018. }
  3019. /**
  3020. * triggered when an node is hidden
  3021. * @event
  3022. * @name hide_node.jstree
  3023. * @param {Object} node the hidden node
  3024. */
  3025. this.trigger('hide_node', { 'node' : obj });
  3026. }
  3027. },
  3028. /**
  3029. * shows a node
  3030. * @name show_node(obj)
  3031. * @param {mixed} obj the node to show
  3032. * @param {Boolean} skip_redraw internal parameter controlling if redraw is called
  3033. * @trigger show_node.jstree
  3034. */
  3035. show_node : function (obj, skip_redraw) {
  3036. var t1, t2;
  3037. if($.vakata.is_array(obj)) {
  3038. obj = obj.slice();
  3039. for(t1 = 0, t2 = obj.length; t1 < t2; t1++) {
  3040. this.show_node(obj[t1], true);
  3041. }
  3042. if (!skip_redraw) {
  3043. this.redraw();
  3044. }
  3045. return true;
  3046. }
  3047. obj = this.get_node(obj);
  3048. if(!obj || obj.id === $.jstree.root) {
  3049. return false;
  3050. }
  3051. if(obj.state.hidden) {
  3052. obj.state.hidden = false;
  3053. this._node_changed(obj.parent);
  3054. if(!skip_redraw) {
  3055. this.redraw();
  3056. }
  3057. /**
  3058. * triggered when an node is shown
  3059. * @event
  3060. * @name show_node.jstree
  3061. * @param {Object} node the shown node
  3062. */
  3063. this.trigger('show_node', { 'node' : obj });
  3064. }
  3065. },
  3066. /**
  3067. * hides all nodes
  3068. * @name hide_all()
  3069. * @trigger hide_all.jstree
  3070. */
  3071. hide_all : function (skip_redraw) {
  3072. var i, m = this._model.data, ids = [];
  3073. for(i in m) {
  3074. if(m.hasOwnProperty(i) && i !== $.jstree.root && !m[i].state.hidden) {
  3075. m[i].state.hidden = true;
  3076. ids.push(i);
  3077. }
  3078. }
  3079. this._model.force_full_redraw = true;
  3080. if(!skip_redraw) {
  3081. this.redraw();
  3082. }
  3083. /**
  3084. * triggered when all nodes are hidden
  3085. * @event
  3086. * @name hide_all.jstree
  3087. * @param {Array} nodes the IDs of all hidden nodes
  3088. */
  3089. this.trigger('hide_all', { 'nodes' : ids });
  3090. return ids;
  3091. },
  3092. /**
  3093. * shows all nodes
  3094. * @name show_all()
  3095. * @trigger show_all.jstree
  3096. */
  3097. show_all : function (skip_redraw) {
  3098. var i, m = this._model.data, ids = [];
  3099. for(i in m) {
  3100. if(m.hasOwnProperty(i) && i !== $.jstree.root && m[i].state.hidden) {
  3101. m[i].state.hidden = false;
  3102. ids.push(i);
  3103. }
  3104. }
  3105. this._model.force_full_redraw = true;
  3106. if(!skip_redraw) {
  3107. this.redraw();
  3108. }
  3109. /**
  3110. * triggered when all nodes are shown
  3111. * @event
  3112. * @name show_all.jstree
  3113. * @param {Array} nodes the IDs of all shown nodes
  3114. */
  3115. this.trigger('show_all', { 'nodes' : ids });
  3116. return ids;
  3117. },
  3118. /**
  3119. * called when a node is selected by the user. Used internally.
  3120. * @private
  3121. * @name activate_node(obj, e)
  3122. * @param {mixed} obj the node
  3123. * @param {Object} e the related event
  3124. * @trigger activate_node.jstree, changed.jstree
  3125. */
  3126. activate_node : function (obj, e) {
  3127. if(this.is_disabled(obj)) {
  3128. return false;
  3129. }
  3130. if(!e || typeof e !== 'object') {
  3131. e = {};
  3132. }
  3133. // 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
  3134. 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;
  3135. if(this._data.core.last_clicked && !this._data.core.last_clicked.state.selected) { this._data.core.last_clicked = null; }
  3136. 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]); }
  3137. 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 ) )) {
  3138. if(!this.settings.core.multiple && (e.metaKey || e.ctrlKey || e.shiftKey) && this.is_selected(obj)) {
  3139. this.deselect_node(obj, false, e);
  3140. }
  3141. else {
  3142. if (this.settings.core.allow_reselect || !this.is_selected(obj) || this._data.core.selected.length !== 1) {
  3143. this.deselect_all(true);
  3144. this.select_node(obj, false, false, e);
  3145. }
  3146. this._data.core.last_clicked = this.get_node(obj);
  3147. }
  3148. }
  3149. else {
  3150. if(e.shiftKey) {
  3151. var o = this.get_node(obj).id,
  3152. l = this._data.core.last_clicked.id,
  3153. p = this.get_node(this._data.core.last_clicked.parent).children,
  3154. c = false,
  3155. i, j;
  3156. for(i = 0, j = p.length; i < j; i += 1) {
  3157. // separate IFs work whem o and l are the same
  3158. if(p[i] === o) {
  3159. c = !c;
  3160. }
  3161. if(p[i] === l) {
  3162. c = !c;
  3163. }
  3164. if(!this.is_disabled(p[i]) && (c || p[i] === o || p[i] === l)) {
  3165. if (!this.is_hidden(p[i])) {
  3166. this.select_node(p[i], true, false, e);
  3167. }
  3168. }
  3169. else {
  3170. if (!e.ctrlKey) {
  3171. this.deselect_node(p[i], true, e);
  3172. }
  3173. }
  3174. }
  3175. this.trigger('changed', { 'action' : 'select_node', 'node' : this.get_node(obj), 'selected' : this._data.core.selected, 'event' : e });
  3176. }
  3177. else {
  3178. if(!this.is_selected(obj)) {
  3179. if (e.ctrlKey) {
  3180. this._data.core.last_clicked = this.get_node(obj);
  3181. }
  3182. this.select_node(obj, false, false, e);
  3183. }
  3184. else {
  3185. this.deselect_node(obj, false, e);
  3186. }
  3187. }
  3188. }
  3189. /**
  3190. * triggered when an node is clicked or intercated with by the user
  3191. * @event
  3192. * @name activate_node.jstree
  3193. * @param {Object} node
  3194. * @param {Object} event the ooriginal event (if any) which triggered the call (may be an empty object)
  3195. */
  3196. this.trigger('activate_node', { 'node' : this.get_node(obj), 'event' : e });
  3197. },
  3198. /**
  3199. * applies the hover state on a node, called when a node is hovered by the user. Used internally.
  3200. * @private
  3201. * @name hover_node(obj)
  3202. * @param {mixed} obj
  3203. * @trigger hover_node.jstree
  3204. */
  3205. hover_node : function (obj) {
  3206. obj = this.get_node(obj, true);
  3207. if(!obj || !obj.length || obj.children('.jstree-hovered').length) {
  3208. return false;
  3209. }
  3210. var o = this.element.find('.jstree-hovered'), t = this.element;
  3211. if(o && o.length) { this.dehover_node(o); }
  3212. obj.children('.jstree-anchor').addClass('jstree-hovered');
  3213. /**
  3214. * triggered when an node is hovered
  3215. * @event
  3216. * @name hover_node.jstree
  3217. * @param {Object} node
  3218. */
  3219. this.trigger('hover_node', { 'node' : this.get_node(obj) });
  3220. setTimeout(function () { t.attr('aria-activedescendant', obj[0].id); }, 0);
  3221. },
  3222. /**
  3223. * removes the hover state from a nodecalled when a node is no longer hovered by the user. Used internally.
  3224. * @private
  3225. * @name dehover_node(obj)
  3226. * @param {mixed} obj
  3227. * @trigger dehover_node.jstree
  3228. */
  3229. dehover_node : function (obj) {
  3230. obj = this.get_node(obj, true);
  3231. if(!obj || !obj.length || !obj.children('.jstree-hovered').length) {
  3232. return false;
  3233. }
  3234. obj.children('.jstree-anchor').removeClass('jstree-hovered');
  3235. /**
  3236. * triggered when an node is no longer hovered
  3237. * @event
  3238. * @name dehover_node.jstree
  3239. * @param {Object} node
  3240. */
  3241. this.trigger('dehover_node', { 'node' : this.get_node(obj) });
  3242. },
  3243. /**
  3244. * select a node
  3245. * @name select_node(obj [, supress_event, prevent_open])
  3246. * @param {mixed} obj an array can be used to select multiple nodes
  3247. * @param {Boolean} supress_event if set to `true` the `changed.jstree` event won't be triggered
  3248. * @param {Boolean} prevent_open if set to `true` parents of the selected node won't be opened
  3249. * @trigger select_node.jstree, changed.jstree
  3250. */
  3251. select_node : function (obj, supress_event, prevent_open, e) {
  3252. var dom, t1, t2, th;
  3253. if($.vakata.is_array(obj)) {
  3254. obj = obj.slice();
  3255. for(t1 = 0, t2 = obj.length; t1 < t2; t1++) {
  3256. this.select_node(obj[t1], supress_event, prevent_open, e);
  3257. }
  3258. return true;
  3259. }
  3260. obj = this.get_node(obj);
  3261. if(!obj || obj.id === $.jstree.root) {
  3262. return false;
  3263. }
  3264. dom = this.get_node(obj, true);
  3265. if(!obj.state.selected) {
  3266. obj.state.selected = true;
  3267. this._data.core.selected.push(obj.id);
  3268. if(!prevent_open) {
  3269. dom = this._open_to(obj);
  3270. }
  3271. if(dom && dom.length) {
  3272. dom.children('.jstree-anchor').addClass('jstree-clicked').attr('aria-selected', true);
  3273. }
  3274. /**
  3275. * triggered when an node is selected
  3276. * @event
  3277. * @name select_node.jstree
  3278. * @param {Object} node
  3279. * @param {Array} selected the current selection
  3280. * @param {Object} event the event (if any) that triggered this select_node
  3281. */
  3282. this.trigger('select_node', { 'node' : obj, 'selected' : this._data.core.selected, 'event' : e });
  3283. if(!supress_event) {
  3284. /**
  3285. * triggered when selection changes
  3286. * @event
  3287. * @name changed.jstree
  3288. * @param {Object} node
  3289. * @param {Object} action the action that caused the selection to change
  3290. * @param {Array} selected the current selection
  3291. * @param {Object} event the event (if any) that triggered this changed event
  3292. */
  3293. this.trigger('changed', { 'action' : 'select_node', 'node' : obj, 'selected' : this._data.core.selected, 'event' : e });
  3294. }
  3295. }
  3296. },
  3297. /**
  3298. * deselect a node
  3299. * @name deselect_node(obj [, supress_event])
  3300. * @param {mixed} obj an array can be used to deselect multiple nodes
  3301. * @param {Boolean} supress_event if set to `true` the `changed.jstree` event won't be triggered
  3302. * @trigger deselect_node.jstree, changed.jstree
  3303. */
  3304. deselect_node : function (obj, supress_event, e) {
  3305. var t1, t2, dom;
  3306. if($.vakata.is_array(obj)) {
  3307. obj = obj.slice();
  3308. for(t1 = 0, t2 = obj.length; t1 < t2; t1++) {
  3309. this.deselect_node(obj[t1], supress_event, e);
  3310. }
  3311. return true;
  3312. }
  3313. obj = this.get_node(obj);
  3314. if(!obj || obj.id === $.jstree.root) {
  3315. return false;
  3316. }
  3317. dom = this.get_node(obj, true);
  3318. if(obj.state.selected) {
  3319. obj.state.selected = false;
  3320. this._data.core.selected = $.vakata.array_remove_item(this._data.core.selected, obj.id);
  3321. if(dom.length) {
  3322. dom.children('.jstree-anchor').removeClass('jstree-clicked').attr('aria-selected', false);
  3323. }
  3324. /**
  3325. * triggered when an node is deselected
  3326. * @event
  3327. * @name deselect_node.jstree
  3328. * @param {Object} node
  3329. * @param {Array} selected the current selection
  3330. * @param {Object} event the event (if any) that triggered this deselect_node
  3331. */
  3332. this.trigger('deselect_node', { 'node' : obj, 'selected' : this._data.core.selected, 'event' : e });
  3333. if(!supress_event) {
  3334. this.trigger('changed', { 'action' : 'deselect_node', 'node' : obj, 'selected' : this._data.core.selected, 'event' : e });
  3335. }
  3336. }
  3337. },
  3338. /**
  3339. * select all nodes in the tree
  3340. * @name select_all([supress_event])
  3341. * @param {Boolean} supress_event if set to `true` the `changed.jstree` event won't be triggered
  3342. * @trigger select_all.jstree, changed.jstree
  3343. */
  3344. select_all : function (supress_event) {
  3345. var tmp = this._data.core.selected.concat([]), i, j;
  3346. this._data.core.selected = this._model.data[$.jstree.root].children_d.concat();
  3347. for(i = 0, j = this._data.core.selected.length; i < j; i++) {
  3348. if(this._model.data[this._data.core.selected[i]]) {
  3349. this._model.data[this._data.core.selected[i]].state.selected = true;
  3350. }
  3351. }
  3352. this.redraw(true);
  3353. /**
  3354. * triggered when all nodes are selected
  3355. * @event
  3356. * @name select_all.jstree
  3357. * @param {Array} selected the current selection
  3358. */
  3359. this.trigger('select_all', { 'selected' : this._data.core.selected });
  3360. if(!supress_event) {
  3361. this.trigger('changed', { 'action' : 'select_all', 'selected' : this._data.core.selected, 'old_selection' : tmp });
  3362. }
  3363. },
  3364. /**
  3365. * deselect all selected nodes
  3366. * @name deselect_all([supress_event])
  3367. * @param {Boolean} supress_event if set to `true` the `changed.jstree` event won't be triggered
  3368. * @trigger deselect_all.jstree, changed.jstree
  3369. */
  3370. deselect_all : function (supress_event) {
  3371. var tmp = this._data.core.selected.concat([]), i, j;
  3372. for(i = 0, j = this._data.core.selected.length; i < j; i++) {
  3373. if(this._model.data[this._data.core.selected[i]]) {
  3374. this._model.data[this._data.core.selected[i]].state.selected = false;
  3375. }
  3376. }
  3377. this._data.core.selected = [];
  3378. this.element.find('.jstree-clicked').removeClass('jstree-clicked').attr('aria-selected', false);
  3379. /**
  3380. * triggered when all nodes are deselected
  3381. * @event
  3382. * @name deselect_all.jstree
  3383. * @param {Object} node the previous selection
  3384. * @param {Array} selected the current selection
  3385. */
  3386. this.trigger('deselect_all', { 'selected' : this._data.core.selected, 'node' : tmp });
  3387. if(!supress_event) {
  3388. this.trigger('changed', { 'action' : 'deselect_all', 'selected' : this._data.core.selected, 'old_selection' : tmp });
  3389. }
  3390. },
  3391. /**
  3392. * checks if a node is selected
  3393. * @name is_selected(obj)
  3394. * @param {mixed} obj
  3395. * @return {Boolean}
  3396. */
  3397. is_selected : function (obj) {
  3398. obj = this.get_node(obj);
  3399. if(!obj || obj.id === $.jstree.root) {
  3400. return false;
  3401. }
  3402. return obj.state.selected;
  3403. },
  3404. /**
  3405. * get an array of all selected nodes
  3406. * @name get_selected([full])
  3407. * @param {mixed} full if set to `true` the returned array will consist of the full node objects, otherwise - only IDs will be returned
  3408. * @return {Array}
  3409. */
  3410. get_selected : function (full) {
  3411. return full ? $.map(this._data.core.selected, function (i) { return this.get_node(i); }.bind(this)) : this._data.core.selected.slice();
  3412. },
  3413. /**
  3414. * get an array of all top level selected nodes (ignoring children of selected nodes)
  3415. * @name get_top_selected([full])
  3416. * @param {mixed} full if set to `true` the returned array will consist of the full node objects, otherwise - only IDs will be returned
  3417. * @return {Array}
  3418. */
  3419. get_top_selected : function (full) {
  3420. var tmp = this.get_selected(true),
  3421. obj = {}, i, j, k, l;
  3422. for(i = 0, j = tmp.length; i < j; i++) {
  3423. obj[tmp[i].id] = tmp[i];
  3424. }
  3425. for(i = 0, j = tmp.length; i < j; i++) {
  3426. for(k = 0, l = tmp[i].children_d.length; k < l; k++) {
  3427. if(obj[tmp[i].children_d[k]]) {
  3428. delete obj[tmp[i].children_d[k]];
  3429. }
  3430. }
  3431. }
  3432. tmp = [];
  3433. for(i in obj) {
  3434. if(obj.hasOwnProperty(i)) {
  3435. tmp.push(i);
  3436. }
  3437. }
  3438. return full ? $.map(tmp, function (i) { return this.get_node(i); }.bind(this)) : tmp;
  3439. },
  3440. /**
  3441. * get an array of all bottom level selected nodes (ignoring selected parents)
  3442. * @name get_bottom_selected([full])
  3443. * @param {mixed} full if set to `true` the returned array will consist of the full node objects, otherwise - only IDs will be returned
  3444. * @return {Array}
  3445. */
  3446. get_bottom_selected : function (full) {
  3447. var tmp = this.get_selected(true),
  3448. obj = [], i, j;
  3449. for(i = 0, j = tmp.length; i < j; i++) {
  3450. if(!tmp[i].children.length) {
  3451. obj.push(tmp[i].id);
  3452. }
  3453. }
  3454. return full ? $.map(obj, function (i) { return this.get_node(i); }.bind(this)) : obj;
  3455. },
  3456. /**
  3457. * gets the current state of the tree so that it can be restored later with `set_state(state)`. Used internally.
  3458. * @name get_state()
  3459. * @private
  3460. * @return {Object}
  3461. */
  3462. get_state : function () {
  3463. var state = {
  3464. 'core' : {
  3465. 'open' : [],
  3466. 'loaded' : [],
  3467. 'scroll' : {
  3468. 'left' : this.element.scrollLeft(),
  3469. 'top' : this.element.scrollTop()
  3470. },
  3471. /*!
  3472. 'themes' : {
  3473. 'name' : this.get_theme(),
  3474. 'icons' : this._data.core.themes.icons,
  3475. 'dots' : this._data.core.themes.dots
  3476. },
  3477. */
  3478. 'selected' : []
  3479. }
  3480. }, i;
  3481. for(i in this._model.data) {
  3482. if(this._model.data.hasOwnProperty(i)) {
  3483. if(i !== $.jstree.root) {
  3484. if(this._model.data[i].state.loaded && this.settings.core.loaded_state) {
  3485. state.core.loaded.push(i);
  3486. }
  3487. if(this._model.data[i].state.opened) {
  3488. state.core.open.push(i);
  3489. }
  3490. if(this._model.data[i].state.selected) {
  3491. state.core.selected.push(i);
  3492. }
  3493. }
  3494. }
  3495. }
  3496. return state;
  3497. },
  3498. /**
  3499. * sets the state of the tree. Used internally.
  3500. * @name set_state(state [, callback])
  3501. * @private
  3502. * @param {Object} state the state to restore. Keep in mind this object is passed by reference and jstree will modify it.
  3503. * @param {Function} callback an optional function to execute once the state is restored.
  3504. * @trigger set_state.jstree
  3505. */
  3506. set_state : function (state, callback) {
  3507. if(state) {
  3508. if(state.core && state.core.selected && state.core.initial_selection === undefined) {
  3509. state.core.initial_selection = this._data.core.selected.concat([]).sort().join(',');
  3510. }
  3511. if(state.core) {
  3512. var res, n, t, _this, i;
  3513. if(state.core.loaded) {
  3514. if(!this.settings.core.loaded_state || !$.vakata.is_array(state.core.loaded) || !state.core.loaded.length) {
  3515. delete state.core.loaded;
  3516. this.set_state(state, callback);
  3517. }
  3518. else {
  3519. this._load_nodes(state.core.loaded, function (nodes) {
  3520. delete state.core.loaded;
  3521. this.set_state(state, callback);
  3522. });
  3523. }
  3524. return false;
  3525. }
  3526. if(state.core.open) {
  3527. if(!$.vakata.is_array(state.core.open) || !state.core.open.length) {
  3528. delete state.core.open;
  3529. this.set_state(state, callback);
  3530. }
  3531. else {
  3532. this._load_nodes(state.core.open, function (nodes) {
  3533. this.open_node(nodes, false, 0);
  3534. delete state.core.open;
  3535. this.set_state(state, callback);
  3536. });
  3537. }
  3538. return false;
  3539. }
  3540. if(state.core.scroll) {
  3541. if(state.core.scroll && state.core.scroll.left !== undefined) {
  3542. this.element.scrollLeft(state.core.scroll.left);
  3543. }
  3544. if(state.core.scroll && state.core.scroll.top !== undefined) {
  3545. this.element.scrollTop(state.core.scroll.top);
  3546. }
  3547. delete state.core.scroll;
  3548. this.set_state(state, callback);
  3549. return false;
  3550. }
  3551. if(state.core.selected) {
  3552. _this = this;
  3553. if (state.core.initial_selection === undefined ||
  3554. state.core.initial_selection === this._data.core.selected.concat([]).sort().join(',')
  3555. ) {
  3556. this.deselect_all();
  3557. $.each(state.core.selected, function (i, v) {
  3558. _this.select_node(v, false, true);
  3559. });
  3560. }
  3561. delete state.core.initial_selection;
  3562. delete state.core.selected;
  3563. this.set_state(state, callback);
  3564. return false;
  3565. }
  3566. for(i in state) {
  3567. if(state.hasOwnProperty(i) && i !== "core" && $.inArray(i, this.settings.plugins) === -1) {
  3568. delete state[i];
  3569. }
  3570. }
  3571. if($.isEmptyObject(state.core)) {
  3572. delete state.core;
  3573. this.set_state(state, callback);
  3574. return false;
  3575. }
  3576. }
  3577. if($.isEmptyObject(state)) {
  3578. state = null;
  3579. if(callback) { callback.call(this); }
  3580. /**
  3581. * triggered when a `set_state` call completes
  3582. * @event
  3583. * @name set_state.jstree
  3584. */
  3585. this.trigger('set_state');
  3586. return false;
  3587. }
  3588. return true;
  3589. }
  3590. return false;
  3591. },
  3592. /**
  3593. * refreshes the tree - all nodes are reloaded with calls to `load_node`.
  3594. * @name refresh()
  3595. * @param {Boolean} skip_loading an option to skip showing the loading indicator
  3596. * @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
  3597. * @trigger refresh.jstree
  3598. */
  3599. refresh : function (skip_loading, forget_state) {
  3600. this._data.core.state = forget_state === true ? {} : this.get_state();
  3601. if(forget_state && $.vakata.is_function(forget_state)) { this._data.core.state = forget_state.call(this, this._data.core.state); }
  3602. this._cnt = 0;
  3603. this._model.data = {};
  3604. this._model.data[$.jstree.root] = {
  3605. id : $.jstree.root,
  3606. parent : null,
  3607. parents : [],
  3608. children : [],
  3609. children_d : [],
  3610. state : { loaded : false }
  3611. };
  3612. this._data.core.selected = [];
  3613. this._data.core.last_clicked = null;
  3614. this._data.core.focused = null;
  3615. var c = this.get_container_ul()[0].className;
  3616. if(!skip_loading) {
  3617. this.element.html("<"+"ul class='"+c+"' role='group'><"+"li class='jstree-initial-node jstree-loading jstree-leaf jstree-last' role='none' id='j"+this._id+"_loading'><i class='jstree-icon jstree-ocl'></i><"+"a class='jstree-anchor' role='treeitem' href='#'><i class='jstree-icon jstree-themeicon-hidden'></i>" + this.get_string("Loading ...") + "</a></li></ul>");
  3618. this.element.attr('aria-activedescendant','j'+this._id+'_loading');
  3619. }
  3620. this.load_node($.jstree.root, function (o, s) {
  3621. if(s) {
  3622. this.get_container_ul()[0].className = c;
  3623. if(this._firstChild(this.get_container_ul()[0])) {
  3624. this.element.attr('aria-activedescendant',this._firstChild(this.get_container_ul()[0]).id);
  3625. }
  3626. this.set_state($.extend(true, {}, this._data.core.state), function () {
  3627. /**
  3628. * triggered when a `refresh` call completes
  3629. * @event
  3630. * @name refresh.jstree
  3631. */
  3632. this.trigger('refresh');
  3633. });
  3634. }
  3635. this._data.core.state = null;
  3636. });
  3637. },
  3638. /**
  3639. * refreshes a node in the tree (reload its children) all opened nodes inside that node are reloaded with calls to `load_node`.
  3640. * @name refresh_node(obj)
  3641. * @param {mixed} obj the node
  3642. * @trigger refresh_node.jstree
  3643. */
  3644. refresh_node : function (obj) {
  3645. obj = this.get_node(obj);
  3646. if(!obj || obj.id === $.jstree.root) { return false; }
  3647. var opened = [], to_load = [], s = this._data.core.selected.concat([]);
  3648. to_load.push(obj.id);
  3649. if(obj.state.opened === true) { opened.push(obj.id); }
  3650. this.get_node(obj, true).find('.jstree-open').each(function() { to_load.push(this.id); opened.push(this.id); });
  3651. this._load_nodes(to_load, function (nodes) {
  3652. this.open_node(opened, false, 0);
  3653. this.select_node(s);
  3654. /**
  3655. * triggered when a node is refreshed
  3656. * @event
  3657. * @name refresh_node.jstree
  3658. * @param {Object} node - the refreshed node
  3659. * @param {Array} nodes - an array of the IDs of the nodes that were reloaded
  3660. */
  3661. this.trigger('refresh_node', { 'node' : obj, 'nodes' : nodes });
  3662. }.bind(this), false, true);
  3663. },
  3664. /**
  3665. * set (change) the ID of a node
  3666. * @name set_id(obj, id)
  3667. * @param {mixed} obj the node
  3668. * @param {String} id the new ID
  3669. * @return {Boolean}
  3670. * @trigger set_id.jstree
  3671. */
  3672. set_id : function (obj, id) {
  3673. obj = this.get_node(obj);
  3674. if(!obj || obj.id === $.jstree.root) { return false; }
  3675. var i, j, m = this._model.data, old = obj.id;
  3676. id = id.toString();
  3677. // update parents (replace current ID with new one in children and children_d)
  3678. m[obj.parent].children[$.inArray(obj.id, m[obj.parent].children)] = id;
  3679. for(i = 0, j = obj.parents.length; i < j; i++) {
  3680. m[obj.parents[i]].children_d[$.inArray(obj.id, m[obj.parents[i]].children_d)] = id;
  3681. }
  3682. // update children (replace current ID with new one in parent and parents)
  3683. for(i = 0, j = obj.children.length; i < j; i++) {
  3684. m[obj.children[i]].parent = id;
  3685. }
  3686. for(i = 0, j = obj.children_d.length; i < j; i++) {
  3687. m[obj.children_d[i]].parents[$.inArray(obj.id, m[obj.children_d[i]].parents)] = id;
  3688. }
  3689. i = $.inArray(obj.id, this._data.core.selected);
  3690. if(i !== -1) { this._data.core.selected[i] = id; }
  3691. // update model and obj itself (obj.id, this._model.data[KEY])
  3692. i = this.get_node(obj.id, true);
  3693. if(i) {
  3694. i.attr('id', id); //.children('.jstree-anchor').attr('id', id + '_anchor').end().attr('aria-labelledby', id + '_anchor');
  3695. if(this.element.attr('aria-activedescendant') === obj.id) {
  3696. this.element.attr('aria-activedescendant', id);
  3697. }
  3698. }
  3699. delete m[obj.id];
  3700. obj.id = id;
  3701. obj.li_attr.id = id;
  3702. m[id] = obj;
  3703. /**
  3704. * triggered when a node id value is changed
  3705. * @event
  3706. * @name set_id.jstree
  3707. * @param {Object} node
  3708. * @param {String} old the old id
  3709. */
  3710. this.trigger('set_id',{ "node" : obj, "new" : obj.id, "old" : old });
  3711. return true;
  3712. },
  3713. /**
  3714. * get the text value of a node
  3715. * @name get_text(obj)
  3716. * @param {mixed} obj the node
  3717. * @return {String}
  3718. */
  3719. get_text : function (obj) {
  3720. obj = this.get_node(obj);
  3721. return (!obj || obj.id === $.jstree.root) ? false : obj.text;
  3722. },
  3723. /**
  3724. * set the text value of a node. Used internally, please use `rename_node(obj, val)`.
  3725. * @private
  3726. * @name set_text(obj, val)
  3727. * @param {mixed} obj the node, you can pass an array to set the text on multiple nodes
  3728. * @param {String} val the new text value
  3729. * @return {Boolean}
  3730. * @trigger set_text.jstree
  3731. */
  3732. set_text : function (obj, val) {
  3733. var t1, t2;
  3734. if($.vakata.is_array(obj)) {
  3735. obj = obj.slice();
  3736. for(t1 = 0, t2 = obj.length; t1 < t2; t1++) {
  3737. this.set_text(obj[t1], val);
  3738. }
  3739. return true;
  3740. }
  3741. obj = this.get_node(obj);
  3742. if(!obj || obj.id === $.jstree.root) { return false; }
  3743. obj.text = val;
  3744. if(this.get_node(obj, true).length) {
  3745. this.redraw_node(obj.id);
  3746. }
  3747. /**
  3748. * triggered when a node text value is changed
  3749. * @event
  3750. * @name set_text.jstree
  3751. * @param {Object} obj
  3752. * @param {String} text the new value
  3753. */
  3754. this.trigger('set_text',{ "obj" : obj, "text" : val });
  3755. return true;
  3756. },
  3757. /**
  3758. * gets a JSON representation of a node (or the whole tree)
  3759. * @name get_json([obj, options])
  3760. * @param {mixed} obj
  3761. * @param {Object} options
  3762. * @param {Boolean} options.no_state do not return state information
  3763. * @param {Boolean} options.no_id do not return ID
  3764. * @param {Boolean} options.no_children do not include children
  3765. * @param {Boolean} options.no_data do not include node data
  3766. * @param {Boolean} options.no_li_attr do not include LI attributes
  3767. * @param {Boolean} options.no_a_attr do not include A attributes
  3768. * @param {Boolean} options.flat return flat JSON instead of nested
  3769. * @return {Object}
  3770. */
  3771. get_json : function (obj, options, flat) {
  3772. obj = this.get_node(obj || $.jstree.root);
  3773. if(!obj) { return false; }
  3774. if(options && options.flat && !flat) { flat = []; }
  3775. var tmp = {
  3776. 'id' : obj.id,
  3777. 'text' : obj.text,
  3778. 'icon' : this.get_icon(obj),
  3779. 'li_attr' : $.extend(true, {}, obj.li_attr),
  3780. 'a_attr' : $.extend(true, {}, obj.a_attr),
  3781. 'state' : {},
  3782. 'data' : options && options.no_data ? false : $.extend(true, $.vakata.is_array(obj.data)?[]:{}, obj.data)
  3783. //( this.get_node(obj, true).length ? this.get_node(obj, true).data() : obj.data ),
  3784. }, i, j;
  3785. if(options && options.flat) {
  3786. tmp.parent = obj.parent;
  3787. }
  3788. else {
  3789. tmp.children = [];
  3790. }
  3791. if(!options || !options.no_state) {
  3792. for(i in obj.state) {
  3793. if(obj.state.hasOwnProperty(i)) {
  3794. tmp.state[i] = obj.state[i];
  3795. }
  3796. }
  3797. } else {
  3798. delete tmp.state;
  3799. }
  3800. if(options && options.no_li_attr) {
  3801. delete tmp.li_attr;
  3802. }
  3803. if(options && options.no_a_attr) {
  3804. delete tmp.a_attr;
  3805. }
  3806. if(options && options.no_id) {
  3807. delete tmp.id;
  3808. if(tmp.li_attr && tmp.li_attr.id) {
  3809. delete tmp.li_attr.id;
  3810. }
  3811. if(tmp.a_attr && tmp.a_attr.id) {
  3812. delete tmp.a_attr.id;
  3813. }
  3814. }
  3815. if(options && options.flat && obj.id !== $.jstree.root) {
  3816. flat.push(tmp);
  3817. }
  3818. if(!options || !options.no_children) {
  3819. for(i = 0, j = obj.children.length; i < j; i++) {
  3820. if(options && options.flat) {
  3821. this.get_json(obj.children[i], options, flat);
  3822. }
  3823. else {
  3824. tmp.children.push(this.get_json(obj.children[i], options));
  3825. }
  3826. }
  3827. }
  3828. return options && options.flat ? flat : (obj.id === $.jstree.root ? tmp.children : tmp);
  3829. },
  3830. /**
  3831. * create a new node (do not confuse with load_node)
  3832. * @name create_node([par, node, pos, callback, is_loaded])
  3833. * @param {mixed} par the parent node (to create a root node use either "#" (string) or `null`)
  3834. * @param {mixed} node the data for the new node (a valid JSON object, or a simple string with the name)
  3835. * @param {mixed} pos the index at which to insert the node, "first" and "last" are also supported, default is "last"
  3836. * @param {Function} callback a function to be called once the node is created
  3837. * @param {Boolean} is_loaded internal argument indicating if the parent node was succesfully loaded
  3838. * @return {String} the ID of the newly create node
  3839. * @trigger model.jstree, create_node.jstree
  3840. */
  3841. create_node : function (par, node, pos, callback, is_loaded) {
  3842. if(par === null) { par = $.jstree.root; }
  3843. par = this.get_node(par);
  3844. if(!par) { return false; }
  3845. pos = pos === undefined ? "last" : pos;
  3846. if(!pos.toString().match(/^(before|after)$/) && !is_loaded && !this.is_loaded(par)) {
  3847. return this.load_node(par, function () { this.create_node(par, node, pos, callback, true); });
  3848. }
  3849. if(!node) { node = { "text" : this.get_string('New node') }; }
  3850. if(typeof node === "string") {
  3851. node = { "text" : node };
  3852. } else {
  3853. node = $.extend(true, {}, node);
  3854. }
  3855. if(node.text === undefined) { node.text = this.get_string('New node'); }
  3856. var tmp, dpc, i, j;
  3857. if(par.id === $.jstree.root) {
  3858. if(pos === "before") { pos = "first"; }
  3859. if(pos === "after") { pos = "last"; }
  3860. }
  3861. switch(pos) {
  3862. case "before":
  3863. tmp = this.get_node(par.parent);
  3864. pos = $.inArray(par.id, tmp.children);
  3865. par = tmp;
  3866. break;
  3867. case "after" :
  3868. tmp = this.get_node(par.parent);
  3869. pos = $.inArray(par.id, tmp.children) + 1;
  3870. par = tmp;
  3871. break;
  3872. case "inside":
  3873. case "first":
  3874. pos = 0;
  3875. break;
  3876. case "last":
  3877. pos = par.children.length;
  3878. break;
  3879. default:
  3880. if(!pos) { pos = 0; }
  3881. break;
  3882. }
  3883. if(pos > par.children.length) { pos = par.children.length; }
  3884. if(node.id === undefined) { node.id = true; }
  3885. if(!this.check("create_node", node, par, pos)) {
  3886. this.settings.core.error.call(this, this._data.core.last_error);
  3887. return false;
  3888. }
  3889. if(node.id === true) { delete node.id; }
  3890. node = this._parse_model_from_json(node, par.id, par.parents.concat());
  3891. if(!node) { return false; }
  3892. tmp = this.get_node(node);
  3893. dpc = [];
  3894. dpc.push(node);
  3895. dpc = dpc.concat(tmp.children_d);
  3896. this.trigger('model', { "nodes" : dpc, "parent" : par.id });
  3897. par.children_d = par.children_d.concat(dpc);
  3898. for(i = 0, j = par.parents.length; i < j; i++) {
  3899. this._model.data[par.parents[i]].children_d = this._model.data[par.parents[i]].children_d.concat(dpc);
  3900. }
  3901. node = tmp;
  3902. tmp = [];
  3903. for(i = 0, j = par.children.length; i < j; i++) {
  3904. tmp[i >= pos ? i+1 : i] = par.children[i];
  3905. }
  3906. tmp[pos] = node.id;
  3907. par.children = tmp;
  3908. this.redraw_node(par, true);
  3909. /**
  3910. * triggered when a node is created
  3911. * @event
  3912. * @name create_node.jstree
  3913. * @param {Object} node
  3914. * @param {String} parent the parent's ID
  3915. * @param {Number} position the position of the new node among the parent's children
  3916. */
  3917. this.trigger('create_node', { "node" : this.get_node(node), "parent" : par.id, "position" : pos });
  3918. if(callback) { callback.call(this, this.get_node(node)); }
  3919. return node.id;
  3920. },
  3921. /**
  3922. * set the text value of a node
  3923. * @name rename_node(obj, val)
  3924. * @param {mixed} obj the node, you can pass an array to rename multiple nodes to the same name
  3925. * @param {String} val the new text value
  3926. * @return {Boolean}
  3927. * @trigger rename_node.jstree
  3928. */
  3929. rename_node : function (obj, val) {
  3930. var t1, t2, old;
  3931. if($.vakata.is_array(obj)) {
  3932. obj = obj.slice();
  3933. for(t1 = 0, t2 = obj.length; t1 < t2; t1++) {
  3934. this.rename_node(obj[t1], val);
  3935. }
  3936. return true;
  3937. }
  3938. obj = this.get_node(obj);
  3939. if(!obj || obj.id === $.jstree.root) { return false; }
  3940. old = obj.text;
  3941. if(!this.check("rename_node", obj, this.get_parent(obj), val)) {
  3942. this.settings.core.error.call(this, this._data.core.last_error);
  3943. return false;
  3944. }
  3945. this.set_text(obj, val); // .apply(this, Array.prototype.slice.call(arguments))
  3946. /**
  3947. * triggered when a node is renamed
  3948. * @event
  3949. * @name rename_node.jstree
  3950. * @param {Object} node
  3951. * @param {String} text the new value
  3952. * @param {String} old the old value
  3953. */
  3954. this.trigger('rename_node', { "node" : obj, "text" : val, "old" : old });
  3955. return true;
  3956. },
  3957. /**
  3958. * remove a node
  3959. * @name delete_node(obj)
  3960. * @param {mixed} obj the node, you can pass an array to delete multiple nodes
  3961. * @return {Boolean}
  3962. * @trigger delete_node.jstree, changed.jstree
  3963. */
  3964. delete_node : function (obj) {
  3965. var t1, t2, par, pos, tmp, i, j, k, l, c, top, lft;
  3966. if($.vakata.is_array(obj)) {
  3967. obj = obj.slice();
  3968. for(t1 = 0, t2 = obj.length; t1 < t2; t1++) {
  3969. this.delete_node(obj[t1]);
  3970. }
  3971. return true;
  3972. }
  3973. obj = this.get_node(obj);
  3974. if(!obj || obj.id === $.jstree.root) { return false; }
  3975. par = this.get_node(obj.parent);
  3976. pos = $.inArray(obj.id, par.children);
  3977. c = false;
  3978. if(!this.check("delete_node", obj, par, pos)) {
  3979. this.settings.core.error.call(this, this._data.core.last_error);
  3980. return false;
  3981. }
  3982. if(pos !== -1) {
  3983. par.children = $.vakata.array_remove(par.children, pos);
  3984. }
  3985. tmp = obj.children_d.concat([]);
  3986. tmp.push(obj.id);
  3987. for(i = 0, j = obj.parents.length; i < j; i++) {
  3988. this._model.data[obj.parents[i]].children_d = $.vakata.array_filter(this._model.data[obj.parents[i]].children_d, function (v) {
  3989. return $.inArray(v, tmp) === -1;
  3990. });
  3991. }
  3992. for(k = 0, l = tmp.length; k < l; k++) {
  3993. if(this._model.data[tmp[k]].state.selected) {
  3994. c = true;
  3995. break;
  3996. }
  3997. }
  3998. if (c) {
  3999. this._data.core.selected = $.vakata.array_filter(this._data.core.selected, function (v) {
  4000. return $.inArray(v, tmp) === -1;
  4001. });
  4002. }
  4003. /**
  4004. * triggered when a node is deleted
  4005. * @event
  4006. * @name delete_node.jstree
  4007. * @param {Object} node
  4008. * @param {String} parent the parent's ID
  4009. */
  4010. this.trigger('delete_node', { "node" : obj, "parent" : par.id });
  4011. if(c) {
  4012. this.trigger('changed', { 'action' : 'delete_node', 'node' : obj, 'selected' : this._data.core.selected, 'parent' : par.id });
  4013. }
  4014. for(k = 0, l = tmp.length; k < l; k++) {
  4015. delete this._model.data[tmp[k]];
  4016. }
  4017. if($.inArray(this._data.core.focused, tmp) !== -1) {
  4018. this._data.core.focused = null;
  4019. top = this.element[0].scrollTop;
  4020. lft = this.element[0].scrollLeft;
  4021. if(par.id === $.jstree.root) {
  4022. if (this._model.data[$.jstree.root].children[0]) {
  4023. this.get_node(this._model.data[$.jstree.root].children[0], true).children('.jstree-anchor').trigger('focus');
  4024. }
  4025. }
  4026. else {
  4027. this.get_node(par, true).children('.jstree-anchor').trigger('focus');
  4028. }
  4029. this.element[0].scrollTop = top;
  4030. this.element[0].scrollLeft = lft;
  4031. }
  4032. this.redraw_node(par, true);
  4033. return true;
  4034. },
  4035. /**
  4036. * check if an operation is premitted on the tree. Used internally.
  4037. * @private
  4038. * @name check(chk, obj, par, pos)
  4039. * @param {String} chk the operation to check, can be "create_node", "rename_node", "delete_node", "copy_node" or "move_node"
  4040. * @param {mixed} obj the node
  4041. * @param {mixed} par the parent
  4042. * @param {mixed} pos the position to insert at, or if "rename_node" - the new name
  4043. * @param {mixed} more some various additional information, for example if a "move_node" operations is triggered by DND this will be the hovered node
  4044. * @return {Boolean}
  4045. */
  4046. check : function (chk, obj, par, pos, more) {
  4047. obj = obj && obj.id ? obj : this.get_node(obj);
  4048. par = par && par.id ? par : this.get_node(par);
  4049. var tmp = chk.match(/^(move_node|copy_node|create_node)$/i) ? par : obj,
  4050. chc = this.settings.core.check_callback;
  4051. if(chk === "move_node" || chk === "copy_node") {
  4052. if((!more || !more.is_multi) && (chk === "move_node" && $.inArray(obj.id, par.children) === pos)) {
  4053. this._data.core.last_error = { 'error' : 'check', 'plugin' : 'core', 'id' : 'core_08', 'reason' : 'Moving node to its current position', 'data' : JSON.stringify({ 'chk' : chk, 'pos' : pos, 'obj' : obj && obj.id ? obj.id : false, 'par' : par && par.id ? par.id : false }) };
  4054. return false;
  4055. }
  4056. if((!more || !more.is_multi) && (obj.id === par.id || (chk === "move_node" && $.inArray(obj.id, par.children) === pos) || $.inArray(par.id, obj.children_d) !== -1)) {
  4057. 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 }) };
  4058. return false;
  4059. }
  4060. }
  4061. if(tmp && tmp.data) { tmp = tmp.data; }
  4062. if(tmp && tmp.functions && (tmp.functions[chk] === false || tmp.functions[chk] === true)) {
  4063. if(tmp.functions[chk] === false) {
  4064. 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 }) };
  4065. }
  4066. return tmp.functions[chk];
  4067. }
  4068. if(chc === false || ($.vakata.is_function(chc) && chc.call(this, chk, obj, par, pos, more) === false) || (chc && chc[chk] === false)) {
  4069. 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 }) };
  4070. return false;
  4071. }
  4072. return true;
  4073. },
  4074. /**
  4075. * get the last error
  4076. * @name last_error()
  4077. * @return {Object}
  4078. */
  4079. last_error : function () {
  4080. return this._data.core.last_error;
  4081. },
  4082. /**
  4083. * move a node to a new parent
  4084. * @name move_node(obj, par [, pos, callback, is_loaded])
  4085. * @param {mixed} obj the node to move, pass an array to move multiple nodes
  4086. * @param {mixed} par the new parent
  4087. * @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`
  4088. * @param {function} callback a function to call once the move is completed, receives 3 arguments - the node, the new parent and the position
  4089. * @param {Boolean} is_loaded internal parameter indicating if the parent node has been loaded
  4090. * @param {Boolean} skip_redraw internal parameter indicating if the tree should be redrawn
  4091. * @param {Boolean} instance internal parameter indicating if the node comes from another instance
  4092. * @trigger move_node.jstree
  4093. */
  4094. move_node : function (obj, par, pos, callback, is_loaded, skip_redraw, origin) {
  4095. var t1, t2, old_par, old_pos, new_par, old_ins, is_multi, dpc, tmp, i, j, k, l, p;
  4096. par = this.get_node(par);
  4097. pos = pos === undefined ? 0 : pos;
  4098. if(!par) { return false; }
  4099. if(!pos.toString().match(/^(before|after)$/) && !is_loaded && !this.is_loaded(par)) {
  4100. return this.load_node(par, function () { this.move_node(obj, par, pos, callback, true, false, origin); });
  4101. }
  4102. if($.vakata.is_array(obj)) {
  4103. if(obj.length === 1) {
  4104. obj = obj[0];
  4105. }
  4106. else {
  4107. //obj = obj.slice();
  4108. for(t1 = 0, t2 = obj.length; t1 < t2; t1++) {
  4109. if((tmp = this.move_node(obj[t1], par, pos, callback, is_loaded, false, origin))) {
  4110. par = tmp;
  4111. pos = "after";
  4112. }
  4113. }
  4114. this.redraw();
  4115. return true;
  4116. }
  4117. }
  4118. obj = obj && (obj.id !== undefined) ? obj : this.get_node(obj);
  4119. if(!obj || obj.id === $.jstree.root) { return false; }
  4120. old_par = (obj.parent || $.jstree.root).toString();
  4121. new_par = (!pos.toString().match(/^(before|after)$/) || par.id === $.jstree.root) ? par : this.get_node(par.parent);
  4122. old_ins = origin ? origin : (this._model.data[obj.id] ? this : $.jstree.reference(obj.id));
  4123. is_multi = !old_ins || !old_ins._id || (this._id !== old_ins._id);
  4124. 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;
  4125. if(old_ins && old_ins._id) {
  4126. obj = old_ins._model.data[obj.id];
  4127. }
  4128. if(is_multi) {
  4129. if((tmp = this.copy_node(obj, par, pos, callback, is_loaded, false, origin))) {
  4130. if(old_ins) { old_ins.delete_node(obj); }
  4131. return tmp;
  4132. }
  4133. return false;
  4134. }
  4135. //var m = this._model.data;
  4136. if(par.id === $.jstree.root) {
  4137. if(pos === "before") { pos = "first"; }
  4138. if(pos === "after") { pos = "last"; }
  4139. }
  4140. switch(pos) {
  4141. case "before":
  4142. pos = $.inArray(par.id, new_par.children);
  4143. break;
  4144. case "after" :
  4145. pos = $.inArray(par.id, new_par.children) + 1;
  4146. break;
  4147. case "inside":
  4148. case "first":
  4149. pos = 0;
  4150. break;
  4151. case "last":
  4152. pos = new_par.children.length;
  4153. break;
  4154. default:
  4155. if(!pos) { pos = 0; }
  4156. break;
  4157. }
  4158. if(pos > new_par.children.length) { pos = new_par.children.length; }
  4159. if(!this.check("move_node", obj, new_par, pos, { 'core' : true, 'origin' : origin, 'is_multi' : (old_ins && old_ins._id && old_ins._id !== this._id), 'is_foreign' : (!old_ins || !old_ins._id) })) {
  4160. this.settings.core.error.call(this, this._data.core.last_error);
  4161. return false;
  4162. }
  4163. if(obj.parent === new_par.id) {
  4164. dpc = new_par.children.concat();
  4165. tmp = $.inArray(obj.id, dpc);
  4166. if(tmp !== -1) {
  4167. dpc = $.vakata.array_remove(dpc, tmp);
  4168. if(pos > tmp) { pos--; }
  4169. }
  4170. tmp = [];
  4171. for(i = 0, j = dpc.length; i < j; i++) {
  4172. tmp[i >= pos ? i+1 : i] = dpc[i];
  4173. }
  4174. tmp[pos] = obj.id;
  4175. new_par.children = tmp;
  4176. this._node_changed(new_par.id);
  4177. this.redraw(new_par.id === $.jstree.root);
  4178. }
  4179. else {
  4180. // clean old parent and up
  4181. tmp = obj.children_d.concat();
  4182. tmp.push(obj.id);
  4183. for(i = 0, j = obj.parents.length; i < j; i++) {
  4184. dpc = [];
  4185. p = old_ins._model.data[obj.parents[i]].children_d;
  4186. for(k = 0, l = p.length; k < l; k++) {
  4187. if($.inArray(p[k], tmp) === -1) {
  4188. dpc.push(p[k]);
  4189. }
  4190. }
  4191. old_ins._model.data[obj.parents[i]].children_d = dpc;
  4192. }
  4193. old_ins._model.data[old_par].children = $.vakata.array_remove_item(old_ins._model.data[old_par].children, obj.id);
  4194. // insert into new parent and up
  4195. for(i = 0, j = new_par.parents.length; i < j; i++) {
  4196. this._model.data[new_par.parents[i]].children_d = this._model.data[new_par.parents[i]].children_d.concat(tmp);
  4197. }
  4198. dpc = [];
  4199. for(i = 0, j = new_par.children.length; i < j; i++) {
  4200. dpc[i >= pos ? i+1 : i] = new_par.children[i];
  4201. }
  4202. dpc[pos] = obj.id;
  4203. new_par.children = dpc;
  4204. new_par.children_d.push(obj.id);
  4205. new_par.children_d = new_par.children_d.concat(obj.children_d);
  4206. // update object
  4207. obj.parent = new_par.id;
  4208. tmp = new_par.parents.concat();
  4209. tmp.unshift(new_par.id);
  4210. p = obj.parents.length;
  4211. obj.parents = tmp;
  4212. // update object children
  4213. tmp = tmp.concat();
  4214. for(i = 0, j = obj.children_d.length; i < j; i++) {
  4215. this._model.data[obj.children_d[i]].parents = this._model.data[obj.children_d[i]].parents.slice(0,p*-1);
  4216. Array.prototype.push.apply(this._model.data[obj.children_d[i]].parents, tmp);
  4217. }
  4218. if(old_par === $.jstree.root || new_par.id === $.jstree.root) {
  4219. this._model.force_full_redraw = true;
  4220. }
  4221. if(!this._model.force_full_redraw) {
  4222. this._node_changed(old_par);
  4223. this._node_changed(new_par.id);
  4224. }
  4225. if(!skip_redraw) {
  4226. this.redraw();
  4227. }
  4228. }
  4229. if(callback) { callback.call(this, obj, new_par, pos); }
  4230. /**
  4231. * triggered when a node is moved
  4232. * @event
  4233. * @name move_node.jstree
  4234. * @param {Object} node
  4235. * @param {String} parent the parent's ID
  4236. * @param {Number} position the position of the node among the parent's children
  4237. * @param {String} old_parent the old parent of the node
  4238. * @param {Number} old_position the old position of the node
  4239. * @param {Boolean} is_multi do the node and new parent belong to different instances
  4240. * @param {jsTree} old_instance the instance the node came from
  4241. * @param {jsTree} new_instance the instance of the new parent
  4242. */
  4243. 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 });
  4244. return obj.id;
  4245. },
  4246. /**
  4247. * copy a node to a new parent
  4248. * @name copy_node(obj, par [, pos, callback, is_loaded])
  4249. * @param {mixed} obj the node to copy, pass an array to copy multiple nodes
  4250. * @param {mixed} par the new parent
  4251. * @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`
  4252. * @param {function} callback a function to call once the move is completed, receives 3 arguments - the node, the new parent and the position
  4253. * @param {Boolean} is_loaded internal parameter indicating if the parent node has been loaded
  4254. * @param {Boolean} skip_redraw internal parameter indicating if the tree should be redrawn
  4255. * @param {Boolean} instance internal parameter indicating if the node comes from another instance
  4256. * @trigger model.jstree copy_node.jstree
  4257. */
  4258. copy_node : function (obj, par, pos, callback, is_loaded, skip_redraw, origin) {
  4259. var t1, t2, dpc, tmp, i, j, node, old_par, new_par, old_ins, is_multi;
  4260. par = this.get_node(par);
  4261. pos = pos === undefined ? 0 : pos;
  4262. if(!par) { return false; }
  4263. if(!pos.toString().match(/^(before|after)$/) && !is_loaded && !this.is_loaded(par)) {
  4264. return this.load_node(par, function () { this.copy_node(obj, par, pos, callback, true, false, origin); });
  4265. }
  4266. if($.vakata.is_array(obj)) {
  4267. if(obj.length === 1) {
  4268. obj = obj[0];
  4269. }
  4270. else {
  4271. //obj = obj.slice();
  4272. for(t1 = 0, t2 = obj.length; t1 < t2; t1++) {
  4273. if((tmp = this.copy_node(obj[t1], par, pos, callback, is_loaded, true, origin))) {
  4274. par = tmp;
  4275. pos = "after";
  4276. }
  4277. }
  4278. this.redraw();
  4279. return true;
  4280. }
  4281. }
  4282. obj = obj && (obj.id !== undefined) ? obj : this.get_node(obj);
  4283. if(!obj || obj.id === $.jstree.root) { return false; }
  4284. old_par = (obj.parent || $.jstree.root).toString();
  4285. new_par = (!pos.toString().match(/^(before|after)$/) || par.id === $.jstree.root) ? par : this.get_node(par.parent);
  4286. old_ins = origin ? origin : (this._model.data[obj.id] ? this : $.jstree.reference(obj.id));
  4287. is_multi = !old_ins || !old_ins._id || (this._id !== old_ins._id);
  4288. if(old_ins && old_ins._id) {
  4289. obj = old_ins._model.data[obj.id];
  4290. }
  4291. if(par.id === $.jstree.root) {
  4292. if(pos === "before") { pos = "first"; }
  4293. if(pos === "after") { pos = "last"; }
  4294. }
  4295. switch(pos) {
  4296. case "before":
  4297. pos = $.inArray(par.id, new_par.children);
  4298. break;
  4299. case "after" :
  4300. pos = $.inArray(par.id, new_par.children) + 1;
  4301. break;
  4302. case "inside":
  4303. case "first":
  4304. pos = 0;
  4305. break;
  4306. case "last":
  4307. pos = new_par.children.length;
  4308. break;
  4309. default:
  4310. if(!pos) { pos = 0; }
  4311. break;
  4312. }
  4313. if(pos > new_par.children.length) { pos = new_par.children.length; }
  4314. if(!this.check("copy_node", obj, new_par, pos, { 'core' : true, 'origin' : origin, 'is_multi' : (old_ins && old_ins._id && old_ins._id !== this._id), 'is_foreign' : (!old_ins || !old_ins._id) })) {
  4315. this.settings.core.error.call(this, this._data.core.last_error);
  4316. return false;
  4317. }
  4318. node = old_ins ? old_ins.get_json(obj, { no_id : true, no_data : true, no_state : true }) : obj;
  4319. if(!node) { return false; }
  4320. if(node.id === true) { delete node.id; }
  4321. node = this._parse_model_from_json(node, new_par.id, new_par.parents.concat());
  4322. if(!node) { return false; }
  4323. tmp = this.get_node(node);
  4324. if(obj && obj.state && obj.state.loaded === false) { tmp.state.loaded = false; }
  4325. dpc = [];
  4326. dpc.push(node);
  4327. dpc = dpc.concat(tmp.children_d);
  4328. this.trigger('model', { "nodes" : dpc, "parent" : new_par.id });
  4329. // insert into new parent and up
  4330. for(i = 0, j = new_par.parents.length; i < j; i++) {
  4331. this._model.data[new_par.parents[i]].children_d = this._model.data[new_par.parents[i]].children_d.concat(dpc);
  4332. }
  4333. dpc = [];
  4334. for(i = 0, j = new_par.children.length; i < j; i++) {
  4335. dpc[i >= pos ? i+1 : i] = new_par.children[i];
  4336. }
  4337. dpc[pos] = tmp.id;
  4338. new_par.children = dpc;
  4339. new_par.children_d.push(tmp.id);
  4340. new_par.children_d = new_par.children_d.concat(tmp.children_d);
  4341. if(new_par.id === $.jstree.root) {
  4342. this._model.force_full_redraw = true;
  4343. }
  4344. if(!this._model.force_full_redraw) {
  4345. this._node_changed(new_par.id);
  4346. }
  4347. if(!skip_redraw) {
  4348. this.redraw(new_par.id === $.jstree.root);
  4349. }
  4350. if(callback) { callback.call(this, tmp, new_par, pos); }
  4351. /**
  4352. * triggered when a node is copied
  4353. * @event
  4354. * @name copy_node.jstree
  4355. * @param {Object} node the copied node
  4356. * @param {Object} original the original node
  4357. * @param {String} parent the parent's ID
  4358. * @param {Number} position the position of the node among the parent's children
  4359. * @param {String} old_parent the old parent of the node
  4360. * @param {Number} old_position the position of the original node
  4361. * @param {Boolean} is_multi do the node and new parent belong to different instances
  4362. * @param {jsTree} old_instance the instance the node came from
  4363. * @param {jsTree} new_instance the instance of the new parent
  4364. */
  4365. 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 });
  4366. return tmp.id;
  4367. },
  4368. /**
  4369. * cut a node (a later call to `paste(obj)` would move the node)
  4370. * @name cut(obj)
  4371. * @param {mixed} obj multiple objects can be passed using an array
  4372. * @trigger cut.jstree
  4373. */
  4374. cut : function (obj) {
  4375. if(!obj) { obj = this._data.core.selected.concat(); }
  4376. if(!$.vakata.is_array(obj)) { obj = [obj]; }
  4377. if(!obj.length) { return false; }
  4378. var tmp = [], o, t1, t2;
  4379. for(t1 = 0, t2 = obj.length; t1 < t2; t1++) {
  4380. o = this.get_node(obj[t1]);
  4381. if(o && (o.id || o.id === 0) && o.id !== $.jstree.root) { tmp.push(o); }
  4382. }
  4383. if(!tmp.length) { return false; }
  4384. ccp_node = tmp;
  4385. ccp_inst = this;
  4386. ccp_mode = 'move_node';
  4387. /**
  4388. * triggered when nodes are added to the buffer for moving
  4389. * @event
  4390. * @name cut.jstree
  4391. * @param {Array} node
  4392. */
  4393. this.trigger('cut', { "node" : obj });
  4394. },
  4395. /**
  4396. * copy a node (a later call to `paste(obj)` would copy the node)
  4397. * @name copy(obj)
  4398. * @param {mixed} obj multiple objects can be passed using an array
  4399. * @trigger copy.jstree
  4400. */
  4401. copy : function (obj) {
  4402. if(!obj) { obj = this._data.core.selected.concat(); }
  4403. if(!$.vakata.is_array(obj)) { obj = [obj]; }
  4404. if(!obj.length) { return false; }
  4405. var tmp = [], o, t1, t2;
  4406. for(t1 = 0, t2 = obj.length; t1 < t2; t1++) {
  4407. o = this.get_node(obj[t1]);
  4408. if(o && (o.id !== undefined) && o.id !== $.jstree.root) { tmp.push(o); }
  4409. }
  4410. if(!tmp.length) { return false; }
  4411. ccp_node = tmp;
  4412. ccp_inst = this;
  4413. ccp_mode = 'copy_node';
  4414. /**
  4415. * triggered when nodes are added to the buffer for copying
  4416. * @event
  4417. * @name copy.jstree
  4418. * @param {Array} node
  4419. */
  4420. this.trigger('copy', { "node" : obj });
  4421. },
  4422. /**
  4423. * get the current buffer (any nodes that are waiting for a paste operation)
  4424. * @name get_buffer()
  4425. * @return {Object} an object consisting of `mode` ("copy_node" or "move_node"), `node` (an array of objects) and `inst` (the instance)
  4426. */
  4427. get_buffer : function () {
  4428. return { 'mode' : ccp_mode, 'node' : ccp_node, 'inst' : ccp_inst };
  4429. },
  4430. /**
  4431. * check if there is something in the buffer to paste
  4432. * @name can_paste()
  4433. * @return {Boolean}
  4434. */
  4435. can_paste : function () {
  4436. return ccp_mode !== false && ccp_node !== false; // && ccp_inst._model.data[ccp_node];
  4437. },
  4438. /**
  4439. * copy or move the previously cut or copied nodes to a new parent
  4440. * @name paste(obj [, pos])
  4441. * @param {mixed} obj the new parent
  4442. * @param {mixed} pos the position to insert at (besides integer, "first" and "last" are supported), defaults to integer `0`
  4443. * @trigger paste.jstree
  4444. */
  4445. paste : function (obj, pos) {
  4446. obj = this.get_node(obj);
  4447. if(!obj || !ccp_mode || !ccp_mode.match(/^(copy_node|move_node)$/) || !ccp_node) { return false; }
  4448. if(this[ccp_mode](ccp_node, obj, pos, false, false, false, ccp_inst)) {
  4449. /**
  4450. * triggered when paste is invoked
  4451. * @event
  4452. * @name paste.jstree
  4453. * @param {String} parent the ID of the receiving node
  4454. * @param {Array} node the nodes in the buffer
  4455. * @param {String} mode the performed operation - "copy_node" or "move_node"
  4456. */
  4457. this.trigger('paste', { "parent" : obj.id, "node" : ccp_node, "mode" : ccp_mode });
  4458. }
  4459. ccp_node = false;
  4460. ccp_mode = false;
  4461. ccp_inst = false;
  4462. },
  4463. /**
  4464. * clear the buffer of previously copied or cut nodes
  4465. * @name clear_buffer()
  4466. * @trigger clear_buffer.jstree
  4467. */
  4468. clear_buffer : function () {
  4469. ccp_node = false;
  4470. ccp_mode = false;
  4471. ccp_inst = false;
  4472. /**
  4473. * triggered when the copy / cut buffer is cleared
  4474. * @event
  4475. * @name clear_buffer.jstree
  4476. */
  4477. this.trigger('clear_buffer');
  4478. },
  4479. /**
  4480. * put a node in edit mode (input field to rename the node)
  4481. * @name edit(obj [, default_text, callback])
  4482. * @param {mixed} obj
  4483. * @param {String} default_text the text to populate the input with (if omitted or set to a non-string value the node's text value is used)
  4484. * @param {Function} callback a function to be called once the text box is blurred, it is called in the instance's scope and receives the node, a status parameter (true if the rename is successful, false otherwise), a boolean indicating if the user cancelled the edit and the original unescaped value provided by the user. You can also access the node's title using .text
  4485. */
  4486. edit : function (obj, default_text, callback) {
  4487. var rtl, w, a, s, t, h1, h2, fn, tmp, cancel = false;
  4488. obj = this.get_node(obj);
  4489. if(!obj) { return false; }
  4490. if(!this.check("edit", obj, this.get_parent(obj))) {
  4491. this.settings.core.error.call(this, this._data.core.last_error);
  4492. return false;
  4493. }
  4494. tmp = obj;
  4495. default_text = typeof default_text === 'string' ? default_text : obj.text;
  4496. this.set_text(obj, "");
  4497. obj = this._open_to(obj);
  4498. tmp.text = default_text;
  4499. rtl = this._data.core.rtl;
  4500. w = this.element.width();
  4501. this._data.core.focused = tmp.id;
  4502. a = obj.children('.jstree-anchor').trigger('focus');
  4503. s = $('<span></span>');
  4504. /*!
  4505. oi = obj.children("i:visible"),
  4506. ai = a.children("i:visible"),
  4507. w1 = oi.width() * oi.length,
  4508. w2 = ai.width() * ai.length,
  4509. */
  4510. t = default_text;
  4511. h1 = $("<"+"div></div>", { css : { "position" : "absolute", "top" : "-200px", "left" : (rtl ? "0px" : "-1000px"), "visibility" : "hidden" } }).appendTo(document.body);
  4512. h2 = $("<"+"input />", {
  4513. "value" : t,
  4514. "class" : "jstree-rename-input",
  4515. // "size" : t.length,
  4516. "css" : {
  4517. "padding" : "0",
  4518. "border" : "1px solid silver",
  4519. "box-sizing" : "border-box",
  4520. "display" : "inline-block",
  4521. "height" : (this._data.core.li_height) + "px",
  4522. "lineHeight" : (this._data.core.li_height) + "px",
  4523. "width" : "150px" // will be set a bit further down
  4524. },
  4525. "blur" : function (e) {
  4526. e.stopImmediatePropagation();
  4527. e.preventDefault();
  4528. var i = s.children(".jstree-rename-input"),
  4529. v = i.val(),
  4530. f = this.settings.core.force_text,
  4531. nv;
  4532. if(v === "") { v = t; }
  4533. h1.remove();
  4534. s.replaceWith(a);
  4535. s.remove();
  4536. t = f ? t : $('<div></div>').append($.parseHTML(t)).html();
  4537. obj = this.get_node(obj);
  4538. this.set_text(obj, t);
  4539. nv = !!this.rename_node(obj, f ? $('<div></div>').text(v).text() : $('<div></div>').append($.parseHTML(v)).html());
  4540. if(!nv) {
  4541. this.set_text(obj, t); // move this up? and fix #483
  4542. }
  4543. this._data.core.focused = tmp.id;
  4544. setTimeout(function () {
  4545. var node = this.get_node(tmp.id, true);
  4546. if(node.length) {
  4547. this._data.core.focused = tmp.id;
  4548. node.children('.jstree-anchor').trigger('focus');
  4549. }
  4550. }.bind(this), 0);
  4551. if(callback) {
  4552. callback.call(this, tmp, nv, cancel, v);
  4553. }
  4554. h2 = null;
  4555. }.bind(this),
  4556. "keydown" : function (e) {
  4557. var key = e.which;
  4558. if(key === 27) {
  4559. cancel = true;
  4560. this.value = t;
  4561. }
  4562. if(key === 27 || key === 13 || key === 37 || key === 38 || key === 39 || key === 40 || key === 32) {
  4563. e.stopImmediatePropagation();
  4564. }
  4565. if(key === 27 || key === 13) {
  4566. e.preventDefault();
  4567. this.blur();
  4568. }
  4569. },
  4570. "click" : function (e) { e.stopImmediatePropagation(); },
  4571. "mousedown" : function (e) { e.stopImmediatePropagation(); },
  4572. "keyup" : function (e) {
  4573. h2.width(Math.min(h1.text("pW" + this.value).width(),w));
  4574. },
  4575. "keypress" : function(e) {
  4576. if(e.which === 13) { return false; }
  4577. }
  4578. });
  4579. fn = {
  4580. fontFamily : a.css('fontFamily') || '',
  4581. fontSize : a.css('fontSize') || '',
  4582. fontWeight : a.css('fontWeight') || '',
  4583. fontStyle : a.css('fontStyle') || '',
  4584. fontStretch : a.css('fontStretch') || '',
  4585. fontVariant : a.css('fontVariant') || '',
  4586. letterSpacing : a.css('letterSpacing') || '',
  4587. wordSpacing : a.css('wordSpacing') || ''
  4588. };
  4589. s.attr('class', a.attr('class')).append(a.contents().clone()).append(h2);
  4590. a.replaceWith(s);
  4591. h1.css(fn);
  4592. h2.css(fn).width(Math.min(h1.text("pW" + h2[0].value).width(),w))[0].select();
  4593. $(document).one('mousedown.jstree touchstart.jstree dnd_start.vakata', function (e) {
  4594. if (h2 && e.target !== h2) {
  4595. $(h2).trigger('blur');
  4596. }
  4597. });
  4598. },
  4599. /**
  4600. * changes the theme
  4601. * @name set_theme(theme_name [, theme_url])
  4602. * @param {String} theme_name the name of the new theme to apply
  4603. * @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.
  4604. * @trigger set_theme.jstree
  4605. */
  4606. set_theme : function (theme_name, theme_url) {
  4607. if(!theme_name) { return false; }
  4608. if(theme_url === true) {
  4609. var dir = this.settings.core.themes.dir;
  4610. if(!dir) { dir = $.jstree.path + '/themes'; }
  4611. theme_url = dir + '/' + theme_name + '/style.css';
  4612. }
  4613. if(theme_url && $.inArray(theme_url, themes_loaded) === -1) {
  4614. $('head').append('<'+'link rel="stylesheet" href="' + theme_url + '" type="text/css" />');
  4615. themes_loaded.push(theme_url);
  4616. }
  4617. if(this._data.core.themes.name) {
  4618. this.element.removeClass('jstree-' + this._data.core.themes.name);
  4619. }
  4620. this._data.core.themes.name = theme_name;
  4621. this.element.addClass('jstree-' + theme_name);
  4622. this.element[this.settings.core.themes.responsive ? 'addClass' : 'removeClass' ]('jstree-' + theme_name + '-responsive');
  4623. /**
  4624. * triggered when a theme is set
  4625. * @event
  4626. * @name set_theme.jstree
  4627. * @param {String} theme the new theme
  4628. */
  4629. this.trigger('set_theme', { 'theme' : theme_name });
  4630. },
  4631. /**
  4632. * gets the name of the currently applied theme name
  4633. * @name get_theme()
  4634. * @return {String}
  4635. */
  4636. get_theme : function () { return this._data.core.themes.name; },
  4637. /**
  4638. * changes the theme variant (if the theme has variants)
  4639. * @name set_theme_variant(variant_name)
  4640. * @param {String|Boolean} variant_name the variant to apply (if `false` is used the current variant is removed)
  4641. */
  4642. set_theme_variant : function (variant_name) {
  4643. if(this._data.core.themes.variant) {
  4644. this.element.removeClass('jstree-' + this._data.core.themes.name + '-' + this._data.core.themes.variant);
  4645. }
  4646. this._data.core.themes.variant = variant_name;
  4647. if(variant_name) {
  4648. this.element.addClass('jstree-' + this._data.core.themes.name + '-' + this._data.core.themes.variant);
  4649. }
  4650. },
  4651. /**
  4652. * gets the name of the currently applied theme variant
  4653. * @name get_theme()
  4654. * @return {String}
  4655. */
  4656. get_theme_variant : function () { return this._data.core.themes.variant; },
  4657. /**
  4658. * shows a striped background on the container (if the theme supports it)
  4659. * @name show_stripes()
  4660. */
  4661. show_stripes : function () {
  4662. this._data.core.themes.stripes = true;
  4663. this.get_container_ul().addClass("jstree-striped");
  4664. /**
  4665. * triggered when stripes are shown
  4666. * @event
  4667. * @name show_stripes.jstree
  4668. */
  4669. this.trigger('show_stripes');
  4670. },
  4671. /**
  4672. * hides the striped background on the container
  4673. * @name hide_stripes()
  4674. */
  4675. hide_stripes : function () {
  4676. this._data.core.themes.stripes = false;
  4677. this.get_container_ul().removeClass("jstree-striped");
  4678. /**
  4679. * triggered when stripes are hidden
  4680. * @event
  4681. * @name hide_stripes.jstree
  4682. */
  4683. this.trigger('hide_stripes');
  4684. },
  4685. /**
  4686. * toggles the striped background on the container
  4687. * @name toggle_stripes()
  4688. */
  4689. toggle_stripes : function () { if(this._data.core.themes.stripes) { this.hide_stripes(); } else { this.show_stripes(); } },
  4690. /**
  4691. * shows the connecting dots (if the theme supports it)
  4692. * @name show_dots()
  4693. */
  4694. show_dots : function () {
  4695. this._data.core.themes.dots = true;
  4696. this.get_container_ul().removeClass("jstree-no-dots");
  4697. /**
  4698. * triggered when dots are shown
  4699. * @event
  4700. * @name show_dots.jstree
  4701. */
  4702. this.trigger('show_dots');
  4703. },
  4704. /**
  4705. * hides the connecting dots
  4706. * @name hide_dots()
  4707. */
  4708. hide_dots : function () {
  4709. this._data.core.themes.dots = false;
  4710. this.get_container_ul().addClass("jstree-no-dots");
  4711. /**
  4712. * triggered when dots are hidden
  4713. * @event
  4714. * @name hide_dots.jstree
  4715. */
  4716. this.trigger('hide_dots');
  4717. },
  4718. /**
  4719. * toggles the connecting dots
  4720. * @name toggle_dots()
  4721. */
  4722. toggle_dots : function () { if(this._data.core.themes.dots) { this.hide_dots(); } else { this.show_dots(); } },
  4723. /**
  4724. * show the node icons
  4725. * @name show_icons()
  4726. */
  4727. show_icons : function () {
  4728. this._data.core.themes.icons = true;
  4729. this.get_container_ul().removeClass("jstree-no-icons");
  4730. /**
  4731. * triggered when icons are shown
  4732. * @event
  4733. * @name show_icons.jstree
  4734. */
  4735. this.trigger('show_icons');
  4736. },
  4737. /**
  4738. * hide the node icons
  4739. * @name hide_icons()
  4740. */
  4741. hide_icons : function () {
  4742. this._data.core.themes.icons = false;
  4743. this.get_container_ul().addClass("jstree-no-icons");
  4744. /**
  4745. * triggered when icons are hidden
  4746. * @event
  4747. * @name hide_icons.jstree
  4748. */
  4749. this.trigger('hide_icons');
  4750. },
  4751. /**
  4752. * toggle the node icons
  4753. * @name toggle_icons()
  4754. */
  4755. toggle_icons : function () { if(this._data.core.themes.icons) { this.hide_icons(); } else { this.show_icons(); } },
  4756. /**
  4757. * show the node ellipsis
  4758. * @name show_icons()
  4759. */
  4760. show_ellipsis : function () {
  4761. this._data.core.themes.ellipsis = true;
  4762. this.get_container_ul().addClass("jstree-ellipsis");
  4763. /**
  4764. * triggered when ellisis is shown
  4765. * @event
  4766. * @name show_ellipsis.jstree
  4767. */
  4768. this.trigger('show_ellipsis');
  4769. },
  4770. /**
  4771. * hide the node ellipsis
  4772. * @name hide_ellipsis()
  4773. */
  4774. hide_ellipsis : function () {
  4775. this._data.core.themes.ellipsis = false;
  4776. this.get_container_ul().removeClass("jstree-ellipsis");
  4777. /**
  4778. * triggered when ellisis is hidden
  4779. * @event
  4780. * @name hide_ellipsis.jstree
  4781. */
  4782. this.trigger('hide_ellipsis');
  4783. },
  4784. /**
  4785. * toggle the node ellipsis
  4786. * @name toggle_icons()
  4787. */
  4788. toggle_ellipsis : function () { if(this._data.core.themes.ellipsis) { this.hide_ellipsis(); } else { this.show_ellipsis(); } },
  4789. /**
  4790. * set the node icon for a node
  4791. * @name set_icon(obj, icon)
  4792. * @param {mixed} obj
  4793. * @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
  4794. */
  4795. set_icon : function (obj, icon) {
  4796. var t1, t2, dom, old;
  4797. if($.vakata.is_array(obj)) {
  4798. obj = obj.slice();
  4799. for(t1 = 0, t2 = obj.length; t1 < t2; t1++) {
  4800. this.set_icon(obj[t1], icon);
  4801. }
  4802. return true;
  4803. }
  4804. obj = this.get_node(obj);
  4805. if(!obj || obj.id === $.jstree.root) { return false; }
  4806. old = obj.icon;
  4807. obj.icon = icon === true || icon === null || icon === undefined || icon === '' ? true : icon;
  4808. dom = this.get_node(obj, true).children(".jstree-anchor").children(".jstree-themeicon");
  4809. if(icon === false) {
  4810. dom.removeClass('jstree-themeicon-custom ' + old).css("background","").removeAttr("rel");
  4811. this.hide_icon(obj);
  4812. }
  4813. else if(icon === true || icon === null || icon === undefined || icon === '') {
  4814. dom.removeClass('jstree-themeicon-custom ' + old).css("background","").removeAttr("rel");
  4815. if(old === false) { this.show_icon(obj); }
  4816. }
  4817. else if(icon.indexOf("/") === -1 && icon.indexOf(".") === -1) {
  4818. dom.removeClass(old).css("background","");
  4819. dom.addClass(icon + ' jstree-themeicon-custom').attr("rel",icon);
  4820. if(old === false) { this.show_icon(obj); }
  4821. }
  4822. else {
  4823. dom.removeClass(old).css("background","");
  4824. dom.addClass('jstree-themeicon-custom').css("background", "url('" + icon + "') center center no-repeat").attr("rel",icon);
  4825. if(old === false) { this.show_icon(obj); }
  4826. }
  4827. return true;
  4828. },
  4829. /**
  4830. * get the node icon for a node
  4831. * @name get_icon(obj)
  4832. * @param {mixed} obj
  4833. * @return {String}
  4834. */
  4835. get_icon : function (obj) {
  4836. obj = this.get_node(obj);
  4837. return (!obj || obj.id === $.jstree.root) ? false : obj.icon;
  4838. },
  4839. /**
  4840. * hide the icon on an individual node
  4841. * @name hide_icon(obj)
  4842. * @param {mixed} obj
  4843. */
  4844. hide_icon : function (obj) {
  4845. var t1, t2;
  4846. if($.vakata.is_array(obj)) {
  4847. obj = obj.slice();
  4848. for(t1 = 0, t2 = obj.length; t1 < t2; t1++) {
  4849. this.hide_icon(obj[t1]);
  4850. }
  4851. return true;
  4852. }
  4853. obj = this.get_node(obj);
  4854. if(!obj || obj === $.jstree.root) { return false; }
  4855. obj.icon = false;
  4856. this.get_node(obj, true).children(".jstree-anchor").children(".jstree-themeicon").addClass('jstree-themeicon-hidden');
  4857. return true;
  4858. },
  4859. /**
  4860. * show the icon on an individual node
  4861. * @name show_icon(obj)
  4862. * @param {mixed} obj
  4863. */
  4864. show_icon : function (obj) {
  4865. var t1, t2, dom;
  4866. if($.vakata.is_array(obj)) {
  4867. obj = obj.slice();
  4868. for(t1 = 0, t2 = obj.length; t1 < t2; t1++) {
  4869. this.show_icon(obj[t1]);
  4870. }
  4871. return true;
  4872. }
  4873. obj = this.get_node(obj);
  4874. if(!obj || obj === $.jstree.root) { return false; }
  4875. dom = this.get_node(obj, true);
  4876. obj.icon = dom.length ? dom.children(".jstree-anchor").children(".jstree-themeicon").attr('rel') : true;
  4877. if(!obj.icon) { obj.icon = true; }
  4878. dom.children(".jstree-anchor").children(".jstree-themeicon").removeClass('jstree-themeicon-hidden');
  4879. return true;
  4880. }
  4881. };
  4882. // helpers
  4883. $.vakata = {};
  4884. // collect attributes
  4885. $.vakata.attributes = function(node, with_values) {
  4886. node = $(node)[0];
  4887. var attr = with_values ? {} : [];
  4888. if(node && node.attributes) {
  4889. $.each(node.attributes, function (i, v) {
  4890. if($.inArray(v.name.toLowerCase(),['style','contenteditable','hasfocus','tabindex']) !== -1) { return; }
  4891. if(v.value !== null && $.vakata.trim(v.value) !== '') {
  4892. if(with_values) { attr[v.name] = v.value; }
  4893. else { attr.push(v.name); }
  4894. }
  4895. });
  4896. }
  4897. return attr;
  4898. };
  4899. $.vakata.array_unique = function(array) {
  4900. var a = [], i, j, l, o = {};
  4901. for(i = 0, l = array.length; i < l; i++) {
  4902. if(o[array[i]] === undefined) {
  4903. a.push(array[i]);
  4904. o[array[i]] = true;
  4905. }
  4906. }
  4907. return a;
  4908. };
  4909. // remove item from array
  4910. $.vakata.array_remove = function(array, from) {
  4911. array.splice(from, 1);
  4912. return array;
  4913. //var rest = array.slice((to || from) + 1 || array.length);
  4914. //array.length = from < 0 ? array.length + from : from;
  4915. //array.push.apply(array, rest);
  4916. //return array;
  4917. };
  4918. // remove item from array
  4919. $.vakata.array_remove_item = function(array, item) {
  4920. var tmp = $.inArray(item, array);
  4921. return tmp !== -1 ? $.vakata.array_remove(array, tmp) : array;
  4922. };
  4923. $.vakata.array_filter = function(c,a,b,d,e) {
  4924. if (c.filter) {
  4925. return c.filter(a, b);
  4926. }
  4927. d=[];
  4928. for (e in c) {
  4929. if (~~e+''===e+'' && e>=0 && a.call(b,c[e],+e,c)) {
  4930. d.push(c[e]);
  4931. }
  4932. }
  4933. return d;
  4934. };
  4935. $.vakata.trim = function (text) {
  4936. return String.prototype.trim ?
  4937. String.prototype.trim.call(text.toString()) :
  4938. text.toString().replace(/^[\s\uFEFF\xA0]+|[\s\uFEFF\xA0]+$/g, '');
  4939. };
  4940. $.vakata.is_function = function(obj) {
  4941. return typeof obj === "function" && typeof obj.nodeType !== "number";
  4942. };
  4943. $.vakata.is_array = Array.isArray || function (obj) {
  4944. return Object.prototype.toString.call(obj) === "[object Array]";
  4945. };
  4946. // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_objects/Function/bind#polyfill
  4947. if (!Function.prototype.bind) {
  4948. Function.prototype.bind = function () {
  4949. var thatFunc = this, thatArg = arguments[0];
  4950. var args = Array.prototype.slice.call(arguments, 1);
  4951. if (typeof thatFunc !== 'function') {
  4952. // closest thing possible to the ECMAScript 5
  4953. // internal IsCallable function
  4954. throw new TypeError('Function.prototype.bind - what is trying to be bound is not callable');
  4955. }
  4956. return function(){
  4957. var funcArgs = args.concat(Array.prototype.slice.call(arguments));
  4958. return thatFunc.apply(thatArg, funcArgs);
  4959. };
  4960. };
  4961. }
  4962. /**
  4963. * ### Changed plugin
  4964. *
  4965. * This plugin adds more information to the `changed.jstree` event. The new data is contained in the `changed` event data property, and contains a lists of `selected` and `deselected` nodes.
  4966. */
  4967. $.jstree.plugins.changed = function (options, parent) {
  4968. var last = [];
  4969. this.trigger = function (ev, data) {
  4970. var i, j;
  4971. if(!data) {
  4972. data = {};
  4973. }
  4974. if(ev.replace('.jstree','') === 'changed') {
  4975. data.changed = { selected : [], deselected : [] };
  4976. var tmp = {};
  4977. for(i = 0, j = last.length; i < j; i++) {
  4978. tmp[last[i]] = 1;
  4979. }
  4980. for(i = 0, j = data.selected.length; i < j; i++) {
  4981. if(!tmp[data.selected[i]]) {
  4982. data.changed.selected.push(data.selected[i]);
  4983. }
  4984. else {
  4985. tmp[data.selected[i]] = 2;
  4986. }
  4987. }
  4988. for(i = 0, j = last.length; i < j; i++) {
  4989. if(tmp[last[i]] === 1) {
  4990. data.changed.deselected.push(last[i]);
  4991. }
  4992. }
  4993. last = data.selected.slice();
  4994. }
  4995. /**
  4996. * triggered when selection changes (the "changed" plugin enhances the original event with more data)
  4997. * @event
  4998. * @name changed.jstree
  4999. * @param {Object} node
  5000. * @param {Object} action the action that caused the selection to change
  5001. * @param {Array} selected the current selection
  5002. * @param {Object} changed an object containing two properties `selected` and `deselected` - both arrays of node IDs, which were selected or deselected since the last changed event
  5003. * @param {Object} event the event (if any) that triggered this changed event
  5004. * @plugin changed
  5005. */
  5006. parent.trigger.call(this, ev, data);
  5007. };
  5008. this.refresh = function (skip_loading, forget_state) {
  5009. last = [];
  5010. return parent.refresh.apply(this, arguments);
  5011. };
  5012. };
  5013. /**
  5014. * ### Checkbox plugin
  5015. *
  5016. * This plugin renders checkbox icons in front of each node, making multiple selection much easier.
  5017. * 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.
  5018. */
  5019. var _i = document.createElement('I');
  5020. _i.className = 'jstree-icon jstree-checkbox';
  5021. _i.setAttribute('role', 'presentation');
  5022. /**
  5023. * stores all defaults for the checkbox plugin
  5024. * @name $.jstree.defaults.checkbox
  5025. * @plugin checkbox
  5026. */
  5027. $.jstree.defaults.checkbox = {
  5028. /**
  5029. * a boolean indicating if checkboxes should be visible (can be changed at a later time using `show_checkboxes()` and `hide_checkboxes`). Defaults to `true`.
  5030. * @name $.jstree.defaults.checkbox.visible
  5031. * @plugin checkbox
  5032. */
  5033. visible : true,
  5034. /**
  5035. * a boolean indicating if checkboxes should cascade down and have an undetermined state. Defaults to `true`.
  5036. * @name $.jstree.defaults.checkbox.three_state
  5037. * @plugin checkbox
  5038. */
  5039. three_state : true,
  5040. /**
  5041. * a boolean indicating if clicking anywhere on the node should act as clicking on the checkbox. Defaults to `true`.
  5042. * @name $.jstree.defaults.checkbox.whole_node
  5043. * @plugin checkbox
  5044. */
  5045. whole_node : true,
  5046. /**
  5047. * a boolean indicating if the selected style of a node should be kept, or removed. Defaults to `true`.
  5048. * @name $.jstree.defaults.checkbox.keep_selected_style
  5049. * @plugin checkbox
  5050. */
  5051. keep_selected_style : true,
  5052. /**
  5053. * This setting controls how cascading and undetermined nodes are applied.
  5054. * 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.
  5055. * If `three_state` is set to `true` this setting is automatically set to 'up+down+undetermined'. Defaults to ''.
  5056. * @name $.jstree.defaults.checkbox.cascade
  5057. * @plugin checkbox
  5058. */
  5059. cascade : '',
  5060. /**
  5061. * 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.
  5062. * @name $.jstree.defaults.checkbox.tie_selection
  5063. * @plugin checkbox
  5064. */
  5065. tie_selection : true,
  5066. /**
  5067. * This setting controls if cascading down affects disabled checkboxes
  5068. * @name $.jstree.defaults.checkbox.cascade_to_disabled
  5069. * @plugin checkbox
  5070. */
  5071. cascade_to_disabled : true,
  5072. /**
  5073. * This setting controls if cascading down affects hidden checkboxes
  5074. * @name $.jstree.defaults.checkbox.cascade_to_hidden
  5075. * @plugin checkbox
  5076. */
  5077. cascade_to_hidden : true
  5078. };
  5079. $.jstree.plugins.checkbox = function (options, parent) {
  5080. this.bind = function () {
  5081. parent.bind.call(this);
  5082. this._data.checkbox.uto = false;
  5083. this._data.checkbox.selected = [];
  5084. if(this.settings.checkbox.three_state) {
  5085. this.settings.checkbox.cascade = 'up+down+undetermined';
  5086. }
  5087. this.element
  5088. .on("init.jstree", function () {
  5089. this._data.checkbox.visible = this.settings.checkbox.visible;
  5090. if(!this.settings.checkbox.keep_selected_style) {
  5091. this.element.addClass('jstree-checkbox-no-clicked');
  5092. }
  5093. if(this.settings.checkbox.tie_selection) {
  5094. this.element.addClass('jstree-checkbox-selection');
  5095. }
  5096. }.bind(this))
  5097. .on("loading.jstree", function () {
  5098. this[ this._data.checkbox.visible ? 'show_checkboxes' : 'hide_checkboxes' ]();
  5099. }.bind(this));
  5100. if(this.settings.checkbox.cascade.indexOf('undetermined') !== -1) {
  5101. this.element
  5102. .on('changed.jstree uncheck_node.jstree check_node.jstree uncheck_all.jstree check_all.jstree move_node.jstree copy_node.jstree redraw.jstree open_node.jstree', function () {
  5103. // only if undetermined is in setting
  5104. if(this._data.checkbox.uto) { clearTimeout(this._data.checkbox.uto); }
  5105. this._data.checkbox.uto = setTimeout(this._undetermined.bind(this), 50);
  5106. }.bind(this));
  5107. }
  5108. if(!this.settings.checkbox.tie_selection) {
  5109. this.element
  5110. .on('model.jstree', function (e, data) {
  5111. var m = this._model.data,
  5112. p = m[data.parent],
  5113. dpc = data.nodes,
  5114. i, j;
  5115. for(i = 0, j = dpc.length; i < j; i++) {
  5116. m[dpc[i]].state.checked = m[dpc[i]].state.checked || (m[dpc[i]].original && m[dpc[i]].original.state && m[dpc[i]].original.state.checked);
  5117. if(m[dpc[i]].state.checked) {
  5118. this._data.checkbox.selected.push(dpc[i]);
  5119. }
  5120. }
  5121. }.bind(this));
  5122. }
  5123. if(this.settings.checkbox.cascade.indexOf('up') !== -1 || this.settings.checkbox.cascade.indexOf('down') !== -1) {
  5124. this.element
  5125. .on('model.jstree', function (e, data) {
  5126. var m = this._model.data,
  5127. p = m[data.parent],
  5128. dpc = data.nodes,
  5129. chd = [],
  5130. c, i, j, k, l, tmp, s = this.settings.checkbox.cascade, t = this.settings.checkbox.tie_selection;
  5131. if(s.indexOf('down') !== -1) {
  5132. // apply down
  5133. if(p.state[ t ? 'selected' : 'checked' ]) {
  5134. for(i = 0, j = dpc.length; i < j; i++) {
  5135. m[dpc[i]].state[ t ? 'selected' : 'checked' ] = true;
  5136. }
  5137. this._data[ t ? 'core' : 'checkbox' ].selected = this._data[ t ? 'core' : 'checkbox' ].selected.concat(dpc);
  5138. }
  5139. else {
  5140. for(i = 0, j = dpc.length; i < j; i++) {
  5141. if(m[dpc[i]].state[ t ? 'selected' : 'checked' ]) {
  5142. for(k = 0, l = m[dpc[i]].children_d.length; k < l; k++) {
  5143. m[m[dpc[i]].children_d[k]].state[ t ? 'selected' : 'checked' ] = true;
  5144. }
  5145. this._data[ t ? 'core' : 'checkbox' ].selected = this._data[ t ? 'core' : 'checkbox' ].selected.concat(m[dpc[i]].children_d);
  5146. }
  5147. }
  5148. }
  5149. }
  5150. if(s.indexOf('up') !== -1) {
  5151. // apply up
  5152. for(i = 0, j = p.children_d.length; i < j; i++) {
  5153. if(!m[p.children_d[i]].children.length) {
  5154. chd.push(m[p.children_d[i]].parent);
  5155. }
  5156. }
  5157. chd = $.vakata.array_unique(chd);
  5158. for(k = 0, l = chd.length; k < l; k++) {
  5159. p = m[chd[k]];
  5160. while(p && p.id !== $.jstree.root) {
  5161. c = 0;
  5162. for(i = 0, j = p.children.length; i < j; i++) {
  5163. c += m[p.children[i]].state[ t ? 'selected' : 'checked' ];
  5164. }
  5165. if(c === j) {
  5166. p.state[ t ? 'selected' : 'checked' ] = true;
  5167. this._data[ t ? 'core' : 'checkbox' ].selected.push(p.id);
  5168. tmp = this.get_node(p, true);
  5169. if(tmp && tmp.length) {
  5170. tmp.children('.jstree-anchor').attr('aria-selected', true).addClass( t ? 'jstree-clicked' : 'jstree-checked');
  5171. }
  5172. }
  5173. else {
  5174. break;
  5175. }
  5176. p = this.get_node(p.parent);
  5177. }
  5178. }
  5179. }
  5180. this._data[ t ? 'core' : 'checkbox' ].selected = $.vakata.array_unique(this._data[ t ? 'core' : 'checkbox' ].selected);
  5181. }.bind(this))
  5182. .on(this.settings.checkbox.tie_selection ? 'select_node.jstree' : 'check_node.jstree', function (e, data) {
  5183. var self = this,
  5184. obj = data.node,
  5185. m = this._model.data,
  5186. par = this.get_node(obj.parent),
  5187. i, j, c, tmp, s = this.settings.checkbox.cascade, t = this.settings.checkbox.tie_selection,
  5188. sel = {}, cur = this._data[ t ? 'core' : 'checkbox' ].selected;
  5189. for (i = 0, j = cur.length; i < j; i++) {
  5190. sel[cur[i]] = true;
  5191. }
  5192. // apply down
  5193. if(s.indexOf('down') !== -1) {
  5194. //this._data[ t ? 'core' : 'checkbox' ].selected = $.vakata.array_unique(this._data[ t ? 'core' : 'checkbox' ].selected.concat(obj.children_d));
  5195. var selectedIds = this._cascade_new_checked_state(obj.id, true);
  5196. var temp = obj.children_d.concat(obj.id);
  5197. for (i = 0, j = temp.length; i < j; i++) {
  5198. if (selectedIds.indexOf(temp[i]) > -1) {
  5199. sel[temp[i]] = true;
  5200. }
  5201. else {
  5202. delete sel[temp[i]];
  5203. }
  5204. }
  5205. }
  5206. // apply up
  5207. if(s.indexOf('up') !== -1) {
  5208. while(par && par.id !== $.jstree.root) {
  5209. c = 0;
  5210. for(i = 0, j = par.children.length; i < j; i++) {
  5211. c += m[par.children[i]].state[ t ? 'selected' : 'checked' ];
  5212. }
  5213. if(c === j) {
  5214. par.state[ t ? 'selected' : 'checked' ] = true;
  5215. sel[par.id] = true;
  5216. //this._data[ t ? 'core' : 'checkbox' ].selected.push(par.id);
  5217. tmp = this.get_node(par, true);
  5218. if(tmp && tmp.length) {
  5219. tmp.children('.jstree-anchor').attr('aria-selected', true).addClass(t ? 'jstree-clicked' : 'jstree-checked');
  5220. }
  5221. }
  5222. else {
  5223. break;
  5224. }
  5225. par = this.get_node(par.parent);
  5226. }
  5227. }
  5228. cur = [];
  5229. for (i in sel) {
  5230. if (sel.hasOwnProperty(i)) {
  5231. cur.push(i);
  5232. }
  5233. }
  5234. this._data[ t ? 'core' : 'checkbox' ].selected = cur;
  5235. }.bind(this))
  5236. .on(this.settings.checkbox.tie_selection ? 'deselect_all.jstree' : 'uncheck_all.jstree', function (e, data) {
  5237. var obj = this.get_node($.jstree.root),
  5238. m = this._model.data,
  5239. i, j, tmp;
  5240. for(i = 0, j = obj.children_d.length; i < j; i++) {
  5241. tmp = m[obj.children_d[i]];
  5242. if(tmp && tmp.original && tmp.original.state && tmp.original.state.undetermined) {
  5243. tmp.original.state.undetermined = false;
  5244. }
  5245. }
  5246. }.bind(this))
  5247. .on(this.settings.checkbox.tie_selection ? 'deselect_node.jstree' : 'uncheck_node.jstree', function (e, data) {
  5248. var self = this,
  5249. obj = data.node,
  5250. dom = this.get_node(obj, true),
  5251. i, j, tmp, s = this.settings.checkbox.cascade, t = this.settings.checkbox.tie_selection,
  5252. cur = this._data[ t ? 'core' : 'checkbox' ].selected, sel = {},
  5253. stillSelectedIds = [],
  5254. allIds = obj.children_d.concat(obj.id);
  5255. // apply down
  5256. if(s.indexOf('down') !== -1) {
  5257. var selectedIds = this._cascade_new_checked_state(obj.id, false);
  5258. cur = $.vakata.array_filter(cur, function(id) {
  5259. return allIds.indexOf(id) === -1 || selectedIds.indexOf(id) > -1;
  5260. });
  5261. }
  5262. // only apply up if cascade up is enabled and if this node is not selected
  5263. // (if all child nodes are disabled and cascade_to_disabled === false then this node will till be selected).
  5264. if(s.indexOf('up') !== -1 && cur.indexOf(obj.id) === -1) {
  5265. for(i = 0, j = obj.parents.length; i < j; i++) {
  5266. tmp = this._model.data[obj.parents[i]];
  5267. tmp.state[ t ? 'selected' : 'checked' ] = false;
  5268. if(tmp && tmp.original && tmp.original.state && tmp.original.state.undetermined) {
  5269. tmp.original.state.undetermined = false;
  5270. }
  5271. tmp = this.get_node(obj.parents[i], true);
  5272. if(tmp && tmp.length) {
  5273. tmp.children('.jstree-anchor').attr('aria-selected', false).removeClass(t ? 'jstree-clicked' : 'jstree-checked');
  5274. }
  5275. }
  5276. cur = $.vakata.array_filter(cur, function(id) {
  5277. return obj.parents.indexOf(id) === -1;
  5278. });
  5279. }
  5280. this._data[ t ? 'core' : 'checkbox' ].selected = cur;
  5281. }.bind(this));
  5282. }
  5283. if(this.settings.checkbox.cascade.indexOf('up') !== -1) {
  5284. this.element
  5285. .on('delete_node.jstree', function (e, data) {
  5286. // apply up (whole handler)
  5287. var p = this.get_node(data.parent),
  5288. m = this._model.data,
  5289. i, j, c, tmp, t = this.settings.checkbox.tie_selection;
  5290. while(p && p.id !== $.jstree.root && !p.state[ t ? 'selected' : 'checked' ]) {
  5291. c = 0;
  5292. for(i = 0, j = p.children.length; i < j; i++) {
  5293. c += m[p.children[i]].state[ t ? 'selected' : 'checked' ];
  5294. }
  5295. if(j > 0 && c === j) {
  5296. p.state[ t ? 'selected' : 'checked' ] = true;
  5297. this._data[ t ? 'core' : 'checkbox' ].selected.push(p.id);
  5298. tmp = this.get_node(p, true);
  5299. if(tmp && tmp.length) {
  5300. tmp.children('.jstree-anchor').attr('aria-selected', true).addClass(t ? 'jstree-clicked' : 'jstree-checked');
  5301. }
  5302. }
  5303. else {
  5304. break;
  5305. }
  5306. p = this.get_node(p.parent);
  5307. }
  5308. }.bind(this))
  5309. .on('move_node.jstree', function (e, data) {
  5310. // apply up (whole handler)
  5311. var is_multi = data.is_multi,
  5312. old_par = data.old_parent,
  5313. new_par = this.get_node(data.parent),
  5314. m = this._model.data,
  5315. p, c, i, j, tmp, t = this.settings.checkbox.tie_selection;
  5316. if(!is_multi) {
  5317. p = this.get_node(old_par);
  5318. while(p && p.id !== $.jstree.root && !p.state[ t ? 'selected' : 'checked' ]) {
  5319. c = 0;
  5320. for(i = 0, j = p.children.length; i < j; i++) {
  5321. c += m[p.children[i]].state[ t ? 'selected' : 'checked' ];
  5322. }
  5323. if(j > 0 && c === j) {
  5324. p.state[ t ? 'selected' : 'checked' ] = true;
  5325. this._data[ t ? 'core' : 'checkbox' ].selected.push(p.id);
  5326. tmp = this.get_node(p, true);
  5327. if(tmp && tmp.length) {
  5328. tmp.children('.jstree-anchor').attr('aria-selected', true).addClass(t ? 'jstree-clicked' : 'jstree-checked');
  5329. }
  5330. }
  5331. else {
  5332. break;
  5333. }
  5334. p = this.get_node(p.parent);
  5335. }
  5336. }
  5337. p = new_par;
  5338. while(p && p.id !== $.jstree.root) {
  5339. c = 0;
  5340. for(i = 0, j = p.children.length; i < j; i++) {
  5341. c += m[p.children[i]].state[ t ? 'selected' : 'checked' ];
  5342. }
  5343. if(c === j) {
  5344. if(!p.state[ t ? 'selected' : 'checked' ]) {
  5345. p.state[ t ? 'selected' : 'checked' ] = true;
  5346. this._data[ t ? 'core' : 'checkbox' ].selected.push(p.id);
  5347. tmp = this.get_node(p, true);
  5348. if(tmp && tmp.length) {
  5349. tmp.children('.jstree-anchor').attr('aria-selected', true).addClass(t ? 'jstree-clicked' : 'jstree-checked');
  5350. }
  5351. }
  5352. }
  5353. else {
  5354. if(p.state[ t ? 'selected' : 'checked' ]) {
  5355. p.state[ t ? 'selected' : 'checked' ] = false;
  5356. this._data[ t ? 'core' : 'checkbox' ].selected = $.vakata.array_remove_item(this._data[ t ? 'core' : 'checkbox' ].selected, p.id);
  5357. tmp = this.get_node(p, true);
  5358. if(tmp && tmp.length) {
  5359. tmp.children('.jstree-anchor').attr('aria-selected', false).removeClass(t ? 'jstree-clicked' : 'jstree-checked');
  5360. }
  5361. }
  5362. else {
  5363. break;
  5364. }
  5365. }
  5366. p = this.get_node(p.parent);
  5367. }
  5368. }.bind(this));
  5369. }
  5370. };
  5371. /**
  5372. * get an array of all nodes whose state is "undetermined"
  5373. * @name get_undetermined([full])
  5374. * @param {boolean} full: if set to `true` the returned array will consist of the full node objects, otherwise - only IDs will be returned
  5375. * @return {Array}
  5376. * @plugin checkbox
  5377. */
  5378. this.get_undetermined = function (full) {
  5379. if (this.settings.checkbox.cascade.indexOf('undetermined') === -1) {
  5380. return [];
  5381. }
  5382. var i, j, k, l, o = {}, m = this._model.data, t = this.settings.checkbox.tie_selection, s = this._data[ t ? 'core' : 'checkbox' ].selected, p = [], tt = this, r = [];
  5383. for(i = 0, j = s.length; i < j; i++) {
  5384. if(m[s[i]] && m[s[i]].parents) {
  5385. for(k = 0, l = m[s[i]].parents.length; k < l; k++) {
  5386. if(o[m[s[i]].parents[k]] !== undefined) {
  5387. break;
  5388. }
  5389. if(m[s[i]].parents[k] !== $.jstree.root) {
  5390. o[m[s[i]].parents[k]] = true;
  5391. p.push(m[s[i]].parents[k]);
  5392. }
  5393. }
  5394. }
  5395. }
  5396. // attempt for server side undetermined state
  5397. this.element.find('.jstree-closed').not(':has(.jstree-children)')
  5398. .each(function () {
  5399. var tmp = tt.get_node(this), tmp2;
  5400. if(!tmp) { return; }
  5401. if(!tmp.state.loaded) {
  5402. if(tmp.original && tmp.original.state && tmp.original.state.undetermined && tmp.original.state.undetermined === true) {
  5403. if(o[tmp.id] === undefined && tmp.id !== $.jstree.root) {
  5404. o[tmp.id] = true;
  5405. p.push(tmp.id);
  5406. }
  5407. for(k = 0, l = tmp.parents.length; k < l; k++) {
  5408. if(o[tmp.parents[k]] === undefined && tmp.parents[k] !== $.jstree.root) {
  5409. o[tmp.parents[k]] = true;
  5410. p.push(tmp.parents[k]);
  5411. }
  5412. }
  5413. }
  5414. }
  5415. else {
  5416. for(i = 0, j = tmp.children_d.length; i < j; i++) {
  5417. tmp2 = m[tmp.children_d[i]];
  5418. if(!tmp2.state.loaded && tmp2.original && tmp2.original.state && tmp2.original.state.undetermined && tmp2.original.state.undetermined === true) {
  5419. if(o[tmp2.id] === undefined && tmp2.id !== $.jstree.root) {
  5420. o[tmp2.id] = true;
  5421. p.push(tmp2.id);
  5422. }
  5423. for(k = 0, l = tmp2.parents.length; k < l; k++) {
  5424. if(o[tmp2.parents[k]] === undefined && tmp2.parents[k] !== $.jstree.root) {
  5425. o[tmp2.parents[k]] = true;
  5426. p.push(tmp2.parents[k]);
  5427. }
  5428. }
  5429. }
  5430. }
  5431. }
  5432. });
  5433. for (i = 0, j = p.length; i < j; i++) {
  5434. if(!m[p[i]].state[ t ? 'selected' : 'checked' ]) {
  5435. r.push(full ? m[p[i]] : p[i]);
  5436. }
  5437. }
  5438. return r;
  5439. };
  5440. /**
  5441. * set the undetermined state where and if necessary. Used internally.
  5442. * @private
  5443. * @name _undetermined()
  5444. * @plugin checkbox
  5445. */
  5446. this._undetermined = function () {
  5447. if(this.element === null) { return; }
  5448. var p = this.get_undetermined(false), i, j, s;
  5449. this.element.find('.jstree-undetermined').removeClass('jstree-undetermined');
  5450. for (i = 0, j = p.length; i < j; i++) {
  5451. s = this.get_node(p[i], true);
  5452. if(s && s.length) {
  5453. s.children('.jstree-anchor').children('.jstree-checkbox').addClass('jstree-undetermined');
  5454. }
  5455. }
  5456. };
  5457. this.redraw_node = function(obj, deep, is_callback, force_render) {
  5458. obj = parent.redraw_node.apply(this, arguments);
  5459. if(obj) {
  5460. var i, j, tmp = null, icon = null;
  5461. for(i = 0, j = obj.childNodes.length; i < j; i++) {
  5462. if(obj.childNodes[i] && obj.childNodes[i].className && obj.childNodes[i].className.indexOf("jstree-anchor") !== -1) {
  5463. tmp = obj.childNodes[i];
  5464. break;
  5465. }
  5466. }
  5467. if(tmp) {
  5468. if(!this.settings.checkbox.tie_selection && this._model.data[obj.id].state.checked) { tmp.className += ' jstree-checked'; }
  5469. icon = _i.cloneNode(false);
  5470. if(this._model.data[obj.id].state.checkbox_disabled) { icon.className += ' jstree-checkbox-disabled'; }
  5471. tmp.insertBefore(icon, tmp.childNodes[0]);
  5472. }
  5473. }
  5474. if(!is_callback && this.settings.checkbox.cascade.indexOf('undetermined') !== -1) {
  5475. if(this._data.checkbox.uto) { clearTimeout(this._data.checkbox.uto); }
  5476. this._data.checkbox.uto = setTimeout(this._undetermined.bind(this), 50);
  5477. }
  5478. return obj;
  5479. };
  5480. /**
  5481. * show the node checkbox icons
  5482. * @name show_checkboxes()
  5483. * @plugin checkbox
  5484. */
  5485. this.show_checkboxes = function () { this._data.core.themes.checkboxes = true; this.get_container_ul().removeClass("jstree-no-checkboxes"); };
  5486. /**
  5487. * hide the node checkbox icons
  5488. * @name hide_checkboxes()
  5489. * @plugin checkbox
  5490. */
  5491. this.hide_checkboxes = function () { this._data.core.themes.checkboxes = false; this.get_container_ul().addClass("jstree-no-checkboxes"); };
  5492. /**
  5493. * toggle the node icons
  5494. * @name toggle_checkboxes()
  5495. * @plugin checkbox
  5496. */
  5497. this.toggle_checkboxes = function () { if(this._data.core.themes.checkboxes) { this.hide_checkboxes(); } else { this.show_checkboxes(); } };
  5498. /**
  5499. * checks if a node is in an undetermined state
  5500. * @name is_undetermined(obj)
  5501. * @param {mixed} obj
  5502. * @return {Boolean}
  5503. */
  5504. this.is_undetermined = function (obj) {
  5505. obj = this.get_node(obj);
  5506. 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;
  5507. if(!obj || obj.state[ t ? 'selected' : 'checked' ] === true || s.indexOf('undetermined') === -1 || (s.indexOf('down') === -1 && s.indexOf('up') === -1)) {
  5508. return false;
  5509. }
  5510. if(!obj.state.loaded && obj.original.state.undetermined === true) {
  5511. return true;
  5512. }
  5513. for(i = 0, j = obj.children_d.length; i < j; i++) {
  5514. if($.inArray(obj.children_d[i], d) !== -1 || (!m[obj.children_d[i]].state.loaded && m[obj.children_d[i]].original.state.undetermined)) {
  5515. return true;
  5516. }
  5517. }
  5518. return false;
  5519. };
  5520. /**
  5521. * disable a node's checkbox
  5522. * @name disable_checkbox(obj)
  5523. * @param {mixed} obj an array can be used too
  5524. * @trigger disable_checkbox.jstree
  5525. * @plugin checkbox
  5526. */
  5527. this.disable_checkbox = function (obj) {
  5528. var t1, t2, dom;
  5529. if($.vakata.is_array(obj)) {
  5530. obj = obj.slice();
  5531. for(t1 = 0, t2 = obj.length; t1 < t2; t1++) {
  5532. this.disable_checkbox(obj[t1]);
  5533. }
  5534. return true;
  5535. }
  5536. obj = this.get_node(obj);
  5537. if(!obj || obj.id === $.jstree.root) {
  5538. return false;
  5539. }
  5540. dom = this.get_node(obj, true);
  5541. if(!obj.state.checkbox_disabled) {
  5542. obj.state.checkbox_disabled = true;
  5543. if(dom && dom.length) {
  5544. dom.children('.jstree-anchor').children('.jstree-checkbox').addClass('jstree-checkbox-disabled');
  5545. }
  5546. /**
  5547. * triggered when an node's checkbox is disabled
  5548. * @event
  5549. * @name disable_checkbox.jstree
  5550. * @param {Object} node
  5551. * @plugin checkbox
  5552. */
  5553. this.trigger('disable_checkbox', { 'node' : obj });
  5554. }
  5555. };
  5556. /**
  5557. * enable a node's checkbox
  5558. * @name enable_checkbox(obj)
  5559. * @param {mixed} obj an array can be used too
  5560. * @trigger enable_checkbox.jstree
  5561. * @plugin checkbox
  5562. */
  5563. this.enable_checkbox = function (obj) {
  5564. var t1, t2, dom;
  5565. if($.vakata.is_array(obj)) {
  5566. obj = obj.slice();
  5567. for(t1 = 0, t2 = obj.length; t1 < t2; t1++) {
  5568. this.enable_checkbox(obj[t1]);
  5569. }
  5570. return true;
  5571. }
  5572. obj = this.get_node(obj);
  5573. if(!obj || obj.id === $.jstree.root) {
  5574. return false;
  5575. }
  5576. dom = this.get_node(obj, true);
  5577. if(obj.state.checkbox_disabled) {
  5578. obj.state.checkbox_disabled = false;
  5579. if(dom && dom.length) {
  5580. dom.children('.jstree-anchor').children('.jstree-checkbox').removeClass('jstree-checkbox-disabled');
  5581. }
  5582. /**
  5583. * triggered when an node's checkbox is enabled
  5584. * @event
  5585. * @name enable_checkbox.jstree
  5586. * @param {Object} node
  5587. * @plugin checkbox
  5588. */
  5589. this.trigger('enable_checkbox', { 'node' : obj });
  5590. }
  5591. };
  5592. this.activate_node = function (obj, e) {
  5593. if($(e.target).hasClass('jstree-checkbox-disabled')) {
  5594. return false;
  5595. }
  5596. if(this.settings.checkbox.tie_selection && (this.settings.checkbox.whole_node || $(e.target).hasClass('jstree-checkbox'))) {
  5597. e.ctrlKey = true;
  5598. }
  5599. if(this.settings.checkbox.tie_selection || (!this.settings.checkbox.whole_node && !$(e.target).hasClass('jstree-checkbox'))) {
  5600. return parent.activate_node.call(this, obj, e);
  5601. }
  5602. if(this.is_disabled(obj)) {
  5603. return false;
  5604. }
  5605. if(this.is_checked(obj)) {
  5606. this.uncheck_node(obj, e);
  5607. }
  5608. else {
  5609. this.check_node(obj, e);
  5610. }
  5611. this.trigger('activate_node', { 'node' : this.get_node(obj) });
  5612. };
  5613. this.delete_node = function (obj) {
  5614. if(this.settings.checkbox.tie_selection || $.vakata.is_array(obj)) {
  5615. return parent.delete_node.call(this, obj);
  5616. }
  5617. var tmp, k, l, c = false;
  5618. obj = this.get_node(obj);
  5619. if(!obj || obj.id === $.jstree.root) { return false; }
  5620. tmp = obj.children_d.concat([]);
  5621. tmp.push(obj.id);
  5622. for(k = 0, l = tmp.length; k < l; k++) {
  5623. if(this._model.data[tmp[k]].state.checked) {
  5624. c = true;
  5625. break;
  5626. }
  5627. }
  5628. if (c) {
  5629. this._data.checkbox.selected = $.vakata.array_filter(this._data.checkbox.selected, function (v) {
  5630. return $.inArray(v, tmp) === -1;
  5631. });
  5632. }
  5633. return parent.delete_node.call(this, obj);
  5634. };
  5635. /**
  5636. * Cascades checked state to a node and all its descendants. This function does NOT affect hidden and disabled nodes (or their descendants).
  5637. * However if these unaffected nodes are already selected their ids will be included in the returned array.
  5638. * @private
  5639. * @name _cascade_new_checked_state(id, checkedState)
  5640. * @param {string} id the node ID
  5641. * @param {bool} checkedState should the nodes be checked or not
  5642. * @returns {Array} Array of all node id's (in this tree branch) that are checked.
  5643. */
  5644. this._cascade_new_checked_state = function (id, checkedState) {
  5645. var self = this;
  5646. var t = this.settings.checkbox.tie_selection;
  5647. var node = this._model.data[id];
  5648. var selectedNodeIds = [];
  5649. var selectedChildrenIds = [], i, j, selectedChildIds;
  5650. if (
  5651. (this.settings.checkbox.cascade_to_disabled || !node.state.disabled) &&
  5652. (this.settings.checkbox.cascade_to_hidden || !node.state.hidden)
  5653. ) {
  5654. //First try and check/uncheck the children
  5655. if (node.children) {
  5656. for (i = 0, j = node.children.length; i < j; i++) {
  5657. var childId = node.children[i];
  5658. selectedChildIds = self._cascade_new_checked_state(childId, checkedState);
  5659. selectedNodeIds = selectedNodeIds.concat(selectedChildIds);
  5660. if (selectedChildIds.indexOf(childId) > -1) {
  5661. selectedChildrenIds.push(childId);
  5662. }
  5663. }
  5664. }
  5665. var dom = self.get_node(node, true);
  5666. //A node's state is undetermined if some but not all of it's children are checked/selected .
  5667. var undetermined = selectedChildrenIds.length > 0 && selectedChildrenIds.length < node.children.length;
  5668. if(node.original && node.original.state && node.original.state.undetermined) {
  5669. node.original.state.undetermined = undetermined;
  5670. }
  5671. //If a node is undetermined then remove selected class
  5672. if (undetermined) {
  5673. node.state[ t ? 'selected' : 'checked' ] = false;
  5674. dom.children('.jstree-anchor').attr('aria-selected', false).removeClass(t ? 'jstree-clicked' : 'jstree-checked');
  5675. }
  5676. //Otherwise, if the checkedState === true (i.e. the node is being checked now) and all of the node's children are checked (if it has any children),
  5677. //check the node and style it correctly.
  5678. else if (checkedState && selectedChildrenIds.length === node.children.length) {
  5679. node.state[ t ? 'selected' : 'checked' ] = checkedState;
  5680. selectedNodeIds.push(node.id);
  5681. dom.children('.jstree-anchor').attr('aria-selected', true).addClass(t ? 'jstree-clicked' : 'jstree-checked');
  5682. }
  5683. else {
  5684. node.state[ t ? 'selected' : 'checked' ] = false;
  5685. dom.children('.jstree-anchor').attr('aria-selected', false).removeClass(t ? 'jstree-clicked' : 'jstree-checked');
  5686. }
  5687. }
  5688. else {
  5689. selectedChildIds = this.get_checked_descendants(id);
  5690. if (node.state[ t ? 'selected' : 'checked' ]) {
  5691. selectedChildIds.push(node.id);
  5692. }
  5693. selectedNodeIds = selectedNodeIds.concat(selectedChildIds);
  5694. }
  5695. return selectedNodeIds;
  5696. };
  5697. /**
  5698. * Gets ids of nodes selected in branch (of tree) specified by id (does not include the node specified by id)
  5699. * @name get_checked_descendants(obj)
  5700. * @param {string} id the node ID
  5701. * @return {Array} array of IDs
  5702. * @plugin checkbox
  5703. */
  5704. this.get_checked_descendants = function (id) {
  5705. var self = this;
  5706. var t = self.settings.checkbox.tie_selection;
  5707. var node = self._model.data[id];
  5708. return $.vakata.array_filter(node.children_d, function(_id) {
  5709. return self._model.data[_id].state[ t ? 'selected' : 'checked' ];
  5710. });
  5711. };
  5712. /**
  5713. * check a node (only if tie_selection in checkbox settings is false, otherwise select_node will be called internally)
  5714. * @name check_node(obj)
  5715. * @param {mixed} obj an array can be used to check multiple nodes
  5716. * @trigger check_node.jstree
  5717. * @plugin checkbox
  5718. */
  5719. this.check_node = function (obj, e) {
  5720. if(this.settings.checkbox.tie_selection) { return this.select_node(obj, false, true, e); }
  5721. var dom, t1, t2, th;
  5722. if($.vakata.is_array(obj)) {
  5723. obj = obj.slice();
  5724. for(t1 = 0, t2 = obj.length; t1 < t2; t1++) {
  5725. this.check_node(obj[t1], e);
  5726. }
  5727. return true;
  5728. }
  5729. obj = this.get_node(obj);
  5730. if(!obj || obj.id === $.jstree.root) {
  5731. return false;
  5732. }
  5733. dom = this.get_node(obj, true);
  5734. if(!obj.state.checked) {
  5735. obj.state.checked = true;
  5736. this._data.checkbox.selected.push(obj.id);
  5737. if(dom && dom.length) {
  5738. dom.children('.jstree-anchor').addClass('jstree-checked');
  5739. }
  5740. /**
  5741. * triggered when an node is checked (only if tie_selection in checkbox settings is false)
  5742. * @event
  5743. * @name check_node.jstree
  5744. * @param {Object} node
  5745. * @param {Array} selected the current selection
  5746. * @param {Object} event the event (if any) that triggered this check_node
  5747. * @plugin checkbox
  5748. */
  5749. this.trigger('check_node', { 'node' : obj, 'selected' : this._data.checkbox.selected, 'event' : e });
  5750. }
  5751. };
  5752. /**
  5753. * uncheck a node (only if tie_selection in checkbox settings is false, otherwise deselect_node will be called internally)
  5754. * @name uncheck_node(obj)
  5755. * @param {mixed} obj an array can be used to uncheck multiple nodes
  5756. * @trigger uncheck_node.jstree
  5757. * @plugin checkbox
  5758. */
  5759. this.uncheck_node = function (obj, e) {
  5760. if(this.settings.checkbox.tie_selection) { return this.deselect_node(obj, false, e); }
  5761. var t1, t2, dom;
  5762. if($.vakata.is_array(obj)) {
  5763. obj = obj.slice();
  5764. for(t1 = 0, t2 = obj.length; t1 < t2; t1++) {
  5765. this.uncheck_node(obj[t1], e);
  5766. }
  5767. return true;
  5768. }
  5769. obj = this.get_node(obj);
  5770. if(!obj || obj.id === $.jstree.root) {
  5771. return false;
  5772. }
  5773. dom = this.get_node(obj, true);
  5774. if(obj.state.checked) {
  5775. obj.state.checked = false;
  5776. this._data.checkbox.selected = $.vakata.array_remove_item(this._data.checkbox.selected, obj.id);
  5777. if(dom.length) {
  5778. dom.children('.jstree-anchor').removeClass('jstree-checked');
  5779. }
  5780. /**
  5781. * triggered when an node is unchecked (only if tie_selection in checkbox settings is false)
  5782. * @event
  5783. * @name uncheck_node.jstree
  5784. * @param {Object} node
  5785. * @param {Array} selected the current selection
  5786. * @param {Object} event the event (if any) that triggered this uncheck_node
  5787. * @plugin checkbox
  5788. */
  5789. this.trigger('uncheck_node', { 'node' : obj, 'selected' : this._data.checkbox.selected, 'event' : e });
  5790. }
  5791. };
  5792. /**
  5793. * checks all nodes in the tree (only if tie_selection in checkbox settings is false, otherwise select_all will be called internally)
  5794. * @name check_all()
  5795. * @trigger check_all.jstree, changed.jstree
  5796. * @plugin checkbox
  5797. */
  5798. this.check_all = function () {
  5799. if(this.settings.checkbox.tie_selection) { return this.select_all(); }
  5800. var tmp = this._data.checkbox.selected.concat([]), i, j;
  5801. this._data.checkbox.selected = this._model.data[$.jstree.root].children_d.concat();
  5802. for(i = 0, j = this._data.checkbox.selected.length; i < j; i++) {
  5803. if(this._model.data[this._data.checkbox.selected[i]]) {
  5804. this._model.data[this._data.checkbox.selected[i]].state.checked = true;
  5805. }
  5806. }
  5807. this.redraw(true);
  5808. /**
  5809. * triggered when all nodes are checked (only if tie_selection in checkbox settings is false)
  5810. * @event
  5811. * @name check_all.jstree
  5812. * @param {Array} selected the current selection
  5813. * @plugin checkbox
  5814. */
  5815. this.trigger('check_all', { 'selected' : this._data.checkbox.selected });
  5816. };
  5817. /**
  5818. * uncheck all checked nodes (only if tie_selection in checkbox settings is false, otherwise deselect_all will be called internally)
  5819. * @name uncheck_all()
  5820. * @trigger uncheck_all.jstree
  5821. * @plugin checkbox
  5822. */
  5823. this.uncheck_all = function () {
  5824. if(this.settings.checkbox.tie_selection) { return this.deselect_all(); }
  5825. var tmp = this._data.checkbox.selected.concat([]), i, j;
  5826. for(i = 0, j = this._data.checkbox.selected.length; i < j; i++) {
  5827. if(this._model.data[this._data.checkbox.selected[i]]) {
  5828. this._model.data[this._data.checkbox.selected[i]].state.checked = false;
  5829. }
  5830. }
  5831. this._data.checkbox.selected = [];
  5832. this.element.find('.jstree-checked').removeClass('jstree-checked');
  5833. /**
  5834. * triggered when all nodes are unchecked (only if tie_selection in checkbox settings is false)
  5835. * @event
  5836. * @name uncheck_all.jstree
  5837. * @param {Object} node the previous selection
  5838. * @param {Array} selected the current selection
  5839. * @plugin checkbox
  5840. */
  5841. this.trigger('uncheck_all', { 'selected' : this._data.checkbox.selected, 'node' : tmp });
  5842. };
  5843. /**
  5844. * checks if a node is checked (if tie_selection is on in the settings this function will return the same as is_selected)
  5845. * @name is_checked(obj)
  5846. * @param {mixed} obj
  5847. * @return {Boolean}
  5848. * @plugin checkbox
  5849. */
  5850. this.is_checked = function (obj) {
  5851. if(this.settings.checkbox.tie_selection) { return this.is_selected(obj); }
  5852. obj = this.get_node(obj);
  5853. if(!obj || obj.id === $.jstree.root) { return false; }
  5854. return obj.state.checked;
  5855. };
  5856. /**
  5857. * get an array of all checked nodes (if tie_selection is on in the settings this function will return the same as get_selected)
  5858. * @name get_checked([full])
  5859. * @param {mixed} full if set to `true` the returned array will consist of the full node objects, otherwise - only IDs will be returned
  5860. * @return {Array}
  5861. * @plugin checkbox
  5862. */
  5863. this.get_checked = function (full) {
  5864. if(this.settings.checkbox.tie_selection) { return this.get_selected(full); }
  5865. return full ? $.map(this._data.checkbox.selected, function (i) { return this.get_node(i); }.bind(this)) : this._data.checkbox.selected.slice();
  5866. };
  5867. /**
  5868. * 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)
  5869. * @name get_top_checked([full])
  5870. * @param {mixed} full if set to `true` the returned array will consist of the full node objects, otherwise - only IDs will be returned
  5871. * @return {Array}
  5872. * @plugin checkbox
  5873. */
  5874. this.get_top_checked = function (full) {
  5875. if(this.settings.checkbox.tie_selection) { return this.get_top_selected(full); }
  5876. var tmp = this.get_checked(true),
  5877. obj = {}, i, j, k, l;
  5878. for(i = 0, j = tmp.length; i < j; i++) {
  5879. obj[tmp[i].id] = tmp[i];
  5880. }
  5881. for(i = 0, j = tmp.length; i < j; i++) {
  5882. for(k = 0, l = tmp[i].children_d.length; k < l; k++) {
  5883. if(obj[tmp[i].children_d[k]]) {
  5884. delete obj[tmp[i].children_d[k]];
  5885. }
  5886. }
  5887. }
  5888. tmp = [];
  5889. for(i in obj) {
  5890. if(obj.hasOwnProperty(i)) {
  5891. tmp.push(i);
  5892. }
  5893. }
  5894. return full ? $.map(tmp, function (i) { return this.get_node(i); }.bind(this)) : tmp;
  5895. };
  5896. /**
  5897. * 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)
  5898. * @name get_bottom_checked([full])
  5899. * @param {mixed} full if set to `true` the returned array will consist of the full node objects, otherwise - only IDs will be returned
  5900. * @return {Array}
  5901. * @plugin checkbox
  5902. */
  5903. this.get_bottom_checked = function (full) {
  5904. if(this.settings.checkbox.tie_selection) { return this.get_bottom_selected(full); }
  5905. var tmp = this.get_checked(true),
  5906. obj = [], i, j;
  5907. for(i = 0, j = tmp.length; i < j; i++) {
  5908. if(!tmp[i].children.length) {
  5909. obj.push(tmp[i].id);
  5910. }
  5911. }
  5912. return full ? $.map(obj, function (i) { return this.get_node(i); }.bind(this)) : obj;
  5913. };
  5914. this.load_node = function (obj, callback) {
  5915. var k, l, i, j, c, tmp;
  5916. if(!$.vakata.is_array(obj) && !this.settings.checkbox.tie_selection) {
  5917. tmp = this.get_node(obj);
  5918. if(tmp && tmp.state.loaded) {
  5919. for(k = 0, l = tmp.children_d.length; k < l; k++) {
  5920. if(this._model.data[tmp.children_d[k]].state.checked) {
  5921. c = true;
  5922. this._data.checkbox.selected = $.vakata.array_remove_item(this._data.checkbox.selected, tmp.children_d[k]);
  5923. }
  5924. }
  5925. }
  5926. }
  5927. return parent.load_node.apply(this, arguments);
  5928. };
  5929. this.get_state = function () {
  5930. var state = parent.get_state.apply(this, arguments);
  5931. if(this.settings.checkbox.tie_selection) { return state; }
  5932. state.checkbox = this._data.checkbox.selected.slice();
  5933. return state;
  5934. };
  5935. this.set_state = function (state, callback) {
  5936. var res = parent.set_state.apply(this, arguments);
  5937. if(res && state.checkbox) {
  5938. if(!this.settings.checkbox.tie_selection) {
  5939. this.uncheck_all();
  5940. var _this = this;
  5941. $.each(state.checkbox, function (i, v) {
  5942. _this.check_node(v);
  5943. });
  5944. }
  5945. delete state.checkbox;
  5946. this.set_state(state, callback);
  5947. return false;
  5948. }
  5949. return res;
  5950. };
  5951. this.refresh = function (skip_loading, forget_state) {
  5952. if(this.settings.checkbox.tie_selection) {
  5953. this._data.checkbox.selected = [];
  5954. }
  5955. return parent.refresh.apply(this, arguments);
  5956. };
  5957. };
  5958. // include the checkbox plugin by default
  5959. // $.jstree.defaults.plugins.push("checkbox");
  5960. /**
  5961. * ### Conditionalselect plugin
  5962. *
  5963. * This plugin allows defining a callback to allow or deny node selection by user input (activate node method).
  5964. */
  5965. /**
  5966. * a callback (function) which is invoked in the instance's scope and receives two arguments - the node and the event that triggered the `activate_node` call. Returning false prevents working with the node, returning true allows invoking activate_node. Defaults to returning `true`.
  5967. * @name $.jstree.defaults.checkbox.visible
  5968. * @plugin checkbox
  5969. */
  5970. $.jstree.defaults.conditionalselect = function () { return true; };
  5971. $.jstree.plugins.conditionalselect = function (options, parent) {
  5972. // own function
  5973. this.activate_node = function (obj, e) {
  5974. if(this.settings.conditionalselect.call(this, this.get_node(obj), e)) {
  5975. return parent.activate_node.call(this, obj, e);
  5976. }
  5977. };
  5978. };
  5979. /**
  5980. * ### Contextmenu plugin
  5981. *
  5982. * Shows a context menu when a node is right-clicked.
  5983. */
  5984. /**
  5985. * stores all defaults for the contextmenu plugin
  5986. * @name $.jstree.defaults.contextmenu
  5987. * @plugin contextmenu
  5988. */
  5989. $.jstree.defaults.contextmenu = {
  5990. /**
  5991. * a boolean indicating if the node should be selected when the context menu is invoked on it. Defaults to `true`.
  5992. * @name $.jstree.defaults.contextmenu.select_node
  5993. * @plugin contextmenu
  5994. */
  5995. select_node : true,
  5996. /**
  5997. * a boolean indicating if the menu should be shown aligned with the node. Defaults to `true`, otherwise the mouse coordinates are used.
  5998. * @name $.jstree.defaults.contextmenu.show_at_node
  5999. * @plugin contextmenu
  6000. */
  6001. show_at_node : true,
  6002. /**
  6003. * 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).
  6004. *
  6005. * Each action consists of a key (a unique name) and a value which is an object with the following properties (only label and action are required). Once a menu item is activated the `action` function will be invoked with an object containing the following keys: item - the contextmenu item definition as seen below, reference - the DOM node that was used (the tree node), element - the contextmenu DOM element, position - an object with x/y properties indicating the position of the menu.
  6006. *
  6007. * * `separator_before` - a boolean indicating if there should be a separator before this item
  6008. * * `separator_after` - a boolean indicating if there should be a separator after this item
  6009. * * `_disabled` - a boolean indicating if this action should be disabled
  6010. * * `label` - a string - the name of the action (could be a function returning a string)
  6011. * * `title` - a string - an optional tooltip for the item
  6012. * * `action` - a function to be executed if this item is chosen, the function will receive
  6013. * * `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
  6014. * * `shortcut` - keyCode which will trigger the action if the menu is open (for example `113` for rename, which equals F2)
  6015. * * `shortcut_label` - shortcut label (like for example `F2` for rename)
  6016. * * `submenu` - an object with the same structure as $.jstree.defaults.contextmenu.items which can be used to create a submenu - each key will be rendered as a separate option in a submenu that will appear once the current item is hovered
  6017. *
  6018. * @name $.jstree.defaults.contextmenu.items
  6019. * @plugin contextmenu
  6020. */
  6021. items : function (o, cb) { // Could be an object directly
  6022. return {
  6023. "create" : {
  6024. "separator_before" : false,
  6025. "separator_after" : true,
  6026. "_disabled" : false, //(this.check("create_node", data.reference, {}, "last")),
  6027. "label" : "Create",
  6028. "action" : function (data) {
  6029. var inst = $.jstree.reference(data.reference),
  6030. obj = inst.get_node(data.reference);
  6031. inst.create_node(obj, {}, "last", function (new_node) {
  6032. try {
  6033. inst.edit(new_node);
  6034. } catch (ex) {
  6035. setTimeout(function () { inst.edit(new_node); },0);
  6036. }
  6037. });
  6038. }
  6039. },
  6040. "rename" : {
  6041. "separator_before" : false,
  6042. "separator_after" : false,
  6043. "_disabled" : false, //(this.check("rename_node", data.reference, this.get_parent(data.reference), "")),
  6044. "label" : "Rename",
  6045. /*!
  6046. "shortcut" : 113,
  6047. "shortcut_label" : 'F2',
  6048. "icon" : "glyphicon glyphicon-leaf",
  6049. */
  6050. "action" : function (data) {
  6051. var inst = $.jstree.reference(data.reference),
  6052. obj = inst.get_node(data.reference);
  6053. inst.edit(obj);
  6054. }
  6055. },
  6056. "remove" : {
  6057. "separator_before" : false,
  6058. "icon" : false,
  6059. "separator_after" : false,
  6060. "_disabled" : false, //(this.check("delete_node", data.reference, this.get_parent(data.reference), "")),
  6061. "label" : "Delete",
  6062. "action" : function (data) {
  6063. var inst = $.jstree.reference(data.reference),
  6064. obj = inst.get_node(data.reference);
  6065. if(inst.is_selected(obj)) {
  6066. inst.delete_node(inst.get_selected());
  6067. }
  6068. else {
  6069. inst.delete_node(obj);
  6070. }
  6071. }
  6072. },
  6073. "ccp" : {
  6074. "separator_before" : true,
  6075. "icon" : false,
  6076. "separator_after" : false,
  6077. "label" : "Edit",
  6078. "action" : false,
  6079. "submenu" : {
  6080. "cut" : {
  6081. "separator_before" : false,
  6082. "separator_after" : false,
  6083. "label" : "Cut",
  6084. "action" : function (data) {
  6085. var inst = $.jstree.reference(data.reference),
  6086. obj = inst.get_node(data.reference);
  6087. if(inst.is_selected(obj)) {
  6088. inst.cut(inst.get_top_selected());
  6089. }
  6090. else {
  6091. inst.cut(obj);
  6092. }
  6093. }
  6094. },
  6095. "copy" : {
  6096. "separator_before" : false,
  6097. "icon" : false,
  6098. "separator_after" : false,
  6099. "label" : "Copy",
  6100. "action" : function (data) {
  6101. var inst = $.jstree.reference(data.reference),
  6102. obj = inst.get_node(data.reference);
  6103. if(inst.is_selected(obj)) {
  6104. inst.copy(inst.get_top_selected());
  6105. }
  6106. else {
  6107. inst.copy(obj);
  6108. }
  6109. }
  6110. },
  6111. "paste" : {
  6112. "separator_before" : false,
  6113. "icon" : false,
  6114. "_disabled" : function (data) {
  6115. return !$.jstree.reference(data.reference).can_paste();
  6116. },
  6117. "separator_after" : false,
  6118. "label" : "Paste",
  6119. "action" : function (data) {
  6120. var inst = $.jstree.reference(data.reference),
  6121. obj = inst.get_node(data.reference);
  6122. inst.paste(obj);
  6123. }
  6124. }
  6125. }
  6126. }
  6127. };
  6128. }
  6129. };
  6130. $.jstree.plugins.contextmenu = function (options, parent) {
  6131. this.bind = function () {
  6132. parent.bind.call(this);
  6133. var last_ts = 0, cto = null, ex, ey;
  6134. this.element
  6135. .on("init.jstree loading.jstree ready.jstree", function () {
  6136. this.get_container_ul().addClass('jstree-contextmenu');
  6137. }.bind(this))
  6138. .on("contextmenu.jstree", ".jstree-anchor", function (e, data) {
  6139. if (e.target.tagName.toLowerCase() === 'input') {
  6140. return;
  6141. }
  6142. e.preventDefault();
  6143. last_ts = e.ctrlKey ? +new Date() : 0;
  6144. if(data || cto) {
  6145. last_ts = (+new Date()) + 10000;
  6146. }
  6147. if(cto) {
  6148. clearTimeout(cto);
  6149. }
  6150. if(!this.is_loading(e.currentTarget)) {
  6151. this.show_contextmenu(e.currentTarget, e.pageX, e.pageY, e);
  6152. }
  6153. }.bind(this))
  6154. .on("click.jstree", ".jstree-anchor", function (e) {
  6155. if(this._data.contextmenu.visible && (!last_ts || (+new Date()) - last_ts > 250)) { // work around safari & macOS ctrl+click
  6156. $.vakata.context.hide();
  6157. }
  6158. last_ts = 0;
  6159. }.bind(this))
  6160. .on("touchstart.jstree", ".jstree-anchor", function (e) {
  6161. if(!e.originalEvent || !e.originalEvent.changedTouches || !e.originalEvent.changedTouches[0]) {
  6162. return;
  6163. }
  6164. ex = e.originalEvent.changedTouches[0].clientX;
  6165. ey = e.originalEvent.changedTouches[0].clientY;
  6166. cto = setTimeout(function () {
  6167. $(e.currentTarget).trigger('contextmenu', true);
  6168. }, 750);
  6169. })
  6170. .on('touchmove.vakata.jstree', function (e) {
  6171. if(cto && e.originalEvent && e.originalEvent.changedTouches && e.originalEvent.changedTouches[0] && (Math.abs(ex - e.originalEvent.changedTouches[0].clientX) > 10 || Math.abs(ey - e.originalEvent.changedTouches[0].clientY) > 10)) {
  6172. clearTimeout(cto);
  6173. $.vakata.context.hide();
  6174. }
  6175. })
  6176. .on('touchend.vakata.jstree', function (e) {
  6177. if(cto) {
  6178. clearTimeout(cto);
  6179. }
  6180. });
  6181. /*!
  6182. if(!('oncontextmenu' in document.body) && ('ontouchstart' in document.body)) {
  6183. var el = null, tm = null;
  6184. this.element
  6185. .on("touchstart", ".jstree-anchor", function (e) {
  6186. el = e.currentTarget;
  6187. tm = +new Date();
  6188. $(document).one("touchend", function (e) {
  6189. e.target = document.elementFromPoint(e.originalEvent.targetTouches[0].pageX - window.pageXOffset, e.originalEvent.targetTouches[0].pageY - window.pageYOffset);
  6190. e.currentTarget = e.target;
  6191. tm = ((+(new Date())) - tm);
  6192. if(e.target === el && tm > 600 && tm < 1000) {
  6193. e.preventDefault();
  6194. $(el).trigger('contextmenu', e);
  6195. }
  6196. el = null;
  6197. tm = null;
  6198. });
  6199. });
  6200. }
  6201. */
  6202. $(document).on("context_hide.vakata.jstree", function (e, data) {
  6203. this._data.contextmenu.visible = false;
  6204. $(data.reference).removeClass('jstree-context');
  6205. }.bind(this));
  6206. };
  6207. this.teardown = function () {
  6208. if(this._data.contextmenu.visible) {
  6209. $.vakata.context.hide();
  6210. }
  6211. $(document).off("context_hide.vakata.jstree");
  6212. parent.teardown.call(this);
  6213. };
  6214. /**
  6215. * prepare and show the context menu for a node
  6216. * @name show_contextmenu(obj [, x, y])
  6217. * @param {mixed} obj the node
  6218. * @param {Number} x the x-coordinate relative to the document to show the menu at
  6219. * @param {Number} y the y-coordinate relative to the document to show the menu at
  6220. * @param {Object} e the event if available that triggered the contextmenu
  6221. * @plugin contextmenu
  6222. * @trigger show_contextmenu.jstree
  6223. */
  6224. this.show_contextmenu = function (obj, x, y, e) {
  6225. obj = this.get_node(obj);
  6226. if(!obj || obj.id === $.jstree.root) { return false; }
  6227. var s = this.settings.contextmenu,
  6228. d = this.get_node(obj, true),
  6229. a = d.children(".jstree-anchor"),
  6230. o = false,
  6231. i = false;
  6232. if(s.show_at_node || x === undefined || y === undefined) {
  6233. o = a.offset();
  6234. x = o.left;
  6235. y = o.top + this._data.core.li_height;
  6236. }
  6237. if(this.settings.contextmenu.select_node && !this.is_selected(obj)) {
  6238. this.activate_node(obj, e);
  6239. }
  6240. i = s.items;
  6241. if($.vakata.is_function(i)) {
  6242. i = i.call(this, obj, function (i) {
  6243. this._show_contextmenu(obj, x, y, i);
  6244. }.bind(this));
  6245. }
  6246. if($.isPlainObject(i)) {
  6247. this._show_contextmenu(obj, x, y, i);
  6248. }
  6249. };
  6250. /**
  6251. * show the prepared context menu for a node
  6252. * @name _show_contextmenu(obj, x, y, i)
  6253. * @param {mixed} obj the node
  6254. * @param {Number} x the x-coordinate relative to the document to show the menu at
  6255. * @param {Number} y the y-coordinate relative to the document to show the menu at
  6256. * @param {Number} i the object of items to show
  6257. * @plugin contextmenu
  6258. * @trigger show_contextmenu.jstree
  6259. * @private
  6260. */
  6261. this._show_contextmenu = function (obj, x, y, i) {
  6262. var d = this.get_node(obj, true),
  6263. a = d.children(".jstree-anchor");
  6264. $(document).one("context_show.vakata.jstree", function (e, data) {
  6265. var cls = 'jstree-contextmenu jstree-' + this.get_theme() + '-contextmenu';
  6266. $(data.element).addClass(cls);
  6267. a.addClass('jstree-context');
  6268. }.bind(this));
  6269. this._data.contextmenu.visible = true;
  6270. $.vakata.context.show(a, { 'x' : x, 'y' : y }, i);
  6271. /**
  6272. * triggered when the contextmenu is shown for a node
  6273. * @event
  6274. * @name show_contextmenu.jstree
  6275. * @param {Object} node the node
  6276. * @param {Number} x the x-coordinate of the menu relative to the document
  6277. * @param {Number} y the y-coordinate of the menu relative to the document
  6278. * @plugin contextmenu
  6279. */
  6280. this.trigger('show_contextmenu', { "node" : obj, "x" : x, "y" : y });
  6281. };
  6282. };
  6283. // contextmenu helper
  6284. (function ($) {
  6285. var right_to_left = false,
  6286. vakata_context = {
  6287. element : false,
  6288. reference : false,
  6289. position_x : 0,
  6290. position_y : 0,
  6291. items : [],
  6292. html : "",
  6293. is_visible : false
  6294. };
  6295. $.vakata.context = {
  6296. settings : {
  6297. hide_onmouseleave : 0,
  6298. icons : true
  6299. },
  6300. _trigger : function (event_name) {
  6301. $(document).triggerHandler("context_" + event_name + ".vakata", {
  6302. "reference" : vakata_context.reference,
  6303. "element" : vakata_context.element,
  6304. "position" : {
  6305. "x" : vakata_context.position_x,
  6306. "y" : vakata_context.position_y
  6307. }
  6308. });
  6309. },
  6310. _execute : function (i) {
  6311. i = vakata_context.items[i];
  6312. return i && (!i._disabled || ($.vakata.is_function(i._disabled) && !i._disabled({ "item" : i, "reference" : vakata_context.reference, "element" : vakata_context.element }))) && i.action ? i.action.call(null, {
  6313. "item" : i,
  6314. "reference" : vakata_context.reference,
  6315. "element" : vakata_context.element,
  6316. "position" : {
  6317. "x" : vakata_context.position_x,
  6318. "y" : vakata_context.position_y
  6319. }
  6320. }) : false;
  6321. },
  6322. _parse : function (o, is_callback) {
  6323. if(!o) { return false; }
  6324. if(!is_callback) {
  6325. vakata_context.html = "";
  6326. vakata_context.items = [];
  6327. }
  6328. var str = "",
  6329. sep = false,
  6330. tmp;
  6331. if(is_callback) { str += "<"+"ul>"; }
  6332. $.each(o, function (i, val) {
  6333. if(!val) { return true; }
  6334. vakata_context.items.push(val);
  6335. if(!sep && val.separator_before) {
  6336. str += "<"+"li class='vakata-context-separator'><"+"a href='#' " + ($.vakata.context.settings.icons ? '' : 'class="vakata-context-no-icons"') + ">&#160;<"+"/a><"+"/li>";
  6337. }
  6338. sep = false;
  6339. str += "<"+"li class='" + (val._class || "") + (val._disabled === true || ($.vakata.is_function(val._disabled) && val._disabled({ "item" : val, "reference" : vakata_context.reference, "element" : vakata_context.element })) ? " vakata-contextmenu-disabled " : "") + "' "+(val.shortcut?" data-shortcut='"+val.shortcut+"' ":'')+">";
  6340. str += "<"+"a href='#' rel='" + (vakata_context.items.length - 1) + "' " + (val.title ? "title='" + val.title + "'" : "") + ">";
  6341. if($.vakata.context.settings.icons) {
  6342. str += "<"+"i ";
  6343. if(val.icon) {
  6344. if(val.icon.indexOf("/") !== -1 || val.icon.indexOf(".") !== -1) { str += " style='background:url(\"" + val.icon + "\") center center no-repeat' "; }
  6345. else { str += " class='" + val.icon + "' "; }
  6346. }
  6347. str += "><"+"/i><"+"span class='vakata-contextmenu-sep'>&#160;<"+"/span>";
  6348. }
  6349. str += ($.vakata.is_function(val.label) ? val.label({ "item" : i, "reference" : vakata_context.reference, "element" : vakata_context.element }) : val.label) + (val.shortcut?' <span class="vakata-contextmenu-shortcut vakata-contextmenu-shortcut-'+val.shortcut+'">'+ (val.shortcut_label || '') +'</span>':'') + "<"+"/a>";
  6350. if(val.submenu) {
  6351. tmp = $.vakata.context._parse(val.submenu, true);
  6352. if(tmp) { str += tmp; }
  6353. }
  6354. str += "<"+"/li>";
  6355. if(val.separator_after) {
  6356. str += "<"+"li class='vakata-context-separator'><"+"a href='#' " + ($.vakata.context.settings.icons ? '' : 'class="vakata-context-no-icons"') + ">&#160;<"+"/a><"+"/li>";
  6357. sep = true;
  6358. }
  6359. });
  6360. str = str.replace(/<li class\='vakata-context-separator'\><\/li\>$/,"");
  6361. if(is_callback) { str += "</ul>"; }
  6362. /**
  6363. * triggered on the document when the contextmenu is parsed (HTML is built)
  6364. * @event
  6365. * @plugin contextmenu
  6366. * @name context_parse.vakata
  6367. * @param {jQuery} reference the element that was right clicked
  6368. * @param {jQuery} element the DOM element of the menu itself
  6369. * @param {Object} position the x & y coordinates of the menu
  6370. */
  6371. if(!is_callback) { vakata_context.html = str; $.vakata.context._trigger("parse"); }
  6372. return str.length > 10 ? str : false;
  6373. },
  6374. _show_submenu : function (o) {
  6375. o = $(o);
  6376. if(!o.length || !o.children("ul").length) { return; }
  6377. var e = o.children("ul"),
  6378. xl = o.offset().left,
  6379. x = xl + o.outerWidth(),
  6380. y = o.offset().top,
  6381. w = e.width(),
  6382. h = e.height(),
  6383. dw = $(window).width() + $(window).scrollLeft(),
  6384. dh = $(window).height() + $(window).scrollTop();
  6385. // може да се спести е една проверка - дали няма някой от класовете вече нагоре
  6386. if(right_to_left) {
  6387. o[x - (w + 10 + o.outerWidth()) < 0 ? "addClass" : "removeClass"]("vakata-context-left");
  6388. }
  6389. else {
  6390. o[x + w > dw && xl > dw - x ? "addClass" : "removeClass"]("vakata-context-right");
  6391. }
  6392. if(y + h + 10 > dh) {
  6393. e.css("bottom","-1px");
  6394. }
  6395. //if does not fit - stick it to the side
  6396. if (o.hasClass('vakata-context-right')) {
  6397. if (xl < w) {
  6398. e.css("margin-right", xl - w);
  6399. }
  6400. } else {
  6401. if (dw - x < w) {
  6402. e.css("margin-left", dw - x - w);
  6403. }
  6404. }
  6405. e.show();
  6406. },
  6407. show : function (reference, position, data) {
  6408. var o, e, x, y, w, h, dw, dh, cond = true;
  6409. if(vakata_context.element && vakata_context.element.length) {
  6410. vakata_context.element.width('');
  6411. }
  6412. switch(cond) {
  6413. case (!position && !reference):
  6414. return false;
  6415. case (!!position && !!reference):
  6416. vakata_context.reference = reference;
  6417. vakata_context.position_x = position.x;
  6418. vakata_context.position_y = position.y;
  6419. break;
  6420. case (!position && !!reference):
  6421. vakata_context.reference = reference;
  6422. o = reference.offset();
  6423. vakata_context.position_x = o.left + reference.outerHeight();
  6424. vakata_context.position_y = o.top;
  6425. break;
  6426. case (!!position && !reference):
  6427. vakata_context.position_x = position.x;
  6428. vakata_context.position_y = position.y;
  6429. break;
  6430. }
  6431. if(!!reference && !data && $(reference).data('vakata_contextmenu')) {
  6432. data = $(reference).data('vakata_contextmenu');
  6433. }
  6434. if($.vakata.context._parse(data)) {
  6435. vakata_context.element.html(vakata_context.html);
  6436. }
  6437. if(vakata_context.items.length) {
  6438. vakata_context.element.appendTo(document.body);
  6439. e = vakata_context.element;
  6440. x = vakata_context.position_x;
  6441. y = vakata_context.position_y;
  6442. w = e.width();
  6443. h = e.height();
  6444. dw = $(window).width() + $(window).scrollLeft();
  6445. dh = $(window).height() + $(window).scrollTop();
  6446. if(right_to_left) {
  6447. x -= (e.outerWidth() - $(reference).outerWidth());
  6448. if(x < $(window).scrollLeft() + 20) {
  6449. x = $(window).scrollLeft() + 20;
  6450. }
  6451. }
  6452. if(x + w + 20 > dw) {
  6453. x = dw - (w + 20);
  6454. }
  6455. if(y + h + 20 > dh) {
  6456. y = dh - (h + 20);
  6457. }
  6458. vakata_context.element
  6459. .css({ "left" : x, "top" : y })
  6460. .show()
  6461. .find('a').first().trigger('focus').parent().addClass("vakata-context-hover");
  6462. vakata_context.is_visible = true;
  6463. /**
  6464. * triggered on the document when the contextmenu is shown
  6465. * @event
  6466. * @plugin contextmenu
  6467. * @name context_show.vakata
  6468. * @param {jQuery} reference the element that was right clicked
  6469. * @param {jQuery} element the DOM element of the menu itself
  6470. * @param {Object} position the x & y coordinates of the menu
  6471. */
  6472. $.vakata.context._trigger("show");
  6473. }
  6474. },
  6475. hide : function () {
  6476. if(vakata_context.is_visible) {
  6477. vakata_context.element.hide().find("ul").hide().end().find(':focus').trigger('blur').end().detach();
  6478. vakata_context.is_visible = false;
  6479. /**
  6480. * triggered on the document when the contextmenu is hidden
  6481. * @event
  6482. * @plugin contextmenu
  6483. * @name context_hide.vakata
  6484. * @param {jQuery} reference the element that was right clicked
  6485. * @param {jQuery} element the DOM element of the menu itself
  6486. * @param {Object} position the x & y coordinates of the menu
  6487. */
  6488. $.vakata.context._trigger("hide");
  6489. }
  6490. }
  6491. };
  6492. $(function () {
  6493. right_to_left = $(document.body).css("direction") === "rtl";
  6494. var to = false;
  6495. vakata_context.element = $("<ul class='vakata-context'></ul>");
  6496. vakata_context.element
  6497. .on("mouseenter", "li", function (e) {
  6498. e.stopImmediatePropagation();
  6499. if($.contains(this, e.relatedTarget)) {
  6500. // премахнато заради delegate mouseleave по-долу
  6501. // $(this).find(".vakata-context-hover").removeClass("vakata-context-hover");
  6502. return;
  6503. }
  6504. if(to) { clearTimeout(to); }
  6505. vakata_context.element.find(".vakata-context-hover").removeClass("vakata-context-hover").end();
  6506. $(this)
  6507. .siblings().find("ul").hide().end().end()
  6508. .parentsUntil(".vakata-context", "li").addBack().addClass("vakata-context-hover");
  6509. $.vakata.context._show_submenu(this);
  6510. })
  6511. // тестово - дали не натоварва?
  6512. .on("mouseleave", "li", function (e) {
  6513. if($.contains(this, e.relatedTarget)) { return; }
  6514. $(this).find(".vakata-context-hover").addBack().removeClass("vakata-context-hover");
  6515. })
  6516. .on("mouseleave", function (e) {
  6517. $(this).find(".vakata-context-hover").removeClass("vakata-context-hover");
  6518. if($.vakata.context.settings.hide_onmouseleave) {
  6519. to = setTimeout(
  6520. (function (t) {
  6521. return function () { $.vakata.context.hide(); };
  6522. }(this)), $.vakata.context.settings.hide_onmouseleave);
  6523. }
  6524. })
  6525. .on("click", "a", function (e) {
  6526. e.preventDefault();
  6527. //})
  6528. //.on("mouseup", "a", function (e) {
  6529. if(!$(this).trigger('blur').parent().hasClass("vakata-context-disabled") && $.vakata.context._execute($(this).attr("rel")) !== false) {
  6530. $.vakata.context.hide();
  6531. }
  6532. })
  6533. .on('keydown', 'a', function (e) {
  6534. var o = null;
  6535. switch(e.which) {
  6536. case 13:
  6537. case 32:
  6538. e.type = "click";
  6539. e.preventDefault();
  6540. $(e.currentTarget).trigger(e);
  6541. break;
  6542. case 37:
  6543. if(vakata_context.is_visible) {
  6544. vakata_context.element.find(".vakata-context-hover").last().closest("li").first().find("ul").hide().find(".vakata-context-hover").removeClass("vakata-context-hover").end().end().children('a').trigger('focus');
  6545. e.stopImmediatePropagation();
  6546. e.preventDefault();
  6547. }
  6548. break;
  6549. case 38:
  6550. if(vakata_context.is_visible) {
  6551. o = vakata_context.element.find("ul:visible").addBack().last().children(".vakata-context-hover").removeClass("vakata-context-hover").prevAll("li:not(.vakata-context-separator)").first();
  6552. if(!o.length) { o = vakata_context.element.find("ul:visible").addBack().last().children("li:not(.vakata-context-separator)").last(); }
  6553. o.addClass("vakata-context-hover").children('a').trigger('focus');
  6554. e.stopImmediatePropagation();
  6555. e.preventDefault();
  6556. }
  6557. break;
  6558. case 39:
  6559. if(vakata_context.is_visible) {
  6560. vakata_context.element.find(".vakata-context-hover").last().children("ul").show().children("li:not(.vakata-context-separator)").removeClass("vakata-context-hover").first().addClass("vakata-context-hover").children('a').trigger('focus');
  6561. e.stopImmediatePropagation();
  6562. e.preventDefault();
  6563. }
  6564. break;
  6565. case 40:
  6566. if(vakata_context.is_visible) {
  6567. o = vakata_context.element.find("ul:visible").addBack().last().children(".vakata-context-hover").removeClass("vakata-context-hover").nextAll("li:not(.vakata-context-separator)").first();
  6568. if(!o.length) { o = vakata_context.element.find("ul:visible").addBack().last().children("li:not(.vakata-context-separator)").first(); }
  6569. o.addClass("vakata-context-hover").children('a').trigger('focus');
  6570. e.stopImmediatePropagation();
  6571. e.preventDefault();
  6572. }
  6573. break;
  6574. case 27:
  6575. $.vakata.context.hide();
  6576. e.preventDefault();
  6577. break;
  6578. default:
  6579. //console.log(e.which);
  6580. break;
  6581. }
  6582. })
  6583. .on('keydown', function (e) {
  6584. e.preventDefault();
  6585. var a = vakata_context.element.find('.vakata-contextmenu-shortcut-' + e.which).parent();
  6586. if(a.parent().not('.vakata-context-disabled')) {
  6587. a.trigger('click');
  6588. }
  6589. });
  6590. $(document)
  6591. .on("mousedown.vakata.jstree", function (e) {
  6592. if(vakata_context.is_visible && vakata_context.element[0] !== e.target && !$.contains(vakata_context.element[0], e.target)) {
  6593. $.vakata.context.hide();
  6594. }
  6595. })
  6596. .on("context_show.vakata.jstree", function (e, data) {
  6597. vakata_context.element.find("li:has(ul)").children("a").addClass("vakata-context-parent");
  6598. if(right_to_left) {
  6599. vakata_context.element.addClass("vakata-context-rtl").css("direction", "rtl");
  6600. }
  6601. // also apply a RTL class?
  6602. vakata_context.element.find("ul").hide().end();
  6603. });
  6604. });
  6605. }($));
  6606. // $.jstree.defaults.plugins.push("contextmenu");
  6607. /**
  6608. * ### Drag'n'drop plugin
  6609. *
  6610. * Enables dragging and dropping of nodes in the tree, resulting in a move or copy operations.
  6611. */
  6612. /**
  6613. * stores all defaults for the drag'n'drop plugin
  6614. * @name $.jstree.defaults.dnd
  6615. * @plugin dnd
  6616. */
  6617. $.jstree.defaults.dnd = {
  6618. /**
  6619. * a boolean indicating if a copy should be possible while dragging (by pressint the meta key or Ctrl). Defaults to `true`.
  6620. * @name $.jstree.defaults.dnd.copy
  6621. * @plugin dnd
  6622. */
  6623. copy : true,
  6624. /**
  6625. * a number indicating how long a node should remain hovered while dragging to be opened. Defaults to `500`.
  6626. * @name $.jstree.defaults.dnd.open_timeout
  6627. * @plugin dnd
  6628. */
  6629. open_timeout : 500,
  6630. /**
  6631. * a function invoked each time a node is about to be dragged, invoked in the tree's scope and receives the nodes about to be dragged as an argument (array) and the event that started the drag - return `false` to prevent dragging
  6632. * @name $.jstree.defaults.dnd.is_draggable
  6633. * @plugin dnd
  6634. */
  6635. is_draggable : true,
  6636. /**
  6637. * 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`
  6638. * @name $.jstree.defaults.dnd.check_while_dragging
  6639. * @plugin dnd
  6640. */
  6641. check_while_dragging : true,
  6642. /**
  6643. * a boolean indicating if nodes from this tree should only be copied with dnd (as opposed to moved), default is `false`
  6644. * @name $.jstree.defaults.dnd.always_copy
  6645. * @plugin dnd
  6646. */
  6647. always_copy : false,
  6648. /**
  6649. * 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`
  6650. * @name $.jstree.defaults.dnd.inside_pos
  6651. * @plugin dnd
  6652. */
  6653. inside_pos : 0,
  6654. /**
  6655. * when starting the drag on a node that is selected this setting controls if all selected nodes are dragged or only the single node, default is `true`, which means all selected nodes are dragged when the drag is started on a selected node
  6656. * @name $.jstree.defaults.dnd.drag_selection
  6657. * @plugin dnd
  6658. */
  6659. drag_selection : true,
  6660. /**
  6661. * controls whether dnd works on touch devices. If left as boolean true dnd will work the same as in desktop browsers, which in some cases may impair scrolling. If set to boolean false dnd will not work on touch devices. There is a special third option - string "selected" which means only selected nodes can be dragged on touch devices.
  6662. * @name $.jstree.defaults.dnd.touch
  6663. * @plugin dnd
  6664. */
  6665. touch : true,
  6666. /**
  6667. * controls whether items can be dropped anywhere on the node, not just on the anchor, by default only the node anchor is a valid drop target. Works best with the wholerow plugin. If enabled on mobile depending on the interface it might be hard for the user to cancel the drop, since the whole tree container will be a valid drop target.
  6668. * @name $.jstree.defaults.dnd.large_drop_target
  6669. * @plugin dnd
  6670. */
  6671. large_drop_target : false,
  6672. /**
  6673. * controls whether a drag can be initiated from any part of the node and not just the text/icon part, works best with the wholerow plugin. Keep in mind it can cause problems with tree scrolling on mobile depending on the interface - in that case set the touch option to "selected".
  6674. * @name $.jstree.defaults.dnd.large_drag_target
  6675. * @plugin dnd
  6676. */
  6677. large_drag_target : false,
  6678. /**
  6679. * controls whether use HTML5 dnd api instead of classical. That will allow better integration of dnd events with other HTML5 controls.
  6680. * @reference http://caniuse.com/#feat=dragndrop
  6681. * @name $.jstree.defaults.dnd.use_html5
  6682. * @plugin dnd
  6683. */
  6684. use_html5: false,
  6685. /**
  6686. * controls whether items can be dropped anywhere on the tree.
  6687. * @name $.jstree.defaults.dnd.blank_space_drop
  6688. * @plugin dnd
  6689. */
  6690. blank_space_drop: false
  6691. };
  6692. var drg, elm;
  6693. // TODO: now check works by checking for each node individually, how about max_children, unique, etc?
  6694. $.jstree.plugins.dnd = function (options, parent) {
  6695. this.init = function (el, options) {
  6696. parent.init.call(this, el, options);
  6697. this.settings.dnd.use_html5 = this.settings.dnd.use_html5 && ('draggable' in document.createElement('span'));
  6698. };
  6699. this.bind = function () {
  6700. parent.bind.call(this);
  6701. this.element
  6702. .on(this.settings.dnd.use_html5 ? 'dragstart.jstree' : 'mousedown.jstree touchstart.jstree', this.settings.dnd.large_drag_target ? '.jstree-node' : '.jstree-anchor', function (e) {
  6703. if(this.settings.dnd.large_drag_target && $(e.target).closest('.jstree-node')[0] !== e.currentTarget) {
  6704. return true;
  6705. }
  6706. if(e.type === "touchstart" && (!this.settings.dnd.touch || (this.settings.dnd.touch === 'selected' && !$(e.currentTarget).closest('.jstree-node').children('.jstree-anchor').hasClass('jstree-clicked')))) {
  6707. return true;
  6708. }
  6709. var obj = this.get_node(e.target),
  6710. mlt = this.is_selected(obj) && this.settings.dnd.drag_selection ? this.get_top_selected().length : 1,
  6711. txt = (mlt > 1 ? mlt + ' ' + this.get_string('nodes') : this.get_text(e.currentTarget));
  6712. if(this.settings.core.force_text) {
  6713. txt = $.vakata.html.escape(txt);
  6714. }
  6715. if(obj && (obj.id || obj.id === 0) && obj.id !== $.jstree.root && (e.which === 1 || e.type === "touchstart" || e.type === "dragstart") &&
  6716. (this.settings.dnd.is_draggable === true || ($.vakata.is_function(this.settings.dnd.is_draggable) && this.settings.dnd.is_draggable.call(this, (mlt > 1 ? this.get_top_selected(true) : [obj]), e)))
  6717. ) {
  6718. drg = { 'jstree' : true, 'origin' : this, 'obj' : this.get_node(obj,true), 'nodes' : mlt > 1 ? this.get_top_selected() : [obj.id] };
  6719. elm = e.currentTarget;
  6720. if (this.settings.dnd.use_html5) {
  6721. $.vakata.dnd._trigger('start', e, { 'helper': $(), 'element': elm, 'data': drg });
  6722. } else {
  6723. this.element.trigger('mousedown.jstree');
  6724. return $.vakata.dnd.start(e, drg, '<div id="jstree-dnd" class="jstree-' + this.get_theme() + ' jstree-' + this.get_theme() + '-' + this.get_theme_variant() + ' ' + ( this.settings.core.themes.responsive ? ' jstree-dnd-responsive' : '' ) + '"><i class="jstree-icon jstree-er"></i>' + txt + '<ins class="jstree-copy">+</ins></div>');
  6725. }
  6726. }
  6727. }.bind(this));
  6728. if (this.settings.dnd.use_html5) {
  6729. this.element
  6730. .on('dragover.jstree', function (e) {
  6731. e.preventDefault();
  6732. $.vakata.dnd._trigger('move', e, { 'helper': $(), 'element': elm, 'data': drg });
  6733. return false;
  6734. })
  6735. //.on('dragenter.jstree', this.settings.dnd.large_drop_target ? '.jstree-node' : '.jstree-anchor', $.proxy(function (e) {
  6736. // e.preventDefault();
  6737. // $.vakata.dnd._trigger('move', e, { 'helper': $(), 'element': elm, 'data': drg });
  6738. // return false;
  6739. // }, this))
  6740. .on('drop.jstree', function (e) {
  6741. e.preventDefault();
  6742. $.vakata.dnd._trigger('stop', e, { 'helper': $(), 'element': elm, 'data': drg });
  6743. return false;
  6744. }.bind(this));
  6745. }
  6746. };
  6747. this.redraw_node = function(obj, deep, callback, force_render) {
  6748. obj = parent.redraw_node.apply(this, arguments);
  6749. if (obj && this.settings.dnd.use_html5) {
  6750. if (this.settings.dnd.large_drag_target) {
  6751. obj.setAttribute('draggable', true);
  6752. } else {
  6753. var i, j, tmp = null;
  6754. for(i = 0, j = obj.childNodes.length; i < j; i++) {
  6755. if(obj.childNodes[i] && obj.childNodes[i].className && obj.childNodes[i].className.indexOf("jstree-anchor") !== -1) {
  6756. tmp = obj.childNodes[i];
  6757. break;
  6758. }
  6759. }
  6760. if(tmp) {
  6761. tmp.setAttribute('draggable', true);
  6762. }
  6763. }
  6764. }
  6765. return obj;
  6766. };
  6767. };
  6768. $(function() {
  6769. // bind only once for all instances
  6770. var lastmv = false,
  6771. laster = false,
  6772. lastev = false,
  6773. opento = false,
  6774. marker = $('<div id="jstree-marker">&#160;</div>').hide(); //.appendTo('body');
  6775. $(document)
  6776. .on('dragover.vakata.jstree', function (e) {
  6777. if (elm) {
  6778. $.vakata.dnd._trigger('move', e, { 'helper': $(), 'element': elm, 'data': drg });
  6779. }
  6780. })
  6781. .on('drop.vakata.jstree', function (e) {
  6782. if (elm) {
  6783. $.vakata.dnd._trigger('stop', e, { 'helper': $(), 'element': elm, 'data': drg });
  6784. elm = null;
  6785. drg = null;
  6786. }
  6787. })
  6788. .on('dnd_start.vakata.jstree', function (e, data) {
  6789. lastmv = false;
  6790. lastev = false;
  6791. if(!data || !data.data || !data.data.jstree) { return; }
  6792. marker.appendTo(document.body); //.show();
  6793. })
  6794. .on('dnd_move.vakata.jstree', function (e, data) {
  6795. var isDifferentNode = data.event.target !== lastev.target;
  6796. if(opento) {
  6797. if (!data.event || data.event.type !== 'dragover' || isDifferentNode) {
  6798. clearTimeout(opento);
  6799. }
  6800. }
  6801. if(!data || !data.data || !data.data.jstree) { return; }
  6802. // if we are hovering the marker image do nothing (can happen on "inside" drags)
  6803. if(data.event.target.id && data.event.target.id === 'jstree-marker') {
  6804. return;
  6805. }
  6806. lastev = data.event;
  6807. var ins = $.jstree.reference(data.event.target),
  6808. ref = false,
  6809. off = false,
  6810. rel = false,
  6811. tmp, l, t, h, p, i, o, ok, t1, t2, op, ps, pr, ip, tm, is_copy, pn, c;
  6812. // if we are over an instance
  6813. if(ins && ins._data && ins._data.dnd) {
  6814. marker.attr('class', 'jstree-' + ins.get_theme() + ( ins.settings.core.themes.responsive ? ' jstree-dnd-responsive' : '' ));
  6815. is_copy = data.data.origin && (data.data.origin.settings.dnd.always_copy || (data.data.origin.settings.dnd.copy && (data.event.metaKey || data.event.ctrlKey)));
  6816. data.helper
  6817. .children().attr('class', 'jstree-' + ins.get_theme() + ' jstree-' + ins.get_theme() + '-' + ins.get_theme_variant() + ' ' + ( ins.settings.core.themes.responsive ? ' jstree-dnd-responsive' : '' ))
  6818. .find('.jstree-copy').first()[ is_copy ? 'show' : 'hide' ]();
  6819. // if are hovering the container itself add a new root node
  6820. //console.log(data.event);
  6821. if( (data.event.target === ins.element[0] || data.event.target === ins.get_container_ul()[0]) && (ins.get_container_ul().children().length === 0 || ins.settings.dnd.blank_space_drop)) {
  6822. ok = true;
  6823. for(t1 = 0, t2 = data.data.nodes.length; t1 < t2; t1++) {
  6824. ok = ok && ins.check( (data.data.origin && (data.data.origin.settings.dnd.always_copy || (data.data.origin.settings.dnd.copy && (data.event.metaKey || data.event.ctrlKey)) ) ? "copy_node" : "move_node"), (data.data.origin && data.data.origin !== ins ? data.data.origin.get_node(data.data.nodes[t1]) : data.data.nodes[t1]), $.jstree.root, 'last', { 'dnd' : true, 'ref' : ins.get_node($.jstree.root), 'pos' : 'i', 'origin' : data.data.origin, 'is_multi' : (data.data.origin && data.data.origin !== ins), 'is_foreign' : (!data.data.origin) });
  6825. if(!ok) { break; }
  6826. }
  6827. if(ok) {
  6828. lastmv = { 'ins' : ins, 'par' : $.jstree.root, 'pos' : 'last' };
  6829. marker.hide();
  6830. data.helper.find('.jstree-icon').first().removeClass('jstree-er').addClass('jstree-ok');
  6831. if (data.event.originalEvent && data.event.originalEvent.dataTransfer) {
  6832. data.event.originalEvent.dataTransfer.dropEffect = is_copy ? 'copy' : 'move';
  6833. }
  6834. return;
  6835. }
  6836. }
  6837. else {
  6838. // if we are hovering a tree node
  6839. ref = ins.settings.dnd.large_drop_target ? $(data.event.target).closest('.jstree-node').children('.jstree-anchor') : $(data.event.target).closest('.jstree-anchor');
  6840. if(ref && ref.length && ref.parent().is('.jstree-closed, .jstree-open, .jstree-leaf')) {
  6841. off = ref.offset();
  6842. rel = (data.event.pageY !== undefined ? data.event.pageY : data.event.originalEvent.pageY) - off.top;
  6843. h = ref.outerHeight();
  6844. if(rel < h / 3) {
  6845. o = ['b', 'i', 'a'];
  6846. }
  6847. else if(rel > h - h / 3) {
  6848. o = ['a', 'i', 'b'];
  6849. }
  6850. else {
  6851. o = rel > h / 2 ? ['i', 'a', 'b'] : ['i', 'b', 'a'];
  6852. }
  6853. $.each(o, function (j, v) {
  6854. switch(v) {
  6855. case 'b':
  6856. l = off.left - 6;
  6857. t = off.top;
  6858. p = ins.get_parent(ref);
  6859. i = ref.parent().index();
  6860. c = 'jstree-below';
  6861. break;
  6862. case 'i':
  6863. ip = ins.settings.dnd.inside_pos;
  6864. tm = ins.get_node(ref.parent());
  6865. l = off.left - 2;
  6866. t = off.top + h / 2 + 1;
  6867. p = tm.id;
  6868. i = ip === 'first' ? 0 : (ip === 'last' ? tm.children.length : Math.min(ip, tm.children.length));
  6869. c = 'jstree-inside';
  6870. break;
  6871. case 'a':
  6872. l = off.left - 6;
  6873. t = off.top + h;
  6874. p = ins.get_parent(ref);
  6875. i = ref.parent().index() + 1;
  6876. c = 'jstree-above';
  6877. break;
  6878. }
  6879. ok = true;
  6880. for(t1 = 0, t2 = data.data.nodes.length; t1 < t2; t1++) {
  6881. 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";
  6882. ps = i;
  6883. if(op === "move_node" && v === 'a' && (data.data.origin && data.data.origin === ins) && p === ins.get_parent(data.data.nodes[t1])) {
  6884. pr = ins.get_node(p);
  6885. if(ps > $.inArray(data.data.nodes[t1], pr.children)) {
  6886. ps -= 1;
  6887. }
  6888. }
  6889. ok = ok && ( (ins && ins.settings && ins.settings.dnd && ins.settings.dnd.check_while_dragging === false) || ins.check(op, (data.data.origin && data.data.origin !== ins ? data.data.origin.get_node(data.data.nodes[t1]) : data.data.nodes[t1]), p, ps, { 'dnd' : true, 'ref' : ins.get_node(ref.parent()), 'pos' : v, 'origin' : data.data.origin, 'is_multi' : (data.data.origin && data.data.origin !== ins), 'is_foreign' : (!data.data.origin) }) );
  6890. if(!ok) {
  6891. if(ins && ins.last_error) { laster = ins.last_error(); }
  6892. break;
  6893. }
  6894. }
  6895. if(v === 'i' && ref.parent().is('.jstree-closed') && ins.settings.dnd.open_timeout) {
  6896. if (!data.event || data.event.type !== 'dragover' || isDifferentNode) {
  6897. if (opento) { clearTimeout(opento); }
  6898. opento = setTimeout((function (x, z) { return function () { x.open_node(z); }; }(ins, ref)), ins.settings.dnd.open_timeout);
  6899. }
  6900. }
  6901. if(ok) {
  6902. pn = ins.get_node(p, true);
  6903. if (!pn.hasClass('.jstree-dnd-parent')) {
  6904. $('.jstree-dnd-parent').removeClass('jstree-dnd-parent');
  6905. pn.addClass('jstree-dnd-parent');
  6906. }
  6907. lastmv = { 'ins' : ins, 'par' : p, 'pos' : v === 'i' && ip === 'last' && i === 0 && !ins.is_loaded(tm) ? 'last' : i };
  6908. marker.css({ 'left' : l + 'px', 'top' : t + 'px' }).show();
  6909. marker.removeClass('jstree-above jstree-inside jstree-below').addClass(c);
  6910. data.helper.find('.jstree-icon').first().removeClass('jstree-er').addClass('jstree-ok');
  6911. if (data.event.originalEvent && data.event.originalEvent.dataTransfer) {
  6912. data.event.originalEvent.dataTransfer.dropEffect = is_copy ? 'copy' : 'move';
  6913. }
  6914. laster = {};
  6915. o = true;
  6916. return false;
  6917. }
  6918. });
  6919. if(o === true) { return; }
  6920. }
  6921. }
  6922. }
  6923. $('.jstree-dnd-parent').removeClass('jstree-dnd-parent');
  6924. lastmv = false;
  6925. data.helper.find('.jstree-icon').removeClass('jstree-ok').addClass('jstree-er');
  6926. if (data.event.originalEvent && data.event.originalEvent.dataTransfer) {
  6927. //data.event.originalEvent.dataTransfer.dropEffect = 'none';
  6928. }
  6929. marker.hide();
  6930. })
  6931. .on('dnd_scroll.vakata.jstree', function (e, data) {
  6932. if(!data || !data.data || !data.data.jstree) { return; }
  6933. marker.hide();
  6934. lastmv = false;
  6935. lastev = false;
  6936. data.helper.find('.jstree-icon').first().removeClass('jstree-ok').addClass('jstree-er');
  6937. })
  6938. .on('dnd_stop.vakata.jstree', function (e, data) {
  6939. $('.jstree-dnd-parent').removeClass('jstree-dnd-parent');
  6940. if(opento) { clearTimeout(opento); }
  6941. if(!data || !data.data || !data.data.jstree) { return; }
  6942. marker.hide().detach();
  6943. var i, j, nodes = [];
  6944. if(lastmv) {
  6945. for(i = 0, j = data.data.nodes.length; i < j; i++) {
  6946. nodes[i] = data.data.origin ? data.data.origin.get_node(data.data.nodes[i]) : data.data.nodes[i];
  6947. }
  6948. lastmv.ins[ data.data.origin && (data.data.origin.settings.dnd.always_copy || (data.data.origin.settings.dnd.copy && (data.event.metaKey || data.event.ctrlKey))) ? 'copy_node' : 'move_node' ](nodes, lastmv.par, lastmv.pos, false, false, false, data.data.origin);
  6949. }
  6950. else {
  6951. i = $(data.event.target).closest('.jstree');
  6952. if(i.length && laster && laster.error && laster.error === 'check') {
  6953. i = i.jstree(true);
  6954. if(i) {
  6955. i.settings.core.error.call(this, laster);
  6956. }
  6957. }
  6958. }
  6959. lastev = false;
  6960. lastmv = false;
  6961. })
  6962. .on('keyup.jstree keydown.jstree', function (e, data) {
  6963. data = $.vakata.dnd._get();
  6964. if(data && data.data && data.data.jstree) {
  6965. if (e.type === "keyup" && e.which === 27) {
  6966. if (opento) { clearTimeout(opento); }
  6967. lastmv = false;
  6968. laster = false;
  6969. lastev = false;
  6970. opento = false;
  6971. marker.hide().detach();
  6972. $.vakata.dnd._clean();
  6973. } else {
  6974. data.helper.find('.jstree-copy').first()[ data.data.origin && (data.data.origin.settings.dnd.always_copy || (data.data.origin.settings.dnd.copy && (e.metaKey || e.ctrlKey))) ? 'show' : 'hide' ]();
  6975. if(lastev) {
  6976. lastev.metaKey = e.metaKey;
  6977. lastev.ctrlKey = e.ctrlKey;
  6978. $.vakata.dnd._trigger('move', lastev);
  6979. }
  6980. }
  6981. }
  6982. });
  6983. });
  6984. // helpers
  6985. (function ($) {
  6986. $.vakata.html = {
  6987. div : $('<div></div>'),
  6988. escape : function (str) {
  6989. return $.vakata.html.div.text(str).html();
  6990. },
  6991. strip : function (str) {
  6992. return $.vakata.html.div.empty().append($.parseHTML(str)).text();
  6993. }
  6994. };
  6995. // private variable
  6996. var vakata_dnd = {
  6997. element : false,
  6998. target : false,
  6999. is_down : false,
  7000. is_drag : false,
  7001. helper : false,
  7002. helper_w: 0,
  7003. data : false,
  7004. init_x : 0,
  7005. init_y : 0,
  7006. scroll_l: 0,
  7007. scroll_t: 0,
  7008. scroll_e: false,
  7009. scroll_i: false,
  7010. is_touch: false
  7011. };
  7012. $.vakata.dnd = {
  7013. settings : {
  7014. scroll_speed : 10,
  7015. scroll_proximity : 20,
  7016. helper_left : 5,
  7017. helper_top : 10,
  7018. threshold : 5,
  7019. threshold_touch : 10
  7020. },
  7021. _trigger : function (event_name, e, data) {
  7022. if (data === undefined) {
  7023. data = $.vakata.dnd._get();
  7024. }
  7025. data.event = e;
  7026. $(document).triggerHandler("dnd_" + event_name + ".vakata", data);
  7027. },
  7028. _get : function () {
  7029. return {
  7030. "data" : vakata_dnd.data,
  7031. "element" : vakata_dnd.element,
  7032. "helper" : vakata_dnd.helper
  7033. };
  7034. },
  7035. _clean : function () {
  7036. if(vakata_dnd.helper) { vakata_dnd.helper.remove(); }
  7037. if(vakata_dnd.scroll_i) { clearInterval(vakata_dnd.scroll_i); vakata_dnd.scroll_i = false; }
  7038. vakata_dnd = {
  7039. element : false,
  7040. target : false,
  7041. is_down : false,
  7042. is_drag : false,
  7043. helper : false,
  7044. helper_w: 0,
  7045. data : false,
  7046. init_x : 0,
  7047. init_y : 0,
  7048. scroll_l: 0,
  7049. scroll_t: 0,
  7050. scroll_e: false,
  7051. scroll_i: false,
  7052. is_touch: false
  7053. };
  7054. elm = null;
  7055. $(document).off("mousemove.vakata.jstree touchmove.vakata.jstree", $.vakata.dnd.drag);
  7056. $(document).off("mouseup.vakata.jstree touchend.vakata.jstree", $.vakata.dnd.stop);
  7057. },
  7058. _scroll : function (init_only) {
  7059. if(!vakata_dnd.scroll_e || (!vakata_dnd.scroll_l && !vakata_dnd.scroll_t)) {
  7060. if(vakata_dnd.scroll_i) { clearInterval(vakata_dnd.scroll_i); vakata_dnd.scroll_i = false; }
  7061. return false;
  7062. }
  7063. if(!vakata_dnd.scroll_i) {
  7064. vakata_dnd.scroll_i = setInterval($.vakata.dnd._scroll, 100);
  7065. return false;
  7066. }
  7067. if(init_only === true) { return false; }
  7068. var i = vakata_dnd.scroll_e.scrollTop(),
  7069. j = vakata_dnd.scroll_e.scrollLeft();
  7070. vakata_dnd.scroll_e.scrollTop(i + vakata_dnd.scroll_t * $.vakata.dnd.settings.scroll_speed);
  7071. vakata_dnd.scroll_e.scrollLeft(j + vakata_dnd.scroll_l * $.vakata.dnd.settings.scroll_speed);
  7072. if(i !== vakata_dnd.scroll_e.scrollTop() || j !== vakata_dnd.scroll_e.scrollLeft()) {
  7073. /**
  7074. * triggered on the document when a drag causes an element to scroll
  7075. * @event
  7076. * @plugin dnd
  7077. * @name dnd_scroll.vakata
  7078. * @param {Mixed} data any data supplied with the call to $.vakata.dnd.start
  7079. * @param {DOM} element the DOM element being dragged
  7080. * @param {jQuery} helper the helper shown next to the mouse
  7081. * @param {jQuery} event the element that is scrolling
  7082. */
  7083. $.vakata.dnd._trigger("scroll", vakata_dnd.scroll_e);
  7084. }
  7085. },
  7086. start : function (e, data, html) {
  7087. if(e.type === "touchstart" && e.originalEvent && e.originalEvent.changedTouches && e.originalEvent.changedTouches[0]) {
  7088. e.pageX = e.originalEvent.changedTouches[0].pageX;
  7089. e.pageY = e.originalEvent.changedTouches[0].pageY;
  7090. e.target = document.elementFromPoint(e.originalEvent.changedTouches[0].pageX - window.pageXOffset, e.originalEvent.changedTouches[0].pageY - window.pageYOffset);
  7091. }
  7092. if(vakata_dnd.is_drag) { $.vakata.dnd.stop({}); }
  7093. try {
  7094. e.currentTarget.unselectable = "on";
  7095. e.currentTarget.onselectstart = function() { return false; };
  7096. if(e.currentTarget.style) {
  7097. e.currentTarget.style.touchAction = "none";
  7098. e.currentTarget.style.msTouchAction = "none";
  7099. e.currentTarget.style.MozUserSelect = "none";
  7100. }
  7101. } catch(ignore) { }
  7102. vakata_dnd.init_x = e.pageX;
  7103. vakata_dnd.init_y = e.pageY;
  7104. vakata_dnd.data = data;
  7105. vakata_dnd.is_down = true;
  7106. vakata_dnd.element = e.currentTarget;
  7107. vakata_dnd.target = e.target;
  7108. vakata_dnd.is_touch = e.type === "touchstart";
  7109. if(html !== false) {
  7110. vakata_dnd.helper = $("<div id='vakata-dnd'></div>").html(html).css({
  7111. "display" : "block",
  7112. "margin" : "0",
  7113. "padding" : "0",
  7114. "position" : "absolute",
  7115. "top" : "-2000px",
  7116. "lineHeight" : "16px",
  7117. "zIndex" : "10000"
  7118. });
  7119. }
  7120. $(document).on("mousemove.vakata.jstree touchmove.vakata.jstree", $.vakata.dnd.drag);
  7121. $(document).on("mouseup.vakata.jstree touchend.vakata.jstree", $.vakata.dnd.stop);
  7122. return false;
  7123. },
  7124. drag : function (e) {
  7125. if(e.type === "touchmove" && e.originalEvent && e.originalEvent.changedTouches && e.originalEvent.changedTouches[0]) {
  7126. e.pageX = e.originalEvent.changedTouches[0].pageX;
  7127. e.pageY = e.originalEvent.changedTouches[0].pageY;
  7128. e.target = document.elementFromPoint(e.originalEvent.changedTouches[0].pageX - window.pageXOffset, e.originalEvent.changedTouches[0].pageY - window.pageYOffset);
  7129. }
  7130. if(!vakata_dnd.is_down) { return; }
  7131. if(!vakata_dnd.is_drag) {
  7132. if(
  7133. Math.abs(e.pageX - vakata_dnd.init_x) > (vakata_dnd.is_touch ? $.vakata.dnd.settings.threshold_touch : $.vakata.dnd.settings.threshold) ||
  7134. Math.abs(e.pageY - vakata_dnd.init_y) > (vakata_dnd.is_touch ? $.vakata.dnd.settings.threshold_touch : $.vakata.dnd.settings.threshold)
  7135. ) {
  7136. if(vakata_dnd.helper) {
  7137. vakata_dnd.helper.appendTo(document.body);
  7138. vakata_dnd.helper_w = vakata_dnd.helper.outerWidth();
  7139. }
  7140. vakata_dnd.is_drag = true;
  7141. $(vakata_dnd.target).one('click.vakata', false);
  7142. /**
  7143. * triggered on the document when a drag starts
  7144. * @event
  7145. * @plugin dnd
  7146. * @name dnd_start.vakata
  7147. * @param {Mixed} data any data supplied with the call to $.vakata.dnd.start
  7148. * @param {DOM} element the DOM element being dragged
  7149. * @param {jQuery} helper the helper shown next to the mouse
  7150. * @param {Object} event the event that caused the start (probably mousemove)
  7151. */
  7152. $.vakata.dnd._trigger("start", e);
  7153. }
  7154. else { return; }
  7155. }
  7156. var d = false, w = false,
  7157. dh = false, wh = false,
  7158. dw = false, ww = false,
  7159. dt = false, dl = false,
  7160. ht = false, hl = false;
  7161. vakata_dnd.scroll_t = 0;
  7162. vakata_dnd.scroll_l = 0;
  7163. vakata_dnd.scroll_e = false;
  7164. $($(e.target).parentsUntil("body").addBack().get().reverse())
  7165. .filter(function () {
  7166. return this.ownerDocument && (/^auto|scroll$/).test($(this).css("overflow")) &&
  7167. (this.scrollHeight > this.offsetHeight || this.scrollWidth > this.offsetWidth);
  7168. })
  7169. .each(function () {
  7170. var t = $(this), o = t.offset();
  7171. if(this.scrollHeight > this.offsetHeight) {
  7172. if(o.top + t.height() - e.pageY < $.vakata.dnd.settings.scroll_proximity) { vakata_dnd.scroll_t = 1; }
  7173. if(e.pageY - o.top < $.vakata.dnd.settings.scroll_proximity) { vakata_dnd.scroll_t = -1; }
  7174. }
  7175. if(this.scrollWidth > this.offsetWidth) {
  7176. if(o.left + t.width() - e.pageX < $.vakata.dnd.settings.scroll_proximity) { vakata_dnd.scroll_l = 1; }
  7177. if(e.pageX - o.left < $.vakata.dnd.settings.scroll_proximity) { vakata_dnd.scroll_l = -1; }
  7178. }
  7179. if(vakata_dnd.scroll_t || vakata_dnd.scroll_l) {
  7180. vakata_dnd.scroll_e = $(this);
  7181. return false;
  7182. }
  7183. });
  7184. if(!vakata_dnd.scroll_e) {
  7185. d = $(document); w = $(window);
  7186. dh = d.height(); wh = w.height();
  7187. dw = d.width(); ww = w.width();
  7188. dt = d.scrollTop(); dl = d.scrollLeft();
  7189. if(dh > wh && e.pageY - dt < $.vakata.dnd.settings.scroll_proximity) { vakata_dnd.scroll_t = -1; }
  7190. if(dh > wh && wh - (e.pageY - dt) < $.vakata.dnd.settings.scroll_proximity) { vakata_dnd.scroll_t = 1; }
  7191. if(dw > ww && e.pageX - dl < $.vakata.dnd.settings.scroll_proximity) { vakata_dnd.scroll_l = -1; }
  7192. if(dw > ww && ww - (e.pageX - dl) < $.vakata.dnd.settings.scroll_proximity) { vakata_dnd.scroll_l = 1; }
  7193. if(vakata_dnd.scroll_t || vakata_dnd.scroll_l) {
  7194. vakata_dnd.scroll_e = d;
  7195. }
  7196. }
  7197. if(vakata_dnd.scroll_e) { $.vakata.dnd._scroll(true); }
  7198. if(vakata_dnd.helper) {
  7199. ht = parseInt(e.pageY + $.vakata.dnd.settings.helper_top, 10);
  7200. hl = parseInt(e.pageX + $.vakata.dnd.settings.helper_left, 10);
  7201. if(dh && ht + 25 > dh) { ht = dh - 50; }
  7202. if(dw && hl + vakata_dnd.helper_w > dw) { hl = dw - (vakata_dnd.helper_w + 2); }
  7203. vakata_dnd.helper.css({
  7204. left : hl + "px",
  7205. top : ht + "px"
  7206. });
  7207. }
  7208. /**
  7209. * triggered on the document when a drag is in progress
  7210. * @event
  7211. * @plugin dnd
  7212. * @name dnd_move.vakata
  7213. * @param {Mixed} data any data supplied with the call to $.vakata.dnd.start
  7214. * @param {DOM} element the DOM element being dragged
  7215. * @param {jQuery} helper the helper shown next to the mouse
  7216. * @param {Object} event the event that caused this to trigger (most likely mousemove)
  7217. */
  7218. $.vakata.dnd._trigger("move", e);
  7219. return false;
  7220. },
  7221. stop : function (e) {
  7222. if(e.type === "touchend" && e.originalEvent && e.originalEvent.changedTouches && e.originalEvent.changedTouches[0]) {
  7223. e.pageX = e.originalEvent.changedTouches[0].pageX;
  7224. e.pageY = e.originalEvent.changedTouches[0].pageY;
  7225. e.target = document.elementFromPoint(e.originalEvent.changedTouches[0].pageX - window.pageXOffset, e.originalEvent.changedTouches[0].pageY - window.pageYOffset);
  7226. }
  7227. if(vakata_dnd.is_drag) {
  7228. /**
  7229. * triggered on the document when a drag stops (the dragged element is dropped)
  7230. * @event
  7231. * @plugin dnd
  7232. * @name dnd_stop.vakata
  7233. * @param {Mixed} data any data supplied with the call to $.vakata.dnd.start
  7234. * @param {DOM} element the DOM element being dragged
  7235. * @param {jQuery} helper the helper shown next to the mouse
  7236. * @param {Object} event the event that caused the stop
  7237. */
  7238. if (e.target !== vakata_dnd.target) {
  7239. $(vakata_dnd.target).off('click.vakata');
  7240. }
  7241. $.vakata.dnd._trigger("stop", e);
  7242. }
  7243. else {
  7244. if(e.type === "touchend" && e.target === vakata_dnd.target) {
  7245. var to = setTimeout(function () { $(e.target).trigger('click'); }, 100);
  7246. $(e.target).one('click', function() { if(to) { clearTimeout(to); } });
  7247. }
  7248. }
  7249. $.vakata.dnd._clean();
  7250. return false;
  7251. }
  7252. };
  7253. }($));
  7254. // include the dnd plugin by default
  7255. // $.jstree.defaults.plugins.push("dnd");
  7256. /**
  7257. * ### Massload plugin
  7258. *
  7259. * Adds massload functionality to jsTree, so that multiple nodes can be loaded in a single request (only useful with lazy loading).
  7260. */
  7261. /**
  7262. * massload configuration
  7263. *
  7264. * It is possible to set this to a standard jQuery-like AJAX config.
  7265. * In addition to the standard jQuery ajax options here you can supply functions for `data` and `url`, the functions will be run in the current instance's scope and a param will be passed indicating which node IDs need to be loaded, the return value of those functions will be used.
  7266. *
  7267. * You can also set this to a function, that function will receive the node IDs being loaded as argument and a second param which is a function (callback) which should be called with the result.
  7268. *
  7269. * Both the AJAX and the function approach rely on the same return value - an object where the keys are the node IDs, and the value is the children of that node as an array.
  7270. *
  7271. * {
  7272. * "id1" : [{ "text" : "Child of ID1", "id" : "c1" }, { "text" : "Another child of ID1", "id" : "c2" }],
  7273. * "id2" : [{ "text" : "Child of ID2", "id" : "c3" }]
  7274. * }
  7275. *
  7276. * @name $.jstree.defaults.massload
  7277. * @plugin massload
  7278. */
  7279. $.jstree.defaults.massload = null;
  7280. $.jstree.plugins.massload = function (options, parent) {
  7281. this.init = function (el, options) {
  7282. this._data.massload = {};
  7283. parent.init.call(this, el, options);
  7284. };
  7285. this._load_nodes = function (nodes, callback, is_callback, force_reload) {
  7286. var s = this.settings.massload,
  7287. toLoad = [],
  7288. m = this._model.data,
  7289. i, j, dom;
  7290. if (!is_callback) {
  7291. for(i = 0, j = nodes.length; i < j; i++) {
  7292. if(!m[nodes[i]] || ( (!m[nodes[i]].state.loaded && !m[nodes[i]].state.failed) || force_reload) ) {
  7293. toLoad.push(nodes[i]);
  7294. dom = this.get_node(nodes[i], true);
  7295. if (dom && dom.length) {
  7296. dom.addClass("jstree-loading").attr('aria-busy',true);
  7297. }
  7298. }
  7299. }
  7300. this._data.massload = {};
  7301. if (toLoad.length) {
  7302. if($.vakata.is_function(s)) {
  7303. return s.call(this, toLoad, function (data) {
  7304. var i, j;
  7305. if(data) {
  7306. for(i in data) {
  7307. if(data.hasOwnProperty(i)) {
  7308. this._data.massload[i] = data[i];
  7309. }
  7310. }
  7311. }
  7312. for(i = 0, j = nodes.length; i < j; i++) {
  7313. dom = this.get_node(nodes[i], true);
  7314. if (dom && dom.length) {
  7315. dom.removeClass("jstree-loading").attr('aria-busy',false);
  7316. }
  7317. }
  7318. parent._load_nodes.call(this, nodes, callback, is_callback, force_reload);
  7319. }.bind(this));
  7320. }
  7321. if(typeof s === 'object' && s && s.url) {
  7322. s = $.extend(true, {}, s);
  7323. if($.vakata.is_function(s.url)) {
  7324. s.url = s.url.call(this, toLoad);
  7325. }
  7326. if($.vakata.is_function(s.data)) {
  7327. s.data = s.data.call(this, toLoad);
  7328. }
  7329. return $.ajax(s)
  7330. .done(function (data,t,x) {
  7331. var i, j;
  7332. if(data) {
  7333. for(i in data) {
  7334. if(data.hasOwnProperty(i)) {
  7335. this._data.massload[i] = data[i];
  7336. }
  7337. }
  7338. }
  7339. for(i = 0, j = nodes.length; i < j; i++) {
  7340. dom = this.get_node(nodes[i], true);
  7341. if (dom && dom.length) {
  7342. dom.removeClass("jstree-loading").attr('aria-busy',false);
  7343. }
  7344. }
  7345. parent._load_nodes.call(this, nodes, callback, is_callback, force_reload);
  7346. }.bind(this))
  7347. .fail(function (f) {
  7348. parent._load_nodes.call(this, nodes, callback, is_callback, force_reload);
  7349. }.bind(this));
  7350. }
  7351. }
  7352. }
  7353. return parent._load_nodes.call(this, nodes, callback, is_callback, force_reload);
  7354. };
  7355. this._load_node = function (obj, callback) {
  7356. var data = this._data.massload[obj.id],
  7357. rslt = null, dom;
  7358. if(data) {
  7359. rslt = this[typeof data === 'string' ? '_append_html_data' : '_append_json_data'](
  7360. obj,
  7361. typeof data === 'string' ? $($.parseHTML(data)).filter(function () { return this.nodeType !== 3; }) : data,
  7362. function (status) { callback.call(this, status); }
  7363. );
  7364. dom = this.get_node(obj.id, true);
  7365. if (dom && dom.length) {
  7366. dom.removeClass("jstree-loading").attr('aria-busy',false);
  7367. }
  7368. delete this._data.massload[obj.id];
  7369. return rslt;
  7370. }
  7371. return parent._load_node.call(this, obj, callback);
  7372. };
  7373. };
  7374. /**
  7375. * ### Search plugin
  7376. *
  7377. * Adds search functionality to jsTree.
  7378. */
  7379. /**
  7380. * stores all defaults for the search plugin
  7381. * @name $.jstree.defaults.search
  7382. * @plugin search
  7383. */
  7384. $.jstree.defaults.search = {
  7385. /**
  7386. * a jQuery-like AJAX config, which jstree uses if a server should be queried for results.
  7387. *
  7388. * A `str` (which is the search string) parameter will be added with the request, an optional `inside` parameter will be added if the search is limited to a node id. The expected result is a JSON array with nodes that need to be opened so that matching nodes will be revealed.
  7389. * Leave this setting as `false` to not query the server. You can also set this to a function, which will be invoked in the instance's scope and receive 3 parameters - the search string, the callback to call with the array of nodes to load, and the optional node ID to limit the search to
  7390. * @name $.jstree.defaults.search.ajax
  7391. * @plugin search
  7392. */
  7393. ajax : false,
  7394. /**
  7395. * Indicates if the search should be fuzzy or not (should `chnd3` match `child node 3`). Default is `false`.
  7396. * @name $.jstree.defaults.search.fuzzy
  7397. * @plugin search
  7398. */
  7399. fuzzy : false,
  7400. /**
  7401. * Indicates if the search should be case sensitive. Default is `false`.
  7402. * @name $.jstree.defaults.search.case_sensitive
  7403. * @plugin search
  7404. */
  7405. case_sensitive : false,
  7406. /**
  7407. * Indicates if the tree should be filtered (by default) to show only matching nodes (keep in mind this can be a heavy on large trees in old browsers).
  7408. * This setting can be changed at runtime when calling the search method. Default is `false`.
  7409. * @name $.jstree.defaults.search.show_only_matches
  7410. * @plugin search
  7411. */
  7412. show_only_matches : false,
  7413. /**
  7414. * Indicates if the children of matched element are shown (when show_only_matches is true)
  7415. * This setting can be changed at runtime when calling the search method. Default is `false`.
  7416. * @name $.jstree.defaults.search.show_only_matches_children
  7417. * @plugin search
  7418. */
  7419. show_only_matches_children : false,
  7420. /**
  7421. * 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`.
  7422. * @name $.jstree.defaults.search.close_opened_onclear
  7423. * @plugin search
  7424. */
  7425. close_opened_onclear : true,
  7426. /**
  7427. * Indicates if only leaf nodes should be included in search results. Default is `false`.
  7428. * @name $.jstree.defaults.search.search_leaves_only
  7429. * @plugin search
  7430. */
  7431. search_leaves_only : false,
  7432. /**
  7433. * 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).
  7434. * 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`.
  7435. * @name $.jstree.defaults.search.search_callback
  7436. * @plugin search
  7437. */
  7438. search_callback : false
  7439. };
  7440. $.jstree.plugins.search = function (options, parent) {
  7441. this.bind = function () {
  7442. parent.bind.call(this);
  7443. this._data.search.str = "";
  7444. this._data.search.dom = $();
  7445. this._data.search.res = [];
  7446. this._data.search.opn = [];
  7447. this._data.search.som = false;
  7448. this._data.search.smc = false;
  7449. this._data.search.hdn = [];
  7450. this.element
  7451. .on("search.jstree", function (e, data) {
  7452. if(this._data.search.som && data.res.length) {
  7453. var m = this._model.data, i, j, p = [], k, l;
  7454. for(i = 0, j = data.res.length; i < j; i++) {
  7455. if(m[data.res[i]] && !m[data.res[i]].state.hidden) {
  7456. p.push(data.res[i]);
  7457. p = p.concat(m[data.res[i]].parents);
  7458. if(this._data.search.smc) {
  7459. for (k = 0, l = m[data.res[i]].children_d.length; k < l; k++) {
  7460. if (m[m[data.res[i]].children_d[k]] && !m[m[data.res[i]].children_d[k]].state.hidden) {
  7461. p.push(m[data.res[i]].children_d[k]);
  7462. }
  7463. }
  7464. }
  7465. }
  7466. }
  7467. p = $.vakata.array_remove_item($.vakata.array_unique(p), $.jstree.root);
  7468. this._data.search.hdn = this.hide_all(true);
  7469. this.show_node(p, true);
  7470. this.redraw(true);
  7471. }
  7472. }.bind(this))
  7473. .on("clear_search.jstree", function (e, data) {
  7474. if(this._data.search.som && data.res.length) {
  7475. this.show_node(this._data.search.hdn, true);
  7476. this.redraw(true);
  7477. }
  7478. }.bind(this));
  7479. };
  7480. /**
  7481. * used to search the tree nodes for a given string
  7482. * @name search(str [, skip_async])
  7483. * @param {String} str the search string
  7484. * @param {Boolean} skip_async if set to true server will not be queried even if configured
  7485. * @param {Boolean} show_only_matches if set to true only matching nodes will be shown (keep in mind this can be very slow on large trees or old browsers)
  7486. * @param {mixed} inside an optional node to whose children to limit the search
  7487. * @param {Boolean} append if set to true the results of this search are appended to the previous search
  7488. * @plugin search
  7489. * @trigger search.jstree
  7490. */
  7491. this.search = function (str, skip_async, show_only_matches, inside, append, show_only_matches_children) {
  7492. if(str === false || $.vakata.trim(str.toString()) === "") {
  7493. return this.clear_search();
  7494. }
  7495. inside = this.get_node(inside);
  7496. inside = inside && (inside.id || inside.id === 0) ? inside.id : null;
  7497. str = str.toString();
  7498. var s = this.settings.search,
  7499. a = s.ajax ? s.ajax : false,
  7500. m = this._model.data,
  7501. f = null,
  7502. r = [],
  7503. p = [], i, j;
  7504. if(this._data.search.res.length && !append) {
  7505. this.clear_search();
  7506. }
  7507. if(show_only_matches === undefined) {
  7508. show_only_matches = s.show_only_matches;
  7509. }
  7510. if(show_only_matches_children === undefined) {
  7511. show_only_matches_children = s.show_only_matches_children;
  7512. }
  7513. if(!skip_async && a !== false) {
  7514. if($.vakata.is_function(a)) {
  7515. return a.call(this, str, function (d) {
  7516. if(d && d.d) { d = d.d; }
  7517. this._load_nodes(!$.vakata.is_array(d) ? [] : $.vakata.array_unique(d), function () {
  7518. this.search(str, true, show_only_matches, inside, append, show_only_matches_children);
  7519. });
  7520. }.bind(this), inside);
  7521. }
  7522. else {
  7523. a = $.extend({}, a);
  7524. if(!a.data) { a.data = {}; }
  7525. a.data.str = str;
  7526. if(inside) {
  7527. a.data.inside = inside;
  7528. }
  7529. if (this._data.search.lastRequest) {
  7530. this._data.search.lastRequest.abort();
  7531. }
  7532. this._data.search.lastRequest = $.ajax(a)
  7533. .fail(function () {
  7534. this._data.core.last_error = { 'error' : 'ajax', 'plugin' : 'search', 'id' : 'search_01', 'reason' : 'Could not load search parents', 'data' : JSON.stringify(a) };
  7535. this.settings.core.error.call(this, this._data.core.last_error);
  7536. }.bind(this))
  7537. .done(function (d) {
  7538. if(d && d.d) { d = d.d; }
  7539. this._load_nodes(!$.vakata.is_array(d) ? [] : $.vakata.array_unique(d), function () {
  7540. this.search(str, true, show_only_matches, inside, append, show_only_matches_children);
  7541. });
  7542. }.bind(this));
  7543. return this._data.search.lastRequest;
  7544. }
  7545. }
  7546. if(!append) {
  7547. this._data.search.str = str;
  7548. this._data.search.dom = $();
  7549. this._data.search.res = [];
  7550. this._data.search.opn = [];
  7551. this._data.search.som = show_only_matches;
  7552. this._data.search.smc = show_only_matches_children;
  7553. }
  7554. f = new $.vakata.search(str, true, { caseSensitive : s.case_sensitive, fuzzy : s.fuzzy });
  7555. $.each(m[inside ? inside : $.jstree.root].children_d, function (ii, i) {
  7556. var v = m[i];
  7557. if(v.text && !v.state.hidden && (!s.search_leaves_only || (v.state.loaded && v.children.length === 0)) && ( (s.search_callback && s.search_callback.call(this, str, v)) || (!s.search_callback && f.search(v.text).isMatch) ) ) {
  7558. r.push(i);
  7559. p = p.concat(v.parents);
  7560. }
  7561. });
  7562. if(r.length) {
  7563. p = $.vakata.array_unique(p);
  7564. for(i = 0, j = p.length; i < j; i++) {
  7565. if(p[i] !== $.jstree.root && m[p[i]] && this.open_node(p[i], null, 0) === true) {
  7566. this._data.search.opn.push(p[i]);
  7567. }
  7568. }
  7569. if(!append) {
  7570. 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(', #')));
  7571. this._data.search.res = r;
  7572. }
  7573. else {
  7574. this._data.search.dom = this._data.search.dom.add($(this.element[0].querySelectorAll('#' + $.map(r, function (v) { return "0123456789".indexOf(v[0]) !== -1 ? '\\3' + v[0] + ' ' + v.substr(1).replace($.jstree.idregex,'\\$&') : v.replace($.jstree.idregex,'\\$&'); }).join(', #'))));
  7575. this._data.search.res = $.vakata.array_unique(this._data.search.res.concat(r));
  7576. }
  7577. this._data.search.dom.children(".jstree-anchor").addClass('jstree-search');
  7578. }
  7579. /**
  7580. * triggered after search is complete
  7581. * @event
  7582. * @name search.jstree
  7583. * @param {jQuery} nodes a jQuery collection of matching nodes
  7584. * @param {String} str the search string
  7585. * @param {Array} res a collection of objects represeing the matching nodes
  7586. * @plugin search
  7587. */
  7588. this.trigger('search', { nodes : this._data.search.dom, str : str, res : this._data.search.res, show_only_matches : show_only_matches });
  7589. };
  7590. /**
  7591. * used to clear the last search (removes classes and shows all nodes if filtering is on)
  7592. * @name clear_search()
  7593. * @plugin search
  7594. * @trigger clear_search.jstree
  7595. */
  7596. this.clear_search = function () {
  7597. if(this.settings.search.close_opened_onclear) {
  7598. this.close_node(this._data.search.opn, 0);
  7599. }
  7600. /**
  7601. * triggered after search is complete
  7602. * @event
  7603. * @name clear_search.jstree
  7604. * @param {jQuery} nodes a jQuery collection of matching nodes (the result from the last search)
  7605. * @param {String} str the search string (the last search string)
  7606. * @param {Array} res a collection of objects represeing the matching nodes (the result from the last search)
  7607. * @plugin search
  7608. */
  7609. this.trigger('clear_search', { 'nodes' : this._data.search.dom, str : this._data.search.str, res : this._data.search.res });
  7610. if(this._data.search.res.length) {
  7611. this._data.search.dom = $(this.element[0].querySelectorAll('#' + $.map(this._data.search.res, function (v) {
  7612. return "0123456789".indexOf(v[0]) !== -1 ? '\\3' + v[0] + ' ' + v.substr(1).replace($.jstree.idregex,'\\$&') : v.replace($.jstree.idregex,'\\$&');
  7613. }).join(', #')));
  7614. this._data.search.dom.children(".jstree-anchor").removeClass("jstree-search");
  7615. }
  7616. this._data.search.str = "";
  7617. this._data.search.res = [];
  7618. this._data.search.opn = [];
  7619. this._data.search.dom = $();
  7620. };
  7621. this.redraw_node = function(obj, deep, callback, force_render) {
  7622. obj = parent.redraw_node.apply(this, arguments);
  7623. if(obj) {
  7624. if($.inArray(obj.id, this._data.search.res) !== -1) {
  7625. var i, j, tmp = null;
  7626. for(i = 0, j = obj.childNodes.length; i < j; i++) {
  7627. if(obj.childNodes[i] && obj.childNodes[i].className && obj.childNodes[i].className.indexOf("jstree-anchor") !== -1) {
  7628. tmp = obj.childNodes[i];
  7629. break;
  7630. }
  7631. }
  7632. if(tmp) {
  7633. tmp.className += ' jstree-search';
  7634. }
  7635. }
  7636. }
  7637. return obj;
  7638. };
  7639. };
  7640. // helpers
  7641. (function ($) {
  7642. // from http://kiro.me/projects/fuse.html
  7643. $.vakata.search = function(pattern, txt, options) {
  7644. options = options || {};
  7645. options = $.extend({}, $.vakata.search.defaults, options);
  7646. if(options.fuzzy !== false) {
  7647. options.fuzzy = true;
  7648. }
  7649. pattern = options.caseSensitive ? pattern : pattern.toLowerCase();
  7650. var MATCH_LOCATION = options.location,
  7651. MATCH_DISTANCE = options.distance,
  7652. MATCH_THRESHOLD = options.threshold,
  7653. patternLen = pattern.length,
  7654. matchmask, pattern_alphabet, match_bitapScore, search;
  7655. if(patternLen > 32) {
  7656. options.fuzzy = false;
  7657. }
  7658. if(options.fuzzy) {
  7659. matchmask = 1 << (patternLen - 1);
  7660. pattern_alphabet = (function () {
  7661. var mask = {},
  7662. i = 0;
  7663. for (i = 0; i < patternLen; i++) {
  7664. mask[pattern.charAt(i)] = 0;
  7665. }
  7666. for (i = 0; i < patternLen; i++) {
  7667. mask[pattern.charAt(i)] |= 1 << (patternLen - i - 1);
  7668. }
  7669. return mask;
  7670. }());
  7671. match_bitapScore = function (e, x) {
  7672. var accuracy = e / patternLen,
  7673. proximity = Math.abs(MATCH_LOCATION - x);
  7674. if(!MATCH_DISTANCE) {
  7675. return proximity ? 1.0 : accuracy;
  7676. }
  7677. return accuracy + (proximity / MATCH_DISTANCE);
  7678. };
  7679. }
  7680. search = function (text) {
  7681. text = options.caseSensitive ? text.toString() : text.toString().toLowerCase();
  7682. if(pattern === text || text.indexOf(pattern) !== -1) {
  7683. return {
  7684. isMatch: true,
  7685. score: 0
  7686. };
  7687. }
  7688. if(!options.fuzzy) {
  7689. return {
  7690. isMatch: false,
  7691. score: 1
  7692. };
  7693. }
  7694. var i, j,
  7695. textLen = text.length,
  7696. scoreThreshold = MATCH_THRESHOLD,
  7697. bestLoc = text.indexOf(pattern, MATCH_LOCATION),
  7698. binMin, binMid,
  7699. binMax = patternLen + textLen,
  7700. lastRd, start, finish, rd, charMatch,
  7701. score = 1,
  7702. locations = [];
  7703. if (bestLoc !== -1) {
  7704. scoreThreshold = Math.min(match_bitapScore(0, bestLoc), scoreThreshold);
  7705. bestLoc = text.lastIndexOf(pattern, MATCH_LOCATION + patternLen);
  7706. if (bestLoc !== -1) {
  7707. scoreThreshold = Math.min(match_bitapScore(0, bestLoc), scoreThreshold);
  7708. }
  7709. }
  7710. bestLoc = -1;
  7711. for (i = 0; i < patternLen; i++) {
  7712. binMin = 0;
  7713. binMid = binMax;
  7714. while (binMin < binMid) {
  7715. if (match_bitapScore(i, MATCH_LOCATION + binMid) <= scoreThreshold) {
  7716. binMin = binMid;
  7717. } else {
  7718. binMax = binMid;
  7719. }
  7720. binMid = Math.floor((binMax - binMin) / 2 + binMin);
  7721. }
  7722. binMax = binMid;
  7723. start = Math.max(1, MATCH_LOCATION - binMid + 1);
  7724. finish = Math.min(MATCH_LOCATION + binMid, textLen) + patternLen;
  7725. rd = new Array(finish + 2);
  7726. rd[finish + 1] = (1 << i) - 1;
  7727. for (j = finish; j >= start; j--) {
  7728. charMatch = pattern_alphabet[text.charAt(j - 1)];
  7729. if (i === 0) {
  7730. rd[j] = ((rd[j + 1] << 1) | 1) & charMatch;
  7731. } else {
  7732. rd[j] = ((rd[j + 1] << 1) | 1) & charMatch | (((lastRd[j + 1] | lastRd[j]) << 1) | 1) | lastRd[j + 1];
  7733. }
  7734. if (rd[j] & matchmask) {
  7735. score = match_bitapScore(i, j - 1);
  7736. if (score <= scoreThreshold) {
  7737. scoreThreshold = score;
  7738. bestLoc = j - 1;
  7739. locations.push(bestLoc);
  7740. if (bestLoc > MATCH_LOCATION) {
  7741. start = Math.max(1, 2 * MATCH_LOCATION - bestLoc);
  7742. } else {
  7743. break;
  7744. }
  7745. }
  7746. }
  7747. }
  7748. if (match_bitapScore(i + 1, MATCH_LOCATION) > scoreThreshold) {
  7749. break;
  7750. }
  7751. lastRd = rd;
  7752. }
  7753. return {
  7754. isMatch: bestLoc >= 0,
  7755. score: score
  7756. };
  7757. };
  7758. return txt === true ? { 'search' : search } : search(txt);
  7759. };
  7760. $.vakata.search.defaults = {
  7761. location : 0,
  7762. distance : 100,
  7763. threshold : 0.6,
  7764. fuzzy : false,
  7765. caseSensitive : false
  7766. };
  7767. }($));
  7768. // include the search plugin by default
  7769. // $.jstree.defaults.plugins.push("search");
  7770. /**
  7771. * ### Sort plugin
  7772. *
  7773. * Automatically sorts all siblings in the tree according to a sorting function.
  7774. */
  7775. /**
  7776. * the settings function used to sort the nodes.
  7777. * It is executed in the tree's context, accepts two nodes as arguments and should return `1` or `-1`.
  7778. * @name $.jstree.defaults.sort
  7779. * @plugin sort
  7780. */
  7781. $.jstree.defaults.sort = function (a, b) {
  7782. //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);
  7783. return this.get_text(a) > this.get_text(b) ? 1 : -1;
  7784. };
  7785. $.jstree.plugins.sort = function (options, parent) {
  7786. this.bind = function () {
  7787. parent.bind.call(this);
  7788. this.element
  7789. .on("model.jstree", function (e, data) {
  7790. this.sort(data.parent, true);
  7791. }.bind(this))
  7792. .on("rename_node.jstree create_node.jstree", function (e, data) {
  7793. this.sort(data.parent || data.node.parent, false);
  7794. this.redraw_node(data.parent || data.node.parent, true);
  7795. }.bind(this))
  7796. .on("move_node.jstree copy_node.jstree", function (e, data) {
  7797. this.sort(data.parent, false);
  7798. this.redraw_node(data.parent, true);
  7799. }.bind(this));
  7800. };
  7801. /**
  7802. * used to sort a node's children
  7803. * @private
  7804. * @name sort(obj [, deep])
  7805. * @param {mixed} obj the node
  7806. * @param {Boolean} deep if set to `true` nodes are sorted recursively.
  7807. * @plugin sort
  7808. * @trigger search.jstree
  7809. */
  7810. this.sort = function (obj, deep) {
  7811. var i, j;
  7812. obj = this.get_node(obj);
  7813. if(obj && obj.children && obj.children.length) {
  7814. obj.children.sort(this.settings.sort.bind(this));
  7815. if(deep) {
  7816. for(i = 0, j = obj.children_d.length; i < j; i++) {
  7817. this.sort(obj.children_d[i], false);
  7818. }
  7819. }
  7820. }
  7821. };
  7822. };
  7823. // include the sort plugin by default
  7824. // $.jstree.defaults.plugins.push("sort");
  7825. /**
  7826. * ### State plugin
  7827. *
  7828. * Saves the state of the tree (selected nodes, opened nodes) on the user's computer using available options (localStorage, cookies, etc)
  7829. */
  7830. var to = false;
  7831. /**
  7832. * stores all defaults for the state plugin
  7833. * @name $.jstree.defaults.state
  7834. * @plugin state
  7835. */
  7836. $.jstree.defaults.state = {
  7837. /**
  7838. * A string for the key to use when saving the current tree (change if using multiple trees in your project). Defaults to `jstree`.
  7839. * @name $.jstree.defaults.state.key
  7840. * @plugin state
  7841. */
  7842. key : 'jstree',
  7843. /**
  7844. * A space separated list of events that trigger a state save. Defaults to `changed.jstree open_node.jstree close_node.jstree`.
  7845. * @name $.jstree.defaults.state.events
  7846. * @plugin state
  7847. */
  7848. events : 'changed.jstree open_node.jstree close_node.jstree check_node.jstree uncheck_node.jstree',
  7849. /**
  7850. * Time in milliseconds after which the state will expire. Defaults to 'false' meaning - no expire.
  7851. * @name $.jstree.defaults.state.ttl
  7852. * @plugin state
  7853. */
  7854. ttl : false,
  7855. /**
  7856. * 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.
  7857. * @name $.jstree.defaults.state.filter
  7858. * @plugin state
  7859. */
  7860. filter : false,
  7861. /**
  7862. * Should loaded nodes be restored (setting this to true means that it is possible that the whole tree will be loaded for some users - use with caution). Defaults to `false`
  7863. * @name $.jstree.defaults.state.preserve_loaded
  7864. * @plugin state
  7865. */
  7866. preserve_loaded : false
  7867. };
  7868. $.jstree.plugins.state = function (options, parent) {
  7869. this.bind = function () {
  7870. parent.bind.call(this);
  7871. var bind = function () {
  7872. this.element.on(this.settings.state.events, function () {
  7873. if(to) { clearTimeout(to); }
  7874. to = setTimeout(function () { this.save_state(); }.bind(this), 100);
  7875. }.bind(this));
  7876. /**
  7877. * triggered when the state plugin is finished restoring the state (and immediately after ready if there is no state to restore).
  7878. * @event
  7879. * @name state_ready.jstree
  7880. * @plugin state
  7881. */
  7882. this.trigger('state_ready');
  7883. }.bind(this);
  7884. this.element
  7885. .on("ready.jstree", function (e, data) {
  7886. this.element.one("restore_state.jstree", bind);
  7887. if(!this.restore_state()) { bind(); }
  7888. }.bind(this));
  7889. };
  7890. /**
  7891. * save the state
  7892. * @name save_state()
  7893. * @plugin state
  7894. */
  7895. this.save_state = function () {
  7896. var tm = this.get_state();
  7897. if (!this.settings.state.preserve_loaded) {
  7898. delete tm.core.loaded;
  7899. }
  7900. var st = { 'state' : tm, 'ttl' : this.settings.state.ttl, 'sec' : +(new Date()) };
  7901. $.vakata.storage.set(this.settings.state.key, JSON.stringify(st));
  7902. };
  7903. /**
  7904. * restore the state from the user's computer
  7905. * @name restore_state()
  7906. * @plugin state
  7907. */
  7908. this.restore_state = function () {
  7909. var k = $.vakata.storage.get(this.settings.state.key);
  7910. if(!!k) { try { k = JSON.parse(k); } catch(ex) { return false; } }
  7911. if(!!k && k.ttl && k.sec && +(new Date()) - k.sec > k.ttl) { return false; }
  7912. if(!!k && k.state) { k = k.state; }
  7913. if(!!k && $.vakata.is_function(this.settings.state.filter)) { k = this.settings.state.filter.call(this, k); }
  7914. if(!!k) {
  7915. if (!this.settings.state.preserve_loaded) {
  7916. delete k.core.loaded;
  7917. }
  7918. this.element.one("set_state.jstree", function (e, data) { data.instance.trigger('restore_state', { 'state' : $.extend(true, {}, k) }); });
  7919. this.set_state(k);
  7920. return true;
  7921. }
  7922. return false;
  7923. };
  7924. /**
  7925. * clear the state on the user's computer
  7926. * @name clear_state()
  7927. * @plugin state
  7928. */
  7929. this.clear_state = function () {
  7930. return $.vakata.storage.del(this.settings.state.key);
  7931. };
  7932. };
  7933. (function ($, undefined) {
  7934. $.vakata.storage = {
  7935. // simply specifying the functions in FF throws an error
  7936. set : function (key, val) { return window.localStorage.setItem(key, val); },
  7937. get : function (key) { return window.localStorage.getItem(key); },
  7938. del : function (key) { return window.localStorage.removeItem(key); }
  7939. };
  7940. }($));
  7941. // include the state plugin by default
  7942. // $.jstree.defaults.plugins.push("state");
  7943. /**
  7944. * ### Types plugin
  7945. *
  7946. * 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.
  7947. */
  7948. /**
  7949. * 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).
  7950. *
  7951. * * `max_children` the maximum number of immediate children this node type can have. Do not specify or set to `-1` for unlimited.
  7952. * * `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.
  7953. * * `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.
  7954. * * `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.
  7955. * * `li_attr` an object of values which will be used to add HTML attributes on the resulting LI DOM node (merged with the node's own data)
  7956. * * `a_attr` an object of values which will be used to add HTML attributes on the resulting A DOM node (merged with the node's own data)
  7957. *
  7958. * There are two predefined types:
  7959. *
  7960. * * `#` represents the root of the tree, for example `max_children` would control the maximum number of root nodes.
  7961. * * `default` represents the default node - any settings here will be applied to all nodes that do not have a type specified.
  7962. *
  7963. * @name $.jstree.defaults.types
  7964. * @plugin types
  7965. */
  7966. $.jstree.defaults.types = {
  7967. 'default' : {}
  7968. };
  7969. $.jstree.defaults.types[$.jstree.root] = {};
  7970. $.jstree.plugins.types = function (options, parent) {
  7971. this.init = function (el, options) {
  7972. var i, j;
  7973. if(options && options.types && options.types['default']) {
  7974. for(i in options.types) {
  7975. if(i !== "default" && i !== $.jstree.root && options.types.hasOwnProperty(i)) {
  7976. for(j in options.types['default']) {
  7977. if(options.types['default'].hasOwnProperty(j) && options.types[i][j] === undefined) {
  7978. options.types[i][j] = options.types['default'][j];
  7979. }
  7980. }
  7981. }
  7982. }
  7983. }
  7984. parent.init.call(this, el, options);
  7985. this._model.data[$.jstree.root].type = $.jstree.root;
  7986. };
  7987. this.refresh = function (skip_loading, forget_state) {
  7988. parent.refresh.call(this, skip_loading, forget_state);
  7989. this._model.data[$.jstree.root].type = $.jstree.root;
  7990. };
  7991. this.bind = function () {
  7992. this.element
  7993. .on('model.jstree', function (e, data) {
  7994. var m = this._model.data,
  7995. dpc = data.nodes,
  7996. t = this.settings.types,
  7997. i, j, c = 'default', k;
  7998. for(i = 0, j = dpc.length; i < j; i++) {
  7999. c = 'default';
  8000. if(m[dpc[i]].original && m[dpc[i]].original.type && t[m[dpc[i]].original.type]) {
  8001. c = m[dpc[i]].original.type;
  8002. }
  8003. if(m[dpc[i]].data && m[dpc[i]].data.jstree && m[dpc[i]].data.jstree.type && t[m[dpc[i]].data.jstree.type]) {
  8004. c = m[dpc[i]].data.jstree.type;
  8005. }
  8006. m[dpc[i]].type = c;
  8007. if(m[dpc[i]].icon === true && t[c].icon !== undefined) {
  8008. m[dpc[i]].icon = t[c].icon;
  8009. }
  8010. if(t[c].li_attr !== undefined && typeof t[c].li_attr === 'object') {
  8011. for (k in t[c].li_attr) {
  8012. if (t[c].li_attr.hasOwnProperty(k)) {
  8013. if (k === 'id') {
  8014. continue;
  8015. }
  8016. else if (m[dpc[i]].li_attr[k] === undefined) {
  8017. m[dpc[i]].li_attr[k] = t[c].li_attr[k];
  8018. }
  8019. else if (k === 'class') {
  8020. m[dpc[i]].li_attr['class'] = t[c].li_attr['class'] + ' ' + m[dpc[i]].li_attr['class'];
  8021. }
  8022. }
  8023. }
  8024. }
  8025. if(t[c].a_attr !== undefined && typeof t[c].a_attr === 'object') {
  8026. for (k in t[c].a_attr) {
  8027. if (t[c].a_attr.hasOwnProperty(k)) {
  8028. if (k === 'id') {
  8029. continue;
  8030. }
  8031. else if (m[dpc[i]].a_attr[k] === undefined) {
  8032. m[dpc[i]].a_attr[k] = t[c].a_attr[k];
  8033. }
  8034. else if (k === 'href' && m[dpc[i]].a_attr[k] === '#') {
  8035. m[dpc[i]].a_attr['href'] = t[c].a_attr['href'];
  8036. }
  8037. else if (k === 'class') {
  8038. m[dpc[i]].a_attr['class'] = t[c].a_attr['class'] + ' ' + m[dpc[i]].a_attr['class'];
  8039. }
  8040. }
  8041. }
  8042. }
  8043. }
  8044. m[$.jstree.root].type = $.jstree.root;
  8045. }.bind(this));
  8046. parent.bind.call(this);
  8047. };
  8048. this.get_json = function (obj, options, flat) {
  8049. var i, j,
  8050. m = this._model.data,
  8051. opt = options ? $.extend(true, {}, options, {no_id:false}) : {},
  8052. tmp = parent.get_json.call(this, obj, opt, flat);
  8053. if(tmp === false) { return false; }
  8054. if($.vakata.is_array(tmp)) {
  8055. for(i = 0, j = tmp.length; i < j; i++) {
  8056. tmp[i].type = (tmp[i].id || tmp[i].id === 0) && m[tmp[i].id] && m[tmp[i].id].type ? m[tmp[i].id].type : "default";
  8057. if(options && options.no_id) {
  8058. delete tmp[i].id;
  8059. if(tmp[i].li_attr && tmp[i].li_attr.id) {
  8060. delete tmp[i].li_attr.id;
  8061. }
  8062. if(tmp[i].a_attr && tmp[i].a_attr.id) {
  8063. delete tmp[i].a_attr.id;
  8064. }
  8065. }
  8066. }
  8067. }
  8068. else {
  8069. tmp.type = (tmp.id || tmp.id === 0) && m[tmp.id] && m[tmp.id].type ? m[tmp.id].type : "default";
  8070. if(options && options.no_id) {
  8071. tmp = this._delete_ids(tmp);
  8072. }
  8073. }
  8074. return tmp;
  8075. };
  8076. this._delete_ids = function (tmp) {
  8077. if($.vakata.is_array(tmp)) {
  8078. for(var i = 0, j = tmp.length; i < j; i++) {
  8079. tmp[i] = this._delete_ids(tmp[i]);
  8080. }
  8081. return tmp;
  8082. }
  8083. delete tmp.id;
  8084. if(tmp.li_attr && tmp.li_attr.id) {
  8085. delete tmp.li_attr.id;
  8086. }
  8087. if(tmp.a_attr && tmp.a_attr.id) {
  8088. delete tmp.a_attr.id;
  8089. }
  8090. if(tmp.children && $.vakata.is_array(tmp.children)) {
  8091. tmp.children = this._delete_ids(tmp.children);
  8092. }
  8093. return tmp;
  8094. };
  8095. this.check = function (chk, obj, par, pos, more) {
  8096. if(parent.check.call(this, chk, obj, par, pos, more) === false) { return false; }
  8097. obj = obj && (obj.id || obj.id === 0) ? obj : this.get_node(obj);
  8098. par = par && (par.id || par.id === 0) ? par : this.get_node(par);
  8099. var m = obj && (obj.id || obj.id === 0) ? (more && more.origin ? more.origin : $.jstree.reference(obj.id)) : null, tmp, d, i, j;
  8100. m = m && m._model && m._model.data ? m._model.data : null;
  8101. switch(chk) {
  8102. case "create_node":
  8103. case "move_node":
  8104. case "copy_node":
  8105. if(chk !== 'move_node' || $.inArray(obj.id, par.children) === -1) {
  8106. tmp = this.get_rules(par);
  8107. if(tmp.max_children !== undefined && tmp.max_children !== -1 && tmp.max_children === par.children.length) {
  8108. this._data.core.last_error = { 'error' : 'check', 'plugin' : 'types', 'id' : 'types_01', 'reason' : 'max_children prevents function: ' + chk, 'data' : JSON.stringify({ 'chk' : chk, 'pos' : pos, 'obj' : obj && (obj.id || obj.id === 0) ? obj.id : false, 'par' : par && (par.id || par.id === 0) ? par.id : false }) };
  8109. return false;
  8110. }
  8111. if(tmp.valid_children !== undefined && tmp.valid_children !== -1 && $.inArray((obj.type || 'default'), tmp.valid_children) === -1) {
  8112. this._data.core.last_error = { 'error' : 'check', 'plugin' : 'types', 'id' : 'types_02', 'reason' : 'valid_children prevents function: ' + chk, 'data' : JSON.stringify({ 'chk' : chk, 'pos' : pos, 'obj' : obj && (obj.id || obj.id === 0) ? obj.id : false, 'par' : par && (par.id || par.id === 0) ? par.id : false }) };
  8113. return false;
  8114. }
  8115. if(m && obj.children_d && obj.parents) {
  8116. d = 0;
  8117. for(i = 0, j = obj.children_d.length; i < j; i++) {
  8118. d = Math.max(d, m[obj.children_d[i]].parents.length);
  8119. }
  8120. d = d - obj.parents.length + 1;
  8121. }
  8122. if(d <= 0 || d === undefined) { d = 1; }
  8123. do {
  8124. if(tmp.max_depth !== undefined && tmp.max_depth !== -1 && tmp.max_depth < d) {
  8125. this._data.core.last_error = { 'error' : 'check', 'plugin' : 'types', 'id' : 'types_03', 'reason' : 'max_depth prevents function: ' + chk, 'data' : JSON.stringify({ 'chk' : chk, 'pos' : pos, 'obj' : obj && (obj.id || obj.id === 0) ? obj.id : false, 'par' : par && (par.id || par.id === 0) ? par.id : false }) };
  8126. return false;
  8127. }
  8128. par = this.get_node(par.parent);
  8129. tmp = this.get_rules(par);
  8130. d++;
  8131. } while(par);
  8132. }
  8133. break;
  8134. }
  8135. return true;
  8136. };
  8137. /**
  8138. * used to retrieve the type settings object for a node
  8139. * @name get_rules(obj)
  8140. * @param {mixed} obj the node to find the rules for
  8141. * @return {Object}
  8142. * @plugin types
  8143. */
  8144. this.get_rules = function (obj) {
  8145. obj = this.get_node(obj);
  8146. if(!obj) { return false; }
  8147. var tmp = this.get_type(obj, true);
  8148. if(tmp.max_depth === undefined) { tmp.max_depth = -1; }
  8149. if(tmp.max_children === undefined) { tmp.max_children = -1; }
  8150. if(tmp.valid_children === undefined) { tmp.valid_children = -1; }
  8151. return tmp;
  8152. };
  8153. /**
  8154. * used to retrieve the type string or settings object for a node
  8155. * @name get_type(obj [, rules])
  8156. * @param {mixed} obj the node to find the rules for
  8157. * @param {Boolean} rules if set to `true` instead of a string the settings object will be returned
  8158. * @return {String|Object}
  8159. * @plugin types
  8160. */
  8161. this.get_type = function (obj, rules) {
  8162. obj = this.get_node(obj);
  8163. return (!obj) ? false : ( rules ? $.extend({ 'type' : obj.type }, this.settings.types[obj.type]) : obj.type);
  8164. };
  8165. /**
  8166. * used to change a node's type
  8167. * @name set_type(obj, type)
  8168. * @param {mixed} obj the node to change
  8169. * @param {String} type the new type
  8170. * @plugin types
  8171. */
  8172. this.set_type = function (obj, type) {
  8173. var m = this._model.data, t, t1, t2, old_type, old_icon, k, d, a;
  8174. if($.vakata.is_array(obj)) {
  8175. obj = obj.slice();
  8176. for(t1 = 0, t2 = obj.length; t1 < t2; t1++) {
  8177. this.set_type(obj[t1], type);
  8178. }
  8179. return true;
  8180. }
  8181. t = this.settings.types;
  8182. obj = this.get_node(obj);
  8183. if(!t[type] || !obj) { return false; }
  8184. d = this.get_node(obj, true);
  8185. if (d && d.length) {
  8186. a = d.children('.jstree-anchor');
  8187. }
  8188. old_type = obj.type;
  8189. old_icon = this.get_icon(obj);
  8190. obj.type = type;
  8191. if(old_icon === true || !t[old_type] || (t[old_type].icon !== undefined && old_icon === t[old_type].icon)) {
  8192. this.set_icon(obj, t[type].icon !== undefined ? t[type].icon : true);
  8193. }
  8194. // remove old type props
  8195. if(t[old_type] && t[old_type].li_attr !== undefined && typeof t[old_type].li_attr === 'object') {
  8196. for (k in t[old_type].li_attr) {
  8197. if (t[old_type].li_attr.hasOwnProperty(k)) {
  8198. if (k === 'id') {
  8199. continue;
  8200. }
  8201. else if (k === 'class') {
  8202. m[obj.id].li_attr['class'] = (m[obj.id].li_attr['class'] || '').replace(t[old_type].li_attr[k], '');
  8203. if (d) { d.removeClass(t[old_type].li_attr[k]); }
  8204. }
  8205. else if (m[obj.id].li_attr[k] === t[old_type].li_attr[k]) {
  8206. m[obj.id].li_attr[k] = null;
  8207. if (d) { d.removeAttr(k); }
  8208. }
  8209. }
  8210. }
  8211. }
  8212. if(t[old_type] && t[old_type].a_attr !== undefined && typeof t[old_type].a_attr === 'object') {
  8213. for (k in t[old_type].a_attr) {
  8214. if (t[old_type].a_attr.hasOwnProperty(k)) {
  8215. if (k === 'id') {
  8216. continue;
  8217. }
  8218. else if (k === 'class') {
  8219. m[obj.id].a_attr['class'] = (m[obj.id].a_attr['class'] || '').replace(t[old_type].a_attr[k], '');
  8220. if (a) { a.removeClass(t[old_type].a_attr[k]); }
  8221. }
  8222. else if (m[obj.id].a_attr[k] === t[old_type].a_attr[k]) {
  8223. if (k === 'href') {
  8224. m[obj.id].a_attr[k] = '#';
  8225. if (a) { a.attr('href', '#'); }
  8226. }
  8227. else {
  8228. delete m[obj.id].a_attr[k];
  8229. if (a) { a.removeAttr(k); }
  8230. }
  8231. }
  8232. }
  8233. }
  8234. }
  8235. // add new props
  8236. if(t[type].li_attr !== undefined && typeof t[type].li_attr === 'object') {
  8237. for (k in t[type].li_attr) {
  8238. if (t[type].li_attr.hasOwnProperty(k)) {
  8239. if (k === 'id') {
  8240. continue;
  8241. }
  8242. else if (m[obj.id].li_attr[k] === undefined) {
  8243. m[obj.id].li_attr[k] = t[type].li_attr[k];
  8244. if (d) {
  8245. if (k === 'class') {
  8246. d.addClass(t[type].li_attr[k]);
  8247. }
  8248. else {
  8249. d.attr(k, t[type].li_attr[k]);
  8250. }
  8251. }
  8252. }
  8253. else if (k === 'class') {
  8254. m[obj.id].li_attr['class'] = t[type].li_attr[k] + ' ' + m[obj.id].li_attr['class'];
  8255. if (d) { d.addClass(t[type].li_attr[k]); }
  8256. }
  8257. }
  8258. }
  8259. }
  8260. if(t[type].a_attr !== undefined && typeof t[type].a_attr === 'object') {
  8261. for (k in t[type].a_attr) {
  8262. if (t[type].a_attr.hasOwnProperty(k)) {
  8263. if (k === 'id') {
  8264. continue;
  8265. }
  8266. else if (m[obj.id].a_attr[k] === undefined) {
  8267. m[obj.id].a_attr[k] = t[type].a_attr[k];
  8268. if (a) {
  8269. if (k === 'class') {
  8270. a.addClass(t[type].a_attr[k]);
  8271. }
  8272. else {
  8273. a.attr(k, t[type].a_attr[k]);
  8274. }
  8275. }
  8276. }
  8277. else if (k === 'href' && m[obj.id].a_attr[k] === '#') {
  8278. m[obj.id].a_attr['href'] = t[type].a_attr['href'];
  8279. if (a) { a.attr('href', t[type].a_attr['href']); }
  8280. }
  8281. else if (k === 'class') {
  8282. m[obj.id].a_attr['class'] = t[type].a_attr['class'] + ' ' + m[obj.id].a_attr['class'];
  8283. if (a) { a.addClass(t[type].a_attr[k]); }
  8284. }
  8285. }
  8286. }
  8287. }
  8288. return true;
  8289. };
  8290. };
  8291. // include the types plugin by default
  8292. // $.jstree.defaults.plugins.push("types");
  8293. /**
  8294. * ### Unique plugin
  8295. *
  8296. * Enforces that no nodes with the same name can coexist as siblings.
  8297. */
  8298. /**
  8299. * stores all defaults for the unique plugin
  8300. * @name $.jstree.defaults.unique
  8301. * @plugin unique
  8302. */
  8303. $.jstree.defaults.unique = {
  8304. /**
  8305. * Indicates if the comparison should be case sensitive. Default is `false`.
  8306. * @name $.jstree.defaults.unique.case_sensitive
  8307. * @plugin unique
  8308. */
  8309. case_sensitive : false,
  8310. /**
  8311. * Indicates if white space should be trimmed before the comparison. Default is `false`.
  8312. * @name $.jstree.defaults.unique.trim_whitespace
  8313. * @plugin unique
  8314. */
  8315. trim_whitespace : false,
  8316. /**
  8317. * A callback executed in the instance's scope when a new node is created with no name and a node with the default name already exists, the two arguments are the conflicting name and the counter. The default will produce results like `New node (2)`.
  8318. * @name $.jstree.defaults.unique.duplicate
  8319. * @plugin unique
  8320. */
  8321. duplicate : function (name, counter) {
  8322. return name + ' (' + counter + ')';
  8323. }
  8324. };
  8325. $.jstree.plugins.unique = function (options, parent) {
  8326. this.check = function (chk, obj, par, pos, more) {
  8327. if(parent.check.call(this, chk, obj, par, pos, more) === false) { return false; }
  8328. obj = obj && (obj.id || obj.id === 0) ? obj : this.get_node(obj);
  8329. par = par && (par.id || par.id === 0) ? par : this.get_node(par);
  8330. if(!par || !par.children) { return true; }
  8331. var n = chk === "rename_node" ? pos : obj.text,
  8332. c = [],
  8333. s = this.settings.unique.case_sensitive,
  8334. w = this.settings.unique.trim_whitespace,
  8335. m = this._model.data, i, j, t;
  8336. for(i = 0, j = par.children.length; i < j; i++) {
  8337. t = m[par.children[i]].text;
  8338. if (!s) {
  8339. t = t.toLowerCase();
  8340. }
  8341. if (w) {
  8342. t = t.replace(/^[\s\uFEFF\xA0]+|[\s\uFEFF\xA0]+$/g, '');
  8343. }
  8344. c.push(t);
  8345. }
  8346. if(!s) { n = n.toLowerCase(); }
  8347. if (w) { n = n.replace(/^[\s\uFEFF\xA0]+|[\s\uFEFF\xA0]+$/g, ''); }
  8348. switch(chk) {
  8349. case "delete_node":
  8350. return true;
  8351. case "rename_node":
  8352. t = obj.text || '';
  8353. if (!s) {
  8354. t = t.toLowerCase();
  8355. }
  8356. if (w) {
  8357. t = t.replace(/^[\s\uFEFF\xA0]+|[\s\uFEFF\xA0]+$/g, '');
  8358. }
  8359. i = ($.inArray(n, c) === -1 || (obj.text && t === n));
  8360. if(!i) {
  8361. this._data.core.last_error = { 'error' : 'check', 'plugin' : 'unique', 'id' : 'unique_01', 'reason' : 'Child with name ' + n + ' already exists. Preventing: ' + chk, 'data' : JSON.stringify({ 'chk' : chk, 'pos' : pos, 'obj' : obj && (obj.id || obj.id === 0) ? obj.id : false, 'par' : par && (par.id || par.id === 0) ? par.id : false }) };
  8362. }
  8363. return i;
  8364. case "create_node":
  8365. i = ($.inArray(n, c) === -1);
  8366. if(!i) {
  8367. this._data.core.last_error = { 'error' : 'check', 'plugin' : 'unique', 'id' : 'unique_04', 'reason' : 'Child with name ' + n + ' already exists. Preventing: ' + chk, 'data' : JSON.stringify({ 'chk' : chk, 'pos' : pos, 'obj' : obj && (obj.id || obj.id === 0) ? obj.id : false, 'par' : par && (par.id || par.id === 0) ? par.id : false }) };
  8368. }
  8369. return i;
  8370. case "copy_node":
  8371. i = ($.inArray(n, c) === -1);
  8372. if(!i) {
  8373. this._data.core.last_error = { 'error' : 'check', 'plugin' : 'unique', 'id' : 'unique_02', 'reason' : 'Child with name ' + n + ' already exists. Preventing: ' + chk, 'data' : JSON.stringify({ 'chk' : chk, 'pos' : pos, 'obj' : obj && (obj.id || obj.id === 0) ? obj.id : false, 'par' : par && (par.id || par.id === 0) ? par.id : false }) };
  8374. }
  8375. return i;
  8376. case "move_node":
  8377. i = ( (obj.parent === par.id && (!more || !more.is_multi)) || $.inArray(n, c) === -1);
  8378. if(!i) {
  8379. this._data.core.last_error = { 'error' : 'check', 'plugin' : 'unique', 'id' : 'unique_03', 'reason' : 'Child with name ' + n + ' already exists. Preventing: ' + chk, 'data' : JSON.stringify({ 'chk' : chk, 'pos' : pos, 'obj' : obj && (obj.id || obj.id === 0) ? obj.id : false, 'par' : par && (par.id || par.id === 0) ? par.id : false }) };
  8380. }
  8381. return i;
  8382. }
  8383. return true;
  8384. };
  8385. this.create_node = function (par, node, pos, callback, is_loaded) {
  8386. if(!node || (typeof node === 'object' && node.text === undefined)) {
  8387. if(par === null) {
  8388. par = $.jstree.root;
  8389. }
  8390. par = this.get_node(par);
  8391. if(!par) {
  8392. return parent.create_node.call(this, par, node, pos, callback, is_loaded);
  8393. }
  8394. pos = pos === undefined ? "last" : pos;
  8395. if(!pos.toString().match(/^(before|after)$/) && !is_loaded && !this.is_loaded(par)) {
  8396. return parent.create_node.call(this, par, node, pos, callback, is_loaded);
  8397. }
  8398. if(!node) { node = {}; }
  8399. var tmp, n, dpc, i, j, m = this._model.data, s = this.settings.unique.case_sensitive, w = this.settings.unique.trim_whitespace, cb = this.settings.unique.duplicate, t;
  8400. n = tmp = this.get_string('New node');
  8401. dpc = [];
  8402. for(i = 0, j = par.children.length; i < j; i++) {
  8403. t = m[par.children[i]].text;
  8404. if (!s) {
  8405. t = t.toLowerCase();
  8406. }
  8407. if (w) {
  8408. t = t.replace(/^[\s\uFEFF\xA0]+|[\s\uFEFF\xA0]+$/g, '');
  8409. }
  8410. dpc.push(t);
  8411. }
  8412. i = 1;
  8413. t = n;
  8414. if (!s) {
  8415. t = t.toLowerCase();
  8416. }
  8417. if (w) {
  8418. t = t.replace(/^[\s\uFEFF\xA0]+|[\s\uFEFF\xA0]+$/g, '');
  8419. }
  8420. while($.inArray(t, dpc) !== -1) {
  8421. n = cb.call(this, tmp, (++i)).toString();
  8422. t = n;
  8423. if (!s) {
  8424. t = t.toLowerCase();
  8425. }
  8426. if (w) {
  8427. t = t.replace(/^[\s\uFEFF\xA0]+|[\s\uFEFF\xA0]+$/g, '');
  8428. }
  8429. }
  8430. node.text = n;
  8431. }
  8432. return parent.create_node.call(this, par, node, pos, callback, is_loaded);
  8433. };
  8434. };
  8435. // include the unique plugin by default
  8436. // $.jstree.defaults.plugins.push("unique");
  8437. /**
  8438. * ### Wholerow plugin
  8439. *
  8440. * Makes each node appear block level. Making selection easier. May cause slow down for large trees in old browsers.
  8441. */
  8442. var div = document.createElement('DIV');
  8443. div.setAttribute('unselectable','on');
  8444. div.setAttribute('role','presentation');
  8445. div.className = 'jstree-wholerow';
  8446. div.innerHTML = '&#160;';
  8447. $.jstree.plugins.wholerow = function (options, parent) {
  8448. this.bind = function () {
  8449. parent.bind.call(this);
  8450. this.element
  8451. .on('ready.jstree set_state.jstree', function () {
  8452. this.hide_dots();
  8453. }.bind(this))
  8454. .on("init.jstree loading.jstree ready.jstree", function () {
  8455. //div.style.height = this._data.core.li_height + 'px';
  8456. this.get_container_ul().addClass('jstree-wholerow-ul');
  8457. }.bind(this))
  8458. .on("deselect_all.jstree", function (e, data) {
  8459. this.element.find('.jstree-wholerow-clicked').removeClass('jstree-wholerow-clicked');
  8460. }.bind(this))
  8461. .on("changed.jstree", function (e, data) {
  8462. this.element.find('.jstree-wholerow-clicked').removeClass('jstree-wholerow-clicked');
  8463. var tmp = false, i, j;
  8464. for(i = 0, j = data.selected.length; i < j; i++) {
  8465. tmp = this.get_node(data.selected[i], true);
  8466. if(tmp && tmp.length) {
  8467. tmp.children('.jstree-wholerow').addClass('jstree-wholerow-clicked');
  8468. }
  8469. }
  8470. }.bind(this))
  8471. .on("open_node.jstree", function (e, data) {
  8472. this.get_node(data.node, true).find('.jstree-clicked').parent().children('.jstree-wholerow').addClass('jstree-wholerow-clicked');
  8473. }.bind(this))
  8474. .on("hover_node.jstree dehover_node.jstree", function (e, data) {
  8475. if(e.type === "hover_node" && this.is_disabled(data.node)) { return; }
  8476. this.get_node(data.node, true).children('.jstree-wholerow')[e.type === "hover_node"?"addClass":"removeClass"]('jstree-wholerow-hovered');
  8477. }.bind(this))
  8478. .on("contextmenu.jstree", ".jstree-wholerow", function (e) {
  8479. if (this._data.contextmenu) {
  8480. e.preventDefault();
  8481. var tmp = $.Event('contextmenu', { metaKey : e.metaKey, ctrlKey : e.ctrlKey, altKey : e.altKey, shiftKey : e.shiftKey, pageX : e.pageX, pageY : e.pageY });
  8482. $(e.currentTarget).closest(".jstree-node").children(".jstree-anchor").first().trigger(tmp);
  8483. }
  8484. }.bind(this))
  8485. /*!
  8486. .on("mousedown.jstree touchstart.jstree", ".jstree-wholerow", function (e) {
  8487. if(e.target === e.currentTarget) {
  8488. var a = $(e.currentTarget).closest(".jstree-node").children(".jstree-anchor");
  8489. e.target = a[0];
  8490. a.trigger(e);
  8491. }
  8492. })
  8493. */
  8494. .on("click.jstree", ".jstree-wholerow", function (e) {
  8495. e.stopImmediatePropagation();
  8496. var tmp = $.Event('click', { metaKey : e.metaKey, ctrlKey : e.ctrlKey, altKey : e.altKey, shiftKey : e.shiftKey });
  8497. $(e.currentTarget).closest(".jstree-node").children(".jstree-anchor").first().trigger(tmp).trigger('focus');
  8498. })
  8499. .on("dblclick.jstree", ".jstree-wholerow", function (e) {
  8500. e.stopImmediatePropagation();
  8501. var tmp = $.Event('dblclick', { metaKey : e.metaKey, ctrlKey : e.ctrlKey, altKey : e.altKey, shiftKey : e.shiftKey });
  8502. $(e.currentTarget).closest(".jstree-node").children(".jstree-anchor").first().trigger(tmp).trigger('focus');
  8503. })
  8504. .on("click.jstree", ".jstree-leaf > .jstree-ocl", function (e) {
  8505. e.stopImmediatePropagation();
  8506. var tmp = $.Event('click', { metaKey : e.metaKey, ctrlKey : e.ctrlKey, altKey : e.altKey, shiftKey : e.shiftKey });
  8507. $(e.currentTarget).closest(".jstree-node").children(".jstree-anchor").first().trigger(tmp).trigger('focus');
  8508. }.bind(this))
  8509. .on("mouseover.jstree", ".jstree-wholerow, .jstree-icon", function (e) {
  8510. e.stopImmediatePropagation();
  8511. if(!this.is_disabled(e.currentTarget)) {
  8512. this.hover_node(e.currentTarget);
  8513. }
  8514. return false;
  8515. }.bind(this))
  8516. .on("mouseleave.jstree", ".jstree-node", function (e) {
  8517. this.dehover_node(e.currentTarget);
  8518. }.bind(this));
  8519. };
  8520. this.teardown = function () {
  8521. if(this.settings.wholerow) {
  8522. this.element.find(".jstree-wholerow").remove();
  8523. }
  8524. parent.teardown.call(this);
  8525. };
  8526. this.redraw_node = function(obj, deep, callback, force_render) {
  8527. obj = parent.redraw_node.apply(this, arguments);
  8528. if(obj) {
  8529. var tmp = div.cloneNode(true);
  8530. //tmp.style.height = this._data.core.li_height + 'px';
  8531. if($.inArray(obj.id, this._data.core.selected) !== -1) { tmp.className += ' jstree-wholerow-clicked'; }
  8532. if(this._data.core.focused && this._data.core.focused === obj.id) { tmp.className += ' jstree-wholerow-hovered'; }
  8533. obj.insertBefore(tmp, obj.childNodes[0]);
  8534. }
  8535. return obj;
  8536. };
  8537. };
  8538. // include the wholerow plugin by default
  8539. // $.jstree.defaults.plugins.push("wholerow");
  8540. if(window.customElements && Object && Object.create) {
  8541. var proto = Object.create(HTMLElement.prototype);
  8542. proto.createdCallback = function () {
  8543. var c = { core : {}, plugins : [] }, i;
  8544. for(i in $.jstree.plugins) {
  8545. if($.jstree.plugins.hasOwnProperty(i) && this.attributes[i]) {
  8546. c.plugins.push(i);
  8547. if(this.getAttribute(i) && JSON.parse(this.getAttribute(i))) {
  8548. c[i] = JSON.parse(this.getAttribute(i));
  8549. }
  8550. }
  8551. }
  8552. for(i in $.jstree.defaults.core) {
  8553. if($.jstree.defaults.core.hasOwnProperty(i) && this.attributes[i]) {
  8554. c.core[i] = JSON.parse(this.getAttribute(i)) || this.getAttribute(i);
  8555. }
  8556. }
  8557. $(this).jstree(c);
  8558. };
  8559. // proto.attributeChangedCallback = function (name, previous, value) { };
  8560. try {
  8561. window.customElements.define("vakata-jstree", function() {}, { prototype: proto });
  8562. } catch (ignore) { }
  8563. }
  8564. }));