2014-09-24 22:06:12 +02:00
|
|
|
/* -*- Mode: javascript; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
|
2014-06-19 21:47:33 +02:00
|
|
|
(function() {
|
2014-09-24 22:06:12 +02:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @name Card
|
|
|
|
* @constructor
|
|
|
|
* @param {object} futureCardData
|
2015-05-06 23:45:28 +02:00
|
|
|
* @param {string} [partial]
|
2014-09-24 22:06:12 +02:00
|
|
|
*/
|
2015-05-06 23:45:28 +02:00
|
|
|
function Card(futureCardData, partial) {
|
2014-09-24 22:06:12 +02:00
|
|
|
|
|
|
|
// Data is immediately available
|
|
|
|
if (typeof futureCardData.then !== 'function') {
|
2015-05-06 23:45:28 +02:00
|
|
|
this.init(futureCardData, partial);
|
2014-09-24 22:06:12 +02:00
|
|
|
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);
|
|
|
|
this.isNew = true;
|
|
|
|
}
|
2014-06-19 21:47:33 +02:00
|
|
|
}
|
2014-09-24 22:06:12 +02:00
|
|
|
else {
|
|
|
|
// The promise will be unwrapped first
|
|
|
|
this.$unwrap(futureCardData);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Card.$TEL_TYPES = ['work', 'home', 'cell', 'fax', 'pager'];
|
|
|
|
Card.$EMAIL_TYPES = ['work', 'home', 'pref'];
|
|
|
|
Card.$URL_TYPES = ['work', 'home', 'pref'];
|
|
|
|
Card.$ADDRESS_TYPES = ['work', 'home'];
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @memberof Card
|
2014-09-29 17:06:46 +02:00
|
|
|
* @desc The factory we'll use to register with Angular.
|
2014-09-24 22:06:12 +02:00
|
|
|
* @returns the Card constructor
|
|
|
|
*/
|
2015-08-03 17:53:54 +02:00
|
|
|
Card.$factory = ['$timeout', 'sgSettings', 'Resource', 'Preferences', 'Gravatar', function($timeout, Settings, Resource, Preferences, Gravatar) {
|
2014-09-24 22:06:12 +02:00
|
|
|
angular.extend(Card, {
|
2015-08-03 17:53:54 +02:00
|
|
|
$$resource: new Resource(Settings.activeUser('folderURL') + 'Contacts', Settings.activeUser()),
|
2015-04-27 19:18:05 +02:00
|
|
|
$timeout: $timeout,
|
2015-08-03 17:53:54 +02:00
|
|
|
$gravatar: Gravatar
|
|
|
|
});
|
|
|
|
// Initialize categories from user's defaults
|
|
|
|
Preferences.ready().then(function() {
|
|
|
|
if (Preferences.defaults.SOGoContactsCategories) {
|
|
|
|
Card.$categories = Preferences.defaults.SOGoContactsCategories;
|
|
|
|
}
|
2014-09-24 22:06:12 +02:00
|
|
|
});
|
2014-06-19 21:47:33 +02:00
|
|
|
|
2014-09-24 22:06:12 +02:00
|
|
|
return Card; // return constructor
|
|
|
|
}];
|
2014-06-19 21:47:33 +02:00
|
|
|
|
2014-09-24 22:06:12 +02:00
|
|
|
/**
|
|
|
|
* @module SOGo.ContactsUI
|
2014-09-29 17:06:46 +02:00
|
|
|
* @desc Factory registration of Card in Angular module.
|
2014-09-24 22:06:12 +02:00
|
|
|
*/
|
2015-07-29 17:47:01 +02:00
|
|
|
try {
|
|
|
|
angular.module('SOGo.ContactsUI');
|
|
|
|
}
|
|
|
|
catch(e) {
|
|
|
|
angular.module('SOGo.ContactsUI', ['SOGo.Common']);
|
|
|
|
}
|
2014-09-24 22:06:12 +02:00
|
|
|
angular.module('SOGo.ContactsUI')
|
2015-07-24 22:14:53 +02:00
|
|
|
.factory('Card', Card.$factory);
|
2014-08-06 21:14:05 +02:00
|
|
|
|
2014-09-24 22:06:12 +02:00
|
|
|
/**
|
|
|
|
* @memberof Card
|
2014-09-29 17:06:46 +02:00
|
|
|
* @desc Fetch a card from a specific addressbook.
|
2015-05-20 16:27:53 +02:00
|
|
|
* @param {string} addressbookId - the addressbook ID
|
|
|
|
* @param {string} cardId - the card ID
|
2014-09-24 22:06:12 +02:00
|
|
|
* @see {@link AddressBook.$getCard}
|
|
|
|
*/
|
2015-05-20 16:27:53 +02:00
|
|
|
Card.$find = function(addressbookId, cardId) {
|
|
|
|
var futureCardData = this.$$resource.fetch([addressbookId, cardId].join('/'), 'view');
|
2014-09-24 22:06:12 +02:00
|
|
|
|
2015-05-20 16:27:53 +02:00
|
|
|
if (cardId) return new Card(futureCardData); // a single card
|
2014-09-24 22:06:12 +02:00
|
|
|
|
|
|
|
return Card.$unwrapCollection(futureCardData); // a collection of cards
|
|
|
|
};
|
|
|
|
|
2015-04-27 19:18:05 +02:00
|
|
|
/**
|
|
|
|
* @function filterCategories
|
|
|
|
* @memberof Card.prototype
|
|
|
|
* @desc Search for categories matching some criterias
|
|
|
|
* @param {string} search - the search string to match
|
|
|
|
* @returns a collection of strings
|
|
|
|
*/
|
|
|
|
Card.filterCategories = function(query) {
|
|
|
|
var re = new RegExp(query, 'i');
|
|
|
|
return _.filter(Card.$categories, function(category) {
|
|
|
|
return category.search(re) != -1;
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2014-09-24 22:06:12 +02:00
|
|
|
/**
|
|
|
|
* @memberof Card
|
2014-09-29 17:06:46 +02:00
|
|
|
* @desc Unwrap to a collection of Card instances.
|
2015-03-20 16:31:50 +01:00
|
|
|
* @param {object} futureCardData
|
2014-09-24 22:06:12 +02:00
|
|
|
*/
|
|
|
|
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);
|
2014-08-20 21:49:44 +02:00
|
|
|
});
|
2014-09-24 22:06:12 +02:00
|
|
|
});
|
|
|
|
});
|
2014-08-20 21:49:44 +02:00
|
|
|
|
2014-09-24 22:06:12 +02:00
|
|
|
return collection;
|
|
|
|
};
|
|
|
|
|
2015-05-06 23:45:28 +02:00
|
|
|
/**
|
|
|
|
* @function init
|
|
|
|
* @memberof Card.prototype
|
|
|
|
* @desc Extend instance with required attributes and new data.
|
|
|
|
* @param {object} data - attributes of card
|
|
|
|
*/
|
|
|
|
Card.prototype.init = function(data, partial) {
|
|
|
|
this.refs = [];
|
|
|
|
angular.extend(this, data);
|
2015-05-07 16:03:30 +02:00
|
|
|
if (!this.$$fullname)
|
|
|
|
this.$$fullname = this.$fullname();
|
|
|
|
if (!this.$$email)
|
2015-07-01 21:21:22 +02:00
|
|
|
this.$$email = this.$preferredEmail(partial);
|
2015-05-07 16:03:30 +02:00
|
|
|
if (!this.$$image)
|
|
|
|
this.$$image = this.image || Card.$gravatar(this.$preferredEmail(partial), 32);
|
2015-05-08 22:40:39 +02:00
|
|
|
this.selected = false;
|
2015-08-05 22:44:25 +02:00
|
|
|
|
|
|
|
// An empty attribute to trick md-autocomplete when adding attendees from the appointment editor
|
|
|
|
this.empty = ' ';
|
2015-05-06 23:45:28 +02:00
|
|
|
};
|
|
|
|
|
2014-09-24 22:06:12 +02:00
|
|
|
/**
|
|
|
|
* @function $id
|
|
|
|
* @memberof Card.prototype
|
2014-09-29 17:06:46 +02:00
|
|
|
* @desc Return the card ID.
|
2014-09-24 22:06:12 +02:00
|
|
|
* @returns the card ID
|
|
|
|
*/
|
|
|
|
Card.prototype.$id = function() {
|
|
|
|
return this.$futureCardData.then(function(data) {
|
|
|
|
return data.id;
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @function $save
|
|
|
|
* @memberof Card.prototype
|
2014-09-29 17:06:46 +02:00
|
|
|
* @desc Save the card to the server.
|
2014-09-24 22:06:12 +02:00
|
|
|
*/
|
|
|
|
Card.prototype.$save = function() {
|
2014-11-27 21:09:12 +01:00
|
|
|
var _this = this,
|
|
|
|
action = 'saveAsContact';
|
|
|
|
|
2015-07-27 18:47:40 +02:00
|
|
|
if (this.c_component == 'vlist') action = 'saveAsList';
|
2014-11-27 21:09:12 +01:00
|
|
|
|
2014-09-24 22:06:12 +02:00
|
|
|
return Card.$$resource.save([this.pid, this.id || '_new_'].join('/'),
|
|
|
|
this.$omit(),
|
|
|
|
{ action: action })
|
|
|
|
.then(function(data) {
|
2014-11-27 21:09:12 +01:00
|
|
|
// Make a copy of the data for an eventual reset
|
|
|
|
_this.$shadowData = _this.$omit(true);
|
2014-09-24 22:06:12 +02:00
|
|
|
return data;
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
Card.prototype.$delete = function(attribute, index) {
|
|
|
|
if (attribute) {
|
|
|
|
if (index > -1 && this[attribute].length > index) {
|
|
|
|
this[attribute].splice(index, 1);
|
|
|
|
}
|
2015-08-20 16:29:47 +02:00
|
|
|
else
|
|
|
|
delete this[attribute];
|
2014-09-24 22:06:12 +02:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
// No arguments -- delete card
|
|
|
|
return Card.$$resource.remove([this.pid, this.id].join('/'));
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
Card.prototype.$fullname = function() {
|
2015-07-27 18:47:40 +02:00
|
|
|
var fn = this.c_cn || '', names;
|
2015-07-24 22:14:53 +02:00
|
|
|
if (fn.length === 0) {
|
2014-09-24 22:06:12 +02:00
|
|
|
names = [];
|
2015-07-27 18:47:40 +02:00
|
|
|
if (this.c_givenname && this.c_givenname.length > 0)
|
|
|
|
names.push(this.c_givenname);
|
2014-09-24 22:06:12 +02:00
|
|
|
if (this.nickname && this.nickname.length > 0)
|
|
|
|
names.push('<em>' + this.nickname + '</em>');
|
2015-07-27 18:47:40 +02:00
|
|
|
if (this.c_sn && this.c_sn.length > 0)
|
|
|
|
names.push(this.c_sn);
|
2014-09-24 22:06:12 +02:00
|
|
|
if (names.length > 0)
|
|
|
|
fn = names.join(' ');
|
2015-07-27 18:47:40 +02:00
|
|
|
else if (this.c_org && this.c_org.length > 0) {
|
|
|
|
fn = this.c_org;
|
2014-09-24 22:06:12 +02:00
|
|
|
}
|
|
|
|
else if (this.emails && this.emails.length > 0) {
|
2015-07-24 22:14:53 +02:00
|
|
|
fn = _.find(this.emails, function(i) { return i.value !== ''; }).value;
|
2014-09-24 22:06:12 +02:00
|
|
|
}
|
|
|
|
else if (this.c_cn && this.c_cn.length > 0) {
|
|
|
|
fn = this.c_cn;
|
|
|
|
}
|
|
|
|
}
|
2014-06-19 21:47:33 +02:00
|
|
|
|
2014-09-24 22:06:12 +02:00
|
|
|
return fn;
|
|
|
|
};
|
|
|
|
|
|
|
|
Card.prototype.$description = function() {
|
|
|
|
var description = [];
|
|
|
|
if (this.title) description.push(this.title);
|
|
|
|
if (this.role) description.push(this.role);
|
|
|
|
if (this.orgUnits && this.orgUnits.length > 0)
|
|
|
|
_.forEach(this.orgUnits, function(unit) {
|
2015-07-24 22:14:53 +02:00
|
|
|
if (unit.value !== '')
|
2014-09-24 22:06:12 +02:00
|
|
|
description.push(unit.value);
|
|
|
|
});
|
2015-09-03 21:51:55 +02:00
|
|
|
if (this.c_org) description.push(this.c_org);
|
2014-09-24 22:06:12 +02:00
|
|
|
if (this.description) description.push(this.description);
|
|
|
|
|
|
|
|
return description.join(', ');
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @function $preferredEmail
|
|
|
|
* @memberof Card.prototype
|
2014-09-29 17:06:46 +02:00
|
|
|
* @desc Get the preferred email address.
|
2014-09-24 22:06:12 +02:00
|
|
|
* @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
|
|
|
|
*/
|
|
|
|
Card.prototype.$preferredEmail = function(partial) {
|
|
|
|
var email, re;
|
|
|
|
if (partial) {
|
2015-05-01 15:41:17 +02:00
|
|
|
re = new RegExp(partial, 'i');
|
2014-09-24 22:06:12 +02:00
|
|
|
email = _.find(this.emails, function(o) {
|
|
|
|
return re.test(o.value);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
if (email) {
|
|
|
|
email = email.value;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
email = '';
|
|
|
|
}
|
|
|
|
}
|
2014-08-20 21:49:44 +02:00
|
|
|
|
2014-09-24 22:06:12 +02:00
|
|
|
return email;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @function $shortFormat
|
|
|
|
* @memberof Card.prototype
|
|
|
|
* @param {string} [partial] - a partial string that the email must match
|
|
|
|
* @returns the fullname along with a matching email address in parentheses
|
|
|
|
*/
|
|
|
|
Card.prototype.$shortFormat = function(partial) {
|
|
|
|
var fullname = this.$fullname(),
|
|
|
|
email = this.$preferredEmail(partial);
|
|
|
|
if (email && email != fullname)
|
2014-12-16 20:26:31 +01:00
|
|
|
fullname += ' <' + email + '>';
|
2014-09-24 22:06:12 +02:00
|
|
|
return fullname;
|
|
|
|
};
|
|
|
|
|
2015-08-20 16:29:47 +02:00
|
|
|
/**
|
|
|
|
* @function $birthday
|
|
|
|
* @memberof Card.prototype
|
|
|
|
* @returns the formatted birthday object
|
|
|
|
*/
|
2014-09-24 22:06:12 +02:00
|
|
|
Card.prototype.$birthday = function() {
|
2015-08-20 16:29:47 +02:00
|
|
|
if (this.birthday) {
|
|
|
|
return [this.birthday.getFullYear(), this.birthday.getMonth() + 1, this.birthday.getDate()].join('/');
|
|
|
|
}
|
|
|
|
return '';
|
2014-09-24 22:06:12 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
Card.prototype.$isCard = function() {
|
2015-07-27 18:47:40 +02:00
|
|
|
return this.c_component == 'vcard';
|
2014-09-24 22:06:12 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
Card.prototype.$isList = function() {
|
2015-07-27 18:47:40 +02:00
|
|
|
return this.c_component == 'vlist';
|
2014-09-24 22:06:12 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
Card.prototype.$addOrgUnit = function(orgUnit) {
|
|
|
|
if (angular.isUndefined(this.orgUnits)) {
|
|
|
|
this.orgUnits = [{value: orgUnit}];
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
for (var i = 0; i < this.orgUnits.length; i++) {
|
|
|
|
if (this.orgUnits[i].value == orgUnit) {
|
|
|
|
break;
|
2014-06-19 21:47:33 +02:00
|
|
|
}
|
2014-09-24 22:06:12 +02:00
|
|
|
}
|
|
|
|
if (i == this.orgUnits.length)
|
|
|
|
this.orgUnits.push({value: orgUnit});
|
|
|
|
}
|
|
|
|
return this.orgUnits.length - 1;
|
|
|
|
};
|
2014-06-19 21:47:33 +02:00
|
|
|
|
2014-09-24 22:06:12 +02:00
|
|
|
Card.prototype.$addCategory = function(category) {
|
|
|
|
if (angular.isUndefined(this.categories)) {
|
|
|
|
this.categories = [{value: category}];
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
for (var i = 0; i < this.categories.length; i++) {
|
|
|
|
if (this.categories[i].value == category) {
|
|
|
|
break;
|
2014-06-19 21:47:33 +02:00
|
|
|
}
|
2014-09-24 22:06:12 +02:00
|
|
|
}
|
|
|
|
if (i == this.categories.length)
|
|
|
|
this.categories.push({value: category});
|
|
|
|
}
|
|
|
|
};
|
2014-06-19 21:47:33 +02:00
|
|
|
|
2014-09-24 22:06:12 +02:00
|
|
|
Card.prototype.$addEmail = function(type) {
|
|
|
|
if (angular.isUndefined(this.emails)) {
|
|
|
|
this.emails = [{type: type, value: ''}];
|
|
|
|
}
|
2015-07-24 22:14:53 +02:00
|
|
|
else if (_.isUndefined(_.find(this.emails, function(i) { return i.value === ''; }))) {
|
2014-09-24 22:06:12 +02:00
|
|
|
this.emails.push({type: type, value: ''});
|
|
|
|
}
|
|
|
|
return this.emails.length - 1;
|
|
|
|
};
|
2014-06-19 21:47:33 +02:00
|
|
|
|
2014-09-24 22:06:12 +02:00
|
|
|
Card.prototype.$addPhone = function(type) {
|
|
|
|
if (angular.isUndefined(this.phones)) {
|
|
|
|
this.phones = [{type: type, value: ''}];
|
|
|
|
}
|
2015-07-24 22:14:53 +02:00
|
|
|
else if (_.isUndefined(_.find(this.phones, function(i) { return i.value === ''; }))) {
|
2014-09-24 22:06:12 +02:00
|
|
|
this.phones.push({type: type, value: ''});
|
|
|
|
}
|
|
|
|
return this.phones.length - 1;
|
|
|
|
};
|
2014-06-19 21:47:33 +02:00
|
|
|
|
2014-09-24 22:06:12 +02:00
|
|
|
Card.prototype.$addUrl = function(type, url) {
|
|
|
|
if (angular.isUndefined(this.urls)) {
|
|
|
|
this.urls = [{type: type, value: url}];
|
|
|
|
}
|
2015-07-24 22:14:53 +02:00
|
|
|
else if (_.isUndefined(_.find(this.urls, function(i) { return i.value == url; }))) {
|
2014-09-24 22:06:12 +02:00
|
|
|
this.urls.push({type: type, value: url});
|
|
|
|
}
|
|
|
|
return this.urls.length - 1;
|
|
|
|
};
|
2014-06-19 21:47:33 +02:00
|
|
|
|
2014-09-24 22:06:12 +02:00
|
|
|
Card.prototype.$addAddress = function(type, postoffice, street, street2, locality, region, country, postalcode) {
|
|
|
|
if (angular.isUndefined(this.addresses)) {
|
|
|
|
this.addresses = [{type: type, postoffice: postoffice, street: street, street2: street2, locality: locality, region: region, country: country, postalcode: postalcode}];
|
|
|
|
}
|
|
|
|
else if (!_.find(this.addresses, function(i) {
|
2015-07-24 22:14:53 +02:00
|
|
|
return i.street == street &&
|
|
|
|
i.street2 == street2 &&
|
|
|
|
i.locality == locality &&
|
|
|
|
i.country == country &&
|
|
|
|
i.postalcode == postalcode;
|
2014-09-24 22:06:12 +02:00
|
|
|
})) {
|
|
|
|
this.addresses.push({type: type, postoffice: postoffice, street: street, street2: street2, locality: locality, region: region, country: country, postalcode: postalcode});
|
|
|
|
}
|
|
|
|
return this.addresses.length - 1;
|
|
|
|
};
|
2014-06-19 21:47:33 +02:00
|
|
|
|
2014-09-24 22:06:12 +02:00
|
|
|
Card.prototype.$addMember = function(email) {
|
2014-11-03 18:19:35 +01:00
|
|
|
var card = new Card({email: email, emails: [{value: email}]}),
|
|
|
|
i;
|
2014-09-24 22:06:12 +02:00
|
|
|
if (angular.isUndefined(this.refs)) {
|
2014-10-25 04:00:27 +02:00
|
|
|
this.refs = [card];
|
|
|
|
}
|
2015-07-24 22:14:53 +02:00
|
|
|
else if (email.length === 0) {
|
2014-10-25 04:00:27 +02:00
|
|
|
this.refs.push(card);
|
2014-09-24 22:06:12 +02:00
|
|
|
}
|
|
|
|
else {
|
2014-11-03 18:19:35 +01:00
|
|
|
for (i = 0; i < this.refs.length; i++) {
|
2014-09-24 22:06:12 +02:00
|
|
|
if (this.refs[i].email == email) {
|
|
|
|
break;
|
2014-09-05 21:12:05 +02:00
|
|
|
}
|
2014-09-24 22:06:12 +02:00
|
|
|
}
|
|
|
|
if (i == this.refs.length)
|
2014-10-25 04:00:27 +02:00
|
|
|
this.refs.push(card);
|
2014-09-24 22:06:12 +02:00
|
|
|
}
|
|
|
|
return this.refs.length - 1;
|
|
|
|
};
|
|
|
|
|
2014-09-29 17:06:46 +02:00
|
|
|
/**
|
|
|
|
* @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);
|
2014-10-25 04:00:27 +02:00
|
|
|
// Reinstanciate Card objects for list members
|
|
|
|
angular.forEach(this.refs, function(o, i) {
|
|
|
|
if (o.email) o.emails = [{value: o.email}];
|
|
|
|
_this.refs[i] = new Card(o);
|
|
|
|
});
|
2014-09-29 17:06:46 +02:00
|
|
|
this.$shadowData = this.$omit(true);
|
|
|
|
};
|
|
|
|
|
2014-09-24 22:06:12 +02:00
|
|
|
/**
|
|
|
|
* @function $updateMember
|
|
|
|
* @memberof Card.prototype
|
|
|
|
* @desc Update an existing list member from a Card instance.
|
|
|
|
* A list member has the following attribtues:
|
|
|
|
* - email
|
|
|
|
* - reference
|
|
|
|
* - fn
|
|
|
|
* @param {number} index
|
|
|
|
* @param {string} email
|
|
|
|
* @param {Card} card
|
|
|
|
*/
|
2015-07-29 17:47:01 +02:00
|
|
|
// Card.prototype.$updateMember = function(index, email, card) {
|
|
|
|
// var ref = {
|
|
|
|
// email: email,
|
|
|
|
// emails: [{value: email}],
|
|
|
|
// reference: card.c_name,
|
|
|
|
// c_cn: card.$fullname()
|
|
|
|
// };
|
|
|
|
// this.refs[index] = new Card(ref);
|
|
|
|
// };
|
2014-09-24 22:06:12 +02:00
|
|
|
|
2014-09-29 17:06:46 +02:00
|
|
|
/**
|
|
|
|
* @function $unwrap
|
|
|
|
* @memberof Card.prototype
|
|
|
|
* @desc Unwrap a promise and make a copy of the resolved data.
|
2015-03-20 16:31:50 +01:00
|
|
|
* @param {object} futureCardData - a promise of the Card's data
|
2014-09-29 17:06:46 +02:00
|
|
|
*/
|
2014-09-24 22:06:12 +02:00
|
|
|
Card.prototype.$unwrap = function(futureCardData) {
|
|
|
|
var _this = this;
|
2014-11-25 22:13:53 +01:00
|
|
|
|
|
|
|
// Expose the promise
|
2015-08-27 21:55:56 +02:00
|
|
|
this.$futureCardData = futureCardData.then(function(data) {
|
|
|
|
_this.init(data);
|
|
|
|
// Instanciate Card objects for list members
|
|
|
|
angular.forEach(_this.refs, function(o, i) {
|
|
|
|
if (o.email) o.emails = [{value: o.email}];
|
|
|
|
o.id = o.reference;
|
|
|
|
_this.refs[i] = new Card(o);
|
2014-09-24 22:06:12 +02:00
|
|
|
});
|
2015-08-27 21:55:56 +02:00
|
|
|
if (_this.birthday) {
|
|
|
|
_this.birthday = new Date(_this.birthday * 1000);
|
|
|
|
}
|
|
|
|
// Make a copy of the data for an eventual reset
|
|
|
|
_this.$shadowData = _this.$omit(true);
|
|
|
|
return _this;
|
2014-09-24 22:06:12 +02:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2014-09-29 17:06:46 +02:00
|
|
|
/**
|
|
|
|
* @function $omit
|
|
|
|
* @memberof Card.prototype
|
|
|
|
* @desc Return a sanitized object used to send to the server.
|
2015-03-20 16:31:50 +01:00
|
|
|
* @param {boolean} [deep] - make a deep copy if true
|
2014-09-29 17:06:46 +02:00
|
|
|
* @return an object literal copy of the Card instance
|
|
|
|
*/
|
|
|
|
Card.prototype.$omit = function(deep) {
|
2014-09-24 22:06:12 +02:00
|
|
|
var card = {};
|
|
|
|
angular.forEach(this, function(value, key) {
|
2014-10-25 04:00:27 +02:00
|
|
|
if (key == 'refs') {
|
|
|
|
card.refs = _.map(value, function(o) {
|
|
|
|
return o.$omit(deep);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
else if (key != 'constructor' && key[0] != '$') {
|
2014-09-29 17:06:46 +02:00
|
|
|
if (deep)
|
|
|
|
card[key] = angular.copy(value);
|
|
|
|
else
|
|
|
|
card[key] = value;
|
2014-09-24 22:06:12 +02:00
|
|
|
}
|
|
|
|
});
|
2015-08-20 16:29:47 +02:00
|
|
|
|
|
|
|
// We convert back our birthday object
|
2015-08-20 21:47:41 +02:00
|
|
|
if (!deep) {
|
|
|
|
if (card.birthday)
|
|
|
|
card.birthday = card.birthday.getTime()/1000;
|
|
|
|
else
|
|
|
|
card.birthday = 0;
|
|
|
|
}
|
2015-08-20 16:29:47 +02:00
|
|
|
|
2014-09-24 22:06:12 +02:00
|
|
|
return card;
|
|
|
|
};
|
2015-07-24 22:14:53 +02:00
|
|
|
|
|
|
|
Card.prototype.toString = function() {
|
|
|
|
var desc = this.id + ' ' + this.$$fullname;
|
|
|
|
|
|
|
|
if (this.$$email)
|
|
|
|
desc += ' <' + this.$$email + '>';
|
|
|
|
|
|
|
|
return '[' + desc + ']';
|
|
|
|
};
|
2014-06-19 21:47:33 +02:00
|
|
|
})();
|