-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
41 lines (33 loc) · 989 Bytes
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
const DOWNLOADER_BUTTON = 'downloader-button';
export default class Downloader {
/**
* @param {HTMLElement} domElem
*/
constructor(domElem) {
this.domElem = domElem;
}
/**
* @param {Array<CS.FileInfo>} files
*/
update(files) {
const fragment = document.createDocumentFragment();
files.forEach(file => {
fragment.appendChild(this._createLink(file))
});
this.domElem.innerHTML = '';
this.domElem.appendChild(fragment);
}
/**
* @param {CS.FileInfo} fileinfo
* @returns {HTMLAnchorElement}
*/
_createLink(fileinfo) {
const blob = new Blob([ fileinfo.content ], { type: fileinfo.mimeType });
const link = document.createElement('a');
link.textContent = fileinfo.title;
link.download = fileinfo.filename;
link.href = URL.createObjectURL(blob);
link.classList.add(DOWNLOADER_BUTTON);
return link;
}
}