items_list.js 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. import { element } from "./element.js";
  2. import { submission } from "./submission.js";
  3. import { project } from "./project.js";
  4. class items_list {
  5. #container;
  6. #click;
  7. constructor(container, click) {
  8. if (!(container instanceof HTMLElement)) {
  9. throw "Container must be HTML element.";
  10. }
  11. if (typeof(click) !== "function") {
  12. throw "Click must be an function.";
  13. }
  14. this.#click = click;
  15. this.#container = container;
  16. this.#container.classList.add("elements-list");
  17. }
  18. get container() {
  19. return this.#container;
  20. }
  21. #clean() {
  22. while (this.container.firstChild) {
  23. this.container.firstChild.remove();
  24. }
  25. }
  26. #add(content) {
  27. const container = document.createElement("div");
  28. const text = document.createElement("p");
  29. const icon = document.createElement("span");
  30. const name = document.createElement("span");
  31. name.innerText = content.name;
  32. container.className = "single-element";
  33. icon.classList.add("icon");
  34. icon.classList.add("material-symbols-outlined");
  35. container.addEventListener("click", () => {
  36. this.#click(content);
  37. });
  38. if (content instanceof project) {
  39. icon.innerText = "tactic";
  40. } else if (content instanceof element) {
  41. icon.innerText = "stroke_partial";
  42. } else if (content instanceof submission) {
  43. icon.innerText = "book_3";
  44. }
  45. text.appendChild(icon);
  46. text.appendChild(name);
  47. container.appendChild(text);
  48. this.container.appendChild(container);
  49. }
  50. update(list) {
  51. if (!(list instanceof Array)) {
  52. throw "List elements to update must be an array.";
  53. }
  54. this.#clean();
  55. list.forEach(count => {
  56. this.#add(count);
  57. });
  58. }
  59. }
  60. export { items_list };