Browse Source

Upload files to 'public_html/scripts'

david.ortiz11 4 years ago
parent
commit
01e9dbe5c7
1 changed files with 161 additions and 0 deletions
  1. 161
    0
      public_html/scripts/login.js

+ 161
- 0
public_html/scripts/login.js View File

@@ -0,0 +1,161 @@
1
+/*  Authors         :   Carlos C. Corrada-Bravo
2
+                        David J. Ortiz-Rivera
3
+
4
+    Organization    :   Centro de Desarrollo y Consultoria Computacional
5
+    Project         :   OPASO Material Registry   
6
+    File            :   login.js
7
+    Description     :   Validate and submit login info */
8
+
9
+/* global variables */
10
+var status,message;
11
+
12
+/* set listeners when document is ready */ 
13
+$(document).ready(function(){
14
+
15
+    /* check for errors */
16
+    error_handler();
17
+
18
+    /* animate form on hover */
19
+    $(".login-field").hover(function(){
20
+        /* extract arg */
21
+        let id = $(this).attr("id");
22
+        hover_input(id,true);
23
+    },function(){
24
+        /* extract arg */
25
+        let text = $(this).val();
26
+
27
+        /* remove  */
28
+        if(!is_valid(text)){
29
+            let id = $(this).attr("id");
30
+            hover_input(id,false);
31
+        }
32
+    });
33
+
34
+    /* animate form on input */
35
+    $(".login-field").on("input",function(){
36
+        /* extract args */
37
+        let text = $(this).val();
38
+        let id = $(this).attr("id");
39
+
40
+        /* remove */
41
+        if(!is_valid(text)){
42
+            hover_input(id);
43
+        }
44
+
45
+        else{
46
+            hover_input(id,true);    
47
+        }
48
+    });
49
+
50
+    /* submit login */
51
+    $(".submit-login").click(log_in);
52
+    
53
+    /* show/hide password */
54
+    $(".view-password").click(function(){
55
+        /* extract args */
56
+        let id = `#${$(this).attr("value")}`;
57
+        let type = $(id).attr("type");
58
+
59
+        /* hide password */
60
+        if(type === "text"){
61
+            $(id).attr("type","password");
62
+        }
63
+
64
+        /* show password */
65
+        else{
66
+            $(id).attr("type","text");
67
+        }
68
+
69
+        $(this).toggleClass("fa-eye");
70
+        $(this).toggleClass("fa-eye-slash");
71
+    });
72
+});
73
+
74
+/* hover_input(id: string) - animate field placeholder */
75
+function hover_input(id,flag){
76
+    /* generate selectors */
77
+    let field = `#${id}`;
78
+    let placeholder = `.${id}`;
79
+    let circle = `.${id}-circle`;
80
+    $(field).toggleClass("shrink",flag);
81
+    $(placeholder).toggleClass("slide",flag);
82
+    $(circle).toggleClass("hide",flag);
83
+}
84
+
85
+/* log_in() - validate and submit credentials */ 
86
+function log_in(){
87
+    /* extract args */
88
+    let email = $("#email").val();
89
+    let password = $("#password").val();
90
+
91
+    /* validate credentials */
92
+    if(is_valid(email) && is_valid(password)){
93
+        $(".invalid").removeClass("invalid");
94
+        $(".invalid-icon").removeClass("invalid-icon");
95
+
96
+        $.post("/scripts/opaso",{query:1,email:email,password:password},function(data){
97
+            // console.log(data);
98
+            /* extract response */
99
+            let response = JSON.parse(data);
100
+            let status = response["status"];
101
+
102
+            /* handle by status */
103
+            switch(status){
104
+                case "success":                 /* on success, redirect */
105
+                    window.location.href = "/menu";
106
+                    break;
107
+               
108
+                default:                        /* on failure/error, display message */
109
+                    set_alert(status,response["message"]);
110
+                    break;
111
+            }
112
+        });
113
+    }
114
+
115
+    /* invalid login info */ 
116
+    else{
117
+        $(".login-field").each(function(){
118
+            /* extract id */
119
+            var id = $(this).attr("id");
120
+            var selector = `.${id}-circle`;
121
+
122
+            /* highlight invalid field */
123
+            if(!$(this).val()){
124
+                $(this).parent().addClass("invalid");
125
+                $(selector).addClass("invalid-icon");
126
+            }
127
+            /* unhighighlight valid field */
128
+            else{
129
+                $(this).parent().removeClass("invalid");
130
+                $(selector).removeClass("invalid-icon");
131
+            }
132
+        });
133
+
134
+        /* display alert */
135
+        status = "warning";
136
+        message = "All fields required";
137
+        set_alert(status,message);
138
+    }
139
+}
140
+
141
+/*  error_handler() - handle token errors */
142
+function error_handler(){
143
+    /* extract error */
144
+    let error = get_arg("error");
145
+    if(error){
146
+        switch(error){
147
+            case "login_failed":
148
+                set_alert("error","Login failed");
149
+                break;
150
+            case "token_expired":
151
+                set_alert("error","Invalid token");
152
+                break;
153
+            case "session_expired":
154
+                set_alert("error","Session expired");
155
+                break;
156
+            default:
157
+                set_alert("error","Some error occurred");
158
+                break;
159
+        }
160
+    }
161
+}