product_container.js 6.9 KB

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