2014-06-19 21:47:33 +02:00
|
|
|
(function() {
|
|
|
|
'use strict';
|
|
|
|
|
2014-08-06 21:14:05 +02:00
|
|
|
/* Constructor */
|
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);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-08-06 21:14:05 +02:00
|
|
|
// The promise will be unwrapped first
|
2014-06-19 21:47:33 +02:00
|
|
|
this.$unwrap(futureAddressBookData);
|
|
|
|
}
|
|
|
|
|
2014-08-06 21:14:05 +02:00
|
|
|
/* The factory we'll use to register with Angular */
|
|
|
|
AddressBook.$factory = ['$timeout', 'sgSettings', 'sgResource', 'sgCard', function($timeout, Settings, Resource, Card) {
|
2014-06-19 21:47:33 +02:00
|
|
|
angular.extend(AddressBook, {
|
|
|
|
$$resource: new Resource(Settings.baseURL),
|
|
|
|
$timeout: $timeout,
|
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 */
|
|
|
|
angular.module('SOGo.Contacts').factory('sgAddressBook', AddressBook.$factory);
|
2014-06-19 21:47:33 +02:00
|
|
|
|
2014-08-20 21:49:44 +02:00
|
|
|
/* Set or get the list of addressbooks */
|
2014-08-06 21:14:05 +02:00
|
|
|
AddressBook.$all = function(data) {
|
|
|
|
if (data) {
|
|
|
|
this.$addressbooks = data;
|
|
|
|
}
|
|
|
|
return this.$addressbooks;
|
|
|
|
};
|
2014-06-19 21:47:33 +02:00
|
|
|
|
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-08-06 21:14:05 +02:00
|
|
|
AddressBook.prototype.$filter = function(search) {
|
|
|
|
var self = this;
|
|
|
|
var params = { 'search': 'name_or_address',
|
|
|
|
'value': search,
|
|
|
|
'sort': 'c_cn',
|
|
|
|
'asc': 'true' };
|
|
|
|
|
|
|
|
return this.$id().then(function(addressbook_id) {
|
|
|
|
var futureAddressBookData = AddressBook.$$resource.filter(addressbook_id, params);
|
|
|
|
return futureAddressBookData.then(function(data) {
|
2014-08-20 21:49:44 +02:00
|
|
|
self.cards = data.cards;
|
|
|
|
// 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-08-20 21:49:44 +02:00
|
|
|
return self.cards;
|
2014-08-06 21:14:05 +02:00
|
|
|
});
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2014-08-20 21:49:44 +02:00
|
|
|
AddressBook.prototype.$delete = function() {
|
|
|
|
return AddressBook.$$resource.remove(this.id);
|
|
|
|
};
|
|
|
|
|
|
|
|
AddressBook.prototype.$save = function() {
|
|
|
|
return AddressBook.$$resource.set(this.id, this.$omit()).then(function (data) {
|
|
|
|
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);
|
|
|
|
self.card.$id().catch(function() {
|
|
|
|
// Card not found
|
|
|
|
self.card = null;
|
2014-06-19 21:47:33 +02:00
|
|
|
});
|
2014-08-20 21:49:44 +02:00
|
|
|
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
|
|
|
};
|
|
|
|
|
|
|
|
AddressBook.prototype.$viewerIsActive = function(editMode) {
|
2014-08-20 21:49:44 +02:00
|
|
|
return (this.card != null && !editMode);
|
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-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;
|
|
|
|
};
|
|
|
|
})();
|