Debian Codegraph @

jupyter-widgets-controls/lib/lumino/tabpanel.js

raw
1 /* This file has code derived from Lumino. The license for this Lumino code is:
2
3 Copyright (c) 2019 Project Jupyter Contributors
4 All rights reserved.
5
6 Redistribution and use in source and binary forms, with or without
7 modification, are permitted provided that the following conditions are met:
8
9 1. Redistributions of source code must retain the above copyright notice, this
10 list of conditions and the following disclaimer.
11
12 2. Redistributions in binary form must reproduce the above copyright notice,
13 this list of conditions and the following disclaimer in the documentation
14 and/or other materials provided with the distribution.
15
16 3. Neither the name of the copyright holder nor the names of its
17 contributors may be used to endorse or promote products derived from
18 this software without specific prior written permission.
19
20 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
23 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
24 FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
26 SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
27 CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
28 OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30
31
32 Copyright (c) 2014-2017, PhosphorJS Contributors
33 All rights reserved.
34
35 Redistribution and use in source and binary forms, with or without
36 modification, are permitted provided that the following conditions are met:
37
38 * Redistributions of source code must retain the above copyright notice, this
39 list of conditions and the following disclaimer.
40
41 * Redistributions in binary form must reproduce the above copyright notice,
42 this list of conditions and the following disclaimer in the documentation
43 and/or other materials provided with the distribution.
44
45 * Neither the name of the copyright holder nor the names of its
46 contributors may be used to endorse or promote products derived from
47 this software without specific prior written permission.
48
49 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
50 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
51 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
52 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
53 FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
54 DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
55 SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
56 CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
57 OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
58 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
59 */
60 import { MessageLoop } from '@lumino/messaging';
61 import { Signal } from '@lumino/signaling';
62 import { Platform } from '@lumino/domutils';
63 import { Panel, PanelLayout, TabBar, Widget } from '@lumino/widgets';
64 /**
65 * A panel where visible widgets are stacked atop one another.
66 *
67 * #### Notes
68 * This class provides a convenience wrapper around a [[PanelLayout]].
69 */
70 export class EventedPanel extends Panel {
71 constructor() {
72 super(...arguments);
73 this._widgetRemoved = new Signal(this);
74 }
75 /**
76 * A signal emitted when a widget is removed from the panel.
77 */
78 get widgetRemoved() {
79 return this._widgetRemoved;
80 }
81 /**
82 * A message handler invoked on a `'child-removed'` message.
83 */
84 onChildRemoved(msg) {
85 this._widgetRemoved.emit(msg.child);
86 }
87 }
88 /**
89 * A widget which combines a `TabBar` and a `EventedPanel`.
90 *
91 * #### Notes
92 * This is a simple panel which handles the common case of a tab bar
93 * placed next to a content area. The selected tab controls the widget
94 * which is shown in the content area.
95 *
96 * For use cases which require more control than is provided by this
97 * panel, the `TabBar` widget may be used independently.
98 *
99 * TODO: Support setting the direction??
100 */
101 export class TabPanel extends Widget {
102 /**
103 * Construct a new tab panel.
104 *
105 * @param options - The options for initializing the tab panel.
106 */
107 constructor(options = {}) {
108 super();
109 this._currentChanged = new Signal(this);
110 this.addClass('jupyter-widget-TabPanel');
111 // Create the tab bar and contents panel.
112 this.tabBar = new TabBar(options);
113 this.tabBar.addClass('jupyter-widget-TabPanel-tabBar');
114 this.tabContents = new EventedPanel();
115 this.tabContents.addClass('jupyter-widget-TabPanel-tabContents');
116 // Connect the tab bar signal handlers.
117 this.tabBar.tabMoved.connect(this._onTabMoved, this);
118 this.tabBar.currentChanged.connect(this._onCurrentChanged, this);
119 this.tabBar.tabCloseRequested.connect(this._onTabCloseRequested, this);
120 this.tabBar.tabActivateRequested.connect(this._onTabActivateRequested, this);
121 // Connect the evented panel signal handlers.
122 this.tabContents.widgetRemoved.connect(this._onWidgetRemoved, this);
123 // Create the layout.
124 const layout = new PanelLayout();
125 // Add the child widgets to the layout.
126 layout.addWidget(this.tabBar);
127 layout.addWidget(this.tabContents);
128 // Install the layout on the tab panel.
129 this.layout = layout;
130 }
131 /**
132 * A signal emitted when the current tab is changed.
133 *
134 * #### Notes
135 * This signal is emitted when the currently selected tab is changed
136 * either through user or programmatic interaction.
137 *
138 * Notably, this signal is not emitted when the index of the current
139 * tab changes due to tabs being inserted, removed, or moved. It is
140 * only emitted when the actual current tab node is changed.
141 */
142 get currentChanged() {
143 return this._currentChanged;
144 }
145 /**
146 * Get the index of the currently selected tab.
147 *
148 * #### Notes
149 * This will be `null` if no tab is selected.
150 */
151 get currentIndex() {
152 const currentIndex = this.tabBar.currentIndex;
153 // Lumino tab bars have an index of -1 if no tab is selected
154 return currentIndex === -1 ? null : currentIndex;
155 }
156 /**
157 * Set the index of the currently selected tab.
158 *
159 * #### Notes
160 * If the index is out of range, it will be set to `null`.
161 */
162 set currentIndex(value) {
163 this.tabBar.currentIndex = value === null ? -1 : value;
164 }
165 /**
166 * Get the currently selected widget.
167 *
168 * #### Notes
169 * This will be `null` if there is no selected tab.
170 */
171 get currentWidget() {
172 const title = this.tabBar.currentTitle;
173 return title ? title.owner : null;
174 }
175 /**
176 * Set the currently selected widget.
177 *
178 * #### Notes
179 * If the widget is not in the panel, it will be set to `null`.
180 */
181 set currentWidget(value) {
182 this.tabBar.currentTitle = value ? value.title : null;
183 }
184 /**
185 * Get the whether the tabs are movable by the user.
186 *
187 * #### Notes
188 * Tabs can always be moved programmatically.
189 */
190 get tabsMovable() {
191 return this.tabBar.tabsMovable;
192 }
193 /**
194 * Set the whether the tabs are movable by the user.
195 *
196 * #### Notes
197 * Tabs can always be moved programmatically.
198 */
199 set tabsMovable(value) {
200 this.tabBar.tabsMovable = value;
201 }
202 /**
203 * A read-only array of the widgets in the panel.
204 */
205 get widgets() {
206 return this.tabContents.widgets;
207 }
208 /**
209 * Add a widget to the end of the tab panel.
210 *
211 * @param widget - The widget to add to the tab panel.
212 *
213 * #### Notes
214 * If the widget is already contained in the panel, it will be moved.
215 *
216 * The widget's `title` is used to populate the tab.
217 */
218 addWidget(widget) {
219 this.insertWidget(this.widgets.length, widget);
220 }
221 /**
222 * Insert a widget into the tab panel at a specified index.
223 *
224 * @param index - The index at which to insert the widget.
225 *
226 * @param widget - The widget to insert into to the tab panel.
227 *
228 * #### Notes
229 * If the widget is already contained in the panel, it will be moved.
230 *
231 * The widget's `title` is used to populate the tab.
232 */
233 insertWidget(index, widget) {
234 if (widget !== this.currentWidget) {
235 widget.hide();
236 }
237 this.tabContents.insertWidget(index, widget);
238 this.tabBar.insertTab(index, widget.title);
239 }
240 /**
241 * Handle the `currentChanged` signal from the tab bar.
242 */
243 _onCurrentChanged(sender, args) {
244 // Extract the previous and current title from the args.
245 const { previousIndex, previousTitle, currentIndex, currentTitle } = args;
246 // Extract the widgets from the titles.
247 const previousWidget = previousTitle ? previousTitle.owner : null;
248 const currentWidget = currentTitle ? currentTitle.owner : null;
249 // Hide the previous widget.
250 if (previousWidget) {
251 previousWidget.hide();
252 }
253 // Show the current widget.
254 if (currentWidget) {
255 currentWidget.show();
256 }
257 // Emit the `currentChanged` signal for the tab panel.
258 this._currentChanged.emit({
259 previousIndex,
260 previousWidget,
261 currentIndex,
262 currentWidget,
263 });
264 // Flush the message loop on IE and Edge to prevent flicker.
265 if (Platform.IS_EDGE || Platform.IS_IE) {
266 MessageLoop.flush();
267 }
268 }
269 /**
270 * Handle the `tabActivateRequested` signal from the tab bar.
271 */
272 _onTabActivateRequested(sender, args) {
273 args.title.owner.activate();
274 }
275 /**
276 * Handle the `tabCloseRequested` signal from the tab bar.
277 */
278 _onTabCloseRequested(sender, args) {
279 args.title.owner.close();
280 }
281 /**
282 * Handle the `tabMoved` signal from the tab bar.
283 */
284 _onTabMoved(sender, args) {
285 this.tabContents.insertWidget(args.toIndex, args.title.owner);
286 }
287 /**
288 * Handle the `widgetRemoved` signal from the stacked panel.
289 */
290 _onWidgetRemoved(sender, widget) {
291 this.tabBar.removeTab(widget.title);
292 }
293 }
294 //# sourceMappingURL=tabpanel.js.map