Proyecto en colaboración con OPASO

hazards.js 7.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  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 : hazard.js
  6. Description : Generate hazard menu/tables */
  7. /* global variables */
  8. var next = 0;
  9. /* fetch ghs codes */
  10. $(document).ready(fetch_ghs);
  11. /* set_listeners() - listen for events */
  12. function set_listeners(){
  13. /* sort table */
  14. $(".sort").click(function(){
  15. sort_key = $(this).attr("value");
  16. if(sort_key === "mat_name"){
  17. $(".sort-material").show();
  18. $(".sort-alt").hide();
  19. }
  20. else{
  21. $(`.${sort_key}`).show();
  22. $(".sort-material").hide();
  23. }
  24. full_sort(next,sort_key);
  25. });
  26. /* redirect to code page */
  27. $(".ghs").click(function(){
  28. let code = $(this).attr("value");
  29. if(code){
  30. window.location.href = `/hazards?ghs=${code}`;
  31. }
  32. /* display error */
  33. else{
  34. set_alert("error","GHS code not found")
  35. }
  36. });
  37. /* download file */
  38. $(".download-option").click(function(){
  39. /* extract args */
  40. $("html").addClass("wait");
  41. let download_type = $(this).attr("value");
  42. /* generate file */
  43. $.post("/scripts/opaso",{query: 24,download_type: download_type,ghs: get_arg("ghs")},function(data){
  44. // console.log(data);
  45. /* extract response */
  46. let response = JSON.parse(data);
  47. let status = response["status"];
  48. /* handle by status */
  49. switch(status){
  50. case "success": /* on success, generate table */
  51. var link=element_gen("a",{href: response["url"],download: response["file_name"]});
  52. link.click();
  53. break;
  54. case "expired": /* on expired, redirect to index */
  55. window.location.href = "/?error=session_expired";
  56. break;
  57. default: /* on error, display message */
  58. /* display alert */
  59. set_alert(status,response["message"]);
  60. break;
  61. }
  62. $("html").removeClass("wait");
  63. });
  64. });
  65. }
  66. /* table_gen(ghs: object) - generate material table */
  67. function table_gen(ghs){
  68. for(var g in ghs){
  69. let tr = element_gen("tr",{id: g});
  70. for(var field in ghs[g]){
  71. switch(field){
  72. case "material": /* material, extract id */
  73. var link = element_gen("a",{class: "mat-link",text: ghs[g][field]["mat_name"],href: `materials?mat_id=${ghs[g][field]["mat_id"]}`,target: "_blank",rel: "noopener noreferrer"});
  74. var attrs = {value: "mat_name",mat_id: ghs[g][field]["mat_id"],childs: {link}};
  75. break;
  76. case "manufacturer": /* manufacturer, extract id */
  77. var attrs ={value: "man_name",man_id: ghs[g][field]["man_id"],text: ghs[g][field]["man_name"]};
  78. break;
  79. case "laboratory": /* laboratory, extract id */
  80. var link = element_gen("a",{class: "lab-link",text: ghs[g][field]["lab_room"],href: `laboratory?lab_id=${ghs[g][field]["lab_id"]}`,target: "_blank",rel: "noopener noreferrer"});
  81. var attrs = {value: "lab_room",lab_id: ghs[g][field]["lab_id"],childs: {link}};
  82. break;
  83. case "sds": /* generate sds link */
  84. if(!ghs[g][field]){
  85. ghs[g][field] = "-";
  86. }
  87. var icon = element_gen("i",{class: "fas fa-link"});
  88. var link = element_gen("a",{href: decodeURI(ghs[g][field]),target: "_blank",rel: "noopener noreferrer",childs: {icon}});
  89. var attrs = {value: field,childs: {link}};
  90. break;
  91. default: /* default */
  92. if(!ghs[g][field]){
  93. ghs[g][field] = "-";
  94. }
  95. var attrs = {value: field,text: ghs[g][field]};
  96. break;
  97. }
  98. var td = element_gen("td",attrs);
  99. tr.append(td);
  100. }
  101. $("tbody").append(tr);
  102. }
  103. $(".processing").hide();
  104. setTimeout(function(){
  105. set_listeners();
  106. $(".hazard").show();
  107. $(".main-wrapper").show();
  108. },250);
  109. }
  110. /* fetch_ghs() - fetch ghs codes */
  111. function fetch_ghs(){
  112. /* verify if ghs is set */
  113. var ghs = get_arg("ghs");
  114. if(ghs){
  115. /* fetch materials */
  116. $.post("/scripts/opaso",{query: 26,ghs: ghs},function(data){
  117. // console.log(data);
  118. /* extract response */
  119. let response = JSON.parse(data);
  120. let status = response["status"];
  121. /* handle by status */
  122. switch(status){
  123. case "success": /* on success, generate table */
  124. $(".title").text(ghs);
  125. $(".description").text(response["description"]);
  126. table_gen(response["ghs"]);
  127. next = Object.keys(response["ghs"]).length;
  128. $(".table-total").text(`Total: ${next}`);
  129. break;
  130. case "expired": /* on expired, redirect to index */
  131. window.location.href = "/?error=session_expired";
  132. break;
  133. default: /* on error, display message */
  134. set_alert(status,response["message"]);
  135. break;
  136. }
  137. });
  138. }
  139. /* fetch all codes */
  140. else{
  141. $.post("/scripts/opaso",{query: 25},function(data){
  142. // console.log(data);
  143. /* extract response */
  144. let response = JSON.parse(data);
  145. let status = response["status"];
  146. /* handle by status */
  147. switch(status){
  148. case "success": /* on success, generate ghs menu */
  149. ghs = response["ghs"];
  150. for(var code in ghs){
  151. var gc = element_gen("span",{class: "ghs-code",text: code});
  152. var w = element_gen("div",{class: "ghs",value: code,childs: {gc}});
  153. var m = element_gen("div",{class: "ghs-wrapper col-lg-1",value: code,childs: {w}});
  154. $(".ghs-codes").append(m);
  155. }
  156. $(".processing").hide();
  157. setTimeout(function(){
  158. set_listeners();
  159. $(".ghs-codes").show();
  160. $(".main-wrapper").show();
  161. },250);
  162. break;
  163. case "expired": /* on expired, redirect to index */
  164. window.location.href = "/?error=session_expired";
  165. break;
  166. default: /* on error, display message */
  167. set_alert(status,response["message"]);
  168. break;
  169. }
  170. });
  171. }
  172. }