瀏覽代碼

Upload files to 'public_html/scripts'

david.ortiz11 5 年之前
父節點
當前提交
a5b9f19e24

+ 242
- 47
public_html/scripts/main.js 查看文件

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

+ 410
- 0
public_html/scripts/manage-laboratories.js 查看文件

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

+ 430
- 0
public_html/scripts/manage-personnel.js 查看文件

@@ -0,0 +1,430 @@
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            :   manage-personnel.js
7
+    Description     :   Generate personnel table */
8
+
9
+/* global values */
10
+var auth = {};
11
+var labs = {};
12
+var access_levels = {};
13
+var current_person_id;
14
+var next;
15
+var current_lab_id;
16
+
17
+/* set listeners when document is ready */ 
18
+$(document).ready(fetch_personnel);
19
+
20
+/* display_form(type: string,transaction: string) - configure/display form  */
21
+function display_form(type,transaction){
22
+    let form = `.${type}-form`;
23
+    let submit = `#${type}-submit`;
24
+    $(".form-shader").addClass("fade-in");
25
+    $(form).addClass("c-form");
26
+    $(form).addClass("slide-down");
27
+    $(submit).attr("value",transaction);
28
+}
29
+
30
+/* hideForm(clear: bool) - hide/clear form  */
31
+function hide_form(clear){
32
+    $(".c-form").removeClass("slide-down");
33
+    $(".c-form").removeClass("c-form");
34
+
35
+    /* wait for animation */
36
+    setTimeout(function(){
37
+        $(".form-shader").removeClass("fade-in");
38
+        $(".laboratories").html("");
39
+        if(clear){
40
+            $(".ap-field").val("");
41
+        }
42
+    },250);
43
+    return false;
44
+}
45
+
46
+/* fetch_access_level(action) - extract user's acess level for lab */
47
+function fetch_access_level(action){
48
+    /* extract access level */
49
+    let lab_id = $(".laboratories").val();
50
+    if(current_person_id in access_levels){
51
+        if(lab_id in access_levels[current_person_id]){
52
+            var access_level = access_levels[current_person_id][lab_id]["access_level"];
53
+        }
54
+
55
+        else{
56
+            var access_level = "none";
57
+        }
58
+    }
59
+
60
+    else{
61
+        var access_level = "none";
62
+    }
63
+
64
+
65
+    /* restrict options */
66
+    if(action === "restrict" && access_level === "technician"){
67
+        $("option[value=investigator]").attr("disabled",true);
68
+    }
69
+
70
+    else{
71
+        $("option[value=investigator]").attr("disabled",false);
72
+    }
73
+
74
+    if(action === "authorize" && access_level === "investigator"){
75
+        $("option[value=technician]").attr("disabled",true);
76
+    }
77
+    
78
+    else{
79
+        $("option[value=technician]").attr("disabled",false);
80
+    }
81
+
82
+    $(".access_level").val(access_level);
83
+}
84
+
85
+/* action - handle material transactions */
86
+function action(){
87
+    /* extract args */
88
+    let action = $(this).attr("value");
89
+    let r = $(this).parent().parent().attr("id");
90
+    let s = `#${r}`;
91
+    var restrict = true;
92
+    let name = $(s).find("td[value=person_name]").text();
93
+    current_person_id = $(s).find("td[value=person_name]").attr("person_id");
94
+
95
+    /* set user info */
96
+    $(".person").text(name);
97
+    $(".main-header").text(`${action} user`);
98
+
99
+    /* authorize */
100
+    if(action === "authorize"){
101
+        for(var lab in labs){
102
+            var option = element_gen("option",{class: "option",text: labs[lab]["lab_room"],value: labs[lab]["lab_id"]});
103
+            $(".laboratories").append(option);
104
+        }
105
+
106
+        $("option[value=none]").attr("disabled",true);
107
+    }
108
+
109
+    /* restrict */
110
+    else{
111
+        if(current_person_id in access_levels){
112
+            var access_level = access_levels[current_person_id];
113
+            for(var l in labs){
114
+                for(var lab_id in access_level){
115
+                    if(labs[l]["lab_id"] == lab_id){
116
+                        var option = element_gen("option",{class: "option",text: access_level[lab_id]["lab_room"],value: lab_id});
117
+                        $(".laboratories").append(option);
118
+                        break;
119
+                    }
120
+                }
121
+            }
122
+        }
123
+
124
+        /* no authorized labs */
125
+        else{
126
+            restrict = false;
127
+        }
128
+
129
+        $("option[value=none]").attr("disabled",false);
130
+    }
131
+
132
+    /* user is being authorized/has authorized labs */
133
+    if(restrict){
134
+        fetch_access_level(action);
135
+        display_form("main",action);
136
+    }
137
+
138
+    else{
139
+        set_alert("warning","No authorized labs found");
140
+    }
141
+}
142
+
143
+/* manage_personnel() - authorize/restrict labs for a user */
144
+function manage_personnel(){
145
+    let action = $(this).attr("value");
146
+    let access_level = $(".access_level").val();
147
+    current_lab_id = $(".laboratories").val();
148
+    let lab_room = $(".laboratories option:selected").text();
149
+
150
+    /* validate and submit */
151
+    if(is_valid(current_lab_id) && is_valid(access_level) && is_valid(action) && is_valid(current_person_id)){
152
+        $.post("/scripts/opaso",{query: 16,lab_id: current_lab_id,access_level: access_level,action: action,person_id: current_person_id},function(data){
153
+            // console.log(data);
154
+            /* extract response */
155
+            let response = JSON.parse(data);
156
+            let status = response["status"];
157
+
158
+            /* handle by status */
159
+            switch(status){
160
+                case "expired":                  /* on expired, redirect to index */
161
+                    window.location.href = "/?error=session_expired";
162
+                    break;
163
+
164
+                case "success":                 /* on success, update access level */
165
+                    /* determine authorized labs/highest access level */
166
+                    if(current_person_id in access_levels){
167
+                        var authorized = [];
168
+                        var highest_access_level = "technician";
169
+
170
+                        if(action === "restrict" && access_level === "none"){
171
+                            delete access_levels[current_person_id][current_lab_id];
172
+                        }
173
+
174
+                        else{
175
+                            access_levels[current_person_id][current_lab_id] = {lab_room: lab_room,access_level: access_level};
176
+                        }
177
+
178
+                        for(var lab_id in access_levels[current_person_id]){
179
+                            authorized.push(access_levels[current_person_id][lab_id]["lab_room"]);
180
+
181
+                            if(access_levels[current_person_id][lab_id]["access_level"] === "investigator"){
182
+                                highest_access_level = "investigator";
183
+                            }
184
+                        }
185
+
186
+                        if(!authorized.length){
187
+                            authorized = "-";
188
+                        }
189
+
190
+                        else{
191
+                            authorized = authorized.join(", ");
192
+                        }
193
+                    }
194
+
195
+                    /* no authorized labs */
196
+                    else{
197
+                        var authorized = lab_room;
198
+                        var highest_access_level = access_level;
199
+                        access_levels[current_person_id] = {};
200
+                        access_levels[current_person_id][current_lab_id] = {lab_room: lab_room,access_level: access_level};
201
+                    }
202
+                    
203
+                    $(`td[person_id=${current_person_id}]`).parent().find("td[value=authorized]").text(authorized);
204
+                    $(`td[person_id=${current_person_id}]`).parent().find("td[value=access_level]").text(highest_access_level);
205
+                    hide_form();
206
+
207
+                default:                        /* on success/error, display message */
208
+                    /* display alert */
209
+                    set_alert(status,response["message"]);
210
+                    break;
211
+            }
212
+        });
213
+    }
214
+
215
+    /* missing args */
216
+    else{
217
+        set_alert("error","One or more missing args");
218
+    }
219
+}
220
+
221
+/* set_listeners() - listen for events */
222
+function set_listeners(){
223
+
224
+    /* authorize/restrict access */
225
+    $("#main-submit").click(manage_personnel);
226
+
227
+    /* register user */
228
+    $("#add-submit").click(register_user);
229
+
230
+    /* fetch access level for lab */
231
+    $(".laboratories").change(function(){
232
+        let action = $("#main-submit").attr("value");
233
+        fetch_access_level(action);
234
+    });
235
+
236
+    /* handle actions */
237
+    $("tbody").on("click",".action",action);
238
+
239
+    /* add material to inventory */
240
+    $("#add").click(function(){
241
+        display_form("add","add-material");
242
+    });
243
+
244
+    /* close form */ 
245
+    $(".close-form").click(function(event){
246
+        if(event.target === event.currentTarget){
247
+            hide_form(false);
248
+        }
249
+    });
250
+}
251
+
252
+/* table_gen(data: dictionary) - generate table body */
253
+function table_gen(data){
254
+    for(var d in data){
255
+        /* generate person row */
256
+        let person_id = data[d]["person_id"];
257
+        let tr = element_gen("tr",{id: d});
258
+        let person = element_gen("td",{person_id: person_id,text: data[d]["person_name"],value: "person_name"});
259
+
260
+        /* determine authorized labs/highest access level */
261
+        var authorized = [];
262
+        let highest_access_level = "technician";
263
+        if(person_id in access_levels){
264
+            for(var lab_id in access_levels[person_id]){
265
+                authorized.push(access_levels[person_id][lab_id]["lab_room"]);
266
+
267
+                if(access_levels[person_id][lab_id]["access_level"] === "investigator"){
268
+                    highest_access_level = "investigator";
269
+                }
270
+            }
271
+        }
272
+
273
+        if(!authorized.length){
274
+            authorized = "-";
275
+        }
276
+
277
+        else{
278
+            authorized = authorized.join(", ");
279
+        }
280
+
281
+        let labs = element_gen("td",{text: authorized,value: "authorized"});
282
+        let access_level = element_gen("td",{text: highest_access_level,value: "access_level"});
283
+        tr.append(person,labs,access_level);
284
+        
285
+        /* generate actions */
286
+        let actions = {authorize:{class: "t-icon material-icons",icon: "add"},restrict: {class: "t-icon material-icons",icon: "remove"}};
287
+        var actions_cell = element_gen("td",{class: "actions-cell"});
288
+        for(var action in actions){
289
+            var icon = element_gen("i",{class: actions[action]["class"],text: actions[action]["icon"]});
290
+            var button = element_gen("button",{class: "button action",id: action,value: action,title: action,childs: {icon}});
291
+            actions_cell.append(button);
292
+        }
293
+
294
+        /* append */
295
+        tr.append(actions_cell);
296
+        $("tbody").append(tr);
297
+    }
298
+
299
+    $(".processing").hide();
300
+    setTimeout(function(){
301
+        set_listeners();
302
+        $(".main-wrapper").show();
303
+    },250);
304
+}
305
+
306
+/* fetch_personnel() - fetch personnel list */
307
+function fetch_personnel(){
308
+    $.post("/scripts/opaso",{query: 15},function(data){
309
+        // console.log(data);
310
+        /* extract reponse */
311
+        let response = JSON.parse(data);
312
+        let status = response["status"];
313
+
314
+        /* handle by status */
315
+        switch(status){
316
+            case "success":                 /* on success, generate table */
317
+                /* extract args */
318
+                let personnel = response["personnel"];
319
+                auth = response["authorized"];
320
+                access_levels = response["access_levels"];
321
+                labs = response["labs"];
322
+                next = Object.keys(personnel).length;
323
+                    
324
+                /* generate personnel table */
325
+                table_gen(personnel);
326
+                $(".table-total").text(`Total: ${next}`);
327
+                break;
328
+
329
+            case "expired":                  /* on expired, redirect to index */
330
+                window.location.href = "/?error=session_expired";
331
+                break;
332
+
333
+            default:                        /* on error, display message */
334
+                set_alert(status,response["message"]);
335
+                break;
336
+        }
337
+    });
338
+}
339
+
340
+/* register_user() - validate input and register user */
341
+function register_user(){
342
+    /* extract args */
343
+    let email = $("#email").val();
344
+    let person_name = $("#person_name").val();
345
+    var phone_number = $("#phone_number").val();
346
+
347
+    /* validate user data */
348
+    if(is_valid(person_name,"text") && is_valid(email,"email")){
349
+        /* validate phone number */
350
+        if(!is_valid(phone_number,"number")){
351
+            phone_number = "n/a"
352
+        }
353
+
354
+        /* reset fields */
355
+        $(".invalid-feedback").hide();
356
+        $(".invalid").removeClass("invalid");
357
+        
358
+        /* query server */
359
+        $.post("/scripts/opaso",{query: 0,person_name: person_name,email: email,phone_number: phone_number},function(data){
360
+            // console.log(data);
361
+            /* extract response */
362
+            let response = JSON.parse(data);
363
+            let status = response["status"];
364
+
365
+            /* handle by status */
366
+            switch(status){
367
+                case "expired":                  /* on expired, redirect to index */
368
+                    window.location.href = "/?error=session_expired";
369
+                    break;
370
+
371
+               case "success":                  /* on success, add user */
372
+                    /* extract person id */
373
+                    let person_id = response["person_id"];
374
+
375
+                    /* generate table row */
376
+                    let tr = element_gen("tr",{class: "personnel-row",id: next});
377
+                    let person = element_gen("td",{class: "personnell-cell",person_id: person_id,text: person_name,value: "person_name"});
378
+                    let labs = element_gen("td",{class: "personnell-cell",text: "-",value: "authorized"});
379
+                    let access_level = element_gen("td",{class: "personnell-cell",text: "technician",value: "access_level"});
380
+                    tr.append(person,labs,access_level);
381
+
382
+                    /* generate actions */
383
+                    let actions = {authorize:{class: "t-icon material-icons",icon: "add"},restrict: {class: "t-icon material-icons",icon: "remove"}};
384
+                    var actions_cell = element_gen("td",{class: "actions-cell"});
385
+                    for(var action in actions){
386
+                        var icon = element_gen("i",{class: actions[action]["class"],text: actions[action]["icon"]});
387
+                        var button = element_gen("button",{class: "button action",id: action,value: action,title: action,childs: {icon}});
388
+                        actions_cell.append(button);
389
+                    }
390
+
391
+                    /* append */
392
+                    tr.append(actions_cell);
393
+                    $("tbody").append(tr);
394
+
395
+                    /* sort */
396
+                    next += 1;
397
+                    full_sort(next,"person_name");
398
+                    $(".table-total").text(`Total: ${next}`);
399
+                    hide_form(true);
400
+
401
+                default:                        /* on success/error, display message */
402
+                    set_alert(status,response["message"]);
403
+                    break;
404
+            }
405
+        });
406
+    }
407
+
408
+    /* invalid user info */ 
409
+    else{
410
+        $(".required").each(function(){
411
+            /* extract id */
412
+            var id = $(this).attr("id");
413
+            var s = `.${id}`;
414
+            
415
+            /* highlight */
416
+            if(!is_valid($(this).val(),id)){
417
+                $(s).show();                
418
+                $(this).addClass("invalid");
419
+            }
420
+
421
+            /* unhighlight */
422
+            else{
423
+                $(s).hide();
424
+                $(this).removeClass("invalid");
425
+            }
426
+        });
427
+
428
+        set_alert("warning","Please correct highlighted fields");
429
+    }
430
+}

