-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
533 lines (461 loc) · 17.4 KB
/
index.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
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
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="description" content="">
<title>Local Storage Dashboard</title>
<link rel="icon" type="image/x-icon" href="https://iconarchive.com/download/i81228/creative-freedom/free-vibrant/Database.ico"/>
<!-- Bootstrap core CSS -->
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/bootstrap/5.0.1/css/bootstrap.min.css" integrity="sha512-Ez0cGzNzHR1tYAv56860NLspgUGuQw16GiOOp/I2LuTmpSK9xDXlgJz3XN4cnpXWDmkNBKXR/VDMTCnAaEooxA==" crossorigin="anonymous" referrerpolicy="no-referrer" />
<style>
body {
font-size: .875rem;
}
.feather {
width: 16px;
height: 16px;
vertical-align: text-bottom;
}
/*
* Sidebar
*/
.sidebar {
position: fixed;
top: 0;
/* rtl:raw:
right: 0;
*/
bottom: 0;
/* rtl:remove */
left: 0;
z-index: 100; /* Behind the navbar */
padding: 48px 0 0; /* Height of navbar */
box-shadow: inset -1px 0 0 rgba(0, 0, 0, .1);
}
@media (max-width: 767.98px) {
.sidebar {
top: 5rem;
}
}
.sidebar-sticky {
position: relative;
top: 0;
height: calc(100vh - 48px);
padding-top: .5rem;
overflow-x: hidden;
overflow-y: auto; /* Scrollable contents if viewport is shorter than content. */
}
.sidebar .nav-link {
font-weight: 500;
color: #333;
}
.sidebar .nav-link .feather {
margin-right: 4px;
color: #727272;
}
.sidebar .nav-link.active {
color: #2470dc;
}
.sidebar .nav-link:hover .feather,
.sidebar .nav-link.active .feather {
color: inherit;
}
.sidebar-heading {
font-size: .75rem;
text-transform: uppercase;
}
/*
* Navbar
*/
.navbar-brand {
padding-top: .75rem;
padding-bottom: .75rem;
font-size: 1rem;
background-color: rgba(0, 0, 0, .25);
box-shadow: inset -1px 0 0 rgba(0, 0, 0, .25);
}
.navbar .navbar-toggler {
top: .25rem;
right: 1rem;
}
.navbar .form-control {
padding: .75rem 1rem;
border-width: 0;
border-radius: 0;
}
.form-control-dark {
color: #fff;
background-color: rgba(255, 255, 255, .1);
border-color: rgba(255, 255, 255, .1);
}
.form-control-dark:focus {
border-color: transparent;
box-shadow: 0 0 0 3px rgba(255, 255, 255, .25);
}
.bd-placeholder-img {
font-size: 1.125rem;
text-anchor: middle;
-webkit-user-select: none;
-moz-user-select: none;
user-select: none;
}
@media (min-width: 768px) {
.bd-placeholder-img-lg {
font-size: 3.5rem;
}
}
</style>
</head>
<body>
<script>
window.onload = (event) => {
console.log("Page is fully loaded");
refreshData();
};
function refreshData() {
console.log("Refreshing data...");
document.getElementById("locStorage-table-loading-div").style.display = 'block';
let data = JSON.parse(localStorage.getItem("TestTable"));
clearTable();
if (data) {
buildlocStorageTable(data);
} else {
console.log("No data in Local Storage: TestTable");
}
}
function submitRec() {
let fnameValue = document.getElementById("fname-input").value
let lnameValue = document.getElementById("lname-input").value
let emailValue = document.getElementById("email-input").value
console.log("Submitting Record for: " + fnameValue + " " + lnameValue + " with email: " + emailValue + ".")
let data = JSON.parse(localStorage.getItem("TestTable"));
let newRecord = {};
let lastId = 0;
if (data === null || data === undefined || data == "") {
data = [];
} else {
if (typeof data[data.length - 1]['id'] == 'undefined') {
} else {
lastId = data[data.length - 1]['id'] + 1;
}
}
newRecord['id'] = lastId;
newRecord['fname'] = fnameValue;
newRecord['lname'] = lnameValue;
newRecord['email_addr'] = emailValue;
data.push(newRecord);
localStorage.setItem("TestTable", JSON.stringify(data));
document.getElementById("fname-input").value = ""
document.getElementById("lname-input").value = ""
document.getElementById("email-input").value = ""
refreshData();
}
function searchRecs() {
let searchValue = document.getElementById("searchbox").value.toLowerCase();
let data = JSON.parse(localStorage.getItem("TestTable"));
let searchResults = [];
if (!data) {
alert("No data to search for!")
} else {
data.forEach(element => {
// If the text values in the element contain the searchValue
if (Object.values(element).toString().toLowerCase().includes(searchValue)) {
// Add the whole element to searchresults list
searchResults.push(element);
}
});
clearTable();
buildlocStorageTable(searchResults);
}
}
function deleteRec(recId) {
let data = JSON.parse(localStorage.getItem("TestTable"));
if (!data) {
alert("No data to delete from!")
} else {
for (a = 0; a < data.length; a++) {
if (data[a]['id'] == recId) {
data.splice(a, 1);
}
}
if (data.length < 1) {
localStorage.removeItem("TestTable");
} else {
localStorage.setItem("TestTable", JSON.stringify(data));
}
}
refreshData();
}
function prepEditRecord(recId) {
clearModal();
let data = JSON.parse(localStorage.getItem("TestTable"));
if (!data) {
alert("No data to delete from!")
} else {
for (a = 0; a < data.length; a++) {
if (data[a]['id'] == recId) {
document.getElementById("id-edit-input").value = data[a]['id'];
document.getElementById("fname-edit-input").value = data[a]['fname'];
document.getElementById("lname-edit-input").value = data[a]['lname'];
document.getElementById("email-edit-input").value = data[a]['email_addr'];
break;
}
}
localStorage.setItem("TestTable", JSON.stringify(data));
}
}
function clearModal() {
// Remove all values from the editbox modal
document.getElementById("id-edit-input").value = ""
document.getElementById("fname-edit-input").value = "";
document.getElementById("lname-edit-input").value = ""
document.getElementById("email-edit-input").value = "";
}
function clearTable() {
// Remove all rows from tbody
document.getElementById("locStorage-table-body").innerHTML = '';
}
function resetValues() {
localStorage.removeItem("TestTable");
}
function saveChanges() {
const recId = document.getElementById("id-edit-input").value;
const fnameEdit = document.getElementById("fname-edit-input").value;
const lnameEdit = document.getElementById("lname-edit-input").value;
const emailEdit = document.getElementById("email-edit-input").value;
let data = JSON.parse(localStorage.getItem("TestTable"));
if (!data) {
alert("No record to edit!")
} else {
for (a = 0; a < data.length; a++) {
if (data[a]['id'] == recId) {
data[a]['fname'] = fnameEdit;
data[a]['lname'] = lnameEdit;
data[a]['email_addr'] = emailEdit;
break;
}
}
localStorage.setItem("TestTable", JSON.stringify(data));
}
var myModalEl = document.getElementById('edit-record-modal');
var modal = bootstrap.Modal.getInstance(myModalEl)
modal.hide();
refreshData();
}
function buildlocStorageTable(jsonData) {
var tbody = document.getElementById("locStorage-table-body");
for (var a = 0; a < jsonData.length; a++) {
var row = tbody.insertRow(tbody.rows.length);
var cell1 = row.insertCell(0);
var cell2 = row.insertCell(1);
var cell3 = row.insertCell(2);
var cell4 = row.insertCell(3);
var cell5 = row.insertCell(4);
cell1.innerHTML = jsonData[a]['id'];
cell2.innerHTML = jsonData[a]['fname'];
cell3.innerHTML = jsonData[a]['lname'];
cell4.innerHTML = jsonData[a]['email_addr'];
cell5.innerHTML = '<button type="button" class="btn btn-danger btn-sm" onClick="deleteRec('+jsonData[a]['id']+')">Delete</button> <button type="button" class="btn btn-primary btn-sm" data-bs-toggle="modal" data-bs-target="#edit-record-modal" style="margin-left:7px;" onClick="prepEditRecord('+jsonData[a]['id']+')">Edit</button>'
document.getElementById("locStorage-table-loading-div").style.display = 'none';
}
}
function showUploadBox() {
document.getElementById("import-data-file").style.display = 'block';
document.getElementById("import-data-file").click();
}
function handleUpload() {
const file = document.getElementById("import-data-file").files[0];
if (file.name.slice(-5) != ".json") {
alert("JSON files only please");
document.getElementById("import-data-file").value = "";
return;
}
if (file) {
var reader = new FileReader();
reader.readAsText(file, "UTF-8");
reader.onload = function (evt) {
buildlocStorageTable(JSON.parse(evt.target.result));
localStorage.setItem("TestTable", evt.target.result);
}
reader.onerror = function (evt) {
console.error(evt);
}
}
}
function exportData() {
let data = JSON.parse(localStorage.getItem("TestTable"));
if (data) {
var a = document.createElement("a");
var myFile = new Blob([JSON.stringify(data)], {type: 'application/json'});
window.URL = window.URL || window.webkitURL;
a.href = window.URL.createObjectURL(myFile, {type: "application/json"});
a.download = "LocalStorageSiteExport.json";
a.click();
} else alert("No data to export.");
}
function generateRandomString(length) {
const characters ='abcdefghijklmnopqrstuvwxyz';
let result = '';
const charactersLength = characters.length;
for ( let i = 0; i < length; i++ ) {
result += characters.charAt(Math.floor(Math.random() * charactersLength));
}
return result;
}
function createTestRecs() {
const lastNameArray = ["Nelson", "Robinson", "Love", "Anderson", "Huff", "Gamble", "Werner", "Brandis", "Hawkins", "Smith", "Johnson", "Brady", "Hancock", "Landry"];
const firstNameArray = ["Sarah", "Mike", "Cameron", "Kane", "Augustus", "Fletcher", "Noah", "Wayne", "Lukas", "Howard", "Delilah", "Lauren", "Violet", "Jamiya", "Julie", "Rebecca", "Tanya", "Morgan", "Stephanie", "Alina"];
const emailArray = ["gmail.com", "hotmail.com", "yahoo.com", "comcast.net"];
var lastId = 0;
let data = JSON.parse(localStorage.getItem("TestTable"));
for (t = 0; t < 10; t++) {
if (typeof data == 'undefined' || data == null) {
data = [];
} else {
lastId = data[data.length - 1]['id'] + 1;
}
var newRec = {};
newRec['id'] = lastId;
newRec['fname'] = firstNameArray[Math.floor(Math.random() * firstNameArray.length)];
newRec['lname'] = lastNameArray[Math.floor(Math.random() * lastNameArray.length)];
newRec['email_addr'] = (newRec['fname'] + "." + newRec['lname'] + "@" + emailArray[Math.floor(Math.random() * emailArray.length)]).toLowerCase();
data.push(newRec);
localStorage.setItem("TestTable", JSON.stringify(data));
}
refreshData();
}
</script>
<header class="navbar navbar-dark sticky-top bg-dark flex-md-nowrap p-0 shadow">
<a class="navbar-brand col-md-3 col-lg-2 me-0 px-3" href="#">Local Storage Dashboard</a>
<input class="form-control form-control-dark w-100" type="text" placeholder="Search" id="searchbox" aria-label="Search" autocomplete="off">
</header>
<div class="container-fluid">
<div class="row">
<nav id="sidebarMenu" class="col-md-2 col-lg-1 d-md-block bg-light sidebar collapse">
<div class="position-sticky pt-3">
</div>
<p id="ver" style="position: absolute;bottom: 0;padding-left: 10px;">v1.0.3</p>
</nav>
<main class="col-md-10 ms-sm-auto col-lg-11 px-md-4">
<!--<canvas class="my-4 w-100" id="myChart" width="900" height="380"></canvas>-->
<br />
<p class="lead">This page represents the ease with which we can leverage Bootstrap, Javascript, and Local Storage to create a feature-filled web application using just 1 html file.</p>
<div class="d-flex justify-content-between flex-wrap flex-md-nowrap align-items-center pt-3 pb-2 mb-3 border-bottom">
<h2>TestTable View</h2>
<div class="btn-toolbar mb-2 mb-md-0">
<div class="btn-group me-2">
<button type="button" class="btn btn-sm btn-outline-secondary" onClick="resetValues();refreshData();">Clear</button>
<button type="button" class="btn btn-sm btn-outline-secondary" onClick="refreshData()">Refresh</button>
<button type="button" class="btn btn-sm btn-outline-secondary" onClick="exportData()" id="export-data-btn">Export</button>
<button type="file" class="btn btn-sm btn-outline-secondary" onClick="showUploadBox()" id="import-data-btn" accept=".json">Import</button>
<input type="file" class="btn btn-sm btn-outline-secondary" onChange="handleUpload()" id="import-data-file" accept=".json" style="display:none"/>
</div>
</div>
</div>
<div class="table-responsive" id="locStorage-table-div">
<table class="table table-striped table-sm" id="locStorage-table">
<thead>
<tr>
<th scope="col">ID</th>
<th scope="col">First Name</th>
<th scope="col">Last Name</th>
<th scope="col">Email Address</th>
<th scope="col">Actions</th>
</tr>
</thead>
<tbody id="locStorage-table-body">
<tr>
<td>1</td>
<td>John</td>
<td>Smith</td>
<td>[email protected]</td>
<td><button type="button" class="btn btn-danger btn-sm" onClick="deleteRec()">Delete</button> <button type="button" class="btn btn-primary btn-sm" data-bs-toggle="modal" data-bs-target="#edit-record-modal" onClick="prepEditRecord()">Edit</button></td>
</tr>
</tbody>
</table>
<div class="spinner-border text-primary" role="status" id="locStorage-table-loading-div" style="display:none">
<span class="visually-hidden">Loading...</span>
</div>
<br />
</div>
<div class="d-flex justify-content-between flex-wrap flex-md-nowrap align-items-center pt-3 pb-2 mb-3 border-bottom">
<h1 class="h2">Add Record</h1>
</div>
<div class="bd-example">
<form class="row g-3">
<div class="col-md-4">
<label for="validationServer01" class="form-label">First name</label>
<input type="text" class="form-control" id="fname-input" value="" autocomplete="off" required>
</div>
<div class="col-md-4">
<label for="validationServer02" class="form-label">Last name</label>
<input type="text" class="form-control" id="lname-input" value="" autocomplete="off" required>
</div>
<div class="col-md-4">
<label for="validationServerUsername" class="form-label">Email Address</label>
<div class="input-group has-validation">
<input type="text" class="form-control" id="email-input" aria-describedby="inputGroupPrepend3" autocomplete="off" required>
</div>
</div>
<div class="col-12">
<button class="btn btn-primary" type="button" onClick="submitRec()">Submit Record</button>
<button class="btn btn-secondary" type="button" onClick="createTestRecs()">Create 10 Test Records</button>
</div>
</form>
</div>
</main>
</div>
</div>
<div class="modal fade" id="edit-record-modal" tabindex="-1" aria-labelledby="exampleModalLabel" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="exampleModalLabel">Edit Record</h5>
<button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
</div>
<div class="modal-body">
<input type="text" class="form-control" id="id-edit-input" value="" autocomplete="off" hidden>
<div class="col-md-12">
<label for="validationServer01" class="form-label">First name</label>
<input type="text" class="form-control" id="fname-edit-input" value="" autocomplete="off" required>
</div>
<div class="col-md-12">
<label for="validationServer02" class="form-label">Last name</label>
<input type="text" class="form-control" id="lname-edit-input" value="" autocomplete="off" required>
</div>
<div class="col-md-12">
<label for="validationServerUsername" class="form-label">Email Address</label>
<div class="input-group has-validation">
<input type="text" class="form-control" id="email-edit-input" aria-describedby="inputGroupPrepend3" autocomplete="off" required>
</div>
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Close</button>
<button type="button" class="btn btn-primary" onClick="saveChanges()">Save changes</button>
</div>
</div>
</div>
</div>
<script>
document.getElementById("searchbox").addEventListener("keyup", (event) => {
if (event.keyCode == 13) {
searchRecs();
}
});
document.getElementById("email-input").addEventListener("keyup", (event) => {
if (event.keyCode == 13) {
submitRec();
}
});
document.getElementById("email-edit-input").addEventListener("keyup", (event) => {
if (event.keyCode == 13) {
saveChanges();
}
});
</script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/bootstrap/5.0.1/js/bootstrap.min.js" integrity="sha512-EKWWs1ZcA2ZY9lbLISPz8aGR2+L7JVYqBAYTq5AXgBkSjRSuQEGqWx8R1zAX16KdXPaCjOCaKE8MCpU0wcHlHA==" crossorigin="anonymous" referrerpolicy="no-referrer"></script>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/feather.min.js" integrity="sha384-uO3SXW5IuS1ZpFPKugNNWqTZRRglnUJK6UAZ/gxOX80nxEkN9NcGZTftn6RzhGWE" crossorigin="anonymous"></script>
</body>
</html>