Skip to content

Commit c95676d

Browse files
author
Paolo Pino
committed
Replaced MaterialAvatar with a more complete Gravatar version
1 parent 57cf98d commit c95676d

File tree

1 file changed

+51
-62
lines changed

1 file changed

+51
-62
lines changed

src/main/java/gwt/material/design/addins/client/avatar/MaterialAvatar.java

Lines changed: 51 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -20,18 +20,15 @@
2020
* #L%
2121
*/
2222

23-
import com.google.gwt.dom.client.Document;
2423
import gwt.material.design.addins.client.MaterialAddins;
25-
import gwt.material.design.addins.client.dnd.MaterialDndClientBundle;
26-
import gwt.material.design.addins.client.dnd.MaterialDndDebugClientBundle;
2724
import gwt.material.design.client.MaterialDesignBase;
28-
import gwt.material.design.client.base.MaterialWidget;
25+
import gwt.material.design.client.ui.MaterialImage;
2926

3027
//@formatter:off
3128

3229
/**
33-
* Generated avatar based on @link(https://jdenticon.com/)
34-
* provides a unique avatar based on unique name.
30+
* Gravatar images based on @link(https://gravatar.com/)
31+
* provides user avatar from email or a unique version of it.
3532
*
3633
* <h3>XML Namespace Declaration</h3>
3734
* <pre>
@@ -42,94 +39,86 @@
4239
*
4340
* <h3>UiBinder Usage:</h3>
4441
* <pre>
42+
* Simple usage
4543
* {@code
4644
*
47-
* <ma:avatar.MaterialAvatar name="kevzlou7979" width="80" height="80"/>
45+
* <ma:avatar.MaterialAvatar width="80" height="80"/>
4846
*
4947
* }
5048
* </pre>
5149
*
52-
* @author kevzlou7979
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+
*
59+
* @author paulux84
5360
* @see <a href="http://gwtmaterialdesign.github.io/gwt-material-demo/#avatar">Material Avatar</a>
5461
*/
5562
//@formatter:on
56-
public class MaterialAvatar extends MaterialWidget {
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+
}
5774

5875
static {
5976
if(MaterialAddins.isDebug()) {
60-
MaterialDesignBase.injectDebugJs(MaterialAvatarDebugClientBundle.INSTANCE.jdenticonDebugJs());
61-
MaterialDesignBase.injectDebugJs(MaterialAvatarDebugClientBundle.INSTANCE.jdenticonDebugJs());
77+
MaterialDesignBase.injectDebugJs(MaterialAvatarDebugClientBundle.INSTANCE.md5DebugJs());
6278
} else {
63-
MaterialDesignBase.injectJs(MaterialAvatarClientBundle.INSTANCE.jdenticonJs());
6479
MaterialDesignBase.injectJs(MaterialAvatarClientBundle.INSTANCE.md5Js());
6580
}
66-
6781
}
6882

69-
private String name;
83+
private static final String gravatarUrl="https://www.gravatar.com/avatar/";
84+
private DefaultAvatarType defaultImage;
85+
private String value;
7086

71-
public MaterialAvatar() {
72-
super(Document.get().createCanvasElement());
73-
}
74-
75-
public MaterialAvatar(String name) {
76-
this();
77-
setName(name);
78-
}
79-
80-
@Override
81-
protected void onLoad() {
82-
super.onLoad();
83-
if(getName() != null) {
84-
initialize();
85-
}
87+
public MaterialAvatar(){
88+
super(gravatarUrl+"?&d="+DefaultAvatarType.identicon);
8689
}
8790

8891
/**
89-
* Get the name of the avatar
90-
* @return
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
9195
*/
92-
public String getName() {
93-
return name;
96+
public void setValue(String value){
97+
this.value=value;
98+
setUrl("https://www.gravatar.com/avatar/"+ generateHashCode(value)+"?&d="+defaultImage);
9499
}
95100

96101
/**
97-
* Set the name of the avatar and hashed it using md5 js library to
98-
* pass it into jdenticon avatar process
99-
* @param name
102+
* Get the value of the avatar
103+
* @return an email or a simple string
100104
*/
101-
public void setName(String name) {
102-
this.name = name;
103-
}
104-
105-
@Override
106-
public void setWidth(String width) {
107-
getElement().setAttribute("width", width);
108-
}
109-
110-
@Override
111-
public void setHeight(String height) {
112-
getElement().setAttribute("height", height);
105+
public String getValue() {
106+
return value;
113107
}
114108

115109
/**
116-
* Generate hash code - needed by jdenticon to generate avatar
117-
* @param value
118-
* @return
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
119114
*/
115+
public void setDefaultAvatarType(DefaultAvatarType defaultAvatarType){
116+
this.defaultImage=defaultAvatarType;
117+
setUrl(gravatarUrl+"?&d="+defaultImage);
118+
}
119+
120120
protected native String generateHashCode(String value) /*-{
121121
return $wnd.md5(value);
122122
}-*/;
123123

124-
/**
125-
* Initialize the avatar process - useful when trying to update your avatar
126-
*/
127-
public void initialize() {
128-
getElement().setAttribute("data-jdenticon-hash", generateHashCode(getName()));
129-
update();
130-
}
131-
132-
protected native void update() /*-{
133-
$wnd.jdenticon();
134-
}-*/;
135124
}

0 commit comments

Comments
 (0)