From cc29e515215970dbcff03eaefa14c5d70e41da19 Mon Sep 17 00:00:00 2001 From: Grace Guo Date: Sat, 23 Sep 2017 11:18:56 -0700 Subject: [PATCH] annotation layers UI for https://github.com/apache/incubator-superset/issues/3502 --- .../javascripts/components/AsyncSelect.jsx | 6 +- .../explore/components/Control.jsx | 4 +- .../controls/SelectAsyncControl.jsx | 62 +++++++++++++++++ superset/assets/javascripts/explore/main.css | 5 +- .../javascripts/explore/stores/controls.jsx | 13 +--- .../javascripts/explore/stores/visTypes.js | 7 +- superset/assets/visualizations/nvd3_vis.js | 67 ++++++++++++++++++- superset/migrations/versions/d39b1e37131d_.py | 22 ++++++ ...tations.py => ddd6ebdd853b_annotations.py} | 0 superset/migrations/versions/f959a6652acd_.py | 22 ++++++ superset/views/annotations.py | 2 +- superset/viz.py | 2 + 12 files changed, 196 insertions(+), 16 deletions(-) create mode 100644 superset/assets/javascripts/explore/components/controls/SelectAsyncControl.jsx create mode 100644 superset/migrations/versions/d39b1e37131d_.py rename superset/migrations/versions/{483e7ff8b241_annotations.py => ddd6ebdd853b_annotations.py} (100%) create mode 100644 superset/migrations/versions/f959a6652acd_.py diff --git a/superset/assets/javascripts/components/AsyncSelect.jsx b/superset/assets/javascripts/components/AsyncSelect.jsx index 007281a116a04..69b4216a9c955 100644 --- a/superset/assets/javascripts/components/AsyncSelect.jsx +++ b/superset/assets/javascripts/components/AsyncSelect.jsx @@ -10,7 +10,10 @@ const propTypes = { onChange: PropTypes.func.isRequired, mutator: PropTypes.func.isRequired, onAsyncError: PropTypes.func, - value: PropTypes.number, + value: PropTypes.oneOfType([ + PropTypes.number, + PropTypes.arrayOf(PropTypes.number), + ]), valueRenderer: PropTypes.func, placeholder: PropTypes.string, autoSelect: PropTypes.bool, @@ -63,6 +66,7 @@ class AsyncSelect extends React.PureComponent { isLoading={this.state.isLoading} onChange={this.onChange.bind(this)} valueRenderer={this.props.valueRenderer} + {...this.props} /> ); diff --git a/superset/assets/javascripts/explore/components/Control.jsx b/superset/assets/javascripts/explore/components/Control.jsx index 5c644c38f7906..972ff0d3c19d4 100644 --- a/superset/assets/javascripts/explore/components/Control.jsx +++ b/superset/assets/javascripts/explore/components/Control.jsx @@ -3,15 +3,16 @@ import PropTypes from 'prop-types'; import BoundsControl from './controls/BoundsControl'; import CheckboxControl from './controls/CheckboxControl'; +import ColorSchemeControl from './controls/ColorSchemeControl'; import DatasourceControl from './controls/DatasourceControl'; import DateFilterControl from './controls/DateFilterControl'; import FilterControl from './controls/FilterControl'; import HiddenControl from './controls/HiddenControl'; +import SelectAsyncControl from './controls/SelectAsyncControl'; import SelectControl from './controls/SelectControl'; import TextAreaControl from './controls/TextAreaControl'; import TextControl from './controls/TextControl'; import VizTypeControl from './controls/VizTypeControl'; -import ColorSchemeControl from './controls/ColorSchemeControl'; const controlMap = { BoundsControl, @@ -25,6 +26,7 @@ const controlMap = { TextControl, VizTypeControl, ColorSchemeControl, + SelectAsyncControl, }; const controlTypes = Object.keys(controlMap); diff --git a/superset/assets/javascripts/explore/components/controls/SelectAsyncControl.jsx b/superset/assets/javascripts/explore/components/controls/SelectAsyncControl.jsx new file mode 100644 index 0000000000000..80eff94daa1f1 --- /dev/null +++ b/superset/assets/javascripts/explore/components/controls/SelectAsyncControl.jsx @@ -0,0 +1,62 @@ +/* global notify */ +import React from 'react'; +import PropTypes from 'prop-types'; +import Select from '../../../components/AsyncSelect'; +import { t } from '../../../locales'; + +const propTypes = { + onChange: PropTypes.func, + value: PropTypes.oneOfType([ + PropTypes.string, + PropTypes.number, + PropTypes.arrayOf(PropTypes.string), + PropTypes.arrayOf(PropTypes.number), + ]), +}; + +const defaultProps = { + onChange: () => {}, +}; + +class SelectAsyncControl extends React.PureComponent { + constructor(props) { + super(props); + this.state = { + value: this.props.value, + }; + } + + onChange(options) { + const optionValues = options.map(option => option.value); + this.setState({ value: optionValues }); + this.props.onChange(optionValues); + } + + mutator(data) { + if (!data || !data.result) { + return []; + } + + return data.result.map(layer => ({ value: layer.id, label: layer.name })); + } + + render() { + return ( +