123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335 |
- //namespace
- var formGenerator={};
- formGenerator.print=
- function(msg){
- console.log(msg);
- }
- formGenerator.init=
- function(cb=null){
- let that=this;
- let action=function(){that.afterScripts(cb);};
- let dependencies=new Array();
- dependencies.push("crf/crfHTML.js");
- dependencies.push("crf/crfSetup.js");
- dependencies.push("crf/crfData.js");
- dependencies.push("crf/runQuery.js");
- LABKEY.requiresScript(dependencies,action);
- }
- formGenerator.afterScripts=
- function(cb=null){
- crfData.setSetup(crfSetup);
- let initData=function(){crfData.init(cb);};
- crfSetup.init(initData);
- }
- formGenerator.setRoleAndSite=
- function(roleAndSite){
- this.roleAndSite=roleAndSite;
- }
- formGenerator.showFormGenerator=
- function(){
- if ("table" in this){
- this.table.display='block';
- return;
- }
- this.addFormGenerator();
- }
- formGenerator.hideFormGenerator=
- function(){
- if (this.table){
- this.table.display='none';
- }
- }
- formGenerator.getCrfSelectRow=
- function(crfRef){
- let rows=this.crfSelectRows;
- for (let i=0;i<rows.length;i++){
- if (rows[i]['crfRef']==crfRef)
- return rows[i];
- }
- 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
-
- let fName='[addFormGenerator]';
- 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('dataForms');
- //let formRows=crfSetup.getRows('generateConfigData');
- let options=new Object();
- for (let i=0;i<formRows.length;i++){
- 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.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";
- fgForm.generateButton.onclick=function(){that.createFormWithId(fgForm);};
-
- }
- formGenerator.addInputRow=
- function(table,header,type){
- let fName='[addInputRow]';
- this.print(fName);
- let row=table.insertRow();
- let cell=crfHTML.createTblHeader(null,row);
- crfHTML.createTextNode(header,null,cell);
- cell=row.insertCell();
- let input=null;
-
- if (type=="select")
- input=crfHTML.createSelect(new Object(),null,cell);
- if (type=="button")
- input=crfHTML.createButton(null,cell);
- if (type=="text"){
- input=crfHTML.createTextArea(null,cell);
- input.cols="65";
- input.rows="5";
- }
- if (type=="label")
- input=crfHTML.createLabel('Loading',null,cell);
- return input;
- }
- 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];
- 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=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]";
- if (crfSelectRow){
- crfEntry['participantStudyId']=crfSelectRow['participantStudyId'];
- crfEntry['participantLocalId']=crfSelectRow['participantLocalId'];
- }
-
- 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('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;
- 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;
- if (crfSelectRow){
- crfEntry.parentCrf=crfRef;
- }
-
- //
- //compose a reviewComments entry
- let reviewComment=new Object();
- reviewComment['submissionDate']=crfEntry['Date'];
- reviewComment['crfRef']=crfRef;
- //comment length
- let x=fgForm.comment.value;
- this.print(fName+' comment length '+x.length);
- if (x.length==0){
- fgForm.warnings.innerHTML='Supply a comment';
- return;
- }
- reviewComment['reviewComment']=fgForm.comment.value;
- let queryId=fgForm.queryId.options[fgForm.queryId.selectedIndex].value;
- reviewComment['queryName']=queryId;
- let crfStatus=crfData.createCrfStatus(crfEntry);
- 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);
- }
- 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 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);
- }
- formGenerator.updateIdListWithData=
- function(fgForm,data){
- let rows=data.rows;
- this.crfSelectRows=data.rows;
- let options=new Object();
- 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);
- }
- 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'];
- }
- formGenerator.redirect=
- function(){
- let formUrl="begin";
- let params=new Object();
- params.name=formUrl;
- params.pageId="CRF";
- //points to crf container
- let containerPath=crfSetup.getContainer('data');
-
- // This changes the page after building the URL.
- //Note that the wiki page destination name is set in params.
-
- var homeURL = LABKEY.ActionURL.buildURL(
- "project", formUrl , containerPath, params);
- this.print("Redirecting to "+homeURL);
- window.location = homeURL;
-
- }
|