DataService.js 14 KB

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