2018-12-14 01:34:54 -06:00
|
|
|
// Libraries
|
2018-11-09 06:17:41 -06:00
|
|
|
import React, { PureComponent } from 'react';
|
2018-12-14 01:34:54 -06:00
|
|
|
|
|
|
|
// Components
|
2018-11-09 06:17:41 -06:00
|
|
|
import CustomScrollbar from 'app/core/components/CustomScrollbar/CustomScrollbar';
|
|
|
|
import { FadeIn } from 'app/core/components/Animations/FadeIn';
|
2018-12-14 01:34:54 -06:00
|
|
|
import { PanelOptionSection } from './PanelOptionSection';
|
2018-11-09 06:17:41 -06:00
|
|
|
|
|
|
|
interface Props {
|
|
|
|
children: JSX.Element;
|
2018-11-21 13:32:04 -06:00
|
|
|
heading: string;
|
2018-12-13 00:44:58 -06:00
|
|
|
renderToolbar?: () => JSX.Element;
|
2018-12-21 04:57:21 -06:00
|
|
|
toolbarItems?: EditorToolbarView[];
|
2018-12-11 23:52:08 -06:00
|
|
|
}
|
|
|
|
|
2018-12-21 04:57:21 -06:00
|
|
|
export interface EditorToolbarView {
|
2018-12-13 14:24:41 -06:00
|
|
|
title?: string;
|
|
|
|
heading?: string;
|
2018-12-11 23:52:08 -06:00
|
|
|
icon?: string;
|
|
|
|
disabled?: boolean;
|
|
|
|
onClick?: () => void;
|
2019-01-04 05:18:49 -06:00
|
|
|
render?: () => JSX.Element;
|
2018-12-21 04:57:21 -06:00
|
|
|
action?: () => void;
|
2019-01-04 05:38:50 -06:00
|
|
|
btnType?: 'danger';
|
2018-11-09 06:17:41 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
interface State {
|
2018-12-21 04:57:21 -06:00
|
|
|
openView?: EditorToolbarView;
|
2018-12-11 23:52:08 -06:00
|
|
|
isOpen: boolean;
|
2018-11-22 05:22:59 -06:00
|
|
|
fadeIn: boolean;
|
2018-11-09 06:17:41 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
export class EditorTabBody extends PureComponent<Props, State> {
|
2018-12-11 23:52:08 -06:00
|
|
|
static defaultProps = {
|
|
|
|
toolbarItems: [],
|
|
|
|
};
|
|
|
|
|
2018-11-09 06:17:41 -06:00
|
|
|
constructor(props) {
|
|
|
|
super(props);
|
|
|
|
|
|
|
|
this.state = {
|
2018-12-11 23:52:08 -06:00
|
|
|
openView: null,
|
2018-11-22 05:22:59 -06:00
|
|
|
fadeIn: false,
|
2018-12-11 23:52:08 -06:00
|
|
|
isOpen: false,
|
2018-11-09 06:17:41 -06:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2018-11-22 05:19:52 -06:00
|
|
|
componentDidMount() {
|
2018-11-22 05:22:59 -06:00
|
|
|
this.setState({ fadeIn: true });
|
2018-11-22 05:19:52 -06:00
|
|
|
}
|
|
|
|
|
2018-12-21 04:57:21 -06:00
|
|
|
onToggleToolBarView = (item: EditorToolbarView) => {
|
2018-12-11 23:52:08 -06:00
|
|
|
this.setState({
|
|
|
|
openView: item,
|
|
|
|
isOpen: !this.state.isOpen,
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
onCloseOpenView = () => {
|
|
|
|
this.setState({ isOpen: false });
|
|
|
|
};
|
|
|
|
|
|
|
|
static getDerivedStateFromProps(props, state) {
|
|
|
|
if (state.openView) {
|
|
|
|
const activeToolbarItem = props.toolbarItems.find(
|
|
|
|
item => item.title === state.openView.title && item.icon === state.openView.icon
|
|
|
|
);
|
|
|
|
if (activeToolbarItem) {
|
|
|
|
return {
|
|
|
|
...state,
|
|
|
|
openView: activeToolbarItem,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return state;
|
|
|
|
}
|
|
|
|
|
2018-12-21 04:57:21 -06:00
|
|
|
renderButton(view: EditorToolbarView) {
|
2018-12-11 23:52:08 -06:00
|
|
|
const onClick = () => {
|
|
|
|
if (view.onClick) {
|
|
|
|
view.onClick();
|
|
|
|
}
|
2018-12-21 04:57:21 -06:00
|
|
|
|
2019-01-04 05:38:50 -06:00
|
|
|
if (view.render) {
|
2018-12-21 04:57:21 -06:00
|
|
|
this.onToggleToolBarView(view);
|
|
|
|
}
|
2018-12-11 23:52:08 -06:00
|
|
|
};
|
|
|
|
|
|
|
|
return (
|
|
|
|
<div className="nav-buttons" key={view.title + view.icon}>
|
|
|
|
<button className="btn navbar-button" onClick={onClick} disabled={view.disabled}>
|
|
|
|
{view.icon && <i className={view.icon} />} {view.title}
|
|
|
|
</button>
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2018-12-21 04:57:21 -06:00
|
|
|
renderOpenView(view: EditorToolbarView) {
|
2018-12-11 23:52:08 -06:00
|
|
|
return (
|
2018-12-14 01:34:54 -06:00
|
|
|
<PanelOptionSection title={view.title || view.heading} onClose={this.onCloseOpenView}>
|
|
|
|
{view.render()}
|
|
|
|
</PanelOptionSection>
|
2018-12-11 23:52:08 -06:00
|
|
|
);
|
|
|
|
}
|
2018-11-09 06:17:41 -06:00
|
|
|
|
|
|
|
render() {
|
2018-12-11 23:52:08 -06:00
|
|
|
const { children, renderToolbar, heading, toolbarItems } = this.props;
|
|
|
|
const { openView, fadeIn, isOpen } = this.state;
|
2018-11-22 05:22:59 -06:00
|
|
|
|
2018-11-09 06:17:41 -06:00
|
|
|
return (
|
2018-11-22 04:41:33 -06:00
|
|
|
<>
|
2018-11-21 13:32:04 -06:00
|
|
|
<div className="toolbar">
|
|
|
|
<div className="toolbar__heading">{heading}</div>
|
2018-12-11 10:03:38 -06:00
|
|
|
{renderToolbar && renderToolbar()}
|
2018-12-13 00:44:58 -06:00
|
|
|
{toolbarItems.length > 0 && (
|
|
|
|
<>
|
|
|
|
<div className="gf-form--grow" />
|
|
|
|
{toolbarItems.map(item => this.renderButton(item))}
|
|
|
|
</>
|
|
|
|
)}
|
2018-11-21 13:32:04 -06:00
|
|
|
</div>
|
2018-11-09 08:49:55 -06:00
|
|
|
<div className="panel-editor__scroll">
|
2018-11-12 10:54:44 -06:00
|
|
|
<CustomScrollbar autoHide={false}>
|
2018-11-22 05:19:52 -06:00
|
|
|
<div className="panel-editor__content">
|
2018-12-13 06:05:43 -06:00
|
|
|
<FadeIn in={isOpen} duration={200} unmountOnExit={true}>
|
|
|
|
{openView && this.renderOpenView(openView)}
|
|
|
|
</FadeIn>
|
2018-11-22 05:22:59 -06:00
|
|
|
<FadeIn in={fadeIn} duration={50}>
|
2018-11-22 05:19:52 -06:00
|
|
|
{children}
|
|
|
|
</FadeIn>
|
|
|
|
</div>
|
2018-11-09 08:49:55 -06:00
|
|
|
</CustomScrollbar>
|
|
|
|
</div>
|
2018-11-22 04:41:33 -06:00
|
|
|
</>
|
2018-11-09 08:49:55 -06:00
|
|
|
);
|
2018-11-09 06:17:41 -06:00
|
|
|
}
|
|
|
|
}
|