DataService.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618
  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. deleteLine(lineId) {
  115. let line = this.getLine(lineId);
  116. if (!line) {
  117. return;
  118. }
  119. let start = this.getPoint(line.startId);
  120. if (start) {
  121. let startParent = start.getParent();
  122. delete startParent[lineId];
  123. if (
  124. Object.keys(startParent).length == 0 &&
  125. start.getCategory() != VectorCategory.Point.BasePoint &&
  126. start.getCategory() != VectorCategory.Point.FixPoint
  127. ) {
  128. this.deletePoint(line.startId);
  129. }
  130. }
  131. let end = this.getPoint(line.endId);
  132. if (end) {
  133. let endParent = end.getParent();
  134. delete endParent[lineId];
  135. if (
  136. Object.keys(endParent).length == 0 &&
  137. end.getCategory() != VectorCategory.Point.BasePoint &&
  138. end.getCategory() != VectorCategory.Point.FixPoint
  139. ) {
  140. this.deletePoint(line.endId);
  141. }
  142. }
  143. delete this.vectorData.lines[lineId];
  144. }
  145. //直线的端点
  146. getPoints() {
  147. return this.vectorData.points;
  148. }
  149. getPoint(pointId) {
  150. return this.vectorData.points[pointId];
  151. }
  152. addPoint(point) {
  153. this.vectorData.points[point.vectorId] = point;
  154. }
  155. deletePoint(pointId) {
  156. let point = this.getPoint(pointId);
  157. if (point) {
  158. delete this.vectorData.points[pointId];
  159. point = null;
  160. }
  161. }
  162. deletePointParent(pointId, lineId) {
  163. if (pointId && lineId) {
  164. let point = this.getPoint(pointId);
  165. if (point) {
  166. delete point.parent[lineId];
  167. }
  168. }
  169. }
  170. //圆圈
  171. getCircles() {
  172. return this.vectorData.circles;
  173. }
  174. getCircle(circleId) {
  175. return this.vectorData.circles[circleId];
  176. }
  177. addCircle(circle) {
  178. this.vectorData.circles[circle.vectorId] = circle;
  179. }
  180. deleteCircle(circleId) {
  181. delete this.vectorData.circles[circleId];
  182. }
  183. //弯曲线条
  184. addCurveLine(curveLine) {
  185. this.vectorData.curvelines[curveLine.vectorId] = curveLine;
  186. }
  187. addCurvePoint(curvePoint) {
  188. this.vectorData.curvePoints[curvePoint.vectorId] = curvePoint;
  189. }
  190. getCurvePoints() {
  191. return this.vectorData.curvePoints;
  192. }
  193. getCurvePoint(curvePointId) {
  194. if (curvePointId) {
  195. return this.vectorData.curvePoints[curvePointId];
  196. } else {
  197. return null;
  198. }
  199. }
  200. deleteCurvePoint(curvePointId) {
  201. delete this.vectorData.curvePoints[curvePointId];
  202. }
  203. getCurveLines() {
  204. return this.vectorData.curvelines;
  205. }
  206. getCurveLine(curveLineId) {
  207. if (curveLineId) {
  208. return this.vectorData.curvelines[curveLineId];
  209. } else {
  210. return null;
  211. }
  212. }
  213. deleteCurveLine(curveLineId) {
  214. delete this.vectorData.curvelines[curveLineId];
  215. }
  216. /**
  217. * 对弯路的操作
  218. */
  219. addCurveRoadPoint(curveRoadPoint) {
  220. this.vectorData.curveRoadPoints[curveRoadPoint.vectorId] = curveRoadPoint;
  221. }
  222. getCurveRoadPoints() {
  223. return this.vectorData.curveRoadPoints;
  224. }
  225. getCurveRoadPoint(curveRoadPointId) {
  226. if (curveRoadPointId) {
  227. return this.vectorData.curveRoadPoints[curveRoadPointId];
  228. } else {
  229. return null;
  230. }
  231. }
  232. deleteCurveRoadPoint(curveRoadPointId) {
  233. delete this.vectorData.curveRoadPoints[curveRoadPointId];
  234. }
  235. addCurveRoad(curveRoad) {
  236. this.vectorData.curveRoads[curveRoad.vectorId] = curveRoad;
  237. }
  238. getCurveRoads() {
  239. return this.vectorData.curveRoads;
  240. }
  241. getCurveRoad(curveRoadId) {
  242. if (curveRoadId) {
  243. return this.vectorData.curveRoads[curveRoadId];
  244. } else {
  245. return null;
  246. }
  247. }
  248. deleteCurveRoad(curveRoadId) {
  249. let curveRoad = this.getCurveRoad(curveRoadId);
  250. for (let i = 0; i < curveRoad.points.length; ++i) {
  251. this.deleteCurveRoadPoint(curveRoad.points[i].vectorId);
  252. }
  253. this.deleteCurveRoadEdge(curveRoad.leftEdgeId);
  254. this.deleteCurveRoadEdge(curveRoad.rightEdgeId);
  255. delete this.vectorData.curveRoads[curveRoadId];
  256. }
  257. getCurveRoadEdges() {
  258. return this.vectorData.curveRoadEdges;
  259. }
  260. getCurveRoadEdge(curveRoadEdgeId) {
  261. return this.vectorData.curveRoadEdges[curveRoadEdgeId];
  262. }
  263. addCurveRoadEdge(curveRoadEdge) {
  264. this.vectorData.curveRoadEdges[curveRoadEdge.vectorId] = curveRoadEdge;
  265. }
  266. deleteCurveRoadEdge(curveRoadEdgeId) {
  267. delete this.vectorData.curveRoadEdges[curveRoadEdgeId];
  268. }
  269. //直线
  270. getRoads() {
  271. return this.vectorData.roads;
  272. }
  273. getRoad(roadId) {
  274. if (roadId) {
  275. return this.vectorData.roads[roadId];
  276. } else {
  277. return null;
  278. }
  279. }
  280. addRoad(road) {
  281. this.vectorData.roads[road.vectorId] = road;
  282. }
  283. deleteRoad(roadId) {
  284. let road = this.getRoad(roadId);
  285. this.deleteRoadEdge(road.leftEdgeId);
  286. this.deleteRoadEdge(road.rightEdgeId);
  287. delete this.vectorData.roads[roadId];
  288. this.deleteRoadPoint(road.startId, roadId);
  289. this.deleteRoadPoint(road.endId, roadId);
  290. }
  291. /**
  292. * 对端点的操作
  293. */
  294. getRoadPoints() {
  295. return this.vectorData.roadPoints;
  296. }
  297. getRoadPoint(roadPointId) {
  298. if (roadPointId) {
  299. return this.vectorData.roadPoints[roadPointId];
  300. } else {
  301. return null;
  302. }
  303. }
  304. deleteRoadPoint(roadPointId, roadId) {
  305. let roadPoint = this.getRoadPoint(roadPointId);
  306. //有可能先删除墙,导致点没了
  307. if (roadPoint) {
  308. if (Object.keys(roadPoint.parent).length == 0) {
  309. roadPoint = null;
  310. delete this.vectorData.roadPoints[roadPointId];
  311. } else if (Object.keys(roadPoint.parent).length == 1 && !roadId) {
  312. delete this.vectorData.roadPoints[roadPointId];
  313. } else if (
  314. Object.keys(roadPoint.parent).length == 1 &&
  315. roadPoint.parent[roadId]
  316. ) {
  317. delete this.vectorData.roadPoints[roadPointId];
  318. } else if (
  319. Object.keys(roadPoint.parent).length == 1 &&
  320. !roadPoint.parent[roadId]
  321. ) {
  322. return;
  323. } else {
  324. delete roadPoint.parent[roadId];
  325. }
  326. }
  327. }
  328. deleteRoadPoint1(roadPointId) {
  329. delete this.vectorData.roadPoints[roadPointId];
  330. }
  331. addRoadPoint(roadPoint) {
  332. this.vectorData.roadPoints[roadPoint.vectorId] = roadPoint;
  333. }
  334. getRoadEdges() {
  335. return this.vectorData.roadEdges;
  336. }
  337. deleteRoadEdge(edgeId) {
  338. delete this.vectorData.roadEdges[edgeId];
  339. }
  340. getRoadEdge(roadEdgeId) {
  341. return this.vectorData.roadEdges[roadEdgeId];
  342. }
  343. addRoadEdge(roadEdge) {
  344. this.vectorData.roadEdges[roadEdge.vectorId] = roadEdge;
  345. }
  346. /**
  347. * 对控制点的操作
  348. */
  349. getCrossPoints() {
  350. return this.vectorData.crossPoints;
  351. }
  352. getCrossPoint(edgeId1, edgeId2) {
  353. if (this.vectorData.crossPoints[edgeId1 + "-" + edgeId2]) {
  354. return this.vectorData.crossPoints[edgeId1 + "-" + edgeId2];
  355. } else if (this.vectorData.crossPoints[edgeId2 + "-" + edgeId1]) {
  356. return this.vectorData.crossPoints[edgeId2 + "-" + edgeId1];
  357. } else {
  358. return null;
  359. }
  360. }
  361. getCrossPoint2(key) {
  362. return this.vectorData.crossPoints[key];
  363. }
  364. getCrossPoint3(vectorId) {
  365. for (let key in this.vectorData.crossPoints) {
  366. if (this.vectorData.crossPoints[key].vectorId == vectorId) {
  367. return this.vectorData.crossPoints[key];
  368. }
  369. }
  370. }
  371. getCrossPoint4(edgeId) {
  372. for (let key in this.vectorData.crossPoints) {
  373. if (key.indexOf(edgeId + "-") > -1 || key.indexOf("-" + edgeId) > -1) {
  374. return this.vectorData.crossPoints[key];
  375. }
  376. }
  377. }
  378. getCrossPointForEdgeId(edgeId, dir) {
  379. for (let key in this.vectorData.crossPoints) {
  380. const crossPoint = this.vectorData.crossPoints[key];
  381. if (
  382. crossPoint.edgeInfo1.id == edgeId &&
  383. crossPoint.edgeInfo1.dir == dir
  384. ) {
  385. return this.vectorData.crossPoints[key];
  386. } else if (
  387. crossPoint.edgeInfo2.id == edgeId &&
  388. crossPoint.edgeInfo2.dir == dir
  389. ) {
  390. return this.vectorData.crossPoints[key];
  391. }
  392. }
  393. }
  394. addCrossPoint(crossPoint) {
  395. this.vectorData.crossPoints[
  396. crossPoint.edgeInfo1.id + "-" + crossPoint.edgeInfo2.id
  397. ] = crossPoint;
  398. }
  399. deleteCrossPoint(edgeId1, edgeId2) {
  400. let key = edgeId1 + "-" + edgeId2;
  401. if (!this.vectorData.crossPoints[key]) {
  402. key = edgeId2 + "-" + edgeId1;
  403. }
  404. if (this.vectorData.crossPoints[key]) {
  405. delete this.vectorData.crossPoints[key];
  406. }
  407. }
  408. deleteCrossPoint1(vectorId) {
  409. for (let key in this.vectorData.crossPoints) {
  410. if (this.vectorData.crossPoints[key].vectorId == vectorId) {
  411. delete this.vectorData.crossPoints[key];
  412. return;
  413. }
  414. }
  415. }
  416. deleteCrossPointForEdge(edgeId, dir) {
  417. let dCrossPointIds = [];
  418. for (let key in this.vectorData.crossPoints) {
  419. const crossPoint = this.vectorData.crossPoints[key];
  420. if (
  421. crossPoint.edgeInfo1.id == edgeId ||
  422. crossPoint.edgeInfo2.id == edgeId
  423. ) {
  424. dCrossPointIds.push(key);
  425. }
  426. }
  427. for (let i = 0; i < dCrossPointIds.length; ++i) {
  428. const crossPoint = this.vectorData.crossPoints[dCrossPointIds[i]];
  429. if (
  430. crossPoint.edgeInfo1.id == edgeId &&
  431. crossPoint.edgeInfo1.dir == dir
  432. ) {
  433. delete this.vectorData.crossPoints[dCrossPointIds[i]];
  434. } else if (
  435. crossPoint.edgeInfo2.id == edgeId &&
  436. crossPoint.edgeInfo2.dir == dir
  437. ) {
  438. delete this.vectorData.crossPoints[dCrossPointIds[i]];
  439. }
  440. }
  441. }
  442. /**
  443. * 对标注的操作
  444. */
  445. addText(text) {
  446. this.vectorData.texts[text.vectorId] = text;
  447. }
  448. getText(textId) {
  449. return this.vectorData.texts[textId];
  450. }
  451. deleteText(textId) {
  452. delete this.vectorData.texts[textId];
  453. }
  454. getTexts() {
  455. return this.vectorData.texts;
  456. }
  457. /**
  458. * 对放大镜的操作
  459. */
  460. addMagnifier(magnifier) {
  461. this.vectorData.magnifiers[magnifier.vectorId] = magnifier;
  462. }
  463. getMagnifier(magnifierId) {
  464. return this.vectorData.magnifiers[magnifierId];
  465. }
  466. deleteMagnifier(magnifierId) {
  467. delete this.vectorData.magnifiers[magnifierId];
  468. }
  469. getMagnifiers() {
  470. return this.vectorData.magnifiers;
  471. }
  472. //处理从svg转换来的图标
  473. addSVG(SVG) {
  474. this.vectorData.svgs[SVG.vectorId] = SVG;
  475. }
  476. getSVG(SVGId) {
  477. return this.vectorData.svgs[SVGId];
  478. }
  479. deleteSVG(SVGId) {
  480. delete this.vectorData.svgs[SVGId];
  481. }
  482. getSVGs() {
  483. return this.vectorData.svgs;
  484. }
  485. clear() {
  486. //直路
  487. this.vectorData.roadPoints = {};
  488. this.vectorData.roads = {};
  489. this.vectorData.roadEdges = {};
  490. //弯路
  491. this.vectorData.curveRoadPoints = {};
  492. this.vectorData.curveRoads = {};
  493. this.vectorData.curveRoadEdges = {};
  494. //直路交叉的时候,产生的曲线控制点
  495. this.vectorData.crossPoints = {};
  496. //直线,起点和终点都是point的id
  497. this.vectorData.lines = {};
  498. //弯曲线条
  499. this.vectorData.curvelines = {};
  500. //线段(完全或者直线)上的端点
  501. this.vectorData.points = {};
  502. this.vectorData.curvePoints = {};
  503. this.vectorData.circles = {};
  504. //基准点
  505. this.vectorData.basePointIds = [];
  506. this.vectorData.texts = {};
  507. this.vectorData.svgs = {};
  508. this.vectorData.magnifiers = {};
  509. }
  510. setRes(value) {
  511. this.vectorData.res = value;
  512. }
  513. getRes() {
  514. return this.vectorData.res;
  515. }
  516. setScale(value) {
  517. this.vectorData.scale = value;
  518. }
  519. getScale() {
  520. return this.vectorData.scale;
  521. }
  522. }
  523. const dataService = new DataService();
  524. export { dataService };