|
| 1 | +<script> |
| 2 | + import { onMount, onDestroy } from 'svelte'; |
| 3 | + import { fly } from 'svelte/transition'; |
| 4 | + import { Input, Tooltip } from '@sveltestrap/sveltestrap'; |
| 5 | + import util from "lodash"; |
| 6 | + |
| 7 | +
|
| 8 | + const maxLength = 50; |
| 9 | +
|
| 10 | + /** @type {{ key: string, displayName: string }[]} */ |
| 11 | + export let items; |
| 12 | + |
| 13 | + /** @type {boolean} */ |
| 14 | + export let disabled = false; |
| 15 | +
|
| 16 | +
|
| 17 | + /** @type {boolean} */ |
| 18 | + let showAdvSearch = false; |
| 19 | +
|
| 20 | + /** @type {any[]} */ |
| 21 | + let innerItems = []; |
| 22 | +
|
| 23 | +
|
| 24 | + onMount(() => { |
| 25 | + init(); |
| 26 | + }); |
| 27 | +
|
| 28 | + onDestroy(() => { |
| 29 | + reset(); |
| 30 | + }); |
| 31 | +
|
| 32 | + function init() { |
| 33 | + showAdvSearch = false; |
| 34 | + reset(); |
| 35 | + } |
| 36 | +
|
| 37 | +
|
| 38 | + /** @param {any} e */ |
| 39 | + function toggleAdvSearch(e) { |
| 40 | + showAdvSearch = e.target.checked; |
| 41 | + if (!showAdvSearch) { |
| 42 | + reset(); |
| 43 | + } |
| 44 | + } |
| 45 | +
|
| 46 | + function reset() { |
| 47 | + innerItems = items?.map(x => { |
| 48 | + return { |
| 49 | + key: x.key, |
| 50 | + checked: false, |
| 51 | + displayName: x.displayName, |
| 52 | + value: '' |
| 53 | + }; |
| 54 | + }) || []; |
| 55 | + } |
| 56 | +
|
| 57 | + |
| 58 | + /** |
| 59 | + * @param {number} idx |
| 60 | + * @param {any} e |
| 61 | + */ |
| 62 | + function toggleItemCheckbox(idx, e) { |
| 63 | + const found = innerItems.find((_, index) => index === idx); |
| 64 | + if (found) { |
| 65 | + found.checked = e.target.checked; |
| 66 | + innerItems = innerItems.map((x, index) => { |
| 67 | + return index === idx ? { ...found } : x; |
| 68 | + }); |
| 69 | + } |
| 70 | + } |
| 71 | +
|
| 72 | + /** |
| 73 | + * @param {number} idx |
| 74 | + * @param {any} e |
| 75 | + */ |
| 76 | + function changeItemValue(idx, e) { |
| 77 | + const found = innerItems.find((_, index) => index === idx); |
| 78 | + if (found) { |
| 79 | + found.value = e.target.value; |
| 80 | + innerItems = innerItems.map((x, index) => { |
| 81 | + return index === idx ? { ...found } : x; |
| 82 | + }); |
| 83 | + } |
| 84 | + } |
| 85 | +</script> |
| 86 | +
|
| 87 | +
|
| 88 | +<div |
| 89 | + class="knowledge-adv-search-container" |
| 90 | + in:fly={{ y: -10, duration: 500 }} |
| 91 | + out:fly={{ y: -10, duration: 200 }} |
| 92 | +> |
| 93 | + <div class="knowledge-adv-search-btn text-primary fw-bold"> |
| 94 | + <Input |
| 95 | + type="switch" |
| 96 | + disabled={disabled} |
| 97 | + checked={showAdvSearch} |
| 98 | + on:change={e => toggleAdvSearch(e)} |
| 99 | + /> |
| 100 | + <div class="line-align-center"> |
| 101 | + <div>{'Advance Search'}</div> |
| 102 | + </div> |
| 103 | + {#if showAdvSearch} |
| 104 | + <div class="line-align-center" id="adv-search-tooltip"> |
| 105 | + <i class="bx bx-info-circle" /> |
| 106 | + </div> |
| 107 | + <Tooltip target="adv-search-tooltip" placement="top" class="demo-tooltip-note"> |
| 108 | + <ul> |
| 109 | + <li>{'Select the checkbox to enable seaching in each field.'}</li> |
| 110 | + <li>{'Empty value will not be used to search.'}</li> |
| 111 | + </ul> |
| 112 | + </Tooltip> |
| 113 | + {/if} |
| 114 | + </div> |
| 115 | +
|
| 116 | + {#if showAdvSearch} |
| 117 | + <div |
| 118 | + class={'knowledge-adv-search-items'} |
| 119 | + in:fly={{ y: -10, duration: 500 }} |
| 120 | + out:fly={{ y: -10, duration: 200 }} |
| 121 | + > |
| 122 | + {#each innerItems as item, idx (idx)} |
| 123 | + <div class="knowledge-adv-search-item"> |
| 124 | + <div class="search-item-cb line-align-center"> |
| 125 | + <Input |
| 126 | + type="checkbox" |
| 127 | + disabled={disabled} |
| 128 | + checked={item.checked} |
| 129 | + on:change={e => toggleItemCheckbox(idx, e)} |
| 130 | + /> |
| 131 | + </div> |
| 132 | + <div class="search-item-name fw-bold line-align-center"> |
| 133 | + <div>{`${item.displayName}:`}</div> |
| 134 | + </div> |
| 135 | + <div class="search-item-content line-align-center"> |
| 136 | + <Input |
| 137 | + type="text" |
| 138 | + disabled={!item.checked || disabled} |
| 139 | + maxlength={maxLength} |
| 140 | + value={item.value} |
| 141 | + on:input={e => changeItemValue(idx, e)} |
| 142 | + /> |
| 143 | + </div> |
| 144 | + </div> |
| 145 | + {/each} |
| 146 | + </div> |
| 147 | + {/if} |
| 148 | +</div> |
0 commit comments