forgejo/web_src/js/utils/dom.js
Giteabot 026167f854
Remove assertion debug code for show/hide refactoring (#23576) (#23868)
Backport #23576 by @wxiaoguang

When doing the refactoring:

* #22950

I added some debug mode code (assertShown) to help to catch bugs, it did
catch some bugs like:

* #23074

If it has been proved that there is no more bugs, this assertion could
be removed easily and clearly.

Feel free to decide when to remove it (feel free to convert it from
Draft to Ready for Review).
2023-04-02 16:20:06 +08:00

44 lines
1.1 KiB
JavaScript

function elementsCall(el, func, ...args) {
if (typeof el === 'string' || el instanceof String) {
el = document.querySelectorAll(el);
}
if (el instanceof Node) {
func(el, ...args);
} else if (el.length !== undefined) {
// this works for: NodeList, HTMLCollection, Array, jQuery
for (const e of el) {
func(e, ...args);
}
} else {
throw new Error('invalid argument to be shown/hidden');
}
}
/**
* @param el string (selector), Node, NodeList, HTMLCollection, Array or jQuery
* @param force force=true to show or force=false to hide, undefined to toggle
*/
function toggleShown(el, force) {
if (force === true) {
el.classList.remove('gt-hidden');
} else if (force === false) {
el.classList.add('gt-hidden');
} else if (force === undefined) {
el.classList.toggle('gt-hidden');
} else {
throw new Error('invalid force argument');
}
}
export function showElem(el) {
elementsCall(el, toggleShown, true);
}
export function hideElem(el) {
elementsCall(el, toggleShown, false);
}
export function toggleElem(el, force) {
elementsCall(el, toggleShown, force);
}