import $ from "jquery"; /** Builds (twitter bootstrap styled) pagination controls. * If the totalDataSize is not null, a horizontal list of page buttons is displayed. * If totalDataSize is null, two links ('Prev' and 'Next) are displayed. * When pages are changed, a 'pagination.page-change' event is fired * sending the event and the (0-based) page requested. */ function Pagination(element, options) { /** the total number of pages */ this.numPages = null; /** the current, active page */ this.currPage = 0; return this.init(element, options); } /** data key under which this object will be stored in the element */ Pagination.prototype.DATA_KEY = "pagination"; /** default options */ Pagination.prototype.defaults = { /** which page to begin at */ startingPage: 0, /** number of data per page */ perPage: 20, /** the total number of data (null == unknown) */ totalDataSize: null, /** size of current data on current page */ currDataSize: null, }; /** init the control, calc numPages if possible, and render * @param {jQuery} the element that will contain the pagination control * @param {Object} options a map containing overrides to the pagination default options */ Pagination.prototype.init = function _init($element, options) { options = options || {}; this.$element = $element; this.options = $.extend(true, {}, this.defaults, options); this.currPage = this.options.startingPage; if (this.options.totalDataSize !== null) { this.numPages = Math.ceil(this.options.totalDataSize / this.options.perPage); // limit currPage by numPages if (this.currPage >= this.numPages) { this.currPage = this.numPages - 1; } } //console.debug( 'Pagination.prototype.init:', this.$element, this.currPage ); //console.debug( JSON.stringify( this.options ) ); // bind to data of element this.$element.data(Pagination.prototype.DATA_KEY, this); this._render(); return this; }; /** helper to create a simple li + a combo */ function _make$Li(contents) { return $(['
  • ', contents, "
  • "].join("")); } /** render previous and next pagination buttons */ Pagination.prototype._render = function __render() { // no data - no pagination if (this.options.totalDataSize === 0) { return this; } // only one page if (this.numPages === 1) { return this; } // when the number of pages are known, render each page as a link if (this.numPages > 0) { this._renderPages(); this._scrollToActivePage(); // when the number of pages is not known, render previous or next } else { this._renderPrevNext(); } return this; }; /** render previous and next pagination buttons */ Pagination.prototype._renderPrevNext = function __renderPrevNext() { var pagination = this; var $prev = _make$Li("Prev"); var $next = _make$Li("Next"); var $paginationContainer = $("