|
|
|
@ -16,348 +16,408 @@ define([
|
|
|
|
|
], function (_, Backbone, Toolbox, DMAPType, DMAPModel) {
|
|
|
|
|
"use strict";
|
|
|
|
|
|
|
|
|
|
var that
|
|
|
|
|
var Client = Toolbox.Base.extend(_.extend({
|
|
|
|
|
|
|
|
|
|
// TODO: Could be interesting to set a userage, could possibly affect transcoding on the server side
|
|
|
|
|
, xhr = function (url, responseType, callback) {
|
|
|
|
|
var xhr = new XMLHttpRequest();
|
|
|
|
|
var contentType = "text/plain; charset=utf-8";
|
|
|
|
|
constructor: function(attributes, options) {
|
|
|
|
|
_.bindAll(this
|
|
|
|
|
, "setAuth"
|
|
|
|
|
, "init"
|
|
|
|
|
, "deinit"
|
|
|
|
|
, "logout"
|
|
|
|
|
, "buildRequestUid"
|
|
|
|
|
, "url"
|
|
|
|
|
, "urlHost"
|
|
|
|
|
|
|
|
|
|
, "xhr"
|
|
|
|
|
, "fetchXHR"
|
|
|
|
|
, "fetchContentTypes"
|
|
|
|
|
, "fetchServerInfo"
|
|
|
|
|
, "fetchLogin"
|
|
|
|
|
, "fetchDatabases"
|
|
|
|
|
, "fetchDatabase"
|
|
|
|
|
, "fetchPlaylists"
|
|
|
|
|
, "fetchPlaylist"
|
|
|
|
|
, "fetchBrowse"
|
|
|
|
|
, "fetchgenres"
|
|
|
|
|
, "fetchArtists"
|
|
|
|
|
, "fetchAlbums"
|
|
|
|
|
|
|
|
|
|
, "on"
|
|
|
|
|
, "off"
|
|
|
|
|
, "trigger"
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
_.extend(this, Client.defaults);
|
|
|
|
|
this.attributes = _.extend({}, Client.prototype.attributes, attributes);
|
|
|
|
|
|
|
|
|
|
this.setAuth();
|
|
|
|
|
|
|
|
|
|
if (typeof responseType === "function") {
|
|
|
|
|
callback = responseType;
|
|
|
|
|
responseType = "arraybuffer";
|
|
|
|
|
return this;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
xhr.open("GET", url, true);
|
|
|
|
|
xhr.responseType = responseType;
|
|
|
|
|
xhr.timeout = 2000;
|
|
|
|
|
xhr.setRequestHeader("Content-type", contentType);
|
|
|
|
|
xhr.overrideMimeType("text/plain; charset=x-user-defined"); // Technically not need with v2 and responseType but for good measures
|
|
|
|
|
, setAuth: function(username, password) {
|
|
|
|
|
if (!_.isUndefined(username)) {
|
|
|
|
|
this.attributes.username = username;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!_.isEmpty(that.attributes.basicauth)) {
|
|
|
|
|
xhr.withCredentials = true;
|
|
|
|
|
xhr.setRequestHeader("Authorization", "Basic " + that.attributes.basicauth);
|
|
|
|
|
if (!_.isUndefined(password)) {
|
|
|
|
|
this.attributes.password = password;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!_.isEmpty(this.attributes.password) || !_.isEmpty(this.attributes.username)) {
|
|
|
|
|
this.attributes.basicauth = window.btoa((this.attributes.username || "") + ":" + (this.attributes.password || ""));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
xhr.onerror = function () {console.log("Error", arguments, this);};
|
|
|
|
|
xhr.onabort = function () {console.log("Abort", arguments, this);};
|
|
|
|
|
xhr.ontimeout = function () {console.log("Timeout", arguments, this);};
|
|
|
|
|
xhr.onload = function (e) {
|
|
|
|
|
if (this.status === 200) {
|
|
|
|
|
callback(null, this.response);
|
|
|
|
|
} else if (this.status === 401) {
|
|
|
|
|
that.trigger("unauthorized", [this, e]);
|
|
|
|
|
} else {
|
|
|
|
|
callback(e.status);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
, init: function () {
|
|
|
|
|
var that = this
|
|
|
|
|
;
|
|
|
|
|
|
|
|
|
|
console.debug("Fetching content types");
|
|
|
|
|
that.fetchContentTypes(function __fnClientCbFetchContentTypes(contentTypes) {
|
|
|
|
|
console.debug("Fetching server info");
|
|
|
|
|
that.fetchServerInfo(function __fnClientCbFetchServerInfo(serverInfo) {
|
|
|
|
|
console.debug("Fetching login");
|
|
|
|
|
that.fetchLogin(function __fnClientCbFetchLogin(sessionInfo) {
|
|
|
|
|
// TODO: Here we need to request /update for the database version
|
|
|
|
|
//console.debug("Fetching databases info");
|
|
|
|
|
//that.fetchDatabases(function () {
|
|
|
|
|
//console.debug("Fetching database 1");
|
|
|
|
|
//that.fetchDatabase(function () {
|
|
|
|
|
//console.debug("Fetching playlists info");
|
|
|
|
|
//that.fetchPlaylists(function () {
|
|
|
|
|
//console.debug("Fetching playlist 1 of db 1");
|
|
|
|
|
//that.fetchPlaylist(function () {
|
|
|
|
|
//console.debug("Fetching genres of db 1");
|
|
|
|
|
//that.fetchGenres(function () {
|
|
|
|
|
//console.debug("Fetching artists of db 1");
|
|
|
|
|
//that.fetchArtists(function () {
|
|
|
|
|
//console.debug("Fetching albums of db 1");
|
|
|
|
|
//that.fetchAlbums(function () {
|
|
|
|
|
that.trigger("inited", serverInfo);
|
|
|
|
|
//});
|
|
|
|
|
//});
|
|
|
|
|
//});
|
|
|
|
|
//});
|
|
|
|
|
//});
|
|
|
|
|
//});
|
|
|
|
|
//});
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
xhr.send();
|
|
|
|
|
} catch (e) {
|
|
|
|
|
console.error(e);
|
|
|
|
|
callback(e, null);
|
|
|
|
|
return this;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
, fetchXHR = function (path, success, error) {
|
|
|
|
|
xhr(that.url(path), function __fetchXHRCallback(err, content) {
|
|
|
|
|
if (err) {
|
|
|
|
|
if (!_.isFunction(error)) {
|
|
|
|
|
error = function (err, cont) {
|
|
|
|
|
throw [err, cont];
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
error(err, content);
|
|
|
|
|
} else {
|
|
|
|
|
success(content);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
, fetchContentTypes = function (success, error) {
|
|
|
|
|
fetchXHR("/content-codes", function __fetchContentTypesCallback(content) {
|
|
|
|
|
that.collections.contentCodes = new DMAPModel(content);
|
|
|
|
|
if (_.isFunction(success)) {
|
|
|
|
|
success(content);
|
|
|
|
|
}
|
|
|
|
|
}, error);
|
|
|
|
|
}
|
|
|
|
|
, deinit: function () {
|
|
|
|
|
var that = this
|
|
|
|
|
;
|
|
|
|
|
|
|
|
|
|
, fetchServerInfo = function (success, error) {
|
|
|
|
|
fetchXHR("/server-info", function __fetchServerInfoCallback(content) {
|
|
|
|
|
that.collections.serverInfo = new DMAPModel(content, {
|
|
|
|
|
contentCodes: that.collections.contentCodes
|
|
|
|
|
});
|
|
|
|
|
this.logout();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (_.isFunction(success)) {
|
|
|
|
|
success(content);
|
|
|
|
|
}
|
|
|
|
|
}, error);
|
|
|
|
|
}
|
|
|
|
|
,logout: function () {
|
|
|
|
|
|
|
|
|
|
, fetchLogin = function (success, error) {
|
|
|
|
|
fetchXHR("/login", success, error);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
, fetchDatabases = function (success, error) {
|
|
|
|
|
fetchXHR("/databases", function __fetchDatabasesCallback(content) {
|
|
|
|
|
that.collections.databasesInfo = new DMAPModel(content, {
|
|
|
|
|
contentCodes: that.collections.contentCodes
|
|
|
|
|
});
|
|
|
|
|
, buildRequestUid: function(path, fields, query) {
|
|
|
|
|
var that = this
|
|
|
|
|
;
|
|
|
|
|
|
|
|
|
|
if (_.isFunction(success)) {
|
|
|
|
|
success(content);
|
|
|
|
|
if (_.isEmpty(path) || !_.isString(path)) {
|
|
|
|
|
throw new Error("INVALID_TYPE_ERROR");
|
|
|
|
|
}
|
|
|
|
|
}, error);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
, fetchDatabase = function (success, error, dbId) {
|
|
|
|
|
if (_.isUndefined(dbId) || !_.isNumeric(dbId)) {
|
|
|
|
|
dbId = 1;
|
|
|
|
|
if (!_.isArray(fields)) {
|
|
|
|
|
fields = [];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!_.isArray(query)) {
|
|
|
|
|
query = [];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fetchXHR("/databases/" + dbId + "/items", function __fetchDatabaseCallback(content) {
|
|
|
|
|
that.collections.databases[dbId] = new DMAPModel(content, {
|
|
|
|
|
contentCodes: that.collections.contentCodes
|
|
|
|
|
});
|
|
|
|
|
,url: function (request) {
|
|
|
|
|
var that = this
|
|
|
|
|
, uri = ""
|
|
|
|
|
;
|
|
|
|
|
|
|
|
|
|
if (_.isFunction(success)) {
|
|
|
|
|
success(content);
|
|
|
|
|
if (typeof request === "string") {
|
|
|
|
|
uri = request; // Add leading slash here!
|
|
|
|
|
}
|
|
|
|
|
}, error);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
, fetchPlaylists = function (success, error, dbId) {
|
|
|
|
|
if (_.isUndefined(dbId) || !_.isNumeric(dbId)) {
|
|
|
|
|
dbId = 1;
|
|
|
|
|
}
|
|
|
|
|
if (_.isNumber(this.collections.session.id)) {
|
|
|
|
|
var prefix = "?";
|
|
|
|
|
|
|
|
|
|
fetchXHR("/databases/" + dbId + "/containers", function __fetchPlaylistsCallback(content) {
|
|
|
|
|
that.collections.playlistsInfo = new DMAPModel(content, {
|
|
|
|
|
contentCodes: that.collections.contentCodes
|
|
|
|
|
});
|
|
|
|
|
if (uri.indexOf("?") >= 0) {
|
|
|
|
|
prefix = "&";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (_.isFunction(success)) {
|
|
|
|
|
success(content);
|
|
|
|
|
uri += prefix + "session-id=" + this.collections.session.id;
|
|
|
|
|
}
|
|
|
|
|
}, error);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
, fetchPlaylist = function (success, error, playlistId, dbId) {
|
|
|
|
|
if (_.isUndefined(playlistId) || !_.isNumeric(playlistId)) {
|
|
|
|
|
playlistId = 1;
|
|
|
|
|
return this.attributes.protocol + "://" + this.urlHost() + uri;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (_.isUndefined(dbId) || !_.isNumeric(dbId)) {
|
|
|
|
|
dbId = 1;
|
|
|
|
|
, urlHost: function () {
|
|
|
|
|
var that = this
|
|
|
|
|
;
|
|
|
|
|
|
|
|
|
|
return this.attributes.hostname + ":" + this.attributes.port;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var requestUri = "/databases/"
|
|
|
|
|
;
|
|
|
|
|
// TODO: Could be interesting to set a useragent, could possibly affect transcoding on the server side
|
|
|
|
|
, xhr: function (url, responseType, callback) {
|
|
|
|
|
var that = this
|
|
|
|
|
;
|
|
|
|
|
|
|
|
|
|
requestUri += dbId;
|
|
|
|
|
requestUri += "/containers/";
|
|
|
|
|
requestUri += playlistId;
|
|
|
|
|
requestUri += "/items";
|
|
|
|
|
requestUri += "?meta=dmap.itemid,dmap.persistentid,dmap.containeritemid";
|
|
|
|
|
var xhr = new XMLHttpRequest();
|
|
|
|
|
var contentType = "text/plain; charset=utf-8";
|
|
|
|
|
|
|
|
|
|
fetchXHR(requestUri, function __fetchPlaylistCallback(content) {
|
|
|
|
|
if (!_.isArray(that.collections.playlists[dbId])) {
|
|
|
|
|
that.collections.playlists[dbId] = [];
|
|
|
|
|
if (typeof responseType === "function") {
|
|
|
|
|
callback = responseType;
|
|
|
|
|
responseType = "arraybuffer";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
that.collections.playlists[dbId][playlistId] = new DMAPModel(content, {
|
|
|
|
|
contentCodes: that.collections.contentCodes
|
|
|
|
|
});
|
|
|
|
|
xhr.open("GET", url, true);
|
|
|
|
|
xhr.responseType = responseType;
|
|
|
|
|
xhr.timeout = 2000;
|
|
|
|
|
xhr.setRequestHeader("Content-type", contentType);
|
|
|
|
|
xhr.overrideMimeType("text/plain; charset=x-user-defined"); // Technically not need with v2 and responseType but for good measures
|
|
|
|
|
|
|
|
|
|
if (_.isFunction(success)) {
|
|
|
|
|
success(content);
|
|
|
|
|
if (!_.isEmpty(that.attributes.basicauth)) {
|
|
|
|
|
xhr.withCredentials = true;
|
|
|
|
|
xhr.setRequestHeader("Authorization", "Basic " + that.attributes.basicauth);
|
|
|
|
|
}
|
|
|
|
|
}, error);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
, fetchBrowse = function (success, error, dbId, type) {
|
|
|
|
|
// TODO: We need to detect if the server supports browsing or not an implement our own otherwise.
|
|
|
|
|
if (_.isEmpty(type) || !_.isString(type) || !_.isArray(that.collections.browser[type])) {
|
|
|
|
|
throw new Error("INVALID_BROWSE_TYPE");
|
|
|
|
|
try {
|
|
|
|
|
xhr.onerror = function () {console.log("Error", arguments, this);};
|
|
|
|
|
xhr.onabort = function () {console.log("Abort", arguments, this);};
|
|
|
|
|
xhr.ontimeout = function () {console.log("Timeout", arguments, this);};
|
|
|
|
|
xhr.onload = function (e) {
|
|
|
|
|
if (this.status === 200) {
|
|
|
|
|
callback(null, this.response);
|
|
|
|
|
} else if (this.status === 401) {
|
|
|
|
|
that.trigger("unauthorized", [this, e]);
|
|
|
|
|
} else {
|
|
|
|
|
callback(e.status);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
xhr.send();
|
|
|
|
|
} catch (e) {
|
|
|
|
|
console.error(e);
|
|
|
|
|
callback(e, null);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var requestUri = "/databases/"
|
|
|
|
|
;
|
|
|
|
|
|
|
|
|
|
requestUri += dbId;
|
|
|
|
|
requestUri += "/browse/";
|
|
|
|
|
requestUri += type;
|
|
|
|
|
requestUri += "?meta=dmap.itemid,dmap.persistentid,dmap.containeritemid";
|
|
|
|
|
, fetchXHR: function (path, success, error) {
|
|
|
|
|
var that = this
|
|
|
|
|
;
|
|
|
|
|
|
|
|
|
|
that.xhr(that.url(path), function __fetchXHRCallback(err, content) {
|
|
|
|
|
if (err) {
|
|
|
|
|
if (!_.isFunction(error)) {
|
|
|
|
|
error = function (err, cont) {
|
|
|
|
|
throw [err, cont];
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fetchXHR(requestUri, function __fetchBrowseCallback(content) {
|
|
|
|
|
that.collections.browser[type][dbId] = new DMAPModel(content, {
|
|
|
|
|
contentCodes: that.collections.contentCodes
|
|
|
|
|
error(err, content);
|
|
|
|
|
} else {
|
|
|
|
|
success(content);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
if (_.isFunction(success)) {
|
|
|
|
|
success(content);
|
|
|
|
|
}
|
|
|
|
|
}, error);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
, fetchGenres = function (success, error, dbId) {
|
|
|
|
|
if (_.isUndefined(dbId) || !_.isNumeric(dbId)) {
|
|
|
|
|
dbId = 1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return fetchBrowse(success, error, dbId, "genres");
|
|
|
|
|
}
|
|
|
|
|
, fetchContentTypes: function (success, error) {
|
|
|
|
|
var that = this
|
|
|
|
|
;
|
|
|
|
|
|
|
|
|
|
, fetchArtists = function (success, error, dbId) {
|
|
|
|
|
if (_.isUndefined(dbId) || !_.isNumeric(dbId)) {
|
|
|
|
|
dbId = 1;
|
|
|
|
|
that.fetchXHR("/content-codes", function __fetchContentTypesCallback(content) {
|
|
|
|
|
that.collections.contentCodes = new DMAPModel(content);
|
|
|
|
|
if (_.isFunction(success)) {
|
|
|
|
|
success(that.collections.contentCodes);
|
|
|
|
|
}
|
|
|
|
|
}, error);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return fetchBrowse(success, error, dbId, "artists");
|
|
|
|
|
}
|
|
|
|
|
, fetchServerInfo: function (success, error) {
|
|
|
|
|
var that = this
|
|
|
|
|
;
|
|
|
|
|
|
|
|
|
|
, fetchAlbums = function (success, error, dbId) {
|
|
|
|
|
if (_.isUndefined(dbId) || !_.isNumeric(dbId)) {
|
|
|
|
|
dbId = 1;
|
|
|
|
|
}
|
|
|
|
|
that.fetchXHR("/server-info", function __fetchServerInfoCallback(content) {
|
|
|
|
|
that.collections.serverInfo = new DMAPModel(content, {
|
|
|
|
|
contentCodes: that.collections.contentCodes
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
return fetchBrowse(success, error, dbId, "albums");
|
|
|
|
|
}
|
|
|
|
|
if (_.isFunction(success)) {
|
|
|
|
|
success(that.collections.serverInfo);
|
|
|
|
|
}
|
|
|
|
|
}, error);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
, Client = Toolbox.Base.extend({
|
|
|
|
|
, fetchLogin: function (success, error) {
|
|
|
|
|
var that = this
|
|
|
|
|
;
|
|
|
|
|
|
|
|
|
|
constructor: function(attributes, options) {
|
|
|
|
|
that = this;
|
|
|
|
|
that.fetchXHR("/login", success, error);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_.extend(that, Client.defaults);
|
|
|
|
|
_.extend(that.attributes, attributes);
|
|
|
|
|
, fetchDatabases: function (success, error) {
|
|
|
|
|
var that = this
|
|
|
|
|
;
|
|
|
|
|
|
|
|
|
|
that.setAuth();
|
|
|
|
|
that.fetchXHR("/databases", function __fetchDatabasesCallback(content) {
|
|
|
|
|
that.collections.databasesInfo = new DMAPModel(content, {
|
|
|
|
|
contentCodes: that.collections.contentCodes
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
return this;
|
|
|
|
|
if (_.isFunction(success)) {
|
|
|
|
|
success(that.collections.databasesInfo);
|
|
|
|
|
}
|
|
|
|
|
}, error);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
, setAuth: function(username, password) {
|
|
|
|
|
if (!_.isUndefined(username)) {
|
|
|
|
|
that.attributes.username = username;
|
|
|
|
|
}
|
|
|
|
|
, fetchDatabase: function (success, error, dbId) {
|
|
|
|
|
var that = this
|
|
|
|
|
;
|
|
|
|
|
|
|
|
|
|
if (!_.isUndefined(password)) {
|
|
|
|
|
that.attributes.password = password;
|
|
|
|
|
if (_.isUndefined(dbId) || !_.isNumeric(dbId)) {
|
|
|
|
|
dbId = 1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!_.isEmpty(that.attributes.password) || !_.isEmpty(that.attributes.username)) {
|
|
|
|
|
that.attributes.basicauth = window.btoa((that.attributes.username || "") + ":" + (that.attributes.password || ""));
|
|
|
|
|
}
|
|
|
|
|
that.fetchXHR("/databases/" + dbId + "/items", function __fetchDatabaseCallback(content) {
|
|
|
|
|
that.collections.databases[dbId] = new DMAPModel(content, {
|
|
|
|
|
contentCodes: that.collections.contentCodes
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
if (_.isFunction(success)) {
|
|
|
|
|
success(that.collections.databases[dbId]);
|
|
|
|
|
}
|
|
|
|
|
}, error);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
, init: function () {
|
|
|
|
|
, fetchPlaylists: function (success, error, dbId) {
|
|
|
|
|
var that = this
|
|
|
|
|
;
|
|
|
|
|
|
|
|
|
|
that.console.debug("Fetching content types");
|
|
|
|
|
fetchContentTypes(function () {
|
|
|
|
|
that.console.debug("Fetching server info");
|
|
|
|
|
fetchServerInfo(function () {
|
|
|
|
|
that.console.debug("Fetching login");
|
|
|
|
|
fetchLogin(function () {
|
|
|
|
|
// TODO: Here we need to request /update for the database version
|
|
|
|
|
that.console.debug("Fetching databases info");
|
|
|
|
|
fetchDatabases(function () {
|
|
|
|
|
//that.console.debug("Fetching database 1");
|
|
|
|
|
//fetchDatabase(function () {
|
|
|
|
|
that.console.debug("Fetching playlists info");
|
|
|
|
|
fetchPlaylists(function () {
|
|
|
|
|
//that.console.debug("Fetching playlist 1 of db 1");
|
|
|
|
|
//fetchPlaylist(function () {
|
|
|
|
|
//that.console.debug("Fetching genres of db 1");
|
|
|
|
|
//fetchGenres(function () {
|
|
|
|
|
//that.console.debug("Fetching artists of db 1");
|
|
|
|
|
//fetchArtists(function () {
|
|
|
|
|
//that.console.debug("Fetching albums of db 1");
|
|
|
|
|
//fetchAlbums(function () {
|
|
|
|
|
that.trigger("inited", that.collections);
|
|
|
|
|
//});
|
|
|
|
|
//});
|
|
|
|
|
//});
|
|
|
|
|
//});
|
|
|
|
|
});
|
|
|
|
|
//});
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
if (_.isUndefined(dbId) || !_.isNumeric(dbId)) {
|
|
|
|
|
dbId = 1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
that.fetchXHR("/databases/" + dbId + "/containers", function __fetchPlaylistsCallback(content) {
|
|
|
|
|
that.collections.playlistsInfo = new DMAPModel(content, {
|
|
|
|
|
contentCodes: that.collections.contentCodes
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
return this;
|
|
|
|
|
if (_.isFunction(success)) {
|
|
|
|
|
success(that.collections.playlistsInfo);
|
|
|
|
|
}
|
|
|
|
|
}, error);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
,logout: function () {
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
, fetchPlaylist: function (success, error, playlistId, dbId) {
|
|
|
|
|
var that = this
|
|
|
|
|
;
|
|
|
|
|
|
|
|
|
|
, buildRequestUid: function(path, fields, query) {
|
|
|
|
|
if (_.isEmpty(path) || !_.isString(path)) {
|
|
|
|
|
throw new Error("INVALID_TYPE_ERROR");
|
|
|
|
|
if (_.isUndefined(playlistId) || !_.isNumeric(playlistId)) {
|
|
|
|
|
playlistId = 1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!_.isArray(fields)) {
|
|
|
|
|
fields = [];
|
|
|
|
|
if (_.isUndefined(dbId) || !_.isNumeric(dbId)) {
|
|
|
|
|
dbId = 1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!_.isArray(query)) {
|
|
|
|
|
query = [];
|
|
|
|
|
}
|
|
|
|
|
var requestUri = "/databases/"
|
|
|
|
|
;
|
|
|
|
|
|
|
|
|
|
requestUri += dbId;
|
|
|
|
|
requestUri += "/containers/";
|
|
|
|
|
requestUri += playlistId;
|
|
|
|
|
requestUri += "/items";
|
|
|
|
|
requestUri += "?meta=dmap.itemid,dmap.persistentid,dmap.containeritemid";
|
|
|
|
|
|
|
|
|
|
that.fetchXHR(requestUri, function __fetchPlaylistCallback(content) {
|
|
|
|
|
if (!_.isArray(that.collections.playlists[dbId])) {
|
|
|
|
|
that.collections.playlists[dbId] = [];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
that.collections.playlists[dbId][playlistId] = new DMAPModel(content, {
|
|
|
|
|
contentCodes: that.collections.contentCodes
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
if (_.isFunction(success)) {
|
|
|
|
|
success(that.collections.playlists[dbId][playlistId]);
|
|
|
|
|
}
|
|
|
|
|
}, error);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
,url: function (request) {
|
|
|
|
|
var uri = ""
|
|
|
|
|
, fetchBrowse: function (success, error, dbId, type) {
|
|
|
|
|
var that = this
|
|
|
|
|
;
|
|
|
|
|
|
|
|
|
|
if (typeof request === "string") {
|
|
|
|
|
uri = request; // Add leading slash here!
|
|
|
|
|
// TODO: We need to detect if the server supports browsing or not an implement our own otherwise.
|
|
|
|
|
if (_.isEmpty(type) || !_.isString(type) || !_.isArray(that.collections.browser[type])) {
|
|
|
|
|
throw new Error("INVALID_BROWSE_TYPE");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (_.isNumber(this.collections.session.id)) {
|
|
|
|
|
var prefix = "?";
|
|
|
|
|
var requestUri = "/databases/"
|
|
|
|
|
;
|
|
|
|
|
|
|
|
|
|
if (uri.indexOf("?") >= 0) {
|
|
|
|
|
prefix = "&";
|
|
|
|
|
requestUri += dbId;
|
|
|
|
|
requestUri += "/browse/";
|
|
|
|
|
requestUri += type;
|
|
|
|
|
requestUri += "?meta=dmap.itemid,dmap.persistentid,dmap.containeritemid";
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
that.fetchXHR(requestUri, function __fetchBrowseCallback(content) {
|
|
|
|
|
that.collections.browser[type][dbId] = new DMAPModel(content, {
|
|
|
|
|
contentCodes: that.collections.contentCodes
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
if (_.isFunction(success)) {
|
|
|
|
|
success(that.collections.browser[type][dbId]);
|
|
|
|
|
}
|
|
|
|
|
}, error);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
uri += prefix + "session-id=" + this.collections.session.id;
|
|
|
|
|
, fetchGenres: function (success, error, dbId) {
|
|
|
|
|
var that = this
|
|
|
|
|
;
|
|
|
|
|
|
|
|
|
|
if (_.isUndefined(dbId) || !_.isNumeric(dbId)) {
|
|
|
|
|
dbId = 1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return this.attributes.protocol + "://" + this.attributes.hostname + ":" + this.attributes.port + uri;
|
|
|
|
|
return that.fetchBrowse(success, error, dbId, "genres");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
, console: {
|
|
|
|
|
log: function() {
|
|
|
|
|
var args = arguments;
|
|
|
|
|
|
|
|
|
|
if (_.isString(args[0])) {
|
|
|
|
|
args[0] = that.url() + " " + args[0];
|
|
|
|
|
}
|
|
|
|
|
, fetchArtists: function (success, error, dbId) {
|
|
|
|
|
var that = this
|
|
|
|
|
;
|
|
|
|
|
|
|
|
|
|
console.log(args);
|
|
|
|
|
if (_.isUndefined(dbId) || !_.isNumeric(dbId)) {
|
|
|
|
|
dbId = 1;
|
|
|
|
|
}
|
|
|
|
|
, debug: function() {
|
|
|
|
|
var args = arguments;
|
|
|
|
|
|
|
|
|
|
if (_.isString(args[0])) {
|
|
|
|
|
args[0] = that.url() + " " + args[0];
|
|
|
|
|
}
|
|
|
|
|
return that.fetchBrowse(success, error, dbId, "artists");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
console.debug(args);
|
|
|
|
|
, fetchAlbums: function (success, error, dbId) {
|
|
|
|
|
var that = this
|
|
|
|
|
;
|
|
|
|
|
|
|
|
|
|
if (_.isUndefined(dbId) || !_.isNumeric(dbId)) {
|
|
|
|
|
dbId = 1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return that.fetchBrowse(success, error, dbId, "albums");
|
|
|
|
|
}
|
|
|
|
|
}, {
|
|
|
|
|
|
|
|
|
|
}, Backbone.Events), {
|
|
|
|
|
defaults: {
|
|
|
|
|
attributes: {
|
|
|
|
|
protocol: window.location.protocol.replace(":", "")
|
|
|
|
@ -376,6 +436,7 @@ define([
|
|
|
|
|
, timedout: 2
|
|
|
|
|
, unauthorized: 3
|
|
|
|
|
, connected: 4
|
|
|
|
|
, loaded: 5
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
, collections: {
|
|
|
|
@ -406,7 +467,7 @@ define([
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
_.extend(Client.prototype, Backbone.Events, {});
|
|
|
|
|
//_.extend(Client.prototype, Backbone.Events);
|
|
|
|
|
|
|
|
|
|
return Client;
|
|
|
|
|
});
|