jquery.ztree.exhide.js 10 KB

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