@catho/quantum
Version:
Catho react components
93 lines (92 loc) • 4.88 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports["default"] = void 0;
function _slicedToArray(r, e) { return _arrayWithHoles(r) || _iterableToArrayLimit(r, e) || _unsupportedIterableToArray(r, e) || _nonIterableRest(); }
function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
function _iterableToArrayLimit(r, l) { var t = null == r ? null : "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"]; if (null != t) { var e, n, i, u, a = [], f = !0, o = !1; try { if (i = (t = t.call(r)).next, 0 === l) { if (Object(t) !== t) return; f = !1; } else for (; !(f = (e = i.call(t)).done) && (a.push(e.value), a.length !== l); f = !0); } catch (r) { o = !0, n = r; } finally { try { if (!f && null != t["return"] && (u = t["return"](), Object(u) !== u)) return; } finally { if (o) throw n; } } return a; } }
function _arrayWithHoles(r) { if (Array.isArray(r)) return r; }
function _toConsumableArray(r) { return _arrayWithoutHoles(r) || _iterableToArray(r) || _unsupportedIterableToArray(r) || _nonIterableSpread(); }
function _nonIterableSpread() { throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
function _unsupportedIterableToArray(r, a) { if (r) { if ("string" == typeof r) return _arrayLikeToArray(r, a); var t = {}.toString.call(r).slice(8, -1); return "Object" === t && r.constructor && (t = r.constructor.name), "Map" === t || "Set" === t ? Array.from(r) : "Arguments" === t || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(t) ? _arrayLikeToArray(r, a) : void 0; } }
function _iterableToArray(r) { if ("undefined" != typeof Symbol && null != r[Symbol.iterator] || null != r["@@iterator"]) return Array.from(r); }
function _arrayWithoutHoles(r) { if (Array.isArray(r)) return _arrayLikeToArray(r); }
function _arrayLikeToArray(r, a) { (null == a || a > r.length) && (a = r.length); for (var e = 0, n = Array(a); e < a; e++) n[e] = r[e]; return n; }
var centerRule = function centerRule(_ref) {
var total = _ref.total,
active = _ref.active;
if (active - 1 <= 0) {
return 1;
}
if (active === total) {
return active - 3;
}
if (active >= total - 1) {
return active - 2;
}
return active - 1;
};
var getNumberPages = function getNumberPages(active, total) {
if (active <= 3 || active === total) {
return 4;
}
return 3;
};
var pagination = function pagination() {
var _ref2 = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {},
_ref2$totalPages = _ref2.totalPages,
totalPages = _ref2$totalPages === void 0 ? 1 : _ref2$totalPages,
_ref2$activePage = _ref2.activePage,
activePage = _ref2$activePage === void 0 ? 1 : _ref2$activePage,
_ref2$hideLastPaginat = _ref2.hideLastPagination,
hideLastPagination = _ref2$hideLastPaginat === void 0 ? false : _ref2$hideLastPaginat;
if (totalPages <= 5) {
return Array.from({
length: totalPages
}, function (_, i) {
return i + 1;
});
}
var initialPages = [1].concat(_toConsumableArray(Array.from({
length: getNumberPages(activePage, totalPages)
}, function (_, i) {
return i + centerRule({
total: totalPages,
active: activePage
});
})), [totalPages]);
var visiblePages = initialPages.filter(function (page, index, array) {
return array.indexOf(page) === index;
});
var _visiblePages = visiblePages,
_visiblePages2 = _slicedToArray(_visiblePages, 2),
firstPage = _visiblePages2[0],
secondPage = _visiblePages2[1];
if (secondPage === firstPage + 2) {
visiblePages = [firstPage, firstPage + 1].concat(_toConsumableArray(visiblePages.slice(1)));
}
var _visiblePages3 = visiblePages,
penultimatePage = _visiblePages3[visiblePages.length - 2],
lastPage = _visiblePages3[visiblePages.length - 1];
if (penultimatePage === lastPage - 2) {
visiblePages = [].concat(_toConsumableArray(visiblePages.slice(0, -1)), [lastPage - 1, lastPage]);
}
var _visiblePages4 = visiblePages;
var _visiblePages5 = _slicedToArray(_visiblePages4, 2);
firstPage = _visiblePages5[0];
secondPage = _visiblePages5[1];
if (secondPage > firstPage + 2) {
visiblePages = [firstPage, '...'].concat(_toConsumableArray(visiblePages.slice(1)));
}
penultimatePage = visiblePages[visiblePages.length - 2];
lastPage = visiblePages[visiblePages.length - 1];
if (penultimatePage < lastPage - 2) {
visiblePages = [].concat(_toConsumableArray(visiblePages.slice(0, -1)), ['...']);
if (!hideLastPagination) {
visiblePages = [].concat(_toConsumableArray(visiblePages), [lastPage]);
}
}
return visiblePages;
};
var _default = exports["default"] = pagination;