From 922c5c04036cb0b0481426ac2df2b1c9e86281d4 Mon Sep 17 00:00:00 2001 From: Koen Punt Date: Fri, 14 Oct 2016 22:43:24 +0200 Subject: [PATCH] prevent activating disabled fields fixes #2567 --- coffee/chosen.jquery.coffee | 29 ++++++++++++++++------------- coffee/chosen.proto.coffee | 27 +++++++++++++++------------ coffee/lib/abstract-chosen.coffee | 6 ++++++ 3 files changed, 37 insertions(+), 25 deletions(-) diff --git a/coffee/chosen.jquery.coffee b/coffee/chosen.jquery.coffee index 5f441137790..dddc6ceb5d9 100644 --- a/coffee/chosen.jquery.coffee +++ b/coffee/chosen.jquery.coffee @@ -127,20 +127,21 @@ class Chosen extends AbstractChosen @selected_item.bind "focus.chosen", @activate_action if !@is_multiple container_mousedown: (evt) -> - if !@is_disabled - if evt and evt.type is "mousedown" and not @results_showing - evt.preventDefault() + return if @is_disabled - if not (evt? and ($ evt.target).hasClass "search-choice-close") - if not @active_field - @search_field.val "" if @is_multiple - $(@container[0].ownerDocument).bind 'click.chosen', @click_test_action - this.results_show() - else if not @is_multiple and evt and (($(evt.target)[0] == @selected_item[0]) || $(evt.target).parents("a.chosen-single").length) - evt.preventDefault() - this.results_toggle() + if evt and evt.type is "mousedown" and not @results_showing + evt.preventDefault() - this.activate_field() + if not (evt? and ($ evt.target).hasClass "search-choice-close") + if not @active_field + @search_field.val "" if @is_multiple + $(@container[0].ownerDocument).bind 'click.chosen', @click_test_action + this.results_show() + else if not @is_multiple and evt and (($(evt.target)[0] == @selected_item[0]) || $(evt.target).parents("a.chosen-single").length) + evt.preventDefault() + this.results_toggle() + + this.activate_field() container_mouseup: (evt) -> this.results_reset(evt) if evt.target.nodeName is "ABBR" and not @is_disabled @@ -168,6 +169,8 @@ class Chosen extends AbstractChosen this.search_field_scale() activate_field: -> + return if @is_disabled + @container.addClass "chosen-container-active" @active_field = true @@ -269,7 +272,7 @@ class Chosen extends AbstractChosen @form_field_label = $("label[for='#{@form_field.id}']") #next check for a for=#{id} if @form_field_label.length > 0 - @form_field_label.bind 'click.chosen', (evt) => if @is_multiple then this.container_mousedown(evt) else this.activate_field() + @form_field_label.bind 'click.chosen', this.label_click_handler show_search_field_default: -> if @is_multiple and this.choices_count() < 1 and not @active_field diff --git a/coffee/chosen.proto.coffee b/coffee/chosen.proto.coffee index e9c19fac7ba..43e2b2257a3 100644 --- a/coffee/chosen.proto.coffee +++ b/coffee/chosen.proto.coffee @@ -122,19 +122,20 @@ class @Chosen extends AbstractChosen @selected_item.observe "focus", @activate_action if !@is_multiple container_mousedown: (evt) -> - if !@is_disabled - if evt and evt.type is "mousedown" and not @results_showing - evt.stop() + return if @is_disabled - if not (evt? and evt.target.hasClassName "search-choice-close") - if not @active_field - @search_field.clear() if @is_multiple - @container.ownerDocument.observe "click", @click_test_action - this.results_show() - else if not @is_multiple and evt and (evt.target is @selected_item || evt.target.up("a.chosen-single")) - this.results_toggle() + if evt and evt.type is "mousedown" and not @results_showing + evt.stop() - this.activate_field() + if not (evt? and evt.target.hasClassName "search-choice-close") + if not @active_field + @search_field.clear() if @is_multiple + @container.ownerDocument.observe "click", @click_test_action + this.results_show() + else if not @is_multiple and evt and (evt.target is @selected_item || evt.target.up("a.chosen-single")) + this.results_toggle() + + this.activate_field() container_mouseup: (evt) -> this.results_reset(evt) if evt.target.nodeName is "ABBR" and not @is_disabled @@ -162,6 +163,8 @@ class @Chosen extends AbstractChosen this.search_field_scale() activate_field: -> + return if @is_disabled + @container.addClassName "chosen-container-active" @active_field = true @@ -260,7 +263,7 @@ class @Chosen extends AbstractChosen @form_field_label = $$("label[for='#{@form_field.id}']").first() #next check for a for=#{id} if @form_field_label? - @form_field_label.observe "click", (evt) => if @is_multiple then this.container_mousedown(evt) else this.activate_field() + @form_field_label.observe "click", this.label_click_handler show_search_field_default: -> if @is_multiple and this.choices_count() < 1 and not @active_field diff --git a/coffee/lib/abstract-chosen.coffee b/coffee/lib/abstract-chosen.coffee index ba20c3bce7b..a43011e5ff9 100644 --- a/coffee/lib/abstract-chosen.coffee +++ b/coffee/lib/abstract-chosen.coffee @@ -69,6 +69,12 @@ class AbstractChosen @active_field = false setTimeout (=> this.blur_test()), 100 + label_click_handler: (evt) => + if @is_multiple + this.container_mousedown(evt) + else + this.activate_field() + results_option_build: (options) -> content = '' shown_results = 0