123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520 |
- /**
- * Created by Mai on 2017/4/1.
- */
- var TREE_SHEET_HELPER = {
- getObjPos: function (obj) {
- let target = obj;
- let pos = {x: obj.offsetLeft, y: obj.offsetTop};
- target = obj.offsetParent;
- while (target) {
- pos.x += target.offsetLeft;
- pos.y += target.offsetTop;
- target = target.offsetParent;
- }
- return pos;
- },
- /**
- * 初始化setting,需要提示单元格text时,必须先初始化setting
- * @param obj
- * @param setting
- */
- /*initSetting: function (obj, setting) {
- setting.pos = this.getObjPos(obj);
- },*/
- initSetting: function (obj, setting) {
- setting.pos = this.getObjPos(obj);
- },
- createNewSpread: function (obj) {
- var spread = new GC.Spread.Sheets.Workbook(obj, {sheetCount: 1});
- spread.options.tabStripVisible = false;
- spread.options.scrollbarMaxAlign = true;
- spread.options.cutCopyIndicatorVisible = false;
- spread.options.allowCopyPasteExcelStyle = false;
- spread.options.allowUserDragDrop = false;
- spread.getActiveSheet().setRowCount(3);
- return spread;
- },
- getSheetCellStyle: function (setting) {
- var style = new GC.Spread.Sheets.Style();
- //style.locked = setting.readOnly ? true : false;
- style.name = setting.id;
- //style.font = setting.data.font;
- style.hAlign = setting.data.hAlign;
- style.vAlign = setting.data.vAlign;
- style.wordWrap = setting.data.wordWrap;
- if (setting.data.formatter) {
- style.formatter = setting.data.formatter;
- }
- return style;
- },
- loadSheetHeader: function (setting, sheet) {
- this.massOperationSheet(sheet, function () {
- if (setting.frozenCols) {
- sheet.frozenColumnCount(setting.frozenCols);
- }
- sheet.setColumnCount(setting.cols.length);
- sheet.setRowCount(setting.headRows, GC.Spread.Sheets.SheetArea.colHeader);
- setting.headRowHeight.forEach(function (rowHeight, index) {
- sheet.setRowHeight(index, rowHeight, GC.Spread.Sheets.SheetArea.colHeader);
- });
- setting.cols.forEach(function (col, index) {
- var i, iRow = 0, cell;
- for (i = 0; i < col.head.spanCols.length; i++) {
- if (col.head.spanCols[i] !== 0) {
- cell = sheet.getCell(iRow, index, GC.Spread.Sheets.SheetArea.colHeader);
- //cell.value(col.head.titleNames[i]).font(col.head.font).hAlign(col.head.hAlign[i]).vAlign(col.head.vAlign[i]).wordWrap(true);
- cell.value(col.head.titleNames[i]).hAlign(col.head.hAlign[i]).vAlign(col.head.vAlign[i]).wordWrap(true);
- }
- if (col.head.spanCols[i] > 1 || col.head.spanRows[i] > 1) {
- sheet.addSpan(iRow, index, col.head.spanRows[i], col.head.spanCols[i], GC.Spread.Sheets.SheetArea.colHeader);
- }
- iRow += col.head.spanRows[i];
- };
- sheet.setColumnWidth(index, col.width);
- sheet.setColumnVisible(index, col.visible && true);
- });
- });
- },
- protectdSheet: function (sheet) {
- var option = {
- allowSelectLockedCells: true,
- allowSelectUnlockedCells: true,
- allowResizeRows: true,
- allowResizeColumns: true
- };
- sheet.options.protectionOptions = option;
- sheet.options.isProtected = true;
- sheet.options.allowCellOverflow = false;
- },
- massOperationSheet: function (sheet, Operation) {
- sheet.suspendPaint();
- sheet.suspendEvent();
- Operation();
- sheet.resumeEvent();
- sheet.resumePaint();
- },
- refreshNodesVisible: function (nodes, sheet, recursive) {
- nodes.forEach(function (node) {
- var iRow;
- iRow = node.serialNo();
- sheet.setRowVisible(iRow, node.visible, GC.Spread.Sheets.SheetArea.viewport);
- if (recursive) {
- TREE_SHEET_HELPER.refreshNodesVisible(node.children, sheet, recursive);
- }
- })
- },
- refreshTreeNodeData: function (setting, sheet, nodes, recursive) {
- nodes.forEach(function (node) {
- let iRow = node.serialNo();
- if(typeof projectObj !== 'undefined'){
- let nodeStyle = projectObj.getNodeColorStyle(sheet, node);
- if(node.data.bgColour){
- nodeStyle.backColor = node.data.bgColour;
- }
- if(nodeStyle){
- sheet.setStyle(iRow, -1, nodeStyle);
- }
- }
- setting.cols.forEach(function (colSetting, iCol) {
- var cell = sheet.getCell(iRow, iCol, GC.Spread.Sheets.SheetArea.viewport);
- /* if(typeof projectObj !== 'undefined'){ 7/28 取消黑体显示
- let boldFontStyle = projectObj.getBoldFontStyle(node, colSetting);
- sheet.setStyle(iRow, iCol, boldFontStyle);
- }
- }*/
- // var getFieldText = function () {
- // var fields = colSetting.data.field.split('.');
- // var validField = fields.reduce(function (field1, field2) {
- // if (eval('node.data.' + field1)) {
- // return field1 + '.' + field2
- // } else {
- // return field1;
- // }
- // });
- // if (eval('node.data.' + validField)) {
- // return eval('node.data.' + validField);
- // } else {
- // return '';
- // }
- // };
- var getFieldText2 = function () {
- var fields = colSetting.data.field.split('.'), iField, data = node.data;
- for (iField = 0; iField < fields.length; iField++) {
- if (data[fields[iField]] && data[fields[iField]]!='0') {
- data = data[fields[iField]];
- } else {
- return '';
- }
- }
- return data;
- };
- if(sheet.name()=="mainSheet"){
- if(colSetting.data.field=="quantity"){
- let tag = node.data.quantityEXP?node.data.quantityEXP:'';
- sheet.setTag(iRow, iCol,tag);
- }
- if(colSetting.data.field=="code"){
- let tag = node.data.adjustState?node.data.adjustState:'';
- sheet.setTag(iRow, iCol,tag);
- }
- if(colSetting.data.field=="name"){
- let tag = node.data.itemCharacterText?node.data.itemCharacterText:'';
- sheet.setTag(iRow, iCol,tag);
- }
- }
- if (colSetting.data.getText && Object.prototype.toString.apply(colSetting.data.getText) === "[object Function]") {
- cell.value(colSetting.data.getText(node));
- } else {
- cell.value(getFieldText2());
- }
- if (colSetting.data.cellType && Object.prototype.toString.apply(colSetting.data.cellType) !== "[object String]") {
- cell.cellType(colSetting.data.cellType(node));
- }
- if(colSetting.data.autoHeight == true){
- colSetting.setAutoHeight(cell,node);
- }
- if(colSetting.editChecking&&colSetting.editChecking(node)){
- cell.locked(true);
- }else if (colSetting.readOnly) {
- if(typeof projectReadOnly !== 'undefined' && projectReadOnly){
- cell.locked(true);
- }else {
- if (Object.prototype.toString.apply(colSetting.readOnly) === "[object Function]") {
- cell.locked(colSetting.readOnly(node));
- } else {
- cell.locked(true);
- }
- }
- } else {
- cell.locked(typeof projectReadOnly !== 'undefined' && projectReadOnly ? true : false);
- }
- });
- if(setting.setAutoFitRow){
- setting.setAutoFitRow(sheet,node)
- }
- if (recursive) {
- TREE_SHEET_HELPER.refreshTreeNodeData(setting, sheet, node.children, recursive);
- }
- });
- },
- refreshChildrenVisiable:function(sheet,tree,node,row,visiable){
- let iCount = node.posterityCount(), i, child;
- for (i = 0; i < iCount; i++) {
- child = tree.items[row + i +1];
- sheet.setRowVisible(row + i + 1, visiable?visiable:child.visible, GC.Spread.Sheets.SheetArea.viewport);
- }
- sheet.invalidateLayout();
- },
- showTreeData: function (setting, sheet, tree) {
- let indent = 20;
- let levelIndent = -5;
- let halfBoxLength = 5;
- let halfExpandLength = 3;
- let TreeNodeCellType = function () {
- };
- TreeNodeCellType.prototype = new GC.Spread.Sheets.CellTypes.Text();
- TreeNodeCellType.prototype.paint = function (ctx, value, x, y, w, h, style, options) {
- if (style.backColor) {
- ctx.save();
- ctx.fillStyle = style.backColor;
- ctx.fillRect(x, y, w, h);
- ctx.restore();
- } else {
- ctx.clearRect(x, y, w, h);
- }
- // ������(x1, y1)���(��, ��), (x2, y2)�յ�(��, ��), ��ɫ
- let drawLine = function (canvas, x1, y1, x2, y2, color) {
- ctx.save();
- // ����ƫ����
- ctx.translate(0.5, 0.5);
- ctx.beginPath();
- ctx.moveTo(x1, y1);
- ctx.lineTo(x2, y2);
- ctx.strokeStyle = color;
- ctx.stroke();
- ctx.restore();
- };
- let drawExpandBox = function (ctx, x, y, w, h, centerX, centerY, expanded) {
- let rect = {}, h1, h2, offset = 1;
- rect.top = centerY - halfBoxLength;
- rect.bottom = centerY + halfBoxLength;
- rect.left = centerX - halfBoxLength;
- rect.right = centerX + halfBoxLength;
- if (rect.left < x + w) {
- rect.right = Math.min(rect.right, x + w);
- ctx.save();
- // ����ƫ����
- ctx.translate(0.5, 0.5);
- ctx.strokeStyle = 'black';
- ctx.beginPath();
- ctx.moveTo(rect.left, rect.top);
- ctx.lineTo(rect.left, rect.bottom);
- ctx.lineTo(rect.right, rect.bottom);
- ctx.lineTo(rect.right, rect.top);
- ctx.lineTo(rect.left, rect.top);
- ctx.stroke();
- ctx.fillStyle = 'white';
- ctx.fill();
- ctx.restore();
- // Draw Horizontal Line
- h1 = centerX - halfExpandLength;
- h2 = Math.min(centerX + halfExpandLength, x + w);
- if (h2 > h1) {
- drawLine(ctx, h1, centerY, h2, centerY, 'black');
- }
- // Draw Vertical Line
- if (!expanded && (centerX < x + w)) {
- drawLine(ctx, centerX, centerY - halfExpandLength, centerX, centerY + halfExpandLength, 'black');
- }
- }
- }
- let node = tree.items[options.row];
- let showTreeLine = true;
- if (!node) { return; }
- let centerX = Math.floor(x) + node.depth() * indent + node.depth() * levelIndent + indent / 2;
- let x1 = centerX + indent / 2;
- let centerY = Math.floor((y + (y + h)) / 2);
- let y1;
- // Draw Sibling Line
- if (showTreeLine) {
- // Draw Horizontal Line
- if (centerX < x + w) {
- drawLine(ctx, centerX, centerY, Math.min(x1, x + w), centerY, 'gray');
- }
- // Draw Vertical Line
- if (centerX < x + w) {
- y1 = node.isLast() ? centerY : y + h;
- if (node.isFirst() && !node.parent) {
- drawLine(ctx, centerX, centerY, centerX, y1, 'gray');
- } else {
- drawLine(ctx, centerX, y, centerX, y1, 'gray');
- }
- }
- }
- // Draw Expand Box
- if (node.children.length > 0) {
- drawExpandBox(ctx, x, y, w, h, centerX, centerY, node.expanded);
- }
- // Draw Parent Line
- if (showTreeLine) {
- var parent = node.parent, parentCenterX = centerX - indent - levelIndent;
- while (parent) {
- if (!parent.isLast()) {
- if (parentCenterX < x + w) {
- drawLine(ctx, parentCenterX, y, parentCenterX, y + h, 'gray');
- }
- }
- parent = parent.parent;
- parentCenterX -= (indent + levelIndent);
- }
- };
- // Draw Text
- x = x + (node.depth() + 1) * indent + node.depth() * levelIndent;
- w = w - (node.depth() + 1) * indent - node.depth() * levelIndent;
- GC.Spread.Sheets.CellTypes.Text.prototype.paint.apply(this, arguments);
- };
- 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) {
- let offset = -1;
- let node = tree.items[hitinfo.row];
- tree.selected = node;
- if (!node || node.children.length === 0) { return; }
- let centerX = hitinfo.cellRect.x + offset + node.depth() * indent + node.depth() * levelIndent + indent / 2;
- let centerY = (hitinfo.cellRect.y + offset + (hitinfo.cellRect.y + offset + hitinfo.cellRect.height)) / 2;
- if (hitinfo.x > centerX - halfBoxLength && hitinfo.x < centerX + halfBoxLength && hitinfo.y > centerY - halfBoxLength && hitinfo.y < centerY + halfBoxLength) {
- node.setExpanded(!node.expanded);
- let sheetName = hitinfo.sheet.name();
- if(sheetName === 'stdBillsLib_bills'){
- sessionStorage.setItem('stdBillsLibExpState', billsLibObj.stdBillsTree.getExpState(billsLibObj.stdBillsTree.items));
- }
- else if(sheetName === 'stdRationLib_chapter'){
- sessionStorage.setItem('stdRationLibExpState', rationLibObj.tree.getExpState(rationLibObj.tree.items));
- }
- else if(sheetName === 'stdBillsGuidance_bills'){
- sessionStorage.setItem('stdBillsGuidanceExpState', billsGuidance.bills.tree.getExpState(billsGuidance.bills.tree.items));
- }
- TREE_SHEET_HELPER.massOperationSheet(hitinfo.sheet, function () {
- let iCount = node.posterityCount(), i, child;
- for (i = 0; i < iCount; i++) {
- child = tree.items[hitinfo.row + i + 1];
- hitinfo.sheet.setRowVisible(hitinfo.row + i + 1, child.visible, hitinfo.sheetArea);
- //hitinfo.sheet.setRowVisible(hitinfo.row + i + 1, child.vis(), hitinfo.sheetArea);
- }
- hitinfo.sheet.invalidateLayout();
- });
- hitinfo.sheet.repaint();
- }
- };
- TreeNodeCellType.prototype.processMouseEnter = function(hitinfo){
- let text = hitinfo.sheet.getText(hitinfo.row, hitinfo.col);
- let tag = hitinfo.sheet.getTag(hitinfo.row, hitinfo.col);
- if(tag){
- TREE_SHEET_HELPER.showTipsDiv(tag,setting,hitinfo);
- }
- };
- TreeNodeCellType.prototype.processMouseLeave = function (hitinfo) {
- let me = this;
- TREE_SHEET_HELPER.tipDiv = 'hide';
- if (me._toolTipElement) {
- $(me._toolTipElement).hide();
- me._toolTipElement = null;
- };
- TREE_SHEET_HELPER.tipDivCheck();//延时检查:当tips正在show的时候,就调用了hide方法,会导致tips一直存在,所以设置一个超时处理
- };
- let TipCellType = function () {};
- TipCellType.prototype = new GC.Spread.Sheets.CellTypes.Text();
- TipCellType.prototype.getHitInfo = function (x, y, cellStyle, cellRect, context) {
- return {
- x: x,
- y: y,
- row: context.row,
- col: context.col,
- cellStyle: cellStyle,
- cellRect: cellRect,
- sheet: context.sheet,
- sheetArea: context.sheetArea
- };
- };
- TipCellType.prototype.processMouseEnter = function (hitinfo) {
- let text = hitinfo.sheet.getText(hitinfo.row, hitinfo.col);
- let value = hitinfo.sheet.getValue(hitinfo.row, hitinfo.col);
- let tag = hitinfo.sheet.getTag(hitinfo.row, hitinfo.col);
- let acStyle = hitinfo.sheet.getActualStyle(hitinfo.row, hitinfo.col),
- zoom = hitinfo.sheet.zoom();
- let textLength = this.getAutoFitWidth(value, text, acStyle, zoom, {sheet: hitinfo.sheet, row: hitinfo.row, col: hitinfo.col, sheetArea: GC.Spread.Sheets.SheetArea.viewport});
- let cellWidth = hitinfo.sheet.getCell(-1, hitinfo.col).width();
- let dataField = setting.cols[hitinfo.col].data.field;
- if(tag==''&&hitinfo.sheet.getCell(hitinfo.row,hitinfo.col).wordWrap()==true){//显示其它列的标记为空并且设置了自动换行
- return;
- }
- if(dataField === 'itemCharacterText' || dataField === 'jobContentText' || dataField === 'adjustState'){
- if((hitinfo.sheet.getParent() === projectObj.mainSpread||hitinfo.sheet.getParent() === tender_obj.tenderSpread) && textLength <= cellWidth)
- return;
- }
- if(hitinfo.sheet.name()=="mainSheet"){
- if(dataField=="quantity"){//显示工程量明细
- text = tag;
- }if(dataField=="name"){//项目特征及内容隐藏时,显示特征及内容
- if(projectObj.ifItemCharHiden(setting)&&tag!=''){
- text = tag;
- }else if(textLength <= cellWidth){
- return;
- }
- }
- else if(tag !== undefined && tag) {
- text = tag;
- }
- }
- TREE_SHEET_HELPER.showTipsDiv(text,setting,hitinfo);
- };
- TipCellType.prototype.processMouseLeave = function (hitinfo) {
- let me = this;
- TREE_SHEET_HELPER.tipDiv = 'hide';
- if (me._toolTipElement) {
- $(me._toolTipElement).hide();
- me._toolTipElement = null;
- };
- TREE_SHEET_HELPER.tipDivCheck();//延时检查:当tips正在show的时候,就调用了hide方法,会导致tips一直存在,所以设置一个超时处理
- }
- TREE_SHEET_HELPER.protectdSheet(sheet);
- TREE_SHEET_HELPER.massOperationSheet(sheet, function () {
- sheet.rowOutlines.direction(GC.Spread.Sheets.Outlines.OutlineDirection.backward);
- sheet.showRowOutline(false);
- if (setting.defaultRowHeight) {
- sheet.defaults.rowHeight = setting.defaultRowHeight;
- }
- sheet.setRowCount(tree.count() + setting.emptyRows, GC.Spread.Sheets.SheetArea.viewport);
- sheet.getRange(tree.count(), -1, setting.emptyRows, -1).locked(true);
- setting.cols.forEach(function (colSetting, iCol) {
- sheet.setStyle(-1, iCol, TREE_SHEET_HELPER.getSheetCellStyle(colSetting));
- if (colSetting.showHint) {
- sheet.getRange(-1, iCol, -1, 1).cellType(new TipCellType());
- }
- if(colSetting.formatter){
- sheet.setFormatter(-1, iCol, colSetting.formatter, GC.Spread.Sheets.SheetArea.viewport);
- }
- });
- sheet.getRange(-1, setting.treeCol, -1, 1).cellType(new TreeNodeCellType());
- TREE_SHEET_HELPER.refreshTreeNodeData(setting, sheet, tree.roots, true);
- TREE_SHEET_HELPER.refreshNodesVisible(tree.roots, sheet, true);
- });
- },
- showTipsDiv:function (text,setting,hitinfo) {
- if (setting.pos && text && text !== '') {
- if (!this._toolTipElement) {
- let div = $('#autoTip')[0];
- if (!div) {
- div = document.createElement("div");
- $(div).css("position", "absolute")
- .css("border", "1px #C0C0C0 solid")
- .css("box-shadow", "1px 2px 5px rgba(0,0,0,0.4)")
- .css("font", "9pt Arial")
- .css("background", "white")
- .css("padding", 5)
- .attr("id", 'autoTip');
- $(div).hide();
- document.body.insertBefore(div, null);
- }
- this._toolTipElement = div;
- //实时读取位置信息
- if(hitinfo.sheet && hitinfo.sheet.getParent().qo){
- setting.pos = SheetDataHelper.getObjPos(hitinfo.sheet.getParent().qo);
- }
- $(this._toolTipElement).text(text);
- //清单指引、清单库做特殊处理
- if($(hitinfo.sheet.getParent().qo).attr('id') === 'stdBillsSpread' || $(hitinfo.sheet.getParent().qo).attr('id') === 'billsGuidance_bills'){
- $(this._toolTipElement).css('top', '').css('left', '').css('width', '');
- let marginLeftMouse;
- if($(this._toolTipElement).width() < hitinfo.x){
- marginLeftMouse = hitinfo.x - $(this._toolTipElement).width();
- }
- $(this._toolTipElement).css("top", setting.pos.y + hitinfo.y + 15).css("left", marginLeftMouse ? setting.pos.x + marginLeftMouse : setting.pos.x);
- }
- else {
- $(this._toolTipElement).css("top", setting.pos.y + hitinfo.y + 15).css("left", setting.pos.x + hitinfo.x + 15);
- }
- $(this._toolTipElement).show("fast");
- TREE_SHEET_HELPER.tipDiv = 'show';//做个标记
- }
- }
- },
- tipDivCheck(){
- setTimeout(function () {
- let tips = $('#autoTip');
- if(TREE_SHEET_HELPER.tipDiv == 'show'){
- return;
- } else if(TREE_SHEET_HELPER.tipDiv == 'hide'&&tips){
- tips.hide();
- TREE_SHEET_HELPER._toolTipElement = null;
- }
- },600)
- }
- };
|