snappymail/dev/Model/Contact.js

127 lines
2.9 KiB
JavaScript
Raw Normal View History

2016-07-07 05:03:30 +08:00
import ko from 'ko';
import { ContactPropertyModel } from 'Model/ContactProperty';
2019-07-05 03:19:24 +08:00
import { ContactPropertyType } from 'Common/Enums';
2016-07-07 05:03:30 +08:00
2019-07-05 03:19:24 +08:00
import { AbstractModel } from 'Knoin/AbstractModel';
2016-07-07 05:03:30 +08:00
2019-07-05 03:19:24 +08:00
class ContactModel extends AbstractModel {
2016-07-16 05:29:42 +08:00
constructor() {
2020-10-19 01:19:45 +08:00
super();
2016-07-07 05:03:30 +08:00
this.id = 0;
2016-07-07 05:03:30 +08:00
this.display = '';
this.properties = [];
this.readOnly = false;
this.focused = ko.observable(false);
this.selected = ko.observable(false);
this.checked = ko.observable(false);
this.deleted = ko.observable(false);
}
/**
* @returns {Array|null}
*/
getNameAndEmailHelper() {
2019-07-05 03:19:24 +08:00
let name = '',
2016-07-07 05:03:30 +08:00
email = '';
2020-09-04 23:07:35 +08:00
if (Array.isNotEmpty(this.properties)) {
this.properties.forEach(property => {
2019-07-05 03:19:24 +08:00
if (property) {
if (ContactPropertyType.FirstName === property.type()) {
name = (property.value() + ' ' + name).trim();
} else if (ContactPropertyType.LastName === property.type()) {
name = (name + ' ' + property.value()).trim();
} else if (!email && ContactPropertyType.Email === property.type()) {
email = property.value();
2016-07-07 05:03:30 +08:00
}
}
});
}
return email ? [email, name] : null;
2016-07-07 05:03:30 +08:00
}
/**
* @static
* @param {FetchJsonContact} json
* @returns {?ContactModel}
2016-07-07 05:03:30 +08:00
*/
static reviveFromJson(json) {
const contact = super.reviveFromJson(json);
if (contact) {
let list = [];
if (Array.isNotEmpty(json.properties)) {
json.properties.forEach(property => {
property = ContactPropertyModel.reviveFromJson(property);
property && list.push(property);
2016-07-07 05:03:30 +08:00
});
}
contact.properties = list;
contact.initDefaultProperties();
2016-07-07 05:03:30 +08:00
}
return contact;
2016-07-07 05:03:30 +08:00
}
initDefaultProperties() {
let list = this.properties;
list.sort((p1,p2) =>{
if (p2.type() == ContactPropertyType.FirstName) {
return 1;
}
if (p1.type() == ContactPropertyType.FirstName || p1.type() == ContactPropertyType.LastName) {
return -1;
}
if (p2.type() == ContactPropertyType.LastName) {
return 1;
}
return 0;
});
let found = list.find(prop => prop.type() == ContactPropertyType.LastName);
if (!found) {
found = new ContactPropertyModel(ContactPropertyType.LastName);
list.unshift(found);
}
found.placeholder('CONTACTS/PLACEHOLDER_ENTER_LAST_NAME');
found = list.find(prop => prop.type() == ContactPropertyType.FirstName);
if (!found) {
found = new ContactPropertyModel(ContactPropertyType.FirstName);
list.unshift(found);
}
found.placeholder('CONTACTS/PLACEHOLDER_ENTER_FIRST_NAME');
this.properties = list;
}
2016-07-07 05:03:30 +08:00
/**
* @returns {string}
*/
generateUid() {
return ''+this.id;
2016-07-07 05:03:30 +08:00
}
/**
* @return string
*/
lineAsCss() {
const result = [];
2019-07-05 03:19:24 +08:00
if (this.deleted()) {
2016-07-07 05:03:30 +08:00
result.push('deleted');
}
2019-07-05 03:19:24 +08:00
if (this.selected()) {
2016-07-07 05:03:30 +08:00
result.push('selected');
}
2019-07-05 03:19:24 +08:00
if (this.checked()) {
2016-07-07 05:03:30 +08:00
result.push('checked');
}
2019-07-05 03:19:24 +08:00
if (this.focused()) {
2016-07-07 05:03:30 +08:00
result.push('focused');
}
return result.join(' ');
}
}
2019-07-05 03:19:24 +08:00
export { ContactModel, ContactModel as default };