Skip to content

Commit

Permalink
Merge pull request #2713 from koenpunt/escape
Browse files Browse the repository at this point in the history
prevent bubbling of escape key
  • Loading branch information
koenpunt authored Oct 11, 2016
2 parents 7259f35 + b05d09e commit 36c0617
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 73 deletions.
40 changes: 7 additions & 33 deletions coffee/chosen.jquery.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,7 @@ class Chosen extends AbstractChosen
@results_showing = true

@search_field.focus()
@search_field.val @search_field.val()
@search_field.val this.get_search_field_value()

this.winnow_results()
@form_field_jq.trigger("chosen:showing_dropdown", {chosen: this})
Expand Down Expand Up @@ -312,7 +312,7 @@ class Chosen extends AbstractChosen
if this.result_deselect( link[0].getAttribute("data-option-array-index") )
this.show_search_field_default()

this.results_hide() if @is_multiple and this.choices_count() > 0 and @search_field.val().length < 1
this.results_hide() if @is_multiple and this.choices_count() > 0 and this.get_search_field_value().length < 1

link.parents('li').first().remove()

Expand Down Expand Up @@ -402,8 +402,11 @@ class Chosen extends AbstractChosen
@selected_item.find("span").first().after "<abbr class=\"search-choice-close\"></abbr>" unless @selected_item.find("abbr").length
@selected_item.addClass("chosen-single-with-deselect")

get_search_field_value: ->
@search_field.val()

get_search_text: ->
$('<div/>').text($.trim(@search_field.val())).html()
$('<div/>').text($.trim(this.get_search_field_value())).html()

winnow_results_set_highlight: ->
selected_results = if not @is_multiple then @search_results.find(".result-selected.active-result") else []
Expand Down Expand Up @@ -457,35 +460,6 @@ class Chosen extends AbstractChosen
@pending_backstroke.removeClass "search-choice-focus" if @pending_backstroke
@pending_backstroke = null

keydown_checker: (evt) ->
stroke = evt.which ? evt.keyCode
this.search_field_scale()

this.clear_backstroke() if stroke != 8 and this.pending_backstroke

switch stroke
when 8
@backstroke_length = this.search_field.val().length
break
when 9
this.result_select(evt) if this.results_showing and not @is_multiple
@mouse_on_container = false
break
when 13
evt.preventDefault() if this.results_showing
break
when 32
evt.preventDefault() if @disable_search
break
when 38
evt.preventDefault()
this.keyup_arrow()
break
when 40
evt.preventDefault()
this.keydown_arrow()
break

search_field_scale: ->
if @is_multiple
h = 0
Expand All @@ -498,7 +472,7 @@ class Chosen extends AbstractChosen
style_block += style + ":" + @search_field.css(style) + ";"

div = $('<div />', { 'style' : style_block })
div.text @search_field.val()
div.text this.get_search_field_value()
$('body').append div

w = div.width() + 25
Expand Down
42 changes: 8 additions & 34 deletions coffee/chosen.proto.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ class @Chosen extends AbstractChosen
@container.addClassName "chosen-container-active"
@active_field = true

@search_field.value = @search_field.value
@search_field.value = this.get_search_field_value()
@search_field.focus()

test_active_click: (evt) ->
Expand Down Expand Up @@ -228,7 +228,7 @@ class @Chosen extends AbstractChosen
@results_showing = true

@search_field.focus()
@search_field.value = @search_field.value
@search_field.value = this.get_search_field_value()

this.winnow_results()
@form_field.fire("chosen:showing_dropdown", {chosen: this})
Expand Down Expand Up @@ -303,7 +303,7 @@ class @Chosen extends AbstractChosen
if this.result_deselect link.readAttribute("rel")
this.show_search_field_default()

this.results_hide() if @is_multiple and this.choices_count() > 0 and @search_field.value.length < 1
this.results_hide() if @is_multiple and this.choices_count() > 0 and this.get_search_field_value().length < 1

link.up('li').remove()

Expand Down Expand Up @@ -392,8 +392,11 @@ class @Chosen extends AbstractChosen
@selected_item.down("span").insert { after: "<abbr class=\"search-choice-close\"></abbr>" } unless @selected_item.down("abbr")
@selected_item.addClassName("chosen-single-with-deselect")

get_search_field_value: ->
@search_field.value

get_search_text: ->
@search_field.value.strip().escapeHTML()
this.get_search_field_value().strip().escapeHTML()

winnow_results_set_highlight: ->
if not @is_multiple
Expand Down Expand Up @@ -452,35 +455,6 @@ class @Chosen extends AbstractChosen
@pending_backstroke.removeClassName("search-choice-focus") if @pending_backstroke
@pending_backstroke = null

keydown_checker: (evt) ->
stroke = evt.which ? evt.keyCode
this.search_field_scale()

this.clear_backstroke() if stroke != 8 and this.pending_backstroke

switch stroke
when 8
@backstroke_length = this.search_field.value.length
break
when 9
this.result_select(evt) if this.results_showing and not @is_multiple
@mouse_on_container = false
break
when 13
evt.preventDefault() if this.results_showing
break
when 32
evt.preventDefault() if @disable_search
break
when 38
evt.preventDefault()
this.keyup_arrow()
break
when 40
evt.preventDefault()
this.keydown_arrow()
break

search_field_scale: ->
if @is_multiple
h = 0
Expand All @@ -492,7 +466,7 @@ class @Chosen extends AbstractChosen
for style in styles
style_block += style + ":" + @search_field.getStyle(style) + ";"

div = new Element('div', { 'style' : style_block }).update(@search_field.value.escapeHTML())
div = new Element('div', { 'style' : style_block }).update(this.get_search_field_value().escapeHTML())
document.body.appendChild(div)

w = Element.measure(div, 'width') + 25
Expand Down
48 changes: 42 additions & 6 deletions coffee/lib/abstract-chosen.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -237,26 +237,62 @@ class AbstractChosen
evt.preventDefault()
this.results_show() unless @results_showing or @is_disabled

keydown_checker: (evt) ->
stroke = evt.which ? evt.keyCode
this.search_field_scale()

this.clear_backstroke() if stroke != 8 and @pending_backstroke

switch stroke
when 8 # backspace
@backstroke_length = this.get_search_field_value().length
break
when 9 # tab
this.result_select(evt) if @results_showing and not @is_multiple
@mouse_on_container = false
break
when 13 # enter
evt.preventDefault() if @results_showing
break
when 27 # escape
evt.preventDefault() if @results_showing
break
when 32 # space
evt.preventDefault() if @disable_search
break
when 38 # up arrow
evt.preventDefault()
this.keyup_arrow()
break
when 40 # down arrow
evt.preventDefault()
this.keydown_arrow()
break

keyup_checker: (evt) ->
stroke = evt.which ? evt.keyCode
this.search_field_scale()

switch stroke
when 8
when 8 # backspace
if @is_multiple and @backstroke_length < 1 and this.choices_count() > 0
this.keydown_backstroke()
else if not @pending_backstroke
this.result_clear_highlight()
this.results_search()
when 13
break
when 13 # enter
evt.preventDefault()
this.result_select(evt) if this.results_showing
when 27
break
when 27 # escape
this.results_hide() if @results_showing
return true
when 9, 38, 40, 16, 91, 17, 18
break
when 9, 16, 17, 18, 38, 40, 91
# don't do anything on these keys
else this.results_search()
else
this.results_search()
break

clipboard_event_checker: (evt) ->
setTimeout (=> this.results_search()), 50
Expand Down

0 comments on commit 36c0617

Please sign in to comment.