drag_tree.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455
  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. const levelField = this.setting.level;
  265. this.expandByCustom(function (n) {
  266. return n[levelField] < level;
  267. });
  268. }
  269. /**
  270. * 自动展开节点node
  271. * @param node
  272. * @returns {*}
  273. */
  274. autoExpandNode(node) {
  275. const parents = this.getAllParents(node);
  276. const reload = [];
  277. for (const p of parents) {
  278. if (!p.expanded) {
  279. reload.push(p);
  280. this.setExpanded(p, true);
  281. }
  282. }
  283. return reload;
  284. }
  285. /**
  286. * 加载数据(动态),只加载不同部分
  287. * @param {Array} datas
  288. * @return {Array} 加载到树的数据
  289. * @privateA
  290. */
  291. _updateData(datas) {
  292. datas = datas instanceof Array ? datas : [datas];
  293. let loadedData = [];
  294. for (const data of datas) {
  295. let node = this.getItems(data[this.setting.id]);
  296. if (node) {
  297. for (const prop in data) {
  298. if (data[prop] !== undefined && data[prop] !== node[prop]) {
  299. if (prop === this.setting.pid) {
  300. loadedData.push(this.getItems(node[this.setting.pid]));
  301. loadedData.push(this.getItems(data[this.setting.pid]));
  302. }
  303. if (prop === this.setting.order) {
  304. loadedData = loadedData.concat(this.getPosterity(node));
  305. }
  306. node[prop] = data[prop];
  307. }
  308. }
  309. loadedData.push(node);
  310. }
  311. }
  312. loadedData = _.uniq(loadedData);
  313. for (const node of loadedData) {
  314. if (node) {
  315. node.children = this.getChildren(node);
  316. node.expanded = node.children.length === 0 ? true : node.children[0].visible;
  317. } else {
  318. this.children = this.getChildren(null);
  319. }
  320. }
  321. this.sortTreeNode(true);
  322. return loadedData;
  323. };
  324. /**
  325. * 加载数据(动态),只加载不同部分
  326. * @param {Array} datas
  327. * @return {Array} 加载到树的数据
  328. * @privateA
  329. */
  330. _loadData(datas) {
  331. datas = datas instanceof Array ? datas : [datas];
  332. const loadedData = [], resortData = [];
  333. for (const data of datas) {
  334. let node = this.getItems(data[this.setting.id]);
  335. if (node) {
  336. const parent = this.getItems(node[this.setting.pid]);
  337. for (const prop in data) {
  338. if (data[prop] !== undefined && data[prop] !== node[prop]) {
  339. node[prop] = data[prop];
  340. if (parent && resortData.indexOf(parent) === -1) {
  341. resortData.push(parent);
  342. }
  343. }
  344. }
  345. loadedData.push(node);
  346. } else {
  347. const keyName = itemsPre + data[this.setting.id];
  348. const node = JSON.parse(JSON.stringify(data));
  349. this.items[keyName] = node;
  350. this.datas.push(node);
  351. node.expanded = true;
  352. node.visible = true;
  353. loadedData.push(node);
  354. if (resortData.indexOf(node) === -1) {
  355. resortData.push(node);
  356. }
  357. const parent = this.getItems(node[this.setting.pid]);
  358. if (parent && resortData.indexOf(parent) === -1) {
  359. resortData.push(parent);
  360. } else {
  361. resortData.push(this.setting.rootId);
  362. }
  363. }
  364. }
  365. for (const node of resortData) {
  366. if (node && node !== this.setting.rootId) {
  367. node.children = this.getChildren(node);
  368. } else {
  369. this.children = this.getChildren(null);
  370. }
  371. }
  372. this.sortTreeNode(true);
  373. for (const node of loadedData) {
  374. if (!node.expanded) {
  375. this.setExpanded(node, true);
  376. }
  377. }
  378. return loadedData;
  379. };
  380. /**
  381. * 清理数据(动态)
  382. * @param datas
  383. * @private
  384. */
  385. _freeData(datas) {
  386. datas = datas instanceof Array ? datas : [datas];
  387. const freeDatas = [];
  388. const removeArrayData = function (array, data) {
  389. const index = array.indexOf(data);
  390. array.splice(index, 1);
  391. };
  392. for (const data of datas) {
  393. const node = this.getItems(data);
  394. if (node) {
  395. freeDatas.push(node);
  396. node.deleteIndex = this.nodes.indexOf(node);
  397. delete this.items[itemsPre + node[this.setting.id]];
  398. if (node[this.setting.pid] !== this.setting.rootId) {
  399. const parent = this.getItems(node[this.setting.pid]);
  400. if (parent) {
  401. removeArrayData(parent.children, node);
  402. }
  403. } else {
  404. removeArrayData(this.children, node);
  405. }
  406. removeArrayData(this.datas, node);
  407. }
  408. }
  409. for (const node of freeDatas) {
  410. removeArrayData(this.nodes, node);
  411. }
  412. return freeDatas;
  413. };
  414. /**
  415. * 因为提交其他数据,引起的树结构数据更新,调用该方法
  416. *
  417. * @param data - 更新的数据 {update, create, delete}
  418. * @returns {{}}
  419. */
  420. loadPostData(data) {
  421. const result = {};
  422. if (data.delete) {
  423. result.delete = this._freeData(data.delete);
  424. }
  425. if (data.create) {
  426. result.create = this._loadData(data.create);
  427. }
  428. if (data.update) {
  429. result.update = this._updateData(data.update);
  430. }
  431. return result;
  432. }
  433. recursiveFun(children, fun) {
  434. if (!fun) return;
  435. if (!children || children.length === 0) return;
  436. for (const c of children) {
  437. this.recursiveFun(c.children, fun);
  438. fun(c);
  439. }
  440. }
  441. }
  442. return new DragTree(setting);
  443. };