'use strict'; /** * * * @author Mai * @date 2019/3/7 * @version */ const EmptyTenderHtml = [ '
', '

还没有标段数据

', '
' ]; // levelTree - setting const levelTreeSetting = { view: { selectedMulti: false }, data: { simpleData: { idKey: 'lid', pIdKey: 'lpId', rootPId: 0, enable: true, } }, edit: { enable: true, showRemoveBtn: false, showRenameBtn: false, drag: { autoExpandTrigger: true, isCopy: false, isMove: true, prev: false, next: false, inner: true, } }, callback: { beforeDrop: beforeDropNode, onDrop: onDropNode, } }; const levelNodes =[]; const tenderTree = []; let parentId = 0; function createTree() { const zTree = $.fn.zTree.getZTreeObj('treeLevel'); if (zTree) { zTree.destroy(); } $.fn.zTree.init($("#treeLevel"), levelTreeSetting, levelNodes); } function beforeDropNode(treeId, treeNodes, targetNode, moveType, isCopy) { if (targetNode !== null && targetNode.lid !== 1) { const parent = targetNode.getParentNode(); if (parent && parent.lid === 1) { return false; } } for (var i=0,l=treeNodes.length; i= 2) { for (const c of targetNode.children) { if (c.lid !== treeNodes[0].lid) { zTree.moveNode(treeNodes[0], c, 'inner'); } } } } } // 查询方法 function findNode (key, value, arr) { for (const a of arr) { if (a[key] && a[key] === value) { return a; } } } function getPId(level) { if (level !== 1) { const p = findNode('level', level - 1, levelNodes); if (p) { return p.lid } else { return 1; } } else { return 2; } } // 分类数据排序 function sortCategory() { category.sort(function (a, b) { return a.level ? (b.level ? a.level - b.level : -1) : a.id - b.id; }); } // 初始化分类树结构数据 function initCategoryLevelNode() { levelNodes.splice(0, levelNodes.length); levelNodes.push( { lid:1, lpId:0, name:"可用类别", open:true, isParent: true, drag: false}, { lid:2, lpId:0, name:"已用类别", open:true, isParent: true, drag: false} ); for (const c of category) { const cate = JSON.parse(JSON.stringify(c)); cate.lid = levelNodes.length + 1; cate.open = true; cate.dropRoot = false; if (!cate.level) { cate.lpId = 1; levelNodes.push(cate); } else { cate.lpId = getPId(cate.level); levelNodes.push(cate); } } } // 新建标段 -- 分类属性选择 function getCategoryHtml() { function getSelectCategoryHtml (cate) { const html = []; html.push('
'); html.push(''); html.push(''); html.push('
'); return html.join(''); } function getRadioCategoryHtml (cate) { const html = []; html.push('
'); html.push('', cate.name, ''); html.push('
'); for (const iV in cate.value) { const v = cate.value[iV]; html.push('
'); html.push(''); html.push(''); html.push('
'); } html.push('
'); html.push('
'); return html.join(''); } const html = []; for (const c of category) { // if (c.type === categoryType.key.dropDown) { html.push(getSelectCategoryHtml(c)); // } else if (c.type === categoryType.key.radio) { // html.push(getRadioCategoryHtml(c)); // } } return html.join(''); } // 初始化TenderTree数据 function initTenderTree () { const levelCategory = category.filter(function (c) { return c.level && c.level > 0; }); function findCategoryNode(cid, value, array) { for (const a of array) { if (a.cid === cid && a.vid === value) { return a; } } } function getCategoryNode(category, value, parent, i = null) { const array = parent ? parent.children : tenderTree; let cate = findCategoryNode(category.id, value, array); if (!cate) { const cateValue = findNode('id', value, category.value); if (!cateValue) return null; cate = { cid: category.id, vid: value, name: cateValue.value, children: [], level: i ? i : category.level, sort_id: ++parentId, }; array.push(cate); } return cate; } function loadTenderCategory (tender) { let tenderCategory = null; for (const [index,lc] of levelCategory.entries()) { const tenderCate = findNode('cid', lc.id, tender.category); if (tenderCate) { tenderCategory = getCategoryNode(lc, tenderCate.value, tenderCategory); } else { if (index === 0 && tender.category) { for (const [i,c] of tender.category.entries()) { const cate = findNode('id', c.cid, category); tenderCategory = getCategoryNode(cate, c.value, tenderCategory, i+1); } } return tenderCategory; } } return tenderCategory; } tenderTree.splice(0, tenderTree.length); for (const t of tenders) { t.valid = true; delete t.level; if (t.category && levelCategory.length > 0) { const parent = loadTenderCategory(t); if (parent) { t.level = parent.level + 1; parent.children.push(t); } else { tenderTree.push(t); } } else { tenderTree.push(t); } } } function recursiveGetTenderNodeHtml (node, arr, pid) { const html = []; html.push(''); // 名称 html.push(''); if (node.cid) { html.push(' ', node.name); } else { html.push(''); html.push(arr.indexOf(node) === arr.length - 1 ? '└' : '├'); html.push(''); html.push('', node.name, ''); } html.push(''); // 创建人 html.push('', node.user_name, ''); // 创建时间 html.push('', node.create_time ? moment(node.create_time).format('YYYY-MM-DD HH:mm:ss') : '', ''); // 完成期数 html.push(''); if (!node.cid) { html.push(node.lastStage ? '第' + node.lastStage.order + '期' : '第0期'); } html.push(''); // 管理 html.push(''); if (!node.cid) { html.push('编辑'); if (node.lastStage === null || node.lastStage === undefined) { html.push('删除'); } else { html.push(''); } } html.push(''); html.push(''); if (node.children) { for (const c of node.children) { html.push(recursiveGetTenderNodeHtml(c, node.children, node.sort_id)); } } return html.join(''); } function getTenderTreeHeaderHtml() { const html = []; html.push('', ''); html.push('', '名称', ''); html.push('', '创建人', ''); html.push('', '创建时间', ''); html.push('', '完成期数', ''); html.push('', '管理', ''); html.push('', ''); return html.join(''); } // 根据TenderTree数据获取Html代码 function getTenderTreeHtml () { if (tenderTree.length > 0) { const html = []; html.push(''); html.push(getTenderTreeHeaderHtml()); for (const t of tenderTree) { html.push(recursiveGetTenderNodeHtml(t, tenderTree, '')); } html.push('
'); return html.join(''); } else { return EmptyTenderHtml.join(''); } } function bindTenderUrl() { // 打开标段 $('body').on('click', '.c-body a[name=name]', function () { const tenderId = parseInt($(this).attr('id')); const tender = _.find(tenders, function (t) { return t.id === tenderId; }); if (tender.measure_type) { window.location.href = '/tender/' + tenderId; } else { for (const a of $('a', '#jlms')) { a.href = '/tender/' + tenderId + '/type?type=' + $(a).attr('mst'); } $('#jlms').modal('show'); } }); // 编辑 $('body').on('click', '.c-body a[name=edit]', function () { const tid = parseInt($(this).parent().attr('tid')); const tender = _.find(tenders, {id: tid}); $('[name=name]', '#edit-bd').val(tender.name); $('input[type=radio]', '#add-bd').prop('checked', false); $('option[value=0]', '#edit-bd').prop('selected', true); for (const c of tender.category) { // $('input[value=' + c.value + ']', '#edit-bd').prop('checked', 'checked'); $('option[value=' + c.value + ']', '#edit-bd').prop('selected', true); } $('#edit-bd-ok').attr('tid', tid); $('#edit-bd').modal('show'); }); // 删除 $('body').on('click', '.c-body a[name=del]', function () { $('#del-bd-ok').attr('tid', $(this).parent().attr('tid')); $('#del-bd').modal('show'); }); } $(document).ready(() => { autoFlashHeight(); sortCategory(); // 初始化分类数据 initCategoryLevelNode(); $('.modal-body', '#add-bd').append(getCategoryHtml()); $('.modal-body', '#edit-bd').append(getCategoryHtml()); // 初始化标段树结构 initTenderTree(); $('.c-body').html(getTenderTreeHtml()); bindTenderUrl(); // 分类 $('#cate-set').on('show.bs.modal', function () { createTree(); }); $('#set-cate-ok').click(function () { const data = []; const zTree = $.fn.zTree.getZTreeObj('treeLevel'); for (const c of category) { const node = zTree.getNodeByParam('id', c.id); const parent = node.getParentNode(); if (parent.lid === 1) { data.push({id: c.id, level: 0}); } else { data.push({id: c.id, level: node.getPath().length - 1}); } } postData('/setting/category/level', data, function (rst) { for (const d of data) { const c = findNode('id', d.id, category); c.level = d.level; } sortCategory(); initCategoryLevelNode(); initTenderTree(); $('.c-body').html(getTenderTreeHtml()); $('#cate-set').modal('hide'); }); }); $('a[name=add]').click(function () { $('input[type=radio]', '#edit-bd').prop('checked', false); $('input[type=radio]', '#add-bd').eq(0).prop('checked', true); }); // 新增标段 $('#add-bd-ok').click(function () { const data = { name: $('[name=name]', '#add-bd').val(), valuation: $('[name=valuation]:checked').val(), category: [], }; if (!data.name || data.name === '') { // TODO 提示用户 return; } for (const c of category) { const cateObj = $('[cate-id=' + c.id + ']', '#add-bd'); if (parseInt($('select', cateObj).val()) !== 0) { const cate = {cid: c.id}; cate.value = parseInt($('select', cateObj).val()); data.category.push(cate); } // if (c.type === categoryType.key.dropDown) { // cate.value = parseInt($('select', cateObj).val()); // } else if (c.type === categoryType.key.radio) { // cate.value = parseInt($('input:checked', cateObj).val()); // } } postData('/list/add', data, function (result) { tenders.push(result); initTenderTree(); $('.c-body').html(getTenderTreeHtml()); bindTenderUrl(); $('#add-bd').modal('hide'); $('[name=name]', '#add-bd').val(''); }); }); // 编辑标段 $('#edit-bd-ok').click(function () { const data = { id: parseInt($(this).attr('tid')), name: $('[name=name]', '#edit-bd').val(), category: [], }; if (!data.name || data.name === '') { // TODO 提示用户 return; } for (const c of category) { const cateObj = $('[cate-id=' + c.id + ']', '#edit-bd'); if (parseInt($('select', cateObj).val()) !== 0) { const cate = {cid: c.id}; cate.value = parseInt($('select', cateObj).val()); data.category.push(cate); } // if (c.type === categoryType.key.dropDown) { // cate.value = parseInt($('select', cateObj).val()); // } else if (c.type === categoryType.key.radio) { // cate.value = parseInt($('input:checked', cateObj).val()); // } // data.category.push(cate); } postData('/list/update', data, function (result) { const tender = _.find(tenders, {id: result.id}); _.assign(tender, result); initTenderTree(); $('.c-body').html(getTenderTreeHtml()); bindTenderUrl(); $('#edit-bd').modal('hide'); }); }); // 删除标段 $('#del-bd-ok').click(function () { const tid = parseInt($(this).attr('tid')); if (tid >= 0) { postData('/list/del', [tid], function (result) { function getCategory(arr, id) { for (const a of arr) { if (a.cid) { const ac = getCategory(a.children, id); if (ac) { return ac; } } else if (a.id === id) { return arr; } } return null; } for (const rid of result) { const tr = $('td[tid=' + rid + ']').parent(); const arr = getCategory(tenderTree, rid); if (arr) { const a = arr.find(function (x) { return x.id === rid; }); if (arr.length > 1 && arr.indexOf(a) === arr.length - 1) { const span = $('span', tr.prev()); span.text('└'); } arr.splice(arr.indexOf(a), 1); } _.remove(tenders, function (n) { return n.id === rid; }); tr.remove(); $('#del-bd').modal('hide'); } }); } else { $('#del-bd').modal('hide'); } }); // 展开和收起 $('body').on('click', '.fold-switch', function () { if ($(this).children('i').hasClass('fa-minus-square-o')) { $(this).children('i').removeClass('fa-minus-square-o').addClass('fa-plus-square-o'); $(this).attr('title', '展开'); const cid = $(this).attr('cid'); const node = findTenderTreeNode(parseInt(cid), tenderTree); doTrStatus(returnItem, 'hide'); } else { $(this).children('i').removeClass('fa-plus-square-o').addClass('fa-minus-square-o'); $(this).attr('title', '收起'); const cid = $(this).attr('cid'); const node = findTenderTreeNode(parseInt(cid), tenderTree); doTrStatus(returnItem, 'show'); } }) }); function doTrStatus(node, status) { if (status === 'show') { $('.c-body').find('tr[pid="'+ node.sort_id +'"]').show(); $('.c-body').find('tr[pid="'+ node.sort_id +'"] .fold-switch').attr('title', '收起'); $('.c-body').find('tr[pid="'+ node.sort_id +'"] .fold-switch i').removeClass('fa-plus-square-o').removeClass('fa-minus-square-o').addClass('fa-minus-square-o'); } else { $('.c-body').find('tr[pid="'+ node.sort_id +'"]').hide(); $('.c-body').find('tr[pid="'+ node.sort_id +'"] .fold-switch').attr('title', '展开'); $('.c-body').find('tr[pid="'+ node.sort_id +'"] .fold-switch i').removeClass('fa-minus-square-o').removeClass('fa-plus-square-o').addClass('fa-plus-square-o'); } // 判断是否还有一层 if (node.children) { for (const c of node.children) { doTrStatus(c, status); } } } let returnItem; const findTenderTreeNode = function(sortId, tree) { tree.forEach((item) => { if (item.sort_id !== undefined && item.sort_id === sortId) { returnItem = item; return item; } else if (item.children && item.children.length > 0) { findTenderTreeNode(sortId, item.children); } }); }