ContactProperty and FilterCondition use reviveFromJson

This commit is contained in:
djmaze 2020-10-20 10:27:00 +02:00
parent 9844c1882c
commit 2470f1add6
9 changed files with 78 additions and 75 deletions

View file

@ -330,7 +330,7 @@ export const ContactPropertyType = {
FirstName: 15, FirstName: 15,
LastName: 16, LastName: 16,
MiddleName: 16, MiddleName: 17,
Nick: 18, Nick: 18,
NamePrefix: 20, NamePrefix: 20,

View file

@ -1,5 +1,6 @@
import ko from 'ko'; import ko from 'ko';
import { ContactPropertyModel } from 'Model/ContactProperty';
import { ContactPropertyType } from 'Common/Enums'; import { ContactPropertyType } from 'Common/Enums';
import { pInt, pString } from 'Common/Utils'; import { pInt, pString } from 'Common/Utils';
@ -56,17 +57,48 @@ class ContactModel extends AbstractModel {
contact.display = pString(json.display); contact.display = pString(json.display);
contact.readOnly = !!json.readOnly; contact.readOnly = !!json.readOnly;
let list = [];
if (Array.isNotEmpty(json.properties)) { if (Array.isNotEmpty(json.properties)) {
json.Properties.forEach(property => { json.Properties.forEach(property => {
if (property && property.Type && null != property.Value && null != property.TypeStr) { property = ContactPropertyModel.reviveFromJson(property);
contact.properties.push([pInt(property.Type), pString(property.Value), pString(property.TypeStr)]); property && list.push(property);
}
}); });
} }
contact.properties = list;
contact.initDefaultProperties();
} }
return contact; return contact;
} }
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;
}
/** /**
* @returns {string} * @returns {string}
*/ */

View file

@ -33,6 +33,17 @@ class ContactPropertyModel extends AbstractModel {
this.regDisposables([this.placeholderValue, this.largeValue]); this.regDisposables([this.placeholderValue, this.largeValue]);
} }
static reviveFromJson(json) {
const property = super.reviveFromJson(json);
if (property) {
property.type(pInt(property.type));
property.typeStr(pInt(property.typeStr));
property.value(pString(json.value));
return property;
}
return null;
}
} }
export { ContactPropertyModel, ContactPropertyModel as default }; export { ContactPropertyModel, ContactPropertyModel as default };

View file

@ -289,15 +289,14 @@ class EmailModel extends AbstractModel {
*/ */
static reviveFromJson(json) { static reviveFromJson(json) {
const email = super.reviveFromJson(json); const email = super.reviveFromJson(json);
if (email && email.email) { if (email) {
email.name = json.Name.trim(); email.name = json.Name;
email.email = json.Email.trim(); email.email = json.Email;
email.dkimStatus = (json.DkimStatus || '').trim(); email.dkimStatus = (json.DkimStatus || '');
email.dkimValue = (json.DkimValue || '').trim(); email.dkimValue = (json.DkimValue || '');
email.clearDuplicateName(); email.clearDuplicateName();
return email;
} }
return null; return email;
} }
/** /**

View file

@ -225,10 +225,7 @@ class FilterModel extends AbstractModel {
if (Array.isNotEmpty(json.Conditions)) { if (Array.isNotEmpty(json.Conditions)) {
filter.conditions( filter.conditions(
json.Conditions.map(aData => { json.Conditions.map(aData => FilterConditionModel.reviveFromJson(aData)).filter(v => v)
const filterCondition = new FilterConditionModel();
return filterCondition && filterCondition.parse(aData) ? filterCondition : null;
}).filter(v => v)
); );
} }

View file

@ -56,17 +56,15 @@ class FilterConditionModel extends AbstractModel {
return true; return true;
} }
parse(json) { static reviveFromJson(json) {
if (json && json.Field && json.Type) { const filter = super.reviveFromJson(json);
this.field(pString(json.Field)); if (filter) {
this.type(pString(json.Type)); this.field(pString(json.field));
this.value(pString(json.Value)); this.type(pString(json.type));
this.valueSecond(pString(json.ValueSecond)); this.value(pString(json.value));
this.valueSecond(pString(json.valueSecond));
return true;
} }
return filter;
return false;
} }
toJson() { toJson() {

View file

@ -489,10 +489,7 @@ class ContactsPopupView extends AbstractViewNext {
* @param {?ContactModel} contact * @param {?ContactModel} contact
*/ */
populateViewContact(contact) { populateViewContact(contact) {
let id = '', let id = '';
lastName = '',
firstName = '';
const list = [];
this.watchHash(false); this.watchHash(false);
@ -501,49 +498,18 @@ class ContactsPopupView extends AbstractViewNext {
if (contact) { if (contact) {
id = contact.id; id = contact.id;
if (Array.isNotEmpty(contact.properties)) {
contact.properties.forEach(property => {
if (property && property[0]) {
if (ContactPropertyType.LastName === property[0]) {
lastName = property[1];
} else if (ContactPropertyType.FirstName === property[0]) {
firstName = property[1];
} else {
list.push(new ContactPropertyModel(property[0], property[2] || '', property[1]));
}
}
});
}
this.viewReadOnly(!!contact.readOnly); this.viewReadOnly(!!contact.readOnly);
} else {
contact = new ContactModel;
contact.initDefaultProperties();
} }
list.unshift(
new ContactPropertyModel(
ContactPropertyType.LastName,
'',
lastName,
false,
this.getPropertyPlaceholder(ContactPropertyType.LastName)
)
);
list.unshift(
new ContactPropertyModel(
ContactPropertyType.FirstName,
'',
firstName,
!contact,
this.getPropertyPlaceholder(ContactPropertyType.FirstName)
)
);
this.viewID(id); this.viewID(id);
delegateRunOnDestroy(this.viewProperties()); delegateRunOnDestroy(this.viewProperties());
this.viewProperties([]); this.viewProperties([]);
this.viewProperties(list); this.viewProperties(contact.properties);
this.watchDirty(false); this.watchDirty(false);
this.watchHash(true); this.watchHash(true);

View file

@ -151,14 +151,14 @@ class Property implements \JsonSerializable
// Simple hack // Simple hack
if ($this && $this->IsWeb()) if ($this && $this->IsWeb())
{ {
$this->Value = \preg_replace('/(skype|ftp|http[s]?)\\\:\/\//i', '$1://', $this->Value); $this->Value = \preg_replace('#(https?)\\\://#i', '$1://', $this->Value);
} }
return array( return array(
'@Object' => 'Object/Property', '@Object' => 'Object/ContactProperty',
'IdProperty' => $this->IdProperty, 'id' => $this->IdProperty,
'Type' => $this->Type, 'type' => $this->Type,
'TypeStr' => $this->TypeStr, 'typeStr' => $this->TypeStr,
'Value' => \MailSo\Base\Utils::Utf8Clear($this->Value) 'value' => \MailSo\Base\Utils::Utf8Clear($this->Value)
)); ));
} }
} }

View file

@ -91,11 +91,11 @@ class FilterCondition implements \JsonSerializable
public function jsonSerialize() public function jsonSerialize()
{ {
return array( return array(
// '@Object' => 'Object/FilterCondition', '@Object' => 'Object/FilterCondition',
'Field' => $this->Field(), 'field' => $this->Field(),
'Type' => $this->Type(), 'type' => $this->Type(),
'Value' => $this->Value(), 'value' => $this->Value(),
'ValueSecond' => $this->ValueSecond() 'valueSecond' => $this->ValueSecond()
); );
} }
} }