{"version":3,"file":"render.mjs","sources":["src/common.mjs","src/render.mjs"],"sourcesContent":["// Common Utilities Class\nexport default class {\n\tconstructor() { }\n\n\t/**\n\t * A method to fetch the most recent articles and return them as an object that can be passed into our model.\n\t * @param {*} context \n\t * @returns an object mapping an area of articles to the news key value.\n\t */\n\tstatic async getArticles(context) {\n\t\tconst contentClient = context.contentClient\n\t\t// Get the three most recent articles, and the fields needed to render them or link to their detail page.\n\t\tlet resArticles = await contentClient.queryItems({\n\t\t\t'q': '(type eq \"BRK_Article\")',\n\t\t\t'fields': 'slug,fields.headline,fields.publish_date,fields.cta_text',\n\t\t\t'limit': 3,\n\t\t\t'orderBy': 'fields.publish_date:desc',\n\t\t})\n\t\tlet arModel = resArticles.items\n\n\t\tlet objDateOptions = {\n\t\t\tmonth: 'long',\n\t\t\tday: 'numeric',\n\t\t\tyear: 'numeric',\n\t\t\ttimeZone: 'UTC'\n\t\t}\n\n\t\t// Fetch detail page ID\n\t\tlet detailPageId = Number(SCSRenderAPI.getCustomSiteProperty(\"newsDetailPageId\"))\n\t\t// Check if the page id was a number.\n\t\t// If it's not a number, and we're in site builder, send an alert\n\t\tif (isNaN(detailPageId)\n\t\t\t&& (\n\t\t\t\tSCSRenderAPI.renderMode == SCSRenderAPI.RENDER_MODE_EDIT\n\t\t\t\t|| SCSRenderAPI.renderMode == SCSRenderAPI.RENDER_MODE_PREVIEW\n\t\t\t)\n\t\t) {\n\t\t\talert('Detail Page is invalid. Please set the detail page id in the site properties.')\n\t\t}\n\n\t\t//Reformat the date field to be 'Month day, year'\n\t\t//Add the link to the detail page to each article object\n\t\tarModel.forEach((item) => {\n\t\t\t//item.fields.publish_date = new Date(`${item.fields.publish_date.value.split('Z')[0]}-07:00`).toLocaleDateString('en-us', objDateOptions);\n\n\t\t\titem.fields.publish_date = new Date(item.fields.publish_date.value).toLocaleDateString('en-us', objDateOptions)\n\t\t\titem.fields.detail_url = SCSRenderAPI.getPageLinkData(detailPageId, { contentId: item.id, contentType: item.type, contentSlug: item.slug }).href\n\t\t})\n\n\t\treturn { news: arModel }\n\t}\n\n\t//Render the articles into the preloaded template\n\tstatic async addArticleList(context, container) {\n\t\tlet objArticles = await this.getArticles({\n\t\t\tcontentClient: context.contentClient\n\t\t})\n\t\tlet elArticleList = container.querySelector('.article-list')\n\t\tlet elTemplate = await SCSRenderAPI.importText(context.assetsPath + '/articlelist.html')\n\n\t\ttry {\n\t\t\telArticleList.innerHTML = context.Mustache.render(elTemplate, objArticles)\n\t\t} catch (e) {\n\t\t\tconsole.log('Failed to expand Mustache template.', e)\n\t\t}\n\t}\n\n\n\tstatic async createHTML(context, args) {\n\t\t// extract all the required dependencies from the context\n\t\tconst Mustache = context.Mustache\n\t\tconst customSettingsData = context.customSettingsData\n\t\tconst template = context.template\n\t\tconst contentClient = context.contentClient\n\t\tconst api = context.api\n\n\t\t// extract the original values (or apply default values)\n\t\tconst customData = (args && args.customSettingsData) || customSettingsData || {}\n\n\t\tcustomData.nls = customData.nls || {};\n\n\t\tlet model = {\n\t\t\thref: api.getPageLinkData(Number(api.getCustomSiteProperty(\"newsListingPageId\"))).href,\n\t\t\tcta: customData.nls.cta || 'More News',\n\t\t\ttitle: customData.nls.title || 'News',\n\t\t}\n\t\t// render the component\n\t\ttry {\n\t\t\treturn Mustache.render(template, model)\n\t\t} catch (e) {\n\t\t\tconsole.log('Failed to expand Mustache template.', e)\n\t\t\treturn ''\n\t\t}\n\t}\n}","import CommonUtils from './common.mjs';\n\n// The Custom Component class will be the \"default\" export from the module\nexport default class {\n\n\tconstructor(args) {\n\t\t// store the args\n\t\tthis.mode = args.viewMode;\n\t\tthis.id = args.id;\n\t\t// get the OCM environment resources \n\t\tthis.sitesSDK = args.SitesSDK;\n\t\tthis.Mustache = SCSRenderAPI.getMustache();\n\n\t\tthis.contentClient = this.sitesSDK.getProperty('contentClient')\n\n\t\t// store the path to the /assets folder\n\t\tthis.assetsPath = import.meta.url.replace('/render.mjs', '');\n\t}\n\n\t/**\n\t * Adds event listeners to re-render the component when settings have been changed.\n\t */\n\taddEventListeners() {\n\t\t// listen for settings update\n\t\tthis.sitesSDK.subscribe(this.sitesSDK.MESSAGE_TYPES.SETTINGS_UPDATED, (props) => {\n\t\t\tif (props.property === 'customSettingsData') {\n\t\t\t\tthis.renderComponent({\n\t\t\t\t\tcustomSettingsData: props.value\n\t\t\t\t});\n\t\t\t} else if (props.property === 'componentLayout') {\n\t\t\t\tthis.renderComponent({\n\t\t\t\t\tcomponentLayout: props.value\n\t\t\t\t});\n\t\t\t}\n\t\t})\n\n/*\t\t// listen for actions\n \t\tthis.sitesSDK.subscribe(this.sitesSDK.MESSAGE_TYPES.EXECUTE_ACTION, (args) => {\n\t\t\t// get action and payload\n\t\t\tvar payload = Array.isArray(args.payload) ? args.payload : [],\n\t\t\t\taction = args.action;\n\n\t\t\t// handle 'setImageWidth' actions\n\t\t\tif (action && action.actionName === 'setImageWidth') {\n\t\t\t\tpayload.forEach((data) => {\n\t\t\t\t\tif (data.name === 'imageWidth') {\n\t\t\t\t\t\tthis.renderComponent({\n\t\t\t\t\t\t\timageWidth: data.value\n\t\t\t\t\t\t});\n\t\t\t\t\t}\n\t\t\t\t});\n\t\t\t}\n\t\t}); */\n\t}\n\n\t// insert the component's HTML into the page \n\t// after it has added the component, it applies any clickHandlers to elements that were added to the page\n\trenderComponent(args) {\n\t\t// add in the event listeners\n\t\tthis.addEventListeners();\n\n\t\tPromise.all([SCSRenderAPI.importText(this.assetsPath + '/layout.html'),\n\t\t\tSCSRenderAPI.importCSS(this.assetsPath + '/styles/design.css')\n\t\t]).then(async (componentResources) => {\n\t\t\tconst componentTemplate = componentResources[0];\n\n\t\t\t// use the common code to generate the HTML for this component based on the componentLayout and customSettingsData\n\t\t\tconst componentHTML = await CommonUtils.createHTML({\n\t\t\t\tMustache: this.Mustache,\n\t\t\t\tcomponentLayout: this.sitesSDK.getProperty('componentLayout'),\n\t\t\t\tcustomSettingsData: this.sitesSDK.getProperty('customSettingsData'),\n\t\t\t\tid: this.id,\n\t\t\t\ttemplate: componentTemplate,\n\t\t\t\tcontentClient: this.contentClient,\n\t\t\t\tapi: SCSRenderAPI\n\t\t\t}, args)\n\n\t\t\t// replace the content of the container with the rendered HTML\n\t\t\tthis.container.innerHTML = componentHTML\n\n\t\t\tCommonUtils.addArticleList({contentClient: this.contentClient, Mustache: this.Mustache, assetsPath: this.assetsPath}, this.container)\n\t\t})\n\t}\n\t/**\n\t * Called when a component is compiled into the page.\n\t * Requests the most recent articles and loads them into the preloaded template\n\t * @param {*} container \n\t */\n\tasync hydrate(container) {\n\t\tthis.container = container\n\t\tawait CommonUtils.addArticleList({contentClient: this.contentClient, Mustache: this.Mustache, assetsPath: this.assetsPath}, container)\n\t}\n\n\t// the render method is called to render the component dynamically onto the page \n\trender(container) {\n\t\t// Listen for settings changes, so we can re-render the component\n\t\tthis.addEventListeners();\n\n\t\tthis.container = container;\n\t\tthis.renderComponent();\n\t}\n}"],"names":["CommonUtils","constructor","static","context","contentClient","arModel","queryItems","q","fields","limit","orderBy","items","month","day","year","timeZone","detailPageId","Number","SCSRenderAPI","getCustomSiteProperty","isNaN","renderMode","RENDER_MODE_EDIT","RENDER_MODE_PREVIEW","alert","forEach","item","publish_date","Date","value","toLocaleDateString","objDateOptions","detail_url","getPageLinkData","contentId","id","contentType","type","contentSlug","slug","href","news","container","objArticles","getArticles","elArticleList","querySelector","importText","assetsPath","innerHTML","Mustache","render","elTemplate","e","console","log","args","customSettingsData","template","api","customData","nls","model","cta","title","this","mode","viewMode","sitesSDK","SitesSDK","getMustache","getProperty","import","meta","url","replace","addEventListeners","subscribe","MESSAGE_TYPES","SETTINGS_UPDATED","props","property","renderComponent","componentLayout","Promise","all","importCSS","then","async","componentTemplate","componentResources","componentHTML","createHTML","addArticleList"],"mappings":"AACqB,MAAAA,EACpBC,cAEA,CAKwBC,yBAACC,GACxB,MAAMC,EAAgBD,EAAQC,cAE9B,IAMIC,SANoBD,EAAcE,WAAW,CAChDC,EAAK,0BACLC,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