| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525 | /** * Created by Zhong on 2017/11/24. */let projFeatureView = {    orgDatas: [],//for compare    datas: [], // 这个数据是显示和操作时用的,入库的时候需要将这份数据转换成可入库的数据结构    workBook: null,    firstData: { dispName: '工程特征', key: 'projFeature', items: [] },//for show    setting: {        header: [            { name: '属性', dataCode: 'dispName', width: 200, vAlign: 'center', hAlign: 'left' },            { name: '值', dataCode: 'value', width: 300, vAlign: 'center', hAlign: 'left' }        ],        options: {            allowContextMenu: false,            tabStripVisible: false,            allowCopyPasteExcelStyle: false,            allowExtendPasteRange: false,            allowUserDragDrop: false,            allowUserDragFill: false,            scrollbarMaxAlign: true        },        numRows: [],        dateRows: [],        locked: {            rows: [],            cols: [0]        }    },    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];        }    },    setCombo: function (sheet, row, items) {        let dynamicCombo = sheetCommonObj.getDynamicCombo();        dynamicCombo.items(items);        dynamicCombo.editable(false);        sheet.getCell(row, 1).cellType(dynamicCombo);    },    getComboItemsByRow: function (row) {        let data = this.datas[row];        if (!data) {            return null;        }        if (!data.cellType || data.cellType !== 'comboBox') {            return null;        }        return data.options ? data.options.split('@') : [];    },    buildHeader: function (sheet, headers) {        let me = projFeatureView;        let fuc = function () {            sheet.options.clipBoardOptions = GC.Spread.Sheets.ClipboardPasteOptions.values;            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);                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']]);            }        };        me.renderSheetFuc(sheet, fuc);    },    buildSheet: function () {        if (!this.workBook) {            this.workBook = new GC.Spread.Sheets.Workbook($('#projFeatureSpread')[0], { sheetCount: 1 });            sheetCommonObj.spreadDefaultStyle(this.workBook);            sheetCommonObj.bindEscKey(this.workBook, [{ sheet: this.workBook.getSheet(0), editStarting: this.onEditStarting, editEnded: this.onEditEnded }]);            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.EditStarting, this.onEditStarting);        sheet.bind(_events.EditEnded, this.onEditEnded);        sheet.bind(_events.EnterCell, this.onEnterCell);        sheet.bind(_events.ClipboardPasting, this.onClipboardPasting);        sheet.bind(_events.ClipboardPasted, this.onClipboardPasted);    },    showData(datas) {        let me = projFeatureView;        let sheet = this.workBook.getActiveSheet();        let cols = this.setting.header;        let fuc = function () {            sheet.setRowCount(datas.length);            me.initTree(sheet, true, datas);            sheet.setFormatter(-1, 1, '@');            //兼容旧数据            // org: compatLockedKeys = ['engineering']            let compatLockedKeys = [],                compatNumKeys = [                    'buildingArea',                    'basementBuildingArea',                    'totalFloors',                    'basementFloors',                    'buildingFloors',                    'buildingHeight',                    'basementHeight',                    'firstFloorHeight',                    'podiumBuildingHeight',                    'standardFloorHeight'                ];            for (let row = 0; row < datas.length; row++) {                if (datas[row].cellType == 'comboBox') {                    let options = datas[row].options ? datas[row].options.split("@") : [];                    me.setCombo(sheet, row, options);                } else if (datas[row].cellType == 'number' || compatNumKeys.includes(datas[row].key)) {                    me.setting.numRows.push(row);                } else if (datas[row].cellType === 'date') {                    me.setting.dateRows.push(row);                }                let readOnly = typeof datas[row].readOnly === 'string' ? JSON.parse(datas[row].readOnly) : datas[row].readOnly;                if (readOnly || datas[row].items || compatLockedKeys.includes(datas[row].key)) {                    me.setting.locked.rows.push(row);                }                for (let col = 0; col < cols.length; col++) {                    sheet.setValue(row, col, datas[row][cols[col]['dataCode']]);                }            }            basicInfoView.setDatePicker(sheet, me.setting.dateRows);        };        this.renderSheetFuc(sheet, fuc);    },    onEditStarting: function (sender, args) {        let me = projFeatureView;        if (me.setting.locked.cols.indexOf(args.col) !== -1) {            args.cancel = true;        }        if (args.col === 1 && me.setting.locked.rows.indexOf(args.row) !== -1) {            args.cancel = true;        }    },    onEditEnded: function (sender, args) {        let me = projFeatureView;        let v = args.editingText ? args.editingText.toString().trim() : '';        if (args.row < me.datas.length) {            let required = typeof me.datas[args.row].required === 'string' ? JSON.parse(me.datas[args.row].required) : me.datas[args.row].required;            if (required && !v) {                v = me.datas[args.row].value;                args.sheet.setValue(args.row, args.col, v);            } else if (me.setting.numRows.indexOf(args.row) !== -1) {//控制数值                if (!me.isNum(v)) {                    alert('只能输入数值');                    v = me.datas[args.row].value && me.isNum(me.datas[args.row].value) ? me.datas[args.row].value : '';                    args.sheet.setValue(args.row, args.col, v);                }            } else if (me.setting.dateRows.indexOf(args.row) !== -1) {                if (v.length > 0) {                    v = formatDate(new Date(v), 'yyyy-MM-dd');                    v = basicInfoView.filtDate(v);                    if (!v) {                        alert('请输入正确的日期格式yyyy-mm-dd');                        args.sheet.setValue(args.row, args.col, me.datas[args.row].value ? me.datas[args.row].value : '');                        return;                    }                }            }            me.datas[args.row].value = v;        }    },    onEnterCell: function (sender, args) {        args.sheet.repaint();    },    onClipboardPasting: function (sender, args) {        let me = projFeatureView;        if (me.setting.locked.cols.indexOf(args.cellRange.col) !== -1) {            args.cancel = true;        }    },    onClipboardPasted: function (sender, args) {        let me = projFeatureView;        let items = sheetCommonObj.analyzePasteData(me.setting, args);        let recRows = [];        if (items.length === 0) {            return;        }        for (let i = 0, len = items.length; i < len; i++) {            let row = i + args.cellRange.row;            let comboItems = me.getComboItemsByRow(row);            let required = typeof me.datas[row].required === 'string' ? JSON.parse(me.datas[row].required) : me.datas[row].required;            if (me.setting.locked.rows.indexOf(row) !== -1) {                recRows.push(row);            }            else if (required && !items[i].value) {                recRows.push(row);            }            //粘贴下拉框数据过滤            else if (comboItems && !comboItems.includes(items[i].value)) {                recRows.push(row);            }            else if (me.setting.numRows.indexOf(row) !== -1 && !me.isNum(items[i].value)) {                recRows.push(row);            }            else if (me.setting.dateRows.indexOf(row) !== -1) {                items[i].value = basicInfoView.filtDate(items[i].value);                if (!me.isDef(items[i].value)) {                    recRows.push(row);                } else {                    me.datas[row].value = items[i].value;                }            }            else {                me.datas[row].value = items[i].value;            }        }        if (recRows.length > 0) {            me.renderSheetFuc(args.sheet, function () {                for (let i = 0, len = recRows.length; i < len; i++) {                    let staticV = me.datas[recRows[i]].value || '';                    args.sheet.setValue(recRows[i], args.cellRange.col, staticV);                }            })        }    },    getFeature: function (featureKey) {        let datas = this.datas;        if (datas.length === 0) {            datas = projectObj.project.property.projectFeature;        }        for (let feature of datas) {            if (feature.key === featureKey) {                return feature.value;            }        }        return null;    },    isDef: function (v) {        return v !== undefined && v !== null;    },    isNum: function (v) {        return this.isDef(v) && !isNaN(v);    },    copyObj: function (obj) {        let newObj = {};        for (let attr in obj) {            newObj[attr] = obj[attr];        }        return newObj;    },    initDatas: function (datas) {        this.datas = _.cloneDeep(datas);        //datas.forEach(item => this.datas.push(_.cloneDeep(item)));    },    //数据库读到的数据转换为展示的结构    toViewDatas: function (datas) {        // 将数据打平(原数据一些子项数据会嵌套在items数组中)        this.firstData.items = datas;        let curID = 1;        return extractDataFromItems([this.firstData]);        // 为了与sheet_common.js -> getTreeNodeCellType共用树结构功能        // 需要设置数据的ID、ParentID        function extractDataFromItems(items, parentID) {            const rst = [];            items.forEach(item => {                // 为了与sheet_common.js -> getTreeNodeCellType共用树结构功能                // 需要设置数据的ID、ParentID                item.ID = curID++;                item.ParentID = parentID || null;                rst.push(item);                if (item.items && item.items.length) {                    rst.push(...extractDataFromItems(item.items, item.ID));                }            });            return rst;        }    },    //展示的结构转换为入库的结构    toSaveDatas: function (datas) {        // ParentID为null的数据是为显示而加入的“工程特征” ParentID为1的数据即为工程特征下的第一层数据,其他数据是从ParentID为1的数据中的items递归打平出来的        // 因此原数据格式就是ParentID为1的数据        const saveData = datas.filter(item => item.ParentID === 1);        clearJunkAttrs(saveData);        return saveData;        function clearJunkAttrs(items) {            items.forEach(item => {                delete item.ID && delete item.ParentID && delete item.collapsed;                if (item.items && item.items.length) {                    clearJunkAttrs(item.items);                }            })        }    },    toUpdate: function (orgDatas, newDatas) {        if (orgDatas.length !== newDatas.length) {            return true;        }        for (let i = 0, len = orgDatas.length; i < len; i++) {            let orgObj = orgDatas[i], newObj = newDatas[i];            if (orgObj.value !== newObj.value) {                return true;            }        }        return false;    },    a_updateInfo: function (datas) {        let me = this;        let url = '/pm/api/updateProjects',            updateData = {                updateType: 'update',                updateData: { ID: parseInt(scUrlUtil.GetQueryString('project')), 'property.projectFeature': datas }            },            postData = {                user_id: userID,                updateData: [updateData]            };        CommonAjax.post(url, postData, function (rstData) {            me.orgDatas = me.toViewDatas(datas);        });    },    initTree: function (sheet, init, datas) {        //sheet.getRange(-1, 0, -1, 1).cellType(this.getTreeNodeCellType(datas));        const parentType = _.groupBy(datas, 'ParentID');        const paint = (ctx, value, x, y, w, h, style, rectInfo, data) => {            const required = typeof data.required === 'string' ? JSON.parse(data.required) : data.required;            const readOnly = typeof data.readOnly === 'string' ? JSON.parse(data.readOnly) : data.readOnly;            if (required && !readOnly) {                const { rectW, margin } = rectInfo;                ctx.save();                ctx.fillStyle = 'red';                const paddingLeft = 8;                const paintX = x + margin + rectW + paddingLeft; // 第一层盒子处[+] / [-]                const paintY = y + Math.round(h / 2) + paddingLeft;                ctx.font = "14px Calibri"                ctx.fillText('*', paintX, paintY);                ctx.restore();            }                    };        for (let i = 0, len = datas.length; i < len; i++) {            if (datas[i].hasOwnProperty('items')) {                let collapsed = false;                if (init) {                    datas[i].collapsed = false;                    collapsed = true;                } else {                    collapsed = datas[i].collapsed == undefined ? true : datas[i].collapsed;                }            }            sheet.getCell(i, 0).cellType(sheetCommonObj.getTreeNodeCellType(datas, i, parentType, paint))        }    },    getTreeNodeCellType: function (data) {        var ns = GC.Spread.Sheets;        var rectW = 10;        var rectH = 10;        var margin = 3;        const indent = 6;        function TreeNodeCellType() {        }        function drowRect(ctx, x, y, w, h) {            ctx.save();            ctx.strokeStyle = "gray";            ctx.translate(0.5, 0.5);            ctx.beginPath();            var rectX = x + margin;            var rectY = y + Math.round(h / 2) - rectH / 2;            ctx.moveTo(rectX, rectY);            ctx.lineTo(rectX, rectY + rectH);            ctx.lineTo(rectX + rectW, rectY + rectH);            ctx.lineTo(rectX + rectW, rectY);            ctx.lineTo(rectX, rectY);            ctx.moveTo(rectX + rectW, y + Math.round(h / 2));            ctx.lineTo(rectX + rectW + 5, y + Math.round(h / 2));            ctx.stroke();            ctx.restore();        }        function drowSymbol(ctx, x, y, w, h, collapsed) {            ctx.save();            ctx.strokeStyle = "#000000";            ctx.translate(0.5, 0.5);            ctx.beginPath();            ctx.moveTo(x + margin + 2, y + Math.round(h / 2));            ctx.lineTo(x + margin + 8, y + Math.round(h / 2));            var rectY = y + Math.round(h / 2) - rectH / 2;            if (collapsed) {                ctx.moveTo(x + margin + rectW / 2, rectY + 2);                ctx.lineTo(x + margin + rectW / 2, rectY + 2 + 6);            }            ctx.stroke();            ctx.restore();        }        function drowSubItem(ctx, x, y, w, h, offset, nextItem) {            offset += indent;            ctx.save();            ctx.strokeStyle = "gray";            ctx.translate(0.5, 0.5);            ctx.beginPath();            ctx.moveTo(x + offset, y);            ctx.lineTo(x + offset, y + Math.round(h / 2));            offset += 9;            ctx.lineTo(x + offset, y + Math.round(h / 2));            if (nextItem && !nextItem.hasOwnProperty('items')) {                ctx.moveTo(x + offset - 9, y + Math.round(h / 2));                ctx.lineTo(x + offset - 9, y + h);            }            ctx.stroke();            ctx.restore();            return offset;        }        TreeNodeCellType.prototype = new ns.CellTypes.Text();        TreeNodeCellType.prototype.paint = function (ctx, value, x, y, w, h, style, options) {            if (value != null) {                var offset = margin + rectW + indent;                var recode = data[options.row];                if (recode && recode.hasOwnProperty('items')) {                    debugger;                    drowRect(ctx, x, y, w, h);                    var collapsed = recode.collapsed == undefined ? true : recode.collapsed;//options.sheet.getTag(options.row,options.col);                    drowSymbol(ctx, x, y, w, h, collapsed);                } else if (recode && !recode.hasOwnProperty('items')) {                    offset = drowSubItem(ctx, x, y, w, h, offset, data[options.row + 1]);                    offset += 1;                }                arguments[2] = x + offset;                arguments[4] = w - offset;                GC.Spread.Sheets.CellTypes.Text.prototype.paint.apply(this, arguments);            }        };        // override getHitInfo to allow cell type get mouse messages        TreeNodeCellType.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            };        }        TreeNodeCellType.prototype.processMouseDown = function (hitinfo) {            var recode = data[hitinfo.row];            let offset = -1,                indent = 20,                halfBoxLength = 5;            let centerX = hitinfo.cellRect.x + offset + indent / 2;            let centerY = (hitinfo.cellRect.y + offset + (hitinfo.cellRect.y + offset + hitinfo.cellRect.height)) / 2            if (recode && recode.hasOwnProperty('items')) {                //方框外1像素内都有效                const boxLengh = 10;                if (hitinfo.x >= centerX - halfBoxLength - 1 && hitinfo.x <= centerX + halfBoxLength + 1 &&                    hitinfo.y >= centerY - halfBoxLength - 1 && hitinfo.y <= centerY + halfBoxLength + 1) {                    var collapsed = recode.collapsed == undefined ? true : recode.collapsed;                    collapsed = !collapsed                    recode.collapsed = collapsed;                    //hitinfo.sheet.setTag(hitinfo.row,hitinfo.col,collapsed);                    hitinfo.sheet.getRange(hitinfo.row + 1, -1, recode.items.length, -1).visible(!collapsed);                    hitinfo.sheet.invalidateLayout();                    hitinfo.sheet.repaint();                }            }        };        return new TreeNodeCellType()    },};$(document).ready(function () {    $('#poj-set').on('shown.bs.modal', function (e) {        //init Spread        projFeatureView.initDatas(projFeatureView.orgDatas);        projFeatureView.buildSheet();        projFeatureView.showData(projFeatureView.datas);        if (projectReadOnly) {            sheetCommonObj.disableSpread(projFeatureView.workBook);        }    });    $('#poj-set').on('hidden.bs.modal', function (e) {        //destroy Spread        if (projFeatureView.workBook) {            projFeatureView.workBook.destroy();            projFeatureView.workBook = null;        }        projFeatureView.datas = [];    });    $('#tab_poj-settings-projFeature').on('shown.bs.tab', function () {        sheetCommonObj.refreshWorkbookDelDefer(projFeatureView.workBook, 100);    });});
 |