loadCAD.js 2.9 KB

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