participantIdManager.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398
  1. //all functions are based off of participantManager (print, config, etc.)
  2. var participantIdManager={};
  3. participantIdManager.set=
  4. function(parentClass){
  5. this.parent=parentClass;
  6. }
  7. participantIdManager.addSelectOptions=
  8. function(pM){
  9. if (pM.mode=="LOCAL") return;
  10. let input=this.getInputElement(pM);
  11. let config=this.parent.config;
  12. let fName='addParticipantSelectOptions';
  13. this.parent.print(fName+' input '+input);
  14. let opt=config.document.createElement("option");
  15. opt.value=-1;
  16. opt.text="<Select>";
  17. input.options[input.options.length]=opt;
  18. //here the lookup is being populated (registrationData)
  19. let demoRows=config.formConfig['registrationData'].rows;
  20. this.parent.print(fName+" demoRows: "+demoRows.length);
  21. for (let i=0;i<demoRows.length;i++){
  22. let opt2=config.document.createElement("option");
  23. opt2.value=i+1;
  24. let id=demoRows[i][this.getCrfEntryFieldName(pM)];
  25. let loc=demoRows[i][this.getCrfEntryFieldName(pM,'LOCAL')];
  26. opt2.text=id+' (Local: '+loc+')';
  27. input.options[input.options.length]=opt2;
  28. this.parent.print(fName+' '+pM.participantField+' '+demoRows[i][pM.participantField]);
  29. }
  30. input.selectedIndex=0;
  31. }
  32. participantIdManager.updateElements=
  33. function(pM){
  34. let fName='[updateElements]';
  35. //reset all values (some might be different depending on the call timing)
  36. //selector is with study
  37. pM.cellSelector=this.parent.getElement(pM.cellSelectorId);
  38. pM.inputSelector=this.parent.getElement(pM.inputSelectorId);
  39. pM.textStudy=this.parent.getElement(pM.textStudyId);
  40. this.parent.print(fName+' selector '+pM.inputSelector+' id '+pM.inputSelectorId);
  41. //value is with local
  42. pM.cellValue=this.parent.getElement(pM.cellValueId);
  43. pM.inputValue=this.parent.getElement(pM.inputValueId);
  44. pM.textLocal=this.parent.getElement(pM.textLocalId);
  45. //pM.inputManageLocal=this.parent.getElement(pM.inputManageLocalId);
  46. //pM.inputManageStudy=this.parent.getElement(pM.inputManageStudyId);
  47. }
  48. participantIdManager.generateEntryField=
  49. function(pM){
  50. let fName='[generateParticipantEntryField]:';
  51. this.parent.print(fName);
  52. //this is HTML designator of area on page
  53. let formName=this.parent.config.masterForm;
  54. pM.tb=this.parent.config.document.createElement('table');
  55. let tb=pM.tb;
  56. tb.className='t2';
  57. let row=tb.insertRow();
  58. //label for local ID
  59. let cell=this.parent.config.document.createElement('th');
  60. row.appendChild(cell);
  61. cell.setAttribute("colspan","1");
  62. cell.style.fontSize="20px";
  63. cell.style.textAlign="left";
  64. //Use study coding for participant field
  65. cell.innerText='Local ID';
  66. //cell.innerText=pM.participantField;
  67. //value
  68. let cellValue=row.insertCell();
  69. cellValue.id=pM.cellValueId;
  70. pM.cellManageLocal=row.insertCell();
  71. //second row for study id
  72. let rowStudy=tb.insertRow();
  73. //label for study ID
  74. let cellStudy=this.parent.config.document.createElement('th');
  75. rowStudy.appendChild(cellStudy);
  76. cellStudy.setAttribute("colspan","1");
  77. cellStudy.style.fontSize="20px";
  78. cellStudy.style.textAlign="left";
  79. //Use study coding for participant field
  80. cellStudy.innerText='Study ID';
  81. //selector for study id
  82. let cellSelector=rowStudy.insertCell();
  83. cellSelector.id=pM.cellSelectorId;
  84. //manage
  85. pM.cellManageStudy=rowStudy.insertCell();
  86. this.parent.getElement(formName).appendChild(tb);
  87. this.parent.print(fName+' done');
  88. }
  89. participantIdManager.getMode=
  90. function(pM,mode="NONE"){
  91. if (mode=="NONE") return pM.mode;
  92. return mode;
  93. }
  94. //reslovers which operate depending on mode
  95. participantIdManager.getInputId=
  96. function(pM,mode="NONE"){
  97. let fName='[getInputId]';
  98. this.parent.print(fName);
  99. if (this.getMode(pM,mode)=="LOCAL") return pM.inputValueId;
  100. return pM.inputSelectorId;
  101. }
  102. participantIdManager.getInputCell=
  103. function(pM,mode="NONE"){
  104. let fName='[getInputCell]';
  105. this.parent.print(fName+' mode '+mode+' getMode '+this.getMode(pM,mode));
  106. if (this.getMode(pM,mode)=="LOCAL") return pM.cellValue;
  107. return pM.cellSelector;
  108. }
  109. participantIdManager.getInputElement=
  110. function(pM,mode="NONE"){
  111. let fName='[getInputElement]';
  112. this.parent.print(fName);
  113. let elementType=this.getInputElementType(pM,mode);
  114. let id=this.getInputId(pM,mode);
  115. let cell=this.getInputCell(pM,mode);
  116. let el=this.parent.getElement(id);
  117. this.parent.print(fName+' mode '+this.getMode(pM,mode)+' type '+elementType+' id '+id+' cell '+cell+' el '+el);
  118. if (el) return el;
  119. el=this.parent.config.document.createElement(elementType);
  120. this.parent.print(fName+' input '+el);
  121. el.id=id;
  122. cell.replaceChildren(el);
  123. this.addSelectOptions(pM);
  124. return el;
  125. }
  126. participantIdManager.getInputElementType=
  127. function(pM,mode="NONE"){
  128. let fName='[getInputElementType]';
  129. this.parent.print(fName);
  130. if (this.getMode(pM,mode)=="LOCAL") return "input";
  131. return "select";
  132. }
  133. participantIdManager.getTextFieldId=
  134. function(pM,mode="NONE"){
  135. let fName='[getTextFieldId]';
  136. this.parent.print(fName);
  137. if (this.getMode(pM,mode)=="LOCAL") return pM.textLocalId;
  138. return pM.textStudyId;
  139. }
  140. participantIdManager.getTextElement=
  141. function(pM,mode="NONE"){
  142. let fName='[getTextElement]';
  143. this.parent.print(fName+' mode '+mode);
  144. let id=this.getTextFieldId(pM,mode);
  145. this.parent.print(fName+' id '+id);
  146. let el=this.parent.getElement(id);
  147. this.parent.print(fName+' el '+el);
  148. if (el) return el;
  149. el=this.parent.config.document.createElement("p");
  150. el.id=id;
  151. let cell=this.getInputCell(pM,mode);
  152. //let oldEl=pM.getInputElement(mode);
  153. cell.replaceChildren(el);
  154. return el;
  155. }
  156. //get the button, create if not there yet
  157. participantIdManager.getInputManage=
  158. function(pM,mode="NONE"){
  159. let fName='[getInputManage]';
  160. //this.parent.print(fName);
  161. //this prevents from having two inputs; it is either local or global from the outset
  162. if ("inputManage" in pM) return pM.inputManage;
  163. pM.inputManage=this.parent.config.document.createElement("input");
  164. let inputManage=pM.inputManage;
  165. inputManage.type="button";
  166. let that=this;
  167. inputManage.onclick=function(){that.manageId(pM);};
  168. //inputManageLocal.id=pM.inputManageLocalId;
  169. let cell=pM.cellManageStudy;
  170. if (this.getMode(pM,mode)=="LOCAL") cell=pM.cellManageLocal;
  171. //this.parent.print(fName+' inputManage '+pM.inputManage+' cell '+cell+' mode '+pM.mode);
  172. cell.appendChild(inputManage);
  173. return inputManage;
  174. }
  175. //callback that splits to edit or set/label mode
  176. participantIdManager.manageId=
  177. function(pM){
  178. let fName='[manageId]';
  179. this.parent.print(fName);
  180. //this can happen after object was created, so make sure current
  181. //elements are used
  182. this.updateElements(pM);
  183. let x=this.getInputManage(pM);
  184. if (x.value=="Set"){
  185. this.setId(pM);
  186. return;
  187. }
  188. if (x.value=="Edit"){
  189. this.editId(pM);
  190. return;
  191. }
  192. }
  193. //set mode
  194. participantIdManager.setId=
  195. function(pM){
  196. this.parent.print(fName);
  197. let el=this.getInputElement(pM);
  198. this.parent.print(fName+" value: "+el.value);
  199. let pId=el.value;
  200. let label=pId;
  201. if (pM.mode!="LOCAL"){
  202. if (el.value<0) return;
  203. let opt=el.options[el.selectedIndex];
  204. label=opt.text;
  205. pId=label.replace(/\(.*\)/,'');
  206. label=label.replace(/ \(Local: /,':');
  207. label=label.replace(/\)/,'');
  208. }
  209. this.setParticipantIdToCrfEntry(pM,pId);//no argument (should come from mode)
  210. this.parent.print(fName+" new value "+pId);
  211. this.setLabelMode(pM,label);
  212. pM.updateCrfEntry();
  213. }
  214. participantIdManager.setLabelMode=
  215. function(pM,pId){
  216. let fName='[setLabelMode1]';
  217. this.parent.print(fName+' id '+pId);
  218. ids=pId.split(':');
  219. let textValue=this.getTextElement(pM);
  220. this.parent.print(fName+' textElement '+textValue);
  221. textValue.innerText=ids[0];
  222. if (pM.mode=="STUDY"){
  223. let loc=ids[1];
  224. //pM.getParticipantIdFromCrfEntry('LOCAL');
  225. this.parent.print(fName+' setting local id '+loc);
  226. let tValLocal=this.getTextElement(pM,'LOCAL');
  227. tValLocal.innerText=loc;
  228. this.setParticipantIdToCrfEntry(pM,loc,'LOCAL');
  229. }
  230. let x=this.getInputManage(pM);//getInputManage
  231. if ("readOnly" in pM){
  232. x.style.display="none";
  233. }
  234. x.value="Edit";
  235. }
  236. //edit mode
  237. participantIdManager.editId=
  238. function(pM){
  239. this.setEditMode(pM);
  240. }
  241. participantIdManager.setEditMode=
  242. function(pM){
  243. let fName='[setEditMode1]';
  244. this.parent.print(fName+' pM '+pM+' mode '+pM.mode);
  245. //input
  246. let el=this.getInputElement(pM);
  247. let x=this.getInputManage(pM);
  248. x.value="Set";
  249. }
  250. //manage interaction to storage/CRF and study/LabKey
  251. participantIdManager.getParticipantField=
  252. function(){
  253. return this.parent.config.formConfig['studyDataAll'].rows[0]['SubjectColumnName'];
  254. }
  255. participantIdManager.getCrfEntryFieldName=
  256. function(pM,mode="NONE"){
  257. let variable="Study";
  258. if (mode=="NONE") mode=pM.mode;
  259. if (mode=="LOCAL") variable="Local";
  260. return 'participant'+variable+'Id';
  261. }
  262. participantIdManager.setParticipantIdToCrfEntry=
  263. function(pM,pId,mode="NONE"){
  264. this.parent.config.formConfig.crfEntry[this.getCrfEntryFieldName(pM,mode)]=pId;
  265. }
  266. participantIdManager.getParticipantIdFromCrfEntry=
  267. function(pM,mode="NONE"){
  268. return this.parent.config.formConfig.crfEntry[this.getCrfEntryFieldName(pM,mode)];
  269. }
  270. participantIdManager.verifyCrfStudyId=
  271. function(pM){
  272. //is studyId already set for the crf
  273. let studyId=this.getParticipantIdFromCrfEntry(pM,'STUDY');
  274. if (!studyId) return;
  275. pM.mode="STUDY";
  276. pM.readOnly="TRUE";
  277. }
  278. participantIdManager.verifyRegistration=
  279. function(pM, formConfig){
  280. //if registration is in,
  281. //then local id should not be changed any longer
  282. let idFieldName=this.getCrfEntryFieldName(pM,"STUDY");
  283. //let fQuery=config.formConfig.registrationData;
  284. let fQuery=formConfig.registrationData;
  285. if (fQuery.rows.length==0) return; //registration is empty
  286. let studyId=fQuery.rows[0][idFieldName];
  287. if (!studyId) return; //study id not set
  288. //set
  289. pM.mode="STUDY";
  290. pM.readOnly="TRUE";
  291. //set crf (this happens later, but probably before the form will be corrected)
  292. this.setParticipantIdToCrfEntry(pM,studyId,"STUDY");
  293. pM.updateCrfEntry();
  294. }
  295. //main interface. Use this to generate object and to refer to it later on
  296. participantIdManager.getObject=
  297. function(){
  298. let fName='[getParticipantManagerObject]';
  299. this.parent.print(fName);
  300. let pM=new Object();
  301. //this never change
  302. pM.participantField=this.getParticipantField();
  303. pM.cellSelectorId=pM.participantField+"_cellSelect";
  304. pM.inputSelectorId=pM.participantField+"_Select";
  305. pM.textStudyId=pM.participantField+"_textStudy";
  306. pM.cellValueId=pM.participantField+"_cellValue";
  307. pM.inputValueId=pM.participantField+"_Value";
  308. pM.textLocalId=pM.participantField+"_textLocal";
  309. pM.inputManageLocalId=pM.participantField+"_ManageLocal";
  310. pM.inputManageStudyId=pM.participantField+"_ManageStudy";
  311. pM.mode="LOCAL";//or "STUDY"
  312. //dummy function to be overloaded by calling class
  313. pM.updateCrfEntry=function(){;}
  314. //init
  315. this.generateEntryField(pM);
  316. this.updateElements(pM);
  317. return pM;
  318. }