snappymail/dev/Model/Folder.js

298 lines
7.5 KiB
JavaScript
Raw Normal View History

2016-07-07 05:03:30 +08:00
import ko from 'ko';
2019-07-05 03:19:24 +08:00
import { FolderType } from 'Common/Enums';
import { isPosNumeric } from 'Common/Utils';
import { i18n, trigger as translatorTrigger } from 'Common/Translator';
import { getFolderInboxName } from 'Common/Cache';
2016-07-07 05:03:30 +08:00
2019-07-05 03:19:24 +08:00
import { AbstractModel } from 'Knoin/AbstractModel';
2016-07-07 05:03:30 +08:00
2019-07-05 03:19:24 +08:00
class FolderModel extends AbstractModel {
2016-07-16 05:29:42 +08:00
constructor() {
2016-07-07 05:03:30 +08:00
super('FolderModel');
this.name = ko.observable('');
this.fullName = '';
this.fullNameRaw = '';
this.fullNameHash = '';
this.delimiter = '';
this.namespace = '';
this.deep = 0;
this.interval = 0;
this.selectable = false;
this.existen = true;
this.type = ko.observable(FolderType.User);
this.focused = ko.observable(false);
this.selected = ko.observable(false);
this.edited = ko.observable(false);
this.subScribed = ko.observable(true);
this.checkable = ko.observable(false);
this.subFolders = ko.observableArray([]);
this.deleteAccess = ko.observable(false);
2019-07-05 03:19:24 +08:00
this.actionBlink = ko.observable(false).extend({ falseTimeout: 1000 });
2016-07-07 05:03:30 +08:00
this.nameForEdit = ko.observable('');
this.privateMessageCountAll = ko.observable(0);
this.privateMessageCountUnread = ko.observable(0);
this.collapsedPrivate = ko.observable(true);
}
/**
* @static
* @param {AjaxJsonFolder} json
* @returns {?FolderModel}
*/
static newInstanceFromJson(json) {
const folder = new FolderModel();
return folder.initByJson(json) ? folder.initComputed() : null;
}
/**
* @returns {FolderModel}
*/
initComputed() {
const inboxFolderName = getFolderInboxName();
this.isInbox = ko.computed(() => FolderType.Inbox === this.type());
this.hasSubScribedSubfolders = ko.computed(
2019-07-05 03:19:24 +08:00
() =>
!!this.subFolders().find(
oFolder => (oFolder.subScribed() || oFolder.hasSubScribedSubfolders()) && !oFolder.isSystemFolder()
2019-07-05 03:19:24 +08:00
)
);
2016-07-07 05:03:30 +08:00
this.canBeEdited = ko.computed(() => FolderType.User === this.type() && this.existen && this.selectable);
this.visible = ko.computed(() => {
2019-07-05 03:19:24 +08:00
const isSubScribed = this.subScribed(),
2016-07-07 05:03:30 +08:00
isSubFolders = this.hasSubScribedSubfolders();
2019-07-05 03:19:24 +08:00
return isSubScribed || (isSubFolders && (!this.existen || !this.selectable));
2016-07-07 05:03:30 +08:00
});
this.isSystemFolder = ko.computed(() => FolderType.User !== this.type());
this.hidden = ko.computed(() => {
2019-07-05 03:19:24 +08:00
const isSystem = this.isSystemFolder(),
2016-07-07 05:03:30 +08:00
isSubFolders = this.hasSubScribedSubfolders();
return (isSystem && !isSubFolders) || (!this.selectable && !isSubFolders);
});
this.selectableForFolderList = ko.computed(() => !this.isSystemFolder() && this.selectable);
2019-07-05 03:19:24 +08:00
this.messageCountAll = ko
.computed({
read: this.privateMessageCountAll,
write: (iValue) => {
if (isPosNumeric(iValue, true)) {
this.privateMessageCountAll(iValue);
} else {
this.privateMessageCountAll.valueHasMutated();
}
2016-07-07 05:03:30 +08:00
}
2019-07-05 03:19:24 +08:00
})
.extend({ notify: 'always' });
this.messageCountUnread = ko
.computed({
read: this.privateMessageCountUnread,
write: (value) => {
if (isPosNumeric(value, true)) {
this.privateMessageCountUnread(value);
} else {
this.privateMessageCountUnread.valueHasMutated();
}
2016-07-07 05:03:30 +08:00
}
2019-07-05 03:19:24 +08:00
})
.extend({ notify: 'always' });
2016-07-07 05:03:30 +08:00
this.printableUnreadCount = ko.computed(() => {
2019-07-05 03:19:24 +08:00
const count = this.messageCountAll(),
2016-07-07 05:03:30 +08:00
unread = this.messageCountUnread(),
type = this.type();
2019-07-05 03:19:24 +08:00
if (0 < count) {
if (FolderType.Draft === type) {
2016-07-07 05:03:30 +08:00
return '' + count;
2019-07-05 03:19:24 +08:00
} else if (
0 < unread &&
FolderType.Trash !== type &&
FolderType.Archive !== type &&
FolderType.SentItems !== type
) {
2016-07-07 05:03:30 +08:00
return '' + unread;
}
}
return '';
});
this.canBeDeleted = ko.computed(() => {
const bSystem = this.isSystemFolder();
return !bSystem && !this.subFolders().length && inboxFolderName !== this.fullNameRaw;
2016-07-07 05:03:30 +08:00
});
2019-07-05 03:19:24 +08:00
this.canBeSubScribed = ko.computed(
() => !this.isSystemFolder() && this.selectable && inboxFolderName !== this.fullNameRaw
);
2016-07-07 05:03:30 +08:00
this.canBeChecked = this.canBeSubScribed;
this.localName = ko.computed(() => {
translatorTrigger();
let name = this.name();
const type = this.type();
2019-07-05 03:19:24 +08:00
if (this.isSystemFolder()) {
switch (type) {
2016-07-07 05:03:30 +08:00
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 name;
});
this.manageFolderSystemName = ko.computed(() => {
translatorTrigger();
let suffix = '';
2019-07-05 03:19:24 +08:00
const type = this.type(),
2016-07-07 05:03:30 +08:00
name = this.name();
2019-07-05 03:19:24 +08:00
if (this.isSystemFolder()) {
switch (type) {
2016-07-07 05:03:30 +08:00
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()) {
2016-07-07 05:03:30 +08:00
suffix = '';
}
return suffix;
});
this.collapsed = ko.computed({
read: () => !this.hidden() && this.collapsedPrivate(),
write: (value) => {
this.collapsedPrivate(value);
}
});
this.hasUnreadMessages = ko.computed(() => 0 < this.messageCountUnread() && this.printableUnreadCount());
2016-07-07 05:03:30 +08:00
this.hasSubScribedUnreadMessagesSubfolders = ko.computed(
2019-07-05 03:19:24 +08:00
() =>
!!this.subFolders().find(
folder => folder.hasUnreadMessages() || folder.hasSubScribedUnreadMessagesSubfolders()
2019-07-05 03:19:24 +08:00
)
);
2016-07-07 05:03:30 +08:00
// subscribe
this.name.subscribe(value => this.nameForEdit(value));
2016-07-07 05:03:30 +08:00
this.edited.subscribe(value => value && this.nameForEdit(this.name()));
2016-07-07 05:03:30 +08:00
this.messageCountUnread.subscribe((unread) => {
2019-07-05 03:19:24 +08:00
if (FolderType.Inbox === this.type()) {
dispatchEvent(new CustomEvent('mailbox.inbox-unread-count', {detail:unread}));
2016-07-07 05:03:30 +08:00
}
});
return this;
}
/**
* @returns {string}
*/
collapsedCss() {
2019-07-05 03:19:24 +08:00
return this.hasSubScribedSubfolders()
? this.collapsed()
? 'icon-right-mini e-collapsed-sign'
: 'icon-down-mini e-collapsed-sign'
: 'icon-none e-collapsed-sign';
2016-07-07 05:03:30 +08:00
}
/**
* @param {AjaxJsonFolder} json
* @returns {boolean}
*/
initByJson(json) {
let bResult = false;
const sInboxFolderName = getFolderInboxName();
2019-07-05 03:19:24 +08:00
if (json && 'Object/Folder' === json['@Object']) {
2016-07-07 05:03:30 +08:00
this.name(json.Name);
this.delimiter = json.Delimiter;
this.fullName = json.FullName;
this.fullNameRaw = json.FullNameRaw;
this.fullNameHash = json.FullNameHash;
this.deep = json.FullNameRaw.split(this.delimiter).length - 1;
this.selectable = !!json.IsSelectable;
this.existen = !!json.IsExists;
this.subScribed(!!json.IsSubscribed);
this.checkable(!!json.Checkable);
this.type(sInboxFolderName === this.fullNameRaw ? FolderType.Inbox : FolderType.User);
bResult = true;
}
return bResult;
}
/**
* @returns {string}
*/
printableFullName() {
return this.fullName.split(this.delimiter).join(' / ');
}
}
2019-07-05 03:19:24 +08:00
export { FolderModel, FolderModel as default };