From 65bf4ddb2cc7980aacf5e3d458865290dca48ea1 Mon Sep 17 00:00:00 2001 From: Shweelan Samson Date: Mon, 24 Jun 2019 14:39:26 -0400 Subject: [PATCH] Add filters help --- ui/autocomplete-compnent.ts | 11 ++++++----- ui/filter-component.ts | 9 ++++++++- ui/smart-query.ts | 32 ++++++++++++++++++++++++++++++++ 3 files changed, 46 insertions(+), 6 deletions(-) diff --git a/ui/autocomplete-compnent.ts b/ui/autocomplete-compnent.ts index a33b305..568fdf7 100644 --- a/ui/autocomplete-compnent.ts +++ b/ui/autocomplete-compnent.ts @@ -20,7 +20,7 @@ export default class Autocomplete { private callback: ( value: string, - callback: (suggestions: string[]) => void + callback: (suggestions: { value: string; tip?: string }[]) => void ) => void; private element: HTMLInputElement; private hideTimeout: NodeJS.Timeout; @@ -137,25 +137,26 @@ export default class Autocomplete { this.selection = ((this.selection % suggestions.length) + suggestions.length) % suggestions.length; - this.default = suggestions[this.selection]; + this.default = suggestions[this.selection].value; } else { - this.default = suggestions[0]; + this.default = suggestions[0].value; } let selectedElement; for (const [idx, suggestion] of suggestions.entries()) { const e = document.createElement("div"); + if (suggestion.tip) e.title = suggestion.tip; e.classList.add("suggestion"); if (idx === this.selection) { e.classList.add("selected"); selectedElement = e; } - const t = document.createTextNode(suggestion); + const t = document.createTextNode(suggestion.value); e.appendChild(t); e.addEventListener("mousedown", ev => { ev.preventDefault(); - el.value = suggestion; + el.value = suggestion.value; if (this.element === el) this.update(); }); this.container.appendChild(e); diff --git a/ui/filter-component.ts b/ui/filter-component.ts index dede1b0..1bc76d2 100644 --- a/ui/filter-component.ts +++ b/ui/filter-component.ts @@ -29,7 +29,14 @@ const getAutocomplete = memoize(resource => { const labels = smartQuery.getLabels(resource); const autocomplete = new Autocomplete("autocomplete", (txt, cb) => { txt = txt.toLowerCase(); - cb(labels.filter(s => s.toLowerCase().includes(txt)).map(s => `${s}: `)); + cb( + labels + .filter(s => s.toLowerCase().includes(txt)) + .map(s => ({ + value: `${s}: `, + tip: smartQuery.getTip(resource, s) + })) + ); }); return autocomplete; }); diff --git a/ui/smart-query.ts b/ui/smart-query.ts index 7a4a56a..7716763 100644 --- a/ui/smart-query.ts +++ b/ui/smart-query.ts @@ -132,6 +132,38 @@ function queryMac(param, value): Expression { ]; } +export function getTip(resource, label): string { + let tip; + if (resources[resource] && resources[resource][label]) { + const param = resources[resource][label]; + const types = + resource === "devices" ? param["type"] : param["type"].split(","); + + const tips = []; + for (const type of types) { + switch (type.trim()) { + case "string": + tips.push("case insensitive string pattern"); + break; + case "number": + tips.push("numeric value"); + break; + case "timestamp": + tips.push( + "Unix timestamp or string in the form YYYY-MM-DDTHH:mm:ss.sssZ" + ); + break; + case "mac": + tips.push("partial case insensitive MAC address"); + break; + } + } + + if (tips.length) tip = `${label}: ${tips.join(", ")}`; + } + return tip; +} + export function unpack(resource, label, value): Expression { if (!resources[resource]) return null; const type = resources[resource][label].type;