oldhaven/app/assets/javascripts/player.coffee

255 lines
7.6 KiB
CoffeeScript
Raw Normal View History

2011-09-08 03:47:11 +04:00
class window.Player
2011-06-27 22:41:36 +04:00
2011-09-15 15:07:35 +04:00
bar_width: 263
2011-06-27 22:41:36 +04:00
jp: null
scrobbled: false
2011-09-15 12:52:29 +04:00
albums: []
playlist: []
2011-06-27 22:41:36 +04:00
initJplayer: ->
self = this
@jp = $("#jplayer")
@jp.jPlayer
swfPath: "/js"
supplied: "mp3"
cssSelectorAncestor: ""
cssSelector:
2011-06-28 00:43:54 +04:00
play: ".player .play"
pause: ".player .pause"
2011-06-27 22:41:36 +04:00
stop: ""
videoPlay: ""
seekBar: ""
playBar: ""
mute: ""
unmute: ""
volumeBar: ""
volumeBarValue: ""
currentTime: ""
duration: ""
@jp.bind $.jPlayer.event.timeupdate, (e) ->
data = e.jPlayer.status
2011-06-28 00:43:54 +04:00
if not _player.scrobbled and data.currentPercentAbsolute > 50
2011-09-15 15:07:35 +04:00
$obj = $('.playlist li.now')
2011-06-27 22:41:36 +04:00
self.scrobble $obj.attr('data-artist'), $obj.attr('data-album'), $obj.attr('data-track')
2011-06-28 00:43:54 +04:00
_player.scrobbled = true
$('.player .progress .loaded').width(data.seekPercent * self.bar_width / 100)
$('.player .progress .played').width(data.currentPercentAbsolute * self.bar_width / 100)
2011-06-27 22:41:36 +04:00
@jp.bind $.jPlayer.event.ended, (e) ->
next = self.nextTrack()
if not next
$('#jplayer').jPlayer 'clearMedia'
2011-06-28 00:43:54 +04:00
$('.player .now-playing').html 'Nothing left to <strike>lose</strike> play'
$('.player .loaded, .player .played').width 0
2011-09-15 15:07:35 +04:00
$('.playlist li').removeClass 'now'
2011-06-27 22:41:36 +04:00
else
self.setTrack next
false
addTracks: (tracks, autoplay) ->
2011-06-27 22:41:36 +04:00
if not autoplay?
autoplay = false
2011-09-15 13:38:24 +04:00
initial_count = $('.playlist li').length
for item in tracks
2011-09-15 13:09:25 +04:00
$('.playlist').append '
2011-09-15 12:52:29 +04:00
<li id="i' +Math.round(Math.random() * 999999)+ '" data-id="'+item.id+'">
2011-09-15 13:09:25 +04:00
<div class="song-duration">' +item.duration+ '</div>
2011-09-15 12:52:29 +04:00
<div class="drag"></div>
<div class="artist-name">' +item.artist+ '</div>
<div class="song-title">' +item.name+ '</div>
</li>'
2011-09-15 13:09:25 +04:00
_player.playlist.push item
2011-09-15 12:52:29 +04:00
$('.playlist').sortable axis: 'y', handle: '.drag'
2011-09-15 13:38:24 +04:00
if autoplay?
_player.setTrack($('.playlist li').last().attr('id').split('i')[1])
else if initial_count == 0 and not _player.hasTrack()
_player.setTrack($('.playlist 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
2011-06-27 22:41:36 +04:00
setTrack: (id) ->
$obj = $('#i' +id)
2011-09-15 13:38:24 +04:00
track = _player.getTrackInfo $obj.attr('data-id')
query = track.artist+ ' &mdash; ' +track.name
2011-06-27 22:41:36 +04:00
$('.player .loaded, .player .played').width 0
2011-09-15 13:38:24 +04:00
$('.player .now-playing').html query
$('.playlist li').removeClass 'now'
2011-06-27 22:41:36 +04:00
$obj.addClass 'now'
2011-09-15 13:38:24 +04:00
console.log(track)
_vkontakte.loadTracksData track.artist, track.name, track.duration, (url) ->
2011-06-28 00:43:54 +04:00
_player.playSource url
2011-09-15 13:38:24 +04:00
this.updateNowListening track.artist, track.album, track.name
false
2011-06-27 22:41:36 +04:00
2011-09-15 13:38:24 +04:00
getTrackInfo: (id) ->
for track in _player.playlist
if track.id == parseInt(id, 10)
return track
false
2011-06-27 22:41:36 +04:00
hasTrack: ->
if $('#jplayer audio').length > 0
2011-06-29 03:44:39 +04:00
return $('#jplayer audio').attr('src')? and $('#jplayer audio').attr('src') != ''
2011-06-27 22:41:36 +04:00
else if $('#jplayer object').length > 0
$('#jplayer').jPlayer 'play'
true
false
playSource: (url) ->
@scrobbled = false
2011-06-27 22:41:36 +04:00
$('#jplayer').jPlayer 'setMedia', mp3: url
$('#jplayer').jPlayer 'play'
false
2011-06-27 22:41:36 +04:00
nextTrack: (manual) ->
manual = manual?
2011-09-15 15:07:35 +04:00
cnt = $('.playlist li').length
2011-06-27 22:41:36 +04:00
if not this.onShuffle() # Shuffle off
2011-09-15 15:07:35 +04:00
if $('.playlist .now').next().length == 0 # Last track and repeat is on
2011-06-28 00:43:54 +04:00
if _player.onRepeat() or manual # Repeat or manual click
2011-09-15 15:07:35 +04:00
return $('.playlist li').first().attr('id').split('i')[1]
2011-06-27 22:41:36 +04:00
else
false
else
2011-09-15 15:07:35 +04:00
return $('.playlist .now').next().attr('id').split('i')[1]
2011-06-27 22:41:36 +04:00
else if cnt == 1 # Single track in the playlist
2011-09-15 15:07:35 +04:00
return $('.playlist li').first().attr('id').split('i')[1]
2011-06-27 22:41:36 +04:00
else # Shuffle on
while true
rnd = Math.floor(Math.random() * (cnt + .999))
2011-09-15 15:07:35 +04:00
$li = $('.playlist li').eq rnd
2011-06-27 22:41:36 +04:00
if $li.length > 0 and not $li.hasClass 'now'
return $li.attr('id').split('i')[1]
false
prevTrack: ->
2011-09-15 15:07:35 +04:00
cnt = $('.playlist li').length
2011-06-28 00:43:54 +04:00
if not _player.onShuffle() # Shuffle off
2011-09-15 15:07:35 +04:00
if $('.playlist .now').prev().length == 0 # First track in the playlist
return $('.playlist li').last().attr('id').split('i')[1]
2011-06-27 22:41:36 +04:00
else
2011-09-15 15:07:35 +04:00
return $('.playlist .now').prev().attr('id').split('i')[1]
2011-06-27 22:41:36 +04:00
else if cnt == 1 # Single track in the playlist
2011-09-15 15:07:35 +04:00
return $('.playlist li').first().attr('id').split('i')[1]
2011-06-27 22:41:36 +04:00
else # Shuffle on
while true
rnd = Math.floor(Math.random() * (cnt + .999))
2011-09-15 15:07:35 +04:00
$li = $('.playlist li').eq rnd
2011-06-27 22:41:36 +04:00
if $li.length > 0 and not $li.hasClass 'now'
return $li.attr('id').split('i')[1]
false
onShuffle: ->
2011-09-15 15:07:35 +04:00
#return $('#shuffle').hasClass 'active'
2011-06-27 22:41:36 +04:00
onRepeat: ->
2011-09-15 15:07:35 +04:00
#return $('#repeat').hasClass 'active'
2011-06-27 22:41:36 +04:00
updateNowListening: (artist, album, track) ->
2011-06-28 00:43:54 +04:00
if _session.getUser().lastfm_username
_session.query '/lastfm/listening?r=' +Math.random(), artist: artist, album: album, track: track
false
2011-06-27 22:41:36 +04:00
scrobble: (artist, album, track) ->
2011-06-28 00:43:54 +04:00
if _session.getUser().lastfm_username
_session.query '/lastfm/scrobble?r=' +Math.random(), artist: artist, album: album, track: track
false
2011-06-27 22:41:36 +04:00
# Player Controls
2011-09-15 15:07:35 +04:00
$('.player .prev').live 'click', ->
2011-06-28 00:43:54 +04:00
_player.setTrack _player.prevTrack()
2011-06-27 22:41:36 +04:00
false
2011-09-15 15:07:35 +04:00
$('.player .next').live 'click', ->
2011-06-28 00:43:54 +04:00
_player.setTrack _player.nextTrack(true)
2011-06-27 22:41:36 +04:00
false
2011-06-28 00:43:54 +04:00
$('.player .play').live 'click', ->
2011-09-15 15:07:35 +04:00
if $('.playlist li').length > 0 and not _player.hasTrack()
_player.setTrack $('.playlist li').first().attr('id').split('i')[1]
2011-06-27 22:41:36 +04:00
false
2011-06-28 00:43:54 +04:00
$('.player .progress').live 'click', (e) ->
$('#jplayer').jPlayer 'playHead', Math.round((e.offsetX / _player.bar_width) * 100)
2011-06-27 22:41:36 +04:00
false
# Player Additional Controls
$('#repeat, #shuffle').live 'click', ->
$(this).toggleClass 'active'
false
$('#empty-playlist').live 'click', ->
if confirm('Are you sure?')
2011-09-15 15:07:35 +04:00
$('.playlist li').remove()
2011-06-27 22:41:36 +04:00
$('#jplayer').jPlayer 'clearMedia'
2011-06-28 00:43:54 +04:00
$('.player .now-playing').text 'Add some music to playlist'
$('.player .loaded, .player .played').width 0
2011-06-27 22:41:36 +04:00
false
# Playlist Actions
2011-09-15 15:07:35 +04:00
$('.playlist li .fade, .playlist li .duration, .playlist li .remove').live 'mousemove mouseover mouseout', (e) ->
2011-06-29 04:02:42 +04:00
if e.type in ['mouseover', 'mousemove'] and ($(window).width() - e.clientX) < 60
2011-06-27 22:41:36 +04:00
$(this).parent().find('.duration').hide()
$(this).parent().find('.remove').show()
else
$(this).parent().find('.remove').hide()
$(this).parent().find('.duration').show()
false
2011-09-15 15:07:35 +04:00
$('.playlist li .remove').live 'click', ->
2011-06-27 22:41:36 +04:00
$li = $(this).parent().parent()
if $li.hasClass 'now'
$('#jplayer').jPlayer 'clearMedia'
2011-06-28 00:43:54 +04:00
$('.player .now-playing').text '...'
$('.player .loaded, .player .played').width 0
2011-06-27 22:41:36 +04:00
$li.remove()
false
2011-09-15 15:07:35 +04:00
$('.playlist li .title .playtrack').live 'click', ->
_player.setTrack $(this).parent().parent().parent().attr('id').split('i')[1]
2011-06-27 22:41:36 +04:00
false
# Adding To Playlist actions
$('.add-album').live 'click', ->
2011-09-15 12:52:29 +04:00
artist = $('.artist-info h2').text()
album = _player.albums[$(this).attr('data-album-id')]
for item in album.tracks.album
item['artist'] = artist
item['album'] = album.name
_player.addTracks album.tracks.album
2011-06-27 22:41:36 +04:00
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]
2011-06-27 22:41:36 +04:00
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
2011-06-27 22:41:36 +04:00
false