|
20 | 20 | * #L% |
21 | 21 | */ |
22 | 22 |
|
23 | | -import com.google.gwt.dom.client.Document; |
24 | 23 | 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; |
27 | 24 | import gwt.material.design.client.MaterialDesignBase; |
28 | | -import gwt.material.design.client.base.MaterialWidget; |
| 25 | +import gwt.material.design.client.ui.MaterialImage; |
29 | 26 |
|
30 | 27 | //@formatter:off |
31 | 28 |
|
32 | 29 | /** |
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. |
35 | 32 | * |
36 | 33 | * <h3>XML Namespace Declaration</h3> |
37 | 34 | * <pre> |
|
42 | 39 | * |
43 | 40 | * <h3>UiBinder Usage:</h3> |
44 | 41 | * <pre> |
| 42 | + * Simple usage |
45 | 43 | * {@code |
46 | 44 | * |
47 | | - * <ma:avatar.MaterialAvatar name="kevzlou7979" width="80" height="80"/> |
| 45 | + * <ma:avatar.MaterialAvatar width="80" height="80"/> |
48 | 46 | * |
49 | 47 | * } |
50 | 48 | * </pre> |
51 | 49 | * |
| 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 |
52 | 59 | * @author kevzlou7979 |
53 | 60 | * @see <a href="http://gwtmaterialdesign.github.io/gwt-material-demo/#avatar">Material Avatar</a> |
54 | 61 | */ |
55 | 62 | //@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 | + } |
57 | 74 |
|
58 | 75 | static { |
59 | 76 | if(MaterialAddins.isDebug()) { |
60 | | - MaterialDesignBase.injectDebugJs(MaterialAvatarDebugClientBundle.INSTANCE.jdenticonDebugJs()); |
61 | | - MaterialDesignBase.injectDebugJs(MaterialAvatarDebugClientBundle.INSTANCE.jdenticonDebugJs()); |
| 77 | + MaterialDesignBase.injectDebugJs(MaterialAvatarDebugClientBundle.INSTANCE.md5DebugJs()); |
62 | 78 | } else { |
63 | | - MaterialDesignBase.injectJs(MaterialAvatarClientBundle.INSTANCE.jdenticonJs()); |
64 | 79 | MaterialDesignBase.injectJs(MaterialAvatarClientBundle.INSTANCE.md5Js()); |
65 | 80 | } |
66 | | - |
67 | | - } |
68 | | - |
69 | | - private String name; |
70 | | - |
71 | | - public MaterialAvatar() { |
72 | | - super(Document.get().createCanvasElement()); |
73 | 81 | } |
74 | 82 |
|
75 | | - public MaterialAvatar(String name) { |
76 | | - this(); |
77 | | - setName(name); |
78 | | - } |
| 83 | + private static final String gravatarUrl="https://www.gravatar.com/avatar/"; |
| 84 | + private DefaultAvatarType defaultImage; |
| 85 | + private String value; |
79 | 86 |
|
80 | | - @Override |
81 | | - protected void onLoad() { |
82 | | - super.onLoad(); |
83 | | - if(getName() != null) { |
84 | | - initialize(); |
85 | | - } |
| 87 | + public MaterialAvatar(){ |
| 88 | + super(gravatarUrl+"?&d="+DefaultAvatarType.mm); |
86 | 89 | } |
87 | 90 |
|
88 | 91 | /** |
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 |
91 | 95 | */ |
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); |
94 | 99 | } |
95 | 100 |
|
96 | 101 | /** |
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 |
100 | 104 | */ |
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; |
113 | 107 | } |
114 | 108 |
|
115 | 109 | /** |
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 |
119 | 114 | */ |
| 115 | + public void setDefaultAvatarType(DefaultAvatarType defaultAvatarType){ |
| 116 | + this.defaultImage=defaultAvatarType; |
| 117 | + setUrl(gravatarUrl+"?&d="+defaultImage); |
| 118 | + } |
| 119 | + |
120 | 120 | protected native String generateHashCode(String value) /*-{ |
121 | 121 | return $wnd.md5(value); |
122 | 122 | }-*/; |
123 | 123 |
|
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 | | - }-*/; |
135 | 124 | } |
0 commit comments