Skip to content

Commit

Permalink
Cleanup default option setup
Browse files Browse the repository at this point in the history
  • Loading branch information
ang-zeyu committed Dec 16, 2022
1 parent cf69c5c commit 0dc60ce
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 43 deletions.
3 changes: 0 additions & 3 deletions packages/search-ui/src/Options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down Expand Up @@ -102,5 +100,4 @@ export type ListItemRender = (
export interface Options {
searcherOptions?: SearcherOptions,
uiOptions?: UiOptions,
otherOptions: ArbitraryOptions
}
68 changes: 28 additions & 40 deletions packages/search-ui/src/search/options.ts
Original file line number Diff line number Diff line change
@@ -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';

Expand All @@ -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 || {};
}

0 comments on commit 0dc60ce

Please sign in to comment.