mirror of
https://github.com/bookwyrm-social/bookwyrm.git
synced 2024-11-25 19:11:09 +00:00
[assets] Simplify addRemoveClass function.
This commit is contained in:
parent
70c652d565
commit
7e49b3cb26
1 changed files with 13 additions and 33 deletions
|
@ -81,9 +81,7 @@ let BookWyrm = new class {
|
|||
revealForm(e) {
|
||||
let hidden = e.currentTarget.closest('.hidden-form').getElementsByClassName('hidden')[0];
|
||||
|
||||
if (hidden) {
|
||||
this.removeClass(hidden, 'hidden');
|
||||
}
|
||||
this.addRemoveClass(hidden, 'hidden', !hidden);
|
||||
}
|
||||
|
||||
toggleAction(e) {
|
||||
|
@ -162,37 +160,19 @@ let BookWyrm = new class {
|
|||
});
|
||||
}
|
||||
|
||||
addRemoveClass(el, classname, bool) {
|
||||
if (bool) {
|
||||
this.addClass(el, classname);
|
||||
/**
|
||||
* Add or remove a class based on a boolean condition.
|
||||
*
|
||||
* @param {object} node - DOM node to change class on
|
||||
* @param {string} classname - Name of the class
|
||||
* @param {boolean} add - Add?
|
||||
* @return {undefined}
|
||||
*/
|
||||
addRemoveClass(node, classname, add) {
|
||||
if (add) {
|
||||
node.classList.add(classname);
|
||||
} else {
|
||||
this.removeClass(el, classname);
|
||||
node.classList.remove(classname);
|
||||
}
|
||||
}
|
||||
|
||||
addClass(el, classname) {
|
||||
let classes = el.className.split(' ');
|
||||
|
||||
if (classes.indexOf(classname) > -1) {
|
||||
return;
|
||||
}
|
||||
|
||||
el.className = classes.concat(classname).join(' ');
|
||||
}
|
||||
|
||||
removeClass(el, className) {
|
||||
let classes = [];
|
||||
|
||||
if (el.className) {
|
||||
classes = el.className.split(' ');
|
||||
}
|
||||
|
||||
const idx = classes.indexOf(className);
|
||||
|
||||
if (idx > -1) {
|
||||
classes.splice(idx, 1);
|
||||
}
|
||||
|
||||
el.className = classes.join(' ');
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue