UNPKG

@botonic/plugin-contentful

Version:

## What Does This Plugin Do?

97 lines 4.67 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.ButtonDelivery = void 0; const tslib_1 = require("tslib"); const cms = tslib_1.__importStar(require("../../cms")); const cms_1 = require("../../cms"); const cms_2 = require("../../cms/cms"); const fields_1 = require("../../manage-cms/fields"); const enums_1 = require("../../util/enums"); const content_delivery_1 = require("../content-delivery"); const delivery_utils_1 = require("../delivery-utils"); const callback_delivery_1 = require("./callback-delivery"); class ButtonDelivery extends content_delivery_1.ContentDelivery { constructor(delivery, resumeErrors) { super(cms.ContentType.BUTTON, delivery, resumeErrors); } button(id, context) { return tslib_1.__awaiter(this, void 0, void 0, function* () { const entry = yield this.getEntry(id, context); return this.fromEntry(entry, context); }); } fromReferenceSkipErrors(entries, context) { return tslib_1.__awaiter(this, void 0, void 0, function* () { return yield this.asyncMap(context, entries, entry => this.fromReference(entry, context)); }); } fromReference(entry, context) { return tslib_1.__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 cms_1.CmsException(`Error loading button with id '${entry.sys.id}'`, e); } }); } fromId(id, context) { return tslib_1.__awaiter(this, void 0, void 0, function* () { const entry = yield this.delivery.getEntry(id, context); const entryType = delivery_utils_1.ContentfulEntryUtils.getContentModel(entry); if ((0, enums_1.isOfType)(entryType, cms_2.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 cms_1.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 = (0, callback_delivery_1.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 cms_1.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) { fields_1.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 = delivery_utils_1.ContentfulEntryUtils.getContentModel(entry); if (modelType === cms_1.ContentType.URL) { return cms.Callback.ofUrl(entry.fields.url || ''); } return new cms.ContentCallback(modelType, entry.sys.id); } } exports.ButtonDelivery = ButtonDelivery; ButtonDelivery.BUTTON_CONTENT_TYPE = 'button'; //# sourceMappingURL=button.js.map