@trustarc/react-cookie-consent-manager
Version:
React components to integrate TrustArc Cookie Consent Manager into React Frameworks
97 lines (96 loc) • 6 kB
JavaScript
;
const generateQueryString = (params) => params.map((item, index) => {
var _a;
const shouldCreateParam = (_a = item.condition) !== null && _a !== void 0 ? _a : item.value;
const param = shouldCreateParam ?
(item.value ? `${item.name}=${item.value}` : item.name)
: "";
return index === 0 ? param : `&${param}`;
});
const logDebug = (debug, ...args) => {
debug = debug !== null && debug !== void 0 ? debug : false;
if (debug) {
console.log(...args);
}
};
const repopulateDiv = (options, bannerId, divId) => {
var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l;
if (!divId || divId == "") {
return;
}
const element = document.getElementById(divId);
if (!element) {
logDebug(options.debug, `Div ${divId} not found, skipping update.`);
return;
}
if (((_a = element.childNodes) === null || _a === void 0 ? void 0 : _a.length) === 0) {
logDebug(options.debug, `Repopulating empty div: ${divId}`);
switch (divId) {
case bannerId:
(_c = (_b = window === null || window === void 0 ? void 0 : window.truste) === null || _b === void 0 ? void 0 : _b.bn) === null || _c === void 0 ? void 0 : _c.reopenBanner();
logDebug(options.debug, "Called window.truste.bn.reopenBanner() for consent banner.");
break;
case options.cookiePreferencesContainer:
(_f = (_e = (_d = window === null || window === void 0 ? void 0 : window.truste) === null || _d === void 0 ? void 0 : _d.eu) === null || _e === void 0 ? void 0 : _e.icon) === null || _f === void 0 ? void 0 : _f.initialize();
logDebug(options.debug, "Called window.truste.eu.icon.initialize() for cookie preferences link.");
break;
case options.irmContainer:
{
const irmLink = (_h = (_g = window === null || window === void 0 ? void 0 : window.truste) === null || _g === void 0 ? void 0 : _g.eu) === null || _h === void 0 ? void 0 : _h.irmLink;
const irmLinkContainer = (_k = (_j = window === null || window === void 0 ? void 0 : window.truste) === null || _j === void 0 ? void 0 : _j.eu) === null || _k === void 0 ? void 0 : _k.irmLinkContainer;
if (irmLink && irmLinkContainer) {
(_l = element.parentElement) === null || _l === void 0 ? void 0 : _l.insertBefore(irmLinkContainer, element.nextSibling);
irmLinkContainer.appendChild(irmLink);
element.remove();
}
}
break;
default:
logDebug(options.debug, `No specific repopulation function for div: ${divId}`);
}
}
else {
logDebug(options.debug, `Div ${divId} is already populated, skipping update.`);
}
};
const getScriptSource = (options, CMPID) => {
var _a, _b, _c, _d, _e, _f;
if (!((_b = (_a = window.truste) === null || _a === void 0 ? void 0 : _a.settings) === null || _b === void 0 ? void 0 : _b.URL_ADV) || !((_d = (_c = window.truste) === null || _c === void 0 ? void 0 : _c.settings) === null || _d === void 0 ? void 0 : _d.URL_PRO)) {
logDebug(options.debug, "Trustart PRO or ADV url setting is missing.");
return "";
}
const hasQueryString = options.language || options.pcookie || options.countryCodeorIP || options.privacyPolicyLink || options.cookiePolicyLink;
const isProVersion = options.ccmVersion === TrustArcVersion.Pro;
if (isProVersion) {
const queryParams = [
{ name: queryStringParams.LOCALE, value: options.language },
{ name: queryStringParams.PCOOKIE, value: options.pcookie },
{ name: queryStringParams.COUNTRY_CODE_OR_IP, value: options.countryCodeorIP },
{ name: queryStringParams.PRIVACY_POLICY_LINK, value: options.privacyPolicyLink },
{ name: queryStringParams.COOKIE_POLICY_LINK, value: encodeURIComponent(options.cookiePolicyLink), condition: options.cookiePolicyLink }
];
return `${(_e = window.truste) === null || _e === void 0 ? void 0 : _e.settings.URL_PRO}/${CMPID}` + !hasQueryString ? "" : ("?" + generateQueryString(queryParams));
}
const queryParams = [
{ name: queryStringParams.DOMAIN, value: CMPID },
{ name: queryStringParams.GTM, value: 1 },
{ name: queryStringParams.COOKIE_PREFERENCES_CONTAINER, value: options.cookiePreferencesContainer },
{ name: queryStringParams.JAVASCRIPT, value: options.js },
{ name: queryStringParams.NOTICE_TYPE, value: options.noticeType },
{ name: queryStringParams.COUNTRY, value: options.countryCodeorIP },
{ name: queryStringParams.STATE, value: options.state },
{ name: "text", value: true, condition: !options.showCookiePreferencesAsButton },
{ name: queryStringParams.PCOOKIE, condition: options.pcookie },
{ name: queryStringParams.LANGUAGE, value: options.language },
{ name: queryStringParams.BEHAVIOR, value: options.behavior },
{ name: queryStringParams.IRM_CONTAINER, value: options.irmContainer },
{ name: queryStringParams.PN, value: options.pn },
{ name: queryStringParams.FADE, value: options.fade },
{ name: queryStringParams.PX, value: options.px },
{ name: queryStringParams.ONCLOSE, value: 1, condition: onclose },
{ name: queryStringParams.OSTYPE, value: "mobile", condition: options.ostype },
{ name: queryStringParams.PRIVACY_POLICY_LINK, value: encodeURIComponent(options.privacyPolicyLink), condition: options.privacyPolicyLink },
{ name: queryStringParams.COOKIE_POLICY_LINK, value: encodeURIComponent(options.cookiePolicyLink), condition: options.cookiePolicyLink },
];
return `${(_f = window.truste) === null || _f === void 0 ? void 0 : _f.settings.URL_ADV}` + generateQueryString(queryParams);
};