Skip to content

Commit

Permalink
prevent activating disabled fields (#2721)
Browse files Browse the repository at this point in the history
fixes #2567
  • Loading branch information
koenpunt authored Oct 16, 2016
1 parent 18195a7 commit 183d1d8
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 25 deletions.
29 changes: 16 additions & 13 deletions coffee/chosen.jquery.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -169,6 +170,8 @@ class Chosen extends AbstractChosen
@search_field.blur()

activate_field: ->
return if @is_disabled

@container.addClass "chosen-container-active"
@active_field = true

Expand Down Expand Up @@ -270,7 +273,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
Expand Down
27 changes: 15 additions & 12 deletions coffee/chosen.proto.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -163,6 +164,8 @@ class @Chosen extends AbstractChosen
@search_field.blur()

activate_field: ->
return if @is_disabled

@container.addClassName "chosen-container-active"
@active_field = true

Expand Down Expand Up @@ -261,7 +264,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
Expand Down
6 changes: 6 additions & 0 deletions coffee/lib/abstract-chosen.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 183d1d8

Please sign in to comment.