2023-03-17 01:21:16 +08:00
< script >
import CommonHelper from "@/utils/CommonHelper";
import tooltip from "@/actions/tooltip";
import Field from "@/components/base/Field.svelte";
import Select from "@/components/base/Select.svelte";
import ObjectSelect from "@/components/base/ObjectSelect.svelte";
import CollectionUpsertPanel from "@/components/collections/CollectionUpsertPanel.svelte";
import SchemaField from "@/components/collections/schema/SchemaField.svelte";
2023-08-09 17:58:20 +08:00
import { collections } from "@/stores/collections";
2023-03-17 01:21:16 +08:00
export let field;
export let key = "";
const isSingleOptions = [
{ label : "Single" , value : true } ,
{ label : "Multiple" , value : false } ,
];
const defaultOptions = [
{ label : "False" , value : false } ,
{ label : "True" , value : true } ,
];
let upsertPanel = null;
let isSingle = field.options?.maxSelect == 1;
let oldIsSingle = isSingle;
2023-07-27 20:57:20 +08:00
$: selectCollections = $collections.filter((c) => c.type != "view");
2023-03-17 01:21:16 +08:00
// load defaults
$: if (CommonHelper.isEmpty(field.options)) {
loadDefaults();
}
$: if (oldIsSingle != isSingle) {
oldIsSingle = isSingle;
if (isSingle) {
field.options.minSelect = null;
field.options.maxSelect = 1;
} else {
field.options.maxSelect = null;
}
}
$: selectedColection = $collections.find((c) => c.id == field.options.collectionId) || null;
function loadDefaults() {
field.options = {
maxSelect: 1,
collectionId: null,
cascadeDelete: false,
};
isSingle = true;
oldIsSingle = isSingle;
}
< / script >
2024-01-22 02:22:56 +08:00
< SchemaField bind:field { key } on:rename on:remove on:duplicate {... $$restProps } >
2023-03-17 01:21:16 +08:00
< svelte:fragment let:interactive >
2023-04-26 19:06:27 +08:00
< div class = "separator" / >
2023-03-17 01:21:16 +08:00
< Field
2023-04-26 19:06:27 +08:00
class="form-field required { ! interactive ? 'readonly' : '' } "
2023-03-17 01:21:16 +08:00
inlineError
name="schema.{ key } .options.collectionId"
let:uniqueId
>
< ObjectSelect
id={ uniqueId }
2023-07-27 20:57:20 +08:00
searchable={ selectCollections . length > 5 }
2023-03-17 01:21:16 +08:00
selectPlaceholder={ "Select collection *" }
noOptionsText="No collections found"
selectionKey="id"
2023-07-27 20:57:20 +08:00
items={ selectCollections }
2023-04-26 23:35:34 +08:00
readonly={ ! interactive || field . id }
2023-03-17 01:21:16 +08:00
bind:keyOfSelected={ field . options . collectionId }
>
< svelte:fragment slot = "afterOptions" >
< hr / >
< button
type="button"
class="btn btn-transparent btn-block btn-sm"
on:click={() => upsertPanel ? . show ()}
>
< i class = "ri-add-line" / >
< span class = "txt" > New collection< / span >
< / button >
< / svelte:fragment >
< / ObjectSelect >
< / Field >
2023-04-26 19:06:27 +08:00
< div class = "separator" / >
2023-03-17 01:21:16 +08:00
< Field
2023-04-26 23:35:34 +08:00
class="form-field form-field-single-multiple-select { ! interactive ? 'readonly' : '' } "
2023-03-17 01:21:16 +08:00
inlineError
let:uniqueId
>
< ObjectSelect
id={ uniqueId }
items={ isSingleOptions }
2023-04-26 23:35:34 +08:00
readonly={ ! interactive }
2023-03-17 01:21:16 +08:00
bind:keyOfSelected={ isSingle }
/>
< / Field >
2023-04-26 19:06:27 +08:00
< div class = "separator" / >
2023-03-17 01:21:16 +08:00
< / svelte:fragment >
< svelte:fragment slot = "options" >
< div class = "grid grid-sm" >
{ #if ! isSingle }
< div class = "col-sm-6" >
< Field class = "form-field" name = "schema. { key } .options.minSelect" let:uniqueId >
< label for = { uniqueId } > Min select </ label >
< input
type="number"
id={ uniqueId }
step="1"
min="1"
placeholder="No min limit"
bind:value={ field . options . minSelect }
/>
< / Field >
< / div >
< div class = "col-sm-6" >
< Field class = "form-field" name = "schema. { key } .options.maxSelect" let:uniqueId >
< label for = { uniqueId } > Max select </ label >
< input
type="number"
id={ uniqueId }
step="1"
placeholder="No max limit"
min={ field . options . minSelect || 2 }
bind:value={ field . options . maxSelect }
/>
< / Field >
< / div >
{ /if }
2023-08-21 23:06:35 +08:00
< div class = "col-sm-12" >
2023-03-17 01:21:16 +08:00
< Field class = "form-field" name = "schema. { key } .options.cascadeDelete" let:uniqueId >
2023-04-15 16:27:07 +08:00
< label for = { uniqueId } >
< span class = "txt" > Cascade delete< / span >
2023-08-09 17:58:20 +08:00
<!-- prettier - ignore -->
2023-04-15 16:27:07 +08:00
< i
class="ri-information-line link-hint"
use:tooltip={{
2023-08-09 17:58:20 +08:00
text: [
2023-08-21 23:06:35 +08:00
`Whether on ${ selectedColection ? . name || "relation" } record deletion to delete also the current corresponding collection record(s).`,
2023-08-09 17:58:20 +08:00
!isSingle ? `For "Multiple" relation fields the cascade delete is triggered only when all ${ selectedColection ? . name || "relation" } ids are removed from the corresponding record.` : null
].filter(Boolean).join("\n\n"),
2023-04-15 16:27:07 +08:00
position: "top",
}}
/>
< / label >
2023-03-17 01:21:16 +08:00
< ObjectSelect
id={ uniqueId }
items={ defaultOptions }
bind:keyOfSelected={ field . options . cascadeDelete }
/>
< / Field >
< / div >
< / div >
< / svelte:fragment >
< / SchemaField >
< CollectionUpsertPanel
bind:this={ upsertPanel }
on:save={( e ) => {
2023-07-27 20:57:20 +08:00
if (e?.detail?.collection?.id && e.detail.collection.type != "view") {
2023-03-17 01:21:16 +08:00
field.options.collectionId = e.detail.collection.id;
}
}}
/>