Skip to content

Commit

Permalink
[asset] add support for sorting table
Browse files Browse the repository at this point in the history
  • Loading branch information
ananthakumaran committed Feb 4, 2024
1 parent 26fb761 commit de0ba74
Show file tree
Hide file tree
Showing 11 changed files with 345 additions and 127 deletions.
20 changes: 20 additions & 0 deletions flake/node-package.nix

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

24 changes: 24 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@
"svelte-local-storage-store": "^0.6.0",
"svelte-select": "^5.8.0",
"svelte-tiny-virtual-list": "^2.0.5",
"tabulator-tables": "^5.5.4",
"textures": "^1.2.3",
"tippy.js": "^6.3.7",
"xlsx": "https://cdn.sheetjs.com/xlsx-0.19.3/xlsx-0.19.3.tgz",
Expand All @@ -78,6 +79,7 @@
"@types/lodash": "^4.14.194",
"@types/papaparse": "^5.3.7",
"@types/sprintf-js": "^1.1.2",
"@types/tabulator-tables": "^5.5.7",
"@typescript-eslint/eslint-plugin": "^5.45.0",
"@typescript-eslint/parser": "^5.45.0",
"autoprefixer": "^10.4.14",
Expand Down
39 changes: 39 additions & 0 deletions src/app.scss
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,45 @@ $editor-full-height: calc(100vh - 45.5px - 7px - 15.75px - 21px - 57.75px - 10.5
@import "bulma-switch/src/sass/index.sass";
@import "@cityssm/bulma-sticky-table/sticky-table";

$backgroundColor: $white;
$borderColor: none;
$textSize: 1rem;
$headerBackgroundColor: $white;
$headerTextColor: $grey-dark;
$headerBorderColor: rgba($grey-lightest, 1);
$headerSeparatorColor: rgba($grey-light, 0.5);
$headerMargin: 0.2857rem;
$sortArrowActive: $black;
$sortArrowInactive: $grey-light;
$rowBackgroundColor: $white;
$rowAltBackgroundColor: $white;
$rowBorderColor: rgba($grey-lightest, 0.5);
$rowTextColor: $grey-dark;
$rowHoverBackground: $white-bis;
$rowSelectedBackground: #9abcea;
$rowSelectedBackgroundHover: #769bcc;
$editBoxColor: #1d68cd;
$errorColor: #dd0000;
$footerBackgroundColor: #fff;
$footerTextColor: #555;
$footerBorderColor: #aaa;
$footerSeparatorColor: #999;
$footerActiveColor: #d00;

@import "tabulator-tables/src/scss/themes/tabulator_simple.scss";

.tabulator .tabulator-header .tabulator-col .tabulator-col-content .tabulator-col-title {
white-space: normal;
}

.tabulator {
border-radius: $radius;
}

.tabulator-col {
padding: 0.5rem;
}

// override message style

.message {
Expand Down
120 changes: 55 additions & 65 deletions src/lib/components/AssetsBalance.svelte
Original file line number Diff line number Diff line change
@@ -1,75 +1,65 @@
<script lang="ts">
import { iconText } from "$lib/icon";
import {
type AssetBreakdown,
depth,
lastName,
isZero,
formatCurrency,
formatFloat,
formatPercentage
} from "$lib/utils";
import { type AssetBreakdown, buildTree } from "$lib/utils";
import _ from "lodash";
import Table from "./Table.svelte";
import type { ColumnDefinition } from "tabulator-tables";
import {
accountName,
formatCurrencyChange,
indendedAssetAccountName,
nonZeroCurrency,
nonZeroFloatChange,
nonZeroPercentageChange
} from "$lib/table_formatters";
export let breakdowns: Record<string, AssetBreakdown>;
export let indent = true;
function calculateChangeClass(gain: number) {
let changeClass = "";
if (gain > 0) {
changeClass = "has-text-success";
} else if (gain < 0) {
changeClass = "has-text-danger";
const columns: ColumnDefinition[] = [
{
title: "Account",
field: "group",
formatter: indent ? indendedAssetAccountName : accountName,
frozen: true
},
{
title: "Investment Amount",
field: "investmentAmount",
hozAlign: "right",
vertAlign: "middle",
formatter: nonZeroCurrency
},
{
title: "Withdrawal Amount",
field: "withdrawalAmount",
hozAlign: "right",
formatter: nonZeroCurrency
},
{
title: "Balance Units",
field: "balanceUnits",
hozAlign: "right",
formatter: nonZeroCurrency
},
{ title: "Market Value", field: "marketAmount", hozAlign: "right", formatter: nonZeroCurrency },
{ title: "Change", field: "gainAmount", hozAlign: "right", formatter: formatCurrencyChange },
{ title: "XIRR", field: "xirr", hozAlign: "right", formatter: nonZeroFloatChange },
{
title: "Absolute Return",
field: "absoluteReturn",
hozAlign: "right",
formatter: nonZeroPercentageChange
}
return changeClass;
];
let tree: AssetBreakdown[] = [];
$: if (breakdowns) {
tree = buildTree(Object.values(breakdowns), (i) => i.group);
}
</script>

<div class="box overflow-x-auto max-h-screen max-w-fit pt-0">
<table class="table is-narrow is-hoverable is-light-border has-sticky-header">
<thead>
<tr>
<th class="py-2">Account</th>
<th class="py-2 has-text-right">Investment Amount</th>
<th class="py-2 has-text-right">Withdrawal Amount</th>
<th class="py-2 has-text-right">Balance Units</th>
<th class="py-2 has-text-right">Market Value</th>
<th class="py-2 has-text-right">Change</th>
<th class="py-2 has-text-right">XIRR</th>
<th class="py-2 has-text-right">Absolute Return</th>
</tr>
</thead>
<tbody class="has-text-grey-dark">
{#each Object.values(breakdowns) as b}
{@const indentWidth = indent ? _.repeat("&emsp;&emsp;", depth(b.group) - 1) : ""}
{@const gain = b.gainAmount}
{@const changeClass = calculateChangeClass(gain)}
<tr>
<td
class="whitespace-nowrap has-text-left"
style="max-width: max(15rem, 33.33vw); overflow: hidden;"
>{@html indentWidth}<span class="has-text-grey custom-icon">{iconText(b.group)}</span>
<a href="/assets/gain/{b.group}">{indent ? lastName(b.group) : b.group}</a></td
>
<td class="has-text-right"
>{!isZero(b.investmentAmount) ? formatCurrency(b.investmentAmount) : ""}</td
>
<td class="has-text-right"
>{!isZero(b.withdrawalAmount) ? formatCurrency(b.withdrawalAmount) : ""}</td
>
<td class="has-text-right">{b.balanceUnits > 0 ? formatFloat(b.balanceUnits, 4) : ""}</td>
<td class="has-text-right"
>{!isZero(b.marketAmount) ? formatCurrency(b.marketAmount) : ""}</td
>
<td class="{changeClass} has-text-right"
>{!isZero(b.investmentAmount) && !isZero(gain) ? formatCurrency(gain) : ""}</td
>
<td class="{changeClass} has-text-right">{!isZero(b.xirr) ? formatFloat(b.xirr) : ""}</td>
<td class="{changeClass} has-text-right"
>{!isZero(b.absoluteReturn) ? formatPercentage(b.absoluteReturn, 2) : ""}</td
>
</tr>
{/each}
</tbody>
</table>
</div>
{#if indent}
<Table data={tree} tree {columns} />
{:else}
<Table data={Object.values(breakdowns)} {columns} />
{/if}
2 changes: 1 addition & 1 deletion src/lib/components/Navbar.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@
label: "More",
href: "/more",
children: [
{ label: "Configuration", href: "/config", tag: "alpha", help: "config" },
{ label: "Configuration", href: "/config", help: "config" },
{ label: "Sheets", href: "/sheets", help: "sheets", disablePreload: true },
{ label: "Goals", href: "/goals", help: "goals" },
{ label: "Doctor", href: "/doctor" },
Expand Down
47 changes: 47 additions & 0 deletions src/lib/components/Table.svelte
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<script lang="ts">
import { rem } from "$lib/utils";
import { onMount } from "svelte";
import { TabulatorFull as Tabulator, type ColumnDefinition } from "tabulator-tables";
export let data: any[];
export let columns: ColumnDefinition[];
export let tree = false;
let tableComponent: HTMLElement;
let tabulator: Tabulator;
$: if (data.length > 0) {
build();
}
async function build() {
if (data.length === 0) {
return;
}
if (tabulator) {
tabulator.replaceData(data);
} else {
tabulator = new Tabulator(tableComponent, {
dataTree: tree,
dataTreeStartExpanded: [true, true, false],
dataTreeBranchElement: false,
dataTreeChildIndent: rem(30),
dataTreeCollapseElement:
"<span class='has-text-link icon is-small mr-3'><i class='fas fa-angle-up'></i></span>",
dataTreeExpandElement:
"<span class='has-text-link icon is-small mr-3'><i class='fas fa-angle-down'></i></span>",
data: data,
columns: columns,
maxHeight: "100vh",
layout: "fitDataTable"
});
}
}
onMount(async () => {
build();
});
</script>

<div class="overflow-x-auto box py-0" style="max-width: 100%;" bind:this={tableComponent}></div>
Loading

0 comments on commit de0ba74

Please sign in to comment.