diff --git a/app/common/main-controller.js b/app/common/main-controller.js index 037805d..47f7856 100644 --- a/app/common/main-controller.js +++ b/app/common/main-controller.js @@ -258,35 +258,6 @@ angular.module('JamStash') }); }; - $rootScope.addToJukebox = function (id) { - if (globals.settings.Debug) { console.log("LOAD JUKEBOX"); } - $.ajax({ - url: globals.BaseURL() + '/jukeboxControl.view?' + globals.BaseParams() + '&action=set&id=' + id, - method: 'GET', - dataType: globals.settings.Protocol, - timeout: globals.settings.Timeout, - success: function (data) { - /* - if (data["subsonic-response"].podcasts.channel !== undefined) { - } - deferred.resolve(podcasts); - */ - $.get(globals.BaseURL() + '/jukeboxControl.view?' + globals.BaseParams() + '&action=start'); - } - }); - }; - $rootScope.sendToJukebox = function (action) { - if (globals.settings.Debug) { console.log("SEND JUKEBOX " + action); } - $.ajax({ - url: globals.BaseURL() + '/jukeboxControl.view?' + globals.BaseParams() + '&action=' + action, - method: 'GET', - dataType: globals.settings.Protocol, - timeout: globals.settings.Timeout, - success: function (data) { - } - }); - }; - $scope.toggleStar = function (item) { subsonic.toggleStar(item).then(function (newStarred) { item.starred = newStarred; diff --git a/app/player/player-directive.js b/app/player/player-directive.js index b61f083..b4fa6c8 100644 --- a/app/player/player-directive.js +++ b/app/player/player-directive.js @@ -123,7 +123,7 @@ angular.module('jamstash.player.directive', ['jamstash.player.service', 'jamstas $player.jPlayer('setMedia', media); if (globals.settings.Jukebox) { $player.jPlayer('mute', true); - scope.addToJukebox(newSong.id); + subsonic.addToJukebox(newSong); } if (playerService.loadSong === true || globals.settings.Jukebox) { // Do not play, only load @@ -152,10 +152,9 @@ angular.module('jamstash.player.directive', ['jamstash.player.service', 'jamstas scope.$watch(function () { return playerService.pauseSong; }, function (newVal) { - if(newVal === true) { - $player.jPlayer('pause'); - } else { - $player.jPlayer('play'); + $player.jPlayer(newVal ? 'pause' : 'play'); + if(globals.settings.Jukebox){ + subsonic.sendToJukebox(newVal ? 'stop' : 'start'); } }); diff --git a/app/player/player-directive_test.js b/app/player/player-directive_test.js index 5220f64..7625fc3 100644 --- a/app/player/player-directive_test.js +++ b/app/player/player-directive_test.js @@ -76,11 +76,12 @@ describe("jplayer directive", function () { it("if the global setting Jukebox is true, it mutes jPlayer and adds the song to subsonic's Jukebox", function () { mockGlobals.settings.Jukebox = true; - scope.addToJukebox = jasmine.createSpy("addToJukebox"); + spyOn(subsonic, "addToJukebox") scope.$apply(); expect($player.jPlayer).toHaveBeenCalledWith('mute', true); - expect(scope.addToJukebox).toHaveBeenCalledWith(playingSong.id); + expect(subsonic.addToJukebox).toHaveBeenCalledWith( + jasmine.objectContaining({ id: playingSong.id})) }); it("if the player service's loadSong flag is true, it does not play the song, it displays the player controls and sets the player to the song's supplied position", function () { @@ -142,6 +143,7 @@ describe("jplayer directive", function () { describe("", function () { beforeEach(function () { $.fn.jPlayer.and.stub(); + spyOn(subsonic, "sendToJukebox") }); it("When the player service's restartSong flag is true, it restarts the current song, resets the restart flag to false and resets the scrobbled flag to false", function () { @@ -170,6 +172,25 @@ describe("jplayer directive", function () { expect($player.jPlayer).toHaveBeenCalledWith('play'); }); + it("When the player service's pauseSong is true and jukebox is enabled, 'stop' is sent to the jukebox", function () { + mockGlobals.settings.Jukebox = true; + playerService.pauseSong = true; + scope.$apply(); + + expect(subsonic.sendToJukebox).toHaveBeenCalledWith('stop'); + }); + + it("Given that the current song is paused and jukebox is enabled, 'start' is sent to the jukebox when it's unpaused", function () { + mockGlobals.settings.Jukebox = true; + + playerService.pauseSong = true; + scope.$apply(); + playerService.pauseSong = false; + scope.$apply(); + + expect(subsonic.sendToJukebox).toHaveBeenCalledWith('start'); + }); + it("When the player service's volume changes, it sets jPlayer's volume", function () { playerService.getVolume.and.returnValue(0.2034); scope.$apply(); diff --git a/app/player/player.js b/app/player/player.js index 18eb078..49d0491 100644 --- a/app/player/player.js +++ b/app/player/player.js @@ -15,22 +15,8 @@ angular.module('jamstash.player.controller', ['jamstash.player.service', 'jamsta $scope.settings = globals.settings; $scope.playerSettings = player.settings; - $scope.play = function () { - if (globals.settings.Jukebox) { - $scope.sendToJukebox('start'); - } else { - player.togglePause(); - } - }; - - $scope.pause = function () { - if (globals.settings.Jukebox) { - $scope.sendToJukebox('stop'); - } else { - player.togglePause(); - } - }; - + $scope.play = player.togglePause; + $scope.pause = player.togglePause; $scope.previousTrack = player.previousTrack; $scope.nextTrack = player.nextTrack; }]); diff --git a/app/subsonic/subsonic-service.js b/app/subsonic/subsonic-service.js index 239b26c..32ef90a 100644 --- a/app/subsonic/subsonic-service.js +++ b/app/subsonic/subsonic-service.js @@ -53,7 +53,9 @@ function subsonicService( scrobble : scrobble, search : search, subsonicRequest : subsonicRequest, - toggleStar : toggleStar + toggleStar : toggleStar, + addToJukebox : addToJukebox, + sendToJukebox : sendToJukebox }); // TODO: Hyz: Remove when refactored @@ -570,4 +572,27 @@ function subsonicService( }); return promise; } + + function addToJukebox(song) { + if (globals.settings.Debug) { console.log("Load Jukebox"); } + var promise = self.subsonicRequest('jukeboxControl.view', { + params: { + action: 'set', + id: song.id + } + }).then(function () { + self.sendToJukebox('start'); + }); + return promise; + } + + function sendToJukebox(action) { + if (globals.settings.Debug) { console.log("Send Jukebox " + action); } + var promise = self.subsonicRequest('jukeboxControl.view', { + params: { + action: action + } + }) + return promise; + } }