contacts/js/components/contactList/contactList_controller.js

113 lines
3.0 KiB
JavaScript
Raw Normal View History

2016-03-16 19:37:40 +00:00
app.controller('contactlistCtrl', function($scope, $filter, $route, $routeParams, ContactService, vCardPropertiesService) {
2015-10-27 17:52:09 +00:00
var ctrl = this;
2015-12-08 11:31:43 +00:00
ctrl.routeParams = $routeParams;
2016-02-24 19:54:35 +00:00
ctrl.t = {
2016-02-29 12:43:05 +00:00
addContact : t('contacts', 'Add contact')
2016-02-24 19:54:35 +00:00
};
2016-02-17 21:34:36 +00:00
ctrl.contactList = [];
ContactService.registerObserverCallback(function(ev) {
2016-02-11 14:30:52 +00:00
$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;
2016-02-11 14:30:52 +00:00
});
});
ContactService.getAll().then(function(contacts) {
2016-03-09 10:13:41 +00:00
$scope.$apply(function() {
ctrl.contacts = contacts;
});
});
$scope.$watch('ctrl.routeParams.uid', function(newValue) {
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()
});
}
2016-02-29 12:00:28 +00:00
unbindWatch(); // unbind as we only want one update
});
}
}
});
$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
});
});
ctrl.createContact = function() {
ContactService.create().then(function(contact) {
['tel', 'adr', 'email'].forEach(function(field) {
var defaultValue = vCardPropertiesService.getMeta(field).defaultValue || {value: ''};
contact.addProperty(field, defaultValue);
} );
if ($routeParams.gid !== t('contacts', 'All contacts')) {
contact.categories($routeParams.gid);
} else {
contact.categories('');
}
$('#details-fullName').focus();
});
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
});