MediaWiki:SmoothiesDebtCalculator.js: Difference between revisions
Jump to navigation
Jump to search
No edit summary |
No edit summary |
||
| Line 76: | Line 76: | ||
let input = document.getElementById(id); | let input = document.getElementById(id); | ||
let saved = | let saved = localStorage.getItem( | ||
"smoothie-" + id | |||
); | |||
| Line 128: | Line 127: | ||
document.getElementById("total").innerHTML = | document.getElementById("total").innerHTML = | ||
Math.floor(total).toLocaleString() + " PKP"; | Math.floor(total).toLocaleString() + " PKP"; | ||
| Line 158: | Line 156: | ||
<label>Amount</label> | <label>Amount</label> | ||
<input id="pkp-amount" type="text | <input id="pkp-amount" type="text"> | ||
`; | `; | ||
| Line 167: | Line 165: | ||
<label>Trade Price (PKP)</label> | <label>Trade Price (PKP)</label> | ||
<input id="dp-price" type="text | <input id="dp-price" type="text"> | ||
<label>Amount</label> | <label>Amount</label> | ||
<input id="dp-amount" type="text | <input id="dp-amount" type="text"> | ||
`; | `; | ||
| Line 179: | Line 177: | ||
<label>Trade Price (PKP)</label> | <label>Trade Price (PKP)</label> | ||
<input id="credits-price" type="text | <input id="credits-price" type="text"> | ||
<label>Amount</label> | <label>Amount</label> | ||
<input id="credits-amount" type="text | <input id="credits-amount" type="text"> | ||
`; | `; | ||
// Remove image links from tab order | |||
document | |||
.querySelectorAll(".debt-calculator a") | |||
.forEach(function(link){ | |||
link.setAttribute( | |||
"tabindex", | |||
"-1" | |||
); | |||
}); | |||
| Line 207: | Line 219: | ||
loadValues(); | loadValues(); | ||
calculateDebt(); | calculateDebt(); | ||
Revision as of 11:08, 18 July 2026
(function () {
function parseAmount(value) {
value = value
.toLowerCase()
.replace(/,/g,"")
.trim();
if (value.endsWith("b")) {
return Number(value.slice(0,-1)) * 1000000000;
}
if (value.endsWith("m")) {
return Number(value.slice(0,-1)) * 1000000;
}
if (value.endsWith("k")) {
return Number(value.slice(0,-1)) * 1000;
}
return Number(value) || 0;
}
function saveValues() {
let fields = [
"pkp-amount",
"dp-price",
"dp-amount",
"credits-price",
"credits-amount"
];
fields.forEach(function(id){
let input = document.getElementById(id);
if(input){
localStorage.setItem(
"smoothie-" + id,
input.value
);
}
});
}
function loadValues() {
let fields = [
"pkp-amount",
"dp-price",
"dp-amount",
"credits-price",
"credits-amount"
];
fields.forEach(function(id){
let input = document.getElementById(id);
let saved = localStorage.getItem(
"smoothie-" + id
);
if(input && saved !== null){
input.value = saved;
}
});
}
function calculateDebt() {
let pkp =
parseAmount(
document.getElementById("pkp-amount").value
);
let dp =
parseAmount(
document.getElementById("dp-price").value
)
*
parseAmount(
document.getElementById("dp-amount").value
);
let credits =
parseAmount(
document.getElementById("credits-price").value
)
*
parseAmount(
document.getElementById("credits-amount").value
);
let total = pkp + dp + credits;
document.getElementById("total").innerHTML =
Math.floor(total).toLocaleString() + " PKP";
let mercherFee = total * 0.08;
document.getElementById("mercher-fee").innerHTML =
Math.floor(mercherFee).toLocaleString() + " PKP";
}
function createCalculator(){
let pkp = document.getElementById("pkp-inputs");
let dp = document.getElementById("dp-inputs");
let credits = document.getElementById("credits-inputs");
if (!pkp || !dp || !credits) {
return;
}
pkp.innerHTML = `
<label>Amount</label>
<input id="pkp-amount" type="text">
`;
dp.innerHTML = `
<label>Trade Price (PKP)</label>
<input id="dp-price" type="text">
<label>Amount</label>
<input id="dp-amount" type="text">
`;
credits.innerHTML = `
<label>Trade Price (PKP)</label>
<input id="credits-price" type="text">
<label>Amount</label>
<input id="credits-amount" type="text">
`;
// Remove image links from tab order
document
.querySelectorAll(".debt-calculator a")
.forEach(function(link){
link.setAttribute(
"tabindex",
"-1"
);
});
document
.querySelectorAll(".debt-item input")
.forEach(function(input){
input.addEventListener(
"input",
function(){
calculateDebt();
saveValues();
}
);
});
loadValues();
calculateDebt();
}
mw.hook("wikipage.content").add(function(){
createCalculator();
});
})();