123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465 |
- /* Authors : Carlos C. Corrada-Bravo
- David J. Ortiz-Rivera
-
- Organization : Centro de Desarrollo y Consultoria Computacional
- Project : OPASO Material Registry
- File : inventory.js
- Description : Generate inventory table */
-
- /* save inventory data globally */
- var editing,inventory,form,text,timeout,i,next;
- var sort_key = "material";
-
- /* fetch inventory */
- $(document).ready(function(){
- link_gen();
- fetch_inventory();
- });
-
- /* display_form() - animate form */
- function display_form(){
- $(".form-shader").addClass("fade-in");
- $(".edit-form").addClass("slide-down");
- }
-
- /* hide_form() - hide form */
- function hide_form(){
- $(".edit-form").removeClass("slide-down");
- setTimeout(function(){
- $(".form-shader").removeClass("fade-in");
- },250);
- }
-
- /* set_listeners() - listen for events */
- function set_listeners(){
- /* option queries */
- $(".action").click(function(){
- /* extract args */
- var option = $(this).attr("id");
- i = $(this).parent().parent().attr("id");
-
- /* handle by option */
- switch(option){
- case "edit": /* edit row, populate row */
- set_form();
- break;
-
- case "remove": /* delete row, confirm */
- set_confirm("Do you wish to delete entry from inventory?",option);
- break;
-
- default: /* undefined, display alert */
- set_alert("error","Invalid option")
- break;
- }
- });
-
- /* submit query */
- $("#yes").click(function(){
- hide_confirm();
- $.post("/scripts/opaso",{query: 8,lab_id: get_arg("lab_id"),data: JSON.stringify(inventory[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, generate table body */
- $(`#${i}`).remove();
- next -= 1;
- $(".table-total").text(`Total: ${next}`);
-
- default: /* on success/error, display message */
- set_alert(status,response["message"]);
- break;
- }
- });
- });
-
- /* cancel query */
- $("#no").click(hide_confirm);
-
- /* set listener for form submit button */
- $(".submit").click(function(){
- if(get_form()){
- /* submit query */
- hide_form();
- $.post("/scripts/opaso",{query: 6,lab_id: get_arg("lab_id"),mat_id: inventory[i]["material"]["mat_id"],man_id: inventory[i]["manufacturer"]["man_id"],uom: inventory[i]["uom"],capacity: inventory[i]["capacity"],data: JSON.stringify(form)},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 inventory */
- // console.log(inventory[i],form);
- for(var field in form){
- if(field === "mat_name"){
- inventory[i]["material"][field] = form[field];
- }
-
- else if(field === "man_name"){
- inventory[i]["manufacturer"][field] = form[field];
- }
-
- else{
- inventory[i][field] = form[field];
- }
-
- if(field === "sds"){
- $(`#${i}`).find(`td[value=${field}]`).find("span").attr("href",form[field]);
- }
- else{
- $(`#${i}`).find(`td[value=${field}]`).text(form[field]);
- }
- }
- // console.log(inventory[i]);
-
- default: /* on success/error, display message */
- set_alert(status,response["message"]);
- break;
- }
- });
- }
- /* invalid form, display warning */
- else{
- set_alert("error","All fields required");
- }
- });
-
- /* edit cell */
- $(".entry-cell").dblclick(function(){
- /* verify if user is editing */
- if(!editing){
- $(this).height($(this).height());
- $(this).css("padding","0px");
-
- if($(this).attr("value") === "sds"){
- text = $(this).find("span").attr("href");
- }
- else{
- text = $(this).text();
- }
-
- i = $(this).parent().attr("id");
- var textarea = element_gen("textarea",{class: "edit",text: text});
- $(this).html(textarea);
- editing = true;
- }
- });
-
- /* submit cell edit */
- $("body").click(function(event){
- var t = $(event.target).attr("class");
- if(editing && t !== "edit"){
- edit_field();
- }
- });
-
- /* submit cell edit */
- $(document).on("keypress",function(event){
- if(event.which == 13 && editing){
- event.preventDefault();
- edit_field();
- }
- });
-
- /* hide form */
- $(".close-form").click(function(e){
- if(e.target === e.currentTarget){
- hide_form();
- }
- });
-
- /* hide confirm alert */
- $(".hide-confirm").click(function(e){
- if(e.target === e.currentTarget){
- hide_confirm();
- }
- });
- }
-
- /* edit_field() - update material field */
- function edit_field(){
- /* extract text */
- var delta = $(".edit").val();
- var field = $(".edit").parent().attr("value");
-
- if(text != delta){
-
- // console.log(inventory,i,inventory[i]);
-
- let data = {mat_id: inventory[i]["material"]["mat_id"],man_id: inventory[i]["manufacturer"]["man_id"],capacity: inventory[i]["capacity"], uom: inventory[i]["uom"]};
-
- if(field === "capacity"){
- data["prev_capacity"] = text;
- data["new_capacity"] = delta;
- total = inventory[i]["total"];
- data["quantity"] = Math.ceil(total/delta);
- }
-
- else if(field === "total"){
- data["total"] = delta;
- data["quantity"] = Math.ceil(delta/data["capacity"]);
- }
-
- else if(field === "quantity"){
- data["quantity"] = delta;
- data["total"] = delta * data["capacity"];
- }
-
- else if(field === "uom"){
- data["prev_uom"] = text;
- data["new_uom"] = delta;
- }
-
- else{
- data[field] = delta;
- }
-
- /* update field */
- $.post("/scripts/opaso",{query: 5,lab_id: get_arg("lab_id"),data: JSON.stringify(data),field: field},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 entry */
- if(field === "mat_name"){
- inventory[i]["material"][field] = delta;
- }
- else if(field === "man_name"){
- inventory[i]["manufacturer"][field] = delta;
- }
- else if(field === "total"){
- inventory[i]["quantity"] = Math.ceil(delta/inventory[i]["capacity"]);
- $(`#${i}`).find("td[value=quantity]").text(inventory[i]["quantity"]);
-
- }
-
- else if(field === "capacity"){
- total = inventory[i]["total"];
- inventory[i]["quantity"] = Math.ceil(total/delta);
- $(`#${i}`).find("td[value=quantity]").text(inventory[i]["quantity"]);
- }
-
- else if(field === "quantity"){
- inventory[i]["total"] = delta * inventory[i]["capacity"];
- $(`#${i}`).find("td[value=total]").text(inventory[i]["total"]);
- }
-
- if(field === "sds"){
- var icon = element_gen("i",{class: "fas fa-link"});
- var link = element_gen("span",{class: "sds",href: decodeURI(delta),target: "_blank",rel: "noopener noreferrer",childs: {icon}});
- $(".edit").parent().html(link);
- }
-
- else{
- $(".edit").parent().html(delta);
- }
-
- if(field !== "mat_name" && field !== "man_name"){
- inventory[i][field] = delta;
- }
-
- $(".edit").parent().removeAttr("style");
-
-
-
- default: /* on error, display message */
- if(field === "sds"){
- var icon = element_gen("i",{class: "fas fa-link"});
- var link = element_gen("span",{class: "sds",href: decodeURI(text),target: "_blank",rel: "noopener noreferrer",childs: {icon}});
- $(".edit").parent().html(link);
- }
-
- else{
- $(".edit").parent().html(text);
- }
-
- /* reset styling */
- $(".edit").parent().removeAttr("style");
- set_alert(status,response["message"]);
- break;
- }
- });
- }
- else{
- if(field === "sds"){
- var icon = element_gen("i",{class: "fas fa-link"});
- var link = element_gen("span",{class: "sds",href: decodeURI(text),target: "_blank",rel: "noopener noreferrer",childs: {icon}});
- $(".edit").parent().html(link);
- }
- else{
- $(".edit").parent().html(text);
- $(".edit").parent().removeAttr("style");
- }
- }
-
- editing = false;
- }
-
- /* get_form(row: string) - get form data from row */
- function get_form(){
- form = {};
- var valid = true;
-
- $(".material-input").each(function(){
- var id = $(this).attr("id");
- // var s = `.${id}`;
- var field = $(this).attr("name");
-
- /* highlight */
- if(!is_valid($(this).val())){
- // $(s).show();
- valid = false;
- $(this).addClass("invalid");
- }
-
- /* unhighlight */
- else{
- // $(s).hide();
- $(this).removeClass("invalid");
- }
-
- form[field] = $(this).val();
- });
-
- return valid;
- }
- /* set_form() - populate form using inventory data */
- function set_form(){
- for(var field in inventory[i]){
- if(field === "material"){
- var s = "mat_name";
- var val = inventory[i][field][s];
- }
-
- else if(field === "manufacturer"){
- var s = "man_name";
- var val = inventory[i][field][s]
- }
-
- else{
- var s = field;
- var val = inventory[i][field];
- }
-
- $(`input[name=${s}]`).val(val);
- }
-
- display_form();
- }
-
- /* table_gen(inventory: dictionary) - generate table body */
- function table_gen(data){
- /* extract entries */
- for(var d in data){
- /* generate table row */
- let tr = element_gen("tr",{class: "entry-row",id: d});
- for(var field in data[d]){
- switch(field){
- case "material": /* material, extract id */
- var link = element_gen("a",{class: "mat-link",text: data[d][field]["mat_name"],href: `materials?mat_id=${data[d][field]["mat_id"]}`,target: "_blank",rel: "noopener noreferrer"});
- var attributes = {class: "entry-cell",value: "mat_name",mat_id: data[d][field]["mat_id"],childs: {link}};
- break;
-
- case "manufacturer": /* manufacturer, extract id */
- var attributes ={class: "entry-cell",value: "man_name",man_id: data[d][field]["man_id"],text: data[d][field]["man_name"]};
- break;
-
- case "sds": /* generate sds link */
- if(!data[d][field]){
- data[d][field] = "-";
- }
- var icon = element_gen("i",{class: "fas fa-link"});
- var link = element_gen("span",{class: "sds",href: decodeURI(data[d][field]),target: "_blank",rel: "noopener noreferrer",childs: {icon}});
- var attributes = {class: "entry-cell",value: field,childs: {link}};
- break;
-
- default: /* default */
- if(!data[d][field]){
- data[d][field] = "-";
- }
- var attributes = {class: "entry-cell",value: field,text: data[d][field]};
- break;
- }
-
- var td = element_gen("td",attributes);
- tr.append(td);
- }
-
- /* generate actions cell */
- let actions = {edit:{class: "t-icon material-icons",icon: "notes"},remove: {class: "t-icon material-icons",icon: "close"}};
- var td = element_gen("td",{class: "actions-cell"});
-
- /* generate actions */
- 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: d,title: action,childs: {icon}});
- td.append(button);
- }
-
- /* append */
- tr.append(td);
- $("tbody").append(tr);
- }
-
- /* display inventory */
- $(".processing").hide();
- setTimeout(function(){
- set_listeners();
- $(".main-wrapper").show();
- },250);
- }
-
- /* fetch_inventory() - fetch lab inventory */
- function fetch_inventory(){
- /* extract lab id */
- let lab_id = get_arg("lab_id");
- if(is_valid(lab_id)){
- $.post("/scripts/opaso",{query: 4,lab_id: lab_id},function(data){
- // console.log(data);
- /* extract response */
- response = JSON.parse(data);
- status = response["status"];
-
- /* handle by status */
- switch(status){
- case "success": /* on success, generate table */
- inventory = response["lab"]["inventory"];
-
- /* generate inventory table */
- table_gen(inventory);
- next = Object.keys(inventory).length;
- $(".table-total").text(`Total: ${next}`);
- break;
- case "expired": /* on expired, redirect */
- window.location.href = "/?error=session_expired";
- break;
-
- default: /* on error, display message */
- set_alert(status,response["message"]);
- break;
- }
- });
- }
- /* missing args */
- else{
- set_alert("error","One or more arguments missing");
- }
- }
|