loadCAD.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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: -310px;
  34. top: 110px;
  35. width: 300px;
  36. height: 200px;
  37. }
  38. .cad > div {
  39. width: 100%;
  40. height: 100%;
  41. }
  42. @media only screen and (max-width: 600px) {
  43. .cad {
  44. top: 138px;
  45. width: 210px;
  46. height: 140px;
  47. }
  48. }
  49. `
  50. document.documentElement.appendChild(style)
  51. $('body').append($layer)
  52. $parent.appendChild(style)
  53. $parent.appendChild($layer)
  54. return $layer
  55. }
  56. function setStyle(signColor, borderColor, borderWidth) {
  57. cad.setDefaultPointStyle({
  58. fillColor: "rgba(0,0,0,0)",
  59. storkeColor: "rgba(0,0,0,0)"
  60. });
  61. console.log(borderWidth)
  62. cad.setDefaultLineStyle({
  63. width: borderWidth,
  64. color: borderColor
  65. });
  66. cad.setDefaultSignStyle({
  67. color: signColor
  68. })
  69. }
  70. return function(floor, $parent, signColor, borderColor, borderWidth) {
  71. if (initFloor) {
  72. console.log('cache')
  73. return setStyle(signColor, borderColor, borderWidth)
  74. }
  75. console.log('load')
  76. initFloor = floor
  77. loadScript(function() {
  78. let $layer = loadDOM($parent)
  79. $layer.style.visibility = 'hidden'
  80. window.cad = structureCAD({
  81. data: {
  82. block: [],
  83. column: [],
  84. door: [],
  85. hole: [],
  86. segment: [],
  87. "vertex-xy": [],
  88. "vertex-z": [],
  89. },
  90. layer: $layer.querySelector('#cad'),
  91. edit: false
  92. });
  93. setStyle(signColor, borderColor, borderWidth)
  94. cad.hideDire()
  95. cad.hideGauge()
  96. console.log('loadData')
  97. cad.loadData(initFloor);
  98. if (point && dire) {
  99. window.cad.setSign(point, dire)
  100. }
  101. $layer.style.visibility = 'visible'
  102. })
  103. }
  104. })();
  105. $.ajax({
  106. url: g_Prefix + "data/" + window.number + "/someData.json" + "?" + Date.now(),
  107. method: 'GET',
  108. success(data) {
  109. if (!data.showCad) return
  110. $.ajax({
  111. url: '//super.4dage.com/data/'+ window.number +'/floor.json',
  112. method: 'GET',
  113. success(res) {
  114. grendCAD(res, document.querySelector('body'), data.cadSignColor, data.cadBorderColor, data.cadBorderWidth)
  115. }
  116. })
  117. }
  118. })