Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix external js updates to multi-selects #1070

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
19 changes: 10 additions & 9 deletions chosen/chosen.jquery.js
Expand Up @@ -340,6 +340,10 @@ Copyright (c) 2011 by Harvest
return this.is_rtl = this.form_field_jq.hasClass("chzn-rtl"); return this.is_rtl = this.form_field_jq.hasClass("chzn-rtl");
}; };


Chosen.prototype.choices = function() {
return this.form_field_jq.children('option[selected=true]').length;
};

Chosen.prototype.finish_setup = function() { Chosen.prototype.finish_setup = function() {
return this.form_field_jq.addClass("chzn-done"); return this.form_field_jq.addClass("chzn-done");
}; };
Expand Down Expand Up @@ -540,9 +544,8 @@ Copyright (c) 2011 by Harvest
var content, data, _i, _len, _ref; var content, data, _i, _len, _ref;
this.parsing = true; this.parsing = true;
this.results_data = root.SelectParser.select_to_array(this.form_field); this.results_data = root.SelectParser.select_to_array(this.form_field);
if (this.is_multiple && this.choices > 0) { if (this.is_multiple && this.choices() > 0) {
this.search_choices.find("li.search-choice").remove(); this.search_choices.find("li.search-choice").remove();
this.choices = 0;
} else if (!this.is_multiple) { } else if (!this.is_multiple) {
this.selected_item.addClass("chzn-default").find("span").text(this.default_text); this.selected_item.addClass("chzn-default").find("span").text(this.default_text);
if (this.disable_search || this.form_field.options.length <= this.disable_search_threshold) { if (this.disable_search || this.form_field.options.length <= this.disable_search_threshold) {
Expand Down Expand Up @@ -618,7 +621,7 @@ Copyright (c) 2011 by Harvest
if (this.result_single_selected) { if (this.result_single_selected) {
this.result_do_highlight(this.result_single_selected); this.result_do_highlight(this.result_single_selected);
} }
} else if (this.max_selected_options <= this.choices) { } else if (this.max_selected_options <= this.choices()) {
this.form_field_jq.trigger("liszt:maxselected", { this.form_field_jq.trigger("liszt:maxselected", {
chosen: this chosen: this
}); });
Expand Down Expand Up @@ -662,7 +665,7 @@ Copyright (c) 2011 by Harvest
}; };


Chosen.prototype.show_search_field_default = function() { Chosen.prototype.show_search_field_default = function() {
if (this.is_multiple && this.choices < 1 && !this.active_field) { if (this.is_multiple && this.choices() < 1 && !this.active_field) {
this.search_field.val(this.default_text); this.search_field.val(this.default_text);
return this.search_field.addClass("default"); return this.search_field.addClass("default");
} else { } else {
Expand Down Expand Up @@ -705,14 +708,13 @@ Copyright (c) 2011 by Harvest
Chosen.prototype.choice_build = function(item) { Chosen.prototype.choice_build = function(item) {
var choice_id, html, link, var choice_id, html, link,
_this = this; _this = this;
if (this.is_multiple && this.max_selected_options <= this.choices) { if (this.is_multiple && this.max_selected_options <= this.choices()) {
this.form_field_jq.trigger("liszt:maxselected", { this.form_field_jq.trigger("liszt:maxselected", {
chosen: this chosen: this
}); });
return false; return false;
} }
choice_id = this.container_id + "_c_" + item.array_index; choice_id = this.container_id + "_c_" + item.array_index;
this.choices += 1;
if (item.disabled) { if (item.disabled) {
html = '<li class="search-choice search-choice-disabled" id="' + choice_id + '"><span>' + item.html + '</span></li>'; html = '<li class="search-choice search-choice-disabled" id="' + choice_id + '"><span>' + item.html + '</span></li>';
} else { } else {
Expand All @@ -737,9 +739,8 @@ Copyright (c) 2011 by Harvest


Chosen.prototype.choice_destroy = function(link) { Chosen.prototype.choice_destroy = function(link) {
if (this.result_deselect(link.attr("rel"))) { if (this.result_deselect(link.attr("rel"))) {
this.choices -= 1;
this.show_search_field_default(); this.show_search_field_default();
if (this.is_multiple && this.choices > 0 && this.search_field.val().length < 1) { if (this.is_multiple && this.choices() > 0 && this.search_field.val().length < 1) {
this.results_hide(); this.results_hide();
} }
link.parents('li').first().remove(); link.parents('li').first().remove();
Expand Down Expand Up @@ -970,7 +971,7 @@ Copyright (c) 2011 by Harvest
if (prev_sibs.length) { if (prev_sibs.length) {
return this.result_do_highlight(prev_sibs.first()); return this.result_do_highlight(prev_sibs.first());
} else { } else {
if (this.choices > 0) { if (this.choices() > 0) {
this.results_hide(); this.results_hide();
} }
return this.result_clear_highlight(); return this.result_clear_highlight();
Expand Down
2 changes: 1 addition & 1 deletion chosen/chosen.jquery.min.js

Large diffs are not rendered by default.

21 changes: 11 additions & 10 deletions chosen/chosen.proto.js
Expand Up @@ -319,6 +319,10 @@ Copyright (c) 2011 by Harvest
return this.form_field.addClassName("chzn-done"); return this.form_field.addClassName("chzn-done");
}; };


Chosen.prototype.choices = function() {
return this.form_field.select('option[selected=true]').length;
};

Chosen.prototype.set_default_values = function() { Chosen.prototype.set_default_values = function() {
Chosen.__super__.set_default_values.call(this); Chosen.__super__.set_default_values.call(this);
this.single_temp = new Template('<a href="javascript:void(0)" class="chzn-single chzn-default" tabindex="-1"><span>#{default}</span><div><b></b></div></a><div class="chzn-drop" style="left:-9000px;"><div class="chzn-search"><input type="text" autocomplete="off" /></div><ul class="chzn-results"></ul></div>'); this.single_temp = new Template('<a href="javascript:void(0)" class="chzn-single chzn-default" tabindex="-1"><span>#{default}</span><div><b></b></div></a><div class="chzn-drop" style="left:-9000px;"><div class="chzn-search"><input type="text" autocomplete="off" /></div><ul class="chzn-results"></ul></div>');
Expand Down Expand Up @@ -523,9 +527,8 @@ Copyright (c) 2011 by Harvest
var content, data, _i, _len, _ref; var content, data, _i, _len, _ref;
this.parsing = true; this.parsing = true;
this.results_data = root.SelectParser.select_to_array(this.form_field); this.results_data = root.SelectParser.select_to_array(this.form_field);
if (this.is_multiple && this.choices > 0) { if (this.is_multiple && this.choices() > 0) {
this.search_choices.select("li.search-choice").invoke("remove"); this.search_choices.select("li.search-choice").invoke("remove");
this.choices = 0;
} else if (!this.is_multiple) { } else if (!this.is_multiple) {
this.selected_item.addClassName("chzn-default").down("span").update(this.default_text); this.selected_item.addClassName("chzn-default").down("span").update(this.default_text);
if (this.disable_search || this.form_field.options.length <= this.disable_search_threshold) { if (this.disable_search || this.form_field.options.length <= this.disable_search_threshold) {
Expand Down Expand Up @@ -599,7 +602,7 @@ Copyright (c) 2011 by Harvest
if (this.result_single_selected) { if (this.result_single_selected) {
this.result_do_highlight(this.result_single_selected); this.result_do_highlight(this.result_single_selected);
} }
} else if (this.max_selected_options <= this.choices) { } else if (this.max_selected_options <= this.choices()) {
this.form_field.fire("liszt:maxselected", { this.form_field.fire("liszt:maxselected", {
chosen: this chosen: this
}); });
Expand Down Expand Up @@ -643,7 +646,7 @@ Copyright (c) 2011 by Harvest
}; };


Chosen.prototype.show_search_field_default = function() { Chosen.prototype.show_search_field_default = function() {
if (this.is_multiple && this.choices < 1 && !this.active_field) { if (this.is_multiple && this.choices() < 1 && !this.active_field) {
this.search_field.value = this.default_text; this.search_field.value = this.default_text;
return this.search_field.addClassName("default"); return this.search_field.addClassName("default");
} else { } else {
Expand Down Expand Up @@ -686,14 +689,13 @@ Copyright (c) 2011 by Harvest
Chosen.prototype.choice_build = function(item) { Chosen.prototype.choice_build = function(item) {
var choice_id, link, var choice_id, link,
_this = this; _this = this;
if (this.is_multiple && this.max_selected_options <= this.choices) { if (this.is_multiple && this.max_selected_options <= this.choices()) {
this.form_field.fire("liszt:maxselected", { this.form_field.fire("liszt:maxselected", {
chosen: this chosen: this
}); });
return false; return false;
} }
choice_id = this.container_id + "_c_" + item.array_index; choice_id = this.container_id + "_c_" + item.array_index;
this.choices += 1;
this.search_container.insert({ this.search_container.insert({
before: (item.disabled ? this.choice_noclose_temp : this.choice_temp).evaluate({ before: (item.disabled ? this.choice_noclose_temp : this.choice_temp).evaluate({
id: choice_id, id: choice_id,
Expand All @@ -719,9 +721,8 @@ Copyright (c) 2011 by Harvest


Chosen.prototype.choice_destroy = function(link) { Chosen.prototype.choice_destroy = function(link) {
if (this.result_deselect(link.readAttribute("rel"))) { if (this.result_deselect(link.readAttribute("rel"))) {
this.choices -= 1;
this.show_search_field_default(); this.show_search_field_default();
if (this.is_multiple && this.choices > 0 && this.search_field.value.length < 1) { if (this.is_multiple && this.choices() > 0 && this.search_field.value.length < 1) {
this.results_hide(); this.results_hide();
} }
link.up('li').remove(); link.up('li').remove();
Expand Down Expand Up @@ -916,7 +917,7 @@ Copyright (c) 2011 by Harvest
if (!this.is_multiple) { if (!this.is_multiple) {
do_high = this.search_results.down(".result-selected.active-result"); do_high = this.search_results.down(".result-selected.active-result");
} }
if (!(do_high != null)) { if (do_high == null) {
do_high = this.search_results.down(".active-result"); do_high = this.search_results.down(".active-result");
} }
if (do_high != null) { if (do_high != null) {
Expand Down Expand Up @@ -971,7 +972,7 @@ Copyright (c) 2011 by Harvest
if (prevs.length) { if (prevs.length) {
return this.result_do_highlight(prevs.first()); return this.result_do_highlight(prevs.first());
} else { } else {
if (this.choices > 0) { if (this.choices() > 0) {
this.results_hide(); this.results_hide();
} }
return this.result_clear_highlight(); return this.result_clear_highlight();
Expand Down
2 changes: 1 addition & 1 deletion chosen/chosen.proto.min.js

Large diffs are not rendered by default.

24 changes: 12 additions & 12 deletions coffee/chosen.jquery.coffee
Expand Up @@ -10,11 +10,11 @@ $.fn.extend({
ua = navigator.userAgent.toLowerCase(); ua = navigator.userAgent.toLowerCase();


match = /(msie) ([\w.]+)/.exec( ua ) || []; match = /(msie) ([\w.]+)/.exec( ua ) || [];

browser = browser =
name: match[ 1 ] || "" name: match[ 1 ] || ""
version: match[ 2 ] || "0" version: match[ 2 ] || "0"

# Do no harm and return as soon as possible for unsupported browsers, namely IE6 and IE7 # Do no harm and return as soon as possible for unsupported browsers, namely IE6 and IE7
# Continue on if running IE document type but in compatibility mode # Continue on if running IE document type but in compatibility mode
return this if browser.name is "msie" and (browser.version is "6.0" or (browser.version is "7.0" and document.documentMode is 7 )) return this if browser.name is "msie" and (browser.version is "6.0" or (browser.version is "7.0" and document.documentMode is 7 ))
Expand All @@ -31,6 +31,9 @@ class Chosen extends AbstractChosen
@current_value = @form_field_jq.val() @current_value = @form_field_jq.val()
@is_rtl = @form_field_jq.hasClass "chzn-rtl" @is_rtl = @form_field_jq.hasClass "chzn-rtl"


choices: ->
return @form_field_jq.children('option[selected=true]').length

finish_setup: -> finish_setup: ->
@form_field_jq.addClass "chzn-done" @form_field_jq.addClass "chzn-done"


Expand All @@ -45,7 +48,7 @@ class Chosen extends AbstractChosen


@f_width = @form_field_jq.outerWidth() @f_width = @form_field_jq.outerWidth()


container_props = container_props =
id: @container_id id: @container_id
class: container_classes.join ' ' class: container_classes.join ' '
style: 'width: ' + (@f_width) + 'px;' #use parens around @f_width so coffeescript doesn't think + ' px' is a function parameter style: 'width: ' + (@f_width) + 'px;' #use parens around @f_width so coffeescript doesn't think + ' px' is a function parameter
Expand Down Expand Up @@ -178,9 +181,8 @@ class Chosen extends AbstractChosen
@parsing = true @parsing = true
@results_data = root.SelectParser.select_to_array @form_field @results_data = root.SelectParser.select_to_array @form_field


if @is_multiple and @choices > 0 if @is_multiple and this.choices() > 0
@search_choices.find("li.search-choice").remove() @search_choices.find("li.search-choice").remove()
@choices = 0
else if not @is_multiple else if not @is_multiple
@selected_item.addClass("chzn-default").find("span").text(@default_text) @selected_item.addClass("chzn-default").find("span").text(@default_text)
if @disable_search or @form_field.options.length <= @disable_search_threshold if @disable_search or @form_field.options.length <= @disable_search_threshold
Expand Down Expand Up @@ -243,7 +245,7 @@ class Chosen extends AbstractChosen
@selected_item.addClass "chzn-single-with-drop" @selected_item.addClass "chzn-single-with-drop"
if @result_single_selected if @result_single_selected
this.result_do_highlight( @result_single_selected ) this.result_do_highlight( @result_single_selected )
else if @max_selected_options <= @choices else if @max_selected_options <= this.choices()
@form_field_jq.trigger("liszt:maxselected", {chosen: this}) @form_field_jq.trigger("liszt:maxselected", {chosen: this})
return false return false


Expand Down Expand Up @@ -272,7 +274,7 @@ class Chosen extends AbstractChosen
@search_field.attr "tabindex", ti @search_field.attr "tabindex", ti


show_search_field_default: -> show_search_field_default: ->
if @is_multiple and @choices < 1 and not @active_field if @is_multiple and this.choices() < 1 and not @active_field
@search_field.val(@default_text) @search_field.val(@default_text)
@search_field.addClass "default" @search_field.addClass "default"
else else
Expand Down Expand Up @@ -300,11 +302,10 @@ class Chosen extends AbstractChosen
this.results_show() this.results_show()


choice_build: (item) -> choice_build: (item) ->
if @is_multiple and @max_selected_options <= @choices if @is_multiple and @max_selected_options <= this.choices()
@form_field_jq.trigger("liszt:maxselected", {chosen: this}) @form_field_jq.trigger("liszt:maxselected", {chosen: this})
return false # fire event return false # fire event
choice_id = @container_id + "_c_" + item.array_index choice_id = @container_id + "_c_" + item.array_index
@choices += 1
if item.disabled if item.disabled
html = '<li class="search-choice search-choice-disabled" id="' + choice_id + '"><span>' + item.html + '</span></li>' html = '<li class="search-choice search-choice-disabled" id="' + choice_id + '"><span>' + item.html + '</span></li>'
else else
Expand All @@ -323,10 +324,9 @@ class Chosen extends AbstractChosen


choice_destroy: (link) -> choice_destroy: (link) ->
if this.result_deselect (link.attr "rel") if this.result_deselect (link.attr "rel")
@choices -= 1
this.show_search_field_default() this.show_search_field_default()


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


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


Expand Down Expand Up @@ -508,7 +508,7 @@ class Chosen extends AbstractChosen
if prev_sibs.length if prev_sibs.length
this.result_do_highlight prev_sibs.first() this.result_do_highlight prev_sibs.first()
else else
this.results_hide() if @choices > 0 this.results_hide() if this.choices() > 0
this.result_clear_highlight() this.result_clear_highlight()


keydown_backstroke: -> keydown_backstroke: ->
Expand Down
20 changes: 10 additions & 10 deletions coffee/chosen.proto.coffee
Expand Up @@ -13,6 +13,9 @@ class Chosen extends AbstractChosen
finish_setup: -> finish_setup: ->
@form_field.addClassName "chzn-done" @form_field.addClassName "chzn-done"


choices: ->
return @form_field.select('option[selected=true]').length

set_default_values: -> set_default_values: ->
super() super()


Expand All @@ -38,7 +41,7 @@ class Chosen extends AbstractChosen
'class': container_classes.join ' ' 'class': container_classes.join ' '
'style': 'width: ' + (@f_width) + 'px' #use parens around @f_width so coffeescript doesn't think + ' px' is a function parameter 'style': 'width: ' + (@f_width) + 'px' #use parens around @f_width so coffeescript doesn't think + ' px' is a function parameter
'title': @form_field.title 'title': @form_field.title

base_template = if @is_multiple then new Element('div', container_props).update( @multi_temp.evaluate({ "default": @default_text}) ) else new Element('div', container_props).update( @single_temp.evaluate({ "default":@default_text }) ) base_template = if @is_multiple then new Element('div', container_props).update( @multi_temp.evaluate({ "default": @default_text}) ) else new Element('div', container_props).update( @single_temp.evaluate({ "default":@default_text }) )


@form_field.hide().insert({ after: base_template }) @form_field.hide().insert({ after: base_template })
Expand Down Expand Up @@ -158,9 +161,8 @@ class Chosen extends AbstractChosen
@parsing = true @parsing = true
@results_data = root.SelectParser.select_to_array @form_field @results_data = root.SelectParser.select_to_array @form_field


if @is_multiple and @choices > 0 if @is_multiple and this.choices() > 0
@search_choices.select("li.search-choice").invoke("remove") @search_choices.select("li.search-choice").invoke("remove")
@choices = 0
else if not @is_multiple else if not @is_multiple
@selected_item.addClassName("chzn-default").down("span").update(@default_text) @selected_item.addClassName("chzn-default").down("span").update(@default_text)
if @disable_search or @form_field.options.length <= @disable_search_threshold if @disable_search or @form_field.options.length <= @disable_search_threshold
Expand Down Expand Up @@ -222,7 +224,7 @@ class Chosen extends AbstractChosen
@selected_item.addClassName('chzn-single-with-drop') @selected_item.addClassName('chzn-single-with-drop')
if @result_single_selected if @result_single_selected
this.result_do_highlight( @result_single_selected ) this.result_do_highlight( @result_single_selected )
else if @max_selected_options <= @choices else if @max_selected_options <= this.choices()
@form_field.fire("liszt:maxselected", {chosen: this}) @form_field.fire("liszt:maxselected", {chosen: this})
return false return false


Expand Down Expand Up @@ -251,7 +253,7 @@ class Chosen extends AbstractChosen
@search_field.tabIndex = ti @search_field.tabIndex = ti


show_search_field_default: -> show_search_field_default: ->
if @is_multiple and @choices < 1 and not @active_field if @is_multiple and this.choices() < 1 and not @active_field
@search_field.value = @default_text @search_field.value = @default_text
@search_field.addClassName "default" @search_field.addClassName "default"
else else
Expand Down Expand Up @@ -279,11 +281,10 @@ class Chosen extends AbstractChosen
this.results_show() this.results_show()


choice_build: (item) -> choice_build: (item) ->
if @is_multiple and @max_selected_options <= @choices if @is_multiple and @max_selected_options <= this.choices()
@form_field.fire("liszt:maxselected", {chosen: this}) @form_field.fire("liszt:maxselected", {chosen: this})
return false return false
choice_id = @container_id + "_c_" + item.array_index choice_id = @container_id + "_c_" + item.array_index
@choices += 1
@search_container.insert @search_container.insert
before: (if item.disabled then @choice_noclose_temp else @choice_temp).evaluate before: (if item.disabled then @choice_noclose_temp else @choice_temp).evaluate
id: choice_id id: choice_id
Expand All @@ -301,10 +302,9 @@ class Chosen extends AbstractChosen


choice_destroy: (link) -> choice_destroy: (link) ->
if this.result_deselect link.readAttribute("rel") if this.result_deselect link.readAttribute("rel")
@choices -= 1
this.show_search_field_default() this.show_search_field_default()


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


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


Expand Down Expand Up @@ -490,7 +490,7 @@ class Chosen extends AbstractChosen
if prevs.length if prevs.length
this.result_do_highlight prevs.first() this.result_do_highlight prevs.first()
else else
this.results_hide() if @choices > 0 this.results_hide() if this.choices() > 0
this.result_clear_highlight() this.result_clear_highlight()


keydown_backstroke: -> keydown_backstroke: ->
Expand Down