2014-09-19 02:46:52 +02:00
|
|
|
/* -*- Mode: javascript; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
|
2014-06-19 21:47:33 +02:00
|
|
|
(function() {
|
2014-09-19 02:46:52 +02:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @name AddressBook
|
|
|
|
* @constructor
|
2014-11-25 22:13:53 +01:00
|
|
|
* @param {object} futureAddressBookData - either an object literal or a promise
|
2014-09-19 02:46:52 +02:00
|
|
|
*/
|
|
|
|
function AddressBook(futureAddressBookData) {
|
|
|
|
// Data is immediately available
|
|
|
|
if (typeof futureAddressBookData.then !== 'function') {
|
2015-04-24 14:45:39 +02:00
|
|
|
this.init(futureAddressBookData);
|
2014-09-19 02:46:52 +02:00
|
|
|
if (this.name && !this.id) {
|
|
|
|
// Create a new addressbook on the server
|
|
|
|
var newAddressBookData = AddressBook.$$resource.create('createFolder', this.name);
|
|
|
|
this.$unwrap(newAddressBookData);
|
|
|
|
}
|
2014-11-03 18:19:35 +01:00
|
|
|
else if (this.id) {
|
2014-12-12 19:39:02 +01:00
|
|
|
this.$acl = new AddressBook.$$Acl('Contacts/' + this.id);
|
2014-10-17 17:01:56 +02:00
|
|
|
}
|
2014-09-19 02:46:52 +02:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
// The promise will be unwrapped first
|
|
|
|
this.$unwrap(futureAddressBookData);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-09-23 17:52:47 +02:00
|
|
|
/**
|
|
|
|
* @memberof AddressBook
|
|
|
|
* @desc The factory we'll use to register with Angular
|
|
|
|
* @returns the AddressBook constructor
|
|
|
|
*/
|
2015-07-31 01:47:53 +02:00
|
|
|
AddressBook.$factory = ['$q', '$timeout', '$log', 'sgSettings', 'Resource', 'Card', 'Acl', 'Preferences', function($q, $timeout, $log, Settings, Resource, Card, Acl, Preferences) {
|
2014-09-19 02:46:52 +02:00
|
|
|
angular.extend(AddressBook, {
|
|
|
|
$q: $q,
|
|
|
|
$timeout: $timeout,
|
2015-01-08 22:11:07 +01:00
|
|
|
$log: $log,
|
2015-08-03 17:53:54 +02:00
|
|
|
$$resource: new Resource(Settings.activeUser('folderURL') + 'Contacts', Settings.activeUser()),
|
2014-10-17 17:01:56 +02:00
|
|
|
$Card: Card,
|
2014-11-03 21:58:54 +01:00
|
|
|
$$Acl: Acl,
|
2015-07-31 01:47:53 +02:00
|
|
|
$Preferences: Preferences,
|
2015-08-14 21:22:59 +02:00
|
|
|
$query: {search: 'name_or_address', value: '', sort: 'c_cn', asc: 1},
|
2015-08-03 22:18:23 +02:00
|
|
|
activeUser: Settings.activeUser(),
|
2015-07-31 01:47:53 +02:00
|
|
|
selectedFolder: null,
|
|
|
|
$refreshTimeout: null
|
2014-09-19 02:46:52 +02:00
|
|
|
});
|
2015-08-14 21:22:59 +02:00
|
|
|
// Initialize sort parameters from user's settings
|
|
|
|
Preferences.ready().then(function() {
|
|
|
|
if (Preferences.settings.Contact.SortingState) {
|
|
|
|
AddressBook.$query.sort = Preferences.settings.Contact.SortingState[0];
|
|
|
|
AddressBook.$query.asc = parseInt(Preferences.settings.Contact.SortingState[1]);
|
|
|
|
}
|
|
|
|
});
|
2014-09-19 02:46:52 +02:00
|
|
|
return AddressBook; // return constructor
|
|
|
|
}];
|
|
|
|
|
2015-07-29 17:47:01 +02:00
|
|
|
/**
|
|
|
|
* @module SOGo.ContactsUI
|
|
|
|
* @desc Factory registration of AddressBook in Angular module.
|
|
|
|
*/
|
|
|
|
try {
|
|
|
|
angular.module('SOGo.ContactsUI');
|
|
|
|
}
|
|
|
|
catch(e) {
|
|
|
|
angular.module('SOGo.ContactsUI', ['SOGo.Common']);
|
|
|
|
}
|
2014-09-19 02:46:52 +02:00
|
|
|
angular.module('SOGo.ContactsUI')
|
2015-05-06 04:06:13 +02:00
|
|
|
.factory('AddressBook', AddressBook.$factory);
|
2014-09-23 17:52:47 +02:00
|
|
|
|
2014-12-12 19:39:02 +01:00
|
|
|
/**
|
|
|
|
* @memberof AddressBook
|
|
|
|
* @desc Search for cards among all addressbooks matching some criterias.
|
|
|
|
* @param {string} search - the search string to match
|
2014-12-18 22:30:59 +01:00
|
|
|
* @param {object} [options] - additional options to the query (excludeGroups and excludeLists)
|
2015-08-05 22:44:25 +02:00
|
|
|
* @param {object[]} excludedCards - a list of Card objects that must be excluded from the results
|
2014-12-12 19:39:02 +01:00
|
|
|
* @returns a collection of Cards instances
|
|
|
|
*/
|
2015-08-05 22:44:25 +02:00
|
|
|
AddressBook.$filterAll = function(search, options, excludedCards) {
|
2014-12-12 19:39:02 +01:00
|
|
|
var params = {search: search};
|
|
|
|
|
2015-08-05 22:44:25 +02:00
|
|
|
if (!search) {
|
|
|
|
// No query specified
|
|
|
|
AddressBook.$cards = [];
|
|
|
|
return AddressBook.$q.when(AddressBook.$cards);
|
|
|
|
}
|
|
|
|
if (angular.isUndefined(AddressBook.$cards)) {
|
|
|
|
// First session query
|
|
|
|
AddressBook.$cards = [];
|
|
|
|
}
|
|
|
|
else if (AddressBook.$query == search) {
|
|
|
|
// Query hasn't changed
|
|
|
|
return AddressBook.$q.when(AddressBook.$cards);
|
|
|
|
}
|
|
|
|
AddressBook.$query = search;
|
|
|
|
|
2014-12-12 19:39:02 +01:00
|
|
|
angular.extend(params, options);
|
2015-08-05 22:44:25 +02:00
|
|
|
|
2014-12-12 19:39:02 +01:00
|
|
|
return AddressBook.$$resource.fetch(null, 'allContactSearch', params).then(function(response) {
|
2015-08-05 22:44:25 +02:00
|
|
|
var results, card, index,
|
|
|
|
compareIds = function(data) {
|
|
|
|
return this.id == data.id;
|
|
|
|
};
|
|
|
|
if (excludedCards) {
|
|
|
|
// Remove excluded cards from results
|
|
|
|
results = _.filter(response.contacts, function(data) {
|
|
|
|
return _.isUndefined(_.find(excludedCards, compareIds, data));
|
|
|
|
});
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
results = response.contacts;
|
|
|
|
}
|
|
|
|
// Remove cards that no longer match the search query
|
|
|
|
for (index = AddressBook.$cards.length - 1; index >= 0; index--) {
|
|
|
|
card = AddressBook.$cards[index];
|
|
|
|
if (_.isUndefined(_.find(results, compareIds, card))) {
|
|
|
|
AddressBook.$cards.splice(index, 1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Add new cards matching the search query
|
|
|
|
_.each(results, function(data, index) {
|
|
|
|
if (_.isUndefined(_.find(AddressBook.$cards, compareIds, data))) {
|
|
|
|
var card = new AddressBook.$Card(data, search);
|
|
|
|
AddressBook.$cards.splice(index, 0, card);
|
|
|
|
}
|
2014-12-12 19:39:02 +01:00
|
|
|
});
|
2015-08-05 22:44:25 +02:00
|
|
|
return AddressBook.$cards;
|
2014-12-12 19:39:02 +01:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2014-09-23 17:52:47 +02:00
|
|
|
/**
|
|
|
|
* @memberof AddressBook
|
|
|
|
* @desc Add a new addressbook to the static list of addressbooks
|
|
|
|
* @param {AddressBook} addressbook - an Addressbook object instance
|
|
|
|
*/
|
|
|
|
AddressBook.$add = function(addressbook) {
|
|
|
|
// Insert new addressbook at proper index
|
2015-04-24 14:45:39 +02:00
|
|
|
var list, sibling, i;
|
2014-11-03 18:19:35 +01:00
|
|
|
|
2015-04-24 14:45:39 +02:00
|
|
|
list = addressbook.isSubscription? this.$subscriptions : this.$addressbooks;
|
|
|
|
sibling = _.find(list, function(o) {
|
2015-07-24 22:14:53 +02:00
|
|
|
return (addressbook.id == 'personal' ||
|
|
|
|
(o.id != 'personal' &&
|
|
|
|
o.name.localeCompare(addressbook.name) === 1));
|
2014-09-23 17:52:47 +02:00
|
|
|
});
|
2015-04-24 14:45:39 +02:00
|
|
|
i = sibling ? _.indexOf(_.pluck(list, 'id'), sibling.id) : 1;
|
|
|
|
list.splice(i, 0, addressbook);
|
2014-09-23 17:52:47 +02:00
|
|
|
};
|
2014-09-19 02:46:52 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @memberof AddressBook
|
2015-07-31 01:47:53 +02:00
|
|
|
* @desc Set or get the list of addressbooks. Will instantiate a new AddressBook object for each item.
|
2014-09-19 02:46:52 +02:00
|
|
|
* @param {array} [data] - the metadata of the addressbooks
|
|
|
|
* @returns the list of addressbooks
|
|
|
|
*/
|
2014-09-23 17:52:47 +02:00
|
|
|
AddressBook.$findAll = function(data) {
|
2014-09-29 17:06:46 +02:00
|
|
|
var _this = this;
|
2014-09-19 02:46:52 +02:00
|
|
|
if (data) {
|
2015-04-24 14:45:39 +02:00
|
|
|
this.$addressbooks = [];
|
|
|
|
this.$subscriptions = [];
|
|
|
|
this.$remotes = [];
|
2014-09-19 02:46:52 +02:00
|
|
|
// Instanciate AddressBook objects
|
2015-04-24 14:45:39 +02:00
|
|
|
angular.forEach(data, function(o, i) {
|
|
|
|
var addressbook = new AddressBook(o);
|
|
|
|
if (addressbook.isRemote)
|
|
|
|
_this.$remotes.push(addressbook);
|
|
|
|
else if (addressbook.isSubscription)
|
|
|
|
_this.$subscriptions.push(addressbook);
|
|
|
|
else
|
|
|
|
_this.$addressbooks.push(addressbook);
|
2014-09-19 02:46:52 +02:00
|
|
|
});
|
|
|
|
}
|
2015-08-21 01:37:30 +02:00
|
|
|
return _.union(this.$addressbooks, this.$subscriptions, this.$remotes);
|
2014-09-19 02:46:52 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
2014-09-23 17:52:47 +02:00
|
|
|
* @memberOf AddressBook
|
2014-11-11 17:14:55 +01:00
|
|
|
* @desc Fetch list of cards and return an AddressBook instance.
|
2014-11-03 18:19:35 +01:00
|
|
|
* @param {string} addressbookId - the addressbook identifier
|
2014-09-23 17:52:47 +02:00
|
|
|
* @returns an AddressBook object instance
|
2014-09-19 02:46:52 +02:00
|
|
|
*/
|
2014-11-03 18:19:35 +01:00
|
|
|
AddressBook.$find = function(addressbookId) {
|
2015-08-25 20:20:20 +02:00
|
|
|
var futureAddressBookData = AddressBook.$Preferences.ready().then(function() {
|
|
|
|
return AddressBook.$$resource.fetch(addressbookId, 'view', AddressBook.$query);
|
|
|
|
});
|
2014-09-19 02:46:52 +02:00
|
|
|
return new AddressBook(futureAddressBookData);
|
|
|
|
};
|
|
|
|
|
2014-11-11 17:14:55 +01:00
|
|
|
/**
|
|
|
|
* @memberOf AddressBook
|
|
|
|
* @desc Subscribe to another user's addressbook and add it to the list of addressbooks.
|
2014-11-25 22:13:53 +01:00
|
|
|
* @param {string} uid - user id
|
|
|
|
* @param {string} path - path of folder for specified user
|
2014-11-11 17:14:55 +01:00
|
|
|
* @returns a promise of the HTTP query result
|
|
|
|
*/
|
|
|
|
AddressBook.$subscribe = function(uid, path) {
|
|
|
|
var _this = this;
|
|
|
|
return AddressBook.$$resource.userResource(uid).fetch(path, 'subscribe').then(function(addressbookData) {
|
|
|
|
var addressbook = new AddressBook(addressbookData);
|
2015-07-24 22:14:53 +02:00
|
|
|
if (_.isUndefined(_.find(_this.$subscriptions, function(o) {
|
2014-11-11 17:14:55 +01:00
|
|
|
return o.id == addressbookData.id;
|
2015-07-24 22:14:53 +02:00
|
|
|
}))) {
|
2015-04-24 14:45:39 +02:00
|
|
|
// Not already subscribed
|
2014-11-11 17:14:55 +01:00
|
|
|
AddressBook.$add(addressbook);
|
|
|
|
}
|
|
|
|
return addressbook;
|
|
|
|
});
|
|
|
|
};
|
2014-09-19 02:46:52 +02:00
|
|
|
|
2015-04-24 14:45:39 +02:00
|
|
|
/**
|
|
|
|
* @function init
|
|
|
|
* @memberof AddressBook.prototype
|
|
|
|
* @desc Extend instance with new data and compute additional attributes.
|
|
|
|
* @param {object} data - attributes of addressbook
|
|
|
|
*/
|
|
|
|
AddressBook.prototype.init = function(data) {
|
2015-08-27 20:09:43 +02:00
|
|
|
this.$isLoading = true;
|
2015-05-28 19:48:45 +02:00
|
|
|
this.$cards = [];
|
|
|
|
this.cards = [];
|
2015-04-24 14:45:39 +02:00
|
|
|
angular.extend(this, data);
|
|
|
|
// Add 'isOwned' and 'isSubscription' attributes based on active user (TODO: add it server-side?)
|
|
|
|
this.isOwned = AddressBook.activeUser.isSuperUser || this.owner == AddressBook.activeUser.login;
|
|
|
|
this.isSubscription = !this.isRemote && this.owner != AddressBook.activeUser.login;
|
|
|
|
};
|
|
|
|
|
2014-11-11 17:14:55 +01:00
|
|
|
/**
|
|
|
|
* @function $id
|
|
|
|
* @memberof AddressBook.prototype
|
|
|
|
* @desc Resolve the addressbook id.
|
|
|
|
* @returns a promise of the addressbook id
|
|
|
|
*/
|
2014-09-19 02:46:52 +02:00
|
|
|
AddressBook.prototype.$id = function() {
|
2015-05-28 19:48:45 +02:00
|
|
|
if (this.id) {
|
|
|
|
// Object already unwrapped
|
|
|
|
return AddressBook.$q.when(this.id);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
// Wait until object is unwrapped
|
|
|
|
return this.$futureAddressBookData.then(function(addressbook) {
|
|
|
|
return addressbook.id;
|
|
|
|
});
|
|
|
|
}
|
2014-09-19 02:46:52 +02:00
|
|
|
};
|
|
|
|
|
2015-05-08 22:42:46 +02:00
|
|
|
/**
|
|
|
|
* @function $selectedCount
|
|
|
|
* @memberof AddressBook.prototype
|
|
|
|
* @desc Return the number of cards selected by the user.
|
|
|
|
* @returns the number of selected cards
|
|
|
|
*/
|
|
|
|
AddressBook.prototype.$selectedCount = function() {
|
|
|
|
var count;
|
|
|
|
|
|
|
|
count = 0;
|
|
|
|
if (this.cards) {
|
2015-07-24 22:14:53 +02:00
|
|
|
count = (_.filter(this.cards, function(card) { return card.selected; })).length;
|
2015-05-08 22:42:46 +02:00
|
|
|
}
|
|
|
|
return count;
|
|
|
|
};
|
|
|
|
|
2015-07-31 01:47:53 +02:00
|
|
|
/**
|
|
|
|
* @function $startRefreshTimeout
|
|
|
|
* @memberof AddressBook.prototype
|
|
|
|
* @desc Starts the refresh timeout for the current selected address book
|
2015-07-31 15:05:56 +02:00
|
|
|
*/
|
2015-07-31 01:47:53 +02:00
|
|
|
AddressBook.prototype.$startRefreshTimeout = function() {
|
|
|
|
var _this = this;
|
|
|
|
|
|
|
|
if (AddressBook.$refreshTimeout)
|
|
|
|
AddressBook.$timeout.cancel(AddressBook.$refreshTimeout);
|
|
|
|
|
|
|
|
AddressBook.$Preferences.ready().then(function() {
|
|
|
|
// Restart the refresh timer, if needed
|
|
|
|
var refreshViewCheck = AddressBook.$Preferences.defaults.SOGoRefreshViewCheck;
|
|
|
|
if (refreshViewCheck && refreshViewCheck != 'manually') {
|
|
|
|
var f = angular.bind(_this, AddressBook.prototype.$reload);
|
|
|
|
AddressBook.$refreshTimeout = AddressBook.$timeout(f, refreshViewCheck.timeInterval()*1000);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2014-09-19 02:46:52 +02:00
|
|
|
/**
|
2015-05-28 19:48:45 +02:00
|
|
|
* @function $reload
|
|
|
|
* @memberof AddressBook.prototype
|
|
|
|
* @desc Reload list of cards
|
|
|
|
* @returns a promise of the Cards instances
|
|
|
|
*/
|
|
|
|
AddressBook.prototype.$reload = function() {
|
|
|
|
var _this = this;
|
|
|
|
|
2015-07-31 01:47:53 +02:00
|
|
|
this.$startRefreshTimeout();
|
2015-08-27 20:09:43 +02:00
|
|
|
return this.$filter();
|
2015-05-28 19:48:45 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
2014-09-19 02:46:52 +02:00
|
|
|
* @function $filter
|
|
|
|
* @memberof AddressBook.prototype
|
|
|
|
* @desc Search for cards matching some criterias
|
|
|
|
* @param {string} search - the search string to match
|
2014-12-18 22:30:59 +01:00
|
|
|
* @param {object} [options] - additional options to the query
|
2014-09-19 02:46:52 +02:00
|
|
|
* @returns a collection of Cards instances
|
|
|
|
*/
|
2015-07-16 22:26:09 +02:00
|
|
|
AddressBook.prototype.$filter = function(search, options, excludedCards) {
|
2015-07-20 20:24:36 +02:00
|
|
|
var _this = this;
|
2015-05-28 19:48:45 +02:00
|
|
|
|
2015-09-03 16:05:25 +02:00
|
|
|
if (!options || !options.dry)
|
|
|
|
this.$isLoading = true;
|
2015-08-27 20:09:43 +02:00
|
|
|
|
2015-08-14 21:22:59 +02:00
|
|
|
return AddressBook.$Preferences.ready().then(function() {
|
|
|
|
if (options) {
|
|
|
|
angular.extend(AddressBook.$query, options);
|
2014-06-19 21:47:33 +02:00
|
|
|
|
2015-08-14 21:22:59 +02:00
|
|
|
if (options.dry) {
|
|
|
|
if (!search) {
|
|
|
|
// No query specified
|
|
|
|
_this.$cards = [];
|
|
|
|
return AddressBook.$q.when(_this.$cards);
|
|
|
|
}
|
|
|
|
else if (AddressBook.$query.value == search) {
|
|
|
|
// Query hasn't changed
|
|
|
|
return AddressBook.$q.when(_this.$cards);
|
|
|
|
}
|
2015-05-27 21:06:42 +02:00
|
|
|
}
|
|
|
|
}
|
2015-07-20 20:24:36 +02:00
|
|
|
|
2015-08-27 20:09:43 +02:00
|
|
|
if (angular.isDefined(search))
|
|
|
|
AddressBook.$query.value = search;
|
2015-05-27 21:06:42 +02:00
|
|
|
|
2015-08-14 21:22:59 +02:00
|
|
|
return _this.$id().then(function(addressbookId) {
|
|
|
|
return AddressBook.$$resource.fetch(addressbookId, 'view', AddressBook.$query);
|
|
|
|
}).then(function(response) {
|
|
|
|
var results, cards, card, index,
|
|
|
|
compareIds = function(data) {
|
|
|
|
return _this.id == data.id;
|
|
|
|
};
|
|
|
|
if (options && options.dry) {
|
|
|
|
// Don't keep a copy of the resulting cards.
|
|
|
|
// This is usefull when doing autocompletion.
|
|
|
|
cards = _this.$cards;
|
2015-05-28 19:48:45 +02:00
|
|
|
}
|
2015-08-14 21:22:59 +02:00
|
|
|
else {
|
|
|
|
cards = _this.cards;
|
2015-07-15 21:20:08 +02:00
|
|
|
}
|
2015-08-14 21:22:59 +02:00
|
|
|
if (excludedCards) {
|
|
|
|
// Remove excluded cards from results
|
|
|
|
results = _.filter(response.cards, function(card) {
|
|
|
|
return _.isUndefined(_.find(excludedCards, compareIds, card));
|
|
|
|
});
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
results = response.cards;
|
2015-07-16 22:26:09 +02:00
|
|
|
}
|
2015-08-14 21:22:59 +02:00
|
|
|
// Remove cards that no longer match the search query
|
|
|
|
for (index = cards.length - 1; index >= 0; index--) {
|
|
|
|
card = cards[index];
|
|
|
|
if (_.isUndefined(_.find(results, compareIds, card))) {
|
|
|
|
cards.splice(index, 1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Add new cards matching the search query
|
|
|
|
_.each(results, function(data, index) {
|
|
|
|
if (_.isUndefined(_.find(cards, compareIds, data))) {
|
|
|
|
var card = new AddressBook.$Card(data, search);
|
|
|
|
cards.splice(index, 0, card);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
// Respect the order of the results
|
|
|
|
_.each(results, function(data, index) {
|
|
|
|
var oldIndex, removedCards;
|
|
|
|
if (cards[index].id != data.id) {
|
|
|
|
oldIndex = _.findIndex(cards, compareIds, data);
|
|
|
|
removedCards = cards.splice(oldIndex, 1);
|
|
|
|
cards.splice(index, 0, removedCards[0]);
|
|
|
|
}
|
|
|
|
});
|
2015-08-27 20:09:43 +02:00
|
|
|
_this.$isLoading = false;
|
2015-08-14 21:22:59 +02:00
|
|
|
return cards;
|
2015-07-16 22:26:09 +02:00
|
|
|
});
|
2015-05-28 19:48:45 +02:00
|
|
|
});
|
2014-09-19 02:46:52 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @function $rename
|
|
|
|
* @memberof AddressBook.prototype
|
2015-01-08 22:11:07 +01:00
|
|
|
* @desc Rename the addressbook and keep the list sorted
|
2014-09-19 02:46:52 +02:00
|
|
|
* @param {string} name - the new name
|
|
|
|
* @returns a promise of the HTTP operation
|
|
|
|
*/
|
|
|
|
AddressBook.prototype.$rename = function(name) {
|
|
|
|
var i = _.indexOf(_.pluck(AddressBook.$addressbooks, 'id'), this.id);
|
|
|
|
this.name = name;
|
|
|
|
AddressBook.$addressbooks.splice(i, 1);
|
|
|
|
AddressBook.$add(this);
|
|
|
|
return this.$save();
|
|
|
|
};
|
|
|
|
|
2014-11-25 22:13:53 +01:00
|
|
|
/**
|
|
|
|
* @function $delete
|
|
|
|
* @memberof AddressBook.prototype
|
|
|
|
* @desc Delete the addressbook from the server and the static list of addressbooks.
|
|
|
|
* @returns a promise of the HTTP operation
|
|
|
|
*/
|
2014-09-19 02:46:52 +02:00
|
|
|
AddressBook.prototype.$delete = function() {
|
2014-11-03 18:19:35 +01:00
|
|
|
var _this = this,
|
2014-11-11 17:14:55 +01:00
|
|
|
d = AddressBook.$q.defer(),
|
2015-04-24 14:45:39 +02:00
|
|
|
list,
|
2014-11-11 17:14:55 +01:00
|
|
|
promise;
|
|
|
|
|
2015-04-24 14:45:39 +02:00
|
|
|
if (this.isSubscription) {
|
2014-11-11 17:14:55 +01:00
|
|
|
promise = AddressBook.$$resource.fetch(this.id, 'unsubscribe');
|
2015-04-24 14:45:39 +02:00
|
|
|
list = AddressBook.$subscriptions;
|
|
|
|
}
|
|
|
|
else {
|
2014-11-12 16:44:43 +01:00
|
|
|
promise = AddressBook.$$resource.remove(this.id);
|
2015-04-24 14:45:39 +02:00
|
|
|
list = AddressBook.$addressbooks;
|
|
|
|
}
|
2014-11-11 17:14:55 +01:00
|
|
|
|
|
|
|
promise.then(function() {
|
2015-04-24 14:45:39 +02:00
|
|
|
var i = _.indexOf(_.pluck(list, 'id'), _this.id);
|
|
|
|
list.splice(i, 1);
|
|
|
|
d.resolve();
|
2014-11-11 17:14:55 +01:00
|
|
|
}, function(data, status) {
|
|
|
|
d.reject(data);
|
|
|
|
});
|
2014-09-19 02:46:52 +02:00
|
|
|
return d.promise;
|
|
|
|
};
|
|
|
|
|
2015-05-08 21:47:09 +02:00
|
|
|
/**
|
|
|
|
* @function $deleteCards
|
|
|
|
* @memberof AddressBook.prototype
|
|
|
|
* @desc Delete multiple cards from addressbook.
|
|
|
|
* @return a promise of the HTTP operation
|
|
|
|
*/
|
|
|
|
AddressBook.prototype.$deleteCards = function(cards) {
|
|
|
|
|
2015-07-24 22:14:53 +02:00
|
|
|
var uids = _.map(cards, function(card) { return card.id; });
|
2015-05-08 21:47:09 +02:00
|
|
|
var _this = this;
|
|
|
|
|
|
|
|
return AddressBook.$$resource.post(this.id, 'batchDelete', {uids: uids}).then(function() {
|
|
|
|
_this.cards = _.difference(_this.cards, cards);
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2015-08-10 15:48:47 +02:00
|
|
|
/**
|
|
|
|
* @function $copyCards
|
|
|
|
* @memberof AddressBook.prototype
|
|
|
|
* @desc Copy multiple cards from addressbook to an other one.
|
|
|
|
* @return a promise of the HTTP operation
|
|
|
|
*/
|
|
|
|
AddressBook.prototype.$copyCards = function(cards, folder) {
|
|
|
|
var uids = _.map(cards, function(card) { return card.id; });
|
|
|
|
return AddressBook.$$resource.post(this.id, 'copy', {uids: uids, folder: folder});
|
|
|
|
};
|
|
|
|
|
2014-11-25 22:13:53 +01:00
|
|
|
/**
|
|
|
|
* @function $save
|
|
|
|
* @memberof AddressBook.prototype
|
|
|
|
* @desc Save the addressbook to the server. This currently can only affect the name of the addressbook.
|
|
|
|
* @returns a promise of the HTTP operation
|
|
|
|
*/
|
2014-09-19 02:46:52 +02:00
|
|
|
AddressBook.prototype.$save = function() {
|
2014-11-03 18:19:35 +01:00
|
|
|
return AddressBook.$$resource.save(this.id, this.$omit()).then(function(data) {
|
2014-09-19 02:46:52 +02:00
|
|
|
return data;
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2014-11-25 22:13:53 +01:00
|
|
|
/**
|
|
|
|
* @function $getCard
|
|
|
|
* @memberof AddressBook.prototype
|
|
|
|
* @desc Fetch the card attributes from the server.
|
|
|
|
* @returns a promise of the HTTP operation
|
|
|
|
*/
|
2014-11-03 18:19:35 +01:00
|
|
|
AddressBook.prototype.$getCard = function(cardId) {
|
2015-08-27 21:55:56 +02:00
|
|
|
var _this = this;
|
|
|
|
|
2014-11-03 18:19:35 +01:00
|
|
|
return this.$id().then(function(addressbookId) {
|
2015-08-27 21:55:56 +02:00
|
|
|
var fullCard,
|
|
|
|
cachedCard = _.find(_this.cards, function(data) {
|
|
|
|
return cardId == data.id;
|
|
|
|
});
|
|
|
|
|
|
|
|
if (cachedCard && cachedCard.$futureCardData)
|
|
|
|
// Full card is available
|
|
|
|
return cachedCard;
|
|
|
|
|
|
|
|
fullCard = AddressBook.$Card.$find(addressbookId, cardId);
|
|
|
|
fullCard.$id().then(function(cardId) {
|
|
|
|
// Extend the Card object of the addressbook list with the full card description
|
|
|
|
if (cachedCard)
|
|
|
|
angular.extend(cachedCard, fullCard);
|
|
|
|
});
|
|
|
|
return fullCard;
|
2014-09-19 02:46:52 +02:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2014-11-25 22:13:53 +01:00
|
|
|
/**
|
|
|
|
* @function $unwrap
|
|
|
|
* @memberof AddressBook.prototype
|
|
|
|
* @desc Unwrap a promise and instanciate new Card objects using received data.
|
|
|
|
* @param {promise} futureAddressBookData - a promise of the AddressBook's data
|
|
|
|
*/
|
2014-09-19 02:46:52 +02:00
|
|
|
AddressBook.prototype.$unwrap = function(futureAddressBookData) {
|
2014-09-29 17:06:46 +02:00
|
|
|
var _this = this;
|
2014-09-19 02:46:52 +02:00
|
|
|
|
2015-07-20 21:32:57 +02:00
|
|
|
// Expose and sesolve the promise
|
|
|
|
this.$futureAddressBookData = futureAddressBookData.then(function(data) {
|
2015-05-28 19:48:45 +02:00
|
|
|
return AddressBook.$timeout(function() {
|
|
|
|
// Extend AddressBook instance from data of addressbooks list.
|
2014-09-19 02:46:52 +02:00
|
|
|
// Will inherit attributes such as isEditable and isRemote.
|
2014-09-23 17:52:47 +02:00
|
|
|
angular.forEach(AddressBook.$findAll(), function(o, i) {
|
2015-05-28 19:48:45 +02:00
|
|
|
if (o.id == data.id) {
|
2014-09-29 17:06:46 +02:00
|
|
|
angular.extend(_this, o);
|
2014-09-19 02:46:52 +02:00
|
|
|
}
|
2014-06-19 21:47:33 +02:00
|
|
|
});
|
2015-05-28 19:48:45 +02:00
|
|
|
// Extend AddressBook instance with received data
|
|
|
|
_this.init(data);
|
2014-09-19 02:46:52 +02:00
|
|
|
// Instanciate Card objects
|
2014-09-29 17:06:46 +02:00
|
|
|
angular.forEach(_this.cards, function(o, i) {
|
|
|
|
_this.cards[i] = new AddressBook.$Card(o);
|
2014-06-19 21:47:33 +02:00
|
|
|
});
|
2014-10-17 17:01:56 +02:00
|
|
|
// Instanciate Acl object
|
2014-12-12 19:39:02 +01:00
|
|
|
_this.$acl = new AddressBook.$$Acl('Contacts/' + _this.id);
|
2015-07-31 01:47:53 +02:00
|
|
|
|
|
|
|
_this.$startRefreshTimeout();
|
|
|
|
|
2015-08-27 20:09:43 +02:00
|
|
|
_this.$isLoading = false;
|
|
|
|
|
2015-05-28 19:48:45 +02:00
|
|
|
return _this;
|
2014-09-19 02:46:52 +02:00
|
|
|
});
|
|
|
|
}, function(data) {
|
2015-01-08 22:11:07 +01:00
|
|
|
_this.isError = true;
|
|
|
|
if (angular.isObject(data)) {
|
|
|
|
AddressBook.$timeout(function() {
|
|
|
|
angular.extend(_this, data);
|
|
|
|
});
|
|
|
|
}
|
2014-09-19 02:46:52 +02:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2014-11-25 22:13:53 +01:00
|
|
|
/**
|
|
|
|
* @function $omit
|
|
|
|
* @memberof AddressBook.prototype
|
|
|
|
* @desc Return a sanitized object used to send to the server.
|
|
|
|
* @return an object literal copy of the Addressbook instance
|
|
|
|
*/
|
2014-09-19 02:46:52 +02:00
|
|
|
AddressBook.prototype.$omit = function() {
|
|
|
|
var addressbook = {};
|
|
|
|
angular.forEach(this, function(value, key) {
|
|
|
|
if (key != 'constructor' &&
|
|
|
|
key != 'cards' &&
|
|
|
|
key[0] != '$') {
|
|
|
|
addressbook[key] = value;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
return addressbook;
|
|
|
|
};
|
2014-06-19 21:47:33 +02:00
|
|
|
})();
|