|
1
|
|
|
define(function(require) { |
|
2
|
|
|
'use strict'; |
|
3
|
|
|
|
|
4
|
|
|
var EmailBodyView; |
|
5
|
|
|
var $ = require('jquery'); |
|
6
|
|
|
var _ = require('underscore'); |
|
7
|
|
|
var BaseView = require('oroui/js/app/views/base/view'); |
|
8
|
|
|
var errorHandler = require('oroui/js/error'); |
|
9
|
|
|
|
|
10
|
|
|
EmailBodyView = BaseView.extend({ |
|
11
|
|
|
autoRender: true, |
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* @type {string} |
|
15
|
|
|
*/ |
|
16
|
|
|
bodyContent: null, |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* @type {Array.<string>} |
|
20
|
|
|
*/ |
|
21
|
|
|
styles: null, |
|
22
|
|
|
|
|
23
|
|
|
events: { |
|
24
|
|
|
'click .email-extra-body-toggle': 'onEmailExtraBodyToggle' |
|
25
|
|
|
}, |
|
26
|
|
|
|
|
27
|
|
|
listen: { |
|
28
|
|
|
'layout:reposition mediator': '_updateHeight' |
|
29
|
|
|
}, |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* @inheritDoc |
|
33
|
|
|
*/ |
|
34
|
|
|
constructor: function EmailBodyView() { |
|
35
|
|
|
EmailBodyView.__super__.constructor.apply(this, arguments); |
|
36
|
|
|
}, |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* @inheritDoc |
|
40
|
|
|
*/ |
|
41
|
|
|
initialize: function(options) { |
|
42
|
|
|
_.extend(this, _.pick(options, ['bodyContent', 'styles'])); |
|
43
|
|
|
this.$frame = this.$el; |
|
44
|
|
|
this.$frame.on('emailShown', _.bind(this._updateHeight, this)); |
|
45
|
|
|
this.$frame.on('load', this.reattachBody.bind(this)); |
|
46
|
|
|
this.setElement(this.$el.contents().find('html')); |
|
47
|
|
|
EmailBodyView.__super__.initialize.apply(this, arguments); |
|
48
|
|
|
}, |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* @inheritDoc |
|
52
|
|
|
*/ |
|
53
|
|
|
dispose: function() { |
|
54
|
|
|
if (this.disposed) { |
|
55
|
|
|
return; |
|
56
|
|
|
} |
|
57
|
|
|
if (this.$frame) { |
|
58
|
|
|
this.$frame.off(); |
|
59
|
|
|
} |
|
60
|
|
|
delete this.$frame; |
|
61
|
|
|
EmailBodyView.__super__.dispose.call(this); |
|
62
|
|
|
}, |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* @inheritDoc |
|
66
|
|
|
*/ |
|
67
|
|
|
render: function() { |
|
68
|
|
|
var $content; |
|
69
|
|
|
var content = this.bodyContent; |
|
70
|
|
|
try { |
|
71
|
|
|
/** |
|
72
|
|
|
* Valid email could not contain a root node after HTML tags strip, |
|
73
|
|
|
* but it is still valid HTML fragment and it should be displayed like an HTML |
|
74
|
|
|
* |
|
75
|
|
|
* Example: |
|
76
|
|
|
* Original email: |
|
77
|
|
|
* <html> |
|
78
|
|
|
* <head> |
|
79
|
|
|
* <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> |
|
80
|
|
|
* </head> |
|
81
|
|
|
* <body> |
|
82
|
|
|
* Some Text <b>Some other text</b> |
|
83
|
|
|
* </body> |
|
84
|
|
|
* </html> |
|
85
|
|
|
* |
|
86
|
|
|
* Content after strip: |
|
87
|
|
|
* Some Text <b>Some other text</b> |
|
88
|
|
|
* |
|
89
|
|
|
* This content will not be correctly parsed by JQuery, but should be displayed as an HTML |
|
90
|
|
|
*/ |
|
91
|
|
|
var contentWithRootNode = '<div>' + content + '</div>'; |
|
92
|
|
|
$content = $(contentWithRootNode); |
|
93
|
|
|
} catch (e) { |
|
94
|
|
|
errorHandler.showErrorInConsole(e); |
|
95
|
|
|
// if content can not be processed as HTML, output it as plain text |
|
96
|
|
|
content = content.replace(/[&<>]/g, function(c) { |
|
97
|
|
|
return '&#' + c.charCodeAt(0) + ';'; |
|
98
|
|
|
}); |
|
99
|
|
|
$content = $('<div class="plain-text">' + content + '</div>'); |
|
100
|
|
|
} |
|
101
|
|
|
this.$('body').html($content); |
|
102
|
|
|
this._injectStyles(); |
|
103
|
|
|
this._markEmailExtraBody(); |
|
104
|
|
|
this._updateHeight(); |
|
105
|
|
|
return this; |
|
106
|
|
|
}, |
|
107
|
|
|
|
|
108
|
|
|
/** |
|
109
|
|
|
* Fixes issue when iframe get empty after DOM-manipulation |
|
110
|
|
|
*/ |
|
111
|
|
|
reattachBody: function() { |
|
112
|
|
|
this.undelegateEvents(); |
|
113
|
|
|
this.$frame.contents().find('html').replaceWith(this.$el); |
|
114
|
|
|
this.delegateEvents(); |
|
115
|
|
|
}, |
|
116
|
|
|
|
|
117
|
|
|
/** |
|
118
|
|
|
* Add application styles to the iframe document |
|
119
|
|
|
* |
|
120
|
|
|
* @protected |
|
121
|
|
|
*/ |
|
122
|
|
|
_injectStyles: function() { |
|
123
|
|
|
var $head; |
|
124
|
|
|
if (!this.styles) { |
|
125
|
|
|
return; |
|
126
|
|
|
} |
|
127
|
|
|
$head = this.$('head'); |
|
128
|
|
|
_.each(this.styles, function(src) { |
|
129
|
|
|
$('<link/>', {rel: 'stylesheet', href: src}).appendTo($head); |
|
130
|
|
|
}); |
|
131
|
|
|
}, |
|
132
|
|
|
|
|
133
|
|
|
/** |
|
134
|
|
|
* Updates height for iFrame element depending on the email content size |
|
135
|
|
|
* |
|
136
|
|
|
* @protected |
|
137
|
|
|
*/ |
|
138
|
|
|
_updateHeight: function() { |
|
139
|
|
|
var $frame = this.$frame; |
|
140
|
|
|
var $el = this.$el; |
|
141
|
|
|
_.delay(function() { |
|
142
|
|
|
$frame.height(0); |
|
143
|
|
|
$frame.height($el[0].scrollHeight); |
|
144
|
|
|
}, 50); |
|
145
|
|
|
}, |
|
146
|
|
|
|
|
147
|
|
|
/** |
|
148
|
|
|
* Marks email extra-body part and adds the toggler |
|
149
|
|
|
* |
|
150
|
|
|
* @protected |
|
151
|
|
|
*/ |
|
152
|
|
|
_markEmailExtraBody: function() { |
|
153
|
|
|
var $extraBodies = this.$('body>.quote, body>.gmail_extra') |
|
154
|
|
|
.not('.email-extra-body') |
|
155
|
|
|
.addClass('email-extra-body'); |
|
156
|
|
|
$('<div class="email-extra-body-toggle"></div>').insertBefore($extraBodies); |
|
157
|
|
|
}, |
|
158
|
|
|
|
|
159
|
|
|
/** |
|
160
|
|
|
* Handles click on email extra-body toggle button |
|
161
|
|
|
* @param {jQuery.Event} e |
|
162
|
|
|
*/ |
|
163
|
|
|
onEmailExtraBodyToggle: function(e) { |
|
164
|
|
|
this.$(e.currentTarget) |
|
165
|
|
|
.next() |
|
166
|
|
|
.toggleClass('in'); |
|
167
|
|
|
this._updateHeight(); |
|
168
|
|
|
} |
|
169
|
|
|
}); |
|
170
|
|
|
|
|
171
|
|
|
return EmailBodyView; |
|
172
|
|
|
}); |
|
173
|
|
|
|