loadCAD.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  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. let $jiantou = document.createElement('div')
  28. let $jtimg = document.createElement('img')
  29. $jtimg.src="static/images/phone_step_05.png"
  30. $jiantou.id = 'cadimg'
  31. $layer.className = 'cad'
  32. $jiantou.className = 'jiantou'
  33. $outerDiv.className = 'cadouter'
  34. $cad.id = 'cad'
  35. $layer.appendChild($cad)
  36. $jiantou.appendChild($jtimg)
  37. $outerDiv.appendChild($layer)
  38. let wh= 200
  39. let style = document.createElement('style')
  40. style.innerHTML = `
  41. .jiantou{
  42. position: fixed;
  43. width: 40px;
  44. height: 50px;
  45. right: 40px;
  46. background: rgba(0, 0, 0, .6);
  47. top: 46px;
  48. display: flex;
  49. justify-content: center;
  50. align-items: center;
  51. border-radius: 0 5px 5px 0;
  52. cursor: pointer;
  53. }
  54. .jiantou>img{
  55. width:30%;
  56. }
  57. .jiantouactive{
  58. right: 0;
  59. transform: rotate(180deg);
  60. }
  61. .cadouter {
  62. position: absolute;
  63. right: 80px;
  64. top: 46px;
  65. width: ${wh}px;
  66. height: ${wh}px;
  67. background: rgba(0, 0, 0, .6);
  68. border-radius: 5px 0 5px 5px;
  69. overflow: hidden;
  70. }
  71. .cadouteractive{
  72. right: 200%;
  73. }
  74. .cad {
  75. position: absolute;
  76. left: 50%;
  77. top: 50%;
  78. right:unset;
  79. transform: translate(-50%, -50%);
  80. width: ${wh*1.4}px;
  81. height: ${wh*1.4}px;
  82. }
  83. .cad > div {
  84. width: 100%;
  85. height: 100%;
  86. }
  87. @media only screen and (max-width: 600px) {
  88. .cadouter,.jiantou{
  89. display: none!important;
  90. }
  91. .cad {
  92. position: absolute;
  93. left: 16px;
  94. top: 65px;
  95. width: 100px;
  96. height: 100px;
  97. background: rgba(0, 0, 0, .3);
  98. border-radius: 5px;
  99. }
  100. }
  101. `
  102. document.documentElement.appendChild(style)
  103. document.documentElement.appendChild($outerDiv)
  104. document.documentElement.appendChild($jiantou)
  105. $parent.appendChild(style)
  106. $parent.appendChild($outerDiv)
  107. $parent.appendChild($jiantou)
  108. $($jiantou).click(()=>{
  109. $($jiantou).toggleClass('jiantouactive')
  110. $($outerDiv).toggleClass('cadouteractive')
  111. })
  112. return $outerDiv
  113. }
  114. function setStyle(signColor, borderColor, borderWidth) {
  115. cad.setDefaultPointStyle({
  116. fillColor: "rgba(0,0,0,0)",
  117. storkeColor: "rgba(0,0,0,0)"
  118. });
  119. console.log(borderWidth)
  120. cad.setDefaultLineStyle({
  121. width: borderWidth,
  122. color: borderColor
  123. });
  124. cad.setDefaultSignStyle({
  125. color: signColor
  126. })
  127. }
  128. return function(floor, $parent, signColor, borderColor, borderWidth) {
  129. if (initFloor) {
  130. console.log('cache')
  131. return setStyle(signColor, borderColor, borderWidth)
  132. }
  133. console.log('load')
  134. initFloor = floor
  135. loadScript(function() {
  136. let $layer = loadDOM($parent)
  137. $layer.style.visibility = 'hidden'
  138. window.cad = structureCAD({
  139. data: {
  140. block: [],
  141. column: [],
  142. door: [],
  143. hole: [],
  144. segment: [],
  145. "vertex-xy": [],
  146. "vertex-z": [],
  147. },
  148. layer: $layer.querySelector('#cad'),
  149. edit: false
  150. });
  151. setStyle(signColor, borderColor, borderWidth)
  152. cad.hideDire()
  153. cad.hideGauge()
  154. console.log('loadData')
  155. cad.loadData(initFloor);
  156. if (point && dire) {
  157. window.cad.setSign(point, dire)
  158. }
  159. $layer.style.visibility = 'visible'
  160. })
  161. }
  162. })();
  163. $.ajax({
  164. url: g_Prefix + "data/" + window.number + "/someData.json" + "?" + Date.now(),
  165. method: 'GET',
  166. success(data) {
  167. if (!data.showCad) return
  168. $.ajax({
  169. url: '//super.4dage.com/data/'+ window.number +'/floor.json',
  170. method: 'GET',
  171. success(res) {
  172. grendCAD(res, document.documentElement, data.cadSignColor, data.cadBorderColor, data.cadBorderWidth)
  173. }
  174. })
  175. }
  176. })