DataService.js 14 KB

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