items_list.js 1.8 KB

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