-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathautocompleteView.coffee
244 lines (189 loc) · 7.72 KB
/
autocompleteView.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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
define [
'jquery'
'backbone'
], (
$
Backbone
) ->
class AutocompleteView extends Backbone.View
#### Autocomplete View ####
#
# A generic autocomplete list view. Handles
# Select(hover/up arrow/down arrow) events and
# Choose (enter, click) events, while delegating
# the handling of those to a parent View. We can
# pass generic collections and models to populate
# the autocomplete, and set seperate handlers in a
# Parent view.
#
# USAGE: In parent view, initialize autocomplete view with
# these options.
#
# el: The list element which contains list items
#
# collection: The collection to populate the
# autocomplete list with.
#
# template:
#
# listItemTemplate: Template for each list
# item, as a function (Jade)
#
# locals: A function that takes a model and
# returns a locals object that can be passed
# to the template
#
# getLabel: A function which takes a model, and
# returns value that should be returned on item
# select for display in input.
#
# matchOn: A function which takes a model, and
# returns a value that you want the autocomplete
# matcher to match on
#
# handlers:
#
# itemSelectedHandler: Handler function for
# item select event. Takes a model.
#
# itemChosenHandler: Handler function for
# item choose event. Takes a model.
#
#
# PUBLIC METHODS:
#
# updateTerm(term): Takes a term, filters
# collection on term using provided
# matchOn(model) method, and updates the list.
#
# unChooseItem(id): Takes a model id, restores
# that model to the original collection.
#
#
#####
# The maximum number of list items to show in the
# autocomplete list.
maxListLength: 4
## Initialization
initialize: (options) ->
_.bindAll()
# Bind all options to 'this' (we need them all)
_.each options, ((v,k) -> this[k]=v),this
@chosenCollection = new Backbone.Collection()
#### Methods that interact with parent view ####
## Called by parent view:
## Given an autocomplete term, filter the
## collection and display those items. Applies
## configured matchOn method to test against term
updateTerm: (term) ->
regex = new RegExp(@escapeRegex(term),"i")
data = @collection.filter(
(model) -> regex.test(@matchOn(model))
this)
@updateList(data)
## Called by parent view:
## Handler for choosing an item
unChooseItem: (id) ->
model = @chosenCollection.get(id)
if model?
@chosenCollection.remove(model)
@collection.add model
## Handler for choosing an item
chooseItem: ($item) ->
id = $item.attr 'data-id'
model = @collection.get id
@handlers.itemChosenHandler model
@collection.remove model
@chosenCollection.add model
## Handler for selecting an item
selectItem: ($item) ->
if @$selection?
$oldSelection = @$selection.removeClass('is-list-item-selected')
@$selection = $item.addClass('is-list-item-selected')
model = @collection.get($item.attr('data-id'))
@handlers.itemSelectedHandler model
return $oldSelection
#### Handling Events ####
## Events
events:
'click .pnt-autocomplete-list-item' : 'chooseItemOnClick'
# TODO get mouse events to work
#'click .autocomplete-list-item' : 'clickedResult'
#'mouseenter .autocomplete-list-item' : 'mouseOverResult'
#'mouseleave .autocomplete-list-item' : 'mouseOutResult'
## Bind key and mouse events to document
bindExternalEvents: ->
$(window).bind 'keydown', @onKeydownBody
# TODO mouse events
#$(window).bind('mousemove', @rebindHover)
#$(window).bind('click', @rebindHover)
## Handle keys that aren't neccessarily used when input is
## focused, like paging down and up the autocomplete
onKeydownBody: (e) ->
switch e.keyCode
when 13 #enter
e.preventDefault()
@chooseSelectedItemOnEnter()
when 38 #up
e.preventDefault()
@selectChangeOn('up')
when 40 #down
e.preventDefault()
@selectChangeOn('down')
## Override Backbone.Views's remove. Manually unbind anything
## that is bound in this method. Then call super remove, which
## removes view from DOM, and calls stopListening to remove any
## bound events that the view has listenTo'd to.
remove: ->
$(window).unbind 'keydown', @onKeydownBody
super()
## Update list with new data. The data is usually a
## subset of this.collection
updateList: (data) ->
# Empty list element
@emptyListEl()
# Return if no data
return if data.length < 1
# If we're going to limit the list, lets do it
# here for performance
data = _.first(data, @maxListLength)
# Append items with updated data
_.each data,
(model) ->
listItem = @template.listItemTemplate(@template.locals(model))
@$el.append $(listItem).attr('data-id', model.id)
this
# Set selection to first list item
@$selection =
@$('.pnt-autocomplete-list-item')
.first().addClass('is-list-item-selected')
## Empty the list, clear selection
emptyListEl: ->
@$el.html('')
@$selection = null
## Event Handlers for clicking/enter-ing to choose
## an item, and call chooseItem on the element
chooseSelectedItemOnEnter: (e) -> @chooseItem @$selection if @$selection?
chooseItemOnClick: (e) -> @chooseItem $(e.target)
## Given a direction 'up' or 'down' move to next or
## previous selected element in autocomplete list.
## If no next/prev, wrap around to first or last.
selectChangeOn: (direction) ->
return if direction not in ['up', 'down']
# TODO: handle this hover stuff
#@hoverEnabled = false
# If something is selected
if @$selection?
# Get next or prev element
$newSelection =
if direction == 'up' then @$selection.prev() else @$selection.next()
if $newSelection.length > 0
$oldSelection = @selectItem($newSelection)
#if direction == 'up' then @scrollUp() else @scrollDown($oldSelection)
else
# If nothing already selected, select first or last
if direction == 'up'
@selectItem @$el.find('.pnt-autocomplete-list-item').last()
else
@selectItem @$el.find('.pnt-autocomplete-list-item').first()
escapeRegex: (value) -> value.replace(/[\-\[\]{}()*+?.,\\\^$|#\s]/g, "\\$&")