Merge pull request #274 from harvesthq/allow_deselect

Add allow deselect option
This commit is contained in:
Patrick Filler 2011-09-28 09:23:02 -07:00
commit 763eecf39f
8 changed files with 101 additions and 12 deletions

Binary file not shown.

Before

Width:  |  Height:  |  Size: 742 B

After

Width:  |  Height:  |  Size: 3.9 KiB

View file

@ -57,6 +57,19 @@
-ms-text-overflow: ellipsis; -ms-text-overflow: ellipsis;
text-overflow: ellipsis; text-overflow: ellipsis;
} }
.chzn-container-single .chzn-single abbr {
display: block;
position: absolute;
right: 26px;
top: 8px;
width: 12px;
height: 13px;
font-size: 1px;
background: url(chosen-sprite.png) right top no-repeat;
}
.chzn-container-single .chzn-single abbr:hover {
background-position: right -11px;
}
.chzn-container-single .chzn-single div { .chzn-container-single .chzn-single div {
-webkit-border-radius: 0 4px 4px 0; -webkit-border-radius: 0 4px 4px 0;
-moz-border-radius : 0 4px 4px 0; -moz-border-radius : 0 4px 4px 0;
@ -191,18 +204,18 @@
.chzn-container-multi .chzn-choices .search-choice .search-choice-close { .chzn-container-multi .chzn-choices .search-choice .search-choice-close {
display: block; display: block;
position: absolute; position: absolute;
right: 5px; right: 3px;
top: 6px; top: 4px;
width: 8px; width: 12px;
height: 9px; height: 13px;
font-size: 1px; font-size: 1px;
background: url(chosen-sprite.png) right top no-repeat; background: url(chosen-sprite.png) right top no-repeat;
} }
.chzn-container-multi .chzn-choices .search-choice .search-choice-close:hover { .chzn-container-multi .chzn-choices .search-choice .search-choice-close:hover {
background-position: right -9px; background-position: right -11px;
} }
.chzn-container-multi .chzn-choices .search-choice-focus .search-choice-close { .chzn-container-multi .chzn-choices .search-choice-focus .search-choice-close {
background-position: right -9px; background-position: right -11px;
} }
/* @end */ /* @end */

View file

@ -52,6 +52,7 @@
this.results_showing = false; this.results_showing = false;
this.result_highlighted = null; this.result_highlighted = null;
this.result_single_selected = null; this.result_single_selected = null;
this.allow_single_deselect = (this.options.allow_single_deselect != null) && this.form_field.options[0].text === "" ? this.options.allow_single_deselect : false;
this.choices = 0; this.choices = 0;
return this.results_none_found = this.options.no_results_text || "No results match"; return this.results_none_found = this.options.no_results_text || "No results match";
}; };
@ -103,6 +104,9 @@
this.container.mousedown(__bind(function(evt) { this.container.mousedown(__bind(function(evt) {
return this.container_mousedown(evt); return this.container_mousedown(evt);
}, this)); }, this));
this.container.mouseup(__bind(function(evt) {
return this.container_mouseup(evt);
}, this));
this.container.mouseenter(__bind(function(evt) { this.container.mouseenter(__bind(function(evt) {
return this.mouse_enter(evt); return this.mouse_enter(evt);
}, this)); }, this));
@ -157,11 +161,13 @@
} }
}; };
Chosen.prototype.container_mousedown = function(evt) { Chosen.prototype.container_mousedown = function(evt) {
var target_node;
if (!this.is_disabled) { if (!this.is_disabled) {
target_node = evt != null ? evt.target.nodeName : null;
if (evt && evt.type === "mousedown") { if (evt && evt.type === "mousedown") {
evt.stopPropagation(); evt.stopPropagation();
} }
if (!this.pending_destroy_click) { if (!this.pending_destroy_click && target_node !== "ABBR") {
if (!this.active_field) { if (!this.active_field) {
if (this.is_multiple) { if (this.is_multiple) {
this.search_field.val(""); this.search_field.val("");
@ -178,6 +184,11 @@
} }
} }
}; };
Chosen.prototype.container_mouseup = function(evt) {
if (evt.target.nodeName === "ABBR") {
return this.results_reset(evt);
}
};
Chosen.prototype.mouse_enter = function() { Chosen.prototype.mouse_enter = function() {
return this.mouse_on_container = true; return this.mouse_on_container = true;
}; };
@ -431,6 +442,15 @@
this.result_deselect(link.attr("rel")); this.result_deselect(link.attr("rel"));
return link.parents('li').first().remove(); return link.parents('li').first().remove();
}; };
Chosen.prototype.results_reset = function(evt) {
this.form_field.options[0].selected = true;
this.selected_item.find("span").text(this.default_text);
this.show_search_field_default();
$(evt.target).remove();
if (this.active_field) {
return this.results_hide();
}
};
Chosen.prototype.result_select = function(evt) { Chosen.prototype.result_select = function(evt) {
var high, high_id, item, position; var high, high_id, item, position;
if (this.result_highlight) { if (this.result_highlight) {
@ -452,6 +472,9 @@
this.choice_build(item); this.choice_build(item);
} else { } else {
this.selected_item.find("span").first().text(item.text); this.selected_item.find("span").first().text(item.text);
if (this.allow_single_deselect) {
this.selected_item.find("span").first().after("<abbr></abbr>");
}
} }
if (!(evt.metaKey && this.is_multiple)) { if (!(evt.metaKey && this.is_multiple)) {
this.results_hide(); this.results_hide();

File diff suppressed because one or more lines are too long

View file

@ -38,6 +38,7 @@
this.results_showing = false; this.results_showing = false;
this.result_highlighted = null; this.result_highlighted = null;
this.result_single_selected = null; this.result_single_selected = null;
this.allow_single_deselect = (this.options.allow_single_deselect != null) && this.form_field.options[0].text === "" ? this.options.allow_single_deselect : false;
this.choices = 0; this.choices = 0;
this.results_none_found = this.options.no_results_text || "No results match"; this.results_none_found = this.options.no_results_text || "No results match";
this.single_temp = new Template('<a href="javascript:void(0)" class="chzn-single"><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"><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>');
@ -94,6 +95,9 @@
this.container.observe("mousedown", __bind(function(evt) { this.container.observe("mousedown", __bind(function(evt) {
return this.container_mousedown(evt); return this.container_mousedown(evt);
}, this)); }, this));
this.container.observe("mouseup", __bind(function(evt) {
return this.container_mouseup(evt);
}, this));
this.container.observe("mouseenter", __bind(function(evt) { this.container.observe("mouseenter", __bind(function(evt) {
return this.mouse_enter(evt); return this.mouse_enter(evt);
}, this)); }, this));
@ -148,11 +152,13 @@
} }
}; };
Chosen.prototype.container_mousedown = function(evt) { Chosen.prototype.container_mousedown = function(evt) {
var target_node;
if (!this.is_disabled) { if (!this.is_disabled) {
target_node = evt != null ? evt.target.nodeName : null;
if (evt && evt.type === "mousedown") { if (evt && evt.type === "mousedown") {
evt.stop(); evt.stop();
} }
if (!this.pending_destroy_click) { if (!this.pending_destroy_click && target_node !== "ABBR") {
if (!this.active_field) { if (!this.active_field) {
if (this.is_multiple) { if (this.is_multiple) {
this.search_field.clear(); this.search_field.clear();
@ -168,6 +174,11 @@
} }
} }
}; };
Chosen.prototype.container_mouseup = function(evt) {
if (evt.target.nodeName === "ABBR") {
return this.results_reset(evt);
}
};
Chosen.prototype.mouse_enter = function() { Chosen.prototype.mouse_enter = function() {
return this.mouse_on_container = true; return this.mouse_on_container = true;
}; };
@ -419,6 +430,15 @@
this.result_deselect(link.readAttribute("rel")); this.result_deselect(link.readAttribute("rel"));
return link.up('li').remove(); return link.up('li').remove();
}; };
Chosen.prototype.results_reset = function(evt) {
this.form_field.options[0].selected = true;
this.selected_item.down("span").update(this.default_text);
this.show_search_field_default();
evt.target.remove();
if (this.active_field) {
return this.results_hide();
}
};
Chosen.prototype.result_select = function(evt) { Chosen.prototype.result_select = function(evt) {
var high, item, position; var high, item, position;
if (this.result_highlight) { if (this.result_highlight) {
@ -439,6 +459,11 @@
this.choice_build(item); this.choice_build(item);
} else { } else {
this.selected_item.down("span").update(item.html); this.selected_item.down("span").update(item.html);
if (this.allow_single_deselect) {
this.selected_item.down("span").insert({
after: "<abbr></abbr>"
});
}
} }
if (!(evt.metaKey && this.is_multiple)) { if (!(evt.metaKey && this.is_multiple)) {
this.results_hide(); this.results_hide();

File diff suppressed because one or more lines are too long

View file

@ -37,6 +37,7 @@ class Chosen
@results_showing = false @results_showing = false
@result_highlighted = null @result_highlighted = null
@result_single_selected = null @result_single_selected = null
@allow_single_deselect = if @options.allow_single_deselect? and @form_field.options[0].text == "" then @options.allow_single_deselect else false
@choices = 0 @choices = 0
@results_none_found = @options.no_results_text or "No results match" @results_none_found = @options.no_results_text or "No results match"
@ -90,6 +91,7 @@ class Chosen
register_observers: -> register_observers: ->
@container.mousedown (evt) => this.container_mousedown(evt) @container.mousedown (evt) => this.container_mousedown(evt)
@container.mouseup (evt) => this.container_mouseup(evt)
@container.mouseenter (evt) => this.mouse_enter(evt) @container.mouseenter (evt) => this.mouse_enter(evt)
@container.mouseleave (evt) => this.mouse_leave(evt) @container.mouseleave (evt) => this.mouse_leave(evt)
@ -121,9 +123,10 @@ class Chosen
container_mousedown: (evt) -> container_mousedown: (evt) ->
if !@is_disabled if !@is_disabled
target_node = if evt? then evt.target.nodeName else null
if evt and evt.type is "mousedown" if evt and evt.type is "mousedown"
evt.stopPropagation() evt.stopPropagation()
if not @pending_destroy_click if not @pending_destroy_click and target_node != "ABBR"
if not @active_field if not @active_field
@search_field.val "" if @is_multiple @search_field.val "" if @is_multiple
$(document).click @click_test_action $(document).click @click_test_action
@ -136,6 +139,9 @@ class Chosen
else else
@pending_destroy_click = false @pending_destroy_click = false
container_mouseup: (evt) ->
this.results_reset(evt) if evt.target.nodeName is "ABBR"
mouse_enter: -> @mouse_on_container = true mouse_enter: -> @mouse_on_container = true
mouse_leave: -> @mouse_on_container = false mouse_leave: -> @mouse_on_container = false
@ -352,6 +358,13 @@ class Chosen
this.result_deselect (link.attr "rel") this.result_deselect (link.attr "rel")
link.parents('li').first().remove() link.parents('li').first().remove()
results_reset: (evt) ->
@form_field.options[0].selected = true
@selected_item.find("span").text @default_text
this.show_search_field_default()
$(evt.target).remove();
this.results_hide() if @active_field
result_select: (evt) -> result_select: (evt) ->
if @result_highlight if @result_highlight
high = @result_highlight high = @result_highlight
@ -377,6 +390,7 @@ class Chosen
this.choice_build item this.choice_build item
else else
@selected_item.find("span").first().text item.text @selected_item.find("span").first().text item.text
@selected_item.find("span").first().after "<abbr></abbr>" if @allow_single_deselect
this.results_hide() unless evt.metaKey and @is_multiple this.results_hide() unless evt.metaKey and @is_multiple

View file

@ -29,6 +29,7 @@ class Chosen
@results_showing = false @results_showing = false
@result_highlighted = null @result_highlighted = null
@result_single_selected = null @result_single_selected = null
@allow_single_deselect = if @options.allow_single_deselect? and @form_field.options[0].text == "" then @options.allow_single_deselect else false
@choices = 0 @choices = 0
@results_none_found = @options.no_results_text or "No results match" @results_none_found = @options.no_results_text or "No results match"
@ -85,6 +86,7 @@ class Chosen
register_observers: -> register_observers: ->
@container.observe "mousedown", (evt) => this.container_mousedown(evt) @container.observe "mousedown", (evt) => this.container_mousedown(evt)
@container.observe "mouseup", (evt) => this.container_mouseup(evt)
@container.observe "mouseenter", (evt) => this.mouse_enter(evt) @container.observe "mouseenter", (evt) => this.mouse_enter(evt)
@container.observe "mouseleave", (evt) => this.mouse_leave(evt) @container.observe "mouseleave", (evt) => this.mouse_leave(evt)
@ -116,9 +118,10 @@ class Chosen
container_mousedown: (evt) -> container_mousedown: (evt) ->
if !@is_disabled if !@is_disabled
target_node = if evt? then evt.target.nodeName else null
if evt and evt.type is "mousedown" if evt and evt.type is "mousedown"
evt.stop() evt.stop()
if not @pending_destroy_click if not @pending_destroy_click and target_node != "ABBR"
if not @active_field if not @active_field
@search_field.clear() if @is_multiple @search_field.clear() if @is_multiple
document.observe "click", @click_test_action document.observe "click", @click_test_action
@ -130,6 +133,9 @@ class Chosen
else else
@pending_destroy_click = false @pending_destroy_click = false
container_mouseup: (evt) ->
this.results_reset(evt) if evt.target.nodeName is "ABBR"
mouse_enter: -> @mouse_on_container = true mouse_enter: -> @mouse_on_container = true
mouse_leave: -> @mouse_on_container = false mouse_leave: -> @mouse_on_container = false
@ -347,6 +353,13 @@ class Chosen
this.result_deselect link.readAttribute("rel") this.result_deselect link.readAttribute("rel")
link.up('li').remove() link.up('li').remove()
results_reset: (evt) ->
@form_field.options[0].selected = true
@selected_item.down("span").update(@default_text)
this.show_search_field_default()
evt.target.remove()
this.results_hide() if @active_field
result_select: (evt) -> result_select: (evt) ->
if @result_highlight if @result_highlight
high = @result_highlight high = @result_highlight
@ -370,6 +383,7 @@ class Chosen
this.choice_build item this.choice_build item
else else
@selected_item.down("span").update(item.html) @selected_item.down("span").update(item.html)
@selected_item.down("span").insert { after: "<abbr></abbr>" } if @allow_single_deselect
this.results_hide() unless evt.metaKey and @is_multiple this.results_hide() unless evt.metaKey and @is_multiple