drag_tree.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454
  1. const createDragTree = function (setting) {
  2. class DragTree {
  3. /**
  4. * 构造函数
  5. */
  6. constructor(setting) {
  7. // 无索引
  8. this.datas = [];
  9. // 以key为索引indexedDB
  10. this.items = {};
  11. // 以排序为索引
  12. this.nodes = [];
  13. // 根节点
  14. this.children = [];
  15. // 树设置
  16. this.setting = setting;
  17. }
  18. /**
  19. * 树结构根据显示排序
  20. */
  21. sortTreeNode(isResort) {
  22. const self = this;
  23. const addSortNodes = function (nodes) {
  24. if (!nodes) { return }
  25. for (let i = 0; i < nodes.length; i++) {
  26. self.nodes.push(nodes[i]);
  27. nodes[i].index = self.nodes.length - 1;
  28. if (!isResort) {
  29. nodes[i].children = self.getChildren(nodes[i]);
  30. } else {
  31. nodes[i].children.sort((a, b) => { return a[self.setting.order] - b[self.setting.order]; })
  32. }
  33. addSortNodes(nodes[i].children);
  34. }
  35. };
  36. this.nodes = [];
  37. if (!isResort) {
  38. this.children = this.getChildren();
  39. } else {
  40. this.children.sort((a, b) => { return a[self.setting.order] - b[self.setting.order]; });
  41. }
  42. addSortNodes(this.children);
  43. }
  44. /**
  45. * 加载数据(初始化), 并给数据添加部分树结构必须数据
  46. * @param datas
  47. */
  48. loadDatas(datas) {
  49. const self = this;
  50. // 清空旧数据
  51. this.items = {};
  52. this.nodes = [];
  53. this.datas = [];
  54. this.children = [];
  55. // 加载全部数据
  56. datas.sort(function (a, b) {
  57. return a[self.setting.level] - b[self.setting.level];
  58. });
  59. for (const data of datas) {
  60. const keyName = itemsPre + data[this.setting.id];
  61. if (this.items[keyName]) continue;
  62. const item = JSON.parse(JSON.stringify(data));
  63. item.children = [];
  64. item.expanded = true;
  65. item.visible = true;
  66. if (item[setting.pid] === setting.rootId) {
  67. this.children.push(item);
  68. } else {
  69. const parent = this.getParent(item);
  70. if (!parent) continue;
  71. parent.children.push(item);
  72. }
  73. this.items[keyName] = item;
  74. this.datas.push(item);
  75. }
  76. this.children.sort((a, b) => { return a[self.setting.order] - b[self.setting.order]; });
  77. this.sortTreeNode(true);
  78. }
  79. getItemsByIndex(index) {
  80. return this.nodes[index];
  81. }
  82. /**
  83. * 根据id获取树结构节点数据
  84. * @param {Number} id
  85. * @returns {Object}
  86. */
  87. getItems(id) {
  88. return this.items[itemsPre + id];
  89. };
  90. getNodeIndex(node) {
  91. return this.nodes.indexOf(node);
  92. }
  93. /**
  94. * 查找node的parent
  95. * @param {Object} node
  96. * @returns {Object}
  97. */
  98. getParent(node) {
  99. return this.getItems(node[this.setting.pid]);
  100. };
  101. getTopParent(node) {
  102. const parents = this.getAllParents(node);
  103. parents.sort((a, b) => { return a.level - b.level; });
  104. return parents[0];
  105. };
  106. getAllParents(node) {
  107. const parents = [];
  108. if (!node) return parents;
  109. let vP = this.getParent(node);
  110. while (vP) {
  111. parents.push(vP);
  112. vP = this.getParent(vP);
  113. }
  114. return parents;
  115. }
  116. /**
  117. * 查找node的前兄弟节点
  118. * @param node
  119. * @returns {*}
  120. */
  121. getPreSiblingNode(node) {
  122. if (!node) return null;
  123. const parent = this.getParent(node);
  124. const siblings = parent ? parent.children : this.children;
  125. const index = siblings.indexOf(node);
  126. return (index > 0) ? siblings[index - 1] : null;
  127. }
  128. /**
  129. * 查找node的后兄弟节点
  130. * @param node
  131. * @returns {*}
  132. */
  133. getNextSiblingNode(node) {
  134. const parent = this.getParent(node);
  135. const siblings = parent ? parent.children : this.children;
  136. const index = siblings.indexOf(node);
  137. if (index >= 0 && index < siblings.length - 1) {
  138. return siblings[index + 1];
  139. } else {
  140. return null;
  141. }
  142. }
  143. /**
  144. * 查询node的已下载子节点
  145. * @param {Object} node
  146. * @returns {Array}
  147. */
  148. getChildren(node) {
  149. const setting = this.setting;
  150. const pid = node ? node[setting.id] : setting.rootId;
  151. const children = this.datas.filter(function (x) {
  152. return x[setting.pid] === pid;
  153. });
  154. children.sort((a, b) => { return a[setting.order] - b[setting.order]; });
  155. return children;
  156. };
  157. /**
  158. * 递归方式 查询node的已下载的全部后代 (兼容full_path不存在的情况)
  159. * @param node
  160. * @returns {*}
  161. * @private
  162. */
  163. _recursiveGetPosterity(node) {
  164. let posterity = node.children;
  165. for (const c of node.children) {
  166. posterity = posterity.concat(this._recursiveGetPosterity(c));
  167. }
  168. return posterity;
  169. };
  170. /**
  171. * 查询node的已下载的全部后代
  172. * @param {Object} node
  173. * @returns {Array}
  174. */
  175. getPosterity(node) {
  176. const self = this;
  177. const posterity = this._recursiveGetPosterity(node);
  178. posterity.sort(function (x, y) {
  179. return self.getNodeIndex(x) - self.getNodeIndex(y);
  180. });
  181. return posterity;
  182. };
  183. /**
  184. * 查询node是否是父节点的最后一个子节点
  185. * @param {Object} node
  186. * @returns {boolean}
  187. */
  188. isLastSibling(node) {
  189. const siblings = this.getChildren(this.getParent(node));
  190. return (siblings && siblings.length > 0) ? node[this.setting.order] === siblings[siblings.length - 1][this.setting.order] : false;
  191. };
  192. /**
  193. * 查询node是否是父节点的最后一个可见子节点
  194. * @param {Object} node
  195. * @returns {boolean}
  196. */
  197. isLastViewSibling(node) {
  198. const siblings = (this.getChildren(this.getParent(node))).filter(x => { return !x.filter });
  199. return (siblings && siblings.length > 0) ? node.order === siblings[siblings.length - 1].order : false;
  200. };
  201. /**
  202. * 得到树结构构成id
  203. * @param node
  204. * @returns {*}
  205. */
  206. getNodeKey(node) {
  207. return node[this.setting.id];
  208. };
  209. /**
  210. * 刷新子节点是否可见
  211. * @param {Object} node
  212. * @private
  213. */
  214. _refreshChildrenVisible(node) {
  215. if (!node.children) {
  216. node.children = this.getChildren(node);
  217. }
  218. if (node.children && node.children.length > 0) {
  219. for (const child of node.children) {
  220. child.visible = node.expanded && node.visible && !child.filter;
  221. this._refreshChildrenVisible(child);
  222. }
  223. }
  224. };
  225. /**
  226. * 设置节点是否展开, 并控制子节点可见
  227. * @param {Object} node
  228. * @param {Boolean} expanded
  229. */
  230. setExpanded(node, expanded) {
  231. node.expanded = expanded;
  232. this._refreshChildrenVisible(node);
  233. };
  234. /**
  235. * 递归 设置节点展开状态
  236. * @param {Array} nodes - 需要设置状态的节点
  237. * @param {Object} parent - nodes的父节点
  238. * @param {Function} checkFun - 判断节点展开状态的方法
  239. * @private
  240. */
  241. _recursiveExpand(nodes, parent, checkFun) {
  242. for (const node of nodes) {
  243. const expanded = checkFun(node);
  244. if (node.expanded !== expanded) {
  245. node.expanded = expanded;
  246. }
  247. node.visible = parent ? (parent.expanded && parent.visible && !node.filter) : !node.filter;
  248. this._recursiveExpand(node.children, node, checkFun);
  249. }
  250. }
  251. /**
  252. * 自定义展开规则
  253. * @param checkFun
  254. */
  255. expandByCustom(checkFun) {
  256. this._recursiveExpand(this.children, null, checkFun);
  257. this._saveMarkExpandFold();
  258. }
  259. /**
  260. * 展开到第几层
  261. * @param {Number} level - 展开层数
  262. */
  263. expandByLevel(level) {
  264. this.expandByCustom(function (n) {
  265. return n.level < level;
  266. });
  267. }
  268. /**
  269. * 自动展开节点node
  270. * @param node
  271. * @returns {*}
  272. */
  273. autoExpandNode(node) {
  274. const parents = this.getAllParents(node);
  275. const reload = [];
  276. for (const p of parents) {
  277. if (!p.expanded) {
  278. reload.push(p);
  279. this.setExpanded(p, true);
  280. }
  281. }
  282. return reload;
  283. }
  284. /**
  285. * 加载数据(动态),只加载不同部分
  286. * @param {Array} datas
  287. * @return {Array} 加载到树的数据
  288. * @privateA
  289. */
  290. _updateData(datas) {
  291. datas = datas instanceof Array ? datas : [datas];
  292. let loadedData = [];
  293. for (const data of datas) {
  294. let node = this.getItems(data[this.setting.id]);
  295. if (node) {
  296. for (const prop in data) {
  297. if (data[prop] !== undefined && data[prop] !== node[prop]) {
  298. if (prop === this.setting.pid) {
  299. loadedData.push(this.getItems(node[this.setting.pid]));
  300. loadedData.push(this.getItems(data[this.setting.pid]));
  301. }
  302. if (prop === this.setting.order) {
  303. loadedData = loadedData.concat(this.getPosterity(node));
  304. }
  305. node[prop] = data[prop];
  306. }
  307. }
  308. loadedData.push(node);
  309. }
  310. }
  311. loadedData = _.uniq(loadedData);
  312. for (const node of loadedData) {
  313. if (node) {
  314. node.children = this.getChildren(node);
  315. node.expanded = node.children.length === 0 ? true : node.children[0].visible;
  316. } else {
  317. this.children = this.getChildren(null);
  318. }
  319. }
  320. this.sortTreeNode(true);
  321. return loadedData;
  322. };
  323. /**
  324. * 加载数据(动态),只加载不同部分
  325. * @param {Array} datas
  326. * @return {Array} 加载到树的数据
  327. * @privateA
  328. */
  329. _loadData(datas) {
  330. datas = datas instanceof Array ? datas : [datas];
  331. const loadedData = [], resortData = [];
  332. for (const data of datas) {
  333. let node = this.getItems(data[this.setting.id]);
  334. if (node) {
  335. const parent = this.getItems(node[this.setting.pid]);
  336. for (const prop in data) {
  337. if (data[prop] !== undefined && data[prop] !== node[prop]) {
  338. node[prop] = data[prop];
  339. if (parent && resortData.indexOf(parent) === -1) {
  340. resortData.push(parent);
  341. }
  342. }
  343. }
  344. loadedData.push(node);
  345. } else {
  346. const keyName = itemsPre + data[this.setting.id];
  347. const node = JSON.parse(JSON.stringify(data));
  348. this.items[keyName] = node;
  349. this.datas.push(node);
  350. node.expanded = true;
  351. node.visible = true;
  352. loadedData.push(node);
  353. if (resortData.indexOf(node) === -1) {
  354. resortData.push(node);
  355. }
  356. const parent = this.getItems(node[this.setting.pid]);
  357. if (parent && resortData.indexOf(parent) === -1) {
  358. resortData.push(parent);
  359. } else {
  360. resortData.push(this.setting.rootId);
  361. }
  362. }
  363. }
  364. for (const node of resortData) {
  365. if (node && node !== this.setting.rootId) {
  366. node.children = this.getChildren(node);
  367. } else {
  368. this.children = this.getChildren(null);
  369. }
  370. }
  371. this.sortTreeNode(true);
  372. for (const node of loadedData) {
  373. if (!node.expanded) {
  374. this.setExpanded(node, true);
  375. }
  376. }
  377. return loadedData;
  378. };
  379. /**
  380. * 清理数据(动态)
  381. * @param datas
  382. * @private
  383. */
  384. _freeData(datas) {
  385. datas = datas instanceof Array ? datas : [datas];
  386. const freeDatas = [];
  387. const removeArrayData = function (array, data) {
  388. const index = array.indexOf(data);
  389. array.splice(index, 1);
  390. };
  391. for (const data of datas) {
  392. const node = this.getItems(data);
  393. if (node) {
  394. freeDatas.push(node);
  395. node.deleteIndex = this.nodes.indexOf(node);
  396. delete this.items[itemsPre + node[this.setting.id]];
  397. if (node[this.setting.pid] !== this.setting.rootId) {
  398. const parent = this.getItems(node[this.setting.pid]);
  399. if (parent) {
  400. removeArrayData(parent.children, node);
  401. }
  402. } else {
  403. removeArrayData(this.children, node);
  404. }
  405. removeArrayData(this.datas, node);
  406. }
  407. }
  408. for (const node of freeDatas) {
  409. removeArrayData(this.nodes, node);
  410. }
  411. return freeDatas;
  412. };
  413. /**
  414. * 因为提交其他数据,引起的树结构数据更新,调用该方法
  415. *
  416. * @param data - 更新的数据 {update, create, delete}
  417. * @returns {{}}
  418. */
  419. loadPostData(data) {
  420. const result = {};
  421. if (data.delete) {
  422. result.delete = this._freeData(data.delete);
  423. }
  424. if (data.create) {
  425. result.create = this._loadData(data.create);
  426. }
  427. if (data.update) {
  428. result.update = this._updateData(data.update);
  429. }
  430. return result;
  431. }
  432. recursiveFun(children, fun) {
  433. if (!fun) return;
  434. if (!children || children.length === 0) return;
  435. for (const c of children) {
  436. this.recursiveFun(c.children, fun);
  437. fun(c);
  438. }
  439. }
  440. }
  441. return new DragTree(setting);
  442. };