mail/src/js/controller/app/navigation.js

204 lines
5.2 KiB
JavaScript
Raw Normal View History

2014-10-02 16:05:44 -04:00
'use strict';
2013-09-17 13:11:30 -04:00
var backBtnHandler = require('../../util/backbutton-handler');
2013-10-02 07:11:18 -04:00
//
// Constants
//
var NOTIFICATION_SENT_TIMEOUT = 2000;
2014-10-02 16:05:44 -04:00
//
// Controller
//
var NavigationCtrl = function($scope, $routeParams, $location, account, email, outbox, notification, appConfig, dialog) {
!$routeParams.dev && !account.isLoggedIn() && $location.path('/'); // init app
2013-09-18 16:05:51 -04:00
var str = appConfig.string,
config = appConfig.config;
2014-10-02 16:05:44 -04:00
//
// scope functions
//
2013-09-30 15:22:46 -04:00
2014-10-02 16:05:44 -04:00
$scope.state.nav = {
open: false,
toggle: function(to) {
this.open = to;
}
};
2014-10-02 16:05:44 -04:00
$scope.openFolder = function(folder) {
$scope.state.nav.currentFolder = folder;
$scope.state.nav.toggle(false);
};
2014-06-03 05:48:11 -04:00
2014-10-02 16:05:44 -04:00
$scope.onOutboxUpdate = function(err, count) {
if (err) {
dialog.error(err);
2014-10-02 16:05:44 -04:00
return;
}
2014-10-02 16:05:44 -04:00
// update the outbox mail count
var ob = _.findWhere($scope.account.folders, {
2014-10-02 16:05:44 -04:00
type: config.outboxMailboxType
});
ob.count = count;
2014-10-02 16:05:44 -04:00
$scope.$apply();
2013-10-11 17:45:30 -04:00
email.refreshFolder({
folder: ob
}, dialog.error);
2014-10-02 16:05:44 -04:00
};
2014-10-02 16:05:44 -04:00
$scope.logout = function() {
dialog.confirm({
title: str.logoutTitle,
message: str.logoutMessage,
2014-10-02 16:05:44 -04:00
callback: function(confirm) {
if (confirm) {
account.logout();
2014-10-02 16:05:44 -04:00
}
}
2014-10-02 16:05:44 -04:00
});
};
2014-10-02 16:05:44 -04:00
//
// Start
//
2014-10-02 16:05:44 -04:00
// handle back button
backBtnHandler.start();
// init folders
initializeFolders();
// select inbox as the current folder on init
if ($scope.account.folders && $scope.account.folders.length > 0) {
$scope.openFolder($scope.account.folders[0]);
}
// connect imap/smtp clients on first startup
account.onConnect(function(err) {
2014-10-02 16:05:44 -04:00
if (err) {
dialog.error(err);
2014-10-02 16:05:44 -04:00
return;
}
2014-10-02 16:05:44 -04:00
// select inbox if not yet selected
if (!$scope.state.nav.currentFolder) {
$scope.openFolder($scope.account.folders[0]);
$scope.$apply();
}
2014-10-02 16:05:44 -04:00
});
2013-09-17 13:11:30 -04:00
2013-10-13 06:46:24 -04:00
//
2014-10-02 16:05:44 -04:00
// helper functions
2013-10-13 06:46:24 -04:00
//
2014-10-02 16:05:44 -04:00
function initializeFolders() {
// create dummy folder in dev environment only
if ($routeParams.dev) {
createDummyFolders();
return;
}
2013-11-08 17:31:20 -05:00
2014-10-02 16:05:44 -04:00
// get pointer to account/folder/message tree on root scope
2014-11-20 16:53:30 -05:00
$scope.$root.account = account.list()[0];
2014-10-02 16:05:44 -04:00
// set notificatio handler for sent messages
outbox.onSent = sentNotification;
2014-10-02 16:05:44 -04:00
// start checking outbox periodically
outbox.startChecking($scope.onOutboxUpdate);
2014-10-02 16:05:44 -04:00
}
function sentNotification(message) {
2014-10-02 16:05:44 -04:00
notification.create({
title: 'Message sent',
message: message.subject,
timeout: NOTIFICATION_SENT_TIMEOUT
2014-10-02 16:05:44 -04:00
}, function() {});
}
// attach dummy folders for development
function createDummyFolders() {
$scope.$root.account = {};
$scope.account.folders = [{
type: 'Inbox',
count: 2,
path: 'INBOX'
}, {
type: 'Sent',
count: 0,
path: 'SENT'
}, {
type: config.outboxMailboxType,
count: 0,
path: config.outboxMailboxPath
}, {
type: 'Drafts',
count: 0,
path: 'DRAFTS'
}, {
type: 'Trash',
count: 0,
path: 'TRASH'
}];
}
};
//
// Directives
//
var ngModule = angular.module('navigation', []);
ngModule.directive('keyShortcuts', function($timeout) {
return function(scope, elm) {
elm.bind('keydown', function(e) {
// global state is not yet set, ignore keybaord shortcuts
if (!scope.state) {
return;
}
2014-10-02 16:05:44 -04:00
var modifier = e.ctrlKey || e.metaKey;
if (modifier && e.keyCode === 78 && scope.state.lightbox !== 'write') {
// n -> new mail
e.preventDefault();
scope.state.writer.write();
scope.$apply();
} else if (modifier && e.keyCode === 70 && scope.state.lightbox !== 'write') {
// f -> find
e.preventDefault();
scope.state.mailList.searching = true;
$timeout(function() {
scope.state.mailList.searching = false;
}, 200);
scope.$apply();
} else if (modifier && e.keyCode === 82 && scope.state.lightbox !== 'write' && scope.state.mailList.selected) {
// r -> reply
e.preventDefault();
scope.state.writer.write(scope.state.mailList.selected);
scope.$apply();
} else if (e.keyCode === 27 && scope.state.lightbox !== undefined) {
// escape -> close current lightbox
e.preventDefault();
scope.state.lightbox = undefined;
scope.$apply();
} else if (e.keyCode === 27 && scope.state.nav.open) {
// escape -> close nav view
e.preventDefault();
scope.state.nav.toggle(false);
scope.$apply();
}
});
};
});
2013-10-13 06:46:24 -04:00
2014-10-08 06:34:34 -04:00
module.exports = NavigationCtrl;