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
|
|
|
|
2020-10-20 04:09:25 +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)) {
|
2020-07-22 20:49:18 +08:00
|
|
|
this.properties.forEach(property => {
|
2019-07-05 03:19:24 +08:00
|
|
|
if (property) {
|
2020-10-20 21:37:06 +08:00
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-07-28 23:20:14 +08:00
|
|
|
return email ? [email, name] : null;
|
2016-07-07 05:03:30 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-10-19 01:46:47 +08:00
|
|
|
* @static
|
|
|
|
* @param {FetchJsonContact} json
|
|
|
|
* @returns {?ContactModel}
|
2016-07-07 05:03:30 +08:00
|
|
|
*/
|
2020-10-19 01:46:47 +08:00
|
|
|
static reviveFromJson(json) {
|
|
|
|
const contact = super.reviveFromJson(json);
|
|
|
|
if (contact) {
|
2020-10-20 16:27:00 +08:00
|
|
|
let list = [];
|
2021-07-22 03:34:17 +08:00
|
|
|
if (arrayLength(json.properties)) {
|
2020-10-20 21:37:06 +08:00
|
|
|
json.properties.forEach(property => {
|
2020-10-20 16:27:00 +08:00
|
|
|
property = ContactPropertyModel.reviveFromJson(property);
|
|
|
|
property && list.push(property);
|
2016-07-07 05:03:30 +08:00
|
|
|
});
|
|
|
|
}
|
2020-10-20 16:27:00 +08:00
|
|
|
contact.properties = list;
|
|
|
|
contact.initDefaultProperties();
|
2016-07-07 05:03:30 +08:00
|
|
|
}
|
2020-10-19 01:46:47 +08:00
|
|
|
return contact;
|
2016-07-07 05:03:30 +08:00
|
|
|
}
|
|
|
|
|
2020-10-20 16:27:00 +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() {
|
2020-10-20 23:39:00 +08:00
|
|
|
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(' ');
|
|
|
|
}
|
|
|
|
}
|