Переглянути джерело

A more free version of formGenerator that allows generation of any form

Andrej Studen 1 рік тому
батько
коміт
1b22c1c5c8
1 змінених файлів з 112 додано та 28 видалено
  1. 112 28
      web/crf/formGenerator.js

+ 112 - 28
web/crf/formGenerator.js

@@ -1,6 +1,11 @@
 //namespace
 var formGenerator={};
 
+formGenerator.print=
+function(msg){
+   console.log(msg);
+}
+
 formGenerator.init=
 function(cb=null){
    let that=this;
@@ -34,7 +39,7 @@ function(){
       this.table.display='block';
       return;
    }
-   addFormGenerator();
+   this.addFormGenerator();
 }
 
 formGenerator.hideFormGenerator=
@@ -55,6 +60,32 @@ function(crfRef){
    return new Object();
 }
 
+formGenerator.setOptions=
+function(fieldName,keyField,valueField){
+   let fName='[setOptions]';
+   let rows=crfSetup.getRows(fieldName);
+   //let formRows=crfSetup.getRows('generateConfigData');
+   let options=new Object();
+   for (let i=0;i<rows.length;i++){
+      let id=rows[i][keyField];
+      let name=rows[i][valueField]
+      this.print(fName+' '+rows[i][keyField]+'/'+name);
+      options[id]=name;
+      //this.addOption(fgForm.formSelect,formName,formId);
+   }
+   return options;
+}
+   
+
+formGenerator.addGenerator=
+function(){
+   let that=this;
+   let action=function(){that.addFormGenerator();};
+   let parseSetup=function(){crfSetup.parseSetup(action);};
+   crfSetup.setContainers(parseSetup);
+	
+}
+
 formGenerator.addFormGenerator=
 function(){
    //parentClass should provide config and print and getContainer
@@ -63,29 +94,46 @@ function(){
    this.print(fName);
    //layout
    this.table=crfHTML.createTable('formDiv');
+   let table=this.table;
    table.className="t2";
    //this is a form manipulator
    let fgForm=new Object();
 
    fgForm.formSelect=this.addInputRow(table,'Select form',"select");
+   fgForm.queryId=this.addInputRow(table,'Select dataset','select');
    fgForm.crfSelect=this.addInputRow(table,'Select CRF',"select");
+   fgForm.userId=this.addInputRow(table,'Assign to user',"select");
+   fgForm.site=this.addInputRow(table,'Assign to site',"select");
+   fgForm.formStatus=this.addInputRow(table,'Select status','select');
    fgForm.comment=this.addInputRow(table,'Enter comment','text');
    fgForm.details=this.addInputRow(table,'Details','label');
    fgForm.warnings=this.addInputRow(table,'Warnings','label');
    fgForm.warnings.innerHTML='formGenerator version 3.1.0';
-   let formRows=crfSetup.getRows('generateConfigData');
+
+   let formRows=crfSetup.getRows('dataForms');
+   //let formRows=crfSetup.getRows('generateConfigData');
    let options=new Object();
    for (let i=0;i<formRows.length;i++){
-      let formId=formRows[i]["formId"];
-      let formName=this.getFormName(formId);
-      this.print(fName+' '+formRows[i]["formId"]+'/'+formName);
+      let formId=formRows[i]["Key"];
+      let formName=formRows[i]['formName']
+      this.print(fName+' '+formRows[i]["Key"]+'/'+formName);
       options[formId]=formName;
       //this.addOption(fgForm.formSelect,formName,formId);
    }
+
+   let selOpt=this.setOptions('formStatusAll','Key','formStatus');
+   crfHTML.addSelectOptions(fgForm.formStatus,selOpt);
+   
+   let uOpt=this.setOptions('users','UserId','DisplayName');
+   crfHTML.addSelectOptions(fgForm.userId,uOpt);
+
+   let siOpt=this.setOptions('siteData','siteNumber','siteName');
+   crfHTML.addSelectOptions(fgForm.site,siOpt);
    //callbacks should be called on copy of this
    let that=this;
    crfHTML.addSelectOptions(fgForm.formSelect,options);
-   fgForm.formSelect.onchange=function(){that.updateIdList(fgForm);};
+   fgForm.formSelect.onchange=function(){that.updateFormList(fgForm);};
+   fgForm.queryId.onchange=function(){that.updateIdList(fgForm);};
    fgForm.crfSelect.onchange=function(){that.updateLabel(fgForm);};
    fgForm.generateButton=this.addInputRow(table,'Generate Form','button');
    fgForm.generateButton.value="Generate Form";
@@ -126,41 +174,54 @@ formGenerator.createFormWithId=
 function(fgForm){
    //get form id and entry id from select and create form as above
    let fName='[createFormWithId]';
-   let ar=roleAndSite.split(':');
-   let role=ar[0];
-   let siteNumber=ar[1];
+   //let ar=roleAndSite.split(':');
+   //let role=ar[0];
+   //let siteNumber=ar[1];
 
    this.print(fName);
    let formId=fgForm.formSelect.options[fgForm.formSelect.selectedIndex].value;
    let crfRef=fgForm.crfSelect.options[fgForm.crfSelect.selectedIndex].text;
-   let configRow=crfSetup.getEntryMap('generateConfigData:formId')[formId];
-   let crfSelectRow=this.getCrfSelectRow(crfRef);
-
-	this.print("Create form w/id "+formId);
+   //let configRow=crfSetup.getEntryMap('generateConfigData:formId')[formId];
+   let crfSelectRow=null;
+   
+   if (crfRef!='Add new'){
+      crfSelectRow=this.getCrfSelectRow(crfRef);
+   }
+	
+   this.print("Create form w/id "+formId);
 	
 	let crfEntry=new Object();
 	crfEntry.entryId=Date.now();
 	crfEntry["Date"]=new Date();
 	crfEntry["View"]="[VIEW]";
-   crfEntry['participantStudyId']=crfSelectRow['participantStudyId'];
-   crfEntry['participantLocalId']=crfSelectRow['participantLocalId'];
+   if (crfSelectRow){
+      crfEntry['participantStudyId']=crfSelectRow['participantStudyId'];
+      crfEntry['participantLocalId']=crfSelectRow['participantLocalId'];
 
-
-	crfEntry.formStatus=configRow['formStatus'];//In progress
+   }
+	
+   let formStatus=fgForm.formStatus.options[fgForm.formStatus.selectedIndex].value;
+   crfEntry.formStatus=formStatus;//In progress
 	//set other variables
 	//requires studyData as part of formConfig
-	let studyData=crfSetup.getRows('studyDataAll')[0];
+	let studyData=crfSetup.getRows('studyData')[0];
    let varRows=crfSetup.getRows('crfStaticVariables');
    for (let i=0;i<varRows.length;i++){
       let varName=varRows[i].staticVariable;
 	   crfEntry[varName]=studyData[varName];
    }
-	crfEntry.UserId=LABKEY.Security.currentUser.id;
-	crfEntry.Site=siteNumber;
+   //crfEntry.UserId=LABKEY.Security.currentUser.id;
+   let userId=fgForm.userId.options[fgForm.userId.selectedIndex].value;
+   crfEntry.UserId=userId;
+
+   let site=fgForm.site.options[fgForm.site.selectedIndex].value;
+	crfEntry.Site=site;
 	this.print("Setting site to id="+crfEntry.Site);
 	//from argument list
 	crfEntry.Form=formId;
-   crfEntry.parentCrf=crfRef;
+   if (crfSelectRow){
+      crfEntry.parentCrf=crfRef;
+   }
   
    //
    //compose a reviewComments entry
@@ -175,34 +236,54 @@ function(fgForm){
       return;
    }
    reviewComment['reviewComment']=fgForm.comment.value;
-   reviewComment['queryName']=configRow['queryId'];
+
+   let queryId=fgForm.queryId.options[fgForm.queryId.selectedIndex].value;
+   reviewComment['queryName']=queryId;
 
    let crfStatus=crfData.createCrfStatus(crfEntry);
-   crfStatus.operator=role;
+   crfStatus.operator='crfManager';
    crfStatus.action='createFormWithId';
+   crfStatus.FormStatus=formStatus;
 
    let that=this;
    let containerPath=crfSetup.getContainer('data');
    let rd=function(data){that.redirect();};
    let pass1=function(data){runQuery.insertRows('lists','crfStatus',[crfStatus],rd,containerPath);};
    let pass=function(data){runQuery.insertRows('lists','reviewComments',[reviewComment],pass1,containerPath);};
-   runQuery.insertRows('lists','crfEntry',crfEntry,pass,containerPath);
+   runQuery.insertRows('lists','crfEntry',[crfEntry],pass,containerPath);
 }
 
 
-formGenerator.updateIdList=
+formGenerator.updateFormList=
 function(fgForm){
    let fName='[updateIdList]';
    let formId=fgForm.formSelect.options[fgForm.formSelect.selectedIndex].value;
    this.print(fName+' id '+formId);
+   let formRows=crfSetup.selectFormSetupRows(formId);
+   let options=new Object();
+   let qMap=crfSetup.getMap('inputLists');
+   for (let i=0;i<formRows.length;i++){
+      let key=formRows[i]['queryName'];
+      let value=qMap[key];
+      options[key]=value;
+   }
+   crfHTML.addSelectOptions(fgForm.queryId,options);
+}
+
+
+
+formGenerator.updateIdList=
+function(fgForm){
    //get query associated with form
-   let configRow=crfSetup.getEntryMap('generateConfigData:formId')[formId];
-   let queryId=configRow['queryId'];
-   this.print(fName+' queryId '+queryId);
+   //let configRow=crfSetup.getEntryMap('generateConfigData:formId')[formId];
+   //let queryId=configRow['queryId'];
+   //this.print(fName+' queryId '+queryId);
+   let queryId=fgForm.queryId.options[fgForm.queryId.selectedIndex].value;
    if (!queryId || queryId<0)
       return;
    let qMap=crfSetup.getMap('inputLists');
    let containerPath=crfSetup.getContainer('data');
+   let that=this;
    let success=function(data){that.updateIdListWithData(fgForm,data);};
    runQuery.selectRows('lists',qMap[queryId],[],success,containerPath);
 }
@@ -215,6 +296,7 @@ function(fgForm,data){
    for (let i=0;i<rows.length;i++){
       options[i]=rows[i]['crfRef'];
    }
+   options[100000]='Add new';
    crfHTML.addSelectOptions(fgForm.crfSelect,options);
    let event=new Event('change');
    fgForm.crfSelect.dispatchEvent(event);
@@ -223,6 +305,8 @@ function(fgForm,data){
 formGenerator.updateLabel=
 function(fgForm){
    let crfRef=fgForm.crfSelect.options[fgForm.crfSelect.selectedIndex].text;
+   if (crfRef=="Add new")
+      return;
    let crfSelectRow=this.getCrfSelectRow(crfRef);
    fgForm.details.innerHTML='Generating for Study:'+crfSelectRow['participantStudyId']+' / Local:'+crfSelectRow['participantLocalId'];
 }