1
0
mirror of synced 2025-02-04 06:09:23 +03:00

Merge pull request #2164 from colemanw/evaluateFix

Provide context for evaluated functions
This commit is contained in:
Kevin Brown 2014-05-01 10:08:42 -04:00
commit ea3c6766a2

View File

@ -553,10 +553,18 @@ the specific language governing permissions and limitations under the Apache Lic
throw new Error(formatterName +" must be a string, function, or falsy value"); throw new Error(formatterName +" must be a string, function, or falsy value");
} }
function evaluate(val) { /**
* Returns a given value
* If given a function, returns its output
*
* @param val string|function
* @param context value of "this" to be passed to function
* @returns {*}
*/
function evaluate(val, context) {
if ($.isFunction(val)) { if ($.isFunction(val)) {
var args = Array.prototype.slice.call(arguments, 1); var args = Array.prototype.slice.call(arguments, 2);
return val.apply(null, args); return val.apply(context, args);
} }
return val; return val;
} }
@ -697,8 +705,8 @@ the specific language governing permissions and limitations under the Apache Lic
syncCssClasses(this.container, this.opts.element, this.opts.adaptContainerCssClass); syncCssClasses(this.container, this.opts.element, this.opts.adaptContainerCssClass);
this.container.attr("style", opts.element.attr("style")); this.container.attr("style", opts.element.attr("style"));
this.container.css(evaluate(opts.containerCss)); this.container.css(evaluate(opts.containerCss, this.opts.element));
this.container.addClass(evaluate(opts.containerCssClass)); this.container.addClass(evaluate(opts.containerCssClass, this.opts.element));
this.elementTabIndex = this.opts.element.attr("tabindex"); this.elementTabIndex = this.opts.element.attr("tabindex");
@ -715,7 +723,7 @@ the specific language governing permissions and limitations under the Apache Lic
syncCssClasses(this.dropdown, this.opts.element, this.opts.adaptDropdownCssClass); syncCssClasses(this.dropdown, this.opts.element, this.opts.adaptDropdownCssClass);
this.dropdown.addClass(evaluate(opts.dropdownCssClass)); this.dropdown.addClass(evaluate(opts.dropdownCssClass, this.opts.element));
this.dropdown.data("select2", this); this.dropdown.data("select2", this);
this.dropdown.on("click", killEvent); this.dropdown.on("click", killEvent);
@ -1088,10 +1096,10 @@ the specific language governing permissions and limitations under the Apache Lic
this.readonly(readonly); this.readonly(readonly);
syncCssClasses(this.container, this.opts.element, this.opts.adaptContainerCssClass); syncCssClasses(this.container, this.opts.element, this.opts.adaptContainerCssClass);
this.container.addClass(evaluate(this.opts.containerCssClass)); this.container.addClass(evaluate(this.opts.containerCssClass, this.opts.element));
syncCssClasses(this.dropdown, this.opts.element, this.opts.adaptDropdownCssClass); syncCssClasses(this.dropdown, this.opts.element, this.opts.adaptDropdownCssClass);
this.dropdown.addClass(evaluate(this.opts.dropdownCssClass)); this.dropdown.addClass(evaluate(this.opts.dropdownCssClass, this.opts.element));
}); });
@ -1297,7 +1305,7 @@ the specific language governing permissions and limitations under the Apache Lic
this.container.removeClass("select2-drop-above"); this.container.removeClass("select2-drop-above");
$dropdown.removeClass("select2-drop-above"); $dropdown.removeClass("select2-drop-above");
} }
css = $.extend(css, evaluate(this.opts.dropdownCss)); css = $.extend(css, evaluate(this.opts.dropdownCss, this.opts.element));
$dropdown.css(css); $dropdown.css(css);
}, },
@ -1454,7 +1462,7 @@ the specific language governing permissions and limitations under the Apache Lic
//abstract //abstract
getMaximumSelectionSize: function() { getMaximumSelectionSize: function() {
return evaluate(this.opts.maximumSelectionSize); return evaluate(this.opts.maximumSelectionSize, this.opts.element);
}, },
// abstract // abstract
@ -1612,7 +1620,7 @@ the specific language governing permissions and limitations under the Apache Lic
self.postprocessResults(data, false, false); self.postprocessResults(data, false, false);
if (data.more===true) { if (data.more===true) {
more.detach().appendTo(results).text(evaluate(self.opts.formatLoadMore, page+1)); more.detach().appendTo(results).text(evaluate(self.opts.formatLoadMore, self.opts.element, page+1));
window.setTimeout(function() { self.loadMoreIfNeeded(); }, 10); window.setTimeout(function() { self.loadMoreIfNeeded(); }, 10);
} else { } else {
more.remove(); more.remove();
@ -1680,14 +1688,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'>" + evaluate(opts.formatSelectionTooBig, maxSelSize) + "</li>"); render("<li class='select2-selection-limit'>" + evaluate(opts.formatSelectionTooBig, opts.element, 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'>" + evaluate(opts.formatInputTooShort, search.val(), opts.minimumInputLength) + "</li>"); render("<li class='select2-no-results'>" + evaluate(opts.formatInputTooShort, opts.element, search.val(), opts.minimumInputLength) + "</li>");
} else { } else {
render(""); render("");
} }
@ -1697,7 +1705,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'>" + evaluate(opts.formatInputTooLong, search.val(), opts.maximumInputLength) + "</li>"); render("<li class='select2-no-results'>" + evaluate(opts.formatInputTooLong, opts.element, search.val(), opts.maximumInputLength) + "</li>");
} else { } else {
render(""); render("");
} }
@ -1705,7 +1713,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'>" + evaluate(opts.formatSearching) + "</li>"); render("<li class='select2-searching'>" + evaluate(opts.formatSearching, opts.element) + "</li>");
} }
search.addClass("select2-active"); search.addClass("select2-active");
@ -1756,7 +1764,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'>" + evaluate(opts.formatNoMatches, search.val()) + "</li>"); render("<li class='select2-no-results'>" + evaluate(opts.formatNoMatches, opts.element, search.val()) + "</li>");
return; return;
} }
@ -1764,7 +1772,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(evaluate(opts.formatLoadMore, this.resultsPage)) + "</li>"); results.append("<li class='select2-more-results'>" + opts.escapeMarkup(evaluate(opts.formatLoadMore, opts.element, this.resultsPage)) + "</li>");
window.setTimeout(function() { self.loadMoreIfNeeded(); }, 10); window.setTimeout(function() { self.loadMoreIfNeeded(); }, 10);
} }
@ -3106,7 +3114,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'>" + evaluate(self.opts.formatNoMatches, self.search.val()) + "</li>"); this.results.append("<li class='select2-no-results'>" + evaluate(self.opts.formatNoMatches, self.opts.element, self.search.val()) + "</li>");
} }
} }
} }