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'),
|
2013-10-23 14:46:42 -04:00
|
|
|
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) {
|
|
|
|
$scope.navOpen = false;
|
2013-10-18 20:58:53 -04:00
|
|
|
$scope.writerOpen = false;
|
2013-10-21 11:10:45 -04:00
|
|
|
$scope.accountOpen = false;
|
2013-10-10 13:15:16 -04:00
|
|
|
|
|
|
|
emailDao = appController._emailDao;
|
|
|
|
|
|
|
|
//
|
|
|
|
// scope functions
|
|
|
|
//
|
2013-09-17 13:11:30 -04:00
|
|
|
|
|
|
|
$scope.openNav = function() {
|
|
|
|
$scope.navOpen = true;
|
|
|
|
};
|
|
|
|
$scope.closeNav = function() {
|
|
|
|
$scope.navOpen = false;
|
|
|
|
};
|
2013-09-18 16:05:51 -04:00
|
|
|
|
2013-10-18 20:58:53 -04:00
|
|
|
$scope.openWriter = function(replyTo) {
|
2013-10-18 21:32:00 -04:00
|
|
|
$scope.writerReply = !! (replyTo);
|
2013-10-18 20:58:53 -04:00
|
|
|
$scope.writerOpen = true;
|
|
|
|
};
|
|
|
|
$scope.closeWriter = function() {
|
|
|
|
$scope.writerOpen = false;
|
|
|
|
};
|
|
|
|
|
2013-09-30 15:22:46 -04:00
|
|
|
$scope.openFolder = function(folder) {
|
|
|
|
$scope.currentFolder = folder;
|
|
|
|
$scope.closeNav();
|
|
|
|
};
|
|
|
|
|
2013-10-22 10:45:50 -04:00
|
|
|
$scope.openAccount = function() {
|
2013-10-21 11:10:45 -04:00
|
|
|
$scope.accountOpen = true;
|
|
|
|
};
|
2013-10-22 10:45:50 -04:00
|
|
|
$scope.closeAccount = function() {
|
|
|
|
$scope.accountOpen = false;
|
|
|
|
};
|
2013-10-21 11:10:45 -04:00
|
|
|
|
2013-10-16 12:56:18 -04:00
|
|
|
$scope.remove = function(email) {
|
2013-10-24 09:18:51 -04:00
|
|
|
if (!email) {
|
2013-10-16 12:56:18 -04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-10-24 09:18:51 -04:00
|
|
|
var index;
|
|
|
|
removeLocalAndShowNext();
|
|
|
|
removeRemote();
|
2013-10-17 04:36:50 -04:00
|
|
|
|
2013-10-24 09:18:51 -04:00
|
|
|
function removeLocalAndShowNext() {
|
2013-10-17 04:36:50 -04:00
|
|
|
index = $scope.emails.indexOf(email);
|
|
|
|
// show the next mail
|
|
|
|
if ($scope.emails.length > 1) {
|
|
|
|
// if we're about to delete the last entry of the array, show the previous (i.e. the one below in the list),
|
|
|
|
// otherwise show the next one (i.e. the one above in the list)
|
|
|
|
$scope.select(_.last($scope.emails) === email ? $scope.emails[index - 1] : $scope.emails[index + 1]);
|
|
|
|
} else {
|
|
|
|
// if we have only one email in the array, show nothing
|
|
|
|
$scope.select();
|
|
|
|
$scope.selected = undefined;
|
|
|
|
}
|
|
|
|
$scope.emails.splice(index, 1);
|
2013-10-24 09:18:51 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
function removeRemote() {
|
|
|
|
var trashFolder = _.findWhere($scope.folders, {
|
|
|
|
type: 'Trash'
|
|
|
|
});
|
|
|
|
if ($scope.currentFolder === trashFolder) {
|
|
|
|
emailDao.imapDeleteMessage({
|
|
|
|
folder: $scope.currentFolder.path,
|
|
|
|
uid: email.uid
|
|
|
|
}, moved);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
emailDao.imapMoveMessage({
|
|
|
|
folder: $scope.currentFolder.path,
|
|
|
|
uid: email.uid,
|
|
|
|
destination: trashFolder.path
|
|
|
|
}, moved);
|
|
|
|
}
|
|
|
|
|
|
|
|
function moved(err) {
|
|
|
|
if (err) {
|
|
|
|
console.error(err);
|
|
|
|
$scope.emails.splice(index, 0, email);
|
|
|
|
$scope.$apply();
|
|
|
|
return;
|
|
|
|
}
|
2013-10-16 12:56:18 -04:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2013-10-24 13:37:07 -04:00
|
|
|
$scope.emptyOutbox = function() {
|
2013-10-23 14:46:42 -04:00
|
|
|
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;
|
|
|
|
|
2013-10-23 14:46:42 -04:00
|
|
|
// 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;
|
2013-10-23 14:46:42 -04:00
|
|
|
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-23 14:46:42 -04:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
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();
|
2013-10-23 14:46:42 -04:00
|
|
|
emailDao.smtpSend(email, function(err) {
|
|
|
|
if (err) {
|
|
|
|
console.error(err);
|
2013-10-24 11:37:16 -04:00
|
|
|
outboxBusy = false;
|
2013-10-23 14:46:42 -04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
removeFromStorage(email.id);
|
2013-10-24 10:45:54 -04:00
|
|
|
send(emails);
|
2013-10-23 14:46:42 -04:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
2013-10-23 14:46:42 -04:00
|
|
|
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;
|
2013-10-23 14:46:42 -04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
outbox.count = (outbox.count > 0) ? outbox.count - 1 : outbox.count;
|
|
|
|
$scope.$apply();
|
2013-10-24 11:37:16 -04:00
|
|
|
outboxBusy = false;
|
2013-10-23 14:46:42 -04:00
|
|
|
});
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
//
|
|
|
|
// Start
|
|
|
|
//
|
|
|
|
|
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]);
|
|
|
|
});
|
|
|
|
|
2013-10-10 13:15:16 -04:00
|
|
|
//
|
|
|
|
// 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;
|
|
|
|
}
|
|
|
|
|
2013-10-18 20:58:53 -04:00
|
|
|
folders.forEach(function(f) {
|
|
|
|
f.count = 0;
|
|
|
|
});
|
|
|
|
|
2013-10-23 14:46:42 -04:00
|
|
|
// 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',
|
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
|
|
|
}, {
|
2013-10-12 13:39:09 -04:00
|
|
|
type: 'Outbox',
|
2013-10-18 20:58:53 -04:00
|
|
|
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',
|
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-10-11 17:45:30 -04:00
|
|
|
}]);
|
2013-10-10 13:15:16 -04:00
|
|
|
}
|
2013-10-23 14:46:42 -04:00
|
|
|
|
|
|
|
function startOutboxSender() {
|
|
|
|
// start periodic checking of outbox
|
2013-10-24 13:37:07 -04:00
|
|
|
senderIntervalId = setInterval($scope.emptyOutbox, config.checkOutboxInterval);
|
2013-10-23 14:46:42 -04:00
|
|
|
}
|
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-10-22 10:45:50 -04:00
|
|
|
var cs = scope.$$childTail;
|
|
|
|
|
|
|
|
if (e.keyCode === 78 && !cs.writerOpen) {
|
2013-10-13 06:56:33 -04:00
|
|
|
// n -> new mail
|
2013-10-13 06:46:24 -04:00
|
|
|
e.preventDefault();
|
2013-10-22 10:45:50 -04:00
|
|
|
cs.openWriter();
|
2013-10-13 06:46:24 -04:00
|
|
|
|
2013-10-22 10:45:50 -04:00
|
|
|
} else if (e.keyCode === 82 && !cs.writerOpen && cs.selected) {
|
2013-10-13 06:56:33 -04:00
|
|
|
// r -> reply
|
2013-10-13 06:46:24 -04:00
|
|
|
e.preventDefault();
|
2013-10-22 10:45:50 -04:00
|
|
|
cs.openWriter(cs.selected);
|
2013-10-18 20:58:53 -04:00
|
|
|
|
2013-10-22 10:45:50 -04:00
|
|
|
} else if (e.keyCode === 27 && cs.writerOpen) {
|
2013-10-18 20:58:53 -04:00
|
|
|
// escape -> close writer
|
|
|
|
e.preventDefault();
|
2013-10-22 10:45:50 -04:00
|
|
|
cs.closeWriter();
|
|
|
|
|
|
|
|
} else if (e.keyCode === 27 && cs.accountOpen) {
|
|
|
|
// escape -> close account view
|
|
|
|
e.preventDefault();
|
|
|
|
cs.closeAccount();
|
2013-10-17 12:45:20 -04:00
|
|
|
|
2013-10-22 10:45:50 -04:00
|
|
|
} else if (e.keyCode === 83 && !cs.writerOpen && cs.synchronize) {
|
2013-10-17 12:45:20 -04:00
|
|
|
// s -> sync folder
|
|
|
|
e.preventDefault();
|
2013-10-22 10:45:50 -04:00
|
|
|
cs.synchronize();
|
2013-10-13 06:46:24 -04:00
|
|
|
}
|
2013-10-18 20:58:53 -04:00
|
|
|
|
|
|
|
scope.$apply();
|
2013-10-13 06:46:24 -04:00
|
|
|
});
|
|
|
|
};
|
|
|
|
});
|
|
|
|
|
2013-09-17 13:11:30 -04:00
|
|
|
return NavigationCtrl;
|
|
|
|
});
|