-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy patht-image.html
430 lines (354 loc) · 10.8 KB
/
t-image.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
420
421
422
423
424
425
426
427
428
429
430
<!--
@license
Copyright (c) 2015 The Polymer Project Authors. All rights reserved.
This code may only be used under the BSD style license found at http://polymer.github.io/LICENSE.txt
The complete set of authors may be found at http://polymer.github.io/AUTHORS.txt
The complete set of contributors may be found at http://polymer.github.io/CONTRIBUTORS.txt
Code distributed by Google as part of the polymer project is also
subject to an additional IP rights grant found at http://polymer.github.io/PATENTS.txt
-->
<link rel="import" href="../polymer/polymer.html">
<link rel="import" href="../iron-flex-layout/iron-flex-layout-classes.html" />
<!--
`t-image` is an element for displaying an image that provides useful sizing and
preloading options not found on the standard `<img>` tag.
The `sizing` option allows the image to be either cropped (`cover`) or
letterboxed (`contain`) to fill a fixed user-size placed on the element.
The `preload` option prevents the browser from rendering the image until the
image is fully loaded. In the interim, either the element's CSS `background-color`
can be be used as the placeholder, or the `placeholder` property can be
set to a URL (preferably a data-URI, for instant rendering) for an
placeholder image.
The `fade` option (only valid when `preload` is set) will cause the placeholder
image/color to be faded out once the image is rendered.
Examples:
Basically identical to <img src="..."> tag:
<t-image src="http://lorempixel.com/400/400"></t-image>
Will letterbox the image to fit:
<t-image style="width:400px; height:400px;" sizing="contain"
src="http://lorempixel.com/600/400"></t-image>
Will crop the image to fit:
<t-image style="width:400px; height:400px;" sizing="cover"
src="http://lorempixel.com/600/400"></t-image>
Will show light-gray background until the image loads:
<t-image style="width:400px; height:400px; background-color: lightgray;"
sizing="cover" preload src="http://lorempixel.com/600/400"></t-image>
Will show a base-64 encoded placeholder image until the image loads:
<t-image style="width:400px; height:400px;" placeholder="data:image/gif;base64,..."
sizing="cover" preload src="http://lorempixel.com/600/400"></t-image>
Will fade the light-gray background out once the image is loaded:
<t-image style="width:400px; height:400px; background-color: lightgray;"
sizing="cover" preload fade src="http://lorempixel.com/600/400"></t-image>
@group Iron Elements
@element t-image
@demo demo/index.html
-->
<dom-module id="t-image">
<style include="iron-flex-alignment"></style>
<style include="iron-positioning"></style>
<style include="iron-flex">
:host {
display: inline-block;
overflow: hidden;
position: relative;
}
:host([sizing]) #img {
display: none;
}
#placeholder {
background-color: inherit;
opacity: 1;
}
#placeholder.faded-out {
transition: opacity 0.5s linear;
opacity: 0;
}
#loadingOverlay{
background: var(--t-image-bg, #ccc);
color: var(--t-image-icon-color, #fff);
padding:10px;
}
#loadingIcon{
max-height: 100px;
max-width: 100px;
width: auto;
height: auto;
}
</style>
<template>
<template is="dom-if" if="[[!loaded]]">
<div class="layout horizontal center center-justified fit" id="loadingOverlay">
<iron-icon id="loadingIcon" icon="[[loadingIcon]]"></iron-icon>
</div>
</template>
<img id="img" role="none" hidden$="[[_computeImageVisibility(sizing)]]">
<div id="placeholder" hidden$="[[_computePlaceholderVisibility(fade,loaded,preload)]]" class$="[[_computePlaceholderClassName(fade,loaded,preload)]]"></div>
<content></content>
</template>
</dom-module>
<script>
Polymer({
is: 't-image',
properties: {
/**
* The URL of an image.
*
* @attribute src
* @type string
* @default ''
*/
src: {
observer: '_srcChanged',
type: String,
value: ''
},
/**
* The icon to put when theres no source
*
* @attribute src
* @type string
* @default 'perm-media'
*/
loadingIcon: {
type: String,
value: 'perm-media'
},
/**
* When true, the image is prevented from loading and any placeholder is
* shown. This may be useful when a binding to the src property is known to
* be invalid, to prevent 404 requests.
*
* @attribute preventLoad
* @type boolean
* @default false
*/
preventLoad: {
type: Boolean,
value: false
},
/**
* Sets a sizing option for the image. Valid values are `contain` (full
* aspect ratio of the image is contained within the element and
* letterboxed) or `cover` (image is cropped in order to fully cover the
* bounds of the element), or `null` (default: image takes natural size).
*
* @attribute sizing
* @type string
* @default null
*/
sizing: {
type: String,
reflectToAttribute:true,
value: "cover"
},
/**
* When a sizing option is uzed (`cover` or `contain`), this determines
* how the image is aligned within the element bounds.
*
* @attribute position
* @type string
* @default 'center'
*/
position: {
type: String,
value: 'center'
},
/**
* When `true`, any change to the `src` property will cause the `placeholder`
* image to be shown until the
*
* @attribute preload
* @type boolean
* @default false
*/
preload: {
reflectToAttribute:true,
type: Boolean,
value: true
},
/**
* This image will be used as a background/placeholder until the src image has
* loaded. Use of a data-URI for placeholder is encouraged for instant rendering.
*
* @attribute placeholder
* @type string
* @default null
*/
placeholder: {
reflectToAttribute:true,
type: String,
value: null
},
/**
* When `preload` is true, setting `fade` to true will cause the image to
* fade into place.
*
* @attribute fade
* @type boolean
* @default false
*/
fade: {
reflectToAttribute:true,
type: Boolean,
value: false
},
/**
* Read-only value that is true when the image is loaded.
*
* @attribute preloaded
* @type boolean
* @default false
*/
loaded: {
notify: true,
type: Boolean,
value: false
},
/**
* Read-only value that tracks the loading state of the image when the `preload`
* option is used.
*
* @attribute loading
* @type boolean
* @default false
*/
loading: {
notify: true,
type: Boolean,
value: false
},
/**
* Can be used to set the width of image (e.g. via binding); size may also be
* set via CSS.
*
* @attribute width
* @type number
* @default null
*/
width: {
observer: '_widthChanged',
type: Number,
reflectToAttribute:true,
value: null
},
/**
* Can be used to set the height of image (e.g. via binding); size may also be
* set via CSS.
*
* @attribute height
* @type number
* @default null
*/
height: {
observer: '_heightChanged',
type: Number,
reflectToAttribute:true,
value: null
},
_placeholderBackgroundUrl: {
type: String,
computed: '_computePlaceholderBackgroundUrl(preload,placeholder)',
observer: '_placeholderBackgroundUrlChanged'
},
requiresPreload: {
type: Boolean,
computed: '_computeRequiresPreload(preload,loaded)'
},
canLoad: {
type: Boolean,
computed: '_computeCanLoad(preventLoad, src)'
}
},
observers: [
'_transformChanged(sizing, position)',
'_loadBehaviorChanged(canLoad, preload, loaded)',
'_loadStateChanged(src, preload, loaded)',
],
ready: function() {
if (!this.hasAttribute('role')) {
this.setAttribute('role', 'img');
}
},
_computeImageVisibility: function() {
return !!this.sizing;
},
_computePlaceholderVisibility: function() {
return !this.preload || (this.loaded && !this.fade);
},
_computePlaceholderClassName: function() {
if (!this.preload) {
return '';
}
var className = 'fit';
if (this.loaded && this.fade) {
className += ' faded-out';
}
return className;
},
_computePlaceholderBackgroundUrl: function() {
if (this.preload && this.placeholder) {
return 'url(' + this.placeholder + ')';
}
return null;
},
_computeRequiresPreload: function() {
return this.preload && !this.loaded;
},
_computeCanLoad: function() {
return Boolean(!this.preventLoad && this.src);
},
_widthChanged: function() {
this.style.width = isNaN(this.width) ? this.width : this.width + 'px';
},
_heightChanged: function() {
this.style.height = isNaN(this.height) ? this.height : this.height + 'px';
},
_srcChanged: function(newSrc, oldSrc) {
if (newSrc !== oldSrc) {
this.loaded = false;
}
},
_placeholderBackgroundUrlChanged: function() {
this.$.placeholder.style.backgroundImage =
this._placeholderBackgroundUrl;
},
_transformChanged: function() {
var placeholderStyle = this.$.placeholder.style;
this.style.backgroundSize =
placeholderStyle.backgroundSize = this.sizing;
this.style.backgroundPosition =
placeholderStyle.backgroundPosition =
this.sizing ? this.position : null;
this.style.backgroundRepeat =
placeholderStyle.backgroundRepeat =
this.sizing ? 'no-repeat' : null;
},
_loadBehaviorChanged: function() {
var img;
if (!this.canLoad) {
return;
}
if (this.requiresPreload) {
img = new Image();
img.src = this.src;
this.loading = true;
img.onload = function() {
this.loading = false;
this.loaded = true;
}.bind(this);
} else {
this.loaded = true;
}
},
_loadStateChanged: function() {
if (this.requiresPreload) {
return;
}
if (this.sizing) {
this.style.backgroundImage = this.src ? 'url(' + this.src + ')': '';
} else {
this.$.img.src = this.src || '';
}
}
});
</script>