1
0
mirror of https://github.com/garraflavatra/rolens.git synced 2025-07-22 23:18:02 +00:00
This commit is contained in:
2023-01-29 20:00:15 +01:00
parent 2d33c6f2ab
commit 1cf89b3278
20 changed files with 817 additions and 370 deletions

View File

@ -0,0 +1,97 @@
<script>
import { resolveKeypath, setValue } from '../../../../utils';
import Icon from '../../../../components/icon.svelte';
import FormInput from './forminput.svelte';
export let item = {};
export let view = {};
export let valid = false;
const validity = {};
$: valid = Object.values(validity).every(v => !!v);
const iconMap = {
string: 'text',
int: 'hash',
long: 'hash',
uint64: 'hash',
double: 'hash',
decimal: 'hash',
bool: 'toggle-l',
date: 'cal',
};
const keypathProxy = new Proxy(item, {
get: (item, key) => resolveKeypath(item, key),
set: (item, key, value) => {
setValue(item, key, value);
return true;
},
});
function reset(columnKey) {
keypathProxy[columnKey] = undefined;
}
</script>
{#if item && view}
{#each view?.columns?.filter(c => c.inputType !== 'none') || [] as column}
<!-- svelte-ignore a11y-label-has-associated-control because FormInput contains one -->
<label class="column">
<div class="label">
<Icon name={iconMap[column.inputType]} />
<span>
{column.key}
{#if column.mandatory}
<span class="tag" class:invalid={!validity[column.key]}>mandatory</span>
{/if}
</span>
</div>
<div class="input">
<FormInput {column} bind:value={keypathProxy[column.key]} bind:valid={validity[column.key]} />
<button type="button" class="btn" title="Reset value" on:click={() => reset(column.key)} disabled={!keypathProxy[column.key]}>
<Icon name="reload" />
</button>
</div>
</label>
{/each}
{/if}
<style>
.column {
display: block;
}
.column + .column {
margin-top: 1rem;
}
.column .label {
margin-bottom: 0.5rem;
display: flex;
align-items: center;
gap: 0.5rem;
}
.column .label :global(svg) {
width: 13px;
height: 13px;
}
.input {
display: grid;
grid-template: 1fr / 1fr auto;
gap: 0.5rem;
}
.tag {
display: inline-block;
background-color: rgba(140, 140, 140, 0.1);
color: #777;
text-transform: uppercase;
font-size: 10px;
padding: 3px 5px;
font-weight: 600;
}
.tag.invalid {
background-color: rgba(255, 80, 80, 0.3);
color: #8d2c2c;
}
</style>

View File

@ -0,0 +1,76 @@
<script>
import { isDate } from '../../../../utils';
import { createEventDispatcher } from 'svelte';
import { input } from '../../../../actions';
export let column = {};
export let value = undefined;
export let valid = true;
const dispatch = createEventDispatcher();
const onValid = () => valid = true;
const onInvalid = () => valid = false;
const numericTypes = [ 'int', 'long', 'uint64', 'double', 'decimal' ];
let dateInput;
let timeInput;
$: type = column.inputType;
$: mandatory = column.mandatory;
$: dispatch('input', value);
$: if (value === undefined) {
dateInput && (dateInput.value = undefined);
timeInput && (timeInput.value = undefined);
mandatory && (valid = false);
}
function setDate(event) {
if (event?.currentTarget?.value) {
if (!isDate(value)) {
value = new Date(event.currentTarget.value);
}
const date = event.currentTarget.value.split('-').map(n => parseInt(n));
value.setFullYear(date[0], date[1], date[2]);
}
}
function setTime(event) {
if (event?.currentTarget?.value) {
const time = event.currentTarget.value.split(':').map(n => parseInt(n));
value.setHours?.(time[0], time[1], 0, 0);
}
}
function selectChange() {
if ((value === undefined) && mandatory) {
valid = false;
}
else {
valid = true;
}
}
</script>
<div class="field {type}">
{#if type === 'string'}
<input type="text" bind:value use:input={{ type, onValid, onInvalid, mandatory }} />
{:else if numericTypes.includes(type)}
<input type="number" bind:value use:input={{ type, onValid, onInvalid, mandatory }} />
{:else if type === 'bool'}
<select bind:value on:change={selectChange}>
<option value={undefined} disabled={mandatory}>Unset</option>
<option value={true}>Yes / true</option>
<option value={false}>No / false</option>
</select>
{:else if type === 'date'}
{@const isNotDate = !isDate(value)}
<input type="date" bind:this={dateInput} on:input={setDate} use:input />
<input type="time" bind:this={timeInput} on:input={setTime} disabled={isNotDate} title={isNotDate ? 'Enter a date first' : ''} />
{/if}
</div>
<style>
.field.date {
display: grid;
grid-template: 1fr / 3fr 1fr;
}
</style>

View File

@ -0,0 +1,134 @@
<script>
import Icon from '../../../../components/icon.svelte';
import { input } from '../../../../actions';
import Modal from '../../../../components/modal.svelte';
import { CreateIndex } from '../../../../../wailsjs/go/app/App';
import { createEventDispatcher } from 'svelte';
export let collection = {};
export let creatingNewIndex = false;
const dispatch = createEventDispatcher();
let index = { model: [] };
function addRule() {
index.model = [ ...index.model, {} ];
}
function removeRule(ruleIndex) {
index.model.splice(ruleIndex, 1);
index.model = index.model;
}
async function create() {
const newIndexName = await CreateIndex(collection.hostKey, collection.dbKey, collection.key, JSON.stringify(index));
if (newIndexName) {
creatingNewIndex = false;
index = { model: [] };
dispatch('reload');
}
}
</script>
<Modal title="Create new index {collection ? `on collection ${collection.key}` : ''}" bind:show={creatingNewIndex}>
<form on:submit|preventDefault={create}>
<label class="field name">
<span class="label">Name</span>
<input type="text" placeholder="Optional" bind:value={index.name} use:input={{ autofocus: true }} />
</label>
<div class="toggles">
<label class="field">
<span class="label">Background (legacy)</span>
<span class="checkbox">
<input type="checkbox" bind:checked={index.background} />
</span>
</label>
<label class="field">
<span class="label">Unique</span>
<span class="checkbox">
<input type="checkbox" bind:checked={index.unique} />
</span>
</label>
<!-- <label class="field">
<span class="label">Drop duplicates</span>
<span class="checkbox">
<input type="checkbox" bind:checked={index.dropDuplicates} />
</span>
</label> -->
<label class="field">
<span class="label">Sparse</span>
<span class="checkbox">
<input type="checkbox" bind:checked={index.sparse} />
</span>
</label>
</div>
<div class="model">
{#each index.model as rule, ruleIndex}
<div class="row">
<label class="field">
<span class="label">Key</span>
<input type="text" placeholder="_id" bind:value={rule.key}>
</label>
<label class="field">
<select bind:value={rule.sort}>
<option value={1}>Ascending</option>
<option value={-1}>Decending</option>
<option value="hashed" disabled={index.model.length > 1}>Hashed</option>
</select>
</label>
<button type="button" class="btn danger" on:click={() => removeRule(ruleIndex)} disabled={index.model.length < 2}>
<Icon name="-" />
</button>
</div>
{:else}
No rules
{/each}
</div>
<div class="buttons">
<button class="btn" type="button" on:click={addRule} disabled={index.model.some(r => r.sort === 'hashed')}>
<Icon name="+" /> Add rule
</button>
<button class="btn" type="submit" disabled={!index.model.length || index.model.some(r => !r.key)}>
<Icon name="+" /> Create index
</button>
</div>
</form>
</Modal>
<style>
.field.name {
margin-bottom: 0.5rem;
}
.toggles {
display: flex;
flex-wrap: wrap;
gap: 0.5rem;
margin-bottom: 0.5rem;
}
.model {
display: grid;
grid-template-columns: 1fr;
gap: 0.5rem;
margin-bottom: 0.5rem;
padding: 0.5rem;
border: 1px solid #ccc;
}
.model .row {
display: grid;
grid-template: 1fr / 1fr auto auto;
gap: 0.5rem;
}
.buttons {
display: flex;
gap: 0.5rem;
}
.buttons button[type="submit"] {
margin-left: auto;
}
</style>

View File

@ -0,0 +1,242 @@
<script>
import TabBar from '../../../../components/tabbar.svelte';
import Modal from '../../../../components/modal.svelte';
import Icon from '../../../../components/icon.svelte';
import { views } from '../../../../stores';
import { randomString } from '../../../../utils';
import { input } from '../../../../actions';
export let collection;
export let show = false;
export let activeViewKey = 'list';
export let firstItem = {};
$: tabs = Object.entries(views.forCollection(collection.hostKey, collection.dbKey, collection.key))
.sort((a, b) => sortTabKeys(a[0], b[0]))
.map(([ key, v ]) => ({ key, title: v.name, closable: key !== 'list' }));
function sortTabKeys(a, b) {
if (a === 'list') {
return -1;
}
if (b === 'list') {
return 1;
}
else {
return a.localeCompare(b);
}
}
function createView() {
const newViewKey = randomString();
$views[newViewKey] = {
name: 'Table view',
host: collection.hostKey,
database: collection.dbKey,
collection: collection.key,
type: 'table',
columns: [ { key: '_id', showInTable: true, inputType: 'string' } ],
};
activeViewKey = newViewKey;
}
function removeView(viewKey) {
const keys = Object.keys($views).sort(sortTabKeys);
const oldIndex = keys.indexOf(viewKey);
const newKey = keys[oldIndex - 1];
activeViewKey = newKey;
delete $views[viewKey];
$views = $views;
}
function addColumn(before) {
if (typeof before === 'number') {
$views[activeViewKey].columns = [
...$views[activeViewKey].columns.slice(0, before),
{ showInTable: true, inputType: 'none' },
...$views[activeViewKey].columns.slice(before),
];
}
else {
$views[activeViewKey].columns = [
...$views[activeViewKey].columns,
{ showInTable: true, inputType: 'none' },
];
}
}
function addSuggestedColumns() {
if ((typeof firstItem !== 'object') || (firstItem === null)) {
return;
}
$views[activeViewKey].columns = Object.keys(firstItem).sort().map(key => ({
key,
showInTable: true,
inputType: 'none',
}));
}
function moveColumn(oldIndex, delta) {
const column = $views[activeViewKey].columns[oldIndex];
const newIndex = oldIndex + delta;
$views[activeViewKey].columns.splice(oldIndex, 1);
$views[activeViewKey].columns.splice(newIndex, 0, column);
$views[activeViewKey].columns = $views[activeViewKey].columns;
}
function removeColumn(index) {
$views[activeViewKey].columns.splice(index, 1);
$views[activeViewKey].columns = $views[activeViewKey].columns;
}
</script>
<Modal title="View configuration" bind:show contentPadding={false}>
<TabBar
{tabs}
canAddTab={true}
on:addTab={createView}
on:closeTab={e => removeView(e.detail)}
bind:selectedKey={activeViewKey}
/>
<div class="options">
{#if $views[activeViewKey]}
<div class="meta">
{#key activeViewKey}
<label class="field">
<span class="label">View name</span>
<input type="text" use:input={{ autofocus: true }} bind:value={$views[activeViewKey].name} disabled={activeViewKey === 'list'} />
</label>
{/key}
<label class="field">
<span class="label">View type</span>
<select bind:value={$views[activeViewKey].type} disabled>
<option value="list">List view</option>
<option value="table">Table view</option>
</select>
</label>
</div>
{#if $views[activeViewKey].type === 'list'}
<div class="flex">
<input type="checkbox" id="hideObjectIndicators" bind:checked={$views[activeViewKey].hideObjectIndicators} />
<label for="hideObjectIndicators">
Hide object indicators ({'{...}'} and [...]) in list view and show nothing instead
</label>
</div>
{:else if $views[activeViewKey].type === 'table'}
<div class="columns">
{#each $views[activeViewKey].columns as column, columnIndex}
<div class="column">
<label class="field">
<input type="text" use:input bind:value={column.key} placeholder="Column keypath" />
</label>
<label class="field" title="Show column in table view">
<span class="label">
<Icon name="table" />
</span>
<span class="checkbox">
<input type="checkbox" bind:checked={column.showInTable} />
</span>
</label>
<label class="field" title="Input type in form view">
<span class="label">
<Icon name="form" />
</span>
<select bind:value={column.inputType}>
<option value="none">Hidden in form</option>
<optgroup label="Strings">
<option value="string">String</option>
<option value="objectid">ObjectID</option>
</optgroup>
<optgroup label="Integers">
<option value="int">Integer (32-bit, signed)</option>
<option value="uint64">Integer (64-bit, unsigned)</option>
<option value="long">Long (64-bit integer, signed)</option>
</optgroup>
<optgroup label="Floats">
<option value="double">Double (64-bit)</option>
<option value="decimal">Decimal (128-bit)</option>
</optgroup>
<optgroup label="Miscellaneous">
<option value="bool">Boolean</option>
<option value="date">Date</option>
</optgroup>
</select>
</label>
<label class="field" title="Mandatory (field must be valid in order to submit form)">
<span class="label">
<Icon name="target" />
</span>
<span class="checkbox">
<input type="checkbox" bind:checked={column.mandatory} disabled={column.inputType === 'none'} />
</span>
</label>
<button class="btn" type="button" on:click={() => addColumn(columnIndex)} title="Add column before this one">
<Icon name="+" />
</button>
<button class="btn" type="button" on:click={() => moveColumn(columnIndex, -1)} disabled={columnIndex === 0} title="Move column one position up">
<Icon name="chev-u" />
</button>
<button class="btn" type="button" on:click={() => moveColumn(columnIndex, 1)} disabled={columnIndex === $views[activeViewKey].columns.length - 1} title="Move column one position down">
<Icon name="chev-d" />
</button>
<button class="btn danger" type="button" on:click={() => removeColumn(columnIndex)} title="Remove this column">
<Icon name="x" />
</button>
</div>
{:else}
<p>No columns yet</p>
{/each}
</div>
<button class="btn" on:click={addColumn}>
<Icon name="+" /> Add column
</button>
<button class="btn" on:click={addSuggestedColumns} disabled={!Object.keys(firstItem || {}).length}>
<Icon name="zap" /> Add suggested columns
</button>
{/if}
{/if}
</div>
</Modal>
<style>
.options {
padding: 1rem;
}
.meta {
display: grid;
grid-template: 1fr / 1fr 1fr;
gap: 0.5rem;
margin-bottom: 1rem;
}
.flex {
display: flex;
gap: 0.5rem;
align-items: center;
}
.columns {
border: 1px solid #ccc;
overflow: auto;
padding: 0.5rem 0.5rem 0;
margin-bottom: 0.5rem;
}
.columns p {
margin-bottom: 0.5rem;
}
.columns .column {
display: grid;
grid-template: 1fr / 1fr repeat(7, auto);
gap: 0.5rem;
margin-bottom: 0.5rem;
}
</style>