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 pathsidebar.js
496 lines (440 loc) · 14.7 KB
/
sidebar.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
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
var gContacts = {};
var gChats = {};
var gUsername = "";
var gFake = false;
function onPersonaLogin(assertion) {
$("#guest").hide();
$("#signin").hide();
remoteLogin({assertion: assertion});
}
function onPersonaLogout(assertion) {
if (gUsername)
remoteLogout();
}
function onPersonaReady() {
if (gUsername || remoteLoginPending)
return;
$("#guest").show();
$("#signin").show();
}
function onLoad() {
var worker = navigator.mozSocial.getWorker();
if (!worker) {
document.body.style.border = "3px solid red";
}
watchPersonaLogins(onPersonaLogin, onPersonaLogout, onPersonaReady);
}
function onContactClick(aEvent) {
callPerson(aEvent.target.getAttribute("user"),
aEvent.target.getAttribute("call") == "audio");
}
function callPerson(aPerson, aAudioCall) {
// Check first if the person is already calling us...
if (aPerson in gChats) {
// If a call is ringing, accept it.
var doc = gChats[aPerson].win.document;
if (doc.getElementById("callAnswer").style.display == "block")
doc.getElementById("accept").onclick();
return;
}
openChat(aPerson, function(aWin) {
var win = gChats[aPerson].win;
var doc = win.document;
doc.getElementById("calling").style.display = "block";
gChats[aPerson].pc = webrtcMedia.startCall(aPerson, win, aAudioCall,
onConnection, setupFileSharing);
gChats[aPerson].audioOnly = aAudioCall;
});
}
function startGuest() {
$("#signin").hide();
$("#guest").html('<form onsubmit="javascript:guestLogin(event);">' +
' <input type="text" id="user"/>' +
' <input type="submit" value="Login"/>' +
'</form>'
);
$("#user").focus();
}
function guestLogin(event) {
var user = $("#user").attr("value");
remoteLogin({assertion: user, fake: true});
gFake = true;
event.preventDefault();
}
function signedIn(aEmail) {
$("#guest").hide();
$("#signin").hide();
var end = location.href.indexOf("/sidebar.htm");
var baselocation = location.href.substr(0, end);
var userdata = {
portrait: baselocation + "/user.png",
userName: aEmail,
dispayName: aEmail,
profileURL: baselocation + "/user.html"
};
document.cookie="userdata="+JSON.stringify(userdata);
window.addEventListener("unload", function() {
document.cookie = 'userdata=; expires=Fri, 27 Jul 2001 02:47:11 UTC; path=/';
});
gUsername = aEmail;
gContacts[aEmail] = $("<li>"); // Avoid displaying the user in the contact list.
}
function onSignout() {
if (!gFake)
signout();
else {
gFake = false;
onPersonaLogout();
}
}
function signedOut() {
delete gContacts[gUsername];
gUsername = "";
reload();
}
function onConnection(aWin, aPc, aPerson, aOriginator) {
}
function insertChatMessage(win, from, message) {
var box = win.document.getElementById("chat");
box.innerHTML += "<span class=\"" + from + "\">" + from + "</span>: " + message + "<br/>";
box.scrollTop = box.scrollTopMax;
}
var filename = "default.txt";
function handleFile(win, blob) {
var url = win.URL.createObjectURL(blob);
// If the file extension is known, just open the file in a new tab.
const ext = ["jpg", "jpeg", "gif", "png", "pdf", "txt"];
if (ext.indexOf(filename.split(".").pop().toLowerCase()) != -1)
win.open(url);
else {
// Otherwise offer to save the file.
// We create a fake link to use its .download property that lets
// us set the filename that will be shown to the user in the
// download dialog.
var a = win.document.createElement("a");
a.href = url;
a.download = filename;
var event = win.document.createEvent("MouseEvents");
event.initMouseEvent("click", true, false, win, 0, 0, 0, 0, 0,
false, false, false, false, 0, null);
a.dispatchEvent(event); // false if event was cancelled
}
}
function gotChat(win, evt) {
if (evt.data instanceof Blob) {
// for file transfer.
handleFile(win, evt.data);
} else {
// either an incoming file or chat.
try {
var details = JSON.parse(evt.data);
if (details.type == "file") {
filename = details.filename;
} else if (details.type == "url") {
win.open(details.url);
} else {
throw new Error("JSON, but not a file");
}
} catch(e) {
insertChatMessage(win, "Them", evt.data);
}
}
}
function setupFileSharing(aWin, aIncomingChannel, aOutgoingChannel, aTarget) {
/* Setup data channel */
aOutgoingChannel.binaryType = "blob";
aIncomingChannel.onmessage = function(evt) {
gotChat(aWin, evt);
};
gChats[aTarget].incomingChannel = aIncomingChannel;
gChats[aTarget].outgoingChannel = aOutgoingChannel;
/* Setup drag and drop for file transfer */
var box = aWin.document.getElementById("content");
box.addEventListener("dragover", ignoreDrag, false);
box.addEventListener("dragleave", ignoreDrag, false);
box.addEventListener("drop", handleDrop, false);
// Now set up the chat interface
aWin.document.getElementById("chatForm").onsubmit = function() {
var localChat = aWin.document.getElementById("localChat");
var message = localChat.value;
aOutgoingChannel.send(message);
localChat.value = "";
// XXX: Sometimes insertChatMessage throws an exception, don't know why yet.
try {
insertChatMessage(aWin, "Me", message);
} catch(e) {}
return false;
};
aWin.document.getElementById("localChat").removeAttribute("disabled");
function ignoreDrag(e) {
e.stopPropagation();
e.preventDefault();
e.dataTransfer.dropEffect = "copy";
if (e.type == "dragover") {
aWin.document.getElementById("fileDrop").style.display = "block";
} else {
aWin.document.getElementById("fileDrop").style.display = "none";
}
}
function handleDrop(e) {
ignoreDrag(e);
var files = e.target.files || e.dataTransfer.files;
if (files.length) {
for (var i = 0, f; f = files[i]; i++) {
aOutgoingChannel.send(JSON.stringify({type: "file", filename: f.name}));
aOutgoingChannel.send(f);
}
} else {
var url = e.dataTransfer.getData("URL");
if (!url.trim()) {
url = e.dataTransfer.mozGetDataAt("text/x-moz-text-internal", 0);
}
aOutgoingChannel.send(JSON.stringify({type: "url", url: url}));
}
}
}
function finishDetachTab(aWindow) {
var doc = aWindow.document;
var localVideo = doc.getElementById("localVideo");
var video = doc.getElementById("remoteVideo");
video.setAttribute("style", "position: fixed; top: 0; left: 0; z-index: 1; background: black;");
var resizeVideo = function() {
var height = aWindow.innerHeight;
var width = aWindow.innerWidth;
video.setAttribute("width", width);
video.setAttribute("height", height);
localVideo.setAttribute("width", "108");
localVideo.setAttribute("height", "81");
localVideo.setAttribute("style", "position: fixed; z-index: 2;");
localVideo.style.top = (height - 81) + "px";
localVideo.style.left = (width - 108) + "px";
};
resizeVideo();
aWindow.addEventListener("resize", resizeVideo);
var button = doc.getElementById("fullTab");
button.onclick = function() {
doc.getElementById("video").mozRequestFullScreen();
};
button.textContent = "Full screen";
}
function setupExpandHandler(win) {
win.document.getElementById("fullTab").onclick = function() {
var doc = win.document;
var event = doc.createEvent("UIEvents");
event.initUIEvent("detachToTab", true, true, win, 0);
win.dispatchEvent(event);
if (!event.defaultPrevented) {
// The add-on isn't installed, fallback to opening a new tab and
// keeping the floating window around.
var tab = win.open(win.location);
tab.addEventListener("DOMContentLoaded", function() {
tab.document.title = win.document.title;
var video = doc.getElementById("remoteVideo");
var newVideo = tab.document.getElementById("remoteVideo");
newVideo.mozSrcObject = video.mozSrcObject;
newVideo.play();
var localVideo = doc.getElementById("localVideo");
var newLocalVideo = tab.document.getElementById("localVideo");
newLocalVideo.mozSrcObject = localVideo.mozSrcObject;
newLocalVideo.play();
finishDetachTab(tab);
});
}
else
finishDetachTab(doc.defaultView);
};
}
function hideVideo(aDoc) {
aDoc.getElementById("fullTab").style.display = "none";
aDoc.getElementById("video").style.display = "none";
aDoc.getElementById("chat").setAttribute("style", "top: 0; height: 246px;");
}
function setupEventSource() {
var source = new EventSource("events?source=sidebar");
source.onerror = function(e) {
reload();
};
source.addEventListener("ping", function(e) {}, false);
source.addEventListener("userjoined", function(e) {
if (e.data in gContacts) {
return;
}
var contact = $('<li class="contact">');
// Add the picture
contact.append($('<div class="userName">' + e.data + '</div>'));
var userButtons = $('<div class="userButtons">');
var userButton = $('<button class="userButtonVideo" user="' + e.data + '" call="video"/>');
userButton.click(onContactClick);
userButtons.append(userButton);
userButton = $('<button class="userButtonAudio" user="' + e.data + '" call="audio"/>');
userButton.click(onContactClick);
userButtons.append(userButton);
contact.append(userButtons);
$("#contacts").append(contact);
gContacts[e.data] = contact;
}, false);
source.addEventListener("userleft", function(e) {
if (!gContacts[e.data]) {
return;
}
gContacts[e.data].remove();
delete gContacts[e.data];
}, false);
source.addEventListener("offer", function(e) {
var data = JSON.parse(e.data);
var from = data.from;
// Silently drop calls from people already calling us.
// The server won't cancel the ongoing call if there's a pending call.
if (from in gChats) {
stopCall(from);
return;
}
openChat(from, function(aWin) {
var win = gChats[from].win;
var doc = win.document;
var offer = JSON.parse(data.request);
offer.sdp = offer.sdp.split("m=").filter(function(s) {
return !s.startsWith("video") || s.indexOf("a=recvonly") == -1;
}).join("m=");
gChats[from].audioOnly = offer.sdp.indexOf("m=video") == -1;
doc.getElementById("callAnswer").style.display = "block";
doc.getElementById("reject").onclick = function() {
win.close();
};
doc.getElementById("accept").onclick = function() {
doc.getElementById("callAnswer").style.display = "none";
var chat = gChats[from];
chat.pc = webrtcMedia.handleOffer(data, win, chat.audioOnly,
onConnection, setupFileSharing);
if (chat.audioOnly)
hideVideo(chat.win.document);
};
});
}, false);
source.addEventListener("answer", function(e) {
var data = JSON.parse(e.data);
var chat = gChats[data.from];
var doc = chat.win.document;
var answer = JSON.parse(data.request);
var pc = chat.pc;
if (!chat.audioOnly && answer.sdp.indexOf("m=video") == -1) {
chat.audioOnly = true;
var audio = doc.getElementById("localAudio");
var video = doc.getElementById("localVideo");
// Using the audio+video stream as audio only has the
// unfortunate effect of keeping the webcam active...
audio.mozSrcObject = video.mozSrcObject;
video.mozSrcObject = null;
}
doc.getElementById("calling").style.display = "none";
if (chat.audioOnly)
hideVideo(doc);
pc.setRemoteDescription(answer, function() {
// Nothing to do for the audio/video. The interesting things for
// them will happen in onaddstream.
}, function(err) {alert("failed to setRemoteDescription with answer, " + err);});
}, false);
source.addEventListener("call", function(e) {
var data = JSON.parse(e.data);
callPerson(data.who);
}, false);
source.addEventListener("stopcall", function(e) {
var data = JSON.parse(e.data);
var chat = gChats[data.from];
if (!chat) {
// The chat to close doesn't exist any more...
return;
}
webrtcMedia.endCall(chat.pc, chat.incomingChannel, chat.outgoingChannel,
chat.win, chat.audioOnly);
delete gChats[data.from];
chat.win.close();
});
window.addEventListener("beforeunload", function() {
source.onerror = null;
source.close();
}, true);
}
function userIsConnected(userdata) {
$("#userid").text(userdata.userName);
$("#usericon").attr("src", userdata.portrait);
$("#signedin").show();
$("#content").show();
setupEventSource();
}
function userIsDisconnected() {
$("#content").hide();
$("#userid").text("");
$("#usericon").attr("src", "");
$("#signedin").hide();
}
var messageHandlers = {
"worker.connected": function(data) {
// our port has connected with the worker, do some initialization
// worker.connected is our own custom message
var worker = navigator.mozSocial.getWorker();
worker.port.postMessage({topic: "broadcast.listen", data: true});
},
"social.user-profile": function(data) {
if (data.userName) {
userIsConnected(data);
} else {
userIsDisconnected();
}
}
};
navigator.mozSocial.getWorker().port.onmessage = function onmessage(e) {
var topic = e.data.topic;
var data = e.data.data;
if (messageHandlers[topic]) {
messageHandlers[topic](data);
}
};
function reload() {
navigator.mozSocial.getWorker()
.port.postMessage({topic: "worker.reload", data: true});
window.location.reload();
}
function openChat(aTarget, aCallback) {
navigator.mozSocial.openChatWindow("./chatWindow.html?id="+(aTarget), function(win) {
gChats[aTarget] = {win: win, pc: null};
win.document.title = aTarget;
setupExpandHandler(win);
win.addEventListener("unload", function() {
if (!(aTarget in gChats))
return;
stopCall(aTarget);
var chat = gChats[aTarget];
webrtcMedia.endCall(chat.pc, chat.dc, chat.win, chat.audioOnly);
delete gChats[aTarget];
});
if (aCallback) {
aCallback(win);
}
});
}
var chatters = 0;
function notify(type) {
var port = navigator.mozSocial.getWorker().port;
// XXX shouldn't need a full url here.
var end = location.href.indexOf("sidebar.htm");
var baselocation = location.href.substr(0, end);
switch (type) {
case "link":
data = {
id: "foo",
type: null,
icon: baselocation+"/icon.png",
body: "This is a cool link",
action: "link",
actionArgs: {
toURL: baselocation
}
};
port.postMessage({topic:"social.notification-create", data: data});
break;
case "chat-request":
port.postMessage({topic:"social.request-chat", data: baselocation+"/chatWindow.html?id="+(chatters++)});
break;
}
}