searcher.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. import { products_loader } from "./products_loader.js";
  2. import { product_containers } from "./product_containers.js";
  3. export class searcher {
  4. #input;
  5. #category;
  6. #manager;
  7. #result;
  8. #counter;
  9. static #instances;
  10. static #add(instance) {
  11. if (typeof(searcher.#instances) !== "object") {
  12. searcher.#instances = new Array();
  13. }
  14. searcher.#instances.push(instance);
  15. }
  16. static reload() {
  17. if (typeof(searcher.#instances) !== "object") {
  18. return ;
  19. }
  20. searcher.#instances.forEach(instance => {
  21. instance.update();
  22. });
  23. }
  24. constructor(search_form, manager, result, counter) {
  25. this.#input = search_form.querySelector("input[type=\"text\"]");
  26. this.#category = search_form.querySelector("select");
  27. this.#manager = manager;
  28. this.#result = result;
  29. this.#counter = counter;
  30. this.#selector_complete();
  31. search_form.addEventListener("submit", (target) => {
  32. target.preventDefault();
  33. this.update();
  34. });
  35. searcher.#add(this);
  36. }
  37. get categories() {
  38. return {
  39. "name": _("name-category"),
  40. "author": _("author-category"),
  41. "barcode": _("barcode-category"),
  42. }
  43. }
  44. #selector_complete() {
  45. const category = this.#category;
  46. const categories = this.categories;
  47. Object.keys(categories).forEach(name => {
  48. const option = document.createElement("option");
  49. option.value = name;
  50. option.innerText = categories[name];
  51. category.appendChild(option);
  52. });
  53. }
  54. get #loader() {
  55. return {
  56. "name": products_loader.search_name,
  57. "author": products_loader.search_author,
  58. "barcode": products_loader.search_barcode,
  59. }[this.category];
  60. }
  61. get category() {
  62. return this.#category.value;
  63. }
  64. get phrase() {
  65. return this.#input.value.trim();
  66. }
  67. get #result_title() {
  68. return this.#result.innerText;
  69. }
  70. set #result_title(target) {
  71. this.#result.innerText = target;
  72. }
  73. async update() {
  74. if (this.phrase.length === 0) {
  75. this.show_all();
  76. return;
  77. }
  78. this.#insert(await this.#loader(this.phrase));
  79. }
  80. set #how_many(target) {
  81. this.#counter.innerText = _("found-count") + target;
  82. }
  83. get #how_many() {
  84. return this.#counter.innerText;
  85. }
  86. #insert(list) {
  87. if (list.length === 0) {
  88. this.#result_title = _("not-found-anything");
  89. } else {
  90. this.#result_title = _("browse-our-products");
  91. }
  92. this.#how_many = list.length;
  93. this.#manager
  94. .clean()
  95. .add_list(list)
  96. .update();
  97. }
  98. async show_all() {
  99. this.#insert(await products_loader.all())
  100. }
  101. }