Skip to content

Commit

Permalink
Move disabled destroy prevention into result_deselect function.
Browse files Browse the repository at this point in the history
  • Loading branch information
Patrick Filler committed May 26, 2012
1 parent aaa627e commit fb0d37f
Show file tree
Hide file tree
Showing 8 changed files with 103 additions and 66 deletions.
55 changes: 35 additions & 20 deletions chosen/chosen.jquery.js
Original file line number Diff line number Diff line change
Expand Up @@ -659,7 +659,7 @@ Copyright (c) 2011 by Harvest
};

Chosen.prototype.choice_build = function(item) {
var choice_id, link,
var choice_id, html, link,
_this = this;
if (this.is_multiple && this.max_selected_options <= this.choices) {
this.form_field_jq.trigger("liszt:maxselected", {
Expand All @@ -669,7 +669,12 @@ Copyright (c) 2011 by Harvest
}
choice_id = this.container_id + "_c_" + item.array_index;
this.choices += 1;
this.search_container.before('<li class="search-choice" id="' + choice_id + '"><span>' + item.html + '</span><a href="javascript:void(0)" class="search-choice-close" rel="' + item.array_index + '"></a></li>');
if (item.disabled) {
html = '<li class="search-choice search-choice-disabled" id="' + choice_id + '"><span>' + item.html + '</span></li>';
} else {
html = '<li class="search-choice" id="' + choice_id + '"><span>' + item.html + '</span><a href="javascript:void(0)" class="search-choice-close" rel="' + item.array_index + '"></a></li>';
}
this.search_container.before(html);
link = $('#' + choice_id).find("a").first();
return link.click(function(evt) {
return _this.choice_destroy_link_click(evt);
Expand All @@ -687,13 +692,14 @@ Copyright (c) 2011 by Harvest
};

Chosen.prototype.choice_destroy = function(link) {
this.choices -= 1;
this.show_search_field_default();
if (this.is_multiple && this.choices > 0 && this.search_field.val().length < 1) {
this.results_hide();
if (this.result_deselect(link.attr("rel"))) {
this.choices -= 1;
this.show_search_field_default();
if (this.is_multiple && this.choices > 0 && this.search_field.val().length < 1) {
this.results_hide();
}
return link.parents('li').first().remove();
}
this.result_deselect(link.attr("rel"));
return link.parents('li').first().remove();
};

Chosen.prototype.results_reset = function() {
Expand Down Expand Up @@ -757,16 +763,21 @@ Copyright (c) 2011 by Harvest
Chosen.prototype.result_deselect = function(pos) {
var result, result_data;
result_data = this.results_data[pos];
result_data.selected = false;
this.form_field.options[result_data.options_index].selected = false;
result = $("#" + this.container_id + "_o_" + pos);
result.removeClass("result-selected").addClass("active-result").show();
this.result_clear_highlight();
this.winnow_results();
this.form_field_jq.trigger("change", {
deselected: this.form_field.options[result_data.options_index].value
});
return this.search_field_scale();
if (!this.form_field.options[result_data.options_index].disabled) {
result_data.selected = false;
this.form_field.options[result_data.options_index].selected = false;
result = $("#" + this.container_id + "_o_" + pos);
result.removeClass("result-selected").addClass("active-result").show();
this.result_clear_highlight();
this.winnow_results();
this.form_field_jq.trigger("change", {
deselected: this.form_field.options[result_data.options_index].value
});
this.search_field_scale();
return true;
} else {
return false;
}
};

Chosen.prototype.single_deselect_control_build = function() {
Expand Down Expand Up @@ -904,12 +915,16 @@ Copyright (c) 2011 by Harvest
};

Chosen.prototype.keydown_backstroke = function() {
var next_available_destroy;
if (this.pending_backstroke) {
this.choice_destroy(this.pending_backstroke.find("a").first());
return this.clear_backstroke();
} else {
this.pending_backstroke = this.search_container.siblings("li.search-choice").last();
return this.pending_backstroke.addClass("search-choice-focus");
next_available_destroy = this.search_container.siblings("li.search-choice").last();
if (next_available_destroy.length && !next_available_destroy.hasClass("search-choice-disabled")) {
this.pending_backstroke = next_available_destroy;
return this.pending_backstroke.addClass("search-choice-focus");
}
}
};

Expand Down
2 changes: 1 addition & 1 deletion chosen/chosen.jquery.min.js

Large diffs are not rendered by default.

25 changes: 17 additions & 8 deletions chosen/chosen.proto.js
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,7 @@ Copyright (c) 2011 by Harvest
this.single_temp = new Template('<a href="javascript:void(0)" class="chzn-single chzn-default"><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.multi_temp = new Template('<ul class="chzn-choices"><li class="search-field"><input type="text" value="#{default}" class="default" autocomplete="off" style="width:25px;" /></li></ul><div class="chzn-drop" style="left:-9000px;"><ul class="chzn-results"></ul></div>');
this.choice_temp = new Template('<li class="search-choice" id="#{id}"><span>#{choice}</span><a href="javascript:void(0)" class="search-choice-close" rel="#{position}"></a></li>');
this.choice_noclose_temp = new Template('<li class="search-choice search-choice-disabled" id="#{id}"><span>#{choice}</span></li>');
return this.no_results_temp = new Template('<li class="no-results">' + this.results_none_found + ' "<span>#{terms}</span>"</li>');
};

Expand Down Expand Up @@ -658,21 +659,25 @@ Copyright (c) 2011 by Harvest
choice_id = this.container_id + "_c_" + item.array_index;
this.choices += 1;
this.search_container.insert({
before: this.choice_temp.evaluate({
before: (item.disabled ? this.choice_noclose_temp : this.choice_temp).evaluate({
id: choice_id,
choice: item.html,
position: item.array_index
})
});
link = $(choice_id).down('a');
return link.observe("click", function(evt) {
return _this.choice_destroy_link_click(evt);
});
if (!item.disabled) {
link = $(choice_id).down('a');
return link.observe("click", function(evt) {
return _this.choice_destroy_link_click(evt);
});
}
};

Chosen.prototype.choice_destroy_link_click = function(evt) {
var result_data;
evt.preventDefault();
if (!this.is_disabled) {
result_data = this.results_data[evt.target.readAttribute("rel")];
if (!this.is_disabled && !result_data.disabled) {
this.pending_destroy_click = true;
return this.choice_destroy(evt.target);
}
Expand Down Expand Up @@ -913,8 +918,12 @@ Copyright (c) 2011 by Harvest
this.choice_destroy(this.pending_backstroke.down("a"));
return this.clear_backstroke();
} else {
this.pending_backstroke = this.search_container.siblings("li.search-choice").last();
return this.pending_backstroke.addClassName("search-choice-focus");
this.pending_backstroke = this.search_container.siblings().reject(function(x) {
return x.hasClassName("search-choice-disabled");
}).last();
if (this.pending_backstroke) {
return this.pending_backstroke.addClassName("search-choice-focus");
}
}
};

Expand Down
2 changes: 1 addition & 1 deletion chosen/chosen.proto.min.js

Large diffs are not rendered by default.

41 changes: 24 additions & 17 deletions coffee/chosen.jquery.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -309,21 +309,20 @@ class Chosen extends AbstractChosen

choice_destroy_link_click: (evt) ->
evt.preventDefault()
result_data = @results_data[$(evt.target).attr "rel"]
if not @is_disabled and not result_data.disabled
if not @is_disabled
@pending_destroy_click = true
this.choice_destroy $(evt.target)
else
evt.stopPropagation

choice_destroy: (link) ->
@choices -= 1
this.show_search_field_default()
if this.result_deselect (link.attr "rel")
@choices -= 1
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 @choices > 0 and @search_field.val().length < 1

this.result_deselect (link.attr "rel")
link.parents('li').first().remove()
link.parents('li').first().remove()

results_reset: ->
@form_field.options[0].selected = true
Expand Down Expand Up @@ -381,17 +380,23 @@ class Chosen extends AbstractChosen

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

@form_field.options[result_data.options_index].selected = false
result = $("#" + @container_id + "_o_" + pos)
result.removeClass("result-selected").addClass("active-result").show()
if not @form_field.options[result_data.options_index].disabled
result_data.selected = false

@form_field.options[result_data.options_index].selected = false
result = $("#" + @container_id + "_o_" + pos)
result.removeClass("result-selected").addClass("active-result").show()

this.result_clear_highlight()
this.winnow_results()
this.result_clear_highlight()
this.winnow_results()

@form_field_jq.trigger "change", {deselected: @form_field.options[result_data.options_index].value}
this.search_field_scale()
@form_field_jq.trigger "change", {deselected: @form_field.options[result_data.options_index].value}
this.search_field_scale()

return true
else
return false

single_deselect_control_build: ->
@selected_item.find("span").first().after "<abbr class=\"search-choice-close\"></abbr>" if @allow_single_deselect and @selected_item.find("abbr").length < 1
Expand Down Expand Up @@ -502,8 +507,10 @@ class Chosen extends AbstractChosen
this.choice_destroy @pending_backstroke.find("a").first()
this.clear_backstroke()
else
@pending_backstroke = @search_container.siblings("li.search-choice:not(.search-choice-disabled)").last()
@pending_backstroke.addClass "search-choice-focus"
next_available_destroy = @search_container.siblings("li.search-choice").last()
if next_available_destroy.length and not next_available_destroy.hasClass("search-choice-disabled")
@pending_backstroke = next_available_destroy
@pending_backstroke.addClass "search-choice-focus"

clear_backstroke: ->
@pending_backstroke.removeClass "search-choice-focus" if @pending_backstroke
Expand Down
40 changes: 23 additions & 17 deletions coffee/chosen.proto.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -301,19 +301,18 @@ class Chosen extends AbstractChosen

choice_destroy_link_click: (evt) ->
evt.preventDefault()
result_data = @results_data[evt.target.readAttribute("rel")]
if not @is_disabled and not result_data.disabled
if not @is_disabled
@pending_destroy_click = true
this.choice_destroy evt.target

choice_destroy: (link) ->
@choices -= 1
this.show_search_field_default()
if this.result_deselect link.readAttribute("rel")
@choices -= 1
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 @choices > 0 and @search_field.value.length < 1

this.result_deselect link.readAttribute("rel")
link.up('li').remove()
link.up('li').remove()

results_reset: ->
@form_field.options[0].selected = true
Expand Down Expand Up @@ -371,17 +370,22 @@ class Chosen extends AbstractChosen

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

if not @form_field.options[result_data.options_index].disabled
result_data.selected = false

@form_field.options[result_data.options_index].selected = false
result = $(@container_id + "_o_" + pos)
result.removeClassName("result-selected").addClassName("active-result").show()
@form_field.options[result_data.options_index].selected = false
result = $(@container_id + "_o_" + pos)
result.removeClassName("result-selected").addClassName("active-result").show()

this.result_clear_highlight()
this.winnow_results()
this.result_clear_highlight()
this.winnow_results()

@form_field.simulate("change") if typeof Event.simulate is 'function'
this.search_field_scale()
@form_field.simulate("change") if typeof Event.simulate is 'function'
this.search_field_scale()
return true
else
return false

single_deselect_control_build: ->
@selected_item.down("span").insert { after: "<abbr class=\"search-choice-close\"></abbr>" } if @allow_single_deselect and not @selected_item.down("abbr")
Expand Down Expand Up @@ -497,9 +501,11 @@ class Chosen extends AbstractChosen
this.choice_destroy @pending_backstroke.down("a")
this.clear_backstroke()
else
@pending_backstroke = @search_container.siblings().reject( (x) -> x.hasClassName("search-choice-disabled") ).last()
next_available_destroy = @search_container.siblings().last()
if next_available_destroy.length and next_available_destroy.hasClassName("search-choice") and not next_available_destroy.hasClassName("search-choice-disabled")
@pending_backstroke = next_available_destroy
@pending_backstroke.addClassName("search-choice-focus") if @pending_backstroke

clear_backstroke: ->
@pending_backstroke.removeClassName("search-choice-focus") if @pending_backstroke
@pending_backstroke = null
Expand Down
2 changes: 1 addition & 1 deletion example.jquery.html
Original file line number Diff line number Diff line change
Expand Up @@ -1212,7 +1212,7 @@ <h2>Selected and Disabled Support</h2>
<option>Asiatic Black Bear</option>
<option>Brown Bear</option>
<option>Giant Panda</option>
<option selected>Sloth Bear</option>
<option selected disabled>Sloth Bear</option>
<option disabled>Sun Bear</option>
<option selected disabled>Paddington Bear</option>
<option selected>Polar Bear</option>
Expand Down
2 changes: 1 addition & 1 deletion example.proto.html
Original file line number Diff line number Diff line change
Expand Up @@ -1211,7 +1211,7 @@ <h2>Selected and Disabled Support</h2>
<option>Asiatic Black Bear</option>
<option>Brown Bear</option>
<option>Giant Panda</option>
<option selected>Sloth Bear</option>
<option selected disabled>Sloth Bear</option>
<option disabled>Sun Bear</option>
<option selected disabled>Paddington Bear</option>
<option selected>Polar Bear</option>
Expand Down

0 comments on commit fb0d37f

Please sign in to comment.