mirror of
https://github.com/garraflavatra/rolens.git
synced 2025-01-18 21:17:59 +00:00
Insert: form improvements
This commit is contained in:
parent
838c8d2ffb
commit
a3335cdc4e
@ -22,7 +22,7 @@
|
|||||||
{title}
|
{title}
|
||||||
|
|
||||||
{#if deletable}
|
{#if deletable}
|
||||||
<button class="delete" on:click={() => dispatch('delete')}>
|
<button class="delete" on:click={() => dispatch('delete')} type="button">
|
||||||
<Icon name="trash" />
|
<Icon name="trash" />
|
||||||
</button>
|
</button>
|
||||||
{/if}
|
{/if}
|
||||||
|
4
frontend/src/lib/strings.js
Normal file
4
frontend/src/lib/strings.js
Normal file
@ -0,0 +1,4 @@
|
|||||||
|
export function capitalise(string = '') {
|
||||||
|
const capitalised = string.charAt(0).toUpperCase() + string.slice(1);
|
||||||
|
return capitalised;
|
||||||
|
}
|
@ -3,10 +3,12 @@
|
|||||||
import Icon from '$components/icon.svelte';
|
import Icon from '$components/icon.svelte';
|
||||||
import { inputTypes } from '$lib/mongo';
|
import { inputTypes } from '$lib/mongo';
|
||||||
import { resolveKeypath, setValue } from '$lib/keypaths';
|
import { resolveKeypath, setValue } from '$lib/keypaths';
|
||||||
|
import Hint from '$components/hint.svelte';
|
||||||
|
|
||||||
export let item = {};
|
export let item = {};
|
||||||
export let view = {};
|
export let view = {};
|
||||||
export let valid = false;
|
export let valid = false;
|
||||||
|
export let emptyHint = '';
|
||||||
|
|
||||||
const validity = {};
|
const validity = {};
|
||||||
$: valid = Object.values(validity).every(v => v !== false);
|
$: valid = Object.values(validity).every(v => v !== false);
|
||||||
@ -51,6 +53,10 @@
|
|||||||
<FormInput {column} bind:value={keypathProxy[column.key]} bind:valid={validity[column.key]} autofocus={index === 0} />
|
<FormInput {column} bind:value={keypathProxy[column.key]} bind:valid={validity[column.key]} autofocus={index === 0} />
|
||||||
</div>
|
</div>
|
||||||
</label>
|
</label>
|
||||||
|
{:else}
|
||||||
|
{#if emptyHint}
|
||||||
|
<Hint>{emptyHint}</Hint>
|
||||||
|
{/if}
|
||||||
{/each}
|
{/each}
|
||||||
{/if}
|
{/if}
|
||||||
|
|
||||||
|
@ -7,6 +7,7 @@
|
|||||||
import { randomString } from '$lib/math';
|
import { randomString } from '$lib/math';
|
||||||
import { inputTypes } from '$lib/mongo';
|
import { inputTypes } from '$lib/mongo';
|
||||||
import views from '$lib/stores/views';
|
import views from '$lib/stores/views';
|
||||||
|
import { capitalise } from '$lib/strings';
|
||||||
import { InsertItems } from '$wails/go/app/App';
|
import { InsertItems } from '$wails/go/app/App';
|
||||||
import { EJSON } from 'bson';
|
import { EJSON } from 'bson';
|
||||||
import { createEventDispatcher } from 'svelte';
|
import { createEventDispatcher } from 'svelte';
|
||||||
@ -16,12 +17,14 @@
|
|||||||
|
|
||||||
const dispatch = createEventDispatcher();
|
const dispatch = createEventDispatcher();
|
||||||
const formValidity = {};
|
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 allValid = 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);
|
$: allValid = Object.values(formValidity).every(v => v !== false);
|
||||||
@ -38,6 +41,10 @@
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
$: if ((viewType === 'form') && !newItems?.length) {
|
||||||
|
newItems = [ {} ];
|
||||||
|
}
|
||||||
|
|
||||||
async function insert() {
|
async function insert() {
|
||||||
insertedIds = await InsertItems(collection.hostKey, collection.dbKey, collection.key, json);
|
insertedIds = await InsertItems(collection.hostKey, collection.dbKey, collection.key, json);
|
||||||
if ((collection.viewKey === 'list') && insertedIds) {
|
if ((collection.viewKey === 'list') && insertedIds) {
|
||||||
@ -66,8 +73,17 @@
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function addRow() {
|
function addRow(beforeIndex = -1) {
|
||||||
newItems = [ ...newItems, {} ];
|
if ((beforeIndex === -1) || (typeof beforeIndex !== 'number')) {
|
||||||
|
newItems = [ ...newItems, {} ];
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
newItems = [
|
||||||
|
...newItems.slice(0, beforeIndex),
|
||||||
|
{},
|
||||||
|
...newItems.slice(beforeIndex + 1),
|
||||||
|
];
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function deleteRow(index) {
|
function deleteRow(index) {
|
||||||
@ -99,7 +115,12 @@
|
|||||||
on:delete={() => deleteRow(index)}
|
on:delete={() => deleteRow(index)}
|
||||||
>
|
>
|
||||||
<fieldset>
|
<fieldset>
|
||||||
<Form bind:item={newItems[index]} bind:valid={formValidity[index]} view={$views[collection.viewKey]} />
|
<Form
|
||||||
|
bind:item={newItems[index]}
|
||||||
|
bind:valid={formValidity[index]}
|
||||||
|
view={$views[collection.viewKey]}
|
||||||
|
emptyHint="This form has no fields. Use the view configurator in the bottom right corner to add fields."
|
||||||
|
/>
|
||||||
</fieldset>
|
</fieldset>
|
||||||
</Details>
|
</Details>
|
||||||
{/each}
|
{/each}
|
||||||
@ -123,15 +144,16 @@
|
|||||||
/>
|
/>
|
||||||
</div>
|
</div>
|
||||||
{/if}
|
{/if}
|
||||||
|
|
||||||
|
{#if collection.viewKey !== 'list'}
|
||||||
|
<button class="btn-sm" type="button" on:click={addRow}>
|
||||||
|
<Icon name="+" /> Add item
|
||||||
|
</button>
|
||||||
|
{/if}
|
||||||
</div>
|
</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}
|
||||||
@ -145,7 +167,7 @@
|
|||||||
<Icon name="code" />
|
<Icon name="code" />
|
||||||
</button>
|
</button>
|
||||||
<button class="btn" type="button" on:click={switchViewType} title="Edit as {oppositeViewType}">
|
<button class="btn" type="button" on:click={switchViewType} title="Edit as {oppositeViewType}">
|
||||||
<Icon name={oppositeViewType} />
|
<Icon name={oppositeViewType} /> {capitalise(oppositeViewType)}
|
||||||
</button>
|
</button>
|
||||||
{/if}
|
{/if}
|
||||||
<label class="field inline">
|
<label class="field inline">
|
||||||
|
Loading…
Reference in New Issue
Block a user