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