LayerManager.js 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. /* eslint-disable no-undef */
  2. export default class LayerManager {
  3. viewer;
  4. container;
  5. listWrapper;
  6. listContainer;
  7. confirmBtn;
  8. cancelBtn;
  9. headerText;
  10. closeBtn;
  11. layers;
  12. checkboxes;
  13. colorInputs;
  14. constructor(viewer, container = document.body) {
  15. this.init(viewer, container);
  16. }
  17. init(viewer, container) {
  18. this.viewer = viewer;
  19. this.container = viewer.viewerContainer;
  20. this.layers = this.viewer.getLayers();
  21. this.buildPage();
  22. this.addContent();
  23. this.addEventHandlers();
  24. }
  25. show() {
  26. if (this.listContainer?.classList.contains("popup-hide")) {
  27. this.listContainer.classList.remove("popup-hide");
  28. this.updatePage();
  29. }
  30. }
  31. hide() {
  32. if (!this.listContainer?.classList.contains("popup-hide")) {
  33. this.listContainer?.classList.add("popup-hide");
  34. // when click the close button, trigger the layers button's click event
  35. const layersBtn = document.querySelector("#Layers");
  36. if (layersBtn.classList.contains("active")) {
  37. layersBtn.click();
  38. }
  39. }
  40. }
  41. destroy() {
  42. this.closeBtn?.removeEventListener("click", this.hide);
  43. this.checkboxes?.forEach((checkbox, i) => {
  44. checkbox.removeEventListener("change", () => {
  45. this.checkboxHandler(checkbox, i);
  46. });
  47. });
  48. this.listContainer.remove();
  49. }
  50. buildPage() {
  51. this.listContainer = document.createElement("div");
  52. this.listContainer.classList.add("popup-container", "popup-hide");
  53. const header = document.createElement("div");
  54. header.classList.add("popup-header");
  55. this.headerText = document.createElement("span");
  56. this.headerText.innerHTML = "图层管理";
  57. this.closeBtn = document.createElement("span");
  58. this.closeBtn.classList.add("popup-close");
  59. this.closeBtn.innerHTML = "X";
  60. header.appendChild(this.headerText);
  61. header.appendChild(this.closeBtn);
  62. this.listContainer.appendChild(header);
  63. this.listWrapper = document.createElement("div");
  64. this.listWrapper.classList.add("popup-wrapper");
  65. this.listContainer.appendChild(this.listWrapper);
  66. this.container.appendChild(this.listContainer);
  67. this.updateHeaderText();
  68. }
  69. addContent() {
  70. let fragment = `
  71. <div class="popup-list-item">
  72. <input type="checkbox" id="toggleAllLayers" checked class="checkbox"></input>
  73. <span class="popup-layer-color">颜色</span>
  74. <span class="popup-layer-name">图层名称</span>
  75. </div>
  76. `;
  77. const bAppendModelId = this.layers.length > 1;
  78. for (let i = 0; i < this.layers.length; ++i) {
  79. const layers = this.layers[i].layers;
  80. let layerNames = Object.keys(layers);
  81. layerNames = layerNames.sort();
  82. for (const layerName of layerNames) {
  83. const dxfLayer = layers[layerName];
  84. // add "<modelId>" as layer name prefix when there is more than one models
  85. const tmpLayerName = bAppendModelId ? `&lt;${this.layers[i].modelId}&gt; ${layerName}` : layerName;
  86. const color = convertDecimalToHex(dxfLayer.color);
  87. const listItem = this.generateListItem(tmpLayerName, dxfLayer.visible, color);
  88. fragment += listItem;
  89. }
  90. }
  91. if (this.listWrapper) {
  92. this.listWrapper.innerHTML = fragment;
  93. }
  94. // add checkboxes events
  95. const checkboxEles = this.listContainer.querySelectorAll("input[type=checkbox]");
  96. this.checkboxes = [].slice.call(checkboxEles, 0);
  97. const colorInputsEles = this.listContainer.querySelectorAll("input[type=color]");
  98. this.colorInputs = [].slice.call(colorInputsEles, 0);
  99. // input[type=checkbox]
  100. this.checkboxes.forEach((checkbox, i) => {
  101. checkbox.addEventListener("change", () => {
  102. this.checkboxHandler(checkbox, i);
  103. });
  104. });
  105. }
  106. generateListItem(layer, visible, color) {
  107. const listItem = `
  108. <div class="popup-list-item">
  109. <input type="checkbox" value="${layer}" ${visible ? "checked" : ""} class="checkbox">
  110. <div class="popup-color" style="background-color: ${color}"></div>
  111. <span class="popup-layer-name">${layer}</span>
  112. </div>
  113. `;
  114. return listItem;
  115. }
  116. addEventHandlers() {
  117. this.closeBtn?.addEventListener("click", this.hide.bind(this));
  118. // const layersBtn = document.querySelector("#Layers");
  119. // layersBtn &&
  120. // layersBtn.addEventListener("click", () => {
  121. // if (layersBtn.classList.contains("active")) {
  122. // if (!this.viewer.layerManager) {
  123. // this.viewer.layerManager = new LayerManager(this.viewer);
  124. // } else {
  125. // this.viewer.layerManager.show();
  126. // }
  127. // } else {
  128. // this.hide();
  129. // }
  130. // });
  131. }
  132. checkboxHandler(checkbox, index) {
  133. if (checkbox.id === "toggleAllLayers") {
  134. for (let i = 0; i < this.layers.length; ++i) {
  135. const modelId = this.layers[i].modelId;
  136. const layers = this.layers[i].layers;
  137. Object.keys(layers).forEach((layerName) => {
  138. this.viewer.setLayerVisibility(layerName, checkbox.checked, modelId);
  139. });
  140. }
  141. this.checkboxes?.forEach((cb) => (cb.checked = checkbox.checked));
  142. return;
  143. }
  144. let modelId = "";
  145. let layerName = checkbox.value;
  146. let layerHandle = undefined;
  147. if (index === this.checkboxes.length - 1) {
  148. layerHandle = Object.values(this.layers.at(-1).layers)[0].handle;
  149. }
  150. const idx = layerName.indexOf(">");
  151. if (idx !== -1) {
  152. modelId = layerName.slice(1, idx);
  153. layerName = layerName.slice(idx + 2);
  154. }
  155. if (!modelId) {
  156. modelId = this.layers[0].modelId;
  157. }
  158. this.viewer.setLayerVisibility(layerName, checkbox.checked, modelId);
  159. }
  160. updatePage() {
  161. const layers = this.viewer.getLayers();
  162. // if layers.length is the same, there is no change to loaded models
  163. if (layers.length === this.layers.length) {
  164. return;
  165. }
  166. this.layers = layers;
  167. this.addContent();
  168. this.updateHeaderText();
  169. }
  170. updateHeaderText() {
  171. let layerCount = 0;
  172. for (let i = 0; i < this.layers.length; ++i) {
  173. const layers = this.layers[i].layers;
  174. layerCount += Object.keys(layers).length;
  175. }
  176. this.headerText.innerHTML = `图层管理 (共 ${layerCount} 个图层)`;
  177. }
  178. }
  179. function convertDecimalToHex(decimal) {
  180. if (decimal === undefined) {
  181. return "#ffffff";
  182. }
  183. const hex = decimal.toString(16);
  184. return `#${hex.padStart(6, "0")}`;
  185. }