path_tree.js 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. const createNewPathTree = function (setting) {
  2. const treeSetting = JSON.parse(JSON.stringify(setting));
  3. const itemsPre = 'id_';
  4. const postData = function (url, data, successCallback) {
  5. $.ajax({
  6. type:"POST",
  7. url: url,
  8. data: {'data': JSON.stringify(data)},
  9. dataType: 'json',
  10. cache: false,
  11. timeout: 5000,
  12. beforeSend: function(xhr) {
  13. let csrfToken = Cookies.get('csrfToken');
  14. xhr.setRequestHeader('x-csrf-token', csrfToken);
  15. },
  16. success: function(result){
  17. if (result.err === 0) {
  18. if (successCallback) {
  19. successCallback(result.data);
  20. }
  21. } else {
  22. toast('error: ' + result.message, 'error', 'exclamation-circle');
  23. }
  24. },
  25. error: function(jqXHR, textStatus, errorThrown){
  26. toast('error ' + textStatus + " " + errorThrown, 'error', 'exclamation-circle');
  27. }
  28. });
  29. };
  30. const PathTree = function () {
  31. // 无索引
  32. this.datas = [];
  33. // 以key为索引
  34. this.items = {};
  35. // 以排序为索引
  36. this.nodes = [];
  37. };
  38. const proto = PathTree.prototype;
  39. /**
  40. * 树结构根据显示排序
  41. */
  42. proto.sortTreeNode = function () {
  43. const self = this;
  44. const addSortNodes = function (nodes) {
  45. for (let i = 0; i < nodes.length; i++) {
  46. self.nodes.push(nodes[i]);
  47. addSortNodes(self.getChildren(nodes[i]));
  48. }
  49. };
  50. self.nodes = [];
  51. addSortNodes(this.getChildren(null));
  52. };
  53. /**
  54. * 加载数据(初始化), 并给数据添加部分树结构必须数据
  55. * @param datas
  56. */
  57. proto.loadDatas = function (datas) {
  58. // 清空旧数据
  59. this.items = {};
  60. this.nodes = [];
  61. // 加载全部数据
  62. for (const data of datas) {
  63. const keyName = itemsPre + data[treeSetting.id];
  64. this.items[keyName] = JSON.parse(JSON.stringify(data));
  65. this.datas.push(this.items[keyName]);
  66. }
  67. this.sortTreeNode();
  68. for (const node of this.nodes) {
  69. const children = this.getChildren(node);
  70. node.expanded = children.length > 0;
  71. node.visible = true;
  72. }
  73. };
  74. /**
  75. * 加载数据(动态),只加载不同部分
  76. * @param {Array} datas
  77. * @return {Array} 加载到树的数据
  78. * @privateA
  79. */
  80. proto._loadData = function (datas) {
  81. const loadedData = [];
  82. for (const data of datas) {
  83. let node = this.getItems(data[treeSetting.id]);
  84. if (node) {
  85. for (const prop in node) {
  86. if (data[prop]) {
  87. node[prop] = data[prop];
  88. }
  89. }
  90. loadedData.push(node);
  91. } else {
  92. const keyName = itemsPre + data[treeSetting.id];
  93. const node = JSON.parse(JSON.stringify(data));
  94. this.items[keyName] = node;
  95. this.datas.push(node);
  96. node.expanded = false;
  97. node.visible = true;
  98. loadedData.push(node);
  99. }
  100. }
  101. this.sortTreeNode();
  102. return loadedData;
  103. };
  104. proto._freeData = function (datas) {
  105. const removeArrayData = function (array, data) {
  106. const index = array.indexOf(data);
  107. array.splice(index, 1);
  108. };
  109. for (const data of datas) {
  110. const node = this.getItems(data[treeSetting.id]);
  111. if (node) {
  112. delete this.items[itemsPre + node[treeSetting.id]];
  113. removeArrayData(this.datas, node);
  114. removeArrayData(this.nodes, node);
  115. }
  116. }
  117. };
  118. /**
  119. * 根据id获取树结构节点数据
  120. * @param {Number} id
  121. * @returns {Object}
  122. */
  123. proto.getItems = function (id) {
  124. return this.items[itemsPre + id];
  125. };
  126. /**
  127. * 查找node的parent
  128. * @param {Object} node
  129. * @returns {Object}
  130. */
  131. proto.getParent = function (node) {
  132. return this.getItems(node[treeSetting.pid]);
  133. };
  134. /**
  135. * 查询node的已下载子节点
  136. * @param {Object} node
  137. * @returns {Array}
  138. */
  139. proto.getChildren = function (node) {
  140. const pid = node ? node[treeSetting.id] : treeSetting.rootId;
  141. const children = this.datas.filter(function (x) {
  142. return x[treeSetting.pid] === pid;
  143. });
  144. children.sort(function (a, b) {
  145. return a.order - b.order;
  146. });
  147. return children;
  148. };
  149. /**
  150. * 查询node的已下载的全部后代
  151. * @param {Object} node
  152. * @returns {Array}
  153. */
  154. proto.getPosterity = function (node) {
  155. const reg = new RegExp('^' + node.full_path + '.');
  156. return this.datas.filter(function (x) {
  157. return reg.test(x.full_path);
  158. })
  159. };
  160. /**
  161. * 查询node是否是父节点的最后一个子节点
  162. * @param {Object} node
  163. * @returns {boolean}
  164. */
  165. proto.isLastSibling = function (node) {
  166. const siblings = this.getChildren(this.getParent(node));
  167. return node.order === siblings.length;
  168. };
  169. /**
  170. * 刷新子节点是否可见
  171. * @param {Object} node
  172. * @private
  173. */
  174. proto._refreshChildrenVisible = function (node) {
  175. const children = this.getChildren(node);
  176. for (const child of children) {
  177. child.visible = node.expanded && node.visible;
  178. this._refreshChildrenVisible(child);
  179. }
  180. }
  181. /**
  182. * 设置节点是否展开, 并控制子节点可见
  183. * @param {Object} node
  184. * @param {Boolean} expanded
  185. */
  186. proto.setExpanded = function (node, expanded) {
  187. node.expanded = expanded;
  188. this._refreshChildrenVisible(node);
  189. };
  190. /**
  191. * 以下方法需等待响应, 通过callback刷新界面
  192. */
  193. /**
  194. * 加载子节点
  195. * @param {Object} node
  196. * @param {function} callback
  197. */
  198. proto.loadChildren = function (node, callback) {
  199. const self = this;
  200. postData('get-children', {id: node[treeSetting.id]}, function (data) {
  201. self._loadData(data);
  202. callback();
  203. });
  204. };
  205. /**
  206. * 树结构基本操作
  207. * @param {String} url - 请求地址
  208. * @param {Object} node - 操作节点
  209. * @param {String} type - 操作类型
  210. * @param {function} callback - 界面刷新
  211. */
  212. proto.baseOperation = function (url, node, type, callback) {
  213. const self = this;
  214. const data = {
  215. id: node[treeSetting.id],
  216. postType: type
  217. };
  218. postData(url, data, function (datas) {
  219. const result = {};
  220. if (datas.update) {
  221. result.update = self._loadData(datas.update);
  222. }
  223. if (datas.create) {
  224. result.create = self._loadData(datas.create);
  225. }
  226. if (datas.delete) {
  227. result.delete = self._freeData(datas.delete);
  228. }
  229. callback(result);
  230. })
  231. };
  232. return new PathTree();
  233. }