bit dojo labs : file explorer

Inspecting: home > js > DojoPager > js > DojoPager.js (download)
//
//  DojoPager JS v1.03
//  03/12/2009
//
//  Copyright (c) 2009 Alan Peng 
//  http://bitdojo.net
//

// var config_example = {
// 	
// 	containerId:  "myPagerContainer",
// 	itemClass:    "myPagerItemClass",
// 	numPerPage:   5,                                // optional
// 	infoId:       "myPagerInfo",                    // optional
// 	infoFormat:   "Page %CurrentPage of %NumPages", // optional
// 	nextActionId: "myPagerNext",                    // optional
// 	prevActionId: "myPagerPrev"                     // optional
// 	
// };

var DojoPager = Class.create({
			
	initialize: function(config){
		
		this.numPerPage = config.numPerPage || 5;
		this.currentPage = 1;
		this.containerId = config.containerId;
		this.itemClass = config.itemClass;
		this.infoId = config.infoId;
		this.infoFormat = config.infoFormat || "%CurrentPage/%NumPages";
		this.nextActionId = config.nextActionId;
		this.prevActionId = config.prevActionId;
		
		this.items = $$("#"+this.containerId+">."+this.itemClass); // #containerId > .itemClass
		this.numItems = this.items.length;
		this.numPages = Math.ceil(this.numItems/this.numPerPage);
		
		this.outerContainer = $(this.containerId);
		this.container = new Element("div");
		this.container.identify();
		this.outerContainer.insert(this.container);

		this.items.each(function(e){
			e.identify();
			e.remove();
		});

		if (this.nextActionId) $(this.nextActionId).observe('click', this.nextPage.bind(this));
		if (this.prevActionId) $(this.prevActionId).observe('click', this.prevPage.bind(this));
		
		this.goToPage(1);
	},
	
	nextPage: function(){
		this.goToPage(this.currentPage+1);
	},
	
	prevPage: function(){
		this.goToPage(this.currentPage-1);
	},
	
	goToPage: function(pageNum){
		
		if(pageNum <= 0) pageNum = this.numPages;
		if(pageNum > this.numPages) pageNum = 1;		
		this.currentPage = pageNum;
		
		var newHolder = new Element("div");
		newHolder.identify();
		newHolder.hide();

		var start = this.currentPage * this.numPerPage - this.numPerPage;
		var end = this.numPerPage + start;
		if(end > this.numItems) end = this.numItems;
		
		for (var i = start; i < end; i++){
			newHolder.insert(this.items[i]);
		};

		this.container.insert(newHolder);
		this.prevHolder = this.currentHolder; 
		this.currentHolder = newHolder;				
		this.currentHolder.show();

		var h = this.currentHolder.getDimensions().height;
		this.outerContainer.style.overflow = "hidden"; 
		this.outerContainer.morph("height:"+h+"px", {duration: 0.5});
		
		if(this.prevHolder) {
			
			this.prevHolder.style.position = "absolute";
			this.prevHolder.style.bottom = "0";
			var wrapper = this.prevHolder.wrap("div");
			wrapper.style.position = "relative";
			wrapper.style.overflow = "hidden";
			wrapper.style.height = this.prevHolder.getHeight()+"px";
			
			Effect.BlindUp(wrapper.identify(), {
				duration: 1.0,
				afterFinish: function(e){
					e.element.hide();
					e.element.remove();
				}
			});
		}
		
		if(this.infoId){
			var s = this.infoFormat.sub("%CurrentPage", this.currentPage);
			s = s.sub("%NumPages", this.numPages);
			$(this.infoId).update(s);
		}
	}
});