2016-03-14 23:05:24 +01:00
|
|
|
var lbry = {
|
|
|
|
isConnected: false,
|
2016-03-15 17:05:11 +01:00
|
|
|
rootPath: '.',
|
2016-03-14 23:05:24 +01:00
|
|
|
colors: {
|
|
|
|
primary: '#155B4A'
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2016-03-21 07:31:25 +01:00
|
|
|
lbry.call = function (method, params, callback)
|
2016-03-16 22:56:51 +01:00
|
|
|
{
|
2016-03-21 07:31:25 +01:00
|
|
|
var xhr = new XMLHttpRequest;
|
|
|
|
xhr.addEventListener('load', function() {
|
|
|
|
// The response from the HTTP endpoint has a "result" key containing a JSON string of the output of the JSON-RPC method itself
|
|
|
|
var method_output = JSON.parse(JSON.parse(xhr.responseText).result);
|
|
|
|
|
|
|
|
if (method_output.code !== 200) {
|
|
|
|
throw new Error('Call to method ' + method + ' failed with message: ' + method_output.message);
|
|
|
|
}
|
|
|
|
|
|
|
|
callback(method_output.result);
|
|
|
|
});
|
|
|
|
xhr.open('POST', 'http://localhost:5279/lbryapi', true);
|
|
|
|
xhr.send(JSON.stringify({
|
|
|
|
'jsonrpc': '2.0',
|
|
|
|
'method': method,
|
|
|
|
'params': params,
|
|
|
|
'id': 0
|
|
|
|
}));
|
2016-03-16 22:56:51 +01:00
|
|
|
}
|
|
|
|
|
2016-03-14 23:05:24 +01:00
|
|
|
//core
|
|
|
|
lbry.connect = function(callback)
|
|
|
|
{
|
|
|
|
//dummy connect function - one of the first things we should do is dump people to get help if it can't connect
|
|
|
|
setTimeout(function() {
|
|
|
|
lbry.isConnected = true;
|
|
|
|
callback();
|
|
|
|
}, 1500);
|
|
|
|
}
|
|
|
|
|
2016-03-16 22:56:51 +01:00
|
|
|
lbry.getBalance = function(callback)
|
2016-03-14 23:05:24 +01:00
|
|
|
{
|
2016-03-16 22:56:51 +01:00
|
|
|
lbry.call("get_balance", {}, callback);
|
2016-03-14 23:05:24 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
lbry.search = function(query, callback)
|
|
|
|
{
|
|
|
|
//simulate searching via setTimeout with
|
|
|
|
setTimeout(function() {
|
2016-03-16 22:56:51 +01:00
|
|
|
lbry.call("search_nametrie", { query: query }, callback);
|
2016-03-14 23:05:24 +01:00
|
|
|
}, 300);
|
|
|
|
}
|
|
|
|
|
|
|
|
//utilities
|
|
|
|
lbry.formatCredits = function(amount, precision)
|
|
|
|
{
|
|
|
|
return amount.toFixed(precision || 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
lbry.loadJs = function(src, type, onload)
|
|
|
|
{
|
|
|
|
var lbryScriptTag = document.getElementById('lbry'),
|
|
|
|
newScriptTag = document.createElement('script'),
|
|
|
|
type = type || 'text/javascript';
|
|
|
|
|
|
|
|
newScriptTag.src = src;
|
|
|
|
newScriptTag.type = type;
|
|
|
|
if (onload)
|
|
|
|
{
|
|
|
|
newScript.onload = onload;
|
|
|
|
}
|
|
|
|
lbryScriptTag.parentNode.insertBefore(newScriptTag, lbryScriptTag);
|
|
|
|
}
|
|
|
|
|
|
|
|
lbry.imagePath = function(file)
|
|
|
|
{
|
|
|
|
return lbry.rootPath + '/img/' + file;
|
|
|
|
}
|