2014-06-19 21:47:33 +02:00
|
|
|
(function() {
|
|
|
|
'use strict';
|
|
|
|
|
2014-09-10 22:20:52 +02:00
|
|
|
/**
|
|
|
|
* @name AddressBook
|
|
|
|
* @constructor
|
|
|
|
* @param {object} futureAddressBookData
|
|
|
|
*/
|
2014-06-19 21:47:33 +02:00
|
|
|
function AddressBook(futureAddressBookData) {
|
2014-08-06 21:14:05 +02:00
|
|
|
// Data is immediately available
|
2014-06-19 21:47:33 +02:00
|
|
|
if (typeof futureAddressBookData.then !== 'function') {
|
|
|
|
angular.extend(this, futureAddressBookData);
|
2014-09-10 22:20: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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
// The promise will be unwrapped first
|
|
|
|
this.$unwrap(futureAddressBookData);
|
2014-06-19 21:47:33 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-08-06 21:14:05 +02:00
|
|
|
/* The factory we'll use to register with Angular */
|
2014-09-10 22:20:52 +02:00
|
|
|
AddressBook.$factory = ['$q', '$timeout', 'sgSettings', 'sgResource', 'sgCard', function($q, $timeout, Settings, Resource, Card) {
|
2014-06-19 21:47:33 +02:00
|
|
|
angular.extend(AddressBook, {
|
2014-09-10 22:20:52 +02:00
|
|
|
$q: $q,
|
2014-06-19 21:47:33 +02:00
|
|
|
$timeout: $timeout,
|
2014-09-10 22:20:52 +02:00
|
|
|
$$resource: new Resource(Settings.baseURL),
|
2014-08-06 21:14:05 +02:00
|
|
|
$Card: Card
|
2014-06-19 21:47:33 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
return AddressBook; // return constructor
|
|
|
|
}];
|
|
|
|
|
2014-08-06 21:14:05 +02:00
|
|
|
/* Factory registration in Angular module */
|
2014-09-05 21:12:05 +02:00
|
|
|
angular.module('SOGo.ContactsUI').factory('sgAddressBook', AddressBook.$factory);
|
2014-06-19 21:47:33 +02:00
|
|
|
|
2014-09-10 22:20: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-08-06 21:14:05 +02:00
|
|
|
AddressBook.$all = function(data) {
|
2014-09-10 22:20:52 +02:00
|
|
|
var self = this;
|
2014-08-06 21:14:05 +02:00
|
|
|
if (data) {
|
|
|
|
this.$addressbooks = data;
|
2014-09-10 22:20:52 +02:00
|
|
|
// Instanciate AddressBook objects
|
|
|
|
angular.forEach(this.$addressbooks, function(o, i) {
|
|
|
|
self.$addressbooks[i] = new AddressBook(o);
|
|
|
|
});
|
2014-08-06 21:14:05 +02:00
|
|
|
}
|
|
|
|
return this.$addressbooks;
|
|
|
|
};
|
2014-06-19 21:47:33 +02:00
|
|
|
|
2014-09-10 22:20:52 +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
|
|
|
|
var sibling = _.find(this.$addressbooks, function(o) {
|
|
|
|
return (o.isRemote || (o.id != 'personal' && o.name.localeCompare(addressbook.name) === 1));
|
|
|
|
});
|
|
|
|
var i = sibling? _.indexOf(_.pluck(this.$addressbooks, 'id'), sibling.id) : 1;
|
|
|
|
this.$addressbooks.splice(i, 0, addressbook);
|
|
|
|
};
|
|
|
|
|
2014-08-20 21:49:44 +02:00
|
|
|
/* Fetch list of cards and return an AddressBook instance */
|
2014-06-19 21:47:33 +02:00
|
|
|
AddressBook.$find = function(addressbook_id) {
|
2014-08-20 21:49:44 +02:00
|
|
|
var futureAddressBookData = AddressBook.$$resource.find(addressbook_id);
|
|
|
|
|
2014-06-19 21:47:33 +02:00
|
|
|
return new AddressBook(futureAddressBookData);
|
|
|
|
};
|
|
|
|
|
2014-08-06 21:14:05 +02:00
|
|
|
/* Instance methods */
|
2014-06-19 21:47:33 +02:00
|
|
|
|
|
|
|
AddressBook.prototype.$id = function() {
|
|
|
|
return this.$futureAddressBookData.then(function(data) {
|
|
|
|
return data.id;
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2014-09-05 21:12:05 +02:00
|
|
|
/**
|
2014-09-10 22:20:52 +02:00
|
|
|
* @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
|
2014-09-05 21:12:05 +02:00
|
|
|
*/
|
|
|
|
AddressBook.prototype.$filter = function(search, options) {
|
2014-08-06 21:14:05 +02:00
|
|
|
var self = this;
|
|
|
|
var params = { 'search': 'name_or_address',
|
|
|
|
'value': search,
|
|
|
|
'sort': 'c_cn',
|
|
|
|
'asc': 'true' };
|
2014-09-05 21:12:05 +02:00
|
|
|
if (options && options.excludeLists) {
|
|
|
|
params.excludeLists = true;
|
|
|
|
}
|
2014-08-06 21:14:05 +02:00
|
|
|
|
|
|
|
return this.$id().then(function(addressbook_id) {
|
|
|
|
var futureAddressBookData = AddressBook.$$resource.filter(addressbook_id, params);
|
|
|
|
return futureAddressBookData.then(function(data) {
|
2014-09-05 21:12:05 +02:00
|
|
|
var cards;
|
|
|
|
if (options && options.dry) {
|
2014-09-10 22:20:52 +02:00
|
|
|
// Don't keep a copy of the resulting cards.
|
|
|
|
// This is usefull when doing autocompletion.
|
2014-09-05 21:12:05 +02:00
|
|
|
cards = data.cards;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
self.cards = data.cards;
|
|
|
|
cards = self.cards;
|
|
|
|
}
|
2014-08-20 21:49:44 +02:00
|
|
|
// Instanciate Card objects
|
2014-09-05 21:12:05 +02:00
|
|
|
angular.forEach(cards, function(o, i) {
|
|
|
|
cards[i] = new AddressBook.$Card(o);
|
2014-08-06 21:14:05 +02:00
|
|
|
});
|
2014-09-05 21:12:05 +02:00
|
|
|
return cards;
|
2014-08-06 21:14:05 +02:00
|
|
|
});
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2014-09-10 22:20:52 +02:00
|
|
|
/**
|
|
|
|
* @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-08-20 21:49:44 +02:00
|
|
|
AddressBook.prototype.$delete = function() {
|
2014-09-10 22:20:52 +02:00
|
|
|
var self = this;
|
|
|
|
var d = AddressBook.$q.defer();
|
|
|
|
AddressBook.$$resource.remove(this.id)
|
|
|
|
.then(function() {
|
|
|
|
var i = _.indexOf(_.pluck(AddressBook.$addressbooks, 'id'), self.id);
|
|
|
|
AddressBook.$addressbooks.splice(i, 1);
|
|
|
|
d.resolve(true);
|
|
|
|
}, function(data, status) {
|
|
|
|
d.reject(data);
|
|
|
|
});
|
|
|
|
return d.promise;
|
2014-08-20 21:49:44 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
AddressBook.prototype.$save = function() {
|
2014-09-10 22:20:52 +02:00
|
|
|
return AddressBook.$$resource.save(this.id, this.$omit()).then(function (data) {
|
2014-08-20 21:49:44 +02:00
|
|
|
return data;
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2014-08-06 21:14:05 +02:00
|
|
|
AddressBook.prototype.$getCard = function(card_id) {
|
2014-06-19 21:47:33 +02:00
|
|
|
var self = this;
|
|
|
|
return this.$id().then(function(addressbook_id) {
|
2014-08-20 21:49:44 +02:00
|
|
|
self.card = AddressBook.$Card.$find(addressbook_id, card_id);
|
|
|
|
return self.card;
|
2014-06-19 21:47:33 +02:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2014-08-06 21:14:05 +02:00
|
|
|
AddressBook.prototype.$resetCard = function() {
|
|
|
|
this.$getCard(this.card.id);
|
2014-06-19 21:47:33 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
// Unwrap a promise
|
|
|
|
AddressBook.prototype.$unwrap = function(futureAddressBookData) {
|
|
|
|
var self = this;
|
|
|
|
|
|
|
|
this.$futureAddressBookData = futureAddressBookData;
|
|
|
|
this.$futureAddressBookData.then(function(data) {
|
2014-08-06 21:14:05 +02:00
|
|
|
AddressBook.$timeout(function() {
|
|
|
|
angular.extend(self, data);
|
2014-08-20 21:49:44 +02:00
|
|
|
// Also extend AddressBook instance from data of addressbooks list.
|
|
|
|
// Will inherit attributes such as isEditable and isRemote.
|
|
|
|
angular.forEach(AddressBook.$all(), function(o, i) {
|
|
|
|
if (o.id == self.id) {
|
|
|
|
angular.extend(self, o);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
// Instanciate Card objects
|
|
|
|
angular.forEach(self.cards, function(o, i) {
|
|
|
|
self.cards[i] = new AddressBook.$Card(o);
|
2014-08-06 21:14:05 +02:00
|
|
|
});
|
|
|
|
});
|
2014-09-10 22:20:52 +02:00
|
|
|
}, function(data) {
|
|
|
|
angular.extend(self, data);
|
|
|
|
self.isError = true;
|
2014-06-19 21:47:33 +02:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
// $omit returns a sanitized object used to send to the server
|
|
|
|
AddressBook.prototype.$omit = function() {
|
|
|
|
var addressbook = {};
|
|
|
|
angular.forEach(this, function(value, key) {
|
|
|
|
if (key != 'constructor' &&
|
2014-08-06 21:14:05 +02:00
|
|
|
key != 'card' &&
|
|
|
|
key != 'cards' &&
|
2014-06-19 21:47:33 +02:00
|
|
|
key[0] != '$') {
|
|
|
|
addressbook[key] = value;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
return addressbook;
|
|
|
|
};
|
|
|
|
})();
|