-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathPlaylistContainer.js
More file actions
126 lines (109 loc) · 4.22 KB
/
PlaylistContainer.js
File metadata and controls
126 lines (109 loc) · 4.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
var b = require('bindings')('spotify.node');
var sp = require('./libspotify');
var util = require('util');
var SpObject = require('./SpObject');
function PlaylistContainer (sp_playlistcontainer) {
this._sp_object = sp_playlistcontainer;
// We need to set up the callbacks first because we are using one of them
// to notify us when the object is ready rather than using a backoff
// isReady check
this._setupNativeCallbacks();
SpObject.apply(this);
}
util.inherits(PlaylistContainer, SpObject);
PlaylistContainer.__proto__ = SpObject;
function playlistcontainer_object_type() { return 'playlistcontainer'; };
PlaylistContainer.__defineGetter__('_object_type', playlistcontainer_object_type);
PlaylistContainer.prototype.__defineGetter__('_object_type', playlistcontainer_object_type);
module.exports = PlaylistContainer;
PlaylistContainer.prototype.PLAYLIST_TYPE_PLAYLIST = b.SP_PLAYLIST_TYPE_PLAYLIST;
PlaylistContainer.prototype.PLAYLIST_TYPE_START_FOLDER = b.SP_PLAYLIST_TYPE_START_FOLDER;
PlaylistContainer.prototype.PLAYLIST_TYPE_END_FOLDER = b.SP_PLAYLIST_TYPE_END_FOLDER;
PlaylistContainer.prototype.PLAYLIST_TYPE_PLACEHOLDER = b.SP_PLAYLIST_TYPE_PLACEHOLDER;
PlaylistContainer.prototype._populateAttributes = function _populateAttributes() {
return this.constructor.super_.prototype._populateAttributes();
};
/**
* gets the number of playlists in the playlist container
*/
PlaylistContainer.prototype.getNumPlaylists = function getNumPlaylists() {
this._readyOrThrow();
return b.playlistcontainer_num_playlists(this._sp_object);
};
/**
* gets the type of the playlist at index in the playlist container
*/
PlaylistContainer.prototype.getPlaylistType = function getPlaylistType(idx) {
this._readyOrThrow();
return b.playlistcontainer_playlist_type(this._sp_object, idx);
};
/**
* gets the id of the playlist folder at index in the playlist container
*/
PlaylistContainer.prototype.getPlaylistFolderID = function getPlaylistFolderID(idx) {
this._readyOrThrow();
return b.playlistcontainer_playlist_folder_id(this._sp_object, idx);
};
/**
* gets the name of the playlist folder at index in the playlist container
*/
PlaylistContainer.prototype.getPlaylistFolderName = function getPlaylistFolderName(idx) {
this._readyOrThrow();
return b.playlistcontainer_playlist_folder_name(this._sp_object, idx);
};
/**
* gets a playlist from the specified index
*/
PlaylistContainer.prototype.getPlaylistAtIndex = function getPlaylistAtIndex(idx) {
return new sp.Playlist(b.playlistcontainer_playlist(this._sp_object, sp.Session.currentSession._sp_session, idx))
}
/**
* gets a list of all the playlists in the playlist container
*/
PlaylistContainer.prototype.getPlaylists = function getPlaylists(callback) {
this._readyOrThrow();
var i, numReady = 0;
var numPlaylists = this.getNumPlaylists();
var playlists = new Array();
for (i = 0; i < numPlaylists; i++) {
var type = this.getPlaylistType(i);
switch (type) {
case b.SP_PLAYLIST_TYPE_PLAYLIST:
playlists.push(this.getPlaylistAtIndex(i));
break;
}
}
for (i = 0; i < playlists.length; i++) {
if (playlists[i].isReady()) {
checkPlaylistsLoaded(++numReady, playlists, callback);
} else {
playlists[i].once('ready', function() {
checkPlaylistsLoaded(++numReady, playlists, callback);
});
}
}
};
var checkPlaylistsLoaded = function(numReady, array, callback) {
if (numReady >= array.length) {
callback(array);
}
};
/**
* C callbacks for spotify events do nothing more than calling their JS equivalent
* here we setup the functions that must be called from C upon these events
*/
PlaylistContainer.prototype._setupNativeCallbacks = function _setupNativeCallbacks() {
var self = this;
this._sp_object.container_loaded = function(data) {
self.emit('ready', data);
};
this._sp_object.playlist_added = function(data) {
self.emit('playlist_added', data);
};
this._sp_object.playlist_removed = function(data) {
self.emit('playlist_removed', data);
};
this._sp_object.playlist_moved = function(data) {
self.emit('playlist_moved', data);
};
};