FloorplanService.js 15 KB

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