Skip to content

Commit

Permalink
Merge pull request #356 from localgovdrupal/feature/1.8.x/352-replace…
Browse files Browse the repository at this point in the history
…-js-cookie

Feature: Replace js cookie
  • Loading branch information
andybroomfield authored Aug 5, 2024
2 parents 45659ca + fb651c3 commit f4eea8b
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 78 deletions.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,16 +1,21 @@
/**
* DO NOT EDIT THIS FILE.
* See the following change record for more information,
* https://www.drupal.org/node/2815083
* @preserve
**/
* @file
* Present a full page alert banner as a dialog.
*
* The alert banner is added by the localgov_alert_banner module.
*
* @see localgov_alert_banner_preprocess_localgov_alert_banner__full()
* @see localgov-alert-banner--full.html.twig
*/

(function launchModalAlertBanner(Drupal, drupalSettings, cookieMonster) {
(function launchModalAlertBanner(Drupal, drupalSettings, cookieStore) {
Drupal.behaviors.launchModalAlertBanner = {
attach: function attach() {
var alertId = drupalSettings.localgov_alert_banner_full_page.localgov_full_page_alert_banner_id;
var lgAlert = document.getElementById(alertId);
const alertId =
drupalSettings.localgov_alert_banner_full_page
.localgov_full_page_alert_banner_id;

const lgAlert = document.getElementById(alertId);
if (lgAlert === null) {
return;
}
Expand All @@ -23,18 +28,36 @@
window.dialogPolyfill.registerDialog(lgAlert);
}

var cancelButton = document.getElementById("".concat(alertId, "-canceloverlay"));
const cancelButton = document.getElementById(`${alertId}-canceloverlay`);

cancelButton.addEventListener("click", function closeAlert() {
lgAlert.close();
});

lgAlert.showModal();
},
isHiddenAlert: function isHiddenAlert(lgAlert) {
var cookie = cookieMonster.get("hide-alert-banner-token");
var cookieTokens = typeof cookie !== "undefined" ? cookie.split("+") : [];
var dismissToken = lgAlert.getAttribute("data-dismiss-alert-token");
var isHidden = cookieTokens.includes(dismissToken);

/**
* Is this a hidden alert?
*
* @param {object} lgAlert
* DOM object.
*
* @return {bool}
* Is the given alert hidden?
*
* @see localgov_alert_banner/js/alert_banner.js
*/
isHiddenAlert(lgAlert) {
const dismissToken = lgAlert.getAttribute("data-dismiss-alert-token");
const isHidden = cookieStore
.split(";")
.some(
(item) =>
item.trim().startsWith("hide-alert-banner-token=") &&
item.includes(dismissToken),
);
return isHidden;
}
},
};
})(Drupal, drupalSettings, window.Cookies);
})(Drupal, drupalSettings, document.cookie);
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,3 @@ full_page_alert_banner:
dependencies:
- core/drupalSettings
- localgov_alert_banner/alert_banner
- core/js-cookie

0 comments on commit f4eea8b

Please sign in to comment.