diff --git a/src/legacy/core_plugins/kibana/common/field_formats/types/__tests__/url.js b/src/legacy/core_plugins/kibana/common/field_formats/types/__tests__/url.js index ff847bea4bee6..74172dbcce314 100644 --- a/src/legacy/core_plugins/kibana/common/field_formats/types/__tests__/url.js +++ b/src/legacy/core_plugins/kibana/common/field_formats/types/__tests__/url.js @@ -39,11 +39,54 @@ describe('UrlFormat', function () { .to.be(''); }); - it('outputs an if type === "img"', function () { - const url = new UrlFormat({ type: 'img' }); + describe('outputs an if type === "img"', function () { + it('default', function () { + const url = new UrlFormat({ type: 'img' }); - expect(url.convert('http://elastic.co', 'html')) - .to.be('A dynamically-specified image located at http://elastic.co'); + expect(url.convert('http://elastic.co', 'html')) + .to.be('A dynamically-specified image located at http://elastic.co'); + }); + + it('with correct width and height set', function () { + const url = new UrlFormat({ type: 'img', width: '12', height: '55' }); + + expect(url.convert('http://elastic.co', 'html')) + .to.be('A dynamically-specified image located at http://elastic.co'); + }); + + it('with correct width and height set if no width specified', function () { + const url = new UrlFormat({ type: 'img', height: '55' }); + + expect(url.convert('http://elastic.co', 'html')) + .to.be('A dynamically-specified image located at http://elastic.co'); + }); + + it('with correct width and height set if no height specified', function () { + const url = new UrlFormat({ type: 'img', width: '22' }); + + expect(url.convert('http://elastic.co', 'html')) + .to.be('A dynamically-specified image located at http://elastic.co'); + }); + + it('only accepts valid numbers for width', function () { + const url = new UrlFormat({ type: 'img', width: 'not a number' }); + + expect(url.convert('http://elastic.co', 'html')) + .to.be('A dynamically-specified image located at http://elastic.co'); + }); + + it('only accepts valid numbers for height', function () { + const url = new UrlFormat({ type: 'img', height: 'not a number' }); + + expect(url.convert('http://elastic.co', 'html')) + .to.be('A dynamically-specified image located at http://elastic.co'); + }); }); describe('url template', function () { diff --git a/src/legacy/core_plugins/kibana/common/field_formats/types/url.js b/src/legacy/core_plugins/kibana/common/field_formats/types/url.js index 63924727ab8bb..11036625776cb 100644 --- a/src/legacy/core_plugins/kibana/common/field_formats/types/url.js +++ b/src/legacy/core_plugins/kibana/common/field_formats/types/url.js @@ -41,7 +41,9 @@ export function createUrlFormat(FieldFormat) { return { type: DEFAULT_URL_TYPE, urlTemplate: null, - labelTemplate: null + labelTemplate: null, + width: null, + height: null, }; } @@ -91,6 +93,15 @@ export function createUrlFormat(FieldFormat) { }; } + _generateImgHtml(url, imageLabel) { + const isValidWidth = !isNaN(parseInt(this.param('width'))); + const isValidHeight = !isNaN(parseInt(this.param('height'))); + const maxWidth = isValidWidth ? `${this.param('width')}px` : 'none'; + const maxHeight = isValidHeight ? `${this.param('height')}px` : 'none'; + + return `${imageLabel}`; + } + static id = 'url'; static title = 'Url'; static fieldType = [ @@ -127,7 +138,7 @@ export function createUrlFormat(FieldFormat) { ? `A dynamically-specified image located at ${url}` : label; - return `${imageLabel}`; + return this._generateImgHtml(url, imageLabel); default: const inWhitelist = whitelistUrlSchemes.some(scheme => url.indexOf(scheme) === 0); if (!inWhitelist && !parsedUrl) { diff --git a/src/legacy/ui/public/field_editor/components/field_format_editor/editors/url/__snapshots__/url.test.js.snap b/src/legacy/ui/public/field_editor/components/field_format_editor/editors/url/__snapshots__/url.test.js.snap index a98e8e1e1b127..72d89bcc5d838 100644 --- a/src/legacy/ui/public/field_editor/components/field_format_editor/editors/url/__snapshots__/url.test.js.snap +++ b/src/legacy/ui/public/field_editor/components/field_format_editor/editors/url/__snapshots__/url.test.js.snap @@ -392,3 +392,205 @@ exports[`UrlFormatEditor should render url template help 1`] = ` /> `; + +exports[`UrlFormatEditor should render width and height fields if image 1`] = ` + + + + + } + labelType="label" + > + + + + + + } + isInvalid={false} + label={ + + } + labelType="label" + > + + + + + + } + isInvalid={false} + label={ + + } + labelType="label" + > + + + + } + labelType="label" + > + + + + } + labelType="label" + > + + + + +`; diff --git a/src/legacy/ui/public/field_editor/components/field_format_editor/editors/url/url.js b/src/legacy/ui/public/field_editor/components/field_format_editor/editors/url/url.js index 4e5f9f39291fe..69eeb20e14666 100644 --- a/src/legacy/ui/public/field_editor/components/field_format_editor/editors/url/url.js +++ b/src/legacy/ui/public/field_editor/components/field_format_editor/editors/url/url.js @@ -25,6 +25,7 @@ import { EuiLink, EuiSelect, EuiSwitch, + EuiFieldNumber } from '@elastic/eui'; import { @@ -68,23 +69,29 @@ export class UrlFormatEditor extends DefaultFormatEditor { }; } + sanitizeNumericValue = (val) => { + const sanitizedValue = parseInt(val); + if (isNaN(sanitizedValue)) { + return ''; + } + return sanitizedValue; + } + onTypeChange = (newType) => { - const { urlTemplate } = this.props.formatParams; - if(newType === 'img' && !urlTemplate) { - this.onChange({ - type: newType, - urlTemplate: this.iconPattern, - }); - } else if(newType !== 'img' && urlTemplate === this.iconPattern) { - this.onChange({ - type: newType, - urlTemplate: null, - }); - } else { - this.onChange({ - type: newType, - }); + const { urlTemplate, width, height } = this.props.formatParams; + const params = { + type: newType + }; + if (newType === 'img') { + params.width = width; + params.height = height; + if (!urlTemplate) { + params.urlTemplate = this.iconPattern; + } + } else if (newType !== 'img' && urlTemplate === this.iconPattern) { + params.urlTemplate = null; } + this.onChange(params); } showUrlTemplateHelp = () => { @@ -113,6 +120,37 @@ export class UrlFormatEditor extends DefaultFormatEditor { }); } + renderWidthHeightParameters = () => { + const width = this.sanitizeNumericValue(this.props.formatParams.width); + const height = this.sanitizeNumericValue(this.props.formatParams.height); + return ( + + } + > + { + this.onChange({ width: e.target.value }); + }} + /> + + } + > + { + this.onChange({ height: e.target.value }); + }} + /> + + + ); + } + render() { const { format, formatParams } = this.props; const { error, samples } = this.state; @@ -197,6 +235,8 @@ export class UrlFormatEditor extends DefaultFormatEditor { /> + { formatParams.type === 'img' && this.renderWidthHeightParameters() } + diff --git a/src/legacy/ui/public/field_editor/components/field_format_editor/editors/url/url.test.js b/src/legacy/ui/public/field_editor/components/field_format_editor/editors/url/url.test.js index 258d2d0b67c0b..22310837a169f 100644 --- a/src/legacy/ui/public/field_editor/components/field_format_editor/editors/url/url.test.js +++ b/src/legacy/ui/public/field_editor/components/field_format_editor/editors/url/url.test.js @@ -92,4 +92,17 @@ describe('UrlFormatEditor', () => { component.update(); expect(component).toMatchSnapshot(); }); + + it('should render width and height fields if image', async () => { + const component = shallow( + + ); + expect(component).toMatchSnapshot(); + }); });