diff options
Diffstat (limited to 'public/javascripts/search.js')
-rw-r--r-- | public/javascripts/search.js | 24 |
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 |