forked from bengott/meteor-avatar
-
Notifications
You must be signed in to change notification settings - Fork 0
/
export.js
117 lines (102 loc) · 3.91 KB
/
export.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
// Avatar object to be exported
Avatar = {
// If defined (e.g. from a startup config file in your app), these options
// override default functionality
options: {
// Determines the type of fallback to use when no image can be found via
// linked services (Gravatar included):
// "default image" (the default option, which will show either the image
// specified by defaultImageUrl, the package's default image, or a Gravatar
// default image)
// OR
// "initials" (show the user's initials).
fallbackType: '',
// This will replace the included default avatar image's URL
// ('packages/bengott_avatar/default.png'). It can be a relative path
// (relative to website's base URL, e.g. 'images/defaultAvatar.png').
defaultImageUrl: '',
// Gravatar default option to use (overrides default image URL)
// Options are available at:
// https://secure.gravatar.com/site/implement/images/#default-image
gravatarDefault: '',
// This property on the user object will be used for retrieving gravatars
// (useful when user emails are not published).
emailHashProperty: ''
},
// Get the initials of the user
getInitials: function (user) {
var initials = '';
var name = '';
var parts = [];
if (user && user.profile && user.profile.firstName) {
initials = user.profile.firstName.charAt(0).toUpperCase();
if (user.profile.lastName) {
initials += user.profile.lastName.charAt(0).toUpperCase();
}
else if (user.profile.familyName) {
initials += user.profile.familyName.charAt(0).toUpperCase();
}
else if (user.profile.secondName) {
initials += user.profile.secondName.charAt(0).toUpperCase();
}
}
else {
if (user && user.profile && user.profile.name) {
name = user.profile.name;
}
else if (user && user.username) {
name = user.username;
}
parts = name.split(' ');
// Limit getInitials to first and last initial to avoid problems with
// very long multi-part names (e.g. "Jose Manuel Garcia Galvez")
initials = _.first(parts).charAt(0).toUpperCase();
if (parts.length > 1) {
initials += _.last(parts).charAt(0).toUpperCase();
}
}
return initials;
},
// Get the url of the user's avatar
getUrl: function (user) {
var url = '';
var defaultUrl, svc;
if (user) {
svc = getService(user);
if (svc === 'twitter') {
// use larger image (200x200 is smallest custom option)
if (Meteor.isClient) {
url = window.location.protocol === "https:" ? user.services.twitter.profile_image_url_https : user.services.twitter.profile_image_url;
} else {
url = user.services.twitter.profile_image_url_https;
}
url = url.replace('_normal.', '_200x200.');
}
else if (svc === 'facebook') {
// use larger image (~200x200)
url = 'http://graph.facebook.com/' + user.services.facebook.id + '/picture?type=large';
}
else if (svc === 'google') {
url = user.services.google.picture;
}
else if (svc === 'github') {
url = 'http://avatars.githubusercontent.com/' + user.services.github.username + '?s=200';
}
else if (svc === 'instagram') {
url = user.services.instagram.profile_picture;
}
else if (svc === 'none') {
defaultUrl = Avatar.options.defaultImageUrl || 'packages/bengott_avatar/default.png';
// If it's a relative path (no '//' anywhere), complete the URL
if (defaultUrl.indexOf('//') === -1) {
// Strip starting slash if it exists
if (defaultUrl.charAt(0) === '/') defaultUrl = defaultUrl.slice(1);
// Then add the relative path to the server's base URL
defaultUrl = Meteor.absoluteUrl() + defaultUrl;
}
url = getGravatarUrl(user, defaultUrl);
}
}
return url;
}
};