Skip to content

Commit

Permalink
More unjankifying
Browse files Browse the repository at this point in the history
  • Loading branch information
isaacwang4076 committed Feb 14, 2017
1 parent 15dec90 commit 7fc2259
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 109 deletions.
30 changes: 11 additions & 19 deletions public/call.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
/***** General variables **************************/
var me = {user_id: "id2", block_list: ["block1"]};
var myID = getSignedCookie("user_id");
var currRoomID = null;
var currRoomUsers = [];
var isLecturer = false;
/**************************************************/
Expand Down Expand Up @@ -78,6 +77,7 @@ peer.on('call', function(call) {
if (me.block_list.indexOf(call.peer) == -1) {
answerCall(call);
}

});

peer.on("disconnected", function() {
Expand Down Expand Up @@ -213,26 +213,13 @@ function answerCallHelper(call) {

// - updates server and returns list of user_id's
// - calls all user_id's
function joinRoomCall(room_id) {

// we're already in this room!
if (currRoomID == room_id) {
return;
}

// don't want to be in two rooms at once
if (currRoomID != null) {
leaveRoom();
}
function joinRoomCall() {

// set currRoomID
currRoomID = room_id;

console.log("joining room with id " + room_id);
console.log("joining room with id " + currRoomID);

// send request to server
var xhr = new XMLHttpRequest();
xhr.open('GET', "/join_room/" + room_id, true);
xhr.open('GET', "/join_room/" + currRoomID, true);
xhr.send();

// on response
Expand Down Expand Up @@ -376,14 +363,19 @@ function toggleRemoteStreamAudioEnabled(call_id) {
}
/*********************************************************************/
/******************************* MISC ********************************/

// when the window is about to close
window.onbeforeunload = function(event) {
// send request to server to tell them we left
// we don't use leaveRoom because request needs to be async
leaveRoomHard();
};

// makes sure we leave the room
function leaveRoomHard() {
if (currRoomID != null) {
var xhr = new XMLHttpRequest();
xhr.open('GET', "/leave_room/" + currRoomID, false);
xhr.send();
}
};
}
/*********************************************************************/
19 changes: 15 additions & 4 deletions public/controllers/room_controller.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
//Room app -- firebase initialized already
var currRoomID = null;
var chatDatabase = null;
var myApp = angular.module("roomApp", []);
var chatMessageList = [];
Expand Down Expand Up @@ -32,7 +33,7 @@ myApp.controller("ChatController", ["$scope", "$http",
chatMessageList = [];

// empty the message list in UI
updateChatView(chatMessageList);
updateChatView();

// set up and start new listener
chatDatabase = databaseRef.child("RoomMessages").child(currRoomID);
Expand Down Expand Up @@ -85,14 +86,14 @@ myApp.controller("ChatController", ["$scope", "$http",
chatDatabase.limitToLast(30).on("child_added", function(snapshot) {
var snapshotValue = snapshot.val();
chatMessageList.push(snapshotValue);
updateChatView(chatMessageList);
updateChatView();
});
}

// Update the chat view display
function updateChatView(list) {
function updateChatView() {
//console.log("updated chat view");
$scope.chatMessageList = list;
$scope.chatMessageList = chatMessageList;
safeApply();
setTimeout(scrollDown, 1);
}
Expand Down Expand Up @@ -172,6 +173,16 @@ myApp.controller("classesController", function($scope, $rootScope) {
// OnClick method that delegates to joinRoomCall and joinRoomChat
$scope.joinRoom = function(room_id){

// if we're already in this room, do nothing
if (currRoomID == room_id) {
return;
}
// leave previous room
leaveRoom();

// set currRoomID for both joinRoomCall and joinRoomChat
currRoomID = room_id;

// delegate to call.js to join the room's call
joinRoomCall(room_id);

Expand Down
90 changes: 8 additions & 82 deletions public/main.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
/***** General variables **************************/
var class_rooms = {} // class_id : class rooms
var rooms = {} // room_id : room
var currRoomID = null;
/**************************************************/

//getClasses();

/****************************** ON-CLICKS ****************************/
function addRoomOnClick() {
var class_id = $('#class_id input:radio:checked').val();
Expand All @@ -16,7 +13,6 @@ function addRoomOnClick() {
addRoom(class_id, room_name, is_lecture);
}
/*********************************************************************/

/******************************** MODEL ******************************/

function Room(room_id, room_name, room_host_id, room_users, class_id, is_lecture, has_tutor) {
Expand All @@ -30,82 +26,6 @@ function Room(room_id, room_name, room_host_id, room_users, class_id, is_lecture
}

/*********************************************************************/

/************************* GET CLASSES & ROOMS ***********************/

// - respond to change in a class's rooms
// - calls removeRoom/getRoom accordingly
/*function onClassRoomsChange(class_id, updated_rooms) {
console.log("rooms for class " + class_id + " are now " + updated_rooms);
// if we have a previous record of this class
if (class_rooms[class_id] != null) {
// remove old rooms
for (i = 0; i < class_rooms[class_id].length; i++) {
var room_id = class_rooms[class_id][i];
// if they aren't in the new list
if (updated_rooms.indexOf(room_id) == -1) {
removeRoom(room_id);
}
}
}
// get new rooms
for (i = 0; i < updated_rooms.length; i++) {
var room_id = updated_rooms[i];
// only if we haven't already gotten it
if (class_rooms[class_id] == null ||
class_rooms[class_id].indexOf(room_id) == -1) {
getRoom(class_id, room_id);
}
}
// set the rooms for this class to the updated version
class_rooms[class_id] = updated_rooms;
}
// - gets all room info for specified room
// - adds the room to rooms and the UI
function getRoom(class_id, room_id) {
console.log("Getting room with id " + room_id);
// add listener for room info
roomsDatabase.child(room_id).on("value", function(snapshot) {
var room = snapshot.val();
if (room) {
// TODO: update UI to add the room
// store the room
rooms[room_id] = new Room(room_id, room.name, room.host_id, room.class_id,
room.is_lecture, room.has_tutor, room.users);
console.log("Got room: ");
console.log(rooms[room_id]);
}
});
}*/

// - removes room from rooms and the UI
function removeRoom(room_id) {

console.log("Removing room with id " + room_id);

roomsDatabase.child(room_id).off();

delete rooms[room_id];

// TODO: update UI
}
/*********************************************************************/

/*************************** CREATING ROOMS **************************/

function addRoom(class_id, room_name, is_lecture) {
Expand All @@ -128,13 +48,19 @@ function addRoom(class_id, room_name, is_lecture) {
}

/*********************************************************************/

/************************* ACCOUNT MANAGEMENT ************************/

function logOut() {

// leave current room
leaveRoomHard();

// erase cookies
removeCookie("user_id");
removeCookie("email");
removeCookie("name");

// go to home
document.location.href = "/";
}

Expand Down
8 changes: 4 additions & 4 deletions public/mainRoom.html
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ <h4 class="panel-title">

<!-- Classmates Sidebar -->
<aside id="sidebar-classmates" role="complementary" style="background-color:green">
<h3>Column 3</h3>
<h3>Buddies</h3>
</aside>

<!-- Create Room Modal -->
Expand Down Expand Up @@ -219,12 +219,12 @@ <h1 class="title">Create a room</h1>
<script src="https://www.gstatic.com/firebasejs/3.6.7/firebase.js"></script>
<script src="firebase_setup.js"></script>


<!-- Other -->
<script src="cookies.js"></script>

<script src="call.js"></script>
<script src="main.js"></script>

<script src="call.js"></script>

<!-- Angular scripts -->
<script src="controllers/room_controller.js"></script>

Expand Down

0 comments on commit 7fc2259

Please sign in to comment.