crfPortal.js 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. function print(config,msg){
  2. config.document.getElementById(config.debugId).value+="\n"+msg;
  3. }
  4. function drawForm(par){
  5. populateSourceTable(par); //populateSourceTableData
  6. let tableId="entryTable";
  7. generateTable(par,"formDiv",tableId);
  8. generateRow(par,tableId,"User");//generateTableRow
  9. populateSelectTableEntry(par,"User");
  10. }
  11. function generateQConfig(listName){
  12. let qConfig=new Object();
  13. qConfig.containerPath="TECANT/Data";
  14. qConfig.schemaName="lists";
  15. qConfig.queryName=listName;
  16. return qConfig;
  17. }
  18. function sourceVar(crfEntryName,elementId,sourceName){
  19. let f=new Object();
  20. f.masterSelectVarName=crfEntryName;
  21. f.selectId=elementId;
  22. f.inputType="innerHTML";
  23. f.sourceSelectVarName=sourceName;
  24. return f;
  25. }
  26. function getField(config, data, varName){
  27. let debug=false;
  28. if (debug) print(config, "getField");
  29. let fields=data.metaData.fields;
  30. for (f in fields){
  31. if (debug) print(config,"Checking "+f+": name "+fields[f].name+"/"+varName);
  32. if (fields[f].name!=varName) continue;
  33. return fields[f];
  34. }
  35. return null;
  36. }
  37. function generateHead(config, headDivName,divName,title){
  38. print(config,"generateHead");
  39. let tb=config.document.createElement('table');
  40. tb.className='t2';
  41. let row=tb.insertRow();
  42. let cell=config.document.createElement('th');
  43. row.appendChild(cell);
  44. cell.setAttribute("colspan","4");
  45. cell.style.fontSize="20px";
  46. cell.style.textAlign="center";
  47. let cellData=config.document.createTextNode(title);
  48. cell.appendChild(cellData);
  49. cell=row.insertCell();
  50. cell.style.fontSize="20px";
  51. let input=config.document.createElement("input");
  52. input.type="button";
  53. input.value="Show";
  54. input.id="toggle"+divName+"VisbilityButton";
  55. input.onclick=function(){toggleVisibility(config,divName,input.id)};
  56. cell.appendChild(input);
  57. config.document.getElementById(headDivName).appendChild(tb);
  58. print(config,"generateHead: Done");
  59. }
  60. function toggleVisibility(config,divName,buttonName){
  61. let x = config.document.getElementById(divName);
  62. if (x.style.display === "none") {
  63. x.style.display = "block";
  64. config.document.getElementById(buttonName).value="Hide";
  65. } else {
  66. x.style.display = "none";
  67. config.document.getElementById(buttonName).value="Show";
  68. }
  69. }
  70. function selectRowsSuccess(config,data){
  71. print(config,"Select rows on "+data.queryName+" got "+data.rows.length+" rows.");
  72. }
  73. function selectRowsFailure(config,errorObj){
  74. print(config,"selectRowsFail: "+errorObj.exception)
  75. }
  76. function populateSourceTable(par){
  77. let debug=true;
  78. if (debug){
  79. print(par.config,"populateSourceTable: Starting");
  80. }
  81. let config=generateQConfig(par.source.queryName);
  82. config.schemaName=par.source.schemaName;
  83. if (!("source" in par)) return;
  84. if (debug){
  85. print(par.config,"populateSourceTable ["+par.source.queryName+"]");
  86. }
  87. config.success=function(data){populateSourceTableData(data,par)};
  88. //config.success=function(data){selectRowsSuccess(par.config,data)};
  89. config.failure=function(errorObj){selectRowsFailure(par.config,errorObj)};
  90. LABKEY.Query.selectRows(config);
  91. }
  92. function populateSourceTableData(data,par){
  93. let debug=true;
  94. if (debug){
  95. print(par.config, "populateSourceTableData: nrow: "+data.rows.length);
  96. }
  97. let entry=data.rows[0];
  98. for (let i=0;i < par.source.vars.length;i++){
  99. let srcVarName=par.source.vars[i];
  100. if (debug){
  101. print(par.config, "populateSourceTable ["+srcVarName+"]");
  102. }
  103. let row=par.vars[srcVarName];
  104. let el=par.config.document.getElementById(row.selectId);
  105. if (debug){
  106. print(par.config, "Element: "+el);
  107. }
  108. el.innerHTML=entry[row.sourceSelectVarName];
  109. }
  110. }
  111. function generateTable(par,divName,elementId){
  112. let debug=true;
  113. if (debug)
  114. print(par.config,"generateTable");
  115. let tb=par.config.document.createElement('table');
  116. tb.className="t2";
  117. tb.id=elementId;
  118. par.config.document.getElementById(divName).appendChild(tb);
  119. if (debug)
  120. print(par.config,"generateTable: Done");
  121. }
  122. function generateRow(par, tableId, rowId){
  123. let debug=true;
  124. if (debug)
  125. print(par.config,"generateRow: Start");
  126. let config=generateQConfig(par.masterQuery);
  127. config.success=function(data){generateTableRow(data, par, tableId, rowId)};
  128. LABKEY.Query.selectRows(config);
  129. if (debug)
  130. print(par.config,"generateRow: End");
  131. return;
  132. }
  133. function generateTableRow(data, par, tableId, rowId){
  134. let debug=true;
  135. if (debug)
  136. print(par.config,"generateTableRow: start");
  137. let tb=par.config.document.getElementById(tableId);
  138. let row=par.vars[rowId];
  139. if (debug)
  140. print(par.config,"getField for "+row.masterSelectVarName);
  141. let field=getField(par.config,data,row.masterSelectVarName);
  142. let trow=tb.insertRow();
  143. let cell=par.config.document.createElement('th');
  144. trow.appendChild(cell);
  145. let text = par.config.document.createTextNode(field.shortCaption);
  146. cell.appendChild(text);
  147. cell=trow.insertCell();
  148. let input = par.config.document.createElement("select");
  149. input.id = row.selectId;
  150. input.onchange=function(){row.callback(par,rowId)};
  151. cell.appendChild(input);
  152. if (debug)
  153. print(par.config,"generateTableRow: end");
  154. }
  155. function populateSelectTableEntry(par,rowId){
  156. let debug=true;
  157. let row=par.vars[rowId];
  158. if (debug)
  159. print(par.config,"populateSelectTableEntry:"+par.masterQuery+"/"+row.masterSelectVarName);
  160. let config=generateQConfig(par.masterQuery);
  161. if ("filter" in row){
  162. //populateSelect on authorizationQuery with authSelectVarName
  163. let filter=row.filter;
  164. if (debug){
  165. print(par.config,"Filter:"+filter.queryName);
  166. print(par.config,"FilterVar "+filter.filterVarName);
  167. }
  168. config.queryName=filter.queryName;
  169. config.filterArray=[];
  170. for (f in filter.filters){
  171. if (debug) print(par.config,"Adding filter: "+f+" val "+filter.filters[f]);
  172. config.filterArray.push(LABKEY.Filter.create(f,filter.filters[f]));
  173. }
  174. }
  175. else{
  176. config.queryName=par.masterQuery;
  177. }
  178. config.success=function(data){populateTableRow(data,par,rowId)};
  179. LABKEY.Query.selectRows(config);
  180. if (debug)
  181. print(par.config,"generateSelect: End");
  182. return;
  183. }
  184. function populateTableRow(data,par,rowId){
  185. //data is output of selectRows on either
  186. // * masterQuery looking at masterSelectVarName or
  187. // * authQuery looking at authSelectVarName
  188. //in both cases, query[varName] is a lookup variable, so do populateSelect with lookupData,queryData and par
  189. let debug=true;
  190. let row=par.vars[rowId];
  191. let varName=row.masterSelectVarName;
  192. if ("filter" in row){
  193. if (row.filter.queryName==data.queryName){
  194. varName=row.filter.filterVarName;
  195. }
  196. }
  197. if (debug)
  198. print(par.config,"generateSelectVar: "+data.queryName+"/"+varName+" size "+data.rows.length);
  199. let field=getField(data,varName);
  200. if (!field) {
  201. print(par.config,"Field "+varName+" not found");
  202. return;
  203. }
  204. if (!("lookup" in field)){
  205. let entry=data.rows[0];
  206. print(par.config,"Field "+varName+" not a lookup");
  207. //populateSelectNotLookup(data,par,rowId, entry);
  208. return;
  209. }
  210. let config=generateQConfig(field.lookup.queryName);
  211. config.schemaName=field.lookup.schemaName;
  212. //config.success=function(lookupData){populateSelect(lookupData,data,par,rowId)};
  213. config.success=function(data){selectRowsSuccess(par.config,data)};
  214. config.failure=function(errorObj){selectRowsFailure(par.config,errorObj)};
  215. LABKEY.Query.selectRows(config);
  216. if (debug)
  217. print(par.config,"generateSelectVar: End");
  218. }
  219. //populateSelect, populateSelectNotLookup
  220. function generateListAndPopulateDaughterSelect(par,rowId){;}
  221. function generateList(par,rowId){;}