Skip to content

Commit

Permalink
Fix browser date format (#57714)
Browse files Browse the repository at this point in the history
* fix browser date formatter
  • Loading branch information
mattkime committed Feb 20, 2020
1 parent c3001c4 commit dfd1959
Show file tree
Hide file tree
Showing 22 changed files with 174 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ import {
BoolFormat,
BytesFormat,
ColorFormat,
DateFormat,
DateNanosFormat,
DurationFormat,
IpFormat,
Expand All @@ -41,7 +40,6 @@ export const baseFormatters: IFieldFormatType[] = [
BoolFormat,
BytesFormat,
ColorFormat,
DateFormat,
DateNanosFormat,
DurationFormat,
IpFormat,
Expand Down
1 change: 0 additions & 1 deletion src/plugins/data/common/field_formats/converters/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@

export { UrlFormat } from './url';
export { BytesFormat } from './bytes';
export { DateFormat } from './date_server';
export { DateNanosFormat } from './date_nanos';
export { RelativeDateFormat } from './relative_date';
export { DurationFormat } from './duration';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,10 @@ export class FieldFormatsRegistry {
return undefined;
};

getTypeWithoutMetaParams = (formatId: FieldFormatId): IFieldFormatType | undefined => {
return this.fieldFormats.get(formatId);
};

/**
* Get the default FieldFormat type (class) for
* a field type, using the format:defaultTypeMap.
Expand Down
1 change: 0 additions & 1 deletion src/plugins/data/common/field_formats/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ export {
BoolFormat,
BytesFormat,
ColorFormat,
DateFormat,
DateNanosFormat,
DurationFormat,
IpFormat,
Expand Down
1 change: 1 addition & 0 deletions src/plugins/data/common/field_formats/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
*/

import { FieldFormat } from './field_format';
export { FieldFormat };

/** @public **/
export type FieldFormatsContentType = 'html' | 'text';
Expand Down
1 change: 1 addition & 0 deletions src/plugins/data/common/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,4 @@ export * from './timefilter/types';
export * from './query/types';
export * from './kbn_field_types/types';
export * from './index_patterns/types';
export { TextContextTypeConvert, IFieldFormatMetaParams } from './field_formats/types';
23 changes: 23 additions & 0 deletions src/plugins/data/public/field_formats/constants.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/*
* 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 { baseFormatters } from '../../common';
import { DateFormat } from './converters/date';

export const baseFormattersPublic = [DateFormat, ...baseFormatters];
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,12 @@
import { i18n } from '@kbn/i18n';
import { memoize, noop } from 'lodash';
import moment from 'moment';
import { KBN_FIELD_TYPES } from '../../kbn_field_types/types';
import { FieldFormat } from '../field_format';
import { TextContextTypeConvert, FIELD_FORMAT_IDS } from '../types';
import {
FieldFormat,
KBN_FIELD_TYPES,
TextContextTypeConvert,
FIELD_FORMAT_IDS,
} from '../../../common';

export class DateFormat extends FieldFormat {
static id = FIELD_FORMAT_IDS.DATE;
Expand Down
20 changes: 20 additions & 0 deletions src/plugins/data/public/field_formats/converters/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/*
* 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.
*/

export { DateFormat } from './date';
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/*
* 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 { FieldFormatsService } from './field_formats_service';
import { coreMock } from '../../../../../src/core/public/mocks';
import { DateFormat } from './converters/date';

describe('FieldFormatService', () => {
test('DateFormat is public version', () => {
const mockCore = coreMock.createSetup();
const service = new FieldFormatsService();
service.setup(mockCore);
const fieldFormatsRegistry = service.start();
const DateFormatFromRegsitry = fieldFormatsRegistry.getTypeWithoutMetaParams('date');

expect(DateFormatFromRegsitry).toEqual(DateFormat);
});
});
19 changes: 12 additions & 7 deletions src/plugins/data/public/field_formats/field_formats_service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,10 @@
*/

import { CoreSetup } from 'src/core/public';
import { FieldFormatsRegistry } from '../../common/field_formats';
import { FieldFormatsRegistry } from '../../common';
import { deserializeFieldFormat } from './utils/deserialize';
import { FormatFactory } from '../../common/field_formats/utils';
import { baseFormattersPublic } from './constants';

export class FieldFormatsService {
private readonly fieldFormatsRegistry: FieldFormatsRegistry = new FieldFormatsRegistry();
Expand All @@ -34,13 +35,17 @@ export class FieldFormatsService {

const getConfig = core.uiSettings.get.bind(core.uiSettings);

this.fieldFormatsRegistry.init(getConfig, {
parsedUrl: {
origin: window.location.origin,
pathname: window.location.pathname,
basePath: core.http.basePath.get(),
this.fieldFormatsRegistry.init(
getConfig,
{
parsedUrl: {
origin: window.location.origin,
pathname: window.location.pathname,
basePath: core.http.basePath.get(),
},
},
});
baseFormattersPublic
);

return this.fieldFormatsRegistry as FieldFormatsSetup;
}
Expand Down
2 changes: 2 additions & 0 deletions src/plugins/data/public/field_formats/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,5 @@
*/

export { FieldFormatsService, FieldFormatsSetup, FieldFormatsStart } from './field_formats_service';
export { DateFormat } from './converters';
export { baseFormattersPublic } from './constants';
4 changes: 3 additions & 1 deletion src/plugins/data/public/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,6 @@ import {
BoolFormat,
BytesFormat,
ColorFormat,
DateFormat,
DateNanosFormat,
DurationFormat,
IpFormat,
Expand All @@ -171,6 +170,9 @@ import {
serializeFieldFormat,
} from '../common/field_formats';

import { DateFormat } from './field_formats';
export { baseFormattersPublic } from './field_formats';

// Field formats helpers namespace:
export const fieldFormats = {
FieldFormat,
Expand Down
1 change: 1 addition & 0 deletions src/plugins/data/public/mocks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ const fieldFormatsMock: IFieldFormatsRegistry = {
register: jest.fn(),
parseDefaultTypeMap: jest.fn(),
deserialize: jest.fn(),
getTypeWithoutMetaParams: jest.fn(),
};

const createSetupContract = (): Setup => {
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,14 @@
import { i18n } from '@kbn/i18n';
import { memoize, noop } from 'lodash';
import moment from 'moment-timezone';
import { KBN_FIELD_TYPES } from '../../kbn_field_types/types';
import { FieldFormat } from '../field_format';
import {
FieldFormat,
KBN_FIELD_TYPES,
TextContextTypeConvert,
FIELD_FORMAT_IDS,
FieldFormatsGetConfigFn,
IFieldFormatMetaParams,
} from '../types';
} from '../../../common';

export class DateFormat extends FieldFormat {
static id = FIELD_FORMAT_IDS.DATE;
Expand Down
20 changes: 20 additions & 0 deletions src/plugins/data/server/field_formats/converters/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/*
* 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.
*/

export { DateFormat } from './date_server';
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/*
* 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 { FieldFormatsService } from './field_formats_service';
import { DateFormat } from './converters/date_server';
import { coreMock } from '../../../../core/server/mocks';

describe('FieldFormatService', () => {
test('DateFormat is server version', async () => {
const service = new FieldFormatsService();
const fieldFormatsService = await service.start();
const uiSettings = coreMock.createStart().uiSettings.asScopedToClient({} as any);
const fieldFormatsRegistry = await fieldFormatsService.fieldFormatServiceFactory(uiSettings);
const DateFormatFromRegsitry = fieldFormatsRegistry.getTypeWithoutMetaParams('date');

expect(DateFormatFromRegsitry).toEqual(DateFormat);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,10 @@
import { has } from 'lodash';
import { FieldFormatsRegistry, IFieldFormatType, baseFormatters } from '../../common/field_formats';
import { IUiSettingsClient } from '../../../../core/server';
import { DateFormat } from './converters';

export class FieldFormatsService {
private readonly fieldFormatClasses: IFieldFormatType[] = baseFormatters;
private readonly fieldFormatClasses: IFieldFormatType[] = [DateFormat, ...baseFormatters];

public setup() {
return {
Expand Down
4 changes: 0 additions & 4 deletions src/plugins/data/server/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,6 @@ import {
BoolFormat,
BytesFormat,
ColorFormat,
DateFormat,
DateNanosFormat,
DurationFormat,
IpFormat,
Expand All @@ -101,13 +100,10 @@ import {
export const fieldFormats = {
FieldFormatsRegistry,
FieldFormat,

serializeFieldFormat,

BoolFormat,
BytesFormat,
ColorFormat,
DateFormat,
DateNanosFormat,
DurationFormat,
IpFormat,
Expand Down
3 changes: 2 additions & 1 deletion src/test_utils/public/stub_field_formats.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,13 @@
import { CoreSetup } from 'kibana/public';
import { DataPublicPluginStart, fieldFormats } from '../../plugins/data/public';
import { deserializeFieldFormat } from '../../plugins/data/public/field_formats/utils/deserialize';
import { baseFormattersPublic } from '../../plugins/data/public';

export const getFieldFormatsRegistry = (core: CoreSetup) => {
const fieldFormatsRegistry = new fieldFormats.FieldFormatsRegistry();
const getConfig = core.uiSettings.get.bind(core.uiSettings);

fieldFormatsRegistry.init(getConfig, {});
fieldFormatsRegistry.init(getConfig, {}, baseFormattersPublic);

fieldFormatsRegistry.deserialize = deserializeFieldFormat.bind(
fieldFormatsRegistry as DataPublicPluginStart['fieldFormats']
Expand Down

0 comments on commit dfd1959

Please sign in to comment.