loadCAD.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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. let $name = document.createElement("div");
  27. $name.className = "name";
  28. // $name.textContent = "当前位置:耀光别墅";
  29. $layer.appendChild($name);
  30. $layer.className = "cad";
  31. $cad.id = "cad";
  32. $layer.appendChild($cad);
  33. let style = document.createElement("style");
  34. style.innerHTML = `
  35. .cad {
  36. position: absolute;
  37. right: 20px;
  38. top: 20px;
  39. width: 180px;
  40. height: 180px;
  41. background: rgba(56,56,56,0.56);
  42. pointer-events: none;
  43. }
  44. .name {
  45. width: 100%;
  46. height: 20px !important;
  47. color: white;
  48. font-size: 14px;
  49. text-align:center;
  50. margin-top: 10px;
  51. }
  52. .cad > div {
  53. width: 100%;
  54. height: 100%;
  55. }
  56. @media only screen and (max-width: 1000px) {
  57. .cad {
  58. position: absolute;
  59. left:auto;
  60. transform: translate(0, 0);
  61. right: 10px;
  62. top: 10px;
  63. width: 120px;
  64. height: 120px;
  65. background: rgba(56,56,56,0.56);
  66. border-radius: 5px;
  67. border: 2px solid #D6B970;
  68. }
  69. .name {
  70. font-size: 12px;
  71. }
  72. }
  73. `;
  74. document.documentElement.appendChild(style);
  75. $("body").append($layer);
  76. $parent.appendChild(style);
  77. $parent.appendChild($layer);
  78. return $layer;
  79. }
  80. function setStyle(signColor, borderColor, borderWidth) {
  81. cad.setDefaultPointStyle({
  82. fillColor: "rgba(0,0,0,0)",
  83. storkeColor: "rgba(0,0,0,0)",
  84. });
  85. console.log(borderWidth);
  86. cad.setDefaultLineStyle({
  87. width: borderWidth,
  88. color: borderColor,
  89. });
  90. cad.setDefaultSignStyle({
  91. color: signColor,
  92. });
  93. }
  94. return function (floor, $parent, signColor, borderColor, borderWidth) {
  95. if (initFloor) {
  96. console.log("cache");
  97. return setStyle(signColor, borderColor, borderWidth);
  98. }
  99. console.log("load");
  100. initFloor = floor;
  101. loadScript(function () {
  102. let $layer = loadDOM($parent);
  103. $layer.style.visibility = "hidden";
  104. window.cad = structureCAD({
  105. data: {
  106. block: [],
  107. column: [],
  108. door: [],
  109. hole: [],
  110. segment: [],
  111. "vertex-xy": [],
  112. "vertex-z": [],
  113. },
  114. layer: $layer.querySelector("#cad"),
  115. edit: false,
  116. });
  117. setStyle(signColor, borderColor, borderWidth);
  118. cad.hideDire();
  119. cad.hideGauge();
  120. console.log("loadData");
  121. cad.loadData(initFloor);
  122. if (point && dire) {
  123. window.cad.setSign(point, dire);
  124. }
  125. $layer.style.visibility = "visible";
  126. });
  127. };
  128. })();
  129. $.ajax({
  130. url: g_Prefix + "data/" + window.number + "/someData.json" + "?" + Date.now(),
  131. method: "GET",
  132. success(data) {
  133. if (!data.showCad) return;
  134. $.ajax({
  135. url: "//super.4dage.com/data/" + window.number + "/floor.json",
  136. method: "GET",
  137. success(res) {
  138. grendCAD(
  139. res,
  140. document.querySelector("body"),
  141. data.cadSignColor,
  142. data.cadBorderColor,
  143. data.cadBorderWidth
  144. );
  145. },
  146. });
  147. },
  148. });