DataService.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622
  1. import Line from "../Geometry/Line.js";
  2. import CurveLine from "../Geometry/CurveLine.js";
  3. import VectorType from "../enum/VectorType.js";
  4. import { coordinate } from "../Coordinate.js";
  5. import VectorCategory from "../enum/VectorCategory.js";
  6. export class DataService {
  7. constructor() {
  8. this.grid = null;
  9. this.vectorData = {
  10. currentId: 0, // 当前可用id
  11. };
  12. }
  13. setCurrentId(id) {
  14. this.vectorData.currentId = id;
  15. }
  16. getCurrentId() {
  17. return this.vectorData.currentId;
  18. }
  19. updateCurrentId() {
  20. ++this.vectorData.currentId;
  21. }
  22. getVectorData() {
  23. return this.vectorData;
  24. }
  25. initGrid() {
  26. this.grid = {
  27. defalutstep1: 25 * coordinate.ratio,
  28. defalutstep2: 125 * coordinate.ratio,
  29. display: true,
  30. };
  31. this.grid.start = JSON.parse(JSON.stringify(coordinate.center));
  32. this.setGridStartPosition();
  33. }
  34. setGridStartPosition() {
  35. this.grid.step1 =
  36. (this.grid.defalutstep1 * coordinate.zoom) / coordinate.defaultZoom;
  37. this.grid.step2 =
  38. (this.grid.defalutstep2 * coordinate.zoom) / coordinate.defaultZoom;
  39. let startScreenPosition = coordinate.getScreenXY(this.grid.start);
  40. while (startScreenPosition.x > 0 || startScreenPosition.y > 0) {
  41. if (startScreenPosition.x > 0) {
  42. startScreenPosition.x -= this.grid.step2;
  43. }
  44. if (startScreenPosition.y > 0) {
  45. startScreenPosition.y -= this.grid.step2;
  46. }
  47. }
  48. this.grid.startX = startScreenPosition.x;
  49. this.grid.startY = startScreenPosition.y;
  50. }
  51. initVectorData() {
  52. this.vectorData.backgroundImg = null;
  53. //直路
  54. this.vectorData.roadPoints = {};
  55. this.vectorData.roads = {};
  56. this.vectorData.roadEdges = {};
  57. //弯路
  58. this.vectorData.curveRoadPoints = {};
  59. this.vectorData.curveRoads = {};
  60. this.vectorData.curveRoadEdges = {};
  61. //直路交叉的时候,产生的曲线控制点
  62. this.vectorData.crossPoints = {};
  63. //直线,起点和终点都是point的id
  64. this.vectorData.lines = {};
  65. //弯曲线条
  66. this.vectorData.curvelines = {};
  67. //线段(完全或者直线)上的端点
  68. this.vectorData.points = {};
  69. this.vectorData.curvePoints = {};
  70. this.vectorData.circles = {};
  71. //基准点
  72. this.vectorData.basePointIds = [];
  73. this.vectorData.texts = {};
  74. this.vectorData.svgs = {};
  75. this.vectorData.magnifiers = {};
  76. this.initGrid();
  77. }
  78. //网格
  79. getGrid() {
  80. return this.grid;
  81. }
  82. setGridForPan() {
  83. this.setGridStartPosition();
  84. }
  85. setGridForZoom() {
  86. this.setGridStartPosition();
  87. }
  88. setGridDisplay(value) {
  89. this.grid.display = value;
  90. }
  91. getGridDisplay() {
  92. return this.grid.display;
  93. }
  94. //背景图片
  95. addBackgroundImg(img) {
  96. this.vectorData.backgroundImg = img;
  97. }
  98. getBackgroundImg() {
  99. return this.vectorData.backgroundImg;
  100. }
  101. setAngle(angle) {
  102. this.vectorData.backgroundImg.angle = angle;
  103. }
  104. //直线
  105. addLine(line) {
  106. this.vectorData.lines[line.vectorId] = line;
  107. }
  108. getLines() {
  109. return this.vectorData.lines;
  110. }
  111. getLine(lineId) {
  112. return this.vectorData.lines[lineId];
  113. }
  114. getGeo(type, id) {
  115. return this[`get${type}`](id);
  116. }
  117. deleteLine(lineId) {
  118. let line = this.getLine(lineId);
  119. if (!line) {
  120. return;
  121. }
  122. let start = this.getPoint(line.startId);
  123. if (start) {
  124. let startParent = start.getParent();
  125. delete startParent[lineId];
  126. if (
  127. Object.keys(startParent).length == 0 &&
  128. start.getCategory() != VectorCategory.Point.BasePoint &&
  129. start.getCategory() != VectorCategory.Point.FixPoint
  130. ) {
  131. this.deletePoint(line.startId);
  132. }
  133. }
  134. let end = this.getPoint(line.endId);
  135. if (end) {
  136. let endParent = end.getParent();
  137. delete endParent[lineId];
  138. if (
  139. Object.keys(endParent).length == 0 &&
  140. end.getCategory() != VectorCategory.Point.BasePoint &&
  141. end.getCategory() != VectorCategory.Point.FixPoint
  142. ) {
  143. this.deletePoint(line.endId);
  144. }
  145. }
  146. delete this.vectorData.lines[lineId];
  147. }
  148. //直线的端点
  149. getPoints() {
  150. return this.vectorData.points;
  151. }
  152. getPoint(pointId) {
  153. return this.vectorData.points[pointId];
  154. }
  155. addPoint(point) {
  156. this.vectorData.points[point.vectorId] = point;
  157. }
  158. deletePoint(pointId) {
  159. let point = this.getPoint(pointId);
  160. if (point) {
  161. delete this.vectorData.points[pointId];
  162. point = null;
  163. }
  164. }
  165. deletePointParent(pointId, lineId) {
  166. if (pointId && lineId) {
  167. let point = this.getPoint(pointId);
  168. if (point) {
  169. delete point.parent[lineId];
  170. }
  171. }
  172. }
  173. //圆圈
  174. getCircles() {
  175. return this.vectorData.circles;
  176. }
  177. getCircle(circleId) {
  178. return this.vectorData.circles[circleId];
  179. }
  180. addCircle(circle) {
  181. this.vectorData.circles[circle.vectorId] = circle;
  182. }
  183. deleteCircle(circleId) {
  184. delete this.vectorData.circles[circleId];
  185. }
  186. //弯曲线条
  187. addCurveLine(curveLine) {
  188. this.vectorData.curvelines[curveLine.vectorId] = curveLine;
  189. }
  190. addCurvePoint(curvePoint) {
  191. this.vectorData.curvePoints[curvePoint.vectorId] = curvePoint;
  192. }
  193. getCurvePoints() {
  194. return this.vectorData.curvePoints;
  195. }
  196. getCurvePoint(curvePointId) {
  197. if (curvePointId) {
  198. return this.vectorData.curvePoints[curvePointId];
  199. } else {
  200. return null;
  201. }
  202. }
  203. deleteCurvePoint(curvePointId) {
  204. delete this.vectorData.curvePoints[curvePointId];
  205. }
  206. getCurveLines() {
  207. return this.vectorData.curvelines;
  208. }
  209. getCurveLine(curveLineId) {
  210. if (curveLineId) {
  211. return this.vectorData.curvelines[curveLineId];
  212. } else {
  213. return null;
  214. }
  215. }
  216. deleteCurveLine(curveLineId) {
  217. delete this.vectorData.curvelines[curveLineId];
  218. }
  219. /**
  220. * 对弯路的操作
  221. */
  222. addCurveRoadPoint(curveRoadPoint) {
  223. this.vectorData.curveRoadPoints[curveRoadPoint.vectorId] = curveRoadPoint;
  224. }
  225. getCurveRoadPoints() {
  226. return this.vectorData.curveRoadPoints;
  227. }
  228. getCurveRoadPoint(curveRoadPointId) {
  229. if (curveRoadPointId) {
  230. return this.vectorData.curveRoadPoints[curveRoadPointId];
  231. } else {
  232. return null;
  233. }
  234. }
  235. deleteCurveRoadPoint(curveRoadPointId) {
  236. delete this.vectorData.curveRoadPoints[curveRoadPointId];
  237. }
  238. addCurveRoad(curveRoad) {
  239. this.vectorData.curveRoads[curveRoad.vectorId] = curveRoad;
  240. }
  241. getCurveRoads() {
  242. return this.vectorData.curveRoads;
  243. }
  244. getCurveRoad(curveRoadId) {
  245. if (curveRoadId) {
  246. return this.vectorData.curveRoads[curveRoadId];
  247. } else {
  248. return null;
  249. }
  250. }
  251. deleteCurveRoad(curveRoadId) {
  252. let curveRoad = this.getCurveRoad(curveRoadId);
  253. for (let i = 0; i < curveRoad.points.length; ++i) {
  254. this.deleteCurveRoadPoint(curveRoad.points[i].vectorId);
  255. }
  256. this.deleteCurveRoadEdge(curveRoad.leftEdgeId);
  257. this.deleteCurveRoadEdge(curveRoad.rightEdgeId);
  258. delete this.vectorData.curveRoads[curveRoadId];
  259. }
  260. getCurveRoadEdges() {
  261. return this.vectorData.curveRoadEdges;
  262. }
  263. getCurveRoadEdge(curveRoadEdgeId) {
  264. return this.vectorData.curveRoadEdges[curveRoadEdgeId];
  265. }
  266. addCurveRoadEdge(curveRoadEdge) {
  267. this.vectorData.curveRoadEdges[curveRoadEdge.vectorId] = curveRoadEdge;
  268. }
  269. deleteCurveRoadEdge(curveRoadEdgeId) {
  270. delete this.vectorData.curveRoadEdges[curveRoadEdgeId];
  271. }
  272. //直线
  273. getRoads() {
  274. return this.vectorData.roads;
  275. }
  276. getRoad(roadId) {
  277. if (roadId) {
  278. return this.vectorData.roads[roadId];
  279. } else {
  280. return null;
  281. }
  282. }
  283. addRoad(road) {
  284. this.vectorData.roads[road.vectorId] = road;
  285. }
  286. deleteRoad(roadId) {
  287. let road = this.getRoad(roadId);
  288. this.deleteRoadEdge(road.leftEdgeId);
  289. this.deleteRoadEdge(road.rightEdgeId);
  290. delete this.vectorData.roads[roadId];
  291. this.deleteRoadPoint(road.startId, roadId);
  292. this.deleteRoadPoint(road.endId, roadId);
  293. }
  294. /**
  295. * 对端点的操作
  296. */
  297. getRoadPoints() {
  298. return this.vectorData.roadPoints;
  299. }
  300. getRoadPoint(roadPointId) {
  301. if (roadPointId) {
  302. return this.vectorData.roadPoints[roadPointId];
  303. } else {
  304. return null;
  305. }
  306. }
  307. deleteRoadPoint(roadPointId, roadId) {
  308. let roadPoint = this.getRoadPoint(roadPointId);
  309. //有可能先删除墙,导致点没了
  310. if (roadPoint) {
  311. if (Object.keys(roadPoint.parent).length == 0) {
  312. roadPoint = null;
  313. delete this.vectorData.roadPoints[roadPointId];
  314. } else if (Object.keys(roadPoint.parent).length == 1 && !roadId) {
  315. delete this.vectorData.roadPoints[roadPointId];
  316. } else if (
  317. Object.keys(roadPoint.parent).length == 1 &&
  318. roadPoint.parent[roadId]
  319. ) {
  320. delete this.vectorData.roadPoints[roadPointId];
  321. } else if (
  322. Object.keys(roadPoint.parent).length == 1 &&
  323. !roadPoint.parent[roadId]
  324. ) {
  325. return;
  326. } else {
  327. delete roadPoint.parent[roadId];
  328. }
  329. }
  330. }
  331. deleteRoadPoint1(roadPointId) {
  332. delete this.vectorData.roadPoints[roadPointId];
  333. }
  334. addRoadPoint(roadPoint) {
  335. this.vectorData.roadPoints[roadPoint.vectorId] = roadPoint;
  336. }
  337. getRoadEdges() {
  338. return this.vectorData.roadEdges;
  339. }
  340. deleteRoadEdge(edgeId) {
  341. delete this.vectorData.roadEdges[edgeId];
  342. }
  343. getRoadEdge(roadEdgeId) {
  344. return this.vectorData.roadEdges[roadEdgeId];
  345. }
  346. addRoadEdge(roadEdge) {
  347. this.vectorData.roadEdges[roadEdge.vectorId] = roadEdge;
  348. }
  349. /**
  350. * 对控制点的操作
  351. */
  352. getCrossPoints() {
  353. return this.vectorData.crossPoints;
  354. }
  355. getCrossPoint(edgeId1, edgeId2) {
  356. if (this.vectorData.crossPoints[edgeId1 + "-" + edgeId2]) {
  357. return this.vectorData.crossPoints[edgeId1 + "-" + edgeId2];
  358. } else if (this.vectorData.crossPoints[edgeId2 + "-" + edgeId1]) {
  359. return this.vectorData.crossPoints[edgeId2 + "-" + edgeId1];
  360. } else {
  361. return null;
  362. }
  363. }
  364. getCrossPoint2(key) {
  365. return this.vectorData.crossPoints[key];
  366. }
  367. getCrossPoint3(vectorId) {
  368. for (let key in this.vectorData.crossPoints) {
  369. if (this.vectorData.crossPoints[key].vectorId == vectorId) {
  370. return this.vectorData.crossPoints[key];
  371. }
  372. }
  373. }
  374. getCrossPoint4(edgeId) {
  375. for (let key in this.vectorData.crossPoints) {
  376. if (key.indexOf(edgeId + "-") > -1 || key.indexOf("-" + edgeId) > -1) {
  377. return this.vectorData.crossPoints[key];
  378. }
  379. }
  380. }
  381. getCrossPointForEdgeId(edgeId, dir) {
  382. for (let key in this.vectorData.crossPoints) {
  383. const crossPoint = this.vectorData.crossPoints[key];
  384. if (
  385. crossPoint.edgeInfo1.id == edgeId &&
  386. crossPoint.edgeInfo1.dir == dir
  387. ) {
  388. return this.vectorData.crossPoints[key];
  389. } else if (
  390. crossPoint.edgeInfo2.id == edgeId &&
  391. crossPoint.edgeInfo2.dir == dir
  392. ) {
  393. return this.vectorData.crossPoints[key];
  394. }
  395. }
  396. }
  397. addCrossPoint(crossPoint) {
  398. this.vectorData.crossPoints[
  399. crossPoint.edgeInfo1.id + "-" + crossPoint.edgeInfo2.id
  400. ] = crossPoint;
  401. }
  402. deleteCrossPoint(edgeId1, edgeId2) {
  403. let key = edgeId1 + "-" + edgeId2;
  404. if (!this.vectorData.crossPoints[key]) {
  405. key = edgeId2 + "-" + edgeId1;
  406. }
  407. if (this.vectorData.crossPoints[key]) {
  408. delete this.vectorData.crossPoints[key];
  409. }
  410. }
  411. deleteCrossPoint1(vectorId) {
  412. for (let key in this.vectorData.crossPoints) {
  413. if (this.vectorData.crossPoints[key].vectorId == vectorId) {
  414. delete this.vectorData.crossPoints[key];
  415. return;
  416. }
  417. }
  418. }
  419. deleteCrossPointForEdge(edgeId, dir) {
  420. let dCrossPointIds = [];
  421. for (let key in this.vectorData.crossPoints) {
  422. const crossPoint = this.vectorData.crossPoints[key];
  423. if (
  424. crossPoint.edgeInfo1.id == edgeId ||
  425. crossPoint.edgeInfo2.id == edgeId
  426. ) {
  427. dCrossPointIds.push(key);
  428. }
  429. }
  430. for (let i = 0; i < dCrossPointIds.length; ++i) {
  431. const crossPoint = this.vectorData.crossPoints[dCrossPointIds[i]];
  432. if (
  433. crossPoint.edgeInfo1.id == edgeId &&
  434. crossPoint.edgeInfo1.dir == dir
  435. ) {
  436. delete this.vectorData.crossPoints[dCrossPointIds[i]];
  437. } else if (
  438. crossPoint.edgeInfo2.id == edgeId &&
  439. crossPoint.edgeInfo2.dir == dir
  440. ) {
  441. delete this.vectorData.crossPoints[dCrossPointIds[i]];
  442. }
  443. }
  444. }
  445. /**
  446. * 对标注的操作
  447. */
  448. addText(text) {
  449. this.vectorData.texts[text.vectorId] = text;
  450. }
  451. getText(textId) {
  452. return this.vectorData.texts[textId];
  453. }
  454. deleteText(textId) {
  455. delete this.vectorData.texts[textId];
  456. }
  457. getTexts() {
  458. return this.vectorData.texts;
  459. }
  460. /**
  461. * 对放大镜的操作
  462. */
  463. addMagnifier(magnifier) {
  464. this.vectorData.magnifiers[magnifier.vectorId] = magnifier;
  465. }
  466. getMagnifier(magnifierId) {
  467. return this.vectorData.magnifiers[magnifierId];
  468. }
  469. deleteMagnifier(magnifierId) {
  470. delete this.vectorData.magnifiers[magnifierId];
  471. }
  472. getMagnifiers() {
  473. return this.vectorData.magnifiers;
  474. }
  475. //处理从svg转换来的图标
  476. addSVG(SVG) {
  477. this.vectorData.svgs[SVG.vectorId] = SVG;
  478. }
  479. getSVG(SVGId) {
  480. return this.vectorData.svgs[SVGId];
  481. }
  482. deleteSVG(SVGId) {
  483. delete this.vectorData.svgs[SVGId];
  484. }
  485. getSVGs() {
  486. return this.vectorData.svgs;
  487. }
  488. clear() {
  489. //直路
  490. this.vectorData.roadPoints = {};
  491. this.vectorData.roads = {};
  492. this.vectorData.roadEdges = {};
  493. //弯路
  494. this.vectorData.curveRoadPoints = {};
  495. this.vectorData.curveRoads = {};
  496. this.vectorData.curveRoadEdges = {};
  497. //直路交叉的时候,产生的曲线控制点
  498. this.vectorData.crossPoints = {};
  499. //直线,起点和终点都是point的id
  500. this.vectorData.lines = {};
  501. //弯曲线条
  502. this.vectorData.curvelines = {};
  503. //线段(完全或者直线)上的端点
  504. this.vectorData.points = {};
  505. this.vectorData.curvePoints = {};
  506. this.vectorData.circles = {};
  507. //基准点
  508. this.vectorData.basePointIds = [];
  509. this.vectorData.texts = {};
  510. this.vectorData.svgs = {};
  511. this.vectorData.magnifiers = {};
  512. }
  513. setRes(value) {
  514. this.vectorData.res = value;
  515. }
  516. getRes() {
  517. return this.vectorData.res;
  518. }
  519. setScale(value) {
  520. this.vectorData.scale = value;
  521. }
  522. getScale() {
  523. return this.vectorData.scale;
  524. }
  525. }
  526. const dataService = new DataService();
  527. export { dataService };