/** * Created by Zhong on 2017/12/1. */ /* * 清单计算基数 * */ let calcBaseView = { //可用计算基数的清单固定列映射(与fixedFlag) inputExpr: $('#calcBaseExp'), confirmBtn: $('#calcBaseConf'), noBaseBills: [1, 3], editingCell: null, workBook: null, setting:{ header: [ {name: '计算基础名称', dataCode: 'base', width: 280, vAlign: 'center', hAlign: 'left'}, {name: '金额', dataCode: 'price', width: 120, vAlign: 'center', hAlign: 'right'} ], options: { tabStripVisible: false, allowCopyPasteExcelStyle : false, allowExtendPasteRange: false, allowUserDragDrop : false, allowUserDragFill: false, scrollbarMaxAlign : true }, dateRows: [], locked: { rows: [], cols: [0, 1] } }, renderSheetFuc: function (sheet, fuc) { sheet.suspendPaint(); sheet.suspendEvent(); fuc(); sheet.resumePaint(); sheet.resumeEvent(); }, setOptions: function (workbook, opts) { for(let opt in opts){ workbook.options[opt] = opts[opt]; } }, buildHeader: function (sheet, headers) { let me = calcBaseView; let fuc = function () { sheet.options.clipBoardOptions = GC.Spread.Sheets.ClipboardPasteOptions.values; sheet.options.isProtected = true; 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); } }; me.renderSheetFuc(sheet, fuc); }, buildSheet: function () { if(!this.workBook){ this.workBook = new GC.Spread.Sheets.Workbook($('#billsBaseSpread')[0], {sheetCount: 1}); this.setOptions(this.workBook, this.setting.options); this.buildHeader(this.workBook.getActiveSheet(), this.setting.header); this.bindEvent(this.workBook); } }, bindEvent: function (workBook) { const _events = GC.Spread.Sheets.Events; let sheet = workBook.getActiveSheet(); sheet.bind(_events.CellDoubleClick, this.onCellDoubleClick); }, showData(datas){ let me = calcBaseView; let sheet = this.workBook.getActiveSheet(); let cols = this.setting.header; let fuc = function () { sheet.setRowCount(datas.length); sheet.setFormatter(-1, 1, '@'); for(let col = 0, cLen = cols.length; col < cLen; col++){ sheet.getRange(-1, col, -1, 1).hAlign(GC.Spread.Sheets.HorizontalAlign[cols[col]['hAlign']]); sheet.getRange(-1, col, -1, 1).vAlign(GC.Spread.Sheets.VerticalAlign[cols[col]['vAlign']]); for(let row = 0, rLen = datas.length; row < rLen; row++){ sheet.setValue(row, col, datas[row][cols[col]['dataCode']]); } } }; this.renderSheetFuc(sheet, fuc); }, onCellDoubleClick: function (sender, args) { let me = calcBaseView; if(args.col === 0){ let baseFigure = args.sheet.getValue(args.row, args.col); if(baseFigure.trim() !== ''){ //在光标后面插入 let insertStr = me.insertStr(baseFigure); me.inputExpr.val(insertStr); } } me.inputExpr.focus(); }, isDef: function (v) { return v !== undefined && v !== null; }, isFlag: function (v) { return this.isDef(v.flagsIndex) && this.isDef(v.flagsIndex.fixed); }, ifEdit: function () { var selected = projectObj.project.mainTree.selected; return MainTreeCol.readOnly.forCalcBase(selected)?false:true; }, canBase: function (node) { return node.sourceType === projectObj.project.Bills.getSourceType() && node.children.length === 0; }, //计算基数转换为显示数据Obj to Array toViewData: function (obj) { let rst = []; for(let figure in obj){ let figureObj = Object.create(null); figureObj.base = figure; figureObj.price = projectObj.project.calcBase.getBase(figure); rst.push(figureObj); } return rst; }, initCalctor: function (node) { let me = calcBaseView; //输入框显示原本的 if(me.isDef(node.data.calcBase)){ me.inputExpr.val(node.data.calcBase); } me.buildSheet(); let baseObj = projectObj.project.calcBase.getBaseByClass(node); console.log(baseObj); me.showData(me.toViewData(baseObj)); }, getInputExpr: function () { return this.inputExpr.val(); }, //四则运算符控制,不可连续出现: ++ +*... arithmeticLeagl: function (v) { let rex = /[\+,\-,\*,\/]{2}/g; return !rex.test(v); }, //运算符点击显示到运算窗口 clickOpr: function (operators) { let me = calcBaseView; for(let i = 0, len = operators.length; i < len; i++){ operators[i].bind('click', function () { let v = $(this)[0].textContent; let insertStr = me.insertStr(v); if(me.arithmeticLeagl(insertStr)){ me.inputExpr.val(insertStr); } me.inputExpr.focus(); }); } }, //光标处插入(替换选中) insertStr: function (v) { let me = calcBaseView; //在光标后面插入 let exp = me.getInputExpr(); let startIdx = me.inputExpr[0].selectionStart; let endIdx = me.inputExpr[0].selectionEnd; let startStr = exp.substring(0, startIdx); let endStr = exp.substring(endIdx, exp.length); return startStr + v + endStr; }, //输入窗口控制 inputControl: function () { let me = calcBaseView; me.inputExpr.keydown(function (e) { if(!me.arithmeticLeagl(me.inputExpr.val() + e.key)){ return false; } }); }, //确认按钮 calcBaseConf: function () { let me = calcBaseView; me.confirmBtn.bind('click', function () { let selected = projectObj.project.mainTree.selected; projectObj.updateCellValue(selected, me.getInputExpr(), {data: {field: 'calcBase'}}); if(projectObj.project.calcBase.success){ $('#qd-jsjs').modal('hide'); } }); }, getCalcBaseCellType:function () { var ns = GC.Spread.Sheets; function CalcBaseCellType() { var init=false; } CalcBaseCellType.prototype = new ns.CellTypes.Text(); CalcBaseCellType.prototype.paint = function (ctx, value, x, y, w, h, style, options) { if(value!=null){ // ctx.fillText(value,x+3+ctx.measureText(value).width,y+h-3); // ctx.fillText(value,x+w-3,y+h-3); GC.Spread.Sheets.CellTypes.Text.prototype.paint.apply(this, arguments); } if(calcBaseView.editingCell){ if(calcBaseView.editingCell.row==options.row&&calcBaseView.editingCell.col==options.col){ var image = document.getElementById('f_btn'),imageMagin = 3; var imageHeight = h-2*imageMagin; var imageWidth = w*2/7; var imageX = x + w - imageWidth- imageMagin, imageY = y + h / 2 - imageHeight / 2; ctx.save(); ctx.drawImage(image, imageX, imageY,imageWidth,imageHeight); ctx.beginPath(); ctx.arc(imageX+imageWidth/2,imageY+imageHeight/2,1,0,360,false); ctx.arc(imageX+imageWidth/2-4,imageY+imageHeight/2,1,0,360,false); ctx.arc(imageX+imageWidth/2+4,imageY+imageHeight/2,1,0,360,false); ctx.fillStyle="black";//填充颜色,默认是黑色 ctx.fill();//画实心圆 ctx.closePath(); ctx.restore(); } } }; CalcBaseCellType.prototype.getHitInfo = function (x, y, cellStyle, cellRect, context) { return { x: x, y: y, row: context.row, col: context.col, cellStyle: cellStyle, cellRect: cellRect, sheetArea: context.sheetArea }; }; CalcBaseCellType.prototype.processMouseDown = function (hitinfo) { let me=calcBaseView; if(me.editingCell==null){ var showSelectBtn = true; if(hitinfo.sheet.name()!='calc_detail'){ showSelectBtn=me.ifEdit(); } if(showSelectBtn){ me.editingCell={ row:hitinfo.row, col:hitinfo.col } hitinfo.sheet.invalidateLayout(); hitinfo.sheet.repaint(); } }else if(hitinfo.row==me.editingCell.row){ var offset=hitinfo.cellRect.x+hitinfo.cellRect.width-6; var imageMagin=3; var imageHeight = hitinfo.cellRect.height-2*imageMagin; var imageWidth = hitinfo.cellRect.width*2/7; if(hitinfo.xoffset-imageWidth){ $('#qd-jsjs').modal({show: true}); } } }; CalcBaseCellType.prototype.processMouseLeave = function (hitinfo) { calcBaseView.editingCell=null; hitinfo.sheet.invalidateLayout(); hitinfo.sheet.repaint(); } return new CalcBaseCellType(); }, }; $(document).ready(function () { $('#qd-jsjs').on('shown.bs.modal', function () { calcBaseView.initCalctor(projectObj.project.mainTree.selected); calcBaseView.workBook.refresh(); }); $('#qd-jsjs').on('hidden.bs.modal', function () { //清空输入框 calcBaseView.inputExpr.val(''); calcBaseView.workBook.destroy(); calcBaseView.workBook = null; }); //bind operator click function calcBaseView.clickOpr([$('#addOpr'), $('#subOpr'), $('#mulOpr'), $('#divOpr'), $('#leftOpr'), $('#rightOpr')]); //bind input control calcBaseView.inputControl(); //confirmBtn calcBaseView.calcBaseConf(); });