filing_template.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. $(document).ready(function() {
  2. autoFlashHeight();
  3. $('#filing').height($(".sjs-height-0").height() - $('#add-slibing').parent().parent().height() - 10);
  4. class FilingObj {
  5. constructor(setting) {
  6. // 原始数据整理后的树结构,用来整理zTree显示
  7. this.dragTree = createDragTree({
  8. id: 'id',
  9. pid: 'tree_pid',
  10. level: 'tree_level',
  11. order: 'tree_order',
  12. rootId: '-1'
  13. });
  14. // 界面显示的zTree
  15. this.setting = setting;
  16. this.filingTree = null;
  17. $('#filing').height($(".sjs-height-0").height()-$('.d-flex',".sjs-height-0").height() - 10);
  18. }
  19. _loadFilingSourceNode() {
  20. const self = this;
  21. const loadChildren = function(children) {
  22. for (const child of children) {
  23. if (child.children && child.children.length > 0) loadChildren(child.children);
  24. child.source_node = self.dragTree.getItems(child.id);
  25. }
  26. };
  27. const nodes = this.filingTree.getNodes();
  28. loadChildren(nodes);
  29. }
  30. loadFiling() {
  31. if (this.filingTree) $.fn.zTree.destroy(this.setting.treeId);
  32. const sortNodes = this.dragTree.nodes.map(x => {
  33. const result = {
  34. id: x.id,
  35. tree_pid: x.tree_pid,
  36. name: x.name + (x.total_file_count > 0 ? `(${x.total_file_count})` : ''),
  37. spid: x.spid,
  38. };
  39. return result;
  40. });
  41. this.filingTree = $.fn.zTree.init($('#filing'), this.setting, sortNodes);
  42. this._loadFilingSourceNode();
  43. const curCache = getLocalCache(this.curFilingKey);
  44. const curNode = curCache ? this.filingTree.getNodeByParam('id', curCache) : null;
  45. if (curNode){
  46. this.filingTree.selectNode(curNode);
  47. filingObj.setCurFiling(curNode);
  48. }
  49. }
  50. analysisFiling(data) {
  51. this.dragTree.loadDatas(data);
  52. this.loadFiling();
  53. }
  54. addSiblingFiling(node) {
  55. const self = this;
  56. postData(`${window.location.pathname}/update`, { updateType: 'add', tree_pid: node.tree_pid, tree_pre_id: node.id }, function(result) {
  57. const refreshData = self.dragTree.loadPostData(result);
  58. const newNode = refreshData.create[0];
  59. const nodes = self.filingTree.addNodes(node.getParentNode(), node.getIndex() + 1, [{ id: newNode.id, tree_pid: newNode.tree_pid, name: newNode.name, spid: newNode.spid }]);
  60. nodes[0].source_node = newNode;
  61. });
  62. }
  63. addChildFiling(node) {
  64. const self = this;
  65. postData(`${window.location.pathname}/update`, { updateType: 'add', tree_pid: node.id }, function(result) {
  66. const refreshData = self.dragTree.loadPostData(result);
  67. const newNode = refreshData.create[0];
  68. const nodes = self.filingTree.addNodes(node, -1, [{ id: newNode.id, tree_pid: newNode.tree_pid, name: newNode.name, spid: newNode.spid}]);
  69. nodes[0].source_node = newNode;
  70. });
  71. }
  72. delFiling(node, callback) {
  73. const parent = node.getParentNode();
  74. const self = this;
  75. postData(`${window.location.pathname}/update`, { updateType: 'del', id: node.id }, function(result) {
  76. self.dragTree.loadPostData(result);
  77. self.filingTree.removeNode(node);
  78. if (parent) {
  79. const path = parent.getPath();
  80. for (const p of path) {
  81. p.name = p.source_node.name + (p.source_node.total_file_count > 0 ? `(${p.source_node.total_file_count})` : '');
  82. filingObj.filingTree.updateNode(p);
  83. }
  84. }
  85. if (callback) callback();
  86. });
  87. }
  88. async renameFiling(node, newName) {
  89. const result = await postDataAsync(`${window.location.pathname}/update`, { updateType:'save', id: node.id, name: newName });
  90. node.source_node.name = newName;
  91. node.name = node.source_node.name + (node.source_node.total_file_count > 0 ? `(${node.source_node.total_file_count})` : '');
  92. return result;
  93. }
  94. async setCurFiling(node) {
  95. filingObj.curFiling = node;
  96. }
  97. moveFiling(node, tree_pid, tree_order) {
  98. if (tree_pid === node.source_node.tree_pid && tree_order === node.source_node.tree_order) return;
  99. const self = this;
  100. postData(`${window.location.pathname}/update`, { updateType: 'move', id: node.id, tree_pid, tree_order }, function(result) {
  101. const refresh = self.dragTree.loadPostData(result);
  102. const updated = [];
  103. for (const u of refresh.update) {
  104. if (!u) continue;
  105. const node = self.filingTree.getNodeByParam('id', u.id);
  106. if (node) {
  107. const path = node.getPath();
  108. for (const p of path) {
  109. if (updated.indexOf(p.id) >= 0) continue;
  110. p.name = p.source_node.name + (p.source_node.total_file_count > 0 ? `(${p.source_node.total_file_count})` : '');
  111. filingObj.filingTree.updateNode(p);
  112. updated.push(p.id);
  113. }
  114. }
  115. }
  116. });
  117. }
  118. }
  119. const levelTreeSetting = {
  120. treeId: 'filing',
  121. view: {
  122. selectedMulti: false,
  123. showIcon: false,
  124. },
  125. data: {
  126. simpleData: {
  127. idKey: 'id',
  128. pIdKey: 'tree_pid',
  129. rootPId: '-1',
  130. enable: true,
  131. }
  132. },
  133. edit: {
  134. enable: true,
  135. showRemoveBtn: !readOnly,
  136. showRenameBtn: !readOnly,
  137. renameTitle: '编辑',
  138. removeTitle: '删除',
  139. drag: {
  140. isCopy: false,
  141. isMove: true,
  142. pre: true,
  143. next: true,
  144. inner: false,
  145. },
  146. editNameSelectAll: true,
  147. },
  148. callback: {
  149. onClick: async function (e, key, node) {
  150. if (filingObj.curFiling && filingObj.curFiling.id === node.id) return;
  151. filingObj.setCurFiling(node);
  152. },
  153. beforeRename: async function(key, node, newName, isCancel) {
  154. if (!isCancel) await filingObj.renameFiling(node, newName);
  155. return true;
  156. },
  157. beforeRemove: function(key, node, isCancel) {
  158. filingObj.delFiling(node, function() {
  159. $('#del-filing').modal('hide');
  160. });
  161. return false;
  162. },
  163. beforeDrop: function(key, nodes, target, moveType, isCopy) {
  164. if (readOnly) return false;
  165. if (!target) return false;
  166. const order = nodes[0].getIndex() + 1;
  167. const targetOrder = target.getIndex() + 1;
  168. const targetParent = target.getParentNode();
  169. const targetMax = targetParent ? targetParent.children.length : filingObj.dragTree.children.length;
  170. if (moveType === 'prev') {
  171. if (target.tree_pid === nodes[0].tree_pid) {
  172. if (targetOrder > order) {
  173. filingObj.moveFiling(nodes[0], target.tree_pid, targetOrder === 1 ? 1 : targetOrder - 1);
  174. } else {
  175. filingObj.moveFiling(nodes[0], target.tree_pid, targetOrder === 1 ? 1 : targetOrder);
  176. }
  177. } else {
  178. filingObj.moveFiling(nodes[0], target.tree_pid, targetOrder === 1 ? 1 : targetOrder);
  179. }
  180. } else if (moveType === 'next') {
  181. if (target.tree_pid === nodes[0].tree_pid) {
  182. if (targetOrder < order) {
  183. filingObj.moveFiling(nodes[0], target.tree_pid, targetOrder === targetMax ? targetMax : targetOrder + 1);
  184. } else {
  185. filingObj.moveFiling(nodes[0], target.tree_pid, targetOrder === targetMax ? targetMax : targetOrder);
  186. }
  187. } else {
  188. filingObj.moveFiling(nodes[0], target.tree_pid, targetOrder + 1);
  189. }
  190. } else if (moveType === 'inner') {
  191. filingObj.moveFiling(nodes[0], target.tree_id, targetMax + 1);
  192. }
  193. }
  194. }
  195. };
  196. const filingObj = new FilingObj(levelTreeSetting);
  197. filingObj.analysisFiling(templateData);
  198. $('#add-slibing').click(() => {
  199. if (!filingObj.curFiling) return;
  200. filingObj.addSiblingFiling(filingObj.curFiling);
  201. });
  202. $('#add-child').click(() => {
  203. if (!filingObj.curFiling) return;
  204. filingObj.addChildFiling(filingObj.curFiling);
  205. });
  206. const hiddenSubmit = function(action, extraName, extraValue) {
  207. $('#hiddenForm').attr('action', action);
  208. if (extraName) {
  209. $('#extra').attr('name', extraName);
  210. $('#extra').val(extraValue);
  211. };
  212. $('#hiddenForm').submit();
  213. };
  214. $('body').on('mouseenter', ".table-file", function(){
  215. $(this).children(".btn-group-table").css("display","block");
  216. });
  217. $('body').on('mouseleave', ".table-file", function(){
  218. $(this).children(".btn-group-table").css("display","none");
  219. });
  220. $('body').on('click', 'a[name=renameTemplate]', function(e){
  221. $(this).parents('.table-file').attr('renaming', '1');
  222. $(`#${this.getAttribute('aria-describedby')}`).remove();
  223. const tempId = $(this).parents('.table-file').attr('tempId');
  224. const template = templateList.find(x => { return x.id === tempId; });
  225. if (!template) return;
  226. const html = [];
  227. html.push(`<div><input type="text" class="form-control form-control-sm" style="width: 300px" value="${template.name}"/></div>`);
  228. html.push('<div class="btn-group-table" style="display: none;">',
  229. `<a href="javascript: void(0)" name="renameOk" class="mr-1"><i class="fa fa-check fa-fw"></i></a>`,
  230. `<a href="javascript: void(0)" class="mr-1" name="renameCancel"><i class="fa fa-remove fa-fw text-danger"></i></a>`, '</div>');
  231. $(`.table-file[tempId=${tempId}]`).html(html.join(''));
  232. e.stopPropagation();
  233. });
  234. $('body').on('click', 'a[name=renameOk]', function(){
  235. const tempId = $(this).parents('.table-file').attr('tempId');
  236. const newName = $(this).parents('.table-file').find('input').val();
  237. hiddenSubmit('/file/template/save?id='+tempId, 'name', newName);
  238. $(this).parents('.table-file').attr('renaming', '0');
  239. });
  240. $('body').on('click', 'a[name=delTemplate]', function(e){
  241. e.stopPropagation();
  242. const tempId = $(this).parents('.table-file').attr('tempId');
  243. hiddenSubmit('/file/template/del?id='+tempId);
  244. });
  245. $('body').on('click', 'a[name=renameCancel]', function() {
  246. $(this).parents('.table-file').attr('renaming', '0');
  247. const tempId = $(this).parents('.table-file').attr('tempId');
  248. const template = templateList.find(x => { return x.id === tempId; });
  249. if (!template) return;
  250. const html = [];
  251. html.push(`<div>${template.name}</div>`);
  252. html.push('<div class="btn-group-table" style="display: none;">',
  253. '<a href="javascript: void(0);" class="mr-1" data-toggle="tooltip" data-placement="bottom" data-original-title="编辑" name="renameTemplate"><i class="fa fa-pencil fa-fw"></i></a>',
  254. '<a href="javascript: void(0);" class="mr-1" data-toggle="tooltip" data-placement="bottom" data-original-title="删除" name="delTemplate"><i class="fa fa-trash-o fa-fw text-danger"></i></a>',
  255. '</div>');
  256. $(`.table-file[tempId=${tempId}]`).html(html.join(''));
  257. });
  258. });