mirror of
https://github.com/soapingtime/diyhrt.git
synced 2026-09-05 04:20:20 +02:00
re-initialize and make some changes. repository got to around 2 gigabytes and needed to be more efficient
This commit is contained in:
@@ -0,0 +1,83 @@
|
||||
$(document).ready( function () {
|
||||
if(!$("span#currency-container").length == 0) {
|
||||
//set a default currency
|
||||
if (typeof localStorage.currency === 'undefined') {
|
||||
localStorage.currency = 'USD';
|
||||
console.log('No currency stored, Default ' + localStorage.currency + ' Selected.');
|
||||
}
|
||||
|
||||
$("#currency-container").append("<select id=\"currencySelector\"></select>");
|
||||
|
||||
var
|
||||
selector = document.getElementById("currencySelector");
|
||||
var
|
||||
currencyElements = document.getElementsByClassName("currency");
|
||||
|
||||
selector.innerHTML = `
|
||||
<option value="usd">USD</option>
|
||||
<option value="eur">EUR</option>
|
||||
<option value="gbp">GBP</option>
|
||||
<option value="cad">CAD</option>
|
||||
<option value="aud">AUD</option>
|
||||
<option value="nok">NOK</option>
|
||||
<option value="disabled">none</option>
|
||||
`;
|
||||
|
||||
|
||||
//enable default
|
||||
selector.value = localStorage.currency.toLowerCase();
|
||||
console.log(localStorage.currency + ' Selected.');
|
||||
|
||||
var
|
||||
currencySymbols = {
|
||||
USD: "$",
|
||||
EUR: "€",
|
||||
GBP: "£",
|
||||
NOK: "kr",
|
||||
AUD: "$",
|
||||
CAD: "$"
|
||||
};
|
||||
|
||||
function curconvert() {
|
||||
if (localStorage.currency != 'DISABLED') {
|
||||
var
|
||||
toCurrency = selector.value.toUpperCase();
|
||||
|
||||
var
|
||||
currencySymbol = currencySymbols[toCurrency];
|
||||
|
||||
for (var i=0,l=currencyElements.length; i<l; ++i) {
|
||||
var
|
||||
el = currencyElements[i];
|
||||
var
|
||||
fromCurrency = el.getAttribute("data-currencyName").toUpperCase();
|
||||
|
||||
if (fromCurrency in usdChangeRate) {
|
||||
var
|
||||
fromCurrencyToUsdAmount = parseFloat(el.innerHTML) / usdChangeRate[fromCurrency];
|
||||
var
|
||||
toCurrencyAmount = fromCurrencyToUsdAmount * usdChangeRate[toCurrency];
|
||||
|
||||
el.innerHTML = toCurrencyAmount.toFixed(2) + "<span>" + currencySymbol + "</span>";
|
||||
el.setAttribute("data-currencyName",toCurrency);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
//run function
|
||||
curconvert();
|
||||
|
||||
//on currency selection, save selected to browser and run again
|
||||
selector.onchange = function() {
|
||||
localStorage.currency = selector.value.toUpperCase();
|
||||
console.log(localStorage.currency + ' Selected.');
|
||||
|
||||
curconvert();
|
||||
|
||||
if (localStorage.currency == 'DISABLED') {
|
||||
location.reload();
|
||||
}
|
||||
}
|
||||
}
|
||||
} );
|
||||
@@ -0,0 +1,2 @@
|
||||
var usdChangeRate = { USD: 1.0, EUR: 1.01991, GBP: 0.896861, NOK: 10.886065, THB: 37.803038, AUD: 1.561524, CAD: 1.38327 };
|
||||
//last updated on 2022-10-02 16:10:03 via https://free.currencyconverterapi.com/.
|
||||
Reference in New Issue
Block a user