generated from Code-Institute-Org/gitpod-full-template
-
Notifications
You must be signed in to change notification settings - Fork 1
/
sidebar.js
84 lines (66 loc) · 2.37 KB
/
sidebar.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
//assigning container and sidebar to variables.
let sidebarContainer = document.getElementById('sidebar-container');
let sidebarMenu = document.getElementById('sidebar');
//close sidebar if clicked outside of it.
sidebarContainer.addEventListener('click', closeSidebar);
//add event listener to menu button -> run openSidebar.
let menuButton = document.getElementById('menu-button');
menuButton.addEventListener('click', openSidebar);
//add event listener to close button -> run closeSidebar.
let closeMenu = document.getElementById('close-menu');
closeMenu.addEventListener('click', closeSidebar);
/**
* Make container visible, disable scrolling, open sidebar.
*/
function openSidebar() {
sidebarContainer.style.visibility = 'visible';
document.body.style.overflow = 'hidden';
sidebarMenu.classList.remove('sidebar-closed');
sidebarMenu.classList.add('sidebar-open');
}
/**
* Make container invisible, enable scrolling, close sidebar.
*/
function closeSidebar() {
sidebarContainer.style.visibility = 'hidden';
document.body.style.overflow = 'initial';
sidebarMenu.classList.remove('sidebar-open');
sidebarMenu.classList.add('sidebar-closed');
}
let nameSpan = document.getElementById('name-span');
let userName = localStorage.getItem('username');
window.onload = loadName();
/**
* Gets username from local storage and populates span.
*/
function loadName() {
//get username.
if (userName === 'null' || userName === null || userName === '') {
//if there is no username set, use 'Employee'.
userName = 'Employee';
console.log('No username');
} else {
//else fill span with username from local storage.
nameSpan.innerHTML = userName;
console.log('Username exists: ' + userName);
}
}
//add event listener to username div in header -> run addName.
let userMenu = document.getElementById('user-name');
userMenu.addEventListener('click', addName);
/**
* Calls a prompt, ouput of which is set as username in localstorage.
*/
function addName() {
var namePrompt = prompt('What is your name?');
//set username.
if (namePrompt === 'null' || namePrompt === null || namePrompt === '') {
//if user cancels out of prompt.
nameSpan.innerHTML = 'Employee';
localStorage.setItem('username', namePrompt);
} else {
//fill name-span with input from prompt.
nameSpan.innerHTML = namePrompt;
localStorage.setItem('username', namePrompt);
}
}