-
-
Notifications
You must be signed in to change notification settings - Fork 288
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
front-end music controller & service
- Loading branch information
1 parent
4549a55
commit 8a18be6
Showing
2 changed files
with
210 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,130 @@ | ||
/** | ||
* Gladys Project | ||
* http://gladysproject.com | ||
* Software under licence Creative Commons 3.0 France | ||
* http://creativecommons.org/licenses/by-nc-sa/3.0/fr/ | ||
* You may not use this software for commercial purposes. | ||
* @author :: Pierre-Gilles Leymarie | ||
*/ | ||
|
||
(function () { | ||
'use strict'; | ||
|
||
angular | ||
.module('gladys') | ||
.controller('MusicCtrl', MusicCtrl); | ||
|
||
MusicCtrl.$inject = ['musicService', 'roomService']; | ||
|
||
function MusicCtrl(musicService, roomService) { | ||
/* jshint validthis: true */ | ||
var vm = this; | ||
vm.roomId = null; | ||
vm.displayAskRoomForm = false; | ||
|
||
vm.selectRoomId = selectRoomId; | ||
|
||
vm.play = play; | ||
vm.pause = pause; | ||
vm.stop = stop; | ||
vm.previous = previous; | ||
vm.next = next; | ||
vm.getCurrentTrack = getCurrentTrack; | ||
vm.getQueue = getQueue; | ||
|
||
activate(); | ||
|
||
function activate(){ | ||
getRoomId(); | ||
if(vm.displayAskRoomForm) getRooms(); | ||
else refreshMusicData(); | ||
} | ||
|
||
|
||
function getRoomId() { | ||
vm.roomId = musicService.getSavedRoomBox(vm.boxId); | ||
if(vm.roomId) vm.displayAskRoomForm = false; | ||
else vm.displayAskRoomForm = true; | ||
} | ||
|
||
function getRooms(){ | ||
roomService.get() | ||
.then(function(data){ | ||
vm.rooms = data.data; | ||
}); | ||
} | ||
|
||
function selectRoomId(roomId){ | ||
musicService.saveRoomBox(vm.boxId, roomId); | ||
vm.displayAskRoomForm = false; | ||
vm.roomId = roomId; | ||
refreshMusicData(); | ||
} | ||
|
||
function refreshMusicData(){ | ||
getPlaying(); | ||
getCurrentTrack(); | ||
getQueue(); | ||
} | ||
|
||
function play(){ | ||
return musicService.play({room: vm.roomId}) | ||
.then(function(){ | ||
vm.playing = true; | ||
}); | ||
} | ||
|
||
function pause(){ | ||
return musicService.pause({room: vm.roomId}) | ||
.then(function(){ | ||
vm.playing = false; | ||
}); | ||
} | ||
|
||
function stop(){ | ||
return musicService.stop({room: vm.roomId}) | ||
.then(function(){ | ||
vm.playing = false; | ||
refreshMusicData(); | ||
}); | ||
} | ||
|
||
function previous(){ | ||
return musicService.previous({room: vm.roomId}) | ||
.then(function(){ | ||
getCurrentTrack(); | ||
getQueue(); | ||
}); | ||
} | ||
|
||
function next(){ | ||
return musicService.next({room: vm.roomId}) | ||
.then(function(){ | ||
getCurrentTrack(); | ||
getQueue(); | ||
}); | ||
} | ||
|
||
function getPlaying(){ | ||
return musicService.getPlaying({room: vm.roomId}) | ||
.then(function(data){ | ||
vm.playing = data.data.playing; | ||
}); | ||
} | ||
|
||
function getCurrentTrack(){ | ||
return musicService.getCurrentTrack({room: vm.roomId}) | ||
.then(function(data){ | ||
vm.currentTrack = data.data; | ||
}); | ||
} | ||
|
||
function getQueue() { | ||
return musicService.getQueue({room: vm.roomId}) | ||
.then(function(data){ | ||
vm.queue = data.data; | ||
}); | ||
} | ||
|
||
} | ||
})(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
/** | ||
* Gladys Project | ||
* http://gladysproject.com | ||
* Software under licence Creative Commons 3.0 France | ||
* http://creativecommons.org/licenses/by-nc-sa/3.0/fr/ | ||
* You may not use this software for commercial purposes. | ||
* @author :: Pierre-Gilles Leymarie | ||
*/ | ||
|
||
(function () { | ||
'use strict'; | ||
|
||
angular | ||
.module('gladys') | ||
.factory('musicService', musicService); | ||
|
||
musicService.$inject = ['$http', 'cacheService']; | ||
|
||
function musicService($http, cacheService) { | ||
|
||
var service = { | ||
play: play, | ||
pause: pause, | ||
stop: stop, | ||
previous: previous, | ||
next: next, | ||
getPlaying: getPlaying, | ||
getCurrentTrack: getCurrentTrack, | ||
getQueue: getQueue, | ||
getSavedRoomBox: getSavedRoomBox, | ||
saveRoomBox: saveRoomBox | ||
}; | ||
|
||
var EXPIRATION = 10*365*24*3600*1000; | ||
|
||
return service; | ||
|
||
function play(params) { | ||
return $http({method: 'POST', url: '/music/play', data: params}); | ||
} | ||
|
||
function pause(params) { | ||
return $http({method: 'POST', url: '/music/pause', data: params}); | ||
} | ||
|
||
function stop(params) { | ||
return $http({method: 'POST', url: '/music/stop', data: params}); | ||
} | ||
|
||
function previous(params) { | ||
return $http({method: 'POST', url: '/music/previous', data: params}); | ||
} | ||
|
||
function next(params) { | ||
return $http({method: 'POST', url: '/music/next', data: params}); | ||
} | ||
|
||
function getPlaying(params) { | ||
return $http({method: 'GET', url: '/music/playing', params: params}); | ||
} | ||
|
||
function getCurrentTrack(params) { | ||
return $http({method: 'GET', url: '/music/currenttrack', params: params}); | ||
} | ||
|
||
function getQueue(params) { | ||
return $http({method: 'GET', url: '/music/queue', params: params}); | ||
} | ||
|
||
// get RoomId from localStorage | ||
function getSavedRoomBox(boxId){ | ||
return cacheService.get('MUSIC_BOX_ID_' + boxId); | ||
} | ||
|
||
// saveRoomId of this box to localStorage | ||
function saveRoomBox(boxId, roomId){ | ||
return cacheService.set('MUSIC_BOX_ID_' + boxId, roomId, EXPIRATION); | ||
} | ||
} | ||
})(); |