node.js 11 KB

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