1 |
6c9cb54a
|
Jan Pašek
|
/**
|
2 |
|
|
* Generate a file to be downloaded by the browser
|
3 |
|
|
* @param filename name of the file to be downloaded
|
4 |
|
|
* @param text content of the downloaded file
|
5 |
|
|
*/
|
6 |
e77db3b6
|
Jan Pašek
|
function download(filename, text) {
|
7 |
6c9cb54a
|
Jan Pašek
|
// first a hidden <a> element is created and download content is assigned
|
8 |
|
|
var element = document.createElement('a');
|
9 |
|
|
element.setAttribute('href', 'data:text/plain;charset=utf-8,' + encodeURIComponent(text));
|
10 |
|
|
element.setAttribute('download', filename);
|
11 |
e77db3b6
|
Jan Pašek
|
|
12 |
6c9cb54a
|
Jan Pašek
|
element.style.display = 'none';
|
13 |
|
|
document.body.appendChild(element);
|
14 |
e77db3b6
|
Jan Pašek
|
|
15 |
6c9cb54a
|
Jan Pašek
|
// generate artificial click event to download the file
|
16 |
|
|
element.click();
|
17 |
e77db3b6
|
Jan Pašek
|
|
18 |
6c9cb54a
|
Jan Pašek
|
// remove the download link from the webpage
|
19 |
|
|
document.body.removeChild(element);
|
20 |
9cac7fd4
|
Jan Pašek
|
}
|
21 |
|
|
|
22 |
6c9cb54a
|
Jan Pašek
|
// Adds toDateInputValue() method to Date object
|
23 |
|
|
// toDateInputValue() produces date in YYYY-MM-DD format
|
24 |
9cac7fd4
|
Jan Pašek
|
Date.prototype.toDateInputValue = (function() {
|
25 |
|
|
var local = new Date(this);
|
26 |
|
|
local.setMinutes(this.getMinutes() - this.getTimezoneOffset());
|
27 |
|
|
return local.toJSON().slice(0,10);
|
28 |
|
|
});
|