Skip to content

Commit

Permalink
Near complete rewrite of EU Cookie Compliance JS for reliability:
Browse files Browse the repository at this point in the history
- Now detaches and re-attaches without issues.

- Correctly hides the overlay if open when detached so that scroll
  blocking and ally.maintain.disabled are disengaged.

- Reworked into JavaScript classes and event subscribers.

- Focus locking now works more reliably in most cases; still shows when
  the pop-up is auto shown by the module's JavaScript, but that can be
  fixed later with a rework of focus hiding.

See: neurocracy/omnipedia#46
  • Loading branch information
Ambient-Impact committed Jan 17, 2024
1 parent aa4f89e commit 2823154
Show file tree
Hide file tree
Showing 7 changed files with 762 additions and 540 deletions.
59 changes: 25 additions & 34 deletions javascript/eu_cookie_compliance.decide_later.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// -----------------------------------------------------------------------------
// Omnipedia - Site theme - EU cookie compliance decide later button
// Omnipedia - Site theme - EU Cookie Compliance decide later button component
// -----------------------------------------------------------------------------

// This adds an event handler to close the pop-up when clicking the "Decide
Expand All @@ -9,12 +9,12 @@ AmbientImpact.onGlobals([
'Drupal.eu_cookie_compliance',
], function() {
AmbientImpact.on([
'OmnipediaSiteThemeEuCookieComplianceElements',
], function(euCookieComplianceElements) {
'OmnipediaSiteThemeEuCookieCompliance',
], function(euCookieCompliance) {
AmbientImpact.addComponent(
'OmnipediaSiteThemeEuCookieComplianceDecideLater',
function(
euCookieComplianceDecideLater, $
euCookieComplianceDecideLater, $,
) {

'use strict';
Expand All @@ -36,44 +36,35 @@ function(
this.addBehaviour(
'OmnipediaSiteThemeEuCookieComplianceDecideLater',
'omnipedia-site-theme-eu-cookie-compliance-decide-later',
euCookieComplianceElements.getBehaviourSelector(),
'#sliding-popup',
function(context, settings) {

/**
* The cookie compliance pop-up, if any, wrapped in a jQuery collection.
*
* @type {jQuery}
*/
let $popUp = euCookieComplianceElements.getPopUp();
$(this).on(
`PrivacyPopupConstructed.${eventNamespace}`,
function(event, popup) {

// Bail if we can't find the pop-up.
if ($popUp.length === 0) {
return;
}
popup.$popup.find(`.${decideLaterButtonClass}`).on(
`click.${eventNamespace}`,
Drupal.eu_cookie_compliance.toggleWithdrawBanner,
);

// Event handler to close the pop-up when the "Decide later" button is
// clicked.
$popUp.find('.' + decideLaterButtonClass).on(
'click.' + eventNamespace,
Drupal.eu_cookie_compliance.toggleWithdrawBanner
);
// Attach binds a one-off handler to the PrivacyPopupDestroyed event which
// removes the click event handler.
}).one(`PrivacyPopupDestroyed.${eventNamespace}`, function(event, popup) {

},
function(context, settings, trigger) {
popup.$popup.find(`.${decideLaterButtonClass}`).off(
`click.${eventNamespace}`,
Drupal.eu_cookie_compliance.toggleWithdrawBanner,
);

/**
* The cookie compliance pop-up, if any, wrapped in a jQuery collection.
*
* @type {jQuery}
*/
let $popUp = euCookieComplianceElements.getPopUp();
});

// Bail if we can't find the pop-up.
if ($popUp.length === 0) {
return;
}
},
function(context, settings, trigger) {

$popUp.find('.' + decideLaterButtonClass).off('click.' + eventNamespace);
// Remove just the constructed event as the destroyed event needs to run
// and will be invoked and then removed as it's a one-off event.
$(this).off(`PrivacyPopupConstructed.${eventNamespace}`);

}
);
Expand Down
126 changes: 0 additions & 126 deletions javascript/eu_cookie_compliance.elements.js

This file was deleted.

Loading

0 comments on commit 2823154

Please sign in to comment.