Switch to FileSaver lib for downloading Blob files, add Blob polyfill

This commit is contained in:
Tautvidas Sipavičius
2015-11-05 17:17:54 +02:00
parent 983df216f3
commit dcfe6357cf
4 changed files with 40 additions and 29 deletions

View File

@ -3,8 +3,10 @@ define([
'mailpoet',
'backbone',
'backbone.marionette',
'jquery'
], function(App, MailPoet, Backbone, Marionette, jQuery) {
'jquery',
'blob',
'filesaver'
], function(App, MailPoet, Backbone, Marionette, jQuery, Blob, FileSaver) {
"use strict";
@ -54,31 +56,15 @@ define([
};
Module.exportTemplate = function(options) {
if (!window.Blob || !window.URL) {
// TODO: Gracefully exit on incompatible browsers
console.log('Template export requires a browser with Blob and URL support.');
return;
}
var data = _.extend(options || {}, {
body: App.getBody(),
});
var blob = new Blob(
[JSON.stringify(data)],
{ type: 'application/json;charset=utf-8' }
);
// Create a template file and force download it
var blob = new window.Blob([JSON.stringify(data)], { type: 'application/json' }),
url = window.URL.createObjectURL(blob),
anchor = document.createElement('a');
anchor.href = url;
anchor.download = 'template.json';
anchor.style.display = 'none';
document.body.appendChild(anchor);
anchor.click();
window.URL.revokeObjectURL(url);
document.body.removeChild(anchor);
FileSaver.saveAs(blob, 'template.json');
};
Module.SaveView = Marionette.LayoutView.extend({