forked from MapComplete/MapComplete
Feature: allow to erase freeform values if there are no mappings
This commit is contained in:
parent
ef34304efa
commit
f7b555b9b6
5 changed files with 192 additions and 158 deletions
|
@ -217,6 +217,7 @@
|
||||||
},
|
},
|
||||||
"enableGeolocationForSafari": "Did you not get the popup to ask for geopermission?",
|
"enableGeolocationForSafari": "Did you not get the popup to ask for geopermission?",
|
||||||
"enableGeolocationForSafariLink": "Learn how to enable the geolocation permission in settings",
|
"enableGeolocationForSafariLink": "Learn how to enable the geolocation permission in settings",
|
||||||
|
"eraseValue": "Erase this value",
|
||||||
"error": "Something went wrong",
|
"error": "Something went wrong",
|
||||||
"example": "Example",
|
"example": "Example",
|
||||||
"examples": "Examples",
|
"examples": "Examples",
|
||||||
|
|
|
@ -28,8 +28,12 @@
|
||||||
* This is only copied to 'value' when appropriate so that no invalid values leak outside;
|
* This is only copied to 'value' when appropriate so that no invalid values leak outside;
|
||||||
* Additionally, the unit is added when copying
|
* Additionally, the unit is added when copying
|
||||||
*/
|
*/
|
||||||
let _value = new UIEventSource(value.data ?? "")
|
export let unvalidatedText = new UIEventSource(value.data ?? "")
|
||||||
|
|
||||||
|
|
||||||
|
if(unvalidatedText == /*Compare by reference!*/ value){
|
||||||
|
throw "Value and unvalidatedText may not be the same store!"
|
||||||
|
}
|
||||||
let validator: Validator = Validators.get(type ?? "string")
|
let validator: Validator = Validators.get(type ?? "string")
|
||||||
if (validator === undefined) {
|
if (validator === undefined) {
|
||||||
console.warn("Didn't find a validator for type", type)
|
console.warn("Didn't find a validator for type", type)
|
||||||
|
@ -41,13 +45,13 @@
|
||||||
if (unit && value.data) {
|
if (unit && value.data) {
|
||||||
const [v, denom] = unit?.findDenomination(value.data, getCountry)
|
const [v, denom] = unit?.findDenomination(value.data, getCountry)
|
||||||
if (denom) {
|
if (denom) {
|
||||||
_value.setData(v)
|
unvalidatedText.setData(v)
|
||||||
selectedUnit.setData(denom.canonical)
|
selectedUnit.setData(denom.canonical)
|
||||||
} else {
|
} else {
|
||||||
_value.setData(value.data ?? "")
|
unvalidatedText.setData(value.data ?? "")
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
_value.setData(value.data ?? "")
|
unvalidatedText.setData(value.data ?? "")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -67,8 +71,8 @@
|
||||||
validator = Validators.get(type ?? "string")
|
validator = Validators.get(type ?? "string")
|
||||||
|
|
||||||
_placeholder = placeholder ?? validator?.getPlaceholder() ?? type
|
_placeholder = placeholder ?? validator?.getPlaceholder() ?? type
|
||||||
if (_value.data?.length > 0) {
|
if (unvalidatedText.data?.length > 0) {
|
||||||
feedback?.setData(validator?.getFeedback(_value.data, getCountry))
|
feedback?.setData(validator?.getFeedback(unvalidatedText.data, getCountry))
|
||||||
} else {
|
} else {
|
||||||
feedback?.setData(undefined)
|
feedback?.setData(undefined)
|
||||||
}
|
}
|
||||||
|
@ -78,7 +82,7 @@
|
||||||
|
|
||||||
function setValues() {
|
function setValues() {
|
||||||
// Update the value stores
|
// Update the value stores
|
||||||
const v = _value.data
|
const v = unvalidatedText.data
|
||||||
if (v === "") {
|
if (v === "") {
|
||||||
value.setData(undefined)
|
value.setData(undefined)
|
||||||
feedback?.setData(undefined)
|
feedback?.setData(undefined)
|
||||||
|
@ -103,12 +107,12 @@
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
onDestroy(_value.addCallbackAndRun((_) => setValues()))
|
onDestroy(unvalidatedText.addCallbackAndRun((_) => setValues()))
|
||||||
if (unit === undefined) {
|
if (unit === undefined) {
|
||||||
onDestroy(
|
onDestroy(
|
||||||
value.addCallbackAndRunD((fromUpstream) => {
|
value.addCallbackAndRunD((fromUpstream) => {
|
||||||
if (_value.data !== fromUpstream && fromUpstream !== "") {
|
if (unvalidatedText.data !== fromUpstream && fromUpstream !== "") {
|
||||||
_value.setData(fromUpstream)
|
unvalidatedText.setData(fromUpstream)
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
)
|
)
|
||||||
|
@ -131,7 +135,7 @@
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
const isValid = _value.map((v) => validator?.isValid(v, getCountry) ?? true)
|
const isValid = unvalidatedText.map((v) => validator?.isValid(v, getCountry) ?? true)
|
||||||
|
|
||||||
let htmlElem: HTMLInputElement | HTMLTextAreaElement
|
let htmlElem: HTMLInputElement | HTMLTextAreaElement
|
||||||
|
|
||||||
|
@ -149,7 +153,7 @@
|
||||||
{#if validator?.textArea}
|
{#if validator?.textArea}
|
||||||
<textarea
|
<textarea
|
||||||
class="w-full"
|
class="w-full"
|
||||||
bind:value={$_value}
|
bind:value={$unvalidatedText}
|
||||||
inputmode={validator?.inputmode ?? "text"}
|
inputmode={validator?.inputmode ?? "text"}
|
||||||
placeholder={_placeholder}
|
placeholder={_placeholder}
|
||||||
bind:this={htmlElem}
|
bind:this={htmlElem}
|
||||||
|
@ -159,7 +163,7 @@
|
||||||
<div class={twMerge("inline-flex", cls)}>
|
<div class={twMerge("inline-flex", cls)}>
|
||||||
<input
|
<input
|
||||||
bind:this={htmlElem}
|
bind:this={htmlElem}
|
||||||
bind:value={$_value}
|
bind:value={$unvalidatedText}
|
||||||
class="w-full"
|
class="w-full"
|
||||||
inputmode={validator?.inputmode ?? "text"}
|
inputmode={validator?.inputmode ?? "text"}
|
||||||
placeholder={_placeholder}
|
placeholder={_placeholder}
|
||||||
|
@ -170,7 +174,7 @@
|
||||||
{/if}
|
{/if}
|
||||||
|
|
||||||
{#if unit !== undefined}
|
{#if unit !== undefined}
|
||||||
<UnitInput {unit} {selectedUnit} textValue={_value} upstreamValue={value} {getCountry} />
|
<UnitInput {unit} {selectedUnit} textValue={unvalidatedText} upstreamValue={value} {getCountry} />
|
||||||
{/if}
|
{/if}
|
||||||
</div>
|
</div>
|
||||||
{/if}
|
{/if}
|
||||||
|
|
|
@ -12,6 +12,7 @@
|
||||||
import type { SpecialVisualizationState } from "../../SpecialVisualization"
|
import type { SpecialVisualizationState } from "../../SpecialVisualization"
|
||||||
|
|
||||||
export let value: UIEventSource<string>
|
export let value: UIEventSource<string>
|
||||||
|
export let unvalidatedText: UIEventSource<string> = new UIEventSource<string>(value.data)
|
||||||
export let config: TagRenderingConfig
|
export let config: TagRenderingConfig
|
||||||
export let tags: UIEventSource<Record<string, string>>
|
export let tags: UIEventSource<Record<string, string>>
|
||||||
|
|
||||||
|
@ -64,6 +65,7 @@
|
||||||
type={config.freeform.type}
|
type={config.freeform.type}
|
||||||
{placeholder}
|
{placeholder}
|
||||||
{value}
|
{value}
|
||||||
|
{unvalidatedText}
|
||||||
/>
|
/>
|
||||||
{/if}
|
{/if}
|
||||||
|
|
||||||
|
@ -74,5 +76,6 @@
|
||||||
{value}
|
{value}
|
||||||
{state}
|
{state}
|
||||||
on:submit
|
on:submit
|
||||||
|
{unvalidatedText}
|
||||||
/>
|
/>
|
||||||
</div>
|
</div>
|
||||||
|
|
|
@ -87,6 +87,7 @@
|
||||||
{state}
|
{state}
|
||||||
{layer}
|
{layer}
|
||||||
on:saved={() => (editMode = false)}
|
on:saved={() => (editMode = false)}
|
||||||
|
allowDeleteOfFreeform={true}
|
||||||
>
|
>
|
||||||
<button
|
<button
|
||||||
slot="cancel"
|
slot="cancel"
|
||||||
|
|
|
@ -22,17 +22,23 @@
|
||||||
import { Unit } from "../../../Models/Unit"
|
import { Unit } from "../../../Models/Unit"
|
||||||
import UserRelatedState from "../../../Logic/State/UserRelatedState"
|
import UserRelatedState from "../../../Logic/State/UserRelatedState"
|
||||||
import { twJoin } from "tailwind-merge"
|
import { twJoin } from "tailwind-merge"
|
||||||
|
import type { UploadableTag } from "../../../Logic/Tags/TagUtils"
|
||||||
import { TagUtils } from "../../../Logic/Tags/TagUtils"
|
import { TagUtils } from "../../../Logic/Tags/TagUtils"
|
||||||
|
|
||||||
import Search from "../../../assets/svg/Search.svelte"
|
import Search from "../../../assets/svg/Search.svelte"
|
||||||
import Login from "../../../assets/svg/Login.svelte"
|
import Login from "../../../assets/svg/Login.svelte"
|
||||||
import { placeholder } from "../../../Utils/placeholder"
|
import { placeholder } from "../../../Utils/placeholder"
|
||||||
|
import { TrashIcon } from "@rgossiaux/svelte-heroicons/solid"
|
||||||
|
import { Tag } from "../../../Logic/Tags/Tag"
|
||||||
|
|
||||||
export let config: TagRenderingConfig
|
export let config: TagRenderingConfig
|
||||||
export let tags: UIEventSource<Record<string, string>>
|
export let tags: UIEventSource<Record<string, string>>
|
||||||
export let selectedElement: Feature
|
export let selectedElement: Feature
|
||||||
export let state: SpecialVisualizationState
|
export let state: SpecialVisualizationState
|
||||||
export let layer: LayerConfig | undefined
|
export let layer: LayerConfig | undefined
|
||||||
export let selectedTags: TagsFilter = undefined
|
export let selectedTags: UploadableTag = undefined
|
||||||
|
|
||||||
|
export let allowDeleteOfFreeform: boolean = false
|
||||||
|
|
||||||
let feedback: UIEventSource<Translation> = new UIEventSource<Translation>(undefined)
|
let feedback: UIEventSource<Translation> = new UIEventSource<Translation>(undefined)
|
||||||
|
|
||||||
|
@ -40,6 +46,8 @@
|
||||||
|
|
||||||
// Will be bound if a freeform is available
|
// Will be bound if a freeform is available
|
||||||
let freeformInput = new UIEventSource<string>(tags?.[config.freeform?.key])
|
let freeformInput = new UIEventSource<string>(tags?.[config.freeform?.key])
|
||||||
|
let freeformInputUnvalidated = new UIEventSource<string>(freeformInput.data)
|
||||||
|
|
||||||
let selectedMapping: number = undefined
|
let selectedMapping: number = undefined
|
||||||
/**
|
/**
|
||||||
* A list of booleans, used if multiAnswer is set
|
* A list of booleans, used if multiAnswer is set
|
||||||
|
@ -145,6 +153,10 @@
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
$: {
|
$: {
|
||||||
|
if (allowDeleteOfFreeform && $freeformInput === undefined && $freeformInputUnvalidated === "" && mappings.length === 0) {
|
||||||
|
selectedTags = new Tag(config.freeform.key, "")
|
||||||
|
} else {
|
||||||
|
|
||||||
try {
|
try {
|
||||||
selectedTags = config?.constructChangeSpecification(
|
selectedTags = config?.constructChangeSpecification(
|
||||||
$freeformInput,
|
$freeformInput,
|
||||||
|
@ -157,8 +169,11 @@
|
||||||
selectedTags = undefined
|
selectedTags = undefined
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function onSave(e = undefined, deleteFreeform = false) {
|
||||||
|
console.log("On click", deleteFreeform, ">>>", selectedTags)
|
||||||
|
|
||||||
function onSave(e) {
|
|
||||||
if (selectedTags === undefined) {
|
if (selectedTags === undefined) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
@ -199,7 +214,7 @@
|
||||||
if (e.key === "Enter") {
|
if (e.key === "Enter") {
|
||||||
e.preventDefault()
|
e.preventDefault()
|
||||||
e.stopPropagation()
|
e.stopPropagation()
|
||||||
onSave(e)
|
onSave()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -269,6 +284,7 @@
|
||||||
{state}
|
{state}
|
||||||
feature={selectedElement}
|
feature={selectedElement}
|
||||||
value={freeformInput}
|
value={freeformInput}
|
||||||
|
unvalidatedText={freeformInputUnvalidated}
|
||||||
on:submit={onSave}
|
on:submit={onSave}
|
||||||
/>
|
/>
|
||||||
{:else if mappings !== undefined && !config.multiAnswer}
|
{:else if mappings !== undefined && !config.multiAnswer}
|
||||||
|
@ -311,6 +327,7 @@
|
||||||
{state}
|
{state}
|
||||||
feature={selectedElement}
|
feature={selectedElement}
|
||||||
value={freeformInput}
|
value={freeformInput}
|
||||||
|
unvalidatedText={freeformInputUnvalidated}
|
||||||
on:selected={() => (selectedMapping = config.mappings?.length)}
|
on:selected={() => (selectedMapping = config.mappings?.length)}
|
||||||
on:submit={onSave}
|
on:submit={onSave}
|
||||||
/>
|
/>
|
||||||
|
@ -354,6 +371,7 @@
|
||||||
{state}
|
{state}
|
||||||
feature={selectedElement}
|
feature={selectedElement}
|
||||||
value={freeformInput}
|
value={freeformInput}
|
||||||
|
unvalidatedText={freeformInputUnvalidated}
|
||||||
on:submit={onSave}
|
on:submit={onSave}
|
||||||
/>
|
/>
|
||||||
</label>
|
</label>
|
||||||
|
@ -378,12 +396,19 @@
|
||||||
<!-- TagRenderingQuestion-buttons -->
|
<!-- TagRenderingQuestion-buttons -->
|
||||||
<slot name="cancel" />
|
<slot name="cancel" />
|
||||||
<slot name="save-button" {selectedTags}>
|
<slot name="save-button" {selectedTags}>
|
||||||
|
{#if allowDeleteOfFreeform && mappings.length === 0 && $freeformInput === undefined && $freeformInputUnvalidated === ""}
|
||||||
|
<button class="primary flex" on:click|stopPropagation|preventDefault={_ => onSave(_, true)}>
|
||||||
|
<TrashIcon class="w-6 h-6 text-red-500" />
|
||||||
|
<Tr t={Translations.t.general.eraseValue}/>
|
||||||
|
</button>
|
||||||
|
{:else}
|
||||||
<button
|
<button
|
||||||
on:click={onSave}
|
on:click={onSave}
|
||||||
class={twJoin(selectedTags === undefined ? "disabled" : "button-shadow", "primary")}
|
class={twJoin(selectedTags === undefined ? "disabled" : "button-shadow", "primary")}
|
||||||
>
|
>
|
||||||
<Tr t={Translations.t.general.save} />
|
<Tr t={Translations.t.general.save} />
|
||||||
</button>
|
</button>
|
||||||
|
{/if}
|
||||||
</slot>
|
</slot>
|
||||||
</div>
|
</div>
|
||||||
{#if UserRelatedState.SHOW_TAGS_VALUES.indexOf($showTags) >= 0 || ($showTags === "" && numberOfCs >= Constants.userJourney.tagsVisibleAt) || $featureSwitchIsTesting || $featureSwitchIsDebugging}
|
{#if UserRelatedState.SHOW_TAGS_VALUES.indexOf($showTags) >= 0 || ($showTags === "" && numberOfCs >= Constants.userJourney.tagsVisibleAt) || $featureSwitchIsTesting || $featureSwitchIsDebugging}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue