forked from deephaven/web-client-ui
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPanelContextMenu.tsx
More file actions
191 lines (163 loc) · 5.05 KB
/
PanelContextMenu.tsx
File metadata and controls
191 lines (163 loc) · 5.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
import React, { PureComponent, type ReactElement } from 'react';
import {
ContextActions,
type ResolvableContextAction,
} from '@deephaven/components';
import type { Container, EventEmitter, Tab } from '@deephaven/golden-layout';
import {
type CustomizableWorkspace,
type RootState,
getWorkspace,
setWorkspace as setWorkspaceAction,
} from '@deephaven/redux';
import { connect } from 'react-redux';
import { type ClosedPanel } from './PanelManager';
import { LayoutUtils } from './layout';
import { PanelEvent } from './PanelEvent';
interface PanelContextMenuProps {
additionalActions: ResolvableContextAction[];
glContainer: Container;
glEventHub: EventEmitter;
workspace: CustomizableWorkspace;
}
interface HasContainer {
container: {
close: () => void;
};
}
class PanelContextMenu extends PureComponent<
PanelContextMenuProps,
Record<string, never>
> {
static defaultProps = {
additionalActions: [],
};
constructor(props: PanelContextMenuProps) {
super(props);
this.handleCloseTab = this.handleCloseTab.bind(this);
this.handleCloseTabsRight = this.handleCloseTabsRight.bind(this);
this.handleCloseTabsAll = this.handleCloseTabsAll.bind(this);
this.handleReopenLast = this.handleReopenLast.bind(this);
}
getAllTabs(): Tab[] {
// return a clone of the tabs array, or returns empty array if null
const { glContainer } = this.props;
return [...(glContainer?.tab?.header?.tabs ?? [])];
}
handleCloseTab(): void {
const { glContainer } = this.props;
glContainer.close();
}
handleCloseTabsAll(): void {
const tabs = this.getAllTabs();
// No need to check if isClosable, golden-layout returns
// false when attempting to close tabs that you can't
tabs.forEach(
tab => (tab?.contentItem as unknown as HasContainer).container?.close()
);
}
handleCloseTabsRight(): void {
const { glContainer } = this.props;
const tabs = this.getAllTabs();
for (let i = tabs.length - 1; i > 0; i -= 1) {
if (
tabs[i].contentItem?.config?.id ===
glContainer.tab?.contentItem?.config?.id
) {
break; // end when we get back to current id
}
// eslint-disable-next-line no-unused-expressions
(tabs[i]?.contentItem as unknown as HasContainer).container?.close();
}
}
handleReopenLast(): void {
const { glContainer, glEventHub } = this.props;
glEventHub.emit(PanelEvent.REOPEN_LAST, glContainer);
}
canCloseTabsRight(): boolean {
const { glContainer } = this.props;
const tabs = this.getAllTabs();
let disabled = true;
for (let i = tabs.length - 1; i > 0; i -= 1) {
if (
tabs[i].contentItem?.config?.id ===
glContainer.tab?.contentItem?.config?.id
) {
break; // end when we get back to current id
}
const closable = Boolean(tabs[i]?.contentItem?.config?.isClosable);
if (closable) {
disabled = false;
break; // end if we find a closeable tab
}
}
return disabled;
}
canCloseAny(): boolean {
const tabs = this.getAllTabs();
let disabled = true;
for (let i = tabs.length - 1; i > 0; i -= 1) {
const closable = Boolean(tabs[i]?.contentItem?.config?.isClosable);
if (closable) {
disabled = false;
break;
}
}
return disabled;
}
canReopenLast(): boolean {
const { workspace, glContainer } = this.props;
const stackId = LayoutUtils.getStackForConfig(
glContainer.layoutManager.root,
glContainer.getConfig()
)?.config.id;
return !workspace.data.closed?.some(
panel => (panel as ClosedPanel).parentStackId === stackId
);
}
render(): ReactElement {
const { additionalActions, glContainer } = this.props;
const contextActions = [...additionalActions];
contextActions.push(() => ({
title: 'Re-open closed panel',
group: ContextActions.groups.medium + 2004,
action: this.handleReopenLast,
disabled: this.canReopenLast(),
}));
const closable = glContainer.tab?.contentItem?.config?.isClosable;
contextActions.push({
title: 'Close',
order: 10,
group: ContextActions.groups.low,
action: this.handleCloseTab,
disabled: closable === undefined || !closable,
});
// pushed as function so the disable check happens on run
contextActions.push(() => ({
title: 'Close Tabs to Right',
order: 20,
group: ContextActions.groups.low,
action: this.handleCloseTabsRight,
disabled: this.canCloseTabsRight(),
}));
contextActions.push(() => ({
title: 'Close All',
order: 30,
group: ContextActions.groups.low,
action: this.handleCloseTabsAll,
disabled: this.canCloseAny(),
}));
return <ContextActions actions={contextActions} />;
}
}
const mapStateToProps = (
state: RootState
): {
workspace: CustomizableWorkspace;
} => ({
workspace: getWorkspace(state),
});
const ConnectedPanelContextMenu = connect(mapStateToProps, {
setWorkspace: setWorkspaceAction,
})(PanelContextMenu);
export default ConnectedPanelContextMenu;