{"version":3,"file":"render.mjs","sources":["src/common.mjs","src/render.mjs"],"sourcesContent":["// Common Utilities Class\nexport default class {\n constructor(contentClient, noPermissionToView) {\n this.contentClient = contentClient;\n this.noPermissionToView = noPermissionToView || \"You do not have permission to view this asset\";\n this.libs = this.contentClient.getLibs() || {};\n }\n\n // Helper method to make an additional Content REST API call to retrieve all items referenced in the data by their ID.\n getRefItems(ids) {\n // Calling getItems() with no ‘ids’ returns all items.\n // If no items are requested, just return a resolved Promise.\n if (ids.length === 0) {\n return Promise.resolve({});\n } else {\n return this.contentClient.getItems({\n \"ids\": ids\n });\n }\n }\n\n //\n // Helper Methods to handle specific field types\n //\n\n findReferenceFieldIds(referencedItems, fields) {\n const referencedIds = [];\n\n referencedItems.forEach((referencedItem) => {\n if (fields[referencedItem]) {\n\n // handle multiple or single value content fields\n (Array.isArray(fields[referencedItem]) ? fields[referencedItem] : [fields[referencedItem]]).forEach((entry) => {\n if (entry) {\n // if asset is accessible, add it\n if (!entry.reference || entry.reference.isAccessible) {\n referencedIds.push(entry.id);\n } else {\n // asset is not accessible, store the message against the entry\n entry.referenceInaccessible = this.noPermissionToView;\n }\n }\n });\n }\n });\n\n return referencedIds;\n }\n\n updateRichTextFields(richTextFields, fields) {\n richTextFields.forEach((richTextField) => {\n if (fields[richTextField]) {\n\n // handle multiple or single value content fields\n if (Array.isArray(fields[richTextField])) {\n fields[richTextField] = fields[richTextField].map((entry) => {\n return this.contentClient.expandMacros(entry);\n });\n } else {\n fields[richTextField] = this.contentClient.expandMacros(fields[richTextField]);\n }\n }\n });\n }\n\n addReferencedItems(referencedItems, results, fields) {\n (results && results.items || []).forEach((item) => {\n referencedItems.forEach((referencedItem) => {\n (Array.isArray(fields[referencedItem]) ? fields[referencedItem] : [fields[referencedItem]]).forEach((entry) => {\n // Retrieve the reference item from the query result.\n if (entry && entry.id === item.id) {\n entry.contentItem = item;\n\n // add in assetURLs for any digital asset references in the referenced item\n const digitalAssetFields = Object.keys(item.fields).filter((key) => {\n const field = item.fields[key];\n return field && (typeof field === 'object') && (field.typeCategory === 'DigitalAssetType');\n });\n this.updateDigitalAssetURLs(digitalAssetFields, item.fields) \n }\n });\n });\n });\n }\n}","\n/* globals Mustache */\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\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\trender(parentObj) {\n\t\tconst contentClient = this.contentClient;\n\t\tconst noPermissionToViewMsg = \"You do not have permission to view this asset\";\n\t\tconst commonUtils = new CommonUtils(contentClient, noPermissionToViewMsg);\n\n\t\tlet contentType;\n\t\tlet customSettings;\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\tcustomSettings = this.content.scsData.customSettingsData || {};\n\t\t}\n\n\t\t// Handle richText expansion\n\t\tconst richTextFields = [\"content\"];\n\t\tcommonUtils.updateRichTextFields(richTextFields, this.content.fields);\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\tcontentClient.importText(this.assetsFolder + '/layout.html'),\n\t\t\tcontentClient.importCSS(this.assetsFolder + '/design.css')\n\t\t]).then((resources) => {\n\t\t\tconst templateHtml = resources[0];\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 = this.Mustache.render(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} catch (e) {\n\t\t\t\tconsole.error(e.stack);\n\t\t\t}\n\t\t});\n\t}\n}"],"names":["CommonUtils","constructor","contentClient","noPermissionToView","this","libs","getLibs","getRefItems","ids","length","Promise","resolve","getItems","findReferenceFieldIds","referencedItems","fields","referencedIds","forEach","referencedItem","Array","isArray","entry","reference","isAccessible","push","id","referenceInaccessible","updateRichTextFields","richTextFields","richTextField","map","expandMacros","addReferencedItems","results","items","item","contentItem","digitalAssetFields","Object","keys","filter","key","field","typeCategory","updateDigitalAssetURLs","render","params","contentItemData","scsData","assetsFolder","import","meta","url","replace","Mustache","window","parentObj","commonUtils","content","assign","showPublishedContent","customSettingsData","all","importText","importCSS","then","resources","templateHtml","template","insertAdjacentHTML","e","console","error","stack"],"mappings":"AACqB,MAAAA,EACjBC,YAAYC,EAAeC,GACvBC,KAAKF,cAAgBA,EACrBE,KAAKD,mBAAqBA,GAAsB,gDAChDC,KAAKC,KAAOD,KAAKF,cAAcI,WAAa,CAAA,CAC/C,CAGDC,YAAYC,GAGR,OAAmB,IAAfA,EAAIC,OACUC,QAACC,QAAQ,CAAA,GAEXT,KAAAA,cAAcU,SAAS,CAC/BJ,IAAOA,GAGlB,CAMDK,sBAAsBC,EAAiBC,GACnC,MAAMC,EAAgB,GAoBtB,OAlBAF,EAAgBG,SAASC,IACjBH,EAAOG,KAGNC,MAAMC,QAAQL,EAAOG,IAAmBH,EAAOG,GAAkB,CAACH,EAAOG,KAAkBD,SAASI,IAC7F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