forked from jupyterlab/jupyterlab
-
Notifications
You must be signed in to change notification settings - Fork 0
/
widget.ts
1518 lines (1366 loc) · 39.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
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright (c) Jupyter Development Team.
// Distributed under the terms of the Modified BSD License.
import {
ArrayExt, each
} from '@phosphor/algorithm';
import {
JSONValue
} from '@phosphor/coreutils';
import {
Message
} from '@phosphor/messaging';
import {
MimeData
} from '@phosphor/coreutils';
import {
AttachedProperty
} from '@phosphor/properties';
import {
ISignal, Signal
} from '@phosphor/signaling';
import {
Drag, IDragEvent
} from '@phosphor/dragdrop';
import {
PanelLayout
} from '@phosphor/widgets';
import {
Widget
} from '@phosphor/widgets';
import {
ICellModel, BaseCellWidget, IMarkdownCellModel,
CodeCellWidget, MarkdownCellWidget,
ICodeCellModel, RawCellWidget, IRawCellModel,
} from '@jupyterlab/cells';
import {
IEditorMimeTypeService, CodeEditor
} from '@jupyterlab/codeeditor';
import {
IChangedArgs, IObservableMap, ObservableMap, IObservableVector,
ObservableVector, nbformat
} from '@jupyterlab/coreutils';
import {
RenderMime
} from '@jupyterlab/rendermime';
import {
OutputAreaWidget
} from '@jupyterlab/outputarea';
import {
INotebookModel
} from './model';
/**
* The class name added to notebook widgets.
*/
const NB_CLASS = 'jp-Notebook';
/**
* The class name added to notebook widget cells.
*/
const NB_CELL_CLASS = 'jp-Notebook-cell';
/**
* The class name added to a notebook in edit mode.
*/
const EDIT_CLASS = 'jp-mod-editMode';
/**
* The class name added to a notebook in command mode.
*/
const COMMAND_CLASS = 'jp-mod-commandMode';
/**
* The class name added to the active cell.
*/
const ACTIVE_CLASS = 'jp-mod-active';
/**
* The class name added to selected cells.
*/
const SELECTED_CLASS = 'jp-mod-selected';
/**
* The class name added to an active cell when there are other selected cells.
*/
const OTHER_SELECTED_CLASS = 'jp-mod-multiSelected';
/**
* The class name added to unconfined images.
*/
const UNCONFINED_CLASS = 'jp-mod-unconfined';
/**
* The class name added to a drop target.
*/
const DROP_TARGET_CLASS = 'jp-mod-dropTarget';
/**
* The class name added to a drop source.
*/
const DROP_SOURCE_CLASS = 'jp-mod-dropSource';
/**
* The class name added to drag images.
*/
const DRAG_IMAGE_CLASS = 'jp-dragImage';
/**
* The class name added to a filled circle.
*/
const FILLED_CIRCLE_CLASS = 'jp-filledCircle';
/**
* The mimetype used for Jupyter cell data.
*/
export
const JUPYTER_CELL_MIME: string = 'application/vnd.jupyter.cells';
/**
* The threshold in pixels to start a drag event.
*/
const DRAG_THRESHOLD = 5;
/**
* The interactivity modes for the notebook.
*/
export
type NotebookMode = 'command' | 'edit';
/**
* A widget which renders static non-interactive notebooks.
*
* #### 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 StaticNotebook extends Widget {
/**
* Construct a notebook widget.
*/
constructor(options: StaticNotebook.IOptions) {
super();
this.addClass(NB_CLASS);
this.rendermime = options.rendermime;
this.layout = new Private.NotebookPanelLayout();
this.contentFactory = options.contentFactory;
this._mimetypeService = options.mimeTypeService;
}
/**
* A signal emitted when the model of the notebook changes.
*/
get modelChanged(): ISignal<this, void> {
return this._modelChanged;
}
/**
* A signal emitted when the model content changes.
*
* #### Notes
* This is a convenience signal that follows the current model.
*/
get modelContentChanged(): ISignal<this, void> {
return this._modelContentChanged;
}
/**
* The cell factory used by the widget.
*/
readonly contentFactory: StaticNotebook.IContentFactory;
/**
* The Rendermime instance used by the widget.
*/
readonly rendermime: RenderMime;
/**
* The model for the widget.
*/
get model(): INotebookModel {
return this._model;
}
set model(newValue: INotebookModel) {
newValue = newValue || null;
if (this._model === newValue) {
return;
}
let oldValue = this._model;
this._model = newValue;
// Trigger private, protected, and public changes.
this._onModelChanged(oldValue, newValue);
this.onModelChanged(oldValue, newValue);
this._modelChanged.emit(void 0);
}
/**
* Get the mimetype for code cells.
*/
get codeMimetype(): string {
return this._mimetype;
}
/**
* A read-only sequence of the widgets in the notebook.
*/
get widgets(): ReadonlyArray<BaseCellWidget> {
return (this.layout as PanelLayout).widgets as ReadonlyArray<BaseCellWidget>;
}
/**
* Dispose of the resources held by the widget.
*/
dispose() {
// Do nothing if already disposed.
if (this.isDisposed) {
return;
}
this._model = null;
super.dispose();
}
/**
* Handle a new model.
*
* #### Notes
* This method is called after the model change has been handled
* internally and before the `modelChanged` signal is emitted.
* The default implementation is a no-op.
*/
protected onModelChanged(oldValue: INotebookModel, newValue: INotebookModel): void {
// No-op.
}
/**
* Handle changes to the notebook model content.
*
* #### Notes
* The default implementation emits the `modelContentChanged` signal.
*/
protected onModelContentChanged(model: INotebookModel, args: void): void {
this._modelContentChanged.emit(void 0);
}
/**
* Handle changes to the notebook model metadata.
*
* #### Notes
* The default implementation updates the mimetypes of the code cells
* when the `language_info` metadata changes.
*/
protected onMetadataChanged(sender: IObservableMap<JSONValue>, args: ObservableMap.IChangedArgs<JSONValue>): void {
switch (args.key) {
case 'language_info':
this._updateMimetype();
break;
default:
break;
}
}
/**
* Handle a cell being inserted.
*
* The default implementation is a no-op
*/
protected onCellInserted(index: number, cell: BaseCellWidget): void {
// This is a no-op.
}
/**
* Handle a cell being moved.
*
* The default implementation is a no-op
*/
protected onCellMoved(fromIndex: number, toIndex: number): void {
// This is a no-op.
}
/**
* Handle a cell being removed.
*
* The default implementation is a no-op
*/
protected onCellRemoved(cell: BaseCellWidget): void {
// This is a no-op.
}
/**
* Handle a new model on the widget.
*/
private _onModelChanged(oldValue: INotebookModel, newValue: INotebookModel): void {
let layout = this.layout as PanelLayout;
if (oldValue) {
oldValue.cells.changed.disconnect(this._onCellsChanged, this);
oldValue.metadata.changed.disconnect(this.onMetadataChanged, this);
oldValue.contentChanged.disconnect(this.onModelContentChanged, this);
// TODO: reuse existing cell widgets if possible.
while (layout.widgets.length) {
this._removeCell(0);
}
}
if (!newValue) {
this._mimetype = 'text/plain';
return;
}
this._updateMimetype();
let cells = newValue.cells;
each(cells, (cell: ICellModel, i: number) => {
this._insertCell(i, cell);
});
cells.changed.connect(this._onCellsChanged, this);
newValue.contentChanged.connect(this.onModelContentChanged, this);
newValue.metadata.changed.connect(this.onMetadataChanged, this);
}
/**
* Handle a change cells event.
*/
private _onCellsChanged(sender: IObservableVector<ICellModel>, args: ObservableVector.IChangedArgs<ICellModel>) {
let index = 0;
switch (args.type) {
case 'add':
index = args.newIndex;
each(args.newValues, value => {
this._insertCell(index++, value);
});
break;
case 'move':
this._moveCell(args.oldIndex, args.newIndex);
break;
case 'remove':
each(args.oldValues, value => {
this._removeCell(args.oldIndex);
});
break;
case 'set':
// TODO: reuse existing widgets if possible.
index = args.newIndex;
each(args.newValues, value => {
this._removeCell(index);
this._insertCell(index, value);
index++;
});
break;
default:
return;
}
}
/**
* Create a cell widget and insert into the notebook.
*/
private _insertCell(index: number, cell: ICellModel): void {
let widget: BaseCellWidget;
switch (cell.type) {
case 'code':
widget = this._createCodeCell(cell as ICodeCellModel);
widget.model.mimeType = this._mimetype;
break;
case 'markdown':
widget = this._createMarkdownCell(cell as IMarkdownCellModel);
break;
default:
widget = this._createRawCell(cell as IRawCellModel);
}
widget.addClass(NB_CELL_CLASS);
let layout = this.layout as PanelLayout;
layout.insertWidget(index, widget);
this.onCellInserted(index, widget);
}
/**
* Create a code cell widget from a code cell model.
*/
private _createCodeCell(model: ICodeCellModel): CodeCellWidget {
let factory = this.contentFactory;
let contentFactory = factory.codeCellContentFactory;
let rendermime = this.rendermime;
let options = { model, rendermime, contentFactory };
return factory.createCodeCell(options, this);
}
/**
* Create a markdown cell widget from a markdown cell model.
*/
private _createMarkdownCell(model: IMarkdownCellModel): MarkdownCellWidget {
let factory = this.contentFactory;
let contentFactory = factory.markdownCellContentFactory;
let rendermime = this.rendermime;
let options = { model, rendermime, contentFactory };
return factory.createMarkdownCell(options, this);
}
/**
* Create a raw cell widget from a raw cell model.
*/
private _createRawCell(model: IRawCellModel): RawCellWidget {
let factory = this.contentFactory;
let contentFactory = factory.rawCellContentFactory;
let options = { model, contentFactory };
return factory.createRawCell(options, this);
}
/**
* Move a cell widget.
*/
private _moveCell(fromIndex: number, toIndex: number): void {
let layout = this.layout as PanelLayout;
layout.insertWidget(toIndex, layout.widgets[fromIndex]);
this.onCellMoved(fromIndex, toIndex);
}
/**
* Remove a cell widget.
*/
private _removeCell(index: number): void {
let layout = this.layout as PanelLayout;
let widget = layout.widgets[index] as BaseCellWidget;
widget.parent = null;
this.onCellRemoved(widget);
widget.dispose();
}
/**
* Update the mimetype of the notebook.
*/
private _updateMimetype(): void {
let info = this._model.metadata.get('language_info') as nbformat.ILanguageInfoMetadata;
if (!info) {
return;
}
this._mimetype = this._mimetypeService.getMimeTypeByLanguage(info);
each(this.widgets, widget => {
if (widget.model.type === 'code') {
widget.model.mimeType = this._mimetype;
}
});
}
private _mimetype = 'text/plain';
private _model: INotebookModel = null;
private _mimetypeService: IEditorMimeTypeService;
private _modelChanged = new Signal<this, void>(this);
private _modelContentChanged = new Signal<this, void>(this);
}
/**
* The namespace for the `StaticNotebook` class statics.
*/
export
namespace StaticNotebook {
/**
* An options object for initializing a static notebook.
*/
export
interface IOptions {
/**
* The rendermime instance used by the widget.
*/
rendermime: RenderMime;
/**
* The language preference for the model.
*/
languagePreference?: string;
/**
* A factory for creating content.
*/
contentFactory: IContentFactory;
/**
* The service used to look up mime types.
*/
mimeTypeService: IEditorMimeTypeService;
}
/**
* A factory for creating notebook content.
*/
export
interface IContentFactory {
/**
* The editor factory.
*/
readonly editorFactory: CodeEditor.Factory;
/**
* The factory for code cell widget content.
*/
readonly codeCellContentFactory?: CodeCellWidget.IContentFactory;
/**
* The factory for raw cell widget content.
*/
readonly rawCellContentFactory?: BaseCellWidget.IContentFactory;
/**
* The factory for markdown cell widget content.
*/
readonly markdownCellContentFactory?: BaseCellWidget.IContentFactory;
/**
* Create a new code cell widget.
*/
createCodeCell(options: CodeCellWidget.IOptions, parent: StaticNotebook): CodeCellWidget;
/**
* Create a new markdown cell widget.
*/
createMarkdownCell(options: MarkdownCellWidget.IOptions, parent: StaticNotebook): MarkdownCellWidget;
/**
* Create a new raw cell widget.
*/
createRawCell(options: RawCellWidget.IOptions, parent: StaticNotebook): RawCellWidget;
}
/**
* The default implementation of an `IContentFactory`.
*/
export
class ContentFactory implements IContentFactory {
/**
* Creates a new renderer.
*/
constructor(options: IContentFactoryOptions) {
let editorFactory = options.editorFactory;
let outputAreaContentFactory = (options.outputAreaContentFactory ||
OutputAreaWidget.defaultContentFactory
);
this.codeCellContentFactory = (options.codeCellContentFactory ||
new CodeCellWidget.ContentFactory({
editorFactory,
outputAreaContentFactory
})
);
this.rawCellContentFactory = (options.rawCellContentFactory ||
new RawCellWidget.ContentFactory({ editorFactory })
);
this.markdownCellContentFactory = (options.markdownCellContentFactory ||
new MarkdownCellWidget.ContentFactory({ editorFactory })
);
}
/**
* The editor factory.
*/
readonly editorFactory: CodeEditor.Factory;
/**
* The factory for code cell widget content.
*/
readonly codeCellContentFactory: CodeCellWidget.IContentFactory;
/**
* The factory for raw cell widget content.
*/
readonly rawCellContentFactory: BaseCellWidget.IContentFactory;
/**
* The factory for markdown cell widget content.
*/
readonly markdownCellContentFactory: BaseCellWidget.IContentFactory;
/**
* Create a new code cell widget.
*/
createCodeCell(options: CodeCellWidget.IOptions, parent: StaticNotebook): CodeCellWidget {
return new CodeCellWidget(options);
}
/**
* Create a new markdown cell widget.
*/
createMarkdownCell(options: MarkdownCellWidget.IOptions, parent: StaticNotebook): MarkdownCellWidget {
return new MarkdownCellWidget(options);
}
/**
* Create a new raw cell widget.
*/
createRawCell(options: RawCellWidget.IOptions, parent: StaticNotebook): RawCellWidget {
return new RawCellWidget(options);
}
}
/**
* An options object for initializing a notebook content factory.
*/
export
interface IContentFactoryOptions {
/**
* The editor factory.
*/
editorFactory: CodeEditor.Factory;
/**
* The factory for output area content.
*/
outputAreaContentFactory?: OutputAreaWidget.IContentFactory;
/**
* The factory for code cell widget content. If given, this will
* take precedence over the `outputAreaContentFactory`.
*/
codeCellContentFactory?: CodeCellWidget.IContentFactory;
/**
* The factory for raw cell widget content.
*/
rawCellContentFactory?: BaseCellWidget.IContentFactory;
/**
* The factory for markdown cell widget content.
*/
markdownCellContentFactory?: BaseCellWidget.IContentFactory;
}
}
/**
* A notebook widget that supports interactivity.
*/
export
class Notebook extends StaticNotebook {
/**
* Construct a notebook widget.
*/
constructor(options: StaticNotebook.IOptions) {
super(options);
this.node.tabIndex = -1; // Allow the widget to take focus.
// Allow the node to scroll while dragging items.
this.node.setAttribute('data-p-dragscroll', 'true');
}
/**
* A signal emitted when the active cell changes.
*
* #### Notes
* This can be due to the active index changing or the
* cell at the active index changing.
*/
get activeCellChanged(): ISignal<this, BaseCellWidget> {
return this._activeCellChanged;
}
/**
* A signal emitted when the state of the notebook changes.
*/
get stateChanged(): ISignal<this, IChangedArgs<any>> {
return this._stateChanged;
}
/**
* A signal emitted when the selection state of the notebook changes.
*/
get selectionChanged(): ISignal<this, void> {
return this._selectionChanged;
}
/**
* The interactivity mode of the notebook.
*/
get mode(): NotebookMode {
return this._mode;
}
set mode(newValue: NotebookMode) {
let activeCell = this.activeCell;
if (!activeCell) {
newValue = 'command';
}
if (newValue === this._mode) {
this._ensureFocus();
return;
}
// Post an update request.
this.update();
let oldValue = this._mode;
this._mode = newValue;
if (newValue === 'edit') {
let node = this.activeCell.editorWidget.node;
this.scrollToPosition(node.getBoundingClientRect().top);
// Edit mode deselects all cells.
each(this.widgets, widget => { this.deselect(widget); });
// Edit mode unrenders an active markdown widget.
if (activeCell instanceof MarkdownCellWidget) {
activeCell.rendered = false;
}
}
this._stateChanged.emit({ name: 'mode', oldValue, newValue });
this._ensureFocus();
}
/**
* The active cell index of the notebook.
*
* #### Notes
* The index will be clamped to the bounds of the notebook cells.
*/
get activeCellIndex(): number {
if (!this.model) {
return -1;
}
return this.model.cells.length ? this._activeCellIndex : -1;
}
set activeCellIndex(newValue: number) {
let oldValue = this._activeCellIndex;
if (!this.model || !this.model.cells.length) {
newValue = -1;
} else {
newValue = Math.max(newValue, 0);
newValue = Math.min(newValue, this.model.cells.length - 1);
}
this._activeCellIndex = newValue;
let cell = this.widgets[newValue];
if (cell !== this._activeCell) {
// Post an update request.
this.update();
this._activeCell = cell;
this._activeCellChanged.emit(cell);
}
if (this.mode === 'edit' && cell instanceof MarkdownCellWidget) {
cell.rendered = false;
}
this._ensureFocus();
if (newValue === oldValue) {
return;
}
this._stateChanged.emit({ name: 'activeCellIndex', oldValue, newValue });
}
/**
* Get the active cell widget.
*/
get activeCell(): BaseCellWidget {
return this._activeCell;
}
/**
* Dispose of the resources held by the widget.
*/
dispose(): void {
if (this._activeCell === null) {
return;
}
this._activeCell = null;
super.dispose();
}
/**
* Select a cell widget.
*
* #### Notes
* It is a no-op if the value does not change.
* It will emit the `selectionChanged` signal.
*/
select(widget: BaseCellWidget): void {
if (Private.selectedProperty.get(widget)) {
return;
}
Private.selectedProperty.set(widget, true);
this._selectionChanged.emit(void 0);
this.update();
}
/**
* Deselect a cell widget.
*
* #### Notes
* It is a no-op if the value does not change.
* It will emit the `selectionChanged` signal.
*/
deselect(widget: BaseCellWidget): void {
if (!Private.selectedProperty.get(widget)) {
return;
}
Private.selectedProperty.set(widget, false);
this._selectionChanged.emit(void 0);
this.update();
}
/**
* Whether a cell is selected or is the active cell.
*/
isSelected(widget: BaseCellWidget): boolean {
if (widget === this._activeCell) {
return true;
}
return Private.selectedProperty.get(widget);
}
/**
* Deselect all of the cells.
*/
deselectAll(): void {
let changed = false;
each(this.widgets, widget => {
if (Private.selectedProperty.get(widget)) {
changed = true;
}
Private.selectedProperty.set(widget, false);
});
if (changed) {
this._selectionChanged.emit(void 0);
}
// Make sure we have a valid active cell.
this.activeCellIndex = this.activeCellIndex;
}
/**
* Scroll so that the given position is visible.
*
* @param position - The vertical position in the notebook widget.
*
* @param threshold - An optional threshold for the scroll. Defaults to 25
* percent of the widget height.
*/
scrollToPosition(position: number, threshold=25): void {
let node = this.node;
let ar = node.getBoundingClientRect();
let delta = position - ar.top - ar.height / 2;
if (Math.abs(delta) > ar.height * threshold / 100) {
node.scrollTop += delta;
}
}
/**
* 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 notebook panel's node. It should
* not be called directly by user code.
*/
handleEvent(event: Event): void {
if (!this.model || this.model.readOnly) {
return;
}
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;
case 'keydown':
this._ensureFocus(true);
break;
case 'dblclick':
this._evtDblClick(event as MouseEvent);
break;
case 'focus':
this._evtFocus(event as MouseEvent);
break;
case 'blur':
this._evtBlur(event as MouseEvent);
break;
case 'p-dragenter':
this._evtDragEnter(event as IDragEvent);
break;
case 'p-dragleave':
this._evtDragLeave(event as IDragEvent);
break;
case 'p-dragover':
this._evtDragOver(event as IDragEvent);
break;
case 'p-drop':
this._evtDrop(event as IDragEvent);
break;
default:
break;
}
}
/**
* Handle `after-attach` messages for the widget.
*/
protected onAfterAttach(msg: Message): void {
super.onAfterAttach(msg);
let node = this.node;
node.addEventListener('mousedown', this);
node.addEventListener('keydown', this);
node.addEventListener('dblclick', this);
node.addEventListener('focus', this, true);
node.addEventListener('blur', this, true);
node.addEventListener('p-dragenter', this);
node.addEventListener('p-dragleave', this);
node.addEventListener('p-dragover', this);
node.addEventListener('p-drop', this);
}
/**
* Handle `before-detach` messages for the widget.
*/
protected onBeforeDetach(msg: Message): void {
let node = this.node;
node.removeEventListener('mousedown', this);
node.removeEventListener('keydown', this);
node.removeEventListener('dblclick', this);
node.removeEventListener('focus', this, true);
node.removeEventListener('blur', this, true);
node.removeEventListener('p-dragenter', this);
node.removeEventListener('p-dragleave', this);
node.removeEventListener('p-dragover', this);
node.removeEventListener('p-drop', this);
document.removeEventListener('mousemove', this, true);
document.removeEventListener('mouseup', this, true);
}
/**
* Handle `'activate-request'` messages.
*/
protected onActivateRequest(msg: Message): void {
this._ensureFocus(true);
}
/**
* Handle `update-request` messages sent to the widget.
*/
protected onUpdateRequest(msg: Message): void {
let activeCell = this.activeCell;
// Set the appropriate classes on the cells.
if (this.mode === 'edit') {
this.addClass(EDIT_CLASS);
this.removeClass(COMMAND_CLASS);
} else {
this.addClass(COMMAND_CLASS);
this.removeClass(EDIT_CLASS);
}
if (activeCell) {
activeCell.addClass(ACTIVE_CLASS);
}
let count = 0;
each(this.widgets, widget => {
if (widget !== activeCell) {
widget.removeClass(ACTIVE_CLASS);
}
widget.removeClass(OTHER_SELECTED_CLASS);
if (this.isSelected(widget)) {
widget.addClass(SELECTED_CLASS);
count++;
} else {
widget.removeClass(SELECTED_CLASS);
}
});
if (count > 1) {
activeCell.addClass(OTHER_SELECTED_CLASS);
}
}
/**
* Handle a cell being inserted.
*/
protected onCellInserted(index: number, cell: BaseCellWidget): void {
cell.editor.edgeRequested.connect(this._onEdgeRequest, this);
// Trigger an update of the active cell.
this.activeCellIndex = this.activeCellIndex;
}
/**
* Handle a cell being moved.
*/
protected onCellMoved(fromIndex: number, toIndex: number): void {
if (fromIndex === this.activeCellIndex) {
this.activeCellIndex = toIndex;
}
}
/**
* Handle a cell being removed.