| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109 |
- import { database } from "./database.js";
- import { lang } from "./lang.js";
- import { items_list } from "./items_list.js";
- class search {
- #container;
- #hint;
- #input;
- #last_content;
- #store;
- #list;
-
- constructor(store) {
- if (!(store instanceof database)) {
- throw "Store must be instance of database";
- }
- this.#last_content = "";
- this.#store = store;
- this.#container = document.createElement("div");
- this.#container.className = "search";
- this.#hint = document.createElement("div");
- this.#hint.className = "hint";
- this.#input = document.createElement("input");
- this.#input.type = "text";
- this.#input.placeholder = lang.search.type;
- const submit = document.createElement("input");
- submit.type = "submit";
- submit.value = lang.search.find;
- const form = document.createElement("form");
-
- this.#list = new items_list(this.#hint, this.click);
- form.addEventListener("submit", (action) => {
- action.preventDefault();
- this.find();
- });
- this.#input.addEventListener("keypress", () => {
- this.refresh();
- });
- this.#input.addEventListener("keyup", () => {
- this.refresh();
- });
- this.#input.addEventListener("change", () => {
- this.refresh();
- });
- form.appendChild(this.#input);
- form.appendChild(submit);
- this.#container.appendChild(form);
- this.#container.appendChild(this.#hint);
- }
- click(item) {
- console.log(item.name);
- }
- get content() {
- return this.#input.value.trim();
- }
- get changed() {
- if (this.content === this.#last_content) {
- return false;
- }
- this.#last_content = this.content;
-
- return true;
- }
- get results() {
- if (this.content.length === 0) {
- return [];
- }
- return this.#store.search(this.content);
- }
- find() {
- if (!this.changed) {
- return;
- }
-
- this.#list.update(this.results);
- }
- refresh() {
- if (!this.changed) {
- return;
- }
-
- this.#list.update(this.results);
- }
- get ui() {
- return this.#container;
- }
- }
- export { search };
|