2013-03-24 02:28:20 +00:00
|
|
|
/*
|
|
|
|
The MIT License (MIT)
|
|
|
|
Copyright (c) 2013 Calvin Montgomery
|
|
|
|
|
|
|
|
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
|
|
|
|
|
|
|
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
|
|
|
|
|
|
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
|
|
*/
|
2013-02-16 17:19:59 +00:00
|
|
|
|
2013-02-16 05:02:42 +00:00
|
|
|
// Wrapped in a function so I can ensure that the socket
|
|
|
|
// is defined before these statements are run
|
|
|
|
function initCallbacks() {
|
|
|
|
socket.on('disconnect', function() {
|
|
|
|
$('<div/>').addClass('alert').addClass('alert-error')
|
|
|
|
.insertAfter($('.row')[0])[0]
|
|
|
|
.innerHTML = "<h3>Disconnected from server</h3>";
|
|
|
|
});
|
|
|
|
|
2013-03-17 17:14:34 +00:00
|
|
|
socket.on('channelNotRegistered', function() {
|
|
|
|
showChannelRegistration();
|
|
|
|
});
|
|
|
|
|
2013-03-20 18:35:06 +00:00
|
|
|
socket.on('announcement', function(data) {
|
|
|
|
showAnnouncement(data.title, data.text);
|
|
|
|
});
|
|
|
|
|
2013-03-17 17:14:34 +00:00
|
|
|
socket.on('registerChannel', function(data) {
|
|
|
|
if(data.success) {
|
|
|
|
$('#chregnotice').remove();
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
alert(data.error);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2013-02-16 05:02:42 +00:00
|
|
|
socket.on('rank', function(data) {
|
2013-03-16 20:39:58 +00:00
|
|
|
if(data.rank >= Rank.Moderator) {
|
2013-02-16 05:02:42 +00:00
|
|
|
$('#playlist_controls').css("display", "block");
|
2013-03-22 20:04:04 +00:00
|
|
|
$('#playlist_controls button').each(function() {
|
|
|
|
$(this).attr('disabled', false);
|
|
|
|
});
|
2013-03-16 20:39:58 +00:00
|
|
|
$('#qlockbtn').css("display", "block");
|
2013-03-16 21:49:58 +00:00
|
|
|
var poll = $('#pollcontainer .active');
|
|
|
|
if(poll.length > 0) {
|
|
|
|
$('<button/>').addClass('btn btn-danger pull-right').text('Close Poll')
|
|
|
|
.insertAfter(poll.find('.close'))
|
|
|
|
.click(function() {
|
|
|
|
socket.emit('closePoll')
|
|
|
|
});
|
|
|
|
}
|
2013-03-18 01:42:07 +00:00
|
|
|
var users = $('#userlist').children();
|
|
|
|
for(var i = 0; i < users.length; i++) {
|
|
|
|
addUserDropdown(users[i], users[i].children[1].innerHTML);
|
|
|
|
}
|
2013-03-22 20:04:04 +00:00
|
|
|
|
|
|
|
$('#chancontrols').show();
|
2013-03-16 20:39:58 +00:00
|
|
|
}
|
2013-02-16 05:02:42 +00:00
|
|
|
RANK = data.rank;
|
|
|
|
});
|
|
|
|
|
|
|
|
socket.on('login', function(data) {
|
|
|
|
if(!data.success)
|
|
|
|
alert(data.error);
|
|
|
|
else {
|
|
|
|
$('#welcome')[0].innerHTML = "Welcome, " + uname;
|
|
|
|
$('#loginform').css("display", "none");
|
2013-03-05 22:56:21 +00:00
|
|
|
$('#logoutform').css("display", "");
|
|
|
|
$('#loggedin').css("display", "");
|
2013-02-16 05:02:42 +00:00
|
|
|
if(pw != "") {
|
|
|
|
createCookie('sync_uname', uname, 1);
|
|
|
|
createCookie('sync_pw', pw, 1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
socket.on('register', function(data) {
|
|
|
|
if(data.error) {
|
|
|
|
alert(data.error);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2013-03-22 20:04:04 +00:00
|
|
|
socket.on('channelOpts', function(opts) {
|
|
|
|
$('#opt_qopen_allow_qnext').prop('checked', opts.qopen_allow_qnext);
|
|
|
|
$('#opt_qopen_allow_move').prop('checked', opts.qopen_allow_move);
|
|
|
|
$('#opt_qopen_allow_delete').prop('checked', opts.qopen_allow_delete);
|
|
|
|
$('#opt_qopen_allow_playnext').prop('checked', opts.qopen_allow_playnext);
|
|
|
|
$('#opt_pagetitle').attr('placeholder', opts.pagetitle);
|
|
|
|
document.title = opts.pagetitle;
|
2013-03-23 22:21:54 +00:00
|
|
|
$('opt_customcss').val(opts.customcss);
|
2013-03-23 22:15:19 +00:00
|
|
|
$('#customCss').remove();
|
|
|
|
if(opts.customcss != "") {
|
|
|
|
$('<link/>').attr("rel", "stylesheet")
|
|
|
|
.attr("href", opts.customcss)
|
|
|
|
.attr("id", "customCss")
|
|
|
|
.insertAfter($('link[href="./assets/css/ytsync.css"]'));
|
|
|
|
}
|
|
|
|
|
2013-03-22 20:04:04 +00:00
|
|
|
CHANNELOPTS = opts;
|
|
|
|
if(opts.qopen_allow_qnext)
|
|
|
|
$('#queue_next').attr('disabled', false);
|
|
|
|
if(opts.qopen_allow_playnext)
|
|
|
|
$('#play_next').attr('disabled', false);
|
|
|
|
rebuildPlaylist();
|
|
|
|
});
|
|
|
|
|
2013-03-07 01:38:16 +00:00
|
|
|
socket.on('usercount', function(data) {
|
|
|
|
$('#usercount').text(data.count + " connected users");
|
|
|
|
});
|
|
|
|
|
2013-02-16 05:02:42 +00:00
|
|
|
socket.on('chatMsg', function(data) {
|
2013-03-05 20:07:21 +00:00
|
|
|
var div = formatChatMessage(data);
|
2013-02-16 05:02:42 +00:00
|
|
|
$('#messagebuffer')[0].appendChild(div);
|
|
|
|
// Cap chatbox at most recent 100 messages
|
|
|
|
if($('#messagebuffer').children().length > 100) {
|
|
|
|
$($('#messagebufer').children()[0]).remove();
|
|
|
|
}
|
|
|
|
$('#messagebuffer').scrollTop($('#messagebuffer').prop("scrollHeight"));
|
|
|
|
});
|
|
|
|
|
|
|
|
socket.on('playlist', function(data) {
|
|
|
|
var ul = $('#queue')[0];
|
|
|
|
var n = ul.children.length;
|
|
|
|
for(var i = 0; i < n; i++) {
|
|
|
|
ul.removeChild(ul.children[0]);
|
|
|
|
}
|
|
|
|
for(var i = 0; i < data.pl.length; i++) {
|
|
|
|
var li = makeQueueEntry(data.pl[i]);
|
2013-03-16 20:39:58 +00:00
|
|
|
if(RANK >= Rank.Moderator || OPENQUEUE)
|
2013-02-16 05:02:42 +00:00
|
|
|
addQueueButtons(li);
|
|
|
|
$(li).appendTo(ul);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
socket.on('queue', function(data) {
|
|
|
|
var li = makeQueueEntry(data.media);
|
2013-03-16 20:39:58 +00:00
|
|
|
if(RANK >= Rank.Moderator || OPENQUEUE)
|
2013-02-16 05:02:42 +00:00
|
|
|
addQueueButtons(li);
|
|
|
|
$(li).css('display', 'none');
|
|
|
|
var idx = data.pos;
|
|
|
|
var ul = $('#queue')[0];
|
|
|
|
$(li).appendTo(ul);
|
|
|
|
if(idx < ul.children.length - 1)
|
|
|
|
moveVideo(ul.children.length - 1, idx);
|
|
|
|
$(li).show('blind');
|
|
|
|
});
|
|
|
|
|
|
|
|
socket.on('unqueue', function(data) {
|
|
|
|
if(data.pos == POSITION && $('#queue').children().length > POSITION + 1) {
|
|
|
|
$($('#queue').children()[POSITION+1]).addClass("alert alert-info");
|
|
|
|
}
|
|
|
|
var li = $('#queue').children()[data.pos];
|
2013-03-17 14:54:01 +00:00
|
|
|
//$(li).hide('blind', function() {
|
2013-02-16 05:02:42 +00:00
|
|
|
$(li).remove();
|
2013-03-17 14:54:01 +00:00
|
|
|
//});
|
2013-02-16 05:02:42 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
socket.on('moveVideo', function(data) {
|
|
|
|
moveVideo(data.src, data.dest);
|
|
|
|
});
|
|
|
|
|
2013-03-16 20:39:58 +00:00
|
|
|
socket.on('queueLock', function(data) {
|
|
|
|
OPENQUEUE = !data.locked;
|
|
|
|
if(OPENQUEUE) {
|
|
|
|
$('#playlist_controls').css('display', '');
|
|
|
|
if(RANK < Rank.Moderator) {
|
|
|
|
$('#qlockbtn').css('display', 'none');
|
2013-03-22 20:04:04 +00:00
|
|
|
rebuildPlaylist();
|
|
|
|
if(!CHANNELOPTS.qopen_allow_qnext)
|
|
|
|
$('#queue_next').attr('disabled', true);
|
|
|
|
if(!CHANNELOPTS.qopen_allow_playnext)
|
|
|
|
$('#play_next').attr('disabled', true);
|
2013-03-16 20:39:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else if(RANK < Rank.Moderator) {
|
|
|
|
$('#playlist_controls').css('display', 'none');
|
|
|
|
}
|
2013-03-22 20:04:04 +00:00
|
|
|
if(OPENQUEUE) {
|
|
|
|
$('#qlockbtn').removeClass('btn-danger')
|
|
|
|
.addClass('btn-success')
|
|
|
|
.text('Lock Queue');
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
$('#qlockbtn').removeClass('btn-success')
|
|
|
|
.addClass('btn-danger')
|
|
|
|
.text('Unlock Queue');
|
2013-03-16 20:39:58 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2013-02-16 05:02:42 +00:00
|
|
|
socket.on('updatePlaylistIdx', function(data) {
|
|
|
|
var liold = $('#queue').children()[POSITION];
|
|
|
|
$(liold).removeClass("alert alert-info");
|
|
|
|
var linew = $('#queue').children()[data.idx];
|
|
|
|
$(linew).addClass("alert alert-info");
|
|
|
|
POSITION= data.idx;
|
|
|
|
});
|
|
|
|
|
|
|
|
socket.on('mediaUpdate', function(data) {
|
2013-03-16 20:13:40 +00:00
|
|
|
$('#currenttitle').text("Currently Playing: " + data.title);
|
2013-02-16 05:02:42 +00:00
|
|
|
if(data.type == "yt")
|
|
|
|
updateYT(data);
|
|
|
|
else if(data.type == "tw")
|
|
|
|
loadTwitch(data.id);
|
2013-03-16 22:17:36 +00:00
|
|
|
else if(data.type == "li")
|
|
|
|
loadLivestream(data.id);
|
2013-02-16 05:02:42 +00:00
|
|
|
else if(data.type == "sc")
|
|
|
|
updateSC(data);
|
|
|
|
else if(data.type == "vi")
|
|
|
|
updateVI(data);
|
2013-03-23 18:17:39 +00:00
|
|
|
else if(data.type == "dm")
|
|
|
|
updateDM(data);
|
2013-02-16 05:02:42 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
socket.on('userlist', function(data) {
|
|
|
|
for(var i = 0; i < data.length; i++) {
|
|
|
|
addUser(data[i].name, data[i].rank, data[i].leader);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
socket.on('addUser', function(data) {
|
|
|
|
addUser(data.name, data.rank, data.leader);
|
|
|
|
});
|
|
|
|
|
|
|
|
socket.on('updateUser', function(data) {
|
|
|
|
if(data.name == uname) {
|
|
|
|
LEADER = data.leader;
|
|
|
|
if(LEADER) {
|
|
|
|
// I'm a leader! Set up sync function
|
|
|
|
sendVideoUpdate = function() {
|
|
|
|
if(MEDIATYPE == "yt") {
|
|
|
|
socket.emit('mediaUpdate', {
|
|
|
|
id: parseYTURL(PLAYER.getVideoUrl()),
|
|
|
|
seconds: PLAYER.getCurrentTime(),
|
|
|
|
paused: PLAYER.getPlayerState() == YT.PlayerState.PAUSED,
|
|
|
|
type: "yt"
|
|
|
|
});
|
|
|
|
}
|
|
|
|
else if(MEDIATYPE == "sc") {
|
|
|
|
PLAYER.getPosition(function(pos) {
|
|
|
|
socket.emit('mediaUpdate', {
|
|
|
|
id: PLAYER.mediaId,
|
|
|
|
seconds: pos / 1000,
|
|
|
|
paused: false,
|
|
|
|
type: "sc"
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
else if(MEDIATYPE == "vi") {
|
|
|
|
PLAYER.api('getCurrentTime', function(data) {
|
|
|
|
socket.emit('mediaUpdate', {
|
|
|
|
id: PLAYER.videoid,
|
2013-03-05 23:06:25 +00:00
|
|
|
seconds: data,
|
2013-02-16 05:02:42 +00:00
|
|
|
paused: false,
|
|
|
|
type: "vi"
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
2013-03-23 18:17:39 +00:00
|
|
|
else if(MEDIATYPE == "dm") {
|
|
|
|
socket.emit('mediaUpdate', {
|
|
|
|
id: PLAYER.mediaId,
|
|
|
|
seconds: PLAYER.currentTime,
|
|
|
|
paused: PLAYER.paused,
|
|
|
|
type: "dm"
|
|
|
|
});
|
|
|
|
}
|
2013-02-16 05:02:42 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
// I'm not a leader. Don't send syncs to the server
|
|
|
|
else {
|
|
|
|
sendVideoUpdate = function() { }
|
|
|
|
}
|
2013-02-16 22:33:38 +00:00
|
|
|
|
|
|
|
RANK = data.rank;
|
|
|
|
if(data.rank >= Rank.Moderator)
|
|
|
|
$('#playlist_controls').css("display", "block");
|
2013-02-16 05:02:42 +00:00
|
|
|
}
|
|
|
|
var users = $('#userlist').children();
|
|
|
|
for(var i = 0; i < users.length; i++) {
|
2013-03-17 16:50:39 +00:00
|
|
|
var name = users[i].children[1].innerHTML;
|
2013-02-16 05:02:42 +00:00
|
|
|
// Reformat user
|
|
|
|
if(name == data.name) {
|
|
|
|
fmtUserlistItem(users[i], data.rank, data.leader);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
socket.on('userLeave', function(data) {
|
|
|
|
var users = $('#userlist').children();
|
|
|
|
for(var i = 0; i < users.length; i++) {
|
2013-03-17 14:42:22 +00:00
|
|
|
var name = users[i].children[1].innerHTML;
|
2013-02-16 05:02:42 +00:00
|
|
|
if(name == data.name) {
|
|
|
|
$('#userlist')[0].removeChild(users[i]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
socket.on('librarySearchResults', function(data) {
|
|
|
|
var n = $('#library').children().length;
|
|
|
|
for(var i = 0; i < n; i++) {
|
|
|
|
$('#library')[0].removeChild($('#library').children()[0]);
|
|
|
|
}
|
|
|
|
var ul = $('#library')[0];
|
|
|
|
for(var i = 0; i < data.results.length; i++) {
|
|
|
|
var li = makeQueueEntry(data.results[i]);
|
2013-03-16 20:39:58 +00:00
|
|
|
if(RANK >= Rank.Moderator || OPENQUEUE)
|
2013-02-16 05:02:42 +00:00
|
|
|
addLibraryButtons(li, data.results[i].id);
|
|
|
|
$(li).appendTo(ul);
|
|
|
|
}
|
|
|
|
});
|
2013-03-16 21:49:58 +00:00
|
|
|
|
|
|
|
socket.on('newPoll', function(data) {
|
|
|
|
addPoll(data);
|
|
|
|
});
|
|
|
|
|
|
|
|
socket.on('updatePoll', function(data) {
|
|
|
|
updatePoll(data);
|
|
|
|
});
|
|
|
|
|
|
|
|
socket.on('closePoll', function() {
|
|
|
|
closePoll();
|
|
|
|
});
|
2013-02-16 05:02:42 +00:00
|
|
|
}
|