Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Display changed field formats without requiring hard page refresh. #53746

Merged
merged 5 commits into from
Dec 23, 2019
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,13 @@ import { CoreSetup, IUiSettingsClient } from 'kibana/public';

import { FieldFormatRegisty } from './field_formats';
import {
BoolFormat,
IFieldFormatType,
PercentFormat,
BoolFormat,
StringFormat,
} from '../../common/field_formats';
import { coreMock } from '../../../../core/public/mocks';
import { KBN_FIELD_TYPES } from '../../common';

const getValueOfPrivateField = (instance: any, field: string) => instance[field];
const getUiSettingsMock = (data: any): IUiSettingsClient['get'] => () => data;
Expand Down Expand Up @@ -115,12 +116,12 @@ describe('FieldFormatRegisty', () => {
test('should set meta params for all instances of FieldFormats', () => {
fieldFormatRegisty.register([StringFormat]);

const ConcreteFormat = fieldFormatRegisty.getType(StringFormat.id);
const DecoratedStingFormat = fieldFormatRegisty.getType(StringFormat.id);

expect(ConcreteFormat).toBeDefined();
expect(DecoratedStingFormat).toBeDefined();

if (ConcreteFormat) {
const stringFormat = new ConcreteFormat({
if (DecoratedStingFormat) {
const stringFormat = new DecoratedStingFormat({
foo: 'foo',
});
const params = getValueOfPrivateField(stringFormat, '_params');
Expand All @@ -132,5 +133,39 @@ describe('FieldFormatRegisty', () => {
expect(params.parsedUrl).toHaveProperty('basePath');
}
});

test('should decorate static fields', () => {
fieldFormatRegisty.register([BoolFormat]);

const DecoratedBoolFormat = fieldFormatRegisty.getType(BoolFormat.id);

expect(DecoratedBoolFormat).toBeDefined();

if (DecoratedBoolFormat) {
expect(DecoratedBoolFormat.id).toBe(BoolFormat.id);
expect(DecoratedBoolFormat.fieldType).toBe(BoolFormat.fieldType);
}
});
});

describe('getByFieldType', () => {
test('should provide an public "getByFieldType" method', () => {
expect(fieldFormatRegisty.getByFieldType).toBeDefined();
expect(typeof fieldFormatRegisty.getByFieldType).toBe('function');
});

test('should decorate returns types', () => {
fieldFormatRegisty.register([StringFormat, BoolFormat]);

const [DecoratedStringFormat] = fieldFormatRegisty.getByFieldType(KBN_FIELD_TYPES.STRING);

expect(DecoratedStringFormat).toBeDefined();

const stingFormat = new DecoratedStringFormat({ foo: 'foo' });
const params = getValueOfPrivateField(stingFormat, '_params');

expect(params).toHaveProperty('foo');
expect(params).toHaveProperty('parsedUrl');
});
});
});
63 changes: 43 additions & 20 deletions src/plugins/data/public/field_formats_provider/field_formats.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
* under the License.
*/

// eslint-disable-next-line max-classes-per-file
import { forOwn, isFunction, memoize } from 'lodash';
import { IUiSettingsClient, CoreSetup } from 'kibana/public';
import {
Expand Down Expand Up @@ -78,10 +79,14 @@ export class FieldFormatRegisty {
* @return {FieldFormat | void}
*/
getType = (formatId: IFieldFormatId): IFieldFormatType | void => {
alexwizp marked this conversation as resolved.
Show resolved Hide resolved
const decoratedFieldFormat: any = this.fieldFormatMetaParamsDecorator(formatId);
const fieldFormat = this.fieldFormats.get(formatId);

if (decoratedFieldFormat) {
return decoratedFieldFormat as IFieldFormatType;
if (fieldFormat) {
const decoratedFieldFormat: any = this.fieldFormatMetaParamsDecorator(fieldFormat);

if (decoratedFieldFormat) {
return decoratedFieldFormat as IFieldFormatType;
}
}
};

Expand Down Expand Up @@ -196,9 +201,12 @@ export class FieldFormatRegisty {
* @return {FieldFormat[]}
*/
getByFieldType(fieldType: KBN_FIELD_TYPES): IFieldFormatType[] {
return [...this.fieldFormats.values()].filter(
(format: IFieldFormatType) => format.fieldType.indexOf(fieldType) !== -1
);
return [...this.fieldFormats.values()]
.filter((format: IFieldFormatType) => format && format.fieldType.indexOf(fieldType) !== -1)
.map(
(format: IFieldFormatType) =>
this.fieldFormatMetaParamsDecorator(format) as IFieldFormatType
);
}

/**
Expand Down Expand Up @@ -232,24 +240,39 @@ export class FieldFormatRegisty {
* FieldFormat decorator - provide a one way to add meta-params for all field formatters
*
* @private
* @param {IFieldFormatId} formatId - the format id
* @param {IFieldFormatType} fieldFormat - field format type
* @return {FieldFormat | void}
*/
private fieldFormatMetaParamsDecorator = (formatId: IFieldFormatId): Function | void => {
const concreteFieldFormat = this.fieldFormats.get(formatId);
const decorateMetaParams = (customOptions: Record<string, any> = {}) => ({
parsedUrl: {
origin: window.location.origin,
pathname: window.location.pathname,
basePath: this.basePath,
},
...customOptions,
});
private fieldFormatMetaParamsDecorator = (
fieldFormat: IFieldFormatType
): IFieldFormatType | void => {
alexwizp marked this conversation as resolved.
Show resolved Hide resolved
const getMetaParams = (customParams: Record<string, any>) => this.buildMetaParams(customParams);

if (concreteFieldFormat) {
return function(params: Record<string, any> = {}, getConfig?: Function) {
return new concreteFieldFormat(decorateMetaParams(params), getConfig);
if (fieldFormat) {
return class DecoratedFieldFormat extends fieldFormat {
static id = fieldFormat.id;
static fieldType = fieldFormat.fieldType;

constructor(params: Record<string, any> = {}, getConfig?: Function) {
super(getMetaParams(params), getConfig);
}
};
}
};

/**
* Build Meta Params
*
* @static
* @param {Record<string, any>} custom params
* @return {Record<string, any>}
*/
private buildMetaParams = (customParams: Record<string, any> = {}): Record<string, any> => ({
alexwizp marked this conversation as resolved.
Show resolved Hide resolved
parsedUrl: {
origin: window.location.origin,
pathname: window.location.pathname,
basePath: this.basePath,
},
...customParams,
});
}