-
Notifications
You must be signed in to change notification settings - Fork 5
/
index.html
576 lines (550 loc) · 26.9 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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
<!doctype html>
<html>
<head>
<title>5napChat</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="//netdna.bootstrapcdn.com/bootstrap/3.0.0/css/bootstrap.min.css">
<style>
body {
overflow:hidden;
}
#login {
background: #888;
}
#login > .modal-dialog > .modal-content > .modal-header {
padding-bottom:0;
}
#loginTabs {
border-bottom:0;
}
#or > h4 {
line-height: 2.2;
padding: 0 1em
}
#friendFilter, #friendFilter + i {
border-radius:0;
}
.tab-pane .list-group-item:first-child {
border-radius:0;
border-top:0;
}
#new {
border-radius: .25em;
border:1px solid #ccc;
border-top-left-radius:0;
border-top-right-radius:0;
border-top:none;
}
li.special {
display:none;
list-style-type:none;
}
#snaps a:link {
text-decoration: underline
}
#gh_fork > img {
z-index:9999
}
</style>
</head>
<body>
<a href="https://github.com/nykac/5napchat-js" id="gh_fork">
<img style="position: absolute; top: 0; right: 0; border: 0;" src="https://s3.amazonaws.com/github/ribbons/forkme_right_orange_ff7600.png" alt="Fork me on GitHub">
</a>
<div class="modal fade" id="login" data-backdrop="static" style="display:block">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<ul class="nav nav-tabs" id="loginTabs">
<li class="active">
<a href="#in" data-toggle="tab"><h4>Login</h4></a>
</li>
<li id="or">
<h4>or</h4>
</li>
<li>
<a href="#up" data-toggle="tab"><h4>Signup</h4></a>
</li>
</ul>
</div>
<div class="modal-body">
<div class="alert alert-danger alert-dismissable" style="display:none">
<button type="button" class="close" data-dismiss="alert" aria-hidden="true">×</button>
<div class="text"></div>
</div>
<div class="tab-content">
<div class="tab-pane active" id="in">
<form class="form-horizontal">
<div class="form-group">
<label for="inputEmail" class="col-lg-2 control-label">Username</label>
<div class="col-lg-10">
<input type="text" class="form-control" id="inUser" placeholder="epic_user"
autofocus>
</div>
</div>
<div class="form-group">
<label for="inputPass" class="col-lg-2 control-label">Password</label>
<div class="col-lg-10">
<input type="password" class="form-control" id="inPass" placeholder="please don't let this be “password”">
</div>
</div>
<div class="form-group">
<div class="col-offset-2 col-lg-10">
<div class="checkbox" checked>
<label>
<input type="checkbox" id="inRemember" checked>Remember me</label>
</div>
</div>
</div>
<div class="form-group">
<div class="col-lg-offset-2 col-lg-10">
<button type="submit" class="btn btn-primary pull-right" id="signIn">Sign in
<i class="glyphicon glyphicon-log-in"></i>
</button>
</div>
</div>
</form>
</div>
<div class="tab-pane" id="up">
<form class="form-horizontal">
<div class="form-group">
<label for="upEmail" class="col-lg-2 control-label">Email</label>
<div class="col-lg-10">
<input type="email" class="form-control" id="upEmail" placeholder="[email protected]"
autofocus>
</div>
</div>
<div class="form-group">
<label for="upPhone" class="col-lg-2 control-label">Phone Number</label>
<div class="col-lg-10">
<input type="tel" class="form-control" id="upPhone" placeholder="+1 (800) 555-2368">
</div>
</div>
<div class="form-group">
<label for="upUser" class="col-lg-2 control-label">Username</label>
<div class="col-lg-10">
<input type="text" class="form-control" id="upUser" placeholder="my_super_cool_username">
</div>
</div>
<div class="form-group">
<label for="upPass" class="col-lg-2 control-label">Password</label>
<div class="col-lg-10">
<input type="password" class="form-control" id="upPass" placeholder="please don't let this be “password”">
</div>
</div>
<div class="form-group">
<div class="col-offset-2 col-lg-10">
<div class="checkbox">
<label>
<input type="checkbox" id="upRemember" checked>Remember me</label>
</div>
</div>
</div>
<div class="form-group">
<div class="col-lg-offset-2 col-lg-10">
<button type="submit" class="btn btn-primary pull-right" id="signUp">Sign up
<i class="glyphicon glyphicon-log-in"></i>
</button>
</div>
</div>
</form>
</div>
</div>
</div>
</div>
</div>
</div>
<div class="modal fade" id="snapImage">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<h4>Snap from
<span class="name">name</span>
</h4>
</div>
<div class="modal-body">
<img id="snapImg" class="img-responsive"></img>
</div>
</div>
</div>
</div>
<div class="modal fade" id="snapVideo">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<h4>Snap from
<span class="name">name</span>
</h4>
</div>
<div class="modal-body">
<video id="snapVid" class="img-responsive" controls/>
</div>
</div>
</div>
</div>
<div class="modal fade" id="settings">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="alert" aria-hidden="true">×</button>
<h3>Settings</h3>
</div>
<div class="modal-body">
<div class="form-horizontal">
<h4>My Account</h4>
<div class="form-group">
<label for="sUsername" class="col-lg-3 control-label">Username</label>
<div class="col-lg-9">
<span class="form-control username"></span>
</div>
</div>
<div class="form-group">
<label for="sPhone" class="col-lg-3 control-label">Mobile #</label>
<div class="col-lg-9">
<input type="tel" class="form-control" id="sPhone" value="+1-800-555-5821">
</div>
</div>
<div class="form-group">
<label for="sEmail" class="col-lg-3 control-label">Email</label>
<div class="col-lg-9">
<input type="email" class="form-control" id="sEmail" value="[email protected]">
</div>
</div>
<hr />
<h4>Who can…</h4>
<div class="form-group">
<label for="sPriv" class="col-lg-3 control-label">Send me snaps</label>
<div class="col-lg-9">
<select class="form-control" id="sPriv">
<option>My Friends</option>
<option>Everyone</option>
</select>
</div>
</div>
<hr />
<div class="row">
<div class="col-lg-3"></div>
<div class="col-lg-9">
<button class="btn btn-block btn-warning" id="clearBtn">Clear feed</button>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
<div class="container">
<div> </div>
<div class="pull-right btn-group">
<button class="btn btn-sm" data-target="#settings" data-toggle="modal">
<i class="glyphicon glyphicon-cog"></i> Settings</button>
<button class="btn btn-danger btn-sm" id="logout">Logout
<i class="glyphicon glyphicon-log-out"></i>
</button>
<span></span>
</div>
<h1>Welcome
<span class="full_name"></span>
</h1>
<ul class="nav nav-tabs">
<li class="active">
<a href="#friends" data-toggle="tab">Friends <i class="glyphicon glyphicon-user"></i></a>
</li>
<li>
<a href="#snaps" data-toggle="tab">Snaps <i class="glyphicon glyphicon-picture"></i></a>
</li>
<li>
<a href="#new" data-toggle="tab">New <i class="glyphicon glyphicon-camera"></i></a>
</li>
</ul>
<div class="tab-content" id="pane">
<div class="tab-pane active" id="friends">
<ul class="list-group">
<li class="input-group">
<input id="friendFilter" class="form-control" />
<i class="glyphicon glyphicon-search input-group-addon"></i>
</li>
<li class="special list-group-item">You don't have a friend named
<span class="user">name</span>, would you like to
<a href="#" id="addBtn">add</a> them?</li>
</ul>
</div>
<div class="tab-pane" id="snaps">
<ul class="list-group"></ul>
</div>
<div class="tab-pane" id="new">
<form action="/upload" method="post" enctype="multipart/form-data">
<input type="file" name="file" />
<input name="recipients" />
<input type="submit"/>
</form>
</div>
</div>
</div>
<script src="//ajax.googleapis.com/ajax/libs/jquery/2.0.2/jquery.min.js"></script>
<script src="//netdna.bootstrapcdn.com/bootstrap/3.0.0-rc1/js/bootstrap.min.js"></script>
<script src="//d1fxtkz8shb9d2.cloudfront.net/sockjs-0.3.min.js"></script>
<script>
$('#login').modal();
if (localStorage && localStorage.username) $('#inUser').val(localStorage.username);
</script>
<script>
window.URL = window.URL || window.webkitURL;
var username;
var sock = new SockJS('https://5napchat.org/rt');
sock.onopen = function() {
if (localStorage.auth_token && localStorage.username) {
var data = JSON.stringify({
action: 'relogon',
user: localStorage.username,
auth_token: localStorage.auth_token
});
sock.send(data);
}
};
var recvSnap = $('<a class="list-group-item">Recieved from <span class="name">Bob</span> <span class="date"></span></a>');
var sendSnap = $('<span class="list-group-item">Sent to <span class="name">Bob</span> <span class="date"></span></span>');
var lgi = $('<li class="list-group-item"><span class="name" contenteditable="plaintext-ONLY"></span><a href="#" class="pull-right glyphicon glyphicon-trash"></button></li>');
if(lgi.find('.name')[0].contentEditable !== 'plaintext-only')
lgi.find('.name').attr('contenteditable', true);
var friends = {};
var snaps = {};
var snapId;
var rememberMe = false;
RegExp.escape = function(string) {
return RegExp(string.replace(/[\-\[\]{}()*+?.,\\\^$|#\s]/g, "\\$&"), "i");
};
var isToday = function(date) {
return new Date().toDateString() == date.toDateString();
};
$('#friendFilter').keyup(function(e) {
var re = RegExp.escape(e.target.value);
var visible = 0;
$('#friends ul > li.list-group-item:not(.special)').each(function() {
if ($(this).text().match(re)) {
$(this).show(0);
++visible;
}
else $(this).hide(400);
});
if (!visible) {
$('#friends ul > li.list-group-item.special').show(400).find('.user').text(e.target.value);
}
else $('#friends ul > li.list-group-item.special').hide();
});
$('#addBtn').click(function(e) {
e.preventDefault();
var friend = $('#friendFilter').val();
var data = {
action: 'addFriend',
friend: friend
};
var li = lgi.clone().attr('id', friend);
li.find('.name').text(friend);
$('#friends ul').append(li);
sock.send(JSON.stringify(data));
$('#friendFilter').val('').keyup();
});
$('#signIn').click(function(e) {
e.preventDefault();
var user = $('#inUser').val();
var pass = $('#inPass').val();
var data = JSON.stringify({
action: 'login',
user: user,
pass: pass
});
rememberMe = $('#inRemember').is(':checked');
username = user;
sock.send(data);
});
$('#signUp').click(function(e) {
e.preventDefault();
var user = $('#upUser').val();
var pass = $('#upPass').val();
var phone = $('#upPhone').val();
var email = $('#upEmail').val();
console.log({
user: user,
pass: pass,
phone: phone,
email: email
});
rememberMe = $('#upRemember').is(':checked');
username = user;
});
$('#logout').click(function(e) {
sock.send('{"action":"logout"}');
});
var sortFriends = function() {
$('#friends > ul').append($('#friends ul > li.list-group-item:not(.special)').get().sort(function(a, b) {
return $(a).text().toUpperCase().localeCompare($(b).text().toUpperCase());
}));
};
var showVideo = function(obj) {
var url = obj.url;
var from = obj.from;
var modal = $('#snapVideo');
modal.find('video').attr('src', url);
modal.find('.name').text(from);
modal.modal('show');
};
var showImage = function(obj) {
var url = obj.url;
var from = obj.from;
var modal = $('#snapImage');
modal.find('img').attr('src', url);
modal.find('.name').text(from);
modal.modal('show');
};
var showSnap = function() {
var type = snaps[snapId].mime.substr(0, 5);
if (type == "image") return showImage(snaps[snapId]);
else return showVideo(snaps[snapId]);
};
var displaySnap = function(id) {
snapId = id;
if (snaps[id] && snaps[id].mime && snaps[id].url) {
showSnap();
}
};
sock.onmessage = function(e) {
var message = JSON.parse(e.data);
switch (message.action) {
case "login":
$('#login').modal('hide');
break;
case "logout":
localStorage.removeItem('auth_token');
$('#login').modal('show');
break;
case "sync":
var sync = message.data;
if (sync.message) {
sock.onmessage({
data: JSON.stringify({
action: 'error',
err: sync.message
})
});
return;
}
username = sync.username || username;
var frag = document.createDocumentFragment();
sync.friends.forEach(function(friend) {
var name = friend.display || friend.name;
var uid = friend.name;
friends[uid] = name;
var li = lgi.clone().attr('id', uid).hide(0);
li.find('.name').text(name).keydown(function(e) {
if (e.keyCode != 13) return true;
e.preventDefault();
var newName = $(this).text();
var friend = $(this).parent().attr('id');
var data = JSON.stringify({
action: 'rename',
friend: friend,
name: newName
});
sock.send(data);
$(this).blur();
});
li.find('a').click(function(e) {
e.preventDefault();
var friend = $(this).parent().attr('id');
var data = JSON.stringify({
action: 'unfriend',
friend: friend,
});
sock.send(data);
$(this).parent().remove();
});
frag.appendChild(li[0]);
});
$('#friends ul>li.list-group-item:not(.special)').remove();
$('#friends ul>li.special').before(frag);
$('#friends input').keyup();
sortFriends();
var frag = document.createDocumentFragment();
sync.snaps.forEach(function(snap) {
var element;
if (snap.sn) element = recvSnap.clone();
else element = sendSnap.clone();
var name = snap.sn || snap.rp;
var recvd = new Date(snap.ts);
element.attr('id', snap.id);
element.find('.name').text(friends[name] || name);
element.find('.date').text(isToday(recvd) ? 'at ' + recvd.toTimeString() : 'on ' + recvd.toDateString());
if (recvd && (snap.t > 0) || (snap.m == 1)) {
element.attr('href', '#').click(function(e) {
e.preventDefault();
if (!snaps[snap.id]) {
var xhr = new XMLHttpRequest();
xhr.open('GET', '/blob/' + username + '/' + snap.id);
xhr.responseType = 'blob';
xhr.onload = function(e) {
var blob = this.response;
window.blob = blob;
snaps[snap.id] = snaps[snap.id] || {};
snaps[snap.id].from = friends[name] || name;
snaps[snap.id].url = window.URL.createObjectURL(blob);
var reader = new FileReader();
reader.onload = function() {
switch (reader.result) {
case "\xff\xd8\xff":
snaps[snap.id].mime = "image/jpeg";
break;
case "\x00\x00\x00":
snaps[snap.id].mime = "video/mp4";
break;
case "GIF":
snaps[snap.id].mime = "image/gif";
break;
case "\x89PN":
snaps[snap.id].mime = "image/png";
break;
case "II*":
case "MM\x00":
snaps[snap.id].mime = "image/tiff";
break;
case "Ogg":
snaps[snap.id].mime = "application/ogg";
break;
default:
snaps[snap.id].mime = "unknown";
}
displaySnap(snap.id);
};
reader.readAsBinaryString(blob.slice(0, 3));
};
xhr.send();
}
displaySnap(snap.id);
});
}
frag.appendChild(element[0]);
});
$('#snaps .list-group').empty().prepend(frag);
if (sync.auth_token) {
document.cookie = 'auth_token=' + sync.auth_token;
if (rememberMe) localStorage.auth_token = sync.auth_token;
else localStorage.removeItem('auth_token')
}
localStorage.username = username;
$('.full_name').text(friends[username])
$('.username').text(username);
break;
case "error":
$('.alert').show().find('.text').html(message.err).show();
sock.onmessage({
data: '{"action":"logout"}'
});
break;
default:
console.log(message);
break;
}
};
</script>
</body>
</html>