-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathshop.html
255 lines (210 loc) · 7.6 KB
/
shop.html
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
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<style>
a {
font-size: 17px;
}
</style>
<link rel="stylesheet"
href="https://cdn.staticfile.org/twitter-bootstrap/4.1.0/css/bootstrap.min.css">
<script src="https://cdn.staticfile.org/jquery/3.2.1/jquery.min.js"></script>
<script
src="https://cdn.staticfile.org/popper.js/1.12.5/umd/popper.min.js"></script>
<script
src="https://cdn.staticfile.org/twitter-bootstrap/4.1.0/js/bootstrap.min.js"></script>
<script src="./scripts/productDB.js"></script>
<title>Shop</title>
</head>
<body>
<nav class="navbar navbar-expand-sm bg-dark navbar-dark">
<a class="navbar-brand" href="shop.html">Shop</a>
<ul class="navbar-nav">
<li class="nav-item" id="navigation-actions"></li>
</ul>
</nav>
<br />
<div class="container">
<span style="font-size:50px">Hottest:</span>
<div id="demo" class="carousel slide" data-ride="carousel">
<ul class="carousel-indicators">
<li data-target="#demo" data-slide-to="0" class="active"></li>
<li data-target="#demo" data-slide-to="1"></li>
<li data-target="#demo" data-slide-to="2"></li>
</ul>
<div class="carousel-inner">
<div class="carousel-item active">
<a href="product.html?productName=Hoodie"><img src="images/Hoodie1.jpg" style="width:70%"></a>
</div>
<div class="carousel-item">
<a href="product.html?productName=Jacket"><img src="images/Jacket1.jpg" style="width:70%"></a>
</div>
<div class="carousel-item">
<a href="product.html?productName=Pants"><img src="images/Pants.jpg" style="width:70%"></a>
</div>
</div>
<a class="carousel-control-prev" href="#demo" data-slide="prev">
<span class="carousel-control-prev-icon"></span>
</a> <a class="carousel-control-next" href="#demo" data-slide="next">
<span class="carousel-control-next-icon"></span>
</a>
</div>
<br/><br/>
<div class="form-group" style="margin-left: 10px">
<span style="float:left">Filter:</span>
<select class="form-control" id="categoryFilter" onchange="applyFilters()" style="width:300px;float:left;">
<option value="None">None</option>
<option value="Clothing">Clothing</option>
<option value="Supplies">Supplies</option>
</select>
<div style="width:30px;height:20px;float:left"></div>
<input type="text" class="form-control" id="search" style="width:300px;float:left">
<div style="width:30px;height:20px;float:left"></div>
<button type="button" onclick="applyFilters()" class="btn btn-primary" style="float:left">Search</button>
</div><br/>
<div style="float: left;">
<span style="font-size:50px">Product List:</span>
<ul id="items" class="list-group"></ul>
</div>
</div>
<div style="position: absolute; right: 20px; top: 70px">
<!-- <div id="form-actions"></div> -->
<br />
<form action="./shopping_cart.html">
<input type="submit" value="Shopping Cart" class="btn btn-primary" />
</form>
</div>
</body>
<script>
//TODO: put this in js file
//based on https://stackoverflow.com/questions/16427636/check-if-localstorage-is-available
try {
localStorage.setItem('feature_test', 'yes');
if (localStorage.getItem('feature_test') === 'yes') {
localStorage.removeItem('feature_test');
// localStorage is enabled
} else {
// localStorage is disabled
alert("Please enable local storage cookies.");
}
} catch(e) {
// localStorage is disabled
alert("Please enable local storage cookies.");
}
console.log("ENAIL:", localStorage.getItem("email"));
if (localStorage.getItem("email")){
// Show Profile navigation
var navigation_profile = document.createElement("a");
var element1 = document.createTextNode("My Profile");
navigation_profile.href = './profile.html';
navigation_profile.appendChild(element1);
navigation_profile.className = "nav-link";
let navigation_action = document.getElementById("navigation-actions");
navigation_action.appendChild(navigation_profile);
}
else{
//Show Sign In navigation
var navigation_profile = document.createElement("a");
var element1 = document.createTextNode("Sign In");
navigation_profile.href = './login.html';
navigation_profile.appendChild(element1);
navigation_profile.className = "nav-link";
let navigation_action = document.getElementById("navigation-actions");
navigation_action.appendChild(navigation_profile);
}
var ul = document.getElementById("items");
//TODO: read this data from a "database" file
var products = getAllProducts();
console.log("Products", products);
displayItems(products);
function displayItems(products){
for(var i in products) {
var li = document.createElement('li');
li.className += "list-group-item";
li.appendChild(document.createTextNode("Name: "));
var a = document.createElement('a');
var linkText = document.createTextNode(products[i][0]);
a.appendChild(linkText);
a.href = "./product.html" + "?productName=" + products[i][0];
li.appendChild(a);
li.appendChild(document.createElement('br'));
li.appendChild(document.createTextNode("Price: $" + products[i][1]));
li.appendChild(document.createElement('br'));
var btn = document.createElement('button');
btn.className += "btn btn-primary";
var t = document.createTextNode("Add to Cart"); // Create a text node
btn.appendChild(t);
btn.setAttribute("id", products[i][0]);
btn.addEventListener('click', function(e){
let item_id = e.target.id;
if (localStorage.getItem("cart-items")){
let items = JSON.parse(localStorage["cart-items"]);
items.push(item_id);
items = [...new Set(items)];
localStorage.setItem("cart-items", JSON.stringify(items));
}
else{
let item = [item_id];
localStorage.setItem("cart-items", JSON.stringify(item));
}
alert(`${item_id} added to cart`);
});
li.appendChild(btn);
li.appendChild(document.createElement('br'));
li.appendChild(document.createElement('br'));
ul.appendChild(li);
}
}
function clearItems(){
var myNode = document.getElementById("items");
while (myNode.firstChild) {
myNode.removeChild(myNode.firstChild);
}
}
function applyFilters(){
clearItems();
var products = getAllProducts();
products = filterByCategory(products);
products = filterBySearch(products);
displayItems(products);
}
function filterByCategory(products){
var productsTemp = [];
var select = document.getElementById("categoryFilter");
var option = select.value;
for(var i in products) {
if(option == "None" || option == products[i][2]){
productsTemp.push(products[i]);
}
}
return productsTemp;
}
function filterBySearch(products){
var search = document.getElementById("search").value;
var productsTemp = [];
for(var i in products) {
var name = products[i][0];
var shortDescription = products[i][3];
var longDescription = products[i][4];
var match = false;
if(!search){
//Not searching
match = true;
}else if(name.includes(search)){
match = true;
}else if(shortDescription.includes(search)){
match = true;
}
else if(longDescription.includes(search)){
match = true;
}
if(match){
productsTemp.push(products[i]);
}
}
return productsTemp;
}
</script>
</html>