OLD | NEW |
| (Empty) |
1 | |
2 | |
3 Polymer({ | |
4 | |
5 is: 'iron-image', | |
6 | |
7 properties: { | |
8 /** | |
9 * The URL of an image. | |
10 * | |
11 * @attribute src | |
12 * @type string | |
13 * @default '' | |
14 */ | |
15 src: { | |
16 observer: '_srcChanged', | |
17 type: String, | |
18 value: '' | |
19 }, | |
20 | |
21 /** | |
22 * When true, the image is prevented from loading and any placeholder is | |
23 * shown. This may be useful when a binding to the src property is known
to | |
24 * be invalid, to prevent 404 requests. | |
25 * | |
26 * @attribute preventLoad | |
27 * @type boolean | |
28 * @default false | |
29 */ | |
30 preventLoad: { | |
31 type: Boolean, | |
32 value: false | |
33 }, | |
34 | |
35 /** | |
36 * Sets a sizing option for the image. Valid values are `contain` (full | |
37 * aspect ratio of the image is contained within the element and | |
38 * letterboxed) or `cover` (image is cropped in order to fully cover the | |
39 * bounds of the element), or `null` (default: image takes natural size). | |
40 * | |
41 * @attribute sizing | |
42 * @type string | |
43 * @default null | |
44 */ | |
45 sizing: { | |
46 type: String, | |
47 value: null | |
48 }, | |
49 | |
50 /** | |
51 * When a sizing option is uzed (`cover` or `contain`), this determines | |
52 * how the image is aligned within the element bounds. | |
53 * | |
54 * @attribute position | |
55 * @type string | |
56 * @default 'center' | |
57 */ | |
58 position: { | |
59 type: String, | |
60 value: 'center' | |
61 }, | |
62 | |
63 /** | |
64 * When `true`, any change to the `src` property will cause the `placehold
er` | |
65 * image to be shown until the | |
66 * | |
67 * @attribute preload | |
68 * @type boolean | |
69 * @default false | |
70 */ | |
71 preload: { | |
72 type: Boolean, | |
73 value: false | |
74 }, | |
75 | |
76 /** | |
77 * This image will be used as a background/placeholder until the src image
has | |
78 * loaded. Use of a data-URI for placeholder is encouraged for instant re
ndering. | |
79 * | |
80 * @attribute placeholder | |
81 * @type string | |
82 * @default null | |
83 */ | |
84 placeholder: { | |
85 type: String, | |
86 value: null | |
87 }, | |
88 | |
89 /** | |
90 * When `preload` is true, setting `fade` to true will cause the image to | |
91 * fade into place. | |
92 * | |
93 * @attribute fade | |
94 * @type boolean | |
95 * @default false | |
96 */ | |
97 fade: { | |
98 type: Boolean, | |
99 value: false | |
100 }, | |
101 | |
102 /** | |
103 * Read-only value that is true when the image is loaded. | |
104 * | |
105 * @attribute preloaded | |
106 * @type boolean | |
107 * @default false | |
108 */ | |
109 loaded: { | |
110 notify: true, | |
111 type: Boolean, | |
112 value: false | |
113 }, | |
114 | |
115 /** | |
116 * Read-only value that tracks the loading state of the image when the `pr
eload` | |
117 * option is used. | |
118 * | |
119 * @attribute loading | |
120 * @type boolean | |
121 * @default false | |
122 */ | |
123 loading: { | |
124 notify: true, | |
125 type: Boolean, | |
126 value: false | |
127 }, | |
128 | |
129 /** | |
130 * Can be used to set the width of image (e.g. via binding); size may also
be | |
131 * set via CSS. | |
132 * | |
133 * @attribute width | |
134 * @type number | |
135 * @default null | |
136 */ | |
137 width: { | |
138 observer: '_widthChanged', | |
139 type: Number, | |
140 value: null | |
141 }, | |
142 | |
143 /** | |
144 * Can be used to set the height of image (e.g. via binding); size may als
o be | |
145 * set via CSS. | |
146 * | |
147 * @attribute height | |
148 * @type number | |
149 * @default null | |
150 */ | |
151 height: { | |
152 observer: '_heightChanged', | |
153 type: Number, | |
154 value: null | |
155 }, | |
156 | |
157 _placeholderBackgroundUrl: { | |
158 type: String, | |
159 computed: '_computePlaceholderBackgroundUrl(preload,placeholder)', | |
160 observer: '_placeholderBackgroundUrlChanged' | |
161 }, | |
162 | |
163 requiresPreload: { | |
164 type: Boolean, | |
165 computed: '_computeRequiresPreload(preload,loaded)' | |
166 }, | |
167 | |
168 canLoad: { | |
169 type: Boolean, | |
170 computed: '_computeCanLoad(preventLoad, src)' | |
171 } | |
172 | |
173 }, | |
174 | |
175 observers: [ | |
176 '_transformChanged(sizing, position)', | |
177 '_loadBehaviorChanged(canLoad, preload, loaded)', | |
178 '_loadStateChanged(src, preload, loaded)', | |
179 ], | |
180 | |
181 ready: function() { | |
182 if (!this.hasAttribute('role')) { | |
183 this.setAttribute('role', 'img'); | |
184 } | |
185 }, | |
186 | |
187 _computeImageVisibility: function() { | |
188 return !!this.sizing; | |
189 }, | |
190 | |
191 _computePlaceholderVisibility: function() { | |
192 return !this.preload || (this.loaded && !this.fade); | |
193 }, | |
194 | |
195 _computePlaceholderClassName: function() { | |
196 if (!this.preload) { | |
197 return ''; | |
198 } | |
199 | |
200 var className = 'fit'; | |
201 if (this.loaded && this.fade) { | |
202 className += ' faded-out'; | |
203 } | |
204 return className; | |
205 }, | |
206 | |
207 _computePlaceholderBackgroundUrl: function() { | |
208 if (this.preload && this.placeholder) { | |
209 return 'url(' + this.placeholder + ')'; | |
210 } | |
211 | |
212 return null; | |
213 }, | |
214 | |
215 _computeRequiresPreload: function() { | |
216 return this.preload && !this.loaded; | |
217 }, | |
218 | |
219 _computeCanLoad: function() { | |
220 return Boolean(!this.preventLoad && this.src); | |
221 }, | |
222 | |
223 _widthChanged: function() { | |
224 this.style.width = isNaN(this.width) ? this.width : this.width + 'px'; | |
225 }, | |
226 | |
227 _heightChanged: function() { | |
228 this.style.height = isNaN(this.height) ? this.height : this.height + 'px'; | |
229 }, | |
230 | |
231 _srcChanged: function(newSrc, oldSrc) { | |
232 if (newSrc !== oldSrc) { | |
233 this.loaded = false; | |
234 } | |
235 }, | |
236 | |
237 _placeholderBackgroundUrlChanged: function() { | |
238 this.$.placeholder.style.backgroundImage = | |
239 this._placeholderBackgroundUrl; | |
240 }, | |
241 | |
242 _transformChanged: function() { | |
243 var placeholderStyle = this.$.placeholder.style; | |
244 | |
245 this.style.backgroundSize = | |
246 placeholderStyle.backgroundSize = this.sizing; | |
247 | |
248 this.style.backgroundPosition = | |
249 placeholderStyle.backgroundPosition = | |
250 this.sizing ? this.position : null; | |
251 | |
252 this.style.backgroundRepeat = | |
253 placeholderStyle.backgroundRepeat = | |
254 this.sizing ? 'no-repeat' : null; | |
255 }, | |
256 | |
257 _loadBehaviorChanged: function() { | |
258 var img; | |
259 | |
260 if (!this.canLoad) { | |
261 return; | |
262 } | |
263 | |
264 if (this.requiresPreload) { | |
265 img = new Image(); | |
266 img.src = this.src; | |
267 | |
268 this.loading = true; | |
269 | |
270 img.onload = function() { | |
271 this.loading = false; | |
272 this.loaded = true; | |
273 }.bind(this); | |
274 } else { | |
275 this.loaded = true; | |
276 } | |
277 }, | |
278 | |
279 _loadStateChanged: function() { | |
280 if (this.requiresPreload) { | |
281 return; | |
282 } | |
283 | |
284 if (this.sizing) { | |
285 this.style.backgroundImage = this.src ? 'url(' + this.src + ')': ''; | |
286 } else { | |
287 this.$.img.src = this.src || ''; | |
288 } | |
289 } | |
290 }); | |
291 | |
OLD | NEW |