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