Refactor attachment saving for DRYness

// FREEBIE
This commit is contained in:
lilia 2016-06-30 00:54:09 -07:00
parent a1345c52b4
commit a069939f65

View file

@ -72,18 +72,23 @@
'click': 'onclick' 'click': 'onclick'
}, },
onclick: function(e) { onclick: function(e) {
if (this.contentType === 'image') { switch (this.contentType) {
var view = new Whisper.LightboxView({ case 'audio':
model: { case 'video':
url : this.objectUrl, return;
blob : this.blob, case 'image':
fileType : this.fileType var view = new Whisper.LightboxView({ model: this });
}
});
view.render(); view.render();
view.$el.appendTo(this.el); view.$el.appendTo(this.el);
view.$el.trigger('show'); view.$el.trigger('show');
} else if (this.contentType !== 'audio' && this.contentType !== 'video') { break;
default:
this.saveFile();
}
},
saveFile: function() {
var blob = this.blob;
var suggestedName; var suggestedName;
if (this.fileType) { if (this.fileType) {
suggestedName = 'signal.' + this.fileType; suggestedName = 'signal.' + this.fileType;
@ -97,13 +102,12 @@
return; return;
} }
entry.createWriter(function(fileWriter) { entry.createWriter(function(fileWriter) {
fileWriter.write(this.blob); fileWriter.write(blob);
}.bind(this)); });
}.bind(this)); });
} else { } else {
console.log('Failed to get window'); console.log('Failed to get window');
} }
}
}, },
render: function() { render: function() {
var View; var View;
@ -133,25 +137,7 @@
'click': 'onclick' 'click': 'onclick'
}, },
save: function(e) { save: function(e) {
var suggestedName; this.model.saveFile();
if (this.model.fileType) {
suggestedName = 'signal.' + this.model.fileType;
}
var w = extension.windows.getViews()[0];
if (w && w.chrome && w.chrome.fileSystem) {
w.chrome.fileSystem.chooseEntry({
type: 'saveFile', suggestedName: suggestedName
}, function(entry) {
if (!entry) {
return;
}
entry.createWriter(function(fileWriter) {
fileWriter.write(this.model.blob);
}.bind(this));
}.bind(this));
} else {
console.log('Failed to get window');
}
}, },
onclick: function(e) { onclick: function(e) {
var $el = this.$(e.target); var $el = this.$(e.target);
@ -162,7 +148,7 @@
} }
}, },
render_attributes: function() { render_attributes: function() {
return { url: this.model.url }; return { url: this.model.objectUrl };
} }
}); });