/** * * DataTable单元格数据模型, 只在DataTable内部使用. * 由于DataTable使用第三方的GridView, 所以为了适配到自己的架构, 增加的DataTableCellItem * * Created by TonyJiang on 16/11/15. */ import {ReportItem} from "../ReportItem"; import {properties, layout} from "../decorates"; import {DataTableCellProperty, CommonProperty, TextProperty, BoxProperty} from "../properties/define"; import {Unit, RectUnit} from "../../drawing/Unit"; import {Border} from "../properties/classes/Border"; import {Sheet} from "../../../libs/gridview/src/model/sheet/index"; import DataTableCellItemLayout from "../layout/DataTableCellItemLayout"; import {CellPoint} from "../../../libs/gridview/src/model/common/cell-point"; import {Cell} from "../../../libs/gridview/src/model/sheet/cell"; import {PropertyChangeEventArgs} from "../PropertyChangeEventArgs"; import {Property} from "../properties/Property"; import {ReportItemBase} from "../ReportItemBase"; import {Font} from "../properties/classes/Font"; import {BORDER_POSITION, TEXT_ALIGN, VERTICAL_ALIGN} from "../../../libs/gridview/src/model/common/index"; import {HorizontalTextAlignEnum, VerticalTextAlignEnum} from "../../configs/enums"; //TODO 让GridView支持Justified文本对齐方式 const textAlignToGridView = { [HorizontalTextAlignEnum.Left]: TEXT_ALIGN.LEFT, [HorizontalTextAlignEnum.Center]: TEXT_ALIGN.CENTER, [HorizontalTextAlignEnum.Right]: TEXT_ALIGN.RIGHT, [HorizontalTextAlignEnum.Justified]: TEXT_ALIGN.CENTER }; const textAlignFromGridView = { [TEXT_ALIGN.LEFT]: HorizontalTextAlignEnum.Left, [TEXT_ALIGN.CENTER]: HorizontalTextAlignEnum.Center, [TEXT_ALIGN.RIGHT]: HorizontalTextAlignEnum.Right }; //TODO 让GridView支持Justified文本对齐方式 const verticalAlignToGridView = { [VerticalTextAlignEnum.Top]: VERTICAL_ALIGN.TOP, [VerticalTextAlignEnum.Middle]: VERTICAL_ALIGN.MIDDLE, [VerticalTextAlignEnum.Bottom]: VERTICAL_ALIGN.BOTTOM, [VerticalTextAlignEnum.Justified]: VERTICAL_ALIGN.MIDDLE }; const verticalAlignFromGridView = { [VERTICAL_ALIGN.TOP]: VerticalTextAlignEnum.Top, [VERTICAL_ALIGN.MIDDLE]: VerticalTextAlignEnum.Middle, [VERTICAL_ALIGN.BOTTOM]: VerticalTextAlignEnum.Bottom }; @layout(new DataTableCellItemLayout()) @properties(DataTableCellProperty) export class DataTableCellItem extends ReportItem { displayName(): string { return '单元格'; } static CellProperty: Property = new Property('cell', null); defaultPropertyValue = { backColor: 'rgba(256,256,256,0)', textColor: 'rgba(0,0,0,1)' }; constructor(cell: Cell, border: Border){ super(); this.initPropertyValues(cell, border); this.initEvent(); } // get bounds():RectUnit { // return new RectUnit(Unit.pixel(0), Unit.pixel(0), this.size.width, this.size.height);//RectUnit.fromLocationAndSize(this.location, this.size); // } initPropertyValues(cell: Cell, border: Border){ this.setPropertyValue(DataTableCellItem.CellProperty, cell); this.setPropertyValue(DataTableCellProperty.text, cell.text); var font:Font = new Font(); if(cell.font){ const [fontSize, fontFamily] = cell.font.split(" "); font.fontSize = Unit.pixel(parseInt(fontSize)); font.fontFamily = fontFamily; } this.setPropertyValue(DataTableCellProperty.font, font); this.setPropertyValue(DataTableCellProperty.backColor, cell.background || this.defaultPropertyValue.backColor); this.setPropertyValue(DataTableCellProperty.textColor, cell.textColor || this.defaultPropertyValue.textColor); this.setPropertyValue(DataTableCellProperty.hAlign, textAlignFromGridView[cell.textAlign]); this.setPropertyValue(DataTableCellProperty.vAlign, verticalAlignFromGridView[cell.verticalAlign]); this.setPropertyValue(DataTableCellProperty.border, border); } initEvent(){ this.on(DataTableCellItem.PropertyChangeEventName, this.onPropertyChange); } //单元格自身属性在这里进行监听改变, 行高, 列宽, 边框由于不是Cell自身属性, 所以在DataTableEditingLayer中进行改变 onPropertyChange = (data: PropertyChangeEventArgs) => { switch(data.property){ case DataTableCellProperty.text: this.changeHandler.textChange(data.newValue); break; case DataTableCellProperty.backColor: this.changeHandler.backColorChange(data.newValue); break; case DataTableCellProperty.textColor: this.changeHandler.textColorChange(data.newValue); break; case DataTableCellProperty.font: this.changeHandler.textFontChange(data.newValue); break; case DataTableCellProperty.hAlign: this.changeHandler.hAlignChange(data.newValue); break; case DataTableCellProperty.vAlign: this.changeHandler.vAlignChange(data.newValue); break; } } private changeHandler = { textChange: (newText: string) => { const newCell = this.getPropertyValue(DataTableCellItem.CellProperty).setText(newText); this.setPropertyValue(DataTableCellItem.CellProperty, newCell); }, backColorChange: (newColor: string) => { const newCell = this.getPropertyValue(DataTableCellItem.CellProperty).setBackground(newColor); this.setPropertyValue(DataTableCellItem.CellProperty, newCell); }, textColorChange: (newColor: string) => { const newCell = this.getPropertyValue(DataTableCellItem.CellProperty).setTextColor(newColor); this.setPropertyValue(DataTableCellItem.CellProperty, newCell); }, textFontChange: (newFont: Font) => { const fontString = newFont.fontSize.toPixel() + 'px ' + newFont.fontFamily; const newCell = this.getPropertyValue(DataTableCellItem.CellProperty).setFont(fontString); this.setPropertyValue(DataTableCellItem.CellProperty, newCell); }, hAlignChange: (newAlign: HorizontalTextAlignEnum) => { const newCell = this.getPropertyValue(DataTableCellItem.CellProperty).setTextAlign(textAlignToGridView[newAlign]); this.setPropertyValue(DataTableCellItem.CellProperty, newCell); }, vAlignChange: (newAlign: VerticalTextAlignEnum) => { const newCell = this.getPropertyValue(DataTableCellItem.CellProperty).setVerticalAlign(verticalAlignToGridView[newAlign]); this.setPropertyValue(DataTableCellItem.CellProperty, newCell); } } visit(){} }