| OLD | NEW |
| (Empty) |
| 1 <!-- | |
| 2 @license | |
| 3 Copyright (c) 2015 The Polymer Project Authors. All rights reserved. | |
| 4 This code may only be used under the BSD style license found at http://polymer.g
ithub.io/LICENSE.txt | |
| 5 The complete set of authors may be found at http://polymer.github.io/AUTHORS.txt | |
| 6 The complete set of contributors may be found at http://polymer.github.io/CONTRI
BUTORS.txt | |
| 7 Code distributed by Google as part of the polymer project is also | |
| 8 subject to an additional IP rights grant found at http://polymer.github.io/PATEN
TS.txt | |
| 9 --> | |
| 10 | |
| 11 <link rel="import" href="../polymer/polymer.html"> | |
| 12 <link rel="import" href="../iron-flex-layout/classes/iron-flex-layout.html"> | |
| 13 | |
| 14 <!-- | |
| 15 `iron-image` is an element for displaying an image that provides useful sizing a
nd | |
| 16 preloading options not found on the standard `<img>` tag. | |
| 17 | |
| 18 The `sizing` option allows the image to be either cropped (`cover`) or | |
| 19 letterboxed (`contain`) to fill a fixed user-size placed on the element. | |
| 20 | |
| 21 The `preload` option prevents the browser from rendering the image until the | |
| 22 image is fully loaded. In the interim, either the element's CSS `background-col
or` | |
| 23 can be be used as the placeholder, or the `placeholder` property can be | |
| 24 set to a URL (preferably a data-URI, for instant rendering) for an | |
| 25 placeholder image. | |
| 26 | |
| 27 The `fade` option (only valid when `preload` is set) will cause the placeholder | |
| 28 image/color to be faded out once the image is rendered. | |
| 29 | |
| 30 Examples: | |
| 31 | |
| 32 Basically identical to <img src="..."> tag: | |
| 33 | |
| 34 <iron-image src="http://lorempixel.com/400/400"></iron-image> | |
| 35 | |
| 36 Will letterbox the image to fit: | |
| 37 | |
| 38 <iron-image style="width:400px; height:400px;" sizing="contain" | |
| 39 src="http://lorempixel.com/600/400"></iron-image> | |
| 40 | |
| 41 Will crop the image to fit: | |
| 42 | |
| 43 <iron-image style="width:400px; height:400px;" sizing="cover" | |
| 44 src="http://lorempixel.com/600/400"></iron-image> | |
| 45 | |
| 46 Will show light-gray background until the image loads: | |
| 47 | |
| 48 <iron-image style="width:400px; height:400px; background-color: lightgray;" | |
| 49 sizing="cover" preload src="http://lorempixel.com/600/400"></iron-image> | |
| 50 | |
| 51 Will show a base-64 encoded placeholder image until the image loads: | |
| 52 | |
| 53 <iron-image style="width:400px; height:400px;" placeholder="data:image/gif;b
ase64,..." | |
| 54 sizing="cover" preload src="http://lorempixel.com/600/400"></iron-image> | |
| 55 | |
| 56 Will fade the light-gray background out once the image is loaded: | |
| 57 | |
| 58 <iron-image style="width:400px; height:400px; background-color: lightgray;" | |
| 59 sizing="cover" preload fade src="http://lorempixel.com/600/400"></iron-ima
ge> | |
| 60 | |
| 61 | |
| 62 @group Iron Elements | |
| 63 @element iron-image | |
| 64 @demo demo/index.html | |
| 65 --> | |
| 66 | |
| 67 <dom-module id="iron-image"> | |
| 68 | |
| 69 <style> | |
| 70 | |
| 71 :host { | |
| 72 display: inline-block; | |
| 73 overflow: hidden; | |
| 74 position: relative; | |
| 75 } | |
| 76 | |
| 77 :host([sizing]) #img { | |
| 78 display: none; | |
| 79 } | |
| 80 | |
| 81 #placeholder { | |
| 82 background-color: inherit; | |
| 83 opacity: 1; | |
| 84 } | |
| 85 | |
| 86 #placeholder.faded-out { | |
| 87 transition: opacity 0.5s linear; | |
| 88 opacity: 0; | |
| 89 } | |
| 90 | |
| 91 </style> | |
| 92 | |
| 93 <template> | |
| 94 | |
| 95 <img id="img" role="none" hidden$="[[_computeImageVisibility(sizing)]]"> | |
| 96 <div id="placeholder" hidden$="[[_computePlaceholderVisibility(fade,loaded,p
reload)]]" class$="[[_computePlaceholderClassName(fade,loaded,preload)]]"></div> | |
| 97 <content></content> | |
| 98 | |
| 99 </template> | |
| 100 | |
| 101 </dom-module> | |
| 102 | |
| 103 <script> | |
| 104 | |
| 105 Polymer({ | |
| 106 | |
| 107 is: 'iron-image', | |
| 108 | |
| 109 properties: { | |
| 110 /** | |
| 111 * The URL of an image. | |
| 112 * | |
| 113 * @attribute src | |
| 114 * @type string | |
| 115 * @default '' | |
| 116 */ | |
| 117 src: { | |
| 118 observer: '_srcChanged', | |
| 119 type: String, | |
| 120 value: '' | |
| 121 }, | |
| 122 | |
| 123 /** | |
| 124 * When true, the image is prevented from loading and any placeholder is | |
| 125 * shown. This may be useful when a binding to the src property is known
to | |
| 126 * be invalid, to prevent 404 requests. | |
| 127 * | |
| 128 * @attribute preventLoad | |
| 129 * @type boolean | |
| 130 * @default false | |
| 131 */ | |
| 132 preventLoad: { | |
| 133 type: Boolean, | |
| 134 value: false | |
| 135 }, | |
| 136 | |
| 137 /** | |
| 138 * Sets a sizing option for the image. Valid values are `contain` (full | |
| 139 * aspect ratio of the image is contained within the element and | |
| 140 * letterboxed) or `cover` (image is cropped in order to fully cover the | |
| 141 * bounds of the element), or `null` (default: image takes natural size). | |
| 142 * | |
| 143 * @attribute sizing | |
| 144 * @type string | |
| 145 * @default null | |
| 146 */ | |
| 147 sizing: { | |
| 148 type: String, | |
| 149 value: null | |
| 150 }, | |
| 151 | |
| 152 /** | |
| 153 * When a sizing option is uzed (`cover` or `contain`), this determines | |
| 154 * how the image is aligned within the element bounds. | |
| 155 * | |
| 156 * @attribute position | |
| 157 * @type string | |
| 158 * @default 'center' | |
| 159 */ | |
| 160 position: { | |
| 161 type: String, | |
| 162 value: 'center' | |
| 163 }, | |
| 164 | |
| 165 /** | |
| 166 * When `true`, any change to the `src` property will cause the `placehold
er` | |
| 167 * image to be shown until the | |
| 168 * | |
| 169 * @attribute preload | |
| 170 * @type boolean | |
| 171 * @default false | |
| 172 */ | |
| 173 preload: { | |
| 174 type: Boolean, | |
| 175 value: false | |
| 176 }, | |
| 177 | |
| 178 /** | |
| 179 * This image will be used as a background/placeholder until the src image
has | |
| 180 * loaded. Use of a data-URI for placeholder is encouraged for instant re
ndering. | |
| 181 * | |
| 182 * @attribute placeholder | |
| 183 * @type string | |
| 184 * @default null | |
| 185 */ | |
| 186 placeholder: { | |
| 187 type: String, | |
| 188 value: null | |
| 189 }, | |
| 190 | |
| 191 /** | |
| 192 * When `preload` is true, setting `fade` to true will cause the image to | |
| 193 * fade into place. | |
| 194 * | |
| 195 * @attribute fade | |
| 196 * @type boolean | |
| 197 * @default false | |
| 198 */ | |
| 199 fade: { | |
| 200 type: Boolean, | |
| 201 value: false | |
| 202 }, | |
| 203 | |
| 204 /** | |
| 205 * Read-only value that is true when the image is loaded. | |
| 206 * | |
| 207 * @attribute preloaded | |
| 208 * @type boolean | |
| 209 * @default false | |
| 210 */ | |
| 211 loaded: { | |
| 212 notify: true, | |
| 213 type: Boolean, | |
| 214 value: false | |
| 215 }, | |
| 216 | |
| 217 /** | |
| 218 * Read-only value that tracks the loading state of the image when the `pr
eload` | |
| 219 * option is used. | |
| 220 * | |
| 221 * @attribute loading | |
| 222 * @type boolean | |
| 223 * @default false | |
| 224 */ | |
| 225 loading: { | |
| 226 notify: true, | |
| 227 type: Boolean, | |
| 228 value: false | |
| 229 }, | |
| 230 | |
| 231 /** | |
| 232 * Can be used to set the width of image (e.g. via binding); size may also
be | |
| 233 * set via CSS. | |
| 234 * | |
| 235 * @attribute width | |
| 236 * @type number | |
| 237 * @default null | |
| 238 */ | |
| 239 width: { | |
| 240 observer: '_widthChanged', | |
| 241 type: Number, | |
| 242 value: null | |
| 243 }, | |
| 244 | |
| 245 /** | |
| 246 * Can be used to set the height of image (e.g. via binding); size may als
o be | |
| 247 * set via CSS. | |
| 248 * | |
| 249 * @attribute height | |
| 250 * @type number | |
| 251 * @default null | |
| 252 */ | |
| 253 height: { | |
| 254 observer: '_heightChanged', | |
| 255 type: Number, | |
| 256 value: null | |
| 257 }, | |
| 258 | |
| 259 _placeholderBackgroundUrl: { | |
| 260 type: String, | |
| 261 computed: '_computePlaceholderBackgroundUrl(preload,placeholder)', | |
| 262 observer: '_placeholderBackgroundUrlChanged' | |
| 263 }, | |
| 264 | |
| 265 requiresPreload: { | |
| 266 type: Boolean, | |
| 267 computed: '_computeRequiresPreload(preload,loaded)' | |
| 268 }, | |
| 269 | |
| 270 canLoad: { | |
| 271 type: Boolean, | |
| 272 computed: '_computeCanLoad(preventLoad, src)' | |
| 273 } | |
| 274 | |
| 275 }, | |
| 276 | |
| 277 observers: [ | |
| 278 '_transformChanged(sizing, position)', | |
| 279 '_loadBehaviorChanged(canLoad, preload, loaded)', | |
| 280 '_loadStateChanged(src, preload, loaded)', | |
| 281 ], | |
| 282 | |
| 283 ready: function() { | |
| 284 if (!this.hasAttribute('role')) { | |
| 285 this.setAttribute('role', 'img'); | |
| 286 } | |
| 287 }, | |
| 288 | |
| 289 _computeImageVisibility: function() { | |
| 290 return !!this.sizing; | |
| 291 }, | |
| 292 | |
| 293 _computePlaceholderVisibility: function() { | |
| 294 return !this.preload || (this.loaded && !this.fade); | |
| 295 }, | |
| 296 | |
| 297 _computePlaceholderClassName: function() { | |
| 298 if (!this.preload) { | |
| 299 return ''; | |
| 300 } | |
| 301 | |
| 302 var className = 'fit'; | |
| 303 if (this.loaded && this.fade) { | |
| 304 className += ' faded-out'; | |
| 305 } | |
| 306 return className; | |
| 307 }, | |
| 308 | |
| 309 _computePlaceholderBackgroundUrl: function() { | |
| 310 if (this.preload && this.placeholder) { | |
| 311 return 'url(' + this.placeholder + ')'; | |
| 312 } | |
| 313 | |
| 314 return null; | |
| 315 }, | |
| 316 | |
| 317 _computeRequiresPreload: function() { | |
| 318 return this.preload && !this.loaded; | |
| 319 }, | |
| 320 | |
| 321 _computeCanLoad: function() { | |
| 322 return Boolean(!this.preventLoad && this.src); | |
| 323 }, | |
| 324 | |
| 325 _widthChanged: function() { | |
| 326 this.style.width = isNaN(this.width) ? this.width : this.width + 'px'; | |
| 327 }, | |
| 328 | |
| 329 _heightChanged: function() { | |
| 330 this.style.height = isNaN(this.height) ? this.height : this.height + 'px'; | |
| 331 }, | |
| 332 | |
| 333 _srcChanged: function(newSrc, oldSrc) { | |
| 334 if (newSrc !== oldSrc) { | |
| 335 this.loaded = false; | |
| 336 } | |
| 337 }, | |
| 338 | |
| 339 _placeholderBackgroundUrlChanged: function() { | |
| 340 this.$.placeholder.style.backgroundImage = | |
| 341 this._placeholderBackgroundUrl; | |
| 342 }, | |
| 343 | |
| 344 _transformChanged: function() { | |
| 345 var placeholderStyle = this.$.placeholder.style; | |
| 346 | |
| 347 this.style.backgroundSize = | |
| 348 placeholderStyle.backgroundSize = this.sizing; | |
| 349 | |
| 350 this.style.backgroundPosition = | |
| 351 placeholderStyle.backgroundPosition = | |
| 352 this.sizing ? this.position : null; | |
| 353 | |
| 354 this.style.backgroundRepeat = | |
| 355 placeholderStyle.backgroundRepeat = | |
| 356 this.sizing ? 'no-repeat' : null; | |
| 357 }, | |
| 358 | |
| 359 _loadBehaviorChanged: function() { | |
| 360 var img; | |
| 361 | |
| 362 if (!this.canLoad) { | |
| 363 return; | |
| 364 } | |
| 365 | |
| 366 if (this.requiresPreload) { | |
| 367 img = new Image(); | |
| 368 img.src = this.src; | |
| 369 | |
| 370 this.loading = true; | |
| 371 | |
| 372 img.onload = function() { | |
| 373 this.loading = false; | |
| 374 this.loaded = true; | |
| 375 }.bind(this); | |
| 376 } else { | |
| 377 this.loaded = true; | |
| 378 } | |
| 379 }, | |
| 380 | |
| 381 _loadStateChanged: function() { | |
| 382 if (this.requiresPreload) { | |
| 383 return; | |
| 384 } | |
| 385 | |
| 386 if (this.sizing) { | |
| 387 this.style.backgroundImage = this.src ? 'url(' + this.src + ')': ''; | |
| 388 } else { | |
| 389 this.$.img.src = this.src || ''; | |
| 390 } | |
| 391 } | |
| 392 }); | |
| 393 | |
| 394 </script> | |
| OLD | NEW |