-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
71 lines (64 loc) · 2.35 KB
/
app.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
const form = document.querySelector('form');
const chatWindow = document.querySelector('#chatwindow');
const ul = document.querySelector('ul');
const li = document.querySelectorAll('li');
const logo = document.querySelector('#logo');
const broadcastName = document.querySelector('#broadcastName');
const updateUsername = document.querySelector('#updateUsername');
const identifier = document.querySelector('#identifier');
document.getElementById('message').focus(); //Puts cursor in Message input
const changeRoom = (room) => {
chatWindow.innerHTML = "";
logo.setAttribute('src', `images/${room}.png`)
localStorage.setItem('lastRoom', chatRoom);
li.forEach(button => {
button.innerText === chatRoom.toUpperCase() ? button.classList.add('selected') : button.classList.remove('selected');
});
}
let chatRoom = 'ds9'
if(localStorage.lastRoom){ //Defaults to last used chat room
chatRoom = localStorage.lastRoom;
changeRoom(chatRoom);
}
let author = "Anonymous"
if(localStorage.localUsername){ //Loads up existing username
author = localStorage.localUsername;
broadcastName.textContent = author;
}
// Updates author information and also stores it locally
const addAuthor = () =>{
author = form.user.value;
localStorage.setItem('localUsername', author);
broadcastName.textContent = author;
updateUsername.classList.add('hide');
form.reset();
}
// ****************************
// * Listen for form updates *
// ****************************
window.addEventListener('submit', e => {
e.preventDefault();
if(form.newchat.value && form.user.value){ //User updating both username and sending a message
addAuthor();
addChat();
}
else if(form.newchat.value){ //Just sending a chat
addChat();
}
else if(form.user.value){ //Just updating username
addAuthor();
}
});
// *****************************
// * Listen for room changing *
// *****************************
ul.addEventListener('click', e => {
if(e.target.tagName === "LI"){
chatRoom = e.target.textContent.toLowerCase();
changeRoom(chatRoom);
}
});
// Reveals Update Username field on click
identifier.addEventListener('click', () => {
updateUsername.classList.toggle('hide');
});