Skip to content

Instantly share code, notes, and snippets.

@petvas
Last active February 2, 2016 15:37
Show Gist options
  • Save petvas/9c93f2bc5ba9f0d77474 to your computer and use it in GitHub Desktop.
Save petvas/9c93f2bc5ba9f0d77474 to your computer and use it in GitHub Desktop.
simple pagination paginator
/*global define, module */
/**
*
* Paginator object facotry
* @param {[Array]} dv dataset
* @param {[Integer]} ipp items per page
* @return {[Object]} paginator object
*
* @example <caption>Example usage.</caption>
* var p = paginator([0,1,2,3,4,5,6,7,8,9,0] , 3, function (currentPaginator) {console.log(currentPaginator.current())});
* p.totalPage(); // 4 !!!carefull index start counting at 0 max index is 3!!!
* p.index(); // 0
* p.prev(); // false
* p.next(); // true console: [3,4,5]
* p.index(); // 1
* p.prev(); // true console: [1,2,3]
* p.index(); // 0
* p.current(); // [1,2,3]
* p.hasNext(); // true
* p.hasPrev(); // false
*
* p.index(3); // 3 [9,0]
* p.hasNext(); // false
*
* p.data([1,2,3,4]);
* p.current(); //exception: index out of range
*
* @author Peter Vaszari
*
* @license
* The MIT License (MIT)
* Copyright (c) 2016 Peter Vaszari
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
(function (root, factory) {
'use strict';
if (typeof define === 'function' && define.amd) {
// AMD. Register as an anonymous module.
define([], factory);
} else if (typeof module === 'object' && module.exports) {
// Node. Does not work with strict CommonJS, but
// only CommonJS-like environments that support module.exports,
// like Node.
module.exports = factory();
} else {
// Browser globals (root is window)
root.paginator = factory();
}
}(this, function () {
'use strict';
return function (dv, ipp, cb) {
var onChnage;
var dataValue;
var itemsPerPageValue;
var indexValue = 0;
var that;
/**
* Set or get items per page
* @param {[integer]} value num. of items per page
* @return {[integer]} num. of items per page
*/
function itemsPerPage(value) {
if (value !== undefined) {
value = +value;
if (isNaN(value) || value % 1 !== 0) {
throw new Error('itemsPerPage is not integer');
}
itemsPerPageValue = value;
}
return itemsPerPageValue;
}
/**
* Get total nuber of pages
* @return {integer} total nuber of pages
*/
function totalPage(value) {
if (value !== undefined) {
throw new Error('totalPage is not writable');
}
return Math.ceil(dataValue.length / itemsPerPage());
}
/**
* Return position is valid or not
* @param {[integer]} position
* @return {Boolean} position is valid
*/
function hasIndex(position) {
return (position >= 0 && position < totalPage());
}
/**
* Set or execute onChange callback function
* @param {Function} cb callback function
* @return void
*/
function change(cb) {
if (cb !== undefined) {
if (typeof cb !== 'function') {
throw new Error('callback need to be function');
}
onChnage = cb;
return;
}
if (onChnage) {
onChnage(that);
}
}
/**
* Set or get parginator index
* @param {[integer]} value index
* @return {[integer]} index
*/
function index(value) {
if (value !== undefined && index() !== value) {
value = +value;
if (isNaN(value) || value % 1 !== 0) {
throw new Error('index is not integer');
}
if (!hasIndex(value)) {
throw new Error('index is out of range');
}
indexValue = value;
change();
}
return indexValue;
}
/**
* Get current items
* @return {array} slice of original array
*/
function current(value) {
if (value !== undefined) {
throw new Error('current is not writable');
}
if (dataValue === undefined) {
throw new Error('data not set');
}
if (!hasIndex(index())) {
throw new Error('index out of range');
}
return dataValue.slice(index() * itemsPerPage(), (index() + 1) * itemsPerPage());
}
/**
* Set or Get dataset
* @param {[array]} value dataset
* @return {[array]} dataset
*/
function data(value) {
if (value !== undefined) {
if (!Array.isArray(value)) {
throw new Error('data is not array');
}
dataValue = value;
}
return dataValue;
}
/**
* Reutrn next postion is valid or not
* @param {[integer]} skip postion
* @return {Boolean} postion is valid
*/
function hasNext(skip) {
skip = +skip || 0;
return hasIndex(index() + 1 + skip);
}
/**
* return previous postion is valid
* @param {[integer]} skip postion
* @return {Boolean} postion is valid
*/
function hasPrev(skip) {
skip = +skip || 0;
return hasIndex(index() - 1 - skip);
}
/**
* Go to next position
* @param {[integer]} skip move more
* @return {Boolean} move was sucessfull
*/
function next(skip) {
skip = +skip || 0;
if (!hasNext(skip)) {
return false;
}
index(index() + 1 + skip);
return true;
}
/**
* Go to previous position
* @param {[integer]} skip move more
* @return {Boolean} move was sucessfull
*/
function prev(skip) {
skip = +skip || 0;
if (!hasPrev(skip)) {
return false;
}
index(index() - 1 - skip);
return false;
}
if (dv) {
data(dv);
}
if (ipp) {
itemsPerPage(ipp);
}
if (cb) {
change(cb);
}
that = {
change: change,
current: current,
data: data,
hasIndex: hasIndex,
hasNext: hasNext,
hasPrev: hasPrev,
index: index,
itemsPerPage: itemsPerPage,
next: next,
prev: prev,
totalPage: totalPage
};
return that;
};
}));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment