@vaadin/upload 24.2.0-alpha1 → 24.2.0-alpha10

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.
@@ -6,10 +6,12 @@
6
6
  import '@vaadin/progress-bar/src/vaadin-progress-bar.js';
7
7
  import './vaadin-upload-icons.js';
8
8
  import { html, PolymerElement } from '@polymer/polymer/polymer-element.js';
9
- import { FocusMixin } from '@vaadin/a11y-base/src/focus-mixin.js';
10
9
  import { ControllerMixin } from '@vaadin/component-base/src/controller-mixin.js';
11
- import { SlotController } from '@vaadin/component-base/src/slot-controller.js';
12
- import { ThemableMixin } from '@vaadin/vaadin-themable-mixin/vaadin-themable-mixin.js';
10
+ import { registerStyles, ThemableMixin } from '@vaadin/vaadin-themable-mixin/vaadin-themable-mixin.js';
11
+ import { UploadFileMixin } from './vaadin-upload-file-mixin.js';
12
+ import { uploadFileStyles } from './vaadin-upload-file-styles.js';
13
+
14
+ registerStyles('vaadin-upload-file', uploadFileStyles, { moduleId: 'vaadin-upload-file-styles' });
13
15
 
14
16
  /**
15
17
  * `<vaadin-upload-file>` element represents a file in the file list of `<vaadin-upload>`.
@@ -48,38 +50,12 @@ import { ThemableMixin } from '@vaadin/vaadin-themable-mixin/vaadin-themable-mix
48
50
  *
49
51
  * @extends HTMLElement
50
52
  * @mixes ControllerMixin
51
- * @mixes FocusMixin
53
+ * @mixes UploadFileMixin
52
54
  * @mixes ThemableMixin
53
55
  */
