-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmyscript.js
358 lines (313 loc) · 11.7 KB
/
myscript.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
"use strict";
// array containing all the user names for autocomplete
const userNames= [];
let search = document.getElementById("search");
let lightboxIsOpen = false;
// change the visibility of grade dropdown
function isStudent() {
if (document.getElementById("current") == null) {
return;
} else if (document.getElementById("current").checked) {
document.getElementById("dropdown").style.display = "block";
} else {
document.getElementById("dropdown").style.display = "none";
document.getElementById("grade").value = "";
}
}
// code for lightbox
// initialize hidden elements
window.onload = function() {
document.getElementById("positionBigImage").style.display = "none";
document.getElementById("lightbox").style.display = "none";
loadImages("all");
};
// set the visibility of ID
function setVisibility(divID, value) {
var element = document.getElementById(divID);
if (element) {
element.style.display = value;
}
}
// hide the lightbox and positionBigImage
function hideLightBox() {
lightboxIsOpen = false;
setVisibility('lightbox', 'none');
setVisibility('positionBigImage', 'none');
}
// display lightbox with big image in it
function displayLightBox(alt, imageFile) {
lightboxIsOpen = true;
let image = new Image();
let bigImage = document.getElementById("bigImage");
let requestedUid = imageFile.split(".")[0];
// update big image to access
image.src = "profileimages/" + imageFile;
image.alt = alt;
bigImage.src = image.src; // put big image in page
bigImage.alt = image.alt;
bigImage.setAttribute("class", "card");
// show light box with big image
setVisibility('lightbox', 'block');
setVisibility('positionBigImage', 'block');
if (imageFile == '') {
document.getElementById("mySidebar").style.zIndex = "3";
} else {
document.getElementById("mySidebar").style.zIndex = "0";
}
// show left right arrow
document.getElementById("left").style.display = "block";
document.getElementById("right").style.display = "block";
if (imageFile != "") {
fetch("./getData.php?uid=" + requestedUid)
.then(response => response.json())
.then(data => updateContents(data))
.catch(err => console.log("error occurred " + err));
}
}
// update image info
function updateContents(data) {
document.getElementById("name").innerHTML = "Username: " + data.name;
if (data.connection == "current"){
document.getElementById("connection").innerHTML = "Connection to Mount Doug: student";
} else {
document.getElementById("connection").innerHTML = "Connection to Mount Doug: " + data.connection;
}
if (data.grade != "") {
document.getElementById("grade").style.display = "block";
document.getElementById("grade").innerHTML = "User's Grade: " + data.grade;
} else {
document.getElementById("grade").style.display = "none";
}
document.getElementById("message").innerHTML = "Little Bit About Themselves: " + data.message;
document.getElementById("download").innerHTML = "<a href='profileimages/" + data.UID + "." + data.imagetype + "' download> Download Image </a>";
}
// load "all", "student", "staff" or "alumni" images only
function loadImages(access) {
fetch("./readjson.php?access=" + access).
then(function(resp) {
return resp.json();
})
.then(
data => {getNames(data, access); displayImages(data);}
);
}
// search images for name or message contains search text
function searchImages() {
let request = document.getElementById("search").value;
fetch("./readjson.php?request=" + request).
then(function(resp) {
return resp.json();
})
.then(data => displayImages(data));
}
// display fetch data in cards
function displayImages(data) {
let i; // counter
let main = document.getElementById("main");
// remove all existing children of main
while (main.children[0]) {
main.removeChild(main.children[0]);
}
// for every image, create a new image object and add to main
for (i in data) {
var iDiv = document.createElement('div');
iDiv.className = 'w3-third w3-container w3-margin-bottom';
let img = new Image();
img.src = "thumbnails/" + data[i].UID + "." + data[i].imagetype;
img.setAttribute("class", "width100 w3-hover-opacity");
img.alt = data[i].UID + "." + data[i].imagetype;
img.setAttribute("onclick", "displayLightBox('" + img.alt + "', '" + data[i].UID + "." + data[i].imagetype + "')");
main.appendChild(iDiv);
iDiv.appendChild(img);
var nameDiv = document.createElement('div');
nameDiv.className = 'w3-container w3-white';
nameDiv.innerHTML = data[i].name;
iDiv.appendChild(nameDiv);
}
}
// move to left of right of the card
function prevNext(movement) {
let cards = document.getElementsByClassName("width100");
let bigImage = document.getElementById("bigImage");
let i = 0;
while (cards[i].getAttribute("alt") != bigImage.getAttribute("alt")) {
++i;
}
if (movement == "right" && i <= cards.length - 2) {
++i;
} else if (movement == "left" && i > 0) {
--i;
}
displayLightBox(cards[i].getAttribute("alt"), cards[i].getAttribute("alt"));
}
// open sidebar menu
function sidebar_open() {
document.getElementById("mySidebar").style.display = "block";
document.getElementById("myOverlay").style.display = "block";
document.getElementById("mySidebar").style.zIndex = "3";
}
// close sidebar menu
function sidebar_close() {
document.getElementById("mySidebar").style.display = "none";
document.getElementById("myOverlay").style.display = "none";
}
// check login form username/password value in order to change the style
function hasval(div) {
var divValue = div.value.trim();
if (divValue == '') {
div.setAttribute("class", "input100");
} else {
div.setAttribute("class", "input100 has-val");
}
}
// remove username/password alert after key down
function removeAlert(div) {
var alertdiv = document.getElementById(div.id + "Err");
if (alertdiv != null) {
alertdiv.style.display = "none";
}
}
// reset gallery after user confirm
function resetGallery() {
let agree = "Click OK to continue reset gallery.";
if (confirm(agree) == true) {
window.location = "./index.php?delete=true";
}
}
// autocomplete function takes two arguments, the text field element and an array of possible autocompleted values
function autocomplete(inp, arr) {
var currentFocus;
// execute a function when someone writes in the text field:
inp.addEventListener("input", function(e) {
var a, b, i, val = this.value;
// close any already open lists of autocompleted values
closeAllLists();
if (!val) { return false;}
currentFocus = -1;
// create a DIV element that will contain the items (values):
a = document.createElement("DIV");
a.setAttribute("id", this.id + "autocomplete-list");
a.setAttribute("class", "autocomplete-items");
// append the DIV element as a child of the autocomplete container:
this.parentNode.appendChild(a);
// for each item in the array
for (i = 0; i < arr.length; i++) {
// check if the item starts with the same letters as the text field value:
if (arr[i].substr(0, val.length).toUpperCase() == val.toUpperCase()) {
// create a DIV element for each matching element
b = document.createElement("DIV");
// make the matching letters bold
b.innerHTML = "<b>" + arr[i].substr(0, val.length) + "</b>";
b.innerHTML += arr[i].substr(val.length);
// insert a input field that will hold the current array item's value
b.innerHTML += "<input type='hidden' value='" + arr[i] + "'>";
// execute a function when someone clicks on the item value (DIV element)
b.addEventListener("click", function(e) {
// insert the value for the autocomplete text field
inp.value = this.getElementsByTagName("input")[0].value;
// close the list of autocompleted values, (or any other open lists of autocompleted values
closeAllLists();
});
a.appendChild(b);
}
}
});
// execute a function presses a key on the keyboard
inp.addEventListener("keydown", function(e) {
var x = document.getElementById(this.id + "autocomplete-list");
if (x) x = x.getElementsByTagName("div");
if (e.keyCode == 40) {
// If the arrow DOWN key is pressed, increase the currentFocus variable
currentFocus++;
// make the current item more visible
addActive(x);
} else if (e.keyCode == 38) { // up
// If the arrow UP key is pressed, decrease the currentFocus variable
currentFocus--;
// make the current item more visible
addActive(x);
} else if (e.keyCode == 13) {
// If the ENTER key is pressed, prevent the form from being submitted
e.preventDefault();
if (currentFocus > -1) {
// and simulate a click on the "active" item
if (x) x[currentFocus].click();
}
}
});
// classify an item as "active"
function addActive(x) {
if (!x) return false;
// start by removing the "active" class on all items
removeActive(x);
if (currentFocus >= x.length) currentFocus = 0;
if (currentFocus < 0) currentFocus = (x.length - 1);
// add class "autocomplete-active"
x[currentFocus].classList.add("autocomplete-active");
}
// remove the "active" class from all autocomplete items
function removeActive(x) {
for (var i = 0; i < x.length; i++) {
x[i].classList.remove("autocomplete-active");
}
}
// close all autocomplete lists in the document, except the one passed as an argument
function closeAllLists(elmnt) {
var x = document.getElementsByClassName("autocomplete-items");
for (var i = 0; i < x.length; i++) {
if (elmnt != x[i] && elmnt != inp) {
x[i].parentNode.removeChild(x[i]);
}
}
}
// execute a function when someone clicks in the document
document.addEventListener("click", function (e) {
closeAllLists(e.target);
});
}
// initiate the autocomplete userNames array from the query all
function getNames(data, access) {
if (access == "all") {
let i;
for (i in data) {
userNames[i] = data[i].name;
}
}
}
// password encryption on the client side
function encrypt() {
var pass = document.getElementById('password').value;
if (pass == "") {
document.getElementById('err').innerHTML='Error:Password is missing';
return false;
} else {
var hash = CryptoJS.MD5(pass);
console.log(hash.toString());
document.getElementById('password').value=hash;
return true;
}
}
// initiate the autocomplete function on the "search" element, and pass along
// the usernames array as possible autocomplete values:
window.addEventListener('load', (event) => {
if (document.getElementById("search")) {
document.getElementById("search").value = '';
autocomplete(document.getElementById("search"), userNames);
}
});
window.addEventListener("keyup", function(event) {
if (event.keyCode === 13) {
event.preventDefault();
document.getElementById("button").click();
}
});
window.addEventListener("keyup", function(event){
if (event.keyCode === 37 && lightboxIsOpen){
event.preventDefault();
document.getElementById("left").click();
}
else if (event.keyCode === 39 && lightboxIsOpen){
event.preventDefault();
document.getElementById("right").click();
}
});