UNPKG

@necord/pagination

Version:

A lightweight Pagination module for Necord

118 lines (117 loc) 4.4 kB
"use strict"; var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) { var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d; if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc); else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r; return c > 3 && r && Object.defineProperty(target, key, r), r; }; var __metadata = (this && this.__metadata) || function (k, v) { if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v); }; var __param = (this && this.__param) || function (paramIndex, decorator) { return function (target, key) { decorator(target, key, paramIndex); } }; var NecordPaginationService_1; Object.defineProperty(exports, "__esModule", { value: true }); exports.NecordPaginationService = void 0; const common_1 = require("@nestjs/common"); const helpers_1 = require("./helpers"); const necord_pagination_module_definition_1 = require("./necord-pagination.module-definition"); const enums_1 = require("./enums"); const discord_js_1 = require("discord.js"); const exceptions_1 = require("./exceptions"); let NecordPaginationService = NecordPaginationService_1 = class NecordPaginationService { constructor(options) { var _a; this.options = options; this.options = this.deepMerge(NecordPaginationService_1.DEFAULT_OPTIONS, options !== null && options !== void 0 ? options : {}); this.cache = new discord_js_1.LimitedCollection({ maxSize: (_a = this.options.cache) === null || _a === void 0 ? void 0 : _a.maxSize }); } /** * Register a new pagination builder * @param factory */ register(factory) { const builder = factory(new helpers_1.PaginationBuilder(this.options)); this.cache.set(builder.customId, builder); return builder; } /** * Alias for register method * @param factory */ create(factory) { return this.register(factory); } get(customId) { const builder = this.cache.get(customId); if (!builder) { throw new exceptions_1.PaginationNotFoundException(); } return builder; } delete(customId) { return this.cache.delete(customId); } copy(customId) { const builder = this.get(customId); const copy = builder.copy(); this.cache.set(copy.customId, copy); return copy; } clear() { this.cache.clear(); } deepMerge(target, source) { for (const key in source) { if (source[key] instanceof Object) { Object.assign(source[key], this.deepMerge(target[key], source[key])); } } Object.assign(target || {}, source); return target; } }; exports.NecordPaginationService = NecordPaginationService; NecordPaginationService.DEFAULT_OPTIONS = { allowTraversal: false, allowSkip: false, buttonsPosition: 'end', buttons: { [enums_1.PaginationAction.First]: { label: 'First', emoji: '⏮️', style: discord_js_1.ButtonStyle.Primary }, [enums_1.PaginationAction.Back]: { label: 'Previous', emoji: '⏪', style: discord_js_1.ButtonStyle.Primary }, [enums_1.PaginationAction.Next]: { label: 'Next', emoji: '⏩', style: discord_js_1.ButtonStyle.Primary }, [enums_1.PaginationAction.Last]: { label: 'Last', emoji: '⏭️', style: discord_js_1.ButtonStyle.Primary }, [enums_1.PaginationAction.Traverse]: { label: 'Traverse', emoji: '🔢', style: discord_js_1.ButtonStyle.Primary } }, cache: { maxSize: Infinity } }; exports.NecordPaginationService = NecordPaginationService = NecordPaginationService_1 = __decorate([ (0, common_1.Injectable)(), __param(0, (0, common_1.Inject)(necord_pagination_module_definition_1.MODULE_OPTIONS_TOKEN)), __metadata("design:paramtypes", [Object]) ], NecordPaginationService);