2016-03-14 23:58:51 +00:00
|
|
|
app.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-16 19:15:14 +00:00
|
|
|
ctrl.selectedContactId = undefined;
|
2016-03-17 01:09:33 +00:00
|
|
|
ctrl.searchTerm = '';
|
2016-03-07 23:40:11 +00:00
|
|
|
|
2016-03-17 12:42:06 +00:00
|
|
|
ctrl.t = {
|
|
|
|
addContact : t('contacts', 'Add contact'),
|
|
|
|
emptySearch : t('contacts', 'No search result for {query}', {query: ctrl.searchTerm})
|
|
|
|
};
|
|
|
|
|
|
|
|
|
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;
|
|
|
|
$route.updateParams({
|
|
|
|
uid: uid
|
|
|
|
});
|
|
|
|
ctrl.selectedContactId = uid;
|
|
|
|
$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;
|
|
|
|
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
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2015-12-16 22:43:33 +00:00
|
|
|
ContactService.getAll().then(function(contacts) {
|
2016-03-09 10:13:41 +00:00
|
|
|
$scope.$apply(function() {
|
2015-12-16 22:43:33 +00:00
|
|
|
ctrl.contacts = contacts;
|
2016-03-04 12:32:51 +00:00
|
|
|
ctrl.loading = false;
|
2015-12-16 22:43:33 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2016-02-29 00:47:22 +00:00
|
|
|
$scope.$watch('ctrl.routeParams.uid', function(newValue) {
|
|
|
|
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-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 = [];
|
|
|
|
// 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()
|
|
|
|
});
|
|
|
|
}
|
2016-02-29 12:00:28 +00:00
|
|
|
unbindWatch(); // unbind as we only want one update
|
2016-02-29 09:10:39 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2015-12-16 22:43:33 +00:00
|
|
|
ctrl.createContact = function() {
|
2016-03-02 14:47:55 +00:00
|
|
|
ContactService.create().then(function(contact) {
|
2016-03-04 10:15:06 +00:00
|
|
|
['tel', 'adr', 'email'].forEach(function(field) {
|
|
|
|
var defaultValue = vCardPropertiesService.getMeta(field).defaultValue || {value: ''};
|
|
|
|
contact.addProperty(field, defaultValue);
|
|
|
|
} );
|
2016-03-08 10:32:11 +00:00
|
|
|
if ($routeParams.gid !== t('contacts', 'All contacts')) {
|
2016-03-02 14:47:55 +00:00
|
|
|
contact.categories($routeParams.gid);
|
2016-03-08 10:32:11 +00:00
|
|
|
} else {
|
|
|
|
contact.categories('');
|
|
|
|
}
|
|
|
|
$('#details-fullName').focus();
|
2016-03-02 14:47:55 +00:00
|
|
|
});
|
2016-01-14 10:40:45 +00:00
|
|
|
};
|
2016-02-18 16:46:06 +00:00
|
|
|
|
2016-02-18 21:07:10 +00:00
|
|
|
ctrl.hasContacts = function () {
|
|
|
|
if (!ctrl.contacts) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return ctrl.contacts.length > 0;
|
|
|
|
};
|
|
|
|
|
2016-02-18 16:58:00 +00:00
|
|
|
$scope.selectedContactId = $routeParams.uid;
|
2016-02-18 16:46:06 +00:00
|
|
|
$scope.setSelected = function (selectedContactId) {
|
|
|
|
$scope.selectedContactId = selectedContactId;
|
|
|
|
};
|
|
|
|
|
2016-03-16 19:37:40 +00:00
|
|
|
});
|