@@ -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
3020const 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+
4444const calendarContainer = document . getElementById ( "calendar" ) ;
4545const sourceInfo = document . getElementById ( "sourceInfo" ) ;
4646const yearSelect = document . getElementById ( "yearSelect" ) ;
4747const 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+ } )
4857document . 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+
5364document . 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 ) ;
0 commit comments