2014-10-02 16:05:44 -04:00
|
|
|
'use strict';
|
2013-09-17 13:11:30 -04:00
|
|
|
|
2014-10-17 05:17:40 -04:00
|
|
|
//
|
|
|
|
// Constants
|
|
|
|
//
|
|
|
|
|
|
|
|
var NOTIFICATION_SENT_TIMEOUT = 2000;
|
|
|
|
|
|
|
|
|
2014-10-02 16:05:44 -04:00
|
|
|
//
|
|
|
|
// Controller
|
|
|
|
//
|
2013-10-10 13:15:16 -04:00
|
|
|
|
2014-12-04 06:46:55 -05:00
|
|
|
var NavigationCtrl = function($scope, $location, account, email, outbox, notification, appConfig, dialog, dummy) {
|
|
|
|
if (!$location.search().dev && !account.isLoggedIn()) {
|
2014-11-26 11:57:14 -05:00
|
|
|
$location.path('/'); // init app
|
|
|
|
return;
|
|
|
|
}
|
2013-09-18 16:05:51 -04:00
|
|
|
|
2014-12-04 06:46:55 -05:00
|
|
|
var str = appConfig.string,
|
2014-11-20 09:14:39 -05:00
|
|
|
config = appConfig.config;
|
|
|
|
|
2014-10-02 16:05:44 -04:00
|
|
|
//
|
2014-12-04 06:46:55 -05: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;
|
|
|
|
}
|
|
|
|
};
|
2013-11-21 09:36:16 -05:00
|
|
|
|
2014-12-04 06:46:55 -05:00
|
|
|
//
|
|
|
|
// url/history handling
|
|
|
|
//
|
|
|
|
|
|
|
|
$scope.loc = $location;
|
|
|
|
|
|
|
|
// 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.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) {
|
2014-11-20 09:14:39 -05:00
|
|
|
dialog.error(err);
|
2014-10-02 16:05:44 -04:00
|
|
|
return;
|
2014-03-28 20:14:52 -04:00
|
|
|
}
|
|
|
|
|
2014-10-02 16:05:44 -04:00
|
|
|
// update the outbox mail count
|
2014-11-19 14:54:59 -05:00
|
|
|
var ob = _.findWhere($scope.account.folders, {
|
2014-10-02 16:05:44 -04:00
|
|
|
type: config.outboxMailboxType
|
2014-03-28 20:14:52 -04:00
|
|
|
});
|
2014-11-19 14:54:59 -05:00
|
|
|
ob.count = count;
|
2014-10-02 16:05:44 -04:00
|
|
|
$scope.$apply();
|
2013-10-11 17:45:30 -04:00
|
|
|
|
2014-11-19 14:54:59 -05:00
|
|
|
email.refreshFolder({
|
|
|
|
folder: ob
|
2014-11-20 09:14:39 -05:00
|
|
|
}, dialog.error);
|
2014-10-02 16:05:44 -04:00
|
|
|
};
|
2014-05-23 08:23:50 -04:00
|
|
|
|
2014-10-02 16:05:44 -04:00
|
|
|
$scope.logout = function() {
|
2014-11-20 09:14:39 -05:00
|
|
|
dialog.confirm({
|
2014-10-17 05:17:40 -04:00
|
|
|
title: str.logoutTitle,
|
|
|
|
message: str.logoutMessage,
|
2014-10-02 16:05:44 -04:00
|
|
|
callback: function(confirm) {
|
|
|
|
if (confirm) {
|
2014-11-20 09:14:39 -05:00
|
|
|
account.logout();
|
2014-10-02 16:05:44 -04:00
|
|
|
}
|
2014-11-20 09:14:39 -05:00
|
|
|
}
|
2014-10-02 16:05:44 -04:00
|
|
|
});
|
|
|
|
};
|
2014-05-23 08:23:50 -04:00
|
|
|
|
2014-10-02 16:05:44 -04:00
|
|
|
//
|
|
|
|
// Start
|
|
|
|
//
|
2014-05-23 08:23:50 -04:00
|
|
|
|
2014-10-02 16:05:44 -04:00
|
|
|
// init folders
|
|
|
|
initializeFolders();
|
|
|
|
|
2014-12-04 06:46:55 -05:00
|
|
|
// folder index url watcher
|
|
|
|
$scope.$watch('(loc.search()).folder', function(folderIndex) {
|
|
|
|
if (typeof folderIndex === 'undefined') {
|
|
|
|
$location.search('folder', 0); // navigate to inbox by default
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
// select current folder
|
|
|
|
folderIndex = typeof folderIndex === 'string' ? parseInt(folderIndex, 10) : folderIndex;
|
|
|
|
if ($scope.account.folders && $scope.account.folders.length > folderIndex) {
|
|
|
|
// navigate to the selected folder index
|
|
|
|
$scope.openFolder($scope.account.folders[folderIndex]);
|
|
|
|
}
|
|
|
|
});
|
2014-12-02 10:12:42 -05:00
|
|
|
|
2014-10-02 16:05:44 -04:00
|
|
|
// connect imap/smtp clients on first startup
|
2014-11-20 09:14:39 -05:00
|
|
|
account.onConnect(function(err) {
|
2014-10-02 16:05:44 -04:00
|
|
|
if (err) {
|
2014-11-20 09:14:39 -05:00
|
|
|
dialog.error(err);
|
2014-10-02 16:05:44 -04:00
|
|
|
return;
|
2014-05-23 08:23:50 -04:00
|
|
|
}
|
|
|
|
|
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();
|
2013-10-10 13:15:16 -04:00
|
|
|
}
|
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
|
2014-12-04 06:46:55 -05:00
|
|
|
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-11-19 14:54:59 -05:00
|
|
|
outbox.onSent = sentNotification;
|
2014-10-02 16:05:44 -04:00
|
|
|
// start checking outbox periodically
|
2014-11-19 14:54:59 -05:00
|
|
|
outbox.startChecking($scope.onOutboxUpdate);
|
2014-10-02 16:05:44 -04:00
|
|
|
}
|
|
|
|
|
2014-11-19 14:54:59 -05:00
|
|
|
function sentNotification(message) {
|
2014-10-02 16:05:44 -04:00
|
|
|
notification.create({
|
|
|
|
title: 'Message sent',
|
2014-11-19 14:54:59 -05:00
|
|
|
message: message.subject,
|
2014-10-17 05:17:40 -04:00
|
|
|
timeout: NOTIFICATION_SENT_TIMEOUT
|
2014-10-02 16:05:44 -04:00
|
|
|
}, function() {});
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
//
|
|
|
|
// 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;
|
|
|
|
}
|
2013-10-18 20:58:53 -04:00
|
|
|
|
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;
|