Proyecto en colaboración con OPASO

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. /* Authors : Carlos C. Corrada-Bravo
  2. David J. Ortiz-Rivera
  3. Organization : Centro de Desarrollo y Consultoria Computacional
  4. Project : OPASO Material Registry
  5. File : transactions.js
  6. Description : Generate transactions table */
  7. /* total transactions */
  8. var next = 0;
  9. /* fetch transactions */
  10. $(document).ready(fetch_transactions);
  11. /* table_gen(transactions: dictionary) - generate and append table row(s) */
  12. function table_gen(transactions){
  13. /* generate rows */
  14. for(var key in transactions){
  15. let tr = element_gen("tr",{class: "transaction-row",id: key});
  16. let fields = transactions[key];
  17. for(var field in fields){
  18. switch(field){
  19. case "material": /* generate material link */
  20. var href = `/materials?mat_id=${fields[field]["mat_id"]}`;
  21. var link = element_gen("a",{class: "mat-link",text: fields[field]["mat_name"],href: href,target: "_blank",rel: "noopener noreferrer"});
  22. var cell = element_gen("td",{class: "transaction-cell",value: "mat_name",childs: {link}});
  23. break;
  24. case "lab": /* generate lab link */
  25. var href = `/laboratory?lab_id=${fields[field]["lab_id"]}`;
  26. var link = element_gen("a",{class: "lab-link",text: fields[field]["lab_room"],href: href,target: "_blank",rel: "noopener noreferrer"});
  27. var cell = element_gen("td",{class: "transaction-cell",value: "lab_room",childs: {link}});
  28. break;
  29. default: /* default */
  30. var cell = element_gen("td",{class: "transaction-cell",text: fields[field],value: field});
  31. break;
  32. }
  33. tr.append(cell);
  34. }
  35. $("tbody").append(tr);
  36. }
  37. $(".processing").hide();
  38. setTimeout(function(){
  39. $(".main-wrapper").show();
  40. },250);
  41. }
  42. /* fetch_transactions() - fetch transactions */
  43. function fetch_transactions(){
  44. $.post("/scripts/opaso",{query: 22},function(data){
  45. console.log(data);
  46. /* extract response */
  47. let response = JSON.parse(data);
  48. let status = response["status"];
  49. /* handle by status */
  50. switch(status){
  51. case "success": /* on success, generate table */
  52. var date = new Date();
  53. date = date.toString().split(" ").splice(1,4).join(" ");
  54. $(".sub-header").text(`As of: ${date}`);
  55. next = Object.keys(response["transactions"]).length;
  56. $(".table-total").text(`Total: ${next}`);
  57. table_gen(response["transactions"]);
  58. break;
  59. case "expired": /* on expired, redirect to index */
  60. window.location.href = "/?error=session_expired";
  61. break;
  62. default: /* on error, display message */
  63. set_alert(status,response["message"]);
  64. break;
  65. }
  66. });
  67. }