1 |
|
2 | export 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 |
|
16 |
|
17 |
|
18 |
|
19 | export function isNodeChecked(node, checkedValues) {
|
20 | if (node.disabled || node.checkboxDisabled) return true;
|
21 |
|
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 |
|
33 |
|
34 |
|
35 | export function forEachEnableNode(node) {
|
36 | var callback = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : function () {};
|
37 |
|
38 | if (node.disabled || node.checkboxDisabled) return;
|
39 |
|
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 |
|
49 |
|
50 |
|
51 |
|
52 | export function isNodeDisabledChecked(node) {
|
53 | if (node.disabled || node.checkboxDisabled) return true;
|
54 |
|
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 |
|
64 |
|
65 |
|
66 |
|
67 |
|
68 | export 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 |
|
76 | if (parentNode.checkable === false) {
|
77 | return getCheckableParentNode(parentNode, _p2n);
|
78 | }
|
79 |
|
80 | return parentNode;
|
81 | }
|
82 |
|
83 |
|
84 |
|
85 |
|
86 |
|
87 | export 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 |
|
98 | export 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 |
|
111 | export 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 |
|
124 | export 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 |
|
134 | export 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 |