DataService.js 14 KB

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