jstree.js 165 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026
  1. /*!
  2. * jsTree {{VERSION}}
  3. * http://jstree.com/
  4. *
  5. * Copyright (c) 2014 Ivan Bozhanov (http://vakata.com)
  6. *
  7. * Licensed same as jquery - under the terms of the MIT License
  8. * http://www.opensource.org/licenses/mit-license.php
  9. */
  10. /*!
  11. * if using jslint please allow for the jQuery global and use following options:
  12. * jslint: loopfunc: true, browser: true, ass: true, bitwise: true, continue: true, nomen: true, plusplus: true, regexp: true, unparam: true, todo: true, white: true
  13. */
  14. /*jshint -W083 */
  15. /*globals jQuery, define, module, exports, require, window, document, postMessage */
  16. (function (factory) {
  17. "use strict";
  18. if (typeof define === 'function' && define.amd) {
  19. define(['jquery'], factory);
  20. }
  21. else if(typeof module !== 'undefined' && module.exports) {
  22. module.exports = factory(require('jquery'));
  23. }
  24. else {
  25. factory(jQuery);
  26. }
  27. }(function ($, undefined) {
  28. "use strict";
  29. // prevent another load? maybe there is a better way?
  30. if($.jstree) {
  31. return;
  32. }
  33. /**
  34. * ### jsTree core functionality
  35. */
  36. // internal variables
  37. var instance_counter = 0,
  38. ccp_node = false,
  39. ccp_mode = false,
  40. ccp_inst = false,
  41. themes_loaded = [],
  42. src = $('script:last').attr('src'),
  43. document = window.document; // local variable is always faster to access then a global
  44. var setImmediate = window.setImmediate;
  45. var Promise = window.Promise;
  46. if (!setImmediate && Promise) {
  47. // Good enough approximation of setImmediate
  48. setImmediate = function (cb, arg) {
  49. Promise.resolve(arg).then(cb);
  50. };
  51. }
  52. /**
  53. * holds all jstree related functions and variables, including the actual class and methods to create, access and manipulate instances.
  54. * @name $.jstree
  55. */
  56. $.jstree = {
  57. /**
  58. * specifies the jstree version in use
  59. * @name $.jstree.version
  60. */
  61. version : '{{VERSION}}',
  62. /**
  63. * holds all the default options used when creating new instances
  64. * @name $.jstree.defaults
  65. */
  66. defaults : {
  67. /**
  68. * configure which plugins will be active on an instance. Should be an array of strings, where each element is a plugin name. The default is `[]`
  69. * @name $.jstree.defaults.plugins
  70. */
  71. plugins : []
  72. },
  73. /**
  74. * stores all loaded jstree plugins (used internally)
  75. * @name $.jstree.plugins
  76. */
  77. plugins : {},
  78. path : src && src.indexOf('/') !== -1 ? src.replace(/\/[^\/]+$/,'') : '',
  79. idregex : /[\\:&!^|()\[\]<>@*'+~#";.,=\- \/${}%?`]/g,
  80. root : '#'
  81. };
  82. /**
  83. * creates a jstree instance
  84. * @name $.jstree.create(el [, options])
  85. * @param {DOMElement|jQuery|String} el the element to create the instance on, can be jQuery extended or a selector
  86. * @param {Object} options options for this instance (extends `$.jstree.defaults`)
  87. * @return {jsTree} the new instance
  88. */
  89. $.jstree.create = function (el, options) {
  90. var tmp = new $.jstree.core(++instance_counter),
  91. opt = options;
  92. options = $.extend(true, {}, $.jstree.defaults, options);
  93. if(opt && opt.plugins) {
  94. options.plugins = opt.plugins;
  95. }
  96. $.each(options.plugins, function (i, k) {
  97. if(i !== 'core') {
  98. tmp = tmp.plugin(k, options[k]);
  99. }
  100. });
  101. $(el).data('jstree', tmp);
  102. tmp.init(el, options);
  103. return tmp;
  104. };
  105. /**
  106. * remove all traces of jstree from the DOM and destroy all instances
  107. * @name $.jstree.destroy()
  108. */
  109. $.jstree.destroy = function () {
  110. $('.jstree:jstree').jstree('destroy');
  111. $(document).off('.jstree');
  112. };
  113. /**
  114. * the jstree class constructor, used only internally
  115. * @private
  116. * @name $.jstree.core(id)
  117. * @param {Number} id this instance's index
  118. */
  119. $.jstree.core = function (id) {
  120. this._id = id;
  121. this._cnt = 0;
  122. this._wrk = null;
  123. this._data = {
  124. core : {
  125. themes : {
  126. name : false,
  127. dots : false,
  128. icons : false,
  129. ellipsis : false
  130. },
  131. selected : [],
  132. last_error : {},
  133. working : false,
  134. worker_queue : [],
  135. focused : null
  136. }
  137. };
  138. };
  139. /**
  140. * get a reference to an existing instance
  141. *
  142. * __Examples__
  143. *
  144. * // provided a container with an ID of "tree", and a nested node with an ID of "branch"
  145. * // all of there will return the same instance
  146. * $.jstree.reference('tree');
  147. * $.jstree.reference('#tree');
  148. * $.jstree.reference($('#tree'));
  149. * $.jstree.reference(document.getElementByID('tree'));
  150. * $.jstree.reference('branch');
  151. * $.jstree.reference('#branch');
  152. * $.jstree.reference($('#branch'));
  153. * $.jstree.reference(document.getElementByID('branch'));
  154. *
  155. * @name $.jstree.reference(needle)
  156. * @param {DOMElement|jQuery|String} needle
  157. * @return {jsTree|null} the instance or `null` if not found
  158. */
  159. $.jstree.reference = function (needle) {
  160. var tmp = null,
  161. obj = null;
  162. if(needle && needle.id && (!needle.tagName || !needle.nodeType)) { needle = needle.id; }
  163. if(!obj || !obj.length) {
  164. try { obj = $(needle); } catch (ignore) { }
  165. }
  166. if(!obj || !obj.length) {
  167. try { obj = $('#' + needle.replace($.jstree.idregex,'\\$&')); } catch (ignore) { }
  168. }
  169. if(obj && obj.length && (obj = obj.closest('.jstree')).length && (obj = obj.data('jstree'))) {
  170. tmp = obj;
  171. }
  172. else {
  173. $('.jstree').each(function () {
  174. var inst = $(this).data('jstree');
  175. if(inst && inst._model.data[needle]) {
  176. tmp = inst;
  177. return false;
  178. }
  179. });
  180. }
  181. return tmp;
  182. };
  183. /**
  184. * Create an instance, get an instance or invoke a command on a instance.
  185. *
  186. * If there is no instance associated with the current node a new one is created and `arg` is used to extend `$.jstree.defaults` for this new instance. There would be no return value (chaining is not broken).
  187. *
  188. * If there is an existing instance and `arg` is a string the command specified by `arg` is executed on the instance, with any additional arguments passed to the function. If the function returns a value it will be returned (chaining could break depending on function).
  189. *
  190. * If there is an existing instance and `arg` is not a string the instance itself is returned (similar to `$.jstree.reference`).
  191. *
  192. * In any other case - nothing is returned and chaining is not broken.
  193. *
  194. * __Examples__
  195. *
  196. * $('#tree1').jstree(); // creates an instance
  197. * $('#tree2').jstree({ plugins : [] }); // create an instance with some options
  198. * $('#tree1').jstree('open_node', '#branch_1'); // call a method on an existing instance, passing additional arguments
  199. * $('#tree2').jstree(); // get an existing instance (or create an instance)
  200. * $('#tree2').jstree(true); // get an existing instance (will not create new instance)
  201. * $('#branch_1').jstree().select_node('#branch_1'); // get an instance (using a nested element and call a method)
  202. *
  203. * @name $().jstree([arg])
  204. * @param {String|Object} arg
  205. * @return {Mixed}
  206. */
  207. $.fn.jstree = function (arg) {
  208. // check for string argument
  209. var is_method = (typeof arg === 'string'),
  210. args = Array.prototype.slice.call(arguments, 1),
  211. result = null;
  212. if(arg === true && !this.length) { return false; }
  213. this.each(function () {
  214. // get the instance (if there is one) and method (if it exists)
  215. var instance = $.jstree.reference(this),
  216. method = is_method && instance ? instance[arg] : null;
  217. // if calling a method, and method is available - execute on the instance
  218. result = is_method && method ?
  219. method.apply(instance, args) :
  220. null;
  221. // if there is no instance and no method is being called - create one
  222. if(!instance && !is_method && (arg === undefined || $.isPlainObject(arg))) {
  223. $.jstree.create(this, arg);
  224. }
  225. // if there is an instance and no method is called - return the instance
  226. if( (instance && !is_method) || arg === true ) {
  227. result = instance || false;
  228. }
  229. // if there was a method call which returned a result - break and return the value
  230. if(result !== null && result !== undefined) {
  231. return false;
  232. }
  233. });
  234. // if there was a method call with a valid return value - return that, otherwise continue the chain
  235. return result !== null && result !== undefined ?
  236. result : this;
  237. };
  238. /**
  239. * used to find elements containing an instance
  240. *
  241. * __Examples__
  242. *
  243. * $('div:jstree').each(function () {
  244. * $(this).jstree('destroy');
  245. * });
  246. *
  247. * @name $(':jstree')
  248. * @return {jQuery}
  249. */
  250. $.expr.pseudos.jstree = $.expr.createPseudo(function(search) {
  251. return function(a) {
  252. return $(a).hasClass('jstree') &&
  253. $(a).data('jstree') !== undefined;
  254. };
  255. });
  256. /**
  257. * stores all defaults for the core
  258. * @name $.jstree.defaults.core
  259. */
  260. $.jstree.defaults.core = {
  261. /**
  262. * data configuration
  263. *
  264. * If left as `false` the HTML inside the jstree container element is used to populate the tree (that should be an unordered list with list items).
  265. *
  266. * You can also pass in a HTML string or a JSON array here.
  267. *
  268. * It is possible to pass in a standard jQuery-like AJAX config and jstree will automatically determine if the response is JSON or HTML and use that to populate the tree.
  269. * In addition to the standard jQuery ajax options here you can supply functions for `data` and `url`, the functions will be run in the current instance's scope and a param will be passed indicating which node is being loaded, the return value of those functions will be used.
  270. *
  271. * The last option is to specify a function, that function will receive the node being loaded as argument and a second param which is a function which should be called with the result.
  272. *
  273. * __Examples__
  274. *
  275. * // AJAX
  276. * $('#tree').jstree({
  277. * 'core' : {
  278. * 'data' : {
  279. * 'url' : '/get/children/',
  280. * 'data' : function (node) {
  281. * return { 'id' : node.id };
  282. * }
  283. * }
  284. * });
  285. *
  286. * // direct data
  287. * $('#tree').jstree({
  288. * 'core' : {
  289. * 'data' : [
  290. * 'Simple root node',
  291. * {
  292. * 'id' : 'node_2',
  293. * 'text' : 'Root node with options',
  294. * 'state' : { 'opened' : true, 'selected' : true },
  295. * 'children' : [ { 'text' : 'Child 1' }, 'Child 2']
  296. * }
  297. * ]
  298. * }
  299. * });
  300. *
  301. * // function
  302. * $('#tree').jstree({
  303. * 'core' : {
  304. * 'data' : function (obj, callback) {
  305. * callback.call(this, ['Root 1', 'Root 2']);
  306. * }
  307. * });
  308. *
  309. * @name $.jstree.defaults.core.data
  310. */
  311. data : false,
  312. /**
  313. * configure the various strings used throughout the tree
  314. *
  315. * You can use an object where the key is the string you need to replace and the value is your replacement.
  316. * Another option is to specify a function which will be called with an argument of the needed string and should return the replacement.
  317. * If left as `false` no replacement is made.
  318. *
  319. * __Examples__
  320. *
  321. * $('#tree').jstree({
  322. * 'core' : {
  323. * 'strings' : {
  324. * 'Loading ...' : 'Please wait ...'
  325. * }
  326. * }
  327. * });
  328. *
  329. * @name $.jstree.defaults.core.strings
  330. */
  331. strings : false,
  332. /**
  333. * determines what happens when a user tries to modify the structure of the tree
  334. * If left as `false` all operations like create, rename, delete, move or copy are prevented.
  335. * You can set this to `true` to allow all interactions or use a function to have better control.
  336. *
  337. * __Examples__
  338. *
  339. * $('#tree').jstree({
  340. * 'core' : {
  341. * 'check_callback' : function (operation, node, node_parent, node_position, more) {
  342. * // operation can be 'create_node', 'rename_node', 'delete_node', 'move_node', 'copy_node' or 'edit'
  343. * // in case of 'rename_node' node_position is filled with the new node name
  344. * return operation === 'rename_node' ? true : false;
  345. * }
  346. * }
  347. * });
  348. *
  349. * @name $.jstree.defaults.core.check_callback
  350. */
  351. check_callback : false,
  352. /**
  353. * a callback called with a single object parameter in the instance's scope when something goes wrong (operation prevented, ajax failed, etc)
  354. * @name $.jstree.defaults.core.error
  355. */
  356. error : $.noop,
  357. /**
  358. * the open / close animation duration in milliseconds - set this to `false` to disable the animation (default is `200`)
  359. * @name $.jstree.defaults.core.animation
  360. */
  361. animation : 200,
  362. /**
  363. * a boolean indicating if multiple nodes can be selected
  364. * @name $.jstree.defaults.core.multiple
  365. */
  366. multiple : true,
  367. /**
  368. * theme configuration object
  369. * @name $.jstree.defaults.core.themes
  370. */
  371. themes : {
  372. /**
  373. * the name of the theme to use (if left as `false` the default theme is used)
  374. * @name $.jstree.defaults.core.themes.name
  375. */
  376. name : false,
  377. /**
  378. * the URL of the theme's CSS file, leave this as `false` if you have manually included the theme CSS (recommended). You can set this to `true` too which will try to autoload the theme.
  379. * @name $.jstree.defaults.core.themes.url
  380. */
  381. url : false,
  382. /**
  383. * the location of all jstree themes - only used if `url` is set to `true`
  384. * @name $.jstree.defaults.core.themes.dir
  385. */
  386. dir : false,
  387. /**
  388. * a boolean indicating if connecting dots are shown
  389. * @name $.jstree.defaults.core.themes.dots
  390. */
  391. dots : true,
  392. /**
  393. * a boolean indicating if node icons are shown
  394. * @name $.jstree.defaults.core.themes.icons
  395. */
  396. icons : true,
  397. /**
  398. * a boolean indicating if node ellipsis should be shown - this only works with a fixed with on the container
  399. * @name $.jstree.defaults.core.themes.ellipsis
  400. */
  401. ellipsis : false,
  402. /**
  403. * a boolean indicating if the tree background is striped
  404. * @name $.jstree.defaults.core.themes.stripes
  405. */
  406. stripes : false,
  407. /**
  408. * a string (or boolean `false`) specifying the theme variant to use (if the theme supports variants)
  409. * @name $.jstree.defaults.core.themes.variant
  410. */
  411. variant : false,
  412. /**
  413. * a boolean specifying if a reponsive version of the theme should kick in on smaller screens (if the theme supports it). Defaults to `false`.
  414. * @name $.jstree.defaults.core.themes.responsive
  415. */
  416. responsive : false
  417. },
  418. /**
  419. * if left as `true` all parents of all selected nodes will be opened once the tree loads (so that all selected nodes are visible to the user)
  420. * @name $.jstree.defaults.core.expand_selected_onload
  421. */
  422. expand_selected_onload : true,
  423. /**
  424. * if left as `true` web workers will be used to parse incoming JSON data where possible, so that the UI will not be blocked by large requests. Workers are however about 30% slower. Defaults to `true`
  425. * @name $.jstree.defaults.core.worker
  426. */
  427. worker : true,
  428. /**
  429. * Force node text to plain text (and escape HTML). Defaults to `false`
  430. * @name $.jstree.defaults.core.force_text
  431. */
  432. force_text : false,
  433. /**
  434. * Should the node be toggled if the text is double clicked. Defaults to `true`
  435. * @name $.jstree.defaults.core.dblclick_toggle
  436. */
  437. dblclick_toggle : true,
  438. /**
  439. * Should the loaded nodes be part of the state. Defaults to `false`
  440. * @name $.jstree.defaults.core.loaded_state
  441. */
  442. loaded_state : false,
  443. /**
  444. * Should the last active node be focused when the tree container is blurred and the focused again. This helps working with screen readers. Defaults to `true`
  445. * @name $.jstree.defaults.core.restore_focus
  446. */
  447. restore_focus : true,
  448. /**
  449. * Force to compute and set "aria-setsize" and "aria-posinset" explicitly for each treeitem.
  450. * Some browsers may compute incorrect elements position and produce wrong announcements for screen readers. Defaults to `false`
  451. * @name $.jstree.defaults.core.compute_elements_positions
  452. */
  453. compute_elements_positions : false,
  454. /**
  455. * Default keyboard shortcuts (an object where each key is the button name or combo - like 'enter', 'ctrl-space', 'p', etc and the value is the function to execute in the instance's scope)
  456. * @name $.jstree.defaults.core.keyboard
  457. */
  458. keyboard : {
  459. 'ctrl-space': function (e) {
  460. // aria defines space only with Ctrl
  461. e.type = "click";
  462. $(e.currentTarget).trigger(e);
  463. },
  464. 'enter': function (e) {
  465. // enter
  466. e.type = "click";
  467. $(e.currentTarget).trigger(e);
  468. },
  469. 'left': function (e) {
  470. // left
  471. e.preventDefault();
  472. if(this.is_open(e.currentTarget)) {
  473. this.close_node(e.currentTarget);
  474. }
  475. else {
  476. var o = this.get_parent(e.currentTarget);
  477. if(o && o.id !== $.jstree.root) { this.get_node(o, true).children('.jstree-anchor').trigger('focus'); }
  478. }
  479. },
  480. 'up': function (e) {
  481. // up
  482. e.preventDefault();
  483. var o = this.get_prev_dom(e.currentTarget);
  484. if(o && o.length) { o.children('.jstree-anchor').trigger('focus'); }
  485. },
  486. 'right': function (e) {
  487. // right
  488. e.preventDefault();
  489. if(this.is_closed(e.currentTarget)) {
  490. this.open_node(e.currentTarget, function (o) { this.get_node(o, true).children('.jstree-anchor').trigger('focus'); });
  491. }
  492. else if (this.is_open(e.currentTarget)) {
  493. var o = this.get_node(e.currentTarget, true).children('.jstree-children')[0];
  494. if(o) { $(this._firstChild(o)).children('.jstree-anchor').trigger('focus'); }
  495. }
  496. },
  497. 'down': function (e) {
  498. // down
  499. e.preventDefault();
  500. var o = this.get_next_dom(e.currentTarget);
  501. if(o && o.length) { o.children('.jstree-anchor').trigger('focus'); }
  502. },
  503. '*': function (e) {
  504. // aria defines * on numpad as open_all - not very common
  505. this.open_all();
  506. },
  507. 'home': function (e) {
  508. // home
  509. e.preventDefault();
  510. var o = this._firstChild(this.get_container_ul()[0]);
  511. if(o) { $(o).children('.jstree-anchor').filter(':visible').trigger('focus'); }
  512. },
  513. 'end': function (e) {
  514. // end
  515. e.preventDefault();
  516. this.element.find('.jstree-anchor').filter(':visible').last().trigger('focus');
  517. },
  518. 'f2': function (e) {
  519. // f2 - safe to include - if check_callback is false it will fail
  520. e.preventDefault();
  521. this.edit(e.currentTarget);
  522. }
  523. },
  524. /**
  525. * Should reselecting an already selected node trigger the select and changed callbacks
  526. * @name $.jstree.defaults.core.allow_reselect
  527. */
  528. allow_reselect : false
  529. };
  530. $.jstree.core.prototype = {
  531. /**
  532. * used to decorate an instance with a plugin. Used internally.
  533. * @private
  534. * @name plugin(deco [, opts])
  535. * @param {String} deco the plugin to decorate with
  536. * @param {Object} opts options for the plugin
  537. * @return {jsTree}
  538. */
  539. plugin : function (deco, opts) {
  540. var Child = $.jstree.plugins[deco];
  541. if(Child) {
  542. this._data[deco] = {};
  543. Child.prototype = this;
  544. return new Child(opts, this);
  545. }
  546. return this;
  547. },
  548. /**
  549. * initialize the instance. Used internally.
  550. * @private
  551. * @name init(el, optons)
  552. * @param {DOMElement|jQuery|String} el the element we are transforming
  553. * @param {Object} options options for this instance
  554. * @trigger init.jstree, loading.jstree, loaded.jstree, ready.jstree, changed.jstree
  555. */
  556. init : function (el, options) {
  557. this._model = {
  558. data : {},
  559. changed : [],
  560. force_full_redraw : false,
  561. redraw_timeout : false,
  562. default_state : {
  563. loaded : true,
  564. opened : false,
  565. selected : false,
  566. disabled : false
  567. }
  568. };
  569. this._model.data[$.jstree.root] = {
  570. id : $.jstree.root,
  571. parent : null,
  572. parents : [],
  573. children : [],
  574. children_d : [],
  575. state : { loaded : false }
  576. };
  577. this.element = $(el).addClass('jstree jstree-' + this._id);
  578. this.settings = options;
  579. this._data.core.ready = false;
  580. this._data.core.loaded = false;
  581. this._data.core.rtl = (this.element.css("direction") === "rtl");
  582. this.element[this._data.core.rtl ? 'addClass' : 'removeClass']("jstree-rtl");
  583. this.element.attr('role','tree');
  584. if(this.settings.core.multiple) {
  585. this.element.attr('aria-multiselectable', true);
  586. }
  587. if(!this.element.attr('tabindex')) {
  588. this.element.attr('tabindex','0');
  589. }
  590. this.bind();
  591. /**
  592. * triggered after all events are bound
  593. * @event
  594. * @name init.jstree
  595. */
  596. this.trigger("init");
  597. this._data.core.original_container_html = this.element.find(" > ul > li").clone(true);
  598. this._data.core.original_container_html
  599. .find("li").addBack()
  600. .contents().filter(function() {
  601. return this.nodeType === 3 && (!this.nodeValue || /^\s+$/.test(this.nodeValue));
  602. })
  603. .remove();
  604. this.element.html("<"+"ul class='jstree-container-ul jstree-children' role='group'><"+"li id='j"+this._id+"_loading' class='jstree-initial-node jstree-loading jstree-leaf jstree-last' role='none'><i class='jstree-icon jstree-ocl'></i><"+"a class='jstree-anchor' role='treeitem' href='#'><i class='jstree-icon jstree-themeicon-hidden'></i>" + this.get_string("Loading ...") + "</a></li></ul>");
  605. this.element.attr('aria-activedescendant','j' + this._id + '_loading');
  606. this._data.core.li_height = this.get_container_ul().children("li").first().outerHeight() || 24;
  607. this._data.core.node = this._create_prototype_node();
  608. /**
  609. * triggered after the loading text is shown and before loading starts
  610. * @event
  611. * @name loading.jstree
  612. */
  613. this.trigger("loading");
  614. this.load_node($.jstree.root);
  615. },
  616. /**
  617. * destroy an instance
  618. * @name destroy()
  619. * @param {Boolean} keep_html if not set to `true` the container will be emptied, otherwise the current DOM elements will be kept intact
  620. */
  621. destroy : function (keep_html) {
  622. /**
  623. * triggered before the tree is destroyed
  624. * @event
  625. * @name destroy.jstree
  626. */
  627. this.trigger("destroy");
  628. if(this._wrk) {
  629. try {
  630. window.URL.revokeObjectURL(this._wrk);
  631. this._wrk = null;
  632. }
  633. catch (ignore) { }
  634. }
  635. if(!keep_html) { this.element.empty(); }
  636. this.teardown();
  637. },
  638. /**
  639. * Create a prototype node
  640. * @name _create_prototype_node()
  641. * @return {DOMElement}
  642. */
  643. _create_prototype_node : function () {
  644. var _node = document.createElement('LI'), _temp1, _temp2;
  645. _node.setAttribute('role', 'none');
  646. _temp1 = document.createElement('I');
  647. _temp1.className = 'jstree-icon jstree-ocl';
  648. _temp1.setAttribute('role', 'presentation');
  649. _node.appendChild(_temp1);
  650. _temp1 = document.createElement('A');
  651. _temp1.className = 'jstree-anchor';
  652. _temp1.setAttribute('href','#');
  653. _temp1.setAttribute('tabindex','-1');
  654. _temp1.setAttribute('role', 'treeitem');
  655. _temp2 = document.createElement('I');
  656. _temp2.className = 'jstree-icon jstree-themeicon';
  657. _temp2.setAttribute('role', 'presentation');
  658. _temp1.appendChild(_temp2);
  659. _node.appendChild(_temp1);
  660. _temp1 = _temp2 = null;
  661. return _node;
  662. },
  663. _kbevent_to_func : function (e) {
  664. var keys = {
  665. 8: "Backspace", 9: "Tab", 13: "Enter", 19: "Pause", 27: "Esc",
  666. 32: "Space", 33: "PageUp", 34: "PageDown", 35: "End", 36: "Home",
  667. 37: "Left", 38: "Up", 39: "Right", 40: "Down", 44: "Print", 45: "Insert",
  668. 46: "Delete", 96: "Numpad0", 97: "Numpad1", 98: "Numpad2", 99 : "Numpad3",
  669. 100: "Numpad4", 101: "Numpad5", 102: "Numpad6", 103: "Numpad7",
  670. 104: "Numpad8", 105: "Numpad9", '-13': "NumpadEnter", 112: "F1",
  671. 113: "F2", 114: "F3", 115: "F4", 116: "F5", 117: "F6", 118: "F7",
  672. 119: "F8", 120: "F9", 121: "F10", 122: "F11", 123: "F12", 144: "Numlock",
  673. 145: "Scrolllock", 16: 'Shift', 17: 'Ctrl', 18: 'Alt',
  674. 48: '0', 49: '1', 50: '2', 51: '3', 52: '4', 53: '5',
  675. 54: '6', 55: '7', 56: '8', 57: '9', 59: ';', 61: '=', 65: 'a',
  676. 66: 'b', 67: 'c', 68: 'd', 69: 'e', 70: 'f', 71: 'g', 72: 'h',
  677. 73: 'i', 74: 'j', 75: 'k', 76: 'l', 77: 'm', 78: 'n', 79: 'o',
  678. 80: 'p', 81: 'q', 82: 'r', 83: 's', 84: 't', 85: 'u', 86: 'v',
  679. 87: 'w', 88: 'x', 89: 'y', 90: 'z', 107: '+', 109: '-', 110: '.',
  680. 186: ';', 187: '=', 188: ',', 189: '-', 190: '.', 191: '/', 192: '`',
  681. 219: '[', 220: '\\',221: ']', 222: "'", 111: '/', 106: '*', 173: '-'
  682. };
  683. var parts = [];
  684. if (e.ctrlKey) { parts.push('ctrl'); }
  685. if (e.altKey) { parts.push('alt'); }
  686. if (e.shiftKey) { parts.push('shift'); }
  687. parts.push(keys[e.which] ? keys[e.which].toLowerCase() : e.which);
  688. parts = parts.sort().join('-').toLowerCase();
  689. if (parts === 'shift-shift' || parts === 'ctrl-ctrl' || parts === 'alt-alt') {
  690. return null;
  691. }
  692. var kb = this.settings.core.keyboard, i, tmp;
  693. for (i in kb) {
  694. if (kb.hasOwnProperty(i)) {
  695. tmp = i;
  696. if (tmp !== '-' && tmp !== '+') {
  697. tmp = tmp.replace('--', '-MINUS').replace('+-', '-MINUS').replace('++', '-PLUS').replace('-+', '-PLUS');
  698. tmp = tmp.split(/-|\+/).sort().join('-').replace('MINUS', '-').replace('PLUS', '+').toLowerCase();
  699. }
  700. if (tmp === parts) {
  701. return kb[i];
  702. }
  703. }
  704. }
  705. return null;
  706. },
  707. /**
  708. * part of the destroying of an instance. Used internally.
  709. * @private
  710. * @name teardown()
  711. */
  712. teardown : function () {
  713. this.unbind();
  714. this.element
  715. .removeClass('jstree')
  716. .removeData('jstree')
  717. .find("[class^='jstree']")
  718. .addBack()
  719. .attr("class", function () { return this.className.replace(/jstree[^ ]*|$/ig,''); });
  720. this.element = null;
  721. },
  722. /**
  723. * bind all events. Used internally.
  724. * @private
  725. * @name bind()
  726. */
  727. bind : function () {
  728. var word = '',
  729. tout = null,
  730. was_click = 0;
  731. this.element
  732. .on("dblclick.jstree", function (e) {
  733. if(e.target.tagName && e.target.tagName.toLowerCase() === "input") { return true; }
  734. if(document.selection && document.selection.empty) {
  735. document.selection.empty();
  736. }
  737. else {
  738. if(window.getSelection) {
  739. var sel = window.getSelection();
  740. try {
  741. sel.removeAllRanges();
  742. sel.collapse();
  743. } catch (ignore) { }
  744. }
  745. }
  746. })
  747. .on("mousedown.jstree", function (e) {
  748. if(e.target === this.element[0]) {
  749. e.preventDefault(); // prevent losing focus when clicking scroll arrows (FF, Chrome)
  750. was_click = +(new Date()); // ie does not allow to prevent losing focus
  751. }
  752. }.bind(this))
  753. .on("mousedown.jstree", ".jstree-ocl", function (e) {
  754. e.preventDefault(); // prevent any node inside from losing focus when clicking the open/close icon
  755. })
  756. .on("click.jstree", ".jstree-ocl", function (e) {
  757. this.toggle_node(e.target);
  758. }.bind(this))
  759. .on("dblclick.jstree", ".jstree-anchor", function (e) {
  760. if(e.target.tagName && e.target.tagName.toLowerCase() === "input") { return true; }
  761. if(this.settings.core.dblclick_toggle) {
  762. this.toggle_node(e.target);
  763. }
  764. }.bind(this))
  765. .on("click.jstree", ".jstree-anchor", function (e) {
  766. e.preventDefault();
  767. if(e.currentTarget !== document.activeElement) { $(e.currentTarget).trigger('focus'); }
  768. this.activate_node(e.currentTarget, e);
  769. }.bind(this))
  770. .on('keydown.jstree', '.jstree-anchor', function (e) {
  771. if(e.target.tagName && e.target.tagName.toLowerCase() === "input") { return true; }
  772. if(this._data.core.rtl) {
  773. if(e.which === 37) { e.which = 39; }
  774. else if(e.which === 39) { e.which = 37; }
  775. }
  776. var f = this._kbevent_to_func(e);
  777. if (f) {
  778. var r = f.call(this, e);
  779. if (r === false || r === true) {
  780. return r;
  781. }
  782. }
  783. }.bind(this))
  784. .on("load_node.jstree", function (e, data) {
  785. if(data.status) {
  786. if(data.node.id === $.jstree.root && !this._data.core.loaded) {
  787. this._data.core.loaded = true;
  788. if(this._firstChild(this.get_container_ul()[0])) {
  789. this.element.attr('aria-activedescendant',this._firstChild(this.get_container_ul()[0]).id);
  790. }
  791. /**
  792. * triggered after the root node is loaded for the first time
  793. * @event
  794. * @name loaded.jstree
  795. */
  796. this.trigger("loaded");
  797. }
  798. if(!this._data.core.ready) {
  799. setTimeout(function() {
  800. if(this.element && !this.get_container_ul().find('.jstree-loading').length) {
  801. this._data.core.ready = true;
  802. if(this._data.core.selected.length) {
  803. if(this.settings.core.expand_selected_onload) {
  804. var tmp = [], i, j;
  805. for(i = 0, j = this._data.core.selected.length; i < j; i++) {
  806. tmp = tmp.concat(this._model.data[this._data.core.selected[i]].parents);
  807. }
  808. tmp = $.vakata.array_unique(tmp);
  809. for(i = 0, j = tmp.length; i < j; i++) {
  810. this.open_node(tmp[i], false, 0);
  811. }
  812. }
  813. this.trigger('changed', { 'action' : 'ready', 'selected' : this._data.core.selected });
  814. }
  815. /**
  816. * triggered after all nodes are finished loading
  817. * @event
  818. * @name ready.jstree
  819. */
  820. this.trigger("ready");
  821. }
  822. }.bind(this), 0);
  823. }
  824. }
  825. }.bind(this))
  826. // quick searching when the tree is focused
  827. .on('keypress.jstree', function (e) {
  828. if(e.target.tagName && e.target.tagName.toLowerCase() === "input") { return true; }
  829. if(tout) { clearTimeout(tout); }
  830. tout = setTimeout(function () {
  831. word = '';
  832. }, 500);
  833. var chr = String.fromCharCode(e.which).toLowerCase(),
  834. col = this.element.find('.jstree-anchor').filter(':visible'),
  835. ind = col.index(document.activeElement) || 0,
  836. end = false;
  837. word += chr;
  838. // match for whole word from current node down (including the current node)
  839. if(word.length > 1) {
  840. col.slice(ind).each(function (i, v) {
  841. if($(v).text().toLowerCase().indexOf(word) === 0) {
  842. $(v).trigger('focus');
  843. end = true;
  844. return false;
  845. }
  846. }.bind(this));
  847. if(end) { return; }
  848. // match for whole word from the beginning of the tree
  849. col.slice(0, ind).each(function (i, v) {
  850. if($(v).text().toLowerCase().indexOf(word) === 0) {
  851. $(v).trigger('focus');
  852. end = true;
  853. return false;
  854. }
  855. }.bind(this));
  856. if(end) { return; }
  857. }
  858. // list nodes that start with that letter (only if word consists of a single char)
  859. if(new RegExp('^' + chr.replace(/[-\/\\^$*+?.()|[\]{}]/g, '\\$&') + '+$').test(word)) {
  860. // search for the next node starting with that letter
  861. col.slice(ind + 1).each(function (i, v) {
  862. if($(v).text().toLowerCase().charAt(0) === chr) {
  863. $(v).trigger('focus');
  864. end = true;
  865. return false;
  866. }
  867. }.bind(this));
  868. if(end) { return; }
  869. // search from the beginning
  870. col.slice(0, ind + 1).each(function (i, v) {
  871. if($(v).text().toLowerCase().charAt(0) === chr) {
  872. $(v).trigger('focus');
  873. end = true;
  874. return false;
  875. }
  876. }.bind(this));
  877. if(end) { return; }
  878. }
  879. }.bind(this))
  880. // THEME RELATED
  881. .on("init.jstree", function () {
  882. var s = this.settings.core.themes;
  883. this._data.core.themes.dots = s.dots;
  884. this._data.core.themes.stripes = s.stripes;
  885. this._data.core.themes.icons = s.icons;
  886. this._data.core.themes.ellipsis = s.ellipsis;
  887. this.set_theme(s.name || "default", s.url);
  888. this.set_theme_variant(s.variant);
  889. }.bind(this))
  890. .on("loading.jstree", function () {
  891. this[ this._data.core.themes.dots ? "show_dots" : "hide_dots" ]();
  892. this[ this._data.core.themes.icons ? "show_icons" : "hide_icons" ]();
  893. this[ this._data.core.themes.stripes ? "show_stripes" : "hide_stripes" ]();
  894. this[ this._data.core.themes.ellipsis ? "show_ellipsis" : "hide_ellipsis" ]();
  895. }.bind(this))
  896. .on('blur.jstree', '.jstree-anchor', function (e) {
  897. this._data.core.focused = null;
  898. $(e.currentTarget).filter('.jstree-hovered').trigger('mouseleave');
  899. this.element.attr('tabindex', '0');
  900. $(e.currentTarget).attr('tabindex', '-1');
  901. }.bind(this))
  902. .on('focus.jstree', '.jstree-anchor', function (e) {
  903. var tmp = this.get_node(e.currentTarget);
  904. if(tmp && (tmp.id || tmp.id === 0)) {
  905. this._data.core.focused = tmp.id;
  906. }
  907. this.element.find('.jstree-hovered').not(e.currentTarget).trigger('mouseleave');
  908. $(e.currentTarget).trigger('mouseenter');
  909. this.element.attr('tabindex', '-1');
  910. $(e.currentTarget).attr('tabindex', '0');
  911. }.bind(this))
  912. .on('focus.jstree', function () {
  913. if(+(new Date()) - was_click > 500 && !this._data.core.focused && this.settings.core.restore_focus) {
  914. was_click = 0;
  915. var act = this.get_node(this.element.attr('aria-activedescendant'), true);
  916. if(act) {
  917. act.find('> .jstree-anchor').trigger('focus');
  918. }
  919. }
  920. }.bind(this))
  921. .on('mouseenter.jstree', '.jstree-anchor', function (e) {
  922. this.hover_node(e.currentTarget);
  923. }.bind(this))
  924. .on('mouseleave.jstree', '.jstree-anchor', function (e) {
  925. this.dehover_node(e.currentTarget);
  926. }.bind(this));
  927. },
  928. /**
  929. * part of the destroying of an instance. Used internally.
  930. * @private
  931. * @name unbind()
  932. */
  933. unbind : function () {
  934. this.element.off('.jstree');
  935. $(document).off('.jstree-' + this._id);
  936. },
  937. /**
  938. * trigger an event. Used internally.
  939. * @private
  940. * @name trigger(ev [, data])
  941. * @param {String} ev the name of the event to trigger
  942. * @param {Object} data additional data to pass with the event
  943. */
  944. trigger : function (ev, data) {
  945. if(!data) {
  946. data = {};
  947. }
  948. data.instance = this;
  949. this.element.triggerHandler(ev.replace('.jstree','') + '.jstree', data);
  950. },
  951. /**
  952. * returns the jQuery extended instance container
  953. * @name get_container()
  954. * @return {jQuery}
  955. */
  956. get_container : function () {
  957. return this.element;
  958. },
  959. /**
  960. * returns the jQuery extended main UL node inside the instance container. Used internally.
  961. * @private
  962. * @name get_container_ul()
  963. * @return {jQuery}
  964. */
  965. get_container_ul : function () {
  966. return this.element.children(".jstree-children").first();
  967. },
  968. /**
  969. * gets string replacements (localization). Used internally.
  970. * @private
  971. * @name get_string(key)
  972. * @param {String} key
  973. * @return {String}
  974. */
  975. get_string : function (key) {
  976. var a = this.settings.core.strings;
  977. if($.vakata.is_function(a)) { return a.call(this, key); }
  978. if(a && a[key]) { return a[key]; }
  979. return key;
  980. },
  981. /**
  982. * gets the first child of a DOM node. Used internally.
  983. * @private
  984. * @name _firstChild(dom)
  985. * @param {DOMElement} dom
  986. * @return {DOMElement}
  987. */
  988. _firstChild : function (dom) {
  989. dom = dom ? dom.firstChild : null;
  990. while(dom !== null && dom.nodeType !== 1) {
  991. dom = dom.nextSibling;
  992. }
  993. return dom;
  994. },
  995. /**
  996. * gets the next sibling of a DOM node. Used internally.
  997. * @private
  998. * @name _nextSibling(dom)
  999. * @param {DOMElement} dom
  1000. * @return {DOMElement}
  1001. */
  1002. _nextSibling : function (dom) {
  1003. dom = dom ? dom.nextSibling : null;
  1004. while(dom !== null && dom.nodeType !== 1) {
  1005. dom = dom.nextSibling;
  1006. }
  1007. return dom;
  1008. },
  1009. /**
  1010. * gets the previous sibling of a DOM node. Used internally.
  1011. * @private
  1012. * @name _previousSibling(dom)
  1013. * @param {DOMElement} dom
  1014. * @return {DOMElement}
  1015. */
  1016. _previousSibling : function (dom) {
  1017. dom = dom ? dom.previousSibling : null;
  1018. while(dom !== null && dom.nodeType !== 1) {
  1019. dom = dom.previousSibling;
  1020. }
  1021. return dom;
  1022. },
  1023. /**
  1024. * get the JSON representation of a node (or the actual jQuery extended DOM node) by using any input (child DOM element, ID string, selector, etc)
  1025. * @name get_node(obj [, as_dom])
  1026. * @param {mixed} obj
  1027. * @param {Boolean} as_dom
  1028. * @return {Object|jQuery}
  1029. */
  1030. get_node : function (obj, as_dom) {
  1031. if(obj && (obj.id || obj.id === 0)) {
  1032. obj = obj.id;
  1033. }
  1034. if (obj instanceof $ && obj.length && obj[0].id) {
  1035. obj = obj[0].id;
  1036. }
  1037. var dom;
  1038. try {
  1039. if(this._model.data[obj]) {
  1040. obj = this._model.data[obj];
  1041. }
  1042. else if(typeof obj === "string" && this._model.data[obj.replace(/^#/, '')]) {
  1043. obj = this._model.data[obj.replace(/^#/, '')];
  1044. }
  1045. else if(typeof obj === "string" && (dom = $('#' + obj.replace($.jstree.idregex,'\\$&'), this.element)).length && this._model.data[dom.closest('.jstree-node').attr('id')]) {
  1046. obj = this._model.data[dom.closest('.jstree-node').attr('id')];
  1047. }
  1048. else if((dom = this.element.find(obj)).length && this._model.data[dom.closest('.jstree-node').attr('id')]) {
  1049. obj = this._model.data[dom.closest('.jstree-node').attr('id')];
  1050. }
  1051. else if((dom = this.element.find(obj)).length && dom.hasClass('jstree')) {
  1052. obj = this._model.data[$.jstree.root];
  1053. }
  1054. else {
  1055. return false;
  1056. }
  1057. if(as_dom) {
  1058. obj = obj.id === $.jstree.root ? this.element : $('#' + obj.id.replace($.jstree.idregex,'\\$&'), this.element);
  1059. }
  1060. return obj;
  1061. } catch (ex) { return false; }
  1062. },
  1063. /**
  1064. * get the path to a node, either consisting of node texts, or of node IDs, optionally glued together (otherwise an array)
  1065. * @name get_path(obj [, glue, ids])
  1066. * @param {mixed} obj the node
  1067. * @param {String} glue if you want the path as a string - pass the glue here (for example '/'), if a falsy value is supplied here, an array is returned
  1068. * @param {Boolean} ids if set to true build the path using ID, otherwise node text is used
  1069. * @return {mixed}
  1070. */
  1071. get_path : function (obj, glue, ids) {
  1072. obj = obj.parents ? obj : this.get_node(obj);
  1073. if(!obj || obj.id === $.jstree.root || !obj.parents) {
  1074. return false;
  1075. }
  1076. var i, j, p = [];
  1077. p.push(ids ? obj.id : obj.text);
  1078. for(i = 0, j = obj.parents.length; i < j; i++) {
  1079. p.push(ids ? obj.parents[i] : this.get_text(obj.parents[i]));
  1080. }
  1081. p = p.reverse().slice(1);
  1082. return glue ? p.join(glue) : p;
  1083. },
  1084. /**
  1085. * get the next visible node that is below the `obj` node. If `strict` is set to `true` only sibling nodes are returned.
  1086. * @name get_next_dom(obj [, strict])
  1087. * @param {mixed} obj
  1088. * @param {Boolean} strict
  1089. * @return {jQuery}
  1090. */
  1091. get_next_dom : function (obj, strict) {
  1092. var tmp;
  1093. obj = this.get_node(obj, true);
  1094. if(obj[0] === this.element[0]) {
  1095. tmp = this._firstChild(this.get_container_ul()[0]);
  1096. while (tmp && tmp.offsetHeight === 0) {
  1097. tmp = this._nextSibling(tmp);
  1098. }
  1099. return tmp ? $(tmp) : false;
  1100. }
  1101. if(!obj || !obj.length) {
  1102. return false;
  1103. }
  1104. if(strict) {
  1105. tmp = obj[0];
  1106. do {
  1107. tmp = this._nextSibling(tmp);
  1108. } while (tmp && tmp.offsetHeight === 0);
  1109. return tmp ? $(tmp) : false;
  1110. }
  1111. if(obj.hasClass("jstree-open")) {
  1112. tmp = this._firstChild(obj.children('.jstree-children')[0]);
  1113. while (tmp && tmp.offsetHeight === 0) {
  1114. tmp = this._nextSibling(tmp);
  1115. }
  1116. if(tmp !== null) {
  1117. return $(tmp);
  1118. }
  1119. }
  1120. tmp = obj[0];
  1121. do {
  1122. tmp = this._nextSibling(tmp);
  1123. } while (tmp && tmp.offsetHeight === 0);
  1124. if(tmp !== null) {
  1125. return $(tmp);
  1126. }
  1127. return obj.parentsUntil(".jstree",".jstree-node").nextAll(".jstree-node:visible").first();
  1128. },
  1129. /**
  1130. * get the previous visible node that is above the `obj` node. If `strict` is set to `true` only sibling nodes are returned.
  1131. * @name get_prev_dom(obj [, strict])
  1132. * @param {mixed} obj
  1133. * @param {Boolean} strict
  1134. * @return {jQuery}
  1135. */
  1136. get_prev_dom : function (obj, strict) {
  1137. var tmp;
  1138. obj = this.get_node(obj, true);
  1139. if(obj[0] === this.element[0]) {
  1140. tmp = this.get_container_ul()[0].lastChild;
  1141. while (tmp && tmp.offsetHeight === 0) {
  1142. tmp = this._previousSibling(tmp);
  1143. }
  1144. return tmp ? $(tmp) : false;
  1145. }
  1146. if(!obj || !obj.length) {
  1147. return false;
  1148. }
  1149. if(strict) {
  1150. tmp = obj[0];
  1151. do {
  1152. tmp = this._previousSibling(tmp);
  1153. } while (tmp && tmp.offsetHeight === 0);
  1154. return tmp ? $(tmp) : false;
  1155. }
  1156. tmp = obj[0];
  1157. do {
  1158. tmp = this._previousSibling(tmp);
  1159. } while (tmp && tmp.offsetHeight === 0);
  1160. if(tmp !== null) {
  1161. obj = $(tmp);
  1162. while(obj.hasClass("jstree-open")) {
  1163. obj = obj.children(".jstree-children").first().children(".jstree-node:visible:last");
  1164. }
  1165. return obj;
  1166. }
  1167. tmp = obj[0].parentNode.parentNode;
  1168. return tmp && tmp.className && tmp.className.indexOf('jstree-node') !== -1 ? $(tmp) : false;
  1169. },
  1170. /**
  1171. * get the parent ID of a node
  1172. * @name get_parent(obj)
  1173. * @param {mixed} obj
  1174. * @return {String}
  1175. */
  1176. get_parent : function (obj) {
  1177. obj = this.get_node(obj);
  1178. if(!obj || obj.id === $.jstree.root) {
  1179. return false;
  1180. }
  1181. return obj.parent;
  1182. },
  1183. /**
  1184. * get a jQuery collection of all the children of a node (node must be rendered), returns false on error
  1185. * @name get_children_dom(obj)
  1186. * @param {mixed} obj
  1187. * @return {jQuery}
  1188. */
  1189. get_children_dom : function (obj) {
  1190. obj = this.get_node(obj, true);
  1191. if(obj[0] === this.element[0]) {
  1192. return this.get_container_ul().children(".jstree-node");
  1193. }
  1194. if(!obj || !obj.length) {
  1195. return false;
  1196. }
  1197. return obj.children(".jstree-children").children(".jstree-node");
  1198. },
  1199. /**
  1200. * checks if a node has children
  1201. * @name is_parent(obj)
  1202. * @param {mixed} obj
  1203. * @return {Boolean}
  1204. */
  1205. is_parent : function (obj) {
  1206. obj = this.get_node(obj);
  1207. return obj && (obj.state.loaded === false || obj.children.length > 0);
  1208. },
  1209. /**
  1210. * checks if a node is loaded (its children are available)
  1211. * @name is_loaded(obj)
  1212. * @param {mixed} obj
  1213. * @return {Boolean}
  1214. */
  1215. is_loaded : function (obj) {
  1216. obj = this.get_node(obj);
  1217. return obj && obj.state.loaded;
  1218. },
  1219. /**
  1220. * check if a node is currently loading (fetching children)
  1221. * @name is_loading(obj)
  1222. * @param {mixed} obj
  1223. * @return {Boolean}
  1224. */
  1225. is_loading : function (obj) {
  1226. obj = this.get_node(obj);
  1227. return obj && obj.state && obj.state.loading;
  1228. },
  1229. /**
  1230. * check if a node is opened
  1231. * @name is_open(obj)
  1232. * @param {mixed} obj
  1233. * @return {Boolean}
  1234. */
  1235. is_open : function (obj) {
  1236. obj = this.get_node(obj);
  1237. return obj && obj.state.opened;
  1238. },
  1239. /**
  1240. * check if a node is in a closed state
  1241. * @name is_closed(obj)
  1242. * @param {mixed} obj
  1243. * @return {Boolean}
  1244. */
  1245. is_closed : function (obj) {
  1246. obj = this.get_node(obj);
  1247. return obj && this.is_parent(obj) && !obj.state.opened;
  1248. },
  1249. /**
  1250. * check if a node has no children
  1251. * @name is_leaf(obj)
  1252. * @param {mixed} obj
  1253. * @return {Boolean}
  1254. */
  1255. is_leaf : function (obj) {
  1256. return !this.is_parent(obj);
  1257. },
  1258. /**
  1259. * loads a node (fetches its children using the `core.data` setting). Multiple nodes can be passed to by using an array.
  1260. * @name load_node(obj [, callback])
  1261. * @param {mixed} obj
  1262. * @param {function} callback a function to be executed once loading is complete, the function is executed in the instance's scope and receives two arguments - the node and a boolean status
  1263. * @return {Boolean}
  1264. * @trigger load_node.jstree
  1265. */
  1266. load_node : function (obj, callback) {
  1267. var dom = this.get_node(obj, true), k, l, i, j, c;
  1268. if($.vakata.is_array(obj)) {
  1269. this._load_nodes(obj.slice(), callback);
  1270. return true;
  1271. }
  1272. obj = this.get_node(obj);
  1273. if(!obj) {
  1274. if(callback) { callback.call(this, obj, false); }
  1275. return false;
  1276. }
  1277. // if(obj.state.loading) { } // the node is already loading - just wait for it to load and invoke callback? but if called implicitly it should be loaded again?
  1278. if(obj.state.loaded) {
  1279. obj.state.loaded = false;
  1280. for(i = 0, j = obj.parents.length; i < j; i++) {
  1281. this._model.data[obj.parents[i]].children_d = $.vakata.array_filter(this._model.data[obj.parents[i]].children_d, function (v) {
  1282. return $.inArray(v, obj.children_d) === -1;
  1283. });
  1284. }
  1285. for(k = 0, l = obj.children_d.length; k < l; k++) {
  1286. if(this._model.data[obj.children_d[k]].state.selected) {
  1287. c = true;
  1288. }
  1289. delete this._model.data[obj.children_d[k]];
  1290. }
  1291. if (c) {
  1292. this._data.core.selected = $.vakata.array_filter(this._data.core.selected, function (v) {
  1293. return $.inArray(v, obj.children_d) === -1;
  1294. });
  1295. }
  1296. obj.children = [];
  1297. obj.children_d = [];
  1298. if(c) {
  1299. this.trigger('changed', { 'action' : 'load_node', 'node' : obj, 'selected' : this._data.core.selected });
  1300. }
  1301. }
  1302. obj.state.failed = false;
  1303. obj.state.loading = true;
  1304. if (obj.id !== $.jstree.root) {
  1305. dom.children(".jstree-anchor").attr('aria-busy', true);
  1306. } else {
  1307. dom.attr('aria-busy', true);
  1308. }
  1309. dom.addClass("jstree-loading");
  1310. this._load_node(obj, function (status) {
  1311. obj = this._model.data[obj.id];
  1312. obj.state.loading = false;
  1313. obj.state.loaded = status;
  1314. obj.state.failed = !obj.state.loaded;
  1315. var dom = this.get_node(obj, true), i = 0, j = 0, m = this._model.data, has_children = false;
  1316. for(i = 0, j = obj.children.length; i < j; i++) {
  1317. if(m[obj.children[i]] && !m[obj.children[i]].state.hidden) {
  1318. has_children = true;
  1319. break;
  1320. }
  1321. }
  1322. if(obj.state.loaded && dom && dom.length) {
  1323. dom.removeClass('jstree-closed jstree-open jstree-leaf');
  1324. if (!has_children) {
  1325. dom.addClass('jstree-leaf');
  1326. }
  1327. else {
  1328. if (obj.id !== '#') {
  1329. dom.addClass(obj.state.opened ? 'jstree-open' : 'jstree-closed');
  1330. }
  1331. }
  1332. }
  1333. if (obj.id !== $.jstree.root) {
  1334. dom.children(".jstree-anchor").attr('aria-busy', false);
  1335. } else {
  1336. dom.attr('aria-busy', false);
  1337. }
  1338. dom.removeClass("jstree-loading");
  1339. /**
  1340. * triggered after a node is loaded
  1341. * @event
  1342. * @name load_node.jstree
  1343. * @param {Object} node the node that was loading
  1344. * @param {Boolean} status was the node loaded successfully
  1345. */
  1346. this.trigger('load_node', { "node" : obj, "status" : status });
  1347. if(callback) {
  1348. callback.call(this, obj, status);
  1349. }
  1350. }.bind(this));
  1351. return true;
  1352. },
  1353. /**
  1354. * load an array of nodes (will also load unavailable nodes as soon as they appear in the structure). Used internally.
  1355. * @private
  1356. * @name _load_nodes(nodes [, callback])
  1357. * @param {array} nodes
  1358. * @param {function} callback a function to be executed once loading is complete, the function is executed in the instance's scope and receives one argument - the array passed to _load_nodes
  1359. */
  1360. _load_nodes : function (nodes, callback, is_callback, force_reload) {
  1361. var r = true,
  1362. c = function () { this._load_nodes(nodes, callback, true); },
  1363. m = this._model.data, i, j, tmp = [];
  1364. for(i = 0, j = nodes.length; i < j; i++) {
  1365. if(m[nodes[i]] && ( (!m[nodes[i]].state.loaded && !m[nodes[i]].state.failed) || (!is_callback && force_reload) )) {
  1366. if(!this.is_loading(nodes[i])) {
  1367. this.load_node(nodes[i], c);
  1368. }
  1369. r = false;
  1370. }
  1371. }
  1372. if(r) {
  1373. for(i = 0, j = nodes.length; i < j; i++) {
  1374. if(m[nodes[i]] && m[nodes[i]].state.loaded) {
  1375. tmp.push(nodes[i]);
  1376. }
  1377. }
  1378. if(callback && !callback.done) {
  1379. callback.call(this, tmp);
  1380. callback.done = true;
  1381. }
  1382. }
  1383. },
  1384. /**
  1385. * loads all unloaded nodes
  1386. * @name load_all([obj, callback])
  1387. * @param {mixed} obj the node to load recursively, omit to load all nodes in the tree
  1388. * @param {function} callback a function to be executed once loading all the nodes is complete,
  1389. * @trigger load_all.jstree
  1390. */
  1391. load_all : function (obj, callback) {
  1392. if(!obj) { obj = $.jstree.root; }
  1393. obj = this.get_node(obj);
  1394. if(!obj) { return false; }
  1395. var to_load = [],
  1396. m = this._model.data,
  1397. c = m[obj.id].children_d,
  1398. i, j;
  1399. if(obj.state && !obj.state.loaded) {
  1400. to_load.push(obj.id);
  1401. }
  1402. for(i = 0, j = c.length; i < j; i++) {
  1403. if(m[c[i]] && m[c[i]].state && !m[c[i]].state.loaded) {
  1404. to_load.push(c[i]);
  1405. }
  1406. }
  1407. if(to_load.length) {
  1408. this._load_nodes(to_load, function () {
  1409. this.load_all(obj, callback);
  1410. });
  1411. }
  1412. else {
  1413. /**
  1414. * triggered after a load_all call completes
  1415. * @event
  1416. * @name load_all.jstree
  1417. * @param {Object} node the recursively loaded node
  1418. */
  1419. if(callback) { callback.call(this, obj); }
  1420. this.trigger('load_all', { "node" : obj });
  1421. }
  1422. },
  1423. /**
  1424. * handles the actual loading of a node. Used only internally.
  1425. * @private
  1426. * @name _load_node(obj [, callback])
  1427. * @param {mixed} obj
  1428. * @param {function} callback a function to be executed once loading is complete, the function is executed in the instance's scope and receives one argument - a boolean status
  1429. * @return {Boolean}
  1430. */
  1431. _load_node : function (obj, callback) {
  1432. var s = this.settings.core.data, t;
  1433. var notTextOrCommentNode = function notTextOrCommentNode () {
  1434. return this.nodeType !== 3 && this.nodeType !== 8;
  1435. };
  1436. // use original HTML
  1437. if(!s) {
  1438. if(obj.id === $.jstree.root) {
  1439. return this._append_html_data(obj, this._data.core.original_container_html.clone(true), function (status) {
  1440. callback.call(this, status);
  1441. });
  1442. }
  1443. else {
  1444. return callback.call(this, false);
  1445. }
  1446. // return callback.call(this, obj.id === $.jstree.root ? this._append_html_data(obj, this._data.core.original_container_html.clone(true)) : false);
  1447. }
  1448. if($.vakata.is_function(s)) {
  1449. return s.call(this, obj, function (d) {
  1450. if(d === false) {
  1451. callback.call(this, false);
  1452. }
  1453. else {
  1454. this[typeof d === 'string' ? '_append_html_data' : '_append_json_data'](obj, typeof d === 'string' ? $($.parseHTML(d)).filter(notTextOrCommentNode) : d, function (status) {
  1455. callback.call(this, status);
  1456. });
  1457. }
  1458. // return d === false ? callback.call(this, false) : callback.call(this, this[typeof d === 'string' ? '_append_html_data' : '_append_json_data'](obj, typeof d === 'string' ? $(d) : d));
  1459. }.bind(this));
  1460. }
  1461. if(typeof s === 'object') {
  1462. if(s.url) {
  1463. s = $.extend(true, {}, s);
  1464. if($.vakata.is_function(s.url)) {
  1465. s.url = s.url.call(this, obj);
  1466. }
  1467. if($.vakata.is_function(s.data)) {
  1468. s.data = s.data.call(this, obj);
  1469. }
  1470. return $.ajax(s)
  1471. .done(function (d,t,x) {
  1472. var type = x.getResponseHeader('Content-Type');
  1473. if((type && type.indexOf('json') !== -1) || typeof d === "object") {
  1474. return this._append_json_data(obj, d, function (status) { callback.call(this, status); });
  1475. //return callback.call(this, this._append_json_data(obj, d));
  1476. }
  1477. if((type && type.indexOf('html') !== -1) || typeof d === "string") {
  1478. return this._append_html_data(obj, $($.parseHTML(d)).filter(notTextOrCommentNode), function (status) { callback.call(this, status); });
  1479. // return callback.call(this, this._append_html_data(obj, $(d)));
  1480. }
  1481. this._data.core.last_error = { 'error' : 'ajax', 'plugin' : 'core', 'id' : 'core_04', 'reason' : 'Could not load node', 'data' : JSON.stringify({ 'id' : obj.id, 'xhr' : x }) };
  1482. this.settings.core.error.call(this, this._data.core.last_error);
  1483. return callback.call(this, false);
  1484. }.bind(this))
  1485. .fail(function (f) {
  1486. this._data.core.last_error = { 'error' : 'ajax', 'plugin' : 'core', 'id' : 'core_04', 'reason' : 'Could not load node', 'data' : JSON.stringify({ 'id' : obj.id, 'xhr' : f }) };
  1487. callback.call(this, false);
  1488. this.settings.core.error.call(this, this._data.core.last_error);
  1489. }.bind(this));
  1490. }
  1491. if ($.vakata.is_array(s)) {
  1492. t = $.extend(true, [], s);
  1493. } else if ($.isPlainObject(s)) {
  1494. t = $.extend(true, {}, s);
  1495. } else {
  1496. t = s;
  1497. }
  1498. if(obj.id === $.jstree.root) {
  1499. return this._append_json_data(obj, t, function (status) {
  1500. callback.call(this, status);
  1501. });
  1502. }
  1503. else {
  1504. this._data.core.last_error = { 'error' : 'nodata', 'plugin' : 'core', 'id' : 'core_05', 'reason' : 'Could not load node', 'data' : JSON.stringify({ 'id' : obj.id }) };
  1505. this.settings.core.error.call(this, this._data.core.last_error);
  1506. return callback.call(this, false);
  1507. }
  1508. //return callback.call(this, (obj.id === $.jstree.root ? this._append_json_data(obj, t) : false) );
  1509. }
  1510. if(typeof s === 'string') {
  1511. if(obj.id === $.jstree.root) {
  1512. return this._append_html_data(obj, $($.parseHTML(s)).filter(notTextOrCommentNode), function (status) {
  1513. callback.call(this, status);
  1514. });
  1515. }
  1516. else {
  1517. this._data.core.last_error = { 'error' : 'nodata', 'plugin' : 'core', 'id' : 'core_06', 'reason' : 'Could not load node', 'data' : JSON.stringify({ 'id' : obj.id }) };
  1518. this.settings.core.error.call(this, this._data.core.last_error);
  1519. return callback.call(this, false);
  1520. }
  1521. //return callback.call(this, (obj.id === $.jstree.root ? this._append_html_data(obj, $(s)) : false) );
  1522. }
  1523. return callback.call(this, false);
  1524. },
  1525. /**
  1526. * adds a node to the list of nodes to redraw. Used only internally.
  1527. * @private
  1528. * @name _node_changed(obj [, callback])
  1529. * @param {mixed} obj
  1530. */
  1531. _node_changed : function (obj) {
  1532. obj = this.get_node(obj);
  1533. if (obj && $.inArray(obj.id, this._model.changed) === -1) {
  1534. this._model.changed.push(obj.id);
  1535. }
  1536. },
  1537. /**
  1538. * appends HTML content to the tree. Used internally.
  1539. * @private
  1540. * @name _append_html_data(obj, data)
  1541. * @param {mixed} obj the node to append to
  1542. * @param {String} data the HTML string to parse and append
  1543. * @trigger model.jstree, changed.jstree
  1544. */
  1545. _append_html_data : function (dom, data, cb) {
  1546. dom = this.get_node(dom);
  1547. dom.children = [];
  1548. dom.children_d = [];
  1549. var dat = data.is('ul') ? data.children() : data,
  1550. par = dom.id,
  1551. chd = [],
  1552. dpc = [],
  1553. m = this._model.data,
  1554. p = m[par],
  1555. s = this._data.core.selected.length,
  1556. tmp, i, j;
  1557. dat.each(function (i, v) {
  1558. tmp = this._parse_model_from_html($(v), par, p.parents.concat());
  1559. if(tmp) {
  1560. chd.push(tmp);
  1561. dpc.push(tmp);
  1562. if(m[tmp].children_d.length) {
  1563. dpc = dpc.concat(m[tmp].children_d);
  1564. }
  1565. }
  1566. }.bind(this));
  1567. p.children = chd;
  1568. p.children_d = dpc;
  1569. for(i = 0, j = p.parents.length; i < j; i++) {
  1570. m[p.parents[i]].children_d = m[p.parents[i]].children_d.concat(dpc);
  1571. }
  1572. /**
  1573. * triggered when new data is inserted to the tree model
  1574. * @event
  1575. * @name model.jstree
  1576. * @param {Array} nodes an array of node IDs
  1577. * @param {String} parent the parent ID of the nodes
  1578. */
  1579. this.trigger('model', { "nodes" : dpc, 'parent' : par });
  1580. if(par !== $.jstree.root) {
  1581. this._node_changed(par);
  1582. this.redraw();
  1583. }
  1584. else {
  1585. this.get_container_ul().children('.jstree-initial-node').remove();
  1586. this.redraw(true);
  1587. }
  1588. if(this._data.core.selected.length !== s) {
  1589. this.trigger('changed', { 'action' : 'model', 'selected' : this._data.core.selected });
  1590. }
  1591. cb.call(this, true);
  1592. },
  1593. /**
  1594. * appends JSON content to the tree. Used internally.
  1595. * @private
  1596. * @name _append_json_data(obj, data)
  1597. * @param {mixed} obj the node to append to
  1598. * @param {String} data the JSON object to parse and append
  1599. * @param {Boolean} force_processing internal param - do not set
  1600. * @trigger model.jstree, changed.jstree
  1601. */
  1602. _append_json_data : function (dom, data, cb, force_processing) {
  1603. if(this.element === null) { return; }
  1604. dom = this.get_node(dom);
  1605. dom.children = [];
  1606. dom.children_d = [];
  1607. // *%$@!!!
  1608. if(data.d) {
  1609. data = data.d;
  1610. if(typeof data === "string") {
  1611. data = JSON.parse(data);
  1612. }
  1613. }
  1614. if(!$.vakata.is_array(data)) { data = [data]; }
  1615. var w = null,
  1616. args = {
  1617. 'df' : this._model.default_state,
  1618. 'dat' : data,
  1619. 'par' : dom.id,
  1620. 'm' : this._model.data,
  1621. 't_id' : this._id,
  1622. 't_cnt' : this._cnt,
  1623. 'sel' : this._data.core.selected
  1624. },
  1625. inst = this,
  1626. func = function (data, undefined) {
  1627. if(data.data) { data = data.data; }
  1628. var dat = data.dat,
  1629. par = data.par,
  1630. chd = [],
  1631. dpc = [],
  1632. add = [],
  1633. df = data.df,
  1634. t_id = data.t_id,
  1635. t_cnt = data.t_cnt,
  1636. m = data.m,
  1637. p = m[par],
  1638. sel = data.sel,
  1639. tmp, i, j, rslt,
  1640. parse_flat = function (d, p, ps) {
  1641. if(!ps) { ps = []; }
  1642. else { ps = ps.concat(); }
  1643. if(p) { ps.unshift(p); }
  1644. var tid = d.id.toString(),
  1645. i, j, c, e,
  1646. tmp = {
  1647. id : tid,
  1648. text : d.text || '',
  1649. icon : d.icon !== undefined ? d.icon : true,
  1650. parent : p,
  1651. parents : ps,
  1652. children : d.children || [],
  1653. children_d : d.children_d || [],
  1654. data : d.data,
  1655. state : { },
  1656. li_attr : { id : false },
  1657. a_attr : { href : '#' },
  1658. original : false
  1659. };
  1660. for(i in df) {
  1661. if(df.hasOwnProperty(i)) {
  1662. tmp.state[i] = df[i];
  1663. }
  1664. }
  1665. if(d && d.data && d.data.jstree && d.data.jstree.icon) {
  1666. tmp.icon = d.data.jstree.icon;
  1667. }
  1668. if(tmp.icon === undefined || tmp.icon === null || tmp.icon === "") {
  1669. tmp.icon = true;
  1670. }
  1671. if(d && d.data) {
  1672. tmp.data = d.data;
  1673. if(d.data.jstree) {
  1674. for(i in d.data.jstree) {
  1675. if(d.data.jstree.hasOwnProperty(i)) {
  1676. tmp.state[i] = d.data.jstree[i];
  1677. }
  1678. }
  1679. }
  1680. }
  1681. if(d && typeof d.state === 'object') {
  1682. for (i in d.state) {
  1683. if(d.state.hasOwnProperty(i)) {
  1684. tmp.state[i] = d.state[i];
  1685. }
  1686. }
  1687. }
  1688. if(d && typeof d.li_attr === 'object') {
  1689. for (i in d.li_attr) {
  1690. if(d.li_attr.hasOwnProperty(i)) {
  1691. tmp.li_attr[i] = d.li_attr[i];
  1692. }
  1693. }
  1694. }
  1695. if(!tmp.li_attr.id) {
  1696. tmp.li_attr.id = tid;
  1697. }
  1698. if(d && typeof d.a_attr === 'object') {
  1699. for (i in d.a_attr) {
  1700. if(d.a_attr.hasOwnProperty(i)) {
  1701. tmp.a_attr[i] = d.a_attr[i];
  1702. }
  1703. }
  1704. }
  1705. if(d && d.children && d.children === true) {
  1706. tmp.state.loaded = false;
  1707. tmp.children = [];
  1708. tmp.children_d = [];
  1709. }
  1710. m[tmp.id] = tmp;
  1711. for(i = 0, j = tmp.children.length; i < j; i++) {
  1712. c = parse_flat(m[tmp.children[i]], tmp.id, ps);
  1713. e = m[c];
  1714. tmp.children_d.push(c);
  1715. if(e.children_d.length) {
  1716. tmp.children_d = tmp.children_d.concat(e.children_d);
  1717. }
  1718. }
  1719. delete d.data;
  1720. delete d.children;
  1721. m[tmp.id].original = d;
  1722. if(tmp.state.selected) {
  1723. add.push(tmp.id);
  1724. }
  1725. return tmp.id;
  1726. },
  1727. parse_nest = function (d, p, ps) {
  1728. if(!ps) { ps = []; }
  1729. else { ps = ps.concat(); }
  1730. if(p) { ps.unshift(p); }
  1731. var tid = false, i, j, c, e, tmp;
  1732. do {
  1733. tid = 'j' + t_id + '_' + (++t_cnt);
  1734. } while(m[tid]);
  1735. tmp = {
  1736. id : false,
  1737. text : typeof d === 'string' ? d : '',
  1738. icon : typeof d === 'object' && d.icon !== undefined ? d.icon : true,
  1739. parent : p,
  1740. parents : ps,
  1741. children : [],
  1742. children_d : [],
  1743. data : null,
  1744. state : { },
  1745. li_attr : { id : false },
  1746. a_attr : { href : '#' },
  1747. original : false
  1748. };
  1749. for(i in df) {
  1750. if(df.hasOwnProperty(i)) {
  1751. tmp.state[i] = df[i];
  1752. }
  1753. }
  1754. if(d && (d.id || d.id === 0)) { tmp.id = d.id.toString(); }
  1755. if(d && d.text) { tmp.text = d.text; }
  1756. if(d && d.data && d.data.jstree && d.data.jstree.icon) {
  1757. tmp.icon = d.data.jstree.icon;
  1758. }
  1759. if(tmp.icon === undefined || tmp.icon === null || tmp.icon === "") {
  1760. tmp.icon = true;
  1761. }
  1762. if(d && d.data) {
  1763. tmp.data = d.data;
  1764. if(d.data.jstree) {
  1765. for(i in d.data.jstree) {
  1766. if(d.data.jstree.hasOwnProperty(i)) {
  1767. tmp.state[i] = d.data.jstree[i];
  1768. }
  1769. }
  1770. }
  1771. }
  1772. if(d && typeof d.state === 'object') {
  1773. for (i in d.state) {
  1774. if(d.state.hasOwnProperty(i)) {
  1775. tmp.state[i] = d.state[i];
  1776. }
  1777. }
  1778. }
  1779. if(d && typeof d.li_attr === 'object') {
  1780. for (i in d.li_attr) {
  1781. if(d.li_attr.hasOwnProperty(i)) {
  1782. tmp.li_attr[i] = d.li_attr[i];
  1783. }
  1784. }
  1785. }
  1786. if(tmp.li_attr.id && !(tmp.id || tmp.id === 0)) {
  1787. tmp.id = tmp.li_attr.id.toString();
  1788. }
  1789. if(!(tmp.id || tmp.id === 0)) {
  1790. tmp.id = tid;
  1791. }
  1792. if(!tmp.li_attr.id) {
  1793. tmp.li_attr.id = tmp.id;
  1794. }
  1795. if(d && typeof d.a_attr === 'object') {
  1796. for (i in d.a_attr) {
  1797. if(d.a_attr.hasOwnProperty(i)) {
  1798. tmp.a_attr[i] = d.a_attr[i];
  1799. }
  1800. }
  1801. }
  1802. if(d && d.children && d.children.length) {
  1803. for(i = 0, j = d.children.length; i < j; i++) {
  1804. c = parse_nest(d.children[i], tmp.id, ps);
  1805. e = m[c];
  1806. tmp.children.push(c);
  1807. if(e.children_d.length) {
  1808. tmp.children_d = tmp.children_d.concat(e.children_d);
  1809. }
  1810. }
  1811. tmp.children_d = tmp.children_d.concat(tmp.children);
  1812. }
  1813. if(d && d.children && d.children === true) {
  1814. tmp.state.loaded = false;
  1815. tmp.children = [];
  1816. tmp.children_d = [];
  1817. }
  1818. delete d.data;
  1819. delete d.children;
  1820. tmp.original = d;
  1821. m[tmp.id] = tmp;
  1822. if(tmp.state.selected) {
  1823. add.push(tmp.id);
  1824. }
  1825. return tmp.id;
  1826. };
  1827. if(dat.length && dat[0].id !== undefined && dat[0].parent !== undefined) {
  1828. // Flat JSON support (for easy import from DB):
  1829. // 1) convert to object (foreach)
  1830. for(i = 0, j = dat.length; i < j; i++) {
  1831. if(!dat[i].children) {
  1832. dat[i].children = [];
  1833. }
  1834. if(!dat[i].state) {
  1835. dat[i].state = {};
  1836. }
  1837. m[dat[i].id.toString()] = dat[i];
  1838. }
  1839. // 2) populate children (foreach)
  1840. for(i = 0, j = dat.length; i < j; i++) {
  1841. if (!m[dat[i].parent.toString()]) {
  1842. if (typeof inst !== "undefined") {
  1843. inst._data.core.last_error = { 'error' : 'parse', 'plugin' : 'core', 'id' : 'core_07', 'reason' : 'Node with invalid parent', 'data' : JSON.stringify({ 'id' : dat[i].id.toString(), 'parent' : dat[i].parent.toString() }) };
  1844. inst.settings.core.error.call(inst, inst._data.core.last_error);
  1845. }
  1846. continue;
  1847. }
  1848. m[dat[i].parent.toString()].children.push(dat[i].id.toString());
  1849. // populate parent.children_d
  1850. p.children_d.push(dat[i].id.toString());
  1851. }
  1852. // 3) normalize && populate parents and children_d with recursion
  1853. for(i = 0, j = p.children.length; i < j; i++) {
  1854. tmp = parse_flat(m[p.children[i]], par, p.parents.concat());
  1855. dpc.push(tmp);
  1856. if(m[tmp].children_d.length) {
  1857. dpc = dpc.concat(m[tmp].children_d);
  1858. }
  1859. }
  1860. for(i = 0, j = p.parents.length; i < j; i++) {
  1861. m[p.parents[i]].children_d = m[p.parents[i]].children_d.concat(dpc);
  1862. }
  1863. // ?) three_state selection - p.state.selected && t - (if three_state foreach(dat => ch) -> foreach(parents) if(parent.selected) child.selected = true;
  1864. rslt = {
  1865. 'cnt' : t_cnt,
  1866. 'mod' : m,
  1867. 'sel' : sel,
  1868. 'par' : par,
  1869. 'dpc' : dpc,
  1870. 'add' : add
  1871. };
  1872. }
  1873. else {
  1874. for(i = 0, j = dat.length; i < j; i++) {
  1875. tmp = parse_nest(dat[i], par, p.parents.concat());
  1876. if(tmp) {
  1877. chd.push(tmp);
  1878. dpc.push(tmp);
  1879. if(m[tmp].children_d.length) {
  1880. dpc = dpc.concat(m[tmp].children_d);
  1881. }
  1882. }
  1883. }
  1884. p.children = chd;
  1885. p.children_d = dpc;
  1886. for(i = 0, j = p.parents.length; i < j; i++) {
  1887. m[p.parents[i]].children_d = m[p.parents[i]].children_d.concat(dpc);
  1888. }
  1889. rslt = {
  1890. 'cnt' : t_cnt,
  1891. 'mod' : m,
  1892. 'sel' : sel,
  1893. 'par' : par,
  1894. 'dpc' : dpc,
  1895. 'add' : add
  1896. };
  1897. }
  1898. if(typeof window === 'undefined' || typeof window.document === 'undefined') {
  1899. postMessage(rslt);
  1900. }
  1901. else {
  1902. return rslt;
  1903. }
  1904. },
  1905. rslt = function (rslt, worker) {
  1906. if(this.element === null) { return; }
  1907. this._cnt = rslt.cnt;
  1908. var i, m = this._model.data;
  1909. for (i in m) {
  1910. if (m.hasOwnProperty(i) && m[i].state && m[i].state.loading && rslt.mod[i]) {
  1911. rslt.mod[i].state.loading = true;
  1912. }
  1913. }
  1914. this._model.data = rslt.mod; // breaks the reference in load_node - careful
  1915. if(worker) {
  1916. var j, a = rslt.add, r = rslt.sel, s = this._data.core.selected.slice();
  1917. m = this._model.data;
  1918. // if selection was changed while calculating in worker
  1919. if(r.length !== s.length || $.vakata.array_unique(r.concat(s)).length !== r.length) {
  1920. // deselect nodes that are no longer selected
  1921. for(i = 0, j = r.length; i < j; i++) {
  1922. if($.inArray(r[i], a) === -1 && $.inArray(r[i], s) === -1) {
  1923. m[r[i]].state.selected = false;
  1924. }
  1925. }
  1926. // select nodes that were selected in the mean time
  1927. for(i = 0, j = s.length; i < j; i++) {
  1928. if($.inArray(s[i], r) === -1) {
  1929. m[s[i]].state.selected = true;
  1930. }
  1931. }
  1932. }
  1933. }
  1934. if(rslt.add.length) {
  1935. this._data.core.selected = this._data.core.selected.concat(rslt.add);
  1936. }
  1937. this.trigger('model', { "nodes" : rslt.dpc, 'parent' : rslt.par });
  1938. if(rslt.par !== $.jstree.root) {
  1939. this._node_changed(rslt.par);
  1940. this.redraw();
  1941. }
  1942. else {
  1943. // this.get_container_ul().children('.jstree-initial-node').remove();
  1944. this.redraw(true);
  1945. }
  1946. if(rslt.add.length) {
  1947. this.trigger('changed', { 'action' : 'model', 'selected' : this._data.core.selected });
  1948. }
  1949. // If no worker, try to mimic worker behavioour, by invoking cb asynchronously
  1950. if (!worker && setImmediate) {
  1951. setImmediate(function(){
  1952. cb.call(inst, true);
  1953. });
  1954. }
  1955. else {
  1956. cb.call(inst, true);
  1957. }
  1958. };
  1959. if(this.settings.core.worker && window.Blob && window.URL && window.Worker) {
  1960. try {
  1961. if(this._wrk === null) {
  1962. this._wrk = window.URL.createObjectURL(
  1963. new window.Blob(
  1964. ['self.onmessage = ' + func.toString()],
  1965. {type:"text/javascript"}
  1966. )
  1967. );
  1968. }
  1969. if(!this._data.core.working || force_processing) {
  1970. this._data.core.working = true;
  1971. w = new window.Worker(this._wrk);
  1972. w.onmessage = function (e) {
  1973. rslt.call(this, e.data, true);
  1974. try { w.terminate(); w = null; } catch(ignore) { }
  1975. if(this._data.core.worker_queue.length) {
  1976. this._append_json_data.apply(this, this._data.core.worker_queue.shift());
  1977. }
  1978. else {
  1979. this._data.core.working = false;
  1980. }
  1981. }.bind(this);
  1982. w.onerror = function (e) {
  1983. rslt.call(this, func(args), false);
  1984. if(this._data.core.worker_queue.length) {
  1985. this._append_json_data.apply(this, this._data.core.worker_queue.shift());
  1986. }
  1987. else {
  1988. this._data.core.working = false;
  1989. }
  1990. }.bind(this);
  1991. if(!args.par) {
  1992. if(this._data.core.worker_queue.length) {
  1993. this._append_json_data.apply(this, this._data.core.worker_queue.shift());
  1994. }
  1995. else {
  1996. this._data.core.working = false;
  1997. }
  1998. }
  1999. else {
  2000. w.postMessage(args);
  2001. }
  2002. }
  2003. else {
  2004. this._data.core.worker_queue.push([dom, data, cb, true]);
  2005. }
  2006. }
  2007. catch(e) {
  2008. rslt.call(this, func(args), false);
  2009. if(this._data.core.worker_queue.length) {
  2010. this._append_json_data.apply(this, this._data.core.worker_queue.shift());
  2011. }
  2012. else {
  2013. this._data.core.working = false;
  2014. }
  2015. }
  2016. }
  2017. else {
  2018. rslt.call(this, func(args), false);
  2019. }
  2020. },
  2021. /**
  2022. * parses a node from a jQuery object and appends them to the in memory tree model. Used internally.
  2023. * @private
  2024. * @name _parse_model_from_html(d [, p, ps])
  2025. * @param {jQuery} d the jQuery object to parse
  2026. * @param {String} p the parent ID
  2027. * @param {Array} ps list of all parents
  2028. * @return {String} the ID of the object added to the model
  2029. */
  2030. _parse_model_from_html : function (d, p, ps) {
  2031. if(!ps) { ps = []; }
  2032. else { ps = [].concat(ps); }
  2033. if(p) { ps.unshift(p); }
  2034. var c, e, m = this._model.data,
  2035. data = {
  2036. id : false,
  2037. text : false,
  2038. icon : true,
  2039. parent : p,
  2040. parents : ps,
  2041. children : [],
  2042. children_d : [],
  2043. data : null,
  2044. state : { },
  2045. li_attr : { id : false },
  2046. a_attr : { href : '#' },
  2047. original : false
  2048. }, i, tmp, tid;
  2049. for(i in this._model.default_state) {
  2050. if(this._model.default_state.hasOwnProperty(i)) {
  2051. data.state[i] = this._model.default_state[i];
  2052. }
  2053. }
  2054. tmp = $.vakata.attributes(d, true);
  2055. $.each(tmp, function (i, v) {
  2056. v = $.vakata.trim(v);
  2057. if(!v.length) { return true; }
  2058. data.li_attr[i] = v;
  2059. if(i === 'id') {
  2060. data.id = v.toString();
  2061. }
  2062. });
  2063. tmp = d.children('a').first();
  2064. if(tmp.length) {
  2065. tmp = $.vakata.attributes(tmp, true);
  2066. $.each(tmp, function (i, v) {
  2067. v = $.vakata.trim(v);
  2068. if(v.length) {
  2069. data.a_attr[i] = v;
  2070. }
  2071. });
  2072. }
  2073. tmp = d.children("a").first().length ? d.children("a").first().clone() : d.clone();
  2074. tmp.children("ins, i, ul").remove();
  2075. tmp = tmp.html();
  2076. tmp = $('<div></div>').html(tmp);
  2077. data.text = this.settings.core.force_text ? tmp.text() : tmp.html();
  2078. tmp = d.data();
  2079. data.data = tmp ? $.extend(true, {}, tmp) : null;
  2080. data.state.opened = d.hasClass('jstree-open');
  2081. data.state.selected = d.children('a').hasClass('jstree-clicked');
  2082. data.state.disabled = d.children('a').hasClass('jstree-disabled');
  2083. if(data.data && data.data.jstree) {
  2084. for(i in data.data.jstree) {
  2085. if(data.data.jstree.hasOwnProperty(i)) {
  2086. data.state[i] = data.data.jstree[i];
  2087. }
  2088. }
  2089. }
  2090. tmp = d.children("a").children(".jstree-themeicon");
  2091. if(tmp.length) {
  2092. data.icon = tmp.hasClass('jstree-themeicon-hidden') ? false : tmp.attr('rel');
  2093. }
  2094. if(data.state.icon !== undefined) {
  2095. data.icon = data.state.icon;
  2096. }
  2097. if(data.icon === undefined || data.icon === null || data.icon === "") {
  2098. data.icon = true;
  2099. }
  2100. tmp = d.children("ul").children("li");
  2101. do {
  2102. tid = 'j' + this._id + '_' + (++this._cnt);
  2103. } while(m[tid]);
  2104. data.id = data.li_attr.id ? data.li_attr.id.toString() : tid;
  2105. if(tmp.length) {
  2106. tmp.each(function (i, v) {
  2107. c = this._parse_model_from_html($(v), data.id, ps);
  2108. e = this._model.data[c];
  2109. data.children.push(c);
  2110. if(e.children_d.length) {
  2111. data.children_d = data.children_d.concat(e.children_d);
  2112. }
  2113. }.bind(this));
  2114. data.children_d = data.children_d.concat(data.children);
  2115. }
  2116. else {
  2117. if(d.hasClass('jstree-closed')) {
  2118. data.state.loaded = false;
  2119. }
  2120. }
  2121. if(data.li_attr['class']) {
  2122. data.li_attr['class'] = data.li_attr['class'].replace('jstree-closed','').replace('jstree-open','');
  2123. }
  2124. if(data.a_attr['class']) {
  2125. data.a_attr['class'] = data.a_attr['class'].replace('jstree-clicked','').replace('jstree-disabled','');
  2126. }
  2127. m[data.id] = data;
  2128. if(data.state.selected) {
  2129. this._data.core.selected.push(data.id);
  2130. }
  2131. return data.id;
  2132. },
  2133. /**
  2134. * parses a node from a JSON object (used when dealing with flat data, which has no nesting of children, but has id and parent properties) and appends it to the in memory tree model. Used internally.
  2135. * @private
  2136. * @name _parse_model_from_flat_json(d [, p, ps])
  2137. * @param {Object} d the JSON object to parse
  2138. * @param {String} p the parent ID
  2139. * @param {Array} ps list of all parents
  2140. * @return {String} the ID of the object added to the model
  2141. */
  2142. _parse_model_from_flat_json : function (d, p, ps) {
  2143. if(!ps) { ps = []; }
  2144. else { ps = ps.concat(); }
  2145. if(p) { ps.unshift(p); }
  2146. var tid = d.id.toString(),
  2147. m = this._model.data,
  2148. df = this._model.default_state,
  2149. i, j, c, e,
  2150. tmp = {
  2151. id : tid,
  2152. text : d.text || '',
  2153. icon : d.icon !== undefined ? d.icon : true,
  2154. parent : p,
  2155. parents : ps,
  2156. children : d.children || [],
  2157. children_d : d.children_d || [],
  2158. data : d.data,
  2159. state : { },
  2160. li_attr : { id : false },
  2161. a_attr : { href : '#' },
  2162. original : false
  2163. };
  2164. for(i in df) {
  2165. if(df.hasOwnProperty(i)) {
  2166. tmp.state[i] = df[i];
  2167. }
  2168. }
  2169. if(d && d.data && d.data.jstree && d.data.jstree.icon) {
  2170. tmp.icon = d.data.jstree.icon;
  2171. }
  2172. if(tmp.icon === undefined || tmp.icon === null || tmp.icon === "") {
  2173. tmp.icon = true;
  2174. }
  2175. if(d && d.data) {
  2176. tmp.data = d.data;
  2177. if(d.data.jstree) {
  2178. for(i in d.data.jstree) {
  2179. if(d.data.jstree.hasOwnProperty(i)) {
  2180. tmp.state[i] = d.data.jstree[i];
  2181. }
  2182. }
  2183. }
  2184. }
  2185. if(d && typeof d.state === 'object') {
  2186. for (i in d.state) {
  2187. if(d.state.hasOwnProperty(i)) {
  2188. tmp.state[i] = d.state[i];
  2189. }
  2190. }
  2191. }
  2192. if(d && typeof d.li_attr === 'object') {
  2193. for (i in d.li_attr) {
  2194. if(d.li_attr.hasOwnProperty(i)) {
  2195. tmp.li_attr[i] = d.li_attr[i];
  2196. }
  2197. }
  2198. }
  2199. if(!tmp.li_attr.id) {
  2200. tmp.li_attr.id = tid;
  2201. }
  2202. if(d && typeof d.a_attr === 'object') {
  2203. for (i in d.a_attr) {
  2204. if(d.a_attr.hasOwnProperty(i)) {
  2205. tmp.a_attr[i] = d.a_attr[i];
  2206. }
  2207. }
  2208. }
  2209. if(d && d.children && d.children === true) {
  2210. tmp.state.loaded = false;
  2211. tmp.children = [];
  2212. tmp.children_d = [];
  2213. }
  2214. m[tmp.id] = tmp;
  2215. for(i = 0, j = tmp.children.length; i < j; i++) {
  2216. c = this._parse_model_from_flat_json(m[tmp.children[i]], tmp.id, ps);
  2217. e = m[c];
  2218. tmp.children_d.push(c);
  2219. if(e.children_d.length) {
  2220. tmp.children_d = tmp.children_d.concat(e.children_d);
  2221. }
  2222. }
  2223. delete d.data;
  2224. delete d.children;
  2225. m[tmp.id].original = d;
  2226. if(tmp.state.selected) {
  2227. this._data.core.selected.push(tmp.id);
  2228. }
  2229. return tmp.id;
  2230. },
  2231. /**
  2232. * parses a node from a JSON object and appends it to the in memory tree model. Used internally.
  2233. * @private
  2234. * @name _parse_model_from_json(d [, p, ps])
  2235. * @param {Object} d the JSON object to parse
  2236. * @param {String} p the parent ID
  2237. * @param {Array} ps list of all parents
  2238. * @return {String} the ID of the object added to the model
  2239. */
  2240. _parse_model_from_json : function (d, p, ps) {
  2241. if(!ps) { ps = []; }
  2242. else { ps = ps.concat(); }
  2243. if(p) { ps.unshift(p); }
  2244. var tid = false, i, j, c, e, m = this._model.data, df = this._model.default_state, tmp;
  2245. do {
  2246. tid = 'j' + this._id + '_' + (++this._cnt);
  2247. } while(m[tid]);
  2248. tmp = {
  2249. id : false,
  2250. text : typeof d === 'string' ? d : '',
  2251. icon : typeof d === 'object' && d.icon !== undefined ? d.icon : true,
  2252. parent : p,
  2253. parents : ps,
  2254. children : [],
  2255. children_d : [],
  2256. data : null,
  2257. state : { },
  2258. li_attr : { id : false },
  2259. a_attr : { href : '#' },
  2260. original : false
  2261. };
  2262. for(i in df) {
  2263. if(df.hasOwnProperty(i)) {
  2264. tmp.state[i] = df[i];
  2265. }
  2266. }
  2267. if(d && (d.id || d.id === 0)) { tmp.id = d.id.toString(); }
  2268. if(d && d.text) { tmp.text = d.text; }
  2269. if(d && d.data && d.data.jstree && d.data.jstree.icon) {
  2270. tmp.icon = d.data.jstree.icon;
  2271. }
  2272. if(tmp.icon === undefined || tmp.icon === null || tmp.icon === "") {
  2273. tmp.icon = true;
  2274. }
  2275. if(d && d.data) {
  2276. tmp.data = d.data;
  2277. if(d.data.jstree) {
  2278. for(i in d.data.jstree) {
  2279. if(d.data.jstree.hasOwnProperty(i)) {
  2280. tmp.state[i] = d.data.jstree[i];
  2281. }
  2282. }
  2283. }
  2284. }
  2285. if(d && typeof d.state === 'object') {
  2286. for (i in d.state) {
  2287. if(d.state.hasOwnProperty(i)) {
  2288. tmp.state[i] = d.state[i];
  2289. }
  2290. }
  2291. }
  2292. if(d && typeof d.li_attr === 'object') {
  2293. for (i in d.li_attr) {
  2294. if(d.li_attr.hasOwnProperty(i)) {
  2295. tmp.li_attr[i] = d.li_attr[i];
  2296. }
  2297. }
  2298. }
  2299. if(tmp.li_attr.id && !(tmp.id || tmp.id === 0)) {
  2300. tmp.id = tmp.li_attr.id.toString();
  2301. }
  2302. if(!(tmp.id || tmp.id === 0)) {
  2303. tmp.id = tid;
  2304. }
  2305. if(!tmp.li_attr.id) {
  2306. tmp.li_attr.id = tmp.id;
  2307. }
  2308. if(d && typeof d.a_attr === 'object') {
  2309. for (i in d.a_attr) {
  2310. if(d.a_attr.hasOwnProperty(i)) {
  2311. tmp.a_attr[i] = d.a_attr[i];
  2312. }
  2313. }
  2314. }
  2315. if(d && d.children && d.children.length) {
  2316. for(i = 0, j = d.children.length; i < j; i++) {
  2317. c = this._parse_model_from_json(d.children[i], tmp.id, ps);
  2318. e = m[c];
  2319. tmp.children.push(c);
  2320. if(e.children_d.length) {
  2321. tmp.children_d = tmp.children_d.concat(e.children_d);
  2322. }
  2323. }
  2324. tmp.children_d = tmp.children.concat(tmp.children_d);
  2325. }
  2326. if(d && d.children && d.children === true) {
  2327. tmp.state.loaded = false;
  2328. tmp.children = [];
  2329. tmp.children_d = [];
  2330. }
  2331. delete d.data;
  2332. delete d.children;
  2333. tmp.original = d;
  2334. m[tmp.id] = tmp;
  2335. if(tmp.state.selected) {
  2336. this._data.core.selected.push(tmp.id);
  2337. }
  2338. return tmp.id;
  2339. },
  2340. /**
  2341. * redraws all nodes that need to be redrawn. Used internally.
  2342. * @private
  2343. * @name _redraw()
  2344. * @trigger redraw.jstree
  2345. */
  2346. _redraw : function () {
  2347. var nodes = this._model.force_full_redraw ? this._model.data[$.jstree.root].children.concat([]) : this._model.changed.concat([]),
  2348. f = document.createElement('UL'), tmp, i, j, fe = this._data.core.focused;
  2349. for(i = 0, j = nodes.length; i < j; i++) {
  2350. tmp = this.redraw_node(nodes[i], true, this._model.force_full_redraw);
  2351. if(tmp && this._model.force_full_redraw) {
  2352. f.appendChild(tmp);
  2353. }
  2354. }
  2355. if(this._model.force_full_redraw) {
  2356. f.className = this.get_container_ul()[0].className;
  2357. f.setAttribute('role','presentation');
  2358. this.element.empty().append(f);
  2359. //this.get_container_ul()[0].appendChild(f);
  2360. }
  2361. if(fe !== null && this.settings.core.restore_focus) {
  2362. tmp = this.get_node(fe, true);
  2363. if(tmp && tmp.length && tmp.children('.jstree-anchor')[0] !== document.activeElement) {
  2364. tmp.children('.jstree-anchor').trigger('focus');
  2365. }
  2366. else {
  2367. this._data.core.focused = null;
  2368. }
  2369. }
  2370. this._model.force_full_redraw = false;
  2371. this._model.changed = [];
  2372. /**
  2373. * triggered after nodes are redrawn
  2374. * @event
  2375. * @name redraw.jstree
  2376. * @param {array} nodes the redrawn nodes
  2377. */
  2378. this.trigger('redraw', { "nodes" : nodes });
  2379. },
  2380. /**
  2381. * redraws all nodes that need to be redrawn or optionally - the whole tree
  2382. * @name redraw([full])
  2383. * @param {Boolean} full if set to `true` all nodes are redrawn.
  2384. */
  2385. redraw : function (full) {
  2386. if(full) {
  2387. this._model.force_full_redraw = true;
  2388. }
  2389. //if(this._model.redraw_timeout) {
  2390. // clearTimeout(this._model.redraw_timeout);
  2391. //}
  2392. //this._model.redraw_timeout = setTimeout($.proxy(this._redraw, this),0);
  2393. this._redraw();
  2394. },
  2395. /**
  2396. * redraws a single node's children. Used internally.
  2397. * @private
  2398. * @name draw_children(node)
  2399. * @param {mixed} node the node whose children will be redrawn
  2400. */
  2401. draw_children : function (node) {
  2402. var obj = this.get_node(node),
  2403. i = false,
  2404. j = false,
  2405. k = false,
  2406. d = document;
  2407. if(!obj) { return false; }
  2408. if(obj.id === $.jstree.root) { return this.redraw(true); }
  2409. node = this.get_node(node, true);
  2410. if(!node || !node.length) { return false; } // TODO: quick toggle
  2411. node.children('.jstree-children').remove();
  2412. node = node[0];
  2413. if(obj.children.length && obj.state.loaded) {
  2414. k = d.createElement('UL');
  2415. k.setAttribute('role', 'group');
  2416. k.className = 'jstree-children';
  2417. for(i = 0, j = obj.children.length; i < j; i++) {
  2418. k.appendChild(this.redraw_node(obj.children[i], true, true));
  2419. }
  2420. node.appendChild(k);
  2421. }
  2422. },
  2423. /**
  2424. * redraws a single node. Used internally.
  2425. * @private
  2426. * @name redraw_node(node, deep, is_callback, force_render)
  2427. * @param {mixed} node the node to redraw
  2428. * @param {Boolean} deep should child nodes be redrawn too
  2429. * @param {Boolean} is_callback is this a recursion call
  2430. * @param {Boolean} force_render should children of closed parents be drawn anyway
  2431. */
  2432. redraw_node : function (node, deep, is_callback, force_render) {
  2433. var obj = this.get_node(node),
  2434. par = false,
  2435. ind = false,
  2436. old = false,
  2437. i = false,
  2438. j = false,
  2439. k = false,
  2440. c = '',
  2441. d = document,
  2442. m = this._model.data,
  2443. f = false,
  2444. s = false,
  2445. tmp = null,
  2446. t = 0,
  2447. l = 0,
  2448. has_children = false,
  2449. last_sibling = false;
  2450. if(!obj) { return false; }
  2451. if(obj.id === $.jstree.root) { return this.redraw(true); }
  2452. deep = deep || obj.children.length === 0;
  2453. node = !document.querySelector ? document.getElementById(obj.id) : this.element[0].querySelector('#' + ("0123456789".indexOf(obj.id[0]) !== -1 ? '\\3' + obj.id[0] + ' ' + obj.id.substr(1).replace($.jstree.idregex,'\\$&') : obj.id.replace($.jstree.idregex,'\\$&')) ); //, this.element);
  2454. if(!node) {
  2455. deep = true;
  2456. //node = d.createElement('LI');
  2457. if(!is_callback) {
  2458. par = obj.parent !== $.jstree.root ? $('#' + obj.parent.replace($.jstree.idregex,'\\$&'), this.element)[0] : null;
  2459. if(par !== null && (!par || !m[obj.parent].state.opened)) {
  2460. return false;
  2461. }
  2462. ind = $.inArray(obj.id, par === null ? m[$.jstree.root].children : m[obj.parent].children);
  2463. }
  2464. }
  2465. else {
  2466. node = $(node);
  2467. if(!is_callback) {
  2468. par = node.parent().parent()[0];
  2469. if(par === this.element[0]) {
  2470. par = null;
  2471. }
  2472. ind = node.index();
  2473. }
  2474. // m[obj.id].data = node.data(); // use only node's data, no need to touch jquery storage
  2475. if(!deep && obj.children.length && !node.children('.jstree-children').length) {
  2476. deep = true;
  2477. }
  2478. if(!deep) {
  2479. old = node.children('.jstree-children')[0];
  2480. }
  2481. f = node.children('.jstree-anchor')[0] === document.activeElement;
  2482. node.remove();
  2483. //node = d.createElement('LI');
  2484. //node = node[0];
  2485. }
  2486. node = this._data.core.node.cloneNode(true);
  2487. // node is DOM, deep is boolean
  2488. c = 'jstree-node ';
  2489. for(i in obj.li_attr) {
  2490. if(obj.li_attr.hasOwnProperty(i)) {
  2491. if(i === 'id') { continue; }
  2492. if(i !== 'class') {
  2493. node.setAttribute(i, obj.li_attr[i]);
  2494. }
  2495. else {
  2496. c += obj.li_attr[i];
  2497. }
  2498. }
  2499. }
  2500. if(!obj.a_attr.id) {
  2501. obj.a_attr.id = obj.id + '_anchor';
  2502. }
  2503. node.childNodes[1].setAttribute('aria-selected', !!obj.state.selected);
  2504. node.childNodes[1].setAttribute('aria-level', obj.parents.length);
  2505. if(this.settings.core.compute_elements_positions) {
  2506. node.childNodes[1].setAttribute('aria-setsize', m[obj.parent].children.length);
  2507. node.childNodes[1].setAttribute('aria-posinset', m[obj.parent].children.indexOf(obj.id) + 1);
  2508. }
  2509. if(obj.state.disabled) {
  2510. node.childNodes[1].setAttribute('aria-disabled', true);
  2511. }
  2512. for(i = 0, j = obj.children.length; i < j; i++) {
  2513. if(!m[obj.children[i]].state.hidden) {
  2514. has_children = true;
  2515. break;
  2516. }
  2517. }
  2518. if(obj.parent !== null && m[obj.parent] && !obj.state.hidden) {
  2519. i = $.inArray(obj.id, m[obj.parent].children);
  2520. last_sibling = obj.id;
  2521. if(i !== -1) {
  2522. i++;
  2523. for(j = m[obj.parent].children.length; i < j; i++) {
  2524. if(!m[m[obj.parent].children[i]].state.hidden) {
  2525. last_sibling = m[obj.parent].children[i];
  2526. }
  2527. if(last_sibling !== obj.id) {
  2528. break;
  2529. }
  2530. }
  2531. }
  2532. }
  2533. if(obj.state.hidden) {
  2534. c += ' jstree-hidden';
  2535. }
  2536. if (obj.state.loading) {
  2537. c += ' jstree-loading';
  2538. }
  2539. if(obj.state.loaded && !has_children) {
  2540. c += ' jstree-leaf';
  2541. }
  2542. else {
  2543. c += obj.state.opened && obj.state.loaded ? ' jstree-open' : ' jstree-closed';
  2544. node.childNodes[1].setAttribute('aria-expanded', (obj.state.opened && obj.state.loaded) );
  2545. }
  2546. if(last_sibling === obj.id) {
  2547. c += ' jstree-last';
  2548. }
  2549. node.id = obj.id;
  2550. node.className = c;
  2551. c = ( obj.state.selected ? ' jstree-clicked' : '') + ( obj.state.disabled ? ' jstree-disabled' : '');
  2552. for(j in obj.a_attr) {
  2553. if(obj.a_attr.hasOwnProperty(j)) {
  2554. if(j === 'href' && obj.a_attr[j] === '#') { continue; }
  2555. if(j !== 'class') {
  2556. node.childNodes[1].setAttribute(j, obj.a_attr[j]);
  2557. }
  2558. else {
  2559. c += ' ' + obj.a_attr[j];
  2560. }
  2561. }
  2562. }
  2563. if(c.length) {
  2564. node.childNodes[1].className = 'jstree-anchor ' + c;
  2565. }
  2566. if((obj.icon && obj.icon !== true) || obj.icon === false) {
  2567. if(obj.icon === false) {
  2568. node.childNodes[1].childNodes[0].className += ' jstree-themeicon-hidden';
  2569. }
  2570. else if(obj.icon.indexOf('/') === -1 && obj.icon.indexOf('.') === -1) {
  2571. node.childNodes[1].childNodes[0].className += ' ' + obj.icon + ' jstree-themeicon-custom';
  2572. }
  2573. else {
  2574. node.childNodes[1].childNodes[0].style.backgroundImage = 'url("'+obj.icon+'")';
  2575. node.childNodes[1].childNodes[0].style.backgroundPosition = 'center center';
  2576. node.childNodes[1].childNodes[0].style.backgroundSize = 'auto';
  2577. node.childNodes[1].childNodes[0].className += ' jstree-themeicon-custom';
  2578. }
  2579. }
  2580. if(this.settings.core.force_text) {
  2581. node.childNodes[1].appendChild(d.createTextNode(obj.text));
  2582. }
  2583. else {
  2584. node.childNodes[1].innerHTML += obj.text;
  2585. }
  2586. if(deep && obj.children.length && (obj.state.opened || force_render) && obj.state.loaded) {
  2587. k = d.createElement('UL');
  2588. k.setAttribute('role', 'group');
  2589. k.className = 'jstree-children';
  2590. for(i = 0, j = obj.children.length; i < j; i++) {
  2591. k.appendChild(this.redraw_node(obj.children[i], deep, true));
  2592. }
  2593. node.appendChild(k);
  2594. }
  2595. if(old) {
  2596. node.appendChild(old);
  2597. }
  2598. if(!is_callback) {
  2599. // append back using par / ind
  2600. if(!par) {
  2601. par = this.element[0];
  2602. }
  2603. for(i = 0, j = par.childNodes.length; i < j; i++) {
  2604. if(par.childNodes[i] && par.childNodes[i].className && par.childNodes[i].className.indexOf('jstree-children') !== -1) {
  2605. tmp = par.childNodes[i];
  2606. break;
  2607. }
  2608. }
  2609. if(!tmp) {
  2610. tmp = d.createElement('UL');
  2611. tmp.setAttribute('role', 'group');
  2612. tmp.className = 'jstree-children';
  2613. par.appendChild(tmp);
  2614. }
  2615. par = tmp;
  2616. if(ind < par.childNodes.length) {
  2617. par.insertBefore(node, par.childNodes[ind]);
  2618. }
  2619. else {
  2620. par.appendChild(node);
  2621. }
  2622. if(f) {
  2623. t = this.element[0].scrollTop;
  2624. l = this.element[0].scrollLeft;
  2625. node.childNodes[1].focus();
  2626. this.element[0].scrollTop = t;
  2627. this.element[0].scrollLeft = l;
  2628. }
  2629. }
  2630. if(obj.state.opened && !obj.state.loaded) {
  2631. obj.state.opened = false;
  2632. setTimeout(function () {
  2633. this.open_node(obj.id, false, 0);
  2634. }.bind(this), 0);
  2635. }
  2636. return node;
  2637. },
  2638. /**
  2639. * opens a node, revealing its children. If the node is not loaded it will be loaded and opened once ready.
  2640. * @name open_node(obj [, callback, animation])
  2641. * @param {mixed} obj the node to open
  2642. * @param {Function} callback a function to execute once the node is opened
  2643. * @param {Number} animation the animation duration in milliseconds when opening the node (overrides the `core.animation` setting). Use `false` for no animation.
  2644. * @trigger open_node.jstree, after_open.jstree, before_open.jstree
  2645. */
  2646. open_node : function (obj, callback, animation) {
  2647. var t1, t2, d, t;
  2648. if($.vakata.is_array(obj)) {
  2649. obj = obj.slice();
  2650. for(t1 = 0, t2 = obj.length; t1 < t2; t1++) {
  2651. this.open_node(obj[t1], callback, animation);
  2652. }
  2653. return true;
  2654. }
  2655. obj = this.get_node(obj);
  2656. if(!obj || obj.id === $.jstree.root) {
  2657. return false;
  2658. }
  2659. animation = animation === undefined ? this.settings.core.animation : animation;
  2660. if(!this.is_closed(obj)) {
  2661. if(callback) {
  2662. callback.call(this, obj, false);
  2663. }
  2664. return false;
  2665. }
  2666. if(!this.is_loaded(obj)) {
  2667. if(this.is_loading(obj)) {
  2668. return setTimeout(function () {
  2669. this.open_node(obj, callback, animation);
  2670. }.bind(this), 500);
  2671. }
  2672. this.load_node(obj, function (o, ok) {
  2673. return ok ? this.open_node(o, callback, animation) : (callback ? callback.call(this, o, false) : false);
  2674. });
  2675. }
  2676. else {
  2677. d = this.get_node(obj, true);
  2678. t = this;
  2679. if(d.length) {
  2680. if(animation && d.children(".jstree-children").length) {
  2681. d.children(".jstree-children").stop(true, true);
  2682. }
  2683. if(obj.children.length && !this._firstChild(d.children('.jstree-children')[0])) {
  2684. this.draw_children(obj);
  2685. //d = this.get_node(obj, true);
  2686. }
  2687. if(!animation) {
  2688. this.trigger('before_open', { "node" : obj });
  2689. d[0].className = d[0].className.replace('jstree-closed', 'jstree-open');
  2690. d[0].childNodes[1].setAttribute("aria-expanded", true);
  2691. }
  2692. else {
  2693. this.trigger('before_open', { "node" : obj });
  2694. d
  2695. .children(".jstree-children").css("display","none").end()
  2696. .removeClass("jstree-closed").addClass("jstree-open")
  2697. .children('.jstree-anchor').attr("aria-expanded", true).end()
  2698. .children(".jstree-children").stop(true, true)
  2699. .slideDown(animation, function () {
  2700. this.style.display = "";
  2701. if (t.element) {
  2702. t.trigger("after_open", { "node" : obj });
  2703. }
  2704. });
  2705. }
  2706. }
  2707. obj.state.opened = true;
  2708. if(callback) {
  2709. callback.call(this, obj, true);
  2710. }
  2711. if(!d.length) {
  2712. /**
  2713. * triggered when a node is about to be opened (if the node is supposed to be in the DOM, it will be, but it won't be visible yet)
  2714. * @event
  2715. * @name before_open.jstree
  2716. * @param {Object} node the opened node
  2717. */
  2718. this.trigger('before_open', { "node" : obj });
  2719. }
  2720. /**
  2721. * triggered when a node is opened (if there is an animation it will not be completed yet)
  2722. * @event
  2723. * @name open_node.jstree
  2724. * @param {Object} node the opened node
  2725. */
  2726. this.trigger('open_node', { "node" : obj });
  2727. if(!animation || !d.length) {
  2728. /**
  2729. * triggered when a node is opened and the animation is complete
  2730. * @event
  2731. * @name after_open.jstree
  2732. * @param {Object} node the opened node
  2733. */
  2734. this.trigger("after_open", { "node" : obj });
  2735. }
  2736. return true;
  2737. }
  2738. },
  2739. /**
  2740. * opens every parent of a node (node should be loaded)
  2741. * @name _open_to(obj)
  2742. * @param {mixed} obj the node to reveal
  2743. * @private
  2744. */
  2745. _open_to : function (obj) {
  2746. obj = this.get_node(obj);
  2747. if(!obj || obj.id === $.jstree.root) {
  2748. return false;
  2749. }
  2750. var i, j, p = obj.parents;
  2751. for(i = 0, j = p.length; i < j; i+=1) {
  2752. if(i !== $.jstree.root) {
  2753. this.open_node(p[i], false, 0);
  2754. }
  2755. }
  2756. return $('#' + obj.id.replace($.jstree.idregex,'\\$&'), this.element);
  2757. },
  2758. /**
  2759. * closes a node, hiding its children
  2760. * @name close_node(obj [, animation])
  2761. * @param {mixed} obj the node to close
  2762. * @param {Number} animation the animation duration in milliseconds when closing the node (overrides the `core.animation` setting). Use `false` for no animation.
  2763. * @trigger close_node.jstree, after_close.jstree
  2764. */
  2765. close_node : function (obj, animation) {
  2766. var t1, t2, t, d;
  2767. if($.vakata.is_array(obj)) {
  2768. obj = obj.slice();
  2769. for(t1 = 0, t2 = obj.length; t1 < t2; t1++) {
  2770. this.close_node(obj[t1], animation);
  2771. }
  2772. return true;
  2773. }
  2774. obj = this.get_node(obj);
  2775. if(!obj || obj.id === $.jstree.root) {
  2776. return false;
  2777. }
  2778. if(this.is_closed(obj)) {
  2779. return false;
  2780. }
  2781. animation = animation === undefined ? this.settings.core.animation : animation;
  2782. t = this;
  2783. d = this.get_node(obj, true);
  2784. obj.state.opened = false;
  2785. /**
  2786. * triggered when a node is closed (if there is an animation it will not be complete yet)
  2787. * @event
  2788. * @name close_node.jstree
  2789. * @param {Object} node the closed node
  2790. */
  2791. this.trigger('close_node',{ "node" : obj });
  2792. if(!d.length) {
  2793. /**
  2794. * triggered when a node is closed and the animation is complete
  2795. * @event
  2796. * @name after_close.jstree
  2797. * @param {Object} node the closed node
  2798. */
  2799. this.trigger("after_close", { "node" : obj });
  2800. }
  2801. else {
  2802. if(!animation) {
  2803. d[0].className = d[0].className.replace('jstree-open', 'jstree-closed');
  2804. d.children('.jstree-anchor').attr("aria-expanded", false);
  2805. d.children('.jstree-children').remove();
  2806. this.trigger("after_close", { "node" : obj });
  2807. }
  2808. else {
  2809. d
  2810. .children(".jstree-children").attr("style","display:block !important").end()
  2811. .removeClass("jstree-open").addClass("jstree-closed")
  2812. .children('.jstree-anchor').attr("aria-expanded", false).end()
  2813. .children(".jstree-children").stop(true, true).slideUp(animation, function () {
  2814. this.style.display = "";
  2815. d.children('.jstree-children').remove();
  2816. if (t.element) {
  2817. t.trigger("after_close", { "node" : obj });
  2818. }
  2819. });
  2820. }
  2821. }
  2822. },
  2823. /**
  2824. * toggles a node - closing it if it is open, opening it if it is closed
  2825. * @name toggle_node(obj)
  2826. * @param {mixed} obj the node to toggle
  2827. */
  2828. toggle_node : function (obj) {
  2829. var t1, t2;
  2830. if($.vakata.is_array(obj)) {
  2831. obj = obj.slice();
  2832. for(t1 = 0, t2 = obj.length; t1 < t2; t1++) {
  2833. this.toggle_node(obj[t1]);
  2834. }
  2835. return true;
  2836. }
  2837. if(this.is_closed(obj)) {
  2838. return this.open_node(obj);
  2839. }
  2840. if(this.is_open(obj)) {
  2841. return this.close_node(obj);
  2842. }
  2843. },
  2844. /**
  2845. * opens all nodes within a node (or the tree), revealing their children. If the node is not loaded it will be loaded and opened once ready.
  2846. * @name open_all([obj, animation, original_obj])
  2847. * @param {mixed} obj the node to open recursively, omit to open all nodes in the tree
  2848. * @param {Number} animation the animation duration in milliseconds when opening the nodes, the default is no animation
  2849. * @param {jQuery} reference to the node that started the process (internal use)
  2850. * @trigger open_all.jstree
  2851. */
  2852. open_all : function (obj, animation, original_obj) {
  2853. if(!obj) { obj = $.jstree.root; }
  2854. obj = this.get_node(obj);
  2855. if(!obj) { return false; }
  2856. var dom = obj.id === $.jstree.root ? this.get_container_ul() : this.get_node(obj, true), i, j, _this;
  2857. if(!dom.length) {
  2858. for(i = 0, j = obj.children_d.length; i < j; i++) {
  2859. if(this.is_closed(this._model.data[obj.children_d[i]])) {
  2860. this._model.data[obj.children_d[i]].state.opened = true;
  2861. }
  2862. }
  2863. return this.trigger('open_all', { "node" : obj });
  2864. }
  2865. original_obj = original_obj || dom;
  2866. _this = this;
  2867. dom = this.is_closed(obj) ? dom.find('.jstree-closed').addBack() : dom.find('.jstree-closed');
  2868. dom.each(function () {
  2869. _this.open_node(
  2870. this,
  2871. function(node, status) { if(status && this.is_parent(node)) { this.open_all(node, animation, original_obj); } },
  2872. animation || 0
  2873. );
  2874. });
  2875. if(original_obj.find('.jstree-closed').length === 0) {
  2876. /**
  2877. * triggered when an `open_all` call completes
  2878. * @event
  2879. * @name open_all.jstree
  2880. * @param {Object} node the opened node
  2881. */
  2882. this.trigger('open_all', { "node" : this.get_node(original_obj) });
  2883. }
  2884. },
  2885. /**
  2886. * closes all nodes within a node (or the tree), revealing their children
  2887. * @name close_all([obj, animation])
  2888. * @param {mixed} obj the node to close recursively, omit to close all nodes in the tree
  2889. * @param {Number} animation the animation duration in milliseconds when closing the nodes, the default is no animation
  2890. * @trigger close_all.jstree
  2891. */
  2892. close_all : function (obj, animation) {
  2893. if(!obj) { obj = $.jstree.root; }
  2894. obj = this.get_node(obj);
  2895. if(!obj) { return false; }
  2896. var dom = obj.id === $.jstree.root ? this.get_container_ul() : this.get_node(obj, true),
  2897. _this = this, i, j;
  2898. if(dom.length) {
  2899. dom = this.is_open(obj) ? dom.find('.jstree-open').addBack() : dom.find('.jstree-open');
  2900. $(dom.get().reverse()).each(function () { _this.close_node(this, animation || 0); });
  2901. }
  2902. for(i = 0, j = obj.children_d.length; i < j; i++) {
  2903. this._model.data[obj.children_d[i]].state.opened = false;
  2904. }
  2905. /**
  2906. * triggered when an `close_all` call completes
  2907. * @event
  2908. * @name close_all.jstree
  2909. * @param {Object} node the closed node
  2910. */
  2911. this.trigger('close_all', { "node" : obj });
  2912. },
  2913. /**
  2914. * checks if a node is disabled (not selectable)
  2915. * @name is_disabled(obj)
  2916. * @param {mixed} obj
  2917. * @return {Boolean}
  2918. */
  2919. is_disabled : function (obj) {
  2920. obj = this.get_node(obj);
  2921. return obj && obj.state && obj.state.disabled;
  2922. },
  2923. /**
  2924. * enables a node - so that it can be selected
  2925. * @name enable_node(obj)
  2926. * @param {mixed} obj the node to enable
  2927. * @trigger enable_node.jstree
  2928. */
  2929. enable_node : function (obj) {
  2930. var t1, t2;
  2931. if($.vakata.is_array(obj)) {
  2932. obj = obj.slice();
  2933. for(t1 = 0, t2 = obj.length; t1 < t2; t1++) {
  2934. this.enable_node(obj[t1]);
  2935. }
  2936. return true;
  2937. }
  2938. obj = this.get_node(obj);
  2939. if(!obj || obj.id === $.jstree.root) {
  2940. return false;
  2941. }
  2942. obj.state.disabled = false;
  2943. this.get_node(obj,true).children('.jstree-anchor').removeClass('jstree-disabled').attr('aria-disabled', false);
  2944. /**
  2945. * triggered when an node is enabled
  2946. * @event
  2947. * @name enable_node.jstree
  2948. * @param {Object} node the enabled node
  2949. */
  2950. this.trigger('enable_node', { 'node' : obj });
  2951. },
  2952. /**
  2953. * disables a node - so that it can not be selected
  2954. * @name disable_node(obj)
  2955. * @param {mixed} obj the node to disable
  2956. * @trigger disable_node.jstree
  2957. */
  2958. disable_node : function (obj) {
  2959. var t1, t2;
  2960. if($.vakata.is_array(obj)) {
  2961. obj = obj.slice();
  2962. for(t1 = 0, t2 = obj.length; t1 < t2; t1++) {
  2963. this.disable_node(obj[t1]);
  2964. }
  2965. return true;
  2966. }
  2967. obj = this.get_node(obj);
  2968. if(!obj || obj.id === $.jstree.root) {
  2969. return false;
  2970. }
  2971. obj.state.disabled = true;
  2972. this.get_node(obj,true).children('.jstree-anchor').addClass('jstree-disabled').attr('aria-disabled', true);
  2973. /**
  2974. * triggered when an node is disabled
  2975. * @event
  2976. * @name disable_node.jstree
  2977. * @param {Object} node the disabled node
  2978. */
  2979. this.trigger('disable_node', { 'node' : obj });
  2980. },
  2981. /**
  2982. * determines if a node is hidden
  2983. * @name is_hidden(obj)
  2984. * @param {mixed} obj the node
  2985. */
  2986. is_hidden : function (obj) {
  2987. obj = this.get_node(obj);
  2988. return obj.state.hidden === true;
  2989. },
  2990. /**
  2991. * hides a node - it is still in the structure but will not be visible
  2992. * @name hide_node(obj)
  2993. * @param {mixed} obj the node to hide
  2994. * @param {Boolean} skip_redraw internal parameter controlling if redraw is called
  2995. * @trigger hide_node.jstree
  2996. */
  2997. hide_node : function (obj, skip_redraw) {
  2998. var t1, t2;
  2999. if($.vakata.is_array(obj)) {
  3000. obj = obj.slice();
  3001. for(t1 = 0, t2 = obj.length; t1 < t2; t1++) {
  3002. this.hide_node(obj[t1], true);
  3003. }
  3004. if (!skip_redraw) {
  3005. this.redraw();
  3006. }
  3007. return true;
  3008. }
  3009. obj = this.get_node(obj);
  3010. if(!obj || obj.id === $.jstree.root) {
  3011. return false;
  3012. }
  3013. if(!obj.state.hidden) {
  3014. obj.state.hidden = true;
  3015. this._node_changed(obj.parent);
  3016. if(!skip_redraw) {
  3017. this.redraw();
  3018. }
  3019. /**
  3020. * triggered when an node is hidden
  3021. * @event
  3022. * @name hide_node.jstree
  3023. * @param {Object} node the hidden node
  3024. */
  3025. this.trigger('hide_node', { 'node' : obj });
  3026. }
  3027. },
  3028. /**
  3029. * shows a node
  3030. * @name show_node(obj)
  3031. * @param {mixed} obj the node to show
  3032. * @param {Boolean} skip_redraw internal parameter controlling if redraw is called
  3033. * @trigger show_node.jstree
  3034. */
  3035. show_node : function (obj, skip_redraw) {
  3036. var t1, t2;
  3037. if($.vakata.is_array(obj)) {
  3038. obj = obj.slice();
  3039. for(t1 = 0, t2 = obj.length; t1 < t2; t1++) {
  3040. this.show_node(obj[t1], true);
  3041. }
  3042. if (!skip_redraw) {
  3043. this.redraw();
  3044. }
  3045. return true;
  3046. }
  3047. obj = this.get_node(obj);
  3048. if(!obj || obj.id === $.jstree.root) {
  3049. return false;
  3050. }
  3051. if(obj.state.hidden) {
  3052. obj.state.hidden = false;
  3053. this._node_changed(obj.parent);
  3054. if(!skip_redraw) {
  3055. this.redraw();
  3056. }
  3057. /**
  3058. * triggered when an node is shown
  3059. * @event
  3060. * @name show_node.jstree
  3061. * @param {Object} node the shown node
  3062. */
  3063. this.trigger('show_node', { 'node' : obj });
  3064. }
  3065. },
  3066. /**
  3067. * hides all nodes
  3068. * @name hide_all()
  3069. * @trigger hide_all.jstree
  3070. */
  3071. hide_all : function (skip_redraw) {
  3072. var i, m = this._model.data, ids = [];
  3073. for(i in m) {
  3074. if(m.hasOwnProperty(i) && i !== $.jstree.root && !m[i].state.hidden) {
  3075. m[i].state.hidden = true;
  3076. ids.push(i);
  3077. }
  3078. }
  3079. this._model.force_full_redraw = true;
  3080. if(!skip_redraw) {
  3081. this.redraw();
  3082. }
  3083. /**
  3084. * triggered when all nodes are hidden
  3085. * @event
  3086. * @name hide_all.jstree
  3087. * @param {Array} nodes the IDs of all hidden nodes
  3088. */
  3089. this.trigger('hide_all', { 'nodes' : ids });
  3090. return ids;
  3091. },
  3092. /**
  3093. * shows all nodes
  3094. * @name show_all()
  3095. * @trigger show_all.jstree
  3096. */
  3097. show_all : function (skip_redraw) {
  3098. var i, m = this._model.data, ids = [];
  3099. for(i in m) {
  3100. if(m.hasOwnProperty(i) && i !== $.jstree.root && m[i].state.hidden) {
  3101. m[i].state.hidden = false;
  3102. ids.push(i);
  3103. }
  3104. }
  3105. this._model.force_full_redraw = true;
  3106. if(!skip_redraw) {
  3107. this.redraw();
  3108. }
  3109. /**
  3110. * triggered when all nodes are shown
  3111. * @event
  3112. * @name show_all.jstree
  3113. * @param {Array} nodes the IDs of all shown nodes
  3114. */
  3115. this.trigger('show_all', { 'nodes' : ids });
  3116. return ids;
  3117. },
  3118. /**
  3119. * called when a node is selected by the user. Used internally.
  3120. * @private
  3121. * @name activate_node(obj, e)
  3122. * @param {mixed} obj the node
  3123. * @param {Object} e the related event
  3124. * @trigger activate_node.jstree, changed.jstree
  3125. */
  3126. activate_node : function (obj, e) {
  3127. if(this.is_disabled(obj)) {
  3128. return false;
  3129. }
  3130. if(!e || typeof e !== 'object') {
  3131. e = {};
  3132. }
  3133. // ensure last_clicked is still in the DOM, make it fresh (maybe it was moved?) and make sure it is still selected, if not - make last_clicked the last selected node
  3134. this._data.core.last_clicked = this._data.core.last_clicked && this._data.core.last_clicked.id !== undefined ? this.get_node(this._data.core.last_clicked.id) : null;
  3135. if(this._data.core.last_clicked && !this._data.core.last_clicked.state.selected) { this._data.core.last_clicked = null; }
  3136. if(!this._data.core.last_clicked && this._data.core.selected.length) { this._data.core.last_clicked = this.get_node(this._data.core.selected[this._data.core.selected.length - 1]); }
  3137. if(!this.settings.core.multiple || (!e.metaKey && !e.ctrlKey && !e.shiftKey) || (e.shiftKey && (!this._data.core.last_clicked || !this.get_parent(obj) || this.get_parent(obj) !== this._data.core.last_clicked.parent ) )) {
  3138. if(!this.settings.core.multiple && (e.metaKey || e.ctrlKey || e.shiftKey) && this.is_selected(obj)) {
  3139. this.deselect_node(obj, false, e);
  3140. }
  3141. else {
  3142. if (this.settings.core.allow_reselect || !this.is_selected(obj) || this._data.core.selected.length !== 1) {
  3143. this.deselect_all(true);
  3144. this.select_node(obj, false, false, e);
  3145. }
  3146. this._data.core.last_clicked = this.get_node(obj);
  3147. }
  3148. }
  3149. else {
  3150. if(e.shiftKey) {
  3151. var o = this.get_node(obj).id,
  3152. l = this._data.core.last_clicked.id,
  3153. p = this.get_node(this._data.core.last_clicked.parent).children,
  3154. c = false,
  3155. i, j;
  3156. for(i = 0, j = p.length; i < j; i += 1) {
  3157. // separate IFs work whem o and l are the same
  3158. if(p[i] === o) {
  3159. c = !c;
  3160. }
  3161. if(p[i] === l) {
  3162. c = !c;
  3163. }
  3164. if(!this.is_disabled(p[i]) && (c || p[i] === o || p[i] === l)) {
  3165. if (!this.is_hidden(p[i])) {
  3166. this.select_node(p[i], true, false, e);
  3167. }
  3168. }
  3169. else {
  3170. if (!e.ctrlKey) {
  3171. this.deselect_node(p[i], true, e);
  3172. }
  3173. }
  3174. }
  3175. this.trigger('changed', { 'action' : 'select_node', 'node' : this.get_node(obj), 'selected' : this._data.core.selected, 'event' : e });
  3176. }
  3177. else {
  3178. if(!this.is_selected(obj)) {
  3179. if (e.ctrlKey) {
  3180. this._data.core.last_clicked = this.get_node(obj);
  3181. }
  3182. this.select_node(obj, false, false, e);
  3183. }
  3184. else {
  3185. this.deselect_node(obj, false, e);
  3186. }
  3187. }
  3188. }
  3189. /**
  3190. * triggered when an node is clicked or intercated with by the user
  3191. * @event
  3192. * @name activate_node.jstree
  3193. * @param {Object} node
  3194. * @param {Object} event the ooriginal event (if any) which triggered the call (may be an empty object)
  3195. */
  3196. this.trigger('activate_node', { 'node' : this.get_node(obj), 'event' : e });
  3197. },
  3198. /**
  3199. * applies the hover state on a node, called when a node is hovered by the user. Used internally.
  3200. * @private
  3201. * @name hover_node(obj)
  3202. * @param {mixed} obj
  3203. * @trigger hover_node.jstree
  3204. */
  3205. hover_node : function (obj) {
  3206. obj = this.get_node(obj, true);
  3207. if(!obj || !obj.length || obj.children('.jstree-hovered').length) {
  3208. return false;
  3209. }
  3210. var o = this.element.find('.jstree-hovered'), t = this.element;
  3211. if(o && o.length) { this.dehover_node(o); }
  3212. obj.children('.jstree-anchor').addClass('jstree-hovered');
  3213. /**
  3214. * triggered when an node is hovered
  3215. * @event
  3216. * @name hover_node.jstree
  3217. * @param {Object} node
  3218. */
  3219. this.trigger('hover_node', { 'node' : this.get_node(obj) });
  3220. setTimeout(function () { t.attr('aria-activedescendant', obj[0].id); }, 0);
  3221. },
  3222. /**
  3223. * removes the hover state from a nodecalled when a node is no longer hovered by the user. Used internally.
  3224. * @private
  3225. * @name dehover_node(obj)
  3226. * @param {mixed} obj
  3227. * @trigger dehover_node.jstree
  3228. */
  3229. dehover_node : function (obj) {
  3230. obj = this.get_node(obj, true);
  3231. if(!obj || !obj.length || !obj.children('.jstree-hovered').length) {
  3232. return false;
  3233. }
  3234. obj.children('.jstree-anchor').removeClass('jstree-hovered');
  3235. /**
  3236. * triggered when an node is no longer hovered
  3237. * @event
  3238. * @name dehover_node.jstree
  3239. * @param {Object} node
  3240. */
  3241. this.trigger('dehover_node', { 'node' : this.get_node(obj) });
  3242. },
  3243. /**
  3244. * select a node
  3245. * @name select_node(obj [, supress_event, prevent_open])
  3246. * @param {mixed} obj an array can be used to select multiple nodes
  3247. * @param {Boolean} supress_event if set to `true` the `changed.jstree` event won't be triggered
  3248. * @param {Boolean} prevent_open if set to `true` parents of the selected node won't be opened
  3249. * @trigger select_node.jstree, changed.jstree
  3250. */
  3251. select_node : function (obj, supress_event, prevent_open, e) {
  3252. var dom, t1, t2, th;
  3253. if($.vakata.is_array(obj)) {
  3254. obj = obj.slice();
  3255. for(t1 = 0, t2 = obj.length; t1 < t2; t1++) {
  3256. this.select_node(obj[t1], supress_event, prevent_open, e);
  3257. }
  3258. return true;
  3259. }
  3260. obj = this.get_node(obj);
  3261. if(!obj || obj.id === $.jstree.root) {
  3262. return false;
  3263. }
  3264. dom = this.get_node(obj, true);
  3265. if(!obj.state.selected) {
  3266. obj.state.selected = true;
  3267. this._data.core.selected.push(obj.id);
  3268. if(!prevent_open) {
  3269. dom = this._open_to(obj);
  3270. }
  3271. if(dom && dom.length) {
  3272. dom.children('.jstree-anchor').addClass('jstree-clicked').attr('aria-selected', true);
  3273. }
  3274. /**
  3275. * triggered when an node is selected
  3276. * @event
  3277. * @name select_node.jstree
  3278. * @param {Object} node
  3279. * @param {Array} selected the current selection
  3280. * @param {Object} event the event (if any) that triggered this select_node
  3281. */
  3282. this.trigger('select_node', { 'node' : obj, 'selected' : this._data.core.selected, 'event' : e });
  3283. if(!supress_event) {
  3284. /**
  3285. * triggered when selection changes
  3286. * @event
  3287. * @name changed.jstree
  3288. * @param {Object} node
  3289. * @param {Object} action the action that caused the selection to change
  3290. * @param {Array} selected the current selection
  3291. * @param {Object} event the event (if any) that triggered this changed event
  3292. */
  3293. this.trigger('changed', { 'action' : 'select_node', 'node' : obj, 'selected' : this._data.core.selected, 'event' : e });
  3294. }
  3295. }
  3296. },
  3297. /**
  3298. * deselect a node
  3299. * @name deselect_node(obj [, supress_event])
  3300. * @param {mixed} obj an array can be used to deselect multiple nodes
  3301. * @param {Boolean} supress_event if set to `true` the `changed.jstree` event won't be triggered
  3302. * @trigger deselect_node.jstree, changed.jstree
  3303. */
  3304. deselect_node : function (obj, supress_event, e) {
  3305. var t1, t2, dom;
  3306. if($.vakata.is_array(obj)) {
  3307. obj = obj.slice();
  3308. for(t1 = 0, t2 = obj.length; t1 < t2; t1++) {
  3309. this.deselect_node(obj[t1], supress_event, e);
  3310. }
  3311. return true;
  3312. }
  3313. obj = this.get_node(obj);
  3314. if(!obj || obj.id === $.jstree.root) {
  3315. return false;
  3316. }
  3317. dom = this.get_node(obj, true);
  3318. if(obj.state.selected) {
  3319. obj.state.selected = false;
  3320. this._data.core.selected = $.vakata.array_remove_item(this._data.core.selected, obj.id);
  3321. if(dom.length) {
  3322. dom.children('.jstree-anchor').removeClass('jstree-clicked').attr('aria-selected', false);
  3323. }
  3324. /**
  3325. * triggered when an node is deselected
  3326. * @event
  3327. * @name deselect_node.jstree
  3328. * @param {Object} node
  3329. * @param {Array} selected the current selection
  3330. * @param {Object} event the event (if any) that triggered this deselect_node
  3331. */
  3332. this.trigger('deselect_node', { 'node' : obj, 'selected' : this._data.core.selected, 'event' : e });
  3333. if(!supress_event) {
  3334. this.trigger('changed', { 'action' : 'deselect_node', 'node' : obj, 'selected' : this._data.core.selected, 'event' : e });
  3335. }
  3336. }
  3337. },
  3338. /**
  3339. * select all nodes in the tree
  3340. * @name select_all([supress_event])
  3341. * @param {Boolean} supress_event if set to `true` the `changed.jstree` event won't be triggered
  3342. * @trigger select_all.jstree, changed.jstree
  3343. */
  3344. select_all : function (supress_event) {
  3345. var tmp = this._data.core.selected.concat([]), i, j;
  3346. this._data.core.selected = this._model.data[$.jstree.root].children_d.concat();
  3347. for(i = 0, j = this._data.core.selected.length; i < j; i++) {
  3348. if(this._model.data[this._data.core.selected[i]]) {
  3349. this._model.data[this._data.core.selected[i]].state.selected = true;
  3350. }
  3351. }
  3352. this.redraw(true);
  3353. /**
  3354. * triggered when all nodes are selected
  3355. * @event
  3356. * @name select_all.jstree
  3357. * @param {Array} selected the current selection
  3358. */
  3359. this.trigger('select_all', { 'selected' : this._data.core.selected });
  3360. if(!supress_event) {
  3361. this.trigger('changed', { 'action' : 'select_all', 'selected' : this._data.core.selected, 'old_selection' : tmp });
  3362. }
  3363. },
  3364. /**
  3365. * deselect all selected nodes
  3366. * @name deselect_all([supress_event])
  3367. * @param {Boolean} supress_event if set to `true` the `changed.jstree` event won't be triggered
  3368. * @trigger deselect_all.jstree, changed.jstree
  3369. */
  3370. deselect_all : function (supress_event) {
  3371. var tmp = this._data.core.selected.concat([]), i, j;
  3372. for(i = 0, j = this._data.core.selected.length; i < j; i++) {
  3373. if(this._model.data[this._data.core.selected[i]]) {
  3374. this._model.data[this._data.core.selected[i]].state.selected = false;
  3375. }
  3376. }
  3377. this._data.core.selected = [];
  3378. this.element.find('.jstree-clicked').removeClass('jstree-clicked').attr('aria-selected', false);
  3379. /**
  3380. * triggered when all nodes are deselected
  3381. * @event
  3382. * @name deselect_all.jstree
  3383. * @param {Object} node the previous selection
  3384. * @param {Array} selected the current selection
  3385. */
  3386. this.trigger('deselect_all', { 'selected' : this._data.core.selected, 'node' : tmp });
  3387. if(!supress_event) {
  3388. this.trigger('changed', { 'action' : 'deselect_all', 'selected' : this._data.core.selected, 'old_selection' : tmp });
  3389. }
  3390. },
  3391. /**
  3392. * checks if a node is selected
  3393. * @name is_selected(obj)
  3394. * @param {mixed} obj
  3395. * @return {Boolean}
  3396. */
  3397. is_selected : function (obj) {
  3398. obj = this.get_node(obj);
  3399. if(!obj || obj.id === $.jstree.root) {
  3400. return false;
  3401. }
  3402. return obj.state.selected;
  3403. },
  3404. /**
  3405. * get an array of all selected nodes
  3406. * @name get_selected([full])
  3407. * @param {mixed} full if set to `true` the returned array will consist of the full node objects, otherwise - only IDs will be returned
  3408. * @return {Array}
  3409. */
  3410. get_selected : function (full) {
  3411. return full ? $.map(this._data.core.selected, function (i) { return this.get_node(i); }.bind(this)) : this._data.core.selected.slice();
  3412. },
  3413. /**
  3414. * get an array of all top level selected nodes (ignoring children of selected nodes)
  3415. * @name get_top_selected([full])
  3416. * @param {mixed} full if set to `true` the returned array will consist of the full node objects, otherwise - only IDs will be returned
  3417. * @return {Array}
  3418. */
  3419. get_top_selected : function (full) {
  3420. var tmp = this.get_selected(true),
  3421. obj = {}, i, j, k, l;
  3422. for(i = 0, j = tmp.length; i < j; i++) {
  3423. obj[tmp[i].id] = tmp[i];
  3424. }
  3425. for(i = 0, j = tmp.length; i < j; i++) {
  3426. for(k = 0, l = tmp[i].children_d.length; k < l; k++) {
  3427. if(obj[tmp[i].children_d[k]]) {
  3428. delete obj[tmp[i].children_d[k]];
  3429. }
  3430. }
  3431. }
  3432. tmp = [];
  3433. for(i in obj) {
  3434. if(obj.hasOwnProperty(i)) {
  3435. tmp.push(i);
  3436. }
  3437. }
  3438. return full ? $.map(tmp, function (i) { return this.get_node(i); }.bind(this)) : tmp;
  3439. },
  3440. /**
  3441. * get an array of all bottom level selected nodes (ignoring selected parents)
  3442. * @name get_bottom_selected([full])
  3443. * @param {mixed} full if set to `true` the returned array will consist of the full node objects, otherwise - only IDs will be returned
  3444. * @return {Array}
  3445. */
  3446. get_bottom_selected : function (full) {
  3447. var tmp = this.get_selected(true),
  3448. obj = [], i, j;
  3449. for(i = 0, j = tmp.length; i < j; i++) {
  3450. if(!tmp[i].children.length) {
  3451. obj.push(tmp[i].id);
  3452. }
  3453. }
  3454. return full ? $.map(obj, function (i) { return this.get_node(i); }.bind(this)) : obj;
  3455. },
  3456. /**
  3457. * gets the current state of the tree so that it can be restored later with `set_state(state)`. Used internally.
  3458. * @name get_state()
  3459. * @private
  3460. * @return {Object}
  3461. */
  3462. get_state : function () {
  3463. var state = {
  3464. 'core' : {
  3465. 'open' : [],
  3466. 'loaded' : [],
  3467. 'scroll' : {
  3468. 'left' : this.element.scrollLeft(),
  3469. 'top' : this.element.scrollTop()
  3470. },
  3471. /*!
  3472. 'themes' : {
  3473. 'name' : this.get_theme(),
  3474. 'icons' : this._data.core.themes.icons,
  3475. 'dots' : this._data.core.themes.dots
  3476. },
  3477. */
  3478. 'selected' : []
  3479. }
  3480. }, i;
  3481. for(i in this._model.data) {
  3482. if(this._model.data.hasOwnProperty(i)) {
  3483. if(i !== $.jstree.root) {
  3484. if(this._model.data[i].state.loaded && this.settings.core.loaded_state) {
  3485. state.core.loaded.push(i);
  3486. }
  3487. if(this._model.data[i].state.opened) {
  3488. state.core.open.push(i);
  3489. }
  3490. if(this._model.data[i].state.selected) {
  3491. state.core.selected.push(i);
  3492. }
  3493. }
  3494. }
  3495. }
  3496. return state;
  3497. },
  3498. /**
  3499. * sets the state of the tree. Used internally.
  3500. * @name set_state(state [, callback])
  3501. * @private
  3502. * @param {Object} state the state to restore. Keep in mind this object is passed by reference and jstree will modify it.
  3503. * @param {Function} callback an optional function to execute once the state is restored.
  3504. * @trigger set_state.jstree
  3505. */
  3506. set_state : function (state, callback) {
  3507. if(state) {
  3508. if(state.core && state.core.selected && state.core.initial_selection === undefined) {
  3509. state.core.initial_selection = this._data.core.selected.concat([]).sort().join(',');
  3510. }
  3511. if(state.core) {
  3512. var res, n, t, _this, i;
  3513. if(state.core.loaded) {
  3514. if(!this.settings.core.loaded_state || !$.vakata.is_array(state.core.loaded) || !state.core.loaded.length) {
  3515. delete state.core.loaded;
  3516. this.set_state(state, callback);
  3517. }
  3518. else {
  3519. this._load_nodes(state.core.loaded, function (nodes) {
  3520. delete state.core.loaded;
  3521. this.set_state(state, callback);
  3522. });
  3523. }
  3524. return false;
  3525. }
  3526. if(state.core.open) {
  3527. if(!$.vakata.is_array(state.core.open) || !state.core.open.length) {
  3528. delete state.core.open;
  3529. this.set_state(state, callback);
  3530. }
  3531. else {
  3532. this._load_nodes(state.core.open, function (nodes) {
  3533. this.open_node(nodes, false, 0);
  3534. delete state.core.open;
  3535. this.set_state(state, callback);
  3536. });
  3537. }
  3538. return false;
  3539. }
  3540. if(state.core.scroll) {
  3541. if(state.core.scroll && state.core.scroll.left !== undefined) {
  3542. this.element.scrollLeft(state.core.scroll.left);
  3543. }
  3544. if(state.core.scroll && state.core.scroll.top !== undefined) {
  3545. this.element.scrollTop(state.core.scroll.top);
  3546. }
  3547. delete state.core.scroll;
  3548. this.set_state(state, callback);
  3549. return false;
  3550. }
  3551. if(state.core.selected) {
  3552. _this = this;
  3553. if (state.core.initial_selection === undefined ||
  3554. state.core.initial_selection === this._data.core.selected.concat([]).sort().join(',')
  3555. ) {
  3556. this.deselect_all();
  3557. $.each(state.core.selected, function (i, v) {
  3558. _this.select_node(v, false, true);
  3559. });
  3560. }
  3561. delete state.core.initial_selection;
  3562. delete state.core.selected;
  3563. this.set_state(state, callback);
  3564. return false;
  3565. }
  3566. for(i in state) {
  3567. if(state.hasOwnProperty(i) && i !== "core" && $.inArray(i, this.settings.plugins) === -1) {
  3568. delete state[i];
  3569. }
  3570. }
  3571. if($.isEmptyObject(state.core)) {
  3572. delete state.core;
  3573. this.set_state(state, callback);
  3574. return false;
  3575. }
  3576. }
  3577. if($.isEmptyObject(state)) {
  3578. state = null;
  3579. if(callback) { callback.call(this); }
  3580. /**
  3581. * triggered when a `set_state` call completes
  3582. * @event
  3583. * @name set_state.jstree
  3584. */
  3585. this.trigger('set_state');
  3586. return false;
  3587. }
  3588. return true;
  3589. }
  3590. return false;
  3591. },
  3592. /**
  3593. * refreshes the tree - all nodes are reloaded with calls to `load_node`.
  3594. * @name refresh()
  3595. * @param {Boolean} skip_loading an option to skip showing the loading indicator
  3596. * @param {Mixed} forget_state if set to `true` state will not be reapplied, if set to a function (receiving the current state as argument) the result of that function will be used as state
  3597. * @trigger refresh.jstree
  3598. */
  3599. refresh : function (skip_loading, forget_state) {
  3600. this._data.core.state = forget_state === true ? {} : this.get_state();
  3601. if(forget_state && $.vakata.is_function(forget_state)) { this._data.core.state = forget_state.call(this, this._data.core.state); }
  3602. this._cnt = 0;
  3603. this._model.data = {};
  3604. this._model.data[$.jstree.root] = {
  3605. id : $.jstree.root,
  3606. parent : null,
  3607. parents : [],
  3608. children : [],
  3609. children_d : [],
  3610. state : { loaded : false }
  3611. };
  3612. this._data.core.selected = [];
  3613. this._data.core.last_clicked = null;
  3614. this._data.core.focused = null;
  3615. var c = this.get_container_ul()[0].className;
  3616. if(!skip_loading) {
  3617. this.element.html("<"+"ul class='"+c+"' role='group'><"+"li class='jstree-initial-node jstree-loading jstree-leaf jstree-last' role='none' id='j"+this._id+"_loading'><i class='jstree-icon jstree-ocl'></i><"+"a class='jstree-anchor' role='treeitem' href='#'><i class='jstree-icon jstree-themeicon-hidden'></i>" + this.get_string("Loading ...") + "</a></li></ul>");
  3618. this.element.attr('aria-activedescendant','j'+this._id+'_loading');
  3619. }
  3620. this.load_node($.jstree.root, function (o, s) {
  3621. if(s) {
  3622. this.get_container_ul()[0].className = c;
  3623. if(this._firstChild(this.get_container_ul()[0])) {
  3624. this.element.attr('aria-activedescendant',this._firstChild(this.get_container_ul()[0]).id);
  3625. }
  3626. this.set_state($.extend(true, {}, this._data.core.state), function () {
  3627. /**
  3628. * triggered when a `refresh` call completes
  3629. * @event
  3630. * @name refresh.jstree
  3631. */
  3632. this.trigger('refresh');
  3633. });
  3634. }
  3635. this._data.core.state = null;
  3636. });
  3637. },
  3638. /**
  3639. * refreshes a node in the tree (reload its children) all opened nodes inside that node are reloaded with calls to `load_node`.
  3640. * @name refresh_node(obj)
  3641. * @param {mixed} obj the node
  3642. * @trigger refresh_node.jstree
  3643. */
  3644. refresh_node : function (obj) {
  3645. obj = this.get_node(obj);
  3646. if(!obj || obj.id === $.jstree.root) { return false; }
  3647. var opened = [], to_load = [], s = this._data.core.selected.concat([]);
  3648. to_load.push(obj.id);
  3649. if(obj.state.opened === true) { opened.push(obj.id); }
  3650. this.get_node(obj, true).find('.jstree-open').each(function() { to_load.push(this.id); opened.push(this.id); });
  3651. this._load_nodes(to_load, function (nodes) {
  3652. this.open_node(opened, false, 0);
  3653. this.select_node(s);
  3654. /**
  3655. * triggered when a node is refreshed
  3656. * @event
  3657. * @name refresh_node.jstree
  3658. * @param {Object} node - the refreshed node
  3659. * @param {Array} nodes - an array of the IDs of the nodes that were reloaded
  3660. */
  3661. this.trigger('refresh_node', { 'node' : obj, 'nodes' : nodes });
  3662. }.bind(this), false, true);
  3663. },
  3664. /**
  3665. * set (change) the ID of a node
  3666. * @name set_id(obj, id)
  3667. * @param {mixed} obj the node
  3668. * @param {String} id the new ID
  3669. * @return {Boolean}
  3670. * @trigger set_id.jstree
  3671. */
  3672. set_id : function (obj, id) {
  3673. obj = this.get_node(obj);
  3674. if(!obj || obj.id === $.jstree.root) { return false; }
  3675. var i, j, m = this._model.data, old = obj.id;
  3676. id = id.toString();
  3677. // update parents (replace current ID with new one in children and children_d)
  3678. m[obj.parent].children[$.inArray(obj.id, m[obj.parent].children)] = id;
  3679. for(i = 0, j = obj.parents.length; i < j; i++) {
  3680. m[obj.parents[i]].children_d[$.inArray(obj.id, m[obj.parents[i]].children_d)] = id;
  3681. }
  3682. // update children (replace current ID with new one in parent and parents)
  3683. for(i = 0, j = obj.children.length; i < j; i++) {
  3684. m[obj.children[i]].parent = id;
  3685. }
  3686. for(i = 0, j = obj.children_d.length; i < j; i++) {
  3687. m[obj.children_d[i]].parents[$.inArray(obj.id, m[obj.children_d[i]].parents)] = id;
  3688. }
  3689. i = $.inArray(obj.id, this._data.core.selected);
  3690. if(i !== -1) { this._data.core.selected[i] = id; }
  3691. // update model and obj itself (obj.id, this._model.data[KEY])
  3692. i = this.get_node(obj.id, true);
  3693. if(i) {
  3694. i.attr('id', id); //.children('.jstree-anchor').attr('id', id + '_anchor').end().attr('aria-labelledby', id + '_anchor');
  3695. if(this.element.attr('aria-activedescendant') === obj.id) {
  3696. this.element.attr('aria-activedescendant', id);
  3697. }
  3698. }
  3699. delete m[obj.id];
  3700. obj.id = id;
  3701. obj.li_attr.id = id;
  3702. m[id] = obj;
  3703. /**
  3704. * triggered when a node id value is changed
  3705. * @event
  3706. * @name set_id.jstree
  3707. * @param {Object} node
  3708. * @param {String} old the old id
  3709. */
  3710. this.trigger('set_id',{ "node" : obj, "new" : obj.id, "old" : old });
  3711. return true;
  3712. },
  3713. /**
  3714. * get the text value of a node
  3715. * @name get_text(obj)
  3716. * @param {mixed} obj the node
  3717. * @return {String}
  3718. */
  3719. get_text : function (obj) {
  3720. obj = this.get_node(obj);
  3721. return (!obj || obj.id === $.jstree.root) ? false : obj.text;
  3722. },
  3723. /**
  3724. * set the text value of a node. Used internally, please use `rename_node(obj, val)`.
  3725. * @private
  3726. * @name set_text(obj, val)
  3727. * @param {mixed} obj the node, you can pass an array to set the text on multiple nodes
  3728. * @param {String} val the new text value
  3729. * @return {Boolean}
  3730. * @trigger set_text.jstree
  3731. */
  3732. set_text : function (obj, val) {
  3733. var t1, t2;
  3734. if($.vakata.is_array(obj)) {
  3735. obj = obj.slice();
  3736. for(t1 = 0, t2 = obj.length; t1 < t2; t1++) {
  3737. this.set_text(obj[t1], val);
  3738. }
  3739. return true;
  3740. }
  3741. obj = this.get_node(obj);
  3742. if(!obj || obj.id === $.jstree.root) { return false; }
  3743. obj.text = val;
  3744. if(this.get_node(obj, true).length) {
  3745. this.redraw_node(obj.id);
  3746. }
  3747. /**
  3748. * triggered when a node text value is changed
  3749. * @event
  3750. * @name set_text.jstree
  3751. * @param {Object} obj
  3752. * @param {String} text the new value
  3753. */
  3754. this.trigger('set_text',{ "obj" : obj, "text" : val });
  3755. return true;
  3756. },
  3757. /**
  3758. * gets a JSON representation of a node (or the whole tree)
  3759. * @name get_json([obj, options])
  3760. * @param {mixed} obj
  3761. * @param {Object} options
  3762. * @param {Boolean} options.no_state do not return state information
  3763. * @param {Boolean} options.no_id do not return ID
  3764. * @param {Boolean} options.no_children do not include children
  3765. * @param {Boolean} options.no_data do not include node data
  3766. * @param {Boolean} options.no_li_attr do not include LI attributes
  3767. * @param {Boolean} options.no_a_attr do not include A attributes
  3768. * @param {Boolean} options.flat return flat JSON instead of nested
  3769. * @return {Object}
  3770. */
  3771. get_json : function (obj, options, flat) {
  3772. obj = this.get_node(obj || $.jstree.root);
  3773. if(!obj) { return false; }
  3774. if(options && options.flat && !flat) { flat = []; }
  3775. var tmp = {
  3776. 'id' : obj.id,
  3777. 'text' : obj.text,
  3778. 'icon' : this.get_icon(obj),
  3779. 'li_attr' : $.extend(true, {}, obj.li_attr),
  3780. 'a_attr' : $.extend(true, {}, obj.a_attr),
  3781. 'state' : {},
  3782. 'data' : options && options.no_data ? false : $.extend(true, $.vakata.is_array(obj.data)?[]:{}, obj.data)
  3783. //( this.get_node(obj, true).length ? this.get_node(obj, true).data() : obj.data ),
  3784. }, i, j;
  3785. if(options && options.flat) {
  3786. tmp.parent = obj.parent;
  3787. }
  3788. else {
  3789. tmp.children = [];
  3790. }
  3791. if(!options || !options.no_state) {
  3792. for(i in obj.state) {
  3793. if(obj.state.hasOwnProperty(i)) {
  3794. tmp.state[i] = obj.state[i];
  3795. }
  3796. }
  3797. } else {
  3798. delete tmp.state;
  3799. }
  3800. if(options && options.no_li_attr) {
  3801. delete tmp.li_attr;
  3802. }
  3803. if(options && options.no_a_attr) {
  3804. delete tmp.a_attr;
  3805. }
  3806. if(options && options.no_id) {
  3807. delete tmp.id;
  3808. if(tmp.li_attr && tmp.li_attr.id) {
  3809. delete tmp.li_attr.id;
  3810. }
  3811. if(tmp.a_attr && tmp.a_attr.id) {
  3812. delete tmp.a_attr.id;
  3813. }
  3814. }
  3815. if(options && options.flat && obj.id !== $.jstree.root) {
  3816. flat.push(tmp);
  3817. }
  3818. if(!options || !options.no_children) {
  3819. for(i = 0, j = obj.children.length; i < j; i++) {
  3820. if(options && options.flat) {
  3821. this.get_json(obj.children[i], options, flat);
  3822. }
  3823. else {
  3824. tmp.children.push(this.get_json(obj.children[i], options));
  3825. }
  3826. }
  3827. }
  3828. return options && options.flat ? flat : (obj.id === $.jstree.root ? tmp.children : tmp);
  3829. },
  3830. /**
  3831. * create a new node (do not confuse with load_node)
  3832. * @name create_node([par, node, pos, callback, is_loaded])
  3833. * @param {mixed} par the parent node (to create a root node use either "#" (string) or `null`)
  3834. * @param {mixed} node the data for the new node (a valid JSON object, or a simple string with the name)
  3835. * @param {mixed} pos the index at which to insert the node, "first" and "last" are also supported, default is "last"
  3836. * @param {Function} callback a function to be called once the node is created
  3837. * @param {Boolean} is_loaded internal argument indicating if the parent node was succesfully loaded
  3838. * @return {String} the ID of the newly create node
  3839. * @trigger model.jstree, create_node.jstree
  3840. */
  3841. create_node : function (par, node, pos, callback, is_loaded) {
  3842. if(par === null) { par = $.jstree.root; }
  3843. par = this.get_node(par);
  3844. if(!par) { return false; }
  3845. pos = pos === undefined ? "last" : pos;
  3846. if(!pos.toString().match(/^(before|after)$/) && !is_loaded && !this.is_loaded(par)) {
  3847. return this.load_node(par, function () { this.create_node(par, node, pos, callback, true); });
  3848. }
  3849. if(!node) { node = { "text" : this.get_string('New node') }; }
  3850. if(typeof node === "string") {
  3851. node = { "text" : node };
  3852. } else {
  3853. node = $.extend(true, {}, node);
  3854. }
  3855. if(node.text === undefined) { node.text = this.get_string('New node'); }
  3856. var tmp, dpc, i, j;
  3857. if(par.id === $.jstree.root) {
  3858. if(pos === "before") { pos = "first"; }
  3859. if(pos === "after") { pos = "last"; }
  3860. }
  3861. switch(pos) {
  3862. case "before":
  3863. tmp = this.get_node(par.parent);
  3864. pos = $.inArray(par.id, tmp.children);
  3865. par = tmp;
  3866. break;
  3867. case "after" :
  3868. tmp = this.get_node(par.parent);
  3869. pos = $.inArray(par.id, tmp.children) + 1;
  3870. par = tmp;
  3871. break;
  3872. case "inside":
  3873. case "first":
  3874. pos = 0;
  3875. break;
  3876. case "last":
  3877. pos = par.children.length;
  3878. break;
  3879. default:
  3880. if(!pos) { pos = 0; }
  3881. break;
  3882. }
  3883. if(pos > par.children.length) { pos = par.children.length; }
  3884. if(node.id === undefined) { node.id = true; }
  3885. if(!this.check("create_node", node, par, pos)) {
  3886. this.settings.core.error.call(this, this._data.core.last_error);
  3887. return false;
  3888. }
  3889. if(node.id === true) { delete node.id; }
  3890. node = this._parse_model_from_json(node, par.id, par.parents.concat());
  3891. if(!node) { return false; }
  3892. tmp = this.get_node(node);
  3893. dpc = [];
  3894. dpc.push(node);
  3895. dpc = dpc.concat(tmp.children_d);
  3896. this.trigger('model', { "nodes" : dpc, "parent" : par.id });
  3897. par.children_d = par.children_d.concat(dpc);
  3898. for(i = 0, j = par.parents.length; i < j; i++) {
  3899. this._model.data[par.parents[i]].children_d = this._model.data[par.parents[i]].children_d.concat(dpc);
  3900. }
  3901. node = tmp;
  3902. tmp = [];
  3903. for(i = 0, j = par.children.length; i < j; i++) {
  3904. tmp[i >= pos ? i+1 : i] = par.children[i];
  3905. }
  3906. tmp[pos] = node.id;
  3907. par.children = tmp;
  3908. this.redraw_node(par, true);
  3909. /**
  3910. * triggered when a node is created
  3911. * @event
  3912. * @name create_node.jstree
  3913. * @param {Object} node
  3914. * @param {String} parent the parent's ID
  3915. * @param {Number} position the position of the new node among the parent's children
  3916. */
  3917. this.trigger('create_node', { "node" : this.get_node(node), "parent" : par.id, "position" : pos });
  3918. if(callback) { callback.call(this, this.get_node(node)); }
  3919. return node.id;
  3920. },
  3921. /**
  3922. * set the text value of a node
  3923. * @name rename_node(obj, val)
  3924. * @param {mixed} obj the node, you can pass an array to rename multiple nodes to the same name
  3925. * @param {String} val the new text value
  3926. * @return {Boolean}
  3927. * @trigger rename_node.jstree
  3928. */
  3929. rename_node : function (obj, val) {
  3930. var t1, t2, old;
  3931. if($.vakata.is_array(obj)) {
  3932. obj = obj.slice();
  3933. for(t1 = 0, t2 = obj.length; t1 < t2; t1++) {
  3934. this.rename_node(obj[t1], val);
  3935. }
  3936. return true;
  3937. }
  3938. obj = this.get_node(obj);
  3939. if(!obj || obj.id === $.jstree.root) { return false; }
  3940. old = obj.text;
  3941. if(!this.check("rename_node", obj, this.get_parent(obj), val)) {
  3942. this.settings.core.error.call(this, this._data.core.last_error);
  3943. return false;
  3944. }
  3945. this.set_text(obj, val); // .apply(this, Array.prototype.slice.call(arguments))
  3946. /**
  3947. * triggered when a node is renamed
  3948. * @event
  3949. * @name rename_node.jstree
  3950. * @param {Object} node
  3951. * @param {String} text the new value
  3952. * @param {String} old the old value
  3953. */
  3954. this.trigger('rename_node', { "node" : obj, "text" : val, "old" : old });
  3955. return true;
  3956. },
  3957. /**
  3958. * remove a node
  3959. * @name delete_node(obj)
  3960. * @param {mixed} obj the node, you can pass an array to delete multiple nodes
  3961. * @return {Boolean}
  3962. * @trigger delete_node.jstree, changed.jstree
  3963. */
  3964. delete_node : function (obj) {
  3965. var t1, t2, par, pos, tmp, i, j, k, l, c, top, lft;
  3966. if($.vakata.is_array(obj)) {
  3967. obj = obj.slice();
  3968. for(t1 = 0, t2 = obj.length; t1 < t2; t1++) {
  3969. this.delete_node(obj[t1]);
  3970. }
  3971. return true;
  3972. }
  3973. obj = this.get_node(obj);
  3974. if(!obj || obj.id === $.jstree.root) { return false; }
  3975. par = this.get_node(obj.parent);
  3976. pos = $.inArray(obj.id, par.children);
  3977. c = false;
  3978. if(!this.check("delete_node", obj, par, pos)) {
  3979. this.settings.core.error.call(this, this._data.core.last_error);
  3980. return false;
  3981. }
  3982. if(pos !== -1) {
  3983. par.children = $.vakata.array_remove(par.children, pos);
  3984. }
  3985. tmp = obj.children_d.concat([]);
  3986. tmp.push(obj.id);
  3987. for(i = 0, j = obj.parents.length; i < j; i++) {
  3988. this._model.data[obj.parents[i]].children_d = $.vakata.array_filter(this._model.data[obj.parents[i]].children_d, function (v) {
  3989. return $.inArray(v, tmp) === -1;
  3990. });
  3991. }
  3992. for(k = 0, l = tmp.length; k < l; k++) {
  3993. if(this._model.data[tmp[k]].state.selected) {
  3994. c = true;
  3995. break;
  3996. }
  3997. }
  3998. if (c) {
  3999. this._data.core.selected = $.vakata.array_filter(this._data.core.selected, function (v) {
  4000. return $.inArray(v, tmp) === -1;
  4001. });
  4002. }
  4003. /**
  4004. * triggered when a node is deleted
  4005. * @event
  4006. * @name delete_node.jstree
  4007. * @param {Object} node
  4008. * @param {String} parent the parent's ID
  4009. */
  4010. this.trigger('delete_node', { "node" : obj, "parent" : par.id });
  4011. if(c) {
  4012. this.trigger('changed', { 'action' : 'delete_node', 'node' : obj, 'selected' : this._data.core.selected, 'parent' : par.id });
  4013. }
  4014. for(k = 0, l = tmp.length; k < l; k++) {
  4015. delete this._model.data[tmp[k]];
  4016. }
  4017. if($.inArray(this._data.core.focused, tmp) !== -1) {
  4018. this._data.core.focused = null;
  4019. top = this.element[0].scrollTop;
  4020. lft = this.element[0].scrollLeft;
  4021. if(par.id === $.jstree.root) {
  4022. if (this._model.data[$.jstree.root].children[0]) {
  4023. this.get_node(this._model.data[$.jstree.root].children[0], true).children('.jstree-anchor').trigger('focus');
  4024. }
  4025. }
  4026. else {
  4027. this.get_node(par, true).children('.jstree-anchor').trigger('focus');
  4028. }
  4029. this.element[0].scrollTop = top;
  4030. this.element[0].scrollLeft = lft;
  4031. }
  4032. this.redraw_node(par, true);
  4033. return true;
  4034. },
  4035. /**
  4036. * check if an operation is premitted on the tree. Used internally.
  4037. * @private
  4038. * @name check(chk, obj, par, pos)
  4039. * @param {String} chk the operation to check, can be "create_node", "rename_node", "delete_node", "copy_node" or "move_node"
  4040. * @param {mixed} obj the node
  4041. * @param {mixed} par the parent
  4042. * @param {mixed} pos the position to insert at, or if "rename_node" - the new name
  4043. * @param {mixed} more some various additional information, for example if a "move_node" operations is triggered by DND this will be the hovered node
  4044. * @return {Boolean}
  4045. */
  4046. check : function (chk, obj, par, pos, more) {
  4047. obj = obj && obj.id ? obj : this.get_node(obj);
  4048. par = par && par.id ? par : this.get_node(par);
  4049. var tmp = chk.match(/^(move_node|copy_node|create_node)$/i) ? par : obj,
  4050. chc = this.settings.core.check_callback;
  4051. if(chk === "move_node" || chk === "copy_node") {
  4052. if((!more || !more.is_multi) && (chk === "move_node" && $.inArray(obj.id, par.children) === pos)) {
  4053. this._data.core.last_error = { 'error' : 'check', 'plugin' : 'core', 'id' : 'core_08', 'reason' : 'Moving node to its current position', 'data' : JSON.stringify({ 'chk' : chk, 'pos' : pos, 'obj' : obj && obj.id ? obj.id : false, 'par' : par && par.id ? par.id : false }) };
  4054. return false;
  4055. }
  4056. if((!more || !more.is_multi) && (obj.id === par.id || (chk === "move_node" && $.inArray(obj.id, par.children) === pos) || $.inArray(par.id, obj.children_d) !== -1)) {
  4057. this._data.core.last_error = { 'error' : 'check', 'plugin' : 'core', 'id' : 'core_01', 'reason' : 'Moving parent inside child', 'data' : JSON.stringify({ 'chk' : chk, 'pos' : pos, 'obj' : obj && obj.id ? obj.id : false, 'par' : par && par.id ? par.id : false }) };
  4058. return false;
  4059. }
  4060. }
  4061. if(tmp && tmp.data) { tmp = tmp.data; }
  4062. if(tmp && tmp.functions && (tmp.functions[chk] === false || tmp.functions[chk] === true)) {
  4063. if(tmp.functions[chk] === false) {
  4064. this._data.core.last_error = { 'error' : 'check', 'plugin' : 'core', 'id' : 'core_02', 'reason' : 'Node data prevents function: ' + chk, 'data' : JSON.stringify({ 'chk' : chk, 'pos' : pos, 'obj' : obj && obj.id ? obj.id : false, 'par' : par && par.id ? par.id : false }) };
  4065. }
  4066. return tmp.functions[chk];
  4067. }
  4068. if(chc === false || ($.vakata.is_function(chc) && chc.call(this, chk, obj, par, pos, more) === false) || (chc && chc[chk] === false)) {
  4069. this._data.core.last_error = { 'error' : 'check', 'plugin' : 'core', 'id' : 'core_03', 'reason' : 'User config for core.check_callback prevents function: ' + chk, 'data' : JSON.stringify({ 'chk' : chk, 'pos' : pos, 'obj' : obj && obj.id ? obj.id : false, 'par' : par && par.id ? par.id : false }) };
  4070. return false;
  4071. }
  4072. return true;
  4073. },
  4074. /**
  4075. * get the last error
  4076. * @name last_error()
  4077. * @return {Object}
  4078. */
  4079. last_error : function () {
  4080. return this._data.core.last_error;
  4081. },
  4082. /**
  4083. * move a node to a new parent
  4084. * @name move_node(obj, par [, pos, callback, is_loaded])
  4085. * @param {mixed} obj the node to move, pass an array to move multiple nodes
  4086. * @param {mixed} par the new parent
  4087. * @param {mixed} pos the position to insert at (besides integer values, "first" and "last" are supported, as well as "before" and "after"), defaults to integer `0`
  4088. * @param {function} callback a function to call once the move is completed, receives 3 arguments - the node, the new parent and the position
  4089. * @param {Boolean} is_loaded internal parameter indicating if the parent node has been loaded
  4090. * @param {Boolean} skip_redraw internal parameter indicating if the tree should be redrawn
  4091. * @param {Boolean} instance internal parameter indicating if the node comes from another instance
  4092. * @trigger move_node.jstree
  4093. */
  4094. move_node : function (obj, par, pos, callback, is_loaded, skip_redraw, origin) {
  4095. var t1, t2, old_par, old_pos, new_par, old_ins, is_multi, dpc, tmp, i, j, k, l, p;
  4096. par = this.get_node(par);
  4097. pos = pos === undefined ? 0 : pos;
  4098. if(!par) { return false; }
  4099. if(!pos.toString().match(/^(before|after)$/) && !is_loaded && !this.is_loaded(par)) {
  4100. return this.load_node(par, function () { this.move_node(obj, par, pos, callback, true, false, origin); });
  4101. }
  4102. if($.vakata.is_array(obj)) {
  4103. if(obj.length === 1) {
  4104. obj = obj[0];
  4105. }
  4106. else {
  4107. //obj = obj.slice();
  4108. for(t1 = 0, t2 = obj.length; t1 < t2; t1++) {
  4109. if((tmp = this.move_node(obj[t1], par, pos, callback, is_loaded, false, origin))) {
  4110. par = tmp;
  4111. pos = "after";
  4112. }
  4113. }
  4114. this.redraw();
  4115. return true;
  4116. }
  4117. }
  4118. obj = obj && (obj.id !== undefined) ? obj : this.get_node(obj);
  4119. if(!obj || obj.id === $.jstree.root) { return false; }
  4120. old_par = (obj.parent || $.jstree.root).toString();
  4121. new_par = (!pos.toString().match(/^(before|after)$/) || par.id === $.jstree.root) ? par : this.get_node(par.parent);
  4122. old_ins = origin ? origin : (this._model.data[obj.id] ? this : $.jstree.reference(obj.id));
  4123. is_multi = !old_ins || !old_ins._id || (this._id !== old_ins._id);
  4124. old_pos = old_ins && old_ins._id && old_par && old_ins._model.data[old_par] && old_ins._model.data[old_par].children ? $.inArray(obj.id, old_ins._model.data[old_par].children) : -1;
  4125. if(old_ins && old_ins._id) {
  4126. obj = old_ins._model.data[obj.id];
  4127. }
  4128. if(is_multi) {
  4129. if((tmp = this.copy_node(obj, par, pos, callback, is_loaded, false, origin))) {
  4130. if(old_ins) { old_ins.delete_node(obj); }
  4131. return tmp;
  4132. }
  4133. return false;
  4134. }
  4135. //var m = this._model.data;
  4136. if(par.id === $.jstree.root) {
  4137. if(pos === "before") { pos = "first"; }
  4138. if(pos === "after") { pos = "last"; }
  4139. }
  4140. switch(pos) {
  4141. case "before":
  4142. pos = $.inArray(par.id, new_par.children);
  4143. break;
  4144. case "after" :
  4145. pos = $.inArray(par.id, new_par.children) + 1;
  4146. break;
  4147. case "inside":
  4148. case "first":
  4149. pos = 0;
  4150. break;
  4151. case "last":
  4152. pos = new_par.children.length;
  4153. break;
  4154. default:
  4155. if(!pos) { pos = 0; }
  4156. break;
  4157. }
  4158. if(pos > new_par.children.length) { pos = new_par.children.length; }
  4159. if(!this.check("move_node", obj, new_par, pos, { 'core' : true, 'origin' : origin, 'is_multi' : (old_ins && old_ins._id && old_ins._id !== this._id), 'is_foreign' : (!old_ins || !old_ins._id) })) {
  4160. this.settings.core.error.call(this, this._data.core.last_error);
  4161. return false;
  4162. }
  4163. if(obj.parent === new_par.id) {
  4164. dpc = new_par.children.concat();
  4165. tmp = $.inArray(obj.id, dpc);
  4166. if(tmp !== -1) {
  4167. dpc = $.vakata.array_remove(dpc, tmp);
  4168. if(pos > tmp) { pos--; }
  4169. }
  4170. tmp = [];
  4171. for(i = 0, j = dpc.length; i < j; i++) {
  4172. tmp[i >= pos ? i+1 : i] = dpc[i];
  4173. }
  4174. tmp[pos] = obj.id;
  4175. new_par.children = tmp;
  4176. this._node_changed(new_par.id);
  4177. this.redraw(new_par.id === $.jstree.root);
  4178. }
  4179. else {
  4180. // clean old parent and up
  4181. tmp = obj.children_d.concat();
  4182. tmp.push(obj.id);
  4183. for(i = 0, j = obj.parents.length; i < j; i++) {
  4184. dpc = [];
  4185. p = old_ins._model.data[obj.parents[i]].children_d;
  4186. for(k = 0, l = p.length; k < l; k++) {
  4187. if($.inArray(p[k], tmp) === -1) {
  4188. dpc.push(p[k]);
  4189. }
  4190. }
  4191. old_ins._model.data[obj.parents[i]].children_d = dpc;
  4192. }
  4193. old_ins._model.data[old_par].children = $.vakata.array_remove_item(old_ins._model.data[old_par].children, obj.id);
  4194. // insert into new parent and up
  4195. for(i = 0, j = new_par.parents.length; i < j; i++) {
  4196. this._model.data[new_par.parents[i]].children_d = this._model.data[new_par.parents[i]].children_d.concat(tmp);
  4197. }
  4198. dpc = [];
  4199. for(i = 0, j = new_par.children.length; i < j; i++) {
  4200. dpc[i >= pos ? i+1 : i] = new_par.children[i];
  4201. }
  4202. dpc[pos] = obj.id;
  4203. new_par.children = dpc;
  4204. new_par.children_d.push(obj.id);
  4205. new_par.children_d = new_par.children_d.concat(obj.children_d);
  4206. // update object
  4207. obj.parent = new_par.id;
  4208. tmp = new_par.parents.concat();
  4209. tmp.unshift(new_par.id);
  4210. p = obj.parents.length;
  4211. obj.parents = tmp;
  4212. // update object children
  4213. tmp = tmp.concat();
  4214. for(i = 0, j = obj.children_d.length; i < j; i++) {
  4215. this._model.data[obj.children_d[i]].parents = this._model.data[obj.children_d[i]].parents.slice(0,p*-1);
  4216. Array.prototype.push.apply(this._model.data[obj.children_d[i]].parents, tmp);
  4217. }
  4218. if(old_par === $.jstree.root || new_par.id === $.jstree.root) {
  4219. this._model.force_full_redraw = true;
  4220. }
  4221. if(!this._model.force_full_redraw) {
  4222. this._node_changed(old_par);
  4223. this._node_changed(new_par.id);
  4224. }
  4225. if(!skip_redraw) {
  4226. this.redraw();
  4227. }
  4228. }
  4229. if(callback) { callback.call(this, obj, new_par, pos); }
  4230. /**
  4231. * triggered when a node is moved
  4232. * @event
  4233. * @name move_node.jstree
  4234. * @param {Object} node
  4235. * @param {String} parent the parent's ID
  4236. * @param {Number} position the position of the node among the parent's children
  4237. * @param {String} old_parent the old parent of the node
  4238. * @param {Number} old_position the old position of the node
  4239. * @param {Boolean} is_multi do the node and new parent belong to different instances
  4240. * @param {jsTree} old_instance the instance the node came from
  4241. * @param {jsTree} new_instance the instance of the new parent
  4242. */
  4243. this.trigger('move_node', { "node" : obj, "parent" : new_par.id, "position" : pos, "old_parent" : old_par, "old_position" : old_pos, 'is_multi' : (old_ins && old_ins._id && old_ins._id !== this._id), 'is_foreign' : (!old_ins || !old_ins._id), 'old_instance' : old_ins, 'new_instance' : this });
  4244. return obj.id;
  4245. },
  4246. /**
  4247. * copy a node to a new parent
  4248. * @name copy_node(obj, par [, pos, callback, is_loaded])
  4249. * @param {mixed} obj the node to copy, pass an array to copy multiple nodes
  4250. * @param {mixed} par the new parent
  4251. * @param {mixed} pos the position to insert at (besides integer values, "first" and "last" are supported, as well as "before" and "after"), defaults to integer `0`
  4252. * @param {function} callback a function to call once the move is completed, receives 3 arguments - the node, the new parent and the position
  4253. * @param {Boolean} is_loaded internal parameter indicating if the parent node has been loaded
  4254. * @param {Boolean} skip_redraw internal parameter indicating if the tree should be redrawn
  4255. * @param {Boolean} instance internal parameter indicating if the node comes from another instance
  4256. * @trigger model.jstree copy_node.jstree
  4257. */
  4258. copy_node : function (obj, par, pos, callback, is_loaded, skip_redraw, origin) {
  4259. var t1, t2, dpc, tmp, i, j, node, old_par, new_par, old_ins, is_multi;
  4260. par = this.get_node(par);
  4261. pos = pos === undefined ? 0 : pos;
  4262. if(!par) { return false; }
  4263. if(!pos.toString().match(/^(before|after)$/) && !is_loaded && !this.is_loaded(par)) {
  4264. return this.load_node(par, function () { this.copy_node(obj, par, pos, callback, true, false, origin); });
  4265. }
  4266. if($.vakata.is_array(obj)) {
  4267. if(obj.length === 1) {
  4268. obj = obj[0];
  4269. }
  4270. else {
  4271. //obj = obj.slice();
  4272. for(t1 = 0, t2 = obj.length; t1 < t2; t1++) {
  4273. if((tmp = this.copy_node(obj[t1], par, pos, callback, is_loaded, true, origin))) {
  4274. par = tmp;
  4275. pos = "after";
  4276. }
  4277. }
  4278. this.redraw();
  4279. return true;
  4280. }
  4281. }
  4282. obj = obj && (obj.id !== undefined) ? obj : this.get_node(obj);
  4283. if(!obj || obj.id === $.jstree.root) { return false; }
  4284. old_par = (obj.parent || $.jstree.root).toString();
  4285. new_par = (!pos.toString().match(/^(before|after)$/) || par.id === $.jstree.root) ? par : this.get_node(par.parent);
  4286. old_ins = origin ? origin : (this._model.data[obj.id] ? this : $.jstree.reference(obj.id));
  4287. is_multi = !old_ins || !old_ins._id || (this._id !== old_ins._id);
  4288. if(old_ins && old_ins._id) {
  4289. obj = old_ins._model.data[obj.id];
  4290. }
  4291. if(par.id === $.jstree.root) {
  4292. if(pos === "before") { pos = "first"; }
  4293. if(pos === "after") { pos = "last"; }
  4294. }
  4295. switch(pos) {
  4296. case "before":
  4297. pos = $.inArray(par.id, new_par.children);
  4298. break;
  4299. case "after" :
  4300. pos = $.inArray(par.id, new_par.children) + 1;
  4301. break;
  4302. case "inside":
  4303. case "first":
  4304. pos = 0;
  4305. break;
  4306. case "last":
  4307. pos = new_par.children.length;
  4308. break;
  4309. default:
  4310. if(!pos) { pos = 0; }
  4311. break;
  4312. }
  4313. if(pos > new_par.children.length) { pos = new_par.children.length; }
  4314. if(!this.check("copy_node", obj, new_par, pos, { 'core' : true, 'origin' : origin, 'is_multi' : (old_ins && old_ins._id && old_ins._id !== this._id), 'is_foreign' : (!old_ins || !old_ins._id) })) {
  4315. this.settings.core.error.call(this, this._data.core.last_error);
  4316. return false;
  4317. }
  4318. node = old_ins ? old_ins.get_json(obj, { no_id : true, no_data : true, no_state : true }) : obj;
  4319. if(!node) { return false; }
  4320. if(node.id === true) { delete node.id; }
  4321. node = this._parse_model_from_json(node, new_par.id, new_par.parents.concat());
  4322. if(!node) { return false; }
  4323. tmp = this.get_node(node);
  4324. if(obj && obj.state && obj.state.loaded === false) { tmp.state.loaded = false; }
  4325. dpc = [];
  4326. dpc.push(node);
  4327. dpc = dpc.concat(tmp.children_d);
  4328. this.trigger('model', { "nodes" : dpc, "parent" : new_par.id });
  4329. // insert into new parent and up
  4330. for(i = 0, j = new_par.parents.length; i < j; i++) {
  4331. this._model.data[new_par.parents[i]].children_d = this._model.data[new_par.parents[i]].children_d.concat(dpc);
  4332. }
  4333. dpc = [];
  4334. for(i = 0, j = new_par.children.length; i < j; i++) {
  4335. dpc[i >= pos ? i+1 : i] = new_par.children[i];
  4336. }
  4337. dpc[pos] = tmp.id;
  4338. new_par.children = dpc;
  4339. new_par.children_d.push(tmp.id);
  4340. new_par.children_d = new_par.children_d.concat(tmp.children_d);
  4341. if(new_par.id === $.jstree.root) {
  4342. this._model.force_full_redraw = true;
  4343. }
  4344. if(!this._model.force_full_redraw) {
  4345. this._node_changed(new_par.id);
  4346. }
  4347. if(!skip_redraw) {
  4348. this.redraw(new_par.id === $.jstree.root);
  4349. }
  4350. if(callback) { callback.call(this, tmp, new_par, pos); }
  4351. /**
  4352. * triggered when a node is copied
  4353. * @event
  4354. * @name copy_node.jstree
  4355. * @param {Object} node the copied node
  4356. * @param {Object} original the original node
  4357. * @param {String} parent the parent's ID
  4358. * @param {Number} position the position of the node among the parent's children
  4359. * @param {String} old_parent the old parent of the node
  4360. * @param {Number} old_position the position of the original node
  4361. * @param {Boolean} is_multi do the node and new parent belong to different instances
  4362. * @param {jsTree} old_instance the instance the node came from
  4363. * @param {jsTree} new_instance the instance of the new parent
  4364. */
  4365. this.trigger('copy_node', { "node" : tmp, "original" : obj, "parent" : new_par.id, "position" : pos, "old_parent" : old_par, "old_position" : old_ins && old_ins._id && old_par && old_ins._model.data[old_par] && old_ins._model.data[old_par].children ? $.inArray(obj.id, old_ins._model.data[old_par].children) : -1,'is_multi' : (old_ins && old_ins._id && old_ins._id !== this._id), 'is_foreign' : (!old_ins || !old_ins._id), 'old_instance' : old_ins, 'new_instance' : this });
  4366. return tmp.id;
  4367. },
  4368. /**
  4369. * cut a node (a later call to `paste(obj)` would move the node)
  4370. * @name cut(obj)
  4371. * @param {mixed} obj multiple objects can be passed using an array
  4372. * @trigger cut.jstree
  4373. */
  4374. cut : function (obj) {
  4375. if(!obj) { obj = this._data.core.selected.concat(); }
  4376. if(!$.vakata.is_array(obj)) { obj = [obj]; }
  4377. if(!obj.length) { return false; }
  4378. var tmp = [], o, t1, t2;
  4379. for(t1 = 0, t2 = obj.length; t1 < t2; t1++) {
  4380. o = this.get_node(obj[t1]);
  4381. if(o && (o.id || o.id === 0) && o.id !== $.jstree.root) { tmp.push(o); }
  4382. }
  4383. if(!tmp.length) { return false; }
  4384. ccp_node = tmp;
  4385. ccp_inst = this;
  4386. ccp_mode = 'move_node';
  4387. /**
  4388. * triggered when nodes are added to the buffer for moving
  4389. * @event
  4390. * @name cut.jstree
  4391. * @param {Array} node
  4392. */
  4393. this.trigger('cut', { "node" : obj });
  4394. },
  4395. /**
  4396. * copy a node (a later call to `paste(obj)` would copy the node)
  4397. * @name copy(obj)
  4398. * @param {mixed} obj multiple objects can be passed using an array
  4399. * @trigger copy.jstree
  4400. */
  4401. copy : function (obj) {
  4402. if(!obj) { obj = this._data.core.selected.concat(); }
  4403. if(!$.vakata.is_array(obj)) { obj = [obj]; }
  4404. if(!obj.length) { return false; }
  4405. var tmp = [], o, t1, t2;
  4406. for(t1 = 0, t2 = obj.length; t1 < t2; t1++) {
  4407. o = this.get_node(obj[t1]);
  4408. if(o && (o.id !== undefined) && o.id !== $.jstree.root) { tmp.push(o); }
  4409. }
  4410. if(!tmp.length) { return false; }
  4411. ccp_node = tmp;
  4412. ccp_inst = this;
  4413. ccp_mode = 'copy_node';
  4414. /**
  4415. * triggered when nodes are added to the buffer for copying
  4416. * @event
  4417. * @name copy.jstree
  4418. * @param {Array} node
  4419. */
  4420. this.trigger('copy', { "node" : obj });
  4421. },
  4422. /**
  4423. * get the current buffer (any nodes that are waiting for a paste operation)
  4424. * @name get_buffer()
  4425. * @return {Object} an object consisting of `mode` ("copy_node" or "move_node"), `node` (an array of objects) and `inst` (the instance)
  4426. */
  4427. get_buffer : function () {
  4428. return { 'mode' : ccp_mode, 'node' : ccp_node, 'inst' : ccp_inst };
  4429. },
  4430. /**
  4431. * check if there is something in the buffer to paste
  4432. * @name can_paste()
  4433. * @return {Boolean}
  4434. */
  4435. can_paste : function () {
  4436. return ccp_mode !== false && ccp_node !== false; // && ccp_inst._model.data[ccp_node];
  4437. },
  4438. /**
  4439. * copy or move the previously cut or copied nodes to a new parent
  4440. * @name paste(obj [, pos])
  4441. * @param {mixed} obj the new parent
  4442. * @param {mixed} pos the position to insert at (besides integer, "first" and "last" are supported), defaults to integer `0`
  4443. * @trigger paste.jstree
  4444. */
  4445. paste : function (obj, pos) {
  4446. obj = this.get_node(obj);
  4447. if(!obj || !ccp_mode || !ccp_mode.match(/^(copy_node|move_node)$/) || !ccp_node) { return false; }
  4448. if(this[ccp_mode](ccp_node, obj, pos, false, false, false, ccp_inst)) {
  4449. /**
  4450. * triggered when paste is invoked
  4451. * @event
  4452. * @name paste.jstree
  4453. * @param {String} parent the ID of the receiving node
  4454. * @param {Array} node the nodes in the buffer
  4455. * @param {String} mode the performed operation - "copy_node" or "move_node"
  4456. */
  4457. this.trigger('paste', { "parent" : obj.id, "node" : ccp_node, "mode" : ccp_mode });
  4458. }
  4459. ccp_node = false;
  4460. ccp_mode = false;
  4461. ccp_inst = false;
  4462. },
  4463. /**
  4464. * clear the buffer of previously copied or cut nodes
  4465. * @name clear_buffer()
  4466. * @trigger clear_buffer.jstree
  4467. */
  4468. clear_buffer : function () {
  4469. ccp_node = false;
  4470. ccp_mode = false;
  4471. ccp_inst = false;
  4472. /**
  4473. * triggered when the copy / cut buffer is cleared
  4474. * @event
  4475. * @name clear_buffer.jstree
  4476. */
  4477. this.trigger('clear_buffer');
  4478. },
  4479. /**
  4480. * put a node in edit mode (input field to rename the node)
  4481. * @name edit(obj [, default_text, callback])
  4482. * @param {mixed} obj
  4483. * @param {String} default_text the text to populate the input with (if omitted or set to a non-string value the node's text value is used)
  4484. * @param {Function} callback a function to be called once the text box is blurred, it is called in the instance's scope and receives the node, a status parameter (true if the rename is successful, false otherwise), a boolean indicating if the user cancelled the edit and the original unescaped value provided by the user. You can also access the node's title using .text
  4485. */
  4486. edit : function (obj, default_text, callback) {
  4487. var rtl, w, a, s, t, h1, h2, fn, tmp, cancel = false;
  4488. obj = this.get_node(obj);
  4489. if(!obj) { return false; }
  4490. if(!this.check("edit", obj, this.get_parent(obj))) {
  4491. this.settings.core.error.call(this, this._data.core.last_error);
  4492. return false;
  4493. }
  4494. tmp = obj;
  4495. default_text = typeof default_text === 'string' ? default_text : obj.text;
  4496. this.set_text(obj, "");
  4497. obj = this._open_to(obj);
  4498. tmp.text = default_text;
  4499. rtl = this._data.core.rtl;
  4500. w = this.element.width();
  4501. this._data.core.focused = tmp.id;
  4502. a = obj.children('.jstree-anchor').trigger('focus');
  4503. s = $('<span></span>');
  4504. /*!
  4505. oi = obj.children("i:visible"),
  4506. ai = a.children("i:visible"),
  4507. w1 = oi.width() * oi.length,
  4508. w2 = ai.width() * ai.length,
  4509. */
  4510. t = default_text;
  4511. h1 = $("<"+"div></div>", { css : { "position" : "absolute", "top" : "-200px", "left" : (rtl ? "0px" : "-1000px"), "visibility" : "hidden" } }).appendTo(document.body);
  4512. h2 = $("<"+"input />", {
  4513. "value" : t,
  4514. "class" : "jstree-rename-input",
  4515. // "size" : t.length,
  4516. "css" : {
  4517. "padding" : "0",
  4518. "border" : "1px solid silver",
  4519. "box-sizing" : "border-box",
  4520. "display" : "inline-block",
  4521. "height" : (this._data.core.li_height) + "px",
  4522. "lineHeight" : (this._data.core.li_height) + "px",
  4523. "width" : "150px" // will be set a bit further down
  4524. },
  4525. "blur" : function (e) {
  4526. e.stopImmediatePropagation();
  4527. e.preventDefault();
  4528. var i = s.children(".jstree-rename-input"),
  4529. v = i.val(),
  4530. f = this.settings.core.force_text,
  4531. nv;
  4532. if(v === "") { v = t; }
  4533. h1.remove();
  4534. s.replaceWith(a);
  4535. s.remove();
  4536. t = f ? t : $('<div></div>').append($.parseHTML(t)).html();
  4537. obj = this.get_node(obj);
  4538. this.set_text(obj, t);
  4539. nv = !!this.rename_node(obj, f ? $('<div></div>').text(v).text() : $('<div></div>').append($.parseHTML(v)).html());
  4540. if(!nv) {
  4541. this.set_text(obj, t); // move this up? and fix #483
  4542. }
  4543. this._data.core.focused = tmp.id;
  4544. setTimeout(function () {
  4545. var node = this.get_node(tmp.id, true);
  4546. if(node.length) {
  4547. this._data.core.focused = tmp.id;
  4548. node.children('.jstree-anchor').trigger('focus');
  4549. }
  4550. }.bind(this), 0);
  4551. if(callback) {
  4552. callback.call(this, tmp, nv, cancel, v);
  4553. }
  4554. h2 = null;
  4555. }.bind(this),
  4556. "keydown" : function (e) {
  4557. var key = e.which;
  4558. if(key === 27) {
  4559. cancel = true;
  4560. this.value = t;
  4561. }
  4562. if(key === 27 || key === 13 || key === 37 || key === 38 || key === 39 || key === 40 || key === 32) {
  4563. e.stopImmediatePropagation();
  4564. }
  4565. if(key === 27 || key === 13) {
  4566. e.preventDefault();
  4567. this.blur();
  4568. }
  4569. },
  4570. "click" : function (e) { e.stopImmediatePropagation(); },
  4571. "mousedown" : function (e) { e.stopImmediatePropagation(); },
  4572. "keyup" : function (e) {
  4573. h2.width(Math.min(h1.text("pW" + this.value).width(),w));
  4574. },
  4575. "keypress" : function(e) {
  4576. if(e.which === 13) { return false; }
  4577. }
  4578. });
  4579. fn = {
  4580. fontFamily : a.css('fontFamily') || '',
  4581. fontSize : a.css('fontSize') || '',
  4582. fontWeight : a.css('fontWeight') || '',
  4583. fontStyle : a.css('fontStyle') || '',
  4584. fontStretch : a.css('fontStretch') || '',
  4585. fontVariant : a.css('fontVariant') || '',
  4586. letterSpacing : a.css('letterSpacing') || '',
  4587. wordSpacing : a.css('wordSpacing') || ''
  4588. };
  4589. s.attr('class', a.attr('class')).append(a.contents().clone()).append(h2);
  4590. a.replaceWith(s);
  4591. h1.css(fn);
  4592. h2.css(fn).width(Math.min(h1.text("pW" + h2[0].value).width(),w))[0].select();
  4593. $(document).one('mousedown.jstree touchstart.jstree dnd_start.vakata', function (e) {
  4594. if (h2 && e.target !== h2) {
  4595. $(h2).trigger('blur');
  4596. }
  4597. });
  4598. },
  4599. /**
  4600. * changes the theme
  4601. * @name set_theme(theme_name [, theme_url])
  4602. * @param {String} theme_name the name of the new theme to apply
  4603. * @param {mixed} theme_url the location of the CSS file for this theme. Omit or set to `false` if you manually included the file. Set to `true` to autoload from the `core.themes.dir` directory.
  4604. * @trigger set_theme.jstree
  4605. */
  4606. set_theme : function (theme_name, theme_url) {
  4607. if(!theme_name) { return false; }
  4608. if(theme_url === true) {
  4609. var dir = this.settings.core.themes.dir;
  4610. if(!dir) { dir = $.jstree.path + '/themes'; }
  4611. theme_url = dir + '/' + theme_name + '/style.css';
  4612. }
  4613. if(theme_url && $.inArray(theme_url, themes_loaded) === -1) {
  4614. $('head').append('<'+'link rel="stylesheet" href="' + theme_url + '" type="text/css" />');
  4615. themes_loaded.push(theme_url);
  4616. }
  4617. if(this._data.core.themes.name) {
  4618. this.element.removeClass('jstree-' + this._data.core.themes.name);
  4619. }
  4620. this._data.core.themes.name = theme_name;
  4621. this.element.addClass('jstree-' + theme_name);
  4622. this.element[this.settings.core.themes.responsive ? 'addClass' : 'removeClass' ]('jstree-' + theme_name + '-responsive');
  4623. /**
  4624. * triggered when a theme is set
  4625. * @event
  4626. * @name set_theme.jstree
  4627. * @param {String} theme the new theme
  4628. */
  4629. this.trigger('set_theme', { 'theme' : theme_name });
  4630. },
  4631. /**
  4632. * gets the name of the currently applied theme name
  4633. * @name get_theme()
  4634. * @return {String}
  4635. */
  4636. get_theme : function () { return this._data.core.themes.name; },
  4637. /**
  4638. * changes the theme variant (if the theme has variants)
  4639. * @name set_theme_variant(variant_name)
  4640. * @param {String|Boolean} variant_name the variant to apply (if `false` is used the current variant is removed)
  4641. */
  4642. set_theme_variant : function (variant_name) {
  4643. if(this._data.core.themes.variant) {
  4644. this.element.removeClass('jstree-' + this._data.core.themes.name + '-' + this._data.core.themes.variant);
  4645. }
  4646. this._data.core.themes.variant = variant_name;
  4647. if(variant_name) {
  4648. this.element.addClass('jstree-' + this._data.core.themes.name + '-' + this._data.core.themes.variant);
  4649. }
  4650. },
  4651. /**
  4652. * gets the name of the currently applied theme variant
  4653. * @name get_theme()
  4654. * @return {String}
  4655. */
  4656. get_theme_variant : function () { return this._data.core.themes.variant; },
  4657. /**
  4658. * shows a striped background on the container (if the theme supports it)
  4659. * @name show_stripes()
  4660. */
  4661. show_stripes : function () {
  4662. this._data.core.themes.stripes = true;
  4663. this.get_container_ul().addClass("jstree-striped");
  4664. /**
  4665. * triggered when stripes are shown
  4666. * @event
  4667. * @name show_stripes.jstree
  4668. */
  4669. this.trigger('show_stripes');
  4670. },
  4671. /**
  4672. * hides the striped background on the container
  4673. * @name hide_stripes()
  4674. */
  4675. hide_stripes : function () {
  4676. this._data.core.themes.stripes = false;
  4677. this.get_container_ul().removeClass("jstree-striped");
  4678. /**
  4679. * triggered when stripes are hidden
  4680. * @event
  4681. * @name hide_stripes.jstree
  4682. */
  4683. this.trigger('hide_stripes');
  4684. },
  4685. /**
  4686. * toggles the striped background on the container
  4687. * @name toggle_stripes()
  4688. */
  4689. toggle_stripes : function () { if(this._data.core.themes.stripes) { this.hide_stripes(); } else { this.show_stripes(); } },
  4690. /**
  4691. * shows the connecting dots (if the theme supports it)
  4692. * @name show_dots()
  4693. */
  4694. show_dots : function () {
  4695. this._data.core.themes.dots = true;
  4696. this.get_container_ul().removeClass("jstree-no-dots");
  4697. /**
  4698. * triggered when dots are shown
  4699. * @event
  4700. * @name show_dots.jstree
  4701. */
  4702. this.trigger('show_dots');
  4703. },
  4704. /**
  4705. * hides the connecting dots
  4706. * @name hide_dots()
  4707. */
  4708. hide_dots : function () {
  4709. this._data.core.themes.dots = false;
  4710. this.get_container_ul().addClass("jstree-no-dots");
  4711. /**
  4712. * triggered when dots are hidden
  4713. * @event
  4714. * @name hide_dots.jstree
  4715. */
  4716. this.trigger('hide_dots');
  4717. },
  4718. /**
  4719. * toggles the connecting dots
  4720. * @name toggle_dots()
  4721. */
  4722. toggle_dots : function () { if(this._data.core.themes.dots) { this.hide_dots(); } else { this.show_dots(); } },
  4723. /**
  4724. * show the node icons
  4725. * @name show_icons()
  4726. */
  4727. show_icons : function () {
  4728. this._data.core.themes.icons = true;
  4729. this.get_container_ul().removeClass("jstree-no-icons");
  4730. /**
  4731. * triggered when icons are shown
  4732. * @event
  4733. * @name show_icons.jstree
  4734. */
  4735. this.trigger('show_icons');
  4736. },
  4737. /**
  4738. * hide the node icons
  4739. * @name hide_icons()
  4740. */
  4741. hide_icons : function () {
  4742. this._data.core.themes.icons = false;
  4743. this.get_container_ul().addClass("jstree-no-icons");
  4744. /**
  4745. * triggered when icons are hidden
  4746. * @event
  4747. * @name hide_icons.jstree
  4748. */
  4749. this.trigger('hide_icons');
  4750. },
  4751. /**
  4752. * toggle the node icons
  4753. * @name toggle_icons()
  4754. */
  4755. toggle_icons : function () { if(this._data.core.themes.icons) { this.hide_icons(); } else { this.show_icons(); } },
  4756. /**
  4757. * show the node ellipsis
  4758. * @name show_icons()
  4759. */
  4760. show_ellipsis : function () {
  4761. this._data.core.themes.ellipsis = true;
  4762. this.get_container_ul().addClass("jstree-ellipsis");
  4763. /**
  4764. * triggered when ellisis is shown
  4765. * @event
  4766. * @name show_ellipsis.jstree
  4767. */
  4768. this.trigger('show_ellipsis');
  4769. },
  4770. /**
  4771. * hide the node ellipsis
  4772. * @name hide_ellipsis()
  4773. */
  4774. hide_ellipsis : function () {
  4775. this._data.core.themes.ellipsis = false;
  4776. this.get_container_ul().removeClass("jstree-ellipsis");
  4777. /**
  4778. * triggered when ellisis is hidden
  4779. * @event
  4780. * @name hide_ellipsis.jstree
  4781. */
  4782. this.trigger('hide_ellipsis');
  4783. },
  4784. /**
  4785. * toggle the node ellipsis
  4786. * @name toggle_icons()
  4787. */
  4788. toggle_ellipsis : function () { if(this._data.core.themes.ellipsis) { this.hide_ellipsis(); } else { this.show_ellipsis(); } },
  4789. /**
  4790. * set the node icon for a node
  4791. * @name set_icon(obj, icon)
  4792. * @param {mixed} obj
  4793. * @param {String} icon the new icon - can be a path to an icon or a className, if using an image that is in the current directory use a `./` prefix, otherwise it will be detected as a class
  4794. */
  4795. set_icon : function (obj, icon) {
  4796. var t1, t2, dom, old;
  4797. if($.vakata.is_array(obj)) {
  4798. obj = obj.slice();
  4799. for(t1 = 0, t2 = obj.length; t1 < t2; t1++) {
  4800. this.set_icon(obj[t1], icon);
  4801. }
  4802. return true;
  4803. }
  4804. obj = this.get_node(obj);
  4805. if(!obj || obj.id === $.jstree.root) { return false; }
  4806. old = obj.icon;
  4807. obj.icon = icon === true || icon === null || icon === undefined || icon === '' ? true : icon;
  4808. dom = this.get_node(obj, true).children(".jstree-anchor").children(".jstree-themeicon");
  4809. if(icon === false) {
  4810. dom.removeClass('jstree-themeicon-custom ' + old).css("background","").removeAttr("rel");
  4811. this.hide_icon(obj);
  4812. }
  4813. else if(icon === true || icon === null || icon === undefined || icon === '') {
  4814. dom.removeClass('jstree-themeicon-custom ' + old).css("background","").removeAttr("rel");
  4815. if(old === false) { this.show_icon(obj); }
  4816. }
  4817. else if(icon.indexOf("/") === -1 && icon.indexOf(".") === -1) {
  4818. dom.removeClass(old).css("background","");
  4819. dom.addClass(icon + ' jstree-themeicon-custom').attr("rel",icon);
  4820. if(old === false) { this.show_icon(obj); }
  4821. }
  4822. else {
  4823. dom.removeClass(old).css("background","");
  4824. dom.addClass('jstree-themeicon-custom').css("background", "url('" + icon + "') center center no-repeat").attr("rel",icon);
  4825. if(old === false) { this.show_icon(obj); }
  4826. }
  4827. return true;
  4828. },
  4829. /**
  4830. * get the node icon for a node
  4831. * @name get_icon(obj)
  4832. * @param {mixed} obj
  4833. * @return {String}
  4834. */
  4835. get_icon : function (obj) {
  4836. obj = this.get_node(obj);
  4837. return (!obj || obj.id === $.jstree.root) ? false : obj.icon;
  4838. },
  4839. /**
  4840. * hide the icon on an individual node
  4841. * @name hide_icon(obj)
  4842. * @param {mixed} obj
  4843. */
  4844. hide_icon : function (obj) {
  4845. var t1, t2;
  4846. if($.vakata.is_array(obj)) {
  4847. obj = obj.slice();
  4848. for(t1 = 0, t2 = obj.length; t1 < t2; t1++) {
  4849. this.hide_icon(obj[t1]);
  4850. }
  4851. return true;
  4852. }
  4853. obj = this.get_node(obj);
  4854. if(!obj || obj === $.jstree.root) { return false; }
  4855. obj.icon = false;
  4856. this.get_node(obj, true).children(".jstree-anchor").children(".jstree-themeicon").addClass('jstree-themeicon-hidden');
  4857. return true;
  4858. },
  4859. /**
  4860. * show the icon on an individual node
  4861. * @name show_icon(obj)
  4862. * @param {mixed} obj
  4863. */
  4864. show_icon : function (obj) {
  4865. var t1, t2, dom;
  4866. if($.vakata.is_array(obj)) {
  4867. obj = obj.slice();
  4868. for(t1 = 0, t2 = obj.length; t1 < t2; t1++) {
  4869. this.show_icon(obj[t1]);
  4870. }
  4871. return true;
  4872. }
  4873. obj = this.get_node(obj);
  4874. if(!obj || obj === $.jstree.root) { return false; }
  4875. dom = this.get_node(obj, true);
  4876. obj.icon = dom.length ? dom.children(".jstree-anchor").children(".jstree-themeicon").attr('rel') : true;
  4877. if(!obj.icon) { obj.icon = true; }
  4878. dom.children(".jstree-anchor").children(".jstree-themeicon").removeClass('jstree-themeicon-hidden');
  4879. return true;
  4880. }
  4881. };
  4882. // helpers
  4883. $.vakata = {};
  4884. // collect attributes
  4885. $.vakata.attributes = function(node, with_values) {
  4886. node = $(node)[0];
  4887. var attr = with_values ? {} : [];
  4888. if(node && node.attributes) {
  4889. $.each(node.attributes, function (i, v) {
  4890. if($.inArray(v.name.toLowerCase(),['style','contenteditable','hasfocus','tabindex']) !== -1) { return; }
  4891. if(v.value !== null && $.vakata.trim(v.value) !== '') {
  4892. if(with_values) { attr[v.name] = v.value; }
  4893. else { attr.push(v.name); }
  4894. }
  4895. });
  4896. }
  4897. return attr;
  4898. };
  4899. $.vakata.array_unique = function(array) {
  4900. var a = [], i, j, l, o = {};
  4901. for(i = 0, l = array.length; i < l; i++) {
  4902. if(o[array[i]] === undefined) {
  4903. a.push(array[i]);
  4904. o[array[i]] = true;
  4905. }
  4906. }
  4907. return a;
  4908. };
  4909. // remove item from array
  4910. $.vakata.array_remove = function(array, from) {
  4911. array.splice(from, 1);
  4912. return array;
  4913. //var rest = array.slice((to || from) + 1 || array.length);
  4914. //array.length = from < 0 ? array.length + from : from;
  4915. //array.push.apply(array, rest);
  4916. //return array;
  4917. };
  4918. // remove item from array
  4919. $.vakata.array_remove_item = function(array, item) {
  4920. var tmp = $.inArray(item, array);
  4921. return tmp !== -1 ? $.vakata.array_remove(array, tmp) : array;
  4922. };
  4923. $.vakata.array_filter = function(c,a,b,d,e) {
  4924. if (c.filter) {
  4925. return c.filter(a, b);
  4926. }
  4927. d=[];
  4928. for (e in c) {
  4929. if (~~e+''===e+'' && e>=0 && a.call(b,c[e],+e,c)) {
  4930. d.push(c[e]);
  4931. }
  4932. }
  4933. return d;
  4934. };
  4935. $.vakata.trim = function (text) {
  4936. return String.prototype.trim ?
  4937. String.prototype.trim.call(text.toString()) :
  4938. text.toString().replace(/^[\s\uFEFF\xA0]+|[\s\uFEFF\xA0]+$/g, '');
  4939. };
  4940. $.vakata.is_function = function(obj) {
  4941. return typeof obj === "function" && typeof obj.nodeType !== "number";
  4942. };
  4943. $.vakata.is_array = Array.isArray || function (obj) {
  4944. return Object.prototype.toString.call(obj) === "[object Array]";
  4945. };
  4946. // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_objects/Function/bind#polyfill
  4947. if (!Function.prototype.bind) {
  4948. Function.prototype.bind = function () {
  4949. var thatFunc = this, thatArg = arguments[0];
  4950. var args = Array.prototype.slice.call(arguments, 1);
  4951. if (typeof thatFunc !== 'function') {
  4952. // closest thing possible to the ECMAScript 5
  4953. // internal IsCallable function
  4954. throw new TypeError('Function.prototype.bind - what is trying to be bound is not callable');
  4955. }
  4956. return function(){
  4957. var funcArgs = args.concat(Array.prototype.slice.call(arguments));
  4958. return thatFunc.apply(thatArg, funcArgs);
  4959. };
  4960. };
  4961. }
  4962. }));