banjo-kentico-cloud-delivery
Version:
Fork of Official Kentico Cloud Delivery SDK
196 lines • 8.36 kB
JavaScript
;
exports.__esModule = true;
var strongly_type_resolver_1 = require("./strongly-type.resolver");
var RichTextResolver = /** @class */ (function () {
function RichTextResolver() {
/**
* Type identifying nested modular content in Rich text fields
*/
this.modularContentobjectType = 'application/kenticocloud';
/**
* This tag wil be used instead of 'object'
*/
this.modularContentTagWrapper = 'div';
/**
* Attribute used to identify modular item based on its codename
*/
this.modularContentCodenameAttributeName = 'data-codename';
/**
* Tag identifying links
*/
this.linkTag = 'a';
/**
* Attributes that identifies if of the content item referenced in links
*/
this.linkContentItemIdAttributeName = 'data-item-id';
}
/**
* Resolves modular content inside the Rich text field.
* Rich text resolved needs to be configured either on the model or query level
*/
RichTextResolver.prototype.resolveHtml = function (html, data) {
var _this = this;
// prepare config
var config = {
enableAdvancedLogging: data.enableAdvancedLogging,
queryConfig: data.queryConfig,
modularContentWrapperTag: data.modularContentWrapperTag,
modularContentWrapperClasses: data.modularContentWrapperClasses
};
var result = data.richTextHtmlParser.resolveRichTextField(html, {
getLinkUrl: function (itemId) { return _this.getLinkUrl({
config: config,
links: data.links,
itemId: itemId,
typeResolvers: data.typeResolvers
}); },
getModularContentHtml: function (itemCodename) { return _this.getHtmlOfModularContent({
itemCodename: itemCodename,
config: config,
modularItems: data.modularItems
}); }
}, {
enableAdvancedLogging: data.enableAdvancedLogging,
queryConfig: data.queryConfig,
modularContentWrapperTag: data.modularContentWrapperTag,
modularContentWrapperClasses: data.modularContentWrapperClasses
});
return result.resolvedHtml;
};
/**
* Resolves modular content inside the Rich text field.
* Rich text resolved needs to be configured either on the model or query level
*/
RichTextResolver.prototype.resolveModules = function (html, data) {
// prepare config
// const config: IHtmlResolverConfig = {
// enableAdvancedLogging: data.enableAdvancedLogging,
// queryConfig: data.queryConfig,
// modularContentWrapperTag: data.modularContentWrapperTag,
// modularContentWrapperClasses: data.modularContentWrapperClasses
// };
return data.modularItems;
// todo: here we need to find the resolved modules
// console.log('kc module modularItems' + data.modularItems);
// const outdata: {module: string, model: any}[] = [];
// data.modularItems.forEach((item: ContentItem) => {
// outdata.push({module: item.system.type, model: item });
// });
// return outdata;
// const result = data.richTextHtmlParser.resolveRichTextField(
// html, {
// getLinkUrl: (itemId: string) => this.getLinkUrl({
// config: config,
// links: data.links,
// itemId: itemId,
// typeResolvers: data.typeResolvers
// }),
// getModularContentHtml: (itemCodename: string) => this.getHtmlOfModularContent({
// itemCodename: itemCodename,
// config: config,
// modularItems: data.modularItems,
// })
// }, {
// enableAdvancedLogging: data.enableAdvancedLogging,
// queryConfig: data.queryConfig,
// modularContentWrapperTag: data.modularContentWrapperTag,
// modularContentWrapperClasses: data.modularContentWrapperClasses
// });
// return result.resolvedHtml;
// /{module: string, model: any}[]
// return [ {
// module: 'rich_html',
// model: '<p>One <strong>Blah</strong>!</p>'
// },
// {
// module: 'inline_article_quote',
// model: { blah: 'yus' }
// },
// {
// module: 'rich_html',
// model: '<p>Two <h3>fdsfsdf</h3></p>'
// },
// {
// module: 'inline_article_quote',
// model: { blah: 'no' }
// },
// {
// module: 'inline_external_link',
// model: { url: 'sdfsdfsdfds' }
// }];
};
RichTextResolver.prototype.getHtmlOfModularContent = function (data) {
// get modular content item
var modularContentItem = data.modularItems.find(function (m) { return m.system.codename === data.itemCodename; });
// check if modular content really exists
if (!modularContentItem) {
throw Error("Cannot resolve modular content in 'RichTextField' for '" + data.itemCodename + "' content item");
}
// create new replacement object
// get html to replace object using Rich text resolver function
var resolver = undefined;
if (data.config.queryConfig.richTextResolver) {
// use resolved defined by query if available
resolver = data.config.queryConfig.richTextResolver;
}
else {
// use default resolver defined in models
if (modularContentItem.richTextResolver) {
resolver = modularContentItem.richTextResolver;
}
}
// check resolver
if (!resolver) {
if (data.config.enableAdvancedLogging) {
console.warn("Cannot resolve modular content of '" + modularContentItem.system.type + "' type in 'RichTextField' because no rich text resolved was configured");
return '';
}
return '';
}
return resolver(modularContentItem);
};
RichTextResolver.prototype.getLinkUrl = function (data) {
// find link with the id of content item
var link = data.links.find(function (m) { return m.itemId === data.itemId; });
if (!link) {
if (data.config.enableAdvancedLogging) {
console.warn("Cannot resolve URL for item '" + data.itemId + "' because no link with this id was found");
}
return '';
}
// try to resolve link using the resolver passed through the query config
var queryLinkResolver = data.config.queryConfig.linkResolver;
var url;
if (queryLinkResolver) {
// try to resolve url using the query config
url = queryLinkResolver(link);
}
if (!url) {
// url was not resolved, try to find global resolver for this particular type
var emptyTypedItem = strongly_type_resolver_1.stronglyTypedResolver.createEmptyTypedObj(link.type, data.typeResolvers);
if (!emptyTypedItem) {
if (data.config.enableAdvancedLogging) {
console.warn("Cannot resolve link for link of '" + link.type + "' type with id '" + link.itemId + "' and url slug '" + link.urlSlug + "'");
}
}
else {
var globalLinkResolver = emptyTypedItem.linkResolver;
if (globalLinkResolver) {
url = globalLinkResolver(link);
}
}
}
// url still wasn't resolved
if (!url) {
if (data.config.enableAdvancedLogging) {
console.warn("Url for content type '" + link.type + "' with id '" + link.itemId + "' resolved to null/undefined");
}
return '';
}
return url;
};
return RichTextResolver;
}());
exports.RichTextResolver = RichTextResolver;
exports.richTextResolver = new RichTextResolver();
//# sourceMappingURL=rich-text.resolver.js.map