From 04a29ae8cd2882d94554a07162db04cc91912cac Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Stefan=20M=C3=BChlinghaus?= Date: Fri, 29 May 2026 21:39:21 +0200 Subject: [PATCH] Runtime Engine generisch --- hugo/content/_content.gotmpl | 4 -- hugo/layouts/conversion/single.html | 7 ++- hugo/static/js/converter.js | 91 +++-------------------------- 3 files changed, 13 insertions(+), 89 deletions(-) diff --git a/hugo/content/_content.gotmpl b/hugo/content/_content.gotmpl index 3914fc4..21a8a30 100644 --- a/hugo/content/_content.gotmpl +++ b/hugo/content/_content.gotmpl @@ -59,8 +59,6 @@ {{ $description := printf "Konvertieren Sie %s in %s. Einfache und schnelle Umrechnung mit Einheitsumrechner." $fromName $toName }} - {{ $fromCurrency := $fromCode }} - {{ $toCurrency := $toCode }} {{ $fromRate := 1 }} {{ if ne $fromCode "eur" }} {{ $fromRate = index $pbRates $fromCode }} @@ -79,8 +77,6 @@ "slug" $slug "description" $description "engine" "runtime" - "currency_from" $fromCurrency - "currency_to" $toCurrency "rate" $rate "rates_updated" $latestUpdate }} diff --git a/hugo/layouts/conversion/single.html b/hugo/layouts/conversion/single.html index 63d4586..86a124a 100644 --- a/hugo/layouts/conversion/single.html +++ b/hugo/layouts/conversion/single.html @@ -13,8 +13,11 @@ "{ toIntermediate: v => %s, fromIntermediate: v => %s }" $toExpr $fromExpr }} {{ else if eq .Params.engine "runtime" }} - {{ $config = dict "fromCurrency" .Params.from - "toCurrency" .Params.to | jsonify }} + {{ $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(); for (const item of data.items || []) { this.rates[item.id] = item.rate; } } 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 }} {{ end }} {{ $availableUnits := partial "available-units.html" (dict "category" .Params.category "units" $catData.units) }} diff --git a/hugo/static/js/converter.js b/hugo/static/js/converter.js index cee4e4f..682086a 100644 --- a/hugo/static/js/converter.js +++ b/hugo/static/js/converter.js @@ -36,92 +36,12 @@ function navActions() { * @returns {object} Alpine component data */ function createConverter(engine, config) { - if (engine === 'runtime') { - return { - inputValue: '1', - result: '', - rates: {}, - ratesError: '', - async init() { - await this.loadRates(); - const params = new URLSearchParams( - window.location.search); - const valueParam = params.get('v'); - if (valueParam) { - this.inputValue = valueParam.replace(/,/g, '.'); - } - this.$watch('inputValue', val => { - if (val && val.indexOf(',') !== -1) { - this.inputValue = val.replace(/,/g, '.'); - } - }); - this.calculate(); - }, - async loadRates() { - 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(); - for (const item of data.items || []) { - this.rates[item.id] = item.rate; - } - } catch (e) { - this.ratesError = - 'Wechselkurse konnten nicht geladen werden.'; - } - }, - calculate() { - try { - this.result = ''; - if (Object.keys(this.rates).length === 0) { - return; - } - const normalized = parseNumber( - this.inputValue); - if (!normalized || isNaN(normalized)) { - return; - } - const fromCode = config.fromCurrency || ''; - const toCode = config.toCurrency || ''; - const fromRate = fromCode === 'eur' - ? 1 : this.rates[fromCode]; - const toRate = toCode === 'eur' - ? 1 : this.rates[toCode]; - if (!fromRate || !toRate) { - return; - } - const value = new Decimal(normalized); - const rate = new Decimal(toRate) - .dividedBy(new Decimal(fromRate)); - const rawResult = value.times(rate); - this.result = prettyNumber(rawResult); - const normalizedInputValue = this.inputValue - .replace(/,/g, '.'); - if (normalizedInputValue - && normalizedInputValue !== '0') { - history.replaceState( - null, '', '?v=' + normalizedInputValue); - } - } catch (e) { - this.result = ''; - } - } - }; - } - return { inputValue: '1', result: '', + rates: {}, ratesError: '', - init() { + async init() { const params = new URLSearchParams( window.location.search); const valueParam = params.get('v'); @@ -133,6 +53,9 @@ function createConverter(engine, config) { this.inputValue = val.replace(/,/g, '.'); } }); + if (typeof config.init === 'function') { + await config.init.call(this); + } this.calculate(); }, calculate() { @@ -144,7 +67,9 @@ function createConverter(engine, config) { } const value = new Decimal(normalized); let rawResult; - if (engine === 'linear') { + if (typeof config.convert === 'function') { + rawResult = config.convert.call(this, value); + } else if (engine === 'linear') { const fromFactor = new Decimal( config.fromFactor); const toFactor = new Decimal(