Skip to content
This repository was archived by the owner on May 5, 2021. It is now read-only.

Commit 29b46ef

Browse files
Zyntondmo-odoo
authored andcommitted
[ADD] DomHelpers: set class helper
1 parent 0c260b4 commit 29b46ef

File tree

1 file changed

+34
-0
lines changed

1 file changed

+34
-0
lines changed

packages/plugin-dom-helpers/src/DomHelpers.ts

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,40 @@ export class DomHelpers<T extends JWPluginConfig = JWPluginConfig> extends JWPlu
114114
};
115115
return context.execCommand(domHelpersToggleClass);
116116
}
117+
/**
118+
* Add (state: true) or remove (state: false) a class or a list of classes
119+
* from a DOM node or a list of DOM nodes.
120+
*
121+
* @param context
122+
* @param originalDomNode
123+
* @param className
124+
* @param state
125+
*/
126+
async setClass(
127+
context: ExecutionContext,
128+
originalDomNode: Node | Node[],
129+
className: string,
130+
state: boolean,
131+
): Promise<ExecCommandResult> {
132+
const domHelpersSetClass = async (): Promise<void> => {
133+
const classes = Array.isArray(className) ? className : [className];
134+
const domNodes = Array.isArray(originalDomNode) ? originalDomNode : [originalDomNode];
135+
for (const domNode of domNodes) {
136+
const Attributes = this._getAttributesConstructor(domNode);
137+
for (const node of this.getNodes(domNode)) {
138+
const classList = node.modifiers.find(Attributes)?.classList;
139+
for (const oneClass of classes) {
140+
if (state && !classList?.has(oneClass)) {
141+
node.modifiers.get(Attributes).classList.add(oneClass);
142+
} else if (!state && classList?.has(oneClass)) {
143+
classList.remove(oneClass);
144+
}
145+
}
146+
}
147+
}
148+
};
149+
return context.execCommand(domHelpersSetClass);
150+
}
117151
/**
118152
* Set an attribute on a DOM node or a list of DOM nodes.
119153
*

0 commit comments

Comments
 (0)