-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
180 lines (162 loc) · 6.47 KB
/
index.html
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Chat con Firebase</title>
<!-- Latest compiled and minified CSS -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
<style>
body { padding-bottom: 70px; }
#img_user {
width: 50px;
height: 50px;
border-radius: 25px;
margin-right: 10px;
}
.img_chat{
width: 35px;
height: 35px;
border-radius: 18px;
margin-right: 10px;
}
.datos_user{
display: none;
text-align: right;
}
a, a:hover, a:active, a:focus, a:visited{
color: white;
text-decoration: none;
}
.centrado{
text-align: center;
}
.contenedor_base{
display: none;
}
.blanquito{
color: white;
}
</style>
</head>
<body>
<nav class="navbar navbar-inverse">
<div class="container">
<div class="row">
<div class="col-lg-3 col md-3 col-sm-6 col-xs-6">
<a href="#" class="navbar-brand blanquito">
<p>ChatUPP</p>
</a>
</div>
<div class="col-lg-9 col md-9 col-sm-6 col-xs-6">
<div class="datos_user">
<img src="" alt="" id="img_user">
<span class="droptown blanquito">
<a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false"><span id="name_user"></span> <span class="caret"></span></a>
<ul class="dropdown-menu dropdown-menu-right">
<li id="logout"><a role="button" href="#">Cerrar sesión</a></li>
</ul>
</span>
</div>
</div>
</div>
</div>
</nav>
<div class="container-fluid">
<div class="row">
<div class="col-lg-12 col-md-12 col-sm-12 col-xs-12 centrado">
<button id="facebook" class="btn btn-primary">
Inicia sesión con Facebook para entrar al chat!
</button>
</div>
</div>
<div id="contenedor-base">
<div class="row">
<div class="col-lg-12 col-md-12 col-sm-12 col-xs 12">
<div class="panel panel-default">
<div class="panel-heading">
<h3 class="panel-title">Mensajes del chat</h3>
</div>
<div class="panel-body">
<ul class="list-group" id="box">
</ul>
</div>
</div>
</div>
</div>
<div class="input-group col-lg-6 col-lg-offset-3 col-md-6 col-md-offset-3 col-sm-offset-1 col-xs-offset-1 col-sm-10 col-xs-10">
<input type="text" class="form-control" id="cuerpo" placeholder="Ingresa un mensaje" aria-describedby="enviar">
<span class="input-group-btn">
<button class="btn" id="enviar"><i class="fa fa-plus" aria-hidden="true"></i></button>
</span>
</div>
</div>
</div>
<!-- Scripts de JQuery y Firebase -->
<script
src="https://code.jquery.com/jquery-3.2.1.js"
integrity="sha256-DZAnKJ/6XZ9si04Hgrsxu/8s717jcIzLy3oi35EouyE="
crossorigin="anonymous"></script>
<script src="https://www.gstatic.com/firebasejs/3.7.2/firebase.js"></script>
<script>
// Initialize Firebase
var config = {
apiKey: "AIzaSyDvp29c4hsnP6QPyUodU2Ga0Lho0I4aThM",
authDomain: "tallerupp.firebaseapp.com",
databaseURL: "https://tallerupp.firebaseio.com",
storageBucket: "tallerupp.appspot.com",
messagingSenderId: "1058142769442"
};
firebase.initializeApp(config);
</script>
<!-- Latest compiled and minified JavaScript -->
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js" integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa" crossorigin="anonymous"></script>
<script src="https://use.fontawesome.com/e433d7b799.js"></script>
<script>
var db = firebase.database();
$('#facebook').on('click', function(){
var provider = new firebase.auth.FacebookAuthProvider();
firebase.auth().signInWithPopup(provider).then(function(result){
var token = result.credential.accessToken;
var user = result.user
});
});
firebase.auth().onAuthStateChanged(function(user){
if(user){
$('#facebook').fadeOut();
$('#contenedor-base').fadeIn();
$('.datos_user').fadeIn();
db.ref('mensajes').on('child_added', function(data){
$('#box').append('<li class="list-group-item"><img class="img_chat" src="'+data.val().img+'">'+data.val().mensaje+' - Enviado por: '+data.val().nombre+'</li>')
});
var nombre = user.displayName;
var img = user.photoURL;
console.log(nombre);
$('#img_user').attr({'src':img});
$('#name_user').text(nombre);
}
else{
$('#facebook').fadeIn();
$('#contenedor-base').fadeOut();
$('.datos_user').fadeOut();
}
});
$('#enviar').on('click', function(){
var mensaje = $('#cuerpo').val();
db.ref('mensajes').push({
mensaje: mensaje,
nombre: $('#name_user').text(),
img: $('#img_user').attr('src'),
});
$('#cuerpo').val('');
});
$('#logout').on('click', function(){
firebase.auth().signOut().then(function(){
$('#img_user').attr({'src':''});
$('#nombre_user').text('');
$('#box').text('');
});
});
</script>
</body>
</html>