|
1 | 1 | <script lang="ts" module>
|
2 |
| - import DOMPurify from "dompurify"; |
| 2 | + import DOMPurify from "dompurify"; |
3 | 3 | import type {SectionConfig} from "../../interfaces.ts";
|
4 | 4 |
|
5 |
| - export function getDefaultSectionConfig(): SectionConfig{ |
| 5 | + export function getDefaultSectionConfig(): SectionConfig { |
6 | 6 | return {
|
7 | 7 | title: "New section",
|
8 | 8 | description: "Section description",
|
|
30 | 30 | editable = false,
|
31 | 31 | viewerMode = false,
|
32 | 32 | sectionTypeEditable = true,
|
33 |
| - displaySectionType=true, |
| 33 | + sectionEditable = true, |
| 34 | + displaySectionType = true, |
34 | 35 | sectionIndex = -1,
|
35 | 36 | onMoveRequest = null,
|
36 |
| - onDeleteSectionRequest = ()=>{}, |
| 37 | + onDeleteSectionRequest = () => { |
| 38 | + }, |
37 | 39 | } = $props();
|
38 | 40 |
|
39 | 41 | //Insert missing keys
|
40 | 42 | let defultConfig = getDefaultSectionConfig()
|
41 |
| - for(let key in defultConfig){ |
42 |
| - if(!(key in config)){ |
| 43 | + for (let key in defultConfig) { |
| 44 | + if (!(key in config)) { |
43 | 45 | config[key] = defultConfig[key];
|
44 | 46 | }
|
45 | 47 | }
|
|
49 | 51 | let fieldComponents = $derived(_fieldComponents.filter(Boolean));
|
50 | 52 |
|
51 | 53 |
|
52 |
| - let fieldValues = $state(value !== null && value !== undefined ? value :[]); |
53 |
| - $effect(()=>{ |
54 |
| - value = _.cloneDeep(fieldValues); |
| 54 | + let fieldValues = $state(value !== null && value !== undefined ? value : []); |
| 55 | + $effect(() => { |
| 56 | + value = _.cloneDeep(fieldValues); |
55 | 57 | });
|
56 | 58 |
|
57 | 59 | let textVal = $state();
|
|
61 | 63 | console.log("Validating section");
|
62 | 64 | let sectionValid = true;
|
63 | 65 | for (let i = 0; i < fieldComponents.length; i++) {
|
64 |
| - if(!fieldComponents[i].validate()){ |
| 66 | + if (!fieldComponents[i].validate()) { |
65 | 67 | sectionValid = false;
|
66 | 68 | }
|
67 | 69 | }
|
68 | 70 |
|
69 | 71 | return sectionValid;
|
70 | 72 | }
|
71 | 73 |
|
72 |
| - export function getValue(){ |
| 74 | + export function getValue() { |
73 | 75 | let fieldValues = []
|
74 | 76 | for (let i = 0; i < fieldComponents.length; i++) {
|
75 | 77 | fieldValues.push(fieldComponents[i].getValue());
|
76 | 78 | }
|
77 |
| - return { name: config.title, fields: fieldValues}; |
| 79 | + return {name: config.title, fields: fieldValues}; |
78 | 80 | }
|
79 | 81 |
|
80 | 82 |
|
|
91 | 93 | }
|
92 | 94 |
|
93 | 95 |
|
94 |
| - function handleMoveRequest(srcSectionIndex, srcFieldIndex, destSectionIndex, destFieldIndex){ |
95 |
| - if(onMoveRequest){ |
| 96 | + function handleMoveRequest(srcSectionIndex, srcFieldIndex, destSectionIndex, destFieldIndex) { |
| 97 | + if (onMoveRequest) { |
96 | 98 | onMoveRequest(srcSectionIndex, srcFieldIndex, destSectionIndex, destFieldIndex);
|
97 | 99 | }
|
98 | 100 | }
|
|
116 | 118 | <div style="display: flex; justify-content: space-between; gap: 1em">
|
117 | 119 | <div style="flex: 1">
|
118 | 120 | <h2>
|
119 |
| - {#if editable} |
120 |
| - <EditableText bind:value={config.title}/> |
121 |
| - {:else} |
122 |
| - {config.title} |
123 |
| - {/if} |
| 121 | + {#if editable} |
| 122 | + <EditableText bind:value={config.title}/> |
| 123 | + {:else} |
| 124 | + {config.title} |
| 125 | + {/if} |
124 | 126 | </h2>
|
125 | 127 | </div>
|
126 | 128 | <div class="text-end">
|
|
154 | 156 | </div>
|
155 | 157 |
|
156 | 158 |
|
157 |
| - {#each config.fields, index (index)} |
| 159 | + {#each config.fields, index(index)} |
158 | 160 | <div class="mb-3">
|
159 | 161 | <InputComponent
|
160 | 162 | bind:config={config.fields[index]}
|
|
172 | 174 | {/each}
|
173 | 175 |
|
174 | 176 | {#if editable}
|
175 |
| - |
176 | 177 | <div class="d-flex justify-content-between">
|
177 | 178 | <button class="btn btn-primary" onclick={addField}><i class="bx bx-plus"></i> Add field</button>
|
178 | 179 | <div></div>
|
179 |
| - <button class="btn btn-danger" onclick={() => {onDeleteSectionRequest()}}><i class="bx bxs-trash"></i> Delete section</button> |
| 180 | + {#if sectionEditable} |
| 181 | + <button class="btn btn-danger" onclick={() => {onDeleteSectionRequest()}}><i |
| 182 | + class="bx bxs-trash"></i> Delete section |
| 183 | + </button> |
| 184 | + {/if} |
180 | 185 | </div>
|
181 |
| - |
182 | 186 | {/if}
|
183 | 187 | </div>
|
184 | 188 | </div>
|
|
0 commit comments