UNPKG

2.68 kBPlain TextView Raw
1// Copyright (c) Jupyter Development Team.
2// Distributed under the terms of the Modified BSD License.
3/*-----------------------------------------------------------------------------
4| Copyright (c) 2014-2017, PhosphorJS Contributors
5|
6| Distributed under the terms of the BSD 3-Clause License.
7|
8| The full license is in the file LICENSE, distributed with this software.
9|----------------------------------------------------------------------------*/
10import { PanelLayout } from './panellayout';
11
12import { Widget } from './widget';
13
14/**
15 * A simple and convenient panel widget class.
16 *
17 * #### Notes
18 * This class is suitable as a base class for implementing a variety of
19 * convenience panel widgets, but can also be used directly with CSS to
20 * arrange a collection of widgets.
21 *
22 * This class provides a convenience wrapper around a [[PanelLayout]].
23 */
24export class Panel extends Widget {
25 /**
26 * Construct a new panel.
27 *
28 * @param options - The options for initializing the panel.
29 */
30 constructor(options: Panel.IOptions = {}) {
31 super();
32 this.addClass('lm-Panel');
33 /* <DEPRECATED> */
34 this.addClass('p-Panel');
35 /* </DEPRECATED> */
36 this.layout = Private.createLayout(options);
37 }
38
39 /**
40 * A read-only array of the widgets in the panel.
41 */
42 get widgets(): ReadonlyArray<Widget> {
43 return (this.layout as PanelLayout).widgets;
44 }
45
46 /**
47 * Add a widget to the end of the panel.
48 *
49 * @param widget - The widget to add to the panel.
50 *
51 * #### Notes
52 * If the widget is already contained in the panel, it will be moved.
53 */
54 addWidget(widget: Widget): void {
55 (this.layout as PanelLayout).addWidget(widget);
56 }
57
58 /**
59 * Insert a widget at the specified index.
60 *
61 * @param index - The index at which to insert the widget.
62 *
63 * @param widget - The widget to insert into to the panel.
64 *
65 * #### Notes
66 * If the widget is already contained in the panel, it will be moved.
67 */
68 insertWidget(index: number, widget: Widget): void {
69 (this.layout as PanelLayout).insertWidget(index, widget);
70 }
71}
72
73/**
74 * The namespace for the `Panel` class statics.
75 */
76export namespace Panel {
77 /**
78 * An options object for creating a panel.
79 */
80 export interface IOptions {
81 /**
82 * The panel layout to use for the panel.
83 *
84 * The default is a new `PanelLayout`.
85 */
86 layout?: PanelLayout;
87 }
88}
89
90/**
91 * The namespace for the module implementation details.
92 */
93namespace Private {
94 /**
95 * Create a panel layout for the given panel options.
96 */
97 export function createLayout(options: Panel.IOptions): PanelLayout {
98 return options.layout || new PanelLayout();
99 }
100}