1
0
mirror of synced 2024-11-26 14:56:07 +03:00

Merge pull request #2092 from colemanw/strings

Accept strings as well as functions for localization
This commit is contained in:
Kevin Brown 2014-02-03 15:41:32 -08:00
commit beabb63f03

View File

@ -564,11 +564,16 @@ the specific language governing permissions and limitations under the Apache Lic
function checkFormatter(formatter, formatterName) { function checkFormatter(formatter, formatterName) {
if ($.isFunction(formatter)) return true; if ($.isFunction(formatter)) return true;
if (!formatter) return false; if (!formatter) return false;
throw new Error(formatterName +" must be a function or a falsy value"); if (typeof(formatter) === 'string') return true;
throw new Error(formatterName +" must be a string, function, or falsy value");
} }
function evaluate(val) { function evaluate(val) {
return $.isFunction(val) ? val() : val; if ($.isFunction(val)) {
var args = Array.prototype.slice.call(arguments, 1);
return val.apply(null, args);
}
return val;
} }
function countResults(results) { function countResults(results) {
@ -1634,14 +1639,14 @@ the specific language governing permissions and limitations under the Apache Lic
if (maxSelSize >=1) { if (maxSelSize >=1) {
data = this.data(); data = this.data();
if ($.isArray(data) && data.length >= maxSelSize && checkFormatter(opts.formatSelectionTooBig, "formatSelectionTooBig")) { if ($.isArray(data) && data.length >= maxSelSize && checkFormatter(opts.formatSelectionTooBig, "formatSelectionTooBig")) {
render("<li class='select2-selection-limit'>" + opts.formatSelectionTooBig(maxSelSize) + "</li>"); render("<li class='select2-selection-limit'>" + evaluate(opts.formatSelectionTooBig, maxSelSize) + "</li>");
return; return;
} }
} }
if (search.val().length < opts.minimumInputLength) { if (search.val().length < opts.minimumInputLength) {
if (checkFormatter(opts.formatInputTooShort, "formatInputTooShort")) { if (checkFormatter(opts.formatInputTooShort, "formatInputTooShort")) {
render("<li class='select2-no-results'>" + opts.formatInputTooShort(search.val(), opts.minimumInputLength) + "</li>"); render("<li class='select2-no-results'>" + evaluate(opts.formatInputTooShort, search.val(), opts.minimumInputLength) + "</li>");
} else { } else {
render(""); render("");
} }
@ -1651,7 +1656,7 @@ the specific language governing permissions and limitations under the Apache Lic
if (opts.maximumInputLength && search.val().length > opts.maximumInputLength) { if (opts.maximumInputLength && search.val().length > opts.maximumInputLength) {
if (checkFormatter(opts.formatInputTooLong, "formatInputTooLong")) { if (checkFormatter(opts.formatInputTooLong, "formatInputTooLong")) {
render("<li class='select2-no-results'>" + opts.formatInputTooLong(search.val(), opts.maximumInputLength) + "</li>"); render("<li class='select2-no-results'>" + evaluate(opts.formatInputTooLong, search.val(), opts.maximumInputLength) + "</li>");
} else { } else {
render(""); render("");
} }
@ -1659,7 +1664,7 @@ the specific language governing permissions and limitations under the Apache Lic
} }
if (opts.formatSearching && this.findHighlightableChoices().length === 0) { if (opts.formatSearching && this.findHighlightableChoices().length === 0) {
render("<li class='select2-searching'>" + opts.formatSearching() + "</li>"); render("<li class='select2-searching'>" + evaluate(opts.formatSearching) + "</li>");
} }
search.addClass("select2-active"); search.addClass("select2-active");
@ -1710,7 +1715,7 @@ the specific language governing permissions and limitations under the Apache Lic
} }
if (data.results.length === 0 && checkFormatter(opts.formatNoMatches, "formatNoMatches")) { if (data.results.length === 0 && checkFormatter(opts.formatNoMatches, "formatNoMatches")) {
render("<li class='select2-no-results'>" + opts.formatNoMatches(search.val()) + "</li>"); render("<li class='select2-no-results'>" + evaluate(opts.formatNoMatches, search.val()) + "</li>");
return; return;
} }
@ -1718,7 +1723,7 @@ the specific language governing permissions and limitations under the Apache Lic
self.opts.populateResults.call(this, results, data.results, {term: search.val(), page: this.resultsPage, context:null}); self.opts.populateResults.call(this, results, data.results, {term: search.val(), page: this.resultsPage, context:null});
if (data.more === true && checkFormatter(opts.formatLoadMore, "formatLoadMore")) { if (data.more === true && checkFormatter(opts.formatLoadMore, "formatLoadMore")) {
results.append("<li class='select2-more-results'>" + self.opts.escapeMarkup(opts.formatLoadMore(this.resultsPage)) + "</li>"); results.append("<li class='select2-more-results'>" + self.opts.escapeMarkup(evaluate(opts.formatLoadMore, this.resultsPage)) + "</li>");
window.setTimeout(function() { self.loadMoreIfNeeded(); }, 10); window.setTimeout(function() { self.loadMoreIfNeeded(); }, 10);
} }
@ -3042,7 +3047,7 @@ the specific language governing permissions and limitations under the Apache Lic
if(!this.opts.createSearchChoice && !choices.filter('.select2-result:not(.select2-selected)').length > 0){ if(!this.opts.createSearchChoice && !choices.filter('.select2-result:not(.select2-selected)').length > 0){
if(!data || data && !data.more && this.results.find(".select2-no-results").length === 0) { if(!data || data && !data.more && this.results.find(".select2-no-results").length === 0) {
if (checkFormatter(self.opts.formatNoMatches, "formatNoMatches")) { if (checkFormatter(self.opts.formatNoMatches, "formatNoMatches")) {
this.results.append("<li class='select2-no-results'>" + self.opts.formatNoMatches(self.search.val()) + "</li>"); this.results.append("<li class='select2-no-results'>" + evaluate(self.opts.formatNoMatches, self.search.val()) + "</li>");
} }
} }
} }