Last active
May 9, 2017 03:55
-
-
Save smith/b4d30f860856f6bcaddb to your computer and use it in GitHub Desktop.
Gravatar Component for Angular 2
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import {Component} from "angular2/core"; | |
const md5 = require("blueimp-md5"); | |
@Component({ | |
selector: "gravatar", | |
inputs: ["defaultStyle", "email", "size"], | |
template: ` | |
<img class="my-gravatar" | |
width="{{size || DEFAULT_SIZE}}" height="{{size || DEFAULT_SIZE}}" | |
src='{{gravatarUrl(defaultStyle, email, size)}}'>` | |
}) | |
export class GravatarComponent { | |
private DEFAULT_SIZE: number = 80; | |
private gravatarUrl(defaultStyle: string = "retro", | |
email: string, size: number = this.DEFAULT_SIZE) { | |
defaultStyle = encodeURIComponent(defaultStyle); | |
return `https://secure.gravatar.com/avatar/ | |
${md5(email.toLowerCase().trim())}? | |
d=${defaultStyle}& | |
s=${size}`.replace(/\s/g, ""); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment