loadCAD.js 4.2 KB

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