From 88b3cc0aa801ea4d3c081ce6ad943808967a2c68 Mon Sep 17 00:00:00 2001
From: "Henrik.Askjer" <henrik.askjer@uib.no>
Date: Fri, 27 Aug 2021 11:07:26 +0200
Subject: [PATCH] New solution to autocorrect issue

---
 src/components/Autocomplete.vue | 39 ++++++++++++++++++---------------
 1 file changed, 21 insertions(+), 18 deletions(-)

diff --git a/src/components/Autocomplete.vue b/src/components/Autocomplete.vue
index b2a8431e..3ad356ae 100644
--- a/src/components/Autocomplete.vue
+++ b/src/components/Autocomplete.vue
@@ -54,22 +54,23 @@
                                                                   n: 9}} )
                       .then(
                             function(response) {
-                                if (self.suggesting) {
-                                  let hits = []
-                                  response.data.forEach((item, i) => {
-                                    let hit = {q: q, label: item[0]}
-                                    hit.lang = item[1][1] ? "bm, nn" : {"bob": "bm", "nob": "nn"}[item[1][0]]
-                                    hits.push(hit)
+                                  let currentSearch = self.$refs.autocomplete.searchInput
+                                  if (currentSearch == q) {
 
-                                  });
+                                    let hits = []
+                                    response.data.forEach((item, i) => {
+                                      let hit = {q: q, label: item[0]}
+                                      hit.lang = item[1][1] ? "bm, nn" : {"bob": "bm", "nob": "nn"}[item[1][0]]
+                                      hits.push(hit)
 
-                                self.items = hits
+                                    });
+
+                                    hits.push({q: currentSearch, label: currentSearch})
+                                    self.items = hits
+                                  }
+                                
                                 
-                                // Fritekstsøk
-                                let currentSearch = self.$refs.autocomplete.searchInput
-                                self.items.push({q: currentSearch, label: currentSearch + ' '})
                                 
-                                }
                                 self.loading = false
                               })
         }, 100)
@@ -84,23 +85,25 @@
         }
       },
       select(item) {
+        this.items = []
         if (item) {
-          if (item.q === this.$refs.autocomplete.searchInput) {
-              this.items = []
-              this.suggesting = false
+          if (typeof item === 'string') { // if blurred without selecting an option
+             // TODO: sett timeout for å vente på siste forslag?
+            item = {q: item}
 
+          }
+          if (item.q == this.$refs.autocomplete.searchInput) {
               this.$emit('submit', item)
               let self = this
               setTimeout(() => self.$refs.autocomplete.$refs.input.select(), 1)
-
           }
-          // TODO: else: sett timeout for å vente på siste forslag
+          
         }
       }
     },
     methods: {
       run_query(q) {
-        this.suggesting = true
+        this.items = []
         this.debounced(q, this)
       }
     },
-- 
GitLab