MediaWiki:SmoothiesDebtCalculator.js: Difference between revisions
Jump to navigation
Jump to search
No edit summary |
No edit summary |
||
| (5 intermediate revisions by the same user not shown) | |||
| Line 1: | Line 1: | ||
(function () { | (function () { | ||
function parseAmount(value) { | function parseAmount(value) { | ||
| Line 7: | Line 8: | ||
.replace(/,/g,"") | .replace(/,/g,"") | ||
.trim(); | .trim(); | ||
if (value.endsWith("b")) { | if (value.endsWith("b")) { | ||
return Number(value.slice(0,-1)) * 1000000000; | return Number(value.slice(0,-1)) * 1000000000; | ||
} | } | ||
if (value.endsWith("m")) { | if (value.endsWith("m")) { | ||
return Number(value.slice(0,-1)) * 1000000; | return Number(value.slice(0,-1)) * 1000000; | ||
} | } | ||
if (value.endsWith("k")) { | if (value.endsWith("k")) { | ||
return Number(value.slice(0,-1)) * 1000; | return Number(value.slice(0,-1)) * 1000; | ||
} | } | ||
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 && saved !== ""){ | |||
input.value = saved; | |||
} | |||
}); | |||
} | |||
function calculateDebt() { | function calculateDebt() { | ||
let totalBox = document.getElementById("total"); | |||
if(!totalBox){ | |||
return; | |||
} | |||
let pkp = | let pkp = | ||
parseAmount(document.getElementById("pkp-amount").value); | parseAmount( | ||
document.getElementById("pkp-amount").value | |||
); | |||
let dp = | let dp = | ||
parseAmount(document.getElementById("dp-price").value) * | parseAmount( | ||
parseAmount(document.getElementById("dp-amount").value); | document.getElementById("dp-price").value | ||
) | |||
* | |||
parseAmount( | |||
document.getElementById("dp-amount").value | |||
); | |||
let credits = | let credits = | ||
parseAmount(document.getElementById("credits-price").value) * | parseAmount( | ||
parseAmount(document.getElementById("credits-amount").value); | document.getElementById("credits-price").value | ||
) | |||
* | |||
parseAmount( | |||
document.getElementById("credits-amount").value | |||
); | |||
| Line 43: | Line 133: | ||
totalBox.innerHTML = | |||
Math.floor(total).toLocaleString() + " PKP"; | Math.floor(total).toLocaleString() + " PKP"; | ||
} | } | ||
function createCalculator() { | function createCalculator(){ | ||
if ( | // Stop MediaWiki hook from rebuilding calculator | ||
if(document.getElementById("pkp-amount")){ | |||
return; | |||
} | |||
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> | <label>Trade Price (PKP)</label> | ||
<input id=" | <input id="credits-price" type="text" value="0"> | ||
</ | <label>Amount</label> | ||
<input id="credits-amount" type="text" value="0"> | |||
`; | |||
// Make images not selectable with TAB | |||
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(); | |||
} | } | ||
| Line 125: | Line 235: | ||
mw.hook("wikipage.content").add(function(){ | mw.hook("wikipage.content").add(function(){ | ||
createCalculator(); | createCalculator(); | ||
}); | }); | ||
Latest revision as of 11:12, 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 && saved !== ""){
input.value = saved;
}
});
}
function calculateDebt() {
let totalBox = document.getElementById("total");
if(!totalBox){
return;
}
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;
totalBox.innerHTML =
Math.floor(total).toLocaleString() + " PKP";
}
function createCalculator(){
// Stop MediaWiki hook from rebuilding calculator
if(document.getElementById("pkp-amount")){
return;
}
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">
`;
// Make images not selectable with TAB
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();
});
})();