UX: move map to note location if a hash is given in the note, fix #2311

This commit is contained in:
Pieter Vander Vennet 2024-12-13 14:43:53 +01:00
parent cc28932534
commit 3fb8f7342f
3 changed files with 47 additions and 34 deletions

View file

@ -4,10 +4,12 @@ import { LocalStorageSource } from "../Web/LocalStorageSource"
import { QueryParameters } from "../Web/QueryParameters"
import Hash from "../Web/Hash"
import OsmObjectDownloader from "../Osm/OsmObjectDownloader"
import { OsmObject } from "../Osm/OsmObject"
import Constants from "../../Models/Constants"
import { Utils } from "../../Utils"
import { GeoLocationState } from "../State/GeoLocationState"
import { OsmConnection } from "../Osm/OsmConnection"
"use strict"
/**
* This actor is responsible to set the map location.
@ -25,7 +27,7 @@ export default class InitialMapPositioning {
public location: UIEventSource<{ lon: number; lat: number }>
public useTerrain: Store<boolean>
constructor(layoutToUse: ThemeConfig, geolocationState: GeoLocationState) {
constructor(layoutToUse: ThemeConfig, geolocationState: GeoLocationState, osmConnection: OsmConnection) {
function localStorageSynced(
key: string,
deflt: number,
@ -47,7 +49,6 @@ export default class InitialMapPositioning {
return src
}
const initialHash = Hash.hash.data
// -- Location control initialization
this.zoom = localStorageSynced(
@ -72,6 +73,7 @@ export default class InitialMapPositioning {
})
this.useTerrain = new ImmutableStore<boolean>(layoutToUse.enableTerrain)
const initialHash = Hash.hash.data
if (initialHash?.match(/^(node|way|relation)\/[0-9]+$/)) {
// We pan to the selected element
const [type, id] = initialHash.split("/")
@ -88,6 +90,15 @@ export default class InitialMapPositioning {
const [lat, lon] = osmObject.centerpoint()
this.location.setData({ lon, lat })
})
} else if (layoutToUse.id === "notes" && initialHash?.match(/[0-9]+/)) {
console.log("Loading note", initialHash)
const noteId = Number(initialHash)
osmConnection.getNote(noteId).then(note => {
const [lon, lat] = note.geometry.coordinates
console.log("Got note:", note)
this.location.set({ lon, lat })
}
)
} else if (
Constants.GeoIpServer &&
lat.data === defaultLat &&

View file

@ -5,6 +5,7 @@ import { Utils } from "../../Utils"
import { LocalStorageSource } from "../Web/LocalStorageSource"
import { AuthConfig } from "./AuthConfig"
import Constants from "../../Models/Constants"
import { Feature, Point } from "geojson"
interface OsmUserInfo {
id: number
@ -218,7 +219,7 @@ export class OsmConnection {
this.auth.xhr(
{
method: "GET",
path: "/api/0.6/user/details",
path: "/api/0.6/user/details"
},
(err, details: XMLDocument) => {
if (err != null) {
@ -330,9 +331,9 @@ export class OsmConnection {
method,
headers: header,
content,
path: `/api/0.6/${path}`,
path: `/api/0.6/${path}`
},
function (err, response) {
function(err, response) {
if (err !== null) {
error(err)
} else {
@ -412,7 +413,7 @@ export class OsmConnection {
"notes.json",
content,
{
"Content-Type": "application/x-www-form-urlencoded; charset=UTF-8",
"Content-Type": "application/x-www-form-urlencoded; charset=UTF-8"
},
true
)
@ -423,6 +424,12 @@ export class OsmConnection {
return id
}
public async getNote(id: number): Promise<Feature<Point>> {
return JSON.parse(await this.get(
"notes/" + id + ".json"
))
}
public static GpxTrackVisibility = ["private", "public", "trackable", "identifiable"] as const
public async uploadGpxTrack(
@ -453,7 +460,7 @@ export class OsmConnection {
file: gpx,
description: options.description,
tags: options.labels?.join(",") ?? "",
visibility: options.visibility,
visibility: options.visibility
}
if (!contents.description) {
@ -461,9 +468,9 @@ export class OsmConnection {
}
const extras = {
file:
'; filename="' +
"; filename=\"" +
(options.filename ?? "gpx_track_mapcomplete_" + new Date().toISOString()) +
'"\r\nContent-Type: application/gpx+xml',
"\"\r\nContent-Type: application/gpx+xml"
}
const boundary = "987654"
@ -471,7 +478,7 @@ export class OsmConnection {
let body = ""
for (const key in contents) {
body += "--" + boundary + "\r\n"
body += 'Content-Disposition: form-data; name="' + key + '"'
body += "Content-Disposition: form-data; name=\"" + key + "\""
if (extras[key] !== undefined) {
body += extras[key]
}
@ -482,7 +489,7 @@ export class OsmConnection {
const response = await this.post("gpx/create", body, {
"Content-Type": "multipart/form-data; boundary=" + boundary,
"Content-Length": "" + body.length,
"Content-Length": "" + body.length
})
const parsed = JSON.parse(response)
console.log("Uploaded GPX track", parsed)
@ -503,9 +510,9 @@ export class OsmConnection {
{
method: "POST",
path: `/api/0.6/notes/${id}/comment?text=${encodeURIComponent(text)}`,
path: `/api/0.6/notes/${id}/comment?text=${encodeURIComponent(text)}`
},
function (err) {
function(err) {
if (err !== null) {
error(err)
} else {
@ -520,7 +527,7 @@ export class OsmConnection {
* To be called by land.html
*/
public finishLogin(callback: (previousURL: string) => void) {
this.auth.authenticate(function () {
this.auth.authenticate(function() {
// Fully authed at this point
console.log("Authentication successful!")
const previousLocation = LocalStorageSource.get("location_before_login")
@ -541,7 +548,7 @@ export class OsmConnection {
*/
singlepage: !this._iframeMode,
auto: true,
apiUrl: this._oauth_config.api_url ?? this._oauth_config.url,
apiUrl: this._oauth_config.api_url ?? this._oauth_config.url
})
}