Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
~
  • Loading branch information
yuh19027 committed May 1, 2020
1 parent 9bde515 commit 4dfa6d7
Show file tree
Hide file tree
Showing 9 changed files with 154 additions and 100 deletions.
Binary file modified .DS_Store
Binary file not shown.
Binary file added chatmate-app.xd
Binary file not shown.
3 changes: 2 additions & 1 deletion css/profile.css
Expand Up @@ -5,7 +5,8 @@ body{

header{
background-color: #3e2fae;
min-height: 40vh
min-height: 50vh;
width: 100vw;
}
header a img{
display: block;
Expand Down
Binary file modified imgs/.DS_Store
Binary file not shown.
1 change: 1 addition & 0 deletions imgs/hamburger.svg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added imgs/icon-transparent.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion index.html 100644 → 100755
Expand Up @@ -14,7 +14,7 @@
<header>
<a><img src="imgs/more.png" alt="Menu Icon" height = 20px width = 30px></a>
<p class=>
<h2>Welcome to Chatmate! &nbsp</h2>
<h2>DMD3440 Chat Room</h2>
</p>
<a href="profile.html"><img src="imgs/profile.png" alt="Profile Icon" height = 30px width = 25px></a>
</header>
Expand Down
31 changes: 28 additions & 3 deletions main.css 100644 → 100755
Expand Up @@ -29,13 +29,32 @@ header{

}

#chatBox p{
/* #chatBox p{
display: inline-block;
background-color: #f9d861;
padding: 7px 10px;
border-radius: 10px;
} */

#chatBox p.user-message {
display: inline-block;
padding: 7px 10px;
background-color:#3e2fae;
color: white;
border-radius: 10px 0px 10px 10px;
float: right;
}

#chatBox p.other-message {
display: inline-block;
padding: 7px 10px;
background-color:#f9d861;
border-radius: 0px 10px 10px 10px;
}

#chatBox h4.user-name{
text-align: right;
}

#chatInput {
position:fixed;
Expand All @@ -54,9 +73,10 @@ header{
#chatInput input {
width:100%;
text-indent: 10px;
background-color: #eee;
background-color: #ffffff;
border-radius: 15px;
border:2px solid #3e2fae;
border:2px solid #3e2fae;
font-family: 'Josefin Sans', sans-serif;
margin-right: 10px;

}
Expand Down Expand Up @@ -96,4 +116,9 @@ h4{
}
.info{
text-align: center;
}

.time-date{
font-size: 0.7em;
color: #707070;
}
217 changes: 122 additions & 95 deletions main.js 100644 → 100755
@@ -1,123 +1,150 @@
// 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.');
}
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 alllMessages = [];
let chatBox = document.querySelector("#chatBox");
let newMsgInput = document.querySelector("#newMsg");
let userName = localStorage.getItem('userName') || "Yucheng";
let userStatus = localStorage.getItem('userStatus') || "update your profile to display a status";
let userProfilePic = localStorage.getItem('userProfilePic');
let usernameDisplay = document.querySelector("#usernameDisplay");
let userStatusDisplay = document.querySelector("#userStatusDisplay");
let userProfilePicDisplay = document.querySelector("#userProfilePicDisplay");
let sendButton = document.querySelector("#sendBtn");
let notifSound = 'notification.mp3';
let notifSoundElement = new Audio(notifSound);


let alllMessages = [];
let chatBox = document.querySelector("#chatBox");
let newMsgInput = document.querySelector("#newMsg");
let userName = localStorage.getItem('userName') || "Yucheng";
let userStatus = localStorage.getItem('userStatus') || "update your profile to display a status";
let userProfilePic = localStorage.getItem('userProfilePic');
let notifSound = 'notification.mp3';
let notifSoundElement = new Audio(notifSound);
let usernameDisplay = document.querySelector("#usernameDisplay");
let userStatusDisplay = document.querySelector("#userStatusDisplay");
let userProfilePicDisplay = document.querySelector("#userProfilePicDisplay");
let sendButton = document.querySelector("#sendBtn")


usernameDisplay.innerHTML = userName;
userStatusDisplay.innerHTML = userStatus;
userProfilePicDisplay.src = userProfilePic;
usernameDisplay.innerHTML = userName;
userStatusDisplay.innerHTML = userStatus;
userProfilePicDisplay.src = userProfilePic;

function clearChatInput() {
document.querySelector("#newMsg").value = "";
document.querySelector("#newMsg").focus();
}

let curr_date = new Date();
let month = curr_date.getMonth() + 1;
let date_str = month.toString() + "/" + curr_date.getDate().toString() + "/" + curr_date.getFullYear().toString();

let curr_time = new Date();
let time_str = curr_time.getHours().toString() + ":" + curr_time.getMinutes().toString();


