api: Move client files and get a basic dashboard working

This commit is contained in:
Thomas R. Koll 2016-01-18 21:09:16 +01:00
parent 5e3d6d6d3d
commit 619ea9906c
15 changed files with 39 additions and 128 deletions

View File

@ -1,7 +0,0 @@
ul.accounts, ul.accounts ul {
list-style-type: none;
padding-left: 0;
}
ul.accounts ul {
margin-left: 15px;
}

View File

@ -1,29 +0,0 @@
<html>
<head>
<meta charset="utf-8">
<title>hledger-api example 01</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.4/jquery.min.js"></script>
</head
<body>
<p>
Account names:
<pre></pre>
<script>
$(document).ready(
function() {
$.ajax({
url: "http://localhost:8001/accounts",
context: document.body,
success: function(data){
$('pre').text(data.join('\n'));
}
});
}
);
</script>
</body>
</html>

View File

@ -1,88 +0,0 @@
<html>
<head>
<meta charset="utf-8">
<title>hledger-api example 01</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.4/jquery.min.js"></script>
</head
<body>
<table border=0 width="100%">
<tr valign="top">
<td width="50%">
<h2>Commodities</h2>
<pre id="commodities"></pre>
<h2>Market Prices</h2>
<pre id="prices"></pre>
<h2>Account names</h2>
<pre id="accounts"></pre>
</td>
<td width="50%">
<h2>Transactions</h2>
<pre id="transactions"></pre>
</td>
</tr>
</table>
<script>
$(document).ready(function(){
get('commodities', id);
get('prices', showMarketPrice);
get('accounts', id);
get('transactions', showTransaction);
});
function get(method, showfn) {
return $.ajax({
url: "http://localhost:8001/"+method,
context: document.body,
success: function(data){
$('#'+method).text(data.map(showfn).join('\n'));
},
error: function(jqXHR, textStatus, errorThrown){
alert('error');
}
});
}
function id(x){ return x; }
function showAmount(a) {
return a.acommodity + a.aquantity;
}
function showMixedAmount(ma) {
return ma.map(showAmount).join(', ');
}
function showMarketPrice(mp) {
return mp.mpdate + ' ' + mp.mpcommodity + ' ' + showAmount(mp.mpamount);
}
function showPosting(p) {
return p.paccount + ' ' + showMixedAmount(p.pamount);
}
function showPostingLine(p) {
return ' ' + showPosting(p) + '\n';
}
function showTransaction(t) {
return (
t.tdate + ' ' + t.tdescription + '\n'
+ t.tpostings.map(showPostingLine).join('')
);
}
</script>
</body>
</html>

View File

@ -1 +0,0 @@
Files under this directory are served by hledger-api when no other API route is matched.

View File

@ -0,0 +1,22 @@
ul.accounts, ul.accounts ul {
list-style-type: none;
padding-left: 0;
}
ul.accounts ul {
margin-left: 15px;
}
ul.accounts li {
clear: both;
border-top: 1px dotted #888;
}
ul.accounts .balances {
display: block;
float: right;
text-align: right;
}
ul.accounts .balance {
display: block;
}
ul.accounts .balance .commodity-side-L {
float: left;
}

View File

@ -2,12 +2,26 @@ var hledger = angular.module('hledger', [
'ui.router', 'ui.router',
'ngResource' 'ngResource'
]) ])
function listToTree(list, id_field, parent_field) {
children = function(list, parent_id) {
return $.grep(list,
function(element) {
return element[parent_field] === parent_id
});
}
$.map(list, function(element) {
element.children = children(list, element[id_field])
});
root = children(list, '');
return root;
}
hledger.config(function($stateProvider, $urlRouterProvider) { hledger.config(function($stateProvider, $urlRouterProvider) {
$urlRouterProvider.otherwise("/accounts"); $urlRouterProvider.otherwise("/accounts");
$stateProvider $stateProvider
.state('accounts', { .state('accounts', {
url: "/accounts", url: "/accounts",
templateUrl: "accounts/view.html", templateUrl: "accounts/index.html",
controller: 'AccountsController' controller: 'AccountsController'
}) })
.state('help', { .state('help', {
@ -22,7 +36,7 @@ hledger.factory('Journal', function($resource) {
hledger.controller("JournalController", function($scope, Journal) { hledger.controller("JournalController", function($scope, Journal) {
Journal.query(function(data) { Journal.query(function(data) {
$scope.journal = data; $scope.journal = data;
}); });
}); });
@ -32,6 +46,6 @@ hledger.factory('Account', function($resource) {
hledger.controller("AccountsController", function($scope, Account) { hledger.controller("AccountsController", function($scope, Account) {
Account.query(function(data) { Account.query(function(data) {
$scope.accounts = data; $scope.accounts = listToTree(data, 'aname', 'aparentname')[0].children;
}); });
}); });