1
0
mirror of https://github.com/SuperBFG7/ympd synced 2024-11-30 00:19:54 +00:00
ympd/htdocs/js/mpd.js

372 lines
10 KiB
JavaScript
Raw Normal View History

2013-11-04 17:18:38 +00:00
var socket;
var last_state;
2013-11-07 12:47:31 +00:00
var current_app;
var is_firefox;
2013-11-04 17:18:38 +00:00
$('#volumeslider').slider().on('slide', function(event) {
socket.send("MPD_API_SET_VOLUME,"+event.value);
});
2013-11-04 17:18:38 +00:00
2013-11-07 12:47:31 +00:00
var app = $.sammy(function() {
this.before('/', function(e, data) {
socket.send("MPD_API_GET_TRACK_INFO");
2013-11-07 16:40:22 +00:00
$('#nav_links > li').removeClass('active');
2013-11-07 12:47:31 +00:00
});
this.get('#/', function() {
current_app = 'playlist';
2013-11-13 14:30:54 +00:00
$('#breadcrump').addClass('hide');
2013-11-07 12:47:31 +00:00
$('#salamisandwich').find("tr:gt(0)").remove();
//if(is_firefox)
$.get( "/api/get_playlist", socket.onmessage);
//else
// socket.send("MPD_API_GET_PLAYLIST");
2013-11-07 12:47:31 +00:00
$('#panel-heading').text("Playlist");
2013-11-07 16:40:22 +00:00
$('#playlist').addClass('active');
2013-11-07 12:47:31 +00:00
});
2013-11-07 16:40:22 +00:00
2013-11-07 12:47:31 +00:00
this.get(/\#\/browse\/(.*)/, function() {
current_app = 'browse';
2013-11-13 14:30:54 +00:00
$('#breadcrump').removeClass('hide').empty();
2013-11-07 12:47:31 +00:00
$('#salamisandwich').find("tr:gt(0)").remove();
var path = this.params['splat'];
if(path == '')
path = "/";
//if(is_firefox)
$.get( "/api/get_browse/" + encodeURIComponent(path), socket.onmessage);
//else
// socket.send("MPD_API_GET_BROWSE,"+path);
2013-11-07 12:47:31 +00:00
$('#panel-heading').text("Browse database: "+path+"");
2013-11-13 14:30:54 +00:00
var path_array = path[0].split('/');
var full_path = "";
$.each(path_array, function(index, chunk) {
if(path_array.length - 1 == index) {
$('#breadcrump').append("<li class=\"active\">"+ chunk + "</li>");
return;
}
2013-11-07 16:40:22 +00:00
2013-11-13 14:30:54 +00:00
full_path = full_path + chunk;
$('#breadcrump').append("<li><a href=\"#/browse/" + full_path + "\">"+chunk+"</a></li>");
full_path += "/";
});
$('#browse').addClass('active');
2013-11-07 16:40:22 +00:00
});
2013-11-07 12:47:31 +00:00
this.get("/", function(context) {
context.redirect("#/");
});
});
2013-11-07 09:09:40 +00:00
$(document).ready(function(){
is_firefox = true;
2013-11-07 09:09:40 +00:00
webSocketConnect();
});
function webSocketConnect() {
if (typeof MozWebSocket != "undefined") {
socket = new MozWebSocket(get_appropriate_ws_url(), "ympd-client");
} else {
socket = new WebSocket(get_appropriate_ws_url(), "ympd-client");
2013-11-04 23:17:28 +00:00
}
2013-11-04 17:18:38 +00:00
try {
socket.onopen = function() {
console.log("Connected");
2013-11-07 12:47:31 +00:00
app.run();
/* Push Initial state on first visit */
//$(window).trigger("statechange")
}
socket.onmessage =function got_packet(msg) {
console.log(typeof msg);
if(msg instanceof MessageEvent) {
if(msg.data === last_state)
return;
var obj = JSON.parse(msg.data);
} else {
var obj = msg;
}
switch (obj.type) {
case "playlist":
2013-11-07 12:47:31 +00:00
if(current_app !== 'playlist')
2013-11-07 09:09:40 +00:00
break;
2013-11-07 16:40:22 +00:00
$('#salamisandwich > tbody').empty();
for (var song in obj.data) {
var minutes = Math.floor(obj.data[song].duration / 60);
var seconds = obj.data[song].duration - minutes * 60;
2013-11-07 16:40:22 +00:00
$('#salamisandwich > tbody').append(
"<tr trackid=\"" + obj.data[song].id + "\"><td>" + obj.data[song].pos + "</td>" +
2013-11-05 23:15:24 +00:00
"<td>"+ obj.data[song].title +"</td>" +
2013-11-07 09:09:40 +00:00
"<td>"+ minutes + ":" + (seconds < 10 ? '0' : '') + seconds +
2013-11-07 16:40:22 +00:00
"</td></tr>");
2013-11-07 09:09:40 +00:00
}
2013-11-07 16:40:22 +00:00
$('#salamisandwich > tbody > tr').on({
mouseover: function(){
if($(this).children().last().has("a").length == 0)
$(this).children().last().append(
2013-11-13 14:30:54 +00:00
"<a class=\"pull-right btn-group-hover\" href=\"#/\" " +
2013-11-07 16:40:22 +00:00
"onclick=\"socket.send('MPD_API_RM_TRACK," + $(this).attr("trackid") +"'); $(this).parents('tr').remove();\">" +
"<span class=\"glyphicon glyphicon-trash\"></span></a>")
.find('a').fadeTo('fast',1);
},
click: function() {
2013-12-03 11:48:52 +00:00
$('#salamisandwich > tbody > tr').removeClass('active');
socket.send('MPD_API_PLAY_TRACK,'+$(this).attr('trackid'));
2013-12-03 11:48:52 +00:00
$(this).addClass('active');
},
2013-11-07 16:40:22 +00:00
mouseleave: function(){
$(this).children().last().find("a").stop().remove();
}
});
2013-11-07 09:09:40 +00:00
break;
case "browse":
2013-11-07 12:47:31 +00:00
//if(state.data.state !== 'nav_browse')
// break;
if(current_app !== 'browse')
2013-11-07 09:09:40 +00:00
break;
for (var item in obj.data) {
switch(obj.data[item].type) {
case "directory":
2013-11-07 16:40:22 +00:00
$('#salamisandwich > tbody').append(
"<tr uri=\"" + obj.data[item].dir + "\">" +
"<td><span class=\"glyphicon glyphicon-folder-open\"></span></td>" +
2013-11-13 14:30:54 +00:00
"<td><a href=\"#/browse/"+ obj.data[item].dir +"\">" + basename(obj.data[item].dir) +"</a></td>" +
2013-11-07 09:09:40 +00:00
"<td></td></tr>");
break;
case "song":
var minutes = Math.floor(obj.data[item].duration / 60);
var seconds = obj.data[item].duration - minutes * 60;
2013-11-07 16:40:22 +00:00
$('#salamisandwich > tbody').append(
"<tr uri=\"" + obj.data[item].uri + "\">" +
"<td><span class=\"glyphicon glyphicon-music\"></span></td>" +
"<td>" + obj.data[item].title +"</td>" +
2013-11-07 09:09:40 +00:00
"<td>"+ minutes + ":" + (seconds < 10 ? '0' : '') + seconds +"</td></tr>");
break;
2013-11-07 16:40:22 +00:00
2013-11-07 09:09:40 +00:00
case "playlist":
break;
}
}
2013-11-07 16:40:22 +00:00
$('#salamisandwich > tbody > tr').on({
mouseover: function(){
if($(this).children().last().has("a").length == 0)
if($(this).attr("uri").length > 0)
$(this).children().last().append(
2013-11-13 14:30:54 +00:00
"<a role=\"button\" class=\"pull-right btn-group-hover\" " +
2013-12-03 11:48:52 +00:00
"onclick=\"socket.send('MPD_API_ADD_TRACK," + $(this).attr("uri") +"'); $(this).parents('tr').addClass('active');\">" +
2013-11-07 16:40:22 +00:00
"<span class=\"glyphicon glyphicon-plus\"></span></a>")
.find('a').fadeTo('fast',1);
},
mouseleave: function(){
$(this).children().last().find("a").stop().remove();
}
});
break;
case "state":
if(JSON.stringify(obj) === JSON.stringify(last_state))
break;
2013-11-07 09:09:40 +00:00
var total_minutes = Math.floor(obj.data.totalTime / 60);
var total_seconds = obj.data.totalTime - total_minutes * 60;
var elapsed_minutes = Math.floor(obj.data.elapsedTime / 60);
var elapsed_seconds = obj.data.elapsedTime - elapsed_minutes * 60;
$('#volumeslider').slider('setValue', obj.data.volume);
var progress = Math.floor(100*obj.data.elapsedTime/obj.data.totalTime) + "%";
$('#progressbar').width(progress);
2013-11-07 09:09:40 +00:00
$('#counter')
.text(elapsed_minutes + ":" +
(elapsed_seconds < 10 ? '0' : '') + elapsed_seconds + " / " +
total_minutes + ":" + (total_seconds < 10 ? '0' : '') + total_seconds);
2013-12-03 11:48:52 +00:00
$('#salamisandwich > tbody > tr').removeClass('active').css("font-weight", "");
$('#salamisandwich > tbody > tr[trackid='+obj.data.currentsongid+']').addClass('active').css("font-weight", "bold");
2013-11-07 09:09:40 +00:00
if(obj.data.random)
$('#btnrandom').addClass("active")
else
$('#btnrandom').removeClass("active");
if(obj.data.consume)
$('#btnconsume').addClass("active")
else
$('#btnconsume').removeClass("active");
if(obj.data.single)
$('#btnsingle').addClass("active")
else
$('#btnsingle').removeClass("active");
if(obj.data.repeat)
$('#btnrepeat').addClass("active")
else
$('#btnrepeat').removeClass("active");
if(last_state && (obj.data.state !== last_state.data.state))
updatePlayIcon(obj.data.state);
if(last_state && (obj.data.volume !== last_state.data.volume))
updateVolumeIcon(obj.data.volume);
2013-11-07 09:09:40 +00:00
if(obj.data.elapsedTime <= 1)
socket.send("MPD_API_GET_TRACK_INFO");
2013-11-09 02:02:49 +00:00
$('#alert').addClass("hide");
last_state = obj;
break;
case "disconnected":
2013-11-07 09:09:40 +00:00
$('#alert')
.text("Server lost connection to MPD Host.")
.removeClass("hide alert-info")
.addClass("alert-danger");
break;
case "current_song":
$('#currenttrack').text(" " + obj.data.title);
2013-11-07 16:40:22 +00:00
if(obj.data.album)
$('#album').text(obj.data.album);
if(obj.data.artist)
$('#artist').text(obj.data.artist);
default:
break;
}
}
socket.onclose = function(){
console.log("Disconnected");
var seconds = 5;
var tm = setInterval(disconnectAlert,1000);
function disconnectAlert() {
$('#alert')
.text("Connection to MPD lost, retrying in " + seconds + " seconds")
.removeClass("hide alert-info")
.addClass("alert-danger");
if(seconds-- <= 0) {
webSocketConnect();
seconds = 5;
$('#alert').addClass("hide");
clearInterval(tm);
}
}
}
} catch(exception) {
alert('<p>Error' + exception);
2013-11-04 17:18:38 +00:00
}
}
2013-11-04 23:17:28 +00:00
2013-11-04 17:18:38 +00:00
function get_appropriate_ws_url()
{
var pcol;
var u = document.URL;
/*
/* We open the websocket encrypted if this page came on an
/* https:// url itself, otherwise unencrypted
/*/
2013-11-04 17:18:38 +00:00
if (u.substring(0, 5) == "https") {
pcol = "wss://";
u = u.substr(8);
} else {
pcol = "ws://";
if (u.substring(0, 4) == "http")
u = u.substr(7);
}
u = u.split('/');
return pcol + u[0];
}
2013-11-04 23:17:28 +00:00
var updateVolumeIcon = function(volume)
2013-11-04 17:18:38 +00:00
{
$("#volume-icon").removeClass("glyphicon-volume-off");
$("#volume-icon").removeClass("glyphicon-volume-up");
2013-11-04 23:17:28 +00:00
$("#volume-icon").removeClass("glyphicon-volume-down");
2013-11-04 17:18:38 +00:00
2013-11-04 23:17:28 +00:00
if(volume == 0) {
2013-11-04 17:18:38 +00:00
$("#volume-icon").addClass("glyphicon-volume-off");
2013-11-04 23:17:28 +00:00
} else if (volume < 50) {
$("#volume-icon").addClass("glyphicon-volume-down");
2013-11-04 17:18:38 +00:00
} else {
$("#volume-icon").addClass("glyphicon-volume-up");
}
}
2013-11-04 23:17:28 +00:00
var updatePlayIcon = function(state)
{
$("#play-icon").removeClass("glyphicon-play")
.removeClass("glyphicon-pause")
.removeClass("glyphicon-stop");
$('#track-icon').removeClass("glyphicon-play")
.removeClass("glyphicon-pause")
.removeClass("glyphicon-stop");
2013-11-04 23:17:28 +00:00
if(state == 1) {
$("#play-icon").addClass("glyphicon-stop");
$('#track-icon').addClass("glyphicon-stop");
2013-11-04 23:17:28 +00:00
} else if(state == 2) {
$("#play-icon").addClass("glyphicon-pause");
$('#track-icon').addClass("glyphicon-play");
2013-11-04 23:17:28 +00:00
} else {
$("#play-icon").addClass("glyphicon-play");
$('#track-icon').addClass("glyphicon-pause");
2013-11-04 23:17:28 +00:00
}
}
function updateDB()
{
socket.send('MPD_API_UPDATE_DB');
2013-11-04 23:17:28 +00:00
$('#alert')
.text("Updating MPD Database...")
.removeClass("hide alert-danger")
.addClass("alert-info");
2013-11-04 23:17:28 +00:00
setTimeout(function() {
$('#alert').addClass("hide");
}, 5000);
2013-11-04 17:18:38 +00:00
}
2013-11-13 14:30:54 +00:00
function basename(path) {
return path.split('/').reverse()[0];
}
2013-11-07 09:09:40 +00:00
$('#btnrandom').on('click', function (e) {
socket.send("MPD_API_TOGGLE_RANDOM," + ($(this).hasClass('active') ? 0 : 1));
});
$('#btnconsume').on('click', function (e) {
socket.send("MPD_API_TOGGLE_CONSUME," + ($(this).hasClass('active') ? 0 : 1));
});
$('#btnsingle').on('click', function (e) {
socket.send("MPD_API_TOGGLE_SINGLE," + ($(this).hasClass('active') ? 0 : 1));
});
$('#btnrepeat').on('click', function (e) {
socket.send("MPD_API_TOGGLE_REPEAT," + ($(this).hasClass('active') ? 0 : 1));
});