import {observable, transaction, action, IObservableArray, computed} from "mobx/lib/mobx"; import {ReportItemBase} from "./ReportItemBase"; import {Unit, SizeUnit} from "../drawing/Unit"; import {properties, layout} from "./decorates"; import {ReportProperty} from "./properties/define"; import {ReportLayout} from "./layout/ReportLayout"; import SnackBar from '../helper/snackbar'; import {ReportBandBase} from "./band/ReportBandBase"; import {IContainer, IComponent} from "../interfaces/Interfaces"; import {GroupCollection, Group} from "./band/Group"; import {GroupHeaderBand, GroupFooterBand, GroupBand} from "./band/GroupBand"; import {TitleBand} from "./band/TitleBand"; import {PageHeaderBand, PageFooterBand} from "./band/PageBand"; import {ColumnHeaderBand, ColumnFooterBand} from "./band/ColumnBand"; import {DetailBand} from "./band/DetailBand"; import {JRPropertiesMap} from "../jasper/JRPropertiesMap"; import {JRPropertiesHolder} from "../jasper/JRPropertiesHolder"; import {JRVisitor} from "../jasper/JRVisitor"; import {JRReport} from "../jasper/JRReport"; import {JRGroup} from "../jasper/JRGroup"; import {JRBand} from "../jasper/JRBand"; import {JRSection} from "../jasper/JRSection"; import {JRDesignSection} from "../jasper/JRDesignSection"; import {WhenResourceMissingTypeEnum, ReportDesignMode} from "../configs/enums"; import {DesignerHost} from "../design/DesignerHost"; import {SelectionService} from '../design/behavior/SelectionService'; import {SelectionTypes} from "../design/behavior/Enums"; import {ReportItem} from "./ReportItem"; import {PropertyChangeEventArgs} from "./PropertyChangeEventArgs"; import {Layout} from "./layout/Layout"; import {PaperSettings} from "../drawing/PaperSettings"; import {checkItemType} from './CheckItemType'; import {ReportBand} from "./band/ReportBand"; class ReportGroupCollection extends GroupCollection { private _report: Report; constructor(report: Report) { super(); this._report = report; } protected onItemAdded(index: number, item: Group): void { super.onItemAdded(index, item); this._report.onGroupAdded(item); } protected onItemRemoved(index: number, item: Group): void { super.onItemRemoved(index, item); this._report.onGroupRemoved(item); } } @layout(new ReportLayout()) @properties(ReportProperty) export class Report extends ReportItemBase implements IContainer, JRReport { private _isUnbound = true; @observable private _forceDisplay = 1; // band宽度 @observable private _bandWidth: Unit; // 当前纸张 与纸张相关的都从此paper中取, 其它属性废弃 @observable private _paper: PaperSettings; private _groups: GroupCollection; private _designMode: ReportDesignMode = ReportDesignMode.ChineseTable; // private _details: IObservableArray = observable([]); private _detailSection: JRDesignSection; private _columnCount = 1; private _bands; private _bandOrder = ["TitleBand", "PageHeaderBand", "ColumnHeaderBand", "content", "ColumnFooterBand", "PageFooterBand"]; static OnResizeChangeEventName = "OnResizeChangeEvent"; constructor(paper: PaperSettings) { super(); this.paper = paper; this._detailSection = new JRDesignSection(); this._groups = new ReportGroupCollection(this); this._bandWidth = new Unit(this.width.pixels - paper.leftMargin.pixels - paper.rightMargin.pixels); this.leftMargin = paper.leftMargin; this.rightMargin = paper.rightMargin; this.topMargin = paper.topMargin; this.bottomMargin = paper.bottomMargin; this._bands = { TitleBand: null, PageHeaderBand: null, ColumnHeaderBand: null, Groups: this._groups, Details: this._detailSection.getBands(), ColumnFooterBand: null, PageFooterBand: null }; // this.paperType = PaperTypeEnum.A4; this.setReport(this); } displayName(): string { return '报表'; } dispose() { super.dispose(); this._isUnbound = true; } get designMode(): ReportDesignMode { return this._designMode; } set designMode(value: ReportDesignMode) { this._designMode = value; } get isUnbound(): boolean { return this._isUnbound; } set isUnbound(value: boolean) { this._isUnbound = value; } getDesignerLayout(): Layout { return super.getDesignerLayout(); } @action protected onPropertyChanged(args: PropertyChangeEventArgs): void { super.onPropertyChanged(args); switch (args.property) { case ReportProperty.paperKind: // 改变纸张类型 this._paper.paperKind = args.newValue; this.setPropertyValue(ReportProperty.paperWidth, this._paper.paperSize.width, true); this.setPropertyValue(ReportProperty.paperHeight, this._paper.paperSize.height, true); this.onResize(); break; case ReportProperty.paperWidth: this._paper.paperSize = new SizeUnit(args.newValue, this._paper.paperSize.height); this.onResize(); break; case ReportProperty.paperHeight: this._paper.paperSize = new SizeUnit(this._paper.paperSize.width, args.newValue); this.onResize(); break; } } get bandWidth(): Unit { return this._bandWidth; } set bandWidth(value: Unit) { this._bandWidth = value; } get columnCount(): number { return this._columnCount; } set columnCount(value: number) { this._columnCount = value; } get columnWidth(): Unit { return this.width.subtract(this.leftMargin).subtract(this.rightMargin); } get columnSpacing(): Unit { return new Unit(0); } get name(): string { return this.getPropertyValue(ReportProperty.name); } set name(value: string) { this.setPropertyValue(ReportProperty.name, value); } get pageWidth(): Unit { // TODO: 暂时以 width 代替 return this.width; } get pageHeight(): Unit { // TODO: 暂时以 height 代替 return this.height; } get groups(): GroupCollection { return this._groups; } get paper(): PaperSettings { return this._paper; } set paper(paper: PaperSettings) { this._paper = paper; this.setPropertyValue(ReportProperty.paperKind, paper.paperKind, true); this.setPropertyValue(ReportProperty.paperWidth, paper.paperSize.width, true); this.setPropertyValue(ReportProperty.paperHeight, paper.paperSize.height, true); } // // 废弃 参考 paper // set paperType(type: PaperTypeEnum){ // // this._bandWidth = new Unit(getPaperSize(type).width.pixels - this.leftMargin.pixels - this.rightMargin.pixels); // this._bandWidth = getPaperSize(type).width.subtract(this.leftMargin).subtract(this.rightMargin); // // transaction(() => { // this.setPropertyValue(ReportProperty.paperType, type); // // // 自定义类型不改变已有的宽度和高度。 // if (type !== PaperTypeEnum.Custom) { // this.setPropertyValue(ReportProperty.paperWidth, getPaperSize(type).width); // this.setPropertyValue(ReportProperty.paperHeight, getPaperSize(type).height); // } // }); // // this.onResize(); // } // // 废弃 // get paperType(): PaperTypeEnum{ // return this.getPropertyValue(ReportProperty.paperType); // } // // // 废弃 // get paperSize(){ // return { // width: this.getPropertyValue(ReportProperty.paperWidth), // height: this.getPropertyValue(ReportProperty.paperHeight) // } // } setNeedDisplay(): any { this._forceDisplay++; } /** * 返回容器中所有的子组件. */ getComponents(): IComponent[] { const allComponents = []; for (let child of this.items) { this.getComponentsRecursive(child, allComponents); } return allComponents; } private getComponentsRecursive(item: ReportItemBase, list: Array): void { list.push(item); for (let child of item.items) { this.getComponentsRecursive(child, list); } } get forceDisplay(): number { return this._forceDisplay; } @computed get width(): Unit { return this._paper.paperSize.width; } @computed get height(): Unit { return this._paper.paperSize.height; } get leftMargin() { return this.getPropertyValue(ReportProperty.leftMargin); } set leftMargin(value: Unit) { const oldValue: Unit = this.leftMargin; this.setPropertyValue(ReportProperty.leftMargin, value); // this._bandWidth = this._bandWidth.add(oldValue.subtract(value)); } get rightMargin(): Unit { return this.getPropertyValue(ReportProperty.rightMargin); } set rightMargin(value: Unit) { this.setPropertyValue(ReportProperty.rightMargin, value); // this._bandWidth = new Unit(this.width.pixels - value.pixels - this.leftMargin.pixels); } get topMargin() { return this.getPropertyValue(ReportProperty.topMargin); } set topMargin(value: Unit) { this.setPropertyValue(ReportProperty.topMargin, value); } get bottomMargin() { return this.getPropertyValue(ReportProperty.bottomMargin); } set bottomMargin(value: Unit) { this.setPropertyValue(ReportProperty.bottomMargin, value); } canParent(item: ReportItemBase): boolean { if (!(item instanceof ReportBandBase)) { return false; } return super.canParent(item); } // addGroup(group:Group):void { // this.groups.push(group); // } onGroupAdded(group: Group): void { group.report = this; } onGroupRemoved(group: Group): void { group.report = null; } protected onItemAdded(item: ReportItemBase): void { super.onItemAdded(item); /*if (item instanceof ReportBandBase) { item.width = new Unit(this.width.pixels-this.leftMargin.pixels-this.rightPadding.pixels); }*/ } protected onResize() { /*for (let item of this.items) { if (item instanceof ReportBandBase) { item.width = this.width; } }*/ this.emitEvent(Report.OnResizeChangeEventName); } get titleBand(): TitleBand { // return this.items.find(item => item.constructor.name === TitleBand.name) as any; return this._bands.TitleBand; } get pageHeaderBand(): PageHeaderBand { // return this.items.find(item => item.constructor.name === PageHeaderBand.name) as any; return this._bands.PageHeaderBand; } get pageFooterBand(): PageFooterBand { // return this.items.find(item => item.constructor.name === PageFooterBand.name) as any; return this._bands.PageFooterBand; } get columnHeaderBand(): ColumnHeaderBand { // return this.items.find(item => item.constructor.name === ColumnHeaderBand.name) as any; return this._bands.ColumnHeaderBand; } get columnFooterBand(): ColumnFooterBand { // return this.items.find(item => item.constructor.name === ColumnFooterBand.name) as any; return this._bands.ColumnFooterBand; } get detailBands(): DetailBand[] { // return this.items.filter(item => item.constructor.name === DetailBand.name).map(item => item as any); return this._detailSection.getBands() as any; } get detailBandCount() { return this._detailSection.getBands().length; } // 添加一个group addGroup() { const group = new Group(true); this._groups.push(group); this.orderBand(); console.log(this._groups); return group; } // 添加一个band addBand(bandName: string) { let band; switch (bandName) { case 'TitleBand': band = new TitleBand(); break; case 'PageHeaderBand': band = new PageHeaderBand(); break; case 'PageFooterBand': band = new PageFooterBand(); break; case 'ColumnHeaderBand': band = new ColumnHeaderBand(); break; case 'ColumnFooterBand': band = new ColumnFooterBand(); break; case 'DetailBand': band = new DetailBand(); break; default: break; } if (bandName === "DetailBand") { this._bands.Details.push(band); } else { this._bands[bandName] = band; } this.items.push(band); this.orderBand(); return band; } //当新增的时候,重新整理Band @action orderBand() { //重排detail const orderDetail = () => { this._detailSection.getBands().map((item)=> { this.items.push(item as any); }); }; //重排group const orderGroup = (index) => { const group = this._groups[index]; if (group.groupHeader) { this.items.push(group.groupHeader); } (this._groups[index + 1]) ? orderGroup(index + 1) : orderDetail(); if (group.groupFooter) { this.items.push(group.groupFooter); } }; //清空items this.items.length = 0; //重新band排序 this._bandOrder.map(bandName => { if (bandName === "content") { (this._groups.length > 0) ? orderGroup(0) : orderDetail(); } else if (this._bands[bandName]) { this.items.push(this._bands[bandName]); } }); } // 添加一个GroupBand addGroupBand(bandName, group) { //只有当group缺少header或footer时,才可能出现通过BandItem来添加GroupHeaderBand,GroupFooterBand的情况 if (bandName.indexOf('Header') > -1) { const band = new GroupHeaderBand(); group.groupHeader = band; } else if (bandName.indexOf('Footer') > -1) { const band = new GroupFooterBand(); group.groupFooter = band; } } // 删除一个GroupBand delBand(band) { const bandName = band.constructor.name; const parent = band.parent; parent.items.remove(band); if (bandName === "DetailBand") { this._detailSection.removeBand(band); // const index = this._details.findIndex( item => item === band ); // console.log("------------->",index); // if(index > -1){ // this._details.splice(index,1); // } } //如果是group的,还要删除group相关 else if (bandName === "GroupHeaderBand") { band.group.groupHeader = null; } else if (bandName === "GroupFooterBand") { band.group.groupFooter = null; } else { this._bands[bandName] = null; } } delGroup(group) { console.log("delGroup"); if (group.groupHeader) { this.items.remove(group.groupHeader); group.groupHeader = null; } if (group.groupFooter) { this.items.remove(group.groupFooter); group.groupFooter = null; } } /** * 移动一个 item 到一个 band 或容器 item 内 */ moveItem(containerItem: ReportItemBase, item: ReportItem) { const parent = containerItem as ReportBand; const acceptResult = parent.canAcceptItem ? parent.canAcceptItem(item) : {result: true}; //是否可以移动 if (acceptResult.result) { item.parent = containerItem; item.top = new Unit(0, item.top.type); const designer = DesignerHost.instance().getDesigner(item); if (designer) { designer.updateMinBounds(); } //TODO: 是否合适?考虑中 const selectionService = DesignerHost.instance().getService(SelectionService); selectionService.setSelectedItems([item], SelectionTypes.Replace); item.setNeedDisplay(); DesignerHost.instance().historyService.snap(); } else { SnackBar.show(acceptResult.msg); DesignerHost.instance().historyService.stayLastPoint(); } } @action exchangeDetailBand(band: DetailBand, type: string) { const index = this._detailSection.getBands().findIndex(item => item === band); const isExchange = this.exchange(this._detailSection.getBands(), type, index); if (isExchange) { this.orderBand(); } } @action exchangeGroup(groupBand: GroupBand, type: string) { const group = groupBand.group; const groups = this._groups; const index = groups.indexOf(group); const isExchange = this.exchange(this._groups, type, index); if (isExchange) { this.orderBand(); } } private exchange(arr, type, index): boolean { const temp = arr[index]; if (type === "down") { if (arr[index + 1]) { arr[index] = arr[index + 1]; arr[index + 1] = temp; return true; } } else { if (arr[index - 1]) { arr[index] = arr[index - 1]; arr[index - 1] = temp; return true; } } return false; } // ------- JRPropertiesHolder hasProperties(): boolean { return null; } getPropertiesMap(): JRPropertiesMap { return null; } getParentProperties(): JRPropertiesHolder { return null; } visit(visitor: JRVisitor) { } // ------- JRReport implements block begin getUUID(): string { return this.objectId; } getName(): string { return this.name || "Sales Report"; } getColumnCount(): number { return this.columnCount; } getColumnWidth(): number { return this.columnWidth.pixels; } getColumnSpacing(): number { return this.columnSpacing.pixels; } getLeftMargin(): number { return this.leftMargin.pixels; } getRightMargin(): number { return this.rightMargin.pixels; } getTopMargin(): number { return this.topMargin.pixels; } getBottomMargin(): number { return this.bottomMargin.pixels; } getPageWidth(): number { return this.pageWidth.pixels; } getPageHeight(): number { return this.pageHeight.pixels; } getGroups(): JRGroup[] { return this.groups; } getTitle(): JRBand { return this.titleBand; } @action setTitle(band: JRBand) { if (this.getTitle()) { this.items.remove(this.getTitle()); } this._bands.TitleBand = band; this.items.push(band); this.orderBand(); } getPageHeader(): JRBand { return this.pageHeaderBand; } @action setPageHeader(band: JRBand): void { if (this.getPageHeader()) { this.items.remove(this.getPageHeader()); } this._bands.PageHeaderBand = band; this.items.push(band); this.orderBand(); } getColumnHeader(): JRBand { return this.columnHeaderBand; } @action setColumnHeader(band: JRBand): void { if (this.getColumnHeader()) { this.items.remove(this.getColumnHeader()); } this._bands.ColumnHeaderBand = band; this.items.push(band); this.orderBand(); } getDetailSection(): JRSection { return this._detailSection; } getColumnFooter(): JRBand { return this.columnFooterBand; } @action setColumnFooter(band: JRBand): void { if (this.getColumnFooter()) { this.items.remove(this.getColumnFooter()); } this._bands.ColumnFooterBand = band; this.items.push(band); this.orderBand(); } getPageFooter(): JRBand { return this.pageFooterBand; } @action setPageFooter(band: JRBand): void { if (this.getPageFooter()) { this.items.remove(this.getPageFooter()); } this._bands.PageFooterBand = band; this.items.push(band); this.orderBand(); } getLastPageFooter(): JRBand { return null; } @action setLastPageFooter(band: JRBand): void { if (this.getLastPageFooter()) { this.items.remove(this.getLastPageFooter()); } this.items.push(band); this.orderBand(); } getSummary(): JRBand { return null; } getNoData(): JRBand { return null; } getWhenResourceMissingTypeValue(): WhenResourceMissingTypeEnum { return null; } isIgnorePagination(): boolean { return null; } // ------- JRReport implements block end }