From 4ef841d355eea315e0010ee55ee6914d91a6e916 Mon Sep 17 00:00:00 2001 From: Orest Bida Date: Fri, 21 Apr 2023 14:44:29 +0200 Subject: [PATCH] Feat: discard unsaved settings when modal is closed (close #481) --- src/cookieconsent.js | 27 ++++++++++++++++++++++++--- 1 file changed, 24 insertions(+), 3 deletions(-) diff --git a/src/cookieconsent.js b/src/cookieconsent.js index 7d81b8e4..8ec4dc50 100644 --- a/src/cookieconsent.js +++ b/src/cookieconsent.js @@ -907,9 +907,9 @@ settings_buttons.appendChild(settings_accept_all_btn); _addEvent(settings_accept_all_btn, 'click', function(){ + _cookieconsent.accept('all'); _cookieconsent.hideSettings(); _cookieconsent.hide(); - _cookieconsent.accept('all'); }); } @@ -926,9 +926,9 @@ settings_reject_all_btn.className = 'c-bn'; _addEvent(settings_reject_all_btn, 'click', function(){ + _cookieconsent.accept([]); _cookieconsent.hideSettings(); _cookieconsent.hide(); - _cookieconsent.accept([]); }); settings_inner.className = "bns-t"; @@ -948,9 +948,9 @@ // Add save preferences button onClick event // Hide both settings modal and consent modal _addEvent(settings_save_btn, 'click', function(){ + _cookieconsent.accept(); _cookieconsent.hideSettings(); _cookieconsent.hide(); - _cookieconsent.accept(); }); } @@ -1924,6 +1924,8 @@ settings_modal_visible = false; + discardUnsavedToggles(); + setFocus(smFocusSpan); settings_container.setAttribute('aria-hidden', 'true'); @@ -2216,6 +2218,25 @@ el && el.focus(); } + /** + * https://github.com/orestbida/cookieconsent/issues/481 + */ + var discardUnsavedToggles = function() { + + /** + * @type {NodeListOf} + */ + var toggles = settings_inner.querySelectorAll('.c-tgl'); + + for(var i=0; i -1; + + toggles[i].checked = is_readonly || _cookieconsent.allowedCategory(category); + } + + } + return _cookieconsent; };