crfVisit.js 92 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247
  1. var crfVisit={};
  2. crfVisit.config=new Object();
  3. crfVisit.setDebug=
  4. function(debug=null){
  5. if (debug){
  6. this.print=function(msg){debug.this.print(msg);};
  7. this.clear=function(){debug.clear();}
  8. return;
  9. }
  10. //provide default functions if not debug object is available
  11. this.print=function(msg){console.log(msg);}
  12. this.clear=function(){;}
  13. }
  14. crfVisit.setDebug();
  15. //harmonize signature
  16. //(schema,query,row,action=cvDoNothing,container=null
  17. crfVisit.insertRows=
  18. function(schema,query,rows,action=null,container=null,failure=null){
  19. this.modifyRows('insert',schema,query,rows,action,container);
  20. }
  21. crfVisit.deleteRows=
  22. function(schema,query,rows,action=null,container=null,failure=null){
  23. this.modifyRows('delete',schema,query,rows,action,container);
  24. }
  25. crfVisit.modifyRows=
  26. function(mode,schema,query,rows,action=null,container=null,failure=null){
  27. //insert rows to container/schema/query and return with action
  28. let fName="[cvModifyRows/"+mode+"]";
  29. this.print(fName+' '+schema+'/'+query);
  30. let qconfig=new Object();
  31. qconfig.schemaName=schema;
  32. qconfig.queryName=query;
  33. if (container) qconfig.containerPath=container;
  34. if (!rows) {
  35. this.print(fName+' rows '+rows);
  36. return;
  37. }
  38. qconfig.rows=rows;
  39. qconfig.success=function(data){;};
  40. if (action) qconfig.success=action;
  41. if (mode=='insert') LABKEY.Query.insertRows(qconfig);
  42. if (mode=='update') LABKEY.Query.updateRows(qconfig);
  43. if (mode=='delete') LABKEY.Query.deleteRows(qconfig);
  44. this.print(fName+" done");
  45. }
  46. crfVisit.selectRows=
  47. function(schema,query,filters=[],action=null, container=null, failure=null, columns=null){
  48. let fName="[cvSelectRows]";
  49. this.print(fName+' '+schema+' '+query+' '+container);
  50. let qconfig=new Object();
  51. qconfig.schemaName=schema;
  52. qconfig.queryName=query;
  53. if (container) qconfig.containerPath=container;
  54. qconfig.filterArray=filters;
  55. qconfig.success=function(data){;};
  56. if (action) qconfig.success=action;
  57. if (failure) qconfig.failure=failure;
  58. if (columns) qconfig.columns=columns;
  59. LABKEY.Query.selectRows(qconfig);
  60. this.print(fName+" done");
  61. }
  62. crfVisit.createCrfStatus=
  63. function(crfEntry){
  64. let crfStatus=new Object();
  65. crfStatus.entryId=crfEntry.entryId;
  66. crfStatus.submissionDate=new Date();
  67. crfStatus.FormStatus=crfEntry.FormStatus;
  68. crfStatus.User=crfEntry.UserId;
  69. crfStatus.Form=crfEntry.Form;
  70. return crfStatus;
  71. }
  72. crfVisit.init=
  73. function(cb=null){
  74. let that=this;
  75. let action=function(){that.scriptsLoaded(cb);};
  76. LABKEY.Utils.requiresScript(["crfTecant/runQuery.js","crfTecant/crfReviewSection.js","crfTecant/participantIdManager.js","crfTecant/variableList.js","crfTecant/webdav.js","crfTecant/crfPrint.js"],action);
  77. }
  78. crfVisit.scriptsLoaded=
  79. function(cb=null){
  80. participantIdManager.set(this);
  81. webdav.set(this);
  82. crfReviewSection.set(this);
  83. crfPrint.set(this);
  84. if (cb) cb();
  85. }
  86. crfVisit.getElement=
  87. function(id){
  88. return this.config.document.getElementById(id);
  89. }
  90. crfVisit.setContainer=
  91. function(label,container){
  92. let config=this.config;
  93. if (!(config.formConfig.hasOwnProperty('container'))){
  94. config.formConfig.container=new Array();
  95. }
  96. config.formConfig.container[label]=container;
  97. }
  98. crfVisit.getContainer=
  99. function(label){
  100. return this.config.formConfig.container[label];
  101. }
  102. crfVisit.getCRFrefFirst=
  103. function(){
  104. //crfRef is part of html call and gets stored in the page
  105. return this.getElement(this.config.crfRefId).innerHTML;
  106. }
  107. crfVisit.getCRFref=
  108. function (){
  109. //'crfRefId'
  110. return this.config.formConfig.crfEntry['entryId'];
  111. }
  112. crfVisit.getCRFrefData=
  113. function(){
  114. let parentCrf=this.config.formConfig.crfEntry['parentCrf'];
  115. if (parentCrf!=undefined) return parentCrf;
  116. return this.getCRFref();
  117. }
  118. crfVisit.onFailure=
  119. function(errorInfo, options, responseObj){
  120. if (errorInfo && errorInfo.exception)
  121. alert("Failure: " + errorInfo.exception);
  122. else
  123. alert("Failure: " + responseObj.statusText);
  124. }
  125. crfVisit.doNothing=
  126. function (){
  127. this.print('doNothing called');
  128. }
  129. crfVisit.getSnapshotObject=
  130. function(){
  131. if (!("dataQueriesSnapshot" in this.config.formConfig))
  132. this.config.formConfig.dataQueriesSnapshot=new Object();
  133. return this.config.formConfig.dataQueriesSnapshot;
  134. }
  135. crfVisit.getQuerySnapshot=
  136. function(queryName){
  137. //check whether queryName is in snapshotObject?
  138. return this.getSnapshotObject()[queryName];
  139. }
  140. crfVisit.getLayoutObject=
  141. function(){
  142. if (!("dataQueriesLayout" in this.config.formConfig))
  143. this.config.formConfig.dataQueriesLayout=new Object();
  144. return this.config.formConfig.dataQueriesLayout;
  145. }
  146. crfVisit.getQueryLayout=
  147. function(queryName){
  148. //check whether queryName is in snapshotObject?
  149. return this.getLayoutObject()[queryName];
  150. }
  151. crfVisit.getLookupObject=
  152. function(){
  153. if (!("lookup" in this.config.formConfig))
  154. this.config.formConfig.lookup=new Object();
  155. return this.config.formConfig.lookup;
  156. }
  157. crfVisit.getLookup=
  158. function(queryName){
  159. let x=this.getLookupObject();
  160. if (queryName in x) return x[queryName];
  161. return null;
  162. }
  163. crfVisit.getQueryList=
  164. function(){
  165. if (!("queryList" in this.config.formConfig))
  166. this.config.formConfig.queryList=new Object();
  167. return this.config.formConfig.queryList;
  168. }
  169. crfVisit.getIdManager=
  170. function(){
  171. if (!("idManager" in this.config.formConfig))
  172. this.config.formConfig.idManager=participantIdManager.getObject();
  173. return this.config.formConfig.idManager;
  174. }
  175. crfVisit.getAdditionalData=
  176. function(formSetupEntry){
  177. //return information on additional data associated with the form
  178. //additionalData is a sub-list with multiple entries per patient/visit
  179. let config=this.config;
  180. //argument is the row of the formSetup setup list
  181. let queryName=config.formConfig.queryMap[formSetupEntry['queryName']];
  182. let fName='[getAdditionalData/'+queryName+']';
  183. this.print(fName);
  184. //additionalData holds a reference to all queries already parsed
  185. //this helps in reducing number of calls to the database (I assume)
  186. if (queryName in config.formConfig.additionalData){
  187. this.print(fName+': Returning preset value');
  188. return config.formConfig.additionalData[queryName];
  189. }
  190. //first time we see this query, so we have to do the setup
  191. this.print(fName+': generating');
  192. config.formConfig.additionalData[queryName]=new Object();
  193. //takes address, so further changes will be to the newly created object
  194. //in fact, ad is just a short alias of the long variable name on the right
  195. let ad=config.formConfig.additionalData[queryName];
  196. //no additional data
  197. if (formSetupEntry["showFlag"]==="NONE") {
  198. this.print(fName+": empty");
  199. return ad;
  200. }
  201. //use showFlag to setup report section of the CRF list
  202. if (formSetupEntry["showFlag"]==="REVIEW") {
  203. //abuse additionalData to signal different segment
  204. this.print(fName+": generateReport");
  205. ad.isReview=true;
  206. return ad;
  207. }
  208. //setup the additionalData memory object
  209. this.print(fName+': setting values');
  210. ad.showFlag=formSetupEntry["showFlag"];
  211. ad.showFlagValue=formSetupEntry["showFlagValue"];
  212. ad.queryName=formSetupEntry["showQuery"];
  213. //for data queries, limit to present CRF only
  214. ad.filters=new Object();
  215. ad.filters['crfRef']=this.getCRFref();
  216. //compose a long debug message
  217. let msg=fName+": flag "+ad.showFlag;
  218. msg+=" value "+ad.showFlagValue;
  219. msg+=" query "+ad.queryName;
  220. this.print(msg);
  221. return ad;
  222. }
  223. crfVisit.selectFormSetupRows=
  224. function(formId){
  225. let formSetupRows=new Array();
  226. let config=this.config;
  227. let allRows=config.formConfig.formSetup.rows;
  228. for (let i=0;i<allRows.length;i++){
  229. let formEntry=allRows[i];
  230. if (formEntry.formName==formId)
  231. formSetupRows.push(formEntry);
  232. }
  233. return formSetupRows;
  234. }
  235. crfVisit.findTitle=
  236. function(queryName){
  237. //find by name from formDatasets
  238. //and set associated title as title
  239. let config=this.config;
  240. let rows=config.formConfig.formDatasets.rows;
  241. for (let i=0;i<rows.length;i++){
  242. let entry=rows[i];
  243. if (entry['queryName']!=queryName) continue;
  244. return entry['title'];
  245. }
  246. return "NONE";
  247. }
  248. crfVisit.makeSetup=
  249. function(listName){
  250. //generate setup object whcih should contain fields:
  251. //readonlyFlag - whether the dataset is writeable
  252. //filters - selection fields that allow creation of LABKEY.Filter.create()
  253. //getInputId - formating of unique ids for html elements
  254. let fName='[Setup]';
  255. this.print(fName+' '+listName);
  256. let setup=new Object();
  257. setup.queryName=listName;
  258. setup.readonlyFlag=function(vName){return false};
  259. setup.filters=new Object();
  260. setup.filters['crfRef']=this.getCRFref();
  261. setup.getInputId=function(vName){return listName+"_"+vName;}
  262. setup.isReview=false;
  263. return setup;
  264. }
  265. crfVisit.getFullAccessSetup=
  266. function(listName){
  267. //addApply - whether a submit/Save button is generated
  268. let setup=this.makeSetup(listName);
  269. setup.addApply="Save";
  270. return setup;
  271. }
  272. crfVisit.getReadonlySetup=
  273. function(listName){
  274. let setup=this.makeSetup(listName);
  275. //see definition of setup object above, change readonly flag
  276. setup.readonlyFlag=function(vName){return true};
  277. return setup;
  278. }
  279. crfVisit.getSetup=
  280. function(listName,writeAccess=true){
  281. //change to section granulated permission of type EDIT, COMMENT, READ
  282. //let formStatus=config.formConfig.formStatus;
  283. //equivalent to READ
  284. if (!writeAccess)
  285. //if (formStatus=="Submitted")
  286. return this.getReadonlySetup(listName);
  287. //if (formStatus=="Approved")
  288. // return readonlySetup(listName);
  289. return this.getFullAccessSetup(listName);
  290. }
  291. crfVisit.generateSection=
  292. function(formSetupEntry){
  293. let config=this.config;
  294. let that=this;
  295. let listName=config.formConfig.queryMap[formSetupEntry['queryName']];
  296. //if (!listName) is for debugSection
  297. if (!listName){
  298. listName="debugSection";
  299. }
  300. let fName='[generateSection/'+listName+']';
  301. let sectionTitle=formSetupEntry['title'];
  302. let accessModeColumn=config.formConfig.operator+'Mode';
  303. let accessMode=formSetupEntry[accessModeColumn];
  304. //this will fix it for later use as well
  305. let additionalData=this.getAdditionalData(formSetupEntry);
  306. this.print(fName);
  307. let formName=config.masterForm;//this is HTML designator of area on page
  308. let debug=true;
  309. let tb=config.document.createElement('table');
  310. tb.className='t2';
  311. let row=tb.insertRow();
  312. let cell=config.document.createElement('th');
  313. row.appendChild(cell);
  314. cell.setAttribute("colspan","4");
  315. cell.style.fontSize="20px";
  316. cell.style.textAlign="center";
  317. let cellData=config.document.createTextNode(sectionTitle);
  318. cell.appendChild(cellData);
  319. cell=row.insertCell();
  320. let input=config.document.createElement("input");
  321. input.type="button";
  322. input.value="Show";
  323. input.id="toggle"+listName+"VisbilityButton";
  324. input.onclick=function(){that.toggleVisibility(listName,input.id)};
  325. cell.appendChild(input);
  326. this.getElement(formName).appendChild(tb);
  327. let div=config.document.createElement('div');
  328. div.id=listName;
  329. div.style.display="none";
  330. this.getElement(formName).appendChild(div);
  331. //here divert for debugArea
  332. if (listName=="debugSection"){
  333. let debugArea=config.document.createElement('textarea');
  334. debugArea.rows=10;
  335. debugArea.cols=95;
  336. debugArea.id=config.debugId;
  337. div.appendChild(debugArea);
  338. return;
  339. }
  340. let divTable=config.document.createElement('div');
  341. divTable.id=listName+"Table";
  342. div.appendChild(divTable);
  343. if ("showFlag" in additionalData) {
  344. additionalData.divName=listName+"SubDiv";
  345. additionalData.divQueryName=listName+"SubDivList";
  346. let div1=config.document.createElement('div');
  347. div1.id=additionalData.divName;
  348. div1.style.display="none";
  349. div.appendChild(div1);
  350. let div2=config.document.createElement('div');
  351. div2.id=additionalData.divQueryName;
  352. div1.appendChild(div2);
  353. }
  354. this.print(fName+" generate master table");
  355. let writeMode=accessMode=="EDIT";
  356. let setup=this.getSetup(listName,writeMode);
  357. if ("isReview" in additionalData){
  358. crfReviewSection.set(this);
  359. let action=function(){crfReviewSection.CB();};
  360. crfReviewSection.generateSection(listName,div.id,action);
  361. return;
  362. }
  363. //master table is unique per visit
  364. setup.unique=true;
  365. this.generateTable(listName,divTable.id,additionalData,setup);
  366. if (debug) this.print("generate master table: done");
  367. let generateSubTable=true;
  368. //generateSubTable equivalent to read/write access to section
  369. if (accessMode != "EDIT")
  370. generateSubTable=false;
  371. if (! ("showFlag" in additionalData) ) generateSubTable=false;
  372. if (generateSubTable){
  373. let qName=additionalData.queryName;
  374. let dName=additionalData.divName;
  375. let xsetup=this.getFullAccessSetup(qName);
  376. //only set master query for additionalData
  377. xsetup.masterQuery=listName;
  378. //if (readonly) setup=readonlySetup(config);
  379. xsetup.subTable=true;
  380. this.generateTable(qName,dName,additionalData,xsetup);
  381. //generateTable(formSetupEntry,qName,dName,additionalData,setup);
  382. }
  383. this.print("generate review");
  384. let divReviewList=config.document.createElement('div');
  385. divReviewList.id=listName+"ReviewList";
  386. div.appendChild(divReviewList);
  387. let divReview=config.document.createElement('div');
  388. divReview.id=listName+"Review";
  389. div.appendChild(divReview);
  390. //assume we already have listId (content of config.setupQueryName is listId)
  391. //we need listName also
  392. //qconfig.queryName=config.setupQueryName;
  393. this.generateReview(divReview.id,divReviewList.id,listName,accessMode);
  394. if (accessMode!='GENERATE') return;
  395. this.print('Adding generate button');
  396. //add generateButton
  397. let divGenerateButton=config.document.createElement('div');
  398. divGenerateButton.id=listName+'GenerateButton';
  399. div.appendChild(divGenerateButton);
  400. this.print('Adding generate button completed to here');
  401. let cb=function(){that.onGenerateQuery(listName);};
  402. this.generateButton(divGenerateButton.id,'Generate','Generate '+listName,'onGenerateQuery',cb);
  403. this.print('Adding generate button completed');
  404. }
  405. crfVisit.generateReview=
  406. function(divReviewId,divReviewListId, listName, accessMode){
  407. let config=this.config;
  408. let listId=config.formConfig.fields[listName].queryId;
  409. //listId is a number->should it be queryName?
  410. let fName='[generateReview]';
  411. this.print(fName+" list "+listId+'/'+listName);
  412. let reviewSetup=new Object();
  413. reviewSetup.readonlyFlag=function(vName){
  414. if (vName=="queryName") return true;
  415. if (vName=="queryname") return true;
  416. if (vName=="ModifiedBy") return true;
  417. return false;};
  418. reviewSetup.addApply="Add Review";
  419. reviewSetup.reviewTable=true;
  420. let generateTableFlag=true;
  421. let formStatus=config.formConfig.formStatus;
  422. //COMMENTS allowed or not
  423. //three levels of access: EDIT, COMMENT, READ
  424. if (accessMode == "READ"){
  425. //if (formStatus == "Approved" ){
  426. delete reviewSetup.addApply;
  427. reviewSetup.readonlyFlag=function(vName){return false;}
  428. generateTableFlag=false;
  429. }
  430. reviewSetup.filters=new Object();
  431. reviewSetup.filters["crfRef"]=this.getCRFref();
  432. if (config.formConfig.crfEntry.parentCrf){
  433. reviewSetup.filters["crfRef"]=this.getCRFref()+";"+config.formConfig.crfEntry.parentCrf;
  434. }
  435. reviewSetup.filters["queryName"]=listId;//entry in reviewComments list is queryname, all in small caps
  436. //needs listName, in argument
  437. reviewSetup.getInputId=function(vName){return listName+"_add"+vName};
  438. reviewSetup.divReviewListId=divReviewListId;
  439. reviewSetup.isReview=true;
  440. let msg="Review: divId: "+divReviewId;
  441. msg+=" inputId: "+reviewSetup.getInputId;
  442. this.print(msg);
  443. this.updateListDisplay(divReviewListId,"reviewComments",reviewSetup.filters,true);
  444. if (! generateTableFlag) return;
  445. this.generateTable("reviewComments",divReviewId,new Object(),reviewSetup);
  446. }
  447. //>>>>>>>>>>trigger visibility of additional lists
  448. crfVisit.setListVisibility=
  449. function(input,setup,readonlyFlag){
  450. let config=this.config;
  451. let fName="[setListVisibility/"+setup.queryName+"]";
  452. this.print(fName);
  453. let additionalData=config.formConfig.additionalData[setup.queryName];
  454. let x = this.getElement(additionalData.divName);
  455. this.print(fName+": Div: "+x);
  456. x.style.display="none";
  457. let sText;
  458. if (readonlyFlag) sText=input.innerText;
  459. else sText=input.options[input.selectedIndex].text;
  460. this.print(fName+": Selected option text: "+sText);
  461. if (sText == additionalData.showFlagValue){
  462. let filters=new Object();
  463. if ("filters" in additionalData) filters=additionalData.filters;
  464. x.style.display = "block";
  465. this.updateListDisplay(additionalData.divQueryName,
  466. additionalData.queryName,filters,readonlyFlag);
  467. }
  468. }
  469. //>>have list refresh when data is added (not optimal yet)
  470. //
  471. crfVisit.updateListDisplay=
  472. function(divName,queryName,filters,readonlyFlag){
  473. //use Labkey.QueryWebPart to show list
  474. let fName="[updateListDisplay]";
  475. this.print(fName+": UpdateListDisplay: Query - "+queryName
  476. +" div - "+divName);
  477. if (divName=="NONE") return;
  478. let crfRef=this.getCRFref();
  479. let div=this.getElement(divName);
  480. this.print(fName+": generating WebPart: "+queryName);
  481. var qconfig=new Object();
  482. qconfig.renderTo=divName;
  483. //point to data container
  484. qconfig.containerPath=this.getContainer('data');
  485. qconfig.schemaName='lists';
  486. qconfig.queryName=queryName;
  487. qconfig.buttonBarPosition='top';
  488. qconfig.filters=[];
  489. for (f in filters){
  490. let fType=LABKEY.Filter.Types.EQUAL;
  491. this.print(fName+' filter ['+f+'] '+filters[f]+'/'+typeof(filters[f])+' ['+fType+']');
  492. if (variableList.isFilterList(filters[f])){
  493. fType=LABKEY.Filter.Types.IN;
  494. }
  495. qconfig.filters.push(LABKEY.Filter.create(f, filters[f],fType));
  496. }
  497. let that=this;
  498. qconfig.success=function(data){that.updateSuccess(data);};
  499. qconfig.failure=function(errorInfo,options,responseObj){that.onFailure(errorInfo,options,responseObj);};
  500. //show only print button
  501. if (readonlyFlag){
  502. qconfig.buttonBar=new Object();
  503. qconfig.buttonBar.items=["print"];
  504. }
  505. LABKEY.QueryWebPart(qconfig);
  506. }
  507. crfVisit.updateSuccess=
  508. function(data){
  509. this.print("Update success");
  510. }
  511. //TODO: this should trigger a data refresh on section, ie populateData(field)
  512. crfVisit.toggleVisibility=
  513. function(divName,buttonName){
  514. let fName='[toggleVisibility/'+divName+']';
  515. this.print(fName);
  516. let config=this.config;
  517. let x = this.getElement(divName);
  518. if (x.style.display === "none") {
  519. //exclude non data sections (like debug)...
  520. this.print(fName+': issuing setData(populateSection)');
  521. x.style.display = "block";
  522. this.getElement(buttonName).value="Hide";
  523. let that=this;
  524. let cb=function(){that.populateSection(divName);};
  525. this.setData(cb);
  526. } else {
  527. x.style.display = "none";
  528. this.getElement(buttonName).value="Show";
  529. }
  530. }
  531. crfVisit.generateButton=
  532. function(divName,caption,label,callbackLabel,callback=null){
  533. this.print("generateButtonX");
  534. let config=this.config;
  535. let tb=config.document.createElement('table');
  536. tb.className="t2";
  537. let r1=tb.insertRow();
  538. th=config.document.createElement('th');
  539. r1.appendChild(th);
  540. th.innerHTML=caption;
  541. //*!*
  542. let c2=r1.insertCell();
  543. let i1=config.document.createElement("input");
  544. i1.type="button";
  545. i1.value=label;
  546. i1.style.fontSize="20px";
  547. let that=this;
  548. if (callback)
  549. i1.onclick=callback;
  550. else
  551. i1.onclick=function(){that[callbackLabel]();};
  552. i1.id='button_'+callbackLabel;
  553. c2.appendChild(i1);
  554. let c1=r1.insertCell();
  555. c1.setAttribute("colspan","1");
  556. //this is only for saveReview?
  557. c1.id=divName+'_reportField';
  558. //c1.id=config.submitReportId;
  559. let el=this.getElement(divName);
  560. this.print("generateButton: element["+divName+"]: "+el);
  561. el.appendChild(tb);
  562. }
  563. crfVisit.generateSubQuery=
  564. function(input, setup, readonlyFlag){
  565. let fName="[generateSubQuery]";
  566. let config=this.config;
  567. if (setup.isReview) return;
  568. if (!(setup.queryName in config.formConfig.additionalData)){
  569. this.print(fName+': no additionalData entry (probably a subquery)');
  570. return;
  571. }
  572. let additionalData=config.formConfig.additionalData[setup.queryName];
  573. if (!("showFlag" in additionalData))
  574. return;
  575. this.print(fName);
  576. let expId=setup.getInputId(additionalData.showFlag);
  577. if (expId!=input.id) {
  578. this.print(fName+": ignoring field "+input.id+"/"+expId);
  579. return;
  580. }
  581. this.print(fName+": Setting onChange to "+input.id);
  582. if (readonlyFlag)
  583. return;
  584. let that=this;
  585. input.onchange=function(){that.setListVisibility(input,setup,readonlyFlag)};
  586. }
  587. //>>populate fields
  588. //
  589. //
  590. //split to field generation and field population
  591. //
  592. crfVisit.addFieldRow=
  593. function(tb,field,setup,additionalData){
  594. let fName="[addFieldRow/"+setup.queryName+':'+field.name+']';
  595. let config=this.config;
  596. let vName=field.name;
  597. let vType=field.type;
  598. let isLookup=("lookup" in field);
  599. this.print(fName+": ["+vName+"/"+vType+'/'+isLookup+"]");
  600. let row=tb.insertRow();
  601. let cell=config.document.createElement('th');
  602. cell.style.width='300px';
  603. row.appendChild(cell);
  604. let text = config.document.createTextNode(field.shortCaption);
  605. cell.appendChild(text);
  606. let input=null;
  607. let colSpan="3";
  608. let cell1=row.insertCell();
  609. cell1.colSpan=colSpan;
  610. let readonlyFlag=setup.readonlyFlag(vName);
  611. //set the html input object
  612. while (1){
  613. if (readonlyFlag){
  614. input=config.document.createElement('label');
  615. input.innerText='Loading';
  616. break;
  617. }
  618. //lookup
  619. if (isLookup){
  620. input = config.document.createElement("select");
  621. break;
  622. }
  623. //date
  624. if (vType=="date"){
  625. input = config.document.createElement("input");
  626. input.type="date";
  627. break;
  628. }
  629. //string
  630. if (vType=="string"){
  631. //we have to make sure UNDEF is carried to below
  632. //since we are adapting file to either show
  633. //current file or allow user to select a file
  634. //
  635. //TODO change this so one can always select file
  636. //but also show the selected file
  637. if(vName.search("reviewComment")>-1){
  638. input = config.document.createElement("textarea");
  639. input.cols="65";
  640. input.rows="5";
  641. break;
  642. }
  643. input=config.document.createElement('input');
  644. input.type="text";
  645. if (vName.search('_file_')<0) break;
  646. cell1.setAttribute('colspan',"1");
  647. let cell2=row.insertCell();
  648. cell2.setAttribute('colspan',"2");
  649. let input1=config.document.createElement('input');
  650. input1.type="file";
  651. input1.id=setup.getInputId(vName)+'_file_';
  652. cell2.appendChild(input1);
  653. break;
  654. }
  655. if (vType=="float"){
  656. input = config.document.createElement("input");
  657. input.type="text";
  658. break;
  659. }
  660. if (vType=="boolean"){
  661. input = config.document.createElement("input");
  662. input.type="checkbox";
  663. this.print("Creating checkbox");
  664. break;
  665. }
  666. break;
  667. }
  668. input.id=setup.getInputId(vName);
  669. cell1.appendChild(input);
  670. this.print(fName+': adding element '+input.id);
  671. this.print(fName+': listing element '+this.getElement(input.id));
  672. //connect associated list
  673. this.generateSubQuery(input,setup,readonlyFlag);
  674. if (readonlyFlag) {
  675. this.print(fName+': exiting(readonlyFlag)');
  676. return;
  677. }
  678. if (!isLookup) {
  679. this.print(fName+': exiting (not lookup)');
  680. return;
  681. }
  682. let lookup=field["lookup"];
  683. //get all values from config.formConfig.lookup[X]
  684. let lObject=config.formConfig.lookup[lookup.queryName];
  685. //debug
  686. this.print(fName+": query: "+lookup.queryName);
  687. this.print(fName+": ElementId: "+input.id);
  688. this.print(fName+": No of options: " +lObject.LUT.length);
  689. this.print(fName+": Element: "+input);
  690. //set the lut value (input is text label) for readonly
  691. //clear existing fields from input
  692. for(let i = input.options.length; i >= 0; i--) {
  693. input.remove(i);
  694. }
  695. //create option -1
  696. let opt = config.document.createElement("option");
  697. opt.text = "<Select>";
  698. opt.value = -1;
  699. input.options[0] = opt;
  700. this.print(fName+": Adding <Select>");
  701. //add other, label them with LUT
  702. for (let v in lObject.LUT) {
  703. this.print(fName+': populating '+v+': '+lObject.LUT[v]);
  704. let opt = config.document.createElement("option");
  705. opt.text = lObject.LUT[v];
  706. opt.value = v;
  707. input.options[input.options.length] = opt;
  708. }
  709. input.selectedIndex=0;
  710. }
  711. crfVisit.addSpecialFieldRows=
  712. function(tb,specFieldSetup,setup){
  713. //tb is the table, specFieldSetup is a row from the table where special fields are being setup
  714. //the first column is fieldUID, which is a colon joined amalgation of queryName:fieldName
  715. let fieldUID=specFieldSetup["fieldUID"];
  716. let x=fieldUID.split(':');
  717. let fieldName=x[1];
  718. let fName="[addSpecialFieldRow/"+fieldUID+"]";
  719. let q=variableList.parseVariables(specFieldSetup['actionParameters']);
  720. let config=this.config;//for add data
  721. this.print(fName);
  722. if (specFieldSetup['actionType']=='textArea'){
  723. let row=tb.insertRow();
  724. let cell1=row.insertCell();
  725. cell1.colSpan="4";
  726. cell1.style.textAlign="justify";
  727. cell1.style.padding="10px";
  728. cell1.style.backgroundColor="#e0e0e0";
  729. cell1.innerText=q['description'];
  730. return;
  731. }
  732. if (specFieldSetup['actionType']=='generationObject'){
  733. //only in EDIT mode!!
  734. let ro=setup.readonlyFlag(fieldName);
  735. if (ro) return;
  736. generateRegistration.set(this);
  737. let gc=generateRegistration.getObject(q,setup.getInputId(fieldName));
  738. let that=this;
  739. let action=function(){that.doNothing();};
  740. if ('mailRecipient' in q){
  741. gc.callback=function(data){that.sendEmail(data,q['mailRecipient'],action,q['subject']);};
  742. }
  743. else
  744. gc.callback=function(data){that.doNothing();};
  745. if ("addData" in q){
  746. vars=q["addData"].split(',');
  747. gc.addData=new Array();
  748. for (let v in vars){
  749. let s=vars[v]
  750. //variable name can be written as A/B where A is the name in addData and B is the variable name in crfEntry
  751. //useful for mocking up crfId from daughter crf-s such as registration
  752. let sArray=s.split('/');
  753. let sTarget=sArray[0];
  754. let sSource=sArray[sArray.length-1];
  755. gc.addData[sTarget]=config.formConfig.crfEntry[sSource];
  756. this.print(fName+" addData ["+sTarget+"]: "+gc.addData[sTarget]);
  757. }
  758. }
  759. let row=tb.insertRow();
  760. let cell=config.document.createElement('th');
  761. row.appendChild(cell);
  762. let text = config.document.createTextNode("Automatic ID generator");
  763. cell.appendChild(text);
  764. let cell1=row.insertCell();
  765. cell1.colSpan="3";
  766. let b=config.document.createElement("input");
  767. b.type="button";
  768. b.id="generateIdButton";
  769. b.onclick=function(){generateRegistration.execute(gc);};
  770. b.value="Generate ID";
  771. cell1.appendChild(b);
  772. }
  773. }
  774. crfVisit.populateFieldRow=
  775. function(entry,field,setup){
  776. this.populateField(entry,field,setup);
  777. this.populateSubQuery(entry,field,setup);
  778. }
  779. crfVisit.populateSubQuery=
  780. function(entry,field,setup){
  781. let fName='[populateSubQuery/'+setup.queryName+':'+field.name+']';
  782. let config=this.config;
  783. if (setup.isReview) return;
  784. if (!(setup.queryName in config.formConfig.additionalData)){
  785. let msg=fName+': no additionalData entry for '+setup.queryName;
  786. msg+=' (probably a subquery)';
  787. this.print(msg);
  788. return;
  789. }
  790. //find if field is connected to a sub array
  791. //find queryName
  792. //
  793. let additionalData=config.formConfig.additionalData[setup.queryName];
  794. this.print(fName);
  795. //let flag=additionalData.showFlag;
  796. if (!("showFlag" in additionalData)) return;
  797. let eId=setup.getInputId(additionalData.showFlag);
  798. let id=setup.getInputId(field.name);
  799. if (eId!=id) {
  800. this.print(fName+": ignoring field "+id+"/"+eId);
  801. return;
  802. }
  803. this.print(fName+': id '+id);
  804. //hard to estimate readonlyFlag
  805. //
  806. let input=this.getElement(id);
  807. let eType=input.nodeName.toLowerCase();
  808. let readonlyFlag=eType!="select";
  809. this.setListVisibility(input,setup,readonlyFlag);
  810. }
  811. crfVisit.clearField=
  812. function(field,setup){
  813. let foo=new Object();
  814. this.populateField(foo,field,setup);
  815. }
  816. crfVisit.populateField=
  817. function(entry,field,setup){
  818. let vName=field.name;
  819. let fName='[populateFieldName/'+vName+']';
  820. let config=this.config;
  821. let varValue="UNDEF";
  822. //if (vName in setup.filters) varValue=setup.filters[vName];
  823. if (vName in entry) varValue=entry[vName];
  824. //if part of the filter, set it to value
  825. if (vName in setup.filters) varValue=setup.filters[vName];
  826. let isLookup=("lookup" in field);
  827. this.print(fName+' v='+varValue+'/'+isLookup+' ['+
  828. setup.getInputId(field.name)+']');
  829. let vType=field.type;
  830. let id=setup.getInputId(vName);
  831. let input=this.getElement(id);
  832. //date
  833. if (vType=="date"){
  834. if (varValue=="UNDEF") varValue=new Date();
  835. else varValue=new Date(varValue);
  836. }
  837. //lookup for readonly
  838. if (isLookup && varValue!="UNDEF"){
  839. let lookup=field["lookup"];
  840. //get all values from config.formConfig.lookup[X]
  841. let lObject=config.formConfig.lookup[lookup.queryName];
  842. varValue=lObject.LUT[varValue];
  843. }
  844. this.print('Element: '+input);
  845. //figure out the element type
  846. let eType=input.nodeName.toLowerCase();
  847. this.print('Element type: '+eType);
  848. //change varValue for printing
  849. if (varValue=="UNDEF") varValue="";
  850. //HTMLTextArea, createElement(textArea)
  851. if (eType==="textarea"){
  852. input.value=varValue;
  853. return;
  854. }
  855. //Text, createTextNode
  856. if (eType==="#text"){
  857. input.nodeValue=varValue;
  858. return;
  859. }
  860. //HTMLLabelElement, createElement('label')
  861. if (eType==="label"){
  862. input.innerText=varValue;
  863. return;
  864. }
  865. //HTMLSelectElement, createElement('select')
  866. if (eType==="select"){
  867. input.selectedIndex=0;
  868. for (let i=0;i<input.options.length;i++){
  869. let v=input.options[i].text;
  870. if (v!=varValue) continue;
  871. input.selectedIndex=i;
  872. break;
  873. }
  874. return;
  875. }
  876. if (eType!="input"){
  877. this.print('Unknown type: '+eType+' encountered, igonring');
  878. return;
  879. }
  880. //HTMLInputElement
  881. let type=input.type;
  882. if (type=="date"){
  883. input.valueAsDate=varValue;
  884. return;
  885. }
  886. //string,float
  887. if (type=="text"){
  888. input.value=varValue;
  889. return;
  890. }
  891. //boolean
  892. if (type=="checkbox"){
  893. input.checked=varValue;
  894. return;
  895. }
  896. this.print('Unknown input type: '+type+'. Ignoring.');
  897. }
  898. crfVisit.populateTable=
  899. function(listName,writeMode){
  900. //function populateTable(formSetupEntry){
  901. //let listName=config.formConfig.queryMap[formSetupEntry['queryName']];
  902. //let accessMode=config.formConfig.operator+'Mode';
  903. //let writeMode=formSetupEntry[accessMode]=='EDIT';
  904. let fName='[populateTable/'+listName+']';
  905. let setup=this.getSetup(listName,writeMode);
  906. let entry=new Object();
  907. //data snapshot
  908. let fQuery=this.getQuerySnapshot(listName);
  909. let queryLayout=this.getQueryLayout(listName);
  910. //here I assume that listName was parsed during setDataLayout and setData
  911. //so that rows was set (even if they are empty)
  912. this.print(fName+"]: nrows "+fQuery.rows.length);
  913. if (fQuery.rows.length>0)
  914. entry=fQuery.rows[0];
  915. let fields=queryLayout.fields;
  916. for (f in fields){
  917. let field=fields[f];
  918. //each field is a new row
  919. this.print(fName+": Adding field: "+f+'/'+field.name+' hidden: '+field.hidden+' type:'+field.type);
  920. if (field.hidden) continue;
  921. if (field.name=="crfRef") continue;
  922. this.populateFieldRow(entry,field,setup);
  923. }
  924. }
  925. crfVisit.generateTable=
  926. function(listName,divName,additionalData,setup){
  927. let fName="[generateTable/"+listName+"]";
  928. let config=this.config;
  929. this.print(fName);
  930. //is listName and setup.queryName a duplicate of the same value
  931. this.print(fName+': setup.queryName '+setup.queryName);
  932. //assume data is set in config.formConfig.dataQueries[data.queryName].rows;
  933. let populateData=true;
  934. if ("subTable" in setup){
  935. this.print(fName+" is subTable");
  936. populateData=false;
  937. }
  938. let entry=new Object();
  939. //data snapshot
  940. let fQuerySnapshot=this.getQuerySnapshot(listName);
  941. let queryLayout=this.getQueryLayout(listName);
  942. //here I assume that listName was parsed during setDataLayout and setData
  943. //so that rows was set (even if they are empty)
  944. this.print(fName+": Nrows "+fQuerySnapshot.rows.length);
  945. if (fQuerySnapshot.rows.length>0)
  946. entry=fQuerySnapshot.rows[0];
  947. if ("reviewTable" in setup){
  948. entry['reviewComment']='';
  949. delete entry["ModifiedBy"];
  950. }
  951. let tb=config.document.createElement('table');
  952. tb.className="t2";
  953. this.getElement(divName).appendChild(tb);
  954. //this are the fields (probably constant)
  955. let fields=queryLayout.fields;
  956. for (f in fields){
  957. let field=fields[f];
  958. let fieldUID=listName+":"+field.name;
  959. //each field is a new row
  960. this.print(fName+": Adding field: "+f+'/'+field.name+' ('+fieldUID+').');
  961. //unique name
  962. if (field.hidden) continue;
  963. if (field.name=="crfRef") continue;
  964. this.addFieldRow(tb,field,setup,additionalData);
  965. if (populateData) this.populateFieldRow(entry,field,setup);
  966. if (fieldUID in config.formConfig["specialFields"]){
  967. let specFieldSetup=config.formConfig["specialFields"][fieldUID];
  968. this.addSpecialFieldRows(tb,specFieldSetup,setup);
  969. }
  970. }
  971. //finish of if apply button is not required
  972. if (!("addApply" in setup)) {
  973. this.print(fName+"populateTable: done");
  974. return;
  975. }
  976. let row=tb.insertRow();
  977. let th=config.document.createElement('th');
  978. row.appendChild(th);
  979. th.innerHTML=setup.addApply;
  980. let cell=row.insertCell();
  981. //cell.setAttribute("colspan","2");
  982. let input=config.document.createElement("input");
  983. input.type="button";
  984. input.value=setup.addApply;
  985. cell.appendChild(input);
  986. let cell1=row.insertCell();
  987. cell1.setAttribute("colspan","2");
  988. cell1.id=setup.getInputId("rerviewLastSave");
  989. cell1.innerHTML="No recent update";
  990. //saveReview is a generic name for saving content of the html page to a list entry
  991. let that=this;
  992. input.onclick=function(){that.saveReview(listName,cell1.id,setup)};
  993. }
  994. crfVisit.setEntryFromElement=
  995. function(entry,elementId, field){
  996. //set value to entry from element using representation (field) from labkey
  997. //
  998. //
  999. let fName='setEntryFromElement';
  1000. let config=this.config;
  1001. let el=this.getElement(elementId);
  1002. if (!el) {
  1003. this.print(fName+" element: "+elementId+" not found");
  1004. return;
  1005. }
  1006. this.print(fName+" element: "+elementId);
  1007. let vName=field.name;
  1008. let vType=field.type;
  1009. let eType=el.nodeName.toLowerCase();
  1010. if (eType==="select"){
  1011. entry[vName]=el.options[el.selectedIndex].value;
  1012. return;
  1013. }
  1014. if (eType==="td"){
  1015. entry[vName]=el.innerText;
  1016. return;
  1017. }
  1018. if (vType=="date"){
  1019. let date=el.valueAsDate;
  1020. if (!date) return;
  1021. date.setUTCHours(12);
  1022. entry[vName]=date.toString();
  1023. this.print(fName+" setting date to "+entry[vName]);
  1024. return;
  1025. }
  1026. if (vType=="string"){
  1027. entry[vName]=el.value;
  1028. if (vName.search('_file_')<0)
  1029. return;
  1030. //upload file
  1031. let id1=elementId+'_file_';
  1032. let input1=this.getElement(id1);
  1033. this.print(fName+' attachment field: '+input1.value);
  1034. //entry[vName]=el.files[0].stream();
  1035. let ctx=new Object();
  1036. ctx['dirName']='consent';
  1037. ctx['ID']=entry['crfRef'];
  1038. //should point to data container
  1039. ctx['project']=getContainer('data');
  1040. //need ID->crf!
  1041. //assume crfRef will get set before this
  1042. //element is encountered
  1043. this.uploadFile(input1,ctx);
  1044. let fv=el.value;
  1045. let suf=fv.split('.').pop();
  1046. entry[vName]=entry['crfRef']+'.'+suf;
  1047. return;
  1048. }
  1049. if (vType=="float" || vType=="int"){
  1050. entry[vName]=el.value;
  1051. if (vName=="queryName") {
  1052. this.print(fName+' parsing queryName: '+el.innerText);
  1053. entry[vName]=config.formConfig.fields[el.innerText].queryId;
  1054. //use queryMap lookup
  1055. }
  1056. return;
  1057. }
  1058. if (vType=="boolean"){
  1059. entry[vName]=el.checked;
  1060. return;
  1061. }
  1062. return;
  1063. }
  1064. crfVisit.saveReview=
  1065. function(queryName,elementId,setup){
  1066. //loads any queryName
  1067. let debug=true;
  1068. let fName='[saveReview/'+queryName+']';
  1069. this.print(fName+" elementId "+elementId);
  1070. let unique=("unique" in setup);
  1071. //data snapshot
  1072. let fQuerySnapshot=this.getQuerySnapshot(queryName);
  1073. let nRows=fQuerySnapshot.rows.length;
  1074. let mode='insert';
  1075. //data layout
  1076. let queryLayout=this.getQueryLayout(queryName);
  1077. let entry=new Object();
  1078. //determine mode based on entry uniqueness and presence of data
  1079. if (unique && nRows>0){
  1080. entry=fQuerySnapshot.rows[0];
  1081. mode='update';
  1082. }
  1083. this.print(fName+' unique '+unique+' mode '+mode+' nRows '+nRows);
  1084. entry.crfRef=this.getCRFrefData();
  1085. this.print(fName+" set crfRef="+entry.crfRef);
  1086. let fields=queryLayout.fields;
  1087. for (f in fields){
  1088. let field=fields[f];
  1089. this.print(fName+" saveReview field: "+field.name);
  1090. if (field.hidden) continue;
  1091. let vName=field.name;
  1092. let vType=field.type;
  1093. this.print(fName+" vType: "+vType);
  1094. if (vName=="crfRef") continue;
  1095. //need to save queryName for reviewComments
  1096. let eId=setup.getInputId(vName);
  1097. //copy values from form to entry
  1098. this.setEntryFromElement(entry,eId,field);
  1099. //clear field value
  1100. if (!unique) this.clearField(field,setup);
  1101. }
  1102. let that=this;
  1103. let action=function(data){that.updateLastSavedFlag(data,setup,elementId)};
  1104. this.modifyRows(mode,'lists',queryName,[entry],action,this.getContainer('data'));
  1105. }
  1106. crfVisit.updateLastSavedFlag=
  1107. function(data,setup,elementId){
  1108. let fName='[updateLastSavedFlag]';
  1109. let config=this.config;
  1110. this.print(fName+" update last saved flag to "+elementId);
  1111. let el=this.getElement(elementId);
  1112. let dt=new Date();
  1113. el.innerHTML="Last saved "+dt.toString();
  1114. if (data.queryName=="reviewComments"){
  1115. this.updateListDisplay(setup.divReviewListId,"reviewComments",setup.filters,true);
  1116. }
  1117. //refresh stored data!
  1118. let writeMode=!setup.readonlyFlag();
  1119. let that=this;
  1120. if ("unique" in setup)
  1121. this.setData(function (){that.populateTable(data.queryName,writeMode);});
  1122. if ("masterQuery" in setup){
  1123. let ad=config.formConfig.additionalData[setup.masterQuery];
  1124. this.print('Updating list display: '+setup.queryName+'/'+ad.queryName);
  1125. this.updateListDisplay(ad.divQueryName,ad.queryName,ad.filters,false);
  1126. }
  1127. }
  1128. //******************************************upload to database *********************
  1129. crfVisit.onDatabaseUpload=
  1130. function(){
  1131. let fName='[onDatabaseUpload]';
  1132. this.print(fName);
  1133. let config=this.config;
  1134. config.upload=new Object();
  1135. let fc=new Object();
  1136. let pM=this.getIdManager();
  1137. fc.participantId=participantIdManager.getParticipantIdFromCrfEntry(pM);
  1138. this.print(fName+' id '+fc.participantId);
  1139. this.afterParticipantId(fc);
  1140. }
  1141. crfVisit.afterParticipantId=
  1142. function(fc){
  1143. this.print("Setting participantId to "+fc.participantId);
  1144. let config=this.config;
  1145. config.upload.participantId=fc.participantId;
  1146. //another select rows to update all queries from setup
  1147. //just use registration for test
  1148. let formSetupRows=config.formConfig.formSetupRows;
  1149. config.upload.queries=new Array();
  1150. this.print("Form rows: "+formSetupRows.length);
  1151. for (let i=0;i<formSetupRows.length;i++){
  1152. let entry=formSetupRows[i];
  1153. //skip reviews
  1154. if (entry.showFlag=="REVIEW") continue;
  1155. //use lookup table to convert from id to name
  1156. let queryName=config.formConfig.queryMap[entry.queryName];
  1157. config.upload.queries.push({queryName:queryName,queryStatus:"QUEUED"});
  1158. this.print('form ['+i+']='+queryName+' '+entry.showFlag+'/'+entry.showQuery);
  1159. if (entry.showQuery=="NONE")
  1160. continue;
  1161. config.upload.queries.push({queryName:entry.showQuery,queryStatus:"QUEUED"});
  1162. }
  1163. //add reviews
  1164. config.upload.queries.push({queryName:"reviewComments",queryStatus:"QUEUED"});
  1165. config.upload.queryId=0;
  1166. this.copyToDataset();
  1167. }
  1168. crfVisit.copyToDataset=
  1169. function(){
  1170. let fName='[copyToDataset]: ';
  1171. let config=this.config;
  1172. this.print(fName+'['+config.upload.queryId+'/'+config.upload.queries.length+']');
  1173. //watch dog + scheduler
  1174. //
  1175. let that=this;
  1176. //watchdog part
  1177. if (config.upload.queryId==config.upload.queries.length) {
  1178. this.print(fName+'completing');
  1179. let targetStatus=config.formConfig.targetStatus['onDatabaseUpload'];
  1180. let targetRecipient=config.formConfig.targetRecipient['onDatabaseUpload'];
  1181. let action=new Object();
  1182. action.name='onDatabaseUpload';
  1183. let redirect=function(){that.redirect();};
  1184. action.cb=function(data){that.sendEmail(data,targetRecipient,redirect,'Form uploaded');}
  1185. this.updateFlag(targetStatus,action);//Approved
  1186. return;
  1187. }
  1188. //scheduler
  1189. let queryName=config.upload.queries[config.upload.queryId].queryName;
  1190. this.print("copyToDataset["+config.upload.queryId+"/"+
  1191. config.upload.queries.length+"]: "+queryName);
  1192. let filters=[LABKEY.Filter.create('crfRef',this.getCRFref())];
  1193. let action=function(data){that.afterListData(data);};
  1194. this.selectRows('lists',queryName,filters,action,this.getContainer('data'));
  1195. }
  1196. crfVisit.afterListData=
  1197. function(data){
  1198. let fName='[afterListData]: ';
  1199. let config=this.config;
  1200. let queryName=config.upload.queries[config.upload.queryId].queryName;
  1201. this.print(fName+" ["+queryName+"/list]: "+data.rows.length+" entries");
  1202. config.upload.queries[config.upload.queryId].listData=data;
  1203. let id=config.upload.participantId;
  1204. let filters=[LABKEY.Filter.create('crfRef',this.getCRFref()),LABKEY.Filter.create('ParticipantId',id)];
  1205. let that=this;
  1206. let action=function(data){that.afterStudyData(data);};
  1207. this.selectRows('study',queryName,filters,action,this.getContainer('data'));
  1208. }
  1209. crfVisit.afterStudyData=
  1210. function(data){
  1211. let fName='[afterStudyData]: ';
  1212. let config=this.config;
  1213. let queryObj=config.upload.queries[config.upload.queryId];
  1214. queryObj.studyData=data;
  1215. let msg=fName+"["+queryObj.queryName+"/study]: "+data.rows.length+" entries";
  1216. this.print(msg);
  1217. let listRows=queryObj.listData.rows;
  1218. //skip uploading an empty set
  1219. if (listRows.length==0){
  1220. this.printErr("List "+queryObj.queryName+" empty.");
  1221. queryObj.queryStatus="DONE";
  1222. config.upload.queryId+=1;
  1223. //back to watchdog
  1224. this.copyToDataset();
  1225. return;
  1226. }
  1227. let studyRows=queryObj.studyData.rows;
  1228. for (let i=0;i<studyRows.length;i++){
  1229. let entry=studyRows[i];
  1230. //
  1231. if (! (i<listRows.length) ) continue;
  1232. let entryList=listRows[i];
  1233. //keeps study only variables (ParticipantId, SequenceNum)
  1234. for (let f in entryList) {
  1235. entry[f]=entryList[f];
  1236. this.print(fName+"Copying ["+f+"]: "+entry[f]+"/"+entryList[f]);
  1237. }
  1238. }
  1239. this.print(fName+' copying completed');
  1240. if (studyRows.length>0) {
  1241. let that=this;
  1242. let action=function(data){that.afterStudyUpload(data);};
  1243. this.modifyRows('update','study',queryObj.queryName,studyRows,action,this.getContainer('data'));
  1244. this.print(fName+'updateRows sent');
  1245. }
  1246. else{
  1247. let data=new Object();
  1248. data.rows=new Array();
  1249. this.afterStudyUpload(data);
  1250. }
  1251. }
  1252. crfVisit.afterStudyUpload=
  1253. function(data){
  1254. let fName='[afterStudyUpload] ';
  1255. let config=this.config;
  1256. let that=this;
  1257. this.print(fName);
  1258. //let participantField=config.participantField;
  1259. let participantField=config.formConfig.studyData["SubjectColumnName"];
  1260. this.print(fName+' participantField: '+participantField);
  1261. let queryObj=config.upload.queries[config.upload.queryId];
  1262. let queryName=queryObj.queryName;
  1263. this.printErr("Updated "+data.rows.length+" rows to "+queryName);
  1264. let studyRows=queryObj.studyData.rows;
  1265. let listRows=queryObj.listData.rows;
  1266. let rows=new Array();
  1267. //also updating existing rows, if they exist
  1268. for (let i=studyRows.length;i<listRows.length;i++){
  1269. let entry=listRows[i];
  1270. //make sure you have the participantField right
  1271. //
  1272. entry[participantField]=config.upload.participantId;
  1273. entry.crfRef=this.getCRFref();
  1274. entry.SequenceNum=this.getCRFref();
  1275. entry.SequenceNum=entry.SequenceNum % 1000000000;
  1276. if (listRows.length>1){
  1277. entry.SequenceNum+=i/100;
  1278. }
  1279. this.print( "Adding sequence number "+entry.SequenceNum);
  1280. rows.push(entry);
  1281. }
  1282. if (rows.length>0){
  1283. let action=function(data){that.afterListUpload(data);};
  1284. this.insertRows('study',queryName,rows,action,this.getContainer('data'));
  1285. }
  1286. else{
  1287. let data=new Object();
  1288. data.rows=rows;
  1289. this.afterListUpload(data);
  1290. }
  1291. }
  1292. crfVisit.afterListUpload=
  1293. function(data){
  1294. let config=this.config;
  1295. let queryObj=config.upload.queries[config.upload.queryId];
  1296. let queryName=queryObj.queryName;
  1297. this.printErr("Inserted "+data.rows.length+" rows to "+queryName);
  1298. queryObj.queryStatus="DONE";
  1299. config.upload.queryId+=1;
  1300. this.copyToDataset();
  1301. }
  1302. //*************************update for further review *************************
  1303. crfVisit.onUpdateForReview=
  1304. function(){
  1305. let config=this.config;
  1306. let targetStatus=config.formConfig.targetStatus['onUpdateForReview'];
  1307. let targetRecipient=config.formConfig.targetRecipient['onUpdateForReview'];
  1308. let action=new Object();
  1309. action.name='onUpdateForReview';
  1310. let that=this;
  1311. let redirect=function(){that.redirect();};
  1312. action.cb=function(data){that.sendEmail(data,targetRecipient,redirect,'Form updated for review');};
  1313. this.updateFlag(targetStatus,action);
  1314. }
  1315. crfVisit.updateFlag=
  1316. function(flag,action){
  1317. let fName='[updateFlag 1]';
  1318. let config=this.config;
  1319. let entry=config.formConfig.crfEntry;
  1320. entry.FormStatus=flag;
  1321. let uId=config.formConfig.currentUser.UserId;
  1322. entry[config.formConfig.operator]=uId;
  1323. this.print(fName+': Form: '+entry.Form);
  1324. this.print(fName+": set form status to "+entry.FormStatus);
  1325. let that=this;
  1326. let cb=function(data){that.completeWithFlag(data,action);};
  1327. this.modifyRows('update','lists','crfEntry',[entry],cb,this.getContainer('data'));
  1328. }
  1329. crfVisit.completeWithFlag=
  1330. function(data,action){
  1331. let fName='[completeWithFlag]';
  1332. this.print(fName+': nrows '+data.rows.length);
  1333. let fentry=data.rows[0];
  1334. this.print(fName+': form status '+fentry.FormStatus);
  1335. this.print(fName+': form '+fentry.Form);
  1336. let crfStatus=this.createCrfStatus(fentry);
  1337. let config=this.config;
  1338. crfStatus.operator=config.formConfig.operator;
  1339. crfStatus.action=action.name;
  1340. let that=this;
  1341. let cb=function(){that.doNothing();};
  1342. if (action.cb) cb=action.cb;
  1343. this.insertRows('lists','crfStatus',[crfStatus],cb,this.getContainer('data'));
  1344. }
  1345. //************************************************ submit *******************************************
  1346. crfVisit.onSubmit=
  1347. function(){
  1348. //update list storage and change status
  1349. this.hideErr();
  1350. this.clearErr();
  1351. this.printErr("onSubmit");
  1352. let that=this;
  1353. let action=function(){that.verifyData();};
  1354. this.setData(action);
  1355. }
  1356. crfVisit.verifyData=
  1357. function(){
  1358. let fName='[verifyData]';
  1359. let config=this.config;
  1360. let qList=this.getQueryList();
  1361. let that=this;
  1362. let doNothing=function(data){that.doNothing();};
  1363. for (q in qList){
  1364. let qData=this.getQuerySnapshot(q);
  1365. if (q=="reviewComments") continue;
  1366. //copy snapshot to history
  1367. if (qData.rows.length==0){
  1368. this.print(fName+' no rows for '+q);
  1369. }
  1370. else
  1371. this.insertRows('lists',q+'History',qData.rows,doNothing,this.getContainer('data'));
  1372. //if it doesn't have additionalData, it is a sub query
  1373. if (!(q in config.formConfig.additionalData)){
  1374. continue;
  1375. }
  1376. if (qData.rows.length<1){
  1377. this.printErr('Missing entry for query '+q);
  1378. return false;
  1379. }
  1380. }
  1381. //this is necessary only for Generated to Generation completed step
  1382. let actionSettings=config.formConfig.actionSettings['onSubmit'];
  1383. if (variableList.hasVariable(actionSettings,"updateRegistration")){
  1384. this.updateRegistration();
  1385. }
  1386. let targetStatus=config.formConfig.targetStatus['onSubmit'];
  1387. let targetRecipient=config.formConfig.targetRecipient['onSubmit'];
  1388. this.print(fName+' targetStatus: '+targetStatus);
  1389. let finalStep=function(){that.redirect();};
  1390. if (variableList.hasVariable(actionSettings,"finalStep")){
  1391. //set to doNothing to remain on submit window
  1392. if (actionSettings.finalStep=="doNothing"){
  1393. finalStep=doNothing;
  1394. }
  1395. }
  1396. let action=new Object();
  1397. action.name='onSubmit';
  1398. action.cb=function(data){that.sendEmail(data,targetRecipient,finalStep,'Form sumbitted');};
  1399. this.updateFlag(targetStatus,action);
  1400. }
  1401. crfVisit.getEmail=
  1402. function(recipientCode){
  1403. this.print('getEmail w/'+recipientCode);
  1404. let config=this.config;
  1405. let recipients=new Array();
  1406. let typeTo=LABKEY.Message.recipientType.to;
  1407. let create=LABKEY.Message.createRecipient;
  1408. let currentUser=config.formConfig.currentUser;
  1409. let formCreator=config.formConfig.formCreator;
  1410. let currentSite=config.formConfig.currentSite;
  1411. let userRows=config.formConfig.userRows;
  1412. let parentUser=undefined;
  1413. if ("parentCrfData" in config.formConfig){
  1414. let parentCrf=config.formConfig.parentCrfData;
  1415. parentUser=this.getUser(parentCrf.rows[0].UserId,'parentUser');
  1416. }
  1417. let recipientCategories=recipientCode.split(',');
  1418. for (let i=0;i<recipientCategories.length;i++){
  1419. let recipient=recipientCategories[i];
  1420. this.print('Checking '+recipient);
  1421. if (recipient=='crfEditor'){
  1422. this.print('Adding :'+formCreator.Email);
  1423. recipients.push(create(typeTo,formCreator.Email));
  1424. if (parentUser==undefined) continue;
  1425. this.print('Adding :'+parentUser.Email);
  1426. recipients.push(create(typeTo,parentUser.Email));
  1427. continue;
  1428. }
  1429. //Monitor or Sponsor
  1430. let fList=recipient+'s';
  1431. let fRows=config.formConfig[fList];
  1432. for (let i=0;i<fRows.length;i++){
  1433. this.print('Checking '+fRows[i].User+'/'+fRows[i].Site);
  1434. if (fRows[i].Site!=currentSite.siteNumber) continue;
  1435. for (let j=0;j<userRows.length;j++){
  1436. if (userRows[j].UserId!=fRows[i].User) continue;
  1437. this.print('Adding :'+userRows[j].Email);
  1438. recipients.push(create(typeTo,userRows[j].Email));
  1439. break;
  1440. }
  1441. }
  1442. }
  1443. return recipients;
  1444. }
  1445. crfVisit.sendEmail=
  1446. function(data,recipient='crfEditor',cb=null,subj='Form submitted'){
  1447. this.print('sendEmail; recipient: '+recipient);
  1448. let config=this.config;
  1449. let that=this;
  1450. if (!cb)
  1451. cb=function(){that.redirect();};
  1452. let st=config.formConfig.settings;
  1453. let cvar='sendEmail';
  1454. if (cvar in st){
  1455. this.print(cvar+' set to '+st[cvar]);
  1456. if (st[cvar]=='FALSE'){
  1457. this.print('Skipping sending emails');
  1458. cb();
  1459. return;
  1460. }
  1461. }
  1462. if (recipient==null){
  1463. this.print('Skipping sending emails w/ no recipients');
  1464. cb();
  1465. return;
  1466. }
  1467. this.print('send email '+data.rows.length);
  1468. let crf=data.rows[0]['entryId'];
  1469. let formId=data.rows[0]['Form'];
  1470. let link=LABKEY.ActionURL.getBaseURL();
  1471. link+=LABKEY.ActionURL.getContainer();
  1472. link+='/crf_tecant-visit.view?';
  1473. link+='entryId='+crf;
  1474. link+='&formId='+formId;
  1475. link+='&role='+recipient;
  1476. //debug
  1477. let recipients=this.getEmail(recipient);
  1478. //from crfManagers list
  1479. let typeHtml=LABKEY.Message.msgType.html;
  1480. let typePlain=LABKEY.Message.msgType.plain;
  1481. let msg1=LABKEY.Message.createMsgContent(typePlain,link);
  1482. //let cb=doNothing;
  1483. //let cb=redirect;
  1484. LABKEY.Message.sendMessage({
  1485. msgFrom:'labkey@fmf.uni-lj.si',
  1486. msgSubject:subj,
  1487. msgRecipients:recipients,
  1488. msgContent:[msg1],
  1489. success: cb
  1490. });
  1491. }
  1492. crfVisit.hideErr=
  1493. function(){
  1494. let el=this.getElement("errorDiv");
  1495. el.style.display="none";
  1496. }
  1497. crfVisit.clearErr=
  1498. function(){
  1499. let el=this.getElement("errorTxt");
  1500. el.value="";
  1501. }
  1502. crfVisit.showErr=
  1503. function(){
  1504. let el=this.getElement("errorDiv");
  1505. el.style.display="block";
  1506. }
  1507. crfVisit.printErr=
  1508. function(msg){
  1509. this.showErr();
  1510. el=this.getElement("errorTxt");
  1511. el.style.color="red";
  1512. el.value+="\n"+msg;
  1513. }
  1514. //**************************************************
  1515. //
  1516. crfVisit.onRemoveCRF=
  1517. function(){
  1518. let fName='[onRemoveCRF]';
  1519. let config=this.config;
  1520. config.inputListsIterator=0;
  1521. this.print(fName+' starting loop');
  1522. //let rd=function(data){redirect();};
  1523. //let cb=function(){cvInsertRows('lists','crfStatus',[crfStatus],rd,getContainer('data'));};
  1524. let that=this;
  1525. let action=function(){that.redirect();};
  1526. let cb=function(){that.removeCrfEntries(action);};
  1527. this.removeCRFLoop(cb);
  1528. }
  1529. crfVisit.removeCRFLoop=
  1530. function(cb){
  1531. let fName='[removeCRFLoop()]';
  1532. let config=this.config;
  1533. let that=this;
  1534. let i=config.inputListsIterator;
  1535. let iMax=config.formConfig.inputLists.rows.length;
  1536. //in fact, we are adding two additional passages of the loop, one for
  1537. //crfEntry, the second for the same query, but using parentCrf as the
  1538. //selection variable
  1539. //let iTotal=iMax+1;
  1540. //let iTotal=iMax+1;
  1541. //
  1542. let actionSettings=config.formConfig.actionSettings['onRemoveCRF'];
  1543. let queryNameDeleteWithParentCrf='NONE';
  1544. if (variableList.hasVariable(actionSettings,'removeWithParentCrf')){
  1545. queryNameDeleteWithParentCrf=actionSettings['removeWithParentCrf'];
  1546. }
  1547. this.print(fName+" ["+i+"/"+iMax+"]");
  1548. if (!(i<iMax)){
  1549. cb();
  1550. return;
  1551. }
  1552. //in all but crfEntry, variable is called crfRef
  1553. let queryName=config.formConfig.inputLists.rows[i].queryName;
  1554. let idVar="crfRef";
  1555. let idValue=config.formConfig.crfEntry['entryId'];
  1556. //delete also crfEntries where parentCrf is set to crf that we are deleting
  1557. if (queryNameDeleteWithParentCrf==queryName){
  1558. idValue=config.formConfig.crfEntry['parentCrf'];
  1559. }
  1560. this.print(fName+" ["+i+"/"+iMax+"] "+queryName+":"+idVar+'/'+idValue);
  1561. let filters=[LABKEY.Filter.create(idVar,idValue)];
  1562. let action=function(data){that.removeListCRF(data,cb);};
  1563. let failure=function(errorInfo){that.skipListCRF(errorInfo,cb);};
  1564. this.selectRows('lists',queryName,filters,action,this.getContainer('data'),failure);
  1565. //selectRows.failure=skipListCRF;
  1566. }
  1567. crfVisit.removeListCRF=
  1568. function(data,cb){
  1569. let fName="[removeListCRF]";
  1570. let config=this.config;
  1571. let that=this;
  1572. this.print(fName+" "+data.queryName+": "+data.rows.length);
  1573. config.inputListsIterator+=1;
  1574. if (data.rows.length==0){
  1575. this.removeCRFLoop(cb);
  1576. return;
  1577. }
  1578. let action=function(data){that.removeCRFLoop(cb)};
  1579. this.deleteRows(data.schemaName,data.queryName,data.rows,action,this.getContainer('data'));
  1580. }
  1581. crfVisit.skipListCRF=
  1582. function(errorInfo,cb){
  1583. let fName='[skipListCRF]';
  1584. this.print(fName+" error in removeCRF: "+errorInfo.exception);
  1585. let config=this.config;
  1586. config.inputListsIterator+=1;
  1587. this.removeCRFLoop(cb);
  1588. }
  1589. crfVisit.removeCrfEntries=
  1590. function(cb){
  1591. let queryName="crfEntry";
  1592. let idVar="entryId";
  1593. let crfRef=this.getCRFref();
  1594. let that=this;
  1595. let filters=[LABKEY.Filter.create('entryId',crfRef)];
  1596. let action=function(data){that.deleteAndUpdateCrfStatus(data,null);}
  1597. this.selectRows('lists',queryName,filters,action,this.getContainer('CRF'));
  1598. let action1=function(data){that.deleteAndUpdateCrfStatus(data,cb);}
  1599. let filters1=[LABKEY.Filter.create('parentCrf',crfRef)];
  1600. this.selectRows('lists',queryName,filters1,action1,this.getContainer('CRF'));
  1601. }
  1602. crfVisit.deleteAndUpdateCrfStatus=
  1603. function(data,cb){
  1604. let fName='[deleteAndUpdateCrfStatus]';
  1605. let config=this.config;
  1606. let that=this;
  1607. let rows=data.rows;
  1608. let stack=new Array();
  1609. stack.push(cb);
  1610. for (let i=0;i<rows.length;i++){
  1611. //generate crfStatus entry out of crfEntry
  1612. let crfStatus=this.createCrfStatus(rows[i]);
  1613. crfStatus.action='onRemoveCRF';
  1614. crfStatus.FormStatus=config.formConfig.targetStatus[crfStatus.action];
  1615. this.print(fName+' status '+crfStatus.FormStatus);
  1616. crfStatus.operator=config.formConfig.operator;
  1617. let k=stack.length-1;
  1618. let containerPath=this.getContainer('CRF');
  1619. stack.push(function(){that.insertRows('lists','crfStatus',[crfStatus],stack[k],containerPath);});
  1620. let k1=k+1;
  1621. stack.push(function(){that.deleteRows('lists','crfEntry',[rows[i]],stack[k1],containerPath);});
  1622. }
  1623. //execute the whole stack
  1624. let m=stack.length-1;
  1625. stack[m]();
  1626. }
  1627. crfVisit.redirect=
  1628. function(){
  1629. let debug=false;
  1630. let formUrl="begin";
  1631. let params=new Object();
  1632. params.name=formUrl;
  1633. params.pageId="CRF";
  1634. //points to crf container
  1635. let containerPath=this.getContainer('CRF');
  1636. // This changes the page after building the URL.
  1637. //Note that the wiki page destination name is set in params.
  1638. var homeURL = LABKEY.ActionURL.buildURL(
  1639. "project", formUrl , containerPath, params);
  1640. this.print("Redirecting to "+homeURL);
  1641. if (debug) return;
  1642. window.location = homeURL;
  1643. }
  1644. //master section, entry point from html files
  1645. crfVisit.generateMasterForm=
  1646. function(){
  1647. let that=this;
  1648. let action=function(){that.setFormConfig();}
  1649. this.init(action);
  1650. }
  1651. //helper function to set basic parameters on web page
  1652. //(fields defined in html file)
  1653. crfVisit.populateBasicData=
  1654. function(){
  1655. let staticData=new Object();
  1656. let titles=new Object();
  1657. let config=this.config;
  1658. staticData['version']=config.formConfig.softwareVersion;
  1659. titles['version']='Software version';
  1660. let varRows=config.formConfig['crfStaticVariables'].rows;
  1661. for (let i=0;i<varRows.length;i++){
  1662. let vName=varRows[i].staticVariable;
  1663. let val=config.formConfig.crfEntry[vName];
  1664. if (val==undefined) continue;
  1665. staticData[vName]=val;
  1666. titles[vName]=varRows[i].Title;
  1667. }
  1668. staticData['investigatorName']=config.formConfig.user['DisplayName'];
  1669. titles['investigatorName']='Investigator';
  1670. staticData['email']=config.formConfig.user['Email'];
  1671. titles['email']='Email';
  1672. staticData['siteName']=config.formConfig.currentSite['siteName'];
  1673. titles['siteName']='Site';
  1674. staticData['sitePhone']=config.formConfig.currentSite['sitePhone'];
  1675. titles['sitePhone']='Telephone(site)';
  1676. for (f in staticData){
  1677. this.addStaticData(f,titles[f],staticData[f]);
  1678. }
  1679. }
  1680. crfVisit.addStaticData=
  1681. function(f,title,value){
  1682. let el=this.getElement(f);
  1683. //populate only
  1684. if (el!=undefined){
  1685. el.innerText=value;
  1686. return;
  1687. }
  1688. //add row to table if element cannot be found
  1689. let table=this.getElement('staticTable');
  1690. let row=table.insertRow();
  1691. let cell=row.insertCell();
  1692. cell.innerText=title;
  1693. let cell1=row.insertCell();
  1694. cell1.id=f;
  1695. cell1.style.fontWeight='bold';
  1696. //populate
  1697. cell1.innerText=value;
  1698. }
  1699. //come here after the layout is read from labkey page
  1700. //
  1701. crfVisit.generateErrorMsg=
  1702. function(msg){
  1703. let config=this.config;
  1704. let txt=config.document.createElement('p');
  1705. txt.innerText=msg;
  1706. this.getElement(config.masterForm).appendChild(txt);
  1707. this.generateButton("submitDiv",'Exit','Exit','redirect');
  1708. }
  1709. crfVisit.getUser=
  1710. function(id,field){
  1711. let config=this.config;
  1712. if (field in config.formConfig) return config.formConfig[field];
  1713. let uRows=config.formConfig.userRows;
  1714. for (let i=0;i<uRows.length;i++){
  1715. let userId=uRows[i].UserId;
  1716. if (userId!=id) continue;
  1717. config.formConfig[field]=uRows[i];
  1718. return config.formConfig[field];
  1719. }
  1720. return null;
  1721. }
  1722. crfVisit.afterConfig=
  1723. function(){
  1724. let fName='[afterConfig]';
  1725. let config=this.config;
  1726. this.print(fName);
  1727. this.populateBasicData();
  1728. //check if user has permission on the form
  1729. let currentUser=this.getUser(LABKEY.Security.currentUser.id,'currentUser');
  1730. let currentSite=config.formConfig.currentSite;
  1731. let formCreator=this.getUser(config.formConfig.crfEntry.UserId,'formCreator');
  1732. let formCreatorId=formCreator.UserId;
  1733. //let formSite=config.formConfig.crfEntry.Site;
  1734. let fList=config.formConfig.operator+'s';
  1735. let fRows=config.formConfig[fList];
  1736. //let currentSiteId=-1;
  1737. let operatorSites=new Array();
  1738. for (let i=0;i<fRows.length;i++){
  1739. if (fRows[i].User!=currentUser.UserId) continue;
  1740. operatorSites.push(fRows[i].Site);
  1741. }
  1742. //depending on operator mode, we should decide what is right
  1743. let operator=config.formConfig.operator;
  1744. if (operator=='crfEditor'){
  1745. //editor can only edit its own forms
  1746. if (currentUser.UserId!=formCreatorId){
  1747. if ("allowFormReassignment" in config.formConfig.settings){
  1748. if (!operatorSites.includes(currentSite.siteNumber)){
  1749. let msg='User '+currentUser.DisplayName;
  1750. msg+=' has no permission for site '+currentSite.siteName;
  1751. this.generateErrorMsg(msg);
  1752. return;
  1753. }
  1754. let that=this;
  1755. let action=new Object();
  1756. action.name="formReassignement";
  1757. action.cb=function(){that.doNothing();}
  1758. config.formConfig.crfEntry['UserId']=currentUser.UserId;
  1759. let status=config.formConfig.crfEntry['FormStatus'];
  1760. this.updateFlag(status,action);
  1761. }
  1762. else{
  1763. let msg='User '+currentUser.DisplayName;
  1764. msg+=' has no permission on this form';
  1765. this.generateErrorMsg(msg);
  1766. return;
  1767. }
  1768. }
  1769. }
  1770. if (operator=='crfMonitor' || operator=='crfSponsor'){
  1771. //monitor can look at forms based on his site
  1772. //find monitor line
  1773. this.print('operator Site: '+operatorSites.length);
  1774. if (operatorSites.length==0){
  1775. let msg='User '+currentUser.DisplayName;
  1776. msg+=' is not a '+operator;
  1777. this.generateErrorMsg(msg);
  1778. return;
  1779. }
  1780. let selectedSite=-1;
  1781. let siteCandidates="[";
  1782. for (let i=0;i<operatorSites.length;i++){
  1783. if (i>0) siteCandidates+=", ";
  1784. siteCandidates+=operatorSites[i];
  1785. if (operatorSites[i]!=currentSite.siteNumber) continue;
  1786. selectedSite=currentSite.siteNumber;
  1787. break;
  1788. }
  1789. siteCandidates+="]";
  1790. if (selectedSite==-1){
  1791. let msg='User '+currentUser.DisplayName;
  1792. msg+=' is not a '+operator+' for site ';
  1793. msg+=currentSite.siteName+'('+currentSite.siteNumber+')';
  1794. msg+='/'+siteCandidates;
  1795. this.generateErrorMsg(msg);
  1796. return;
  1797. }
  1798. }
  1799. this.print('User '+currentUser.DisplayName+'/'+
  1800. config.formConfig.currentSite['siteName']+
  1801. ' acting as '+config.formConfig.operator);
  1802. let rows=config.formConfig.crfButtons.rows;
  1803. config.formConfig.targetStatus=new Array();
  1804. config.formConfig.targetRecipient=new Array();
  1805. config.formConfig.actionSettings=new Array();
  1806. for (let i=0; i<rows.length; i++){
  1807. let action=rows[i].action;//String
  1808. let tstatus=rows[i].targetFormStatus;
  1809. let trecip=rows[i].targetRecipient;
  1810. config.formConfig.targetStatus[action]=tstatus;
  1811. config.formConfig.targetRecipient[action]=trecip;
  1812. //allow for settings to be promoted with each action (and potentially parsed and acted upon)
  1813. config.formConfig.actionSettings[action]=undefined;
  1814. let aSet=rows[i].actionSettings;
  1815. if (aSet){
  1816. config.formConfig.actionSettings[action]=variableList.parseVariables(aSet);
  1817. variableList.printVariables(this,config.formConfig.actionSettings[action]);
  1818. }
  1819. }
  1820. let formStatus=config.formConfig.formStatus;
  1821. //let functionArray=new Array();
  1822. this.print("Generating buttons for formStatus \""+ formStatus+"\"");
  1823. let allButtonRows=config.formConfig.crfButtons.rows;
  1824. let buttonRows=new Array();
  1825. //specifying role=X in actionSettings will limit button to that role
  1826. for (let i=0;i<allButtonRows.length;i++){
  1827. let action=allButtonRows[i]['action'];
  1828. //filter on actionSettings
  1829. let as=config.formConfig.actionSettings[action];
  1830. if (variableList.hasVariable(as,'role')){
  1831. this.print('Role['+config.formConfig.operator+'/'+as['role']+'] limited for action '+action);
  1832. //mismatch skips addition of button to buttonRows
  1833. if (config.formConfig.operator!=as['role']) continue;
  1834. }
  1835. buttonRows.push(allButtonRows[i]);
  1836. }
  1837. for (let i=0;i<buttonRows.length;i++){
  1838. let bt=buttonRows[i];
  1839. //if (typeof window[bt.action]==="function"){
  1840. this.generateButton("submitDiv",bt.caption,bt.label,bt.action,null);
  1841. //}
  1842. //else{
  1843. // this.print('No match for function :'+bt.action+
  1844. // ' obj: '+window[bt.action]);
  1845. //}
  1846. }
  1847. this.print('Here');
  1848. //here we should get data. For now, just initialize objects that will hold data
  1849. let that=this;
  1850. let action=function(){that.afterDataLayout();};
  1851. this.setDataLayout(action);//callback is afterDataLayout
  1852. }
  1853. crfVisit.afterDataLayout=
  1854. function(){
  1855. let that=this;
  1856. let action=function(){that.afterData();};
  1857. //let action=function(){that.doNothing();};
  1858. this.setData(action);//callback is afterData
  1859. }
  1860. crfVisit.updateRegistration=
  1861. function(){
  1862. let fName="[updateRegistration]";
  1863. let config=this.config;
  1864. this.print(fName);
  1865. let pM=this.getIdManager();
  1866. let idFieldName=participantIdManager.getCrfEntryFieldName(pM,"STUDY");
  1867. //have to reload query data
  1868. let regQueryPars=variableList.parseVariables(config.formConfig.settings['registrationQuery']);
  1869. let regQuery=regQueryPars['query'];
  1870. let fQuery=this.getQuerySnapshot(regQuery);
  1871. if (fQuery.rows.length==0) {
  1872. this.print(fName+" registration is empty");
  1873. return; //registration is empty
  1874. }
  1875. let regEntry=fQuery.rows[0];
  1876. for (x in regEntry){
  1877. this.print(fName+" ["+x+"] "+regEntry[x]);
  1878. }
  1879. let studyId=fQuery.rows[0][idFieldName];
  1880. if (!studyId) {
  1881. this.print(fName+" study id not set ("+idFieldName+'/'+studyId+")");
  1882. return; //study id not set
  1883. }
  1884. //set
  1885. participantIdManager.setParticipantIdToCrfEntry(pM,studyId,"STUDY");
  1886. //this will only update crfEntry in memory, but not on LabKey,
  1887. //we are counting on updateFlag to follow updateRegistration
  1888. //update parentCRF as well, here we schedule update of data entry as well
  1889. if ("parentCrfData" in config.formConfig){
  1890. let parentCrfEntry=config.formConfig.parentCrfData.rows[0];
  1891. parentCrfEntry[idFieldName]=studyId;
  1892. let that=this;
  1893. let action={name:"updateRegistration",cb:function(){that.doNothing();}};
  1894. let cb=function(data){that.completeWithFlag(data,action);};
  1895. this.modifyRows('update','lists','crfEntry',[parentCrfEntry],cb,this.getContainer('CRF'));
  1896. }
  1897. }
  1898. crfVisit.afterData=
  1899. function(){
  1900. let fName='afterData';
  1901. let config=this.config;
  1902. //operatorBasedAccessMode
  1903. let accessMode=config.formConfig.operator+'Mode';
  1904. let rowsSetup=config.formConfig.formSetupRows;
  1905. let idMode=config.formConfig.form['idMode'];
  1906. //set default value if no value is in the list (read value is null)
  1907. if (!idMode) idMode="STUDY:EDIT";
  1908. this.print(fName+': idMode '+idMode);
  1909. //add print to config so participantManager can use it
  1910. let pM=this.getIdManager();
  1911. //extend object
  1912. let that=this;
  1913. let action=new Object();
  1914. action.name='updateCrfEntry';
  1915. action.cb=function(){that.doNothing();};
  1916. pM.updateCrfEntry=function(){that.updateFlag(config.formConfig.crfEntry['FormStatus'],action);};
  1917. let idModeArray=idMode.split(':');
  1918. pM.mode="STUDY";
  1919. if (idModeArray.includes("LOCAL")) {
  1920. pM.mode="LOCAL";
  1921. //OK, but check if CRF or registration indicate that study id is already set
  1922. participantIdManager.verifyCrfStudyId(pM);
  1923. //study id should already be set by updateRegistration
  1924. //verifyRegistration(pM);
  1925. }
  1926. if (idModeArray.includes("READONLY")){
  1927. pM.readOnly="TRUE";
  1928. }
  1929. let pId=participantIdManager.getParticipantIdFromCrfEntry(pM);
  1930. if (!pId){
  1931. participantIdManager.setEditMode(pM);
  1932. }
  1933. else{
  1934. let label=pId;
  1935. if (pM.mode=="STUDY"){
  1936. let loc=participantIdManager.getParticipantIdFromCrfEntry(pM,'LOCAL');
  1937. label=pId+':'+loc;
  1938. pM.readOnly="true";
  1939. }
  1940. participantIdManager.setLabelMode(pM,label);
  1941. //in STUDY mode also change LOCAL ID from crfEntry
  1942. }
  1943. for (let i=0;i<rowsSetup.length;i++){
  1944. let entry=rowsSetup[i];
  1945. let queryName=config.formConfig.queryMap[entry['queryName']];
  1946. this.print(fName+" ["+queryName+"]: showFlag: "+entry["showFlag"]);
  1947. this.print(fName+" ["+queryName+"]: accessMode: "+entry[accessMode]);
  1948. const nData=this.getQuerySnapshot(queryName).rows.length;
  1949. this.print(fName+" ["+queryName+"]: nData: "+nData);
  1950. //skip sections
  1951. //also from fields
  1952. if (entry[accessMode]=="NONE") continue;
  1953. //skip readonly empty records
  1954. //if (entry[accessMode]=="READ" && nData==0) continue;
  1955. //let additionalData=new Object();
  1956. //setAdditionalData(additionalData,entry);
  1957. //section fits one dataset/list
  1958. this.generateSection(entry);
  1959. //generateSection(queryName,entry["title"],entry[accessMode],
  1960. // additionalData);
  1961. }
  1962. }
  1963. crfVisit.findSetupRow=
  1964. function(queryName,formId){
  1965. let config=this.config;
  1966. let rowsSetup=config.formConfig.formSetupRows;
  1967. for (let i=0;i<rowsSetup.length;i++){
  1968. let e=rowsSetup[i];
  1969. let queryName1=config.formConfig.queryMap[e['queryName']];
  1970. if (e.formName!=formId) continue;
  1971. if (queryName1!=queryName) continue;
  1972. return e;
  1973. }
  1974. return null;
  1975. }
  1976. crfVisit.populateSection=
  1977. function(queryName){
  1978. let fName='[populateSection/'+queryName+']';
  1979. let config=this.config;
  1980. this.print(fName);
  1981. //old setting
  1982. let formId=config.formId;
  1983. //new setting
  1984. formId=config.formConfig.formId;
  1985. let entry=this.findSetupRow(queryName,formId);
  1986. //ignore names without associated entry in formSetup
  1987. if (entry==undefined){
  1988. this.print(fName+': no matching FormSetup entry found');
  1989. return;
  1990. }
  1991. //populate comes after generate, we should be pretty safe in taking
  1992. //already generated additionalData
  1993. if (!(queryName in config.formConfig.additionalData)){
  1994. this.print(fName+': no additionalData generated for '+queryName);
  1995. return;
  1996. }
  1997. let additionalData=config.formConfig.additionalData[queryName];
  1998. this.print(fName+': using additionalData '+additionalData);
  1999. if ("isReview" in additionalData){
  2000. let action=function(){crfReviewSection.CB();};
  2001. crfReviewSection.generateSection(queryName,queryName,action);
  2002. return;
  2003. }
  2004. let accessMode=config.formConfig.operator+'Mode';
  2005. let aM=entry[accessMode];
  2006. this.print(fName+': accessMode '+aM);
  2007. if (aM!='GENERATE'){
  2008. let writeMode=entry[accessMode]=='EDIT';
  2009. this.print(fName+': mode='+writeMode);
  2010. this.populateTable(queryName,writeMode);
  2011. return;
  2012. }
  2013. //deal with generate
  2014. //
  2015. //already available -> shift to READ mode
  2016. let divTable=queryName+'Table';
  2017. let divObj=this.getElement(divTable);
  2018. let divRev=this.getElement(queryName+'Review');
  2019. let divRLi=this.getElement(queryName+'ReviewList');
  2020. let divGBu=this.getElement(queryName+'GenerateButton');
  2021. this.print('div GBU: '+divGBu);
  2022. divObj.style.display="block";
  2023. divRev.style.display="block";
  2024. divRLi.style.display="block";
  2025. if (divGBu!=undefined) divGBu.style.display="none";
  2026. let nData=this.getQuerySnapshot(queryName).rows.length;
  2027. this.print('['+queryName+']: nrows '+nData);
  2028. if (nData>0){
  2029. this.populateTable(queryName,0);
  2030. return;
  2031. }
  2032. //hide table
  2033. divObj.style.display="none";
  2034. divRev.style.display="none";
  2035. divRLi.style.display="none";
  2036. if (divGBu!=undefined) divGBu.style.display="block";
  2037. //add buttons?
  2038. //is button already generated?
  2039. //populateTable(entry);
  2040. }
  2041. //******* generateQuery infrastructure *********************
  2042. crfVisit.onGenerateQuery=
  2043. function(queryName){
  2044. let fName='[onGenerateQuery]';
  2045. this.print(fName+' '+queryName);
  2046. //
  2047. let config=this.config;
  2048. let cfgRows=config.formConfig.generateConfigData.rows;
  2049. // //queryName to queryId?
  2050. let queryId=config.formConfig.fields[queryName].queryId;
  2051. let cfgRow=undefined;
  2052. for (let i=0;i<cfgRows.length;i++){
  2053. if (cfgRows[i].queryId!=queryId) continue;
  2054. cfgRow=cfgRows[i];
  2055. break;
  2056. }
  2057. if (cfgRow==undefined){
  2058. this.print('generateConfig for queryName['+queryId+']='+queryName+' not found');
  2059. return;
  2060. }
  2061. //add config to the list
  2062. if (!("generateConfig" in config.formConfig)){
  2063. config.formConfig.generateConfig=new Object();
  2064. }
  2065. config.formConfig.generateConfig[queryName]=cfgRow;
  2066. if (!("generateForm" in config.formConfig)){
  2067. config.formConfig.generateForm=new Object();
  2068. }
  2069. //
  2070. let formRows=config.formConfig.formRows;
  2071. let formId=cfgRow.formId;
  2072. for (let i=0;i<formRows.length;i++){
  2073. if (formRows[i].Key==formId) {
  2074. config.formConfig.generateForm[queryName]=formRows[i];
  2075. break;
  2076. }
  2077. }
  2078. //this.print('XcfgRow '+config.formConfig.generateForm[queryName]);
  2079. //
  2080. // //check if all required datasets were at least saved
  2081. this.checkGenerationFields(queryName);
  2082. }
  2083. crfVisit.checkGenerationFields=
  2084. function(queryName){
  2085. let fName='[checkGenerationFields]';
  2086. let config=this.config;
  2087. let genForm=config.formConfig.generateForm[queryName];
  2088. let genCfg=config.formConfig.generateConfig[queryName];
  2089. let mailRecipient=genCfg.emailRecipient;
  2090. //list of queries that are part of Registration form
  2091. this.print(fName);
  2092. this.print(fName+' setRecipient: '+mailRecipient);
  2093. let formId=genForm.Key;
  2094. this.print(fName+" Checking form w/id "+formId);
  2095. let selectGenerationRows=this.selectFormSetupRows(formId);
  2096. //registration rows
  2097. for (let i=0;i<selectGenerationRows.length;i++){
  2098. let row=selectGenerationRows[i];
  2099. let queryId=row.queryName;
  2100. let fQueryName=config.formConfig.queryMap[queryId];
  2101. if (fQueryName==queryName) continue;
  2102. let fQuery=this.getQuerySnapshot(fQueryName);
  2103. this.print('Checking '+fQueryName+' nrows: '+fQuery.rows.length);
  2104. if (fQuery.rows.length==0){
  2105. this.generateError(queryName,fQueryName);
  2106. return;
  2107. }
  2108. }
  2109. this.generateMessage(queryName,'Vailidation OK');
  2110. this.print('callback: set recipient: '+mailRecipient);
  2111. let that=this;
  2112. let cb=function(){that.prepareForm(queryName,formId,mailRecipient);};
  2113. this.generateListEntry(formId,queryName,cb);
  2114. }
  2115. crfVisit.prepareForm=
  2116. function(queryName,formId,mailRecipient){
  2117. let fName="[prepareForm]";
  2118. this.print(fName+' recipient '+mailRecipient);
  2119. //look for existing registration entry
  2120. let that=this;
  2121. let action=function(data){that.generateForm(data,queryName,mailRecipient);};
  2122. let formFilter=LABKEY.Filter.create('Form',formId);
  2123. let parentCrfFilter=LABKEY.Filter.create('parentCrf',this.getCRFref());
  2124. let filters=[formFilter,parentCrfFilter];
  2125. this.selectRows('lists','crfEntry',filters,action,this.getContainer('data'));
  2126. }
  2127. crfVisit.generateError=
  2128. function(queryName,fQueryName){
  2129. let elName=queryName+'GenerateButton'+'_reportField';
  2130. let el=this.getElement(elName);
  2131. el.innerText='Error: '+fQueryName+' was not set';
  2132. el.style.color='red';
  2133. }
  2134. crfVisit.generateMessage=
  2135. function(queryName,msg){
  2136. let elName=queryName+'GenerateButton'+'_reportField';
  2137. let el=this.getElement(elName);
  2138. el.innerText=msg;
  2139. el.style.color='green';
  2140. }
  2141. crfVisit.generateForm=
  2142. function(data,queryName,mailRecipient){
  2143. let fName='[generateForm]';
  2144. this.print(fName+' recipient: '+mailRecipient);
  2145. //
  2146. const nData=data.rows.length;
  2147. this.print(fName+' Registration: '+nData+' rows');
  2148. let config=this.config;
  2149. let formRow=config.formConfig.generateForm[queryName];
  2150. let formCfg=config.formConfig.generateConfig[queryName];
  2151. //we have to generate masterQuery with parentCrf and crfRef
  2152. //and crfEntry with new entryId and parentCrf equal to crfRef
  2153. if (nData>0) {
  2154. this.generateMessage(queryName,'Registration already generated.');
  2155. return;
  2156. }
  2157. let formId=formRow.Key;
  2158. let formName=formRow.formName;
  2159. let crfBase=config.formConfig.crfEntry;
  2160. let crfEntry=new Object();
  2161. //add new reference
  2162. crfEntry.entryId=Date.now();
  2163. crfEntry.parentCrf=this.getCRFref();
  2164. crfEntry["Date"]=new Date();
  2165. crfEntry["View"]="[VIEW]";
  2166. crfEntry.formStatus=1;//In progress
  2167. //checks for both field presence (if not in query, undefined) and field value (if not set, null)
  2168. this.print(fName+' setup status: '+formCfg.formStatus);
  2169. if (formCfg.formStatus){
  2170. crfEntry.formStatus=formCfg.formStatus;
  2171. }
  2172. //get local Id
  2173. let pM=this.getIdManager();
  2174. crfEntry[participantIdManager.getCrfEntryFieldName(pM)]=participantIdManager.getParticipantIdFromCrfEntry(pM);
  2175. // //set other variables
  2176. //requires studyData as part of formConfig
  2177. // let studyData=config.formConfig.studyData;
  2178. this.print('Adding study: '+crfBase.EudraCTNumber);
  2179. crfEntry.EudraCTNumber=crfBase.EudraCTNumber;
  2180. crfEntry.StudyCoordinator=crfBase.StudyCoordinator;
  2181. crfEntry.StudySponsor=crfBase.StudySponsor;
  2182. crfEntry.RegulatoryNumber=crfBase.RegulatoryNumber;
  2183. //
  2184. // //find sponsor for site
  2185. let site=crfBase.Site;
  2186. let crfSponsors=config.formConfig.crfSponsors;
  2187. let users=config.formConfig.userRows;
  2188. for (let i=0;i<crfSponsors.length;i++){
  2189. //this.print('Checking for site '+crfSponsors[i].Site);
  2190. if (crfSponsors[i].Site!=site) continue;
  2191. config.formConfig.sponsorId=crfSponsors[i].User;
  2192. //this.print('Setting id '+config.formConfig.sponsorId);
  2193. //finds first
  2194. break;
  2195. }
  2196. for (let j=0;j<users.length;j++){
  2197. if (config.formConfig.sponsorId!=users[j].UserId) continue;
  2198. config.formConfig.sponsor=users[j];
  2199. //finds first (should be unique)
  2200. break;
  2201. }
  2202. this.print('Selecting '+config.formConfig.sponsor.DisplayName+' as sponsor');
  2203. //different user than the original form...
  2204. //should be set to the study sponsor
  2205. crfEntry.UserId=config.formConfig.sponsor.UserId;
  2206. crfEntry.Site=site;
  2207. // //set formId to one found through registration search
  2208. crfEntry.Form=formId;
  2209. ////
  2210. let crfStatus=this.createCrfStatus(crfEntry);
  2211. crfStatus.operator=config.formConfig.operator;
  2212. crfStatus.action='generateForm';
  2213. let that=this;
  2214. let action=function(){that.doNothing();};
  2215. let cb=function(data){that.sendEmail(data,mailRecipient,action,formName+' generated');}
  2216. let containerPath=this.getContainer('data');
  2217. let pass=function(data){that.insertRows('lists','crfStatus',[crfStatus],cb,containerPath);};
  2218. this.insertRows('lists','crfEntry',[crfEntry],pass,this.getContainer('data'));
  2219. }
  2220. crfVisit.generateListEntry=
  2221. function(formId,queryName,cb){
  2222. //check if registration was already generated
  2223. let config=this.config;
  2224. let formRows=config.formConfig.formRows;
  2225. let qForm=undefined;
  2226. for (let i=0;i<formRows.length;i++){
  2227. if (formRows[i].Key!=formId) continue;
  2228. qForm=formRows[i];
  2229. }
  2230. let nData=this.getQuerySnapshot(queryName).rows.length;
  2231. if (nData>0) return;
  2232. //create new list entry
  2233. let pM=this.getIdManager();
  2234. let e2=new Object();
  2235. e2.crfRef=this.getCRFref();
  2236. e2.registrationStatus=0;
  2237. e2.submissionDate=new Date();
  2238. e2[participantIdManager.getCrfEntryFieldName(pM)]=participantIdManager.getParticipantIdFromCrfEntry(pM);
  2239. this.print('set values');
  2240. this.insertRows('lists',queryName,[e2],cb,this.getContainer('data'));
  2241. }
  2242. // ******************** end form generator (Registration) ********************
  2243. //jump to populate table/generate review, etc defined at the begining of the file
  2244. //entry point from generateMasterForm
  2245. crfVisit.setFormConfig=
  2246. function(){
  2247. let fName="[setFormConfig]";
  2248. let config=this.config;
  2249. //add object to store form related data
  2250. config.formConfig=new Object();
  2251. config.formConfig.softwareVersion='T.15.68';
  2252. this.print(fName+" generateMasterForm");
  2253. //set containers for data and configuration
  2254. //TODO: set this from a query
  2255. //
  2256. this.setContainer('data',LABKEY.ActionURL.getContainer());
  2257. this.setContainer('config',LABKEY.ActionURL.getContainer());
  2258. this.setContainer('CRF',LABKEY.ActionURL.getContainer());
  2259. //this is local data
  2260. let that=this;
  2261. let action=function(data){that.afterSettings(data);};
  2262. this.selectRows('lists','crfSettings',[],action,this.getContainer('CRF'));
  2263. //store form related data to this object
  2264. }
  2265. crfVisit.afterSettings=
  2266. function(data){
  2267. let fName='[afterSettings]';
  2268. let config=this.config;
  2269. config.formConfig.settings=variableList.convertToDictionary(data.rows);
  2270. let st=config.formConfig.settings;
  2271. this.print('afterSettings');
  2272. for (let k in st){
  2273. this.print(fName+'\t'+k+'='+st[k]);
  2274. }
  2275. //if ('dataContainer' in st){
  2276. // setContainer('data',st['dataContainer']);
  2277. //}
  2278. let vname='configContainer';
  2279. if (vname in st){
  2280. this.setContainer('config',st[vname]);
  2281. }
  2282. this.print('Config: '+this.getContainer('config'));
  2283. this.print('Data: '+this.getContainer('data'));
  2284. //use first-> we must first establish link to the rigth crf entry
  2285. let filters=[LABKEY.Filter.create('entryId',this.getCRFrefFirst())];
  2286. let that=this;
  2287. let action=function(data){that.afterCRFEntry(data);};
  2288. this.selectRows('lists','crfEntry',filters,action,this.getContainer('data'));
  2289. }
  2290. crfVisit.afterCRFEntry=
  2291. function(data){
  2292. let config=this.config;
  2293. let fName='[afterCRFEntry]';
  2294. config.formConfig.crfEntry=data.rows[0];
  2295. this.print("Setting crfEntry (x) to "+config.formConfig.crfEntry["entryId"]);
  2296. //for empty records or those with parentCrf not set, parentCrf comes up as null
  2297. //nevertheless, with two equal signs, check against undefined also works
  2298. this.print('parentCrf set to '+config.formConfig.crfEntry.parentCrf);
  2299. this.collectData();
  2300. }
  2301. crfVisit.collectData=
  2302. function(){
  2303. let config=this.config;
  2304. let targetObject=config.formConfig;
  2305. let queryArray=new Array();
  2306. //k
  2307. //site
  2308. queryArray.push(runQuery.makeQuery(targetObject,'config','site','siteData',[]));
  2309. //users
  2310. queryArray.push(runQuery.makeQuery(targetObject,'CRF','users','userData',[]));
  2311. queryArray[queryArray.length-1].schemaName='core';
  2312. //crfEditors
  2313. queryArray.push(runQuery.makeQuery(targetObject,'config','crfEditors','crfEditorsData',[]));
  2314. //crfMonitors
  2315. queryArray.push(runQuery.makeQuery(targetObject,'config','crfMonitors','crfMonitorsData',[]));
  2316. //crfSponsors
  2317. queryArray.push(runQuery.makeQuery(targetObject,'config','crfSponsors','crfSponsorsData',[]));
  2318. //study static data
  2319. queryArray.push(
  2320. runQuery.makeQuery(targetObject,'data','crfStaticVariables','crfStaticVariables',[]));
  2321. queryArray.push(runQuery.makeQuery(targetObject,'data','specialFields','specialFieldsQuery',[]));
  2322. //study
  2323. queryArray.push(runQuery.makeQuery(targetObject,'data','Study','studyDataAll1',[]));
  2324. let e=queryArray[queryArray.length-1];
  2325. //overload schema name
  2326. e.schemaName='study';
  2327. //make sure variables not part of default view are loaded
  2328. //here we should already have read crfStaticVariables table
  2329. e.columns="SubjectColumnName,EudraCTNumber,StudySponsor";
  2330. e.columns+=",StudyCoordinator,RegulatoryNumber";
  2331. //formStatus
  2332. let varLabel='sourceFormStatus';
  2333. let formStatus=config.formConfig.crfEntry['FormStatus'];
  2334. let formFilter=LABKEY.Filter.create('Key',formStatus);
  2335. queryArray.push(
  2336. runQuery.makeQuery(targetObject,'config','FormStatus','formStatusData',[formFilter]));
  2337. //crfButtons
  2338. let statusFilter=LABKEY.Filter.create(varLabel,formStatus);
  2339. queryArray.push(
  2340. runQuery.makeQuery(targetObject,'config','crfButtons','crfButtons',[statusFilter]));
  2341. //Forms
  2342. queryArray.push(runQuery.makeQuery(targetObject,'config','Forms','formData',[]));
  2343. //FormSetup
  2344. queryArray.push(runQuery.makeQuery(targetObject,'config','FormSetup','formSetup',[]));
  2345. //generateConfig
  2346. queryArray.push(
  2347. runQuery.makeQuery(targetObject,'config','generateConfig','generateConfigData',[]));
  2348. //inputLists
  2349. queryArray.push(
  2350. runQuery.makeQuery(targetObject,'config','inputLists','inputLists',[]));
  2351. //parentCrf
  2352. let parentCrf=config.formConfig.crfEntry['parentCrf'];
  2353. if (parentCrf!=undefined){
  2354. let crfFilter=LABKEY.Filter.create('entryId',parentCrf);
  2355. queryArray.push(runQuery.makeQuery(targetObject,'data','crfEntry','parentCrfData',[crfFilter]));
  2356. }
  2357. this.print('running getDataFromQueries');
  2358. let that=this;
  2359. //let action=function(data){that.doNothing();};
  2360. let action=function(){that.addStudyData();};
  2361. runQuery.getDataFromQueries(this,queryArray,action);
  2362. }
  2363. crfVisit.addStudyData=
  2364. function(){
  2365. let fName='addStudyData';
  2366. let config=this.config;
  2367. //convert specialFields to array
  2368. let q=config.formConfig["specialFieldsQuery"].rows;
  2369. config.formConfig.specialFields=variableList.convertToAssociatedArray(q,"fieldUID");
  2370. this.print(fName);
  2371. let queryArray=new Array();
  2372. let targetObject=config.formConfig;
  2373. //study
  2374. queryArray.push(runQuery.makeQuery(targetObject,'data','Study','studyDataAll',[]));
  2375. //queryArray.push(runQuery.makeQuery('data','Study','studyDataAll',[]));
  2376. let e=queryArray[queryArray.length-1];
  2377. //overload schema name
  2378. e.schemaName='study';
  2379. //make sure variables not part of default view are loaded
  2380. //here we should already have read crfStaticVariables table
  2381. let staticVarRows=config.formConfig['crfStaticVariables'].rows;
  2382. let columnModel=""
  2383. for (let i=0;i<staticVarRows.length;i++){
  2384. if (i>0) columnModel+=',';
  2385. columnModel+=staticVarRows[i]['staticVariable'];
  2386. }
  2387. e.columns=columnModel;
  2388. //also collect ids already in study
  2389. //registrationQuery should be a dataset
  2390. //since monitors can review late, it might be profitable to use lists
  2391. //rather than study
  2392. let regQueryPars=variableList.parseVariables(config.formConfig.settings['registrationQuery']);
  2393. let regQuery=regQueryPars['query'];
  2394. let regSchema='study';
  2395. if ('schema' in regQueryPars){
  2396. regSchema=regQueryPars['schema'];
  2397. }
  2398. queryArray.push(runQuery.makeQuery(targetObject,'data',regQuery,'registrationData',[]));
  2399. queryArray[queryArray.length-1].schemaName=regSchema;
  2400. let that=this;
  2401. let action=function(){that.fcontinue();};
  2402. runQuery.getDataFromQueries(this,queryArray,action);
  2403. }
  2404. crfVisit.fcontinue=
  2405. function(){
  2406. //debug
  2407. let fName='[fcontinue]';
  2408. let config=this.config;
  2409. let varRows=config.formConfig['crfStaticVariables'].rows;
  2410. let studyVars=config.formConfig['studyDataAll'].rows[0];
  2411. for (let i=0;i<varRows.length;i++){
  2412. let vName=varRows[i].staticVariable;
  2413. this.print(fName+' '+vName+': '+studyVars[vName]);
  2414. }
  2415. //parse site
  2416. config.formConfig.siteRows=config.formConfig.siteData.rows;
  2417. let sRows=config.formConfig.siteRows;
  2418. for (let i=0;i<sRows.length;i++){
  2419. let siteId=sRows[i].siteNumber;
  2420. this.print('site '+siteId);
  2421. if (siteId==config.formConfig.crfEntry.Site){
  2422. config.formConfig.currentSite=sRows[i];
  2423. break;
  2424. }
  2425. }
  2426. //config.formConfig.site=data.rows[0];
  2427. this.print("Setting site name to "+config.formConfig.currentSite.siteName);
  2428. //study
  2429. config.formConfig.studyData=config.formConfig.studyDataAll.rows[0];
  2430. this.print("XSetting participantField to "+
  2431. config.formConfig.studyData["SubjectColumnName"]);
  2432. config.formConfig.crfEditors=config.formConfig.crfEditorsData.rows;
  2433. config.formConfig.crfMonitors=config.formConfig.crfMonitorsData.rows;
  2434. config.formConfig.crfSponsors=config.formConfig.crfSponsorsData.rows;
  2435. config.formConfig.userRows=config.formConfig.userData.rows;
  2436. let uRows=config.formConfig.userRows;
  2437. for (let i=0;i<uRows.length;i++){
  2438. let userId=uRows[i].UserId;
  2439. if (userId==config.formConfig.crfEntry.UserId){
  2440. config.formConfig.user=uRows[i];
  2441. break;
  2442. }
  2443. }
  2444. //config.formConfig.user=data.rows[0];
  2445. this.print("Setting user to "+config.formConfig.user["DisplayName"]);
  2446. let fsRows=config.formConfig.formStatusData.rows;
  2447. config.formConfig.formStatus=fsRows[0].formStatus;
  2448. config.formConfig.operator=config.role;
  2449. //config.formConfig.operator=fsRows[0].operator;
  2450. this.print('Setting operator to: '+config.formConfig.operator);
  2451. config.formConfig.formRows=config.formConfig.formData.rows;
  2452. //point formId to point to form set in crfEntry
  2453. config.formConfig.formId=config.formConfig.crfEntry['Form'];
  2454. //old setting, set from URL in visit.html
  2455. let formId=config.formId;
  2456. //new setting, set from crfEntry
  2457. formId=config.formConfig.formId;
  2458. let formRows=config.formConfig.formRows;
  2459. //filter out the current form
  2460. for (let i=0;i<formRows.length;i++){
  2461. if (formRows[i].Key==formId){
  2462. config.formConfig.form=formRows[i];
  2463. break;
  2464. }
  2465. }
  2466. config.formConfig.formSetupRows=this.selectFormSetupRows(formId);
  2467. this.print("Number of datasets for form ["+formId+"]: "+
  2468. config.formConfig.formSetupRows.length);
  2469. let fields=config.formConfig.formSetup.metaData.fields;
  2470. //get the lookup for queryName column
  2471. let formQueryName='queryName';
  2472. let field="NONE";
  2473. for (f in fields){
  2474. if (fields[f]['name']!=formQueryName) continue;
  2475. field=fields[f];
  2476. break;
  2477. }
  2478. let lookup=field.lookup;
  2479. this.print("Getting dataset names from "+lookup.queryName);
  2480. //inputLists should be in configuration container
  2481. let that=this;
  2482. let action=function(data){that.afterFormDatasets(data);};
  2483. //let action=function(data){that.doNothing();};
  2484. this.selectRows(lookup.schemaName,lookup.queryName,[],action,this.getContainer('config'));
  2485. }
  2486. crfVisit.afterFormDatasets=
  2487. function(data){
  2488. let fName='[afterFormDatasets]';
  2489. this.print(fName+' nrows '+data.rows.length);
  2490. let config=this.config;
  2491. config.formConfig.formDatasets=data;//inputLists
  2492. config.formConfig.fields=new Object();
  2493. config.formConfig.queryMap=new Object();
  2494. config.formConfig.additionalData=new Object();
  2495. let rows=config.formConfig.formSetupRows;
  2496. //should skip report only rows
  2497. for (let i=0;i<rows.length;i++){
  2498. let entry=rows[i];
  2499. let reviewField=(entry['showFlag']=='REVIEW');
  2500. //is the operator set yet?
  2501. let accessMode=config.formConfig.operator+'Mode';
  2502. let skipField=(entry[accessMode]=="NONE");
  2503. let queryId=entry['queryName'];
  2504. let lookupRows=config.formConfig.formDatasets.rows;
  2505. this.print('QueryID['+i+']='+queryId);
  2506. let dentry;
  2507. for (let j=0;j<lookupRows.length;j++){
  2508. if (queryId!=lookupRows[j]['Key']) continue;
  2509. dentry=lookupRows[j];
  2510. break;
  2511. }
  2512. let qName=dentry['queryName'];
  2513. //update list of dataset formConfig is observing (fields/queryMap)
  2514. while (1){
  2515. //review contains no data
  2516. if (reviewField) break;
  2517. if (skipField) break;
  2518. //already in fields
  2519. if (qName in config.formConfig.fields) break;
  2520. config.formConfig.fields[qName]=new Object();
  2521. break;
  2522. }
  2523. while(1){
  2524. //already done
  2525. if (queryId in config.formConfig.queryMap) break;
  2526. config.formConfig.queryMap[queryId]=qName;
  2527. break;
  2528. }
  2529. if (reviewField) continue;
  2530. if (skipField) continue;
  2531. //only do this for real lists
  2532. let field=config.formConfig.fields[qName];
  2533. field.title=entry['title'];
  2534. field.queryId=queryId;
  2535. }
  2536. this.print("List of datasets in form : ");
  2537. for (f in config.formConfig.fields){
  2538. let field=config.formConfig.fields[f];
  2539. this.print("\t"+f+" ID: "+field.queryId+' title '+field.title);
  2540. }
  2541. this.afterConfig();
  2542. }
  2543. //>>>>>>>>>>>>>>>>>new>>>>>>>>>>>>
  2544. crfVisit.setDataLayout=
  2545. function(cb){
  2546. let fName='[setDataLayout]';
  2547. let config=this.config;
  2548. this.print(fName);
  2549. let rowsSetup=config.formConfig.formSetupRows;
  2550. let queryArray=new Array();
  2551. let dS=this.getLayoutObject();//reference only
  2552. let qList=this.getQueryList();
  2553. let qMap=config.formConfig.queryMap;
  2554. //config.formConfig.lookup=new Object();
  2555. for (let i=0;i<rowsSetup.length;i++){
  2556. let entry=rowsSetup[i];
  2557. //skip review rows
  2558. if (entry['showFlag']=='REVIEW')
  2559. continue;
  2560. let queryId=entry['queryName'];
  2561. let q=qMap[queryId];
  2562. queryArray.push(runQuery.makeQuery(dS,'data',q,q,[]));
  2563. qList[q]=0;
  2564. this.print(fName+' adding '+q);
  2565. if (entry['showQuery']!="NONE"){
  2566. let sq=entry['showQuery'];
  2567. queryArray.push(runQuery.makeQuery(dS,'data',sq,sq,[]));
  2568. qList[sq]=0;
  2569. this.print(fName+' adding '+sq);
  2570. }
  2571. }
  2572. //always add reviews
  2573. let q='reviewComments';
  2574. queryArray.push(runQuery.makeQuery(dS,'data',q,q,[]));
  2575. qList[q]=0;
  2576. let that=this;
  2577. let action=function(){that.processLayout(cb);};
  2578. runQuery.getDataFromQueries(this,queryArray,action);
  2579. }
  2580. //this happens after the for loop, so all dataQueries objects are set
  2581. crfVisit.processLayout=
  2582. function(cb){
  2583. let fName='[processLayout]';
  2584. let qList=this.getQueryList();
  2585. //for layouts
  2586. let queryArray=new Array();
  2587. let targetObject=this.getLookupObject();
  2588. let lookupSet=new Object();
  2589. for (let q in qList){
  2590. let qobject=this.getQueryLayout(q);
  2591. this.print(fName+" inspecting layout for "+q+" "+qobject);
  2592. qobject.fields=qobject.metaData.fields;
  2593. qobject.title=this.findTitle(q);
  2594. //check for lookups
  2595. for (let f in qobject.fields){
  2596. //anything else is simple but lookup
  2597. let field=qobject.fields[f];
  2598. if (!("lookup" in field)) continue;
  2599. let lookup=field.lookup;
  2600. let qObject=this.getLookup(lookup.queryName);
  2601. if (qObject) continue;
  2602. //add to list
  2603. let qName=lookup.queryName;
  2604. let qCode=qName+':'+lookup.keyColumn+':'+lookup.displayColumn;
  2605. let e=runQuery.makeQuery(targetObject,'data',qName,qCode,[]);
  2606. //adjust minor settings
  2607. if (lookup.containerPath) e.containerPath=lookup.containerPath;
  2608. e.schemaName=lookup.schemaName;
  2609. e.columns=lookup.keyColumn+','+lookup.displayColumn;
  2610. lookupSet[qCode]=e;
  2611. this.print(fName+' inserting '+qCode);
  2612. }
  2613. }
  2614. for (let x in lookupSet){
  2615. queryArray.push(lookupSet[x]);
  2616. this.print(fName+' adding '+x);
  2617. for (let v in lookupSet[x]){
  2618. this.print(fName+' value ['+v+'] '+lookupSet[x][v]);
  2619. }
  2620. }
  2621. //this.print(fName+' print '+targetObject.print);
  2622. let that=this;
  2623. let action=function(){that.processLookup(cb);};
  2624. this.print(fName+' getDataFromQueries');
  2625. runQuery.getDataFromQueries(this,queryArray,action);
  2626. this.print(fName+' getDataFromQueries done');
  2627. }
  2628. crfVisit.processLookup=
  2629. function(cb){
  2630. let fName="[processLookup]";
  2631. let obj=this.getLookupObject();
  2632. for (let q in obj){
  2633. this.print(fName+" "+q);
  2634. let a=q.split(':');
  2635. if (a.length<3) continue;
  2636. let lookupName=a[0];
  2637. let key=a[1];
  2638. let val=a[2];
  2639. obj[lookupName]=new Object();
  2640. this.print(fName+' adding ['+lookupName+'] '+key+'/'+val);
  2641. let lObject=obj[lookupName];
  2642. lObject.LUT=new Array();//key to value
  2643. lObject.ValToKey=new Array();//value to key
  2644. lObject.keyColumn=key
  2645. lObject.displayColumn=val;
  2646. let qRows=obj[q].rows;
  2647. for (let i=0;i<qRows.length;i++){
  2648. let r=qRows[i];
  2649. this.print(fName+' LUT ['+r[key]+'] '+r[val]);
  2650. lObject.LUT[r[key]]=r[val];
  2651. lObject.ValToKey[r[val]]=r[key];
  2652. }
  2653. }
  2654. cb();
  2655. }
  2656. crfVisit.setData=
  2657. function(cb){
  2658. fName='[setData]';
  2659. let crfMatch=this.getCRFref();
  2660. let config=this.config;
  2661. let parentCrf=config.formConfig.crfEntry['parentCrf'];
  2662. if (parentCrf!=undefined) crfMatch=parentCrf;
  2663. this.print(fName+' form crf ['+this.getCRFref()+'] matching for crfRef='+crfMatch);
  2664. let queryArray=new Array();
  2665. let targetObject=this.getSnapshotObject();
  2666. //collect data and execute callback cb for queries in cb.queryList
  2667. let qList=this.getQueryList();
  2668. for (q in qList){
  2669. let filters=[LABKEY.Filter.create("crfRef",crfMatch)];
  2670. queryArray.push(runQuery.makeQuery(targetObject,'data',q,q,filters));
  2671. }
  2672. runQuery.getDataFromQueries(this,queryArray,cb);
  2673. }
  2674. crfVisit.uploadFile=
  2675. function(inputElement,context){
  2676. //context should have ID and dirName attributes;
  2677. //path will be dirName/ID/fieldName_ID.suf
  2678. //where suf is identical to localPath content picked from
  2679. //inputElement
  2680. this.print('uploadFile: '+inputElement.value+'/');
  2681. if (inputElement.type=="text") return;
  2682. this.print('uploadFile: '+inputElement.files+'/');
  2683. this.print('uploadFile: '+inputElement.files.length+'/');
  2684. if (inputElement.files.length>0){
  2685. let file=inputElement.files[0];
  2686. this.print('uploadFile: '+inputElement.value+'/'+file.size);
  2687. webdav.uploadFile(file,context);
  2688. }
  2689. }
  2690. crfVisit.printForm=
  2691. function(){
  2692. crfPrint.printForm();
  2693. }