1
0
mirror of https://github.com/moparisthebest/mail synced 2024-11-10 11:15:01 -05:00
mail/src/js/controller/mail-list.js

293 lines
9.9 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-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-10-17 04:36:50 -04:00
$scope.$parent.select = $scope.select = function(email) {
2013-10-04 12:01:42 -04:00
if (!email) {
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
}
$scope.selected = email;
2013-09-18 14:45:58 -04:00
// set selected in parent scope ro it can be displayed in the read view
$scope.$parent.selected = $scope.selected;
2013-10-04 11:02:27 -04:00
// mark selected message as 'read'
markAsRead(email);
};
$scope.$parent.synchronize = $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-09-30 15:22:46 -04:00
$scope.$watch('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-10-16 12:56:18 -04:00
$scope.$parent.emails = $scope.emails = createDummyMails();
2013-10-09 04:22:29 -04:00
$scope.select($scope.emails[0]);
});
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) {
console.log(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) {
console.log(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) {
console.log(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 : '';
$scope.$parent.lastUpdateLbl = $scope.lastUpdateLbl;
$scope.$parent.lastUpdate = $scope.lastUpdate;
}
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-10-16 12:56:18 -04:00
$scope.$parent.emails = $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() {
return $scope.$parent.currentFolder;
}
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.openReadMode();
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) {
console.log(err);
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;
});