UNPKG

6.03 kBJavaScriptView Raw
1/* eslint-disable valid-jsdoc */
2export function normalizeToArray(values) {
3 if (values !== undefined && values !== null) {
4 if (Array.isArray(values)) {
5 return [].concat(values);
6 }
7
8 return [values];
9 }
10
11 return [];
12}
13
14/**
15 * 判断子节点是否是选中状态,如果 checkable={false} 则向下递归,
16 * @param {Node} child
17 * @param {Array} checkedValues
18 */
19export function isNodeChecked(node, checkedValues) {
20 if (node.disabled || node.checkboxDisabled) return true;
21 /* istanbul ignore next */
22 if (node.checkable === false) {
23 return !node.children || node.children.length === 0 || node.children.every(function (c) {
24 return isNodeChecked(c, checkedValues);
25 });
26 }
27 return checkedValues.indexOf(node.value) > -1;
28}
29
30/**
31 * 遍历所有可用的子节点
32 * @param {Node}
33 * @param {Function} callback
34 */
35export function forEachEnableNode(node) {
36 var callback = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : function () {};
37
38 if (node.disabled || node.checkboxDisabled) return;
39 // eslint-disable-next-line callback-return
40 callback(node);
41 if (node.children && node.children.length > 0) {
42 node.children.forEach(function (child) {
43 return forEachEnableNode(child, callback);
44 });
45 }
46}
47/**
48 * 判断节点是否禁用checked
49 * @param {Node} node
50 * @returns {Boolean}
51 */
52export function isNodeDisabledChecked(node) {
53 if (node.disabled || node.checkboxDisabled) return true;
54 /* istanbul ignore next */
55 if (node.checkable === false) {
56 return !node.children || node.children.length === 0 || node.children.every(isNodeDisabledChecked);
57 }
58
59 return false;
60}
61
62/**
63 * 递归获取一个 checkable = {true} 的父节点,当 checkable={false} 时继续往上查找
64 * @param {Node} node
65 * @param {Map} _p2n
66 * @return {Node}
67 */
68export function getCheckableParentNode(node, _p2n) {
69 var parentPos = node.pos.split(['-']);
70 if (parentPos.length === 2) return node;
71 parentPos.splice(parentPos.length - 1, 1);
72 parentPos = parentPos.join('-');
73 var parentNode = _p2n[parentPos];
74 if (parentNode.disabled || parentNode.checkboxDisabled) return false;
75 /* istanbul ignore next */
76 if (parentNode.checkable === false) {
77 return getCheckableParentNode(parentNode, _p2n);
78 }
79
80 return parentNode;
81}
82/**
83 * 过滤子节点
84 * @param {Array} values
85 * @param {Object} _v2n
86 */
87export function filterChildValue(values, _v2n, _p2n) {
88 var newValues = [];
89 values.forEach(function (value) {
90 var node = getCheckableParentNode(_v2n[value], _p2n);
91 if (!node || node.checkable === false || node === _v2n[value] || values.indexOf(node.value) === -1) {
92 newValues.push(value);
93 }
94 });
95 return newValues;
96}
97
98export function filterParentValue(values, _v2n) {
99 var newValues = [];
100
101 for (var i = 0; i < values.length; i++) {
102 var node = _v2n[values[i]];
103 if (!node.children || node.children.length === 0 || node.children.every(isNodeDisabledChecked)) {
104 newValues.push(values[i]);
105 }
106 }
107
108 return newValues;
109}
110
111export function isDescendantOrSelf(currentPos, targetPos) {
112 if (!currentPos || !targetPos) {
113 return false;
114 }
115
116 var currentNums = currentPos.split('-');
117 var targetNums = targetPos.split('-');
118
119 return currentNums.length <= targetNums.length && currentNums.every(function (num, index) {
120 return num === targetNums[index];
121 });
122}
123
124export function isSiblingOrSelf(currentPos, targetPos) {
125 var currentNums = currentPos.split('-').slice(0, -1);
126 var targetNums = targetPos.split('-').slice(0, -1);
127
128 return currentNums.length === targetNums.length && currentNums.every(function (num, index) {
129 return num === targetNums[index];
130 });
131}
132
133// eslint-disable-next-line max-statements
134export function getAllCheckedValues(checkedValues, _v2n, _p2n) {
135 checkedValues = normalizeToArray(checkedValues);
136 var filteredValues = checkedValues.filter(function (value) {
137 return !!_v2n[value];
138 });
139 var flatValues = [].concat(filterChildValue(filteredValues, _v2n, _p2n), filteredValues.filter(function (value) {
140 return _v2n[value].disabled || _v2n[value].checkboxDisabled;
141 }));
142 var removeValue = function removeValue(child) {
143 if (child.disabled || child.checkboxDisabled) return;
144 if (child.checkable === false && child.children && child.children.length > 0) {
145 return child.children.forEach(removeValue);
146 }
147 flatValues.splice(flatValues.indexOf(child.value), 1);
148 };
149
150 var addParentValue = function addParentValue(i, parent) {
151 return flatValues.splice(i, 0, parent.value);
152 };
153
154 var values = [].concat(flatValues);
155 for (var i = 0; i < values.length; i++) {
156 var pos = _v2n[values[i]].pos;
157 var nums = pos.split('-');
158 if (nums.length === 2) {
159 break;
160 }
161 for (var j = nums.length - 2; j > 0; j--) {
162 var parentPos = nums.slice(0, j + 1).join('-');
163 var parent = _p2n[parentPos];
164 if (parent.checkable === false || parent.disabled || parent.checkboxDisabled) continue;
165 var parentChecked = parent.children.every(function (child) {
166 return isNodeChecked(child, flatValues);
167 });
168 if (parentChecked) {
169 parent.children.forEach(removeValue);
170 addParentValue(i, parent);
171 } else {
172 break;
173 }
174 }
175 }
176
177 var newValues = [];
178 flatValues.forEach(function (value) {
179 if (_v2n[value].disabled || _v2n[value].checkboxDisabled) {
180 newValues.push(value);
181 return;
182 }
183 forEachEnableNode(_v2n[value], function (node) {
184 if (node.checkable === false) return;
185 newValues.push(node.value);
186 });
187 });
188
189 return newValues;
190}
\No newline at end of file