loadCAD.js 3.7 KB

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