123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410 |
- /* Authors : Carlos C. Corrada-Bravo
- David J. Ortiz-Rivera
-
- Organization : Centro de Desarrollo y Consultoria Computacional
- Project : OPASO Material Registry
- File : manage-laboratories.js
- Description : Generate lab table */
-
- /* global values */
- var auth = {};
- var access_levels = {};
- var form_timeout;
- var current_person_id;
- var next;
- var current_lab_id;
- var r;
- var sort_key = "lab_room";
-
- /* set listeners when document is ready */
- $(document).ready(fetch_personnel);
-
- /* display_form(type: string,transaction: string) - configure/display form */
- function display_form(type,transaction){
- let form = `.${type}-form`;
- let submit = `#${type}-submit`;
- $(".form-shader").addClass("fade-in");
- $(form).addClass("c-form");
- $(form).addClass("slide-down");
- $(submit).attr("value",transaction);
- }
-
- /* hideForm(clear: bool) - hide/clear form */
- function hide_form(clear){
- $(".c-form").removeClass("slide-down");
- $(".c-form").removeClass("c-form");
-
- /* wait for animation */
- setTimeout(function(){
- $(".form-shader").removeClass("fade-in");
- $(".laboratories").html("");
- if(clear){
- $(".ap-field").val("");
- }
- },250);
- return false;
- }
-
- /* action() - display laboratory form */
- function action(){
- /* extract args */
- let action = $(this).attr("id");
- r = $(this).parent().parent().attr("id");
- let s = `#${r}`;
- let pi = $(s).find("td[value=pi]").attr("person_id");
- current_lab_id = $(s).find("td[value=lab_room]").attr("lab_id");
-
- /* populate form */
- if(action === "edit"){
- $(".form .ap-field").each(function(){
- $(this).val($(s).find(`td[value=${$(this).attr("id")}]`).text());
- });
-
- /* set pi */
- $(".main-pi").val(pi);
- }
-
- display_form("main",action);
- }
-
- /* edit_lab() - update lab */
- function edit_lab(){
- let pi = $(".main-pi").val();
- let lab_name = $("#lab_name").val();
- let lab_room = $("#lab_room").val();
- let building = $("#building").val();
- var extension = $("#extension").val();
- let department = $("#department").val();
- let person_name = $(".main-pi option:selected").text();
-
- /* validate input */
- if(is_valid(lab_room) && is_valid(lab_name) && is_valid(department) && is_valid(building) && is_valid(pi,"number")){
- /* validate phone number */
- if(!is_valid(extension,"number")){
- extension = "n/a";
- }
-
- /* reset fields */
- $(".invalid-feedback").hide();
- $(".required").removeClass("invalid");
-
- /* update lab */
- $.post("/scripts/opaso",{query: 18,lab_id: current_lab_id,lab_name: lab_name,lab_room: lab_room,department: department,building: building,extension: extension,pi: pi},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, update lab */
- let s = `#${r}`;
-
- // console.log(r,s);
- let lab = {lab_name: lab_name,department: department,building: building,extension: extension};
- $(s).find("td[value=pi]").text(person_name);
- $(s).find("td[value=pi]").attr("person_id",pi);
- $(s).find("td[value=lab_room]").find(".lab-link").text(lab_room);
-
- /* update entries */
- for(var field in lab){
- // console.log($(s).find(`td[value=${field}]`).text());
- $(s).find(`td[value=${field}]`).text(lab[field]);
- }
-
- /* sort table */
- full_sort(next,sort_key);
- hide_form();
-
- default: /* on success/error, display message */
- set_alert(status,response["message"]);
- break;
- }
- });
- }
-
- /* invalid user info */
- else{
- $(".main-required").each(function(){
- var id = $(this).attr("id");
- var s = `.${id}`;
-
- /* highlight */
- if(!is_valid($(this).val(),id)){
- $(this).addClass("invalid");
- $(s).show();
- }
-
- /* unhighlight */
- else{
- $(this).removeClass("invalid");
- $(s).hide();
- }
- });
-
- /* display alert */
- set_alert("warning","Please correct highlighted fields");
- }
- }
-
- /* add_laboratory() - validate input and add laboratory */
- function add_laboratory(){
- /* extract args */
- let pi = $(".add-pi").val();
- let lab_name = $("#add_lab_name").val();
- let lab_room = $("#add_lab_room").val();
- let building = $("#add_building").val();
- var extension = $("#add_extension").val();
- let department = $("#add_department").val();
- let person_name = $(".add-pi option:selected").text();
-
- /* validate input */
- if(is_valid(lab_room) && is_valid(lab_name) && is_valid(department) && is_valid(building) && is_valid(pi,"number")){
- /* validate phone number */
- if(!is_valid(extension,"number")){
- extension = "n/a";
- }
-
- /* reset fields */
- $(".invalid-feedback").hide();
- $(".required").removeClass("invalid");
-
- $.post("/scripts/opaso",{query: 14,lab_name: lab_name,lab_room: lab_room,department: department,building: building,extension: extension,pi: pi},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 */
- window.location.href = "/?error=session_expired";
- break;
-
- case "success": /* on success, generate lab row */
- let lab_id = response["lab_id"];
- let lab = {lab_room: lab_room,lab_name: lab_name,department: department,building: building,extension: extension,person_name: person_name};
-
- /* generate lab row */
- let tr = element_gen("tr",{class: "lab-row",id: next});
- for(var field in lab){
- if(field === "lab_room"){
- var href = `/laboratory?lab_id=${lab_id}`;
- var link = element_gen("a",{class: "lab-link",href: href,target: "_blank",rel: "noopener noreferrer",text: lab[field]});
- var td = element_gen("td",{class: "lab-cell",lab_id: lab_id,value: "lab_room",childs: {link}});
- }
-
- else if(field === "person_name"){
- var td = element_gen("td",{class: "lab-cell",value: "pi",person_id: pi,text: lab[field]});
- }
-
- else{
- var td = element_gen("td",{class: "lab-cell",value: field,text: lab[field]});
- }
-
- tr.append(td);
- }
-
- /* generate actions */
- let actions = {edit:{class: "t-icon material-icons",icon: "notes"}};
- var actions_cell = element_gen("td",{class: "actions-cell"});
- for(var action in actions){
- var icon = element_gen("i",{class: actions[action]["class"],text: actions[action]["icon"]});
- var button = element_gen("button",{class: "button action",id: action,value: action,title: action,childs: {icon}});
- actions_cell.append(button);
- }
-
- /* append */
- tr.append(actions_cell);
- $("tbody").append(tr);
-
- /* sort table */
- next += 1;
- full_sort(next,sort_key);
- $(".table-total").text(`Total: ${next}`);
- hide_form(true);
-
- default: /* on success/error, display message */
- set_alert(status,response["message"]);
- break;
- }
- });
- }
-
- /* invalid user info */
- else{
- $(".required").each(function(){
- var id = $(this).attr("id");
- var s = `.${id}`;
-
- /* highlight */
- if(!is_valid($(this).val(),id)){
- $(this).addClass("invalid");
- $(s).show();
- }
-
- /* highlight */
- else{
- $(this).removeClass("invalid");
- $(s).hide();
- }
- });
-
- /* display alert */
- set_alert("warning","Please correct highlighted fields");
- }
- }
-
- /* set_listeners() - listen for events */
- function set_listeners(){
- /* sorts table */
- $(".sort").click(function(){
- sort_key = $(this).attr("value");
-
- if(sort_key === "lab_room"){
- $(".sort-primary").show();
- $(".sort-alt").hide();
- }
-
- else{
- $(".sort-alt").show();
- $(".sort-primary").hide();
- }
-
- full_sort(next,sort_key);
- });
- /* register user */
- $("#main-submit").click(edit_lab);
-
- /* register user */
- $("#add-submit").click(add_laboratory);
-
- /* handle actions */
- $("tbody").on("click",".action",action);
-
- /* add material to inventory */
- $("#add").click(function(){
- display_form("add","add-material");
- });
-
- /* close form */
- $(".close-form").click(function(e){
- if(e.target === e.currentTarget){
- hide_form(false);
- }
- });
- }
-
- /* table_gen(personnel: dictionary) - generate table body */
- function table_gen(data){
- for(var d in data){
- /* generate lab row */
- let tr = element_gen("tr",{class: "lab-row",id: d});
- for(var field in data[d]){
- if(field === "lab"){
- var href = `/laboratory?lab_id=${data[d][field]["lab_id"]}`;
- var link = element_gen("a",{class: "lab-link",href: href,target: "_blank",rel: "noopener noreferrer",text: data[d][field]["lab_room"]});
- var td = element_gen("td",{class: "lab-cell",lab_id: data[d][field]["lab_id"],value: "lab_room",childs: {link}});
- }
-
- else if(field === "pi"){
- var td = element_gen("td",{class: "lab-cell",value: field,person_id: data[d][field]["person_id"],text: data[d][field]["person_name"]});
- }
-
- else{
- /* correct null entries */
- if(!data[d][field]){
- data[d][field] = "-";
- }
-
- var td = element_gen("td",{class: "lab-cell",value: field,text: data[d][field]});
- }
-
- tr.append(td);
- }
-
- /* generate actions */
- let actions = {edit:{class: "t-icon material-icons",icon: "notes"}};
- var actions_cell = element_gen("td",{class: "actions-cell"});
- for(var action in actions){
- var icon = element_gen("i",{class: actions[action]["class"],text: actions[action]["icon"]});
- var button = element_gen("button",{class: "button action",id: action,value: action,title: action,childs: {icon}});
- actions_cell.append(button);
- }
-
- /* append */
- tr.append(actions_cell);
- $("tbody").append(tr);
- }
-
- $(".processing").hide();
- setTimeout(function(){
- set_listeners();
- $(".main-wrapper").show();
- },250);
- }
-
- /* fetch_personnel() - generate select menu for pi's */
- function fetch_labs(){
- /* query server */
- $.post("/scripts/opaso",{query: 17},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 select */
- /* generate lab table */
- let labs = response["laboratories"];
- next = Object.keys(labs).length;
- table_gen(labs);
- $(".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_personnel() - generate select menu for pi's */
- function fetch_personnel(){
- $.post("/scripts/opaso",{query: 13},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 select */
- let personnel = response["personnel"];
- for(var person in personnel){
- var option = element_gen("option",{class: "option",text: personnel[person]["person_name"],value: personnel[person]["person_id"]});
- $(".pi").append(option);
- }
-
- fetch_labs();
- 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;
- }
- });
- }
|