product_container.js 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. import { product } from "./product.js";
  2. import { product_fullscreen } from "./product_fullscreen.js";
  3. import { login_manager } from "./login_manager.js";
  4. import { delete_product_window } from "./delete_product_window.js";
  5. import { product_editor } from "./product_editor.js";
  6. import { product_rent } from "./product_rent.js";
  7. import { product_give_back } from "./product_give_back.js";
  8. export class product_container {
  9. #target;
  10. #node;
  11. #login;
  12. constructor(target) {
  13. this.#target = new product(target.dump);
  14. this.#node = null;
  15. this.#login = new login_manager().logged_in;
  16. }
  17. get #header() {
  18. const header = document.createElement("div");
  19. header.classList.add("header");
  20. const title = document.createElement("h3");
  21. title.innerText = this.#target.name;
  22. header.appendChild(title);
  23. if (this.#login) {
  24. header.appendChild(this.#manage);
  25. }
  26. return header;
  27. }
  28. get #manage() {
  29. const manage = document.createElement("div");
  30. manage.classList.add("manage");
  31. const all_rents_button = document.createElement("button");
  32. all_rents_button.classList.add("material-icons");
  33. all_rents_button.classList.add("all-rents-button");
  34. all_rents_button.innerText = "list";
  35. manage.appendChild(all_rents_button);
  36. const rent_button = document.createElement("button");
  37. rent_button.classList.add("material-icons");
  38. rent_button.classList.add("rent-button");
  39. rent_button.innerText = "backpack";
  40. manage.appendChild(rent_button);
  41. const give_back_button = document.createElement("button");
  42. give_back_button.classList.add("material-icons");
  43. give_back_button.classList.add("give-back-button");
  44. give_back_button.innerText = "save_alt";
  45. manage.appendChild(give_back_button);
  46. const edit_button = document.createElement("button");
  47. edit_button.classList.add("material-icons");
  48. edit_button.classList.add("edit-button");
  49. edit_button.innerText = "edit";
  50. manage.appendChild(edit_button);
  51. const delete_button = document.createElement("button");
  52. delete_button.classList.add("material-icons");
  53. delete_button.classList.add("delete-button");
  54. delete_button.innerText = "remove_circle_outline";
  55. manage.appendChild(delete_button);
  56. all_rents_button.addEventListener("click", () => {
  57. new product_all_rents(this.#target).show();
  58. });
  59. rent_button.addEventListener("click", () => {
  60. new product_rent(this.#target).show();
  61. });
  62. give_back_button.addEventListener("click", () => {
  63. new product_give_back(this.#target).show();
  64. });
  65. edit_button.addEventListener("click", () => {
  66. new product_editor(this.#target).show();
  67. });
  68. delete_button.addEventListener("click", () => {
  69. new delete_product_window(this.#target).show();
  70. });
  71. return manage;
  72. }
  73. get #description() {
  74. const container = document.createElement("div");
  75. container.classList.add("description");
  76. const description = document.createElement("p");
  77. description.innerText = this.#target.description;
  78. description.classList.add("content");
  79. const author_container = document.createElement("div");
  80. author_container.classList.add("author");
  81. const author = document.createElement("span");
  82. author.innerText = this.#target.author;
  83. const author_icon = document.createElement("span");
  84. author_icon.classList.add("material-icons");
  85. author_icon.innerText = "attribution";
  86. author_container.appendChild(author_icon);
  87. author_container.appendChild(author);
  88. const stock_count = document.createElement("p");
  89. stock_count.classList.add("stock-count");
  90. stock_count.classList.add("material-icons");
  91. if (this.#target.on_stock > 0) {
  92. stock_count.innerText = "check_circle";
  93. stock_count.classList.add("avairable");
  94. } else {
  95. stock_count.innerText = "cancel";
  96. stock_count.classList.add("unavairable");
  97. }
  98. const barcode_container = document.createElement("p");
  99. barcode_container.classList.add("barcode");
  100. const barcode = document.createElement("span");
  101. barcode.innerText = this.#target.barcode;
  102. barcode.classList.add("numbers");
  103. const barcode_icon = document.createElement("span");
  104. barcode_icon.classList.add("material-icons");
  105. barcode_icon.innerText = "qr_code_scanner";
  106. barcode_container.appendChild(barcode_icon);
  107. barcode_container.appendChild(barcode);
  108. container.appendChild(description);
  109. container.appendChild(author_container);
  110. container.appendChild(barcode_container);
  111. container.appendChild(stock_count);
  112. return container;
  113. }
  114. get #cache_bypass() {
  115. return "?cache=" + new String(Math.floor(Math.random() * 100));
  116. }
  117. get #image() {
  118. const image = document.createElement("img");
  119. image.classList.add("image");
  120. image.src = this.#target.thumbnail + this.#cache_bypass;
  121. image.alt = this.#target.name;
  122. image.addEventListener("click", () => {
  123. new product_fullscreen(this.#target).show();
  124. });
  125. return image;
  126. }
  127. get node() {
  128. if (this.#node !== null) {
  129. return this.#node;
  130. }
  131. const bottom_container = document.createElement("div");
  132. bottom_container.classList.add("bottom-container");
  133. bottom_container.appendChild(this.#description);
  134. bottom_container.appendChild(this.#image);
  135. const container = document.createElement("div");
  136. container.classList.add("product");
  137. container.appendChild(this.#header);
  138. container.appendChild(bottom_container);
  139. return this.#node = container;
  140. }
  141. add(target) {
  142. const node = this.node;
  143. node.style.opacity = "0";
  144. node.style.transition = "opacity 0.5s";
  145. target.appendChild(node);
  146. setTimeout(() => {
  147. node.style.opacity = "1";
  148. }, 50);
  149. }
  150. drop() {
  151. const container = this.#node;
  152. if (container === null) {
  153. throw new TypeError("It is not showed yet.");
  154. }
  155. container.style.opacity = "1";
  156. container.style.transition = "opacity 0.5s";
  157. setTimeout(() => {
  158. container.style.opacity = "0";
  159. }, 50);
  160. setTimeout(() => {
  161. this.#node = null;
  162. container.remove();
  163. }, 550);
  164. }
  165. }