|
|
|
@ -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
|
|
|
|
|
|
|
|
|
|
if (!_.isEmpty(that.attributes.basicauth)) {
|
|
|
|
|
xhr.withCredentials = true;
|
|
|
|
|
xhr.setRequestHeader("Authorization", "Basic " + that.attributes.basicauth);
|
|
|
|
|
}
|
|
|
|
|
, setAuth: function(username, password) {
|
|
|
|
|
if (!_.isUndefined(username)) {
|
|
|
|
|
this.attributes.username = username;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
if (!_.isUndefined(password)) {
|
|
|
|
|
this.attributes.password = password;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
xhr.send();
|
|
|
|
|
} catch (e) {
|
|
|
|
|
console.error(e);
|
|
|
|
|
callback(e, null);
|
|
|
|
|
if (!_.isEmpty(this.attributes.password) || !_.isEmpty(this.attributes.username)) {
|
|
|
|
|
this.attributes.basicauth = window.btoa((this.attributes.username || "") + ":" + (this.attributes.password || ""));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
, 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);
|
|
|
|
|
}
|
|
|
|
|
, init: function () {
|
|
|
|
|
var that = this
|
|
|
|
|
;
|
|
|
|
|
|
|
|
|
|
, fetchServerInfo = function (success, error) {
|
|
|
|
|
fetchXHR("/server-info", function __fetchServerInfoCallback(content) {
|
|
|
|
|
that.collections.serverInfo = new DMAPModel(content, {
|
|
|
|
|
contentCodes: that.collections.contentCodes
|
|
|
|
|
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);
|
|
|
|
|
//});
|
|
|
|
|
//});
|
|
|
|
|
//});
|
|
|
|
|
//});
|
|
|
|
|
//});
|
|
|
|
|
//});
|
|
|
|
|
//});
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
if (_.isFunction(success)) {
|
|
|
|
|
success(content);
|
|
|
|
|
}
|
|
|
|
|
}, error);
|
|
|
|
|
}
|
|
|
|
|
return this;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
, fetchLogin = function (success, error) {
|
|
|
|
|
fetchXHR("/login", success, error);
|
|
|
|
|
}
|
|
|
|
|
, deinit: function () {
|
|
|
|
|
var that = this
|
|
|
|
|
;
|
|
|
|
|
|
|
|
|
|
, fetchDatabases = function (success, error) {
|
|
|
|
|
fetchXHR("/databases", function __fetchDatabasesCallback(content) {
|
|
|
|
|
that.collections.databasesInfo = new DMAPModel(content, {
|
|
|
|
|
contentCodes: that.collections.contentCodes
|
|
|
|
|
});
|
|
|
|
|
this.logout();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (_.isFunction(success)) {
|
|
|
|
|
success(content);
|
|
|
|
|
}
|
|
|
|
|
}, error);
|
|
|
|
|
}
|
|
|
|
|
,logout: function () {
|
|
|
|
|
|
|
|
|
|
, fetchDatabase = function (success, error, dbId) {
|
|
|
|
|
if (_.isUndefined(dbId) || !_.isNumeric(dbId)) {
|
|
|
|
|
dbId = 1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fetchXHR("/databases/" + dbId + "/items", function __fetchDatabaseCallback(content) {
|
|
|
|
|
that.collections.databases[dbId] = 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);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
, fetchPlaylists = function (success, error, dbId) {
|
|
|
|
|
if (_.isUndefined(dbId) || !_.isNumeric(dbId)) {
|
|
|
|
|
dbId = 1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fetchXHR("/databases/" + dbId + "/containers", function __fetchPlaylistsCallback(content) {
|
|
|
|
|
that.collections.playlistsInfo = new DMAPModel(content, {
|
|
|
|
|
contentCodes: that.collections.contentCodes
|
|
|
|
|
});
|
|
|
|
|
if (!_.isArray(fields)) {
|
|
|
|
|
fields = [];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (_.isFunction(success)) {
|
|
|
|
|
success(content);
|
|
|
|
|
if (!_.isArray(query)) {
|
|
|
|
|
query = [];
|
|
|
|
|
}
|
|
|
|
|
}, error);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
, fetchPlaylist = function (success, error, playlistId, dbId) {
|
|
|
|
|
if (_.isUndefined(playlistId) || !_.isNumeric(playlistId)) {
|
|
|
|
|
playlistId = 1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (_.isUndefined(dbId) || !_.isNumeric(dbId)) {
|
|
|
|
|
dbId = 1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var requestUri = "/databases/"
|
|
|
|
|
;
|
|
|
|
|
|
|
|
|
|
requestUri += dbId;
|
|
|
|
|
requestUri += "/containers/";
|
|
|
|
|
requestUri += playlistId;
|
|
|
|
|
requestUri += "/items";
|
|
|
|
|
requestUri += "?meta=dmap.itemid,dmap.persistentid,dmap.containeritemid";
|
|
|
|
|
,url: function (request) {
|
|
|
|
|
var that = this
|
|
|
|
|
, uri = ""
|
|
|
|
|
;
|
|
|
|
|
|
|
|
|
|
fetchXHR(requestUri, function __fetchPlaylistCallback(content) {
|
|
|
|
|
if (!_.isArray(that.collections.playlists[dbId])) {
|
|
|
|
|
that.collections.playlists[dbId] = [];
|
|
|
|
|
if (typeof request === "string") {
|
|
|
|
|
uri = request; // Add leading slash here!
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
that.collections.playlists[dbId][playlistId] = new DMAPModel(content, {
|
|
|
|
|
contentCodes: that.collections.contentCodes
|
|
|
|
|
});
|
|
|
|
|
if (_.isNumber(this.collections.session.id)) {
|
|
|
|
|
var prefix = "?";
|
|
|
|
|
|
|
|
|
|
if (_.isFunction(success)) {
|
|
|
|
|
success(content);
|
|
|
|
|
if (uri.indexOf("?") >= 0) {
|
|
|
|
|
prefix = "&";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
uri += prefix + "session-id=" + this.collections.session.id;
|
|
|
|
|
}
|
|
|
|
|
}, error);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return this.attributes.protocol + "://" + this.urlHost() + uri;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
, 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");
|
|
|
|
|
, 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 += "/browse/";
|
|
|
|
|
requestUri += type;
|
|
|
|
|
requestUri += "?meta=dmap.itemid,dmap.persistentid,dmap.containeritemid";
|
|
|
|
|
var xhr = new XMLHttpRequest();
|
|
|
|
|
var contentType = "text/plain; charset=utf-8";
|
|
|
|
|
|
|
|
|
|
if (typeof responseType === "function") {
|
|
|
|
|
callback = responseType;
|
|
|
|
|
responseType = "arraybuffer";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fetchXHR(requestUri, function __fetchBrowseCallback(content) {
|
|
|
|
|
that.collections.browser[type][dbId] = 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);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
, fetchGenres = function (success, error, dbId) {
|
|
|
|
|
if (_.isUndefined(dbId) || !_.isNumeric(dbId)) {
|
|
|
|
|
dbId = 1;
|
|
|
|
|
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);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return fetchBrowse(success, error, dbId, "genres");
|
|
|
|
|
}
|
|
|
|
|
, 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];
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
, fetchArtists = function (success, error, dbId) {
|
|
|
|
|
if (_.isUndefined(dbId) || !_.isNumeric(dbId)) {
|
|
|
|
|
dbId = 1;
|
|
|
|
|
error(err, content);
|
|
|
|
|
} else {
|
|
|
|
|
success(content);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return fetchBrowse(success, error, dbId, "artists");
|
|
|
|
|
}
|
|
|
|
|
, fetchContentTypes: function (success, error) {
|
|
|
|
|
var that = this
|
|
|
|
|
;
|
|
|
|
|
|
|
|
|
|
, fetchAlbums = 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, "albums");
|
|
|
|
|
}
|
|
|
|
|
, fetchServerInfo: function (success, error) {
|
|
|
|
|
var that = this
|
|
|
|
|
;
|
|
|
|
|
|
|
|
|
|
that.fetchXHR("/server-info", function __fetchServerInfoCallback(content) {
|
|
|
|
|
that.collections.serverInfo = new DMAPModel(content, {
|
|
|
|
|
contentCodes: that.collections.contentCodes
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
, Client = Toolbox.Base.extend({
|
|
|
|
|
if (_.isFunction(success)) {
|
|
|
|
|
success(that.collections.serverInfo);
|
|
|
|
|
}
|
|
|
|
|
}, error);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
constructor: function(attributes, options) {
|
|
|
|
|
that = this;
|
|
|
|
|
, fetchLogin: function (success, error) {
|
|
|
|
|
var that = this
|
|
|
|
|
;
|
|
|
|
|
|
|
|
|
|
_.extend(that, Client.defaults);
|
|
|
|
|
_.extend(that.attributes, attributes);
|
|
|
|
|
that.fetchXHR("/login", success, error);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
that.setAuth();
|
|
|
|
|
, fetchDatabases: function (success, error) {
|
|
|
|
|
var that = this
|
|
|
|
|
;
|
|
|
|
|
|
|
|
|
|
return this;
|
|
|
|
|
that.fetchXHR("/databases", function __fetchDatabasesCallback(content) {
|
|
|
|
|
that.collections.databasesInfo = new DMAPModel(content, {
|
|
|
|
|
contentCodes: that.collections.contentCodes
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
});
|