snappymail/dev/View/User/MailBox/FolderList.js

227 lines
6.1 KiB
JavaScript
Raw Normal View History

import ko from 'ko';
import { Capa, Focused, Layout, KeyState } from 'Common/Enums';
import { leftPanelDisabled, moveAction } from 'Common/Globals';
2019-07-05 03:19:24 +08:00
import { mailBox, settings } from 'Common/Links';
import { setFolderHash } from 'Common/Cache';
import AppStore from 'Stores/User/App';
import SettingsStore from 'Stores/User/Settings';
import FolderStore from 'Stores/User/Folder';
import MessageStore from 'Stores/User/Message';
import { view, ViewType, showScreenPopup } from 'Knoin/Knoin';
2019-07-05 03:19:24 +08:00
import { AbstractViewNext } from 'Knoin/AbstractViewNext';
const Settings = rl.settings;
@view({
name: 'View/User/MailBox/FolderList',
type: ViewType.Left,
templateID: 'MailFolderList'
})
2019-07-05 03:19:24 +08:00
class FolderListMailBoxUserView extends AbstractViewNext {
constructor() {
super();
this.oContentScrollable = null;
this.composeInEdit = AppStore.composeInEdit;
this.messageList = MessageStore.messageList;
this.folderList = FolderStore.folderList;
this.folderListSystem = FolderStore.folderListSystem;
this.foldersChanging = FolderStore.foldersChanging;
2017-02-09 01:48:53 +08:00
this.moveAction = moveAction;
this.foldersListWithSingleInboxRootFolder = FolderStore.foldersListWithSingleInboxRootFolder;
this.leftPanelDisabled = leftPanelDisabled;
2016-04-29 04:32:54 +08:00
this.allowComposer = !!Settings.capa(Capa.Composer);
this.allowContacts = !!AppStore.contactsIsAllowed();
this.allowFolders = !!Settings.capa(Capa.Folders);
this.folderListFocused = ko.computed(() => Focused.FolderList === AppStore.focusedState());
this.isInboxStarred = ko.computed(
2019-07-05 03:19:24 +08:00
() =>
FolderStore.currentFolder() &&
FolderStore.currentFolder().isInbox() &&
MessageStore.messageListSearch().trim().includes('is:flagged')
);
}
2014-08-21 23:08:34 +08:00
onBuild(dom) {
const qs = s => dom.querySelector(s),
eqs = (ev, s) => ev.target.closestWithin(s, dom),
isMobile = Settings.app('mobile'),
fSelectFolder = (el, event, starred) => {
2017-02-09 01:48:53 +08:00
const isMove = moveAction();
2019-07-05 03:19:24 +08:00
if (isMobile) {
leftPanelDisabled(true);
}
2016-04-29 04:32:54 +08:00
event.preventDefault();
2016-04-29 04:32:54 +08:00
2019-07-05 03:19:24 +08:00
if (starred) {
event.stopPropagation();
}
const folder = ko.dataFor(el);
2019-07-05 03:19:24 +08:00
if (folder) {
if (isMove) {
2017-02-09 01:48:53 +08:00
moveAction(false);
2020-09-15 15:29:25 +08:00
rl.app.moveMessagesToFolder(
2017-02-09 01:48:53 +08:00
FolderStore.currentFolderFullNameRaw(),
MessageStore.messageListCheckedOrSelectedUidsWithSubMails(),
folder.fullNameRaw,
event.ctrlKey
2017-02-09 01:48:53 +08:00
);
2019-07-05 03:19:24 +08:00
} else {
if (Layout.NoPreview === SettingsStore.layout()) {
2017-02-09 01:48:53 +08:00
MessageStore.message(null);
}
2019-07-05 03:19:24 +08:00
if (folder.fullNameRaw === FolderStore.currentFolderFullNameRaw()) {
2017-02-09 01:48:53 +08:00
setFolderHash(folder.fullNameRaw, '');
}
rl.route.setHash(starred
? mailBox(folder.fullNameHash, 1, 'is:flagged')
: mailBox(folder.fullNameHash)
);
}
2017-02-09 01:48:53 +08:00
AppStore.focusedState(Focused.MessageList);
}
};
2020-08-27 21:45:47 +08:00
this.oContentScrollable = qs('.b-content');
dom.addEventListener('click', event => {
let el = event.target.closest('.e-collapsed-sign');
if (el) {
const folder = ko.dataFor(el);
if (folder) {
const collapsed = folder.collapsed();
2020-09-15 15:29:25 +08:00
rl.app.setExpandedFolder(folder.fullNameHash, collapsed);
folder.collapsed(!collapsed);
event.preventDefault();
event.stopPropagation();
return;
}
}
el = eqs(event, '.b-folders .e-item .e-link.selectable .inbox-star-icon');
el && fSelectFolder(el, event, !this.isInboxStarred());
el = eqs(event, '.b-folders .e-item .e-link.selectable');
el && fSelectFolder(el, event, false);
});
shortcuts.add('arrowup,arrowdown', '', KeyState.FolderList, event => {
let items = [], index = 0;
dom.querySelectorAll('.b-folders .e-item .e-link:not(.hidden)').forEach(node => {
if (node.offsetHeight || node.getClientRects().length) {
items.push(node);
if (node.matches('.focused')) {
node.classList.remove('focused');
index = items.length - 1;
2020-08-27 21:45:47 +08:00
}
2014-08-21 23:08:34 +08:00
}
});
if (items.length) {
if ('ArrowUp' === event.key) {
index && --index;
} else if (index < items.length - 1) {
2020-08-27 21:45:47 +08:00
++index;
2014-08-21 23:08:34 +08:00
}
items[index].classList.add('focused');
this.scrollToFocused();
}
2014-08-21 23:08:34 +08:00
return false;
});
2014-08-21 23:08:34 +08:00
shortcuts.add('enter,open', '', KeyState.FolderList, () => {
2020-08-27 21:45:47 +08:00
const item = qs('.b-folders .e-item .e-link:not(.hidden).focused');
if (item) {
AppStore.focusedState(Focused.MessageList);
2020-08-27 21:45:47 +08:00
item.click();
}
return false;
});
shortcuts.add('space', '', KeyState.FolderList, () => {
2020-08-27 21:45:47 +08:00
const item = qs('.b-folders .e-item .e-link:not(.hidden).focused'),
folder = item && ko.dataFor(item);
if (folder) {
const collapsed = folder.collapsed();
2020-09-15 15:29:25 +08:00
rl.app.setExpandedFolder(folder.fullNameHash, collapsed);
2020-08-27 21:45:47 +08:00
folder.collapsed(!collapsed);
2016-06-30 08:02:45 +08:00
}
return false;
});
// shortcuts.add('tab', 'shift', KeyState.FolderList, () => {
shortcuts.add('escape,tab,arrowright', '', KeyState.FolderList, () => {
AppStore.focusedState(Focused.MessageList);
2017-02-09 01:48:53 +08:00
moveAction(false);
return false;
});
2020-08-27 21:45:47 +08:00
AppStore.focusedState.subscribe(value => {
let el = qs('.b-folders .e-item .e-link.focused');
el && qs('.b-folders .e-item .e-link.focused').classList.remove('focused');
2019-07-05 03:19:24 +08:00
if (Focused.FolderList === value) {
2020-08-27 21:45:47 +08:00
el = qs('.b-folders .e-item .e-link.selected');
el && qs('.b-folders .e-item .e-link.selected').classList.add('focused');
}
});
}
scrollToFocused() {
2020-08-27 21:45:47 +08:00
const scrollable = this.oContentScrollable;
if (scrollable) {
let block, focused = scrollable.querySelector('.e-item .e-link.focused');
if (focused) {
const fRect = focused.getBoundingClientRect(),
sRect = scrollable.getBoundingClientRect();
if (fRect.top < sRect.top) {
block = 'start';
} else if (fRect.bottom > sRect.bottom) {
block = 'end';
}
block && focused.scrollIntoView(block === 'start');
2014-08-21 23:08:34 +08:00
}
}
2016-06-30 08:02:45 +08:00
}
composeClick() {
2019-07-05 03:19:24 +08:00
if (Settings.capa(Capa.Composer)) {
showScreenPopup(require('View/Popup/Compose'));
}
2016-06-30 08:02:45 +08:00
}
createFolder() {
showScreenPopup(require('View/Popup/FolderCreate'));
2016-06-30 08:02:45 +08:00
}
configureFolders() {
rl.route.setHash(settings('folders'));
}
2016-06-30 08:02:45 +08:00
contactsClick() {
2019-07-05 03:19:24 +08:00
if (this.allowContacts) {
showScreenPopup(require('View/Popup/Contacts'));
}
2016-06-30 08:02:45 +08:00
}
}
2019-07-05 03:19:24 +08:00
export { FolderListMailBoxUserView, FolderListMailBoxUserView as default };