Skip to content

Commit

Permalink
[Data plugin] Add configuration property to enable / disable autocomp…
Browse files Browse the repository at this point in the history
…lete (#67847)

Co-authored-by: Elastic Machine <elasticmachine@users.noreply.github.com>
  • Loading branch information
alexwizp and elasticmachine authored Jun 4, 2020
1 parent 0493978 commit 6195f3e
Show file tree
Hide file tree
Showing 16 changed files with 188 additions and 98 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@ Constructs a new instance of the `DataPublicPlugin` class
<b>Signature:</b>

```typescript
constructor(initializerContext: PluginInitializerContext);
constructor(initializerContext: PluginInitializerContext<ConfigSchema>);
```

## Parameters

| Parameter | Type | Description |
| --- | --- | --- |
| initializerContext | <code>PluginInitializerContext</code> | |
| initializerContext | <code>PluginInitializerContext&lt;ConfigSchema&gt;</code> | |

Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@
<b>Signature:</b>

```typescript
export declare function plugin(initializerContext: PluginInitializerContext): DataPublicPlugin;
export declare function plugin(initializerContext: PluginInitializerContext<ConfigSchema>): DataPublicPlugin;
```

## Parameters

| Parameter | Type | Description |
| --- | --- | --- |
| initializerContext | <code>PluginInitializerContext</code> | |
| initializerContext | <code>PluginInitializerContext&lt;ConfigSchema&gt;</code> | |

<b>Returns:</b>

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<!-- Do not edit this file. It is automatically generated by API Documenter. -->

[Home](./index.md) &gt; [kibana-plugin-plugins-data-server](./kibana-plugin-plugins-data-server.md) &gt; [config](./kibana-plugin-plugins-data-server.config.md)

## config variable

<b>Signature:</b>

```typescript
config: PluginConfigDescriptor<ConfigSchema>
```
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@
| Variable | Description |
| --- | --- |
| [castEsToKbnFieldTypeName](./kibana-plugin-plugins-data-server.castestokbnfieldtypename.md) | Get the KbnFieldType name for an esType string |
| [config](./kibana-plugin-plugins-data-server.config.md) | |
| [esFilters](./kibana-plugin-plugins-data-server.esfilters.md) | |
| [esKuery](./kibana-plugin-plugins-data-server.eskuery.md) | |
| [esQuery](./kibana-plugin-plugins-data-server.esquery.md) | |
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@ Constructs a new instance of the `DataServerPlugin` class
<b>Signature:</b>

```typescript
constructor(initializerContext: PluginInitializerContext);
constructor(initializerContext: PluginInitializerContext<ConfigSchema>);
```

## Parameters

| Parameter | Type | Description |
| --- | --- | --- |
| initializerContext | <code>PluginInitializerContext</code> | |
| initializerContext | <code>PluginInitializerContext&lt;ConfigSchema&gt;</code> | |

Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,14 @@ Static code to be shared externally
<b>Signature:</b>

```typescript
export declare function plugin(initializerContext: PluginInitializerContext): DataServerPlugin;
export declare function plugin(initializerContext: PluginInitializerContext<ConfigSchema>): DataServerPlugin;
```

## Parameters

| Parameter | Type | Description |
| --- | --- | --- |
| initializerContext | <code>PluginInitializerContext</code> | |
| initializerContext | <code>PluginInitializerContext&lt;ConfigSchema&gt;</code> | |

<b>Returns:</b>

Expand Down
33 changes: 33 additions & 0 deletions src/plugins/data/config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*
* Licensed to Elasticsearch B.V. under one or more contributor
* license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright
* ownership. Elasticsearch B.V. licenses this file to you under
* the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

import { schema, TypeOf } from '@kbn/config-schema';

export const configSchema = schema.object({
autocomplete: schema.object({
querySuggestions: schema.object({
enabled: schema.boolean({ defaultValue: true }),
}),
valueSuggestions: schema.object({
enabled: schema.boolean({ defaultValue: true }),
}),
}),
});

export type ConfigSchema = TypeOf<typeof configSchema>;
19 changes: 16 additions & 3 deletions src/plugins/data/public/autocomplete/autocomplete_service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,30 @@
* under the License.
*/

import { CoreSetup } from 'src/core/public';
import { CoreSetup, PluginInitializerContext } from 'src/core/public';
import { QuerySuggestionGetFn } from './providers/query_suggestion_provider';
import {
getEmptyValueSuggestions,
setupValueSuggestionProvider,
ValueSuggestionsGetFn,
} from './providers/value_suggestion_provider';

import { ConfigSchema } from '../../config';

export class AutocompleteService {
autocompleteConfig: ConfigSchema['autocomplete'];

constructor(initializerContext: PluginInitializerContext<ConfigSchema>) {
const { autocomplete } = initializerContext.config.get<ConfigSchema>();

this.autocompleteConfig = autocomplete;
}

private readonly querySuggestionProviders: Map<string, QuerySuggestionGetFn> = new Map();
private getValueSuggestions?: ValueSuggestionsGetFn;

private addQuerySuggestionProvider = (language: string, provider: QuerySuggestionGetFn): void => {
if (language && provider) {
if (language && provider && this.autocompleteConfig.querySuggestions.enabled) {
this.querySuggestionProviders.set(language, provider);
}
};
Expand All @@ -47,7 +58,9 @@ export class AutocompleteService {

/** @public **/
public setup(core: CoreSetup) {
this.getValueSuggestions = setupValueSuggestionProvider(core);
this.getValueSuggestions = this.autocompleteConfig.valueSuggestions.enabled
? setupValueSuggestionProvider(core)
: getEmptyValueSuggestions;

return {
addQuerySuggestionProvider: this.addQuerySuggestionProvider,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ interface ValueSuggestionsGetFnArgs {
signal?: AbortSignal;
}

export const getEmptyValueSuggestions = (() => Promise.resolve([])) as ValueSuggestionsGetFn;

export const setupValueSuggestionProvider = (core: CoreSetup): ValueSuggestionsGetFn => {
const requestSuggestions = memoize(
(index: string, field: IFieldType, query: string, boolFilter: any = [], signal?: AbortSignal) =>
Expand Down
3 changes: 2 additions & 1 deletion src/plugins/data/public/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import './index.scss';

import { PluginInitializerContext } from '../../../core/public';
import { ConfigSchema } from '../config';

/*
* Filters:
Expand Down Expand Up @@ -450,7 +451,7 @@ export {

import { DataPublicPlugin } from './plugin';

export function plugin(initializerContext: PluginInitializerContext) {
export function plugin(initializerContext: PluginInitializerContext<ConfigSchema>) {
return new DataPublicPlugin(initializerContext);
}

Expand Down
6 changes: 4 additions & 2 deletions src/plugins/data/public/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import {
Plugin,
PackageInfo,
} from 'src/core/public';
import { ConfigSchema } from '../config';
import { Storage, IStorageWrapper, createStartServicesGetter } from '../../kibana_utils/public';
import {
DataPublicPluginSetup,
Expand Down Expand Up @@ -83,17 +84,18 @@ declare module '../../ui_actions/public' {
}

export class DataPublicPlugin implements Plugin<DataPublicPluginSetup, DataPublicPluginStart> {
private readonly autocomplete = new AutocompleteService();
private readonly autocomplete: AutocompleteService;
private readonly searchService: SearchService;
private readonly fieldFormatsService: FieldFormatsService;
private readonly queryService: QueryService;
private readonly storage: IStorageWrapper;
private readonly packageInfo: PackageInfo;

constructor(initializerContext: PluginInitializerContext) {
constructor(initializerContext: PluginInitializerContext<ConfigSchema>) {
this.searchService = new SearchService();
this.queryService = new QueryService();
this.fieldFormatsService = new FieldFormatsService();
this.autocomplete = new AutocompleteService(initializerContext);
this.storage = new Storage(window.localStorage);
this.packageInfo = initializerContext.env.packageInfo;
}
Expand Down
Loading

0 comments on commit 6195f3e

Please sign in to comment.