2015-09-07 21:53:43 +00:00
|
|
|
/*
|
|
|
|
* vim: ts=4:sw=4:expandtab
|
2015-01-11 11:27:22 +00:00
|
|
|
*/
|
|
|
|
(function () {
|
|
|
|
'use strict';
|
|
|
|
|
2017-04-18 22:20:45 +00:00
|
|
|
var FileView = Whisper.View.extend({
|
|
|
|
tagName: 'div',
|
|
|
|
className: 'fileView',
|
|
|
|
templateName: 'file-view',
|
|
|
|
render_attributes: function() {
|
2017-05-09 17:10:50 +00:00
|
|
|
return this.model;
|
2015-12-22 01:36:33 +00:00
|
|
|
}
|
2015-12-05 06:39:15 +00:00
|
|
|
});
|
|
|
|
|
2015-03-02 19:49:23 +00:00
|
|
|
var ImageView = Backbone.View.extend({
|
|
|
|
tagName: 'img',
|
2015-10-21 17:52:20 +00:00
|
|
|
initialize: function(dataUrl) {
|
|
|
|
this.dataUrl = dataUrl;
|
|
|
|
},
|
2015-03-03 21:23:55 +00:00
|
|
|
events: {
|
2015-10-20 21:18:46 +00:00
|
|
|
'load': 'update',
|
2015-03-03 21:23:55 +00:00
|
|
|
},
|
|
|
|
update: function() {
|
2015-11-12 18:35:29 +00:00
|
|
|
this.trigger('update');
|
2015-03-03 21:23:55 +00:00
|
|
|
},
|
2015-10-21 17:52:20 +00:00
|
|
|
render: function() {
|
2015-12-05 06:39:15 +00:00
|
|
|
this.$el.attr('src', this.dataUrl);
|
|
|
|
return this;
|
2015-03-02 19:49:23 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2015-03-03 21:05:39 +00:00
|
|
|
var MediaView = Backbone.View.extend({
|
2015-10-21 17:52:20 +00:00
|
|
|
initialize: function(dataUrl, contentType) {
|
|
|
|
this.dataUrl = dataUrl;
|
|
|
|
this.contentType = contentType;
|
2015-03-02 19:49:23 +00:00
|
|
|
this.$el.attr('controls', '');
|
2015-03-03 21:23:55 +00:00
|
|
|
},
|
|
|
|
events: {
|
2015-11-12 18:35:29 +00:00
|
|
|
'canplay': 'canplay'
|
2015-03-03 21:23:55 +00:00
|
|
|
},
|
2015-11-12 18:35:29 +00:00
|
|
|
canplay: function() {
|
|
|
|
this.trigger('update');
|
2015-03-02 19:49:23 +00:00
|
|
|
},
|
2015-10-21 17:52:20 +00:00
|
|
|
render: function() {
|
2015-03-02 19:49:23 +00:00
|
|
|
var $el = $('<source>');
|
2015-10-21 17:52:20 +00:00
|
|
|
$el.attr('src', this.dataUrl);
|
|
|
|
$el.attr('type', this.contentType);
|
2015-03-02 19:49:23 +00:00
|
|
|
this.$el.append($el);
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2015-03-03 21:05:39 +00:00
|
|
|
var AudioView = MediaView.extend({ tagName: 'audio' });
|
|
|
|
var VideoView = MediaView.extend({ tagName: 'video' });
|
|
|
|
|
2017-04-18 22:20:45 +00:00
|
|
|
// Blacklist common file types known to be unsupported in Chrome
|
|
|
|
var UnsupportedFileTypes = [
|
|
|
|
'audio/aiff',
|
|
|
|
'video/quicktime'
|
|
|
|
];
|
|
|
|
|
2015-01-11 11:27:22 +00:00
|
|
|
Whisper.AttachmentView = Backbone.View.extend({
|
2015-03-03 02:37:20 +00:00
|
|
|
tagName: 'span',
|
2017-05-11 23:45:26 +00:00
|
|
|
className: function() {
|
|
|
|
if (this.isImage()) {
|
|
|
|
return 'attachment';
|
|
|
|
} else {
|
|
|
|
return 'attachment bubbled';
|
|
|
|
}
|
|
|
|
},
|
2017-01-12 21:51:38 +00:00
|
|
|
initialize: function(options) {
|
2016-06-20 18:37:26 +00:00
|
|
|
this.blob = new Blob([this.model.data], {type: this.model.contentType});
|
2017-07-11 22:24:13 +00:00
|
|
|
if (!this.model.size) {
|
|
|
|
this.model.size = this.model.data.byteLength;
|
|
|
|
}
|
2017-01-12 21:51:38 +00:00
|
|
|
if (options.timestamp) {
|
|
|
|
this.timestamp = options.timestamp;
|
|
|
|
}
|
2016-06-20 18:37:26 +00:00
|
|
|
},
|
|
|
|
events: {
|
|
|
|
'click': 'onclick'
|
|
|
|
},
|
2017-07-26 21:55:59 +00:00
|
|
|
unload: function() {
|
|
|
|
this.blob = null;
|
|
|
|
|
|
|
|
if (this.lightBoxView) {
|
|
|
|
this.lightBoxView.remove();
|
|
|
|
}
|
|
|
|
if (this.fileView) {
|
|
|
|
this.fileView.remove();
|
|
|
|
}
|
|
|
|
if (this.view) {
|
|
|
|
this.view.remove();
|
|
|
|
}
|
|
|
|
|
|
|
|
this.remove();
|
|
|
|
},
|
2017-04-18 22:29:14 +00:00
|
|
|
getFileType: function() {
|
|
|
|
switch(this.model.contentType) {
|
|
|
|
case 'video/quicktime': return 'mov';
|
|
|
|
default: return this.model.contentType.split('/')[1];
|
|
|
|
}
|
|
|
|
},
|
2016-06-20 18:37:26 +00:00
|
|
|
onclick: function(e) {
|
2017-05-11 23:45:26 +00:00
|
|
|
if (this.isImage()) {
|
2017-07-26 21:55:59 +00:00
|
|
|
this.lightBoxView = new Whisper.LightboxView({ model: this });
|
|
|
|
this.lightBoxView.render();
|
|
|
|
this.lightBoxView.$el.appendTo(this.el);
|
|
|
|
this.lightBoxView.$el.trigger('show');
|
2016-06-30 07:54:09 +00:00
|
|
|
|
2017-05-11 23:45:26 +00:00
|
|
|
} else {
|
|
|
|
this.saveFile();
|
|
|
|
}
|
|
|
|
},
|
|
|
|
isVoiceMessage: function() {
|
|
|
|
if (this.model.flags & textsecure.protobuf.AttachmentPointer.Flags.VOICE_MESSAGE) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Support for android legacy voice messages
|
|
|
|
if (this.isAudio() && this.model.fileName === null) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
isAudio: function() {
|
|
|
|
return this.model.contentType.startsWith('audio/');
|
|
|
|
},
|
|
|
|
isVideo: function() {
|
|
|
|
return this.model.contentType.startsWith('video/');
|
|
|
|
},
|
|
|
|
isImage: function() {
|
|
|
|
return this.model.contentType.startsWith('image/');
|
|
|
|
},
|
|
|
|
mediaType: function() {
|
|
|
|
if (this.isVoiceMessage()) {
|
|
|
|
return 'voice';
|
|
|
|
} else if (this.isAudio()) {
|
|
|
|
return 'audio';
|
|
|
|
} else if (this.isVideo()) {
|
|
|
|
return 'video';
|
|
|
|
} else if (this.isImage()) {
|
|
|
|
return 'image';
|
|
|
|
}
|
|
|
|
},
|
|
|
|
displayName: function() {
|
|
|
|
if (this.isVoiceMessage()) {
|
|
|
|
return i18n('voiceMessage');
|
|
|
|
}
|
2017-05-19 17:25:30 +00:00
|
|
|
if (this.model.fileName) {
|
|
|
|
return this.model.fileName;
|
|
|
|
}
|
2017-05-11 23:45:26 +00:00
|
|
|
if (this.isAudio() || this.isVideo()) {
|
2017-05-31 16:56:21 +00:00
|
|
|
return i18n('mediaMessage');
|
2017-05-11 23:45:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return i18n('unnamedFile');
|
2016-06-30 07:54:09 +00:00
|
|
|
},
|
2017-02-06 02:59:20 +00:00
|
|
|
suggestedName: function() {
|
2017-04-18 18:26:05 +00:00
|
|
|
if (this.model.fileName) {
|
|
|
|
return this.model.fileName;
|
|
|
|
}
|
|
|
|
|
2017-02-06 02:59:20 +00:00
|
|
|
var suggestion = 'signal';
|
2017-01-12 21:51:38 +00:00
|
|
|
if (this.timestamp) {
|
2017-02-06 02:59:20 +00:00
|
|
|
suggestion += moment(this.timestamp).format('-YYYY-MM-DD-HHmmss');
|
2017-01-12 21:51:38 +00:00
|
|
|
}
|
2017-04-18 22:29:14 +00:00
|
|
|
var fileType = this.getFileType();
|
|
|
|
if (fileType) {
|
|
|
|
suggestion += '.' + fileType;
|
2016-06-30 07:54:09 +00:00
|
|
|
}
|
2017-02-06 02:59:20 +00:00
|
|
|
return suggestion;
|
|
|
|
},
|
|
|
|
saveFile: function() {
|
|
|
|
var blob = this.blob;
|
2016-06-30 07:54:09 +00:00
|
|
|
var w = extension.windows.getViews()[0];
|
|
|
|
if (w && w.chrome && w.chrome.fileSystem) {
|
|
|
|
w.chrome.fileSystem.chooseEntry({
|
2017-02-06 02:59:20 +00:00
|
|
|
type: 'saveFile', suggestedName: this.suggestedName()
|
2016-06-30 07:54:09 +00:00
|
|
|
}, function(entry) {
|
|
|
|
if (!entry) {
|
|
|
|
return;
|
2016-06-20 18:37:26 +00:00
|
|
|
}
|
2016-06-30 07:54:09 +00:00
|
|
|
entry.createWriter(function(fileWriter) {
|
|
|
|
fileWriter.write(blob);
|
|
|
|
});
|
2016-06-20 18:37:26 +00:00
|
|
|
});
|
2016-06-30 07:54:09 +00:00
|
|
|
} else {
|
|
|
|
console.log('Failed to get window');
|
2016-06-20 18:37:26 +00:00
|
|
|
}
|
|
|
|
},
|
2015-01-11 11:27:22 +00:00
|
|
|
render: function() {
|
2017-05-11 23:45:26 +00:00
|
|
|
if (!this.isImage()) {
|
|
|
|
this.renderFileView();
|
|
|
|
}
|
2015-10-21 17:52:20 +00:00
|
|
|
var View;
|
2017-05-11 23:45:26 +00:00
|
|
|
if (this.isImage()) {
|
|
|
|
View = ImageView;
|
|
|
|
} else if (this.isAudio()) {
|
|
|
|
View = AudioView;
|
|
|
|
} else if (this.isVideo()) {
|
|
|
|
View = VideoView;
|
2015-03-02 19:49:23 +00:00
|
|
|
}
|
2017-04-18 22:20:45 +00:00
|
|
|
|
|
|
|
if (!View || _.contains(UnsupportedFileTypes, this.model.contentType)) {
|
2017-05-11 23:45:26 +00:00
|
|
|
this.update();
|
|
|
|
return this;
|
2017-04-18 22:20:45 +00:00
|
|
|
}
|
|
|
|
|
2016-06-20 18:37:26 +00:00
|
|
|
if (!this.objectUrl) {
|
|
|
|
this.objectUrl = window.URL.createObjectURL(this.blob);
|
|
|
|
}
|
2017-04-18 22:20:45 +00:00
|
|
|
this.view = new View(this.objectUrl, this.model.contentType);
|
|
|
|
this.view.$el.appendTo(this.$el);
|
|
|
|
this.listenTo(this.view, 'update', this.update);
|
|
|
|
this.view.render();
|
2017-04-20 01:16:54 +00:00
|
|
|
if (View !== ImageView) {
|
|
|
|
this.timeout = setTimeout(this.onTimeout.bind(this), 5000);
|
|
|
|
}
|
2015-12-22 01:36:33 +00:00
|
|
|
return this;
|
2017-04-18 22:20:45 +00:00
|
|
|
},
|
|
|
|
onTimeout: function() {
|
|
|
|
// Image or media element failed to load. Fall back to FileView.
|
|
|
|
this.stopListening(this.view);
|
2017-05-11 23:45:26 +00:00
|
|
|
this.update();
|
2017-04-18 22:20:45 +00:00
|
|
|
},
|
|
|
|
renderFileView: function() {
|
2017-06-16 17:46:55 +00:00
|
|
|
this.fileView = new FileView({
|
2017-05-09 17:10:50 +00:00
|
|
|
model: {
|
2017-05-11 23:45:26 +00:00
|
|
|
mediaType: this.mediaType(),
|
|
|
|
fileName: this.displayName(),
|
2017-05-09 17:10:50 +00:00
|
|
|
fileSize: window.filesize(this.model.size),
|
2017-05-11 23:45:26 +00:00
|
|
|
altText: i18n('clickToSave')
|
2017-05-09 17:10:50 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2017-06-16 17:46:55 +00:00
|
|
|
this.fileView.$el.appendTo(this.$el.empty());
|
|
|
|
this.fileView.render();
|
2017-04-18 22:20:45 +00:00
|
|
|
return this;
|
|
|
|
},
|
|
|
|
update: function() {
|
|
|
|
clearTimeout(this.timeout);
|
|
|
|
this.trigger('update');
|
2015-01-11 11:27:22 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2016-06-20 18:37:26 +00:00
|
|
|
Whisper.LightboxView = Whisper.View.extend({
|
|
|
|
templateName: 'lightbox',
|
|
|
|
className: 'modal lightbox',
|
2016-06-30 07:55:44 +00:00
|
|
|
initialize: function() {
|
2016-06-30 23:56:28 +00:00
|
|
|
this.window = extension.windows.getViews()[0];
|
2016-06-30 07:55:44 +00:00
|
|
|
this.$document = $(this.window.document);
|
|
|
|
this.listener = this.onkeyup.bind(this);
|
|
|
|
this.$document.on('keyup', this.listener);
|
|
|
|
},
|
2016-06-20 18:37:26 +00:00
|
|
|
events: {
|
|
|
|
'click .save': 'save',
|
|
|
|
'click .close': 'remove',
|
|
|
|
'click': 'onclick'
|
|
|
|
},
|
|
|
|
save: function(e) {
|
2016-06-30 07:54:09 +00:00
|
|
|
this.model.saveFile();
|
2016-06-20 18:37:26 +00:00
|
|
|
},
|
|
|
|
onclick: function(e) {
|
|
|
|
var $el = this.$(e.target);
|
|
|
|
if (!$el.hasClass('image') && !$el.closest('.controls').length ) {
|
|
|
|
e.preventDefault();
|
|
|
|
this.remove();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
},
|
2016-06-30 07:55:44 +00:00
|
|
|
onkeyup: function(e) {
|
|
|
|
if (e.keyCode === 27) {
|
|
|
|
this.remove();
|
|
|
|
this.$document.off('keyup', this.listener);
|
|
|
|
}
|
|
|
|
},
|
2016-06-20 18:37:26 +00:00
|
|
|
render_attributes: function() {
|
2016-06-30 07:54:09 +00:00
|
|
|
return { url: this.model.objectUrl };
|
2016-06-20 18:37:26 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2015-01-11 11:27:22 +00:00
|
|
|
})();
|