/** * Created by guopeng on 16/7/26. */ import * as ReactDOM from "react-dom"; import {DropTargetMonitor, DropTargetSpec} from "react-dnd"; import {PointF} from "../drawing/Unit"; import {DesignerHost} from "../design/DesignerHost"; import {SelectionService} from "../design/behavior/SelectionService"; import {ItemBodyGlyph} from "../design/behavior/ItemBodyGlyph"; import {BandDesignerBase} from "../design/BandDesignerBase"; import {ReportItemBaseDesigner} from "../design/designer/ReportItemBaseDesigner"; import {Unit, UnitType, RectF} from "../drawing/Unit"; import {ReportRootDesigner} from "../design/designer/ReportRootDesigner"; import {ReportBandBase} from "../items/band/ReportBandBase"; import {ReportItem} from "../items/ReportItem"; import {SnappingEngine} from "../design/behavior/SnappingEngine"; import {SnapLineEngine} from "../design/behavior/SnapLineEngine"; import SnackBar from '../helper/snackbar'; let singletonInstance: DNDManager; export default class DNDManager { private _host: DesignerHost; private _snappingEngine: SnappingEngine; constructor(){ this._host = DesignerHost.instance(); } static instance(): DNDManager{ if (! singletonInstance) { singletonInstance = new DNDManager(); } return singletonInstance; } toolboxDrop(props, monitor?:DropTargetMonitor, component?: any) { const clientOffset:{x:number, y:number} = monitor.getClientOffset(); if(component){ const pt = this.getMousePointToStage(clientOffset,this._getContentPosition(ReactDOM.findDOMNode(component))); const band = this.getDropBandItem(pt); const item = monitor.getItem()&&monitor.getItem()["model"]; if(band && item){ const acceptResult = band.canAcceptItem ? band.canAcceptItem(item) : {result: true}; if (acceptResult.result) { const bandDesigner = DesignerHost.instance().getDesigner(band); const bound = bandDesigner.getBounds(); const preferredSize = item.preferredSize(); band.items.push(DesignerHost.createItem(item,{ top: pt.y - bound.y, left: pt.x - bound.x, width: preferredSize.width, height: preferredSize.height })); bandDesigner.updateMinBounds(); let selectionService = this._host.getService(SelectionService) as SelectionService; selectionService.clear(); selectionService.setSelectedItems([item]); DesignerHost.instance().historyService.snap(); } else { SnackBar.show(acceptResult.msg); } } } this.clear(); } toolboxDragHover(props, monitor?:DropTargetMonitor, component?: any){ const clientOffset:{x:number, y:number} = monitor.getClientOffset(); const dndItem = monitor.getItem(); if(component && dndItem) { const item = dndItem["model"]; const dnd = dndItem["dnd"]; const lastPoint = dnd.lastPoint; let pt: PointF = this.getMousePointToStage(clientOffset, this._getContentPosition(ReactDOM.findDOMNode(component))); pt.x = Math.ceil(pt.x); pt.y = Math.ceil(pt.y); if(pt.x === lastPoint.x && pt.y === lastPoint.y){return;} if(!dnd.isfirstEnter){ dnd.isfirstEnter = true; this.initDragtoolboxItem(item); }else{ if (this._snappingEngine) { const preferredSize = item.preferredSize(); const bounds = new RectF(pt.x, pt.y,preferredSize.width,preferredSize.height); const snapOffset = this._snappingEngine.snap(bounds.location, bounds, null); } } item.top = Math.ceil(pt.x); item.left = Math.ceil(pt.y); dnd.lastPoint = pt; } } horizonRulerDrop(props, monitor?:DropTargetMonitor, component?: any) { const clientOffset:{x:number, y:number} = monitor.getClientOffset(); const lineItem: any = monitor.getItem(); if(lineItem){ lineItem.lastPoint = {x: 0}; } DesignerHost.instance().historyService.snap(); } horizonRulerHover(props, monitor?:DropTargetMonitor, component?: any){ // let reportModel = component.reportModel; const reportModel = DesignerHost.instance().designReport.report; const paperSize = reportModel.paper.paperSize; const halfPaperWidth = paperSize.width.pixels / 2; // 纸张一半宽度 const differenceOffset: {x: number} = monitor.getDifferenceFromInitialOffset(); // 相对于beginDrag时位置的间距 const lineItem: any = monitor.getItem(); // 此处获取的是`beginDrag`时的item if (lineItem) { const lastPoint = lineItem.lastPoint; if(lastPoint.x === differenceOffset.x){return;} // 拖动左侧游标 if (lineItem.lineType === "left") { let x = lineItem.leftMargin.pixels + differenceOffset.x; // 拖动不能超过一半纸张宽度和标尺外 if (x > -1 && Math.abs(x) < halfPaperWidth) { reportModel.leftMargin = new Unit(x); } } else { // 拖动右侧游标 let x = lineItem.rightMargin.pixels - differenceOffset.x; // 拖动不能超过一半纸张宽度和标尺外 if (x > -1 && Math.abs(x) < halfPaperWidth) { reportModel.rightMargin = new Unit(x); } } lineItem.lastPoint = {x: differenceOffset.x} } } verticalRulerDrop(props, monitor?:DropTargetMonitor, component?: any) { const clientOffset:{x:number, y:number} = monitor.getClientOffset(); const lineItem: any = monitor.getItem(); if(lineItem){ lineItem.lastPoint = {y: 0}; } DesignerHost.instance().historyService.snap(); } verticalRulerHover(props, monitor?:DropTargetMonitor, component?: any){ // let reportModel = component.reportModel; const reportModel = DesignerHost.instance().designReport.report; const paperSize = reportModel.paper.paperSize; const halfPaperHeight = paperSize.height.pixels / 2; // 纸张一半宽度 const differenceOffset: {y: number} = monitor.getDifferenceFromInitialOffset(); // 相对于beginDrag时位置的间距 const lineItem: any = monitor.getItem(); // 此处获取的是`beginDrag`时的item if (lineItem) { const lastPoint = lineItem.lastPoint; if(lastPoint.y === differenceOffset.y){return;} // 拖动左侧游标 if (lineItem.lineType === "top") { let y = lineItem.topMargin.pixels + differenceOffset.y; // 拖动不能超过一半纸张宽度和标尺外 if (y > -1 && Math.abs(y) < halfPaperHeight) { reportModel.topMargin = new Unit(y); } } else { // 拖动右侧游标 let y = lineItem.bottomMargin.pixels - differenceOffset.y; // 拖动不能超过一半纸张宽度和标尺外 if (y > -1 && Math.abs(y) < halfPaperHeight) { reportModel.bottomMargin = new Unit(y); } } lineItem.lastPoint = {y: differenceOffset.y} } } initDragtoolboxItem(item: ReportItem){ this.clear(); const componentsToDrag: Array = [item]; // 实例化对齐辅助线服务对象 this._snappingEngine = new SnappingEngine(this._host, componentsToDrag); const snapLineEngine: SnapLineEngine = this._snappingEngine.snapLineEngine; if(this._snappingEngine.snapLineEngine){ snapLineEngine.on("repaint", (val) => { this._host.designReport.report.setNeedDisplay(); }); } } inspectorDrop(props, monitor?:DropTargetMonitor, component?: any){ const inspectorModel= DesignerHost.instance().uiModel.inspectorModel; inspectorModel.isDrag = false; } // 移动 inspector 的窗口大小 inspectorHover(props, monitor?:DropTargetMonitor, component?: any){ const inspectorModel= DesignerHost.instance().uiModel.inspectorModel; const differenceOffset: {x: number} = monitor.getDifferenceFromInitialOffset(); const lineItem: any = monitor.getItem(); const width = lineItem.inspectorWidth - differenceOffset.x; if(inspectorModel.inspectorWidth !== width){ inspectorModel.inspectorWidth = width; } } /** * 通过坐标获取所在band * @param pt * @returns {any} */ getDropBandItem(pt: PointF){ const designerHost = DesignerHost.instance(); const bands: Array = ReportRootDesigner.getBandOrderd(designerHost.designReport.report); const bandItem = bands.find(function (band,idx,arr) { return designerHost.getDesigner(band).getBounds().contains(pt); }); if(bandItem){ return bandItem; } return null; } getMousePointToStage(point: any, containerPoint): PointF { let contentPosition = containerPoint; let x = null; let y = null; // mouse events x = point.x - contentPosition.x; y = point.y - contentPosition.y; if (x !== null && y !== null) { return new PointF(x, y); } return new PointF(-100000, -100000); } _getContentPosition(dom): PointF { var rect = dom.getBoundingClientRect(); return new PointF(rect.left, rect.top); } /** * 清理不需要再占用的资源 */ 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; } } }