product_containers.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. import { product } from "./product.js";
  2. import { product_container } from "./product_container.js";
  3. export class product_containers {
  4. #content;
  5. #where;
  6. #inserted;
  7. #counter;
  8. #count;
  9. #next_button;
  10. #previous_button;
  11. constructor(where) {
  12. this.#counter = 0;
  13. this.#count = 24;
  14. this.#content = new Array();
  15. this.#inserted = new Array();
  16. this.#where = document.createElement("div");
  17. this.#where.classList.add("products-list");
  18. this.#next_button = document.createElement("input");
  19. this.#next_button.type = "button";
  20. this.#next_button.value = _("show-next");
  21. this.#previous_button = document.createElement("input");
  22. this.#previous_button.type = "button";
  23. this.#previous_button.value = _("show-previous");
  24. this.#next_button.addEventListener("click", () => {
  25. this.#next();
  26. });
  27. this.#previous_button.addEventListener("click", () => {
  28. this.#previous();
  29. });
  30. where.appendChild(this.#previous_button);
  31. where.appendChild(this.#where);
  32. where.appendChild(this.#next_button);
  33. }
  34. add_list(target) {
  35. target.forEach(count => {
  36. this.add(count);
  37. });
  38. return this;
  39. }
  40. add(target) {
  41. const current = new product_container(target);
  42. this.#content.push(current);
  43. return this;
  44. }
  45. clean() {
  46. this.#counter = 0;
  47. this.#content = new Array();
  48. return this;
  49. }
  50. #update_buttons() {
  51. if (this.previous_avairable) {
  52. this.#previous_button.style.opacity = "1";
  53. } else {
  54. this.#previous_button.style.opacity = "0";
  55. }
  56. if (this.next_avairable) {
  57. this.#next_button.style.opacity = "1";
  58. } else {
  59. this.#next_button.style.opacity = "0";
  60. }
  61. }
  62. update() {
  63. this.#update_buttons();
  64. this.#hide();
  65. setTimeout(() => {
  66. const first = this.#counter;
  67. const last = this.#counter + this.#count;
  68. const current = this.#content.slice(first, last);
  69. current.forEach(count => {
  70. this.#show_sigle(count);
  71. });
  72. }, 500);
  73. return this;
  74. }
  75. get next_avairable() {
  76. return (this.#counter + this.#count) <= this.#content.length;
  77. }
  78. get previous_avairable() {
  79. return this.#counter >= this.#count;
  80. }
  81. #next() {
  82. if (!this.next_avairable) return;
  83. this.#counter += this.#count;
  84. this.update();
  85. }
  86. #previous() {
  87. if (!this.previous_avairable) return;
  88. this.#counter -= this.#count;
  89. this.#counter = this.#counter < 0 ? 0 : this.#counter;
  90. this.update();
  91. }
  92. #show_sigle(target) {
  93. this.#inserted.push(target);
  94. target.add(this.#where);
  95. }
  96. #hide() {
  97. this.#inserted.forEach(count => {
  98. count.drop();
  99. });
  100. this.#inserted = new Array();
  101. return this;
  102. }
  103. }