1
0
mirror of synced 2024-11-22 21:16:10 +03:00

allow disabling of some messages by specifying a null formatter. fixes #280, #182

This commit is contained in:
Igor Vaynberg 2012-07-30 13:49:34 +03:00
parent e2ea6faac7
commit dc339391f8

View File

@ -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 * 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) { if (opts.maximumSelectionSize >=1) {
data = this.data(); data = this.data();
if ($.isArray(data) && data.length >= opts.maximumSelectionSize) { if ($.isArray(data) && data.length >= opts.maximumSelectionSize && checkFormatter(opts.formatSelectionTooBig, "formatSelectionTooBig")) {
render("<li class='select2-selection-limit'>" + opts.formatSelectionTooBig(opts.maximumSelectionSize) + "</li>"); render("<li class='select2-selection-limit'>" + opts.formatSelectionTooBig(opts.maximumSelectionSize) + "</li>");
return; return;
} }
} }
if (search.val().length < opts.minimumInputLength) { if (search.val().length < opts.minimumInputLength && checkFormatter(opts.formatInputTooShort, "formatInputTooShort")) {
render("<li class='select2-no-results'>" + opts.formatInputTooShort(search.val(), opts.minimumInputLength) + "</li>"); render("<li class='select2-no-results'>" + opts.formatInputTooShort(search.val(), opts.minimumInputLength) + "</li>");
return; return;
} }
@ -1078,7 +1092,7 @@
} }
} }
if (data.results.length === 0) { 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'>" + opts.formatNoMatches(search.val()) + "</li>");
return; return;
} }
@ -1086,7 +1100,7 @@
results.empty(); results.empty();
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) { if (data.more === true && checkFormatter(opts.formatLoadMore, "formatLoadMore")) {
results.children().filter(":last").append("<li class='select2-more-results'>" + escapeMarkup(opts.formatLoadMore(this.resultsPage)) + "</li>"); results.children().filter(":last").append("<li class='select2-more-results'>" + escapeMarkup(opts.formatLoadMore(this.resultsPage)) + "</li>");
window.setTimeout(function() { self.loadMoreIfNeeded(); }, 10); window.setTimeout(function() { self.loadMoreIfNeeded(); }, 10);
} }