aboutsummaryrefslogtreecommitdiff
path: root/public/javascripts/search.js
diff options
context:
space:
mode:
authorBuildTools <Noah Loomans>2016-12-08 22:43:21 +0100
committerBuildTools <Noah Loomans>2016-12-08 22:43:21 +0100
commit5c7fed6eda250d91d4babd14da73166425fab85b (patch)
tree3a108428592dbd72a92294e19469d8c1b77a29e3 /public/javascripts/search.js
parentc547e6910ea7ad077771343ae75a4204223a118a (diff)
moved function from autocomplete to search
Diffstat (limited to 'public/javascripts/search.js')
-rw-r--r--public/javascripts/search.js24
1 files changed, 24 insertions, 0 deletions
diff --git a/public/javascripts/search.js b/public/javascripts/search.js
index fb40f78..673afea 100644
--- a/public/javascripts/search.js
+++ b/public/javascripts/search.js
@@ -1,3 +1,6 @@
+/* global USERS */
+
+const fuzzy = require('fuzzy')
const autocomplete = require('./autocomplete')
const iframe = require('./iframe')
@@ -21,9 +24,30 @@ self._handleSubmit = function (event) {
self.submit()
}
+self._calculate = function (searchTerm) {
+ const allResults = fuzzy.filter(searchTerm, USERS, {
+ extract: item => item.value
+ })
+ const firstResults = allResults.slice(0, 7)
+
+ const originalResults = firstResults.map(result => result.original)
+
+ return originalResults
+}
+
+self._handleTextUpdate = function () {
+ const results = self._calculate(self._nodes.input.value)
+
+ autocomplete.removeAllItems()
+ for (let i = 0; i < results.length; i++) {
+ autocomplete.addItem(results[i])
+ }
+}
+
autocomplete.events.on('select', self.submit)
self._nodes.search.addEventListener('submit', self._handleSubmit)
+self._nodes.input.addEventListener('input', self._handleTextUpdate)
self._nodes.input.addEventListener('focus', autocomplete.show)
// TODO: hide on escape key press