This commit is contained in:
2020-09-08 19:10:56 +00:00
parent 10abb41a0c
commit 1ac6fc68a7
2 changed files with 71 additions and 26 deletions

View File

@@ -69,7 +69,7 @@ namespace AyaNova.Biz
return null; return null;
} }
Report newObject = new Report(); Report newObject = new Report();
CopyObject.Copy(dbObject, newObject); CopyObject.Copy(dbObject, newObject);
string newUniqueName = string.Empty; string newUniqueName = string.Empty;
bool NotUnique = true; bool NotUnique = true;
@@ -402,21 +402,21 @@ namespace AyaNova.Biz
clientMeta = reportParam.ClientMeta.ToString(); clientMeta = reportParam.ClientMeta.ToString();
//add Server meta data //add Server meta data
var serverMeta=$"{{ayApiUrl:`{apiUrl}`}}"; var serverMeta = $"{{ayApiUrl:`{apiUrl}`}}";
#if (DEBUG) #if (DEBUG)
//view page contents //view page contents
var pagecontent = await page.GetContentAsync(); var pagecontent = await page.GetContentAsync();
#endif #endif
//prePareData / preRender //prePareData / preRender
var ReportDataObject=$"{{ ayReportData:{ReportData}, ayClientMetaData:{clientMeta}, ayServerMetaData:{serverMeta} }}"; var ReportDataObject = $"{{ ayReportData:{ReportData}, ayClientMetaData:{clientMeta}, ayServerMetaData:{serverMeta} }}";
var preRenderResult=await page.EvaluateExpressionAsync<dynamic>($"ayPreRender({ReportDataObject});"); var PreParedReportDataObject = await page.EvaluateExpressionAsync<dynamic>($"ayPreRender({ReportDataObject});");//note ayPreRender is async but dont' use await to call it as the EvaluateExpressionAsync function knows how to handle that already
//compile the template //compile the template
var compileScript = $"Handlebars.compile(`{report.Template}`)({preRenderResult});"; var compileScript = $"Handlebars.compile(`{report.Template}`)({PreParedReportDataObject});";
var compiledHTML = await page.EvaluateExpressionAsync<string>(compileScript); var compiledHTML = await page.EvaluateExpressionAsync<string>(compileScript);
//render report as HTML //render report as HTML
@@ -424,12 +424,7 @@ namespace AyaNova.Biz
//add style (after page or it won't work) //add style (after page or it won't work)
if (!string.IsNullOrWhiteSpace(report.Style)) if (!string.IsNullOrWhiteSpace(report.Style))
{
await page.AddStyleTagAsync(new AddTagOptions { Content = report.Style }); await page.AddStyleTagAsync(new AddTagOptions { Content = report.Style });
}
//If need the generated page content
//var pagecontent = await page.GetContentAsync();
string outputFileName = StringUtil.ReplaceLastOccurrence(FileUtil.NewRandomFileName, ".", "") + ".pdf"; string outputFileName = StringUtil.ReplaceLastOccurrence(FileUtil.NewRandomFileName, ".", "") + ".pdf";
string outputFullPath = System.IO.Path.Combine(FileUtil.TemporaryFilesFolder, outputFileName); string outputFullPath = System.IO.Path.Combine(FileUtil.TemporaryFilesFolder, outputFileName);

View File

@@ -34,15 +34,65 @@ async function ayPreRender(ayAllData) {
} }
} }
// function ayPreRender(ayAllData) { ///////////////////////////////////
// if (typeof ayPrepareData === "function") { // GET DATA FROM API SERVER
// return ayPrepareData(ayAllData); //
// } else { async function ayGetFromAPI(route, token) {
// return ayAllData; 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;
}
}
// function ayPreRender(ayAllData) { ///////////////////////////////////
// return ayAllData; // POST DATA TO API SERVER
// // return typeof ayPrepareData; //
// } 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;
}
*/