From e2e2868309055cefee925b7fd017ae24e1d8d0b7 Mon Sep 17 00:00:00 2001 From: Gregory Eremin Date: Sun, 2 Sep 2012 02:38:28 +0400 Subject: [PATCH] Player seek feature --- .../backbone/models/player.js.coffee | 21 ++++++++++++++----- .../backbone/models/track.js.coffee | 6 +++++- .../javascripts/bindings/player.js.coffee | 13 ++++++++++++ .../javascripts/bindings/track.js.coffee | 4 ++++ app/assets/stylesheets/album-track.css.scss | 12 +++++++++++ 5 files changed, 50 insertions(+), 6 deletions(-) diff --git a/app/assets/javascripts/backbone/models/player.js.coffee b/app/assets/javascripts/backbone/models/player.js.coffee index d250e73..fed655d 100644 --- a/app/assets/javascripts/backbone/models/player.js.coffee +++ b/app/assets/javascripts/backbone/models/player.js.coffee @@ -3,6 +3,7 @@ class BeatHaven.Models.Player extends Backbone.Model playlist: null tracks: null current_track: null + move_it_mousedown: false initialize: -> @playlist = new BeatHaven.Collections.Tracklist() @@ -15,11 +16,14 @@ class BeatHaven.Models.Player extends Backbone.Model 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() + 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() $(".player .controls .play").css(display: "none") $(".player .controls .pause").css(display: "inline-block") @@ -29,6 +33,11 @@ class BeatHaven.Models.Player extends Backbone.Model $(".player .controls .play").css(display: "inline-block") $(".player .controls .pause").css(display: "none") + seek: (percent) -> + return false unless @current_track? + position = @current_track.get("duration") * 1000 * percent + @current_track.get("sm_obj").setPosition(position) + next: -> return false unless @current_track? if @playlist_on @@ -56,9 +65,11 @@ class BeatHaven.Models.Player extends Backbone.Model $(".player .progress-bar .title").html("#{params.artists.join(', ')} — #{params.track}") update_buffer_bar: (event) -> + # not implemented false update_progress_bar: (obj) -> + return false if @move_it_mousedown percent = obj.position / obj.duration * 100 $(".player .progress-bar .bar").css(width: "#{percent}%") diff --git a/app/assets/javascripts/backbone/models/track.js.coffee b/app/assets/javascripts/backbone/models/track.js.coffee index 3cf9be6..9fa24cc 100644 --- a/app/assets/javascripts/backbone/models/track.js.coffee +++ b/app/assets/javascripts/backbone/models/track.js.coffee @@ -6,6 +6,10 @@ class BeatHaven.Models.Track extends Backbone.Model else this.find_and_start() + pause: -> + BH.Player.pause() + this.node().addClass("paused") + start: -> BH.Player.update_title( artists: @.get("artists") @@ -14,7 +18,7 @@ class BeatHaven.Models.Track extends Backbone.Model 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") + $(".artist-page .tracks li[data-id]").removeClass("now-playing").removeClass("paused") this.node().addClass("now-playing") find_and_start: -> diff --git a/app/assets/javascripts/bindings/player.js.coffee b/app/assets/javascripts/bindings/player.js.coffee index 9d6cec3..9cc2c86 100644 --- a/app/assets/javascripts/bindings/player.js.coffee +++ b/app/assets/javascripts/bindings/player.js.coffee @@ -7,3 +7,16 @@ $ -> BH.Player.pause() $(".player .controls .next").live "click", (e) -> BH.Player.next() + + $(".player .move-it") + .live "mousedown", (e) -> + BH.Player.move_it_mousedown = true + .live "mouseup", (e) -> + BH.Player.move_it_mousedown = false + return false unless BH.Player.current_track? + percent = e.offsetX / $(this).width() + BH.Player.seek(percent) + .live "mousemove", (e) -> + return false unless BH.Player.move_it_mousedown and BH.Player.current_track? + percent = e.offsetX / $(this).width() * 100 + $(".player .progress-bar .bar").css(width: "#{percent}%") diff --git a/app/assets/javascripts/bindings/track.js.coffee b/app/assets/javascripts/bindings/track.js.coffee index 07ef850..7aa5dd7 100644 --- a/app/assets/javascripts/bindings/track.js.coffee +++ b/app/assets/javascripts/bindings/track.js.coffee @@ -3,3 +3,7 @@ $ -> e.preventDefault() id = parseInt($(this).parent().data("id"), 10) BH.Player.tracks.get(id).play() + $(".track-pause").live "click", (e) -> + e.preventDefault() + id = parseInt($(this).parent().data("id"), 10) + BH.Player.tracks.get(id).pause() diff --git a/app/assets/stylesheets/album-track.css.scss b/app/assets/stylesheets/album-track.css.scss index cb7c15e..a181397 100644 --- a/app/assets/stylesheets/album-track.css.scss +++ b/app/assets/stylesheets/album-track.css.scss @@ -39,6 +39,18 @@ opacity: 1; } } + &.paused { + .track-play { + display: block; + + i { + opacity: 1; + } + } + .track-pause { + display: none; + } + } } .btn-round {