2018-11-09 06:17:41 -06:00
|
|
|
import React, { PureComponent } from 'react';
|
|
|
|
import CustomScrollbar from 'app/core/components/CustomScrollbar/CustomScrollbar';
|
|
|
|
import { FadeIn } from 'app/core/components/Animations/FadeIn';
|
|
|
|
|
|
|
|
interface Props {
|
|
|
|
children: JSX.Element;
|
2018-11-21 13:32:04 -06:00
|
|
|
heading: string;
|
2018-11-15 00:33:59 -06:00
|
|
|
main?: EditorToolBarView;
|
2018-11-09 06:17:41 -06:00
|
|
|
toolbarItems: EditorToolBarView[];
|
|
|
|
}
|
|
|
|
|
|
|
|
export interface EditorToolBarView {
|
|
|
|
title: string;
|
2018-11-09 08:49:55 -06:00
|
|
|
imgSrc?: string;
|
|
|
|
icon?: string;
|
2018-11-20 07:17:03 -06:00
|
|
|
disabled?: boolean;
|
|
|
|
onClick?: () => void;
|
2018-11-20 09:33:26 -06:00
|
|
|
render: (closeFunction: any) => JSX.Element | JSX.Element[];
|
2018-11-09 06:17:41 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
interface State {
|
|
|
|
openView?: EditorToolBarView;
|
2018-11-22 05:19:52 -06:00
|
|
|
test: boolean;
|
2018-11-09 06:17:41 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
export class EditorTabBody extends PureComponent<Props, State> {
|
|
|
|
constructor(props) {
|
|
|
|
super(props);
|
|
|
|
|
|
|
|
this.state = {
|
|
|
|
openView: null,
|
2018-11-22 05:19:52 -06:00
|
|
|
test: false,
|
2018-11-09 06:17:41 -06:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2018-11-22 05:19:52 -06:00
|
|
|
componentDidMount() {
|
|
|
|
this.setState({ test: true });
|
|
|
|
}
|
|
|
|
|
2018-11-09 06:17:41 -06:00
|
|
|
onToggleToolBarView = (item: EditorToolBarView) => {
|
|
|
|
this.setState({
|
|
|
|
openView: item === this.state.openView ? null : item,
|
|
|
|
});
|
2018-11-09 08:49:55 -06:00
|
|
|
};
|
2018-11-09 06:17:41 -06:00
|
|
|
|
|
|
|
onCloseOpenView = () => {
|
|
|
|
this.setState({ openView: null });
|
2018-11-09 08:49:55 -06:00
|
|
|
};
|
2018-11-09 06:17:41 -06:00
|
|
|
|
2018-11-20 07:17:03 -06:00
|
|
|
static getDerivedStateFromProps(props, state) {
|
|
|
|
if (state.openView) {
|
2018-11-20 09:33:26 -06:00
|
|
|
const activeToolbarItem = props.toolbarItems.find(
|
|
|
|
item => item.title === state.openView.title && item.icon === state.openView.icon
|
|
|
|
);
|
2018-11-20 07:17:03 -06:00
|
|
|
if (activeToolbarItem) {
|
|
|
|
return {
|
|
|
|
...state,
|
|
|
|
openView: activeToolbarItem,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return state;
|
|
|
|
}
|
|
|
|
|
2018-11-09 08:49:55 -06:00
|
|
|
renderMainSelection(view: EditorToolBarView) {
|
2018-11-09 06:17:41 -06:00
|
|
|
return (
|
2018-11-20 10:11:47 -06:00
|
|
|
<div className="toolbar__main" onClick={() => this.onToggleToolBarView(view)} key={view.title + view.icon}>
|
2018-11-10 10:27:25 -06:00
|
|
|
<img className="toolbar__main-image" src={view.imgSrc} />
|
|
|
|
<div className="toolbar__main-name">{view.title}</div>
|
2018-11-09 06:17:41 -06:00
|
|
|
<i className="fa fa-caret-down" />
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2018-11-09 08:49:55 -06:00
|
|
|
renderButton(view: EditorToolBarView) {
|
2018-11-20 07:17:03 -06:00
|
|
|
const onClick = () => {
|
|
|
|
if (view.onClick) {
|
|
|
|
view.onClick();
|
|
|
|
}
|
|
|
|
this.onToggleToolBarView(view);
|
|
|
|
};
|
|
|
|
|
2018-11-09 08:49:55 -06:00
|
|
|
return (
|
2018-11-20 10:11:47 -06:00
|
|
|
<div className="nav-buttons" key={view.title + view.icon}>
|
2018-11-20 07:17:03 -06:00
|
|
|
<button className="btn navbar-button" onClick={onClick} disabled={view.disabled}>
|
2018-11-09 13:33:33 -06:00
|
|
|
{view.icon && <i className={view.icon} />} {view.title}
|
2018-11-09 08:49:55 -06:00
|
|
|
</button>
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2018-11-09 06:17:41 -06:00
|
|
|
renderOpenView(view: EditorToolBarView) {
|
|
|
|
return (
|
2018-11-10 10:27:25 -06:00
|
|
|
<div className="toolbar-subview">
|
|
|
|
<button className="toolbar-subview__close" onClick={this.onCloseOpenView}>
|
2018-11-09 13:33:33 -06:00
|
|
|
<i className="fa fa-chevron-up" />
|
2018-11-09 06:17:41 -06:00
|
|
|
</button>
|
2018-11-19 07:35:16 -06:00
|
|
|
{view.render(this.onCloseOpenView)}
|
2018-11-09 06:17:41 -06:00
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
render() {
|
2018-11-21 13:32:04 -06:00
|
|
|
const { children, toolbarItems, main, heading } = this.props;
|
2018-11-22 05:19:52 -06:00
|
|
|
const { openView, test } = this.state;
|
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>
|
|
|
|
{main && this.renderMainSelection(main)}
|
|
|
|
<div className="gf-form--grow" />
|
|
|
|
{toolbarItems.map(item => this.renderButton(item))}
|
|
|
|
</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:13:28 -06:00
|
|
|
<FadeIn in={openView !== null} duration={200}>
|
|
|
|
<div className="panel-editor__toolbar-view">{openView && this.renderOpenView(openView)}</div>
|
|
|
|
</FadeIn>
|
2018-11-22 05:19:52 -06:00
|
|
|
<div className="panel-editor__content">
|
|
|
|
<FadeIn in={test} duration={50}>
|
|
|
|
{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
|
|
|
}
|
|
|
|
}
|