UNPKG

@iredium/butterfly

Version:
211 lines (210 loc) 9.75 kB
"use strict"; var __assign = (this && this.__assign) || function () { __assign = Object.assign || function(t) { for (var s, i = 1, n = arguments.length; i < n; i++) { s = arguments[i]; for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p]; } return t; }; return __assign.apply(this, arguments); }; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; var __generator = (this && this.__generator) || function (thisArg, body) { var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g; return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g; function verb(n) { return function (v) { return step([n, v]); }; } function step(op) { if (f) throw new TypeError("Generator is already executing."); while (_) try { if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t; if (y = 0, t) op = [op[0] & 2, t.value]; switch (op[0]) { case 0: case 1: t = op; break; case 4: _.label++; return { value: op[1], done: false }; case 5: _.label++; y = op[1]; op = [0]; continue; case 7: op = _.ops.pop(); _.trys.pop(); continue; default: if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; } if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; } if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; } if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; } if (t[2]) _.ops.pop(); _.trys.pop(); continue; } op = body.call(thisArg, _); } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; } if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true }; } }; Object.defineProperty(exports, "__esModule", { value: true }); exports.Pagination = void 0; var Pagination = /** @class */ (function () { function Pagination(_a) { var Model = _a.Model, searchKeyword = _a.searchKeyword, _b = _a.offset, offset = _b === void 0 ? 0 : _b, _c = _a.limit, limit = _c === void 0 ? 20 : _c, _d = _a.sort, sort = _d === void 0 ? { created_at: -1 } : _d, _e = _a.populates, populates = _e === void 0 ? [] : _e, _f = _a.query, query = _f === void 0 ? {} : _f, _g = _a.filteredQuery, filteredQuery = _g === void 0 ? {} : _g, _h = _a.options, options = _h === void 0 ? {} : _h; this.Model = Model; this.offset = offset; this.limit = limit; this.sort = sort; this.populates = populates; this.options = options; this.searchKeyword = searchKeyword; this.query = this.cleanQuery(query); this.filteredQuery = this.cleanQuery(filteredQuery); this.data = null; this.meta = null; this.total = 0; this.totalPages = 0; this.currentPage = 0; } Pagination.prototype.run = function () { return __awaiter(this, void 0, void 0, function () { var query, $text, result, index, _a, total; return __generator(this, function (_b) { switch (_b.label) { case 0: query = this.filteredQuery; if (this.searchKeyword) { $text = { $search: this.searchKeyword }; query.$text = $text; } result = this.Model.find(query) .sort(this.sort) .skip(this.offset) .limit(this.limit); for (index in this.populates) { result.populate(this.populates[index]); } _a = this; return [4 /*yield*/, result.exec()]; case 1: _a.data = _b.sent(); return [4 /*yield*/, this.Model.countDocuments(query).exec()]; case 2: total = _b.sent(); this.total = total; this.meta = { offset: this.offset, limit: this.limit, total: total }; this.totalPages = Math.ceil(this.meta.total / this.limit); this.currentPage = Math.floor((this.offset / this.limit) + 1); return [2 /*return*/, this]; } }); }); }; Pagination.prototype.getData = function () { return this.data; }; Pagination.prototype.getMeta = function () { return { currentItemCount: this.data ? this.data['length'] : [], itemsPerPage: this.limit, startIndex: this.offset, totalItems: this.total, pageIndex: this.currentPage, totalPages: this.totalPages }; }; Pagination.prototype.links = function () { var pages = []; var currentPage = this.currentPage; var previousPage = this.currentPage - 1; var nextPage = this.currentPage + 1; if (this.totalPages <= 7) { for (var page = 1; page <= this.totalPages; page++) { pages.push(this.createPageInfo(page)); } } else { var linkCount = 7; var min = 2; var max = linkCount - 1; var medianPadding = 1; if (currentPage - medianPadding > min && currentPage + medianPadding < this.totalPages - 1) { pages.push(this.createPageInfo(1)); pages.push(this.createPageInfo(null)); for (var page = currentPage - medianPadding; page <= currentPage + medianPadding; page++) { pages.push(this.createPageInfo(page)); } pages.push(this.createPageInfo(null)); pages.push(this.createPageInfo(this.totalPages)); } else if (currentPage - medianPadding <= min) { for (var page = 1; page < max; page++) { pages.push(this.createPageInfo(page)); } pages.push(this.createPageInfo(null)); pages.push(this.createPageInfo(this.totalPages)); } else if (currentPage + medianPadding >= max) { for (var page = this.totalPages; page > this.totalPages - (linkCount - 2); page--) { pages.unshift(this.createPageInfo(page)); } pages.unshift(this.createPageInfo(null)); pages.unshift(this.createPageInfo(1)); } } return { current: this.createPageInfo(currentPage), prev: this.createPageInfo(previousPage), next: this.createPageInfo(nextPage), pages: pages }; }; Pagination.prototype.createPageInfo = function (page) { if (!page || page < 1 || page > this.totalPages) return { page: null }; return { page: page, current: page.toString() === this.currentPage.toString(), queryString: this.buildQueryString(page) }; }; Pagination.prototype.buildQueryString = function (page) { var offset = (page - 1) * this.limit; var queryString = ''; var query = __assign(__assign(__assign({}, this.meta), this.query), { offset: offset }); delete query.deleted_at; delete query.total; for (var key in query) { if (!queryString) queryString = '?'; queryString += key + "=" + query[key] + "&"; } if (queryString[queryString.length - 1] === '&') queryString = queryString.substring(0, queryString.length - 1); return queryString; }; Pagination.prototype.cleanQuery = function (query) { var filteredQuery = Object.assign({ deleted_at: null }, query); for (var key in filteredQuery) { if (filteredQuery[key] === '') delete filteredQuery[key]; if (filteredQuery[key] === 'null') filteredQuery[key] = null; } if (this.options.withDeleted) delete filteredQuery.deleted_at; delete filteredQuery.offset; delete filteredQuery.limit; return filteredQuery; }; return Pagination; }()); exports.Pagination = Pagination;