Browse Source

Adding callback to generateMasterForm to execute functions upon completed load of a form (for example submit or upload)

Andrej Studen 1 week ago
parent
commit
347d5c066f
1 changed files with 45 additions and 16 deletions
  1. 45 16
      web/crf/crfVisitNew.js

+ 45 - 16
web/crf/crfVisitNew.js

@@ -915,6 +915,28 @@ function(entry,field,setup){
 	this.print('Unknown input type: '+type+'. Ignoring.');
 }
 
+crfVisit.updateSelect=
+function(el,val){
+   //attempt to set element value by comparing value to option.text for el.options
+   //this is assuming el is a select and there is no match for option.value for val
+
+   let fName='[updateSelect]';
+   let eType=el.nodeName.toLowerCase();
+   if (eType!='select'){
+      return;
+   }
+   for (let i=0;i<el.length;i++){
+      let opt=el.options[i];
+      if (opt.text==val){
+         el.value=opt.value;
+         //this.print(fName+' MATCH '+val+' ['+i+']: '+opt.value+'/'+opt.text);
+         break;
+      }
+      //this.print(fName+' '+val+' ['+i+']: '+opt.value+'/'+opt.text);
+
+    }
+}
+
 crfVisit.populateSpecialFields=
 function(entry,field,setup){
    let fName='[populateSpecialFields]';
@@ -933,8 +955,13 @@ function(entry,field,setup){
       let varName=field.name;
       if ("varName" in q)  varName=q["varName"];
       let id=crfData.getCrfEntry()[varName];
+      //what if el is a select -> we should set value to value of the appropriate option
+      //where text matches the variable value
       el.value=id;
-      this.print(fName+' specialFields ['+field.name+'] '+id+'/'+el.value);
+      if (!el.value){
+         this.updateSelect(el,id);
+      }
+      this.print(fName+' specialFields ['+field.name+'] '+id+'/'+el.tagName+'/'+el.value);
    }
 
 
@@ -1692,9 +1719,9 @@ function(){
 
 //master section, entry point from html files
 crfVisit.generateMasterForm=
-function(){
+function(cb=null){
    let that=this;
-   let action=function(){that.setFormConfig();}
+   let action=function(){that.setFormConfig(cb);}
    this.init(action);
 }
 
@@ -1706,7 +1733,7 @@ function(){
 
    let staticData=new Object();
    let titles=new Object();
-   staticData['version']='0.16.5'	
+   staticData['version']='0.16.6'	
    titles['version']='Software version';
    let varRows=crfSetup.getRows('crfStaticVariables');
    for (let i=0;i<varRows.length;i++){
@@ -1785,7 +1812,7 @@ function(){
 	this.print('operator Site: '+operatorSites.length);
 	if (operatorSites.length==0){
       let msg='User '+currentUser.DisplayName;
-      msg+=' is not a '+operator;
+      msg+=' is not a '+this.role;
       this.generateErrorMsg(msg);
       return false;
    }
@@ -1848,7 +1875,7 @@ function(){
 }
 
 crfVisit.afterConfig=
-function(){
+function(cb=null){
    let fName='[afterConfig]';
 	this.print(fName);	
    
@@ -1897,16 +1924,16 @@ function(){
 
 	//here we should get data. For now, just initialize objects that will hold data
    let that=this;
-   let action=function(){that.afterDataLayout();};
+   let action=function(){that.afterDataLayout(cb);};
    let formId=crfData.getCrfEntry()['Form'];
 	crfData.setDataLayout(formId,this.role,action);//callback is afterDataLayout
 }
 
 crfVisit.afterDataLayout=
-function(){
+function(cb=null){
 
    let that=this;
-   let action=function(){that.afterData();};
+   let action=function(){that.afterData(cb);};
    //let action=function(){that.doNothing();};
 	crfData.setData(crfData.getCrfRefForData(),action);//callback is afterData
 }
@@ -1954,10 +1981,12 @@ function(cb=null,data){
 }
 
 crfVisit.afterData=
-function(){
+function(cb=null){
 	let fName='afterData';
    this.configureIdManager();
    this.generateSections();
+   if(cb)
+      cb();
 }
 
 
@@ -2311,17 +2340,17 @@ function(formId,queryName,cb){
 
 //entry point from generateMasterForm
 crfVisit.setFormConfig=
-function(){
+function(cb=null){
    let fName="[setFormConfig]";
    let crfRef=this.crfRef;
    let that=this;
-   let afterCrfEntry=function(){that.afterCrfEntry();};
+   let afterCrfEntry=function(){that.afterCrfEntry(cb);};
    let action=function(){crfData.setCrfEntry(crfRef,afterCrfEntry);};//afterCrfEntry
    crfSetup.setContainers(action);
 }
 
 crfVisit.afterCrfEntry=
-function(){
+function(cb=null){
    let fName='[afterCRFEntry]';
 	this.print(fName+" setting crfEntry (x) to "+crfData.getCrfEntry()["entryId"]);
 	//for empty records or those with parentCrf not set, parentCrf comes up as null
@@ -2331,7 +2360,7 @@ function(){
 	this.print(fName+' parentCrf set to '+parentCrf);
    if (parentCrf) crfSetup.parentCrf=parentCrf;
    let that=this;
-   let action=function(){that.parseSetup();};
+   let action=function(){that.parseSetup(cb);};
    crfSetup.parseSetup(action);
 }
 
@@ -2352,7 +2381,7 @@ function(formStatus){
 }
 
 crfVisit.parseSetup=
-function(){
+function(cb=null){
 
    //debug
    let fName='[parseSetup]';
@@ -2410,7 +2439,7 @@ function(){
 
    crfSetup.setAdditionalData(this.crfRef,formId);
 	
-	this.afterConfig();
+	this.afterConfig(cb);
 
 }