{
private _dragSource?: DragSource;
private _rootNode: HTMLElement | null;
private _workbench: Workbench | undefined;
public constructor(props: IModuleProps) {
super(props);
this._rootNode = null;
}
public componentDidMount(): void {
this._setWorkbench(this.props.workbench);
}
public componentDidUpdate(oldProps: IModuleProps): void {
const disabledChanged = (!!oldProps.disabled !== !!this.props.disabled);
if (disabledChanged) {
this._removeDragSourceFromWorkbench(oldProps.workbench);
}
this._setWorkbench(this.props.workbench);
if (disabledChanged) {
this._updateDragSourceForProps( /* rootNodeChanged = */ false);
}
}
public componentWillUnmount(): void {
this._setWorkbench(undefined);
}
public render(): React.ReactElement {
const { badge, disabled, icon, label } = this.props;
const classes = ["wb-module"];
if (disabled) {
classes.push("wb-module-disabled");
}
const iconSpan = icon ? {icon} : null;
const labelSpan = label ? {label} : null;
const iconAndBadge = iconSpan ? (
{iconSpan}{badge}
) : null;
return (
{iconAndBadge}
{labelSpan}
);
}
private _onClick = (event: React.SyntheticEvent) => {
const { onClick } = this.props;
if (onClick) {
onClick(event);
}
}
private _onWorkbenchLayoutChanged = () => {
this._removeDragSourceFromWorkbench(this.props.workbench);
this._updateDragSourceForProps( /* rootNodeChanged = */ false);
}
private _setRootNode = (node: HTMLElement | null) => {
if (this._rootNode === node) {
return;
}
this._rootNode = node;
this._updateDragSourceForProps( /* rootNodeChanged = */ true);
}
private _setWorkbench = (workbench: Workbench | undefined) => {
if (this._workbench === workbench) {
return;
}
if (this._workbench !== undefined) {
this._workbench.off("layoutChanged", this._onWorkbenchLayoutChanged);
}
this._workbench = workbench;
if (this._workbench !== undefined) {
this._workbench.on("layoutChanged", this._onWorkbenchLayoutChanged);
}
this._updateDragSourceForProps(false);
}
private _updateDragSourceForProps = (
rootNodeChanged: boolean,
props?: IModuleProps
) => {
props = (props !== undefined) ? props : this.props;
const { disabled, workbench } = props;
const node = this._rootNode;
const needsDragSource = (node !== null && node !== undefined &&
workbench !== undefined &&
workbench.isRendered && !disabled);
if (needsDragSource) {
if (this._dragSource === undefined || rootNodeChanged) {
this._removeDragSourceFromWorkbench(workbench);
this._dragSource = workbench!.createDragSource(
node!, createItemConfigurationFromProps(props) as any
);
}
} else {
this._removeDragSourceFromWorkbench(workbench);
}
}
private _removeDragSourceFromWorkbench = (workbench: Workbench | undefined) => {
if (this._dragSource !== undefined) {
if (workbench !== undefined) {
workbench.removeDragSource(this._dragSource);
}
this._dragSource = undefined;
}
}
}