import * as React from "react"; // import {Group} from 'react-konva'; // import {observer} from "mobx-react/index"; import {Glyph} from "./Glyph"; import {RectF, PointF} from "../../drawing/Unit"; import {ReportItemBaseDesigner} from "../designer/ReportItemBaseDesigner"; import {ReportItemBase} from "../../items/ReportItemBase"; import {Cursor} from "../../drawing/Cursor"; import {Behavior} from "./Behavior"; import {SelectionRules} from "./Enums"; import {PropertyChangeEventArgs} from "../../items/PropertyChangeEventArgs"; import {CommonProperty} from "../../items/properties/define"; import {autobind} from "core-decorators"; import {GroupObjectPool} from "../../drawing/KonvaObjectPools"; const Konva = require("konva"); export class ItemBodyGlyph extends Glyph { private _designer: ReportItemBaseDesigner; private _cursor:Cursor; private _layer:any; private _previousX:number; private _previousY:number; private _needDisplay = false; constructor(designer:ReportItemBaseDesigner, cursor:Cursor, behavior:Behavior) { super(behavior); this._cursor = cursor; this._designer = designer; this.item.on(ReportItemBase.PropertyChangeEventName, this.onPropertyChanged); } @autobind protected onPropertyChanged(arg: PropertyChangeEventArgs) { if (arg.property !== CommonProperty.x && arg.property !== CommonProperty.y) { this._needDisplay = true; } } dispose() { if (this._layer) { // this._layer.remove(); this._layer = null; } super.dispose(); } get designer():ReportItemBaseDesigner { return this._designer; } get item():ReportItemBase { return this._designer.item; } get bounds():RectF { return this._designer.getBounds(); } get cursor():Cursor { return this._cursor; } set cursor(value:Cursor) { this._cursor = value; } get allowMove():boolean { return (this.designer.selectionRules & SelectionRules.Moveable) !== SelectionRules.None && !this.designer.locked; } get allowDrop():boolean { return this.designer.allowDrop; } hitTest(pt:PointF):Cursor { if (! this.item) { return null; } if (this.bounds.contains(pt)) { return this._cursor; } return null; } private renderLayer(parentNode) { const bounds = this.bounds; this._previousX = bounds.x; this._previousY = bounds.y; const rootProps = { x: bounds.x, y: bounds.y, width: bounds.width, height: bounds.height, clipX: 0, clipY: 0, clipWidth: bounds.width, clipHeight: bounds.height, listening: false, }; const clipBounds = bounds.clone(); clipBounds.x = 0; clipBounds.y = 0; if (this.designer.item) { const groupNode = GroupObjectPool.get(rootProps); const backgroundNode = this.designer.renderBackground(clipBounds); const bodyNode = this.designer.render(clipBounds); parentNode.add(groupNode); if (backgroundNode) { groupNode.add(backgroundNode); } if (bodyNode) { groupNode.add(bodyNode); } } this._needDisplay = false; } private offsetLayer() { const bounds = this.bounds; const offsetX = bounds.x - this._previousX; const offsetY = bounds.y - this._previousY; if (offsetX !== 0 || offsetY !== 0) { this._layer.getCanvas()._canvas.style.left = offsetX + 'px'; this._layer.getCanvas()._canvas.style.top = offsetY + 'px'; } } render(parentNode):void { this.renderLayer(parentNode); // if (! this._layer) { // const id = `itembody:${this.item.objectId}`; // const layers = parentNode.getStage().find('#' + id); // // if (layers.length === 0) { // this._layer = new Konva.Layer(); // this._layer.listening(false); // this._layer.id(id); // parentNode.getStage().add(this._layer); // } // else { // this._layer = layers[0]; // } // // this._needDisplay = true; // } // // if (this._needDisplay) { // console.log('render itembody !!!'); // // this._layer.destroyChildren(); // this._layer.removeChildren(); // this._layer.getCanvas()._canvas.style.left = '0px'; // this._layer.getCanvas()._canvas.style.top = '0px'; // this.renderLayer(this._layer); // this._layer.batchDraw(); // } // else { // this.offsetLayer(); // } } }