@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/fileupload.js
ADDED
|
@@ -0,0 +1,392 @@
|
|
|
1
|
+
class FileUpload {
|
|
2
|
+
constructor(element) {
|
|
3
|
+
this.element = element;
|
|
4
|
+
this.dropzone = element.querySelector('.ds-fileupload__dropzone');
|
|
5
|
+
this.input = element.querySelector('.ds-fileupload__input');
|
|
6
|
+
this.list = element.querySelector('.ds-fileupload__list');
|
|
7
|
+
this.progress = element.querySelector('.ds-fileupload__preview');
|
|
8
|
+
|
|
9
|
+
this.files = [];
|
|
10
|
+
this.maxSize = parseInt(element.getAttribute('data-max-size')) || 0;
|
|
11
|
+
this.maxCount = parseInt(element.getAttribute('data-max-count')) || 0;
|
|
12
|
+
this._listeners = [];
|
|
13
|
+
|
|
14
|
+
this.init();
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
init() {
|
|
18
|
+
this.bindEvents();
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
bindEvents() {
|
|
22
|
+
const clickHandler = (e) => {
|
|
23
|
+
if (e.target === this.input || this.input.contains(e.target)) return;
|
|
24
|
+
this.input.click();
|
|
25
|
+
};
|
|
26
|
+
|
|
27
|
+
const changeHandler = (e) => {
|
|
28
|
+
const files = Array.from(e.target.files);
|
|
29
|
+
this.addFiles(files);
|
|
30
|
+
e.target.value = '';
|
|
31
|
+
};
|
|
32
|
+
|
|
33
|
+
const dragoverHandler = (e) => {
|
|
34
|
+
e.preventDefault();
|
|
35
|
+
e.stopPropagation();
|
|
36
|
+
this.dropzone.classList.add('is-dragging');
|
|
37
|
+
};
|
|
38
|
+
|
|
39
|
+
const dragleaveHandler = (e) => {
|
|
40
|
+
e.preventDefault();
|
|
41
|
+
e.stopPropagation();
|
|
42
|
+
this.dropzone.classList.remove('is-dragging');
|
|
43
|
+
};
|
|
44
|
+
|
|
45
|
+
const dropHandler = (e) => {
|
|
46
|
+
e.preventDefault();
|
|
47
|
+
e.stopPropagation();
|
|
48
|
+
this.dropzone.classList.remove('is-dragging');
|
|
49
|
+
|
|
50
|
+
const files = Array.from(e.dataTransfer.files);
|
|
51
|
+
this.addFiles(files);
|
|
52
|
+
};
|
|
53
|
+
|
|
54
|
+
this.dropzone.addEventListener('click', clickHandler);
|
|
55
|
+
this.input.addEventListener('change', changeHandler);
|
|
56
|
+
this.dropzone.addEventListener('dragover', dragoverHandler);
|
|
57
|
+
this.dropzone.addEventListener('dragleave', dragleaveHandler);
|
|
58
|
+
this.dropzone.addEventListener('drop', dropHandler);
|
|
59
|
+
|
|
60
|
+
this._listeners.push(
|
|
61
|
+
{ el: this.dropzone, event: 'click', handler: clickHandler },
|
|
62
|
+
{ el: this.input, event: 'change', handler: changeHandler },
|
|
63
|
+
{ el: this.dropzone, event: 'dragover', handler: dragoverHandler },
|
|
64
|
+
{ el: this.dropzone, event: 'dragleave', handler: dragleaveHandler },
|
|
65
|
+
{ el: this.dropzone, event: 'drop', handler: dropHandler }
|
|
66
|
+
);
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
addFiles(files) {
|
|
70
|
+
files.forEach(file => {
|
|
71
|
+
if (this.maxCount > 0 && this.files.length >= this.maxCount) {
|
|
72
|
+
this.showError(`Maximum ${this.maxCount} files allowed`);
|
|
73
|
+
return;
|
|
74
|
+
}
|
|
75
|
+
if (this.isValidFile(file)) {
|
|
76
|
+
this.files.push(file);
|
|
77
|
+
this.renderFileItem(file);
|
|
78
|
+
this.showPreview(file);
|
|
79
|
+
}
|
|
80
|
+
});
|
|
81
|
+
|
|
82
|
+
this.dispatchChange();
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
isValidFile(file) {
|
|
86
|
+
const accept = this.input.getAttribute('accept');
|
|
87
|
+
if (accept && accept !== '') {
|
|
88
|
+
const types = accept.split(',').map(t => t.trim());
|
|
89
|
+
const fileType = file.type;
|
|
90
|
+
const fileName = file.name.toLowerCase();
|
|
91
|
+
|
|
92
|
+
const isValidType = types.some(type => {
|
|
93
|
+
if (type.startsWith('.')) {
|
|
94
|
+
return fileName.endsWith(type);
|
|
95
|
+
} else if (type.includes('/')) {
|
|
96
|
+
if (type.endsWith('/*')) {
|
|
97
|
+
return fileType.startsWith(type.replace('/*', ''));
|
|
98
|
+
}
|
|
99
|
+
return fileType === type;
|
|
100
|
+
}
|
|
101
|
+
return true;
|
|
102
|
+
});
|
|
103
|
+
|
|
104
|
+
if (!isValidType) {
|
|
105
|
+
this.showError(`File type not allowed: ${file.type}`);
|
|
106
|
+
return false;
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
if (this.maxSize > 0 && file.size > this.maxSize) {
|
|
111
|
+
this.showError(`File size exceeds ${this.formatSize(this.maxSize)}`);
|
|
112
|
+
return false;
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
return true;
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
renderFileItem(file) {
|
|
119
|
+
const item = document.createElement('div');
|
|
120
|
+
item.className = 'ds-fileupload__item';
|
|
121
|
+
item.dataset.filename = file.name;
|
|
122
|
+
|
|
123
|
+
const fileIcon = this.getFileIcon(file.type);
|
|
124
|
+
|
|
125
|
+
item.innerHTML = `
|
|
126
|
+
<div class="ds-fileupload__icon" style="width: 24px; height: 24px; border-radius: 4px;">
|
|
127
|
+
${fileIcon}
|
|
128
|
+
</div>
|
|
129
|
+
<span class="ds-fileupload__filename">${this.truncateFilename(file.name)}</span>
|
|
130
|
+
<span class="ds-fileupload__size">${this.formatSize(file.size)}</span>
|
|
131
|
+
<button class="ds-fileupload__remove" type="button" aria-label="Remove file">
|
|
132
|
+
<svg width="14" height="14" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2">
|
|
133
|
+
<path d="M18 6L6 18"/>
|
|
134
|
+
<path d="M6 6l12 12"/>
|
|
135
|
+
</svg>
|
|
136
|
+
</button>
|
|
137
|
+
`;
|
|
138
|
+
|
|
139
|
+
const removeBtn = item.querySelector('.ds-fileupload__remove');
|
|
140
|
+
const removeHandler = () => {
|
|
141
|
+
this.removeFile(file, item);
|
|
142
|
+
};
|
|
143
|
+
removeBtn.addEventListener('click', removeHandler);
|
|
144
|
+
|
|
145
|
+
this._listeners.push({ el: removeBtn, event: 'click', handler: removeHandler });
|
|
146
|
+
|
|
147
|
+
if (!this.list) {
|
|
148
|
+
this.list = document.createElement('div');
|
|
149
|
+
this.list.className = 'ds-fileupload__list';
|
|
150
|
+
this.element.appendChild(this.list);
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
this.list.appendChild(item);
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
getFileIcon(type) {
|
|
157
|
+
if (type.startsWith('image/')) {
|
|
158
|
+
return '<svg width="14" height="14" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"><rect x="3" y="3" width="18" height="18" rx="2" ry="2"/><circle cx="8.5" cy="8.5" r="1.5"/><polyline points="21 15 16 10 5 21"/></svg>';
|
|
159
|
+
}
|
|
160
|
+
if (type.startsWith('video/')) {
|
|
161
|
+
return '<svg width="14" height="14" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"><polygon points="23 7 16 12 23 17 23 7"/><rect x="1" y="5" width="15" height="14" rx="2" ry="2"/></svg>';
|
|
162
|
+
}
|
|
163
|
+
if (type.startsWith('audio/')) {
|
|
164
|
+
return '<svg width="14" height="14" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"><polygon points="11 5 6 9 2 9 2 15 6 15 11 19 11 5"/><path d="M19.07 4.93a10 10 0 0 1 0 14.14M15.54 8.46a5 5 0 0 1 0 7.07"/></svg>';
|
|
165
|
+
}
|
|
166
|
+
if (type.includes('pdf')) {
|
|
167
|
+
return '<svg width="14" height="14" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"><path d="M14 2H6a2 2 0 0 0-2 2v16a2 2 0 0 0 2 2h12a2 2 0 0 0 2-2V8z"/><polyline points="14 2 14 8 20 8"/><line x1="16" y1="13" x2="8" y2="13"/><line x1="16" y1="17" x2="8" y2="17"/><polyline points="10 9 9 9 8 9"/></svg>';
|
|
168
|
+
}
|
|
169
|
+
if (type.includes('document') || type.includes('text')) {
|
|
170
|
+
return '<svg width="14" height="14" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"><path d="M14 2H6a2 2 0 0 0-2 2v16a2 2 0 0 0 2 2h12a2 2 0 0 0 2-2V8z"/><polyline points="14 2 14 8 20 8"/><line x1="16" y1="13" x2="8" y2="13"/><line x1="16" y1="17" x2="8" y2="17"/><polyline points="10 9 9 9 8 9"/></svg>';
|
|
171
|
+
}
|
|
172
|
+
if (type.includes('zip') || type.includes('archive')) {
|
|
173
|
+
return '<svg width="14" height="14" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"><rect x="3" y="11" width="18" height="11" rx="2" ry="2"/><path d="M16 11V7a4 4 0 0 0-8 0v4"/><polyline points="10 14 8 16 6 14"/></svg>';
|
|
174
|
+
}
|
|
175
|
+
return '<svg width="14" height="14" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"><path d="M14 2H6a2 2 0 0 0-2 2v16a2 2 0 0 0 2 2h12a2 2 0 0 0 2-2V8z"/><polyline points="14 2 14 8 20 8"/></svg>';
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
truncateFilename(name, maxLength = 20) {
|
|
179
|
+
if (name.length <= maxLength) return name;
|
|
180
|
+
const extension = name.substring(name.lastIndexOf('.'));
|
|
181
|
+
const nameWithoutExt = name.substring(0, name.lastIndexOf('.'));
|
|
182
|
+
const truncated = nameWithoutExt.substring(0, maxLength - extension.length - 3) + '...';
|
|
183
|
+
return truncated + extension;
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
formatSize(bytes) {
|
|
187
|
+
if (bytes === 0) return '0 B';
|
|
188
|
+
const k = 1024;
|
|
189
|
+
const sizes = ['B', 'KB', 'MB', 'GB'];
|
|
190
|
+
const i = Math.floor(Math.log(bytes) / Math.log(k));
|
|
191
|
+
return parseFloat((bytes / Math.pow(k, i)).toFixed(1)) + ' ' + sizes[i];
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
removeFile(file, itemElement) {
|
|
195
|
+
this.files = this.files.filter(f => f !== file);
|
|
196
|
+
if (itemElement) {
|
|
197
|
+
itemElement.remove();
|
|
198
|
+
}
|
|
199
|
+
if (this.files.length === 0 && this.list) {
|
|
200
|
+
this.list.remove();
|
|
201
|
+
this.list = null;
|
|
202
|
+
}
|
|
203
|
+
this.dispatchChange();
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
clearFiles() {
|
|
207
|
+
this.files = [];
|
|
208
|
+
if (this.list) {
|
|
209
|
+
this.list.remove();
|
|
210
|
+
this.list = null;
|
|
211
|
+
}
|
|
212
|
+
if (this.preview) {
|
|
213
|
+
this.preview.innerHTML = '';
|
|
214
|
+
}
|
|
215
|
+
this.clearError();
|
|
216
|
+
this.dispatchChange();
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
showError(message) {
|
|
220
|
+
this.clearError();
|
|
221
|
+
this.dropzone.classList.add('is-error');
|
|
222
|
+
|
|
223
|
+
const errorElement = document.createElement('div');
|
|
224
|
+
errorElement.className = 'ds-fileupload__error';
|
|
225
|
+
errorElement.textContent = message;
|
|
226
|
+
errorElement.setAttribute('role', 'alert');
|
|
227
|
+
errorElement.setAttribute('aria-live', 'polite');
|
|
228
|
+
this.dropzone.appendChild(errorElement);
|
|
229
|
+
|
|
230
|
+
setTimeout(() => {
|
|
231
|
+
this.clearError();
|
|
232
|
+
}, 5000);
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
clearError() {
|
|
236
|
+
this.dropzone.classList.remove('is-error');
|
|
237
|
+
const errorElement = this.dropzone.querySelector('.ds-fileupload__error');
|
|
238
|
+
if (errorElement) {
|
|
239
|
+
errorElement.remove();
|
|
240
|
+
}
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
showPreview(file) {
|
|
244
|
+
if (!file.type.startsWith('image/')) return;
|
|
245
|
+
|
|
246
|
+
if (!this.preview) {
|
|
247
|
+
this.preview = document.createElement('div');
|
|
248
|
+
this.preview.className = 'ds-fileupload__preview';
|
|
249
|
+
this.element.insertBefore(this.preview, this.list || null);
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
const reader = new FileReader();
|
|
253
|
+
reader.onload = (e) => {
|
|
254
|
+
const previewItem = document.createElement('div');
|
|
255
|
+
previewItem.className = 'ds-fileupload__preview-item';
|
|
256
|
+
previewItem.innerHTML = `
|
|
257
|
+
<img src="${e.target.result}" alt="${file.name}">
|
|
258
|
+
<button class="ds-fileupload__preview-remove" type="button" aria-label="Remove preview">
|
|
259
|
+
<svg width="14" height="14" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2">
|
|
260
|
+
<path d="M18 6L6 18"/>
|
|
261
|
+
<path d="M6 6l12 12"/>
|
|
262
|
+
</svg>
|
|
263
|
+
</button>
|
|
264
|
+
`;
|
|
265
|
+
|
|
266
|
+
const previewRemoveBtn = previewItem.querySelector('.ds-fileupload__preview-remove');
|
|
267
|
+
const previewRemoveHandler = () => {
|
|
268
|
+
this.removeFile(file, this.list?.querySelector(`[data-filename="${file.name}"]`));
|
|
269
|
+
previewItem.remove();
|
|
270
|
+
if (this.preview && this.preview.children.length === 0) {
|
|
271
|
+
this.preview.remove();
|
|
272
|
+
this.preview = null;
|
|
273
|
+
}
|
|
274
|
+
};
|
|
275
|
+
previewRemoveBtn.addEventListener('click', previewRemoveHandler);
|
|
276
|
+
this._listeners.push({ el: previewRemoveBtn, event: 'click', handler: previewRemoveHandler });
|
|
277
|
+
|
|
278
|
+
this.preview.appendChild(previewItem);
|
|
279
|
+
};
|
|
280
|
+
reader.readAsDataURL(file);
|
|
281
|
+
}
|
|
282
|
+
|
|
283
|
+
updateProgress(percent) {
|
|
284
|
+
if (!this.progress) {
|
|
285
|
+
this.progress = document.createElement('div');
|
|
286
|
+
this.progress.className = 'ds-fileupload__progress';
|
|
287
|
+
this.element.insertBefore(this.progress, this.list || null);
|
|
288
|
+
}
|
|
289
|
+
|
|
290
|
+
this.progress.style.display = 'block';
|
|
291
|
+
|
|
292
|
+
const bar = this.progress.querySelector('.ds-fileupload__progress-bar') || document.createElement('div');
|
|
293
|
+
bar.className = 'ds-fileupload__progress-bar';
|
|
294
|
+
bar.style.width = `${percent}%`;
|
|
295
|
+
|
|
296
|
+
if (!this.progress.querySelector('.ds-fileupload__progress-bar')) {
|
|
297
|
+
this.progress.appendChild(bar);
|
|
298
|
+
}
|
|
299
|
+
|
|
300
|
+
if (percent >= 100) {
|
|
301
|
+
setTimeout(() => {
|
|
302
|
+
if (this.progress) {
|
|
303
|
+
this.progress.remove();
|
|
304
|
+
this.progress = null;
|
|
305
|
+
}
|
|
306
|
+
}, 500);
|
|
307
|
+
}
|
|
308
|
+
}
|
|
309
|
+
|
|
310
|
+
simulateUpload(file) {
|
|
311
|
+
this.updateProgress(0);
|
|
312
|
+
const totalChunks = 100;
|
|
313
|
+
let currentChunk = 0;
|
|
314
|
+
const chunkSize = Math.max(1, Math.floor(file.size / totalChunks));
|
|
315
|
+
|
|
316
|
+
const uploadInterval = setInterval(() => {
|
|
317
|
+
currentChunk++;
|
|
318
|
+
const percent = Math.min(100, Math.floor((currentChunk / totalChunks) * 100));
|
|
319
|
+
this.updateProgress(percent);
|
|
320
|
+
|
|
321
|
+
if (currentChunk >= totalChunks) {
|
|
322
|
+
clearInterval(uploadInterval);
|
|
323
|
+
this.updateProgress(100);
|
|
324
|
+
}
|
|
325
|
+
}, Math.max(50, Math.floor(5000 / totalChunks)));
|
|
326
|
+
|
|
327
|
+
return uploadInterval;
|
|
328
|
+
}
|
|
329
|
+
|
|
330
|
+
getFiles() {
|
|
331
|
+
return [...this.files];
|
|
332
|
+
}
|
|
333
|
+
|
|
334
|
+
dispatchChange() {
|
|
335
|
+
this.element.dispatchEvent(new CustomEvent('ds-fileupload-change', {
|
|
336
|
+
detail: {
|
|
337
|
+
files: this.getFiles(),
|
|
338
|
+
count: this.files.length
|
|
339
|
+
}
|
|
340
|
+
}));
|
|
341
|
+
}
|
|
342
|
+
|
|
343
|
+
destroy() {
|
|
344
|
+
this._listeners.forEach(({ el, event, handler }) => {
|
|
345
|
+
el.removeEventListener(event, handler);
|
|
346
|
+
});
|
|
347
|
+
this._listeners = null;
|
|
348
|
+
this.files = [];
|
|
349
|
+
this.dropzone = null;
|
|
350
|
+
this.input = null;
|
|
351
|
+
this.list = null;
|
|
352
|
+
this.progress = null;
|
|
353
|
+
this.preview = null;
|
|
354
|
+
this.element = null;
|
|
355
|
+
}
|
|
356
|
+
}
|
|
357
|
+
|
|
358
|
+
function initFileUpload(element) {
|
|
359
|
+
if (element.__kupolaInitialized) return;
|
|
360
|
+
|
|
361
|
+
const instance = new FileUpload(element);
|
|
362
|
+
element.__kupolaInstance = instance;
|
|
363
|
+
element.__kupolaInitialized = true;
|
|
364
|
+
}
|
|
365
|
+
|
|
366
|
+
function cleanupFileUpload(element) {
|
|
367
|
+
if (!element.__kupolaInitialized || !element.__kupolaInstance) return;
|
|
368
|
+
|
|
369
|
+
const instance = element.__kupolaInstance;
|
|
370
|
+
instance.destroy();
|
|
371
|
+
|
|
372
|
+
element.__kupolaInstance = null;
|
|
373
|
+
element.__kupolaInitialized = false;
|
|
374
|
+
}
|
|
375
|
+
|
|
376
|
+
function initFileUploads() {
|
|
377
|
+
document.querySelectorAll('.ds-fileupload').forEach(el => {
|
|
378
|
+
initFileUpload(el);
|
|
379
|
+
});
|
|
380
|
+
}
|
|
381
|
+
|
|
382
|
+
if (typeof module !== 'undefined' && module.exports) {
|
|
383
|
+
module.exports = { FileUpload, initFileUploads, initFileUpload, cleanupFileUpload };
|
|
384
|
+
} else {
|
|
385
|
+
window.FileUpload = FileUpload;
|
|
386
|
+
window.initFileUpload = initFileUpload;
|
|
387
|
+
window.cleanupFileUpload = cleanupFileUpload;
|
|
388
|
+
}
|
|
389
|
+
|
|
390
|
+
if (window.kupolaInitializer) {
|
|
391
|
+
window.kupolaInitializer.register('fileupload', initFileUpload, cleanupFileUpload);
|
|
392
|
+
}
|
package/js/form.js
ADDED
|
@@ -0,0 +1,291 @@
|
|
|
1
|
+
(function(window) {
|
|
2
|
+
class KupolaForm {
|
|
3
|
+
constructor(formOrSelector, options = {}) {
|
|
4
|
+
this.form = typeof formOrSelector === 'string'
|
|
5
|
+
? document.querySelector(formOrSelector)
|
|
6
|
+
: formOrSelector;
|
|
7
|
+
|
|
8
|
+
this.options = {
|
|
9
|
+
validateOnSubmit: true,
|
|
10
|
+
validateOnChange: false,
|
|
11
|
+
validateOnBlur: false,
|
|
12
|
+
preventDefault: true,
|
|
13
|
+
...options
|
|
14
|
+
};
|
|
15
|
+
|
|
16
|
+
this.submitHandlers = [];
|
|
17
|
+
this.isSubmitting = false;
|
|
18
|
+
this.errors = {};
|
|
19
|
+
this.rules = {};
|
|
20
|
+
this.validator = window.kupolaValidator || new window.KupolaValidator();
|
|
21
|
+
|
|
22
|
+
if (!this.form || !(this.form instanceof HTMLFormElement)) {
|
|
23
|
+
console.warn('[KupolaForm] Invalid form element');
|
|
24
|
+
return;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
this._init();
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
_init() {
|
|
31
|
+
if (this.options.validateOnChange) {
|
|
32
|
+
this.form.addEventListener('input', (e) => {
|
|
33
|
+
this._validateField(e.target);
|
|
34
|
+
});
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
if (this.options.validateOnBlur) {
|
|
38
|
+
this.form.addEventListener('blur', (e) => {
|
|
39
|
+
if (e.target.form === this.form) {
|
|
40
|
+
this._validateField(e.target);
|
|
41
|
+
}
|
|
42
|
+
}, true);
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
submit(handler) {
|
|
47
|
+
const wrappedHandler = async (e) => {
|
|
48
|
+
if (this.options.preventDefault) {
|
|
49
|
+
e.preventDefault();
|
|
50
|
+
}
|
|
51
|
+
if (this.isSubmitting) return;
|
|
52
|
+
this.isSubmitting = true;
|
|
53
|
+
|
|
54
|
+
try {
|
|
55
|
+
if (this.options.validateOnSubmit) {
|
|
56
|
+
const validation = await this.validate();
|
|
57
|
+
if (!validation.valid) {
|
|
58
|
+
this._showErrors(validation.errors);
|
|
59
|
+
return;
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
const result = handler(e);
|
|
64
|
+
if (result instanceof Promise) {
|
|
65
|
+
await result;
|
|
66
|
+
}
|
|
67
|
+
} finally {
|
|
68
|
+
this.isSubmitting = false;
|
|
69
|
+
}
|
|
70
|
+
};
|
|
71
|
+
|
|
72
|
+
this.submitHandlers.push(wrappedHandler);
|
|
73
|
+
this.form.addEventListener('submit', wrappedHandler);
|
|
74
|
+
|
|
75
|
+
return () => {
|
|
76
|
+
const index = this.submitHandlers.indexOf(wrappedHandler);
|
|
77
|
+
if (index > -1) {
|
|
78
|
+
this.submitHandlers.splice(index, 1);
|
|
79
|
+
this.form.removeEventListener('submit', wrappedHandler);
|
|
80
|
+
}
|
|
81
|
+
};
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
setRules(rules) {
|
|
85
|
+
this.rules = { ...this.rules, ...rules };
|
|
86
|
+
this._applyRulesToDOM();
|
|
87
|
+
return this;
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
_applyRulesToDOM() {
|
|
91
|
+
Object.keys(this.rules).forEach(name => {
|
|
92
|
+
const input = this.form.querySelector(`[name="${name}"]`);
|
|
93
|
+
if (!input) return;
|
|
94
|
+
|
|
95
|
+
const fieldRules = this.rules[name];
|
|
96
|
+
const ruleString = this._rulesToString(fieldRules);
|
|
97
|
+
input.setAttribute('data-validate', ruleString);
|
|
98
|
+
});
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
_rulesToString(rules) {
|
|
102
|
+
if (typeof rules === 'function') {
|
|
103
|
+
return 'custom';
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
const parts = [];
|
|
107
|
+
Object.keys(rules).forEach(rule => {
|
|
108
|
+
const config = rules[rule];
|
|
109
|
+
if (typeof config === 'object' && config !== null) {
|
|
110
|
+
if (rule === 'pattern') {
|
|
111
|
+
parts.push(`pattern:${config.source?.replace(/\//g, '')}`);
|
|
112
|
+
} else if (rule === 'equalTo') {
|
|
113
|
+
parts.push(`equalTo:${config}`);
|
|
114
|
+
} else {
|
|
115
|
+
parts.push(rule);
|
|
116
|
+
}
|
|
117
|
+
} else if (typeof config === 'number' || typeof config === 'string') {
|
|
118
|
+
parts.push(`${rule}:${config}`);
|
|
119
|
+
} else if (config === true) {
|
|
120
|
+
parts.push(rule);
|
|
121
|
+
}
|
|
122
|
+
});
|
|
123
|
+
return parts.join('|');
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
async validate(fields = null) {
|
|
127
|
+
this.errors = {};
|
|
128
|
+
const allErrors = [];
|
|
129
|
+
|
|
130
|
+
const inputs = fields
|
|
131
|
+
? Array.isArray(fields) ? fields : [fields]
|
|
132
|
+
: this.form.querySelectorAll('input, select, textarea');
|
|
133
|
+
|
|
134
|
+
for (const input of inputs) {
|
|
135
|
+
const name = input.name;
|
|
136
|
+
if (!name) continue;
|
|
137
|
+
|
|
138
|
+
const errors = await this._validateField(input);
|
|
139
|
+
if (errors.length > 0) {
|
|
140
|
+
this.errors[name] = errors;
|
|
141
|
+
allErrors.push(...errors);
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
return {
|
|
146
|
+
valid: allErrors.length === 0,
|
|
147
|
+
errors: allErrors,
|
|
148
|
+
fieldErrors: this.errors
|
|
149
|
+
};
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
async _validateField(input) {
|
|
153
|
+
const name = input.name;
|
|
154
|
+
const rules = this.rules[name];
|
|
155
|
+
|
|
156
|
+
if (typeof rules === 'function') {
|
|
157
|
+
const result = rules(input.value.trim(), input);
|
|
158
|
+
if (result instanceof Promise) {
|
|
159
|
+
const asyncResult = await result;
|
|
160
|
+
if (asyncResult !== true) {
|
|
161
|
+
const displayName = input.getAttribute('data-label') || input.getAttribute('placeholder') || name;
|
|
162
|
+
const message = typeof asyncResult === 'string' ? asyncResult : `${displayName} 验证失败`;
|
|
163
|
+
this.validator.showError(input, message);
|
|
164
|
+
return [message];
|
|
165
|
+
}
|
|
166
|
+
} else if (result !== true) {
|
|
167
|
+
const displayName = input.getAttribute('data-label') || input.getAttribute('placeholder') || name;
|
|
168
|
+
const message = typeof result === 'string' ? result : `${displayName} 验证失败`;
|
|
169
|
+
this.validator.showError(input, message);
|
|
170
|
+
return [message];
|
|
171
|
+
}
|
|
172
|
+
this.validator.clearError(input);
|
|
173
|
+
return [];
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
const isValid = await this.validator.validateInputAsync(input);
|
|
177
|
+
if (!isValid) {
|
|
178
|
+
const errorElement = input.parentElement?.querySelector('.ds-input__error');
|
|
179
|
+
return errorElement ? [errorElement.textContent] : ['Validation failed'];
|
|
180
|
+
}
|
|
181
|
+
return [];
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
_showErrors(errors) {
|
|
185
|
+
if (typeof Message !== 'undefined') {
|
|
186
|
+
Message.error(errors.join('\n'));
|
|
187
|
+
} else if (typeof console !== 'undefined') {
|
|
188
|
+
console.error('Form validation errors:', errors);
|
|
189
|
+
}
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
getValues() {
|
|
193
|
+
const values = {};
|
|
194
|
+
const inputs = this.form.querySelectorAll('input, select, textarea');
|
|
195
|
+
|
|
196
|
+
inputs.forEach(input => {
|
|
197
|
+
const name = input.name;
|
|
198
|
+
if (!name) return;
|
|
199
|
+
|
|
200
|
+
if (input.type === 'checkbox') {
|
|
201
|
+
if (!values[name]) {
|
|
202
|
+
values[name] = [];
|
|
203
|
+
}
|
|
204
|
+
if (input.checked) {
|
|
205
|
+
values[name].push(input.value);
|
|
206
|
+
}
|
|
207
|
+
} else if (input.type === 'radio') {
|
|
208
|
+
if (input.checked) {
|
|
209
|
+
values[name] = input.value;
|
|
210
|
+
}
|
|
211
|
+
} else if (input.type === 'file') {
|
|
212
|
+
values[name] = input.files;
|
|
213
|
+
} else {
|
|
214
|
+
values[name] = input.value;
|
|
215
|
+
}
|
|
216
|
+
});
|
|
217
|
+
|
|
218
|
+
return values;
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
setValues(values) {
|
|
222
|
+
Object.keys(values).forEach(key => {
|
|
223
|
+
const input = this.form.querySelector(`[name="${key}"]`);
|
|
224
|
+
if (!input) return;
|
|
225
|
+
|
|
226
|
+
if (input.type === 'checkbox') {
|
|
227
|
+
const valuesArray = Array.isArray(values[key]) ? values[key] : [values[key]];
|
|
228
|
+
input.checked = valuesArray.includes(input.value);
|
|
229
|
+
} else if (input.type === 'radio') {
|
|
230
|
+
const radio = this.form.querySelector(`[name="${key}"][value="${values[key]}"]`);
|
|
231
|
+
if (radio) radio.checked = true;
|
|
232
|
+
} else if (input.type === 'file') {
|
|
233
|
+
const dataTransfer = new DataTransfer();
|
|
234
|
+
if (values[key] instanceof FileList) {
|
|
235
|
+
Array.from(values[key]).forEach(file => {
|
|
236
|
+
dataTransfer.items.add(file);
|
|
237
|
+
});
|
|
238
|
+
}
|
|
239
|
+
input.files = dataTransfer.files;
|
|
240
|
+
} else {
|
|
241
|
+
input.value = values[key];
|
|
242
|
+
}
|
|
243
|
+
});
|
|
244
|
+
return this;
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
reset() {
|
|
248
|
+
this.form.reset();
|
|
249
|
+
this.errors = {};
|
|
250
|
+
this.validator.resetForm(this.form);
|
|
251
|
+
return this;
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
clearErrors() {
|
|
255
|
+
this.errors = {};
|
|
256
|
+
this.form.querySelectorAll('.ds-input--error').forEach(input => {
|
|
257
|
+
this.validator.clearError(input);
|
|
258
|
+
});
|
|
259
|
+
return this;
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
getError(fieldName) {
|
|
263
|
+
return this.errors[fieldName] || [];
|
|
264
|
+
}
|
|
265
|
+
|
|
266
|
+
hasError(fieldName) {
|
|
267
|
+
return this.errors[fieldName] && this.errors[fieldName].length > 0;
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
destroy() {
|
|
271
|
+
this.submitHandlers.forEach(handler => {
|
|
272
|
+
this.form.removeEventListener('submit', handler);
|
|
273
|
+
});
|
|
274
|
+
this.submitHandlers = [];
|
|
275
|
+
this.errors = {};
|
|
276
|
+
this.rules = {};
|
|
277
|
+
}
|
|
278
|
+
}
|
|
279
|
+
|
|
280
|
+
function useForm(formOrSelector, options = {}) {
|
|
281
|
+
return new KupolaForm(formOrSelector, options);
|
|
282
|
+
}
|
|
283
|
+
|
|
284
|
+
function createForm(formOrSelector, options = {}) {
|
|
285
|
+
return new KupolaForm(formOrSelector, options);
|
|
286
|
+
}
|
|
287
|
+
|
|
288
|
+
window.KupolaForm = KupolaForm;
|
|
289
|
+
window.useForm = useForm;
|
|
290
|
+
window.createForm = createForm;
|
|
291
|
+
})(window);
|