formGenerator.js 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. //namespace
  2. var formGenerator={};
  3. formGenerator.init=
  4. function(cb=null){
  5. let that=this;
  6. let action=function(){that.afterScripts(cb);};
  7. let dependencies=new Array();
  8. dependencies.push("crf/crfHTML.js");
  9. dependencies.push("crf/crfSetup.js");
  10. dependencies.push("crf/crfData.js");
  11. dependencies.push("crf/runQuery.js");
  12. LABKEY.requiresScript(dependencies,action);
  13. }
  14. formGenerator.afterScripts=
  15. function(cb=null){
  16. crfData.setSetup(crfSetup);
  17. let initData=function(){crfData.init(cb);};
  18. crfSetup.init(initData);
  19. }
  20. formGenerator.setRoleAndSite=
  21. function(roleAndSite){
  22. this.roleAndSite=roleAndSite;
  23. }
  24. formGenerator.showFormGenerator=
  25. function(){
  26. if ("table" in this){
  27. this.table.display='block';
  28. return;
  29. }
  30. addFormGenerator();
  31. }
  32. formGenerator.hideFormGenerator=
  33. function(){
  34. if (this.table){
  35. this.table.display='none';
  36. }
  37. }
  38. formGenerator.getCrfSelectRow=
  39. function(crfRef){
  40. let rows=this.crfSelectRows;
  41. for (let i=0;i<rows.length;i++){
  42. if (rows[i]['crfRef']==crfRef)
  43. return rows[i];
  44. }
  45. return new Object();
  46. }
  47. formGenerator.addFormGenerator=
  48. function(){
  49. //parentClass should provide config and print and getContainer
  50. let fName='[addFormGenerator]';
  51. this.print(fName);
  52. //layout
  53. this.table=crfHTML.createTable('formDiv');
  54. table.className="t2";
  55. //this is a form manipulator
  56. let fgForm=new Object();
  57. fgForm.formSelect=this.addInputRow(table,'Select form',"select");
  58. fgForm.crfSelect=this.addInputRow(table,'Select CRF',"select");
  59. fgForm.comment=this.addInputRow(table,'Enter comment','text');
  60. fgForm.details=this.addInputRow(table,'Details','label');
  61. fgForm.warnings=this.addInputRow(table,'Warnings','label');
  62. fgForm.warnings.innerHTML='formGenerator version 3.1.0';
  63. let formRows=crfSetup.getRows('generateConfigData');
  64. let options=new Object();
  65. for (let i=0;i<formRows.length;i++){
  66. let formId=formRows[i]["formId"];
  67. let formName=this.getFormName(formId);
  68. this.print(fName+' '+formRows[i]["formId"]+'/'+formName);
  69. options[formId]=formName;
  70. //this.addOption(fgForm.formSelect,formName,formId);
  71. }
  72. //callbacks should be called on copy of this
  73. let that=this;
  74. crfHTML.addSelectOptions(fgForm.formSelect,options);
  75. fgForm.formSelect.onchange=function(){that.updateIdList(fgForm);};
  76. fgForm.crfSelect.onchange=function(){that.updateLabel(fgForm);};
  77. fgForm.generateButton=this.addInputRow(table,'Generate Form','button');
  78. fgForm.generateButton.value="Generate Form";
  79. fgForm.generateButton.onclick=function(){that.createFormWithId(fgForm);};
  80. }
  81. formGenerator.addInputRow=
  82. function(table,header,type){
  83. let fName='[addInputRow]';
  84. this.print(fName);
  85. let row=table.insertRow();
  86. let cell=crfHTML.createTblHeader(null,row);
  87. crfHTML.createTextNode(header,null,cell);
  88. cell=row.insertCell();
  89. let input=null;
  90. if (type=="select")
  91. input=crfHTML.createSelect(new Object(),null,cell);
  92. if (type=="button")
  93. input=crfHTML.createButton(null,cell);
  94. if (type=="text"){
  95. input=crfHTML.createTextArea(null,cell);
  96. input.cols="65";
  97. input.rows="5";
  98. }
  99. if (type=="label")
  100. input=crfHTML.createLabel('Loading',null,cell);
  101. return input;
  102. }
  103. formGenerator.createFormWithId=
  104. function(fgForm){
  105. //get form id and entry id from select and create form as above
  106. let fName='[createFormWithId]';
  107. let ar=roleAndSite.split(':');
  108. let role=ar[0];
  109. let siteNumber=ar[1];
  110. this.print(fName);
  111. let formId=fgForm.formSelect.options[fgForm.formSelect.selectedIndex].value;
  112. let crfRef=fgForm.crfSelect.options[fgForm.crfSelect.selectedIndex].text;
  113. let configRow=crfSetup.getEntryMap('generateConfigData:formId')[formId];
  114. let crfSelectRow=this.getCrfSelectRow(crfRef);
  115. this.print("Create form w/id "+formId);
  116. let crfEntry=new Object();
  117. crfEntry.entryId=Date.now();
  118. crfEntry["Date"]=new Date();
  119. crfEntry["View"]="[VIEW]";
  120. crfEntry['participantStudyId']=crfSelectRow['participantStudyId'];
  121. crfEntry['participantLocalId']=crfSelectRow['participantLocalId'];
  122. crfEntry.formStatus=configRow['formStatus'];//In progress
  123. //set other variables
  124. //requires studyData as part of formConfig
  125. let studyData=crfSetup.getRows('studyDataAll')[0];
  126. let varRows=crfSetup.getRows('crfStaticVariables');
  127. for (let i=0;i<varRows.length;i++){
  128. let varName=varRows[i].staticVariable;
  129. crfEntry[varName]=studyData[varName];
  130. }
  131. crfEntry.UserId=LABKEY.Security.currentUser.id;
  132. crfEntry.Site=siteNumber;
  133. this.print("Setting site to id="+crfEntry.Site);
  134. //from argument list
  135. crfEntry.Form=formId;
  136. crfEntry.parentCrf=crfRef;
  137. //
  138. //compose a reviewComments entry
  139. let reviewComment=new Object();
  140. reviewComment['submissionDate']=crfEntry['Date'];
  141. reviewComment['crfRef']=crfRef;
  142. //comment length
  143. let x=fgForm.comment.value;
  144. this.print(fName+' comment length '+x.length);
  145. if (x.length==0){
  146. fgForm.warnings.innerHTML='Supply a comment';
  147. return;
  148. }
  149. reviewComment['reviewComment']=fgForm.comment.value;
  150. reviewComment['queryName']=configRow['queryId'];
  151. let crfStatus=crfData.createCrfStatus(crfEntry);
  152. crfStatus.operator=role;
  153. crfStatus.action='createFormWithId';
  154. let that=this;
  155. let containerPath=crfSetup.getContainer('data');
  156. let rd=function(data){that.redirect();};
  157. let pass1=function(data){runQuery.insertRows('lists','crfStatus',[crfStatus],rd,containerPath);};
  158. let pass=function(data){runQuery.insertRows('lists','reviewComments',[reviewComment],pass1,containerPath);};
  159. runQuery.insertRows('lists','crfEntry',crfEntry,pass,containerPath);
  160. }
  161. formGenerator.updateIdList=
  162. function(fgForm){
  163. let fName='[updateIdList]';
  164. let formId=fgForm.formSelect.options[fgForm.formSelect.selectedIndex].value;
  165. this.print(fName+' id '+formId);
  166. //get query associated with form
  167. let configRow=crfSetup.getEntryMap('generateConfigData:formId')[formId];
  168. let queryId=configRow['queryId'];
  169. this.print(fName+' queryId '+queryId);
  170. if (!queryId || queryId<0)
  171. return;
  172. let qMap=crfSetup.getMap('inputLists');
  173. let containerPath=crfSetup.getContainer('data');
  174. let success=function(data){that.updateIdListWithData(fgForm,data);};
  175. runQuery.selectRows('lists',qMap[queryId],[],success,containerPath);
  176. }
  177. formGenerator.updateIdListWithData=
  178. function(fgForm,data){
  179. let rows=data.rows;
  180. this.crfSelectRows=data.rows;
  181. let options=new Object();
  182. for (let i=0;i<rows.length;i++){
  183. options[i]=rows[i]['crfRef'];
  184. }
  185. crfHTML.addSelectOptions(fgForm.crfSelect,options);
  186. let event=new Event('change');
  187. fgForm.crfSelect.dispatchEvent(event);
  188. }
  189. formGenerator.updateLabel=
  190. function(fgForm){
  191. let crfRef=fgForm.crfSelect.options[fgForm.crfSelect.selectedIndex].text;
  192. let crfSelectRow=this.getCrfSelectRow(crfRef);
  193. fgForm.details.innerHTML='Generating for Study:'+crfSelectRow['participantStudyId']+' / Local:'+crfSelectRow['participantLocalId'];
  194. }
  195. formGenerator.redirect=
  196. function(){
  197. let formUrl="begin";
  198. let params=new Object();
  199. params.name=formUrl;
  200. params.pageId="CRF";
  201. //points to crf container
  202. let containerPath=crfSetup.getContainer('data');
  203. // This changes the page after building the URL.
  204. //Note that the wiki page destination name is set in params.
  205. var homeURL = LABKEY.ActionURL.buildURL(
  206. "project", formUrl , containerPath, params);
  207. this.print("Redirecting to "+homeURL);
  208. window.location = homeURL;
  209. }