1
0
mirror of synced 2024-11-26 14:56:07 +03:00

delay finding the body until it is needed and cache it.

This commit is contained in:
Igor Vaynberg 2012-07-17 12:01:22 +03:00
parent 3ed2cc091a
commit 7947803df4

View File

@ -197,6 +197,20 @@
}; };
} }
/**
* A simple implementation of a thunk
* @param formula function used to lazily initialize the thunk
* @return {Function}
*/
function thunk(formula) {
var evaluated = false,
value;
return function() {
if (evaluated === false) { value = formula(); evaluated=true; }
return value;
};
};
function installDebouncedScroll(threshold, element) { function installDebouncedScroll(threshold, element) {
var notify = debounce(threshold, function (e) { element.trigger("scroll-debounced", e);}); var notify = debounce(threshold, function (e) { element.trigger("scroll-debounced", e);});
element.bind("scroll", function (e) { element.bind("scroll", function (e) {
@ -429,7 +443,9 @@
this.enabled=true; this.enabled=true;
this.container = this.createContainer(); this.container = this.createContainer();
this.body = $("body"); // cache for future access
// cache the body so future lookups are cheap
this.body = thunk(function() { return opts.element.closest("body"); });
if (opts.element.attr("class") !== undefined) { if (opts.element.attr("class") !== undefined) {
this.container.addClass(opts.element.attr("class")); this.container.addClass(opts.element.attr("class"));
@ -720,7 +736,7 @@
viewportBottom = $(window).scrollTop() + document.documentElement.clientHeight, viewportBottom = $(window).scrollTop() + document.documentElement.clientHeight,
dropTop = offset.top + height, dropTop = offset.top + height,
enoughRoomBelow = dropTop + dropHeight <= viewportBottom, enoughRoomBelow = dropTop + dropHeight <= viewportBottom,
enoughRoomAbove = (offset.top - dropHeight) >= document.body.scrollTop, enoughRoomAbove = (offset.top - dropHeight) >= this.body().scrollTop,
aboveNow = this.dropdown.hasClass("select2-drop-above"), aboveNow = this.dropdown.hasClass("select2-drop-above"),
above, above,
css; css;
@ -803,8 +819,8 @@
this.updateResults(true); this.updateResults(true);
if(this.dropdown[0] !== this.body.children().last()[0]) { if(this.dropdown[0] !== this.body().children().last()[0]) {
this.dropdown.detach().appendTo(this.body); this.dropdown.detach().appendTo(this.body());
} }
this.dropdown.show(); this.dropdown.show();