From 58890aac9e8bc736384cf671cfd9bba00a38bc55 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pavel=20Konvi=C4=8Dka?= Date: Tue, 31 Mar 2026 00:01:51 +0200 Subject: [PATCH 1/2] feat: add function callback support to AdtJsComponents.init() --- src/ComponentLoader.js | 40 +++++++++++++++++++++++++++------------- 1 file changed, 27 insertions(+), 13 deletions(-) diff --git a/src/ComponentLoader.js b/src/ComponentLoader.js index 44d8497..0c22ea7 100644 --- a/src/ComponentLoader.js +++ b/src/ComponentLoader.js @@ -10,6 +10,30 @@ const init = (selector, path) => { componentsConfig = JSON.parse(bodyDataset); } + const runPath = (path, selector) => { + if (path.startsWith('~')) { + import('~/src/' + path.slice(1) + '/index.js').then(component => { + component.default.run(componentsConfig[selector] || {}); + }); + } else if (path.includes('/')) { + import('JsComponents/' + path + '/index.js').then(component => { + component.default.run(componentsConfig[selector] || {}); + }); + } else { + import('adt-js-components/src/' + path + '/index.js').then(component => { + component.default.run(componentsConfig[selector] || {}); + }); + } + }; + + const existingTarget = document.querySelector(`[data-adt-${selector}]`); + if (existingTarget && !loadedComponents.includes(path)) { + loadedComponents.push(path); + typeof path === 'function' + ? path(componentsConfig[selector] || {}) + : runPath(path, selector); + } + const observer = new MutationObserver((mutationsList) => { // component is already loaded if (loadedComponents.includes(path)) return; @@ -21,20 +45,10 @@ const init = (selector, path) => { if (target) { loadedComponents.push(path); - if (path.startsWith('~')) { - import('~/src/' + path.slice(1) + '/index.js').then(component => { - component.default.run(componentsConfig[selector] || {}); - }); - - } else if (path.includes('/')) { - import('JsComponents/' + path + '/index.js').then(component => { - component.default.run(componentsConfig[selector] || {}); - }); - + if (typeof path === 'function') { + path(componentsConfig[selector] || {}); } else { - import('adt-js-components/src/' + path + '/index.js').then(component => { - component.default.run(componentsConfig[selector] || {}); - }); + runPath(path, selector); } break; } From 651d1b4912eaf1b47f85fffaac5c2c3c6fa1062e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pavel=20Konvi=C4=8Dka?= Date: Mon, 6 Apr 2026 18:57:11 +0200 Subject: [PATCH 2/2] add firebase scripts --- src/Messaging/index.js | 21 +++++++++++++++++++++ src/Notifications/index.js | 33 +++++++++++++++++++++++++++++++++ src/Translate/index.js | 33 +++++++++++++++++++++++++++++++++ 3 files changed, 87 insertions(+) create mode 100644 src/Messaging/index.js create mode 100644 src/Notifications/index.js create mode 100644 src/Translate/index.js diff --git a/src/Messaging/index.js b/src/Messaging/index.js new file mode 100644 index 0000000..eebbec0 --- /dev/null +++ b/src/Messaging/index.js @@ -0,0 +1,21 @@ +import { initializeApp } from "firebase/app"; +import { getMessaging, isSupported } from "firebase/messaging"; + +const run = async (config) => { + const app = initializeApp(config.initializeConfig); + + const supported = await isSupported(); + const messaging = supported ? getMessaging(app) : null; + + navigator.serviceWorker.addEventListener('message', (event) => { + const payload = event.data; + $(document).trigger(`messaging.${payload.data.action}`, { + action: payload.data.action, + body: payload.data.body + }); + }); + + window.messaging = messaging; +} + +export default {run}; \ No newline at end of file diff --git a/src/Notifications/index.js b/src/Notifications/index.js new file mode 100644 index 0000000..9f2df6d --- /dev/null +++ b/src/Notifications/index.js @@ -0,0 +1,33 @@ +import { getToken } from "firebase/messaging"; + +const run = (config) => { + $('[data-adt-notifications]').on('click', function () { + if (window.messaging) { + Notification.requestPermission().then(function (permission) { + if (permission !== 'granted') { + alert(_('appJs.firebase.error.notificationsPermissionError')); + return; + } + + getToken(window.messaging, { vapidKey: config.vapidKey }) + .then(function (currentToken) { + if (currentToken) { + $.nette.ajax({ + url: config.setFirebaseTokenLink.replace('__firebaseToken__', currentToken) + }); + } else { + alert(_('appJs.firebase.error.notificationsPermissionError')); + } + }) + .catch(function (err) { + console.error(err); + alert(_('appJs.firebase.error.notificationsPermissionError')); + }); + }); + } else { + alert(_('appJs.firebase.error.notificationsNotSupported')); + } + }); +} + +export default { run }; \ No newline at end of file diff --git a/src/Translate/index.js b/src/Translate/index.js new file mode 100644 index 0000000..3740e50 --- /dev/null +++ b/src/Translate/index.js @@ -0,0 +1,33 @@ +const run = (config) => { + + const _ = (message, number, params) => { + let s = config.all[message]; + + if (s === undefined) { + return null; + } + + if (params) { + for (const key in params) { + s = s.replaceAll(`%${key}%`, params[key]); + } + } + + if (s.includes("|")) { + const options = s.split("|"); + if (Math.abs(number) === 1) { + s = options[0]; + } else if (Math.abs(number) >= 2 && Math.abs(number) <= 4) { + s = options[1]; + } else { + s = options[2]; + } + } + + return s; + } + + window._ = _; +} + +export default {run}; \ No newline at end of file