loadCAD.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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. top: 65px;
  63. width: 100px;
  64. height: 100px;
  65. background: rgba(0, 0, 0, .3);
  66. border-radius: 5px;
  67. g.sign>path {
  68. opacity: 1;
  69. }
  70. }
  71. .img {
  72. width: 90%;
  73. z-index: 30000;
  74. }
  75. }
  76. `
  77. document.documentElement.appendChild(style)
  78. document.documentElement.appendChild($layer)
  79. document.documentElement.appendChild($img)
  80. $parent.appendChild(style)
  81. $parent.appendChild($layer)
  82. $parent.appendChild($img)
  83. return [$layer, $img]
  84. }
  85. function setStyle(signColor, borderColor, borderWidth) {
  86. cad.setDefaultPointStyle({
  87. fillColor: 'rgba(0,0,0,0)',
  88. storkeColor: 'rgba(0,0,0,0)'
  89. })
  90. console.log(borderWidth)
  91. cad.setDefaultLineStyle({
  92. width: borderWidth,
  93. color: borderColor
  94. })
  95. cad.setDefaultSignStyle({
  96. color: signColor
  97. })
  98. }
  99. return function (floor, $parent, signColor, borderColor, borderWidth) {
  100. if (initFloor) {
  101. console.log('cache')
  102. return setStyle(signColor, borderColor, borderWidth)
  103. }
  104. console.log('load')
  105. initFloor = floor
  106. loadScript(function () {
  107. let [$layer, $img] = loadDOM($parent)
  108. // $layer.addEventListener('click', () => {
  109. // console.log(12)
  110. // $img.src = './images/map_2.png'
  111. // $img.style.display = $img.style.display === 'block' ? 'none' : 'block'
  112. // })
  113. $layer.style.visibility = 'hidden'
  114. window.cad = structureCAD({
  115. data: {
  116. block: [],
  117. column: [],
  118. door: [],
  119. hole: [],
  120. segment: [],
  121. 'vertex-xy': [],
  122. 'vertex-z': []
  123. },
  124. layer: $layer.querySelector('#cad'),
  125. edit: false
  126. })
  127. setStyle(signColor, borderColor, borderWidth)
  128. cad.hideDire()
  129. cad.hideGauge()
  130. console.log('loadData')
  131. cad.loadData(initFloor)
  132. if (point && dire) {
  133. window.cad.setSign(point, dire)
  134. }
  135. $layer.style.visibility = 'visible'
  136. })
  137. }
  138. })()
  139. $.ajax({
  140. url: g_Prefix + 'data/' + window.number + '/someData.json' + '?' + Date.now(),
  141. method: 'GET',
  142. success(data) {
  143. if (!data.showCad) return
  144. $.ajax({
  145. url: '//super.4dage.com/data/' + window.number + '/floor.json',
  146. method: 'GET',
  147. success(res) {
  148. grendCAD(res, document.documentElement, data.cadSignColor, data.cadBorderColor, data.cadBorderWidth)
  149. }
  150. })
  151. }
  152. })