DataService.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569
  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 = {
  9. startX: 0,
  10. startY: 0,
  11. step1: 50,
  12. step2: 250,
  13. defalutstep1: 50,
  14. defalutstep2: 250,
  15. display: true,
  16. };
  17. this.vectorData = {
  18. currentId: 0, // 当前可用id
  19. };
  20. }
  21. setCurrentId(id) {
  22. this.vectorData.currentId = id;
  23. }
  24. getCurrentId() {
  25. return this.vectorData.currentId;
  26. }
  27. updateCurrentId() {
  28. ++this.vectorData.currentId;
  29. }
  30. getVectorData() {
  31. return this.vectorData;
  32. }
  33. initVectorData() {
  34. this.vectorData.backgroundImg = null;
  35. //直路
  36. this.vectorData.roadPoints = {};
  37. this.vectorData.roads = {};
  38. this.vectorData.roadEdges = {};
  39. //弯路
  40. this.vectorData.curveRoadPoints = {};
  41. this.vectorData.curveRoads = {};
  42. this.vectorData.curveRoadEdges = {};
  43. //直路交叉的时候,产生的曲线控制点
  44. this.vectorData.crossPoints = {};
  45. //直线,起点和终点都是point的id
  46. this.vectorData.lines = {};
  47. //弯曲线条
  48. this.vectorData.curvelines = {};
  49. //线段(完全或者直线)上的端点
  50. this.vectorData.points = {};
  51. this.vectorData.circles = {};
  52. //基准点
  53. this.vectorData.basePointIds = [];
  54. this.vectorData.texts = {};
  55. this.vectorData.svgs = {};
  56. this.vectorData.magnifiers = {};
  57. }
  58. //网格
  59. getGrid() {
  60. return this.grid;
  61. }
  62. setGridForPan(dx, dy) {
  63. this.grid.startX += dx;
  64. this.grid.startY += dy;
  65. this.grid.step1 =
  66. (this.grid.defalutstep1 * coordinate.zoom) / coordinate.defaultZoom;
  67. this.grid.step2 =
  68. (this.grid.defalutstep2 * coordinate.zoom) / coordinate.defaultZoom;
  69. while (this.grid.startX > 0) {
  70. this.grid.startX -= this.grid.step2;
  71. }
  72. while (this.grid.startY > 0) {
  73. this.grid.startY -= this.grid.step2;
  74. }
  75. }
  76. setGridForZoom(w, h, ratio) {
  77. console.log("setGridForZoom:" + ratio);
  78. this.grid.startX = w / 2 - (ratio * w) / 2;
  79. this.grid.startY = w / 2 - (ratio * h) / 2;
  80. this.grid.step1 = this.grid.defalutstep1 * ratio;
  81. this.grid.step2 = this.grid.defalutstep2 * ratio;
  82. while (this.grid.startX > 0) {
  83. this.grid.startX -= this.grid.step2;
  84. }
  85. while (this.grid.startY > 0) {
  86. this.grid.startY -= this.grid.step2;
  87. }
  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. addCurveLine(curveLine) {
  173. this.vectorData.curvelines[curveLine.vectorId] = curveLine;
  174. }
  175. addCurvePoint(curvePoint) {
  176. this.vectorData.curvePoints[curvePoint.vectorId] = curvePoint;
  177. }
  178. getCurvePoints() {
  179. return this.vectorData.curvePoints;
  180. }
  181. getCurvePoint(curvePointId) {
  182. if (curvePointId) {
  183. return this.vectorData.curvePoints[curvePointId];
  184. } else {
  185. return null;
  186. }
  187. }
  188. deleteCurvePoint(curvePointId) {
  189. delete this.vectorData.curvePoints[curvePointId];
  190. }
  191. getCurveLines() {
  192. return this.vectorData.curvelines;
  193. }
  194. getCurveLine(curveLineId) {
  195. if (curveLineId) {
  196. return this.vectorData.curvelines[curveLineId];
  197. } else {
  198. return null;
  199. }
  200. }
  201. /**
  202. * 对弯路的操作
  203. */
  204. addCurveRoadPoint(curveRoadPoint) {
  205. this.vectorData.curveRoadPoints[curveRoadPoint.vectorId] = curveRoadPoint;
  206. }
  207. getCurveRoadPoints() {
  208. return this.vectorData.curveRoadPoints;
  209. }
  210. getCurveRoadPoint(curveRoadPointId) {
  211. if (curveRoadPointId) {
  212. return this.vectorData.curveRoadPoints[curveRoadPointId];
  213. } else {
  214. return null;
  215. }
  216. }
  217. deleteCurveRoadPoint(curveRoadPointId) {
  218. delete this.vectorData.curveRoadPoints[curveRoadPointId];
  219. }
  220. addCurveRoad(curveRoad) {
  221. this.vectorData.curveRoads[curveRoad.vectorId] = curveRoad;
  222. }
  223. getCurveRoads() {
  224. return this.vectorData.curveRoads;
  225. }
  226. getCurveRoad(curveRoadId) {
  227. if (curveRoadId) {
  228. return this.vectorData.curveRoads[curveRoadId];
  229. } else {
  230. return null;
  231. }
  232. }
  233. deleteCurveRoad(curveRoadId) {
  234. let curveRoad = this.getCurveRoad(curveRoadId);
  235. for (let i = 0; i < curveRoad.points.length; ++i) {
  236. this.deleteCurveRoadPoint(curveRoad.points[i].vectorId);
  237. }
  238. this.deleteCurveRoadEdge(curveRoad.leftEdgeId);
  239. this.deleteCurveRoadEdge(curveRoad.rightEdgeId);
  240. delete this.vectorData.curveRoads[curveRoadId];
  241. }
  242. getCurveRoadEdge(curveRoadEdgeId) {
  243. return this.vectorData.curveRoadEdges[curveRoadEdgeId];
  244. }
  245. addCurveRoadEdge(curveRoadEdge) {
  246. this.vectorData.curveRoadEdges[curveRoadEdge.vectorId] = curveRoadEdge;
  247. }
  248. deleteCurveRoadEdge(curveRoadEdgeId) {
  249. delete this.vectorData.curveRoadEdges[curveRoadEdgeId];
  250. }
  251. //直线
  252. getRoads() {
  253. return this.vectorData.roads;
  254. }
  255. getRoad(roadId) {
  256. if (roadId) {
  257. return this.vectorData.roads[roadId];
  258. } else {
  259. return null;
  260. }
  261. }
  262. addRoad(road) {
  263. this.vectorData.roads[road.vectorId] = road;
  264. }
  265. deleteRoad(roadId) {
  266. let road = this.getRoad(roadId);
  267. this.deleteRoadPoint(road.startId, roadId);
  268. this.deleteRoadPoint(road.endId, roadId);
  269. this.deleteRoadEdge(road.leftEdgeId);
  270. this.deleteRoadEdge(road.rightEdgeId);
  271. delete this.vectorData.roads[roadId];
  272. }
  273. /**
  274. * 对端点的操作
  275. */
  276. getRoadPoints() {
  277. return this.vectorData.roadPoints;
  278. }
  279. getRoadPoint(roadPointId) {
  280. if (roadPointId) {
  281. return this.vectorData.roadPoints[roadPointId];
  282. } else {
  283. return null;
  284. }
  285. }
  286. deleteRoadPoint(roadPointId, roadId) {
  287. let roadPoint = this.getRoadPoint(roadPointId);
  288. //有可能先删除墙,导致点没了
  289. if (roadPoint) {
  290. if (Object.keys(roadPoint.parent).length == 0) {
  291. roadPoint = null;
  292. delete this.vectorData.roadPoints[roadPointId];
  293. } else if (Object.keys(roadPoint.parent).length == 1 && !roadId) {
  294. delete this.vectorData.roadPoints[roadPointId];
  295. } else if (
  296. Object.keys(roadPoint.parent).length == 1 &&
  297. roadPoint.parent[roadId]
  298. ) {
  299. delete this.vectorData.roadPoints[roadPointId];
  300. } else if (
  301. Object.keys(roadPoint.parent).length == 1 &&
  302. !roadPoint.parent[roadId]
  303. ) {
  304. return;
  305. } else {
  306. delete roadPoint.parent[roadId];
  307. }
  308. }
  309. }
  310. deleteRoadPoint1(roadPointId) {
  311. delete this.vectorData.roadPoints[roadPointId];
  312. }
  313. addRoadPoint(roadPoint) {
  314. this.vectorData.roadPoints[roadPoint.vectorId] = roadPoint;
  315. }
  316. getRoadEdges() {
  317. return this.vectorData.roadEdges;
  318. }
  319. deleteRoadEdge(edgeId) {
  320. delete this.vectorData.roadEdges[edgeId];
  321. }
  322. getRoadEdge(roadEdgeId) {
  323. return this.vectorData.roadEdges[roadEdgeId];
  324. }
  325. addRoadEdge(roadEdge) {
  326. this.vectorData.roadEdges[roadEdge.vectorId] = roadEdge;
  327. }
  328. /**
  329. * 对控制点的操作
  330. */
  331. getCrossPoints() {
  332. return this.vectorData.crossPoints;
  333. }
  334. getCrossPoint(edgeId1, edgeId2) {
  335. if (this.vectorData.crossPoints[edgeId1 + "-" + edgeId2]) {
  336. return this.vectorData.crossPoints[edgeId1 + "-" + edgeId2];
  337. } else if (this.vectorData.crossPoints[edgeId2 + "-" + edgeId1]) {
  338. return this.vectorData.crossPoints[edgeId2 + "-" + edgeId1];
  339. } else {
  340. return null;
  341. }
  342. }
  343. getCrossPoint2(key) {
  344. return this.vectorData.crossPoints[key];
  345. }
  346. getCrossPoint3(vectorId) {
  347. for (let key in this.vectorData.crossPoints) {
  348. if (this.vectorData.crossPoints[key].vectorId == vectorId) {
  349. return this.vectorData.crossPoints[key];
  350. }
  351. }
  352. }
  353. getCrossPointForEdgeId(edgeId, dir) {
  354. for (let key in this.vectorData.crossPoints) {
  355. const crossPoint = this.vectorData.crossPoints[key];
  356. if (
  357. crossPoint.edgeInfo1.id == edgeId &&
  358. crossPoint.edgeInfo1.dir == dir
  359. ) {
  360. return this.vectorData.crossPoints[key];
  361. } else if (
  362. crossPoint.edgeInfo2.id == edgeId &&
  363. crossPoint.edgeInfo2.dir == dir
  364. ) {
  365. return this.vectorData.crossPoints[key];
  366. }
  367. }
  368. }
  369. addCrossPoint(crossPoint) {
  370. this.vectorData.crossPoints[
  371. crossPoint.edgeInfo1.id + "-" + crossPoint.edgeInfo2.id
  372. ] = crossPoint;
  373. }
  374. deleteCrossPoint(edgeId1, edgeId2) {
  375. let key = edgeId1 + "-" + edgeId2;
  376. if (!this.vectorData.crossPoints[key]) {
  377. key = edgeId2 + "-" + edgeId1;
  378. }
  379. if (this.vectorData.crossPoints[key]) {
  380. delete this.vectorData.crossPoints[key];
  381. }
  382. }
  383. deleteCrossPoint1(vectorId) {
  384. for (let key in this.vectorData.crossPoints) {
  385. if (this.vectorData.crossPoints[key].vectorId == vectorId) {
  386. delete this.vectorData.crossPoints[key];
  387. return;
  388. }
  389. }
  390. }
  391. deleteCrossPointForEdge(edgeId, dir) {
  392. let dCrossPointIds = [];
  393. for (let key in this.vectorData.crossPoints) {
  394. const crossPoint = this.vectorData.crossPoints[key];
  395. if (
  396. crossPoint.edgeInfo1.id == edgeId ||
  397. crossPoint.edgeInfo2.id == edgeId
  398. ) {
  399. dCrossPointIds.push(key);
  400. }
  401. }
  402. for (let i = 0; i < dCrossPointIds.length; ++i) {
  403. const crossPoint = this.vectorData.crossPoints[dCrossPointIds[i]];
  404. if (
  405. crossPoint.edgeInfo1.id == edgeId &&
  406. crossPoint.edgeInfo1.dir == dir
  407. ) {
  408. delete this.vectorData.crossPoints[dCrossPointIds[i]];
  409. } else if (
  410. crossPoint.edgeInfo2.id == edgeId &&
  411. crossPoint.edgeInfo2.dir == dir
  412. ) {
  413. delete this.vectorData.crossPoints[dCrossPointIds[i]];
  414. }
  415. }
  416. }
  417. /**
  418. * 对标注的操作
  419. */
  420. addText(text) {
  421. this.vectorData.texts[text.vectorId] = text;
  422. }
  423. getText(textId) {
  424. return this.vectorData.texts[textId];
  425. }
  426. deleteText(textId) {
  427. delete this.vectorData.texts[textId];
  428. }
  429. getTexts() {
  430. return this.vectorData.texts;
  431. }
  432. /**
  433. * 对放大镜的操作
  434. */
  435. addMagnifier(magnifier) {
  436. this.vectorData.magnifiers[magnifier.vectorId] = magnifier;
  437. }
  438. getMagnifier(magnifierId) {
  439. return this.vectorData.magnifiers[magnifierId];
  440. }
  441. deleteMagnifier(magnifierId) {
  442. delete this.vectorData.magnifiers[magnifierId];
  443. }
  444. getMagnifiers() {
  445. return this.vectorData.magnifiers;
  446. }
  447. //处理从svg转换来的图标
  448. addSVG(SVG) {
  449. this.vectorData.svgs[SVG.vectorId] = SVG;
  450. }
  451. getSVG(SVGId) {
  452. return this.vectorData.svgs[SVGId];
  453. }
  454. deleteSVG(SVGId) {
  455. delete this.vectorData.svgs[SVGId];
  456. }
  457. getSVGs() {
  458. return this.vectorData.svgs;
  459. }
  460. clear() {
  461. //直路
  462. this.vectorData.roadPoints = {};
  463. this.vectorData.roads = {};
  464. this.vectorData.roadEdges = {};
  465. //弯路
  466. this.vectorData.curveRoadPoints = {};
  467. this.vectorData.curveRoads = {};
  468. this.vectorData.curveRoadEdges = {};
  469. //直路交叉的时候,产生的曲线控制点
  470. this.vectorData.crossPoints = {};
  471. //直线,起点和终点都是point的id
  472. this.vectorData.lines = {};
  473. //弯曲线条
  474. this.vectorData.curvelines = {};
  475. //线段(完全或者直线)上的端点
  476. this.vectorData.points = {};
  477. this.vectorData.circles = {};
  478. //基准点
  479. this.vectorData.basePointIds = [];
  480. this.vectorData.texts = {};
  481. this.vectorData.svgs = {};
  482. this.vectorData.magnifiers = {};
  483. }
  484. }
  485. const dataService = new DataService();
  486. export { dataService };