//global config variable const config=new Object(); function print(msg){ config.document.getElementById(config.debugArea).value+="\n"+msg; } function clear(){ config.document.getElementById(config.debugArea).value=""; } function doNothing(){ print('doNothing called'); } function generateDescription(){ //loop over all forms //read the setup print('Generate description'); setFormConfig(); } function setContainer(label,container){ if (!(config.formConfig.hasOwnProperty('container'))){ config.formConfig.container=new Array(); } config.formConfig.container[label]=container; } function getContainer(label){ return config.formConfig.container[label]; } function makeQuery(containerName,queryName,fieldName,filterArray){ let e=new Object(); e.containerName=containerName; e.queryName=queryName; e.fieldName=fieldName; e.filterArray=filterArray; return e; } function getDataFromQueries(queryArray,cb){ //queryArray should contain elements with //- fieldName to set the data variable //- containerName to select container (data,config,CRF) //- queryName to select query //- filterArray to perform filtering, empty array works //- callback cb to be called with no arguments // afterQuery(new Object(),-1,queryArray,cb); } function afterQuery(data,id,queryArray,cb){ //queryArray should contain elements with //- fieldName to set the data variable //- containerName to select container (data,config,CRF) //- queryName to select query //- filterArray to perform filtering, empty array works //- callback cb to be called with no arguments // //it should be called with id -1. // print('afterQuery['+id+'/'+queryArray.length+']: '); if (id>-1){ let fieldName=queryArray[id].fieldName; print('afterQuery['+fieldName+']: '+data.rows.length); config.formConfig[fieldName]=data; } id+=1; if (id==queryArray.length) { cb(); return; } let e=queryArray[id]; let qconfig=new Object(); qconfig.containerPath=getContainer(e.containerName); qconfig.schemaName="lists"; if ("schemaName" in e){ print('afterQuery: schemaName='+e.schemaName); qconfig.schemaName=e.schemaName; } if ("columns" in e){ print('afterQuery: columns='+e.columns); qconfig.columns=e.columns; } qconfig.queryName=e.queryName; //this should point to configuration container //don't filter -> so we can pick up other forms (say registration) later on //qconfig.filterArray=[LABKEY.Filter.create('Key',config.formId)]; if ("filterArray" in e) qconfig.filterArray=e.filterArray; //qconfig.filterArray=[LABKEY.Filter.create('formStatus',1)] qconfig.success=function(data){afterQuery(data,id,queryArray,cb);}; qconfig.failure=doNothing; LABKEY.Query.selectRows(qconfig); } function setFormConfig(){ //add object to store form related data config.formConfig=new Object(); config.formConfig.softwareVersion='0.0.1'; let debug=true; if (debug) print("setFormConfig"); //set containers for data and configuration //TODO: set this from a query // setContainer('data',LABKEY.ActionURL.getContainer()); setContainer('config',LABKEY.ActionURL.getContainer()); setContainer('CRF',LABKEY.ActionURL.getContainer()); let selectRows=new Object(); //this is local data selectRows.containerPath=getContainer('CRF'); selectRows.schemaName='lists'; selectRows.queryName='crfSettings'; //store form related data to this object selectRows.success=afterSettings; LABKEY.Query.selectRows(selectRows); } function afterSettings(data){ config.formConfig.settings=new Array(); for (let i=0;i100){ clearInterval(config.blobInterval); } } function printLayout(){ config.doc=new PDFDocument(); //config.doc.end(); let stream = config.doc.pipe(blobStream()).on("finish",function(){ config.blob=stream.toBlob("application/pdf");}); print("BLob: "+config.blob); config.a = config.document.createElement("a"); config.document.body.appendChild(config.a); config.a.innerHTML="Download PDF"; config.a.style = "display: none"; config.count=0; //run until blob is set config.blobInterval=setInterval(checkBlob,1000); //pick data from crfForm list print("Printing form"); printData(); } function printTitlePDF(fontSize,title){ config.doc.y+=10; config.doc.font('Courier-Bold').fontSize(fontSize).text(title); } function printPDF(field){ //object field should have a name, type, caption //entry should have field.name //lookup is null or has a lookup table LUT //for value v of entry[field.name] // //the total width of a A4 page is 598 px, //left margin is 72. With a right margin of 50, //the total available with is 476 px. let w=476; let spacing=25; let w1=(w-spacing)*0.5; let fontSize=14; print('printPDF: entry['+field.name); print('printPDF: field type:'+field.type); //measure text let label=field.caption; let opt={width:w1}; config.doc.fontSize(fontSize); //for more eloquent display the height of the text //can be measured prior to output //use currentLineHeight to scale height //let lineH=config.doc.currentLineHeight(1); //let h=config.doc.heightOfString(label,opt)/lineH; //print label config.doc.font('Courier').text(label,opt); //align last row of description w/ first row of value config.doc.moveUp(); //store x value for later use let tx=config.doc.x; let ty=config.doc.y; //shift for value output config.doc.x+=w1+spacing; let v=field.type; if ('lookup' in field){ v+='/'+field.lookup.queryName; } print('v: '+v); config.doc.font('Courier-Bold').text(v,opt); //restore x value config.doc.x=tx; } function printPDFEntry(field,entry){ //object field should have a name, type, caption //entry should have field.name //lookup is null or has a lookup table LUT //for value v of entry[field.name] // //the total width of a A4 page is 598 px, //left margin is 72. With a right margin of 50, //the total available with is 476 px. let w=476; let spacing=25; let w1=(w-spacing)*0.5; let fontSize=14; print('printPDF: entry['+field.name); print('printPDF: field type:'+field.type); //measure text let label=entry[field.name]; let opt={width:w1}; config.doc.fontSize(fontSize); //for more eloquent display the height of the text //can be measured prior to output //use currentLineHeight to scale height //let lineH=config.doc.currentLineHeight(1); //let h=config.doc.heightOfString(label,opt)/lineH; //print label config.doc.font('Courier').text(label,opt); }