@pacem/pacem-scaffolding 1.0.0-abel
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/LICENSE +191 -0
- package/NOTICE +4 -0
- package/dist/browser/pacem-scaffolding.js +10430 -0
- package/dist/browser/pacem-scaffolding.js.map +1 -0
- package/dist/browser/pacem-scaffolding.min.js +2 -0
- package/dist/browser/pacem-scaffolding.min.js.map +1 -0
- package/dist/bundle/pacem-scaffolding.min.mjs +380 -0
- package/dist/bundle/pacem-scaffolding.mjs +10046 -0
- package/dist/bundle/pacem-scaffolding.mjs.map +7 -0
- package/dist/esm/calendar-picker.js +478 -0
- package/dist/esm/char-counter.js +43 -0
- package/dist/esm/checkbox-list.js +75 -0
- package/dist/esm/checkbox.js +112 -0
- package/dist/esm/childform.js +237 -0
- package/dist/esm/contenteditable.js +326 -0
- package/dist/esm/datetime-picker.js +310 -0
- package/dist/esm/form.js +630 -0
- package/dist/esm/formfield.js +1065 -0
- package/dist/esm/geolocate.js +197 -0
- package/dist/esm/hierarchy.js +509 -0
- package/dist/esm/index-components-scaffolding.js +39 -0
- package/dist/esm/index-components.js +1 -0
- package/dist/esm/index-iife.js +3 -0
- package/dist/esm/index-root.js +2 -0
- package/dist/esm/index-scaffolding.js +3 -0
- package/dist/esm/index.js +2 -0
- package/dist/esm/input-chars.js +185 -0
- package/dist/esm/input-color.js +80 -0
- package/dist/esm/input-email.js +32 -0
- package/dist/esm/input-file.js +691 -0
- package/dist/esm/input-hidden.js +25 -0
- package/dist/esm/input-image.js +391 -0
- package/dist/esm/input-number.js +58 -0
- package/dist/esm/input-password.js +71 -0
- package/dist/esm/input-search.js +27 -0
- package/dist/esm/input-tel.js +31 -0
- package/dist/esm/input-text.js +36 -0
- package/dist/esm/input-url.js +32 -0
- package/dist/esm/input.js +215 -0
- package/dist/esm/l10n.js +66 -0
- package/dist/esm/latlng.js +178 -0
- package/dist/esm/namevalue-list.js +139 -0
- package/dist/esm/property-picker.js +84 -0
- package/dist/esm/radio-list.js +70 -0
- package/dist/esm/radio.js +79 -0
- package/dist/esm/select.js +126 -0
- package/dist/esm/slider.js +284 -0
- package/dist/esm/snapshot.js +325 -0
- package/dist/esm/suggest.js +545 -0
- package/dist/esm/tags.js +174 -0
- package/dist/esm/textarea-markdown.js +75 -0
- package/dist/esm/textarea.js +51 -0
- package/dist/esm/types-components.js +611 -0
- package/dist/esm/types.js +8 -0
- package/dist/esm/validators.js +409 -0
- package/package.json +38 -0
- package/typings/index.d.ts +2283 -0
|
@@ -0,0 +1,691 @@
|
|
|
1
|
+
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
|
|
2
|
+
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
3
|
+
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
4
|
+
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
|
5
|
+
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
6
|
+
};
|
|
7
|
+
import { P, PCSS, CustomElement, Watch, Defaults, ViewChild, CustomTypedEvent, CustomEventUtils, PropertyConverters, Animations, Utils, stopPropagationHandler, avoidHandler } from '@pacem/pacem-core';
|
|
8
|
+
import { PacemBaseElement } from './types-components';
|
|
9
|
+
export const FileUploadEventName = 'fileupload';
|
|
10
|
+
export const FileChangeEventName = 'filechange';
|
|
11
|
+
class StoragePickEvent extends CustomTypedEvent {
|
|
12
|
+
constructor(file) {
|
|
13
|
+
super('pick', file);
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
export class FileUploadEvent extends CustomTypedEvent {
|
|
17
|
+
constructor(file) {
|
|
18
|
+
super(FileUploadEventName, file);
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
export class FileChangeEvent extends CustomTypedEvent {
|
|
22
|
+
constructor(file) {
|
|
23
|
+
super(FileChangeEventName, file);
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
const ERROR_BINDING = '!$pacem.isNullOrEmpty(:host.value) && :host.invalidFile && $pacem.isNullOrEmpty(:host.url)';
|
|
27
|
+
async function computeDirStats(dir, stats = { files: 0, directories: 0, size: 0 }) {
|
|
28
|
+
for await (let [_, entry] of dir.entries()) {
|
|
29
|
+
if (entry.kind === 'file') {
|
|
30
|
+
stats.files++;
|
|
31
|
+
const file = await entry.getFile();
|
|
32
|
+
stats.size += file.size;
|
|
33
|
+
}
|
|
34
|
+
else {
|
|
35
|
+
stats.directories++;
|
|
36
|
+
await computeDirStats(entry, stats);
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
return stats;
|
|
40
|
+
}
|
|
41
|
+
async function computeFileStats(files) {
|
|
42
|
+
const stats = { files: 0, directories: 0, size: 0 };
|
|
43
|
+
for (let f of files) {
|
|
44
|
+
const file = await f.getFile();
|
|
45
|
+
stats.files++;
|
|
46
|
+
stats.size += file.size;
|
|
47
|
+
}
|
|
48
|
+
return stats;
|
|
49
|
+
}
|
|
50
|
+
/**
|
|
51
|
+
* PacemUploader Component
|
|
52
|
+
*/
|
|
53
|
+
let PacemUploadElement = class PacemUploadElement extends PacemBaseElement {
|
|
54
|
+
constructor(_tweener = new Animations.TweenService()) {
|
|
55
|
+
super();
|
|
56
|
+
this._tweener = _tweener;
|
|
57
|
+
this.undoCaption = 'undo';
|
|
58
|
+
this.retryCaption = 'retry';
|
|
59
|
+
this.clearCaption = 'clear';
|
|
60
|
+
this.uploading = false;
|
|
61
|
+
this.size = 0;
|
|
62
|
+
this.percentage = .0;
|
|
63
|
+
this.complete = false;
|
|
64
|
+
this.failed = false;
|
|
65
|
+
this.invalidFile = false;
|
|
66
|
+
this._fields = {
|
|
67
|
+
'parallelism': 3,
|
|
68
|
+
'uid': '',
|
|
69
|
+
'ongoing': 0,
|
|
70
|
+
'enqueuer': null,
|
|
71
|
+
'blob': null,
|
|
72
|
+
'retryFrom': 0,
|
|
73
|
+
'chunkSize': 1024 * 128,
|
|
74
|
+
'undone': false
|
|
75
|
+
};
|
|
76
|
+
}
|
|
77
|
+
convertValueAttributeToProperty(attr) {
|
|
78
|
+
return attr; // better assumptions anyone?
|
|
79
|
+
}
|
|
80
|
+
acceptValue(val) {
|
|
81
|
+
this.percentage = 0;
|
|
82
|
+
this.uploading = false;
|
|
83
|
+
this._fileupload.value = '';
|
|
84
|
+
}
|
|
85
|
+
_getMimeIcon(file = this.value, uploading = this.uploading, mode = this.selectMode ?? 'file') {
|
|
86
|
+
if (uploading) {
|
|
87
|
+
return 'sync';
|
|
88
|
+
}
|
|
89
|
+
const hasValue = !Utils.isNullOrEmpty(file);
|
|
90
|
+
switch (mode) {
|
|
91
|
+
case 'directory':
|
|
92
|
+
case 'directory-picker':
|
|
93
|
+
return hasValue ? 'folder' : 'drive_folder_upload';
|
|
94
|
+
case 'multiple':
|
|
95
|
+
case 'multiple-picker':
|
|
96
|
+
return hasValue ? 'files' : 'upload_file';
|
|
97
|
+
}
|
|
98
|
+
if (Utils.isNullOrEmpty(file)) {
|
|
99
|
+
return 'upload_file';
|
|
100
|
+
}
|
|
101
|
+
var filename = file;
|
|
102
|
+
if (typeof file === 'object') {
|
|
103
|
+
filename = file.name;
|
|
104
|
+
}
|
|
105
|
+
const extPattern = /\.([\w]+)(\s|$)/;
|
|
106
|
+
const match = extPattern.exec(filename);
|
|
107
|
+
if (match && match.length) {
|
|
108
|
+
switch (match[1].toLowerCase()) {
|
|
109
|
+
case 'pdf':
|
|
110
|
+
return 'picture_as_pdf';
|
|
111
|
+
//case 'txt':
|
|
112
|
+
//case 'doc':
|
|
113
|
+
//case 'docx':
|
|
114
|
+
// return '<i class="pacem-icon">collection_text</i>';
|
|
115
|
+
case 'jpg':
|
|
116
|
+
case 'jpeg':
|
|
117
|
+
case 'gif':
|
|
118
|
+
case 'png':
|
|
119
|
+
return 'filter';
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
return 'filter_none';
|
|
123
|
+
}
|
|
124
|
+
get blob() {
|
|
125
|
+
return this._fields.blob;
|
|
126
|
+
}
|
|
127
|
+
get inputFields() {
|
|
128
|
+
return [this._fileupload];
|
|
129
|
+
}
|
|
130
|
+
getViewValue(val) {
|
|
131
|
+
const value = this._localValue || val;
|
|
132
|
+
if (Utils.isNullOrEmpty(value)) {
|
|
133
|
+
return '';
|
|
134
|
+
}
|
|
135
|
+
if (typeof value === 'string') {
|
|
136
|
+
return value;
|
|
137
|
+
}
|
|
138
|
+
let caption = 'name' in value ? value.name : '';
|
|
139
|
+
if (Utils.isArray(value)) {
|
|
140
|
+
caption = value.map(i => i.name).join(', ');
|
|
141
|
+
}
|
|
142
|
+
const stats = this._stats;
|
|
143
|
+
if (Utils.isNullOrEmpty(stats)) {
|
|
144
|
+
return caption;
|
|
145
|
+
}
|
|
146
|
+
const { size, files } = stats;
|
|
147
|
+
return `${caption}\n(${files} file${files === 1 ? '' : 's'}, ${Utils.core.size(size)})`;
|
|
148
|
+
}
|
|
149
|
+
toggleReadonlyView(readonly) {
|
|
150
|
+
this._setupInputElement(readonly);
|
|
151
|
+
}
|
|
152
|
+
viewActivatedCallback() {
|
|
153
|
+
super.viewActivatedCallback();
|
|
154
|
+
this._setupInputElement();
|
|
155
|
+
this._fileupload.addEventListener('click', stopPropagationHandler, false);
|
|
156
|
+
}
|
|
157
|
+
propertyChangedCallback(name, old, val, first) {
|
|
158
|
+
super.propertyChangedCallback(name, old, val, first);
|
|
159
|
+
if (!first) {
|
|
160
|
+
switch (name) {
|
|
161
|
+
case 'selectMode':
|
|
162
|
+
this._setupInputElement();
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
disconnectedCallback() {
|
|
167
|
+
if (this._fileupload) {
|
|
168
|
+
this._fileupload.removeEventListener('click', stopPropagationHandler, false);
|
|
169
|
+
}
|
|
170
|
+
super.disconnectedCallback();
|
|
171
|
+
}
|
|
172
|
+
_setupInputElement(readonly = this.readonly, mode = this.selectMode ?? 'file') {
|
|
173
|
+
const input = this._fileupload;
|
|
174
|
+
input.removeAttribute('webkitdirectory');
|
|
175
|
+
input.removeAttribute('multiple');
|
|
176
|
+
input.hidden = readonly;
|
|
177
|
+
switch (mode) {
|
|
178
|
+
case 'directory':
|
|
179
|
+
input.setAttribute('webkitdirectory', 'true');
|
|
180
|
+
break;
|
|
181
|
+
case 'multiple':
|
|
182
|
+
input.setAttribute('multiple', 'true');
|
|
183
|
+
break;
|
|
184
|
+
case 'directory-picker':
|
|
185
|
+
case 'file-picker':
|
|
186
|
+
case 'multiple-picker':
|
|
187
|
+
input.hidden = true;
|
|
188
|
+
break;
|
|
189
|
+
}
|
|
190
|
+
}
|
|
191
|
+
/**
|
|
192
|
+
* Triggered from the component's template.
|
|
193
|
+
*/
|
|
194
|
+
_dispatchDownload(evt) {
|
|
195
|
+
avoidHandler(evt);
|
|
196
|
+
const value = /* download the file being currently precessed, if available */ this._localValue || this.value, dispatch = (name) => {
|
|
197
|
+
this.dispatchEvent(new CustomEvent('download', { detail: name, bubbles: true, cancelable: false }));
|
|
198
|
+
};
|
|
199
|
+
if (!Utils.isNullOrEmpty(value)) {
|
|
200
|
+
if (typeof value === 'string') {
|
|
201
|
+
// dispatch outside if url is provided
|
|
202
|
+
dispatch(value);
|
|
203
|
+
}
|
|
204
|
+
else {
|
|
205
|
+
if ('content' in value && !Utils.isNullOrEmpty(value.content)) {
|
|
206
|
+
// download right-away the content when provided
|
|
207
|
+
Utils.download(Utils.dataURLToBlob('data:application/download;base64,' + value.content), value.name, value.type);
|
|
208
|
+
}
|
|
209
|
+
else if ('name' in value) {
|
|
210
|
+
if ('kind' in value) {
|
|
211
|
+
if (value.kind === 'file') {
|
|
212
|
+
value.getFile().then(file => {
|
|
213
|
+
Utils.download(file, file.name, file.type);
|
|
214
|
+
});
|
|
215
|
+
}
|
|
216
|
+
else {
|
|
217
|
+
dispatch(value.name);
|
|
218
|
+
}
|
|
219
|
+
}
|
|
220
|
+
else {
|
|
221
|
+
// dispatch outside if content is empty
|
|
222
|
+
dispatch(value.name);
|
|
223
|
+
}
|
|
224
|
+
}
|
|
225
|
+
}
|
|
226
|
+
}
|
|
227
|
+
}
|
|
228
|
+
reset() {
|
|
229
|
+
super.reset();
|
|
230
|
+
this._localValue = null;
|
|
231
|
+
this.invalidFile = false;
|
|
232
|
+
}
|
|
233
|
+
_validateAll() {
|
|
234
|
+
const stats = this._stats;
|
|
235
|
+
const size = this.maxSize;
|
|
236
|
+
if (size > 0 && size < stats.size) {
|
|
237
|
+
this.invalidFile = true;
|
|
238
|
+
return false;
|
|
239
|
+
}
|
|
240
|
+
// all-righty then
|
|
241
|
+
this.invalidFile = false;
|
|
242
|
+
return true;
|
|
243
|
+
}
|
|
244
|
+
_validateSingleFile(file = this._fileupload.files[0], filename) {
|
|
245
|
+
if (file instanceof File) {
|
|
246
|
+
filename = file.name;
|
|
247
|
+
}
|
|
248
|
+
filename = filename.substring(filename.lastIndexOf('\\') + 1);
|
|
249
|
+
// max size exceeded
|
|
250
|
+
const size = this.maxSize;
|
|
251
|
+
if (size > 0 && size < file.size) {
|
|
252
|
+
this.invalidFile = true;
|
|
253
|
+
return false;
|
|
254
|
+
}
|
|
255
|
+
// pattern unmatched
|
|
256
|
+
const pattern = this.pattern;
|
|
257
|
+
if (pattern && !(new RegExp(pattern, 'i').test(filename))) {
|
|
258
|
+
this.invalidFile = true;
|
|
259
|
+
return false;
|
|
260
|
+
}
|
|
261
|
+
// all-righty then
|
|
262
|
+
this.invalidFile = false;
|
|
263
|
+
return true;
|
|
264
|
+
}
|
|
265
|
+
upload(file, filename, state) {
|
|
266
|
+
if (this._validateSingleFile(file, filename)) {
|
|
267
|
+
return this._upload(file, filename, state);
|
|
268
|
+
}
|
|
269
|
+
}
|
|
270
|
+
async _upload(file, filename, state) {
|
|
271
|
+
var uploader = this, fields = uploader._fields;
|
|
272
|
+
if (!file)
|
|
273
|
+
return;
|
|
274
|
+
if (!filename && file instanceof File) {
|
|
275
|
+
filename = file.name;
|
|
276
|
+
}
|
|
277
|
+
uploader.failed = false;
|
|
278
|
+
fields.undone = false;
|
|
279
|
+
fields.ongoing = 0;
|
|
280
|
+
const blob = file, size = uploader.size = blob.size;
|
|
281
|
+
filename = filename.substring(filename.lastIndexOf('\\') + 1);
|
|
282
|
+
uploader.percentage = .0;
|
|
283
|
+
uploader.complete = false;
|
|
284
|
+
//
|
|
285
|
+
var request = {
|
|
286
|
+
filename, length: size, action: "start", state, type: file.type
|
|
287
|
+
};
|
|
288
|
+
//
|
|
289
|
+
uploader.uploading = true;
|
|
290
|
+
try {
|
|
291
|
+
var r = await uploader._fetch(request);
|
|
292
|
+
if (r.ok) {
|
|
293
|
+
var json = await r.json();
|
|
294
|
+
fields.retryFrom = 0;
|
|
295
|
+
fields.blob = blob;
|
|
296
|
+
fields.uid = json.uid;
|
|
297
|
+
uploader._manage();
|
|
298
|
+
}
|
|
299
|
+
else {
|
|
300
|
+
uploader.uploading = false;
|
|
301
|
+
}
|
|
302
|
+
return r;
|
|
303
|
+
}
|
|
304
|
+
catch (e) {
|
|
305
|
+
uploader.uploading = false;
|
|
306
|
+
}
|
|
307
|
+
finally {
|
|
308
|
+
}
|
|
309
|
+
}
|
|
310
|
+
async _blobToBase64(blob) {
|
|
311
|
+
const result = await Utils.blobToDataURL(blob);
|
|
312
|
+
return result.substring(result.indexOf('base64,') + 7);
|
|
313
|
+
}
|
|
314
|
+
async _doUpload(blob, skip) {
|
|
315
|
+
var uploader = this, fields = uploader._fields;
|
|
316
|
+
fields.ongoing++;
|
|
317
|
+
//
|
|
318
|
+
const chunk = await this._blobToBase64(blob);
|
|
319
|
+
var request = {
|
|
320
|
+
chunk: chunk,
|
|
321
|
+
uid: fields.uid, position: skip,
|
|
322
|
+
action: "do"
|
|
323
|
+
};
|
|
324
|
+
try {
|
|
325
|
+
const r = await uploader._fetch(request);
|
|
326
|
+
if (r.ok) {
|
|
327
|
+
fields.ongoing--;
|
|
328
|
+
if (!!fields.undone)
|
|
329
|
+
return;
|
|
330
|
+
const json = await r.json();
|
|
331
|
+
const result = json;
|
|
332
|
+
// parallelism > 1 may cause non-ordered responses
|
|
333
|
+
if (result.percentage > this.percentage) {
|
|
334
|
+
await uploader._tweenPercentage(Math.round(Math.max(1, result.percentage)), 200);
|
|
335
|
+
}
|
|
336
|
+
if (uploader.complete != result.complete) {
|
|
337
|
+
uploader.complete = result.complete;
|
|
338
|
+
if (uploader.complete === true) {
|
|
339
|
+
uploader.uploading = false;
|
|
340
|
+
uploader.changeHandler(new FileUploadEvent(result));
|
|
341
|
+
}
|
|
342
|
+
}
|
|
343
|
+
}
|
|
344
|
+
else {
|
|
345
|
+
// error occurred
|
|
346
|
+
fields.retryFrom = skip;
|
|
347
|
+
uploader.failed = true;
|
|
348
|
+
uploader.uploading = false;
|
|
349
|
+
}
|
|
350
|
+
}
|
|
351
|
+
catch (_) {
|
|
352
|
+
// crash
|
|
353
|
+
fields.retryFrom = skip;
|
|
354
|
+
uploader.failed = true;
|
|
355
|
+
uploader.uploading = false;
|
|
356
|
+
}
|
|
357
|
+
}
|
|
358
|
+
_manage() {
|
|
359
|
+
const fields = this._fields, size = this.size, blob = fields.blob, chunkSize = this.chunkSize || fields.chunkSize;
|
|
360
|
+
var start = fields.retryFrom;
|
|
361
|
+
var end = start + chunkSize;
|
|
362
|
+
//
|
|
363
|
+
fields.enqueuer = setInterval(() => {
|
|
364
|
+
if (start < size && !this.failed) {
|
|
365
|
+
const parallelism = this.parallelism || fields.parallelism;
|
|
366
|
+
if (fields.ongoing >= parallelism)
|
|
367
|
+
return;
|
|
368
|
+
this._doUpload(blob.slice(start, end), start);
|
|
369
|
+
start = end;
|
|
370
|
+
end = start + chunkSize;
|
|
371
|
+
}
|
|
372
|
+
else {
|
|
373
|
+
var input = this._fileupload;
|
|
374
|
+
input.value = '';
|
|
375
|
+
window.clearInterval(fields.enqueuer);
|
|
376
|
+
}
|
|
377
|
+
}, 100);
|
|
378
|
+
}
|
|
379
|
+
async _buildLocalValue(file, filename = file.name, blob = file) {
|
|
380
|
+
return this._localValue = {
|
|
381
|
+
name: filename,
|
|
382
|
+
size: blob.size,
|
|
383
|
+
type: file.type,
|
|
384
|
+
lastModified: Utils.parseDate(file.lastModified).toISOString(),
|
|
385
|
+
content: await this._blobToBase64(blob)
|
|
386
|
+
};
|
|
387
|
+
}
|
|
388
|
+
_canPick(w) {
|
|
389
|
+
return "showDirectoryPicker" in w &&
|
|
390
|
+
(() => {
|
|
391
|
+
try {
|
|
392
|
+
return w.self === w.top;
|
|
393
|
+
}
|
|
394
|
+
catch {
|
|
395
|
+
return false;
|
|
396
|
+
}
|
|
397
|
+
})();
|
|
398
|
+
}
|
|
399
|
+
_tryPickDirectory() {
|
|
400
|
+
if (!this._canPick(window)) {
|
|
401
|
+
return;
|
|
402
|
+
}
|
|
403
|
+
window.showDirectoryPicker({ mode: 'read' }).then(directory => {
|
|
404
|
+
this.changeHandler(new StoragePickEvent(directory));
|
|
405
|
+
});
|
|
406
|
+
}
|
|
407
|
+
_tryPickFiles(multiple = this.selectMode === 'multiple-picker', pattern = this.pattern) {
|
|
408
|
+
if (!this._canPick(window)) {
|
|
409
|
+
return;
|
|
410
|
+
}
|
|
411
|
+
window.showOpenFilePicker({ multiple }).then(files => {
|
|
412
|
+
this.changeHandler(new StoragePickEvent(files));
|
|
413
|
+
});
|
|
414
|
+
}
|
|
415
|
+
/**
|
|
416
|
+
* Called from the template.
|
|
417
|
+
* @param evt
|
|
418
|
+
*/
|
|
419
|
+
_clickHandler(evt) {
|
|
420
|
+
avoidHandler(evt);
|
|
421
|
+
if (this.readonly) {
|
|
422
|
+
return;
|
|
423
|
+
}
|
|
424
|
+
switch (this.selectMode) {
|
|
425
|
+
case 'directory-picker':
|
|
426
|
+
this._tryPickDirectory();
|
|
427
|
+
break;
|
|
428
|
+
case 'file-picker':
|
|
429
|
+
case 'multiple-picker':
|
|
430
|
+
this._tryPickFiles();
|
|
431
|
+
break;
|
|
432
|
+
}
|
|
433
|
+
}
|
|
434
|
+
async _mimicCompute() {
|
|
435
|
+
// mimic upload
|
|
436
|
+
this.uploading = true; // <- triggers anim
|
|
437
|
+
if (!Utils.isNullOrEmpty(this.value)) {
|
|
438
|
+
this.percentage = 0;
|
|
439
|
+
await Utils.waitForAnimationEnd(this._tuner);
|
|
440
|
+
}
|
|
441
|
+
else {
|
|
442
|
+
await Utils.idle(250);
|
|
443
|
+
}
|
|
444
|
+
await this._tweener.run(0, 100, 500, 0, Animations.Easings.sineInOut, (t, v) => {
|
|
445
|
+
this.percentage = v;
|
|
446
|
+
});
|
|
447
|
+
// reset pct
|
|
448
|
+
this.percentage = .0;
|
|
449
|
+
this.uploading = false;
|
|
450
|
+
}
|
|
451
|
+
onChange(evt) {
|
|
452
|
+
return new Promise(async (resolve, reject) => {
|
|
453
|
+
if (CustomEventUtils.isInstanceOf(evt, FileUploadEvent)) {
|
|
454
|
+
// result of an upload?
|
|
455
|
+
const file = this.value = evt.detail.filename;
|
|
456
|
+
this.dispatchEvent(evt);
|
|
457
|
+
resolve(file);
|
|
458
|
+
}
|
|
459
|
+
else if (CustomEventUtils.isInstanceOf(evt, StoragePickEvent)) {
|
|
460
|
+
// FileSystem API?
|
|
461
|
+
let val = evt.detail;
|
|
462
|
+
let directory, file, files;
|
|
463
|
+
switch (this.selectMode) {
|
|
464
|
+
case 'directory-picker':
|
|
465
|
+
directory = val;
|
|
466
|
+
this._stats = await computeDirStats(directory);
|
|
467
|
+
break;
|
|
468
|
+
case 'multiple-picker':
|
|
469
|
+
case 'file-picker':
|
|
470
|
+
files = val;
|
|
471
|
+
if (files.length === 1) {
|
|
472
|
+
val = files[0];
|
|
473
|
+
}
|
|
474
|
+
this._stats = await computeFileStats(files);
|
|
475
|
+
break;
|
|
476
|
+
default:
|
|
477
|
+
// shouldn't be here
|
|
478
|
+
this._stats = { directories: 0, files: 0, size: 0 };
|
|
479
|
+
break;
|
|
480
|
+
}
|
|
481
|
+
resolve(this.value = this._localValue = val);
|
|
482
|
+
const valid = this._validateAll();
|
|
483
|
+
this.dispatchEvent(new FileChangeEvent({ directory, file, files, valid }));
|
|
484
|
+
}
|
|
485
|
+
else {
|
|
486
|
+
// HTMLInputElement change event
|
|
487
|
+
let input = this._fileupload;
|
|
488
|
+
if (input.files.length === 0) {
|
|
489
|
+
// clear
|
|
490
|
+
this._stats = { directories: 0, files: 0, size: 0 };
|
|
491
|
+
resolve(this.value = null);
|
|
492
|
+
}
|
|
493
|
+
else {
|
|
494
|
+
const files = input.files.length;
|
|
495
|
+
this._stats = { directories: 0, files, size: Array.from(input.files).reduce((acc, file, _) => acc += file.size, 0) };
|
|
496
|
+
if (files > 1) {
|
|
497
|
+
// directory or multiple files
|
|
498
|
+
const selectedFiles = Array.from(input.files);
|
|
499
|
+
resolve(this.value = this._localValue = selectedFiles);
|
|
500
|
+
const valid = this._validateAll();
|
|
501
|
+
this.dispatchEvent(new FileChangeEvent({ files: selectedFiles, valid }));
|
|
502
|
+
}
|
|
503
|
+
else {
|
|
504
|
+
const file = input.files[0], filename = file.name;
|
|
505
|
+
let blob = file;
|
|
506
|
+
// validate file
|
|
507
|
+
if (!this._validateSingleFile(file) && !Utils.isNullOrEmpty(this.url)) {
|
|
508
|
+
// do not start uploads with an invalid file
|
|
509
|
+
resolve(this.value = await this._buildLocalValue(file));
|
|
510
|
+
}
|
|
511
|
+
else {
|
|
512
|
+
if (/\.(jpe?g|png)$/i.test(filename) && this.maxImageWidth > 0 && this.maxImageHeight > 0) {
|
|
513
|
+
blob = await Utils.Images.resize(blob, this.maxImageWidth, this.maxImageHeight, { quality: .8 });
|
|
514
|
+
}
|
|
515
|
+
await this._buildLocalValue(file, filename, blob);
|
|
516
|
+
if (Utils.isNullOrEmpty(this.url)) {
|
|
517
|
+
// mimic upload
|
|
518
|
+
await this._mimicCompute();
|
|
519
|
+
// no direct upload? set the value to the very File
|
|
520
|
+
resolve(this.value = this._localValue);
|
|
521
|
+
}
|
|
522
|
+
else {
|
|
523
|
+
// upload, then wait for resulting filename (value will be the filename string)
|
|
524
|
+
this._upload(blob, input.value);
|
|
525
|
+
resolve(this.value);
|
|
526
|
+
}
|
|
527
|
+
}
|
|
528
|
+
// dispatch filechange
|
|
529
|
+
this.dispatchEvent(new FileChangeEvent({ valid: !this.invalidFile, file }));
|
|
530
|
+
}
|
|
531
|
+
}
|
|
532
|
+
}
|
|
533
|
+
});
|
|
534
|
+
}
|
|
535
|
+
_fetch(request) {
|
|
536
|
+
return fetch(this.url, {
|
|
537
|
+
method: 'POST', credentials: this.fetchCredentials, headers: Utils.extend({ 'Accept': 'application/json', 'Content-Type': 'application/json' }, this.fetchHeaders || {}), body: JSON.stringify(request)
|
|
538
|
+
});
|
|
539
|
+
}
|
|
540
|
+
async _undo(e) {
|
|
541
|
+
e.preventDefault();
|
|
542
|
+
e.stopPropagation();
|
|
543
|
+
var Uploader = this, fields = Uploader._fields, input = Uploader._fileupload;
|
|
544
|
+
clearInterval(fields.enqueuer);
|
|
545
|
+
var request = {
|
|
546
|
+
action: "undo", uid: fields.uid
|
|
547
|
+
};
|
|
548
|
+
try {
|
|
549
|
+
var r = await Uploader._fetch(request);
|
|
550
|
+
if (r.ok) {
|
|
551
|
+
fields.undone = true;
|
|
552
|
+
Uploader.size = 0;
|
|
553
|
+
Uploader._tweenPercentage(.0, 300);
|
|
554
|
+
}
|
|
555
|
+
return r;
|
|
556
|
+
}
|
|
557
|
+
catch (e) {
|
|
558
|
+
}
|
|
559
|
+
finally {
|
|
560
|
+
// This input element accepts a filename, which may only be programmatically set to the empty string(!)
|
|
561
|
+
input.value = '';
|
|
562
|
+
Uploader.uploading = false;
|
|
563
|
+
return r;
|
|
564
|
+
}
|
|
565
|
+
}
|
|
566
|
+
_tweenPercentage(pct, duration = 500) {
|
|
567
|
+
const from = this.percentage;
|
|
568
|
+
return this._tweener.run(from, pct, duration, 0, Animations.Easings.sineInOut, (t, v) => {
|
|
569
|
+
this.percentage = v;
|
|
570
|
+
});
|
|
571
|
+
}
|
|
572
|
+
_retry(e) {
|
|
573
|
+
e.preventDefault();
|
|
574
|
+
e.stopPropagation();
|
|
575
|
+
this.failed = false;
|
|
576
|
+
this._manage();
|
|
577
|
+
}
|
|
578
|
+
_clear(e) {
|
|
579
|
+
e.preventDefault();
|
|
580
|
+
e.stopPropagation();
|
|
581
|
+
this.invalidFile = false;
|
|
582
|
+
const input = this.inputFields[0];
|
|
583
|
+
// https://stackoverflow.com/a/35323290
|
|
584
|
+
input.value = '';
|
|
585
|
+
if (!/safari/i.test(navigator.userAgent)) {
|
|
586
|
+
input.type = '';
|
|
587
|
+
input.type = 'file';
|
|
588
|
+
}
|
|
589
|
+
this.changeHandler(null);
|
|
590
|
+
}
|
|
591
|
+
};
|
|
592
|
+
__decorate([
|
|
593
|
+
Watch({ emit: false, converter: PropertyConverters.Json })
|
|
594
|
+
], PacemUploadElement.prototype, "fetchCredentials", void 0);
|
|
595
|
+
__decorate([
|
|
596
|
+
Watch({ emit: false, converter: PropertyConverters.String })
|
|
597
|
+
], PacemUploadElement.prototype, "fetchHeaders", void 0);
|
|
598
|
+
__decorate([
|
|
599
|
+
ViewChild('input[type=file]')
|
|
600
|
+
], PacemUploadElement.prototype, "_fileupload", void 0);
|
|
601
|
+
__decorate([
|
|
602
|
+
ViewChild(`${P}-tuner`)
|
|
603
|
+
], PacemUploadElement.prototype, "_tuner", void 0);
|
|
604
|
+
__decorate([
|
|
605
|
+
Watch({ converter: PropertyConverters.String })
|
|
606
|
+
], PacemUploadElement.prototype, "undoCaption", void 0);
|
|
607
|
+
__decorate([
|
|
608
|
+
Watch({ converter: PropertyConverters.String })
|
|
609
|
+
], PacemUploadElement.prototype, "retryCaption", void 0);
|
|
610
|
+
__decorate([
|
|
611
|
+
Watch({ converter: PropertyConverters.String })
|
|
612
|
+
], PacemUploadElement.prototype, "clearCaption", void 0);
|
|
613
|
+
__decorate([
|
|
614
|
+
Watch({ emit: false, reflectBack: true, converter: PropertyConverters.String })
|
|
615
|
+
], PacemUploadElement.prototype, "pattern", void 0);
|
|
616
|
+
__decorate([
|
|
617
|
+
Watch({ emit: false, reflectBack: true, converter: PropertyConverters.String })
|
|
618
|
+
], PacemUploadElement.prototype, "url", void 0);
|
|
619
|
+
__decorate([
|
|
620
|
+
Watch({ emit: false, reflectBack: true, converter: PropertyConverters.Number })
|
|
621
|
+
], PacemUploadElement.prototype, "maxImageWidth", void 0);
|
|
622
|
+
__decorate([
|
|
623
|
+
Watch({ emit: false, reflectBack: true, converter: PropertyConverters.Number })
|
|
624
|
+
], PacemUploadElement.prototype, "parallelism", void 0);
|
|
625
|
+
__decorate([
|
|
626
|
+
Watch({ emit: false, reflectBack: true, converter: PropertyConverters.Number })
|
|
627
|
+
], PacemUploadElement.prototype, "chunkSize", void 0);
|
|
628
|
+
__decorate([
|
|
629
|
+
Watch({ emit: false, reflectBack: true, converter: PropertyConverters.Number })
|
|
630
|
+
], PacemUploadElement.prototype, "maxImageHeight", void 0);
|
|
631
|
+
__decorate([
|
|
632
|
+
Watch({ emit: false, reflectBack: true, converter: PropertyConverters.Number })
|
|
633
|
+
], PacemUploadElement.prototype, "maxSize", void 0);
|
|
634
|
+
__decorate([
|
|
635
|
+
Watch({ reflectBack: true, converter: PropertyConverters.String })
|
|
636
|
+
], PacemUploadElement.prototype, "selectMode", void 0);
|
|
637
|
+
__decorate([
|
|
638
|
+
Watch({ converter: PropertyConverters.Boolean })
|
|
639
|
+
], PacemUploadElement.prototype, "uploading", void 0);
|
|
640
|
+
__decorate([
|
|
641
|
+
Watch({ converter: PropertyConverters.Number })
|
|
642
|
+
], PacemUploadElement.prototype, "size", void 0);
|
|
643
|
+
__decorate([
|
|
644
|
+
Watch({ converter: PropertyConverters.Number })
|
|
645
|
+
], PacemUploadElement.prototype, "percentage", void 0);
|
|
646
|
+
__decorate([
|
|
647
|
+
Watch({ converter: PropertyConverters.Boolean })
|
|
648
|
+
], PacemUploadElement.prototype, "complete", void 0);
|
|
649
|
+
__decorate([
|
|
650
|
+
Watch({ converter: PropertyConverters.Boolean })
|
|
651
|
+
], PacemUploadElement.prototype, "failed", void 0);
|
|
652
|
+
__decorate([
|
|
653
|
+
Watch({ converter: PropertyConverters.Boolean })
|
|
654
|
+
], PacemUploadElement.prototype, "invalidFile", void 0);
|
|
655
|
+
PacemUploadElement = __decorate([
|
|
656
|
+
CustomElement({
|
|
657
|
+
tagName: P + '-upload', shadow: Defaults.USE_SHADOW_ROOT,
|
|
658
|
+
template: `<${P}-button on-click=":host._dispatchDownload($event)" class="${PCSS}-upload ${PCSS}-viewfinder flat" css-class="{{ {'upload-chosen': !$pacem.isNullOrEmpty(:host.value) && !:host.uploading, 'upload-error': ${ERROR_BINDING}, 'upload-readonly': :host.readonly } }}">
|
|
659
|
+
|
|
660
|
+
<${P}-panel class="upload-button" hide="{{ (:host.uploading || :host.failed) && !$pacem.isNullOrEmpty(:host.url) }}">
|
|
661
|
+
<${P}-button tooltip="{{ :host.readonly ? :host.viewValue : 'upload' }}" class="button-circle pos-relative overflow-hidden"
|
|
662
|
+
icon-glyph="{{ :host._getMimeIcon(:host.value, :host.uploading, :host.selectMode) }}"
|
|
663
|
+
on-click=":host._clickHandler($event)"
|
|
664
|
+
on-mouseover="$this.iconGlyph = :host._getMimeIcon(:host.readonly ? :host.value : '', :host.uploading, :host.selectMode)"
|
|
665
|
+
on-mouseout="$this.iconGlyph = :host._getMimeIcon(:host.value, :host.uploading, :host.selectMode)"
|
|
666
|
+
css-class="{{ {'${PCSS}-anim anim-rotate': :host.uploading, 'button-error': :host.invalidFile, 'button-primary': $pacem.isNullOrEmpty(:host.value) && !:host.invalidFile, 'button-success': !Pacem.Utils.isNullOrEmpty(:host.value) && !:host.invalidFile } }}">
|
|
667
|
+
<input type="file" class="${PCSS}-transparent ${PCSS}-clickable pos-absolute absolute-left absolute-right absolute-top absolute-bottom" />
|
|
668
|
+
</${P}-button>
|
|
669
|
+
</${P}-panel>
|
|
670
|
+
<${P}-panel class="upload-button" hide="{{ !:host.failed }}">
|
|
671
|
+
<${P}-button class="button-circle button-flat" icon-glyph="refresh"
|
|
672
|
+
tooltip="{{ :host.retryCaption }}" on-click=":host._retry($event)"><${P}-text text="{{ :host.retryCaption }}"></${P}-text></${P}-button>
|
|
673
|
+
</${P}-panel>
|
|
674
|
+
<${P}-panel class="upload-button" hide="{{ !:host.uploading || $pacem.isNullOrEmpty(:host.url) }}">
|
|
675
|
+
<${P}-button class="button-circle button-flat" icon-glyph="clear" tooltip="{{ :host.undoCaption }}"
|
|
676
|
+
on-click=":host._undo($event)"><${P}-text text="{{ :host.undoCaption }}"></${P}-text></${P}-button>
|
|
677
|
+
</${P}-panel>
|
|
678
|
+
|
|
679
|
+
<${P}-span tooltip="{{ :host.viewValue }}"
|
|
680
|
+
hide="{{ $pacem.isNullOrEmpty(:host.value) || :host.uploading }}" class="upload-data readonly text-reset display-block ${PCSS}-anim text-truncate text-left ${PCSS}-pad pad-right-3" text="{{ :host.viewValue }}"></${P}-span>
|
|
681
|
+
|
|
682
|
+
<${P}-panel class="upload-progress hit-none" hide="{{ :host.readonly || (!$pacem.isNullOrEmpty(:host.value) && !:host.uploading) }}">
|
|
683
|
+
<${P}-tuner value="{{ :host.percentage }}" css-class="{{ {'tuner-success': !:host.invalidFile, 'tuner-error': :host.invalidFile} }}" interactive="false"></${P}-tuner>
|
|
684
|
+
</${P}-panel>
|
|
685
|
+
|
|
686
|
+
<${P}-button class="button-circle button-flat clear-button" icon-glyph="clear" hide="{{ $pacem.isNullOrEmpty(:host.value) || :host.uploading }}" tooltip="{{ :host.clearCaption }}" on-click=":host._clear($event)"></${P}-button>
|
|
687
|
+
|
|
688
|
+
</${P}-button>`
|
|
689
|
+
})
|
|
690
|
+
], PacemUploadElement);
|
|
691
|
+
export { PacemUploadElement };
|