-
Notifications
You must be signed in to change notification settings - Fork 1
/
ua-facebook-picture.js
52 lines (43 loc) · 1.38 KB
/
ua-facebook-picture.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
'use strict';
/* AngularJS directive to show the Facebook profile picture of the logged in user using UserApp. */
angular.module('UserApp.facebook-picture', []).
directive('uaFacebookPicture', function(user, UserApp) {
return function(scope, element, attrs) {
function getFacebookProfilePicture(callback) {
if (callback && user.current) {
user.avatarCache = user.avatarCache || {};
if (user.avatarCache[user.current.user_id]) {
callback(user.avatarCache[user.current.user_id]);
return;
}
UserApp.OAuth.Connection.search({
user_id: 'self',
fields: ['provider_id', 'provider_user_data']
}, function(error, result) {
if (!error) {
for (var i = 0; i < result.items.length; ++i) {
if (result.items[i].provider_id == 'facebook') {
var url = 'http://graph.facebook.com/' + result.items[i].provider_user_data.id + '/picture?type=' + (attrs.uaFacebookPicture || 'small');
if (attrs.width) {
url += '&width=' + attrs.width;
}
if (attrs.height) {
url += '&height=' + attrs.height;
}
user.avatarCache[user.current.user_id] = url;
callback(url);
return;
}
}
}
callback(null);
});
}
}
getFacebookProfilePicture(function(url) {
if (url) {
element.attr('src', url);
}
});
};
});