From a069939f657b4145c52b221ef5f64400925804dd Mon Sep 17 00:00:00 2001 From: lilia Date: Thu, 30 Jun 2016 00:54:09 -0700 Subject: [PATCH] Refactor attachment saving for DRYness // FREEBIE --- js/views/attachment_view.js | 84 ++++++++++++++++--------------------- 1 file changed, 35 insertions(+), 49 deletions(-) diff --git a/js/views/attachment_view.js b/js/views/attachment_view.js index 1bf038d7..88089d3b 100644 --- a/js/views/attachment_view.js +++ b/js/views/attachment_view.js @@ -72,37 +72,41 @@ 'click': 'onclick' }, onclick: function(e) { - if (this.contentType === 'image') { - var view = new Whisper.LightboxView({ - model: { - url : this.objectUrl, - blob : this.blob, - fileType : this.fileType + switch (this.contentType) { + case 'audio': + case 'video': + return; + case 'image': + var view = new Whisper.LightboxView({ model: this }); + view.render(); + view.$el.appendTo(this.el); + view.$el.trigger('show'); + break; + + default: + this.saveFile(); + } + }, + saveFile: function() { + var blob = this.blob; + var suggestedName; + if (this.fileType) { + suggestedName = 'signal.' + this.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(blob); + }); }); - view.render(); - view.$el.appendTo(this.el); - view.$el.trigger('show'); - } else if (this.contentType !== 'audio' && this.contentType !== 'video') { - var suggestedName; - if (this.fileType) { - suggestedName = 'signal.' + this.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.blob); - }.bind(this)); - }.bind(this)); - } else { - console.log('Failed to get window'); - } + } else { + console.log('Failed to get window'); } }, render: function() { @@ -133,25 +137,7 @@ 'click': 'onclick' }, save: function(e) { - var suggestedName; - 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'); - } + this.model.saveFile(); }, onclick: function(e) { var $el = this.$(e.target); @@ -162,7 +148,7 @@ } }, render_attributes: function() { - return { url: this.model.url }; + return { url: this.model.objectUrl }; } });