jquery.autopager-1.0.0.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. /*
  2. * jQuery.autopager v1.0.0
  3. *
  4. * Copyright (c) 2009 lagos
  5. * Dual licensed under the MIT and GPL licenses.
  6. *
  7. * http://lagoscript.org
  8. */
  9. (function($) {
  10. var window = this, options = {},
  11. content, currentUrl, nextUrl,
  12. active = false,
  13. defaults = {
  14. autoLoad: true,
  15. page: 1,
  16. content: '.content',
  17. link: 'a[rel=next]',
  18. insertBefore: null,
  19. appendTo: null,
  20. start: function() {},
  21. load: function() {},
  22. disabled: false
  23. };
  24. $.autopager = function(_options) {
  25. var autopager = this.autopager;
  26. if (typeof _options === 'string' && $.isFunction(autopager[_options])) {
  27. var args = Array.prototype.slice.call(arguments, 1),
  28. value = autopager[_options].apply(autopager, args);
  29. return value === autopager || value === undefined ? this : value;
  30. }
  31. _options = $.extend({}, defaults, _options);
  32. autopager.option(_options);
  33. content = $(_options.content).filter(':last');
  34. if (content.length) {
  35. if (!_options.insertBefore && !_options.appendTo) {
  36. var insertBefore = content.next();
  37. if (insertBefore.length) {
  38. set('insertBefore', insertBefore);
  39. } else {
  40. set('appendTo', content.parent());
  41. }
  42. }
  43. }
  44. setUrl();
  45. return this;
  46. };
  47. $.extend($.autopager, {
  48. option: function(key, value) {
  49. var _options = key;
  50. if (typeof key === "string") {
  51. if (value === undefined) {
  52. return options[key];
  53. }
  54. _options = {};
  55. _options[key] = value;
  56. }
  57. $.each(_options, function(key, value) {
  58. set(key, value);
  59. });
  60. return this;
  61. },
  62. enable: function() {
  63. set('disabled', false);
  64. return this;
  65. },
  66. disable: function() {
  67. set('disabled', true);
  68. return this;
  69. },
  70. destroy: function() {
  71. this.autoLoad(false);
  72. options = {};
  73. content = currentUrl = nextUrl = undefined;
  74. return this;
  75. },
  76. autoLoad: function(value) {
  77. return this.option('autoLoad', value);
  78. },
  79. load: function() {
  80. if (active || !nextUrl || options.disabled) {
  81. return;
  82. }
  83. active = true;
  84. options.start(currentHash(), nextHash());
  85. $.get(nextUrl, insertContent);
  86. return this;
  87. }
  88. });
  89. function set(key, value) {
  90. switch (key) {
  91. case 'autoLoad':
  92. if (value && !options.autoLoad) {
  93. $(window).scroll(loadOnScroll);
  94. } else if (!value && options.autoLoad) {
  95. $(window).unbind('scroll', loadOnScroll);
  96. }
  97. break;
  98. case 'insertBefore':
  99. if (value) {
  100. options.appendTo = null;
  101. }
  102. break
  103. case 'appendTo':
  104. if (value) {
  105. options.insertBefore = null;
  106. }
  107. break
  108. }
  109. options[key] = value;
  110. }
  111. function setUrl(context) {
  112. currentUrl = nextUrl || window.location.href;
  113. nextUrl = $(options.link, context).attr('href');
  114. }
  115. function loadOnScroll() {
  116. if (content.offset().top + content.height() < $(document).scrollTop() + $(window).height()) {
  117. $.autopager.load();
  118. }
  119. }
  120. function insertContent(res) {
  121. var _options = options,
  122. nextPage = $('<div/>').append(res.replace(/<script(.|\s)*?\/script>/g, "")),
  123. nextContent = nextPage.find(_options.content);
  124. set('page', _options.page + 1);
  125. setUrl(nextPage);
  126. if (nextContent.length) {
  127. if (_options.insertBefore) {
  128. nextContent.insertBefore(_options.insertBefore);
  129. } else {
  130. nextContent.appendTo(_options.appendTo);
  131. }
  132. _options.load.call(nextContent.get(), currentHash(), nextHash());
  133. content = nextContent.filter(':last');
  134. }
  135. active = false;
  136. }
  137. function currentHash() {
  138. return {
  139. page: options.page,
  140. url: currentUrl
  141. };
  142. }
  143. function nextHash() {
  144. return {
  145. page: options.page + 1,
  146. url: nextUrl
  147. };
  148. }
  149. })(jQuery);