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
|
angular.module('contactsApp')
.controller('contactlistCtrl', function($scope, $filter, $route, $routeParams, $timeout, ContactService, SortByService, vCardPropertiesService, SearchService) {
var ctrl = this;
ctrl.routeParams = $routeParams;
ctrl.contactList = [];
ctrl.searchTerm = '';
ctrl.show = true;
ctrl.invalid = false;
ctrl.limitTo = 25;
ctrl.sortBy = SortByService.getSortBy();
ctrl.t = {
emptySearch : t('contacts', 'No search result for {query}', {query: ctrl.searchTerm})
};
ctrl.resetLimitTo = function () {
ctrl.limitTo = 25;
clearInterval(ctrl.intervalId);
ctrl.intervalId = setInterval(
function () {
if (!ctrl.loading && ctrl.contacts && ctrl.contacts.length > ctrl.limitTo) {
ctrl.limitTo += 25;
$scope.$apply();
}
}, 300);
};
$scope.query = function(contact) {
return contact.matches(SearchService.getSearchTerm());
};
SortByService.subscribe(function(newValue) {
ctrl.sortBy = newValue;
});
SearchService.registerObserverCallback(function(ev) {
if (ev.event === 'submitSearch') {
var uid = !_.isEmpty(ctrl.contactList) ? ctrl.contactList[0].uid() : undefined;
ctrl.setSelectedId(uid);
$scope.$apply();
}
if (ev.event === 'changeSearch') {
ctrl.resetLimitTo();
ctrl.searchTerm = ev.searchTerm;
ctrl.t.emptySearch = t('contacts',
'No search result for {query}',
{query: ctrl.searchTerm}
);
$scope.$apply();
}
});
ctrl.loading = true;
ContactService.registerObserverCallback(function(ev) {
$timeout(function () { $scope.$apply(function() {
if (ev.event === 'delete') {
if (ctrl.contactList.length === 1) {
$route.updateParams({
gid: $routeParams.gid,
uid: undefined
});
} else {
for (var i = 0, length = ctrl.contactList.length; i < length; i++) {
if (ctrl.contactList[i].uid() === ev.uid) {
$route.updateParams({
gid: $routeParams.gid,
uid: (ctrl.contactList[i+1]) ? ctrl.contactList[i+1].uid() : ctrl.contactList[i-1].uid()
});
break;
}
}
}
}
else if (ev.event === 'create') {
$route.updateParams({
gid: $routeParams.gid,
uid: ev.uid
});
}
ctrl.contacts = ev.contacts;
}); });
});
// Get contacts
ContactService.getAll().then(function(contacts) {
if(contacts.length>0) {
$scope.$apply(function() {
ctrl.contacts = contacts;
});
} else {
ctrl.loading = false;
}
});
var getVisibleNames = function getVisibleNames() {
function isScrolledIntoView(el) {
var elemTop = el.getBoundingClientRect().top;
var elemBottom = el.getBoundingClientRect().bottom;
var bothAboveViewport = elemBottom < 0;
var bothBelowViewPort = elemTop > window.innerHeight;
var isVisible = !bothAboveViewport && !bothBelowViewPort;
return isVisible;
}
var elements = Array.prototype.slice.call(document.querySelectorAll('.contact__icon:not(.ng-hide)'));
var names = elements
.filter(isScrolledIntoView)
.map(function (el) {
var siblings = Array.prototype.slice.call(el.parentElement.children);
var nameElement = siblings.find(function (sibling) {
return sibling.getAttribute('class').indexOf('content-list-item-line-one') !== -1;
});
return nameElement.innerText;
});
return names;
};
var timeoutId = null;
document.querySelector('.app-content-list').addEventListener('scroll', function () {
clearTimeout(timeoutId);
timeoutId = setTimeout(function () {
var names = getVisibleNames();
ContactService.getFullContacts(names);
}, 250);
});
// Wait for ctrl.contactList to be updated, load the contact requested in the URL if any, and
// load full details for the probably initially visible contacts.
// Then kill the watch.
var unbindListWatch = $scope.$watch('ctrl.contactList', function() {
if(ctrl.contactList && ctrl.contactList.length > 0) {
// Check if a specific uid is requested
if($routeParams.uid && $routeParams.gid) {
ctrl.contactList.forEach(function(contact) {
if(contact.uid() === $routeParams.uid) {
ctrl.setSelectedId($routeParams.uid);
ctrl.loading = false;
}
});
}
// No contact previously loaded, let's load the first of the list if not in mobile mode
if(ctrl.loading && $(window).width() > 768) {
ctrl.setSelectedId(ctrl.contactList[0].uid());
}
var firstNames = ctrl.contactList.slice(0, 20).map(function (c) { return c.displayName(); });
ContactService.getFullContacts(firstNames);
ctrl.loading = false;
unbindListWatch();
}
});
$scope.$watch('ctrl.routeParams.uid', function(newValue, oldValue) {
// Used for mobile view to clear the url
if(typeof oldValue != 'undefined' && typeof newValue == 'undefined' && $(window).width() <= 768) {
// no contact selected
ctrl.show = true;
return;
}
if(newValue === undefined) {
// we might have to wait until ng-repeat filled the contactList
if(ctrl.contactList && ctrl.contactList.length > 0) {
$route.updateParams({
gid: $routeParams.gid,
uid: ctrl.contactList[0].uid()
});
} else {
// watch for next contactList update
var unbindWatch = $scope.$watch('ctrl.contactList', function() {
if(ctrl.contactList && ctrl.contactList.length > 0) {
$route.updateParams({
gid: $routeParams.gid,
uid: ctrl.contactList[0].uid()
});
}
unbindWatch(); // unbind as we only want one update
});
}
} else
|