1
0
mirror of https://github.com/tomahawk-player/tomahawk.git synced 2025-08-12 00:54:20 +02:00

Some style fixes in tomahawk.js

This commit is contained in:
Enno Gottschalk
2015-10-13 18:36:49 +02:00
parent ee8c687577
commit f89439a46a

View File

@@ -62,8 +62,12 @@ RSVP.on('error', function (reason) {
* (version1 > version2) == 1
*/
Tomahawk.versionCompare = function (version1, version2) {
var v1 = version1.split('.').map(function (item) { return parseInt(item); });
var v2 = version2.split('.').map(function (item) { return parseInt(item); });
var v1 = version1.split('.').map(function (item) {
return parseInt(item);
});
var v2 = version2.split('.').map(function (item) {
return parseInt(item);
});
var length = Math.max(v1.length, v2.length);
var i = 0;
@@ -104,7 +108,6 @@ Tomahawk.atLeastVersion = function (version) {
return (Tomahawk.versionCompare(Tomahawk.apiVersion, version) >= 0);
};
Tomahawk.resolver = {
scriptPath: Tomahawk.resolverData().scriptPath
};
@@ -133,10 +136,9 @@ Tomahawk.htmlDecode = (function() {
})();
Tomahawk.dumpResult = function (result) {
var results = result.results,
i = 0;
var results = result.results;
Tomahawk.log("Dumping " + results.length + " results for query " + result.qid + "...");
for (i = 0; i < results.length; i++) {
for (var i = 0; i < results.length; i++) {
Tomahawk.log(results[i].artist + " - " + results[i].track + " | " + results[i].url);
}
@@ -156,7 +158,6 @@ Tomahawk.extend = function (object, members) {
return newObject;
};
var TomahawkResolverCapability = {
NullCapability: 0,
Browsable: 1,
@@ -281,14 +282,19 @@ Tomahawk.Resolver = {
var that = this;
var collectionPromises = [];
Tomahawk.collections.forEach(function (col) {
if(col.resolve)
if (col.resolve) {
collectionPromises.push(col.resolve({artist: artist, album: album, track: title}));
}
});
Promise.all(collectionPromises).then(function (collectionResults) {
var merged = [];
return merged.concat.apply(merged, collectionResults);
}).then(function (collectionResults) {
Promise.resolve(that.resolve({artist: artist, album: album, track:title})).then(function(results){
Promise.resolve(that.resolve({
artist: artist,
album: album,
track: title
})).then(function (results) {
Tomahawk.addTrackResults({
'qid': qid,
'results': that._convertUrls(results.concat(collectionResults))
@@ -297,8 +303,7 @@ Tomahawk.Resolver = {
});
},
_adapter_init: function ()
{
_adapter_init: function () {
this._urlProtocol = this.settings.name.replace(/[^a-zA-Z]/g, '').toLowerCase();
Tomahawk.addCustomUrlHandler(this._urlProtocol, 'getStreamUrl', true);
Tomahawk.log('Registered custom url handler for protocol "' + this._urlProtocol + '"');
@@ -312,13 +317,13 @@ Tomahawk.Resolver = {
});
},
_adapter_search: function (qid, query)
{
_adapter_search: function (qid, query) {
var that = this;
var collectionPromises = [];
Tomahawk.collections.forEach(function (col) {
if(col.search)
if (col.search) {
collectionPromises.push(col.search({query: query}));
}
});
Promise.all(collectionPromises).then(function (collectionResults) {
var merged = [];
@@ -558,7 +563,8 @@ Tomahawk.asyncRequest = function (url, callback, extraHeaders, options) {
}
}
xmlHttpRequest.onreadystatechange = function () {
if (xmlHttpRequest.readyState == 4 && httpSuccessStatuses.indexOf(xmlHttpRequest.status) != -1) {
if (xmlHttpRequest.readyState == 4
&& httpSuccessStatuses.indexOf(xmlHttpRequest.status) != -1) {
callback.call(window, xmlHttpRequest);
} else if (xmlHttpRequest.readyState === 4) {
Tomahawk.log("Failed to do " + method + " request: to: " + url);
@@ -624,7 +630,8 @@ Tomahawk.ajax = function(url, settings) {
settings.data = JSON.stringify(settings.data);
settings.contentType = settings.contentType || 'application/json';
} else {
throw new Error("Tomahawk.ajax: unknown dataFormat requested: " + settings.dataFormat);
throw new Error("Tomahawk.ajax: unknown dataFormat requested: "
+ settings.dataFormat);
}
} else {
throw new Error("Tomahawk.ajax: data should be either object or string");
@@ -843,8 +850,12 @@ Tomahawk.localStorage = Tomahawk.localStorage || {
// some aliases
Tomahawk.setTimeout = Tomahawk.setTimeout || window.setTimeout;
Tomahawk.setInterval = Tomahawk.setInterval || window.setInterval;
Tomahawk.base64Decode = function(a) { return window.atob(a); };
Tomahawk.base64Encode = function(b) { return window.btoa(b); };
Tomahawk.base64Decode = function (a) {
return window.atob(a);
};
Tomahawk.base64Encode = function (b) {
return window.btoa(b);
};
Tomahawk.PluginManager = {
objects: {},
@@ -858,8 +869,7 @@ Tomahawk.PluginManager = {
},
registerPlugin: function (type, object) {
this.objects[this.identifyObject(object)] = object;
if (type === 'collection')
{
if (type === 'collection') {
Tomahawk.collections.push(object);
}
@@ -894,7 +904,8 @@ Tomahawk.PluginManager = {
}
if (typeof this.objects[objectId][methodName] === 'function') {
if (!Tomahawk.resolver.instance.apiVersion || Tomahawk.resolver.instance.apiVersion < 0.9) {
if (!Tomahawk.resolver.instance.apiVersion
|| Tomahawk.resolver.instance.apiVersion < 0.9) {
if (methodName == 'artists') {
return new Promise(function (resolve, reject) {
pluginManager.resolve[requestId] = resolve;
@@ -941,7 +952,8 @@ Tomahawk.PluginManager = {
},
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) {
Tomahawk.reportScriptJobResults({
requestId: requestId,
data: result
@@ -1257,8 +1269,6 @@ Tomahawk.Collection = {
Transaction: function (collection, id) {
this.ensureDb = function () {
var that = this;
return new RSVP.Promise(function (resolve, reject) {
if (!collection.cachedDbs.hasOwnProperty(id)) {
Tomahawk.log("Opening database");
@@ -1332,14 +1342,14 @@ Tomahawk.Collection = {
var that = this;
that.stmtsToResolve = that.statements.length;
that.results = that.statements.slice();
Tomahawk.log('Executing ' + that.stmtsToResolve + ' deffered SQL statements in transaction');
Tomahawk.log('Executing ' + that.stmtsToResolve
+ ' deferred SQL statements in transaction');
return new Promise(function (resolve, reject) {
if (that.statements.length == 0)
if (that.statements.length == 0) {
resolve([]);
else{
} else {
that.db.transaction(function (tx) {
for (var i = 0; i < that.statements.length; ++i)
{
for (var i = 0; i < that.statements.length; ++i) {
var stmt = that.statements[i];
tx.executeSql(stmt.statement, stmt.args,
(function () {
@@ -1347,8 +1357,7 @@ Tomahawk.Collection = {
//capture value of i
var originalI = i;
return function (tx, results) {
if (typeof that.statements[originalI].map !== 'undefined')
{
if (typeof that.statements[originalI].map !== 'undefined') {
var map = that.statements[originalI].map;
that.results[originalI] = [];
for (var ii = 0; ii < results.rows.length; ii++) {
@@ -1357,11 +1366,11 @@ Tomahawk.Collection = {
));
}
}
else
else {
that.results[originalI] = results;
}
that.stmtsToResolve--;
if(that.stmtsToResolve == 0)
{
if (that.stmtsToResolve == 0) {
that.statements = [];
resolve(that.results);
}
@@ -1377,7 +1386,7 @@ Tomahawk.Collection = {
});
}
});
},
};
this.sql = function (sqlStatement, sqlArgs, mapFunction) {
this.statements.push({statement: sqlStatement, args: sqlArgs, map: mapFunction});
@@ -1435,6 +1444,7 @@ Tomahawk.Collection = {
};
},
addTracks: function (params) {
var that = this;
var id = params.id;
@@ -1524,7 +1534,6 @@ Tomahawk.Collection = {
}
}).then(function () {
// Insert all albums
var promises = [];
for (var i = 0; i < tracks.length; i++) {
(function (track) {
var albumArtistId = cachedAlbumArtists[track.album].albumArtistId;
@@ -1558,7 +1567,6 @@ Tomahawk.Collection = {
}
}).then(function () {
// Now we are ready to insert the tracks
var promises = [];
for (var i = 0; i < tracks.length; i++) {
(function (track) {
// Get all relevant ids that we stored in the previous steps
@@ -1647,9 +1655,9 @@ Tomahawk.Collection = {
},
_fuzzyIndexIdsToTracks: function (resultIds, id) {
var that = this;
if(typeof id === 'undefined')
if (typeof id === 'undefined') {
id = this.settings.id;
}
var t = new Tomahawk.Collection.Transaction(this, id);
return t.beginTransaction().then(function () {
var mapFn = function (row) {
@@ -1698,17 +1706,11 @@ Tomahawk.Collection = {
},
resolve: function (params) {
var id = params.id;
if(typeof id === 'undefined')
id = this.settings.id;
var resultIds = Tomahawk.resolveFromFuzzyIndex(params.artist, params.album, params.track);
return this._fuzzyIndexIdsToTracks(resultIds);
},
search: function (params) {
var id = params.id;
if(typeof id === 'undefined')
id = this.settings.id;
var resultIds = Tomahawk.searchFuzzyIndex(params.query);
return this._fuzzyIndexIdsToTracks(resultIds);
},
@@ -1716,8 +1718,9 @@ Tomahawk.Collection = {
tracks: function (params, where) {
//TODO filter/where support
var id = params.id;
if(typeof id === 'undefined')
if (typeof id === 'undefined') {
id = this.settings.id;
}
var t = new Tomahawk.Collection.Transaction(this, id);
return t.beginTransaction().then(function () {
@@ -1760,8 +1763,9 @@ Tomahawk.Collection = {
albums: function (params, where) {
//TODO filter/where support
var id = params.id;
if(typeof id === 'undefined')
if (typeof id === 'undefined') {
id = this.settings.id;
}
var t = new Tomahawk.Collection.Transaction(this, id);
return t.beginTransaction().then(function () {
@@ -1789,8 +1793,12 @@ Tomahawk.Collection = {
return (e.albumArtist != '' && e.album != '');
});
return {
artists: results.map(function(i){ return i.albumArtist;}),
albums: results.map(function(i){ return i.album;})
artists: results.map(function (i) {
return i.albumArtist;
}),
albums: results.map(function (i) {
return i.album;
})
};
});
},
@@ -1798,13 +1806,13 @@ Tomahawk.Collection = {
artists: function (params) {
//TODO filter/where support
var id = params.id;
if(typeof id === 'undefined')
if (typeof id === 'undefined') {
id = this.settings.id;
}
var t = new Tomahawk.Collection.Transaction(this, id);
return t.beginTransaction().then(function () {
var mapFn = function(r)
{
var mapFn = function (r) {
return r.artist;
};
t.sqlSelect("artists", mapFn, ["artist", "artistDisambiguation"]);
@@ -1836,24 +1844,27 @@ Tomahawk.Collection = {
artistAlbums: function (params) {
//TODO filter/where support
var id = params.id;
if(typeof id === 'undefined')
if (typeof id === 'undefined') {
id = this.settings.id;
}
var artist = params.artist;
//var artistDisambiguation = params.artistDisambiguation;
var that = this;
var t = new Tomahawk.Collection.Transaction(this, id);
return t.beginTransaction().then(function () {
t.sqlSelect("artists",function(r){return r._id;}, ["_id"], {
t.sqlSelect("artists", function (r) {
return r._id;
}, ["_id"], {
artist: artist
//artistDisambiguation: artistDisambiguation
});
return t.execDefferedStatements();
}).then(function (results) {
var artistId = results[0][0];
t.sqlSelect("artistAlbums",function(r){return r.album;}, ["albumId", 'album'], {
t.sqlSelect("artistAlbums", function (r) {
return r.album;
}, ["albumId", 'album'], {
artistId: artistId
}, [
{
@@ -1875,8 +1886,9 @@ Tomahawk.Collection = {
albumTracks: function (params) {
//TODO filter/where support
var id = params.id;
if(typeof id === 'undefined')
if (typeof id === 'undefined') {
id = this.settings.id;
}
var albumArtist = params.artist;
//var albumArtistDisambiguation = params.albumArtistDisambiguation;
var album = params.album;
@@ -1885,14 +1897,18 @@ Tomahawk.Collection = {
var t = new Tomahawk.Collection.Transaction(this, id);
return t.beginTransaction().then(function () {
t.sqlSelect("artists", function(r){return r._id;},["_id"], {
artist: albumArtist,
t.sqlSelect("artists", function (r) {
return r._id;
}, ["_id"], {
artist: albumArtist
//artistDisambiguation: albumArtistDisambiguation
});
return t.execDefferedStatements();
}).then(function (results) {
var albumArtistId = results[0][0];
t.sqlSelect("albums",function(r){return r._id;}, ["_id"], {
t.sqlSelect("albums", function (r) {
return r._id;
}, ["_id"], {
album: album,
albumArtistId: albumArtistId
});
@@ -1907,15 +1923,16 @@ Tomahawk.Collection = {
collection: function () {
this.settings.trackcount = this._trackCount;
if(! this.settings.description)
if (!this.settings.description) {
this.settings.description = this.settings.prettyname;
}
this.settings.capabilities = [Tomahawk.Collection.BrowseCapability.Artists,
Tomahawk.Collection.BrowseCapability.Albums, Tomahawk.Collection.BrowseCapability.Tracks];
Tomahawk.Collection.BrowseCapability.Albums,
Tomahawk.Collection.BrowseCapability.Tracks];
return this.settings;
}
};
// Legacy compability for 0.8 and before
Tomahawk.reportCapabilities = function (capabilities) {
if (capabilities & TomahawkResolverCapability.Browsable) {
@@ -1925,7 +1942,8 @@ Tomahawk.reportCapabilities = function (capabilities) {
Tomahawk.nativeReportCapabilities(capabilities);
};
Tomahawk.addArtistResults = Tomahawk.addAlbumResults = Tomahawk.addAlbumTrackResults = function (result) {
Tomahawk.addArtistResults = Tomahawk.addAlbumResults = Tomahawk.addAlbumTrackResults
= function (result) {
Tomahawk.PluginManager.resolve[result.qid](result);
delete Tomahawk.PluginManager.resolve[result.qid];
};