UNPKG

@prisma-cms/front-editor

Version:
154 lines 6.58 kB
"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } }); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k); __setModuleDefault(result, mod); return result; }; var __exportStar = (this && this.__exportStar) || function(m, exports) { for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p); }; var __rest = (this && this.__rest) || function (s, e) { var t = {}; for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0) t[p] = s[p]; if (s != null && typeof Object.getOwnPropertySymbols === "function") for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) { if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i])) t[p[i]] = s[p[i]]; } return t; }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.Pagination = void 0; const react_1 = __importStar(require("react")); const withStyles_1 = __importDefault(require("material-ui/styles/withStyles")); const link_1 = __importDefault(require("next/link")); const urijs_1 = __importDefault(require("urijs")); __exportStar(require("./interfaces"), exports); const context_1 = __importDefault(require("@prisma-cms/context")); const styles = { row: { display: 'flex', flexDirection: 'row', flexWrap: 'nowrap', justifyContent: 'center', listStyle: 'none', // margin: 0, padding: 0, }, active: { background: '#ddd', }, control: {}, link: { padding: '3px 6px', border: '1px solid #ddd', marginLeft: 3, marginRight: 3, textDecoration: 'none', '&:hover': { background: '#dfdfdf', }, }, }; class Pagination extends react_1.Component { constructor() { super(...arguments); this.getNewLocation = (page) => { const { router } = this.context; const { pagevariable = 'page' } = this.props; const asPath = router === null || router === void 0 ? void 0 : router.asPath; const uri = new urijs_1.default(asPath); const query = uri.query(true); Object.assign(query, { [pagevariable]: page > 1 ? page : undefined, }); uri.query(query); return uri.resource(); }; } getPage() { const { // pagevariable, page, } = this.props; // const page = this.props[pagevariable]; return page || 1; } render() { const _a = this.props, { classes, limit, total, rowProps } = _a, // pagevariable, other = __rest(_a, ["classes", "limit", "total", "rowProps"]); const page = this.getPage(); if (!page || !limit || !total) { return null; } const pages = Math.ceil(total / limit); if (pages < 2) { return null; } const { row: rowClass, wrapper: wrapperClass, control: controlClass, link: linkClass, active: activeClass, } = classes || {}; const rows = []; if (page > 1) { // var href = this.getNewLocation(1); const href = this.getNewLocation(page - 1); rows.push(react_1.default.createElement("li", { key: "page-1-0", className: controlClass }, react_1.default.createElement(link_1.default, { href: href }, react_1.default.createElement("a", { className: linkClass }, "\u00AB")))); } let lstr = false; let rstr = false; for (let i = 1; i <= pages; i++) { if ((page > 2 && i < page - 1 && i > 1) || (pages - page > 3 && i > page + 1 && i < pages - 1)) { if (!lstr && i > 1 && i < page) { rows.push(react_1.default.createElement("li", { key: i, className: controlClass }, react_1.default.createElement("span", null, "..."))); lstr = true; } if (!rstr && i > page && i < pages) { rows.push(react_1.default.createElement("li", { key: i, className: controlClass }, react_1.default.createElement("span", null, "..."))); rstr = true; } } else { const href = this.getNewLocation(i); rows.push(react_1.default.createElement("li", { key: i, className: controlClass }, react_1.default.createElement(link_1.default, { href: href }, react_1.default.createElement("a", { className: [linkClass, i === page ? activeClass : null].join(' ') }, i)))); } } if (page < pages) { const href = this.getNewLocation(page + 1); rows.push(react_1.default.createElement("li", { key: 'page-' + pages + '-0', className: controlClass }, react_1.default.createElement(link_1.default, { href: href }, react_1.default.createElement("a", { className: linkClass }, "\u00BB")))); } return (react_1.default.createElement("div", Object.assign({ className: [wrapperClass, 'Pagination--root'].join(' ') }, other), react_1.default.createElement("ul", Object.assign({ className: rowClass }, rowProps), rows))); } } exports.Pagination = Pagination; Pagination.contextType = context_1.default; Pagination.defaultProps = { pagevariable: 'page', }; exports.default = withStyles_1.default(styles)((props) => (react_1.default.createElement(Pagination, Object.assign({}, props)))); //# sourceMappingURL=index.js.map