Proyecto en colaboración con OPASO

materials.js 9.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. /* Authors : Carlos C. Corrada-Bravo
  2. David J. Ortiz-Rivera
  3. Organization : Centro de Desarrollo y Consultoria Computacional
  4. Project : OPASO Material Registry
  5. File : materials.js
  6. Description : Generate materials table */
  7. /* extract material id */
  8. let mat_id = get_arg("mat_id");
  9. var next = 0;
  10. /* fetch materials */
  11. $(document).ready(init_material);
  12. /* set_listeners() - listen for events */
  13. function set_listeners(){
  14. /* sort table */
  15. $(".sort").click(function(){
  16. sort_key = $(this).attr("value");
  17. if(sort_key === "mat_name"){
  18. $(".sort-material").show();
  19. $(".sort-alt").hide();
  20. }
  21. else{
  22. $(".sort-alt").show();
  23. $(".sort-material").hide();
  24. }
  25. full_sort(next,sort_key);
  26. });
  27. /* download report */
  28. $(".download-option").click(function(){
  29. /* generate file */
  30. $("html").addClass("wait");
  31. let download_type = $(this).attr("value");
  32. $.post("/scripts/opaso",{query: 24,download_type: download_type},function(data){
  33. // console.log(data);
  34. /* extract args */
  35. let response = JSON.parse(data);
  36. let status = response["status"];
  37. /* handle by status */
  38. switch(status){
  39. case "success": /* on success, generate table */
  40. var link=element_gen("a",{href: response["url"],download: response["file_name"]});
  41. link.click();
  42. break;
  43. case "expired": /* on expired, redirect to index */
  44. window.location.href = "/?error=session_expired";
  45. break;
  46. default: /* on error, display message */
  47. set_alert(status,response["message"]);
  48. break;
  49. }
  50. $("html").removeClass("wait");
  51. });
  52. });
  53. /* fetch material data */
  54. $("td[value=mat_name]").click(function(){
  55. let mat_id = $(this).attr("mat_id");
  56. let mat_name = $(this).text();
  57. /* query server */
  58. $.post("/scripts/opaso",{query: 21,mat_id: mat_id},function(data){
  59. // console.log(data);
  60. /* extract response */
  61. let response = JSON.parse(data);
  62. let status = response["status"];
  63. /* handle by status */
  64. switch(status){
  65. case "success": /* on success, display material data */
  66. /* extract data */
  67. let details = response["details"];
  68. $(".material-data").html("");
  69. for(var d in details){
  70. var r = element_gen("div",{class: "material-data-row"});
  71. for(var a in details[d]){
  72. if(a === "lab"){
  73. let href = `/laboratory?lab_id=${details[d][a]["lab_id"]}`;
  74. let link = element_gen("a",{class: "lab-link",target: "_blank",rel: "noopener noreferrer",text: details[d][a]["lab_room"],href: href});
  75. let data = element_gen("h5",{class: "header material-data-header",childs: {link}});
  76. r.append(data);
  77. }
  78. else{
  79. if(!details[d][a]){
  80. details[d][a] = "-";
  81. }
  82. let data_header = element_gen("h6",{class: "header material-data-sub-header",text: `${a}:`});
  83. let data = element_gen("span",{class: "material-data-text",text: details[d][a]});
  84. let w = element_gen("span",{class: "material-data-text-wrapper",childs: {data_header,data}});
  85. r.append(w);
  86. }
  87. }
  88. $(".material-name").text(mat_name);
  89. $(".material-data").append(r);
  90. }
  91. $(".material-data-shader").addClass("slide-right");
  92. break;
  93. case "expired": /* on expired, redirect */
  94. window.location.href = "/?error=session_expired";
  95. break;
  96. default: /* on error, display message */
  97. set_alert(status,response["message"]);
  98. break;
  99. }
  100. });
  101. });
  102. /* close form */
  103. $(".hide-material-data").click(function(event){
  104. if(event.target === event.currentTarget){
  105. $(".material-data-shader").removeClass("slide-right")
  106. }
  107. });
  108. }
  109. /* table_gen(data: dictionary,identifiers: dictionary) - generate table body */
  110. function table_gen(data,identifiers){
  111. /* extract details */
  112. for(var i in identifiers){
  113. var mat_id = identifiers[i]["mat_id"];
  114. let tr = element_gen("tr",{class: "material-row",id: i});
  115. for(var field in data[mat_id]){
  116. /* handle by field */
  117. switch(field){
  118. case "mat_name":
  119. var attr = {class: "material-cell",value: field,mat_id: mat_id,text: data[mat_id][field]};
  120. break;
  121. case "total":
  122. var total = "";
  123. for(var uom in data[mat_id][field]){
  124. total += `${data[mat_id][field][uom]}${uom} / `;
  125. }
  126. var attr = {class: "material-cell",value: field,text: total.slice(0,-3)};
  127. break;
  128. default:
  129. if(!data[mat_id][field]){
  130. data[mat_id][field] = "-";
  131. }
  132. var attr = {class: "material-cell",value: field,text: data[mat_id][field]};
  133. break;
  134. }
  135. var td = element_gen("td",attr);
  136. tr.append(td);
  137. }
  138. $("tbody").append(tr);
  139. }
  140. $(".processing").hide();
  141. setTimeout(function(){
  142. set_listeners();
  143. $(".main-wrapper").show();
  144. },250);
  145. }
  146. /* fetch_materials(page: int) - fetch material details */
  147. function fetch_materials(page){
  148. /* query server */
  149. $.post("/scripts/opaso",{query: 19,page: page},function(data){
  150. // console.log(data);
  151. /* extract status */
  152. response = JSON.parse(data);
  153. status = response["status"];
  154. /* handle by status */
  155. switch(status){
  156. case "success": /* on success, generate table */
  157. /* extract args */
  158. let materials = response["materials"];
  159. let identifiers = response["identifiers"];
  160. next = Object.keys(identifiers).length;
  161. $(".table-total").text(`Total: ${next}`);
  162. table_gen(materials,identifiers);
  163. break;
  164. case "expired": /* on expired, redirect to index */
  165. window.location.href = "/?error=session_expired";
  166. break;
  167. default: /* on error, display message */
  168. /* display alert */
  169. set_alert(status,response["message"]);
  170. break;
  171. }
  172. });
  173. }
  174. /* init_material() - fetch material data */
  175. function init_material(){
  176. /* material set */
  177. if(is_valid(mat_id)){
  178. $.post("/scripts/opaso",{query: 23,mat_id: mat_id},function(data){
  179. // console.log(data);
  180. /* extract response */
  181. let response = JSON.parse(data);
  182. let status = response["status"];
  183. /* handle by status */
  184. switch(status){
  185. case "success": /* on success, generate table */
  186. /* extract args */
  187. let material = response["material"];
  188. let identifier = response["identifier"];
  189. next = Object.keys(identifier).length;
  190. $(".table-total").text(`Total: ${next}`);
  191. table_gen(material,identifier);
  192. break;
  193. case "expired": /* on expired, redirect to index */
  194. window.location.href = "/?error=session_expired";
  195. break;
  196. default: /* on error, display message */
  197. set_alert(status,response["message"]);
  198. break;
  199. }
  200. });
  201. }
  202. else{
  203. $.post("/scripts/opaso",{query: 20},function(data){
  204. // console.log(data);
  205. /* extract response */
  206. let response = JSON.parse(data);
  207. let status = response["status"];
  208. /* handle by status */
  209. switch(status){
  210. case "success": /* on success, generate table */
  211. /* extract args */
  212. // let total = response["total"];
  213. // next = total;
  214. fetch_materials(0);
  215. break;
  216. case "expired": /* on expired, redirect to index */
  217. window.location.href = "/?error=session_expired";
  218. break;
  219. default: /* on error, display message */
  220. set_alert(status,response["message"])
  221. break;
  222. }
  223. });
  224. }
  225. }