adt-js-components 1.10.10 → 1.11.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +1 -1
- package/src/ComponentLoader.js +27 -13
- package/src/Messaging/index.js +21 -0
- package/src/Notifications/index.js +33 -0
- package/src/Translate/index.js +33 -0
package/package.json
CHANGED
package/src/ComponentLoader.js
CHANGED
|
@@ -10,6 +10,30 @@ const init = (selector, path) => {
|
|
|
10
10
|
componentsConfig = JSON.parse(bodyDataset);
|
|
11
11
|
}
|
|
12
12
|
|
|
13
|
+
const runPath = (path, selector) => {
|
|
14
|
+
if (path.startsWith('~')) {
|
|
15
|
+
import('~/src/' + path.slice(1) + '/index.js').then(component => {
|
|
16
|
+
component.default.run(componentsConfig[selector] || {});
|
|
17
|
+
});
|
|
18
|
+
} else if (path.includes('/')) {
|
|
19
|
+
import('JsComponents/' + path + '/index.js').then(component => {
|
|
20
|
+
component.default.run(componentsConfig[selector] || {});
|
|
21
|
+
});
|
|
22
|
+
} else {
|
|
23
|
+
import('adt-js-components/src/' + path + '/index.js').then(component => {
|
|
24
|
+
component.default.run(componentsConfig[selector] || {});
|
|
25
|
+
});
|
|
26
|
+
}
|
|
27
|
+
};
|
|
28
|
+
|
|
29
|
+
const existingTarget = document.querySelector(`[data-adt-${selector}]`);
|
|
30
|
+
if (existingTarget && !loadedComponents.includes(path)) {
|
|
31
|
+
loadedComponents.push(path);
|
|
32
|
+
typeof path === 'function'
|
|
33
|
+
? path(componentsConfig[selector] || {})
|
|
34
|
+
: runPath(path, selector);
|
|
35
|
+
}
|
|
36
|
+
|
|
13
37
|
const observer = new MutationObserver((mutationsList) => {
|
|
14
38
|
// component is already loaded
|
|
15
39
|
if (loadedComponents.includes(path)) return;
|
|
@@ -21,20 +45,10 @@ const init = (selector, path) => {
|
|
|
21
45
|
if (target) {
|
|
22
46
|
loadedComponents.push(path);
|
|
23
47
|
|
|
24
|
-
if (path
|
|
25
|
-
|
|
26
|
-
component.default.run(componentsConfig[selector] || {});
|
|
27
|
-
});
|
|
28
|
-
|
|
29
|
-
} else if (path.includes('/')) {
|
|
30
|
-
import('JsComponents/' + path + '/index.js').then(component => {
|
|
31
|
-
component.default.run(componentsConfig[selector] || {});
|
|
32
|
-
});
|
|
33
|
-
|
|
48
|
+
if (typeof path === 'function') {
|
|
49
|
+
path(componentsConfig[selector] || {});
|
|
34
50
|
} else {
|
|
35
|
-
|
|
36
|
-
component.default.run(componentsConfig[selector] || {});
|
|
37
|
-
});
|
|
51
|
+
runPath(path, selector);
|
|
38
52
|
}
|
|
39
53
|
break;
|
|
40
54
|
}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import { initializeApp } from "firebase/app";
|
|
2
|
+
import { getMessaging, isSupported } from "firebase/messaging";
|
|
3
|
+
|
|
4
|
+
const run = async (config) => {
|
|
5
|
+
const app = initializeApp(config.initializeConfig);
|
|
6
|
+
|
|
7
|
+
const supported = await isSupported();
|
|
8
|
+
const messaging = supported ? getMessaging(app) : null;
|
|
9
|
+
|
|
10
|
+
navigator.serviceWorker.addEventListener('message', (event) => {
|
|
11
|
+
const payload = event.data;
|
|
12
|
+
$(document).trigger(`messaging.${payload.data.action}`, {
|
|
13
|
+
action: payload.data.action,
|
|
14
|
+
body: payload.data.body
|
|
15
|
+
});
|
|
16
|
+
});
|
|
17
|
+
|
|
18
|
+
window.messaging = messaging;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
export default {run};
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import { getToken } from "firebase/messaging";
|
|
2
|
+
|
|
3
|
+
const run = (config) => {
|
|
4
|
+
$('[data-adt-notifications]').on('click', function () {
|
|
5
|
+
if (window.messaging) {
|
|
6
|
+
Notification.requestPermission().then(function (permission) {
|
|
7
|
+
if (permission !== 'granted') {
|
|
8
|
+
alert(_('appJs.firebase.error.notificationsPermissionError'));
|
|
9
|
+
return;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
getToken(window.messaging, { vapidKey: config.vapidKey })
|
|
13
|
+
.then(function (currentToken) {
|
|
14
|
+
if (currentToken) {
|
|
15
|
+
$.nette.ajax({
|
|
16
|
+
url: config.setFirebaseTokenLink.replace('__firebaseToken__', currentToken)
|
|
17
|
+
});
|
|
18
|
+
} else {
|
|
19
|
+
alert(_('appJs.firebase.error.notificationsPermissionError'));
|
|
20
|
+
}
|
|
21
|
+
})
|
|
22
|
+
.catch(function (err) {
|
|
23
|
+
console.error(err);
|
|
24
|
+
alert(_('appJs.firebase.error.notificationsPermissionError'));
|
|
25
|
+
});
|
|
26
|
+
});
|
|
27
|
+
} else {
|
|
28
|
+
alert(_('appJs.firebase.error.notificationsNotSupported'));
|
|
29
|
+
}
|
|
30
|
+
});
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
export default { run };
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
const run = (config) => {
|
|
2
|
+
|
|
3
|
+
const _ = (message, number, params) => {
|
|
4
|
+
let s = config.all[message];
|
|
5
|
+
|
|
6
|
+
if (s === undefined) {
|
|
7
|
+
return null;
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
if (params) {
|
|
11
|
+
for (const key in params) {
|
|
12
|
+
s = s.replaceAll(`%${key}%`, params[key]);
|
|
13
|
+
}
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
if (s.includes("|")) {
|
|
17
|
+
const options = s.split("|");
|
|
18
|
+
if (Math.abs(number) === 1) {
|
|
19
|
+
s = options[0];
|
|
20
|
+
} else if (Math.abs(number) >= 2 && Math.abs(number) <= 4) {
|
|
21
|
+
s = options[1];
|
|
22
|
+
} else {
|
|
23
|
+
s = options[2];
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
return s;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
window._ = _;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
export default {run};
|