2022-10-30 16:28:14 +08:00
|
|
|
<script context="module">
|
|
|
|
let cachedRuleComponent;
|
|
|
|
</script>
|
|
|
|
|
|
|
|
<script>
|
|
|
|
import { tick } from "svelte";
|
|
|
|
import Field from "@/components/base/Field.svelte";
|
|
|
|
|
|
|
|
export let collection = null;
|
|
|
|
export let rule = null;
|
|
|
|
export let label = "Rule";
|
|
|
|
export let formKey = "rule";
|
|
|
|
export let required = false;
|
|
|
|
|
|
|
|
let editorRef = null;
|
|
|
|
let tempValue = null;
|
|
|
|
let ruleInputComponent = cachedRuleComponent;
|
|
|
|
let isRuleComponentLoading = false;
|
|
|
|
|
|
|
|
$: isAdminOnly = rule === null;
|
|
|
|
|
2023-01-08 04:25:56 +08:00
|
|
|
loadEditorComponent();
|
|
|
|
|
2022-10-30 16:28:14 +08:00
|
|
|
async function loadEditorComponent() {
|
|
|
|
if (ruleInputComponent || isRuleComponentLoading) {
|
|
|
|
return; // already loaded or in the process
|
|
|
|
}
|
|
|
|
|
|
|
|
isRuleComponentLoading = true;
|
|
|
|
|
|
|
|
ruleInputComponent = (await import("@/components/base/FilterAutocompleteInput.svelte")).default;
|
|
|
|
|
|
|
|
cachedRuleComponent = ruleInputComponent;
|
|
|
|
|
|
|
|
isRuleComponentLoading = false;
|
|
|
|
}
|
|
|
|
|
2023-01-08 04:25:56 +08:00
|
|
|
async function unlock() {
|
|
|
|
rule = tempValue || "";
|
|
|
|
await tick();
|
|
|
|
editorRef?.focus();
|
|
|
|
}
|
|
|
|
|
|
|
|
async function lock() {
|
|
|
|
tempValue = rule;
|
|
|
|
rule = null;
|
|
|
|
}
|
2022-10-30 16:28:14 +08:00
|
|
|
</script>
|
|
|
|
|
|
|
|
{#if isRuleComponentLoading}
|
|
|
|
<div class="txt-center">
|
|
|
|
<span class="loader" />
|
|
|
|
</div>
|
|
|
|
{:else}
|
2023-01-08 04:25:56 +08:00
|
|
|
<Field
|
|
|
|
class="form-field rule-field m-0 {required ? 'requied' : ''} {isAdminOnly ? 'disabled' : ''}"
|
|
|
|
name={formKey}
|
|
|
|
let:uniqueId
|
|
|
|
>
|
|
|
|
<label for={uniqueId}>
|
2023-02-22 04:24:49 +08:00
|
|
|
<span class="txt" class:txt-hint={isAdminOnly}>
|
|
|
|
{label}
|
|
|
|
</span>
|
|
|
|
<span class="label label-sm" class:label-danger={!isAdminOnly}>
|
|
|
|
{isAdminOnly ? "Admins only" : "Custom rule"}
|
2023-01-08 04:25:56 +08:00
|
|
|
</span>
|
|
|
|
|
|
|
|
{#if isAdminOnly}
|
2023-01-08 17:11:02 +08:00
|
|
|
<button
|
|
|
|
type="button"
|
2023-01-24 03:57:35 +08:00
|
|
|
class="btn btn-sm btn-transparent btn-success lock-toggle"
|
2023-01-08 17:11:02 +08:00
|
|
|
on:click={unlock}
|
|
|
|
>
|
2023-01-08 04:25:56 +08:00
|
|
|
<i class="ri-lock-unlock-line" />
|
2023-02-22 04:24:49 +08:00
|
|
|
<span class="txt">Enable custom rule</span>
|
2023-01-08 04:25:56 +08:00
|
|
|
</button>
|
|
|
|
{:else}
|
2023-01-24 03:57:35 +08:00
|
|
|
<button
|
|
|
|
type="button"
|
|
|
|
class="btn btn-sm btn-transparent btn-hint lock-toggle"
|
|
|
|
on:click={lock}
|
|
|
|
>
|
2023-01-08 04:25:56 +08:00
|
|
|
<i class="ri-lock-line" />
|
|
|
|
<span class="txt">Set Admins only</span>
|
|
|
|
</button>
|
|
|
|
{/if}
|
|
|
|
</label>
|
|
|
|
|
|
|
|
<svelte:component
|
|
|
|
this={ruleInputComponent}
|
|
|
|
id={uniqueId}
|
|
|
|
bind:this={editorRef}
|
|
|
|
bind:value={rule}
|
|
|
|
baseCollection={collection}
|
|
|
|
disabled={isAdminOnly}
|
|
|
|
/>
|
|
|
|
|
|
|
|
<div class="help-block">
|
|
|
|
<slot {isAdminOnly}>
|
|
|
|
<p>
|
|
|
|
{#if isAdminOnly}
|
|
|
|
Only admins will be able to perform this action (
|
2023-02-22 04:24:49 +08:00
|
|
|
<button type="button" class="link-primary" on:click={unlock}>unlock to change</button>
|
2023-01-08 04:25:56 +08:00
|
|
|
).
|
|
|
|
{:else}
|
|
|
|
Leave empty to grant everyone access.
|
|
|
|
{/if}
|
|
|
|
</p>
|
|
|
|
</slot>
|
|
|
|
</div>
|
|
|
|
</Field>
|
2022-10-30 16:28:14 +08:00
|
|
|
{/if}
|
|
|
|
|
|
|
|
<style>
|
2023-02-22 04:24:49 +08:00
|
|
|
label .label {
|
|
|
|
margin: -5px 0;
|
|
|
|
}
|
2023-01-08 04:25:56 +08:00
|
|
|
.lock-toggle {
|
2023-01-08 17:11:02 +08:00
|
|
|
position: absolute;
|
|
|
|
right: 0px;
|
|
|
|
top: 0px;
|
|
|
|
min-width: 135px;
|
2023-02-19 01:33:42 +08:00
|
|
|
padding: 10px;
|
2023-01-08 17:11:02 +08:00
|
|
|
border-top-left-radius: 0;
|
|
|
|
border-bottom-right-radius: 0;
|
2023-02-19 01:33:42 +08:00
|
|
|
background: rgba(53, 71, 104, 0.08);
|
2022-10-30 16:28:14 +08:00
|
|
|
}
|
|
|
|
</style>
|