forked from khizarnaeem/userAvatar
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathenhancedavatar.js
74 lines (73 loc) · 2.57 KB
/
enhancedavatar.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
var appModule = angular.module('enhancedavatar', []);
appModule
.factory("avatarService", [function(){
var avatarService = function(name){
var colorCodes = {
1: "#F29691",
2: "#92D6C2",
3: "#CFD696",
4: "#FACA82",
5: "#D7ADE0"
};
var i1 = "", i2 = "", nameArray = [];
if (angular.isDefined(name.charAt(0))) {
i1 = angular.uppercase(name.charAt(0));
nameArray = name.split(" ");
if (nameArray.length >= 2) {
i2 = angular.uppercase(nameArray[nameArray.length - 1].charAt(0));
}
} else {
i1 = "U";
i2="";
}
var initials = i1+i2;
console.log(i1,i2);
var charCode = initials.charCodeAt(0) + initials.charCodeAt(1);
charCode = charCode >= 130 && charCode <= 144 ? 1 : charCode >= 145 && charCode <= 158 ? 2 : charCode > 158 && charCode <= 172 ? 3 : charCode >= 173 && charCode <= 186 ? 4 : 5;
var background = colorCodes[charCode];
return ({ "Initials": initials, "Background": background });
}
return {
getAvatar: avatarService
}
}]).directive('enhancedavatar', ["avatarService","$timeout", function (avatarService,$timeout) {
return {
restrict: 'EAC',
scope: {
name: '@',
img:'@',
width:'@',
height:'@'
},
template: '<div class="generic-avatar" style="width:{{width}};height:{{height}};">'+
'<a class="color" data-ng-if="!ImageAvailable" style="height:{{height}};border-radius:{{width}};background-color:{{GenericAvatar.Background}}"></a>'+
'<a class="name" style="min-width:{{width}};top:{{getOneFifth(height)}};font-size:{{getHalf(height)}}">{{GenericAvatar.Initials}}</a>' +
'<a class="img" data-ng-if="ImageAvailable" style="width:{{width}};height:{{height}};border-radius:{{width}};background-image:url({{img}});"></a>' +
'</div>',
controller: 'enhancedAvatarCtrl'
};
}]).controller('enhancedAvatarCtrl',['$scope','$timeout','avatarService',function ($scope,$timeout,avatarService) {
$scope.getHalf=function(value){
if(value)
return (parseFloat(value)/2)+(value.replace(/\d+\.?\d*/,""));
};
$scope.getOneFifth=function(value){
if(value)
return (parseFloat(value)/5)+(value.replace(/\d+\.?\d*/,""));
};
$timeout(function(){
if(!$scope.width){
$scope.width='38px';
}
if(!$scope.height){
$scope.height='38px';
}
$scope.ImageAvailable = false;
if (!$scope.img) {
$scope.GenericAvatar = avatarService.getAvatar($scope.name);
} else {
console.log($scope.img)
$scope.ImageAvailable = true;
}
},10);
}]);