jquery.ztree.exhide.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405
  1. /*
  2. * JQuery zTree exHideNodes
  3. * v3.5.42
  4. * http://treejs.cn/
  5. *
  6. * Copyright (c) 2010 Hunter.z
  7. *
  8. * Licensed same as jquery - MIT License
  9. * http://www.opensource.org/licenses/mit-license.php
  10. *
  11. * Date: 2020-01-19
  12. */
  13. (function ($) {
  14. var _setting = {
  15. data: {
  16. key: {
  17. isHidden: "isHidden"
  18. }
  19. }
  20. };
  21. //default init node of exLib
  22. var _initNode = function (setting, level, n, parentNode, isFirstNode, isLastNode, openFlag) {
  23. var isHidden = data.isHidden(setting, n);
  24. data.isHidden(setting, n, isHidden);
  25. data.initHideForExCheck(setting, n);
  26. },
  27. //add dom for check
  28. _beforeA = function (setting, node, html) {
  29. },
  30. //update zTreeObj, add method of exLib
  31. _zTreeTools = function (setting, zTreeTools) {
  32. zTreeTools.showNodes = function (nodes, options) {
  33. view.showNodes(setting, nodes, options);
  34. }
  35. zTreeTools.showNode = function (node, options) {
  36. if (!node) {
  37. return;
  38. }
  39. view.showNodes(setting, [node], options);
  40. }
  41. zTreeTools.hideNodes = function (nodes, options) {
  42. view.hideNodes(setting, nodes, options);
  43. }
  44. zTreeTools.hideNode = function (node, options) {
  45. if (!node) {
  46. return;
  47. }
  48. view.hideNodes(setting, [node], options);
  49. }
  50. var _checkNode = zTreeTools.checkNode;
  51. if (_checkNode) {
  52. zTreeTools.checkNode = function (node, checked, checkTypeFlag, callbackFlag) {
  53. if (!!node && !!data.isHidden(setting, node)) {
  54. return;
  55. }
  56. _checkNode.apply(zTreeTools, arguments);
  57. }
  58. }
  59. },
  60. //method of operate data
  61. _data = {
  62. initHideForExCheck: function (setting, n) {
  63. var isHidden = data.isHidden(setting, n);
  64. if (isHidden && setting.check && setting.check.enable) {
  65. if (typeof n._nocheck == "undefined") {
  66. n._nocheck = !!n.nocheck
  67. n.nocheck = true;
  68. }
  69. n.check_Child_State = -1;
  70. if (view.repairParentChkClassWithSelf) {
  71. view.repairParentChkClassWithSelf(setting, n);
  72. }
  73. }
  74. },
  75. initShowForExCheck: function (setting, n) {
  76. var isHidden = data.isHidden(setting, n);
  77. if (!isHidden && setting.check && setting.check.enable) {
  78. if (typeof n._nocheck != "undefined") {
  79. n.nocheck = n._nocheck;
  80. delete n._nocheck;
  81. }
  82. if (view.setChkClass) {
  83. var checkObj = $$(n, consts.id.CHECK, setting);
  84. view.setChkClass(setting, checkObj, n);
  85. }
  86. if (view.repairParentChkClassWithSelf) {
  87. view.repairParentChkClassWithSelf(setting, n);
  88. }
  89. }
  90. }
  91. },
  92. //method of operate ztree dom
  93. _view = {
  94. clearOldFirstNode: function (setting, node) {
  95. var n = node.getNextNode();
  96. while (!!n) {
  97. if (n.isFirstNode) {
  98. n.isFirstNode = false;
  99. view.setNodeLineIcos(setting, n);
  100. break;
  101. }
  102. if (n.isLastNode) {
  103. break;
  104. }
  105. n = n.getNextNode();
  106. }
  107. },
  108. clearOldLastNode: function (setting, node, openFlag) {
  109. var n = node.getPreNode();
  110. while (!!n) {
  111. if (n.isLastNode) {
  112. n.isLastNode = false;
  113. if (openFlag) {
  114. view.setNodeLineIcos(setting, n);
  115. }
  116. break;
  117. }
  118. if (n.isFirstNode) {
  119. break;
  120. }
  121. n = n.getPreNode();
  122. }
  123. },
  124. makeDOMNodeMainBefore: function (html, setting, node) {
  125. var isHidden = data.isHidden(setting, node);
  126. html.push("<li ", (isHidden ? "style='display:none;' " : ""), "id='", node.tId, "' class='", consts.className.LEVEL, node.level, "' tabindex='0' hidefocus='true' treenode>");
  127. },
  128. showNode: function (setting, node, options) {
  129. data.isHidden(setting, node, false);
  130. data.initShowForExCheck(setting, node);
  131. $$(node, setting).show();
  132. },
  133. showNodes: function (setting, nodes, options) {
  134. if (!nodes || nodes.length == 0) {
  135. return;
  136. }
  137. var pList = {}, i, j;
  138. for (i = 0, j = nodes.length; i < j; i++) {
  139. var n = nodes[i];
  140. if (!pList[n.parentTId]) {
  141. var pn = n.getParentNode();
  142. pList[n.parentTId] = (pn === null) ? data.getRoot(setting) : n.getParentNode();
  143. }
  144. view.showNode(setting, n, options);
  145. }
  146. for (var tId in pList) {
  147. var children = data.nodeChildren(setting, pList[tId]);
  148. view.setFirstNodeForShow(setting, children);
  149. view.setLastNodeForShow(setting, children);
  150. }
  151. },
  152. hideNode: function (setting, node, options) {
  153. data.isHidden(setting, node, true);
  154. node.isFirstNode = false;
  155. node.isLastNode = false;
  156. data.initHideForExCheck(setting, node);
  157. view.cancelPreSelectedNode(setting, node);
  158. $$(node, setting).hide();
  159. },
  160. hideNodes: function (setting, nodes, options) {
  161. if (!nodes || nodes.length == 0) {
  162. return;
  163. }
  164. var pList = {}, i, j;
  165. for (i = 0, j = nodes.length; i < j; i++) {
  166. var n = nodes[i];
  167. if ((n.isFirstNode || n.isLastNode) && !pList[n.parentTId]) {
  168. var pn = n.getParentNode();
  169. pList[n.parentTId] = (pn === null) ? data.getRoot(setting) : n.getParentNode();
  170. }
  171. view.hideNode(setting, n, options);
  172. }
  173. for (var tId in pList) {
  174. var children = data.nodeChildren(setting, pList[tId]);
  175. view.setFirstNodeForHide(setting, children);
  176. view.setLastNodeForHide(setting, children);
  177. }
  178. },
  179. setFirstNode: function (setting, parentNode) {
  180. var children = data.nodeChildren(setting, parentNode);
  181. var isHidden = data.isHidden(setting, children[0], false);
  182. if (children.length > 0 && !isHidden) {
  183. children[0].isFirstNode = true;
  184. } else if (children.length > 0) {
  185. view.setFirstNodeForHide(setting, children);
  186. }
  187. },
  188. setLastNode: function (setting, parentNode) {
  189. var children = data.nodeChildren(setting, parentNode);
  190. var isHidden = data.isHidden(setting, children[0]);
  191. if (children.length > 0 && !isHidden) {
  192. children[children.length - 1].isLastNode = true;
  193. } else if (children.length > 0) {
  194. view.setLastNodeForHide(setting, children);
  195. }
  196. },
  197. setFirstNodeForHide: function (setting, nodes) {
  198. var n, i, j;
  199. for (i = 0, j = nodes.length; i < j; i++) {
  200. n = nodes[i];
  201. if (n.isFirstNode) {
  202. break;
  203. }
  204. var isHidden = data.isHidden(setting, n);
  205. if (!isHidden && !n.isFirstNode) {
  206. n.isFirstNode = true;
  207. view.setNodeLineIcos(setting, n);
  208. break;
  209. } else {
  210. n = null;
  211. }
  212. }
  213. return n;
  214. },
  215. setFirstNodeForShow: function (setting, nodes) {
  216. var n, i, j, first, old;
  217. for (i = 0, j = nodes.length; i < j; i++) {
  218. n = nodes[i];
  219. var isHidden = data.isHidden(setting, n);
  220. if (!first && !isHidden && n.isFirstNode) {
  221. first = n;
  222. break;
  223. } else if (!first && !isHidden && !n.isFirstNode) {
  224. n.isFirstNode = true;
  225. first = n;
  226. view.setNodeLineIcos(setting, n);
  227. } else if (first && n.isFirstNode) {
  228. n.isFirstNode = false;
  229. old = n;
  230. view.setNodeLineIcos(setting, n);
  231. break;
  232. } else {
  233. n = null;
  234. }
  235. }
  236. return {"new": first, "old": old};
  237. },
  238. setLastNodeForHide: function (setting, nodes) {
  239. var n, i;
  240. for (i = nodes.length - 1; i >= 0; i--) {
  241. n = nodes[i];
  242. if (n.isLastNode) {
  243. break;
  244. }
  245. var isHidden = data.isHidden(setting, n);
  246. if (!isHidden && !n.isLastNode) {
  247. n.isLastNode = true;
  248. view.setNodeLineIcos(setting, n);
  249. break;
  250. } else {
  251. n = null;
  252. }
  253. }
  254. return n;
  255. },
  256. setLastNodeForShow: function (setting, nodes) {
  257. var n, i, j, last, old;
  258. for (i = nodes.length - 1; i >= 0; i--) {
  259. n = nodes[i];
  260. var isHidden = data.isHidden(setting, n);
  261. if (!last && !isHidden && n.isLastNode) {
  262. last = n;
  263. break;
  264. } else if (!last && !isHidden && !n.isLastNode) {
  265. n.isLastNode = true;
  266. last = n;
  267. view.setNodeLineIcos(setting, n);
  268. } else if (last && n.isLastNode) {
  269. n.isLastNode = false;
  270. old = n;
  271. view.setNodeLineIcos(setting, n);
  272. break;
  273. } else {
  274. n = null;
  275. }
  276. }
  277. return {"new": last, "old": old};
  278. }
  279. },
  280. _z = {
  281. view: _view,
  282. data: _data
  283. };
  284. $.extend(true, $.fn.zTree._z, _z);
  285. var zt = $.fn.zTree,
  286. tools = zt._z.tools,
  287. consts = zt.consts,
  288. view = zt._z.view,
  289. data = zt._z.data,
  290. event = zt._z.event,
  291. $$ = tools.$;
  292. data.isHidden = function (setting, node, newIsHidden) {
  293. if (!node) {
  294. return false;
  295. }
  296. var key = setting.data.key.isHidden;
  297. if (typeof newIsHidden !== 'undefined') {
  298. if (typeof newIsHidden === "string") {
  299. newIsHidden = tools.eqs(newIsHidden, "true");
  300. }
  301. newIsHidden = !!newIsHidden;
  302. node[key] = newIsHidden;
  303. } else if (typeof node[key] == "string"){
  304. node[key] = tools.eqs(node[key], "true");
  305. } else {
  306. node[key] = !!node[key];
  307. }
  308. return node[key];
  309. };
  310. data.exSetting(_setting);
  311. data.addInitNode(_initNode);
  312. data.addBeforeA(_beforeA);
  313. data.addZTreeTools(_zTreeTools);
  314. // Override method in core
  315. var _dInitNode = data.initNode;
  316. data.initNode = function (setting, level, node, parentNode, isFirstNode, isLastNode, openFlag) {
  317. var tmpPNode = (parentNode) ? parentNode : data.getRoot(setting),
  318. children = tmpPNode[setting.data.key.children];
  319. data.tmpHideFirstNode = view.setFirstNodeForHide(setting, children);
  320. data.tmpHideLastNode = view.setLastNodeForHide(setting, children);
  321. if (openFlag) {
  322. view.setNodeLineIcos(setting, data.tmpHideFirstNode);
  323. view.setNodeLineIcos(setting, data.tmpHideLastNode);
  324. }
  325. isFirstNode = (data.tmpHideFirstNode === node);
  326. isLastNode = (data.tmpHideLastNode === node);
  327. if (_dInitNode) _dInitNode.apply(data, arguments);
  328. if (openFlag && isLastNode) {
  329. view.clearOldLastNode(setting, node, openFlag);
  330. }
  331. };
  332. var _makeChkFlag = data.makeChkFlag;
  333. if (!!_makeChkFlag) {
  334. data.makeChkFlag = function (setting, node) {
  335. if (!!node && !!data.isHidden(setting, node)) {
  336. return;
  337. }
  338. _makeChkFlag.apply(data, arguments);
  339. }
  340. }
  341. var _getTreeCheckedNodes = data.getTreeCheckedNodes;
  342. if (!!_getTreeCheckedNodes) {
  343. data.getTreeCheckedNodes = function (setting, nodes, checked, results) {
  344. if (!!nodes && nodes.length > 0) {
  345. var p = nodes[0].getParentNode();
  346. if (!!p && !!data.isHidden(setting, p)) {
  347. return [];
  348. }
  349. }
  350. return _getTreeCheckedNodes.apply(data, arguments);
  351. }
  352. }
  353. var _getTreeChangeCheckedNodes = data.getTreeChangeCheckedNodes;
  354. if (!!_getTreeChangeCheckedNodes) {
  355. data.getTreeChangeCheckedNodes = function (setting, nodes, results) {
  356. if (!!nodes && nodes.length > 0) {
  357. var p = nodes[0].getParentNode();
  358. if (!!p && !!data.isHidden(setting, p)) {
  359. return [];
  360. }
  361. }
  362. return _getTreeChangeCheckedNodes.apply(data, arguments);
  363. }
  364. }
  365. var _expandCollapseSonNode = view.expandCollapseSonNode;
  366. if (!!_expandCollapseSonNode) {
  367. view.expandCollapseSonNode = function (setting, node, expandFlag, animateFlag, callback) {
  368. if (!!node && !!data.isHidden(setting, node)) {
  369. return;
  370. }
  371. _expandCollapseSonNode.apply(view, arguments);
  372. }
  373. }
  374. var _setSonNodeCheckBox = view.setSonNodeCheckBox;
  375. if (!!_setSonNodeCheckBox) {
  376. view.setSonNodeCheckBox = function (setting, node, value, srcNode) {
  377. if (!!node && !!data.isHidden(setting, node)) {
  378. return;
  379. }
  380. _setSonNodeCheckBox.apply(view, arguments);
  381. }
  382. }
  383. var _repairParentChkClassWithSelf = view.repairParentChkClassWithSelf;
  384. if (!!_repairParentChkClassWithSelf) {
  385. view.repairParentChkClassWithSelf = function (setting, node) {
  386. if (!!node && !!data.isHidden(setting, node)) {
  387. return;
  388. }
  389. _repairParentChkClassWithSelf.apply(view, arguments);
  390. }
  391. }
  392. })(jQuery);