Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
9 changed files
with
231 additions
and
17 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
<html> | ||
<head> | ||
<meta charset="utf-8"> | ||
<meta name="theme-color" content="#f00" /> | ||
<meta name="viewport" content="width=device-width, initial-scale=1"> | ||
<title>DMD 3440 - Room of Chat</title> | ||
<link rel="manifest" href="manifest.json?version=2"> | ||
</head> | ||
<body> | ||
<div id="chatLog"></div> | ||
<style> | ||
#chatLog{ | ||
max-height: 80vh; | ||
overflow: scroll; | ||
} | ||
.myMessage{ | ||
position: fixed; | ||
width: 100%; | ||
bottom: 0px; | ||
} | ||
</style> | ||
|
||
<div id="chatLog"></div> | ||
<div>My Profile pic: | ||
<input type="file" id="profilePic"> | ||
<button id="saveProfile"></button> | ||
|
||
</div> | ||
|
||
</div> | ||
|
||
|
||
<div class="myMessage"> | ||
<input type="text" id="inputBox"> | ||
<button id="sendButton">Send</button> | ||
</div> | ||
|
||
|
||
<!-- Firebase App (the core Firebase SDK) is always required and must be listed first --> | ||
<script src="https://www.gstatic.com/firebasejs/7.9.2/firebase-app.js"></script> | ||
|
||
<!-- If you enabled Analytics in your project, add the Firebase SDK for Analytics --> | ||
<script src="https://www.gstatic.com/firebasejs/7.9.2/firebase-analytics.js"></script> | ||
|
||
<!-- Add Firebase products that you want to use --> | ||
<script src="https://www.gstatic.com/firebasejs/7.9.2/firebase-auth.js"></script> | ||
<script src="https://www.gstatic.com/firebasejs/7.9.2/firebase-firestore.js"></script> | ||
<script src="js/firebase-config.js"></script> | ||
<script src="js/register-sw.js"></script> | ||
<script src="js/profile.js"></script> | ||
<script src="js/firebase-chat-functions.js"></script> | ||
<script src="js/demo.js"></script> | ||
|
||
</body> | ||
</html> |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
// var initialMessages = getAllMessagesFromFirebase(); | ||
// console.log(initialMessages); | ||
var username = "Meira"; | ||
var chatLog = document.querySelector("#chatLog"); | ||
var inputBox = document.querySelector("#inputBox"); | ||
var sendButton = document.querySelector("#sendButton"); | ||
var profilePicInput = document.querySelector("#profilePic"); | ||
var saveProfileButton = document.querySelector("#saveProfile"); | ||
var profilePicDisplayElement = document.querySelector("#profilePicDisplay"); | ||
var profilePic = localStorage.getItem("userProfilePic") || "unknown.jpg"; | ||
|
||
var testMessage = { | ||
dateStamp:"983459835498345", | ||
message: "Hello test!", | ||
sentBy: "Meira" | ||
} | ||
|
||
profilePicDisplayElement.src = profilePic; | ||
|
||
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 displayMessage(message) { | ||
var messageParagraph = document.createElement("p"); | ||
messageParagraph.innerHTML = message.dateStamp + " " + message.message + " " + message.sentBy; | ||
|
||
chatLog.appendChild(messageParagraph); | ||
chatLog.scrollTop = chatLog.scrollHeight; | ||
} | ||
|
||
watchFirebaseForChanges( | ||
function(msg){ | ||
displayMessage(msg.data()) | ||
} | ||
); | ||
|
||
sendButton.addEventListener("click", function () { | ||
var msgObj = { | ||
dateStamp:Date.now(), | ||
message: inputBox.value, | ||
sentBy: username | ||
} | ||
|
||
saveMessageToFirebase(msgObj); | ||
|
||
inputBox.value = ""; | ||
inputBox.focus(); | ||
}); | ||
|
||
saveProfileButton.addEventListener("click", function() { | ||
saveProfileImageLocally(); | ||
}); |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
function saveMessageToFirebase(msg) { | ||
db.collection("messages").add(msg) | ||
} | ||
|
||
function getAllMessagesFromFirebase() { | ||
let allMsgs = []; | ||
db.collection("messages").orderBy('dateStamp', 'asc').limit(200).get().then(function(querySnapshot) { | ||
querySnapshot.forEach(function(doc) { | ||
// doc.data() is never undefined for query doc snapshots | ||
allMsgs.push(doc.data()) | ||
}); | ||
}); | ||
|
||
return allMsgs; | ||
} | ||
|
||
function watchFirebaseForChanges(callBack) { | ||
db.collection("messages").orderBy('dateStamp','asc').onSnapshot(function(querySnapshot) { | ||
querySnapshot.docChanges().forEach(function(change) { | ||
if (change.type === "added") { | ||
callBack(change.doc); | ||
} | ||
}); | ||
}); | ||
} | ||
|
||
|
||
//getAllMessagesFromFirebase(); | ||
//watchFirebaseForChanges(function(msg){displayNewMessage(msg.data())}) |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
var firebaseConfig = { | ||
apiKey: "AIzaSyAbh39Rb-F9_PO8lpAoob_qA1dYUVNYAkg", | ||
authDomain: "dmd-3440-pwa-demo.firebaseapp.com", | ||
databaseURL: "https://dmd-3440-pwa-demo.firebaseio.com", | ||
projectId: "dmd-3440-pwa-demo", | ||
storageBucket: "dmd-3440-pwa-demo.appspot.com", | ||
messagingSenderId: "239477441112", | ||
appId: "1:239477441112:web:0dcd3f94fe067b09ac402d" | ||
}; | ||
// Initialize Firebase | ||
firebase.initializeApp(firebaseConfig); | ||
var db = firebase.firestore(); |
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
// 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.'); | ||
} |
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