-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
656 lines (575 loc) · 28.7 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
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>
</title>
<link rel="stylesheet" href="http://code.jquery.com/mobile/1.1.0/jquery.mobile-1.1.0.min.css" />
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
<script src="http://code.jquery.com/mobile/1.1.0/jquery.mobile-1.1.0.min.js"></script>
<script src="http://www.parsecdn.com/js/parse-1.0.1.min.js"></script>
<script src="http://underscorejs.org/underscore-min.js"></script>
<style>
.message {
padding: 5px 5px 5px 5px;
}
.username {
font-weight: strong;
color: #00d;
}
.msg-date {
color: #bbb;
display: block;
text-align: right;
}
.msgContainerDiv {
/* overflow-y: scroll;
height: 250px; */
}
</style>
</head>
<body>
<!-- div id="fb-root"></div -->
<script>
$(document).bind("mobileinit", function(){
//$.mobile.ajaxEnabled = false;
$.mobile.defaultPageTransition = "slide";
});
(function() {
/*
var e = document.createElement('script'); e.async = true;
e.src = document.location.protocol + '//connect.facebook.net/en_US/all.js';
document.getElementById('fb-root').appendChild(e);
*/
Parse.initialize("Zp7oxcFEKpi95Meynk7tzDECfzCeJYUzRiUK52tG", "uEhgrhWdcoc003OO5P33GSYdMUYT4IAQwqhoGOXi");
}());
$(document).ready(function(){
if (Parse.User.current()) {
loadUserData(function() {
readyToPlay();
});
} else {
//document.location = "#pageLogin";
$.mobile.changePage($("#pageLogin"));
}
})
</script>
<script>
/*
window.fbAsyncInit = function() {
FB.init({ appId: '216190471835507',
status: true,
cookie: true,
xfbml: true,
oauth: true});
FB.Event.subscribe('auth.statusChange', handleStatusChange);
}; */
var READY_TO_PLAY_CHECKIN_DURATION = 60 * 60 * 1000; // 1 hour check-in
function loginUser() {
//document.location = "#page0"; // loading screen
//FB.login(function(response) {}, {scope:'email'});
$.mobile.showPageLoadingMsg();
var email = $('#login_email').val();
var password = $('#login_password').val();
Parse.User.logIn(email, password, {
success: function(user) {
loadUserData(function() {
readyToPlay();
});
},
error: function(user, error) {
$.mobile.hidePageLoadingMsg();
alert("Error: " + error.code + " " + error.message);
}
});
}
function registerUser() {
$.mobile.showPageLoadingMsg();
var email = $('#login_email').val();
var password = $('#login_password').val();
// register with Parse
var user = new Parse.User();
user.set("username", email);
user.set("password", password);
user.set("email", email);
user.set("last_checkin", new Date());
user.set("friends", []); // array of usernames
user.signUp(null, {
success: function(user) {
$.mobile.hidePageLoadingMsg();
//document.location = "#pageSetProfile";
$.mobile.changePage($("#pageSetProfile"));
},
error: function(user, error) {
$.mobile.hidePageLoadingMsg();
alert("Error: " + error.code + " " + error.message);
//document.location = "#pageLogin";
$.mobile.changePage($("#pageSetProfile"));
}
});
}
function logout() {
$.mobile.showPageLoadingMsg();
var me = Parse.User.current();
me.set("last_checkin", null);
me.save(null, {
success: function(user) {
$.mobile.hidePageLoadingMsg();
//document.location = "#pageLogin"
$.mobile.changePage($("#pageLogin"));
Parse.User.logOut();
}
});
}
function updateProfile() {
$.mobile.showPageLoadingMsg();
var pname = $('#public_name').val();
var me = Parse.User.current();
me.set("public_name", pname);
me.save(null,{
success: function(user) {
// TODO: if this was a new user registration, we should find any friends they might have and ask about it
readyToPlay();
},
error: function(user, error) {
$.mobile.hidePageLoadingMsg();
alert("Error: " + error.code + " " + error.message);
}
});
}
function readyToPlay() {
// Note: no show loading msg here, since it's already shown
var me = Parse.User.current();
me.set("last_checkin", new Date());
me.save(null, {
success: function(user) {
$.mobile.hidePageLoadingMsg();
$.mobile.changePage($("#pagePlayList"));
},
error: function(user, error) {
$.mobile.hidePageLoadingMsg();
alert("Error: " + error.code + " " + error.message);
}
});
}
function addFriend() {
/* FB.api('/me/friends', function(response) {
console.log(response)
}); */
var new_email = $('#friend_email').val();
if ( new_email && /(.+)@(.+){2,}\.(.+){2,}/.test(new_email) ) {
var me = Parse.User.current();
var friends = me.get("friends");
friends = friends ? friends : [];
// skip if we already have it
if ( !_(friends).include(new_email) ) {
friends.push( new_email );
me.set("friends", friends);
$.mobile.showPageLoadingMsg();
me.save(null, {
success: function(user) {
$('#friend_email').val("");
loadUserData(function() { $.mobile.hidePageLoadingMsg() });
},
error: function(user, error) {
$.mobile.hidePageLoadingMsg();
alert("Error: " + error.code + " " + error.message);
}
});
} else {
alert("Error: " + new_email + " is already a friend!");
}
}
}
function removeFriend(email) {
if ( !confirm("Are you sure you want remove " + email + " as a friend?") ) { return; }
$.mobile.showPageLoadingMsg();
var me = Parse.User.current();
var friends = me.get("friends");
friends = friends ? friends : [];
me.set("friends", _(friends).without(email) );
me.save(null, {
success: function(user) {
loadUserData(function() { $.mobile.hidePageLoadingMsg() });
},
error: function(user, error) {
$.mobile.hidePageLoadingMsg();
alert("Error: " + error.code + " " + error.message);
}
});
}
function refreshUserData() {
$.mobile.showPageLoadingMsg();
loadUserData(function() {
$.mobile.hidePageLoadingMsg();
});
}
function loadFriendChat( friend_email ) {
// retrieve user object
var friend = _currentFriendChat = _(_playTimeData.friends).find(function(el) {
return el.email === friend_email;
}).user;
// Set public name in header of chat
$('#chat_header').html( friend.get("public_name") );
// Set new location
// document.location = "#pageChat"
$.mobile.changePage($("#pageChat"));
// Start loading chat messages / refreshing them
refreshFriendChat();
}
function getCurrentChatKey() {
// total hack for compound key
return [_currentFriendChat.id, Parse.User.current().id].sort().join();
}
function refreshFriendChat() {
$.mobile.showPageLoadingMsg();
// Reload associated chat messages for a friend
var ChatMessage = Parse.Object.extend("ChatMessage");
var query = new Parse.Query(ChatMessage);
var chatters_id = getCurrentChatKey();
query.equalTo( "chatters", chatters_id );
query.ascending( "createdAt" );
query.include("author");
query.find({
success: function(results) {
clearChatMessages();
_(results).each(function(chat_msg) {
appendMessage( chat_msg );
});
$.mobile.hidePageLoadingMsg();
},
error: function(error) {
alert("Error: " + error.code + " " + error.message);
$.mobile.hidePageLoadingMsg();
}
})
}
function sendChatMessage() {
var msg = $('#messageText').val();
var ChatMessage = Parse.Object.extend("ChatMessage");
var myMessage = new ChatMessage();
myMessage.set("content", msg);
myMessage.set("author", Parse.User.current());
myMessage.set("chatters", getCurrentChatKey());
$.mobile.showPageLoadingMsg();
myMessage.save(null, {
success: function(msg) {
appendMessage( msg );
$('#messageText').val("");
$.mobile.hidePageLoadingMsg();
},
error: function(msg, error) {
alert("Error: " + error.code + " " + error.message);
$.mobile.hidePageLoadingMsg();
}
});
}
function clearChatMessages() {
$("#incomingMessages").empty();
}
function formatDate(date) {
var month = date.getMonth()+1;
var day = date.getDate();
var hour = date.getHours();
var minute = date.getMinutes();
var ampm = hour >= 12 ? "PM" : "AM";
hour = hour > 12 ? hour % 12 : hour;
function pad(n) { return +n >= 10 ? n : ("0" + n); }
return month + "/" + day + " " + hour + ":" + pad(minute) + " " + ampm;
}
function appendMessage( msg ) {
var name = msg.get("author").get("public_name"),
message = msg.get("content"),
date = new Date( msg.createdAt );
var date_msg = formatDate( date );
$("#incomingMessages").prepend("<div class='message_container'>"
+ "<div class='message'>"
+ "<span class='username'>" + name + ":</span> "
+ message
+ "</div>"
+ "<div class='msg-date'>" + date_msg + "</div>"
+ "</div>");
$("#incomingMessages").scrollTop($("#incomingMessages")[0].scrollHeight);
}
</script>
<script>
var _playTimeData = undefined;
var _currentFriendChat = undefined;
/*
function handleStatusChange(response) {
document.body.className = response.authResponse ? 'connected' : 'not_connected';
if (response.authResponse) {
console.log(response);
loadUserData(response);
} else {
document.location = "#pageLogin";
}
} */
function loadUserData(cb) {
// Load our Play Time friends, etc. from the data store
loadData(cb);
// Load our basic info from FB
/* FB.api('/me', function(response) {
_fbMeResponse = response;
// update personalized messages
$('#welcome_msg').html("Welcome back, " + response.name + ".");
maybeStartApp();
}); */
}
function loadData(cb) {
var new_friends = [];
var old_friends = _playTimeData ? _playTimeData.friends : [];
// Find all users in the list of emails that are our friends
var query = new Parse.Query(Parse.User);
query.containedIn( "email", Parse.User.current().get("friends") );
query.find({
success: function(results) {
// construct a friends list
var found_emails = []
_(results).each(function(el) {
var stat = "offline";
var last_checkin = el.get("last_checkin");
var checkin_msg = "";
if ( last_checkin ) {
var diff = new Date() - last_checkin;
if ( diff <= READY_TO_PLAY_CHECKIN_DURATION ) {
stat = "ready_to_play";
checkin_msg = Math.ceil(diff / 1000 / 60);
checkin_msg = checkin_msg > 1 ? checkin_msg + " minutes ago" : checkin_msg + " minute ago";
} else {
checkin_msg = formatDate( last_checkin );
}
}
new_friends.push({ "name" : el.get("public_name"), "status" : stat, "email" : el.get("email"), "user" : el, "last_checkin" : checkin_msg });
found_emails.push(el.get("email"));
});
// HACK: for any email addressess for which we couldn't find a user in Parse, include them as off-line
var emails_not_found = _.difference(Parse.User.current().get("friends"), found_emails);
_(emails_not_found).each(function(email) {
new_friends.push({ "name" : email, "status" : "offline", "email" : email, "user" : null, "last_checkin" : "" });
});
updateFriendsList(new_friends, old_friends);
// update local data
_playTimeData = {"friends" : new_friends};
// if we got a callback, pass it along
if ( cb ) {
cb();
}
},
error: function(error) {
$.mobile.hidePageLoadingMsg();
alert("Error: " + error.code + " " + error.message);
}
});
}
function updateFriendsList(new_friends, old_friends) {
// sort helper function
function sortByName(a, b){
var aName = a.name.toLowerCase();
var bName = b.name.toLowerCase();
return ((aName < bName) ? -1 : ((aName > bName) ? 1 : 0));
}
// Figure out what has been updated...
old_friends.sort(sortByName);
new_friends.sort(sortByName);
var old_i = 0, new_i = 0;
var added_friends = [];
var removed_friends = [];
while ( old_i < old_friends.length && new_i < new_friends.length ) {
var old_friend = old_friends[old_i];
var new_friend = new_friends[new_i];
// if new friends are missing an item from old friends, something must have deleted them from our list
if ( old_friend.name < new_friend.name ) {
removed_friends.push(old_friend);
old_i++;
// if old friends are missing an item found in new friends, we have a new friend to add
} else if ( old_friend.name > new_friend.name ) {
added_friends.push(new_friend);
new_i++;
// if it's the same friend, check if it's been updated
} else {
if ( old_friend.status != new_friend.status ) {
// cheap trick: just make sure we remove it, then re-add it with the new status
removed_friends.push(old_friend);
added_friends.push(new_friend);
}
old_i++;
new_i++;
}
}
// add rest of old friends to the removed list
while ( old_i < old_friends.length ) {
removed_friends.push( old_friends[old_i] );
old_i++
}
// add rest of new friends to the added list
while ( new_i < new_friends.length ) {
added_friends.push( new_friends[new_i] );
new_i++
}
// Use our 3 arrays of changes to update the DOM
var friend_list_jq = $('#friend_list_ul');
var ready_to_play_header_jq = $('#ready_to_play_header');
var offline_header_jq = $('#offline_header');
var friend_template = _.template('<li data-theme="c" data-friend-name="<%= name %>"><a style="padding-left: 5px; padding-right: 15px" href="" onclick="loadFriendChat(\'<%= email %>\');" data-transition="slide"><%= name %><span style="color: #aaa; font-weight: normal"> <%= last_checkin %></span></a></li>')
var friend_delete_template = _.template('<li data-theme="c" data-icon="delete" data-friend-name="<%= name %>"><a href="#pageManageFriends" data-transition="none" onclick="removeFriend(\'<%= email %>\')"><%= name %></a></li>');
_(removed_friends).each(function(el) {
console.log("removing " + el.name);
friend_list_jq.find("[data-friend-name='" + el.name + "']").remove();
$('#friend_delete_list').find("[data-friend-name='" + el.name + "']").remove();
});
_(added_friends).each(function(el) {
console.log("inserting " + el.name + " with html " + friend_template(el));
if ( el.status === "offline" ) {
$( friend_template(el) ).insertAfter( offline_header_jq );
} else {
$( friend_template(el) ).insertAfter( ready_to_play_header_jq );
}
// add to deletion list
$('#friend_delete_list').append( friend_delete_template(el) )
});
// if this isn't the first time we've been called, refresh
if ( _playTimeData ) {
try {
$('#friend_list_ul').listview('refresh');
} catch (e) {}
try {
$('#friend_delete_list').listview('refresh');
} catch (e) {}
}
}
</script>
<div data-role="page" id="page0">
<div data-theme="a" data-role="header">
<div data-theme="a" data-role="header">
<h3>
PlayTime Demo
</h3>
</div>
</div>
<div data-role="content">
<div>
<p style="text-align: center; ">
<strong>
Loading...
</strong>
</p>
</div>
</div>
</div>
<div data-role="page" id="pageSetProfile">
<div data-theme="a" data-role="header">
<div data-theme="a" data-role="header">
<h3>
Update Profile
</h3>
</div>
</div>
<div data-role="content">
<form id="profile_form" action="#pageSetProfile" method="get" onsubmit="return false;">
<div data-role="fieldcontain">
<label for="public_name">Public Name:</label>
<input type="text" name="public_name" id="public_name" value="" />
<a data-role="button" data-transition="slide" data-theme="a" href="#pageSetProfile" onClick="updateProfile()">Update Profile</a>
</div>
</form>
</div>
</div>
<div data-role="page" id="pageLogin">
<div data-theme="a" data-role="header">
<div data-theme="a" data-role="header">
<h3>
Play Time Demo
</h3>
</div>
</div>
<div data-role="content">
<form id="login_form" action="#pageLogin" method="get" onsubmit="return false;">
<div data-role="fieldcontain">
<label for="login_email">Email:</label>
<input type="email" name="login_email" id="login_email" value="" />
<label for="login_password">Password:</label>
<input type="password" name="login_password" id="login_password" value="" />
<div class="ui-grid-a">
<div class="ui-block-a">
<a data-role="button" data-transition="slide" data-theme="a" href="#pageLogin" onClick="registerUser();">Register</a>
</div>
<div class="ui-block-b">
<a data-role="button" data-transition="slide" data-theme="b" href="#pageLogin" onClick="loginUser();">Login</a>
</div>
</div>
</div>
</form>
</div>
</div>
<div data-role="page" id="pagePlayList">
<div data-theme="a" data-role="header" data-position="inline">
<a href="#pagePlayList" data-icon="delete" onclick="logout()">Logout</a>
<h3>Ready to Play!</h3>
<a href="#pagePlayList" data-icon="refresh" data-theme="b" onclick="refreshUserData()">Refresh</a>
</div>
<div data-role="content">
<h2 style="margin: 0; text-align: center">Welcome back!</h1>
<div style="margin: 0">
<p>
<small>
We've set your status so your friends know that you are <span style="color: #007700; font-weight: bold;">ready to play</span> for the <span style="font-weight: bold">next hour</span>.
</small>
</p>
<p style="display: none;">
<small>If you don't want to play right now, just log out and we'll update your status.</small>
</p>
</div>
<ul id="friend_list_ul" data-role="listview" data-divider-theme="b" data-inset="true">
<li id="ready_to_play_header" data-role="list-divider" role="heading">
Ready to Play!
</li>
<li id="offline_header" data-role="list-divider" role="heading">
Offline
</li>
</ul>
<p>
<small>
Send messages to friends above, or check back later to see if anyone has left you a new message.
</small>
</p>
<a data-role="button" data-transition="slide" data-theme="a" href="#pageManageFriends">Add or Remove Friends</a>
</div>
</div>
<div data-role="page" id="pageManageFriends" data-add-back-btn="true">
<div data-theme="a" data-role="header">
<h3>
Manage Friends
</h3>
</div>
<div data-role="content">
<form id="friend_form" action="#pageManageFriends" method="get" onsubmit="return false;">
<div data-role="fieldcontain">
<input type="email" name="friend_email" id="friend_email" value="" placeholder="[email protected]"/>
<a data-role="button" data-transition="slide" data-theme="b" href="#pageManageFriends" onClick="addFriend()">Add Friend with Email Address</a>
</div>
</form>
<ul id="friend_delete_list" data-role="listview" data-divider-theme="b" data-inset="true">
<!-- Friends to delete -->
</ul>
</div>
</div>
<div data-role="page" id="pageChat">
<div data-theme="a" data-role="header" data-position="inline" data-theme="a">
<a href="#pagePlayList" data-icon="back" data-rel="back" data-transition="slide" data-theme="a">Back</a>
<h3 id="chat_header">
Chat
</h3>
<a href="#pageChat" data-icon="refresh" data-theme="b" onclick="refreshFriendChat()">Refresh</a>
</div>
<div data-role="content">
<div id="incomingMessages" name="incomingMessages" class="msgContainerDiv"></div>
</div>
<div data-role="footer" data-position="fixed">
<textarea name="messageText" id="messageText" data-theme="b" placeholder="Enter message here."></textarea>
<button data-theme="a" id="chatSendButton" name="chatSendButton" onclick="sendChatMessage()">Send</button>
</div>
</div>
</body>
</html>