1
YUI.add('music-io', function (Y, NAME) {
4
/*global STREAMING_URL, STREAMING_PASS, STREAMING_USER */
5
var ERRATA = 'c=web&v=1.2&f=json',
6
ONEERROR = 'one:error',
9
AMPERSANDSONGEQUALS = '&songId=',
10
CREATEPLAYLISTVIEW = 'createPlaylist.view?',
11
GETMUSICDIRECTORYVIEW = 'getMusicDirectory.view?id=',
12
GETRANDOMSONGSVIEW = 'getRandomSongs.view?',
15
namespace = Y.namespace('Music');
17
namespace.DataSource = Y.Base.create('dataSource', Y.DataSource.IO, [], {
18
/* This is perverse, I know, but it prevents DRY, so hooray. */
19
_defDataFn: function(e) {
23
results: (Y.Lang.isArray(data)) ? data: [data],
24
meta: (meta) ? meta : {}
27
response.json = Y.JSON.parse(e.data.responseText)['subsonic-response'];
28
if (response.json.error) {
29
Y.Global.fire(ONEERROR, {
30
message: response.json.error.message
32
Y.Do.Halt('An error in the API caused a halt');
36
this.fire('response', Y.mix({
37
'response': response}, e));
39
getArtists: function(callback) {
41
request: 'getIndexes.view?' + ERRATA,
43
failure: function() { Y.Global.fire(ONEERROR); },
48
createPlaylist: function(name, songs, callback) {
49
var songParams = EMPTY,
50
songsLength = songs.length,
54
for (i = 0; i < songsLength; i++) {
55
songParams = songParams + AMPERSANDSONGEQUALS + songs[i];
57
params = 'name=' + name + songParams;
59
request: CREATEPLAYLISTVIEW + params + AMPERSAND + ERRATA,
62
failure: function() { Y.Global.fire(ONEERROR); }
66
updatePlaylist: function(playlistId, songs, callback) {
67
var songParams = EMPTY,
68
songsLength = songs.length,
72
for (i = 0; i < songsLength; i++) {
73
songParams = songParams + AMPERSANDSONGEQUALS + songs[i];
75
params = 'playlistId=' + playlistId + songParams;
77
request: CREATEPLAYLISTVIEW + params + AMPERSAND + ERRATA,
80
failure: function() { Y.Global.fire(ONEERROR); }
84
deletePlaylist: function(playlistId, callback) {
86
request: 'deletePlaylist.view?id=' + playlistId + AMPERSAND + ERRATA,
92
getPlaylist: function(playlistId, callback) {
94
request: 'getPlaylist.view?id=' + playlistId + AMPERSAND + ERRATA,
100
getSongs: function(callback) {
102
request: 'getSongs.view?' + ERRATA,
104
failure: function() { Y.Global.fire(ONEERROR); },
109
getRandomSongs: function(nResults, callback) {
112
url = GETRANDOMSONGSVIEW + ERRATA;
114
url = GETRANDOMSONGSVIEW + SIZE + "=" + nResults + AMPERSAND + ERRATA;
119
failure: function() { Y.Global.fire(ONEERROR); },
124
getAlbums: function(artistId, callback) {
127
url = 'getAlbums.view?' + ERRATA;
129
url = GETMUSICDIRECTORYVIEW + artistId + AMPERSAND + ERRATA;
134
failure: function() { Y.Global.fire(ONEERROR); },
139
getAlbum: function(albumId, callback) {
141
request: GETMUSICDIRECTORYVIEW + albumId + AMPERSAND + ERRATA,
143
failure: function() { Y.Global.fire(ONEERROR); },
148
getStreamingURI: function(song) {
149
if (song === undefined) {
152
return STREAMING_URL + '/rest/stream.view?id=' + song.id + '&u=' + STREAMING_USER + '&p=' + STREAMING_PASS + AMPERSAND + ERRATA;
155
getAlbumArt: function(song, image) {
156
var defaultURL = MEDIA_URL + 'img/default-album-art.png',
159
image.set("alt", EMPTY);
160
if (song.coverArt === undefined) {
161
image.set(SRC, defaultURL);
164
url = 'getCoverArt.view?id=' + song.coverArt + AMPERSAND + ERRATA;
165
image.set(SRC, this.get('source') + url);
167
search: function(term, callback) {
169
request: 'search.view?any=' + term + AMPERSAND + ERRATA,
178
{fn: Y.Plugin.DataSourceCache, cfg: { max: 3 }}
180
source: { value: '/api/music/1.0/' }
184
namespace.PlaylistsDS = Y.Base.create('playlistsds', Y.DataSource.IO, [], {
185
/* This is perverse, I know, but it prevents DRY, so hooray. */
186
_defDataFn: function(e) {
190
results: (Y.Lang.isArray(data)) ? data: [data],
191
meta: (meta) ? meta : {}
194
response.json = Y.JSON.parse(e.data.responseText)['subsonic-response'];
195
if (response.json.error) {
196
Y.Global.fire(ONEERROR, {
197
message: response.json.error.message
199
Y.Do.Halt('An error in the API caused a halt');
203
this.fire('response', Y.mix({
204
'response': response}, e));
206
getPlaylists: function(callback) {
208
request: 'getPlaylists.view?' + ERRATA,
210
failure: function() { Y.Global.fire(ONEERROR); },
218
{fn: Y.Plugin.DataSourceCache, cfg: { max: 3 }}
220
source: { value: '/api/music/1.0/' }
230
"datasource-function",
232
"datasource-jsonschema",
233
"datasource-xmlschema",
234
"datasource-arrayschema",
235
"datasource-textschema",