Permalink
Cannot retrieve contributors at this time
Name already in use
A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
dmd3440-chat-app/main.js
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
67 lines (51 sloc)
2.06 KB
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let allMessages = []; | |
let chatBox = document.querySelector("#chatBox"); | |
let newMsgInput = document.querySelector("#newMsg"); | |
let userName = localStorage.getItem('userName') || "me"; | |
let userStatus = localStorage.getItem('userStatus') || "update your profile to display a status"; | |
let userProfilePic = localStorage.getItem('userProfilePic'); | |
let notifSound = 'for-sure.mp3'; | |
let notifSoundElement = new Audio(notifSound); | |
let usernameDisplay = document.querySelector("#usernameDisplay"); | |
let userStatusDisplay = document.querySelector("#userStatusDisplay"); | |
let userProfilePicDisplay = document.querySelector("#userProfilePicDisplay"); | |
usernameDisplay.innerHTML = userName; | |
userStatusDisplay.innerHTML = userStatus; | |
userProfilePicDisplay.src = userProfilePic; | |
function clearChatInput() { | |
document.querySelector("#newMsg").value = ""; | |
document.querySelector("#newMsg").focus(); | |
} | |
function displayNewMessage(msg) { | |
var newMsgP = document.createElement("p"); | |
newMsgP.innerHTML = "<strong>" + msg.sentBy + "</strong>: " + msg.message; | |
chatBox.appendChild(newMsgP); | |
window.navigator.vibrate(200); | |
notifSoundElement.play(); | |
chatBox.scrollTop = chatBox.scrollHeight; | |
clearChatInput(); | |
} | |
function makeNewMessageFromLocalUser() { | |
let usrMsg = { | |
sentBy: userName, | |
dateStamp: Date.now(), | |
message: newMsgInput.value | |
} | |
if(usrMsg.message !== "") { | |
// allMessages.push(usrMsg); | |
// // end make message | |
// displayNewMessage(usrMsg) | |
saveMessageToFirebase(usrMsg); | |
} | |
} | |
//** All the triggers and event listener assignments down here... */ | |
// assign the "Make New Message" function to the button click | |
document.querySelector("#sendBtn").addEventListener('click', function(){ | |
makeNewMessageFromLocalUser() | |
}); | |
// since we're not using a real form, assign it to the "keyup return" of the message input too | |
document.querySelector("#newMsg").addEventListener('keyup', function(e) { | |
if (e.keyCode === 13) { | |
makeNewMessageFromLocalUser(); | |
} | |
}); |