UNPKG

fox-block-builder

Version:

Maintainable code for loop slack-block-kit-like modal builder

91 lines 4.19 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.FormBuilder = exports.MenuBuilder = exports.PageBuilder = exports.PluginModalBuilder = void 0; const internal_1 = require("../../internal"); const enum_1 = require("../../internal/enum"); const _1 = require("."); class PluginModalBuilder { constructor({ view, type, }) { var _a; this.view = view.buildToObject(); this.type = type; /** Фильтруем пустые блоки */ this.view.blocks = (_a = this.view.blocks) === null || _a === void 0 ? void 0 : _a.filter((b) => !!b); if (!this.view.close) { this.view.close = { text: 'Назад', type: internal_1.ObjectType.Text }; } if (!this.view.submit) { this.view.close = { text: 'Отправить', type: internal_1.ObjectType.Text }; } } } exports.PluginModalBuilder = PluginModalBuilder; class PageBuilder extends PluginModalBuilder { constructor({ view }) { super({ view, type: enum_1.LoopPluginModalType.Page }); } } exports.PageBuilder = PageBuilder; class MenuBuilder extends PluginModalBuilder { constructor({ view }) { super({ view, type: enum_1.LoopPluginModalType.Menu }); } } exports.MenuBuilder = MenuBuilder; /** Конструктор формы */ class FormBuilder extends PluginModalBuilder { constructor({ view, callbackId, meta, map }) { var _a, _b, _c, _d; /** Задаем callback_id и метаданные модалки */ const formView = view .callbackId(callbackId) .privateMetaData(JSON.stringify(meta !== null && meta !== void 0 ? meta : '{}')); /** Отправляем их в базовый конструктор */ super({ view: formView, type: enum_1.LoopPluginModalType.Form }); this.type = enum_1.LoopPluginModalType.Form; /** Ставим тип модалки */ this.type = enum_1.LoopPluginModalType.Form; /** Если есть видимость внутри блоков - пока переводим в мапу */ if (this.view.blocks.some((b) => !!b.visibleIf)) { /** Создаем мапу если нету */ if (!map) map = (0, _1.PluginModalMap)(); /** Обходим блоки */ for (const visibleIfBlock of this.view.blocks.filter((b) => !!b.visibleIf)) { /** AND / обратно совместимый простой вариант */ if ((_a = visibleIfBlock.visibleIf) === null || _a === void 0 ? void 0 : _a.AND) { for (const visibleIfBlockAndRef of (_b = visibleIfBlock.visibleIf) === null || _b === void 0 ? void 0 : _b.AND) { map.addValues([ visibleIfBlock.block_id, [visibleIfBlockAndRef.block_id, visibleIfBlockAndRef.values], ]); } } /** Любые значения */ if ((_c = visibleIfBlock.visibleIf) === null || _c === void 0 ? void 0 : _c.OR) { for (const visibleIfBlockAndRef of (_d = visibleIfBlock.visibleIf) === null || _d === void 0 ? void 0 : _d.OR) { map.addValues([ visibleIfBlock.block_id, [ visibleIfBlockAndRef.block_id, visibleIfBlockAndRef.values.map((v) => `*${v}`), ], ]); } } } } /** Задаем мапу если есть */ this.map = map === null || map === void 0 ? void 0 : map.values; } addMeta(meta) { var _a; this.view.private_metadata = JSON.stringify({ ...JSON.parse((_a = this.view.private_metadata) !== null && _a !== void 0 ? _a : '{}'), ...meta, }); return this; } } exports.FormBuilder = FormBuilder; //# sourceMappingURL=plugin-modal.builder.js.map