| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115 |
- import { products_loader } from "./products_loader.js";
- import { product_containers } from "./product_containers.js";
- export class searcher {
- #input;
- #category;
- #manager;
- #result;
- static #instances;
- static #add(instance) {
- if (typeof(searcher.#instances) !== "object") {
- searcher.#instances = new Array();
- }
- searcher.#instances.push(instance);
- }
- static reload() {
- if (typeof(searcher.#instances) !== "object") {
- return ;
- }
- searcher.#instances.forEach(instance => {
- instance.update();
- });
- }
-
- constructor(search_form, manager, result) {
- this.#input = search_form.querySelector("input[type=\"text\"]");
- this.#category = search_form.querySelector("select");
- this.#manager = manager;
- this.#result = result;
- this.#selector_complete();
- search_form.addEventListener("submit", (target) => {
- target.preventDefault();
- this.update();
- });
- searcher.#add(this);
- }
- get categories() {
- return {
- "name": _("name-category"),
- "author": _("author-category"),
- "barcode": _("barcode-category"),
- }
- }
- #selector_complete() {
- const category = this.#category;
- const categories = this.categories;
- Object.keys(categories).forEach(name => {
- const option = document.createElement("option");
- option.value = name;
- option.innerText = categories[name];
-
- category.appendChild(option);
- });
- }
- get #loader() {
- return {
- "name": products_loader.search_name,
- "author": products_loader.search_author,
- "barcode": products_loader.search_barcode,
- }[this.category];
- }
- get category() {
- return this.#category.value;
- }
- get phrase() {
- return this.#input.value.trim();
- }
- get #result_title() {
- return this.#result.innerText;
- }
- set #result_title(target) {
- this.#result.innerText = target;
- }
- async update() {
- if (this.phrase.length === 0) {
- this.show_all();
- return;
- }
- this.#insert(await this.#loader(this.phrase));
- }
- #insert(list) {
- if (list.length === 0) {
- this.#result_title = _("not-found-anything");
- } else {
- this.#result_title = _("browse-our-products");
- }
- this.#manager
- .clean()
- .add_list(list)
- .update();
- }
- async show_all() {
- this.#insert(await products_loader.all())
- }
- }
|