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

183 lines
4.6 KiB
JavaScript
Raw Normal View History

2014-10-02 16:05:44 -04:00
'use strict';
2013-09-17 13:11:30 -04:00
//
// Constants
//
var NOTIFICATION_SENT_TIMEOUT = 2000;
2014-10-02 16:05:44 -04:00
//
// Controller
//
2014-12-17 12:18:26 -05:00
var NavigationCtrl = function($scope, $location, $q, account, email, outbox, notification, appConfig, dialog, dummy) {
if (!$location.search().dev && !account.isLoggedIn()) {
$location.path('/'); // init app
return;
}
2013-09-18 16:05:51 -04:00
var str = appConfig.string,
config = appConfig.config;
2014-10-02 16:05:44 -04:00
//
// scope state
2014-10-02 16:05:44 -04:00
//
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;
}
};
//
// url/history handling
//
2014-12-11 09:02:08 -05:00
$scope.loc = $location;
2014-12-05 10:57:53 -05:00
/**
* Close read mode and go to folder
*/
$scope.navigate = function(folderIndex) {
2014-12-11 09:02:08 -05:00
$location.search('uid', null); // close the read mode
$location.search('folder', folderIndex); // open the n-th folder
2014-12-05 10:57:53 -05:00
};
2014-12-11 09:02:08 -05:00
// folder index url watcher
$scope.$watch('(loc.search()).folder', function(folderIndex) {
if (!$scope.account.folders || !$scope.account.folders.length) {
// there's no folder to navigate to
return;
}
// normalize folder index to [0 ; $scope.account.folders.length - 1]
folderIndex = parseInt(folderIndex, 10);
if (isNaN(folderIndex) || folderIndex < 0 || folderIndex > ($scope.account.folders.length - 1)) {
// array index out of bounds or nonsensical data
$location.search('folder', 0);
return;
}
// navigate to folder[folderIndex]
2014-12-12 04:49:16 -05:00
// navigate to the selected folder index
$scope.state.nav.currentFolder = $scope.account.folders[folderIndex];
$scope.state.nav.toggle(false);
2014-12-11 09:02:08 -05:00
});
// nav open/close state url watcher
$scope.$watch('(loc.search()).nav', function(open) {
// synchronize the url to the scope state
$scope.state.nav.toggle(!!open);
});
$scope.$watch('state.nav.open', function(value) {
// synchronize the scope state to the url
$location.search('nav', value ? true : null);
});
// lightbox state url watcher
$scope.$watch('(loc.search()).lightbox', function(value) {
// synchronize the url to the scope state
$scope.state.lightbox = (value) ? value : undefined;
});
$scope.$watch('state.lightbox', function(value) {
// synchronize the scope state to the url
$location.search('lightbox', value ? value : null);
});
//
// scope functions
//
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
});
if (!ob) {
// the outbox folder has not been initialized yet
return;
}
ob.count = count;
2013-10-11 17:45:30 -04:00
2014-12-17 12:18:26 -05:00
return $q(function(resolve) {
resolve();
}).then(function() {
return email.refreshFolder({
folder: ob
});
}).catch(dialog.error);
2014-10-02 16:05:44 -04:00
};
2014-10-02 16:05:44 -04:00
$scope.logout = function() {
2014-12-17 12:18:26 -05:00
return dialog.confirm({
title: str.logoutTitle,
message: str.logoutMessage,
2014-10-02 16:05:44 -04:00
callback: function(confirm) {
if (confirm) {
2014-12-15 13:31:34 -05:00
account.logout().catch(dialog.error);
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
// init folders
initializeFolders();
// 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) {
2014-12-12 04:49:16 -05:00
$scope.navigate(0);
}
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 ($location.search().dev) {
2014-12-03 13:05:54 -05:00
$scope.$root.account = {};
$scope.account.folders = dummy.listFolders();
2014-10-02 16:05:44 -04:00
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
2014-12-11 09:02:08 -05:00
outbox.onSent = function(message) {
notification.create({
title: 'Message sent',
message: message.subject,
timeout: NOTIFICATION_SENT_TIMEOUT
}, function() {});
};
2014-10-02 16:05:44 -04:00
// start checking outbox periodically
outbox.startChecking($scope.onOutboxUpdate);
2014-10-02 16:05:44 -04:00
}
};
2014-10-08 06:34:34 -04:00
module.exports = NavigationCtrl;