From 5dd350a674d6aaa475f2481ca20f69273a8f22b5 Mon Sep 17 00:00:00 2001 From: Koen Punt Date: Thu, 20 Oct 2016 16:54:12 +0200 Subject: [PATCH] let coffeescript handle function scope --- coffee/chosen.jquery.coffee | 72 ++++++++++++++--------------- coffee/chosen.proto.coffee | 76 +++++++++++++++---------------- coffee/lib/abstract-chosen.coffee | 26 +++++------ 3 files changed, 87 insertions(+), 87 deletions(-) diff --git a/coffee/chosen.jquery.coffee b/coffee/chosen.jquery.coffee index 76fd6cbcf0c..9729e8022d4 100644 --- a/coffee/chosen.jquery.coffee +++ b/coffee/chosen.jquery.coffee @@ -71,37 +71,37 @@ class Chosen extends AbstractChosen @form_field_jq.trigger("chosen:ready", {chosen: this}) register_observers: -> - @container.bind 'touchstart.chosen', (evt) => this.container_mousedown(evt); return - @container.bind 'touchend.chosen', (evt) => this.container_mouseup(evt); return - - @container.bind 'mousedown.chosen', (evt) => this.container_mousedown(evt); return - @container.bind 'mouseup.chosen', (evt) => this.container_mouseup(evt); return - @container.bind 'mouseenter.chosen', (evt) => this.mouse_enter(evt); return - @container.bind 'mouseleave.chosen', (evt) => this.mouse_leave(evt); return - - @search_results.bind 'mouseup.chosen', (evt) => this.search_results_mouseup(evt); return - @search_results.bind 'mouseover.chosen', (evt) => this.search_results_mouseover(evt); return - @search_results.bind 'mouseout.chosen', (evt) => this.search_results_mouseout(evt); return - @search_results.bind 'mousewheel.chosen DOMMouseScroll.chosen', (evt) => this.search_results_mousewheel(evt); return - - @search_results.bind 'touchstart.chosen', (evt) => this.search_results_touchstart(evt); return - @search_results.bind 'touchmove.chosen', (evt) => this.search_results_touchmove(evt); return - @search_results.bind 'touchend.chosen', (evt) => this.search_results_touchend(evt); return - - @form_field_jq.bind "chosen:updated.chosen", (evt) => this.results_update_field(evt); return - @form_field_jq.bind "chosen:activate.chosen", this.activate_field - @form_field_jq.bind "chosen:open.chosen", (evt) => this.container_mousedown(evt); return - @form_field_jq.bind "chosen:close.chosen", (evt) => this.close_field(evt); return - - @search_field.bind 'blur.chosen', (evt) => this.input_blur(evt); return - @search_field.bind 'keyup.chosen', (evt) => this.keyup_checker(evt); return - @search_field.bind 'keydown.chosen', (evt) => this.keydown_checker(evt); return - @search_field.bind 'focus.chosen', (evt) => this.input_focus(evt); return - @search_field.bind 'cut.chosen', (evt) => this.clipboard_event_checker(evt); return - @search_field.bind 'paste.chosen', (evt) => this.clipboard_event_checker(evt); return + @container.bind 'touchstart.chosen', this.container_mousedown + @container.bind 'touchend.chosen', this.container_mouseup + + @container.bind 'mousedown.chosen', this.container_mousedown + @container.bind 'mouseup.chosen', this.container_mouseup + @container.bind 'mouseenter.chosen', this.mouse_enter + @container.bind 'mouseleave.chosen', this.mouse_leave + + @search_results.bind 'mouseup.chosen', this.search_results_mouseup + @search_results.bind 'mouseover.chosen', this.search_results_mouseover + @search_results.bind 'mouseout.chosen', this.search_results_mouseout + @search_results.bind 'mousewheel.chosen DOMMouseScroll.chosen', this.search_results_mousewheel + + @search_results.bind 'touchstart.chosen', this.search_results_touchstart + @search_results.bind 'touchmove.chosen', this.search_results_touchmove + @search_results.bind 'touchend.chosen', this.search_results_touchend + + @form_field_jq.bind 'chosen:updated.chosen', this.results_update_field + @form_field_jq.bind 'chosen:activate.chosen', this.activate_field + @form_field_jq.bind 'chosen:open.chosen', this.container_mousedown + @form_field_jq.bind 'chosen:close.chosen', this.close_field + + @search_field.bind 'blur.chosen', this.input_blur + @search_field.bind 'keyup.chosen', this.keyup_checker + @search_field.bind 'keydown.chosen', this.keydown_checker + @search_field.bind 'focus.chosen', this.input_focus + @search_field.bind 'cut.chosen', this.clipboard_event_checker + @search_field.bind 'paste.chosen', this.clipboard_event_checker if @is_multiple - @search_choices.bind 'click.chosen', (evt) => this.choices_click(evt); return + @search_choices.bind 'click.chosen', this.choices_click else @container.bind 'click.chosen', (evt) -> evt.preventDefault(); return # gobble click of anchor @@ -130,7 +130,7 @@ class Chosen extends AbstractChosen else unless @is_multiple @selected_item.bind 'focus.chosen', this.activate_field - container_mousedown: (evt) -> + container_mousedown: (evt) => return if @is_disabled if evt and evt.type in ['mousedown', 'touchstart'] and not @results_showing @@ -147,10 +147,10 @@ class Chosen extends AbstractChosen this.activate_field() - container_mouseup: (evt) -> + container_mouseup: (evt) => this.results_reset(evt) if evt.target.nodeName is "ABBR" and not @is_disabled - search_results_mousewheel: (evt) -> + search_results_mousewheel: (evt) => delta = evt.originalEvent.deltaY or -evt.originalEvent.wheelDelta or evt.originalEvent.detail if evt.originalEvent if delta? evt.preventDefault() @@ -160,7 +160,7 @@ class Chosen extends AbstractChosen blur_test: -> this.close_field() if not @active_field and @container.hasClass "chosen-container-active" - close_field: -> + close_field: => $(@container[0].ownerDocument).unbind 'click.chosen', this.test_active_click @active_field = false @@ -286,18 +286,18 @@ class Chosen extends AbstractChosen @search_field.val("") @search_field.removeClass "default" - search_results_mouseup: (evt) -> + search_results_mouseup: (evt) => target = if $(evt.target).hasClass "active-result" then $(evt.target) else $(evt.target).parents(".active-result").first() if target.length @result_highlight = target this.result_select(evt) @search_field.focus() - search_results_mouseover: (evt) -> + search_results_mouseover: (evt) => target = if $(evt.target).hasClass "active-result" then $(evt.target) else $(evt.target).parents(".active-result").first() this.result_do_highlight( target ) if target - search_results_mouseout: (evt) -> + search_results_mouseout: (evt) => this.result_clear_highlight() if $(evt.target).hasClass "active-result" or $(evt.target).parents('.active-result').first() choice_build: (item) -> diff --git a/coffee/chosen.proto.coffee b/coffee/chosen.proto.coffee index 502b3d3acef..98653ed072a 100644 --- a/coffee/chosen.proto.coffee +++ b/coffee/chosen.proto.coffee @@ -55,40 +55,40 @@ class @Chosen extends AbstractChosen @form_field.fire("chosen:ready", {chosen: this}) register_observers: -> - @container.observe "touchstart", (evt) => this.container_mousedown(evt) - @container.observe "touchend", (evt) => this.container_mouseup(evt) - - @container.observe "mousedown", (evt) => this.container_mousedown(evt) - @container.observe "mouseup", (evt) => this.container_mouseup(evt) - @container.observe "mouseenter", (evt) => this.mouse_enter(evt) - @container.observe "mouseleave", (evt) => this.mouse_leave(evt) - - @search_results.observe "mouseup", (evt) => this.search_results_mouseup(evt) - @search_results.observe "mouseover", (evt) => this.search_results_mouseover(evt) - @search_results.observe "mouseout", (evt) => this.search_results_mouseout(evt) - @search_results.observe "mousewheel", (evt) => this.search_results_mousewheel(evt) - @search_results.observe "DOMMouseScroll", (evt) => this.search_results_mousewheel(evt) - - @search_results.observe "touchstart", (evt) => this.search_results_touchstart(evt) - @search_results.observe "touchmove", (evt) => this.search_results_touchmove(evt) - @search_results.observe "touchend", (evt) => this.search_results_touchend(evt) - - @form_field.observe "chosen:updated", (evt) => this.results_update_field(evt) - @form_field.observe "chosen:activate", this.activate_field - @form_field.observe "chosen:open", (evt) => this.container_mousedown(evt) - @form_field.observe "chosen:close", (evt) => this.close_field(evt) - - @search_field.observe "blur", (evt) => this.input_blur(evt) - @search_field.observe "keyup", (evt) => this.keyup_checker(evt) - @search_field.observe "keydown", (evt) => this.keydown_checker(evt) - @search_field.observe "focus", (evt) => this.input_focus(evt) - @search_field.observe "cut", (evt) => this.clipboard_event_checker(evt) - @search_field.observe "paste", (evt) => this.clipboard_event_checker(evt) + @container.observe 'touchstart', this.container_mousedown + @container.observe 'touchend', this.container_mouseup + + @container.observe 'mousedown', this.container_mousedown + @container.observe 'mouseup', this.container_mouseup + @container.observe 'mouseenter', this.mouse_enter + @container.observe 'mouseleave', this.mouse_leave + + @search_results.observe 'mouseup', this.search_results_mouseup + @search_results.observe 'mouseover', this.search_results_mouseover + @search_results.observe 'mouseout', this.search_results_mouseout + @search_results.observe 'mousewheel', this.search_results_mousewheel + @search_results.observe 'DOMMouseScroll', this.search_results_mousewheel + + @search_results.observe 'touchstart', this.search_results_touchstart + @search_results.observe 'touchmove', this.search_results_touchmove + @search_results.observe 'touchend', this.search_results_touchend + + @form_field.observe 'chosen:updated', this.results_update_field + @form_field.observe 'chosen:activate', this.activate_field + @form_field.observe 'chosen:open', this.container_mousedown + @form_field.observe 'chosen:close', this.close_field + + @search_field.observe 'blur', this.input_blur + @search_field.observe 'keyup', this.keyup_checker + @search_field.observe 'keydown', this.keydown_checker + @search_field.observe 'focus', this.input_focus + @search_field.observe 'cut', this.clipboard_event_checker + @search_field.observe 'paste', this.clipboard_event_checker if @is_multiple - @search_choices.observe "click", (evt) => this.choices_click(evt) + @search_choices.observe 'click', this.choices_click else - @container.observe "click", (evt) => evt.preventDefault() # gobble click of anchor + @container.observe 'click', (evt) => evt.preventDefault() # gobble click of anchor destroy: -> @container.ownerDocument.stopObserving 'click', this.test_active_click @@ -132,7 +132,7 @@ class @Chosen extends AbstractChosen else unless @is_multiple @selected_item.observe 'focus', this.activate_field - container_mousedown: (evt) -> + container_mousedown: (evt) => return if @is_disabled if evt and evt.type in ['mousedown', 'touchstart'] and not @results_showing @@ -148,10 +148,10 @@ class @Chosen extends AbstractChosen this.activate_field() - container_mouseup: (evt) -> + container_mouseup: (evt) => this.results_reset(evt) if evt.target.nodeName is "ABBR" and not @is_disabled - search_results_mousewheel: (evt) -> + search_results_mousewheel: (evt) => delta = evt.deltaY or -evt.wheelDelta or evt.detail if delta? evt.preventDefault() @@ -161,7 +161,7 @@ class @Chosen extends AbstractChosen blur_test: -> this.close_field() if not @active_field and @container.hasClassName("chosen-container-active") - close_field: -> + close_field: => @container.ownerDocument.stopObserving 'click', this.test_active_click @active_field = false @@ -285,18 +285,18 @@ class @Chosen extends AbstractChosen @search_field.value = "" @search_field.removeClassName "default" - search_results_mouseup: (evt) -> + search_results_mouseup: (evt) => target = if evt.target.hasClassName("active-result") then evt.target else evt.target.up(".active-result") if target @result_highlight = target this.result_select(evt) @search_field.focus() - search_results_mouseover: (evt) -> + search_results_mouseover: (evt) => target = if evt.target.hasClassName("active-result") then evt.target else evt.target.up(".active-result") this.result_do_highlight( target ) if target - search_results_mouseout: (evt) -> + search_results_mouseout: (evt) => this.result_clear_highlight() if evt.target.hasClassName('active-result') or evt.target.up('.active-result') choice_build: (item) -> diff --git a/coffee/lib/abstract-chosen.coffee b/coffee/lib/abstract-chosen.coffee index 51fdd621d9a..0380e1220f3 100644 --- a/coffee/lib/abstract-chosen.coffee +++ b/coffee/lib/abstract-chosen.coffee @@ -53,16 +53,16 @@ class AbstractChosen else item.html - mouse_enter: -> @mouse_on_container = true - mouse_leave: -> @mouse_on_container = false + mouse_enter: => @mouse_on_container = true + mouse_leave: => @mouse_on_container = false - input_focus: -> + input_focus: => if @is_multiple - setTimeout (=> this.container_mousedown()), 50 unless @active_field + setTimeout (this.container_mousedown), 50 unless @active_field else @activate_field() unless @active_field - input_blur: -> + input_blur: => if not @mouse_on_container @active_field = false setTimeout (=> this.blur_test()), 100 @@ -134,7 +134,7 @@ class AbstractChosen this.outerHTML(group_el) - results_update_field: -> + results_update_field: => this.set_default_text() this.results_reset_cleanup() if not @is_multiple this.result_clear_highlight() @@ -239,11 +239,11 @@ class AbstractChosen return @selected_option_count - choices_click: (evt) -> + choices_click: (evt) => evt.preventDefault() this.results_show() unless @results_showing or @is_disabled - keydown_checker: (evt) -> + keydown_checker: (evt) => stroke = evt.which ? evt.keyCode this.search_field_scale() @@ -275,7 +275,7 @@ class AbstractChosen this.keydown_arrow() break - keyup_checker: (evt) -> + keyup_checker: (evt) => stroke = evt.which ? evt.keyCode this.search_field_scale() @@ -300,7 +300,7 @@ class AbstractChosen this.results_search() break - clipboard_event_checker: -> + clipboard_event_checker: => return if @is_disabled setTimeout (=> this.results_search()), 50 @@ -314,15 +314,15 @@ class AbstractChosen return true - search_results_touchstart: (evt) -> + search_results_touchstart: (evt) => @touch_started = true this.search_results_mouseover(evt) - search_results_touchmove: (evt) -> + search_results_touchmove: (evt) => @touch_started = false this.search_results_mouseout(evt) - search_results_touchend: (evt) -> + search_results_touchend: (evt) => this.search_results_mouseup(evt) if @touch_started outerHTML: (element) ->