diff options
author | Hsieh Chin Fan <pham@topo.tw> | 2024-10-31 17:11:55 +0800 |
---|---|---|
committer | Hsieh Chin Fan <pham@topo.tw> | 2024-11-03 14:01:05 +0800 |
commit | 1fc35b33eacf0f06370fc14798f65f5ec0972195 (patch) | |
tree | c745d7c1691d4baa92d208bcee26abacb5485d0c /src/MenuItem.mjs | |
parent | 11ff731337c6823e74821d06457972c6d0528c34 (diff) |
feat: patch 7ee1ad6
* add spinning circle for Networking UX
* display marker for each results
Diffstat (limited to 'src/MenuItem.mjs')
-rw-r--r-- | src/MenuItem.mjs | 75 |
1 files changed, 58 insertions, 17 deletions
diff --git a/src/MenuItem.mjs b/src/MenuItem.mjs index 84a1405..464dd2d 100644 --- a/src/MenuItem.mjs +++ b/src/MenuItem.mjs | |||
@@ -1,7 +1,7 @@ | |||
1 | import { shiftByWindow } from './utils.mjs' | 1 | import { shiftByWindow } from './utils.mjs' |
2 | import { addMarkerByPoint } from './dumbyUtils.mjs' | 2 | import { addMarkerByPoint } from './dumbyUtils.mjs' |
3 | /* eslint-disable-next-line no-unused-vars */ | 3 | /* eslint-disable-next-line no-unused-vars */ |
4 | import { GeoLink, getMarkersFromMaps, removeLeaderLines } from './Link.mjs' | 4 | import { GeoLink, getMarkersFromMaps, getMarkersByGeoLink, removeLeaderLines, updateMapCameraByMarker } from './Link.mjs' |
5 | import * as markers from './marker.mjs' | 5 | import * as markers from './marker.mjs' |
6 | import { parseConfigsFromYaml } from 'mapclay' | 6 | import { parseConfigsFromYaml } from 'mapclay' |
7 | 7 | ||
@@ -587,25 +587,66 @@ export const editMap = (map, dumbymap) => { | |||
587 | }) | 587 | }) |
588 | } | 588 | } |
589 | 589 | ||
590 | export const addLinkbyNominatim = (range) => { | 590 | /** |
591 | * addLinkbyGeocoding. | ||
592 | * | ||
593 | * @param {Range} range | ||
594 | */ | ||
595 | export const addLinkbyGeocoding = (range) => { | ||
591 | return Item({ | 596 | return Item({ |
592 | text: 'Add Link by Geocoding', | 597 | text: 'Add Link by Geocoding', |
598 | className: ['keep-menu'], | ||
599 | onclick: async (e) => { | ||
600 | /** Add spinning circle for Network */ | ||
601 | e.target.classList.add('with-spinning-circle') | ||
602 | const menu = e.target.closest('.dumby-menu') | ||
603 | |||
604 | if (!menu) return | ||
605 | /** Geocoding by Nominatim */ | ||
606 | // TODO Add more params like limit: | ||
607 | // https://nominatim.org/release-docs/latest/api/Search/ | ||
608 | const query = range.toString() | ||
609 | const response = await fetch(`https://nominatim.openstreetmap.org/search?q=${query.toString()}&format=json`) | ||
610 | const places = await response.json() | ||
611 | menu.replaceChildren() | ||
612 | |||
613 | // Show Message if no result found | ||
614 | if (places.length === 0) { | ||
615 | menu.appendChild(Item({ text: 'No Result Found' })) | ||
616 | return | ||
617 | } | ||
618 | |||
619 | // Add items for each results | ||
620 | const items = places.map(geocodingResult((a) => { | ||
621 | a.className = 'not-geolink from-geocoding' | ||
622 | a.textContent = query | ||
623 | range.deleteContents() | ||
624 | range.insertNode(a) | ||
625 | })) | ||
626 | menu.replaceChildren(...items) | ||
627 | }, | ||
628 | }) | ||
629 | } | ||
630 | |||
631 | export const geocodingResult = (callback) => (result) => { | ||
632 | const item = Item({ | ||
633 | text: result.display_name, | ||
593 | onclick: () => { | 634 | onclick: () => { |
594 | const place = range.toString() | 635 | const a = document.createElement('a') |
595 | fetch(`https://nominatim.openstreetmap.org/search?q=${place.toString()}&format=json`) | 636 | a.href = `geo:${result.lat},${result.lon}?name=${result.name}&osm=${result.osm_type}/${result.osm_id}` |
596 | .then(res => res.json()) | 637 | a.title = result.display_name |
597 | .then(places => { | 638 | callback(a) |
598 | if (places.length === 0) return | ||
599 | console.log('nomiatim', places) | ||
600 | range.deleteContents() | ||
601 | places.forEach(p => { | ||
602 | const a = document.createElement('a') | ||
603 | a.className = 'not-geolink from-geocoding' | ||
604 | a.href = `geo:${p.lat},${p.lon}?name=${p.name}&osm=${p.osm_type}/${p.osm_id}` | ||
605 | a.textContent = place | ||
606 | range.insertNode(a) | ||
607 | }) | ||
608 | }) | ||
609 | }, | 639 | }, |
610 | }) | 640 | }) |
641 | item.onmouseover = () => { | ||
642 | const markers = getMarkersFromMaps( | ||
643 | [result.lon, result.lat], | ||
644 | { type: 'circle', title: result.display_name }, | ||
645 | ) | ||
646 | markers.forEach(updateMapCameraByMarker([result.lon, result.lat])) | ||
647 | item.onmouseout = () => { | ||
648 | markers.forEach(m => m.remove()) | ||
649 | } | ||
650 | } | ||
651 | return item | ||
611 | } | 652 | } |