FloorplanService.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536
  1. import { floorplanData } from '../FloorplanData'
  2. import { coordinate } from '../Coordinate.js'
  3. import Constant from '../Constant'
  4. import { getUrlSrc } from "@/store/case";
  5. import Title from '../Geometry/Title.js'
  6. import BgImage from '../Geometry/BgImage.js'
  7. import Compass from '../Geometry/Compass.js'
  8. export class FloorplanService {
  9. constructor() {
  10. this.currentId = 0 // 当前可用id
  11. this.currentFloor = 0 // 当前楼层,第一层是0
  12. this.angle = 0 //旋转角度
  13. }
  14. setCurrentId(id) {
  15. this.currentId = id
  16. }
  17. getCurrentId() {
  18. return this.currentId
  19. }
  20. updateCurrentId() {
  21. ++this.currentId
  22. }
  23. setCurrentFloor(floor) {
  24. if (floorplanData.floors.length == 1) {
  25. this.currentFloor = 0
  26. } else {
  27. this.currentFloor = floor
  28. }
  29. }
  30. getCurrentFloor() {
  31. return this.currentFloor
  32. }
  33. getFloorNum() {
  34. return floorplanData.floors.length
  35. }
  36. initFloor(floorNum) {
  37. floorplanData.initFloor(floorNum)
  38. }
  39. getFloors() {
  40. return floorplanData.floors
  41. }
  42. getPoint(pointId, floor) {
  43. if (floor == null || typeof floor == 'undefined') {
  44. floor = this.currentFloor
  45. }
  46. return floorplanData.floors[floor].points[pointId]
  47. }
  48. deletePoint(pointId, wallId, floor) {
  49. if (floor == null || typeof floor == 'undefined') {
  50. floor = this.currentFloor
  51. }
  52. let point = this.getPoint(pointId)
  53. //有可能先删除墙,导致点没了
  54. if (point) {
  55. if (Object.keys(point.parent).length == 0) {
  56. point = null
  57. delete floorplanData.floors[floor].points[pointId]
  58. } else if (Object.keys(point.parent).length == 1 && !wallId) {
  59. delete floorplanData.floors[floor].points[pointId]
  60. } else if (Object.keys(point.parent).length == 1 && point.parent[wallId]) {
  61. delete floorplanData.floors[floor].points[pointId]
  62. } else if (Object.keys(point.parent).length == 1 && !point.parent[wallId]) {
  63. return
  64. } else {
  65. delete point.parent[wallId]
  66. }
  67. }
  68. }
  69. getWall(wallId, floor) {
  70. if (floor == null || typeof floor == 'undefined') {
  71. floor = this.currentFloor
  72. }
  73. return floorplanData.floors[floor].walls[wallId]
  74. }
  75. deleteWall(wallId, floor) {
  76. if (floor == null || typeof floor == 'undefined') {
  77. floor = this.currentFloor
  78. }
  79. let wall = this.getWall(wallId, floor)
  80. this.deletePoint(wall.start, wallId, floor)
  81. this.deletePoint(wall.end, wallId, floor)
  82. delete floorplanData.floors[floor].walls[wallId]
  83. }
  84. getAngle() {
  85. return this.angle
  86. }
  87. setAngle(angle) {
  88. this.angle = angle
  89. }
  90. getFloorData(floor) {
  91. if (floor == null || typeof floor == 'undefined') {
  92. floor = this.currentFloor
  93. }
  94. return floorplanData.floors[floor]
  95. }
  96. getWalls(floor) {
  97. if (floor == null || typeof floor == 'undefined') {
  98. floor = this.currentFloor
  99. }
  100. return floorplanData.floors[floor].walls
  101. }
  102. getPoints(floor) {
  103. if (floor == null || typeof floor == 'undefined') {
  104. floor = this.currentFloor
  105. }
  106. return floorplanData.floors[floor].points
  107. }
  108. addWall(wall, floor) {
  109. if (floor == null || typeof floor == 'undefined') {
  110. floor = this.currentFloor
  111. }
  112. floorplanData.floors[floor].walls[wall.vectorId] = wall
  113. }
  114. addPoint(point, floor) {
  115. if (floor == null || typeof floor == 'undefined') {
  116. floor = this.currentFloor
  117. }
  118. floorplanData.floors[floor].points[point.vectorId] = point
  119. }
  120. addRectangle(rectangle,floor){
  121. if (floor == null || typeof floor == 'undefined') {
  122. floor = this.currentFloor
  123. }
  124. floorplanData.floors[floor].rectangles[rectangle.vectorId] = rectangle
  125. }
  126. getRectangle(rectangleId, floor) {
  127. if (floor == null || typeof floor == 'undefined') {
  128. floor = this.currentFloor
  129. }
  130. return floorplanData.floors[floor].rectangles[rectangleId]
  131. }
  132. deleteRectangle(rectangleId, floor){
  133. if (floor == null || typeof floor == 'undefined') {
  134. floor = this.currentFloor
  135. }
  136. let rectangle = this.getRectangle(rectangleId, floor)
  137. rectangle = null
  138. delete floorplanData.floors[floor].rectangles[rectangleId]
  139. }
  140. addCircle(circle,floor){
  141. if (floor == null || typeof floor == 'undefined') {
  142. floor = this.currentFloor
  143. }
  144. floorplanData.floors[floor].circles[circle.vectorId] = circle
  145. }
  146. getCircle(circleId, floor) {
  147. if (floor == null || typeof floor == 'undefined') {
  148. floor = this.currentFloor
  149. }
  150. return floorplanData.floors[floor].circles[circleId]
  151. }
  152. deleteCircle(circleId, floor){
  153. if (floor == null || typeof floor == 'undefined') {
  154. floor = this.currentFloor
  155. }
  156. let circle = this.getCircle(circleId, floor)
  157. circle = null
  158. delete floorplanData.floors[floor].circles[circleId]
  159. }
  160. addArrow(arrow,floor){
  161. if (floor == null || typeof floor == 'undefined') {
  162. floor = this.currentFloor
  163. }
  164. floorplanData.floors[floor].arrows[arrow.vectorId] = arrow
  165. }
  166. getArrow(arrowId, floor) {
  167. if (floor == null || typeof floor == 'undefined') {
  168. floor = this.currentFloor
  169. }
  170. return floorplanData.floors[floor].arrows[arrowId]
  171. }
  172. deleteArrow(arrowId, floor){
  173. if (floor == null || typeof floor == 'undefined') {
  174. floor = this.currentFloor
  175. }
  176. let arrow = this.getArrow(arrowId, floor)
  177. arrow = null
  178. delete floorplanData.floors[floor].arrows[arrowId]
  179. }
  180. addIcon(icon,floor){
  181. if (floor == null || typeof floor == 'undefined') {
  182. floor = this.currentFloor
  183. }
  184. floorplanData.floors[floor].icons[icon.vectorId] = icon
  185. }
  186. getIcon(iconId, floor) {
  187. if (floor == null || typeof floor == 'undefined') {
  188. floor = this.currentFloor
  189. }
  190. return floorplanData.floors[floor].icons[iconId]
  191. }
  192. deleteIcon(iconId, floor){
  193. if (floor == null || typeof floor == 'undefined') {
  194. floor = this.currentFloor
  195. }
  196. let icon = this.getIcon(iconId, floor)
  197. icon = null
  198. delete floorplanData.floors[floor].icons[iconId]
  199. }
  200. addSign(sign, floor) {
  201. if (floor == null || typeof floor == 'undefined') {
  202. floor = this.currentFloor
  203. }
  204. floorplanData.floors[floor].signs[sign.vectorId] = sign
  205. }
  206. getSign(signId, floor) {
  207. if (floor == null || typeof floor == 'undefined') {
  208. floor = this.currentFloor
  209. }
  210. return floorplanData.floors[floor].signs[signId]
  211. }
  212. deleteSign(signId, floor) {
  213. if (floor == null || typeof floor == 'undefined') {
  214. floor = this.currentFloor
  215. }
  216. let sign = this.getSign(signId, floor)
  217. sign = null
  218. delete floorplanData.floors[floor].signs[signId]
  219. }
  220. addTag(tag, floor) {
  221. if (floor == null || typeof floor == 'undefined') {
  222. floor = this.currentFloor
  223. }
  224. floorplanData.floors[floor].tags[tag.vectorId] = tag
  225. }
  226. getTag(tagId, floor) {
  227. if (floor == null || typeof floor == 'undefined') {
  228. floor = this.currentFloor
  229. }
  230. return floorplanData.floors[floor].tags[tagId]
  231. }
  232. deleteTag(tagId, floor) {
  233. if (floor == null || typeof floor == 'undefined') {
  234. floor = this.currentFloor
  235. }
  236. let tag = this.getTag(tagId, floor)
  237. tag = null
  238. delete floorplanData.floors[floor].tags[tagId]
  239. }
  240. getTags(floor) {
  241. if (floor == null || typeof floor == 'undefined') {
  242. floor = this.currentFloor
  243. }
  244. return floorplanData.floors[floor].tags
  245. }
  246. addCustomImage(customImage, floor) {
  247. if (floor == null || typeof floor == 'undefined') {
  248. floor = this.currentFloor
  249. }
  250. floorplanData.floors[floor].customImages[customImage.vectorId] = customImage
  251. }
  252. getCustomImage(customImageId, floor) {
  253. if (floor == null || typeof floor == 'undefined') {
  254. floor = this.currentFloor
  255. }
  256. return floorplanData.floors[floor].customImages[customImageId]
  257. }
  258. deleteCustomImage(customImageId, floor) {
  259. if (floor == null || typeof floor == 'undefined') {
  260. floor = this.currentFloor
  261. }
  262. let customImage = this.getCustomImage(customImageId, floor)
  263. customImage = null
  264. delete floorplanData.floors[floor].customImages[customImageId]
  265. }
  266. getBgImage(bgImageId,floor) {
  267. if (floor == null || typeof floor == 'undefined') {
  268. floor = this.currentFloor
  269. }
  270. return floorplanData.floors[floor].bgImage
  271. }
  272. deleteBgImage(floor) {
  273. if (floor == null || typeof floor == 'undefined') {
  274. floor = this.currentFloor
  275. }
  276. floorplanData.floors[floor].bgImage = {}
  277. }
  278. addBgImage(bgImage, floor) {
  279. if (floor == null || typeof floor == 'undefined') {
  280. floor = this.currentFloor
  281. }
  282. floorplanData.floors[floor].bgImage = bgImage
  283. }
  284. addTable(table, floor) {
  285. if (floor == null || typeof floor == 'undefined') {
  286. floor = this.currentFloor
  287. }
  288. floorplanData.floors[floor].tables[table.vectorId] = table
  289. }
  290. getTable(tableId, floor) {
  291. if (floor == null || typeof floor == 'undefined') {
  292. floor = this.currentFloor
  293. }
  294. return floorplanData.floors[floor].tables[tableId]
  295. }
  296. deleteTable(tableId, floor) {
  297. if (floor == null || typeof floor == 'undefined') {
  298. floor = this.currentFloor
  299. }
  300. let table = this.getTable(tableId, floor)
  301. table = null
  302. delete floorplanData.floors[floor].tables[tableId]
  303. }
  304. getTables(floor) {
  305. if (floor == null || typeof floor == 'undefined') {
  306. floor = this.currentFloor
  307. }
  308. return floorplanData.floors[floor].tables
  309. }
  310. addCell(cell, floor) {
  311. if (floor == null || typeof floor == 'undefined') {
  312. floor = this.currentFloor
  313. }
  314. floorplanData.floors[floor].cells[cell.vectorId] = cell
  315. }
  316. getCell(cellId, floor) {
  317. if (floor == null || typeof floor == 'undefined') {
  318. floor = this.currentFloor
  319. }
  320. return floorplanData.floors[floor].cells[cellId]
  321. }
  322. deleteCell(cellId, floor) {
  323. if (floor == null || typeof floor == 'undefined') {
  324. floor = this.currentFloor
  325. }
  326. let cell = this.getCell(cellId, floor)
  327. cell = null
  328. delete floorplanData.floors[floor].cells[cellId]
  329. }
  330. getCells(floor) {
  331. if (floor == null || typeof floor == 'undefined') {
  332. floor = this.currentFloor
  333. }
  334. return floorplanData.floors[floor].cells
  335. }
  336. getRectangles(floor) {
  337. if (floor == null || typeof floor == 'undefined') {
  338. floor = this.currentFloor
  339. }
  340. return floorplanData.floors[floor].rectangles
  341. }
  342. getCircles(floor) {
  343. if (floor == null || typeof floor == 'undefined') {
  344. floor = this.currentFloor
  345. }
  346. return floorplanData.floors[floor].circles
  347. }
  348. getArrows(floor) {
  349. if (floor == null || typeof floor == 'undefined') {
  350. floor = this.currentFloor
  351. }
  352. return floorplanData.floors[floor].arrows
  353. }
  354. getIcons(floor) {
  355. if (floor == null || typeof floor == 'undefined') {
  356. floor = this.currentFloor
  357. }
  358. return floorplanData.floors[floor].icons
  359. }
  360. getSigns(floor) {
  361. if (floor == null || typeof floor == 'undefined') {
  362. floor = this.currentFloor
  363. }
  364. return floorplanData.floors[floor].signs
  365. }
  366. getCustomImages(floor){
  367. if (floor == null || typeof floor == 'undefined') {
  368. floor = this.currentFloor
  369. }
  370. return floorplanData.floors[floor].customImages
  371. }
  372. clear() {
  373. if (floorplanData.floors[this.currentFloor]) {
  374. floorplanData.floors[this.currentFloor].points = {}
  375. floorplanData.floors[this.currentFloor].walls = {}
  376. floorplanData.floors[this.currentFloor].rectangles = {}
  377. floorplanData.floors[this.currentFloor].circles = {}
  378. floorplanData.floors[this.currentFloor].tags = {}
  379. floorplanData.floors[this.currentFloor].tables = {}
  380. floorplanData.floors[this.currentFloor].cells = {}
  381. floorplanData.floors[this.currentFloor].signs = {}
  382. floorplanData.floors[this.currentFloor].customImages = {}
  383. floorplanData.floors[this.currentFloor].arrows = {}
  384. floorplanData.floors[this.currentFloor].icons = []
  385. floorplanData.floors[this.currentFloor].bgImage = {}
  386. }
  387. }
  388. deleteFloorData() {
  389. floorplanData.floors = []
  390. }
  391. createTitle(value,vectorId,floor){
  392. if (floor == null || typeof floor == 'undefined') {
  393. floor = this.currentFloor
  394. }
  395. const title = new Title(value,vectorId,floor)
  396. return title
  397. }
  398. addTitle(title, floor) {
  399. if (floor == null || typeof floor == 'undefined') {
  400. floor = this.currentFloor
  401. }
  402. floorplanData.floors[floor].title = title
  403. }
  404. updateTitle(value,floor){
  405. if (floor == null || typeof floor == 'undefined') {
  406. floor = this.currentFloor
  407. }
  408. const title = floorplanData.floors[floor].title
  409. title.setValue(value)
  410. }
  411. getTitle(floor) {
  412. if (floor == null || typeof floor == 'undefined') {
  413. floor = this.currentFloor
  414. }
  415. return floorplanData.floors[floor].title
  416. }
  417. createCompass(value,vectorId,floor){
  418. if (floor == null || typeof floor == 'undefined') {
  419. floor = this.currentFloor
  420. }
  421. const compass = new Compass(value,vectorId,floor)
  422. return compass
  423. }
  424. addCompass(compass, floor) {
  425. if (floor == null || typeof floor == 'undefined') {
  426. floor = this.currentFloor
  427. }
  428. floorplanData.floors[floor].compass = compass
  429. }
  430. updateCompass(value,floor){
  431. if (floor == null || typeof floor == 'undefined') {
  432. floor = this.currentFloor
  433. }
  434. const compass = floorplanData.floors[floor].compass
  435. compass.setAngle(value)
  436. }
  437. // eslint-disable-next-line no-dupe-class-members
  438. getCompass(floor) {
  439. if (floor == null || typeof floor == 'undefined') {
  440. floor = this.currentFloor
  441. }
  442. return floorplanData.floors[floor].compass
  443. }
  444. isUrl(string){
  445. try {
  446. new URL(string);
  447. return true;
  448. } catch (err) {
  449. return false;
  450. }
  451. }
  452. async loadImageData(src){
  453. const imageData = await new Promise((resolve, reject) => {
  454. var img = new Image()
  455. img.src = this.isUrl(src)?src:(getUrlSrc({type: 102}) + '/' + src);
  456. img.crossOrigin=""
  457. img.onload = function () {
  458. resolve(img)
  459. }.bind(this)
  460. })
  461. return imageData
  462. }
  463. }
  464. const floorplanService = new FloorplanService()
  465. export { floorplanService }