123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569 |
- 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 = {
- startX: 0,
- startY: 0,
- step1: 50,
- step2: 250,
- defalutstep1: 50,
- defalutstep2: 250,
- display: true,
- };
- this.vectorData = {
- currentId: 0, // 当前可用id
- };
- }
- setCurrentId(id) {
- this.vectorData.currentId = id;
- }
- getCurrentId() {
- return this.vectorData.currentId;
- }
- updateCurrentId() {
- ++this.vectorData.currentId;
- }
- getVectorData() {
- return this.vectorData;
- }
- 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.circles = {};
- //基准点
- this.vectorData.basePointIds = [];
- this.vectorData.texts = {};
- this.vectorData.svgs = {};
- this.vectorData.magnifiers = {};
- }
- //网格
- getGrid() {
- return this.grid;
- }
- setGridForPan(dx, dy) {
- this.grid.startX += dx;
- this.grid.startY += dy;
- this.grid.step1 =
- (this.grid.defalutstep1 * coordinate.zoom) / coordinate.defaultZoom;
- this.grid.step2 =
- (this.grid.defalutstep2 * coordinate.zoom) / coordinate.defaultZoom;
- while (this.grid.startX > 0) {
- this.grid.startX -= this.grid.step2;
- }
- while (this.grid.startY > 0) {
- this.grid.startY -= this.grid.step2;
- }
- }
- setGridForZoom(w, h, ratio) {
- console.log("setGridForZoom:" + ratio);
- this.grid.startX = w / 2 - (ratio * w) / 2;
- this.grid.startY = w / 2 - (ratio * h) / 2;
- this.grid.step1 = this.grid.defalutstep1 * ratio;
- this.grid.step2 = this.grid.defalutstep2 * ratio;
- while (this.grid.startX > 0) {
- this.grid.startX -= this.grid.step2;
- }
- while (this.grid.startY > 0) {
- this.grid.startY -= this.grid.step2;
- }
- }
- 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);
- let start = this.getPoint(line.startId);
- if (start) {
- 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;
- }
- }
- //圆圈
- 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;
- }
- }
- /**
- * 对弯路的操作
- */
- 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];
- }
- 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.deleteRoadPoint(road.startId, roadId);
- this.deleteRoadPoint(road.endId, roadId);
- this.deleteRoadEdge(road.leftEdgeId);
- this.deleteRoadEdge(road.rightEdgeId);
- delete this.vectorData.roads[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];
- }
- }
- }
- 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.circles = {};
- //基准点
- this.vectorData.basePointIds = [];
- this.vectorData.texts = {};
- this.vectorData.svgs = {};
- this.vectorData.magnifiers = {};
- }
- }
- const dataService = new DataService();
- export { dataService };
|