formGenerator.js 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  1. //namespace
  2. var formGenerator={};
  3. formGenerator.set=
  4. function(parentClass){
  5. this.parent=parentClass;
  6. }
  7. formGenerator.addFormGenerator=
  8. function(){
  9. //parentClass should provide config and print and getContainer
  10. let config=this.parent.config;
  11. let fName='[addFormGenerator]';
  12. this.parent.print(fName);
  13. //layout
  14. let table=config.document.createElement("table");
  15. table.className="t2";
  16. config.document.getElementById(config.div).appendChild(table);
  17. //this is a form manipulator
  18. let fgForm=new Object();
  19. fgForm.formSelect=this.addInputRow(table,'Select form',"select");
  20. fgForm.crfSelect=this.addInputRow(table,'Select CRF',"select");
  21. fgForm.comment=this.addInputRow(table,'Enter comment','text');
  22. fgForm.details=this.addInputRow(table,'Details','label');
  23. fgForm.warnings=this.addInputRow(table,'Warnings','label');
  24. fgForm.warnings.innerHTML='formGenerator version 2.1.0';
  25. this.addOption(fgForm.formSelect,'<Select>',-1);
  26. let formRows=config.formConfig.generateConfigData.rows;
  27. for (let i=0;i<formRows.length;i++){
  28. let formId=formRows[i]["formId"];
  29. let formName=this.getFormName(formId);
  30. this.parent.print(fName+' '+formRows[i]["formId"]+'/'+formName);
  31. this.addOption(fgForm.formSelect,formName,formId);
  32. }
  33. //callbacks should be called on copy of this
  34. let that=this;
  35. fgForm.formSelect.onchange=function(){that.updateIdList(fgForm);};
  36. fgForm.crfSelect.onchange=function(){that.updateLabel(fgForm);};
  37. fgForm.generateButton=this.addInputRow(table,'Generate Form','button');
  38. fgForm.generateButton.value="Generate Form";
  39. fgForm.generateButton.onclick=function(){that.createFormWithId(fgForm);};
  40. }
  41. formGenerator.insertRow=
  42. function(schemaName,queryName,row,cb=null,containerPath=null){
  43. let fName='[fgInsertRow]';
  44. this.parent.print(fName);
  45. //cb=function(data){....}
  46. let qconfig=new Object();
  47. if (containerPath)
  48. qconfig.containerPath=containerPath;
  49. qconfig.schemaName=schemaName;
  50. qconfig.queryName=queryName;
  51. qconfig.success=function(data){;};
  52. if (cb) qconfig.success=cb;
  53. qconfig.rows=[row];
  54. this.parent.print(fName+' qconfig '+qconfig);
  55. LABKEY.Query.insertRows(qconfig);
  56. }
  57. formGenerator.addInputRow=
  58. function(table,header,type){
  59. let config=this.parent.config;
  60. let fName='[addInputRow]';
  61. this.parent.print(fName);
  62. let row=table.insertRow();
  63. let cell=config.document.createElement('th');
  64. let text=config.document.createTextNode(header);
  65. cell.appendChild(text);
  66. row.appendChild(cell);
  67. let input=null;
  68. if (type=="select")
  69. input=config.document.createElement(type);
  70. if (type=="button"){
  71. input=config.document.createElement("input");
  72. input.type="button";
  73. }
  74. if (type=="text"){
  75. input=config.document.createElement('textarea');
  76. input.cols="65";
  77. input.rows="5";
  78. }
  79. if (type=="label")
  80. input=config.document.createElement(type);
  81. let cell1=row.insertCell();
  82. cell1.appendChild(input);
  83. return input;
  84. }
  85. formGenerator.getFormName=
  86. function(formId){
  87. let config=this.parent.config;
  88. let rows=config.formConfig.dataForms.rows;
  89. for (let i=0;i<rows.length;i++){
  90. if (rows[i]['Key']==formId){
  91. return rows[i]['formName'];
  92. }
  93. }
  94. return "NONE";
  95. }
  96. formGenerator.getQueryName=
  97. function(queryId){
  98. let config=this.parent.config;
  99. let rows=config.formConfig.inputLists.rows;
  100. for (let i=0;i<rows.length;i++){
  101. if (rows[i]['Key']==queryId){
  102. return rows[i]['queryName'];
  103. }
  104. }
  105. return "NONE";
  106. }
  107. formGenerator.getGCRow=
  108. function(formId){
  109. let config=this.parent.config;
  110. let formRows=config.formConfig.generateConfigData.rows;
  111. for (let i=0;i<formRows.length;i++){
  112. if (formRows[i]['formId']==formId){
  113. return formRows[i];
  114. }
  115. }
  116. return Object();
  117. }
  118. formGenerator.getCrfSelectRow=
  119. function(crfRef){
  120. let config=this.parent.config;
  121. let rows=config.formConfig.crfSelectRows;
  122. for (let i=0;i<rows.length;i++){
  123. if (rows[i]['crfRef']==crfRef)
  124. return rows[i];
  125. }
  126. return Object();
  127. }
  128. formGenerator.addOption=
  129. function(input,name,value){
  130. let config=this.parent.config;
  131. let opt=config.document.createElement("option");
  132. opt.text=name;
  133. opt.value=value;
  134. input.options[input.options.length]=opt;
  135. }
  136. formGenerator.clearOptions=
  137. function(input){
  138. for(let i = input.options.length; i >= 0; i--) {
  139. input.remove(i);
  140. }
  141. }
  142. formGenerator.createFormWithId=
  143. function(fgForm){
  144. //get form id and entry id from select and create form as above
  145. let fName='[createFormWithId]';
  146. this.parent.print(fName);
  147. let config=this.parent.config;
  148. let formId=fgForm.formSelect.options[fgForm.formSelect.selectedIndex].value;
  149. let crfRef=fgForm.crfSelect.options[fgForm.crfSelect.selectedIndex].text;
  150. let configRow=this.getGCRow(formId);
  151. let crfSelectRow=this.getCrfSelectRow(crfRef);
  152. let formConfig=config.formConfig;
  153. this.parent.print("Create form w/id "+formId);
  154. let crfEntry=new Object();
  155. crfEntry.entryId=Date.now();
  156. crfEntry["Date"]=new Date();
  157. crfEntry["View"]="[VIEW]";
  158. crfEntry['participantStudyId']=crfSelectRow['participantStudyId'];
  159. crfEntry['participantLocalId']=crfSelectRow['participantLocalId'];
  160. crfEntry.formStatus=configRow['formStatus'];//In progress
  161. //set other variables
  162. //requires studyData as part of formConfig
  163. let studyData=formConfig.studyData.rows[0];
  164. let varRows=formConfig['crfStaticVariables'].rows;
  165. for (let i=0;i<varRows.length;i++){
  166. let varName=varRows[i].staticVariable;
  167. crfEntry[varName]=studyData[varName];
  168. }
  169. crfEntry.UserId=LABKEY.Security.currentUser.id;
  170. crfEntry.Site=config.formConfig.currentSites[0].siteNumber;
  171. this.parent.print("Setting site to id="+crfEntry.Site);
  172. //from argument list
  173. crfEntry.Form=formId;
  174. crfEntry.parentCrf=crfRef;
  175. //
  176. //compose a reviewComments entry
  177. let reviewComment=new Object();
  178. reviewComment['submissionDate']=crfEntry['Date'];
  179. reviewComment['crfRef']=crfRef;
  180. //comment length
  181. let x=fgForm.comment.value;
  182. this.parent.print(fName+' comment length '+x.length);
  183. if (x.length==0){
  184. fgForm.warnings.innerHTML='Supply a comment';
  185. return;
  186. }
  187. reviewComment['reviewComment']=fgForm.comment.value;
  188. reviewComment['queryName']=configRow['queryId'];
  189. let crfStatus=new Object();
  190. crfStatus.entryId=crfEntry.entryId;
  191. crfStatus.submissionDate=new Date();
  192. crfStatus.FormStatus=crfEntry.formStatus;
  193. crfStatus.User=crfEntry.UserId;
  194. crfStatus.Form=crfEntry.Form;
  195. crfStatus.operator=config.role;
  196. crfStatus.action='createFormWithId';
  197. let that=this;
  198. let containerPath=this.parent.getContainer('data');
  199. let rd=function(data){that.redirect();};
  200. let pass1=function(data){that.insertRow('lists','crfStatus',crfStatus,rd,containerPath);};
  201. let pass=function(data){that.insertRow('lists','reviewComments',reviewComment,pass1,containerPath);};
  202. this.insertRow('lists','crfEntry',crfEntry,pass,this.parent.getContainer('data'));
  203. }
  204. formGenerator.updateIdList=
  205. function(fgForm){
  206. let fName='[updateIdList]';
  207. let formId=fgForm.formSelect.options[fgForm.formSelect.selectedIndex].value;
  208. this.parent.print(fName+' id '+formId);
  209. //remove old options
  210. this.clearOptions(fgForm.crfSelect);
  211. this.parent.print(fName+' options cleared');
  212. //get query associated with form
  213. let configRow=this.getGCRow(formId);
  214. let queryId=configRow['queryId'];
  215. this.parent.print(fName+' queryId '+queryId);
  216. if (!queryId || queryId<0)
  217. return;
  218. let qselect=new Object();
  219. qselect.containerPath=this.parent.getContainer('data');
  220. qselect.schemaName='lists';
  221. qselect.queryName=this.getQueryName(queryId);
  222. let that=this;
  223. qselect.success=function(data){that.updateIdListWithData(fgForm,data);};
  224. LABKEY.Query.selectRows(qselect);
  225. }
  226. formGenerator.updateIdListWithData=
  227. function(fgForm,data){
  228. let config=this.parent.config;
  229. let rows=data.rows;
  230. config.formConfig.crfSelectRows=data.rows;
  231. for (let i=0;i<rows.length;i++){
  232. this.addOption(fgForm.crfSelect,rows[i]['crfRef'],i);
  233. }
  234. let event=new Event('change');
  235. fgForm.crfSelect.dispatchEvent(event);
  236. }
  237. formGenerator.updateLabel=
  238. function(fgForm){
  239. let crfRef=fgForm.crfSelect.options[fgForm.crfSelect.selectedIndex].text;
  240. let crfSelectRow=this.getCrfSelectRow(crfRef);
  241. fgForm.details.innerHTML='Generating for Study:'+crfSelectRow['participantStudyId']+' / Local:'+crfSelectRow['participantLocalId'];
  242. }
  243. formGenerator.redirect=
  244. function(){
  245. let debug=false;
  246. let formUrl="begin";
  247. let params=new Object();
  248. params.name=formUrl;
  249. params.pageId="CRF";
  250. //points to crf container
  251. let containerPath=this.parent.getContainer('data');
  252. // This changes the page after building the URL.
  253. //Note that the wiki page destination name is set in params.
  254. var homeURL = LABKEY.ActionURL.buildURL(
  255. "project", formUrl , containerPath, params);
  256. this.parent.print("Redirecting to "+homeURL);
  257. if (debug) return;
  258. window.location = homeURL;
  259. }