loadCAD.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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 = 'static/js/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: 80px;
  34. top: 16px;
  35. width: 200px;
  36. height: 165px;
  37. background: rgba(0, 0, 0, .3);
  38. border-radius: 5px;
  39. top: -12px;
  40. right: 12px;
  41. }
  42. .cad > div {
  43. width: 100%;
  44. height: 100%;
  45. }
  46. @media only screen and (max-width: 600px) {
  47. .cad {
  48. position: absolute;
  49. left: 16px;
  50. top: 65px;
  51. width: 100px;
  52. height: 100px;
  53. background: rgba(0, 0, 0, .3);
  54. border-radius: 5px;
  55. }
  56. }
  57. `
  58. document.documentElement.appendChild(style)
  59. document.documentElement.appendChild($layer)
  60. $parent.appendChild(style)
  61. $parent.appendChild($layer)
  62. return $layer
  63. }
  64. function setStyle(signColor, borderColor, borderWidth) {
  65. cad.setDefaultPointStyle({
  66. fillColor: "rgba(0,0,0,0)",
  67. storkeColor: "rgba(0,0,0,0)"
  68. });
  69. console.log(borderWidth)
  70. cad.setDefaultLineStyle({
  71. width: borderWidth,
  72. color: borderColor
  73. });
  74. cad.setDefaultSignStyle({
  75. color: signColor
  76. })
  77. }
  78. return function(floor, $parent, signColor, borderColor, borderWidth) {
  79. if (initFloor) {
  80. console.log('cache')
  81. return setStyle(signColor, borderColor, borderWidth)
  82. }
  83. console.log('load')
  84. initFloor = floor
  85. loadScript(function() {
  86. let $layer = loadDOM($parent)
  87. $layer.style.visibility = 'hidden'
  88. window.cad = structureCAD({
  89. data: {
  90. block: [],
  91. column: [],
  92. door: [],
  93. hole: [],
  94. segment: [],
  95. "vertex-xy": [],
  96. "vertex-z": [],
  97. },
  98. layer: $layer.querySelector('#cad'),
  99. edit: false
  100. });
  101. setStyle(signColor, borderColor, borderWidth)
  102. cad.hideDire()
  103. cad.hideGauge()
  104. console.log('loadData')
  105. cad.loadData(initFloor);
  106. if (point && dire) {
  107. window.cad.setSign(point, dire)
  108. }
  109. $layer.style.visibility = 'visible'
  110. })
  111. }
  112. })();