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

509 lines
16 KiB
JavaScript
Raw Normal View History

2014-10-02 16:05:44 -04:00
'use strict';
var searchTimeout, firstSelect;
//
// Constants
//
2014-10-02 16:05:44 -04:00
var INIT_DISPLAY_LEN = 20,
SCROLL_DISPLAY_LEN = 10,
FOLDER_TYPE_INBOX = 'Inbox',
NOTIFICATION_INBOX_TIMEOUT = 5000;
var MailListCtrl = function($scope, $routeParams, statusDisplay, notification, email, keychain, dialog) {
2014-10-02 16:05:44 -04:00
//
// Init
//
2013-09-28 10:08:12 -04:00
2014-10-02 16:05:44 -04:00
/**
* Gathers unread notifications to be cancelled later
*/
$scope.pendingNotifications = [];
2014-10-02 16:05:44 -04:00
//
// scope functions
//
2014-02-17 08:31:14 -05:00
$scope.getBody = function(message) {
email.getBody({
2014-10-02 16:05:44 -04:00
folder: currentFolder(),
message: message
2014-10-02 16:05:44 -04:00
}, function(err) {
if (err && err.code !== 42) {
dialog.error(err);
2013-10-04 12:01:42 -04:00
return;
}
2013-11-18 11:44:59 -05:00
2014-10-02 16:05:44 -04:00
// display fetched body
$scope.$digest();
// automatically decrypt if it's the selected message
if (message === currentMessage()) {
email.decryptBody({
message: message
}, dialog.error);
}
2014-10-02 16:05:44 -04:00
});
};
2014-02-21 04:47:49 -05:00
2014-10-02 16:05:44 -04:00
/**
* Called when clicking on an message list item
2014-10-02 16:05:44 -04:00
*/
$scope.select = function(message) {
2014-10-02 16:05:44 -04:00
// unselect an item
if (!message) {
2014-10-02 16:05:44 -04:00
$scope.state.mailList.selected = undefined;
return;
}
2014-02-17 08:31:14 -05:00
$scope.state.mailList.selected = message;
2014-10-02 16:05:44 -04:00
if (!firstSelect) {
// only toggle to read view on 2nd select in mobile mode
$scope.state.read.toggle(true);
}
firstSelect = false;
2014-05-23 04:52:34 -04:00
keychain.refreshKeyForUserId({
userId: message.from[0].address
}, onKeyRefreshed);
2014-05-23 04:52:34 -04:00
2014-10-02 16:05:44 -04:00
function onKeyRefreshed(err) {
if (err) {
dialog.error(err);
2014-10-02 16:05:44 -04:00
}
email.decryptBody({
message: message
}, dialog.error);
2014-10-02 16:05:44 -04:00
// if the message is unread, please sync the new state.
2014-10-02 16:05:44 -04:00
// otherweise forget about it.
if (!message.unread) {
2014-10-02 16:05:44 -04:00
return;
}
// let's close pending notifications for unread messages in the inbox
if (currentFolder().type === FOLDER_TYPE_INBOX) {
while ($scope.pendingNotifications.length) {
notification.close($scope.pendingNotifications.shift());
}
2014-10-02 16:05:44 -04:00
}
$scope.state.actionBar.markMessage(message, false);
2014-10-02 16:05:44 -04:00
}
};
2013-11-11 09:53:34 -05:00
2014-10-02 16:05:44 -04:00
//
// watch tasks
//
2014-10-02 16:05:44 -04:00
/**
* List messages from folder when user changes folder
2014-10-02 16:05:44 -04:00
*/
$scope._stopWatchTask = $scope.$watch('state.nav.currentFolder', function() {
if (!currentFolder()) {
return;
}
2014-10-02 16:05:44 -04:00
// reset searchFilter
$scope.searchText = undefined;
2014-10-02 16:05:44 -04:00
// in development, display dummy mail objects
if ($routeParams.dev) {
statusDisplay.update('Last update: ', new Date());
2014-10-02 16:05:44 -04:00
currentFolder().messages = createDummyMails();
return;
}
2014-10-02 16:05:44 -04:00
// display and select first
openCurrentFolder();
});
2014-10-02 16:05:44 -04:00
$scope.watchMessages = $scope.$watchCollection('state.nav.currentFolder.messages', function(messages) {
if (!messages) {
return;
}
2014-10-02 16:05:44 -04:00
// sort message by uid
currentFolder().messages.sort(byUidDescending);
// set display buffer to first messages
$scope.displayMessages = currentFolder().messages.slice(0, INIT_DISPLAY_LEN);
2014-10-02 16:05:44 -04:00
// Shows the next message based on the uid of the currently selected element
if (currentFolder().messages.indexOf(currentMessage()) === -1) {
firstSelect = true; // reset first selection
$scope.select($scope.displayMessages[0]);
}
});
2014-10-02 16:05:44 -04:00
/**
* display more items (for infinite scrolling)
*/
$scope.displayMore = function() {
if (!currentFolder() || !$scope.displayMessages) {
// folders not yet initialized
return;
}
2014-10-02 16:05:44 -04:00
var len = currentFolder().messages.length,
dLen = $scope.displayMessages.length;
2014-10-02 16:05:44 -04:00
if (dLen === len || $scope.searchText) {
// all messages are already displayed or we're in search mode
return;
}
2014-10-02 16:05:44 -04:00
// copy next interval of messages to the end of the display messages array
var next = currentFolder().messages.slice(dLen, dLen + SCROLL_DISPLAY_LEN);
Array.prototype.push.apply($scope.displayMessages, next);
};
2014-10-02 16:05:44 -04:00
/**
* This method is called when the user changes the searchText
*/
$scope.displaySearchResults = function(searchText) {
if (searchTimeout) {
// remove timeout to wait for user typing query
clearTimeout(searchTimeout);
}
2014-10-02 16:05:44 -04:00
if (!searchText) {
// set display buffer to first messages
$scope.displayMessages = currentFolder().messages.slice(0, INIT_DISPLAY_LEN);
statusDisplay.setSearching(false);
statusDisplay.update('Online');
2014-10-02 16:05:44 -04:00
return;
}
2014-10-02 16:05:44 -04:00
// display searching spinner
statusDisplay.setSearching(true);
statusDisplay.update('Searching ...');
2014-10-02 16:05:44 -04:00
searchTimeout = setTimeout(function() {
$scope.$apply(function() {
// filter relevant messages
$scope.displayMessages = $scope.search(currentFolder().messages, searchText);
statusDisplay.setSearching(false);
statusDisplay.update('Matches in this folder');
2014-10-02 16:05:44 -04:00
});
}, 500);
};
2014-10-02 16:05:44 -04:00
/**
* Do full text search on messages. Parse meta data first
*/
$scope.search = function(messages, searchText) {
// don't filter on empty searchText
if (!searchText) {
return messages;
}
2014-10-02 16:05:44 -04:00
// escape search string
searchText = searchText.replace(/([.*+?^${}()|\[\]\/\\])/g, "\\$1");
// compare all strings (case insensitive)
var regex = new RegExp(searchText, 'i');
2014-10-02 16:05:44 -04:00
function contains(input) {
if (!input) {
return false;
}
2014-10-02 16:05:44 -04:00
return regex.test(input);
}
2014-10-02 16:05:44 -04:00
function checkAddresses(header) {
if (!header || !header.length) {
return false;
}
2014-10-02 16:05:44 -04:00
for (var i = 0; i < header.length; i++) {
if (contains(header[i].name) || contains(header[i].address)) {
return true;
}
}
2014-10-02 16:05:44 -04:00
return false;
}
/**
2014-10-02 16:05:44 -04:00
* Filter meta data first and then only look at plaintext and decrypted message bodies
*/
2014-10-02 16:05:44 -04:00
function matchMetaDataFirst(m) {
// compare subject
if (contains(m.subject)) {
return true;
}
2014-10-02 16:05:44 -04:00
// compares address headers
if (checkAddresses(m.from) || checkAddresses(m.to) || checkAddresses(m.cc) || checkAddresses(m.bcc)) {
return true;
}
// compare plaintext body
if (m.body && !m.encrypted && contains(m.body)) {
return true;
}
// compare decrypted body
if (m.body && m.encrypted && m.decrypted && contains(m.body)) {
return true;
}
// compare plaintex html body
if (m.html && !m.encrypted && contains(m.html)) {
return true;
}
// compare decrypted html body
if (m.html && m.encrypted && m.decrypted && contains(m.html)) {
return true;
}
return false;
}
2013-09-28 10:08:12 -04:00
2014-10-02 16:05:44 -04:00
// user native js Array.filter
return messages.filter(matchMetaDataFirst);
};
2014-10-02 16:05:44 -04:00
/**
* Sync current folder when client comes back online
*/
$scope.watchOnline = $scope.$watch('account.online', function(isOnline) {
if (isOnline) {
statusDisplay.update('Online');
2014-10-02 16:05:44 -04:00
openCurrentFolder();
} else {
statusDisplay.update('Offline mode');
2013-09-26 07:26:57 -04:00
}
2014-10-02 16:05:44 -04:00
}, true);
2013-09-30 15:22:46 -04:00
2014-10-02 16:05:44 -04:00
//
// Helper Functions
//
2014-10-02 16:05:44 -04:00
function openCurrentFolder() {
if (!currentFolder()) {
return;
}
email.openFolder({
2014-10-02 16:05:44 -04:00
folder: currentFolder()
}, function(error) {
// dont wait until scroll to load visible mail bodies
$scope.loadVisibleBodies();
2014-05-08 10:25:20 -04:00
2014-10-02 16:05:44 -04:00
// don't display error for offline case
if (error && error.code === 42) {
return;
}
dialog.error(error);
2014-10-02 16:05:44 -04:00
});
}
2014-05-08 10:25:20 -04:00
2014-10-02 16:05:44 -04:00
function currentFolder() {
return $scope.state.nav.currentFolder;
}
2014-05-08 10:25:20 -04:00
2014-10-02 16:05:44 -04:00
function currentMessage() {
return $scope.state.mailList.selected;
}
2014-05-08 10:25:20 -04:00
2014-10-02 16:05:44 -04:00
//
// Notification API
//
2014-05-08 10:25:20 -04:00
(email || {}).onIncomingMessage = function(msgs) {
2014-10-02 16:05:44 -04:00
var note, title, message, unreadMsgs;
2014-05-08 10:25:20 -04:00
2014-10-02 16:05:44 -04:00
unreadMsgs = msgs.filter(function(msg) {
return msg.unread;
});
if (unreadMsgs.length === 0) {
return;
}
2014-10-02 16:05:44 -04:00
if (unreadMsgs.length === 1) {
title = unreadMsgs[0].from[0].name || unreadMsgs[0].from[0].address;
message = unreadMsgs[0].subject;
} else {
title = unreadMsgs.length + ' new messages';
message = _.pluck(unreadMsgs, 'subject').join('\n');
}
note = notification.create({
title: title,
message: message,
onClick: function() {
// force toggle into read mode when notification is clicked
firstSelect = false;
// remove from pending notificatiosn
var index = $scope.pendingNotifications.indexOf(note);
if (index !== -1) {
$scope.pendingNotifications.splice(index, 1);
}
2014-10-02 16:05:44 -04:00
// mark message as read
$scope.select(_.findWhere(currentFolder().messages, {
uid: unreadMsgs[0].uid
}));
},
timeout: NOTIFICATION_INBOX_TIMEOUT
2014-10-02 16:05:44 -04:00
});
$scope.pendingNotifications.push(note);
2013-09-26 07:26:57 -04:00
};
2014-10-02 16:05:44 -04:00
};
2014-10-02 16:05:44 -04:00
//
// Directives
//
2014-10-02 16:05:44 -04:00
var ngModule = angular.module('mail-list', []);
2014-10-02 16:05:44 -04:00
ngModule.directive('listScroll', function() {
return {
link: function(scope, elm, attrs) {
var model = attrs.listScroll,
listEl = elm[0],
scrollTimeout;
2014-10-02 16:05:44 -04:00
/*
* iterates over the mails in the mail list and loads their bodies if they are visible in the viewport
*/
scope.loadVisibleBodies = function() {
var listBorder = listEl.getBoundingClientRect(),
top = listBorder.top,
bottom = listBorder.bottom,
listItems = listEl.children[0].children,
inViewport = false,
listItem, message,
isPartiallyVisibleTop, isPartiallyVisibleBottom, isVisible,
displayMessages = scope[model];
if (!top && !bottom) {
// list not visible
return;
}
for (var i = 0, len = listItems.length; i < len; i++) {
// the n-th list item (the dom representation of an message) corresponds to
2014-10-02 16:05:44 -04:00
// the n-th message model in the filteredMessages array
listItem = listItems.item(i).getBoundingClientRect();
if (!displayMessages || displayMessages.length <= i) {
// stop if i get larger than the size of filtered messages
break;
}
2014-10-02 16:05:44 -04:00
message = displayMessages[i];
2014-10-02 16:05:44 -04:00
isPartiallyVisibleTop = listItem.top < top && listItem.bottom > top; // a portion of the list item is visible on the top
isPartiallyVisibleBottom = listItem.top < bottom && listItem.bottom > bottom; // a portion of the list item is visible on the bottom
isVisible = (listItem.top || listItem.bottom) && listItem.top >= top && listItem.bottom <= bottom; // the list item is visible as a whole
if (isPartiallyVisibleTop || isVisible || isPartiallyVisibleBottom) {
// we are now iterating over visible elements
inViewport = true;
// load mail body of visible
scope.getBody(message);
} else if (inViewport) {
// we are leaving the viewport, so stop iterating over the items
break;
}
2014-10-02 16:05:44 -04:00
}
};
2014-10-02 16:05:44 -04:00
// load body when scrolling
listEl.onscroll = function() {
if (scrollTimeout) {
// remove timeout so that only scroll end
clearTimeout(scrollTimeout);
}
scrollTimeout = setTimeout(function() {
scope.loadVisibleBodies();
2014-10-02 16:05:44 -04:00
}, 300);
};
2014-10-02 16:05:44 -04:00
// load the visible message bodies, when the list is re-initialized and when scrolling stopped
scope.$watchCollection(model, function() {
scope.loadVisibleBodies();
});
}
2014-10-02 16:05:44 -04:00
};
});
function byUidDescending(a, b) {
if (a.uid < b.uid) {
return 1;
} else if (b.uid < a.uid) {
return -1;
} else {
return 0;
}
2014-10-02 16:05:44 -04:00
}
// Helper for development mode
function createDummyMails() {
var uid = 1000000;
var Email = function(unread, attachments, answered) {
this.uid = uid--;
this.from = [{
name: 'Whiteout Support',
address: 'support@whiteout.io'
}]; // sender address
this.to = [{
address: 'max.musterman@gmail.com'
}, {
address: 'max.musterman@gmail.com'
}]; // list of receivers
this.cc = [{
address: 'john.doe@gmail.com'
}]; // list of receivers
this.attachments = attachments ? [{
"filename": "a.md",
"filesize": 123,
"mimeType": "text/x-markdown",
"part": "2",
"content": null
}, {
"filename": "b.md",
"filesize": 456,
"mimeType": "text/x-markdown",
"part": "3",
"content": null
}, {
"filename": "c.md",
"filesize": 789,
"mimeType": "text/x-markdown",
"part": "4",
"content": null
}] : [];
this.unread = unread;
this.answered = answered;
this.sentDate = new Date('Thu Sep 19 2013 20:41:23 GMT+0200 (CEST)');
this.subject = 'Getting started'; // Subject line
this.body = 'And a good day to you too sir. \n' +
'\n' +
'Thursday, Apr 24, 2014 3:33 PM safewithme.testuser@gmail.com wrote:\n' +
'> adsfadfasdfasdfasfdasdfasdfas\n' +
'\n' +
'http://example.com\n' +
'\n' +
'> Tuesday, Mar 25, 2014 4:19 PM gianniarcore@gmail.com wrote:\n' +
'>> from 0.7.0.1\n' +
'>>\n' +
'>> God speed!'; // plaintext body
//this.html = '<!DOCTYPE html><html><head></head><body><h1 style="border: 1px solid red; width: 500px;">Hello there' + Math.random() + '</h1></body></html>';
2014-10-02 16:05:44 -04:00
this.encrypted = true;
this.decrypted = true;
};
2014-10-02 16:05:44 -04:00
var dummies = [],
i = 100;
while (i--) {
// every second/third/fourth dummy mail with unread/attachments/answered
dummies.push(new Email((i % 2 === 0), (i % 3 === 0), (i % 5 === 0)));
}
2014-10-02 16:05:44 -04:00
return dummies;
}
2014-10-08 06:34:34 -04:00
module.exports = MailListCtrl;