-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
188 lines (155 loc) · 6.78 KB
/
script.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
function toggleDropdown(dropdownName) {
let dropdown = document.getElementById(dropdownName);
dropdown.style.display = (dropdown.style.display == 'flex') ? 'none' : 'flex';
};
function openForm(formName) {
// Close any open forms
closeForm("lost-form");
closeForm("found-form");
// Open the form
let form = document.getElementById(formName);
form.style.display = 'flex';
// Disable the forum post area
let forum = document.getElementById("post-container");
forum.style.display = 'none';
};
function closeForm(formName) {
// Close the form
let form = document.getElementById(formName);
form.style.display = 'none';
// Re-enable the forum post area
let forum = document.getElementById("post-container");
forum.style.display = 'flex';
};
function validateFoundForm(description, location, image) {
let valid = true;
// Check that each input is valid
// For each invalid input, unhide the error message
if (description == '') {
document.getElementById('found-desc-error').style.display = 'block';
valid = false;
} else { document.getElementById('found-desc-error').style.display = 'none'; } // Hide error message if input is good
if (location == '') {
document.getElementById('location-found-error').style.display = 'block';
valid = false;
} else { document.getElementById('location-found-error').style.display = 'none'; } // Hide error message if input is good
if (image == '') {
document.getElementById('found-img-error').style.display = 'block';
valid = false;
} else { document.getElementById('found-img-error').style.display = 'none'; } // Hide error message if input is good
// Return False for invalid, True for valid
return valid;
};
var idNum = 0;
function postFoundItem() {
// Get inputs as strings
let itemText = document.getElementById("item-desc");
let foundLocation = document.getElementById("location-found");
let image = document.getElementById("found-img");
// Strip "C:\fakepath\" from input image"
let imageFile = image.value.replace("C:\\fakepath\\", '');
// Validate form entries before continuing
if (!validateFoundForm(itemText.value, foundLocation.value, imageFile)) {
return;
}
// Get today's date in M/D/YYYY format
let date = new Date().toLocaleDateString();
// Make forum-post div and add to Found Items
// NOTE: NOT GOOD PRACTICE -- LEADS TO XSS ATTACKS
// Currently ignoring because this is a project to be locally hosted only
let newItem = `<div class="forum-item" id="post-${idNum}">
<img class="delete" src="images/cross.svg" onclick="removePost('post-${idNum}');">
<div class="forum-image">
<img src="images/${imageFile}">
</div>
<div class="forum-labels">
<label><b>Item</b>: ${itemText.value}</label><br>
<label><b>Location</b>: ${foundLocation.value}</label><br>
<label><b>Found On</b>: ${date}</label><br>
<label><b>Email Me</b>: [email protected]</label>
</div>
</div>`;
let foundContainer = document.getElementById("found-header");
foundContainer.insertAdjacentHTML("afterend", newItem);
// Reset fields and close the form
idNum++; // Increment idNum for next post
itemText.value = '';
foundLocation.value = '';
image.value = '';
closeForm("found-form");
};
function validateLostForm(description, location) {
let valid = true;
// Check that each input is valid
// For each invalid input, unhide the error message
if (description == '') {
document.getElementById('lost-desc-error').style.display = 'block';
valid = false;
} else { document.getElementById('lost-desc-error').style.display = 'none'; } // Hide error message if input is good
if (location == '') {
document.getElementById('last-seen-error').style.display = 'block';
valid = false;
} else { document.getElementById('last-seen-error').style.display = 'none'; } // Hide error message if input is good
// Return False for invalid, True for valid
return valid;
};
function postLostItem() {
// Get inputs as strings
let itemText = document.getElementById("item-desc-lost");
let lostLocation = document.getElementById("last-seen");
let image = document.getElementById("lost-img");
// Validate form input before continuing
if (!validateLostForm(itemText.value, lostLocation.value)) {
return;
}
// Get today's date in M/D/YYYY format
let date = new Date().toLocaleDateString();
// Strip "C:\fakepath\" from input image"
let imageFile = image.value.replace("C:\\fakepath\\", 'images/');
if (imageFile == '') { // Use placeholder image
imageFile = 'images/gallery.svg';
}
// Make forum-post div and add to Lost Items
// NOTE: NOT GOOD PRACTICE -- LEADS TO XSS ATTACKS
// Currently ignoring because this is a project to be locally hosted only
let newItem = `<div class="forum-item" id="post-${idNum}">
<img class="delete" src="images/cross.svg" onclick="removePost('post-${idNum}');">
<div class="forum-image">
<img src="${imageFile}">
</div>
<div class="forum-labels">
<label><b>Item</b>: ${itemText.value}</label><br>
<label><b>Last Seen</b>: ${lostLocation.value}</label><br>
<label><b>Lost On</b>: ${date}</label><br>
<label><b>Email Me</b>: [email protected]</label>
</div>
</div>`;
let foundContainer = document.getElementById("lost-header");
foundContainer.insertAdjacentHTML("afterend", newItem);
// Reset fields and close the form
idNum++; // Increment idNum
itemText.value = '';
lostLocation.value = '';
image.value = '';
closeForm("lost-form");
};
function resetAllForms() {
// Get all fields from found item form
let itemText = document.getElementById("item-desc");
let foundLocation = document.getElementById("location-found");
let image = document.getElementById("found-img");
// Get all fields from lost item form
let lostItemText = document.getElementById("item-desc-lost");
let lastSeen = document.getElementById("last-seen");
let lostImage = document.getElementById("lost-img");
// Reset fields
itemText.value = '';
foundLocation.value = '';
image.value = '';
lostItemText.value = '';
lastSeen.value = '';
lostImage.value = '';
};
function removePost(postID) {
document.getElementById(postID).remove();
}