153 lines
6.2 KiB
JavaScript
153 lines
6.2 KiB
JavaScript
/* Urutau - REPORTS page: a live preview of the HTML report (the same document
|
|
that is downloaded and printed), following the period and host chosen in the
|
|
header, plus the report language. Two actions only: download and print.
|
|
The report is served by GET /api/v1/report?format=html (self-contained, no
|
|
script); the preview is a same-origin iframe of it with inline=1. */
|
|
(function () {
|
|
'use strict';
|
|
|
|
var ICON = '<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1.8" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"><path d="M7 3h7l5 5v13H7z"/><path d="M14 3v5h5M10 13h6M10 17h6"/></svg>';
|
|
var LANG_KEY = 'urutau.report.lang';
|
|
var A4_PX = 794; // A4 width at 96 dpi: the preview is always an A4 sheet, scaled to fit
|
|
|
|
function boot() {
|
|
var U = window.Urutau;
|
|
if (!U || !U.registerPage) return false;
|
|
U.registerI18n({
|
|
en: {
|
|
'rpt.title': 'Reports',
|
|
'rpt.dl': 'Download',
|
|
'rpt.print': 'Print',
|
|
'rpt.lang': 'Report language',
|
|
'rpt.scope': 'Period and server follow the selection at the top of the page.',
|
|
'rpt.preview': 'Report preview',
|
|
'rpt.loading': 'Building the report...',
|
|
'rpt.err': 'Could not build the report.',
|
|
'rpt.retry': 'Try again'
|
|
},
|
|
'pt-BR': {
|
|
'rpt.title': 'Relatórios',
|
|
'rpt.dl': 'Baixar',
|
|
'rpt.print': 'Imprimir',
|
|
'rpt.lang': 'Idioma do relatório',
|
|
'rpt.scope': 'O período e o servidor seguem a seleção no topo da página.',
|
|
'rpt.preview': 'Prévia do relatório',
|
|
'rpt.loading': 'Montando o relatório...',
|
|
'rpt.err': 'Não foi possível montar o relatório.',
|
|
'rpt.retry': 'Tentar de novo'
|
|
}
|
|
});
|
|
var t = U.t, el = U.el;
|
|
var ctx0 = null, root = null, frame = null, status = null, wrap = null, lang = '';
|
|
var docH = 1123;
|
|
|
|
// fit: the iframe keeps the A4 width (so the report never reflows to the
|
|
// screen) and is scaled down to the space available.
|
|
function fit() {
|
|
if (!frame || !wrap) return;
|
|
var avail = wrap.clientWidth;
|
|
var scale = Math.min(1, avail / A4_PX);
|
|
frame.style.width = A4_PX + 'px';
|
|
frame.style.height = docH + 'px';
|
|
frame.style.transform = 'scale(' + scale + ')';
|
|
wrap.style.height = Math.ceil(docH * scale) + 'px';
|
|
}
|
|
function onResize() { fit(); }
|
|
|
|
try { lang = localStorage.getItem(LANG_KEY) || ''; } catch (e) { lang = ''; }
|
|
|
|
function reportLang() {
|
|
if (lang === 'en' || lang === 'pt-BR') return lang;
|
|
return ctx0 && ctx0.lang === 'en' ? 'en' : 'pt-BR';
|
|
}
|
|
function url(inline) {
|
|
var p = U.rangeParams();
|
|
if (ctx0 && ctx0.host && ctx0.host !== 'all') p.host = ctx0.host;
|
|
p.format = 'html';
|
|
p.lang = reportLang();
|
|
if (inline) p.inline = '1';
|
|
return '/api/v1/report?' + new URLSearchParams(p).toString();
|
|
}
|
|
function showStatus(key, retry) {
|
|
while (status.firstChild) status.removeChild(status.firstChild);
|
|
status.hidden = !key;
|
|
if (!key) return;
|
|
status.appendChild(el('span', null, t(key)));
|
|
if (retry) {
|
|
var b = el('button', { type: 'button', class: 'btn' }, t('rpt.retry'));
|
|
b.addEventListener('click', refresh);
|
|
status.appendChild(b);
|
|
}
|
|
}
|
|
function refresh() {
|
|
if (!frame) return;
|
|
showStatus('rpt.loading');
|
|
frame.src = url(true);
|
|
}
|
|
|
|
function build(c) {
|
|
ctx0 = c;
|
|
root = el('div', { class: 'rpt' });
|
|
var bar = el('div', { class: 'rpt-bar' });
|
|
var sel = el('select', { class: 'rpt-sel', 'aria-label': t('rpt.lang') });
|
|
[['pt-BR', 'Português'], ['en', 'English']].forEach(function (o) {
|
|
var opt = el('option', { value: o[0] }, o[1]);
|
|
if (reportLang() === o[0]) opt.selected = true;
|
|
sel.appendChild(opt);
|
|
});
|
|
sel.addEventListener('change', function () {
|
|
lang = sel.value;
|
|
try { localStorage.setItem(LANG_KEY, lang); } catch (e) { /* private mode */ }
|
|
refresh();
|
|
});
|
|
bar.appendChild(el('label', { class: 'rpt-opt' }, el('span', null, t('rpt.lang')), sel));
|
|
bar.appendChild(el('p', { class: 'rpt-hint' }, t('rpt.scope')));
|
|
var actions = el('div', { class: 'rpt-actions' });
|
|
var dl = el('button', { type: 'button', class: 'btn' }, t('rpt.dl'));
|
|
dl.addEventListener('click', function () {
|
|
var a = document.createElement('a');
|
|
a.href = url(false); a.download = '';
|
|
document.body.appendChild(a); a.click(); document.body.removeChild(a);
|
|
});
|
|
var pr = el('button', { type: 'button', class: 'btn btn-primary' }, t('rpt.print'));
|
|
pr.addEventListener('click', function () {
|
|
try { frame.contentWindow.focus(); frame.contentWindow.print(); }
|
|
catch (e) { window.open(url(true), '_blank', 'noopener'); }
|
|
});
|
|
actions.appendChild(dl); actions.appendChild(pr);
|
|
bar.appendChild(actions);
|
|
|
|
status = el('div', { class: 'rpt-status', role: 'status' });
|
|
frame = el('iframe', { class: 'rpt-frame', title: t('rpt.preview') });
|
|
frame.addEventListener('load', function () {
|
|
var ok = false;
|
|
try {
|
|
var d = frame.contentDocument;
|
|
ok = !!(d && d.querySelector('h1'));
|
|
if (ok) docH = Math.max(d.documentElement.scrollHeight, d.body ? d.body.scrollHeight : 0, 1123);
|
|
} catch (e) { ok = false; }
|
|
fit();
|
|
showStatus(ok ? '' : 'rpt.err', !ok);
|
|
});
|
|
root.appendChild(bar);
|
|
root.appendChild(status);
|
|
wrap = el('div', { class: 'rpt-fit' }, frame);
|
|
root.appendChild(el('div', { class: 'rpt-sheetwrap' }, wrap));
|
|
window.addEventListener('resize', onResize);
|
|
return root;
|
|
}
|
|
|
|
U.registerPage({
|
|
id: 'reports', group: 'reports', icon: ICON, titleKey: 'rpt.title',
|
|
mount: function (host, c) { host.appendChild(build(c)); fit(); refresh(); },
|
|
update: function (c) { ctx0 = c; refresh(); },
|
|
unmount: function () { window.removeEventListener('resize', onResize); if (root && root.parentNode) root.parentNode.removeChild(root); root = frame = status = wrap = null; }
|
|
});
|
|
return true;
|
|
}
|
|
|
|
if (!boot()) {
|
|
var tries = 0, iv = setInterval(function () { if (boot() || ++tries > 50) clearInterval(iv); }, 100);
|
|
}
|
|
})();
|