Proyecto en colaboración con OPASO

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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 : offered.js
  6. Description : Generate offered materials table */
  7. /* global values */
  8. var i;
  9. var next = 0;
  10. var person_id;
  11. var offered = {};
  12. /* fetch offered materials */
  13. $(document).ready(fetch_labs);
  14. /* set_listeners() - listen for events */
  15. function set_listeners(){
  16. /* confirm request */
  17. $(".request").click(function(){
  18. i = $(this).attr("value");
  19. $(".form-message").text(`Request ${offered[i]["amount"]} of ${offered[i]["material"]["mat_name"]}?`);
  20. display_form();
  21. });
  22. /* send request */
  23. $(".submit").click(function(){
  24. hide_form();
  25. let r_lab_id = $(".labs").val();
  26. $.post("/scripts/opaso",{query: 28,person_id: person_id,lab_id: r_lab_id,request: JSON.stringify(offered[i])},function(data){
  27. // console.log(data);
  28. /* extract response */
  29. let response = JSON.parse(data);
  30. let status = response["status"];
  31. /* handle by status */
  32. switch(status){
  33. case "expired": /* on expired, redirect to index */
  34. window.location.href = "/?error=session_expired";
  35. break;
  36. default: /* on success/error, display message */
  37. set_alert(status,response["message"]);
  38. break;
  39. }
  40. });
  41. });
  42. /* close form */
  43. $(".close-form").click(function(e){
  44. /* prevent child propagation */
  45. if(e.target !== e.currentTarget){
  46. return;
  47. }
  48. /* hide form */
  49. hide_form();
  50. });
  51. }
  52. /* display_form() - display form */
  53. function display_form(){
  54. $(".form-shader").addClass("fade-in");
  55. $(".lab-form").addClass("slide-down");
  56. }
  57. /* hide_form() - hide form */
  58. function hide_form(){
  59. $(".lab-form").removeClass("slide-down");
  60. setTimeout(function(){
  61. $(".form-shader").removeClass("fade-in");
  62. },250);
  63. }
  64. /* table_gen(data: dictionary) - generate table body */
  65. function table_gen(data){
  66. /* extract entries */
  67. for(var d in data){
  68. let tr = element_gen("tr",{id: d});
  69. let fs = data[d];
  70. for(var f in fs){
  71. switch(f){
  72. case "person": /* extract id */
  73. var attrs = {person_id: fs[f]["person_id"],value: "person_name", text: fs[f]["person_name"]};
  74. break;
  75. case "manufacturer": /* extract id */
  76. if(!fs[f]["man_name"]){
  77. fs[f]["man_name"] = "-";
  78. }
  79. var attrs = {man_id: fs[f]["man_id"],value: "man_name", text: fs[f]["man_name"]};
  80. break;
  81. case "material": /* extract id */
  82. var href = `/materials?mat_id=${fs[f]["mat_id"]}`;
  83. var link = element_gen("a",{class: "mat-link",target: "_blank",rel: "noopener noreferrer",text: fs[f]["mat_name"],href: href});
  84. var attrs = {value: "mat_name",childs: {link}};
  85. break;
  86. case "lab": /* extract id */
  87. var href = `/laboratory?lab_id=${fs[f]["lab_id"]}`;
  88. var link = element_gen("a",{class: "lab-link",target: "_blank",rel: "noopener noreferrer",text: fs[f]["lab_room"],href: href});
  89. var attrs = {value: "lab_room",childs: {link}};
  90. break;
  91. default: /* default */
  92. var attrs = {text: fs[f],value: f};
  93. break;
  94. }
  95. var td = element_gen("td",attrs);
  96. tr.append(td);
  97. }
  98. let icon =element_gen("i",{class: "request-icon material-icons", text: "notes"});
  99. let button = element_gen("button",{class: "action request",value: d,title: "request",childs: {icon}});
  100. var td = element_gen("td",{value: "action",childs:{button}});
  101. tr.append(td);
  102. $("tbody").append(tr);
  103. }
  104. $(".processing").hide();
  105. setTimeout(function(){
  106. set_listeners();
  107. $(".main-wrapper").show();
  108. },250);
  109. }
  110. /* fetch_materials() - fetch offered materials */
  111. function fetch_materials(){
  112. $.post("/scripts/opaso",{query: 27},function(data){
  113. // console.log(data);
  114. /* extract response */
  115. let response = JSON.parse(data);
  116. let status = response["status"];
  117. /* handle by status */
  118. switch(status){
  119. case "success": /* on success, generate table */
  120. var date = new Date();
  121. date = date.toString().split(" ").splice(1,4).join(" ");
  122. offered = response["offered"];
  123. person_id = response["person_id"];
  124. next = Object.keys(offered).length;
  125. $(".sub-header").text(`As of: ${date}`);
  126. $(".table-total").text(`Total: ${next}`);
  127. table_gen(offered);
  128. break;
  129. case "expired": /* on expired, redirect to index */
  130. window.location.href = "/?error=session_expired";
  131. break;
  132. default: /* on error, display message */
  133. set_alert(status,response["message"]);
  134. break;
  135. }
  136. });
  137. }
  138. /* fetch_labs() - fetch authorized labs */
  139. function fetch_labs(){
  140. $.post("/scripts/opaso",{query: 2},function(data){
  141. // console.log(data);
  142. /* extract response */
  143. let response = JSON.parse(data);
  144. let status = response["status"];
  145. /* handle by status */
  146. switch(status){
  147. case "success": /* on success, generate authorized labs form */
  148. let auth = response["authorized"];
  149. for(var lab in auth){
  150. var option = element_gen("option",{class: "lab",value: auth[lab]["lab_id"],text: auth[lab]["lab_room"]});
  151. $(".labs").append(option);
  152. }
  153. fetch_materials();
  154. break;
  155. case "expired": /* on expired, redirect to index */
  156. window.location.href = "/?error=session_expired";
  157. break;
  158. default: /* on error, display message */
  159. set_alert(status,response["message"]);
  160. break;
  161. }
  162. });
  163. }