jquery.ztree.exhide.js 11.1 KB
Newer Older
Z
ztree 已提交
1
/*
2
 * JQuery zTree exHideNodes v3.5.35
Z
ztree 已提交
3
 * http://treejs.cn/
Z
ztree 已提交
4 5 6 7 8 9 10
 *
 * Copyright (c) 2010 Hunter.z
 *
 * Licensed same as jquery - MIT License
 * http://www.opensource.org/licenses/mit-license.php
 *
 * email: hunter.z@263.net
11
 * Date: 2018-03-30
Z
ztree 已提交
12 13
 */
(function($){
14 15 16 17 18 19 20
  var _setting = {
    data: {
      key: {
        isHidden: "isHidden"
      }
    }
  };
Z
ztree 已提交
21 22
	//default init node of exLib
	var _initNode = function(setting, level, n, parentNode, isFirstNode, isLastNode, openFlag) {
23 24
		var isHidden = data.isHidden(setting, n);
		data.isHidden(setting, n, isHidden);
Z
ztree 已提交
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
		data.initHideForExCheck(setting, n);
	},
	//add dom for check
	_beforeA = function(setting, node, html) {},
	//update zTreeObj, add method of exLib
	_zTreeTools = function(setting, zTreeTools) {
		zTreeTools.showNodes = function(nodes, options) {
			view.showNodes(setting, nodes, options);
		}
		zTreeTools.showNode = function(node, options) {
			if (!node) {
				return;
			}
			view.showNodes(setting, [node], options);
		}
		zTreeTools.hideNodes = function(nodes, options) {
			view.hideNodes(setting, nodes, options);
		}
		zTreeTools.hideNode = function(node, options) {
			if (!node) {
				return;
			}
			view.hideNodes(setting, [node], options);
		}

		var _checkNode = zTreeTools.checkNode;
		if (_checkNode) {
			zTreeTools.checkNode = function(node, checked, checkTypeFlag, callbackFlag) {
53
				if (!!node && !!data.isHidden(setting, node)) {
Z
ztree 已提交
54 55 56 57 58 59 60 61 62
					return;
				}
				_checkNode.apply(zTreeTools, arguments);
			}
		}
	},
	//method of operate data
	_data = {
		initHideForExCheck: function(setting, n) {
63 64
      var isHidden = data.isHidden(setting, n);
			if (isHidden && setting.check && setting.check.enable) {
Z
ztree 已提交
65 66 67 68 69 70 71 72 73 74 75
				if(typeof n._nocheck == "undefined") {
					n._nocheck = !!n.nocheck
					n.nocheck = true;
				}
				n.check_Child_State = -1;
				if (view.repairParentChkClassWithSelf) {
					view.repairParentChkClassWithSelf(setting, n);
				}
			}
		},
		initShowForExCheck: function(setting, n) {
76 77
      var isHidden = data.isHidden(setting, n);
			if (!isHidden && setting.check && setting.check.enable) {
Z
ztree 已提交
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107
				if(typeof n._nocheck != "undefined") {
					n.nocheck = n._nocheck;
					delete n._nocheck;
				}
				if (view.setChkClass) {
					var checkObj = $$(n, consts.id.CHECK, setting);
					view.setChkClass(setting, checkObj, n);
				}
				if (view.repairParentChkClassWithSelf) {
					view.repairParentChkClassWithSelf(setting, n);
				}
			}
		}
	},
	//method of operate ztree dom
	_view = {
		clearOldFirstNode: function(setting, node) {
			var n = node.getNextNode();
			while(!!n){
				if (n.isFirstNode) {
					n.isFirstNode = false;
					view.setNodeLineIcos(setting, n);
					break;
				}
				if (n.isLastNode) {
					break;
				}
				n = n.getNextNode();
			}
		},
108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123
        clearOldLastNode: function(setting, node, openFlag) {
            var n = node.getPreNode();
            while(!!n){
                if (n.isLastNode) {
                    n.isLastNode = false;
                    if (openFlag) {
                        view.setNodeLineIcos(setting, n);
                    }
                    break;
                }
                if (n.isFirstNode) {
                    break;
                }
                n = n.getPreNode();
            }
        },
Z
ztree 已提交
124
		makeDOMNodeMainBefore: function(html, setting, node) {
125 126
      var isHidden = data.isHidden(setting, node);
			html.push("<li ", (isHidden ? "style='display:none;' " : ""), "id='", node.tId, "' class='", consts.className.LEVEL, node.level,"' tabindex='0' hidefocus='true' treenode>");
Z
ztree 已提交
127 128
		},
		showNode: function(setting, node, options) {
129
      data.isHidden(setting, node, false);
Z
ztree 已提交
130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146
			data.initShowForExCheck(setting, node);
			$$(node, setting).show();
		},
		showNodes: function(setting, nodes, options) {
			if (!nodes || nodes.length == 0) {
				return;
			}
			var pList = {}, i, j;
			for (i=0, j=nodes.length; i<j; i++) {
				var n = nodes[i];
				if (!pList[n.parentTId]) {
					var pn = n.getParentNode();
					pList[n.parentTId] = (pn === null) ? data.getRoot(setting) : n.getParentNode();
				}
				view.showNode(setting, n, options);
			}
			for (var tId in pList) {
147
				var children = data.nodeChildren(setting, pList[tId]);
Z
ztree 已提交
148 149 150 151 152
				view.setFirstNodeForShow(setting, children);
				view.setLastNodeForShow(setting, children);
			}
		},
		hideNode: function(setting, node, options) {
153
      data.isHidden(setting, node, true);
Z
ztree 已提交
154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173
			node.isFirstNode = false;
			node.isLastNode = false;
			data.initHideForExCheck(setting, node);
			view.cancelPreSelectedNode(setting, node);
			$$(node, setting).hide();
		},
		hideNodes: function(setting, nodes, options) {
			if (!nodes || nodes.length == 0) {
				return;
			}
			var pList = {}, i, j;
			for (i=0, j=nodes.length; i<j; i++) {
				var n = nodes[i];
				if ((n.isFirstNode || n.isLastNode) && !pList[n.parentTId]) {
					var pn = n.getParentNode();
					pList[n.parentTId] = (pn === null) ? data.getRoot(setting) : n.getParentNode();
				}
				view.hideNode(setting, n, options);
			}
			for (var tId in pList) {
174
				var children = data.nodeChildren(setting, pList[tId]);
Z
ztree 已提交
175 176 177 178 179
				view.setFirstNodeForHide(setting, children);
				view.setLastNodeForHide(setting, children);
			}
		},
		setFirstNode: function(setting, parentNode) {
180 181 182 183 184 185
      var children = data.nodeChildren(setting, parentNode);
      var isHidden = data.isHidden(setting, children[0], false);
			if (children.length > 0 && !isHidden) {
        children[0].isFirstNode = true;
			} else if (children.length > 0) {
				view.setFirstNodeForHide(setting, children);
Z
ztree 已提交
186 187 188
			}
		},
		setLastNode: function(setting, parentNode) {
189 190 191 192 193 194
      var children = data.nodeChildren(setting, parentNode);
      var isHidden = data.isHidden(setting, children[0]);
			if (children.length > 0 && !isHidden) {
        children[children.length - 1].isLastNode = true;
			} else if (children.length > 0) {
				view.setLastNodeForHide(setting, children);
Z
ztree 已提交
195 196 197 198 199 200 201 202 203
			}
		},
		setFirstNodeForHide: function(setting, nodes) {
			var n,i,j;
			for (i=0, j=nodes.length; i<j; i++) {
				n = nodes[i];
				if (n.isFirstNode) {
					break;
				}
204 205
        var isHidden = data.isHidden(setting, n);
				if (!isHidden && !n.isFirstNode) {
Z
ztree 已提交
206 207 208 209 210 211 212 213 214 215 216 217 218
					n.isFirstNode = true;
					view.setNodeLineIcos(setting, n);
					break;
				} else {
					n = null;
				}
			}
			return n;
		},
		setFirstNodeForShow: function(setting, nodes) {
			var n,i,j, first, old;
			for(i=0, j=nodes.length; i<j; i++) {
				n = nodes[i];
219 220
        var isHidden = data.isHidden(setting, n);
				if (!first && !isHidden && n.isFirstNode) {
Z
ztree 已提交
221 222
					first = n;
					break;
223
				} else if (!first && !isHidden && !n.isFirstNode) {
Z
ztree 已提交
224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244
					n.isFirstNode = true;
					first = n;
					view.setNodeLineIcos(setting, n);
				} else if (first && n.isFirstNode) {
					n.isFirstNode = false;
					old = n;
					view.setNodeLineIcos(setting, n);
					break;
				} else {
					n = null;
				}
			}
			return {"new":first, "old":old};
		},
		setLastNodeForHide: function(setting, nodes) {
			var n,i;
			for (i=nodes.length-1; i>=0; i--) {
				n = nodes[i];
				if (n.isLastNode) {
					break;
				}
245 246
        var isHidden = data.isHidden(setting, n);
				if (!isHidden && !n.isLastNode) {
Z
ztree 已提交
247 248 249 250 251 252 253 254 255 256 257 258 259
					n.isLastNode = true;
					view.setNodeLineIcos(setting, n);
					break;
				} else {
					n = null;
				}
			}
			return n;
		},
		setLastNodeForShow: function(setting, nodes) {
			var n,i,j, last, old;
			for (i=nodes.length-1; i>=0; i--) {
				n = nodes[i];
260 261
        var isHidden = data.isHidden(setting, n);
				if (!last && !isHidden && n.isLastNode) {
Z
ztree 已提交
262 263
					last = n;
					break;
264
				} else if (!last && !isHidden && !n.isLastNode) {
Z
ztree 已提交
265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294
					n.isLastNode = true;
					last = n;
					view.setNodeLineIcos(setting, n);
				} else if (last && n.isLastNode) {
					n.isLastNode = false;
					old = n;
					view.setNodeLineIcos(setting, n);
					break;
				} else {
					n = null;
				}
			}
			return {"new":last, "old":old};
		}
	},

	_z = {
		view: _view,
		data: _data
	};
	$.extend(true, $.fn.zTree._z, _z);

	var zt = $.fn.zTree,
	tools = zt._z.tools,
	consts = zt.consts,
	view = zt._z.view,
	data = zt._z.data,
	event = zt._z.event,
	$$ = tools.$;

295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310
  data.isHidden = function(setting, node, newIsHidden) {
    if (!node) {
      return false;
    }
    var key = setting.data.key.isHidden;
    if (typeof newIsHidden !== 'undefined') {
      if (typeof newIsHidden === "string") {
        newIsHidden = tools.eqs(checked, "true");
      }
      newIsHidden = !!newIsHidden;
      node[key] = newIsHidden;
    }
    return node[key];
  };

  data.exSetting(_setting);
Z
ztree 已提交
311 312 313 314 315 316
	data.addInitNode(_initNode);
	data.addBeforeA(_beforeA);
	data.addZTreeTools(_zTreeTools);

//	Override method in core
	var _dInitNode = data.initNode;
317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332
    data.initNode = function(setting, level, node, parentNode, isFirstNode, isLastNode, openFlag) {
        var tmpPNode = (parentNode) ? parentNode: data.getRoot(setting),
            children = tmpPNode[setting.data.key.children];
        data.tmpHideFirstNode = view.setFirstNodeForHide(setting, children);
        data.tmpHideLastNode = view.setLastNodeForHide(setting, children);
        if (openFlag) {
            view.setNodeLineIcos(setting, data.tmpHideFirstNode);
            view.setNodeLineIcos(setting, data.tmpHideLastNode);
        }
        isFirstNode = (data.tmpHideFirstNode === node);
        isLastNode = (data.tmpHideLastNode === node);
        if (_dInitNode) _dInitNode.apply(data, arguments);
        if (openFlag && isLastNode) {
            view.clearOldLastNode(setting, node, openFlag);
        }
    };
Z
ztree 已提交
333 334 335 336

	var _makeChkFlag = data.makeChkFlag;
	if (!!_makeChkFlag) {
		data.makeChkFlag = function(setting, node) {
337
			if (!!node && !!data.isHidden(setting, node)) {
Z
ztree 已提交
338 339 340 341 342 343 344 345 346 347 348
				return;
			}
			_makeChkFlag.apply(data, arguments);
		}
	}

	var _getTreeCheckedNodes = data.getTreeCheckedNodes;
	if (!!_getTreeCheckedNodes) {
		data.getTreeCheckedNodes = function(setting, nodes, checked, results) {
			if (!!nodes && nodes.length > 0) {
				var p = nodes[0].getParentNode();
349
				if (!!p && !!data.isHidden(setting, p)) {
Z
ztree 已提交
350 351 352 353 354 355 356 357 358 359 360 361
					return [];
				}
			}
			return _getTreeCheckedNodes.apply(data, arguments);
		}
	}

	var _getTreeChangeCheckedNodes = data.getTreeChangeCheckedNodes;
	if (!!_getTreeChangeCheckedNodes) {
		data.getTreeChangeCheckedNodes = function(setting, nodes, results) {
			if (!!nodes && nodes.length > 0) {
				var p = nodes[0].getParentNode();
362
				if (!!p && !!data.isHidden(setting, p)) {
Z
ztree 已提交
363 364 365 366 367 368 369 370 371 372
					return [];
				}
			}
			return _getTreeChangeCheckedNodes.apply(data, arguments);
		}
	}

	var _expandCollapseSonNode = view.expandCollapseSonNode;
	if (!!_expandCollapseSonNode) {
		view.expandCollapseSonNode = function(setting, node, expandFlag, animateFlag, callback) {
373
			if (!!node && !!data.isHidden(setting, node)) {
Z
ztree 已提交
374 375 376 377 378 379 380 381 382
				return;
			}
			_expandCollapseSonNode.apply(view, arguments);
		}
	}

	var _setSonNodeCheckBox = view.setSonNodeCheckBox;
	if (!!_setSonNodeCheckBox) {
		view.setSonNodeCheckBox = function(setting, node, value, srcNode) {
383
			if (!!node && !!data.isHidden(setting, node)) {
Z
ztree 已提交
384 385 386 387 388 389 390 391 392
				return;
			}
			_setSonNodeCheckBox.apply(view, arguments);
		}
	}

	var _repairParentChkClassWithSelf = view.repairParentChkClassWithSelf;
	if (!!_repairParentChkClassWithSelf) {
		view.repairParentChkClassWithSelf = function(setting, node) {
393
			if (!!node && !!data.isHidden(setting, node)) {
Z
ztree 已提交
394 395 396 397 398 399
				return;
			}
			_repairParentChkClassWithSelf.apply(view, arguments);
		}
	}
})(jQuery);