import { HTMLWidget } from "@hpcc-js/common"; import * as AutoComplete from "javascript-autocomplete"; import "../src/AutoCompleteText.css"; export class AutoCompleteText extends HTMLWidget { _span; _prompt; _input; _prevMinCharsText; _autoComplete; constructor() { super(); this._tag = "div"; } autoCompleteTextData() { if (this.data().length === 0) return []; const view = this._db.rollupView([this.textColumn(), this.valueColumn()]); return view.entries().map(function (row, idx) { return { idx, text: row.key, value: row.values.length ? row.values[0].key : "", origRow: row.values.length && row.values[0].value.length ? row.values[0].value[0] : [] }; }, this); } enter(domNode, element) { super.enter(domNode, element); this._span = element.append("span"); this._prompt = this._span.append("label") .attr("for", this.id() + "_input") ; this._input = this._span.append("input") .attr("id", this.id() + "_input") .attr("name", this.id() + "_input_name") .attr("type", "text") .attr("placeholder", this.placeholder()) ; } update(domNode, element) { super.update(domNode, element); this._prompt.text(this.label()); this._input.style("width", this.textboxWidth_exists() ? this.textboxWidth() + "px" : null); if (this._prevMinCharsText !== this.minCharsText()) { this._prevMinCharsText = this.minCharsText(); if (this._autoComplete) { this._autoComplete.destroy(); } const context = this; this._autoComplete = new AutoComplete({ selector: "#" + this.id() + "_input", minChars: this.minCharsText(), delay: 150, offsetLeft: 0, offsetTop: 1, source: (term, suggest) => { const field = context._db.fieldByLabel(context.textColumn()); if (field) { term = term.toLowerCase(); const suggestions = context.autoCompleteTextData().filter(function (row) { return row.origRow[field.idx].toLowerCase().indexOf(term) >= 0; }).map(function (row) { return { text: row.origRow[field.idx], rowIdx: row.idx }; }); suggest(suggestions); } }, renderItem: (item, search) => { search = search.replace(/[-\/\\^$*+?.()|[\]{}]/g, "\\$&"); const re = new RegExp("(" + search.split(" ").join("|") + ")", "gi"); return '