forked from jupyterlab/jupyterlab
-
Notifications
You must be signed in to change notification settings - Fork 0
/
widget.ts
899 lines (793 loc) · 21.5 KB
/
widget.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
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
// Copyright (c) Jupyter Development Team.
// Distributed under the terms of the Modified BSD License.
import {
Kernel, KernelMessage, nbformat
} from '@jupyterlab/services';
import {
IDisposable
} from '@phosphor/disposable';
import {
ISignal
} from '@phosphor/signaling';
import {
Message
} from '@phosphor/messaging';
import {
MimeData
} from '@phosphor/coreutils';
import {
Drag
} from '@phosphor/dragdrop';
import {
Panel, PanelLayout
} from '@phosphor/widgets';
import {
Widget
} from '@phosphor/widgets';
import {
ObservableVector
} from '../common/observablevector';
import {
IOutputModel, RenderMime
} from '../rendermime';
/**
* The threshold in pixels to start a drag event.
*/
const DRAG_THRESHOLD = 5;
/**
* The factory MIME type supported by phosphor dock panels.
*/
const FACTORY_MIME = 'application/vnd.phosphor.widget-factory';
/**
* The class name added to an output area widget.
*/
const OUTPUT_AREA_CLASS = 'jp-OutputAreaWidget';
/**
* The class name added to a "mirrored" output area widget created by a drag.
*/
const MIRRORED_OUTPUT_AREA_CLASS = 'jp-MirroredOutputArea';
/**
* The class name added to an child widget.
*/
const CHILD_CLASS = 'jp-OutputAreaWidget-child';
/**
* The class name added to output area gutters.
*/
const GUTTER_CLASS = 'jp-OutputAreaWidget-gutter';
/**
* The class name added to output area outputs.
*/
const OUTPUT_CLASS = 'jp-OutputAreaWidget-output';
/**
* The class name added to an execution result.
*/
const EXECTUTE_CLASS = 'jp-OutputAreaWidget-executeResult';
/**
* The class name added to stdin data.
*/
const STDIN_CLASS = 'jp-OutputAreaWidget-stdin';
/**
* The class name added to stdin data prompt nodes.
*/
const STDIN_PROMPT_CLASS = 'jp-StdinWidget-prompt';
/**
* The class name added to stdin data input nodes.
*/
const STDIN_INPUT_CLASS = 'jp-StdinWidget-input';
/**
* The class name added to stdin rendered text nodes.
*/
const STDIN_RENDERED_CLASS = 'jp-StdinWidget-rendered';
/**
* The class name added to fixed height output areas.
*/
const FIXED_HEIGHT_CLASS = 'jp-mod-fixedHeight';
/**
* The class name added to collaped output areas.
*/
const COLLAPSED_CLASS = 'jp-mod-collapsed';
/**
* An output area widget.
*
* #### Notes
* The widget model must be set separately and can be changed
* at any time. Consumers of the widget must account for a
* `null` model, and may want to listen to the `modelChanged`
* signal.
*/
export
class OutputAreaWidget extends Widget {
/**
* Construct an output area widget.
*/
constructor(options: OutputAreaWidget.IOptions) {
super();
let model = this.model = options.model;
this.addClass(OUTPUT_AREA_CLASS);
this.rendermime = options.rendermime;
this.contentFactory = (
options.contentFactory || OutputAreaWidget.defaultContentFactory
);
this.layout = new PanelLayout();
for (let i = 0; i < model.length; i++) {
let output = model.get(i);
this._insertOutput(i, output);
}
model.changed.connect(this._onModelChanged, this);
}
/**
* Create a mirrored output area widget.
*/
mirror(): OutputAreaWidget {
let rendermime = this.rendermime;
let contentFactory = this.contentFactory;
let model = this.model;
let widget = new OutputAreaWidget({ model, rendermime, contentFactory });
widget.title.label = 'Mirrored Output';
widget.title.closable = true;
widget.addClass(MIRRORED_OUTPUT_AREA_CLASS);
return widget;
}
/**
* The model used by the widget.
*/
readonly model: IOutputAreaModel;
/**
* Te rendermime instance used by the widget.
*/
readonly rendermime: RenderMime;
/**
* The content factory used by the widget.
*/
readonly contentFactory: OutputAreaWidget.IContentFactory;
/**
* A read-only sequence of the widgets in the output area.
*/
get widgets(): ReadonlyArray<Widget> {
return (this.layout as PanelLayout).widgets;
}
/**
* The collapsed state of the widget.
*/
get collapsed(): boolean {
return this._collapsed;
}
set collapsed(value: boolean) {
if (this._collapsed === value) {
return;
}
this._collapsed = value;
this.update();
}
/**
* The fixed height state of the widget.
*/
get fixedHeight(): boolean {
return this._fixedHeight;
}
set fixedHeight(value: boolean) {
if (this._fixedHeight === value) {
return;
}
this._fixedHeight = value;
this.update();
}
/**
* Execute code on a kernel and handle response messages.
*/
execute(code: string, kernel: Kernel.IKernel): Promise<KernelMessage.IExecuteReplyMsg> {
// Bail if the model is disposed.
if (this.model.isDisposed) {
return Promise.reject('Model is disposed');
}
// Override the default for `stop_on_error`.
let content: KernelMessage.IExecuteRequest = {
code,
stop_on_error: true
};
this.model.clear();
// Make sure there were no input widgets.
if (this.widgets.length) {
this._clear();
}
return new Promise<KernelMessage.IExecuteReplyMsg>((resolve, reject) => {
// Bail if the model is disposed.
if (this.model.isDisposed) {
return Promise.reject('Model is disposed');
}
let future = kernel.requestExecute(content);
// Handle published messages.
future.onIOPub = (msg: KernelMessage.IIOPubMessage) => {
this._onIOPub(msg);
};
// Handle the execute reply.
future.onReply = (msg: KernelMessage.IExecuteReplyMsg) => {
this._onExecuteReply(msg);
resolve(msg);
};
// Handle stdin.
future.onStdin = (msg: KernelMessage.IStdinMessage) => {
if (KernelMessage.isInputRequestMsg(msg)) {
this._onInputRequest(msg, kernel);
}
};
});
}
/**
* Handle `update-request` messages.
*/
protected onUpdateRequest(msg: Message): void {
this.toggleClass(COLLAPSED_CLASS, this.collapsed);
this.toggleClass(FIXED_HEIGHT_CLASS, this.fixedHeight);
}
/**
* Clear the widget inputs and outputs.
*/
private _clear(): void {
// Bail if there is no work to do.
if (!this.widgets.length) {
return;
}
// Remove all of our widgets.
let length = this.widgets.length;
for (let i = 0; i < length; i++) {
let widget = this.widgets[0];
widget.parent = null;
widget.dispose();
}
// When an output area is cleared and then quickly replaced with new
// content (as happens with @interact in widgets, for example), the
// quickly changing height can make the page jitter.
// We introduce a small delay in the minimum height
// to prevent this jitter.
let rect = this.node.getBoundingClientRect();
this.node.style.minHeight = `${rect.height}px`;
if (this._minHeightTimeout) {
clearTimeout(this._minHeightTimeout);
}
this._minHeightTimeout = window.setTimeout(() => {
if (this.isDisposed) {
return;
}
this.node.style.minHeight = '';
}, 50);
}
/**
* Handle an iopub message.
*/
private _onIOPub(msg: KernelMessage.IIOPubMessage): void {
let model = this.model;
let msgType = msg.header.msg_type;
switch (msgType) {
case 'execute_result':
case 'display_data':
case 'stream':
case 'error':
let output = msg.content as nbformat.IOutput;
output.output_type = msgType as nbformat.OutputType;
model.add(output);
break;
case 'clear_output':
let wait = (msg as KernelMessage.IClearOutputMsg).content.wait;
model.clear(wait);
break;
default:
break;
}
}
/**
* Handle an execute reply message.
*/
private _onExecuteReply(msg: KernelMessage.IExecuteReplyMsg): void {
// API responses that contain a pager are special cased and their type
// is overriden from 'execute_reply' to 'display_data' in order to
// render output.
let model = this.model;
let content = msg.content as KernelMessage.IExecuteOkReply;
let payload = content && content.payload;
if (!payload || !payload.length) {
return;
}
let pages = payload.filter(i => (i as any).source === 'page');
if (!pages.length) {
return;
}
let page = JSON.parse(JSON.stringify(pages[0]));
let output: nbformat.IOutput = {
output_type: 'display_data',
data: (page as any).data as nbformat.IMimeBundle,
metadata: {}
};
model.add(output);
}
/**
* Handle an input request from a kernel.
*/
private _onInputRequest(msg: KernelMessage.IInputRequestMsg, kernel: Kernel.IKernel): void {
// Add an output widget to the end.
let factory = this.contentFactory;
let prompt = msg.content.prompt;
let password = msg.content.password;
let panel = new Panel();
panel.addClass(CHILD_CLASS);
panel.addClass(STDIN_CLASS);
let gutter = factory.createGutter();
gutter.addClass(GUTTER_CLASS);
panel.addWidget(gutter);
let input = factory.createStdin({ prompt, password, kernel });
input.addClass(STDIN_CLASS);
panel.addWidget(input);
let layout = this.layout as PanelLayout;
layout.addWidget(panel);
}
/**
* Insert an output to the layout.
*/
private _insertOutput(index: number, model: IOutputModel): void {
let panel = new Panel();
panel.addClass(CHILD_CLASS);
panel.addClass(OUTPUT_CLASS);
let gutter = this.contentFactory.createGutter();
gutter.executionCount = model.executionCount;
gutter.addClass(GUTTER_CLASS);
panel.addWidget(gutter);
let output = this._createOutput(model);
output.toggleClass(EXECTUTE_CLASS, model.executionCount !== null);
panel.addWidget(output);
let layout = this.layout as PanelLayout;
layout.insertWidget(index, panel);
}
/**
* Update an output in place.
*/
private _setOutput(index: number, model: IOutputModel): void {
let layout = this.layout as PanelLayout;
let widgets = this.widgets;
// Skip any stdin widgets to find the correct index.
for (let i = 0; i < index; i++) {
if (widgets[i].hasClass(STDIN_CLASS)) {
index++;
}
}
layout.widgets[index].dispose();
this._insertOutput(index, model);
}
/**
* Create an output.
*/
private _createOutput(model: IOutputModel): Widget {
let widget = this.rendermime.render(model);
widget.addClass(CHILD_CLASS);
widget.addClass(OUTPUT_CLASS);
return widget;
}
/**
* Follow changes on the model state.
*/
private _onModelChanged(sender: IOutputAreaModel, args: ObservableVector.IChangedArgs<IOutputModel>) {
switch (args.type) {
case 'add':
// Children are always added at the end.
this._insertOutput(this.widgets.length, args.newValues[0]);
break;
case 'remove':
// Only clear is supported by the model.
if (this.widgets.length) {
this._clear();
}
break;
case 'set':
this._setOutput(args.newIndex, args.newValues[0]);
break;
default:
break;
}
}
private _fixedHeight = false;
private _collapsed = false;
private _minHeightTimeout: number = null;
}
/**
* A namespace for OutputAreaWidget statics.
*/
export
namespace OutputAreaWidget {
/**
* The options to pass to an `OutputAreaWidget`.
*/
export
interface IOptions {
/**
* The rendermime instance used by the widget.
*/
rendermime: RenderMime;
/**
* The model used by the widget.
*/
model: IOutputAreaModel;
/**
* The output widget content factory.
*
* Defaults to a shared `IContentFactory` instance.
*/
contentFactory?: IContentFactory;
}
/**
* The interface for a gutter widget.
*/
export
interface IGutterWidget extends Widget {
/**
* The execution count for the widget.
*/
executionCount: nbformat.ExecutionCount;
}
/**
* The options to create a stdin widget.
*/
export
interface IStdinOptions {
/**
* The prompt text.
*/
prompt: string;
/**
* Whether the input is a password.
*/
password: boolean;
/**
* The kernel associated with the request.
*/
kernel: Kernel.IKernel;
}
/**
* An output widget content factory.
*/
export
interface IContentFactory {
/**
* Create a gutter for an output or input.
*
*/
createGutter(): IGutterWidget;
/**
* Create an stdin widget.
*/
createStdin(options: IStdinOptions): Widget;
}
/**
* The default implementation of `IContentFactory`.
*/
export
class ContentFactory implements IContentFactory {
/**
* Create the gutter for the widget.
*/
createGutter(): IGutterWidget {
return new GutterWidget();
}
/**
* Create an stdin widget.
*/
createStdin(options: IStdinOptions): Widget {
return new StdinWidget(options);
}
}
/**
* The default `ContentFactory` instance.
*/
export
const defaultContentFactory = new ContentFactory();
/**
* The default stdin widget.
*/
export
class StdinWidget extends Widget {
/**
* Construct a new input widget.
*/
constructor(options: IStdinOptions) {
super({ node: Private.createInputWidgetNode() });
let text = this.node.firstChild as HTMLElement;
text.textContent = options.prompt;
this._input = this.node.lastChild as HTMLInputElement;
if (options.password) {
this._input.type = 'password';
}
this._kernel = options.kernel;
}
/**
* Handle the DOM events for the widget.
*
* @param event - The DOM event sent to the widget.
*
* #### Notes
* This method implements the DOM `EventListener` interface and is
* called in response to events on the dock panel's node. It should
* not be called directly by user code.
*/
handleEvent(event: Event): void {
let input = this._input;
if (event.type === 'keydown') {
if ((event as KeyboardEvent).keyCode === 13) { // Enter
this._kernel.sendInputReply({
value: input.value
});
let rendered = document.createElement('span');
rendered.className = STDIN_RENDERED_CLASS;
if (input.type === 'password') {
rendered.textContent = Array(input.value.length + 1).join('·');
} else {
rendered.textContent = input.value;
}
this.node.replaceChild(rendered, input);
}
}
}
/**
* Handle `after-attach` messages sent to the widget.
*/
protected onAfterAttach(msg: Message): void {
this._input.addEventListener('keydown', this);
this.update();
}
/**
* Handle `update-request` messages sent to the widget.
*/
protected onUpdateRequest(msg: Message): void {
this._input.focus();
}
/**
* Handle `before-detach` messages sent to the widget.
*/
protected onBeforeDetach(msg: Message): void {
this._input.removeEventListener('keydown', this);
}
private _kernel: Kernel.IKernel = null;
private _input: HTMLInputElement = null;
}
/**
* The default output gutter.
*/
export
class GutterWidget extends Widget {
/**
* The execution count for the widget.
*/
get executionCount(): nbformat.ExecutionCount {
return this._executionCount;
}
set executionCount(value: nbformat.ExecutionCount) {
this._executionCount = value;
if (value === null) {
this.node.textContent = '';
} else {
this.node.textContent = `Out[${value}]:`;
}
}
/**
* Handle the DOM events for the output gutter widget.
*
* @param event - The DOM event sent to the widget.
*
* #### 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 'mouseup':
this._evtMouseup(event as MouseEvent);
break;
case 'mousemove':
this._evtMousemove(event as MouseEvent);
break;
default:
break;
}
}
/**
* A message handler invoked on an `'after-attach'` message.
*/
protected onAfterAttach(msg: Message): void {
super.onAfterAttach(msg);
this.node.addEventListener('mousedown', this);
}
/**
* A message handler invoked on a `'before-detach'` message.
*/
protected onBeforeDetach(msg: Message): void {
super.onBeforeDetach(msg);
let node = this.node;
node.removeEventListener('mousedown', this);
}
/**
* Handle the `'mousedown'` event for the widget.
*/
private _evtMousedown(event: MouseEvent): void {
// Left mouse press for drag start.
if (event.button === 0) {
this._dragData = { pressX: event.clientX, pressY: event.clientY };
document.addEventListener('mouseup', this, true);
document.addEventListener('mousemove', this, true);
}
}
/**
* Handle the `'mouseup'` event for the widget.
*/
private _evtMouseup(event: MouseEvent): void {
if (event.button !== 0 || !this._drag) {
document.removeEventListener('mousemove', this, true);
document.removeEventListener('mouseup', this, true);
return;
}
event.preventDefault();
event.stopPropagation();
}
/**
* Handle the `'mousemove'` event for the widget.
*/
private _evtMousemove(event: MouseEvent): void {
event.preventDefault();
event.stopPropagation();
// Bail if we are the one dragging.
if (this._drag) {
return;
}
// Check for a drag initialization.
let data = this._dragData;
let dx = Math.abs(event.clientX - data.pressX);
let dy = Math.abs(event.clientY - data.pressY);
if (dx < DRAG_THRESHOLD && dy < DRAG_THRESHOLD) {
return;
}
this._startDrag(event.clientX, event.clientY);
}
/**
* Start a drag event.
*/
private _startDrag(clientX: number, clientY: number): void {
// Set up the drag event.
this._drag = new Drag({
mimeData: new MimeData(),
supportedActions: 'copy',
proposedAction: 'copy'
});
this._drag.mimeData.setData(FACTORY_MIME, () => {
let outputArea = this.parent.parent as OutputAreaWidget;
return outputArea.mirror();
});
// Remove mousemove and mouseup listeners and start the drag.
document.removeEventListener('mousemove', this, true);
document.removeEventListener('mouseup', this, true);
this._drag.start(clientX, clientY).then(action => {
this._drag = null;
});
}
/**
* Dispose of the resources held by the widget.
*/
dispose() {
this._dragData = null;
this._drag = null;
super.dispose();
}
private _drag: Drag = null;
private _dragData: { pressX: number, pressY: number } = null;
private _executionCount: nbformat.ExecutionCount = null;
}
}
/**
* The model for an output area.
*/
export
interface IOutputAreaModel extends IDisposable {
/**
* A signal emitted when the model state changes.
*/
readonly stateChanged: ISignal<IOutputAreaModel, void>;
/**
* A signal emitted when the model changes.
*/
readonly changed: ISignal<IOutputAreaModel, ObservableVector.IChangedArgs<IOutputModel>>;
/**
* The length of the items in the model.
*/
readonly length: number;
/**
* Whether the output area is trusted.
*/
trusted: boolean;
/**
* The output content factory used by the model.
*/
readonly contentFactory: IOutputAreaModel.IContentFactory;
/**
* Get an item at the specified index.
*/
get(index: number): IOutputModel;
/**
* Add an output, which may be combined with previous output.
*
* #### Notes
* The output bundle is copied.
* Contiguous stream outputs of the same `name` are combined.
*/
add(output: nbformat.IOutput): number;
/**
* Clear all of the output.
*
* @param wait - Delay clearing the output until the next message is added.
*/
clear(wait?: boolean): void;
/**
* Deserialize the model from JSON.
*
* #### Notes
* This will clear any existing data.
*/
fromJSON(values: nbformat.IOutput[]): void;
/**
* Serialize the model to JSON.
*/
toJSON(): nbformat.IOutput[];
}
/**
* The namespace for IOutputAreaModel interfaces.
*/
export
namespace IOutputAreaModel {
/**
* The options used to create a output area model.
*/
export
interface IOptions {
/**
* The initial values for the model.
*/
values?: nbformat.IOutput[];
/**
* Whether the output is trusted. The default is false.
*/
trusted?: boolean;
/**
* The output content factory used by the model.
*
* If not given, a default factory will be used.
*/
contentFactory?: IContentFactory;
}
/**
* The interface for an output content factory.
*/
export
interface IContentFactory {
/**
* Create an output model.
*/
createOutputModel(options: IOutputModel.IOptions): IOutputModel;
}
}
/**
* A namespace for private data.
*/
namespace Private {
/**
* Create the node for an InputWidget.
*/
export
function createInputWidgetNode(): HTMLElement {
let node = document.createElement('div');
let prompt = document.createElement('span');
prompt.className = STDIN_PROMPT_CLASS;
let input = document.createElement('input');
input.className = STDIN_INPUT_CLASS;
node.appendChild(prompt);
node.appendChild(input);
return node;
}
}