2023-04-24 18:24:58 -04:00
|
|
|
/**
|
|
|
|
* Create a new element with the given attrs and children.
|
|
|
|
* Children can be a string for text nodes or other elements.
|
|
|
|
* @param {String} tagName
|
|
|
|
* @param {Object<String, String>} attrs
|
|
|
|
* @param {Element[]|String[]}children
|
|
|
|
* @return {*}
|
|
|
|
*/
|
|
|
|
export function elem(tagName, attrs = {}, children = []) {
|
|
|
|
const el = document.createElement(tagName);
|
|
|
|
|
|
|
|
for (const [key, val] of Object.entries(attrs)) {
|
2023-05-22 09:19:18 -04:00
|
|
|
if (val === null) {
|
|
|
|
el.removeAttribute(key);
|
|
|
|
} else {
|
|
|
|
el.setAttribute(key, val);
|
|
|
|
}
|
2023-04-24 18:24:58 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
for (const child of children) {
|
|
|
|
if (typeof child === 'string') {
|
|
|
|
el.append(document.createTextNode(child));
|
|
|
|
} else {
|
|
|
|
el.append(child);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return el;
|
|
|
|
}
|
|
|
|
|
2019-06-07 12:46:19 -04:00
|
|
|
/**
|
|
|
|
* Run the given callback against each element that matches the given selector.
|
|
|
|
* @param {String} selector
|
|
|
|
* @param {Function<Element>} callback
|
|
|
|
*/
|
|
|
|
export function forEach(selector, callback) {
|
|
|
|
const elements = document.querySelectorAll(selector);
|
2023-04-18 17:20:02 -04:00
|
|
|
for (const element of elements) {
|
2019-06-07 12:46:19 -04:00
|
|
|
callback(element);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Helper to listen to multiple DOM events
|
|
|
|
* @param {Element} listenerElement
|
|
|
|
* @param {Array<String>} events
|
|
|
|
* @param {Function<Event>} callback
|
|
|
|
*/
|
|
|
|
export function onEvents(listenerElement, events, callback) {
|
2023-04-18 17:20:02 -04:00
|
|
|
for (const eventName of events) {
|
2019-06-07 12:46:19 -04:00
|
|
|
listenerElement.addEventListener(eventName, callback);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-24 13:26:28 -04:00
|
|
|
/**
|
|
|
|
* Helper to run an action when an element is selected.
|
|
|
|
* A "select" is made to be accessible, So can be a click, space-press or enter-press.
|
2020-06-27 18:56:01 -04:00
|
|
|
* @param {HTMLElement|Array} elements
|
|
|
|
* @param {function} callback
|
2019-08-24 13:26:28 -04:00
|
|
|
*/
|
2020-06-27 18:56:01 -04:00
|
|
|
export function onSelect(elements, callback) {
|
|
|
|
if (!Array.isArray(elements)) {
|
|
|
|
elements = [elements];
|
|
|
|
}
|
|
|
|
|
|
|
|
for (const listenerElement of elements) {
|
|
|
|
listenerElement.addEventListener('click', callback);
|
2023-04-18 17:20:02 -04:00
|
|
|
listenerElement.addEventListener('keydown', event => {
|
2020-06-27 18:56:01 -04:00
|
|
|
if (event.key === 'Enter' || event.key === ' ') {
|
|
|
|
event.preventDefault();
|
|
|
|
callback(event);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2023-05-31 11:38:20 -04:00
|
|
|
* Listen to key press on the given element(s).
|
|
|
|
* @param {String} key
|
2020-06-27 18:56:01 -04:00
|
|
|
* @param {HTMLElement|Array} elements
|
|
|
|
* @param {function} callback
|
|
|
|
*/
|
2023-05-31 11:38:20 -04:00
|
|
|
function onKeyPress(key, elements, callback) {
|
2020-06-27 18:56:01 -04:00
|
|
|
if (!Array.isArray(elements)) {
|
|
|
|
elements = [elements];
|
|
|
|
}
|
2020-07-04 11:53:02 -04:00
|
|
|
|
|
|
|
const listener = event => {
|
2023-05-31 11:38:20 -04:00
|
|
|
if (event.key === key) {
|
2020-07-04 11:53:02 -04:00
|
|
|
callback(event);
|
|
|
|
}
|
2023-04-18 17:20:02 -04:00
|
|
|
};
|
2020-07-04 11:53:02 -04:00
|
|
|
|
2023-05-31 11:38:20 -04:00
|
|
|
elements.forEach(e => e.addEventListener('keydown', listener));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Listen to enter press on the given element(s).
|
|
|
|
* @param {HTMLElement|Array} elements
|
|
|
|
* @param {function} callback
|
|
|
|
*/
|
|
|
|
export function onEnterPress(elements, callback) {
|
|
|
|
onKeyPress('Enter', elements, callback);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Listen to escape press on the given element(s).
|
|
|
|
* @param {HTMLElement|Array} elements
|
|
|
|
* @param {function} callback
|
|
|
|
*/
|
|
|
|
export function onEscapePress(elements, callback) {
|
|
|
|
onKeyPress('Escape', elements, callback);
|
2019-08-24 13:26:28 -04:00
|
|
|
}
|
|
|
|
|
2019-06-07 12:46:19 -04:00
|
|
|
/**
|
|
|
|
* Set a listener on an element for an event emitted by a child
|
|
|
|
* matching the given childSelector param.
|
|
|
|
* Used in a similar fashion to jQuery's $('listener').on('eventName', 'childSelector', callback)
|
|
|
|
* @param {Element} listenerElement
|
|
|
|
* @param {String} childSelector
|
|
|
|
* @param {String} eventName
|
|
|
|
* @param {Function} callback
|
|
|
|
*/
|
|
|
|
export function onChildEvent(listenerElement, childSelector, eventName, callback) {
|
2023-04-18 17:20:02 -04:00
|
|
|
listenerElement.addEventListener(eventName, event => {
|
2019-06-07 12:46:19 -04:00
|
|
|
const matchingChild = event.target.closest(childSelector);
|
|
|
|
if (matchingChild) {
|
|
|
|
callback.call(matchingChild, event, matchingChild);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Look for elements that match the given selector and contain the given text.
|
|
|
|
* Is case insensitive and returns the first result or null if nothing is found.
|
|
|
|
* @param {String} selector
|
|
|
|
* @param {String} text
|
|
|
|
* @returns {Element}
|
|
|
|
*/
|
|
|
|
export function findText(selector, text) {
|
|
|
|
const elements = document.querySelectorAll(selector);
|
|
|
|
text = text.toLowerCase();
|
2023-04-18 17:20:02 -04:00
|
|
|
for (const element of elements) {
|
2019-06-07 12:46:19 -04:00
|
|
|
if (element.textContent.toLowerCase().includes(text)) {
|
|
|
|
return element;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return null;
|
2020-07-04 11:53:02 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Show a loading indicator in the given element.
|
|
|
|
* This will effectively clear the element.
|
|
|
|
* @param {Element} element
|
|
|
|
*/
|
|
|
|
export function showLoading(element) {
|
2023-04-18 17:20:02 -04:00
|
|
|
element.innerHTML = '<div class="loading-container"><div></div><div></div><div></div></div>';
|
2020-07-24 19:20:58 -04:00
|
|
|
}
|
|
|
|
|
2023-04-24 18:24:58 -04:00
|
|
|
/**
|
|
|
|
* Get a loading element indicator element.
|
|
|
|
* @returns {Element}
|
|
|
|
*/
|
|
|
|
export function getLoading() {
|
|
|
|
const wrap = document.createElement('div');
|
|
|
|
wrap.classList.add('loading-container');
|
|
|
|
wrap.innerHTML = '<div></div><div></div><div></div>';
|
|
|
|
return wrap;
|
|
|
|
}
|
|
|
|
|
2020-07-24 19:20:58 -04:00
|
|
|
/**
|
|
|
|
* Remove any loading indicators within the given element.
|
|
|
|
* @param {Element} element
|
|
|
|
*/
|
|
|
|
export function removeLoading(element) {
|
|
|
|
const loadingEls = element.querySelectorAll('.loading-container');
|
|
|
|
for (const el of loadingEls) {
|
|
|
|
el.remove();
|
|
|
|
}
|
2022-10-21 05:13:11 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Convert the given html data into a live DOM element.
|
|
|
|
* Initiates any components defined in the data.
|
|
|
|
* @param {String} html
|
|
|
|
* @returns {Element}
|
|
|
|
*/
|
|
|
|
export function htmlToDom(html) {
|
|
|
|
const wrap = document.createElement('div');
|
|
|
|
wrap.innerHTML = html;
|
2022-11-14 18:19:02 -05:00
|
|
|
window.$components.init(wrap);
|
2022-10-21 05:13:11 -04:00
|
|
|
return wrap.children[0];
|
2023-04-18 17:20:02 -04:00
|
|
|
}
|