Skip to content

Commit

Permalink
Merge pull request harvesthq#1 from kamal/master
Browse files Browse the repository at this point in the history
Guard against adding already selected option
  • Loading branch information
koenpunt committed Aug 5, 2011
2 parents f22c7fb + b3ae3f5 commit db96382
Show file tree
Hide file tree
Showing 6 changed files with 103 additions and 24 deletions.
44 changes: 40 additions & 4 deletions chosen/chosen.jquery.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@
/*
Chosen source: generate output using 'cake build'
Copyright (c) 2011 by Harvest
*/ var $, Chosen, get_side_border_padding, root;
*/
var $, Chosen, get_side_border_padding, root;
var __bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; };
root = this;
$ = jQuery;
Expand All @@ -25,7 +26,8 @@
}
});
Chosen = (function() {
function Chosen(elmn) {
function Chosen(elmn, data, options) {
this.options = $.extend({}, options);
this.set_default_values();
this.form_field = elmn;
this.form_field_jq = $(this.form_field);
Expand Down Expand Up @@ -540,11 +542,45 @@
}
};
Chosen.prototype.no_results = function(terms) {
var no_results_html;
no_results_html = $('<li class="no-results">No results match "<span></span>"</li>');
var no_results_html, option, regex, selected;
no_results_html = $('<li class="no-results">No results match "<span></span>".</li>');
no_results_html.find("span").first().html(terms);
regex = new RegExp('^' + terms + '$', 'i');
selected = (function() {
var _i, _len, _ref, _results;
_ref = this.results_data;
_results = [];
for (_i = 0, _len = _ref.length; _i < _len; _i++) {
option = _ref[_i];
if (regex.test(option.value) && option.selected) {
_results.push(option);
}
}
return _results;
}).call(this);
if (selected.length === 0) {
no_results_html.append(' <a href="javascript:void(0);" class="option-add">Add this item</a>');
no_results_html.find("a.option-add").bind("click", __bind(function(evt) {
return this.select_add_option(terms);
}, this));
}
return this.search_results.append(no_results_html);
};
Chosen.prototype.select_add_option = function(terms) {
var new_option_html;
if ($.isFunction(this.options.addOption)) {
this.options.addOption(terms);
} else {
new_option_html = $('<option />', {
value: terms
}).text(terms);
this.form_field_jq.append(new_option_html);
this.form_field_jq.trigger("liszt:updated");
}
this.search_field.val(terms);
this.search_field.trigger("keyup");
return this.result_select();
};
Chosen.prototype.no_results_clear = function() {
return this.search_results.find(".no-results").remove();
};
Expand Down
Loading

0 comments on commit db96382

Please sign in to comment.