participantIdManager.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462
  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(['crf/crfHTML.js','crf/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);
  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. let sMapEntry=sMap[this.siteNumber];
  106. let qPar=new Object();
  107. qPar['queryName']='participantRegistration'+this.siteNumber;
  108. qPar['codeBase']=sMapEntry['codeBase'];
  109. qPar['schemaName']='lists';
  110. qPar['codeField']='registrationCode';
  111. let genObj=generateRegistration.getObject(qPar,'participantIdManager_localInput');
  112. let that=this;
  113. genObj.callback=function(){that.setId('LOCAL');};
  114. //attach execute to a button
  115. let cellButton=this.table.rows[2].cells[2];
  116. cellButton.style.alignItems='center';
  117. this.generateButton=crfHTML.createButton(null,cellButton);
  118. this.generateButton.id="participantIdManager_generateIdButton";
  119. this.generateButton.onclick=function(){generateRegistration.execute(genObj);};
  120. this.generateButton.value="Generate Local ID";
  121. let cellReset=this.table.rows[3].cells[2];
  122. this.resetButton=crfHTML.createButton(null,cellReset);
  123. this.resetButton.id='participantIdManager_resetButton';
  124. this.resetButton.onclick=function(){that.resetLocal();}
  125. this.resetButton.value='Reset Local ID';
  126. this.print(fName+' done');
  127. }
  128. participantIdManager.getRow=
  129. function(mode=null){
  130. let row=this.table.rows[1];
  131. if (mode=="LOCAL")
  132. row=this.table.rows[0];
  133. return row;
  134. }
  135. participantIdManager.getValueCell=
  136. function(mode="NONE"){
  137. return this.getRow(mode).cells[1];
  138. }
  139. participantIdManager.getButtonCell=
  140. function(mode="NONE"){
  141. return this.getRow(mode).cells[2];
  142. }
  143. participantIdManager.getValueElement=
  144. function(mode=null){
  145. return this.getValueCell(mode).firstChild;
  146. }
  147. participantIdManager.getInputElement=
  148. function(mode=null){
  149. let fName='[getInputElement]';
  150. this.print(fName);
  151. let el=this.getValueElement(mode);
  152. if (el) return el;
  153. return this.createInputElement(mode);
  154. }
  155. participantIdManager.createInputElement=
  156. function(mode=null){
  157. let elementType=this.getInputElementType(mode);
  158. let cell=this.getValueCell(mode);
  159. if (elementType=="input") {
  160. el=crfHTML.createTextInput();
  161. el.id='participantIdManager_localInput';
  162. }
  163. if (elementType=="select") el=crfHTML.createSelect(new Object());
  164. this.print(fName+' input '+el);
  165. cell.replaceChildren(el);
  166. this.addSelectOptions(mode);
  167. return el;
  168. }
  169. participantIdManager.getInputElementType=
  170. function(mode=null){
  171. let fName='[getInputElementType]';
  172. this.print(fName);
  173. if (mode=="LOCAL") return "input";
  174. return "select";
  175. }
  176. participantIdManager.getTextElement=
  177. function(mode=null){
  178. let fName='[getTextElement]';
  179. let el=this.getValueElement(mode);
  180. if (el) return el;
  181. return createTextElement(mode);
  182. }
  183. participantIdManager.createTextElement=
  184. function(mode=null){
  185. let el=crfHTML.createParagraph('');
  186. let cell=this.getValueCell(mode);
  187. //let oldEl=pM.getInputElement(mode);
  188. cell.replaceChildren(el);
  189. return el;
  190. }
  191. //get the button, create if not there yet
  192. participantIdManager.getInputButton=
  193. function(mode=null){
  194. let fName='[getInputManage]';
  195. //this.print(fName);
  196. //this prevents from having two inputs; it is either local or global from the outset
  197. let cell=this.getButtonCell(mode);
  198. let el=cell.firstChild;
  199. if (el) return el;
  200. el=crfHTML.createButton(null,cell);
  201. let that=this;
  202. el.onclick=function(){that.manageId(mode);};
  203. return el;
  204. }
  205. //callback that splits to edit or set/label mode
  206. participantIdManager.manageId=
  207. function(mode){
  208. let fName='[manageId]';
  209. this.print(fName);
  210. //this can happen after object was created, so make sure current
  211. //elements are used
  212. //this.updateElements();
  213. let x=this.getInputButton(mode);
  214. if (x.value=="Set"){
  215. this.setId(mode);
  216. return;
  217. }
  218. if (x.value=="Edit"){
  219. this.editId(mode);
  220. return;
  221. }
  222. }
  223. //set mode
  224. participantIdManager.setId=
  225. function(mode=null){
  226. let fName='[setId]';
  227. this.print(fName);
  228. let el=this.getInputElement(mode);
  229. this.print(fName+" value: "+el.value);
  230. let pId=el.value;
  231. let label=pId;
  232. if (mode!="LOCAL"){
  233. //extract from select
  234. if (el.value<0) return;
  235. let opt=el.options[el.selectedIndex];
  236. label=opt.text;
  237. pId=crfSetup.getStudyId(label);
  238. //label=label.replace(/ \(Local: /,':');
  239. //label=label.replace(/\)/,'');
  240. }
  241. this.setParticipantIdToCrfEntry(mode,pId);//no argument (should come from mode)
  242. this.print(fName+" new value "+pId);
  243. this.setLabelMode(mode,label);
  244. this.updateCrfEntry();
  245. }
  246. participantIdManager.setLabelMode=
  247. function(mode,label){
  248. let fName='[setLabelMode1]';
  249. this.print(fName+' id '+label);
  250. //this will give two for study and one entry for local
  251. //let ids=pId.split(':');
  252. let id=label;
  253. if (mode!='LOCAL'){
  254. if (label=='NOT SET')
  255. id=label;
  256. else
  257. id=crfSetup.getStudyId(label);
  258. }
  259. let textValue=this.createTextElement(mode);
  260. this.print(fName+' textElement '+textValue);
  261. textValue.innerText=id;
  262. if (mode!="LOCAL" && label!='NOT SET'){
  263. let loc=crfSetup.getLocalId(label);
  264. this.print(fName+' setting local id '+loc);
  265. if (this.isValid(loc)){
  266. //pM.getParticipantIdFromCrfEntry('LOCAL');
  267. let tValLocal=this.createTextElement('LOCAL');
  268. tValLocal.innerText=loc;
  269. }
  270. //this.setParticipantIdToCrfEntry(pM,loc,'LOCAL');
  271. }
  272. let x=this.getInputButton(mode);//getInputManage
  273. let readonly=this.isReadonly(mode);
  274. if (readonly){
  275. x.style.display="none";
  276. }
  277. x.value="Edit";
  278. if (mode=='LOCAL')
  279. this.generateButton.style.display='none';
  280. }
  281. //edit mode
  282. participantIdManager.editId=
  283. function(mode){
  284. this.setEditMode(mode);
  285. }
  286. participantIdManager.setEditMode=
  287. function(mode){
  288. let fName='[setEditMode1]';
  289. this.print(fName+' pM '+this+' mode '+mode);
  290. //input
  291. let el=this.createInputElement(mode);
  292. let x=this.getInputButton(mode);
  293. x.value="Set";
  294. if (mode=='LOCAL'){
  295. this.generateButton.style.display='block';
  296. x.style.display='block';
  297. }
  298. }
  299. participantIdManager.resetLocal=
  300. function(){
  301. this.clearReadonly('LOCAL');
  302. this.setEditMode('LOCAL');
  303. this.resetButton.style.display='none';
  304. }
  305. //manage interaction to storage/CRF and study/LabKey
  306. participantIdManager.getParticipantField=
  307. function(){
  308. return this.setup.getRows('studyData')[0]['SubjectColumnName'];
  309. }
  310. participantIdManager.getCrfEntryFieldName=
  311. function(mode=null){
  312. if (mode=="LOCAL") return crfSetup.getLocalIdLabel();
  313. return crfSetup.getStudyIdLabel();
  314. }
  315. participantIdManager.setParticipantIdToCrfEntry=
  316. function(mode,pId){
  317. let id=pId;
  318. this.data.getCrfEntry()[this.getCrfEntryFieldName(mode)]=id;
  319. }
  320. participantIdManager.getParticipantIdFromCrfEntry=
  321. function(mode=null){
  322. return this.data.getCrfEntry()[this.getCrfEntryFieldName(mode)];
  323. }
  324. participantIdManager.isValid=
  325. function(name){
  326. if (!name) return false;
  327. if (name=="null") return false;
  328. return true;
  329. }
  330. participantIdManager.verifyCrfStudyId=
  331. function(){
  332. let fName='[verifyCrfStatusId]';
  333. //is studyId already set for the crf
  334. let studyId=this.getParticipantIdFromCrfEntry('STUDY');
  335. let localId=this.getParticipantIdFromCrfEntry('LOCAL');
  336. this.print(fName+' studyId '+studyId+' localId '+localId);
  337. this.resetButton.style.display='none';
  338. if (!studyId) return;
  339. let originalMode=this.getMode();
  340. if (originalMode=='LOCAL' && this.setup.getSettings("allowLocalIdChange")) {
  341. this.resetButton.style.display='block';
  342. }
  343. else{
  344. this.setReadonly('LOCAL');
  345. }
  346. this.setReadonly('STUDY');
  347. this.setMode("STUDY");
  348. this.setLabelMode('STUDY',this.setup.getParticipantLabel(this.data.getCrfEntry()));
  349. //get registration map to fill local id
  350. let that=this;
  351. let completeVerification=function(){that.completeVerification(studyId);}
  352. this.data.setRegistration(completeVerification);
  353. }
  354. participantIdManager.completeVerification=
  355. function(studyId){
  356. let fName='[completeVerification]';
  357. //subject to content of localId
  358. let rMapEntry=this.data.getRegistrationEntryMap(this.getCrfEntryFieldName('STUDY'))[studyId];
  359. //try to set it from registration
  360. let localId=rMapEntry[this.getCrfEntryFieldName('LOCAL')];
  361. this.print(fName+' localId '+localId+' isNull '+(localId==null)+' is"null" '+(localId=="null"));
  362. let localIdFromEntry=this.getParticipantIdFromCrfEntry('LOCAL');
  363. if (localIdFromEntry!=localId){
  364. //update mismatches
  365. this.setParticipantIdToCrfEntry('LOCAL',localId);
  366. this.updateCrfEntry();
  367. }
  368. //ignore the one set in the crfEntry, studyId prevails
  369. //if (!this.isValid(localId)) {
  370. // let localId=this.getParticipantIdFromCrfEntry('LOCAL');
  371. //}
  372. if (this.isValid(localId)) {
  373. this.setReadonly('LOCAL');
  374. this.setLabelMode('LOCAL',localId);
  375. }
  376. else{
  377. this.setEditMode('LOCAL');
  378. }
  379. }