jquery.ztree.core.js 86 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913
  1. /*
  2. * JQuery zTree core v3.5.28
  3. * http://treejs.cn/
  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: 2017-01-20
  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. NAME: 'node_name'
  23. },
  24. event: {
  25. NODECREATED: "ztree_nodeCreated",
  26. CLICK: "ztree_click",
  27. EXPAND: "ztree_expand",
  28. COLLAPSE: "ztree_collapse",
  29. ASYNC_SUCCESS: "ztree_async_success",
  30. ASYNC_ERROR: "ztree_async_error",
  31. REMOVE: "ztree_remove",
  32. SELECTED: "ztree_selected",
  33. UNSELECTED: "ztree_unselected"
  34. },
  35. id: {
  36. A: "_a",
  37. ICON: "_ico",
  38. SPAN: "_span",
  39. SWITCH: "_switch",
  40. UL: "_ul"
  41. },
  42. line: {
  43. ROOT: "root",
  44. ROOTS: "roots",
  45. CENTER: "center",
  46. BOTTOM: "bottom",
  47. NOLINE: "noline",
  48. LINE: "line"
  49. },
  50. folder: {
  51. OPEN: "open",
  52. CLOSE: "close",
  53. DOCU: "docu"
  54. },
  55. node: {
  56. CURSELECTED: "curSelectedNode"
  57. }
  58. },
  59. //default setting of core
  60. _setting = {
  61. treeId: "",
  62. treeObj: null,
  63. view: {
  64. addDiyDom: null,
  65. autoCancelSelected: true,
  66. dblClickExpand: true,
  67. expandSpeed: "fast",
  68. fontCss: {},
  69. nameIsHTML: false,
  70. selectedMulti: true,
  71. showIcon: true,
  72. showLine: true,
  73. showTitle: true,
  74. txtSelectedEnable: false
  75. },
  76. data: {
  77. key: {
  78. children: "children",
  79. name: "name",
  80. title: "",
  81. url: "url",
  82. icon: "icon"
  83. },
  84. simpleData: {
  85. enable: false,
  86. idKey: "id",
  87. pIdKey: "pId",
  88. rootPId: null
  89. },
  90. keep: {
  91. parent: false,
  92. leaf: false
  93. }
  94. },
  95. async: {
  96. enable: false,
  97. contentType: "application/x-www-form-urlencoded",
  98. type: "post",
  99. dataType: "text",
  100. url: "",
  101. autoParam: [],
  102. otherParam: [],
  103. dataFilter: null
  104. },
  105. callback: {
  106. beforeAsync: null,
  107. beforeClick: null,
  108. beforeDblClick: null,
  109. beforeRightClick: null,
  110. beforeMouseDown: null,
  111. beforeMouseUp: null,
  112. beforeExpand: null,
  113. beforeCollapse: null,
  114. beforeRemove: null,
  115. onAsyncError: null,
  116. onAsyncSuccess: null,
  117. onNodeCreated: null,
  118. onClick: null,
  119. onDblClick: null,
  120. onRightClick: null,
  121. onMouseDown: null,
  122. onMouseUp: null,
  123. onExpand: null,
  124. onCollapse: null,
  125. onRemove: null
  126. }
  127. },
  128. //default root of core
  129. //zTree use root to save full data
  130. _initRoot = function (setting) {
  131. var r = data.getRoot(setting);
  132. if (!r) {
  133. r = {};
  134. data.setRoot(setting, r);
  135. }
  136. r[setting.data.key.children] = [];
  137. r.expandTriggerFlag = false;
  138. r.curSelectedList = [];
  139. r.noSelection = true;
  140. r.createdNodes = [];
  141. r.zId = 0;
  142. r._ver = (new Date()).getTime();
  143. },
  144. //default cache of core
  145. _initCache = function (setting) {
  146. var c = data.getCache(setting);
  147. if (!c) {
  148. c = {};
  149. data.setCache(setting, c);
  150. }
  151. c.nodes = [];
  152. c.doms = [];
  153. },
  154. //default bindEvent of core
  155. _bindEvent = function (setting) {
  156. var o = setting.treeObj,
  157. c = consts.event;
  158. o.bind(c.NODECREATED, function (event, treeId, node) {
  159. tools.apply(setting.callback.onNodeCreated, [event, treeId, node]);
  160. });
  161. o.bind(c.CLICK, function (event, srcEvent, treeId, node, clickFlag) {
  162. tools.apply(setting.callback.onClick, [srcEvent, treeId, node, clickFlag]);
  163. });
  164. o.bind(c.EXPAND, function (event, treeId, node) {
  165. tools.apply(setting.callback.onExpand, [event, treeId, node]);
  166. });
  167. o.bind(c.COLLAPSE, function (event, treeId, node) {
  168. tools.apply(setting.callback.onCollapse, [event, treeId, node]);
  169. });
  170. o.bind(c.ASYNC_SUCCESS, function (event, treeId, node, msg) {
  171. tools.apply(setting.callback.onAsyncSuccess, [event, treeId, node, msg]);
  172. });
  173. o.bind(c.ASYNC_ERROR, function (event, treeId, node, XMLHttpRequest, textStatus, errorThrown) {
  174. tools.apply(setting.callback.onAsyncError, [event, treeId, node, XMLHttpRequest, textStatus, errorThrown]);
  175. });
  176. o.bind(c.REMOVE, function (event, treeId, treeNode) {
  177. tools.apply(setting.callback.onRemove, [event, treeId, treeNode]);
  178. });
  179. o.bind(c.SELECTED, function (event, treeId, node) {
  180. tools.apply(setting.callback.onSelected, [treeId, node]);
  181. });
  182. o.bind(c.UNSELECTED, function (event, treeId, node) {
  183. tools.apply(setting.callback.onUnSelected, [treeId, node]);
  184. });
  185. },
  186. _unbindEvent = function (setting) {
  187. var o = setting.treeObj,
  188. c = consts.event;
  189. o.unbind(c.NODECREATED)
  190. .unbind(c.CLICK)
  191. .unbind(c.EXPAND)
  192. .unbind(c.COLLAPSE)
  193. .unbind(c.ASYNC_SUCCESS)
  194. .unbind(c.ASYNC_ERROR)
  195. .unbind(c.REMOVE)
  196. .unbind(c.SELECTED)
  197. .unbind(c.UNSELECTED);
  198. },
  199. //default event proxy of core
  200. _eventProxy = function (event) {
  201. var target = event.target,
  202. setting = data.getSetting(event.data.treeId),
  203. tId = "", node = null,
  204. nodeEventType = "", treeEventType = "",
  205. nodeEventCallback = null, treeEventCallback = null,
  206. tmp = null;
  207. if (tools.eqs(event.type, "mousedown")) {
  208. treeEventType = "mousedown";
  209. } else if (tools.eqs(event.type, "mouseup")) {
  210. treeEventType = "mouseup";
  211. } else if (tools.eqs(event.type, "contextmenu")) {
  212. treeEventType = "contextmenu";
  213. } else if (tools.eqs(event.type, "click")) {
  214. if (tools.eqs(target.tagName, "span") && target.getAttribute("treeNode" + consts.id.SWITCH) !== null) {
  215. tId = tools.getNodeMainDom(target).id;
  216. nodeEventType = "switchNode";
  217. } else {
  218. tmp = tools.getMDom(setting, target, [{tagName: "a", attrName: "treeNode" + consts.id.A}]);
  219. if (tmp) {
  220. tId = tools.getNodeMainDom(tmp).id;
  221. nodeEventType = "clickNode";
  222. }
  223. }
  224. } else if (tools.eqs(event.type, "dblclick")) {
  225. treeEventType = "dblclick";
  226. tmp = tools.getMDom(setting, target, [{tagName: "a", attrName: "treeNode" + consts.id.A}]);
  227. if (tmp) {
  228. tId = tools.getNodeMainDom(tmp).id;
  229. nodeEventType = "switchNode";
  230. }
  231. }
  232. if (treeEventType.length > 0 && tId.length == 0) {
  233. tmp = tools.getMDom(setting, target, [{tagName: "a", attrName: "treeNode" + consts.id.A}]);
  234. if (tmp) {
  235. tId = tools.getNodeMainDom(tmp).id;
  236. }
  237. }
  238. // event to node
  239. if (tId.length > 0) {
  240. node = data.getNodeCache(setting, tId);
  241. switch (nodeEventType) {
  242. case "switchNode" :
  243. if (!node.isParent) {
  244. nodeEventType = "";
  245. } else if (tools.eqs(event.type, "click")
  246. || (tools.eqs(event.type, "dblclick") && tools.apply(setting.view.dblClickExpand, [setting.treeId, node], setting.view.dblClickExpand))) {
  247. nodeEventCallback = handler.onSwitchNode;
  248. } else {
  249. nodeEventType = "";
  250. }
  251. break;
  252. case "clickNode" :
  253. nodeEventCallback = handler.onClickNode;
  254. break;
  255. }
  256. }
  257. // event to zTree
  258. switch (treeEventType) {
  259. case "mousedown" :
  260. treeEventCallback = handler.onZTreeMousedown;
  261. break;
  262. case "mouseup" :
  263. treeEventCallback = handler.onZTreeMouseup;
  264. break;
  265. case "dblclick" :
  266. treeEventCallback = handler.onZTreeDblclick;
  267. break;
  268. case "contextmenu" :
  269. treeEventCallback = handler.onZTreeContextmenu;
  270. break;
  271. }
  272. var proxyResult = {
  273. stop: false,
  274. node: node,
  275. nodeEventType: nodeEventType,
  276. nodeEventCallback: nodeEventCallback,
  277. treeEventType: treeEventType,
  278. treeEventCallback: treeEventCallback
  279. };
  280. return proxyResult
  281. },
  282. //default init node of core
  283. _initNode = function (setting, level, n, parentNode, isFirstNode, isLastNode, openFlag) {
  284. if (!n) return;
  285. var r = data.getRoot(setting),
  286. childKey = setting.data.key.children;
  287. n.level = level;
  288. n.tId = setting.treeId + "_" + (++r.zId);
  289. n.parentTId = parentNode ? parentNode.tId : null;
  290. n.open = (typeof n.open == "string") ? tools.eqs(n.open, "true") : !!n.open;
  291. if (n[childKey] && n[childKey].length > 0) {
  292. n.isParent = true;
  293. n.zAsync = true;
  294. } else {
  295. n.isParent = (typeof n.isParent == "string") ? tools.eqs(n.isParent, "true") : !!n.isParent;
  296. n.open = (n.isParent && !setting.async.enable) ? n.open : false;
  297. n.zAsync = !n.isParent;
  298. }
  299. n.isFirstNode = isFirstNode;
  300. n.isLastNode = isLastNode;
  301. n.getParentNode = function () {
  302. return data.getNodeCache(setting, n.parentTId);
  303. };
  304. n.getPreNode = function () {
  305. return data.getPreNode(setting, n);
  306. };
  307. n.getNextNode = function () {
  308. return data.getNextNode(setting, n);
  309. };
  310. n.getIndex = function () {
  311. return data.getNodeIndex(setting, n);
  312. };
  313. n.getPath = function () {
  314. return data.getNodePath(setting, n);
  315. };
  316. n.isAjaxing = false;
  317. data.fixPIdKeyValue(setting, n);
  318. },
  319. _init = {
  320. bind: [_bindEvent],
  321. unbind: [_unbindEvent],
  322. caches: [_initCache],
  323. nodes: [_initNode],
  324. proxys: [_eventProxy],
  325. roots: [_initRoot],
  326. beforeA: [],
  327. afterA: [],
  328. innerBeforeA: [],
  329. innerAfterA: [],
  330. zTreeTools: []
  331. },
  332. //method of operate data
  333. data = {
  334. addNodeCache: function (setting, node) {
  335. data.getCache(setting).nodes[data.getNodeCacheId(node.tId)] = node;
  336. },
  337. getNodeCacheId: function (tId) {
  338. return tId.substring(tId.lastIndexOf("_") + 1);
  339. },
  340. addAfterA: function (afterA) {
  341. _init.afterA.push(afterA);
  342. },
  343. addBeforeA: function (beforeA) {
  344. _init.beforeA.push(beforeA);
  345. },
  346. addInnerAfterA: function (innerAfterA) {
  347. _init.innerAfterA.push(innerAfterA);
  348. },
  349. addInnerBeforeA: function (innerBeforeA) {
  350. _init.innerBeforeA.push(innerBeforeA);
  351. },
  352. addInitBind: function (bindEvent) {
  353. _init.bind.push(bindEvent);
  354. },
  355. addInitUnBind: function (unbindEvent) {
  356. _init.unbind.push(unbindEvent);
  357. },
  358. addInitCache: function (initCache) {
  359. _init.caches.push(initCache);
  360. },
  361. addInitNode: function (initNode) {
  362. _init.nodes.push(initNode);
  363. },
  364. addInitProxy: function (initProxy, isFirst) {
  365. if (!!isFirst) {
  366. _init.proxys.splice(0, 0, initProxy);
  367. } else {
  368. _init.proxys.push(initProxy);
  369. }
  370. },
  371. addInitRoot: function (initRoot) {
  372. _init.roots.push(initRoot);
  373. },
  374. addNodesData: function (setting, parentNode, index, nodes) {
  375. var childKey = setting.data.key.children, params;
  376. if (!parentNode[childKey]) {
  377. parentNode[childKey] = [];
  378. index = -1;
  379. } else if (index >= parentNode[childKey].length) {
  380. index = -1;
  381. }
  382. if (parentNode[childKey].length > 0 && index === 0) {
  383. parentNode[childKey][0].isFirstNode = false;
  384. view.setNodeLineIcos(setting, parentNode[childKey][0]);
  385. } else if (parentNode[childKey].length > 0 && index < 0) {
  386. parentNode[childKey][parentNode[childKey].length - 1].isLastNode = false;
  387. view.setNodeLineIcos(setting, parentNode[childKey][parentNode[childKey].length - 1]);
  388. }
  389. parentNode.isParent = true;
  390. if (index < 0) {
  391. parentNode[childKey] = parentNode[childKey].concat(nodes);
  392. } else {
  393. params = [index, 0].concat(nodes);
  394. parentNode[childKey].splice.apply(parentNode[childKey], params);
  395. }
  396. },
  397. addSelectedNode: function (setting, node) {
  398. var root = data.getRoot(setting);
  399. if (!data.isSelectedNode(setting, node)) {
  400. root.curSelectedList.push(node);
  401. }
  402. },
  403. addCreatedNode: function (setting, node) {
  404. if (!!setting.callback.onNodeCreated || !!setting.view.addDiyDom) {
  405. var root = data.getRoot(setting);
  406. root.createdNodes.push(node);
  407. }
  408. },
  409. addZTreeTools: function (zTreeTools) {
  410. _init.zTreeTools.push(zTreeTools);
  411. },
  412. exSetting: function (s) {
  413. $.extend(true, _setting, s);
  414. },
  415. fixPIdKeyValue: function (setting, node) {
  416. if (setting.data.simpleData.enable) {
  417. node[setting.data.simpleData.pIdKey] = node.parentTId ? node.getParentNode()[setting.data.simpleData.idKey] : setting.data.simpleData.rootPId;
  418. }
  419. },
  420. getAfterA: function (setting, node, array) {
  421. for (var i = 0, j = _init.afterA.length; i < j; i++) {
  422. _init.afterA[i].apply(this, arguments);
  423. }
  424. },
  425. getBeforeA: function (setting, node, array) {
  426. for (var i = 0, j = _init.beforeA.length; i < j; i++) {
  427. _init.beforeA[i].apply(this, arguments);
  428. }
  429. },
  430. getInnerAfterA: function (setting, node, array) {
  431. for (var i = 0, j = _init.innerAfterA.length; i < j; i++) {
  432. _init.innerAfterA[i].apply(this, arguments);
  433. }
  434. },
  435. getInnerBeforeA: function (setting, node, array) {
  436. for (var i = 0, j = _init.innerBeforeA.length; i < j; i++) {
  437. _init.innerBeforeA[i].apply(this, arguments);
  438. }
  439. },
  440. getCache: function (setting) {
  441. return caches[setting.treeId];
  442. },
  443. getNodeIndex: function (setting, node) {
  444. if (!node) return null;
  445. var childKey = setting.data.key.children,
  446. p = node.parentTId ? node.getParentNode() : data.getRoot(setting);
  447. for (var i = 0, l = p[childKey].length - 1; i <= l; i++) {
  448. if (p[childKey][i] === node) {
  449. return i;
  450. }
  451. }
  452. return -1;
  453. },
  454. getNextNode: function (setting, node) {
  455. if (!node) return null;
  456. var childKey = setting.data.key.children,
  457. p = node.parentTId ? node.getParentNode() : data.getRoot(setting);
  458. for (var i = 0, l = p[childKey].length - 1; i <= l; i++) {
  459. if (p[childKey][i] === node) {
  460. return (i == l ? null : p[childKey][i + 1]);
  461. }
  462. }
  463. return null;
  464. },
  465. getNodeByParam: function (setting, nodes, key, value) {
  466. if (!nodes || !key) return null;
  467. var childKey = setting.data.key.children;
  468. for (var i = 0, l = nodes.length; i < l; i++) {
  469. if (nodes[i][key] == value) {
  470. return nodes[i];
  471. }
  472. var tmp = data.getNodeByParam(setting, nodes[i][childKey], key, value);
  473. if (tmp) return tmp;
  474. }
  475. return null;
  476. },
  477. getNodeCache: function (setting, tId) {
  478. if (!tId) return null;
  479. var n = caches[setting.treeId].nodes[data.getNodeCacheId(tId)];
  480. return n ? n : null;
  481. },
  482. getNodeName: function (setting, node) {
  483. var nameKey = setting.data.key.name;
  484. return "" + node[nameKey];
  485. },
  486. getNodePath: function (setting, node) {
  487. if (!node) return null;
  488. var path;
  489. if (node.parentTId) {
  490. path = node.getParentNode().getPath();
  491. } else {
  492. path = [];
  493. }
  494. if (path) {
  495. path.push(node);
  496. }
  497. return path;
  498. },
  499. getNodeTitle: function (setting, node) {
  500. var t = setting.data.key.title === "" ? setting.data.key.name : setting.data.key.title;
  501. return "" + node[t];
  502. },
  503. getNodes: function (setting) {
  504. return data.getRoot(setting)[setting.data.key.children];
  505. },
  506. getNodesByParam: function (setting, nodes, key, value) {
  507. if (!nodes || !key) return [];
  508. var childKey = setting.data.key.children,
  509. result = [];
  510. for (var i = 0, l = nodes.length; i < l; i++) {
  511. if (nodes[i][key] == value) {
  512. result.push(nodes[i]);
  513. }
  514. result = result.concat(data.getNodesByParam(setting, nodes[i][childKey], key, value));
  515. }
  516. return result;
  517. },
  518. getNodesByParamFuzzy: function (setting, nodes, key, value) {
  519. if (!nodes || !key) return [];
  520. var childKey = setting.data.key.children,
  521. result = [];
  522. value = value.toLowerCase();
  523. for (var i = 0, l = nodes.length; i < l; i++) {
  524. if (typeof nodes[i][key] == "string" && nodes[i][key].toLowerCase().indexOf(value) > -1) {
  525. result.push(nodes[i]);
  526. }
  527. result = result.concat(data.getNodesByParamFuzzy(setting, nodes[i][childKey], key, value));
  528. }
  529. return result;
  530. },
  531. getNodesByFilter: function (setting, nodes, filter, isSingle, invokeParam) {
  532. if (!nodes) return (isSingle ? null : []);
  533. var childKey = setting.data.key.children,
  534. result = isSingle ? null : [];
  535. for (var i = 0, l = nodes.length; i < l; i++) {
  536. if (tools.apply(filter, [nodes[i], invokeParam], false)) {
  537. if (isSingle) {
  538. return nodes[i];
  539. }
  540. result.push(nodes[i]);
  541. }
  542. var tmpResult = data.getNodesByFilter(setting, nodes[i][childKey], filter, isSingle, invokeParam);
  543. if (isSingle && !!tmpResult) {
  544. return tmpResult;
  545. }
  546. result = isSingle ? tmpResult : result.concat(tmpResult);
  547. }
  548. return result;
  549. },
  550. getPreNode: function (setting, node) {
  551. if (!node) return null;
  552. var childKey = setting.data.key.children,
  553. p = node.parentTId ? node.getParentNode() : data.getRoot(setting);
  554. for (var i = 0, l = p[childKey].length; i < l; i++) {
  555. if (p[childKey][i] === node) {
  556. return (i == 0 ? null : p[childKey][i - 1]);
  557. }
  558. }
  559. return null;
  560. },
  561. getRoot: function (setting) {
  562. return setting ? roots[setting.treeId] : null;
  563. },
  564. getRoots: function () {
  565. return roots;
  566. },
  567. getSetting: function (treeId) {
  568. return settings[treeId];
  569. },
  570. getSettings: function () {
  571. return settings;
  572. },
  573. getZTreeTools: function (treeId) {
  574. var r = this.getRoot(this.getSetting(treeId));
  575. return r ? r.treeTools : null;
  576. },
  577. initCache: function (setting) {
  578. for (var i = 0, j = _init.caches.length; i < j; i++) {
  579. _init.caches[i].apply(this, arguments);
  580. }
  581. },
  582. initNode: function (setting, level, node, parentNode, preNode, nextNode) {
  583. for (var i = 0, j = _init.nodes.length; i < j; i++) {
  584. _init.nodes[i].apply(this, arguments);
  585. }
  586. },
  587. initRoot: function (setting) {
  588. for (var i = 0, j = _init.roots.length; i < j; i++) {
  589. _init.roots[i].apply(this, arguments);
  590. }
  591. },
  592. isSelectedNode: function (setting, node) {
  593. var root = data.getRoot(setting);
  594. for (var i = 0, j = root.curSelectedList.length; i < j; i++) {
  595. if (node === root.curSelectedList[i]) return true;
  596. }
  597. return false;
  598. },
  599. removeNodeCache: function (setting, node) {
  600. var childKey = setting.data.key.children;
  601. if (node[childKey]) {
  602. for (var i = 0, l = node[childKey].length; i < l; i++) {
  603. data.removeNodeCache(setting, node[childKey][i]);
  604. }
  605. }
  606. data.getCache(setting).nodes[data.getNodeCacheId(node.tId)] = null;
  607. },
  608. removeSelectedNode: function (setting, node) {
  609. var root = data.getRoot(setting);
  610. for (var i = 0, j = root.curSelectedList.length; i < j; i++) {
  611. if (node === root.curSelectedList[i] || !data.getNodeCache(setting, root.curSelectedList[i].tId)) {
  612. root.curSelectedList.splice(i, 1);
  613. setting.treeObj.trigger(consts.event.UNSELECTED, [setting.treeId, node]);
  614. i--;
  615. j--;
  616. }
  617. }
  618. },
  619. setCache: function (setting, cache) {
  620. caches[setting.treeId] = cache;
  621. },
  622. setRoot: function (setting, root) {
  623. roots[setting.treeId] = root;
  624. },
  625. setZTreeTools: function (setting, zTreeTools) {
  626. for (var i = 0, j = _init.zTreeTools.length; i < j; i++) {
  627. _init.zTreeTools[i].apply(this, arguments);
  628. }
  629. },
  630. transformToArrayFormat: function (setting, nodes) {
  631. if (!nodes) return [];
  632. var childKey = setting.data.key.children,
  633. r = [];
  634. if (tools.isArray(nodes)) {
  635. for (var i = 0, l = nodes.length; i < l; i++) {
  636. r.push(nodes[i]);
  637. if (nodes[i][childKey])
  638. r = r.concat(data.transformToArrayFormat(setting, nodes[i][childKey]));
  639. }
  640. } else {
  641. r.push(nodes);
  642. if (nodes[childKey])
  643. r = r.concat(data.transformToArrayFormat(setting, nodes[childKey]));
  644. }
  645. return r;
  646. },
  647. transformTozTreeFormat: function (setting, sNodes) {
  648. var i, l,
  649. key = setting.data.simpleData.idKey,
  650. parentKey = setting.data.simpleData.pIdKey,
  651. childKey = setting.data.key.children;
  652. if (!key || key == "" || !sNodes) return [];
  653. if (tools.isArray(sNodes)) {
  654. var r = [];
  655. var tmpMap = {};
  656. for (i = 0, l = sNodes.length; i < l; i++) {
  657. tmpMap[sNodes[i][key]] = sNodes[i];
  658. }
  659. for (i = 0, l = sNodes.length; i < l; i++) {
  660. if (tmpMap[sNodes[i][parentKey]] && sNodes[i][key] != sNodes[i][parentKey]) {
  661. if (!tmpMap[sNodes[i][parentKey]][childKey])
  662. tmpMap[sNodes[i][parentKey]][childKey] = [];
  663. tmpMap[sNodes[i][parentKey]][childKey].push(sNodes[i]);
  664. } else {
  665. r.push(sNodes[i]);
  666. }
  667. }
  668. return r;
  669. } else {
  670. return [sNodes];
  671. }
  672. }
  673. },
  674. //method of event proxy
  675. event = {
  676. bindEvent: function (setting) {
  677. for (var i = 0, j = _init.bind.length; i < j; i++) {
  678. _init.bind[i].apply(this, arguments);
  679. }
  680. },
  681. unbindEvent: function (setting) {
  682. for (var i = 0, j = _init.unbind.length; i < j; i++) {
  683. _init.unbind[i].apply(this, arguments);
  684. }
  685. },
  686. bindTree: function (setting) {
  687. var eventParam = {
  688. treeId: setting.treeId
  689. },
  690. o = setting.treeObj;
  691. if (!setting.view.txtSelectedEnable) {
  692. // for can't select text
  693. o.bind('selectstart', handler.onSelectStart).css({
  694. "-moz-user-select": "-moz-none"
  695. });
  696. }
  697. o.bind('click', eventParam, event.proxy);
  698. o.bind('dblclick', eventParam, event.proxy);
  699. o.bind('mouseover', eventParam, event.proxy);
  700. o.bind('mouseout', eventParam, event.proxy);
  701. o.bind('mousedown', eventParam, event.proxy);
  702. o.bind('mouseup', eventParam, event.proxy);
  703. o.bind('contextmenu', eventParam, event.proxy);
  704. },
  705. unbindTree: function (setting) {
  706. var o = setting.treeObj;
  707. o.unbind('selectstart', handler.onSelectStart)
  708. .unbind('click', event.proxy)
  709. .unbind('dblclick', event.proxy)
  710. .unbind('mouseover', event.proxy)
  711. .unbind('mouseout', event.proxy)
  712. .unbind('mousedown', event.proxy)
  713. .unbind('mouseup', event.proxy)
  714. .unbind('contextmenu', event.proxy);
  715. },
  716. doProxy: function (e) {
  717. var results = [];
  718. for (var i = 0, j = _init.proxys.length; i < j; i++) {
  719. var proxyResult = _init.proxys[i].apply(this, arguments);
  720. results.push(proxyResult);
  721. if (proxyResult.stop) {
  722. break;
  723. }
  724. }
  725. return results;
  726. },
  727. proxy: function (e) {
  728. var setting = data.getSetting(e.data.treeId);
  729. if (!tools.uCanDo(setting, e)) return true;
  730. var results = event.doProxy(e),
  731. r = true, x = false;
  732. for (var i = 0, l = results.length; i < l; i++) {
  733. var proxyResult = results[i];
  734. if (proxyResult.nodeEventCallback) {
  735. x = true;
  736. r = proxyResult.nodeEventCallback.apply(proxyResult, [e, proxyResult.node]) && r;
  737. }
  738. if (proxyResult.treeEventCallback) {
  739. x = true;
  740. r = proxyResult.treeEventCallback.apply(proxyResult, [e, proxyResult.node]) && r;
  741. }
  742. }
  743. return r;
  744. }
  745. },
  746. //method of event handler
  747. handler = {
  748. onSwitchNode: function (event, node) {
  749. var setting = data.getSetting(event.data.treeId);
  750. if (node.open) {
  751. if (tools.apply(setting.callback.beforeCollapse, [setting.treeId, node], true) == false) return true;
  752. data.getRoot(setting).expandTriggerFlag = true;
  753. view.switchNode(setting, node);
  754. } else {
  755. if (tools.apply(setting.callback.beforeExpand, [setting.treeId, node], true) == false) return true;
  756. data.getRoot(setting).expandTriggerFlag = true;
  757. view.switchNode(setting, node);
  758. }
  759. return true;
  760. },
  761. onClickNode: function (event, node) {
  762. var setting = data.getSetting(event.data.treeId),
  763. 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;
  764. if (tools.apply(setting.callback.beforeClick, [setting.treeId, node, clickFlag], true) == false) return true;
  765. if (clickFlag === 0) {
  766. view.cancelPreSelectedNode(setting, node);
  767. } else {
  768. view.selectNode(setting, node, clickFlag === 2);
  769. }
  770. setting.treeObj.trigger(consts.event.CLICK, [event, setting.treeId, node, clickFlag]);
  771. return true;
  772. },
  773. onZTreeMousedown: function (event, node) {
  774. var setting = data.getSetting(event.data.treeId);
  775. if (tools.apply(setting.callback.beforeMouseDown, [setting.treeId, node], true)) {
  776. tools.apply(setting.callback.onMouseDown, [event, setting.treeId, node]);
  777. }
  778. return true;
  779. },
  780. onZTreeMouseup: function (event, node) {
  781. var setting = data.getSetting(event.data.treeId);
  782. if (tools.apply(setting.callback.beforeMouseUp, [setting.treeId, node], true)) {
  783. tools.apply(setting.callback.onMouseUp, [event, setting.treeId, node]);
  784. }
  785. return true;
  786. },
  787. onZTreeDblclick: function (event, node) {
  788. var setting = data.getSetting(event.data.treeId);
  789. if (tools.apply(setting.callback.beforeDblClick, [setting.treeId, node], true)) {
  790. tools.apply(setting.callback.onDblClick, [event, setting.treeId, node]);
  791. }
  792. return true;
  793. },
  794. onZTreeContextmenu: function (event, node) {
  795. var setting = data.getSetting(event.data.treeId);
  796. if (tools.apply(setting.callback.beforeRightClick, [setting.treeId, node], true)) {
  797. tools.apply(setting.callback.onRightClick, [event, setting.treeId, node]);
  798. }
  799. return (typeof setting.callback.onRightClick) != "function";
  800. },
  801. onSelectStart: function (e) {
  802. var n = e.originalEvent.srcElement.nodeName.toLowerCase();
  803. return (n === "input" || n === "textarea" );
  804. }
  805. },
  806. //method of tools for zTree
  807. tools = {
  808. apply: function (fun, param, defaultValue) {
  809. if ((typeof fun) == "function") {
  810. return fun.apply(zt, param ? param : []);
  811. }
  812. return defaultValue;
  813. },
  814. canAsync: function (setting, node) {
  815. var childKey = setting.data.key.children;
  816. return (setting.async.enable && node && node.isParent && !(node.zAsync || (node[childKey] && node[childKey].length > 0)));
  817. },
  818. clone: function (obj) {
  819. if (obj === null) return null;
  820. var o = tools.isArray(obj) ? [] : {};
  821. for (var i in obj) {
  822. o[i] = (obj[i] instanceof Date) ? new Date(obj[i].getTime()) : (typeof obj[i] === "object" ? tools.clone(obj[i]) : obj[i]);
  823. }
  824. return o;
  825. },
  826. eqs: function (str1, str2) {
  827. return str1.toLowerCase() === str2.toLowerCase();
  828. },
  829. isArray: function (arr) {
  830. return Object.prototype.toString.apply(arr) === "[object Array]";
  831. },
  832. isElement: function (o) {
  833. return (
  834. typeof HTMLElement === "object" ? o instanceof HTMLElement : //DOM2
  835. o && typeof o === "object" && o !== null && o.nodeType === 1 && typeof o.nodeName === "string"
  836. );
  837. },
  838. $: function (node, exp, setting) {
  839. if (!!exp && typeof exp != "string") {
  840. setting = exp;
  841. exp = "";
  842. }
  843. if (typeof node == "string") {
  844. return $(node, setting ? setting.treeObj.get(0).ownerDocument : null);
  845. } else {
  846. return $("#" + node.tId + exp, setting ? setting.treeObj : null);
  847. }
  848. },
  849. getMDom: function (setting, curDom, targetExpr) {
  850. if (!curDom) return null;
  851. while (curDom && curDom.id !== setting.treeId) {
  852. for (var i = 0, l = targetExpr.length; curDom.tagName && i < l; i++) {
  853. if (tools.eqs(curDom.tagName, targetExpr[i].tagName) && curDom.getAttribute(targetExpr[i].attrName) !== null) {
  854. return curDom;
  855. }
  856. }
  857. curDom = curDom.parentNode;
  858. }
  859. return null;
  860. },
  861. getNodeMainDom: function (target) {
  862. return ($(target).parent("li").get(0) || $(target).parentsUntil("li").parent().get(0));
  863. },
  864. isChildOrSelf: function (dom, parentId) {
  865. return ( $(dom).closest("#" + parentId).length > 0 );
  866. },
  867. uCanDo: function (setting, e) {
  868. return true;
  869. }
  870. },
  871. //method of operate ztree dom
  872. view = {
  873. addNodes: function (setting, parentNode, index, newNodes, isSilent) {
  874. if (setting.data.keep.leaf && parentNode && !parentNode.isParent) {
  875. return;
  876. }
  877. if (!tools.isArray(newNodes)) {
  878. newNodes = [newNodes];
  879. }
  880. if (setting.data.simpleData.enable) {
  881. newNodes = data.transformTozTreeFormat(setting, newNodes);
  882. }
  883. if (parentNode) {
  884. var target_switchObj = $$(parentNode, consts.id.SWITCH, setting),
  885. target_icoObj = $$(parentNode, consts.id.ICON, setting),
  886. target_ulObj = $$(parentNode, consts.id.UL, setting);
  887. if (!parentNode.open) {
  888. view.replaceSwitchClass(parentNode, target_switchObj, consts.folder.CLOSE);
  889. view.replaceIcoClass(parentNode, target_icoObj, consts.folder.CLOSE);
  890. parentNode.open = false;
  891. target_ulObj.css({
  892. "display": "none"
  893. });
  894. }
  895. data.addNodesData(setting, parentNode, index, newNodes);
  896. view.createNodes(setting, parentNode.level + 1, newNodes, parentNode, index);
  897. if (!isSilent) {
  898. view.expandCollapseParentNode(setting, parentNode, true);
  899. }
  900. } else {
  901. data.addNodesData(setting, data.getRoot(setting), index, newNodes);
  902. view.createNodes(setting, 0, newNodes, null, index);
  903. }
  904. },
  905. appendNodes: function (setting, level, nodes, parentNode, index, initFlag, openFlag) {
  906. if (!nodes) return [];
  907. var html = [],
  908. childKey = setting.data.key.children;
  909. var tmpPNode = (parentNode) ? parentNode : data.getRoot(setting),
  910. tmpPChild = tmpPNode[childKey],
  911. isFirstNode, isLastNode;
  912. if (!tmpPChild || index >= tmpPChild.length - nodes.length) {
  913. index = -1;
  914. }
  915. for (var i = 0, l = nodes.length; i < l; i++) {
  916. var node = nodes[i];
  917. if (initFlag) {
  918. isFirstNode = ((index === 0 || tmpPChild.length == nodes.length) && (i == 0));
  919. isLastNode = (index < 0 && i == (nodes.length - 1));
  920. data.initNode(setting, level, node, parentNode, isFirstNode, isLastNode, openFlag);
  921. data.addNodeCache(setting, node);
  922. }
  923. var childHtml = [];
  924. if (node[childKey] && node[childKey].length > 0) {
  925. //make child html first, because checkType
  926. childHtml = view.appendNodes(setting, level + 1, node[childKey], node, -1, initFlag, openFlag && node.open);
  927. }
  928. if (openFlag) {
  929. view.makeDOMNodeMainBefore(html, setting, node);
  930. view.makeDOMNodeLine(html, setting, node);
  931. data.getBeforeA(setting, node, html);
  932. view.makeDOMNodeNameBefore(html, setting, node);
  933. data.getInnerBeforeA(setting, node, html);
  934. view.makeDOMNodeIcon(html, setting, node);
  935. data.getInnerAfterA(setting, node, html);
  936. view.makeDOMNodeNameAfter(html, setting, node);
  937. data.getAfterA(setting, node, html);
  938. if (node.isParent && node.open) {
  939. view.makeUlHtml(setting, node, html, childHtml.join(''));
  940. }
  941. view.makeDOMNodeMainAfter(html, setting, node);
  942. data.addCreatedNode(setting, node);
  943. }
  944. }
  945. return html;
  946. },
  947. appendParentULDom: function (setting, node) {
  948. var html = [],
  949. nObj = $$(node, setting);
  950. if (!nObj.get(0) && !!node.parentTId) {
  951. view.appendParentULDom(setting, node.getParentNode());
  952. nObj = $$(node, setting);
  953. }
  954. var ulObj = $$(node, consts.id.UL, setting);
  955. if (ulObj.get(0)) {
  956. ulObj.remove();
  957. }
  958. var childKey = setting.data.key.children,
  959. childHtml = view.appendNodes(setting, node.level + 1, node[childKey], node, -1, false, true);
  960. view.makeUlHtml(setting, node, html, childHtml.join(''));
  961. nObj.append(html.join(''));
  962. },
  963. asyncNode: function (setting, node, isSilent, callback) {
  964. var i, l;
  965. if (node && !node.isParent) {
  966. tools.apply(callback);
  967. return false;
  968. } else if (node && node.isAjaxing) {
  969. return false;
  970. } else if (tools.apply(setting.callback.beforeAsync, [setting.treeId, node], true) == false) {
  971. tools.apply(callback);
  972. return false;
  973. }
  974. if (node) {
  975. node.isAjaxing = true;
  976. var icoObj = $$(node, consts.id.ICON, setting);
  977. icoObj.attr({"style": "", "class": consts.className.BUTTON + " " + consts.className.ICO_LOADING});
  978. }
  979. var tmpParam = {};
  980. for (i = 0, l = setting.async.autoParam.length; node && i < l; i++) {
  981. var pKey = setting.async.autoParam[i].split("="), spKey = pKey;
  982. if (pKey.length > 1) {
  983. spKey = pKey[1];
  984. pKey = pKey[0];
  985. }
  986. tmpParam[spKey] = node[pKey];
  987. }
  988. if (tools.isArray(setting.async.otherParam)) {
  989. for (i = 0, l = setting.async.otherParam.length; i < l; i += 2) {
  990. tmpParam[setting.async.otherParam[i]] = setting.async.otherParam[i + 1];
  991. }
  992. } else {
  993. for (var p in setting.async.otherParam) {
  994. tmpParam[p] = setting.async.otherParam[p];
  995. }
  996. }
  997. var _tmpV = data.getRoot(setting)._ver;
  998. $.ajax({
  999. contentType: setting.async.contentType,
  1000. cache: false,
  1001. type: setting.async.type,
  1002. url: tools.apply(setting.async.url, [setting.treeId, node], setting.async.url),
  1003. data: setting.async.contentType.indexOf('application/json') > -1 ? JSON.stringify(tmpParam) : tmpParam,
  1004. dataType: setting.async.dataType,
  1005. success: function (msg) {
  1006. if (_tmpV != data.getRoot(setting)._ver) {
  1007. return;
  1008. }
  1009. var newNodes = [];
  1010. try {
  1011. if (!msg || msg.length == 0) {
  1012. newNodes = [];
  1013. } else if (typeof msg == "string") {
  1014. newNodes = eval("(" + msg + ")");
  1015. } else {
  1016. newNodes = msg;
  1017. }
  1018. } catch (err) {
  1019. newNodes = msg;
  1020. }
  1021. if (node) {
  1022. node.isAjaxing = null;
  1023. node.zAsync = true;
  1024. }
  1025. view.setNodeLineIcos(setting, node);
  1026. if (newNodes && newNodes !== "") {
  1027. newNodes = tools.apply(setting.async.dataFilter, [setting.treeId, node, newNodes], newNodes);
  1028. view.addNodes(setting, node, -1, !!newNodes ? tools.clone(newNodes) : [], !!isSilent);
  1029. } else {
  1030. view.addNodes(setting, node, -1, [], !!isSilent);
  1031. }
  1032. setting.treeObj.trigger(consts.event.ASYNC_SUCCESS, [setting.treeId, node, msg]);
  1033. tools.apply(callback);
  1034. },
  1035. error: function (XMLHttpRequest, textStatus, errorThrown) {
  1036. if (_tmpV != data.getRoot(setting)._ver) {
  1037. return;
  1038. }
  1039. if (node) node.isAjaxing = null;
  1040. view.setNodeLineIcos(setting, node);
  1041. setting.treeObj.trigger(consts.event.ASYNC_ERROR, [setting.treeId, node, XMLHttpRequest, textStatus, errorThrown]);
  1042. }
  1043. });
  1044. return true;
  1045. },
  1046. cancelPreSelectedNode: function (setting, node, excludeNode) {
  1047. var list = data.getRoot(setting).curSelectedList,
  1048. i, n;
  1049. for (i = list.length - 1; i >= 0; i--) {
  1050. n = list[i];
  1051. if (node === n || (!node && (!excludeNode || excludeNode !== n))) {
  1052. $$(n, consts.id.A, setting).removeClass(consts.node.CURSELECTED);
  1053. if (node) {
  1054. data.removeSelectedNode(setting, node);
  1055. break;
  1056. } else {
  1057. list.splice(i, 1);
  1058. setting.treeObj.trigger(consts.event.UNSELECTED, [setting.treeId, n]);
  1059. }
  1060. }
  1061. }
  1062. },
  1063. createNodeCallback: function (setting) {
  1064. if (!!setting.callback.onNodeCreated || !!setting.view.addDiyDom) {
  1065. var root = data.getRoot(setting);
  1066. while (root.createdNodes.length > 0) {
  1067. var node = root.createdNodes.shift();
  1068. tools.apply(setting.view.addDiyDom, [setting.treeId, node]);
  1069. if (!!setting.callback.onNodeCreated) {
  1070. setting.treeObj.trigger(consts.event.NODECREATED, [setting.treeId, node]);
  1071. }
  1072. }
  1073. }
  1074. },
  1075. createNodes: function (setting, level, nodes, parentNode, index) {
  1076. if (!nodes || nodes.length == 0) return;
  1077. var root = data.getRoot(setting),
  1078. childKey = setting.data.key.children,
  1079. openFlag = !parentNode || parentNode.open || !!$$(parentNode[childKey][0], setting).get(0);
  1080. root.createdNodes = [];
  1081. var zTreeHtml = view.appendNodes(setting, level, nodes, parentNode, index, true, openFlag),
  1082. parentObj, nextObj;
  1083. if (!parentNode) {
  1084. parentObj = setting.treeObj;
  1085. //setting.treeObj.append(zTreeHtml.join(''));
  1086. } else {
  1087. var ulObj = $$(parentNode, consts.id.UL, setting);
  1088. if (ulObj.get(0)) {
  1089. parentObj = ulObj;
  1090. //ulObj.append(zTreeHtml.join(''));
  1091. }
  1092. }
  1093. if (parentObj) {
  1094. if (index >= 0) {
  1095. nextObj = parentObj.children()[index];
  1096. }
  1097. if (index >= 0 && nextObj) {
  1098. $(nextObj).before(zTreeHtml.join(''));
  1099. } else {
  1100. parentObj.append(zTreeHtml.join(''));
  1101. }
  1102. }
  1103. view.createNodeCallback(setting);
  1104. },
  1105. destroy: function (setting) {
  1106. if (!setting) return;
  1107. data.initCache(setting);
  1108. data.initRoot(setting);
  1109. event.unbindTree(setting);
  1110. event.unbindEvent(setting);
  1111. setting.treeObj.empty();
  1112. delete settings[setting.treeId];
  1113. },
  1114. expandCollapseNode: function (setting, node, expandFlag, animateFlag, callback) {
  1115. var root = data.getRoot(setting),
  1116. childKey = setting.data.key.children;
  1117. var tmpCb, _callback;
  1118. if (!node) {
  1119. tools.apply(callback, []);
  1120. return;
  1121. }
  1122. if (root.expandTriggerFlag) {
  1123. _callback = callback;
  1124. tmpCb = function () {
  1125. if (_callback) _callback();
  1126. if (node.open) {
  1127. setting.treeObj.trigger(consts.event.EXPAND, [setting.treeId, node]);
  1128. } else {
  1129. setting.treeObj.trigger(consts.event.COLLAPSE, [setting.treeId, node]);
  1130. }
  1131. };
  1132. callback = tmpCb;
  1133. root.expandTriggerFlag = false;
  1134. }
  1135. if (!node.open && node.isParent && ((!$$(node, consts.id.UL, setting).get(0)) || (node[childKey] && node[childKey].length > 0 && !$$(node[childKey][0], setting).get(0)))) {
  1136. view.appendParentULDom(setting, node);
  1137. view.createNodeCallback(setting);
  1138. }
  1139. if (node.open == expandFlag) {
  1140. tools.apply(callback, []);
  1141. return;
  1142. }
  1143. var ulObj = $$(node, consts.id.UL, setting),
  1144. switchObj = $$(node, consts.id.SWITCH, setting),
  1145. icoObj = $$(node, consts.id.ICON, setting);
  1146. if (node.isParent) {
  1147. node.open = !node.open;
  1148. if (node.iconOpen && node.iconClose) {
  1149. icoObj.attr("style", view.makeNodeIcoStyle(setting, node));
  1150. }
  1151. if (node.open) {
  1152. view.replaceSwitchClass(node, switchObj, consts.folder.OPEN);
  1153. view.replaceIcoClass(node, icoObj, consts.folder.OPEN);
  1154. if (animateFlag == false || setting.view.expandSpeed == "") {
  1155. ulObj.show();
  1156. tools.apply(callback, []);
  1157. } else {
  1158. if (node[childKey] && node[childKey].length > 0) {
  1159. ulObj.slideDown(setting.view.expandSpeed, callback);
  1160. } else {
  1161. ulObj.show();
  1162. tools.apply(callback, []);
  1163. }
  1164. }
  1165. } else {
  1166. view.replaceSwitchClass(node, switchObj, consts.folder.CLOSE);
  1167. view.replaceIcoClass(node, icoObj, consts.folder.CLOSE);
  1168. if (animateFlag == false || setting.view.expandSpeed == "" || !(node[childKey] && node[childKey].length > 0)) {
  1169. ulObj.hide();
  1170. tools.apply(callback, []);
  1171. } else {
  1172. ulObj.slideUp(setting.view.expandSpeed, callback);
  1173. }
  1174. }
  1175. } else {
  1176. tools.apply(callback, []);
  1177. }
  1178. },
  1179. expandCollapseParentNode: function (setting, node, expandFlag, animateFlag, callback) {
  1180. if (!node) return;
  1181. if (!node.parentTId) {
  1182. view.expandCollapseNode(setting, node, expandFlag, animateFlag, callback);
  1183. return;
  1184. } else {
  1185. view.expandCollapseNode(setting, node, expandFlag, animateFlag);
  1186. }
  1187. if (node.parentTId) {
  1188. view.expandCollapseParentNode(setting, node.getParentNode(), expandFlag, animateFlag, callback);
  1189. }
  1190. },
  1191. expandCollapseSonNode: function (setting, node, expandFlag, animateFlag, callback) {
  1192. var root = data.getRoot(setting),
  1193. childKey = setting.data.key.children,
  1194. treeNodes = (node) ? node[childKey] : root[childKey],
  1195. selfAnimateSign = (node) ? false : animateFlag,
  1196. expandTriggerFlag = data.getRoot(setting).expandTriggerFlag;
  1197. data.getRoot(setting).expandTriggerFlag = false;
  1198. if (treeNodes) {
  1199. for (var i = 0, l = treeNodes.length; i < l; i++) {
  1200. if (treeNodes[i]) view.expandCollapseSonNode(setting, treeNodes[i], expandFlag, selfAnimateSign);
  1201. }
  1202. }
  1203. data.getRoot(setting).expandTriggerFlag = expandTriggerFlag;
  1204. view.expandCollapseNode(setting, node, expandFlag, animateFlag, callback);
  1205. },
  1206. isSelectedNode: function (setting, node) {
  1207. if (!node) {
  1208. return false;
  1209. }
  1210. var list = data.getRoot(setting).curSelectedList,
  1211. i;
  1212. for (i = list.length - 1; i >= 0; i--) {
  1213. if (node === list[i]) {
  1214. return true;
  1215. }
  1216. }
  1217. return false;
  1218. },
  1219. makeDOMNodeIcon: function (html, setting, node) {
  1220. var nameStr = data.getNodeName(setting, node),
  1221. name = setting.view.nameIsHTML ? nameStr : nameStr.replace(/&/g, '&amp;').replace(/</g, '&lt;').replace(/>/g, '&gt;');
  1222. html.push("<span id='", node.tId, consts.id.ICON,
  1223. "' title='' treeNode", consts.id.ICON, " class='", view.makeNodeIcoClass(setting, node),
  1224. "' style='", view.makeNodeIcoStyle(setting, node), "'></span><span id='", node.tId, consts.id.SPAN,
  1225. "' class='", consts.className.NAME,
  1226. "'>", name, "</span>");
  1227. },
  1228. makeDOMNodeLine: function (html, setting, node) {
  1229. html.push("<span id='", node.tId, consts.id.SWITCH, "' title='' class='", view.makeNodeLineClass(setting, node), "' treeNode", consts.id.SWITCH, "></span>");
  1230. },
  1231. makeDOMNodeMainAfter: function (html, setting, node) {
  1232. html.push("</li>");
  1233. },
  1234. makeDOMNodeMainBefore: function (html, setting, node) {
  1235. html.push("<li id='", node.tId, "' class='", consts.className.LEVEL, node.level, "' tabindex='0' hidefocus='true' treenode>");
  1236. },
  1237. makeDOMNodeNameAfter: function (html, setting, node) {
  1238. html.push("</a>");
  1239. },
  1240. makeDOMNodeNameBefore: function (html, setting, node) {
  1241. var title = data.getNodeTitle(setting, node),
  1242. url = view.makeNodeUrl(setting, node),
  1243. fontcss = view.makeNodeFontCss(setting, node),
  1244. fontStyle = [];
  1245. for (var f in fontcss) {
  1246. fontStyle.push(f, ":", fontcss[f], ";");
  1247. }
  1248. html.push("<a id='", node.tId, consts.id.A, "' class='", consts.className.LEVEL, node.level, "' treeNode", consts.id.A, " onclick=\"", (node.click || ''),
  1249. "\" ", ((url != null && url.length > 0) ? "href='" + url + "'" : ""), " target='", view.makeNodeTarget(node), "' style='", fontStyle.join(''),
  1250. "'");
  1251. if (tools.apply(setting.view.showTitle, [setting.treeId, node], setting.view.showTitle) && title) {
  1252. html.push("title='", title.replace(/'/g, "&#39;").replace(/</g, '&lt;').replace(/>/g, '&gt;'), "'");
  1253. }
  1254. html.push(">");
  1255. },
  1256. makeNodeFontCss: function (setting, node) {
  1257. var fontCss = tools.apply(setting.view.fontCss, [setting.treeId, node], setting.view.fontCss);
  1258. return (fontCss && ((typeof fontCss) != "function")) ? fontCss : {};
  1259. },
  1260. makeNodeIcoClass: function (setting, node) {
  1261. var icoCss = ["ico"];
  1262. if (!node.isAjaxing) {
  1263. icoCss[0] = (node.iconSkin ? node.iconSkin + "_" : "") + icoCss[0];
  1264. if (node.isParent) {
  1265. icoCss.push(node.open ? consts.folder.OPEN : consts.folder.CLOSE);
  1266. } else {
  1267. icoCss.push(consts.folder.DOCU);
  1268. }
  1269. }
  1270. return consts.className.BUTTON + " " + icoCss.join('_');
  1271. },
  1272. makeNodeIcoStyle: function (setting, node) {
  1273. var icoStyle = [];
  1274. if (!node.isAjaxing) {
  1275. var icon = (node.isParent && node.iconOpen && node.iconClose) ? (node.open ? node.iconOpen : node.iconClose) : node[setting.data.key.icon];
  1276. if (icon) icoStyle.push("background:url(", icon, ") 0 0 no-repeat;");
  1277. if (setting.view.showIcon == false || !tools.apply(setting.view.showIcon, [setting.treeId, node], true)) {
  1278. icoStyle.push("width:0px;height:0px;");
  1279. }
  1280. }
  1281. return icoStyle.join('');
  1282. },
  1283. makeNodeLineClass: function (setting, node) {
  1284. var lineClass = [];
  1285. if (setting.view.showLine) {
  1286. if (node.level == 0 && node.isFirstNode && node.isLastNode) {
  1287. lineClass.push(consts.line.ROOT);
  1288. } else if (node.level == 0 && node.isFirstNode) {
  1289. lineClass.push(consts.line.ROOTS);
  1290. } else if (node.isLastNode) {
  1291. lineClass.push(consts.line.BOTTOM);
  1292. } else {
  1293. lineClass.push(consts.line.CENTER);
  1294. }
  1295. } else {
  1296. lineClass.push(consts.line.NOLINE);
  1297. }
  1298. if (node.isParent) {
  1299. lineClass.push(node.open ? consts.folder.OPEN : consts.folder.CLOSE);
  1300. } else {
  1301. lineClass.push(consts.folder.DOCU);
  1302. }
  1303. return view.makeNodeLineClassEx(node) + lineClass.join('_');
  1304. },
  1305. makeNodeLineClassEx: function (node) {
  1306. return consts.className.BUTTON + " " + consts.className.LEVEL + node.level + " " + consts.className.SWITCH + " ";
  1307. },
  1308. makeNodeTarget: function (node) {
  1309. return (node.target || "_blank");
  1310. },
  1311. makeNodeUrl: function (setting, node) {
  1312. var urlKey = setting.data.key.url;
  1313. return node[urlKey] ? node[urlKey] : null;
  1314. },
  1315. makeUlHtml: function (setting, node, html, content) {
  1316. 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"), "'>");
  1317. html.push(content);
  1318. html.push("</ul>");
  1319. },
  1320. makeUlLineClass: function (setting, node) {
  1321. return ((setting.view.showLine && !node.isLastNode) ? consts.line.LINE : "");
  1322. },
  1323. removeChildNodes: function (setting, node) {
  1324. if (!node) return;
  1325. var childKey = setting.data.key.children,
  1326. nodes = node[childKey];
  1327. if (!nodes) return;
  1328. for (var i = 0, l = nodes.length; i < l; i++) {
  1329. data.removeNodeCache(setting, nodes[i]);
  1330. }
  1331. data.removeSelectedNode(setting);
  1332. delete node[childKey];
  1333. if (!setting.data.keep.parent) {
  1334. node.isParent = false;
  1335. node.open = false;
  1336. var tmp_switchObj = $$(node, consts.id.SWITCH, setting),
  1337. tmp_icoObj = $$(node, consts.id.ICON, setting);
  1338. view.replaceSwitchClass(node, tmp_switchObj, consts.folder.DOCU);
  1339. view.replaceIcoClass(node, tmp_icoObj, consts.folder.DOCU);
  1340. $$(node, consts.id.UL, setting).remove();
  1341. } else {
  1342. $$(node, consts.id.UL, setting).empty();
  1343. }
  1344. },
  1345. scrollIntoView: function (dom) {
  1346. if (!dom) {
  1347. return;
  1348. }
  1349. // code src: http://jsfiddle.net/08u6cxwj/
  1350. if (!Element.prototype.scrollIntoViewIfNeeded) {
  1351. Element.prototype.scrollIntoViewIfNeeded = function (centerIfNeeded) {
  1352. function withinBounds(value, min, max, extent) {
  1353. if (false === centerIfNeeded || max <= value + extent && value <= min + extent) {
  1354. return Math.min(max, Math.max(min, value));
  1355. } else {
  1356. return (min + max) / 2;
  1357. }
  1358. }
  1359. function makeArea(left, top, width, height) {
  1360. return {
  1361. "left": left, "top": top, "width": width, "height": height
  1362. , "right": left + width, "bottom": top + height
  1363. , "translate": function (x, y) {
  1364. return makeArea(x + left, y + top, width, height);
  1365. }
  1366. , "relativeFromTo": function (lhs, rhs) {
  1367. var newLeft = left, newTop = top;
  1368. lhs = lhs.offsetParent;
  1369. rhs = rhs.offsetParent;
  1370. if (lhs === rhs) {
  1371. return area;
  1372. }
  1373. for (; lhs; lhs = lhs.offsetParent) {
  1374. newLeft += lhs.offsetLeft + lhs.clientLeft;
  1375. newTop += lhs.offsetTop + lhs.clientTop;
  1376. }
  1377. for (; rhs; rhs = rhs.offsetParent) {
  1378. newLeft -= rhs.offsetLeft + rhs.clientLeft;
  1379. newTop -= rhs.offsetTop + rhs.clientTop;
  1380. }
  1381. return makeArea(newLeft, newTop, width, height);
  1382. }
  1383. };
  1384. }
  1385. var parent, elem = this, area = makeArea(
  1386. this.offsetLeft, this.offsetTop,
  1387. this.offsetWidth, this.offsetHeight);
  1388. while (tools.isElement(parent = elem.parentNode)) {
  1389. var clientLeft = parent.offsetLeft + parent.clientLeft;
  1390. var clientTop = parent.offsetTop + parent.clientTop;
  1391. // Make area relative to parent's client area.
  1392. area = area.relativeFromTo(elem, parent).translate(-clientLeft, -clientTop);
  1393. parent.scrollLeft = withinBounds(
  1394. parent.scrollLeft,
  1395. area.right - parent.clientWidth, area.left,
  1396. parent.clientWidth);
  1397. parent.scrollTop = withinBounds(
  1398. parent.scrollTop,
  1399. area.bottom - parent.clientHeight, area.top,
  1400. parent.clientHeight);
  1401. // Determine actual scroll amount by reading back scroll properties.
  1402. area = area.translate(clientLeft - parent.scrollLeft,
  1403. clientTop - parent.scrollTop);
  1404. elem = parent;
  1405. }
  1406. };
  1407. }
  1408. dom.scrollIntoViewIfNeeded();
  1409. },
  1410. setFirstNode: function (setting, parentNode) {
  1411. var childKey = setting.data.key.children, childLength = parentNode[childKey].length;
  1412. if (childLength > 0) {
  1413. parentNode[childKey][0].isFirstNode = true;
  1414. }
  1415. },
  1416. setLastNode: function (setting, parentNode) {
  1417. var childKey = setting.data.key.children, childLength = parentNode[childKey].length;
  1418. if (childLength > 0) {
  1419. parentNode[childKey][childLength - 1].isLastNode = true;
  1420. }
  1421. },
  1422. removeNode: function (setting, node) {
  1423. var root = data.getRoot(setting),
  1424. childKey = setting.data.key.children,
  1425. parentNode = (node.parentTId) ? node.getParentNode() : root;
  1426. node.isFirstNode = false;
  1427. node.isLastNode = false;
  1428. node.getPreNode = function () {
  1429. return null;
  1430. };
  1431. node.getNextNode = function () {
  1432. return null;
  1433. };
  1434. if (!data.getNodeCache(setting, node.tId)) {
  1435. return;
  1436. }
  1437. $$(node, setting).remove();
  1438. data.removeNodeCache(setting, node);
  1439. data.removeSelectedNode(setting, node);
  1440. for (var i = 0, l = parentNode[childKey].length; i < l; i++) {
  1441. if (parentNode[childKey][i].tId == node.tId) {
  1442. parentNode[childKey].splice(i, 1);
  1443. break;
  1444. }
  1445. }
  1446. view.setFirstNode(setting, parentNode);
  1447. view.setLastNode(setting, parentNode);
  1448. var tmp_ulObj, tmp_switchObj, tmp_icoObj,
  1449. childLength = parentNode[childKey].length;
  1450. //repair nodes old parent
  1451. if (!setting.data.keep.parent && childLength == 0) {
  1452. //old parentNode has no child nodes
  1453. parentNode.isParent = false;
  1454. parentNode.open = false;
  1455. tmp_ulObj = $$(parentNode, consts.id.UL, setting);
  1456. tmp_switchObj = $$(parentNode, consts.id.SWITCH, setting);
  1457. tmp_icoObj = $$(parentNode, consts.id.ICON, setting);
  1458. view.replaceSwitchClass(parentNode, tmp_switchObj, consts.folder.DOCU);
  1459. view.replaceIcoClass(parentNode, tmp_icoObj, consts.folder.DOCU);
  1460. tmp_ulObj.css("display", "none");
  1461. } else if (setting.view.showLine && childLength > 0) {
  1462. //old parentNode has child nodes
  1463. var newLast = parentNode[childKey][childLength - 1];
  1464. tmp_ulObj = $$(newLast, consts.id.UL, setting);
  1465. tmp_switchObj = $$(newLast, consts.id.SWITCH, setting);
  1466. tmp_icoObj = $$(newLast, consts.id.ICON, setting);
  1467. if (parentNode == root) {
  1468. if (parentNode[childKey].length == 1) {
  1469. //node was root, and ztree has only one root after move node
  1470. view.replaceSwitchClass(newLast, tmp_switchObj, consts.line.ROOT);
  1471. } else {
  1472. var tmp_first_switchObj = $$(parentNode[childKey][0], consts.id.SWITCH, setting);
  1473. view.replaceSwitchClass(parentNode[childKey][0], tmp_first_switchObj, consts.line.ROOTS);
  1474. view.replaceSwitchClass(newLast, tmp_switchObj, consts.line.BOTTOM);
  1475. }
  1476. } else {
  1477. view.replaceSwitchClass(newLast, tmp_switchObj, consts.line.BOTTOM);
  1478. }
  1479. tmp_ulObj.removeClass(consts.line.LINE);
  1480. }
  1481. },
  1482. replaceIcoClass: function (node, obj, newName) {
  1483. if (!obj || node.isAjaxing) return;
  1484. var tmpName = obj.attr("class");
  1485. if (tmpName == undefined) return;
  1486. var tmpList = tmpName.split("_");
  1487. switch (newName) {
  1488. case consts.folder.OPEN:
  1489. case consts.folder.CLOSE:
  1490. case consts.folder.DOCU:
  1491. tmpList[tmpList.length - 1] = newName;
  1492. break;
  1493. }
  1494. obj.attr("class", tmpList.join("_"));
  1495. },
  1496. replaceSwitchClass: function (node, obj, newName) {
  1497. if (!obj) return;
  1498. var tmpName = obj.attr("class");
  1499. if (tmpName == undefined) return;
  1500. var tmpList = tmpName.split("_");
  1501. switch (newName) {
  1502. case consts.line.ROOT:
  1503. case consts.line.ROOTS:
  1504. case consts.line.CENTER:
  1505. case consts.line.BOTTOM:
  1506. case consts.line.NOLINE:
  1507. tmpList[0] = view.makeNodeLineClassEx(node) + newName;
  1508. break;
  1509. case consts.folder.OPEN:
  1510. case consts.folder.CLOSE:
  1511. case consts.folder.DOCU:
  1512. tmpList[1] = newName;
  1513. break;
  1514. }
  1515. obj.attr("class", tmpList.join("_"));
  1516. if (newName !== consts.folder.DOCU) {
  1517. obj.removeAttr("disabled");
  1518. } else {
  1519. obj.attr("disabled", "disabled");
  1520. }
  1521. },
  1522. selectNode: function (setting, node, addFlag) {
  1523. if (!addFlag) {
  1524. view.cancelPreSelectedNode(setting, null, node);
  1525. }
  1526. $$(node, consts.id.A, setting).addClass(consts.node.CURSELECTED);
  1527. data.addSelectedNode(setting, node);
  1528. setting.treeObj.trigger(consts.event.SELECTED, [setting.treeId, node]);
  1529. },
  1530. setNodeFontCss: function (setting, treeNode) {
  1531. var aObj = $$(treeNode, consts.id.A, setting),
  1532. fontCss = view.makeNodeFontCss(setting, treeNode);
  1533. if (fontCss) {
  1534. aObj.css(fontCss);
  1535. }
  1536. },
  1537. setNodeLineIcos: function (setting, node) {
  1538. if (!node) return;
  1539. var switchObj = $$(node, consts.id.SWITCH, setting),
  1540. ulObj = $$(node, consts.id.UL, setting),
  1541. icoObj = $$(node, consts.id.ICON, setting),
  1542. ulLine = view.makeUlLineClass(setting, node);
  1543. if (ulLine.length == 0) {
  1544. ulObj.removeClass(consts.line.LINE);
  1545. } else {
  1546. ulObj.addClass(ulLine);
  1547. }
  1548. switchObj.attr("class", view.makeNodeLineClass(setting, node));
  1549. if (node.isParent) {
  1550. switchObj.removeAttr("disabled");
  1551. } else {
  1552. switchObj.attr("disabled", "disabled");
  1553. }
  1554. icoObj.removeAttr("style");
  1555. icoObj.attr("style", view.makeNodeIcoStyle(setting, node));
  1556. icoObj.attr("class", view.makeNodeIcoClass(setting, node));
  1557. },
  1558. setNodeName: function (setting, node) {
  1559. var title = data.getNodeTitle(setting, node),
  1560. nObj = $$(node, consts.id.SPAN, setting);
  1561. nObj.empty();
  1562. if (setting.view.nameIsHTML) {
  1563. nObj.html(data.getNodeName(setting, node));
  1564. } else {
  1565. nObj.text(data.getNodeName(setting, node));
  1566. }
  1567. if (tools.apply(setting.view.showTitle, [setting.treeId, node], setting.view.showTitle)) {
  1568. var aObj = $$(node, consts.id.A, setting);
  1569. aObj.attr("title", !title ? "" : title);
  1570. }
  1571. },
  1572. setNodeTarget: function (setting, node) {
  1573. var aObj = $$(node, consts.id.A, setting);
  1574. aObj.attr("target", view.makeNodeTarget(node));
  1575. },
  1576. setNodeUrl: function (setting, node) {
  1577. var aObj = $$(node, consts.id.A, setting),
  1578. url = view.makeNodeUrl(setting, node);
  1579. if (url == null || url.length == 0) {
  1580. aObj.removeAttr("href");
  1581. } else {
  1582. aObj.attr("href", url);
  1583. }
  1584. },
  1585. switchNode: function (setting, node) {
  1586. if (node.open || !tools.canAsync(setting, node)) {
  1587. view.expandCollapseNode(setting, node, !node.open);
  1588. } else if (setting.async.enable) {
  1589. if (!view.asyncNode(setting, node)) {
  1590. view.expandCollapseNode(setting, node, !node.open);
  1591. return;
  1592. }
  1593. } else if (node) {
  1594. view.expandCollapseNode(setting, node, !node.open);
  1595. }
  1596. }
  1597. };
  1598. // zTree defind
  1599. $.fn.zTree = {
  1600. consts: _consts,
  1601. _z: {
  1602. tools: tools,
  1603. view: view,
  1604. event: event,
  1605. data: data
  1606. },
  1607. getZTreeObj: function (treeId) {
  1608. var o = data.getZTreeTools(treeId);
  1609. return o ? o : null;
  1610. },
  1611. destroy: function (treeId) {
  1612. if (!!treeId && treeId.length > 0) {
  1613. view.destroy(data.getSetting(treeId));
  1614. } else {
  1615. for (var s in settings) {
  1616. view.destroy(settings[s]);
  1617. }
  1618. }
  1619. },
  1620. init: function (obj, zSetting, zNodes) {
  1621. var setting = tools.clone(_setting);
  1622. $.extend(true, setting, zSetting);
  1623. setting.treeId = obj.attr("id");
  1624. setting.treeObj = obj;
  1625. setting.treeObj.empty();
  1626. settings[setting.treeId] = setting;
  1627. //For some older browser,(e.g., ie6)
  1628. if (typeof document.body.style.maxHeight === "undefined") {
  1629. setting.view.expandSpeed = "";
  1630. }
  1631. data.initRoot(setting);
  1632. var root = data.getRoot(setting),
  1633. childKey = setting.data.key.children;
  1634. zNodes = zNodes ? tools.clone(tools.isArray(zNodes) ? zNodes : [zNodes]) : [];
  1635. if (setting.data.simpleData.enable) {
  1636. root[childKey] = data.transformTozTreeFormat(setting, zNodes);
  1637. } else {
  1638. root[childKey] = zNodes;
  1639. }
  1640. data.initCache(setting);
  1641. event.unbindTree(setting);
  1642. event.bindTree(setting);
  1643. event.unbindEvent(setting);
  1644. event.bindEvent(setting);
  1645. var zTreeTools = {
  1646. setting: setting,
  1647. addNodes: function (parentNode, index, newNodes, isSilent) {
  1648. if (!parentNode) parentNode = null;
  1649. if (parentNode && !parentNode.isParent && setting.data.keep.leaf) return null;
  1650. var i = parseInt(index, 10);
  1651. if (isNaN(i)) {
  1652. isSilent = !!newNodes;
  1653. newNodes = index;
  1654. index = -1;
  1655. } else {
  1656. index = i;
  1657. }
  1658. if (!newNodes) return null;
  1659. var xNewNodes = tools.clone(tools.isArray(newNodes) ? newNodes : [newNodes]);
  1660. function addCallback() {
  1661. view.addNodes(setting, parentNode, index, xNewNodes, (isSilent == true));
  1662. }
  1663. if (tools.canAsync(setting, parentNode)) {
  1664. view.asyncNode(setting, parentNode, isSilent, addCallback);
  1665. } else {
  1666. addCallback();
  1667. }
  1668. return xNewNodes;
  1669. },
  1670. cancelSelectedNode: function (node) {
  1671. view.cancelPreSelectedNode(setting, node);
  1672. },
  1673. destroy: function () {
  1674. view.destroy(setting);
  1675. },
  1676. expandAll: function (expandFlag) {
  1677. expandFlag = !!expandFlag;
  1678. view.expandCollapseSonNode(setting, null, expandFlag, true);
  1679. return expandFlag;
  1680. },
  1681. expandNode: function (node, expandFlag, sonSign, focus, callbackFlag) {
  1682. if (!node || !node.isParent) return null;
  1683. if (expandFlag !== true && expandFlag !== false) {
  1684. expandFlag = !node.open;
  1685. }
  1686. callbackFlag = !!callbackFlag;
  1687. if (callbackFlag && expandFlag && (tools.apply(setting.callback.beforeExpand, [setting.treeId, node], true) == false)) {
  1688. return null;
  1689. } else if (callbackFlag && !expandFlag && (tools.apply(setting.callback.beforeCollapse, [setting.treeId, node], true) == false)) {
  1690. return null;
  1691. }
  1692. if (expandFlag && node.parentTId) {
  1693. view.expandCollapseParentNode(setting, node.getParentNode(), expandFlag, false);
  1694. }
  1695. if (expandFlag === node.open && !sonSign) {
  1696. return null;
  1697. }
  1698. data.getRoot(setting).expandTriggerFlag = callbackFlag;
  1699. if (!tools.canAsync(setting, node) && sonSign) {
  1700. view.expandCollapseSonNode(setting, node, expandFlag, true, showNodeFocus);
  1701. } else {
  1702. node.open = !expandFlag;
  1703. view.switchNode(this.setting, node);
  1704. showNodeFocus();
  1705. }
  1706. return expandFlag;
  1707. function showNodeFocus() {
  1708. var a = $$(node, setting).get(0);
  1709. if (a && focus !== false) {
  1710. view.scrollIntoView(a);
  1711. }
  1712. }
  1713. },
  1714. getNodes: function () {
  1715. return data.getNodes(setting);
  1716. },
  1717. getNodeByParam: function (key, value, parentNode) {
  1718. if (!key) return null;
  1719. return data.getNodeByParam(setting, parentNode ? parentNode[setting.data.key.children] : data.getNodes(setting), key, value);
  1720. },
  1721. getNodeByTId: function (tId) {
  1722. return data.getNodeCache(setting, tId);
  1723. },
  1724. getNodesByParam: function (key, value, parentNode) {
  1725. if (!key) return null;
  1726. return data.getNodesByParam(setting, parentNode ? parentNode[setting.data.key.children] : data.getNodes(setting), key, value);
  1727. },
  1728. getNodesByParamFuzzy: function (key, value, parentNode) {
  1729. if (!key) return null;
  1730. return data.getNodesByParamFuzzy(setting, parentNode ? parentNode[setting.data.key.children] : data.getNodes(setting), key, value);
  1731. },
  1732. getNodesByFilter: function (filter, isSingle, parentNode, invokeParam) {
  1733. isSingle = !!isSingle;
  1734. if (!filter || (typeof filter != "function")) return (isSingle ? null : []);
  1735. return data.getNodesByFilter(setting, parentNode ? parentNode[setting.data.key.children] : data.getNodes(setting), filter, isSingle, invokeParam);
  1736. },
  1737. getNodeIndex: function (node) {
  1738. if (!node) return null;
  1739. var childKey = setting.data.key.children,
  1740. parentNode = (node.parentTId) ? node.getParentNode() : data.getRoot(setting);
  1741. for (var i = 0, l = parentNode[childKey].length; i < l; i++) {
  1742. if (parentNode[childKey][i] == node) return i;
  1743. }
  1744. return -1;
  1745. },
  1746. getSelectedNodes: function () {
  1747. var r = [], list = data.getRoot(setting).curSelectedList;
  1748. for (var i = 0, l = list.length; i < l; i++) {
  1749. r.push(list[i]);
  1750. }
  1751. return r;
  1752. },
  1753. isSelectedNode: function (node) {
  1754. return data.isSelectedNode(setting, node);
  1755. },
  1756. reAsyncChildNodes: function (parentNode, reloadType, isSilent) {
  1757. if (!this.setting.async.enable) return;
  1758. var isRoot = !parentNode;
  1759. if (isRoot) {
  1760. parentNode = data.getRoot(setting);
  1761. }
  1762. if (reloadType == "refresh") {
  1763. var childKey = this.setting.data.key.children;
  1764. for (var i = 0, l = parentNode[childKey] ? parentNode[childKey].length : 0; i < l; i++) {
  1765. data.removeNodeCache(setting, parentNode[childKey][i]);
  1766. }
  1767. data.removeSelectedNode(setting);
  1768. parentNode[childKey] = [];
  1769. if (isRoot) {
  1770. this.setting.treeObj.empty();
  1771. } else {
  1772. var ulObj = $$(parentNode, consts.id.UL, setting);
  1773. ulObj.empty();
  1774. }
  1775. }
  1776. view.asyncNode(this.setting, isRoot ? null : parentNode, !!isSilent);
  1777. },
  1778. refresh: function () {
  1779. this.setting.treeObj.empty();
  1780. var root = data.getRoot(setting),
  1781. nodes = root[setting.data.key.children]
  1782. data.initRoot(setting);
  1783. root[setting.data.key.children] = nodes
  1784. data.initCache(setting);
  1785. view.createNodes(setting, 0, root[setting.data.key.children], null, -1);
  1786. },
  1787. removeChildNodes: function (node) {
  1788. if (!node) return null;
  1789. var childKey = setting.data.key.children,
  1790. nodes = node[childKey];
  1791. view.removeChildNodes(setting, node);
  1792. return nodes ? nodes : null;
  1793. },
  1794. removeNode: function (node, callbackFlag) {
  1795. if (!node) return;
  1796. callbackFlag = !!callbackFlag;
  1797. if (callbackFlag && tools.apply(setting.callback.beforeRemove, [setting.treeId, node], true) == false) return;
  1798. view.removeNode(setting, node);
  1799. if (callbackFlag) {
  1800. this.setting.treeObj.trigger(consts.event.REMOVE, [setting.treeId, node]);
  1801. }
  1802. },
  1803. selectNode: function (node, addFlag, isSilent) {
  1804. if (!node) return;
  1805. if (tools.uCanDo(setting)) {
  1806. addFlag = setting.view.selectedMulti && addFlag;
  1807. if (node.parentTId) {
  1808. view.expandCollapseParentNode(setting, node.getParentNode(), true, false, showNodeFocus);
  1809. } else if (!isSilent) {
  1810. try {
  1811. $$(node, setting).focus().blur();
  1812. } catch (e) {
  1813. }
  1814. }
  1815. view.selectNode(setting, node, addFlag);
  1816. }
  1817. function showNodeFocus() {
  1818. if (isSilent) {
  1819. return;
  1820. }
  1821. var a = $$(node, setting).get(0);
  1822. view.scrollIntoView(a);
  1823. }
  1824. },
  1825. transformTozTreeNodes: function (simpleNodes) {
  1826. return data.transformTozTreeFormat(setting, simpleNodes);
  1827. },
  1828. transformToArray: function (nodes) {
  1829. return data.transformToArrayFormat(setting, nodes);
  1830. },
  1831. updateNode: function (node, checkTypeFlag) {
  1832. if (!node) return;
  1833. var nObj = $$(node, setting);
  1834. if (nObj.get(0) && tools.uCanDo(setting)) {
  1835. view.setNodeName(setting, node);
  1836. view.setNodeTarget(setting, node);
  1837. view.setNodeUrl(setting, node);
  1838. view.setNodeLineIcos(setting, node);
  1839. view.setNodeFontCss(setting, node);
  1840. }
  1841. }
  1842. }
  1843. root.treeTools = zTreeTools;
  1844. data.setZTreeTools(setting, zTreeTools);
  1845. if (root[childKey] && root[childKey].length > 0) {
  1846. view.createNodes(setting, 0, root[childKey], null, -1);
  1847. } else if (setting.async.enable && setting.async.url && setting.async.url !== '') {
  1848. view.asyncNode(setting);
  1849. }
  1850. return zTreeTools;
  1851. }
  1852. };
  1853. var zt = $.fn.zTree,
  1854. $$ = tools.$,
  1855. consts = zt.consts;
  1856. })(jQuery);