The player is playing!
This commit is contained in:
@@ -2,10 +2,63 @@ class BeatHaven.Models.Player extends Backbone.Model
|
||||
playlist_on: false
|
||||
playlist: null
|
||||
tracks: null
|
||||
current_track: null
|
||||
|
||||
initialize: ->
|
||||
@playlist = new BeatHaven.Collections.Tracklist()
|
||||
@tracks = new BeatHaven.Collections.Tracklist()
|
||||
|
||||
play: (track) ->
|
||||
unless track?
|
||||
if @current_track?
|
||||
@current_track.get("sm_obj").resume()
|
||||
else
|
||||
this.play_something()
|
||||
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()
|
||||
$(".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")
|
||||
|
||||
next: ->
|
||||
return false unless @current_track?
|
||||
if @playlist_on
|
||||
# not implemented
|
||||
else
|
||||
nodes = @current_track.node().next()
|
||||
return false unless nodes.length == 1
|
||||
@tracks.get(parseInt($(nodes[0]).data("id"), 10)).play()
|
||||
|
||||
prev: ->
|
||||
return false unless @current_track?
|
||||
if @playlist_on
|
||||
# not implemented
|
||||
else
|
||||
nodes = @current_track.node().prev()
|
||||
return false unless nodes.length == 1
|
||||
@tracks.get(parseInt($(nodes[0]).data("id"), 10)).play()
|
||||
|
||||
play_something: ->
|
||||
nodes = $(".artist-page .tracks li[data-id]")
|
||||
return false unless nodes.length > 0
|
||||
@tracks.get(parseInt($(nodes[0]).data("id"), 10)).play()
|
||||
|
||||
update_title: (params) ->
|
||||
$(".player .progress-bar .title").html("#{params.artists.join(', ')} — #{params.track}")
|
||||
|
||||
update_buffer_bar: (event) ->
|
||||
false
|
||||
|
||||
update_progress_bar: (obj) ->
|
||||
percent = obj.position / obj.duration * 100
|
||||
$(".player .progress-bar .bar").css(width: "#{percent}%")
|
||||
|
||||
|
||||
@@ -1,7 +1,43 @@
|
||||
class BeatHaven.Models.Track extends Backbone.Model
|
||||
|
||||
play: ->
|
||||
BeatHaven.player.update_title(
|
||||
if @.get("sm_obj")?
|
||||
this.start()
|
||||
else
|
||||
this.find_and_start()
|
||||
|
||||
start: ->
|
||||
BH.Player.update_title(
|
||||
artists: @.get("artists")
|
||||
track: @.get("title")
|
||||
)
|
||||
unless @.get("sm_obj")?
|
||||
this.add_to_library(autoload: true, autoplay: false)
|
||||
BH.Player.play(this)
|
||||
$(".artist-page .tracks li[data-id]").removeClass("now-playing")
|
||||
this.node().addClass("now-playing")
|
||||
|
||||
find_and_start: ->
|
||||
self = this
|
||||
BH.VK.Music.search @.get("artists")[0], @.get("title"), @.get("length"), (url) ->
|
||||
self.set(url: url)
|
||||
self.start()
|
||||
|
||||
add_to_library: (params) ->
|
||||
obj = soundManager.createSound
|
||||
id: @.get("id")
|
||||
url: @.get("url")
|
||||
autoLoad: params.autoload
|
||||
autoPlay: params.autoplay
|
||||
whileloading: ->
|
||||
BH.Player.update_buffer_bar(this)
|
||||
whileplaying: ->
|
||||
BH.Player.update_progress_bar(this)
|
||||
onfinish: ->
|
||||
BH.Player.next()
|
||||
ondataerror: ->
|
||||
BH.Player.next()
|
||||
@.set("sm_obj", obj)
|
||||
|
||||
node: ->
|
||||
$(".artist-page .tracks li[data-id='#{@.get("id")}']")
|
||||
|
||||
@@ -0,0 +1,27 @@
|
||||
class BeatHaven.Models.User extends Backbone.Model
|
||||
|
||||
auth: ->
|
||||
BH.log "Authenticating user ..."
|
||||
this.query "/api/session/auth", {}, (response) ->
|
||||
if response.error?
|
||||
# report error
|
||||
else
|
||||
BH.User.set(JSON.parse(response.user))
|
||||
if response.is_newbie
|
||||
BH.log "Requesting user tracks from Vkontakte ..."
|
||||
# BH.VK.set_favorites()
|
||||
|
||||
query: (path, params, callback) ->
|
||||
query_params = $.extend {}, @.get("vk_session"), params
|
||||
query_params.authenticity_token = $('meta[name="csrf-token"]').attr("content")
|
||||
$.post path, query_params, callback
|
||||
false
|
||||
|
||||
set_favorites: (tracks) ->
|
||||
BH.log tracks
|
||||
BH.log "Sending your Vkontakte media collection to BeatHaven ..."
|
||||
this.query "/user/set_first_favorites", tracks: tracks, (response) ->
|
||||
if response.error?
|
||||
BH.log "Got error: #{response.error}"
|
||||
else
|
||||
BH.log "We believe your favorite artists are #{response.join(', ')}"
|
||||
@@ -0,0 +1,43 @@
|
||||
class BeatHaven.Models.VK extends Backbone.Model
|
||||
|
||||
session_length: 3600 # seconds
|
||||
Music: null
|
||||
|
||||
popup: ->
|
||||
alert(1)
|
||||
|
||||
init: ->
|
||||
@Music = new VkMusic()
|
||||
BH.log "Initializing Vkontakte API ..."
|
||||
window.VK.init(apiId: VK_APP_ID)
|
||||
BH.VK.auth()
|
||||
# VK.Widgets.Like("vk-like", {type: "mini", height: 20, pageUrl: "http://beathaven.org/", text: "Like"})
|
||||
|
||||
auth: ->
|
||||
BH.log "Requesting new Vkontakte session ..."
|
||||
window.VK.Auth.getLoginStatus (response) ->
|
||||
BH.VK.auth_info(response)
|
||||
false
|
||||
, 8
|
||||
false
|
||||
|
||||
auth_info: (response) ->
|
||||
if typeof response isnt "undefined" and response.session?
|
||||
BH.User.set(vk_session: response.session)
|
||||
BH.User.auth()
|
||||
|
||||
if response.session.expire?
|
||||
expire_in = response.session.expire * 1000 - new Date().getTime()
|
||||
# time is an illusion...
|
||||
expire_in = @session_length * 1000
|
||||
BH.log "Session will expire in #{Math.round(expire_in / 1000)} seconds"
|
||||
setTimeout ->
|
||||
BH.log "Session expired"
|
||||
BH.VK.auth()
|
||||
, expire_in + 1000
|
||||
else
|
||||
BH.log "Not authorized"
|
||||
|
||||
set_favorites: ->
|
||||
window.VK.Api.call "audio.get", uid: BH.User.vk_id(), (response) ->
|
||||
BH.User.set_favorites(response.response)
|
||||
Reference in New Issue
Block a user