Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function $log(){
- try{console.log.apply(console.log,arguments);}catch(e){}
- }
- var Content_Lazy_Loader = Class.create({
- /**
- *
- * Description: Iterates through an array lazyly, only when a scrollbar reaches
- * the lower part. Used to load content stored in an array on demand.
- *
- * @param data Array (Data MUST BE AN ARRAY!!)
- * @param contentContainer Element
- * @param options Hash/Object
- * displayRecs - the number of records to display at a time
- * iterator - method used to iterate through data, params: callback(currentRecord,start(int),stop(int),size(int))
- * onFirstLoop - method ran only once after completing the first loop
- * afterLoop - method to be ran after every time a loop occurs
- */
- initialize: function(data,contentContainer,options){
- try{
- this.settings = $H({
- 'displayRecs': 10,//number of records to display at once
- 'iterator': Prototype.emptyFunction,//function for use inside the for loop
- 'onFirstLoop': Prototype.emptyFunction,//to be run on the first initial loop through data
- 'afterLoop': Prototype.emptyFunction//to be run everytime a loop ends
- }).update(options);
- this.settings.update({
- 'displayCount':this.settings.get('displayRecs'),//record num to end at
- 'displayStart':0//used to keep track of what records to show next
- });
- this.data = data;//holds Array with data
- this.size = data.size();
- this.contentContainer = contentContainer;//to observe whether we reached bottom of scroller
- this.populateData();
- }catch(e){
- $log(e);
- }
- },
- /**
- * @description Iterates through the array
- */
- populateData: function(){
- try{
- var stop,
- start = this.settings.get('displayStart'),
- count = this.settings.get('displayCount');
- if(count > this.size){//stop trying to show records if we have reached the max
- stop = this.size;
- this.contentContainer.stopObserving('scroll');//stop observing the scrollbar
- }else{
- stop = count;
- }
- for(; start < stop; start++){
- this.settings.get('iterator')(this.data[start],start,stop,this.size);
- }
- this.settings.get('afterLoop')();
- if(this.settings.get('displayStart') === 0){//used to peform the following only once
- this.contentContainer.observe('scroll',this.eventScroll.bind(this));//observes the scrollbar to show more records
- this.settings.get('onFirstLoop')();
- }
- this.settings.update({
- 'displayStart': start,//used for recursion where to start showing records next time load data
- 'displayCount': count + this.settings.get('displayRecs')//used for recursion where to stop showing records next time load data
- });
- }catch(e){
- $log(e);
- }
- },
- /**
- * @description Checks if the obj scrollbar has reached the bottom
- * @param obj the element to check
- * @returns {Boolean}
- */
- isEndScroll: function(obj){
- try{
- return ((obj.scrollTop + obj.offsetHeight) === obj.scrollHeight);
- }catch(e){
- $log(e);
- }
- },
- /**
- * @description Event callback when user scrolls determines whether or not to
- * populate the data.
- */
- eventScroll: function(){
- try{
- if(this.isEndScroll(this.contentContainer)){
- this.populateData(this.data);
- }
- }catch(e){
- $log(e);
- }
- }
- });
Add Comment
Please, Sign In to add comment