-
Notifications
You must be signed in to change notification settings - Fork 8.3k
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
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
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 |
---|---|---|
|
@@ -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 |
---|---|---|
|
@@ -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 |
---|---|---|
|
@@ -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); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit. conditions should be wrapped in braces. |
||
|
||
// 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.
There was a problem hiding this comment.
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