From 71f6fb25e5099c8a54065b8ebf8fe3e3e6c11303 Mon Sep 17 00:00:00 2001 From: djmaze Date: Wed, 16 Sep 2020 16:33:53 +0200 Subject: [PATCH] Let all the new CollectionModels use AbstractCollectionModel --- dev/Model/AbstractCollection.js | 15 +++++++++++++++ dev/Model/AttachmentCollection.js | 13 +++---------- dev/Model/EmailCollection.js | 9 ++------- dev/Model/FolderCollection.js | 22 ++++++++++------------ dev/Model/MessageCollection.js | 24 +++++++++--------------- 5 files changed, 39 insertions(+), 44 deletions(-) create mode 100644 dev/Model/AbstractCollection.js diff --git a/dev/Model/AbstractCollection.js b/dev/Model/AbstractCollection.js new file mode 100644 index 000000000..b68a8b481 --- /dev/null +++ b/dev/Model/AbstractCollection.js @@ -0,0 +1,15 @@ + +export class AbstractCollectionModel extends Array +{ + constructor(props) { + super(); + props && Object.entries(props).forEach(([key, value]) => '@' !== key[0] && (this[key] = value)); +// props[@Count] + } + + static getFromJSON(object, name) { + return object && 'Collection/'+name === object['@Object'] && Array.isArray(object['@Collection']) + ? object['@Collection'] + : null; + } +} diff --git a/dev/Model/AttachmentCollection.js b/dev/Model/AttachmentCollection.js index 2f06d3fa3..9dfb52788 100644 --- a/dev/Model/AttachmentCollection.js +++ b/dev/Model/AttachmentCollection.js @@ -1,22 +1,17 @@ +import { AbstractCollectionModel } from 'Model/AbstractCollection'; import { AttachmentModel } from 'Model/Attachment'; 'use strict'; -class AttachmentCollectionModel extends Array +export class AttachmentCollectionModel extends AbstractCollectionModel { - constructor() { - super(); - } - /** * @param {?Array} json * @returns {AttachmentCollectionModel} */ static reviveFromJson(items, foundedCIDs) { let result = new AttachmentCollectionModel; - if (items && 'Collection/AttachmentCollection' === items['@Object']) { - items = items['@Collection']; - } + items = this.getFromJSON(items, 'AttachmentCollection') || items; Array.isArray(items) && items.forEach(attachment => { attachment = AttachmentModel.newInstanceFromJson(attachment); if (attachment) { @@ -45,5 +40,3 @@ class AttachmentCollectionModel extends Array return this.find(item => cid === item.cidWithOutTags); } } - -export { AttachmentCollectionModel, AttachmentCollectionModel as default }; diff --git a/dev/Model/EmailCollection.js b/dev/Model/EmailCollection.js index 7689ef5a1..ac194cf4c 100644 --- a/dev/Model/EmailCollection.js +++ b/dev/Model/EmailCollection.js @@ -1,13 +1,10 @@ +import { AbstractCollectionModel } from 'Model/AbstractCollection'; import { EmailModel } from 'Model/Email'; 'use strict'; -class EmailCollectionModel extends Array +export class EmailCollectionModel extends AbstractCollectionModel { - constructor() { - super(); - } - /** * @param {?Array} json * @returns {EmailCollectionModel} @@ -45,5 +42,3 @@ class EmailCollectionModel extends Array return result.join(', '); } } - -export { EmailCollectionModel, EmailCollectionModel as default }; diff --git a/dev/Model/FolderCollection.js b/dev/Model/FolderCollection.js index 7293a259c..a879cba1c 100644 --- a/dev/Model/FolderCollection.js +++ b/dev/Model/FolderCollection.js @@ -1,3 +1,5 @@ +import { AbstractCollectionModel } from 'Model/AbstractCollection'; + import { UNUSED_OPTION_VALUE } from 'Common/Consts'; import { pInt } from 'Common/Utils'; import { ClientSideKeyName, ServerFolderType } from 'Common/Enums'; @@ -22,34 +24,32 @@ normalizeFolder = sFolderFullNameRaw => ('' === sFolderFullNameRaw ? sFolderFullNameRaw : ''; -class FolderCollectionModel extends Array +export class FolderCollectionModel extends AbstractCollectionModel { +/* constructor() { super(); -/* this.CountRec this.FoldersHash this.IsThreadsSupported this.Namespace; this.Optimized this.SystemFolders -*/ } +*/ /** * @param {?Object} json * @returns {FolderCollectionModel} */ - static reviveFromJson(collection) { - if (collection && 'Collection/FolderCollection' === collection['@Object'] - && Array.isArray(collection['@Collection'])) { - const result = new FolderCollectionModel, + static reviveFromJson(object) { + const collection = this.getFromJSON(object, 'FolderCollection'); + if (collection) { + const result = new FolderCollectionModel(object), expandedFolders = Local.get(ClientSideKeyName.ExpandedFolders), bDisplaySpecSetting = FolderStore.displaySpecSetting(); - Object.entries(collection).forEach(([key, value]) => '@' !== key[0] && (result[key] = value)); - - collection['@Collection'].forEach(oFolder => { + collection.forEach(oFolder => { if (oFolder) { let oCacheFolder = Cache.getFolderFromCacheList(oFolder.FullNameRaw); if (!oCacheFolder) { @@ -157,5 +157,3 @@ class FolderCollectionModel extends Array } } - -export { FolderCollectionModel, FolderCollectionModel as default }; diff --git a/dev/Model/MessageCollection.js b/dev/Model/MessageCollection.js index 366de5aff..caa06ffd2 100644 --- a/dev/Model/MessageCollection.js +++ b/dev/Model/MessageCollection.js @@ -1,3 +1,4 @@ +import { AbstractCollectionModel } from 'Model/AbstractCollection'; import { MessageModel } from 'Model/Message'; import { @@ -8,11 +9,11 @@ import { 'use strict'; -class MessageCollectionModel extends Array +export class MessageCollectionModel extends AbstractCollectionModel { +/* constructor() { super(); -/* this.Filtered this.Folder this.FolderHash @@ -25,23 +26,20 @@ class MessageCollectionModel extends Array this.Search this.ThreadUid this.UidNext -*/ } +*/ /** * @param {?Object} json * @returns {MessageCollectionModel} */ - static reviveFromJson(collection, cached) { - if (collection - && 'Collection/MessageCollection' === collection['@Object'] - && Array.isArray(collection['@Collection'])) { - const result = new MessageCollectionModel; - - Object.entries(collection).forEach(([key, value]) => '@' !== key[0] && (result[key] = value)); + static reviveFromJson(object, cached) { + const collection = this.getFromJSON(object, 'MessageCollection'); + if (collection) { + const result = new MessageCollectionModel(object); let newCount = 0; - collection['@Collection'].forEach(message => { + collection.forEach(message => { if (message && 'Object/Message' === message['@Object']) { message = MessageModel.newInstanceFromJson(message); if (message) { @@ -59,11 +57,7 @@ class MessageCollectionModel extends Array } }); -// collection[@Count] == result.length - return result; } } } - -export { MessageCollectionModel, MessageCollectionModel as default };