participantIdManager.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463
  1. //all functions are based off of participantManager (print, config, etc.)
  2. var participantIdManager={};
  3. participantIdManager.print=
  4. function(msg){
  5. console.log(msg);
  6. }
  7. participantIdManager.init=
  8. function(cb=null){
  9. let that=this;
  10. let action=function(){that.afterScripts(cb);};
  11. LABKEY.requiresScript(['crfDORA/crfHTML.js','crfDORA/generateRegistration.js'],action);
  12. }
  13. participantIdManager.afterScripts=
  14. function(cb=null){
  15. crfHTML.init();
  16. generateRegistration.init(cb);
  17. }
  18. participantIdManager.set=
  19. function(setup,data){
  20. this.setup=setup;
  21. this.data=data;
  22. this.updateCrfEntry=function(){;}
  23. this.mode='STUDY';
  24. //this.generateTable();
  25. if (!("readonly" in this)){
  26. this.readonly=new Object();
  27. this.readonly['LOCAL']=false;
  28. this.readonly['STUDY']=false;
  29. }
  30. }
  31. participantIdManager.setMode=
  32. function(mode){
  33. this.mode=mode;
  34. }
  35. participantIdManager.getMode=
  36. function(){
  37. return this.mode;
  38. }
  39. participantIdManager.setReadonly=
  40. function(mode){
  41. if (mode=="LOCAL"){
  42. this.readonly[mode]=true;
  43. return;
  44. }
  45. this.readonly['STUDY']=true;
  46. }
  47. participantIdManager.clearReadonly=
  48. function(mode){
  49. if (mode=='LOCAL'){
  50. this.readonly[mode]=false;
  51. return;
  52. }
  53. this.readonly['STUDY']=false;
  54. }
  55. participantIdManager.isReadonly=
  56. function(mode){
  57. if (mode=='LOCAL') return this.readonly['LOCAL'];
  58. return this.readonly['STUDY'];
  59. }
  60. participantIdManager.addSelectOptions=
  61. function(mode=null){
  62. if (mode=="LOCAL") return;
  63. let input=this.getInputElement(mode);
  64. let fName='addParticipantSelectOptions';
  65. this.print(fName+' input '+input);
  66. //here the lookup is being populated (registrationData)
  67. let demoRows=this.setup.getRows('registrationData');
  68. this.print(fName+" demoRows: "+demoRows.length);
  69. let opts=new Object();
  70. for (let i=0;i<demoRows.length;i++){
  71. opt2[i+1]=this.setup.getParticipantLabel(demoRows[i]);
  72. }
  73. crfHTML.addSelectOptions(input,opts);
  74. }
  75. participantIdManager.insertRow
  76. =function(label){
  77. let row=this.table.insertRow();
  78. //label for local ID
  79. let cell=crfHTML.createTblHeader(null,row);
  80. cell.setAttribute("colspan","1");
  81. cell.style.fontSize="20px";
  82. cell.style.textAlign="left";
  83. cell.innerText=label;
  84. //value
  85. row.insertCell();
  86. //button
  87. row.insertCell();
  88. }
  89. participantIdManager.generateTable=
  90. function(){
  91. let fName='[generateParticipantEntryField]:';
  92. this.print(fName+' site '+this.siteNumber);
  93. //this is HTML designator of area on page
  94. let formName=this.masterForm;
  95. this.print(fName+' master '+formName);
  96. this.table=crfHTML.createTable(formName);
  97. this.table.className='t2';
  98. this.insertRow('Local ID');
  99. this.insertRow('Study ID');
  100. this.insertRow('ID generator');
  101. this.insertRow('Reset local ID');
  102. this.getInputButton('LOCAL');
  103. this.getInputButton('STUDY');
  104. let sMap=this.setup.getEntryMap('siteData:siteNumber');
  105. this.print(fName+' sites: '+Object.keys(sMap).length);
  106. let sMapEntry=sMap[this.siteNumber];
  107. let qPar=new Object();
  108. qPar['queryName']='participantRegistration'+this.siteNumber;
  109. qPar['codeBase']=sMapEntry['codeBase'];
  110. qPar['schemaName']='lists';
  111. qPar['codeField']='registrationCode';
  112. let genObj=generateRegistration.getObject(qPar,'participantIdManager_localInput');
  113. let that=this;
  114. genObj.callback=function(){that.setId('LOCAL');};
  115. //attach execute to a button
  116. let cellButton=this.table.rows[2].cells[2];
  117. cellButton.style.alignItems='center';
  118. this.generateButton=crfHTML.createButton(null,cellButton);
  119. this.generateButton.id="participantIdManager_generateIdButton";
  120. this.generateButton.onclick=function(){generateRegistration.execute(genObj);};
  121. this.generateButton.value="Generate Local ID";
  122. let cellReset=this.table.rows[3].cells[2];
  123. this.resetButton=crfHTML.createButton(null,cellReset);
  124. this.resetButton.id='participantIdManager_resetButton';
  125. this.resetButton.onclick=function(){that.resetLocal();}
  126. this.resetButton.value='Reset Local ID';
  127. this.print(fName+' done');
  128. }
  129. participantIdManager.getRow=
  130. function(mode=null){
  131. let row=this.table.rows[1];
  132. if (mode=="LOCAL")
  133. row=this.table.rows[0];
  134. return row;
  135. }
  136. participantIdManager.getValueCell=
  137. function(mode="NONE"){
  138. return this.getRow(mode).cells[1];
  139. }
  140. participantIdManager.getButtonCell=
  141. function(mode="NONE"){
  142. return this.getRow(mode).cells[2];
  143. }
  144. participantIdManager.getValueElement=
  145. function(mode=null){
  146. return this.getValueCell(mode).firstChild;
  147. }
  148. participantIdManager.getInputElement=
  149. function(mode=null){
  150. let fName='[getInputElement]';
  151. this.print(fName);
  152. let el=this.getValueElement(mode);
  153. if (el) return el;
  154. return this.createInputElement(mode);
  155. }
  156. participantIdManager.createInputElement=
  157. function(mode=null){
  158. let elementType=this.getInputElementType(mode);
  159. let cell=this.getValueCell(mode);
  160. if (elementType=="input") {
  161. el=crfHTML.createTextInput();
  162. el.id='participantIdManager_localInput';
  163. }
  164. if (elementType=="select") el=crfHTML.createSelect(new Object());
  165. this.print(fName+' input '+el);
  166. cell.replaceChildren(el);
  167. this.addSelectOptions(mode);
  168. return el;
  169. }
  170. participantIdManager.getInputElementType=
  171. function(mode=null){
  172. let fName='[getInputElementType]';
  173. this.print(fName);
  174. if (mode=="LOCAL") return "input";
  175. return "select";
  176. }
  177. participantIdManager.getTextElement=
  178. function(mode=null){
  179. let fName='[getTextElement]';
  180. let el=this.getValueElement(mode);
  181. if (el) return el;
  182. return createTextElement(mode);
  183. }
  184. participantIdManager.createTextElement=
  185. function(mode=null){
  186. let el=crfHTML.createParagraph('');
  187. let cell=this.getValueCell(mode);
  188. //let oldEl=pM.getInputElement(mode);
  189. cell.replaceChildren(el);
  190. return el;
  191. }
  192. //get the button, create if not there yet
  193. participantIdManager.getInputButton=
  194. function(mode=null){
  195. let fName='[getInputManage]';
  196. //this.print(fName);
  197. //this prevents from having two inputs; it is either local or global from the outset
  198. let cell=this.getButtonCell(mode);
  199. let el=cell.firstChild;
  200. if (el) return el;
  201. el=crfHTML.createButton(null,cell);
  202. let that=this;
  203. el.onclick=function(){that.manageId(mode);};
  204. return el;
  205. }
  206. //callback that splits to edit or set/label mode
  207. participantIdManager.manageId=
  208. function(mode){
  209. let fName='[manageId]';
  210. this.print(fName);
  211. //this can happen after object was created, so make sure current
  212. //elements are used
  213. //this.updateElements();
  214. let x=this.getInputButton(mode);
  215. if (x.value=="Set"){
  216. this.setId(mode);
  217. return;
  218. }
  219. if (x.value=="Edit"){
  220. this.editId(mode);
  221. return;
  222. }
  223. }
  224. //set mode
  225. participantIdManager.setId=
  226. function(mode=null){
  227. let fName='[setId]';
  228. this.print(fName);
  229. let el=this.getInputElement(mode);
  230. this.print(fName+" value: "+el.value);
  231. let pId=el.value;
  232. let label=pId;
  233. if (mode!="LOCAL"){
  234. //extract from select
  235. if (el.value<0) return;
  236. let opt=el.options[el.selectedIndex];
  237. label=opt.text;
  238. pId=crfSetup.getStudyId(label);
  239. //label=label.replace(/ \(Local: /,':');
  240. //label=label.replace(/\)/,'');
  241. }
  242. this.setParticipantIdToCrfEntry(mode,pId);//no argument (should come from mode)
  243. this.print(fName+" new value "+pId);
  244. this.setLabelMode(mode,label);
  245. this.updateCrfEntry();
  246. }
  247. participantIdManager.setLabelMode=
  248. function(mode,label){
  249. let fName='[setLabelMode1]';
  250. this.print(fName+' id '+label);
  251. //this will give two for study and one entry for local
  252. //let ids=pId.split(':');
  253. let id=label;
  254. if (mode!='LOCAL'){
  255. if (label=='NOT SET')
  256. id=label;
  257. else
  258. id=crfSetup.getStudyId(label);
  259. }
  260. let textValue=this.createTextElement(mode);
  261. this.print(fName+' textElement '+textValue);
  262. textValue.innerText=id;
  263. if (mode!="LOCAL" && label!='NOT SET'){
  264. let loc=crfSetup.getLocalId(label);
  265. this.print(fName+' setting local id '+loc);
  266. if (this.isValid(loc)){
  267. //pM.getParticipantIdFromCrfEntry('LOCAL');
  268. let tValLocal=this.createTextElement('LOCAL');
  269. tValLocal.innerText=loc;
  270. }
  271. //this.setParticipantIdToCrfEntry(pM,loc,'LOCAL');
  272. }
  273. let x=this.getInputButton(mode);//getInputManage
  274. let readonly=this.isReadonly(mode);
  275. if (readonly){
  276. x.style.display="none";
  277. }
  278. x.value="Edit";
  279. if (mode=='LOCAL')
  280. this.generateButton.style.display='none';
  281. }
  282. //edit mode
  283. participantIdManager.editId=
  284. function(mode){
  285. this.setEditMode(mode);
  286. }
  287. participantIdManager.setEditMode=
  288. function(mode){
  289. let fName='[setEditMode1]';
  290. this.print(fName+' pM '+this+' mode '+mode);
  291. //input
  292. let el=this.createInputElement(mode);
  293. let x=this.getInputButton(mode);
  294. x.value="Set";
  295. if (mode=='LOCAL'){
  296. this.generateButton.style.display='block';
  297. x.style.display='block';
  298. }
  299. }
  300. participantIdManager.resetLocal=
  301. function(){
  302. this.clearReadonly('LOCAL');
  303. this.setEditMode('LOCAL');
  304. this.resetButton.style.display='none';
  305. }
  306. //manage interaction to storage/CRF and study/LabKey
  307. participantIdManager.getParticipantField=
  308. function(){
  309. return this.setup.getRows('studyData')[0]['SubjectColumnName'];
  310. }
  311. participantIdManager.getCrfEntryFieldName=
  312. function(mode=null){
  313. if (mode=="LOCAL") return crfSetup.getLocalIdLabel();
  314. return crfSetup.getStudyIdLabel();
  315. }
  316. participantIdManager.setParticipantIdToCrfEntry=
  317. function(mode,pId){
  318. let id=pId;
  319. this.data.getCrfEntry()[this.getCrfEntryFieldName(mode)]=id;
  320. }
  321. participantIdManager.getParticipantIdFromCrfEntry=
  322. function(mode=null){
  323. return this.data.getCrfEntry()[this.getCrfEntryFieldName(mode)];
  324. }
  325. participantIdManager.isValid=
  326. function(name){
  327. if (!name) return false;
  328. if (name=="null") return false;
  329. return true;
  330. }
  331. participantIdManager.verifyCrfStudyId=
  332. function(){
  333. let fName='[verifyCrfStatusId]';
  334. //is studyId already set for the crf
  335. let studyId=this.getParticipantIdFromCrfEntry('STUDY');
  336. let localId=this.getParticipantIdFromCrfEntry('LOCAL');
  337. this.print(fName+' studyId '+studyId+' localId '+localId);
  338. this.resetButton.style.display='none';
  339. if (!studyId) return;
  340. let originalMode=this.getMode();
  341. if (originalMode=='LOCAL' && this.setup.getSettings("allowLocalIdChange")) {
  342. this.resetButton.style.display='block';
  343. }
  344. else{
  345. this.setReadonly('LOCAL');
  346. }
  347. this.setReadonly('STUDY');
  348. this.setMode("STUDY");
  349. this.setLabelMode('STUDY',this.setup.getParticipantLabel(this.data.getCrfEntry()));
  350. //get registration map to fill local id
  351. let that=this;
  352. let completeVerification=function(){that.completeVerification(studyId);}
  353. this.data.setRegistration(completeVerification);
  354. }
  355. participantIdManager.completeVerification=
  356. function(studyId){
  357. let fName='[completeVerification]';
  358. //subject to content of localId
  359. let rMapEntry=this.data.getRegistrationEntryMap(this.getCrfEntryFieldName('STUDY'))[studyId];
  360. //try to set it from registration
  361. let localId=rMapEntry[this.getCrfEntryFieldName('LOCAL')];
  362. this.print(fName+' localId '+localId+' isNull '+(localId==null)+' is"null" '+(localId=="null"));
  363. let localIdFromEntry=this.getParticipantIdFromCrfEntry('LOCAL');
  364. if (localIdFromEntry!=localId){
  365. //update mismatches
  366. this.setParticipantIdToCrfEntry('LOCAL',localId);
  367. this.updateCrfEntry();
  368. }
  369. //ignore the one set in the crfEntry, studyId prevails
  370. //if (!this.isValid(localId)) {
  371. // let localId=this.getParticipantIdFromCrfEntry('LOCAL');
  372. //}
  373. if (this.isValid(localId)) {
  374. this.setReadonly('LOCAL');
  375. this.setLabelMode('LOCAL',localId);
  376. }
  377. else{
  378. this.setEditMode('LOCAL');
  379. }
  380. }