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

Separate vis-specific controls from centralized controls #6827

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
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
102 changes: 76 additions & 26 deletions superset/assets/src/explore/components/ControlPanelsContainer.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,15 @@ import PropTypes from 'prop-types';
import { bindActionCreators } from 'redux';
import { connect } from 'react-redux';
import { Alert, Tab, Tabs } from 'react-bootstrap';
import { isPlainObject } from 'lodash';
import { t } from '@superset-ui/translation';

import controlPanelConfigs, { sectionsToRender } from '../controlPanels';
import ControlPanelSection from './ControlPanelSection';
import ControlRow from './ControlRow';
import Control from './Control';
import controls from '../controls';
import * as actions from '../actions/exploreActions';
import controlConfigs from '../controls';
import * as exploreActions from '../actions/exploreActions';

const propTypes = {
actions: PropTypes.object.isRequired,
Expand All @@ -44,18 +45,21 @@ const propTypes = {
class ControlPanelsContainer extends React.Component {
constructor(props) {
super(props);
this.removeAlert = this.removeAlert.bind(this);

this.getControlData = this.getControlData.bind(this);
this.removeAlert = this.removeAlert.bind(this);
this.renderControl = this.renderControl.bind(this);
this.renderControlPanelSection = this.renderControlPanelSection.bind(this);
}

getControlData(controlName) {
if (React.isValidElement(controlName)) {
return controlName;
}

const control = this.props.controls[controlName];
// Identifying mapStateToProps function to apply (logic can't be in store)
let mapF = controls[controlName].mapStateToProps;
let mapF = controlConfigs[controlName].mapStateToProps;

// Looking to find mapStateToProps override for this viz type
const config = controlPanelConfigs[this.props.controls.viz_type.value] || {};
Expand All @@ -69,19 +73,58 @@ class ControlPanelsContainer extends React.Component {
}
return control;
}

sectionsToRender() {
return sectionsToRender(this.props.form_data.viz_type, this.props.datasource_type);
}

removeAlert() {
this.props.actions.removeControlPanelAlert();
}

renderControl(name, config) {
const { actions, controls, exploreState, form_data: formData } = this.props;

// Looking to find mapStateToProps override for this viz type
const controlPanelConfig = controlPanelConfigs[controls.viz_type.value].controlOverrides || {};
const overrides = controlPanelConfig[name];

// Identifying mapStateToProps function to apply (logic can't be in store)
const mapFn = (overrides && overrides.mapStateToProps)
? overrides.mapStateToProps
: config.mapStateToProps;

const controlData = controls[name];

// Applying mapStateToProps if needed
const additionalProps = mapFn
? { ...controlData, ...mapFn(exploreState, controlData, actions) }
: controlData;

const { validationErrors, provideFormDataToProps } = controlData;

return (
<Control
name={name}
key={`control-${name}`}
value={formData[name]}
validationErrors={validationErrors}
actions={actions}
formData={provideFormDataToProps ? formData : null}
{...additionalProps}
/>
);
}

renderControlPanelSection(section) {
const ctrls = this.props.controls;
const { controls } = this.props;

const hasErrors = section.controlSetRows.some(rows => rows.some(s => (
ctrls[s] &&
ctrls[s].validationErrors &&
(ctrls[s].validationErrors.length > 0)
controls[s] &&
controls[s].validationErrors &&
(controls[s].validationErrors.length > 0)
)));

return (
<ControlPanelSection
key={section.label}
Expand All @@ -94,21 +137,27 @@ class ControlPanelsContainer extends React.Component {
<ControlRow
key={`controlsetrow-${i}`}
className="control-row"
controls={controlSets.map((controlName) => {
if (!controlName) {
controls={controlSets.map((controlItem) => {
if (!controlItem) {
// When the item is invalid
return null;
} else if (React.isValidElement(controlName)) {
return controlName;
} else if (ctrls[controlName]) {
return (<Control
name={controlName}
key={`control-${controlName}`}
value={this.props.form_data[controlName]}
validationErrors={ctrls[controlName].validationErrors}
actions={this.props.actions}
formData={ctrls[controlName].provideFormDataToProps ? this.props.form_data : null}
{...this.getControlData(controlName)}
/>);
} else if (React.isValidElement(controlItem)) {
// When the item is a React element
return controlItem;
} else if (isPlainObject(controlItem) && controlItem.name && controlItem.config) {
// When the item is { name, config }, meaning the control config
// is specified directly. Do not have to look up by name from
// centralized configs.
const { name, config } = controlItem;

return this.renderControl(name, config);
} else if (controls[controlItem]) {
// When the item is string name, meaning the control config
// is not specified directly. Have to look up the config from
// centralized configs.
const name = controlItem;

return this.renderControl(name, controlConfigs[name]);
}
return null;
})}
Expand All @@ -117,17 +166,18 @@ class ControlPanelsContainer extends React.Component {
</ControlPanelSection>
);
}

render() {
const allSectionsToRender = this.sectionsToRender();
const querySectionsToRender = [];
const displaySectionsToRender = [];
allSectionsToRender.forEach((section) => {
if (section.controlSetRows.some(rows => rows.some(
control => (
controls[control] &&
controlConfigs[control] &&
(
!controls[control].renderTrigger ||
controls[control].tabOverride === 'data'
!controlConfigs[control].renderTrigger ||
controlConfigs[control].tabOverride === 'data'
)
)))) {
querySectionsToRender.push(section);
Expand Down Expand Up @@ -178,7 +228,7 @@ function mapStateToProps({ explore }) {

function mapDispatchToProps(dispatch) {
return {
actions: bindActionCreators(actions, dispatch),
actions: bindActionCreators(exploreActions, dispatch),
};
}

Expand Down
30 changes: 27 additions & 3 deletions superset/assets/src/explore/controlPanels/PairedTtest.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,33 @@ export default {
label: t('Paired t-test'),
expanded: false,
controlSetRows: [
['significance_level'],
['pvalue_precision'],
['liftvalue_precision'],
[{
name: 'significance_level',
config: {
type: 'TextControl',
label: t('Significance Level'),
default: 0.05,
description: t('Threshold alpha level for determining significance'),
},
}],
[{
name: 'pvalue_precision',
config: {
type: 'TextControl',
label: t('p-value precision'),
default: 6,
description: t('Number of decimal places with which to display p-values'),
},
}],
[{
name: 'liftvalue_precision',
config: {
type: 'TextControl',
label: t('Lift percent precision'),
default: 4,
description: t('Number of decimal places with which to display lift values'),
},
}],
],
},
],
Expand Down
14 changes: 13 additions & 1 deletion superset/assets/src/explore/controlPanels/Rose.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,19 @@ export default {
controlSetRows: [
['color_scheme', 'label_colors'],
['number_format', 'date_time_format'],
['rich_tooltip', 'rose_area_proportion'],
['rich_tooltip', {
name: 'rose_area_proportion',
config: {
type: 'CheckboxControl',
label: t('Use Area Proportions'),
description: t(
'Check if the Rose Chart should use segment area instead of ' +
'segment radius for proportioning',
),
default: false,
renderTrigger: true,
},
}],
],
},
NVD3TimeSeries[1],
Expand Down
32 changes: 0 additions & 32 deletions superset/assets/src/explore/controls.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -2101,45 +2101,13 @@ export const controls = {
}),
},

significance_level: {
type: 'TextControl',
label: t('Significance Level'),
default: 0.05,
description: t('Threshold alpha level for determining significance'),
},

pvalue_precision: {
type: 'TextControl',
label: t('p-value precision'),
default: 6,
description: t('Number of decimal places with which to display p-values'),
},

liftvalue_precision: {
type: 'TextControl',
label: t('Lift percent precision'),
default: 4,
description: t('Number of decimal places with which to display lift values'),
},

column_collection: {
type: 'CollectionControl',
label: t('Time Series Columns'),
validators: [v.nonEmpty],
controlName: 'TimeSeriesColumnControl',
},

rose_area_proportion: {
type: 'CheckboxControl',
label: t('Use Area Proportions'),
description: t(
'Check if the Rose Chart should use segment area instead of ' +
'segment radius for proportioning',
),
default: false,
renderTrigger: true,
},

time_series_option: {
type: 'SelectControl',
label: t('Options'),
Expand Down
Loading