items_list.js 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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. icon.className = "material-symbols-outlined";
  33. container.className = "single-element";
  34. container.addEventListener("click", () => {
  35. this.#click(content);
  36. });
  37. if (content instanceof project) {
  38. icon.innerText = "tactic";
  39. } else if (content instanceof element) {
  40. icon.innerText = "stroke_partial";
  41. } else if (content instanceof submission) {
  42. icon.innerText = "book_3";
  43. }
  44. text.appendChild(icon);
  45. text.appendChild(name);
  46. container.appendChild(text);
  47. this.container.appendChild(container);
  48. }
  49. update(list) {
  50. if (!(list instanceof Array)) {
  51. throw "List elements to update must be an array.";
  52. }
  53. this.#clean();
  54. list.forEach(count => {
  55. this.#add(count);
  56. });
  57. }
  58. }
  59. export { items_list };