-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsearch_field.coffee
46 lines (38 loc) · 1.32 KB
/
search_field.coffee
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
Spomet.defaultSearch = new Spomet.Search
Template.spometSearch.helpers
latestPhrase: () ->
phrase = Spomet.defaultSearch.getCurrentPhrase()
if phrase? then phrase else ''
searchInProgress: () ->
Spomet.defaultSearch.isSearching()?
searching: () ->
Spomet.defaultSearch.getCurrentPhrase()?
typeaheadSource = (query) ->
[start..., last] = @query.split ' '
r = new RegExp "^#{last}"
cursor = Spomet.CommonTerms.find
token: r
tlength: {$gt: last.length}
fixed = start.join ' '
cursor.map (e) ->
if fixed and fixed.length > 0
fixed + ' ' + e.token
else
e.token
Template.spometSearch.rendered = () ->
$('input.spomet-search-field').typeahead
source: typeaheadSource
updater: (item) ->
$('input.spomet-search-field')[0].value = item
Spomet.defaultSearch.find item
matcher: (item) ->
true
Template.spometSearch.events
'submit form': (e) ->
e.preventDefault()
Spomet.defaultSearch.clearSearch()
phrase = $('input.spomet-search-field')[0].value
if phrase? and phrase.length > 0
Spomet.defaultSearch.find phrase
'click button.spomet-reset-search': (e) ->
Spomet.defaultSearch.clearSearch()