Improve contact module models

- Fixed reset behavior
- Created an abstract state for the card viewer and editor
- Moved the card instance from the $rootScope.addressbook to the $scope
This commit is contained in:
Francis Lachapelle
2014-09-29 11:06:46 -04:00
parent 33179bbfb9
commit caa920e0ff
7 changed files with 227 additions and 219 deletions
@@ -65,12 +65,12 @@
* @returns the list of addressbooks
*/
AddressBook.$findAll = function(data) {
var self = this;
var _this = this;
if (data) {
this.$addressbooks = data;
// Instanciate AddressBook objects
angular.forEach(this.$addressbooks, function(o, i) {
self.$addressbooks[i] = new AddressBook(o);
_this.$addressbooks[i] = new AddressBook(o);
});
}
return this.$addressbooks;
@@ -105,7 +105,7 @@
* @returns a collection of Cards instances
*/
AddressBook.prototype.$filter = function(search, options) {
var self = this;
var _this = this;
var params = { 'search': 'name_or_address',
'value': search,
'sort': 'c_cn',
@@ -124,8 +124,8 @@
cards = data.cards;
}
else {
self.cards = data.cards;
cards = self.cards;
_this.cards = data.cards;
cards = _this.cards;
}
// Instanciate Card objects
angular.forEach(cards, function(o, i) {
@@ -152,11 +152,11 @@
};
AddressBook.prototype.$delete = function() {
var self = this;
var _this = this;
var d = AddressBook.$q.defer();
AddressBook.$$resource.remove(this.id)
.then(function() {
var i = _.indexOf(_.pluck(AddressBook.$addressbooks, 'id'), self.id);
var i = _.indexOf(_.pluck(AddressBook.$addressbooks, 'id'), _this.id);
AddressBook.$addressbooks.splice(i, 1);
d.resolve(true);
}, function(data, status) {
@@ -172,40 +172,38 @@
};
AddressBook.prototype.$getCard = function(card_id) {
var self = this;
return this.$id().then(function(addressbook_id) {
self.card = AddressBook.$Card.$find(addressbook_id, card_id);
return self.card;
return AddressBook.$Card.$find(addressbook_id, card_id);
});
};
AddressBook.prototype.$resetCard = function() {
this.$getCard(this.card.id);
};
// Unwrap a promise
AddressBook.prototype.$unwrap = function(futureAddressBookData) {
var self = this;
var _this = this;
// Expose the promise
this.$futureAddressBookData = futureAddressBookData;
// Resolve the promise
this.$futureAddressBookData.then(function(data) {
AddressBook.$timeout(function() {
angular.extend(self, data);
angular.extend(_this, data);
// Also extend AddressBook instance from data of addressbooks list.
// Will inherit attributes such as isEditable and isRemote.
angular.forEach(AddressBook.$findAll(), function(o, i) {
if (o.id == self.id) {
angular.extend(self, o);
if (o.id == _this.id) {
angular.extend(_this, o);
}
});
// Instanciate Card objects
angular.forEach(self.cards, function(o, i) {
self.cards[i] = new AddressBook.$Card(o);
angular.forEach(_this.cards, function(o, i) {
_this.cards[i] = new AddressBook.$Card(o);
});
});
}, function(data) {
angular.extend(self, data);
self.isError = true;
AddressBook.$timeout(function() {
angular.extend(_this, data);
_this.isError = true;
});
});
};
@@ -214,7 +212,6 @@
var addressbook = {};
angular.forEach(this, function(value, key) {
if (key != 'constructor' &&
key != 'card' &&
key != 'cards' &&
key[0] != '$') {
addressbook[key] = value;
+49 -19
View File
@@ -34,7 +34,7 @@
/**
* @memberof Card
* @desc The factory we'll use to register with Angular
* @desc The factory we'll use to register with Angular.
* @returns the Card constructor
*/
Card.$factory = ['$timeout', 'sgSettings', 'sgResource', function($timeout, Settings, Resource) {
@@ -48,7 +48,7 @@
/**
* @module SOGo.ContactsUI
* @desc Factory registration of Card in Angular module
* @desc Factory registration of Card in Angular module.
*/
angular.module('SOGo.ContactsUI')
.factory('sgCard', Card.$factory)
@@ -56,7 +56,7 @@
/**
* @name sgAddress
* @memberof ContactsUI
* @desc Directive to format a postal address
* @desc Directive to format a postal address.
*/
.directive('sgAddress', function() {
return {
@@ -83,7 +83,7 @@
/**
* @memberof Card
* @desc Fetch a card from a specific addressbook
* @desc Fetch a card from a specific addressbook.
* @param {string} addressbook_id - the addressbook ID
* @param {string} card_id - the card ID
* @see {@link AddressBook.$getCard}
@@ -98,7 +98,7 @@
/**
* @memberof Card
* @desc Unwrap to a collection of Card instances
* @desc Unwrap to a collection of Card instances.
* @param {Object} futureCardData
*/
Card.$unwrapCollection = function(futureCardData) {
@@ -120,7 +120,7 @@
/**
* @function $id
* @memberof Card.prototype
* @desc Return the card ID
* @desc Return the card ID.
* @returns the card ID
*/
Card.prototype.$id = function() {
@@ -132,7 +132,7 @@
/**
* @function $save
* @memberof Card.prototype
* @desc Save the card to the server
* @desc Save the card to the server.
*/
Card.prototype.$save = function() {
var action = 'saveAsContact';
@@ -202,7 +202,7 @@
/**
* @function $preferredEmail
* @memberof Card.prototype
* @desc Get the preferred email address
* @desc Get the preferred email address.
* @param {string} [partial] - a partial string that the email must match
* @returns the first email address of type "pref" or the first address if none found
*/
@@ -355,6 +355,22 @@
return this.refs.length - 1;
};
/**
* @function $reset
* @memberof Card.prototype
* @desc Reset the original state the card's data.
*/
Card.prototype.$reset = function() {
var _this = this;
angular.forEach(this, function(value, key) {
if (key != 'constructor' && key[0] != '$') {
delete _this[key];
}
});
angular.extend(this, this.$shadowData);
this.$shadowData = this.$omit(true);
};
/**
* @function $updateMember
* @memberof Card.prototype
@@ -372,30 +388,44 @@
this.refs[index] = ref;
};
// Unwrap a promise
/**
* @function $unwrap
* @memberof Card.prototype
* @desc Unwrap a promise and make a copy of the resolved data.
* @param {Object} futureCardData - a promise of the Card's data
*/
Card.prototype.$unwrap = function(futureCardData) {
var _this = this;
if (futureCardData) {
// Expose the promise
this.$futureCardData = futureCardData;
}
return 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.
return Card.$timeout(function() {
// Resolve the promise
this.$futureCardData.then(function(data) {
// Calling $timeout will force Angular to refresh the view
Card.$timeout(function() {
angular.extend(_this, data);
console.debug(angular.toJson(data));
return _this;
// Make a copy of the data in order for an eventual reset.
_this.$shadowData = _this.$omit(true);
});
});
};
// Return a sanitized object used to send to the server
Card.prototype.$omit = function() {
/**
* @function $omit
* @memberof Card.prototype
* @desc Return a sanitized object used to send to the server.
* @param {boolean} deep - make a deep copy if true
* @return an object literal copy of the Card instance
*/
Card.prototype.$omit = function(deep) {
var card = {};
angular.forEach(this, function(value, key) {
if (key != 'constructor' && key[0] != '$') {
card[key] = value;
if (deep)
card[key] = angular.copy(value);
else
card[key] = value;
}
});
return card;