const $ = window.$ = require('jquery'); import React from 'react'; import { ButtonGroup } from 'react-bootstrap'; import Button from '../../components/Button'; import { showModal } from '../../modules/utils'; import CssEditor from './CssEditor'; import RefreshIntervalModal from './RefreshIntervalModal'; import CodeModal from './CodeModal'; import SliceAdder from './SliceAdder'; const propTypes = { dashboard: React.PropTypes.object.isRequired, }; class Controls extends React.PureComponent { constructor(props) { super(props); this.state = { css: props.dashboard.css, cssTemplates: [], }; } refresh() { this.props.dashboard.sliceObjects.forEach((slice) => { slice.render(true); }); } componentWillMount() { $.get('/csstemplateasyncmodelview/api/read', (data) => { const cssTemplates = data.result.map((row) => ({ value: row.template_name, css: row.css, label: row.template_name, })); this.setState({ cssTemplates }); }); } save() { const dashboard = this.props.dashboard; const expandedSlices = {}; $.each($('.slice_info'), function () { const widget = $(this).parents('.widget'); const sliceDescription = widget.find('.slice_description'); if (sliceDescription.is(':visible')) { expandedSlices[$(widget).attr('data-slice-id')] = true; } }); const positions = dashboard.reactGridLayout.serialize(); const data = { positions, css: this.state.css, expanded_slices: expandedSlices, }; $.ajax({ type: 'POST', url: '/superset/save_dash/' + dashboard.id + '/', data: { data: JSON.stringify(data), }, success() { dashboard.onSave(); showModal({ title: 'Success', body: 'This dashboard was saved successfully.', }); }, error(error) { const errorMsg = this.getAjaxErrorMsg(error); showModal({ title: 'Error', body: 'Sorry, there was an error saving this dashboard: ' + errorMsg, }); }, }); } changeCss(css) { this.setState({ css }); this.props.dashboard.onChange(); } render() { const dashboard = this.props.dashboard; const canSave = dashboard.context.dash_save_perm; return ( } /> dashboard.startPeriodicRender(refreshInterval * 1000)} triggerNode={ } /> } /> } initialCss={dashboard.css} templates={this.state.cssTemplates} onChange={this.changeCss.bind(this)} /> ); } } Controls.propTypes = propTypes; export default Controls;