class SmartInput extends HTMLElement { #data = null; #rootdoc = null; #delay = 300; #value = null; #timeoutid = null; #input = null; #list = null; #label = null; constructor(){ super() this.#rootdoc = this.attachShadow({ "mode" : "open"}) this.render() } connectedCallback(){} set data(data){ this.#data = data; this.render() } render(){ this.#rootdoc.innerHTML = `
` this.#list = this.#rootdoc.querySelector("ul") this.#input = this.#rootdoc.querySelector("input") this.#label = this.#rootdoc.querySelector("small#restriction-enzyme-feedback") this.#rootdoc.addEventListener("keydown", ev=>{ if(ev.code === "Escape"){ this.#list.classList.add("d-none") } }) this.#input.addEventListener("input", ev=>{ this.#value = ev.target.value this.#label.textContent = "" if(this.#timeoutid != null) window.clearTimeout(this.#timeoutid) this.#timeoutid = window.setTimeout(()=>{ this.renderOptions() }, this.#delay) }) this.#input.addEventListener("keypress", ev=>{ if(ev.code === "Enter"){ this.#list.classList.add("d-none") document.querySelector("igene-data-display").updateEnzyme(this.#input.value) } }) this.#list.addEventListener("click", ev=>{ if(ev.target.classList.contains("list-group-item")){ this.#input.value = ev.target.getAttribute("data-sequence") this.#label.textContent = ev.target.getAttribute("data-enzyme") this.#list.classList.add("d-none") document.querySelector("igene-data-display").updateEnzyme(this.#input.value) } }) } renderOptions(){ if(this.#value == null || this.#value === ""){ this.#list.classList.add("d-none") return } const options = this.#data.filter(d=>{ return d.name.startsWith(this.#value) || d.sequence.startsWith(this.#value) }).map(o=>{ return `
  • ${o.name}${o.sequence}
  • ` }).join("") this.#list.innerHTML = options this.#list.classList.remove("d-none") } } window.customElements.define('nw-smart-input', SmartInput);