wallet-core/packages/taler-wallet-webextension/src/renderHtml.tsx

171 lines
3.9 KiB
TypeScript
Raw Normal View History

2016-09-29 16:31:55 +02:00
/*
This file is part of TALER
(C) 2016 INRIA
TALER is free software; you can redistribute it and/or modify it under the
terms of the GNU General Public License as published by the Free Software
Foundation; either version 3, or (at your option) any later version.
TALER is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
A PARTICULAR PURPOSE. See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License along with
TALER; see the file COPYING. If not, see <http://www.gnu.org/licenses/>
*/
/**
* Helpers functions to render Taler-related data structures to HTML.
*
* @author Florian Dold
*/
2017-05-24 16:45:57 +02:00
/**
* Imports.
*/
2021-03-27 14:35:58 +01:00
import {
AmountJson,
Amounts,
amountFractionalBase,
} from "@gnu-taler/taler-util";
2021-08-23 21:51:49 +02:00
import { Component, ComponentChildren, JSX, h } from "preact";
2016-09-29 16:31:55 +02:00
2017-06-04 17:56:55 +02:00
/**
* Render amount as HTML, which non-breaking space between
* decimal value and currency.
*/
2020-04-06 20:02:01 +02:00
export function renderAmount(amount: AmountJson | string): JSX.Element {
let a;
if (typeof amount === "string") {
a = Amounts.parse(amount);
} else {
a = amount;
}
if (!a) {
return <span>(invalid amount)</span>;
}
2021-03-27 14:35:58 +01:00
const x = a.value + a.fraction / amountFractionalBase;
2019-09-05 16:23:54 +02:00
return (
<span>
{x}&nbsp;{a.currency}
</span>
);
2017-06-04 17:56:55 +02:00
}
2020-04-06 20:02:01 +02:00
export const AmountView = ({
amount,
}: {
amount: AmountJson | string;
}): JSX.Element => renderAmount(amount);
2017-06-04 17:56:55 +02:00
2017-05-24 16:45:57 +02:00
/**
* Abbreviate a string to a given length, and show the full
* string on hover as a tooltip.
*/
2020-04-06 20:02:01 +02:00
export function abbrev(s: string, n = 5): JSX.Element {
2016-10-12 02:55:53 +02:00
let sAbbrev = s;
if (s.length > n) {
sAbbrev = s.slice(0, n) + "..";
}
return (
<span class="abbrev" title={s}>
2016-10-12 02:55:53 +02:00
{sAbbrev}
</span>
);
}
interface CollapsibleState {
collapsed: boolean;
}
interface CollapsibleProps {
initiallyCollapsed: boolean;
title: string;
}
2017-10-15 19:28:35 +02:00
/**
* Component that shows/hides its children when clicking
* a heading.
*/
2021-11-15 15:18:58 +01:00
export class Collapsible extends Component<CollapsibleProps, CollapsibleState> {
constructor(props: CollapsibleProps) {
super(props);
this.state = { collapsed: props.initiallyCollapsed };
}
2020-04-06 20:02:01 +02:00
render(): JSX.Element {
const doOpen = (e: any): void => {
2019-09-05 16:23:54 +02:00
this.setState({ collapsed: false });
e.preventDefault();
};
2020-04-06 20:02:01 +02:00
const doClose = (e: any): void => {
2019-09-05 16:23:54 +02:00
this.setState({ collapsed: true });
e.preventDefault();
};
if (this.state.collapsed) {
2019-09-05 16:23:54 +02:00
return (
<h2>
<a class="opener opener-collapsed" href="#" onClick={doOpen}>
{" "}
2019-09-05 16:23:54 +02:00
{this.props.title}
</a>
</h2>
);
}
return (
<div>
2019-09-05 16:23:54 +02:00
<h2>
<a class="opener opener-open" href="#" onClick={doClose}>
{" "}
2019-09-05 16:23:54 +02:00
{this.props.title}
</a>
</h2>
{this.props.children}
</div>
);
}
}
2017-11-30 04:07:36 +01:00
interface ExpanderTextProps {
text: string;
}
/**
* Show a heading with a toggle to show/hide the expandable content.
*/
2020-04-06 20:02:01 +02:00
export function ExpanderText({ text }: ExpanderTextProps): JSX.Element {
return <span>{text}</span>;
}
2021-11-15 15:18:58 +01:00
export interface LoadingButtonProps
extends JSX.HTMLAttributes<HTMLButtonElement> {
2021-05-07 15:38:28 +02:00
isLoading: boolean;
2019-08-31 11:49:36 +02:00
}
2021-11-15 15:18:58 +01:00
export function ProgressButton({
isLoading,
...rest
}: LoadingButtonProps): JSX.Element {
2019-08-31 11:49:36 +02:00
return (
2021-11-15 15:18:58 +01:00
<button class="pure-button pure-button-primary" type="button" {...rest}>
2021-05-07 15:38:28 +02:00
{isLoading ? (
2019-09-05 16:23:54 +02:00
<span>
2021-11-15 15:18:58 +01:00
<object class="svg-icon svg-baseline" data="/img/spinner-bars.svg" />
2019-09-05 16:23:54 +02:00
</span>
) : null}{" "}
2021-05-07 15:38:28 +02:00
{rest.children}
2019-08-31 11:49:36 +02:00
</button>
);
}
2021-11-15 15:18:58 +01:00
export function PageLink(props: {
pageName: string;
children?: ComponentChildren;
}): JSX.Element {
2021-06-16 22:01:06 +02:00
const url = chrome.extension.getURL(`/static/wallet.html#/${props.pageName}`);
2019-09-05 16:23:54 +02:00
return (
2021-11-15 15:18:58 +01:00
<a class="actionLink" href={url} target="_blank" rel="noopener noreferrer">
2019-09-05 16:23:54 +02:00
{props.children}
</a>
);
}