diff --git a/assets/js/controls/queue.js b/assets/js/controls/queue.js index 32d7a99..d355565 100644 --- a/assets/js/controls/queue.js +++ b/assets/js/controls/queue.js @@ -5,6 +5,19 @@ var Queue = can.Control.extend({ queueSongs: options.queueSongs, status: options.status })); + }, + + playSong: function(pos) { + can.ajax({ + url: '/api/control/play', + data: { pos: pos }, + type: 'PUT' + }); + }, + + 'li click': function(element, event) { + var pos = $(element).data('pos'); + this.playSong(pos); } }); diff --git a/models/control.rb b/models/control.rb index 486770f..b313af2 100644 --- a/models/control.rb +++ b/models/control.rb @@ -6,8 +6,8 @@ class Control [:play, :stop, :next, :previous, :pause] end - def play - MPDConnection.mpd.play + def play(pos = nil) + MPDConnection.mpd.play(pos) end def stop diff --git a/models/song.rb b/models/song.rb index 490f349..6b78e3f 100644 --- a/models/song.rb +++ b/models/song.rb @@ -1,17 +1,18 @@ require './models/mpd_connection' -class Song < Struct.new(:id, :artist, :album, :title, :playing) +class Song < Struct.new(:id, :artist, :album, :title, :pos, :playing) - def initialize(song, playing: false) + def initialize(song, pos: nil, playing: false) self.id = song.id self.artist = song.artist self.album = song.album self.title = song.title + self.pos = pos self.playing = playing end def self.queue current_song = MPDConnection.mpd.status[:songid] - MPDConnection.mpd.queue.map { |song| self.new(song, playing: (song.id == current_song)) } + MPDConnection.mpd.queue.map { |song| self.new(song, playing: (song.id == current_song), pos: song.pos) } end end diff --git a/mpd_client.rb b/mpd_client.rb index ddfb61e..4ec3612 100644 --- a/mpd_client.rb +++ b/mpd_client.rb @@ -89,6 +89,10 @@ class MPDClient < Sinatra::Base JSON({ data: Song.queue.map(&:to_h) }) end + put '/control/play' do + Control.play(params[:pos]) + end + put '/control/:action' do if Control.controls.include?(params[:action].to_sym) Control.send(params[:action]) diff --git a/public/views/queue.ejs b/public/views/queue.ejs index f647cd7..39c3710 100644 --- a/public/views/queue.ejs +++ b/public/views/queue.ejs @@ -1,6 +1,6 @@
<%= song.attr('title') %>
<%= song.attr('artist') %>