loadCAD.js 3.1 KB

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