1
0
Fork 0
oldhaven/public/javascripts/coffee/player.js

276 lines
9.5 KiB
JavaScript

var Player;
Player = (function() {
function Player() {}
Player.prototype.bar_width = 330;
Player.prototype.jp = null;
Player.prototype.scrobbled = false;
Player.prototype.initJplayer = function() {
var self;
self = this;
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) {
var $obj, data;
data = e.jPlayer.status;
if (!_player.scrobbled && data.currentPercentAbsolute > 50) {
$obj = $('.playlist-tracks li.now');
self.scrobble($obj.attr('data-artist'), $obj.attr('data-album'), $obj.attr('data-track'));
_player.scrobbled = true;
}
$('.player .progress .loaded').width(data.seekPercent * self.bar_width / 100);
return $('.player .progress .played').width(data.currentPercentAbsolute * self.bar_width / 100);
});
this.jp.bind($.jPlayer.event.ended, function(e) {
var next;
next = self.nextTrack();
if (!next) {
$('#jplayer').jPlayer('clearMedia');
$('.player .now-playing').html('Nothing left to <strike>lose</strike> play');
$('.player .loaded, .player .played').width(0);
return $('.playlist-tracks li').removeClass('now');
} else {
return self.setTrack(next);
}
});
return false;
};
Player.prototype.addTrack = function(artist, album, track, length, autoplay) {
var initial_count;
if (!(autoplay != null)) {
autoplay = false;
}
initial_count = $('.playlist-tracks li').length;
$('.playlist-tracks').append('\
<li id="i' + Math.round(Math.random() * 999999) + '" data-artist="' + artist.trim() + '" data-album="' + album.trim() + '" data-track="' + track.trim() + '" data-length="' + length + '">\
<div class="item">\
<div class="fade"></div>\
<span class="title" title="' + artist.htmlsafe() + " &mdash; " + track.htmlsafe() + '">' + 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) {
return _player.setTrack($('.playlist-tracks li').last().attr('id').split('i')[1]);
} else if (initial_count === 0 && !_player.hasTrack()) {
return _player.setTrack($('.playlist-tracks li').first().attr('id').split('i')[1]);
}
};
Player.prototype.setTrack = function(id) {
var $obj, query;
$obj = $('#i' + id);
query = $obj.attr('data-artist') + ' &mdash; ' + $obj.attr('data-track');
$('.player .now-playing').html(query + '<div class="fade"></div>');
$('.playlist-tracks li').removeClass('now');
$obj.addClass('now');
$('.player .loaded, .player .played').width(0);
_vkontakte.loadTracksData($obj.attr('data-artist'), $obj.attr('data-track'), $obj.attr('data-length'), function(url) {
return _player.playSource(url);
});
return this.updateNowListening($obj.attr('data-artist'), $obj.attr('data-album'), $obj.attr('data-track'));
};
Player.prototype.hasTrack = function() {
if ($('#jplayer audio').length > 0) {
return $('#jplayer audio').attr('src') != null;
} else if ($('#jplayer object').length > 0) {
$('#jplayer').jPlayer('play');
true;
}
return false;
};
Player.prototype.playSource = function(url) {
$('#jplayer').jPlayer('setMedia', {
mp3: url
});
$('#jplayer').jPlayer('play');
return this.scrobbled = false;
};
Player.prototype.nextTrack = function(manual) {
var $li, cnt, rnd;
manual = manual != null;
cnt = $('.playlist-tracks li').length;
if (!this.onShuffle()) {
if ($('.playlist-tracks .now').next().length === 0) {
if (_player.onRepeat() || manual) {
return $('.playlist-tracks li').first().attr('id').split('i')[1];
} else {
false;
}
} else {
return $('.playlist-tracks .now').next().attr('id').split('i')[1];
}
} else if (cnt === 1) {
return $('.playlist-tracks li').first().attr('id').split('i')[1];
} else {
while (true) {
rnd = Math.floor(Math.random() * (cnt + .999));
$li = $('.playlist-tracks li').eq(rnd);
if ($li.length > 0 && !$li.hasClass('now')) {
return $li.attr('id').split('i')[1];
}
}
}
return false;
};
Player.prototype.prevTrack = function() {
var $li, cnt, rnd;
cnt = $('.playlist-tracks li').length;
if (!_player.onShuffle()) {
if ($('.playlist-tracks .now').prev().length === 0) {
return $('.playlist-tracks li').last().attr('id').split('i')[1];
} else {
return $('.playlist-tracks .now').prev().attr('id').split('i')[1];
}
} else if (cnt === 1) {
return $('.playlist-tracks li').first().attr('id').split('i')[1];
} else {
while (true) {
rnd = Math.floor(Math.random() * (cnt + .999));
$li = $('.playlist-tracks li').eq(rnd);
if ($li.length > 0 && !$li.hasClass('now')) {
return $li.attr('id').split('i')[1];
}
}
}
return false;
};
Player.prototype.onShuffle = function() {
return $('#shuffle').hasClass('active');
};
Player.prototype.onRepeat = function() {
return $('#repeat').hasClass('active');
};
Player.prototype.updateNowListening = function(artist, album, track) {
if (_session.getUser().lastfm_username) {
return _session.query('/lastfm/listening?r=' + Math.random(), {
artist: artist,
album: album,
track: track
});
}
};
Player.prototype.scrobble = function(artist, album, track) {
if (_session.getUser().lastfm_username) {
return _session.query('/lastfm/scrobble?r=' + Math.random(), {
artist: artist,
album: album,
track: track
});
}
};
return Player;
})();
$('.player .controls .prev').live('click', function() {
_player.setTrack(_player.prevTrack());
return false;
});
$('.player .controls .next').live('click', function() {
_player.setTrack(_player.nextTrack(true));
return false;
});
$('.player .play').live('click', function() {
if ($('.playlist-tracks li').length > 0 && !_player.hasTrack()) {
_player.setTrack($('.playlist-tracks li').first().attr('id').split('i')[1]);
}
return false;
});
$('.player .progress').live('click', function(e) {
$('#jplayer').jPlayer('playHead', Math.round((e.offsetX / _player.bar_width) * 100));
return false;
});
$('#repeat, #shuffle').live('click', function() {
$(this).toggleClass('active');
return false;
});
$('#empty-playlist').live('click', function() {
if (confirm('Are you sure?')) {
$('.playlist-tracks li').remove();
$('#jplayer').jPlayer('clearMedia');
$('.player .now-playing').text('Add some music to playlist');
$('.player .loaded, .player .played').width(0);
}
return false;
});
$('.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();
}
return false;
});
$('.playlist-tracks li .remove').live('click', function() {
var $li;
$li = $(this).parent().parent();
if ($li.hasClass('now')) {
$('#jplayer').jPlayer('clearMedia');
$('.player .now-playing').text('...');
$('.player .loaded, .player .played').width(0);
}
$li.remove();
return false;
});
$('.playlist-tracks li').live('dblclick', function() {
_player.setTrack($(this).attr('id').split('i')[1]);
return false;
});
$('.add-album').live('click', function() {
var album, artist, item, length, track_name, _i, _len, _ref;
artist = $('.artist-info .name').html();
album = $(this).parent().parent().parent().find('h2.name').text().replace(/\s\([\d]{4}\)$/, '');
_ref = $(this).parent().parent().parent().find('.tracklist li');
for (_i = 0, _len = _ref.length; _i < _len; _i++) {
item = _ref[_i];
track_name = $(item).find('.trackname').html();
length = $(item).find('.length').html();
_player.addTrack(artist, album, track_name, length);
}
return false;
});
$('.add-track').live('click', function() {
var album, artist, length, track_name;
artist = $('.artist-info .name').html();
album = $(this).parent().parent().parent().parent().find('h2.name').text().replace(/\s\([\d]{4}\)$/, '');
track_name = $(this).parent().find('.trackname').html();
length = $(this).parent().find('.length').html();
_player.addTrack(artist, album, track_name, length);
return false;
});
$('.tracklist li').live('mouseover mouseout', function(e) {
if (e.type === 'mouseover') {
$(this).find('.add-track').show();
} else {
$(this).find('.add-track').hide();
}
return false;
});
$('.tracklist li').live('dblclick', function(e) {
var album, artist, length, track_name;
artist = $('.artist-info .name').html();
album = $(this).parent().parent().parent().find('h2.name').text().replace(/\s\([\d]{4}\)$/, '');
track_name = $(this).find('.trackname').html();
length = $(this).find('.length').html();
_player.addTrack(artist, album, track_name, length, true);
return false;
});