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-05-07 15:38:28 +02:00
|
|
|
import { Component, ComponentChildren, JSX } from "preact";
|
|
|
|
import { JSXInternal } from "preact/src/jsx";
|
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 {
|
2018-02-20 09:33:17 +01:00
|
|
|
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} {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 className="abbrev" title={s}>
|
|
|
|
{sAbbrev}
|
|
|
|
</span>
|
|
|
|
);
|
2016-11-13 08:16:12 +01:00
|
|
|
}
|
2017-08-27 04:19:11 +02:00
|
|
|
|
|
|
|
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-05-07 15:38:28 +02:00
|
|
|
export class Collapsible extends Component<
|
2019-09-05 16:23:54 +02:00
|
|
|
CollapsibleProps,
|
|
|
|
CollapsibleState
|
|
|
|
> {
|
2017-08-27 04:19:11 +02:00
|
|
|
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 });
|
2017-08-27 04:19:11 +02:00
|
|
|
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 });
|
2017-08-27 04:19:11 +02:00
|
|
|
e.preventDefault();
|
|
|
|
};
|
|
|
|
if (this.state.collapsed) {
|
2019-09-05 16:23:54 +02:00
|
|
|
return (
|
|
|
|
<h2>
|
|
|
|
<a className="opener opener-collapsed" href="#" onClick={doOpen}>
|
2020-05-01 10:46:56 +02:00
|
|
|
{" "}
|
2019-09-05 16:23:54 +02:00
|
|
|
{this.props.title}
|
|
|
|
</a>
|
|
|
|
</h2>
|
|
|
|
);
|
2017-08-27 04:19:11 +02:00
|
|
|
}
|
|
|
|
return (
|
|
|
|
<div>
|
2019-09-05 16:23:54 +02:00
|
|
|
<h2>
|
|
|
|
<a className="opener opener-open" href="#" onClick={doClose}>
|
2020-05-01 10:46:56 +02:00
|
|
|
{" "}
|
2019-09-05 16:23:54 +02:00
|
|
|
{this.props.title}
|
|
|
|
</a>
|
|
|
|
</h2>
|
2017-08-27 04:19:11 +02:00
|
|
|
{this.props.children}
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
2017-11-30 04:07:36 +01:00
|
|
|
|
2017-12-10 18:25:44 +01:00
|
|
|
interface ExpanderTextProps {
|
|
|
|
text: string;
|
|
|
|
}
|
|
|
|
|
2018-01-03 14:42:06 +01:00
|
|
|
/**
|
|
|
|
* 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 {
|
2019-09-05 16:10:53 +02:00
|
|
|
return <span>{text}</span>;
|
2017-12-10 18:25:44 +01:00
|
|
|
}
|
|
|
|
|
2021-05-07 15:38:28 +02:00
|
|
|
export interface LoadingButtonProps extends JSX.HTMLAttributes<HTMLButtonElement> {
|
|
|
|
isLoading: boolean;
|
2019-08-31 11:49:36 +02:00
|
|
|
}
|
|
|
|
|
2021-05-07 15:38:28 +02:00
|
|
|
export function ProgressButton({isLoading, ...rest}: LoadingButtonProps): JSX.Element {
|
2019-08-31 11:49:36 +02:00
|
|
|
return (
|
|
|
|
<button
|
|
|
|
className="pure-button pure-button-primary"
|
|
|
|
type="button"
|
2021-05-07 15:38:28 +02:00
|
|
|
{...rest}
|
2019-08-31 11:49:36 +02:00
|
|
|
>
|
2021-05-07 15:38:28 +02:00
|
|
|
{isLoading ? (
|
2019-09-05 16:23:54 +02:00
|
|
|
<span>
|
|
|
|
<object
|
|
|
|
className="svg-icon svg-baseline"
|
|
|
|
data="/img/spinner-bars.svg"
|
|
|
|
/>
|
|
|
|
</span>
|
|
|
|
) : null}{" "}
|
2021-05-07 15:38:28 +02:00
|
|
|
{rest.children}
|
2019-08-31 11:49:36 +02:00
|
|
|
</button>
|
|
|
|
);
|
2019-09-05 16:10:53 +02:00
|
|
|
}
|
|
|
|
|
2020-04-06 20:02:01 +02:00
|
|
|
export function PageLink(
|
2021-05-07 15:38:28 +02:00
|
|
|
props: { pageName: string, children?: ComponentChildren },
|
2020-04-06 20:02:01 +02:00
|
|
|
): JSX.Element {
|
2021-05-07 23:10:27 +02:00
|
|
|
const url = chrome.extension.getURL(`/static/popup.html#/${props.pageName}`);
|
2019-09-05 16:23:54 +02:00
|
|
|
return (
|
2020-04-07 10:07:32 +02:00
|
|
|
<a
|
|
|
|
className="actionLink"
|
|
|
|
href={url}
|
|
|
|
target="_blank"
|
|
|
|
rel="noopener noreferrer"
|
|
|
|
>
|
2019-09-05 16:23:54 +02:00
|
|
|
{props.children}
|
|
|
|
</a>
|
|
|
|
);
|
|
|
|
}
|