2016-03-19 11:02:13 +00:00
|
|
|
angular.module('contactsApp')
|
2017-08-15 09:42:12 +00:00
|
|
|
.controller('contactlistCtrl', function($scope, $filter, $route, $routeParams, $timeout, AddressBookService, ContactService, SortByService, vCardPropertiesService, SearchService) {
|
2015-10-27 17:52:09 +00:00
|
|
|
var ctrl = this;
|
2015-12-08 11:31:43 +00:00
|
|
|
|
2016-02-18 14:19:57 +00:00
|
|
|
ctrl.routeParams = $routeParams;
|
2016-02-17 21:34:36 +00:00
|
|
|
|
2017-09-15 14:58:21 +00:00
|
|
|
ctrl.filteredContacts = []; // the displayed contacts list
|
2016-03-17 01:09:33 +00:00
|
|
|
ctrl.searchTerm = '';
|
2016-02-18 21:10:56 +00:00
|
|
|
ctrl.show = true;
|
2016-04-07 06:01:22 +00:00
|
|
|
ctrl.invalid = false;
|
2017-01-02 18:19:18 +00:00
|
|
|
ctrl.limitTo = 25;
|
2016-03-07 23:40:11 +00:00
|
|
|
|
2017-02-01 12:04:55 +00:00
|
|
|
ctrl.sortBy = SortByService.getSortBy();
|
|
|
|
|
2016-03-17 12:42:06 +00:00
|
|
|
ctrl.t = {
|
|
|
|
emptySearch : t('contacts', 'No search result for {query}', {query: ctrl.searchTerm})
|
|
|
|
};
|
|
|
|
|
2017-01-02 18:19:18 +00:00
|
|
|
ctrl.resetLimitTo = function () {
|
|
|
|
ctrl.limitTo = 25;
|
|
|
|
clearInterval(ctrl.intervalId);
|
|
|
|
ctrl.intervalId = setInterval(
|
|
|
|
function () {
|
2017-09-13 17:55:22 +00:00
|
|
|
if (!ctrl.loading && ctrl.contactList && ctrl.contactList.length > ctrl.limitTo) {
|
2017-01-02 18:19:18 +00:00
|
|
|
ctrl.limitTo += 25;
|
|
|
|
$scope.$apply();
|
|
|
|
}
|
|
|
|
}, 300);
|
|
|
|
};
|
|
|
|
|
2016-03-07 23:40:11 +00:00
|
|
|
$scope.query = function(contact) {
|
2016-03-14 23:58:51 +00:00
|
|
|
return contact.matches(SearchService.getSearchTerm());
|
2016-03-07 23:40:11 +00:00
|
|
|
};
|
|
|
|
|
2017-02-01 12:04:55 +00:00
|
|
|
SortByService.subscribe(function(newValue) {
|
|
|
|
ctrl.sortBy = newValue;
|
|
|
|
});
|
|
|
|
|
2016-03-14 23:58:51 +00:00
|
|
|
SearchService.registerObserverCallback(function(ev) {
|
2016-03-16 19:15:14 +00:00
|
|
|
if (ev.event === 'submitSearch') {
|
2017-09-15 14:58:21 +00:00
|
|
|
var uid = !_.isEmpty(ctrl.filteredContacts) ? ctrl.filteredContacts[0].uid() : undefined;
|
2016-03-17 17:07:39 +00:00
|
|
|
ctrl.setSelectedId(uid);
|
2016-03-16 19:15:14 +00:00
|
|
|
$scope.$apply();
|
|
|
|
}
|
|
|
|
if (ev.event === 'changeSearch') {
|
2017-01-02 18:19:18 +00:00
|
|
|
ctrl.resetLimitTo();
|
2016-03-17 01:09:33 +00:00
|
|
|
ctrl.searchTerm = ev.searchTerm;
|
2016-03-17 12:42:06 +00:00
|
|
|
ctrl.t.emptySearch = t('contacts',
|
|
|
|
'No search result for {query}',
|
|
|
|
{query: ctrl.searchTerm}
|
|
|
|
);
|
2016-03-16 19:15:14 +00:00
|
|
|
$scope.$apply();
|
|
|
|
}
|
2016-03-07 23:40:11 +00:00
|
|
|
});
|
2016-02-29 00:47:22 +00:00
|
|
|
|
2016-03-04 12:32:51 +00:00
|
|
|
ctrl.loading = true;
|
|
|
|
|
2016-02-29 10:52:09 +00:00
|
|
|
ContactService.registerObserverCallback(function(ev) {
|
2017-08-21 13:58:21 +00:00
|
|
|
$timeout(function() {
|
|
|
|
$scope.$apply(function() {
|
|
|
|
if (ev.event === 'delete') {
|
2017-08-21 14:00:24 +00:00
|
|
|
ctrl.selectNearestContact(ev.uid);
|
2017-08-21 13:58:21 +00:00
|
|
|
}
|
|
|
|
else if (ev.event === 'create') {
|
|
|
|
$route.updateParams({
|
|
|
|
gid: $routeParams.gid,
|
|
|
|
uid: ev.uid
|
|
|
|
});
|
|
|
|
}
|
|
|
|
else if (ev.event === 'importend') {
|
|
|
|
$route.updateParams({
|
|
|
|
gid: t('contacts', 'All contacts')
|
|
|
|
});
|
|
|
|
}
|
|
|
|
if(ev.contacts.length !== 0) {
|
2017-09-13 17:55:22 +00:00
|
|
|
ctrl.contactList = ev.contacts;
|
2017-08-21 13:58:21 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
2016-02-11 14:30:52 +00:00
|
|
|
});
|
|
|
|
|
2017-08-15 09:42:12 +00:00
|
|
|
AddressBookService.registerObserverCallback(function(ev) {
|
2017-08-21 13:58:21 +00:00
|
|
|
$timeout(function() {
|
|
|
|
$scope.$apply(function() {
|
|
|
|
if (ev.event === 'delete') {
|
|
|
|
// Get contacts
|
|
|
|
ctrl.loading = true;
|
|
|
|
ContactService.updateDeletedAddressbook(function() {
|
|
|
|
ContactService.getAll().then(function(contacts) {
|
2017-09-13 17:55:22 +00:00
|
|
|
ctrl.contactList = contacts;
|
2017-08-21 13:58:21 +00:00
|
|
|
ctrl.loading = false;
|
2017-08-21 14:00:24 +00:00
|
|
|
ctrl.selectNearestContact(ctrl.getSelectedId());
|
2017-08-21 13:58:21 +00:00
|
|
|
});
|
2017-08-20 15:46:26 +00:00
|
|
|
});
|
2017-08-21 13:58:21 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
2017-08-15 09:42:12 +00:00
|
|
|
});
|
|
|
|
|
2016-03-31 03:52:35 +00:00
|
|
|
// Get contacts
|
2015-12-16 22:43:33 +00:00
|
|
|
ContactService.getAll().then(function(contacts) {
|
2016-04-07 06:27:52 +00:00
|
|
|
if(contacts.length>0) {
|
|
|
|
$scope.$apply(function() {
|
2017-09-13 17:55:22 +00:00
|
|
|
ctrl.contactList = contacts;
|
2016-04-07 06:27:52 +00:00
|
|
|
});
|
|
|
|
} else {
|
|
|
|
ctrl.loading = false;
|
|
|
|
}
|
2016-03-31 03:52:35 +00:00
|
|
|
});
|
|
|
|
|
2017-08-25 02:49:42 +00:00
|
|
|
var getVisibleContacts = function() {
|
|
|
|
var scrolled = $('.app-content-list').scrollTop();
|
|
|
|
var elHeight = $('.contacts-list').children().outerHeight(true);
|
|
|
|
var listHeight = $('.app-content-list').height();
|
2017-01-04 18:13:49 +00:00
|
|
|
|
2017-08-25 02:58:07 +00:00
|
|
|
var topContact = Math.round(scrolled/elHeight);
|
|
|
|
var contactsCount = Math.round(listHeight/elHeight);
|
2017-01-04 18:13:49 +00:00
|
|
|
|
2017-09-15 14:58:21 +00:00
|
|
|
return ctrl.filteredContacts.slice(topContact-1, topContact+contactsCount+1);
|
2017-01-04 18:13:49 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
var timeoutId = null;
|
|
|
|
document.querySelector('.app-content-list').addEventListener('scroll', function () {
|
|
|
|
clearTimeout(timeoutId);
|
|
|
|
timeoutId = setTimeout(function () {
|
2017-08-25 02:49:42 +00:00
|
|
|
var contacts = getVisibleContacts();
|
|
|
|
ContactService.getFullContacts(contacts);
|
2017-01-04 18:13:49 +00:00
|
|
|
}, 250);
|
|
|
|
});
|
|
|
|
|
2017-09-15 14:58:21 +00:00
|
|
|
// Wait for ctrl.filteredContacts to be updated, load the contact requested in the URL if any, and
|
2017-01-04 18:13:49 +00:00
|
|
|
// load full details for the probably initially visible contacts.
|
|
|
|
// Then kill the watch.
|
2017-09-15 14:58:21 +00:00
|
|
|
var unbindListWatch = $scope.$watch('ctrl.filteredContacts', function() {
|
|
|
|
if(ctrl.filteredContacts && ctrl.filteredContacts.length > 0) {
|
2016-03-31 03:52:35 +00:00
|
|
|
// Check if a specific uid is requested
|
|
|
|
if($routeParams.uid && $routeParams.gid) {
|
2017-09-15 14:58:21 +00:00
|
|
|
ctrl.filteredContacts.forEach(function(contact) {
|
2016-03-31 03:52:35 +00:00
|
|
|
if(contact.uid() === $routeParams.uid) {
|
|
|
|
ctrl.setSelectedId($routeParams.uid);
|
|
|
|
ctrl.loading = false;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2017-01-08 17:55:56 +00:00
|
|
|
// No contact previously loaded, let's load the first of the list if not in mobile mode
|
|
|
|
if(ctrl.loading && $(window).width() > 768) {
|
2017-09-15 14:58:21 +00:00
|
|
|
ctrl.setSelectedId(ctrl.filteredContacts[0].uid());
|
2017-01-08 17:55:56 +00:00
|
|
|
}
|
2017-08-24 18:43:57 +00:00
|
|
|
// Get full data for the first 20 contacts of the list
|
2017-09-15 14:58:21 +00:00
|
|
|
ContactService.getFullContacts(ctrl.filteredContacts.slice(0, 20));
|
2016-03-04 12:32:51 +00:00
|
|
|
ctrl.loading = false;
|
2016-03-31 03:52:35 +00:00
|
|
|
unbindListWatch();
|
|
|
|
}
|
2015-12-16 22:43:33 +00:00
|
|
|
});
|
|
|
|
|
2016-02-18 21:10:56 +00:00
|
|
|
$scope.$watch('ctrl.routeParams.uid', function(newValue, oldValue) {
|
|
|
|
// Used for mobile view to clear the url
|
2016-03-31 03:52:35 +00:00
|
|
|
if(typeof oldValue != 'undefined' && typeof newValue == 'undefined' && $(window).width() <= 768) {
|
2016-02-18 21:10:56 +00:00
|
|
|
// no contact selected
|
|
|
|
ctrl.show = true;
|
|
|
|
return;
|
|
|
|
}
|
2016-02-29 00:47:22 +00:00
|
|
|
if(newValue === undefined) {
|
|
|
|
// we might have to wait until ng-repeat filled the contactList
|
2017-09-15 14:58:21 +00:00
|
|
|
if(ctrl.filteredContacts && ctrl.filteredContacts.length > 0) {
|
2016-02-29 00:47:22 +00:00
|
|
|
$route.updateParams({
|
|
|
|
gid: $routeParams.gid,
|
2017-09-15 14:58:21 +00:00
|
|
|
uid: ctrl.filteredContacts[0].uid()
|
2016-02-29 00:47:22 +00:00
|
|
|
});
|
|
|
|
} else {
|
|
|
|
// watch for next contactList update
|
2017-09-15 14:58:21 +00:00
|
|
|
var unbindWatch = $scope.$watch('ctrl.filteredContacts', function() {
|
|
|
|
if(ctrl.filteredContacts && ctrl.filteredContacts.length > 0) {
|
2016-02-29 09:10:39 +00:00
|
|
|
$route.updateParams({
|
|
|
|
gid: $routeParams.gid,
|
2017-09-15 14:58:21 +00:00
|
|
|
uid: ctrl.filteredContacts[0].uid()
|
2016-02-29 09:10:39 +00:00
|
|
|
});
|
|
|
|
}
|
2016-02-29 12:00:28 +00:00
|
|
|
unbindWatch(); // unbind as we only want one update
|
2016-02-29 00:47:22 +00:00
|
|
|
});
|
|
|
|
}
|
2016-02-18 21:10:56 +00:00
|
|
|
} else {
|
|
|
|
// displaying contact details
|
|
|
|
ctrl.show = false;
|
2016-02-29 00:47:22 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2016-02-29 09:10:39 +00:00
|
|
|
$scope.$watch('ctrl.routeParams.gid', function() {
|
|
|
|
// we might have to wait until ng-repeat filled the contactList
|
2017-09-15 14:58:21 +00:00
|
|
|
ctrl.filteredContacts = [];
|
2017-01-02 18:19:18 +00:00
|
|
|
ctrl.resetLimitTo();
|
2016-03-31 03:52:35 +00:00
|
|
|
// not in mobile mode
|
|
|
|
if($(window).width() > 768) {
|
|
|
|
// watch for next contactList update
|
2017-09-15 14:58:21 +00:00
|
|
|
var unbindWatch = $scope.$watch('ctrl.filteredContacts', function() {
|
|
|
|
if(ctrl.filteredContacts && ctrl.filteredContacts.length > 0) {
|
2016-03-31 03:52:35 +00:00
|
|
|
$route.updateParams({
|
|
|
|
gid: $routeParams.gid,
|
2017-09-15 14:58:21 +00:00
|
|
|
uid: $routeParams.uid || ctrl.filteredContacts[0].uid()
|
2016-03-31 03:52:35 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
unbindWatch(); // unbind as we only want one update
|
|
|
|
});
|
|
|
|
}
|
2016-02-29 09:10:39 +00:00
|
|
|
});
|
|
|
|
|
2016-04-07 06:01:22 +00:00
|
|
|
// Watch if we have an invalid contact
|
2017-09-15 14:58:21 +00:00
|
|
|
$scope.$watch('ctrl.filteredContacts[0].displayName()', function(displayName) {
|
2016-04-15 09:50:52 +00:00
|
|
|
ctrl.invalid = (displayName === '');
|
2016-04-07 06:01:22 +00:00
|
|
|
});
|
|
|
|
|
2016-02-18 21:07:10 +00:00
|
|
|
ctrl.hasContacts = function () {
|
2017-09-13 17:55:22 +00:00
|
|
|
if (!ctrl.contactList) {
|
2016-02-18 21:07:10 +00:00
|
|
|
return false;
|
|
|
|
}
|
2017-09-13 17:55:22 +00:00
|
|
|
return ctrl.contactList.length > 0;
|
2016-02-18 21:07:10 +00:00
|
|
|
};
|
|
|
|
|
2016-03-17 17:07:39 +00:00
|
|
|
ctrl.setSelectedId = function (contactId) {
|
|
|
|
$route.updateParams({
|
|
|
|
uid: contactId
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
ctrl.getSelectedId = function() {
|
|
|
|
return $routeParams.uid;
|
2016-02-18 16:46:06 +00:00
|
|
|
};
|
|
|
|
|
2017-08-21 14:00:24 +00:00
|
|
|
ctrl.selectNearestContact = function(contactId) {
|
2017-09-15 14:58:21 +00:00
|
|
|
if (ctrl.filteredContacts.length === 1) {
|
2017-08-20 15:46:26 +00:00
|
|
|
$route.updateParams({
|
|
|
|
gid: $routeParams.gid,
|
|
|
|
uid: undefined
|
|
|
|
});
|
|
|
|
} else {
|
2017-09-15 14:58:21 +00:00
|
|
|
for (var i = 0, length = ctrl.filteredContacts.length; i < length; i++) {
|
2017-08-20 15:46:26 +00:00
|
|
|
// Get nearest contact
|
2017-09-15 14:58:21 +00:00
|
|
|
if (ctrl.filteredContacts[i].uid() === contactId) {
|
2017-08-20 15:46:26 +00:00
|
|
|
$route.updateParams({
|
|
|
|
gid: $routeParams.gid,
|
2017-09-15 14:58:21 +00:00
|
|
|
uid: (ctrl.filteredContacts[i+1]) ? ctrl.filteredContacts[i+1].uid() : ctrl.filteredContacts[i-1].uid()
|
2017-08-20 15:46:26 +00:00
|
|
|
});
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2016-03-16 19:37:40 +00:00
|
|
|
});
|