-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfunctions.js
187 lines (147 loc) · 4.38 KB
/
functions.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
function click(elementId, fn) {
var element = document.getElementById(elementId);
if (element) {
element.addEventListener("click", fn);
}
}
function redirect(path) {
window.location = path;
return false;
}
function loginWithGoogle() {
var provider = new firebase.auth.GoogleAuthProvider();
firebase.auth().signInWithPopup(provider).then(function(result) {
// The signed-in user info.
var user = result.user;
// Create user
createUser(user.uid, user.displayName, user.email, user.photoURL);
}).catch(function(error) {
console.log(error.message);
});
}
function logInUser() {
// Log in with Google
// using Firebase
loginWithGoogle();
}
function createUser(uid, uname, uemail, photoURL) {
// Get a reference to the database service
var database = firebase.database();
var usersRef = database.ref("users");
var user = {
id: uid,
name: uname,
email: uemail,
imageUrl: photoURL
};
usersRef.child(uid).set(user).then(function() {
redirect("chat.html");
});
}
function ifUserIsLoggedIn(fn) {
firebase.auth().onAuthStateChanged(function(user) {
if (user) {
window.currentUser = {
id: user.uid,
name: user.displayName,
email: user.email,
imageUrl: user.photoURL
};
fn();
} else {
// No user is signed in.
// Redirect to home page
redirect('index.html');
}
});
}
function getElement(id) {
return document.getElementById(id);
}
function updateUserData() {
var usernameElement = getElement("username");
var userImageElement = getElement("user-image");
usernameElement.textContent = window.currentUser.name;
userImageElement.src = window.currentUser.imageUrl;
}
function loadUsers(fn) {
var database = firebase.database();
var usersRef = database.ref("users");
usersRef.on('value', function(snapshot) {
var users = snapshot.val();
fn(users);
});
}
function renderUser(user) {
var uid = user.id;
var chat_id = getChatId(window.currentUser.id, uid);
var name = user.name;
var imageUrl = user.imageUrl;
if (!imageUrl) {
imageUrl = "default.png";
}
var html = '<div id="' + chat_id + '" class="member"><img src="'+ imageUrl +'">';
html += '<span id="member-name">' + name + '</span>';
html+= '</div>';
return html;
}
function getChatId(id1, id2) {
if (id1 > id2) {
return id1 + "" + id2;
}
return id2 + "" + id1;
}
function onClickMultiple(className, func) {
document.addEventListener('click', function(event) {
if (event.target.classList.contains(className)) {
func(event.target);
}
});
}
function loadMessages(chat_id, fn) {
var database = firebase.database();
var chatsRef = database.ref("chats");
chatsRef.child(chat_id).on('value', function(snapshot) {
var messages = snapshot.val();
fn(messages);
});
}
function renderMessage(message) {
var text = message.text;
var msgClass = "message";
if (message.sender_id == window.currentUser.id) {
msgClass = "message by-user";
}
var html = '<div class="'+ msgClass +'">' + text + '</div>';
return html;
}
function sendMessage(chat_id, text, fn) {
var message = {
text: text,
sender_id: window.currentUser.id
};
var database = firebase.database();
var chatsRef = database.ref("chats");
var chat = chatsRef.child(chat_id);
var newMessageId = chatsRef.push().key;
chat.child(newMessageId).set(message).then(fn);
}
function removeClassMultiple(elementClass, classNameToRemove) {
var elements = document.getElementsByClassName(elementClass);
for (var i = 0; i < elements.length; i++) {
elements[i].classList.remove(classNameToRemove);
}
}
function scrollToBottom(element) {
element.scrollTop = element.scrollHeight;
}
function logoutUser() {
// Sign out user
firebase.auth().signOut().then(function() {
// User signed out
// redirect to home page.
redirect('index.html');
}, function(error) {
console.log(error);
});
}