Skip to content

Commit 43e381e

Browse files
committed
Provide context for evaluated functions
1 parent 9460b0c commit 43e381e

1 file changed

Lines changed: 26 additions & 18 deletions

File tree

select2.js

Lines changed: 26 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -568,10 +568,18 @@ the specific language governing permissions and limitations under the Apache Lic
568568
throw new Error(formatterName +" must be a string, function, or falsy value");
569569
}
570570

571-
function evaluate(val) {
571+
/**
572+
* Returns a given value
573+
* If given a function, returns its output
574+
*
575+
* @param val string|function
576+
* @param context value of "this" to be passed to function
577+
* @returns {*}
578+
*/
579+
function evaluate(val, context) {
572580
if ($.isFunction(val)) {
573-
var args = Array.prototype.slice.call(arguments, 1);
574-
return val.apply(null, args);
581+
var args = Array.prototype.slice.call(arguments, 2);
582+
return val.apply(context, args);
575583
}
576584
return val;
577585
}
@@ -700,8 +708,8 @@ the specific language governing permissions and limitations under the Apache Lic
700708
syncCssClasses(this.container, this.opts.element, this.opts.adaptContainerCssClass);
701709

702710
this.container.attr("style", opts.element.attr("style"));
703-
this.container.css(evaluate(opts.containerCss));
704-
this.container.addClass(evaluate(opts.containerCssClass));
711+
this.container.css(evaluate(opts.containerCss, this.opts.element));
712+
this.container.addClass(evaluate(opts.containerCssClass, this.opts.element));
705713

706714
this.elementTabIndex = this.opts.element.attr("tabindex");
707715

@@ -718,7 +726,7 @@ the specific language governing permissions and limitations under the Apache Lic
718726

719727
syncCssClasses(this.dropdown, this.opts.element, this.opts.adaptDropdownCssClass);
720728

721-
this.dropdown.addClass(evaluate(opts.dropdownCssClass));
729+
this.dropdown.addClass(evaluate(opts.dropdownCssClass, this.opts.element));
722730
this.dropdown.data("select2", this);
723731
this.dropdown.on("click", killEvent);
724732

@@ -1057,10 +1065,10 @@ the specific language governing permissions and limitations under the Apache Lic
10571065
this.readonly(readonly);
10581066

10591067
syncCssClasses(this.container, this.opts.element, this.opts.adaptContainerCssClass);
1060-
this.container.addClass(evaluate(this.opts.containerCssClass));
1068+
this.container.addClass(evaluate(this.opts.containerCssClass, this.opts.element));
10611069

10621070
syncCssClasses(this.dropdown, this.opts.element, this.opts.adaptDropdownCssClass);
1063-
this.dropdown.addClass(evaluate(this.opts.dropdownCssClass));
1071+
this.dropdown.addClass(evaluate(this.opts.dropdownCssClass, this.opts.element));
10641072

10651073
});
10661074

@@ -1263,7 +1271,7 @@ the specific language governing permissions and limitations under the Apache Lic
12631271
this.container.removeClass("select2-drop-above");
12641272
$dropdown.removeClass("select2-drop-above");
12651273
}
1266-
css = $.extend(css, evaluate(this.opts.dropdownCss));
1274+
css = $.extend(css, evaluate(this.opts.dropdownCss, this.opts.element));
12671275

12681276
$dropdown.css(css);
12691277
},
@@ -1420,7 +1428,7 @@ the specific language governing permissions and limitations under the Apache Lic
14201428

14211429
//abstract
14221430
getMaximumSelectionSize: function() {
1423-
return evaluate(this.opts.maximumSelectionSize);
1431+
return evaluate(this.opts.maximumSelectionSize, this.opts.element);
14241432
},
14251433

14261434
// abstract
@@ -1570,7 +1578,7 @@ the specific language governing permissions and limitations under the Apache Lic
15701578
self.postprocessResults(data, false, false);
15711579

