123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264 |
- /* Authors : Carlos C. Corrada-Bravo
- David J. Ortiz-Rivera
-
- Organization : Centro de Desarrollo y Consultoria Computacional
- Project : OPASO Material Registry
- File : materials.js
- Description : Generate materials table */
-
- /* extract material id */
- let mat_id = get_arg("mat_id");
- var next = 0;
-
- /* fetch materials */
- $(document).ready(init_material);
-
- /* 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-alt").show();
- $(".sort-material").hide();
- }
- full_sort(next,sort_key);
- });
-
- /* download report */
- $(".download-option").click(function(){
- /* generate file */
- $("html").addClass("wait");
- let download_type = $(this).attr("value");
- $.post("/scripts/opaso",{query: 24,download_type: download_type},function(data){
- // console.log(data);
- /* extract args */
- 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 */
- set_alert(status,response["message"]);
- break;
- }
-
- $("html").removeClass("wait");
- });
- });
-
- /* fetch material data */
- $("td[value=mat_name]").click(function(){
- let mat_id = $(this).attr("mat_id");
- let mat_name = $(this).text();
-
- /* query server */
- $.post("/scripts/opaso",{query: 21,mat_id: mat_id},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, display material data */
- /* extract data */
- let details = response["details"];
- $(".material-data").html("");
- for(var d in details){
- var r = element_gen("div",{class: "material-data-row"});
- for(var a in details[d]){
- if(a === "lab"){
- let href = `/laboratory?lab_id=${details[d][a]["lab_id"]}`;
- let link = element_gen("a",{class: "lab-link",target: "_blank",rel: "noopener noreferrer",text: details[d][a]["lab_room"],href: href});
- let data = element_gen("h5",{class: "header material-data-header",childs: {link}});
- r.append(data);
- }
-
- else{
- if(!details[d][a]){
- details[d][a] = "-";
- }
- let data_header = element_gen("h6",{class: "header material-data-sub-header",text: `${a}:`});
- let data = element_gen("span",{class: "material-data-text",text: details[d][a]});
- let w = element_gen("span",{class: "material-data-text-wrapper",childs: {data_header,data}});
- r.append(w);
-
- }
- }
-
- $(".material-name").text(mat_name);
- $(".material-data").append(r);
- }
-
- $(".material-data-shader").addClass("slide-right");
- break;
-
- case "expired": /* on expired, redirect */
- window.location.href = "/?error=session_expired";
- break;
-
- default: /* on error, display message */
- set_alert(status,response["message"]);
- break;
- }
- });
- });
-
- /* close form */
- $(".hide-material-data").click(function(event){
- if(event.target === event.currentTarget){
- $(".material-data-shader").removeClass("slide-right")
- }
- });
- }
-
- /* table_gen(data: dictionary,identifiers: dictionary) - generate table body */
- function table_gen(data,identifiers){
- /* extract details */
- for(var i in identifiers){
- var mat_id = identifiers[i]["mat_id"];
- let tr = element_gen("tr",{class: "material-row",id: i});
-
- for(var field in data[mat_id]){
-
- /* handle by field */
- switch(field){
- case "mat_name":
- var attr = {class: "material-cell",value: field,mat_id: mat_id,text: data[mat_id][field]};
- break;
-
- case "total":
- var total = "";
- for(var uom in data[mat_id][field]){
- total += `${data[mat_id][field][uom]}${uom} / `;
- }
- var attr = {class: "material-cell",value: field,text: total.slice(0,-3)};
- break;
-
- default:
- if(!data[mat_id][field]){
- data[mat_id][field] = "-";
- }
- var attr = {class: "material-cell",value: field,text: data[mat_id][field]};
- break;
- }
-
- var td = element_gen("td",attr);
- tr.append(td);
- }
-
- $("tbody").append(tr);
- }
-
- $(".processing").hide();
- setTimeout(function(){
- set_listeners();
- $(".main-wrapper").show();
- },250);
- }
-
- /* fetch_materials(page: int) - fetch material details */
- function fetch_materials(page){
- /* query server */
- $.post("/scripts/opaso",{query: 19,page: page},function(data){
- // console.log(data);
- /* extract status */
- response = JSON.parse(data);
- status = response["status"];
-
- /* handle by status */
- switch(status){
- case "success": /* on success, generate table */
- /* extract args */
- let materials = response["materials"];
- let identifiers = response["identifiers"];
- next = Object.keys(identifiers).length;
- $(".table-total").text(`Total: ${next}`);
- table_gen(materials,identifiers);
- 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;
- }
- });
- }
-
- /* init_material() - fetch material data */
- function init_material(){
- /* material set */
- if(is_valid(mat_id)){
- $.post("/scripts/opaso",{query: 23,mat_id: mat_id},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 */
- /* extract args */
- let material = response["material"];
- let identifier = response["identifier"];
- next = Object.keys(identifier).length;
- $(".table-total").text(`Total: ${next}`);
- table_gen(material,identifier);
- 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;
- }
- });
-
- }
- else{
- $.post("/scripts/opaso",{query: 20},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 */
- /* extract args */
- // let total = response["total"];
- // next = total;
- fetch_materials(0);
- 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;
- }
- });
- }
- }
|