adt-js-components 2.0.3 → 2.1.1

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": "2.0.3",
3
+ "version": "2.1.1",
4
4
  "description": "JavaScript components for Nette framework",
5
5
  "main": "index.js",
6
6
  "scripts": {
@@ -11,6 +11,10 @@
11
11
  "url": "git+https://github.com/AppsDevTeam/js-components.git"
12
12
  },
13
13
  "dependencies": {
14
+ "nette.ajax.js": "^2.3.0"
15
+ },
16
+ "peerDependencies": {
17
+ "jquery": "^3.4.1",
14
18
  "select2": "^4.0.13",
15
19
  "select2-bootstrap-5-theme": "^1.1.1",
16
20
  "autonumeric": "^4.6.0",
@@ -23,14 +27,26 @@
23
27
  "scrollparent": "^2.0.1",
24
28
  "tinymce": "^5.7.0",
25
29
  "tinymce-i18n": "^20.12.25",
26
- "nette.ajax.js": "^2.3.0",
27
30
  "leaflet": "^1.9.4",
28
31
  "leaflet.markercluster": "^1.5.3",
29
- "firebase": "^12.10.0",
30
- "@firebase/messaging": "^0.12.24"
32
+ "firebase": "^12.10.0"
31
33
  },
32
- "peerDependencies": {
33
- "jquery": "^3.4.1"
34
+ "peerDependenciesMeta": {
35
+ "select2": { "optional": true },
36
+ "select2-bootstrap-5-theme": { "optional": true },
37
+ "autonumeric": { "optional": true },
38
+ "@here/flexpolyline": { "optional": true },
39
+ "flatpickr": { "optional": true },
40
+ "script-loader": { "optional": true },
41
+ "timepicker": { "optional": true },
42
+ "glightbox": { "optional": true },
43
+ "@googlemaps/js-api-loader": { "optional": true },
44
+ "scrollparent": { "optional": true },
45
+ "tinymce": { "optional": true },
46
+ "tinymce-i18n": { "optional": true },
47
+ "leaflet": { "optional": true },
48
+ "leaflet.markercluster": { "optional": true },
49
+ "firebase": { "optional": true }
34
50
  },
35
51
  "author": "Apps Dev Team",
36
52
  "license": "MIT",
@@ -88,18 +88,48 @@ const run = (config) => {
88
88
  updateButtons();
89
89
  });
90
90
 
91
- // Initial state: check if notifications are already enabled
92
- if (Notification.permission === 'granted' && window.messaging) {
93
- getToken(window.messaging, { vapidKey: config.vapidKey })
94
- .then(function (currentToken) {
95
- if (currentToken) {
96
- window.adtNotificationsToken = currentToken;
91
+ // window.messaging se inicializuje async v Messaging komponentě, která nemusí
92
+ // doběhnout dřív než tento init (typicky po F5). Počkáme na ni, jinak by se
93
+ // getToken nikdy nezavolal a window.adtNotificationsToken by zůstal nenastavený.
94
+ var waitForMessaging = function (timeoutMs) {
95
+ return new Promise(function (resolve) {
96
+ if (window.messaging) {
97
+ resolve(window.messaging);
98
+ return;
99
+ }
100
+ var elapsed = 0;
101
+ var step = 100;
102
+ var interval = setInterval(function () {
103
+ if (window.messaging) {
104
+ clearInterval(interval);
105
+ resolve(window.messaging);
106
+ } else if ((elapsed += step) >= timeoutMs) {
107
+ clearInterval(interval);
108
+ resolve(null);
97
109
  }
110
+ }, step);
111
+ });
112
+ };
113
+
114
+ // Initial state: check if notifications are already enabled
115
+ if (Notification.permission === 'granted') {
116
+ waitForMessaging(5000).then(function (messaging) {
117
+ if (!messaging) {
98
118
  updateButtons();
99
- })
100
- .catch(function () {
101
- updateButtons();
102
- });
119
+ return;
120
+ }
121
+
122
+ getToken(messaging, { vapidKey: config.vapidKey })
123
+ .then(function (currentToken) {
124
+ if (currentToken) {
125
+ window.adtNotificationsToken = currentToken;
126
+ }
127
+ updateButtons();
128
+ })
129
+ .catch(function () {
130
+ updateButtons();
131
+ });
132
+ });
103
133
  } else {
104
134
  updateButtons();
105
135
  }
@@ -8,6 +8,30 @@ function getErrorElementId(el) {
8
8
  return isList(el) ? el.id.split('-').slice(0, -1).join('-') : el.id;
9
9
  }
10
10
 
11
+ function toggleInvalid(el, invalid) {
12
+ el.classList.toggle('is-invalid', invalid);
13
+ // because of radio lists and checkbox lists
14
+ if (isList(el)) {
15
+ el.parentNode.classList.toggle('is-invalid', invalid);
16
+ }
17
+ // because of https://github.com/twbs/bootstrap/issues/25110
18
+ if (el.parentNode.classList.contains('input-group')) {
19
+ el.parentNode.classList.toggle('has-validation', invalid);
20
+ el.parentNode.classList.toggle('is-invalid', invalid);
21
+ }
22
+ }
23
+
24
+ function hasErrors(errorElement) {
25
+ return Array.from(errorElement.childNodes).some(node => node.nodeName !== 'SCRIPT' && node.textContent.trim() !== '');
26
+ }
27
+
28
+ function markErrorsFor(errorElement) {
29
+ const el = document.getElementById(errorElement.getAttribute('data-adt-errors-for'));
30
+ if (el && el.tagName !== 'FORM') {
31
+ toggleInvalid(el, hasErrors(errorElement));
32
+ }
33
+ }
34
+
11
35
  function scrollToFirstError(form) {
12
36
  const el = $(form).find('.alert-danger:first, .is-invalid:first')[0];
13
37
 
@@ -28,10 +52,17 @@ function run(options) {
28
52
  if (isList(this)) {
29
53
  $(this).parent().parent().find('.is-invalid').removeClass('is-invalid');
30
54
  }
55
+ if (this.parentNode.classList.contains('input-group')) {
56
+ this.parentNode.classList.remove('is-invalid');
57
+ }
31
58
  });
32
59
  }
33
60
 
34
61
  $.nette.ext('live').after(function($el) {
62
+ $el.find('[data-adt-errors-for]').addBack('[data-adt-errors-for]').each(function() {
63
+ markErrorsFor(this);
64
+ });
65
+
35
66
  window.addEventListener('pageshow', (e) => {
36
67
  if (e.persisted) {
37
68
  $el.find('[data-adt-submit-form]').each(function(e) {
@@ -85,15 +116,7 @@ function run(options) {
85
116
  errorElement.innerHTML += `<div>${error.message}</div>`;
86
117
  }
87
118
 
88
- error.element.classList.add('is-invalid');
89
- // because of radio lists and checkbox lists
90
- if (isList(error.element)) {
91
- error.element.parentNode.classList.add('is-invalid');
92
- }
93
- // because of https://github.com/twbs/bootstrap/issues/25110
94
- if (error.element.parentNode.classList.contains('input-group')) {
95
- error.element.parentNode.classList.add('has-validation');
96
- }
119
+ toggleInvalid(error.element, true);
97
120
  }
98
121
 
99
122
  if (errors.length) {