snappymail/dev/Model/Contact.js

124 lines
2.8 KiB
JavaScript
Raw Normal View History

2021-07-22 03:34:17 +08:00
import { arrayLength } from 'Common/Utils';
2021-01-25 05:58:06 +08:00
import { ContactPropertyModel, ContactPropertyType } from 'Model/ContactProperty';
2019-07-05 03:19:24 +08:00
import { AbstractModel } from 'Knoin/AbstractModel';
2016-07-07 05:03:30 +08:00
2021-01-22 23:32:08 +08:00
export 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;
2020-10-25 18:46:58 +08:00
this.addObservables({
focused: false,
selected: false,
checked: false,
deleted: false
});
2016-07-07 05:03:30 +08:00
}
/**
* @returns {Array|null}
*/
getNameAndEmailHelper() {
2019-07-05 03:19:24 +08:00
let name = '',
2016-07-07 05:03:30 +08:00
email = '';
2021-07-22 03:34:17 +08:00
if (arrayLength(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 = [];
2021-07-22 03:34:17 +08:00
if (arrayLength(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(' ');
}
}