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

355 lines
12 KiB
JavaScript
Raw Normal View History

define(function(require) {
'use strict';
2013-09-11 17:31:08 -04:00
var _ = require('underscore'),
angular = require('angular'),
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,
2013-09-15 11:05:37 -04:00
emailDao;
var MailListCtrl = function($scope) {
2013-09-27 11:10:11 -04:00
var offset = 0,
num = 100,
firstSelect = true;
2013-09-26 07:26:57 -04:00
2013-11-08 17:31:20 -05:00
//
// Init
//
2013-09-15 11:05:37 -04:00
emailDao = appController._emailDao;
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);
}
function notificationClicked(uidString) {
var email, uid = parseInt(uidString, 10);
if (isNaN(uid)) {
return;
}
email = _.findWhere($scope.emails, {
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;
2013-10-04 11:02:27 -04:00
// mark selected message as 'read'
markAsRead(email);
};
2013-11-08 17:31:20 -05:00
$scope.synchronize = function(callback) {
2013-09-28 10:08:12 -04:00
updateStatus('Syncing ...');
// sync from imap to local db
syncImapFolder({
2013-09-30 15:22:46 -04:00
folder: getFolder().path,
2013-09-28 10:08:12 -04:00
offset: -num,
num: offset
}, function() {
// list again from local db after syncing
listLocalMessages({
2013-09-30 15:22:46 -04:00
folder: getFolder().path,
2013-09-28 10:08:12 -04:00
offset: offset,
num: num
}, function() {
updateStatus('Last update: ', new Date());
if (callback) {
callback();
}
2013-09-28 10:08:12 -04:00
});
});
};
2013-11-08 17:31:20 -05:00
$scope.remove = function(email) {
if (!email) {
return;
}
2013-11-11 09:53:34 -05:00
var index, trashFolder;
trashFolder = _.findWhere($scope.folders, {
type: 'Trash'
});
if (getFolder() === trashFolder) {
$scope.state.dialog = {
open: true,
2013-11-11 10:22:30 -05:00
title: 'Delete',
message: 'Delete this message permanently?',
2013-11-11 09:53:34 -05:00
callback: function(ok) {
if (!ok) {
return;
}
removeLocalAndShowNext();
removeRemote();
}
};
return;
}
2013-11-08 17:31:20 -05:00
removeLocalAndShowNext();
removeRemote();
function removeLocalAndShowNext() {
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.state.mailList.selected = undefined;
}
$scope.emails.splice(index, 1);
}
function removeRemote() {
if (getFolder() === trashFolder) {
emailDao.imapDeleteMessage({
folder: getFolder().path,
uid: email.uid
}, moved);
return;
}
emailDao.imapMoveMessage({
folder: getFolder().path,
uid: email.uid,
destination: trashFolder.path
}, moved);
}
function moved(err) {
if (err) {
$scope.emails.splice(index, 0, email);
$scope.onError(err);
2013-11-08 17:31:20 -05:00
return;
}
}
};
$scope.$watch('state.nav.currentFolder', function() {
if (!getFolder()) {
return;
}
2013-09-30 15:22:46 -04:00
// production... in chrome packaged app
if (window.chrome && chrome.identity) {
initList();
return;
}
2013-10-09 04:22:29 -04:00
2013-09-30 15:22:46 -04:00
// development... display dummy mail objects
firstSelect = true;
2013-10-09 04:22:29 -04:00
updateStatus('Last update: ', new Date());
2013-11-08 17:31:20 -05:00
$scope.emails = createDummyMails();
2013-10-09 04:22:29 -04:00
$scope.select($scope.emails[0]);
});
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
//
function notificationForEmail(email) {
chrome.notifications.create('' + email.uid, {
type: 'basic',
title: email.from[0].address,
message: email.subject.split(str.subjectPrefix)[1],
iconUrl: chrome.runtime.getURL(cfg.iconPath)
}, function() {});
}
2013-09-26 07:26:57 -04:00
function initList() {
2013-09-28 10:08:12 -04:00
updateStatus('Read cache ...');
2013-09-26 07:26:57 -04:00
// list messaged from local db
listLocalMessages({
2013-09-30 15:22:46 -04:00
folder: getFolder().path,
2013-09-26 07:26:57 -04:00
offset: offset,
num: num
2013-10-11 17:02:37 -04:00
}, function sync() {
2013-09-30 15:22:46 -04:00
updateStatus('Syncing ...');
$scope.$apply();
// sync imap folder to local db
$scope.synchronize();
});
}
2013-09-26 07:26:57 -04:00
function syncImapFolder(options, callback) {
2013-09-30 15:22:46 -04:00
emailDao.unreadMessages(getFolder().path, function(err, unreadCount) {
2013-09-26 07:26:57 -04:00
if (err) {
updateStatus('Error on sync!');
$scope.onError(err);
2013-09-26 07:26:57 -04:00
return;
}
2013-09-30 15:22:46 -04:00
// set unread count in folder model
2013-10-18 21:55:12 -04:00
getFolder().count = unreadCount;
2013-09-30 15:22:46 -04:00
$scope.$apply();
2013-09-11 17:31:08 -04:00
2013-09-30 15:22:46 -04:00
emailDao.imapSync(options, function(err) {
if (err) {
updateStatus('Error on sync!');
$scope.onError(err);
2013-09-30 15:22:46 -04:00
return;
}
callback();
});
2013-09-26 07:26:57 -04:00
});
}
2013-09-11 17:31:08 -04:00
2013-09-26 07:26:57 -04:00
function listLocalMessages(options, callback) {
2013-10-13 06:46:24 -04:00
firstSelect = true;
2013-09-26 07:26:57 -04:00
emailDao.listMessages(options, function(err, emails) {
2013-09-11 17:31:08 -04:00
if (err) {
updateStatus('Error listing cache!');
$scope.onError(err);
2013-09-11 17:31:08 -04:00
return;
}
2013-09-26 07:26:57 -04:00
callback(emails);
2013-09-28 10:08:12 -04:00
displayEmails(emails);
2013-09-05 04:59:55 -04:00
});
2013-09-26 07:26:57 -04:00
}
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.emails = [];
$scope.select();
$scope.$apply();
2013-09-26 07:26:57 -04:00
return;
}
2013-09-26 07:26:57 -04:00
// sort by uid
emails = _.sortBy(emails, function(e) {
return -e.uid;
});
2013-11-08 17:31:20 -05:00
$scope.emails = emails;
2013-09-26 07:26:57 -04:00
$scope.select($scope.emails[0]);
$scope.$apply();
}
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-10-04 11:02:27 -04:00
function markAsRead(email) {
// don't mark top selected email automatically
if (firstSelect) {
firstSelect = false;
return;
}
2013-10-04 11:02:27 -04:00
$scope.state.read.toggle(true);
if (!window.chrome || !chrome.socket) {
return;
}
2013-10-24 09:18:16 -04:00
if (!email.unread) {
return;
}
email.unread = false;
2013-10-04 11:02:27 -04:00
emailDao.imapMarkMessageRead({
folder: getFolder().path,
uid: email.uid
}, function(err) {
if (err) {
updateStatus('Error marking read!');
$scope.onError(err);
2013-10-04 11:02:27 -04:00
return;
}
});
}
2013-09-26 07:26:57 -04:00
};
2013-10-09 04:22:29 -04:00
function createDummyMails() {
var Email = function(unread, attachments, answered, html) {
2013-09-06 12:34:36 -04:00
this.uid = '1';
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-11-13 08:10:43 -05:00
ngModule.directive('ngIscroll', function($parse) {
return {
2013-11-13 08:10:43 -05:00
link: function(scope, elm, attrs) {
var model = $parse(attrs.ngIscroll);
scope.$watch(model, function() {
var myScroll;
// activate iscroll
myScroll = new IScroll(elm[0], {
mouseWheel: true
});
2013-10-19 09:06:23 -04:00
});
}
};
});
return MailListCtrl;
});