2018-09-25 16:28:26 +00:00
|
|
|
import Component from '@ember/component';
|
|
|
|
import { computed } from '@ember/object';
|
2018-04-03 14:16:57 +00:00
|
|
|
import hbs from 'htmlbars-inline-precompile';
|
|
|
|
|
2018-09-25 16:28:26 +00:00
|
|
|
export default Component.extend({
|
2019-05-13 19:05:25 +00:00
|
|
|
layout: hbs`{{#if (has-block)}} {{yield}} {{else}} {{actionText}} {{/if}}`,
|
2018-04-03 14:16:57 +00:00
|
|
|
tagName: 'a',
|
|
|
|
role: 'button',
|
|
|
|
attributeBindings: ['role', 'download', 'href'],
|
|
|
|
download: computed('filename', 'extension', function() {
|
|
|
|
return `${this.get('filename')}-${new Date().toISOString()}.${this.get('extension')}`;
|
|
|
|
}),
|
|
|
|
|
2018-04-17 22:04:34 +00:00
|
|
|
fileLike: computed('data', 'mime', 'stringify', 'download', function() {
|
2018-04-17 20:02:37 +00:00
|
|
|
let file;
|
2018-04-03 14:16:57 +00:00
|
|
|
let data = this.get('data');
|
2018-04-17 20:02:37 +00:00
|
|
|
let filename = this.get('download');
|
|
|
|
let mime = this.get('mime');
|
2018-04-03 14:16:57 +00:00
|
|
|
if (this.get('stringify')) {
|
|
|
|
data = JSON.stringify(data, null, 2);
|
|
|
|
}
|
2018-04-17 20:02:37 +00:00
|
|
|
if (window.navigator.msSaveOrOpenBlob) {
|
|
|
|
file = new Blob([data], { type: mime });
|
|
|
|
file.name = filename;
|
|
|
|
} else {
|
|
|
|
file = new File([data], filename, { type: mime });
|
|
|
|
}
|
|
|
|
return file;
|
|
|
|
}),
|
2018-04-03 14:16:57 +00:00
|
|
|
|
2018-04-17 20:02:37 +00:00
|
|
|
href: computed('fileLike', function() {
|
|
|
|
return window.URL.createObjectURL(this.get('fileLike'));
|
2018-04-03 14:16:57 +00:00
|
|
|
}),
|
|
|
|
|
2018-04-17 20:02:37 +00:00
|
|
|
click(event) {
|
|
|
|
if (!window.navigator.msSaveOrOpenBlob) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
event.preventDefault();
|
|
|
|
let file = this.get('fileLike');
|
|
|
|
//lol whyyyy
|
|
|
|
window.navigator.msSaveOrOpenBlob(file, file.name);
|
|
|
|
},
|
|
|
|
|
2018-04-03 14:16:57 +00:00
|
|
|
actionText: 'Download',
|
|
|
|
data: null,
|
|
|
|
filename: null,
|
|
|
|
mime: 'text/plain',
|
|
|
|
extension: 'txt',
|
|
|
|
stringify: false,
|
|
|
|
});
|