import {IDisposable, IServiceProvider, IComponent} from "../../interfaces/Interfaces"; import {ReportItemBase} from "../../items/ReportItemBase"; import {PointF, SizeF, RectF, Unit} from "../../drawing/Unit"; import {DesignerHost} from "../DesignerHost"; import {BehaviorService} from "./BehaviorService"; import {ReportItemBaseDesigner} from "../designer/ReportItemBaseDesigner"; import {SelectionService} from "./SelectionService"; import {SelectionTypes, SelectionRules, DragAction} from "./Enums"; import {action} from "mobx/lib/mobx"; import {SnappingEngine} from "./SnappingEngine"; import {QueryContinueDragEventArgs} from "./QueryContinueDragEventArgs"; import PaperHeight from "../../common/PaperHeight"; import {ReportItem} from "../../items/ReportItem"; import {ReportBand} from "../../items/band/ReportBand"; /** * 处理元素的 Drag 行为 * 要更好的理解这个行为的执行过程, 需要结合下面几个类来理解. * 1. ParentItemBehavior * 2. AdornerWindow * 3. BehaviorService * * 主要的过程是这样的: * 1. * ItemBodyBehavior.startDrag * -> BehaviorService.doDragDrop * -> AdornerWindow observer listener && AdornerWindow.doDragDrop * 2. * AdornerWindow.onDragXXX * -> BehaviorService.propagateHitTest * -> ParentItemBehavior.onDragXXX */ export class DropSourceBehavior implements IDisposable { private _host: DesignerHost; private _behaviorService: BehaviorService; private _serviceProvider: IServiceProvider; private _snappingEngine: SnappingEngine; private _dragItems: Array; private _startPos: PointF; private _dataObject: BehaviorDataObject; private _paperHeight: PaperHeight; constructor(serviceProvider: IServiceProvider, items: Array, startPos: PointF) { this._serviceProvider = serviceProvider; this._startPos = startPos.clone(); this._host = DesignerHost.instance(); this._behaviorService = serviceProvider.getService(BehaviorService) as BehaviorService; this._dataObject = new BehaviorDataObject(items, startPos, this._host); this._paperHeight = PaperHeight.instance(); this.initDragItems(); } get dataObject(): BehaviorDataObject { return this._dataObject; } get paperHeight(): PaperHeight { return this._paperHeight; } dispose() { this.clear(); this._dragItems = null; } /** * 根据 action 判断继续执行 Drag 还是结束 Drag. * @param args */ queryContinueDrag(args: QueryContinueDragEventArgs): void { if (args.action !== DragAction.Continue) { // 不继续执行, 清理状态结束 Drag this.clear(); } } /** * 反馈 Drag 执行过程 */ giveFeedback(): void { if (this._dataObject.target && this._dragItems && this._dragItems.length > 0) { let mousePos = this.dataObject.mousePosition.clone(); const parentDesigner = this._host.getDesigner(this.dataObject.target) as ReportItemBaseDesigner; const zoom = this._behaviorService.zoom; // 对坐标进行对齐处理. // this._dragItems[0] 是 Drag 中领头元素. mousePos = this.snap(mousePos, this._dragItems[0]); for (let dragItem of this._dragItems) { // 移动每一个元素到新的位置. dragItem.moveTo(mousePos, parentDesigner, zoom, this); } this.dataObject.mousePosition = mousePos; } this.dataObject.target = null; } /** * 对坐标进行对齐处理. * @param mousePos * @param leadingItem * @returns {PointF} */ private snap(mousePos: PointF, leadingItem: DragItem): PointF { let pt1 = mousePos.clone(); const offset = new PointF(mousePos.x - this._startPos.x, mousePos.y - this._startPos.y); const bounds = leadingItem.originalBounds; // 将区域偏移 bounds.offset(offset); // 获的元素的容器设计对象 const parentDesigner = leadingItem.designer.parentDesigner; if (parentDesigner) { const parentBounds = parentDesigner.getBounds(); // 如果超出容器边距则对齐到容器边距. if (bounds.left < parentBounds.left) { bounds.x = parentBounds.left; } if (bounds.top < parentBounds.top) { bounds.y = parentBounds.top; } if ((parentDesigner.selectionRules & SelectionRules.RightSizeable) == SelectionRules.None) { if (bounds.right > parentBounds.right) { bounds.x = parentBounds.right - bounds.width; } } } if (this._snappingEngine) { const snapOffset = this._snappingEngine.snap(bounds.location, bounds, null); pt1 = pt1.add(snapOffset); } return pt1; } /** * 初始化 Drag 对象 */ private initDragItems() { this.clear(); this._dragItems = null; // 记录要 drag 的报表元素 const componentsToDrag: Array = []; // 根据要 drag 报表元素创建的实际执行 drag 结果的对象. const dragItems: Array = []; for (let dragComponent of this.dataObject.dragComponents) { const designer = this._host.getDesigner(dragComponent.component) as ReportItemBaseDesigner; componentsToDrag.push(dragComponent.component); // 根据要 drag 报表元素创建的实际执行 drag 结果的对象 // 记录开始时报表元素的 drag 偏移位置和元素自己的位置和大小. dragItems.push(new DragItem(designer, dragComponent.offset, dragComponent.bounds)); } const selectionService = this._host.getService(SelectionService) as SelectionService; // 更新选择的元素 selectionService.setSelectedItems(componentsToDrag, SelectionTypes.Add); // 实例化对齐辅助线服务对象 this._snappingEngine = new SnappingEngine(this._host, componentsToDrag); this._dragItems = dragItems; } /** * 清理不需要再占用的资源 */ private clear() { if (this._dragItems) { for (let dragItem of this._dragItems) { dragItem.dispose(); } this._dragItems = null; } if (this._snappingEngine) { this._snappingEngine.dispose(); this._snappingEngine = null; } } } export class BehaviorDataObject { target: IComponent; mousePosition: PointF; private _dragComponents: Array; private _startPos: PointF; constructor(dragItems: Array, startPos: PointF, host: DesignerHost) { this._startPos = startPos; this._dragComponents = []; for (let item of dragItems) { const designer = host.getDesigner(item) as ReportItemBaseDesigner; if (designer) { const bounds = designer.getBounds(); const location = bounds.location; const offset = new SizeF(startPos.x - location.x, startPos.y - location.y); this._dragComponents.push(new DragComponent(item, offset, bounds)); } } } get dragComponents(): Array { return this._dragComponents; } get startPos(): PointF { return this._startPos; } } export class DragItem implements IDisposable { private _designer: ReportItemBaseDesigner; private _initBounds: RectF; private _offset: SizeF; private _initLeft: Unit; private _initTop: Unit; private _hasLeftProperty: boolean; private _hasTopProperty: boolean; /** * 返回报表元素开始 drag move 最初的位置和大小. * @returns {RectF} */ get originalBounds(): RectF { return this._initBounds.clone(); } get designer(): ReportItemBaseDesigner { return this._designer; } constructor(designer: ReportItemBaseDesigner, offset: SizeF, initBounds: RectF) { this._designer = designer; this._initBounds = initBounds; this._offset = offset; // 设置 designer 进入 dragging 状态中. designer.dragging = true; // 判断 designer 是否允许设置 left 和 top. this._hasLeftProperty = designer.item["left"]; this._hasTopProperty = designer.item["top"]; if (this._hasTopProperty) { this._initTop = designer.reportObject["top"]; } if (this._hasLeftProperty) { this._initLeft = designer.reportObject["left"]; } } /** * 移动元素到新的位置 * @param position * @param parentDesigner * @param zoom */ moveTo(position: PointF, parentDesigner: ReportItemBaseDesigner, zoom: number, self: DropSourceBehavior): void { this.setParent(parentDesigner); const item = this._designer.item as ReportItem; const bounds = parentDesigner.getBounds(); if (! self.paperHeight.canMove(item, bounds, position, this._offset)) { return } if (this._hasLeftProperty) { let x = position.x - this._offset.width - bounds.x; // 如果已经到达左边距,则不允许修改坐标 if (x < 0) { x = 0; } item["left"] = Unit.fromPixels(x / zoom, this._initLeft.type); } if (this._hasTopProperty) { const y = Math.max(0, position.y - this._offset.height - bounds.y); item["top"] = Unit.fromPixels(y / zoom, this._initTop.type); } } /** * 元素的 parent 发生变化,将 item 从原来的 parent 中移除,加入到新的 parent.items 中. * @param parentDesigner */ setParent(parentDesigner: ReportItemBaseDesigner): void { const item = this._designer.item; const currentParentDesigner = this._designer.parentDesigner; if (currentParentDesigner != parentDesigner) { if (currentParentDesigner) { const currentParent = currentParentDesigner.item; currentParent.items.remove(item); } if (parentDesigner) { const parent = parentDesigner.item; parent.items.push(item); } } } dispose() { this._designer.dragging = false; this._designer = null; } } export class DragComponent { private _component: IComponent; private _offset: SizeF; private _bounds: RectF; constructor(component: IComponent, offset: SizeF, bounds: RectF) { this._component = component; this._offset = offset; this._bounds = bounds; } get component(): IComponent { return this._component; } get offset(): SizeF { return this._offset; } get bounds(): RectF { return this._bounds; } }