Improving TrkSegViewer

This commit is contained in:
Eric van der Vlist 2023-01-07 18:46:14 +01:00
parent 7483d9f904
commit 1f2979b28b
6 changed files with 51 additions and 14 deletions

View File

@ -8,7 +8,7 @@ import { useI18n } from '@solid-primitives/i18n';
import dispatch from '../../workers/dispatcher-main'; import dispatch from '../../workers/dispatcher-main';
interface Props { interface Props {
target: any; target: () => any;
putAction: string; putAction: string;
putParamName: string; putParamName: string;
} }
@ -20,14 +20,14 @@ const DisplayOrGetAddress: Component<Props> = ({
}) => { }) => {
const [t, { add, locale, dict }] = useI18n(); const [t, { add, locale, dict }] = useI18n();
const findClickHandlerFactory = (target: any) => async () => { const findClickHandler = async () => {
const address = await findAddress(target.$.lon, target.$.lat, locale); const address = await findAddress(target().$.lon, target().$.lat, locale);
const updatedTarget = cloneDeep(target); const updatedTarget = cloneDeep(target());
if (updatedTarget.extensions === undefined) { if (updatedTarget.extensions === undefined) {
updatedTarget.extensions = {}; updatedTarget.extensions = {};
} }
updatedTarget.extensions.address = address; updatedTarget.extensions.address = address;
const params: any = { id: target.id }; const params: any = { id: target().id };
params[putParamName] = updatedTarget; params[putParamName] = updatedTarget;
dispatch({ dispatch({
action: putAction, action: putAction,
@ -37,14 +37,14 @@ const DisplayOrGetAddress: Component<Props> = ({
return ( return (
<Show <Show
when={target.extensions?.address} when={target().extensions?.address}
fallback={ fallback={
<IconButton color='primary' onClick={findClickHandlerFactory(target)}> <IconButton color='primary' onClick={findClickHandler}>
<SearchIcon /> <SearchIcon />
</IconButton> </IconButton>
} }
> >
<>{getVillageOrTown(target.extensions?.address)}</> <>{getVillageOrTown(target().extensions?.address)}</>
</Show> </Show>
); );
}; };

View File

@ -4,7 +4,6 @@ import RteIcon from '../../icons/directions-svgrepo-com.svg?component-solid';
import { useI18n } from '@solid-primitives/i18n'; import { useI18n } from '@solid-primitives/i18n';
import Tree from '../tree'; import Tree from '../tree';
import { getLength } from 'ol/sphere';
import { LineString } from 'ol/geom'; import { LineString } from 'ol/geom';
import DisplayOrGetAddress from '../display-or-get-address'; import DisplayOrGetAddress from '../display-or-get-address';
import { getFormatedLength } from '../../lib/ol'; import { getFormatedLength } from '../../lib/ol';
@ -16,7 +15,7 @@ interface Props {
const RteViewer: Component<Props> = ({ rteId }) => { const RteViewer: Component<Props> = ({ rteId }) => {
const [t, { add, locale, dict }] = useI18n(); const [t, { add, locale, dict }] = useI18n();
const rte = peekCachedSignal({ id: rteId, method: 'getRte' }); const rte = peekCachedSignal({ id: rteId, action: 'getRte' });
console.log({ caller: 'RteViewer', rteId, rte: rte() }); console.log({ caller: 'RteViewer', rteId, rte: rte() });
const title = () => { const title = () => {
return rte().name; return rte().name;
@ -38,13 +37,13 @@ const RteViewer: Component<Props> = ({ rteId }) => {
<div> <div>
{getFormatedLength(lineString)} {t('from')}{' '} {getFormatedLength(lineString)} {t('from')}{' '}
<DisplayOrGetAddress <DisplayOrGetAddress
target={rte().rtept.at(0)} target={() => rte().rtept.at(0)}
putAction='putRtept' putAction='putRtept'
putParamName='rtept' putParamName='rtept'
/>{' '} />{' '}
{t('to')}{' '} {t('to')}{' '}
<DisplayOrGetAddress <DisplayOrGetAddress
target={rte().rtept.at(-1)} target={() => rte().rtept.at(-1)}
putAction='putRtept' putAction='putRtept'
putParamName='rtept' putParamName='rtept'
/> />

View File

@ -2,18 +2,28 @@ import { Component } from 'solid-js';
import { peekCachedSignal } from '../../workers/cached-signals'; import { peekCachedSignal } from '../../workers/cached-signals';
import Tree from '../tree'; import Tree from '../tree';
import TrkIcon from '../../icons/human-footprints-svgrepo-com.svg?component-solid'; import TrkIcon from '../../icons/human-footprints-svgrepo-com.svg?component-solid';
import { useI18n } from '@solid-primitives/i18n';
import { getFormatedLength } from '../../lib/ol';
import DisplayOrGetAddress from '../display-or-get-address';
import { LineString } from 'ol/geom';
interface Props { interface Props {
trksegId: string; trksegId: string;
} }
const TrksegViewer: Component<Props> = ({ trksegId }) => { const TrksegViewer: Component<Props> = ({ trksegId }) => {
const trkseg = peekCachedSignal({ id: trksegId, method: 'getTrkseg' }); const [t, { add, locale, dict }] = useI18n();
const trkseg = peekCachedSignal({ id: trksegId, action: 'getTrkseg' });
console.log({ caller: 'TrksegViewer', trksegId, trkseg: trkseg() }); console.log({ caller: 'TrksegViewer', trksegId, trkseg: trkseg() });
const title = () => { const title = () => {
return trkseg().name; return trkseg().name;
}; };
const lineString = new LineString(
trkseg().trkpt.map((trkpt: any) => [trkpt.$.lon, trkpt.$.lat])
);
return ( return (
<Tree <Tree
title={ title={
@ -21,7 +31,24 @@ const TrksegViewer: Component<Props> = ({ trksegId }) => {
<TrkIcon fill='grey' width='16' height='16' /> {title()} <TrkIcon fill='grey' width='16' height='16' /> {title()}
</> </>
} }
content={undefined} content={
<>
<div>
{getFormatedLength(lineString)} {t('from')}{' '}
<DisplayOrGetAddress
target={() => trkseg().trkpt.at(0)}
putAction='putTrkpt'
putParamName='trkpt'
/>{' '}
{t('to')}{' '}
<DisplayOrGetAddress
target={() => trkseg().trkpt.at(-1)}
putAction='putTrkpt'
putParamName='trkpt'
/>
</div>
</>
}
subTree={undefined} subTree={undefined}
/> />
); );

View File

@ -48,3 +48,11 @@ export const putNewTrkpt = async (id?: IdTrk | IdGpx | IdTrkseg | IdTrkpt) => {
); );
return finalId; return finalId;
}; };
export const putTrkpt = async (params: any) => {
const { id, trkpt } = params;
trkpt.id = undefined;
await put(id, 'trkpt', (doc) => trkpt, trkpt);
return trkpt;
};

View File

@ -41,6 +41,7 @@ export const getTrkseg = async (params: any) => {
//level 1 //level 1
if (row.doc.type === 'trkpt') { if (row.doc.type === 'trkpt') {
target.splice(1); target.splice(1);
row.doc.doc.id = row.doc._id;
appendToArray(target.at(-1), row.doc.type, row.doc.doc); appendToArray(target.at(-1), row.doc.type, row.doc.doc);
target.push(row.doc.doc); target.push(row.doc.doc);
} }

View File

@ -17,6 +17,7 @@ import { putRtept } from '../db/rtept';
import { getSettings, putSettings } from '../db/settings'; import { getSettings, putSettings } from '../db/settings';
import { getState, setState } from '../db/state'; import { getState, setState } from '../db/state';
import { getTrk, putNewTrk } from '../db/trk'; import { getTrk, putNewTrk } from '../db/trk';
import { putTrkpt } from '../db/trkpt';
import { getTrkseg, appendTrkpt } from '../db/trkseg'; import { getTrkseg, appendTrkpt } from '../db/trkseg';
import { getWpt, putWpt } from '../db/wpt'; import { getWpt, putWpt } from '../db/wpt';
import { until } from '../lib/async-wait'; import { until } from '../lib/async-wait';
@ -51,6 +52,7 @@ onmessage = async function (e) {
putWpt, putWpt,
putRte, putRte,
putRtept, putRtept,
putTrkpt,
getState, getState,
setState, setState,