DataService.js 13 KB

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