All pastes #78740 Raw Edit

Anonymous

public javascript v1 · immutable
·published 2006-07-04 14:40 UTC
rendered paste body
Pagination = Class.create();/** * * Arguments: *  Object options *   HTMLElement list - The element to use for pagination *                      The script will add LIs to this element, so make sure it's an OL or UL *   Integer maxPages = 5 - the amount of pages to show at max *   Integer currentPage = 1 - the page that is currently viewed *   Integer pageCount - the amount of items in total * */Pagination.prototype = {    initialize : function (options) {        this.setList(options.list);	    this.setMaxPages(options.maxPages || 5);	    this.setCurrentPage(options.currentPage || 1);	    this.setPageCount(options.pageCount);	    this.setLinkUrls(options.linkUrls);	    	    this.clearList();	    this.createLinks();    },	/*	 * Empties the list completely	 */	clearList : function () {		var oldList = this.getList();        //console.log("oldList", oldList);		var newList = oldList.cloneNode(false);        //console.log("newList", newList);		this.setList(newList);        //console.log(oldList.parentNode, oldList.parentNode.replaceChild);		oldList.parentNode.replaceChild(newList, oldList);	},	createLinks : function () {		for (var i = 1, maxPages = this.getMaxPages(), pageCount = this.getPageCount(); i <= maxPages && i <= pageCount; i++) {			this.addLink(i);		}	},	addLink : function (pageNumber) {        this.getList().appendChild(Tag("li", {},            Tag("a", {                    href : this.getLinkUrls() + pageNumber.toString(),                    onclick : function () {                        return false;                        }                    }, pageNumber            )        ));	},    /*     * Accessors     */    /*     * Set the list to be associated with the pagination     */	setList : function (list) {		if (list && list.tagName) {			this.list = list;		} else {			throw new Error("list has to be an instance of HTMLElement");		}		return this;	},	getList : function () {		return this.list;	},	setMaxPages : function (pageCount) {		this.maxPages = parseInt(pageCount, 10);		return this;	},	getMaxPages : function () {		return this.maxPages;	},	setCurrentPage : function (pageNumber) {		this.currentPage = parseInt(pageNumber, 10);	},    setPageCount : function (pageCount) {        this.pageCount = parseInt(pageCount, 10);    },    getPageCount : function () {        return this.pageCount;    },    setLinkUrls : function (linkUrls) {        this.linkUrls = linkUrls;    },    getLinkUrls : function () {        return this.linkUrls;    }};