123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654 |
- 'use strict';
- /**
- *
- *
- * @author Zhong
- * @date 2018/6/11
- * @version
- */
- const billsGuidance = (function () {
- const libSel = $('#stdBillsGuidanceLibSelect');
- //工作内容
- let stdBillsJobData = [];
- //项目特征
- let stdBillsFeatureData = [];
- const bills = {
- dom: $('#billsGuidance_bills'),
- workBook: null,
- cache: [],
- tree: null,
- controller: null,
- treeSetting: {
- treeCol: 0,
- emptyRows: 0,
- headRows: 1,
- headRowHeight: [40],
- defaultRowHeight: 21,
- cols: [{
- width: 160,
- readOnly: true,
- head: {
- titleNames: ["项目编码"],
- spanCols: [1],
- spanRows: [1],
- vAlign: [1],
- hAlign: [1],
- font: ["Arial"]
- },
- data: {
- field: "code",
- vAlign: 1,
- hAlign: 0,
- font: "Arial"
- }
- }, {
- width: 220,
- readOnly: true,
- head: {
- titleNames: ["项目名称"],
- spanCols: [1],
- spanRows: [1],
- vAlign: [1],
- hAlign: [1],
- font: ["Arial"]
- },
- data: {
- field: "name",
- vAlign: 1,
- hAlign: 0,
- font: "Arial"
- }
- },
- {
- width: 45,
- readOnly: true,
- head: {
- titleNames: ["计量单位"],
- spanCols: [1],
- spanRows: [1],
- vAlign: [1],
- hAlign: [1],
- font: ["Arial"]
- },
- data: {
- field: "unit",
- vAlign: 1,
- hAlign: 1,
- font: "Arial"
- }
- }
- ]
- },
- headers: [
- {name: '项目编码', dataCode: 'code', width: 160, vAlign: 'center', hAlign: 'left', formatter: '@'},
- {name: '项目名称', dataCode: 'name', width: 220, vAlign: 'center', hAlign: 'left', formatter: '@'},
- {name: '单位', dataCode: 'unit', width: 45, vAlign: 'center', hAlign: 'center', formatter: '@'},
- ],
- events: {
- SelectionChanging: function (sender, info) {
- billsInitSel(info.newSelections[0].row);
- },
- CellDoubleClick: function (sender, args) {
- if(!bills.tree){
- return;
- }
- let node = bills.tree.items[args.row];
- if(!node){
- return;
- }
- if(node.children.length === 0){
- //插入清单
- let insert = billsLibObj.insertBills(stdBillsJobData, stdBillsFeatureData, node);
- if(insert){
- //插入选中的定额
- let addRationDatas = getInsertRationData(getCheckedRows());
- insertRations(addRationDatas);
- }
- }
- else {
- node.setExpanded(!node.expanded);
- //设置展开收起状态
- sessionStorage.setItem('stdBillsGuidanceExpState', bills.tree.getExpState(bills.tree.items));
- renderSheetFunc(args.sheet, function () {
- let iCount = node.posterityCount(), i, child;
- for (i = 0; i < iCount; i++) {
- child = bills.tree.items[args.row + i + 1];
- args.sheet.setRowVisible(args.row + i + 1, child.visible, args.sheetArea);
- }
- args.sheet.invalidateLayout();
- });
- args.sheet.repaint();
- }
- }
- }
- };
- //项目指引类型
- const itemType = {
- job: 0,
- ration: 1
- };
- const guideItem = {
- dom: $('#billsGuidance_items'),
- workBook: null,
- tree: null,
- controller: null,
- treeSetting: {
- treeCol: 0,
- emptyRows: 0,
- headRows: 1,
- headRowHeight: [40],
- defaultRowHeight: 21,
- cols: [{
- width: 420,
- readOnly: false,
- head: {
- titleNames: ["项目指引"],
- spanCols: [1],
- spanRows: [1],
- vAlign: [1],
- hAlign: [1],
- font: ["Arial"]
- },
- data: {
- field: "name",
- vAlign: 1,
- hAlign: 0,
- font: "Arial"
- }
- },
- {
- width: 35,
- readOnly: false,
- head: {
- titleNames: ["选择"],
- spanCols: [1],
- spanRows: [1],
- vAlign: [1],
- hAlign: [1],
- font: ["Arial"]
- },
- data: {
- field: "select",
- vAlign: 1,
- hAlign: 1,
- font: "Arial"
- }
- }
- ]
- },
- headers: [
- {name: '项目指引', dataCode: 'name', width: 300, vAlign: 'center', hAlign: 'left', formatter: '@'},
- {name: '选择', dataCode: 'select', width: 35, vAlign: 'center', hAlign: 'center', formatter: '@'},
- ],
- events: {
- EditStarting: function (sender, args) {
- if(!bills.tree || guideItem.headers[args.col]['dataCode'] === 'name'){
- args.cancel = true;
- }
- },
- ButtonClicked: function (sender, args) {
- if(args.sheet.isEditing()){
- args.sheet.endEdit(true);
- }
- },
- CellDoubleClick: function (sender, args) {
- if(!bills.tree || !bills.tree.selected){
- return;
- }
- let node = bills.tree.selected.guidance.tree.selected;
- if(!node){
- return;
- }
- if(node.children.length === 0){
- if(guideItem.headers[args.col]['dataCode'] === 'name'){
- insertRations(getInsertRationData([args.row]));
- }
- }
- else {
- node.setExpanded(!node.expanded);
- renderSheetFunc(args.sheet, function () {
- let iCount = node.posterityCount(), i, child;
- for (i = 0; i < iCount; i++) {
- child = bills.tree.selected.guidance.tree.items[args.row + i + 1];
- args.sheet.setRowVisible(args.row + i + 1, child.visible, args.sheetArea);
- }
- args.sheet.invalidateLayout();
- });
- args.sheet.repaint();
- }
- }
- }
- };
- const options = {
- workBook: {
- tabStripVisible: false,
- allowContextMenu: false,
- allowCopyPasteExcelStyle : false,
- allowExtendPasteRange: false,
- allowUserDragDrop : false,
- allowUserDragFill: false,
- scrollbarMaxAlign : true
- },
- sheet: {
- protectionOptions: {allowResizeRows: true, allowResizeColumns: true},
- clipBoardOptions: GC.Spread.Sheets.ClipboardPasteOptions.values
- }
- };
- //渲染时方法,停止渲染
- //@param {Object}sheet {Function}func @return {void}
- function renderSheetFunc(sheet, func){
- sheet.suspendEvent();
- sheet.suspendPaint();
- if(func){
- func();
- }
- sheet.resumeEvent();
- sheet.resumePaint();
- }
- //设置表选项
- //@param {Object}workBook {Object}opts @return {void}
- function setOptions (workBook, opts) {
- for(let opt in opts.workBook){
- workBook.options[opt] = opts.workBook[opt];
- }
- for(let opt in opts.sheet){
- workBook.getActiveSheet().options[opt] = opts.sheet[opt];
- }
- }
- //建表头
- //@param {Object}sheet {Array}headers @return {void}
- function buildHeader(sheet, headers) {
- let fuc = function () {
- sheet.setColumnCount(headers.length);
- sheet.setRowHeight(0, 40, GC.Spread.Sheets.SheetArea.colHeader);
- for(let i = 0, len = headers.length; i < len; i++){
- sheet.setValue(0, i, headers[i].name, GC.Spread.Sheets.SheetArea.colHeader);
- sheet.setColumnWidth(i, headers[i].width, GC.Spread.Sheets.SheetArea.colHeader);
- if(headers[i].formatter){
- sheet.setFormatter(-1, i, headers[i].formatter);
- }
- sheet.getRange(-1, i, -1, 1).hAlign(GC.Spread.Sheets.HorizontalAlign[headers[i]['hAlign']]);
- sheet.getRange(-1, i, -1, 1).vAlign(GC.Spread.Sheets.VerticalAlign[headers[i]['vAlign']]);
- }
- };
- renderSheetFunc(sheet, fuc);
- }
- //表监听事件
- //@param {Object}workBook @return {void}
- function bindEvent(workBook, events) {
- if(Object.keys(events).length === 0){
- return;
- }
- const Events = GC.Spread.Sheets.Events;
- for(let event in events){
- workBook.bind(Events[event], events[event]);
- }
- }
- //建表
- //@param {Object}module @return {void}
- function buildSheet(module) {
- if(!module.workBook){
- module.workBook = new GC.Spread.Sheets.Workbook(module.dom[0], {sheetCount: 1});
- let sheet = module.workBook.getActiveSheet();
- if(module === bills){
- //默认初始可控制焦点在清单表中
- module.workBook.focus();
- sheet.options.isProtected = true;
- sheet.name('stdBillsGuidance_bills');
- }
- if(module === guideItem){
- sheet.options.isProtected = true;
- sheet.getRange(-1, 1, -1, 1).locked(false);
- sheet.getRange(-1, 0, -1, 1).locked(true);
- }
- setOptions(module.workBook, options);
- buildHeader(module.workBook.getActiveSheet(), module.headers);
- bindEvent(module.workBook, module.events);
- }
- }
- //清空表数据
- //@param {Object}sheet {Array}headers {Number}rowCount @return {void}
- function cleanData(sheet, headers, rowCount){
- renderSheetFunc(sheet, function () {
- sheet.clear(-1, 0, -1, headers.length, GC.Spread.Sheets.SheetArea.viewport, GC.Spread.Sheets.StorageType.data);
- if (rowCount > 0) {
- sheet.setRowCount(rowCount);
- }
- });
- }
- //初始化各工作表
- //@param {Array}modules @return {void}
- function initWorkBooks(modules){
- for(let module of modules){
- buildSheet(module);
- }
- }
- //初始化并输出树
- //@param {Object}module {Object}sheet {Object}treeSetting {Array}datas
- function initTree(module, sheet, treeSetting, datas){
- module.tree = idTree.createNew({id: 'ID', pid: 'ParentID', nid: 'NextSiblingID', rootId: -1, autoUpdate: true});
- module.controller = TREE_SHEET_CONTROLLER.createNew(module.tree, sheet, treeSetting);
- module.tree.loadDatas(datas);
- if(module === bills){
- initExpandStat();
- }
- module.controller.showTreeData();
- }
- //项目指引表焦点控制
- //@param {Number}row @return {void}
- function guideItemInitSel(row){
- let billsNode = bills.tree.selected;
- let node = null;
- if(billsNode && billsNode.guidance.tree){
- node = billsNode.guidance.tree.items[row];
- if(node){
- billsNode.guidance.tree.selected = node;
- }
- }
- }
- //根据项目指引的类型设置单元格类型,定额类型的项目指引为复选框
- //@param {Array}nodes @return {void}
- function setItemCellType(nodes){
- //设置单元格类型
- const base = new GC.Spread.Sheets.CellTypes.Base();
- const checkBox = new GC.Spread.Sheets.CellTypes.CheckBox();
- const sheet = guideItem.workBook.getActiveSheet();
- renderSheetFunc(sheet, function(){
- for(let node of nodes){
- sheet.setCellType(node.serialNo(), 1, node.data.type === itemType.ration ? checkBox : base);
- }
- });
- }
- //清单表焦点控制
- //@param {Number}row @return {void}
- function billsInitSel(row){
- if(!bills.tree){
- return;
- }
- let guideSheet = guideItem.workBook.getActiveSheet();
- cleanData(guideSheet, guideItem.headers, -1);
- let node = bills.tree.items[row];
- if(!node){
- return;
- }
- bills.tree.selected = node;
- if(!node.guidance.tree){
- CommonAjax.post('/billsGuidance/api/getItemsByBills', {guidanceLibID: libSel.val(), billsID: node.getID()}, function (rstData) {
- initTree(node.guidance, guideSheet, guideItem.treeSetting, rstData);
- setItemCellType(node.guidance.tree.items);
- //项目指引初始焦点
- guideItemInitSel(guideSheet.getActiveRowIndex() ? guideSheet.getActiveRowIndex() : 0);
- });
- }
- else{
- node.guidance.controller.showTreeData();
- setItemCellType(node.guidance.tree.items);
- //项目指引初始焦点
- guideItemInitSel(guideSheet.getActiveRowIndex() ? guideSheet.getActiveRowIndex() : 0);
- }
- }
- //初始化清单的工作内容和项目特征
- //@param {Number}billsLibId @return {void}
- function initJobAndCharacter(billsLibId){
- CommonAjax.post('/stdBillsEditor/getJobContent', {userId: userID, billsLibId: billsLibId}, function (datas) {
- stdBillsJobData = datas;
- });
- CommonAjax.post('/stdBillsEditor/getItemCharacter', {userId: userID, billsLibId: billsLibId}, function (datas) {
- stdBillsFeatureData = datas;
- });
- }
- //初始化清单展开收起状态
- //@return {void}
- function initExpandStat(){
- //读取展开收起状态
- let currentExpState = sessionStorage.getItem('stdBillsGuidanceExpState');
- if(currentExpState){
- bills.tree.setExpandedByState(bills.tree.items, currentExpState);
- }
- //非叶子节点默认收起
- else{
- bills.tree.setRootExpanded(bills.tree.roots, false);
- }
- }
- //初始选择清单指引库
- //@param {Number}libID @return {void}
- function libInitSel(libID){
- //获取清单
- CommonAjax.post('/billsGuidance/api/getLibWithBills', {libID: libID}, function(rstData){
- //获取清单库中的工作内容和项目特征
- initJobAndCharacter(rstData.guidanceLib.billsLibId);
- initTree(bills, bills.workBook.getActiveSheet(), bills.treeSetting, rstData.bills);
- //每一棵项目指引树挂在清单节点上
- for(let node of bills.tree.items){
- node.guidance = {tree: null, controller: null};
- }
- //默认初始节点
- billsInitSel(0);
- });
- }
- //初始化清单指引库
- //@param {Array}libDats @return {void}
- function initLibs(libDatas){
- libSel.empty();
- if(!libDatas){
- return;
- }
- let selectedLib = sessionStorage.getItem('stdBillsGuidance');
- for(let libData of libDatas){
- let opt = $('<option>').val(libData.id).text(libData.name);
- if(selectedLib && libData.id == selectedLib){
- opt.attr('selected', 'selected');
- }
- libSel.append(opt);
- }
- //初始默认选择
- libInitSel(libSel.select().val());
- }
- //初始化视图
- //@param {void} @return {void}
- function initViews(){
- //赋初始高度
- if($('#billsGuidance_bills').height() === 0 || $('#billsGuidance_items').height() === 0){
- let height = $(window).height()-$(".header").height()-$(".toolsbar").height()-$(".tools-bar-height-z").height();
- $('#billsGuidance_bills').height(height / 2);
- $('#billsGuidance_items').height(height / 2);
- }
- let modules = [bills, guideItem];
- initWorkBooks(modules);
- }
- //获取选中的行
- //@return {Array}
- function getCheckedRows(){
- let rst = [];
- let itemSheet = guideItem.workBook.getActiveSheet();
- for(let row = 0; row < itemSheet.getRowCount(); row++){
- let rowV = itemSheet.getValue(row, 1);
- if(rowV){
- rst.push(row);
- }
- }
- return rst;
- }
- //获取选中的定额数据
- //@param {Array}rows @return {Array}
- function getInsertRationData(rows){
- let rst = [];
- for(let row of rows){
- let node = bills.tree.selected.guidance.tree.items[row];
- if(node && node.data.type === itemType.ration){
- rst.push({itemQuery: {userID: userID, ID: node.data.rationID}, rationType: rationType.ration});
- }
- }
- return rst;
- }
- //插入定额
- //@return {void}
- function insertRations(addRationDatas){
- if(addRationDatas.length > 0){
- projectObj.project.Ration.addMultiRation(addRationDatas, function () {
- //恢复
- let sheet = guideItem.workBook.getActiveSheet();
- renderSheetFunc(sheet, function () {
- for(let row = 0; row < sheet.getRowCount(); row++){
- if(sheet.getValue(row, 1)){
- sheet.setValue(row, 1, false);
- }
- }
- });
- });
- }
- }
- //展开至搜索出来点的节点
- //@param {Array}nodes @return {void}
- function expandSearchNodes(nodes){
- let that = this;
- let billsSheet = bills.workBook.getActiveSheet();
- renderSheetFunc(billsSheet, function () {
- function expParentNode(node){
- if(node.parent && !node.parent.expanded){
- node.parent.setExpanded(true);
- expParentNode(node.parent);
- }
- }
- for(let node of nodes){
- expParentNode(node);
- }
- TREE_SHEET_HELPER.refreshTreeNodeData(bills.treeSetting, billsSheet, bills.tree.roots, true);
- TREE_SHEET_HELPER.refreshNodesVisible(bills.tree.roots, billsSheet, true);
- });
- }
- //各按钮监听事件
- //@return {void}
- function bindBtn(){
- //打开清单指引库
- $('#stdBillsGuidanceTab').click(function () {
- if(libSel.children().length === 0){
- initLibs(projectInfoObj.projectInfo.engineeringInfo.billsGuidance_lib);
- }
- });
- //更改清单指引库
- $('#stdBillsGuidanceLibSelect').change(function () {
- //关闭搜索窗口
- $('#billsGuidanceSearchResult').hide();
- billsLibObj.clearHighLight(bills.workBook);
- libInitSel($(this).select().val());
- //记住选项
- sessionStorage.setItem('stdBillsGuidance', $(this).select().val());
- //清除展开收起状态sessionStorage
- sessionStorage.removeItem('stdBillsGuidanceExpState');
- });
- //插入定额
- $('#guidanceInsertRation').click(function () {
- let addRationDatas = getInsertRationData(getCheckedRows());
- insertRations(addRationDatas);
- });
- //插入清单
- $('#guidanceInsertBills').click(function () {
- //插入清单
- if(!bills.tree || !bills.tree.selected){
- return;
- }
- if(bills.tree.selected.children.length === 0){
- let insert = billsLibObj.insertBills(stdBillsJobData, stdBillsFeatureData, bills.tree.selected);
- if(insert){
- //插入选中的定额
- let addRationDatas = getInsertRationData(getCheckedRows());
- insertRations(addRationDatas);
- }
- }
- });
- //搜索
- $('#stdBillsGuidanceSearch>span>button').click(function () {
- if(!bills.tree){
- return;
- }
- let billsSheet = bills.workBook.getActiveSheet();
- billsLibObj.clearHighLight(bills.workBook);
- let keyword = $('#stdBillsGuidanceSearch>input').val();
- if (!keyword || keyword === '') {
- $('#billsGuidanceSearchResult').hide();
- return;
- }
- let result = bills.tree.items.filter(function (item) {
- let codeIs = item.data.code ? item.data.code.indexOf(keyword) !== -1 : false;
- let nameIs = item.data.name ? item.data.name.indexOf(keyword) !== -1 : false;
- return codeIs || nameIs;
- });
- result.sort(function (x, y) {
- return x.serialNo() - y.serialNo();
- });
- if (result.length !== 0) {
- //展开搜索出来的节点
- expandSearchNodes(result);
- //设置记住展开
- sessionStorage.setItem('stdBillsGuidanceExpState', bills.tree.getExpState(bills.tree.items));
- let sel = billsSheet.getSelections();
- bills.controller.setTreeSelected(result[0]);
- billsSheet.setSelection(result[0].serialNo(), sel[0].col, 1, 1);
- billsInitSel(result[0].serialNo());
- for (let node of result) {
- billsSheet.getRange(node.serialNo(), -1, 1, -1).backColor('lemonChiffon');
- }
- $('#nextBillsGuidance').show();
- $('#nextBillsGuidance').unbind('click');
- $('#nextBillsGuidance').bind('click', function () {
- let cur = bills.tree.selected, resultIndex = result.indexOf(cur), sel = billsSheet.getSelections();
- if (resultIndex === result.length - 1) {
- bills.controller.setTreeSelected(result[0]);
- billsSheet.setSelection(result[0].serialNo(), sel[0].col, 1, 1);
- billsInitSel(result[0].serialNo());
- billsSheet.showRow(result[0].serialNo(), GC.Spread.Sheets.VerticalPosition.bottom);
- } else {
- bills.controller.setTreeSelected(result[resultIndex + 1]);
- billsSheet.setSelection(result[resultIndex + 1].serialNo(), sel[0].col, 1, 1);
- billsInitSel(result[resultIndex + 1].serialNo());
- billsSheet.showRow(result[resultIndex + 1].serialNo(), GC.Spread.Sheets.VerticalPosition.bottom);
- }
- });
- } else {
- billsLibObj.clearHighLight(bills.workBook);
- $('#nextBillsGuidance').hide();
- }
- $('#billsGuidanceSearchResultCount').text('搜索结果:' + result.length);
- $('#billsGuidanceSearchResult').show();
- });
- //搜索框回车
- $('#stdBillsGuidanceSearch>input').bind('keypress', function (event) {
- if(event.keyCode === 13){
- $(this).blur();
- $('#stdBillsGuidanceSearch>span>button').click();
- }
- });
- // 关闭搜索结果
- $('#closeSearchBillsGuidance').click(function () {
- $('#billsGuidanceSearchResult').hide();
- billsLibObj.clearHighLight(bills.workBook);
- refreshWorkBook();
- });
- }
- //刷新表
- //@return {void}
- function refreshWorkBook(){
- if(bills.workBook){
- bills.workBook.refresh();
- }
- if(guideItem.workBook){
- guideItem.workBook.refresh();
- }
- }
- return {initViews, bindBtn, refreshWorkBook, bills};
- })();
- $(document).ready(function(){
- billsGuidance.initViews();
- billsGuidance.bindBtn();
- });
|