Auto-complete matches can be fuzzy-filtered. Typing non-word characters confirms the match automatically.

Extract fuzzyFilter function into a file that is shared between file finder and autocompleter. Fix jQuery.fn.preempt to pass its arguments to the event handler.
This commit is contained in:
Nathan Sobo
2012-04-19 18:17:08 -06:00
parent cdd824960e
commit 0024cf89de
5 changed files with 98 additions and 31 deletions

View File

@@ -0,0 +1,16 @@
stringScore = require 'stringscore'
module.exports = (candidates, query, options) ->
if query
scoredCandidates = candidates.map (candidate) ->
string = if options.key? then candidate[options.key] else candidate
{ candidate, score: stringScore(string, query) }
scoredCandidates.sort (a, b) ->
if a.score > b.score then -1
else if a.score < b.score then 1
else 0
candidates = (scoredCandidate.candidate for scoredCandidate in scoredCandidates when scoredCandidate.score > 0)
candidates = candidates[0...options.maxResults] if options.maxResults?
candidates

View File

@@ -16,8 +16,8 @@ $.fn.containsElement = (element) ->
(element[0].compareDocumentPosition(this[0]) & 8) == 8
$.fn.preempt = (eventName, handler) ->
@on eventName, (e) ->
if handler() == false then e.stopImmediatePropagation()
@on eventName, (e, args...) ->
if handler(e, args...) == false then e.stopImmediatePropagation()
eventNameWithoutNamespace = eventName.split('.')[0]
handlers = @data('events')[eventNameWithoutNamespace]