Basic JSON object properties revival now handled by AbstractModel

This will be better for future use of JSON.stringify() and JSON.parse()
For now the difference between the PHP JSON being PascalCase and the JS object properties being camelCase is handled by AbstractModel
This commit is contained in:
djmaze 2020-10-20 17:39:00 +02:00
parent 53bf7c1d60
commit 188a40b196
17 changed files with 131 additions and 185 deletions

View file

@ -5,6 +5,18 @@ function disposeOne(disposable) {
}
}
function typeCast(current, value) {
switch (typeof current)
{
case 'boolean': return !!value;
case 'string': return null != value ? '' + value : '';
case 'number':
value = parseInt(value, 10);
return (isNaN(value) || !isFinite(value)) ? 0 : value;
}
return value;
}
export class AbstractModel {
disposables = [];
@ -13,7 +25,7 @@ export class AbstractModel {
*/
constructor() {
/*
if (new.target === Parent) {
if (new.target === AbstractModel) {
throw new Error("Can't instantiate abstract class!");
}
this.sModelName = new.target.name;
@ -50,15 +62,35 @@ export class AbstractModel {
* @returns {*Model}
*/
static reviveFromJson(json) {
// Object/Attachment
// Object/Contact
// Object/Email
// Object/Filter
// Object/Folder
// Object/Message
// Object/Template
return this.validJson(json) ? new this() : null;
// json && Object.entries(json).forEach(([key, value]) => '@' !== key[0] && (this[key] = value));
let obj = this.validJson(json) ? new this() : null;
try {
obj && Object.entries(json).forEach(([key, value]) => {
if ('@' !== key[0]) {
key = key[0].toLowerCase() + key.substr(1);
switch (typeof obj[key])
{
case 'function':
if (ko.isObservable(obj[key]) && !ko.isObservableArray(obj[key])) {
obj[key](typeCast(obj[key](), value));
}
// else { console.log('('+(typeof obj[key])+') '+key+' not revived'); }
break;
case 'boolean':
case 'string':
case 'number':
obj[key] = typeCast(obj[key], value);
break;
// case 'undefined':
// default:
// console.log('('+(typeof obj[key])+') '+key+' not revived');
}
}
});
} catch (e) {
console.log(this.name);
console.error(e);
}
return obj;
}
}

View file

@ -47,23 +47,6 @@ class AttachmentModel extends AbstractModel {
static reviveFromJson(json) {
const attachment = super.reviveFromJson(json);
if (attachment) {
attachment.mimeType = ((json.MimeType || '').toLowerCase()).trim();
attachment.fileName = json.FileName.trim();
// if it is inline
attachment.isInline = !!json.IsInline;
// if inline image is linked with CID in html
// and 'src="cid:' or background-image:url(cid:)
attachment.isLinked = !!json.IsLinked;
attachment.isThumbnail = !!json.IsThumbnail;
attachment.cid = json.CID;
attachment.contentLocation = json.ContentLocation;
attachment.download = json.Download;
attachment.folder = json.Folder;
attachment.uid = json.Uid;
attachment.mimeIndex = json.MimeIndex;
attachment.framed = !!json.Framed;
attachment.friendlySize = File.friendlySize(json.EstimatedSize);
attachment.cidWithoutTags = attachment.cid.replace(/^<+/, '').replace(/>+$/, '');

View file

@ -2,7 +2,6 @@ import ko from 'ko';
import { ContactPropertyModel } from 'Model/ContactProperty';
import { ContactPropertyType } from 'Common/Enums';
import { pInt, pString } from 'Common/Utils';
import { AbstractModel } from 'Knoin/AbstractModel';
@ -53,10 +52,6 @@ class ContactModel extends AbstractModel {
static reviveFromJson(json) {
const contact = super.reviveFromJson(json);
if (contact) {
contact.id = pInt(json.id);
contact.display = pString(json.display);
contact.readOnly = !!json.readOnly;
let list = [];
if (Array.isNotEmpty(json.properties)) {
json.properties.forEach(property => {
@ -103,7 +98,7 @@ class ContactModel extends AbstractModel {
* @returns {string}
*/
generateUid() {
return pString(this.id);
return ''+this.id;
}
/**

View file

@ -42,16 +42,7 @@ class ContactPropertyModel extends AbstractModel {
};
}
static reviveFromJson(json) {
const property = super.reviveFromJson(json);
if (property) {
property.type(pInt(json.type));
property.typeStr(pString(json.typeStr));
property.value(pString(json.value));
return property;
}
return null;
}
// static reviveFromJson(json) {}
}
export { ContactPropertyModel, ContactPropertyModel as default };

View file

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

View file

@ -216,10 +216,6 @@ class FilterModel extends AbstractModel {
const filter = super.reviveFromJson(json);
if (filter) {
filter.id = pString(json.ID);
filter.name(pString(json.Name));
filter.enabled(!!json.Enabled);
filter.conditionsType(pString(json.ConditionsType));
filter.conditions([]);
@ -229,13 +225,6 @@ class FilterModel extends AbstractModel {
);
}
filter.actionType(pString(json.ActionType));
filter.actionValue(pString(json.ActionValue));
filter.actionValueSecond(pString(json.ActionValueSecond));
filter.actionValueThird(pString(json.ActionValueThird));
filter.actionValueFourth(pString(json.ActionValueFourth));
filter.actionNoStop(!json.Stop);
filter.actionKeep(!!json.Keep);
filter.actionMarkAsRead(!!json.MarkAsRead);

View file

@ -1,7 +1,6 @@
import ko from 'ko';
import { FilterConditionField, FilterConditionType } from 'Common/Enums';
import { pString } from 'Common/Utils';
import { AbstractModel } from 'Knoin/AbstractModel';
@ -56,16 +55,7 @@ class FilterConditionModel extends AbstractModel {
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 filter;
}
// static reviveFromJson(json) {}
toJson() {
return {

View file

@ -49,17 +49,11 @@ class FolderModel extends AbstractModel {
static reviveFromJson(json) {
const folder = super.reviveFromJson(json);
if (folder) {
folder.name(json.Name);
folder.delimiter = json.Delimiter;
folder.fullName = json.FullName;
folder.fullNameRaw = json.FullNameRaw;
folder.fullNameHash = json.FullNameHash;
folder.deep = json.FullNameRaw.split(folder.delimiter).length - 1;
folder.selectable = !!json.IsSelectable;
folder.existen = !!json.IsExists;
folder.subScribed(!!json.IsSubscribed);
folder.checkable(!!json.Checkable);
folder.isInbox = ko.computed(() => FolderType.Inbox === folder.type());

View file

@ -100,11 +100,6 @@ class MessageModel extends AbstractModel {
const oMessageModel = super.reviveFromJson(json);
if (oMessageModel) {
oMessageModel.folderFullNameRaw = json.Folder;
oMessageModel.uid = json.Uid;
oMessageModel.hash = json.Hash;
oMessageModel.requestHash = json.RequestHash;
oMessageModel.size(pInt(json.Size));
oMessageModel.from = EmailCollectionModel.reviveFromJson(json.From);
oMessageModel.to = EmailCollectionModel.reviveFromJson(json.To);
@ -114,7 +109,6 @@ class MessageModel extends AbstractModel {
oMessageModel.deliveredTo = EmailCollectionModel.reviveFromJson(json.DeliveredTo);
oMessageModel.unsubsribeLinks = Array.isNotEmpty(json.UnsubsribeLinks) ? json.UnsubsribeLinks : [];
oMessageModel.subject(json.Subject);
if (isArray(json.SubjectParts)) {
oMessageModel.subjectPrefix(json.SubjectParts[0]);
oMessageModel.subjectSuffix(json.SubjectParts[1]);
@ -123,8 +117,6 @@ class MessageModel extends AbstractModel {
oMessageModel.subjectSuffix(oMessageModel.subject());
}
oMessageModel.dateTimeStampInUTC(pInt(json.DateTimeStampInUTC));
oMessageModel.fromEmailString(oMessageModel.from.toString(true));
oMessageModel.fromClearEmailString(oMessageModel.from.toStringClear());
oMessageModel.toEmailsString(oMessageModel.to.toString(true));

View file

@ -1,7 +1,5 @@
import ko from 'ko';
import { pString } from 'Common/Utils';
import { AbstractModel } from 'Knoin/AbstractModel';
class TemplateModel extends AbstractModel {
@ -10,7 +8,7 @@ class TemplateModel extends AbstractModel {
* @param {string} name
* @param {string} body
*/
constructor(id, name, body) {
constructor(id = '', name = '', body = '') {
super();
this.id = id;
@ -21,21 +19,7 @@ class TemplateModel extends AbstractModel {
this.deleteAccess = ko.observable(false);
}
/**
* @static
* @param {FetchJsonTemplate} json
* @returns {?TemplateModel}
*/
static reviveFromJson(json) {
const template = super.reviveFromJson(json);
if (template) {
template.id = pString(json.ID);
template.name = pString(json.Name);
template.body = pString(json.Body);
template.populated = !!json.Populated;
}
return template;
}
// static reviveFromJson(json) {}
}
export { TemplateModel, TemplateModel as default };