Advertisement
Guest User

Untitled

a guest
Jun 30th, 2015
239
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2. jQuery Infinite Pages v0.2.0
  3. https://github.com/magoosh/jquery-infinite-pages
  4. Released under the MIT License
  5.  */
  6. var slice = [].slice;
  7.  
  8. (function($, window) {
  9.   var InfinitePages;
  10.   InfinitePages = (function() {
  11.     InfinitePages.prototype.defaults = {
  12.       debug: false,
  13.       navSelector: 'a[rel=next]',
  14.       buffer: 1000,
  15.       loading: null,
  16.       success: null,
  17.       error: null,
  18.       context: window,
  19.       state: {
  20.         paused: false,
  21.         loading: false
  22.       }
  23.     };
  24.  
  25.     function InfinitePages(container, options) {
  26.       this.options = $.extend({}, this.defaults, options);
  27.       this.$container = $(container);
  28.       this.$table = $(container).find('table');
  29.       this.$context = $(this.options.context);
  30.       this.init();
  31.     }
  32.  
  33.     InfinitePages.prototype.init = function() {
  34.       var scrollHandler, scrollTimeout;
  35.       scrollTimeout = null;
  36.       scrollHandler = ((function(_this) {
  37.         return function() {
  38.           return _this.check();
  39.         };
  40.       })(this));
  41.       return this.$context.scroll(function() {
  42.         if (scrollTimeout) {
  43.           clearTimeout(scrollTimeout);
  44.           scrollTimeout = null;
  45.         }
  46.         return scrollTimeout = setTimeout(scrollHandler, 250);
  47.       });
  48.     };
  49.  
  50.     InfinitePages.prototype._log = function(msg) {
  51.       if (this.options.debug) {
  52.         return typeof console !== "undefined" && console !== null ? console.log(msg) : void 0;
  53.       }
  54.     };
  55.  
  56.     InfinitePages.prototype.check = function() {
  57.       var distance, nav, windowBottom;
  58.       nav = this.$container.find(this.options.navSelector);
  59.       if (nav.size() === 0) {
  60.         return this._log("No more pages to load");
  61.       } else {
  62.         windowBottom = this.$context.scrollTop() + this.$context.height();
  63.         distance = nav.offset().top - windowBottom;
  64.         if (this.options.state.paused) {
  65.           return this._log("Paused");
  66.         } else if (this.options.state.loading) {
  67.           return this._log("Waiting...");
  68.         } else if (distance > this.options.buffer) {
  69.           return this._log((distance - this.options.buffer) + "px remaining...");
  70.         } else {
  71.           return this.next();
  72.         }
  73.       }
  74.     };
  75.  
  76.     InfinitePages.prototype.next = function() {
  77.       if (this.options.state.done) {
  78.         return this._log("Loaded all pages");
  79.       } else {
  80.         this._loading();
  81.         return $.getScript(this.$container.find(this.options.navSelector).attr('href')).done((function(_this) {
  82.           return function() {
  83.             return _this._success();
  84.           };
  85.         })(this)).fail((function(_this) {
  86.           return function() {
  87.             return _this._error();
  88.           };
  89.         })(this));
  90.       }
  91.     };
  92.  
  93.     InfinitePages.prototype._loading = function() {
  94.       this.options.state.loading = true;
  95.       this._log("Loading next page...");
  96.       if (typeof this.options.loading === 'function') {
  97.         return this.$container.find(this.options.navSelector).each(this.options.loading);
  98.       }
  99.     };
  100.  
  101.     InfinitePages.prototype._success = function() {
  102.       this.options.state.loading = false;
  103.       this._log("New page loaded!");
  104.       if (typeof this.options.success === 'function') {
  105.         return this.$container.find(this.options.navSelector).each(this.options.success);
  106.       }
  107.     };
  108.  
  109.     InfinitePages.prototype._error = function() {
  110.       this.options.state.loading = false;
  111.       this._log("Error loading new page :(");
  112.       if (typeof this.options.error === 'function') {
  113.         return this.$container.find(this.options.navSelector).each(this.options.error);
  114.       }
  115.     };
  116.  
  117.     InfinitePages.prototype.pause = function() {
  118.       this.options.state.paused = true;
  119.       return this._log("Scroll checks paused");
  120.     };
  121.  
  122.     InfinitePages.prototype.resume = function() {
  123.       this.options.state.paused = false;
  124.       this._log("Scroll checks resumed");
  125.       return this.check();
  126.     };
  127.  
  128.     return InfinitePages;
  129.  
  130.   })();
  131.   return $.fn.extend({
  132.     infinitePages: function() {
  133.       var args, option;
  134.       option = arguments[0], args = 2 <= arguments.length ? slice.call(arguments, 1) : [];
  135.       return this.each(function() {
  136.         var $this, data;
  137.         $this = $(this);
  138.         data = $this.data('infinitepages');
  139.         if (!data) {
  140.           $this.data('infinitepages', (data = new InfinitePages(this, option)));
  141.         }
  142.         if (typeof option === 'string') {
  143.           return data[option].apply(data, args);
  144.         }
  145.       });
  146.     }
  147.   });
  148. })(window.jQuery, window);
  149. RunLink
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement