-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfiregroupchat.js
170 lines (142 loc) · 5.05 KB
/
firegroupchat.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
'use strict';
// Add your Firebase app url here
var ref = new Firebase("https://<YOUR-FIREBASE-APP>.firebaseio.com");
//check authentication state
var authData = ref.getAuth();
//Create user list html
function userHtml(user){
if(user.connected){
var html = '<li class="media"><div class="media-body"><div class="media">';
html += '<a class="pull-left" href="javascript:;">';
html += '<img class="media-object img-circle" style="max-height:40px;" src="'+user.img+'" /></a>';
html += '<div class="media-body" ><h5>'+user.name+'</h5></div></div></div></li>';
$('.user_list').prepend(html);
}
}
//Create chatlist html
function chatHtml(chat){
var date = moment(chat.date).format("Do MMMM, h:mm a");
var html = '<li class="media cht"> <div class="media-body"><div class="media">';
html +='<a class="pull-left" href="javascript:;"><img class="media-object img-circle " src="'+chat.img+'" /></a>';
html +='<div class="media-body" >'+chat.msg+' <br /><small class="text-muted">'+chat.name+' | '+date+'</small><hr />';
html +='</div></div></div></li>';
$('.chat_list').append(html);
}
//Always scroll to last message
function scrolltoTop(){
$('.chat_list').animate({scrollTop: $('.chat_list')[0].scrollHeight});
}
//Get all user and chat data from firebase
function getData(authData){
$('.fblogin').addClass('hide');
$('.chat_container').removeClass('hide');
$('.user_container').removeClass('hide');
//Called anytime new user data is added or updated in firebase
ref.child("users").on("value", function(snapshot) {
$('.user_list').empty();
ref.child("users").on("child_added", function(snapshot) {
userHtml(snapshot.val());
});
});
var initialData = false;
//Triggered once for each existing chat and then again every time a new chat is added
ref.child("chats").on("child_added", function(snapshot) {
var chat = snapshot.val();
chatHtml(chat);
if(initialData){
scrolltoTop();
if(authData.uid != chat.uid)
PageTitleNotification.On(chat.name+" says...", 2000);
}
});
//trigger after load all chat data and scroll to last message
ref.child("chats").once("value", function(snap) {
initialData = true
scrolltoTop();
});
var myConnectionsRef = ref.child('/users/'+authData.uid+'/connected');
// stores the timestamp of my last disconnect (the last time I was seen online)
var lastOnlineRef = ref.child('/users/'+authData.uid+'/lastOnline');
var connectedRef = ref.child('/.info/connected');
connectedRef.on('value', function(snap) {
if (snap.val() === true) {
// Do anything here that should happen only if online (or on reconnect)
/// add this device to my connections list
var con = myConnectionsRef.push(true);
// when I disconnect, remove this device
con.onDisconnect().remove();
// when I disconnect, update the last time I was seen online
lastOnlineRef.onDisconnect().set(Firebase.ServerValue.TIMESTAMP);
}
});
}
// Check user is logged in or not
if(authData) {
getData(authData);
}else {
$('.fblogin').removeClass('hide');
console.log("User is not logged in");
}
// Authenticate user with facebook and save user data to firebase
$('.fblogin').click(function(){
ref.authWithOAuthPopup("facebook", function(error, authData) {
if (error) {
console.log("Login Failed!", error);
}else {
ref.child("users").child(authData.uid).set({
uid: authData.uid,
provider: authData.provider,
name: authData.facebook.displayName,
img: authData.facebook.cachedUserProfile.picture.data.url,
});
getData(authData);
}
});
});
//Send message to firebase server
$('.btn_send').click(function(){
var msg = $('#inputMsg').val();
var authData = ref.getAuth();
if(msg.length > 0 && ref.getAuth()){
ref.child("chats").push({
uid: authData.uid,
name: authData.facebook.displayName,
img: authData.facebook.cachedUserProfile.picture.data.url,
msg: msg,
date: Firebase.ServerValue.TIMESTAMP
})
$('#inputMsg').val('')
}
});
//Trigger enter key keypress
$('#inputMsg').keypress(function (e) {
var key = e.which;
if(key == 13)
{
$('.btn_send').click();
return false;
}
});
//Show new messages alert on the page title bar
var PageTitleNotification = {
Vars:{
OriginalTitle: document.title,
Interval: null
},
On: function(notification, intervalSpeed){
var _this = this;
_this.Vars.Interval = setInterval(function(){
document.title = (_this.Vars.OriginalTitle == document.title)
? notification
: _this.Vars.OriginalTitle;
}, (intervalSpeed) ? intervalSpeed : 3000);
},
Off: function(){
clearInterval(this.Vars.Interval);
document.title = this.Vars.OriginalTitle;
}
}
//Close new message alert when focus on the message input field
$( "#inputMsg" ).focus(function() {
PageTitleNotification.Off();
});