williaster commented on a change in pull request #3518: Full Annotation
Framework
URL:
https://github.com/apache/incubator-superset/pull/3518#discussion_r155893133
##########
File path:
superset/assets/javascripts/explore/components/controls/AnnotationLayerControl.jsx
##########
@@ -0,0 +1,171 @@
+import React from 'react';
+import PropTypes from 'prop-types';
+import { OverlayTrigger, Popover, ListGroup, ListGroupItem } from
'react-bootstrap';
+import { connect } from 'react-redux';
+import { runAnnotationQuery } from '../../actions/chartActions';
+import InfoTooltipWithTrigger from
'../../../components/InfoTooltipWithTrigger';
+
+
+import AnnotationLayer from './AnnotationLayer';
+import { t } from '../../../locales';
+
+
+const propTypes = {
+ colorScheme: PropTypes.string.isRequired,
+ annotationError: PropTypes.object,
+ annotationQuery: PropTypes.object,
+
+ validationErrors: PropTypes.array,
+ name: PropTypes.string.isRequired,
+ actions: PropTypes.object,
+ value: PropTypes.arrayOf(PropTypes.object),
+ onChange: PropTypes.func,
+ refreshAnnotationData: PropTypes.func,
+};
+
+const defaultProps = {
+ value: [],
+ annotationError: {},
+ annotationQuery: {},
+ onChange: () => {},
+};
+
+class AnnotationLayerControl extends React.PureComponent {
+ constructor(props) {
+ super(props);
+ this.addAnnotationLayer = this.addAnnotationLayer.bind(this);
+ this.removeAnnotationLayer = this.removeAnnotationLayer.bind(this);
+ }
+
+ componentWillReceiveProps(nextProps) {
+ const { name, annotationError, validationErrors, value } = nextProps;
+ if (Object.keys(annotationError).length && !validationErrors.length) {
+ this.props.actions.setControlValue(name, value,
Object.keys(annotationError));
+ }
+ if (!Object.keys(annotationError).length && validationErrors.length) {
+ this.props.actions.setControlValue(name, value, []);
+ }
+ }
+
+ addAnnotationLayer(annotationLayer) {
+ const annotation = annotationLayer;
+ let annotations = this.props.value.slice();
+ const i = annotations.findIndex(x => x.name === (annotation.oldName ||
annotation.name));
+ delete annotation.oldName;
+ if (i > -1) {
+ annotations[i] = annotation;
+ } else {
+ annotations = annotations.concat(annotation);
+ }
+ this.props.refreshAnnotationData(annotation);
+ this.props.onChange(annotations);
+ }
+
+ removeAnnotationLayer(annotation) {
+ const annotations = this.props.value.slice()
+ .filter(x => x.name !== annotation.oldName);
+ this.props.onChange(annotations);
+ }
+
+ renderPopover(parent, annotation, error) {
+ const id = !annotation ? '_new' : annotation.name;
+ return (
+ <Popover
+ style={{ maxWidth: 'none' }}
+ title={annotation ? 'Edit Annotation Layer' : 'Add Annotation Layer'}
+ id={`annotation-pop-${id}`}
+ >
+ <AnnotationLayer
+ {...annotation}
+ error={error}
+ colorScheme={this.props.colorScheme}
+ addAnnotationLayer={this.addAnnotationLayer}
+ removeAnnotationLayer={this.removeAnnotationLayer}
+ close={() => this.refs[parent].hide()}
+ />
+ </Popover>
+ );
+ }
+
+ renderInfo(anno) {
+ const { annotationError, annotationQuery } = this.props;
+ if (annotationQuery[anno.name]) {
+ return (
+ <i className="fa fa-refresh" style={{ color: 'orange' }} aria-hidden />
+ );
+ }
+ if (annotationError[anno.name]) {
+ return (
+ <InfoTooltipWithTrigger
+ label="validation-errors"
+ bsStyle="danger"
+ tooltip={annotationError[anno.name]}
+ />
+ );
+ }
+ if (!anno.show) {
+ return <span style={{ color: 'red' }}> 'Hidden' </span>;
+ }
+ return '';
+ }
+
+ render() {
+ const annotations = this.props.value.map((anno, i) => (
+ <OverlayTrigger
+ key={i}
+ trigger="click"
+ rootClose
+ ref={`overlay-${i}`}
+ placement="right"
+ overlay={this.renderPopover(`overlay-${i}`, anno,
+ this.props.annotationError[anno.name])}
+ >
+ <ListGroupItem>
+ <span>{anno.name}</span>
+ <span style={{ float: 'right' }}>
+ {this.renderInfo(anno)}
+ </span>
+ </ListGroupItem>
+ </OverlayTrigger>
+ ));
+ return (
+ <div>
+ <ListGroup>
+ {annotations}
+ <OverlayTrigger
+ trigger="click"
+ rootClose
+ ref="overlay-new"
+ placement="right"
+ overlay={this.renderPopover('overlay-new')}
+ >
+ <ListGroupItem>
+ <i className="fa fa-plus" /> {t('Add Annotation Layer')}
+ </ListGroupItem>
+ </OverlayTrigger>
+ </ListGroup>
+ </div>
+ );
+ }
+}
+
+AnnotationLayerControl.propTypes = propTypes;
+AnnotationLayerControl.defaultProps = defaultProps;
+
+// Tried to hook this up through stores/control.jsx instead of using redux
+// directly, could not figure out how to get acess to the color_scheme
Review comment:
@graceguo-supercat do you have any thoughts on this?
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
[email protected]
With regards,
Apache Git Services