'use strict';
/**
* Created by mdonnel on 10/04/2017.
*/
var EventModel = Backbone.Model.extend({
'initialize': function initialize() {
this.update();
},
'getDays': function getDays(startdate, enddate) {
var r = void 0,
s = void 0,
e = void 0;
s = startdate.getTime();
e = enddate.getTime();
r = (e - s) / (24 * 60 * 60 * 1000);
return r;
},
'update': function update() {
var now = new Date();
var mod = 3600000 - now.getTime() % 3600000;
var data = {};
data.days = Math.ceil(this.getDays(now, this.get('event')));
data.weeks = Math.ceil(this.getDays(now, this.get('event')) / 7);
this.set('data', data);
var clockFn = function clockFn() {
this.update();
};
setTimeout(clockFn.bind(this), mod + 10);
}
});
var EventView = Backbone.View.extend({
'tagName': 'div',
'initialize': function initialize() {
_.bindAll(this, 'render');
this.model.bind('change', this.render);
this.id = 'e_' + Math.random().toString(36).substr(2, 9);
this.$events = $('#events');
this.$myEvent = null;
this.$el = this.$events;
this.initView();
this.render();
},
'render': function render() {
var label = this.model.get('label');
var data = this.model.get('data');
var str = label + ' ' + data.days + ' days / ' + data.weeks + ' weeks';
this.$myEvent.empty().append(str);
},
'initView': function initView() {
var html = '
';
this.$html = $(html);
this.$events.append(this.$html);
this.$myEvent = $('#' + this.id);
}
});
'use strict';
/**
* Created by mdonnel on 22/03/2017.
*/
var BitcoinModel = Backbone.Model.extend({
'initialize': function initialize() {
this.set('url', '/btc');
this.set('balanceUrl', '/balance');
var data = {
'lastGBP': 0.0,
'lastUSD': 0.0,
'lows': { 'gbp': 0, 'usd': 0 },
'highs': { 'gbp': 0, 'usd': 0 },
'eclass': '',
'balance': 0.0,
'trend': 0
};
this.set('btcdata', data);
this.set('balance', 0);
this.update();
this.updateHourly();
},
'update': function update() {
this.getBTC();
// this.getBalance();
var now = new Date();
var mod = 300000 - now.getTime() % 300000;
var btcupdateFn = function btcupdateFn() {
this.update();
};
setTimeout(btcupdateFn.bind(this), mod + 10);
},
'updateHourly': function updateHourly() {
this.getBalance();
var now = new Date();
var mod = 3600000 - now.getTime() % 3600000;
var btcupdateFn = function btcupdateFn() {
this.update();
};
setTimeout(btcupdateFn.bind(this), mod + 10);
},
'recalc': function recalc() {
var data = this.get('btcdata');
var lastGBP = data.lastGBP;
var lastUSD = void 0;
var g = data.gbp;
var u = data.usd;
var lows = data.lows;
var highs = data.highs;
var eclass = data.eclass;
var balance = data.balance;
var trend = data.trend;
if (trend === undefined || trend === null) trend = 1;
if (g !== undefined) {
if (data.lastGBP !== 0) {
if (g > lastGBP) {
eclass = 'up';
} else {
eclass = 'down';
}
} else {
lows.gbp = g;
lows.usd = u;
highs.gbp = g;
highs.usd = u;
}
lastGBP = g;
lastUSD = u;
if (g < lows.gbp) lows.gbp = g;
if (u < lows.usd) lows.usd = u;
if (highs.gbp < g) highs.gbp = g;
if (highs.usd < u) highs.usd = u;
data = {
lastGBP: lastGBP,
lastUSD: lastUSD,
lows: lows,
highs: highs,
eclass: eclass,
balance: balance,
trend: trend
};
}
data.stub = Math.random(Number.MAX_SAFE_INTEGER).toString(32);
this.set('btcdata', data);
// total = myBTC * g;
},
'getBTC': function getBTC() {
var self = this;
var url = this.get('url');
$.ajax({
'type': 'GET',
'url': url,
'data': '',
'dataType': 'json',
'timeout': 10000,
// contentType: ('application/json'),
'headers': {
'Access-Control-Allow-Origin': '*',
'Access-Control-Allow-Methods': 'PUT, GET, POST, DELETE, OPTIONS',
'Access-Control-Allow-Headers': 'Content-Type'
},
'success': function success(data) {
var gbp = data.bpi.GBP.rate_float,
usd = data.bpi.USD.rate_float;
var trend = data.trend;
var btcdata = self.get('btcdata');
btcdata.gbp = gbp;
btcdata.usd = usd;
btcdata.trend = trend;
self.set('btcdata', btcdata);
self.recalc();
},
'error': function error(xhr, type) {
void 0;
void 0;
void 0;
}
});
},
'getBalance': function getBalance() {
var self = this;
var url = this.get('balanceUrl');
$.ajax({
'type': 'GET',
'url': url,
'data': '',
'dataType': 'json',
'timeout': 10000,
// contentType: ('application/json'),
'headers': {
'Access-Control-Allow-Origin': '*',
'Access-Control-Allow-Methods': 'PUT, GET, POST, DELETE, OPTIONS',
'Access-Control-Allow-Headers': 'Content-Type'
},
'success': function success(data) {
var balance = data.balance;
self.set('balance', balance);
self.recalc();
},
'error': function error(xhr, type) {
void 0;
void 0;
void 0;
}
});
}
});
var Bitcoin = Backbone.View.extend({
'tagName': 'div',
'initialize': function initialize() {
_.bindAll(this, 'render');
this.model.bind('change', this.render);
this.$btc = $('#btc');
this.$trend = $('#trend');
},
'render': function render() {
var btcdata = this.model.get('btcdata');
var balance = this.model.get('balance');
// console.log(`Balance: ${btcdata.balance.toFixed(4)}`);
// console.log(btcdata.lastGBP);
var owned = parseFloat(btcdata.lastGBP) * parseFloat(balance);
// console.log(`owned: ${owned}`);
var title = 'High: $' + parseFloat(btcdata.highs.usd.toFixed(2)) + ' / Low $' + parseFloat(btcdata.lows.usd.toFixed(2));
var trendClass = '';
if (btcdata.trend > 1.00) trendClass = 'trendUp';else if (btcdata.trend < 1.00) trendClass = 'trendDown';else trendClass = '';
this.$trend.removeClass();
this.$trend.addClass(trendClass);
this.$btc.removeClass();
this.$btc.addClass(btcdata.eclass);
this.$btc.html('$' + parseFloat(btcdata.lastUSD.toFixed(2)) + ' / £' + parseFloat(btcdata.lastGBP.toFixed(2)) + '