@kupola/kupola 1.2.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/CHANGELOG.md +130 -0
- package/INTEGRATION.md +440 -0
- package/PROJECT_SUMMARY.md +312 -0
- package/README.md +286 -0
- package/SKILL.md +572 -0
- package/css/accessibility.css +119 -0
- package/css/animations.css +224 -0
- package/css/brand-themes.css +300 -0
- package/css/colors_and_type.css +441 -0
- package/css/components-ext.css +4165 -0
- package/css/components.css +1483 -0
- package/css/kupola.css +12 -0
- package/css/responsive.css +697 -0
- package/css/scaffold.css +145 -0
- package/css/states.css +316 -0
- package/css/theme-dark.css +296 -0
- package/css/theme-light.css +296 -0
- package/css/utilities.css +171 -0
- package/dist/css/kupola.css +12 -0
- package/dist/icons/icons/activity.svg +3 -0
- package/dist/icons/icons/alert-circle.svg +1 -0
- package/dist/icons/icons/alert-triangle.svg +1 -0
- package/dist/icons/icons/alert.d27e24.svg +1 -0
- package/dist/icons/icons/alert.svg +1 -0
- package/dist/icons/icons/apple.svg +1 -0
- package/dist/icons/icons/arrow-down.svg +1 -0
- package/dist/icons/icons/arrow-expand.svg +1 -0
- package/dist/icons/icons/arrow-left.svg +1 -0
- package/dist/icons/icons/arrow-minimize.svg +1 -0
- package/dist/icons/icons/arrow-right-to-line.svg +1 -0
- package/dist/icons/icons/arrow-right.svg +1 -0
- package/dist/icons/icons/arrow-up-right.svg +1 -0
- package/dist/icons/icons/arrow-up.0c0c0d.svg +1 -0
- package/dist/icons/icons/arrow-up.svg +1 -0
- package/dist/icons/icons/at.svg +1 -0
- package/dist/icons/icons/atom.0c0c0d.svg +1 -0
- package/dist/icons/icons/atom.svg +1 -0
- package/dist/icons/icons/bar-chart-2.svg +1 -0
- package/dist/icons/icons/bar.svg +1 -0
- package/dist/icons/icons/bell.svg +1 -0
- package/dist/icons/icons/box.svg +1 -0
- package/dist/icons/icons/bug.svg +1 -0
- package/dist/icons/icons/button.svg +1 -0
- package/dist/icons/icons/calendar.svg +1 -0
- package/dist/icons/icons/check-circle.33c192.svg +1 -0
- package/dist/icons/icons/check-circle.svg +1 -0
- package/dist/icons/icons/check-square.svg +4 -0
- package/dist/icons/icons/check.d1d3db.svg +1 -0
- package/dist/icons/icons/check.svg +1 -0
- package/dist/icons/icons/chevron-down.svg +1 -0
- package/dist/icons/icons/chevron-left.svg +1 -0
- package/dist/icons/icons/chevron-right.svg +1 -0
- package/dist/icons/icons/chevron-up.svg +1 -0
- package/dist/icons/icons/circle.svg +1 -0
- package/dist/icons/icons/clock.svg +1 -0
- package/dist/icons/icons/cmd.svg +1 -0
- package/dist/icons/icons/code.svg +1 -0
- package/dist/icons/icons/columns.svg +1 -0
- package/dist/icons/icons/copy.svg +1 -0
- package/dist/icons/icons/cpu.svg +1 -0
- package/dist/icons/icons/dashboard.0c0c0d.svg +1 -0
- package/dist/icons/icons/dashboard.svg +1 -0
- package/dist/icons/icons/dollar.svg +1 -0
- package/dist/icons/icons/download.svg +1 -0
- package/dist/icons/icons/edit.svg +1 -0
- package/dist/icons/icons/extensions.svg +1 -0
- package/dist/icons/icons/external.svg +1 -0
- package/dist/icons/icons/eye.svg +1 -0
- package/dist/icons/icons/file-text.svg +1 -0
- package/dist/icons/icons/file.svg +1 -0
- package/dist/icons/icons/files.svg +1 -0
- package/dist/icons/icons/filter.svg +1 -0
- package/dist/icons/icons/folder-open.svg +4 -0
- package/dist/icons/icons/folder.svg +1 -0
- package/dist/icons/icons/gear.svg +1 -0
- package/dist/icons/icons/git.svg +1 -0
- package/dist/icons/icons/github.svg +1 -0
- package/dist/icons/icons/globe.svg +1 -0
- package/dist/icons/icons/grid-2x2.svg +1 -0
- package/dist/icons/icons/hash.svg +1 -0
- package/dist/icons/icons/heart.svg +1 -0
- package/dist/icons/icons/help-circle.svg +5 -0
- package/dist/icons/icons/help.svg +1 -0
- package/dist/icons/icons/home.svg +1 -0
- package/dist/icons/icons/icons.svg +153 -0
- package/dist/icons/icons/image.svg +1 -0
- package/dist/icons/icons/info-circle.svg +1 -0
- package/dist/icons/icons/info-square.svg +1 -0
- package/dist/icons/icons/info.387bff.svg +1 -0
- package/dist/icons/icons/info.svg +1 -0
- package/dist/icons/icons/input.svg +1 -0
- package/dist/icons/icons/key.svg +1 -0
- package/dist/icons/icons/layers.svg +1 -0
- package/dist/icons/icons/layout-grid.svg +6 -0
- package/dist/icons/icons/layout.svg +1 -0
- package/dist/icons/icons/link.svg +1 -0
- package/dist/icons/icons/list.svg +1 -0
- package/dist/icons/icons/lock.svg +1 -0
- package/dist/icons/icons/log-out.svg +1 -0
- package/dist/icons/icons/logo.svg +1 -0
- package/dist/icons/icons/mail.svg +1 -0
- package/dist/icons/icons/menu.svg +1 -0
- package/dist/icons/icons/message-circle.svg +1 -0
- package/dist/icons/icons/message-plus.svg +1 -0
- package/dist/icons/icons/message-square.svg +1 -0
- package/dist/icons/icons/mic.svg +1 -0
- package/dist/icons/icons/minus.svg +1 -0
- package/dist/icons/icons/moon.svg +1 -0
- package/dist/icons/icons/more-h.svg +1 -0
- package/dist/icons/icons/mouse.svg +1 -0
- package/dist/icons/icons/palette.svg +1 -0
- package/dist/icons/icons/panel-bottom.svg +1 -0
- package/dist/icons/icons/pause.svg +1 -0
- package/dist/icons/icons/play.svg +1 -0
- package/dist/icons/icons/plug.svg +1 -0
- package/dist/icons/icons/plus.0c0c0d.svg +1 -0
- package/dist/icons/icons/plus.svg +1 -0
- package/dist/icons/icons/refresh.svg +1 -0
- package/dist/icons/icons/ruler.svg +1 -0
- package/dist/icons/icons/scroll-text.svg +1 -0
- package/dist/icons/icons/search-menu.svg +1 -0
- package/dist/icons/icons/search.svg +1 -0
- package/dist/icons/icons/send.0c0c0d.svg +1 -0
- package/dist/icons/icons/send.svg +1 -0
- package/dist/icons/icons/settings.0c0c0d.svg +1 -0
- package/dist/icons/icons/settings.svg +1 -0
- package/dist/icons/icons/shield.svg +1 -0
- package/dist/icons/icons/sidebar-left.svg +1 -0
- package/dist/icons/icons/sidebar-right.svg +1 -0
- package/dist/icons/icons/sliders.svg +1 -0
- package/dist/icons/icons/sparkles.32f08c.svg +1 -0
- package/dist/icons/icons/sparkles.svg +1 -0
- package/dist/icons/icons/square.svg +1 -0
- package/dist/icons/icons/star.svg +1 -0
- package/dist/icons/icons/sun.svg +1 -0
- package/dist/icons/icons/table.svg +1 -0
- package/dist/icons/icons/terminal.svg +1 -0
- package/dist/icons/icons/trash.svg +1 -0
- package/dist/icons/icons/trending-down.svg +1 -0
- package/dist/icons/icons/trending-up.svg +1 -0
- package/dist/icons/icons/type.svg +1 -0
- package/dist/icons/icons/upload.svg +1 -0
- package/dist/icons/icons/user-circle.svg +1 -0
- package/dist/icons/icons/user-plus.svg +6 -0
- package/dist/icons/icons/user.svg +1 -0
- package/dist/icons/icons/users.svg +1 -0
- package/dist/icons/icons/warning.svg +1 -0
- package/dist/icons/icons/wrench.svg +1 -0
- package/dist/icons/icons/x-circle.svg +1 -0
- package/dist/icons/icons/x.svg +1 -0
- package/dist/icons/icons/zap.0c0c0d.svg +1 -0
- package/dist/icons/icons/zap.svg +1 -0
- package/dist/kupola.cjs.js +258 -0
- package/dist/kupola.cjs.js.map +1 -0
- package/dist/kupola.esm.js +7640 -0
- package/dist/kupola.esm.js.map +1 -0
- package/dist/kupola.umd.js +258 -0
- package/dist/kupola.umd.js.map +1 -0
- package/dist/types/kupola.d.ts +1359 -0
- package/dist/utils/utils/Kupola.cs +77 -0
- package/dist/utils/utils/Kupola.java +104 -0
- package/dist/utils/utils/kupola.go +120 -0
- package/dist/utils/utils/kupola.js +63 -0
- package/dist/utils/utils/kupola.py +1392 -0
- package/dist/utils/utils/kupola.rb +69 -0
- package/icons/activity.svg +3 -0
- package/icons/alert-circle.svg +1 -0
- package/icons/alert-triangle.svg +1 -0
- package/icons/alert.d27e24.svg +1 -0
- package/icons/alert.svg +1 -0
- package/icons/apple.svg +1 -0
- package/icons/arrow-down.svg +1 -0
- package/icons/arrow-expand.svg +1 -0
- package/icons/arrow-left.svg +1 -0
- package/icons/arrow-minimize.svg +1 -0
- package/icons/arrow-right-to-line.svg +1 -0
- package/icons/arrow-right.svg +1 -0
- package/icons/arrow-up-right.svg +1 -0
- package/icons/arrow-up.0c0c0d.svg +1 -0
- package/icons/arrow-up.svg +1 -0
- package/icons/at.svg +1 -0
- package/icons/atom.0c0c0d.svg +1 -0
- package/icons/atom.svg +1 -0
- package/icons/bar-chart-2.svg +1 -0
- package/icons/bar.svg +1 -0
- package/icons/bell.svg +1 -0
- package/icons/box.svg +1 -0
- package/icons/bug.svg +1 -0
- package/icons/button.svg +1 -0
- package/icons/calendar.svg +1 -0
- package/icons/check-circle.33c192.svg +1 -0
- package/icons/check-circle.svg +1 -0
- package/icons/check-square.svg +4 -0
- package/icons/check.d1d3db.svg +1 -0
- package/icons/check.svg +1 -0
- package/icons/chevron-down.svg +1 -0
- package/icons/chevron-left.svg +1 -0
- package/icons/chevron-right.svg +1 -0
- package/icons/chevron-up.svg +1 -0
- package/icons/circle.svg +1 -0
- package/icons/clock.svg +1 -0
- package/icons/cmd.svg +1 -0
- package/icons/code.svg +1 -0
- package/icons/columns.svg +1 -0
- package/icons/copy.svg +1 -0
- package/icons/cpu.svg +1 -0
- package/icons/dashboard.0c0c0d.svg +1 -0
- package/icons/dashboard.svg +1 -0
- package/icons/dollar.svg +1 -0
- package/icons/download.svg +1 -0
- package/icons/edit.svg +1 -0
- package/icons/extensions.svg +1 -0
- package/icons/external.svg +1 -0
- package/icons/eye.svg +1 -0
- package/icons/file-text.svg +1 -0
- package/icons/file.svg +1 -0
- package/icons/files.svg +1 -0
- package/icons/filter.svg +1 -0
- package/icons/folder-open.svg +4 -0
- package/icons/folder.svg +1 -0
- package/icons/gear.svg +1 -0
- package/icons/git.svg +1 -0
- package/icons/github.svg +1 -0
- package/icons/globe.svg +1 -0
- package/icons/grid-2x2.svg +1 -0
- package/icons/hash.svg +1 -0
- package/icons/heart.svg +1 -0
- package/icons/help-circle.svg +5 -0
- package/icons/help.svg +1 -0
- package/icons/home.svg +1 -0
- package/icons/icons.svg +153 -0
- package/icons/image.svg +1 -0
- package/icons/info-circle.svg +1 -0
- package/icons/info-square.svg +1 -0
- package/icons/info.387bff.svg +1 -0
- package/icons/info.svg +1 -0
- package/icons/input.svg +1 -0
- package/icons/key.svg +1 -0
- package/icons/layers.svg +1 -0
- package/icons/layout-grid.svg +6 -0
- package/icons/layout.svg +1 -0
- package/icons/link.svg +1 -0
- package/icons/list.svg +1 -0
- package/icons/lock.svg +1 -0
- package/icons/log-out.svg +1 -0
- package/icons/logo.svg +1 -0
- package/icons/mail.svg +1 -0
- package/icons/menu.svg +1 -0
- package/icons/message-circle.svg +1 -0
- package/icons/message-plus.svg +1 -0
- package/icons/message-square.svg +1 -0
- package/icons/mic.svg +1 -0
- package/icons/minus.svg +1 -0
- package/icons/moon.svg +1 -0
- package/icons/more-h.svg +1 -0
- package/icons/mouse.svg +1 -0
- package/icons/palette.svg +1 -0
- package/icons/panel-bottom.svg +1 -0
- package/icons/pause.svg +1 -0
- package/icons/play.svg +1 -0
- package/icons/plug.svg +1 -0
- package/icons/plus.0c0c0d.svg +1 -0
- package/icons/plus.svg +1 -0
- package/icons/refresh.svg +1 -0
- package/icons/ruler.svg +1 -0
- package/icons/scroll-text.svg +1 -0
- package/icons/search-menu.svg +1 -0
- package/icons/search.svg +1 -0
- package/icons/send.0c0c0d.svg +1 -0
- package/icons/send.svg +1 -0
- package/icons/settings.0c0c0d.svg +1 -0
- package/icons/settings.svg +1 -0
- package/icons/shield.svg +1 -0
- package/icons/sidebar-left.svg +1 -0
- package/icons/sidebar-right.svg +1 -0
- package/icons/sliders.svg +1 -0
- package/icons/sparkles.32f08c.svg +1 -0
- package/icons/sparkles.svg +1 -0
- package/icons/square.svg +1 -0
- package/icons/star.svg +1 -0
- package/icons/sun.svg +1 -0
- package/icons/table.svg +1 -0
- package/icons/terminal.svg +1 -0
- package/icons/trash.svg +1 -0
- package/icons/trending-down.svg +1 -0
- package/icons/trending-up.svg +1 -0
- package/icons/type.svg +1 -0
- package/icons/upload.svg +1 -0
- package/icons/user-circle.svg +1 -0
- package/icons/user-plus.svg +6 -0
- package/icons/user.svg +1 -0
- package/icons/users.svg +1 -0
- package/icons/warning.svg +1 -0
- package/icons/wrench.svg +1 -0
- package/icons/x-circle.svg +1 -0
- package/icons/x.svg +1 -0
- package/icons/zap.0c0c0d.svg +1 -0
- package/icons/zap.svg +1 -0
- package/js/calendar.js +184 -0
- package/js/carousel.js +173 -0
- package/js/collapse.js +127 -0
- package/js/color-picker.js +144 -0
- package/js/component.js +276 -0
- package/js/composition-api.js +458 -0
- package/js/countdown.js +144 -0
- package/js/data-bind.js +1286 -0
- package/js/datepicker.js +270 -0
- package/js/dialog.js +131 -0
- package/js/drawer.js +118 -0
- package/js/dropdown.js +122 -0
- package/js/dynamic-tags.js +156 -0
- package/js/error-handler.js +181 -0
- package/js/fileupload.js +392 -0
- package/js/form.js +291 -0
- package/js/global-events.js +203 -0
- package/js/heatmap.js +405 -0
- package/js/http.js +419 -0
- package/js/i18n.js +228 -0
- package/js/icons.js +165 -0
- package/js/image-preview.js +218 -0
- package/js/initializer.js +162 -0
- package/js/kupola-core.js +66 -0
- package/js/kupola-devtools.js +598 -0
- package/js/kupola-lifecycle.js +510 -0
- package/js/message.js +60 -0
- package/js/modal.js +191 -0
- package/js/notification.js +73 -0
- package/js/numberinput.js +146 -0
- package/js/performance.js +250 -0
- package/js/registry.js +205 -0
- package/js/router.js +396 -0
- package/js/security.js +189 -0
- package/js/select.js +143 -0
- package/js/slide-captcha.js +504 -0
- package/js/slider.js +94 -0
- package/js/statcard.js +219 -0
- package/js/tag.js +83 -0
- package/js/test-utils.js +251 -0
- package/js/theme.js +363 -0
- package/js/timepicker.js +238 -0
- package/js/tooltip.js +122 -0
- package/js/utils.js +1458 -0
- package/js/validation.js +553 -0
- package/js/virtual-list.js +593 -0
- package/package.json +105 -0
- package/templates/base.html +30 -0
- package/templates/base_dashboard.html +99 -0
- package/types/kupola.d.ts +1359 -0
- package/utils/Kupola.cs +77 -0
- package/utils/Kupola.java +104 -0
- package/utils/kupola.go +120 -0
- package/utils/kupola.js +63 -0
- package/utils/kupola.py +1392 -0
- package/utils/kupola.rb +69 -0
package/js/validation.js
ADDED
|
@@ -0,0 +1,553 @@
|
|
|
1
|
+
class KupolaValidator {
|
|
2
|
+
constructor() {
|
|
3
|
+
this.validators = {
|
|
4
|
+
required: this.validateRequired,
|
|
5
|
+
email: this.validateEmail,
|
|
6
|
+
url: this.validateUrl,
|
|
7
|
+
minLength: this.validateMinLength,
|
|
8
|
+
maxLength: this.validateMaxLength,
|
|
9
|
+
pattern: this.validatePattern,
|
|
10
|
+
min: this.validateMin,
|
|
11
|
+
max: this.validateMax,
|
|
12
|
+
equalTo: this.validateEqualTo,
|
|
13
|
+
phone: this.validatePhone,
|
|
14
|
+
date: this.validateDate,
|
|
15
|
+
number: this.validateNumber
|
|
16
|
+
};
|
|
17
|
+
|
|
18
|
+
this.customValidators = {};
|
|
19
|
+
this.asyncValidators = {};
|
|
20
|
+
this.customAsyncValidators = {};
|
|
21
|
+
this.formStates = {};
|
|
22
|
+
this.submitting = new Set();
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
addValidator(name, callback) {
|
|
26
|
+
this.customValidators[name] = callback;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
addAsyncValidator(name, callback) {
|
|
30
|
+
this.customAsyncValidators[name] = callback;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
validate(form) {
|
|
34
|
+
const formId = form.id || `form-${Math.random().toString(36).substr(2, 9)}`;
|
|
35
|
+
const errors = {};
|
|
36
|
+
const inputs = form.querySelectorAll('[data-validate]');
|
|
37
|
+
let hasError = false;
|
|
38
|
+
|
|
39
|
+
inputs.forEach(input => {
|
|
40
|
+
const name = input.name || input.id;
|
|
41
|
+
const rules = this.parseRules(input.getAttribute('data-validate'));
|
|
42
|
+
const value = this.getValue(input);
|
|
43
|
+
|
|
44
|
+
for (const [rule, params] of Object.entries(rules)) {
|
|
45
|
+
const validatorFunc = this.customValidators[rule] || this.validators[rule];
|
|
46
|
+
const isValid = validatorFunc?.(value, params);
|
|
47
|
+
if (!isValid) {
|
|
48
|
+
errors[name] = this.getErrorMessage(rule, params, input);
|
|
49
|
+
this.showError(input, errors[name]);
|
|
50
|
+
hasError = true;
|
|
51
|
+
break;
|
|
52
|
+
} else {
|
|
53
|
+
this.clearError(input);
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
});
|
|
57
|
+
|
|
58
|
+
this.formStates[formId] = {
|
|
59
|
+
valid: !hasError,
|
|
60
|
+
errors: errors,
|
|
61
|
+
errorCount: Object.keys(errors).length
|
|
62
|
+
};
|
|
63
|
+
|
|
64
|
+
this.updateFormState(form);
|
|
65
|
+
|
|
66
|
+
return !hasError;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
getValue(input) {
|
|
70
|
+
if (input.classList.contains('ds-datepicker__input') || input.classList.contains('ds-timepicker__input')) {
|
|
71
|
+
return input.value.trim();
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
if (input.closest('.ds-select')) {
|
|
75
|
+
const select = input.closest('.ds-select');
|
|
76
|
+
const valueEl = select.querySelector('.ds-select__value') || select.querySelector('.ds-select__trigger span');
|
|
77
|
+
return valueEl ? valueEl.textContent.trim() : '';
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
if (input.closest('.ds-fileupload')) {
|
|
81
|
+
const upload = input.closest('.ds-fileupload');
|
|
82
|
+
const uploadInstance = upload.__fileUploadInstance;
|
|
83
|
+
return uploadInstance && uploadInstance.getFiles().length > 0 ? 'has-files' : '';
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
return input.value.trim();
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
validateInput(input) {
|
|
90
|
+
const rules = this.parseRules(input.getAttribute('data-validate'));
|
|
91
|
+
const value = this.getValue(input);
|
|
92
|
+
|
|
93
|
+
for (const [rule, params] of Object.entries(rules)) {
|
|
94
|
+
const validatorFunc = this.customValidators[rule] || this.validators[rule];
|
|
95
|
+
const isValid = validatorFunc?.(value, params);
|
|
96
|
+
if (!isValid) {
|
|
97
|
+
this.showError(input, this.getErrorMessage(rule, params, input));
|
|
98
|
+
return false;
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
this.clearError(input);
|
|
103
|
+
return true;
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
validateAll() {
|
|
107
|
+
const forms = document.querySelectorAll('form[data-validation]');
|
|
108
|
+
let allValid = true;
|
|
109
|
+
|
|
110
|
+
forms.forEach(form => {
|
|
111
|
+
if (!this.validate(form)) {
|
|
112
|
+
allValid = false;
|
|
113
|
+
}
|
|
114
|
+
});
|
|
115
|
+
|
|
116
|
+
return allValid;
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
async validateAsync(form, options = {}) {
|
|
120
|
+
const formId = form.id || `form-${Math.random().toString(36).substr(2, 9)}`;
|
|
121
|
+
const errors = {};
|
|
122
|
+
const group = options.group;
|
|
123
|
+
const inputs = group
|
|
124
|
+
? form.querySelectorAll(`[data-validate][data-validate-group="${group}"]`)
|
|
125
|
+
: form.querySelectorAll('[data-validate]');
|
|
126
|
+
|
|
127
|
+
let hasError = false;
|
|
128
|
+
|
|
129
|
+
for (const input of inputs) {
|
|
130
|
+
const isValid = await this.validateInputAsync(input);
|
|
131
|
+
if (!isValid) {
|
|
132
|
+
hasError = true;
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
const formErrors = {};
|
|
137
|
+
inputs.forEach(input => {
|
|
138
|
+
const name = input.name || input.id;
|
|
139
|
+
const errorElement = input.parentElement.querySelector('.ds-input__error');
|
|
140
|
+
if (errorElement) {
|
|
141
|
+
formErrors[name] = errorElement.textContent;
|
|
142
|
+
}
|
|
143
|
+
});
|
|
144
|
+
|
|
145
|
+
this.formStates[formId] = {
|
|
146
|
+
valid: !hasError,
|
|
147
|
+
errors: formErrors,
|
|
148
|
+
errorCount: Object.keys(formErrors).length
|
|
149
|
+
};
|
|
150
|
+
|
|
151
|
+
this.updateFormState(form);
|
|
152
|
+
|
|
153
|
+
return !hasError;
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
async validateInputAsync(input) {
|
|
157
|
+
const rules = this.parseRules(input.getAttribute('data-validate'));
|
|
158
|
+
const asyncRules = this.parseRules(input.getAttribute('data-validate-async') || '');
|
|
159
|
+
const value = this.getValue(input);
|
|
160
|
+
|
|
161
|
+
for (const [rule, params] of Object.entries(rules)) {
|
|
162
|
+
const validatorFunc = this.customValidators[rule] || this.validators[rule];
|
|
163
|
+
const isValid = validatorFunc?.(value, params);
|
|
164
|
+
if (!isValid) {
|
|
165
|
+
this.showError(input, this.getErrorMessage(rule, params, input));
|
|
166
|
+
return false;
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
for (const [rule, params] of Object.entries(asyncRules)) {
|
|
171
|
+
const validatorFunc = this.customAsyncValidators[rule] || this.asyncValidators[rule];
|
|
172
|
+
if (validatorFunc) {
|
|
173
|
+
try {
|
|
174
|
+
const isValid = await validatorFunc(value, params, input);
|
|
175
|
+
if (!isValid) {
|
|
176
|
+
this.showError(input, this.getErrorMessage(rule, params, input));
|
|
177
|
+
return false;
|
|
178
|
+
}
|
|
179
|
+
} catch (e) {
|
|
180
|
+
this.showError(input, e.message || 'Validation error');
|
|
181
|
+
return false;
|
|
182
|
+
}
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
this.clearError(input);
|
|
187
|
+
return true;
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
async validateGroup(form, groupName) {
|
|
191
|
+
const inputs = form.querySelectorAll(`[data-validate][data-validate-group="${groupName}"]`);
|
|
192
|
+
let hasError = false;
|
|
193
|
+
|
|
194
|
+
for (const input of inputs) {
|
|
195
|
+
const isValid = await this.validateInputAsync(input);
|
|
196
|
+
if (!isValid) {
|
|
197
|
+
hasError = true;
|
|
198
|
+
}
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
return !hasError;
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
getGroups(form) {
|
|
205
|
+
const groups = new Set();
|
|
206
|
+
form.querySelectorAll('[data-validate-group]').forEach(input => {
|
|
207
|
+
groups.add(input.getAttribute('data-validate-group'));
|
|
208
|
+
});
|
|
209
|
+
return Array.from(groups);
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
getFormState(form) {
|
|
213
|
+
const formId = form.id || `form-${Math.random().toString(36).substr(2, 9)}`;
|
|
214
|
+
return this.formStates[formId] || { valid: true, errors: {}, errorCount: 0, loading: false, submitting: false, disabled: false };
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
updateFormState(form) {
|
|
218
|
+
const state = this.getFormState(form);
|
|
219
|
+
|
|
220
|
+
if (state.valid) {
|
|
221
|
+
form.classList.remove('ds-form--invalid');
|
|
222
|
+
form.classList.add('ds-form--valid');
|
|
223
|
+
} else {
|
|
224
|
+
form.classList.remove('ds-form--valid');
|
|
225
|
+
form.classList.add('ds-form--invalid');
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
if (state.loading) {
|
|
229
|
+
form.classList.add('ds-form--loading');
|
|
230
|
+
} else {
|
|
231
|
+
form.classList.remove('ds-form--loading');
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
if (state.submitting) {
|
|
235
|
+
form.classList.add('ds-form--submitting');
|
|
236
|
+
} else {
|
|
237
|
+
form.classList.remove('ds-form--submitting');
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
if (state.disabled) {
|
|
241
|
+
form.classList.add('ds-form--disabled');
|
|
242
|
+
form.querySelectorAll('input, select, textarea, button').forEach(el => el.disabled = true);
|
|
243
|
+
} else {
|
|
244
|
+
form.classList.remove('ds-form--disabled');
|
|
245
|
+
form.querySelectorAll('input, select, textarea, button').forEach(el => {
|
|
246
|
+
if (!el.hasAttribute('data-permanent-disabled')) {
|
|
247
|
+
el.disabled = false;
|
|
248
|
+
}
|
|
249
|
+
});
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
const statusElement = form.querySelector('.ds-form__status');
|
|
253
|
+
if (statusElement) {
|
|
254
|
+
if (state.errorCount > 0) {
|
|
255
|
+
statusElement.textContent = `${state.errorCount} ${state.errorCount === 1 ? 'error' : 'errors'} found`;
|
|
256
|
+
statusElement.classList.add('ds-form__status--error');
|
|
257
|
+
} else {
|
|
258
|
+
statusElement.textContent = 'All fields are valid';
|
|
259
|
+
statusElement.classList.remove('ds-form__status--error');
|
|
260
|
+
}
|
|
261
|
+
}
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
setFormLoading(form, loading) {
|
|
265
|
+
const formId = form.id || `form-${Math.random().toString(36).substr(2, 9)}`;
|
|
266
|
+
if (!this.formStates[formId]) {
|
|
267
|
+
this.formStates[formId] = { valid: true, errors: {}, errorCount: 0, loading: false, submitting: false, disabled: false };
|
|
268
|
+
}
|
|
269
|
+
this.formStates[formId].loading = loading;
|
|
270
|
+
this.updateFormState(form);
|
|
271
|
+
}
|
|
272
|
+
|
|
273
|
+
setFormSubmitting(form, submitting) {
|
|
274
|
+
const formId = form.id || `form-${Math.random().toString(36).substr(2, 9)}`;
|
|
275
|
+
if (!this.formStates[formId]) {
|
|
276
|
+
this.formStates[formId] = { valid: true, errors: {}, errorCount: 0, loading: false, submitting: false, disabled: false };
|
|
277
|
+
}
|
|
278
|
+
this.formStates[formId].submitting = submitting;
|
|
279
|
+
this.updateFormState(form);
|
|
280
|
+
}
|
|
281
|
+
|
|
282
|
+
setFormDisabled(form, disabled) {
|
|
283
|
+
const formId = form.id || `form-${Math.random().toString(36).substr(2, 9)}`;
|
|
284
|
+
if (!this.formStates[formId]) {
|
|
285
|
+
this.formStates[formId] = { valid: true, errors: {}, errorCount: 0, loading: false, submitting: false, disabled: false };
|
|
286
|
+
}
|
|
287
|
+
this.formStates[formId].disabled = disabled;
|
|
288
|
+
this.updateFormState(form);
|
|
289
|
+
}
|
|
290
|
+
|
|
291
|
+
resetForm(form) {
|
|
292
|
+
const formId = form.id || `form-${Math.random().toString(36).substr(2, 9)}`;
|
|
293
|
+
this.formStates[formId] = { valid: true, errors: {}, errorCount: 0, loading: false, submitting: false, disabled: false };
|
|
294
|
+
form.reset();
|
|
295
|
+
form.querySelectorAll('.ds-input--error').forEach(el => {
|
|
296
|
+
el.classList.remove('ds-input--error');
|
|
297
|
+
const errorElement = el.parentElement?.querySelector('.ds-input__error');
|
|
298
|
+
if (errorElement) {
|
|
299
|
+
errorElement.textContent = '';
|
|
300
|
+
}
|
|
301
|
+
});
|
|
302
|
+
this.updateFormState(form);
|
|
303
|
+
}
|
|
304
|
+
|
|
305
|
+
parseRules(rulesString) {
|
|
306
|
+
const rules = {};
|
|
307
|
+
const parts = rulesString.split('|');
|
|
308
|
+
|
|
309
|
+
parts.forEach(part => {
|
|
310
|
+
const [rule, params] = part.split(':');
|
|
311
|
+
rules[rule] = params ? params.split(',') : [];
|
|
312
|
+
});
|
|
313
|
+
|
|
314
|
+
return rules;
|
|
315
|
+
}
|
|
316
|
+
|
|
317
|
+
validateRequired(value) {
|
|
318
|
+
return value !== '';
|
|
319
|
+
}
|
|
320
|
+
|
|
321
|
+
validateEmail(value) {
|
|
322
|
+
const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
|
|
323
|
+
return emailRegex.test(value);
|
|
324
|
+
}
|
|
325
|
+
|
|
326
|
+
validateUrl(value) {
|
|
327
|
+
const urlRegex = /^(https?:\/\/)?([\da-z.-]+)\.([a-z.]{2,6})([/\w .-]*)*\/?$/;
|
|
328
|
+
return urlRegex.test(value);
|
|
329
|
+
}
|
|
330
|
+
|
|
331
|
+
validateMinLength(value, [min]) {
|
|
332
|
+
return value.length >= parseInt(min);
|
|
333
|
+
}
|
|
334
|
+
|
|
335
|
+
validateMaxLength(value, [max]) {
|
|
336
|
+
return value.length <= parseInt(max);
|
|
337
|
+
}
|
|
338
|
+
|
|
339
|
+
validatePattern(value, [pattern]) {
|
|
340
|
+
const regex = new RegExp(pattern);
|
|
341
|
+
return regex.test(value);
|
|
342
|
+
}
|
|
343
|
+
|
|
344
|
+
validateMin(value, [min]) {
|
|
345
|
+
return parseFloat(value) >= parseFloat(min);
|
|
346
|
+
}
|
|
347
|
+
|
|
348
|
+
validateMax(value, [max]) {
|
|
349
|
+
return parseFloat(value) <= parseFloat(max);
|
|
350
|
+
}
|
|
351
|
+
|
|
352
|
+
validateEqualTo(value, [targetId]) {
|
|
353
|
+
const target = document.getElementById(targetId);
|
|
354
|
+
return target && value === target.value;
|
|
355
|
+
}
|
|
356
|
+
|
|
357
|
+
validatePhone(value) {
|
|
358
|
+
const phoneRegex = /^[\d\s\-\+\(\)]{7,20}$/;
|
|
359
|
+
return phoneRegex.test(value);
|
|
360
|
+
}
|
|
361
|
+
|
|
362
|
+
validateDate(value) {
|
|
363
|
+
const dateRegex = /^\d{4}[-/]\d{2}[-/]\d{2}$/;
|
|
364
|
+
return dateRegex.test(value) && !isNaN(Date.parse(value));
|
|
365
|
+
}
|
|
366
|
+
|
|
367
|
+
validateNumber(value) {
|
|
368
|
+
return !isNaN(parseFloat(value)) && isFinite(value);
|
|
369
|
+
}
|
|
370
|
+
|
|
371
|
+
showError(input, message) {
|
|
372
|
+
input.classList.add('ds-input--error');
|
|
373
|
+
input.classList.remove('ds-input--success');
|
|
374
|
+
input.setAttribute('aria-invalid', 'true');
|
|
375
|
+
|
|
376
|
+
let errorElement = input.parentElement.querySelector('.ds-input__error');
|
|
377
|
+
if (!errorElement) {
|
|
378
|
+
errorElement = document.createElement('span');
|
|
379
|
+
errorElement.className = 'ds-input__error';
|
|
380
|
+
errorElement.setAttribute('role', 'alert');
|
|
381
|
+
errorElement.setAttribute('aria-live', 'polite');
|
|
382
|
+
input.parentElement.appendChild(errorElement);
|
|
383
|
+
}
|
|
384
|
+
errorElement.textContent = message;
|
|
385
|
+
|
|
386
|
+
this.removeStatusIcon(input);
|
|
387
|
+
|
|
388
|
+
input.dispatchEvent(new CustomEvent('validation-error', { detail: { message } }));
|
|
389
|
+
}
|
|
390
|
+
|
|
391
|
+
clearError(input) {
|
|
392
|
+
input.classList.remove('ds-input--error');
|
|
393
|
+
input.setAttribute('aria-invalid', 'false');
|
|
394
|
+
|
|
395
|
+
const errorElement = input.parentElement.querySelector('.ds-input__error');
|
|
396
|
+
if (errorElement) {
|
|
397
|
+
errorElement.remove();
|
|
398
|
+
}
|
|
399
|
+
|
|
400
|
+
input.dispatchEvent(new CustomEvent('validation-success'));
|
|
401
|
+
}
|
|
402
|
+
|
|
403
|
+
showSuccess(input) {
|
|
404
|
+
input.classList.add('ds-input--success');
|
|
405
|
+
input.classList.remove('ds-input--error');
|
|
406
|
+
input.setAttribute('aria-invalid', 'false');
|
|
407
|
+
|
|
408
|
+
this.removeStatusIcon(input);
|
|
409
|
+
|
|
410
|
+
const icon = document.createElement('span');
|
|
411
|
+
icon.className = 'ds-input__status-icon ds-input__status-icon--success';
|
|
412
|
+
icon.innerHTML = '<svg width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"><polyline points="20 6 9 17 4 12"/></svg>';
|
|
413
|
+
|
|
414
|
+
input.parentElement.appendChild(icon);
|
|
415
|
+
}
|
|
416
|
+
|
|
417
|
+
removeStatusIcon(input) {
|
|
418
|
+
const icon = input.parentElement.querySelector('.ds-input__status-icon');
|
|
419
|
+
if (icon) {
|
|
420
|
+
icon.remove();
|
|
421
|
+
}
|
|
422
|
+
}
|
|
423
|
+
|
|
424
|
+
getErrorMessage(rule, params, input) {
|
|
425
|
+
const customMessage = input.getAttribute(`data-message-${rule}`);
|
|
426
|
+
if (customMessage) return customMessage;
|
|
427
|
+
|
|
428
|
+
const messages = {
|
|
429
|
+
required: 'This field is required',
|
|
430
|
+
email: 'Please enter a valid email address',
|
|
431
|
+
url: 'Please enter a valid URL',
|
|
432
|
+
minLength: `Minimum length is ${params[0]} characters`,
|
|
433
|
+
maxLength: `Maximum length is ${params[0]} characters`,
|
|
434
|
+
pattern: 'Please enter a valid value',
|
|
435
|
+
min: `Minimum value is ${params[0]}`,
|
|
436
|
+
max: `Maximum value is ${params[0]}`,
|
|
437
|
+
equalTo: 'Values do not match',
|
|
438
|
+
phone: 'Please enter a valid phone number',
|
|
439
|
+
date: 'Please enter a valid date (YYYY-MM-DD)',
|
|
440
|
+
number: 'Please enter a valid number'
|
|
441
|
+
};
|
|
442
|
+
return messages[rule] || 'Invalid input';
|
|
443
|
+
}
|
|
444
|
+
}
|
|
445
|
+
|
|
446
|
+
const validator = new KupolaValidator();
|
|
447
|
+
|
|
448
|
+
if (!window.__kupolaValidationInitialized) {
|
|
449
|
+
window.__kupolaValidationInitialized = true;
|
|
450
|
+
document.addEventListener('DOMContentLoaded', () => {
|
|
451
|
+
document.querySelectorAll('form[data-validation]').forEach(form => {
|
|
452
|
+
form.addEventListener('submit', async (e) => {
|
|
453
|
+
const formId = form.id || `form-${Math.random().toString(36).substr(2, 9)}`;
|
|
454
|
+
|
|
455
|
+
if (validator.submitting.has(formId)) {
|
|
456
|
+
e.preventDefault();
|
|
457
|
+
return;
|
|
458
|
+
}
|
|
459
|
+
|
|
460
|
+
e.preventDefault();
|
|
461
|
+
|
|
462
|
+
const hasAsyncValidation = form.querySelector('[data-validate-async]') !== null;
|
|
463
|
+
|
|
464
|
+
let isValid;
|
|
465
|
+
if (hasAsyncValidation) {
|
|
466
|
+
isValid = await validator.validateAsync(form);
|
|
467
|
+
} else {
|
|
468
|
+
isValid = validator.validate(form);
|
|
469
|
+
}
|
|
470
|
+
|
|
471
|
+
if (!isValid) {
|
|
472
|
+
const firstError = form.querySelector('.ds-input--error');
|
|
473
|
+
if (firstError) {
|
|
474
|
+
firstError.focus();
|
|
475
|
+
}
|
|
476
|
+
} else {
|
|
477
|
+
validator.submitting.add(formId);
|
|
478
|
+
const submitBtn = form.querySelector('button[type="submit"]');
|
|
479
|
+
if (submitBtn) {
|
|
480
|
+
const originalText = submitBtn.textContent;
|
|
481
|
+
submitBtn.setAttribute('data-original-text', originalText);
|
|
482
|
+
submitBtn.textContent = 'Submitting...';
|
|
483
|
+
submitBtn.disabled = true;
|
|
484
|
+
}
|
|
485
|
+
|
|
486
|
+
try {
|
|
487
|
+
const onSubmit = form.getAttribute('data-on-submit');
|
|
488
|
+
if (onSubmit && window[onSubmit]) {
|
|
489
|
+
await window[onSubmit](form);
|
|
490
|
+
} else {
|
|
491
|
+
form.submit();
|
|
492
|
+
}
|
|
493
|
+
} finally {
|
|
494
|
+
validator.submitting.delete(formId);
|
|
495
|
+
if (submitBtn) {
|
|
496
|
+
submitBtn.textContent = submitBtn.getAttribute('data-original-text') || 'Submit';
|
|
497
|
+
submitBtn.disabled = false;
|
|
498
|
+
}
|
|
499
|
+
}
|
|
500
|
+
}
|
|
501
|
+
});
|
|
502
|
+
|
|
503
|
+
form.querySelectorAll('[data-validate]').forEach(input => {
|
|
504
|
+
input.addEventListener('blur', () => {
|
|
505
|
+
setTimeout(() => {
|
|
506
|
+
const activeElement = document.activeElement;
|
|
507
|
+
if (activeElement && activeElement.closest('.ds-select')) {
|
|
508
|
+
return;
|
|
509
|
+
}
|
|
510
|
+
const isValid = validator.validateInput(input);
|
|
511
|
+
if (isValid && input.value.trim()) {
|
|
512
|
+
validator.showSuccess(input);
|
|
513
|
+
}
|
|
514
|
+
}, 50);
|
|
515
|
+
});
|
|
516
|
+
|
|
517
|
+
const debounce = (func, wait) => {
|
|
518
|
+
let timeout;
|
|
519
|
+
return (...args) => {
|
|
520
|
+
clearTimeout(timeout);
|
|
521
|
+
timeout = setTimeout(() => func.apply(this, args), wait);
|
|
522
|
+
};
|
|
523
|
+
};
|
|
524
|
+
|
|
525
|
+
const validateOnInput = debounce(() => {
|
|
526
|
+
const value = validator.getValue(input);
|
|
527
|
+
if (value.length > 0 || input.classList.contains('ds-input--error')) {
|
|
528
|
+
const isValid = validator.validateInput(input);
|
|
529
|
+
if (isValid && value) {
|
|
530
|
+
validator.showSuccess(input);
|
|
531
|
+
}
|
|
532
|
+
} else {
|
|
533
|
+
validator.removeStatusIcon(input);
|
|
534
|
+
}
|
|
535
|
+
}, 300);
|
|
536
|
+
|
|
537
|
+
input.addEventListener('input', validateOnInput);
|
|
538
|
+
|
|
539
|
+
input.addEventListener('keyup', (e) => {
|
|
540
|
+
if (e.key === 'Enter') {
|
|
541
|
+
const isValid = validator.validateInput(input);
|
|
542
|
+
if (isValid && input.value.trim()) {
|
|
543
|
+
validator.showSuccess(input);
|
|
544
|
+
}
|
|
545
|
+
}
|
|
546
|
+
});
|
|
547
|
+
});
|
|
548
|
+
});
|
|
549
|
+
});
|
|
550
|
+
}
|
|
551
|
+
|
|
552
|
+
window.KupolaValidator = KupolaValidator;
|
|
553
|
+
window.kupolaValidator = validator;
|