import {ISupportInitialize, IServiceProvider, IContainer} from "../interfaces/Interfaces"; import {Unit, UnitType} from "../drawing/Unit"; import {ReportRootDesigner} from "./designer/ReportRootDesigner"; import {Report} from "../items/Report"; import ReportConfig from "./ReportConfig"; import {SelectionService} from "./behavior/SelectionService"; import HistoryService from "./HistoryService"; import UIModel from "./UIModel"; import {ReportBandBase} from "../items/band/ReportBandBase"; import {PageBandDesigner} from "./designer/PageBandDesigner"; import {TitleBand} from "../items/band/TitleBand"; import {PageHeaderBand, PageFooterBand} from "../items/band/PageBand"; import {DetailBand} from "../items/band/DetailBand"; import {ColumnHeaderBand, ColumnFooterBand} from "../items/band/ColumnBand"; import {GroupHeaderBand, GroupFooterBand} from "../items/band/GroupBand"; import {ReportItem} from "../items/ReportItem"; import {StaticTextItem} from "../items/StaticTextItem"; import TextFieldItem from "../items/TextFieldItem"; import {ImageItem} from "../items/ImageItem"; import CheckboxItem from "../items/CheckboxItem"; import EllipseItem from "../items/EllipseItem"; import RectangleItem from "../items/RectangleItem"; import {DataTableItem} from "../items/data-table/DataTableItem"; import {ReportBandDesigner} from "./designer/ReportBandDesigner"; import {TextItemDesigner} from "./designer/TextItemDesigner"; import CheckboxDesigner from "../design/designer/CheckboxDesigner"; import {PictureBoxDesigner} from "./designer/PictureBoxDesigner"; import LineShapeDesigner from "../design/designer/LineShapeDesigner"; import RectangleDesigner from "../design/designer/RectangleDesigner"; import EllipseDesigner from "../design/designer/EllipseDesigner"; import DataTableDesigner from "../design/designer/DataTableDesigner"; import DataTableCellDesigner from "./designer/DataTableCellDesigner"; import {action} from "mobx"; import {JSONSchemaLoader} from "../schema/loader/JSONSchemaLoader"; import {LineShape} from "../items/LineShape"; const color = require('onecolor'); const designerMap = { 'TitleBand': ReportBandDesigner, 'PageHeaderBand': PageBandDesigner, 'PageFooterBand': PageBandDesigner, 'DetailBand': ReportBandDesigner, 'GroupHeaderBand': ReportBandDesigner, 'GroupFooterBand': ReportBandDesigner, 'ColumnHeaderBand': ReportBandDesigner, 'ColumnFooterBand': ReportBandDesigner, 'StaticTextItem': TextItemDesigner, 'TextFieldItem': TextItemDesigner, 'ImageItem': PictureBoxDesigner, 'CheckboxItem': CheckboxDesigner, 'LineShape': LineShapeDesigner, 'EllipseItem': EllipseDesigner, 'RectangleItem': RectangleDesigner, 'StaticTableItem': DataTableDesigner, 'DataTableItem': DataTableDesigner, 'DataTableCellItem': DataTableCellDesigner }; const bandMap = { 'TitleBand': TitleBand, 'PageHeaderBand': PageHeaderBand, 'PageFooterBand': PageFooterBand, 'DetailBand': DetailBand, 'GroupHeaderBand': GroupHeaderBand, 'GroupFooterBand': GroupFooterBand, 'ColumnHeaderBand': ColumnHeaderBand, 'ColumnFooterBand': ColumnFooterBand }; const itemMap = { 'StaticText': StaticTextItem, 'TextField': TextFieldItem, 'Image': ImageItem, 'Checkbox': CheckboxItem, 'Line': LineShape, 'Ellipse': EllipseItem, 'Rectangle': RectangleItem, 'DataTable': DataTableItem }; let singletonInstance: DesignerHost; export class DesignerHost implements IServiceProvider { private _services = {}; private _currentReport: ReportRootDesigner; private _uiModel: UIModel; private _historyService : HistoryService; /** * 报表的配置 */ private _config: any; private _dataSource: Function; private _uploadImage: Function; private _onOpen: Function; private _onSave: Function; private _onChange: Function; constructor() { } static instance(): DesignerHost { if (! singletonInstance) { singletonInstance = new DesignerHost(); } return singletonInstance; } get container(): IContainer { return this._currentReport.report; } get historyService():HistoryService { return this._historyService; } get uiModel():UIModel { return this._uiModel; } get config() { return this._config; } get dataSource() { return this._dataSource; } get uploadImage() { return this._uploadImage; } get onChange() { return this._onChange; } get onOpen() { return this._onOpen; } get onSave() { return this._onSave; } @action setDesignReport(report:Report):void { if (report !== this.designReport.report) { const selectionService:SelectionService = this.getService(SelectionService) as SelectionService; const selectedItems = selectionService.getSelectedItems(); const allItems = report.getAllItems(); const newSelectedItems = []; selectedItems.forEach(selectedItem => { for (let item of allItems) { if (item.objectId === selectedItem.objectId) { newSelectedItems.push(item); return; } } }); this.designReport.initialize(report); selectionService.replaceSelectedItems(newSelectedItems); } } newReport(data:any): ReportRootDesigner { const {config, model, dataSource, uploadImage, onOpen, onSave, onChange} = data; this._currentReport = new ReportRootDesigner(); //报表配置 this._config = new ReportConfig(config); //数据源 this._dataSource = dataSource; //删除图片接口 this._uploadImage = uploadImage; //外部接受报表事件的方法 this._onChange = onChange; this._onOpen = onOpen; this._onSave = onSave; //创建纸张大小 // const paperSettings:PaperSettings = new PaperSettings(); // const report = new Report(paperSettings); const report = JSONSchemaLoader.load(model); report.isUnbound = false; // 或者直接设置已定义的纸张 // report.paper = new Paper(A4); this._currentReport.initialize(report); // //初始化报表 // this._initReport(report, model); // 创建设计器的历史管理器, 负责 undo 和 redo 操作 this._historyService = new HistoryService(); this.addService(HistoryService, this._historyService); this._historyService.snap(); // 创建设计器界面相关 UI Model this._uiModel = new UIModel(); return this._currentReport; } /** * 根据初始化的数据,创建整个报表设计器item数据模型 * @private * @param report * @param model */ _initReport(report:Report, model:any) { //获得配置里设置的不使用的band const blockBands = this._config.blockBands; for (let bandName in model){ if (blockBands[bandName] !== true && bandName !== 'groups') { this._initBand(report, bandName, model[bandName]); } else if (blockBands.Group !== true) { this._initGroup(report, model[bandName]); } } } /** * 初始化band * @param report * @param bandName * @param bandData * @private */ _initBand(report:Report, bandName:string, bandData:any){ //创建band const band = report.addBand(bandName); //是否设置了band的高度 if (bandData.height) { (band as ReportBandBase).height = Unit.cm(bandData.height); } //是否有items if (bandData.items && bandData.items.length > 0) { bandData.items.forEach(item => { if (itemMap[item.type]) { band.items.push(DesignerHost.createItem(new itemMap[item.type](item.data), item.layout)); } else{ throw new Error(item.type + '对应的报表元素不存在'); } }); } } /** * 初始化groups * @param report * @param groupArray * @private */ _initGroup(report:Report, groupArray:any){ let group; if (groupArray && groupArray.length > 0){ group = report.addGroup(); } } get designReport(): ReportRootDesigner { return this._currentReport; } addService(type: any, service: any) { this._services[type.toString()] = service; } getService(type: any): any { return this._services[type.toString()]; } getDesigner(item: any): any { let designer: ISupportInitialize = null; const itemName = item.constructor.name; if (item["__$designer"]) { return item["__$designer"]; } if (! item.report) { return null; } if (item.report.isUnbound) { return null; } if (item instanceof Report) { designer = this.designReport; return designer; } if (designerMap[itemName]){ designer = new designerMap[itemName](); item["__$designer"] = designer; designer.initialize(item); } return designer; } static createItem(item: ReportItem, properties={}){ const unitProperties = ["width", "height", "left", "top"]; for(var key in properties){ let value: any = properties[key]; if(typeof value === "number" && unitProperties.indexOf(key) !== -1){ value = new Unit(value); } item[key] = value; } return item; } }