This repository has been archived by the owner on Dec 4, 2017. It is now read-only.
forked from mixedpuppy/socialapi-demo
-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathuser.js
80 lines (68 loc) · 1.93 KB
/
user.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
var gUserEmail;
var gContacts = {};
function signedIn(aEmail) {
gUserEmail = aEmail;
$("#useridbox").text("Welcome " + aEmail + "!");
$("#useridbox").show();
$("#nouserid").hide();
$("#signin").hide();
$("#signout").show();
gContacts[aEmail] = $("<li>"); // Avoid displaying the user in the contact list.
setupEventSource();
}
function signedOut() {
gUserEmail = "";
$("#useridbox").text("");
$("#useridbox").hide();
$("#signout").hide();
window.location.reload();
}
function onContactClick(aEvent) {
initiateCall(aEvent.target.innerHTML);
}
function onPersonaLogin(assertion) {
$("#signin").hide();
// XXX this generates a second log in at the server, but we need it for remote connections.
remoteLogin({assertion: assertion});
}
function onPersonaLogout() {
// XXX Assume the sidebar handles the remote part of this.
// We'll need to keep an eye out for changes if we close the sidebar.
remoteLogout();
}
function onPersonaReady() {
if (gUserEmail || remoteLoginPending)
return;
$("#signin").show();
}
function onLoad() {
watchPersonaLogins(onPersonaLogin, onPersonaLogout, onPersonaReady);
}
function setupEventSource() {
var source = new EventSource("events?source=user");
source.onerror = function(e) {
window.location.reload();
};
source.addEventListener("ping", function(e) {}, false);
source.addEventListener("userjoined", function(e) {
if (e.data in gContacts) {
return;
}
var button = $('<button class="userButton">' + e.data + '</button>');
var c = $("<li>");
$("#contacts").append(c.append(button));
button.click(onContactClick);
gContacts[e.data] = c;
}, false);
source.addEventListener("userleft", function(e) {
if (!gContacts[e.data]) {
return;
}
gContacts[e.data].remove();
delete gContacts[e.data];
}, false);
window.addEventListener("beforeunload", function() {
source.onerror = null;
source.close();
}, true);
}