Skip to content

Commit

Permalink
Fix a few bugs and make things nicer:
Browse files Browse the repository at this point in the history
- Runs are no longer deleted on load
- the filename is based on the date
- the map re-centers when a run is loaded from a file
- the upload form's text is reset when a run is uploaded
  • Loading branch information
vincens2005 committed Dec 26, 2021
1 parent f5fc34b commit 1e8bbbe
Showing 1 changed file with 14 additions and 6 deletions.
20 changes: 14 additions & 6 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ function runToJson(run: CurrentRun): string {
return JSON.stringify(runJSON);
}

function jsonToRun(json: string): boolean {
function jsonToRun(json: string, changeView: boolean = false): boolean {
try {
let runJSON = JSON.parse(json);
let lngLat = new LngLat(runJSON.start.lng, runJSON.start.lat);
Expand All @@ -202,8 +202,14 @@ function jsonToRun(json: string): boolean {
}
prev = lngLat;
}
clearRun();
clearRun(false);
currentRun = newRun;
if (changeView) {
map.flyTo({
center: [runJSON.start.lng, runJSON.start.lat],
zoom: 14
});
}
return true;
}
catch (err) {
Expand All @@ -223,7 +229,8 @@ function downloadRun(): void {
let url = URL.createObjectURL(file);
let link = document.createElement("a");
link.href = url;
link.download = "currentRun.runmap";
let date = new Date();
link.download = `run-${date.getMonth() + 1}-${date.getDate()}-${date.getFullYear() % 100}.runmap`;
link.click();
}

Expand All @@ -238,7 +245,7 @@ async function loadRun(e: Event): Promise<void> {
e.preventDefault();
if (!runInput.files.length) return void (runInput.parentElement.querySelector("span").innerText = "No file selected");
let json = await runInput.files[0].text();
let loadsuccessful = jsonToRun(json);
let loadsuccessful = jsonToRun(json, true);
if (!loadsuccessful) return void (runInput.parentElement.querySelector("span").innerText = "Error loading run");
closeMenu();
showRunButtons();
Expand Down Expand Up @@ -333,11 +340,11 @@ function removeLastSegment(): void {
preferenceService.saveLastRun(runToJson(currentRun));
}

function clearRun(): void {
function clearRun(commit: boolean = true): void {
while (currentRun) {
removeLastSegment();
}
preferenceService.saveLastRun(runToJson(currentRun));
if (commit) preferenceService.saveLastRun(runToJson(currentRun));
}

function updateLengthElement(): void {
Expand Down Expand Up @@ -376,6 +383,7 @@ function closeMenu(hideForm: boolean = true) {
uploadContainer.setAttribute('aria-hidden', 'true');
scrimElement.classList.remove('scrim-shown');
scrimElement.classList.add('scrim-hidden');
runInput.parentElement.querySelector("span").innerText = "drag a file or click here";
}

function setFollowRoads(value: boolean) {
Expand Down

0 comments on commit 1e8bbbe

Please sign in to comment.