import * as React from "react"; // import {Rect} from "react-konva"; import {observable,observe} from "mobx/lib/mobx"; import {ReportItemBase} from "../../items/ReportItemBase"; import {ISupportInitialize, IDisposable} from "../../interfaces/Interfaces"; import {Glyph} from "../behavior/Glyph"; import {Report} from "../../items/Report"; import {ItemFrameGlyph, SelectionBorderGlyph} from "../behavior/SelectionBorderGlyph"; import {RectF, RectUnit, Unit, SizeF, SizeUnit} from "../../drawing/Unit"; import { Edges, ItemSelectionType, SelectionRules, SelectionBorderGlyphType, GrabHandleGlyphType, BorderStatus } from "../behavior/Enums"; import {ItemBodyGlyph} from "../behavior/ItemBodyGlyph"; import {Cursor} from "../../drawing/Cursor"; import {Behavior} from "../behavior/Behavior"; import {ItemBodyBehavior} from "../behavior/ItemBodyBehavior"; import {DesignerHost} from "../DesignerHost"; import {BehaviorService} from "../behavior/BehaviorService"; import {ResizeBehavior} from "../behavior/ResizeBehavior"; import {GrabNormalGlyph, GrabLockGlyph} from "../behavior/GrabHandleGlyph"; import {SnapLine, SnapLineType, SnapLinePriority} from "../behavior/SnapLine"; import {DimensionsGlyph} from "../behavior/DimensionsGlyph"; import {OverLapService} from "../behavior/OverLapService"; import {ReportRootDesigner} from "./ReportRootDesigner"; import {RectObjectPool} from "../../drawing/KonvaObjectPools"; import {BoxItem} from "../../items/BoxItem"; const Konva = require("konva"); const dumpText = new Konva.Text(); export abstract class ReportItemBaseDesigner implements IDisposable, ISupportInitialize { protected _behaviorService: BehaviorService; static MinimumSize = new SizeUnit(Unit.pixel(1), Unit.pixel(0)); private _item: any; private _itemGlyph: Glyph; private _itemFramesGlyph: ItemFrameGlyph; private _host: DesignerHost; private _defaultBehavior: Behavior; private _dragging: boolean; private _resizing: SelectionRules = SelectionRules.None; private _resizeBehavior: ResizeBehavior; @observable protected _selected: Boolean = false; @observable private _locked: boolean = false; protected static calculateTextHeight(text: string, fontSize: number, maxWidth: number): number { // const props = { // x: 0, // y: 0, // text: text, // fontSize: fontSize, // width: maxWidth, // } dumpText.text(text); dumpText.width(maxWidth); dumpText.fontSize(fontSize); // const dumpText = new Konva.Text(props); return dumpText.getHeight(); } dispose() { if (this._itemGlyph) { this._itemGlyph.dispose(); } if (this._itemFramesGlyph) { this._itemFramesGlyph.dispose(); } } get selected(): Boolean { return this._selected; } set selected(value: Boolean) { this._selected = value; } get locked(): boolean { return this._locked; } set locked(value: boolean) { this._locked = value; } get item(): ReportItemBase { return this._item; } get reportDesigner(): ReportRootDesigner { return this.parentDesigner.reportDesigner; } get reportObject(): ReportItemBase { return this.item as ReportItemBase; } get behaviorService(): BehaviorService { if (!this._behaviorService) { this._behaviorService = DesignerHost.instance().getService(BehaviorService); } return this._behaviorService; } get snapLines(): Array { return this.getSnapLines(); } protected getSnapLines(): Array { const list: Array = []; if (this.isParentDesigner() && !(this instanceof Report)) { this.addPaddingSnapLines(list); } return list; } get bounds(): RectUnit { throw new Error(); }; getBounds(absolute = true): RectF { return this.calculateBounds(absolute, this.behaviorService.zoom); } getClientRectangle(absolute = false): RectF { const bounds = this.getBounds(absolute); const zoom = this.behaviorService.zoom; const paddingLeft = 0; const paddingTop = 0; const paddingRight = 0; const paddingBottom = 0; const rc = new RectF( paddingLeft , paddingTop , Math.max(0, bounds.width - paddingLeft - paddingRight) , Math.max(0, bounds.height - paddingTop - paddingBottom)); if (absolute) { rc.offset(bounds.location); } return rc; } get resizing(): SelectionRules { return this._resizing; } set resizing(value: SelectionRules) { this._resizing = value; } get dragging(): boolean { return this._dragging; } set dragging(value: boolean) { this._dragging = value; this.reportObject.items.forEach(child => { const designer = this.host.getDesigner(child) as ReportItemBaseDesigner; if (designer) { designer.dragging = value; } }) } /** * 返回允许 Drop 报表元素 * @returns {boolean} */ get allowDrop(): boolean { // 只有容器元素并且自己不处于 drag 状态中可以接受 Drop return this.isParentDesigner() && !this.dragging; } isParentDesigner(): boolean { return false; } isTableCell(): boolean { return false; } /** * 计算允许调整的最小尺寸 * @returns {SizeF} */ calculateAllowedShrinkSize(): SizeF { const zoom = this.behaviorService.zoom; const minSize = ReportItemBaseDesigner.MinimumSize; let minWidth = minSize.width.fPixels * zoom; let minHeight = minSize.height.fPixels * zoom; for (let childItem of this.item.items) { const designer = this.host.getDesigner(childItem) as ReportItemBaseDesigner; if (designer) { const bounds = designer.getBounds(false); const currentMinSize = new SizeF(bounds.right, bounds.bottom); minWidth = Math.max(minWidth, currentMinSize.width); minHeight = Math.max(minHeight, currentMinSize.height); } } return new SizeF(minWidth, minHeight); } calculateMinSize(): SizeF { const zoom = this.behaviorService.zoom; const minSize = ReportItemBaseDesigner.MinimumSize; let minWidth = minSize.width.fPixels * zoom; let minHeight = minSize.height.fPixels * zoom; for (let childItem of this.item.items) { const designer = this.host.getDesigner(childItem) as ReportItemBaseDesigner; if (designer) { const bounds = designer.getBounds(false); minWidth = Math.max(minWidth, bounds.right); minHeight = Math.max(minHeight, bounds.bottom); } } return new SizeF(minWidth, minHeight); } /** * 自动 Grow 容器的大小使得容器可以容纳显示所有的元素. * @returns {boolean} */ updateMinBounds(): boolean { let changed = false; let bounds = this.getBounds(); const minSize = this.calculateMinSize(); const zoom = this.behaviorService.zoom; if (this.item["width"]) { if (minSize.width > bounds.width) { const width = this.item["width"] as Unit; this.item["width"] = Unit.pixel(minSize.width / zoom).changeType(width.type); changed = true; } } if (this.item["height"]) { if (minSize.height > bounds.height) { const height = this.item["height"] as Unit; this.item["height"] = Unit.pixel(Math.floor(minSize.height / zoom)).changeType(height.type); changed = true; } } if (this.parentDesigner) { this.parentDesigner.updateMinBounds(); } return changed; } private addPaddingSnapLines(snapLines: Array): void { const bounds = this.getBounds(); const zoom = this.behaviorService.zoom; const padding = { Left: Unit.Zero, Top: Unit.Zero, Right: Unit.Zero, Bottom: Unit.Zero, } snapLines.push(new SnapLine(SnapLineType.Vertical, bounds.left + zoom * padding.Left.fPixels, SnapLinePriority.Always, "Padding.Left")); snapLines.push(new SnapLine(SnapLineType.Vertical, bounds.right - zoom * padding.Right.fPixels, SnapLinePriority.Always, "Padding.Right")); snapLines.push(new SnapLine(SnapLineType.Horizontal, bounds.top + zoom * padding.Top.fPixels, SnapLinePriority.Always, "Padding.Top")); snapLines.push(new SnapLine(SnapLineType.Horizontal, bounds.bottom - zoom * padding.Bottom.fPixels, SnapLinePriority.Always, "Padding.Bottom")); } private calculateBounds(absolute: boolean, zoom = 1): RectF { const bounds = this.bounds; const rect = new RectF( Math.floor(bounds.x.pixels * zoom), Math.floor(bounds.y.pixels * zoom), Math.floor(bounds.width.pixels * zoom), Math.floor(bounds.height.pixels * zoom) ); if (absolute) { const parent = this.item.parent; if (parent) { const parentDesigner = this.host.getDesigner(parent) as ReportItemBaseDesigner; if (parentDesigner) { rect.offset(parentDesigner.calculateBounds(true, zoom).location); } } } return rect; } initialize(item: ReportItemBase): void { this._item = item; this.observerChildrenChanged(); } protected observerChildrenChanged():void { if (this.isTableCell()) { return; } // observe(this._item.items, () => { // if (this._item.report && (! this._item.report.isUnbound) && DesignerHost.instance().historyService) { // DesignerHost.instance().historyService.snap(); // } // }) } get host(): DesignerHost { if (!this._host) { this._host = DesignerHost.instance(); } return this._host; } get selectionRules(): SelectionRules { return SelectionRules.None; } get parentDesigner(): ReportItemBaseDesigner { if (this.item && this.item.parent) { return this.host.getDesigner(this.item.parent) as ReportItemBaseDesigner; } return null; } getItemGlyph(selType: ItemSelectionType): Glyph { if (this.isTableCell()) { return null; } if (!this._itemGlyph) { this._itemGlyph = this.getDefaultItemBodyGlyph(); } return this._itemGlyph; } protected getDefaultItemBodyGlyph():ItemBodyGlyph { return new ItemBodyGlyph(this, Cursor.Default, this.defaultBehavior); } get defaultBehavior(): Behavior { if (!this._defaultBehavior) { this._defaultBehavior = this.createDefaultBehavior(); } return this._defaultBehavior; } canResize(resizeRule: SelectionRules): boolean { if ((this.resizing & resizeRule) != 0) { const item = this.item; switch (resizeRule) { case SelectionRules.LeftSizeable: return (null != item["left"] && null != item["width"]); case SelectionRules.RightSizeable: return (null != item["width"]); case SelectionRules.TopSizeable: return (null != item["top"] && null != item["height"]); case SelectionRules.BottomSizeable: return (null != item["height"]); } } return false; } createDefaultBehavior(): Behavior { return new ItemBodyBehavior(this, this.host); } getItemDimensionGlyphs(selType: ItemSelectionType): Array { if (this.isTableCell()) { return null; } if (this.resizing != SelectionRules.None) { let edges = Edges.None; if (this.canResize(SelectionRules.LeftSizeable)) { edges |= Edges.Left; } else if (this.canResize(SelectionRules.RightSizeable)) { edges |= Edges.Right; } if (this.canResize(SelectionRules.TopSizeable)) { edges |= Edges.Top; } else if (this.canResize(SelectionRules.BottomSizeable)) { edges |= Edges.Bottom; } const unitBounds = this.bounds; const bounds = this.getBounds(); const g = new DimensionsGlyph(bounds , edges , unitBounds.width , unitBounds.height); return [g]; } return null; } getItemFrameGlyphs(selType: ItemSelectionType, isOverlapped): Array { if (this.isTableCell()) { return null; } if (this.item.constructor !== Report) { let borderEdges: Edges; let borderSize: number; let borderStatus: BorderStatus = BorderStatus.UnLocked; if (selType === ItemSelectionType.NotSelected) { borderSize = 1; borderEdges = Edges.None; if (this.item instanceof BoxItem) { const boxItem = this.item as BoxItem; if (! boxItem.border.top.borderVisible) { borderEdges |= Edges.Top; } if (! boxItem.border.left.borderVisible) { borderEdges |= Edges.Left; } if (! boxItem.border.bottom.borderVisible) { borderEdges |= Edges.Bottom; } if (! boxItem.border.right.borderVisible) { borderEdges |= Edges.Right; } } else { borderEdges = Edges.All; } } else if (this._locked) { borderEdges = Edges.All; borderStatus = BorderStatus.Locked; } else { borderEdges = Edges.All; borderSize = 2; } if (isOverlapped) { borderEdges = Edges.All; borderStatus = BorderStatus.OverLapped; } if (!this._itemFramesGlyph) { this._itemFramesGlyph = new ItemFrameGlyph(this.item, this.getBounds(), borderEdges, borderSize, selType, borderStatus); } this._itemFramesGlyph.setItemId(this.item.objectId); this._itemFramesGlyph.bounds = this.getBounds(); this._itemFramesGlyph.borderSize = borderSize; this._itemFramesGlyph.selType = selType; this._itemFramesGlyph.edges = borderEdges; this._itemFramesGlyph.borderStatus = borderStatus; return [ this._itemFramesGlyph ] } } getSelectionGlyphs(selType: ItemSelectionType|any): Array { if (this.isTableCell()) { return null; } if (selType === ItemSelectionType.NotSelected) { return null; } const bounds = this.getBounds(); const glyphs: Array = []; if (this.item.constructor !== Report) { let borderRules: SelectionRules = SelectionRules.None; if (selType !== ItemSelectionType.NotSelected) { borderRules = this.selectionRules; } let borderStatus = BorderStatus.UnLocked; if (this._locked) { borderStatus = BorderStatus.Locked; } glyphs.push(new SelectionBorderGlyph(this.item, bounds, borderRules, SelectionBorderGlyphType.Top, borderStatus, this.resizeBehavior)) glyphs.push(new SelectionBorderGlyph(this.item, bounds, borderRules, SelectionBorderGlyphType.Left, borderStatus, this.resizeBehavior)) glyphs.push(new SelectionBorderGlyph(this.item, bounds, borderRules, SelectionBorderGlyphType.Bottom, borderStatus, this.resizeBehavior)) glyphs.push(new SelectionBorderGlyph(this.item, bounds, borderRules, SelectionBorderGlyphType.Right, borderStatus, this.resizeBehavior)) let GrabObject = null; if (this._locked) GrabObject = GrabLockGlyph; else GrabObject = GrabNormalGlyph; if (selType !== ItemSelectionType.NotSelected) { const primary = (selType === ItemSelectionType.PrimarySelection); if ((this.selectionRules & SelectionRules.RightSizeable) != SelectionRules.None) { glyphs.push(new GrabObject(this.item, bounds, GrabHandleGlyphType.Right, this.resizeBehavior, primary)); if ((this.selectionRules & SelectionRules.TopSizeable) != SelectionRules.None) { glyphs.push(new GrabObject(this.item, bounds, GrabHandleGlyphType.TopRight, this.resizeBehavior, primary)); } if ((this.selectionRules & SelectionRules.BottomSizeable) != SelectionRules.None) { glyphs.push(new GrabObject(this.item, bounds, GrabHandleGlyphType.BottomRight, this.resizeBehavior, primary)); } } if ((this.selectionRules & SelectionRules.LeftSizeable) != SelectionRules.None) { glyphs.push(new GrabObject(this.item, bounds, GrabHandleGlyphType.Left, this.resizeBehavior, primary)); if ((this.selectionRules & SelectionRules.TopSizeable) != SelectionRules.None) { glyphs.push(new GrabObject(this.item, bounds, GrabHandleGlyphType.TopLeft, this.resizeBehavior, primary)); } if ((this.selectionRules & SelectionRules.BottomSizeable) != SelectionRules.None) { glyphs.push(new GrabObject(this.item, bounds, GrabHandleGlyphType.BottomLeft, this.resizeBehavior, primary)); } } if ((this.selectionRules & SelectionRules.TopSizeable) != SelectionRules.None) { glyphs.push(new GrabObject(this.item, bounds, GrabHandleGlyphType.Top, this.resizeBehavior, primary)); } if ((this.selectionRules & SelectionRules.BottomSizeable) != SelectionRules.None) { glyphs.push(new GrabObject(this.item, bounds, GrabHandleGlyphType.Bottom, this.resizeBehavior, primary)); } } } return glyphs; } checkOverlap(item: ReportItemBase): void { if (this.isTableCell()) { return; } let overlapService = this.host.getService(OverLapService) as OverLapService; if (! overlapService) { overlapService = new OverLapService(); this.host.addService(OverLapService, overlapService); } overlapService.checkOverlap(item); } get resizeBehavior(): Behavior { if (!this._resizeBehavior) { this._resizeBehavior = new ResizeBehavior(this, this.host); } return this._resizeBehavior; } static drawBackground(bounds: RectF, color): any { let props = { x: bounds.x, y: bounds.y, width: bounds.width, height: bounds.height, fill: color, listening: false, }; // return new Konva.Rect(props); return RectObjectPool.get(props); } renderBackground(bounds: RectF): any { } render(bounds: RectF): any { } }