filing_template.js 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497
  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. $('body').on('click', 'a[name=shareTemplate]', function(e){
  266. e.stopPropagation();
  267. const tempId = $(this).parents('.table-file').attr('tempId');
  268. hiddenSubmit('/file/template/save?id='+tempId, 'is_share', 1);
  269. });
  270. $('body').on('click', 'a[name=shareOffTemplate]', function(e){
  271. e.stopPropagation();
  272. const tempId = $(this).parents('.table-file').attr('tempId');
  273. hiddenSubmit('/file/template/save?id='+tempId, 'is_share', 0);
  274. });
  275. class MultiObj {
  276. constructor(setting) {
  277. this.modal = $(`#${setting.modal}`);
  278. this.spread = SpreadJsObj.createNewSpread($(`#${setting.spread}`)[0]);
  279. this.sheet = this.spread.getActiveSheet();
  280. this.spreadSetting = {
  281. cols: [
  282. { title: '名称', colSpan: '1', rowSpan: '1', field: 'name', hAlign: 0, width: 250, formatter: '@', readOnly: true, cellType: 'tree' },
  283. { title: '固定', colSpan: '1', rowSpan: '1', field: 'is_fixed', hAlign: 1, width: 50, cellType: 'checkbox' },
  284. { title: '提示', colSpan: '1', rowSpan: '1', field: 'tips', hAlign: 0, width: 280, formatter: '@', wordWrap: 1 },
  285. ],
  286. emptyRows: 0,
  287. headRows: 1,
  288. headRowHeight: [32],
  289. defaultRowHeight: 21,
  290. headerFont: '12px 微软雅黑',
  291. font: '12px 微软雅黑',
  292. };
  293. SpreadJsObj.initSheet(this.sheet, this.spreadSetting);
  294. this.checkTree = createNewPathTree('base', {
  295. id: 'tree_id',
  296. pid: 'tree_pid',
  297. order: 'order',
  298. level: 'level',
  299. isLeaf: 'is_leaf',
  300. fullPath: 'full_path',
  301. rootId: -1,
  302. });
  303. const self = this;
  304. this.modal.bind('shown.bs.modal', function() {
  305. self.spread.refresh();
  306. });
  307. this.spread.bind(spreadNS.Events.ButtonClicked, function(e, info) {
  308. if (!info.sheet.zh_setting) return;
  309. const sheet = info.sheet, cellType = sheet.getCellType(info.row, info.col);
  310. if (cellType instanceof spreadNS.CellTypes.CheckBox) {
  311. if (sheet.isEditing()) sheet.endEdit(true);
  312. }
  313. const col = info.sheet.zh_setting.cols[info.col];
  314. if (col.field !== 'is_fixed') return;
  315. const tree = self.checkTree;
  316. const node = tree.nodes[info.row];
  317. if (node.level <= 1 && node.is_fixed) {
  318. toastr.warning('顶层节点不可取消固定');
  319. SpreadJsObj.reLoadRowsData(info.sheet, [info.row]);
  320. return;
  321. }
  322. const row = [];
  323. if (!node.is_fixed) {
  324. const relas = [];
  325. if (node.level > 1) {
  326. const parents = tree.getAllParents(node);
  327. for (const p of parents) {
  328. relas.push(...p.children);
  329. if (p.level === 1) relas.push(p);
  330. }
  331. } else {
  332. relas.push(node);
  333. }
  334. for (const p of relas) {
  335. p.is_fixed = true;
  336. row.push(tree.nodes.indexOf(p));
  337. }
  338. } else {
  339. const parent = tree.getParent(node);
  340. const posterity = tree.getPosterity(parent);
  341. for (const p of posterity) {
  342. p.is_fixed = false;
  343. row.push(tree.nodes.indexOf(p));
  344. }
  345. }
  346. SpreadJsObj.reLoadRowsData(info.sheet, row);
  347. });
  348. this.spread.bind(spreadNS.Events.EditEnded, function(e, info) {
  349. if (!info.sheet.zh_setting) return;
  350. const col = info.sheet.zh_setting.cols[info.col];
  351. const node = SpreadJsObj.getSelectObject(info.sheet);
  352. node[col.field] = trimInvalidChar(info.editingText);
  353. SpreadJsObj.reLoadRowData(info.sheet, info.row)
  354. });
  355. $(`#${setting.modal}-ok`).click(function() {
  356. try {
  357. const data = self.getMultiUpdateData();
  358. filingObj.batchUpdateFiling(data, function() {
  359. self.modal.modal('hide');
  360. });
  361. } catch(err) {
  362. toastr.error(err.stack ? '保存配置数据错误' : err);
  363. }
  364. });
  365. }
  366. getMultiUpdateData() {
  367. const data = [], self = this;
  368. for (const [i, node] of this.checkTree.nodes.entries()) {
  369. node.source_filing_type = i + 1;
  370. }
  371. const getUpdateData = function(children) {
  372. for (const [i, node] of children.entries()) {
  373. let filing_type = node.source_filing_type;
  374. if (!node.is_fixed) {
  375. const parents = self.checkTree.getAllParents(node);
  376. parents.sort((a, b) => { return b.tree_level - a.tree_level});
  377. const fixedParent = parents.find(function(x) { return x.is_fixed; });
  378. if (!fixedParent) throw `【${node.name}】查询不到固定信息`;
  379. filing_type = fixedParent.source_filing_type;
  380. }
  381. data.push({ id: node.id, is_fixed: node.is_fixed, filing_type, tree_order: i + 1, tips: node.tips || '' });
  382. if (node.children) getUpdateData(node.children);
  383. }
  384. };
  385. getUpdateData(this.checkTree.children);
  386. return {updateType: 'multi', data};
  387. }
  388. _convertData(sourceTree) {
  389. const data = [];
  390. for (const node of sourceTree.nodes) {
  391. const parent = node.tree_pid === '-1' ? undefined : data.find(x => { return x.id === node.tree_pid; });
  392. const child = sourceTree.nodes.find(x => { return x.tree_pid === node.id; });
  393. data.push({
  394. id: node.id,
  395. tree_id: data.length + 1,
  396. tree_pid: parent ? parent.tree_id : -1,
  397. order: node.tree_order + 1,
  398. level: node.tree_level,
  399. is_leaf: !child,
  400. full_path: '',
  401. name: node.name,
  402. is_fixed: node.is_fixed,
  403. filing_type: node.filing_type,
  404. tips: node.tips,
  405. });
  406. }
  407. return data;
  408. }
  409. reload(sourceTree) {
  410. this.checkTree.loadDatas(this._convertData(sourceTree));
  411. SpreadJsObj.loadSheetData(this.sheet, SpreadJsObj.DataType.Tree, this.checkTree);
  412. }
  413. show() {
  414. this.modal.modal('show');
  415. }
  416. exportFile(sourceTree) {
  417. const exportData = sourceTree.nodes.map(node => { return {
  418. id: node.id,
  419. tree_pid: node.tree_pid,
  420. tree_order: node.tree_order,
  421. tree_level: node.tree_level,
  422. name: node.name,
  423. is_fixed: node.is_fixed,
  424. filing_type: node.filing_type,
  425. tips: node.tips,
  426. }});
  427. const blob = new Blob([JSON.stringify(exportData, '', '')], { type: 'application/text'});
  428. const template = templateList.find(x => { return x.id === sourceTree.nodes[0].temp_id });
  429. saveAs(blob, `${template.name}.json`);
  430. }
  431. importFromJSON(str) {
  432. try {
  433. const data = JSON.parse(str);
  434. filingObj.batchUpdateFiling({ updateType: 'import', data });
  435. } catch(err) {
  436. toastr.error('导入文件格式错误,无法解析');
  437. }
  438. }
  439. importFile(file) {
  440. if (!file) return;
  441. const self = this;
  442. let reader = new FileReader();
  443. reader.onload = function() {
  444. self.importFromJSON(this.result);
  445. };
  446. reader.readAsText(file);
  447. }
  448. }
  449. let multiObj = new MultiObj({ modal: 'multi-set', spread: 'multi-spread' });
  450. $('#multi-setting').click(() => {
  451. multiObj.reload(filingObj.dragTree);
  452. multiObj.show();
  453. });
  454. $('#export-template').click(() => {
  455. multiObj.exportFile(filingObj.dragTree);
  456. });
  457. $('#import-template').click(() => {
  458. selectFile({
  459. fileType: '*.json',
  460. select: function(file) {
  461. multiObj.importFile(file)
  462. }
  463. });
  464. });
  465. $('#import-share-template').click(() => {
  466. const check = $('[name=sst-check]');
  467. for (const c of check) {
  468. c.checked = false;
  469. }
  470. $('#select-share-template').modal('show');
  471. });
  472. $('[name=sst-check]').change(function() {
  473. const check = $('[name=sst-check]');
  474. for (const c of check) {
  475. if (c.value !== this.value) c.checked = false;
  476. }
  477. // if (val.length > 1) val.shift();
  478. });
  479. $('#sst-ok').click(function() {
  480. const tempId = $('[name=sst-check]:checked').val();
  481. if (!tempId) {
  482. toastr.warning('请选择要导入的模板');
  483. return;
  484. }
  485. hiddenSubmit('/file/template/save', 'share_id', tempId);
  486. });
  487. });