1
0
Fork 0
oldhaven/public/js/beathaven/player.js

199 lines
6.3 KiB
JavaScript

var Player = {
bar_width: 330,
jp: null,
scrobbled: false,
initJplayer: function() {
this.jp = $("#jplayer");
this.jp.jPlayer({
swfPath: "/js",
supplied: "mp3",
cssSelectorAncestor: "",
cssSelector: {
play: "#player .play",
pause: "#player .pause",
stop: "",
videoPlay: "",
seekBar: "",
playBar: "",
mute: "",
unmute: "",
volumeBar: "",
volumeBarValue: "",
currentTime: "",
duration: ""
}
});
this.jp.bind($.jPlayer.event.timeupdate, function(e){
data = e.jPlayer.status;
if (!Player.scrobbled && data.currentPercentAbsolute > 50) {
var $obj = $('.playlist-tracks li.now');
Player.scrobble($obj.attr('data-artist'), $obj.attr('data-album'), $obj.attr('data-track'));
Player.scrobbled = true;
}
$('#player .progress .loaded').width(data.seekPercent * Player.bar_width / 100);
$('#player .progress .played').width(data.currentPercentAbsolute * Player.bar_width / 100);
});
this.jp.bind($.jPlayer.event.ended, function(e){
Player.setTrack(Player.nextTrack());
});
},
addTrack: function(artist, album, track, length, autoplay) {
if (typeof autoplay === 'undefined') {
autoplay = false;
}
var initial_count = $('.playlist-tracks li').length;
$('.playlist-tracks').append('\
<li id="i'+ Math.round(Math.random() * 999999) +'" data-artist="'+ artist +'" data-album="'+ album +'" data-track="'+ track +'" data-length="'+ length +'">\
<div class="item">\
<div class="fade"></div>\
<span class="title">'+ artist +' &mdash; '+ track +'</span>\
<span class="duration">'+ length +'</span>\
<div class="remove">remove</div>\
</div>\
</li>\
');
$('#playlist').html($('.playlist-tracks')).scrollbar();
$('.playlist-tracks').sortable();
if (autoplay) {
Player.setTrack($('.playlist-tracks li').last().attr('id').split('i')[1]);
} else if (initial_count === 0 && !Player.hasTrack()) {
Player.setTrack($('.playlist-tracks li').first().attr('id').split('i')[1]);
}
},
setTrack: function(id) {
var $obj = $('#i'+ id);
var query = $obj.attr('data-artist') +'&mdash;'+ $obj.attr('data-track');
$('#player .now-playing').html(query);
$('.playlist-tracks li').removeClass('now');
$obj.addClass('now');
$('#player .loaded, #player .played').width(0);
loadTracksData($obj.attr('data-artist'), $obj.attr('data-track'), $obj.attr('data-length'), Player.playSource);
Player.updateNowListening($obj.attr('data-artist'), $obj.attr('data-album'), $obj.attr('data-track'));
},
hasTrack: function() {
if ($('#jplayer audio').length > 0) {
return typeof $('#jplayer audio').attr('src') !== 'undefined';
} else if ($('#jplayer audio').length > 0) {
$('#jplayer').jPlayer('play');
return true;
}
return false;
},
playSource: function(url) {
$('#jplayer').jPlayer('setMedia', {mp3: url}).jPlayer('play');
Player.scrobbled = false;
},
nextTrack: function() {
if (true) { // Shuffle off
if ($('.playlist-tracks .now').next().length == 0 && true) { // Last track in the playlist and repeat is on
return $('.playlist-tracks li').first().attr('id').split('i')[1];
} else {
return $('.playlist-tracks .now').next().attr('id').split('i')[1];
}
}
},
prevTrack: function() {
if (true) { // Shuffle off
if ($('.playlist-tracks .now').prev().length == 0 && true) { // First track in the playlist
return $('.playlist-tracks li').last().attr('id').split('i')[1];
} else {
return $('.playlist-tracks .now').prev().attr('id').split('i')[1];
}
}
},
updateNowListening: function(artist, album, track) {
Session.query('/lastfm/listening?r='+ Math.random(), {'artist': artist, 'album': album, 'track': track})
},
scrobble: function(artist, album, track) {
Session.query('/lastfm/scrobble?r='+ Math.random(), {'artist': artist, 'album': album, 'track': track})
}
}
$(function(){
Player.initJplayer();
});
$('#player .controls .prev').live('click', function(){
Player.setTrack(Player.prevTrack());
});
$('#player .controls .next').live('click', function(){
Player.setTrack(Player.nextTrack());
});
$('.add-album').live('click', function() {
var artist = $('.artist-info .name').html();
var album = $(this).parent().parent().parent().find('h2.name').text().replace(/\s\([\d]{4}\)$/, '');
$(this).parent().parent().parent().find('.tracklist li').each(function(i, item){
var track_name = $(item).find('.trackname').html();
var length = $(item).find('.length').html();
Player.addTrack(artist, album, track_name, length);
});
})
$('.add-track').live('click', function(){
var artist = $('.artist-info .name').html();
var album = $(this).parent().parent().parent().parent().find('h2.name').text().replace(/\s\([\d]{4}\)$/, '');
var track_name = $(this).parent().find('.trackname').html();
var length = $(this).parent().find('.length').html();
Player.addTrack(artist, album, track_name, length);
});
$('#player .play').live('click', function(){
if ($('.playlist-tracks li').length > 0 && !Player.hasTrack()) {
Player.setTrack($('.playlist-tracks li').first().attr('id').split('i')[1]);
}
});
$('.tracklist li').live('mouseover mouseout', function(e){
if (e.type == 'mouseover') {
$(this).find('.add-track').show();
} else {
$(this).find('.add-track').hide();
}
}).live('dblclick', function(e){
var artist = $('.artist-info .name').html();
var album = $(this).parent().parent().parent().find('h2.name').text().replace(/\s\([\d]{4}\)$/, '');
var track_name = $(this).find('.trackname').html();
var length = $(this).find('.length').html();
Player.addTrack(artist, album, track_name, length, true);
});
$('.playlist-tracks li .fade, .playlist-tracks li .duration, .playlist-tracks li .remove').live('mouseover mouseout', function(e){
if (e.type == 'mouseover') {
$(this).parent().find('.duration').hide();
$(this).parent().find('.remove').show();
} else {
$(this).parent().find('.remove').hide();
$(this).parent().find('.duration').show();
}
});
$('.playlist-tracks li .remove').live('click', function(){
var $li = $(this).parent().parent();
if ($li.hasClass('now')) {
Player.reset();
}
$li.remove();
});
$('.playlist-tracks li').live('dblclick', function(){
Player.setTrack($(this).attr('id').split('i')[1]);
});
$('#player .progress').live('click', function(e){
$('#jplayer').jPlayer('playHead', Math.round((e.offsetX / Player.bar_width) * 100));
});