2014-06-19 21:47:33 +02:00
|
|
|
(function() {
|
|
|
|
'use strict';
|
|
|
|
|
|
|
|
/* Constructor */
|
|
|
|
|
2014-08-06 21:14:05 +02:00
|
|
|
function Card(futureCardData) {
|
2014-06-19 21:47:33 +02:00
|
|
|
|
|
|
|
/* DATA IS IMMEDIATELY AVAILABLE */
|
2014-08-06 21:14:05 +02:00
|
|
|
// if (!futureCardData.inspect) {
|
|
|
|
// angular.extend(this, futureCardData);
|
2014-06-19 21:47:33 +02:00
|
|
|
// return;
|
|
|
|
// }
|
|
|
|
|
|
|
|
/* THE PROMISE WILL BE UNWRAPPED FIRST */
|
2014-08-06 21:14:05 +02:00
|
|
|
this.$unwrap(futureCardData);
|
2014-06-19 21:47:33 +02:00
|
|
|
}
|
|
|
|
|
2014-08-06 21:14:05 +02:00
|
|
|
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'];
|
2014-06-19 21:47:33 +02:00
|
|
|
|
|
|
|
/* THE FACTORY WE'LL USE TO REGISTER WITH ANGULAR */
|
2014-08-06 21:14:05 +02:00
|
|
|
Card.$factory = ['$timeout', 'sgSettings', 'sgResource', function($timeout, Settings, Resource) {
|
|
|
|
angular.extend(Card, {
|
2014-06-19 21:47:33 +02:00
|
|
|
$$resource: new Resource(Settings.baseURL),
|
|
|
|
$timeout: $timeout
|
|
|
|
});
|
|
|
|
|
2014-08-06 21:14:05 +02:00
|
|
|
return Card; // return constructor
|
2014-06-19 21:47:33 +02:00
|
|
|
}];
|
|
|
|
|
|
|
|
/* ANGULAR MODULE REGISTRATION */
|
2014-08-06 21:14:05 +02:00
|
|
|
angular.module('SOGo.Contacts')
|
|
|
|
.factory('sgCard', Card.$factory)
|
|
|
|
|
|
|
|
.directive('sgAddress', function() {
|
|
|
|
return {
|
|
|
|
restrict: 'A',
|
|
|
|
replace: true,
|
|
|
|
scope: { data: '=sgAddress' },
|
|
|
|
controller: ['$scope', function($scope) {
|
|
|
|
$scope.addressLines = function(data) {
|
|
|
|
var lines = [];
|
|
|
|
if (data.street) lines.push(data.street);
|
|
|
|
if (data.street2) lines.push(data.street2);
|
|
|
|
var locality_region = [];
|
|
|
|
if (data.locality) locality_region.push(data.locality);
|
|
|
|
if (data.region) locality_region.push(data.region);
|
|
|
|
if (locality_region.length > 0) lines.push(locality_region.join(', '));
|
|
|
|
if (data.country) lines.push(data.country);
|
|
|
|
if (data.postalcode) lines.push(data.postalcode);
|
|
|
|
return lines.join('<br>');
|
|
|
|
};
|
|
|
|
}],
|
|
|
|
template: '<address ng-bind-html="addressLines(data)"></address>'
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2014-06-19 21:47:33 +02:00
|
|
|
|
|
|
|
/* FETCH A UNIT */
|
2014-08-06 21:14:05 +02:00
|
|
|
Card.$find = function(addressbook_id, card_id) {
|
2014-06-19 21:47:33 +02:00
|
|
|
|
|
|
|
/* FALLS BACK ON AN INSTANCE OF RESOURCE */
|
|
|
|
// Fetch data over the wire.
|
2014-08-06 21:14:05 +02:00
|
|
|
var futureCardData = this.$$resource.find([addressbook_id, card_id].join('/'));
|
2014-06-19 21:47:33 +02:00
|
|
|
|
2014-08-06 21:14:05 +02:00
|
|
|
if (card_id) return new Card(futureCardData); // a single card
|
2014-06-19 21:47:33 +02:00
|
|
|
|
2014-08-06 21:14:05 +02:00
|
|
|
return Card.$unwrapCollection(futureCardData); // a collection of cards
|
2014-06-19 21:47:33 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
// Instance methods
|
|
|
|
|
2014-08-06 21:14:05 +02:00
|
|
|
Card.prototype.$id = function() {
|
2014-06-19 21:47:33 +02:00
|
|
|
//var self = this;
|
|
|
|
return this.$futureAddressBookData.then(function(data) {
|
|
|
|
return data.id;
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
// Unwrap a promise
|
2014-08-06 21:14:05 +02:00
|
|
|
Card.prototype.$unwrap = function(futureCardData) {
|
2014-06-19 21:47:33 +02:00
|
|
|
var self = this;
|
|
|
|
|
2014-08-06 21:14:05 +02:00
|
|
|
this.$futureCardData = futureCardData;
|
|
|
|
this.$futureCardData.then(function(data) {
|
2014-06-19 21:47:33 +02:00
|
|
|
// The success callback. Calling _.extend from $timeout will wrap it into a try/catch call.
|
2014-08-06 21:14:05 +02:00
|
|
|
Card.$timeout(function() {
|
2014-06-19 21:47:33 +02:00
|
|
|
angular.extend(self, data);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2014-08-06 21:14:05 +02:00
|
|
|
Card.prototype.$fullname = function() {
|
2014-06-19 21:47:33 +02:00
|
|
|
var fn = this.fn || '';
|
|
|
|
if (fn.length == 0) {
|
|
|
|
var names = [];
|
|
|
|
if (this.givenname && this.givenname.length > 0)
|
|
|
|
names.push(this.givenname);
|
|
|
|
if (this.nickname && this.nickname.length > 0)
|
|
|
|
names.push("<em>" + this.nickname + "</em>");
|
|
|
|
if (this.sn && this.sn.length > 0)
|
|
|
|
names.push(this.sn);
|
|
|
|
if (names.length > 0)
|
|
|
|
fn = names.join(' ');
|
|
|
|
else if (this.org && this.org.length > 0) {
|
|
|
|
fn = this.org;
|
|
|
|
}
|
|
|
|
else if (this.emails && this.emails.length > 0) {
|
|
|
|
fn = _.find(this.emails, function(i) { return i.value != ''; }).value;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return fn;
|
|
|
|
};
|
|
|
|
|
2014-08-06 21:14:05 +02:00
|
|
|
Card.prototype.$description = function() {
|
2014-06-19 21:47:33 +02:00
|
|
|
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) {
|
|
|
|
if (unit.value != '')
|
|
|
|
description.push(unit.value);
|
|
|
|
});
|
|
|
|
if (this.org) description.push(this.org);
|
|
|
|
if (this.description) description.push(this.description);
|
|
|
|
|
|
|
|
return description.join(', ');
|
|
|
|
};
|
|
|
|
|
2014-08-06 21:14:05 +02:00
|
|
|
Card.prototype.$birthday = function() {
|
2014-06-19 21:47:33 +02:00
|
|
|
return new Date(this.birthday*1000);
|
|
|
|
};
|
|
|
|
|
2014-08-06 21:14:05 +02:00
|
|
|
Card.prototype.$isCard = function() {
|
2014-06-19 21:47:33 +02:00
|
|
|
return this.tag == 'VCARD';
|
|
|
|
};
|
|
|
|
|
2014-08-06 21:14:05 +02:00
|
|
|
Card.prototype.$isList = function() {
|
2014-06-19 21:47:33 +02:00
|
|
|
return this.tag == 'VLIST';
|
|
|
|
};
|
|
|
|
|
2014-08-06 21:14:05 +02:00
|
|
|
Card.prototype.$delete = function(attribute, index) {
|
2014-06-19 21:47:33 +02:00
|
|
|
if (index > -1 && this[attribute].length > index) {
|
|
|
|
this[attribute].splice(index, 1);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2014-08-06 21:14:05 +02:00
|
|
|
Card.prototype.$addOrgUnit = function(orgUnit) {
|
2014-06-19 21:47:33 +02:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (i == this.orgUnits.length)
|
|
|
|
this.orgUnits.push({value: orgUnit});
|
|
|
|
}
|
|
|
|
return this.orgUnits.length - 1;
|
|
|
|
};
|
|
|
|
|
2014-08-06 21:14:05 +02:00
|
|
|
Card.prototype.$addCategory = function(category) {
|
2014-06-19 21:47:33 +02:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (i == this.categories.length)
|
|
|
|
this.categories.push({value: category});
|
|
|
|
}
|
|
|
|
return this.categories.length - 1;
|
|
|
|
};
|
|
|
|
|
2014-08-06 21:14:05 +02:00
|
|
|
Card.prototype.$addEmail = function(type) {
|
2014-06-19 21:47:33 +02:00
|
|
|
if (angular.isUndefined(this.emails)) {
|
|
|
|
this.emails = [{type: type, value: ''}];
|
|
|
|
}
|
|
|
|
else if (!_.find(this.emails, function(i) { return i.value == ''; })) {
|
|
|
|
this.emails.push({type: type, value: ''});
|
|
|
|
}
|
|
|
|
return this.emails.length - 1;
|
|
|
|
};
|
|
|
|
|
2014-08-06 21:14:05 +02:00
|
|
|
Card.prototype.$addPhone = function(type) {
|
2014-06-19 21:47:33 +02:00
|
|
|
if (angular.isUndefined(this.phones)) {
|
|
|
|
this.phones = [{type: type, value: ''}];
|
|
|
|
}
|
|
|
|
else if (!_.find(this.phones, function(i) { return i.value == ''; })) {
|
|
|
|
this.phones.push({type: type, value: ''});
|
|
|
|
}
|
|
|
|
return this.phones.length - 1;
|
|
|
|
};
|
|
|
|
|
2014-08-06 21:14:05 +02:00
|
|
|
Card.prototype.$addUrl = function(type, url) {
|
2014-06-19 21:47:33 +02:00
|
|
|
if (angular.isUndefined(this.urls)) {
|
|
|
|
this.urls = [{type: type, value: url}];
|
|
|
|
}
|
|
|
|
else if (!_.find(this.urls, function(i) { return i.value == url; })) {
|
|
|
|
this.urls.push({type: type, value: url});
|
|
|
|
}
|
|
|
|
return this.urls.length - 1;
|
|
|
|
};
|
|
|
|
|
2014-08-06 21:14:05 +02:00
|
|
|
Card.prototype.$addAddress = function(type, postoffice, street, street2, locality, region, country, postalcode) {
|
2014-06-19 21:47:33 +02:00
|
|
|
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) {
|
|
|
|
return i.street == street
|
|
|
|
&& i.street2 == street2
|
|
|
|
&& i.locality == locality
|
|
|
|
&& i.country == country
|
|
|
|
&& i.postalcode == postalcode;
|
|
|
|
})) {
|
|
|
|
this.addresses.push({type: type, postoffice: postoffice, street: street, street2: street2, locality: locality, region: region, country: country, postalcode: postalcode});
|
|
|
|
}
|
|
|
|
return this.addresses.length - 1;
|
|
|
|
};
|
|
|
|
|
|
|
|
/* never call for now */
|
2014-08-06 21:14:05 +02:00
|
|
|
Card.$unwrapCollection = function(futureCardData) {
|
2014-06-19 21:47:33 +02:00
|
|
|
var collection = {};
|
|
|
|
|
2014-08-06 21:14:05 +02:00
|
|
|
collection.$futureCardData = futureCardData;
|
2014-06-19 21:47:33 +02:00
|
|
|
|
2014-08-06 21:14:05 +02:00
|
|
|
futureCardData.then(function(cards) {
|
|
|
|
Card.$timeout(function() {
|
|
|
|
angular.forEach(cards, function(data, index) {
|
|
|
|
collection[data.id] = new Card(data);
|
2014-06-19 21:47:33 +02:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
return collection;
|
|
|
|
};
|
|
|
|
|
|
|
|
// $omit returns a sanitized object used to send to the server
|
2014-08-06 21:14:05 +02:00
|
|
|
Card.prototype.$omit = function() {
|
|
|
|
var card = {};
|
2014-06-19 21:47:33 +02:00
|
|
|
angular.forEach(this, function(value, key) {
|
|
|
|
if (key != 'constructor' && key[0] != '$') {
|
2014-08-06 21:14:05 +02:00
|
|
|
card[key] = value;
|
2014-06-19 21:47:33 +02:00
|
|
|
}
|
|
|
|
});
|
2014-08-06 21:14:05 +02:00
|
|
|
return card;
|
2014-06-19 21:47:33 +02:00
|
|
|
};
|
|
|
|
|
2014-08-06 21:14:05 +02:00
|
|
|
Card.prototype.$save = function() {
|
|
|
|
return Card.$$resource.set([this.pid, this.id].join('/'), this.$omit()).then(function (data) {
|
2014-06-19 21:47:33 +02:00
|
|
|
return data;
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
})();
|