2023-01-31 15:58:23 +00:00
|
|
|
<script>
|
2023-01-31 20:12:31 +00:00
|
|
|
import { canBeObjectId, numericInputTypes } from '../utils';
|
2023-01-31 15:58:23 +00:00
|
|
|
import { input } from '../actions';
|
|
|
|
import Icon from './icon.svelte';
|
|
|
|
import { ObjectId } from 'bson';
|
|
|
|
import Datepicker from './datepicker.svelte';
|
2023-01-31 20:12:31 +00:00
|
|
|
import { onMount } from 'svelte';
|
2023-01-31 15:58:23 +00:00
|
|
|
|
|
|
|
export let column = {};
|
|
|
|
export let value = undefined;
|
|
|
|
export let valid = true;
|
2023-01-31 20:12:31 +00:00
|
|
|
export let autofocus = false;
|
2023-01-31 15:58:23 +00:00
|
|
|
|
|
|
|
const onValid = () => valid = true;
|
|
|
|
const onInvalid = () => valid = false;
|
|
|
|
let objectIdInput;
|
|
|
|
let showDatepicker;
|
2023-01-31 20:12:31 +00:00
|
|
|
let selectInput;
|
2023-01-31 15:58:23 +00:00
|
|
|
$: type = column.inputType;
|
|
|
|
$: mandatory = column.mandatory;
|
|
|
|
|
|
|
|
$: if ((value === undefined) && mandatory) {
|
|
|
|
valid = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
function markInputValid(input) {
|
|
|
|
input.setCustomValidity('');
|
|
|
|
input.reportValidity();
|
|
|
|
input.classList.remove('invalid');
|
|
|
|
valid = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
function setObjectId(event) {
|
2023-01-31 20:12:31 +00:00
|
|
|
if (canBeObjectId(valid)) {
|
2023-01-31 15:58:23 +00:00
|
|
|
value = new ObjectId(event.currentTarget?.value);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function generateObjectId() {
|
|
|
|
value = new ObjectId();
|
|
|
|
objectIdInput.value = value.toString();
|
|
|
|
markInputValid(objectIdInput);
|
|
|
|
objectIdInput.disabled = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
function editObjectId() {
|
|
|
|
if (!objectIdInput) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
objectIdInput.disabled = false;
|
|
|
|
objectIdInput.focus();
|
|
|
|
}
|
|
|
|
|
|
|
|
function selectChange() {
|
|
|
|
if ((value === undefined) && mandatory) {
|
|
|
|
valid = false;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
valid = true;
|
|
|
|
}
|
|
|
|
}
|
2023-01-31 20:12:31 +00:00
|
|
|
|
|
|
|
onMount(() => {
|
|
|
|
if (autofocus && selectInput) {
|
|
|
|
selectInput.focus();
|
|
|
|
}
|
|
|
|
});
|
2023-01-31 15:58:23 +00:00
|
|
|
</script>
|
|
|
|
|
|
|
|
<div class="forminput {type}">
|
|
|
|
<div class="field">
|
|
|
|
{#if type === 'string'}
|
2023-01-31 20:12:31 +00:00
|
|
|
<input type="text" bind:value use:input={{ type, onValid, onInvalid, mandatory, autofocus }} />
|
2023-01-31 15:58:23 +00:00
|
|
|
{:else if type === 'objectid'}
|
|
|
|
<input
|
|
|
|
type="text"
|
|
|
|
bind:this={objectIdInput}
|
|
|
|
on:input={setObjectId}
|
2023-01-31 20:12:31 +00:00
|
|
|
use:input={{ type, onValid, onInvalid, mandatory, autofocus }}
|
2023-01-31 15:58:23 +00:00
|
|
|
/>
|
|
|
|
{:else if numericInputTypes.includes(type)}
|
2023-01-31 20:12:31 +00:00
|
|
|
<input type="number" bind:value use:input={{ type, onValid, onInvalid, mandatory, autofocus }} />
|
2023-01-31 15:58:23 +00:00
|
|
|
{:else if type === 'bool'}
|
2023-01-31 20:12:31 +00:00
|
|
|
<select bind:value on:change={selectChange} bind:this={selectInput}>
|
2023-01-31 15:58:23 +00:00
|
|
|
<option value={undefined} disabled={mandatory}>Unset</option>
|
|
|
|
<option value={true}>Yes / true</option>
|
|
|
|
<option value={false}>No / false</option>
|
|
|
|
</select>
|
|
|
|
{:else if type === 'date'}
|
|
|
|
<input type="text" readonly value={value?.toString() || '...'} on:focus={() => showDatepicker = true} />
|
|
|
|
{/if}
|
|
|
|
</div>
|
|
|
|
|
|
|
|
<div class="actions">
|
|
|
|
{#if type === 'objectid'}
|
|
|
|
{#if objectIdInput?.disabled}
|
|
|
|
<button class="btn-sm" type="button" title="Edit object id" on:click={editObjectId}>
|
|
|
|
<Icon name="edit" />
|
|
|
|
</button>
|
|
|
|
{/if}
|
|
|
|
<button class="btn-sm" type="button" title="Generate random object id" on:click={generateObjectId}>
|
|
|
|
<Icon name="reload" />
|
|
|
|
</button>
|
|
|
|
{:else if type === 'date'}
|
|
|
|
<button class="btn-sm" type="button" title="Edit date" on:click={() => showDatepicker = true}>
|
|
|
|
<Icon name="edit" />
|
|
|
|
</button>
|
|
|
|
<button class="btn-sm" type="button" title="Set date to now" on:click={() => value = new Date()}>
|
|
|
|
<Icon name="o" />
|
|
|
|
</button>
|
|
|
|
{/if}
|
|
|
|
<button class="btn-sm" type="button" title="Reset field to default value" on:click={() => value = undefined}>
|
|
|
|
<Icon name="trash" />
|
|
|
|
</button>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
|
|
|
|
{#if type === 'date'}
|
|
|
|
<Datepicker bind:value bind:show={showDatepicker} />
|
|
|
|
{/if}
|
|
|
|
|
|
|
|
<style>
|
|
|
|
.forminput {
|
|
|
|
position: relative;
|
|
|
|
}
|
|
|
|
|
|
|
|
.forminput.date input {
|
|
|
|
cursor: pointer;
|
|
|
|
}
|
|
|
|
|
|
|
|
.actions {
|
2023-01-31 20:12:31 +00:00
|
|
|
display: flex;
|
2023-01-31 15:58:23 +00:00
|
|
|
position: absolute;
|
|
|
|
right: 5px;
|
2023-01-31 20:12:31 +00:00
|
|
|
top: 5px;
|
2023-01-31 15:58:23 +00:00
|
|
|
background-color: #fff;
|
|
|
|
}
|
|
|
|
.actions button:last-child {
|
|
|
|
border-radius: 2px 6px 6px 2px;
|
|
|
|
}
|
|
|
|
</style>
|