Skip to content

Commit 056fa9e

Browse files
committed
let coffeescript handle function scope
1 parent 4d9250d commit 056fa9e

File tree

3 files changed

+87
-87
lines changed

3 files changed

+87
-87
lines changed

coffee/chosen.jquery.coffee

+36-36
Original file line numberDiff line numberDiff line change
@@ -69,37 +69,37 @@ class Chosen extends AbstractChosen
6969
@form_field_jq.trigger("chosen:ready", {chosen: this})
7070

7171
register_observers: ->
72-
@container.bind 'touchstart.chosen', (evt) => this.container_mousedown(evt); evt.preventDefault()
73-
@container.bind 'touchend.chosen', (evt) => this.container_mouseup(evt); evt.preventDefault()
74-
75-
@container.bind 'mousedown.chosen', (evt) => this.container_mousedown(evt); return
76-
@container.bind 'mouseup.chosen', (evt) => this.container_mouseup(evt); return
77-
@container.bind 'mouseenter.chosen', (evt) => this.mouse_enter(evt); return
78-
@container.bind 'mouseleave.chosen', (evt) => this.mouse_leave(evt); return
79-
80-
@search_results.bind 'mouseup.chosen', (evt) => this.search_results_mouseup(evt); return
81-
@search_results.bind 'mouseover.chosen', (evt) => this.search_results_mouseover(evt); return
82-
@search_results.bind 'mouseout.chosen', (evt) => this.search_results_mouseout(evt); return
83-
@search_results.bind 'mousewheel.chosen DOMMouseScroll.chosen', (evt) => this.search_results_mousewheel(evt); return
84-
85-
@search_results.bind 'touchstart.chosen', (evt) => this.search_results_touchstart(evt); return
86-
@search_results.bind 'touchmove.chosen', (evt) => this.search_results_touchmove(evt); return
87-
@search_results.bind 'touchend.chosen', (evt) => this.search_results_touchend(evt); return
88-
89-
@form_field_jq.bind "chosen:updated.chosen", (evt) => this.results_update_field(evt); return
90-
@form_field_jq.bind "chosen:activate.chosen", this.activate_field
91-
@form_field_jq.bind "chosen:open.chosen", (evt) => this.container_mousedown(evt); return
92-
@form_field_jq.bind "chosen:close.chosen", (evt) => this.close_field(evt); return
93-
94-
@search_field.bind 'blur.chosen', (evt) => this.input_blur(evt); return
95-
@search_field.bind 'keyup.chosen', (evt) => this.keyup_checker(evt); return
96-
@search_field.bind 'keydown.chosen', (evt) => this.keydown_checker(evt); return
97-
@search_field.bind 'focus.chosen', (evt) => this.input_focus(evt); return
98-
@search_field.bind 'cut.chosen', (evt) => this.clipboard_event_checker(evt); return
99-
@search_field.bind 'paste.chosen', (evt) => this.clipboard_event_checker(evt); return
72+
@container.bind 'touchstart.chosen', this.container_mousedown
73+
@container.bind 'touchend.chosen', this.container_mouseup
74+
75+
@container.bind 'mousedown.chosen', this.container_mousedown
76+
@container.bind 'mouseup.chosen', this.container_mouseup
77+
@container.bind 'mouseenter.chosen', this.mouse_enter
78+
@container.bind 'mouseleave.chosen', this.mouse_leave
79+
80+
@search_results.bind 'mouseup.chosen', this.search_results_mouseup
81+
@search_results.bind 'mouseover.chosen', this.search_results_mouseover
82+
@search_results.bind 'mouseout.chosen', this.search_results_mouseout
83+
@search_results.bind 'mousewheel.chosen DOMMouseScroll.chosen', this.search_results_mousewheel
84+
85+
@search_results.bind 'touchstart.chosen', this.search_results_touchstart
86+
@search_results.bind 'touchmove.chosen', this.search_results_touchmove
87+
@search_results.bind 'touchend.chosen', this.search_results_touchend
88+
89+
@form_field_jq.bind 'chosen:updated.chosen', this.results_update_field
90+
@form_field_jq.bind 'chosen:activate.chosen', this.activate_field
91+
@form_field_jq.bind 'chosen:open.chosen', this.container_mousedown
92+
@form_field_jq.bind 'chosen:close.chosen', this.close_field
93+
94+
@search_field.bind 'blur.chosen', this.input_blur
95+
@search_field.bind 'keyup.chosen', this.keyup_checker
96+
@search_field.bind 'keydown.chosen', this.keydown_checker
97+
@search_field.bind 'focus.chosen', this.input_focus
98+
@search_field.bind 'cut.chosen', this.clipboard_event_checker
99+
@search_field.bind 'paste.chosen', this.clipboard_event_checker
100100

