Guest User

Untitled

a guest
Jul 20th, 2018
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.75 KB | None | 0 0
  1. function $log(){
  2. try{console.log.apply(console.log,arguments);}catch(e){}
  3. }
  4. var Content_Lazy_Loader = Class.create({
  5. /**
  6. *
  7. * Description: Iterates through an array lazyly, only when a scrollbar reaches
  8. * the lower part. Used to load content stored in an array on demand.
  9. *
  10. * @param data Array (Data MUST BE AN ARRAY!!)
  11. * @param contentContainer Element
  12. * @param options Hash/Object
  13. * displayRecs - the number of records to display at a time
  14. * iterator - method used to iterate through data, params: callback(currentRecord,start(int),stop(int),size(int))
  15. * onFirstLoop - method ran only once after completing the first loop
  16. * afterLoop - method to be ran after every time a loop occurs
  17. */
  18. initialize: function(data,contentContainer,options){
  19. try{
  20. this.settings = $H({
  21. 'displayRecs': 10,//number of records to display at once
  22. 'iterator': Prototype.emptyFunction,//function for use inside the for loop
  23. 'onFirstLoop': Prototype.emptyFunction,//to be run on the first initial loop through data
  24. 'afterLoop': Prototype.emptyFunction//to be run everytime a loop ends
  25. }).update(options);
  26.  
  27. this.settings.update({
  28. 'displayCount':this.settings.get('displayRecs'),//record num to end at
  29. 'displayStart':0//used to keep track of what records to show next
  30. });
  31.  
  32. this.data = data;//holds Array with data
  33. this.size = data.size();
  34. this.contentContainer = contentContainer;//to observe whether we reached bottom of scroller
  35. this.populateData();
  36. }catch(e){
  37. $log(e);
  38. }
  39. },
  40. /**
  41. * @description Iterates through the array
  42. */
  43. populateData: function(){
  44. try{
  45. var stop,
  46. start = this.settings.get('displayStart'),
  47. count = this.settings.get('displayCount');
  48.  
  49. if(count > this.size){//stop trying to show records if we have reached the max
  50. stop = this.size;
  51. this.contentContainer.stopObserving('scroll');//stop observing the scrollbar
  52. }else{
  53. stop = count;
  54. }
  55. for(; start < stop; start++){
  56. this.settings.get('iterator')(this.data[start],start,stop,this.size);
  57. }
  58. this.settings.get('afterLoop')();
  59. if(this.settings.get('displayStart') === 0){//used to peform the following only once
  60. this.contentContainer.observe('scroll',this.eventScroll.bind(this));//observes the scrollbar to show more records
  61. this.settings.get('onFirstLoop')();
  62. }
  63. this.settings.update({
  64. 'displayStart': start,//used for recursion where to start showing records next time load data
  65. 'displayCount': count + this.settings.get('displayRecs')//used for recursion where to stop showing records next time load data
  66. });
  67. }catch(e){
  68. $log(e);
  69. }
  70. },
  71. /**
  72. * @description Checks if the obj scrollbar has reached the bottom
  73. * @param obj the element to check
  74. * @returns {Boolean}
  75. */
  76. isEndScroll: function(obj){
  77. try{
  78. return ((obj.scrollTop + obj.offsetHeight) === obj.scrollHeight);
  79. }catch(e){
  80. $log(e);
  81. }
  82. },
  83. /**
  84. * @description Event callback when user scrolls determines whether or not to
  85. * populate the data.
  86. */
  87. eventScroll: function(){
  88. try{
  89. if(this.isEndScroll(this.contentContainer)){
  90. this.populateData(this.data);
  91. }
  92. }catch(e){
  93. $log(e);
  94. }
  95. }
  96. });
Add Comment
Please, Sign In to add comment