DxfSettingsPanel.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. export default class DxfSettingsPanel {
  2. viewer;
  3. parentContainer;
  4. panel; // settings panel
  5. closeBtn;
  6. colorInputs;
  7. /* eslint-disable no-undef */
  8. constructor(viewer, parentContainer = document.body) {
  9. this.init(viewer, parentContainer);
  10. }
  11. init(viewer, parentContainer) {
  12. this.viewer = viewer;
  13. this.parentContainer = viewer.viewerContainer;
  14. this.buildPage();
  15. this.addEventHandlers();
  16. }
  17. show() {
  18. if (this.panel?.classList.contains("panel-hide")) {
  19. this.panel.classList.remove("panel-hide");
  20. }
  21. }
  22. hide() {
  23. if (!this.panel?.classList.contains("panel-hide")) {
  24. this.panel?.classList.add("panel-hide");
  25. // when click the close button, trigger the layers button's click event
  26. const layersBtn = document.querySelector("#Settings");
  27. if (layersBtn.classList.contains("active")) {
  28. layersBtn.click();
  29. }
  30. }
  31. }
  32. destroy() {
  33. this.closeBtn?.removeEventListener("click", this.hide);
  34. this.checkboxes?.forEach((checkbox, i) => {
  35. checkbox.removeEventListener("change", () => {
  36. this.checkboxHandler(checkbox, i);
  37. });
  38. });
  39. this.panel.remove();
  40. }
  41. buildPage() {
  42. this.panel = document.createElement("div");
  43. this.panel.classList.add("panel-container", "panel-hide");
  44. const header = document.createElement("div");
  45. header.innerText = "设置";
  46. header.classList.add("panel-header");
  47. this.panel.appendChild(header);
  48. this.closeBtn = document.createElement("span");
  49. this.closeBtn.classList.add("panel-close-btn");
  50. this.closeBtn.innerHTML = "X";
  51. header.appendChild(this.closeBtn);
  52. const bgColors = [
  53. { name: "默认", rgb: [0.082, 0.11, 0.117], color: "#212830" },
  54. { name: "黑色", rgb: [0, 0, 0], color: "#000000" },
  55. { name: "白色", rgb: [1, 1, 1], color: "#ffffff" },
  56. { name: "灰色", rgb: [0.6, 0.6, 0.6], color: "#888888" },
  57. ];
  58. let colorHtml = "";
  59. bgColors.forEach((item) => {
  60. colorHtml += `
  61. <div>${item.name}
  62. <input type="button" class="color-input" style="background-color: ${item.color}" value=${item.color}></input>
  63. </div>
  64. `;
  65. });
  66. const bgColorDiv = document.createElement("div");
  67. bgColorDiv.classList.add("panel-body");
  68. bgColorDiv.innerHTML = `
  69. <table>
  70. <tr>
  71. <td style="width: 40%">背景色</td>
  72. <td>${colorHtml}</td>
  73. </tr>
  74. </table>
  75. `;
  76. this.panel.appendChild(bgColorDiv);
  77. this.parentContainer.appendChild(this.panel);
  78. }
  79. addEventHandlers() {
  80. this.closeBtn?.addEventListener("click", this.hide.bind(this));
  81. const bgColorInputs = document.querySelectorAll("input[type=button]");
  82. this.colorInputs = [].slice.call(bgColorInputs, 0);
  83. this.colorInputs.forEach((input) => {
  84. input.addEventListener("click", () => {
  85. const rgb = this.convertHexToRGB(input.value);
  86. this.setBackgroundColor(rgb);
  87. });
  88. // input.onclick = () => {
  89. // const rgb = this.convertHexToRGB(input.value);
  90. // this.setBackgroundColor(rgb);
  91. // };
  92. });
  93. // const settingsToolbarBtn = document.querySelector("#Settings");
  94. // settingsToolbarBtn && settingsToolbarBtn.addEventListener("click", () => {
  95. // if (settingsToolbarBtn.classList.contains("active")) {
  96. // if (!window.dxfSettingsPanel) {
  97. // window.dxfSettingsPanel = new DxfSettingsPanel(this.viewer);
  98. // } else {
  99. // window.dxfSettingsPanel.show();
  100. // }
  101. // } else {
  102. // this.hide();
  103. // }
  104. // });
  105. }
  106. // convert "#rrggbb" to { r, g, b }
  107. convertHexToRGB(hex) {
  108. const color = {};
  109. hex = `0x${hex.slice(1)}`;
  110. color.r = ((hex >> 16) & 255) / 255;
  111. color.g = ((hex >> 8) & 255) / 255;
  112. color.b = (hex & 255) / 255;
  113. return color;
  114. }
  115. setBackgroundColor(rgb) {
  116. this.viewer.setBackgroundColor(rgb.r, rgb.g, rgb.b);
  117. }
  118. }