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

275 lines
6.4 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'),
LinkBuilder = require('Common/LinkBuilder'),
2014-08-21 23:08:34 +08:00
Settings = require('Storage/Settings'),
Cache = require('Storage/App/Cache'),
Data = require('Storage/App/Data'),
2014-08-22 23:08:56 +08:00
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
*/
function FolderListMailBoxAppView()
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;
2014-08-22 23:08:56 +08:00
this.messageList = Data.messageList;
this.folderList = Data.folderList;
this.folderListSystem = Data.folderListSystem;
this.foldersChanging = Data.foldersChanging;
2014-08-22 23:08:56 +08:00
this.leftPanelDisabled = Globals.leftPanelDisabled;
2014-08-21 23:08:34 +08:00
this.iDropOverTimer = 0;
2014-08-27 23:59:44 +08:00
this.allowContacts = !!Settings.settingsGet('ContactsIsAllowed');
2014-08-21 23:08:34 +08:00
kn.constructorEnd(this);
}
kn.extendAsViewModel(['View/App/MailBox/FolderList', 'MailBoxFolderListViewModel'], FolderListMailBoxAppView);
_.extend(FolderListMailBoxAppView.prototype, AbstractView.prototype);
FolderListMailBoxAppView.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();
require('App/App').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)
{
2014-08-22 23:08:56 +08:00
if (Enums.Layout.NoPreview === Data.layout())
2014-08-21 23:08:34 +08:00
{
2014-08-22 23:08:56 +08:00
Data.message(null);
2014-08-21 23:08:34 +08:00
}
2014-08-22 23:08:56 +08:00
if (oFolder.fullNameRaw === Data.currentFolderFullNameRaw())
2014-08-21 23:08:34 +08:00
{
Cache.setFolderHash(oFolder.fullNameRaw, '');
}
kn.setHash(LinkBuilder.mailBox(oFolder.fullNameHash));
}
})
;
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])
{
2014-08-21 23:08:34 +08:00
self.folderList.focused(false);
$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();
require('App/App').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 () {
self.folderList.focused(false);
return false;
});
self.folderList.focused.subscribe(function (bValue) {
$('.b-folders .e-item .e-link.focused', oDom).removeClass('focused');
if (bValue)
{
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
});
};
FolderListMailBoxAppView.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);
require('App/App').setExpandedFolder(oFolder.fullNameHash, true);
2014-08-21 23:08:34 +08:00
Utils.windowResize();
}, 500);
}
2014-08-21 23:08:34 +08:00
};
FolderListMailBoxAppView.prototype.messagesDropOut = function ()
2014-08-21 23:08:34 +08:00
{
window.clearTimeout(this.iDropOverTimer);
};
FolderListMailBoxAppView.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
*/
FolderListMailBoxAppView.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))
{
require('App/App').moveMessagesToFolder(sFromFolderFullNameRaw, aUids, oToFolder.fullNameRaw, bCopy);
2014-08-21 23:08:34 +08:00
}
}
2014-08-21 23:08:34 +08:00
};
FolderListMailBoxAppView.prototype.composeClick = function ()
{
kn.showScreenPopup(require('View/Popup/Compose'));
2014-08-21 23:08:34 +08:00
};
FolderListMailBoxAppView.prototype.createFolder = function ()
{
kn.showScreenPopup(require('View/Popup/FolderCreate'));
2014-08-21 23:08:34 +08:00
};
FolderListMailBoxAppView.prototype.configureFolders = function ()
2014-08-21 23:08:34 +08:00
{
kn.setHash(LinkBuilder.settings('folders'));
};
FolderListMailBoxAppView.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
};
module.exports = FolderListMailBoxAppView;
2014-09-05 06:49:03 +08:00
}());