UNPKG

@shopgate/pwa-common

Version:

Common library for the Shopgate Connect PWA.

114 lines (105 loc) 3.48 kB
/* eslint-disable extra-rules/potential-point-free */ /** * Class to maintain embedded media within DOM containers. */ let EmbeddedMedia = /*#__PURE__*/function () { /** * Constructor */ function EmbeddedMedia() { this.providers = new Set(); } /** * Add a provider for embedded media. * @param {Object} provider A provider instance. */ var _proto = EmbeddedMedia.prototype; _proto.addProvider = function addProvider(provider) { this.providers.add(provider); } /** * Remove a provider for embedded media. * @param {Object} provider A provider instance. */; _proto.removeProvider = function removeProvider(provider) { this.providers.delete(provider); } /** * Add a DOM container with embedded media. * @param {ParentNode} container A DOM container. */; _proto.add = function add(container) { this.providers.forEach(provider => { provider.add(container); }); } /** * Remove a DOM container. Should be called whenever a component which hosts a DOM container with * embedded media is unmounted. * @param {ParentNode} container A DOM container. */; _proto.remove = function remove(container) { this.providers.forEach(provider => { provider.remove(container); }); } /** * Searches for embedded media and replaces it with a placeholder element when required cookie * consent is not accepted. * Should be invoked before container content is added to the DOM to fulfill all regulations. * @param {ParentNode} container A DOM container. * @param {Object} [cookieConsentSettings] Additional settings related to cookie consent. * @param {boolean} [cookieConsentSettings.comfortCookiesAccepted] Whether comfort cookies * are accepted. * @param {boolean} [cookieConsentSettings.statisticsCookiesAccepted] Whether statistics cookies * are accepted. */; _proto.handleCookieConsent = function handleCookieConsent(container, cookieConsentSettings = {}) { const cookieConsent = { comfortCookiesAccepted: false, statisticsCookiesAccepted: false, ...cookieConsentSettings }; this.providers.forEach(provider => { if (provider.handleCookieConsent) { provider.handleCookieConsent(container, cookieConsent); } }); } /** * Applies optimizations to embedded media iframes within the given container. * Common enhancements include adding responsive wrappers and appropriate * sandbox attributes to improve security and layout behavior. * * @param {Document} document - The DOM document containing iframes to optimize. */; _proto.applyIframeOptimizations = function applyIframeOptimizations(document) { this.providers.forEach(provider => { provider.applyIframeOptimizations(document); }); } /** * Stops all playing media within the DOM containers. */; _proto.stop = function stop() { this.providers.forEach(provider => { provider.stop(); }); } /** * Check if we have media providers with not-ready SDK * @returns {boolean} */; _proto.getHasPendingProviders = function getHasPendingProviders() { let hasPendingProviders = false; this.providers.forEach(provider => { if (provider.isPending) { hasPendingProviders = true; } }); return hasPendingProviders; }; return EmbeddedMedia; }(); export default new EmbeddedMedia(); /* eslint-enable extra-rules/potential-point-free */