Skip to content

Commit 169f975

Browse files
committed
Add option to share persistent URL
1 parent 834cb7f commit 169f975

File tree

4 files changed

+42
-18
lines changed

4 files changed

+42
-18
lines changed

app.js

Lines changed: 26 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -15,16 +15,6 @@ const countries = [
1515
];
1616

1717

18-
let selectedMonthRange = document.location.hash.split("#")[1] || null;
19-
let selectedCountries = document.location.hash.split("#")[2]?.split("+") || ["DE"];
20-
selectedCountries = [...new Set(selectedCountries).intersection(new Set(countries.map(c => c.code)))];
21-
let locale = document.location.hash.split("#")[3];
22-
23-
function updateHash() {
24-
document.location.hash = selectedMonthRange + "#" + selectedCountries.toSorted().join("+") + (locale !== "de" ? "#" + locale : "");
25-
}
26-
27-
2818
// ------------------------------------------------------------
2919
// Initialisierung
3020
const API_BASE = "https://openholidaysapi.org";
@@ -38,18 +28,39 @@ let i18n = {
3828
nationwide: "landesweit",
3929
mioResidents: "Mio. Einwohner",
4030
incompleteData: "Unvollständige Datenbasis",
41-
dataSources: "Datenquellen"
31+
dataSources: "Datenquellen",
32+
share: "Link teilen",
33+
shareInfo: "Persistenter Link wurde in die Zwischenablage kopiert"
4234
};
4335

36+
37+
const url = new URL(location)
38+
let selectedMonthRange = url.searchParams.get("range");
39+
let selectedCountries = url.searchParams.get("countries")?.split("|") || ["DE"];
40+
selectedCountries = [...new Set(selectedCountries).intersection(new Set(countries.map(c => c.code)))];
41+
let locale = url.searchParams.get("lang") || "de";
42+
43+
4444
const calendarContainer = document.getElementById("calendar");
4545
const sourceInfo = document.getElementById("sourceInfo");
4646
const yearSelect = document.getElementById("yearSelect");
4747
const countryList = document.getElementById("countryList");
48+
const shareLinkButton = document.getElementById("shareLink");
49+
shareLinkButton.addEventListener("click", e => {
50+
const url = new URL(location)
51+
url.searchParams.set("range", selectedMonthRange)
52+
url.searchParams.set("countries", selectedCountries.toSorted().join("|"));
53+
url.searchParams.set('lang', locale);
54+
window.prompt(i18n.shareInfo, url.toString());
55+
navigator.clipboard.writeText(url.toString());
56+
})
4857
document.getElementById("languageSelector").onclick = async e => {
4958
locale = locale === "de" ? "en" : "de";
50-
updateHash()
51-
document.location.reload();
59+
const url = new URL(location)
60+
url.searchParams.set('lang', locale);
61+
location.href = url.toString();
5262
};
63+
5364
document.addEventListener("DOMContentLoaded", async () => {
5465

5566
await i18ninit();
@@ -63,6 +74,8 @@ document.addEventListener("DOMContentLoaded", async () => {
6374
throw e
6475
}
6576

77+
shareLinkButton.text = i18n.share
78+
6679
sourceInfo.append(i18n.dataSources + ": ")
6780

6881
function sourceLink(url, label) {
@@ -143,7 +156,6 @@ function populateYearSelect() {
143156
yearSelect.value = selectedMonthRange
144157
yearSelect.addEventListener("change", async e => {
145158
selectedMonthRange = e.currentTarget.value;
146-
updateHash();
147159
await updateCalendar();
148160
});
149161
}
@@ -168,7 +180,6 @@ function renderCountrySelection() {
168180
selectedCountries.push(c.code);
169181
div.classList.add("active");
170182
}
171-
updateHash();
172183
await updateCalendar();
173184
});
174185
countryList.appendChild(div);

i18n/en.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,7 @@
99
"nationwide": "national",
1010
"mioResidents": "M people",
1111
"incompleteData": "Incomplete data",
12-
"dataSources": "Data sources"
12+
"dataSources": "Data sources",
13+
"share": "Share link",
14+
"shareInfo": "Persistent link was copied to the clipboard"
1315
}

index.html

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,10 @@ <h1 data-i18n="title" onclick="document.location='.'">Holidense: Feriendichte Ka
1515
gerade Urlaub hat. Die Daten basieren auf der
1616
<a href="https://www.openholidaysapi.org" target="_blank">OpenHolidays API</a>.
1717
</p>
18-
<button id="languageSelector">🌐</button>
18+
<div class="option-icons">
19+
<button class="option-icon" id="languageSelector" title="Toggle language EN/DE &#013;Sprache wechseln DE/EN">🌐</button>
20+
<button class="option-icon" id="shareLink">🔗</button>
21+
</div>
1922
</header>
2023

2124
<section id="controls">

styles.css

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,18 +51,26 @@ a:hover {
5151
}
5252
}
5353

54-
#languageSelector {
54+
.option-icons {
5555
position: absolute;
5656
right: 0;
5757
top: 0;
5858
margin: 10px;
59+
}
60+
61+
.option-icon {
5962
cursor: pointer;
6063
border: none;
6164
background: none;
6265
color: #7fb6ff;
66+
opacity: 0.7;
6367
font-size: 1rem;
6468
}
6569

70+
.option-icon:hover {
71+
opacity: 1;
72+
}
73+
6674
#controls, #countrySelect {
6775
display: flex;
6876
flex-wrap: wrap;

0 commit comments

Comments
 (0)