54
- class UploadFile extends FocusMixin(ThemableMixin(ControllerMixin(PolymerElement))) {
56
+ class UploadFile extends UploadFileMixin(ThemableMixin(ControllerMixin(PolymerElement))) {
55
57
  static get template() {
56
58
  return html`
57
- <style>
58
- :host {
59
- display: block;
60
- }
61
-
62
- [hidden] {
63
- display: none;
64
- }
65
-
66
- [part='row'] {
67
- list-style-type: none;
68
- }
69
-
70
- button {
71
- background: transparent;
72
- padding: 0;
73
- border: none;
74
- box-shadow: none;
75
- }
76
-
77
- :host([complete]) ::slotted([slot='progress']),
78
- :host([error]) ::slotted([slot='progress']) {
79
- display: none !important;
80
- }
81
- </style>
82
-
83
59
  <div part="row">
84
60
  <div part="info">
85
61
  <div part="done-icon" hidden$="[[!complete]]" aria-hidden="true"></div>
@@ -129,173 +105,6 @@ class UploadFile extends FocusMixin(ThemableMixin(ControllerMixin(PolymerElement
129
105
  return 'vaadin-upload-file';
130
106
  }
131
107
 
132
- static get properties() {
133
- return {
134
- /**
135
- * True if uploading is completed, false otherwise.
136
- */
137
- complete: {
138
- type: Boolean,
139
- value: false,
140
- reflectToAttribute: true,
141
- },
142
-
143
- /**
144
- * Error message returned by the server, if any.
145
- */
146
- errorMessage: {
147
- type: String,
148
- value: '',
149
- observer: '_errorMessageChanged',
150
- },
151
-
152
- /**
153
- * The object representing a file.
154
- */
155
- file: {
156
- type: Object,
157
- },
158
-
159
- /**
160
- * Name of the uploading file.
161
- */
162
- fileName: {
163
- type: String,
164
- },
165
-
166
- /**
167
- * True if uploading is not started, false otherwise.
168
- */
169
- held: {
170
- type: Boolean,
171
- value: false,
172
- },
173
-
174
- /**
175
- * True if remaining time is unknown, false otherwise.
176
- */
177
- indeterminate: {
178
- type: Boolean,
179
- value: false,
180
- reflectToAttribute: true,
181
- },
182
-
183
- /**
184
- * The object used to localize this component.
185
- */
186
- i18n: {
187
- type: Object,
188
- },
189
-
190
- /**
191
- * Number representing the uploading progress.
192
- */
193
- progress: {
194
- type: Number,
195
- },
196
-
197
- /**
198
- * Uploading status.
199
- */
200
- status: {
201
- type: String,
202
- },
203
-
204
- /**
205
- * Indicates whether the element can be focused and where it participates in sequential keyboard navigation.
206
- * @protected
207
- */
208
- tabindex: {
209
- type: Number,
210
- value: 0,
211
- reflectToAttribute: true,
212
- },
213
-
214
- /**
215
- * True if uploading is in progress, false otherwise.
216
- */
217
- uploading: {
218
- type: Boolean,
219
- value: false,
220
- reflectToAttribute: true,
221
- },
222
-
223
- /** @private */
224
- _progress: {
225
- type: Object,
226
- },
227
- };
228
- }
229
-
230
- static get observers() {
231
- return ['__updateProgress(_progress, progress, indeterminate)'];
232
- }
233
-
234
- /** @protected */
235
- ready() {
236
- super.ready();
237
-
238
- this.addController(
239
- new SlotController(this, 'progress', 'vaadin-progress-bar', {
240
- initializer: (progress) => {
241
- this._progress = progress;
242
- },
243
- }),
244
- );
245
-
246
- // Handle moving focus to the button on Tab.
247
- this.shadowRoot.addEventListener('focusin', (e) => {
248
- const target = e.composedPath()[0];
249
-
250
- if (target.getAttribute('part').endsWith('button')) {
251
- this._setFocused(false);
252
- }
253
- });
254
-
255
- // Handle moving focus from the button on Shift Tab.
256
- this.shadowRoot.addEventListener('focusout', (e) => {
257
- if (e.relatedTarget === this) {
258
- this._setFocused(true);
259
- }
260
- });
261
- }
262
-
263
- /**
264
- * Override method inherited from `FocusMixin` to mark the file as focused
265
- * only when the host is focused.
266
- * @param {Event} event
267
- * @return {boolean}
268
- * @protected
269
- */
270
- _shouldSetFocus(event) {
271
- return event.composedPath()[0] === this;
272
- }
273
-
274
- /** @private */
275
- _errorMessageChanged(errorMessage) {
276
- this.toggleAttribute('error', Boolean(errorMessage));
277
- }
278
-
279
- /** @private */
280
- __updateProgress(progress, value, indeterminate) {
281
- if (progress) {
282
- progress.value = isNaN(value) ? 0 : value / 100;
283
- progress.indeterminate = indeterminate;
284
- }
285
- }
286
-
287
- /** @private */
288
- _fireFileEvent(e) {
289
- e.preventDefault();
290
- return this.dispatchEvent(
291
- new CustomEvent(e.target.getAttribute('file-event'), {
292
- detail: { file: this.file },
293
- bubbles: true,
294
- composed: true,
295
- }),
296
- );
297
- }
298
-
299
108
  /**
300
109
  * Fired when the retry button is pressed. It is listened by `vaadin-upload`
301
110
  * which will start a new upload process of this file.
@@ -0,0 +1,254 @@
1
+ /**
2
+ * @license
3
+ * Copyright (c) 2016 - 2023 Vaadin Ltd.
4
+ * This program is available under Apache License Version 2.0, available at https://vaadin.com/license/
5
+ */
6
+ import type { Constructor } from '@open-wc/dedupe-mixin';
7
+
8
+ export interface UploadFile extends File {
9
+ uploadTarget: string;
10
+ elapsed: number;
11
+ elapsedStr: string;
12
+ remaining: number;
13
+ remainingStr: string;
14
+ progress: number;
15
+ speed: number;
16
+ totalStr: string;
17
+ loaded: number;
18
+ loadedStr: string;
19
+ status: string;
20
+ error: string;
21
+ abort?: boolean;
22
+ complete?: boolean;
23
+ uploading?: boolean;
24
+ }
25
+
26
+ export interface UploadI18n {
27
+ dropFiles: {
28
+ one: string;
29
+ many: string;
30
+ };
31
+ addFiles: {
32
+ one: string;
33
+ many: string;
34
+ };
35
+ error: {
36
+ tooManyFiles: string;
37
+ fileIsTooBig: string;
38
+ incorrectFileType: string;
39
+ };
40
+ uploading: {
41
+ status: {
42
+ connecting: string;
43
+ stalled: string;
44
+ processing: string;
45
+ held: string;
46
+ };
47
+ remainingTime: {
48
+ prefix: string;
49
+ unknown: string;
50
+ };
51
+ error: {
52
+ serverUnavailable: string;
53
+ unexpectedServerError: string;
54
+ forbidden: string;
55
+ };
56
+ };
57
+ units: {
58
+ size: string[];
59
+ sizeBase?: number;
60
+ };
61
+ formatSize?(bytes: number): string;
62
+ formatTime?(seconds: number, units: number[]): string;
63
+ }
64
+
65
+ export type UploadMethod = 'POST' | 'PUT';
66
+
67
+ export declare function UploadMixin<T extends Constructor<HTMLElement>>(base: T): Constructor<UploadMixinClass> & T;
68
+
69
+ export declare class UploadMixinClass {
70
+ /**
71
+ * Define whether the element supports dropping files on it for uploading.
72
+ * By default it's enabled in desktop and disabled in touch devices
73
+ * because mobile devices do not support drag events in general. Setting
74
+ * it false means that drop is enabled even in touch-devices, and true
75
+ * disables drop in all devices.
76
+ */
77
+ nodrop: boolean;
78
+
79
+ /**
80
+ * The server URL. The default value is an empty string, which means that
81
+ * _window.location_ will be used.
82
+ */
83
+ target: string;
84
+
85
+ /**
86
+ * HTTP Method used to send the files. Only POST and PUT are allowed.
87
+ */
88
+ method: UploadMethod;
89
+
90
+ /**
91
+ * Key-Value map to send to the server. If you set this property as an
92
+ * attribute, use a valid JSON string, for example:
93
+ * ```
94
+ * <vaadin-upload headers='{"X-Foo": "Bar"}'></vaadin-upload>
95
+ * ```
96
+ */
97
+ headers: object | string | null;
98
+
99
+ /**
100
+ * Max time in milliseconds for the entire upload process, if exceeded the
101
+ * request will be aborted. Zero means that there is no timeout.
102
+ */
103
+ timeout: number;
104
+
105
+ /**
106
+ * The array of files being processed, or already uploaded.
107
+ *
108
+ * Each element is a [`File`](https://developer.mozilla.org/en-US/docs/Web/API/File)
109
+ * object with a number of extra properties to track the upload process:
110
+ * - `uploadTarget`: The target URL used to upload this file.
111
+ * - `elapsed`: Elapsed time since the upload started.
112
+ * - `elapsedStr`: Human-readable elapsed time.
113
+ * - `remaining`: Number of seconds remaining for the upload to finish.
114
+ * - `remainingStr`: Human-readable remaining time for the upload to finish.
115
+ * - `progress`: Percentage of the file already uploaded.
116
+ * - `speed`: Upload speed in kB/s.
117
+ * - `size`: File size in bytes.
118
+ * - `totalStr`: Human-readable total size of the file.
119
+ * - `loaded`: Bytes transferred so far.
120
+ * - `loadedStr`: Human-readable uploaded size at the moment.
121
+ * - `status`: Status of the upload process.
122
+ * - `error`: Error message in case the upload failed.
123
+ * - `abort`: True if the file was canceled by the user.
124
+ * - `complete`: True when the file was transferred to the server.
125
+ * - `uploading`: True while transferring data to the server.
126
+ */
127
+ files: UploadFile[];
128
+
129
+ /**
130
+ * Limit of files to upload, by default it is unlimited. If the value is
131
+ * set to one, native file browser will prevent selecting multiple files.
132
+ * @attr {number} max-files
133
+ */
134
+ maxFiles: number;
135
+
136
+ /**
137
+ * Specifies if the maximum number of files have been uploaded
138
+ * @attr {boolean} max-files-reached
139
+ */
140
+ readonly maxFilesReached: boolean;
141
+
142
+ /**
143
+ * Specifies the types of files that the server accepts.
144
+ * Syntax: a comma-separated list of MIME type patterns (wildcards are
145
+ * allowed) or file extensions.
146
+ * Notice that MIME types are widely supported, while file extensions
147
+ * are only implemented in certain browsers, so avoid using it.
148
+ * Example: accept="video/*,image/tiff" or accept=".pdf,audio/mp3"
149
+ */
150
+ accept: string;
151
+
152
+ /**
153
+ * Specifies the maximum file size in bytes allowed to upload.
154
+ * Notice that it is a client-side constraint, which will be checked before
155
+ * sending the request. Obviously you need to do the same validation in
156
+ * the server-side and be sure that they are aligned.
157
+ * @attr {number} max-file-size
158
+ */
159
+ maxFileSize: number;
160
+
161
+ /**
162
+ * Specifies the 'name' property at Content-Disposition
163
+ * @attr {string} form-data-name
164
+ */
165
+ formDataName: string;
166
+
167
+ /**
168
+ * Prevents upload(s) from immediately uploading upon adding file(s).
169
+ * When set, you must manually trigger uploads using the `uploadFiles` method
170
+ * @attr {boolean} no-auto
171
+ */
172
+ noAuto: boolean;
173
+
174
+ /**
175
+ * Set the withCredentials flag on the request.
176
+ * @attr {boolean} with-credentials
177
+ */
178
+ withCredentials: boolean;
179
+
180
+ /**
181
+ * Pass-through to input's capture attribute. Allows user to trigger device inputs
182
+ * such as camera or microphone immediately.
183
+ */
184
+ capture: string | null | undefined;
185
+
186
+ /**
187
+ * The object used to localize this component.
188
+ * For changing the default localization, change the entire
189
+ * _i18n_ object or just the property you want to modify.
190
+ *
191
+ * The object has the following JSON structure and default values:
192
+ *
193
+ * ```
194
+ * {
195
+ * dropFiles: {
196
+ * one: 'Drop file here',
197
+ * many: 'Drop files here'
198
+ * },
199
+ * addFiles: {
200
+ * one: 'Select File...',
201
+ * many: 'Upload Files...'
202
+ * },
203
+ * error: {
204
+ * tooManyFiles: 'Too Many Files.',
205
+ * fileIsTooBig: 'File is Too Big.',
206
+ * incorrectFileType: 'Incorrect File Type.'
207
+ * },
208
+ * uploading: {
209
+ * status: {
210
+ * connecting: 'Connecting...',
211
+ * stalled: 'Stalled',
212
+ * processing: 'Processing File...',
213
+ * held: 'Queued'
214
+ * },
215
+ * remainingTime: {
216
+ * prefix: 'remaining time: ',
217
+ * unknown: 'unknown remaining time'
218
+ * },
219
+ * error: {
220
+ * serverUnavailable: 'Server Unavailable',
221
+ * unexpectedServerError: 'Unexpected Server Error',
222
+ * forbidden: 'Forbidden'
223
+ * }
224
+ * },
225
+ * file: {
226
+ * retry: 'Retry',
227
+ * start: 'Start',
228
+ * remove: 'Remove'
229
+ * },
230
+ * units: {
231
+ * size: ['B', 'kB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'],
232
+ * sizeBase: 1000
233
+ * },
234
+ * formatSize: function(bytes) {
235
+ * // returns the size followed by the best suitable unit
236
+ * },
237
+ * formatTime: function(seconds, [secs, mins, hours]) {
238
+ * // returns a 'HH:MM:SS' string
239
+ * }
240
+ * }
241
+ * ```
242
+ *
243
+ * @type {!UploadI18n}
244
+ * @default {English}
245
+ */
246
+ i18n: UploadI18n;
247
+
248
+ /**
249
+ * Triggers the upload of any files that are not completed
250
+ *
251
+ * @param files Files being uploaded. Defaults to all outstanding files
252
+ */
253
+ uploadFiles(files?: UploadFile | UploadFile[]): void;
254
+ }