From 175c78441fa0522a0f7d8f453842bcae835c6a81 Mon Sep 17 00:00:00 2001 From: Coleman Watts Date: Fri, 31 Jan 2014 21:00:47 -0800 Subject: [PATCH] Accept strings as well as functions for localization --- select2.js | 23 ++++++++++++++--------- 1 file changed, 14 insertions(+), 9 deletions(-) diff --git a/select2.js b/select2.js index ec10d987..0543aafb 100644 --- a/select2.js +++ b/select2.js @@ -564,11 +564,16 @@ the specific language governing permissions and limitations under the Apache Lic function checkFormatter(formatter, formatterName) { if ($.isFunction(formatter)) return true; 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) { - 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) { @@ -1634,14 +1639,14 @@ the specific language governing permissions and limitations under the Apache Lic if (maxSelSize >=1) { data = this.data(); if ($.isArray(data) && data.length >= maxSelSize && checkFormatter(opts.formatSelectionTooBig, "formatSelectionTooBig")) { - render("
  • " + opts.formatSelectionTooBig(maxSelSize) + "
  • "); + render("
  • " + evaluate(opts.formatSelectionTooBig, maxSelSize) + "
  • "); return; } } if (search.val().length < opts.minimumInputLength) { if (checkFormatter(opts.formatInputTooShort, "formatInputTooShort")) { - render("
  • " + opts.formatInputTooShort(search.val(), opts.minimumInputLength) + "
  • "); + render("
  • " + evaluate(opts.formatInputTooShort, search.val(), opts.minimumInputLength) + "
  • "); } else { 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 (checkFormatter(opts.formatInputTooLong, "formatInputTooLong")) { - render("
  • " + opts.formatInputTooLong(search.val(), opts.maximumInputLength) + "
  • "); + render("
  • " + evaluate(opts.formatInputTooLong, search.val(), opts.maximumInputLength) + "
  • "); } else { render(""); } @@ -1659,7 +1664,7 @@ the specific language governing permissions and limitations under the Apache Lic } if (opts.formatSearching && this.findHighlightableChoices().length === 0) { - render("
  • " + opts.formatSearching() + "
  • "); + render("
  • " + evaluate(opts.formatSearching) + "
  • "); } 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")) { - render("
  • " + opts.formatNoMatches(search.val()) + "
  • "); + render("
  • " + evaluate(opts.formatNoMatches, search.val()) + "
  • "); 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}); if (data.more === true && checkFormatter(opts.formatLoadMore, "formatLoadMore")) { - results.append("
  • " + self.opts.escapeMarkup(opts.formatLoadMore(this.resultsPage)) + "
  • "); + results.append("
  • " + self.opts.escapeMarkup(evaluate(opts.formatLoadMore, this.resultsPage)) + "
  • "); 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(!data || data && !data.more && this.results.find(".select2-no-results").length === 0) { if (checkFormatter(self.opts.formatNoMatches, "formatNoMatches")) { - this.results.append("
  • " + self.opts.formatNoMatches(self.search.val()) + "
  • "); + this.results.append("
  • " + evaluate(self.opts.formatNoMatches, self.search.val()) + "
  • "); } } }