'use strict'; /** * * * @author Zhong * @date 2018/6/11 * @version */ const billsGuidance = (function () { const libSel = $('#stdBillsGuidanceLibSelect'); 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: 200, readOnly: true, head: { titleNames: ["项目编码"], spanCols: [1], spanRows: [1], vAlign: [1], hAlign: [1], font: ["Arial"] }, data: { field: "code", vAlign: 1, hAlign: 0, font: "Arial" } }, { width: 200, readOnly: true, head: { titleNames: ["项目名称"], spanCols: [1], spanRows: [1], vAlign: [1], hAlign: [1], font: ["Arial"] }, data: { field: "name", vAlign: 1, hAlign: 0, font: "Arial" } }] }, headers: [ {name: '项目编码', dataCode: 'code', width: 200, vAlign: 'center', hAlign: 'left', formatter: '@'}, {name: '项目名称', dataCode: 'name', width: 200, vAlign: 'center', hAlign: 'left', formatter: '@'} ], events: { SelectionChanging: function (sender, info) { billsInitSel(info.newSelections[0].row); } } }; //项目指引类型 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: 300, readOnly: false, head: { titleNames: ["项目指引"], spanCols: [1], spanRows: [1], vAlign: [1], hAlign: [1], font: ["Arial"] }, data: { field: "name", vAlign: 1, hAlign: 0, font: "Arial" } }, { width: 50, 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: 'name', width: 50, vAlign: 'center', hAlign: 'center', formatter: '@'}, ], events: { EditStarting: function (sender, args) { if(guideItem.headers[args.col]['dataCode'] === 'name'){ args.cancel = true; } }, ButtonClicked: function (sender, args) { if(args.sheet.isEditing()){ args.sheet.endEdit(true); } }, } }; 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; let sheet = workBook.getActiveSheet(); 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; } 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); 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){ 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}libID @return {void} function libInitSel(libID){ //获取清单 CommonAjax.post('/billsGuidance/api/getLibWithBills', {libID: libID}, function(rstData){ 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(); for(let libData of libDatas){ let opt = ``; libSel.append(opt); } //初始默认选择 libInitSel(libSel.select().val()); } //初始化视图 //@param {void} @return {void} function initViews(){ let modules = [bills, guideItem]; initWorkBooks(modules); } //各按钮监听事件 //@return {void} function bindBtn(){ //打开清单指引库 $('#stdBillsGuidanceTab').click(function () { if(libSel.children().length === 0){ initLibs(projectInfoObj.projectInfo.engineeringInfo.billsGuidance_lib); } }); //更改清单指引库 $('#stdBillsGuidanceLibSelect').change(function () { libInitSel($(this).select().val()); }); //插入定额 $('#guidanceInsertRation').click(function () { }); //插入清单 $('#guidanceInsertBills').click(function () { }); } //刷新表 //@return {void} function refreshWorkBook(){ if(bills.workBook){ bills.workBook.refresh(); } if(guideItem.workBook){ guideItem.workBook.refresh(); } } return {initViews, bindBtn, refreshWorkBook}; })(); $(document).ready(function(){ billsGuidance.initViews(); billsGuidance.bindBtn(); });