2014-09-17 22:28:40 +02:00
|
|
|
/* -*- Mode: javascript; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
2014-06-19 21:50:29 +02:00
|
|
|
/* JavaScript for SOGoContacts */
|
|
|
|
|
|
|
|
(function() {
|
2014-09-17 22:28:40 +02:00
|
|
|
'use strict';
|
2014-06-19 21:50:29 +02:00
|
|
|
|
2014-09-17 22:28:40 +02:00
|
|
|
angular.module('SOGo.Common', []);
|
2014-06-19 21:50:29 +02:00
|
|
|
|
2014-09-17 22:28:40 +02:00
|
|
|
angular.module('SOGo.ContactsUI', ['ngSanitize', 'ui.router', 'mm.foundation', 'SOGo.Common', 'SOGo.UIDesktop'])
|
2014-06-19 21:50:29 +02:00
|
|
|
|
2014-08-06 21:14:05 +02:00
|
|
|
.constant('sgSettings', {
|
2014-09-17 22:28:40 +02:00
|
|
|
baseURL: ApplicationBaseURL
|
2014-08-06 21:14:05 +02:00
|
|
|
})
|
2014-06-19 21:50:29 +02:00
|
|
|
|
2014-08-06 21:14:05 +02:00
|
|
|
.config(['$stateProvider', '$urlRouterProvider', function($stateProvider, $urlRouterProvider) {
|
2014-09-17 22:28:40 +02:00
|
|
|
$stateProvider
|
|
|
|
.state('addressbook', {
|
2014-09-19 02:46:52 +02:00
|
|
|
url: '/:addressbookId',
|
2014-09-17 22:28:40 +02:00
|
|
|
views: {
|
|
|
|
addressbooks: {
|
|
|
|
templateUrl: 'addressbooks.html',
|
|
|
|
controller: 'AddressBookCtrl'
|
|
|
|
}
|
|
|
|
},
|
|
|
|
resolve: {
|
|
|
|
stateAddressbooks: ['sgAddressBook', function(AddressBook) {
|
|
|
|
return AddressBook.$all(contactFolders);
|
|
|
|
}],
|
|
|
|
stateAddressbook: ['$stateParams', 'sgAddressBook', function($stateParams, AddressBook) {
|
2014-09-19 02:46:52 +02:00
|
|
|
return AddressBook.$find($stateParams.addressbookId);
|
2014-09-17 22:28:40 +02:00
|
|
|
}]
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.state('addressbook.card', {
|
2014-09-19 02:46:52 +02:00
|
|
|
url: '/:cardId',
|
2014-09-17 22:28:40 +02:00
|
|
|
views: {
|
|
|
|
card: {
|
|
|
|
templateUrl: 'card.html',
|
|
|
|
controller: 'CardCtrl'
|
|
|
|
}
|
|
|
|
},
|
|
|
|
resolve: {
|
|
|
|
stateCard: ['$stateParams', 'stateAddressbook', function($stateParams, stateAddressbook) {
|
2014-09-19 02:46:52 +02:00
|
|
|
return stateAddressbook.$getCard($stateParams.cardId);
|
2014-09-17 22:28:40 +02:00
|
|
|
}]
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.state('addressbook.new', {
|
2014-09-19 02:46:52 +02:00
|
|
|
url: '/:contactType/new',
|
2014-09-17 22:28:40 +02:00
|
|
|
views: {
|
|
|
|
card: {
|
|
|
|
templateUrl: 'cardEditor.html',
|
|
|
|
controller: 'CardCtrl'
|
|
|
|
}
|
|
|
|
},
|
|
|
|
resolve: {
|
|
|
|
stateCard: ['$stateParams', 'stateAddressbook', 'sgCard', function($stateParams, stateAddressbook, Card) {
|
2014-09-19 02:46:52 +02:00
|
|
|
var tag = 'v' + $stateParams.contactType;
|
|
|
|
stateAddressbook.card = new Card({ pid: $stateParams.addressbookId, tag: tag });
|
2014-09-17 22:28:40 +02:00
|
|
|
return stateAddressbook.card;
|
|
|
|
}]
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.state('addressbook.editor', {
|
2014-09-19 02:46:52 +02:00
|
|
|
url: '/:cardId/edit',
|
2014-09-17 22:28:40 +02:00
|
|
|
views: {
|
|
|
|
card: {
|
|
|
|
templateUrl: 'cardEditor.html',
|
|
|
|
controller: 'CardCtrl'
|
|
|
|
}
|
|
|
|
},
|
|
|
|
resolve: {
|
|
|
|
stateCard: ['$stateParams', 'stateAddressbook', function($stateParams, stateAddressbook) {
|
2014-09-19 02:46:52 +02:00
|
|
|
return stateAddressbook.$getCard($stateParams.cardId);
|
2014-09-17 22:28:40 +02:00
|
|
|
}]
|
|
|
|
}
|
|
|
|
});
|
2014-06-19 21:50:29 +02:00
|
|
|
|
2014-09-17 22:28:40 +02:00
|
|
|
// if none of the above states are matched, use this as the fallback
|
|
|
|
$urlRouterProvider.otherwise('/personal');
|
2014-08-06 21:14:05 +02:00
|
|
|
}])
|
2014-06-19 21:50:29 +02:00
|
|
|
|
2014-08-06 21:14:05 +02:00
|
|
|
.directive('sgFocusOn', function() {
|
2014-09-17 22:28:40 +02:00
|
|
|
return function(scope, elem, attr) {
|
|
|
|
scope.$on('sgFocusOn', function(e, name) {
|
|
|
|
if (name === attr.sgFocusOn) {
|
|
|
|
elem[0].focus();
|
|
|
|
elem[0].select();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
};
|
2014-08-06 21:14:05 +02:00
|
|
|
})
|
2014-06-19 21:50:29 +02:00
|
|
|
|
2014-09-17 22:28:40 +02:00
|
|
|
.factory('sgFocus', ['$rootScope', '$timeout', function($rootScope, $timeout) {
|
2014-08-06 21:14:05 +02:00
|
|
|
return function(name) {
|
2014-09-17 22:28:40 +02:00
|
|
|
$timeout(function() {
|
2014-08-06 21:14:05 +02:00
|
|
|
$rootScope.$broadcast('sgFocusOn', name);
|
2014-06-19 21:50:29 +02:00
|
|
|
});
|
2014-08-06 21:14:05 +02:00
|
|
|
}
|
|
|
|
}])
|
2014-06-19 21:50:29 +02:00
|
|
|
|
2014-09-17 22:28:40 +02:00
|
|
|
.controller('AddressBookCtrl', ['$state', '$scope', '$rootScope', '$stateParams', '$timeout', '$modal', 'sgFocus', 'sgCard', 'sgAddressBook', 'sgDialog', 'stateAddressbooks', 'stateAddressbook', function($state, $scope, $rootScope, $stateParams, $timeout, $modal, focus, Card, AddressBook, Dialog, stateAddressbooks, stateAddressbook) {
|
2014-09-19 02:46:52 +02:00
|
|
|
var currentAddressbook;
|
2014-08-20 21:49:44 +02:00
|
|
|
|
2014-09-19 02:46:52 +02:00
|
|
|
// Resolve objects
|
|
|
|
$scope.addressbooks = stateAddressbooks;
|
2014-09-17 22:28:40 +02:00
|
|
|
$rootScope.addressbook = stateAddressbook;
|
|
|
|
|
2014-09-19 02:46:52 +02:00
|
|
|
// $scope objects
|
|
|
|
$scope.search = { status: null, filter: null, lastFilter: null };
|
|
|
|
|
2014-09-17 22:28:40 +02:00
|
|
|
// Adjust search status depending on addressbook type
|
2014-09-19 02:46:52 +02:00
|
|
|
currentAddressbook = _.find($scope.addressbooks, function(o) {
|
|
|
|
return o.id == $stateParams.addressbookId;
|
2014-09-17 22:28:40 +02:00
|
|
|
});
|
2014-09-19 02:46:52 +02:00
|
|
|
$scope.search.status = (currentAddressbook && currentAddressbook.isRemote)? 'remote-addressbook' : '';
|
2014-09-17 22:28:40 +02:00
|
|
|
|
|
|
|
// $scope functions
|
|
|
|
$scope.select = function(rowIndex) {
|
|
|
|
$scope.editMode = false;
|
|
|
|
};
|
|
|
|
$scope.newAddressbook = function() {
|
|
|
|
$scope.editMode = false;
|
|
|
|
Dialog.prompt(l('New addressbook'),
|
|
|
|
l('Name of new addressbook'))
|
|
|
|
.then(function(name) {
|
|
|
|
if (name && name.length > 0) {
|
|
|
|
var addressbook = new AddressBook(
|
|
|
|
{
|
|
|
|
name: name,
|
|
|
|
isEditable: true,
|
|
|
|
isRemote: false
|
2014-08-20 21:49:44 +02:00
|
|
|
}
|
2014-09-17 22:28:40 +02:00
|
|
|
);
|
|
|
|
AddressBook.$add(addressbook);
|
2014-08-06 21:14:05 +02:00
|
|
|
}
|
2014-09-17 22:28:40 +02:00
|
|
|
});
|
|
|
|
};
|
|
|
|
$scope.edit = function(i) {
|
|
|
|
if (!$rootScope.addressbook.isRemote) {
|
|
|
|
if (angular.isUndefined(i)) {
|
2014-09-19 02:46:52 +02:00
|
|
|
i = _.indexOf(_.pluck($scope.addressbooks, 'id'), $rootScope.addressbook.id);
|
2014-09-17 22:28:40 +02:00
|
|
|
}
|
|
|
|
$scope.editMode = $rootScope.addressbook.id;
|
|
|
|
focus('addressBookName_' + i);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
$scope.save = function(i) {
|
2014-09-19 02:46:52 +02:00
|
|
|
var name = $scope.addressbooks[i].name;
|
2014-09-17 22:28:40 +02:00
|
|
|
if (name && name.length > 0) {
|
|
|
|
$scope.addressbook.$rename(name)
|
|
|
|
.then(function(data) {
|
|
|
|
$scope.editMode = false;
|
|
|
|
}, function(data, status) {
|
|
|
|
Dialog.alert(l('Warning'), data);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
};
|
|
|
|
$scope.confirmDelete = function() {
|
|
|
|
Dialog.confirm(l('Warning'), l('Are you sure you want to delete the addressbook <em>%{0}</em>?',
|
|
|
|
$rootScope.addressbook.name))
|
|
|
|
.then(function(res) {
|
|
|
|
if (res) {
|
|
|
|
$rootScope.addressbook.$delete()
|
|
|
|
.then(function() {
|
|
|
|
$rootScope.addressbook = null;
|
2014-06-19 21:50:29 +02:00
|
|
|
}, function(data, status) {
|
2014-09-17 22:28:40 +02:00
|
|
|
Dialog.alert(l('An error occured while deleting the addressbook "%{0}".',
|
|
|
|
$rootScope.addressbook.name),
|
|
|
|
l(data.error));
|
2014-09-10 22:20:52 +02:00
|
|
|
});
|
2014-08-06 21:14:05 +02:00
|
|
|
}
|
2014-09-17 22:28:40 +02:00
|
|
|
});
|
|
|
|
};
|
|
|
|
$scope.importCards = function() {
|
|
|
|
};
|
|
|
|
$scope.share = function() {
|
|
|
|
var modal = $modal.open({
|
|
|
|
templateUrl: 'addressbookSharing.html',
|
|
|
|
controller: function($scope, $modalInstance) {
|
|
|
|
$scope.closeModal = function() {
|
|
|
|
$modalInstance.close();
|
|
|
|
};
|
|
|
|
}
|
|
|
|
});
|
|
|
|
};
|
|
|
|
$scope.doSearch = function(keyEvent) {
|
2014-09-19 02:46:52 +02:00
|
|
|
if ($scope.search.filter != $scope.search.lastFilter) {
|
2014-09-17 22:28:40 +02:00
|
|
|
if ($scope.search.filter.length > 2) {
|
|
|
|
$rootScope.addressbook.$filter($scope.search.filter).then(function(data) {
|
|
|
|
if (data.length == 0)
|
|
|
|
$scope.search.status = 'no-result';
|
|
|
|
else
|
|
|
|
$scope.search.status = '';
|
|
|
|
});
|
|
|
|
}
|
|
|
|
else if ($scope.search.filter.length == 0) {
|
2014-09-19 02:46:52 +02:00
|
|
|
$rootScope.addressbook = AddressBook.$find($stateParams.addressbookId);
|
2014-09-17 22:28:40 +02:00
|
|
|
// Extend resulting model instance with parameters from addressbooks listing
|
2014-09-19 02:46:52 +02:00
|
|
|
var o = _.find($scope.addressbooks, function(o) {
|
|
|
|
return o.id == $stateParams.addressbookId;
|
2014-09-17 22:28:40 +02:00
|
|
|
});
|
|
|
|
$scope.search.status = (o.isRemote)? 'remote-addressbook' : '';
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
$scope.search.status = 'min-char';
|
|
|
|
$rootScope.addressbook.cards = [];
|
|
|
|
}
|
|
|
|
}
|
2014-09-19 02:46:52 +02:00
|
|
|
$scope.search.lastFilter = $scope.search.filter;
|
2014-09-17 22:28:40 +02:00
|
|
|
};
|
2014-08-06 21:14:05 +02:00
|
|
|
}])
|
|
|
|
|
2014-09-05 21:12:05 +02:00
|
|
|
.controller('CardCtrl', ['$scope', '$rootScope', 'sgAddressBook', 'sgCard', 'sgDialog', 'sgFocus', '$state', '$stateParams', function($scope, $rootScope, AddressBook, Card, Dialog, focus, $state, $stateParams) {
|
2014-09-19 02:46:52 +02:00
|
|
|
$scope.allEmailTypes = Card.$EMAIL_TYPES;
|
|
|
|
$scope.allTelTypes = Card.$TEL_TYPES;
|
|
|
|
$scope.allUrlTypes = Card.$URL_TYPES;
|
|
|
|
$scope.allAddressTypes = Card.$ADDRESS_TYPES;
|
2014-08-20 21:49:44 +02:00
|
|
|
|
2014-09-19 02:46:52 +02:00
|
|
|
$rootScope.masterCard = angular.copy($rootScope.addressbook.card);
|
2014-09-05 21:12:05 +02:00
|
|
|
|
2014-09-17 22:28:40 +02:00
|
|
|
$scope.addOrgUnit = function() {
|
|
|
|
var i = $rootScope.addressbook.card.$addOrgUnit('');
|
|
|
|
focus('orgUnit_' + i);
|
|
|
|
};
|
|
|
|
$scope.addCategory = function() {
|
2014-09-19 02:46:52 +02:00
|
|
|
var i = $rootScope.addressbook.card.$addCategory('');
|
2014-09-17 22:28:40 +02:00
|
|
|
focus('category_' + i);
|
|
|
|
};
|
|
|
|
$scope.addEmail = function() {
|
2014-09-19 02:46:52 +02:00
|
|
|
var i = $rootScope.addressbook.card.$addEmail('');
|
2014-09-17 22:28:40 +02:00
|
|
|
focus('email_' + i);
|
|
|
|
};
|
|
|
|
$scope.addPhone = function() {
|
2014-09-19 02:46:52 +02:00
|
|
|
var i = $rootScope.addressbook.card.$addPhone('');
|
2014-09-17 22:28:40 +02:00
|
|
|
focus('phone_' + i);
|
|
|
|
};
|
|
|
|
$scope.addUrl = function() {
|
|
|
|
var i = $rootScope.addressbook.card.$addUrl('', '');
|
|
|
|
focus('url_' + i);
|
|
|
|
};
|
|
|
|
$scope.addAddress = function() {
|
|
|
|
var i = $rootScope.addressbook.card.$addAddress('', '', '', '', '', '', '', '');
|
|
|
|
focus('address_' + i);
|
|
|
|
};
|
|
|
|
$scope.addMember = function() {
|
|
|
|
var i = $rootScope.addressbook.card.$addMember('');
|
|
|
|
focus('ref_' + i);
|
|
|
|
};
|
|
|
|
$scope.save = function(form) {
|
|
|
|
if (form.$valid) {
|
|
|
|
$rootScope.addressbook.card.$save()
|
|
|
|
.then(function(data) {
|
|
|
|
var i = _.indexOf(_.pluck($rootScope.addressbook.cards, 'id'), $rootScope.addressbook.card.id);
|
|
|
|
if (i < 0) {
|
|
|
|
// Reload contacts list and show addressbook in which the card has been created
|
|
|
|
$rootScope.addressbook = AddressBook.$find(data.pid);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
// Update contacts list with new version of the Card object
|
|
|
|
$rootScope.addressbook.cards[i] = angular.copy($rootScope.addressbook.card);
|
|
|
|
}
|
|
|
|
$state.go('addressbook.card');
|
|
|
|
}, function(data, status) {
|
|
|
|
console.debug('failed');
|
|
|
|
});
|
|
|
|
}
|
|
|
|
};
|
|
|
|
$scope.cancel = function() {
|
|
|
|
$scope.reset();
|
2014-09-19 02:46:52 +02:00
|
|
|
delete $rootScope.masterCard;
|
2014-09-17 22:28:40 +02:00
|
|
|
if ($scope.addressbook.card.id) {
|
|
|
|
// Cancelling the edition of an existing card
|
2014-09-19 02:46:52 +02:00
|
|
|
$state.go('addressbook.card', { cardId: $scope.addressbook.card.id });
|
2014-09-17 22:28:40 +02:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
// Cancelling the creation of a card
|
|
|
|
delete $rootScope.addressbook.card;
|
2014-09-19 02:46:52 +02:00
|
|
|
$state.go('addressbook', { addressbookId: $scope.addressbook.id });
|
2014-09-17 22:28:40 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
$scope.reset = function() {
|
2014-09-19 02:46:52 +02:00
|
|
|
$rootScope.addressbook.card = angular.copy($rootScope.masterCard);
|
2014-09-17 22:28:40 +02:00
|
|
|
};
|
|
|
|
$scope.confirmDelete = function(card) {
|
|
|
|
Dialog.confirm(l('Warning'),
|
|
|
|
l('Are you sure you want to delete the card of <em>%{0}</em>?', card.$fullname()))
|
|
|
|
.then(function(res) {
|
|
|
|
if (res) {
|
|
|
|
// User confirmed the deletion
|
|
|
|
card.$delete()
|
|
|
|
.then(function() {
|
|
|
|
$rootScope.addressbook.cards = _.reject($rootScope.addressbook.cards, function(o) {
|
|
|
|
return o.id == card.id;
|
|
|
|
});
|
|
|
|
delete $rootScope.addressbook.card;
|
|
|
|
}, function(data, status) {
|
|
|
|
Dialog.alert(l('Warning'), l('An error occured while deleting the card "%{0}".',
|
|
|
|
card.$fullname()));
|
2014-09-05 21:12:05 +02:00
|
|
|
});
|
2014-09-17 22:28:40 +02:00
|
|
|
}
|
|
|
|
});
|
|
|
|
};
|
2014-08-06 21:14:05 +02:00
|
|
|
}]);
|
2014-06-19 21:50:29 +02:00
|
|
|
|
|
|
|
})();
|