Proyecto en colaboración con OPASO

inventory.js 13KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  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 : inventory.js
  6. Description : Fetch lab inventory */
  7. /* globals */
  8. var editing,form_is_active,inventory,new_inventory,text;
  9. /* get_inventory() - fetch lab inventory from server */
  10. function get_inventory(){
  11. var laboratory = get_arg("lab"); /* get lab id from url */
  12. $.post("/scripts/opaso.php",{query: 4,laboratory: laboratory},function(data){
  13. console.log(data);
  14. try{
  15. var response = JSON.parse(data); /* parse response as json */
  16. switch(response["status"]){
  17. case "success": /* on success, generate table body */
  18. /* update inventory header */
  19. $(".header").text(response["laboratory"]);
  20. /* extract inventory from response and generate table body */
  21. inventory = response["inventory"];
  22. var tbody = create_element("tbody","inventory-body");
  23. var row_index = 1;
  24. for(var row in inventory){
  25. /* generate option buttons */
  26. var edit = create_button("Edit","fas fa-pen","btn options","edit");
  27. var copy = create_button("Copy","fas fa-copy","btn options","copy");
  28. var remove = create_button("Remove","fas fa-minus-square","btn options","remove");
  29. /* append buttons to cell */
  30. var options = create_element("td","options-cell");
  31. options.append(edit,copy,remove);
  32. /* generate table row */
  33. var index = create_element("td","index",row_index);
  34. var tr = create_element("tr","inventory-row");
  35. tr.setAttribute("id",row);
  36. tr.append(options,index);
  37. /* generate inventory columns */
  38. for(var col in inventory[row]){
  39. var cell = create_element("td","inventory-cell",inventory[row][col]);
  40. cell.setAttribute("val",col); /* identify cell field */
  41. tr.append(cell);
  42. }
  43. tbody.append(tr);
  44. row_index += 1;
  45. }
  46. $(".table").append(tbody);
  47. break;
  48. case "error": /* on error, display message */
  49. display_error(response["error"]);
  50. break;
  51. default: /* undefined status */
  52. display_error("Undefined status.");
  53. break;
  54. }
  55. set_listeners(); /* listen for cell/button clicks */
  56. loading_screen(false); /* hide loading screen */
  57. }
  58. catch(error){
  59. console.log(error.message);
  60. display_error(error.message);
  61. }
  62. });
  63. }
  64. /* set_listeners() - set listeners for cell/button clicks */
  65. function set_listeners(){
  66. /* set listener for option buttons */
  67. $(".options").click(function(){
  68. /* extract args */
  69. var option = $(this).attr("id");
  70. var row = $(this).parent().parent().attr("id"); /* row id */
  71. switch(option){
  72. case "edit": /* edit inventory row (fallthrough) */
  73. case "copy": /* copy inventory row */
  74. $(".form-header").text(option + " entry");
  75. set_form(row); /* populate form */
  76. $(".submit").attr("id",option);
  77. $(".submit").attr("val",row);
  78. $(".close-form").attr("val",row);
  79. break;
  80. case "remove": /* delete row form inventory */
  81. /* extract arg */
  82. var laboratory = get_arg("lab");
  83. $.post("/scripts/opaso.php",{query: 8,laboratory: laboratory,eid: row},function(data){ /* send row to be deleted */
  84. // console.log(data);
  85. try{
  86. var response = JSON.parse(data); /* parse response as json */
  87. switch(response["status"]){
  88. case "success": /* on success, generate table body */
  89. break;
  90. case "error": /* on error, display message */
  91. display_error(response["error"]);
  92. break;
  93. default: /* undefined status */
  94. display_error("Undefined status.");
  95. break;
  96. }
  97. }
  98. catch(error){
  99. display_error(error.message);
  100. }
  101. });
  102. break;
  103. default: /* undefined option */
  104. display_error("Undefined option.");
  105. break;
  106. }
  107. });
  108. /* set listener for form submit button */
  109. $(".submit").click(function(){
  110. /* extract args */
  111. var option = $(this).attr("id");
  112. var row = $(this).attr("val"); /* row id */
  113. var laboratory = get_arg("lab");
  114. /* set query for option */
  115. var query = null;
  116. switch(option){
  117. case "edit": /* edit row */
  118. query = 6;
  119. break;
  120. case "copy": /* copy row */
  121. query = 7;
  122. break;
  123. default: /* undefined option */
  124. display_error("Undefined option.");
  125. break;
  126. }
  127. /* get and validate form fields */
  128. var form = get_form(row);
  129. if(valid(form) && form_is_active){
  130. reset_form(); /* reset form */
  131. /* submit query for row */
  132. console.log(JSON.stringify(form));
  133. $.post("/scripts/opaso.php",{query: query,laboratory: laboratory,eid: row, fields: JSON.stringify(form)},function(data){
  134. console.log(data);
  135. /* parse response as json and display result of query */
  136. try{
  137. var response = JSON.parse(data); /* parse response as json */
  138. switch(response["status"]){
  139. case "success": /* on success, generate table body */
  140. break;
  141. case "error": /* on error, display message */
  142. display_error(response["error"]);
  143. break;
  144. default: /* undefined status */
  145. display_error("Undefined status.");
  146. break;
  147. }
  148. }
  149. /* handle error */
  150. catch(error){
  151. display_error(error.message);
  152. }
  153. });
  154. }
  155. /* non-valid form, display warning */
  156. else{
  157. $(".fields-required").show();
  158. }
  159. });
  160. /* set listener for form close buttons */
  161. $(".close-form").click(function(){
  162. reset_form(); /* reset form */
  163. });
  164. /* set listener for double clicks on cells */
  165. $(".inventory-cell").dblclick(function(){
  166. /* generate text area if user isn't editing */
  167. if(!editing){
  168. $(this).height($(this).height()); /* avoid cell from collapsing */
  169. $(this).css("padding","0px"); /* remove padding */
  170. /* generate textarea with text from clicked cell */
  171. text = $(this).text();
  172. var textarea = create_element("textarea","edit-text",text);
  173. textarea.style.width = $(this).width() + "px";
  174. textarea.style.height = $(this).height() + "px";
  175. /* insert text area into DOM */
  176. $(this).html(textarea);
  177. editing = true; /* user is editing */
  178. }
  179. });
  180. /* set listener for clicks on body */
  181. $("body").click(function(element){
  182. /* extract class of clicked element */
  183. var target = $(element.target).attr("class");
  184. if(editing && target != "edit-text"){
  185. /* extract text from textarea and field and row id */
  186. var delta_text = $(".edit-text").val();
  187. var field = $(".edit-text").parent().val();
  188. var row = $(".edit-text").parent().parent().attr("id");
  189. /* reset styling and insert text to cell */
  190. $(".edit-text").parent().removeAttr("style");
  191. $(".edit-text").parent().html(delta_text);
  192. editing = false; /* deactivate editing */
  193. /* submit row changes to inventory */
  194. if(text != delta_text){
  195. /* extract arg */
  196. var laboratory = get_arg("lab");
  197. /* send new field text */
  198. $.post("/scripts/opaso.php",{query: 5,laboratory: laboratory,eid: row,text: delta_text, field: field},function(data){
  199. console.log(data);
  200. try{
  201. var response = JSON.parse(data); /* parse response as json */
  202. switch(response["status"]){
  203. case "success": /* on success, generate table body */
  204. break;
  205. case "error": /* on error, display message */
  206. display_error(response["error"]);
  207. break;
  208. default: /* undefined status */
  209. display_error("Undefined status.");
  210. break;
  211. }
  212. }
  213. catch(error){
  214. display_error(error.message);
  215. }
  216. });
  217. }
  218. }
  219. /* reset form on exit */
  220. else if(form_is_active && target == "form-window"){
  221. reset_form(); /* reset form */
  222. }
  223. });
  224. /* set listener for dropdown menu */
  225. $( ".navbar-wrapper" ).hover(function(){}, /* on hover in, do nothing */
  226. /* on hover out, hide dropdown menu */
  227. function(){
  228. /* check if menu is visible */
  229. if($(".dropdown-menu").is(':visible')){
  230. $(".dropdown-menu").removeClass("show"); /* hide menu */
  231. }
  232. }
  233. );
  234. }
  235. /* reset_form() - reset form and its styling */
  236. function reset_form(){
  237. /* reset styling (label) */
  238. $(".required-field").each(function(){
  239. $(this).removeClass("required-field");
  240. $(this).text($(this).text().replace("*",""));
  241. });
  242. /* reset styling (input) */
  243. $(".empty-field").each(function(){
  244. $(this).removeClass("empty-field");
  245. });
  246. /* hide from */
  247. $(".form-window").hide();
  248. $(".fields-required").hide();
  249. form_is_active = false; /* deactivate form */
  250. }
  251. /* valid(form: dictionary) - check for invalid form entries */
  252. function valid(form){
  253. var valid = true;
  254. for(var field in form){
  255. /* highlight empty fields */
  256. if(!form[field]){
  257. /* if not currently highlighted */
  258. if($("[for=" + field + "]").attr("class") != "required-field"){
  259. /* highlight label */
  260. $("[for=" + field + "]").text($("[for=" + field + "]").text() + "*");
  261. $("[for=" + field + "]").addClass("required-field");
  262. /* highlight field */
  263. $("[name=" + field + "]").addClass("empty-field");
  264. }
  265. valid = false;
  266. }
  267. /* remove styling for non empty fields */
  268. else{
  269. /* if currently highlighted... */
  270. if($("[for=" + field + "]").attr("class") == "required-field"){
  271. /* reset styling for label */
  272. $("[for=" + field + "]").text($("[for=" + field + "]").text().replace("*",""));
  273. $("[for=" + field + "]").removeClass("required-field");
  274. /* reset styling for field */
  275. $("[name=" + field + "]").removeClass("empty-field");
  276. }
  277. }
  278. }
  279. return valid;
  280. }
  281. /* get_form(row: string) - get form data */
  282. function get_form(row){
  283. var form = {};
  284. for(var col in inventory[row]){
  285. form[col] = $("[name=" + col + "]").val();
  286. }
  287. return form;
  288. }
  289. /* set_form(row: string) - populate form using inventory data */
  290. function set_form(row){
  291. /* fill form input by name */
  292. for(var col in inventory[row]){
  293. $("[name=" + col + "]").val(inventory[row][col]);
  294. }
  295. /* display form */
  296. $(".form-window").show();
  297. /* form is active */
  298. form_is_active = true;
  299. }
  300. /* Load navbar and footer into document and fetch inventory */
  301. $(document).ready(function(){
  302. $(".navbar-wrapper").load("/navbar.html",function(){
  303. $(".footer-wrapper").load("/footer.html",function(){
  304. current(); /* highlight current navbar link */
  305. get_inventory(); /* fetch inventory from server */
  306. });
  307. });
  308. });