Added addComputables() to *Model

This commit is contained in:
djmaze 2020-10-25 14:14:14 +01:00
parent 22f606ea75
commit b165a1de4f
6 changed files with 221 additions and 223 deletions

View file

@ -41,6 +41,10 @@ export class AbstractModel {
*/ */
} }
addComputables(obj) {
Object.entries(obj).forEach(([key, fn]) => this[key] = ko.computed(fn) );
}
addSubscribables(obj) { addSubscribables(obj) {
Object.entries(obj).forEach(([key, fn]) => this.disposables.push( this[key].subscribe(fn) ) ); Object.entries(obj).forEach(([key, fn]) => this.disposables.push( this[key].subscribe(fn) ) );
} }

View file

@ -1,4 +1,3 @@
import ko from 'ko';
import { pInt } from 'Common/Utils'; import { pInt } from 'Common/Utils';
import { File } from 'Common/File'; import { File } from 'Common/File';
@ -37,27 +36,27 @@ class ComposeAttachmentModel extends AbstractModel {
complete: false complete: false
}); });
this.progressText = ko.computed(() => { this.addComputables({
const p = this.progress(); progressText: () => {
return 0 === p ? '' : '' + (98 < p ? 100 : p) + '%'; const p = this.progress();
}); return 0 === p ? '' : '' + (98 < p ? 100 : p) + '%';
},
this.progressStyle = ko.computed(() => { progressStyle: () => {
const p = this.progress(); const p = this.progress();
return 0 === p ? '' : 'width:' + (98 < p ? 100 : p) + '%'; return 0 === p ? '' : 'width:' + (98 < p ? 100 : p) + '%';
}); },
this.title = ko.computed(() => { title: () => this.error() || this.fileName(),
return this.error() || this.fileName();
});
this.friendlySize = ko.computed(() => { friendlySize: () => {
const localSize = this.size(); const localSize = this.size();
return null === localSize ? '' : File.friendlySize(localSize); return null === localSize ? '' : File.friendlySize(localSize);
}); },
this.mimeType = ko.computed(() => File.getContentType(this.fileName())); mimeType: () => File.getContentType(this.fileName()),
this.fileExt = ko.computed(() => File.getExtension(this.fileName())); fileExt: () => File.getExtension(this.fileName())
});
} }
static fromAttachment(item) static fromAttachment(item)

View file

