2016-03-19 11:02:13 +00:00
|
|
|
angular.module('contactsApp')
|
|
|
|
.controller('contactlistCtrl', function($scope, $filter, $route, $routeParams, ContactService, 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
|
|
|
|
2016-02-29 00:47:22 +00:00
|
|
|
ctrl.contactList = [];
|
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;
|
2016-03-07 23:40:11 +00:00
|
|
|
|
2016-03-17 12:42:06 +00:00
|
|
|
ctrl.t = {
|
|
|
|
emptySearch : t('contacts', 'No search result for {query}', {query: ctrl.searchTerm})
|
|
|
|
};
|
|
|
|
|
2016-07-16 12:54:26 +00:00
|
|
|
$scope.getCountString = function(contacts) {
|
|
|
|
return n('contacts', '%n contact', '%n contacts', contacts.length);
|
|
|
|
};
|
2016-03-17 12:42:06 +00:00
|
|
|
|
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
|
|
|
};
|
|
|
|
|
2016-03-14 23:58:51 +00:00
|
|
|
SearchService.registerObserverCallback(function(ev) {
|
2016-03-16 19:15:14 +00:00
|
|
|
if (ev.event === 'submitSearch') {
|
|
|
|
var uid = !_.isEmpty(ctrl.contactList) ? ctrl.contactList[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') {
|
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) {
|
2016-02-11 14:30:52 +00:00
|
|
|
$scope.$apply(function() {
|
2016-02-29 10:52:09 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-03-02 14:47:55 +00:00
|
|
|
else if (ev.event === 'create') {
|
|
|
|
$route.updateParams({
|
|
|
|
gid: $routeParams.gid,
|
|
|
|
uid: ev.uid
|
|
|
|
});
|
|
|
|
}
|
2016-02-29 10:52:09 +00:00
|
|
|
ctrl.contacts = ev.contacts;
|
2016-02-11 14:30:52 +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() {
|
|
|
|
ctrl.contacts = contacts;
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
ctrl.loading = false;
|
|
|
|
}
|
2016-03-31 03:52:35 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
// Wait for ctrl.contactList to be updated, load the first contact and 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());
|
2016-03-18 18:42:42 +00:00
|
|
|
}
|
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
|
2016-02-29 09:10:39 +00:00
|
|
|
if(ctrl.contactList && ctrl.contactList.length > 0) {
|
2016-02-29 00:47:22 +00:00
|
|
|
$route.updateParams({
|
|
|
|
gid: $routeParams.gid,
|
|
|
|
uid: ctrl.contactList[0].uid()
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
// watch for next contactList update
|
2016-02-29 09:10:39 +00:00
|
|
|
var unbindWatch = $scope.$watch('ctrl.contactList', function() {
|
|
|
|
if(ctrl.contactList && ctrl.contactList.length > 0) {
|
|
|
|
$route.updateParams({
|
|
|
|
gid: $routeParams.gid,
|
|
|
|
uid: ctrl.contactList[0].uid()
|
|
|
|
});
|
|
|
|
}
|
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
|
|
|
|
ctrl.contactList = [];
|
2016-03-31 03:52:35 +00:00
|
|
|
// not in mobile mode
|
|
|
|
if($(window).width() > 768) {
|
|
|
|
// 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
|
|
|
|
});
|
|
|
|
}
|
2016-02-29 09:10:39 +00:00
|
|
|
});
|
|
|
|
|
2016-04-07 06:01:22 +00:00
|
|
|
// Watch if we have an invalid contact
|
2016-04-15 09:50:52 +00:00
|
|
|
$scope.$watch('ctrl.contactList[0].displayName()', function(displayName) {
|
|
|
|
ctrl.invalid = (displayName === '');
|
2016-04-07 06:01:22 +00:00
|
|
|
});
|
|
|
|
|
2016-02-18 21:07:10 +00:00
|
|
|
ctrl.hasContacts = function () {
|
|
|
|
if (!ctrl.contacts) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return ctrl.contacts.length > 0;
|
|
|
|
};
|
|
|
|
|
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
|
|
|
};
|
|
|
|
|
2016-03-16 19:37:40 +00:00
|
|
|
});
|