-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhostabee-thumbnail-list.html
419 lines (388 loc) · 13.3 KB
/
hostabee-thumbnail-list.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
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
<!--
@license
Copyright (c) 2019 Hostabee SAS.
This program is available under Apache License Version 2.0.
-->
<link rel="import" href="../polymer/polymer-element.html">
<link rel="import" href="../polymer/lib/elements/dom-repeat.html">
<link rel="import" href="../iron-image/iron-image.html">
<link rel="import" href="../iron-icon/iron-icon.html">
<link rel="import" href="./hostabee-icons.html">
<dom-module id="hostabee-thumbnail-list">
<template>
<style>
:host {
border: 1px solid var(--divider-color, #dbdbdb);
border-radius: 10px;
background-color: var(--primary-background-color, #ffffff);
@apply --layout-horizontal;
@apply --layout-wrap;
}
:host([hidden]) {
visibility: hidden;
border: 0;
width: 0;
}
:host([disabled]) {
pointer-events: none;
opacity: 0.4;
}
.flex {
@apply --layout-flex;
}
.thumbnail {
overflow: hidden;
}
.thumbnail__title {
padding: 5px;
font-weight: 500;
background: white;
border-bottom: 1px solid var(--divider-color, #dbdbdb);
color: var(--secondary-text-color, #737373);
@apply --layout-horizontal;
}
.thumbnail__title>span {
line-height: 24px;
white-space: nowrap;
overflow: hidden;
text-overflow: ellipsis;
}
.thumbnail__picture {
display: block;
width: 100%;
height: 100%;
background-color: var(--primary-background-color, #ffffff);
}
.thumbnail__button--remove {
min-width: 24px;
color: var(--secondary-text-color, #737373);
cursor: pointer;
}
</style>
<template is="dom-repeat" items="{{thumbs}}" as="thumb" on-rendered-item-count-changed="_renderedItemsChanged">
<div class="thumbnail" on-click="_dispatchClickEvent">
<div class="thumbnail__title">
<span>[[thumb.name]]</span>
<span class="flex"></span>
<iron-icon id="remove-thumbnail-[[index]]" class="thumbnail__button--remove" icon="hostabee-icons:cancel" on-click="_removePicture" hidden$="[[!editable]]"></iron-icon>
</div>
<iron-image class="thumbnail__picture" src="[[thumb.url]]" alt="[[thumb.name]]" height="[[heightThumbnails]]px" sizing="cover" fade></iron-image>
</div>
</template>
</template>
<script>
/**
* `hostabee-thumbnail-list`
*
* Displays pictures as thumbnails in a square container. Width of pictures
* are automatically adjusted.
*
* @summary Displays pictures as thumbnails in a square container.
* @customElement
* @polymer
* @extends {Polymer.Element}
* @memberof Hostabee
* @demo demo/hostabee-thumbnail-list/basic.html Basic
*/
class HostabeeThumbnailList extends Polymer.Element {
static get is() {
return 'hostabee-thumbnail-list';
}
static get properties() {
return {
/**
* Is editable. If true the pictures can be removed from the list.
*
* @type {Boolean}
* @default false
*/
editable: {
type: Boolean,
value: false,
},
/**
* List of file to load.
* See: https://developer.mozilla.org/en-US/docs/Web/API/File
*
* @type {Array<File>}
* @default []
*/
files: {
type: Array,
value: [],
notify: true,
observer: '_filesChanged'
},
/**
* Limit of pictures in the list. Can't be set higher than 4.
*
* @type {Number}
* @default 4
*/
limit: {
type: Number,
value: 4,
observer: '_limitChanged'
},
/**
* Height of thumbnails in pixels.
*
* @type {Number}
* @default 200
*/
heightThumbnails: {
type: Number,
value: 200,
},
/**
* Thumbnails name and data URL.
*
* @type {Array}
* @default []
*/
thumbs: {
type: Array,
value: [],
readOnly: true,
notify: true,
},
/**
* EXIF data orientation must be fixed or not. Is `True` if the device
* loading the file is using a camera and if required libraries to
* fix the orientation are available in the app using this element.
*
* @type {Boolean}
*/
_mustFixPictureOrientation: {
type: Boolean,
value() {
return window.orientation != undefined &&
'loadImage' in window &&
'EXIF' in window;
},
}
};
}
/**
* Array of strings describing multi-property observer methods and their
* dependant properties
*/
static get observers() {
return [
'_computeThumbnails(files.*)',
];
}
/**
* Ensures list of file does not exceed defined limit.
*/
_filesChanged(files) {
if (!files) {
this.set('files', []);
} else if (files.length > this.limit) {
this.set('files', files.slice(0, this.limit));
}
}
/**
* Dispatches event with the file clicked.
*/
_dispatchClickEvent(event) {
this.dispatchEvent(new CustomEvent('thumbnail-click', {
bubbles: true,
composed: true,
detail: {
index: event.model.index,
file: event.model.file,
},
}));
}
/**
* Resizes the pictures and changes corners rounded according to the number
* of pictures displayed. Also updates the margin between each pictures.
*/
_renderedItemsChanged(event) {
let thumbnails = event.target.parentNode.querySelectorAll('.thumbnail');
this.toggleAttribute('hidden', thumbnails.length == 0);
let style;
if (thumbnails.length == 1) {
// Round corners
style = thumbnails[0].style;
style.setProperty('width', '100%');
style.setProperty('margin', '0px');
style.setProperty('border-radius', '9px');
} else if (thumbnails.length == 2) {
// Split width in half
thumbnails.forEach((tn) => tn.style.setProperty('width', 'calc(50% - 5px)'));
// Round top and bottom left corners
style = thumbnails[0].style;
style.setProperty('margin', '0px 5px 0px 0px');
style.setProperty('border-radius', '9px 0px 0px 9px');
// Round top and bottom right corners
style = thumbnails[1].style;
style.setProperty('margin', '0px 0px 0px 5px');
style.setProperty('border-radius', '0px 9px 9px 0px');
} else if (thumbnails.length == 3) {
// Split width in half and round top left corner
style = thumbnails[0].style;
style.setProperty('width', 'calc(50% - 5px)');
style.setProperty('margin', '0px 5px 5px 0px');
style.setProperty('border-radius', '9px 0px 0px 0px');
// Split width in half and round top right corner
style = thumbnails[1].style;
style.setProperty('width', 'calc(50% - 5px)');
style.setProperty('margin', '0px 0px 5px 5px');
style.setProperty('border-radius', '0px 9px 0px 0px');
// Full width and round bottom right and left corners
style = thumbnails[2].style;
style.setProperty('width', '100%');
style.setProperty('margin', '5px 0px 0px 0px');
style.setProperty('border-radius', '0px 0px 9px 9px');
} else if (thumbnails.length == 4) {
// Split width in half
thumbnails.forEach((tn) => tn.style.setProperty('width', 'calc(50% - 5px)'));
// Round top left corner
style = thumbnails[0].style;
style.setProperty('margin', '0px 5px 5px 0px');
style.setProperty('border-radius', '9px 0px 0px 0px');
// Round top right corner
style = thumbnails[1].style;
style.setProperty('margin', '0px 0px 5px 5px');
style.setProperty('border-radius', '0px 9px 0px 0px');
// Round bottom left corner
style = thumbnails[2].style;
style.setProperty('margin', '5px 5px 0px 0px');
style.setProperty('border-radius', '0px 0px 0px 9px');
// Round bottom right corner
style = thumbnails[3].style;
style.setProperty('margin', '5px 0px 0px 5px');
style.setProperty('border-radius', '0px 0px 9px 0px');
}
}
/**
* Ensures the limit max of displayed thumbnails is not higher than 4.
*
* @param {Number} count The limit max of pictures to be displayed
*/
_limitChanged(count) {
if (count > 4) {
this.limit = 4;
}
}
/**
* Removes a picture from the list. The picture which the cross icon was
* clicked is removed.
*
* @param {Event} event Click event.
*/
_removePicture(event) {
// Stop click event propagation on the thuhmbail
event.stopPropagation();
event.preventDefault();
let index = event.target.id.split('remove-thumbnail-')[1];
this.splice('files', index, 1);
}
/**
* Computes list of thumbnails to be displayed from the provided files.
*/
async _computeThumbnails(changeRecord) {
// Stop if no change
if (!changeRecord) {
return;
}
// Remove thumbnail accroding to the file removed from the list
if (changeRecord.path == 'files.splices') {
changeRecord.value.indexSplices
.forEach((s) => this.splice('thumbs', s.index, 1));
return;
}
// Get new list of files to convert into thumbnails
let files = [];
if (changeRecord.path == 'files') {
files = changeRecord.value.slice(0, this.limit);
} else if (changeRecord.path == 'files.length') {
files = changeRecord.base.slice(0, this.limit);
}
if (!this._checkIsTypeFile(files)) {
return;
}
let thumbs = await this._filesToThumbnails(files);
this._setThumbs(thumbs);
}
/**
* Converts a File into a Thumbnail.
*
* @param {Array<File>} files The file list to be converted into thumbnails
* @return {Array<Object>} list of thumbnails
*/
async _filesToThumbnails(files) {
try {
if (!this._mustFixPictureOrientation) {
return files.map((file) => Object.assign({}, {
name: file.name,
url: URL.createObjectURL(file)
}));
}
let thumbs = await Promise.all(files.map((file) => this._fixOrientation(file)));
return thumbs;
} catch (error) {
console.warn("Can't get URL. Provided files must be instances of File class.", error);
}
}
/**
* Checks if the given files are instacnes of class File.
*
* @param {Array} files The files to check the type of.
* @return {Boolean} True if all files are instances of File, false otherwise.
*/
_checkIsTypeFile(files) {
return files.reduce((valid, obj) => valid && obj.constructor.name == 'File', true);
}
/**
* Ensures the orientation of the EXIF (Exchangeable image file format) is
* properly defined on mobile device when the photo is took with a camera.
*
* For more information see this issue:
* https://stackoverflow.com/questions/20600800
*
* @param {File} file The file to fix the orientation of.
* @return {Promise<String>} a Promise which resolve the data URL of the
* image after have fixed the orientation.
*/
_fixOrientation(file) {
return new Promise((resolve, reject) => {
window.loadImage(file, async function(img) {
if (img.type === 'error') {
console.info('Could not fix image orientation.');
resolve({
name: file.name,
url: URL.createObjectURL(file),
});
}
// Fix image orientation
await window.EXIF.getData(img, async function() {
var orientation = EXIF.getTag(this, 'Orientation');
var canvas = await window.loadImage.scale(img, {
orientation: orientation || 0,
canvas: true,
noRevoke: true,
});
resolve({
name: file.name,
url: canvas.toDataURL(),
});
});
}, {
noRevoke: true,
});
});
}
/**
* Dispatched when a thumbnail is clicked. The detail of the event
* contains the `thumbnail` (a File) and its index in the list.
* _(bubbles: true, composed: true)_
*
* @event thumbnail-click
*/
}
window.customElements.define(HostabeeThumbnailList.is, HostabeeThumbnailList);
</script>
</dom-module>