Skip to content

Instantly share code, notes, and snippets.

@zacharytamas
Created August 20, 2014 01:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zacharytamas/ef12ab6b67b5d2ba0477 to your computer and use it in GitHub Desktop.
Save zacharytamas/ef12ab6b67b5d2ba0477 to your computer and use it in GitHub Desktop.
<img-placeholder>
<link rel="import" href="../bower_components/polymer/polymer.html">
<polymer-element name="img-placeholder"
attributes="width height src alt">
<template>
<img src="http://placehold.it/{{ generatedSrc }}"
width="{{ width }}"
height="{{ height }}"
alt="{{ alt }}">
</template>
<script>
Polymer('img-placeholder', {
// Defaults
width: 256,
height: null,
generatedSrc: null,
ready: function() {
this.calculateSrc();
},
calculateSrc: function () {
var extension = '.gif',
srcArray = [];
// If no height is provided, make the image square.
if (! this.height) {
this.height = this.width;
}
// If a `src` attribute is supplied, mostly throw it away,
// but use it to find the image extension used.
if (this.src) {
extension = this.src.slice(this.src.lastIndexOf('.'));
}
srcArray.push(this.width, 'x', this.height, extension);
// If an `alt` attribute is supplied, use it as text for
// the image.
if (this.alt) {
srcArray.push('&text=' + encodeURIComponent(this.alt));
}
this.generatedSrc = srcArray.join('');
},
attributeChanged: function() {
this.calculateSrc();
}
});
</script>
</polymer-element>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment