UNPKG

@ebenos/viber-elements

Version:

Elements Library for the Ebony framework.

116 lines 4.24 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.Carousel = void 0; const attachments_1 = require("./attachments"); function createCarousel(elements, options) { const color = (k, t) => { const defaultStyle = { title: { textColor: '#ffffff', backgroundColor: '#ffffff' }, button: { textColor: '#ffffff', backgroundColor: '#2db9b9' }, subtitle: { backgroundColor: '#ffffff', textColor: '#696969' }, backgroundColor: '#ffffff', imageBackgroundColor: '#ffffff' }; if (k === 'imageBackgroundColor' || k === 'backgroundColor') { return (options === null || options === void 0 ? void 0 : options.style) ? options.style[k] : defaultStyle[k]; } if (t === undefined) { throw new Error('Need to identify subproperty'); } const element = (options === null || options === void 0 ? void 0 : options.style) ? options.style[k] : defaultStyle[k]; if (element === undefined) { throw new Error('Non-existant element'); } const style = element[t]; return style ? style : defaultStyle[k][t]; }; const frame = (options === null || options === void 0 ? void 0 : options.frame) ? options.frame : undefined; let hasSubtitle = false, hasTitle = false, hasImage = false, largerButtons = 0; for (const e of elements) { if (e.subtitle !== undefined) { hasSubtitle = true; } if (e.title !== undefined) { hasTitle = true; } if (e.image !== undefined) { hasImage = true; } if (largerButtons < e.buttons.length) { largerButtons = e.buttons.length; } } const maxRows = largerButtons + (hasSubtitle ? 1 : 0) + (hasTitle ? 1 : 0) + (hasImage ? 3 : 0); const buttonsArray = elements.map((e) => [ ...(hasImage ? [ new attachments_1.Button({ Rows: 3, BgColor: color('imageBackgroundColor'), Columns: 6, Image: e.image, ActionType: 'none', ActionBody: '' }) ] : []), ...(hasTitle ? [ new attachments_1.Button({ Rows: 1, Columns: 6, BgColor: color('title', 'backgroundColor'), Text: `<b><font color="${color('title', 'textColor')}">${e.title}</font></b>`, ActionType: 'none', ActionBody: '' }) ] : []), ...(hasSubtitle ? [ new attachments_1.Button({ Rows: 1, Columns: 6, BgColor: color('subtitle', 'backgroundColor'), Text: `<font color="${color('subtitle', 'textColor')}">${e.subtitle ? e.subtitle : ''}</font>`, ActionType: 'none', TextSize: 'small', TextHAlign: 'left', ActionBody: '' }) ] : []), ...e.buttons.map((button) => new attachments_1.Button({ Rows: 1, Columns: 6, Text: `<font color="${color('button', 'textColor')}"><b>${button.text}</b></font>`, BgColor: color('button', 'backgroundColor'), ActionType: 'url' in button ? 'open-url' : 'reply', Silent: 'url' in button, ActionBody: 'url' in button ? button.url : button.payload, Frame: frame })) ]); return { ButtonsGroupColumns: 6, ButtonsGroupRows: maxRows, BgColor: color('backgroundColor'), Buttons: buttonsArray.flat() }; } class Carousel extends attachments_1.RichMedia { constructor(elements, options) { super(createCarousel(elements, options)); } } exports.Carousel = Carousel; //# sourceMappingURL=carousel.js.map