path_tree.js 8.9 KB

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