forked from Abe90/paper-avatar
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpaper-avatar.html
221 lines (177 loc) · 4.42 KB
/
paper-avatar.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
<link rel="import" href="../polymer/polymer.html">
<script src="lib/jdenticon-1.4.0.min.js"></script>
<script src="lib/md5.min.js"></script>
<!--
`paper-avatar`
User avatar in material style
### Styling
To change the background color:
paper-avatar {
--paper-avatar-color: red;
}
To change the size of the avatar:
paper-avatar {
--paper-avatar-width: 60px;
}
Custom property | Description | Default
----------------|-------------|----------
`--paper-avatar-width` | Size (width and height) of the avatar image | `40px`
`--paper-avatar-color` | Background color of the avatar image |
@demo demo/index.html
-->
<dom-module id="paper-avatar">
<template>
<style>
:host {
--paper-avatar-width: 40px;
}
:host {
display: inline-block;
box-sizing: border-box;
position: relative;
width: var(--paper-avatar-width);
height: var(--paper-avatar-width);
border-radius: 50%;
cursor: default;
background-color: var(--paper-avatar-color, --paper-avatar-bgcolor);
-webkit-user-select: none;
-moz-user-select: none;
-ms-user-select: none;
user-select: none;
}
:host > * {
pointer-events: none;
}
#label, #img, #jdenticon {
position: absolute;
top: 0;
left: 0;
right: 0;
bottom: 0;
width: 100%;
height: 100%;
border-radius: 50%;
}
#label {
overflow: hidden;
display: -ms-flexbox;
display: -webkit-flex;
display: flex;
-webkit-flex-direction: row;
-ms-flex-direction: row;
flex-direction: row;
-webkit-align-items: center;
-ms-flex-align: center;
align-items: center;
}
#label span {
display: block;
width: 100%;
font-weight: 400;
color: rgba(255, 255, 255, .8);
text-transform: capitalize;
font-family: 'Roboto', 'Noto', sans-serif;
-webkit-font-smoothing: antialiased;
text-align: center;
font-size: calc(var(--paper-avatar-width) / 1.65);
}
#jdenticon {
width: var(--paper-avatar-width);
height: var(--paper-avatar-width);
}
*[hidden] {
display: none !important;
}
</style>
<div id="label" title="[[label]]"><span>[[_label(label)]]</span></div>
<svg id="jdenticon" width="40" height="40"><content></content></svg>
<template is="dom-if" if="[[src]]">
<img id="img" src="[[src]]" title="[[label]]" on-load="_onImgLoad" on-error="_onImgError" title="[[color]]">
</template>
</template>
<script>
Polymer({
is : 'paper-avatar',
properties: {
/**
* Image address or base64
*/
src: {
type: String
},
/**
* Label with username
*/
label: {
type: String,
observer: '_observerLabel'
},
/**
* Show two chars in avatar
*/
twoChars: {
type: Boolean,
value: false
},
/**
* Array of colors for avatar background
*/
colors: {
type: Array,
},
/**
* Set true if you want use a jdenticon avatar
*/
jdenticon: {
type: Boolean,
value: false
}
},
_observerLabel: function(label){
if(label){
if(this.jdenticon){
this.$.label.hidden = true;
jdenticon.config = {
lightness: {
color: [1, 1],
grayscale: [1, 1]
},
saturation: 1
};
jdenticon.update(this.$.jdenticon, md5(label));
}
this.customStyle['--paper-avatar-bgcolor'] = this._parseColor(label);
this.updateStyles();
}
},
_label: function(label){
if(!label)
return "";
if(this.twoChars){
if(this.label.indexOf(" ") > -1){
var matches = this.label.match(/\b(\w)/g);
return matches[0] + matches[1];
} else {
return label.substring(0, 2);
}
}
return label.charAt(0);
},
_onImgLoad: function(e){
e.currentTarget.hidden = false;
},
_onImgError: function(e){
e.currentTarget.hidden = true;
},
_parseColor: function(label){
var colors = this.colors ? this.colors : ["#F44336", "#E91E63", "#9C27B0", "#673AB7", "#3F51B5", "#2196F3", "#03A9F4", "#00BCD4", "#795548", "#009688", "#4CAF50", "#8BC34A", "#CDDC39", "#FFEB3B", "#FFC107", "#FF9800", "#FF5722", "#9E9E9E", "#607D8B"];
var hash = 0;
for(var a = 0; a < label.length; a++)
hash += (label.charCodeAt(a) << 5);
if(hash >= colors.length)
return colors[hash % colors.length];
return colors[hash];
}
});
</script>
</dom-module>