From 062b62fb585b3a7ac8415d3fc8caedcb1872b106 Mon Sep 17 00:00:00 2001 From: Jorge Bolois Date: Mon, 29 May 2023 16:35:40 +0200 Subject: [PATCH] Adding some utils to the template --- src/util/Event.js | 23 +++++++++++++++++++++++ src/util/Log.js | 7 +++++++ src/util/StringUtil.js | 7 +++++++ 3 files changed, 37 insertions(+) create mode 100644 src/util/Event.js create mode 100644 src/util/Log.js create mode 100644 src/util/StringUtil.js diff --git a/src/util/Event.js b/src/util/Event.js new file mode 100644 index 0000000..809ba6a --- /dev/null +++ b/src/util/Event.js @@ -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); + }); + } + +} diff --git a/src/util/Log.js b/src/util/Log.js new file mode 100644 index 0000000..bbbc4af --- /dev/null +++ b/src/util/Log.js @@ -0,0 +1,7 @@ +export default class Log { + + static debug(message) { + console.log(`%c [DEBUG] ${message} `, 'background: blue; color: white; font-size: 13px;'); + } + +} diff --git a/src/util/StringUtil.js b/src/util/StringUtil.js new file mode 100644 index 0000000..5bea6a1 --- /dev/null +++ b/src/util/StringUtil.js @@ -0,0 +1,7 @@ +export default class StringUtil { + + static capitalizeFirstLetter(string) { + return string[0].toUpperCase() + string.slice(1); + } + +}