import {IObservableArray, action, observable} from "mobx/lib/mobx"; import {DesignerHost} from "../DesignerHost"; import {ReportItemBase} from "../../items/ReportItemBase"; import {ReportItemBaseDesigner} from "../designer/ReportItemBaseDesigner"; import {PointF} from "../../drawing/Unit"; export class OverLapService { private _host: DesignerHost; private _overlapItems: IObservableArray; //有交叉的item static AddItem: string = 'ADD'; static RemoveItem:string = 'REMOVE'; constructor() { this._host = DesignerHost.instance(); this._overlapItems = observable([]); } /** * 获取重叠的items * @returns {Array} */ getOverlappedItems(): Array { return this._overlapItems.map(item => item); } getItemOverlapped(item: ReportItemBase): boolean { return this._overlapItems.indexOf(item) != -1; } setOverlappedItems(items: Array,removeItems: Array): void { items.forEach((item) => { this.setOverlappedItem(item,OverLapService.AddItem); }); removeItems.forEach((item) => { this.setOverlappedItem(item,OverLapService.RemoveItem); }); } setOverlappedItem(item: ReportItemBase, isOverlap: string): void { if( this._overlapItems.indexOf(item) == -1 ) { if (isOverlap == OverLapService.AddItem) { this._overlapItems.push(item); } }else { if (isOverlap == OverLapService.RemoveItem) { this._overlapItems.remove(item); } } } clear(): void { this._overlapItems.length = 0; } checkItemOverlap (items: Array, overlaps: Array): Array { const tempItems = items.slice(0); const len = items.length; if (items.length > 0) { const compareItem = items[0]; const compareDesigner = this._host.getDesigner(compareItem) as ReportItemBaseDesigner; const compareBounds = compareDesigner.getBounds(); const pos = new PointF(compareBounds.x + compareBounds.width/2, compareBounds.y + compareBounds.height/2); let hasAdd = false; for(var i = 1; i < len; i++) { const item = items[i]; const designer = this._host.getDesigner(item) as ReportItemBaseDesigner; if (designer) { const bounds = designer.getBounds(); const itemCenter = new PointF(bounds.x + bounds.width/2, bounds.y + bounds.height/2); const xlen = Math.abs(itemCenter.x - pos.x); const ylen = Math.abs(itemCenter.y - pos.y); if (xlen < (bounds.width+compareBounds.width)/2 && ylen < (bounds.height+compareBounds.height)/2 ) { overlaps.push(item); !hasAdd && overlaps.push(compareItem); hasAdd = true; } } } tempItems.splice(0,1); return this.checkItemOverlap(tempItems,overlaps) } else { return overlaps; } } /** * 检测item是否发生重叠 * @param items */ checkOverlap(changeItem: ReportItemBase): void { const parent = changeItem.parent; if (parent && parent.items) { let overlaps = []; overlaps = this.checkItemOverlap(parent.items, overlaps); // 重叠与未重叠的 const unOverlaps = parent.items.reduce( (items,item) => { if(overlaps.indexOf(item) == -1) items.push(item); return items; },[]); this.setOverlappedItems(overlaps, unOverlaps); } } }