-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathxenon-file-drop.html
283 lines (259 loc) · 10.1 KB
/
xenon-file-drop.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
<link rel="import" href="../polymer/polymer.html">
<link rel="import" href="../iron-flex-layout/classes/iron-flex-layout.html">
<!--
### Quick Start
`xenon-file-drop` is a wrapper around an invisible `file` input. The most basic use case is to style the `xenon-file-drop` directly, and set the appropriate attributes (`required`, `accept`, `multiple`, `name`) for the `file` input.
```css
xenon-file-drop {
width: 200px;
height: 200px;
background-color: #fafafa;
}
```
```html
<xenon-file-drop accept="image/*" files="{{files}}"></xenon-file-drop>
```
You can also customize the interior of the `xenon-file-drop` via `content` named `drop-zone`.
```html
<xenon-file-drop multiple accept="image/*" files="{{files}}">
<div slot="drop-zone">
<iron-icon icon="description"></iron-icon>
<h3>Drop your file here</h3>
</div>
</xenon-file-drop>
```
### Installation
```
bower install --save acaxlabs/xenon-file-drop
```
### Styling
You can style `xenon-file-drop` normally, but there are 3 additional states available for styling.
**.dragover**
You can style `xenon-file-drop` when a file is dragged over the drop-zone with the `dragover` class.
```css
xenon-file-drop.dragover { border: 1px dashed grey; }
```
**.errored**
You can style `xenon-file-drop` when there is any error with the `errored` class.
```css
xenon-file-drop.errored { border: 1px dashed red; }
```
**[has-files]**
You can style `xenon-file-drop` when there are at least 1 selected file with the `has-files` attribute.
```css
xenon-file-drop[has-files] { border: 1px solid grey; }
```
* @customElement
* @polymer
* @demo demo/index.html
-->
<dom-module id="xenon-file-drop">
<template>
<style include="iron-flex iron-flex-alignment">
:host {
display: block;
box-sizing: border-box;
cursor: pointer;
@apply(--layout-vertical);
@apply(--layout-center);
@apply(--layout-center-justified);
}
#files { opacity: 0; }
</style>
<input id="files"
type="file"
name$="[[name]]"
accept$="[[accept]]"
required$="[[required]]"
on-change="_onFilePick" />
<content select="drop-zone"></content>
</template>
<script>
Polymer({
is: 'xenon-file-drop',
/**
* Fired when files are selected.
*
* @event selected
* @param {FileList} fileList [FileList](https://developer.mozilla.org/en-US/docs/Web/API/FileList) of selected files.
*/
/**
* Fired when error is encountered.
*
* @event error
* @param {ErrorEvent} error ErrorEvent
*/
properties: {
/**
* Indicates if any files are selected.
* @type {Boolean}
*/
hasFiles: {
type: Boolean,
notify: true,
readOnly: true,
reflectToAttribute: true,
computed: '_hasFiles(files)'
},
/**
* Specifies that the user must fill in a value before submitting a form.
* @type {Boolean}
*/
required: {
type: Boolean,
reflectToAttribute: true
},
/**
* Indicate the types of files that the server accepts, otherwise it will
* be ignored. The value must be a comma-separated list of unique content
* type specifiers:
* - A file extension starting with the STOP character (U+002E). (e.g. .jpg, .png, .doc).
* - A valid MIME type with no extensions.
* - audio/* representing sound files.
* - video/* representing video files.
* - image/* representing image files.
*
* @type {String}
*/
accept: {
type: String,
reflectToAttribute: true
},
/**
* The name of the control, which is submitted with the form data.
* @type {String}
*/
name: {
type: String,
reflectToAttribute: true
},
/**
* An array of [Files](https://developer.mozilla.org/en-US/docs/Web/API/File) currently selected.
* @type {File[]}
*/
files: {
type: Array,
notify: true,
readOnly: true
},
/**
* The most recent [File](https://developer.mozilla.org/en-US/docs/Web/API/File) selected.
* @type {File}
*/
lastFile: {
type: Object,
notify: true,
readOnly: true,
computed: '_getLastFile(files)'
},
/**
* Number of Files selected.
* @type {Number}
*/
numFilesSelected: {
type: Number,
notify: true,
readOnly: true,
computed: '_getLen(files)'
},
/**
* Last error event.
* @type {{message: String}}
*/
lastError: {
type: Object,
notify: true,
readOnly: true,
observer: '_lastErrorChanged'
}
},
ready: function () {
this.addEventListener('dragover', e => this._onDragEvent(e));
this.addEventListener('dragleave', e => this._onDragEvent(e));
this.addEventListener('drop', e => this._onFileDrop(e));
this.addEventListener('click', e => this.$.files.click());
},
_lastErrorChanged(error) {
if (error) {
this.classList.toggle('errored', true);
this.dispatchEvent(error);
} else {
this.classList.toggle('errored', false);
}
},
_validate(fileList) {
if (!this.accept || this.accept.length === 0) return true;
var acceptList = this.accept.split(',').map(s => s.trim().toLowerCase());
if (acceptList.length === 0) return true;
var hasAudio = acceptList.indexOf('audio/*') >= 0;
var hasVideo = acceptList.indexOf('video/*') >= 0;
var hasImage = acceptList.indexOf('image/*') >= 0;
for (let i = 0, len = fileList.length; i < len; ++i) {
let ext =
'.' +
fileList[i].name
.split('.')
.pop()
.toLowerCase();
if (acceptList.indexOf(ext) >= 0) continue;
if (hasAudio && fileList[i].type.split('/')[0] === 'audio') continue;
if (hasVideo && fileList[i].type.split('/')[0] === 'video') continue;
if (hasImage && fileList[i].type.split('/')[0] === 'image') continue;
if (acceptList.indexOf(fileList[i].type) >= 0) continue;
// did not match anything in accept
let message = `${fileList[i]
.name} has invalid file format not found in accept attribute!`;
this._setLastError(new ErrorEvent('error', { message }));
return false;
}
return true;
},
_getLastFile(files) {
return files ? files[files.length - 1] : null;
},
_getLen(files) {
return files ? files.length : 0;
},
_hasFiles(files) {
return files && files.length > 0;
},
_onDragEvent(e) {
e.stopPropagation();
e.preventDefault();
this.classList.toggle('dragover', e.type === 'dragover');
e.dataTransfer.dropEffect = 'drop';
},
_onFilePick(e) {
e.stopPropagation();
e.preventDefault();
this._setLastError(null);
this._setFiles(this._toArray(e.target.files));
this.dispatchEvent(new CustomEvent('selected', { detail: e.target.files }));
},
_onFileDrop(e) {
e.stopPropagation();
e.preventDefault();
this._setLastError(null);
this.classList.toggle('dragover', false);
var files = e.dataTransfer.files;
if (!this.multiple && files.length > 1) {
let message = `${files.length} files selected when multiple attribute is not present!`;
this._setLastError(new ErrorEvent('error', { message }));
return;
}
var ok = this._validate(files);
if (ok) {
this._setFiles(this._toArray(files));
this.dispatchEvent(new CustomEvent('selected', { detail: files }));
}
},
_toArray(fileList) {
var a = [];
for (let i = 0, len = fileList.length; i < len; ++i) {
a.push(fileList.item(i));
}
return a;
}
});
</script>
</dom-module>