import Line from "../Geometry/Line.js"; import CurveLine from "../Geometry/CurveLine.js"; import VectorType from "../enum/VectorType.js"; import { coordinate } from "../Coordinate.js"; import VectorCategory from "../enum/VectorCategory.js"; export class DataService { constructor() { this.grid = null; this.vectorData = { currentId: 0, // 当前可用id }; } setCurrentId(id) { this.vectorData.currentId = id; } getCurrentId() { return this.vectorData.currentId; } updateCurrentId() { ++this.vectorData.currentId; } getVectorData() { return this.vectorData; } initGrid() { this.grid = { defalutstep1: 25 * coordinate.ratio, defalutstep2: 125 * coordinate.ratio, display: true, }; this.grid.start = JSON.parse(JSON.stringify(coordinate.center)); this.setGridStartPosition(); } setGridStartPosition() { this.grid.step1 = (this.grid.defalutstep1 * coordinate.zoom) / coordinate.defaultZoom; this.grid.step2 = (this.grid.defalutstep2 * coordinate.zoom) / coordinate.defaultZoom; let startScreenPosition = coordinate.getScreenXY(this.grid.start); while (startScreenPosition.x > 0 || startScreenPosition.y > 0) { if (startScreenPosition.x > 0) { startScreenPosition.x -= this.grid.step2; } if (startScreenPosition.y > 0) { startScreenPosition.y -= this.grid.step2; } } this.grid.startX = startScreenPosition.x; this.grid.startY = startScreenPosition.y; } initVectorData() { this.vectorData.backgroundImg = null; //直路 this.vectorData.roadPoints = {}; this.vectorData.roads = {}; this.vectorData.roadEdges = {}; //弯路 this.vectorData.curveRoadPoints = {}; this.vectorData.curveRoads = {}; this.vectorData.curveRoadEdges = {}; //直路交叉的时候,产生的曲线控制点 this.vectorData.crossPoints = {}; //直线,起点和终点都是point的id this.vectorData.lines = {}; //弯曲线条 this.vectorData.curvelines = {}; //线段(完全或者直线)上的端点 this.vectorData.points = {}; this.vectorData.curvePoints = {}; this.vectorData.circles = {}; //基准点 this.vectorData.basePointIds = []; this.vectorData.texts = {}; this.vectorData.svgs = {}; this.vectorData.magnifiers = {}; this.initGrid(); } //网格 getGrid() { return this.grid; } setGridForPan() { this.setGridStartPosition(); } setGridForZoom() { this.setGridStartPosition(); } setGridDisplay(value) { this.grid.display = value; } getGridDisplay() { return this.grid.display; } //背景图片 addBackgroundImg(img) { this.vectorData.backgroundImg = img; } getBackgroundImg() { return this.vectorData.backgroundImg; } setAngle(angle) { this.vectorData.backgroundImg.angle = angle; } //直线 addLine(line) { this.vectorData.lines[line.vectorId] = line; } getLines() { return this.vectorData.lines; } getLine(lineId) { return this.vectorData.lines[lineId]; } deleteLine(lineId) { let line = this.getLine(lineId); if (!line) { return; } let start = this.getPoint(line.startId); if (start && start.getCategory() != VectorCategory.Point.BasePoint) { let startParent = start.getParent(); delete startParent[lineId]; if ( Object.keys(startParent).length == 0 && start.getCategory() != VectorCategory.Point.BasePoint ) { this.deletePoint(line.startId); } } let end = this.getPoint(line.endId); if (end) { let endParent = end.getParent(); delete endParent[lineId]; if ( Object.keys(endParent).length == 0 && end.getCategory() != VectorCategory.Point.BasePoint ) { this.deletePoint(line.endId); } } delete this.vectorData.lines[lineId]; } //直线的端点 getPoints() { return this.vectorData.points; } getPoint(pointId) { return this.vectorData.points[pointId]; } addPoint(point) { this.vectorData.points[point.vectorId] = point; } deletePoint(pointId) { let point = this.getPoint(pointId); if (point) { delete this.vectorData.points[pointId]; point = null; } } deletePointParent(pointId, lineId) { if (pointId && lineId) { let point = this.getPoint(pointId); if (point) { delete point.parent[lineId]; } } } //圆圈 getCircles() { return this.vectorData.circles; } getCircle(circleId) { return this.vectorData.circles[circleId]; } addCircle(circle) { this.vectorData.circles[circle.vectorId] = circle; } deleteCircle(circleId) { delete this.vectorData.circles[circleId]; } //弯曲线条 addCurveLine(curveLine) { this.vectorData.curvelines[curveLine.vectorId] = curveLine; } addCurvePoint(curvePoint) { this.vectorData.curvePoints[curvePoint.vectorId] = curvePoint; } getCurvePoints() { return this.vectorData.curvePoints; } getCurvePoint(curvePointId) { if (curvePointId) { return this.vectorData.curvePoints[curvePointId]; } else { return null; } } deleteCurvePoint(curvePointId) { delete this.vectorData.curvePoints[curvePointId]; } getCurveLines() { return this.vectorData.curvelines; } getCurveLine(curveLineId) { if (curveLineId) { return this.vectorData.curvelines[curveLineId]; } else { return null; } } deleteCurveLine(curveLineId) { delete this.vectorData.curvelines[curveLineId]; } /** * 对弯路的操作 */ addCurveRoadPoint(curveRoadPoint) { this.vectorData.curveRoadPoints[curveRoadPoint.vectorId] = curveRoadPoint; } getCurveRoadPoints() { return this.vectorData.curveRoadPoints; } getCurveRoadPoint(curveRoadPointId) { if (curveRoadPointId) { return this.vectorData.curveRoadPoints[curveRoadPointId]; } else { return null; } } deleteCurveRoadPoint(curveRoadPointId) { delete this.vectorData.curveRoadPoints[curveRoadPointId]; } addCurveRoad(curveRoad) { this.vectorData.curveRoads[curveRoad.vectorId] = curveRoad; } getCurveRoads() { return this.vectorData.curveRoads; } getCurveRoad(curveRoadId) { if (curveRoadId) { return this.vectorData.curveRoads[curveRoadId]; } else { return null; } } deleteCurveRoad(curveRoadId) { let curveRoad = this.getCurveRoad(curveRoadId); for (let i = 0; i < curveRoad.points.length; ++i) { this.deleteCurveRoadPoint(curveRoad.points[i].vectorId); } this.deleteCurveRoadEdge(curveRoad.leftEdgeId); this.deleteCurveRoadEdge(curveRoad.rightEdgeId); delete this.vectorData.curveRoads[curveRoadId]; } getCurveRoadEdges() { return this.vectorData.curveRoadEdges; } getCurveRoadEdge(curveRoadEdgeId) { return this.vectorData.curveRoadEdges[curveRoadEdgeId]; } addCurveRoadEdge(curveRoadEdge) { this.vectorData.curveRoadEdges[curveRoadEdge.vectorId] = curveRoadEdge; } deleteCurveRoadEdge(curveRoadEdgeId) { delete this.vectorData.curveRoadEdges[curveRoadEdgeId]; } //直线 getRoads() { return this.vectorData.roads; } getRoad(roadId) { if (roadId) { return this.vectorData.roads[roadId]; } else { return null; } } addRoad(road) { this.vectorData.roads[road.vectorId] = road; } deleteRoad(roadId) { let road = this.getRoad(roadId); this.deleteRoadEdge(road.leftEdgeId); this.deleteRoadEdge(road.rightEdgeId); delete this.vectorData.roads[roadId]; this.deleteRoadPoint(road.startId, roadId); this.deleteRoadPoint(road.endId, roadId); } /** * 对端点的操作 */ getRoadPoints() { return this.vectorData.roadPoints; } getRoadPoint(roadPointId) { if (roadPointId) { return this.vectorData.roadPoints[roadPointId]; } else { return null; } } deleteRoadPoint(roadPointId, roadId) { let roadPoint = this.getRoadPoint(roadPointId); //有可能先删除墙,导致点没了 if (roadPoint) { if (Object.keys(roadPoint.parent).length == 0) { roadPoint = null; delete this.vectorData.roadPoints[roadPointId]; } else if (Object.keys(roadPoint.parent).length == 1 && !roadId) { delete this.vectorData.roadPoints[roadPointId]; } else if ( Object.keys(roadPoint.parent).length == 1 && roadPoint.parent[roadId] ) { delete this.vectorData.roadPoints[roadPointId]; } else if ( Object.keys(roadPoint.parent).length == 1 && !roadPoint.parent[roadId] ) { return; } else { delete roadPoint.parent[roadId]; } } } deleteRoadPoint1(roadPointId) { delete this.vectorData.roadPoints[roadPointId]; } addRoadPoint(roadPoint) { this.vectorData.roadPoints[roadPoint.vectorId] = roadPoint; } getRoadEdges() { return this.vectorData.roadEdges; } deleteRoadEdge(edgeId) { delete this.vectorData.roadEdges[edgeId]; } getRoadEdge(roadEdgeId) { return this.vectorData.roadEdges[roadEdgeId]; } addRoadEdge(roadEdge) { this.vectorData.roadEdges[roadEdge.vectorId] = roadEdge; } /** * 对控制点的操作 */ getCrossPoints() { return this.vectorData.crossPoints; } getCrossPoint(edgeId1, edgeId2) { if (this.vectorData.crossPoints[edgeId1 + "-" + edgeId2]) { return this.vectorData.crossPoints[edgeId1 + "-" + edgeId2]; } else if (this.vectorData.crossPoints[edgeId2 + "-" + edgeId1]) { return this.vectorData.crossPoints[edgeId2 + "-" + edgeId1]; } else { return null; } } getCrossPoint2(key) { return this.vectorData.crossPoints[key]; } getCrossPoint3(vectorId) { for (let key in this.vectorData.crossPoints) { if (this.vectorData.crossPoints[key].vectorId == vectorId) { return this.vectorData.crossPoints[key]; } } } getCrossPoint4(edgeId) { for (let key in this.vectorData.crossPoints) { if (key.indexOf(edgeId + "-") > -1 || key.indexOf("-" + edgeId) > -1) { return this.vectorData.crossPoints[key]; } } } getCrossPointForEdgeId(edgeId, dir) { for (let key in this.vectorData.crossPoints) { const crossPoint = this.vectorData.crossPoints[key]; if ( crossPoint.edgeInfo1.id == edgeId && crossPoint.edgeInfo1.dir == dir ) { return this.vectorData.crossPoints[key]; } else if ( crossPoint.edgeInfo2.id == edgeId && crossPoint.edgeInfo2.dir == dir ) { return this.vectorData.crossPoints[key]; } } } addCrossPoint(crossPoint) { this.vectorData.crossPoints[ crossPoint.edgeInfo1.id + "-" + crossPoint.edgeInfo2.id ] = crossPoint; } deleteCrossPoint(edgeId1, edgeId2) { let key = edgeId1 + "-" + edgeId2; if (!this.vectorData.crossPoints[key]) { key = edgeId2 + "-" + edgeId1; } if (this.vectorData.crossPoints[key]) { delete this.vectorData.crossPoints[key]; } } deleteCrossPoint1(vectorId) { for (let key in this.vectorData.crossPoints) { if (this.vectorData.crossPoints[key].vectorId == vectorId) { delete this.vectorData.crossPoints[key]; return; } } } deleteCrossPointForEdge(edgeId, dir) { let dCrossPointIds = []; for (let key in this.vectorData.crossPoints) { const crossPoint = this.vectorData.crossPoints[key]; if ( crossPoint.edgeInfo1.id == edgeId || crossPoint.edgeInfo2.id == edgeId ) { dCrossPointIds.push(key); } } for (let i = 0; i < dCrossPointIds.length; ++i) { const crossPoint = this.vectorData.crossPoints[dCrossPointIds[i]]; if ( crossPoint.edgeInfo1.id == edgeId && crossPoint.edgeInfo1.dir == dir ) { delete this.vectorData.crossPoints[dCrossPointIds[i]]; } else if ( crossPoint.edgeInfo2.id == edgeId && crossPoint.edgeInfo2.dir == dir ) { delete this.vectorData.crossPoints[dCrossPointIds[i]]; } } } /** * 对标注的操作 */ addText(text) { this.vectorData.texts[text.vectorId] = text; } getText(textId) { return this.vectorData.texts[textId]; } deleteText(textId) { delete this.vectorData.texts[textId]; } getTexts() { return this.vectorData.texts; } /** * 对放大镜的操作 */ addMagnifier(magnifier) { this.vectorData.magnifiers[magnifier.vectorId] = magnifier; } getMagnifier(magnifierId) { return this.vectorData.magnifiers[magnifierId]; } deleteMagnifier(magnifierId) { delete this.vectorData.magnifiers[magnifierId]; } getMagnifiers() { return this.vectorData.magnifiers; } //处理从svg转换来的图标 addSVG(SVG) { this.vectorData.svgs[SVG.vectorId] = SVG; } getSVG(SVGId) { return this.vectorData.svgs[SVGId]; } deleteSVG(SVGId) { delete this.vectorData.svgs[SVGId]; } getSVGs() { return this.vectorData.svgs; } clear() { //直路 this.vectorData.roadPoints = {}; this.vectorData.roads = {}; this.vectorData.roadEdges = {}; //弯路 this.vectorData.curveRoadPoints = {}; this.vectorData.curveRoads = {}; this.vectorData.curveRoadEdges = {}; //直路交叉的时候,产生的曲线控制点 this.vectorData.crossPoints = {}; //直线,起点和终点都是point的id this.vectorData.lines = {}; //弯曲线条 this.vectorData.curvelines = {}; //线段(完全或者直线)上的端点 this.vectorData.points = {}; this.vectorData.curvePoints = {}; this.vectorData.circles = {}; //基准点 this.vectorData.basePointIds = []; this.vectorData.texts = {}; this.vectorData.svgs = {}; this.vectorData.magnifiers = {}; } setRes(value) { this.vectorData.res = value; } getRes() { return this.vectorData.res; } setScale(value) { this.vectorData.scale = value; } getScale() { return this.vectorData.scale; } } const dataService = new DataService(); export { dataService };