2023-01-10 17:28:27 +01:00
|
|
|
<script>
|
2023-01-23 20:47:43 +01:00
|
|
|
import { FindItems, RemoveItemById } from '../../../../wailsjs/go/app/App';
|
2023-01-16 20:03:56 +01:00
|
|
|
import CodeExample from '../../../components/code-example.svelte';
|
|
|
|
import { input } from '../../../actions';
|
|
|
|
import ObjectGrid from '../../../components/objectgrid.svelte';
|
|
|
|
import Icon from '../../../components/icon.svelte';
|
2023-01-19 08:57:25 +01:00
|
|
|
import ObjectViewer from '../../../components/objectviewer.svelte';
|
2023-01-19 20:57:22 +01:00
|
|
|
import FindViewConfigModal from './find-viewconfig.svelte';
|
|
|
|
import { onMount } from 'svelte';
|
2023-01-20 13:54:57 +01:00
|
|
|
import Grid from '../../../components/grid.svelte';
|
2023-01-23 20:47:43 +01:00
|
|
|
import { applicationSettings, views } from '../../../stores';
|
2023-01-24 20:55:53 +01:00
|
|
|
import { EJSON } from 'bson';
|
2023-01-10 17:28:27 +01:00
|
|
|
|
|
|
|
export let collection;
|
|
|
|
|
|
|
|
const defaults = {
|
|
|
|
query: '{}',
|
2023-01-20 15:35:16 +01:00
|
|
|
sort: $applicationSettings.defaultSort || '{ "_id": 1 }',
|
2023-01-10 17:28:27 +01:00
|
|
|
fields: '{}',
|
|
|
|
skip: 0,
|
2023-01-20 15:35:16 +01:00
|
|
|
limit: $applicationSettings.defaultLimit || 15,
|
2023-01-10 17:28:27 +01:00
|
|
|
};
|
|
|
|
|
2023-01-14 20:38:39 +01:00
|
|
|
let form = { ...defaults };
|
2023-01-23 20:47:43 +01:00
|
|
|
let activeViewKey = 'list';
|
2023-01-11 20:41:15 +01:00
|
|
|
let result = {};
|
|
|
|
let submittedForm = {};
|
2023-01-10 17:28:27 +01:00
|
|
|
let queryField;
|
2023-01-19 09:18:21 +01:00
|
|
|
let activePath = [];
|
2023-01-19 08:57:25 +01:00
|
|
|
let objectViewerData;
|
2023-01-19 20:57:22 +01:00
|
|
|
let viewConfigModalOpen = false;
|
2023-01-10 17:28:27 +01:00
|
|
|
$: code = `db.${collection.key}.find(${form.query || '{}'}${form.fields && form.fields !== '{}' ? `, ${form.fields}` : ''}).sort(${form.sort})${form.skip ? `.skip(${form.skip})` : ''}${form.limit ? `.limit(${form.limit})` : ''};`;
|
2023-01-20 13:54:57 +01:00
|
|
|
$: lastPage = (submittedForm.limit && result?.results?.length) ? Math.max(0, Math.ceil((result.total - submittedForm.limit) / submittedForm.limit)) : 0;
|
|
|
|
$: activePage = (submittedForm.limit && submittedForm.skip && result?.results?.length) ? submittedForm.skip / submittedForm.limit : 0;
|
2023-01-10 17:28:27 +01:00
|
|
|
|
2023-01-19 20:57:22 +01:00
|
|
|
$: collection && refresh();
|
2023-01-20 13:54:57 +01:00
|
|
|
|
|
|
|
async function submitQuery() {
|
|
|
|
activePath = [];
|
2023-01-24 20:55:53 +01:00
|
|
|
const newResult = await FindItems(collection.hostKey, collection.dbKey, collection.key, JSON.stringify(form));
|
|
|
|
if (newResult) {
|
|
|
|
newResult.results = newResult.results?.map(s => EJSON.parse(s, { relaxed: false }));
|
|
|
|
result = newResult;
|
2023-01-20 13:54:57 +01:00
|
|
|
submittedForm = JSON.parse(JSON.stringify(form));
|
|
|
|
}
|
|
|
|
resetFocus();
|
|
|
|
}
|
|
|
|
|
|
|
|
async function refresh() {
|
2023-01-20 15:35:16 +01:00
|
|
|
if ($applicationSettings.autosubmitQuery) {
|
|
|
|
await submitQuery();
|
|
|
|
}
|
2023-01-19 20:57:22 +01:00
|
|
|
}
|
|
|
|
|
2023-01-10 20:22:57 +01:00
|
|
|
function prev() {
|
|
|
|
form.skip -= form.limit;
|
|
|
|
if (form.skip < 0) {
|
|
|
|
form.skip = 0;
|
|
|
|
}
|
|
|
|
submitQuery();
|
|
|
|
}
|
|
|
|
|
|
|
|
function next() {
|
|
|
|
form.skip += form.limit;
|
|
|
|
submitQuery();
|
|
|
|
}
|
|
|
|
|
2023-01-20 13:54:57 +01:00
|
|
|
function first() {
|
|
|
|
form.skip = 0;
|
|
|
|
submitQuery();
|
|
|
|
}
|
|
|
|
|
|
|
|
function last() {
|
|
|
|
form.skip = lastPage * submittedForm.limit;
|
|
|
|
submitQuery();
|
|
|
|
}
|
|
|
|
|
2023-01-19 09:18:21 +01:00
|
|
|
async function removeActive() {
|
|
|
|
if (!activePath[0]) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
const ok = await RemoveItemById(collection.hostKey, collection.dbKey, collection.key, activePath[0]);
|
|
|
|
if (ok) {
|
|
|
|
await submitQuery();
|
|
|
|
}
|
2023-01-10 20:22:57 +01:00
|
|
|
}
|
|
|
|
|
2023-01-11 20:41:15 +01:00
|
|
|
function resetFocus() {
|
2023-01-10 17:28:27 +01:00
|
|
|
queryField?.focus();
|
|
|
|
queryField?.select();
|
2023-01-11 20:41:15 +01:00
|
|
|
}
|
|
|
|
|
2023-01-15 12:02:17 +01:00
|
|
|
function openJson(itemId) {
|
2023-01-18 16:31:13 +01:00
|
|
|
const item = result?.results?.find(i => i._id == itemId);
|
2023-01-19 08:57:25 +01:00
|
|
|
objectViewerData = item;
|
2023-01-15 12:02:17 +01:00
|
|
|
}
|
|
|
|
|
2023-01-13 16:56:48 +01:00
|
|
|
export function performQuery(q) {
|
|
|
|
form = { ...defaults, ...q };
|
|
|
|
submitQuery();
|
|
|
|
}
|
2023-01-19 20:57:22 +01:00
|
|
|
|
|
|
|
onMount(refresh);
|
2023-01-10 17:28:27 +01:00
|
|
|
</script>
|
|
|
|
|
2023-01-11 20:41:15 +01:00
|
|
|
<div class="find">
|
|
|
|
<form on:submit|preventDefault={submitQuery}>
|
|
|
|
<div class="form-row one">
|
|
|
|
<label class="field">
|
|
|
|
<span class="label">Query or id</span>
|
2023-01-17 17:03:11 +01:00
|
|
|
<input type="text" class="code" bind:this={queryField} bind:value={form.query} use:input={{ json: true, autofocus: true }} placeholder={defaults.query} />
|
2023-01-11 20:41:15 +01:00
|
|
|
</label>
|
|
|
|
|
|
|
|
<label class="field">
|
|
|
|
<span class="label">Sort</span>
|
|
|
|
<input type="text" class="code" bind:value={form.sort} use:input={{ json: true }} placeholder={defaults.sort} />
|
|
|
|
</label>
|
|
|
|
</div>
|
2023-01-10 17:28:27 +01:00
|
|
|
|
2023-01-11 20:41:15 +01:00
|
|
|
<div class="form-row two">
|
|
|
|
<label class="field">
|
|
|
|
<span class="label">Fields</span>
|
|
|
|
<input type="text" class="code" bind:value={form.fields} use:input={{ json: true }} placeholder={defaults.fields} />
|
|
|
|
</label>
|
2023-01-10 17:28:27 +01:00
|
|
|
|
2023-01-11 20:41:15 +01:00
|
|
|
<label class="field">
|
|
|
|
<span class="label">Skip</span>
|
2023-01-20 13:54:57 +01:00
|
|
|
<input type="number" min="0" bind:value={form.skip} use:input placeholder={defaults.skip} list="skipstops" />
|
2023-01-11 20:41:15 +01:00
|
|
|
</label>
|
2023-01-10 17:28:27 +01:00
|
|
|
|
2023-01-11 20:41:15 +01:00
|
|
|
<label class="field">
|
|
|
|
<span class="label">Limit</span>
|
2023-01-20 13:54:57 +01:00
|
|
|
<input type="number" min="0" bind:value={form.limit} use:input placeholder={defaults.limit} list="limits" />
|
2023-01-11 20:41:15 +01:00
|
|
|
</label>
|
2023-01-10 17:28:27 +01:00
|
|
|
|
2023-01-11 20:41:15 +01:00
|
|
|
<button type="submit" class="btn">Run</button>
|
|
|
|
</div>
|
|
|
|
</form>
|
2023-01-10 17:28:27 +01:00
|
|
|
|
2023-01-11 20:41:15 +01:00
|
|
|
<CodeExample {code} />
|
2023-01-10 20:10:39 +01:00
|
|
|
|
2023-01-11 20:41:15 +01:00
|
|
|
<div class="result">
|
|
|
|
<div class="grid">
|
|
|
|
{#key result}
|
2023-01-23 20:47:43 +01:00
|
|
|
{#if activeViewKey === 'table'}
|
2023-01-20 13:54:57 +01:00
|
|
|
<Grid
|
|
|
|
key="_id"
|
2023-01-23 20:47:43 +01:00
|
|
|
columns={$views[activeViewKey]?.columns?.map(c => ({ key: c.key, title: c.key })) || []}
|
2023-01-20 13:54:57 +01:00
|
|
|
showHeaders={true}
|
|
|
|
items={result.results || []}
|
|
|
|
bind:activePath
|
|
|
|
on:trigger={e => openJson(e.detail?.itemKey)}
|
|
|
|
/>
|
2023-01-23 20:47:43 +01:00
|
|
|
{:else if activeViewKey === 'list'}
|
2023-01-20 13:54:57 +01:00
|
|
|
<ObjectGrid
|
|
|
|
data={result.results}
|
2023-01-23 20:47:43 +01:00
|
|
|
hideObjectIndicators={$views[activeViewKey]?.hideObjectIndicators}
|
2023-01-20 13:54:57 +01:00
|
|
|
bind:activePath
|
|
|
|
on:trigger={e => openJson(e.detail?.itemKey)}
|
|
|
|
/>
|
|
|
|
{/if}
|
2023-01-11 20:41:15 +01:00
|
|
|
{/key}
|
2023-01-10 20:10:39 +01:00
|
|
|
</div>
|
2023-01-11 20:41:15 +01:00
|
|
|
|
|
|
|
<div class="controls">
|
|
|
|
<div>
|
2023-01-13 16:56:48 +01:00
|
|
|
{#key result}
|
|
|
|
<span class="flash-green">Results: {result.total || 0}</span>
|
|
|
|
{/key}
|
2023-01-11 20:41:15 +01:00
|
|
|
</div>
|
|
|
|
<div>
|
2023-01-19 20:57:22 +01:00
|
|
|
<button class="btn" on:click={() => viewConfigModalOpen = true} title="Configure view">
|
|
|
|
<Icon name="cog" />
|
|
|
|
</button>
|
|
|
|
<button class="btn danger" on:click={removeActive} disabled={!activePath?.length} title="Drop selected item">
|
2023-01-11 20:41:15 +01:00
|
|
|
<Icon name="-" />
|
|
|
|
</button>
|
2023-01-20 13:54:57 +01:00
|
|
|
<button class="btn" on:click={first} disabled={!submittedForm.limit || (submittedForm.skip <= 0) || !result?.results || (activePage === 0)} title="First page">
|
|
|
|
<Icon name="chevs-l" />
|
|
|
|
</button>
|
|
|
|
<button class="btn" on:click={prev} disabled={!submittedForm.limit || (submittedForm.skip <= 0) || !result?.results || (activePage === 0)} title="Previous {submittedForm.limit} items">
|
2023-01-11 20:41:15 +01:00
|
|
|
<Icon name="chev-l" />
|
|
|
|
</button>
|
2023-01-20 13:54:57 +01:00
|
|
|
<button class="btn" on:click={next} disabled={!submittedForm.limit || ((result?.results?.length || 0) < submittedForm.limit) || !result?.results || !lastPage || (activePage >= lastPage)} title="Next {submittedForm.limit} items">
|
2023-01-11 20:41:15 +01:00
|
|
|
<Icon name="chev-r" />
|
|
|
|
</button>
|
2023-01-20 13:54:57 +01:00
|
|
|
<button class="btn" on:click={last} disabled={!submittedForm.limit || ((result?.results?.length || 0) < submittedForm.limit) || !result?.results || !lastPage || (activePage >= lastPage)} title="Last page">
|
|
|
|
<Icon name="chevs-r" />
|
|
|
|
</button>
|
2023-01-11 20:41:15 +01:00
|
|
|
</div>
|
2023-01-10 20:10:39 +01:00
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</div>
|
2023-01-10 17:28:27 +01:00
|
|
|
|
2023-01-19 08:57:25 +01:00
|
|
|
<ObjectViewer bind:data={objectViewerData} />
|
2023-01-23 20:47:43 +01:00
|
|
|
<FindViewConfigModal
|
|
|
|
bind:show={viewConfigModalOpen}
|
|
|
|
bind:activeViewKey
|
|
|
|
firstItem={result.results?.[0]}
|
|
|
|
{collection}
|
|
|
|
/>
|
2023-01-20 13:54:57 +01:00
|
|
|
|
|
|
|
<datalist id="limits">
|
|
|
|
{#each [ 1, 5, 10, 25, 50, 100, 200 ] as value}
|
|
|
|
<option {value} />
|
|
|
|
{/each}
|
|
|
|
</datalist>
|
|
|
|
|
|
|
|
{#if submittedForm?.limit}
|
|
|
|
<datalist id="skipstops">
|
|
|
|
{#each Array(lastPage).fill('').map((_, i) => i * submittedForm.limit) as value}
|
|
|
|
<option {value} />
|
|
|
|
{/each}
|
|
|
|
</datalist>
|
|
|
|
{/if}
|
2023-01-15 12:02:17 +01:00
|
|
|
|
2023-01-10 17:28:27 +01:00
|
|
|
<style>
|
2023-01-11 20:41:15 +01:00
|
|
|
.find {
|
2023-01-10 17:28:27 +01:00
|
|
|
display: grid;
|
|
|
|
gap: 0.5rem;
|
2023-01-11 20:41:15 +01:00
|
|
|
grid-template: auto auto 1fr / 1fr;
|
2023-01-10 17:28:27 +01:00
|
|
|
}
|
2023-01-11 20:41:15 +01:00
|
|
|
|
|
|
|
.form-row {
|
2023-01-10 17:28:27 +01:00
|
|
|
display: grid;
|
|
|
|
gap: 0.5rem;
|
2023-01-11 20:41:15 +01:00
|
|
|
}
|
|
|
|
.form-row.one {
|
|
|
|
grid-template: 1fr / 3fr 2fr;
|
2023-01-10 17:28:27 +01:00
|
|
|
margin-bottom: 0.5rem;
|
|
|
|
}
|
2023-01-11 20:41:15 +01:00
|
|
|
.form-row.two {
|
|
|
|
grid-template: 1fr / 5fr 1fr 1fr 1fr;
|
|
|
|
}
|
2023-01-10 20:10:39 +01:00
|
|
|
|
|
|
|
.result {
|
2023-01-11 20:41:15 +01:00
|
|
|
display: grid;
|
|
|
|
grid-template: 1fr auto / 1fr;
|
2023-01-10 20:10:39 +01:00
|
|
|
gap: 0.5rem;
|
2023-01-17 16:22:49 +01:00
|
|
|
overflow: auto;
|
|
|
|
min-height: 0;
|
|
|
|
min-width: 0;
|
2023-01-10 20:10:39 +01:00
|
|
|
}
|
2023-01-11 20:41:15 +01:00
|
|
|
.result > .grid {
|
|
|
|
overflow: auto;
|
2023-01-17 16:22:49 +01:00
|
|
|
min-height: 0;
|
|
|
|
min-width: 0;
|
|
|
|
border: 1px solid #ccc;
|
2023-01-23 13:41:43 +01:00
|
|
|
background-color: #fff;
|
2023-01-10 20:10:39 +01:00
|
|
|
}
|
|
|
|
.result > .controls {
|
|
|
|
display: flex;
|
|
|
|
justify-content: space-between;
|
|
|
|
align-items: center;
|
|
|
|
}
|
2023-01-10 17:28:27 +01:00
|
|
|
</style>
|