import {EventEmitter2} from "eventemitter2"; import {DesignerHost} from '../design/DesignerHost'; import {Report} from "./Report"; import { observable, IObservableArray, observe, IArraySplice, extendObservable, action } from "mobx/lib/mobx"; import {Property} from "./properties/Property"; import {UnitMargins} from "../drawing/Unit"; import {IComponent, IDisposable} from "../interfaces/Interfaces"; import {PropertyChangeEventArgs} from "./PropertyChangeEventArgs"; import {JRPropertiesHolder} from "../jasper/JRPropertiesHolder"; import {JRChild} from "../jasper/JRChild"; import {JRPropertiesMap} from "../jasper/JRPropertiesMap"; import {JRVisitor} from "../jasper/JRVisitor"; import {LAYOUT_KEY} from "./constants"; import {Layout} from "./layout/Layout"; import {Style} from "../drawing/Style"; const uuid = require("uuid"); export abstract class ReportItemBase implements IComponent, IDisposable, JRChild, JRPropertiesHolder { private _style: Style; private _objectId = uuid.v1(); private _events = new EventEmitter2(); private _propertyValues;// = observable({}); private _items: IObservableArray; private _parent: ReportItemBase; private _report: Report; private _margins: UnitMargins; private _itemsDisposer: any; private _itemsInterceptDisposer: any; private _disposed = false; static PropertyChangeEventName = "PropertyChangeEvent"; constructor() { this.initProperties(); this.initEvents(); this._propertyValues = observable({}); } get className(): string { return this.constructor.name; } getDesignerLayout(): Layout { return this.constructor[LAYOUT_KEY]; } /** * 返回显示给用户看得懂的名字 */ abstract displayName():string; dispose() { if (this._disposed) { return; } this._disposed = true; if (this["__$designer"]) { (this["__$designer"] as IDisposable).dispose(); this["__$designer"] = null; } this._itemsDisposer(); this._itemsInterceptDisposer(); this._events.removeAllListeners(); this._events = null; this._report = null; this._parent = null; this._propertyValues = null; if (this._items) { for (let item of this._items) { item.dispose(); } // this._items.clear(); this._items = null; } // if (this._parent && this._parent.items) { // this._parent.items.remove(this); // } } initEvents(): void{ this.on(ReportItemBase.PropertyChangeEventName, this.setDesignerOverlap.bind(this)); } on(event: string, listener: EventEmitter2.Listener):void { this._events.on(event, listener); } off(event: string, listener: EventEmitter2.Listener):void { this._events.off(event, listener); } get objectId():string { return this._objectId; } set objectId(value:string) { this._objectId = value; } getPropertyValue(property:Property): any { if (this._disposed) { return property.defaultValue; } if (!this._propertyValues.hasOwnProperty(property.name)) { extendObservable(this._propertyValues, {[property.name]: property.defaultValue}); } return this._propertyValues[property.name]; } /** * 得到所有 items ,递归包含它们的 items */ getAllItems(allItems:Array = []):Array { if (this._items) { for (let item of this._items) { allItems.push(item); item.getAllItems(allItems); } } return allItems; } /** * * @param property * @param newValue * @param silent: true 表示沉默,不发生事件通知。 */ @action setPropertyValue(property:Property, newValue:any, silent = false):void { if (this._disposed) { return; } // 如果是新添加的属性, 先进行observe if(!this._propertyValues.hasOwnProperty(property.name)){ extendObservable(this._propertyValues, {[property.name]: newValue}); this.notifyPropertyChanged(new PropertyChangeEventArgs(property, undefined, newValue), silent); this.setNeedDisplay(); } else{ const oldValue = this._propertyValues[property.name]; if (newValue !== oldValue) { this._propertyValues[property.name] = newValue; this.notifyPropertyChanged(new PropertyChangeEventArgs(property, oldValue, newValue), silent); this.setNeedDisplay(); } } } protected notifyPropertyChanged(args:PropertyChangeEventArgs, silent = false):void { if (! silent) { this.onPropertyChanged(args); // MODIFIED GP silent时不触发事件 if (args) { this._events.emit(ReportItemBase.PropertyChangeEventName, args); } } } protected onPropertyChanged(args:PropertyChangeEventArgs):void { } protected emitEvent(name:string, args?:any):void { this._events.emit(name, args); } setDesignerOverlap(args): void{ const locationProperties = ['x','y','width','height']; if (locationProperties.indexOf(args.property.name) !== -1) { this.checkItemsOverlap(this); } } private checkItemsOverlap(item: ReportItemBase): void { const designer = DesignerHost.instance().getDesigner(item); if ( designer && !designer.isParentDesigner()) { designer.checkOverlap(item); } } /** * 设置 View 需要进行重新绘制. */ setNeedDisplay() { if (this.parent) { this.parent.setNeedDisplay(); } } get margins(): UnitMargins { if (! this._margins) { this._margins = new UnitMargins(); } return this._margins; } get items():IObservableArray { return this._items; } get parent():ReportItemBase { return this._parent; } set parent(value:ReportItemBase) { this._setParent(value); } get style():Style { return this._style; } set style(styles){ let style = this._style; for(var key in styles){ style[key] = styles[key]; } } @action private _setParent(value:ReportItemBase) { if (value !== this._parent) { if (this._parent) { this._parent.items.remove(this); } if (value) { value.items.push(this); } } } get report():Report { return this._report; } setReport(report: Report):void { this._report = report; if (this._items) { this._items.forEach(item => item.setReport(report)); } } canAdd(item:ReportItemBase):boolean { if (!this.canParent(item)){ return false; } let reportItem; reportItem = this; while (reportItem.parent) { if (!reportItem.canBeAncestorOf(item)) { return false; } reportItem = reportItem.parent; } return true; } canParent(item:ReportItemBase):boolean { return this.canContain(item); } canContain(item: ReportItemBase):boolean { return true; } canBeAncestorOf(item:ReportItemBase):boolean { if (!this.canContain(item)){ return false; } for (let itemChild of item.items){ if (!this.canBeAncestorOf(itemChild)){ return false; } } return true; } protected initProperties():void { this._style = new Style(this); this._items = observable([]); this._itemsInterceptDisposer = this._items.intercept((change) => { if (change.type === "splice") { if (change['added']) { change['added'].forEach((item:ReportItemBase) => { item._parent = this; item.setReport(this.report); }); } } return change; }); this._itemsDisposer = observe(this._items, (change:IArraySplice):void => { if (change.type === "splice") { change.removed && change.added.forEach((item:ReportItemBase) => { this.onItemAdded(item); }); change.removed.forEach((item:ReportItemBase) => { item._parent = null; item.setReport(null); this.onItemRemoved(item); }); this.items.length > 0 && this.checkItemsOverlap(this.items[0]); } }); } protected onItemAdded(item: ReportItemBase):void { } protected onItemRemoved(item: ReportItemBase):void { } @action restore(data){ const designer = DesignerHost.instance().getDesigner(this); designer.selected = data.selected; designer.locked = data.locked; } snap(){ const baseInfo = this.snapBaseInfo(); return baseInfo; } snapBaseInfo(){ const designer = DesignerHost.instance().getDesigner(this); return { id: this._objectId, selected: designer.selected, locked: designer.locked, properties:{ } } } getUUID(): string { return this.objectId; } // ----------------- JRPropertiesHolder hasProperties():boolean { return null; } getPropertiesMap():JRPropertiesMap { return null; } getParentProperties():JRPropertiesHolder { return null; } abstract visit(visitor:JRVisitor); }