Spaces:
Paused
Paused
File size: 14,649 Bytes
55bd140 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 |
<script lang="ts">
import { getContext, onMount, tick } from 'svelte';
import { models, config } from '$lib/stores';
import { toast } from 'svelte-sonner';
import { copyToClipboard } from '$lib/utils';
import XMark from '$lib/components/icons/XMark.svelte';
import Modal from '$lib/components/common/Modal.svelte';
import Spinner from '$lib/components/common/Spinner.svelte';
import MapSelector from '$lib/components/common/Valves/MapSelector.svelte';
const i18n = getContext('i18n');
export let show = false;
export let variables = {};
export let onSave = (e) => {};
let loading = false;
let variableValues = {};
const submitHandler = async () => {
onSave(variableValues);
show = false;
};
const init = async () => {
loading = true;
variableValues = {};
for (const variable of Object.keys(variables)) {
if (variables[variable]?.default !== undefined) {
variableValues[variable] = variables[variable].default;
} else {
variableValues[variable] = '';
}
}
loading = false;
await tick();
const firstInputElement = document.getElementById('input-variable-0');
if (firstInputElement) {
console.log('Focusing first input element:', firstInputElement);
firstInputElement.focus();
}
};
$: if (show) {
init();
}
</script>
<Modal bind:show size="md">
<div>
<div class=" flex justify-between dark:text-gray-300 px-5 pt-4 pb-2">
<div class=" text-lg font-medium self-center">{$i18n.t('Input Variables')}</div>
<button
class="self-center"
on:click={() => {
show = false;
}}
>
<XMark className={'size-5'} />
</button>
</div>
<div class="flex flex-col md:flex-row w-full px-5 pb-4 md:space-x-4 dark:text-gray-200">
<div class=" flex flex-col w-full sm:flex-row sm:justify-center sm:space-x-6">
<form
class="flex flex-col w-full"
on:submit|preventDefault={() => {
submitHandler();
}}
>
<div class="px-1">
{#if !loading}
<div class="flex flex-col gap-1">
{#each Object.keys(variables) as variable, idx}
{@const { type, ...variableAttributes } = variables[variable] ?? {}}
<div class=" py-0.5 w-full justify-between">
<div class="flex w-full justify-between mb-1.5">
<div class=" self-center text-xs font-medium">
{variable}
{#if variables[variable]?.required ?? false}
<span class=" text-gray-500">*{$i18n.t('required')}</span>
{/if}
</div>
</div>
<div class="flex mt-0.5 mb-0.5 space-x-2">
<div class=" flex-1">
{#if variables[variable]?.type === 'select'}
{@const options = variableAttributes?.options ?? []}
{@const placeholder = variableAttributes?.placeholder ?? ''}
<select
class="w-full rounded-lg py-2 px-4 text-sm dark:text-gray-300 dark:bg-gray-850 outline-hidden border border-gray-100 dark:border-gray-850"
bind:value={variableValues[variable]}
id="input-variable-{idx}"
>
{#if placeholder}
<option value="" disabled selected>
{placeholder}
</option>
{/if}
{#each options as option}
<option value={option} selected={option === variableValues[variable]}>
{option}
</option>
{/each}
</select>
{:else if variables[variable]?.type === 'checkbox'}
<div class="flex items-center space-x-2">
<div class="relative flex justify-center items-center gap-2">
<input
type="checkbox"
bind:checked={variableValues[variable]}
class="size-3.5 rounded cursor-pointer border border-gray-200 dark:border-gray-700"
id="input-variable-{idx}"
{...variableAttributes}
/>
<label for="input-variable-{idx}" class="text-sm"
>{variables[variable]?.label ?? variable}</label
>
</div>
<input
type="text"
class="flex-1 py-1 text-sm dark:text-gray-300 bg-transparent outline-hidden"
placeholder={$i18n.t('Enter value (true/false)')}
bind:value={variableValues[variable]}
autocomplete="off"
required={variables[variable]?.required ?? false}
/>
</div>
{:else if variables[variable]?.type === 'color'}
<div class="flex items-center space-x-2">
<div class="relative size-6">
<input
type="color"
class="size-6 rounded cursor-pointer border border-gray-200 dark:border-gray-700"
value={variableValues[variable]}
id="input-variable-{idx}"
on:input={(e) => {
// Convert the color value to uppercase immediately
variableValues[variable] = e.target.value.toUpperCase();
}}
{...variableAttributes}
/>
</div>
<input
type="text"
class="flex-1 py-2 text-sm dark:text-gray-300 bg-transparent outline-hidden"
placeholder={$i18n.t('Enter hex color (e.g. #FF0000)')}
bind:value={variableValues[variable]}
autocomplete="off"
required={variables[variable]?.required ?? false}
/>
</div>
{:else if variables[variable]?.type === 'date'}
<input
type="date"
class="w-full rounded-lg py-2 px-4 text-sm dark:text-gray-300 dark:bg-gray-850 outline-hidden border border-gray-100 dark:border-gray-850"
placeholder={variables[variable]?.placeholder ?? ''}
bind:value={variableValues[variable]}
autocomplete="off"
id="input-variable-{idx}"
required={variables[variable]?.required ?? false}
{...variableAttributes}
/>
{:else if variables[variable]?.type === 'datetime-local'}
<input
type="datetime-local"
class="w-full rounded-lg py-2 px-4 text-sm dark:text-gray-300 dark:bg-gray-850 outline-hidden border border-gray-100 dark:border-gray-850"
placeholder={variables[variable]?.placeholder ?? ''}
bind:value={variableValues[variable]}
autocomplete="off"
id="input-variable-{idx}"
required={variables[variable]?.required ?? false}
{...variableAttributes}
/>
{:else if variables[variable]?.type === 'email'}
<input
type="email"
class="w-full rounded-lg py-2 px-4 text-sm dark:text-gray-300 dark:bg-gray-850 outline-hidden border border-gray-100 dark:border-gray-850"
placeholder={variables[variable]?.placeholder ?? ''}
bind:value={variableValues[variable]}
autocomplete="off"
id="input-variable-{idx}"
required={variables[variable]?.required ?? false}
{...variableAttributes}
/>
{:else if variables[variable]?.type === 'month'}
<input
type="month"
class="w-full rounded-lg py-2 px-4 text-sm dark:text-gray-300 dark:bg-gray-850 outline-hidden border border-gray-100 dark:border-gray-850"
placeholder={variables[variable]?.placeholder ?? ''}
bind:value={variableValues[variable]}
autocomplete="off"
id="input-variable-{idx}"
required={variables[variable]?.required ?? false}
{...variableAttributes}
/>
{:else if variables[variable]?.type === 'number'}
<input
type="number"
class="w-full rounded-lg py-2 px-4 text-sm dark:text-gray-300 dark:bg-gray-850 outline-hidden border border-gray-100 dark:border-gray-850"
placeholder={variables[variable]?.placeholder ?? ''}
bind:value={variableValues[variable]}
autocomplete="off"
id="input-variable-{idx}"
required={variables[variable]?.required ?? false}
{...variableAttributes}
/>
{:else if variables[variable]?.type === 'range'}
<div class="flex items-center space-x-2">
<div class="relative flex justify-center items-center gap-2 flex-1">
<input
type="range"
bind:value={variableValues[variable]}
class="w-full rounded-lg py-1 px-4 text-sm dark:text-gray-300 dark:bg-gray-850 outline-hidden border border-gray-100 dark:border-gray-850"
id="input-variable-{idx}"
{...variableAttributes}
/>
</div>
<input
type="text"
class=" py-1 text-sm dark:text-gray-300 bg-transparent outline-hidden text-right"
placeholder={$i18n.t('Enter value')}
bind:value={variableValues[variable]}
autocomplete="off"
required={variables[variable]?.required ?? false}
/>
</div>
<!-- <input
type="range"
class="w-full rounded-lg py-2 px-4 text-sm dark:text-gray-300 dark:bg-gray-850 outline-hidden border border-gray-100 dark:border-gray-850"
placeholder={variables[variable]?.placeholder ?? ''}
bind:value={variableValues[variable]}
autocomplete="off"
id="input-variable-{idx}"
required
/> -->
{:else if variables[variable]?.type === 'tel'}
<input
type="tel"
class="w-full rounded-lg py-2 px-4 text-sm dark:text-gray-300 dark:bg-gray-850 outline-hidden border border-gray-100 dark:border-gray-850"
placeholder={variables[variable]?.placeholder ?? ''}
bind:value={variableValues[variable]}
autocomplete="off"
id="input-variable-{idx}"
required={variables[variable]?.required ?? false}
{...variableAttributes}
/>
{:else if variables[variable]?.type === 'text'}
<input
type="text"
class="w-full rounded-lg py-2 px-4 text-sm dark:text-gray-300 dark:bg-gray-850 outline-hidden border border-gray-100 dark:border-gray-850"
placeholder={variables[variable]?.placeholder ?? ''}
bind:value={variableValues[variable]}
autocomplete="off"
id="input-variable-{idx}"
required={variables[variable]?.required ?? false}
{...variableAttributes}
/>
{:else if variables[variable]?.type === 'time'}
<input
type="time"
class="w-full rounded-lg py-2 px-4 text-sm dark:text-gray-300 dark:bg-gray-850 outline-hidden border border-gray-100 dark:border-gray-850"
placeholder={variables[variable]?.placeholder ?? ''}
bind:value={variableValues[variable]}
autocomplete="off"
id="input-variable-{idx}"
required={variables[variable]?.required ?? false}
{...variableAttributes}
/>
{:else if variables[variable]?.type === 'url'}
<input
type="url"
class="w-full rounded-lg py-2 px-4 text-sm dark:text-gray-300 dark:bg-gray-850 outline-hidden border border-gray-100 dark:border-gray-850"
placeholder={variables[variable]?.placeholder ?? ''}
bind:value={variableValues[variable]}
autocomplete="off"
id="input-variable-{idx}"
required={variables[variable]?.required ?? false}
{...variableAttributes}
/>
{:else if variables[variable]?.type === 'map'}
<!-- EXPERIMENTAL INPUT TYPE, DO NOT USE IN PRODUCTION -->
<div class="flex flex-col items-center gap-1">
<MapSelector
setViewLocation={((variableValues[variable] ?? '').includes(',') ??
false)
? variableValues[variable].split(',')
: null}
onClick={(value) => {
variableValues[variable] = value;
}}
/>
<input
type="text"
class=" w-full py-1 text-left text-sm dark:text-gray-300 bg-transparent outline-hidden"
placeholder={$i18n.t('Enter coordinates (e.g. 51.505, -0.09)')}
bind:value={variableValues[variable]}
autocomplete="off"
required={variables[variable]?.required ?? false}
/>
</div>
{:else}
<textarea
class="w-full rounded-lg py-2 px-4 text-sm dark:text-gray-300 dark:bg-gray-850 outline-hidden border border-gray-100 dark:border-gray-850"
placeholder={variables[variable]?.placeholder ?? ''}
bind:value={variableValues[variable]}
autocomplete="off"
id="input-variable-{idx}"
required={variables[variable]?.required ?? false}
/>
{/if}
</div>
</div>
<!-- {#if (valvesSpec.properties[property]?.description ?? null) !== null}
<div class="text-xs text-gray-500">
{valvesSpec.properties[property].description}
</div>
{/if} -->
</div>
{/each}
</div>
{:else}
<Spinner className="size-5" />
{/if}
</div>
<div class="flex justify-end pt-3 text-sm font-medium">
<button
class="px-3.5 py-1.5 text-sm font-medium bg-white hover:bg-gray-100 text-black dark:bg-black dark:text-white dark:hover:bg-gray-900 transition rounded-full"
type="button"
on:click={() => {
show = false;
}}
>
{$i18n.t('Cancel')}
</button>
<button
class="px-3.5 py-1.5 text-sm font-medium bg-black hover:bg-gray-900 text-white dark:bg-white dark:text-black dark:hover:bg-gray-100 transition rounded-full"
type="submit"
>
{$i18n.t('Save')}
</button>
</div>
</form>
</div>
</div>
</div>
</Modal>
|