From d5b52cb9a32d59403bc2cd01e8e50bd70899345c Mon Sep 17 00:00:00 2001 From: Almar Klein Date: Wed, 8 Nov 2023 15:30:22 +0100 Subject: [PATCH] Preset drowndown filters based on tags in ds (#422) * Preset drowndown filters based on tags in ds * Search also works for tags and ds's, and only with one word * case insensitive --- timetagger/app/dialogs.py | 44 ++++++++++++++++++++++++++++++++------- 1 file changed, 36 insertions(+), 8 deletions(-) diff --git a/timetagger/app/dialogs.py b/timetagger/app/dialogs.py index c208698d..ce2c2440 100644 --- a/timetagger/app/dialogs.py +++ b/timetagger/app/dialogs.py @@ -1203,6 +1203,10 @@ def init(self): suggestions = matches1 suggestions.extend(matches2) + # All suggestions are insert-on-select + for i in range(len(suggestions)): + suggestions[i].push(False) + # Show if suggestions: if show_descriptions: @@ -1226,28 +1230,48 @@ def init(self): def show_suggestions(self, kind=""): suggestions = [] types = [] + _, words = utils.get_tags_and_parts_from_string(self._input.value) + search_word = None + if len(words) == 1: + search_word = words[0].lower() # Collect recent ds's if "descriptions" in kind: types.push("Recent descriptions") + if search_word: + for ds, ds_t2 in self._suggested_ds_recent: + if search_word in ds.lower(): + html = "" + ds + "match" + suggestions.push((ds, html, True)) for ds, ds_t2 in self._suggested_ds_recent: date = days_ago(ds_t2) date = {0: "today", 1: "yesterday"}.get(date, date + " days ago") html = ds + "recent: " + date + "" - suggestions.push((ds, html)) + suggestions.push((ds, html, True)) # Collect presets if "presets" in kind: types.push("Presets") - for preset in self._get_suggested_tags_presets(): + presets = self._get_suggested_tags_presets() + if search_word: + for preset in presets: + if search_word in preset: + html = "" + preset + "match" + suggestions.push((preset, html, True)) + for preset in presets: html = preset + "preset" - suggestions.push((preset, html)) + suggestions.push((preset, html, False)) # Collect tags if "tags" in kind: types.push("Recent tags") + if search_word: + for tag, tag_t2 in self._suggested_tags_recent: + if search_word in tag: + html = "" + tag + "match" + suggestions.push((tag, html, True)) for tag, tag_t2 in self._suggested_tags_recent: date = days_ago(tag_t2) date = {0: "today", 1: "yesterday"}.get(date, date + " days ago") html = tag + "recent: " + date + "" - suggestions.push((tag, html)) + suggestions.push((tag, html, False)) # Show if not types: self.clear() @@ -1315,8 +1339,8 @@ def _show(self, headline, suggestions, show_toggle=True): self._div.appendChild(item) # Add suggestions self._suggested_tags_in_autocomp = [] - for text, html in suggestions: # text is a tag or a preset - self._suggested_tags_in_autocomp.push(text) + for text, html, select_does_replace in suggestions: # text is a tag or a preset + self._suggested_tags_in_autocomp.push((text, select_does_replace)) i = len(self._suggested_tags_in_autocomp) - 1 item = document.createElement("div") item.classList.add("tag-suggestion") @@ -1325,7 +1349,6 @@ def _show(self, headline, suggestions, show_toggle=True): item.setAttribute("onmousedown", onclick) self._div.appendChild(item) # Show - self._select_does_replace = "descriptions" in headline self._div.hidden = False self._make_active(0) @@ -1357,8 +1380,13 @@ def _finish_cb(self, e, i): def _finish(self, text): self.clear() + if isinstance(text, str): + text = text + select_does_replace = False + else: + text, select_does_replace = text if text: - if self._select_does_replace: + if select_does_replace: # Recent ds, just replace the whole thing self._input.value = text self._input.selectionStart = self._input.selectionEnd = len(text)