| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308 | /** * Created by Zhong on 2017/12/1. *//** 清单计算基数* */let calcBaseView = {    //可用计算基数的清单固定列映射(与fixedFlag)    inputExpr: $('#calcBaseExp'),    confirmBtn: $('#calcBaseConf'),    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        },        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, '@');            let style = new GC.Spread.Sheets.Style();            style.formatter = MainTreeCol.getNumberFormatter(decimalObj.bills.totalPrice, true);            sheet.setStyle(-1, 1, style);            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(cbParser.toFExpr(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();    },    //四则运算符控制,不可连续出现: ++ +*...    arithmeticLegal: function (v) {        let rex = /[\+,\-,\*,\/]{2}/g;        let rex2 = /[{]{2}/g;        let rex3 = /[}]{2}/g;        let rex4 = /[F]{2}/g;        let rex5 = /[.]{2}/g;        let rex6 = /[%]{2}/g;        return !rex.test(v) && !rex2.test(v) && !rex3.test(v) && !rex4.test(v) && !rex5.test(v) && !rex6.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.arithmeticLegal(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.arithmeticLegal(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.x<offset&&hitinfo.x>offset-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();});
 |