formGenerator.js 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  1. //namespace
  2. var formGenerator={};
  3. formGenerator.print=
  4. function(msg){
  5. console.log(msg);
  6. }
  7. formGenerator.init=
  8. function(cb=null){
  9. let that=this;
  10. let action=function(){that.afterScripts(cb);};
  11. let dependencies=new Array();
  12. dependencies.push("crf/crfHTML.js");
  13. dependencies.push("crf/crfSetup.js");
  14. dependencies.push("crf/crfData.js");
  15. dependencies.push("crf/runQuery.js");
  16. LABKEY.requiresScript(dependencies,action);
  17. }
  18. formGenerator.afterScripts=
  19. function(cb=null){
  20. crfData.setSetup(crfSetup);
  21. let initData=function(){crfData.init(cb);};
  22. crfSetup.init(initData);
  23. }
  24. formGenerator.setRoleAndSite=
  25. function(roleAndSite){
  26. this.roleAndSite=roleAndSite;
  27. }
  28. formGenerator.showFormGenerator=
  29. function(){
  30. if ("table" in this){
  31. this.table.display='block';
  32. return;
  33. }
  34. this.addFormGenerator();
  35. }
  36. formGenerator.hideFormGenerator=
  37. function(){
  38. if (this.table){
  39. this.table.display='none';
  40. }
  41. }
  42. formGenerator.getCrfSelectRow=
  43. function(crfRef){
  44. let rows=this.crfSelectRows;
  45. for (let i=0;i<rows.length;i++){
  46. if (rows[i]['crfRef']==crfRef)
  47. return rows[i];
  48. }
  49. return new Object();
  50. }
  51. formGenerator.setOptions=
  52. function(fieldName,keyField,valueField){
  53. let fName='[setOptions]';
  54. let rows=crfSetup.getRows(fieldName);
  55. //let formRows=crfSetup.getRows('generateConfigData');
  56. let options=new Object();
  57. for (let i=0;i<rows.length;i++){
  58. let id=rows[i][keyField];
  59. let name=rows[i][valueField]
  60. this.print(fName+' '+rows[i][keyField]+'/'+name);
  61. options[id]=name;
  62. //this.addOption(fgForm.formSelect,formName,formId);
  63. }
  64. return options;
  65. }
  66. formGenerator.addGenerator=
  67. function(){
  68. let that=this;
  69. let action=function(){that.addFormGenerator();};
  70. let parseSetup=function(){crfSetup.parseSetup(action);};
  71. crfSetup.setContainers(parseSetup);
  72. }
  73. formGenerator.addFormGenerator=
  74. function(){
  75. //parentClass should provide config and print and getContainer
  76. let fName='[addFormGenerator]';
  77. this.print(fName);
  78. //layout
  79. this.table=crfHTML.createTable('formDiv');
  80. let table=this.table;
  81. table.className="t2";
  82. //this is a form manipulator
  83. let fgForm=new Object();
  84. fgForm.formSelect=this.addInputRow(table,'Select form',"select");
  85. fgForm.queryId=this.addInputRow(table,'Select dataset','select');
  86. fgForm.crfSelect=this.addInputRow(table,'Select CRF',"select");
  87. fgForm.userId=this.addInputRow(table,'Assign to user',"select");
  88. fgForm.site=this.addInputRow(table,'Assign to site',"select");
  89. fgForm.formStatus=this.addInputRow(table,'Select status','select');
  90. fgForm.comment=this.addInputRow(table,'Enter comment','text');
  91. fgForm.details=this.addInputRow(table,'Details','label');
  92. fgForm.warnings=this.addInputRow(table,'Warnings','label');
  93. fgForm.warnings.innerHTML='formGenerator version 3.1.0';
  94. let formRows=crfSetup.getRows('dataForms');
  95. //let formRows=crfSetup.getRows('generateConfigData');
  96. let options=new Object();
  97. for (let i=0;i<formRows.length;i++){
  98. let formId=formRows[i]["Key"];
  99. let formName=formRows[i]['formName']
  100. this.print(fName+' '+formRows[i]["Key"]+'/'+formName);
  101. options[formId]=formName;
  102. //this.addOption(fgForm.formSelect,formName,formId);
  103. }
  104. let selOpt=this.setOptions('formStatusAll','Key','formStatus');
  105. crfHTML.addSelectOptions(fgForm.formStatus,selOpt);
  106. let uOpt=this.setOptions('users','UserId','DisplayName');
  107. crfHTML.addSelectOptions(fgForm.userId,uOpt);
  108. let siOpt=this.setOptions('siteData','siteNumber','siteName');
  109. crfHTML.addSelectOptions(fgForm.site,siOpt);
  110. //callbacks should be called on copy of this
  111. let that=this;
  112. crfHTML.addSelectOptions(fgForm.formSelect,options);
  113. fgForm.formSelect.onchange=function(){that.updateFormList(fgForm);};
  114. fgForm.queryId.onchange=function(){that.updateIdList(fgForm);};
  115. fgForm.crfSelect.onchange=function(){that.updateLabel(fgForm);};
  116. fgForm.generateButton=this.addInputRow(table,'Generate Form','button');
  117. fgForm.generateButton.value="Generate Form";
  118. fgForm.generateButton.onclick=function(){that.createFormWithId(fgForm);};
  119. }
  120. formGenerator.addInputRow=
  121. function(table,header,type){
  122. let fName='[addInputRow]';
  123. this.print(fName);
  124. let row=table.insertRow();
  125. let cell=crfHTML.createTblHeader(null,row);
  126. crfHTML.createTextNode(header,null,cell);
  127. cell=row.insertCell();
  128. let input=null;
  129. if (type=="select")
  130. input=crfHTML.createSelect(new Object(),null,cell);
  131. if (type=="button")
  132. input=crfHTML.createButton(null,cell);
  133. if (type=="text"){
  134. input=crfHTML.createTextArea(null,cell);
  135. input.cols="65";
  136. input.rows="5";
  137. }
  138. if (type=="label")
  139. input=crfHTML.createLabel('Loading',null,cell);
  140. return input;
  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. //let ar=roleAndSite.split(':');
  147. //let role=ar[0];
  148. //let siteNumber=ar[1];
  149. this.print(fName);
  150. let formId=fgForm.formSelect.options[fgForm.formSelect.selectedIndex].value;
  151. let crfRef=fgForm.crfSelect.options[fgForm.crfSelect.selectedIndex].text;
  152. //let configRow=crfSetup.getEntryMap('generateConfigData:formId')[formId];
  153. let crfSelectRow=null;
  154. if (crfRef!='Add new'){
  155. crfSelectRow=this.getCrfSelectRow(crfRef);
  156. }
  157. this.print("Create form w/id "+formId);
  158. let crfEntry=new Object();
  159. crfEntry.entryId=Date.now();
  160. crfEntry["Date"]=new Date();
  161. crfEntry["View"]="[VIEW]";
  162. if (crfSelectRow){
  163. crfEntry['participantStudyId']=crfSelectRow['participantStudyId'];
  164. crfEntry['participantLocalId']=crfSelectRow['participantLocalId'];
  165. }
  166. let formStatus=fgForm.formStatus.options[fgForm.formStatus.selectedIndex].value;
  167. crfEntry.formStatus=formStatus;//In progress
  168. //set other variables
  169. //requires studyData as part of formConfig
  170. let studyData=crfSetup.getRows('studyData')[0];
  171. let varRows=crfSetup.getRows('crfStaticVariables');
  172. for (let i=0;i<varRows.length;i++){
  173. let varName=varRows[i].staticVariable;
  174. crfEntry[varName]=studyData[varName];
  175. }
  176. //crfEntry.UserId=LABKEY.Security.currentUser.id;
  177. let userId=fgForm.userId.options[fgForm.userId.selectedIndex].value;
  178. crfEntry.UserId=userId;
  179. let site=fgForm.site.options[fgForm.site.selectedIndex].value;
  180. crfEntry.Site=site;
  181. this.print("Setting site to id="+crfEntry.Site);
  182. //from argument list
  183. crfEntry.Form=formId;
  184. if (crfSelectRow){
  185. crfEntry.parentCrf=crfRef;
  186. }
  187. //
  188. //compose a reviewComments entry
  189. let reviewComment=new Object();
  190. reviewComment['submissionDate']=crfEntry['Date'];
  191. reviewComment['crfRef']=crfRef;
  192. //comment length
  193. let x=fgForm.comment.value;
  194. this.print(fName+' comment length '+x.length);
  195. if (x.length==0){
  196. fgForm.warnings.innerHTML='Supply a comment';
  197. return;
  198. }
  199. reviewComment['reviewComment']=fgForm.comment.value;
  200. let queryId=fgForm.queryId.options[fgForm.queryId.selectedIndex].value;
  201. reviewComment['queryName']=queryId;
  202. let crfStatus=crfData.createCrfStatus(crfEntry);
  203. crfStatus.operator='crfManager';
  204. crfStatus.action='createFormWithId';
  205. crfStatus.FormStatus=formStatus;
  206. let that=this;
  207. let containerPath=crfSetup.getContainer('data');
  208. let rd=function(data){that.redirect();};
  209. let pass1=function(data){runQuery.insertRows('lists','crfStatus',[crfStatus],rd,containerPath);};
  210. let pass=function(data){runQuery.insertRows('lists','reviewComments',[reviewComment],pass1,containerPath);};
  211. runQuery.insertRows('lists','crfEntry',[crfEntry],pass,containerPath);
  212. }
  213. formGenerator.updateFormList=
  214. function(fgForm){
  215. let fName='[updateIdList]';
  216. let formId=fgForm.formSelect.options[fgForm.formSelect.selectedIndex].value;
  217. this.print(fName+' id '+formId);
  218. let formRows=crfSetup.selectFormSetupRows(formId);
  219. let options=new Object();
  220. let qMap=crfSetup.getMap('inputLists');
  221. for (let i=0;i<formRows.length;i++){
  222. let key=formRows[i]['queryName'];
  223. let value=qMap[key];
  224. options[key]=value;
  225. }
  226. crfHTML.addSelectOptions(fgForm.queryId,options);
  227. }
  228. formGenerator.updateIdList=
  229. function(fgForm){
  230. //get query associated with form
  231. //let configRow=crfSetup.getEntryMap('generateConfigData:formId')[formId];
  232. //let queryId=configRow['queryId'];
  233. //this.print(fName+' queryId '+queryId);
  234. let queryId=fgForm.queryId.options[fgForm.queryId.selectedIndex].value;
  235. if (!queryId || queryId<0)
  236. return;
  237. let qMap=crfSetup.getMap('inputLists');
  238. let containerPath=crfSetup.getContainer('data');
  239. let that=this;
  240. let success=function(data){that.updateIdListWithData(fgForm,data);};
  241. runQuery.selectRows('lists',qMap[queryId],[],success,containerPath);
  242. }
  243. formGenerator.updateIdListWithData=
  244. function(fgForm,data){
  245. let rows=data.rows;
  246. this.crfSelectRows=data.rows;
  247. let options=new Object();
  248. for (let i=0;i<rows.length;i++){
  249. options[i]=rows[i]['crfRef'];
  250. }
  251. options[100000]='Add new';
  252. crfHTML.addSelectOptions(fgForm.crfSelect,options);
  253. let event=new Event('change');
  254. fgForm.crfSelect.dispatchEvent(event);
  255. }
  256. formGenerator.updateLabel=
  257. function(fgForm){
  258. let crfRef=fgForm.crfSelect.options[fgForm.crfSelect.selectedIndex].text;
  259. if (crfRef=="Add new")
  260. return;
  261. let crfSelectRow=this.getCrfSelectRow(crfRef);
  262. fgForm.details.innerHTML='Generating for Study:'+crfSelectRow['participantStudyId']+' / Local:'+crfSelectRow['participantLocalId'];
  263. }
  264. formGenerator.redirect=
  265. function(){
  266. let formUrl="begin";
  267. let params=new Object();
  268. params.name=formUrl;
  269. params.pageId="CRF";
  270. //points to crf container
  271. let containerPath=crfSetup.getContainer('data');
  272. // This changes the page after building the URL.
  273. //Note that the wiki page destination name is set in params.
  274. var homeURL = LABKEY.ActionURL.buildURL(
  275. "project", formUrl , containerPath, params);
  276. this.print("Redirecting to "+homeURL);
  277. window.location = homeURL;
  278. }