define(function(require) {
'use strict';
var angular = require('angular'),
appController = require('js/app-controller'),
aes = require('cryptoLib/aes-cbc'),
util = require('cryptoLib/util'),
str = require('js/app-config').string,
emailDao;
//
// Controller
//
var WriteCtrl = function($scope, $filter) {
$scope.signature = str.signature;
emailDao = appController._emailDao;
//
// Init
//
$scope.state.writer = {
open: false,
write: function(replyTo) {
this.open = true;
$scope.replyTo = replyTo;
resetFields();
// fill fields depending on replyTo
fillFields(replyTo);
$scope.updatePreview();
$scope.verifyTo();
},
close: function() {
this.open = false;
}
};
function resetFields() {
$scope.writerTitle = 'New email';
$scope.to = '';
$scope.subject = '';
$scope.body = '';
$scope.ciphertextPreview = '';
}
function fillFields(re) {
var from, cleaned, body;
if (!re) {
return;
}
$scope.writerTitle = 'Reply';
// fill recipient field
$scope.to = re.from[0].address;
// fill subject
$scope.subject = 'Re: ' + ((re.subject) ? re.subject.replace('Re: ', '') : '');
// fill text body
from = re.from[0].name || re.from[0].address;
body = '
' + $filter('date')(re.sentDate, 'EEEE, MMM d, yyyy h:mm a') + ' ' + from + ' wrote:
> ';
// clean text from markup if to prevent injection in contenteditable
if (!re.html) {
cleaned = angular.element('
' + re.body + '
').text(); body += cleaned.split('\n').join('' + text + '
').text(); } // // Directives // var ngModule = angular.module('write', []); ngModule.directive('contenteditable', function() { return { require: 'ngModel', link: function(scope, elm, attrs, ctrl) { // view -> model elm.on('keyup keydown', function() { scope.$apply(function() { ctrl.$setViewValue(elm.html()); }); }); // model -> view ctrl.$render = function() { elm.html(ctrl.$viewValue); }; // load init value from DOM ctrl.$setViewValue(elm.html()); } }; }); ngModule.directive('focusMe', function($timeout, $parse) { return { //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); } }); } }; }); 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); }); } }; }); return WriteCtrl; });