File size: 9,205 Bytes
bd98362 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 | /**
* @author Ryan Ogden
*/
;(function($, window) {
'use strict';
var pluginName = 'infiniteScrollHelper',
namespace = 'plugin_' + pluginName;
/*-------------------------------------------- */
/** Plugin Defaults */
/*-------------------------------------------- */
var defaults = {
/**
* The amount of pixels from the bottom of the scrolling element in which the loadMore callback will be invoked
* @type {number}
*/
bottomBuffer: 0,
/**
* The interval, in milliseconds that the scroll event handler will be debounced
* @type {number}
*/
debounceInt: 100,
/**
* A callback that must return `true` or `false`, signaling whether loading has completed. This callback is passed a `pageCount` argument.
* @type {function}
*/
doneLoading: null,
/**
* The interval, in milliseconds, that the doneLoading callback will be called
* @type {number}
*/
interval: 300,
/**
* The class that will be added to the target element once loadMore has been invoked
* @type {string}
*/
loadingClass: 'loading',
/**
* A selector targeting the element that will receive the class specified by the `loadingClass` option
* @type {string}
*/
loadingClassTarget: null,
/**
* The amount of time, in milliseconds, before the loadMore callback is invoked once the bottom of the scroll container has been reached
* @type {number}
*/
loadMoreDelay: 0,
/**
* A callback function that will be invoked when the scrollbar eclipses the bottom threshold of the scrolling element,
* @type {function}
*/
loadMore: $.noop,
/**
* The starting page count that the plugin increment each time loadMore is invoked
* @type {number}
*/
startingPageCount: 1,
/**
* Whether or not the plugin should make an initial call to loadMore. This can be set to true if, for instance, you need to load
* the initial content asynchronously on page load
* @type {boolean}
*/
triggerInitialLoad: false
};
/*-------------------------------------------- */
/** Plugin Constructor */
/*-------------------------------------------- */
/**
* The Plugin constructor
* @constructor
* @param {HTMLElement} element The element that will be monitored
* @param {object} options The plugin options
*/
function Plugin(element, options) {
this.options = $.extend({}, defaults, options);
this.$element = $(element);
this.$win = $(window);
this.$loadingClassTarget = this._getLoadingClassTarget();
this.$scrollContainer = this._getScrollContainer();
this.loading = false;
this.doneLoadingInt = null;
this.pageCount = this.options.triggerInitialLoad ? this.options.startingPageCount - 1 : this.options.startingPageCount;
this.destroyed = false;
this._init();
}
/*-------------------------------------------- */
/** Private Methods */
/*-------------------------------------------- */
/**
* Initializes the plugin
* @private
*/
Plugin.prototype._init = function() {
this._addListeners();
/* Call initial begin load if option is true. If not, simulate a scroll incase
the scroll element container height is greater than the contents */
if (this.options.triggerInitialLoad) {
this._beginLoadMore(this.options.loadMoreDelay);
} else {
this._handleScroll();
}
};
/**
* Returns the element that should have the loading class applied to it when
* the plugin is in the loading state
* @return {jQuery} The jQuery wrapped element
* @private
*/
Plugin.prototype._getLoadingClassTarget = function() {
return this.options.loadingClassTarget ? $(this.options.loadingClassTarget) : this.$element;
};
/**
* Finds the element that acts as the scroll container for the infinite
* scroll content
* @return {jQuery} The jQuery object that wraps the scroll container
*/
Plugin.prototype._getScrollContainer = function() {
var $scrollContainer = null;
// see if the target element is overflow-y:scroll. If so, it is the
// scroll container
if (this.$element.css('overflow-y') == 'scroll') {
$scrollContainer = this.$element;
}
// see if a parent is overflow-y:scroll. If so, it is the scroll container
if (!$scrollContainer) {
$scrollContainer = this.$element.parents().filter(function() {
return $(this).css('overflow-y') == 'scroll';
});
}
// if the target element or any parent aren't overflow-y:scroll,
// assume the window as the scroll container
$scrollContainer = $scrollContainer.length > 0 ? $scrollContainer : this.$win;
return $scrollContainer;
};
/**
* Adds listeners required for plugin to function
* @private
*/
Plugin.prototype._addListeners = function() {
var self = this;
this.$scrollContainer.on('scroll.' + pluginName, debounce(function() {
self._handleScroll();
}, this.options.debounceInt));
};
/**
* Removes all listeners required by the plugin
* @private
*/
Plugin.prototype._removeListeners = function() {
this.$scrollContainer.off('scroll.' + pluginName);
};
/**
* Handles the scroll logic and determins when to trigger the load more callback
* @private
*/
Plugin.prototype._handleScroll = function(e) {
var self = this;
if (this._shouldTriggerLoad()) {
this._beginLoadMore(this.options.loadMoreDelay);
// if a the doneLoading callback was provided, set an interval to check when to call it
if (this.options.doneLoading) {
this.doneLoadingInt = setInterval(
function() {
if (self.options.doneLoading(self.pageCount)) {
self._endLoadMore();
}
},
this.options.interval
);
}
}
};
/**
* Determines if the user scrolled far enough to trigger the load more callback
* @return {boolean} true if the load more callback should be triggered, false otherwise
* @private
*/
Plugin.prototype._shouldTriggerLoad = function() {
var elementBottom = this._getElementHeight(),
scrollBottom = this.$scrollContainer.scrollTop() + this.$scrollContainer.height() + this.options.bottomBuffer;
return (!this.loading && scrollBottom >= elementBottom && this.$element.is(':visible'));
};
/**
* Retrieves the height of the element being scrolled.
* @return {number} The height of the element being scrolled
* @private
*/
Plugin.prototype._getElementHeight = function() {
if (this.$element == this.$scrollContainer) {
return this.$element[0].scrollHeight;
} else {
return this.$element.height();
}
};
/**
* Initialize a call to the loadMore callback and set to loading state
* @param {number} delay The amount of time, in milliseconds, to wait before calling the load more callback
* @private
*/
Plugin.prototype._beginLoadMore = function(delay) {
delay = delay || 0;
setTimeout($.proxy(function() {
this.pageCount++;
this.options.loadMore(this.pageCount, $.proxy(this._endLoadMore, this));
this.$loadingClassTarget.addClass(this.options.loadingClass);
}, this), delay);
this.loading = true;
this._removeListeners();
};
/**
* Return the plugin to the not loading state
* @private
*/
Plugin.prototype._endLoadMore = function() {
clearInterval(this.doneLoadingInt);
this.loading = false;
this.$loadingClassTarget.removeClass(this.options.loadingClass);
!this.destroyed && this._addListeners();
};
/*-------------------------------------------- */
/** Public Methods */
/*-------------------------------------------- */
/**
* Destroys the plugin instance
* @public
*/
Plugin.prototype.destroy = function() {
this._removeListeners();
this.options.loadMore = null;
this.options.doneLoading = null;
$.data(this.$element[0], namespace, null);
clearInterval(this.doneLoadingInt);
this.destroyed = true;
};
/*-------------------------------------------- */
/** Helpers */
/*-------------------------------------------- */
// A utility method for calling methods on the plugin instance
function callMethod(instance, method, args) {
if ( instance && $.isFunction(instance[method]) ) {
instance[method].apply(instance, args);
}
}
// Borrowed from Underscore.js (http://underscorejs.org/)
function debounce(func, wait, immediate) {
var timeout;
return function() {
var context = this, args = arguments;
var later = function() {
timeout = null;
if (!immediate) func.apply(context, args);
};
var callNow = immediate && !timeout;
clearTimeout(timeout);
timeout = setTimeout(later, wait);
if (callNow) func.apply(context, args);
};
}
/*-------------------------------------------- */
/** Plugin Definition */
/*-------------------------------------------- */
$.fn[pluginName] = function(options) {
var method = false,
methodArgs = arguments;
if (typeof options == 'string') {
method = options;
}
return this.each(function() {
var plugin = $.data(this, namespace);
if (!plugin && !method) {
$.data(this, namespace, new Plugin(this, options));
} else if (method) {
callMethod(plugin, method, Array.prototype.slice.call(methodArgs, 1));
}
});
};
// expose plugin constructor on window
window.InfiniteScrollHelper = window.InfiniteScrollHelper || Plugin;
})(jQuery, window); |