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

[ML] Convert anomalies controls to EUI / React #19856

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
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

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -7,37 +7,50 @@


/*
* AngularJS directive+service for a checkbox element to toggle charts display.
* React component for a checkbox element to toggle charts display.
*/
import React, { Component } from 'react';

import { stateFactoryProvider } from 'plugins/ml/factories/state_factory';
import {
EuiCheckbox
} from '@elastic/eui';

import template from './checkbox_showcharts.html';
import 'plugins/ml/components/controls/controls_select';
import makeId from '@elastic/eui/lib/components/form/form_row/make_id';

import { uiModules } from 'ui/modules';
const module = uiModules.get('apps/ml');
class CheckboxShowCharts extends Component {
constructor(props) {
super(props);

module
.service('mlCheckboxShowChartsService', function (Private) {
const stateFactory = Private(stateFactoryProvider);
this.state = stateFactory('mlCheckboxShowCharts', {
showCharts: true
});
})
.directive('mlCheckboxShowCharts', function (mlCheckboxShowChartsService) {
return {
restrict: 'E',
template,
scope: {
visible: '='
},
link: function (scope) {
scope.showCharts = mlCheckboxShowChartsService.state.get('showCharts');
scope.toggleChartsVisibility = function () {
mlCheckboxShowChartsService.state.set('showCharts', scope.showCharts);
mlCheckboxShowChartsService.state.changed();
};
}
// Restore the checked setting from the state.
const mlCheckboxShowChartsService = this.props.mlCheckboxShowChartsService;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit. could be added as a class variable. e.g. this.mlCheckboxShowChartsService = ...
so it doesn't then have to be read out of the props again in the onChange function

const showCharts = mlCheckboxShowChartsService.state.get('showCharts');

this.state = {
checked: showCharts
};
});
}

onChange = (e) => {
const showCharts = e.target.checked;
const mlCheckboxShowChartsService = this.props.mlCheckboxShowChartsService;
mlCheckboxShowChartsService.state.set('showCharts', showCharts);
mlCheckboxShowChartsService.state.changed();

this.setState({
checked: showCharts,
});
};

render() {
return (
<EuiCheckbox
id={makeId()}
label="Show charts"
checked={this.state.checked}
onChange={this.onChange}
/>
);
}
}

export { CheckboxShowCharts };
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License;
* you may not use this file except in compliance with the Elastic License.
*/


import 'ngreact';

import { stateFactoryProvider } from 'plugins/ml/factories/state_factory';

import { uiModules } from 'ui/modules';
const module = uiModules.get('apps/ml', ['react']);

import { CheckboxShowCharts } from './checkbox_showcharts';

module.service('mlCheckboxShowChartsService', function (Private) {
const stateFactory = Private(stateFactoryProvider);
this.state = stateFactory('mlCheckboxShowCharts', {
showCharts: true
});
})
.directive('mlCheckboxShowCharts', function ($injector) {
const reactDirective = $injector.get('reactDirective');
const mlCheckboxShowChartsService = $injector.get('mlCheckboxShowChartsService');

return reactDirective(
CheckboxShowCharts,
undefined,
{ restrict: 'E' },
{ mlCheckboxShowChartsService }
);
});
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,4 @@
*/



import './checkbox_showcharts';
import './checkbox_showcharts_directive';

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,4 @@
*/



import './select_interval.js';
import './select_interval_directive';

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -7,65 +7,73 @@


/*
* AngularJS directive for rendering a select element with various interval levels.
* React component for rendering a select element with various aggregation interval levels.
*/

import _ from 'lodash';
import React, { Component } from 'react';

import { stateFactoryProvider } from 'plugins/ml/factories/state_factory';
import {
EuiSelect
} from '@elastic/eui';

import template from './select_interval.html';
import 'plugins/ml/components/controls/controls_select';

import { uiModules } from 'ui/modules';
const module = uiModules.get('apps/ml');
const OPTIONS = [
{ value: 'auto', text: 'Auto' },
{ value: 'hour', text: '1 hour' },
{ value: 'day', text: '1 day' },
{ value: 'second', text: 'Show all' }
];

module
.service('mlSelectIntervalService', function (Private) {
const stateFactory = Private(stateFactoryProvider);
this.state = stateFactory('mlSelectInterval', {
interval: { display: 'Auto', val: 'auto' }
});
})
.directive('mlSelectInterval', function (mlSelectIntervalService) {
return {
restrict: 'E',
template,
link: function (scope, element) {
scope.intervalOptions = [
{ display: 'Auto', val: 'auto' },
{ display: '1 hour', val: 'hour' },
{ display: '1 day', val: 'day' },
{ display: 'Show all', val: 'second' }
];

const intervalState = mlSelectIntervalService.state.get('interval');
const intervalValue = _.get(intervalState, 'val', 'auto');
let intervalOption = scope.intervalOptions.find(d => d.val === intervalValue);
if (intervalOption === undefined) {
// Attempt to set value in URL which doesn't map to one of the options.
intervalOption = scope.intervalOptions.find(d => d.val === 'auto');
}
scope.interval = intervalOption;
mlSelectIntervalService.state.set('interval', scope.interval);

scope.setInterval = function (interval) {
if (!_.isEqual(scope.interval, interval)) {
scope.interval = interval;
mlSelectIntervalService.state.set('interval', scope.interval).changed();
}
};

function setScopeInterval() {
scope.setInterval(mlSelectIntervalService.state.get('interval'));
}

mlSelectIntervalService.state.watch(setScopeInterval);

element.on('$destroy', () => {
mlSelectIntervalService.state.unwatch(setScopeInterval);
scope.$destroy();
});
}
function optionValueToInterval(value) {
// Builds the corresponding interval object with the required display and val properties
// from the specified value.
const option = OPTIONS.find(opt => opt.value === value);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit. conditions should be wrapped in braces.
const option = OPTIONS.find(opt => (opt.value === value));


// Default to auto if supplied value doesn't map to one of the options.
let interval = OPTIONS[0];
if (option !== undefined) {
interval = { display: option.text, val: option.value };
}

return interval;
}

class SelectInterval extends Component {
constructor(props) {
super(props);

// Restore the interval from the state, or default to auto.
const mlSelectIntervalService = this.props.mlSelectIntervalService;
const intervalState = mlSelectIntervalService.state.get('interval');
const intervalValue = _.get(intervalState, 'val', 'auto');
const interval = optionValueToInterval(intervalValue);
mlSelectIntervalService.state.set('interval', interval);

this.state = {
value: interval.val
};
});
}

onChange = (e) => {
this.setState({
value: e.target.value,
});

const interval = optionValueToInterval(e.target.value);
const mlSelectIntervalService = this.props.mlSelectIntervalService;
mlSelectIntervalService.state.set('interval', interval).changed();
};

render() {
return (
<EuiSelect
options={OPTIONS}
className="ml-select-interval"
value={this.state.value}
onChange={this.onChange}
/>
);
}
}

export { SelectInterval };
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License;
* you may not use this file except in compliance with the Elastic License.
*/


import 'ngreact';

import { stateFactoryProvider } from 'plugins/ml/factories/state_factory';

import { uiModules } from 'ui/modules';
const module = uiModules.get('apps/ml', ['react']);

import { SelectInterval } from './select_interval';

module.service('mlSelectIntervalService', function (Private) {
const stateFactory = Private(stateFactoryProvider);
this.state = stateFactory('mlSelectInterval', {
interval: { display: 'Auto', val: 'auto' }
});
})
.directive('mlSelectInterval', function ($injector) {
const reactDirective = $injector.get('reactDirective');
const mlSelectIntervalService = $injector.get('mlSelectIntervalService');

return reactDirective(
SelectInterval,
undefined,
{ restrict: 'E' },
{ mlSelectIntervalService }
);
});

This file was deleted.

Loading