Make classNames used by classesPersister private

Both the remove and replace methods rely on it
having parity with the $els array property so it
is a good idea to stop it, and $els, being
changable by other code. As noted in:

https://github.com/alphagov/notifications-admin/pull/4155#discussion_r804639058
This commit is contained in:
Tom Byers
2022-02-11 15:40:47 +00:00
parent 4f59396a00
commit 9646e17b03

View File

@@ -14,19 +14,22 @@
// Methods to ensure the DOM fragment is clean of classes added by JS before diffing
// and that they are replaced afterwards.
var classesPersister = {
classNames: [],
$els: [],
_classNames: [],
_$els: [],
addClassName: function (className) {
this._classNames.push(className);
},
remove: function () {
this.classNames.forEach(className => {
this._classNames.forEach(className => {
var $elsWithClassName = $('.' + className).removeClass(className);
// store elements for that className at the same index
this.$els.push($elsWithClassName);
this._$els.push($elsWithClassName);
});
},
replace: function () {
this.classNames.forEach((className, index) => {
var $el = this.$els[index];
this._classNames.forEach((className, index) => {
var $el = this._$els[index];
if (global.document.body.contains($el.get(0))) {
$el.addClass(className);
@@ -101,7 +104,7 @@
if ($contents.data('classesToPersist') !== undefined) {
$contents.data('classesToPersist')
.split(' ')
.forEach(className => classesPersister.classNames.push(className));
.forEach(className => classesPersister.addClassName(className));
}
setTimeout(