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

293 lines
7.1 KiB
JavaScript
Raw Normal View History

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