Skip to content

Commit

Permalink
Merge pull request #1232 from harvesthq/delete-choice-refactor
Browse files Browse the repository at this point in the history
Delete choice refactor
  • Loading branch information
pfiller committed May 23, 2013
2 parents fcaddb8 + 26add0a commit ba5960d
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 43 deletions.
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

0 comments on commit ba5960d

Please sign in to comment.