From 8c8e8ac36b1776fb167a1ac848c04efd45a4fedf Mon Sep 17 00:00:00 2001 From: Artyom Gospodarsky Date: Tue, 22 Oct 2019 11:31:05 +0300 Subject: [PATCH] Remove extra Field Format wrappers and move into data plugin (#47948) (#48787) * Remove extra wrappers * Move Field Format utils into data plugin * Change i18n prefix * Add a new alias to i18n * Fix issues * Make Numeral utils on their own * Enhance Numeral interface * Revert Percent changes * Add common Numeral for Number, Bite, Percent * Fix i18n for some utils * Remove test duplicates * Remove test duplicates * Make a common import public path * Reimport default color * Change paths for server modules * Change default color constant name --- .i18nrc.json | 2 +- .../common/field_formats/types/_numeral.ts | 87 ------- .../common/field_formats/types/boolean.ts | 54 ----- .../common/field_formats/types/color.ts | 75 ------ .../kibana/common/field_formats/types/date.ts | 82 ------- .../common/field_formats/types/date_server.ts | 92 -------- .../common/field_formats/types/source.ts | 83 ------- .../common/field_formats/types/string.ts | 136 ----------- .../kibana/common/field_formats/types/url.ts | 205 ---------------- .../field_formats/__tests__/_boolean.js | 76 ------ .../public/field_formats/__tests__/_color.js | 90 ------- .../public/field_formats/__tests__/_date.js | 71 ------ .../field_formats/__tests__/_duration.js | 62 ----- .../public/field_formats/__tests__/_source.js | 60 ----- .../public/field_formats/__tests__/_string.js | 71 ------ .../field_formats/__tests__/_truncate.js | 52 ----- .../public/field_formats/__tests__/_url.js | 221 ------------------ .../kibana/public/field_formats/register.js | 62 ++--- .../kibana/server/field_formats/register.js | 62 ++--- .../mixin/field_formats_mixin.ts | 8 +- .../mixin/field_formats_service.test.ts | 5 +- .../mixin/field_formats_service.ts | 6 +- .../editors/color/color.js | 4 +- .../editors/color/color.test.js | 4 +- .../lib/__tests__/get_default_format.test.js | 9 +- .../ui/public/registry/field_formats.js | 7 +- .../field_formats/constants}/color_default.ts | 2 +- .../common/field_formats/constants/index.ts} | 13 +- .../field_formats/converters}/boolean.test.ts | 4 +- .../field_formats/converters/boolean.ts | 50 ++++ .../field_formats/converters}/bytes.test.ts | 4 +- .../common/field_formats/converters}/bytes.ts | 13 +- .../field_formats/converters}/color.test.ts | 6 +- .../common/field_formats/converters/color.ts | 69 ++++++ .../field_formats/converters}/date.test.ts | 4 +- .../common/field_formats/converters/date.ts | 76 ++++++ .../converters}/date_nanos.test.ts | 6 +- .../field_formats/converters}/date_nanos.ts | 106 ++++----- .../field_formats/converters/date_server.ts | 88 +++++++ .../converters}/duration.test.ts | 4 +- .../field_formats/converters}/duration.ts | 102 ++++---- .../common/field_formats/converters/index.ts} | 37 ++- .../field_formats/converters}/ip.test.ts | 4 +- .../common/field_formats/converters/ip.ts} | 28 +-- .../field_formats/converters}/number.test.ts | 4 +- .../field_formats/converters}/number.ts | 13 +- .../field_formats/converters/numeral.ts | 74 ++++++ .../field_formats/converters}/percent.test.ts | 4 +- .../field_formats/converters}/percent.ts | 34 +-- .../converters}/relative_date.test.ts | 4 +- .../converters}/relative_date.ts | 48 ++-- .../field_formats/converters/source.test.ts | 45 ++++ .../common/field_formats/converters/source.ts | 76 ++++++ .../converters}/static_lookup.ts | 50 ++-- .../field_formats/converters}/string.test.ts | 4 +- .../common/field_formats/converters/string.ts | 130 +++++++++++ .../converters}/truncate.test.ts | 4 +- .../field_formats/converters}/truncate.ts | 40 ++-- .../field_formats/converters}/url.test.ts | 6 +- .../common/field_formats/converters/url.ts | 202 ++++++++++++++++ .../data/common/field_formats/index.ts | 2 + .../legacy/plugins/apm/typings/numeral.d.ts | 2 + .../csv/server/__tests__/execute_job.js | 4 +- .../server/lib/__tests__/field_format_map.js | 5 +- 64 files changed, 1146 insertions(+), 1907 deletions(-) delete mode 100644 src/legacy/core_plugins/kibana/common/field_formats/types/_numeral.ts delete mode 100644 src/legacy/core_plugins/kibana/common/field_formats/types/boolean.ts delete mode 100644 src/legacy/core_plugins/kibana/common/field_formats/types/color.ts delete mode 100644 src/legacy/core_plugins/kibana/common/field_formats/types/date.ts delete mode 100644 src/legacy/core_plugins/kibana/common/field_formats/types/date_server.ts delete mode 100644 src/legacy/core_plugins/kibana/common/field_formats/types/source.ts delete mode 100644 src/legacy/core_plugins/kibana/common/field_formats/types/string.ts delete mode 100644 src/legacy/core_plugins/kibana/common/field_formats/types/url.ts delete mode 100644 src/legacy/core_plugins/kibana/public/field_formats/__tests__/_boolean.js delete mode 100644 src/legacy/core_plugins/kibana/public/field_formats/__tests__/_color.js delete mode 100644 src/legacy/core_plugins/kibana/public/field_formats/__tests__/_date.js delete mode 100644 src/legacy/core_plugins/kibana/public/field_formats/__tests__/_duration.js delete mode 100644 src/legacy/core_plugins/kibana/public/field_formats/__tests__/_source.js delete mode 100644 src/legacy/core_plugins/kibana/public/field_formats/__tests__/_string.js delete mode 100644 src/legacy/core_plugins/kibana/public/field_formats/__tests__/_truncate.js delete mode 100644 src/legacy/core_plugins/kibana/public/field_formats/__tests__/_url.js rename src/{legacy/core_plugins/kibana/common/field_formats/types => plugins/data/common/field_formats/constants}/color_default.ts (95%) rename src/{legacy/core_plugins/kibana/public/field_formats/__tests__/index.js => plugins/data/common/field_formats/constants/index.ts} (76%) rename src/{legacy/core_plugins/kibana/common/field_formats/types => plugins/data/common/field_formats/converters}/boolean.test.ts (95%) create mode 100644 src/plugins/data/common/field_formats/converters/boolean.ts rename src/{legacy/core_plugins/kibana/common/field_formats/types => plugins/data/common/field_formats/converters}/bytes.test.ts (93%) rename src/{legacy/core_plugins/kibana/common/field_formats/types => plugins/data/common/field_formats/converters}/bytes.ts (80%) rename src/{legacy/core_plugins/kibana/common/field_formats/types => plugins/data/common/field_formats/converters}/color.test.ts (96%) create mode 100644 src/plugins/data/common/field_formats/converters/color.ts rename src/{legacy/core_plugins/kibana/common/field_formats/types => plugins/data/common/field_formats/converters}/date.test.ts (96%) create mode 100644 src/plugins/data/common/field_formats/converters/date.ts rename src/{legacy/core_plugins/kibana/common/field_formats/types => plugins/data/common/field_formats/converters}/date_nanos.test.ts (95%) rename src/{legacy/core_plugins/kibana/common/field_formats/types => plugins/data/common/field_formats/converters}/date_nanos.ts (56%) create mode 100644 src/plugins/data/common/field_formats/converters/date_server.ts rename src/{legacy/core_plugins/kibana/common/field_formats/types => plugins/data/common/field_formats/converters}/duration.test.ts (96%) rename src/{legacy/core_plugins/kibana/common/field_formats/types => plugins/data/common/field_formats/converters}/duration.ts (50%) rename src/{legacy/core_plugins/kibana/common/field_formats/types/ip.ts => plugins/data/common/field_formats/converters/index.ts} (55%) rename src/{legacy/core_plugins/kibana/common/field_formats/types => plugins/data/common/field_formats/converters}/ip.test.ts (94%) rename src/{legacy/core_plugins/kibana/public/field_formats/__tests__/_ip.js => plugins/data/common/field_formats/converters/ip.ts} (57%) rename src/{legacy/core_plugins/kibana/common/field_formats/types => plugins/data/common/field_formats/converters}/number.test.ts (93%) rename src/{legacy/core_plugins/kibana/common/field_formats/types => plugins/data/common/field_formats/converters}/number.ts (80%) create mode 100644 src/plugins/data/common/field_formats/converters/numeral.ts rename src/{legacy/core_plugins/kibana/common/field_formats/types => plugins/data/common/field_formats/converters}/percent.test.ts (93%) rename src/{legacy/core_plugins/kibana/common/field_formats/types => plugins/data/common/field_formats/converters}/percent.ts (57%) rename src/{legacy/core_plugins/kibana/common/field_formats/types => plugins/data/common/field_formats/converters}/relative_date.test.ts (92%) rename src/{legacy/core_plugins/kibana/common/field_formats/types => plugins/data/common/field_formats/converters}/relative_date.ts (55%) create mode 100644 src/plugins/data/common/field_formats/converters/source.test.ts create mode 100644 src/plugins/data/common/field_formats/converters/source.ts rename src/{legacy/core_plugins/kibana/common/field_formats/types => plugins/data/common/field_formats/converters}/static_lookup.ts (56%) rename src/{legacy/core_plugins/kibana/common/field_formats/types => plugins/data/common/field_formats/converters}/string.test.ts (96%) create mode 100644 src/plugins/data/common/field_formats/converters/string.ts rename src/{legacy/core_plugins/kibana/common/field_formats/types => plugins/data/common/field_formats/converters}/truncate.test.ts (94%) rename src/{legacy/core_plugins/kibana/common/field_formats/types => plugins/data/common/field_formats/converters}/truncate.ts (58%) rename src/{legacy/core_plugins/kibana/common/field_formats/types => plugins/data/common/field_formats/converters}/url.test.ts (98%) create mode 100644 src/plugins/data/common/field_formats/converters/url.ts diff --git a/.i18nrc.json b/.i18nrc.json index a83f57107577..a225498bb409 100644 --- a/.i18nrc.json +++ b/.i18nrc.json @@ -1,7 +1,7 @@ { "paths": { "common.ui": "src/legacy/ui", - "data": "src/legacy/core_plugins/data", + "data": ["src/legacy/core_plugins/data", "src/plugins/data"], "expressions": "src/legacy/core_plugins/expressions", "kibana_react": "src/legacy/core_plugins/kibana_react", "server": "src/legacy/server", diff --git a/src/legacy/core_plugins/kibana/common/field_formats/types/_numeral.ts b/src/legacy/core_plugins/kibana/common/field_formats/types/_numeral.ts deleted file mode 100644 index 0d39075a1dec..000000000000 --- a/src/legacy/core_plugins/kibana/common/field_formats/types/_numeral.ts +++ /dev/null @@ -1,87 +0,0 @@ -/* - * 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. - */ - -// @ts-ignore -import numeral from '@elastic/numeral'; -// @ts-ignore -import numeralLanguages from '@elastic/numeral/languages'; -import { assign, has } from 'lodash'; -import { - FieldFormat, - KBN_FIELD_TYPES, - TextContextTypeConvert, -} from '../../../../../../plugins/data/common/'; - -const numeralInst = numeral(); - -numeralLanguages.forEach(function(numeralLanguage: Record) { - numeral.language(numeralLanguage.id, numeralLanguage.lang); -}); - -export function createNumeralFormat(opts: Record) { - class NumeralFormat extends FieldFormat { - static id = opts.id; - static title = opts.title; - static fieldType = KBN_FIELD_TYPES.NUMBER; - - private getConfig: Function; - - constructor(params: Record, getConfig: Function) { - super(params); - this.getConfig = getConfig; - } - - getParamDefaults() { - if (has(opts, 'getParamDefaults')) { - return opts.getParamDefaults(this.getConfig); - } - - return { - pattern: this.getConfig(`format:${opts.id}:defaultPattern`), - }; - } - - textConvert: TextContextTypeConvert = val => { - if (val === -Infinity) return '-∞'; - if (val === +Infinity) return '+∞'; - if (typeof val !== 'number') { - val = parseFloat(val); - } - - if (isNaN(val)) return ''; - - const previousLocale = numeral.language(); - const defaultLocale = - (this.getConfig && this.getConfig('format:number:defaultLocale')) || 'en'; - numeral.language(defaultLocale); - - const formatted = numeralInst.set(val).format(this.param('pattern')); - - numeral.language(previousLocale); - - return opts.afterConvert ? opts.afterConvert.call(this, formatted) : formatted; - }; - } - - if (opts.prototype) { - assign(NumeralFormat.prototype, opts.prototype); - } - - return NumeralFormat; -} diff --git a/src/legacy/core_plugins/kibana/common/field_formats/types/boolean.ts b/src/legacy/core_plugins/kibana/common/field_formats/types/boolean.ts deleted file mode 100644 index 840bf20a9af3..000000000000 --- a/src/legacy/core_plugins/kibana/common/field_formats/types/boolean.ts +++ /dev/null @@ -1,54 +0,0 @@ -/* - * 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 { - FieldFormat, - asPrettyString, - KBN_FIELD_TYPES, - TextContextTypeConvert, -} from '../../../../../../plugins/data/common'; - -export function createBoolFormat() { - return class BoolFormat extends FieldFormat { - static id = 'boolean'; - static title = 'Boolean'; - static fieldType = [KBN_FIELD_TYPES.BOOLEAN, KBN_FIELD_TYPES.NUMBER, KBN_FIELD_TYPES.STRING]; - - textConvert: TextContextTypeConvert = value => { - if (typeof value === 'string') { - value = value.trim().toLowerCase(); - } - - switch (value) { - case false: - case 0: - case 'false': - case 'no': - return 'false'; - case true: - case 1: - case 'true': - case 'yes': - return 'true'; - default: - return asPrettyString(value); - } - }; - }; -} diff --git a/src/legacy/core_plugins/kibana/common/field_formats/types/color.ts b/src/legacy/core_plugins/kibana/common/field_formats/types/color.ts deleted file mode 100644 index 065918c6bbdf..000000000000 --- a/src/legacy/core_plugins/kibana/common/field_formats/types/color.ts +++ /dev/null @@ -1,75 +0,0 @@ -/* - * 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 { findLast, cloneDeep, template, escape } from 'lodash'; -import { - asPrettyString, - FieldFormat, - KBN_FIELD_TYPES, - HtmlContextTypeConvert, -} from '../../../../../../plugins/data/common/'; -import { DEFAULT_COLOR } from './color_default'; - -const convertTemplate = template('<%- val %>'); - -export function createColorFormat() { - class ColorFormat extends FieldFormat { - static id = 'color'; - static title = 'Color'; - static fieldType = [KBN_FIELD_TYPES.NUMBER, KBN_FIELD_TYPES.STRING]; - - getParamDefaults() { - return { - fieldType: null, // populated by editor, see controller below - colors: [cloneDeep(DEFAULT_COLOR)], - }; - } - - findColorRuleForVal(val: any) { - switch (this.param('fieldType')) { - case 'string': - return findLast(this.param('colors'), (colorParam: typeof DEFAULT_COLOR) => { - return new RegExp(colorParam.regex).test(val); - }); - - case 'number': - return findLast(this.param('colors'), ({ range }) => { - if (!range) return; - const [start, end] = range.split(':'); - return val >= Number(start) && val <= Number(end); - }); - - default: - return null; - } - } - - htmlConvert: HtmlContextTypeConvert = val => { - const color = this.findColorRuleForVal(val) as typeof DEFAULT_COLOR; - if (!color) return escape(asPrettyString(val)); - - let style = ''; - if (color.text) style += `color: ${color.text};`; - if (color.background) style += `background-color: ${color.background};`; - return convertTemplate({ val, style }); - }; - } - - return ColorFormat; -} diff --git a/src/legacy/core_plugins/kibana/common/field_formats/types/date.ts b/src/legacy/core_plugins/kibana/common/field_formats/types/date.ts deleted file mode 100644 index b04a1729ee13..000000000000 --- a/src/legacy/core_plugins/kibana/common/field_formats/types/date.ts +++ /dev/null @@ -1,82 +0,0 @@ -/* - * 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 { memoize, noop } from 'lodash'; -import moment from 'moment'; -import { - FieldFormat, - KBN_FIELD_TYPES, - TextContextTypeConvert, -} from '../../../../../../plugins/data/common/'; - -export function createDateFormat() { - return class DateFormat extends FieldFormat { - static id = 'date'; - static title = 'Date'; - static fieldType = KBN_FIELD_TYPES.DATE; - - private getConfig: Function; - private memoizedConverter: Function = noop; - private memoizedPattern: string = ''; - private timeZone: string = ''; - - constructor(params: Record, getConfig: Function) { - super(params); - - this.getConfig = getConfig; - } - - getParamDefaults() { - return { - pattern: this.getConfig('dateFormat'), - timezone: this.getConfig('dateFormat:tz'), - }; - } - - textConvert: TextContextTypeConvert = val => { - // don't give away our ref to converter so - // we can hot-swap when config changes - const pattern = this.param('pattern'); - const timezone = this.param('timezone'); - - const timezoneChanged = this.timeZone !== timezone; - const datePatternChanged = this.memoizedPattern !== pattern; - if (timezoneChanged || datePatternChanged) { - this.timeZone = timezone; - this.memoizedPattern = pattern; - - this.memoizedConverter = memoize(function converter(value: any) { - if (value === null || value === undefined) { - return '-'; - } - - const date = moment(value); - - if (date.isValid()) { - return date.format(pattern); - } else { - return value; - } - }); - } - - return this.memoizedConverter(val); - }; - }; -} diff --git a/src/legacy/core_plugins/kibana/common/field_formats/types/date_server.ts b/src/legacy/core_plugins/kibana/common/field_formats/types/date_server.ts deleted file mode 100644 index fa6bf8b26147..000000000000 --- a/src/legacy/core_plugins/kibana/common/field_formats/types/date_server.ts +++ /dev/null @@ -1,92 +0,0 @@ -/* - * 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 { memoize, noop } from 'lodash'; -import moment from 'moment-timezone'; -import { - FieldFormat, - KBN_FIELD_TYPES, - TextContextTypeConvert, -} from '../../../../../../plugins/data/common/'; - -export function createDateOnServerFormat() { - return class DateFormat extends FieldFormat { - static id = 'date'; - static title = 'Date'; - static fieldType = KBN_FIELD_TYPES.DATE; - - private getConfig: Function; - private memoizedConverter: Function = noop; - private memoizedPattern: string = ''; - private timeZone: string = ''; - - constructor(params: Record, getConfig: Function) { - super(params); - - this.getConfig = getConfig; - this.memoizedConverter = memoize((val: any) => { - if (val == null) { - return '-'; - } - - /* On the server, importing moment returns a new instance. Unlike on - * the client side, it doesn't have the dateFormat:tz configuration - * baked in. - * We need to set the timezone manually here. The date is taken in as - * UTC and converted into the desired timezone. */ - let date; - if (this.timeZone === 'Browser') { - // Assume a warning has been logged this can be unpredictable. It - // would be too verbose to log anything here. - date = moment.utc(val); - } else { - date = moment.utc(val).tz(this.timeZone); - } - - if (date.isValid()) { - return date.format(this.memoizedPattern); - } else { - return val; - } - }); - } - - getParamDefaults() { - return { - pattern: this.getConfig('dateFormat'), - timezone: this.getConfig('dateFormat:tz'), - }; - } - - textConvert: TextContextTypeConvert = val => { - // don't give away our ref to converter so we can hot-swap when config changes - const pattern = this.param('pattern'); - const timezone = this.param('timezone'); - - const timezoneChanged = this.timeZone !== timezone; - const datePatternChanged = this.memoizedPattern !== pattern; - if (timezoneChanged || datePatternChanged) { - this.timeZone = timezone; - this.memoizedPattern = pattern; - } - - return this.memoizedConverter(val); - }; - }; -} diff --git a/src/legacy/core_plugins/kibana/common/field_formats/types/source.ts b/src/legacy/core_plugins/kibana/common/field_formats/types/source.ts deleted file mode 100644 index 4d73203696bc..000000000000 --- a/src/legacy/core_plugins/kibana/common/field_formats/types/source.ts +++ /dev/null @@ -1,83 +0,0 @@ -/* - * 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 { template, escape, keys } from 'lodash'; -// @ts-ignore -import { noWhiteSpace } from '../../utils/no_white_space'; -// @ts-ignore -import { shortenDottedString } from '../../utils/shorten_dotted_string'; -import { - FieldFormat, - KBN_FIELD_TYPES, - TextContextTypeConvert, - HtmlContextTypeConvert, -} from '../../../../../../plugins/data/common/'; - -const templateHtml = ` -
- <% defPairs.forEach(function (def) { %> -
<%- def[0] %>:
-
<%= def[1] %>
- <%= ' ' %> - <% }); %> -
`; -const doTemplate = template(noWhiteSpace(templateHtml)); - -export function createSourceFormat() { - class SourceFormat extends FieldFormat { - static id = '_source'; - static title = '_source'; - static fieldType = KBN_FIELD_TYPES._SOURCE; - - private getConfig: Function; - - constructor(params: Record, getConfig: Function) { - super(params); - - this.getConfig = getConfig; - } - - textConvert: TextContextTypeConvert = value => JSON.stringify(value); - - htmlConvert: HtmlContextTypeConvert = (value, field, hit) => { - if (!field) { - const converter = this.getConverterFor('text') as Function; - - return escape(converter(value)); - } - - const highlights = (hit && hit.highlight) || {}; - const formatted = field.indexPattern.formatHit(hit); - const highlightPairs: any[] = []; - const sourcePairs: any[] = []; - const isShortDots = this.getConfig('shortDots:enable'); - - keys(formatted).forEach(key => { - const pairs = highlights[key] ? highlightPairs : sourcePairs; - const newField = isShortDots ? shortenDottedString(key) : key; - const val = formatted[key]; - pairs.push([newField, val]); - }, []); - - return doTemplate({ defPairs: highlightPairs.concat(sourcePairs) }); - }; - } - - return SourceFormat; -} diff --git a/src/legacy/core_plugins/kibana/common/field_formats/types/string.ts b/src/legacy/core_plugins/kibana/common/field_formats/types/string.ts deleted file mode 100644 index c41b94cab7cf..000000000000 --- a/src/legacy/core_plugins/kibana/common/field_formats/types/string.ts +++ /dev/null @@ -1,136 +0,0 @@ -/* - * 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 { i18n } from '@kbn/i18n'; -import { asPrettyString } from '../../../../../../plugins/data/common/field_formats'; -import { - FieldFormat, - KBN_FIELD_TYPES, - TextContextTypeConvert, -} from '../../../../../../plugins/data/common/'; -// @ts-ignore -import { shortenDottedString } from '../../utils/shorten_dotted_string'; - -const TRANSFORM_OPTIONS = [ - { - kind: false, - text: i18n.translate('kbn.common.fieldFormats.string.transformOptions.none', { - defaultMessage: '- None -', - }), - }, - { - kind: 'lower', - text: i18n.translate('kbn.common.fieldFormats.string.transformOptions.lower', { - defaultMessage: 'Lower Case', - }), - }, - { - kind: 'upper', - text: i18n.translate('kbn.common.fieldFormats.string.transformOptions.upper', { - defaultMessage: 'Upper Case', - }), - }, - { - kind: 'title', - text: i18n.translate('kbn.common.fieldFormats.string.transformOptions.title', { - defaultMessage: 'Title Case', - }), - }, - { - kind: 'short', - text: i18n.translate('kbn.common.fieldFormats.string.transformOptions.short', { - defaultMessage: 'Short Dots', - }), - }, - { - kind: 'base64', - text: i18n.translate('kbn.common.fieldFormats.string.transformOptions.base64', { - defaultMessage: 'Base64 Decode', - }), - }, - { - kind: 'urlparam', - text: i18n.translate('kbn.common.fieldFormats.string.transformOptions.url', { - defaultMessage: 'URL Param Decode', - }), - }, -]; -const DEFAULT_TRANSFORM_OPTION = false; - -export function createStringFormat() { - class StringFormat extends FieldFormat { - static id = 'string'; - static title = 'String'; - static fieldType = [ - KBN_FIELD_TYPES.NUMBER, - KBN_FIELD_TYPES.BOOLEAN, - KBN_FIELD_TYPES.DATE, - KBN_FIELD_TYPES.IP, - KBN_FIELD_TYPES.ATTACHMENT, - KBN_FIELD_TYPES.GEO_POINT, - KBN_FIELD_TYPES.GEO_SHAPE, - KBN_FIELD_TYPES.STRING, - KBN_FIELD_TYPES.MURMUR3, - KBN_FIELD_TYPES.UNKNOWN, - KBN_FIELD_TYPES.CONFLICT, - ]; - static transformOptions = TRANSFORM_OPTIONS; - - getParamDefaults() { - return { - transform: DEFAULT_TRANSFORM_OPTION, - }; - } - - private base64Decode(val: string) { - try { - return Buffer.from(val, 'base64').toString('utf8'); - } catch (e) { - return asPrettyString(val); - } - } - - private toTitleCase(val: string) { - return val.replace(/\w\S*/g, txt => { - return txt.charAt(0).toUpperCase() + txt.substr(1).toLowerCase(); - }); - } - - textConvert: TextContextTypeConvert = val => { - switch (this.param('transform')) { - case 'lower': - return String(val).toLowerCase(); - case 'upper': - return String(val).toUpperCase(); - case 'title': - return this.toTitleCase(val); - case 'short': - return shortenDottedString(val); - case 'base64': - return this.base64Decode(val); - case 'urlparam': - return decodeURIComponent(val); - default: - return asPrettyString(val); - } - }; - } - - return StringFormat; -} diff --git a/src/legacy/core_plugins/kibana/common/field_formats/types/url.ts b/src/legacy/core_plugins/kibana/common/field_formats/types/url.ts deleted file mode 100644 index b5be60be05bb..000000000000 --- a/src/legacy/core_plugins/kibana/common/field_formats/types/url.ts +++ /dev/null @@ -1,205 +0,0 @@ -/* - * 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 { i18n } from '@kbn/i18n'; -import { escape, memoize } from 'lodash'; -import { - getHighlightHtml, - FieldFormat, - KBN_FIELD_TYPES, - TextContextTypeConvert, - HtmlContextTypeConvert, -} from '../../../../../../plugins/data/common/'; - -const templateMatchRE = /{{([\s\S]+?)}}/g; -const whitelistUrlSchemes = ['http://', 'https://']; - -const URL_TYPES = [ - { - kind: 'a', - text: i18n.translate('kbn.common.fieldFormats.url.types.link', { - defaultMessage: 'Link', - }), - }, - { - kind: 'img', - text: i18n.translate('kbn.common.fieldFormats.url.types.img', { - defaultMessage: 'Image', - }), - }, - { - kind: 'audio', - text: i18n.translate('kbn.common.fieldFormats.url.types.audio', { - defaultMessage: 'Audio', - }), - }, -]; -const DEFAULT_URL_TYPE = 'a'; - -export function createUrlFormat() { - class UrlFormat extends FieldFormat { - static id = 'url'; - static title = 'Url'; - static fieldType = [ - KBN_FIELD_TYPES.NUMBER, - KBN_FIELD_TYPES.BOOLEAN, - KBN_FIELD_TYPES.DATE, - KBN_FIELD_TYPES.IP, - KBN_FIELD_TYPES.STRING, - KBN_FIELD_TYPES.MURMUR3, - KBN_FIELD_TYPES.UNKNOWN, - KBN_FIELD_TYPES.CONFLICT, - ]; - static urlTypes = URL_TYPES; - - constructor(params: Record) { - super(params); - this.compileTemplate = memoize(this.compileTemplate); - } - - getParamDefaults() { - return { - type: DEFAULT_URL_TYPE, - urlTemplate: null, - labelTemplate: null, - }; - } - - private formatLabel(value: string, url?: string): string { - const template = this.param('labelTemplate'); - if (url == null) url = this.formatUrl(value); - if (!template) return url; - - return this.compileTemplate(template)({ - value, - url, - }); - } - - private formatUrl(value: string): string { - const template = this.param('urlTemplate'); - if (!template) return value; - - return this.compileTemplate(template)({ - value: encodeURIComponent(value), - rawValue: value, - }); - } - - private compileTemplate(template: string): Function { - // trim all the odd bits, the variable names - const parts = template.split(templateMatchRE).map((part, i) => (i % 2 ? part.trim() : part)); - - return function(locals: Record): string { - // replace all the odd bits with their local var - let output = ''; - let i = -1; - while (++i < parts.length) { - if (i % 2) { - if (locals.hasOwnProperty(parts[i])) { - const local = locals[parts[i]]; - output += local == null ? '' : local; - } - } else { - output += parts[i]; - } - } - - return output; - }; - } - - private generateImgHtml(url: string, imageLabel: string): string { - const isValidWidth = !isNaN(parseInt(this.param('width'), 10)); - const isValidHeight = !isNaN(parseInt(this.param('height'), 10)); - const maxWidth = isValidWidth ? `${this.param('width')}px` : 'none'; - const maxHeight = isValidHeight ? `${this.param('height')}px` : 'none'; - - return `${imageLabel}`; - } - - textConvert: TextContextTypeConvert = value => this.formatLabel(value); - - htmlConvert: HtmlContextTypeConvert = (rawValue, field, hit, parsedUrl) => { - const url = escape(this.formatUrl(rawValue)); - const label = escape(this.formatLabel(rawValue, url)); - - switch (this.param('type')) { - case 'audio': - return `