Guest User

Untitled

a guest
May 17th, 2018
243
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.72 KB | None | 0 0
  1. // =======================================================================
  2. // EndlessPage - implement endless page with will_paginate plugin
  3. //
  4. // Author: Shawn Veader (shawn@veader.org)
  5. //
  6. // Parameters:
  7. // total_page: total number of pages, found in @collection.page_count
  8. // url: URL used to request more data
  9. // auth_token: the authenticity token needed for Rails 2.X
  10. //
  11. // Requires: Prototype for Javascript and will_paginate plugin for Rails
  12. // =======================================================================
  13. var EndlessPage = Class.create();
  14. EndlessPage.prototype = {
  15. initialize: function (total_pages, url, auth_token) {
  16. this.timer = null;
  17. this.current_page = 1;
  18. this.total_pages = total_pages;
  19. this.ajax_path = url;
  20. this.interval = 1000; // 1 second
  21. this.scroll_offset = 0.6; // 60%
  22. this.auth_token = auth_token;
  23.  
  24. // start the listener
  25. this.start_listener();
  26. },
  27.  
  28. stop_listener: function () {
  29. this.timer = null;
  30. },
  31.  
  32. start_listener: function () {
  33. this.timer = setTimeout('ep._check_scroll()', this.interval);
  34. },
  35.  
  36. _check_scroll: function () {
  37. if(this.timer == undefined || this.total_pages == this.current_page) {
  38. // listener was stopped or we've run out of pages
  39. return;
  40. }
  41.  
  42. var offset = document.viewport.getScrollOffsets()[1]; // second of the pair is the horizontal scroll
  43.  
  44. // if slider past our scroll offset, then fire a request for more data
  45. if(offset/document.viewport.getHeight() > this.scroll_offset) {
  46. this.current_page++; // move to next page
  47. new Ajax.Request(this.ajax_path, { parameters: { authenticity_token: this.auth_token, page: this.current_page } });
  48. }
  49.  
  50. // start the listener again
  51. this.start_listener();
  52. }
  53. };
Add Comment
Please, Sign In to add comment