123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134 |
- /* Authors : Carlos C. Corrada-Bravo
- David J. Ortiz-Rivera
-
- Organization : Centro de Desarrollo y Consultoria Computacional
- Project : OPASO Material Registry
- File : requests.js
- Description : Generate requests table */
-
- /* global values */
- var i;
- var person_id;
- var requested = {};
- next = 0;
-
- /* fetch requested materials */
- $(document).ready(fetch_requests);
-
- /* set_listeners() - listen for events */
- function set_listeners(){
- /* confirm request */
- $(".action").click(function(){
- i = $(this).attr("value");
- type = $(this).attr("id");
-
- $.post("/scripts/opaso",{query: 30,type: type,material: JSON.stringify(requested[i])},function(data){
- // console.log(data);
- /* extract response */
- let response = JSON.parse(data);
- let status = response["status"];
-
- /* handle by status */
- switch(status){
- case "expired": /* on expired, redirect to index */
- window.location.href = "/?error=session_expired";
- break;
-
- case "success": /* on success, delete row */
- $(`#${i}`).remove();
- next -= 1;
- $(".table-total").text(`Total: ${next}`);
-
- default: /* on success/error, display message */
- set_alert(status,response["message"]);
- break;
- }
- });
-
- });
- }
-
- /* table_gen(data: dictionary) - generate table body */
- function table_gen(data){
- for(var d in data){
- let tr = element_gen("tr",{id: d});
- let fs = data[d];
- for(var f in fs){
- switch(f){
- case "person": /* extract id */
- var attrs = {person_id: fs[f]["person_id"],value: "person_name", text: fs[f]["person_name"]};
- break;
-
- case "manufacturer": /* extract id */
- var attrs = {man_id: fs[f]["man_id"],value: "man_name", text: fs[f]["man_name"]};
- break;
-
- case "material": /* extract id */
- var href = `/materials?mat_id=${fs[f]["mat_id"]}`;
- var link = element_gen("a",{class: "mat-link",text: fs[f]["mat_name"],target: "_blank",rel: "noopener noreferrer",href: href});
- var attrs = {value: "mat_name",childs: {link}};
- break;
-
- case "prev_lab": /* extract id */
- case "new_lab": /* extract id */
- var href = `/laboratory?lab_id=${fs[f]["lab_id"]}`;
- var link = element_gen("a",{class: "lab-link",target: "_blank",rel: "noopener noreferrer",text: fs[f]["lab_room"],href: href});
- var attrs = {value: f,childs: {link}};
- break;
- default: /* default */
- var attrs = {text: fs[f],value: f};
- break;
- }
-
- var td = element_gen("td",attrs);
- tr.append(td);
- }
-
- let ai =element_gen("i",{class: "request-icon material-icons", text: "check"});
- let ri =element_gen("i",{class: "request-icon material-icons", text: "close"});
- let ab = element_gen("button",{class: "action request",value: d,id: "accept",title: "request",childs: {ai}});
- let rb = element_gen("button",{class: "action request",value: d,id: "reject",title: "request",childs: {ri}});
- var td = element_gen("td",{value: "action",childs:{ab,rb}});
- tr.append(td);
- $("tbody").append(tr);
- }
-
- $(".processing").hide();
- setTimeout(function(){
- set_listeners();
- $(".main-wrapper").show();
- },250);
- }
-
-
- /* fetch_requests() - fetch material requests */
- function fetch_requests(){
- $.post("/scripts/opaso",{query: 29},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 date = new Date();
- date = date.toString().split(" ").splice(1,4).join(" ");
- $(".sub-header").text(`As of: ${date}`);
-
- requested = response["requested"];
- next = Object.keys(requested).length;
- $(".table-total").text(`Total: ${next}`);
- table_gen(requested);
- 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;
- }
- });
- }
|