@ -1,5 +1,3 @@
import ko from 'ko';
import { ContactPropertyType } from 'Common/Enums'; import { ContactPropertyType } from 'Common/Enums';
import { pInt, pString } from 'Common/Utils'; import { pInt, pString } from 'Common/Utils';
import { i18n } from 'Common/Translator'; import { i18n } from 'Common/Translator';
@ -26,12 +24,14 @@ class ContactPropertyModel extends AbstractModel {
placeholder: placeholder placeholder: placeholder
}); });
this.placeholderValue = ko.computed(() => { this.addComputables({
const v = this.placeholder(); placeholderValue: () => {
return v ? i18n(v) : ''; const v = this.placeholder();
}); return v ? i18n(v) : '';
},
this.largeValue = ko.computed(() => ContactPropertyType.Note === this.type()); largeValue: () => ContactPropertyType.Note === this.type()
});
} }
toJSON() { toJSON() {

View file

@ -53,62 +53,64 @@ class FilterModel extends AbstractModel {
return folder ? folder.fullName.replace('.' === folder.delimiter ? /\./ : /[\\/]+/, ' / ') : folderFullNameRaw; return folder ? folder.fullName.replace('.' === folder.delimiter ? /\./ : /[\\/]+/, ' / ') : folderFullNameRaw;
}; };
this.nameSub = ko.computed(() => { this.addComputables({
let result = ''; nameSub: () => {
const actionValue = this.actionValue(); let result = '';
const actionValue = this.actionValue();
switch (this.actionType()) { switch (this.actionType()) {
case FiltersAction.MoveTo: case FiltersAction.MoveTo:
result = i18n('SETTINGS_FILTERS/SUBNAME_MOVE_TO', { result = i18n('SETTINGS_FILTERS/SUBNAME_MOVE_TO', {
FOLDER: fGetRealFolderName(actionValue) FOLDER: fGetRealFolderName(actionValue)
}); });
break; break;
case FiltersAction.Forward: case FiltersAction.Forward:
result = i18n('SETTINGS_FILTERS/SUBNAME_FORWARD_TO', { result = i18n('SETTINGS_FILTERS/SUBNAME_FORWARD_TO', {
EMAIL: actionValue EMAIL: actionValue
}); });
break; break;
case FiltersAction.Vacation: case FiltersAction.Vacation:
result = i18n('SETTINGS_FILTERS/SUBNAME_VACATION_MESSAGE'); result = i18n('SETTINGS_FILTERS/SUBNAME_VACATION_MESSAGE');
break; break;
case FiltersAction.Reject: case FiltersAction.Reject:
result = i18n('SETTINGS_FILTERS/SUBNAME_REJECT'); result = i18n('SETTINGS_FILTERS/SUBNAME_REJECT');
break; break;
case FiltersAction.Discard: case FiltersAction.Discard:
result = i18n('SETTINGS_FILTERS/SUBNAME_DISCARD'); result = i18n('SETTINGS_FILTERS/SUBNAME_DISCARD');
break; break;
// no default // no default
}
return result ? '(' + result + ')' : '';
},
actionTemplate: () => {
let result = '';
switch (this.actionType()) {
case FiltersAction.Forward:
result = 'SettingsFiltersActionForward';
break;
case FiltersAction.Vacation:
result = 'SettingsFiltersActionVacation';
break;
case FiltersAction.Reject:
result = 'SettingsFiltersActionReject';
break;
case FiltersAction.None:
result = 'SettingsFiltersActionNone';
break;
case FiltersAction.Discard:
result = 'SettingsFiltersActionDiscard';
break;
case FiltersAction.MoveTo:
default:
result = 'SettingsFiltersActionMoveToFolder';
break;
}
return result;
} }
return result ? '(' + result + ')' : '';
});
this.actionTemplate = ko.computed(() => {
let result = '';
switch (this.actionType()) {
case FiltersAction.Forward:
result = 'SettingsFiltersActionForward';
break;
case FiltersAction.Vacation:
result = 'SettingsFiltersActionVacation';
break;
case FiltersAction.Reject:
result = 'SettingsFiltersActionReject';
break;
case FiltersAction.None:
result = 'SettingsFiltersActionNone';
break;
case FiltersAction.Discard:
result = 'SettingsFiltersActionDiscard';
break;
case FiltersAction.MoveTo:
default:
result = 'SettingsFiltersActionMoveToFolder';
break;
}
return result;
}); });
this.addSubscribables({ this.addSubscribables({

View file

@ -59,37 +59,7 @@ class FolderModel extends AbstractModel {
folder.subScribed(!!json.IsSubscribed); folder.subScribed(!!json.IsSubscribed);
folder.isInbox = ko.computed(() => FolderType.Inbox === folder.type()); folder.messageCountAll = ko.computed({
folder.hasSubScribedSubfolders = ko.computed(
() =>
!!folder.subFolders().find(
oFolder => (oFolder.subScribed() || oFolder.hasSubScribedSubfolders()) && !oFolder.isSystemFolder()
)
);
folder.canBeEdited = ko.computed(() => FolderType.User === folder.type() && folder.existen && folder.selectable);
folder.visible = ko.computed(() => {
const isSubScribed = folder.subScribed(),
isSubFolders = folder.hasSubScribedSubfolders();
return isSubScribed || (isSubFolders && (!folder.existen || !folder.selectable));
});
folder.isSystemFolder = ko.computed(() => FolderType.User !== folder.type());
folder.hidden = ko.computed(() => {
const isSystem = folder.isSystemFolder(),
isSubFolders = folder.hasSubScribedSubfolders();
return (isSystem && !isSubFolders) || (!folder.selectable && !isSubFolders);
});
folder.selectableForFolderList = ko.computed(() => !folder.isSystemFolder() && folder.selectable);
folder.messageCountAll = ko
.computed({
read: folder.privateMessageCountAll, read: folder.privateMessageCountAll,
write: (iValue) => { write: (iValue) => {
if (isPosNumeric(iValue, true)) { if (isPosNumeric(iValue, true)) {
@ -101,8 +71,7 @@ class FolderModel extends AbstractModel {
}) })
.extend({ notify: 'always' }); .extend({ notify: 'always' });
folder.messageCountUnread = ko folder.messageCountUnread = ko.computed({
.computed({
read: folder.privateMessageCountUnread, read: folder.privateMessageCountUnread,
write: (value) => { write: (value) => {
if (isPosNumeric(value, true)) { if (isPosNumeric(value, true)) {
@ -114,125 +83,150 @@ class FolderModel extends AbstractModel {
}) })
.extend({ notify: 'always' }); .extend({ notify: 'always' });
folder.printableUnreadCount = ko.computed(() => { folder.addComputables({
const count = folder.messageCountAll(),
unread = folder.messageCountUnread(),
type = folder.type();
if (0 < count) { isInbox: () => FolderType.Inbox === folder.type(),
if (FolderType.Draft === type) {
return '' + count; hasSubScribedSubfolders:
() =>
!!folder.subFolders().find(
oFolder => (oFolder.subScribed() || oFolder.hasSubScribedSubfolders()) && !oFolder.isSystemFolder()
),
canBeEdited: () => FolderType.User === folder.type() && folder.existen && folder.selectable,
visible: () => {
const isSubScribed = folder.subScribed(),
isSubFolders = folder.hasSubScribedSubfolders();
return isSubScribed || (isSubFolders && (!folder.existen || !folder.selectable));
},
isSystemFolder: () => FolderType.User !== folder.type(),
hidden: () => {
const isSystem = folder.isSystemFolder(),
isSubFolders = folder.hasSubScribedSubfolders();
return (isSystem && !isSubFolders) || (!folder.selectable && !isSubFolders);
},
printableUnreadCount: () => {
const count = folder.messageCountAll(),
unread = folder.messageCountUnread(),
type = folder.type();
if (0 < count) {
if (FolderType.Draft === type) {
return '' + count;
}
if (
0 < unread &&
FolderType.Trash !== type &&
FolderType.Archive !== type &&
FolderType.SentItems !== type
) {
return '' + unread;
}
} }
if (
0 < unread && return '';
FolderType.Trash !== type && },
FolderType.Archive !== type &&
FolderType.SentItems !== type canBeDeleted: () => !folder.isSystemFolder() && !folder.subFolders().length,
) {
return '' + unread; selectableForFolderList: () => !folder.isSystemFolder() && folder.selectable,
canBeSubScribed: () => !folder.isSystemFolder() && folder.selectable,
canBeChecked: () => !folder.isSystemFolder() && folder.selectable,
localName: () => {
translatorTrigger();
let name = folder.name();
const type = folder.type();
if (folder.isSystemFolder()) {
switch (type) {
case FolderType.Inbox:
name = i18n('FOLDER_LIST/INBOX_NAME');
break;
case FolderType.SentItems:
name = i18n('FOLDER_LIST/SENT_NAME');
break;
case FolderType.Draft:
name = i18n('FOLDER_LIST/DRAFTS_NAME');
break;
case FolderType.Spam:
name = i18n('FOLDER_LIST/SPAM_NAME');
break;
case FolderType.Trash:
name = i18n('FOLDER_LIST/TRASH_NAME');
break;
case FolderType.Archive:
name = i18n('FOLDER_LIST/ARCHIVE_NAME');
break;
// no default
}
} }
}
return ''; return name;
}); },
folder.canBeDeleted = ko.computed( manageFolderSystemName: () => {
() => !folder.isSystemFolder() && !folder.subFolders().length translatorTrigger();
);
folder.canBeSubScribed = ko.computed( let suffix = '';
() => !folder.isSystemFolder() && folder.selectable const type = folder.type(),
); name = folder.name();
folder.canBeChecked = folder.canBeSubScribed; if (folder.isSystemFolder()) {
switch (type) {
folder.localName = ko.computed(() => { case FolderType.Inbox:
translatorTrigger(); suffix = '(' + i18n('FOLDER_LIST/INBOX_NAME') + ')';
break;
let name = folder.name(); case FolderType.SentItems:
const type = folder.type(); suffix = '(' + i18n('FOLDER_LIST/SENT_NAME') + ')';
break;
if (folder.isSystemFolder()) { case FolderType.Draft:
switch (type) { suffix = '(' + i18n('FOLDER_LIST/DRAFTS_NAME') + ')';
case FolderType.Inbox: break;
name = i18n('FOLDER_LIST/INBOX_NAME'); case FolderType.Spam:
break; suffix = '(' + i18n('FOLDER_LIST/SPAM_NAME') + ')';
case FolderType.SentItems: break;
name = i18n('FOLDER_LIST/SENT_NAME'); case FolderType.Trash:
break; suffix = '(' + i18n('FOLDER_LIST/TRASH_NAME') + ')';
case FolderType.Draft: break;
name = i18n('FOLDER_LIST/DRAFTS_NAME'); case FolderType.Archive:
break; suffix = '(' + i18n('FOLDER_LIST/ARCHIVE_NAME') + ')';
case FolderType.Spam: break;
name = i18n('FOLDER_LIST/SPAM_NAME'); // no default
break; }
case FolderType.Trash:
name = i18n('FOLDER_LIST/TRASH_NAME');
break;
case FolderType.Archive:
name = i18n('FOLDER_LIST/ARCHIVE_NAME');
break;
// no default
} }
}
return name; if ((suffix && '(' + name + ')' === suffix) || '(inbox)' === suffix.toLowerCase()) {
}); suffix = '';
folder.manageFolderSystemName = ko.computed(() => {
translatorTrigger();
let suffix = '';
const type = folder.type(),
name = folder.name();
if (folder.isSystemFolder()) {
switch (type) {
case FolderType.Inbox:
suffix = '(' + i18n('FOLDER_LIST/INBOX_NAME') + ')';
break;
case FolderType.SentItems:
suffix = '(' + i18n('FOLDER_LIST/SENT_NAME') + ')';
break;
case FolderType.Draft:
suffix = '(' + i18n('FOLDER_LIST/DRAFTS_NAME') + ')';
break;
case FolderType.Spam:
suffix = '(' + i18n('FOLDER_LIST/SPAM_NAME') + ')';
break;
case FolderType.Trash:
suffix = '(' + i18n('FOLDER_LIST/TRASH_NAME') + ')';
break;
case FolderType.Archive:
suffix = '(' + i18n('FOLDER_LIST/ARCHIVE_NAME') + ')';
break;
// no default
} }
}
if ((suffix && '(' + name + ')' === suffix) || '(inbox)' === suffix.toLowerCase()) { return suffix;
suffix = ''; },
}
return suffix; collapsed: {
read: () => !folder.hidden() && folder.collapsedPrivate(),
write: (value) => {
folder.collapsedPrivate(value);
}
},
hasUnreadMessages: () => 0 < folder.messageCountUnread() && folder.printableUnreadCount(),
hasSubScribedUnreadMessagesSubfolders: () =>
!!folder.subFolders().find(
folder => folder.hasUnreadMessages() || folder.hasSubScribedUnreadMessagesSubfolders()
)
}); });
folder.collapsed = ko.computed({
read: () => !folder.hidden() && folder.collapsedPrivate(),
write: (value) => {
folder.collapsedPrivate(value);
}
});
folder.hasUnreadMessages = ko.computed(() => 0 < folder.messageCountUnread() && folder.printableUnreadCount());
folder.hasSubScribedUnreadMessagesSubfolders = ko.computed(
() =>
!!folder.subFolders().find(
folder => folder.hasUnreadMessages() || folder.hasSubScribedUnreadMessagesSubfolders()
)
);
folder.addSubscribables({ folder.addSubscribables({
name: value => folder.nameForEdit(value), name: value => folder.nameForEdit(value),

View file

@ -71,16 +71,15 @@ class MessageModel extends AbstractModel {
hasFlaggedSubMessage: false hasFlaggedSubMessage: false
}); });
this.attachmentIconClass = ko.computed(() =>
File.getCombinedIconClass(this.hasAttachments() ? this.attachmentsSpecData() : [])
);
this.attachments = ko.observableArray(new AttachmentCollectionModel); this.attachments = ko.observableArray(new AttachmentCollectionModel);
this.attachmentsSpecData = ko.observableArray([]); this.attachmentsSpecData = ko.observableArray([]);
this.threads = ko.observableArray([]); this.threads = ko.observableArray([]);
this.threadsLen = ko.computed(() => this.threads().length); this.addComputables({
this.isImportant = ko.computed(() => MessagePriority.High === this.priority()); attachmentIconClass: () => File.getCombinedIconClass(this.hasAttachments() ? this.attachmentsSpecData() : []),
threadsLen: () => this.threads().length,
isImportant: () => MessagePriority.High === this.priority(),
});
} }
_reset() { _reset() {