1
0
mirror of synced 2025-02-03 21:59:24 +03:00

Provide context for evaluated functions

This commit is contained in:
Coleman Watts 2014-03-05 21:02:06 -05:00
parent 9460b0c9b6
commit 43e381ed37

View File

@ -568,10 +568,18 @@ the specific language governing permissions and limitations under the Apache Lic
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)) {
var args = Array.prototype.slice.call(arguments, 1);
return val.apply(null, args);
var args = Array.prototype.slice.call(arguments, 2);
return val.apply(context, args);
}
return val;
}
@ -700,8 +708,8 @@ the specific language governing permissions and limitations under the Apache Lic
syncCssClasses(this.container, this.opts.element, this.opts.adaptContainerCssClass);
this.container.attr("style", opts.element.attr("style"));
this.container.css(evaluate(opts.containerCss));
this.container.addClass(evaluate(opts.containerCssClass));
this.container.css(evaluate(opts.containerCss, this.opts.element));
this.container.addClass(evaluate(opts.containerCssClass, this.opts.element));
this.elementTabIndex = this.opts.element.attr("tabindex");
@ -718,7 +726,7 @@ the specific language governing permissions and limitations under the Apache Lic
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.on("click", killEvent);
@ -1057,10 +1065,10 @@ the specific language governing permissions and limitations under the Apache Lic
this.readonly(readonly);
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);
this.dropdown.addClass(evaluate(this.opts.dropdownCssClass));
this.dropdown.addClass(evaluate(this.opts.dropdownCssClass, this.opts.element));
});
@ -1263,7 +1271,7 @@ the specific language governing permissions and limitations under the Apache Lic
this.container.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);
},
@ -1420,7 +1428,7 @@ the specific language governing permissions and limitations under the Apache Lic
//abstract
getMaximumSelectionSize: function() {
return evaluate(this.opts.maximumSelectionSize);
return evaluate(this.opts.maximumSelectionSize, this.opts.element);
},
// abstract
@ -1570,7 +1578,7 @@ the specific language governing permissions and limitations under the Apache Lic
self.postprocessResults(data, false, false);
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);
} else {
more.remove();
@ -1638,14 +1646,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("<li class='select2-selection-limit'>" + evaluate(opts.formatSelectionTooBig, maxSelSize) + "</li>");
render("<li class='select2-selection-limit'>" + evaluate(opts.formatSelectionTooBig, opts.element, maxSelSize) + "</li>");
return;
}
}
if (search.val().length < opts.minimumInputLength) {
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 {
render("");
}
@ -1655,7 +1663,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("<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 {
render("");
}
@ -1663,7 +1671,7 @@ the specific language governing permissions and limitations under the Apache Lic
}
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");
@ -1714,7 +1722,7 @@ the specific language governing permissions and limitations under the Apache Lic
}
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;
}
@ -1722,7 +1730,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("<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);
}
@ -3046,7 +3054,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("<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>");
}
}
}