loadCAD.js 3.3 KB

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