Jamstash/app/common/selectedsongs-service.js
Hyzual 5d385149f3 Refactor song selection across the app
- Added a new SelectedSongs service. Its purpose is to store an array of selected songs and all behaviour associated with it : adding songs to it, removing them, toggling a song, etc. It also changes each song's selected property.
- Removed selectAll, selectNone, playAll, playFrom, addSongsToQueue and selectSong from main-controller.js. Those are now defined either in queue.js, subsonic.js or archive.js, depending on where they are used. I know it does add some duplication between archive.js and subsonic.js, but this will be addressed in a later commit (maybe by creating a shared service).
- Refactored subsonic-service.js' addToPlaylist() and split it between subsonic-service.js and subsonic.js (like all the other playlist-related functions).
- Moved subsonic-service.js' songsRemoveSelected() to subsonic.js since it only removes songs from the scope.
- Removed $rootScope and utils (unused dependencies) from subsonic-service.js
2015-07-12 01:52:42 +02:00

76 lines
1.6 KiB
JavaScript

/**
* jamstash.selectedsongs Module
*
* Manages the list of selected songs accross the app to avoid duplicating
* those functions both in Subsonic and Archive.org contexts
*/
angular.module('jamstash.selectedsongs', ['ngLodash'])
.service('SelectedSongs', SelectedSongs);
SelectedSongs.$inject = ['lodash'];
function SelectedSongs(_) {
'use strict';
var self = this;
_.extend(self, {
add : add,
addSongs: addSongs,
get : get,
remove : remove,
reset : reset,
toggle : toggle
});
var selectedSongs = [];
function add(song) {
song.selected = true;
selectedSongs.push(song);
selectedSongs = _.uniq(selectedSongs);
return self;
}
function addSongs(songs) {
_.forEach(songs, function (song) {
song.selected = true;
});
selectedSongs = _.union(selectedSongs, songs);
return self;
}
function get() {
return selectedSongs;
}
function remove(song) {
var removedSong = _(selectedSongs).remove(function (selectedSong) {
return selectedSong === song;
}).first();
_.set(removedSong, 'selected', false);
return self;
}
function toggle(song) {
if (song.selected) {
self.remove(song);
} else {
self.add(song);
}
return self;
}
function reset() {
_.forEach(selectedSongs, function (song) {
song.selected = false;
});
selectedSongs = [];
return self;
}
}