2013-10-02 07:11:18 -04:00
|
|
|
define(function(require) {
|
2013-09-17 13:11:30 -04:00
|
|
|
'use strict';
|
|
|
|
|
2013-10-13 06:46:24 -04:00
|
|
|
var angular = require('angular'),
|
|
|
|
appController = require('js/app-controller'),
|
2014-05-23 08:23:50 -04:00
|
|
|
config = require('js/app-config').config,
|
2014-04-01 07:16:39 -04:00
|
|
|
notification = require('js/util/notification'),
|
2013-10-16 12:56:18 -04:00
|
|
|
_ = require('underscore'),
|
2013-11-19 10:14:48 -05:00
|
|
|
emailDao, outboxBo;
|
2013-10-02 07:11:18 -04:00
|
|
|
|
2013-10-13 07:51:34 -04:00
|
|
|
//
|
|
|
|
// Controller
|
|
|
|
//
|
|
|
|
|
2013-09-17 13:11:30 -04:00
|
|
|
var NavigationCtrl = function($scope) {
|
2013-10-10 13:15:16 -04:00
|
|
|
emailDao = appController._emailDao;
|
2013-11-19 10:14:48 -05:00
|
|
|
outboxBo = appController._outboxBo;
|
2013-10-10 13:15:16 -04:00
|
|
|
|
|
|
|
//
|
|
|
|
// scope functions
|
|
|
|
//
|
2013-09-17 13:11:30 -04:00
|
|
|
|
2013-11-08 15:35:30 -05:00
|
|
|
$scope.state.nav = {
|
|
|
|
open: false,
|
|
|
|
toggle: function(to) {
|
|
|
|
this.open = to;
|
|
|
|
}
|
2013-09-17 13:11:30 -04:00
|
|
|
};
|
2013-09-18 16:05:51 -04:00
|
|
|
|
2013-09-30 15:22:46 -04:00
|
|
|
$scope.openFolder = function(folder) {
|
2013-11-08 17:31:20 -05:00
|
|
|
$scope.state.nav.currentFolder = folder;
|
2013-11-08 15:35:30 -05:00
|
|
|
$scope.state.nav.toggle(false);
|
2013-09-30 15:22:46 -04:00
|
|
|
};
|
|
|
|
|
2013-11-21 09:36:16 -05:00
|
|
|
$scope.onOutboxUpdate = function(err, count) {
|
|
|
|
if (err) {
|
|
|
|
$scope.onError(err);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-05-23 08:23:50 -04:00
|
|
|
// update the outbox mail count
|
2014-02-24 04:14:07 -05:00
|
|
|
var outbox = _.findWhere($scope.account.folders, {
|
2014-05-23 08:23:50 -04:00
|
|
|
type: config.outboxMailboxType
|
2013-11-21 09:36:16 -05:00
|
|
|
});
|
2014-05-23 08:23:50 -04:00
|
|
|
outbox.count = count;
|
|
|
|
$scope.$apply();
|
|
|
|
|
|
|
|
emailDao.refreshFolder({
|
|
|
|
folder: outbox
|
|
|
|
}, $scope.onError);
|
2013-11-21 09:36:16 -05:00
|
|
|
};
|
|
|
|
|
2013-10-23 14:46:42 -04:00
|
|
|
//
|
|
|
|
// Start
|
|
|
|
//
|
|
|
|
|
2013-11-08 15:35:30 -05:00
|
|
|
// init folders
|
2014-05-23 08:23:50 -04:00
|
|
|
initializeFolders();
|
2014-04-28 12:09:51 -04:00
|
|
|
|
2013-12-03 13:21:50 -05:00
|
|
|
// select inbox as the current folder on init
|
2014-03-28 20:14:52 -04:00
|
|
|
if ($scope.account.folders && $scope.account.folders.length > 0) {
|
|
|
|
$scope.openFolder($scope.account.folders[0]);
|
|
|
|
}
|
2014-03-28 13:08:04 -04:00
|
|
|
// connect imap/smtp clients on first startup
|
2014-03-28 20:14:52 -04:00
|
|
|
appController.onConnect(function(err) {
|
|
|
|
if (err) {
|
|
|
|
$scope.onError(err);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// select inbox if not yet selected
|
2014-04-29 19:20:36 -04:00
|
|
|
if (!$scope.state.nav.currentFolder) {
|
|
|
|
$scope.openFolder($scope.account.folders[0]);
|
|
|
|
$scope.$apply();
|
|
|
|
}
|
2014-03-28 20:14:52 -04:00
|
|
|
});
|
2013-10-11 17:45:30 -04:00
|
|
|
|
2013-10-10 13:15:16 -04:00
|
|
|
//
|
|
|
|
// helper functions
|
|
|
|
//
|
|
|
|
|
2014-05-23 08:23:50 -04:00
|
|
|
function initializeFolders() {
|
|
|
|
// create dummy folder in dev environment only
|
|
|
|
if (!window.chrome || !chrome.identity) {
|
|
|
|
createDummyFolders();
|
2013-10-11 17:45:30 -04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-05-23 08:23:50 -04:00
|
|
|
// get pointer to account/folder/message tree on root scope
|
|
|
|
$scope.$root.account = emailDao._account;
|
|
|
|
|
|
|
|
// set notificatio handler for sent messages
|
|
|
|
outboxBo.onSent = sentNotification;
|
|
|
|
// start checking outbox periodically
|
|
|
|
outboxBo.startChecking($scope.onOutboxUpdate);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
function sentNotification(email) {
|
|
|
|
notification.create({
|
|
|
|
id: 'o' + email.id,
|
|
|
|
title: 'Message sent',
|
|
|
|
message: email.subject
|
|
|
|
}, function() {});
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// attach dummy folders for development
|
|
|
|
function createDummyFolders() {
|
2013-12-03 13:21:50 -05:00
|
|
|
$scope.$root.account = {};
|
|
|
|
$scope.account.folders = [{
|
2013-10-12 13:39:09 -04:00
|
|
|
type: 'Inbox',
|
2013-10-18 20:58:53 -04:00
|
|
|
count: 2,
|
2013-10-12 13:39:09 -04:00
|
|
|
path: 'INBOX'
|
2013-10-11 17:45:30 -04:00
|
|
|
}, {
|
2013-10-12 13:39:09 -04:00
|
|
|
type: 'Sent',
|
2013-10-18 20:58:53 -04:00
|
|
|
count: 0,
|
2013-10-12 13:39:09 -04:00
|
|
|
path: 'SENT'
|
2013-10-11 17:45:30 -04:00
|
|
|
}, {
|
2014-05-23 08:23:50 -04:00
|
|
|
type: config.outboxMailboxType,
|
2013-10-18 20:58:53 -04:00
|
|
|
count: 0,
|
2014-05-23 08:23:50 -04:00
|
|
|
path: config.outboxMailboxPath
|
2013-10-11 17:45:30 -04:00
|
|
|
}, {
|
2013-10-12 13:39:09 -04:00
|
|
|
type: 'Drafts',
|
2013-10-18 20:58:53 -04:00
|
|
|
count: 0,
|
2013-10-12 13:39:09 -04:00
|
|
|
path: 'DRAFTS'
|
2013-10-11 17:45:30 -04:00
|
|
|
}, {
|
2013-10-12 13:39:09 -04:00
|
|
|
type: 'Trash',
|
2013-10-18 20:58:53 -04:00
|
|
|
count: 0,
|
2013-10-12 13:39:09 -04:00
|
|
|
path: 'TRASH'
|
2013-12-03 13:21:50 -05:00
|
|
|
}];
|
2013-10-10 13:15:16 -04:00
|
|
|
}
|
2013-09-17 13:11:30 -04:00
|
|
|
};
|
|
|
|
|
2013-10-13 06:46:24 -04:00
|
|
|
//
|
|
|
|
// Directives
|
|
|
|
//
|
|
|
|
|
|
|
|
var ngModule = angular.module('navigation', []);
|
2013-12-06 11:30:49 -05:00
|
|
|
ngModule.directive('keyShortcuts', function($timeout) {
|
2013-10-13 06:46:24 -04:00
|
|
|
return function(scope, elm) {
|
|
|
|
elm.bind('keydown', function(e) {
|
2013-11-08 17:31:20 -05:00
|
|
|
// global state is not yet set, ignore keybaord shortcuts
|
|
|
|
if (!scope.state) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-12-06 05:51:13 -05:00
|
|
|
var modifier = e.ctrlKey || e.metaKey;
|
|
|
|
|
2014-04-23 11:45:02 -04:00
|
|
|
if (modifier && e.keyCode === 78 && scope.state.lightbox !== 'write') {
|
2013-10-13 06:56:33 -04:00
|
|
|
// n -> new mail
|
2013-10-13 06:46:24 -04:00
|
|
|
e.preventDefault();
|
2013-11-08 15:55:08 -05:00
|
|
|
scope.state.writer.write();
|
2014-04-22 13:41:14 -04:00
|
|
|
scope.$apply();
|
2013-10-13 06:46:24 -04:00
|
|
|
|
2014-04-23 11:45:02 -04:00
|
|
|
} else if (modifier && e.keyCode === 70 && scope.state.lightbox !== 'write') {
|
2013-12-06 11:30:49 -05:00
|
|
|
// f -> find
|
|
|
|
e.preventDefault();
|
|
|
|
scope.state.mailList.searching = true;
|
|
|
|
$timeout(function() {
|
|
|
|
scope.state.mailList.searching = false;
|
|
|
|
}, 200);
|
2014-04-22 13:41:14 -04:00
|
|
|
scope.$apply();
|
2013-12-06 11:30:49 -05:00
|
|
|
|
2014-04-23 11:45:02 -04:00
|
|
|
} else if (modifier && e.keyCode === 82 && scope.state.lightbox !== 'write' && scope.state.mailList.selected) {
|
2013-10-13 06:56:33 -04:00
|
|
|
// r -> reply
|
2013-10-13 06:46:24 -04:00
|
|
|
e.preventDefault();
|
2013-11-08 17:31:20 -05:00
|
|
|
scope.state.writer.write(scope.state.mailList.selected);
|
2014-04-22 13:41:14 -04:00
|
|
|
scope.$apply();
|
2013-10-18 20:58:53 -04:00
|
|
|
|
2014-04-23 11:45:02 -04:00
|
|
|
} else if (modifier && e.keyCode === 83 && scope.state.lightbox !== 'write' && scope.state.mailList.synchronize) {
|
2013-12-06 05:51:13 -05:00
|
|
|
// s -> sync folder
|
|
|
|
e.preventDefault();
|
|
|
|
scope.state.mailList.synchronize();
|
2014-04-22 13:41:14 -04:00
|
|
|
scope.$apply();
|
2013-12-06 05:51:13 -05:00
|
|
|
|
2014-04-23 11:45:02 -04:00
|
|
|
} else if (e.keyCode === 27 && scope.state.lightbox !== undefined) {
|
|
|
|
// escape -> close current lightbox
|
2013-10-18 20:58:53 -04:00
|
|
|
e.preventDefault();
|
2014-04-23 11:45:02 -04:00
|
|
|
scope.state.lightbox = undefined;
|
2014-04-22 13:41:14 -04:00
|
|
|
scope.$apply();
|
2014-03-06 12:19:51 -05:00
|
|
|
|
2013-11-09 07:44:06 -05:00
|
|
|
} else if (e.keyCode === 27 && scope.state.nav.open) {
|
|
|
|
// escape -> close nav view
|
|
|
|
e.preventDefault();
|
|
|
|
scope.state.nav.toggle(false);
|
2014-04-22 13:41:14 -04:00
|
|
|
scope.$apply();
|
2013-10-13 06:46:24 -04:00
|
|
|
}
|
2013-10-18 20:58:53 -04:00
|
|
|
|
2013-10-13 06:46:24 -04:00
|
|
|
});
|
|
|
|
};
|
|
|
|
});
|
|
|
|
|
2013-09-17 13:11:30 -04:00
|
|
|
return NavigationCtrl;
|
|
|
|
});
|