029707c1fe11630528dfd8974dd3d6b32c7928b8
[pixelated-user-agent.git] / web-ui / app / js / mail_view / ui / mail_view.js
1 /*
2  * Copyright (c) 2014 ThoughtWorks, Inc.
3  *
4  * Pixelated is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU Affero General Public License as published by
6  * the Free Software Foundation, either version 3 of the License, or
7  * (at your option) any later version.
8  *
9  * Pixelated is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU Affero General Public License for more details.
13  *
14  * You should have received a copy of the GNU Affero General Public License
15  * along with Pixelated. If not, see <http://www.gnu.org/licenses/>.
16  */
17
18 define(
19   [
20     'flight/lib/component',
21     'views/templates',
22     'mail_view/ui/mail_actions',
23     'helpers/view_helper',
24     'mixins/with_hide_and_show',
25     'mixins/with_mail_tagging',
26     'mixins/with_mail_sandbox',
27     'page/events',
28     'views/i18n'
29   ],
30
31   function (defineComponent, templates, mailActions, viewHelpers, withHideAndShow, withMailTagging, withMailSandbox, events, i18n) {
32   'use strict';
33
34     return defineComponent(mailView, mailActions, withHideAndShow, withMailTagging, withMailSandbox);
35
36     function mailView() {
37       this.defaultAttrs({
38         tags: '.mail-read-view__header-tags-tag',
39         newTagInput: '#new-tag-input',
40         newTagButton: '#new-tag-button',
41         trashButton: '#trash-button',
42         archiveButton: '#archive-button',
43         closeMailButton: '.close-mail-button'
44       });
45
46       this.displayMail = function (event, data) {
47         this.attr.mail = data.mail;
48
49         var signed, encrypted, attachments;
50
51         data.mail.security_casing = data.mail.security_casing || {};
52         signed = this.checkSigned(data.mail);
53         encrypted = this.checkEncrypted(data.mail);
54         attachments = data.mail.attachments.map(function (attachment) {
55             attachment.received = true;
56             return attachment;
57         });
58
59         if(data.mail.mailbox === 'sent') {
60           encrypted = undefined;
61           signed = undefined;
62         }
63
64         this.$node.html(templates.mails.fullView({
65           header: data.mail.header,
66           body: [],
67           statuses: viewHelpers.formatStatusClasses(data.mail.status),
68           ident: data.mail.ident,
69           tags: data.mail.tags,
70           encryptionStatus: encrypted,
71           signatureStatus: signed,
72           attachments: attachments
73         }));
74
75         this.showMailOnSandbox(this.attr.mail);
76
77         this.attachTagCompletion(this.attr.mail);
78
79         this.select('tags').on('click', function (event) {
80           this.removeTag($(event.target).text());
81         }.bind(this));
82
83         this.addTagLoseFocus();
84         this.on(this.select('newTagButton'), 'click', this.showNewTagInput);
85         this.on(this.select('newTagInput'), 'keydown', this.handleKeyDown);
86         this.on(this.select('newTagInput'), 'blur', this.addTagLoseFocus);
87         this.on(this.select('trashButton'), 'click', this.moveToTrash);
88         this.on(this.select('closeMailButton'), 'click', this.openNoMessageSelectedPane);
89
90         mailActions.attachTo('#mail-actions', data);
91         this.resetScroll();
92       };
93
94       this.resetScroll = function(){
95         $('#right-pane').scrollTop(0);
96       };
97
98       this.checkEncrypted = function(mail) {
99         var ENCRYPTED_FLAG = {
100           cssClass: 'security-status__label--encrypted',
101           label: 'encrypted',
102           tooltipText: 'encrypted-label-tooltip'
103         };
104
105         var ENCRYPTED_WITH_ERROR_FLAG = {
106           cssClass: 'security-status__label--encrypted--with-error',
107           label: 'encryption-error',
108           tooltipText: 'encryption-error-label-tooltip'
109         };
110
111         var NOT_ENCRYPTED_FLAG = {
112           cssClass: 'security-status__label--not-encrypted',
113           label: 'not-encrypted',
114           tooltipText: 'not-encrypted-label-tooltip'
115         };
116
117         if(_.isEmpty(mail.security_casing.locks)) {
118           return NOT_ENCRYPTED_FLAG;
119         }
120
121         if(this.hasAnyEncryptionInfo(mail)) {
122           return ENCRYPTED_FLAG;
123         }
124
125         return ENCRYPTED_WITH_ERROR_FLAG;
126       };
127
128       this.hasAnyEncryptionInfo = function(mail) {
129         return _.any(mail.security_casing.locks, function (lock) {
130           return lock.state === 'valid';
131         });
132       };
133
134       this.checkSigned = function(mail) {
135
136         var SIGNED_FLAG = {
137           cssClass: 'security-status__label--signed',
138           label: 'signed',
139           tooltipText: 'signed-label-tooltip'
140         };
141
142         var SIGNED_REVOKED_FLAG = {
143           cssClass: 'security-status__label--signed--revoked',
144           label: 'signature-revoked',
145           tooltipText: 'not-signed-label-tooltip'
146         };
147
148         var SIGNED_EXPIRED_FLAG = {
149           cssClass: 'security-status__label--signed--expired',
150           label: 'signature-expired',
151           tooltipText: 'not-signed-label-tooltip'
152         };
153
154         var SIGNED_NOT_TRUSTED_FLAG = {
155           cssClass: 'security-status__label--signed--not-trusted',
156           label: 'signature-not-trusted',
157           tooltipText: 'not-signed-label-tooltip'
158         };
159
160         var NOT_SIGNED_FLAG = {
161           cssClass: 'security-status__label--not-signed',
162           label: 'not-signed',
163           tooltipText: 'not-signed-label-tooltip'
164         };
165
166         var hasNoSignatureInformation = _.any(mail.security_casing.imprints, function (imprint) {
167           return imprint.state === 'no_signature_information';
168         });
169
170         if(_.isEmpty(mail.security_casing.imprints) || hasNoSignatureInformation) {
171           return NOT_SIGNED_FLAG;
172         }
173
174         if(_.any(mail.security_casing.imprints, function(imprint) { return imprint.state === 'from_revoked'; })) {
175           return SIGNED_REVOKED_FLAG;
176         }
177
178         if(_.any(mail.security_casing.imprints, function(imprint) { return imprint.state === 'from_expired'; })) {
179           return SIGNED_EXPIRED_FLAG;
180         }
181
182         if(this.isNotTrusted(mail)) {
183           return SIGNED_NOT_TRUSTED_FLAG;
184         }
185
186         return SIGNED_FLAG;
187       };
188
189       this.isNotTrusted = function(mail){
190         return _.any(mail.security_casing.imprints, function(imprint) {
191           if(_.isNull(imprint.seal)){
192             return true;
193           }
194           var currentTrust = _.isUndefined(imprint.seal.trust) ? imprint.seal.validity : imprint.seal.trust;
195           return currentTrust === 'no_trust';
196         });
197       };
198
199       this.openNoMessageSelectedPane = function(ev, data) {
200         this.trigger(document, events.dispatchers.rightPane.openNoMessageSelected);
201       };
202
203       this.handleKeyDown = function(event) {
204         var ENTER_KEY = 13;
205         var ESC_KEY = 27;
206
207         if (event.which === ENTER_KEY){
208           event.preventDefault();
209           if (this.select('newTagInput').val().trim() !== '') {
210             this.createNewTag();
211           }
212         } else if (event.which === ESC_KEY) {
213           event.preventDefault();
214           this.addTagLoseFocus();
215         }
216       };
217
218       this.addTagLoseFocus = function () {
219         this.select('newTagInput').hide();
220         this.select('newTagInput').typeahead('val', '');
221       };
222
223       this.showNewTagInput = function () {
224         this.select('newTagInput').show();
225         this.select('newTagInput').focus();
226       };
227
228       this.removeTag = function (tag) {
229         tag = tag.toString();
230         var filteredTags = _.without(this.attr.mail.tags, tag);
231         this.updateTags(this.attr.mail, filteredTags);
232         this.trigger(document, events.dispatchers.tags.refreshTagList);
233       };
234
235       this.moveToTrash = function(){
236         this.trigger(document, events.ui.mail.delete, { mail: this.attr.mail });
237       };
238
239       this.tagsUpdated = function(ev, data) {
240         data = data || {};
241         this.attr.mail.tags = data.tags;
242         this.displayMail({}, { mail: this.attr.mail });
243       };
244
245       this.mailDeleted = function(ev, data) {
246         if (_.contains(_.pluck(data.mails, 'ident'),  this.attr.mail.ident)) {
247           this.openNoMessageSelectedPane();
248         }
249       };
250
251       this.fetchMailToShow = function () {
252         this.trigger(events.mail.want, {mail: this.attr.ident, caller: this});
253       };
254
255       this.highlightMailContent = function (event, data) {
256         // we can't directly manipulate the iFrame to highlight the content
257         // so we need to take an indirection where we directly manipulate
258         // the mail content to accomodate the highlighting
259         this.trigger(document, events.mail.highlightMailContent, data);
260       };
261
262       this.after('initialize', function () {
263         this.on(this, events.mail.notFound, this.openNoMessageSelectedPane);
264         this.on(this, events.mail.here, this.highlightMailContent);
265         this.on(document, events.mail.display, this.displayMail);
266         this.on(document, events.dispatchers.rightPane.clear, this.teardown);
267         this.on(document, events.mail.tags.updated, this.tagsUpdated);
268         this.on(document, events.mail.deleted, this.mailDeleted);
269         this.fetchMailToShow();
270       });
271     }
272   }
273 );