mail/src/js/controller/write.js

261 lines
7.3 KiB
JavaScript
Raw Normal View History

define(function(require) {
2013-09-11 16:11:26 -04:00
'use strict';
var angular = require('angular'),
2013-09-15 11:05:37 -04:00
appController = require('js/app-controller'),
aes = require('cryptoLib/aes-cbc'),
util = require('cryptoLib/util'),
2013-09-15 11:05:37 -04:00
emailDao;
//
// Controller
//
2013-09-12 11:22:17 -04:00
2013-10-18 21:32:00 -04:00
var WriteCtrl = function($scope, $filter) {
emailDao = appController._emailDao;
2013-10-12 13:39:09 -04:00
//
// Init
//
2013-11-08 15:55:08 -05:00
$scope.state.writer = {
open: false,
write: function(replyTo) {
this.open = true;
$scope.replyTo = replyTo;
2013-11-08 15:55:08 -05:00
resetFields();
// fill fields depending on replyTo
fillFields(replyTo);
$scope.updatePreview();
2013-11-08 15:55:08 -05:00
$scope.verifyTo();
},
close: function() {
this.open = false;
2013-10-18 21:32:00 -04:00
}
2013-11-08 15:55:08 -05:00
};
2013-10-18 21:32:00 -04:00
function resetFields() {
$scope.writerTitle = 'New email';
2013-10-21 09:02:54 -04:00
$scope.to = '';
$scope.subject = '';
$scope.body = '';
$scope.ciphertextPreview = '';
2013-10-18 21:32:00 -04:00
}
2013-10-12 13:39:09 -04:00
function fillFields(re) {
2013-11-26 13:06:37 -05:00
var from, body;
2013-10-13 07:49:37 -04:00
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;
2013-11-26 13:06:37 -05:00
body = '\n\n' + $filter('date')(re.sentDate, 'EEEE, MMM d, yyyy h:mm a') + ' ' + from + ' wrote:\n> ';
2013-11-26 13:06:37 -05:00
// only display non html mails in reply part
if (!re.html) {
2013-11-26 13:06:37 -05:00
body += re.body.split('\n').join('\n> ');
$scope.body = body;
}
2013-10-12 13:39:09 -04:00
}
//
// Editing headers
//
$scope.verifyTo = function() {
if (!$scope.to) {
resetDisplay();
return;
}
// set display to insecure while fetching keys
2013-11-11 10:11:06 -05:00
$scope.toKey = undefined;
displayInsecure();
// check if to address is contained in known public keys
emailDao._keychain.getReceiverPublicKey($scope.to, function(err, key) {
if (err) {
$scope.onError(err);
return;
}
// compare again since model could have changed during the roundtrip
if (key && key.userId === $scope.to) {
2013-11-11 10:11:06 -05:00
$scope.toKey = key;
displaySecure();
$scope.$apply();
}
});
};
function resetDisplay() {
$scope.toSecure = undefined;
$scope.sendBtnText = undefined;
}
function displaySecure() {
$scope.toSecure = true;
$scope.sendBtnText = 'Send securely';
}
function displayInsecure() {
$scope.toSecure = false;
$scope.sendBtnText = 'Invite & send securely';
}
//
// Editing email body
2013-10-12 13:39:09 -04:00
//
// generate key,iv for encryption preview
var key = util.random(128),
iv = util.random(128);
$scope.updatePreview = function() {
var body = $scope.body.trim();
2013-09-14 08:23:46 -04:00
// Although this does encrypt live using AES, this is just for show. The plaintext is encrypted seperately before sending the email.
$scope.ciphertextPreview = (body) ? aes.encrypt(body, key, iv) : '';
};
2013-09-15 11:05:37 -04:00
$scope.sendToOutbox = function() {
2013-11-26 13:06:37 -05:00
var to, email;
2013-09-15 11:05:37 -04:00
// validate recipients
to = $scope.to.replace(/\s/g, '').split(/[,;]/);
if (!to || to.length < 1) {
2013-11-11 10:11:06 -05:00
$scope.onError({
errMsg: 'Seperate recipients with a comma!',
sync: true
2013-11-11 10:11:06 -05:00
});
return;
}
2013-09-15 11:05:37 -04:00
email = {
to: [], // list of receivers
subject: $scope.subject, // Subject line
2013-11-26 13:06:37 -05:00
body: $scope.body // use parsed plaintext body
2013-09-15 11:05:37 -04:00
};
email.from = [{
name: '',
address: emailDao._account.emailAddress
}];
to.forEach(function(address) {
email.to.push({
name: '',
address: address
});
});
emailDao.store(email, function(err) {
2013-09-15 11:05:37 -04:00
if (err) {
$scope.onError(err);
2013-09-15 11:05:37 -04:00
return;
}
2013-11-08 17:31:20 -05:00
$scope.state.writer.close();
$scope.$apply();
$scope.emptyOutbox($scope.onOutboxUpdate);
2013-12-05 12:50:03 -05:00
markAnswered();
2013-09-15 11:05:37 -04:00
});
};
2013-12-05 12:50:03 -05:00
function markAnswered() {
// mark replyTo as answered
if (!$scope.replyTo) {
return;
}
// mark list object
$scope.replyTo.answered = true;
2013-12-05 12:50:03 -05:00
emailDao.sync({
folder: $scope.state.nav.currentFolder.path
}, $scope.onError);
}
2013-09-12 11:22:17 -04:00
};
2013-09-11 16:11:26 -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() {
scope.$apply(function() {
2013-11-26 13:06:37 -05:00
// set model
ctrl.$setViewValue(elm[0].innerText);
});
});
// model -> view
2013-10-12 13:39:09 -04:00
ctrl.$render = function() {
2013-11-26 13:06:37 -05:00
elm[0].innerText = ctrl.$viewValue;
};
// load init value from DOM
2013-11-26 13:06:37 -05:00
ctrl.$setViewValue(elm[0].innerText);
}
};
});
2013-10-12 13:39:09 -04:00
2013-10-19 09:06:23 -04:00
ngModule.directive('focusMe', function($timeout, $parse) {
return {
2013-10-19 09:06:23 -04:00
//scope: true, // optionally create a child scope
link: function(scope, element, attrs) {
var model = $parse(attrs.focusMe);
scope.$watch(model, function(value) {
if (value === true) {
$timeout(function() {
element[0].focus();
}, 100);
2013-10-19 09:06:23 -04:00
}
});
}
};
});
ngModule.directive('focusChild', function() {
return {
//scope: true, // optionally create a child scope
link: function(scope, element) {
element[0].onclick = function() {
element[0].children[0].focus();
};
}
};
});
ngModule.directive('autoSize', function($parse) {
return {
require: 'ngModel',
link: function(scope, elm, attrs) {
var model = $parse(attrs.autoSize);
scope.$watch(model, function(value) {
if (!value) {
return;
}
var width = ((value.length + 2) * 8) + 'px';
elm.css('width', width);
});
}
};
});
2013-09-11 16:11:26 -04:00
return WriteCtrl;
});