Skip to content

Commit

Permalink
pidora.js rewrite. Closes #20
Browse files Browse the repository at this point in the history
jacroe committed Jul 18, 2013
1 parent 4143c70 commit 910e24e
Showing 1 changed file with 99 additions and 84 deletions.
183 changes: 99 additions & 84 deletions inc/pidora.js
Original file line number Diff line number Diff line change
@@ -1,121 +1,136 @@
$(document).ready(function()
{
var msgShown = false;
oldData = $('#content h1').html();
var newDataPlain, oldSongData, newSongData;
window.setInterval(function()
{
if(msgShown)
$('#msg').fadeOut('slow', function()
$.get("api.php", function(newDataPlain)
{
$('#content').fadeIn('slow');
});
$.get("api.php", function(newData)
{
var newDataObj = JSON.parse(newData)
if(newDataObj.msg)
newData = JSON.parse(newDataPlain);
if(newData.title)
{
$('#content').fadeOut('slow', function()
if($('#msg').is(':visible'))
{
$('#msg h1').html(newDataObj.msg);
$('#msg').fadeIn('slow');
msgShown = true;
});
clearScreen(function()
{
$('#content').fadeIn('slow');
});
}
newSongData = newData;
if(JSON.stringify(oldSongData) !== JSON.stringify(newSongData))
{
oldSongData = newSongData;
clearScreen(function()
{
updateSong(newSongData);
$('#content').fadeIn('slow');
});
setMousetraps();
}
}
else if (!(oldData == newDataObj.title) && newDataObj.title)
else if(newData.msg)
{
oldData = newDataObj.title;
$('#content').fadeOut('slow', function()
clearScreen(function()
{
$('#content h1').html(newDataObj.title)
$('#content h2').html(newDataObj.artist)
$('#content .album').html(newDataObj.album)
$('#content .details').html("EMPTY").hide()
if(newDataObj.loved) {$('#content .love').show()} else {$('#content .love').hide()}
$('#content .albumart').attr("src", newDataObj.artURL)
$('#content').fadeIn('slow')
$('#msg h1').html(newData.msg);
$('#msg').fadeIn('slow');
});
setMousetraps();
}
else if(newDataObj.loved && !($('#content .love').is(':visible')))
{
$('#content .love').show();
}
});
}, 3000);
});
function stationSetup()
{
index = 0;
getStations(index);
Mousetrap.reset();
Mousetrap.bind('0', function() { control('s'.concat(index,'0')); });
Mousetrap.bind('1', function() { control('s'.concat(index,'1')); });
Mousetrap.bind('2', function() { control('s'.concat(index,'2')); });
Mousetrap.bind('3', function() { control('s'.concat(index,'3')); });
Mousetrap.bind('4', function() { control('s'.concat(index,'4')); });
Mousetrap.bind('5', function() { control('s'.concat(index,'5')); });
Mousetrap.bind('6', function() { control('s'.concat(index,'6')); });
Mousetrap.bind('7', function() { control('s'.concat(index,'7')); });
Mousetrap.bind('8', function() { control('s'.concat(index,'8')); });
Mousetrap.bind('9', function() { control('s'.concat(index,'9')); });
Mousetrap.bind('n', function() { getStations(++index, true); })
Mousetrap.bind('b', function() { getStations(--index, true); })
Mousetrap.bind('esc', function()
{
$('#stationList').fadeOut('slow', function(){$('#content').fadeIn('slow')});
setMousetraps();
});
}
function getStations(index, shown)

function clearScreen(doNext)
{
$.get("api.php", {station:index}).done(function(stationList)
$('#content, #msg, #stationList').fadeOut('slow').promise().done(function()
{
if(shown)
{
$('#stationList').fadeOut('slow', function()
{
$('#stationList').html(stationList).fadeIn('slow');
});
}
else
{
$('#content').fadeOut('slow', function()
{
$('#stationList').html(stationList).fadeIn('slow');
});
}
doNext();
});
}
function control(action)

function updateSong(data)
{
$.get("api.php", {control:action});
$('#content h1').html(data.title);
$('#content h2').html(data.artist);
$('#content .album').html(data.album);
$('#content .details').html("EMPTY").hide();
if(data.loved)
$('#content .love').show();
else
$('#content .love').hide();
$('#content .albumart').attr("src", data.artURL);
};
function explain()

function explainSong()
{
details = $('p.details').html();
if (details == "EMPTY")
{
$('p.details').html("Grabbing explanation...").fadeToggle("slow");
$.get("api.php",{control:"e"}).done(function(data)
$('p.details').html("Grabbing explanation...").fadeToggle('slow')
$.get("api.php", {control:'e'}).done(function(explainPlain)
{
dataObj = JSON.parse(data);
explain = JSON.parse(explainPlain);
$('p.details').fadeOut('slow', function()
{
$(this).html(dataObj.explanation).fadeIn('slow')
$(this).html(explain.explanation).fadeIn('slow');
});
});
}
else {$('p.details').fadeToggle("slow");}
else
$('p.details').fadeToggle('slow');
}

function stationSetup()
{
var index = 0;
getStations(index);
Mousetrap.reset();
Mousetrap.bind('0', function() { sendCommand('s'.concat(index,'0')); });
Mousetrap.bind('1', function() { sendCommand('s'.concat(index,'1')); });
Mousetrap.bind('2', function() { sendCommand('s'.concat(index,'2')); });
Mousetrap.bind('3', function() { sendCommand('s'.concat(index,'3')); });
Mousetrap.bind('4', function() { sendCommand('s'.concat(index,'4')); });
Mousetrap.bind('5', function() { sendCommand('s'.concat(index,'5')); });
Mousetrap.bind('6', function() { sendCommand('s'.concat(index,'6')); });
Mousetrap.bind('7', function() { sendCommand('s'.concat(index,'7')); });
Mousetrap.bind('8', function() { sendCommand('s'.concat(index,'8')); });
Mousetrap.bind('9', function() { sendCommand('s'.concat(index,'9')); });
Mousetrap.bind('n', function() { getStations(++index); });
Mousetrap.bind('b', function() { getStations(--index); });
Mousetrap.bind('esc', function()
{
clearScreen(function()
{
$('#content').fadeIn('slow');
setMousetraps();
});
});
}

function getStations(index)
{
$.get("api.php", {station:index}).done(function(stationList)
{
clearScreen(function()
{
$('#stationList').html(stationList).fadeIn('slow');
});
});
}

function sendCommand(action)
{
$.get("api.php", {control:action}); // TODO - Rename "control" to "command"
};

function setMousetraps()
{
Mousetrap.reset();
Mousetrap.bind(['p', 'space'], function() { control('p'); });
Mousetrap.bind('n', function() { control('n'); });
Mousetrap.bind('l', function() { control('+'); });
Mousetrap.bind('b', function() { control('-'); });
Mousetrap.bind('t', function() { control('t'); });
Mousetrap.bind('q', function() { control('q'); });
Mousetrap.bind('e', function() { explain(); });
Mousetrap.bind(['p', 'space'], function() { sendCommand('p'); });
Mousetrap.bind('n', function() { sendCommand('n'); });
Mousetrap.bind('l', function() { sendCommand('+'); });
Mousetrap.bind('b', function() { sendCommand('-'); });
Mousetrap.bind('t', function() { sendCommand('t'); });
Mousetrap.bind('q', function() { sendCommand('q'); });
Mousetrap.bind('e', function() { explainSong(); });
Mousetrap.bind('s', function() { stationSetup(); });
}

0 comments on commit 910e24e

Please sign in to comment.