2020 * #L%
2121 */
2222
23+ import com .google .gwt .dom .client .Document ;
2324import gwt .material .design .addins .client .MaterialAddins ;
2425import gwt .material .design .client .MaterialDesignBase ;
25- import gwt .material .design .client .ui . MaterialImage ;
26+ import gwt .material .design .client .base . MaterialWidget ;
2627
2728//@formatter:off
2829
2930/**
30- * Gravatar images based on @link(https://gravatar .com/)
31- * provides user avatar from email or a unique version of it .
31+ * Generated avatar based on @link(https://jdenticon .com/)
32+ * provides a unique avatar based on unique name .
3233 *
3334 * <h3>XML Namespace Declaration</h3>
3435 * <pre>
3940 *
4041 * <h3>UiBinder Usage:</h3>
4142 * <pre>
42- * Simple usage
4343 * {@code
4444 *
45- * <ma:avatar.MaterialAvatar width="80" height="80"/>
45+ * <ma:avatar.MaterialAvatar name="kevzlou7979" width="80" height="80"/>
4646 *
4747 * }
4848 * </pre>
4949 *
50- * Gravatar usage
51- * {@code
52- *
53- * <ma:avatar.MaterialAvatar value="<email or any string>" width="80" height="80" defaultAvatarType="monsterid"/>
54- *
55- * }
56- * </pre>
57- *
58- * @author paulux84
5950 * @author kevzlou7979
6051 * @see <a href="http://gwtmaterialdesign.github.io/gwt-material-demo/#avatar">Material Avatar</a>
6152 */
6253//@formatter:on
63- public class MaterialAvatar extends MaterialImage {
64-
65-
66- public enum DefaultAvatarType {
67- mm ,
68- identicon ,
69- monsterid ,
70- wavatar ,
71- retro ,
72- blank
73- }
54+ public class MaterialAvatar extends MaterialWidget {
7455
7556 static {
7657 if (MaterialAddins .isDebug ()) {
77- MaterialDesignBase .injectDebugJs (MaterialAvatarDebugClientBundle .INSTANCE .md5DebugJs ());
58+ MaterialDesignBase .injectDebugJs (MaterialAvatarDebugClientBundle .INSTANCE .jdenticonDebugJs ());
59+ MaterialDesignBase .injectDebugJs (MaterialAvatarDebugClientBundle .INSTANCE .jdenticonDebugJs ());
7860 } else {
61+ MaterialDesignBase .injectJs (MaterialAvatarClientBundle .INSTANCE .jdenticonJs ());
7962 MaterialDesignBase .injectJs (MaterialAvatarClientBundle .INSTANCE .md5Js ());
8063 }
64+
8165 }
8266
83- private static final String gravatarUrl ="https://www.gravatar.com/avatar/" ;
84- private DefaultAvatarType defaultImage ;
85- private String value ;
67+ private String name ;
8668
87- public MaterialAvatar (){
88- super (gravatarUrl + "?&d=" + DefaultAvatarType . mm );
69+ public MaterialAvatar () {
70+ super (Document . get (). createCanvasElement () );
8971 }
9072
91- /**
92- * Set the value of the avatar and pass it's md5 hash to gravatar service.
93- * If an email is passed and that email is registered on gravat you get related avatar
94- * @param value a simple string or user email
95- */
96- public void setValue (String value ){
97- this .value =value ;
98- setUrl ("https://www.gravatar.com/avatar/" + generateHashCode (value )+"?&d=" +defaultImage );
73+ public MaterialAvatar (String name ) {
74+ this ();
75+ setName (name );
76+ }
77+
78+ @ Override
79+ protected void onLoad () {
80+ super .onLoad ();
81+ if (getName () != null ) {
82+ initialize ();
83+ }
9984 }
10085
10186 /**
102- * Get the value of the avatar
103- * @return an email or a simple string
87+ * Get the name of the avatar
88+ * @return
10489 */
105- public String getValue () {
106- return value ;
90+ public String getName () {
91+ return name ;
10792 }
10893
10994 /**
110- * In addition to allowing you to use user avatar, Gravatar has a number of built in options which you can also use as defaults.
111- * Most of these work by taking the requested email hash and using it to generate a themed image that is unique to that email address.
112- * To use these options, just pass one of the {@link DefaultAvatarType}
113- * @param defaultAvatarType
95+ * Set the name of the avatar and hashed it using md5 js library to
96+ * pass it into jdenticon avatar process
97+ * @param name
11498 */
115- public void setDefaultAvatarType (DefaultAvatarType defaultAvatarType ){
116- this .defaultImage =defaultAvatarType ;
117- setUrl (gravatarUrl +"?&d=" +defaultImage );
99+ public void setName (String name ) {
100+ this .name = name ;
101+ }
102+
103+ @ Override
104+ public void setWidth (String width ) {
105+ getElement ().setAttribute ("width" , width );
118106 }
119107
108+ @ Override
109+ public void setHeight (String height ) {
110+ getElement ().setAttribute ("height" , height );
111+ }
112+
113+ /**
114+ * Generate hash code - needed by jdenticon to generate avatar
115+ * @param value
116+ * @return
117+ */
120118 protected native String generateHashCode (String value ) /*-{
121119 return $wnd.md5(value);
122120 }-*/ ;
123121
124- }
122+ /**
123+ * Initialize the avatar process - useful when trying to update your avatar
124+ */
125+ public void initialize () {
126+ getElement ().setAttribute ("data-jdenticon-hash" , generateHashCode (getName ()));
127+ update ();
128+ }
129+
130+ protected native void update () /*-{
131+ $wnd.jdenticon();
132+ }-*/ ;
133+ }
0 commit comments