diff --git a/index.mjs b/index.mjs index 02e1bbb..883212e 100644 --- a/index.mjs +++ b/index.mjs @@ -1,3 +1,3 @@ -import CookieConsent from './src/cookie-consent.mjs'; +import Dialog from "./src/dialog"; -export default CookieConsent; +export default Dialog; diff --git a/package.json b/package.json index ea77344..3256b08 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@grrr/cookie-consent", - "version": "1.2.2", + "version": "1.2.1-beta.2", "description": "Cookie consent with accessible dialog, agnostic tag triggers and conditional content, script and embed hooks.", "keywords": [ "cookie consent", diff --git a/src/config-defaults.mjs b/src/config-defaults.mjs index ed7673c..3e29ada 100644 --- a/src/config-defaults.mjs +++ b/src/config-defaults.mjs @@ -1,20 +1,21 @@ export const DEFAULTS = { - type: 'checkbox', - prefix: 'cookie-consent', + type: "checkbox", + prefix: "cookie-consent", append: true, appendDelay: 500, acceptAllButton: false, labels: { - title: 'Cookies & Privacy', - description: '

This site makes use of third-party cookies. Read more in our privacy policy.

', + title: "Cookies & Privacy", + description: + '

This site makes use of third-party cookies. Read more in our privacy policy.

', button: { - default: 'Save preferences', - acceptAll: 'Accept all', + default: "Save preferences", + acceptAll: "Accept all", }, aria: { - button: 'Confirm cookie settings', - tabList: 'List with cookie types', - tabToggle: 'Toggle cookie tab', + button: "Confirm cookie settings", + tabList: "List with cookie types", + tabToggle: "Toggle cookie tab", }, }, }; diff --git a/src/config.mjs b/src/config.mjs index eb77611..42bf1dc 100644 --- a/src/config.mjs +++ b/src/config.mjs @@ -1,10 +1,10 @@ -import { DEFAULTS } from './config-defaults.mjs'; -import { getEntryByDotString } from './utils.mjs'; +import { DEFAULTS } from "./config-defaults"; +import { getEntryByDotString } from "./utils"; /** * Config getter with defaults fallback and warning when required values are missing. */ -const Config = settings => { +const Config = (settings) => { return { get: (entryString, required = false) => { const value = getEntryByDotString(settings, entryString); @@ -12,7 +12,9 @@ const Config = settings => { console.warn(`Required setting '${entryString}' is missing.`); return undefined; } - return value === undefined ? getEntryByDotString(DEFAULTS, entryString) : value; + return value === undefined + ? getEntryByDotString(DEFAULTS, entryString) + : value; }, }; }; diff --git a/src/cookie-consent.mjs b/src/cookie-consent.mjs deleted file mode 100644 index 2959b73..0000000 --- a/src/cookie-consent.mjs +++ /dev/null @@ -1,65 +0,0 @@ -import Config from './config.mjs'; -import Dialog from './dialog.mjs'; -import DomToggler from './dom-toggler.mjs'; -import EventDispatcher from './event-dispatcher.mjs'; -import Preferences from './preferences.mjs'; - -/** - * Main constructor, which provides the API to the outside. - */ -const CookieConsent = settings => { - - // Show warning when settings are missing. - if (typeof settings !== 'object' || !Object.keys(settings).length) { - console.warn(`No settings specified.`); - } - - // Construct 'classes'. - const config = Config(settings); - const preferences = Preferences(config.get('prefix')); - const dialog = Dialog({ config, preferences }); - const domToggler = DomToggler(config); - const events = EventDispatcher(); - - // Update initial content. - domToggler.toggle(preferences); - - const updatePreference = (cookies) => { - preferences.store(cookies); - events.dispatch('update', preferences.getAll()); - domToggler.toggle(preferences); - }; - - // Initialize dialog and bind `submit` event. - dialog.init(); - dialog.on('submit', updatePreference); - - // Append dialog to the DOM, if this is not explicitly prevented. - if (config.get('append') !== false) { - const appendEl = document.querySelector('main') || document.body.firstElementChild; - const container = appendEl ? appendEl.parentNode : document.body; - container.insertBefore(dialog.element, appendEl); - } - - // Show the dialog when no preferences are found. If found, fire the `update` event. - if (preferences.hasPreferences()) { - events.dispatch('update', preferences.getAll()); - } else { - // Show the dialog. Invoked via a timeout, to ensure it's added in the next cycle - // to cater for possible transitions. - window.setTimeout(() => dialog.show(), config.get('appendDelay')); - } - - return { - getDialog: () => dialog.element, - hideDialog: dialog.hide, - showDialog: dialog.show, - isAccepted: preferences.getState, - getPreferences: preferences.getAll, - on: events.add, - updatePreference, - }; - -}; - -export default CookieConsent; diff --git a/src/dialog-tablist.mjs b/src/dialog-tablist.mjs index 41e1355..27aef31 100644 --- a/src/dialog-tablist.mjs +++ b/src/dialog-tablist.mjs @@ -1,21 +1,25 @@ -import { htmlToElement } from '@grrr/utils'; -import EventDispatcher from './event-dispatcher.mjs'; +import { htmlToElement } from "@grrr/utils"; +import EventDispatcher from "./event-dispatcher"; + +import Config from "./config"; +import Preferences from "./preferences"; /** * Dialog tab list with cookie tabs. */ -const DialogTabList = ({ config, preferences }) => { - +const DialogTabList = (cookieInformation) => { const events = EventDispatcher(); - const TYPE = config.get('type'); - const PREFIX = config.get('prefix'); + const TYPE = Config().get("type"); + const PREFIX = Config().get("prefix"); /** * Render cookie tabs. */ - const renderTab = ({ id, label, description, required, checked, accepted }, index) => { - + const renderTab = ( + { id, label, description, required, checked, accepted }, + index + ) => { /** * Check if the checkbox should be checked: * @@ -24,37 +28,53 @@ const DialogTabList = ({ config, preferences }) => { * `required: false`, because of #3) * 3. Use the `checked` setting. */ - const shouldBeChecked = typeof accepted !== 'undefined' + const shouldBeChecked = typeof accepted !== "undefined" ? accepted - : required === true - ? required + : required === true ? required : checked; return ` -
  • -
    -