Fixes und Währungsumstellung
This commit is contained in:
@@ -1,107 +1,5 @@
|
||||
'use strict';
|
||||
|
||||
/**
|
||||
* Alpine.js navigation helpers for conversion page.
|
||||
* Handles navigation via hidden AJAX link.
|
||||
*/
|
||||
function navActions() {
|
||||
return {
|
||||
navigate(url) {
|
||||
this.$refs.ajaxLink.href = url;
|
||||
this.$nextTick(() => this.$refs.ajaxLink.click());
|
||||
},
|
||||
navigateWithValue(url) {
|
||||
const el = document.getElementById('value');
|
||||
const val = el ? el.value.replace(/,/g, '.') : '1';
|
||||
this.$refs.ajaxLink.href = url + '?v=' + val;
|
||||
this.$nextTick(() => this.$refs.ajaxLink.click());
|
||||
},
|
||||
navigateWithResult(url) {
|
||||
const el = document.getElementById('result');
|
||||
const val = el ? el.value.replace(/,/g, '.') : '';
|
||||
if (val) {
|
||||
this.$refs.ajaxLink.href = url + '?v=' + val;
|
||||
} else {
|
||||
this.$refs.ajaxLink.href = url;
|
||||
}
|
||||
this.$nextTick(() => this.$refs.ajaxLink.click());
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Factory function for Alpine.js converter components.
|
||||
* @param {string} engine - 'linear', 'intermediate' or 'runtime'
|
||||
* @param {object} config - Engine-specific configuration
|
||||
* @returns {object} Alpine component data
|
||||
*/
|
||||
function createConverter(engine, config) {
|
||||
return {
|
||||
inputValue: '1',
|
||||
result: '',
|
||||
rates: {},
|
||||
ratesError: '',
|
||||
ratesUpdated: '',
|
||||
currentRate: 0,
|
||||
async init() {
|
||||
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, '.');
|
||||
}
|
||||
});
|
||||
if (typeof config.init === 'function') {
|
||||
await config.init.call(this);
|
||||
}
|
||||
this.calculate();
|
||||
},
|
||||
calculate() {
|
||||
try {
|
||||
const normalized = parseNumber(this.inputValue);
|
||||
if (!normalized || isNaN(normalized)) {
|
||||
this.result = '';
|
||||
return;
|
||||
}
|
||||
const value = new Decimal(normalized);
|
||||
let rawResult;
|
||||
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(
|
||||
config.toFactor);
|
||||
rawResult = value.times(fromFactor)
|
||||
.dividedBy(toFactor);
|
||||
} else if (engine === 'intermediate') {
|
||||
let v = value.toNumber();
|
||||
v = config.toIntermediate(v);
|
||||
v = config.fromIntermediate(v);
|
||||
rawResult = new Decimal(v);
|
||||
} else {
|
||||
this.result = '';
|
||||
return;
|
||||
}
|
||||
this.result = prettyNumber(rawResult);
|
||||
const normalizedInputValue = this.inputValue
|
||||
.replace(/,/g, '.');
|
||||
if (normalizedInputValue
|
||||
&& normalizedInputValue !== '0') {
|
||||
history.replaceState(
|
||||
null, '', '?v=' + normalizedInputValue);
|
||||
}
|
||||
} catch (e) {
|
||||
this.result = '';
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Formats a number as plain digits with dot as decimal separator.
|
||||
* @param {Decimal|string|number} num
|
||||
@@ -126,7 +24,7 @@ function prettyNumber(num, minPrecision, maxPrecision) {
|
||||
// Larger numbers need fewer decimal places for readable output;
|
||||
// smaller numbers get more to stay meaningful.
|
||||
let precision = maxPrecision;
|
||||
if (absVal.gte(1000000)) {
|
||||
if (absVal.gte(10000)) {
|
||||
precision = 2;
|
||||
} else if (absVal.gte(1000)) {
|
||||
precision = 4;
|
||||
@@ -160,3 +58,39 @@ function parseNumber(input) {
|
||||
}
|
||||
return input.replace(/,/g, '.') || '0';
|
||||
}
|
||||
|
||||
/**
|
||||
* Prepares input value for conversion.
|
||||
* Returns a Decimal, or 1 if input is empty.
|
||||
* @param {string} input - User input from field
|
||||
* @returns {Decimal} Prepared value
|
||||
*/
|
||||
function prepareValue(input) {
|
||||
if (!input || input.trim() === '') {
|
||||
return new Decimal(1);
|
||||
}
|
||||
return new Decimal(input.replace(/,/g, '.'));
|
||||
}
|
||||
|
||||
/**
|
||||
* Updates the URL with the current input value.
|
||||
* @param {string} value - The input value
|
||||
*/
|
||||
function updateUrl(value) {
|
||||
const normalized = value ? value.replace(/,/g, '.') : '';
|
||||
if (normalized && normalized !== '0') {
|
||||
history.replaceState(null, '', '?v=' + normalized);
|
||||
} else {
|
||||
history.replaceState(null, '', '?v=1');
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Navigates to a new URL with the current input value as parameter.
|
||||
* @param {string} url - Target URL
|
||||
* @param {string} value - Current input value
|
||||
*/
|
||||
function navigate(url, value) {
|
||||
const normalized = value ? value.replace(/,/g, '.') : '1';
|
||||
window.location.href = url + '?v=' + normalized;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user