101101
if @is_multiple
102-
@search_choices.bind 'click.chosen', (evt) => this.choices_click(evt); return
102+
@search_choices.bind 'click.chosen', this.choices_click
103103
else
104104
@container.bind 'click.chosen', (evt) -> evt.preventDefault(); return # gobble click of anchor
105105

@@ -127,7 +127,7 @@ class Chosen extends AbstractChosen
127127
else unless @is_multiple
128128
@selected_item.bind 'focus.chosen', this.activate_field
129129

130-
container_mousedown: (evt) ->
130+
container_mousedown: (evt) =>
131131
return if @is_disabled
132132

133133
if evt and evt.type is "mousedown" and not @results_showing
@@ -144,10 +144,10 @@ class Chosen extends AbstractChosen
144144

145145
this.activate_field()
146146

147-
container_mouseup: (evt) ->
147+
container_mouseup: (evt) =>
148148
this.results_reset(evt) if evt.target.nodeName is "ABBR" and not @is_disabled
149149

150-
search_results_mousewheel: (evt) ->
150+
search_results_mousewheel: (evt) =>
151151
delta = evt.originalEvent.deltaY or -evt.originalEvent.wheelDelta or evt.originalEvent.detail if evt.originalEvent
152152
if delta?
153153
evt.preventDefault()
@@ -157,7 +157,7 @@ class Chosen extends AbstractChosen
157157
blur_test: ->
158158
this.close_field() if not @active_field and @container.hasClass "chosen-container-active"
159159

160-
close_field: ->
160+
close_field: =>
161161
$(@container[0].ownerDocument).unbind 'click.chosen', this.test_active_click
162162

163163
@active_field = false
@@ -283,18 +283,18 @@ class Chosen extends AbstractChosen
283283
@search_field.val("")
284284
@search_field.removeClass "default"
285285

286-
search_results_mouseup: (evt) ->
286+
search_results_mouseup: (evt) =>
287287
target = if $(evt.target).hasClass "active-result" then $(evt.target) else $(evt.target).parents(".active-result").first()
288288
if target.length
289289
@result_highlight = target
290290
this.result_select(evt)
291291
@search_field.focus()
292292

293-
search_results_mouseover: (evt) ->
293+
search_results_mouseover: (evt) =>
294294
target = if $(evt.target).hasClass "active-result" then $(evt.target) else $(evt.target).parents(".active-result").first()
295295
this.result_do_highlight( target ) if target
296296

297-
search_results_mouseout: (evt) ->
297+
search_results_mouseout: (evt) =>
298298
this.result_clear_highlight() if $(evt.target).hasClass "active-result" or $(evt.target).parents('.active-result').first()
299299

300300
choice_build: (item) ->

coffee/chosen.proto.coffee

+38-38
Original file line numberDiff line numberDiff line change
@@ -50,40 +50,40 @@ class @Chosen extends AbstractChosen
5050
@form_field.fire("chosen:ready", {chosen: this})
5151

