mail/src/js/controller/navigation.js

234 lines
6.7 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'),
appController = require('js/app-controller'),
2013-10-16 12:56:18 -04:00
_ = require('underscore'),
config = require('js/app-config').config,
2013-10-24 11:37:16 -04:00
emailDao, senderIntervalId,
outboxBusy = false;
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 = {};
emailDao = appController._emailDao;
//
// scope functions
//
2013-09-17 13:11:30 -04:00
$scope.$root.onError = function(options) {
console.error(options);
$scope.state.dialog = {
open: true,
title: options.title || 'Error',
message: options.message || options.errMsg
};
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
};
2013-11-08 17:36:36 -05:00
//
// Outbox checker
//
2013-10-24 13:37:07 -04:00
$scope.emptyOutbox = function() {
var dbType = 'email_OUTBOX',
outbox = _.findWhere($scope.folders, {
type: 'Outbox'
});
checkStorage();
function checkStorage() {
2013-10-24 11:37:16 -04:00
if (outboxBusy) {
return;
}
outboxBusy = true;
// get last item from outbox
emailDao._devicestorage.listItems(dbType, 0, null, function(err, pending) {
if (err) {
console.error(err);
2013-10-24 11:37:16 -04:00
outboxBusy = false;
return;
}
// update outbox folder count
outbox.count = pending.length;
$scope.$apply();
2013-10-24 10:45:54 -04:00
// sending pending mails
send(pending);
});
}
2013-10-24 10:45:54 -04:00
function send(emails) {
if (emails.length === 0) {
2013-10-24 11:37:16 -04:00
outboxBusy = false;
2013-10-24 10:45:54 -04:00
return;
}
2013-10-24 13:37:07 -04:00
2013-10-24 10:45:54 -04:00
var email = emails.shift();
emailDao.smtpSend(email, function(err) {
if (err) {
console.error(err);
2013-10-24 11:37:16 -04:00
outboxBusy = false;
return;
}
removeFromStorage(email.id);
2013-10-24 10:45:54 -04:00
send(emails);
});
}
function removeFromStorage(id) {
if (!id) {
console.error('Cannot remove email from storage without a valid id!');
2013-10-24 11:37:16 -04:00
outboxBusy = false;
return;
}
// delete email from local storage
var key = dbType + '_' + id;
emailDao._devicestorage.removeList(key, function(err) {
if (err) {
console.error(err);
2013-10-24 11:37:16 -04:00
outboxBusy = false;
return;
}
outbox.count = (outbox.count > 0) ? outbox.count - 1 : outbox.count;
$scope.$apply();
2013-10-24 11:37:16 -04:00
outboxBusy = false;
});
}
};
//
// Start
//
// init folders
2013-10-11 17:45:30 -04:00
initFolders(function(folders) {
$scope.folders = folders;
// select inbox as the current folder on init
$scope.openFolder($scope.folders[0]);
});
//
// helper functions
//
function initFolders(callback) {
2013-10-11 17:45:30 -04:00
if (window.chrome && chrome.identity) {
emailDao.imapListFolders(function(err, folders) {
if (err) {
console.log(err);
return;
}
folders.forEach(function(f) {
f.count = 0;
});
// start checking outbox periodically
startOutboxSender();
2013-10-11 17:45:30 -04:00
callback(folders);
$scope.$apply();
});
return;
}
callback([{
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'
2013-10-11 17:45:30 -04:00
}]);
}
function startOutboxSender() {
// start periodic checking of outbox
2013-10-24 13:37:07 -04:00
senderIntervalId = setInterval($scope.emptyOutbox, config.checkOutboxInterval);
}
2013-09-17 13:11:30 -04:00
};
2013-10-13 06:46:24 -04:00
//
// Directives
//
var ngModule = angular.module('navigation', []);
ngModule.directive('keyShortcuts', function() {
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;
}
if (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-11-08 17:31:20 -05:00
} else if (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-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
2013-11-08 17:31:20 -05:00
} else if (e.keyCode === 83 && scope.state.writer && !scope.state.writer.open && scope.state.mailList.synchronize) {
2013-10-17 12:45:20 -04:00
// s -> sync folder
e.preventDefault();
2013-11-08 17:31:20 -05:00
scope.state.mailList.synchronize();
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;
});