Skip to content

Commit

Permalink
[Vis: Default editor] EUIficate raw json control (elastic#32888)
Browse files Browse the repository at this point in the history
* EUIficate raw json control

* Remove unused validate-json directive

* Update tests

* Update styles

* Move validation logic down to control

* Fix type
  • Loading branch information
maryia-lapata committed Mar 28, 2019
1 parent 519840a commit 41f032e
Show file tree
Hide file tree
Showing 13 changed files with 203 additions and 204 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ import dateMath from '@elastic/datemath';
import 'ui/doc_table';
import 'ui/visualize';
import 'ui/fixed_scroll';
import 'ui/directives/validate_json';
import 'ui/filters/moment';
import 'ui/index_patterns';
import 'ui/state_management/app_state';
Expand Down
49 changes: 49 additions & 0 deletions src/legacy/ui/public/agg_types/__tests__/utils.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/*
* 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 { isValidJson } from '../utils';

const input = {
valid: '{ "test": "json input" }',
invalid: 'strings are not json',
};

describe('AggType utils', () => {
describe('isValidJson', () => {
it('should return true when empty string', () => {
expect(isValidJson('')).toBe(true);
});

it('should return true when undefine', () => {
expect(isValidJson(undefined as any)).toBe(true);
});

it('should return false when invalid string', () => {
expect(isValidJson(input.invalid)).toBe(false);
});

it('should return true when valid string', () => {
expect(isValidJson(input.valid)).toBe(true);
});

it('should return false if a number', () => {
expect(isValidJson('0')).toBe(false);
});
});
});
23 changes: 0 additions & 23 deletions src/legacy/ui/public/agg_types/controls/raw_json.html

This file was deleted.

76 changes: 76 additions & 0 deletions src/legacy/ui/public/agg_types/controls/raw_json.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
/*
* 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 React from 'react';

import { EuiFormRow, EuiIconTip, EuiTextArea } from '@elastic/eui';
import { i18n } from '@kbn/i18n';
import { FormattedMessage } from '@kbn/i18n/react';
import { AggParamEditorProps } from '../../vis/editors/default';
import { isValidJson } from '../utils';

function RawJsonParamEditor({
agg,
value,
setValue,
isInvalid,
setValidity,
}: AggParamEditorProps<string>) {
const label = (
<>
<FormattedMessage id="common.ui.aggTypes.jsonInputLabel" defaultMessage="JSON input" />{' '}
<EuiIconTip
position="right"
content={i18n.translate('common.ui.aggTypes.jsonInputTooltip', {
defaultMessage:
"Any JSON formatted properties you add here will be merged with the elasticsearch aggregation definition for this section. For example 'shard_size' on a terms aggregation.",
})}
type="questionInCircle"
/>
</>
);

const onChange = (ev: React.ChangeEvent<HTMLTextAreaElement>) => {
const textValue = ev.target.value;
setValue(textValue);
setValidity(isValidJson(textValue));
};

setValidity(isValidJson(value));

return (
<EuiFormRow
label={label}
isInvalid={isInvalid}
fullWidth={true}
className="visEditorSidebar__aggParamFormRow"
>
<EuiTextArea
id={`visEditorRawJson${agg.id}`}
isInvalid={isInvalid}
value={value || ''}
onChange={onChange}
rows={2}
fullWidth={true}
/>
</EuiFormRow>
);
}

export { RawJsonParamEditor };
4 changes: 2 additions & 2 deletions src/legacy/ui/public/agg_types/param_types/json.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
*/

import _ from 'lodash';
import editorHtml from '../controls/raw_json.html';
import { RawJsonParamEditor } from '../controls/raw_json';
import { BaseParamType } from './base';
import { createLegacyClass } from '../../utils/legacy_class';

Expand All @@ -30,7 +30,7 @@ function JsonParamType(config) {
JsonParamType.Super.call(this, config);
}

JsonParamType.prototype.editor = editorHtml;
JsonParamType.prototype.editorComponent = RawJsonParamEditor;

/**
* Write the aggregation parameter.
Expand Down
39 changes: 39 additions & 0 deletions src/legacy/ui/public/agg_types/utils.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*
* 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.
*/

function isValidJson(value: string): boolean {
if (!value || value.length === 0) {
return true;
}

const trimmedValue = value.trim();

if (trimmedValue[0] === '{' || trimmedValue[0] === '[') {
try {
JSON.parse(trimmedValue);
return true;
} catch (e) {
return false;
}
} else {
return false;
}
}

export { isValidJson };
111 changes: 0 additions & 111 deletions src/legacy/ui/public/directives/__tests__/validate_json.js

This file was deleted.

64 changes: 0 additions & 64 deletions src/legacy/ui/public/directives/validate_json.js

This file was deleted.

Loading

0 comments on commit 41f032e

Please sign in to comment.