forked from firefox-devtools/debugger
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTabs.js
More file actions
206 lines (173 loc) · 4.94 KB
/
Tabs.js
File metadata and controls
206 lines (173 loc) · 4.94 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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at <http://mozilla.org/MPL/2.0/>. */
// @flow
import React, { PureComponent } from "react";
import { connect } from "react-redux";
import { bindActionCreators } from "redux";
import * as I from "immutable";
import { getSelectedSource, getSourcesForTabs } from "../../selectors";
import { isVisible } from "../../utils/ui";
import { getHiddenTabs } from "../../utils/tabs";
import { getFilename, isPretty } from "../../utils/source";
import actions from "../../actions";
import { debounce } from "lodash";
import "./Tabs.css";
import Tab from "./Tab";
import PaneToggleButton from "../shared/Button/PaneToggle";
import Dropdown from "../shared/Dropdown";
import type { List } from "immutable";
import type { SourceRecord } from "../../types";
type SourcesList = List<SourceRecord>;
type Props = {
tabSources: SourcesList,
selectedSource: SourceRecord,
selectSpecificSource: Object => void,
moveTab: (string, number) => void,
closeTab: string => void,
togglePaneCollapse: () => void,
showSource: string => void,
horizontal: boolean,
startPanelCollapsed: boolean,
endPanelCollapsed: boolean
};
type State = {
dropdownShown: boolean,
hiddenTabs: SourcesList
};
class Tabs extends PureComponent<Props, State> {
onTabContextMenu: Function;
showContextMenu: Function;
updateHiddenTabs: Function;
toggleSourcesDropdown: Function;
renderDropdownSource: Function;
renderTabs: Function;
renderDropDown: Function;
renderStartPanelToggleButton: Function;
renderEndPanelToggleButton: Function;
onResize: Function;
constructor(props) {
super(props);
this.state = {
dropdownShown: false,
hiddenTabs: I.List()
};
this.onResize = debounce(() => {
this.updateHiddenTabs();
});
}
componentDidUpdate(prevProps) {
if (!(prevProps === this.props)) {
this.updateHiddenTabs();
}
}
componentDidMount() {
this.updateHiddenTabs();
window.addEventListener("resize", this.onResize);
}
componentWillUnmount() {
window.removeEventListener("resize", this.onResize);
}
/*
* Updates the hiddenSourceTabs state, by
* finding the source tabs which are wrapped and are not on the top row.
*/
updateHiddenTabs() {
if (!this.refs.sourceTabs) {
return;
}
const { selectedSource, tabSources, moveTab } = this.props;
const sourceTabEls = this.refs.sourceTabs.children;
const hiddenTabs = getHiddenTabs(tabSources, sourceTabEls);
if (isVisible() && hiddenTabs.indexOf(selectedSource) !== -1) {
return moveTab(selectedSource.url, 0);
}
this.setState({ hiddenTabs });
}
toggleSourcesDropdown(e) {
this.setState(prevState => ({
dropdownShown: !prevState.dropdownShown
}));
}
getIconClass(source: SourceRecord) {
if (isPretty(source)) {
return "prettyPrint";
}
if (source.isBlackBoxed) {
return "blackBox";
}
return "file";
}
renderDropdownSource = (source: SourceRecord) => {
const { selectSpecificSource } = this.props;
const filename = getFilename(source.toJS());
const onClick = () => selectSpecificSource(source.id);
return (
<li key={source.id} onClick={onClick}>
<img className={`dropdown-icon ${this.getIconClass(source)}`} />
{filename}
</li>
);
};
renderTabs() {
const { tabSources } = this.props;
if (!tabSources) {
return;
}
return (
<div className="source-tabs" ref="sourceTabs">
{tabSources.map((source, index) => <Tab key={index} source={source} />)}
</div>
);
}
renderDropdown() {
const hiddenTabs = this.state.hiddenTabs;
if (!hiddenTabs || hiddenTabs.size == 0) {
return null;
}
const Panel = <ul>{hiddenTabs.map(this.renderDropdownSource)}</ul>;
return <Dropdown panel={Panel} icon={"»"} />;
}
renderStartPanelToggleButton() {
return (
<PaneToggleButton
position="start"
collapsed={!this.props.startPanelCollapsed}
handleClick={this.props.togglePaneCollapse}
/>
);
}
renderEndPanelToggleButton() {
const { horizontal, endPanelCollapsed, togglePaneCollapse } = this.props;
if (!horizontal) {
return;
}
return (
<PaneToggleButton
position="end"
collapsed={!endPanelCollapsed}
handleClick={togglePaneCollapse}
horizontal={horizontal}
/>
);
}
render() {
return (
<div className="source-header">
{this.renderStartPanelToggleButton()}
{this.renderTabs()}
{this.renderDropdown()}
{this.renderEndPanelToggleButton()}
</div>
);
}
}
export default connect(
state => {
return {
selectedSource: getSelectedSource(state),
tabSources: getSourcesForTabs(state)
};
},
dispatch => bindActionCreators(actions, dispatch)
)(Tabs);