sogo/UI/WebServerResources/js/ContactsUI.js

427 lines
16 KiB
JavaScript
Raw Normal View History

/* -*- Mode: javascript; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2014-06-19 21:50:29 +02:00
/* JavaScript for SOGoContacts */
(function() {
'use strict';
2014-06-19 21:50:29 +02:00
angular.module('SOGo.Common', []);
2014-06-19 21:50:29 +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', {
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) {
$stateProvider
.state('addressbook', {
url: '/:addressbookId',
views: {
addressbooks: {
templateUrl: 'addressbooks.html',
controller: 'AddressBookCtrl'
}
},
resolve: {
stateAddressbooks: ['sgAddressBook', function(AddressBook) {
return AddressBook.$findAll(contactFolders);
}],
stateAddressbook: ['$stateParams', 'sgAddressBook', function($stateParams, AddressBook) {
return AddressBook.$find($stateParams.addressbookId);
}]
}
})
.state('addressbook.new', {
url: '/:contactType/new',
views: {
card: {
templateUrl: 'cardEditor.html',
controller: 'CardCtrl'
}
},
resolve: {
stateCard: ['$stateParams', 'stateAddressbook', 'sgCard', function($stateParams, stateAddressbook, Card) {
var tag = 'v' + $stateParams.contactType,
card = new Card({ pid: $stateParams.addressbookId, tag: tag });
return card;
}]
}
})
.state('addressbook.card', {
url: '/:cardId',
abstract: true,
views: {
card: {
template: '<ui-view/>',
}
},
resolve: {
stateCard: ['$stateParams', 'stateAddressbook', function($stateParams, stateAddressbook) {
return stateAddressbook.$getCard($stateParams.cardId);
}]
}
})
.state('addressbook.card.view', {
url: '/view',
templateUrl: 'card.html',
controller: 'CardCtrl'
})
.state('addressbook.card.editor', {
url: '/edit',
templateUrl: 'cardEditor.html',
controller: 'CardCtrl'
});
2014-06-19 21:50:29 +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() {
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
.factory('sgFocus', ['$rootScope', '$timeout', function($rootScope, $timeout) {
2014-08-06 21:14:05 +02:00
return function(name) {
$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
.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) {
var currentAddressbook;
// Resolve objects
$scope.addressbooks = stateAddressbooks;
$rootScope.addressbook = stateAddressbook;
// $scope objects
$scope.search = { status: null, filter: null, lastFilter: null };
// Adjust search status depending on addressbook type
currentAddressbook = _.find($scope.addressbooks, function(o) {
return o.id == $stateParams.addressbookId;
});
$scope.search.status = (currentAddressbook && currentAddressbook.isRemote)? 'remote-addressbook' : '';
// $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
}
);
AddressBook.$add(addressbook);
2014-08-06 21:14:05 +02:00
}
});
};
$scope.edit = function(i) {
if (!$rootScope.addressbook.isRemote) {
if (angular.isUndefined(i)) {
i = _.indexOf(_.pluck($scope.addressbooks, 'id'), $rootScope.addressbook.id);
}
$scope.editMode = $rootScope.addressbook.id;
$scope.originalAddressbook = angular.extend({}, $scope.addressbook.$omit());
focus('addressBookName_' + i);
}
};
$scope.revertEditing = function(i) {
$scope.addressbooks[i].name = $scope.originalAddressbook.name;
$scope.editMode = false;
};
$scope.save = function(i) {
var name = $scope.addressbooks[i].name;
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) {
Dialog.alert(l('An error occured while deleting the addressbook "%{0}".',
$rootScope.addressbook.name),
l(data.error));
});
2014-08-06 21:14:05 +02:00
}
});
};
$scope.importCards = function() {
2014-10-15 20:37:15 +02:00
};
$scope.exportCards = function() {
window.location.href = ApplicationBaseURL + "/" + $rootScope.addressbook.id + "/exportFolder";
};
$scope.share = function() {
var modal = $modal.open({
templateUrl: 'addressbookSharing.html',
controller: function($scope, $http, $modalInstance, sgAclUsers, sgUser) {
/* Variables for the scope */
$scope.AclUsers = new sgAclUsers($rootScope.addressbook);
$scope.User = new sgUser($rootScope.addressbook);
var dirtyObjects = {};
$scope.User.$acls().then(function(users) {
$scope.users = [];
angular.forEach(users, function(user){
user.canSubscribeUser = (user.isSubscribed) ? false : true;
$scope.users.push(user);
})
});
/* Functions */
$scope.closeModal = function() {
$modalInstance.close();
};
$scope.saveModal = function() {
if(!_.isEmpty(dirtyObjects)) {
if(dirtyObjects["anonymous"])
{
Dialog.confirm(l("Warning"), l("Any user with an account on this system will be able to access your folder. Are you certain you trust them all?")).then(function(res){
if(res){
$scope.AclUsers.saveUsersRights(dirtyObjects);
$modalInstance.close();
};
})
}
else if (dirtyObjects["<default>"]) {
Dialog.confirm(l("Warning"), l("Potentially anyone on the Internet will be able to access your folder, even if they do not have an account on this system. Is this information suitable for the public Internet?")).then(function(res){
if(res){
$scope.AclUsers.saveUsersRights(dirtyObjects);
$modalInstance.close();
};
})
}
else {
$scope.AclUsers.saveUsersRights(dirtyObjects);
var usersToSubscribe = [];
angular.forEach(dirtyObjects, function(dirtyObject){
if(dirtyObject.canSubscribeUser && dirtyObject.isSubscribed){
usersToSubscribe.push(dirtyObject.uid);
}
})
if(!_.isEmpty(usersToSubscribe))
$scope.AclUsers.subscribeUsers(usersToSubscribe);
$modalInstance.close();
}
}
else
$scope.$aclEditorModal.remove();
};
$scope.removeUser = function() {
if (!_.isEmpty($scope.userSelected)) {
if(dirtyObjects[$scope.userSelected.uid])
delete dirtyObjects[$scope.userSelected.uid];
$scope.AclUsers.removeUser($scope.userSelected.uid);
// Remove from the users list
$scope.users = _.reject($scope.users, function(o) {
return o.uid == $scope.userSelected.uid;
});
$scope.userSelected = {};
}
};
$scope.addUser = function(user) {
if (user.uid) {
// Looks through the list and returns the first value that matches all of the key-value pairs listed
if(!_.findWhere($scope.users, {uid: user.uid})) {
$scope.AclUsers.addUser(user.uid);
$scope.User.$acls().then(function(users) {
$scope.users = [];
angular.forEach(users, function(user){
user.canSubscribeUser = (user.isSubscribed) ? false : true;
$scope.users.push(user);
})
});
}
else
Dialog.alert(l('Warning'), l('You have already subscribed to that folder!'));
}
else
Dialog.alert(l('Warning'), l('Please select a user inside your domain'));
};
$scope.selectUser = function(user) {
// Check if it is a different user
if ($scope.userSelected != user){
$scope.userSelected = {};
$scope.selected = user;
$scope.userSelected = user;
if (dirtyObjects[$scope.userSelected.uid]) {
$scope.userSelected.aclOptions = dirtyObjects[$scope.userSelected.uid].aclOptions;
}
else {
$scope.AclUsers.userRights($scope.userSelected.uid).then(function(userRights) {
$scope.userSelected.aclOptions = userRights;
});
}
}
};
$scope.markUserAsDirty = function(user) {
if(!$scope.userSelected) {
$scope.selectUser(user);
dirtyObjects[$scope.userSelected.uid] = $scope.userSelected;
}
else
dirtyObjects[$scope.userSelected.uid] = $scope.userSelected;
};
$scope.displayUserRights = function() {
return ($scope.userSelected && ($scope.userSelected.uid != "anonymous")) ? true : false;
};
$scope.userIsReadOnly = function() {
return (!$scope.userSelected || $scope.userSelected.userClass == "public-user");
};
}
});
};
$scope.doSearch = function(keyEvent) {
if ($scope.search.filter != $scope.search.lastFilter) {
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) {
$rootScope.addressbook = AddressBook.$find($stateParams.addressbookId);
// Extend resulting model instance with parameters from addressbooks listing
var o = _.find($scope.addressbooks, function(o) {
return o.id == $stateParams.addressbookId;
});
$scope.search.status = (o.isRemote)? 'remote-addressbook' : '';
}
else {
$scope.search.status = 'min-char';
$rootScope.addressbook.cards = [];
}
}
$scope.search.lastFilter = $scope.search.filter;
};
2014-08-06 21:14:05 +02:00
}])
/**
* Controller to view and edit a card
*/
.controller('CardCtrl', ['$scope', '$rootScope', '$timeout', 'sgAddressBook', 'sgCard', 'sgDialog', 'sgFocus', '$state', '$stateParams', 'stateCard', function($scope, $rootScope, $timeout, AddressBook, Card, Dialog, focus, $state, $stateParams, stateCard) {
$scope.card = stateCard;
$scope.allEmailTypes = Card.$EMAIL_TYPES;
$scope.allTelTypes = Card.$TEL_TYPES;
$scope.allUrlTypes = Card.$URL_TYPES;
$scope.allAddressTypes = Card.$ADDRESS_TYPES;
$scope.addOrgUnit = function() {
var i = $scope.card.$addOrgUnit('');
focus('orgUnit_' + i);
};
$scope.addCategory = function() {
var i = $scope.card.$addCategory('');
focus('category_' + i);
};
$scope.addEmail = function() {
var i = $scope.card.$addEmail('');
focus('email_' + i);
};
$scope.addPhone = function() {
var i = $scope.card.$addPhone('');
focus('phone_' + i);
};
$scope.addUrl = function() {
var i = $scope.card.$addUrl('', '');
focus('url_' + i);
};
$scope.addAddress = function() {
var i = $scope.card.$addAddress('', '', '', '', '', '', '', '');
focus('address_' + i);
};
$scope.addMember = function() {
var i = $scope.card.$addMember('');
focus('ref_' + i);
};
$scope.save = function(form) {
if (form.$valid) {
$scope.card.$save()
.then(function(data) {
var i = _.indexOf(_.pluck($rootScope.addressbook.cards, 'id'), $scope.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($scope.card);
}
$state.go('addressbook.card.view', { cardId: $scope.card.id });
}, function(data, status) {
console.debug('failed');
});
}
};
$scope.reset = function() {
$scope.card.$reset();
};
$scope.cancel = function() {
$scope.card.$reset();
if ($scope.card.isNew) {
// Cancelling the creation of a card
delete $scope.card;
$state.go('addressbook', { addressbookId: $scope.addressbook.id });
}
else {
// Cancelling the edition of an existing card
$state.go('addressbook.card.view', { cardId: $scope.card.id });
}
};
$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() {
// Remove card from list of addressbook
$rootScope.addressbook.cards = _.reject($rootScope.addressbook.cards, function(o) {
return o.id == card.id;
});
// Remove card object from scope
delete $scope.card;
}, function(data, status) {
Dialog.alert(l('Warning'), l('An error occured while deleting the card "%{0}".',
card.$fullname()));
});
}
});
};
2014-08-06 21:14:05 +02:00
}]);
2014-06-19 21:50:29 +02:00
})();