Skip to content

Commit

Permalink
Always set selected text and classes in a method.
Browse files Browse the repository at this point in the history
It should have been this way from the beginning, but it wasn't. Moving
to a method will move results_otion_build to AbstractChosen
  • Loading branch information
pfiller committed Jul 2, 2013
1 parent d82d344 commit 3d6173a
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 15 deletions.
18 changes: 12 additions & 6 deletions coffee/chosen.jquery.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ class Chosen extends AbstractChosen
if @is_multiple
@search_choices.find("li.search-choice").remove()
else if not @is_multiple
@selected_item.addClass("chzn-default").find("span").text(@default_text)
this.single_set_selected_text()
if @disable_search or @form_field.options.length <= @disable_search_threshold
@search_field.prop('readonly', true)
@container.addClass "chzn-container-single-nosearch"
Expand Down Expand Up @@ -197,7 +197,7 @@ class Chosen extends AbstractChosen
if data.selected and @is_multiple
this.choice_build data
else if data.selected and not @is_multiple
@selected_item.removeClass("chzn-default").find("span").text data.text
this.single_set_selected_text(data.text)
this.single_deselect_control_build() if @allow_single_deselect

@search_results.html content
Expand Down Expand Up @@ -320,8 +320,7 @@ class Chosen extends AbstractChosen
results_reset: ->
@form_field.options[0].selected = true
@selected_option_count = null
@selected_item.find("span").text @default_text
@selected_item.addClass("chzn-default") if not @is_multiple
this.single_set_selected_text()
this.show_search_field_default()
this.results_reset_cleanup()
@form_field_jq.trigger "change"
Expand All @@ -347,7 +346,6 @@ class Chosen extends AbstractChosen
else
@search_results.find(".result-selected").removeClass "result-selected"
@result_single_selected = high
@selected_item.removeClass("chzn-default")

high.addClass "result-selected"

Expand All @@ -361,7 +359,7 @@ class Chosen extends AbstractChosen
if @is_multiple
this.choice_build item
else
@selected_item.find("span").first().text item.text
this.single_set_selected_text(item.text)
this.single_deselect_control_build() if @allow_single_deselect

this.results_hide() unless (evt.metaKey or evt.ctrlKey) and @is_multiple
Expand All @@ -372,6 +370,14 @@ class Chosen extends AbstractChosen
@current_selectedIndex = @form_field.selectedIndex
this.search_field_scale()

single_set_selected_text: (text=@default_text) ->
if text is @default_text
@selected_item.addClass("chzn-default")
else
@selected_item.removeClass("chzn-default")

@selected_item.find("span").text(text)

result_deselect: (pos) ->
result_data = @results_data[pos]

Expand Down
24 changes: 16 additions & 8 deletions public/chosen.jquery.js
Original file line number Diff line number Diff line change
Expand Up @@ -586,7 +586,7 @@
if (this.is_multiple) {
this.search_choices.find("li.search-choice").remove();
} else if (!this.is_multiple) {
this.selected_item.addClass("chzn-default").find("span").text(this.default_text);
this.single_set_selected_text();
if (this.disable_search || this.form_field.options.length <= this.disable_search_threshold) {
this.search_field.prop('readonly', true);
this.container.addClass("chzn-container-single-nosearch");
Expand Down Expand Up @@ -620,7 +620,7 @@
if (data.selected && this.is_multiple) {
this.choice_build(data);
} else if (data.selected && !this.is_multiple) {
this.selected_item.removeClass("chzn-default").find("span").text(data.text);
this.single_set_selected_text(data.text);
if (this.allow_single_deselect) {
this.single_deselect_control_build();
}
Expand Down Expand Up @@ -799,10 +799,7 @@
Chosen.prototype.results_reset = function() {
this.form_field.options[0].selected = true;
this.selected_option_count = null;
this.selected_item.find("span").text(this.default_text);
if (!this.is_multiple) {
this.selected_item.addClass("chzn-default");
}
this.single_set_selected_text();
this.show_search_field_default();
this.results_reset_cleanup();
this.form_field_jq.trigger("change");
Expand Down Expand Up @@ -834,7 +831,6 @@
} else {
this.search_results.find(".result-selected").removeClass("result-selected");
this.result_single_selected = high;
this.selected_item.removeClass("chzn-default");
}
high.addClass("result-selected");
position = high_id.substr(high_id.lastIndexOf("_") + 1);
Expand All @@ -845,7 +841,7 @@
if (this.is_multiple) {
this.choice_build(item);
} else {
this.selected_item.find("span").first().text(item.text);
this.single_set_selected_text(item.text);
if (this.allow_single_deselect) {
this.single_deselect_control_build();
}
Expand All @@ -864,6 +860,18 @@
}
};

Chosen.prototype.single_set_selected_text = function(text) {
if (text == null) {
text = this.default_text;
}
if (text === this.default_text) {
this.selected_item.addClass("chzn-default");
} else {
this.selected_item.removeClass("chzn-default");
}
return this.selected_item.find("span").text(text);
};

Chosen.prototype.result_deselect = function(pos) {
var result, result_data;

Expand Down
Loading

0 comments on commit 3d6173a

Please sign in to comment.