Browse Source

Delegating common functions and adjusting file names

Andrej Studen 1 year ago
parent
commit
5a11e8cbe9
3 changed files with 56 additions and 169 deletions
  1. 1 1
      views/approvedPortal.webpart.xml
  2. 45 162
      web/crf/crfManager.js
  3. 10 6
      web/crf/formPortal.js

+ 1 - 1
views/approvedPortal.webpart.xml

@@ -1,4 +1,4 @@
 <webpart xlmns="http://labkey.org/data/xml/webpart"
-	title="CRF Manager Portal">
+	title="CRF Approved Portal">
 	<view name="approvedPortal"/>
 </webpart>

+ 45 - 162
web/crf/crfManager.js

@@ -1,130 +1,18 @@
-//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(){
-
-	
+	let debug=true;
+   let fName='[generateDescription]';
+   if (debug)
+      print(fName);
 
 	//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());
@@ -135,40 +23,12 @@ function setFormConfig(){
 	selectRows.schemaName='lists';
 	selectRows.queryName='crfSettings';
 	//store form related data to this object
-	selectRows.success=afterSettings;
+	selectRows.success=function(data){afterSettings(data,collectLayout);};
 	LABKEY.Query.selectRows(selectRows);
 
 }
 
-function afterSettings(data){
-
-	config.formConfig.settings=new Array();
-	for (let i=0;i<data.rows.length;i++){
-		let n=data.rows[i]['name'];
-		let v=data.rows[i]['value'];
-		config.formConfig.settings[n]=v;
-	}
-
-	let st=config.formConfig.settings;
-	print('afterSettings');
-	for (let k in st){
-		print('\t'+k+'='+st[k]);
-	}
-
-	//if ('dataContainer' in st){
-	//	setContainer('data',st['dataContainer']);
-	//}
-	let vname='configContainer';
-	if (vname in st){
-		setContainer('config',st[vname]);
-	}
-	print('Config: '+getContainer('config'));
-	print('Data: '+getContainer('data'));
-	collectData();
-}
-
-
-function collectData(){
+function collectLayout(){
 
 	let queryArray=new Array();
 	//users
@@ -184,7 +44,7 @@ function collectData(){
 	//
 
 	print('running getDataFromQueries');
-	getDataFromQueries(queryArray,fcontinue);
+	getDataFromQueries(queryArray,loadForms);
 }
 
 function findName(listId){
@@ -195,7 +55,7 @@ function findName(listId){
 	}
 }
 
-function fcontinue(){
+function loadForms(){
 	print('loadedData');
 	let queryArray=new Array();
 	
@@ -213,7 +73,7 @@ function fcontinue(){
 		queryArray.push(makeQuery('data',showQuery,showQuery,[]));
 	}
 
-	getDataFromQueries(queryArray,fcontinue1);
+	getDataFromQueries(queryArray,printLayout);
 
 }
 
@@ -268,10 +128,6 @@ function printFields(lookupList,listName){
 	}
 }
 
-function fcontinue1(){
-	printLayout();
-}
-
 function printData(){
 	let frows=config.formConfig.formData.rows;
 	let lookupList=new Set();
@@ -343,7 +199,7 @@ function checkBlob(){
 
 function printLayout(){
 
-	config.doc=new PDFDocument();
+	config.doc=new PDFDocument({margins: {top:20,left:50,right:100,bottom:20},paragraphGap:10});
 	//config.doc.end();
 	let stream = config.doc.pipe(blobStream()).on("finish",function(){
 			config.blob=stream.toBlob("application/pdf");});
@@ -364,6 +220,8 @@ function printLayout(){
 
 function printTitlePDF(fontSize,title){
 	config.doc.y+=10;
+   //x margin
+   //config.doc.x=50;
 	config.doc.font('Courier-Bold').fontSize(fontSize).text(title);
 }
 
@@ -376,15 +234,23 @@ function printPDF(field){
 	//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 fName='[printPDF]';
 	
-	let w=476;
+	let w=676;
 	let spacing=25;
 	let w1=(w-spacing)*0.5;
 	let fontSize=14;	
+   let margin=50;
+   let topMargin=20;
 	
-	print('printPDF: entry['+field.name);
+	print(fName+' entry['+field.name);
 	print('printPDF: field type:'+field.type);
 
+   config.doc.y+=10;
+   let ty1=config.doc.y;
+
+
 	//measure text
 	let label=field.caption;
 	let opt={width:w1};
@@ -400,24 +266,41 @@ function printPDF(field){
 	//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
+   //store x value for later use
 	let tx=config.doc.x;
 	let ty=config.doc.y;
+   
+   let height=config.doc.heightOfString(label,opt);
+   let lWidth=config.doc.widthOfString(label,opt);
+   
+   let ypos=ty1;
+   if (ty<ty1) ypos=topMargin;
+   let xpos=lWidth+spacing+margin;
+   if (height>2*fontSize){
+      //multiline text
+      ypos=0.5*(ty+ty1);
+      if (ty<ty1){
+         //page break
+         ypos=0.5*ty;
+      }
+      xpos=w1+spacing+margin;
+	}
 
 	//shift for value output
-	config.doc.x+=w1+spacing;
-	let v=field.type;
+	config.doc.x=xpos;
+   config.doc.y=ypos;
+	let v='['+field.type;
 	if ('lookup' in field){
 		v+='/'+field.lookup.queryName;
 	}
+   v+=']';
 	print('v: '+v);
 	config.doc.font('Courier-Bold').text(v,opt);
 
 	//restore x value
 	config.doc.x=tx;
+   //set y to maximum of current and position of the label
+   if (config.doc.y<ty) config.doc.y=ty;
 	
 }
 

+ 10 - 6
web/crf/formPortal.js

@@ -131,12 +131,13 @@ function generateFormArray(){
 	selectRows.schemaName='lists';
 	selectRows.queryName='crfSettings';
 	//store form related data to this object
-	selectRows.success=afterSettings;
+	selectRows.success=function(data){afterSettings(data,collectData);};
 	LABKEY.Query.selectRows(selectRows);
 
 }
 
-function afterSettings(data){
+//also used by crfManager
+function afterSettings(data,cb){
 
 	config.formConfig.settings=new Array();
 	for (let i=0;i<data.rows.length;i++){
@@ -160,8 +161,11 @@ function afterSettings(data){
 	}
 	print('Config: '+getContainer('config'));
 	print('Data: '+getContainer('data'));
+   //collectData();
+   cb();
+}
 
-
+function collectData(){
 	//setup queryArray
 	let queryArray=new Array();
 
@@ -207,14 +211,14 @@ function addStudyData(){
       columnModel+=varRows[i]['staticVariable'];
    }
 	e.columns=columnModel;
-   getDataFromQueries(queryArray,fcontinue);
+   getDataFromQueries(queryArray,generateWidgets);
 
 
 }
 
 
-function fcontinue(){
-   let fName='[fcontinue]';
+function generateWidgets(){
+   let fName='[generateWidgets]';
 	let formConfig=config.formConfig;
 
 	print("Number of study data entries: "+formConfig.studyData.rows.length);