Proyecto en colaboración con OPASO

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  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 : main.js
  6. Description : Utility functions */
  7. /* set listeners */
  8. $(document).ready(function(){
  9. current();
  10. $(".logout").click(log_out);
  11. $(".clear-btn").click(clear_form);
  12. /* search table */
  13. $(".search-form").on("input",function(){
  14. /* extract args */
  15. let q = $(this).val();
  16. $(".hide").removeClass("hide");
  17. /* hide non matching strings */
  18. if(is_valid(q)){
  19. search(q);
  20. $(".search-form-wrapper").addClass("active-form");
  21. }
  22. else{
  23. $(".search-form-wrapper").removeClass("active-form");
  24. $(".table-total").text(`Total: ${next}`)
  25. }
  26. });
  27. });
  28. /* clear_form() - reset search form */
  29. function clear_form(){
  30. $(".search-form").val("");
  31. $(".search-form-wrapper").removeClass("active-form");
  32. $(".hide").removeClass("hide");
  33. $(".table-total").text(`Total: ${next}`);
  34. }
  35. /* search(q: string) - search for string in table */
  36. function search(q){
  37. var misses = 0;
  38. $(".table-wrapper tbody tr").each(function(){
  39. /* set matched flag */
  40. var match = false;
  41. var row = $(this).find("td");
  42. /* search row */
  43. for(var c = 0; c < row.length - 1; c++){
  44. if(row[c].textContent.toLowerCase().includes(q.toLowerCase())){
  45. match = true;
  46. break;
  47. }
  48. }
  49. /* hide row */
  50. if(!match){
  51. $(this).addClass("hide");
  52. misses += 1;
  53. }
  54. });
  55. $(".table-total").text(`Total: ${next - misses}`);
  56. }
  57. /* current() - highlight current page in navbar */
  58. function current(){
  59. let c = `.nav-link[href="${window.location.pathname}"]`;
  60. $(c).addClass("current");
  61. }
  62. /* get_arg(arg) - fetches argument from url if set */
  63. function get_arg(arg){
  64. let url = new URL(window.location.href);
  65. return url.searchParams.get(arg);
  66. }
  67. /* log_out() - destroy session and redirect to index */
  68. function log_out(){
  69. $.post("/scripts/opaso",{query:11},function(data){
  70. /* extract response */
  71. let response = JSON.parse(data);
  72. let status = response["status"];
  73. /* handle by status */
  74. switch(status){
  75. case "success": /* on success, redirect to index */
  76. window.location.href = "/";
  77. break;
  78. default: /* on error, do nothing */
  79. break;
  80. }
  81. });
  82. }
  83. /* is_valid(arg: string,type: string) - verify if arg is valid */
  84. function is_valid(arg,type){
  85. /* null entries */
  86. if(arg === null){
  87. arg = "";
  88. }
  89. /* handle by type */
  90. switch(type){
  91. case "email":
  92. var re = /^[a-zA-Z0-9_.+-]+@(upr)\.edu$/g;
  93. return re.test(arg);
  94. case "number":
  95. return /^\d+$/.test(arg);
  96. default:
  97. return arg.trim().length > 0;
  98. }
  99. }
  100. /* set_alert(type: string,message: string) - display alert */
  101. function set_alert(type,message){
  102. /* set icon */
  103. $(".message-icon").hide();
  104. let icon = `.${type}-icon`;
  105. $(icon).show();
  106. /* set alert */
  107. $(".alert").addClass(type);
  108. $(".alert-status").text(`Status: ${type}`);
  109. $(".alert-message").text(`Message: ${message}`);
  110. /* animate alert */
  111. $(".alerts").addClass("slide-up");
  112. hide_alert();
  113. }
  114. /* hide_alert() - hide and reset alert */
  115. function hide_alert(){
  116. /* wait for animation */
  117. setTimeout(function(){
  118. $(".alerts").removeClass("slide-up");
  119. setTimeout(function(){
  120. $(".message-icon").hide();
  121. $(".alert-message").text("");
  122. $(".alert").attr("class","alert");
  123. },500);
  124. },5000);
  125. return false;
  126. }
  127. /* element_gen(type: string,attributes: object) - generates an html element */
  128. function element_gen(type,attributes){
  129. /* set attributes */
  130. var element = document.createElement(type);
  131. for(var a in attributes){
  132. var attr = attributes[a];
  133. /* handle by attribute */
  134. switch(a){
  135. case "text":
  136. element.textContent = attr;
  137. break;
  138. case "childs":
  139. for(var child in attr){
  140. element.append(attr[child]);
  141. }
  142. break;
  143. default:
  144. element.setAttribute(a,attr);
  145. break;
  146. }
  147. }
  148. return element;
  149. }
  150. /* link_gen() - generates navbar links with args */
  151. function link_gen(){
  152. $(".arg-link").each(function(){
  153. /* check for initial translation */
  154. var lab_id = get_arg("lab_id");
  155. try{
  156. var link = new URL($(this).attr("href"));
  157. }
  158. catch{
  159. var link = new URL(window.location.origin + $(this).attr("href"));
  160. }
  161. link.searchParams.set("lab_id",lab_id);
  162. $(this).attr("href",link);
  163. });
  164. }
  165. /* set_confirm(message: string,query: string) - configure alert */
  166. function set_confirm(message,query){
  167. $(".confirm-message").text(message);
  168. $("#yes").attr("value",query);
  169. $(".alert-wrapper").addClass("quick-fade-in");
  170. }
  171. /* hide_confirm() - reset alert */
  172. function hide_confirm(){
  173. $(".alert-wrapper").removeClass("quick-fade-in");
  174. $(".message").text("");
  175. }
  176. /* is_defined(arg: any) - return true if an argument is defined */
  177. function is_defined(arg){
  178. return arg !== undefined;
  179. }
  180. /* full_sort(size: int,sort_key: string) - sort table; O(n^2) */
  181. function full_sort(size,sort_key){
  182. var m_selector = `td[value=${sort_key}]`;
  183. for(var i = 0; i < size; i++){
  184. var min = `#${i}`;
  185. var min_key = $(min).find(m_selector).text();
  186. if(sort_key == "total"){
  187. min_key = parseFloat(min_key);
  188. }
  189. else{
  190. min_key = min_key.toLowerCase();
  191. }
  192. var min_cap = parseFloat($(min).find("td[value=capacity]").text());
  193. var min_idx = i;
  194. /* find min candidate */
  195. for(var j = i; j < size; j++){
  196. var candidate = `#${j}`;
  197. var c_key = $(candidate).find(m_selector).text();
  198. if(sort_key == "total"){
  199. c_key = parseFloat(c_key);
  200. }
  201. else{
  202. c_key = c_key.toLowerCase();
  203. }
  204. var c_cap = parseFloat($(candidate).find("td[value=capacity]").text());
  205. if(min_key > c_key){
  206. min_idx = j;
  207. min = candidate;
  208. min_key = c_key;
  209. min_cap = c_cap;
  210. }
  211. }
  212. /* switch */
  213. $(min).insertBefore($(`#${i}`));
  214. $(`#${i}`).insertAfter($(`#${min_idx-1}`));
  215. /* swap ids */
  216. var id = $(min).attr("id");
  217. $(`#${i}`).attr("id",id);
  218. $(min).attr("id",i);
  219. }
  220. }