UNPKG

@trustarc/react-cookie-consent-manager

Version:

React components to integrate TrustArc Cookie Consent Manager into React Frameworks

119 lines (118 loc) 7.04 kB
import { defaultOptions } from "./const/default-options.const.js"; import { queryStringParams } from "./const/query-params.const.js"; import { TrustArcVersion } from "./enum/trustarc-version.enum.js"; export class TrustArcHelper { // Generate query string based on given parameters static generateQueryString(params) { var mappedQuery = 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 param !== "" ? index === 0 ? param : `&${param}` : ""; }); mappedQuery = mappedQuery.filter(item => item.length > 0 && item.indexOf('undefined') === -1); return mappedQuery.join(""); } // Log debug messages if debugging is enabled static logDebug(debug, ...args) { debug = debug !== null && debug !== void 0 ? debug : false; if (debug) { console.log(...args); } } // Repopulate a div based on provided options static repopulateDiv(options, bannerId, divId) { var _a, _b, _c, _d, _e, _f, _g, _h, _j; if (!divId || divId == "") { return; } const element = document.getElementById(divId); if (!element) { console.log(`Div ${divId} not found, skipping update.`); this.logDebug(options.debug, `Div ${divId} not found, skipping update.`); return; } if (((_a = element.childNodes) === null || _a === void 0 ? void 0 : _a.length) === 0) { this.logDebug(options.debug, `Repopulating empty div: ${divId}`); switch (divId) { case bannerId: if ((_b = window.truste) === null || _b === void 0 ? void 0 : _b.bn) { window.truste.bn.bannerMain(); } this.logDebug(options.debug, "Called window.truste.bn.bannerMain() for consent banner."); break; case options.cookiePreferencesContainer: if ((_d = (_c = window.truste) === null || _c === void 0 ? void 0 : _c.eu) === null || _d === void 0 ? void 0 : _d.icon) { window.truste.eu.icon.initialize(); } this.logDebug(options.debug, "Called window.truste.eu.icon.initialize() for cookie preferences link."); break; case options.irmContainer: { const irmLink = (_f = (_e = window === null || window === void 0 ? void 0 : window.truste) === null || _e === void 0 ? void 0 : _e.eu) === null || _f === void 0 ? void 0 : _f.irmLink; const irmLinkContainer = (_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.irmLinkContainer; if (irmLink && irmLinkContainer) { (_j = element.parentElement) === null || _j === void 0 ? void 0 : _j.insertBefore(irmLinkContainer, element.nextSibling); irmLinkContainer.appendChild(irmLink); element.remove(); } } break; default: this.logDebug(options.debug, `No specific repopulation function for div: ${divId}`); } } else { this.logDebug(options.debug, `Div ${divId} is already populated, skipping update.`); } } // Get the script source based on options and CMPID static getScriptSource(options, CMPID) { var _a, _b; if (!((_a = window.trusteSettings) === null || _a === void 0 ? void 0 : _a.URL_ADV) || !((_b = window.trusteSettings) === null || _b === void 0 ? void 0 : _b.URL_PRO)) { this.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 `${window.trusteSettings.URL_PRO}/${CMPID}` + (!hasQueryString ? "" : "?" + this.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: options.onclose, condition: onclose }, { name: queryStringParams.OSTYPE, value: options.ostype, 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 `${window.trusteSettings.URL_ADV}` + "?" + this.generateQueryString(queryParams); } static setDefaultValues(options) { return Object.assign(Object.assign({}, options), defaultOptions); } }