Skip to content

Commit

Permalink
Feat: discard unsaved settings when modal is closed (close #481)
Browse files Browse the repository at this point in the history
  • Loading branch information
orestbida committed Apr 21, 2023
1 parent 6a60ac3 commit 4ef841d
Showing 1 changed file with 24 additions and 3 deletions.
27 changes: 24 additions & 3 deletions src/cookieconsent.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
});
}

Expand 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";
Expand All @@ -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();
});
}

Expand Down Expand Up @@ -1924,6 +1924,8 @@

settings_modal_visible = false;

discardUnsavedToggles();

setFocus(smFocusSpan);

settings_container.setAttribute('aria-hidden', 'true');
Expand Down Expand Up @@ -2216,6 +2218,25 @@
el && el.focus();
}

/**
* https://github.com/orestbida/cookieconsent/issues/481
*/
var discardUnsavedToggles = function() {

/**
* @type {NodeListOf<HTMLInputElement>}
*/
var toggles = settings_inner.querySelectorAll('.c-tgl');

for(var i=0; i<toggles.length; i++) {
var category = toggles[i].value;
var is_readonly = readonly_categories.indexOf(category) > -1;

toggles[i].checked = is_readonly || _cookieconsent.allowedCategory(category);
}

}

return _cookieconsent;
};

Expand Down

0 comments on commit 4ef841d

Please sign in to comment.