1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 | import $ from 'jquery'; import React, { PropTypes } from 'react'; import { connect } from 'react-redux'; import { Panel, Alert } from 'react-bootstrap'; import visMap from '../../../visualizations/main'; import { d3format } from '../../modules/utils'; import ExploreActionButtons from '../../explore/components/ExploreActionButtons'; import FaveStar from '../../components/FaveStar'; import TooltipWrapper from '../../components/TooltipWrapper'; import Timer from '../../components/Timer'; const CHART_STATUS_MAP = { failed: 'danger', loading: 'warning', success: 'success', }; const propTypes = { actions: PropTypes.object.isRequired, can_download: PropTypes.bool.isRequired, slice_id: PropTypes.string.isRequired, slice_name: PropTypes.string.isRequired, viz_type: PropTypes.string.isRequired, height: PropTypes.string.isRequired, containerId: PropTypes.string.isRequired, json_endpoint: PropTypes.string.isRequired, csv_endpoint: PropTypes.string.isRequired, standalone_endpoint: PropTypes.string.isRequired, query: PropTypes.string, column_formats: PropTypes.object, data: PropTypes.any, chartStatus: PropTypes.string, isStarred: PropTypes.bool.isRequired, chartUpdateStartTime: PropTypes.number.isRequired, chartUpdateEndTime: PropTypes.number, alert: PropTypes.string, table_name: PropTypes.string, }; class ChartContainer extends React.Component { constructor(props) { super(props); this.state = { selector: `#${props.containerId}`, mockSlice: {}, viz: {}, }; } componentWillMount() { const mockSlice = this.getMockedSliceObject(this.props); this.setState({ mockSlice, viz: visMap[this.props.viz_type](mockSlice), }); } componentDidMount() { this.state.viz.render(); } componentWillReceiveProps(nextProps) { if (nextProps.chartStatus === 'loading') { const mockSlice = this.getMockedSliceObject(nextProps); this.setState({ mockSlice, viz: visMap[nextProps.viz_type](mockSlice), }); } } componentDidUpdate(prevProps) { if (this.props.chartStatus === 'loading') { this.state.viz.render(); } if (prevProps.height !== this.props.height) { this.state.viz.resize(); } } getMockedSliceObject(props) { return { viewSqlQuery: props.query, data: { csv_endpoint: props.csv_endpoint, json_endpoint: props.json_endpoint, standalone_endpoint: props.standalone_endpoint, }, containerId: props.containerId, jsonEndpoint: () => props.json_endpoint, container: { html: (data) => { // this should be a callback to clear the contents of the slice container $(this.state.selector).html(data); }, css: (dim, size) => { // dimension can be 'height' // pixel string can be '300px' // should call callback to adjust height of chart $(this.state.selector).css(dim, size); }, height: () => parseInt(this.props.height, 10) - 100, show: () => { this.render(); }, get: (n) => ($(this.state.selector).get(n)), find: (classname) => ($(this.state.selector).find(classname)), }, width: () => this.chartContainerRef.getBoundingClientRect().width, height: () => parseInt(this.props.height, 10) - 100, selector: this.state.selector, setFilter: () => { // set filter according to data in store // used in FilterBox.onChange() }, getFilters: () => ( // return filter objects from viz.formData {} ), done: (payload) => { // finished rendering callback // Todo: end timer and chartLoading set to success props.actions.chartUpdateSucceeded(payload.query); }, clearError: () => { // no need to do anything here since Alert is closable // query button will also remove Alert }, error(msg) { let payload = { error: msg }; try { payload = JSON.parse(msg); } catch (e) { // pass } props.actions.chartUpdateFailed(payload.error, payload.query); }, d3format: (col, number) => { // mock d3format function in Slice object in superset.js const format = props.column_formats[col]; return d3format(format, number); }, }; } removeAlert() { this.props.actions.removeChartAlert(); } renderChartTitle() { let title; if (this.props.slice_name) { title = this.props.slice_name; } else { title = `[${this.props.table_name}] - untitled`; } return title; } renderChart() { if (this.props.alert) { return ( <Alert bsStyle="warning"> {this.props.alert} <i className="fa fa-close pull-right" onClick={this.removeAlert.bind(this)} style={{ cursor: 'pointer' }} /> </Alert> ); } const loading = this.props.chartStatus === 'loading'; return ( <div> {loading && <img alt="loading" width="25" src="/static/assets/images/loading.gif" style={{ position: 'absolute' }} /> } <div id={this.props.containerId} ref={(ref) => { this.chartContainerRef = ref; }} className={this.props.viz_type} style={{ opacity: loading ? '0.25' : '1', }} /> </div> ); } render() { return ( <div className="chart-container"> <Panel style={{ height: this.props.height }} header={ <div id="slice-header" className="clearfix panel-title-large" > {this.renderChartTitle()} {this.props.slice_id && <span> <FaveStar sliceId={this.props.slice_id} actions={this.props.actions} isStarred={this.props.isStarred} /> <TooltipWrapper label="edit-desc" tooltip="Edit Description" > <a className="edit-desc-icon" href={`/slicemodelview/edit/${this.props.slice_id}`} > <i className="fa fa-edit" /> </a> </TooltipWrapper> </span> } <div className="pull-right"> <Timer startTime={this.props.chartUpdateStartTime} endTime={this.props.chartUpdateEndTime} isRunning={this.props.chartStatus === 'loading'} state={CHART_STATUS_MAP[this.props.chartStatus]} style={{ fontSize: '10px', marginRight: '5px' }} /> <ExploreActionButtons slice={this.state.mockSlice} canDownload={this.props.can_download} query={this.props.query} /> </div> </div> } > {this.renderChart()} </Panel> </div> ); } } ChartContainer.propTypes = propTypes; function mapStateToProps(state) { return { containerId: `slice-container-${state.viz.form_data.slice_id}`, slice_id: state.viz.form_data.slice_id, slice_name: state.viz.form_data.slice_name, viz_type: state.viz.form_data.viz_type, can_download: state.can_download, csv_endpoint: state.viz.csv_endpoint, json_endpoint: state.viz.json_endpoint, standalone_endpoint: state.viz.standalone_endpoint, chartUpdateStartTime: state.chartUpdateStartTime, chartUpdateEndTime: state.chartUpdateEndTime, query: state.viz.query, column_formats: state.viz.column_formats, data: state.viz.data, chartStatus: state.chartStatus, isStarred: state.isStarred, alert: state.chartAlert, table_name: state.viz.form_data.datasource_name, }; } export default connect(mapStateToProps, () => ({}))(ChartContainer); |