{"version":3,"file":"render.mjs","sources":["src/common.mjs","src/render.mjs"],"sourcesContent":["// Common Utilities Class\nexport default class {\n\tconstructor(objArgs) {\n\t\tthis.contentClient = objArgs.contentClient;\n\t\tthis.Mustache = objArgs.Mustache;\n\t\tthis.noPermissionToView =\n\t\t\tobjArgs.noPermissionToView || 'You do not have permission to view this asset';\n\t\tthis.objModel = objArgs.content;\n\t}\n\n\t// Helper method to make an additional Content REST API call to retrieve all items referenced in the data by their ID.\n\tgetRefItems(ids) {\n\t\t// Calling getItems() with no ‘ids’ returns all items.\n\t\t// If no items are requested, just return a resolved Promise.\n\t\tif (ids.length === 0) {\n\t\t\treturn Promise.resolve({});\n\t\t} else {\n\t\t\treturn this.contentClient.getItems({\n\t\t\t\t'ids': ids,\n\t\t\t});\n\t\t}\n\t}\n\n\t//\n\t// Helper Methods to handle specific field types\n\t//\n\n\tfindReferenceFieldIds(referencedItems, fields) {\n\t\tconst referencedIds = [];\n\n\t\treferencedItems.forEach((referencedItem) => {\n\t\t\tif (fields[referencedItem]) {\n\t\t\t\t// handle multiple or single value content fields\n\t\t\t\t(Array.isArray(fields[referencedItem])\n\t\t\t\t\t? fields[referencedItem]\n\t\t\t\t\t: [fields[referencedItem]]\n\t\t\t\t).forEach((entry) => {\n\t\t\t\t\tif (entry) {\n\t\t\t\t\t\t// if asset is accessible, add it\n\t\t\t\t\t\tif (!entry.reference || entry.reference.isAccessible) {\n\t\t\t\t\t\t\treferencedIds.push(entry.id);\n\t\t\t\t\t\t} else {\n\t\t\t\t\t\t\t// asset is not accessible, store the message against the entry\n\t\t\t\t\t\t\tentry.referenceInaccessible = this.noPermissionToView;\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\n\t\treturn referencedIds;\n\t}\n\n\tupdateDigitalAssetURLs(digitalAssetFields, fields) {\n\t\tdigitalAssetFields.forEach((digitalAssetField) => {\n\t\t\tif (fields[digitalAssetField]) {\n\t\t\t\t// handle multiple or single value content fields\n\t\t\t\t(Array.isArray(fields[digitalAssetField])\n\t\t\t\t\t? fields[digitalAssetField]\n\t\t\t\t\t: [fields[digitalAssetField]]\n\t\t\t\t).forEach((entry) => {\n\t\t\t\t\tif (entry) {\n\t\t\t\t\t\tif (!entry.reference || entry.reference.isAccessible) {\n\t\t\t\t\t\t\tif (entry.type === 'Video' || entry.type === 'File') {\n\t\t\t\t\t\t\t\tentry.showName = true;\n\t\t\t\t\t\t\t} else {\n\t\t\t\t\t\t\t\tentry.url = this.contentClient.getRenditionURL({\n\t\t\t\t\t\t\t\t\t'id': entry.id,\n\t\t\t\t\t\t\t\t});\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t} else {\n\t\t\t\t\t\t\t// asset is not accessible, store the message against the entry\n\t\t\t\t\t\t\tentry.referenceInaccessible = this.noPermissionToView;\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\taddReferencedItems(referencedItems, results, fields) {\n\t\t((results && results.items) || []).forEach((item) => {\n\t\t\treferencedItems.forEach((referencedItem) => {\n\t\t\t\t(Array.isArray(fields[referencedItem])\n\t\t\t\t\t? fields[referencedItem]\n\t\t\t\t\t: [fields[referencedItem]]\n\t\t\t\t).forEach((entry) => {\n\t\t\t\t\t// Retrieve the reference item from the query result.\n\t\t\t\t\tif (entry && entry.id === item.id) {\n\t\t\t\t\t\tentry.contentItem = item;\n\n\t\t\t\t\t\t// add in assetURLs for any digital asset references in the referenced item\n\t\t\t\t\t\tconst digitalAssetFields = Object.keys(item.fields).filter((key) => {\n\t\t\t\t\t\t\tconst field = item.fields[key];\n\t\t\t\t\t\t\treturn (\n\t\t\t\t\t\t\t\tfield &&\n\t\t\t\t\t\t\t\ttypeof field === 'object' &&\n\t\t\t\t\t\t\t\tfield.typeCategory === 'DigitalAssetType'\n\t\t\t\t\t\t\t);\n\t\t\t\t\t\t});\n\t\t\t\t\t\tthis.updateDigitalAssetURLs(digitalAssetFields, item.fields);\n\t\t\t\t\t}\n\t\t\t\t});\n\t\t\t});\n\t\t});\n\t}\n\n\t/**\n\t * Expand custom macros in the returned HTML\n\t * @param {string} strInput\n\t * @returns {string} HTML string with macros expanded\n\t */\n\texpandMacros(strInput) {\n\t\t// replace \"[[page-221]]\" with \"[!--$SCS_PAGE--]221[/!--$SCS_PAGE--]\"\n\t\tlet strOutput = strInput.replace(\n\t\t\t/\\[\\[page-(\\d+)\\]\\]/g,\n\t\t\t'[!--$SCS_PAGE--]$1[/!--$SCS_PAGE--]'\n\t\t);\n\n\t\t// replace \"[[asset-221]]\" with \"[!--$CEC_DIGITAL_ASSET--]221[/--$CEC_DIGITAL_ASSET--]\"\n\t\tstrOutput = strOutput.replace(\n\t\t\t/\\[\\[asset-(\\d+)\\]\\]/g,\n\t\t\t'[!--$CEC_DIGITAL_ASSET--]$1[/!--$CEC_DIGITAL_ASSET--]'\n\t\t);\n\n\t\tstrOutput = this.contentClient.expandMacros(strOutput);\n\t\treturn strOutput;\n\t}\n\n\tasync createHTML(template, objModel) {\n\t\t// Render the contents\n\t\t// First render marries asset data with the template\n\t\t// Second render marries mustache tokens from within the asset data\n\t\t// to the model, example {{ThisYear}}\n\t\tlet strRenderedHTML = this.Mustache.render(template, objModel);\n\t\tstrRenderedHTML = this.Mustache.render(strRenderedHTML, objModel);\n\t\tstrRenderedHTML = this.expandMacros(strRenderedHTML);\n\t\treturn strRenderedHTML;\n\t}\n}\n","\n/* globals Mustache */\n\nimport CommonUtils from './common.mjs';\n\nexport default class {\n\t// class variables\n\tcontentVersion = \">=1.0.0 <2.0.0\"\n\n\t// Content Layout constructor\n\tconstructor(params) {\n\t\t// store passed in values\n\t\tthis.contentItemData = params.contentItemData || {};\n\t\tthis.scsData = params.scsData;\n\t\tthis.contentClient = params.contentClient;\n\t\tthis.libs = this.contentClient.getLibs() || {};\n\t\t// store path to the \"assets\" folder\n\t\tthis.assetsFolder = import.meta.url.replace('/render.mjs', '');\n\n\t\t// access resources\n\t\tthis.Mustache = params.Mustache || this.libs.Mustache || window.Mustache;\n\t}\n\n\t// Main rendering function:\n\t// - Updates the data to handle any required additional requests and support granular permissions\n\t// - Expand the Mustache template with the updated data\n\t// - Appends the expanded template HTML to the parentObj DOM element\n\n\taddClickHandlers(parentObj) {\n\t\tconst flipCard = parentObj.querySelector(\".flip\")\n\t\tif(flipCard){\n\t\t\tflipCard.addEventListener(\"click\", () => {\n\t\t\t\tflipCard.classList.toggle(\"active\")\n\t\t\t})\n\t\t}\n\n\n\t}\n\n\trender(parentObj) {\n\t\tconst contentClient = this.contentClient;\n\t\tlet contentType;\n\t\tlet customSettings;\n\t\tlet secureContent = false;\n\n\t\t// extract the content that will be used as the model\n\t\tthis.content = Object.assign({}, this.contentItemData);\n\n\t\t// If used with CECS Sites, Sites will pass in context information via the scsData property.\n\t\tif (this.scsData) {\n\t\t\tthis.content = Object.assign(this.content, {\n\t\t\t\t\"scsData\": this.scsData\n\t\t\t});\n\t\t\tcontentType = this.content.scsData.showPublishedContent === true ? \"published\" : \"draft\";\n\t\t}\n\n\t\tlet objContext = {\n\t\t\tMustache: this.Mustache,\n\t\t\tcontentClient: contentClient,\n\t\t\tcontent: this.contentItemData,\n\t\t}\n\t\tconst commonUtils = new CommonUtils(objContext);\n\n\t\t//\n\t\t// Handle fields specific to this content type.\n\t\t//\n\n\t\t// If displaying detail items, get the IDs of any referenced assets\n\t\t// we will do an additional query to retrieve these so we can render them as well.\n\t\tconst referredFields = [\"primary_link\", \"link\"];\n\t\tconst referredIds = commonUtils.findReferenceFieldIds(referredFields, this.content.fields);\n\n\t\t// Handle expansion of URLs and check permissions for access to referenced digital asset\n\t\tconst digitalAssetFields = [\"image\"];\n\t\tcommonUtils.updateDigitalAssetURLs(digitalAssetFields, this.content.fields);\n\n\n\t\t//\n\t\t// Fetch any referenced items and resources used to render the content layout\n\t\t//\n\t\tPromise.all([\n\t\t\tcommonUtils.getRefItems(referredIds),\n\t\t\tcontentClient.importText(this.assetsFolder + '/layout.html'),\n\t\t\tcontentClient.importCSS(this.assetsFolder + '/styles/design.css')\n\t\t]).then(async (resources) => {\n\t\t\tconst results = resources[0];\n\t\t\tconst templateHtml = resources[1];\n\t\t\t// Store the retrieved referenced items in the model used by the template.\n\t\t\tcommonUtils.addReferencedItems(referredFields, results, this.content.fields);\n\n\t\t\t// apply the model to the template\n\t\t\ttry {\n\t\t\t\t// Use Mustache to expand the HTML template with the data.\n\t\t\t\tconst template = await commonUtils.createHTML(templateHtml, this.content);\n\n\t\t\t\t// Insert the expanded template into the passed in container.\n\t\t\t\tif (template) {\n\t\t\t\t\tparentObj.insertAdjacentHTML('beforeend', template);\n\t\t\t\t}\n\t\t\t\tthis.addClickHandlers(parentObj);\n\t\t\t} catch (e) {\n\t\t\t\tconsole.error(e.stack);\n\t\t\t}\n\t\t});\n\t}\n\thydrate(parentObj) {\n\t\tconst contentClient = this.contentClient;\n\t\tlet contentType;\n\t\tlet customSettings;\n\t\tlet secureContent = false;\n\n\t\t// extract the content that will be used as the model\n\t\tthis.content = Object.assign({}, this.contentItemData);\n\n\t\t// If used with CECS Sites, Sites will pass in context information via the scsData property.\n\t\tif (this.scsData) {\n\t\t\tthis.content = Object.assign(this.content, {\n\t\t\t\t\"scsData\": this.scsData\n\t\t\t});\n\t\t\tcontentType = this.content.scsData.showPublishedContent === true ? \"published\" : \"draft\";\n\t\t}\n\n\t\ttry {\n\t\t\tthis.addClickHandlers(parentObj);\n\t\t} catch (e) {\n\t\t\tconsole.error(e.stack);\n\t\t}\n\t}\n}\n"],"names":["constructor","objArgs","this","contentClient","Mustache","noPermissionToView","objModel","content","getRefItems","ids","length","Promise","resolve","getItems","findReferenceFieldIds","referencedItems","fields","referencedIds","forEach","referencedItem","Array","isArray","entry","reference","isAccessible","push","id","referenceInaccessible","updateDigitalAssetURLs","digitalAssetFields","digitalAssetField","type","showName","url","getRenditionURL","addReferencedItems","results","items","item","contentItem","Object","keys","filter","key","field","typeCategory","expandMacros","strInput","strOutput","replace","async","template","render","strRenderedHTML","params","_defineProperty","contentItemData","scsData","libs","getLibs","assetsFolder","import","meta","window","addClickHandlers","parentObj","flipCard","querySelector","addEventListener","classList","toggle","assign","showPublishedContent","objContext","commonUtils","CommonUtils","referredFields","referredIds","all","importText","importCSS","then","resources","templateHtml","createHTML","insertAdjacentHTML","e","console","error","stack","hydrate"],"mappings":"gdAECA,YAAYC,GACXC,KAAKC,cAAgBF,EAAQE,cAC7BD,KAAKE,SAAWH,EAAQG,SACxBF,KAAKG,mBACJJ,EAAQI,oBAAsB,gDAC/BH,KAAKI,SAAWL,EAAQM,OACzB,CAGAC,YAAYC,GAGX,OAAmB,IAAfA,EAAIC,OACAC,QAAQC,QAAQ,CAAA,GAEhBV,KAAKC,cAAcU,SAAS,CAClCJ,IAAOA,GAGV,CAMAK,sBAAsBC,EAAiBC,GACtC,MAAMC,EAAgB,GAsBtB,OApBAF,EAAgBG,SAASC,IACpBH,EAAOG,KAETC,MAAMC,QAAQL,EAAOG,IACnBH,EAAOG,GACP,CAACH,EAAOG,KACTD,SAASI,IACNA,KAEEA,EAAMC,WAAaD,EAAMC,UAAUC,aACvCP,EAAcQ,KAAKH,EAAMI,IAGzBJ,EAAMK,sBAAwBzB,KAAKG,mBAErC,GAEF,KAIF,CAEAuB,uBAAuBC,EAAoBb,GAC1Ca,EAAmBX,SAASY,IACvBd,EAAOc,KAETV,MAAMC,QAAQL,EAAOc,IACnBd,EAAOc,GACP,CAACd,EAAOc,KACTZ,SAASI,IACNA,KACEA,EAAMC,WAAaD,EAAMC,UAAUC,aACpB,UAAfF,EAAMS,MAAmC,SAAfT,EAAMS,KACnCT,EAAMU,UAAW,EAEjBV,EAAMW,IAAM/B,KAAKC,cAAc+B,gBAAgB,CAC9CR,GAAMJ,EAAMI,KAKdJ,EAAMK,sBAAwBzB,KAAKG,mBAErC,GAEF,GAEF,CAEA8B,mBAAmBpB,EAAiBqB,EAASpB,IAC1CoB,GAAWA,EAAQC,OAAU,IAAInB,SAASoB,IA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