ultimate-pagination
Version:
Universal pagination model generation algorithm that can be used to build a UI component
304 lines (288 loc) • 12 kB
JavaScript
(function webpackUniversalModuleDefinition(root, factory) {
if(typeof exports === 'object' && typeof module === 'object')
module.exports = factory();
else if(typeof define === 'function' && define.amd)
define([], factory);
else if(typeof exports === 'object')
exports["ultimatePagination"] = factory();
else
root["ultimatePagination"] = factory();
})(this, function() {
return /******/ (function(modules) { // webpackBootstrap
/******/ // The module cache
/******/ var installedModules = {};
/******/
/******/ // The require function
/******/ function __webpack_require__(moduleId) {
/******/
/******/ // Check if module is in cache
/******/ if(installedModules[moduleId])
/******/ return installedModules[moduleId].exports;
/******/
/******/ // Create a new module (and put it into the cache)
/******/ var module = installedModules[moduleId] = {
/******/ i: moduleId,
/******/ l: false,
/******/ exports: {}
/******/ };
/******/
/******/ // Execute the module function
/******/ modules[moduleId].call(module.exports, module, module.exports, __webpack_require__);
/******/
/******/ // Flag the module as loaded
/******/ module.l = true;
/******/
/******/ // Return the exports of the module
/******/ return module.exports;
/******/ }
/******/
/******/
/******/ // expose the modules object (__webpack_modules__)
/******/ __webpack_require__.m = modules;
/******/
/******/ // expose the module cache
/******/ __webpack_require__.c = installedModules;
/******/
/******/ // identity function for calling harmony imports with the correct context
/******/ __webpack_require__.i = function(value) { return value; };
/******/
/******/ // define getter function for harmony exports
/******/ __webpack_require__.d = function(exports, name, getter) {
/******/ if(!__webpack_require__.o(exports, name)) {
/******/ Object.defineProperty(exports, name, {
/******/ configurable: false,
/******/ enumerable: true,
/******/ get: getter
/******/ });
/******/ }
/******/ };
/******/
/******/ // getDefaultExport function for compatibility with non-harmony modules
/******/ __webpack_require__.n = function(module) {
/******/ var getter = module && module.__esModule ?
/******/ function getDefault() { return module['default']; } :
/******/ function getModuleExports() { return module; };
/******/ __webpack_require__.d(getter, 'a', getter);
/******/ return getter;
/******/ };
/******/
/******/ // Object.prototype.hasOwnProperty.call
/******/ __webpack_require__.o = function(object, property) { return Object.prototype.hasOwnProperty.call(object, property); };
/******/
/******/ // __webpack_public_path__
/******/ __webpack_require__.p = "";
/******/
/******/ // Load entry module and return exports
/******/ return __webpack_require__(__webpack_require__.s = 3);
/******/ })
/************************************************************************/
/******/ ([
/* 0 */
/***/ (function(module, exports, __webpack_require__) {
;
exports.ITEM_TYPES = {
PAGE: 'PAGE',
ELLIPSIS: 'ELLIPSIS',
FIRST_PAGE_LINK: 'FIRST_PAGE_LINK',
PREVIOUS_PAGE_LINK: 'PREVIOUS_PAGE_LINK',
NEXT_PAGE_LINK: 'NEXT_PAGE_LINK',
LAST_PAGE_LINK: 'LAST_PAGE_LINK'
};
exports.ITEM_KEYS = {
FIRST_ELLIPSIS: -1,
SECOND_ELLIPSIS: -2,
FIRST_PAGE_LINK: -3,
PREVIOUS_PAGE_LINK: -4,
NEXT_PAGE_LINK: -5,
LAST_PAGE_LINK: -6
};
/***/ }),
/* 1 */
/***/ (function(module, exports, __webpack_require__) {
;
var ultimate_pagination_constants_1 = __webpack_require__(0);
exports.createFirstEllipsis = function (pageNumber) {
return {
type: ultimate_pagination_constants_1.ITEM_TYPES.ELLIPSIS,
key: ultimate_pagination_constants_1.ITEM_KEYS.FIRST_ELLIPSIS,
value: pageNumber,
isActive: false
};
};
exports.createSecondEllipsis = function (pageNumber) {
return {
type: ultimate_pagination_constants_1.ITEM_TYPES.ELLIPSIS,
key: ultimate_pagination_constants_1.ITEM_KEYS.SECOND_ELLIPSIS,
value: pageNumber,
isActive: false
};
};
exports.createFirstPageLink = function (options) {
var currentPage = options.currentPage;
return {
type: ultimate_pagination_constants_1.ITEM_TYPES.FIRST_PAGE_LINK,
key: ultimate_pagination_constants_1.ITEM_KEYS.FIRST_PAGE_LINK,
value: 1,
isActive: currentPage === 1
};
};
exports.createPreviousPageLink = function (options) {
var currentPage = options.currentPage;
return {
type: ultimate_pagination_constants_1.ITEM_TYPES.PREVIOUS_PAGE_LINK,
key: ultimate_pagination_constants_1.ITEM_KEYS.PREVIOUS_PAGE_LINK,
value: Math.max(1, currentPage - 1),
isActive: currentPage === 1
};
};
exports.createNextPageLink = function (options) {
var currentPage = options.currentPage, totalPages = options.totalPages;
return {
type: ultimate_pagination_constants_1.ITEM_TYPES.NEXT_PAGE_LINK,
key: ultimate_pagination_constants_1.ITEM_KEYS.NEXT_PAGE_LINK,
value: Math.min(totalPages, currentPage + 1),
isActive: currentPage === totalPages
};
};
exports.createLastPageLink = function (options) {
var currentPage = options.currentPage, totalPages = options.totalPages;
return {
type: ultimate_pagination_constants_1.ITEM_TYPES.LAST_PAGE_LINK,
key: ultimate_pagination_constants_1.ITEM_KEYS.LAST_PAGE_LINK,
value: totalPages,
isActive: currentPage === totalPages
};
};
exports.createPageFunctionFactory = function (options) {
var currentPage = options.currentPage;
return function (pageNumber) {
return {
type: ultimate_pagination_constants_1.ITEM_TYPES.PAGE,
key: pageNumber,
value: pageNumber,
isActive: pageNumber === currentPage
};
};
};
/***/ }),
/* 2 */
/***/ (function(module, exports, __webpack_require__) {
;
function createRange(start, end) {
var range = [];
for (var i = start; i <= end; i++) {
range.push(i);
}
return range;
}
exports.createRange = createRange;
/***/ }),
/* 3 */
/***/ (function(module, exports, __webpack_require__) {
;
var ultimate_pagination_utils_1 = __webpack_require__(2);
var ultimate_pagination_item_factories_1 = __webpack_require__(1);
function getPaginationModel(options) {
if (options == null) {
throw new Error('getPaginationModel(): options object should be a passed');
}
var totalPages = Number(options.totalPages);
if (isNaN(totalPages)) {
throw new Error('getPaginationModel(): totalPages should be a number');
}
if (totalPages < 0) {
throw new Error('getPaginationModel(): totalPages shouldn\'t be a negative number');
}
var currentPage = Number(options.currentPage);
if (isNaN(currentPage)) {
throw new Error('getPaginationModel(): currentPage should be a number');
}
if (currentPage < 0) {
throw new Error('getPaginationModel(): currentPage shouldn\'t be a negative number');
}
if (currentPage > totalPages) {
throw new Error('getPaginationModel(): currentPage shouldn\'t be greater than totalPages');
}
var boundaryPagesRange = (options.boundaryPagesRange == null ? 1 : Number(options.boundaryPagesRange));
if (isNaN(boundaryPagesRange)) {
throw new Error('getPaginationModel(): boundaryPagesRange should be a number');
}
if (boundaryPagesRange < 0) {
throw new Error('getPaginationModel(): boundaryPagesRange shouldn\'t be a negative number');
}
var siblingPagesRange = (options.siblingPagesRange == null ? 1 : Number(options.siblingPagesRange));
if (isNaN(siblingPagesRange)) {
throw new Error('getPaginationModel(): siblingPagesRange should be a number');
}
if (siblingPagesRange < 0) {
throw new Error('getPaginationModel(): siblingPagesRange shouldn\'t be a negative number');
}
var hidePreviousAndNextPageLinks = Boolean(options.hidePreviousAndNextPageLinks);
var hideFirstAndLastPageLinks = Boolean(options.hideFirstAndLastPageLinks);
var hideEllipsis = Boolean(options.hideEllipsis);
var ellipsisSize = (hideEllipsis ? 0 : 1);
var paginationModel = [];
var createPage = ultimate_pagination_item_factories_1.createPageFunctionFactory(options);
if (!hideFirstAndLastPageLinks) {
paginationModel.push(ultimate_pagination_item_factories_1.createFirstPageLink(options));
}
if (!hidePreviousAndNextPageLinks) {
paginationModel.push(ultimate_pagination_item_factories_1.createPreviousPageLink(options));
}
// Simplify generation of pages if number of available items is equal or greater than total pages to show
if (1 + 2 * ellipsisSize + 2 * siblingPagesRange + 2 * boundaryPagesRange >= totalPages) {
var allPages = ultimate_pagination_utils_1.createRange(1, totalPages).map(createPage);
paginationModel.push.apply(paginationModel, allPages);
}
else {
// Calculate group of first pages
var firstPagesStart = 1;
var firstPagesEnd = boundaryPagesRange;
var firstPages = ultimate_pagination_utils_1.createRange(firstPagesStart, firstPagesEnd).map(createPage);
// Calculate group of last pages
var lastPagesStart = totalPages + 1 - boundaryPagesRange;
var lastPagesEnd = totalPages;
var lastPages = ultimate_pagination_utils_1.createRange(lastPagesStart, lastPagesEnd).map(createPage);
// Calculate group of main pages
var mainPagesStart = Math.min(Math.max(currentPage - siblingPagesRange, firstPagesEnd + ellipsisSize + 1), lastPagesStart - ellipsisSize - 2 * siblingPagesRange - 1);
var mainPagesEnd = mainPagesStart + 2 * siblingPagesRange;
var mainPages = ultimate_pagination_utils_1.createRange(mainPagesStart, mainPagesEnd).map(createPage);
// Add group of first pages
paginationModel.push.apply(paginationModel, firstPages);
if (!hideEllipsis) {
// Calculate and add ellipsis before group of main pages
var firstEllipsisPageNumber = mainPagesStart - 1;
var showPageInsteadOfFirstEllipsis = (firstEllipsisPageNumber === firstPagesEnd + 1);
var createFirstEllipsisOrPage = showPageInsteadOfFirstEllipsis ? createPage : ultimate_pagination_item_factories_1.createFirstEllipsis;
var firstEllipsis = createFirstEllipsisOrPage(firstEllipsisPageNumber);
paginationModel.push(firstEllipsis);
}
// Add group of main pages
paginationModel.push.apply(paginationModel, mainPages);
if (!hideEllipsis) {
// Calculate and add ellipsis after group of main pages
var secondEllipsisPageNumber = mainPagesEnd + 1;
var showPageInsteadOfSecondEllipsis = (secondEllipsisPageNumber === lastPagesStart - 1);
var createSecondEllipsisOrPage = showPageInsteadOfSecondEllipsis ? createPage : ultimate_pagination_item_factories_1.createSecondEllipsis;
var secondEllipsis = createSecondEllipsisOrPage(secondEllipsisPageNumber);
paginationModel.push(secondEllipsis);
}
// Add group of last pages
paginationModel.push.apply(paginationModel, lastPages);
}
if (!hidePreviousAndNextPageLinks) {
paginationModel.push(ultimate_pagination_item_factories_1.createNextPageLink(options));
}
if (!hideFirstAndLastPageLinks) {
paginationModel.push(ultimate_pagination_item_factories_1.createLastPageLink(options));
}
return paginationModel;
}
exports.getPaginationModel = getPaginationModel;
var ultimate_pagination_constants_1 = __webpack_require__(0);
exports.ITEM_TYPES = ultimate_pagination_constants_1.ITEM_TYPES;
exports.ITEM_KEYS = ultimate_pagination_constants_1.ITEM_KEYS;
/***/ })
/******/ ]);
});
//# sourceMappingURL=ultimate-pagination.js.map