jstree.js 291 KB

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