-
Notifications
You must be signed in to change notification settings - Fork 34
/
ngletteravatar.js
238 lines (220 loc) · 9.35 KB
/
ngletteravatar.js
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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
/**
* NG Letter Avatar is directive for AngularJS apps
* @version v4.0.4 - 2016-06-01 * @link https://github.com/uttesh/ngletteravatar
* @author Uttesh Kumar T.H. <[email protected]>
* @license MIT License, http://www.opensource.org/licenses/MIT
*/
'use strict';
var nla = angular.module('ngLetterAvatar', []);
nla.constant('defaultSettings', {
alphabetcolors: ["#5A8770", "#B2B7BB", "#6FA9AB", "#F5AF29", "#0088B9", "#F18636", "#D93A37", "#A6B12E", "#5C9BBC", "#F5888D", "#9A89B5", "#407887", "#9A89B5", "#5A8770", "#D33F33", "#A2B01F", "#F0B126", "#0087BF", "#F18636", "#0087BF", "#B2B7BB", "#72ACAE", "#9C8AB4", "#5A8770", "#EEB424", "#407887"],
textColor: '#ffffff',
defaultBorder: 'border:5px solid white',
triangleup: 'width: 0;height: 0;border-left: 50px solid transparent;border-right: 50px solid transparent;border-bottom: 100px solid;',
fontsize: 30, // unit in pixels
height: 50, // unit in pixels
width: 50, // unit in pixels
fontWeight: 400, //
charCount: 1,
fontFamily: 'HelveticaNeue-Light,Helvetica Neue Light,Helvetica Neue,Helvetica, Arial,Lucida Grande, sans-serif',
base: 'data:image/svg+xml;base64,',
radius: 'border-radius:50%;',
custombgcolor: '',
dynamic: 'false',
rotatedeg: '0'
});
/**
* directive to create the avatar
* @param {type} param1
* @param {type} param2
*/
nla.directive('ngLetterAvatar', ['defaultSettings', function (defaultSettings) {
return {
restrict: 'AE',
replace: true,
scope: {
alphabetcolors: '=alphabetcolors',
data: '@'
},
link: function (scope, element, attrs) {
/**
* Populate the attribute values to params object
* @type type
*/
var params = {
charCount: attrs.charcount || defaultSettings.charCount,
data: attrs.data,
textColor: defaultSettings.textColor,
height: attrs.height || defaultSettings.height,
width: attrs.width || defaultSettings.width,
fontsize: attrs.fontsize || defaultSettings.fontsize,
fontWeight: attrs.fontweight || defaultSettings.fontWeight,
fontFamily: attrs.fontfamily || defaultSettings.fontFamily,
avatarBorderStyle: attrs.avatarcustomborder,
avatardefaultBorder: attrs.avatarborder,
defaultBorder: defaultSettings.defaultBorder,
shape: attrs.shape,
alphabetcolors: scope.alphabetcolors || defaultSettings.alphabetcolors,
avatarCustomBGColor: attrs.avatarcustombgcolor || defaultSettings.custombgcolor,
dynamic: attrs.dynamic || defaultSettings.dynamic,
rotatedeg: attrs.rotatedeg || defaultSettings.rotatedeg
};
/**
* to generate the avatar dynamically on data change, enable the below function to watch the event
*/
if (params.dynamic === 'true') {
scope.$watch('data', function () {
_generateLetterAvatar();
});
} else {
_generateLetterAvatar();
}
function _generateLetterAvatar() {
var c = '';
if (params.charCount == 2) {
var _data = getFirstAndLastName(scope.data.toUpperCase());
if (_data) {
c = _data;
} else {
c = scope.data.substr(0, params.charCount).toUpperCase();
}
} else {
c = scope.data.substr(0, params.charCount).toUpperCase();
}
var cobj = getCharacterObject(c, params.textColor, params.fontFamily, params.fontWeight, params.fontsize);
var colorIndex = '';
var color = '';
/**
* Populate the colors according to attributes
*/
if (c.charCodeAt(0) < 65) {
color = getRandomColors();
} else {
colorIndex = Math.floor((c.charCodeAt(0) - 65) % params.alphabetcolors.length);
color = params.alphabetcolors[colorIndex];
}
if (params.avatarCustomBGColor) {
color = params.avatarCustomBGColor;
}
var svg = getImgTag(params.width, params.height, color);
svg.append(cobj);
var lvcomponent = angular.element('<div>').append(svg.clone()).html();
var svgHtml = window.btoa(unescape(encodeURIComponent(lvcomponent)));
var component;
var base = defaultSettings.base;
var _style = '';
if (params.avatarBorderStyle) {
_style = params.avatarBorderStyle;
} else if (params.avatardefaultBorder) {
_style = params.defaultBorder;
}
if (params.rotatedeg != '0') {
_style = '-ms-transform: rotate(' + params.rotatedeg + 'deg); -webkit-transform: rotate(' + params.rotatedeg + 'deg); transform: rotate(' + params.rotatedeg + 'deg)';
}
if (params.shape) {
if (params.shape === 'round') {
var round_style = defaultSettings.radius + _style;
if (scope.data.indexOf('http') > -1 || scope.data.indexOf('data:image') > -1) {
var img_size = 'width:' + params.width + 'px;height:' + params.height + 'px;';
component = "<img src=" + scope.data + " style='" + img_size + round_style + "' />";
} else {
component = "<img src=" + base + svgHtml + " style='" + round_style + "' title='" + scope.data + "' />";
}
}
} else {
if (scope.data.indexOf('http') > -1 || scope.data.indexOf('data:image') > -1) {
var img_size = 'width:' + params.width + 'px;height:' + params.height + 'px;';
component = "<img src=" + scope.data + " style='" + img_size + _style + "' />";
} else {
component = "<img src=" + base + svgHtml + " style='" + _style + "' title='" + scope.data + "' />";
}
}
if (params.dynamic === 'true') {
element.empty();
element.append(component);
} else {
element.replaceWith(component);
}
}
}
};
}]);
/**
* Get the random colors
* @returns {String}
*/
function getRandomColors() {
var letters = '0123456789ABCDEF'.split('');
var _color = '#';
for (var i = 0; i < 6; i++) {
_color += letters[Math.floor(Math.random() * 16)];
}
return _color;
}
/**
* get the first name and last name first letters and combined and form the letter avatar
* @param {type} data
* @returns {unresolved}
*/
function getFirstAndLastName(data) {
var names = data.split(" ");
if (names && names.length >= 2) {
var firstName = names[0];
var lastName = names[1];
if (firstName && lastName) {
var text = firstName.substr(0, 1) + lastName.substr(0, 1);
return text;
} else {
return data.substr(0, 2);
}
}
}
/**
* Populate the svg tag which will used for the avatar generation
* @param {type} width
* @param {type} height
* @param {type} color
* @returns {unresolved}
*/
function getImgTag(width, height, color) {
var svgTag = angular.element('<svg></svg>')
.attr({
'xmlns': 'http://www.w3.org/2000/svg',
'pointer-events': 'none',
'width': width,
'height': height
})
.css({
'background-color': color,
'width': width + 'px',
'height': height + 'px'
});
return svgTag;
}
/**
* Generate the Letter tag by using the svg text element
* @param {type} character
* @param {type} textColor
* @param {type} fontFamily
* @param {type} fontWeight
* @param {type} fontsize
* @returns {unresolved}
*/
function getCharacterObject(character, textColor, fontFamily, fontWeight, fontsize) {
var textTag = angular.element('<text text-anchor="middle"></text>')
.attr({
'y': '50%',
'x': '50%',
'dy': '0.35em',
//'stroke': '#000000',
'pointer-events': 'auto',
'fill': textColor,
'font-family': fontFamily
})
.html(character)
.css({
'font-weight': fontWeight,
'font-size': fontsize + 'px',
});
return textTag;
}