loadCAD.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. window.grendCAD = (function grendCAD() {
  2. let initFloor
  3. let initScript
  4. let initDOM
  5. let point, dire
  6. window.cad = {
  7. setSign: function (p, d) {
  8. point = p
  9. dire = d
  10. }
  11. }
  12. function loadScript(cb) {
  13. if (initScript) return cb()
  14. let $script = document.createElement('script')
  15. $script.src = '//www.4dmodel.com/CAD/bundle.js'
  16. $script.onload = function () {
  17. initScript = true
  18. cb()
  19. }
  20. document.documentElement.appendChild($script)
  21. }
  22. function loadDOM($parent) {
  23. if (initDOM) return initDOM
  24. let $layer = document.createElement('div')
  25. let $cad = document.createElement('div')
  26. let $img = document.createElement('img')
  27. $layer.className = 'cad'
  28. $img.className = 'img'
  29. $cad.id = 'cad'
  30. $layer.appendChild($cad)
  31. let style = document.createElement('style')
  32. style.innerHTML = `
  33. .cad {
  34. position: absolute;
  35. right: 20px;
  36. top: 16px;
  37. width: 200px;
  38. height: 200px;
  39. background: rgba(0, 0, 0, .3);
  40. border-radius: 5px;
  41. g.sign>path {
  42. opacity: 1;
  43. }
  44. }
  45. .cad > div {
  46. width: 100%;
  47. height: 100%;
  48. }
  49. .img {
  50. display: none;
  51. width: 60%;
  52. object-fit: cover;
  53. position: fixed;
  54. top: 50%;
  55. left: 50%;
  56. transform: translate(-50%, -50%);
  57. transition: opacity 0.3s ease;
  58. }
  59. @media only screen and (max-width: 1000px) {
  60. .cad {
  61. position: absolute;
  62. left: 16px;
  63. top: 65px;
  64. width: 100px;
  65. height: 100px;
  66. background: rgba(0, 0, 0, .3);
  67. border-radius: 5px;
  68. g.sign>path {
  69. opacity: 1;
  70. }
  71. }
  72. .img {
  73. width: 90%;
  74. z-index: 30000;
  75. }
  76. }
  77. `
  78. document.documentElement.appendChild(style)
  79. document.documentElement.appendChild($layer)
  80. document.documentElement.appendChild($img)
  81. $parent.appendChild(style)
  82. $parent.appendChild($layer)
  83. $parent.appendChild($img)
  84. return [$layer, $img]
  85. }
  86. function setStyle(signColor, borderColor, borderWidth) {
  87. cad.setDefaultPointStyle({
  88. fillColor: 'rgba(0,0,0,0)',
  89. storkeColor: 'rgba(0,0,0,0)'
  90. })
  91. console.log(borderWidth)
  92. cad.setDefaultLineStyle({
  93. width: borderWidth,
  94. color: borderColor
  95. })
  96. cad.setDefaultSignStyle({
  97. color: signColor
  98. })
  99. }
  100. return function (floor, $parent, signColor, borderColor, borderWidth) {
  101. if (initFloor) {
  102. console.log('cache')
  103. return setStyle(signColor, borderColor, borderWidth)
  104. }
  105. console.log('load')
  106. initFloor = floor
  107. loadScript(function () {
  108. let [$layer, $img] = loadDOM($parent)
  109. // $layer.addEventListener('click', () => {
  110. // console.log(12)
  111. // $img.src = './images/map_2.png'
  112. // $img.style.display = $img.style.display === 'block' ? 'none' : 'block'
  113. // })
  114. $layer.style.visibility = 'hidden'
  115. window.cad = structureCAD({
  116. data: {
  117. block: [],
  118. column: [],
  119. door: [],
  120. hole: [],
  121. segment: [],
  122. 'vertex-xy': [],
  123. 'vertex-z': []
  124. },
  125. layer: $layer.querySelector('#cad'),
  126. edit: false
  127. })
  128. setStyle(signColor, borderColor, borderWidth)
  129. cad.hideDire()
  130. cad.hideGauge()
  131. console.log('loadData')
  132. cad.loadData(initFloor)
  133. if (point && dire) {
  134. window.cad.setSign(point, dire)
  135. }
  136. $layer.style.visibility = 'visible'
  137. })
  138. }
  139. })()
  140. $.ajax({
  141. url: g_Prefix + 'data/' + window.number + '/someData.json' + '?' + Date.now(),
  142. method: 'GET',
  143. success(data) {
  144. if (!data.showCad) return
  145. $.ajax({
  146. url: '//super.4dage.com/data/' + window.number + '/floor.json',
  147. method: 'GET',
  148. success(res) {
  149. grendCAD(res, document.documentElement, data.cadSignColor, data.cadBorderColor, data.cadBorderWidth)
  150. }
  151. })
  152. }
  153. })