mirror of
https://github.com/garraflavatra/rolens.git
synced 2025-01-18 21:17:59 +00:00
Form list
This commit is contained in:
parent
15a20d4916
commit
23526ee641
67
frontend/src/components/details.svelte
Normal file
67
frontend/src/components/details.svelte
Normal file
@ -0,0 +1,67 @@
|
|||||||
|
<script>
|
||||||
|
import { createEventDispatcher, onMount } from 'svelte';
|
||||||
|
import Icon from './icon.svelte';
|
||||||
|
|
||||||
|
export let title = '';
|
||||||
|
export let initiallyOpen = false;
|
||||||
|
export let deletable = false;
|
||||||
|
|
||||||
|
const dispatch = createEventDispatcher();
|
||||||
|
/** @type {HTMLDetailsElement} */ let detailsElement;
|
||||||
|
|
||||||
|
onMount(() => {
|
||||||
|
if (initiallyOpen && detailsElement) {
|
||||||
|
detailsElement.open = initiallyOpen;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
</script>
|
||||||
|
|
||||||
|
<details bind:this={detailsElement}>
|
||||||
|
<summary>
|
||||||
|
<Icon name="chev-d" />
|
||||||
|
{title}
|
||||||
|
|
||||||
|
{#if deletable}
|
||||||
|
<button class="delete" on:click={() => dispatch('delete')}>
|
||||||
|
<Icon name="trash" />
|
||||||
|
</button>
|
||||||
|
{/if}
|
||||||
|
</summary>
|
||||||
|
|
||||||
|
<slot />
|
||||||
|
</details>
|
||||||
|
|
||||||
|
<style>
|
||||||
|
details {
|
||||||
|
border: 1px solid #aaa;
|
||||||
|
border-radius: 4px;
|
||||||
|
padding: 0.5em 0.5em 0;
|
||||||
|
margin-bottom: 0.5rem;
|
||||||
|
}
|
||||||
|
details[open] {
|
||||||
|
padding: 0.5em;
|
||||||
|
}
|
||||||
|
|
||||||
|
summary {
|
||||||
|
font-weight: 700;
|
||||||
|
margin: -0.5em -0.5em 0;
|
||||||
|
padding: 0.5em;
|
||||||
|
display: flex;
|
||||||
|
gap: 0.5rem;
|
||||||
|
align-items: center;
|
||||||
|
cursor: pointer;
|
||||||
|
}
|
||||||
|
summary :global(svg) {
|
||||||
|
width: 14px;
|
||||||
|
height: 14px;
|
||||||
|
}
|
||||||
|
details[open] summary {
|
||||||
|
border-bottom: 1px solid #aaa;
|
||||||
|
margin-bottom: 0.5em;
|
||||||
|
}
|
||||||
|
|
||||||
|
button.delete {
|
||||||
|
margin-left: auto;
|
||||||
|
color: #c00;
|
||||||
|
}
|
||||||
|
</style>
|
@ -1,18 +1,21 @@
|
|||||||
<script>
|
<script>
|
||||||
import { numericInputTypes } from '../utils';
|
import { canBeObjectId, numericInputTypes } from '../utils';
|
||||||
import { input } from '../actions';
|
import { input } from '../actions';
|
||||||
import Icon from './icon.svelte';
|
import Icon from './icon.svelte';
|
||||||
import { ObjectId } from 'bson';
|
import { ObjectId } from 'bson';
|
||||||
import Datepicker from './datepicker.svelte';
|
import Datepicker from './datepicker.svelte';
|
||||||
|
import { onMount } from 'svelte';
|
||||||
|
|
||||||
export let column = {};
|
export let column = {};
|
||||||
export let value = undefined;
|
export let value = undefined;
|
||||||
export let valid = true;
|
export let valid = true;
|
||||||
|
export let autofocus = false;
|
||||||
|
|
||||||
const onValid = () => valid = true;
|
const onValid = () => valid = true;
|
||||||
const onInvalid = () => valid = false;
|
const onInvalid = () => valid = false;
|
||||||
let objectIdInput;
|
let objectIdInput;
|
||||||
let showDatepicker;
|
let showDatepicker;
|
||||||
|
let selectInput;
|
||||||
$: type = column.inputType;
|
$: type = column.inputType;
|
||||||
$: mandatory = column.mandatory;
|
$: mandatory = column.mandatory;
|
||||||
|
|
||||||
@ -28,7 +31,7 @@
|
|||||||
}
|
}
|
||||||
|
|
||||||
function setObjectId(event) {
|
function setObjectId(event) {
|
||||||
if (valid) {
|
if (canBeObjectId(valid)) {
|
||||||
value = new ObjectId(event.currentTarget?.value);
|
value = new ObjectId(event.currentTarget?.value);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -56,23 +59,29 @@
|
|||||||
valid = true;
|
valid = true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
onMount(() => {
|
||||||
|
if (autofocus && selectInput) {
|
||||||
|
selectInput.focus();
|
||||||
|
}
|
||||||
|
});
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
<div class="forminput {type}">
|
<div class="forminput {type}">
|
||||||
<div class="field">
|
<div class="field">
|
||||||
{#if type === 'string'}
|
{#if type === 'string'}
|
||||||
<input type="text" bind:value use:input={{ type, onValid, onInvalid, mandatory }} />
|
<input type="text" bind:value use:input={{ type, onValid, onInvalid, mandatory, autofocus }} />
|
||||||
{:else if type === 'objectid'}
|
{:else if type === 'objectid'}
|
||||||
<input
|
<input
|
||||||
type="text"
|
type="text"
|
||||||
bind:this={objectIdInput}
|
bind:this={objectIdInput}
|
||||||
on:input={setObjectId}
|
on:input={setObjectId}
|
||||||
use:input={{ type, onValid, onInvalid, mandatory }}
|
use:input={{ type, onValid, onInvalid, mandatory, autofocus }}
|
||||||
/>
|
/>
|
||||||
{:else if numericInputTypes.includes(type)}
|
{:else if numericInputTypes.includes(type)}
|
||||||
<input type="number" bind:value use:input={{ type, onValid, onInvalid, mandatory }} />
|
<input type="number" bind:value use:input={{ type, onValid, onInvalid, mandatory, autofocus }} />
|
||||||
{:else if type === 'bool'}
|
{:else if type === 'bool'}
|
||||||
<select bind:value on:change={selectChange}>
|
<select bind:value on:change={selectChange} bind:this={selectInput}>
|
||||||
<option value={undefined} disabled={mandatory}>Unset</option>
|
<option value={undefined} disabled={mandatory}>Unset</option>
|
||||||
<option value={true}>Yes / true</option>
|
<option value={true}>Yes / true</option>
|
||||||
<option value={false}>No / false</option>
|
<option value={false}>No / false</option>
|
||||||
@ -120,17 +129,13 @@
|
|||||||
}
|
}
|
||||||
|
|
||||||
.actions {
|
.actions {
|
||||||
display: none;
|
display: flex;
|
||||||
position: absolute;
|
position: absolute;
|
||||||
right: 5px;
|
right: 5px;
|
||||||
top: 6px;
|
top: 5px;
|
||||||
background-color: #fff;
|
background-color: #fff;
|
||||||
}
|
}
|
||||||
.actions button:last-child {
|
.actions button:last-child {
|
||||||
border-radius: 2px 6px 6px 2px;
|
border-radius: 2px 6px 6px 2px;
|
||||||
}
|
}
|
||||||
|
|
||||||
.forminput:hover .actions {
|
|
||||||
display: flex;
|
|
||||||
}
|
|
||||||
</style>
|
</style>
|
||||||
|
@ -26,7 +26,7 @@
|
|||||||
let _items = [];
|
let _items = [];
|
||||||
|
|
||||||
$: refresh(hideObjectIndicators, items);
|
$: refresh(hideObjectIndicators, items);
|
||||||
$: inputsValid = Object.values(validity).every(v => !!v);
|
$: inputsValid = Object.values(validity).every(v => v !== false);
|
||||||
|
|
||||||
function refresh(hideObjectIndicators, items) {
|
function refresh(hideObjectIndicators, items) {
|
||||||
_items = objectToArray(items).map(item => {
|
_items = objectToArray(items).map(item => {
|
||||||
|
@ -1,7 +1,5 @@
|
|||||||
<script>
|
<script>
|
||||||
import { createEventDispatcher } from 'svelte';
|
|
||||||
import GridItems from './grid-items.svelte';
|
import GridItems from './grid-items.svelte';
|
||||||
import Icon from './icon.svelte';
|
|
||||||
|
|
||||||
export let columns = [];
|
export let columns = [];
|
||||||
export let items = [];
|
export let items = [];
|
||||||
@ -11,17 +9,10 @@
|
|||||||
export let showHeaders = false;
|
export let showHeaders = false;
|
||||||
export let hideObjectIndicators = false;
|
export let hideObjectIndicators = false;
|
||||||
export let hideChildrenToggles = false;
|
export let hideChildrenToggles = false;
|
||||||
export let canAddRows = false;
|
|
||||||
export let canSelect = true;
|
export let canSelect = true;
|
||||||
export let canRemoveItems = false;
|
export let canRemoveItems = false;
|
||||||
export let inputsValid = false;
|
export let inputsValid = false;
|
||||||
// export let actions = [];
|
// export let actions = [];
|
||||||
|
|
||||||
const dispatch = createEventDispatcher();
|
|
||||||
|
|
||||||
function addRow() {
|
|
||||||
dispatch('addRow');
|
|
||||||
}
|
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
<div class="grid">
|
<div class="grid">
|
||||||
@ -73,14 +64,6 @@
|
|||||||
on:trigger
|
on:trigger
|
||||||
/>
|
/>
|
||||||
</tbody>
|
</tbody>
|
||||||
|
|
||||||
{#if canAddRows}
|
|
||||||
<tfoot>
|
|
||||||
<button class="btn-sm" type="button" on:click={addRow}>
|
|
||||||
<Icon name="+" />
|
|
||||||
</button>
|
|
||||||
</tfoot>
|
|
||||||
{/if}
|
|
||||||
</table>
|
</table>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
|
@ -23,16 +23,18 @@
|
|||||||
};
|
};
|
||||||
|
|
||||||
const keypathProxy = new Proxy(item, {
|
const keypathProxy = new Proxy(item, {
|
||||||
get: (item, key) => resolveKeypath(item, key),
|
get: resolveKeypath,
|
||||||
set: (item, key, value) => {
|
set: (item, key, value) => {
|
||||||
setValue(item, key, value);
|
setValue(item, key, value);
|
||||||
return true;
|
// eslint-disable-next-line no-self-assign
|
||||||
|
value = value;
|
||||||
|
return item;
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
{#if item && view}
|
{#if item && view}
|
||||||
{#each view?.columns?.filter(c => inputTypes.includes(c.inputType)) || [] as column}
|
{#each view?.columns?.filter(c => inputTypes.includes(c.inputType)) || [] as column, index}
|
||||||
<!-- svelte-ignore a11y-label-has-associated-control because FormInput contains one -->
|
<!-- svelte-ignore a11y-label-has-associated-control because FormInput contains one -->
|
||||||
<label class="column">
|
<label class="column">
|
||||||
<div class="label">
|
<div class="label">
|
||||||
@ -45,7 +47,7 @@
|
|||||||
</span>
|
</span>
|
||||||
</div>
|
</div>
|
||||||
<div class="input">
|
<div class="input">
|
||||||
<FormInput {column} bind:value={keypathProxy[column.key]} bind:valid={validity[column.key]} />
|
<FormInput {column} bind:value={keypathProxy[column.key]} bind:valid={validity[column.key]} autofocus={index === 0} />
|
||||||
</div>
|
</div>
|
||||||
</label>
|
</label>
|
||||||
{/each}
|
{/each}
|
||||||
|
@ -8,21 +8,33 @@
|
|||||||
import ObjectViewer from '../../../components/objectviewer.svelte';
|
import ObjectViewer from '../../../components/objectviewer.svelte';
|
||||||
import Grid from '../../../components/grid.svelte';
|
import Grid from '../../../components/grid.svelte';
|
||||||
import { inputTypes, randomString } from '../../../utils';
|
import { inputTypes, randomString } from '../../../utils';
|
||||||
|
import { EJSON } from 'bson';
|
||||||
|
import Details from '../../../components/details.svelte';
|
||||||
|
|
||||||
export let collection;
|
export let collection;
|
||||||
|
|
||||||
const dispatch = createEventDispatcher();
|
const dispatch = createEventDispatcher();
|
||||||
|
const formValidity = {};
|
||||||
let json = '';
|
let json = '';
|
||||||
let newItems = [];
|
let newItems = [];
|
||||||
let insertedIds;
|
let insertedIds;
|
||||||
let objectViewerData = '';
|
let objectViewerData = '';
|
||||||
let viewType = 'form';
|
let viewType = 'form';
|
||||||
let formValid = false;
|
let allValid = false;
|
||||||
$: viewsForCollection = views.forCollection(collection.hostKey, collection.dbKey, collection.key);
|
$: viewsForCollection = views.forCollection(collection.hostKey, collection.dbKey, collection.key);
|
||||||
$: oppositeViewType = viewType === 'table' ? 'form' : 'table';
|
$: oppositeViewType = viewType === 'table' ? 'form' : 'table';
|
||||||
|
$: allValid = Object.values(formValidity).every(v => v !== false);
|
||||||
|
|
||||||
$: if (collection.viewKey !== 'list') {
|
$: {
|
||||||
json = JSON.stringify(newItems, undefined, 2);
|
if (collection.viewKey === 'list') {
|
||||||
|
try {
|
||||||
|
newItems = EJSON.parse(json, { relaxed: false });
|
||||||
|
}
|
||||||
|
catch { /* ok */ }
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
json = EJSON.stringify(newItems, undefined, 2, { relaxed: false });
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async function insert() {
|
async function insert() {
|
||||||
@ -56,9 +68,15 @@
|
|||||||
function addRow() {
|
function addRow() {
|
||||||
newItems = [ ...newItems, {} ];
|
newItems = [ ...newItems, {} ];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function deleteRow(index) {
|
||||||
|
newItems.splice(index, 1);
|
||||||
|
newItems = newItems;
|
||||||
|
}
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
<form on:submit|preventDefault={insert}>
|
<form on:submit|preventDefault={insert}>
|
||||||
|
<div class="items">
|
||||||
{#if collection.viewKey === 'list'}
|
{#if collection.viewKey === 'list'}
|
||||||
<label class="field">
|
<label class="field">
|
||||||
<textarea
|
<textarea
|
||||||
@ -72,7 +90,18 @@
|
|||||||
</label>
|
</label>
|
||||||
{:else if viewType === 'form'}
|
{:else if viewType === 'form'}
|
||||||
<div class="form">
|
<div class="form">
|
||||||
<Form bind:item={newItems[0]} bind:valid={formValid} view={$views[collection.viewKey]} />
|
{#each newItems as item, index}
|
||||||
|
<Details
|
||||||
|
title="Item #{index + 1} {(item._id !== undefined) ? `(${item._id})` : ''}"
|
||||||
|
initiallyOpen={index === 0}
|
||||||
|
deletable={true}
|
||||||
|
on:delete={() => deleteRow(index)}
|
||||||
|
>
|
||||||
|
<fieldset>
|
||||||
|
<Form bind:item={newItems[index]} bind:valid={formValidity[index]} view={$views[collection.viewKey]} />
|
||||||
|
</fieldset>
|
||||||
|
</Details>
|
||||||
|
{/each}
|
||||||
</div>
|
</div>
|
||||||
{:else if viewType === 'table'}
|
{:else if viewType === 'table'}
|
||||||
<div class="table">
|
<div class="table">
|
||||||
@ -85,18 +114,23 @@
|
|||||||
.map(c => ({ ...c, id: randomString(8), title: c.key })) || []
|
.map(c => ({ ...c, id: randomString(8), title: c.key })) || []
|
||||||
}
|
}
|
||||||
showHeaders={true}
|
showHeaders={true}
|
||||||
canAddRows={true}
|
|
||||||
canSelect={false}
|
canSelect={false}
|
||||||
canRemoveItems={true}
|
canRemoveItems={true}
|
||||||
hideChildrenToggles={true}
|
hideChildrenToggles={true}
|
||||||
on:addRow={addRow}
|
on:addRow={addRow}
|
||||||
bind:inputsValid={formValid}
|
bind:inputsValid={allValid}
|
||||||
/>
|
/>
|
||||||
</div>
|
</div>
|
||||||
{/if}
|
{/if}
|
||||||
|
</div>
|
||||||
|
|
||||||
<div class="flex">
|
<div class="flex">
|
||||||
<div>
|
<div>
|
||||||
|
{#if collection.viewKey !== 'list'}
|
||||||
|
<button class="btn" type="button" on:click={addRow}>
|
||||||
|
<Icon name="+" /> Add item
|
||||||
|
</button>
|
||||||
|
{/if}
|
||||||
{#if insertedIds}
|
{#if insertedIds}
|
||||||
<span class="flash-green">Success! {insertedIds.length} document{insertedIds.length > 1 ? 's' : ''} inserted</span>
|
<span class="flash-green">Success! {insertedIds.length} document{insertedIds.length > 1 ? 's' : ''} inserted</span>
|
||||||
{/if}
|
{/if}
|
||||||
@ -123,7 +157,7 @@
|
|||||||
<Icon name="cog" />
|
<Icon name="cog" />
|
||||||
</button>
|
</button>
|
||||||
</label>
|
</label>
|
||||||
<button type="submit" class="btn" disabled={$views[collection.viewKey]?.type === 'list' ? !json : !formValid}>
|
<button type="submit" class="btn" disabled={$views[collection.viewKey]?.type === 'list' ? !json : !allValid}>
|
||||||
<Icon name="+" /> Insert
|
<Icon name="+" /> Insert
|
||||||
</button>
|
</button>
|
||||||
</div>
|
</div>
|
||||||
@ -135,11 +169,14 @@
|
|||||||
<style>
|
<style>
|
||||||
form {
|
form {
|
||||||
display: grid;
|
display: grid;
|
||||||
grid-template-rows: 1fr auto;
|
grid-template: 1fr auto / 1fr;
|
||||||
gap: 0.5rem;
|
gap: 0.5rem;
|
||||||
}
|
}
|
||||||
|
|
||||||
.table {
|
.items {
|
||||||
|
overflow: auto;
|
||||||
|
}
|
||||||
|
.items .table {
|
||||||
background-color: #fff;
|
background-color: #fff;
|
||||||
border: 1px solid #ccc;
|
border: 1px solid #ccc;
|
||||||
}
|
}
|
||||||
|
@ -2,7 +2,7 @@ import { ObjectId } from 'bson';
|
|||||||
import { get } from 'svelte/store';
|
import { get } from 'svelte/store';
|
||||||
import { environment } from './stores';
|
import { environment } from './stores';
|
||||||
|
|
||||||
// Calculate the min and max values of signed integers with n bits
|
// Calculate the min and max values of (un)signed integers with n bits
|
||||||
export const intMin = bits => Math.pow(2, bits - 1) * -1;
|
export const intMin = bits => Math.pow(2, bits - 1) * -1;
|
||||||
export const intMax = bits => Math.pow(2, bits - 1) - 1;
|
export const intMax = bits => Math.pow(2, bits - 1) - 1;
|
||||||
export const uintMax = bits => Math.pow(2, bits) - 1;
|
export const uintMax = bits => Math.pow(2, bits) - 1;
|
||||||
@ -64,6 +64,7 @@ export function setValue(object, path, value) {
|
|||||||
}
|
}
|
||||||
return part;
|
return part;
|
||||||
});
|
});
|
||||||
|
|
||||||
let result = object;
|
let result = object;
|
||||||
while (parts.length) {
|
while (parts.length) {
|
||||||
const part = parts.shift();
|
const part = parts.shift();
|
||||||
@ -78,6 +79,7 @@ export function setValue(object, path, value) {
|
|||||||
}
|
}
|
||||||
result = result[part];
|
result = result[part];
|
||||||
}
|
}
|
||||||
|
|
||||||
return object;
|
return object;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user