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

Delete choice refactor #1232

Merged
merged 3 commits into from
May 23, 2013
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 15 additions & 9 deletions chosen/chosen.jquery.js
Original file line number Diff line number Diff line change
Expand Up @@ -726,26 +726,32 @@ Copyright (c) 2011 by Harvest
};

Chosen.prototype.choice_build = function(item) {
var choice_id, html, link,
var choice, close_link,
_this = this;
if (this.is_multiple && this.max_selected_options <= this.choices) {
this.form_field_jq.trigger("liszt:maxselected", {
chosen: this
});
return false;
}
choice_id = this.container_id + "_c_" + item.array_index;
this.choices += 1;
choice = $('<li />', {
"class": "search-choice"
}).html("<span>" + item.html + "</span>");
if (item.disabled) {
html = '<li class="search-choice search-choice-disabled" id="' + choice_id + '"><span>' + item.html + '</span></li>';
choice.addClass("search-choice-disabled");
} 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>';
close_link = $('<a />', {
href: '#',
"class": 'search-choice-close',
rel: item.array_index
});
close_link.click(function(evt) {
return _this.choice_destroy_link_click(evt);
});
choice.append(close_link);
}
this.search_container.before(html);
link = $('#' + choice_id).find("a").first();
return link.click(function(evt) {
return _this.choice_destroy_link_click(evt);
});
return this.search_container.before(choice);
};

Chosen.prototype.choice_destroy_link_click = function(evt) {
Expand Down
2 changes: 1 addition & 1 deletion chosen/chosen.jquery.min.js

Large diffs are not rendered by default.

31 changes: 17 additions & 14 deletions chosen/chosen.proto.js
Original file line number Diff line number Diff line change
Expand Up @@ -356,8 +356,6 @@ Copyright (c) 2011 by Harvest
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"><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"><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 @@ -717,29 +715,34 @@ Copyright (c) 2011 by Harvest
};

Chosen.prototype.choice_build = function(item) {
var choice_id, link,
var choice, close_link,
_this = this;
if (this.is_multiple && this.max_selected_options <= this.choices) {
this.form_field.fire("liszt:maxselected", {
chosen: this
});
return false;
}
choice_id = this.container_id + "_c_" + item.array_index;
this.choices += 1;
this.search_container.insert({
before: (item.disabled ? this.choice_noclose_temp : this.choice_temp).evaluate({
id: choice_id,
choice: item.html,
position: item.array_index
})
});
if (!item.disabled) {
link = $(choice_id).down('a');
return link.observe("click", function(evt) {
choice = new Element('li', {
"class": "search-choice"
}).update("<span>" + item.html + "</span>");
if (item.disabled) {
choice.addClassName('search-choice-disabled');
} else {
close_link = new Element('a', {
href: '#',
"class": 'search-choice-close',
rel: item.array_index
});
close_link.observe("click", function(evt) {
return _this.choice_destroy_link_click(evt);
});
choice.insert(close_link);
}
return this.search_container.insert({
before: choice
});
};

Chosen.prototype.choice_destroy_link_click = function(evt) {
Expand Down
2 changes: 1 addition & 1 deletion chosen/chosen.proto.min.js

Large diffs are not rendered by default.

18 changes: 11 additions & 7 deletions coffee/chosen.jquery.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -291,16 +291,20 @@ class Chosen extends AbstractChosen
choice_build: (item) ->
if @is_multiple and @max_selected_options <= @choices
@form_field_jq.trigger("liszt:maxselected", {chosen: this})
return false # fire event
choice_id = @container_id + "_c_" + item.array_index
return false

@choices += 1

choice = $('<li />', { class: "search-choice" }).html("<span>#{item.html}</span>")

if item.disabled
html = '<li class="search-choice search-choice-disabled" id="' + choice_id + '"><span>' + item.html + '</span></li>'
choice.addClass 'search-choice-disabled'
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>'
@search_container.before html
link = $('#' + choice_id).find("a").first()
link.click (evt) => this.choice_destroy_link_click(evt)
close_link = $('<a />', { href: '#', class: 'search-choice-close', rel: item.array_index })
close_link.click (evt) => this.choice_destroy_link_click(evt)
choice.append close_link

@search_container.before choice

choice_destroy_link_click: (evt) ->
evt.preventDefault()
Expand Down
23 changes: 12 additions & 11 deletions coffee/chosen.proto.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@ class Chosen extends AbstractChosen
# HTML Templates
@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"><div class="chzn-search"><input type="text" autocomplete="off" /></div><ul class="chzn-results"></ul></div>')
@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"><ul class="chzn-results"></ul></div>')
@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>')
@choice_noclose_temp = new Template('<li class="search-choice search-choice-disabled" id="#{id}"><span>#{choice}</span></li>')
@no_results_temp = new Template('<li class="no-results">' + @results_none_found + ' "<span>#{terms}</span>"</li>')

set_up_html: ->
Expand Down Expand Up @@ -281,16 +279,19 @@ class Chosen extends AbstractChosen
if @is_multiple and @max_selected_options <= @choices
@form_field.fire("liszt:maxselected", {chosen: this})
return false
choice_id = @container_id + "_c_" + item.array_index

@choices += 1
@search_container.insert
before: (if item.disabled then @choice_noclose_temp else @choice_temp).evaluate
id: choice_id
choice: item.html
position: item.array_index
if not item.disabled
link = $(choice_id).down('a')
link.observe "click", (evt) => this.choice_destroy_link_click(evt)

choice = new Element('li', { class: "search-choice" }).update("<span>#{item.html}</span>")

if item.disabled
choice.addClassName 'search-choice-disabled'
else
close_link = new Element('a', { href: '#', class: 'search-choice-close', rel: item.array_index })
close_link.observe "click", (evt) => this.choice_destroy_link_click(evt)
choice.insert close_link

@search_container.insert { before: choice }

choice_destroy_link_click: (evt) ->
evt.preventDefault()
Expand Down