fcb9ef8788
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). cc: @silverwind
51 lines
1.3 KiB
JavaScript
51 lines
1.3 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);
|
|
}
|
|
|
|
export function onDomReady(cb) {
|
|
if (document.readyState === 'loading') {
|
|
document.addEventListener('DOMContentLoaded', cb);
|
|
} else {
|
|
cb();
|
|
}
|
|
}
|