slack-block-builder
Version:
Maintainable code for interactive Slack messages, modals, home tabs, and workflow steps. A must-have for the Slack Block Kit framework.
96 lines (95 loc) • 5.72 kB
JavaScript
"use strict";
/* eslint-disable max-len */
Object.defineProperty(exports, "__esModule", { value: true });
exports.Components = exports.Accordion = exports.EasyPaginator = exports.Paginator = void 0;
const paginator_ui_component_1 = require("./paginator-ui-component");
const accordion_ui_component_1 = require("./accordion-ui-component");
const internal_1 = require("../internal");
/**
* @param {Object} [params] Parameters passed to the constructor.
* @param {string} [params.items] An array of items to be displayed in the paginated content.
* @param {int} [params.page] The number of the page to display.
* @param {int} [params.perPage] The number of items to display on a page.
* @param {int} [params.totalItems] The total number of items in the data set across all pages.
* @param {PaginatorActionIdFn} [params.actionId] A function that receives pagination data and returns a string to set as the action IDs of the navigation buttons.
* @param {PaginatorBuilderFn} [params.blocksForEach] A function that receives an object with a single item and returns the blocks to create for that item.
* @param {string} [params.nextButtonText] The text to display on the button that moves forward in the pagination.
* @param {string} [params.previousButtonText] The text to display on the button that moves backward in the pagination.
* @param {PaginatorPageCountTextFn} [params.pageCountText] A function to create a custom page count in the UI.
*
* {@link https://www.blockbuilder.dev/#/components/paginator|View in Block Builder Documentation}
*/
function Paginator(params) {
const { page, perPage, totalItems } = params;
const stateManager = new internal_1.PaginatorStateManager({ page, perPage, totalItems });
return new paginator_ui_component_1.PaginatorUIComponent({
items: params.items,
paginator: stateManager,
nextButtonText: params.nextButtonText || null,
previousButtonText: params.previousButtonText || null,
pageCountTextFunction: params.pageCountText || null,
actionIdFunction: params.actionId,
builderFunction: params.blocksForEach,
});
}
exports.Paginator = Paginator;
/**
* @param {Object} [params] Parameters passed to the constructor.
* @param {string} [params.items] An array of items to be displayed in the paginated content.
* @param {int} [params.page] The number of the page to display.
* @param {int} [params.perPage] The number of items to display on a page.
* @param {PaginatorActionIdFn} [params.actionId] A function that receives pagination data and returns a string to set as the action IDs of the navigation buttons.
* @param {PaginatorBuilderFn} [params.blocksForEach] A function that receives an object with a single item and returns the blocks to create for that item.
* @param {string} [params.nextButtonText] The text to display on the button that moves forward in the pagination.
* @param {string} [params.previousButtonText] The text to display on the button that moves backward in the pagination.
* @param {PaginatorPageCountTextFn} [params.pageCountText] A function to create a custom page count in the UI.
*
* {@link https://www.blockbuilder.dev/#/components/easy-paginator|View in Block Builder Documentation}
*/
function EasyPaginator(params) {
const { page, perPage, items } = params;
const totalItems = items.length;
const paginationCalculator = new internal_1.PaginatorStateManager({ page, perPage, totalItems });
const extractedItems = paginationCalculator.extractItems(items);
return new paginator_ui_component_1.PaginatorUIComponent({
paginator: paginationCalculator,
items: extractedItems,
nextButtonText: params.nextButtonText || null,
previousButtonText: params.previousButtonText || null,
pageCountTextFunction: params.pageCountText || null,
actionIdFunction: params.actionId,
builderFunction: params.blocksForEach,
});
}
exports.EasyPaginator = EasyPaginator;
/**
* @param {Object} [params] Parameters passed to the constructor.
* @param {string} [params.items] An array of items to be displayed in the expandable/collapsable content.
* @param {AccordionTitleTextFn} [params.titleText] A function that receives an object with a single item and returns a string to be displayed next to the expand/collapse button.
* @param {AccordionActionIdFn} [params.actionId] A function that receives the accordion state data and returns a string to set as the action IDs of the expand/collapse buttons.
* @param {AccordionBuilderFn} [params.blocksForExpanded] A function that receives an object with a single item and returns the blocks to create for that item.
* @param {string} [params.expandButtonText] The text to display on the button that expands an item in the UI.
* @param {string} [params.collapseButtonText] The text to display on the button that collapses an item in the UI.
*
* {@link https://www.blockbuilder.dev/#/components/accordion|View in Block Builder Documentation}
*/
function Accordion(params) {
const { items, expandedItems, collapseOnExpand } = params;
const stateManager = new internal_1.AccordionStateManager({ expandedItems, collapseOnExpand });
return new accordion_ui_component_1.AccordionUIComponent({
items,
paginator: stateManager,
expandButtonText: params.expandButtonText || null,
collapseButtonText: params.collapseButtonText || null,
titleTextFunction: params.titleText,
actionIdFunction: params.actionId,
builderFunction: params.blocksForExpanded,
});
}
exports.Accordion = Accordion;
const components = {
Paginator,
EasyPaginator,
Accordion,
};
exports.Components = components;