Fix up unsupported attachment rendering

Rather than simply displaying an inactionable error, render a link that
allows the user to save the unsupported attachment.

// FREEBIE
This commit is contained in:
lilia 2015-12-21 17:36:33 -08:00
parent 239ec8e318
commit 881aa1685d
4 changed files with 46 additions and 28 deletions

View file

@ -4,8 +4,24 @@
(function () {
'use strict';
Whisper.FileTypeToast = Whisper.ToastView.extend({
template: $('#attachment-type-modal').html()
var FileView = Backbone.View.extend({
tagName: 'a',
initialize: function(dataUrl) {
this.dataUrl = dataUrl;
this.$el.text("Unsupported attachment type. Click to save.");
},
events: {
'click': 'open'
},
open: function (e) {
e.preventDefault();
window.open(this.dataUrl, '_blank');
},
render: function() {
this.$el.attr('href', this.dataUrl);
this.trigger('update');
return this;
}
});
var ImageView = Backbone.View.extend({
@ -58,33 +74,18 @@
className: 'attachment',
render: function() {
var View;
var isUnsupportedType = false;
switch(this.model.contentType.split('/')[0]) {
case 'image': View = ImageView; break;
case 'audio': View = AudioView; break;
case 'video': View = VideoView; break;
default:
isUnsupportedType = true;
default : View = FileView; break;
}
if (isUnsupportedType) {
var toast = new Whisper.FileTypeToast({
model: {type: this.model.contentType.split('/')[0]}
});
toast.$el.insertAfter(this.$el);
toast.render();
return toast;
} else {
var blob = new Blob([this.model.data], {type: this.model.contentType});
var view = new View(window.URL.createObjectURL(blob), this.model.contentType);
view.$el.appendTo(this.$el);
view.render();
view.on('update', this.trigger.bind(this, 'update'));
return this;
}
},
deleteView: function(e) {
if (e) { e.stopPropagation(); }
var blob = new Blob([this.model.data], {type: this.model.contentType});
var view = new View(window.URL.createObjectURL(blob), this.model.contentType);
view.$el.appendTo(this.$el);
view.on('update', this.trigger.bind(this, 'update'));
view.render();
return this;
}
});

View file

@ -100,12 +100,13 @@
},
loadAttachments: function() {
this.model.get('attachments').forEach(function(attachment) {
var view = new Whisper.AttachmentView({ model: attachment }).render();
var view = new Whisper.AttachmentView({ model: attachment });
this.listenTo(view, 'update', function() {
this.trigger('beforeChangeHeight');
this.$('.attachments').append(view.el);
this.trigger('afterChangeHeight');
});
view.render();
}.bind(this));
}
});