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 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 | import $ from 'jquery'; import Mustache from 'mustache'; import React, { PropTypes } from 'react'; import { connect } from 'react-redux'; import { Alert, Collapse, Label, Panel } from 'react-bootstrap'; import visMap from '../../../visualizations/main'; import { d3format } from '../../modules/utils'; import ExploreActionButtons from './ExploreActionButtons'; import FaveStar from '../../components/FaveStar'; import TooltipWrapper from '../../components/TooltipWrapper'; import Timer from '../../components/Timer'; import { getExploreUrl } from '../exploreUtils'; import { getFormDataFromControls } from '../stores/store'; const CHART_STATUS_MAP = { failed: 'danger', loading: 'warning', success: 'success', }; const propTypes = { actions: PropTypes.object.isRequired, alert: PropTypes.string, can_download: PropTypes.bool.isRequired, chartStatus: PropTypes.string, chartUpdateEndTime: PropTypes.number, chartUpdateStartTime: PropTypes.number.isRequired, column_formats: PropTypes.object, containerId: PropTypes.string.isRequired, height: PropTypes.string.isRequired, isStarred: PropTypes.bool.isRequired, slice: PropTypes.object, table_name: PropTypes.string, viz_type: PropTypes.string.isRequired, formData: PropTypes.object, latestQueryFormData: PropTypes.object, }; class ChartContainer extends React.PureComponent { constructor(props) { super(props); this.state = { selector: `#${props.containerId}`, showStackTrace: false, }; } renderViz() { this.props.actions.renderTriggered(); const mockSlice = this.getMockedSliceObject(); this.setState({ mockSlice }); try { visMap[this.props.viz_type](mockSlice, this.props.queryResponse); } catch (e) { this.props.actions.chartRenderingFailed(e); } } componentDidUpdate(prevProps) { if ( this.props.queryResponse && ( prevProps.queryResponse !== this.props.queryResponse || prevProps.height !== this.props.height || this.props.triggerRender ) && !this.props.queryResponse.error && this.props.chartStatus !== 'failed' && this.props.chartStatus !== 'stopped' ) { this.renderViz(); } } getMockedSliceObject() { const props = this.props; const getHeight = () => { const headerHeight = this.props.standalone ? 0 : 100; return parseInt(props.height, 10) - headerHeight; }; return { viewSqlQuery: this.props.queryResponse.query, containerId: props.containerId, selector: this.state.selector, formData: this.props.formData, 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: getHeight, show: () => { }, get: (n) => ($(this.state.selector).get(n)), find: (classname) => ($(this.state.selector).find(classname)), }, width: () => this.chartContainerRef.getBoundingClientRect().width, height: getHeight, render_template: (s) => { const context = { width: this.width, height: this.height, }; return Mustache.render(s, context); }, setFilter: () => {}, getFilters: () => ( // return filter objects from viz.formData {} ), addFilter: () => {}, removeFilter: () => {}, done: () => {}, clearError: () => { // no need to do anything here since Alert is closable // query button will also remove Alert }, error() {}, d3format: (col, number) => { // mock d3format function in Slice object in superset.js const format = props.column_formats[col]; return d3format(format, number); }, data: { csv_endpoint: getExploreUrl(this.props.formData, 'csv'), json_endpoint: getExploreUrl(this.props.formData, 'json'), standalone_endpoint: getExploreUrl( this.props.formData, 'standalone'), }, }; } removeAlert() { this.props.actions.removeChartAlert(); } renderChartTitle() { let title; if (this.props.slice) { title = this.props.slice.slice_name; } else { title = `[${this.props.table_name}] - untitled`; } return title; } renderAlert() { const msg = ( <div> {this.props.alert} <i className="fa fa-close pull-right" onClick={this.removeAlert.bind(this)} style={{ cursor: 'pointer' }} /> </div>); return ( <div> <Alert bsStyle="warning" onClick={() => this.setState({ showStackTrace: !this.state.showStackTrace })} > {msg} </Alert> {this.props.queryResponse && this.props.queryResponse.stacktrace && <Collapse in={this.state.showStackTrace}> <pre> {this.props.queryResponse.stacktrace} </pre> </Collapse> } </div>); } renderChart() { if (this.props.alert) { return this.renderAlert(); } 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> ); } runQuery() { this.props.actions.runQuery(this.props.formData, true); } render() { if (this.props.standalone) { // dom manipulation hack to get rid of the boostrap theme's body background $('body').addClass('background-transparent'); return this.renderChart(); } const queryResponse = this.props.queryResponse; 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 && <span> <FaveStar sliceId={this.props.slice.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.slice_id}`} > <i className="fa fa-edit" /> </a> </TooltipWrapper> </span> } <div className="pull-right"> {this.props.chartStatus === 'success' && queryResponse && queryResponse.is_cached && <TooltipWrapper tooltip="Loaded from cache. Click to force refresh" label="cache-desc" > <Label style={{ fontSize: '10px', marginRight: '5px', cursor: 'pointer' }} onClick={this.runQuery.bind(this)} > cached </Label> </TooltipWrapper> } <Timer startTime={this.props.chartUpdateStartTime} endTime={this.props.chartUpdateEndTime} isRunning={this.props.chartStatus === 'loading'} status={CHART_STATUS_MAP[this.props.chartStatus]} style={{ fontSize: '10px', marginRight: '5px' }} /> <ExploreActionButtons slice={this.state.mockSlice} canDownload={this.props.can_download} chartStatus={this.props.chartStatus} queryResponse={queryResponse} queryEndpoint={getExploreUrl(this.props.latestQueryFormData, 'query')} /> </div> </div> } > {this.renderChart()} </Panel> </div> ); } } ChartContainer.propTypes = propTypes; function mapStateToProps(state) { const formData = getFormDataFromControls(state.controls); return { alert: state.chartAlert, can_download: state.can_download, chartStatus: state.chartStatus, chartUpdateEndTime: state.chartUpdateEndTime, chartUpdateStartTime: state.chartUpdateStartTime, column_formats: state.datasource ? state.datasource.column_formats : null, containerId: state.slice ? `slice-container-${state.slice.slice_id}` : 'slice-container', formData, latestQueryFormData: state.latestQueryFormData, isStarred: state.isStarred, queryResponse: state.queryResponse, slice: state.slice, standalone: state.standalone, table_name: formData.datasource_name, viz_type: formData.viz_type, triggerRender: state.triggerRender, datasourceType: state.datasource ? state.datasource.type : null, }; } export default connect(mapStateToProps, () => ({}))(ChartContainer); |