drag_tree.js 16 KB

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