filing_template.js 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465
  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. batchUpdateFiling(data, callback) {
  119. const self = this;
  120. postData(`${window.location.pathname}/update`, data, function(result) {
  121. self.analysisFiling(result);
  122. if (callback) callback();
  123. })
  124. }
  125. }
  126. const levelTreeSetting = {
  127. treeId: 'filing',
  128. view: {
  129. selectedMulti: false,
  130. showIcon: false,
  131. },
  132. data: {
  133. simpleData: {
  134. idKey: 'id',
  135. pIdKey: 'tree_pid',
  136. rootPId: '-1',
  137. enable: true,
  138. }
  139. },
  140. edit: {
  141. enable: true,
  142. showRemoveBtn: !readOnly,
  143. showRenameBtn: !readOnly,
  144. renameTitle: '编辑',
  145. removeTitle: '删除',
  146. drag: {
  147. isCopy: false,
  148. isMove: true,
  149. pre: true,
  150. next: true,
  151. inner: false,
  152. },
  153. editNameSelectAll: true,
  154. },
  155. callback: {
  156. onClick: async function (e, key, node) {
  157. if (filingObj.curFiling && filingObj.curFiling.id === node.id) return;
  158. filingObj.setCurFiling(node);
  159. },
  160. beforeRename: async function(key, node, newName, isCancel) {
  161. if (!isCancel) await filingObj.renameFiling(node, newName);
  162. return true;
  163. },
  164. beforeRemove: function(key, node, isCancel) {
  165. filingObj.delFiling(node, function() {
  166. $('#del-filing').modal('hide');
  167. });
  168. return false;
  169. },
  170. beforeDrop: function(key, nodes, target, moveType, isCopy) {
  171. if (readOnly) return false;
  172. if (!target) return false;
  173. const order = nodes[0].getIndex() + 1;
  174. const targetOrder = target.getIndex() + 1;
  175. const targetParent = target.getParentNode();
  176. const targetMax = targetParent ? targetParent.children.length : filingObj.dragTree.children.length;
  177. if (moveType === 'prev') {
  178. if (target.tree_pid === nodes[0].tree_pid) {
  179. if (targetOrder > order) {
  180. filingObj.moveFiling(nodes[0], target.tree_pid, targetOrder === 1 ? 1 : targetOrder - 1);
  181. } else {
  182. filingObj.moveFiling(nodes[0], target.tree_pid, targetOrder === 1 ? 1 : targetOrder);
  183. }
  184. } else {
  185. filingObj.moveFiling(nodes[0], target.tree_pid, targetOrder === 1 ? 1 : targetOrder);
  186. }
  187. } else if (moveType === 'next') {
  188. if (target.tree_pid === nodes[0].tree_pid) {
  189. if (targetOrder < order) {
  190. filingObj.moveFiling(nodes[0], target.tree_pid, targetOrder === targetMax ? targetMax : targetOrder + 1);
  191. } else {
  192. filingObj.moveFiling(nodes[0], target.tree_pid, targetOrder === targetMax ? targetMax : targetOrder);
  193. }
  194. } else {
  195. filingObj.moveFiling(nodes[0], target.tree_pid, targetOrder + 1);
  196. }
  197. } else if (moveType === 'inner') {
  198. filingObj.moveFiling(nodes[0], target.tree_id, targetMax + 1);
  199. }
  200. }
  201. }
  202. };
  203. const filingObj = new FilingObj(levelTreeSetting);
  204. filingObj.analysisFiling(templateData);
  205. $('#add-slibing').click(() => {
  206. if (!filingObj.curFiling) return;
  207. filingObj.addSiblingFiling(filingObj.curFiling);
  208. });
  209. $('#add-child').click(() => {
  210. if (!filingObj.curFiling) return;
  211. filingObj.addChildFiling(filingObj.curFiling);
  212. });
  213. const hiddenSubmit = function(action, extraName, extraValue) {
  214. $('#hiddenForm').attr('action', action);
  215. if (extraName) {
  216. $('#extra').attr('name', extraName);
  217. $('#extra').val(extraValue);
  218. };
  219. $('#hiddenForm').submit();
  220. };
  221. $('body').on('mouseenter', ".table-file", function(){
  222. $(this).children(".btn-group-table").css("display","block");
  223. });
  224. $('body').on('mouseleave', ".table-file", function(){
  225. $(this).children(".btn-group-table").css("display","none");
  226. });
  227. $('body').on('click', 'a[name=renameTemplate]', function(e){
  228. $(this).parents('.table-file').attr('renaming', '1');
  229. $(`#${this.getAttribute('aria-describedby')}`).remove();
  230. const tempId = $(this).parents('.table-file').attr('tempId');
  231. const template = templateList.find(x => { return x.id === tempId; });
  232. if (!template) return;
  233. const html = [];
  234. html.push(`<div><input type="text" class="form-control form-control-sm" style="width: 300px" value="${template.name}"/></div>`);
  235. html.push('<div class="btn-group-table" style="display: none;">',
  236. `<a href="javascript: void(0)" name="renameOk" class="mr-1"><i class="fa fa-check fa-fw"></i></a>`,
  237. `<a href="javascript: void(0)" class="mr-1" name="renameCancel"><i class="fa fa-remove fa-fw text-danger"></i></a>`, '</div>');
  238. $(`.table-file[tempId=${tempId}]`).html(html.join(''));
  239. e.stopPropagation();
  240. });
  241. $('body').on('click', 'a[name=renameOk]', function(){
  242. const tempId = $(this).parents('.table-file').attr('tempId');
  243. const newName = $(this).parents('.table-file').find('input').val();
  244. hiddenSubmit('/file/template/save?id='+tempId, 'name', newName);
  245. $(this).parents('.table-file').attr('renaming', '0');
  246. });
  247. $('body').on('click', 'a[name=delTemplate]', function(e){
  248. e.stopPropagation();
  249. const tempId = $(this).parents('.table-file').attr('tempId');
  250. hiddenSubmit('/file/template/del?id='+tempId);
  251. });
  252. $('body').on('click', 'a[name=renameCancel]', function() {
  253. $(this).parents('.table-file').attr('renaming', '0');
  254. const tempId = $(this).parents('.table-file').attr('tempId');
  255. const template = templateList.find(x => { return x.id === tempId; });
  256. if (!template) return;
  257. const html = [];
  258. html.push(`<div>${template.name}</div>`);
  259. html.push('<div class="btn-group-table" style="display: none;">',
  260. '<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>',
  261. '<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>',
  262. '</div>');
  263. $(`.table-file[tempId=${tempId}]`).html(html.join(''));
  264. });
  265. class MultiObj {
  266. constructor(setting) {
  267. this.modal = $(`#${setting.modal}`);
  268. this.spread = SpreadJsObj.createNewSpread($(`#${setting.spread}`)[0]);
  269. this.sheet = this.spread.getActiveSheet();
  270. this.spreadSetting = {
  271. cols: [
  272. { title: '名称', colSpan: '1', rowSpan: '1', field: 'name', hAlign: 0, width: 250, formatter: '@', readOnly: true, cellType: 'tree' },
  273. { title: '固定', colSpan: '1', rowSpan: '1', field: 'is_fixed', hAlign: 1, width: 50, cellType: 'checkbox' },
  274. { title: '提示', colSpan: '1', rowSpan: '1', field: 'tips', hAlign: 0, width: 280, formatter: '@', wordWrap: 1 },
  275. ],
  276. emptyRows: 0,
  277. headRows: 1,
  278. headRowHeight: [32],
  279. defaultRowHeight: 21,
  280. headerFont: '12px 微软雅黑',
  281. font: '12px 微软雅黑',
  282. };
  283. SpreadJsObj.initSheet(this.sheet, this.spreadSetting);
  284. this.checkTree = createNewPathTree('base', {
  285. id: 'tree_id',
  286. pid: 'tree_pid',
  287. order: 'order',
  288. level: 'level',
  289. isLeaf: 'is_leaf',
  290. fullPath: 'full_path',
  291. rootId: -1,
  292. });
  293. const self = this;
  294. this.modal.bind('shown.bs.modal', function() {
  295. self.spread.refresh();
  296. });
  297. this.spread.bind(spreadNS.Events.ButtonClicked, function(e, info) {
  298. if (!info.sheet.zh_setting) return;
  299. const sheet = info.sheet, cellType = sheet.getCellType(info.row, info.col);
  300. if (cellType instanceof spreadNS.CellTypes.CheckBox) {
  301. if (sheet.isEditing()) sheet.endEdit(true);
  302. }
  303. const col = info.sheet.zh_setting.cols[info.col];
  304. if (col.field !== 'is_fixed') return;
  305. const tree = self.checkTree;
  306. const node = tree.nodes[info.row];
  307. if (node.level <= 1 && node.is_fixed) {
  308. toastr.warning('顶层节点不可取消固定');
  309. SpreadJsObj.reLoadRowsData(info.sheet, [info.row]);
  310. return;
  311. }
  312. const row = [];
  313. if (!node.is_fixed) {
  314. const relas = [];
  315. if (node.level > 1) {
  316. const parents = tree.getAllParents(node);
  317. for (const p of parents) {
  318. relas.push(...p.children);
  319. if (p.level === 1) relas.push(p);
  320. }
  321. } else {
  322. relas.push(node);
  323. }
  324. for (const p of relas) {
  325. p.is_fixed = true;
  326. row.push(tree.nodes.indexOf(p));
  327. }
  328. } else {
  329. const parent = tree.getParent(node);
  330. const posterity = tree.getPosterity(parent);
  331. for (const p of posterity) {
  332. p.is_fixed = false;
  333. row.push(tree.nodes.indexOf(p));
  334. }
  335. }
  336. SpreadJsObj.reLoadRowsData(info.sheet, row);
  337. });
  338. this.spread.bind(spreadNS.Events.EditEnded, function(e, info) {
  339. if (!info.sheet.zh_setting) return;
  340. const col = info.sheet.zh_setting.cols[info.col];
  341. const node = SpreadJsObj.getSelectObject(info.sheet);
  342. node[col.field] = trimInvalidChar(info.editingText);
  343. SpreadJsObj.reLoadRowData(info.sheet, info.row)
  344. });
  345. $(`#${setting.modal}-ok`).click(function() {
  346. try {
  347. const data = self.getMultiUpdateData();
  348. filingObj.batchUpdateFiling(data, function() {
  349. self.modal.modal('hide');
  350. });
  351. } catch(err) {
  352. toastr.error(err.stack ? '保存配置数据错误' : err);
  353. }
  354. });
  355. }
  356. getMultiUpdateData() {
  357. const data = [], self = this;
  358. for (const [i, node] of this.checkTree.nodes.entries()) {
  359. node.source_filing_type = i + 1;
  360. }
  361. const getUpdateData = function(children) {
  362. for (const [i, node] of children.entries()) {
  363. let filing_type = node.source_filing_type;
  364. if (!node.is_fixed) {
  365. const parents = self.checkTree.getAllParents(node);
  366. parents.sort((a, b) => { return b.tree_level - a.tree_level});
  367. const fixedParent = parents.find(function(x) { return x.is_fixed; });
  368. if (!fixedParent) throw `【${node.name}】查询不到固定信息`;
  369. filing_type = fixedParent.source_filing_type;
  370. }
  371. data.push({ id: node.id, is_fixed: node.is_fixed, filing_type, tree_order: i + 1, tips: node.tips || '' });
  372. if (node.children) getUpdateData(node.children);
  373. }
  374. };
  375. getUpdateData(this.checkTree.children);
  376. return {updateType: 'multi', data};
  377. }
  378. _convertData(sourceTree) {
  379. const data = [];
  380. for (const node of sourceTree.nodes) {
  381. const parent = node.tree_pid === '-1' ? undefined : data.find(x => { return x.id === node.tree_pid; });
  382. const child = sourceTree.nodes.find(x => { return x.tree_pid === node.id; });
  383. data.push({
  384. id: node.id,
  385. tree_id: data.length + 1,
  386. tree_pid: parent ? parent.tree_id : -1,
  387. order: node.tree_order + 1,
  388. level: node.tree_level,
  389. is_leaf: !child,
  390. full_path: '',
  391. name: node.name,
  392. is_fixed: node.is_fixed,
  393. filing_type: node.filing_type,
  394. tips: node.tips,
  395. });
  396. }
  397. return data;
  398. }
  399. reload(sourceTree) {
  400. this.checkTree.loadDatas(this._convertData(sourceTree));
  401. SpreadJsObj.loadSheetData(this.sheet, SpreadJsObj.DataType.Tree, this.checkTree);
  402. }
  403. show() {
  404. this.modal.modal('show');
  405. }
  406. exportFile(sourceTree) {
  407. const exportData = sourceTree.nodes.map(node => { return {
  408. id: node.id,
  409. tree_pid: node.tree_pid,
  410. tree_order: node.tree_order,
  411. tree_level: node.tree_level,
  412. name: node.name,
  413. is_fixed: node.is_fixed,
  414. filing_type: node.filing_type,
  415. tips: node.tips,
  416. }});
  417. const blob = new Blob([JSON.stringify(exportData, '', '')], { type: 'application/text'});
  418. const template = templateList.find(x => { return x.id === sourceTree.nodes[0].temp_id });
  419. saveAs(blob, `${template.name}.json`);
  420. }
  421. importFromJSON(str) {
  422. try {
  423. const data = JSON.parse(str);
  424. filingObj.batchUpdateFiling({ updateType: 'import', data });
  425. } catch(err) {
  426. toastr.error('导入文件格式错误,无法解析');
  427. }
  428. }
  429. importFile(file) {
  430. if (!file) return;
  431. const self = this;
  432. let reader = new FileReader();
  433. reader.onload = function() {
  434. self.importFromJSON(this.result);
  435. };
  436. reader.readAsText(file);
  437. }
  438. }
  439. let multiObj = new MultiObj({ modal: 'multi-set', spread: 'multi-spread' });
  440. $('#multi-setting').click(() => {
  441. multiObj.reload(filingObj.dragTree);
  442. multiObj.show();
  443. });
  444. $('#export-template').click(() => {
  445. multiObj.exportFile(filingObj.dragTree);
  446. });
  447. $('#import-template').click(() => {
  448. selectFile({
  449. fileType: '*.json',
  450. select: function(file) {
  451. multiObj.importFile(file)
  452. }
  453. });
  454. });
  455. });