//display message
function displayNewMessage(msg) {
var newMsgUsername = document.createElement('h4');
var newMsgP = document.createElement("p");

function clearChatInput() {
document.querySelector("#newMsg").value = "";
document.querySelector("#newMsg").focus();
// user's message on the right while others' message on the left
if(msg.sentBy == localStorage.userName){
newMsgUsername.classList.add("user-name");
newMsgP.classList.add("user-message");
}else{
newMsgUsername.classList.add("other-name");
newMsgP.classList.add("other-message");
}

let curr_date = new Date();
let month = curr_date.getMonth() + 1;
let date_str = month.toString() + "/" + curr_date.getDate().toString() + "/" + curr_date.getFullYear().toString();


function displayNewMessage(msg) {
var newMsgUsername = document.createElement('h4');
var newMsgP = document.createElement("p");
newMsgUsername.innerHTML = msg.date_str + " " + msg.sentBy + " ";
newMsgP.innerHTML = msg.message;
chatBox.appendChild(newMsgUsername);
chatBox.appendChild(newMsgP);

window.navigator.vibrate(200);

notifSoundElement.play();
newMsgUsername.innerHTML ="<strong>" + msg.sentBy + "</strong>" + "<br>" + "<span class=\"time-date\">" + msg.time_str + "</span>"+ " " + "<span class=\"time-date\">" + msg.date_str + "</span>";
newMsgP.innerHTML = msg.message;
chatBox.appendChild(newMsgUsername);
chatBox.appendChild(newMsgP);

window.navigator.vibrate(200);

chatBox.scrollTop = chatBox.scrollHeight;
notifSoundElement.play();

clearChatInput();
}
chatBox.scrollTop = chatBox.scrollHeight;

function makeNewMessageFromLocalUser() {
let usrMsg = {
sentBy: userName,
date_str: month.toString() + "/" + curr_date.getDate().toString() + "/" + curr_date.getFullYear().toString(),
//dateStamp: Date.now(),
message: newMsgInput.value
}
clearChatInput();
}

if(usrMsg.message !== "") {
//allMessages.push(usrMsg);
// end make message
displayNewMessage(usrMsg);
saveMessageToFirebase(usrMsg);
}
}
watchFirebaseForChanges(

watchFirebaseForChanges(
function(msg){
displayNewMessage(msg.data())
displayMessage(msg.data())
}
);
/** 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();
}
});

// when username input is changed, update localstorage and global variable
// usernameDisplay.addEventListener('blur', function() {
// var oldUsername = localStorage.getItem("userName");
// var newUsername = this.innerHTML;
function makeNewMessageFromLocalUser() {
let usrMsg = {
sentBy: userName,
date_str: month.toString() + "/" + curr_date.getDate().toString() + "/" + curr_date.getFullYear().toString(),
//dateStamp: Date.now(),
message: newMsgInput.value
}

// var alertNew = {
// sentBy: "system",
// dateStamp: Date.now(),
// message: `${oldUsername} has changed their name to ${newUsername}`
// }
if(usrMsg.message !== "") {
//allMessages.push(usrMsg);
// end make message
displayNewMessage(usrMsg);
saveMessageToFirebase(usrMsg);
}
}
watchFirebaseForChanges(
function(msg){
displayNewMessage(msg.data())
}
);
/** All the triggers and event listener assignments down here... */

// displayNewMessage(alertNew);
// 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

// localStorage.setItem("userName", newUsername);
// userName = localStorage.getItem("userName");
// });
document.querySelector("#newMsg").addEventListener('keyup', function(e) {
if (e.keyCode === 13) {
makeNewMessageFromLocalUser();
}
});

sendButton.addEventListener("click", function () {
var msgObj = {
dateStamp:Date.now(),
message: inputBox.value,
sentBy: username
}
// when username input is changed, update localstorage and global variable
// usernameDisplay.addEventListener('blur', function() {
// var oldUsername = localStorage.getItem("userName");
// var newUsername = this.innerHTML;

saveMessageToFirebase(msgObj);
// var alertNew = {
// sentBy: "system",
// dateStamp: Date.now(),
// message: `${oldUsername} has changed their name to ${newUsername}`
// }

inputBox.value = "";
inputBox.focus();
});
// displayNewMessage(alertNew);


// localStorage.setItem("userName", newUsername);
// userName = localStorage.getItem("userName");
// });

sendButton.addEventListener("click", function () {
var msgObj = {
dateStamp:Date.now(),
message: inputBox.value,
sentBy: username
}

saveMessageToFirebase(msgObj);

inputBox.value = "";
inputBox.focus();
});

0 comments on commit 4dfa6d7

Please sign in to comment.