path_tree.js 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  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. alert('error: ' + result.message, 'error', 'exclamation-circle');
  23. }
  24. },
  25. error: function(jqXHR, textStatus, errorThrown){
  26. alert('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 {function} callback
  210. */
  211. proto.addNode = function (url, node, callback) {
  212. const self = this;
  213. postData(url, {id: node[treeSetting.id]}, function (data) {
  214. self._loadData(data.update);
  215. const newNodes = self._loadData(data.create);
  216. callback(newNodes);
  217. });
  218. };
  219. /**
  220. * 删除节点
  221. * @param {String} url - 请求地址
  222. * @param {Object} node - 要删除的节点
  223. * @param {function} callback
  224. */
  225. proto.deleteNode = function (url, node, callback){
  226. const self = this;
  227. postData(url, {id: node[treeSetting.id]}, function (data) {
  228. self._loadData(data.update);
  229. self._freeData(data.delete);
  230. callback();
  231. });
  232. };
  233. /**
  234. * 上移
  235. * @param {String} url
  236. * @param {Object} node
  237. * @param {function} callback
  238. */
  239. proto.upMoveNode = function (url, node, callback) {
  240. const self = this;
  241. postData(url, {id: node[treeSetting.id]}, function (data) {
  242. const nodes = self._loadData(data.update);
  243. callback(nodes);
  244. });
  245. };
  246. /**
  247. * 下移
  248. * @param {String} url
  249. * @param {Object} node
  250. * @param {function} callback
  251. */
  252. proto.downMoveNode = function (url, node, callback) {
  253. const self = this;
  254. postData(url, {id: node[treeSetting.id]}, function (data) {
  255. const nodes = self._loadData(data.update);
  256. callback(nodes);
  257. });
  258. };
  259. /**
  260. * 升级
  261. * @param {String} url
  262. * @param {Object} node
  263. * @param {function} callback
  264. */
  265. proto.upLevelNode = function (url, node, callback) {
  266. const self = this;
  267. postData(url, {id: node[treeSetting.id]}, function (data) {
  268. self._loadData(data.update);
  269. callback();
  270. });
  271. };
  272. /**
  273. * 降级
  274. * @param {String} url
  275. * @param {Object} node
  276. * @param {function} callback
  277. */
  278. proto.downLevelNode = function (url, node, callback) {
  279. const self = this;
  280. postData(url, {id: node[treeSetting.id]}, function (data) {
  281. self._loadData(data.update);
  282. callback();
  283. });
  284. };
  285. return new PathTree();
  286. }