mirror of
https://github.com/the-djmaze/snappymail.git
synced 2026-08-29 20:19:22 +03:00
Added addComputables() to *Model
This commit is contained in:
parent
22f606ea75
commit
b165a1de4f
6 changed files with 221 additions and 223 deletions
|
|
@ -41,6 +41,10 @@ export class AbstractModel {
|
|||
*/
|
||||
}
|
||||
|
||||
addComputables(obj) {
|
||||
Object.entries(obj).forEach(([key, fn]) => this[key] = ko.computed(fn) );
|
||||
}
|
||||
|
||||
addSubscribables(obj) {
|
||||
Object.entries(obj).forEach(([key, fn]) => this.disposables.push( this[key].subscribe(fn) ) );
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,4 +1,3 @@
|
|||
import ko from 'ko';
|
||||
import { pInt } from 'Common/Utils';
|
||||
import { File } from 'Common/File';
|
||||
|
||||
|
|
@ -37,27 +36,27 @@ class ComposeAttachmentModel extends AbstractModel {
|
|||
complete: false
|
||||
});
|
||||
|
||||
this.progressText = ko.computed(() => {
|
||||
const p = this.progress();
|
||||
return 0 === p ? '' : '' + (98 < p ? 100 : p) + '%';
|
||||
});
|
||||
this.addComputables({
|
||||
progressText: () => {
|
||||
const p = this.progress();
|
||||
return 0 === p ? '' : '' + (98 < p ? 100 : p) + '%';
|
||||
},
|
||||
|
||||
this.progressStyle = ko.computed(() => {
|
||||
const p = this.progress();
|
||||
return 0 === p ? '' : 'width:' + (98 < p ? 100 : p) + '%';
|
||||
});
|
||||
progressStyle: () => {
|
||||
const p = this.progress();
|
||||
return 0 === p ? '' : 'width:' + (98 < p ? 100 : p) + '%';
|
||||
},
|
||||
|
||||
this.title = ko.computed(() => {
|
||||
return this.error() || this.fileName();
|
||||
});
|
||||
title: () => this.error() || this.fileName(),
|
||||
|
||||
this.friendlySize = ko.computed(() => {
|
||||
const localSize = this.size();
|
||||
return null === localSize ? '' : File.friendlySize(localSize);
|
||||
});
|
||||
friendlySize: () => {
|
||||
const localSize = this.size();
|
||||
return null === localSize ? '' : File.friendlySize(localSize);
|
||||
},
|
||||
|
||||
this.mimeType = ko.computed(() => File.getContentType(this.fileName()));
|
||||
this.fileExt = ko.computed(() => File.getExtension(this.fileName()));
|
||||
mimeType: () => File.getContentType(this.fileName()),
|
||||
fileExt: () => File.getExtension(this.fileName())
|
||||
});
|
||||
}
|
||||
|
||||
static fromAttachment(item)
|
||||
|
|
|
|||
|
|
@ -1,5 +1,3 @@
|
|||
import ko from 'ko';
|
||||
|
||||
import { ContactPropertyType } from 'Common/Enums';
|
||||
import { pInt, pString } from 'Common/Utils';
|
||||
import { i18n } from 'Common/Translator';
|
||||
|
|
@ -26,12 +24,14 @@ class ContactPropertyModel extends AbstractModel {
|
|||
placeholder: placeholder
|
||||
});
|
||||
|
||||
this.placeholderValue = ko.computed(() => {
|
||||
const v = this.placeholder();
|
||||
return v ? i18n(v) : '';
|
||||
});
|
||||
this.addComputables({
|
||||
placeholderValue: () => {
|
||||
const v = this.placeholder();
|
||||
return v ? i18n(v) : '';
|
||||
},
|
||||
|
||||
this.largeValue = ko.computed(() => ContactPropertyType.Note === this.type());
|
||||
largeValue: () => ContactPropertyType.Note === this.type()
|
||||
});
|
||||
}
|
||||
|
||||
toJSON() {
|
||||
|
|
|
|||
|
|
@ -53,62 +53,64 @@ class FilterModel extends AbstractModel {
|
|||
return folder ? folder.fullName.replace('.' === folder.delimiter ? /\./ : /[\\/]+/, ' / ') : folderFullNameRaw;
|
||||
};
|
||||
|
||||
this.nameSub = ko.computed(() => {
|
||||
let result = '';
|
||||
const actionValue = this.actionValue();
|
||||
this.addComputables({
|
||||
nameSub: () => {
|
||||
let result = '';
|
||||
const actionValue = this.actionValue();
|
||||
|
||||
switch (this.actionType()) {
|
||||
case FiltersAction.MoveTo:
|
||||
result = i18n('SETTINGS_FILTERS/SUBNAME_MOVE_TO', {
|
||||
FOLDER: fGetRealFolderName(actionValue)
|
||||
});
|
||||
break;
|
||||
case FiltersAction.Forward:
|
||||
result = i18n('SETTINGS_FILTERS/SUBNAME_FORWARD_TO', {
|
||||
EMAIL: actionValue
|
||||
});
|
||||
break;
|
||||
case FiltersAction.Vacation:
|
||||
result = i18n('SETTINGS_FILTERS/SUBNAME_VACATION_MESSAGE');
|
||||
break;
|
||||
case FiltersAction.Reject:
|
||||
result = i18n('SETTINGS_FILTERS/SUBNAME_REJECT');
|
||||
break;
|
||||
case FiltersAction.Discard:
|
||||
result = i18n('SETTINGS_FILTERS/SUBNAME_DISCARD');
|
||||
break;
|
||||
// no default
|
||||
switch (this.actionType()) {
|
||||
case FiltersAction.MoveTo:
|
||||
result = i18n('SETTINGS_FILTERS/SUBNAME_MOVE_TO', {
|
||||
FOLDER: fGetRealFolderName(actionValue)
|
||||
});
|
||||
break;
|
||||
case FiltersAction.Forward:
|
||||
result = i18n('SETTINGS_FILTERS/SUBNAME_FORWARD_TO', {
|
||||
EMAIL: actionValue
|
||||
});
|
||||
break;
|
||||
case FiltersAction.Vacation:
|
||||
result = i18n('SETTINGS_FILTERS/SUBNAME_VACATION_MESSAGE');
|
||||
break;
|
||||
case FiltersAction.Reject:
|
||||
result = i18n('SETTINGS_FILTERS/SUBNAME_REJECT');
|
||||
break;
|
||||
case FiltersAction.Discard:
|
||||
result = i18n('SETTINGS_FILTERS/SUBNAME_DISCARD');
|
||||
break;
|
||||
// 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({
|
||||
|
|
|
|||
|
|
@ -59,37 +59,7 @@ class FolderModel extends AbstractModel {
|
|||
|
||||
folder.subScribed(!!json.IsSubscribed);
|
||||
|
||||
folder.isInbox = ko.computed(() => FolderType.Inbox === folder.type());
|
||||
|
||||
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({
|
||||
folder.messageCountAll = ko.computed({
|
||||
read: folder.privateMessageCountAll,
|
||||
write: (iValue) => {
|
||||
if (isPosNumeric(iValue, true)) {
|
||||
|
|
@ -101,8 +71,7 @@ class FolderModel extends AbstractModel {
|
|||
})
|
||||
.extend({ notify: 'always' });
|
||||
|
||||
folder.messageCountUnread = ko
|
||||
.computed({
|
||||
folder.messageCountUnread = ko.computed({
|
||||
read: folder.privateMessageCountUnread,
|
||||
write: (value) => {
|
||||
if (isPosNumeric(value, true)) {
|
||||
|
|
@ -114,125 +83,150 @@ class FolderModel extends AbstractModel {
|
|||
})
|
||||
.extend({ notify: 'always' });
|
||||
|
||||
folder.printableUnreadCount = ko.computed(() => {
|
||||
const count = folder.messageCountAll(),
|
||||
unread = folder.messageCountUnread(),
|
||||
type = folder.type();
|
||||
folder.addComputables({
|
||||
|
||||
if (0 < count) {
|
||||
if (FolderType.Draft === type) {
|
||||
return '' + count;
|
||||
isInbox: () => FolderType.Inbox === folder.type(),
|
||||
|
||||
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 &&
|
||||
FolderType.Trash !== type &&
|
||||
FolderType.Archive !== type &&
|
||||
FolderType.SentItems !== type
|
||||
) {
|
||||
return '' + unread;
|
||||
|
||||
return '';
|
||||
},
|
||||
|
||||
canBeDeleted: () => !folder.isSystemFolder() && !folder.subFolders().length,
|
||||
|
||||
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(
|
||||
() => !folder.isSystemFolder() && !folder.subFolders().length
|
||||
);
|
||||
manageFolderSystemName: () => {
|
||||
translatorTrigger();
|
||||
|
||||
folder.canBeSubScribed = ko.computed(
|
||||
() => !folder.isSystemFolder() && folder.selectable
|
||||
);
|
||||
let suffix = '';
|
||||
const type = folder.type(),
|
||||
name = folder.name();
|
||||
|
||||
folder.canBeChecked = folder.canBeSubScribed;
|
||||
|
||||
folder.localName = ko.computed(() => {
|
||||
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
|
||||
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
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return name;
|
||||
});
|
||||
|
||||
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()) {
|
||||
suffix = '';
|
||||
}
|
||||
}
|
||||
|
||||
if ((suffix && '(' + name + ')' === suffix) || '(inbox)' === suffix.toLowerCase()) {
|
||||
suffix = '';
|
||||
}
|
||||
return 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({
|
||||
name: value => folder.nameForEdit(value),
|
||||
|
||||
|
|
|
|||
|
|
@ -71,16 +71,15 @@ class MessageModel extends AbstractModel {
|
|||
hasFlaggedSubMessage: false
|
||||
});
|
||||
|
||||
this.attachmentIconClass = ko.computed(() =>
|
||||
File.getCombinedIconClass(this.hasAttachments() ? this.attachmentsSpecData() : [])
|
||||
);
|
||||
|
||||
this.attachments = ko.observableArray(new AttachmentCollectionModel);
|
||||
this.attachmentsSpecData = ko.observableArray([]);
|
||||
this.threads = ko.observableArray([]);
|
||||
|
||||
this.threadsLen = ko.computed(() => this.threads().length);
|
||||
this.isImportant = ko.computed(() => MessagePriority.High === this.priority());
|
||||
this.addComputables({
|
||||
attachmentIconClass: () => File.getCombinedIconClass(this.hasAttachments() ? this.attachmentsSpecData() : []),
|
||||
threadsLen: () => this.threads().length,
|
||||
isImportant: () => MessagePriority.High === this.priority(),
|
||||
});
|
||||
}
|
||||
|
||||
_reset() {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue