MediaWiki:SmoothiesDebtCalculator.js: Difference between revisions
Jump to navigation
Jump to search
No edit summary |
No edit summary |
||
| Line 26: | Line 26: | ||
return Number(value) || 0; | 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; | |||
} | |||
}); | |||
} | } | ||
| Line 65: | Line 128: | ||
document.getElementById("total").innerHTML = | document.getElementById("total").innerHTML = | ||
Math.floor(total).toLocaleString() + " PKP"; | Math.floor(total).toLocaleString() + " PKP"; | ||
let mercherFee = total * 0.08; | |||
document.getElementById("mercher-fee").innerHTML = | |||
Math.floor(mercherFee).toLocaleString() + " PKP"; | |||
} | } | ||
| Line 79: | Line 149: | ||
if(!pkp || !dp || !credits) { | if (!pkp || !dp || !credits) { | ||
return; | return; | ||
} | } | ||
| Line 88: | Line 158: | ||
<label>Amount</label> | <label>Amount</label> | ||
<input id="pkp-amount" value="0"> | <input id="pkp-amount" type="text" value="0"> | ||
`; | `; | ||
| Line 97: | Line 167: | ||
<label>Trade Price (PKP)</label> | <label>Trade Price (PKP)</label> | ||
<input id="dp-price" value="0"> | <input id="dp-price" type="text" value="0"> | ||
<label>Amount</label> | <label>Amount</label> | ||
<input id="dp-amount" value="0"> | <input id="dp-amount" type="text" value="0"> | ||
`; | `; | ||
| Line 109: | Line 179: | ||
<label>Trade Price (PKP)</label> | <label>Trade Price (PKP)</label> | ||
<input id="credits-price" value="0"> | <input id="credits-price" type="text" value="0"> | ||
<label>Amount</label> | <label>Amount</label> | ||
<input id="credits-amount" value="0"> | <input id="credits-amount" type="text" value="0"> | ||
`; | `; | ||
| Line 124: | Line 194: | ||
input.addEventListener( | input.addEventListener( | ||
"input", | "input", | ||
calculateDebt | function(){ | ||
calculateDebt(); | |||
saveValues(); | |||
} | |||
); | ); | ||
}); | }); | ||
loadValues(); | |||
calculateDebt(); | |||
} | } | ||
Revision as of 11:06, 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" value="0">
`;
dp.innerHTML = `
<label>Trade Price (PKP)</label>
<input id="dp-price" type="text" value="0">
<label>Amount</label>
<input id="dp-amount" type="text" value="0">
`;
credits.innerHTML = `
<label>Trade Price (PKP)</label>
<input id="credits-price" type="text" value="0">
<label>Amount</label>
<input id="credits-amount" type="text" value="0">
`;
document
.querySelectorAll(".debt-item input")
.forEach(function(input){
input.addEventListener(
"input",
function(){
calculateDebt();
saveValues();
}
);
});
loadValues();
calculateDebt();
}
mw.hook("wikipage.content").add(function(){
createCalculator();
});
})();