2014-06-19 21:50:29 +02:00
|
|
|
/* -*- Mode: javascript; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
|
|
|
|
/* JavaScript for SOGoContacts */
|
|
|
|
|
|
|
|
(function() {
|
|
|
|
'use strict';
|
|
|
|
|
2014-08-06 21:14:05 +02:00
|
|
|
angular.module('SOGo.Common', []);
|
2014-06-19 21:50:29 +02:00
|
|
|
|
2014-09-05 21:12:05 +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-08-11 16:10:25 +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) {
|
|
|
|
$stateProvider
|
|
|
|
.state('addressbook', {
|
|
|
|
url: "/:addressbook_id",
|
|
|
|
views: {
|
|
|
|
'addressbook': {
|
|
|
|
templateUrl: "addressbook.html",
|
|
|
|
controller: 'AddressBookCtrl'
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.state('addressbook.card', {
|
|
|
|
url: "/:card_id",
|
|
|
|
views: {
|
|
|
|
'card': {
|
|
|
|
templateUrl: "card.html",
|
2014-09-05 21:12:05 +02:00
|
|
|
controller: 'CardCtrl'
|
2014-08-06 21:14:05 +02:00
|
|
|
}
|
|
|
|
}
|
2014-08-20 21:49:44 +02:00
|
|
|
})
|
|
|
|
.state('addressbook.new', {
|
|
|
|
url: "/:contact_type/new",
|
|
|
|
views: {
|
|
|
|
'card': {
|
2014-09-05 21:12:05 +02:00
|
|
|
templateUrl: "cardEditor.html",
|
|
|
|
controller: 'CardCtrl'
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.state('addressbook.editor', {
|
|
|
|
url: "/:card_id/edit",
|
|
|
|
views: {
|
|
|
|
'card': {
|
|
|
|
templateUrl: "cardEditor.html",
|
|
|
|
controller: 'CardCtrl'
|
2014-08-20 21:49:44 +02:00
|
|
|
}
|
|
|
|
}
|
2014-06-19 21:50:29 +02:00
|
|
|
});
|
|
|
|
|
2014-08-06 21:14:05 +02:00
|
|
|
// if none of the above states are matched, use this as the fallback
|
|
|
|
$urlRouterProvider.otherwise('/personal');
|
|
|
|
}])
|
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-06-19 21:50:29 +02:00
|
|
|
|
2014-08-06 21:14:05 +02:00
|
|
|
.factory('sgFocus', ['$rootScope', '$timeout', function ($rootScope, $timeout) {
|
|
|
|
return function(name) {
|
|
|
|
$timeout(function (){
|
|
|
|
$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-08-20 21:49:44 +02:00
|
|
|
.controller('AddressBookCtrl', ['$state', '$scope', '$rootScope', '$stateParams', '$timeout', '$modal', 'sgFocus', 'sgCard', 'sgAddressBook', 'sgDialog', function($state, $scope, $rootScope, $stateParams, $timeout, $modal, focus, Card, AddressBook, Dialog) {
|
|
|
|
// $scope objects
|
2014-08-06 21:14:05 +02:00
|
|
|
$scope.search = { 'status': null, 'filter': null, 'last_filter': null };
|
2014-08-20 21:49:44 +02:00
|
|
|
|
2014-08-06 21:14:05 +02:00
|
|
|
if ($stateParams.addressbook_id &&
|
|
|
|
($rootScope.addressbook == undefined || $stateParams.addressbook_id != $rootScope.addressbook.id)) {
|
2014-08-20 21:49:44 +02:00
|
|
|
// Selected addressbook has changed; fetch list of contacts
|
2014-08-06 21:14:05 +02:00
|
|
|
$rootScope.addressbook = AddressBook.$find($stateParams.addressbook_id);
|
2014-08-20 21:49:44 +02:00
|
|
|
// Adjust search status depending on addressbook type
|
2014-08-06 21:14:05 +02:00
|
|
|
var o = _.find($rootScope.addressbooks, function(o) {
|
|
|
|
return o.id == $stateParams.addressbook_id;
|
|
|
|
});
|
2014-08-20 21:49:44 +02:00
|
|
|
$scope.search.status = (o && o.isRemote)? 'remote-addressbook' : '';
|
2014-08-06 21:14:05 +02:00
|
|
|
}
|
|
|
|
// Initialize with data from template
|
|
|
|
$scope.init = function() {
|
|
|
|
$rootScope.addressbooks = AddressBook.$all(contactFolders);
|
|
|
|
};
|
2014-08-20 21:49:44 +02:00
|
|
|
// $scope functions
|
2014-08-06 21:14:05 +02:00
|
|
|
$scope.select = function(rowIndex) {
|
|
|
|
$scope.editMode = false;
|
|
|
|
};
|
2014-09-10 22:20:52 +02:00
|
|
|
$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) {
|
2014-08-20 21:49:44 +02:00
|
|
|
if (!$rootScope.addressbook.isRemote) {
|
|
|
|
if (angular.isUndefined(i)) {
|
|
|
|
i = _.indexOf(_.pluck($rootScope.addressbooks, 'id'), $rootScope.addressbook.id);
|
|
|
|
}
|
|
|
|
$scope.editMode = $rootScope.addressbook.id;
|
|
|
|
focus('addressBookName_' + i);
|
2014-08-06 21:14:05 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
$scope.save = function(i) {
|
|
|
|
$rootScope.addressbook.name = $rootScope.addressbooks[i].name;
|
|
|
|
$rootScope.addressbook.$save()
|
2014-06-19 21:50:29 +02:00
|
|
|
.then(function(data) {
|
|
|
|
$scope.editMode = false;
|
|
|
|
}, function(data, status) {
|
2014-09-05 21:12:05 +02:00
|
|
|
Dialog.alert(l('Warning'), data);
|
2014-06-19 21:50:29 +02:00
|
|
|
});
|
2014-08-06 21:14:05 +02:00
|
|
|
};
|
2014-08-20 21:49:44 +02:00
|
|
|
$scope.confirmDelete = function() {
|
|
|
|
Dialog.confirm(l('Warning'), l('Are you sure you want to delete the addressbook "%{0}"?',
|
2014-09-10 22:20:52 +02:00
|
|
|
$rootScope.addressbook.name))
|
|
|
|
.then(function(res) {
|
|
|
|
if (res) {
|
|
|
|
$rootScope.addressbook.$delete()
|
|
|
|
.then(function() {
|
|
|
|
$rootScope.addressbook = null;
|
|
|
|
}, function(data, status) {
|
|
|
|
Dialog.alert(l('An error occured while deleting the addressbook "%{0}".',
|
|
|
|
$rootScope.addressbook.name),
|
|
|
|
l(data.error));
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
2014-08-20 21:49:44 +02:00
|
|
|
};
|
|
|
|
$scope.share = function() {
|
2014-08-06 21:14:05 +02:00
|
|
|
var modal = $modal.open({
|
|
|
|
templateUrl: 'addressbookSharing.html',
|
|
|
|
controller: function($scope, $modalInstance) {
|
|
|
|
$scope.closeModal = function() {
|
|
|
|
$modalInstance.close();
|
|
|
|
};
|
|
|
|
}
|
|
|
|
});
|
|
|
|
};
|
|
|
|
$scope.doSearch = function(keyEvent) {
|
|
|
|
if ($scope.search.filter != $scope.search.last_filter) {
|
|
|
|
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.addressbook_id);
|
|
|
|
// Extend resulting model instance with parameters from addressbooks listing
|
|
|
|
var o = _.find($rootScope.addressbooks, function(o) {
|
|
|
|
return o.id == $stateParams.addressbook_id;
|
|
|
|
});
|
|
|
|
$scope.search.status = (o.isRemote)? 'remote-addressbook' : '';
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
$scope.search.status = 'min-char';
|
|
|
|
$rootScope.addressbook.cards = [];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$scope.search.last_filter = $scope.search.filter;
|
|
|
|
};
|
|
|
|
}])
|
|
|
|
|
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-08-06 21:14:05 +02:00
|
|
|
if ($stateParams.card_id) {
|
2014-09-05 21:12:05 +02:00
|
|
|
if (!$rootScope.addressbook) {
|
2014-08-06 21:14:05 +02:00
|
|
|
// Card is directly access with URL fragment
|
|
|
|
$rootScope.addressbook = AddressBook.$find($stateParams.addressbook_id);
|
|
|
|
}
|
2014-08-20 21:49:44 +02:00
|
|
|
$rootScope.addressbook.$getCard($stateParams.card_id)
|
2014-08-06 21:14:05 +02:00
|
|
|
$scope.editMode = false;
|
2014-06-19 21:50:29 +02:00
|
|
|
}
|
2014-08-20 21:49:44 +02:00
|
|
|
else if ($stateParams.contact_type) {
|
|
|
|
// Create new card or list
|
|
|
|
var tag = 'v' + $stateParams.contact_type;
|
|
|
|
$scope.addressbook.card = new Card({ 'pid': $stateParams.addressbook_id, 'tag': tag });
|
|
|
|
$scope.editMode = true;
|
|
|
|
}
|
2014-09-05 21:12:05 +02:00
|
|
|
|
2014-08-06 21:14:05 +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-05 21:12:05 +02:00
|
|
|
$rootScope.master_card = angular.copy($rootScope.addressbook.card);
|
|
|
|
|
2014-08-06 21:14:05 +02:00
|
|
|
$scope.addOrgUnit = function() {
|
|
|
|
var i = $rootScope.addressbook.card.$addOrgUnit('');
|
|
|
|
focus('orgUnit_' + i);
|
|
|
|
};
|
|
|
|
$scope.addCategory = function() {
|
|
|
|
var i = $rootScope.addressbook.card.$addCategory($scope.new_category);
|
|
|
|
focus('category_' + i);
|
|
|
|
};
|
|
|
|
$scope.addEmail = function() {
|
|
|
|
var i = $rootScope.addressbook.card.$addEmail($scope.new_email_type);
|
|
|
|
focus('email_' + i);
|
|
|
|
};
|
|
|
|
$scope.addPhone = function() {
|
|
|
|
var i = $rootScope.addressbook.card.$addPhone($scope.new_phone_type);
|
|
|
|
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);
|
|
|
|
};
|
2014-09-05 21:12:05 +02:00
|
|
|
$scope.addMember = function() {
|
|
|
|
var i = $rootScope.addressbook.card.$addMember('');
|
|
|
|
focus('ref_' + i);
|
|
|
|
};
|
|
|
|
$scope.save = function(form) {
|
|
|
|
if (form.$valid) {
|
2014-08-06 21:14:05 +02:00
|
|
|
$rootScope.addressbook.card.$save()
|
|
|
|
.then(function(data) {
|
2014-08-20 21:49:44 +02:00
|
|
|
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);
|
|
|
|
}
|
2014-09-05 21:12:05 +02:00
|
|
|
$state.go('addressbook.card');
|
2014-08-06 21:14:05 +02:00
|
|
|
}, function(data, status) {
|
|
|
|
console.debug("failed");
|
|
|
|
});
|
|
|
|
}
|
|
|
|
};
|
|
|
|
$scope.cancel = function() {
|
|
|
|
$scope.reset();
|
2014-09-05 21:12:05 +02:00
|
|
|
delete $rootScope.master_card;
|
2014-09-10 22:20:52 +02:00
|
|
|
if ($scope.addressbook.card.id) {
|
|
|
|
// Cancelling the edition of an existing card
|
|
|
|
$state.go('addressbook.card', { card_id: $scope.addressbook.card.id });
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
// Cancelling the creation of a card
|
|
|
|
delete $rootScope.addressbook.card;
|
|
|
|
$state.go('addressbook', { addressbook_id: $scope.addressbook.id });
|
|
|
|
}
|
2014-08-06 21:14:05 +02:00
|
|
|
};
|
|
|
|
$scope.reset = function() {
|
|
|
|
$rootScope.addressbook.card = angular.copy($rootScope.master_card);
|
|
|
|
};
|
2014-08-20 21:49:44 +02:00
|
|
|
$scope.confirmDelete = function(card) {
|
|
|
|
Dialog.confirm(l('Warning'),
|
2014-09-05 21:12:05 +02:00
|
|
|
l('Are you sure you want to delete the card of <b>%{0}</b>?', card.$fullname()))
|
|
|
|
.then(function(res) {
|
|
|
|
if (res) {
|
|
|
|
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-08-20 21:49:44 +02:00
|
|
|
};
|
2014-08-06 21:14:05 +02:00
|
|
|
}]);
|
2014-06-19 21:50:29 +02:00
|
|
|
|
|
|
|
})();
|