forked from Meteor-Community-Packages/meteor-autocomplete
-
Notifications
You must be signed in to change notification settings - Fork 0
/
templates.coffee
53 lines (42 loc) · 1.64 KB
/
templates.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
47
48
49
50
51
52
53
autocompleteData = ->
attributes: _.omit(@, 'settings') # Render all but the settings parameter
ac: new AutoComplete(@settings)
UI.registerHelper "inputAutocomplete", ->
UI.Component.extend
kind: "InputAutocomplete",
data: autocompleteData.call(@)
render: -> Template._inputAutocomplete
UI.registerHelper "textareaAutocomplete", ->
UI.Component.extend
kind: "TextareaAutocomplete",
data: autocompleteData.call(@)
render: -> Template._textareaAutocomplete
# Events on template instances, sent to the autocomplete class
events =
"keydown": (e) -> @ac.onKeyDown(e)
"keyup": (e) -> @ac.onKeyUp(e)
"focus": (e) -> @ac.onFocus(e)
"blur": (e) -> @ac.onBlur(e)
Template._inputAutocomplete.events = events
Template._textareaAutocomplete.events = events
# Set nodes on render in the autocomplete class
# This will re-render on every change due to the Blaze hack above
init = ->
@data.ac.element = @firstNode
@data.ac.$element = $(@firstNode)
@data.ac.tmplInst = this
Template._inputAutocomplete.rendered = init
Template._textareaAutocomplete.rendered = init
###
List rendering helpers
###
Template._autocompleteContainer.destroyed = ->
# console.log "autocomplete destroyed"
@data.teardown()
Template._autocompleteContainer.events =
# tmplInst.data is the AutoComplete instance
"click .-autocomplete-item": (e, tmplInst) -> tmplInst.data.onItemClick(this, e)
"mouseenter .-autocomplete-item": (e, tmplInst) -> tmplInst.data.onItemHover(this, e)
Template._autocompleteContainer.empty = -> @filteredList().count() is 0
Template._autocompleteContainer.noMatchTemplate = ->
@matchedRule().noMatchTemplate || Template._noMatch