|
1 | 1 | <!-- Copyright by the Spark Development Network; Licensed under the Rock Community License --> |
2 | 2 | <template> |
3 | | - <SearchField v-model="searchTerm" :initialSearchFilters="options?.searchFilters ?? undefined" /> |
| 3 | + <div class="smartsearch searchinput"> |
| 4 | + <i class="ti ti-search"></i> |
| 5 | + <ul class="nav pull-right smartsearch-type"> |
| 6 | + <li class="dropdown"><a class="dropdown-toggle navbar-link" data-toggle="dropdown"><span>{{ defaultFilterLabel }}</span><b class="ti ti-caret-down-filled"></b></a> |
| 7 | + <ul class="dropdown-menu"> |
| 8 | + <li v-for="(filter, index) in searchFilters" :key="filter.key ?? index" :data-key="filter.key" :data-target="filter.resultUrl"><a>{{ filter.label }}</a></li> |
| 9 | + </ul> |
| 10 | + </li> |
| 11 | + </ul> |
| 12 | + <input type="search" ref="inputElement" :id="uniqueId" v-model="internalValue" accesskey="q" class="searchinput tt-query" autocomplete="off" spellcheck="false" style="position: relative; vertical-align: top; background-color: transparent;" dir="auto"> |
| 13 | + <input type="hidden" ref="hiddenFilterInput" name="searchField_hSearchFilter" id="searchField_hSearchFilter"> |
| 14 | + </div> |
4 | 15 | </template> |
5 | 16 |
|
6 | 17 | <script setup lang="ts"> |
7 | | - import { ref } from "vue"; |
| 18 | + import { computed, ref, watch } from "vue"; |
8 | 19 | import { onConfigurationValuesChanged, useConfigurationValues, useReloadBlock } from "@Obsidian/Utility/block"; |
9 | 20 | import { CustomBlockBox } from "@Obsidian/ViewModels/Blocks/customBlockBox"; |
10 | 21 | import { SmartSearchOptionsBag } from "@Obsidian/ViewModels/Blocks/Core/SmartSearch/smartSearchOptionsBag"; |
11 | | - import SearchField from "@Obsidian/Controls/searchField.obs"; |
| 22 | + import { SearchFilterBag } from "@Obsidian/ViewModels/Blocks/Core/SmartSearch/searchFilterBag"; |
| 23 | + import { useHttp } from "@Obsidian/Utility/http"; |
| 24 | + import { newGuid } from "@Obsidian/Utility/guid"; |
12 | 25 |
|
13 | 26 | const config = useConfigurationValues<CustomBlockBox<object, SmartSearchOptionsBag>>(); |
14 | | - const options = config.options; |
15 | | - const searchTerm = ref(""); |
| 27 | + |
| 28 | + // #region Values |
| 29 | + |
| 30 | + const internalValue = ref(""); |
| 31 | + const http = useHttp(); |
| 32 | + const uniqueId = `rock-searchField-${newGuid()}`; |
| 33 | + const inputElement = ref<HTMLInputElement>(); |
| 34 | + const hiddenFilterInput = ref<HTMLInputElement>(); |
| 35 | + |
| 36 | + /** |
| 37 | + * The available search filters. |
| 38 | + */ |
| 39 | + const searchFilters = ref<SearchFilterBag[]>(); |
| 40 | + |
| 41 | + // #endregion |
| 42 | + |
| 43 | + // #region Computed Values |
| 44 | + |
| 45 | + /** |
| 46 | + * Gets the display label of the default search filter (e.g. "Name"). |
| 47 | + */ |
| 48 | + const defaultFilterLabel = computed<string>(() => { |
| 49 | + if (!searchFilters.value || searchFilters.value.length === 0) { |
| 50 | + return ""; |
| 51 | + } |
| 52 | + |
| 53 | + return searchFilters.value[0].label ?? ""; |
| 54 | + }); |
| 55 | + |
| 56 | + // #endregion |
| 57 | + |
| 58 | + // #region Functions |
| 59 | + |
| 60 | + /** Fetches the available search filters from the API. */ |
| 61 | + async function fetchSearchFilters(): Promise<void> { |
| 62 | + const url = "/api/v2/Controls/SearchFieldGetSearchFilters"; |
| 63 | + const response = await http.post<SearchFilterBag[]>(url); |
| 64 | + |
| 65 | + if (response.isSuccess && response.data) { |
| 66 | + searchFilters.value = response.data; |
| 67 | + } |
| 68 | + else { |
| 69 | + console.error("Error fetching items from server", response.errorMessage); |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + // #endregion |
| 74 | + |
| 75 | + // #region Watchers |
| 76 | + |
| 77 | + watch([inputElement, hiddenFilterInput, searchFilters], () => { |
| 78 | + if (inputElement.value && hiddenFilterInput.value && searchFilters.value) { |
| 79 | + if (searchFilters.value.length > 0) { |
| 80 | + hiddenFilterInput.value.value = searchFilters.value[0].key ?? ""; |
| 81 | + } |
| 82 | + |
| 83 | + // eslint-disable-next-line @typescript-eslint/no-explicit-any |
| 84 | + (window["Rock"] as any).controls.searchField.initialize({ controlId: uniqueId }); |
| 85 | + } |
| 86 | + }); |
| 87 | + |
| 88 | + // #endregion |
| 89 | + |
| 90 | + if (config.options?.searchFilters) { |
| 91 | + searchFilters.value = config.options.searchFilters; |
| 92 | + } |
| 93 | + else { |
| 94 | + fetchSearchFilters(); |
| 95 | + } |
16 | 96 |
|
17 | 97 | onConfigurationValuesChanged(useReloadBlock()); |
18 | 98 | </script> |
0 commit comments