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 | |----------------------------------------------------------------------------*/
|
10 | import { BoxLayout } from './boxlayout';
|
11 |
|
12 | import { Panel } from './panel';
|
13 |
|
14 | import { Widget } from './widget';
|
15 |
|
16 | /**
|
17 | * A panel which arranges its widgets in a single row or column.
|
18 | *
|
19 | * #### Notes
|
20 | * This class provides a convenience wrapper around a [[BoxLayout]].
|
21 | */
|
22 | export class BoxPanel extends Panel {
|
23 | /**
|
24 | * Construct a new box panel.
|
25 | *
|
26 | * @param options - The options for initializing the box panel.
|
27 | */
|
28 | constructor(options: BoxPanel.IOptions = {}) {
|
29 | super({ layout: Private.createLayout(options) });
|
30 | this.addClass('lm-BoxPanel');
|
31 | /* <DEPRECATED> */
|
32 | this.addClass('p-BoxPanel');
|
33 | /* </DEPRECATED> */
|
34 | }
|
35 |
|
36 | /**
|
37 | * Get the layout direction for the box panel.
|
38 | */
|
39 | get direction(): BoxPanel.Direction {
|
40 | return (this.layout as BoxLayout).direction;
|
41 | }
|
42 |
|
43 | /**
|
44 | * Set the layout direction for the box panel.
|
45 | */
|
46 | set direction(value: BoxPanel.Direction) {
|
47 | (this.layout as BoxLayout).direction = value;
|
48 | }
|
49 |
|
50 | /**
|
51 | * Get the content alignment for the box panel.
|
52 | *
|
53 | * #### Notes
|
54 | * This is the alignment of the widgets in the layout direction.
|
55 | *
|
56 | * The alignment has no effect if the widgets can expand to fill the
|
57 | * entire box layout.
|
58 | */
|
59 | get alignment(): BoxPanel.Alignment {
|
60 | return (this.layout as BoxLayout).alignment;
|
61 | }
|
62 |
|
63 | /**
|
64 | * Set the content alignment for the box panel.
|
65 | *
|
66 | * #### Notes
|
67 | * This is the alignment of the widgets in the layout direction.
|
68 | *
|
69 | * The alignment has no effect if the widgets can expand to fill the
|
70 | * entire box layout.
|
71 | */
|
72 | set alignment(value: BoxPanel.Alignment) {
|
73 | (this.layout as BoxLayout).alignment = value;
|
74 | }
|
75 |
|
76 | /**
|
77 | * Get the inter-element spacing for the box panel.
|
78 | */
|
79 | get spacing(): number {
|
80 | return (this.layout as BoxLayout).spacing;
|
81 | }
|
82 |
|
83 | /**
|
84 | * Set the inter-element spacing for the box panel.
|
85 | */
|
86 | set spacing(value: number) {
|
87 | (this.layout as BoxLayout).spacing = value;
|
88 | }
|
89 |
|
90 | /**
|
91 | * A message handler invoked on a `'child-added'` message.
|
92 | */
|
93 | protected onChildAdded(msg: Widget.ChildMessage): void {
|
94 | msg.child.addClass('lm-BoxPanel-child');
|
95 | /* <DEPRECATED> */
|
96 | msg.child.addClass('p-BoxPanel-child');
|
97 | /* </DEPRECATED> */
|
98 | }
|
99 |
|
100 | /**
|
101 | * A message handler invoked on a `'child-removed'` message.
|
102 | */
|
103 | protected onChildRemoved(msg: Widget.ChildMessage): void {
|
104 | msg.child.removeClass('lm-BoxPanel-child');
|
105 | /* <DEPRECATED> */
|
106 | msg.child.removeClass('p-BoxPanel-child');
|
107 | /* </DEPRECATED> */
|
108 | }
|
109 | }
|
110 |
|
111 | /**
|
112 | * The namespace for the `BoxPanel` class statics.
|
113 | */
|
114 | export namespace BoxPanel {
|
115 | /**
|
116 | * A type alias for a box panel direction.
|
117 | */
|
118 | export type Direction = BoxLayout.Direction;
|
119 |
|
120 | /**
|
121 | * A type alias for a box panel alignment.
|
122 | */
|
123 | export type Alignment = BoxLayout.Alignment;
|
124 |
|
125 | /**
|
126 | * An options object for initializing a box panel.
|
127 | */
|
128 | export interface IOptions {
|
129 | /**
|
130 | * The layout direction of the panel.
|
131 | *
|
132 | * The default is `'top-to-bottom'`.
|
133 | */
|
134 | direction?: Direction;
|
135 |
|
136 | /**
|
137 | * The content alignment of the panel.
|
138 | *
|
139 | * The default is `'start'`.
|
140 | */
|
141 | alignment?: Alignment;
|
142 |
|
143 | /**
|
144 | * The spacing between items in the panel.
|
145 | *
|
146 | * The default is `4`.
|
147 | */
|
148 | spacing?: number;
|
149 |
|
150 | /**
|
151 | * The box layout to use for the box panel.
|
152 | *
|
153 | * If this is provided, the other options are ignored.
|
154 | *
|
155 | * The default is a new `BoxLayout`.
|
156 | */
|
157 | layout?: BoxLayout;
|
158 | }
|
159 |
|
160 | /**
|
161 | * Get the box panel stretch factor for the given widget.
|
162 | *
|
163 | * @param widget - The widget of interest.
|
164 | *
|
165 | * @returns The box panel stretch factor for the widget.
|
166 | */
|
167 | export function getStretch(widget: Widget): number {
|
168 | return BoxLayout.getStretch(widget);
|
169 | }
|
170 |
|
171 | /**
|
172 | * Set the box panel stretch factor for the given widget.
|
173 | *
|
174 | * @param widget - The widget of interest.
|
175 | *
|
176 | * @param value - The value for the stretch factor.
|
177 | */
|
178 | export function setStretch(widget: Widget, value: number): void {
|
179 | BoxLayout.setStretch(widget, value);
|
180 | }
|
181 |
|
182 | /**
|
183 | * Get the box panel size basis for the given widget.
|
184 | *
|
185 | * @param widget - The widget of interest.
|
186 | *
|
187 | * @returns The box panel size basis for the widget.
|
188 | */
|
189 | export function getSizeBasis(widget: Widget): number {
|
190 | return BoxLayout.getSizeBasis(widget);
|
191 | }
|
192 |
|
193 | /**
|
194 | * Set the box panel size basis for the given widget.
|
195 | *
|
196 | * @param widget - The widget of interest.
|
197 | *
|
198 | * @param value - The value for the size basis.
|
199 | */
|
200 | export function setSizeBasis(widget: Widget, value: number): void {
|
201 | BoxLayout.setSizeBasis(widget, value);
|
202 | }
|
203 | }
|
204 |
|
205 | /**
|
206 | * The namespace for the module implementation details.
|
207 | */
|
208 | namespace Private {
|
209 | /**
|
210 | * Create a box layout for the given panel options.
|
211 | */
|
212 | export function createLayout(options: BoxPanel.IOptions): BoxLayout {
|
213 | return options.layout || new BoxLayout(options);
|
214 | }
|
215 | }
|