Skip to content

Commit

Permalink
apply custom css for dashboard initially load (apache#4031)
Browse files Browse the repository at this point in the history
  • Loading branch information
Grace Guo authored and michellethomas committed May 23, 2018
1 parent 39d7658 commit 141508f
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 25 deletions.
26 changes: 24 additions & 2 deletions superset/assets/javascripts/dashboard/components/Controls.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,11 @@ class Controls extends React.PureComponent {
};
this.refresh = this.refresh.bind(this);
this.toggleModal = this.toggleModal.bind(this);
this.updateDom = this.updateDom.bind(this);
}
componentWillMount() {
this.updateDom(this.state.css);

$.get('/csstemplateasyncmodelview/api/read', (data) => {
const cssTemplates = data.result.map(row => ({
value: row.template_name,
Expand All @@ -88,9 +91,28 @@ class Controls extends React.PureComponent {
this.setState({ currentModal });
}
changeCss(css) {
this.setState({ css });
this.setState({ css }, () => {
this.updateDom(css);
});
this.props.onChange();
}
updateDom(css) {
const className = 'CssEditor-css';
const head = document.head || document.getElementsByTagName('head')[0];
let style = document.querySelector('.' + className);

if (!style) {
style = document.createElement('style');
style.className = className;
style.type = 'text/css';
head.appendChild(style);
}
if (style.styleSheet) {
style.styleSheet.cssText = css;
} else {
style.innerHTML = css;
}
}
render() {
const { dashboard, userId, filters,
addSlicesToDashboard, startPeriodicRender,
Expand Down Expand Up @@ -175,7 +197,7 @@ class Controls extends React.PureComponent {
faIcon="css3"
/>
}
initialCss={dashboard.css}
initialCss={this.state.css}
templates={this.state.cssTemplates}
onChange={this.changeCss.bind(this)}
/>
Expand Down
26 changes: 3 additions & 23 deletions superset/assets/javascripts/dashboard/components/CssEditor.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -30,30 +30,10 @@ class CssEditor extends React.PureComponent {
cssTemplateOptions: [],
};
}
componentWillMount() {
this.updateDom();
}
changeCss(css) {
this.setState({ css }, this.updateDom);
this.props.onChange(css);
}
updateDom() {
const css = this.state.css;
const className = 'CssEditor-css';
const head = document.head || document.getElementsByTagName('head')[0];
let style = document.querySelector('.' + className);

if (!style) {
style = document.createElement('style');
style.className = className;
style.type = 'text/css';
head.appendChild(style);
}
if (style.styleSheet) {
style.styleSheet.cssText = css;
} else {
style.innerHTML = css;
}
this.setState({ css }, () => {
this.props.onChange(css);
});
}
changeCssTemplate(opt) {
this.changeCss(opt.css);
Expand Down

0 comments on commit 141508f

Please sign in to comment.