5252
register_observers: ->
53-
@container.observe "touchstart", (evt) => this.container_mousedown(evt); evt.preventDefault()
54-
@container.observe "touchend", (evt) => this.container_mouseup(evt); evt.preventDefault()
55-
56-
@container.observe "mousedown", (evt) => this.container_mousedown(evt)
57-
@container.observe "mouseup", (evt) => this.container_mouseup(evt)
58-
@container.observe "mouseenter", (evt) => this.mouse_enter(evt)
59-
@container.observe "mouseleave", (evt) => this.mouse_leave(evt)
60-
61-
@search_results.observe "mouseup", (evt) => this.search_results_mouseup(evt)
62-
@search_results.observe "mouseover", (evt) => this.search_results_mouseover(evt)
63-
@search_results.observe "mouseout", (evt) => this.search_results_mouseout(evt)
64-
@search_results.observe "mousewheel", (evt) => this.search_results_mousewheel(evt)
65-
@search_results.observe "DOMMouseScroll", (evt) => this.search_results_mousewheel(evt)
66-
67-
@search_results.observe "touchstart", (evt) => this.search_results_touchstart(evt)
68-
@search_results.observe "touchmove", (evt) => this.search_results_touchmove(evt)
69-
@search_results.observe "touchend", (evt) => this.search_results_touchend(evt)
70-
71-
@form_field.observe "chosen:updated", (evt) => this.results_update_field(evt)
72-
@form_field.observe "chosen:activate", this.activate_field
73-
@form_field.observe "chosen:open", (evt) => this.container_mousedown(evt)
74-
@form_field.observe "chosen:close", (evt) => this.close_field(evt)
75-
76-
@search_field.observe "blur", (evt) => this.input_blur(evt)
77-
@search_field.observe "keyup", (evt) => this.keyup_checker(evt)
78-
@search_field.observe "keydown", (evt) => this.keydown_checker(evt)
79-
@search_field.observe "focus", (evt) => this.input_focus(evt)
80-
@search_field.observe "cut", (evt) => this.clipboard_event_checker(evt)
81-
@search_field.observe "paste", (evt) => this.clipboard_event_checker(evt)
53+
@container.observe 'touchstart', this.container_mousedown
54+
@container.observe 'touchend', this.container_mouseup
55+
56+
@container.observe 'mousedown', this.container_mousedown
57+
@container.observe 'mouseup', this.container_mouseup
58+
@container.observe 'mouseenter', this.mouse_enter
59+
@container.observe 'mouseleave', this.mouse_leave
60+
61+
@search_results.observe 'mouseup', this.search_results_mouseup
62+
@search_results.observe 'mouseover', this.search_results_mouseover
63+
@search_results.observe 'mouseout', this.search_results_mouseout
64+
@search_results.observe 'mousewheel', this.search_results_mousewheel
65+
@search_results.observe 'DOMMouseScroll', this.search_results_mousewheel
66+
67+
@search_results.observe 'touchstart', this.search_results_touchstart
68+
@search_results.observe 'touchmove', this.search_results_touchmove
69+
@search_results.observe 'touchend', this.search_results_touchend
70+
71+
@form_field.observe 'chosen:updated', this.results_update_field
72+
@form_field.observe 'chosen:activate', this.activate_field
73+
@form_field.observe 'chosen:open', this.container_mousedown
74+
@form_field.observe 'chosen:close', this.close_field
75+
76+
@search_field.observe 'blur', this.input_blur
77+
@search_field.observe 'keyup', this.keyup_checker
78+
@search_field.observe 'keydown', this.keydown_checker
79+
@search_field.observe 'focus', this.input_focus
80+
@search_field.observe 'cut', this.clipboard_event_checker
81+
@search_field.observe 'paste', this.clipboard_event_checker
8282

8383
if @is_multiple
84-
@search_choices.observe "click", (evt) => this.choices_click(evt)
84+
@search_choices.observe 'click', this.choices_click
8585
else
86-
@container.observe "click", (evt) => evt.preventDefault() # gobble click of anchor
86+
@container.observe 'click', (evt) => evt.preventDefault() # gobble click of anchor
8787

8888
destroy: ->
8989
@container.ownerDocument.stopObserving 'click', this.test_active_click
@@ -126,7 +126,7 @@ class @Chosen extends AbstractChosen
126126
else unless @is_multiple
127127
@selected_item.observe 'focus', this.activate_field
128128

129-
container_mousedown: (evt) ->
129+
container_mousedown: (evt) =>
130130
return if @is_disabled
131131

132132
if evt and evt.type is "mousedown" and not @results_showing
@@ -142,10 +142,10 @@ class @Chosen extends AbstractChosen
142142

143143
this.activate_field()
144144

145-
container_mouseup: (evt) ->
145+
container_mouseup: (evt) =>
146146
this.results_reset(evt) if evt.target.nodeName is "ABBR" and not @is_disabled
147147

