Valves.svelte 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. <script>
  2. import { onMount, getContext, createEventDispatcher } from 'svelte';
  3. const dispatch = createEventDispatcher();
  4. const i18n = getContext('i18n');
  5. import Switch from './Switch.svelte';
  6. export let valvesSpec = null;
  7. export let valves = {};
  8. </script>
  9. {#if valvesSpec && Object.keys(valvesSpec?.properties ?? {}).length}
  10. {#each Object.keys(valvesSpec.properties) as property, idx}
  11. <div class=" py-0.5 w-full justify-between">
  12. <div class="flex w-full justify-between">
  13. <div class=" self-center text-xs font-medium">
  14. {valvesSpec.properties[property].title}
  15. {#if (valvesSpec?.required ?? []).includes(property)}
  16. <span class=" text-gray-500">*required</span>
  17. {/if}
  18. </div>
  19. <button
  20. class="p-1 px-3 text-xs flex rounded-sm transition"
  21. type="button"
  22. on:click={() => {
  23. valves[property] =
  24. (valves[property] ?? null) === null
  25. ? (valvesSpec.properties[property]?.default ?? '')
  26. : null;
  27. dispatch('change');
  28. }}
  29. >
  30. {#if (valves[property] ?? null) === null}
  31. <span class="ml-2 self-center">
  32. {#if (valvesSpec?.required ?? []).includes(property)}
  33. {$i18n.t('None')}
  34. {:else}
  35. {$i18n.t('Default')}
  36. {/if}
  37. </span>
  38. {:else}
  39. <span class="ml-2 self-center"> {$i18n.t('Custom')} </span>
  40. {/if}
  41. </button>
  42. </div>
  43. {#if (valves[property] ?? null) !== null}
  44. <!-- {valves[property]} -->
  45. <div class="flex mt-0.5 mb-1.5 space-x-2">
  46. <div class=" flex-1">
  47. {#if valvesSpec.properties[property]?.enum ?? null}
  48. <select
  49. 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"
  50. bind:value={valves[property]}
  51. on:change={() => {
  52. dispatch('change');
  53. }}
  54. >
  55. {#each valvesSpec.properties[property].enum as option}
  56. <option value={option} selected={option === valves[property]}>
  57. {option}
  58. </option>
  59. {/each}
  60. </select>
  61. {:else if (valvesSpec.properties[property]?.type ?? null) === 'boolean'}
  62. <div class="flex justify-between items-center">
  63. <div class="text-xs text-gray-500">
  64. {valves[property] ? 'Enabled' : 'Disabled'}
  65. </div>
  66. <div class=" pr-2">
  67. <Switch
  68. bind:state={valves[property]}
  69. on:change={() => {
  70. dispatch('change');
  71. }}
  72. />
  73. </div>
  74. </div>
  75. {:else if (valvesSpec.properties[property]?.type ?? null) !== 'string'}
  76. <input
  77. 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"
  78. type="text"
  79. placeholder={valvesSpec.properties[property].title}
  80. bind:value={valves[property]}
  81. autocomplete="off"
  82. required
  83. on:change={() => {
  84. dispatch('change');
  85. }}
  86. />
  87. {:else}
  88. <textarea
  89. 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"
  90. placeholder={valvesSpec.properties[property].title}
  91. bind:value={valves[property]}
  92. autocomplete="off"
  93. required
  94. on:change={() => {
  95. dispatch('change');
  96. }}
  97. />
  98. {/if}
  99. </div>
  100. </div>
  101. {/if}
  102. {#if (valvesSpec.properties[property]?.description ?? null) !== null}
  103. <div class="text-xs text-gray-500">
  104. {valvesSpec.properties[property].description}
  105. </div>
  106. {/if}
  107. </div>
  108. {/each}
  109. {:else}
  110. <div class="text-xs">No valves</div>
  111. {/if}