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') {
|
|
|
|
angular.extend(this, futureAddressBookData);
|
|
|
|
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
|
|
|
|
*/
|
2014-10-17 17:01:56 +02:00
|
|
|
AddressBook.$factory = ['$q', '$timeout', 'sgSettings', 'sgResource', 'sgCard', 'sgAcl', function($q, $timeout, Settings, Resource, Card, Acl) {
|
2014-09-19 02:46:52 +02:00
|
|
|
angular.extend(AddressBook, {
|
|
|
|
$q: $q,
|
|
|
|
$timeout: $timeout,
|
2014-12-12 19:39:02 +01: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,
|
|
|
|
activeUser: Settings.activeUser
|
2014-09-19 02:46:52 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
return AddressBook; // return constructor
|
|
|
|
}];
|
|
|
|
|
|
|
|
/* Factory registration in Angular module */
|
|
|
|
angular.module('SOGo.ContactsUI')
|
2014-09-23 17:52:47 +02:00
|
|
|
.factory('sgAddressBook', AddressBook.$factory);
|
|
|
|
|
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
|
|
|
|
* @param {hash} [options] - additional options to the query (excludeGroups and excludeLists)
|
|
|
|
* @returns a collection of Cards instances
|
|
|
|
*/
|
|
|
|
AddressBook.$filterAll = function(search, options) {
|
|
|
|
var params = {search: search};
|
|
|
|
|
|
|
|
angular.extend(params, options);
|
|
|
|
return AddressBook.$$resource.fetch(null, 'allContactSearch', params).then(function(response) {
|
|
|
|
var results = [];
|
|
|
|
angular.forEach(response.contacts, function(data) {
|
|
|
|
var card = new AddressBook.$Card(data);
|
|
|
|
results.push(card);
|
|
|
|
});
|
|
|
|
return results;
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
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
|
2014-11-03 18:19:35 +01:00
|
|
|
var sibling, i;
|
|
|
|
|
2014-11-11 17:14:55 +01:00
|
|
|
addressbook.isOwned = this.activeUser.isSuperUser || addressbook.owner == this.activeUser.login;
|
2014-11-12 16:44:43 +01:00
|
|
|
addressbook.isSubscription = addressbook.owner != this.activeUser.login;
|
2014-11-03 18:19:35 +01:00
|
|
|
sibling = _.find(this.$addressbooks, function(o) {
|
2014-11-11 17:14:55 +01:00
|
|
|
return (o.isRemote || (o.id != 'personal'
|
2014-11-12 16:44:43 +01:00
|
|
|
&& o.isSubscription === addressbook.isSubscription
|
2014-11-11 17:14:55 +01:00
|
|
|
&& o.name.localeCompare(addressbook.name) === 1));
|
2014-09-23 17:52:47 +02:00
|
|
|
});
|
2014-11-03 18:19:35 +01:00
|
|
|
i = sibling ? _.indexOf(_.pluck(this.$addressbooks, 'id'), sibling.id) : 1;
|
2014-09-23 17:52:47 +02:00
|
|
|
this.$addressbooks.splice(i, 0, addressbook);
|
|
|
|
};
|
2014-09-19 02:46:52 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @memberof AddressBook
|
|
|
|
* @desc Set or get the list of addressbooks. Will instanciate a new AddressBook object for each item.
|
|
|
|
* @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) {
|
|
|
|
this.$addressbooks = data;
|
|
|
|
// Instanciate AddressBook objects
|
|
|
|
angular.forEach(this.$addressbooks, function(o, i) {
|
2014-09-29 17:06:46 +02:00
|
|
|
_this.$addressbooks[i] = new AddressBook(o);
|
2014-11-12 16:44:43 +01:00
|
|
|
// Add 'isOwned' and 'isSubscription' attributes based on active user (TODO: add it server-side?)
|
|
|
|
_this.$addressbooks[i].isSubscription = _this.$addressbooks[i].owner != _this.activeUser.login;
|
2014-11-03 21:58:54 +01:00
|
|
|
_this.$addressbooks[i].isOwned = _this.activeUser.isSuperUser
|
|
|
|
|| _this.$addressbooks[i].owner == _this.activeUser.login;
|
2014-09-19 02:46:52 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
return this.$addressbooks;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
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) {
|
|
|
|
var futureAddressBookData = AddressBook.$$resource.fetch(addressbookId, 'view');
|
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);
|
|
|
|
if (!_.find(_this.$addressbooks, function(o) {
|
|
|
|
return o.id == addressbookData.id;
|
|
|
|
})) {
|
|
|
|
AddressBook.$add(addressbook);
|
|
|
|
}
|
|
|
|
return addressbook;
|
|
|
|
});
|
|
|
|
};
|
2014-09-19 02:46:52 +02:00
|
|
|
|
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() {
|
|
|
|
return this.$futureAddressBookData.then(function(data) {
|
|
|
|
return data.id;
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @function $filter
|
|
|
|
* @memberof AddressBook.prototype
|
|
|
|
* @desc Search for cards matching some criterias
|
|
|
|
* @param {string} search - the search string to match
|
|
|
|
* @param {hash} [options] - additional options to the query
|
|
|
|
* @returns a collection of Cards instances
|
|
|
|
*/
|
|
|
|
AddressBook.prototype.$filter = function(search, options) {
|
2014-11-03 18:19:35 +01:00
|
|
|
var _this = this,
|
|
|
|
params = {
|
|
|
|
search: 'name_or_address',
|
|
|
|
value: search,
|
|
|
|
sort: 'c_cn',
|
|
|
|
asc: 'true'
|
|
|
|
};
|
2014-09-19 02:46:52 +02:00
|
|
|
if (options && options.excludeLists) {
|
|
|
|
params.excludeLists = true;
|
2014-06-19 21:47:33 +02:00
|
|
|
}
|
|
|
|
|
2014-11-03 18:19:35 +01:00
|
|
|
return this.$id().then(function(addressbookId) {
|
|
|
|
var futureAddressBookData = AddressBook.$$resource.fetch(addressbookId, 'view', params);
|
2014-09-19 02:46:52 +02:00
|
|
|
return futureAddressBookData.then(function(data) {
|
|
|
|
var cards;
|
|
|
|
if (options && options.dry) {
|
|
|
|
// Don't keep a copy of the resulting cards.
|
|
|
|
// This is usefull when doing autocompletion.
|
|
|
|
cards = data.cards;
|
2014-08-06 21:14:05 +02:00
|
|
|
}
|
2014-09-19 02:46:52 +02:00
|
|
|
else {
|
2014-09-29 17:06:46 +02:00
|
|
|
_this.cards = data.cards;
|
|
|
|
cards = _this.cards;
|
2014-09-05 21:12:05 +02:00
|
|
|
}
|
2014-09-19 02:46:52 +02:00
|
|
|
// Instanciate Card objects
|
|
|
|
angular.forEach(cards, function(o, i) {
|
|
|
|
cards[i] = new AddressBook.$Card(o);
|
2014-08-06 21:14:05 +02:00
|
|
|
});
|
2014-10-17 17:01:56 +02:00
|
|
|
|
2014-09-19 02:46:52 +02:00
|
|
|
return cards;
|
|
|
|
});
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @function $rename
|
|
|
|
* @memberof AddressBook.prototype
|
|
|
|
* @desc Rename the addressbook
|
|
|
|
* @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(),
|
|
|
|
promise;
|
|
|
|
|
2014-11-12 16:44:43 +01:00
|
|
|
if (this.isSubscription)
|
2014-11-11 17:14:55 +01:00
|
|
|
promise = AddressBook.$$resource.fetch(this.id, 'unsubscribe');
|
2014-11-12 16:44:43 +01:00
|
|
|
else
|
|
|
|
promise = AddressBook.$$resource.remove(this.id);
|
2014-11-11 17:14:55 +01:00
|
|
|
|
|
|
|
promise.then(function() {
|
|
|
|
var i = _.indexOf(_.pluck(AddressBook.$addressbooks, 'id'), _this.id);
|
|
|
|
AddressBook.$addressbooks.splice(i, 1);
|
|
|
|
d.resolve(true);
|
|
|
|
}, function(data, status) {
|
|
|
|
d.reject(data);
|
|
|
|
});
|
2014-09-19 02:46:52 +02:00
|
|
|
return d.promise;
|
|
|
|
};
|
|
|
|
|
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) {
|
|
|
|
return this.$id().then(function(addressbookId) {
|
|
|
|
return AddressBook.$Card.$find(addressbookId, cardId);
|
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
|
|
|
|
2014-09-29 17:06:46 +02:00
|
|
|
// Expose the promise
|
2014-09-19 02:46:52 +02:00
|
|
|
this.$futureAddressBookData = futureAddressBookData;
|
2014-09-29 17:06:46 +02:00
|
|
|
// Resolve the promise
|
2014-09-19 02:46:52 +02:00
|
|
|
this.$futureAddressBookData.then(function(data) {
|
|
|
|
AddressBook.$timeout(function() {
|
2014-09-29 17:06:46 +02:00
|
|
|
angular.extend(_this, data);
|
2014-09-19 02:46:52 +02:00
|
|
|
// Also extend AddressBook instance from data of addressbooks list.
|
|
|
|
// Will inherit attributes such as isEditable and isRemote.
|
2014-09-23 17:52:47 +02:00
|
|
|
angular.forEach(AddressBook.$findAll(), function(o, i) {
|
2014-09-29 17:06:46 +02:00
|
|
|
if (o.id == _this.id) {
|
|
|
|
angular.extend(_this, o);
|
2014-09-19 02:46:52 +02:00
|
|
|
}
|
2014-06-19 21:47:33 +02:00
|
|
|
});
|
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);
|
2014-09-19 02:46:52 +02:00
|
|
|
});
|
|
|
|
}, function(data) {
|
2014-09-29 17:06:46 +02:00
|
|
|
AddressBook.$timeout(function() {
|
|
|
|
angular.extend(_this, data);
|
|
|
|
_this.isError = true;
|
|
|
|
});
|
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
|
|
|
})();
|