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';
|
2020-09-04 23:07:35 +08:00
|
|
|
import { pInt, pString } from 'Common/Utils';
|
2019-07-05 03:19:24 +08:00
|
|
|
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 = '';
|
|
|
|
|
2020-09-04 23:07:35 +08:00
|
|
|
if (Array.isNotEmpty(this.properties)) {
|
2020-07-22 20:49:18 +08:00
|
|
|
this.properties.forEach(property => {
|
2019-07-05 03:19:24 +08:00
|
|
|
if (property) {
|
|
|
|
if (ContactPropertyType.FirstName === property[0]) {
|
2020-08-07 00:24:46 +08:00
|
|
|
name = (property[1] + ' ' + name).trim();
|
2019-07-05 03:19:24 +08:00
|
|
|
} else if (ContactPropertyType.LastName === property[0]) {
|
2020-08-07 00:24:46 +08:00
|
|
|
name = (name + ' ' + property[1]).trim();
|
2020-07-28 23:20:14 +08:00
|
|
|
} else if (!email && ContactPropertyType.Email === property[0]) {
|
2016-07-07 05:03:30 +08:00
|
|
|
email = property[1];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-07-28 23:20:14 +08:00
|
|
|
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;
|
|
|
|
|
2020-09-04 23:07:35 +08:00
|
|
|
if (Array.isNotEmpty(json.Properties)) {
|
2020-07-22 20:49:18 +08:00
|
|
|
json.Properties.forEach(property => {
|
2020-08-18 03:57:56 +08:00
|
|
|
if (property && property.Type && null != property.Value && null != 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 };
|