Fixes und Währungsumstellung
This commit is contained in:
@@ -1,36 +1,21 @@
|
||||
{{ define "main" }}
|
||||
{{ $catData := index hugo.Data .Params.category }}
|
||||
{{ $config := "" }}
|
||||
{{ if eq .Params.engine "linear" }}
|
||||
{{ $config = dict "fromFactor" .Params.from_factor
|
||||
"toFactor" .Params.to_factor | jsonify }}
|
||||
{{ else if eq .Params.engine "intermediate" }}
|
||||
{{ $toExpr := index $catData.to_intermediate
|
||||
.Params.from | default "v" }}
|
||||
{{ $fromExpr := index $catData.from_intermediate
|
||||
.Params.to | default "v" }}
|
||||
{{ $config = printf
|
||||
"{ toIntermediate: v => %s, fromIntermediate: v => %s }"
|
||||
$toExpr $fromExpr }}
|
||||
{{ else if eq .Params.engine "runtime" }}
|
||||
{{ $fromCode := .Params.from }}
|
||||
{{ $toCode := .Params.to }}
|
||||
{{ $config = printf
|
||||
"{ init: async function () { const pbUrl = document.querySelector('meta[name=\\'pocketbase-url\\']')?.content || 'https://www.alphabreed.com'; try { const response = await fetch(`${pbUrl}/api/collections/currencies/records`); if (!response.ok) { this.ratesError = 'Wechselkurse konnten nicht geladen werden.'; return; } const data = await response.json(); let latestUpdate = ''; for (const item of data.items || []) { this.rates[item.id] = item.rate; if (!latestUpdate || item.updated > latestUpdate) { latestUpdate = item.updated; } } const fromRate = '%s' === 'eur' ? 1 : this.rates['%s']; const toRate = '%s' === 'eur' ? 1 : this.rates['%s']; if (fromRate && toRate) { this.currentRate = parseFloat(toRate) / parseFloat(fromRate); } if (latestUpdate) { this.ratesUpdated = new Date(latestUpdate).toLocaleString('de-DE', { day: '2-digit', month: 'long', year: 'numeric', hour: '2-digit', minute: '2-digit' }); } } catch (e) { this.ratesError = 'Wechselkurse konnten nicht geladen werden.'; } }, convert: function (value) { const fromRate = '%s' === 'eur' ? 1 : this.rates['%s']; const toRate = '%s' === 'eur' ? 1 : this.rates['%s']; if (!fromRate || !toRate) { return null; } return value.times(new Decimal(toRate)).dividedBy(new Decimal(fromRate)); } }"
|
||||
$fromCode $fromCode $toCode $toCode
|
||||
$fromCode $fromCode $toCode $toCode }}
|
||||
{{ end }}
|
||||
{{ $availableUnits := partial "available-units.html"
|
||||
(dict "category" .Params.category "units" $catData.units) }}
|
||||
|
||||
<div x-data="createConverter('{{ .Params.engine }}',
|
||||
{{ $config | safeJS }})"
|
||||
x-init="init()">
|
||||
{{ $engine := .Params.engine | default "linear" }}
|
||||
|
||||
{{ if eq $engine "currencies" }}
|
||||
{{ partial "conversion/currencies-data" . }}
|
||||
{{ else if eq $engine "temperatures" }}
|
||||
{{ partial "conversion/temperatures-data" . }}
|
||||
{{ else }}
|
||||
{{ partial "conversion/linear-data" . }}
|
||||
{{ end }}
|
||||
|
||||
<div id="conversionform"
|
||||
class="bg-white p-8 rounded-lg shadow-md w-full
|
||||
dark:bg-gray-700 dark:text-grey-200"
|
||||
x-data="navActions()">
|
||||
dark:bg-gray-700 dark:text-grey-200">
|
||||
|
||||
<h1 id="headline"
|
||||
class="text-2xl font-bold mb-6 text-center">
|
||||
@@ -40,18 +25,13 @@
|
||||
|
||||
<div>
|
||||
|
||||
<a x-ref="ajaxLink"
|
||||
x-target.push="maincontent"
|
||||
href="/"
|
||||
class="hidden"></a>
|
||||
|
||||
<div>
|
||||
<label for="type"
|
||||
class="block text-sm font-medium opacity-75">
|
||||
Einheitentyp:
|
||||
</label>
|
||||
<select id="type" name="type"
|
||||
@change="navigate($event.target.value)"
|
||||
@change="navigate($event.target.value, '1')"
|
||||
class="select mt-1 block w-full">
|
||||
{{ range $slug, $data := hugo.Data }}
|
||||
{{ if and $data.slug $data.name }}
|
||||
@@ -79,8 +59,8 @@
|
||||
Umrechnen von:
|
||||
</label>
|
||||
<select id="from" name="from"
|
||||
@change="navigateWithValue(
|
||||
$event.target.value)"
|
||||
@change="navigate(
|
||||
$event.target.value, inputValue)"
|
||||
class="select mt-1 block w-full">
|
||||
{{ range $catData.unitOrder }}
|
||||
{{ $unitData := index $availableUnits . }}
|
||||
@@ -98,8 +78,8 @@
|
||||
|
||||
<div class="text-center sm:pt-6">
|
||||
<button type="button"
|
||||
@click="navigateWithResult(
|
||||
$el.dataset.swapUrl)"
|
||||
@click="navigate(
|
||||
$el.dataset.swapUrl, prettyNumber(convert(prepareValue(inputValue))))"
|
||||
data-swap-url="/{{ .Params.to }}-in-{{ .Params.from }}/"
|
||||
class="inline-flex items-center text-sm
|
||||
font-medium text-primary
|
||||
@@ -110,8 +90,7 @@
|
||||
stroke="currentColor"
|
||||
class="w-6 h-6">
|
||||
<path stroke-linecap="round"
|
||||
stroke-linejoin="round"
|
||||
d="M7.5 21 3 16.5m0 0L7.5 12M3
|
||||
stroke-linejoin="round" d="M7.5 21 3 16.5m0 0L7.5 12M3
|
||||
16.5h13.5m0-13.5L21 7.5m0
|
||||
0L16.5 12M21 7.5H7.5" />
|
||||
</svg>
|
||||
@@ -125,8 +104,8 @@
|
||||
Umrechnen in:
|
||||
</label>
|
||||
<select id="to" name="to"
|
||||
@change="navigateWithValue(
|
||||
$event.target.value)"
|
||||
@change="navigate(
|
||||
$event.target.value, inputValue)"
|
||||
class="select mt-1 block w-full">
|
||||
{{ range $catData.unitOrder }}
|
||||
{{ $unitData := index $availableUnits . }}
|
||||
@@ -155,7 +134,7 @@
|
||||
<input type="text" id="value"
|
||||
inputmode="decimal"
|
||||
x-model="inputValue"
|
||||
@input="calculate()"
|
||||
@input.debounce.500ms="updateUrl(inputValue)"
|
||||
class="textinput mt-1 block w-full"
|
||||
placeholder="Wert eingeben">
|
||||
</div>
|
||||
@@ -166,13 +145,8 @@
|
||||
</label>
|
||||
<input type="text" id="result"
|
||||
disabled
|
||||
:value="result"
|
||||
:value="prettyNumber(convert(prepareValue(inputValue)))"
|
||||
class="textinput mt-1 block w-full">
|
||||
<p x-show="ratesError"
|
||||
x-cloak
|
||||
class="text-sm text-red-600 mt-1
|
||||
dark:text-red-400"
|
||||
x-text="ratesError"></p>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
@@ -188,5 +162,3 @@
|
||||
</div>
|
||||
|
||||
{{ end }}
|
||||
|
||||
|
||||
|
||||
@@ -48,24 +48,32 @@
|
||||
{{ replace $fromI "v" $intermediateName 1 }}</b>
|
||||
</p>
|
||||
{{ end }}
|
||||
{{ else if eq .Params.engine "runtime" }}
|
||||
{{ else if eq .Params.engine "temperatures" }}
|
||||
<p class="mb-4 mt-8">
|
||||
Die Umrechnung von {{ $fromName }} in {{ $toName }}
|
||||
erfolgt mittels dieser Formel:
|
||||
</p>
|
||||
<div x-show="currentRate > 0">
|
||||
<p class="text-lg text-primary dark:text-primary-light">
|
||||
<b>{{ .Params.displayFormula }}</b>
|
||||
</p>
|
||||
{{ else if eq .Params.engine "currencies" }}
|
||||
<p class="mb-4 mt-8">
|
||||
Die Umrechnung von {{ $fromName }} in {{ $toName }}
|
||||
erfolgt mittels dieser Formel:
|
||||
</p>
|
||||
<div x-show="rate > 0">
|
||||
<p class="text-lg text-primary dark:text-primary-light"
|
||||
x-show="currentRate >= 1">
|
||||
x-show="rate >= 1">
|
||||
<b x-text="'{{ $toName }} = {{ $fromName }} × '
|
||||
+ prettyNumber(currentRate)"></b>
|
||||
+ prettyNumber(rate)"></b>
|
||||
</p>
|
||||
<p class="text-lg text-primary dark:text-primary-light"
|
||||
x-show="currentRate < 1">
|
||||
x-show="rate < 1">
|
||||
<b x-text="'{{ $toName }} = {{ $fromName }} / '
|
||||
+ prettyNumber(1 / currentRate)"></b>
|
||||
+ prettyNumber(1 / rate)"></b>
|
||||
</p>
|
||||
</div>
|
||||
<div x-show="!currentRate">
|
||||
<div x-show="!rate">
|
||||
{{ $rate := float .Params.rate }}
|
||||
{{ if ge $rate 1 }}
|
||||
<p class="text-lg text-primary dark:text-primary-light">
|
||||
|
||||
40
hugo/layouts/partials/conversion/currencies-data.html
Normal file
40
hugo/layouts/partials/conversion/currencies-data.html
Normal file
@@ -0,0 +1,40 @@
|
||||
<div x-data="{
|
||||
inputValue: '1',
|
||||
rate: 0,
|
||||
formula: '',
|
||||
ratesError: '',
|
||||
ratesUpdated: '',
|
||||
convert(v) { return v.times(this.rate); },
|
||||
async init() {
|
||||
const params = new URLSearchParams(window.location.search);
|
||||
const valueParam = params.get('v');
|
||||
if (valueParam) this.inputValue = valueParam.replace(/,/g, '.');
|
||||
|
||||
const pbUrl = document.querySelector('meta[name=\'pocketbase-url\']')?.content || 'https://www.alphabreed.com';
|
||||
try {
|
||||
const response = await fetch(pbUrl + '/api/collections/currencies/records');
|
||||
if (!response.ok) {
|
||||
this.ratesError = 'Wechselkurse konnten nicht geladen werden.';
|
||||
return;
|
||||
}
|
||||
const data = await response.json();
|
||||
const rates = {};
|
||||
let updated = '';
|
||||
for (const item of data.items || []) {
|
||||
rates[item.id] = item.rate;
|
||||
updated = item.updated;
|
||||
}
|
||||
const fromRate = '{{ .Params.from }}' === 'eur' ? 1 : rates['{{ .Params.from }}'];
|
||||
const toRate = '{{ .Params.to }}' === 'eur' ? 1 : rates['{{ .Params.to }}'];
|
||||
if (fromRate && toRate) {
|
||||
this.rate = parseFloat(toRate) / parseFloat(fromRate);
|
||||
this.ratesUpdated = new Date(updated).toLocaleString('de-DE', { day: '2-digit', month: 'long', year: 'numeric', hour: '2-digit', minute: '2-digit' });
|
||||
this.formula = '1 {{ .Params.from | upper }} = ' + prettyNumber(this.rate) + ' {{ .Params.to | upper }}';
|
||||
} else {
|
||||
this.ratesError = 'Wechselkurse nicht gefunden.';
|
||||
}
|
||||
} catch (e) {
|
||||
this.ratesError = 'Wechselkurse konnten nicht geladen werden.';
|
||||
}
|
||||
}
|
||||
}">
|
||||
9
hugo/layouts/partials/conversion/linear-data.html
Normal file
9
hugo/layouts/partials/conversion/linear-data.html
Normal file
@@ -0,0 +1,9 @@
|
||||
<div x-data="{
|
||||
inputValue: '1',
|
||||
convert: v => new Decimal(v).times({{ .Params.from_factor }}).dividedBy({{ .Params.to_factor }}),
|
||||
async init() {
|
||||
const params = new URLSearchParams(window.location.search);
|
||||
const valueParam = params.get('v');
|
||||
if (valueParam) this.inputValue = valueParam.replace(/,/g, '.');
|
||||
}
|
||||
}" x-init="init()">
|
||||
9
hugo/layouts/partials/conversion/temperatures-data.html
Normal file
9
hugo/layouts/partials/conversion/temperatures-data.html
Normal file
@@ -0,0 +1,9 @@
|
||||
<div x-data="{
|
||||
inputValue: '1',
|
||||
convert: {{ .Params.convertFormula | safeJS }},
|
||||
async init() {
|
||||
const params = new URLSearchParams(window.location.search);
|
||||
const valueParam = params.get('v');
|
||||
if (valueParam) this.inputValue = valueParam.replace(/,/g, '.');
|
||||
}
|
||||
}" x-init="init()">
|
||||
Reference in New Issue
Block a user