'use strict'; /** * * * @author EllisRan * @date 2020/03/06 * @version */ let hideList = []; 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); } }); }; function removeValueToCate(cate) { const changeCate = JSON.parse(JSON.stringify(cate)); const newCate = []; for (const c of changeCate) { delete c.value; newCate.push(c); } return newCate; } function recursiveExpand(nodes, parent, checkFun) { for (const node of nodes) { const expanded = checkFun(node); if (!expanded && node.sort_id) hideList.push({sort_id: node.sort_id}); if (node.expanded !== expanded) { node.expanded = expanded; if (node.sort_id) { if (node.expanded) { $('.c-body tr td span[cid="' + node.sort_id + '"]').children('i').removeClass('fa-plus-square-o').addClass('fa-minus-square-o'); $('.c-body tr td span[cid="' + node.sort_id + '"]').attr('title', '收起'); } else { $('.c-body tr td span[cid="' + node.sort_id + '"]').children('i').removeClass('fa-minus-square-o').addClass('fa-plus-square-o'); $('.c-body tr td span[cid="' + node.sort_id + '"]').attr('title', '展开'); } doTrStatus(node, node.expanded ? 'show' : 'hide'); } } node.visible = parent ? (parent.expanded && parent.visible) : true; if (node.children) recursiveExpand(node.children, node, checkFun); } } function expandByCustom(checkFun) { hideList = []; recursiveExpand(tenderTree, null, checkFun); } function expandByLevel(level) { expandByCustom(function (n) { return n.level < level; }); setLocalCache(uphlname, JSON.stringify(hideList)); } // 根据标段类别设置排序 function sortTenderTree(teTree = tenderTree) { for (const tender of teTree) { if (tender.sort) { // 当前层排序 teTree.sort(function (a, b) { if (a.sort && b.sort) { return a.sort - b.sort; } else if (a.sort && !b.sort) { return -1; } else if (b.sort && !a.sort) { return 1; } else { return 0; } }); } } for (const tender of teTree) { if (tender.children) { sortTenderTree(tender.children); } } } function localHideList(wap = false) { const pro_cate = getLocalCache('pro_'+ pid + '_category_list'); const cate = JSON.stringify(removeValueToCate(category)); if (pro_cate && cate === pro_cate) { const userTenderHideList = getLocalCache(uphlname); if (userTenderHideList) { hideList = JSON.parse(userTenderHideList); for (const h of hideList) { const cid = h.sort_id; const node = findTenderTreeNode(parseInt(cid), tenderTree); $('.c-body tr td span[cid="' + cid + '"]').children('i').removeClass('fa-minus-square-o').addClass('fa-plus-square-o'); $('.c-body tr td span[cid="' + cid + '"]').attr('title', '展开'); doTrStatus(returnItem, 'hide'); } } } else { removeLocalCache(uphlname); setLocalCache('pro_'+ pid + '_category_list', cate); } if (!wap) setTopTr(); } $(window).resize(setTopTr); // 设置表头固定并动态调整宽度高度 function setTopTr() { for(let item = 0; item < $(".c-body table>thead>tr>th").length; item ++) { $(".c-body table>thead>tr>th").eq(item).outerWidth($(".c-body table>tbody>tr:first").children('td').eq(item).outerWidth()); } $('.c-body table').css('margin-top', $(".c-body table>thead").height() - 4); // $('.c-body table').css('margin-top', -2); } function doTrStatus(node, status, all = '') { if (node) { if (status === 'show') { $('.c-body').find('tr[pid="'+ node.sort_id +'"]').show(); if (all === 'all') { $('.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(); if (all === 'all') { if (node.children) { hideList.push({sort_id: node.sort_id}); setLocalCache(uphlname, JSON.stringify(hideList)); } $('.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 && all === '') { for (const [index,c] of node.children.entries()) { const title = $('.c-body').find('tr[pid="'+ node.sort_id +'"] .fold-switch').eq(index).attr('title'); if (title === '收起') { doTrStatus(c, status); } } } else if (node.children && all === 'all') { for (const c of node.children) { doTrStatus(c, status, 'all'); } } } } let tenderTreeShowLevel; const getChildrenLevel = function (node) { let iLevel = node.level || 1; if (node.children && node.children.length > 0) { for (const c of node.children) { iLevel = Math.max(iLevel, getChildrenLevel(c)); } } return iLevel; }; $(document).ready(() => { // 展开和收起 $('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'); hideList.push({sort_id: cid}); setLocalCache(uphlname, JSON.stringify(hideList)); } 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'); const index = hideList.findIndex(function(item) { return parseInt(item.sort_id) === parseInt(cid); }); hideList.splice(index, 1); setLocalCache(uphlname, JSON.stringify(hideList)); } setTopTr(); }); // 一键展开和收起 $('body').on('click', '.tree-toggle', function () { const item = $(this).attr('data-item'); hideList = []; if (item === 'open') { setLocalCache(uphlname, JSON.stringify(hideList)); } for (const tree of tenderTree) { if (tree && tree.sort_id !== undefined) { const cid = tree.sort_id; const node = findTenderTreeNode(parseInt(cid), tenderTree); if (item === 'open') { $('.c-body tr td span[cid="' + cid + '"]').children('i').removeClass('fa-plus-square-o').addClass('fa-minus-square-o'); $('.c-body tr td span[cid="' + cid + '"]').attr('title', '收起'); doTrStatus(returnItem, 'show', 'all'); } else if (item === 'hide') { $('.c-body tr td span[cid="' + cid + '"]').children('i').removeClass('fa-minus-square-o').addClass('fa-plus-square-o'); $('.c-body tr td span[cid="' + cid + '"]').attr('title', '展开'); doTrStatus(returnItem, 'hide', 'all'); } } } setTopTr(); }); tenderTreeShowLevel = $.cs_showLevel({ selector: '#show-level', levels: [ { type: 'sort', count: 5, visible_count: function () { return tenderTree.map(getChildrenLevel).reduce((x, y) => { return Math.max(x, y); }, 0) - 1; } }, { type: 'last', title: '最底层', visible: function () { const count = tenderTree.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": expandByLevel(parseInt(tag)); break; case "last": expandByLevel(20); break; default: return; } } }); });