mirror of
https://github.com/tomahawk-player/tomahawk.git
synced 2025-08-06 06:07:37 +02:00
Merge changes in tomahawk.js from tomahawk-player/tomahawk-android
This commit is contained in:
@@ -42,17 +42,18 @@ Tomahawk.apiVersion = "0.2.2";
|
|||||||
//Statuses considered a success for HTTP request
|
//Statuses considered a success for HTTP request
|
||||||
var httpSuccessStatuses = [200, 201];
|
var httpSuccessStatuses = [200, 201];
|
||||||
|
|
||||||
// install RSVP.Promise as global Promise
|
// install RSVP error handler for uncaught(!) errors
|
||||||
if(window.Promise === undefined) {
|
RSVP.on('error', function (reason) {
|
||||||
window.Promise = window.RSVP.Promise;
|
var resolverName = "";
|
||||||
window.RSVP.on('error', function(reason) {
|
if (Tomahawk.resolver.instance) {
|
||||||
|
resolverName = Tomahawk.resolver.instance.settings.name + " - ";
|
||||||
|
}
|
||||||
if (reason) {
|
if (reason) {
|
||||||
console.error(reason.message, reason);
|
console.error(resolverName + 'Uncaught error:' + JSON.stringify(reason));
|
||||||
} else {
|
} else {
|
||||||
console.error('Error: error thrown from RSVP but it was empty');
|
console.error(resolverName + 'Uncaught error: error thrown from RSVP but it was empty');
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Compares versions strings
|
* Compares versions strings
|
||||||
@@ -231,24 +232,40 @@ var TomahawkResolver = {
|
|||||||
collection: function () {
|
collection: function () {
|
||||||
return {};
|
return {};
|
||||||
},
|
},
|
||||||
getStreamUrl: function(params) {
|
|
||||||
Tomahawk.reportStreamUrl(params.qid, params.url);
|
|
||||||
},
|
|
||||||
_testConfig: function (config) {
|
_testConfig: function (config) {
|
||||||
return Promise.resolve(this.testConfig(config)).then(function() {
|
return Promise.resolve(this.testConfig(config)).then(function() {
|
||||||
return { result: Tomahawk.ConfigTestResultType.Success };
|
return { result: Tomahawk.ConfigTestResultType.Success };
|
||||||
});
|
});
|
||||||
},
|
},
|
||||||
testConfig: function () {
|
testConfig: function () {
|
||||||
|
this.configTest();
|
||||||
|
},
|
||||||
|
getStreamUrl: function (qid, url) {
|
||||||
|
Tomahawk.reportStreamUrl(qid, url);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
Tomahawk.Resolver = {
|
||||||
Tomahawk.Resolver = Tomahawk.extend(TomahawkResolver, {
|
init: function () {
|
||||||
|
},
|
||||||
|
scriptPath: function () {
|
||||||
|
return Tomahawk.resolverData().scriptPath;
|
||||||
|
},
|
||||||
|
getConfigUi: function () {
|
||||||
|
return {};
|
||||||
|
},
|
||||||
|
getUserConfig: function () {
|
||||||
|
return JSON.parse(window.localStorage[this.scriptPath()] || "{}");
|
||||||
|
},
|
||||||
saveUserConfig: function () {
|
saveUserConfig: function () {
|
||||||
window.localStorage[this.scriptPath()] = JSON.stringify(Tomahawk.resolverData().config);
|
window.localStorage[this.scriptPath()] = JSON.stringify(Tomahawk.resolverData().config);
|
||||||
this.newConfigSaved(Tomahawk.resolverData().config);
|
this.newConfigSaved(Tomahawk.resolverData().config);
|
||||||
},
|
},
|
||||||
|
newConfigSaved: function () {
|
||||||
|
},
|
||||||
|
getStreamUrl: function (params) {
|
||||||
|
return params;
|
||||||
|
},
|
||||||
|
|
||||||
_convertUrls: function(results) {
|
_convertUrls: function(results) {
|
||||||
var that = this;
|
var that = this;
|
||||||
@@ -321,7 +338,7 @@ Tomahawk.Resolver = Tomahawk.extend(TomahawkResolver, {
|
|||||||
return { result: Tomahawk.ConfigTestResultType.Success };
|
return { result: Tomahawk.ConfigTestResultType.Success };
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
});
|
};
|
||||||
|
|
||||||
/**** begin example implementation of a resolver ****/
|
/**** begin example implementation of a resolver ****/
|
||||||
|
|
||||||
@@ -478,7 +495,7 @@ Tomahawk.asyncRequestIdCounter = 0;
|
|||||||
Tomahawk.asyncRequestCallbacks = {};
|
Tomahawk.asyncRequestCallbacks = {};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Pass the natively retrived reply back to the javascript callback
|
* Pass the natively retrieved reply back to the javascript callback
|
||||||
* and augment the fake XMLHttpRequest object.
|
* and augment the fake XMLHttpRequest object.
|
||||||
*
|
*
|
||||||
* Internal use only!
|
* Internal use only!
|
||||||
@@ -585,9 +602,15 @@ Tomahawk.ajax = function(url, settings) {
|
|||||||
var str = [];
|
var str = [];
|
||||||
for(var p in obj) {
|
for(var p in obj) {
|
||||||
if(obj[p] !== undefined) {
|
if(obj[p] !== undefined) {
|
||||||
|
if (Array.isArray(obj[p])) {
|
||||||
|
for (var i = 0; i < obj[p].length; i++) {
|
||||||
|
str.push(encodeURIComponent(p) + "=" + encodeURIComponent(obj[p][i]));
|
||||||
|
}
|
||||||
|
} else {
|
||||||
str.push(encodeURIComponent(p) + "=" + encodeURIComponent(obj[p]));
|
str.push(encodeURIComponent(p) + "=" + encodeURIComponent(obj[p]));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
str.sort();
|
str.sort();
|
||||||
|
|
||||||
@@ -887,6 +910,27 @@ Tomahawk.PluginManager = {
|
|||||||
pluginManager.resolve[requestId] = resolve;
|
pluginManager.resolve[requestId] = resolve;
|
||||||
Tomahawk.resolver.instance.tracks(requestId, params.artist, params.album);
|
Tomahawk.resolver.instance.tracks(requestId, params.artist, params.album);
|
||||||
});
|
});
|
||||||
|
} else if (methodName == 'lookupUrl') {
|
||||||
|
return new RSVP.Promise(function (resolve, reject) {
|
||||||
|
pluginManager.resolve[params.url] = resolve;
|
||||||
|
Tomahawk.resolver.instance.lookupUrl(params.url);
|
||||||
|
});
|
||||||
|
} else if (methodName == 'getStreamUrl') {
|
||||||
|
return new RSVP.Promise(function (resolve, reject) {
|
||||||
|
pluginManager.resolve[requestId] = resolve;
|
||||||
|
Tomahawk.resolver.instance.getStreamUrl(requestId, params.url);
|
||||||
|
});
|
||||||
|
} else if (methodName == 'resolve') {
|
||||||
|
return new RSVP.Promise(function (resolve, reject) {
|
||||||
|
pluginManager.resolve[requestId] = resolve;
|
||||||
|
Tomahawk.resolver.instance.resolve(requestId, params.artist,
|
||||||
|
params.album, params.track);
|
||||||
|
});
|
||||||
|
} else if (methodName == 'search') {
|
||||||
|
return new RSVP.Promise(function (resolve, reject) {
|
||||||
|
pluginManager.resolve[requestId] = resolve;
|
||||||
|
Tomahawk.resolver.instance.search(requestId, params.query);
|
||||||
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -898,17 +942,10 @@ Tomahawk.PluginManager = {
|
|||||||
|
|
||||||
invoke: function (requestId, objectId, methodName, params ) {
|
invoke: function (requestId, objectId, methodName, params ) {
|
||||||
Promise.resolve(this.invokeSync(requestId, objectId, methodName, params)).then(function (result) {
|
Promise.resolve(this.invokeSync(requestId, objectId, methodName, params)).then(function (result) {
|
||||||
if (typeof result === 'object') {
|
|
||||||
Tomahawk.reportScriptJobResults({
|
Tomahawk.reportScriptJobResults({
|
||||||
requestId: requestId,
|
requestId: requestId,
|
||||||
data: result
|
data: result
|
||||||
});
|
});
|
||||||
} else {
|
|
||||||
Tomahawk.reportScriptJobResults({
|
|
||||||
requestId: requestId,
|
|
||||||
error: "Scripts need to return objects for requests: methodName: " + methodName + " params: " + JSON.stringify(params)
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}, function (error) {
|
}, function (error) {
|
||||||
Tomahawk.reportScriptJobResults({
|
Tomahawk.reportScriptJobResults({
|
||||||
requestId: requestId,
|
requestId: requestId,
|
||||||
@@ -918,6 +955,32 @@ Tomahawk.PluginManager = {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
Tomahawk.NativeScriptJobManager = {
|
||||||
|
idCounter: 0,
|
||||||
|
deferreds: {},
|
||||||
|
invoke: function (methodName, params) {
|
||||||
|
var requestId = this.idCounter++;
|
||||||
|
Tomahawk.invokeNativeScriptJob(requestId, methodName, JSON.stringify(params));
|
||||||
|
this.deferreds[requestId] = RSVP.defer();
|
||||||
|
return this.deferreds[requestId].promise;
|
||||||
|
},
|
||||||
|
reportNativeScriptJobResult: function (requestId, result) {
|
||||||
|
var deferred = this.deferreds[requestId];
|
||||||
|
if (!deferred) {
|
||||||
|
Tomahawk.log("Deferred object with the given requestId is not present!");
|
||||||
|
}
|
||||||
|
deferred.resolve(result);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
Tomahawk.UrlType = {
|
||||||
|
Any: 0,
|
||||||
|
Playlist: 1,
|
||||||
|
Track: 2,
|
||||||
|
Album: 3,
|
||||||
|
Artist: 4,
|
||||||
|
XspfPlaylist: 5
|
||||||
|
};
|
||||||
|
|
||||||
Tomahawk.ConfigTestResultType = {
|
Tomahawk.ConfigTestResultType = {
|
||||||
Other: 0,
|
Other: 0,
|
||||||
@@ -1244,7 +1307,11 @@ Tomahawk.Collection = {
|
|||||||
"FOREIGN KEY(artistId) REFERENCES artists(_id)," +
|
"FOREIGN KEY(artistId) REFERENCES artists(_id)," +
|
||||||
"FOREIGN KEY(albumId) REFERENCES albums(_id))", []);
|
"FOREIGN KEY(albumId) REFERENCES albums(_id))", []);
|
||||||
});
|
});
|
||||||
|
//m.migration(2, function (tx) {
|
||||||
|
// //Tomahawk.log("Migrating to db version 2");
|
||||||
|
//});
|
||||||
|
|
||||||
|
m.execute();
|
||||||
}
|
}
|
||||||
resolve(collection.cachedDbs[id]);
|
resolve(collection.cachedDbs[id]);
|
||||||
});
|
});
|
||||||
@@ -1862,3 +1929,54 @@ Tomahawk.addArtistResults = Tomahawk.addAlbumResults = Tomahawk.addAlbumTrackRes
|
|||||||
Tomahawk.PluginManager.resolve[result.qid](result);
|
Tomahawk.PluginManager.resolve[result.qid](result);
|
||||||
delete Tomahawk.PluginManager.resolve[result.qid];
|
delete Tomahawk.PluginManager.resolve[result.qid];
|
||||||
};
|
};
|
||||||
|
|
||||||
|
Tomahawk.addTrackResults = function (result) {
|
||||||
|
Tomahawk.PluginManager.resolve[result.qid](result.results);
|
||||||
|
delete Tomahawk.PluginManager.resolve[result.qid];
|
||||||
|
};
|
||||||
|
|
||||||
|
Tomahawk.reportStreamUrl = function (qid, streamUrl, headers) {
|
||||||
|
Tomahawk.PluginManager.resolve[qid]({
|
||||||
|
url: streamUrl,
|
||||||
|
headers: headers
|
||||||
|
});
|
||||||
|
delete Tomahawk.PluginManager.resolve[qid];
|
||||||
|
};
|
||||||
|
|
||||||
|
Tomahawk.addUrlResult = function(url, result) {
|
||||||
|
/* Merge the whole mess into one consistent result which is independent of type
|
||||||
|
var cleanResult = {
|
||||||
|
type: result.type,
|
||||||
|
guid: result.guid,
|
||||||
|
info: result.info,
|
||||||
|
creator: result.creator,
|
||||||
|
linkUrl: result.url
|
||||||
|
};
|
||||||
|
if (cleanResult.type == "track") {
|
||||||
|
cleanResult.track = result.title;
|
||||||
|
cleanResult.artist = result.artist;
|
||||||
|
} else if (cleanResult.type == "artist") {
|
||||||
|
cleanResult.artist = result.name;
|
||||||
|
} else if (cleanResult.type == "album") {
|
||||||
|
cleanResult.album = result.name;
|
||||||
|
cleanResult.artist = result.artist;
|
||||||
|
} else if (cleanResult.type == "playlist") {
|
||||||
|
cleanResult.title = result.title;
|
||||||
|
} else if (cleanResult.type == "xspf-url") {
|
||||||
|
cleanResult.url = result.url;
|
||||||
|
}
|
||||||
|
if (result.tracks) {
|
||||||
|
cleanResult.tracks = [];
|
||||||
|
var i;
|
||||||
|
for (i=0;i<result.tracks.length;i++) {
|
||||||
|
var cleanTrack = {
|
||||||
|
track: result.tracks[i].title,
|
||||||
|
artist: result.tracks[i].artist
|
||||||
|
};
|
||||||
|
cleanResult.push(cleanTrack)
|
||||||
|
}
|
||||||
|
Tomahawk.PluginManager.resolve[url](cleanResult);
|
||||||
|
*/
|
||||||
|
Tomahawk.PluginManager.resolve[url](result);
|
||||||
|
delete Tomahawk.PluginManager.resolve[url];
|
||||||
|
};
|
||||||
|
Reference in New Issue
Block a user