Adding some utils to the template

This commit is contained in:
Jorge Bolois 2023-05-29 16:35:40 +02:00
parent c25f0c361e
commit 062b62fb58
3 changed files with 37 additions and 0 deletions

23
src/util/Event.js Normal file
View File

@ -0,0 +1,23 @@
import Log from "./Log.js";
import StringUtil from "./StringUtil.js";
export default class Event {
static click(selector, callback) {
this._addEvent('click', selector, callback)
}
static change(selector, callback) {
this._addEvent('change', selector, callback)
}
static _addEvent(eventName, selector, callback) {
Log.debug(`Adding event: ${eventName} to: ${selector}`);
document.addEventListener(eventName, (event) => {
if (!event.target.matches(selector)) return;
Log.debug(`${StringUtil.capitalizeFirstLetter(eventName)} on: ${selector}`);
callback(event.target);
});
}
}

7
src/util/Log.js Normal file
View File

@ -0,0 +1,7 @@
export default class Log {
static debug(message) {
console.log(`%c [DEBUG] ${message} `, 'background: blue; color: white; font-size: 13px;');
}
}

7
src/util/StringUtil.js Normal file
View File

@ -0,0 +1,7 @@
export default class StringUtil {
static capitalizeFirstLetter(string) {
return string[0].toUpperCase() + string.slice(1);
}
}