Proyecto en colaboración con OPASO

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410
  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 : manage-laboratories.js
  6. Description : Generate lab table */
  7. /* global values */
  8. var auth = {};
  9. var access_levels = {};
  10. var form_timeout;
  11. var current_person_id;
  12. var next;
  13. var current_lab_id;
  14. var r;
  15. var sort_key = "lab_room";
  16. /* set listeners when document is ready */
  17. $(document).ready(fetch_personnel);
  18. /* display_form(type: string,transaction: string) - configure/display form */
  19. function display_form(type,transaction){
  20. let form = `.${type}-form`;
  21. let submit = `#${type}-submit`;
  22. $(".form-shader").addClass("fade-in");
  23. $(form).addClass("c-form");
  24. $(form).addClass("slide-down");
  25. $(submit).attr("value",transaction);
  26. }
  27. /* hideForm(clear: bool) - hide/clear form */
  28. function hide_form(clear){
  29. $(".c-form").removeClass("slide-down");
  30. $(".c-form").removeClass("c-form");
  31. /* wait for animation */
  32. setTimeout(function(){
  33. $(".form-shader").removeClass("fade-in");
  34. $(".laboratories").html("");
  35. if(clear){
  36. $(".ap-field").val("");
  37. }
  38. },250);
  39. return false;
  40. }
  41. /* action() - display laboratory form */
  42. function action(){
  43. /* extract args */
  44. let action = $(this).attr("id");
  45. r = $(this).parent().parent().attr("id");
  46. let s = `#${r}`;
  47. let pi = $(s).find("td[value=pi]").attr("person_id");
  48. current_lab_id = $(s).find("td[value=lab_room]").attr("lab_id");
  49. /* populate form */
  50. if(action === "edit"){
  51. $(".form .ap-field").each(function(){
  52. $(this).val($(s).find(`td[value=${$(this).attr("id")}]`).text());
  53. });
  54. /* set pi */
  55. $(".main-pi").val(pi);
  56. }
  57. display_form("main",action);
  58. }
  59. /* edit_lab() - update lab */
  60. function edit_lab(){
  61. let pi = $(".main-pi").val();
  62. let lab_name = $("#lab_name").val();
  63. let lab_room = $("#lab_room").val();
  64. let building = $("#building").val();
  65. var extension = $("#extension").val();
  66. let department = $("#department").val();
  67. let person_name = $(".main-pi option:selected").text();
  68. /* validate input */
  69. if(is_valid(lab_room) && is_valid(lab_name) && is_valid(department) && is_valid(building) && is_valid(pi,"number")){
  70. /* validate phone number */
  71. if(!is_valid(extension,"number")){
  72. extension = "n/a";
  73. }
  74. /* reset fields */
  75. $(".invalid-feedback").hide();
  76. $(".required").removeClass("invalid");
  77. /* update lab */
  78. $.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){
  79. // console.log(data);
  80. /* extract response */
  81. let response = JSON.parse(data);
  82. let status = response["status"];
  83. /* handle by status */
  84. switch(status){
  85. case "expired": /* on expired, redirect to index */
  86. window.location.href = "/?error=session_expired";
  87. break;
  88. case "success": /* on success, update lab */
  89. let s = `#${r}`;
  90. // console.log(r,s);
  91. let lab = {lab_name: lab_name,department: department,building: building,extension: extension};
  92. $(s).find("td[value=pi]").text(person_name);
  93. $(s).find("td[value=pi]").attr("person_id",pi);
  94. $(s).find("td[value=lab_room]").find(".lab-link").text(lab_room);
  95. /* update entries */
  96. for(var field in lab){
  97. // console.log($(s).find(`td[value=${field}]`).text());
  98. $(s).find(`td[value=${field}]`).text(lab[field]);
  99. }
  100. /* sort table */
  101. full_sort(next,sort_key);
  102. hide_form();
  103. default: /* on success/error, display message */
  104. set_alert(status,response["message"]);
  105. break;
  106. }
  107. });
  108. }
  109. /* invalid user info */
  110. else{
  111. $(".main-required").each(function(){
  112. var id = $(this).attr("id");
  113. var s = `.${id}`;
  114. /* highlight */
  115. if(!is_valid($(this).val(),id)){
  116. $(this).addClass("invalid");
  117. $(s).show();
  118. }
  119. /* unhighlight */
  120. else{
  121. $(this).removeClass("invalid");
  122. $(s).hide();
  123. }
  124. });
  125. /* display alert */
  126. set_alert("warning","Please correct highlighted fields");
  127. }
  128. }
  129. /* add_laboratory() - validate input and add laboratory */
  130. function add_laboratory(){
  131. /* extract args */
  132. let pi = $(".add-pi").val();
  133. let lab_name = $("#add_lab_name").val();
  134. let lab_room = $("#add_lab_room").val();
  135. let building = $("#add_building").val();
  136. var extension = $("#add_extension").val();
  137. let department = $("#add_department").val();
  138. let person_name = $(".add-pi option:selected").text();
  139. /* validate input */
  140. if(is_valid(lab_room) && is_valid(lab_name) && is_valid(department) && is_valid(building) && is_valid(pi,"number")){
  141. /* validate phone number */
  142. if(!is_valid(extension,"number")){
  143. extension = "n/a";
  144. }
  145. /* reset fields */
  146. $(".invalid-feedback").hide();
  147. $(".required").removeClass("invalid");
  148. $.post("/scripts/opaso",{query: 14,lab_name: lab_name,lab_room: lab_room,department: department,building: building,extension: extension,pi: pi},function(data){
  149. // console.log(data);
  150. /* extract response */
  151. let response = JSON.parse(data);
  152. let status = response["status"];
  153. /* handle by status */
  154. switch(status){
  155. case "expired": /* on expired, redirect */
  156. window.location.href = "/?error=session_expired";
  157. break;
  158. case "success": /* on success, generate lab row */
  159. let lab_id = response["lab_id"];
  160. let lab = {lab_room: lab_room,lab_name: lab_name,department: department,building: building,extension: extension,person_name: person_name};
  161. /* generate lab row */
  162. let tr = element_gen("tr",{class: "lab-row",id: next});
  163. for(var field in lab){
  164. if(field === "lab_room"){
  165. var href = `/laboratory?lab_id=${lab_id}`;
  166. var link = element_gen("a",{class: "lab-link",href: href,target: "_blank",rel: "noopener noreferrer",text: lab[field]});
  167. var td = element_gen("td",{class: "lab-cell",lab_id: lab_id,value: "lab_room",childs: {link}});
  168. }
  169. else if(field === "person_name"){
  170. var td = element_gen("td",{class: "lab-cell",value: "pi",person_id: pi,text: lab[field]});
  171. }
  172. else{
  173. var td = element_gen("td",{class: "lab-cell",value: field,text: lab[field]});
  174. }
  175. tr.append(td);
  176. }
  177. /* generate actions */
  178. let actions = {edit:{class: "t-icon material-icons",icon: "notes"}};
  179. var actions_cell = element_gen("td",{class: "actions-cell"});
  180. for(var action in actions){
  181. var icon = element_gen("i",{class: actions[action]["class"],text: actions[action]["icon"]});
  182. var button = element_gen("button",{class: "button action",id: action,value: action,title: action,childs: {icon}});
  183. actions_cell.append(button);
  184. }
  185. /* append */
  186. tr.append(actions_cell);
  187. $("tbody").append(tr);
  188. /* sort table */
  189. next += 1;
  190. full_sort(next,sort_key);
  191. $(".table-total").text(`Total: ${next}`);
  192. hide_form(true);
  193. default: /* on success/error, display message */
  194. set_alert(status,response["message"]);
  195. break;
  196. }
  197. });
  198. }
  199. /* invalid user info */
  200. else{
  201. $(".required").each(function(){
  202. var id = $(this).attr("id");
  203. var s = `.${id}`;
  204. /* highlight */
  205. if(!is_valid($(this).val(),id)){
  206. $(this).addClass("invalid");
  207. $(s).show();
  208. }
  209. /* highlight */
  210. else{
  211. $(this).removeClass("invalid");
  212. $(s).hide();
  213. }
  214. });
  215. /* display alert */
  216. set_alert("warning","Please correct highlighted fields");
  217. }
  218. }
  219. /* set_listeners() - listen for events */
  220. function set_listeners(){
  221. /* sorts table */
  222. $(".sort").click(function(){
  223. sort_key = $(this).attr("value");
  224. if(sort_key === "lab_room"){
  225. $(".sort-primary").show();
  226. $(".sort-alt").hide();
  227. }
  228. else{
  229. $(".sort-alt").show();
  230. $(".sort-primary").hide();
  231. }
  232. full_sort(next,sort_key);
  233. });
  234. /* register user */
  235. $("#main-submit").click(edit_lab);
  236. /* register user */
  237. $("#add-submit").click(add_laboratory);
  238. /* handle actions */
  239. $("tbody").on("click",".action",action);
  240. /* add material to inventory */
  241. $("#add").click(function(){
  242. display_form("add","add-material");
  243. });
  244. /* close form */
  245. $(".close-form").click(function(e){
  246. if(e.target === e.currentTarget){
  247. hide_form(false);
  248. }
  249. });
  250. }
  251. /* table_gen(personnel: dictionary) - generate table body */
  252. function table_gen(data){
  253. for(var d in data){
  254. /* generate lab row */
  255. let tr = element_gen("tr",{class: "lab-row",id: d});
  256. for(var field in data[d]){
  257. if(field === "lab"){
  258. var href = `/laboratory?lab_id=${data[d][field]["lab_id"]}`;
  259. var link = element_gen("a",{class: "lab-link",href: href,target: "_blank",rel: "noopener noreferrer",text: data[d][field]["lab_room"]});
  260. var td = element_gen("td",{class: "lab-cell",lab_id: data[d][field]["lab_id"],value: "lab_room",childs: {link}});
  261. }
  262. else if(field === "pi"){
  263. var td = element_gen("td",{class: "lab-cell",value: field,person_id: data[d][field]["person_id"],text: data[d][field]["person_name"]});
  264. }
  265. else{
  266. /* correct null entries */
  267. if(!data[d][field]){
  268. data[d][field] = "-";
  269. }
  270. var td = element_gen("td",{class: "lab-cell",value: field,text: data[d][field]});
  271. }
  272. tr.append(td);
  273. }
  274. /* generate actions */
  275. let actions = {edit:{class: "t-icon material-icons",icon: "notes"}};
  276. var actions_cell = element_gen("td",{class: "actions-cell"});
  277. for(var action in actions){
  278. var icon = element_gen("i",{class: actions[action]["class"],text: actions[action]["icon"]});
  279. var button = element_gen("button",{class: "button action",id: action,value: action,title: action,childs: {icon}});
  280. actions_cell.append(button);
  281. }
  282. /* append */
  283. tr.append(actions_cell);
  284. $("tbody").append(tr);
  285. }
  286. $(".processing").hide();
  287. setTimeout(function(){
  288. set_listeners();
  289. $(".main-wrapper").show();
  290. },250);
  291. }
  292. /* fetch_personnel() - generate select menu for pi's */
  293. function fetch_labs(){
  294. /* query server */
  295. $.post("/scripts/opaso",{query: 17},function(data){
  296. // console.log(data);
  297. /* extract args */
  298. let response = JSON.parse(data);
  299. let status = response["status"];
  300. /* handle by status */
  301. switch(status){
  302. case "success": /* on success, generate select */
  303. /* generate lab table */
  304. let labs = response["laboratories"];
  305. next = Object.keys(labs).length;
  306. table_gen(labs);
  307. $(".table-total").text(`Total: ${next}`);
  308. break;
  309. case "expired": /* on expired, redirect to index */
  310. window.location.href = "/?error=session_expired";
  311. break;
  312. default: /* on error, display message */
  313. set_alert(status,response["message"]);
  314. break;
  315. }
  316. });
  317. }
  318. /* fetch_personnel() - generate select menu for pi's */
  319. function fetch_personnel(){
  320. $.post("/scripts/opaso",{query: 13},function(data){
  321. // console.log(data);
  322. /* extract response */
  323. let response = JSON.parse(data);
  324. let status = response["status"];
  325. /* handle by status */
  326. switch(status){
  327. case "success": /* on success, generate select */
  328. let personnel = response["personnel"];
  329. for(var person in personnel){
  330. var option = element_gen("option",{class: "option",text: personnel[person]["person_name"],value: personnel[person]["person_id"]});
  331. $(".pi").append(option);
  332. }
  333. fetch_labs();
  334. break;
  335. case "expired": /* on expired, redirect to index */
  336. window.location.href = "/?error=session_expired";
  337. break;
  338. default: /* on error, display message */
  339. set_alert(status,response["message"]);
  340. break;
  341. }
  342. });
  343. }