use fewer global vars and move init code to a central place

Signed-off-by: Christoph Wurst <christoph@winzerhof-wurst.at>
This commit is contained in:
Christoph Wurst 2016-10-18 16:40:09 +02:00
Родитель c583f41d6f
Коммит eadc2749e6
6 изменённых файлов: 195 добавлений и 166 удалений

Просмотреть файл

@ -1,4 +1,4 @@
/* global Marionette, Backbone, webrtc */
/* global Marionette, Backbone, OCA */
/**
* @author Christoph Wurst <christoph@winzerhof-wurst.at>
@ -20,7 +20,7 @@
*
*/
(function(OCA, Marionette, Backbone, webrtc) {
(function(OCA, Marionette, Backbone) {
'use strict';
OCA.SpreedMe = OCA.SpreedMe || {};
@ -71,11 +71,11 @@
var videoHidden = false;
$('#hideVideo').click(function() {
if (videoHidden) {
webrtc.resumeVideo();
OCA.SpreedMe.webrtc.resumeVideo();
$(this).data('title', 'Disable video').removeClass('video-disabled');
videoHidden = false;
} else {
webrtc.pauseVideo();
OCA.SpreedMe.webrtc.pauseVideo();
$(this).data('title', 'Enable video').addClass('video-disabled');
videoHidden = true;
}
@ -83,11 +83,11 @@
var audioMuted = false;
$('#mute').click(function() {
if (audioMuted) {
webrtc.unmute();
OCA.SpreedMe.webrtc.unmute();
$(this).data('title', 'Mute audio').removeClass('audio-disabled');
audioMuted = false;
} else {
webrtc.mute();
OCA.SpreedMe.webrtc.mute();
$(this).data('title', 'Enable audio').addClass('audio-disabled');
audioMuted = true;
}
@ -175,6 +175,8 @@
console.log('Starting spreed …');
var self = this;
OCA.SpreedMe.initWebRTC();
OCA.SpreedMe.initRooms();
this._registerPageEvents();
this._onRegisterHashChange();
@ -192,4 +194,4 @@
});
OCA.SpreedMe.App = App;
})(OCA, Marionette, Backbone, webrtc);
})(OCA, Marionette, Backbone);

Просмотреть файл

@ -1,4 +1,4 @@
/* global Backbone */
/* global Backbone, OCA */
/**
* @author Christoph Wurst <christoph@winzerhof-wurst.at>
@ -35,4 +35,5 @@
});
OCA.SpreedMe.Models.Room = Room;
})(OCA, Backbone);

Просмотреть файл

@ -1,4 +1,4 @@
/* global Backbone */
/* global Backbone, OC, OCA */
/**
* @author Christoph Wurst <christoph@winzerhof-wurst.at>
@ -20,7 +20,8 @@
*
*/
(function(OCA, Backbone) {
(function(OCA, OC, Backbone) {
'use strict';
OCA.SpreedMe = OCA.SpreedMe || {};
OCA.SpreedMe.Models = OCA.SpreedMe.Models || {};
@ -32,4 +33,5 @@
});
OCA.SpreedMe.Models.RoomCollection = RoomCollection;
})(OCA, Backbone);
})(OCA, OC, Backbone);

Просмотреть файл

