From 0dc60ce3ebc928e6ee9c53d298323732dc7c0dc9 Mon Sep 17 00:00:00 2001 From: Ze Yu Date: Fri, 16 Dec 2022 23:34:39 +0800 Subject: [PATCH] Cleanup default option setup --- packages/search-ui/src/Options.ts | 3 -- packages/search-ui/src/search/options.ts | 68 ++++++++++-------------- 2 files changed, 28 insertions(+), 43 deletions(-) diff --git a/packages/search-ui/src/Options.ts b/packages/search-ui/src/Options.ts index 8bd30a33..4e8f78d1 100644 --- a/packages/search-ui/src/Options.ts +++ b/packages/search-ui/src/Options.ts @@ -3,8 +3,6 @@ import { Result } from '@infisearch/search-lib/lib/results/Result'; import { Query } from '@infisearch/search-lib'; import { CreateElement } from '@infisearch/search-lib/lib/utils/dom'; -export type ArbitraryOptions = any; - export enum UiMode { Auto = 'auto', Dropdown = 'dropdown', @@ -102,5 +100,4 @@ export type ListItemRender = ( export interface Options { searcherOptions?: SearcherOptions, uiOptions?: UiOptions, - otherOptions: ArbitraryOptions } diff --git a/packages/search-ui/src/search/options.ts b/packages/search-ui/src/search/options.ts index c7813086..c30242ff 100644 --- a/packages/search-ui/src/search/options.ts +++ b/packages/search-ui/src/search/options.ts @@ -1,4 +1,4 @@ -import { Options, UiMode } from '../Options'; +import { Options, UiMode, UiOptions } from '../Options'; import { listItemRender } from '../searchResultTransform/listItemRender'; import { TRANSLATIONS } from '../translations/en'; @@ -7,65 +7,53 @@ export function prepareOptions(options: Options) { // ------------------------------------------------------------ // Ui Options - - options.uiOptions = options.uiOptions || ({} as any); + + const suppliedUiOpts = (options.uiOptions || {}) as UiOptions; + options.uiOptions = { + mode: UiMode.Auto, + inputDebounce: 100, + isMobileDevice: () => window.matchMedia('only screen and (max-width: 768px)').matches, + preprocessQuery: (q) => q, + dropdownAlignment: 'bottom-end', + resultsPerPage: 10, + maxSubMatches: 2, + fsScrollLock: true, + fsContainer: document.getElementsByTagName('body')[0] as HTMLElement, + sortFields: {}, + multiSelectFilters: [], + numericFilters: [], + listItemRender, + ...suppliedUiOpts, + translations: { + ...TRANSLATIONS, + ...(suppliedUiOpts.translations || {}), + }, + }; + const { uiOptions } = options; - if (uiOptions.sourceFilesUrl && !uiOptions.sourceFilesUrl.endsWith('/')) { uiOptions.sourceFilesUrl += '/'; } - - uiOptions.mode = uiOptions.mode || UiMode.Auto; - uiOptions.isMobileDevice = uiOptions.isMobileDevice - || (() => window.matchMedia('only screen and (max-width: 768px)').matches); - if (uiOptions.mode === UiMode.Target) { if (typeof uiOptions.target === 'string') { uiOptions.target = document.getElementById(uiOptions.target); } - + if (!uiOptions.target) { throw new Error('\'target\' mode specified but no valid target option specified'); } } - + if (!('input' in uiOptions) || typeof uiOptions.input === 'string') { uiOptions.input = document.getElementById(uiOptions.input as any || 'infi-search') as HTMLInputElement; } - + if ([UiMode.Dropdown, UiMode.Target].includes(uiOptions.mode) && !uiOptions.input) { throw new Error('\'dropdown\' or \'target\' mode specified but no input element found'); } - - if (!('inputDebounce' in uiOptions)) { - uiOptions.inputDebounce = 100; - } - - uiOptions.preprocessQuery = uiOptions.preprocessQuery || ((q) => q); - - uiOptions.dropdownAlignment = uiOptions.dropdownAlignment || 'bottom-end'; - + if (typeof uiOptions.fsContainer === 'string') { uiOptions.fsContainer = document.getElementById(uiOptions.fsContainer) as HTMLElement; } - uiOptions.fsContainer = uiOptions.fsContainer || document.getElementsByTagName('body')[0] as HTMLElement; - - uiOptions.resultsPerPage = uiOptions.resultsPerPage || 10; - uiOptions.maxSubMatches = uiOptions.maxSubMatches || 2; - - uiOptions.translations = { - ...TRANSLATIONS, - ...(uiOptions.translations || {}), - }; - if (!('fsScrollLock' in uiOptions)) { - uiOptions.fsScrollLock = true; - } - uiOptions.sortFields = uiOptions.sortFields || {}; - uiOptions.multiSelectFilters = uiOptions.multiSelectFilters || []; - uiOptions.numericFilters = uiOptions.numericFilters || []; - - uiOptions.listItemRender = uiOptions.listItemRender || listItemRender; - - options.otherOptions = options.otherOptions || {}; }