Contacts: Add possibility to add/delete objects

This commit is contained in:
Francis Lachapelle
2015-06-11 15:25:31 -04:00
parent 0c720bb902
commit 958f0b4958
6 changed files with 277 additions and 154 deletions
@@ -27,6 +27,7 @@
/* Factory registration in Angular module */
angular.module('SOGo.Contacts').factory('sgAddressBook', AddressBook.$factory);
/* Set or get the list of addressbooks */
AddressBook.$all = function(data) {
if (data) {
this.$addressbooks = data;
@@ -34,10 +35,10 @@
return this.$addressbooks;
};
/* Fetch list of cards */
/* Fetch list of cards and return an AddressBook instance */
AddressBook.$find = function(addressbook_id) {
var futureAddressBookData = AddressBook.$$resource.find(addressbook_id); // should return attributes of addressbook +
// AddressBook.$selectedId(addressbook_id);
var futureAddressBookData = AddressBook.$$resource.find(addressbook_id);
return new AddressBook(futureAddressBookData);
};
@@ -59,32 +60,35 @@
return this.$id().then(function(addressbook_id) {
var futureAddressBookData = AddressBook.$$resource.filter(addressbook_id, params);
return futureAddressBookData.then(function(data) {
return AddressBook.$timeout(function() {
self.cards = data.cards;
return self.cards;
self.cards = data.cards;
// Instanciate Card objects
angular.forEach(self.cards, function(o, i) {
self.cards[i] = new AddressBook.$Card(o);
});
return self.cards;
});
});
};
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;
});
};
AddressBook.prototype.$getCard = function(card_id) {
var self = this;
return this.$id().then(function(addressbook_id) {
var card = AddressBook.$Card.$find(addressbook_id, card_id);
AddressBook.$timeout(function() {
self.card = card;
});
return card.$futureCardData.then(function() {
angular.forEach(self.cards, function(o, i) {
if (o.c_name == card_id) {
angular.extend(self.card, o);
}
});
return self.card;
self.card = AddressBook.$Card.$find(addressbook_id, card_id);
self.card.$id().catch(function() {
// Card not found
self.card = null;
});
return self.card;
});
};
@@ -93,7 +97,7 @@
};
AddressBook.prototype.$viewerIsActive = function(editMode) {
return (!angular.isUndefined(this.card) && !editMode);
return (this.card != null && !editMode);
};
// Unwrap a promise
@@ -104,12 +108,16 @@
this.$futureAddressBookData.then(function(data) {
AddressBook.$timeout(function() {
angular.extend(self, data);
self.$id().then(function(addressbook_id) {
angular.forEach(AddressBook.$all(), function(o, i) {
if (o.id == addressbook_id) {
angular.extend(self, o);
}
});
// 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);
});
});
});
@@ -128,10 +136,4 @@
});
return addressbook;
};
AddressBook.prototype.$save = function() {
return AddressBook.$$resource.set(this.id, this.$omit()).then(function (data) {
return data;
});
};
})();
+84 -54
View File
@@ -2,16 +2,21 @@
'use strict';
/* Constructor */
function Card(futureCardData) {
/* DATA IS IMMEDIATELY AVAILABLE */
// if (!futureCardData.inspect) {
// angular.extend(this, futureCardData);
// return;
// }
// Data is immediately available
if (typeof futureCardData.then !== 'function') {
angular.extend(this, futureCardData);
if (this.pid && !this.id) {
// Prepare for the creation of a new card;
// Get UID from the server.
var newCardData = Card.$$resource.newguid(this.pid);
this.$unwrap(newCardData);
}
return;
}
/* THE PROMISE WILL BE UNWRAPPED FIRST */
// The promise will be unwrapped first
this.$unwrap(futureCardData);
}
@@ -20,7 +25,7 @@
Card.$url_types = ['work', 'home', 'pref'];
Card.$address_types = ['work', 'home'];
/* THE FACTORY WE'LL USE TO REGISTER WITH ANGULAR */
/* The factory we'll use to register with Angular */
Card.$factory = ['$timeout', 'sgSettings', 'sgResource', function($timeout, Settings, Resource) {
angular.extend(Card, {
$$resource: new Resource(Settings.baseURL),
@@ -30,10 +35,11 @@
return Card; // return constructor
}];
/* ANGULAR MODULE REGISTRATION */
/* Factory registration in Angular module */
angular.module('SOGo.Contacts')
.factory('sgCard', Card.$factory)
// Directive to format a postal address
.directive('sgAddress', function() {
return {
restrict: 'A',
@@ -57,12 +63,8 @@
}
});
/* FETCH A UNIT */
/* Fetch a card */
Card.$find = function(addressbook_id, card_id) {
/* FALLS BACK ON AN INSTANCE OF RESOURCE */
// Fetch data over the wire.
var futureCardData = this.$$resource.find([addressbook_id, card_id].join('/'));
if (card_id) return new Card(futureCardData); // a single card
@@ -70,26 +72,53 @@
return Card.$unwrapCollection(futureCardData); // a collection of cards
};
// Instance methods
/* Unwrap to a collection of Card instances */
Card.$unwrapCollection = function(futureCardData) {
var collection = {};
collection.$futureCardData = futureCardData;
futureCardData.then(function(cards) {
Card.$timeout(function() {
angular.forEach(cards, function(data, index) {
collection[data.id] = new Card(data);
});
});
});
return collection;
};
/* Instance methods */
Card.prototype.$id = function() {
//var self = this;
return this.$futureAddressBookData.then(function(data) {
return this.$futureCardData.then(function(data) {
return data.id;
});
};
// Unwrap a promise
Card.prototype.$unwrap = function(futureCardData) {
var self = this;
this.$futureCardData = futureCardData;
this.$futureCardData.then(function(data) {
// The success callback. Calling _.extend from $timeout will wrap it into a try/catch call.
Card.$timeout(function() {
angular.extend(self, data);
Card.prototype.$save = function() {
var action = 'saveAsContact';
if (this.tag == 'VLIST') action = 'saveAsList';
//var action = 'saveAs' + this.tag.substring(1).capitalize();
return Card.$$resource.set([this.pid, this.id || '_new_'].join('/'),
this.$omit(),
{ 'action': action })
.then(function (data) {
return data;
});
});
};
Card.prototype.$delete = function(attribute, index) {
if (attribute) {
if (index > -1 && this[attribute].length > index) {
this[attribute].splice(index, 1);
}
}
else {
// No arguments -- delete card
return Card.$$resource.remove([this.pid, this.id].join('/'));
}
};
Card.prototype.$fullname = function() {
@@ -110,6 +139,9 @@
else if (this.emails && this.emails.length > 0) {
fn = _.find(this.emails, function(i) { return i.value != ''; }).value;
}
else if (this.c_cn && this.c_cn.length > 0) {
fn = this.c_cn;
}
}
return fn;
@@ -130,22 +162,30 @@
return description.join(', ');
};
Card.prototype.$preferredEmail = function() {
var email = _.find(this.emails, function(o) {
return o.type == 'pref';
});
if (email) {
email = email.value;
}
else if (this.emails && this.emails.length) {
email = this.emails[0].value;
}
return email;
};
Card.prototype.$birthday = function() {
return new Date(this.birthday*1000);
};
Card.prototype.$isCard = function() {
return this.tag == 'VCARD';
return this.tag == 'vcard';
};
Card.prototype.$isList = function() {
return this.tag == 'VLIST';
};
Card.prototype.$delete = function(attribute, index) {
if (index > -1 && this[attribute].length > index) {
this[attribute].splice(index, 1);
}
return this.tag == 'vlist';
};
Card.prototype.$addOrgUnit = function(orgUnit) {
@@ -226,24 +266,21 @@
return this.addresses.length - 1;
};
/* never call for now */
Card.$unwrapCollection = function(futureCardData) {
var collection = {};
// Unwrap a promise
Card.prototype.$unwrap = function(futureCardData) {
var self = this;
collection.$futureCardData = futureCardData;
futureCardData.then(function(cards) {
this.$futureCardData = futureCardData;
this.$futureCardData.then(function(data) {
// The success callback. Calling _.extend from $timeout will wrap it into a try/catch call and return
// a promise resolved immediately.
Card.$timeout(function() {
angular.forEach(cards, function(data, index) {
collection[data.id] = new Card(data);
});
angular.extend(self, data);
});
});
return collection;
};
// $omit returns a sanitized object used to send to the server
// Return a sanitized object used to send to the server
Card.prototype.$omit = function() {
var card = {};
angular.forEach(this, function(value, key) {
@@ -253,11 +290,4 @@
});
return card;
};
Card.prototype.$save = function() {
return Card.$$resource.set([this.pid, this.id].join('/'), this.$omit()).then(function (data) {
return data;
});
};
})();