@communities-webruntime/services
Version:
If you would like to run Lightning Web Runtime without the CLI, we expose some of our programmatic APIs available in Node.js. If you're looking for the CLI documentation [you can find that here](https://www.npmjs.com/package/@communities-webruntime/cli).
72 lines • 2.7 kB
JavaScript
;
/** @hidden */
/**
* Copyright (c) 2019, salesforce.com, inc.
* All rights reserved.
* SPDX-License-Identifier: MIT
* For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/MIT
*/
/**
* Transform each branding properties to HTML-generation-friendly version and
* converts it to a map with each unique tokens as a key.
*
* Overrides duplicates sequentially.
*
* @param {*} brandingProperties List of branding properties
*/
function getBrandingTokenMap(brandingProperties) {
return brandingProperties.reduce((result, brandingProperty) => {
brandingProperty.tokens.forEach(token => {
result[token] = getBrandingPropertyValue(brandingProperty);
});
return result;
}, {});
}
/**
* Evaluates a branding property and performs any necessary transformations to the value
* used in the HTML generation.
*
* @param {Map} brandingProperty the branding property that has at least an id and value
*/
function getBrandingPropertyValue(brandingProperty) {
let normalizedValue = brandingProperty.value;
// If 'type' is defined, we need to do some transformations for special site.com declarative values
if (brandingProperty.type &&
brandingProperty.type.name &&
brandingProperty.type.name === 'Image') {
normalizedValue = processImageTypeValue(brandingProperty.value);
}
if (brandingProperty.type &&
brandingProperty.type.name &&
brandingProperty.type.name === 'Picklist') {
normalizedValue = getPicklistTypeValue(brandingProperty.value);
}
return normalizedValue;
}
/**
* Process a declarative branding property value of type Image.
* Expects a raw image URL.
* @param {string} value image branding property value
*/
function processImageTypeValue(value) {
return `url(${value})`;
}
/**
* Fix any declarative values that need massaging to work off-core.
* Get the canonical value from a declarative branding property value of type Picklist.
* Expects a string in the form "labelA:valueA,labelB:valueB:default,labelC:valueC,..."
* @param {string} value picklist branding property value
*/
function getPicklistTypeValue(value) {
const allOptions = value.split(',');
const defaultOptions = allOptions.filter(o => o.match(/:default$/i));
// Use the default, if one is defined. Otherwise, use the first picklist option.
const selectedOption = defaultOptions.length > 0 ? defaultOptions[0] : allOptions[0];
// Separate the label:value
return selectedOption.split(':')[1];
}
module.exports = {
getBrandingTokenMap,
getBrandingPropertyValue,
};
//# sourceMappingURL=process-branding.js.map