|
1
|
|
|
angular.module('chat').directive('avatar', ['contacts', function(contacts) { |
|
2
|
|
|
return { |
|
3
|
|
|
restrict: 'A', |
|
4
|
|
|
link: function ($scope, element, attrs) { |
|
5
|
|
|
applyContactAvatar(element, attrs.addressbookBackend, attrs.addressbookId, attrs.id, attrs.displayname, attrs.size); |
|
6
|
|
|
if(attrs.online !== undefined){ |
|
7
|
|
|
element.online(attrs.isonline); |
|
8
|
|
|
$scope.$watch('contacts', function(){ |
|
9
|
|
|
element.online(contacts.contacts[attrs.id].online); |
|
10
|
|
|
}, true); |
|
11
|
|
|
} |
|
12
|
|
|
} |
|
13
|
|
|
}; |
|
14
|
|
|
|
|
15
|
|
|
function applyContactAvatar(element, addressbookBackend, addressBookId, id, displayname, size){ |
|
16
|
|
|
var cacheTime = Cache.day(1); |
|
17
|
|
|
element.height(size); |
|
18
|
|
|
element.width(size); |
|
19
|
|
|
|
|
20
|
|
|
// First generate an id of this contact which is used in the cache |
|
21
|
|
|
var cacheId = id; |
|
22
|
|
|
// Next check if the cacheId occurs in the cache |
|
23
|
|
|
var value = Cache.get(cacheId) |
|
|
|
|
|
|
24
|
|
|
if (value !== undefined) { |
|
25
|
|
|
if (value.noAvatar == true) { |
|
|
|
|
|
|
26
|
|
|
element.imageplaceholder(displayname); |
|
27
|
|
|
} else { |
|
28
|
|
|
applyAvatar(value.base64 + "?requesttoken=" + oc_requesttoken, size); |
|
29
|
|
|
} |
|
30
|
|
|
} else { |
|
31
|
|
|
// check if the contact is an ownCloud user or a contact |
|
32
|
|
|
if (addressbookBackend === 'local' && addressBookId === "-1") { |
|
33
|
|
|
// This is a NOT saved contact |
|
34
|
|
|
element.imageplaceholder(displayname); |
|
35
|
|
|
} |
|
36
|
|
|
else if (addressbookBackend === 'local' && addressBookId === "") { |
|
37
|
|
|
// ownCloud user |
|
38
|
|
|
var url = OC.generateUrl( |
|
39
|
|
|
'/avatar/{user}/{size}?requesttoken={requesttoken}', |
|
40
|
|
|
{ |
|
41
|
|
|
user: id, |
|
42
|
|
|
size: size * window.devicePixelRatio, |
|
43
|
|
|
requesttoken: oc_requesttoken |
|
44
|
|
|
}); |
|
45
|
|
|
var urlWithoutRT = OC.generateUrl( |
|
46
|
|
|
'/avatar/{user}/{size}', |
|
47
|
|
|
{user: id, size: size * window.devicePixelRatio}); |
|
48
|
|
|
$.get(url, function (result) { |
|
49
|
|
|
if (typeof(result) === 'object') { |
|
50
|
|
|
Cache.set(cacheId, {"noAvatar": true}, cacheTime); |
|
51
|
|
|
element.imageplaceholder(displayname); |
|
52
|
|
|
} else { |
|
53
|
|
|
Cache.set(cacheId, { |
|
54
|
|
|
"noAvatar": false, |
|
55
|
|
|
"base64": urlWithoutRT |
|
56
|
|
|
}, cacheTime); |
|
57
|
|
|
applyAvatar(url, size); |
|
58
|
|
|
} |
|
59
|
|
|
}); |
|
60
|
|
|
} else { |
|
61
|
|
|
var url = OC.generateUrl('/apps/contacts/addressbook/{backend}/{addressbook_id}/contact/{contact_id}/photo?requesttoken={requesttoken}', |
|
|
|
|
|
|
62
|
|
|
{ |
|
63
|
|
|
backend: addressbookBackend, |
|
64
|
|
|
contact_id: id, |
|
65
|
|
|
addressbook_id: addressBookId, |
|
66
|
|
|
requesttoken: oc_requesttoken |
|
67
|
|
|
}); |
|
68
|
|
|
var urlWithoutRT = OC.generateUrl('/apps/contacts/addressbook/{backend}/{addressbook_id}/contact/{contact_id}/photo', |
|
|
|
|
|
|
69
|
|
|
{ |
|
70
|
|
|
backend: addressbookBackend, |
|
71
|
|
|
contact_id: id, |
|
72
|
|
|
addressbook_id: addressBookId |
|
73
|
|
|
}); |
|
74
|
|
|
$.get(url, function (result) { |
|
75
|
|
|
if (typeof(result) === 'object') { |
|
76
|
|
|
Cache.set(cacheId, {"noAvatar": true}, cacheTime); |
|
77
|
|
|
element.imageplaceholder(displayname); |
|
78
|
|
|
} else { |
|
79
|
|
|
Cache.set(cacheId, { |
|
80
|
|
|
"noAvatar": false, |
|
81
|
|
|
"base64": url |
|
82
|
|
|
}, cacheTime); |
|
83
|
|
|
applyAvatar(url, size); |
|
84
|
|
|
} |
|
85
|
|
|
}); |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
} |
|
89
|
|
|
} |
|
90
|
|
|
function applyAvatar(url, size){ |
|
91
|
|
|
element.show(); |
|
92
|
|
|
element.html('<img width="' + size + '" height="' + size + '" src="'+ url +'">'); |
|
93
|
|
|
} |
|
94
|
|
|
}]); |
Requirement of semicolons purely is a coding style issue since JavaScript has specific rules about semicolons which are followed by all browsers.
Further Readings: