node.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485
  1. import objectAssign from 'element-ui/src/utils/merge';
  2. import { markNodeData, NODE_KEY } from './util';
  3. import { arrayFindIndex } from 'element-ui/src/utils/util';
  4. export const getChildState = node => {
  5. let all = true;
  6. let none = true;
  7. let allWithoutDisable = true;
  8. for (let i = 0, j = node.length; i < j; i++) {
  9. const n = node[i];
  10. if (n.checked !== true || n.indeterminate) {
  11. all = false;
  12. if (!n.disabled) {
  13. allWithoutDisable = false;
  14. }
  15. }
  16. if (n.checked !== false || n.indeterminate) {
  17. none = false;
  18. }
  19. }
  20. return { all, none, allWithoutDisable, half: !all && !none };
  21. };
  22. const reInitChecked = function(node) {
  23. if (node.childNodes.length === 0) return;
  24. const {all, none, half} = getChildState(node.childNodes);
  25. if (all) {
  26. node.checked = true;
  27. node.indeterminate = false;
  28. } else if (half) {
  29. node.checked = false;
  30. node.indeterminate = true;
  31. } else if (none) {
  32. node.checked = false;
  33. node.indeterminate = false;
  34. }
  35. const parent = node.parent;
  36. if (!parent || parent.level === 0) return;
  37. if (!node.store.checkStrictly) {
  38. reInitChecked(parent);
  39. }
  40. };
  41. const getPropertyFromData = function(node, prop) {
  42. const props = node.store.props;
  43. const data = node.data || {};
  44. const config = props[prop];
  45. if (typeof config === 'function') {
  46. return config(data, node);
  47. } else if (typeof config === 'string') {
  48. return data[config];
  49. } else if (typeof config === 'undefined') {
  50. const dataProp = data[prop];
  51. return dataProp === undefined ? '' : dataProp;
  52. }
  53. };
  54. let nodeIdSeed = 0;
  55. export default class Node {
  56. constructor(options) {
  57. this.id = nodeIdSeed++;
  58. this.text = null;
  59. this.checked = false;
  60. this.indeterminate = false;
  61. this.data = null;
  62. this.expanded = false;
  63. this.parent = null;
  64. this.visible = true;
  65. this.isCurrent = false;
  66. for (let name in options) {
  67. if (options.hasOwnProperty(name)) {
  68. this[name] = options[name];
  69. }
  70. }
  71. // internal
  72. this.level = 0;
  73. this.loaded = false;
  74. this.childNodes = [];
  75. this.loading = false;
  76. if (this.parent) {
  77. this.level = this.parent.level + 1;
  78. }
  79. const store = this.store;
  80. if (!store) {
  81. throw new Error('[Node]store is required!');
  82. }
  83. store.registerNode(this);
  84. const props = store.props;
  85. if (props && typeof props.isLeaf !== 'undefined') {
  86. const isLeaf = getPropertyFromData(this, 'isLeaf');
  87. if (typeof isLeaf === 'boolean') {
  88. this.isLeafByUser = isLeaf;
  89. }
  90. }
  91. if (store.lazy !== true && this.data) {
  92. this.setData(this.data);
  93. if (store.defaultExpandAll) {
  94. this.expanded = true;
  95. }
  96. } else if (this.level > 0 && store.lazy && store.defaultExpandAll) {
  97. this.expand();
  98. }
  99. if (!Array.isArray(this.data)) {
  100. markNodeData(this, this.data);
  101. }
  102. if (!this.data) return;
  103. const defaultExpandedKeys = store.defaultExpandedKeys;
  104. const key = store.key;
  105. if (key && defaultExpandedKeys && defaultExpandedKeys.indexOf(this.key) !== -1) {
  106. this.expand(null, store.autoExpandParent);
  107. }
  108. if (key && store.currentNodeKey !== undefined && this.key === store.currentNodeKey) {
  109. store.currentNode = this;
  110. store.currentNode.isCurrent = true;
  111. }
  112. if (store.lazy) {
  113. store._initDefaultCheckedNode(this);
  114. }
  115. this.updateLeafState();
  116. }
  117. setData(data) {
  118. if (!Array.isArray(data)) {
  119. markNodeData(this, data);
  120. }
  121. this.data = data;
  122. this.childNodes = [];
  123. let children;
  124. if (this.level === 0 && this.data instanceof Array) {
  125. children = this.data;
  126. } else {
  127. children = getPropertyFromData(this, 'children') || [];
  128. }
  129. for (let i = 0, j = children.length; i < j; i++) {
  130. this.insertChild({ data: children[i] });
  131. }
  132. }
  133. get label() {
  134. return getPropertyFromData(this, 'label');
  135. }
  136. get key() {
  137. const nodeKey = this.store.key;
  138. if (this.data) return this.data[nodeKey];
  139. return null;
  140. }
  141. get disabled() {
  142. return getPropertyFromData(this, 'disabled');
  143. }
  144. get nextSibling() {
  145. const parent = this.parent;
  146. if (parent) {
  147. const index = parent.childNodes.indexOf(this);
  148. if (index > -1) {
  149. return parent.childNodes[index + 1];
  150. }
  151. }
  152. return null;
  153. }
  154. get previousSibling() {
  155. const parent = this.parent;
  156. if (parent) {
  157. const index = parent.childNodes.indexOf(this);
  158. if (index > -1) {
  159. return index > 0 ? parent.childNodes[index - 1] : null;
  160. }
  161. }
  162. return null;
  163. }
  164. contains(target, deep = true) {
  165. const walk = function(parent) {
  166. const children = parent.childNodes || [];
  167. let result = false;
  168. for (let i = 0, j = children.length; i < j; i++) {
  169. const child = children[i];
  170. if (child === target || (deep && walk(child))) {
  171. result = true;
  172. break;
  173. }
  174. }
  175. return result;
  176. };
  177. return walk(this);
  178. }
  179. remove() {
  180. const parent = this.parent;
  181. if (parent) {
  182. parent.removeChild(this);
  183. }
  184. }
  185. insertChild(child, index, batch) {
  186. if (!child) throw new Error('insertChild error: child is required.');
  187. if (!(child instanceof Node)) {
  188. if (!batch) {
  189. const children = this.getChildren(true);
  190. if (children.indexOf(child.data) === -1) {
  191. if (typeof index === 'undefined' || index < 0) {
  192. children.push(child.data);
  193. } else {
  194. children.splice(index, 0, child.data);
  195. }
  196. }
  197. }
  198. objectAssign(child, {
  199. parent: this,
  200. store: this.store
  201. });
  202. child = new Node(child);
  203. }
  204. child.level = this.level + 1;
  205. if (typeof index === 'undefined' || index < 0) {
  206. this.childNodes.push(child);
  207. } else {
  208. this.childNodes.splice(index, 0, child);
  209. }
  210. this.updateLeafState();
  211. }
  212. insertBefore(child, ref) {
  213. let index;
  214. if (ref) {
  215. index = this.childNodes.indexOf(ref);
  216. }
  217. this.insertChild(child, index);
  218. }
  219. insertAfter(child, ref) {
  220. let index;
  221. if (ref) {
  222. index = this.childNodes.indexOf(ref);
  223. if (index !== -1) index += 1;
  224. }
  225. this.insertChild(child, index);
  226. }
  227. removeChild(child) {
  228. const children = this.getChildren() || [];
  229. const dataIndex = children.indexOf(child.data);
  230. if (dataIndex > -1) {
  231. children.splice(dataIndex, 1);
  232. }
  233. const index = this.childNodes.indexOf(child);
  234. if (index > -1) {
  235. this.store && this.store.deregisterNode(child);
  236. child.parent = null;
  237. this.childNodes.splice(index, 1);
  238. }
  239. this.updateLeafState();
  240. }
  241. removeChildByData(data) {
  242. let targetNode = null;
  243. for (let i = 0; i < this.childNodes.length; i++) {
  244. if (this.childNodes[i].data === data) {
  245. targetNode = this.childNodes[i];
  246. break;
  247. }
  248. }
  249. if (targetNode) {
  250. this.removeChild(targetNode);
  251. }
  252. }
  253. expand(callback, expandParent) {
  254. const done = () => {
  255. if (expandParent) {
  256. let parent = this.parent;
  257. while (parent.level > 0) {
  258. parent.expanded = true;
  259. parent = parent.parent;
  260. }
  261. }
  262. this.expanded = true;
  263. if (callback) callback();
  264. };
  265. if (this.shouldLoadData()) {
  266. this.loadData((data) => {
  267. if (data instanceof Array) {
  268. if (this.checked) {
  269. this.setChecked(true, true);
  270. } else if (!this.store.checkStrictly) {
  271. reInitChecked(this);
  272. }
  273. done();
  274. }
  275. });
  276. } else {
  277. done();
  278. }
  279. }
  280. doCreateChildren(array, defaultProps = {}) {
  281. array.forEach((item) => {
  282. this.insertChild(objectAssign({ data: item }, defaultProps), undefined, true);
  283. });
  284. }
  285. collapse() {
  286. this.expanded = false;
  287. }
  288. shouldLoadData() {
  289. return this.store.lazy === true && this.store.load && !this.loaded;
  290. }
  291. updateLeafState() {
  292. if (this.store.lazy === true && this.loaded !== true && typeof this.isLeafByUser !== 'undefined') {
  293. this.isLeaf = this.isLeafByUser;
  294. return;
  295. }
  296. const childNodes = this.childNodes;
  297. if (!this.store.lazy || (this.store.lazy === true && this.loaded === true)) {
  298. this.isLeaf = !childNodes || childNodes.length === 0;
  299. return;
  300. }
  301. this.isLeaf = false;
  302. }
  303. setChecked(value, deep, recursion, passValue) {
  304. this.indeterminate = value === 'half';
  305. this.checked = value === true;
  306. if (this.store.checkStrictly) return;
  307. if (!(this.shouldLoadData() && !this.store.checkDescendants)) {
  308. let { all, allWithoutDisable } = getChildState(this.childNodes);
  309. if (!this.isLeaf && (!all && allWithoutDisable)) {
  310. this.checked = false;
  311. value = false;
  312. }
  313. const handleDescendants = () => {
  314. if (deep) {
  315. const childNodes = this.childNodes;
  316. for (let i = 0, j = childNodes.length; i < j; i++) {
  317. const child = childNodes[i];
  318. passValue = passValue || value !== false;
  319. const isCheck = child.disabled ? child.checked : passValue;
  320. child.setChecked(isCheck, deep, true, passValue);
  321. }
  322. const { half, all } = getChildState(childNodes);
  323. if (!all) {
  324. this.checked = all;
  325. this.indeterminate = half;
  326. }
  327. }
  328. };
  329. if (this.shouldLoadData()) {
  330. // Only work on lazy load data.
  331. this.loadData(() => {
  332. handleDescendants();
  333. reInitChecked(this);
  334. }, {
  335. checked: value !== false
  336. });
  337. return;
  338. } else {
  339. handleDescendants();
  340. }
  341. }
  342. const parent = this.parent;
  343. if (!parent || parent.level === 0) return;
  344. if (!recursion) {
  345. reInitChecked(parent);
  346. }
  347. }
  348. getChildren(forceInit = false) { // this is data
  349. if (this.level === 0) return this.data;
  350. const data = this.data;
  351. if (!data) return null;
  352. const props = this.store.props;
  353. let children = 'children';
  354. if (props) {
  355. children = props.children || 'children';
  356. }
  357. if (data[children] === undefined) {
  358. data[children] = null;
  359. }
  360. if (forceInit && !data[children]) {
  361. data[children] = [];
  362. }
  363. return data[children];
  364. }
  365. updateChildren() {
  366. const newData = this.getChildren() || [];
  367. const oldData = this.childNodes.map((node) => node.data);
  368. const newDataMap = {};
  369. const newNodes = [];
  370. newData.forEach((item, index) => {
  371. const key = item[NODE_KEY];
  372. const isNodeExists = !!key && arrayFindIndex(oldData, data => data[NODE_KEY] === key) >= 0;
  373. if (isNodeExists) {
  374. newDataMap[key] = { index, data: item };
  375. } else {
  376. newNodes.push({ index, data: item });
  377. }
  378. });
  379. if (!this.store.lazy) {
  380. oldData.forEach((item) => {
  381. if (!newDataMap[item[NODE_KEY]]) this.removeChildByData(item);
  382. });
  383. }
  384. newNodes.forEach(({ index, data }) => {
  385. this.insertChild({ data }, index);
  386. });
  387. this.updateLeafState();
  388. }
  389. loadData(callback, defaultProps = {}) {
  390. if (this.store.lazy === true && this.store.load && !this.loaded && (!this.loading || Object.keys(defaultProps).length)) {
  391. this.loading = true;
  392. const resolve = (children) => {
  393. this.loaded = true;
  394. this.loading = false;
  395. this.childNodes = [];
  396. this.doCreateChildren(children, defaultProps);
  397. this.updateLeafState();
  398. if (callback) {
  399. callback.call(this, children);
  400. }
  401. };
  402. this.store.load(this, resolve);
  403. } else {
  404. if (callback) {
  405. callback.call(this);
  406. }
  407. }
  408. }
  409. }