filing_manage.js 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457
  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. calcTotalFileCount() {
  31. this.dragTree.recursiveFun(this.dragTree.children, x => {
  32. if (x.children && x.children.length > 0) {
  33. x.total_file_count = x.children.reduce((pre, c) => {
  34. return pre + c.total_file_count
  35. }, 0);
  36. } else {
  37. x.total_file_count = x.file_count || 0;
  38. }
  39. });
  40. }
  41. loadFiling() {
  42. if (this.filingTree) $.fn.zTree.destroy(this.setting.treeId);
  43. const sortNodes = this.dragTree.nodes.map(x => {
  44. const result = {
  45. id: x.id,
  46. tree_pid: x.tree_pid,
  47. name: x.name + (x.total_file_count > 0 ? `(${x.total_file_count})` : ''),
  48. spid: x.spid,
  49. };
  50. return result;
  51. });
  52. this.filingTree = $.fn.zTree.init($('#filing'), this.setting, sortNodes);
  53. this._loadFilingSourceNode();
  54. const curCache = getLocalCache(this.curFilingKey);
  55. const curNode = curCache ? this.filingTree.getNodeByParam('id', curCache) : null;
  56. if (curNode){
  57. this.filingTree.selectNode(curNode);
  58. filingObj.setCurFiling(curNode);
  59. }
  60. }
  61. analysisFiling(data) {
  62. this.dragTree.loadDatas(data);
  63. this.calcTotalFileCount();
  64. this.loadFiling();
  65. }
  66. addSiblingFiling(node) {
  67. const self = this;
  68. postData(`${window.location.pathname}/update`, { updateType: 'add', tree_pid: node.tree_pid, tree_pre_id: node.id }, function(result) {
  69. const refreshData = self.dragTree.loadPostData(result);
  70. const newNode = refreshData.create[0];
  71. const nodes = self.filingTree.addNodes(node.getParentNode(), node.getIndex() + 1, [{ id: newNode.id, tree_pid: newNode.tree_pid, name: newNode.name, spid: newNode.spid }]);
  72. nodes[0].source_node = newNode;
  73. });
  74. }
  75. addChildFiling(node) {
  76. const self = this;
  77. postData(`${window.location.pathname}/update`, { updateType: 'add', tree_pid: node.id }, function(result) {
  78. const refreshData = self.dragTree.loadPostData(result);
  79. const newNode = refreshData.create[0];
  80. const nodes = self.filingTree.addNodes(node, -1, [{ id: newNode.id, tree_pid: newNode.tree_pid, name: newNode.name, spid: newNode.spid}]);
  81. nodes[0].source_node = newNode;
  82. });
  83. }
  84. delFiling(node, callback) {
  85. if (node.file_count > 0) return;
  86. const parent = node.getParentNode();
  87. const self = this;
  88. postData(`${window.location.pathname}/update`, { updateType: 'del', id: node.id }, function(result) {
  89. self.dragTree.loadPostData(result);
  90. self.filingTree.removeNode(node);
  91. self.calcTotalFileCount();
  92. if (parent) {
  93. const path = parent.getPath();
  94. for (const p of path) {
  95. p.name = p.source_node.name + (p.source_node.total_file_count > 0 ? `(${p.source_node.total_file_count})` : '');
  96. filingObj.filingTree.updateNode(p);
  97. }
  98. }
  99. if (callback) callback();
  100. });
  101. }
  102. async renameFiling(node, newName) {
  103. const result = await postDataAsync(`${window.location.pathname}/update`, { updateType:'save', id: node.id, name: newName });
  104. node.source_node.name = newName;
  105. node.name = node.source_node.name + (node.source_node.total_file_count > 0 ? `(${node.source_node.total_file_count})` : '');
  106. return result;
  107. }
  108. async setCurFiling(node) {
  109. filingObj.curFiling = node;
  110. }
  111. moveFiling(node, tree_pid, tree_order, callback) {
  112. if (node.file_count > 0) return;
  113. if (tree_pid === node.source_node.tree_pid && tree_order === node.source_node.tree_order) return;
  114. const self = this;
  115. postData(`${window.location.pathname}/update`, { updateType: 'move', id: node.id, tree_pid, tree_order }, function(result) {
  116. const refresh = self.dragTree.loadPostData(result);
  117. self.calcTotalFileCount();
  118. const updated = [];
  119. for (const u of refresh.update) {
  120. if (!u) continue;
  121. const node = self.filingTree.getNodeByParam('id', u.id);
  122. if (node) {
  123. const path = node.getPath();
  124. for (const p of path) {
  125. if (updated.indexOf(p.id) >= 0) continue;
  126. p.name = p.source_node.name + (p.source_node.total_file_count > 0 ? `(${p.source_node.total_file_count})` : '');
  127. filingObj.filingTree.updateNode(p);
  128. updated.push(p.id);
  129. }
  130. }
  131. }
  132. if (callback) callback();
  133. });
  134. }
  135. batchUpdateFiling(data, callback) {
  136. const self = this;
  137. postData(`${window.location.pathname}/update`, data, function(result) {
  138. self.analysisFiling(result);
  139. if (callback) callback();
  140. })
  141. }
  142. }
  143. const levelTreeSetting = {
  144. treeId: 'filing',
  145. view: {
  146. selectedMulti: false,
  147. showIcon: false,
  148. },
  149. data: {
  150. simpleData: {
  151. idKey: 'id',
  152. pIdKey: 'tree_pid',
  153. rootPId: '-1',
  154. enable: true,
  155. }
  156. },
  157. edit: {
  158. enable: !readOnly,
  159. showRemoveBtn: !readOnly,
  160. showRenameBtn: !readOnly,
  161. renameTitle: '编辑',
  162. removeTitle: '删除',
  163. drag: {
  164. isCopy: false,
  165. isMove: false,
  166. pre: false,
  167. next: false,
  168. inner: false,
  169. },
  170. editNameSelectAll: true,
  171. },
  172. callback: {
  173. onClick: async function (e, key, node) {
  174. if (filingObj.curFiling && filingObj.curFiling.id === node.id) return;
  175. filingObj.setCurFiling(node);
  176. },
  177. beforeEditName: function(key, node) {
  178. node.name = node.source_node.name;
  179. },
  180. beforeRename: async function(key, node, newName, isCancel) {
  181. if (!isCancel) await filingObj.renameFiling(node, newName);
  182. return true;
  183. },
  184. beforeRemove: function(key, node, isCancel) {
  185. filingObj.deleteNode = node;
  186. $('#del-filing').modal('show');
  187. return false;
  188. },
  189. beforeDrop: function(key, nodes, target, moveType, isCopy) {
  190. if (readOnly) return false;
  191. if (!target) return false;
  192. const order = nodes[0].getIndex() + 1;
  193. const targetOrder = target.getIndex() + 1;
  194. const targetParent = target.getParentNode();
  195. const targetMax = targetParent ? targetParent.children.length : filingObj.dragTree.children.length;
  196. if (moveType === 'prev') {
  197. if (target.tree_pid === nodes[0].tree_pid) {
  198. if (targetOrder > order) {
  199. filingObj.moveFiling(nodes[0], target.tree_pid, targetOrder === 1 ? 1 : targetOrder - 1);
  200. } else {
  201. filingObj.moveFiling(nodes[0], target.tree_pid, targetOrder === 1 ? 1 : targetOrder);
  202. }
  203. } else {
  204. filingObj.moveFiling(nodes[0], target.tree_pid, targetOrder === 1 ? 1 : targetOrder);
  205. }
  206. } else if (moveType === 'next') {
  207. if (target.tree_pid === nodes[0].tree_pid) {
  208. if (targetOrder < order) {
  209. filingObj.moveFiling(nodes[0], target.tree_pid, targetOrder === targetMax ? targetMax : targetOrder + 1);
  210. } else {
  211. filingObj.moveFiling(nodes[0], target.tree_pid, targetOrder === targetMax ? targetMax : targetOrder);
  212. }
  213. } else {
  214. filingObj.moveFiling(nodes[0], target.tree_pid, targetOrder + 1);
  215. }
  216. } else if (moveType === 'inner') {
  217. filingObj.moveFiling(nodes[0], target.tree_id, targetMax + 1);
  218. }
  219. }
  220. }
  221. };
  222. const filingObj = new FilingObj(levelTreeSetting);
  223. filingObj.analysisFiling(filingData);
  224. $('#add-slibing').click(() => {
  225. if (!filingObj.curFiling) return;
  226. filingObj.addSiblingFiling(filingObj.curFiling);
  227. });
  228. $('#add-child').click(() => {
  229. if (!filingObj.curFiling) return;
  230. filingObj.addChildFiling(filingObj.curFiling);
  231. });
  232. $('#up-move').click(() => {
  233. if (!filingObj.curFiling) return;
  234. if (!filingObj.curFiling.getPreNode()) return;
  235. filingObj.moveFiling(filingObj.curFiling, filingObj.curFiling.tree_pid, filingObj.curFiling.source_node.tree_order - 1, function () {
  236. filingObj.filingTree.moveNode(filingObj.curFiling.getPreNode(), filingObj.curFiling, "prev", true);
  237. });
  238. });
  239. $('#down-move').click(() => {
  240. if (!filingObj.curFiling) return;
  241. if (!filingObj.curFiling.getNextNode()) return;
  242. filingObj.moveFiling(filingObj.curFiling, filingObj.curFiling.tree_pid, filingObj.curFiling.source_node.tree_order + 1, function () {
  243. filingObj.filingTree.moveNode(filingObj.curFiling.getNextNode(), filingObj.curFiling, "next", true);
  244. });
  245. });
  246. $('#del-filing-ok').click(() => {
  247. if (!filingObj.deleteNode) return;
  248. filingObj.delFiling(filingObj.deleteNode, function() {
  249. $('#del-filing').modal('hide');
  250. delete filingObj.deleteNode;
  251. });
  252. });
  253. class MultiObj {
  254. constructor(setting) {
  255. this.modal = $(`#${setting.modal}`);
  256. this.spread = SpreadJsObj.createNewSpread($(`#${setting.spread}`)[0]);
  257. this.sheet = this.spread.getActiveSheet();
  258. this.spreadSetting = {
  259. cols: [
  260. { title: '名称', colSpan: '1', rowSpan: '1', field: 'name', hAlign: 0, width: 250, formatter: '@', readOnly: true, cellType: 'tree' },
  261. { title: '文件数', colSpan: '1', rowSpan: '1', field: 'file_count', hAlign: 1, width: 50, readOnly: true },
  262. { title: '授权', colSpan: '1', rowSpan: '1', field: 'permission_count', hAlign: 1, width: 50, readOnly: true },
  263. { title: '固定', colSpan: '1', rowSpan: '1', field: 'is_fixed', hAlign: 1, width: 50, cellType: 'checkbox' },
  264. { title: '类型', colSpan: '1', rowSpan: '1', field: 'filing_type', hAlign: 1, width: 50, readOnly: true, visible: is_debug, },
  265. { title: '新类型', colSpan: '1', rowSpan: '1', field: 'new_filing_type', hAlign: 1, width: 50, readOnly: true, visible: is_debug, },
  266. { title: '归档单位', colSpan: '1', rowSpan: '1', field: 'file_company', hAlign: 0, width: 80, formatter: '@' },
  267. { title: '提示', colSpan: '1', rowSpan: '1', field: 'tips', hAlign: 0, width: 280, formatter: '@', wordWrap: 1 },
  268. ],
  269. emptyRows: 0,
  270. headRows: 1,
  271. headRowHeight: [32],
  272. defaultRowHeight: 21,
  273. headerFont: '12px 微软雅黑',
  274. font: '12px 微软雅黑',
  275. };
  276. SpreadJsObj.initSheet(this.sheet, this.spreadSetting);
  277. this.checkTree = createNewPathTree('base', {
  278. id: 'tree_id',
  279. pid: 'tree_pid',
  280. order: 'order',
  281. level: 'level',
  282. isLeaf: 'is_leaf',
  283. fullPath: 'full_path',
  284. rootId: -1,
  285. });
  286. const self = this;
  287. this.modal.bind('shown.bs.modal', function() {
  288. self.spread.refresh();
  289. });
  290. this.spread.bind(spreadNS.Events.ButtonClicked, function(e, info) {
  291. if (!info.sheet.zh_setting) return;
  292. const sheet = info.sheet, cellType = sheet.getCellType(info.row, info.col);
  293. if (cellType instanceof spreadNS.CellTypes.CheckBox) {
  294. if (sheet.isEditing()) sheet.endEdit(true);
  295. }
  296. const col = info.sheet.zh_setting.cols[info.col];
  297. if (col.field !== 'is_fixed') return;
  298. const tree = self.checkTree;
  299. const node = tree.nodes[info.row];
  300. if (node.level <= 1 && node.is_fixed) {
  301. toastr.warning('顶层节点不可取消固定');
  302. SpreadJsObj.reLoadRowsData(info.sheet, [info.row]);
  303. return;
  304. }
  305. const row = [];
  306. if (!node.is_fixed) {
  307. const relas = [];
  308. if (node.level > 1) {
  309. const parents = tree.getAllParents(node);
  310. for (const p of parents) {
  311. relas.push(...p.children);
  312. if (p.level === 1) relas.push(p);
  313. }
  314. } else {
  315. relas.push(node);
  316. }
  317. for (const p of relas) {
  318. p.is_fixed = true;
  319. row.push(tree.nodes.indexOf(p));
  320. }
  321. } else {
  322. const parent = tree.getParent(node);
  323. const posterity = tree.getPosterity(parent);
  324. for (const p of posterity) {
  325. p.is_fixed = false;
  326. row.push(tree.nodes.indexOf(p));
  327. }
  328. }
  329. SpreadJsObj.reLoadRowsData(info.sheet, row);
  330. });
  331. this.spread.bind(spreadNS.Events.EditEnded, function(e, info) {
  332. if (!info.sheet.zh_setting) return;
  333. const col = info.sheet.zh_setting.cols[info.col];
  334. const node = SpreadJsObj.getSelectObject(info.sheet);
  335. node[col.field] = trimInvalidChar(info.editingText);
  336. SpreadJsObj.reLoadRowData(info.sheet, info.row)
  337. });
  338. $(`#${setting.modal}-ok`).click(function() {
  339. try {
  340. const data = self.getMultiUpdateData();
  341. filingObj.batchUpdateFiling(data, function() {
  342. window.location.reload();
  343. });
  344. } catch(err) {
  345. toastr.error(err.stack ? '保存配置数据错误' : err);
  346. }
  347. });
  348. $(`#${setting.modal}-recalc`).click(function() {
  349. self.reCalcFilingType();
  350. SpreadJsObj.reLoadSheetData(self.sheet);
  351. });
  352. if (is_debug) {
  353. $(`#${setting.modal}-recalc`).show();
  354. } else {
  355. $(`#${setting.modal}-recalc`).hide();
  356. }
  357. }
  358. reCalcFilingType() {
  359. for (const node of this.checkTree.nodes) {
  360. delete node.new_filing_type;
  361. node.is_fixed = node.is_fixed ? 1 : 0;
  362. if (node.file_count > 0 && node.is_fixed !== node.org_is_fixed) {
  363. throw `【${node.name}】下已存在文件,不可修改是否为固定节点`;
  364. }
  365. if (node.permission_count > 0 && node.is_fixed !== node.org_is_fixed) {
  366. throw `【${node.name}】下已授权给用户,不可修改是否为固定节点`;
  367. }
  368. }
  369. const sftIndex = [];
  370. const getNewSft = function () {
  371. let i = 1;
  372. while(sftIndex[i]) {
  373. i++;
  374. }
  375. return i;
  376. };
  377. for (const node of this.checkTree.nodes) {
  378. if (node.file_count || node.permission_count) {
  379. node.new_filing_type = node.filing_type;
  380. } else if (node.is_fixed) {
  381. node.new_filing_type = getNewSft();
  382. } else {
  383. const parent = this.checkTree.getParent(node);
  384. if (!parent) return [false, '顶层节点必须为固定节点'];
  385. node.new_filing_type = parent.new_filing_type;
  386. }
  387. sftIndex[node.new_filing_type] = node;
  388. }
  389. }
  390. getMultiUpdateData() {
  391. this.reCalcFilingType();
  392. const data = [];
  393. const getUpdateData = function(children) {
  394. for (const [i, node] of children.entries()) {
  395. data.push({ id: node.id, is_fixed: node.is_fixed, filing_type: node.new_filing_type, tree_order: i + 1, tips: node.tips || '', file_company: node.file_company || '' });
  396. // data.push({ id: node.id, is_fixed: node.is_fixed, filing_type: node.filing_type, file_count: node.file_count, new_filing_type: node.new_filing_type, tree_order: i + 1, tips: node.tips || '' });
  397. if (node.children) getUpdateData(node.children);
  398. }
  399. };
  400. getUpdateData(this.checkTree.children);
  401. return {updateType: 'multi', data};
  402. }
  403. _convertData(sourceTree) {
  404. const data = [];
  405. for (const node of sourceTree.nodes) {
  406. const parent = node.tree_pid === '-1' ? undefined : data.find(x => { return x.id === node.tree_pid; });
  407. const child = sourceTree.nodes.find(x => { return x.tree_pid === node.id; });
  408. data.push({
  409. id: node.id,
  410. tree_id: data.length + 1,
  411. tree_pid: parent ? parent.tree_id : -1,
  412. order: node.tree_order + 1,
  413. level: node.tree_level,
  414. is_leaf: !child,
  415. full_path: '',
  416. name: node.name,
  417. org_is_fixed: node.is_fixed,
  418. is_fixed: node.is_fixed,
  419. filing_type: node.filing_type,
  420. file_company: node.file_company,
  421. tips: node.tips,
  422. file_count: node.file_count,
  423. permission_count: node.permission_count,
  424. });
  425. }
  426. return data;
  427. }
  428. calculateFileCount(arr) {
  429. let count = 0;
  430. for (const a of arr) {
  431. if (a.children && a.children.length > 0) a.file_count = this.calculateFileCount(a.children);
  432. count = count + a.file_count;
  433. }
  434. return count;
  435. }
  436. reload(sourceTree) {
  437. this.checkTree.loadDatas(this._convertData(sourceTree));
  438. this.calculateFileCount(this.checkTree.children);
  439. SpreadJsObj.loadSheetData(this.sheet, SpreadJsObj.DataType.Tree, this.checkTree);
  440. }
  441. show() {
  442. this.modal.modal('show');
  443. }
  444. }
  445. let multiObj = new MultiObj({ modal: 'multi-set', spread: 'multi-spread' });
  446. $('#multi-setting').click(() => {
  447. multiObj.reload(filingObj.dragTree);
  448. multiObj.show();
  449. });
  450. });