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("