From 43e381ed3740ef5a72d3d2fa496dace89c9c1403 Mon Sep 17 00:00:00 2001 From: Coleman Watts Date: Wed, 5 Mar 2014 21:02:06 -0500 Subject: [PATCH] Provide context for evaluated functions --- select2.js | 44 ++++++++++++++++++++++++++------------------ 1 file changed, 26 insertions(+), 18 deletions(-) diff --git a/select2.js b/select2.js index d5af816f..3f2c0c48 100644 --- a/select2.js +++ b/select2.js @@ -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("
  • " + evaluate(opts.formatSelectionTooBig, maxSelSize) + "
  • "); + render("
  • " + evaluate(opts.formatSelectionTooBig, opts.element, maxSelSize) + "
  • "); return; } } if (search.val().length < opts.minimumInputLength) { if (checkFormatter(opts.formatInputTooShort, "formatInputTooShort")) { - render("
  • " + evaluate(opts.formatInputTooShort, search.val(), opts.minimumInputLength) + "
  • "); + render("
  • " + evaluate(opts.formatInputTooShort, opts.element, search.val(), opts.minimumInputLength) + "
  • "); } 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("
  • " + evaluate(opts.formatInputTooLong, search.val(), opts.maximumInputLength) + "
  • "); + render("
  • " + evaluate(opts.formatInputTooLong, opts.element, search.val(), opts.maximumInputLength) + "
  • "); } 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("
  • " + evaluate(opts.formatSearching) + "
  • "); + render("
  • " + evaluate(opts.formatSearching, opts.element) + "
  • "); } 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("
  • " + evaluate(opts.formatNoMatches, search.val()) + "
  • "); + render("
  • " + evaluate(opts.formatNoMatches, opts.element, search.val()) + "
  • "); 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("
  • " + self.opts.escapeMarkup(evaluate(opts.formatLoadMore, this.resultsPage)) + "
  • "); + results.append("
  • " + opts.escapeMarkup(evaluate(opts.formatLoadMore, opts.element, this.resultsPage)) + "
  • "); 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("
  • " + evaluate(self.opts.formatNoMatches, self.search.val()) + "
  • "); + this.results.append("
  • " + evaluate(self.opts.formatNoMatches, self.opts.element, self.search.val()) + "
  • "); } } }