@ -1,46 +1,50 @@
// TODO(fancycode): Should load through AMD if possible.
/* global webrtc: false */
/* global OC, OCA */
$(document).ready(function() {
var editRoomname = $('#edit-roomname');
editRoomname.keyup(function() {
editRoomname.tooltip('hide');
editRoomname.removeClass('error');
});
(function(OCA, OC, $) {
'use strict';
OCA.SpreedMe = OCA.SpreedMe || {};
var currentRoomId = 0;
function initRooms() {
var editRoomname = $('#edit-roomname');
editRoomname.keyup(function() {
editRoomname.tooltip('hide');
editRoomname.removeClass('error');
});
var currentRoomId = 0;
var roomChannel = Backbone.Radio.channel('rooms');
OCA.SpreedMe.Rooms = {
join: function(roomId) {
$('#emptycontent').hide();
$('.videoView').addClass('hidden');
$('#app-content').addClass('icon-loading');
OCA.SpreedMe.Rooms = {
join: function(roomId) {
$('#emptycontent').hide();
$('.videoView').addClass('hidden');
$('#app-content').addClass('icon-loading');
currentRoomId = roomId;
webrtc.joinRoom(roomId);
currentRoomId = roomId;
OCA.SpreedMe.webrtc.joinRoom(roomId);
roomChannel.trigger('active', roomId);
OCA.SpreedMe.Rooms.ping();
},
currentRoom: function() {
return currentRoomId;
},
peers: function(roomId) {
return $.ajax({
url: OC.generateUrl('/apps/spreed/api/room/{roomId}/peers', {roomId: roomId})
});
},
ping: function() {
$.post(
OC.generateUrl('/apps/spreed/api/ping'),
{
currentRoom: OCA.SpreedMe.Rooms.currentRoom()
}
);
}
};
OCA.SpreedMe.Rooms.ping();
},
currentRoom: function() {
return currentRoomId;
},
peers: function(roomId) {
return $.ajax({
url: OC.generateUrl('/apps/spreed/api/room/{roomId}/peers', {roomId: roomId})
});
},
ping: function() {
$.post(
OC.generateUrl('/apps/spreed/api/ping'),
{
currentRoom: OCA.SpreedMe.Rooms.currentRoom()
}
);
}
};
$(document).click(function(e) {
var target = e.target;
@ -56,4 +60,6 @@ $(document).ready(function() {
}
});
});
OCA.SpreedMe.initRooms = initRooms;
})(OCA, OC, $);

Просмотреть файл

@ -1,137 +1,155 @@
// TODO(fancycode): Should load through AMD if possible.
/* global SimpleWebRTC: false */
/* global SimpleWebRTC, OC, OCA: false */
var webrtc;
function openEventSource() {
(function(OCA, OC) {
'use strict';
// Connect to the messages endpoint and pull for new messages
var messageEventSource = new OC.EventSource(OC.generateUrl('/apps/spreed/messages'));
var previousUsersInRoom = [];
Array.prototype.diff = function(a) {
return this.filter(function(i) {return a.indexOf(i) < 0;});
};
messageEventSource.listen('usersInRoom', function(users) {
var currentUsersInRoom = [];
users.forEach(function(user) {
currentUsersInRoom.push(user['userId']);
OCA.SpreedMe = OCA.SpreedMe || {};
/**
* @private
*/
function openEventSource() {
// Connect to the messages endpoint and pull for new messages
var messageEventSource = new OC.EventSource(OC.generateUrl('/apps/spreed/messages'));
var previousUsersInRoom = [];
Array.prototype.diff = function(a) {
return this.filter(function(i) {
return a.indexOf(i) < 0;
});
};
messageEventSource.listen('usersInRoom', function(users) {
var currentUsersInRoom = [];
users.forEach(function(user) {
currentUsersInRoom.push(user['userId']);
});
if (currentUsersInRoom.length !== previousUsersInRoom.length) {
$('#app-content').attr('class', '');
$('#app-content').addClass('participants-' + currentUsersInRoom.length);
}
var disconnectedUsers = previousUsersInRoom.diff(currentUsersInRoom);
disconnectedUsers.forEach(function(user) {
OCA.SpreedMe.webrtc.removePeers(user);
});
previousUsersInRoom = currentUsersInRoom;
});
if(currentUsersInRoom.length !== previousUsersInRoom.length) {
$('#app-content').attr('class','');
$('#app-content').addClass('participants-'+currentUsersInRoom.length);
}
messageEventSource.listen('message', function(message) {
message = JSON.parse(message);
var peers = self.webrtc.getPeers(message.from, message.roomType);
var peer;
var disconnectedUsers = previousUsersInRoom.diff(currentUsersInRoom);
disconnectedUsers.forEach(function(user) {
webrtc.removePeers(user);
});
previousUsersInRoom = currentUsersInRoom;
});
messageEventSource.listen('message', function(message) {
message = JSON.parse(message);
var peers = self.webrtc.getPeers(message.from, message.roomType);
var peer;
if (message.type === 'offer') {
if (peers.length) {
peers.forEach(function (p) {
if (p.sid === message.sid) {
peer = p;
}
});
}
if (!peer) {
peer = self.webrtc.createPeer({
id: message.from,
sid: message.sid,
type: message.roomType,
enableDataChannels: false,
sharemyscreen: message.roomType === 'screen' && !message.broadcaster,
broadcaster: message.roomType === 'screen' && !message.broadcaster ? self.connection.getSessionid() : null
});
webrtc.emit('createdPeer', peer);
}
peer.handleMessage(message);
} else if (peers.length) {
peers.forEach(function (peer) {
if (message.sid) {
if (peer.sid === message.sid) {
if (message.type === 'offer') {
if (peers.length) {
peers.forEach(function(p) {
if (p.sid === message.sid) {
peer = p;
}
});
}
if (!peer) {
peer = self.webrtc.createPeer({
id: message.from,
sid: message.sid,
type: message.roomType,
enableDataChannels: false,
sharemyscreen: message.roomType === 'screen' && !message.broadcaster,
broadcaster: message.roomType === 'screen' && !message.broadcaster ? self.connection.getSessionid() : null
});
OCA.SpreedMe.webrtc.emit('createdPeer', peer);
}
peer.handleMessage(message);
} else if (peers.length) {
peers.forEach(function(peer) {
if (message.sid) {
if (peer.sid === message.sid) {
peer.handleMessage(message);
}
} else {
peer.handleMessage(message);
}
});
}
});
}
function initWebRTC() {
'use strict';
webrtc = new SimpleWebRTC({
localVideoEl: 'localVideo',
remoteVideosEl: '',
autoRequestMedia: true,
debug: false,
autoAdjustMic: false,
detectSpeakingEvents: true,
connection: OCA.SpreedMe.XhrConnection,
supportDataChannel: true
});
OCA.SpreedMe.webrtc = webrtc;
OCA.SpreedMe.webrtc.on('localMediaError', function(error) {
console.log("Access to local media failed", error);
var message, messageAdditional;
if (error.name === "NotAllowedError") {
if (error.message && error.message.indexOf("Only secure origins") !== -1) {
message = t('spreed', 'Access to microphone & camera is only possible with HTTPS');
messageAdditional = t('spreed', 'Please adjust your configuration');
} else {
peer.handleMessage(message);
message = t('spreed', 'Access to microphone & camera was denied');
$('#emptycontent p').hide();
}
});
}
});
}
$(document).ready(function() {
webrtc = new SimpleWebRTC({
localVideoEl: 'localVideo',
remoteVideosEl: '',
autoRequestMedia: true,
debug: false,
autoAdjustMic: false,
detectSpeakingEvents: true,
connection: OCA.SpreedMe.XhrConnection,
supportDataChannel: true
});
webrtc.on('localMediaError', function(error) {
console.log("Access to local media failed", error);
var message, messageAdditional;
if (error.name === "NotAllowedError") {
if (error.message && error.message.indexOf("Only secure origins") !== -1) {
message = t('spreed', 'Access to microphone & camera is only possible with HTTPS');
messageAdditional = t('spreed', 'Please adjust your configuration');
} else {
message = t('spreed', 'Access to microphone & camera was denied');
message = t('spreed', 'Error while accessing local media: {error}', {error: error.message || error.name});
$('#emptycontent p').hide();
}
} else {
message = t('spreed', 'Error while accessing local media: {error}', {error: error.message || error.name});
$('#emptycontent p').hide();
}
$('#emptycontent h2').text(message);
$('#emptycontent p').text(messageAdditional);
});
$('#emptycontent h2').text(message);
$('#emptycontent p').text(messageAdditional);
});
webrtc.on('joinedRoom', function() {
$('#app-content').removeClass('icon-loading');
$('.videoView').removeClass('hidden');
openEventSource();
OCA.SpreedMe.app.syncRooms();
});
OCA.SpreedMe.webrtc.on('joinedRoom', function() {
$('#app-content').removeClass('icon-loading');
$('.videoView').removeClass('hidden');
openEventSource();
OCA.SpreedMe.app.syncRooms();
});
webrtc.on('videoAdded', function (video, peer) {
console.log('video added', peer);
var remotes = document.getElementById('remotes');
if (remotes) {
// Indicator for username
var userIndicator = document.createElement('div');
userIndicator.className = 'nameIndicator';
userIndicator.textContent = peer.id;
OCA.SpreedMe.webrtc.on('videoAdded', function(video, peer) {
console.log('video added', peer);
var remotes = document.getElementById('remotes');
if (remotes) {
// Indicator for username
var userIndicator = document.createElement('div');
userIndicator.className = 'nameIndicator';
userIndicator.textContent = peer.id;
// Generic container
var container = document.createElement('div');
container.className = 'videoContainer';
container.id = 'container_' + webrtc.getDomId(peer);
container.appendChild(video);
container.appendChild(userIndicator);
video.oncontextmenu = function () { return false; };
remotes.appendChild(container);
}
});
// Generic container
var container = document.createElement('div');
container.className = 'videoContainer';
container.id = 'container_' + OCA.SpreedMe.webrtc.getDomId(peer);
container.appendChild(video);
container.appendChild(userIndicator);
video.oncontextmenu = function() {
return false;
};
remotes.appendChild(container);
}
});
// a peer was removed
webrtc.on('videoRemoved', function (video, peer) {
var remotes = document.getElementById('remotes');
var el = document.getElementById(peer ? 'container_' + webrtc.getDomId(peer) : 'localScreenContainer');
if (remotes && el) {
remotes.removeChild(el);
}
});
});
// a peer was removed
OCA.SpreedMe.webrtc.on('videoRemoved', function(video, peer) {
var remotes = document.getElementById('remotes');
var el = document.getElementById(peer ? 'container_' + OCA.SpreedMe.webrtc.getDomId(peer) : 'localScreenContainer');
if (remotes && el) {
remotes.removeChild(el);
}
});
}
OCA.SpreedMe.initWebRTC = initWebRTC;
})(OCA, OC);

Просмотреть файл

@ -12,9 +12,9 @@ script(
'models/roomcollection',
'views/roomlistview',
'simplewebrtc',
'webrtc',
'xhrconnection',
'rooms',
'webrtc',
'app',
'init',
]