Skip to content

Commit

Permalink
Move the value transfer into the event handlers
Browse files Browse the repository at this point in the history
Before this commit, the work of moving the interim value out of the
`focus_field` and into the `search_field` and then clearing the
`focus_field` was split into separate places:

- `get_search_field_value` was doing the copying.
- and then we had to remember to clear the `focus_field`, if it existed, anytime we cleared the `search_field`

This commit moves all of that into one place, the event handlers where
the `focus_field` passes control back to the rest of Chosen.

Note: the `setTimeout`s are needed for the cut/paste events to make sure the
value being read from the `focus_field` happens after the events finish
changing the input's value.
  • Loading branch information
Arun Srinivasan committed Apr 4, 2018
1 parent e5de6f4 commit 1b359fc
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 14 deletions.
17 changes: 10 additions & 7 deletions coffee/chosen.jquery.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -107,11 +107,16 @@ class Chosen extends AbstractChosen
@container.on 'click.chosen', (evt) -> evt.preventDefault(); return # gobble click of anchor

@focus_field.on 'blur.chosen', (evt) => this.input_blur(evt); return
@focus_field.on 'keyup.chosen', (evt) => this.keyup_checker(evt); return
@focus_field.on 'keydown.chosen', (evt) => this.keydown_checker(evt); return
@focus_field.on 'focus.chosen', (evt) => this.input_focus(evt); return
@focus_field.on 'cut.chosen', (evt) => this.clipboard_event_checker(evt); return
@focus_field.on 'paste.chosen', (evt) => this.clipboard_event_checker(evt); return

transfer_value = () =>
@search_field.val(@search_field.val() + @focus_field.val())
@focus_field.val('')

@focus_field.on 'keyup.chosen', (evt) => transfer_value(); this.keyup_checker(evt); return
@focus_field.on 'keydown.chosen', (evt) => transfer_value(); this.keydown_checker(evt); return
@focus_field.on 'cut.chosen', (evt) => setTimeout(transfer_value, 0); this.clipboard_event_checker(evt); return
@focus_field.on 'paste.chosen', (evt) => setTimeout(transfer_value, 0); this.clipboard_event_checker(evt); return

destroy: ->
$(@container[0].ownerDocument).off 'click.chosen', @click_test_action
Expand Down Expand Up @@ -256,7 +261,6 @@ class Chosen extends AbstractChosen

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

this.winnow_results()
@form_field_jq.trigger("chosen:showing_dropdown", {chosen: this})
Expand Down Expand Up @@ -379,7 +383,6 @@ class Chosen extends AbstractChosen
@form_field.options[item.options_index].selected = true
@selected_option_count = null
@search_field.val("")
@focus_field.val("")

if @is_multiple
this.choice_build item
Expand Down Expand Up @@ -433,7 +436,7 @@ class Chosen extends AbstractChosen
@selected_item.addClass("chosen-single-with-deselect")

get_search_field_value: ->
@search_field.val() + (@focus_field.val() || "")
@search_field.val()

get_search_text: ->
$.trim this.get_search_field_value()
Expand Down
17 changes: 10 additions & 7 deletions coffee/chosen.proto.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -86,11 +86,16 @@ class @Chosen extends AbstractChosen
@container.observe "click", (evt) => evt.preventDefault() # gobble click of anchor

@focus_field.observe "blur", (evt) => this.input_blur(evt)
@focus_field.observe "keyup", (evt) => this.keyup_checker(evt)
@focus_field.observe "keydown", (evt) => this.keydown_checker(evt)
@focus_field.observe "focus", (evt) => this.input_focus(evt)
@focus_field.observe "cut", (evt) => this.clipboard_event_checker(evt)
@focus_field.observe "paste", (evt) => this.clipboard_event_checker(evt)

transfer_value = () =>
@search_field.value = @search_field.value + @focus_field.value
@focus_field.value = ""

@focus_field.observe "keyup", (evt) => transfer_value(); this.keyup_checker(evt)
@focus_field.observe "keydown", (evt) => transfer_value(); this.keydown_checker(evt)
@focus_field.observe "cut", (evt) => setTimeout(transfer_value, 0); this.clipboard_event_checker(evt)
@focus_field.observe "paste", (evt) => setTimeout(transfer_value, 0); this.clipboard_event_checker(evt)

destroy: ->
@container.ownerDocument.stopObserving "click", @click_test_action
Expand Down Expand Up @@ -249,7 +254,6 @@ class @Chosen extends AbstractChosen

@search_field.focus()
@search_field.value = this.get_search_field_value()
@focus_field?.value = ""

this.winnow_results()
@form_field.fire("chosen:showing_dropdown", {chosen: this})
Expand Down Expand Up @@ -372,7 +376,6 @@ class @Chosen extends AbstractChosen
@form_field.options[item.options_index].selected = true
@selected_option_count = null
@search_field.value = ""
@focus_field?.value = ""

if @is_multiple
this.choice_build item
Expand Down Expand Up @@ -425,7 +428,7 @@ class @Chosen extends AbstractChosen
@selected_item.addClassName("chosen-single-with-deselect")

get_search_field_value: ->
@search_field.value + (@focus_field?.value || "")
@search_field.value

get_search_text: ->
this.get_search_field_value().strip()
Expand Down

0 comments on commit 1b359fc

Please sign in to comment.