No Description

jquery.ztree.core-3.5.js 55KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715
  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);