1
0
Fork 0
beathaven/app/assets/javascripts/backbone/models/player.js.coffee

93 lines
2.5 KiB
CoffeeScript
Raw Normal View History

2012-09-01 17:55:01 +00:00
class BeatHaven.Models.Player extends Backbone.Model
playlist_on: false
playlist: null
tracks: null
2012-09-01 22:03:19 +00:00
current_track: null
2012-09-01 22:38:28 +00:00
move_it_mousedown: false
2012-09-01 17:55:01 +00:00
initialize: ->
2012-09-09 23:47:36 +00:00
@playlist = new BeatHaven.Modules.Playlist()
2012-09-01 17:55:01 +00:00
@tracks = new BeatHaven.Collections.Tracklist()
2012-09-09 23:47:36 +00:00
#
# Actions
#
2012-09-01 22:03:19 +00:00
play: (track) ->
unless track?
if @current_track?
@current_track.get("sm_obj").resume()
else
this.play_something()
else
2012-09-01 22:38:28 +00:00
if @current_track? and @current_track == track
@current_track.get("sm_obj").resume()
else
if @current_track?
@current_track.get("sm_obj").stop()
$(".player .progress-bar .bar").css(width: 0)
@current_track = track
@current_track.get("sm_obj").play()
2012-09-01 22:03:19 +00:00
$(".player .controls .play").css(display: "none")
$(".player .controls .pause").css(display: "inline-block")
pause: ->
return false unless @current_track?
@current_track.get("sm_obj").pause()
$(".player .controls .play").css(display: "inline-block")
$(".player .controls .pause").css(display: "none")
2012-09-01 22:38:28 +00:00
seek: (percent) ->
return false unless @current_track?
position = @current_track.get("duration") * 1000 * percent
@current_track.get("sm_obj").setPosition(position)
2012-09-01 22:03:19 +00:00
next: ->
return false unless @current_track?
if @playlist_on
# not implemented
else
2012-09-17 08:25:38 +00:00
node = $(".tracks li.now-playing").neighbour(".tracks li", 1)
return false unless node?
@tracks.get(parseInt($(node).data("id"), 10)).play()
2012-09-01 22:03:19 +00:00
prev: ->
return false unless @current_track?
if @playlist_on
# not implemented
else
2012-09-17 08:25:38 +00:00
node = $(".tracks li.now-playing").neighbour(".tracks li", -1)
return false unless node?
@tracks.get(parseInt($(node).data("id"), 10)).play()
2012-09-01 22:03:19 +00:00
play_something: ->
nodes = $(".artist-page .tracks li[data-id]")
return false unless nodes.length > 0
@tracks.get(parseInt($(nodes[0]).data("id"), 10)).play()
2012-09-09 23:47:36 +00:00
#
# Playlist
#
add_track_to_playlist: (track) ->
@playlist.add(track).render()
remove_track_from_playlist: (track) ->
@playlist.remove(track).render()
#
# Supportive
#
2012-09-01 17:55:01 +00:00
update_title: (params) ->
$(".player .progress-bar .title").html("#{params.artists.join(', ')} — #{params.track}")
2012-09-01 22:03:19 +00:00
update_buffer_bar: (event) ->
2012-09-01 22:38:28 +00:00
# not implemented
2012-09-01 22:03:19 +00:00
false
update_progress_bar: (obj) ->
2012-09-01 22:38:28 +00:00
return false if @move_it_mousedown
2012-09-01 22:03:19 +00:00
percent = obj.position / obj.duration * 100
$(".player .progress-bar .bar").css(width: "#{percent}%")