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.
169 lines (126 sloc)
5.12 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
// Register a service worker, this one located in serviceworker.js | |
// A service worker is a piece of code the browser runs behind the scenes. | |
if ('serviceWorker' in navigator) { | |
console.log('CLIENT: service worker registration in progress.'); | |
navigator.serviceWorker.register('sw.js').then(function() { | |
console.log('CLIENT: service worker registration complete.'); | |
}, function() { | |
console.log('CLIENT: service worker registration failure.'); | |
}); | |
} else { | |
console.log('CLIENT: service workers are not supported.'); | |
} | |
let curr_time = new Date(); | |
let time_str = curr_time.getHours().toString() + ":" + curr_time.getMinutes().toString(); | |
document.getElementById("time").innerHTML = time_str; | |
var sampleMessage = { | |
sentBy:"Joel", | |
dateStamp:"02/20/2020 at 2:49", | |
text:"Hello, this is a new message." | |
} | |
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'); | |
var inputBox = document.querySelector("#newMsg"); | |
var sendButton = document.querySelector("#sendBtn"); | |
let notifSound = 'notification-sound.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; | |
let usernameDisplay2 = document.querySelector("#usernameDisplay2"); | |
let userStatusDisplay2 = document.querySelector("#userStatusDisplay2"); | |
let userProfilePicDisplay2 = document.querySelector("#userProfilePicDisplay2"); | |
usernameDisplay2.innerHTML = userName; | |
userStatusDisplay2.innerHTML = userStatus; | |
userProfilePicDisplay2.src = userProfilePic; | |
function saveProfileImageLocally() { | |
if (profilePicInput.files && profilePicInput.files[0]) { | |
var reader = new FileReader(); | |
reader.onload = function(e) { | |
localStorage.setItem("userProfilePic", reader.result); | |
} | |
reader.readAsDataURL(profilePicInput.files[0]); | |
} | |
} | |
function clearChatInput() { | |
document.querySelector("#newMsg").value = ""; | |
document.querySelector("#newMsg").focus(); | |
} | |
sendButton.addEventListener("click", function () { | |
var msgObj = { | |
dateStamp: Date.now(), | |
message: inputBox.value, | |
sentBy: userName | |
} | |
if(msgObj.message !== "") { | |
saveMessageToFirebase(msgObj); | |
window.navigator.vibrate(200); | |
notifSoundElement.play(); | |
} | |
inputBox.value = ""; | |
inputBox.focus(); | |
}) | |
function displayMessage(message) { | |
let curr_date = new Date(message.dateStamp); | |
let month = curr_date.getMonth() + 1; | |
let date_str = month.toString() + "/" + curr_date.getDate().toString() + "/" + curr_date.getFullYear().toString(); | |
let curr_time = new Date(message.dateStamp); | |
let time_str = curr_time.getHours().toString() + ":" + curr_time.getMinutes().toString(); | |
var messageSender = document.createElement("p"); | |
var messageDateTime = document.createElement("p"); | |
var messageParagraph = document.createElement("p"); | |
var messageContent = document.createElement("div"); | |
var messageSendAndDate = document.createElement("div"); | |
if(message.sentBy == localStorage.userName){ | |
messageContent.classList.add("content-right"); | |
messageSendAndDate.classList.add("display-right"); | |
messageParagraph.classList.add("msg-right"); | |
} else { | |
messageContent.classList.add("content-left") | |
messageSendAndDate.classList.add("display-left") | |
messageParagraph.classList.add("msg-left"); | |
} | |
messageParagraph.innerHTML = message.message; | |
messageParagraph.classList.add("sentMsg"); | |
messageSender.innerHTML = "<strong>" + message.sentBy + "</strong>"; | |
messageSender.classList.add("sender"); | |
messageDateTime.innerHTML = time_str + " " + date_str; | |
messageDateTime.classList.add("dateTime") | |
messageSendAndDate.appendChild(messageSender); | |
messageSendAndDate.appendChild(messageDateTime); | |
messageSendAndDate.classList.add("senderAndDate") | |
messageContent.appendChild(messageParagraph); | |
chatBox.appendChild(messageSendAndDate); | |
chatBox.appendChild(messageContent); | |
// chatBox.appendChild(messageDateTime); | |
// chatBox.appendChild(messageSender); | |
chatBox.scrollTop = chatBox.scrollHeight; | |
} | |
watchFirebaseForChanges( | |
function(msg){ | |
displayMessage(msg.data()) | |
} | |
); | |
document.querySelector("#newMsg").addEventListener('keyup', function(e) { | |
if (e.keyCode === 13) { | |
var msgObj = { | |
dateStamp: Date.now(), | |
message: inputBox.value, | |
sentBy: userName | |
} | |
if(msgObj.message !== "") { | |
saveMessageToFirebase(msgObj); | |
window.navigator.vibrate(200); | |
notifSoundElement.play(); | |
} | |
inputBox.value = ""; | |
inputBox.focus(); | |
} | |
}); |