123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203 |
- /* Authors : Carlos C. Corrada-Bravo
- David J. Ortiz-Rivera
-
- Organization : Centro de Desarrollo y Consultoria Computacional
- Project : OPASO Material Registry
- File : hazard.js
- Description : Generate hazard menu/tables */
-
- /* global variables */
- var next = 0;
-
- /* fetch ghs codes */
- $(document).ready(fetch_ghs);
-
- /* set_listeners() - listen for events */
- function set_listeners(){
- /* sort table */
- $(".sort").click(function(){
- sort_key = $(this).attr("value");
-
- if(sort_key === "mat_name"){
- $(".sort-material").show();
- $(".sort-alt").hide();
- }
- else{
- $(`.${sort_key}`).show();
- $(".sort-material").hide();
- }
- full_sort(next,sort_key);
- });
-
- /* redirect to code page */
- $(".ghs").click(function(){
- let code = $(this).attr("value");
- if(code){
- window.location.href = `/hazards?ghs=${code}`;
- }
-
- /* display error */
- else{
- set_alert("error","GHS code not found")
- }
- });
-
- /* download file */
- $(".download-option").click(function(){
- /* extract args */
- $("html").addClass("wait");
- let download_type = $(this).attr("value");
-
- /* generate file */
- $.post("/scripts/opaso",{query: 24,download_type: download_type,ghs: get_arg("ghs")},function(data){
- // console.log(data);
- /* extract response */
- let response = JSON.parse(data);
- let status = response["status"];
-
- /* handle by status */
- switch(status){
- case "success": /* on success, generate table */
- var link=element_gen("a",{href: response["url"],download: response["file_name"]});
- link.click();
- break;
-
- case "expired": /* on expired, redirect to index */
- window.location.href = "/?error=session_expired";
- break;
-
- default: /* on error, display message */
- /* display alert */
- set_alert(status,response["message"]);
- break;
- }
-
- $("html").removeClass("wait");
- });
- });
- }
-
- /* table_gen(ghs: object) - generate material table */
- function table_gen(ghs){
- for(var g in ghs){
- let tr = element_gen("tr",{id: g});
- for(var field in ghs[g]){
- switch(field){
- case "material": /* material, extract id */
- 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"});
- var attrs = {value: "mat_name",mat_id: ghs[g][field]["mat_id"],childs: {link}};
- break;
-
- case "manufacturer": /* manufacturer, extract id */
- var attrs ={value: "man_name",man_id: ghs[g][field]["man_id"],text: ghs[g][field]["man_name"]};
- break;
-
- case "laboratory": /* laboratory, extract id */
- 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"});
- var attrs = {value: "lab_room",lab_id: ghs[g][field]["lab_id"],childs: {link}};
- break;
-
- case "sds": /* generate sds link */
- if(!ghs[g][field]){
- ghs[g][field] = "-";
- }
- var icon = element_gen("i",{class: "fas fa-link"});
- var link = element_gen("a",{href: decodeURI(ghs[g][field]),target: "_blank",rel: "noopener noreferrer",childs: {icon}});
- var attrs = {value: field,childs: {link}};
- break;
-
- default: /* default */
- if(!ghs[g][field]){
- ghs[g][field] = "-";
- }
- var attrs = {value: field,text: ghs[g][field]};
- break;
- }
-
- var td = element_gen("td",attrs);
- tr.append(td);
- }
-
- $("tbody").append(tr);
- }
-
- $(".processing").hide();
- setTimeout(function(){
- set_listeners();
- $(".hazard").show();
- $(".main-wrapper").show();
- },250);
- }
-
- /* fetch_ghs() - fetch ghs codes */
- function fetch_ghs(){
- /* verify if ghs is set */
- var ghs = get_arg("ghs");
- if(ghs){
- /* fetch materials */
- $.post("/scripts/opaso",{query: 26,ghs: ghs},function(data){
- // console.log(data);
- /* extract response */
- let response = JSON.parse(data);
- let status = response["status"];
-
- /* handle by status */
- switch(status){
- case "success": /* on success, generate table */
- $(".title").text(ghs);
- $(".description").text(response["description"]);
- table_gen(response["ghs"]);
- next = Object.keys(response["ghs"]).length;
- $(".table-total").text(`Total: ${next}`);
- break;
-
- case "expired": /* on expired, redirect to index */
- window.location.href = "/?error=session_expired";
- break;
-
- default: /* on error, display message */
- set_alert(status,response["message"]);
- break;
- }
- });
-
- }
-
- /* fetch all codes */
- else{
- $.post("/scripts/opaso",{query: 25},function(data){
- // console.log(data);
- /* extract response */
- let response = JSON.parse(data);
- let status = response["status"];
-
- /* handle by status */
- switch(status){
- case "success": /* on success, generate ghs menu */
- ghs = response["ghs"];
- for(var code in ghs){
- var gc = element_gen("span",{class: "ghs-code",text: code});
- var w = element_gen("div",{class: "ghs",value: code,childs: {gc}});
- var m = element_gen("div",{class: "ghs-wrapper col-lg-1",value: code,childs: {w}});
- $(".ghs-codes").append(m);
- }
-
- $(".processing").hide();
- setTimeout(function(){
- set_listeners();
- $(".ghs-codes").show();
- $(".main-wrapper").show();
- },250);
- break;
-
- case "expired": /* on expired, redirect to index */
- window.location.href = "/?error=session_expired";
- break;
-
- default: /* on error, display message */
- set_alert(status,response["message"]);
- break;
- }
- });
- }
- }
|