adt-js-components 1.0.8 → 1.1.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 +1 -1
- package/src/SubmitForm/index.js +65 -12
package/package.json
CHANGED
package/src/ComponentLoader.js
CHANGED
|
@@ -20,7 +20,7 @@ const init = (selector, path) => {
|
|
|
20
20
|
loadedComponents.push(path);
|
|
21
21
|
|
|
22
22
|
if (path.includes('/')) {
|
|
23
|
-
import(
|
|
23
|
+
import(path + '/index.js').then(component => {
|
|
24
24
|
component.default.run(componentsConfig[selector] || {});
|
|
25
25
|
});
|
|
26
26
|
|
package/src/SubmitForm/index.js
CHANGED
|
@@ -1,6 +1,69 @@
|
|
|
1
1
|
const Scrollparent = require("scrollparent");
|
|
2
2
|
|
|
3
|
+
function isList(el) {
|
|
4
|
+
return el.name.endsWith('[]') || el.type === 'radio';
|
|
5
|
+
}
|
|
6
|
+
|
|
7
|
+
function getErrorElementId(el) {
|
|
8
|
+
return isList(el) ? el.id.split('-').slice(0, -1).join('-') : el.id;
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
function scrollToFirstError(form) {
|
|
12
|
+
const el = $(form).find('.alert-danger:first, .is-invalid:first')[0];
|
|
13
|
+
|
|
14
|
+
// we have to find first scrollable element (it can be document or modal for example)
|
|
15
|
+
let scrollParent = Scrollparent(el);
|
|
16
|
+
if (scrollParent.tagName === 'BODY') {
|
|
17
|
+
scrollParent = document.scrollingElement || document.documentElement;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
$(scrollParent).animate({
|
|
21
|
+
scrollTop: el.getBoundingClientRect().top - el.offsetParent.getBoundingClientRect().top - 100
|
|
22
|
+
}, 500);
|
|
23
|
+
}
|
|
24
|
+
|
|
3
25
|
function run(options) {
|
|
26
|
+
$.nette.ext('live').after(function($el) {
|
|
27
|
+
$el.find('[data-app-submit-form]').find('input, textarea, select').on('input', function(e) {
|
|
28
|
+
this.classList.remove('is-invalid');
|
|
29
|
+
if (isList(this)) {
|
|
30
|
+
$(this).parent().parent().find('.is-invalid').removeClass('is-invalid');
|
|
31
|
+
}
|
|
32
|
+
});
|
|
33
|
+
});
|
|
34
|
+
|
|
35
|
+
if (typeof Nette !== "undefined") {
|
|
36
|
+
Nette.showFormErrors = function(form, errors) {
|
|
37
|
+
// remove previously displayed error messages
|
|
38
|
+
for (const error of errors) {
|
|
39
|
+
document.getElementById('snippet-' + getErrorElementId(error.element) + '-errors').innerHTML = '';
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
for (const error of errors) {
|
|
43
|
+
// because radio lists and checkbox lists contains one error message multiple times
|
|
44
|
+
if (!document.getElementById('snippet-' + getErrorElementId(error.element) + '-errors').innerHTML.includes(error.message)) {
|
|
45
|
+
document.getElementById('snippet-' + getErrorElementId(error.element) + '-errors').innerHTML += '<div>' + error.message + '</div>';
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
error.element.classList.add('is-invalid');
|
|
49
|
+
// because of radio lists and checkbox lists
|
|
50
|
+
if (isList(error.element)) {
|
|
51
|
+
error.element.parentNode.classList.add('is-invalid');
|
|
52
|
+
}
|
|
53
|
+
// because of https://github.com/twbs/bootstrap/issues/25110
|
|
54
|
+
if (error.element.parentNode.classList.contains('input-group')) {
|
|
55
|
+
error.element.parentNode.classList.add('has-validation');
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
if (errors.length) {
|
|
60
|
+
scrollToFirstError(form);
|
|
61
|
+
}
|
|
62
|
+
};
|
|
63
|
+
} else {
|
|
64
|
+
console.error('Package nette-forms is missing!');
|
|
65
|
+
}
|
|
66
|
+
|
|
4
67
|
$.nette.ext("submitForm", {
|
|
5
68
|
before: function (xhr, settings) {
|
|
6
69
|
if (settings.nette && settings.nette.form && settings.nette.form.attr('data-adt-submit-form') !== undefined) {
|
|
@@ -34,18 +97,8 @@ function run(options) {
|
|
|
34
97
|
},
|
|
35
98
|
complete: function (xhr, status, settings) {
|
|
36
99
|
// if there are errors we will scroll to first of them
|
|
37
|
-
if (settings.nette && settings.nette.form && settings.nette.form.attr('data-adt-submit-form') !== undefined && settings.nette.form.find('.alert-danger').length > 0) {
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
// we have to find first scrollable element (it can be document or modal for example)
|
|
41
|
-
let scrollParent = Scrollparent($error[0]);
|
|
42
|
-
if (scrollParent.tagName === 'BODY') {
|
|
43
|
-
scrollParent = document.scrollingElement || document.documentElement;
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
$(scrollParent).animate({
|
|
47
|
-
scrollTop: $error.offset().top - 100
|
|
48
|
-
}, 500);
|
|
100
|
+
if (settings.nette && settings.nette.form && settings.nette.form.attr('data-adt-submit-form') !== undefined && settings.nette.form.find('.alert-danger, .is-invalid').length > 0) {
|
|
101
|
+
scrollToFirstError(settings.nette.form);
|
|
49
102
|
}
|
|
50
103
|
}
|
|
51
104
|
});
|