+ 264
- 5
public_html/scripts/materials.js 查看文件

@@ -1,5 +1,264 @@
1
-let user = "david.ortiz";
2
-let q = "all";
3
-$.post("/scripts/materials.php",{user:user,type:type},function(response){
4
-	console.log(response);
5
-});
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            :   materials.js
7
+    Description     :   Generate materials table */
8
+
9
+/* extract material id */
10
+let mat_id = get_arg("mat_id");
11
+var next = 0;
12
+
13
+/* fetch materials */ 
14
+$(document).ready(init_material);
15
+
16
+/* set_listeners() - listen for events */
17
+function set_listeners(){
18
+    /* sort table */
19
+    $(".sort").click(function(){
20
+        sort_key = $(this).attr("value");
21
+        if(sort_key === "mat_name"){
22
+            $(".sort-material").show();
23
+            $(".sort-alt").hide();
24
+        }
25
+        else{
26
+            $(".sort-alt").show();
27
+            $(".sort-material").hide();
28
+        }
29
+        full_sort(next,sort_key);
30
+    });
31
+
32
+    /* download report */
33
+    $(".download-option").click(function(){
34
+        /* generate file */
35
+        $("html").addClass("wait");
36
+        let download_type = $(this).attr("value");
37
+        $.post("/scripts/opaso",{query: 24,download_type: download_type},function(data){
38
+            // console.log(data);
39
+            /* extract args */
40
+            let response = JSON.parse(data);
41
+            let status = response["status"];
42
+
43
+            /* handle by status */
44
+            switch(status){
45
+                case "success":                 /* on success, generate table */
46
+                    var link=element_gen("a",{href: response["url"],download: response["file_name"]});
47
+                    link.click();
48
+                    break;
49
+                
50
+                case "expired":                 /* on expired, redirect to index */
51
+                    window.location.href = "/?error=session_expired";
52
+                    break;
53
+
54
+                default:                        /* on error, display message */
55
+                    set_alert(status,response["message"]);
56
+                    break;
57
+            }
58
+
59
+            $("html").removeClass("wait");
60
+        });
61
+    });
62
+
63
+    /* fetch material data */
64
+    $("td[value=mat_name]").click(function(){
65
+        let mat_id = $(this).attr("mat_id");
66
+        let mat_name = $(this).text();
67
+        
68
+        /* query server */
69
+        $.post("/scripts/opaso",{query: 21,mat_id: mat_id},function(data){
70
+            // console.log(data);
71
+            /* extract response */
72
+            let response = JSON.parse(data);
73
+            let status = response["status"];
74
+
75
+            /* handle by status */
76
+            switch(status){
77
+                case "success":                 /* on success, display material data */
78
+                    /* extract data */
79
+                    let details = response["details"];
80
+                    $(".material-data").html("");
81
+                    for(var d in details){
82
+                        var r = element_gen("div",{class: "material-data-row"});
83
+                        for(var a in details[d]){
84
+                            if(a === "lab"){
85
+                                let href = `/laboratory?lab_id=${details[d][a]["lab_id"]}`;
86
+                                let link = element_gen("a",{class: "lab-link",target: "_blank",rel: "noopener noreferrer",text: details[d][a]["lab_room"],href: href});
87
+                                let data = element_gen("h5",{class: "header material-data-header",childs: {link}});
88
+                                r.append(data);
89
+                            }
90
+
91
+                            else{
92
+                                if(!details[d][a]){
93
+                                    details[d][a] = "-";
94
+                                }
95
+                                let data_header = element_gen("h6",{class: "header material-data-sub-header",text: `${a}:`});
96
+                                let data = element_gen("span",{class: "material-data-text",text: details[d][a]});
97
+                                let w = element_gen("span",{class: "material-data-text-wrapper",childs: {data_header,data}});
98
+                                r.append(w);
99
+
100
+                            }
101
+                        }
102
+
103
+                        $(".material-name").text(mat_name);
104
+                        $(".material-data").append(r);
105
+                    }
106
+
107
+                    $(".material-data-shader").addClass("slide-right");
108
+                    break;
109
+                
110
+                case "expired":                 /* on expired, redirect */
111
+                    window.location.href = "/?error=session_expired";
112
+                    break;
113
+
114
+                default:                        /* on error, display message */
115
+                    set_alert(status,response["message"]);
116
+                    break;
117
+            }
118
+        });
119
+    });
120
+
121
+    /* close form */ 
122
+    $(".hide-material-data").click(function(event){
123
+        if(event.target === event.currentTarget){
124
+            $(".material-data-shader").removeClass("slide-right")
125
+        }
126
+    });
127
+}
128
+
129
+/* table_gen(data: dictionary,identifiers: dictionary) - generate table body */
130
+function table_gen(data,identifiers){
131
+    /* extract details */
132
+    for(var i in identifiers){
133
+        var mat_id = identifiers[i]["mat_id"];
134
+        let tr = element_gen("tr",{class: "material-row",id: i});
135
+
136
+        for(var field in data[mat_id]){
137
+
138
+            /* handle by field */
139
+            switch(field){
140
+                case "mat_name":
141
+                    var attr = {class: "material-cell",value: field,mat_id: mat_id,text: data[mat_id][field]};
142
+                    break;
143
+                
144
+                case "total":       
145
+                    var total = "";
146
+                    for(var uom in data[mat_id][field]){
147
+                        total += `${data[mat_id][field][uom]}${uom} / `;
148
+                    }
149
+                    var attr = {class: "material-cell",value: field,text: total.slice(0,-3)};
150
+                    break;
151
+
152
+                default:
153
+                    if(!data[mat_id][field]){
154
+                        data[mat_id][field] = "-";
155
+                    }
156
+                    var attr = {class: "material-cell",value: field,text: data[mat_id][field]};
157
+                    break;
158
+            }
159
+
160
+            var td = element_gen("td",attr);
161
+            tr.append(td);
162
+        }
163
+
164
+        $("tbody").append(tr);
165
+    }
166
+
167
+    $(".processing").hide();
168
+    setTimeout(function(){
169
+        set_listeners();
170
+        $(".main-wrapper").show();
171
+    },250);
172
+}
173
+
174
+/* fetch_materials(page: int) - fetch material details */ 
175
+function fetch_materials(page){
176
+    /* query server */
177
+    $.post("/scripts/opaso",{query: 19,page: page},function(data){
178
+        // console.log(data);
179
+        /* extract status */
180
+        response = JSON.parse(data);
181
+        status = response["status"];
182
+
183
+        /* handle by status */
184
+        switch(status){
185
+            case "success":                 /* on success, generate table */
186
+                /* extract args */
187
+                let materials = response["materials"];
188
+                let identifiers = response["identifiers"];
189
+                next = Object.keys(identifiers).length;
190
+                $(".table-total").text(`Total: ${next}`);
191
+                table_gen(materials,identifiers);
192
+                break;
193
+            
194
+            case "expired":                 /* on expired, redirect to index */
195
+                window.location.href = "/?error=session_expired";
196
+                break;
197
+
198
+            default:                        /* on error, display message */
199
+                /* display alert */
200
+                set_alert(status,response["message"]);
201
+                break;
202
+        }
203
+    });
204
+}
205
+
206
+/* init_material() - fetch material data */ 
207
+function init_material(){
208
+    /* material set */
209
+    if(is_valid(mat_id)){
210
+        $.post("/scripts/opaso",{query: 23,mat_id: mat_id},function(data){
211
+            // console.log(data);
212
+            /* extract response */
213
+            let response = JSON.parse(data);
214
+            let status = response["status"];
215
+
216
+            /* handle by status */
217
+            switch(status){
218
+                case "success":                 /* on success, generate table */
219
+                    /* extract args */
220
+                    let material = response["material"];
221
+                    let identifier = response["identifier"];
222
+                    next = Object.keys(identifier).length;
223
+                    $(".table-total").text(`Total: ${next}`);
224
+                    table_gen(material,identifier);
225
+                    break;
226
+                
227
+                case "expired":                 /* on expired, redirect to index */
228
+                    window.location.href = "/?error=session_expired";
229
+                    break;
230
+
231
+                default:                        /* on error, display message */
232
+                    set_alert(status,response["message"]);
233
+                    break;
234
+            }
235
+        });
236
+
237
+    }
238
+    else{
239
+        $.post("/scripts/opaso",{query: 20},function(data){
240
+            // console.log(data);
241
+            /* extract response */
242
+            let response = JSON.parse(data);
243
+            let status = response["status"];
244
+
245
+            /* handle by status */
246
+            switch(status){
247
+                case "success":                 /* on success, generate table */
248
+                    /* extract args */
249
+                    // let total = response["total"];
250
+                    // next = total;
251
+                    fetch_materials(0);
252
+                    break;
253
+                
254
+                case "expired":                 /* on expired, redirect to index */
255
+                    window.location.href = "/?error=session_expired";
256
+                    break;
257
+
258
+                default:                        /* on error, display message */
259
+                    set_alert(status,response["message"])
260
+                    break;
261
+            }
262
+        });
263
+    }
264
+}