15721580
if (data.more===true) {
1573-
more.detach().appendTo(results).text(evaluate(self.opts.formatLoadMore, page+1));
1581+
more.detach().appendTo(results).text(evaluate(self.opts.formatLoadMore, self.opts.element, page+1));
15741582
window.setTimeout(function() { self.loadMoreIfNeeded(); }, 10);
15751583
} else {
15761584
more.remove();
@@ -1638,14 +1646,14 @@ the specific language governing permissions and limitations under the Apache Lic
16381646
if (maxSelSize >=1) {
16391647
data = this.data();
16401648
if ($.isArray(data) && data.length >= maxSelSize && checkFormatter(opts.formatSelectionTooBig, "formatSelectionTooBig")) {
1641-
render("<li class='select2-selection-limit'>" + evaluate(opts.formatSelectionTooBig, maxSelSize) + "</li>");
1649+
render("<li class='select2-selection-limit'>" + evaluate(opts.formatSelectionTooBig, opts.element, maxSelSize) + "</li>");
16421650
return;
16431651
}
16441652
}
16451653

16461654
if (search.val().length < opts.minimumInputLength) {
16471655
if (checkFormatter(opts.formatInputTooShort, "formatInputTooShort")) {
1648-
render("<li class='select2-no-results'>" + evaluate(opts.formatInputTooShort, search.val(), opts.minimumInputLength) + "</li>");
1656+
render("<li class='select2-no-results'>" + evaluate(opts.formatInputTooShort, opts.element, search.val(), opts.minimumInputLength) + "</li>");
16491657
} else {
16501658
render("");
16511659
}
@@ -1655,15 +1663,15 @@ the specific language governing permissions and limitations under the Apache Lic
16551663

16561664
if (opts.maximumInputLength && search.val().length > opts.maximumInputLength) {
16571665
if (checkFormatter(opts.formatInputTooLong, "formatInputTooLong")) {
1658-
render("<li class='select2-no-results'>" + evaluate(opts.formatInputTooLong, search.val(), opts.maximumInputLength) + "</li>");
1666+
render("<li class='select2-no-results'>" + evaluate(opts.formatInputTooLong, opts.element, search.val(), opts.maximumInputLength) + "</li>");
16591667
} else {
16601668
render("");
16611669
}
16621670
return;
16631671
}
16641672

16651673
if (opts.formatSearching && this.findHighlightableChoices().length === 0) {
1666-
render("<li class='select2-searching'>" + evaluate(opts.formatSearching) + "</li>");
1674+
render("<li class='select2-searching'>" + evaluate(opts.formatSearching, opts.element) + "</li>");
16671675
}
16681676

16691677
search.addClass("select2-active");
@@ -1714,15 +1722,15 @@ the specific language governing permissions and limitations under the Apache Lic
17141722
}
17151723

17161724
if (data.results.length === 0 && checkFormatter(opts.formatNoMatches, "formatNoMatches")) {
1717-
render("<li class='select2-no-results'>" + evaluate(opts.formatNoMatches, search.val()) + "</li>");
1725+
render("<li class='select2-no-results'>" + evaluate(opts.formatNoMatches, opts.element, search.val()) + "</li>");
17181726
return;
17191727
}
17201728

17211729
results.empty();
17221730
self.opts.populateResults.call(this, results, data.results, {term: search.val(), page: this.resultsPage, context:null});
17231731

17241732
if (data.more === true && checkFormatter(opts.formatLoadMore, "formatLoadMore")) {
1725-
results.append("<li class='select2-more-results'>" + self.opts.escapeMarkup(evaluate(opts.formatLoadMore, this.resultsPage)) + "</li>");
1733+
results.append("<li class='select2-more-results'>" + opts.escapeMarkup(evaluate(opts.formatLoadMore, opts.element, this.resultsPage)) + "</li>");
17261734
window.setTimeout(function() { self.loadMoreIfNeeded(); }, 10);
17271735
}
17281736

@@ -3046,7 +3054,7 @@ the specific language governing permissions and limitations under the Apache Lic
30463054
if(!this.opts.createSearchChoice && !choices.filter('.select2-result:not(.select2-selected)').length > 0){
30473055
if(!data || data && !data.more && this.results.find(".select2-no-results").length === 0) {
30483056
if (checkFormatter(self.opts.formatNoMatches, "formatNoMatches")) {
3049-
this.results.append("<li class='select2-no-results'>" + evaluate(self.opts.formatNoMatches, self.search.val()) + "</li>");
3057+
this.results.append("<li class='select2-no-results'>" + evaluate(self.opts.formatNoMatches, self.opts.element, self.search.val()) + "</li>");
30503058
}
30513059
}
30523060
}

0 commit comments

Comments
 (0)