Some new stuff

This commit is contained in:
Martin Donnelly 2017-09-05 11:36:24 +01:00
parent fa6dede096
commit b31e719ed9
5 changed files with 103 additions and 1 deletions

18
app/css/pug.css Normal file
View File

@ -0,0 +1,18 @@
textarea {
width: 500px;
height: 200px;
}
body {
font-family: 'Lato', sans-serif;
}
#result {
clear: both;
margin-top: 10;
}
.error {
font-style: italic;
color: red;
}

48
app/pug-app.js Normal file
View File

@ -0,0 +1,48 @@
document.addEventListener('DOMContentLoaded', init, false);
let pug, $input, $data, $result, $jsonError, $pugError;
function init() {
pug = require('pug');
$input = document.querySelector('#input');
$data = document.querySelector('#data');
$result = document.querySelector('#result');
$jsonError = document.querySelector('#jsonError');
$pugError = document.querySelector('#pugError');
$input.addEventListener('input', doRender, false);
$data.addEventListener('input', doRender, false);
console.log('Ready');
}
function doRender() {
let input = $input.value;
let data = $data.value;
$pugError.innerHTML = '';
if(!input.length) return;
if(data.length)
try {
data = JSON.parse(data);
console.log(data);
$jsonError.innerHTML = '';
} catch(e) {
console.log('invalid json');
$jsonError.innerHTML = 'Invalid JSON';
data = {};
}
else {
data = {};
$jsonError.innerHTML = '';
}
try {
let result = pug.render(input, data);
console.log(result);
$result.innerHTML = result;
} catch(e) {
console.log('pug error');
$pugError.innerHTML = e.message;
console.dir(e);
}
}

View File

@ -51,6 +51,7 @@ function getBitcoin () {
btcQuery(a => {
// logger.info(a);
logger.info('Got btc data. Storing it');
logger.debug('>> btc', a);
btcCache = a;
history.push(a.bpi.USD.rate_float);
btcCache.history = history.get();

View File

@ -2,7 +2,7 @@ const http = require('http');
const LimitedArray = require('limitedarray');
const trend = require('trend');
const logger = require('log4js').getLogger('fx');
const delay = (60000 * 30);
const delay = 2764800; // 32 days divided by 1000
let fxCache = {};
let history = new LimitedArray(48); // one days worth in 5 minute chunks
@ -36,6 +36,8 @@ function getFx() {
logger.info('Got FX data. Storing it');
fxCache = a;
let minRates = {USD:fxCache.rates.USD, GBP:fxCache.rates.GBP, SEK:fxCache.rates.SEK};
fxCache.rates = minRates;
history.push(a.rates.GBP);
fxCache.history = history.get();
fxCache.trend = trend(fxCache.history, {avgPoints: 12});

33
views/pages/pug.ejs Normal file
View File

@ -0,0 +1,33 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
<meta name="description" content="">
<meta name="viewport" content="width=device-width">
<link href="https://fonts.googleapis.com/css?family=Lato" rel="stylesheet">
<link rel="stylesheet" href="pug.css">
</head>
<body>
<h2>Pug Tester</h2>
<div style="float:left;margin-right:20px">
<h3>Template String</h3>
<textarea id="input"></textarea>
<div id="pugError" class="error"></div>
</div>
<div style="float:left">
<h3>JSON Data for Template</h3>
<textarea id="data"></textarea>
<div id="jsonError" class="error"></div>
</div>
<div id="result"></div>
<script src="https://pugjs.org/js/pug.js"></script>
<script src="pug-app.js"></script>
</body>
</html>