UNPKG

@ayanaware/bentocord

Version:

Bentocord is a Bento plugin designed to rapidly build fully functional Discord Bots.

78 lines 2.4 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.Paginator = void 0; class Paginator { constructor(ctx, items, options) { this.currentPage = 0; this.ctx = ctx; this.items = items; this.options = { itemsPerPage: 10, ...options }; // update currentPage if focused was provided if (typeof this.options.focused === 'number') { this.currentPage = Math.floor(options.focused / this.options.itemsPerPage) ?? 0; } } get page() { if (this.currentPage > this.pageCount - 1) this.currentPage = this.pageCount - 1; if (this.currentPage < 0) this.currentPage = 0; return this.currentPage; } set page(page) { // constrain if (page < 0) page = 0; if (page > this.pageCount - 1) page = this.pageCount - 1; this.currentPage = page; } get pageCount() { let itemCount = 0; if (Array.isArray(this.items)) itemCount = this.items.length; else itemCount = this.itemCount; if (itemCount === 0) return 0; return Math.ceil(itemCount / this.options.itemsPerPage) || 1; } get hasNext() { return this.page < this.pageCount - 1; } get hasPrev() { return this.page > 0; } /** * Get item at given index * @param index Index * @returns PaginatorPageItem */ async getItem(index) { let items = this.items; if (typeof items === 'function') items = await items(); this.itemCount = items.length; const item = items[index] ?? null; return { item, index }; } async getItems(page) { const num = page || this.currentPage; const start = num * this.options.itemsPerPage; const end = start + this.options.itemsPerPage; let items = this.items; if (typeof items === 'function') items = await items(); this.itemCount = items.length; const out = []; for (let index = start; index < end; index++) { const item = items[index] ?? null; if (!item) break; out.push({ item, index }); } return out; } } exports.Paginator = Paginator; //# sourceMappingURL=Paginator.js.map