From 0431f6a13c97d2a19ded69f14e93600c8d34bf3d Mon Sep 17 00:00:00 2001 From: Koen Punt Date: Sun, 2 Oct 2016 19:47:53 +0200 Subject: [PATCH 1/4] add method to retrieve search field value --- coffee/chosen.jquery.coffee | 11 +++++++---- coffee/chosen.proto.coffee | 13 ++++++++----- 2 files changed, 15 insertions(+), 9 deletions(-) diff --git a/coffee/chosen.jquery.coffee b/coffee/chosen.jquery.coffee index 004e0441d6d..f1cc0b739b6 100644 --- a/coffee/chosen.jquery.coffee +++ b/coffee/chosen.jquery.coffee @@ -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}) @@ -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() @@ -402,8 +402,11 @@ class Chosen extends AbstractChosen @selected_item.find("span").first().after "" unless @selected_item.find("abbr").length @selected_item.addClass("chosen-single-with-deselect") + get_search_field_value: -> + @search_field.val() + get_search_text: -> - $('
').text($.trim(@search_field.val())).html() + $('
').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 [] @@ -498,7 +501,7 @@ class Chosen extends AbstractChosen style_block += style + ":" + @search_field.css(style) + ";" div = $('
', { 'style' : style_block }) - div.text @search_field.val() + div.text this.get_search_field_value() $('body').append div w = div.width() + 25 diff --git a/coffee/chosen.proto.coffee b/coffee/chosen.proto.coffee index e5b1185d674..a879915455d 100644 --- a/coffee/chosen.proto.coffee +++ b/coffee/chosen.proto.coffee @@ -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) -> @@ -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}) @@ -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() @@ -392,8 +392,11 @@ class @Chosen extends AbstractChosen @selected_item.down("span").insert { after: "" } 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 @@ -492,7 +495,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 From 926369a1df90ccb8959bbfd329f7be9b4ebf4172 Mon Sep 17 00:00:00 2001 From: Koen Punt Date: Sun, 2 Oct 2016 19:53:16 +0200 Subject: [PATCH 2/4] move keydown_checker to AbstractChosen --- coffee/chosen.jquery.coffee | 29 ----------------------------- coffee/chosen.proto.coffee | 29 ----------------------------- coffee/lib/abstract-chosen.coffee | 29 +++++++++++++++++++++++++++++ 3 files changed, 29 insertions(+), 58 deletions(-) diff --git a/coffee/chosen.jquery.coffee b/coffee/chosen.jquery.coffee index f1cc0b739b6..df4e1988069 100644 --- a/coffee/chosen.jquery.coffee +++ b/coffee/chosen.jquery.coffee @@ -460,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 diff --git a/coffee/chosen.proto.coffee b/coffee/chosen.proto.coffee index a879915455d..e58bbd90cce 100644 --- a/coffee/chosen.proto.coffee +++ b/coffee/chosen.proto.coffee @@ -455,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 diff --git a/coffee/lib/abstract-chosen.coffee b/coffee/lib/abstract-chosen.coffee index 6b1c3d51eb0..a2d43c17cff 100644 --- a/coffee/lib/abstract-chosen.coffee +++ b/coffee/lib/abstract-chosen.coffee @@ -237,6 +237,35 @@ 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 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() From 06ef830b6781413a7cf632f9fe60ce32d2ccc0ce Mon Sep 17 00:00:00 2001 From: Koen Punt Date: Sun, 2 Oct 2016 19:52:23 +0200 Subject: [PATCH 3/4] update keyup_checker add breaks and comments --- coffee/lib/abstract-chosen.coffee | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/coffee/lib/abstract-chosen.coffee b/coffee/lib/abstract-chosen.coffee index a2d43c17cff..5420fd98117 100644 --- a/coffee/lib/abstract-chosen.coffee +++ b/coffee/lib/abstract-chosen.coffee @@ -271,21 +271,25 @@ class AbstractChosen 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 From b05d09edbc6e16f64af0f3f8b6d2bb3c89411a61 Mon Sep 17 00:00:00 2001 From: Koen Punt Date: Sun, 2 Oct 2016 19:53:43 +0200 Subject: [PATCH 4/4] prevent event bubbling on escape when results are showing --- coffee/lib/abstract-chosen.coffee | 3 +++ 1 file changed, 3 insertions(+) diff --git a/coffee/lib/abstract-chosen.coffee b/coffee/lib/abstract-chosen.coffee index 5420fd98117..4dd306a2124 100644 --- a/coffee/lib/abstract-chosen.coffee +++ b/coffee/lib/abstract-chosen.coffee @@ -254,6 +254,9 @@ class AbstractChosen 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