From 174067d94691b152204d764bb1a38558ced7f185 Mon Sep 17 00:00:00 2001 From: Chandler Swift Date: Thu, 7 Mar 2024 00:49:53 -0600 Subject: [PATCH] Add save function and stub out for restore --- index.html | 83 +++++++++++++++++++++++++++++++++++------------------- 1 file changed, 54 insertions(+), 29 deletions(-) diff --git a/index.html b/index.html index ce58eaa..89dae19 100644 --- a/index.html +++ b/index.html @@ -11,12 +11,18 @@

Name All the Cities

-
- - +
+
+ + +
+
+ + +
@@ -101,20 +107,17 @@ "All above 50k": cities => cities.filter(city => city.pop >= 50000), "All above 25k": cities => cities.filter(city => city.pop >= 25000), }, - guess() { - const rank = this.simplified_cities.indexOf(simplify(this.city_guess)) - if (rank >= 0) { - const city = this.state_cities[rank]; + async mounted() { + // Fire off this request ASAP + this.shapes_request = fetch("data/states.geojson"); - if (!city.guessed) { - city.guessed = true; - this.message = `${city.name} (population ${city.pop}) is the ${ordinal_suffix_of(rank + 1)} most populated city in ${this.state}.`; - this.city_guess = ""; - this.draw(); - } else { - this.message = `Already guessed ${city.name} (population ${city.pop}, rank ${rank}).`; - this.city_guess = ""; - } + // Check if there's a parameter in the URL already + this.state_name = (new URLSearchParams(window.location.search)).get('state'); + if (this.state_name) { + this.launch(); + } else { + const response = await fetch("data/states.json"); + this.states = await response.json(); } }, async launch() { @@ -136,17 +139,39 @@ this.draw(); }, - async mounted() { - // Fire off this request ASAP - this.shapes_request = fetch("data/states.geojson"); + async save() { + const data = { + state: this.state_name, + cities: this.state_cities, + time: new Date, + }; + // https://stackoverflow.com/a/30800715 + const dataStr = "data:text/json;charset=utf-8," + encodeURIComponent(JSON.stringify(data)); + const downloadAnchorNode = document.createElement('a'); + downloadAnchorNode.setAttribute("href", dataStr); + const safeStateName = this.state_name.replace(/[^A-Za-z]/, '-'); + const datestring = `${data.time.getFullYear()}-${data.time.getMonth() + 1}-${data.time.getDate()}`; + downloadAnchorNode.setAttribute("download", `name-all-cities-${safeStateName}-${datestring}.json`); + document.body.appendChild(downloadAnchorNode); // required for firefox + downloadAnchorNode.click(); + downloadAnchorNode.remove(); + }, + async restore() { + }, + guess() { + const rank = this.simplified_cities.indexOf(simplify(this.city_guess)) + if (rank >= 0) { + const city = this.state_cities[rank]; - // Check if there's a parameter in the URL already - this.state_name = (new URLSearchParams(window.location.search)).get('state'); - if (this.state_name) { - this.launch(); - } else { - const response = await fetch("data/states.json"); - this.states = await response.json(); + if (!city.guessed) { + city.guessed = true; + this.message = `${city.name} (population ${city.pop}) is the ${ordinal_suffix_of(rank + 1)} most populated city in ${this.state}.`; + this.city_guess = ""; + this.draw(); + } else { + this.message = `Already guessed ${city.name} (population ${city.pop}, rank ${rank}).`; + this.city_guess = ""; + } } }, draw() {