Repository navigation
Expand file tree
/
Copy pathattachment.js
More file actions
executable file
·263 lines (229 loc) · 9.34 KB
/
<
8000
span class="tooltip__XwO6MO prc-TooltipV2-Tooltip-tLeuB" data-direction="s" data-component="Tooltip" aria-label="Copy path" aria-hidden="true" id="_R_7lcpala9lik5_">Copy pathattachment.js
File metadata and controls
executable file
·263 lines (229 loc) · 9.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
/** Creates an Attachment object
* @param {String} type of attachment (image, video, etc).
* @param {String} uri Location or datauri of image to show.
* @param {Number} size Size of attachment in bytes.
* @return {Attachment} new attachment object.
*
* The render method creates an iframe to represent the
* attachment in the message composition area. An iframe
* is used because Gecko will still try to put the
* cursor into elements with [contentEditable=false].
* Instead of a bunch of JavaScript to manage where the
* caret is and what to delete on backspace, the
* contentEditable region treats the iframe as a simple
* block. Win.
*
* It uses the main sms.css stylesheet so that styles
* can be defined in that.
*/
(function(exports) {
'use strict';
// thumbnails should be 80*80px (plus border) but can be extended up to 120px,
// either horizontally or vertically
var MIN_THUMBNAIL_WIDTH_HEIGHT = 80; // min = 80px
var MAX_THUMBNAIL_WIDTH_HEIGHT = 120; // max = 120px
// do not create thumbnails for too big attachments
// (see bug 805114 for a similar issue in Gallery)
var MAX_THUMBNAIL_GENERATION_SIZE = 1.5 * 1024 * 1024; // 1.5MB
function Attachment(blob, options) {
options = options || {};
this.blob = blob;
this.name = blob.name || options.name ||
navigator.mozL10n.get('unnamed-attachment');
this.isDraft = !!options.isDraft;
}
Attachment.prototype = {
get size() {
return this.blob.size;
},
get sizeForL10n() { // blob size with unit (KB or MB)
var sizeKB = this.blob.size / 1024;
var sizeMB = sizeKB / 1024;
if (sizeKB < 1000) {
return {
l10nId: 'attachmentSize',
l10nArgs: { n: sizeKB.toFixed(1) }
};
} else {
return {
l10nId: 'attachmentSizeMB',
l10nArgs: { n: sizeMB.toFixed(1) }
};
}
},
get type() {
return Utils.typeFromMimeType(this.blob.type);
},
getThumbnail: function(callback) {
if (typeof(callback) !== 'function') {
return;
}
// The thumbnail format matches the blob format.
var type = this.blob.type;
// The container size is set to 80*80px by default (plus border);
// as soon as the image width and height are known, the container can be
// extended up to 120px, either horizontally or vertically.
var img = new Image();
img.src = window.URL.createObjectURL(this.blob);
img. onBlobLoaded() {
window.URL.revokeObjectURL(img.src);
// compute thumbnail size
var min = MIN_THUMBNAIL_WIDTH_HEIGHT;
var max = MAX_THUMBNAIL_WIDTH_HEIGHT;
var width, height;
if (img.width < img.height) {
width = min;
height = Math.min(img.height / img.width * min, max);
} else {
width = Math.min(img.width / img.height * min, max);
height = min;
}
// turn this thumbnail into a dataURL
var canvas = document.createElement('canvas');
var ratio = Math.max(img.width / width, img.height / height);
canvas.width = Math.round(img.width / ratio);
canvas.height = Math.round(img.height / ratio);
var context = canvas.getContext('2d');
context.drawImage(img, 0, 0, width, height);
var data = canvas.toDataURL(type);
callback({
width: width,
height: height,
data: data
});
};
img. onBlobError() {
callback({
width: MIN_THUMBNAIL_WIDTH_HEIGHT,
height: MIN_THUMBNAIL_WIDTH_HEIGHT,
error: true
});
};
},
getAttachmentSrc: function(thumbnail, tmplID) {
// interpolate the #attachment-[no]preview-tmpl template
thumbnail = thumbnail || {};
var sizeL10n = this.sizeForL10n;
return Template(tmplID).interpolate({
type: this.type,
errorClass: thumbnail.error ? 'corrupted' : '',
imgData: thumbnail.data,
fileName: this.name.slice(this.name.lastIndexOf('/') + 1),
sizeL10nId: sizeL10n.l10nId,
sizeL10nArgs: JSON.stringify(sizeL10n.l10nArgs)
});
},
render: function(readyCallback) {
/**
* A <div> container suits most of the cases where we want to display an
* MMS attachment (= icon + file name + file size). However, drafts are a
* specific case because they are inside an editable area.
*
* A <div contenteditable="false"> container would be fine for drafts but
* Gecko does not support it at the moment, see bug 685445:
* https://bugzilla.mozilla.org/show_bug.cgi?id=685445
*
* By using an <iframe> for drafts, we make sure that the attachment block
* is deletable but not editable; outside of the Compose area, a <div>
* container is still fine -- and it's *way* faster.
*/
var container = document.createElement(this.isDraft ? 'iframe' : 'div');
// display the attachment in the iframe/div container
var setAttachmentContainerSrc = (function(thumbnail) {
thumbnail = thumbnail || {
width: MIN_THUMBNAIL_WIDTH_HEIGHT,
height: MIN_THUMBNAIL_WIDTH_HEIGHT,
data: '',
error: false
};
var hasPreview = (thumbnail.data && !thumbnail.error);
if (hasPreview) {
var borderWidth = 1; // px
container.style.width = (thumbnail.width + 2 * borderWidth) + 'px';
container.style.height = (thumbnail.height + 2 * borderWidth) + 'px';
}
var previewClass = hasPreview ? 'preview' : 'nopreview';
var tmplID = 'attachment-' + previewClass + '-tmpl';
container.classList.add(previewClass);
if (this.isDraft) { // <iframe>
// The attachment's iFrame requires access to the parent document's
// context so that URIs for Blobs created in the parent may resolve as
// expected.
container.setAttribute('sandbox', 'allow-same-origin');
var tmplSrc = Template('attachment-draft-tmpl').interpolate({
previewClass: previewClass,
baseURL: location.protocol + '//' + location.host + '/',
attachmentHTML: this.getAttachmentSrc(thumbnail, tmplID)
}, { safe: ['attachmentHTML'] });
// append the source when it's appended to the dom and loaded
container.addEventListener('load', function onload() {
this.removeEventListener('load', onload);
this.contentDocument.documentElement.innerHTML = tmplSrc;
});
// Attach click listeners and fire the callback when rendering is
// complete: we can't bind `readyCallback' to the `load' event
// listener because it would break our unit tests.
container.addEventListener('load', iframeLoad);
container.src = 'about:blank';
} else { // <div>
container.innerHTML = this.getAttachmentSrc(thumbnail, tmplID);
}
if (readyCallback) {
readyCallback();
}
}).bind(this);
container.className = 'attachment-container';
container.dataset.attachmentType = this.type;
// We special case audio to display an image of an audio attachment video
// currently falls through this path too, we should revisit this with
// Bug 869244 - [MMS] 'Thumbnail'/'Poster' in video attachment is needed.
if (this.type === 'img' && this.size < MAX_THUMBNAIL_GENERATION_SIZE) {
// TODO: store this thumbnail data (indexedDB)
// Bug 876467 - [MMS] generate, store, and reuse image thumbnails
this.getThumbnail(setAttachmentContainerSrc);
} else {
// Display the default attachment placeholder for the current type: img,
// audio, video, other. We have to be asynchronous to keep the
// behaviour consistent with the thumbnail case.
setTimeout(setAttachmentContainerSrc);
}
// Remember: the <iframe> content is created asynchrounously.
return container;
},
view: function(options) {
// Make sure media is openable and savable even if:
// - Blob mimetype is unsupported but file extension is valid.
// - File extenion is missing or invalid but mimetype is supported.
var mimetype =
MimeMapper.guessTypeFromFileProperties(this.name, this.blob.type);
var filename = MimeMapper.ensureFilenameMatchesType(this.name, mimetype);
var activity = new MozActivity({
name: 'open',
data: {
type: mimetype,
filename: filename,
blob: this.blob,
allowSave: options && options.allowSave
}
});
activity. {
var _ = navigator.mozL10n.get;
console.error('error with open activity', this.error.name);
if (this.error.name === 'ActivityCanceled') {
return;
}
alert(_('attachmentOpenError'));
};
}
};
function iframeLoad(event) {
// Bubble click events from inside the iframe.
var iframe = event.target;
var clickOnFrame = iframe.click.bind(iframe);
iframe.removeEventListener('load', iframeLoad);
navigator.mozL10n.translate(iframe.contentDocument.body);
iframe.contentDocument.addEventListener('click', clickOnFrame);
iframe.contentDocument.addEventListener('contextmenu', clickOnFrame);
}
exports.Attachment = Attachment;
}(this));