1
0
mirror of https://github.com/garraflavatra/rolens.git synced 2025-04-19 08:51:03 +00:00

177 lines
4.6 KiB
Svelte
Raw Normal View History

2023-01-10 17:28:27 +01:00
<script>
2023-01-17 09:47:07 +01:00
import { FindItems } 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';
import CodeViewer from '../../../components/codeviewer.svelte';
2023-01-10 17:28:27 +01:00
export let collection;
const defaults = {
query: '{}',
sort: '{ "_id": 1 }',
fields: '{}',
skip: 0,
2023-01-14 20:38:39 +01:00
limit: 15,
2023-01-10 17:28:27 +01:00
};
2023-01-14 20:38:39 +01:00
let form = { ...defaults };
2023-01-11 20:41:15 +01:00
let result = {};
let submittedForm = {};
2023-01-10 17:28:27 +01:00
let queryField;
2023-01-18 16:31:13 +01:00
let activePath = '';
2023-01-15 12:02:17 +01:00
let json = '';
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})` : ''};`;
async function submitQuery() {
2023-01-18 16:31:13 +01:00
activePath = [];
2023-01-17 09:47:07 +01:00
result = await FindItems(collection.hostKey, collection.dbKey, collection.key, JSON.stringify(form));
2023-01-11 20:41:15 +01:00
if (result) {
submittedForm = JSON.parse(JSON.stringify(form));
}
resetFocus();
2023-01-10 17:28:27 +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();
}
function remove() {
2023-01-18 16:31:13 +01:00
// todo
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);
if (!item) {
return;
}
2023-01-15 12:02:17 +01:00
json = JSON.stringify(item, undefined, 2);
}
2023-01-13 16:56:48 +01:00
export function performQuery(q) {
form = { ...defaults, ...q };
submitQuery();
}
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>
<input type="number" min="0" bind:value={form.skip} use:input placeholder={defaults.skip} />
</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>
<input type="number" min="0" bind:value={form.limit} use:input placeholder={defaults.limit} />
</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-18 16:31:13 +01:00
<ObjectGrid data={result.results} bind:activePath on:trigger={e => openJson(e.detail?.itemKey)} />
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-18 16:31:13 +01:00
<button class="btn danger" on:click={remove} disabled={!activePath?.length}>
2023-01-11 20:41:15 +01:00
<Icon name="-" />
</button>
<button class="btn" on:click={prev} disabled={!submittedForm.limit || (submittedForm.skip <= 0) || !result?.results?.length}>
<Icon name="chev-l" />
</button>
<button class="btn" on:click={next} disabled={!submittedForm.limit || ((result?.results?.length || 0) < submittedForm.limit) || !result?.results?.length}>
<Icon name="chev-r" />
</button>
</div>
2023-01-10 20:10:39 +01:00
</div>
</div>
</div>
2023-01-10 17:28:27 +01:00
2023-01-15 12:09:27 +01:00
<CodeViewer bind:code={json} language="json" />
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-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>