|
23 | 23 | } |
24 | 24 | // Create mandatory fields in case of minItems |
25 | 25 | if (schemaProperty.isRequired()) { |
26 | | - const minItems = getMinItems(schemaProperty.referenceSchema); |
| 26 | + const minItems = getMinItems(); |
27 | 27 | if (minItems !== null) { |
28 | 28 | for (let i = count; i < minItems; i++) { |
29 | 29 | addNestedProperty(undefined, false); |
|
34 | 34 | }); |
35 | 35 |
|
36 | 36 | /** |
37 | | - * @param {object} referenceSchema |
38 | 37 | * @returns {number|null} |
39 | 38 | */ |
40 | | - function getMinItems(referenceSchema) { |
41 | | - if ('minItems' in referenceSchema) { |
42 | | - return /** @type {number} */ (referenceSchema.minItems); |
| 39 | + function getMinItems() { |
| 40 | + if ('minItems' in schemaProperty) { |
| 41 | + return /** @type {number} */ (schemaProperty.minItems); |
43 | 42 | } |
44 | 43 | return null; |
45 | 44 | } |
46 | 45 |
|
47 | | - /** |
48 | | - * @param {object} referenceSchema |
49 | | - * @returns {number|null} |
50 | | - */ |
51 | | - function getMaxItems(referenceSchema) { |
52 | | - if ('maxItems' in referenceSchema) { |
53 | | - return /** @type {number} */ (referenceSchema.maxItems); |
| 46 | + function getMaxItems() { |
| 47 | + if ('maxItems' in schemaProperty) { |
| 48 | + return /** @type {number} */ (schemaProperty.maxItems); |
54 | 49 | } |
55 | 50 | return null; |
56 | 51 | } |
|
97 | 92 | * @param {any[]} nestedProperties |
98 | 93 | */ |
99 | 94 | function canAddMoreItems(nestedProperties) { |
100 | | - const maxItems = getMaxItems(schemaProperty.referenceSchema); |
| 95 | + const maxItems = getMaxItems(); |
101 | 96 | return maxItems === null || nestedProperties.length < maxItems; |
102 | 97 | } |
103 | 98 |
|
104 | 99 | /** |
105 | | - * @param {import('$lib/components/common/jschema/schema_management').SchemaProperty} schemaProperty |
106 | 100 | * @returns {boolean} true if has fixed length (minItems === maxItems), false otherwise |
107 | 101 | */ |
108 | | - function isTuple(schemaProperty) { |
109 | | - const minItems = getMinItems(schemaProperty.referenceSchema); |
110 | | - const maxItems = getMaxItems(schemaProperty.referenceSchema); |
| 102 | + function isTuple() { |
| 103 | + const minItems = getMinItems(); |
| 104 | + const maxItems = getMaxItems(); |
111 | 105 | return minItems !== null && maxItems !== null && minItems === maxItems; |
112 | 106 | } |
113 | 107 |
|
|
116 | 110 | * @returns {boolean} true if the nested properties can be removed, false otherwise |
117 | 111 | */ |
118 | 112 | function showRemoveButton(schemaProperty) { |
119 | | - if (isTuple(schemaProperty)) { |
| 113 | + if (isTuple()) { |
120 | 114 | return false; |
121 | 115 | } |
122 | 116 | if (!schemaProperty.isRequired()) { |
123 | 117 | return true; |
124 | 118 | } |
125 | | - const minItems = getMinItems(schemaProperty.referenceSchema); |
| 119 | + const minItems = getMinItems(); |
126 | 120 | if (minItems === null) { |
127 | 121 | return true; |
128 | 122 | } |
|
132 | 126 | $: addNestedPropertyBtnDisabled = !canAddMoreItems(nestedProperties); |
133 | 127 |
|
134 | 128 | function addTuple() { |
135 | | - const minItems = /** @type {number} */ (getMinItems(schemaProperty.referenceSchema)); |
| 129 | + const minItems = /** @type {number} */ (getMinItems()); |
136 | 130 | if ( |
137 | 131 | !Array.isArray(schemaProperty.defaultValue) || |
138 | 132 | schemaProperty.defaultValue.length !== minItems |
|
180 | 174 | > |
181 | 175 | <div class="accordion-body p-1"> |
182 | 176 | <div class="d-flex justify-content-center p-2"> |
183 | | - {#if !isTuple(schemaProperty)} |
| 177 | + {#if !isTuple()} |
184 | 178 | <button |
185 | 179 | class="btn btn-primary" |
186 | 180 | type="button" |
|
229 | 223 | <PropertyDiscriminator schemaProperty={nestedProperty} /> |
230 | 224 | </div> |
231 | 225 | <div class="align-self-right mt-2 me-2"> |
232 | | - {#if nestedProperties.length > 1 && !isTuple(schemaProperty)} |
| 226 | + {#if nestedProperties.length > 1 && !isTuple()} |
233 | 227 | <button |
234 | 228 | class="btn btn-light" |
235 | 229 | on:click|preventDefault={() => moveUp(index)} |
|
0 commit comments