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 ) {
2014-09-23 17:52:47 +02:00
return AddressBook . $findAll ( contactFolders ) ;
2014-09-17 22:28:40 +02:00
} ] ,
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.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-29 17:06:46 +02:00
var tag = 'v' + $stateParams . contactType ,
card = new Card ( { pid : $stateParams . addressbookId , tag : tag } ) ;
return card ;
2014-09-17 22:28:40 +02:00
} ]
}
} )
2014-09-29 17:06:46 +02:00
. state ( 'addressbook.card' , {
url : '/:cardId' ,
abstract : true ,
2014-09-17 22:28:40 +02:00
views : {
card : {
2014-09-29 17:06:46 +02:00
template : '<ui-view/>' ,
2014-09-17 22:28:40 +02:00
}
} ,
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-09-29 17:06:46 +02:00
} )
. state ( 'addressbook.card.view' , {
url : '/view' ,
templateUrl : 'card.html' ,
controller : 'CardCtrl'
} )
. state ( 'addressbook.card.editor' , {
url : '/edit' ,
templateUrl : 'cardEditor.html' ,
controller : 'CardCtrl'
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 ;
2014-10-07 20:54:12 +02:00
$scope . originalAddressbook = angular . extend ( { } , $scope . addressbook . $omit ( ) ) ;
2014-09-17 22:28:40 +02:00
focus ( 'addressBookName_' + i ) ;
}
} ;
2014-10-07 20:54:12 +02:00
$scope . revertEditing = function ( i ) {
$scope . addressbooks [ i ] . name = $scope . originalAddressbook . name ;
$scope . editMode = false ;
} ;
2014-09-17 22:28:40 +02:00
$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 ( ) {
2014-10-15 20:37:15 +02:00
} ;
$scope . exportCards = function ( ) {
window . location . href = ApplicationBaseURL + "/" + $rootScope . addressbook . id + "/exportFolder" ;
2014-09-17 22:28:40 +02:00
} ;
$scope . share = function ( ) {
var modal = $modal . open ( {
templateUrl : 'addressbookSharing.html' ,
2014-10-08 20:08:54 +02:00
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 */
2014-09-17 22:28:40 +02:00
$scope . closeModal = function ( ) {
$modalInstance . close ( ) ;
} ;
2014-10-08 20:08:54 +02:00
$scope . saveModal = function ( ) {
if ( ! _ . isEmpty ( dirtyObjects ) ) {
2014-10-08 21:14:15 +02:00
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 ( ) ;
}
2014-10-08 20:08:54 +02:00
}
2014-10-08 21:14:15 +02:00
else
$scope . $aclEditorModal . remove ( ) ;
2014-10-08 20:08:54 +02:00
} ;
$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" ) ;
} ;
2014-09-17 22:28:40 +02:00
}
} ) ;
} ;
$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-29 17:06:46 +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 ;
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-17 22:28:40 +02:00
$scope . addOrgUnit = function ( ) {
2014-09-29 17:06:46 +02:00
var i = $scope . card . $addOrgUnit ( '' ) ;
2014-09-17 22:28:40 +02:00
focus ( 'orgUnit_' + i ) ;
} ;
$scope . addCategory = function ( ) {
2014-09-29 17:06:46 +02:00
var i = $scope . card . $addCategory ( '' ) ;
2014-09-17 22:28:40 +02:00
focus ( 'category_' + i ) ;
} ;
$scope . addEmail = function ( ) {
2014-09-29 17:06:46 +02:00
var i = $scope . card . $addEmail ( '' ) ;
2014-09-17 22:28:40 +02:00
focus ( 'email_' + i ) ;
} ;
$scope . addPhone = function ( ) {
2014-09-29 17:06:46 +02:00
var i = $scope . card . $addPhone ( '' ) ;
2014-09-17 22:28:40 +02:00
focus ( 'phone_' + i ) ;
} ;
$scope . addUrl = function ( ) {
2014-09-29 17:06:46 +02:00
var i = $scope . card . $addUrl ( '' , '' ) ;
2014-09-17 22:28:40 +02:00
focus ( 'url_' + i ) ;
} ;
$scope . addAddress = function ( ) {
2014-09-29 17:06:46 +02:00
var i = $scope . card . $addAddress ( '' , '' , '' , '' , '' , '' , '' , '' ) ;
2014-09-17 22:28:40 +02:00
focus ( 'address_' + i ) ;
} ;
$scope . addMember = function ( ) {
2014-09-29 17:06:46 +02:00
var i = $scope . card . $addMember ( '' ) ;
2014-09-17 22:28:40 +02:00
focus ( 'ref_' + i ) ;
} ;
$scope . save = function ( form ) {
if ( form . $valid ) {
2014-09-29 17:06:46 +02:00
$scope . card . $save ( )
2014-09-17 22:28:40 +02:00
. then ( function ( data ) {
2014-09-29 17:06:46 +02:00
var i = _ . indexOf ( _ . pluck ( $rootScope . addressbook . cards , 'id' ) , $scope . card . id ) ;
2014-09-17 22:28:40 +02:00
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
2014-09-29 17:06:46 +02:00
$rootScope . addressbook . cards [ i ] = angular . copy ( $scope . card ) ;
2014-09-17 22:28:40 +02:00
}
2014-09-29 17:06:46 +02:00
$state . go ( 'addressbook.card.view' , { cardId : $scope . card . id } ) ;
2014-09-17 22:28:40 +02:00
} , function ( data , status ) {
console . debug ( 'failed' ) ;
} ) ;
}
} ;
2014-09-29 17:06:46 +02:00
$scope . reset = function ( ) {
2014-09-29 18:12:28 +02:00
$scope . card . $reset ( ) ;
2014-09-29 17:06:46 +02:00
} ;
2014-09-17 22:28:40 +02:00
$scope . cancel = function ( ) {
2014-09-29 18:12:28 +02:00
$scope . card . $reset ( ) ;
2014-09-29 17:06:46 +02:00
if ( $scope . card . isNew ) {
2014-09-17 22:28:40 +02:00
// Cancelling the creation of a card
2014-09-29 17:06:46 +02:00
delete $scope . card ;
2014-09-19 02:46:52 +02:00
$state . go ( 'addressbook' , { addressbookId : $scope . addressbook . id } ) ;
2014-09-17 22:28:40 +02:00
}
2014-09-29 17:06:46 +02:00
else {
// Cancelling the edition of an existing card
$state . go ( 'addressbook.card.view' , { cardId : $scope . card . id } ) ;
}
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 ( ) {
2014-09-29 18:12:28 +02:00
// Remove card from list of addressbook
2014-09-17 22:28:40 +02:00
$rootScope . addressbook . cards = _ . reject ( $rootScope . addressbook . cards , function ( o ) {
return o . id == card . id ;
} ) ;
2014-09-29 18:12:28 +02:00
// Remove card object from scope
2014-09-29 17:06:46 +02:00
delete $scope . card ;
2014-09-17 22:28:40 +02:00
} , 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
} ) ( ) ;