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

354 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;
}
// split text only emails into parts for easier rendering
if (!email.html && typeof email.body === 'string') {
email.bodyDisplayParts = [];
var parts = email.body.split('\n');
parts.forEach(function(part) {
if (part.trim().length > 0) {
email.bodyDisplayParts.push(part);
}
});
2013-09-30 15:22:46 -04:00
}
2013-11-08 17:31:20 -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;
}
var index;
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() {
var trashFolder = _.findWhere($scope.folders, {
type: 'Trash'
});
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) {
console.error(err);
$scope.emails.splice(index, 0, email);
$scope.$apply();
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) {
$scope.onError(err);
updateStatus('Error on sync!');
$scope.$apply();
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) {
$scope.onError(err);
updateStatus('Error on sync!');
$scope.$apply();
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) {
$scope.onError(err);
updateStatus('Error listing cache!');
$scope.$apply();
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) {
$scope.onError(err);
2013-10-04 11:02:27 -04:00
updateStatus('Error marking read!');
$scope.$apply();
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)');
this.subject = "Welcome Max"; // Subject line
this.body = "Hi Max,\n\n" +
"Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet.\n\n" +
"Duis autem vel eum iriure dolor in hendrerit in vulputate velit esse molestie consequat, vel illum dolore eu feugiat nulla facilisis at vero eros et accumsan et iusto odio dignissim qui blandit praesent luptatum zzril delenit augue duis dolore te feugait nulla facilisi. Lorem ipsum dolor sit amet.\n\n" +
2013-09-11 09:19:18 -04:00
"Best regards\nYour whiteout 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-10-19 09:06:23 -04:00
ngModule.directive('ngIscroll', function($timeout) {
return {
link: function(scope, elm) {
2013-10-19 09:06:23 -04:00
$timeout(function() {
var myScroll;
// activate iscroll
myScroll = new IScroll(elm[0], {
mouseWheel: true
});
2013-10-19 09:06:23 -04:00
});
}
};
});
return MailListCtrl;
});