This commit is contained in:
relikd
2020-02-21 11:11:44 +01:00
commit ab2a7fa108
5 changed files with 265 additions and 0 deletions

7
LICENSE Normal file
View File

@@ -0,0 +1,7 @@
Copyright 2020 Oleg Geier
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

8
README.md Normal file
View File

@@ -0,0 +1,8 @@
# Soli-WG
Der solidarische WG Kosten Rechner. [Hier ausprobieren](https://relikd.github.io/Soli-WG/).
## License
[MIT License](https://opensource.org/licenses/MIT)

64
index.html Normal file
View File

@@ -0,0 +1,64 @@
<!doctype html>
<html lang="de">
<head>
<meta charset="utf-8">
<title>WG Kosten</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" type="text/css" href="style.css">
<script src="script.js"></script>
</head>
<body>
<!-- Templates -->
<div id="input-template" style="display:none">
<div class="inpt">
<input type="number" min="1" step="0.01" placeholder="0" onblur="leaveInput(this)" onkeyup="nextInput(event)">
<span>t</span>
</div>
</div>
<div id="room-template" style="display:none">
<div class="room">
<span>Zimmer</span> <num class="qm" sign="qm"></num>
<button type="button" onclick="delRoom(this)"></button>
<div class="residents">
<button type="button" onclick="delPerson(this)">-1</button>
<span>n</span> <i>1</i>
<button type="button" onclick="addPerson(this)">+1</button>
</div>
</div>
</div>
<!-- Main -->
<h1>WG Kosten <a id="shortlink"></a></h1>
<p>Nachzahlung? Mietkosten ausfüllen, Zusatzkosten frei lassen.</p>
<section id="flat">
<div>
<label>Mietkosten</label>
<num id="rent" sign="€"></num>
<p>Kosten die nach Fläche aufgeteilt werden (z.B. Kaltmiete, Heizung)</p>
</div><div>
<label>Zusatzkosten</label>
<num id="shared" sign="€" optional></num>
<p>Kosten die nach Person aufgeteilt werden (z.B. Internet, Strom, Telefon)</p>
</div><div>
<label>Gesamtfläche</label>
<num id="qm" sign="qm"></num>
<p>Summe aller Räume in Quadratmetern</p>
</div>
</section>
<h2>Zimmer</h2>
<section id="rooms">
<button id="add-room" type="button" onclick="addRoom(this)"></button>
</section>
<ul onclick="toggleOptions(this)"><li>Erweiterte Optionen</li></ul>
<section id="options">
<div>
<p>Solidaritätsfaktor: <b id="soli-val"></b></p>
<input id="soli" type="range" min="0" max="100" value="0" step="5" oninput="soliChanged()" onchange="soliChanged()">
</div>
</section>
<div id="error" hidden></div>
</body>
</html>

115
script.js Normal file
View File

@@ -0,0 +1,115 @@
window.addEventListener('load', function() {
function replInpt(n){
let t=getTemplate("input-template")
let x=t.firstElementChild; x.id=n.id; x.classList=n.classList
n.hasAttribute("optional") ? x.min=0 : x.required=true
t.lastElementChild.innerHTML=n.getAttribute("sign")
n.parentNode.replaceChild(t,n)
}
let x=document.getElementsByTagName("NUM")
for(var i=x.length-1;i>=0;i--) replInpt(x[i])
readGET()
toggleOptions()
soliChanged()
$_id("rent").focus()
})
function $_id(t){return document.getElementById(t)}
function leaveInput(n){n.value=Math.abs(Number(n.value)); update()}
function nextInput(e){
if(e.key==="Enter"&&(e.target.value||!e.target.required)){
if(e.target.classList.contains("add-person")) addPerson(e.target)
else focusNextInput(e.target)
}
}
function focusNextInput(n){
if(n.id!=="qm"){
let x=document.getElementsByTagName("INPUT")
for(var i=0;i<x.length-2;i++){//-2:soli
if(n==x[i]) return x[i+1].focus()
}
}; $_id("add-room").focus()
}
function addPerson(n){
let x=n.parentNode.querySelector("i"), v=parseInt(x.innerHTML)
x.innerHTML=(v?v+1:2); update()
}
function delPerson(n){
let x=n.parentNode.querySelector("i"), v=parseInt(x.innerHTML)
x.innerHTML=(v>2?v-1:1); update()
}
function addRoom(n,sz=null,ct=1,cli=false){
let x=getTemplate("room-template"), y=x.querySelector("input")
y.value=sz; x.querySelector(".residents>i").innerHTML=ct
n.parentNode.insertBefore(x,n)
if(!cli){ update(); y.focus() }
}
function delRoom(n){n.parentNode.parentNode.removeChild(n.parentNode); update()}
function soliChanged(){
$_id("soli-val").innerHTML=$_id("soli").value
update()
}
function toggleOptions(n) {
let x=$_id("options"), ul=x.previousElementSibling
x.className=x.className ? "" : "close"
x.style=x.className ? "display:none" : ""
ul.style="list-style-type:disclosure-"+(x.className ? "closed" : "open")
}
function getTemplate(t){return $_id(t).firstElementChild.cloneNode(true)}
function setError(t){let x=$_id("error"); x.innerHTML=t; x.hidden=!t}
function update() {
setError("")
writeGET()
let r=$_id("rooms"),
cl=r.querySelectorAll(".residents>span")
for(var i=0;i<cl.length;i++) cl[i].innerHTML=NaN
let rent=parseFloat($_id("rent").value),
shared=Number($_id("shared").value),
qm=parseFloat($_id("qm").value),
soli=Number($_id("soli").value)/100.0,
ra=r.querySelectorAll("INPUT.qm"),
rb=r.querySelectorAll(".residents")
if(!rent||!qm||!ra.length||ra.length!=rb.length) return
var ls=[], lr=[]
for(var i=0;i<ra.length;i++){
ls.push(parseFloat(ra[i].value)||0)
lr.push(parseInt(rb[i].querySelector("i").innerHTML)||1)
}
let s=ls.reduce((a,b)=>a+b,0), c=lr.reduce((a,b)=>a+b,0)
if(s>qm) return setError("Die Zimmer sind größer als die gesamte Wohnung")
let z=(shared+rent*(1-s/qm))/c
let avg=(shared+rent)/c
for(var i=0;i<rb.length;i++){
let n=z+(rent*ls[i]/qm)/lr[i]
n+=(avg-n)*soli
rb[i].querySelector("span").innerHTML=n.toFixed(2).replace(".",",")
}
}
function readGET() {
var GET={}
function q(t){if(GET[t]) $_id(t).value=GET[t]}
location.search.substr(1).split("&").forEach(function(x){GET[x.split("=")[0]]=x.split("=")[1]})
q("rent"); q("shared"); q("qm"); q("soli")
if(GET["r"]) {
let x=GET["r"].split("-"),
ar=$_id("add-room")
for(var i=0;i<x.length;i++){
let r=x[i].split(":")
addRoom(ar,r[0],r[1]||1,cli=true)
}
}
}
function writeGET() {
function q(t){x=$_id(t).value; return (x>0?t+"="+x+"&":"")}
let x=$_id("rooms").querySelectorAll(".room")
var qr=[]
for(var i=0;i<x.length;i++){
let s=parseFloat(x[i].querySelector("input").value)
let c=parseInt(x[i].querySelector(".residents>i").innerHTML)
qr.push(c>1 ? s+":"+c : s)
}
let qq=q("rent")+q("shared")+q("qm")+q("soli")+"r="+qr.join("-")
let link=$_id("shortlink")
link.href=location.pathname+"?"+qq
link.innerHTML="?"+qq
// history.replaceState(null,"", "?"+qq)
}

71
style.css Normal file
View File

@@ -0,0 +1,71 @@
#error{
position:fixed; top:0; left:0; width:100%;
background:#A00; color:white;
text-align:center;
font-size:1.2em;
padding:12px;
}
body{
max-width: 1200px;
font-family:sans-serif;
padding:20px;
background:#FFF;
color:#444;
}
#shortlink{
color: unset;
font-size: 0.4em
}
/* Grid Layout */
section{display:flex; flex-flow:row wrap}
section>*{
min-height:80px;
min-width:80px;
max-width:300px;
margin:3px;
padding:1em;
background:#BCD;
}
#add-room{font-size:3em; line-height:0}
/* Residents */
.residents>i{color:#555}
.residents>i::before{content:"("}
.residents>i::after{content:"x)"}
.residents>button{margin:2em 1em 0}
.residents>span::after{content:" €"}
.room{position:relative}
.room>button{position:absolute; top:0; right:0; scale:.75}
/* Customize Input Field */
.inpt{display:inline-table}
.inpt>input{width:5em}
.inpt>*{
display:table-cell;
padding:4px 6px;
border:1px solid #CCC;
}
.inpt>span{
color:#555;
background:#EEE;
vertical-align:middle;
border-left:0;
font-size:80%;
}
#soli-val{display:inline-block; width:2.5em}
#soli-val:after{content:"%"}
#soli{width:100%; padding:0; margin:1em 0 0}
/* Customize Button */
input:invalid{background:#FE8}
button:hover{background:#CCC}
button{
padding:4px 6px;
border:1px solid #CCC;
background:#EEE;
}
/* Dark Mode */
@media(prefers-color-scheme:dark){
body{background:#000; color:#FFF}
section>*{background:#123}
button{border:1px solid #444; background:#222; color:#FFF}
button:hover{background:#444}
.residents>i{color:#BBB}
}