260 lines
8.4 KiB
CoffeeScript
260 lines
8.4 KiB
CoffeeScript
class window.Player
|
|
|
|
bar_width: 330
|
|
jp: null
|
|
scrobbled: false
|
|
|
|
initJplayer: ->
|
|
self = this
|
|
|
|
@jp = $("#jplayer")
|
|
@jp.jPlayer
|
|
swfPath: "/js"
|
|
supplied: "mp3"
|
|
cssSelectorAncestor: ""
|
|
cssSelector:
|
|
play: ".player .play"
|
|
pause: ".player .pause"
|
|
stop: ""
|
|
videoPlay: ""
|
|
seekBar: ""
|
|
playBar: ""
|
|
mute: ""
|
|
unmute: ""
|
|
volumeBar: ""
|
|
volumeBarValue: ""
|
|
currentTime: ""
|
|
duration: ""
|
|
|
|
@jp.bind $.jPlayer.event.timeupdate, (e) ->
|
|
data = e.jPlayer.status
|
|
if not _player.scrobbled and 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)
|
|
$('.player .progress .played').width(data.currentPercentAbsolute * self.bar_width / 100)
|
|
|
|
@jp.bind $.jPlayer.event.ended, (e) ->
|
|
next = self.nextTrack()
|
|
if not next
|
|
$('#jplayer').jPlayer 'clearMedia'
|
|
$('.player .now-playing').html 'Nothing left to <strike>lose</strike> play'
|
|
$('.player .loaded, .player .played').width 0
|
|
$('.playlist-tracks li').removeClass 'now'
|
|
else
|
|
self.setTrack next
|
|
false
|
|
|
|
addTracks: (tracks, autoplay) ->
|
|
if not autoplay?
|
|
autoplay = false
|
|
initial_count = $('.playlist-tracks li').length
|
|
for item in tracks
|
|
$('.playlist-tracks').append '
|
|
<li id="i' +Math.round(Math.random() * 999999)+ '" data-id="'+item.id+'" data-artist="'+item.artist.trim()+'" data-album="'+item.album.trim()+'" data-track="'+item.name.trim()+'" data-length="'+item.length+'">
|
|
<div class="item">
|
|
<div class="fade"></div>
|
|
<div class="dragbox"></div>
|
|
<span class="title">
|
|
<span class="data artist" title="Open ' +item.artist.htmlsafe()+ '\'s page">' +item.artist+ '</span>
|
|
—
|
|
<span class="playtrack" title="Play ' +item.name.htmlsafe()+ ' by ' +item.artist.htmlsafe()+ '">' +item.name+ '</span>
|
|
</span>
|
|
<span class="duration">' +item.length+ '</span>
|
|
<div class="remove">remove</div>
|
|
</div>
|
|
</li>'
|
|
|
|
$('.playlist').html($('.playlist-tracks')).scrollbar()
|
|
$('.playlist-tracks').sortable axis: 'y', handle: '.dragbox'
|
|
|
|
if autoplay
|
|
_player.setTrack($('.playlist-tracks li').last().attr('id').split('i')[1])
|
|
else if initial_count == 0 and not _player.hasTrack()
|
|
_player.setTrack($('.playlist-tracks li').first().attr('id').split('i')[1])
|
|
false
|
|
|
|
getDataFromLi: (obj) ->
|
|
id = $(obj).attr 'data-id'
|
|
track_name = $(obj).find('.trackname').html()
|
|
length = $(obj).find('.length').html()
|
|
id: id, name: track_name, length: length
|
|
|
|
setTrack: (id) ->
|
|
$obj = $('#i' +id)
|
|
query = $obj.attr('data-artist')+ ' — ' +$obj.attr('data-track')
|
|
|
|
$('.player .loaded, .player .played').width 0
|
|
$('.player .now-playing').html query+'<div class="fade"></div>'
|
|
$('.playlist-tracks li').removeClass 'now'
|
|
$obj.addClass 'now'
|
|
$('.tracklist li').removeClass 'now'
|
|
$('.tracklist li[data-id="'+$obj.attr('data-id')+'"]').addClass 'now'
|
|
|
|
_vkontakte.loadTracksData $obj.attr('data-artist'), $obj.attr('data-track'), $obj.attr('data-length'), (url) ->
|
|
_player.playSource url
|
|
this.updateNowListening $obj.attr('data-artist'), $obj.attr('data-album'), $obj.attr('data-track')
|
|
false
|
|
|
|
hasTrack: ->
|
|
if $('#jplayer audio').length > 0
|
|
return $('#jplayer audio').attr('src')? and $('#jplayer audio').attr('src') != ''
|
|
else if $('#jplayer object').length > 0
|
|
$('#jplayer').jPlayer 'play'
|
|
true
|
|
false
|
|
|
|
playSource: (url) ->
|
|
@scrobbled = false
|
|
$('#jplayer').jPlayer 'setMedia', mp3: url
|
|
$('#jplayer').jPlayer 'play'
|
|
false
|
|
|
|
nextTrack: (manual) ->
|
|
manual = manual?
|
|
cnt = $('.playlist-tracks li').length
|
|
if not this.onShuffle() # Shuffle off
|
|
if $('.playlist-tracks .now').next().length == 0 # Last track and repeat is on
|
|
if _player.onRepeat() or manual # Repeat or manual click
|
|
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 # Single track in the playlist
|
|
return $('.playlist-tracks li').first().attr('id').split('i')[1]
|
|
else # Shuffle on
|
|
while true
|
|
rnd = Math.floor(Math.random() * (cnt + .999))
|
|
$li = $('.playlist-tracks li').eq rnd
|
|
if $li.length > 0 and not $li.hasClass 'now'
|
|
return $li.attr('id').split('i')[1]
|
|
false
|
|
|
|
prevTrack: ->
|
|
cnt = $('.playlist-tracks li').length
|
|
if not _player.onShuffle() # Shuffle off
|
|
if $('.playlist-tracks .now').prev().length == 0 # 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]
|
|
else if cnt == 1 # Single track in the playlist
|
|
return $('.playlist-tracks li').first().attr('id').split('i')[1]
|
|
else # Shuffle on
|
|
while true
|
|
rnd = Math.floor(Math.random() * (cnt + .999))
|
|
$li = $('.playlist-tracks li').eq rnd
|
|
if $li.length > 0 and not $li.hasClass 'now'
|
|
return $li.attr('id').split('i')[1]
|
|
false
|
|
|
|
onShuffle: ->
|
|
return $('#shuffle').hasClass 'active'
|
|
|
|
onRepeat: ->
|
|
return $('#repeat').hasClass 'active'
|
|
|
|
updateNowListening: (artist, album, track) ->
|
|
if _session.getUser().lastfm_username
|
|
_session.query '/lastfm/listening?r=' +Math.random(), artist: artist, album: album, track: track
|
|
false
|
|
|
|
scrobble: (artist, album, track) ->
|
|
if _session.getUser().lastfm_username
|
|
_session.query '/lastfm/scrobble?r=' +Math.random(), artist: artist, album: album, track: track
|
|
false
|
|
|
|
|
|
# Player Controls
|
|
|
|
$('.player .controls .prev').live 'click', ->
|
|
_player.setTrack _player.prevTrack()
|
|
false
|
|
|
|
$('.player .controls .next').live 'click', ->
|
|
_player.setTrack _player.nextTrack(true)
|
|
false
|
|
|
|
$('.player .play').live 'click', ->
|
|
if $('.playlist-tracks li').length > 0 and not _player.hasTrack()
|
|
_player.setTrack $('.playlist-tracks li').first().attr('id').split('i')[1]
|
|
false
|
|
|
|
$('.player .progress').live 'click', (e) ->
|
|
$('#jplayer').jPlayer 'playHead', Math.round((e.offsetX / _player.bar_width) * 100)
|
|
false
|
|
|
|
# Player Additional Controls
|
|
|
|
$('#repeat, #shuffle').live 'click', ->
|
|
$(this).toggleClass 'active'
|
|
false
|
|
|
|
$('#empty-playlist').live 'click', ->
|
|
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
|
|
false
|
|
|
|
# Playlist Actions
|
|
|
|
$('.playlist-tracks li .fade, .playlist-tracks li .duration, .playlist-tracks li .remove').live 'mousemove mouseover mouseout', (e) ->
|
|
if e.type in ['mouseover', 'mousemove'] and ($(window).width() - e.clientX) < 60
|
|
$(this).parent().find('.duration').hide()
|
|
$(this).parent().find('.remove').show()
|
|
else
|
|
$(this).parent().find('.remove').hide()
|
|
$(this).parent().find('.duration').show()
|
|
false
|
|
|
|
$('.playlist-tracks li .remove').live 'click', ->
|
|
$li = $(this).parent().parent()
|
|
if $li.hasClass 'now'
|
|
$('#jplayer').jPlayer 'clearMedia'
|
|
$('.player .now-playing').text '...'
|
|
$('.player .loaded, .player .played').width 0
|
|
$li.remove()
|
|
false
|
|
|
|
$('.playlist-tracks li .title .playtrack').live 'click', ->
|
|
_player.setTrack $(this).parent().parent().parent().attr('id').split('i')[1]
|
|
false
|
|
|
|
# Adding To Playlist actions
|
|
|
|
$('.add-album').live 'click', ->
|
|
artist = $('.artist-info .name').html()
|
|
album = $(this).parent().parent().parent().find('h2.name').text().replace /\s\([\d]{4}\)$/, ''
|
|
tracks = []
|
|
for item in $(this).parent().parent().parent().find('.tracklist li')
|
|
track = _player.getDataFromLi item
|
|
track['artist'] = artist
|
|
track['album'] = album
|
|
tracks.push track
|
|
_player.addTracks tracks
|
|
false
|
|
|
|
$('.add-track').live 'click', ->
|
|
track = _player.getDataFromLi $(this).parent()
|
|
track['artist'] = $('.artist-info .name').html()
|
|
track['album'] = $(this).parent().parent().parent().parent().find('h2.name').text().replace /\s\([\d]{4}\)$/, ''
|
|
_player.addTracks [track]
|
|
false
|
|
|
|
$('.tracklist li').live 'mouseover mouseout', (e) ->
|
|
if e.type == 'mouseover'
|
|
$(this).find('.add-track').show()
|
|
else
|
|
$(this).find('.add-track').hide()
|
|
false
|
|
|
|
$('.tracklist li').live 'click', (e) ->
|
|
track = _player.getDataFromLi this
|
|
track['artist'] = $('.artist-info .name').html()
|
|
track['album'] = $(this).parent().parent().parent().find('h2.name').text().replace /\s\([\d]{4}\)$/, ''
|
|
_player.addTracks [track], true
|
|
false
|