UNPKG

@botonic/plugin-contentful

Version:

## What Does This Plugin Do?

93 lines 4.38 kB
import { __awaiter } from "tslib"; import * as cms from '../../cms'; import { Button, CmsException, ContentType } from '../../cms'; import { TopContentType } from '../../cms/cms'; import { CONTENT_FIELDS } from '../../manage-cms/fields'; import { isOfType } from '../../util/enums'; import { ContentDelivery } from '../content-delivery'; import { ContentfulEntryUtils, } from '../delivery-utils'; import { getTargetCallback } from './callback-delivery'; export class ButtonDelivery extends ContentDelivery { constructor(delivery, resumeErrors) { super(cms.ContentType.BUTTON, delivery, resumeErrors); } button(id, context) { return __awaiter(this, void 0, void 0, function* () { const entry = yield this.getEntry(id, context); return this.fromEntry(entry, context); }); } fromReferenceSkipErrors(entries, context) { return __awaiter(this, void 0, void 0, function* () { return yield this.asyncMap(context, entries, entry => this.fromReference(entry, context)); }); } fromReference(entry, context) { return __awaiter(this, void 0, void 0, function* () { // we could pass the entry to fromId to avoid fetching it again, but it makes // the code more complex when the reference is a button try { return yield this.fromId(entry.sys.id, context); } catch (e) { throw new CmsException(`Error loading button with id '${entry.sys.id}'`, e); } }); } fromId(id, context) { return __awaiter(this, void 0, void 0, function* () { const entry = yield this.delivery.getEntry(id, context); const entryType = ContentfulEntryUtils.getContentModel(entry); if (isOfType(entryType, TopContentType)) { return this.fromContentReference(entry, context); } if (entryType === ButtonDelivery.BUTTON_CONTENT_TYPE) { const buttonEntry = entry; return this.fromEntry(buttonEntry, context); } throw new Error(`Unexpected type ${entryType}`); }); } fromEntry(buttonEntry, context) { var _a; if (!buttonEntry.fields.target) { throw new CmsException(`Button ${this.entryId(buttonEntry)} has no target`); } // target may be empty if we got it from a reference (delivery does not provide infinite recursive references) const callback = getTargetCallback(buttonEntry.fields.target, context); const newButton = new cms.Button(buttonEntry.sys.id, buttonEntry.fields.name, (_a = buttonEntry.fields.text) !== null && _a !== void 0 ? _a : '', callback); return this.addCustomFields(newButton, buttonEntry.fields); } // TODO move to a new CmsUtils.buttonToCallback(cms.ContentCallback)? fromContentReference(entry, _context) { const fields = entry.fields; const text = fields.shortText || ''; const newButton = new Button(entry.sys.id, fields.name, text, ButtonDelivery.callbackFromEntry(entry)); return this.addCustomFields(newButton, fields, true); } addCustomFields(button, entryFields, buttonIsMessageContent = false) { const buttonAttributes = Object.keys(button); const knownFields = ['target']; //if the button is created from a content (text, image, etc) we have to avoid adding all their fields as custom fields if (buttonIsMessageContent) { CONTENT_FIELDS.forEach((field) => { knownFields.push(field.cmsName); }); } const customKeys = Object.keys(entryFields).filter((field) => !buttonAttributes.includes(field) && !knownFields.includes(field)); button.customFields = {}; for (const customKey of customKeys) { button.customFields[customKey] = entryFields[customKey]; } return button; } static callbackFromEntry(entry) { const modelType = ContentfulEntryUtils.getContentModel(entry); if (modelType === ContentType.URL) { return cms.Callback.ofUrl(entry.fields.url || ''); } return new cms.ContentCallback(modelType, entry.sys.id); } } ButtonDelivery.BUTTON_CONTENT_TYPE = 'button'; //# sourceMappingURL=button.js.map