mail/src/js/controller/mail-list.js

268 lines
9.3 KiB
JavaScript
Raw Normal View History

define(function(require) {
'use strict';
var angular = require('angular'),
2013-12-04 08:15:12 -05:00
_ = require('underscore'),
2013-09-11 17:31:08 -04:00
appController = require('js/app-controller'),
IScroll = require('iscroll'),
str = require('js/app-config').string,
cfg = require('js/app-config').config,
emailDao, outboxBo;
var MailListCtrl = function($scope) {
2013-11-08 17:31:20 -05:00
//
// Init
//
2013-09-15 11:05:37 -04:00
emailDao = appController._emailDao;
outboxBo = appController._outboxBo;
2013-12-04 08:15:12 -05:00
// push handler
if (emailDao) {
emailDao.onIncomingMessage = function(email) {
if (email.subject.indexOf(str.subjectPrefix) === -1) {
return;
}
// sync
$scope.synchronize(function() {
// show notification
notificationForEmail(email);
});
};
chrome.notifications.onClicked.addListener(notificationClicked);
}
2013-12-04 08:15:12 -05:00
function notificationClicked(uidString) {
var email, uid = parseInt(uidString, 10);
2013-12-04 08:15:12 -05:00
if (isNaN(uid)) {
return;
}
email = _.findWhere(getFolder().messages, {
uid: uid
});
if (email) {
$scope.select(email);
}
}
2013-09-28 10:08:12 -04:00
//
// scope functions
//
2013-11-08 17:31:20 -05:00
$scope.select = function(email) {
2013-10-04 12:01:42 -04:00
if (!email) {
$scope.state.mailList.selected = undefined;
2013-10-04 12:01:42 -04:00
return;
}
2013-11-18 11:44:59 -05:00
$scope.state.mailList.selected = email;
$scope.state.read.toggle(true);
2013-11-18 11:44:59 -05:00
// if the email is unread, please sync the new state.
// otherweise forget about it.
if (!email.unread) {
return;
}
email.unread = false;
$scope.synchronize();
};
2013-12-04 08:15:12 -05:00
$scope.synchronize = function(callback) {
// if we're in the outbox, don't do an imap sync
if (getFolder().type === 'Outbox') {
updateStatus('Last update: ', new Date());
displayEmails(outboxBo.pendingEmails);
return;
}
2013-09-28 10:08:12 -04:00
updateStatus('Syncing ...');
// let email dao handle sync transparently
emailDao.sync({
folder: getFolder().path
}, function(err) {
if (err && err.code === 409) {
// sync still busy
return;
}
if (err) {
updateStatus('Error on sync!');
$scope.onError(err);
return;
}
2013-11-11 09:53:34 -05:00
// sort emails
displayEmails(getFolder().messages);
// display last update
updateStatus('Last update: ', new Date());
$scope.$apply();
2013-12-04 08:15:12 -05:00
if (callback) {
callback();
}
});
};
$scope.remove = function(email) {
if (!email) {
2013-11-11 09:53:34 -05:00
return;
}
2013-12-04 08:15:12 -05:00
var index, currentFolder, outboxFolder;
2013-12-04 08:15:12 -05:00
currentFolder = getFolder();
// trashFolder = _.findWhere($scope.folders, {
// type: 'Trash'
// });
2013-12-04 08:15:12 -05:00
outboxFolder = _.findWhere($scope.account.folders, {
type: 'Outbox'
});
2013-12-04 08:15:12 -05:00
if (currentFolder === outboxFolder) {
$scope.onError({
errMsg: 'Deleting messages from the outbox is not yet supported.'
});
return;
}
2013-12-04 08:15:12 -05:00
removeAndShowNext();
$scope.synchronize();
function removeAndShowNext() {
index = getFolder().messages.indexOf(email);
// show the next mail
if (getFolder().messages.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(getFolder().messages) === email ? getFolder().messages[index - 1] : getFolder().messages[index + 1]);
} else {
// if we have only one email in the array, show nothing
$scope.select();
$scope.state.mailList.selected = undefined;
}
getFolder().messages.splice(index, 1);
}
2013-11-08 17:31:20 -05:00
};
$scope._stopWatchTask = $scope.$watch('state.nav.currentFolder', function() {
if (!getFolder()) {
return;
}
// development... display dummy mail objects
if (!window.chrome || !chrome.identity) {
updateStatus('Last update: ', new Date());
getFolder().messages = createDummyMails();
displayEmails(getFolder().messages);
return;
}
2013-09-30 15:22:46 -04:00
// production... in chrome packaged app
// if we're in the outbox, read directly from there.
if (getFolder().type === 'Outbox') {
updateStatus('Last update: ', new Date());
displayEmails(outboxBo.pendingEmails);
2013-09-30 15:22:46 -04:00
return;
}
2013-10-09 04:22:29 -04:00
displayEmails(getFolder().messages);
$scope.synchronize();
});
2013-11-08 17:31:20 -05:00
// share local scope functions with root state
$scope.state.mailList = {
remove: $scope.remove,
synchronize: $scope.synchronize
};
2013-09-28 10:08:12 -04:00
//
// helper functions
//
2013-12-04 08:15:12 -05:00
function notificationForEmail(email) {
2013-12-05 09:22:44 -05:00
chrome.notifications.create('' + email.uid, {
2013-12-04 08:15:12 -05:00
type: 'basic',
title: email.from[0].name || email.from[0].address,
message: email.subject.replace(str.subjectPrefix, ''),
2013-12-04 08:15:12 -05:00
iconUrl: chrome.runtime.getURL(cfg.iconPath)
}, function() {});
}
2013-09-28 10:08:12 -04:00
function updateStatus(lbl, time) {
$scope.lastUpdateLbl = lbl;
$scope.lastUpdate = (time) ? time : '';
}
2013-09-26 07:26:57 -04:00
function displayEmails(emails) {
if (!emails || emails.length < 1) {
2013-09-30 15:22:46 -04:00
$scope.select();
2013-09-26 07:26:57 -04:00
return;
}
2013-12-04 11:13:24 -05:00
if (!$scope.state.mailList.selected) {
// select first message
$scope.select(emails[emails.length - 1]);
}
2013-09-26 07:26:57 -04:00
}
2013-09-30 15:22:46 -04:00
function getFolder() {
2013-11-08 17:31:20 -05:00
return $scope.state.nav.currentFolder;
2013-09-30 15:22:46 -04:00
}
2013-09-26 07:26:57 -04:00
};
2013-10-09 04:22:29 -04:00
function createDummyMails() {
var uid = 0;
var Email = function(unread, attachments, answered, html) {
this.uid = uid++;
this.from = [{
name: 'Whiteout Support',
address: 'support@whiteout.io'
}]; // sender address
this.to = [{
address: 'max.musterman@gmail.com'
}]; // list of receivers
2013-09-11 17:33:13 -04:00
this.attachments = (attachments) ? [true] : undefined;
this.unread = unread;
this.answered = answered;
this.html = html;
2013-09-26 07:26:57 -04:00
this.sentDate = new Date('Thu Sep 19 2013 20:41:23 GMT+0200 (CEST)');
2013-11-18 11:44:59 -05:00
this.subject = 'Getting started'; // Subject line
this.body = 'Here are a few pointers to help you get started with Whiteout Mail.\n\n# Write encrypted message\n- You can compose a message by clicking on the compose button on the upper right (keyboard shortcut is "n" for a new message or "r" to reply).\n- When typing the recipient\'s email address, secure recipients are marked with a blue label and insecure recipients are red.\n- When sending an email to insecure recipients, the default behavior for Whiteout Mail is to invite them to the service and only send the message content in an encrypted form, once they have joined.\n\n# Advanced features\n- To verify a recipient\'s PGP key, you can hover over the blue label containing their email address and their key fingerprint will be displayed.\n- To view your own key fingerprint, open the account view in the navigation bar on the left. You can compare these with your correspondants over a second channel such as a phonecall.\n\nWe hope this helped you to get started with Whiteout Mail.\n\nYour Whiteout Networks team'; // plaintext body
};
var dummys = [new Email(true, true), new Email(true, false, false, true), new Email(false, true, true), new Email(false), new Email(false), new Email(false), new Email(false), new Email(false), new Email(false), new Email(false), new Email(false), new Email(false), new Email(false), new Email(false), new Email(false), new Email(false), new Email(false), new Email(false)];
2013-10-09 04:22:29 -04:00
return dummys;
}
//
// Directives
//
var ngModule = angular.module('mail-list', []);
2013-12-03 15:07:28 -05:00
ngModule.directive('ngIscroll', function() {
return {
2013-11-13 08:10:43 -05:00
link: function(scope, elm, attrs) {
2013-12-03 15:07:28 -05:00
var model = attrs.ngIscroll;
2013-11-13 08:10:43 -05:00
scope.$watch(model, function() {
var myScroll;
// activate iscroll
myScroll = new IScroll(elm[0], {
mouseWheel: true
});
2013-12-03 15:07:28 -05:00
}, true);
}
};
});
return MailListCtrl;
});