123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265 |
- /* Authors : Carlos C. Corrada-Bravo
- David J. Ortiz-Rivera
-
- Organization : Centro de Desarrollo y Consultoria Computacional
- Project : OPASO Material Registry
- File : main.js
- Description : Utility functions */
-
- /* set listeners */
- $(document).ready(function(){
- current();
- $(".logout").click(log_out);
- $(".clear-btn").click(clear_form);
-
- /* search table */
- $(".search-form").on("input",function(){
- /* extract args */
- let q = $(this).val();
- $(".hide").removeClass("hide");
-
- /* hide non matching strings */
- if(is_valid(q)){
- search(q);
- $(".search-form-wrapper").addClass("active-form");
- }
-
- else{
- $(".search-form-wrapper").removeClass("active-form");
- $(".table-total").text(`Total: ${next}`)
- }
- });
- });
-
- /* clear_form() - reset search form */
- function clear_form(){
- $(".search-form").val("");
- $(".search-form-wrapper").removeClass("active-form");
- $(".hide").removeClass("hide");
- $(".table-total").text(`Total: ${next}`);
- }
-
- /* search(q: string) - search for string in table */
- function search(q){
- var misses = 0;
- $(".table-wrapper tbody tr").each(function(){
- /* set matched flag */
- var match = false;
- var row = $(this).find("td");
-
- /* search row */
- for(var c = 0; c < row.length - 1; c++){
- if(row[c].textContent.toLowerCase().includes(q.toLowerCase())){
- match = true;
- break;
- }
- }
-
- /* hide row */
- if(!match){
- $(this).addClass("hide");
- misses += 1;
- }
- });
-
- $(".table-total").text(`Total: ${next - misses}`);
- }
-
- /* current() - highlight current page in navbar */
- function current(){
- let c = `.nav-link[href="${window.location.pathname}"]`;
- $(c).addClass("current");
- }
-
- /* get_arg(arg) - fetches argument from url if set */
- function get_arg(arg){
- let url = new URL(window.location.href);
- return url.searchParams.get(arg);
- }
-
- /* log_out() - destroy session and redirect to index */
- function log_out(){
- $.post("/scripts/opaso",{query:11},function(data){
- /* extract response */
- let response = JSON.parse(data);
- let status = response["status"];
-
- /* handle by status */
- switch(status){
- case "success": /* on success, redirect to index */
- window.location.href = "/";
- break;
-
- default: /* on error, do nothing */
- break;
- }
- });
- }
-
- /* is_valid(arg: string,type: string) - verify if arg is valid */
- function is_valid(arg,type){
- /* null entries */
- if(arg === null){
- arg = "";
- }
-
- /* handle by type */
- switch(type){
- case "email":
- var re = /^[a-zA-Z0-9_.+-]+@(upr)\.edu$/g;
- return re.test(arg);
-
- case "number":
- return /^\d+$/.test(arg);
-
- default:
- return arg.trim().length > 0;
- }
- }
-
- /* set_alert(type: string,message: string) - display alert */
- function set_alert(type,message){
- /* set icon */
- $(".message-icon").hide();
- let icon = `.${type}-icon`;
- $(icon).show();
-
- /* set alert */
- $(".alert").addClass(type);
- $(".alert-status").text(`Status: ${type}`);
- $(".alert-message").text(`Message: ${message}`);
-
- /* animate alert */
- $(".alerts").addClass("slide-up");
- hide_alert();
- }
-
- /* hide_alert() - hide and reset alert */
- function hide_alert(){
- /* wait for animation */
- setTimeout(function(){
- $(".alerts").removeClass("slide-up");
- setTimeout(function(){
- $(".message-icon").hide();
- $(".alert-message").text("");
- $(".alert").attr("class","alert");
- },500);
- },5000);
-
- return false;
- }
-
- /* element_gen(type: string,attributes: object) - generates an html element */
- function element_gen(type,attributes){
- /* set attributes */
- var element = document.createElement(type);
- for(var a in attributes){
- var attr = attributes[a];
-
- /* handle by attribute */
- switch(a){
- case "text":
- element.textContent = attr;
- break;
-
- case "childs":
- for(var child in attr){
- element.append(attr[child]);
- }
- break;
-
- default:
- element.setAttribute(a,attr);
- break;
- }
- }
- return element;
- }
-
- /* link_gen() - generates navbar links with args */
- function link_gen(){
- $(".arg-link").each(function(){
- /* check for initial translation */
- var lab_id = get_arg("lab_id");
-
- try{
- var link = new URL($(this).attr("href"));
- }
-
- catch{
- var link = new URL(window.location.origin + $(this).attr("href"));
- }
-
- link.searchParams.set("lab_id",lab_id);
- $(this).attr("href",link);
- });
- }
-
- /* set_confirm(message: string,query: string) - configure alert */
- function set_confirm(message,query){
- $(".confirm-message").text(message);
- $("#yes").attr("value",query);
- $(".alert-wrapper").addClass("quick-fade-in");
- }
-
- /* hide_confirm() - reset alert */
- function hide_confirm(){
- $(".alert-wrapper").removeClass("quick-fade-in");
- $(".message").text("");
- }
-
- /* is_defined(arg: any) - return true if an argument is defined */
- function is_defined(arg){
- return arg !== undefined;
- }
-
- /* full_sort(size: int,sort_key: string) - sort table; O(n^2) */
- function full_sort(size,sort_key){
- var m_selector = `td[value=${sort_key}]`;
- for(var i = 0; i < size; i++){
- var min = `#${i}`;
- var min_key = $(min).find(m_selector).text();
-
- if(sort_key == "total"){
- min_key = parseFloat(min_key);
- }
-
- else{
- min_key = min_key.toLowerCase();
- }
-
- var min_cap = parseFloat($(min).find("td[value=capacity]").text());
- var min_idx = i;
-
- /* find min candidate */
- for(var j = i; j < size; j++){
- var candidate = `#${j}`;
- var c_key = $(candidate).find(m_selector).text();
-
- if(sort_key == "total"){
- c_key = parseFloat(c_key);
- }
-
- else{
- c_key = c_key.toLowerCase();
- }
-
- var c_cap = parseFloat($(candidate).find("td[value=capacity]").text());
- if(min_key > c_key){
- min_idx = j;
- min = candidate;
- min_key = c_key;
- min_cap = c_cap;
- }
- }
-
- /* switch */
- $(min).insertBefore($(`#${i}`));
- $(`#${i}`).insertAfter($(`#${min_idx-1}`));
-
- /* swap ids */
- var id = $(min).attr("id");
- $(`#${i}`).attr("id",id);
- $(min).attr("id",i);
- }
- }
|