loadCAD.js 3.6 KB

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