Skip to content

Commit

Permalink
Format code
Browse files Browse the repository at this point in the history
  • Loading branch information
CountNick committed Jun 19, 2024
1 parent 78f1d5b commit 55931b7
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 60 deletions.
12 changes: 6 additions & 6 deletions src/cookie-consent.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,8 @@ export default class Dialog extends HTMLElement {
saveButtonText: Config().get("labels.aria.button"),
defaultButtonLabel: Config().get("labels.button.default"),
acceptAllButton:
Config().get("acceptAllButton") && !Preferences().hasPreferences(),
Config().get("acceptAllButton")
&& !Preferences().hasPreferences(),
};
// custom content from data-attributes
const customContent = {
Expand Down Expand Up @@ -185,9 +186,9 @@ export default class Dialog extends HTMLElement {
const checkedCount = values.filter((v) => v.accepted).length;
const userOptionsChecked = checkedCount >= requiredCount;
if (
this.data.acceptAllButton &&
this.config.type === "checkbox" &&
!userOptionsChecked
this.data.acceptAllButton
&& this.config.type === "checkbox"
&& !userOptionsChecked
) {
return values.map((value) => ({
...value,
Expand Down Expand Up @@ -250,8 +251,7 @@ export default class Dialog extends HTMLElement {
// Loop through arrayfiedTabListChildren
tabListChildren.forEach((input) => {
// Find all input elements
const inputElement =
input.firstElementChild.firstElementChild.firstElementChild;
const inputElement = input.firstElementChild.firstElementChild.firstElementChild;
// Loop through updated cookies
this.cookies.forEach((cookie) => {
// set the checked state to the updated cookie state
Expand Down
17 changes: 6 additions & 11 deletions src/dialog-tablist.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,9 @@ const DialogTabList = (cookieInformation) => {
* `required: false`, because of #3)
* 3. Use the `checked` setting.
*/
const shouldBeChecked =
typeof accepted !== "undefined"
? accepted
: required === true
const shouldBeChecked = typeof accepted !== "undefined"
? accepted
: required === true
? required
: checked;

Expand Down Expand Up @@ -96,9 +95,7 @@ const DialogTabList = (cookieInformation) => {
: undefined,
}));
return `
<ul part="${PREFIX}__tab-list" class="${PREFIX}__tab-list" role="tablist" aria-label="${Config().get(
"labels.aria.tabList"
)}">
<ul part="${PREFIX}__tab-list" class="${PREFIX}__tab-list" role="tablist" aria-label="${Config().get("labels.aria.tabList")}">
${cookiesWithState.map(renderTab).join("")}
</ul>
`;
Expand Down Expand Up @@ -127,8 +124,7 @@ const DialogTabList = (cookieInformation) => {
const controls = targetTab ? targetTab.getAttribute("aria-controls") : "";
tabs.forEach((tab) => tab.setAttribute("aria-selected", tab === targetTab));
panels.forEach((panel) =>
panel.setAttribute("aria-hidden", controls !== panel.id)
);
panel.setAttribute("aria-hidden", controls !== panel.id));
};

/**
Expand All @@ -140,8 +136,7 @@ const DialogTabList = (cookieInformation) => {
tabs.forEach((tab) => {
tab.addEventListener("click", (e) => {
e.preventDefault();
const targetTab =
tab.getAttribute("aria-selected") === "true" ? null : tab;
const targetTab = tab.getAttribute("aria-selected") === "true" ? null : tab;
selectTab({ tabs, panels, targetTab });
});
});
Expand Down
77 changes: 42 additions & 35 deletions src/dom-toggler.mjs
Original file line number Diff line number Diff line change
@@ -1,18 +1,17 @@
const GENERAL_ATTRIBUTE = 'data-cookie-consent';
const ACCEPTED_STATE_ATTRIBUTE = 'data-cookie-consent-accepted';
const REJECTED_STATE_ATTRIBUTE = 'data-cookie-consent-rejected';
const GENERAL_ATTRIBUTE = "data-cookie-consent";
const ACCEPTED_STATE_ATTRIBUTE = "data-cookie-consent-accepted";
const REJECTED_STATE_ATTRIBUTE = "data-cookie-consent-rejected";

/**
* DOM toggler, which enables conditional script tags or embedded content.
*/
const DomToggler = config => {

const DomToggler = (config) => {
/**
* Append a single script.
* @TODO append it to the same location and re-add classes and attributes.
*/
const appendScript = script => {
const newScript = document.createElement('script');
const appendScript = (script) => {
const newScript = document.createElement("script");
if (script.src) {
newScript.src = script.src;
} else {
Expand All @@ -29,8 +28,10 @@ const DomToggler = config => {
if (!accepted) {
return;
}
const scripts = document.documentElement.querySelectorAll(`script[${GENERAL_ATTRIBUTE}="${id}"]`);
[...scripts].forEach(script => {
const scripts = document.documentElement.querySelectorAll(
`script[${GENERAL_ATTRIBUTE}="${id}"]`
);
[...scripts].forEach((script) => {
appendScript(script);
script.parentNode.removeChild(script);
});
Expand All @@ -39,72 +40,78 @@ const DomToggler = config => {
/**
* Show a single iframe.
*/
const showIframe = iframe => {
const src = iframe.getAttribute('data-src');
const showIframe = (iframe) => {
const src = iframe.getAttribute("data-src");
if (src) {
iframe.setAttribute('src', src);
iframe.removeAttribute('data-src');
iframe.setAttribute("src", src);
iframe.removeAttribute("data-src");
}
};

/**
* Hide a single iframe.
*/
const hideIframe = iframe => {
const src = iframe.getAttribute('src');
const hideIframe = (iframe) => {
const src = iframe.getAttribute("src");
if (src) {
iframe.setAttribute('data-src', src);
iframe.removeAttribute('src');
iframe.setAttribute("data-src", src);
iframe.removeAttribute("src");
}
};

/**
* Show conditional iframes based on wanted state.
*/
const toggleConditionalIframes = ({ id, accepted }) => {
const iframes = document.body.querySelectorAll(`iframe[${GENERAL_ATTRIBUTE}="${id}"]`);
[...iframes].forEach(el => accepted ? showIframe(el) : hideIframe(el));
const iframes = document.body.querySelectorAll(
`iframe[${GENERAL_ATTRIBUTE}="${id}"]`
);
[...iframes].forEach((el) => (accepted ? showIframe(el) : hideIframe(el)));
};

/**
* Show a single element.
*/
const showElement = el => {
el.style.display = '';
el.removeAttribute('aria-hidden');
el.removeAttribute('hidden');
const showElement = (el) => {
el.style.display = "";
el.removeAttribute("aria-hidden");
el.removeAttribute("hidden");
};

/**
* Hide a single element.
*/
const hideElement = el => {
el.style.display = 'none';
el.setAttribute('aria-hidden', 'true');
const hideElement = (el) => {
el.style.display = "none";
el.setAttribute("aria-hidden", "true");
};

/**
* Show conditional elements based on wanted state.
*/
const toggleConditionalElements = ({ id, accepted }) => {
const accepts = document.body.querySelectorAll(`[${ACCEPTED_STATE_ATTRIBUTE}="${id}"]`);
const rejects = document.body.querySelectorAll(`[${REJECTED_STATE_ATTRIBUTE}="${id}"]`);
[...accepts].forEach(el => accepted ? showElement(el) : hideElement(el));
[...rejects].forEach(el => accepted ? hideElement(el) : showElement(el));
const accepts = document.body.querySelectorAll(
`[${ACCEPTED_STATE_ATTRIBUTE}="${id}"]`
);
const rejects = document.body.querySelectorAll(
`[${REJECTED_STATE_ATTRIBUTE}="${id}"]`
);
[...accepts].forEach((el) =>
accepted ? showElement(el) : hideElement(el));
[...rejects].forEach((el) =>
accepted ? hideElement(el) : showElement(el));
};

return {
toggle: preferences => {
const cookies = config.get('cookies') || [];
cookies.forEach(type => {
toggle: (preferences) => {
const cookies = config.get("cookies") || [];
cookies.forEach((type) => {
const accepted = preferences.getState(type.id);
toggleScripts({ id: type.id, accepted });
toggleConditionalElements({ id: type.id, accepted });
toggleConditionalIframes({ id: type.id, accepted });
});
},
};

};

export default DomToggler;
16 changes: 8 additions & 8 deletions src/storage.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@ const Storage = () => {
const storageModule = supportsLocalStorage()
? window.localStorage
: {
attributes: {},
setItem(key, val) {
this.attributes[key] = val;
},
getItem(key) {
return this.attributes[key];
},
};
attributes: {},
setItem(key, val) {
this.attributes[key] = val;
},
getItem(key) {
return this.attributes[key];
},
};

const get = (key, value) => storageModule.getItem(key, value);
const set = (key, value) => storageModule.setItem(key, value);
Expand Down

0 comments on commit 55931b7

Please sign in to comment.