loadCAD.js 3.6 KB

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