jstree.js 271 KB

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