jquery.ztree.all-3.5.js 118 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535
  1. /*
  2. * JQuery zTree core v3.5.18
  3. * http://zTree.me/
  4. *
  5. * Copyright (c) 2010 Hunter.z
  6. *
  7. * Licensed same as jquery - MIT License
  8. * http://www.opensource.org/licenses/mit-license.php
  9. *
  10. * email: hunter.z@263.net
  11. * Date: 2015-05-25
  12. */
  13. (function($){
  14. var settings = {}, roots = {}, caches = {},
  15. //default consts of core
  16. _consts = {
  17. className: {
  18. BUTTON: "button",
  19. LEVEL: "level",
  20. ICO_LOADING: "ico_loading",
  21. SWITCH: "switch"
  22. },
  23. event: {
  24. NODECREATED: "ztree_nodeCreated",
  25. CLICK: "ztree_click",
  26. EXPAND: "ztree_expand",
  27. COLLAPSE: "ztree_collapse",
  28. ASYNC_SUCCESS: "ztree_async_success",
  29. ASYNC_ERROR: "ztree_async_error",
  30. REMOVE: "ztree_remove",
  31. SELECTED: "ztree_selected",
  32. UNSELECTED: "ztree_unselected"
  33. },
  34. id: {
  35. A: "_a",
  36. ICON: "_ico",
  37. SPAN: "_span",
  38. SWITCH: "_switch",
  39. UL: "_ul"
  40. },
  41. line: {
  42. ROOT: "root",
  43. ROOTS: "roots",
  44. CENTER: "center",
  45. BOTTOM: "bottom",
  46. NOLINE: "noline",
  47. LINE: "line"
  48. },
  49. folder: {
  50. OPEN: "open",
  51. CLOSE: "close",
  52. DOCU: "docu"
  53. },
  54. node: {
  55. CURSELECTED: "curSelectedNode"
  56. }
  57. },
  58. //default setting of core
  59. _setting = {
  60. treeId: "",
  61. treeObj: null,
  62. view: {
  63. addDiyDom: null,
  64. autoCancelSelected: true,
  65. dblClickExpand: true,
  66. expandSpeed: "fast",
  67. fontCss: {},
  68. nameIsHTML: false,
  69. selectedMulti: true,
  70. showIcon: true,
  71. showLine: true,
  72. showTitle: true,
  73. txtSelectedEnable: false
  74. },
  75. data: {
  76. key: {
  77. children: "children",
  78. name: "name",
  79. title: "",
  80. url: "url"
  81. },
  82. simpleData: {
  83. enable: false,
  84. idKey: "id",
  85. pIdKey: "pId",
  86. rootPId: null
  87. },
  88. keep: {
  89. parent: false,
  90. leaf: false
  91. }
  92. },
  93. async: {
  94. enable: false,
  95. contentType: "application/x-www-form-urlencoded",
  96. type: "post",
  97. dataType: "text",
  98. url: "",
  99. autoParam: [],
  100. otherParam: [],
  101. dataFilter: null
  102. },
  103. callback: {
  104. beforeAsync:null,
  105. beforeClick:null,
  106. beforeDblClick:null,
  107. beforeRightClick:null,
  108. beforeMouseDown:null,
  109. beforeMouseUp:null,
  110. beforeExpand:null,
  111. beforeCollapse:null,
  112. beforeRemove:null,
  113. onAsyncError:null,
  114. onAsyncSuccess:null,
  115. onNodeCreated:null,
  116. onClick:null,
  117. onDblClick:null,
  118. onRightClick:null,
  119. onMouseDown:null,
  120. onMouseUp:null,
  121. onExpand:null,
  122. onCollapse:null,
  123. onRemove:null
  124. }
  125. },
  126. //default root of core
  127. //zTree use root to save full data
  128. _initRoot = function (setting) {
  129. var r = data.getRoot(setting);
  130. if (!r) {
  131. r = {};
  132. data.setRoot(setting, r);
  133. }
  134. r[setting.data.key.children] = [];
  135. r.expandTriggerFlag = false;
  136. r.curSelectedList = [];
  137. r.noSelection = true;
  138. r.createdNodes = [];
  139. r.zId = 0;
  140. r._ver = (new Date()).getTime();
  141. },
  142. //default cache of core
  143. _initCache = function(setting) {
  144. var c = data.getCache(setting);
  145. if (!c) {
  146. c = {};
  147. data.setCache(setting, c);
  148. }
  149. c.nodes = [];
  150. c.doms = [];
  151. },
  152. //default bindEvent of core
  153. _bindEvent = function(setting) {
  154. var o = setting.treeObj,
  155. c = consts.event;
  156. o.bind(c.NODECREATED, function (event, treeId, node) {
  157. tools.apply(setting.callback.onNodeCreated, [event, treeId, node]);
  158. });
  159. o.bind(c.CLICK, function (event, srcEvent, treeId, node, clickFlag) {
  160. tools.apply(setting.callback.onClick, [srcEvent, treeId, node, clickFlag]);
  161. });
  162. o.bind(c.EXPAND, function (event, treeId, node) {
  163. tools.apply(setting.callback.onExpand, [event, treeId, node]);
  164. });
  165. o.bind(c.COLLAPSE, function (event, treeId, node) {
  166. tools.apply(setting.callback.onCollapse, [event, treeId, node]);
  167. });
  168. o.bind(c.ASYNC_SUCCESS, function (event, treeId, node, msg) {
  169. tools.apply(setting.callback.onAsyncSuccess, [event, treeId, node, msg]);
  170. });
  171. o.bind(c.ASYNC_ERROR, function (event, treeId, node, XMLHttpRequest, textStatus, errorThrown) {
  172. tools.apply(setting.callback.onAsyncError, [event, treeId, node, XMLHttpRequest, textStatus, errorThrown]);
  173. });
  174. o.bind(c.REMOVE, function (event, treeId, treeNode) {
  175. tools.apply(setting.callback.onRemove, [event, treeId, treeNode]);
  176. });
  177. o.bind(c.SELECTED, function (event, srcEvent, treeId, node) {
  178. tools.apply(setting.callback.onSelected, [srcEvent, treeId, node]);
  179. });
  180. o.bind(c.UNSELECTED, function (event, srcEvent, treeId, node) {
  181. tools.apply(setting.callback.onUnSelected, [srcEvent, treeId, node]);
  182. });
  183. },
  184. _unbindEvent = function(setting) {
  185. var o = setting.treeObj,
  186. c = consts.event;
  187. o.unbind(c.NODECREATED)
  188. .unbind(c.CLICK)
  189. .unbind(c.EXPAND)
  190. .unbind(c.COLLAPSE)
  191. .unbind(c.ASYNC_SUCCESS)
  192. .unbind(c.ASYNC_ERROR)
  193. .unbind(c.REMOVE)
  194. .unbind(c.SELECTED)
  195. .unbind(c.UNSELECTED);
  196. },
  197. //default event proxy of core
  198. _eventProxy = function(event) {
  199. var target = event.target,
  200. setting = data.getSetting(event.data.treeId),
  201. tId = "", node = null,
  202. nodeEventType = "", treeEventType = "",
  203. nodeEventCallback = null, treeEventCallback = null,
  204. tmp = null;
  205. if (tools.eqs(event.type, "mousedown")) {
  206. treeEventType = "mousedown";
  207. } else if (tools.eqs(event.type, "mouseup")) {
  208. treeEventType = "mouseup";
  209. } else if (tools.eqs(event.type, "contextmenu")) {
  210. treeEventType = "contextmenu";
  211. } else if (tools.eqs(event.type, "click")) {
  212. if (tools.eqs(target.tagName, "span") && target.getAttribute("treeNode"+ consts.id.SWITCH) !== null) {
  213. tId = tools.getNodeMainDom(target).id;
  214. nodeEventType = "switchNode";
  215. } else {
  216. tmp = tools.getMDom(setting, target, [{tagName:"a", attrName:"treeNode"+consts.id.A}]);
  217. if (tmp) {
  218. tId = tools.getNodeMainDom(tmp).id;
  219. nodeEventType = "clickNode";
  220. }
  221. }
  222. } else if (tools.eqs(event.type, "dblclick")) {
  223. treeEventType = "dblclick";
  224. tmp = tools.getMDom(setting, target, [{tagName:"a", attrName:"treeNode"+consts.id.A}]);
  225. if (tmp) {
  226. tId = tools.getNodeMainDom(tmp).id;
  227. nodeEventType = "switchNode";
  228. }
  229. }
  230. if (treeEventType.length > 0 && tId.length == 0) {
  231. tmp = tools.getMDom(setting, target, [{tagName:"a", attrName:"treeNode"+consts.id.A}]);
  232. if (tmp) {tId = tools.getNodeMainDom(tmp).id;}
  233. }
  234. // event to node
  235. if (tId.length>0) {
  236. node = data.getNodeCache(setting, tId);
  237. switch (nodeEventType) {
  238. case "switchNode" :
  239. if (!node.isParent) {
  240. nodeEventType = "";
  241. } else if (tools.eqs(event.type, "click")
  242. || (tools.eqs(event.type, "dblclick") && tools.apply(setting.view.dblClickExpand, [setting.treeId, node], setting.view.dblClickExpand))) {
  243. nodeEventCallback = handler.onSwitchNode;
  244. } else {
  245. nodeEventType = "";
  246. }
  247. break;
  248. case "clickNode" :
  249. nodeEventCallback = handler.onClickNode;
  250. break;
  251. }
  252. }
  253. // event to zTree
  254. switch (treeEventType) {
  255. case "mousedown" :
  256. treeEventCallback = handler.onZTreeMousedown;
  257. break;
  258. case "mouseup" :
  259. treeEventCallback = handler.onZTreeMouseup;
  260. break;
  261. case "dblclick" :
  262. treeEventCallback = handler.onZTreeDblclick;
  263. break;
  264. case "contextmenu" :
  265. treeEventCallback = handler.onZTreeContextmenu;
  266. break;
  267. }
  268. var proxyResult = {
  269. stop: false,
  270. node: node,
  271. nodeEventType: nodeEventType,
  272. nodeEventCallback: nodeEventCallback,
  273. treeEventType: treeEventType,
  274. treeEventCallback: treeEventCallback
  275. };
  276. return proxyResult
  277. },
  278. //default init node of core
  279. _initNode = function(setting, level, n, parentNode, isFirstNode, isLastNode, openFlag) {
  280. if (!n) return;
  281. var r = data.getRoot(setting),
  282. childKey = setting.data.key.children;
  283. n.level = level;
  284. n.tId = setting.treeId + "_" + (++r.zId);
  285. n.parentTId = parentNode ? parentNode.tId : null;
  286. n.open = (typeof n.open == "string") ? tools.eqs(n.open, "true") : !!n.open;
  287. if (n[childKey] && n[childKey].length > 0) {
  288. n.isParent = true;
  289. n.zAsync = true;
  290. } else {
  291. n.isParent = (typeof n.isParent == "string") ? tools.eqs(n.isParent, "true") : !!n.isParent;
  292. n.open = (n.isParent && !setting.async.enable) ? n.open : false;
  293. n.zAsync = !n.isParent;
  294. }
  295. n.isFirstNode = isFirstNode;
  296. n.isLastNode = isLastNode;
  297. n.getParentNode = function() {return data.getNodeCache(setting, n.parentTId);};
  298. n.getPreNode = function() {return data.getPreNode(setting, n);};
  299. n.getNextNode = function() {return data.getNextNode(setting, n);};
  300. n.isAjaxing = false;
  301. data.fixPIdKeyValue(setting, n);
  302. },
  303. _init = {
  304. bind: [_bindEvent],
  305. unbind: [_unbindEvent],
  306. caches: [_initCache],
  307. nodes: [_initNode],
  308. proxys: [_eventProxy],
  309. roots: [_initRoot],
  310. beforeA: [],
  311. afterA: [],
  312. innerBeforeA: [],
  313. innerAfterA: [],
  314. zTreeTools: []
  315. },
  316. //method of operate data
  317. data = {
  318. addNodeCache: function(setting, node) {
  319. data.getCache(setting).nodes[data.getNodeCacheId(node.tId)] = node;
  320. },
  321. getNodeCacheId: function(tId) {
  322. return tId.substring(tId.lastIndexOf("_")+1);
  323. },
  324. addAfterA: function(afterA) {
  325. _init.afterA.push(afterA);
  326. },
  327. addBeforeA: function(beforeA) {
  328. _init.beforeA.push(beforeA);
  329. },
  330. addInnerAfterA: function(innerAfterA) {
  331. _init.innerAfterA.push(innerAfterA);
  332. },
  333. addInnerBeforeA: function(innerBeforeA) {
  334. _init.innerBeforeA.push(innerBeforeA);
  335. },
  336. addInitBind: function(bindEvent) {
  337. _init.bind.push(bindEvent);
  338. },
  339. addInitUnBind: function(unbindEvent) {
  340. _init.unbind.push(unbindEvent);
  341. },
  342. addInitCache: function(initCache) {
  343. _init.caches.push(initCache);
  344. },
  345. addInitNode: function(initNode) {
  346. _init.nodes.push(initNode);
  347. },
  348. addInitProxy: function(initProxy, isFirst) {
  349. if (!!isFirst) {
  350. _init.proxys.splice(0,0,initProxy);
  351. } else {
  352. _init.proxys.push(initProxy);
  353. }
  354. },
  355. addInitRoot: function(initRoot) {
  356. _init.roots.push(initRoot);
  357. },
  358. addNodesData: function(setting, parentNode, nodes) {
  359. var childKey = setting.data.key.children;
  360. if (!parentNode[childKey]) parentNode[childKey] = [];
  361. if (parentNode[childKey].length > 0) {
  362. parentNode[childKey][parentNode[childKey].length - 1].isLastNode = false;
  363. view.setNodeLineIcos(setting, parentNode[childKey][parentNode[childKey].length - 1]);
  364. }
  365. parentNode.isParent = true;
  366. parentNode[childKey] = parentNode[childKey].concat(nodes);
  367. },
  368. addSelectedNode: function(setting, node) {
  369. var root = data.getRoot(setting);
  370. if (!data.isSelectedNode(setting, node)) {
  371. root.curSelectedList.push(node);
  372. }
  373. },
  374. addCreatedNode: function(setting, node) {
  375. if (!!setting.callback.onNodeCreated || !!setting.view.addDiyDom) {
  376. var root = data.getRoot(setting);
  377. root.createdNodes.push(node);
  378. }
  379. },
  380. addZTreeTools: function(zTreeTools) {
  381. _init.zTreeTools.push(zTreeTools);
  382. },
  383. exSetting: function(s) {
  384. $.extend(true, _setting, s);
  385. },
  386. fixPIdKeyValue: function(setting, node) {
  387. if (setting.data.simpleData.enable) {
  388. node[setting.data.simpleData.pIdKey] = node.parentTId ? node.getParentNode()[setting.data.simpleData.idKey] : setting.data.simpleData.rootPId;
  389. }
  390. },
  391. getAfterA: function(setting, node, array) {
  392. for (var i=0, j=_init.afterA.length; i<j; i++) {
  393. _init.afterA[i].apply(this, arguments);
  394. }
  395. },
  396. getBeforeA: function(setting, node, array) {
  397. for (var i=0, j=_init.beforeA.length; i<j; i++) {
  398. _init.beforeA[i].apply(this, arguments);
  399. }
  400. },
  401. getInnerAfterA: function(setting, node, array) {
  402. for (var i=0, j=_init.innerAfterA.length; i<j; i++) {
  403. _init.innerAfterA[i].apply(this, arguments);
  404. }
  405. },
  406. getInnerBeforeA: function(setting, node, array) {
  407. for (var i=0, j=_init.innerBeforeA.length; i<j; i++) {
  408. _init.innerBeforeA[i].apply(this, arguments);
  409. }
  410. },
  411. getCache: function(setting) {
  412. return caches[setting.treeId];
  413. },
  414. getNextNode: function(setting, node) {
  415. if (!node) return null;
  416. var childKey = setting.data.key.children,
  417. p = node.parentTId ? node.getParentNode() : data.getRoot(setting);
  418. for (var i=0, l=p[childKey].length-1; i<=l; i++) {
  419. if (p[childKey][i] === node) {
  420. return (i==l ? null : p[childKey][i+1]);
  421. }
  422. }
  423. return null;
  424. },
  425. getNodeByParam: function(setting, nodes, key, value) {
  426. if (!nodes || !key) return null;
  427. var childKey = setting.data.key.children;
  428. for (var i = 0, l = nodes.length; i < l; i++) {
  429. if (nodes[i][key] == value) {
  430. return nodes[i];
  431. }
  432. var tmp = data.getNodeByParam(setting, nodes[i][childKey], key, value);
  433. if (tmp) return tmp;
  434. }
  435. return null;
  436. },
  437. getNodeCache: function(setting, tId) {
  438. if (!tId) return null;
  439. var n = caches[setting.treeId].nodes[data.getNodeCacheId(tId)];
  440. return n ? n : null;
  441. },
  442. getNodeName: function(setting, node) {
  443. var nameKey = setting.data.key.name;
  444. return "" + node[nameKey];
  445. },
  446. getNodeTitle: function(setting, node) {
  447. var t = setting.data.key.title === "" ? setting.data.key.name : setting.data.key.title;
  448. return "" + node[t];
  449. },
  450. getNodes: function(setting) {
  451. return data.getRoot(setting)[setting.data.key.children];
  452. },
  453. getNodesByParam: function(setting, nodes, key, value) {
  454. if (!nodes || !key) return [];
  455. var childKey = setting.data.key.children,
  456. result = [];
  457. for (var i = 0, l = nodes.length; i < l; i++) {
  458. if (nodes[i][key] == value) {
  459. result.push(nodes[i]);
  460. }
  461. result = result.concat(data.getNodesByParam(setting, nodes[i][childKey], key, value));
  462. }
  463. return result;
  464. },
  465. getNodesByParamFuzzy: function(setting, nodes, key, value) {
  466. if (!nodes || !key) return [];
  467. var childKey = setting.data.key.children,
  468. result = [];
  469. value = value.toLowerCase();
  470. for (var i = 0, l = nodes.length; i < l; i++) {
  471. if (typeof nodes[i][key] == "string" && nodes[i][key].toLowerCase().indexOf(value)>-1) {
  472. result.push(nodes[i]);
  473. }
  474. result = result.concat(data.getNodesByParamFuzzy(setting, nodes[i][childKey], key, value));
  475. }
  476. return result;
  477. },
  478. getNodesByFilter: function(setting, nodes, filter, isSingle, invokeParam) {
  479. if (!nodes) return (isSingle ? null : []);
  480. var childKey = setting.data.key.children,
  481. result = isSingle ? null : [];
  482. for (var i = 0, l = nodes.length; i < l; i++) {
  483. if (tools.apply(filter, [nodes[i], invokeParam], false)) {
  484. if (isSingle) {return nodes[i];}
  485. result.push(nodes[i]);
  486. }
  487. var tmpResult = data.getNodesByFilter(setting, nodes[i][childKey], filter, isSingle, invokeParam);
  488. if (isSingle && !!tmpResult) {return tmpResult;}
  489. result = isSingle ? tmpResult : result.concat(tmpResult);
  490. }
  491. return result;
  492. },
  493. getPreNode: function(setting, node) {
  494. if (!node) return null;
  495. var childKey = setting.data.key.children,
  496. p = node.parentTId ? node.getParentNode() : data.getRoot(setting);
  497. for (var i=0, l=p[childKey].length; i<l; i++) {
  498. if (p[childKey][i] === node) {
  499. return (i==0 ? null : p[childKey][i-1]);
  500. }
  501. }
  502. return null;
  503. },
  504. getRoot: function(setting) {
  505. return setting ? roots[setting.treeId] : null;
  506. },
  507. getRoots: function() {
  508. return roots;
  509. },
  510. getSetting: function(treeId) {
  511. return settings[treeId];
  512. },
  513. getSettings: function() {
  514. return settings;
  515. },
  516. getZTreeTools: function(treeId) {
  517. var r = this.getRoot(this.getSetting(treeId));
  518. return r ? r.treeTools : null;
  519. },
  520. initCache: function(setting) {
  521. for (var i=0, j=_init.caches.length; i<j; i++) {
  522. _init.caches[i].apply(this, arguments);
  523. }
  524. },
  525. initNode: function(setting, level, node, parentNode, preNode, nextNode) {
  526. for (var i=0, j=_init.nodes.length; i<j; i++) {
  527. _init.nodes[i].apply(this, arguments);
  528. }
  529. },
  530. initRoot: function(setting) {
  531. for (var i=0, j=_init.roots.length; i<j; i++) {
  532. _init.roots[i].apply(this, arguments);
  533. }
  534. },
  535. isSelectedNode: function(setting, node) {
  536. var root = data.getRoot(setting);
  537. for (var i=0, j=root.curSelectedList.length; i<j; i++) {
  538. if(node === root.curSelectedList[i]) return true;
  539. }
  540. return false;
  541. },
  542. removeNodeCache: function(setting, node) {
  543. var childKey = setting.data.key.children;
  544. if (node[childKey]) {
  545. for (var i=0, l=node[childKey].length; i<l; i++) {
  546. arguments.callee(setting, node[childKey][i]);
  547. }
  548. }
  549. data.getCache(setting).nodes[data.getNodeCacheId(node.tId)] = null;
  550. },
  551. removeSelectedNode: function(setting, node) {
  552. var root = data.getRoot(setting);
  553. for (var i=0, j=root.curSelectedList.length; i<j; i++) {
  554. if(node === root.curSelectedList[i] || !data.getNodeCache(setting, root.curSelectedList[i].tId)) {
  555. root.curSelectedList.splice(i, 1);
  556. i--;j--;
  557. }
  558. }
  559. },
  560. setCache: function(setting, cache) {
  561. caches[setting.treeId] = cache;
  562. },
  563. setRoot: function(setting, root) {
  564. roots[setting.treeId] = root;
  565. },
  566. setZTreeTools: function(setting, zTreeTools) {
  567. for (var i=0, j=_init.zTreeTools.length; i<j; i++) {
  568. _init.zTreeTools[i].apply(this, arguments);
  569. }
  570. },
  571. transformToArrayFormat: function (setting, nodes) {
  572. if (!nodes) return [];
  573. var childKey = setting.data.key.children,
  574. r = [];
  575. if (tools.isArray(nodes)) {
  576. for (var i=0, l=nodes.length; i<l; i++) {
  577. r.push(nodes[i]);
  578. if (nodes[i][childKey])
  579. r = r.concat(data.transformToArrayFormat(setting, nodes[i][childKey]));
  580. }
  581. } else {
  582. r.push(nodes);
  583. if (nodes[childKey])
  584. r = r.concat(data.transformToArrayFormat(setting, nodes[childKey]));
  585. }
  586. return r;
  587. },
  588. transformTozTreeFormat: function(setting, sNodes) {
  589. var i,l,
  590. key = setting.data.simpleData.idKey,
  591. parentKey = setting.data.simpleData.pIdKey,
  592. childKey = setting.data.key.children;
  593. if (!key || key=="" || !sNodes) return [];
  594. if (tools.isArray(sNodes)) {
  595. var r = [];
  596. var tmpMap = [];
  597. for (i=0, l=sNodes.length; i<l; i++) {
  598. tmpMap[sNodes[i][key]] = sNodes[i];
  599. }
  600. for (i=0, l=sNodes.length; i<l; i++) {
  601. if (tmpMap[sNodes[i][parentKey]] && sNodes[i][key] != sNodes[i][parentKey]) {
  602. if (!tmpMap[sNodes[i][parentKey]][childKey])
  603. tmpMap[sNodes[i][parentKey]][childKey] = [];
  604. tmpMap[sNodes[i][parentKey]][childKey].push(sNodes[i]);
  605. } else {
  606. r.push(sNodes[i]);
  607. }
  608. }
  609. return r;
  610. }else {
  611. return [sNodes];
  612. }
  613. }
  614. },
  615. //method of event proxy
  616. event = {
  617. bindEvent: function(setting) {
  618. for (var i=0, j=_init.bind.length; i<j; i++) {
  619. _init.bind[i].apply(this, arguments);
  620. }
  621. },
  622. unbindEvent: function(setting) {
  623. for (var i=0, j=_init.unbind.length; i<j; i++) {
  624. _init.unbind[i].apply(this, arguments);
  625. }
  626. },
  627. bindTree: function(setting) {
  628. var eventParam = {
  629. treeId: setting.treeId
  630. },
  631. o = setting.treeObj;
  632. if (!setting.view.txtSelectedEnable) {
  633. // for can't select text
  634. o.bind('selectstart', function(e){
  635. var node
  636. var n = e.originalEvent.srcElement.nodeName.toLowerCase();
  637. return (n === "input" || n === "textarea" );
  638. }).css({
  639. "-moz-user-select":"-moz-none"
  640. });
  641. }
  642. o.bind('click', eventParam, event.proxy);
  643. o.bind('dblclick', eventParam, event.proxy);
  644. o.bind('mouseover', eventParam, event.proxy);
  645. o.bind('mouseout', eventParam, event.proxy);
  646. o.bind('mousedown', eventParam, event.proxy);
  647. o.bind('mouseup', eventParam, event.proxy);
  648. o.bind('contextmenu', eventParam, event.proxy);
  649. },
  650. unbindTree: function(setting) {
  651. var o = setting.treeObj;
  652. o.unbind('click', event.proxy)
  653. .unbind('dblclick', event.proxy)
  654. .unbind('mouseover', event.proxy)
  655. .unbind('mouseout', event.proxy)
  656. .unbind('mousedown', event.proxy)
  657. .unbind('mouseup', event.proxy)
  658. .unbind('contextmenu', event.proxy);
  659. },
  660. doProxy: function(e) {
  661. var results = [];
  662. for (var i=0, j=_init.proxys.length; i<j; i++) {
  663. var proxyResult = _init.proxys[i].apply(this, arguments);
  664. results.push(proxyResult);
  665. if (proxyResult.stop) {
  666. break;
  667. }
  668. }
  669. return results;
  670. },
  671. proxy: function(e) {
  672. var setting = data.getSetting(e.data.treeId);
  673. if (!tools.uCanDo(setting, e)) return true;
  674. var results = event.doProxy(e),
  675. r = true, x = false;
  676. for (var i=0, l=results.length; i<l; i++) {
  677. var proxyResult = results[i];
  678. if (proxyResult.nodeEventCallback) {
  679. x = true;
  680. r = proxyResult.nodeEventCallback.apply(proxyResult, [e, proxyResult.node]) && r;
  681. }
  682. if (proxyResult.treeEventCallback) {
  683. x = true;
  684. r = proxyResult.treeEventCallback.apply(proxyResult, [e, proxyResult.node]) && r;
  685. }
  686. }
  687. return r;
  688. }
  689. },
  690. //method of event handler
  691. handler = {
  692. onSwitchNode: function (event, node) {
  693. var setting = data.getSetting(event.data.treeId);
  694. if (node.open) {
  695. if (tools.apply(setting.callback.beforeCollapse, [setting.treeId, node], true) == false) return true;
  696. data.getRoot(setting).expandTriggerFlag = true;
  697. view.switchNode(setting, node);
  698. } else {
  699. if (tools.apply(setting.callback.beforeExpand, [setting.treeId, node], true) == false) return true;
  700. data.getRoot(setting).expandTriggerFlag = true;
  701. view.switchNode(setting, node);
  702. }
  703. return true;
  704. },
  705. onClickNode: function (event, node) {
  706. var setting = data.getSetting(event.data.treeId),
  707. clickFlag = ( (setting.view.autoCancelSelected && (event.ctrlKey || event.metaKey)) && data.isSelectedNode(setting, node)) ? 0 : (setting.view.autoCancelSelected && (event.ctrlKey || event.metaKey) && setting.view.selectedMulti) ? 2 : 1;
  708. if (tools.apply(setting.callback.beforeClick, [setting.treeId, node, clickFlag], true) == false) return true;
  709. if (clickFlag === 0) {
  710. view.cancelPreSelectedNode(setting, node);
  711. } else {
  712. view.selectNode(setting, node, clickFlag === 2);
  713. }
  714. setting.treeObj.trigger(consts.event.CLICK, [event, setting.treeId, node, clickFlag]);
  715. return true;
  716. },
  717. onZTreeMousedown: function(event, node) {
  718. var setting = data.getSetting(event.data.treeId);
  719. if (tools.apply(setting.callback.beforeMouseDown, [setting.treeId, node], true)) {
  720. tools.apply(setting.callback.onMouseDown, [event, setting.treeId, node]);
  721. }
  722. return true;
  723. },
  724. onZTreeMouseup: function(event, node) {
  725. var setting = data.getSetting(event.data.treeId);
  726. if (tools.apply(setting.callback.beforeMouseUp, [setting.treeId, node], true)) {
  727. tools.apply(setting.callback.onMouseUp, [event, setting.treeId, node]);
  728. }
  729. return true;
  730. },
  731. onZTreeDblclick: function(event, node) {
  732. var setting = data.getSetting(event.data.treeId);
  733. if (tools.apply(setting.callback.beforeDblClick, [setting.treeId, node], true)) {
  734. tools.apply(setting.callback.onDblClick, [event, setting.treeId, node]);
  735. }
  736. return true;
  737. },
  738. onZTreeContextmenu: function(event, node) {
  739. var setting = data.getSetting(event.data.treeId);
  740. if (tools.apply(setting.callback.beforeRightClick, [setting.treeId, node], true)) {
  741. tools.apply(setting.callback.onRightClick, [event, setting.treeId, node]);
  742. }
  743. return (typeof setting.callback.onRightClick) != "function";
  744. }
  745. },
  746. //method of tools for zTree
  747. tools = {
  748. apply: function(fun, param, defaultValue) {
  749. if ((typeof fun) == "function") {
  750. return fun.apply(zt, param?param:[]);
  751. }
  752. return defaultValue;
  753. },
  754. canAsync: function(setting, node) {
  755. var childKey = setting.data.key.children;
  756. return (setting.async.enable && node && node.isParent && !(node.zAsync || (node[childKey] && node[childKey].length > 0)));
  757. },
  758. clone: function (obj){
  759. if (obj === null) return null;
  760. var o = tools.isArray(obj) ? [] : {};
  761. for(var i in obj){
  762. o[i] = (obj[i] instanceof Date) ? new Date(obj[i].getTime()) : (typeof obj[i] === "object" ? arguments.callee(obj[i]) : obj[i]);
  763. }
  764. return o;
  765. },
  766. eqs: function(str1, str2) {
  767. return str1.toLowerCase() === str2.toLowerCase();
  768. },
  769. isArray: function(arr) {
  770. return Object.prototype.toString.apply(arr) === "[object Array]";
  771. },
  772. $: function(node, exp, setting) {
  773. if (!!exp && typeof exp != "string") {
  774. setting = exp;
  775. exp = "";
  776. }
  777. if (typeof node == "string") {
  778. return $(node, setting ? setting.treeObj.get(0).ownerDocument : null);
  779. } else {
  780. return $("#" + node.tId + exp, setting ? setting.treeObj : null);
  781. }
  782. },
  783. getMDom: function (setting, curDom, targetExpr) {
  784. if (!curDom) return null;
  785. while (curDom && curDom.id !== setting.treeId) {
  786. for (var i=0, l=targetExpr.length; curDom.tagName && i<l; i++) {
  787. if (tools.eqs(curDom.tagName, targetExpr[i].tagName) && curDom.getAttribute(targetExpr[i].attrName) !== null) {
  788. return curDom;
  789. }
  790. }
  791. curDom = curDom.parentNode;
  792. }
  793. return null;
  794. },
  795. getNodeMainDom:function(target) {
  796. return ($(target).parent("li").get(0) || $(target).parentsUntil("li").parent().get(0));
  797. },
  798. isChildOrSelf: function(dom, parentId) {
  799. return ( $(dom).closest("#" + parentId).length> 0 );
  800. },
  801. uCanDo: function(setting, e) {
  802. return true;
  803. }
  804. },
  805. //method of operate ztree dom
  806. view = {
  807. addNodes: function(setting, parentNode, newNodes, isSilent) {
  808. if (setting.data.keep.leaf && parentNode && !parentNode.isParent) {
  809. return;
  810. }
  811. if (!tools.isArray(newNodes)) {
  812. newNodes = [newNodes];
  813. }
  814. if (setting.data.simpleData.enable) {
  815. newNodes = data.transformTozTreeFormat(setting, newNodes);
  816. }
  817. if (parentNode) {
  818. var target_switchObj = $$(parentNode, consts.id.SWITCH, setting),
  819. target_icoObj = $$(parentNode, consts.id.ICON, setting),
  820. target_ulObj = $$(parentNode, consts.id.UL, setting);
  821. if (!parentNode.open) {
  822. view.replaceSwitchClass(parentNode, target_switchObj, consts.folder.CLOSE);
  823. view.replaceIcoClass(parentNode, target_icoObj, consts.folder.CLOSE);
  824. parentNode.open = false;
  825. target_ulObj.css({
  826. "display": "none"
  827. });
  828. }
  829. data.addNodesData(setting, parentNode, newNodes);
  830. view.createNodes(setting, parentNode.level + 1, newNodes, parentNode);
  831. if (!isSilent) {
  832. view.expandCollapseParentNode(setting, parentNode, true);
  833. }
  834. } else {
  835. data.addNodesData(setting, data.getRoot(setting), newNodes);
  836. view.createNodes(setting, 0, newNodes, null);
  837. }
  838. },
  839. appendNodes: function(setting, level, nodes, parentNode, initFlag, openFlag) {
  840. if (!nodes) return [];
  841. var html = [],
  842. childKey = setting.data.key.children;
  843. for (var i = 0, l = nodes.length; i < l; i++) {
  844. var node = nodes[i];
  845. if (initFlag) {
  846. var tmpPNode = (parentNode) ? parentNode: data.getRoot(setting),
  847. tmpPChild = tmpPNode[childKey],
  848. isFirstNode = ((tmpPChild.length == nodes.length) && (i == 0)),
  849. isLastNode = (i == (nodes.length - 1));
  850. data.initNode(setting, level, node, parentNode, isFirstNode, isLastNode, openFlag);
  851. data.addNodeCache(setting, node);
  852. }
  853. var childHtml = [];
  854. if (node[childKey] && node[childKey].length > 0) {
  855. //make child html first, because checkType
  856. childHtml = view.appendNodes(setting, level + 1, node[childKey], node, initFlag, openFlag && node.open);
  857. }
  858. if (openFlag) {
  859. view.makeDOMNodeMainBefore(html, setting, node);
  860. view.makeDOMNodeLine(html, setting, node);
  861. data.getBeforeA(setting, node, html);
  862. view.makeDOMNodeNameBefore(html, setting, node);
  863. data.getInnerBeforeA(setting, node, html);
  864. view.makeDOMNodeIcon(html, setting, node);
  865. data.getInnerAfterA(setting, node, html);
  866. view.makeDOMNodeNameAfter(html, setting, node);
  867. data.getAfterA(setting, node, html);
  868. if (node.isParent && node.open) {
  869. view.makeUlHtml(setting, node, html, childHtml.join(''));
  870. }
  871. view.makeDOMNodeMainAfter(html, setting, node);
  872. data.addCreatedNode(setting, node);
  873. }
  874. }
  875. return html;
  876. },
  877. appendParentULDom: function(setting, node) {
  878. var html = [],
  879. nObj = $$(node, setting);
  880. if (!nObj.get(0) && !!node.parentTId) {
  881. view.appendParentULDom(setting, node.getParentNode());
  882. nObj = $$(node, setting);
  883. }
  884. var ulObj = $$(node, consts.id.UL, setting);
  885. if (ulObj.get(0)) {
  886. ulObj.remove();
  887. }
  888. var childKey = setting.data.key.children,
  889. childHtml = view.appendNodes(setting, node.level+1, node[childKey], node, false, true);
  890. view.makeUlHtml(setting, node, html, childHtml.join(''));
  891. nObj.append(html.join(''));
  892. },
  893. asyncNode: function(setting, node, isSilent, callback) {
  894. var i, l;
  895. if (node && !node.isParent) {
  896. tools.apply(callback);
  897. return false;
  898. } else if (node && node.isAjaxing) {
  899. return false;
  900. } else if (tools.apply(setting.callback.beforeAsync, [setting.treeId, node], true) == false) {
  901. tools.apply(callback);
  902. return false;
  903. }
  904. if (node) {
  905. node.isAjaxing = true;
  906. var icoObj = $$(node, consts.id.ICON, setting);
  907. icoObj.attr({"style":"", "class":consts.className.BUTTON + " " + consts.className.ICO_LOADING});
  908. }
  909. var tmpParam = {};
  910. for (i = 0, l = setting.async.autoParam.length; node && i < l; i++) {
  911. var pKey = setting.async.autoParam[i].split("="), spKey = pKey;
  912. if (pKey.length>1) {
  913. spKey = pKey[1];
  914. pKey = pKey[0];
  915. }
  916. tmpParam[spKey] = node[pKey];
  917. }
  918. if (tools.isArray(setting.async.otherParam)) {
  919. for (i = 0, l = setting.async.otherParam.length; i < l; i += 2) {
  920. tmpParam[setting.async.otherParam[i]] = setting.async.otherParam[i + 1];
  921. }
  922. } else {
  923. for (var p in setting.async.otherParam) {
  924. tmpParam[p] = setting.async.otherParam[p];
  925. }
  926. }
  927. var _tmpV = data.getRoot(setting)._ver;
  928. $.ajax({
  929. contentType: setting.async.contentType,
  930. cache: false,
  931. type: setting.async.type,
  932. url: tools.apply(setting.async.url, [setting.treeId, node], setting.async.url),
  933. data: tmpParam,
  934. dataType: setting.async.dataType,
  935. success: function(msg) {
  936. if (_tmpV != data.getRoot(setting)._ver) {
  937. return;
  938. }
  939. var newNodes = [];
  940. try {
  941. if (!msg || msg.length == 0) {
  942. newNodes = [];
  943. } else if (typeof msg == "string") {
  944. newNodes = eval("(" + msg + ")");
  945. } else {
  946. newNodes = msg;
  947. }
  948. } catch(err) {
  949. newNodes = msg;
  950. }
  951. if (node) {
  952. node.isAjaxing = null;
  953. node.zAsync = true;
  954. }
  955. view.setNodeLineIcos(setting, node);
  956. if (newNodes && newNodes !== "") {
  957. newNodes = tools.apply(setting.async.dataFilter, [setting.treeId, node, newNodes], newNodes);
  958. view.addNodes(setting, node, !!newNodes ? tools.clone(newNodes) : [], !!isSilent);
  959. } else {
  960. view.addNodes(setting, node, [], !!isSilent);
  961. }
  962. setting.treeObj.trigger(consts.event.ASYNC_SUCCESS, [setting.treeId, node, msg]);
  963. tools.apply(callback);
  964. },
  965. error: function(XMLHttpRequest, textStatus, errorThrown) {
  966. if (_tmpV != data.getRoot(setting)._ver) {
  967. return;
  968. }
  969. if (node) node.isAjaxing = null;
  970. view.setNodeLineIcos(setting, node);
  971. setting.treeObj.trigger(consts.event.ASYNC_ERROR, [setting.treeId, node, XMLHttpRequest, textStatus, errorThrown]);
  972. }
  973. });
  974. return true;
  975. },
  976. cancelPreSelectedNode: function (setting, node, excludeNode) {
  977. var list = data.getRoot(setting).curSelectedList,
  978. i, n;
  979. for (i=list.length-1; i>=0; i--) {
  980. n = list[i];
  981. if (node === n || (!node && (!excludeNode || excludeNode !== n))) {
  982. $$(n, consts.id.A, setting).removeClass(consts.node.CURSELECTED);
  983. if (node) {
  984. data.removeSelectedNode(setting, node);
  985. setting.treeObj.trigger(consts.event.UNSELECTED, [event, setting.treeId, n]);
  986. break;
  987. } else {
  988. list.splice(i, 1);
  989. setting.treeObj.trigger(consts.event.UNSELECTED, [event, setting.treeId, n]);
  990. }
  991. }
  992. }
  993. },
  994. createNodeCallback: function(setting) {
  995. if (!!setting.callback.onNodeCreated || !!setting.view.addDiyDom) {
  996. var root = data.getRoot(setting);
  997. while (root.createdNodes.length>0) {
  998. var node = root.createdNodes.shift();
  999. tools.apply(setting.view.addDiyDom, [setting.treeId, node]);
  1000. if (!!setting.callback.onNodeCreated) {
  1001. setting.treeObj.trigger(consts.event.NODECREATED, [setting.treeId, node]);
  1002. }
  1003. }
  1004. }
  1005. },
  1006. createNodes: function(setting, level, nodes, parentNode) {
  1007. if (!nodes || nodes.length == 0) return;
  1008. var root = data.getRoot(setting),
  1009. childKey = setting.data.key.children,
  1010. openFlag = !parentNode || parentNode.open || !!$$(parentNode[childKey][0], setting).get(0);
  1011. root.createdNodes = [];
  1012. var zTreeHtml = view.appendNodes(setting, level, nodes, parentNode, true, openFlag);
  1013. if (!parentNode) {
  1014. setting.treeObj.append(zTreeHtml.join(''));
  1015. } else {
  1016. var ulObj = $$(parentNode, consts.id.UL, setting);
  1017. if (ulObj.get(0)) {
  1018. ulObj.append(zTreeHtml.join(''));
  1019. }
  1020. }
  1021. view.createNodeCallback(setting);
  1022. },
  1023. destroy: function(setting) {
  1024. if (!setting) return;
  1025. data.initCache(setting);
  1026. data.initRoot(setting);
  1027. event.unbindTree(setting);
  1028. event.unbindEvent(setting);
  1029. setting.treeObj.empty();
  1030. delete settings[setting.treeId];
  1031. },
  1032. expandCollapseNode: function(setting, node, expandFlag, animateFlag, callback) {
  1033. var root = data.getRoot(setting),
  1034. childKey = setting.data.key.children;
  1035. if (!node) {
  1036. tools.apply(callback, []);
  1037. return;
  1038. }
  1039. if (root.expandTriggerFlag) {
  1040. var _callback = callback;
  1041. callback = function(){
  1042. if (_callback) _callback();
  1043. if (node.open) {
  1044. setting.treeObj.trigger(consts.event.EXPAND, [setting.treeId, node]);
  1045. } else {
  1046. setting.treeObj.trigger(consts.event.COLLAPSE, [setting.treeId, node]);
  1047. }
  1048. };
  1049. root.expandTriggerFlag = false;
  1050. }
  1051. if (!node.open && node.isParent && ((!$$(node, consts.id.UL, setting).get(0)) || (node[childKey] && node[childKey].length>0 && !$$(node[childKey][0], setting).get(0)))) {
  1052. view.appendParentULDom(setting, node);
  1053. view.createNodeCallback(setting);
  1054. }
  1055. if (node.open == expandFlag) {
  1056. tools.apply(callback, []);
  1057. return;
  1058. }
  1059. var ulObj = $$(node, consts.id.UL, setting),
  1060. switchObj = $$(node, consts.id.SWITCH, setting),
  1061. icoObj = $$(node, consts.id.ICON, setting);
  1062. if (node.isParent) {
  1063. node.open = !node.open;
  1064. if (node.iconOpen && node.iconClose) {
  1065. icoObj.attr("style", view.makeNodeIcoStyle(setting, node));
  1066. }
  1067. if (node.open) {
  1068. view.replaceSwitchClass(node, switchObj, consts.folder.OPEN);
  1069. view.replaceIcoClass(node, icoObj, consts.folder.OPEN);
  1070. if (animateFlag == false || setting.view.expandSpeed == "") {
  1071. ulObj.show();
  1072. tools.apply(callback, []);
  1073. } else {
  1074. if (node[childKey] && node[childKey].length > 0) {
  1075. ulObj.slideDown(setting.view.expandSpeed, callback);
  1076. } else {
  1077. ulObj.show();
  1078. tools.apply(callback, []);
  1079. }
  1080. }
  1081. } else {
  1082. view.replaceSwitchClass(node, switchObj, consts.folder.CLOSE);
  1083. view.replaceIcoClass(node, icoObj, consts.folder.CLOSE);
  1084. if (animateFlag == false || setting.view.expandSpeed == "" || !(node[childKey] && node[childKey].length > 0)) {
  1085. ulObj.hide();
  1086. tools.apply(callback, []);
  1087. } else {
  1088. ulObj.slideUp(setting.view.expandSpeed, callback);
  1089. }
  1090. }
  1091. } else {
  1092. tools.apply(callback, []);
  1093. }
  1094. },
  1095. expandCollapseParentNode: function(setting, node, expandFlag, animateFlag, callback) {
  1096. if (!node) return;
  1097. if (!node.parentTId) {
  1098. view.expandCollapseNode(setting, node, expandFlag, animateFlag, callback);
  1099. return;
  1100. } else {
  1101. view.expandCollapseNode(setting, node, expandFlag, animateFlag);
  1102. }
  1103. if (node.parentTId) {
  1104. view.expandCollapseParentNode(setting, node.getParentNode(), expandFlag, animateFlag, callback);
  1105. }
  1106. },
  1107. expandCollapseSonNode: function(setting, node, expandFlag, animateFlag, callback) {
  1108. var root = data.getRoot(setting),
  1109. childKey = setting.data.key.children,
  1110. treeNodes = (node) ? node[childKey]: root[childKey],
  1111. selfAnimateSign = (node) ? false : animateFlag,
  1112. expandTriggerFlag = data.getRoot(setting).expandTriggerFlag;
  1113. data.getRoot(setting).expandTriggerFlag = false;
  1114. if (treeNodes) {
  1115. for (var i = 0, l = treeNodes.length; i < l; i++) {
  1116. if (treeNodes[i]) view.expandCollapseSonNode(setting, treeNodes[i], expandFlag, selfAnimateSign);
  1117. }
  1118. }
  1119. data.getRoot(setting).expandTriggerFlag = expandTriggerFlag;
  1120. view.expandCollapseNode(setting, node, expandFlag, animateFlag, callback );
  1121. },
  1122. isSelectedNode: function (setting, node) {
  1123. if (!node) {
  1124. return false;
  1125. }
  1126. var list = data.getRoot(setting).curSelectedList,
  1127. i;
  1128. for (i=list.length-1; i>=0; i--) {
  1129. if (node === list[i]) {
  1130. return true;
  1131. }
  1132. }
  1133. return false;
  1134. },
  1135. makeDOMNodeIcon: function(html, setting, node) {
  1136. var nameStr = data.getNodeName(setting, node),
  1137. name = setting.view.nameIsHTML ? nameStr : nameStr.replace(/&/g,'&amp;').replace(/</g,'&lt;').replace(/>/g,'&gt;');
  1138. html.push("<span id='", node.tId, consts.id.ICON,
  1139. "' title='' treeNode", consts.id.ICON," class='", view.makeNodeIcoClass(setting, node),
  1140. "' style='", view.makeNodeIcoStyle(setting, node), "'></span><span id='", node.tId, consts.id.SPAN,
  1141. "'>",name,"</span>");
  1142. },
  1143. makeDOMNodeLine: function(html, setting, node) {
  1144. html.push("<span id='", node.tId, consts.id.SWITCH, "' title='' class='", view.makeNodeLineClass(setting, node), "' treeNode", consts.id.SWITCH,"></span>");
  1145. },
  1146. makeDOMNodeMainAfter: function(html, setting, node) {
  1147. html.push("</li>");
  1148. },
  1149. makeDOMNodeMainBefore: function(html, setting, node) {
  1150. html.push("<li id='", node.tId, "' class='", consts.className.LEVEL, node.level,"' tabindex='0' hidefocus='true' treenode>");
  1151. },
  1152. makeDOMNodeNameAfter: function(html, setting, node) {
  1153. html.push("</a>");
  1154. },
  1155. makeDOMNodeNameBefore: function(html, setting, node) {
  1156. var title = data.getNodeTitle(setting, node),
  1157. url = view.makeNodeUrl(setting, node),
  1158. fontcss = view.makeNodeFontCss(setting, node),
  1159. fontStyle = [];
  1160. for (var f in fontcss) {
  1161. fontStyle.push(f, ":", fontcss[f], ";");
  1162. }
  1163. html.push("<a id='", node.tId, consts.id.A, "' class='", consts.className.LEVEL, node.level,"' treeNode", consts.id.A," onclick=\"", (node.click || ''),
  1164. "\" ", ((url != null && url.length > 0) ? "href='" + url + "'" : ""), " target='",view.makeNodeTarget(node),"' style='", fontStyle.join(''),
  1165. "'");
  1166. if (tools.apply(setting.view.showTitle, [setting.treeId, node], setting.view.showTitle) && title) {html.push("title='", title.replace(/'/g,"&#39;").replace(/</g,'&lt;').replace(/>/g,'&gt;'),"'");}
  1167. html.push(">");
  1168. },
  1169. makeNodeFontCss: function(setting, node) {
  1170. var fontCss = tools.apply(setting.view.fontCss, [setting.treeId, node], setting.view.fontCss);
  1171. return (fontCss && ((typeof fontCss) != "function")) ? fontCss : {};
  1172. },
  1173. makeNodeIcoClass: function(setting, node) {
  1174. var icoCss = ["ico"];
  1175. if (!node.isAjaxing) {
  1176. icoCss[0] = (node.iconSkin ? node.iconSkin + "_" : "") + icoCss[0];
  1177. if (node.isParent) {
  1178. icoCss.push(node.open ? consts.folder.OPEN : consts.folder.CLOSE);
  1179. } else {
  1180. icoCss.push(consts.folder.DOCU);
  1181. }
  1182. }
  1183. return consts.className.BUTTON + " " + icoCss.join('_');
  1184. },
  1185. makeNodeIcoStyle: function(setting, node) {
  1186. var icoStyle = [];
  1187. if (!node.isAjaxing) {
  1188. var icon = (node.isParent && node.iconOpen && node.iconClose) ? (node.open ? node.iconOpen : node.iconClose) : node.icon;
  1189. if (icon) icoStyle.push("background:url(", icon, ") 0 0 no-repeat;");
  1190. if (setting.view.showIcon == false || !tools.apply(setting.view.showIcon, [setting.treeId, node], true)) {
  1191. icoStyle.push("width:0px;height:0px;");
  1192. }
  1193. }
  1194. return icoStyle.join('');
  1195. },
  1196. makeNodeLineClass: function(setting, node) {
  1197. var lineClass = [];
  1198. if (setting.view.showLine) {
  1199. if (node.level == 0 && node.isFirstNode && node.isLastNode) {
  1200. lineClass.push(consts.line.ROOT);
  1201. } else if (node.level == 0 && node.isFirstNode) {
  1202. lineClass.push(consts.line.ROOTS);
  1203. } else if (node.isLastNode) {
  1204. lineClass.push(consts.line.BOTTOM);
  1205. } else {
  1206. lineClass.push(consts.line.CENTER);
  1207. }
  1208. } else {
  1209. lineClass.push(consts.line.NOLINE);
  1210. }
  1211. if (node.isParent) {
  1212. lineClass.push(node.open ? consts.folder.OPEN : consts.folder.CLOSE);
  1213. } else {
  1214. lineClass.push(consts.folder.DOCU);
  1215. }
  1216. return view.makeNodeLineClassEx(node) + lineClass.join('_');
  1217. },
  1218. makeNodeLineClassEx: function(node) {
  1219. return consts.className.BUTTON + " " + consts.className.LEVEL + node.level + " " + consts.className.SWITCH + " ";
  1220. },
  1221. makeNodeTarget: function(node) {
  1222. return (node.target || "_blank");
  1223. },
  1224. makeNodeUrl: function(setting, node) {
  1225. var urlKey = setting.data.key.url;
  1226. return node[urlKey] ? node[urlKey] : null;
  1227. },
  1228. makeUlHtml: function(setting, node, html, content) {
  1229. html.push("<ul id='", node.tId, consts.id.UL, "' class='", consts.className.LEVEL, node.level, " ", view.makeUlLineClass(setting, node), "' style='display:", (node.open ? "block": "none"),"'>");
  1230. html.push(content);
  1231. html.push("</ul>");
  1232. },
  1233. makeUlLineClass: function(setting, node) {
  1234. return ((setting.view.showLine && !node.isLastNode) ? consts.line.LINE : "");
  1235. },
  1236. removeChildNodes: function(setting, node) {
  1237. if (!node) return;
  1238. var childKey = setting.data.key.children,
  1239. nodes = node[childKey];
  1240. if (!nodes) return;
  1241. for (var i = 0, l = nodes.length; i < l; i++) {
  1242. data.removeNodeCache(setting, nodes[i]);
  1243. }
  1244. data.removeSelectedNode(setting);
  1245. delete node[childKey];
  1246. if (!setting.data.keep.parent) {
  1247. node.isParent = false;
  1248. node.open = false;
  1249. var tmp_switchObj = $$(node, consts.id.SWITCH, setting),
  1250. tmp_icoObj = $$(node, consts.id.ICON, setting);
  1251. view.replaceSwitchClass(node, tmp_switchObj, consts.folder.DOCU);
  1252. view.replaceIcoClass(node, tmp_icoObj, consts.folder.DOCU);
  1253. $$(node, consts.id.UL, setting).remove();
  1254. } else {
  1255. $$(node, consts.id.UL, setting).empty();
  1256. }
  1257. },
  1258. setFirstNode: function(setting, parentNode) {
  1259. var childKey = setting.data.key.children, childLength = parentNode[childKey].length;
  1260. if ( childLength > 0) {
  1261. parentNode[childKey][0].isFirstNode = true;
  1262. }
  1263. },
  1264. setLastNode: function(setting, parentNode) {
  1265. var childKey = setting.data.key.children, childLength = parentNode[childKey].length;
  1266. if ( childLength > 0) {
  1267. parentNode[childKey][childLength - 1].isLastNode = true;
  1268. }
  1269. },
  1270. removeNode: function(setting, node) {
  1271. var root = data.getRoot(setting),
  1272. childKey = setting.data.key.children,
  1273. parentNode = (node.parentTId) ? node.getParentNode() : root;
  1274. node.isFirstNode = false;
  1275. node.isLastNode = false;
  1276. node.getPreNode = function() {return null;};
  1277. node.getNextNode = function() {return null;};
  1278. if (!data.getNodeCache(setting, node.tId)) {
  1279. return;
  1280. }
  1281. $$(node, setting).remove();
  1282. data.removeNodeCache(setting, node);
  1283. data.removeSelectedNode(setting, node);
  1284. for (var i = 0, l = parentNode[childKey].length; i < l; i++) {
  1285. if (parentNode[childKey][i].tId == node.tId) {
  1286. parentNode[childKey].splice(i, 1);
  1287. break;
  1288. }
  1289. }
  1290. view.setFirstNode(setting, parentNode);
  1291. view.setLastNode(setting, parentNode);
  1292. var tmp_ulObj,tmp_switchObj,tmp_icoObj,
  1293. childLength = parentNode[childKey].length;
  1294. //repair nodes old parent
  1295. if (!setting.data.keep.parent && childLength == 0) {
  1296. //old parentNode has no child nodes
  1297. parentNode.isParent = false;
  1298. parentNode.open = false;
  1299. tmp_ulObj = $$(parentNode, consts.id.UL, setting);
  1300. tmp_switchObj = $$(parentNode, consts.id.SWITCH, setting);
  1301. tmp_icoObj = $$(parentNode, consts.id.ICON, setting);
  1302. view.replaceSwitchClass(parentNode, tmp_switchObj, consts.folder.DOCU);
  1303. view.replaceIcoClass(parentNode, tmp_icoObj, consts.folder.DOCU);
  1304. tmp_ulObj.css("display", "none");
  1305. } else if (setting.view.showLine && childLength > 0) {
  1306. //old parentNode has child nodes
  1307. var newLast = parentNode[childKey][childLength - 1];
  1308. tmp_ulObj = $$(newLast, consts.id.UL, setting);
  1309. tmp_switchObj = $$(newLast, consts.id.SWITCH, setting);
  1310. tmp_icoObj = $$(newLast, consts.id.ICON, setting);
  1311. if (parentNode == root) {
  1312. if (parentNode[childKey].length == 1) {
  1313. //node was root, and ztree has only one root after move node
  1314. view.replaceSwitchClass(newLast, tmp_switchObj, consts.line.ROOT);
  1315. } else {
  1316. var tmp_first_switchObj = $$(parentNode[childKey][0], consts.id.SWITCH, setting);
  1317. view.replaceSwitchClass(parentNode[childKey][0], tmp_first_switchObj, consts.line.ROOTS);
  1318. view.replaceSwitchClass(newLast, tmp_switchObj, consts.line.BOTTOM);
  1319. }
  1320. } else {
  1321. view.replaceSwitchClass(newLast, tmp_switchObj, consts.line.BOTTOM);
  1322. }
  1323. tmp_ulObj.removeClass(consts.line.LINE);
  1324. }
  1325. },
  1326. replaceIcoClass: function(node, obj, newName) {
  1327. if (!obj || node.isAjaxing) return;
  1328. var tmpName = obj.attr("class");
  1329. if (tmpName == undefined) return;
  1330. var tmpList = tmpName.split("_");
  1331. switch (newName) {
  1332. case consts.folder.OPEN:
  1333. case consts.folder.CLOSE:
  1334. case consts.folder.DOCU:
  1335. tmpList[tmpList.length-1] = newName;
  1336. break;
  1337. }
  1338. obj.attr("class", tmpList.join("_"));
  1339. },
  1340. replaceSwitchClass: function(node, obj, newName) {
  1341. if (!obj) return;
  1342. var tmpName = obj.attr("class");
  1343. if (tmpName == undefined) return;
  1344. var tmpList = tmpName.split("_");
  1345. switch (newName) {
  1346. case consts.line.ROOT:
  1347. case consts.line.ROOTS:
  1348. case consts.line.CENTER:
  1349. case consts.line.BOTTOM:
  1350. case consts.line.NOLINE:
  1351. tmpList[0] = view.makeNodeLineClassEx(node) + newName;
  1352. break;
  1353. case consts.folder.OPEN:
  1354. case consts.folder.CLOSE:
  1355. case consts.folder.DOCU:
  1356. tmpList[1] = newName;
  1357. break;
  1358. }
  1359. obj.attr("class", tmpList.join("_"));
  1360. if (newName !== consts.folder.DOCU) {
  1361. obj.removeAttr("disabled");
  1362. } else {
  1363. obj.attr("disabled", "disabled");
  1364. }
  1365. },
  1366. selectNode: function(setting, node, addFlag) {
  1367. if (!addFlag) {
  1368. view.cancelPreSelectedNode(setting, null, node);
  1369. }
  1370. $$(node, consts.id.A, setting).addClass(consts.node.CURSELECTED);
  1371. data.addSelectedNode(setting, node);
  1372. setting.treeObj.trigger(consts.event.SELECTED, [event, setting.treeId, node]);
  1373. },
  1374. setNodeFontCss: function(setting, treeNode) {
  1375. var aObj = $$(treeNode, consts.id.A, setting),
  1376. fontCss = view.makeNodeFontCss(setting, treeNode);
  1377. if (fontCss) {
  1378. aObj.css(fontCss);
  1379. }
  1380. },
  1381. setNodeLineIcos: function(setting, node) {
  1382. if (!node) return;
  1383. var switchObj = $$(node, consts.id.SWITCH, setting),
  1384. ulObj = $$(node, consts.id.UL, setting),
  1385. icoObj = $$(node, consts.id.ICON, setting),
  1386. ulLine = view.makeUlLineClass(setting, node);
  1387. if (ulLine.length==0) {
  1388. ulObj.removeClass(consts.line.LINE);
  1389. } else {
  1390. ulObj.addClass(ulLine);
  1391. }
  1392. switchObj.attr("class", view.makeNodeLineClass(setting, node));
  1393. if (node.isParent) {
  1394. switchObj.removeAttr("disabled");
  1395. } else {
  1396. switchObj.attr("disabled", "disabled");
  1397. }
  1398. icoObj.removeAttr("style");
  1399. icoObj.attr("style", view.makeNodeIcoStyle(setting, node));
  1400. icoObj.attr("class", view.makeNodeIcoClass(setting, node));
  1401. },
  1402. setNodeName: function(setting, node) {
  1403. var title = data.getNodeTitle(setting, node),
  1404. nObj = $$(node, consts.id.SPAN, setting);
  1405. nObj.empty();
  1406. if (setting.view.nameIsHTML) {
  1407. nObj.html(data.getNodeName(setting, node));
  1408. } else {
  1409. nObj.text(data.getNodeName(setting, node));
  1410. }
  1411. if (tools.apply(setting.view.showTitle, [setting.treeId, node], setting.view.showTitle)) {
  1412. var aObj = $$(node, consts.id.A, setting);
  1413. aObj.attr("title", !title ? "" : title);
  1414. }
  1415. },
  1416. setNodeTarget: function(setting, node) {
  1417. var aObj = $$(node, consts.id.A, setting);
  1418. aObj.attr("target", view.makeNodeTarget(node));
  1419. },
  1420. setNodeUrl: function(setting, node) {
  1421. var aObj = $$(node, consts.id.A, setting),
  1422. url = view.makeNodeUrl(setting, node);
  1423. if (url == null || url.length == 0) {
  1424. aObj.removeAttr("href");
  1425. } else {
  1426. aObj.attr("href", url);
  1427. }
  1428. },
  1429. switchNode: function(setting, node) {
  1430. if (node.open || !tools.canAsync(setting, node)) {
  1431. view.expandCollapseNode(setting, node, !node.open);
  1432. } else if (setting.async.enable) {
  1433. if (!view.asyncNode(setting, node)) {
  1434. view.expandCollapseNode(setting, node, !node.open);
  1435. return;
  1436. }
  1437. } else if (node) {
  1438. view.expandCollapseNode(setting, node, !node.open);
  1439. }
  1440. }
  1441. };
  1442. // zTree defind
  1443. $.fn.zTree = {
  1444. consts : _consts,
  1445. _z : {
  1446. tools: tools,
  1447. view: view,
  1448. event: event,
  1449. data: data
  1450. },
  1451. getZTreeObj: function(treeId) {
  1452. var o = data.getZTreeTools(treeId);
  1453. return o ? o : null;
  1454. },
  1455. destroy: function(treeId) {
  1456. if (!!treeId && treeId.length > 0) {
  1457. view.destroy(data.getSetting(treeId));
  1458. } else {
  1459. for(var s in settings) {
  1460. view.destroy(settings[s]);
  1461. }
  1462. }
  1463. },
  1464. init: function(obj, zSetting, zNodes) {
  1465. var setting = tools.clone(_setting);
  1466. $.extend(true, setting, zSetting);
  1467. setting.treeId = obj.attr("id");
  1468. setting.treeObj = obj;
  1469. setting.treeObj.empty();
  1470. settings[setting.treeId] = setting;
  1471. //For some older browser,(e.g., ie6)
  1472. if(typeof document.body.style.maxHeight === "undefined") {
  1473. setting.view.expandSpeed = "";
  1474. }
  1475. data.initRoot(setting);
  1476. var root = data.getRoot(setting),
  1477. childKey = setting.data.key.children;
  1478. zNodes = zNodes ? tools.clone(tools.isArray(zNodes)? zNodes : [zNodes]) : [];
  1479. if (setting.data.simpleData.enable) {
  1480. root[childKey] = data.transformTozTreeFormat(setting, zNodes);
  1481. } else {
  1482. root[childKey] = zNodes;
  1483. }
  1484. data.initCache(setting);
  1485. event.unbindTree(setting);
  1486. event.bindTree(setting);
  1487. event.unbindEvent(setting);
  1488. event.bindEvent(setting);
  1489. var zTreeTools = {
  1490. setting : setting,
  1491. addNodes : function(parentNode, newNodes, isSilent) {
  1492. if (!newNodes) return null;
  1493. if (!parentNode) parentNode = null;
  1494. if (parentNode && !parentNode.isParent && setting.data.keep.leaf) return null;
  1495. var xNewNodes = tools.clone(tools.isArray(newNodes)? newNodes: [newNodes]);
  1496. function addCallback() {
  1497. view.addNodes(setting, parentNode, xNewNodes, (isSilent==true));
  1498. }
  1499. if (tools.canAsync(setting, parentNode)) {
  1500. view.asyncNode(setting, parentNode, isSilent, addCallback);
  1501. } else {
  1502. addCallback();
  1503. }
  1504. return xNewNodes;
  1505. },
  1506. cancelSelectedNode : function(node) {
  1507. view.cancelPreSelectedNode(setting, node);
  1508. },
  1509. destroy : function() {
  1510. view.destroy(setting);
  1511. },
  1512. expandAll : function(expandFlag) {
  1513. expandFlag = !!expandFlag;
  1514. view.expandCollapseSonNode(setting, null, expandFlag, true);
  1515. return expandFlag;
  1516. },
  1517. expandNode : function(node, expandFlag, sonSign, focus, callbackFlag) {
  1518. if (!node || !node.isParent) return null;
  1519. if (expandFlag !== true && expandFlag !== false) {
  1520. expandFlag = !node.open;
  1521. }
  1522. callbackFlag = !!callbackFlag;
  1523. if (callbackFlag && expandFlag && (tools.apply(setting.callback.beforeExpand, [setting.treeId, node], true) == false)) {
  1524. return null;
  1525. } else if (callbackFlag && !expandFlag && (tools.apply(setting.callback.beforeCollapse, [setting.treeId, node], true) == false)) {
  1526. return null;
  1527. }
  1528. if (expandFlag && node.parentTId) {
  1529. view.expandCollapseParentNode(setting, node.getParentNode(), expandFlag, false);
  1530. }
  1531. if (expandFlag === node.open && !sonSign) {
  1532. return null;
  1533. }
  1534. data.getRoot(setting).expandTriggerFlag = callbackFlag;
  1535. if (!tools.canAsync(setting, node) && sonSign) {
  1536. view.expandCollapseSonNode(setting, node, expandFlag, true, function() {
  1537. if (focus !== false) {try{$$(node, setting).focus().blur();}catch(e){}}
  1538. });
  1539. } else {
  1540. node.open = !expandFlag;
  1541. view.switchNode(this.setting, node);
  1542. if (focus !== false) {try{$$(node, setting).focus().blur();}catch(e){}}
  1543. }
  1544. return expandFlag;
  1545. },
  1546. getNodes : function() {
  1547. return data.getNodes(setting);
  1548. },
  1549. getNodeByParam : function(key, value, parentNode) {
  1550. if (!key) return null;
  1551. return data.getNodeByParam(setting, parentNode?parentNode[setting.data.key.children]:data.getNodes(setting), key, value);
  1552. },
  1553. getNodeByTId : function(tId) {
  1554. return data.getNodeCache(setting, tId);
  1555. },
  1556. getNodesByParam : function(key, value, parentNode) {
  1557. if (!key) return null;
  1558. return data.getNodesByParam(setting, parentNode?parentNode[setting.data.key.children]:data.getNodes(setting), key, value);
  1559. },
  1560. getNodesByParamFuzzy : function(key, value, parentNode) {
  1561. if (!key) return null;
  1562. return data.getNodesByParamFuzzy(setting, parentNode?parentNode[setting.data.key.children]:data.getNodes(setting), key, value);
  1563. },
  1564. getNodesByFilter: function(filter, isSingle, parentNode, invokeParam) {
  1565. isSingle = !!isSingle;
  1566. if (!filter || (typeof filter != "function")) return (isSingle ? null : []);
  1567. return data.getNodesByFilter(setting, parentNode?parentNode[setting.data.key.children]:data.getNodes(setting), filter, isSingle, invokeParam);
  1568. },
  1569. getNodeIndex : function(node) {
  1570. if (!node) return null;
  1571. var childKey = setting.data.key.children,
  1572. parentNode = (node.parentTId) ? node.getParentNode() : data.getRoot(setting);
  1573. for (var i=0, l = parentNode[childKey].length; i < l; i++) {
  1574. if (parentNode[childKey][i] == node) return i;
  1575. }
  1576. return -1;
  1577. },
  1578. getSelectedNodes : function() {
  1579. var r = [], list = data.getRoot(setting).curSelectedList;
  1580. for (var i=0, l=list.length; i<l; i++) {
  1581. r.push(list[i]);
  1582. }
  1583. return r;
  1584. },
  1585. isSelectedNode : function(node) {
  1586. return data.isSelectedNode(setting, node);
  1587. },
  1588. reAsyncChildNodes : function(parentNode, reloadType, isSilent) {
  1589. if (!this.setting.async.enable) return;
  1590. var isRoot = !parentNode;
  1591. if (isRoot) {
  1592. parentNode = data.getRoot(setting);
  1593. }
  1594. if (reloadType=="refresh") {
  1595. var childKey = this.setting.data.key.children;
  1596. for (var i = 0, l = parentNode[childKey] ? parentNode[childKey].length : 0; i < l; i++) {
  1597. data.removeNodeCache(setting, parentNode[childKey][i]);
  1598. }
  1599. data.removeSelectedNode(setting);
  1600. parentNode[childKey] = [];
  1601. if (isRoot) {
  1602. this.setting.treeObj.empty();
  1603. } else {
  1604. var ulObj = $$(parentNode, consts.id.UL, setting);
  1605. ulObj.empty();
  1606. }
  1607. }
  1608. view.asyncNode(this.setting, isRoot? null:parentNode, !!isSilent);
  1609. },
  1610. refresh : function() {
  1611. this.setting.treeObj.empty();
  1612. var root = data.getRoot(setting),
  1613. nodes = root[setting.data.key.children]
  1614. data.initRoot(setting);
  1615. root[setting.data.key.children] = nodes
  1616. data.initCache(setting);
  1617. view.createNodes(setting, 0, root[setting.data.key.children]);
  1618. },
  1619. removeChildNodes : function(node) {
  1620. if (!node) return null;
  1621. var childKey = setting.data.key.children,
  1622. nodes = node[childKey];
  1623. view.removeChildNodes(setting, node);
  1624. return nodes ? nodes : null;
  1625. },
  1626. removeNode : function(node, callbackFlag) {
  1627. if (!node) return;
  1628. callbackFlag = !!callbackFlag;
  1629. if (callbackFlag && tools.apply(setting.callback.beforeRemove, [setting.treeId, node], true) == false) return;
  1630. view.removeNode(setting, node);
  1631. if (callbackFlag) {
  1632. this.setting.treeObj.trigger(consts.event.REMOVE, [setting.treeId, node]);
  1633. }
  1634. },
  1635. selectNode : function(node, addFlag) {
  1636. if (!node) return;
  1637. if (tools.uCanDo(setting)) {
  1638. addFlag = setting.view.selectedMulti && addFlag;
  1639. if (node.parentTId) {
  1640. view.expandCollapseParentNode(setting, node.getParentNode(), true, false, function() {
  1641. try{$$(node, setting).focus().blur();}catch(e){}
  1642. });
  1643. } else {
  1644. try{$$(node, setting).focus().blur();}catch(e){}
  1645. }
  1646. view.selectNode(setting, node, addFlag);
  1647. }
  1648. },
  1649. transformTozTreeNodes : function(simpleNodes) {
  1650. return data.transformTozTreeFormat(setting, simpleNodes);
  1651. },
  1652. transformToArray : function(nodes) {
  1653. return data.transformToArrayFormat(setting, nodes);
  1654. },
  1655. updateNode : function(node, checkTypeFlag) {
  1656. if (!node) return;
  1657. var nObj = $$(node, setting);
  1658. if (nObj.get(0) && tools.uCanDo(setting)) {
  1659. view.setNodeName(setting, node);
  1660. view.setNodeTarget(setting, node);
  1661. view.setNodeUrl(setting, node);
  1662. view.setNodeLineIcos(setting, node);
  1663. view.setNodeFontCss(setting, node);
  1664. }
  1665. }
  1666. }
  1667. root.treeTools = zTreeTools;
  1668. data.setZTreeTools(setting, zTreeTools);
  1669. if (root[childKey] && root[childKey].length > 0) {
  1670. view.createNodes(setting, 0, root[childKey]);
  1671. } else if (setting.async.enable && setting.async.url && setting.async.url !== '') {
  1672. view.asyncNode(setting);
  1673. }
  1674. return zTreeTools;
  1675. }
  1676. };
  1677. var zt = $.fn.zTree,
  1678. $$ = tools.$,
  1679. consts = zt.consts;
  1680. })(jQuery);
  1681. /*
  1682. * JQuery zTree excheck v3.5.18
  1683. * http://zTree.me/
  1684. *
  1685. * Copyright (c) 2010 Hunter.z
  1686. *
  1687. * Licensed same as jquery - MIT License
  1688. * http://www.opensource.org/licenses/mit-license.php
  1689. *
  1690. * email: hunter.z@263.net
  1691. * Date: 2015-05-25
  1692. */
  1693. (function($){
  1694. //default consts of excheck
  1695. var _consts = {
  1696. event: {
  1697. CHECK: "ztree_check"
  1698. },
  1699. id: {
  1700. CHECK: "_check"
  1701. },
  1702. checkbox: {
  1703. STYLE: "checkbox",
  1704. DEFAULT: "chk",
  1705. DISABLED: "disable",
  1706. FALSE: "false",
  1707. TRUE: "true",
  1708. FULL: "full",
  1709. PART: "part",
  1710. FOCUS: "focus"
  1711. },
  1712. radio: {
  1713. STYLE: "radio",
  1714. TYPE_ALL: "all",
  1715. TYPE_LEVEL: "level"
  1716. }
  1717. },
  1718. //default setting of excheck
  1719. _setting = {
  1720. check: {
  1721. enable: false,
  1722. autoCheckTrigger: false,
  1723. chkStyle: _consts.checkbox.STYLE,
  1724. nocheckInherit: false,
  1725. chkDisabledInherit: false,
  1726. radioType: _consts.radio.TYPE_LEVEL,
  1727. chkboxType: {
  1728. "Y": "ps",
  1729. "N": "ps"
  1730. }
  1731. },
  1732. data: {
  1733. key: {
  1734. checked: "checked"
  1735. }
  1736. },
  1737. callback: {
  1738. beforeCheck:null,
  1739. onCheck:null
  1740. }
  1741. },
  1742. //default root of excheck
  1743. _initRoot = function (setting) {
  1744. var r = data.getRoot(setting);
  1745. r.radioCheckedList = [];
  1746. },
  1747. //default cache of excheck
  1748. _initCache = function(treeId) {},
  1749. //default bind event of excheck
  1750. _bindEvent = function(setting) {
  1751. var o = setting.treeObj,
  1752. c = consts.event;
  1753. o.bind(c.CHECK, function (event, srcEvent, treeId, node) {
  1754. event.srcEvent = srcEvent;
  1755. tools.apply(setting.callback.onCheck, [event, treeId, node]);
  1756. });
  1757. },
  1758. _unbindEvent = function(setting) {
  1759. var o = setting.treeObj,
  1760. c = consts.event;
  1761. o.unbind(c.CHECK);
  1762. },
  1763. //default event proxy of excheck
  1764. _eventProxy = function(e) {
  1765. var target = e.target,
  1766. setting = data.getSetting(e.data.treeId),
  1767. tId = "", node = null,
  1768. nodeEventType = "", treeEventType = "",
  1769. nodeEventCallback = null, treeEventCallback = null;
  1770. if (tools.eqs(e.type, "mouseover")) {
  1771. if (setting.check.enable && tools.eqs(target.tagName, "span") && target.getAttribute("treeNode"+ consts.id.CHECK) !== null) {
  1772. tId = tools.getNodeMainDom(target).id;
  1773. nodeEventType = "mouseoverCheck";
  1774. }
  1775. } else if (tools.eqs(e.type, "mouseout")) {
  1776. if (setting.check.enable && tools.eqs(target.tagName, "span") && target.getAttribute("treeNode"+ consts.id.CHECK) !== null) {
  1777. tId = tools.getNodeMainDom(target).id;
  1778. nodeEventType = "mouseoutCheck";
  1779. }
  1780. } else if (tools.eqs(e.type, "click")) {
  1781. if (setting.check.enable && tools.eqs(target.tagName, "span") && target.getAttribute("treeNode"+ consts.id.CHECK) !== null) {
  1782. tId = tools.getNodeMainDom(target).id;
  1783. nodeEventType = "checkNode";
  1784. }
  1785. }
  1786. if (tId.length>0) {
  1787. node = data.getNodeCache(setting, tId);
  1788. switch (nodeEventType) {
  1789. case "checkNode" :
  1790. nodeEventCallback = _handler.onCheckNode;
  1791. break;
  1792. case "mouseoverCheck" :
  1793. nodeEventCallback = _handler.onMouseoverCheck;
  1794. break;
  1795. case "mouseoutCheck" :
  1796. nodeEventCallback = _handler.onMouseoutCheck;
  1797. break;
  1798. }
  1799. }
  1800. var proxyResult = {
  1801. stop: nodeEventType === "checkNode",
  1802. node: node,
  1803. nodeEventType: nodeEventType,
  1804. nodeEventCallback: nodeEventCallback,
  1805. treeEventType: treeEventType,
  1806. treeEventCallback: treeEventCallback
  1807. };
  1808. return proxyResult
  1809. },
  1810. //default init node of excheck
  1811. _initNode = function(setting, level, n, parentNode, isFirstNode, isLastNode, openFlag) {
  1812. if (!n) return;
  1813. var checkedKey = setting.data.key.checked;
  1814. if (typeof n[checkedKey] == "string") n[checkedKey] = tools.eqs(n[checkedKey], "true");
  1815. n[checkedKey] = !!n[checkedKey];
  1816. n.checkedOld = n[checkedKey];
  1817. if (typeof n.nocheck == "string") n.nocheck = tools.eqs(n.nocheck, "true");
  1818. n.nocheck = !!n.nocheck || (setting.check.nocheckInherit && parentNode && !!parentNode.nocheck);
  1819. if (typeof n.chkDisabled == "string") n.chkDisabled = tools.eqs(n.chkDisabled, "true");
  1820. n.chkDisabled = !!n.chkDisabled || (setting.check.chkDisabledInherit && parentNode && !!parentNode.chkDisabled);
  1821. if (typeof n.halfCheck == "string") n.halfCheck = tools.eqs(n.halfCheck, "true");
  1822. n.halfCheck = !!n.halfCheck;
  1823. n.check_Child_State = -1;
  1824. n.check_Focus = false;
  1825. n.getCheckStatus = function() {return data.getCheckStatus(setting, n);};
  1826. if (setting.check.chkStyle == consts.radio.STYLE && setting.check.radioType == consts.radio.TYPE_ALL && n[checkedKey] ) {
  1827. var r = data.getRoot(setting);
  1828. r.radioCheckedList.push(n);
  1829. }
  1830. },
  1831. //add dom for check
  1832. _beforeA = function(setting, node, html) {
  1833. var checkedKey = setting.data.key.checked;
  1834. if (setting.check.enable) {
  1835. data.makeChkFlag(setting, node);
  1836. html.push("<span ID='", node.tId, consts.id.CHECK, "' class='", view.makeChkClass(setting, node), "' treeNode", consts.id.CHECK, (node.nocheck === true?" style='display:none;'":""),"></span>");
  1837. }
  1838. },
  1839. //update zTreeObj, add method of check
  1840. _zTreeTools = function(setting, zTreeTools) {
  1841. zTreeTools.checkNode = function(node, checked, checkTypeFlag, callbackFlag) {
  1842. var checkedKey = this.setting.data.key.checked;
  1843. if (node.chkDisabled === true) return;
  1844. if (checked !== true && checked !== false) {
  1845. checked = !node[checkedKey];
  1846. }
  1847. callbackFlag = !!callbackFlag;
  1848. if (node[checkedKey] === checked && !checkTypeFlag) {
  1849. return;
  1850. } else if (callbackFlag && tools.apply(this.setting.callback.beforeCheck, [this.setting.treeId, node], true) == false) {
  1851. return;
  1852. }
  1853. if (tools.uCanDo(this.setting) && this.setting.check.enable && node.nocheck !== true) {
  1854. node[checkedKey] = checked;
  1855. var checkObj = $$(node, consts.id.CHECK, this.setting);
  1856. if (checkTypeFlag || this.setting.check.chkStyle === consts.radio.STYLE) view.checkNodeRelation(this.setting, node);
  1857. view.setChkClass(this.setting, checkObj, node);
  1858. view.repairParentChkClassWithSelf(this.setting, node);
  1859. if (callbackFlag) {
  1860. this.setting.treeObj.trigger(consts.event.CHECK, [null, this.setting.treeId, node]);
  1861. }
  1862. }
  1863. }
  1864. zTreeTools.checkAllNodes = function(checked) {
  1865. view.repairAllChk(this.setting, !!checked);
  1866. }
  1867. zTreeTools.getCheckedNodes = function(checked) {
  1868. var childKey = this.setting.data.key.children;
  1869. checked = (checked !== false);
  1870. return data.getTreeCheckedNodes(this.setting, data.getRoot(this.setting)[childKey], checked);
  1871. }
  1872. zTreeTools.getChangeCheckedNodes = function() {
  1873. var childKey = this.setting.data.key.children;
  1874. return data.getTreeChangeCheckedNodes(this.setting, data.getRoot(this.setting)[childKey]);
  1875. }
  1876. zTreeTools.setChkDisabled = function(node, disabled, inheritParent, inheritChildren) {
  1877. disabled = !!disabled;
  1878. inheritParent = !!inheritParent;
  1879. inheritChildren = !!inheritChildren;
  1880. view.repairSonChkDisabled(this.setting, node, disabled, inheritChildren);
  1881. view.repairParentChkDisabled(this.setting, node.getParentNode(), disabled, inheritParent);
  1882. }
  1883. var _updateNode = zTreeTools.updateNode;
  1884. zTreeTools.updateNode = function(node, checkTypeFlag) {
  1885. if (_updateNode) _updateNode.apply(zTreeTools, arguments);
  1886. if (!node || !this.setting.check.enable) return;
  1887. var nObj = $$(node, this.setting);
  1888. if (nObj.get(0) && tools.uCanDo(this.setting)) {
  1889. var checkObj = $$(node, consts.id.CHECK, this.setting);
  1890. if (checkTypeFlag == true || this.setting.check.chkStyle === consts.radio.STYLE) view.checkNodeRelation(this.setting, node);
  1891. view.setChkClass(this.setting, checkObj, node);
  1892. view.repairParentChkClassWithSelf(this.setting, node);
  1893. }
  1894. }
  1895. },
  1896. //method of operate data
  1897. _data = {
  1898. getRadioCheckedList: function(setting) {
  1899. var checkedList = data.getRoot(setting).radioCheckedList;
  1900. for (var i=0, j=checkedList.length; i<j; i++) {
  1901. if(!data.getNodeCache(setting, checkedList[i].tId)) {
  1902. checkedList.splice(i, 1);
  1903. i--; j--;
  1904. }
  1905. }
  1906. return checkedList;
  1907. },
  1908. getCheckStatus: function(setting, node) {
  1909. if (!setting.check.enable || node.nocheck || node.chkDisabled) return null;
  1910. var checkedKey = setting.data.key.checked,
  1911. r = {
  1912. checked: node[checkedKey],
  1913. half: node.halfCheck ? node.halfCheck : (setting.check.chkStyle == consts.radio.STYLE ? (node.check_Child_State === 2) : (node[checkedKey] ? (node.check_Child_State > -1 && node.check_Child_State < 2) : (node.check_Child_State > 0)))
  1914. };
  1915. return r;
  1916. },
  1917. getTreeCheckedNodes: function(setting, nodes, checked, results) {
  1918. if (!nodes) return [];
  1919. var childKey = setting.data.key.children,
  1920. checkedKey = setting.data.key.checked,
  1921. onlyOne = (checked && setting.check.chkStyle == consts.radio.STYLE && setting.check.radioType == consts.radio.TYPE_ALL);
  1922. results = !results ? [] : results;
  1923. for (var i = 0, l = nodes.length; i < l; i++) {
  1924. if (nodes[i].nocheck !== true && nodes[i].chkDisabled !== true && nodes[i][checkedKey] == checked) {
  1925. results.push(nodes[i]);
  1926. if(onlyOne) {
  1927. break;
  1928. }
  1929. }
  1930. data.getTreeCheckedNodes(setting, nodes[i][childKey], checked, results);
  1931. if(onlyOne && results.length > 0) {
  1932. break;
  1933. }
  1934. }
  1935. return results;
  1936. },
  1937. getTreeChangeCheckedNodes: function(setting, nodes, results) {
  1938. if (!nodes) return [];
  1939. var childKey = setting.data.key.children,
  1940. checkedKey = setting.data.key.checked;
  1941. results = !results ? [] : results;
  1942. for (var i = 0, l = nodes.length; i < l; i++) {
  1943. if (nodes[i].nocheck !== true && nodes[i].chkDisabled !== true && nodes[i][checkedKey] != nodes[i].checkedOld) {
  1944. results.push(nodes[i]);
  1945. }
  1946. data.getTreeChangeCheckedNodes(setting, nodes[i][childKey], results);
  1947. }
  1948. return results;
  1949. },
  1950. makeChkFlag: function(setting, node) {
  1951. if (!node) return;
  1952. var childKey = setting.data.key.children,
  1953. checkedKey = setting.data.key.checked,
  1954. chkFlag = -1;
  1955. if (node[childKey]) {
  1956. for (var i = 0, l = node[childKey].length; i < l; i++) {
  1957. var cNode = node[childKey][i];
  1958. var tmp = -1;
  1959. if (setting.check.chkStyle == consts.radio.STYLE) {
  1960. if (cNode.nocheck === true || cNode.chkDisabled === true) {
  1961. tmp = cNode.check_Child_State;
  1962. } else if (cNode.halfCheck === true) {
  1963. tmp = 2;
  1964. } else if (cNode[checkedKey]) {
  1965. tmp = 2;
  1966. } else {
  1967. tmp = cNode.check_Child_State > 0 ? 2:0;
  1968. }
  1969. if (tmp == 2) {
  1970. chkFlag = 2; break;
  1971. } else if (tmp == 0){
  1972. chkFlag = 0;
  1973. }
  1974. } else if (setting.check.chkStyle == consts.checkbox.STYLE) {
  1975. if (cNode.nocheck === true || cNode.chkDisabled === true) {
  1976. tmp = cNode.check_Child_State;
  1977. } else if (cNode.halfCheck === true) {
  1978. tmp = 1;
  1979. } else if (cNode[checkedKey] ) {
  1980. tmp = (cNode.check_Child_State === -1 || cNode.check_Child_State === 2) ? 2 : 1;
  1981. } else {
  1982. tmp = (cNode.check_Child_State > 0) ? 1 : 0;
  1983. }
  1984. if (tmp === 1) {
  1985. chkFlag = 1; break;
  1986. } else if (tmp === 2 && chkFlag > -1 && i > 0 && tmp !== chkFlag) {
  1987. chkFlag = 1; break;
  1988. } else if (chkFlag === 2 && tmp > -1 && tmp < 2) {
  1989. chkFlag = 1; break;
  1990. } else if (tmp > -1) {
  1991. chkFlag = tmp;
  1992. }
  1993. }
  1994. }
  1995. }
  1996. node.check_Child_State = chkFlag;
  1997. }
  1998. },
  1999. //method of event proxy
  2000. _event = {
  2001. },
  2002. //method of event handler
  2003. _handler = {
  2004. onCheckNode: function (event, node) {
  2005. if (node.chkDisabled === true) return false;
  2006. var setting = data.getSetting(event.data.treeId),
  2007. checkedKey = setting.data.key.checked;
  2008. if (tools.apply(setting.callback.beforeCheck, [setting.treeId, node], true) == false) return true;
  2009. node[checkedKey] = !node[checkedKey];
  2010. view.checkNodeRelation(setting, node);
  2011. var checkObj = $$(node, consts.id.CHECK, setting);
  2012. view.setChkClass(setting, checkObj, node);
  2013. view.repairParentChkClassWithSelf(setting, node);
  2014. setting.treeObj.trigger(consts.event.CHECK, [event, setting.treeId, node]);
  2015. return true;
  2016. },
  2017. onMouseoverCheck: function(event, node) {
  2018. if (node.chkDisabled === true) return false;
  2019. var setting = data.getSetting(event.data.treeId),
  2020. checkObj = $$(node, consts.id.CHECK, setting);
  2021. node.check_Focus = true;
  2022. view.setChkClass(setting, checkObj, node);
  2023. return true;
  2024. },
  2025. onMouseoutCheck: function(event, node) {
  2026. if (node.chkDisabled === true) return false;
  2027. var setting = data.getSetting(event.data.treeId),
  2028. checkObj = $$(node, consts.id.CHECK, setting);
  2029. node.check_Focus = false;
  2030. view.setChkClass(setting, checkObj, node);
  2031. return true;
  2032. }
  2033. },
  2034. //method of tools for zTree
  2035. _tools = {
  2036. },
  2037. //method of operate ztree dom
  2038. _view = {
  2039. checkNodeRelation: function(setting, node) {
  2040. var pNode, i, l,
  2041. childKey = setting.data.key.children,
  2042. checkedKey = setting.data.key.checked,
  2043. r = consts.radio;
  2044. if (setting.check.chkStyle == r.STYLE) {
  2045. var checkedList = data.getRadioCheckedList(setting);
  2046. if (node[checkedKey]) {
  2047. if (setting.check.radioType == r.TYPE_ALL) {
  2048. for (i = checkedList.length-1; i >= 0; i--) {
  2049. pNode = checkedList[i];
  2050. if (pNode[checkedKey] && pNode != node) {
  2051. pNode[checkedKey] = false;
  2052. checkedList.splice(i, 1);
  2053. view.setChkClass(setting, $$(pNode, consts.id.CHECK, setting), pNode);
  2054. if (pNode.parentTId != node.parentTId) {
  2055. view.repairParentChkClassWithSelf(setting, pNode);
  2056. }
  2057. }
  2058. }
  2059. checkedList.push(node);
  2060. } else {
  2061. var parentNode = (node.parentTId) ? node.getParentNode() : data.getRoot(setting);
  2062. for (i = 0, l = parentNode[childKey].length; i < l; i++) {
  2063. pNode = parentNode[childKey][i];
  2064. if (pNode[checkedKey] && pNode != node) {
  2065. pNode[checkedKey] = false;
  2066. view.setChkClass(setting, $$(pNode, consts.id.CHECK, setting), pNode);
  2067. }
  2068. }
  2069. }
  2070. } else if (setting.check.radioType == r.TYPE_ALL) {
  2071. for (i = 0, l = checkedList.length; i < l; i++) {
  2072. if (node == checkedList[i]) {
  2073. checkedList.splice(i, 1);
  2074. break;
  2075. }
  2076. }
  2077. }
  2078. } else {
  2079. if (node[checkedKey] && (!node[childKey] || node[childKey].length==0 || setting.check.chkboxType.Y.indexOf("s") > -1)) {
  2080. view.setSonNodeCheckBox(setting, node, true);
  2081. }
  2082. if (!node[checkedKey] && (!node[childKey] || node[childKey].length==0 || setting.check.chkboxType.N.indexOf("s") > -1)) {
  2083. view.setSonNodeCheckBox(setting, node, false);
  2084. }
  2085. if (node[checkedKey] && setting.check.chkboxType.Y.indexOf("p") > -1) {
  2086. view.setParentNodeCheckBox(setting, node, true);
  2087. }
  2088. if (!node[checkedKey] && setting.check.chkboxType.N.indexOf("p") > -1) {
  2089. view.setParentNodeCheckBox(setting, node, false);
  2090. }
  2091. }
  2092. },
  2093. makeChkClass: function(setting, node) {
  2094. var checkedKey = setting.data.key.checked,
  2095. c = consts.checkbox, r = consts.radio,
  2096. fullStyle = "";
  2097. if (node.chkDisabled === true) {
  2098. fullStyle = c.DISABLED;
  2099. } else if (node.halfCheck) {
  2100. fullStyle = c.PART;
  2101. } else if (setting.check.chkStyle == r.STYLE) {
  2102. fullStyle = (node.check_Child_State < 1)? c.FULL:c.PART;
  2103. } else {
  2104. fullStyle = node[checkedKey] ? ((node.check_Child_State === 2 || node.check_Child_State === -1) ? c.FULL:c.PART) : ((node.check_Child_State < 1)? c.FULL:c.PART);
  2105. }
  2106. var chkName = setting.check.chkStyle + "_" + (node[checkedKey] ? c.TRUE : c.FALSE) + "_" + fullStyle;
  2107. chkName = (node.check_Focus && node.chkDisabled !== true) ? chkName + "_" + c.FOCUS : chkName;
  2108. return consts.className.BUTTON + " " + c.DEFAULT + " " + chkName;
  2109. },
  2110. repairAllChk: function(setting, checked) {
  2111. if (setting.check.enable && setting.check.chkStyle === consts.checkbox.STYLE) {
  2112. var checkedKey = setting.data.key.checked,
  2113. childKey = setting.data.key.children,
  2114. root = data.getRoot(setting);
  2115. for (var i = 0, l = root[childKey].length; i<l ; i++) {
  2116. var node = root[childKey][i];
  2117. if (node.nocheck !== true && node.chkDisabled !== true) {
  2118. node[checkedKey] = checked;
  2119. }
  2120. view.setSonNodeCheckBox(setting, node, checked);
  2121. }
  2122. }
  2123. },
  2124. repairChkClass: function(setting, node) {
  2125. if (!node) return;
  2126. data.makeChkFlag(setting, node);
  2127. if (node.nocheck !== true) {
  2128. var checkObj = $$(node, consts.id.CHECK, setting);
  2129. view.setChkClass(setting, checkObj, node);
  2130. }
  2131. },
  2132. repairParentChkClass: function(setting, node) {
  2133. if (!node || !node.parentTId) return;
  2134. var pNode = node.getParentNode();
  2135. view.repairChkClass(setting, pNode);
  2136. view.repairParentChkClass(setting, pNode);
  2137. },
  2138. repairParentChkClassWithSelf: function(setting, node) {
  2139. if (!node) return;
  2140. var childKey = setting.data.key.children;
  2141. if (node[childKey] && node[childKey].length > 0) {
  2142. view.repairParentChkClass(setting, node[childKey][0]);
  2143. } else {
  2144. view.repairParentChkClass(setting, node);
  2145. }
  2146. },
  2147. repairSonChkDisabled: function(setting, node, chkDisabled, inherit) {
  2148. if (!node) return;
  2149. var childKey = setting.data.key.children;
  2150. if (node.chkDisabled != chkDisabled) {
  2151. node.chkDisabled = chkDisabled;
  2152. }
  2153. view.repairChkClass(setting, node);
  2154. if (node[childKey] && inherit) {
  2155. for (var i = 0, l = node[childKey].length; i < l; i++) {
  2156. var sNode = node[childKey][i];
  2157. view.repairSonChkDisabled(setting, sNode, chkDisabled, inherit);
  2158. }
  2159. }
  2160. },
  2161. repairParentChkDisabled: function(setting, node, chkDisabled, inherit) {
  2162. if (!node) return;
  2163. if (node.chkDisabled != chkDisabled && inherit) {
  2164. node.chkDisabled = chkDisabled;
  2165. }
  2166. view.repairChkClass(setting, node);
  2167. view.repairParentChkDisabled(setting, node.getParentNode(), chkDisabled, inherit);
  2168. },
  2169. setChkClass: function(setting, obj, node) {
  2170. if (!obj) return;
  2171. if (node.nocheck === true) {
  2172. obj.hide();
  2173. } else {
  2174. obj.show();
  2175. }
  2176. obj.attr('class', view.makeChkClass(setting, node));
  2177. },
  2178. setParentNodeCheckBox: function(setting, node, value, srcNode) {
  2179. var childKey = setting.data.key.children,
  2180. checkedKey = setting.data.key.checked,
  2181. checkObj = $$(node, consts.id.CHECK, setting);
  2182. if (!srcNode) srcNode = node;
  2183. data.makeChkFlag(setting, node);
  2184. if (node.nocheck !== true && node.chkDisabled !== true) {
  2185. node[checkedKey] = value;
  2186. view.setChkClass(setting, checkObj, node);
  2187. if (setting.check.autoCheckTrigger && node != srcNode) {
  2188. setting.treeObj.trigger(consts.event.CHECK, [null, setting.treeId, node]);
  2189. }
  2190. }
  2191. if (node.parentTId) {
  2192. var pSign = true;
  2193. if (!value) {
  2194. var pNodes = node.getParentNode()[childKey];
  2195. for (var i = 0, l = pNodes.length; i < l; i++) {
  2196. if ((pNodes[i].nocheck !== true && pNodes[i].chkDisabled !== true && pNodes[i][checkedKey])
  2197. || ((pNodes[i].nocheck === true || pNodes[i].chkDisabled === true) && pNodes[i].check_Child_State > 0)) {
  2198. pSign = false;
  2199. break;
  2200. }
  2201. }
  2202. }
  2203. if (pSign) {
  2204. view.setParentNodeCheckBox(setting, node.getParentNode(), value, srcNode);
  2205. }
  2206. }
  2207. },
  2208. setSonNodeCheckBox: function(setting, node, value, srcNode) {
  2209. if (!node) return;
  2210. var childKey = setting.data.key.children,
  2211. checkedKey = setting.data.key.checked,
  2212. checkObj = $$(node, consts.id.CHECK, setting);
  2213. if (!srcNode) srcNode = node;
  2214. var hasDisable = false;
  2215. if (node[childKey]) {
  2216. for (var i = 0, l = node[childKey].length; i < l && node.chkDisabled !== true; i++) {
  2217. var sNode = node[childKey][i];
  2218. view.setSonNodeCheckBox(setting, sNode, value, srcNode);
  2219. if (sNode.chkDisabled === true) hasDisable = true;
  2220. }
  2221. }
  2222. if (node != data.getRoot(setting) && node.chkDisabled !== true) {
  2223. if (hasDisable && node.nocheck !== true) {
  2224. data.makeChkFlag(setting, node);
  2225. }
  2226. if (node.nocheck !== true && node.chkDisabled !== true) {
  2227. node[checkedKey] = value;
  2228. if (!hasDisable) node.check_Child_State = (node[childKey] && node[childKey].length > 0) ? (value ? 2 : 0) : -1;
  2229. } else {
  2230. node.check_Child_State = -1;
  2231. }
  2232. view.setChkClass(setting, checkObj, node);
  2233. if (setting.check.autoCheckTrigger && node != srcNode && node.nocheck !== true && node.chkDisabled !== true) {
  2234. setting.treeObj.trigger(consts.event.CHECK, [null, setting.treeId, node]);
  2235. }
  2236. }
  2237. }
  2238. },
  2239. _z = {
  2240. tools: _tools,
  2241. view: _view,
  2242. event: _event,
  2243. data: _data
  2244. };
  2245. $.extend(true, $.fn.zTree.consts, _consts);
  2246. $.extend(true, $.fn.zTree._z, _z);
  2247. var zt = $.fn.zTree,
  2248. tools = zt._z.tools,
  2249. consts = zt.consts,
  2250. view = zt._z.view,
  2251. data = zt._z.data,
  2252. event = zt._z.event,
  2253. $$ = tools.$;
  2254. data.exSetting(_setting);
  2255. data.addInitBind(_bindEvent);
  2256. data.addInitUnBind(_unbindEvent);
  2257. data.addInitCache(_initCache);
  2258. data.addInitNode(_initNode);
  2259. data.addInitProxy(_eventProxy, true);
  2260. data.addInitRoot(_initRoot);
  2261. data.addBeforeA(_beforeA);
  2262. data.addZTreeTools(_zTreeTools);
  2263. var _createNodes = view.createNodes;
  2264. view.createNodes = function(setting, level, nodes, parentNode) {
  2265. if (_createNodes) _createNodes.apply(view, arguments);
  2266. if (!nodes) return;
  2267. view.repairParentChkClassWithSelf(setting, parentNode);
  2268. }
  2269. var _removeNode = view.removeNode;
  2270. view.removeNode = function(setting, node) {
  2271. var parentNode = node.getParentNode();
  2272. if (_removeNode) _removeNode.apply(view, arguments);
  2273. if (!node || !parentNode) return;
  2274. view.repairChkClass(setting, parentNode);
  2275. view.repairParentChkClass(setting, parentNode);
  2276. }
  2277. var _appendNodes = view.appendNodes;
  2278. view.appendNodes = function(setting, level, nodes, parentNode, initFlag, openFlag) {
  2279. var html = "";
  2280. if (_appendNodes) {
  2281. html = _appendNodes.apply(view, arguments);
  2282. }
  2283. if (parentNode) {
  2284. data.makeChkFlag(setting, parentNode);
  2285. }
  2286. return html;
  2287. }
  2288. })(jQuery);
  2289. /*
  2290. * JQuery zTree exedit v3.5.18
  2291. * http://zTree.me/
  2292. *
  2293. * Copyright (c) 2010 Hunter.z
  2294. *
  2295. * Licensed same as jquery - MIT License
  2296. * http://www.opensource.org/licenses/mit-license.php
  2297. *
  2298. * email: hunter.z@263.net
  2299. * Date: 2015-05-25
  2300. */
  2301. (function($){
  2302. //default consts of exedit
  2303. var _consts = {
  2304. event: {
  2305. DRAG: "ztree_drag",
  2306. DROP: "ztree_drop",
  2307. RENAME: "ztree_rename",
  2308. DRAGMOVE:"ztree_dragmove"
  2309. },
  2310. id: {
  2311. EDIT: "_edit",
  2312. INPUT: "_input",
  2313. REMOVE: "_remove"
  2314. },
  2315. move: {
  2316. TYPE_INNER: "inner",
  2317. TYPE_PREV: "prev",
  2318. TYPE_NEXT: "next"
  2319. },
  2320. node: {
  2321. CURSELECTED_EDIT: "curSelectedNode_Edit",
  2322. TMPTARGET_TREE: "tmpTargetzTree",
  2323. TMPTARGET_NODE: "tmpTargetNode"
  2324. }
  2325. },
  2326. //default setting of exedit
  2327. _setting = {
  2328. edit: {
  2329. enable: false,
  2330. editNameSelectAll: false,
  2331. showRemoveBtn: true,
  2332. showRenameBtn: true,
  2333. removeTitle: "remove",
  2334. renameTitle: "rename",
  2335. drag: {
  2336. autoExpandTrigger: false,
  2337. isCopy: true,
  2338. isMove: true,
  2339. prev: true,
  2340. next: true,
  2341. inner: true,
  2342. minMoveSize: 5,
  2343. borderMax: 10,
  2344. borderMin: -5,
  2345. maxShowNodeNum: 5,
  2346. autoOpenTime: 500
  2347. }
  2348. },
  2349. view: {
  2350. addHoverDom: null,
  2351. removeHoverDom: null
  2352. },
  2353. callback: {
  2354. beforeDrag:null,
  2355. beforeDragOpen:null,
  2356. beforeDrop:null,
  2357. beforeEditName:null,
  2358. beforeRename:null,
  2359. onDrag:null,
  2360. onDragMove:null,
  2361. onDrop:null,
  2362. onRename:null
  2363. }
  2364. },
  2365. //default root of exedit
  2366. _initRoot = function (setting) {
  2367. var r = data.getRoot(setting), rs = data.getRoots();
  2368. r.curEditNode = null;
  2369. r.curEditInput = null;
  2370. r.curHoverNode = null;
  2371. r.dragFlag = 0;
  2372. r.dragNodeShowBefore = [];
  2373. r.dragMaskList = new Array();
  2374. rs.showHoverDom = true;
  2375. },
  2376. //default cache of exedit
  2377. _initCache = function(treeId) {},
  2378. //default bind event of exedit
  2379. _bindEvent = function(setting) {
  2380. var o = setting.treeObj;
  2381. var c = consts.event;
  2382. o.bind(c.RENAME, function (event, treeId, treeNode, isCancel) {
  2383. tools.apply(setting.callback.onRename, [event, treeId, treeNode, isCancel]);
  2384. });
  2385. o.bind(c.DRAG, function (event, srcEvent, treeId, treeNodes) {
  2386. tools.apply(setting.callback.onDrag, [srcEvent, treeId, treeNodes]);
  2387. });
  2388. o.bind(c.DRAGMOVE,function(event, srcEvent, treeId, treeNodes){
  2389. tools.apply(setting.callback.onDragMove,[srcEvent, treeId, treeNodes]);
  2390. });
  2391. o.bind(c.DROP, function (event, srcEvent, treeId, treeNodes, targetNode, moveType, isCopy) {
  2392. tools.apply(setting.callback.onDrop, [srcEvent, treeId, treeNodes, targetNode, moveType, isCopy]);
  2393. });
  2394. },
  2395. _unbindEvent = function(setting) {
  2396. var o = setting.treeObj;
  2397. var c = consts.event;
  2398. o.unbind(c.RENAME);
  2399. o.unbind(c.DRAG);
  2400. o.unbind(c.DRAGMOVE);
  2401. o.unbind(c.DROP);
  2402. },
  2403. //default event proxy of exedit
  2404. _eventProxy = function(e) {
  2405. var target = e.target,
  2406. setting = data.getSetting(e.data.treeId),
  2407. relatedTarget = e.relatedTarget,
  2408. tId = "", node = null,
  2409. nodeEventType = "", treeEventType = "",
  2410. nodeEventCallback = null, treeEventCallback = null,
  2411. tmp = null;
  2412. if (tools.eqs(e.type, "mouseover")) {
  2413. tmp = tools.getMDom(setting, target, [{tagName:"a", attrName:"treeNode"+consts.id.A}]);
  2414. if (tmp) {
  2415. tId = tools.getNodeMainDom(tmp).id;
  2416. nodeEventType = "hoverOverNode";
  2417. }
  2418. } else if (tools.eqs(e.type, "mouseout")) {
  2419. tmp = tools.getMDom(setting, relatedTarget, [{tagName:"a", attrName:"treeNode"+consts.id.A}]);
  2420. if (!tmp) {
  2421. tId = "remove";
  2422. nodeEventType = "hoverOutNode";
  2423. }
  2424. } else if (tools.eqs(e.type, "mousedown")) {
  2425. tmp = tools.getMDom(setting, target, [{tagName:"a", attrName:"treeNode"+consts.id.A}]);
  2426. if (tmp) {
  2427. tId = tools.getNodeMainDom(tmp).id;
  2428. nodeEventType = "mousedownNode";
  2429. }
  2430. }
  2431. if (tId.length>0) {
  2432. node = data.getNodeCache(setting, tId);
  2433. switch (nodeEventType) {
  2434. case "mousedownNode" :
  2435. nodeEventCallback = _handler.onMousedownNode;
  2436. break;
  2437. case "hoverOverNode" :
  2438. nodeEventCallback = _handler.onHoverOverNode;
  2439. break;
  2440. case "hoverOutNode" :
  2441. nodeEventCallback = _handler.onHoverOutNode;
  2442. break;
  2443. }
  2444. }
  2445. var proxyResult = {
  2446. stop: false,
  2447. node: node,
  2448. nodeEventType: nodeEventType,
  2449. nodeEventCallback: nodeEventCallback,
  2450. treeEventType: treeEventType,
  2451. treeEventCallback: treeEventCallback
  2452. };
  2453. return proxyResult
  2454. },
  2455. //default init node of exedit
  2456. _initNode = function(setting, level, n, parentNode, isFirstNode, isLastNode, openFlag) {
  2457. if (!n) return;
  2458. n.isHover = false;
  2459. n.editNameFlag = false;
  2460. },
  2461. //update zTreeObj, add method of edit
  2462. _zTreeTools = function(setting, zTreeTools) {
  2463. zTreeTools.cancelEditName = function(newName) {
  2464. var root = data.getRoot(this.setting);
  2465. if (!root.curEditNode) return;
  2466. view.cancelCurEditNode(this.setting, newName?newName:null, true);
  2467. }
  2468. zTreeTools.copyNode = function(targetNode, node, moveType, isSilent) {
  2469. if (!node) return null;
  2470. if (targetNode && !targetNode.isParent && this.setting.data.keep.leaf && moveType === consts.move.TYPE_INNER) return null;
  2471. var _this = this,
  2472. newNode = tools.clone(node);
  2473. if (!targetNode) {
  2474. targetNode = null;
  2475. moveType = consts.move.TYPE_INNER;
  2476. }
  2477. if (moveType == consts.move.TYPE_INNER) {
  2478. function copyCallback() {
  2479. view.addNodes(_this.setting, targetNode, [newNode], isSilent);
  2480. }
  2481. if (tools.canAsync(this.setting, targetNode)) {
  2482. view.asyncNode(this.setting, targetNode, isSilent, copyCallback);
  2483. } else {
  2484. copyCallback();
  2485. }
  2486. } else {
  2487. view.addNodes(this.setting, targetNode.parentNode, [newNode], isSilent);
  2488. view.moveNode(this.setting, targetNode, newNode, moveType, false, isSilent);
  2489. }
  2490. return newNode;
  2491. }
  2492. zTreeTools.editName = function(node) {
  2493. if (!node || !node.tId || node !== data.getNodeCache(this.setting, node.tId)) return;
  2494. if (node.parentTId) view.expandCollapseParentNode(this.setting, node.getParentNode(), true);
  2495. view.editNode(this.setting, node)
  2496. }
  2497. zTreeTools.moveNode = function(targetNode, node, moveType, isSilent) {
  2498. if (!node) return node;
  2499. if (targetNode && !targetNode.isParent && this.setting.data.keep.leaf && moveType === consts.move.TYPE_INNER) {
  2500. return null;
  2501. } else if (targetNode && ((node.parentTId == targetNode.tId && moveType == consts.move.TYPE_INNER) || $$(node, this.setting).find("#" + targetNode.tId).length > 0)) {
  2502. return null;
  2503. } else if (!targetNode) {
  2504. targetNode = null;
  2505. }
  2506. var _this = this;
  2507. function moveCallback() {
  2508. view.moveNode(_this.setting, targetNode, node, moveType, false, isSilent);
  2509. }
  2510. if (tools.canAsync(this.setting, targetNode) && moveType === consts.move.TYPE_INNER) {
  2511. view.asyncNode(this.setting, targetNode, isSilent, moveCallback);
  2512. } else {
  2513. moveCallback();
  2514. }
  2515. return node;
  2516. }
  2517. zTreeTools.setEditable = function(editable) {
  2518. this.setting.edit.enable = editable;
  2519. return this.refresh();
  2520. }
  2521. },
  2522. //method of operate data
  2523. _data = {
  2524. setSonNodeLevel: function(setting, parentNode, node) {
  2525. if (!node) return;
  2526. var childKey = setting.data.key.children;
  2527. node.level = (parentNode)? parentNode.level + 1 : 0;
  2528. if (!node[childKey]) return;
  2529. for (var i = 0, l = node[childKey].length; i < l; i++) {
  2530. if (node[childKey][i]) data.setSonNodeLevel(setting, node, node[childKey][i]);
  2531. }
  2532. }
  2533. },
  2534. //method of event proxy
  2535. _event = {
  2536. },
  2537. //method of event handler
  2538. _handler = {
  2539. onHoverOverNode: function(event, node) {
  2540. var setting = data.getSetting(event.data.treeId),
  2541. root = data.getRoot(setting);
  2542. if (root.curHoverNode != node) {
  2543. _handler.onHoverOutNode(event);
  2544. }
  2545. root.curHoverNode = node;
  2546. view.addHoverDom(setting, node);
  2547. },
  2548. onHoverOutNode: function(event, node) {
  2549. var setting = data.getSetting(event.data.treeId),
  2550. root = data.getRoot(setting);
  2551. if (root.curHoverNode && !data.isSelectedNode(setting, root.curHoverNode)) {
  2552. view.removeTreeDom(setting, root.curHoverNode);
  2553. root.curHoverNode = null;
  2554. }
  2555. },
  2556. onMousedownNode: function(eventMouseDown, _node) {
  2557. var i,l,
  2558. setting = data.getSetting(eventMouseDown.data.treeId),
  2559. root = data.getRoot(setting), roots = data.getRoots();
  2560. //right click can't drag & drop
  2561. if (eventMouseDown.button == 2 || !setting.edit.enable || (!setting.edit.drag.isCopy && !setting.edit.drag.isMove)) return true;
  2562. //input of edit node name can't drag & drop
  2563. var target = eventMouseDown.target,
  2564. _nodes = data.getRoot(setting).curSelectedList,
  2565. nodes = [];
  2566. if (!data.isSelectedNode(setting, _node)) {
  2567. nodes = [_node];
  2568. } else {
  2569. for (i=0, l=_nodes.length; i<l; i++) {
  2570. if (_nodes[i].editNameFlag && tools.eqs(target.tagName, "input") && target.getAttribute("treeNode"+consts.id.INPUT) !== null) {
  2571. return true;
  2572. }
  2573. nodes.push(_nodes[i]);
  2574. if (nodes[0].parentTId !== _nodes[i].parentTId) {
  2575. nodes = [_node];
  2576. break;
  2577. }
  2578. }
  2579. }
  2580. view.editNodeBlur = true;
  2581. view.cancelCurEditNode(setting);
  2582. var doc = $(setting.treeObj.get(0).ownerDocument),
  2583. body = $(setting.treeObj.get(0).ownerDocument.body), curNode, tmpArrow, tmpTarget,
  2584. isOtherTree = false,
  2585. targetSetting = setting,
  2586. sourceSetting = setting,
  2587. preNode, nextNode,
  2588. preTmpTargetNodeId = null,
  2589. preTmpMoveType = null,
  2590. tmpTargetNodeId = null,
  2591. moveType = consts.move.TYPE_INNER,
  2592. mouseDownX = eventMouseDown.clientX,
  2593. mouseDownY = eventMouseDown.clientY,
  2594. startTime = (new Date()).getTime();
  2595. if (tools.uCanDo(setting)) {
  2596. doc.bind("mousemove", _docMouseMove);
  2597. }
  2598. function _docMouseMove(event) {
  2599. //avoid start drag after click node
  2600. if (root.dragFlag == 0 && Math.abs(mouseDownX - event.clientX) < setting.edit.drag.minMoveSize
  2601. && Math.abs(mouseDownY - event.clientY) < setting.edit.drag.minMoveSize) {
  2602. return true;
  2603. }
  2604. var i, l, tmpNode, tmpDom, tmpNodes,
  2605. childKey = setting.data.key.children;
  2606. body.css("cursor", "pointer");
  2607. if (root.dragFlag == 0) {
  2608. if (tools.apply(setting.callback.beforeDrag, [setting.treeId, nodes], true) == false) {
  2609. _docMouseUp(event);
  2610. return true;
  2611. }
  2612. for (i=0, l=nodes.length; i<l; i++) {
  2613. if (i==0) {
  2614. root.dragNodeShowBefore = [];
  2615. }
  2616. tmpNode = nodes[i];
  2617. if (tmpNode.isParent && tmpNode.open) {
  2618. view.expandCollapseNode(setting, tmpNode, !tmpNode.open);
  2619. root.dragNodeShowBefore[tmpNode.tId] = true;
  2620. } else {
  2621. root.dragNodeShowBefore[tmpNode.tId] = false;
  2622. }
  2623. }
  2624. root.dragFlag = 1;
  2625. roots.showHoverDom = false;
  2626. tools.showIfameMask(setting, true);
  2627. //sort
  2628. var isOrder = true, lastIndex = -1;
  2629. if (nodes.length>1) {
  2630. var pNodes = nodes[0].parentTId ? nodes[0].getParentNode()[childKey] : data.getNodes(setting);
  2631. tmpNodes = [];
  2632. for (i=0, l=pNodes.length; i<l; i++) {
  2633. if (root.dragNodeShowBefore[pNodes[i].tId] !== undefined) {
  2634. if (isOrder && lastIndex > -1 && (lastIndex+1) !== i) {
  2635. isOrder = false;
  2636. }
  2637. tmpNodes.push(pNodes[i]);
  2638. lastIndex = i;
  2639. }
  2640. if (nodes.length === tmpNodes.length) {
  2641. nodes = tmpNodes;
  2642. break;
  2643. }
  2644. }
  2645. }
  2646. if (isOrder) {
  2647. preNode = nodes[0].getPreNode();
  2648. nextNode = nodes[nodes.length-1].getNextNode();
  2649. }
  2650. //set node in selected
  2651. curNode = $$("<ul class='zTreeDragUL'></ul>", setting);
  2652. for (i=0, l=nodes.length; i<l; i++) {
  2653. tmpNode = nodes[i];
  2654. tmpNode.editNameFlag = false;
  2655. view.selectNode(setting, tmpNode, i>0);
  2656. view.removeTreeDom(setting, tmpNode);
  2657. if (i > setting.edit.drag.maxShowNodeNum-1) {
  2658. continue;
  2659. }
  2660. tmpDom = $$("<li id='"+ tmpNode.tId +"_tmp'></li>", setting);
  2661. tmpDom.append($$(tmpNode, consts.id.A, setting).clone());
  2662. tmpDom.css("padding", "0");
  2663. tmpDom.children("#" + tmpNode.tId + consts.id.A).removeClass(consts.node.CURSELECTED);
  2664. curNode.append(tmpDom);
  2665. if (i == setting.edit.drag.maxShowNodeNum-1) {
  2666. tmpDom = $$("<li id='"+ tmpNode.tId +"_moretmp'><a> ... </a></li>", setting);
  2667. curNode.append(tmpDom);
  2668. }
  2669. }
  2670. curNode.attr("id", nodes[0].tId + consts.id.UL + "_tmp");
  2671. curNode.addClass(setting.treeObj.attr("class"));
  2672. curNode.appendTo(body);
  2673. tmpArrow = $$("<span class='tmpzTreeMove_arrow'></span>", setting);
  2674. tmpArrow.attr("id", "zTreeMove_arrow_tmp");
  2675. tmpArrow.appendTo(body);
  2676. setting.treeObj.trigger(consts.event.DRAG, [event, setting.treeId, nodes]);
  2677. }
  2678. if (root.dragFlag == 1) {
  2679. if (tmpTarget && tmpArrow.attr("id") == event.target.id && tmpTargetNodeId && (event.clientX + doc.scrollLeft()+2) > ($("#" + tmpTargetNodeId + consts.id.A, tmpTarget).offset().left)) {
  2680. var xT = $("#" + tmpTargetNodeId + consts.id.A, tmpTarget);
  2681. event.target = (xT.length > 0) ? xT.get(0) : event.target;
  2682. } else if (tmpTarget) {
  2683. tmpTarget.removeClass(consts.node.TMPTARGET_TREE);
  2684. if (tmpTargetNodeId) $("#" + tmpTargetNodeId + consts.id.A, tmpTarget).removeClass(consts.node.TMPTARGET_NODE + "_" + consts.move.TYPE_PREV)
  2685. .removeClass(consts.node.TMPTARGET_NODE + "_" + _consts.move.TYPE_NEXT).removeClass(consts.node.TMPTARGET_NODE + "_" + _consts.move.TYPE_INNER);
  2686. }
  2687. tmpTarget = null;
  2688. tmpTargetNodeId = null;
  2689. //judge drag & drop in multi ztree
  2690. isOtherTree = false;
  2691. targetSetting = setting;
  2692. var settings = data.getSettings();
  2693. for (var s in settings) {
  2694. if (settings[s].treeId && settings[s].edit.enable && settings[s].treeId != setting.treeId
  2695. && (event.target.id == settings[s].treeId || $(event.target).parents("#" + settings[s].treeId).length>0)) {
  2696. isOtherTree = true;
  2697. targetSetting = settings[s];
  2698. }
  2699. }
  2700. var docScrollTop = doc.scrollTop(),
  2701. docScrollLeft = doc.scrollLeft(),
  2702. treeOffset = targetSetting.treeObj.offset(),
  2703. scrollHeight = targetSetting.treeObj.get(0).scrollHeight,
  2704. scrollWidth = targetSetting.treeObj.get(0).scrollWidth,
  2705. dTop = (event.clientY + docScrollTop - treeOffset.top),
  2706. dBottom = (targetSetting.treeObj.height() + treeOffset.top - event.clientY - docScrollTop),
  2707. dLeft = (event.clientX + docScrollLeft - treeOffset.left),
  2708. dRight = (targetSetting.treeObj.width() + treeOffset.left - event.clientX - docScrollLeft),
  2709. isTop = (dTop < setting.edit.drag.borderMax && dTop > setting.edit.drag.borderMin),
  2710. isBottom = (dBottom < setting.edit.drag.borderMax && dBottom > setting.edit.drag.borderMin),
  2711. isLeft = (dLeft < setting.edit.drag.borderMax && dLeft > setting.edit.drag.borderMin),
  2712. isRight = (dRight < setting.edit.drag.borderMax && dRight > setting.edit.drag.borderMin),
  2713. isTreeInner = dTop > setting.edit.drag.borderMin && dBottom > setting.edit.drag.borderMin && dLeft > setting.edit.drag.borderMin && dRight > setting.edit.drag.borderMin,
  2714. isTreeTop = (isTop && targetSetting.treeObj.scrollTop() <= 0),
  2715. isTreeBottom = (isBottom && (targetSetting.treeObj.scrollTop() + targetSetting.treeObj.height()+10) >= scrollHeight),
  2716. isTreeLeft = (isLeft && targetSetting.treeObj.scrollLeft() <= 0),
  2717. isTreeRight = (isRight && (targetSetting.treeObj.scrollLeft() + targetSetting.treeObj.width()+10) >= scrollWidth);
  2718. if (event.target && tools.isChildOrSelf(event.target, targetSetting.treeId)) {
  2719. //get node <li> dom
  2720. var targetObj = event.target;
  2721. while (targetObj && targetObj.tagName && !tools.eqs(targetObj.tagName, "li") && targetObj.id != targetSetting.treeId) {
  2722. targetObj = targetObj.parentNode;
  2723. }
  2724. var canMove = true;
  2725. //don't move to self or children of self
  2726. for (i=0, l=nodes.length; i<l; i++) {
  2727. tmpNode = nodes[i];
  2728. if (targetObj.id === tmpNode.tId) {
  2729. canMove = false;
  2730. break;
  2731. } else if ($$(tmpNode, setting).find("#" + targetObj.id).length > 0) {
  2732. canMove = false;
  2733. break;
  2734. }
  2735. }
  2736. if (canMove && event.target && tools.isChildOrSelf(event.target, targetObj.id + consts.id.A)) {
  2737. tmpTarget = $(targetObj);
  2738. tmpTargetNodeId = targetObj.id;
  2739. }
  2740. }
  2741. //the mouse must be in zTree
  2742. tmpNode = nodes[0];
  2743. if (isTreeInner && tools.isChildOrSelf(event.target, targetSetting.treeId)) {
  2744. //judge mouse move in root of ztree
  2745. if (!tmpTarget && (event.target.id == targetSetting.treeId || isTreeTop || isTreeBottom || isTreeLeft || isTreeRight) && (isOtherTree || (!isOtherTree && tmpNode.parentTId))) {
  2746. tmpTarget = targetSetting.treeObj;
  2747. }
  2748. //auto scroll top
  2749. if (isTop) {
  2750. targetSetting.treeObj.scrollTop(targetSetting.treeObj.scrollTop()-10);
  2751. } else if (isBottom) {
  2752. targetSetting.treeObj.scrollTop(targetSetting.treeObj.scrollTop()+10);
  2753. }
  2754. if (isLeft) {
  2755. targetSetting.treeObj.scrollLeft(targetSetting.treeObj.scrollLeft()-10);
  2756. } else if (isRight) {
  2757. targetSetting.treeObj.scrollLeft(targetSetting.treeObj.scrollLeft()+10);
  2758. }
  2759. //auto scroll left
  2760. if (tmpTarget && tmpTarget != targetSetting.treeObj && tmpTarget.offset().left < targetSetting.treeObj.offset().left) {
  2761. targetSetting.treeObj.scrollLeft(targetSetting.treeObj.scrollLeft()+ tmpTarget.offset().left - targetSetting.treeObj.offset().left);
  2762. }
  2763. }
  2764. curNode.css({
  2765. "top": (event.clientY + docScrollTop + 3) + "px",
  2766. "left": (event.clientX + docScrollLeft + 3) + "px"
  2767. });
  2768. var dX = 0;
  2769. var dY = 0;
  2770. if (tmpTarget && tmpTarget.attr("id")!=targetSetting.treeId) {
  2771. var tmpTargetNode = tmpTargetNodeId == null ? null: data.getNodeCache(targetSetting, tmpTargetNodeId),
  2772. isCopy = ((event.ctrlKey || event.metaKey) && setting.edit.drag.isMove && setting.edit.drag.isCopy) || (!setting.edit.drag.isMove && setting.edit.drag.isCopy),
  2773. isPrev = !!(preNode && tmpTargetNodeId === preNode.tId),
  2774. isNext = !!(nextNode && tmpTargetNodeId === nextNode.tId),
  2775. isInner = (tmpNode.parentTId && tmpNode.parentTId == tmpTargetNodeId),
  2776. canPrev = (isCopy || !isNext) && tools.apply(targetSetting.edit.drag.prev, [targetSetting.treeId, nodes, tmpTargetNode], !!targetSetting.edit.drag.prev),
  2777. canNext = (isCopy || !isPrev) && tools.apply(targetSetting.edit.drag.next, [targetSetting.treeId, nodes, tmpTargetNode], !!targetSetting.edit.drag.next),
  2778. canInner = (isCopy || !isInner) && !(targetSetting.data.keep.leaf && !tmpTargetNode.isParent) && tools.apply(targetSetting.edit.drag.inner, [targetSetting.treeId, nodes, tmpTargetNode], !!targetSetting.edit.drag.inner);
  2779. if (!canPrev && !canNext && !canInner) {
  2780. tmpTarget = null;
  2781. tmpTargetNodeId = "";
  2782. moveType = consts.move.TYPE_INNER;
  2783. tmpArrow.css({
  2784. "display":"none"
  2785. });
  2786. if (window.zTreeMoveTimer) {
  2787. clearTimeout(window.zTreeMoveTimer);
  2788. window.zTreeMoveTargetNodeTId = null
  2789. }
  2790. } else {
  2791. var tmpTargetA = $("#" + tmpTargetNodeId + consts.id.A, tmpTarget),
  2792. tmpNextA = tmpTargetNode.isLastNode ? null : $("#" + tmpTargetNode.getNextNode().tId + consts.id.A, tmpTarget.next()),
  2793. tmpTop = tmpTargetA.offset().top,
  2794. tmpLeft = tmpTargetA.offset().left,
  2795. prevPercent = canPrev ? (canInner ? 0.25 : (canNext ? 0.5 : 1) ) : -1,
  2796. nextPercent = canNext ? (canInner ? 0.75 : (canPrev ? 0.5 : 0) ) : -1,
  2797. dY_percent = (event.clientY + docScrollTop - tmpTop)/tmpTargetA.height();
  2798. if ((prevPercent==1 ||dY_percent<=prevPercent && dY_percent>=-.2) && canPrev) {
  2799. dX = 1 - tmpArrow.width();
  2800. dY = tmpTop - tmpArrow.height()/2;
  2801. moveType = consts.move.TYPE_PREV;
  2802. } else if ((nextPercent==0 || dY_percent>=nextPercent && dY_percent<=1.2) && canNext) {
  2803. dX = 1 - tmpArrow.width();
  2804. dY = (tmpNextA == null || (tmpTargetNode.isParent && tmpTargetNode.open)) ? (tmpTop + tmpTargetA.height() - tmpArrow.height()/2) : (tmpNextA.offset().top - tmpArrow.height()/2);
  2805. moveType = consts.move.TYPE_NEXT;
  2806. }else {
  2807. dX = 5 - tmpArrow.width();
  2808. dY = tmpTop;
  2809. moveType = consts.move.TYPE_INNER;
  2810. }
  2811. tmpArrow.css({
  2812. "display":"block",
  2813. "top": dY + "px",
  2814. "left": (tmpLeft + dX) + "px"
  2815. });
  2816. tmpTargetA.addClass(consts.node.TMPTARGET_NODE + "_" + moveType);
  2817. if (preTmpTargetNodeId != tmpTargetNodeId || preTmpMoveType != moveType) {
  2818. startTime = (new Date()).getTime();
  2819. }
  2820. if (tmpTargetNode && tmpTargetNode.isParent && moveType == consts.move.TYPE_INNER) {
  2821. var startTimer = true;
  2822. if (window.zTreeMoveTimer && window.zTreeMoveTargetNodeTId !== tmpTargetNode.tId) {
  2823. clearTimeout(window.zTreeMoveTimer);
  2824. window.zTreeMoveTargetNodeTId = null;
  2825. }else if (window.zTreeMoveTimer && window.zTreeMoveTargetNodeTId === tmpTargetNode.tId) {
  2826. startTimer = false;
  2827. }
  2828. if (startTimer) {
  2829. window.zTreeMoveTimer = setTimeout(function() {
  2830. if (moveType != consts.move.TYPE_INNER) return;
  2831. if (tmpTargetNode && tmpTargetNode.isParent && !tmpTargetNode.open && (new Date()).getTime() - startTime > targetSetting.edit.drag.autoOpenTime
  2832. && tools.apply(targetSetting.callback.beforeDragOpen, [targetSetting.treeId, tmpTargetNode], true)) {
  2833. view.switchNode(targetSetting, tmpTargetNode);
  2834. if (targetSetting.edit.drag.autoExpandTrigger) {
  2835. targetSetting.treeObj.trigger(consts.event.EXPAND, [targetSetting.treeId, tmpTargetNode]);
  2836. }
  2837. }
  2838. }, targetSetting.edit.drag.autoOpenTime+50);
  2839. window.zTreeMoveTargetNodeTId = tmpTargetNode.tId;
  2840. }
  2841. }
  2842. }
  2843. } else {
  2844. moveType = consts.move.TYPE_INNER;
  2845. if (tmpTarget && tools.apply(targetSetting.edit.drag.inner, [targetSetting.treeId, nodes, null], !!targetSetting.edit.drag.inner)) {
  2846. tmpTarget.addClass(consts.node.TMPTARGET_TREE);
  2847. } else {
  2848. tmpTarget = null;
  2849. }
  2850. tmpArrow.css({
  2851. "display":"none"
  2852. });
  2853. if (window.zTreeMoveTimer) {
  2854. clearTimeout(window.zTreeMoveTimer);
  2855. window.zTreeMoveTargetNodeTId = null;
  2856. }
  2857. }
  2858. preTmpTargetNodeId = tmpTargetNodeId;
  2859. preTmpMoveType = moveType;
  2860. setting.treeObj.trigger(consts.event.DRAGMOVE, [event, setting.treeId, nodes]);
  2861. }
  2862. return false;
  2863. }
  2864. doc.bind("mouseup", _docMouseUp);
  2865. function _docMouseUp(event) {
  2866. if (window.zTreeMoveTimer) {
  2867. clearTimeout(window.zTreeMoveTimer);
  2868. window.zTreeMoveTargetNodeTId = null;
  2869. }
  2870. preTmpTargetNodeId = null;
  2871. preTmpMoveType = null;
  2872. doc.unbind("mousemove", _docMouseMove);
  2873. doc.unbind("mouseup", _docMouseUp);
  2874. doc.unbind("selectstart", _docSelect);
  2875. body.css("cursor", "auto");
  2876. if (tmpTarget) {
  2877. tmpTarget.removeClass(consts.node.TMPTARGET_TREE);
  2878. if (tmpTargetNodeId) $("#" + tmpTargetNodeId + consts.id.A, tmpTarget).removeClass(consts.node.TMPTARGET_NODE + "_" + consts.move.TYPE_PREV)
  2879. .removeClass(consts.node.TMPTARGET_NODE + "_" + _consts.move.TYPE_NEXT).removeClass(consts.node.TMPTARGET_NODE + "_" + _consts.move.TYPE_INNER);
  2880. }
  2881. tools.showIfameMask(setting, false);
  2882. roots.showHoverDom = true;
  2883. if (root.dragFlag == 0) return;
  2884. root.dragFlag = 0;
  2885. var i, l, tmpNode;
  2886. for (i=0, l=nodes.length; i<l; i++) {
  2887. tmpNode = nodes[i];
  2888. if (tmpNode.isParent && root.dragNodeShowBefore[tmpNode.tId] && !tmpNode.open) {
  2889. view.expandCollapseNode(setting, tmpNode, !tmpNode.open);
  2890. delete root.dragNodeShowBefore[tmpNode.tId];
  2891. }
  2892. }
  2893. if (curNode) curNode.remove();
  2894. if (tmpArrow) tmpArrow.remove();
  2895. var isCopy = ((event.ctrlKey || event.metaKey) && setting.edit.drag.isMove && setting.edit.drag.isCopy) || (!setting.edit.drag.isMove && setting.edit.drag.isCopy);
  2896. if (!isCopy && tmpTarget && tmpTargetNodeId && nodes[0].parentTId && tmpTargetNodeId==nodes[0].parentTId && moveType == consts.move.TYPE_INNER) {
  2897. tmpTarget = null;
  2898. }
  2899. if (tmpTarget) {
  2900. var dragTargetNode = tmpTargetNodeId == null ? null: data.getNodeCache(targetSetting, tmpTargetNodeId);
  2901. if (tools.apply(setting.callback.beforeDrop, [targetSetting.treeId, nodes, dragTargetNode, moveType, isCopy], true) == false) {
  2902. view.selectNodes(sourceSetting, nodes);
  2903. return;
  2904. }
  2905. var newNodes = isCopy ? tools.clone(nodes) : nodes;
  2906. function dropCallback() {
  2907. if (isOtherTree) {
  2908. if (!isCopy) {
  2909. for(var i=0, l=nodes.length; i<l; i++) {
  2910. view.removeNode(setting, nodes[i]);
  2911. }
  2912. }
  2913. if (moveType == consts.move.TYPE_INNER) {
  2914. view.addNodes(targetSetting, dragTargetNode, newNodes);
  2915. } else {
  2916. view.addNodes(targetSetting, dragTargetNode.getParentNode(), newNodes);
  2917. if (moveType == consts.move.TYPE_PREV) {
  2918. for (i=0, l=newNodes.length; i<l; i++) {
  2919. view.moveNode(targetSetting, dragTargetNode, newNodes[i], moveType, false);
  2920. }
  2921. } else {
  2922. for (i=-1, l=newNodes.length-1; i<l; l--) {
  2923. view.moveNode(targetSetting, dragTargetNode, newNodes[l], moveType, false);
  2924. }
  2925. }
  2926. }
  2927. } else {
  2928. if (isCopy && moveType == consts.move.TYPE_INNER) {
  2929. view.addNodes(targetSetting, dragTargetNode, newNodes);
  2930. } else {
  2931. if (isCopy) {
  2932. view.addNodes(targetSetting, dragTargetNode.getParentNode(), newNodes);
  2933. }
  2934. if (moveType != consts.move.TYPE_NEXT) {
  2935. for (i=0, l=newNodes.length; i<l; i++) {
  2936. view.moveNode(targetSetting, dragTargetNode, newNodes[i], moveType, false);
  2937. }
  2938. } else {
  2939. for (i=-1, l=newNodes.length-1; i<l; l--) {
  2940. view.moveNode(targetSetting, dragTargetNode, newNodes[l], moveType, false);
  2941. }
  2942. }
  2943. }
  2944. }
  2945. view.selectNodes(targetSetting, newNodes);
  2946. $$(newNodes[0], setting).focus().blur();
  2947. setting.treeObj.trigger(consts.event.DROP, [event, targetSetting.treeId, newNodes, dragTargetNode, moveType, isCopy]);
  2948. }
  2949. if (moveType == consts.move.TYPE_INNER && tools.canAsync(targetSetting, dragTargetNode)) {
  2950. view.asyncNode(targetSetting, dragTargetNode, false, dropCallback);
  2951. } else {
  2952. dropCallback();
  2953. }
  2954. } else {
  2955. view.selectNodes(sourceSetting, nodes);
  2956. setting.treeObj.trigger(consts.event.DROP, [event, setting.treeId, nodes, null, null, null]);
  2957. }
  2958. }
  2959. doc.bind("selectstart", _docSelect);
  2960. function _docSelect() {
  2961. return false;
  2962. }
  2963. //Avoid FireFox's Bug
  2964. //If zTree Div CSS set 'overflow', so drag node outside of zTree, and event.target is error.
  2965. if(eventMouseDown.preventDefault) {
  2966. eventMouseDown.preventDefault();
  2967. }
  2968. return true;
  2969. }
  2970. },
  2971. //method of tools for zTree
  2972. _tools = {
  2973. getAbs: function (obj) {
  2974. var oRect = obj.getBoundingClientRect(),
  2975. scrollTop = document.body.scrollTop+document.documentElement.scrollTop,
  2976. scrollLeft = document.body.scrollLeft+document.documentElement.scrollLeft;
  2977. return [oRect.left+scrollLeft,oRect.top+scrollTop];
  2978. },
  2979. inputFocus: function(inputObj) {
  2980. if (inputObj.get(0)) {
  2981. inputObj.focus();
  2982. tools.setCursorPosition(inputObj.get(0), inputObj.val().length);
  2983. }
  2984. },
  2985. inputSelect: function(inputObj) {
  2986. if (inputObj.get(0)) {
  2987. inputObj.focus();
  2988. inputObj.select();
  2989. }
  2990. },
  2991. setCursorPosition: function(obj, pos){
  2992. if(obj.setSelectionRange) {
  2993. obj.focus();
  2994. obj.setSelectionRange(pos,pos);
  2995. } else if (obj.createTextRange) {
  2996. var range = obj.createTextRange();
  2997. range.collapse(true);
  2998. range.moveEnd('character', pos);
  2999. range.moveStart('character', pos);
  3000. range.select();
  3001. }
  3002. },
  3003. showIfameMask: function(setting, showSign) {
  3004. var root = data.getRoot(setting);
  3005. //clear full mask
  3006. while (root.dragMaskList.length > 0) {
  3007. root.dragMaskList[0].remove();
  3008. root.dragMaskList.shift();
  3009. }
  3010. if (showSign) {
  3011. //show mask
  3012. var iframeList = $$("iframe", setting);
  3013. for (var i = 0, l = iframeList.length; i < l; i++) {
  3014. var obj = iframeList.get(i),
  3015. r = tools.getAbs(obj),
  3016. dragMask = $$("<div id='zTreeMask_" + i + "' class='zTreeMask' style='top:" + r[1] + "px; left:" + r[0] + "px; width:" + obj.offsetWidth + "px; height:" + obj.offsetHeight + "px;'></div>", setting);
  3017. dragMask.appendTo($$("body", setting));
  3018. root.dragMaskList.push(dragMask);
  3019. }
  3020. }
  3021. }
  3022. },
  3023. //method of operate ztree dom
  3024. _view = {
  3025. addEditBtn: function(setting, node) {
  3026. if (node.editNameFlag || $$(node, consts.id.EDIT, setting).length > 0) {
  3027. return;
  3028. }
  3029. if (!tools.apply(setting.edit.showRenameBtn, [setting.treeId, node], setting.edit.showRenameBtn)) {
  3030. return;
  3031. }
  3032. var aObj = $$(node, consts.id.A, setting),
  3033. editStr = "<span class='" + consts.className.BUTTON + " edit' id='" + node.tId + consts.id.EDIT + "' title='"+tools.apply(setting.edit.renameTitle, [setting.treeId, node], setting.edit.renameTitle)+"' treeNode"+consts.id.EDIT+" style='display:none;'></span>";
  3034. aObj.append(editStr);
  3035. $$(node, consts.id.EDIT, setting).bind('click',
  3036. function() {
  3037. if (!tools.uCanDo(setting) || tools.apply(setting.callback.beforeEditName, [setting.treeId, node], true) == false) return false;
  3038. view.editNode(setting, node);
  3039. return false;
  3040. }
  3041. ).show();
  3042. },
  3043. addRemoveBtn: function(setting, node) {
  3044. if (node.editNameFlag || $$(node, consts.id.REMOVE, setting).length > 0) {
  3045. return;
  3046. }
  3047. if (!tools.apply(setting.edit.showRemoveBtn, [setting.treeId, node], setting.edit.showRemoveBtn)) {
  3048. return;
  3049. }
  3050. var aObj = $$(node, consts.id.A, setting),
  3051. removeStr = "<span class='" + consts.className.BUTTON + " remove' id='" + node.tId + consts.id.REMOVE + "' title='"+tools.apply(setting.edit.removeTitle, [setting.treeId, node], setting.edit.removeTitle)+"' treeNode"+consts.id.REMOVE+" style='display:none;'></span>";
  3052. aObj.append(removeStr);
  3053. $$(node, consts.id.REMOVE, setting).bind('click',
  3054. function() {
  3055. if (!tools.uCanDo(setting) || tools.apply(setting.callback.beforeRemove, [setting.treeId, node], true) == false) return false;
  3056. view.removeNode(setting, node);
  3057. setting.treeObj.trigger(consts.event.REMOVE, [setting.treeId, node]);
  3058. return false;
  3059. }
  3060. ).bind('mousedown',
  3061. function(eventMouseDown) {
  3062. return true;
  3063. }
  3064. ).show();
  3065. },
  3066. addHoverDom: function(setting, node) {
  3067. if (data.getRoots().showHoverDom) {
  3068. node.isHover = true;
  3069. if (setting.edit.enable) {
  3070. view.addEditBtn(setting, node);
  3071. view.addRemoveBtn(setting, node);
  3072. }
  3073. tools.apply(setting.view.addHoverDom, [setting.treeId, node]);
  3074. }
  3075. },
  3076. cancelCurEditNode: function (setting, forceName, isCancel) {
  3077. var root = data.getRoot(setting),
  3078. nameKey = setting.data.key.name,
  3079. node = root.curEditNode;
  3080. if (node) {
  3081. var inputObj = root.curEditInput,
  3082. newName = forceName ? forceName:(isCancel ? node[nameKey]: inputObj.val());
  3083. if (tools.apply(setting.callback.beforeRename, [setting.treeId, node, newName, isCancel], true) === false) {
  3084. return false;
  3085. }
  3086. node[nameKey] = newName;
  3087. var aObj = $$(node, consts.id.A, setting);
  3088. aObj.removeClass(consts.node.CURSELECTED_EDIT);
  3089. inputObj.unbind();
  3090. view.setNodeName(setting, node);
  3091. node.editNameFlag = false;
  3092. root.curEditNode = null;
  3093. root.curEditInput = null;
  3094. view.selectNode(setting, node, false);
  3095. setting.treeObj.trigger(consts.event.RENAME, [setting.treeId, node, isCancel]);
  3096. }
  3097. root.noSelection = true;
  3098. return true;
  3099. },
  3100. editNode: function(setting, node) {
  3101. var root = data.getRoot(setting);
  3102. view.editNodeBlur = false;
  3103. if (data.isSelectedNode(setting, node) && root.curEditNode == node && node.editNameFlag) {
  3104. setTimeout(function() {tools.inputFocus(root.curEditInput);}, 0);
  3105. return;
  3106. }
  3107. var nameKey = setting.data.key.name;
  3108. node.editNameFlag = true;
  3109. view.removeTreeDom(setting, node);
  3110. view.cancelCurEditNode(setting);
  3111. view.selectNode(setting, node, false);
  3112. $$(node, consts.id.SPAN, setting).html("<input type=text class='rename' id='" + node.tId + consts.id.INPUT + "' treeNode" + consts.id.INPUT + " >");
  3113. var inputObj = $$(node, consts.id.INPUT, setting);
  3114. inputObj.attr("value", node[nameKey]);
  3115. if (setting.edit.editNameSelectAll) {
  3116. tools.inputSelect(inputObj);
  3117. } else {
  3118. tools.inputFocus(inputObj);
  3119. }
  3120. inputObj.bind('blur', function(event) {
  3121. if (!view.editNodeBlur) {
  3122. view.cancelCurEditNode(setting);
  3123. }
  3124. }).bind('keydown', function(event) {
  3125. if (event.keyCode=="13") {
  3126. view.editNodeBlur = true;
  3127. view.cancelCurEditNode(setting);
  3128. } else if (event.keyCode=="27") {
  3129. view.cancelCurEditNode(setting, null, true);
  3130. }
  3131. }).bind('click', function(event) {
  3132. return false;
  3133. }).bind('dblclick', function(event) {
  3134. return false;
  3135. });
  3136. $$(node, consts.id.A, setting).addClass(consts.node.CURSELECTED_EDIT);
  3137. root.curEditInput = inputObj;
  3138. root.noSelection = false;
  3139. root.curEditNode = node;
  3140. },
  3141. moveNode: function(setting, targetNode, node, moveType, animateFlag, isSilent) {
  3142. var root = data.getRoot(setting),
  3143. childKey = setting.data.key.children;
  3144. if (targetNode == node) return;
  3145. if (setting.data.keep.leaf && targetNode && !targetNode.isParent && moveType == consts.move.TYPE_INNER) return;
  3146. var oldParentNode = (node.parentTId ? node.getParentNode(): root),
  3147. targetNodeIsRoot = (targetNode === null || targetNode == root);
  3148. if (targetNodeIsRoot && targetNode === null) targetNode = root;
  3149. if (targetNodeIsRoot) moveType = consts.move.TYPE_INNER;
  3150. var targetParentNode = (targetNode.parentTId ? targetNode.getParentNode() : root);
  3151. if (moveType != consts.move.TYPE_PREV && moveType != consts.move.TYPE_NEXT) {
  3152. moveType = consts.move.TYPE_INNER;
  3153. }
  3154. if (moveType == consts.move.TYPE_INNER) {
  3155. if (targetNodeIsRoot) {
  3156. //parentTId of root node is null
  3157. node.parentTId = null;
  3158. } else {
  3159. if (!targetNode.isParent) {
  3160. targetNode.isParent = true;
  3161. targetNode.open = !!targetNode.open;
  3162. view.setNodeLineIcos(setting, targetNode);
  3163. }
  3164. node.parentTId = targetNode.tId;
  3165. }
  3166. }
  3167. //move node Dom
  3168. var targetObj, target_ulObj;
  3169. if (targetNodeIsRoot) {
  3170. targetObj = setting.treeObj;
  3171. target_ulObj = targetObj;
  3172. } else {
  3173. if (!isSilent && moveType == consts.move.TYPE_INNER) {
  3174. view.expandCollapseNode(setting, targetNode, true, false);
  3175. } else if (!isSilent) {
  3176. view.expandCollapseNode(setting, targetNode.getParentNode(), true, false);
  3177. }
  3178. targetObj = $$(targetNode, setting);
  3179. target_ulObj = $$(targetNode, consts.id.UL, setting);
  3180. if (!!targetObj.get(0) && !target_ulObj.get(0)) {
  3181. var ulstr = [];
  3182. view.makeUlHtml(setting, targetNode, ulstr, '');
  3183. targetObj.append(ulstr.join(''));
  3184. }
  3185. target_ulObj = $$(targetNode, consts.id.UL, setting);
  3186. }
  3187. var nodeDom = $$(node, setting);
  3188. if (!nodeDom.get(0)) {
  3189. nodeDom = view.appendNodes(setting, node.level, [node], null, false, true).join('');
  3190. } else if (!targetObj.get(0)) {
  3191. nodeDom.remove();
  3192. }
  3193. if (target_ulObj.get(0) && moveType == consts.move.TYPE_INNER) {
  3194. target_ulObj.append(nodeDom);
  3195. } else if (targetObj.get(0) && moveType == consts.move.TYPE_PREV) {
  3196. targetObj.before(nodeDom);
  3197. } else if (targetObj.get(0) && moveType == consts.move.TYPE_NEXT) {
  3198. targetObj.after(nodeDom);
  3199. }
  3200. //repair the data after move
  3201. var i,l,
  3202. tmpSrcIndex = -1,
  3203. tmpTargetIndex = 0,
  3204. oldNeighbor = null,
  3205. newNeighbor = null,
  3206. oldLevel = node.level;
  3207. if (node.isFirstNode) {
  3208. tmpSrcIndex = 0;
  3209. if (oldParentNode[childKey].length > 1 ) {
  3210. oldNeighbor = oldParentNode[childKey][1];
  3211. oldNeighbor.isFirstNode = true;
  3212. }
  3213. } else if (node.isLastNode) {
  3214. tmpSrcIndex = oldParentNode[childKey].length -1;
  3215. oldNeighbor = oldParentNode[childKey][tmpSrcIndex - 1];
  3216. oldNeighbor.isLastNode = true;
  3217. } else {
  3218. for (i = 0, l = oldParentNode[childKey].length; i < l; i++) {
  3219. if (oldParentNode[childKey][i].tId == node.tId) {
  3220. tmpSrcIndex = i;
  3221. break;
  3222. }
  3223. }
  3224. }
  3225. if (tmpSrcIndex >= 0) {
  3226. oldParentNode[childKey].splice(tmpSrcIndex, 1);
  3227. }
  3228. if (moveType != consts.move.TYPE_INNER) {
  3229. for (i = 0, l = targetParentNode[childKey].length; i < l; i++) {
  3230. if (targetParentNode[childKey][i].tId == targetNode.tId) tmpTargetIndex = i;
  3231. }
  3232. }
  3233. if (moveType == consts.move.TYPE_INNER) {
  3234. if (!targetNode[childKey]) targetNode[childKey] = new Array();
  3235. if (targetNode[childKey].length > 0) {
  3236. newNeighbor = targetNode[childKey][targetNode[childKey].length - 1];
  3237. newNeighbor.isLastNode = false;
  3238. }
  3239. targetNode[childKey].splice(targetNode[childKey].length, 0, node);
  3240. node.isLastNode = true;
  3241. node.isFirstNode = (targetNode[childKey].length == 1);
  3242. } else if (targetNode.isFirstNode && moveType == consts.move.TYPE_PREV) {
  3243. targetParentNode[childKey].splice(tmpTargetIndex, 0, node);
  3244. newNeighbor = targetNode;
  3245. newNeighbor.isFirstNode = false;
  3246. node.parentTId = targetNode.parentTId;
  3247. node.isFirstNode = true;
  3248. node.isLastNode = false;
  3249. } else if (targetNode.isLastNode && moveType == consts.move.TYPE_NEXT) {
  3250. targetParentNode[childKey].splice(tmpTargetIndex + 1, 0, node);
  3251. newNeighbor = targetNode;
  3252. newNeighbor.isLastNode = false;
  3253. node.parentTId = targetNode.parentTId;
  3254. node.isFirstNode = false;
  3255. node.isLastNode = true;
  3256. } else {
  3257. if (moveType == consts.move.TYPE_PREV) {
  3258. targetParentNode[childKey].splice(tmpTargetIndex, 0, node);
  3259. } else {
  3260. targetParentNode[childKey].splice(tmpTargetIndex + 1, 0, node);
  3261. }
  3262. node.parentTId = targetNode.parentTId;
  3263. node.isFirstNode = false;
  3264. node.isLastNode = false;
  3265. }
  3266. data.fixPIdKeyValue(setting, node);
  3267. data.setSonNodeLevel(setting, node.getParentNode(), node);
  3268. //repair node what been moved
  3269. view.setNodeLineIcos(setting, node);
  3270. view.repairNodeLevelClass(setting, node, oldLevel)
  3271. //repair node's old parentNode dom
  3272. if (!setting.data.keep.parent && oldParentNode[childKey].length < 1) {
  3273. //old parentNode has no child nodes
  3274. oldParentNode.isParent = false;
  3275. oldParentNode.open = false;
  3276. var tmp_ulObj = $$(oldParentNode, consts.id.UL, setting),
  3277. tmp_switchObj = $$(oldParentNode, consts.id.SWITCH, setting),
  3278. tmp_icoObj = $$(oldParentNode, consts.id.ICON, setting);
  3279. view.replaceSwitchClass(oldParentNode, tmp_switchObj, consts.folder.DOCU);
  3280. view.replaceIcoClass(oldParentNode, tmp_icoObj, consts.folder.DOCU);
  3281. tmp_ulObj.css("display", "none");
  3282. } else if (oldNeighbor) {
  3283. //old neigbor node
  3284. view.setNodeLineIcos(setting, oldNeighbor);
  3285. }
  3286. //new neigbor node
  3287. if (newNeighbor) {
  3288. view.setNodeLineIcos(setting, newNeighbor);
  3289. }
  3290. //repair checkbox / radio
  3291. if (!!setting.check && setting.check.enable && view.repairChkClass) {
  3292. view.repairChkClass(setting, oldParentNode);
  3293. view.repairParentChkClassWithSelf(setting, oldParentNode);
  3294. if (oldParentNode != node.parent)
  3295. view.repairParentChkClassWithSelf(setting, node);
  3296. }
  3297. //expand parents after move
  3298. if (!isSilent) {
  3299. view.expandCollapseParentNode(setting, node.getParentNode(), true, animateFlag);
  3300. }
  3301. },
  3302. removeEditBtn: function(setting, node) {
  3303. $$(node, consts.id.EDIT, setting).unbind().remove();
  3304. },
  3305. removeRemoveBtn: function(setting, node) {
  3306. $$(node, consts.id.REMOVE, setting).unbind().remove();
  3307. },
  3308. removeTreeDom: function(setting, node) {
  3309. node.isHover = false;
  3310. view.removeEditBtn(setting, node);
  3311. view.removeRemoveBtn(setting, node);
  3312. tools.apply(setting.view.removeHoverDom, [setting.treeId, node]);
  3313. },
  3314. repairNodeLevelClass: function(setting, node, oldLevel) {
  3315. if (oldLevel === node.level) return;
  3316. var liObj = $$(node, setting),
  3317. aObj = $$(node, consts.id.A, setting),
  3318. ulObj = $$(node, consts.id.UL, setting),
  3319. oldClass = consts.className.LEVEL + oldLevel,
  3320. newClass = consts.className.LEVEL + node.level;
  3321. liObj.removeClass(oldClass);
  3322. liObj.addClass(newClass);
  3323. aObj.removeClass(oldClass);
  3324. aObj.addClass(newClass);
  3325. ulObj.removeClass(oldClass);
  3326. ulObj.addClass(newClass);
  3327. },
  3328. selectNodes : function(setting, nodes) {
  3329. for (var i=0, l=nodes.length; i<l; i++) {
  3330. view.selectNode(setting, nodes[i], i>0);
  3331. }
  3332. }
  3333. },
  3334. _z = {
  3335. tools: _tools,
  3336. view: _view,
  3337. event: _event,
  3338. data: _data
  3339. };
  3340. $.extend(true, $.fn.zTree.consts, _consts);
  3341. $.extend(true, $.fn.zTree._z, _z);
  3342. var zt = $.fn.zTree,
  3343. tools = zt._z.tools,
  3344. consts = zt.consts,
  3345. view = zt._z.view,
  3346. data = zt._z.data,
  3347. event = zt._z.event,
  3348. $$ = tools.$;
  3349. data.exSetting(_setting);
  3350. data.addInitBind(_bindEvent);
  3351. data.addInitUnBind(_unbindEvent);
  3352. data.addInitCache(_initCache);
  3353. data.addInitNode(_initNode);
  3354. data.addInitProxy(_eventProxy);
  3355. data.addInitRoot(_initRoot);
  3356. data.addZTreeTools(_zTreeTools);
  3357. var _cancelPreSelectedNode = view.cancelPreSelectedNode;
  3358. view.cancelPreSelectedNode = function (setting, node) {
  3359. var list = data.getRoot(setting).curSelectedList;
  3360. for (var i=0, j=list.length; i<j; i++) {
  3361. if (!node || node === list[i]) {
  3362. view.removeTreeDom(setting, list[i]);
  3363. if (node) break;
  3364. }
  3365. }
  3366. if (_cancelPreSelectedNode) _cancelPreSelectedNode.apply(view, arguments);
  3367. }
  3368. var _createNodes = view.createNodes;
  3369. view.createNodes = function(setting, level, nodes, parentNode) {
  3370. if (_createNodes) {
  3371. _createNodes.apply(view, arguments);
  3372. }
  3373. if (!nodes) return;
  3374. if (view.repairParentChkClassWithSelf) {
  3375. view.repairParentChkClassWithSelf(setting, parentNode);
  3376. }
  3377. }
  3378. var _makeNodeUrl = view.makeNodeUrl;
  3379. view.makeNodeUrl = function(setting, node) {
  3380. return setting.edit.enable ? null : (_makeNodeUrl.apply(view, arguments));
  3381. }
  3382. var _removeNode = view.removeNode;
  3383. view.removeNode = function(setting, node) {
  3384. var root = data.getRoot(setting);
  3385. if (root.curEditNode === node) root.curEditNode = null;
  3386. if (_removeNode) {
  3387. _removeNode.apply(view, arguments);
  3388. }
  3389. }
  3390. var _selectNode = view.selectNode;
  3391. view.selectNode = function(setting, node, addFlag) {
  3392. var root = data.getRoot(setting);
  3393. if (data.isSelectedNode(setting, node) && root.curEditNode == node && node.editNameFlag) {
  3394. return false;
  3395. }
  3396. if (_selectNode) _selectNode.apply(view, arguments);
  3397. view.addHoverDom(setting, node);
  3398. return true;
  3399. }
  3400. var _uCanDo = tools.uCanDo;
  3401. tools.uCanDo = function(setting, e) {
  3402. var root = data.getRoot(setting);
  3403. if (e && (tools.eqs(e.type, "mouseover") || tools.eqs(e.type, "mouseout") || tools.eqs(e.type, "mousedown") || tools.eqs(e.type, "mouseup"))) {
  3404. return true;
  3405. }
  3406. if (root.curEditNode) {
  3407. view.editNodeBlur = false;
  3408. root.curEditInput.focus();
  3409. }
  3410. return (!root.curEditNode) && (_uCanDo ? _uCanDo.apply(view, arguments) : true);
  3411. }
  3412. })(jQuery);