Skip to content

Commit b5c2dd5

Browse files
authored
Cookie consent - Add "Reject all" button (#497)
* Update `setHasConsent` function to easily reject all groups by adding `status` param * If cookie consent has been rejected, then set cookie for ~6 months As per @rmc47 's suggestion * Add a "Reject additional cookies" button next to the accept button * Compiled JavaScript * Add "Reject all" button to cookie consent dialog * Version bump
1 parent 4cad433 commit b5c2dd5

File tree

6 files changed

+43
-14
lines changed

6 files changed

+43
-14
lines changed

dist/honeycomb.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2723,9 +2723,9 @@ var scrollOnClick = function scrollOnClick() {
27232723
var focus = $this.attr('data-scroll-to-focus') || false;
27242724
var hash = isHashOnThisPage(href);
27252725
if (hash) {
2726-
var _window$jQuery, _window$jQuery$offset;
2726+
var _window$jQuery;
27272727
e.preventDefault();
2728-
var hashTop = (_window$jQuery = window.jQuery(hash)) === null || _window$jQuery === void 0 ? void 0 : (_window$jQuery$offset = _window$jQuery.offset()) === null || _window$jQuery$offset === void 0 ? void 0 : _window$jQuery$offset.top;
2728+
var hashTop = (_window$jQuery = window.jQuery(hash)) === null || _window$jQuery === void 0 || (_window$jQuery = _window$jQuery.offset()) === null || _window$jQuery === void 0 ? void 0 : _window$jQuery.top;
27292729
if (typeof hashTop === 'undefined') {
27302730
window.console.warn("Honeycomb: Element with ID \"".concat(hash, "\" not found, so can't scroll to it."));
27312731
return;
@@ -3564,8 +3564,9 @@ var addInlineVideos = function addInlineVideos() {
35643564
// If video is already loaded, skip.
35653565
// NB we look for the iframe for the specific video, because in React we may be reusing an old VideoPlayer component.
35663566
if (videoContainer.querySelector("iframe[id^=\"".concat(videoId, "\"]"))) {
3567-
return "continue";
3567+
return 1; // continue
35683568
}
3569+
35693570
var duration;
35703571
var currentTime;
35713572
var percentages;
@@ -3679,8 +3680,7 @@ var addInlineVideos = function addInlineVideos() {
36793680
videoCounter++;
36803681
};
36813682
for (_iterator2.s(); !(_step2 = _iterator2.n()).done;) {
3682-
var _ret = _loop();
3683-
if (_ret === "continue") continue;
3683+
if (_loop()) continue;
36843684
}
36853685
} catch (err) {
36863686
_iterator2.e(err);

dist/honeycomb.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "honeycomb-web-toolkit",
3-
"version": "14.1.15",
3+
"version": "14.1.16",
44
"repository": {
55
"type": "git",
66
"url": "https://github.com/red-gate/honeycomb-web-toolkit"

src/cookie-consent/js/honeycomb.cookie-consent.dialog.js

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,14 +51,28 @@ const open = ( consentGroups = null, setHasConsent = null ) => {
5151

5252
// Allow all button.
5353
const acceptAll = document.createElement('button');
54-
acceptAll.setAttribute('class', 'button button--primary button--small spaced-bottom--tight');
54+
acceptAll.setAttribute('class', 'button button--primary button--small spaced-bottom--tight spaced-right--tight');
5555
acceptAll.innerHTML = 'Accept all';
5656
acceptAll.addEventListener('click', e => {
5757
e.preventDefault();
5858
setHasConsent(null);
5959
});
6060
inner.appendChild(acceptAll);
6161

62+
// Reject all button.
63+
const rejectAll = document.createElement('button');
64+
rejectAll.setAttribute('class', 'button button--small spaced-bottom--tight');
65+
rejectAll.innerHTML = 'Reject all';
66+
rejectAll.addEventListener('click', e => {
67+
e.preventDefault();
68+
const rejections = {};
69+
for (let consentGroup in consentGroups) {
70+
rejections[consentGroup] = 0;
71+
}
72+
setHasConsent(rejections);
73+
});
74+
inner.appendChild(rejectAll);
75+
6276
// Group heading.
6377
const groupHeading = document.createElement('h2');
6478
groupHeading.setAttribute('class', 'gamma text--redgate');

src/cookie-consent/js/honeycomb.cookie-consent.js

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -224,23 +224,25 @@ const hasConsent = ( group = null ) => {
224224

225225
/**
226226
* Set the consent cookie with the groups consent statuses, and for it to
227-
* expire in 31 days.
227+
* expire in 31 days if cookies accepted, and 6 months if rejected.
228228
*
229229
* @param {Object|Null} groups The groups object with the group as the property,
230230
* and the consent status as the value (0|1)
231+
* @param {Boolean} status The status to set consent to if groups is null.
232+
* Defaults to true (accepted)
231233
*/
232-
const setHasConsent = ( groups = null ) => {
234+
const setHasConsent = ( groups = null, status = true ) => {
233235

234236
// If no groups info is passed in, then set all groups to have consent.
235237
if ( groups === null ) {
236238
groups = {};
237239
getConsentGroups().forEach(group => {
238-
groups[group] = 1;
240+
groups[group] = status === true ? 1 : 0;
239241
});
240242
}
241243

242244
cookie.set(getConsentCookieName(), JSON.stringify(groups), {
243-
'max-age': 2678400,
245+
'max-age': (status === 1) ? 2678400 : 16070400,
244246
domain: getConsentCookieDomain(),
245247
});
246248
};
@@ -322,6 +324,19 @@ const displayNotification = () => {
322324
acceptButtonListItem.appendChild(acceptButton);
323325
list.appendChild(acceptButtonListItem);
324326

327+
// Reject button.
328+
const rejectButtonListItem = document.createElement('li');
329+
rejectButtonListItem.className = 'spaced-right--tight';
330+
const rejectButton = document.createElement('button');
331+
rejectButton.className = 'button button--small';
332+
rejectButton.innerHTML = 'Reject additional cookies';
333+
rejectButton.addEventListener('click', () => {
334+
setHasConsent(null, false);
335+
hideNotification();
336+
});
337+
rejectButtonListItem.appendChild(rejectButton);
338+
list.appendChild(rejectButtonListItem);
339+
325340
// Customise link.
326341
const customiseLinkItem = document.createElement('li');
327342
customiseLinkItem.className = 'spaced-right--tight';

0 commit comments

Comments
 (0)