| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173 |
- /*
- * jQuery.autopager v1.0.0
- *
- * Copyright (c) 2009 lagos
- * Dual licensed under the MIT and GPL licenses.
- *
- * http://lagoscript.org
- */
- (function($) {
- var window = this, options = {},
- content, currentUrl, nextUrl,
- active = false,
- defaults = {
- autoLoad: true,
- page: 1,
- content: '.content',
- link: 'a[rel=next]',
- insertBefore: null,
- appendTo: null,
- start: function() {},
- load: function() {},
- disabled: false
- };
- $.autopager = function(_options) {
- var autopager = this.autopager;
- if (typeof _options === 'string' && $.isFunction(autopager[_options])) {
- var args = Array.prototype.slice.call(arguments, 1),
- value = autopager[_options].apply(autopager, args);
- return value === autopager || value === undefined ? this : value;
- }
- _options = $.extend({}, defaults, _options);
- autopager.option(_options);
- content = $(_options.content).filter(':last');
- if (content.length) {
- if (!_options.insertBefore && !_options.appendTo) {
- var insertBefore = content.next();
- if (insertBefore.length) {
- set('insertBefore', insertBefore);
- } else {
- set('appendTo', content.parent());
- }
- }
- }
- setUrl();
- return this;
- };
- $.extend($.autopager, {
- option: function(key, value) {
- var _options = key;
- if (typeof key === "string") {
- if (value === undefined) {
- return options[key];
- }
- _options = {};
- _options[key] = value;
- }
- $.each(_options, function(key, value) {
- set(key, value);
- });
- return this;
- },
- enable: function() {
- set('disabled', false);
- return this;
- },
- disable: function() {
- set('disabled', true);
- return this;
- },
- destroy: function() {
- this.autoLoad(false);
- options = {};
- content = currentUrl = nextUrl = undefined;
- return this;
- },
- autoLoad: function(value) {
- return this.option('autoLoad', value);
- },
- load: function() {
- if (active || !nextUrl || options.disabled) {
- return;
- }
- active = true;
- options.start(currentHash(), nextHash());
- $.get(nextUrl, insertContent);
- return this;
- }
- });
- function set(key, value) {
- switch (key) {
- case 'autoLoad':
- if (value && !options.autoLoad) {
- $(window).scroll(loadOnScroll);
- } else if (!value && options.autoLoad) {
- $(window).unbind('scroll', loadOnScroll);
- }
- break;
- case 'insertBefore':
- if (value) {
- options.appendTo = null;
- }
- break
- case 'appendTo':
- if (value) {
- options.insertBefore = null;
- }
- break
- }
- options[key] = value;
- }
- function setUrl(context) {
- currentUrl = nextUrl || window.location.href;
- nextUrl = $(options.link, context).attr('href');
- }
- function loadOnScroll() {
- if (content.offset().top + content.height() < $(document).scrollTop() + $(window).height()) {
- $.autopager.load();
- }
- }
- function insertContent(res) {
- var _options = options,
- nextPage = $('<div/>').append(res.replace(/<script(.|\s)*?\/script>/g, "")),
- nextContent = nextPage.find(_options.content);
- set('page', _options.page + 1);
- setUrl(nextPage);
- if (nextContent.length) {
- if (_options.insertBefore) {
- nextContent.insertBefore(_options.insertBefore);
- } else {
- nextContent.appendTo(_options.appendTo);
- }
- _options.load.call(nextContent.get(), currentHash(), nextHash());
- content = nextContent.filter(':last');
- }
- active = false;
- }
- function currentHash() {
- return {
- page: options.page,
- url: currentUrl
- };
- }
- function nextHash() {
- return {
- page: options.page + 1,
- url: nextUrl
- };
- }
- })(jQuery);
|