Files
raven/server/AyaNova/resource/rpt/ay-report.js
2020-09-08 19:10:56 +00:00

99 lines
2.7 KiB
JavaScript

function ayRegisterHelpers() {
Handlebars.registerHelper("ayCaps", function (aString) {
return aString.toUpperCase();
});
Handlebars.registerHelper("ayMarkdown", function (astring) {
return marked(astring, { breaks: true });
});
Handlebars.registerHelper("ayJSON", function (obj) {
return JSON.stringify(obj, null, 3);
});
Handlebars.registerHelper("ayLink", function (text, url) {
var url = Handlebars.escapeExpression(url),
text = Handlebars.escapeExpression(text);
return new Handlebars.SafeString("<a href='" + url + "'>" + text + "</a>");
});
Handlebars.registerHelper("ayLogo", function (size) {
var url = `${Handlebars.escapeExpression(
this.ayServerMetaData.ayApiUrl
)}logo/${size}`;
return new Handlebars.SafeString("<img src='" + url + "'/>");
});
} //eof
async function ayPreRender(ayAllData) {
if (typeof ayPrepareData === "function") {
return await ayPrepareData(ayAllData);
} else {
return ayAllData;
}
}
///////////////////////////////////
// GET DATA FROM API SERVER
//
async function ayGetFromAPI(route, token) {
try {
let r = await fetch(route, {
method: "get",
mode: "cors",
headers: {
Accept: "application/json",
"Content-Type": "application/json",
Authorization: token
}
});
return await r.json();
} catch (error) {
//fundamental error, can't proceed with this call
// handleError("GET", error, route);
//todo: deal with this properly
throw error;
}
}
///////////////////////////////////
// POST DATA TO API SERVER
//
async function ayPostToAPI(route, token, data) {
try {
fetchOptions = {
method: "post",
mode: "cors",
headers: {
Accept: "application/json",
"Content-Type": "application/json",
Authorization: token
},
body: JSON.stringify(data)
};
let r = await fetch(route, fetchOptions);
return await r.json();
} catch (error) {
//todo: better handle this
throw error;
}
}
/*
async function ayPrepareData(reportData) {
//this function (if present) is called with the report data
//before the report is rendered
//modify data as required here and return it to change the data before the report renders
//Example of using API method to fetch data from API server and make it available to the report template
let route=`${reportData.ayServerMetaData.ayApiUrl}enum-list/list/AyaType`;
//Put the data into the main report data object so it's available to the template
reportData.myData={AyaTypeList:await ayGetFromAPI(route, reportData.ayClientMetaData.Authorization)};
return reportData;
}
*/