mirror of
https://github.com/the-djmaze/snappymail.git
synced 2025-03-06 11:49:32 +08:00
ContactProperty and FilterCondition use reviveFromJson
This commit is contained in:
parent
9844c1882c
commit
2470f1add6
9 changed files with 78 additions and 75 deletions
|
@ -330,7 +330,7 @@ export const ContactPropertyType = {
|
|||
|
||||
FirstName: 15,
|
||||
LastName: 16,
|
||||
MiddleName: 16,
|
||||
MiddleName: 17,
|
||||
Nick: 18,
|
||||
|
||||
NamePrefix: 20,
|
||||
|
|
|
@ -1,5 +1,6 @@
|
|||
import ko from 'ko';
|
||||
|
||||
import { ContactPropertyModel } from 'Model/ContactProperty';
|
||||
import { ContactPropertyType } from 'Common/Enums';
|
||||
import { pInt, pString } from 'Common/Utils';
|
||||
|
||||
|
@ -56,17 +57,48 @@ class ContactModel extends AbstractModel {
|
|||
contact.display = pString(json.display);
|
||||
contact.readOnly = !!json.readOnly;
|
||||
|
||||
let list = [];
|
||||
if (Array.isNotEmpty(json.properties)) {
|
||||
json.Properties.forEach(property => {
|
||||
if (property && property.Type && null != property.Value && null != property.TypeStr) {
|
||||
contact.properties.push([pInt(property.Type), pString(property.Value), pString(property.TypeStr)]);
|
||||
}
|
||||
property = ContactPropertyModel.reviveFromJson(property);
|
||||
property && list.push(property);
|
||||
});
|
||||
}
|
||||
contact.properties = list;
|
||||
contact.initDefaultProperties();
|
||||
}
|
||||
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}
|
||||
*/
|
||||
|
|
|
@ -33,6 +33,17 @@ class ContactPropertyModel extends AbstractModel {
|
|||
|
||||
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 };
|
||||
|
|
|
@ -289,15 +289,14 @@ class EmailModel extends AbstractModel {
|
|||
*/
|
||||
static reviveFromJson(json) {
|
||||
const email = super.reviveFromJson(json);
|
||||
if (email && email.email) {
|
||||
email.name = json.Name.trim();
|
||||
email.email = json.Email.trim();
|
||||
email.dkimStatus = (json.DkimStatus || '').trim();
|
||||
email.dkimValue = (json.DkimValue || '').trim();
|
||||
if (email) {
|
||||
email.name = json.Name;
|
||||
email.email = json.Email;
|
||||
email.dkimStatus = (json.DkimStatus || '');
|
||||
email.dkimValue = (json.DkimValue || '');
|
||||
email.clearDuplicateName();
|
||||
return email;
|
||||
}
|
||||
return null;
|
||||
return email;
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -225,10 +225,7 @@ class FilterModel extends AbstractModel {
|
|||
|
||||
if (Array.isNotEmpty(json.Conditions)) {
|
||||
filter.conditions(
|
||||
json.Conditions.map(aData => {
|
||||
const filterCondition = new FilterConditionModel();
|
||||
return filterCondition && filterCondition.parse(aData) ? filterCondition : null;
|
||||
}).filter(v => v)
|
||||
json.Conditions.map(aData => FilterConditionModel.reviveFromJson(aData)).filter(v => v)
|
||||
);
|
||||
}
|
||||
|
||||
|
|
|
@ -56,17 +56,15 @@ class FilterConditionModel extends AbstractModel {
|
|||
return true;
|
||||
}
|
||||
|
||||
parse(json) {
|
||||
if (json && json.Field && json.Type) {
|
||||
this.field(pString(json.Field));
|
||||
this.type(pString(json.Type));
|
||||
this.value(pString(json.Value));
|
||||
this.valueSecond(pString(json.ValueSecond));
|
||||
|
||||
return true;
|
||||
static reviveFromJson(json) {
|
||||
const filter = super.reviveFromJson(json);
|
||||
if (filter) {
|
||||
this.field(pString(json.field));
|
||||
this.type(pString(json.type));
|
||||
this.value(pString(json.value));
|
||||
this.valueSecond(pString(json.valueSecond));
|
||||
}
|
||||
|
||||
return false;
|
||||
return filter;
|
||||
}
|
||||
|
||||
toJson() {
|
||||
|
|
|
@ -489,10 +489,7 @@ class ContactsPopupView extends AbstractViewNext {
|
|||
* @param {?ContactModel} contact
|
||||
*/
|
||||
populateViewContact(contact) {
|
||||
let id = '',
|
||||
lastName = '',
|
||||
firstName = '';
|
||||
const list = [];
|
||||
let id = '';
|
||||
|
||||
this.watchHash(false);
|
||||
|
||||
|
@ -501,49 +498,18 @@ class ContactsPopupView extends AbstractViewNext {
|
|||
|
||||
if (contact) {
|
||||
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);
|
||||
} 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);
|
||||
|
||||
delegateRunOnDestroy(this.viewProperties());
|
||||
|
||||
this.viewProperties([]);
|
||||
this.viewProperties(list);
|
||||
this.viewProperties(contact.properties);
|
||||
|
||||
this.watchDirty(false);
|
||||
this.watchHash(true);
|
||||
|
|
|
@ -151,14 +151,14 @@ class Property implements \JsonSerializable
|
|||
// Simple hack
|
||||
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(
|
||||
'@Object' => 'Object/Property',
|
||||
'IdProperty' => $this->IdProperty,
|
||||
'Type' => $this->Type,
|
||||
'TypeStr' => $this->TypeStr,
|
||||
'Value' => \MailSo\Base\Utils::Utf8Clear($this->Value)
|
||||
'@Object' => 'Object/ContactProperty',
|
||||
'id' => $this->IdProperty,
|
||||
'type' => $this->Type,
|
||||
'typeStr' => $this->TypeStr,
|
||||
'value' => \MailSo\Base\Utils::Utf8Clear($this->Value)
|
||||
));
|
||||
}
|
||||
}
|
||||
|
|
|
@ -91,11 +91,11 @@ class FilterCondition implements \JsonSerializable
|
|||
public function jsonSerialize()
|
||||
{
|
||||
return array(
|
||||
// '@Object' => 'Object/FilterCondition',
|
||||
'Field' => $this->Field(),
|
||||
'Type' => $this->Type(),
|
||||
'Value' => $this->Value(),
|
||||
'ValueSecond' => $this->ValueSecond()
|
||||
'@Object' => 'Object/FilterCondition',
|
||||
'field' => $this->Field(),
|
||||
'type' => $this->Type(),
|
||||
'value' => $this->Value(),
|
||||
'valueSecond' => $this->ValueSecond()
|
||||
);
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue