2 * Create a new element with the given attrs and children.
3 * Children can be a string for text nodes or other elements.
4 * @param {String} tagName
5 * @param {Object<String, String>} attrs
6 * @param {Element[]|String[]}children
9 export function elem(tagName, attrs = {}, children = []) {
10 const el = document.createElement(tagName);
12 for (const [key, val] of Object.entries(attrs)) {
14 el.removeAttribute(key);
16 el.setAttribute(key, val);
20 for (const child of children) {
21 if (typeof child === 'string') {
22 el.append(document.createTextNode(child));
32 * Run the given callback against each element that matches the given selector.
33 * @param {String} selector
34 * @param {Function<Element>} callback
36 export function forEach(selector, callback) {
37 const elements = document.querySelectorAll(selector);
38 for (const element of elements) {
44 * Helper to listen to multiple DOM events
45 * @param {Element} listenerElement
46 * @param {Array<String>} events
47 * @param {Function<Event>} callback
49 export function onEvents(listenerElement, events, callback) {
50 for (const eventName of events) {
51 listenerElement.addEventListener(eventName, callback);
56 * Helper to run an action when an element is selected.
57 * A "select" is made to be accessible, So can be a click, space-press or enter-press.
58 * @param {HTMLElement|Array} elements
59 * @param {function} callback
61 export function onSelect(elements, callback) {
62 if (!Array.isArray(elements)) {
63 elements = [elements];
66 for (const listenerElement of elements) {
67 listenerElement.addEventListener('click', callback);
68 listenerElement.addEventListener('keydown', event => {
69 if (event.key === 'Enter' || event.key === ' ') {
70 event.preventDefault();
78 * Listen to enter press on the given element(s).
79 * @param {HTMLElement|Array} elements
80 * @param {function} callback
82 export function onEnterPress(elements, callback) {
83 if (!Array.isArray(elements)) {
84 elements = [elements];
87 const listener = event => {
88 if (event.key === 'Enter') {
93 elements.forEach(e => e.addEventListener('keypress', listener));
97 * Set a listener on an element for an event emitted by a child
98 * matching the given childSelector param.
99 * Used in a similar fashion to jQuery's $('listener').on('eventName', 'childSelector', callback)
100 * @param {Element} listenerElement
101 * @param {String} childSelector
102 * @param {String} eventName
103 * @param {Function} callback
105 export function onChildEvent(listenerElement, childSelector, eventName, callback) {
106 listenerElement.addEventListener(eventName, event => {
107 const matchingChild = event.target.closest(childSelector);
109 callback.call(matchingChild, event, matchingChild);
115 * Look for elements that match the given selector and contain the given text.
116 * Is case insensitive and returns the first result or null if nothing is found.
117 * @param {String} selector
118 * @param {String} text
121 export function findText(selector, text) {
122 const elements = document.querySelectorAll(selector);
123 text = text.toLowerCase();
124 for (const element of elements) {
125 if (element.textContent.toLowerCase().includes(text)) {
133 * Show a loading indicator in the given element.
134 * This will effectively clear the element.
135 * @param {Element} element
137 export function showLoading(element) {
138 element.innerHTML = '<div class="loading-container"><div></div><div></div><div></div></div>';
142 * Get a loading element indicator element.
145 export function getLoading() {
146 const wrap = document.createElement('div');
147 wrap.classList.add('loading-container');
148 wrap.innerHTML = '<div></div><div></div><div></div>';
153 * Remove any loading indicators within the given element.
154 * @param {Element} element
156 export function removeLoading(element) {
157 const loadingEls = element.querySelectorAll('.loading-container');
158 for (const el of loadingEls) {
164 * Convert the given html data into a live DOM element.
165 * Initiates any components defined in the data.
166 * @param {String} html
169 export function htmlToDom(html) {
170 const wrap = document.createElement('div');
171 wrap.innerHTML = html;
172 window.$components.init(wrap);
173 return wrap.children[0];