From dc339391f8e3ada5bc5a4e5ea1cedb9945d438ef Mon Sep 17 00:00:00 2001 From: Igor Vaynberg Date: Mon, 30 Jul 2012 13:49:34 +0300 Subject: [PATCH] allow disabling of some messages by specifying a null formatter. fixes #280, #182 --- select2.js | 22 ++++++++++++++++++---- 1 file changed, 18 insertions(+), 4 deletions(-) diff --git a/select2.js b/select2.js index 4bbb121f..9d1ffba7 100755 --- a/select2.js +++ b/select2.js @@ -386,6 +386,20 @@ }; } + /** + * Checks if the formatter function should be used. + * + * Throws an error if it is not a function. Returns true if it should be used, + * false if no formatting should be performed. + * + * @param formatter + */ + function checkFormatter(formatter, formatterName) { + if ($.isFunction(formatter)) return true; + if (!formatter) return fasle; + throw new Error("formatterName must be a function or a falsy value"); + } + /** * blurs any Select2 container that has focus when an element outside them was clicked or received focus * @@ -1042,13 +1056,13 @@ if (opts.maximumSelectionSize >=1) { data = this.data(); - if ($.isArray(data) && data.length >= opts.maximumSelectionSize) { + if ($.isArray(data) && data.length >= opts.maximumSelectionSize && checkFormatter(opts.formatSelectionTooBig, "formatSelectionTooBig")) { render("
  • " + opts.formatSelectionTooBig(opts.maximumSelectionSize) + "
  • "); return; } } - if (search.val().length < opts.minimumInputLength) { + if (search.val().length < opts.minimumInputLength && checkFormatter(opts.formatInputTooShort, "formatInputTooShort")) { render("
  • " + opts.formatInputTooShort(search.val(), opts.minimumInputLength) + "
  • "); return; } @@ -1078,7 +1092,7 @@ } } - if (data.results.length === 0) { + if (data.results.length === 0 && checkFormatter(opts.formatNoMatches, "formatNoMatches")) { render("
  • " + opts.formatNoMatches(search.val()) + "
  • "); return; } @@ -1086,7 +1100,7 @@ results.empty(); self.opts.populateResults.call(this, results, data.results, {term: search.val(), page: this.resultsPage, context:null}); - if (data.more === true) { + if (data.more === true && checkFormatter(opts.formatLoadMore, "formatLoadMore")) { results.children().filter(":last").append("
  • " + escapeMarkup(opts.formatLoadMore(this.resultsPage)) + "
  • "); window.setTimeout(function() { self.loadMoreIfNeeded(); }, 10); }