snappymail/dev/Model/Contact.js

110 lines
2.3 KiB
JavaScript
Raw Normal View History

2016-07-07 05:03:30 +08:00
import ko from 'ko';
2019-07-05 03:19:24 +08:00
import { ContactPropertyType } from 'Common/Enums';
import { trim, isNonEmptyArray, isNormal, pInt, pString } from 'Common/Utils';
import { emptyContactPic } from 'Common/Links';
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() {
2016-07-07 05:03:30 +08:00
super('ContactModel');
this.idContact = 0;
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 = '';
2019-07-05 03:19:24 +08:00
if (isNonEmptyArray(this.properties)) {
this.properties.forEach(property => {
2019-07-05 03:19:24 +08:00
if (property) {
if (ContactPropertyType.FirstName === property[0]) {
2016-07-07 05:03:30 +08:00
name = trim(property[1] + ' ' + name);
2019-07-05 03:19:24 +08:00
} else if (ContactPropertyType.LastName === property[0]) {
2016-07-07 05:03:30 +08:00
name = trim(name + ' ' + property[1]);
} else if (!email && ContactPropertyType.Email === property[0]) {
2016-07-07 05:03:30 +08:00
email = property[1];
}
}
});
}
return email ? [email, name] : null;
2016-07-07 05:03:30 +08:00
}
/**
* @param {Object} oItem
* @returns {boolean}
*/
parse(json) {
let result = false;
2019-07-05 03:19:24 +08:00
if (json && 'Object/Contact' === json['@Object']) {
2016-07-07 05:03:30 +08:00
this.idContact = pInt(json.IdContact);
this.display = pString(json.Display);
this.readOnly = !!json.ReadOnly;
2019-07-05 03:19:24 +08:00
if (isNonEmptyArray(json.Properties)) {
json.Properties.forEach(property => {
2019-07-05 03:19:24 +08:00
if (property && property.Type && isNormal(property.Value) && isNormal(property.TypeStr)) {
2016-07-07 05:03:30 +08:00
this.properties.push([pInt(property.Type), pString(property.Value), pString(property.TypeStr)]);
}
});
}
result = true;
}
return result;
}
/**
* @returns {string}
*/
srcAttr() {
return emptyContactPic();
}
/**
* @returns {string}
*/
generateUid() {
return pString(this.idContact);
}
/**
* @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 };