123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567 |
- 'use strict';
- /**
- *
- *
- * @author Mai
- * @date 2019/3/7
- * @version
- */
- const EmptyTenderHtml = [
- '<div class="jumbotron">',
- '<h3 class="display-6">还没有标段数据</h3>',
- '</div>'
- ];
- // 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<l; i++) {
- if (treeNodes[i].drag === false) {
- return false;
- }
- if (!targetNode && treeNodes[i].dropRoot === false) {
- return false;
- }
- if(treeNodes[i].isParent === true && targetNode.lid !== 1){
- return false;
- }
- }
- return true;
- }
- function onDropNode(event, treeId, treeNodes, targetNode, moveType) {
- const zTree = $.fn.zTree.getZTreeObj(treeId);
- function resetFixNode(id) {
- const node = zTree.getNodeByParam('lid', id);
- node.isParent = true;
- zTree.updateNode(node, false);
- zTree.expandNode(node, true);
- }
- function moveChildren(children, node) {
- if (!children || children.length === 0) { return }
- for (const c of children) {
- moveChildren(c.children, node);
- zTree.moveNode(node, c, 'inner');
- }
- }
- resetFixNode(1);
- resetFixNode(2);
- if (targetNode !== null && targetNode.lid === 1 && treeNodes[0].children && treeNodes[0].children.length !== 0) {
- moveChildren(treeNodes[0].children, zTree.getNodeByParam('lid', 1));
- } else if (targetNode !== null && targetNode.lid !== 1) {
- if (targetNode.children.length >= 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('<div class="form-group" cate-id="' + cate.id + '">');
- html.push('<label>', cate.name, '</label>');
- html.push('<select class="form-control form-control-sm">');
- for (const v of cate.value) {
- html.push('<option value="' + v.id + '">', v.value, '</option>');
- }
- html.push('<option value="0">不选</option>');
- html.push('</select>');
- html.push('</div>');
- return html.join('');
- }
- function getRadioCategoryHtml (cate) {
- const html = [];
- html.push('<div class="form-group" cate-id="' + cate.id + '">');
- html.push('<lable>', cate.name, '</lable>');
- html.push('<div>');
- for (const iV in cate.value) {
- const v = cate.value[iV];
- html.push('<div class="form-check-inline">');
- html.push('<input class="form-check-input" type="radio"', 'name="' + cate.name + '" ', 'value="' , v.id, (iV == 0 ? '" checked="' : ''), '">');
- html.push('<label class="form-check-label">', v.value, '</label>');
- html.push('</div>');
- }
- html.push('</div>');
- html.push('</div>');
- 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('<tr pid="' + pid + '">');
- // 名称
- html.push('<td class="in-' + node.level + '">');
- if (node.cid) {
- html.push('<span class="fold-switch mr-1" title="收起" cid="'+ node.sort_id +'"><i class="fa fa-minus-square-o"></i></span> <i class="fa fa-folder-o"></i> ', node.name);
- } else {
- html.push('<span class="text-muted mr-2">');
- html.push(arr.indexOf(node) === arr.length - 1 ? '└' : '├');
- html.push('</span>');
- html.push('<a href="javascript: void(0)" name="name" id="' + node.id + '">', node.name, '</a>');
- }
- html.push('</td>');
- // 创建人
- html.push('<td>', node.user_name, '</td>');
- // 创建时间
- html.push('<td>', node.create_time ? moment(node.create_time).format('YYYY-MM-DD HH:mm:ss') : '', '</td>');
- // 完成期数
- html.push('<td>');
- if (!node.cid) {
- html.push(node.lastStage ? '第' + node.lastStage.order + '期' : '第0期');
- }
- html.push('</td>');
- // 管理
- html.push('<td tid="' + node.id + '">');
- if (!node.cid) {
- html.push('<a href="javascript: void(0)" name="edit" class="btn btn-outline-primary btn-sm">编辑</a>');
- if (node.lastStage === null || node.lastStage === undefined) {
- html.push('<a href="javascript: void(0)" name="del" class="btn btn-outline-danger btn-sm ml-1">删除</a>');
- } else {
- html.push('<button class="btn btn-outline-secondary btn-sm ml-1" data-toggle="tooltip" data-placement="top" title="请先删除所有期">删除</button>');
- }
- }
- html.push('</td>');
- html.push('</tr>');
- 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('<thead>', '<tr>');
- html.push('<th>', '名称', '</th>');
- html.push('<th>', '创建人', '</th>');
- html.push('<th>', '创建时间', '</th>');
- html.push('<th>', '完成期数', '</th>');
- html.push('<th>', '管理', '</th>');
- html.push('</tr>', '</thead>');
- return html.join('');
- }
- // 根据TenderTree数据获取Html代码
- function getTenderTreeHtml () {
- if (tenderTree.length > 0) {
- const html = [];
- html.push('<table class="table table-hover table-bordered">');
- html.push(getTenderTreeHeaderHtml());
- for (const t of tenderTree) {
- html.push(recursiveGetTenderNodeHtml(t, tenderTree, ''));
- }
- html.push('</table>');
- 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);
- }
- });
- }
|