sub_project.js 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471
  1. const NameChange = function (obj) {
  2. if (obj.value.length > 100) {
  3. obj.classList.add('is-invalid');
  4. } else {
  5. obj.classList.remove('is-invalid');
  6. }
  7. };
  8. $(document).ready(function() {
  9. const projectTreeObj = (function(setting){
  10. const ProjectTree = createDragTree(setting.treeSetting);
  11. ProjectTree.loadDatas(setting.source);
  12. const TableObj = $(setting.table);
  13. const TableHeaderObj = $(setting.tableHeader);
  14. let tenderTreeShowLevel;
  15. let colSetCache;
  16. const Utils = {
  17. generateColSetCache: function() {
  18. const result = {};
  19. colSet.forEach(x => {
  20. result[x.field] = { show: x.show, alias: x.alias || x.name };
  21. });
  22. return result;
  23. },
  24. getHeaderHtml: function() {
  25. colSetCache = this.generateColSetCache();
  26. const html = [];
  27. html.push('<tr class="text-center">');
  28. if (colSetCache.name.show) html.push('<th style="min-width: 300px">', colSetCache.name.alias, '</th>');
  29. if (colSetCache.budget_std.show) html.push('<th width="8%">', colSetCache.budget_std.alias, '</th>');
  30. if (colSetCache.proj_status.show) html.push('<th width="5%">', colSetCache.proj_status.alias || '-', '</th>');
  31. if (colSetCache.tender_count.show) html.push('<th width="5%">', colSetCache.tender_count.alias, '</th>');
  32. if (colSetCache.file_count.show) html.push('<th width="5%">', colSetCache.file_count.alias, '<i class="fa fa-question-circle text-primary" data-placement="bottom" data-toggle="tooltip" data-original-title="指资料归集的所有文件个数"></i>', '</th>');
  33. if (colSetCache.contract_count1.show) html.push('<th width="5%">', colSetCache.contract_count1.alias, '<i class="fa fa-question-circle text-primary" data-placement="bottom" data-toggle="tooltip" data-original-title="项目支出合同个数"></i>', '</th>');
  34. if (colSetCache.management.show) html.push('<th width="20%">', colSetCache.management.alias, '</th>');
  35. if (colSetCache.create_time.show) html.push('<th width="8%">', colSetCache.create_time.alias, '</th>');
  36. if (colSetCache.control.show && canEdit) html.push('<th width="20%">', colSetCache.control.alias, '</th>');
  37. html.push('</tr>');
  38. console.log(html);
  39. return html.join('');
  40. },
  41. getRowTdHtml: function (node, tree) {
  42. const html = [];
  43. // 名称
  44. if (colSetCache.name.show) {
  45. html.push('<td width="20%" class="in-' + node.tree_level + '">');
  46. if (node.is_folder) {
  47. if (node.children.length > 0) {
  48. html.push('<span onselectstart="return false" style="{-moz-user-select:none}" class="fold-switch mr-1" title="收起" id="'+ node.id +'"><i class="fa fa-minus-square-o"></i></span> <i class="fa fa-folder-o"></i> ', node.name);
  49. } else {
  50. html.push('<i class="fa fa-folder-o"></i> ', node.name);
  51. }
  52. } else {
  53. html.push(`<span class="text-muted mr-2">${tree.isLastSibling(node) ? '└' : '├'}</span>`);
  54. html.push(`<a href="sp/${node.id}/dashboard" name="name" id="${node.id}">`, node.name, '</a>');
  55. }
  56. html.push('</td>');
  57. }
  58. // 概预算标准
  59. if (colSetCache.budget_std.show) {
  60. if (node.is_folder) {
  61. html.push(`<td class="text-center"></td>`);
  62. } else {
  63. if (node.std_name) {
  64. html.push(`<td class="text-center">${node.std_name}</td>`);
  65. } else if (canEdit) {
  66. html.push(`<td class="text-center"><button class="btn btn-outline-primary btn-sm ml-1" name="set-std">选择</button></td>`);
  67. } else {
  68. html.push(`<td class="text-center"></td>`);
  69. }
  70. }
  71. }
  72. // 建设状态
  73. if (colSetCache.proj_status.show) {
  74. if (node.is_folder) {
  75. html.push(`<td class="text-center"></td>`);
  76. } else {
  77. html.push(`<td class="text-center">${node.proj_status}</td>`);
  78. }
  79. }
  80. // 创建时间
  81. if (node.is_folder) {
  82. if (colSetCache.tender_count.show) html.push(`<td class="text-center"></td>`);
  83. if (colSetCache.file_count.show) html.push(`<td class="text-center"></td>`);
  84. if (colSetCache.contract_count1.show) html.push(`<td class="text-center"></td>`);
  85. if (colSetCache.management.show) html.push(`<td class="text-center"></td>`);
  86. if (colSetCache.create_time.show) html.push(`<td class="text-center"></td>`);
  87. } else {
  88. // 标段个数
  89. if (colSetCache.tender_count.show) html.push(`<td class="text-center">${node.tender_count}</td>`);
  90. // 文件个数
  91. if (colSetCache.file_count.show) html.push(`<td class="text-center">${node.file_count}</td>`);
  92. // 合同个数
  93. if (colSetCache.contract_count1.show) html.push(`<td class="text-center">${node.contract_count1}</td>`);
  94. if (colSetCache.management.show) {
  95. if (canEdit) {
  96. html.push(`<td class="text-center">${node.management || ''}<a class="ml-2" href="javascript: void(0)" name="set-management"><i class="fa fa-pencil-square-o "></i></a>${node.management ? '<a class="ml-2" href="javascript: void(0)" name="refresh-management"><i class="fa fa-refresh "></i></a>': ''}</td>`);
  97. } else {
  98. html.push(`<td class="text-center">${node.management || ''}</td>`);
  99. }
  100. }
  101. if (colSetCache.create_time.show) html.push(`<td class="text-center">${moment(node.create_time).format('YYYY-MM-DD')}</td>`);
  102. }
  103. // 操作
  104. if (canEdit && colSetCache.control.show) {
  105. html.push(`<td>`);
  106. html.push('<button class="btn btn-outline-primary btn-sm ml-1" name="edit">编辑</button>');
  107. html.push('<button class="btn btn-outline-primary btn-sm ml-1" name="up"><i class="fa fa-arrow-up"></i></button>');
  108. html.push('<button class="btn btn-outline-primary btn-sm ml-1" name="down"><i class="fa fa-arrow-down"></i></button>');
  109. html.push('<button class="btn btn-outline-primary btn-sm ml-1" name="top">顶层</button>');
  110. if (node.is_folder) {
  111. if (!node.children || node.children.length === 0) {
  112. html.push('<button class="btn btn-outline-danger btn-sm ml-1" name="del">删除</button>');
  113. }
  114. } else {
  115. const delHint = node.tender_count > 0 ? '请先删除所有标段' : (node.contract_count > 0 ? '请先删除所有合同' : (node.file_count > 0 ? '请先删除所有资料归集文件' : ''));
  116. if (delHint) {
  117. html.push(`<button class="btn btn-outline-secondary btn-sm ml-1" data-toggle="tooltip" data-placement="top" data-original-title="${delHint}" name="del">删除</button>`);
  118. } else {
  119. html.push('<button class="btn btn-outline-danger btn-sm ml-1" name="del">删除</button>');
  120. }
  121. }
  122. html.push('</td>');
  123. }
  124. return html.join('');
  125. },
  126. getNodeTrHtml: function (node, tree) {
  127. const html = [];
  128. html.push(`<tr tree_id="${node.id}" draggable="true">`);
  129. html.push(Utils.getRowTdHtml(node, tree));
  130. html.push(`</tr>`);
  131. return html.join('');
  132. },
  133. reloadTable: function () {
  134. TableHeaderObj.html(Utils.getHeaderHtml());
  135. const html = [];
  136. for (const node of ProjectTree.nodes) {
  137. html.push(Utils.getNodeTrHtml(node, ProjectTree));
  138. }
  139. TableObj.html(html.join(''));
  140. },
  141. getSelectNode: function() {
  142. const selectId = $('tr.table-active').attr('tree_id');
  143. return selectId ? ProjectTree.getItems(selectId) : null;
  144. },
  145. getSelectNodeId: function() {
  146. const selectId = $('tr.table-active').attr('tree_id');
  147. return selectId || setting.treeSetting.rootId;
  148. },
  149. refreshAddButton: function() {
  150. const select = this.getSelectNode();
  151. // $('[href="#add-folder"]').attr("disabled", select && (select.tree_level >= 4 || !select.is_folder));
  152. if (select && (select.tree_level >= 4 || !select.is_folder)) {
  153. $('[href="#add-folder"]').hide();
  154. } else {
  155. $('[href="#add-folder"]').show();
  156. }
  157. if (select && !select.is_folder) {
  158. $('[href="#add-project"]').hide();
  159. } else {
  160. $('[href="#add-project"]').show();
  161. }
  162. },
  163. refreshTreeTable: function(result) {
  164. ProjectTree.loadDatas(result);
  165. if (ProjectTree.nodes.length > 0 && $('#no-project').length > 0) window.location.reload();
  166. Utils.reloadTable();
  167. },
  168. refreshRow: function(result) {
  169. const refreshData = ProjectTree.loadPostData(result);
  170. if (!refreshData.update) return;
  171. for (const u of refreshData.update) {
  172. $(`tr[tree_id=${u.id}]`).html(Utils.getRowTdHtml(u, ProjectTree));
  173. }
  174. },
  175. dropTo: function() {
  176. if (!Utils.dropNode.is_folder) {
  177. toastr.warning('请将文件夹或者项目拖动到目标文件夹下');
  178. return;
  179. }
  180. const parents = ProjectTree.getAllParents(Utils.dropNode);
  181. const existParent = parents.find(x => { return x.id === Utils.dragNode.id; });
  182. if (existParent) {
  183. toastr.error('请勿将文件夹移动到子文件夹下');
  184. return;
  185. }
  186. const posterity = ProjectTree.getPosterity(Utils.dragNode);
  187. const maxLevelFolder = posterity.reduce((prev, cur) => { return cur.is_folder ? Math.max(cur.tree_level, prev) : prev; }, Utils.dragNode.tree_level);
  188. const resultMaxLevelFolder = Utils.dropNode.tree_level + maxLevelFolder - Utils.dragNode.tree_level + 1;
  189. if (resultMaxLevelFolder > 4) {
  190. toastr.warning('文件夹不可超过4层');
  191. return;
  192. }
  193. postData('/subproj/dragTo', { drag_id: Utils.dragNode.id, drop_id: Utils.dropNode.id }, function (result){
  194. Utils.refreshTreeTable(result);
  195. });
  196. delete Utils.dragNode;
  197. delete Utils.dropNode;
  198. },
  199. move: function(node, type) {
  200. postData('/subproj/move', { id: node.id, type }, function (result) {
  201. Utils.refreshTreeTable(result);
  202. });
  203. },
  204. expandByLevel: function(level){
  205. ProjectTree.expandByLevel(level);
  206. for (const node of ProjectTree.nodes) {
  207. const tr = $(`tr[tree_id=${node.id}]`);
  208. if (node.expanded) {
  209. $('.fold-switch', tr).html(`<i class="fa fa-minus-square-o"></i>`);
  210. } else {
  211. $('.fold-switch', tr).html(`<i class="fa fa-plus-square-o"></i>`);
  212. }
  213. if (node.visible) {
  214. tr.show();
  215. } else {
  216. tr.hide();
  217. }
  218. }
  219. }
  220. };
  221. Utils.reloadTable();
  222. $('body').on('click', 'tr[tree_id]', function() {
  223. if ($(this).hasClass('table-active')) {
  224. $(this).removeClass('table-active');
  225. } else {
  226. $('tr[tree_id].table-active').removeClass('table-active');
  227. $(this).addClass('table-active');
  228. }
  229. Utils.refreshAddButton();
  230. });
  231. $('body').on('dragstart', 'tr[tree_id]', function(e) {
  232. Utils.dragNode = ProjectTree.getItems(e.target.getAttribute('tree_id'));
  233. });
  234. $('body').on('drop', 'tr[tree_id]', function(e) {
  235. Utils.dropNode = ProjectTree.getItems(e.currentTarget.getAttribute('tree_id'));
  236. Utils.dropTo();
  237. });
  238. $('body').on('dragover', 'tr[tree_id]', function(e) {
  239. const parent = ProjectTree.getItems(e.currentTarget.getAttribute('tree_id'));
  240. return !parent || !parent.is_folder || parent.tree_level > 3 || parent.id === Utils.dragNode.id;
  241. });
  242. $('body').on('click', 'button[name=del]', function(e) {
  243. const treeId = $(this).parent().parent().attr('tree_id');
  244. const node = ProjectTree.getItems(treeId);
  245. if (node.is_folder) {
  246. if (node.children && node.children.length > 0) return;
  247. $('.modal-title', '#del').html('确认删除文件夹');
  248. $('#del-hint').attr('tree_id', treeId).html(`确定删除「<strong style="word-break: break-word;">${node.name}</strong>」及其下所有内容?`);
  249. $('#del').modal('show');
  250. } else {
  251. if (node.tender_count || node.file_count || node.contract_count) return;
  252. $('.modal-title', '#del').html('确认删除项目');
  253. $('#del-hint').attr('tree_id', treeId).html(`确定删除「<strong style="word-break: break-word;">${node.name}</strong>」?`);
  254. $('#del').modal('show');
  255. }
  256. });
  257. $('body').on('click', 'button[name=up]', function (e) {
  258. const treeId = $(this).parent().parent().attr('tree_id');
  259. const node = ProjectTree.getItems(treeId);
  260. if (!ProjectTree.getPreSiblingNode(node)) {
  261. toastr.warning('不可上移');
  262. return;
  263. }
  264. Utils.move(node, 'up');
  265. });
  266. $('body').on('click', 'button[name=down]', function (e) {
  267. const treeId = $(this).parent().parent().attr('tree_id');
  268. const node = ProjectTree.getItems(treeId);
  269. if (!ProjectTree.getNextSiblingNode(node)) {
  270. toastr.warning('不可下移');
  271. return;
  272. }
  273. Utils.move(node, 'down');
  274. });
  275. $('body').on('click', 'button[name=top]', function (e) {
  276. const treeId = $(this).parent().parent().attr('tree_id');
  277. const node = ProjectTree.getItems(treeId);
  278. if (!ProjectTree.getParent(node)) {
  279. toastr.warning('已是顶层节点');
  280. return;
  281. }
  282. Utils.move(node, 'top');
  283. });
  284. $('body').on('click', 'button[name=edit]', function(e) {
  285. const treeId = $(this).parent().parent().attr('tree_id');
  286. const node = ProjectTree.getItems(treeId);
  287. $('#edit-project-name').attr('tree_id', treeId).val(node.name);
  288. $('#edit-project').modal('show');
  289. });
  290. $('body').on('click', 'button[name=set-std]', function(e) {
  291. const treeId = $(this).parent().parent().attr('tree_id');
  292. const node = ProjectTree.getItems(treeId);
  293. if (node.is_folder || node.std_id) return;
  294. $('[name=std_id]').attr('tree_id', treeId);
  295. $('#set-std').modal('show');
  296. });
  297. $('body').on('click', 'a[name=set-management]', function(e) {
  298. const treeId = $(this).parent().parent().attr('tree_id');
  299. const node = ProjectTree.getItems(treeId);
  300. if (node.is_folder) return;
  301. $('#sm-management').attr('tree_id', treeId);
  302. $('#sm-management').val(node.management);
  303. if (node.filing_template_id) {
  304. const ft = $(`[value=${node.filing_template_id}]`)[0];
  305. if (ft) $('#sm-ft')[0].selectedIndex = Array.prototype.indexOf.call(ft.parentNode.children, ft);
  306. }
  307. //$('#sm-ft').val(node.filing_template_name);
  308. $('#sm-ft').attr('disabled', !!node.management);
  309. $('#set-management').modal('show');
  310. });
  311. $('body').on('click', 'a[name=refresh-management]', function(e) {
  312. const treeId = $(this).parent().parent().attr('tree_id');
  313. const node = ProjectTree.getItems(treeId);
  314. if (node.is_folder) return;
  315. postData('/subproj/refresh', { id: treeId }, function(result) {
  316. toastr.success(`更新${result.um + result.dm + result.im}条用户权限`);
  317. });
  318. });
  319. $('body').on('click', '.fold-switch', function() {
  320. const id = this.getAttribute('id');
  321. const node = ProjectTree.getItems(id);
  322. ProjectTree.setExpanded(node, !node.expanded);
  323. const posterity = ProjectTree.getPosterity(node);
  324. if (node.expanded) {
  325. $(this).html(`<i class="fa fa-minus-square-o"></i>`);
  326. } else {
  327. $(this).html(`<i class="fa fa-plus-square-o"></i>`);
  328. }
  329. for (const p of posterity) {
  330. if (p.visible) {
  331. $(`tr[tree_id=${p.id}]`).show();
  332. } else {
  333. $(`tr[tree_id=${p.id}]`).hide();
  334. }
  335. }
  336. });
  337. const getChildrenLevel = function (node) {
  338. let iLevel = node.tree_level || 1;
  339. if (node.children && node.children.length > 0) {
  340. for (const c of node.children) {
  341. iLevel = Math.max(iLevel, getChildrenLevel(c));
  342. }
  343. }
  344. return iLevel;
  345. };
  346. tenderTreeShowLevel = $.cs_showLevel({
  347. selector: '#show-level',
  348. levels: [
  349. {
  350. type: 'sort', count: 5, visible_count: function () {
  351. return ProjectTree.children.map(getChildrenLevel).reduce((x, y) => { return Math.max(x, y); }, 0) - 1;
  352. }
  353. },
  354. {
  355. type: 'last', title: '最底层', visible: function () {
  356. const count = ProjectTree.children.map(getChildrenLevel).reduce((x, y) => { return Math.max(x, y); }, 0) - 1;
  357. return count > 0;
  358. }
  359. },
  360. ],
  361. showLevel: function (tag) {
  362. switch (tag) {
  363. case "1":
  364. case "2":
  365. case "3":
  366. case "4":
  367. case "5":
  368. Utils.expandByLevel(parseInt(tag));
  369. break;
  370. case "last":
  371. Utils.expandByLevel(20);
  372. break;
  373. default: return;
  374. }
  375. }
  376. });
  377. tenderTreeShowLevel.initShowLevel();
  378. tenderTreeShowLevel.refreshMenuVisible();
  379. return { ProjectTree, TableObj, ...Utils };
  380. })({
  381. treeSetting: { id: 'id', pid: 'tree_pid', level: 'tree_level', order: 'tree_order', rootId: '-1' },
  382. source: projectList,
  383. table: '#projectList',
  384. tableHeader: '#projectListHeader',
  385. });
  386. $('#add-folder').on('show.bs.modal', function(e) {
  387. const select = projectTreeObj.getSelectNode();
  388. if (select) {
  389. if (select.is_folder) e.stopPropagation();
  390. $('#folder-hint').html(`在文件夹 [${select.name}] 下新增`).show();
  391. } else {
  392. $('#folder-hint').hide();
  393. }
  394. $('#folder-name').val('');
  395. });
  396. $('#add-folder-ok').click(function() {
  397. const tree_pid = projectTreeObj.getSelectNodeId();
  398. const name = $('#folder-name').val();
  399. if (!name || name.length > 100) return;
  400. postData('/subproj/addFolder', { name, is_folder: 1, tree_pid }, function(result) {
  401. projectTreeObj.refreshTreeTable(result);
  402. $('#add-folder').modal('hide');
  403. });
  404. });
  405. $('#add-project').on('show.bs.modal', function(e) {
  406. const select = projectTreeObj.getSelectNode();
  407. if (select) {
  408. if (select.is_folder) e.stopPropagation();
  409. $('#project-hint').html(`在文件夹 [${select.name}] 下新增`).show();
  410. } else {
  411. $('#project-hint').hide();
  412. }
  413. $('#project-name').val('');
  414. });
  415. $('#add-project-ok').click(function() {
  416. const tree_pid = projectTreeObj.getSelectNodeId();
  417. const name = $('#project-name').val();
  418. if (!name || name.length > 100) return;
  419. postData('/subproj/addProj', { name, is_folder: 1, tree_pid }, function(result) {
  420. projectTreeObj.refreshTreeTable(result);
  421. $('#add-project').modal('hide');
  422. });
  423. });
  424. $('#del-ok').click(function() {
  425. const id = $('#del-hint').attr('tree_id');
  426. postData('/subproj/del', { id }, function(result) {
  427. projectTreeObj.refreshTreeTable(result);
  428. $('#del').modal('hide');
  429. $('#del-hint').attr('tree_id', '');
  430. });
  431. });
  432. $('#edit-project-ok').click(function() {
  433. const nameObj = $('#edit-project-name');
  434. const name = nameObj.val();
  435. if (!name || name.length > 100) return;
  436. postData('/subproj/save', { id: nameObj.attr('tree_id'), name }, function(result) {
  437. projectTreeObj.refreshRow(result);
  438. $('#edit-project').modal('hide');
  439. $('#edit-project-name').attr('tree_id', '');
  440. });
  441. });
  442. $('#set-std-ok').click(function() {
  443. const select = $('[name=std_id]:checked');
  444. const id = select.attr('tree_id');
  445. const std_id = parseInt(select.val());
  446. postData('/subproj/save', { id, std_id }, function(result) {
  447. projectTreeObj.refreshRow(result);
  448. $('#set-std').modal('hide');
  449. $('[name=std_id]').attr('tree_id', '');
  450. });
  451. });
  452. $('#set-management-ok').click(function() {
  453. const select = $('#sm-management');
  454. const id = select.attr('tree_id');
  455. const management = select.val();
  456. const filingTemplate =$('#sm-ft').val();
  457. postData('/subproj/save', { id, management, filingTemplate }, function(result) {
  458. projectTreeObj.refreshRow(result);
  459. $('#set-management').modal('hide');
  460. $('#sm-management').attr('tree_id', '');
  461. });
  462. });
  463. });