Chore: Housekeeping

This commit is contained in:
Pieter Vander Vennet 2023-12-21 01:46:18 +01:00
parent ef0ba091eb
commit 319c0e2573
77 changed files with 2485 additions and 1727 deletions

View file

@ -17,18 +17,16 @@
let featureBearing: number = 45
if (feature?.geometry?.type === "LineString") {
/* Bearing between -180 and + 180, positive is clockwise*/
featureBearing = Math.round(GeoOperations.bearing(
feature.geometry.coordinates[0],
feature.geometry.coordinates.at(-1),
))
featureBearing = Math.round(
GeoOperations.bearing(feature.geometry.coordinates[0], feature.geometry.coordinates.at(-1))
)
}
let previewDegrees: UIEventSource<string> = new UIEventSource<string>(undefined)
let previewPercentage: UIEventSource<string> = new UIEventSource<string>(undefined)
function degreesToPercentage(beta: number): string {
const perc = Math.tan(beta * Math.PI / 180) * 100
const perc = Math.tan((beta * Math.PI) / 180) * 100
const rounded = Math.round(perc / 2.5) * 2.5
return rounded + "%"
}
@ -40,7 +38,7 @@
let gotMeasurement = orientation.gotMeasurement
let valuesign = alpha.map(phoneBearing => {
let valuesign = alpha.map((phoneBearing) => {
if (featureBearing === undefined) {
return 1
}
@ -56,31 +54,30 @@
} else {
return -1
}
})
beta.map(beta => {
// As one moves forward on a way, a positive incline gets higher, and a negative incline gets lower.
let valueSign = valuesign.data
beta.map(
(beta) => {
// As one moves forward on a way, a positive incline gets higher, and a negative incline gets lower.
let valueSign = valuesign.data
if (mode === "degrees") {
value.setData(valueSign * beta + "°")
} else {
value.setData(degreesToPercentage(valueSign * beta))
}
previewDegrees.setData(beta + "°")
previewPercentage.setData(degreesToPercentage(beta))
}, [valuesign, beta])
if (mode === "degrees") {
value.setData(valueSign * beta + "°")
} else {
value.setData(degreesToPercentage(valueSign * beta))
}
previewDegrees.setData(beta + "°")
previewPercentage.setData(degreesToPercentage(beta))
},
[valuesign, beta]
)
</script>
{#if $gotMeasurement}
<div class="flex flex-col m-2">
<div class="flex w-full">
<div class="font-bold w-full flex justify-around items-center text-5xl">
{#if $gotMeasurement}
<div class="m-2 flex flex-col">
<div class="flex w-full">
<div class="flex w-full items-center justify-around text-5xl font-bold">
<div>
{$previewDegrees}
</div>
@ -88,7 +85,6 @@
{$previewPercentage}
</div>
</div>
</div>
<div>
@ -96,14 +92,14 @@
</div>
<If condition={state?.featureSwitchIsTesting ?? new ImmutableStore(true)}>
<span class="subtle">
Way: {featureBearing}°, compass: {$alpha}°, diff: {(featureBearing - $alpha)}
{#if $valuesign === 1}
Forward
{:else}
Backward
{/if}
</span>
<span class="subtle">
Way: {featureBearing}°, compass: {$alpha}°, diff: {featureBearing - $alpha}
{#if $valuesign === 1}
Forward
{:else}
Backward
{/if}
</span>
</If>
</div>
{/if}

View file

@ -39,8 +39,8 @@
{#if availableLanguages?.length > 1}
<form class={twMerge("flex max-w-full items-center pr-4", clss)}>
<label class="flex neutral-label" use:ariaLabel={Translations.t.general.pickLanguage}>
<LanguageIcon class="h-4 w-4 mr-1 shrink-0" aria-hidden="true" />
<label class="neutral-label flex" use:ariaLabel={Translations.t.general.pickLanguage}>
<LanguageIcon class="mr-1 h-4 w-4 shrink-0" aria-hidden="true" />
<Dropdown cls="max-w-full" value={assignTo}>
{#if preferredFiltered}
{#each preferredFiltered as language}
@ -54,12 +54,12 @@
<option disabled />
{/if}
{#each availableLanguages.filter(l => l !== "_context") as language}
{#each availableLanguages.filter((l) => l !== "_context") as language}
<option value={language} class="font-bold">
{native[language] ?? ""}
{#if language !== $current}
{#if language_translations[language]?.[$current] !== undefined}
({ language_translations[language]?.[$current] + " - " + language ?? language})
({language_translations[language]?.[$current] + " - " + language ?? language})
{:else}
({language})
{/if}

View file

@ -1,102 +1,102 @@
<script lang="ts">
import { UIEventSource } from "../../Logic/UIEventSource";
import type { ValidatorType } from "./Validators";
import Validators from "./Validators";
import { ExclamationIcon } from "@rgossiaux/svelte-heroicons/solid";
import { Translation } from "../i18n/Translation";
import { createEventDispatcher, onDestroy } from "svelte";
import { Validator } from "./Validator";
import { Unit } from "../../Models/Unit";
import UnitInput from "../Popup/UnitInput.svelte";
import { Utils } from "../../Utils";
import { twMerge } from "tailwind-merge";
import { UIEventSource } from "../../Logic/UIEventSource"
import type { ValidatorType } from "./Validators"
import Validators from "./Validators"
import { ExclamationIcon } from "@rgossiaux/svelte-heroicons/solid"
import { Translation } from "../i18n/Translation"
import { createEventDispatcher, onDestroy } from "svelte"
import { Validator } from "./Validator"
import { Unit } from "../../Models/Unit"
import UnitInput from "../Popup/UnitInput.svelte"
import { Utils } from "../../Utils"
import { twMerge } from "tailwind-merge"
export let type: ValidatorType;
export let feedback: UIEventSource<Translation> | undefined = undefined;
export let cls: string = undefined;
export let getCountry: () => string | undefined;
export let placeholder: string | Translation | undefined;
export let unit: Unit = undefined;
export let type: ValidatorType
export let feedback: UIEventSource<Translation> | undefined = undefined
export let cls: string = undefined
export let getCountry: () => string | undefined
export let placeholder: string | Translation | undefined
export let unit: Unit = undefined
/**
* Valid state, exported to the calling component
*/
export let value: UIEventSource<string | undefined>;
export let value: UIEventSource<string | undefined>
/**
* Internal state bound to the input element.
*
* This is only copied to 'value' when appropriate so that no invalid values leak outside;
* Additionally, the unit is added when copying
*/
let _value = new UIEventSource(value.data ?? "");
let _value = new UIEventSource(value.data ?? "")
let validator: Validator = Validators.get(type ?? "string");
let validator: Validator = Validators.get(type ?? "string")
if (validator === undefined) {
console.warn("Didn't find a validator for type", type);
console.warn("Didn't find a validator for type", type)
}
let selectedUnit: UIEventSource<string> = new UIEventSource<string>(undefined);
let _placeholder = placeholder ?? validator?.getPlaceholder() ?? type;
let selectedUnit: UIEventSource<string> = new UIEventSource<string>(undefined)
let _placeholder = placeholder ?? validator?.getPlaceholder() ?? type
function initValueAndDenom() {
if (unit && value.data) {
const [v, denom] = unit?.findDenomination(value.data, getCountry);
const [v, denom] = unit?.findDenomination(value.data, getCountry)
if (denom) {
_value.setData(v);
selectedUnit.setData(denom.canonical);
_value.setData(v)
selectedUnit.setData(denom.canonical)
} else {
_value.setData(value.data ?? "");
_value.setData(value.data ?? "")
}
} else {
_value.setData(value.data ?? "");
_value.setData(value.data ?? "")
}
}
initValueAndDenom();
initValueAndDenom()
$: {
// The type changed -> reset some values
validator = Validators.get(type ?? "string");
validator = Validators.get(type ?? "string")
_placeholder = placeholder ?? validator?.getPlaceholder() ?? type;
feedback?.setData(validator?.getFeedback(_value.data, getCountry));
_placeholder = placeholder ?? validator?.getPlaceholder() ?? type
feedback?.setData(validator?.getFeedback(_value.data, getCountry))
initValueAndDenom();
initValueAndDenom()
}
function setValues() {
// Update the value stores
const v = _value.data;
const v = _value.data
if (!validator?.isValid(v, getCountry) || v === "") {
feedback?.setData(validator?.getFeedback(v, getCountry));
value.setData("");
return;
feedback?.setData(validator?.getFeedback(v, getCountry))
value.setData("")
return
}
if (unit !== undefined && isNaN(Number(v))) {
value.setData(undefined);
return;
value.setData(undefined)
return
}
feedback?.setData(undefined);
feedback?.setData(undefined)
if (selectedUnit.data) {
value.setData(unit.toOsm(v, selectedUnit.data))
} else {
value.setData(v);
value.setData(v)
}
}
onDestroy(_value.addCallbackAndRun((_) => setValues()));
onDestroy(_value.addCallbackAndRun((_) => setValues()))
if (unit === undefined) {
onDestroy(
value.addCallbackAndRunD((fromUpstream) => {
if (_value.data !== fromUpstream && fromUpstream !== "") {
_value.setData(fromUpstream);
_value.setData(fromUpstream)
}
})
);
}else{
// Handled by the UnitInput
)
} else {
// Handled by the UnitInput
}
onDestroy(selectedUnit.addCallback((_) => setValues()));
onDestroy(selectedUnit.addCallback((_) => setValues()))
if (validator === undefined) {
throw (
"Not a valid type (no validator found) for type '" +
@ -109,17 +109,17 @@
)
.slice(0, 5)
.join(", ")
);
)
}
const isValid = _value.map((v) => validator?.isValid(v, getCountry) ?? true);
const isValid = _value.map((v) => validator?.isValid(v, getCountry) ?? true)
let htmlElem: HTMLInputElement;
let htmlElem: HTMLInputElement
let dispatch = createEventDispatcher<{ selected; submit }>();
let dispatch = createEventDispatcher<{ selected; submit }>()
$: {
if (htmlElem !== undefined) {
htmlElem.onfocus = () => dispatch("selected");
htmlElem.onfocus = () => dispatch("selected")
}
}
@ -128,9 +128,9 @@
*/
function sendSubmit() {
if (feedback?.data) {
console.log("Not sending a submit as there is feedback");
console.log("Not sending a submit as there is feedback")
}
dispatch("submit");
dispatch("submit")
}
</script>