-
-
Notifications
You must be signed in to change notification settings - Fork 126
/
splitpanel.ts
487 lines (429 loc) · 12.4 KB
/
splitpanel.ts
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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
// Copyright (c) Jupyter Development Team.
// Distributed under the terms of the Modified BSD License.
/*-----------------------------------------------------------------------------
| Copyright (c) 2014-2017, PhosphorJS Contributors
|
| Distributed under the terms of the BSD 3-Clause License.
|
| The full license is in the file LICENSE, distributed with this software.
|----------------------------------------------------------------------------*/
import { ArrayExt } from '@lumino/algorithm';
import { IDisposable } from '@lumino/disposable';
import { Drag } from '@lumino/dragdrop';
import { Message } from '@lumino/messaging';
import { Panel } from './panel';
import { SplitLayout } from './splitlayout';
import { Widget } from './widget';
/**
* A panel which arranges its widgets into resizable sections.
*
* #### Notes
* This class provides a convenience wrapper around a [[SplitLayout]].
*/
export class SplitPanel extends Panel {
/**
* Construct a new split panel.
*
* @param options - The options for initializing the split panel.
*/
constructor(options: SplitPanel.IOptions = {}) {
super({ layout: Private.createLayout(options) });
this.addClass('lm-SplitPanel');
/* <DEPRECATED> */
this.addClass('p-SplitPanel');
/* </DEPRECATED> */
}
/**
* Dispose of the resources held by the panel.
*/
dispose(): void {
this._releaseMouse();
super.dispose();
}
/**
* Get the layout orientation for the split panel.
*/
get orientation(): SplitPanel.Orientation {
return (this.layout as SplitLayout).orientation;
}
/**
* Set the layout orientation for the split panel.
*/
set orientation(value: SplitPanel.Orientation) {
(this.layout as SplitLayout).orientation = value;
}
/**
* Get the content alignment for the split panel.
*
* #### Notes
* This is the alignment of the widgets in the layout direction.
*
* The alignment has no effect if the widgets can expand to fill the
* entire split panel.
*/
get alignment(): SplitPanel.Alignment {
return (this.layout as SplitLayout).alignment;
}
/**
* Set the content alignment for the split panel.
*
* #### Notes
* This is the alignment of the widgets in the layout direction.
*
* The alignment has no effect if the widgets can expand to fill the
* entire split panel.
*/
set alignment(value: SplitPanel.Alignment) {
(this.layout as SplitLayout).alignment = value;
}
/**
* Get the inter-element spacing for the split panel.
*/
get spacing(): number {
return (this.layout as SplitLayout).spacing;
}
/**
* Set the inter-element spacing for the split panel.
*/
set spacing(value: number) {
(this.layout as SplitLayout).spacing = value;
}
/**
* The renderer used by the split panel.
*/
get renderer(): SplitPanel.IRenderer {
return (this.layout as SplitLayout).renderer;
}
/**
* A read-only array of the split handles in the panel.
*/
get handles(): ReadonlyArray<HTMLDivElement> {
return (this.layout as SplitLayout).handles;
}
/**
* Get the relative sizes of the widgets in the panel.
*
* @returns A new array of the relative sizes of the widgets.
*
* #### Notes
* The returned sizes reflect the sizes of the widgets normalized
* relative to their siblings.
*
* This method **does not** measure the DOM nodes.
*/
relativeSizes(): number[] {
return (this.layout as SplitLayout).relativeSizes();
}
/**
* Set the relative sizes for the widgets in the panel.
*
* @param sizes - The relative sizes for the widgets in the panel.
*
* #### Notes
* Extra values are ignored, too few will yield an undefined layout.
*
* The actual geometry of the DOM nodes is updated asynchronously.
*/
setRelativeSizes(sizes: number[]): void {
(this.layout as SplitLayout).setRelativeSizes(sizes);
}
/**
* Handle the DOM events for the split panel.
*
* @param event - The DOM event sent to the panel.
*
* #### Notes
* This method implements the DOM `EventListener` interface and is
* called in response to events on the panel's DOM node. It should
* not be called directly by user code.
*/
handleEvent(event: Event): void {
switch (event.type) {
case 'mousedown':
this._evtMouseDown(event as MouseEvent);
break;
case 'mousemove':
this._evtMouseMove(event as MouseEvent);
break;
case 'mouseup':
this._evtMouseUp(event as MouseEvent);
break;
case 'keydown':
this._evtKeyDown(event as KeyboardEvent);
break;
case 'contextmenu':
event.preventDefault();
event.stopPropagation();
break;
}
}
/**
* A message handler invoked on a `'before-attach'` message.
*/
protected onBeforeAttach(msg: Message): void {
this.node.addEventListener('mousedown', this);
}
/**
* A message handler invoked on an `'after-detach'` message.
*/
protected onAfterDetach(msg: Message): void {
this.node.removeEventListener('mousedown', this);
this._releaseMouse();
}
/**
* A message handler invoked on a `'child-added'` message.
*/
protected onChildAdded(msg: Widget.ChildMessage): void {
msg.child.addClass('lm-SplitPanel-child');
/* <DEPRECATED> */
msg.child.addClass('p-SplitPanel-child');
/* </DEPRECATED> */
this._releaseMouse();
}
/**
* A message handler invoked on a `'child-removed'` message.
*/
protected onChildRemoved(msg: Widget.ChildMessage): void {
msg.child.removeClass('lm-SplitPanel-child');
/* <DEPRECATED> */
msg.child.removeClass('p-SplitPanel-child');
/* </DEPRECATED> */
this._releaseMouse();
}
/**
* Handle the `'keydown'` event for the split panel.
*/
private _evtKeyDown(event: KeyboardEvent): void {
// Stop input events during drag.
if (this._pressData) {
event.preventDefault();
event.stopPropagation();
}
// Release the mouse if `Escape` is pressed.
if (event.keyCode === 27) {
this._releaseMouse();
}
}
/**
* Handle the `'mousedown'` event for the split panel.
*/
private _evtMouseDown(event: MouseEvent): void {
// Do nothing if the left mouse button is not pressed.
if (event.button !== 0) {
return;
}
// Find the handle which contains the mouse target, if any.
let layout = this.layout as SplitLayout;
let index = ArrayExt.findFirstIndex(layout.handles, handle => {
return handle.contains(event.target as HTMLElement);
});
// Bail early if the mouse press was not on a handle.
if (index === -1) {
return;
}
// Stop the event when a split handle is pressed.
event.preventDefault();
event.stopPropagation();
// Add the extra document listeners.
document.addEventListener('mouseup', this, true);
document.addEventListener('mousemove', this, true);
document.addEventListener('keydown', this, true);
document.addEventListener('contextmenu', this, true);
// Compute the offset delta for the handle press.
let delta: number;
let handle = layout.handles[index];
let rect = handle.getBoundingClientRect();
if (layout.orientation === 'horizontal') {
delta = event.clientX - rect.left;
} else {
delta = event.clientY - rect.top;
}
// Override the cursor and store the press data.
let style = window.getComputedStyle(handle);
let override = Drag.overrideCursor(style.cursor!);
this._pressData = { index, delta, override };
}
/**
* Handle the `'mousemove'` event for the split panel.
*/
private _evtMouseMove(event: MouseEvent): void {
// Stop the event when dragging a split handle.
event.preventDefault();
event.stopPropagation();
// Compute the desired offset position for the handle.
let pos: number;
let layout = this.layout as SplitLayout;
let rect = this.node.getBoundingClientRect();
if (layout.orientation === 'horizontal') {
pos = event.clientX - rect.left - this._pressData!.delta;
} else {
pos = event.clientY - rect.top - this._pressData!.delta;
}
// Move the handle as close to the desired position as possible.
layout.moveHandle(this._pressData!.index, pos);
}
/**
* Handle the `'mouseup'` event for the split panel.
*/
private _evtMouseUp(event: MouseEvent): void {
// Do nothing if the left mouse button is not released.
if (event.button !== 0) {
return;
}
// Stop the event when releasing a handle.
event.preventDefault();
event.stopPropagation();
// Finalize the mouse release.
this._releaseMouse();
}
/**
* Release the mouse grab for the split panel.
*/
private _releaseMouse(): void {
// Bail early if no drag is in progress.
if (!this._pressData) {
return;
}
// Clear the override cursor.
this._pressData.override.dispose();
this._pressData = null;
// Remove the extra document listeners.
document.removeEventListener('mouseup', this, true);
document.removeEventListener('mousemove', this, true);
document.removeEventListener('keydown', this, true);
document.removeEventListener('contextmenu', this, true);
}
private _pressData: Private.IPressData | null = null;
}
/**
* The namespace for the `SplitPanel` class statics.
*/
export namespace SplitPanel {
/**
* A type alias for a split panel orientation.
*/
export type Orientation = SplitLayout.Orientation;
/**
* A type alias for a split panel alignment.
*/
export type Alignment = SplitLayout.Alignment;
/**
* A type alias for a split panel renderer.
*/
export type IRenderer = SplitLayout.IRenderer;
/**
* An options object for initializing a split panel.
*/
export interface IOptions {
/**
* The renderer to use for the split panel.
*
* The default is a shared renderer instance.
*/
renderer?: IRenderer;
/**
* The layout orientation of the panel.
*
* The default is `'horizontal'`.
*/
orientation?: Orientation;
/**
* The content alignment of the panel.
*
* The default is `'start'`.
*/
alignment?: Alignment;
/**
* The spacing between items in the panel.
*
* The default is `4`.
*/
spacing?: number;
/**
* The split layout to use for the split panel.
*
* If this is provided, the other options are ignored.
*
* The default is a new `SplitLayout`.
*/
layout?: SplitLayout;
}
/**
* The default implementation of `IRenderer`.
*/
export class Renderer implements IRenderer {
/**
* Create a new handle for use with a split panel.
*
* @returns A new handle element for a split panel.
*/
createHandle(): HTMLDivElement {
let handle = document.createElement('div');
handle.className = 'lm-SplitPanel-handle';
/* <DEPRECATED> */
handle.classList.add('p-SplitPanel-handle');
/* </DEPRECATED> */
return handle;
}
}
/**
* The default `Renderer` instance.
*/
export const defaultRenderer = new Renderer();
/**
* Get the split panel stretch factor for the given widget.
*
* @param widget - The widget of interest.
*
* @returns The split panel stretch factor for the widget.
*/
export function getStretch(widget: Widget): number {
return SplitLayout.getStretch(widget);
}
/**
* Set the split panel stretch factor for the given widget.
*
* @param widget - The widget of interest.
*
* @param value - The value for the stretch factor.
*/
export function setStretch(widget: Widget, value: number): void {
SplitLayout.setStretch(widget, value);
}
}
/**
* The namespace for the module implementation details.
*/
namespace Private {
/**
* An object which holds mouse press data.
*/
export interface IPressData {
/**
* The index of the pressed handle.
*/
index: number;
/**
* The offset of the press in handle coordinates.
*/
delta: number;
/**
* The disposable which will clear the override cursor.
*/
override: IDisposable;
}
/**
* Create a split layout for the given panel options.
*/
export function createLayout(options: SplitPanel.IOptions): SplitLayout {
return (
options.layout ||
new SplitLayout({
renderer: options.renderer || SplitPanel.defaultRenderer,
orientation: options.orientation,
alignment: options.alignment,
spacing: options.spacing
})
);
}
}