'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, showHint: 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){ //插入清单 if (/\//.test(node.data.unit)) { let canAdd = true; $.bootstrapLoading.start(); let existB = projectObj.project.Bills.sameStdCodeBillsData(node.data.code); if (existB) { let std = JSON.parse(JSON.stringify(node.data)); std.unit = existB.unit; canAdd = ProjectController.addBills(projectObj.project, projectObj.mainController, std); if(canAdd !== null || canAdd !== false){ //插入选中的定额 let addRationDatas = getInsertRationData(getCheckedRows()); insertRations(addRationDatas); } if(canAdd === false && $.bootstrapLoading.isLoading()){ $.bootstrapLoading.end(); } } else { ConfirmModal.stdBillsUnit.check(node.data, function (std) { canAdd = ProjectController.addBills(projectObj.project, projectObj.mainController, std); if(canAdd !== null || canAdd !== false){ //插入选中的定额 let addRationDatas = getInsertRationData(getCheckedRows()); insertRations(addRationDatas); } if(canAdd === false && $.bootstrapLoading.isLoading()){ $.bootstrapLoading.end(); } }, function () { if($.bootstrapLoading.isLoading()){ $.bootstrapLoading.end(); } }); } } else { 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: 1, emptyRows: 0, headRows: 1, headRowHeight: [40], defaultRowHeight: 21, cols: [ { 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" } }, { 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" } } ] }, headers: [ {name: '选择', dataCode: 'select', width: 35, vAlign: 'center', hAlign: 'center', formatter: '@'}, {name: '项目指引', dataCode: 'name', width: 300, vAlign: 'center', hAlign: 'left', 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); } refreshInsertRation(); }, 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'); //设置悬浮提示 TREE_SHEET_HELPER.initSetting(bills.dom[0], bills.treeSetting); } if(module === guideItem){ sheet.options.isProtected = true; sheet.getRange(-1, 0, -1, 1).locked(false); sheet.getRange(-1, 1, -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(), 0, node.data.type === itemType.ration ? checkBox : base); } }); } //清单表焦点控制 //@param {Number}row @return {void} function billsInitSel(row){ let guideSheet = guideItem.workBook.getActiveSheet(); cleanData(guideSheet, guideItem.headers, -1); refreshInsertRation(); if(!bills.tree){ return; } 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); } } //设置tag以悬浮提示 function setTagForHint(nodes){ let sheet = bills.workBook.getActiveSheet(); renderSheetFunc(sheet, function () { for(let node of nodes){ sheet.setTag(node.serialNo(), 2, node.data.ruleText ? node.data.ruleText : ''); } }); } //初始选择清单指引库 //@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}; } setTagForHint(bills.tree.items); //默认初始节点 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 = $('