node.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538
  1. import {
  2. markNodeData,
  3. objectAssign,
  4. arrayFindIndex,
  5. getChildState,
  6. reInitChecked,
  7. getPropertyFromData,
  8. isNull,
  9. NODE_KEY
  10. } from '../tool/util';
  11. const getStore = function(store) {
  12. let thisStore = store;
  13. return function() {
  14. return thisStore;
  15. }
  16. }
  17. let nodeIdSeed = 0;
  18. export default class Node {
  19. constructor(options) {
  20. this.time = new Date().getTime();
  21. this.id = nodeIdSeed++;
  22. this.text = null;
  23. this.checked = false;
  24. this.indeterminate = false;
  25. this.data = null;
  26. this.expanded = false;
  27. this.parentId = null;
  28. this.visible = true;
  29. this.isCurrent = false;
  30. for (let name in options) {
  31. if (options.hasOwnProperty(name)) {
  32. if (name === 'store') {
  33. this.store = getStore(options[name]);
  34. } else {
  35. this[name] = options[name];
  36. }
  37. }
  38. }
  39. if (!this.store()) {
  40. throw new Error('[Node]store is required!');
  41. }
  42. // internal
  43. this.level = 0;
  44. this.loaded = false;
  45. this.childNodesId = [];
  46. this.loading = false;
  47. this.label = getPropertyFromData(this, 'label');
  48. this.key = this._getKey();
  49. this.disabled = getPropertyFromData(this, 'disabled');
  50. this.nextSibling = null;
  51. this.previousSibling = null;
  52. this.icon = '';
  53. this._handleParentAndLevel();
  54. this._handleProps();
  55. this._handleExpand();
  56. this._handleCurrent();
  57. if (this.store().lazy) {
  58. this.store()._initDefaultCheckedNode(this);
  59. }
  60. this.updateLeafState();
  61. }
  62. _getKey() {
  63. if (!this.data || Array.isArray(this.data)) return null;
  64. if (typeof this.data === 'object') {
  65. const nodeKey = this.store().key;
  66. const key = this.data[nodeKey];
  67. if (typeof key === 'undefined') {
  68. throw new Error(`您配置的node-key为"${nodeKey}",但数据中并未找到对应"${nodeKey}"属性的值,请检查node-key的配置是否合理`)
  69. }
  70. return key;
  71. }
  72. throw new Error('不合法的data数据');
  73. }
  74. _handleParentAndLevel() {
  75. if (this.parentId !== null) {
  76. let parent = this.getParent(this.parentId);
  77. if (this.store().isInjectParentInNode) {
  78. this.parent = parent;
  79. }
  80. // 由于这里做了修改,默认第一个对象不会被注册到nodesMap中,所以找不到parent会报错,所以默认parent的level是0
  81. if (!parent) {
  82. parent = {
  83. level: 0
  84. }
  85. } else {
  86. const parentChildNodes = parent.getChildNodes(parent.childNodesId);
  87. const index = parent.childNodesId.indexOf(this.key);
  88. this.nextSibling = index > -1 ? parentChildNodes[index + 1] : null;
  89. this.previousSibling = index > 0 ? parentChildNodes[index - 1] : null;
  90. }
  91. this.level = parent.level + 1;
  92. }
  93. }
  94. _handleProps() {
  95. const props = this.store().props;
  96. if (this.store().showNodeIcon) {
  97. if (props && typeof props.icon !== 'undefined') {
  98. this.icon = getPropertyFromData(this, 'icon');
  99. } else {
  100. console.warn('请配置props属性中的"icon"字段')
  101. }
  102. }
  103. this.store().registerNode(this);
  104. if (props && typeof props.isLeaf !== 'undefined') {
  105. const isLeaf = getPropertyFromData(this, 'isLeaf');
  106. if (typeof isLeaf === 'boolean') {
  107. this.isLeafByUser = isLeaf;
  108. }
  109. }
  110. }
  111. _handleExpand() {
  112. if (this.store().lazy !== true && this.data) {
  113. this.setData(this.data);
  114. if (this.store().defaultExpandAll) {
  115. this.expanded = true;
  116. }
  117. } else if (this.level > 0 && this.store().lazy && this.store().defaultExpandAll) {
  118. this.expand();
  119. }
  120. if (!Array.isArray(this.data)) {
  121. markNodeData(this, this.data);
  122. }
  123. if (!this.data) return;
  124. const defaultExpandedKeys = this.store().defaultExpandedKeys;
  125. const key = this.store().key;
  126. if (key && defaultExpandedKeys && defaultExpandedKeys.indexOf(this.key) !== -1) {
  127. this.expand(null, this.store().autoExpandparent);
  128. }
  129. }
  130. _handleCurrent() {
  131. const key = this.store().key;
  132. if (key && this.store().currentNodeKey !== undefined && this.key === this.store().currentNodeKey) {
  133. this.store().currentNode = this;
  134. this.store().currentNode.isCurrent = true;
  135. }
  136. }
  137. destroyStore() {
  138. getStore(null)
  139. }
  140. setData(data) {
  141. if (!Array.isArray(data)) {
  142. markNodeData(this, data);
  143. }
  144. this.data = data;
  145. this.childNodesId = [];
  146. let children;
  147. if (this.level === 0 && Array.isArray(this.data)) {
  148. children = this.data;
  149. } else {
  150. children = getPropertyFromData(this, 'children') || [];
  151. }
  152. for (let i = 0, j = children.length; i < j; i++) {
  153. this.insertChild({
  154. data: children[i]
  155. });
  156. }
  157. }
  158. contains(target, deep = true) {
  159. const walk = function(parent) {
  160. const children = parent.getChildNodes(parent.childNodesId) || [];
  161. let result = false;
  162. for (let i = 0, j = children.length; i < j; i++) {
  163. const child = children[i];
  164. if (child === target || (deep && walk(child))) {
  165. result = true;
  166. break;
  167. }
  168. }
  169. return result;
  170. };
  171. return walk(this);
  172. }
  173. remove() {
  174. if (this.parentId !== null) {
  175. const parent = this.getParent(this.parentId);
  176. parent.removeChild(this);
  177. }
  178. }
  179. insertChild(child, index, batch) {
  180. if (!child) throw new Error('insertChild error: child is required.');
  181. if (!(child instanceof Node)) {
  182. if (!batch) {
  183. const children = this.getChildren(true);
  184. if (children.indexOf(child.data) === -1) {
  185. if (typeof index === 'undefined' || index < 0) {
  186. children.push(child.data);
  187. } else {
  188. children.splice(index, 0, child.data);
  189. }
  190. }
  191. }
  192. objectAssign(child, {
  193. parentId: isNull(this.key) ? '' : this.key,
  194. store: this.store()
  195. });
  196. child = new Node(child);
  197. }
  198. child.level = this.level + 1;
  199. if (typeof index === 'undefined' || index < 0) {
  200. this.childNodesId.push(child.key);
  201. } else {
  202. this.childNodesId.splice(index, 0, child.key);
  203. }
  204. this.updateLeafState();
  205. }
  206. insertBefore(child, ref) {
  207. let index;
  208. if (ref) {
  209. index = this.childNodesId.indexOf(ref.id);
  210. }
  211. this.insertChild(child, index);
  212. }
  213. insertAfter(child, ref) {
  214. let index;
  215. if (ref) {
  216. index = this.childNodesId.indexOf(ref.id);
  217. if (index !== -1) index += 1;
  218. }
  219. this.insertChild(child, index);
  220. }
  221. removeChild(child) {
  222. const children = this.getChildren() || [];
  223. const dataIndex = children.indexOf(child.data);
  224. if (dataIndex > -1) {
  225. children.splice(dataIndex, 1);
  226. }
  227. const index = this.childNodesId.indexOf(child.key);
  228. if (index > -1) {
  229. this.store() && this.store().deregisterNode(child);
  230. child.parentId = null;
  231. this.childNodesId.splice(index, 1);
  232. }
  233. this.updateLeafState();
  234. }
  235. removeChildByData(data) {
  236. let targetNode = null;
  237. for (let i = 0; i < this.childNodesId.length; i++) {
  238. let node = this.getChildNodes(this.childNodesId);
  239. if (node[i].data === data) {
  240. targetNode = node[i];
  241. break;
  242. }
  243. }
  244. if (targetNode) {
  245. this.removeChild(targetNode);
  246. }
  247. }
  248. // 为了避免APP端parent嵌套结构导致报错,这里parent需要从nodesMap中获取
  249. getParent(parentId) {
  250. try {
  251. if (!parentId.toString()) return null;
  252. return this.store().nodesMap[parentId];
  253. } catch (error) {
  254. return null;
  255. }
  256. }
  257. // 为了避免APP端childNodes嵌套结构导致报错,这里childNodes需要从nodesMap中获取
  258. getChildNodes(childNodesId) {
  259. let childNodes = [];
  260. if (childNodesId.length === 0) return childNodes;
  261. childNodesId.forEach((key) => {
  262. childNodes.push(this.store().nodesMap[key]);
  263. })
  264. return childNodes;
  265. }
  266. expand(callback, expandparent) {
  267. const done = () => {
  268. if (expandparent) {
  269. let parent = this.getParent(this.parentId);
  270. while (parent && parent.level > 0) {
  271. parent.expanded = true;
  272. parent = this.getParent(parent.parentId);
  273. }
  274. }
  275. this.expanded = true;
  276. if (callback) callback();
  277. };
  278. if (this.shouldLoadData()) {
  279. this.loadData(function(data) {
  280. if (Array.isArray(data)) {
  281. if (this.checked) {
  282. this.setChecked(true, true);
  283. } else if (!this.store().checkStrictly) {
  284. reInitChecked(this);
  285. }
  286. done();
  287. }
  288. });
  289. } else {
  290. done();
  291. }
  292. }
  293. doCreateChildren(array, defaultProps = {}) {
  294. array.forEach((item) => {
  295. this.insertChild(objectAssign({
  296. data: item
  297. }, defaultProps), undefined, true);
  298. });
  299. }
  300. collapse() {
  301. this.expanded = false;
  302. }
  303. shouldLoadData() {
  304. return this.store().lazy === true && this.store().load && !this.loaded;
  305. }
  306. updateLeafState() {
  307. if (this.store().lazy === true && this.loaded !== true && typeof this.isLeafByUser !== 'undefined') {
  308. this.isLeaf = this.isLeafByUser;
  309. return;
  310. }
  311. const childNodesId = this.childNodesId;
  312. if (!this.store().lazy || (this.store().lazy === true && this.loaded === true)) {
  313. this.isLeaf = !childNodesId || childNodesId.length === 0;
  314. return;
  315. }
  316. this.isLeaf = false;
  317. }
  318. setChecked(value, deep, recursion, passValue) {
  319. this.indeterminate = value === 'half';
  320. this.checked = value === true;
  321. if (this.checked && this.store().expandOnCheckNode) {
  322. this.expand(null, true)
  323. }
  324. if (this.store().checkStrictly) return;
  325. if (this.store().showRadio) return;
  326. if (!(this.shouldLoadData() && !this.store().checkDescendants)) {
  327. let childNodes = this.getChildNodes(this.childNodesId);
  328. let {
  329. all,
  330. allWithoutDisable
  331. } = getChildState(childNodes);
  332. if (!this.isLeaf && (!all && allWithoutDisable)) {
  333. this.checked = false;
  334. value = false;
  335. }
  336. const handleDescendants = () => {
  337. if (deep) {
  338. let childNodes = this.getChildNodes(this.childNodesId)
  339. for (let i = 0, j = childNodes.length; i < j; i++) {
  340. const child = childNodes[i];
  341. passValue = passValue || value !== false;
  342. const isCheck = child.disabled ? child.checked : passValue;
  343. child.setChecked(isCheck, deep, true, passValue);
  344. }
  345. const {
  346. half,
  347. all
  348. } = getChildState(childNodes);
  349. if (!all) {
  350. this.checked = all;
  351. this.indeterminate = half;
  352. }
  353. }
  354. };
  355. if (this.shouldLoadData()) {
  356. this.loadData(() => {
  357. handleDescendants();
  358. reInitChecked(this);
  359. }, {
  360. checked: value !== false
  361. });
  362. return;
  363. } else {
  364. handleDescendants();
  365. }
  366. }
  367. if (!this.parentId) return;
  368. let parent = this.getParent(this.parentId);
  369. if (parent && parent.level === 0) return;
  370. if (!recursion) {
  371. reInitChecked(parent);
  372. }
  373. }
  374. setRadioChecked(value) {
  375. const allNodes = this.store()._getAllNodes().sort((a, b) => b.level - a.level);
  376. allNodes.forEach(node => node.setChecked(false, false));
  377. this.checked = value === true;
  378. }
  379. getChildren(forceInit = false) {
  380. if (this.level === 0) return this.data;
  381. const data = this.data;
  382. if (!data) return null;
  383. const props = this.store().props;
  384. let children = 'children';
  385. if (props) {
  386. children = props.children || 'children';
  387. }
  388. if (data[children] === undefined) {
  389. data[children] = null;
  390. }
  391. if (forceInit && !data[children]) {
  392. data[children] = [];
  393. }
  394. return data[children];
  395. }
  396. updateChildren() {
  397. let childNodes = this.getChildNodes(this.childNodesId);
  398. const newData = this.getChildren() || [];
  399. const oldData = childNodes.map((node) => node.data);
  400. const newDataMap = {};
  401. const newNodes = [];
  402. newData.forEach((item, index) => {
  403. const key = item[NODE_KEY];
  404. const isNodeExists = !!key && arrayFindIndex(oldData, data => data[NODE_KEY] === key) >= 0;
  405. if (isNodeExists) {
  406. newDataMap[key] = {
  407. index,
  408. data: item
  409. };
  410. } else {
  411. newNodes.push({
  412. index,
  413. data: item
  414. });
  415. }
  416. });
  417. if (!this.store().lazy) {
  418. oldData.forEach((item) => {
  419. if (!newDataMap[item[NODE_KEY]]) this.removeChildByData(item);
  420. });
  421. }
  422. newNodes.forEach(({
  423. index,
  424. data
  425. }) => {
  426. this.insertChild({
  427. data
  428. }, index);
  429. });
  430. this.updateLeafState();
  431. }
  432. loadData(callback, defaultProps = {}) {
  433. if (this.store().lazy === true &&
  434. this.store().load && !this.loaded &&
  435. (!this.loading || Object.keys(defaultProps).length)
  436. ) {
  437. this.loading = true;
  438. const resolve = (children) => {
  439. this.loaded = true;
  440. this.loading = false;
  441. this.childNodesId = [];
  442. this.doCreateChildren(children, defaultProps);
  443. this.updateLeafState();
  444. callback && callback.call(this,children);
  445. };
  446. this.store().load(this, resolve);
  447. } else {
  448. callback && callback.call(this);
  449. }
  450. }
  451. }