mail/src/js/controller/navigation.js

191 lines
6.2 KiB
JavaScript
Raw Normal View History

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'),
str = require('js/app-config').string,
cfg = require('js/app-config').config,
2013-10-13 06:46:24 -04:00
appController = require('js/app-controller'),
errorUtil = require('js/util/error'),
2013-10-16 12:56:18 -04:00
_ = require('underscore'),
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-11-09 06:28:12 -05:00
// global state... inherited to all child scopes
$scope.$root.state = {};
// attach global error handler
errorUtil.attachHandler($scope);
emailDao = appController._emailDao;
outboxBo = appController._outboxBo;
//
// scope functions
//
2013-09-17 13:11:30 -04: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;
$scope.state.nav.toggle(false);
2013-09-30 15:22:46 -04:00
};
$scope.onOutboxUpdate = function(err, count) {
if (err) {
$scope.onError(err);
return;
}
2014-02-24 04:14:07 -05:00
// update the outbox mail count. this should normally happen during the delta sync
// problem is that the outbox continuously retries in the background, whereas the delta sync only runs
// when the outbox is currently viewed...
var outbox = _.findWhere($scope.account.folders, {
type: 'Outbox'
});
2014-02-24 04:14:07 -05:00
if (outbox === $scope.state.nav.currentFolder) {
$scope.state.mailList.synchronize();
} else {
2014-02-24 04:14:07 -05:00
outbox.count = count;
$scope.$apply();
}
};
//
// Start
//
// init folders
initFolders();
// select inbox as the current folder on init
$scope.openFolder($scope.account.folders[0]);
// connect imap/smtp clients on first startup
appController.onConnect($scope.onError);
2013-10-11 17:45:30 -04:00
//
// helper functions
//
function initFolders() {
2013-10-11 17:45:30 -04:00
if (window.chrome && chrome.identity) {
// 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);
// make function available globally for write controller
$scope.emptyOutbox = outboxBo._processOutbox.bind(outboxBo);
2013-10-11 17:45:30 -04:00
return;
}
// attach dummy folders for development
$scope.$root.account = {};
$scope.account.folders = [{
2013-10-12 13:39:09 -04:00
type: 'Inbox',
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',
count: 0,
2013-10-12 13:39:09 -04:00
path: 'SENT'
2013-10-11 17:45:30 -04:00
}, {
2013-10-12 13:39:09 -04:00
type: 'Outbox',
count: 0,
2013-10-12 13:39:09 -04:00
path: 'OUTBOX'
2013-10-11 17:45:30 -04:00
}, {
2013-10-12 13:39:09 -04:00
type: 'Drafts',
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',
count: 0,
2013-10-12 13:39:09 -04:00
path: 'TRASH'
}];
}
function sentNotification(email) {
chrome.notifications.create('o' + email.id, {
type: 'basic',
2013-12-06 12:46:26 -05:00
title: 'Message sent',
message: email.subject.replace(str.subjectPrefix, ''),
iconUrl: chrome.runtime.getURL(cfg.iconPath)
}, function() {});
}
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;
if (modifier && e.keyCode === 78 && scope.state.writer && !scope.state.writer.open) {
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();
2013-10-13 06:46:24 -04:00
2013-12-06 11:30:49 -05:00
} else if (modifier && e.keyCode === 70 && !scope.state.writer.open) {
// f -> find
e.preventDefault();
scope.state.mailList.searching = true;
$timeout(function() {
scope.state.mailList.searching = false;
}, 200);
2013-12-06 05:51:13 -05:00
} else if (modifier && e.keyCode === 82 && scope.state.writer && !scope.state.writer.open && 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);
2013-12-06 05:51:13 -05:00
} else if (modifier && e.keyCode === 83 && scope.state.writer && !scope.state.writer.open && scope.state.mailList.synchronize) {
// s -> sync folder
e.preventDefault();
scope.state.mailList.synchronize();
2013-11-08 15:55:08 -05:00
} else if (e.keyCode === 27 && scope.state.writer.open) {
// escape -> close writer
e.preventDefault();
2013-11-08 15:55:08 -05:00
scope.state.writer.close();
2013-10-22 10:45:50 -04:00
2013-11-08 17:53:33 -05:00
} else if (e.keyCode === 27 && scope.state.account.open) {
2013-10-22 10:45:50 -04:00
// escape -> close account view
e.preventDefault();
2013-11-08 17:53:33 -05:00
scope.state.account.toggle(false);
2013-10-17 12:45:20 -04:00
} else if (e.keyCode === 27 && scope.state.contacts.open) {
// escape -> close contacts view
e.preventDefault();
scope.state.contacts.toggle(false);
} else if (e.keyCode === 27 && scope.state.nav.open) {
// escape -> close nav view
e.preventDefault();
scope.state.nav.toggle(false);
2013-10-13 06:46:24 -04:00
}
scope.$apply();
2013-10-13 06:46:24 -04:00
});
};
});
2013-09-17 13:11:30 -04:00
return NavigationCtrl;
});