adt-js-components 1.11.0 → 1.11.2

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 CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "adt-js-components",
3
- "version": "1.11.0",
3
+ "version": "1.11.2",
4
4
  "description": "JavaScript components for Nette framework",
5
5
  "main": "index.js",
6
6
  "scripts": {
@@ -25,7 +25,9 @@
25
25
  "tinymce-i18n": "^20.12.25",
26
26
  "nette.ajax.js": "^2.3.0",
27
27
  "leaflet": "^1.9.4",
28
- "leaflet.markercluster": "^1.5.3"
28
+ "leaflet.markercluster": "^1.5.3",
29
+ "firebase": "^12.10.0",
30
+ "@firebase/messaging": "^0.12.24"
29
31
  },
30
32
  "peerDependencies": {
31
33
  "jquery": "^3.4.1"
@@ -1,7 +1,33 @@
1
- import { getToken } from "firebase/messaging";
1
+ import { getToken, deleteToken } from "firebase/messaging";
2
2
 
3
3
  const run = (config) => {
4
- $('[data-adt-notifications]').on('click', function () {
4
+ const $enableBtn = $('[data-adt-notifications-enable]');
5
+ const $disableBtn = $('[data-adt-notifications-disable]');
6
+
7
+ const updateButtons = () => {
8
+ if (Notification.permission !== 'granted') {
9
+ $enableBtn.show();
10
+ $disableBtn.hide();
11
+ return;
12
+ }
13
+
14
+ navigator.serviceWorker.getRegistrations().then(function (registrations) {
15
+ var hasFirebaseSw = registrations.some(function (registration) {
16
+ return registration.active && registration.active.scriptURL.includes('firebase-messaging-sw');
17
+ });
18
+
19
+ if (hasFirebaseSw) {
20
+ $enableBtn.hide();
21
+ $disableBtn.show();
22
+ } else {
23
+ $enableBtn.show();
24
+ $disableBtn.hide();
25
+ }
26
+ });
27
+ };
28
+
29
+ // Enable notifications
30
+ $enableBtn.on('click', function () {
5
31
  if (window.messaging) {
6
32
  Notification.requestPermission().then(function (permission) {
7
33
  if (permission !== 'granted') {
@@ -12,9 +38,11 @@ const run = (config) => {
12
38
  getToken(window.messaging, { vapidKey: config.vapidKey })
13
39
  .then(function (currentToken) {
14
40
  if (currentToken) {
41
+ window.adtNotificationsToken = currentToken;
15
42
  $.nette.ajax({
16
43
  url: config.setFirebaseTokenLink.replace('__firebaseToken__', currentToken)
17
44
  });
45
+ updateButtons();
18
46
  } else {
19
47
  alert(_('appJs.firebase.error.notificationsPermissionError'));
20
48
  }
@@ -28,6 +56,50 @@ const run = (config) => {
28
56
  alert(_('appJs.firebase.error.notificationsNotSupported'));
29
57
  }
30
58
  });
59
+
60
+ // Disable notifications
61
+ $disableBtn.on('click', function () {
62
+ if (window.messaging) {
63
+ deleteToken(window.messaging)
64
+ .then(function () {
65
+ // Unregister service worker
66
+ return navigator.serviceWorker.getRegistrations();
67
+ })
68
+ .then(function (registrations) {
69
+ registrations.forEach(function (registration) {
70
+ if (registration.active && registration.active.scriptURL.includes('firebase-messaging-sw')) {
71
+ registration.unregister();
72
+ }
73
+ });
74
+ })
75
+ .catch(function (err) {
76
+ console.error(err);
77
+ });
78
+ }
79
+
80
+ $.nette.ajax({
81
+ url: config.removeAllFirebaseTokensLink
82
+ });
83
+
84
+ window.adtNotificationsToken = null;
85
+ updateButtons();
86
+ });
87
+
88
+ // Initial state: check if notifications are already enabled
89
+ if (Notification.permission === 'granted' && window.messaging) {
90
+ getToken(window.messaging, { vapidKey: config.vapidKey })
91
+ .then(function (currentToken) {
92
+ if (currentToken) {
93
+ window.adtNotificationsToken = currentToken;
94
+ }
95
+ updateButtons();
96
+ })
97
+ .catch(function () {
98
+ updateButtons();
99
+ });
100
+ } else {
101
+ updateButtons();
102
+ }
31
103
  }
32
104
 
33
105
  export default { run };