@windwalker-io/unicorn-next 0.1.5 → 0.1.6
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/.editorconfig +18 -18
- package/.gulp.json +7 -7
- package/bin/release.mjs +47 -47
- package/dist/chunks/button-radio.js.map +1 -1
- package/dist/chunks/checkboxes-multi-select.js.map +1 -1
- package/dist/chunks/field-cascade-select.js.map +1 -1
- package/dist/chunks/field-file-drag.js.map +1 -1
- package/dist/chunks/field-flatpickr.js.map +1 -1
- package/dist/chunks/field-modal-select.js.map +1 -1
- package/dist/chunks/field-modal-tree.js +2 -2
- package/dist/chunks/field-modal-tree.js.map +1 -1
- package/dist/chunks/field-multi-uploader.js.map +1 -1
- package/dist/chunks/field-repeatable.js.map +1 -1
- package/dist/chunks/field-single-image-drag.js.map +1 -1
- package/dist/chunks/form.js.map +1 -1
- package/dist/chunks/grid.js.map +1 -1
- package/dist/chunks/http-client.js.map +1 -1
- package/dist/chunks/iframe-modal.js.map +1 -1
- package/dist/chunks/keep-tab.js.map +1 -1
- package/dist/chunks/legacy.js.map +1 -1
- package/dist/chunks/list-dependent.js.map +1 -1
- package/dist/chunks/s3-multipart-uploader.js.map +1 -1
- package/dist/chunks/s3-uploader.js.map +1 -1
- package/dist/chunks/show-on.js.map +1 -1
- package/dist/chunks/tinymce.js +1 -1
- package/dist/chunks/tinymce.js.map +1 -1
- package/dist/chunks/ui-bootstrap5.js.map +1 -1
- package/dist/chunks/unicorn.js +20 -14
- package/dist/chunks/unicorn.js.map +1 -1
- package/dist/chunks/validation.js +1 -1
- package/dist/chunks/validation.js.map +1 -1
- package/dist/index.d.ts +16 -14
- package/dist/unicorn.js +10 -8
- package/fusionfile.mjs +155 -155
- package/package.json +103 -103
- package/scss/bootstrap/multi-level-menu.scss +121 -121
- package/scss/editor.scss +116 -116
- package/scss/field/file-drag.scss +102 -102
- package/scss/field/single-image-drag.scss +88 -88
- package/scss/field/vue-drag-uploader.scss +160 -160
- package/scss/switcher.scss +156 -156
- package/src/app.ts +128 -128
- package/src/bootstrap/button-radio.ts +208 -208
- package/src/bootstrap/keep-tab.ts +155 -155
- package/src/composable/index.ts +22 -21
- package/src/composable/useCheckboxesMultiSelect.ts +22 -22
- package/src/composable/useFieldCascadeSelect.ts +9 -9
- package/src/composable/useFieldFileDrag.ts +9 -9
- package/src/composable/useFieldFlatpickr.ts +3 -3
- package/src/composable/useFieldModalSelect.ts +6 -6
- package/src/composable/useFieldModalTree.ts +3 -3
- package/src/composable/useFieldMultiUploader.ts +9 -9
- package/src/composable/useFieldRepeatable.ts +9 -9
- package/src/composable/useFieldSingleImageDrag.ts +9 -9
- package/src/composable/useForm.ts +43 -43
- package/src/composable/useGrid.ts +57 -57
- package/src/composable/useHttp.ts +9 -9
- package/src/composable/useIframeModal.ts +10 -10
- package/src/composable/useListDependent.ts +26 -26
- package/src/composable/useQueue.ts +13 -13
- package/src/composable/useS3Uploader.ts +32 -32
- package/src/composable/useShowOn.ts +9 -9
- package/src/composable/useStack.ts +13 -13
- package/src/composable/useTinymce.ts +29 -29
- package/src/composable/useTomSelect.ts +72 -72
- package/src/composable/useUIBootstrap5.ts +48 -48
- package/src/composable/useUniDirective.ts +32 -32
- package/src/composable/useValidation.ts +50 -50
- package/src/data.ts +34 -34
- package/src/events.ts +82 -82
- package/src/legacy/legacy.ts +190 -190
- package/src/legacy/loader.ts +125 -125
- package/src/module/checkboxes-multi-select.ts +54 -54
- package/src/module/field-cascade-select.ts +292 -292
- package/src/module/field-file-drag.ts +295 -295
- package/src/module/field-flatpickr.ts +130 -130
- package/src/module/field-modal-select.ts +179 -179
- package/src/module/field-modal-tree.ts +31 -31
- package/src/module/field-multi-uploader.ts +368 -368
- package/src/module/field-repeatable.ts +202 -202
- package/src/module/field-single-image-drag.ts +475 -475
- package/src/module/form.ts +223 -223
- package/src/module/grid.ts +465 -465
- package/src/module/http-client.ts +248 -248
- package/src/module/iframe-modal.ts +170 -170
- package/src/module/list-dependent.ts +321 -321
- package/src/module/s3-multipart-uploader.ts +300 -300
- package/src/module/s3-uploader.ts +234 -234
- package/src/module/show-on.ts +175 -175
- package/src/module/tinymce.ts +276 -276
- package/src/module/ui-bootstrap5.ts +116 -116
- package/src/module/validation.ts +1046 -1046
- package/src/plugin/index.ts +1 -1
- package/src/plugin/php-adapter.ts +72 -72
- package/src/polyfill/form-request-submit.ts +31 -31
- package/src/polyfill/index.ts +9 -9
- package/src/service/animate.ts +58 -58
- package/src/service/crypto.ts +27 -27
- package/src/service/dom-watcher.ts +62 -62
- package/src/service/dom.ts +265 -265
- package/src/service/helper.ts +48 -48
- package/src/service/index.ts +10 -10
- package/src/service/lang.ts +122 -122
- package/src/service/loader.ts +152 -152
- package/src/service/router.ts +118 -118
- package/src/service/ui.ts +525 -525
- package/src/service/uri.ts +106 -106
- package/src/types/base.ts +9 -9
- package/src/types/index.ts +4 -4
- package/src/types/modal-tree.ts +12 -12
- package/src/types/plugin.ts +6 -6
- package/src/types/shims.d.ts +18 -18
- package/src/types/ui.ts +6 -6
- package/src/unicorn.ts +79 -79
- package/src/utilities/arr.ts +25 -25
- package/src/utilities/base.ts +9 -9
- package/src/utilities/data.ts +48 -48
- package/src/utilities/index.ts +5 -5
- package/src/utilities/tree.ts +20 -20
- package/src/vue/components/ModalTree/ModalTreeApp.vue +175 -175
- package/src/vue/components/ModalTree/TreeItem.vue +262 -262
- package/src/vue/components/ModalTree/TreeModal.vue +225 -225
- package/tests/test.js +4 -4
- package/tsconfig.js.json +25 -25
- package/tsconfig.json +17 -17
- package/vite.assets.config.ts +61 -61
- package/vite.config.test.ts +36 -36
- package/vite.config.ts +112 -112
|
@@ -1,295 +1,295 @@
|
|
|
1
|
-
import css from '../../scss/field/file-drag.scss?inline';
|
|
2
|
-
import { useUniDirective } from '../composable';
|
|
3
|
-
import { __, html, injectCssToDocument, simpleAlert, uid, watchAttributes } from '../service';
|
|
4
|
-
import { mergeDeep } from '../utilities';
|
|
5
|
-
|
|
6
|
-
export interface FileDragOptions {
|
|
7
|
-
maxFiles: number | undefined;
|
|
8
|
-
maxSize: number | undefined;
|
|
9
|
-
placeholder: string;
|
|
10
|
-
height: number;
|
|
11
|
-
}
|
|
12
|
-
|
|
13
|
-
const defaultOptions: FileDragOptions = {
|
|
14
|
-
maxFiles: undefined,
|
|
15
|
-
maxSize: undefined,
|
|
16
|
-
placeholder: '',
|
|
17
|
-
height: 125,
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
export class FileDragElement extends HTMLElement {
|
|
21
|
-
static is = 'uni-file-drag';
|
|
22
|
-
|
|
23
|
-
element!: HTMLInputElement;
|
|
24
|
-
overlayLabel!: HTMLLabelElement;
|
|
25
|
-
button!: HTMLButtonElement;
|
|
26
|
-
options!: FileDragOptions;
|
|
27
|
-
|
|
28
|
-
get inputSelector() {
|
|
29
|
-
return this.getAttribute('selector') || 'input[type=file]';
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
get multiple() {
|
|
33
|
-
return this.element.multiple;
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
connectedCallback(): void {
|
|
37
|
-
this.element = this.querySelector(this.inputSelector)!;
|
|
38
|
-
|
|
39
|
-
this.prepareElements();
|
|
40
|
-
|
|
41
|
-
const options = JSON.parse(this.getAttribute('options') || '{}') || {};
|
|
42
|
-
|
|
43
|
-
const observer = watchAttributes(this.element);
|
|
44
|
-
observer.watch('readonly', (el) => {
|
|
45
|
-
el.disabled = el.readOnly;
|
|
46
|
-
});
|
|
47
|
-
|
|
48
|
-
if (this.element.readOnly) {
|
|
49
|
-
this.element.disabled = true;
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
this.options = mergeDeep({}, defaultOptions, options);
|
|
53
|
-
|
|
54
|
-
this.bindEvent();
|
|
55
|
-
|
|
56
|
-
this.style.visibility = '';
|
|
57
|
-
|
|
58
|
-
this.style.height = (this.options.height || 100) + 'px';
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
bindEvent() {
|
|
62
|
-
this.element.addEventListener('dragover', () => {
|
|
63
|
-
this.element.classList.add('hover');
|
|
64
|
-
});
|
|
65
|
-
|
|
66
|
-
this.element.addEventListener('dragleave', () => {
|
|
67
|
-
this.element.classList.remove('hover');
|
|
68
|
-
});
|
|
69
|
-
|
|
70
|
-
this.element.addEventListener('drop', () => {
|
|
71
|
-
this.element.classList.remove('hover');
|
|
72
|
-
});
|
|
73
|
-
|
|
74
|
-
this.onChange();
|
|
75
|
-
|
|
76
|
-
this.element.addEventListener('change', (e) => {
|
|
77
|
-
this.onChange(e);
|
|
78
|
-
});
|
|
79
|
-
this.element.addEventListener('input', (e) => {
|
|
80
|
-
this.onChange(e);
|
|
81
|
-
});
|
|
82
|
-
}
|
|
83
|
-
|
|
84
|
-
prepareElements() {
|
|
85
|
-
if (this.children.length === 0) {
|
|
86
|
-
this.createElementsLayout();
|
|
87
|
-
}
|
|
88
|
-
|
|
89
|
-
this.overlayLabel = this.querySelector<HTMLLabelElement>('[data-overlay-label]')!;
|
|
90
|
-
|
|
91
|
-
let button = this.overlayLabel.querySelector('button');
|
|
92
|
-
|
|
93
|
-
// B/C for new file drag style
|
|
94
|
-
if (!button) {
|
|
95
|
-
button = document.createElement('button');
|
|
96
|
-
button.type = 'button';
|
|
97
|
-
button.setAttribute('class', 'c-file-drag-input__button btn btn-success btn-sm px-2 py-1');
|
|
98
|
-
button.innerText = __('unicorn.field.file.drag.button.text');
|
|
99
|
-
this.overlayLabel.appendChild(button);
|
|
100
|
-
}
|
|
101
|
-
|
|
102
|
-
this.button = button;
|
|
103
|
-
}
|
|
104
|
-
|
|
105
|
-
createElementsLayout() {
|
|
106
|
-
this.id ||= 'c-file-drag-' + uid();
|
|
107
|
-
const name = this.getAttribute('name') || 'file';
|
|
108
|
-
const inputId = this.id + '__input';
|
|
109
|
-
const btnText = __('unicorn.field.file.drag.button.text');
|
|
110
|
-
|
|
111
|
-
const input = html(`<input id="${inputId}" type="file" name="${name}" />`);
|
|
112
|
-
const label = html(`<label class="px-3 c-file-drag-input__label"
|
|
113
|
-
data-overlay-label
|
|
114
|
-
for="${inputId}">
|
|
115
|
-
<span class="label-text d-block">
|
|
116
|
-
<span class="fa fa-upload"></span>
|
|
117
|
-
</span>
|
|
118
|
-
<button type="button" class="c-file-drag-input__button btn btn-success btn-sm px-2 py-1">
|
|
119
|
-
${btnText}
|
|
120
|
-
</button>
|
|
121
|
-
</label>`);
|
|
122
|
-
|
|
123
|
-
this.element = input as HTMLInputElement;
|
|
124
|
-
this.overlayLabel = label as HTMLLabelElement;
|
|
125
|
-
|
|
126
|
-
this.appendChild(input);
|
|
127
|
-
this.appendChild(label);
|
|
128
|
-
}
|
|
129
|
-
|
|
130
|
-
onChange(evt?: Event) {
|
|
131
|
-
const files = this.element.files || [];
|
|
132
|
-
const limit = this.options.maxFiles;
|
|
133
|
-
const maxSize = this.options.maxSize;
|
|
134
|
-
let placeholder = this.options.placeholder;
|
|
135
|
-
|
|
136
|
-
const accepted = (this.element.getAttribute('accept') || this.element.getAttribute('data-accepted') || '')
|
|
137
|
-
.split(',')
|
|
138
|
-
.map(v => v.trim())
|
|
139
|
-
.filter(v => v.length > 0)
|
|
140
|
-
.map(v => {
|
|
141
|
-
if (v.indexOf('/') === -1 && v[0] === '.') {
|
|
142
|
-
return v.substring(1);
|
|
143
|
-
}
|
|
144
|
-
|
|
145
|
-
return v;
|
|
146
|
-
});
|
|
147
|
-
|
|
148
|
-
let text: string;
|
|
149
|
-
|
|
150
|
-
if (!placeholder) {
|
|
151
|
-
if (this.multiple) {
|
|
152
|
-
placeholder = __('unicorn.field.file.drag.placeholder.multiple');
|
|
153
|
-
} else {
|
|
154
|
-
placeholder = __('unicorn.field.file.drag.placeholder.single');
|
|
155
|
-
}
|
|
156
|
-
}
|
|
157
|
-
|
|
158
|
-
// Files limit
|
|
159
|
-
if (limit && files.length > limit) {
|
|
160
|
-
this.alert(__('unicorn.field.file.drag.message.max.files', limit), '', 'warning');
|
|
161
|
-
evt?.preventDefault();
|
|
162
|
-
return;
|
|
163
|
-
}
|
|
164
|
-
|
|
165
|
-
// Files size
|
|
166
|
-
let fileSize = 0;
|
|
167
|
-
Array.prototype.forEach.call(files, file => {
|
|
168
|
-
this.checkFileType(accepted, file);
|
|
169
|
-
|
|
170
|
-
fileSize += file.size;
|
|
171
|
-
});
|
|
172
|
-
|
|
173
|
-
if (maxSize && (fileSize / 1024 / 1024) > maxSize) {
|
|
174
|
-
this.alert(
|
|
175
|
-
__(
|
|
176
|
-
'unicorn.field.file.drag.message.max.size',
|
|
177
|
-
maxSize < 1 ? (maxSize * 1024) + 'KB' : maxSize + 'MB'
|
|
178
|
-
),
|
|
179
|
-
'',
|
|
180
|
-
'warning'
|
|
181
|
-
);
|
|
182
|
-
evt?.preventDefault();
|
|
183
|
-
return;
|
|
184
|
-
}
|
|
185
|
-
|
|
186
|
-
if (files.length > 1) {
|
|
187
|
-
text = `<span class="fa fa-files fa-copy"></span> ${__('unicorn.field.file.drag.selected', files.length)}`;
|
|
188
|
-
} else if (files.length === 1) {
|
|
189
|
-
text = `<span class="fa fa-file"></span> ${files[0].name}`;
|
|
190
|
-
} else {
|
|
191
|
-
text = `<span class="fa fa-upload"></span> ${placeholder}`;
|
|
192
|
-
}
|
|
193
|
-
|
|
194
|
-
//replace the "Choose a file" label
|
|
195
|
-
this.overlayLabel.querySelector<HTMLSpanElement>('span')!.innerHTML = text;
|
|
196
|
-
}
|
|
197
|
-
|
|
198
|
-
checkFileType(accepted: string[], file: File) {
|
|
199
|
-
const fileExt = file.name.split('.').pop() || '';
|
|
200
|
-
|
|
201
|
-
if (accepted.length) {
|
|
202
|
-
let allow = false;
|
|
203
|
-
|
|
204
|
-
accepted.forEach((type) => {
|
|
205
|
-
if (allow) {
|
|
206
|
-
return;
|
|
207
|
-
}
|
|
208
|
-
|
|
209
|
-
if (type.indexOf('/') !== -1) {
|
|
210
|
-
if (this.compareMimeType(type, file.type)) {
|
|
211
|
-
allow = true;
|
|
212
|
-
}
|
|
213
|
-
} else {
|
|
214
|
-
if (type.toLowerCase() === fileExt.toLowerCase()) {
|
|
215
|
-
allow = true;
|
|
216
|
-
}
|
|
217
|
-
}
|
|
218
|
-
});
|
|
219
|
-
|
|
220
|
-
if (!allow) {
|
|
221
|
-
this.alert(
|
|
222
|
-
__('unicorn.field.file.drag.message.unaccepted.files'),
|
|
223
|
-
__('unicorn.field.file.drag.message.unaccepted.files.desc', accepted.join(', ')),
|
|
224
|
-
'warning'
|
|
225
|
-
);
|
|
226
|
-
throw new Error('Not accepted file ext');
|
|
227
|
-
}
|
|
228
|
-
}
|
|
229
|
-
}
|
|
230
|
-
|
|
231
|
-
compareMimeType(accepted: string, mime: string) {
|
|
232
|
-
const accepted2 = accepted.split('/');
|
|
233
|
-
const mime2 = mime.split('/');
|
|
234
|
-
|
|
235
|
-
if (accepted2[1] === '*') {
|
|
236
|
-
return accepted2[0] === mime2[0];
|
|
237
|
-
}
|
|
238
|
-
|
|
239
|
-
return accepted === mime;
|
|
240
|
-
}
|
|
241
|
-
|
|
242
|
-
async alert(title: string, text: string = '', type: string = 'info') {
|
|
243
|
-
await simpleAlert(title, text, type);
|
|
244
|
-
}
|
|
245
|
-
}
|
|
246
|
-
|
|
247
|
-
async function init() {
|
|
248
|
-
injectCssToDocument(document, css);
|
|
249
|
-
customElements.define(FileDragElement.is, FileDragElement);
|
|
250
|
-
|
|
251
|
-
return useUniDirective('file-drag-field', {
|
|
252
|
-
mounted(el) {
|
|
253
|
-
const input = el.querySelector<HTMLInputElement>('input[type=file]')!;
|
|
254
|
-
const placeholderInput = el.querySelector<HTMLInputElement>('[data-role=placeholder]')!;
|
|
255
|
-
|
|
256
|
-
const preview = el.querySelector('.c-file-drag-preview');
|
|
257
|
-
|
|
258
|
-
if (preview) {
|
|
259
|
-
const previewLink = preview.querySelector<HTMLAnchorElement>('.c-file-drag-preview__link')!;
|
|
260
|
-
const delButton = preview.querySelector<HTMLAnchorElement>('.c-file-drag-preview__delete')!;
|
|
261
|
-
// let linkTitle = previewLink.textContent;
|
|
262
|
-
let inputValue = placeholderInput.value;
|
|
263
|
-
let required = input.required;
|
|
264
|
-
|
|
265
|
-
if (placeholderInput.value) {
|
|
266
|
-
input.required = false;
|
|
267
|
-
}
|
|
268
|
-
|
|
269
|
-
delButton.addEventListener('click', () => {
|
|
270
|
-
if (delButton.classList.contains('active')) {
|
|
271
|
-
// Restore
|
|
272
|
-
previewLink.style.textDecoration = '';
|
|
273
|
-
previewLink.style.setProperty('color', '');
|
|
274
|
-
placeholderInput.value = inputValue;
|
|
275
|
-
delButton.classList.remove('active');
|
|
276
|
-
input.required = false;
|
|
277
|
-
} else {
|
|
278
|
-
// Delete
|
|
279
|
-
previewLink.style.textDecoration = 'line-through';
|
|
280
|
-
previewLink.style.color = 'var(--fd-delete-color, var(--bs-danger))';
|
|
281
|
-
placeholderInput.value = '';
|
|
282
|
-
delButton.classList.add('active');
|
|
283
|
-
input.required = required;
|
|
284
|
-
}
|
|
285
|
-
});
|
|
286
|
-
}
|
|
287
|
-
}
|
|
288
|
-
});
|
|
289
|
-
}
|
|
290
|
-
|
|
291
|
-
export const ready = init();
|
|
292
|
-
|
|
293
|
-
export interface FileDragModule {
|
|
294
|
-
FileDragElement: typeof FileDragElement;
|
|
295
|
-
}
|
|
1
|
+
import css from '../../scss/field/file-drag.scss?inline';
|
|
2
|
+
import { useUniDirective } from '../composable';
|
|
3
|
+
import { __, html, injectCssToDocument, simpleAlert, uid, watchAttributes } from '../service';
|
|
4
|
+
import { mergeDeep } from '../utilities';
|
|
5
|
+
|
|
6
|
+
export interface FileDragOptions {
|
|
7
|
+
maxFiles: number | undefined;
|
|
8
|
+
maxSize: number | undefined;
|
|
9
|
+
placeholder: string;
|
|
10
|
+
height: number;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
const defaultOptions: FileDragOptions = {
|
|
14
|
+
maxFiles: undefined,
|
|
15
|
+
maxSize: undefined,
|
|
16
|
+
placeholder: '',
|
|
17
|
+
height: 125,
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
export class FileDragElement extends HTMLElement {
|
|
21
|
+
static is = 'uni-file-drag';
|
|
22
|
+
|
|
23
|
+
element!: HTMLInputElement;
|
|
24
|
+
overlayLabel!: HTMLLabelElement;
|
|
25
|
+
button!: HTMLButtonElement;
|
|
26
|
+
options!: FileDragOptions;
|
|
27
|
+
|
|
28
|
+
get inputSelector() {
|
|
29
|
+
return this.getAttribute('selector') || 'input[type=file]';
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
get multiple() {
|
|
33
|
+
return this.element.multiple;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
connectedCallback(): void {
|
|
37
|
+
this.element = this.querySelector(this.inputSelector)!;
|
|
38
|
+
|
|
39
|
+
this.prepareElements();
|
|
40
|
+
|
|
41
|
+
const options = JSON.parse(this.getAttribute('options') || '{}') || {};
|
|
42
|
+
|
|
43
|
+
const observer = watchAttributes(this.element);
|
|
44
|
+
observer.watch('readonly', (el) => {
|
|
45
|
+
el.disabled = el.readOnly;
|
|
46
|
+
});
|
|
47
|
+
|
|
48
|
+
if (this.element.readOnly) {
|
|
49
|
+
this.element.disabled = true;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
this.options = mergeDeep({}, defaultOptions, options);
|
|
53
|
+
|
|
54
|
+
this.bindEvent();
|
|
55
|
+
|
|
56
|
+
this.style.visibility = '';
|
|
57
|
+
|
|
58
|
+
this.style.height = (this.options.height || 100) + 'px';
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
bindEvent() {
|
|
62
|
+
this.element.addEventListener('dragover', () => {
|
|
63
|
+
this.element.classList.add('hover');
|
|
64
|
+
});
|
|
65
|
+
|
|
66
|
+
this.element.addEventListener('dragleave', () => {
|
|
67
|
+
this.element.classList.remove('hover');
|
|
68
|
+
});
|
|
69
|
+
|
|
70
|
+
this.element.addEventListener('drop', () => {
|
|
71
|
+
this.element.classList.remove('hover');
|
|
72
|
+
});
|
|
73
|
+
|
|
74
|
+
this.onChange();
|
|
75
|
+
|
|
76
|
+
this.element.addEventListener('change', (e) => {
|
|
77
|
+
this.onChange(e);
|
|
78
|
+
});
|
|
79
|
+
this.element.addEventListener('input', (e) => {
|
|
80
|
+
this.onChange(e);
|
|
81
|
+
});
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
prepareElements() {
|
|
85
|
+
if (this.children.length === 0) {
|
|
86
|
+
this.createElementsLayout();
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
this.overlayLabel = this.querySelector<HTMLLabelElement>('[data-overlay-label]')!;
|
|
90
|
+
|
|
91
|
+
let button = this.overlayLabel.querySelector('button');
|
|
92
|
+
|
|
93
|
+
// B/C for new file drag style
|
|
94
|
+
if (!button) {
|
|
95
|
+
button = document.createElement('button');
|
|
96
|
+
button.type = 'button';
|
|
97
|
+
button.setAttribute('class', 'c-file-drag-input__button btn btn-success btn-sm px-2 py-1');
|
|
98
|
+
button.innerText = __('unicorn.field.file.drag.button.text');
|
|
99
|
+
this.overlayLabel.appendChild(button);
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
this.button = button;
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
createElementsLayout() {
|
|
106
|
+
this.id ||= 'c-file-drag-' + uid();
|
|
107
|
+
const name = this.getAttribute('name') || 'file';
|
|
108
|
+
const inputId = this.id + '__input';
|
|
109
|
+
const btnText = __('unicorn.field.file.drag.button.text');
|
|
110
|
+
|
|
111
|
+
const input = html(`<input id="${inputId}" type="file" name="${name}" />`);
|
|
112
|
+
const label = html(`<label class="px-3 c-file-drag-input__label"
|
|
113
|
+
data-overlay-label
|
|
114
|
+
for="${inputId}">
|
|
115
|
+
<span class="label-text d-block">
|
|
116
|
+
<span class="fa fa-upload"></span>
|
|
117
|
+
</span>
|
|
118
|
+
<button type="button" class="c-file-drag-input__button btn btn-success btn-sm px-2 py-1">
|
|
119
|
+
${btnText}
|
|
120
|
+
</button>
|
|
121
|
+
</label>`);
|
|
122
|
+
|
|
123
|
+
this.element = input as HTMLInputElement;
|
|
124
|
+
this.overlayLabel = label as HTMLLabelElement;
|
|
125
|
+
|
|
126
|
+
this.appendChild(input);
|
|
127
|
+
this.appendChild(label);
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
onChange(evt?: Event) {
|
|
131
|
+
const files = this.element.files || [];
|
|
132
|
+
const limit = this.options.maxFiles;
|
|
133
|
+
const maxSize = this.options.maxSize;
|
|
134
|
+
let placeholder = this.options.placeholder;
|
|
135
|
+
|
|
136
|
+
const accepted = (this.element.getAttribute('accept') || this.element.getAttribute('data-accepted') || '')
|
|
137
|
+
.split(',')
|
|
138
|
+
.map(v => v.trim())
|
|
139
|
+
.filter(v => v.length > 0)
|
|
140
|
+
.map(v => {
|
|
141
|
+
if (v.indexOf('/') === -1 && v[0] === '.') {
|
|
142
|
+
return v.substring(1);
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
return v;
|
|
146
|
+
});
|
|
147
|
+
|
|
148
|
+
let text: string;
|
|
149
|
+
|
|
150
|
+
if (!placeholder) {
|
|
151
|
+
if (this.multiple) {
|
|
152
|
+
placeholder = __('unicorn.field.file.drag.placeholder.multiple');
|
|
153
|
+
} else {
|
|
154
|
+
placeholder = __('unicorn.field.file.drag.placeholder.single');
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
// Files limit
|
|
159
|
+
if (limit && files.length > limit) {
|
|
160
|
+
this.alert(__('unicorn.field.file.drag.message.max.files', limit), '', 'warning');
|
|
161
|
+
evt?.preventDefault();
|
|
162
|
+
return;
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
// Files size
|
|
166
|
+
let fileSize = 0;
|
|
167
|
+
Array.prototype.forEach.call(files, file => {
|
|
168
|
+
this.checkFileType(accepted, file);
|
|
169
|
+
|
|
170
|
+
fileSize += file.size;
|
|
171
|
+
});
|
|
172
|
+
|
|
173
|
+
if (maxSize && (fileSize / 1024 / 1024) > maxSize) {
|
|
174
|
+
this.alert(
|
|
175
|
+
__(
|
|
176
|
+
'unicorn.field.file.drag.message.max.size',
|
|
177
|
+
maxSize < 1 ? (maxSize * 1024) + 'KB' : maxSize + 'MB'
|
|
178
|
+
),
|
|
179
|
+
'',
|
|
180
|
+
'warning'
|
|
181
|
+
);
|
|
182
|
+
evt?.preventDefault();
|
|
183
|
+
return;
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
if (files.length > 1) {
|
|
187
|
+
text = `<span class="fa fa-files fa-copy"></span> ${__('unicorn.field.file.drag.selected', files.length)}`;
|
|
188
|
+
} else if (files.length === 1) {
|
|
189
|
+
text = `<span class="fa fa-file"></span> ${files[0].name}`;
|
|
190
|
+
} else {
|
|
191
|
+
text = `<span class="fa fa-upload"></span> ${placeholder}`;
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
//replace the "Choose a file" label
|
|
195
|
+
this.overlayLabel.querySelector<HTMLSpanElement>('span')!.innerHTML = text;
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
checkFileType(accepted: string[], file: File) {
|
|
199
|
+
const fileExt = file.name.split('.').pop() || '';
|
|
200
|
+
|
|
201
|
+
if (accepted.length) {
|
|
202
|
+
let allow = false;
|
|
203
|
+
|
|
204
|
+
accepted.forEach((type) => {
|
|
205
|
+
if (allow) {
|
|
206
|
+
return;
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
if (type.indexOf('/') !== -1) {
|
|
210
|
+
if (this.compareMimeType(type, file.type)) {
|
|
211
|
+
allow = true;
|
|
212
|
+
}
|
|
213
|
+
} else {
|
|
214
|
+
if (type.toLowerCase() === fileExt.toLowerCase()) {
|
|
215
|
+
allow = true;
|
|
216
|
+
}
|
|
217
|
+
}
|
|
218
|
+
});
|
|
219
|
+
|
|
220
|
+
if (!allow) {
|
|
221
|
+
this.alert(
|
|
222
|
+
__('unicorn.field.file.drag.message.unaccepted.files'),
|
|
223
|
+
__('unicorn.field.file.drag.message.unaccepted.files.desc', accepted.join(', ')),
|
|
224
|
+
'warning'
|
|
225
|
+
);
|
|
226
|
+
throw new Error('Not accepted file ext');
|
|
227
|
+
}
|
|
228
|
+
}
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
compareMimeType(accepted: string, mime: string) {
|
|
232
|
+
const accepted2 = accepted.split('/');
|
|
233
|
+
const mime2 = mime.split('/');
|
|
234
|
+
|
|
235
|
+
if (accepted2[1] === '*') {
|
|
236
|
+
return accepted2[0] === mime2[0];
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
return accepted === mime;
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
async alert(title: string, text: string = '', type: string = 'info') {
|
|
243
|
+
await simpleAlert(title, text, type);
|
|
244
|
+
}
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
async function init() {
|
|
248
|
+
injectCssToDocument(document, css);
|
|
249
|
+
customElements.define(FileDragElement.is, FileDragElement);
|
|
250
|
+
|
|
251
|
+
return useUniDirective('file-drag-field', {
|
|
252
|
+
mounted(el) {
|
|
253
|
+
const input = el.querySelector<HTMLInputElement>('input[type=file]')!;
|
|
254
|
+
const placeholderInput = el.querySelector<HTMLInputElement>('[data-role=placeholder]')!;
|
|
255
|
+
|
|
256
|
+
const preview = el.querySelector('.c-file-drag-preview');
|
|
257
|
+
|
|
258
|
+
if (preview) {
|
|
259
|
+
const previewLink = preview.querySelector<HTMLAnchorElement>('.c-file-drag-preview__link')!;
|
|
260
|
+
const delButton = preview.querySelector<HTMLAnchorElement>('.c-file-drag-preview__delete')!;
|
|
261
|
+
// let linkTitle = previewLink.textContent;
|
|
262
|
+
let inputValue = placeholderInput.value;
|
|
263
|
+
let required = input.required;
|
|
264
|
+
|
|
265
|
+
if (placeholderInput.value) {
|
|
266
|
+
input.required = false;
|
|
267
|
+
}
|
|
268
|
+
|
|
269
|
+
delButton.addEventListener('click', () => {
|
|
270
|
+
if (delButton.classList.contains('active')) {
|
|
271
|
+
// Restore
|
|
272
|
+
previewLink.style.textDecoration = '';
|
|
273
|
+
previewLink.style.setProperty('color', '');
|
|
274
|
+
placeholderInput.value = inputValue;
|
|
275
|
+
delButton.classList.remove('active');
|
|
276
|
+
input.required = false;
|
|
277
|
+
} else {
|
|
278
|
+
// Delete
|
|
279
|
+
previewLink.style.textDecoration = 'line-through';
|
|
280
|
+
previewLink.style.color = 'var(--fd-delete-color, var(--bs-danger))';
|
|
281
|
+
placeholderInput.value = '';
|
|
282
|
+
delButton.classList.add('active');
|
|
283
|
+
input.required = required;
|
|
284
|
+
}
|
|
285
|
+
});
|
|
286
|
+
}
|
|
287
|
+
}
|
|
288
|
+
});
|
|
289
|
+
}
|
|
290
|
+
|
|
291
|
+
export const ready = init();
|
|
292
|
+
|
|
293
|
+
export interface FileDragModule {
|
|
294
|
+
FileDragElement: typeof FileDragElement;
|
|
295
|
+
}
|