148-
search_results_mousewheel: (evt) ->
148+
search_results_mousewheel: (evt) =>
149149
delta = evt.deltaY or -evt.wheelDelta or evt.detail
150150
if delta?
151151
evt.preventDefault()
@@ -155,7 +155,7 @@ class @Chosen extends AbstractChosen
155155
blur_test: ->
156156
this.close_field() if not @active_field and @container.hasClassName("chosen-container-active")
157157

158-
close_field: ->
158+
close_field: =>
159159
@container.ownerDocument.stopObserving 'click', this.test_active_click
160160

161161
@active_field = false
@@ -279,18 +279,18 @@ class @Chosen extends AbstractChosen
279279
@search_field.value = ""
280280
@search_field.removeClassName "default"
281281

282-
search_results_mouseup: (evt) ->
282+
search_results_mouseup: (evt) =>
283283
target = if evt.target.hasClassName("active-result") then evt.target else evt.target.up(".active-result")
284284
if target
285285
@result_highlight = target
286286
this.result_select(evt)
287287
@search_field.focus()
288288

289-
search_results_mouseover: (evt) ->
289+
search_results_mouseover: (evt) =>
290290
target = if evt.target.hasClassName("active-result") then evt.target else evt.target.up(".active-result")
291291
this.result_do_highlight( target ) if target
292292

293-
search_results_mouseout: (evt) ->
293+
search_results_mouseout: (evt) =>
294294
this.result_clear_highlight() if evt.target.hasClassName('active-result') or evt.target.up('.active-result')
295295

296296
choice_build: (item) ->

coffee/lib/abstract-chosen.coffee

+13-13
Original file line numberDiff line numberDiff line change
@@ -53,16 +53,16 @@ class AbstractChosen
5353
else
5454
item.html
5555

56-
mouse_enter: -> @mouse_on_container = true
57-
mouse_leave: -> @mouse_on_container = false
56+
mouse_enter: => @mouse_on_container = true
57+
mouse_leave: => @mouse_on_container = false
5858

59-
input_focus: ->
59+
input_focus: =>
6060
if @is_multiple
61-
setTimeout (=> this.container_mousedown()), 50 unless @active_field
61+
setTimeout (this.container_mousedown), 50 unless @active_field
6262
else
6363
@activate_field() unless @active_field
6464

65-
input_blur: ->
65+
input_blur: =>
6666
if not @mouse_on_container
6767
@active_field = false
6868
setTimeout (=> this.blur_test()), 100
@@ -134,7 +134,7 @@ class AbstractChosen
134134

135135
this.outerHTML(group_el)
136136

137-
results_update_field: ->
137+
results_update_field: =>
138138
this.set_default_text()
139139
this.results_reset_cleanup() if not @is_multiple
140140
this.result_clear_highlight()
@@ -239,11 +239,11 @@ class AbstractChosen
239239

240240
return @selected_option_count
241241

242-
choices_click: (evt) ->
242+
choices_click: (evt) =>
243243
evt.preventDefault()
244244
this.results_show() unless @results_showing or @is_disabled
245245

246-
keydown_checker: (evt) ->
246+
keydown_checker: (evt) =>
247247
stroke = evt.which ? evt.keyCode
248248
this.search_field_scale()
249249

@@ -275,7 +275,7 @@ class AbstractChosen
275275
this.keydown_arrow()
276276
break
277277

278-
keyup_checker: (evt) ->
278+
keyup_checker: (evt) =>
279279
stroke = evt.which ? evt.keyCode
280280
this.search_field_scale()
281281

@@ -300,7 +300,7 @@ class AbstractChosen
300300
this.results_search()
301301
break
302302

303-
clipboard_event_checker: ->
303+
clipboard_event_checker: =>
304304
return if @is_disabled
305305
setTimeout (=> this.results_search()), 50
306306

@@ -314,15 +314,15 @@ class AbstractChosen
314314

315315
return true
316316

317-
search_results_touchstart: (evt) ->
317+
search_results_touchstart: (evt) =>
318318
@touch_started = true
319319
this.search_results_mouseover(evt)
320320

321-
search_results_touchmove: (evt) ->
321+
search_results_touchmove: (evt) =>
322322
@touch_started = false
323323
this.search_results_mouseout(evt)
324324

325-
search_results_touchend: (evt) ->
325+
search_results_touchend: (evt) =>
326326
this.search_results_mouseup(evt) if @touch_started
327327

328328
outerHTML: (element) ->

0 commit comments

Comments
 (0)