2020-08-22 04:05:26 +12:00
|
|
|
<script>
|
|
|
|
import {
|
|
|
|
Popover,
|
|
|
|
TextButton,
|
|
|
|
Button,
|
|
|
|
Icon,
|
|
|
|
Input,
|
|
|
|
Select,
|
|
|
|
} from "@budibase/bbui"
|
|
|
|
import { backendUiStore } from "builderStore"
|
|
|
|
import { notifier } from "builderStore/store/notifications"
|
2020-09-30 01:54:04 +13:00
|
|
|
import CreateEditRecord from "./CreateEditRecord.svelte"
|
2020-08-22 04:05:26 +12:00
|
|
|
|
|
|
|
const CONDITIONS = [
|
|
|
|
{
|
|
|
|
name: "Equals",
|
|
|
|
key: "EQUALS",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Less Than",
|
|
|
|
key: "LT",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Less Than Or Equal",
|
|
|
|
key: "LTE",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "More Than",
|
|
|
|
key: "MT",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "More Than Or Equal",
|
|
|
|
key: "MTE",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Contains",
|
|
|
|
key: "CONTAINS",
|
|
|
|
},
|
|
|
|
]
|
|
|
|
|
|
|
|
const CONJUNCTIONS = [
|
|
|
|
{
|
|
|
|
name: "Or",
|
|
|
|
key: "OR",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "And",
|
|
|
|
key: "AND",
|
|
|
|
},
|
|
|
|
]
|
|
|
|
|
|
|
|
export let view = {}
|
|
|
|
|
|
|
|
let anchor
|
|
|
|
let dropdown
|
|
|
|
|
|
|
|
$: viewModel = $backendUiStore.models.find(
|
|
|
|
({ _id }) => _id === $backendUiStore.selectedView.modelId
|
|
|
|
)
|
|
|
|
$: fields = viewModel && Object.keys(viewModel.schema)
|
|
|
|
|
|
|
|
function saveView() {
|
|
|
|
backendUiStore.actions.views.save(view)
|
|
|
|
notifier.success(`View ${view.name} saved.`)
|
|
|
|
dropdown.hide()
|
|
|
|
}
|
|
|
|
|
|
|
|
function removeFilter(idx) {
|
2020-08-26 02:39:49 +12:00
|
|
|
view.filters.splice(idx, 1)
|
|
|
|
view.filters = view.filters
|
2020-08-22 04:05:26 +12:00
|
|
|
}
|
|
|
|
|
|
|
|
function addFilter() {
|
2020-09-14 22:03:27 +12:00
|
|
|
view.filters.push({})
|
|
|
|
view.filters = view.filters
|
|
|
|
}
|
|
|
|
|
|
|
|
function isMultipleChoice(field) {
|
|
|
|
return (
|
|
|
|
viewModel.schema[field].constraints &&
|
|
|
|
viewModel.schema[field].constraints.inclusion &&
|
|
|
|
viewModel.schema[field].constraints.inclusion.length
|
|
|
|
)
|
2020-08-22 04:05:26 +12:00
|
|
|
}
|
|
|
|
</script>
|
|
|
|
|
|
|
|
<div bind:this={anchor}>
|
|
|
|
<TextButton
|
|
|
|
text
|
|
|
|
small
|
|
|
|
on:click={dropdown.show}
|
2020-08-26 02:39:49 +12:00
|
|
|
active={view.filters && view.filters.length}>
|
2020-08-22 04:05:26 +12:00
|
|
|
<Icon name="filter" />
|
|
|
|
Filter
|
|
|
|
</TextButton>
|
|
|
|
</div>
|
|
|
|
<Popover bind:this={dropdown} {anchor} align="left">
|
2020-09-26 00:11:57 +12:00
|
|
|
<div class="actions">
|
2020-09-25 22:35:32 +12:00
|
|
|
<h5>Filter</h5>
|
|
|
|
{#if view.filters.length}
|
|
|
|
<div class="input-group-row">
|
|
|
|
{#each view.filters as filter, idx}
|
|
|
|
{#if idx === 0}
|
|
|
|
<p>Where</p>
|
|
|
|
{:else}
|
|
|
|
<Select secondary thin bind:value={filter.conjunction}>
|
|
|
|
<option value="">Choose an option</option>
|
|
|
|
{#each CONJUNCTIONS as conjunction}
|
|
|
|
<option value={conjunction.key}>{conjunction.name}</option>
|
|
|
|
{/each}
|
|
|
|
</Select>
|
|
|
|
{/if}
|
|
|
|
<Select secondary thin bind:value={filter.key}>
|
|
|
|
<option value="">Choose an option</option>
|
|
|
|
{#each fields as field}
|
|
|
|
<option value={field}>{field}</option>
|
|
|
|
{/each}
|
|
|
|
</Select>
|
|
|
|
<Select secondary thin bind:value={filter.condition}>
|
|
|
|
<option value="">Choose an option</option>
|
|
|
|
{#each CONDITIONS as condition}
|
|
|
|
<option value={condition.key}>{condition.name}</option>
|
|
|
|
{/each}
|
|
|
|
</Select>
|
|
|
|
{#if filter.key && isMultipleChoice(filter.key)}
|
|
|
|
<Select secondary thin bind:value={filter.value}>
|
|
|
|
<option value="">Choose an option</option>
|
|
|
|
{#each viewModel.schema[filter.key].constraints.inclusion as option}
|
|
|
|
<option value={option}>{option}</option>
|
|
|
|
{/each}
|
|
|
|
</Select>
|
|
|
|
{:else}
|
2020-09-30 01:54:04 +13:00
|
|
|
<Input thin placeholder="Value" bind:value={filter.value} />
|
2020-09-25 22:35:32 +12:00
|
|
|
{/if}
|
|
|
|
<i class="ri-close-circle-fill" on:click={() => removeFilter(idx)} />
|
2020-08-22 04:05:26 +12:00
|
|
|
{/each}
|
2020-09-25 22:35:32 +12:00
|
|
|
</div>
|
|
|
|
{/if}
|
|
|
|
<div class="footer">
|
|
|
|
<Button text on:click={addFilter}>Add Filter</Button>
|
|
|
|
<div class="buttons">
|
|
|
|
<Button secondary on:click={dropdown.hide}>Cancel</Button>
|
|
|
|
<Button primary on:click={saveView}>Save</Button>
|
|
|
|
</div>
|
2020-08-22 04:05:26 +12:00
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</Popover>
|
|
|
|
|
|
|
|
<style>
|
2020-09-26 00:11:57 +12:00
|
|
|
.actions {
|
2020-09-25 22:35:32 +12:00
|
|
|
display: grid;
|
|
|
|
grid-gap: var(--spacing-xl);
|
|
|
|
}
|
|
|
|
|
2020-08-22 04:05:26 +12:00
|
|
|
h5 {
|
2020-09-25 22:35:32 +12:00
|
|
|
margin: 0;
|
2020-08-22 04:05:26 +12:00
|
|
|
font-weight: 500;
|
|
|
|
}
|
|
|
|
|
2020-09-25 22:35:32 +12:00
|
|
|
.footer {
|
2020-08-22 04:05:26 +12:00
|
|
|
display: flex;
|
|
|
|
justify-content: space-between;
|
|
|
|
align-items: center;
|
|
|
|
}
|
2020-09-25 22:35:32 +12:00
|
|
|
.buttons {
|
|
|
|
display: flex;
|
|
|
|
justify-content: flex-end;
|
|
|
|
gap: var(--spacing-m);
|
2020-08-22 04:05:26 +12:00
|
|
|
}
|
|
|
|
|
|
|
|
.ri-close-circle-fill {
|
|
|
|
cursor: pointer;
|
|
|
|
}
|
|
|
|
|
|
|
|
.input-group-row {
|
|
|
|
display: grid;
|
|
|
|
grid-template-columns: minmax(50px, auto) 1fr 1fr 1fr 15px;
|
|
|
|
gap: var(--spacing-s);
|
|
|
|
align-items: center;
|
|
|
|
}
|
|
|
|
|
|
|
|
p {
|
|
|
|
margin: 0;
|
|
|
|
font-size: var(--font-size-xs);
|
|
|
|
}
|
|
|
|
</style>
|