@dlr-eoc/utils-browser
Version:
This library contains a collection of utilities like download data as blob and Paper layout.
100 lines (95 loc) • 2.68 kB
JavaScript
function downloadJson(data, fileName) {
const jsonData = JSON.stringify(data);
const blob = new Blob([jsonData], { type: 'text/json;charset=utf-8;' });
return downloadBlob(blob, fileName);
}
function downloadBlob(blob, fileName) {
const url = window.URL.createObjectURL(blob);
downloadUrl(url, fileName);
}
function downloadUrl(url, fileName) {
// window.open(url) doesn't work here. Instead, we create a temporary link item and simulate a click on it.
const a = document.createElement('a');
document.body.appendChild(a);
a.style.display = 'none';
a.href = url;
a.download = fileName;
a.click();
window.URL.revokeObjectURL(url);
document.body.removeChild(a);
}
const PaperSizeTable = {
A6: {
longCm: 14.8,
shortCm: 10.5
},
A5: {
longCm: 21.0,
shortCm: 14.8
},
A4: {
longCm: 29.7,
shortCm: 21.0
},
A3: {
longCm: 42.0,
shortCm: 29.7
},
A2: {
longCm: 59.4,
shortCm: 42.0
},
A1: {
longCm: 84.1,
shortCm: 59.4
},
A0: {
longCm: 118.9,
shortCm: 84.1
},
};
const inchPerCm = 0.393701;
/**
* A utility class intended to help with getting the dimensions of a paper.
* Especially useful for preparing html that needs to be printed.
*/
class Paper {
constructor(format, resolution, orientation) {
this.format = format;
this.resolution = resolution;
this.orientation = orientation;
const shortCm = PaperSizeTable[format].shortCm;
const longCm = PaperSizeTable[format].longCm;
const shortPx = Math.floor(shortCm * inchPerCm * resolution);
const longPx = Math.floor(longCm * inchPerCm * resolution);
if (orientation === 'portrait') {
this.widthCm = shortCm;
this.heightCm = longCm;
this.widthPx = shortPx;
this.heightPx = longPx;
}
else {
this.widthCm = longCm;
this.heightCm = shortCm;
this.widthPx = longPx;
this.heightPx = shortPx;
}
}
updateFormat(format) {
return new Paper(format, this.resolution, this.orientation);
}
updateResolution(resolution) {
return new Paper(this.format, resolution, this.orientation);
}
updateOrientation(orientation) {
return new Paper(this.format, this.resolution, orientation);
}
}
/*
* Public API Surface of utils-browser
*/
/**
* Generated bundle index. Do not edit.
*/
export { Paper, downloadBlob, downloadJson, downloadUrl };
//# sourceMappingURL=dlr-eoc-utils-browser.mjs.map