show balance

This commit is contained in:
Florian Dold 2015-12-16 08:01:43 +01:00
parent 3315470598
commit 1b295d0f1a
7 changed files with 127 additions and 167 deletions

View File

@ -23,7 +23,7 @@ function openTalerDb() {
case 0:
db.createObjectStore("mints", { keyPath: "baseUrl" });
db.createObjectStore("reserves", { keyPath: "reserve_pub" });
db.createObjectStore("denoms", { keyPath: "denom_pub" });
db.createObjectStore("denoms", { keyPath: "denomPub" });
db.createObjectStore("coins", { keyPath: "coinPub" });
db.createObjectStore("withdrawals", { keyPath: "id", autoIncrement: true });
db.createObjectStore("transactions", { keyPath: "id", autoIncrement: true });
@ -343,8 +343,16 @@ function updateMintFromUrl(db, baseUrl) {
baseUrl: baseUrl,
keys: mintKeysJson
};
let tx = db.transaction(['mints'], 'readwrite');
let tx = db.transaction(['mints', 'denoms'], 'readwrite');
tx.objectStore('mints').put(mint);
for (let d of mintKeysJson.denoms) {
// TODO: verify and complete
let di = {
denomPub: d.denom_pub,
value: d.value
};
tx.objectStore('denoms').put(di);
}
tx.oncomplete = (e) => {
resolve(mint);
};
@ -384,13 +392,56 @@ function dumpDb(db, detail, sendResponse) {
}
return true;
}
function reset(db, detail, sendResponse) {
let tx = db.transaction(db.objectStoreNames, 'readwrite');
for (let i = 0; i < db.objectStoreNames.length; i++) {
tx.objectStore(db.objectStoreNames[i]).clear();
}
indexedDB.deleteDatabase(DB_NAME);
console.log("reset done");
return false;
}
function balances(db, detail, sendResponse) {
let byCurrency = {};
let tx = db.transaction(['coins', 'denoms']);
let req = tx.objectStore('coins').openCursor();
req.onsuccess = (e) => {
let cursor = req.result;
if (cursor) {
tx.objectStore('denoms').get(cursor.value.denomPub).onsuccess = (e2) => {
let d = e2.target.result;
console.log("got denom", JSON.stringify(d));
let acc = byCurrency[d.value.currency];
if (!acc) {
acc = new Amount(d.value);
console.log("initial:", JSON.stringify(acc.toJson()));
byCurrency[d.value.currency] = acc.toJson();
}
else {
let am = new Amount(acc);
am.add(new Amount(d.value));
byCurrency[d.value.currency] = am.toJson();
console.log("then:", JSON.stringify(am.toJson()));
}
};
cursor.continue();
}
else {
sendResponse(byCurrency);
console.log("response", JSON.stringify(byCurrency));
}
};
return true;
}
chrome.browserAction.setBadgeText({ text: "" });
openTalerDb().then((db) => {
console.log("db loaded");
chrome.runtime.onMessage.addListener(function (req, sender, onresponse) {
let dispatch = {
"confirm-reserve": confirmReserve,
"dump-db": dumpDb
"dump-db": dumpDb,
"balances": balances,
"reset": reset
};
if (req.type in dispatch) {
return dispatch[req.type](db, req.detail, onresponse);

View File

@ -26,7 +26,7 @@ function openTalerDb(): Promise<IDBDatabase> {
case 0: // DB does not exist yet
db.createObjectStore("mints", { keyPath: "baseUrl" });
db.createObjectStore("reserves", { keyPath: "reserve_pub"});
db.createObjectStore("denoms", { keyPath: "denom_pub" });
db.createObjectStore("denoms", { keyPath: "denomPub" });
db.createObjectStore("coins", { keyPath: "coinPub" });
db.createObjectStore("withdrawals", { keyPath: "id", autoIncrement: true });
db.createObjectStore("transactions", { keyPath: "id", autoIncrement: true });
@ -126,14 +126,18 @@ function rankDenom(denom1: any, denom2: any) {
}
interface ReservePub {
reservePub: string;
interface AmountJson {
value: number;
fraction: number;
currency: string;
}
interface CoinPub {
coinPub: string;
}
interface Denomination {
value: AmountJson;
denomPub: string;
}
interface PreCoin {
coinPub: string;
@ -407,8 +411,16 @@ function updateMintFromUrl(db, baseUrl) {
baseUrl: baseUrl,
keys: mintKeysJson
};
let tx = db.transaction(['mints'], 'readwrite');
let tx = db.transaction(['mints', 'denoms'], 'readwrite');
tx.objectStore('mints').put(mint);
for (let d of mintKeysJson.denoms) {
// TODO: verify and complete
let di = {
denomPub: d.denom_pub,
value: d.value
}
tx.objectStore('denoms').put(di);
}
tx.oncomplete = (e) => {
resolve(mint);
};
@ -451,6 +463,50 @@ function dumpDb(db, detail, sendResponse) {
}
// Just for debugging.
function reset(db, detail, sendResponse) {
let tx = db.transaction(db.objectStoreNames, 'readwrite');
for (let i = 0; i < db.objectStoreNames.length; i++) {
tx.objectStore(db.objectStoreNames[i]).clear();
}
indexedDB.deleteDatabase(DB_NAME);
chrome.browserAction.setBadgeText({text: ""});
console.log("reset done");
return false;
}
function balances(db, detail, sendResponse) {
let byCurrency = {};
let tx = db.transaction(['coins', 'denoms']);
let req = tx.objectStore('coins').openCursor();
req.onsuccess = (e) => {
let cursor = req.result;
if (cursor) {
tx.objectStore('denoms').get(cursor.value.denomPub).onsuccess = (e2) => {
let d = e2.target.result;
console.log("got denom", JSON.stringify(d));
let acc = byCurrency[d.value.currency];
if (!acc) {
acc = new Amount(d.value);
console.log("initial:", JSON.stringify(acc.toJson()));
byCurrency[d.value.currency] = acc.toJson();
} else {
let am = new Amount(acc);
am.add(new Amount(d.value));
byCurrency[d.value.currency] = am.toJson();
console.log("then:", JSON.stringify(am.toJson()));
}
};
cursor.continue();
} else {
sendResponse(byCurrency);
console.log("response", JSON.stringify(byCurrency));
}
}
return true;
}
chrome.browserAction.setBadgeText({text: ""});
openTalerDb().then((db) => {
@ -459,7 +515,9 @@ openTalerDb().then((db) => {
function (req, sender, onresponse) {
let dispatch = {
"confirm-reserve": confirmReserve,
"dump-db": dumpDb
"dump-db": dumpDb,
"balances": balances,
"reset": reset
}
if (req.type in dispatch) {
return dispatch[req.type](db, req.detail, onresponse);

View File

@ -18,7 +18,7 @@
"browser_action": {
"default_icon": "icons/taler-logo-24.png",
"default_title": "Taler",
"default_popup": "popup/wallet.html"
"default_popup": "popup/balance-overview.html"
},
"content_scripts": [

View File

@ -1,5 +1,5 @@
body {
width: 35em;
width: 30em;
margin: 0;
padding: 0
}

View File

@ -1,51 +0,0 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<link rel="stylesheet" href="popup.css" type="text/css">
<script src="../lib/util.js" type="text/javascript"></script>
<script src="wallet.js" type="text/javascript"></script>
</head>
<body>
<div id="header" class="nav">
<a href="wallet.html" class="active">Wallet</a>
<a href="transactions.html">Transactions</a>
<a href="reserves.html">Reserves</a>
<button id="debug">Debug!</button>
</div>
<div id="content">
<table id="wallet-table" class="hidden">
<thead>
<tr>
<th colspan="2">Amount</th>
<th>Show</th>
</tr>
</thead>
<tbody>
<!--
<tr>
<td class="amount">42</td>
<td class="currency">EUR</td>
<td class="select"><input type="checkbox" /></td>
</tr>
<tr>
<td class="amount">23</td>
<td class="currency">USD</td>
<td class="select"><input type="checkbox" /></td>
</tr>
<tr>
<td class="amount">1337</td>
<td class="currency">KUD</td>
<td class="select"><input type="checkbox" /></td>
</tr>
-->
</tbody>
</table>
<p id="wallet-empty">The wallet is empty.</p>
</div>
</body>
</html>

View File

@ -1,100 +0,0 @@
'use strict';
var selected_currency = 'EUR'; // FIXME
function select_currency (checkbox, currency, amount)
{
selected_currency = currency;
if (checkbox.checked)
{
let inputs = document.getElementsByTagName('input');
for (let i = 0; i < inputs.length; i++)
{
let input = inputs[i];
if (input != checkbox)
input.checked = false;
}
chrome.browserAction.setBadgeText({text: amount.toString()})
chrome.browserAction.setTitle({title: 'Taler: ' + amount + ' ' + currency});
}
else
{
chrome.browserAction.setBadgeText({text: ''})
chrome.browserAction.setTitle({title: 'Taler'});
}
}
function add_currency (currency, amount)
{
let empty = document.getElementById('wallet-empty');
if (! /\bhidden\b/.test(empty.className))
empty.className += ' hidden';
let table = document.getElementById('wallet-table');
table.className = table.className.replace(/\bhidden\b/, '');
let tr = document.createElement('tr');
tr.id = 'wallet-table-'+ currency;
table.appendChild(tr);
let td_amount = document.createElement('td');
td_amount.id = 'wallet-currency-'+ currency +'-amount';
td_amount.className = 'amount';
let text_amount = document.createTextNode(amount);
tr.appendChild(td_amount).appendChild(text_amount);
let td_currency = document.createElement('td');
td_currency.id = 'wallet-table-'+ currency +'-currency';
td_currency.className = 'currency';
let text_currency = document.createTextNode(currency);
tr.appendChild(td_currency).appendChild(text_currency);
let td_select = document.createElement('td');
td_select.id = 'wallet-table-'+ currency +'-select';
td_select.className = 'select';
let checkbox = document.createElement('input');
checkbox.id = 'wallet-table-'+ currency +'-checkbox';
checkbox.setAttribute('type', 'checkbox');
if (currency == selected_currency)
checkbox.checked = true;
tr.appendChild(td_select).appendChild(checkbox);
checkbox._amount = amount;
checkbox.addEventListener('click', function () {
select_currency(this, currency, this._amount);
});
}
function update_currency (currency, amount)
{
let td_amount = document.getElementById('wallet-currency-'+ currency +'-amount');
let text_amount = document.createTextNode(amount);
td_amount.removeChild(td_amount.firstChild);
td_amount.appendChild(text_amount);
let checkbox = document.getElementById('wallet-table-'+ currency +'-checkbox');
checkbox._amount = amount;
}
document.addEventListener('DOMContentLoaded', (e) => {
//chrome.runtime.sendMessage({type: "WALLET_GET"}, function(wallet) {
// for (let currency in wallet) {
// let amount = amount_format(wallet[currency]);
// add_currency(currency, amount);
// }
//});
// FIXME: remove
add_currency('EUR', 42);
add_currency('USD', 17);
add_currency('KUD', 1337);
update_currency('USD', 23);
document.getElementById("debug").addEventListener("click", (e) => {
chrome.tabs.create({
"url": chrome.extension.getURL("pages/debug.html")
});
});
});

View File

@ -1,10 +1,12 @@
{
"compilerOptions": {
"target": "es6"
"target": "es6",
"jsx": "react"
},
"files": [
"background/wallet.ts",
"background/emscriptif.ts",
"lib/util.ts"
"lib/util.ts",
"popup/balance-overview.tsx"
]
}