123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191 |
- /* Authors : Carlos C. Corrada-Bravo
- David J. Ortiz-Rivera
-
- Organization : Centro de Desarrollo y Consultoria Computacional
- Project : OPASO Material Registry
- File : offered.js
- Description : Generate offered materials table */
-
- /* global values */
- var i;
- var next = 0;
- var person_id;
- var offered = {};
-
- /* fetch offered materials */
- $(document).ready(fetch_labs);
-
- /* set_listeners() - listen for events */
- function set_listeners(){
- /* confirm request */
- $(".request").click(function(){
- i = $(this).attr("value");
- $(".form-message").text(`Request ${offered[i]["amount"]} of ${offered[i]["material"]["mat_name"]}?`);
- display_form();
- });
-
- /* send request */
- $(".submit").click(function(){
- hide_form();
- let r_lab_id = $(".labs").val();
- $.post("/scripts/opaso",{query: 28,person_id: person_id,lab_id: r_lab_id,request: JSON.stringify(offered[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;
-
- default: /* on success/error, display message */
- set_alert(status,response["message"]);
- break;
- }
- });
- });
-
- /* close form */
- $(".close-form").click(function(e){
- /* prevent child propagation */
- if(e.target !== e.currentTarget){
- return;
- }
- /* hide form */
- hide_form();
- });
- }
-
- /* display_form() - display form */
- function display_form(){
- $(".form-shader").addClass("fade-in");
- $(".lab-form").addClass("slide-down");
- }
-
- /* hide_form() - hide form */
- function hide_form(){
- $(".lab-form").removeClass("slide-down");
- setTimeout(function(){
- $(".form-shader").removeClass("fade-in");
- },250);
- }
-
- /* table_gen(data: dictionary) - generate table body */
- function table_gen(data){
- /* extract entries */
- 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 */
- if(!fs[f]["man_name"]){
- fs[f]["man_name"] = "-";
- }
-
- 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",target: "_blank",rel: "noopener noreferrer",text: fs[f]["mat_name"],href: href});
- var attrs = {value: "mat_name",childs: {link}};
- break;
-
- case "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: "lab_room",childs: {link}};
- break;
-
- default: /* default */
- var attrs = {text: fs[f],value: f};
- break;
- }
-
- var td = element_gen("td",attrs);
- tr.append(td);
- }
-
- let icon =element_gen("i",{class: "request-icon material-icons", text: "notes"});
- let button = element_gen("button",{class: "action request",value: d,title: "request",childs: {icon}});
- var td = element_gen("td",{value: "action",childs:{button}});
- tr.append(td);
- $("tbody").append(tr);
- }
-
- $(".processing").hide();
- setTimeout(function(){
- set_listeners();
- $(".main-wrapper").show();
- },250);
- }
-
- /* fetch_materials() - fetch offered materials */
- function fetch_materials(){
- $.post("/scripts/opaso",{query: 27},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(" ");
- offered = response["offered"];
- person_id = response["person_id"];
- next = Object.keys(offered).length;
- $(".sub-header").text(`As of: ${date}`);
- $(".table-total").text(`Total: ${next}`);
- table_gen(offered);
- 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_labs() - fetch authorized labs */
- function fetch_labs(){
- $.post("/scripts/opaso",{query: 2},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 authorized labs form */
- let auth = response["authorized"];
- for(var lab in auth){
- var option = element_gen("option",{class: "lab",value: auth[lab]["lab_id"],text: auth[lab]["lab_room"]});
- $(".labs").append(option);
- }
-
- fetch_materials();
- 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;
- }
- });
- }
|