2013-09-13 08:11:47 -04:00
|
|
|
define(function(require) {
|
2013-09-11 16:11:26 -04:00
|
|
|
'use strict';
|
|
|
|
|
2013-09-13 08:11:47 -04:00
|
|
|
var angular = require('angular'),
|
2013-09-15 11:05:37 -04:00
|
|
|
appController = require('js/app-controller'),
|
2013-09-13 08:11:47 -04:00
|
|
|
aes = require('cryptoLib/aes-cbc'),
|
|
|
|
util = require('cryptoLib/util'),
|
2013-09-15 11:05:37 -04:00
|
|
|
str = require('js/app-config').string,
|
|
|
|
emailDao;
|
2013-09-13 08:11:47 -04:00
|
|
|
|
|
|
|
//
|
|
|
|
// Controller
|
|
|
|
//
|
2013-09-12 11:22:17 -04:00
|
|
|
|
2013-10-18 21:32:00 -04:00
|
|
|
var WriteCtrl = function($scope, $filter) {
|
2013-09-13 08:11:47 -04:00
|
|
|
$scope.signature = str.signature;
|
2013-10-18 21:32:00 -04:00
|
|
|
emailDao = appController._emailDao;
|
2013-09-13 08:11:47 -04:00
|
|
|
|
2013-10-12 13:39:09 -04:00
|
|
|
//
|
|
|
|
// Init
|
|
|
|
//
|
|
|
|
|
2013-10-18 21:32:00 -04:00
|
|
|
$scope.$watch('writerOpen', function() {
|
|
|
|
resetFields();
|
|
|
|
if ($scope.writerReply) {
|
|
|
|
fillFields($scope.selected);
|
2013-10-18 21:55:12 -04:00
|
|
|
$scope.updatePreview();
|
2013-10-18 21:32:00 -04:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
function resetFields() {
|
|
|
|
$scope.writerTitle = 'New email';
|
|
|
|
$scope.to = undefined;
|
|
|
|
$scope.subject = undefined;
|
2013-10-18 21:55:12 -04:00
|
|
|
if ($scope.$$childTail) {
|
|
|
|
$scope.$$childTail.body = '';
|
|
|
|
}
|
|
|
|
$scope.ciphertextPreview = undefined;
|
2013-10-18 21:32:00 -04:00
|
|
|
}
|
2013-10-12 13:39:09 -04:00
|
|
|
|
|
|
|
function fillFields(re) {
|
2013-10-13 07:49:37 -04:00
|
|
|
var from, body, bodyRows;
|
|
|
|
|
2013-10-12 13:39:09 -04:00
|
|
|
if (!re) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-10-18 21:32:00 -04:00
|
|
|
$scope.writerTitle = 'Reply';
|
2013-10-12 13:39:09 -04:00
|
|
|
// fill recipient field
|
|
|
|
$scope.to = re.from[0].address;
|
|
|
|
// fill subject
|
|
|
|
$scope.subject = 'Re: ' + ((re.subject) ? re.subject.replace('Re: ', '') : '');
|
|
|
|
|
|
|
|
// fill text body
|
2013-10-13 07:49:37 -04:00
|
|
|
from = re.from[0].name || re.from[0].address;
|
|
|
|
body = '<br><br>' + $filter('date')(re.sentDate, 'EEEE, MMM d, yyyy h:mm a') + ' ' + from + ' wrote:';
|
|
|
|
bodyRows = re.body.split('\n');
|
2013-10-12 13:39:09 -04:00
|
|
|
bodyRows.forEach(function(row) {
|
2013-10-13 07:49:37 -04:00
|
|
|
body += (!re.html) ? '<br>' + row : '';
|
2013-10-12 13:39:09 -04:00
|
|
|
});
|
2013-10-18 21:55:12 -04:00
|
|
|
$scope.$$childTail.body = body;
|
2013-10-12 13:39:09 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// Editing
|
|
|
|
//
|
|
|
|
|
2013-09-13 08:11:47 -04:00
|
|
|
// generate key,iv for encryption preview
|
|
|
|
var key = util.random(128),
|
|
|
|
iv = util.random(128);
|
|
|
|
|
|
|
|
$scope.updatePreview = function() {
|
2013-10-18 20:58:53 -04:00
|
|
|
var body = $scope.$$childTail.body;
|
2013-09-15 11:05:37 -04:00
|
|
|
// remove generated html from body
|
|
|
|
body = parseBody(body);
|
2013-09-14 08:23:46 -04:00
|
|
|
|
2013-10-05 08:16:04 -04:00
|
|
|
// Although this does encrypt live using AES, this is just for show. The plaintext is encrypted seperately before sending the email.
|
2013-10-18 20:58:53 -04:00
|
|
|
$scope.ciphertextPreview = (body) ? aes.encrypt(body, key, iv) : '';
|
2013-09-13 08:11:47 -04:00
|
|
|
};
|
2013-09-15 11:05:37 -04:00
|
|
|
|
|
|
|
$scope.sendEmail = function() {
|
|
|
|
var to, body, email;
|
|
|
|
|
|
|
|
// validate recipients
|
|
|
|
to = $scope.to.replace(/\s/g, '').split(/[,;]/);
|
|
|
|
if (!to || to.length < 1) {
|
|
|
|
console.log('Seperate recipients with a comma!');
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
body = $scope.body;
|
|
|
|
// remove generated html from body
|
|
|
|
body = parseBody(body);
|
|
|
|
|
|
|
|
email = {
|
|
|
|
to: [], // list of receivers
|
|
|
|
subject: $scope.subject, // Subject line
|
|
|
|
body: body // plaintext body
|
|
|
|
};
|
|
|
|
email.from = [{
|
|
|
|
name: '',
|
|
|
|
address: emailDao._account.emailAddress
|
|
|
|
}];
|
|
|
|
to.forEach(function(address) {
|
|
|
|
email.to.push({
|
|
|
|
name: '',
|
|
|
|
address: address
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
emailDao.smtpSend(email, function(err) {
|
|
|
|
if (err) {
|
|
|
|
console.log(err);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (window.chrome && chrome.app.window) {
|
|
|
|
// close the chrome window
|
|
|
|
chrome.app.window.current().close();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
};
|
2013-09-12 11:22:17 -04:00
|
|
|
};
|
2013-09-11 16:11:26 -04:00
|
|
|
|
2013-09-15 11:05:37 -04:00
|
|
|
function parseBody(body) {
|
|
|
|
function has(substr) {
|
|
|
|
return (body.indexOf(substr) !== -1);
|
|
|
|
}
|
|
|
|
while (has('<div>')) {
|
|
|
|
body = body.replace('<div>', '\n');
|
|
|
|
}
|
|
|
|
while (has('<br>')) {
|
|
|
|
body = body.replace('<br>', '\n');
|
|
|
|
}
|
|
|
|
while (has('</div>')) {
|
|
|
|
body = body.replace('</div>', '');
|
|
|
|
}
|
|
|
|
|
|
|
|
return body;
|
|
|
|
}
|
|
|
|
|
2013-09-13 08:11:47 -04:00
|
|
|
//
|
|
|
|
// Directives
|
|
|
|
//
|
|
|
|
|
|
|
|
var ngModule = angular.module('write', []);
|
|
|
|
ngModule.directive('contenteditable', function() {
|
|
|
|
return {
|
|
|
|
require: 'ngModel',
|
|
|
|
link: function(scope, elm, attrs, ctrl) {
|
|
|
|
// view -> model
|
2013-10-13 07:49:37 -04:00
|
|
|
elm.on('keyup keydown', function() {
|
2013-09-13 08:11:47 -04:00
|
|
|
scope.$apply(function() {
|
|
|
|
ctrl.$setViewValue(elm.html());
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
// model -> view
|
2013-10-12 13:39:09 -04:00
|
|
|
ctrl.$render = function() {
|
|
|
|
elm.html(ctrl.$viewValue);
|
2013-09-13 08:11:47 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
// load init value from DOM
|
|
|
|
ctrl.$setViewValue(elm.html());
|
|
|
|
}
|
|
|
|
};
|
|
|
|
});
|
2013-10-12 13:39:09 -04:00
|
|
|
|
2013-09-13 08:11:47 -04:00
|
|
|
ngModule.directive('focusMe', function($timeout) {
|
|
|
|
return {
|
|
|
|
link: function(scope, element) {
|
|
|
|
$timeout(function() {
|
|
|
|
element[0].focus();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
};
|
|
|
|
});
|
|
|
|
|
2013-09-11 16:11:26 -04:00
|
|
|
return WriteCtrl;
|
|
|
|
});
|