+ 61
- 0
public_html/scripts/menu.js 查看文件

@@ -0,0 +1,61 @@
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            :   menu.js
7
+    Description     :   Generate user menu */
8
+
9
+/* fetch authorized labs */ 
10
+$(document).ready(menu);
11
+
12
+/* menu() - generate user menu */
13
+function menu(){
14
+    /* query server */
15
+    $.post("/scripts/opaso",{query: 2},function(data){
16
+        // console.log(data);
17
+        /* extract response */
18
+        let response = JSON.parse(data);
19
+        let status = response["status"];
20
+
21
+        /* handle by status */
22
+        switch(status){
23
+            case "success":                 /* on success, generate menu */
24
+                /* extract authorized laboratories */
25
+                let auth = response["authorized"];
26
+                let r = element_gen("div",{class: "lab-row"});
27
+                
28
+                /* generate laboratory links */
29
+                for(var lab in auth){
30
+                    /* generate elements */
31
+                    let room = element_gen("div",{class: "lab-room",text: auth[lab]["lab_room"]});
32
+                    let p = `(${auth[lab]["access_level"].charAt()})`;
33
+                    let privileges = element_gen("p",{class: "privileges-indicator",text: p});
34
+
35
+                    /* set lab link */
36
+                    let href = `/laboratory?lab_id=${auth[lab]["lab_id"]}`;
37
+                    let link = element_gen("a",{class: "lab-link",href: href,childs:{room,privileges}});
38
+                    let w = element_gen("div",{class: "col-lg-3 laboratory-wrapper"});
39
+
40
+                    w.append(link);
41
+                    r.append(w);
42
+                }
43
+                $(".laboratories").append(r);
44
+
45
+                /* display menu */
46
+                $(".processing").hide();
47
+                setTimeout(function(){
48
+                    $(".main-wrapper").show();
49
+                },250);
50
+                break;
51
+
52
+            case "expired":                  /* on expired, redirect to index */
53
+                window.location.href = "/?error=session_expired";
54
+                break;
55
+
56
+            default:                        /* on error, display message */
57
+                set_alert(status,response["message"]);
58
+                break;
59
+        }
60
+    });
61
+}