loadCAD.js 4.3 KB

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