$(document).ready(function() { autoFlashHeight(); const projectTreeObj = (function(setting){ const ProjectTree = createNewPathTree('revise', setting.treeSetting); ProjectTree.loadDatas(setting.source); treeCalc.calculateAll(ProjectTree); const TableObj = $(setting.table); let tenderTreeShowLevel; const Utils = { getProgressHtml: function(total, yf, title = '') { if (total) { let yfP = ZhCalc.mul(ZhCalc.div(yf, total, 2), 100, 0); let other = Math.max(ZhCalc.sub(total, yf), 0); let otherP = Math.max(100 - yfP, 0); const html = '
' + '
' + yfP + '%
' + '
' + otherP + '%
' + '
'; return html; } else { return ''; } }, getRowTdHtml: function (node, tree) { const html = []; // 名称 html.push(''); if (node.is_folder) { if (node.children.length > 0) { html.push(' ', node.name); } else { html.push(' ', node.name); } } else { html.push(`${tree.isLastSibling(node) ? '└' : '├'}`); html.push('', node.name, ''); } html.push(''); // 创建时间 if (node.is_folder) { html.push(``); } else { html.push(`${moment(node.create_time).format('YYYY-MM-DD')}`); } html.push(`${node.expenses_count || ''} ${node.expenses_total_price || ''} ${Utils.getProgressHtml(node.expenses_total_price, node.expenses_yf_price, '累计应付')} ${node.income_count || ''} ${node.income_total_price || ''} ${Utils.getProgressHtml(node.income_total_price, node.income_yf_price, '累计应回')} `); // 操作 if (is_admin) { html.push(``); if (!node.is_folder) { html.push(` 成员管理`); } html.push(''); } return html.join(''); }, getNodeTrHtml: function (node, tree) { const html = []; html.push(``); html.push(Utils.getRowTdHtml(node, tree)); html.push(``); return html.join(''); }, reloadTable: function () { const html = []; for (const node of ProjectTree.nodes) { html.push(Utils.getNodeTrHtml(node, ProjectTree)); } TableObj.html(html.join('')); }, getSelectNode: function() { const selectId = $('tr.table-active').attr('tree_id'); return selectId ? ProjectTree.getItems(selectId) : null; }, getSelectNodeId: function() { const selectId = $('tr.table-active').attr('tree_id'); return selectId || setting.treeSetting.rootId; }, refreshTreeTable: function(result) { ProjectTree.loadDatas(result); if (ProjectTree.nodes.length > 0 && $('#no-project').length > 0) window.location.reload(); Utils.reloadTable(); }, refreshRow: function(result) { const refreshData = ProjectTree.loadPostData(result); if (!refreshData.update) return; for (const u of refreshData.update) { $(`tr[tree_id=${u.id}]`).html(Utils.getRowTdHtml(u, ProjectTree)); } }, expandByLevel: function(level){ ProjectTree.expandByLevel(level); for (const node of ProjectTree.nodes) { const tr = $(`tr[tree_id=${node.id}]`); if (node.expanded) { $('.fold-switch', tr).html(``); } else { $('.fold-switch', tr).html(``); } if (node.visible) { tr.show(); } else { tr.hide(); } } } }; Utils.reloadTable(); $('body').on('click', 'tr[tree_id]', function() { if ($(this).hasClass('table-active')) { $(this).removeClass('table-active'); } else { $('tr[tree_id].table-active').removeClass('table-active'); $(this).addClass('table-active'); } // Utils.refreshAddButton(); }); $('body').on('dragstart', 'tr[tree_id]', function(e) { Utils.dragNode = ProjectTree.getItems(e.target.getAttribute('tree_id')); }); $('body').on('drop', 'tr[tree_id]', function(e) { Utils.dropNode = ProjectTree.getItems(e.currentTarget.getAttribute('tree_id')); // Utils.dropTo(); }); $('body').on('dragover', 'tr[tree_id]', function(e) { const parent = ProjectTree.getItems(e.currentTarget.getAttribute('tree_id')); return !parent || !parent.is_folder || parent.tree_level > 3 || parent.id === Utils.dragNode.id; }); $('body').on('click', '.fold-switch', function() { const id = this.getAttribute('id'); const node = ProjectTree.getItems(id); ProjectTree.setExpanded(node, !node.expanded); const posterity = ProjectTree.getPosterity(node); if (node.expanded) { $(this).html(``); } else { $(this).html(``); } for (const p of posterity) { if (p.visible) { $(`tr[tree_id=${p.id}]`).show(); } else { $(`tr[tree_id=${p.id}]`).hide(); } } }); const getChildrenLevel = function (node) { let iLevel = node.tree_level || 1; if (node.children && node.children.length > 0) { for (const c of node.children) { iLevel = Math.max(iLevel, getChildrenLevel(c)); } } return iLevel; }; tenderTreeShowLevel = $.cs_showLevel({ selector: '#show-level', levels: [ { type: 'sort', count: 5, visible_count: function () { return ProjectTree.children.map(getChildrenLevel).reduce((x, y) => { return Math.max(x, y); }, 0) - 1; } }, { type: 'last', title: '最底层', visible: function () { const count = ProjectTree.children.map(getChildrenLevel).reduce((x, y) => { return Math.max(x, y); }, 0) - 1; return count > 0; } }, ], showLevel: function (tag) { switch (tag) { case "1": case "2": case "3": case "4": case "5": Utils.expandByLevel(parseInt(tag)); break; case "last": Utils.expandByLevel(20); break; default: return; } } }); tenderTreeShowLevel.initShowLevel(); tenderTreeShowLevel.refreshMenuVisible(); return { ProjectTree, TableObj, ...Utils }; })({ treeSetting: { id: 'id', pid: 'tree_pid', level: 'tree_level', order: 'tree_order', rootId: '-1', keys: ['id', 'project_id'], calcFields: ['expenses_count', 'expenses_total_price', 'expenses_yf_price', 'income_count', 'income_total_price', 'income_yf_price'], }, source: projectList, table: '#projectList', }); });