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

307 lines
7.6 KiB
JavaScript
Raw Normal View History

2016-06-30 08:02:45 +08:00
var
window = require('window'),
_ = require('_'),
$ = require('$'),
ko = require('ko'),
key = require('key'),
2014-08-25 23:49:01 +08:00
2016-06-30 08:02:45 +08:00
Utils = require('Common/Utils'),
Enums = require('Common/Enums'),
Globals = require('Common/Globals'),
Links = require('Common/Links'),
2016-06-30 08:02:45 +08:00
Cache = require('Common/Cache'),
2016-06-30 08:02:45 +08:00
AppStore = require('Stores/User/App'),
SettingsStore = require('Stores/User/Settings'),
FolderStore = require('Stores/User/Folder'),
MessageStore = require('Stores/User/Message'),
2016-06-30 08:02:45 +08:00
Settings = require('Storage/Settings'),
2014-12-28 03:48:55 +08:00
2016-06-30 08:02:45 +08:00
kn = require('Knoin/Knoin'),
AbstractView = require('Knoin/AbstractView');
2016-06-30 08:02:45 +08:00
/**
* @constructor
* @extends AbstractView
*/
function FolderListMailBoxUserView()
{
AbstractView.call(this, 'Left', 'MailFolderList');
2016-06-30 08:02:45 +08:00
this.oContentVisible = null;
this.oContentScrollable = null;
2016-06-30 08:02:45 +08:00
this.composeInEdit = AppStore.composeInEdit;
2016-06-30 08:02:45 +08:00
this.messageList = MessageStore.messageList;
this.folderList = FolderStore.folderList;
this.folderListSystem = FolderStore.folderListSystem;
this.foldersChanging = FolderStore.foldersChanging;
2016-06-30 08:02:45 +08:00
this.foldersListWithSingleInboxRootFolder = FolderStore.foldersListWithSingleInboxRootFolder;
2016-06-30 08:02:45 +08:00
this.leftPanelDisabled = Globals.leftPanelDisabled;
2016-04-29 04:32:54 +08:00
2016-06-30 08:02:45 +08:00
this.iDropOverTimer = 0;
2016-06-30 08:02:45 +08:00
this.allowComposer = !!Settings.capa(Enums.Capa.Composer);
this.allowContacts = !!AppStore.contactsIsAllowed();
this.allowFolders = !!Settings.capa(Enums.Capa.Folders);
2016-06-30 08:02:45 +08:00
this.folderListFocused = ko.computed(function() {
return Enums.Focused.FolderList === AppStore.focusedState();
});
2016-06-30 08:02:45 +08:00
this.isInboxStarred = ko.computed(function() {
return FolderStore.currentFolder() &&
FolderStore.currentFolder().isInbox() &&
-1 < Utils.trim(MessageStore.messageListSearch()).indexOf('is:flagged');
});
2014-08-21 23:08:34 +08:00
2016-06-30 08:02:45 +08:00
kn.constructorEnd(this);
}
2016-05-01 09:07:10 +08:00
2016-06-30 08:02:45 +08:00
kn.extendAsViewModel(['View/User/MailBox/FolderList', 'View/App/MailBox/FolderList', 'MailBoxFolderListViewModel'], FolderListMailBoxUserView);
_.extend(FolderListMailBoxUserView.prototype, AbstractView.prototype);
2014-08-21 23:08:34 +08:00
2016-06-30 08:02:45 +08:00
FolderListMailBoxUserView.prototype.onBuild = function(oDom)
{
this.oContentVisible = $('.b-content', oDom);
this.oContentScrollable = $('.content', this.oContentVisible);
2016-06-30 08:02:45 +08:00
var
self = this,
bMobile = Settings.appSettingsGet('mobile'),
fSelectFolder = function(oEvent, bStarred) {
2014-08-21 23:08:34 +08:00
2016-06-30 08:02:45 +08:00
if (bMobile)
{
Globals.leftPanelDisabled(true);
2016-04-29 04:32:54 +08:00
}
2016-06-30 08:02:45 +08:00
oEvent.preventDefault();
2016-04-29 04:32:54 +08:00
2016-06-30 08:02:45 +08:00
if (bStarred)
{
oEvent.stopPropagation();
}
var
2016-06-30 08:02:45 +08:00
oFolder = ko.dataFor(this);
2016-06-30 08:02:45 +08:00
if (oFolder)
{
2016-06-30 08:02:45 +08:00
if (Enums.Layout.NoPreview === SettingsStore.layout())
{
2016-06-30 08:02:45 +08:00
MessageStore.message(null);
}
2016-06-30 08:02:45 +08:00
if (oFolder.fullNameRaw === FolderStore.currentFolderFullNameRaw())
2014-08-21 23:08:34 +08:00
{
2016-06-30 08:02:45 +08:00
Cache.setFolderHash(oFolder.fullNameRaw, '');
2014-08-21 23:08:34 +08:00
}
2016-06-30 08:02:45 +08:00
if (bStarred)
{
2016-06-30 08:02:45 +08:00
kn.setHash(Links.mailBox(oFolder.fullNameHash, 1, 'is:flagged'));
}
2016-06-30 08:02:45 +08:00
else
2014-08-21 23:08:34 +08:00
{
2016-06-30 08:02:45 +08:00
kn.setHash(Links.mailBox(oFolder.fullNameHash));
2014-08-21 23:08:34 +08:00
}
}
2016-06-30 08:02:45 +08:00
};
2014-08-21 23:08:34 +08:00
2016-06-30 08:02:45 +08:00
oDom
.on('click', '.b-folders .e-item .e-link .e-collapsed-sign', function(oEvent) {
2014-08-21 23:08:34 +08:00
2016-08-10 03:52:30 +08:00
var oFolder = ko.dataFor(this);
2016-06-30 08:02:45 +08:00
if (oFolder && oEvent)
{
2016-08-10 03:52:30 +08:00
var bCollapsed = oFolder.collapsed();
2016-06-30 08:02:45 +08:00
require('App/User').default.setExpandedFolder(oFolder.fullNameHash, bCollapsed);
oFolder.collapsed(!bCollapsed);
oEvent.preventDefault();
oEvent.stopPropagation();
}
2016-06-30 08:02:45 +08:00
})
.on('click', '.b-folders .e-item .e-link.selectable .inbox-star-icon', function(oEvent) {
fSelectFolder.call(this, oEvent, !self.isInboxStarred());
})
.on('click', '.b-folders .e-item .e-link.selectable', function(oEvent) {
fSelectFolder.call(this, oEvent, false);
2014-08-21 23:08:34 +08:00
});
2016-06-30 08:02:45 +08:00
key('up, down', Enums.KeyState.FolderList, function(event, handler) {
2016-06-30 08:02:45 +08:00
var
2016-07-06 03:52:52 +08:00
iKeyCode = handler && 'up' === handler.shortcut ? Enums.EventKeyCode.Up : Enums.EventKeyCode.Down,
2016-06-30 08:02:45 +08:00
$items = $('.b-folders .e-item .e-link:not(.hidden):visible', oDom);
2016-06-30 08:02:45 +08:00
if (event && $items.length)
{
2016-08-10 03:52:30 +08:00
var iIndex = $items.index($items.filter('.focused'));
2016-06-30 08:02:45 +08:00
if (-1 < iIndex)
{
$items.eq(iIndex).removeClass('focused');
}
2016-07-06 03:52:52 +08:00
if (Enums.EventKeyCode.Up === iKeyCode && 0 < iIndex)
{
2016-06-30 08:02:45 +08:00
iIndex -= 1;
2014-08-21 23:08:34 +08:00
}
2016-07-06 03:52:52 +08:00
else if (Enums.EventKeyCode.Down === iKeyCode && iIndex < $items.length - 1)
2014-08-21 23:08:34 +08:00
{
2016-06-30 08:02:45 +08:00
iIndex += 1;
}
2016-06-30 08:02:45 +08:00
$items.eq(iIndex).addClass('focused');
self.scrollToFocused();
2014-08-21 23:08:34 +08:00
}
return false;
2016-06-30 08:02:45 +08:00
});
2014-08-21 23:08:34 +08:00
2016-06-30 08:02:45 +08:00
key('enter', Enums.KeyState.FolderList, function() {
var $items = $('.b-folders .e-item .e-link:not(.hidden).focused', oDom);
if ($items.length && $items[0])
{
2016-06-30 08:02:45 +08:00
AppStore.focusedState(Enums.Focused.MessageList);
$items.click();
}
return false;
});
2014-08-21 23:08:34 +08:00
2016-06-30 08:02:45 +08:00
key('space', Enums.KeyState.FolderList, function() {
2016-08-10 03:52:30 +08:00
var $items = $('.b-folders .e-item .e-link:not(.hidden).focused', oDom);
2016-06-30 08:02:45 +08:00
if ($items.length && $items[0])
{
2016-08-10 03:52:30 +08:00
var oFolder = ko.dataFor($items[0]);
2016-06-30 08:02:45 +08:00
if (oFolder)
2014-08-21 23:08:34 +08:00
{
2016-08-10 03:52:30 +08:00
var bCollapsed = oFolder.collapsed();
2016-06-30 08:02:45 +08:00
require('App/User').default.setExpandedFolder(oFolder.fullNameHash, bCollapsed);
oFolder.collapsed(!bCollapsed);
2014-08-21 23:08:34 +08:00
}
}
2016-06-30 08:02:45 +08:00
return false;
});
key('esc, tab, shift+tab, right', Enums.KeyState.FolderList, function() {
AppStore.focusedState(Enums.Focused.MessageList);
return false;
});
AppStore.focusedState.subscribe(function(mValue) {
$('.b-folders .e-item .e-link.focused', oDom).removeClass('focused');
if (Enums.Focused.FolderList === mValue)
{
2016-06-30 08:02:45 +08:00
$('.b-folders .e-item .e-link.selected', oDom).addClass('focused');
}
2016-06-30 08:02:45 +08:00
});
};
2016-06-30 08:02:45 +08:00
FolderListMailBoxUserView.prototype.messagesDropOver = function(oFolder)
{
window.clearTimeout(this.iDropOverTimer);
if (oFolder && oFolder.collapsed())
{
2016-06-30 08:02:45 +08:00
this.iDropOverTimer = window.setTimeout(function() {
oFolder.collapsed(false);
require('App/User').default.setExpandedFolder(oFolder.fullNameHash, true);
Utils.windowResize();
2016-07-06 03:52:52 +08:00
}, Enums.Magics.Time500ms);
2016-06-30 08:02:45 +08:00
}
};
FolderListMailBoxUserView.prototype.messagesDropOut = function()
{
window.clearTimeout(this.iDropOverTimer);
};
2016-06-30 08:02:45 +08:00
FolderListMailBoxUserView.prototype.scrollToFocused = function()
{
if (!this.oContentVisible || !this.oContentScrollable)
2014-08-21 23:08:34 +08:00
{
2016-06-30 08:02:45 +08:00
return false;
}
var
iOffset = 20,
oFocused = $('.e-item .e-link.focused', this.oContentScrollable),
oPos = oFocused.position(),
iVisibleHeight = this.oContentVisible.height(),
iFocusedHeight = oFocused.outerHeight();
if (oPos && (0 > oPos.top || oPos.top + iFocusedHeight > iVisibleHeight))
{
if (0 > oPos.top)
{
this.oContentScrollable.scrollTop(this.oContentScrollable.scrollTop() + oPos.top - iOffset);
}
else
{
this.oContentScrollable.scrollTop(this.oContentScrollable.scrollTop() + oPos.top - iVisibleHeight + iFocusedHeight + iOffset);
}
return true;
}
2016-06-30 08:02:45 +08:00
return false;
};
/**
* @param {FolderModel} oToFolder
* @param {{helper:jQuery}} oUi
* @returns {void}
*/
FolderListMailBoxUserView.prototype.messagesDrop = function(oToFolder, oUi)
{
if (oToFolder && oUi && oUi.helper)
{
2016-06-30 08:02:45 +08:00
var
sFromFolderFullNameRaw = oUi.helper.data('rl-folder'),
bCopy = Globals.$html.hasClass('rl-ctrl-key-pressed'),
aUids = oUi.helper.data('rl-uids');
if (Utils.isNormal(sFromFolderFullNameRaw) && '' !== sFromFolderFullNameRaw && Utils.isArray(aUids))
{
2016-06-30 08:02:45 +08:00
require('App/User').default.moveMessagesToFolder(sFromFolderFullNameRaw, aUids, oToFolder.fullNameRaw, bCopy);
}
2016-06-30 08:02:45 +08:00
}
};
2016-06-30 08:02:45 +08:00
FolderListMailBoxUserView.prototype.composeClick = function()
{
if (Settings.capa(Enums.Capa.Composer))
{
kn.showScreenPopup(require('View/Popup/Compose'));
}
};
FolderListMailBoxUserView.prototype.createFolder = function()
{
kn.showScreenPopup(require('View/Popup/FolderCreate'));
};
FolderListMailBoxUserView.prototype.configureFolders = function()
{
kn.setHash(Links.settings('folders'));
};
FolderListMailBoxUserView.prototype.contactsClick = function()
{
if (this.allowContacts)
{
kn.showScreenPopup(require('View/Popup/Contacts'));
}
};
2016-06-30 08:02:45 +08:00
module.exports = FolderListMailBoxUserView;