adt-js-components 1.7.1 → 1.8.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 CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "adt-js-components",
3
- "version": "1.7.1",
3
+ "version": "1.8.0",
4
4
  "description": "JavaScript components for Nette framework",
5
5
  "main": "index.js",
6
6
  "scripts": {
@@ -69,6 +69,10 @@ const initRecaptcha = () => {
69
69
  init('recaptcha', 'Recaptcha');
70
70
  };
71
71
 
72
+ const initRedrawSnippet = () => {
73
+ init('redraw-snippet', 'RedrawSnippet');
74
+ };
75
+
72
76
  const initSelect2 = () => {
73
77
  init('select2', 'Select2');
74
78
  };
@@ -122,6 +126,7 @@ export default {
122
126
  initGLightbox,
123
127
  initGoogleMaps,
124
128
  initRecaptcha,
129
+ initRedrawSnippet,
125
130
  initSelect2,
126
131
  initSubmitForm,
127
132
  initTinyMCE,
@@ -0,0 +1,43 @@
1
+ async function run(options) {
2
+ function applyEventHandlers(el) {
3
+ var $el= $(el);
4
+ if ($el.is('button, input[type="button"], input[type="submit"]')) {
5
+ $el.on('click', sendNetteAjax);
6
+ } else if ($el.is('input:not([type="button"]):not([type="submit"]), select, textarea')) {
7
+ $el.on('change', sendNetteAjax);
8
+ }
9
+ }
10
+
11
+ function sendNetteAjax(e) {
12
+ $(document).find('[name="' + $(e.currentTarget).attr('data-adt-redraw-snippet') + '"]').netteAjax(e);
13
+ }
14
+
15
+ const observer = new MutationObserver(mutations => {
16
+ mutations.forEach(mutation => {
17
+ if (mutation.type === "childList") {
18
+ mutation.addedNodes.forEach(node => {
19
+ if (node.nodeType === 1) {
20
+ if (node.hasAttribute("data-adt-redraw-snippet")) {
21
+ applyEventHandlers(node);
22
+ }
23
+
24
+ node.querySelectorAll('[data-adt-redraw-snippet]').forEach(child => {
25
+ applyEventHandlers(child);
26
+ });
27
+ }
28
+ });
29
+ }
30
+ });
31
+ });
32
+
33
+ observer.observe(document.body, {
34
+ childList: true,
35
+ subtree: true,
36
+ });
37
+
38
+ document.querySelectorAll('[data-adt-redraw-snippet]').forEach(function(el) {
39
+ applyEventHandlers(el);
40
+ });
41
+ }
42
+
43
+ export default { run }
@@ -73,10 +73,16 @@ function run(options) {
73
73
  el.innerHTML = '';
74
74
  });
75
75
 
76
+ const alertErrors = [];
76
77
  for (const error of errors) {
77
78
  // because radio lists and checkbox lists contains one error message multiple times
78
- if (!document.getElementById('snippet-' + getErrorElementId(error.element) + '-errors').innerHTML.includes(error.message)) {
79
- document.getElementById('snippet-' + getErrorElementId(error.element) + '-errors').innerHTML += '<div>' + error.message + '</div>';
79
+ const errorElementId = 'snippet-' + getErrorElementId(error.element) + '-errors';
80
+ const errorElement = document.getElementById(errorElementId);
81
+ if (!errorElement) {
82
+ alertErrors.push(error.message);
83
+ console.warn(`Element with ID "${errorElementId}" no exist.`);
84
+ } else if (!errorElement.innerHTML.includes(error.message)) {
85
+ errorElement.innerHTML += `<div>${error.message}</div>`;
80
86
  }
81
87
 
82
88
  error.element.classList.add('is-invalid');
@@ -93,6 +99,10 @@ function run(options) {
93
99
  if (errors.length) {
94
100
  scrollToFirstError(form);
95
101
  }
102
+
103
+ if (alertErrors.length) {
104
+ alert(alertErrors.join('\n'));
105
+ }
96
106
  };
97
107
  } else {
98
108
  console.error('Package nette-forms is missing!');