forked from jupyterlab/jupyterlab
/
listing.ts
2026 lines (1822 loc) · 55 KB
/
listing.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 {
Dialog,
DOMUtils,
showDialog,
showErrorMessage
} from '@jupyterlab/apputils';
import { PathExt, Time } from '@jupyterlab/coreutils';
import {
IDocumentManager,
isValidFileName,
renameFile
} from '@jupyterlab/docmanager';
import { DocumentRegistry } from '@jupyterlab/docregistry';
import { Contents } from '@jupyterlab/services';
import { IIconRegistry } from '@jupyterlab/ui-components';
import {
ArrayExt,
ArrayIterator,
each,
filter,
find,
IIterator,
map,
toArray
} from '@phosphor/algorithm';
import { MimeData, PromiseDelegate } from '@phosphor/coreutils';
import { ElementExt } from '@phosphor/domutils';
import { Drag, IDragEvent } from '@phosphor/dragdrop';
import { Message, MessageLoop } from '@phosphor/messaging';
import { ISignal, Signal } from '@phosphor/signaling';
import { Widget } from '@phosphor/widgets';
import { FileBrowserModel } from './model';
/**
* The class name added to DirListing widget.
*/
const DIR_LISTING_CLASS = 'jp-DirListing';
/**
* The class name added to a dir listing header node.
*/
const HEADER_CLASS = 'jp-DirListing-header';
/**
* The class name added to a dir listing list header cell.
*/
const HEADER_ITEM_CLASS = 'jp-DirListing-headerItem';
/**
* The class name added to a header cell text node.
*/
const HEADER_ITEM_TEXT_CLASS = 'jp-DirListing-headerItemText';
/**
* The class name added to a header cell icon node.
*/
const HEADER_ITEM_ICON_CLASS = 'jp-DirListing-headerItemIcon';
/**
* The class name added to the dir listing content node.
*/
const CONTENT_CLASS = 'jp-DirListing-content';
/**
* The class name added to dir listing content item.
*/
const ITEM_CLASS = 'jp-DirListing-item';
/**
* The class name added to the listing item text cell.
*/
const ITEM_TEXT_CLASS = 'jp-DirListing-itemText';
/**
* The class name added to the listing item icon cell.
*/
const ITEM_ICON_CLASS = 'jp-DirListing-itemIcon';
/**
* The class name added to the listing item modified cell.
*/
const ITEM_MODIFIED_CLASS = 'jp-DirListing-itemModified';
/**
* The class name added to the dir listing editor node.
*/
const EDITOR_CLASS = 'jp-DirListing-editor';
/**
* The class name added to the name column header cell.
*/
const NAME_ID_CLASS = 'jp-id-name';
/**
* The class name added to the modified column header cell.
*/
const MODIFIED_ID_CLASS = 'jp-id-modified';
/**
* The mime type for a contents drag object.
*/
const CONTENTS_MIME = 'application/x-jupyter-icontents';
/**
* The mime type for a rich contents drag object.
*/
const CONTENTS_MIME_RICH = 'application/x-jupyter-icontentsrich';
/**
* The class name added to drop targets.
*/
const DROP_TARGET_CLASS = 'jp-mod-dropTarget';
/**
* The class name added to selected rows.
*/
const SELECTED_CLASS = 'jp-mod-selected';
/**
* The class name added to drag state icons to add space between the icon and the file name
*/
const DRAG_ICON_CLASS = 'jp-DragIcon';
/**
* The class name added to the widget when there are items on the clipboard.
*/
const CLIPBOARD_CLASS = 'jp-mod-clipboard';
/**
* The class name added to cut rows.
*/
const CUT_CLASS = 'jp-mod-cut';
/**
* The class name added when there are more than one selected rows.
*/
const MULTI_SELECTED_CLASS = 'jp-mod-multiSelected';
/**
* The class name added to indicate running notebook.
*/
const RUNNING_CLASS = 'jp-mod-running';
/**
* The class name added for a decending sort.
*/
const DESCENDING_CLASS = 'jp-mod-descending';
/**
* The maximum duration between two key presses when selecting files by prefix.
*/
const PREFIX_APPEND_DURATION = 1000;
/**
* The threshold in pixels to start a drag event.
*/
const DRAG_THRESHOLD = 5;
/**
* A boolean indicating whether the platform is Mac.
*/
const IS_MAC = !!navigator.platform.match(/Mac/i);
/**
* The factory MIME type supported by phosphor dock panels.
*/
const FACTORY_MIME = 'application/vnd.phosphor.widget-factory';
/**
* A widget which hosts a file list area.
*/
export class DirListing extends Widget {
/**
* Construct a new file browser directory listing widget.
*
* @param model - The file browser view model.
*/
constructor(options: DirListing.IOptions) {
super({
node: (options.renderer =
options.renderer ||
new DirListing.Renderer(options.model.iconRegistry)).createNode()
});
this.addClass(DIR_LISTING_CLASS);
this._model = options.model;
this._model.fileChanged.connect(this._onFileChanged, this);
this._model.refreshed.connect(this._onModelRefreshed, this);
this._model.pathChanged.connect(this._onPathChanged, this);
this._editNode = document.createElement('input');
this._editNode.className = EDITOR_CLASS;
this._manager = this._model.manager;
this._renderer = options.renderer;
const headerNode = DOMUtils.findElement(this.node, HEADER_CLASS);
this._renderer.populateHeaderNode(headerNode);
this._manager.activateRequested.connect(this._onActivateRequested, this);
}
/**
* Dispose of the resources held by the directory listing.
*/
dispose(): void {
this._items.length = 0;
this._sortedItems.length = 0;
this._clipboard.length = 0;
super.dispose();
}
/**
* Get the model used by the listing.
*/
get model(): FileBrowserModel {
return this._model;
}
/**
* Get the dir listing header node.
*
* #### Notes
* This is the node which holds the header cells.
*
* Modifying this node directly can lead to undefined behavior.
*/
get headerNode(): HTMLElement {
return DOMUtils.findElement(this.node, HEADER_CLASS);
}
/**
* Get the dir listing content node.
*
* #### Notes
* This is the node which holds the item nodes.
*
* Modifying this node directly can lead to undefined behavior.
*/
get contentNode(): HTMLElement {
return DOMUtils.findElement(this.node, CONTENT_CLASS);
}
/**
* The renderer instance used by the directory listing.
*/
get renderer(): DirListing.IRenderer {
return this._renderer;
}
/**
* The current sort state.
*/
get sortState(): DirListing.ISortState {
return this._sortState;
}
/**
* A signal fired when an item is opened.
*/
get onItemOpened(): ISignal<DirListing, Contents.IModel> {
return this._onItemOpened;
}
/**
* Create an iterator over the listing's selected items.
*
* @returns A new iterator over the listing's selected items.
*/
selectedItems(): IIterator<Contents.IModel> {
let items = this._sortedItems;
return filter(items, item => this._selection[item.name]);
}
/**
* Create an iterator over the listing's sorted items.
*
* @returns A new iterator over the listing's sorted items.
*/
sortedItems(): IIterator<Contents.IModel> {
return new ArrayIterator(this._sortedItems);
}
/**
* Sort the items using a sort condition.
*/
sort(state: DirListing.ISortState): void {
this._sortedItems = Private.sort(this.model.items(), state);
this._sortState = state;
this.update();
}
/**
* Rename the first currently selected item.
*
* @returns A promise that resolves with the new name of the item.
*/
rename(): Promise<string> {
return this._doRename();
}
/**
* Cut the selected items.
*/
cut(): void {
this._isCut = true;
this._copy();
this.update();
}
/**
* Copy the selected items.
*/
copy(): void {
this._copy();
}
/**
* Paste the items from the clipboard.
*
* @returns A promise that resolves when the operation is complete.
*/
paste(): Promise<void> {
if (!this._clipboard.length) {
this._isCut = false;
return Promise.resolve(undefined);
}
const basePath = this._model.path;
let promises: Promise<Contents.IModel>[] = [];
each(this._clipboard, path => {
if (this._isCut) {
const parts = path.split('/');
const name = parts[parts.length - 1];
const newPath = PathExt.join(basePath, name);
promises.push(this._model.manager.rename(path, newPath));
} else {
promises.push(this._model.manager.copy(path, basePath));
}
});
// Remove any cut modifiers.
each(this._items, item => {
item.classList.remove(CUT_CLASS);
});
this._clipboard.length = 0;
this._isCut = false;
this.removeClass(CLIPBOARD_CLASS);
return Promise.all(promises)
.then(() => {
return undefined;
})
.catch(error => {
void showErrorMessage('Paste Error', error);
});
}
/**
* Delete the currently selected item(s).
*
* @returns A promise that resolves when the operation is complete.
*/
async delete(): Promise<void> {
const items = this._sortedItems.filter(item => this._selection[item.name]);
if (!items.length) {
return;
}
const message =
items.length === 1
? `Are you sure you want to permanently delete: ${items[0].name}?`
: `Are you sure you want to permanently delete the ${items.length} ` +
`files/folders selected?`;
const result = await showDialog({
title: 'Delete',
body: message,
buttons: [Dialog.cancelButton(), Dialog.warnButton({ label: 'Delete' })]
});
if (!this.isDisposed && result.button.accept) {
await this._delete(items.map(item => item.path));
}
}
/**
* Duplicate the currently selected item(s).
*
* @returns A promise that resolves when the operation is complete.
*/
duplicate(): Promise<void> {
const basePath = this._model.path;
let promises: Promise<Contents.IModel>[] = [];
each(this.selectedItems(), item => {
if (item.type !== 'directory') {
let oldPath = PathExt.join(basePath, item.name);
promises.push(this._model.manager.copy(oldPath, basePath));
}
});
return Promise.all(promises)
.then(() => {
return undefined;
})
.catch(error => {
void showErrorMessage('Duplicate file', error);
});
}
/**
* Download the currently selected item(s).
*/
async download(): Promise<void> {
await Promise.all(
toArray(this.selectedItems())
.filter(item => item.type !== 'directory')
.map(item => this._model.download(item.path))
);
}
/**
* Shut down kernels on the applicable currently selected items.
*
* @returns A promise that resolves when the operation is complete.
*/
shutdownKernels(): Promise<void> {
const model = this._model;
const items = this._sortedItems;
const paths = items.map(item => item.path);
const promises = toArray(this._model.sessions())
.filter(session => {
let index = ArrayExt.firstIndexOf(paths, session.path);
return this._selection[items[index].name];
})
.map(session => model.manager.services.sessions.shutdown(session.id));
return Promise.all(promises)
.then(() => {
return undefined;
})
.catch(error => {
void showErrorMessage('Shut down kernel', error);
});
}
/**
* Select next item.
*
* @param keepExisting - Whether to keep the current selection and add to it.
*/
selectNext(keepExisting = false): void {
let index = -1;
let selected = Object.keys(this._selection);
let items = this._sortedItems;
if (selected.length === 1 || keepExisting) {
// Select the next item.
let name = selected[selected.length - 1];
index = ArrayExt.findFirstIndex(items, value => value.name === name);
index += 1;
if (index === this._items.length) {
index = 0;
}
} else if (selected.length === 0) {
// Select the first item.
index = 0;
} else {
// Select the last selected item.
let name = selected[selected.length - 1];
index = ArrayExt.findFirstIndex(items, value => value.name === name);
}
if (index !== -1) {
this._selectItem(index, keepExisting);
ElementExt.scrollIntoViewIfNeeded(this.contentNode, this._items[index]);
}
}
/**
* Select previous item.
*
* @param keepExisting - Whether to keep the current selection and add to it.
*/
selectPrevious(keepExisting = false): void {
let index = -1;
let selected = Object.keys(this._selection);
let items = this._sortedItems;
if (selected.length === 1 || keepExisting) {
// Select the previous item.
let name = selected[0];
index = ArrayExt.findFirstIndex(items, value => value.name === name);
index -= 1;
if (index === -1) {
index = this._items.length - 1;
}
} else if (selected.length === 0) {
// Select the last item.
index = this._items.length - 1;
} else {
// Select the first selected item.
let name = selected[0];
index = ArrayExt.findFirstIndex(items, value => value.name === name);
}
if (index !== -1) {
this._selectItem(index, keepExisting);
ElementExt.scrollIntoViewIfNeeded(this.contentNode, this._items[index]);
}
}
/**
* Select the first item that starts with prefix being typed.
*/
selectByPrefix(): void {
const prefix = this._searchPrefix.toLowerCase();
let items = this._sortedItems;
let index = ArrayExt.findFirstIndex(items, value => {
return value.name.toLowerCase().substr(0, prefix.length) === prefix;
});
if (index !== -1) {
this._selectItem(index, false);
ElementExt.scrollIntoViewIfNeeded(this.contentNode, this._items[index]);
}
}
/**
* Get whether an item is selected by name.
*
* @param name - The name of of the item.
*
* @returns Whether the item is selected.
*/
isSelected(name: string): boolean {
return this._selection[name] === true;
}
/**
* Find a model given a click.
*
* @param event - The mouse event.
*
* @returns The model for the selected file.
*/
modelForClick(event: MouseEvent): Contents.IModel | undefined {
let items = this._sortedItems;
let index = Private.hitTestNodes(this._items, event.clientX, event.clientY);
if (index !== -1) {
return items[index];
}
return undefined;
}
/**
* Clear the selected items.
*/
clearSelectedItems() {
this._selection = Object.create(null);
}
/**
* Select an item by name.
*
* @param name - The name of the item to select.
*
* @returns A promise that resolves when the name is selected.
*/
async selectItemByName(name: string): Promise<void> {
// Make sure the file is available.
await this.model.refresh();
if (this.isDisposed) {
throw new Error('File browser is disposed.');
}
let items = this._sortedItems;
let index = ArrayExt.findFirstIndex(items, value => value.name === name);
if (index === -1) {
throw new Error('Item does not exist.');
}
this._selectItem(index, false);
MessageLoop.sendMessage(this, Widget.Msg.UpdateRequest);
ElementExt.scrollIntoViewIfNeeded(this.contentNode, this._items[index]);
}
/**
* Handle the DOM events for the directory listing.
*
* @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;
case 'keydown':
this._evtKeydown(event as KeyboardEvent);
break;
case 'click':
this._evtClick(event as MouseEvent);
break;
case 'dblclick':
this._evtDblClick(event as MouseEvent);
break;
case 'dragenter':
case 'dragover':
this.addClass('jp-mod-native-drop');
event.preventDefault();
break;
case 'dragleave':
case 'dragend':
this.removeClass('jp-mod-native-drop');
break;
case 'drop':
this.removeClass('jp-mod-native-drop');
this._evtNativeDrop(event as DragEvent);
break;
case 'scroll':
this._evtScroll(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;
}
}
/**
* A message handler invoked on an `'after-attach'` message.
*/
protected onAfterAttach(msg: Message): void {
super.onAfterAttach(msg);
let node = this.node;
let content = DOMUtils.findElement(node, CONTENT_CLASS);
node.addEventListener('mousedown', this);
node.addEventListener('keydown', this);
node.addEventListener('click', this);
node.addEventListener('dblclick', this);
content.addEventListener('dragenter', this);
content.addEventListener('dragover', this);
content.addEventListener('dragleave', this);
content.addEventListener('dragend', this);
content.addEventListener('drop', this);
content.addEventListener('scroll', this);
content.addEventListener('p-dragenter', this);
content.addEventListener('p-dragleave', this);
content.addEventListener('p-dragover', this);
content.addEventListener('p-drop', this);
}
/**
* A message handler invoked on a `'before-detach'` message.
*/
protected onBeforeDetach(msg: Message): void {
super.onBeforeDetach(msg);
let node = this.node;
let content = DOMUtils.findElement(node, CONTENT_CLASS);
node.removeEventListener('mousedown', this);
node.removeEventListener('keydown', this);
node.removeEventListener('click', this);
node.removeEventListener('dblclick', this);
content.removeEventListener('scroll', this);
content.removeEventListener('dragover', this);
content.removeEventListener('dragover', this);
content.removeEventListener('dragleave', this);
content.removeEventListener('dragend', this);
content.removeEventListener('drop', this);
content.removeEventListener('p-dragenter', this);
content.removeEventListener('p-dragleave', this);
content.removeEventListener('p-dragover', this);
content.removeEventListener('p-drop', this);
document.removeEventListener('mousemove', this, true);
document.removeEventListener('mouseup', this, true);
}
/**
* A message handler invoked on an `'after-show'` message.
*/
protected onAfterShow(msg: Message): void {
if (this._isDirty) {
// Update the sorted items.
this.sort(this.sortState);
this.update();
}
}
/**
* A handler invoked on an `'update-request'` message.
*/
protected onUpdateRequest(msg: Message): void {
this._isDirty = false;
// Fetch common variables.
let items = this._sortedItems;
let nodes = this._items;
let content = DOMUtils.findElement(this.node, CONTENT_CLASS);
let renderer = this._renderer;
this.removeClass(MULTI_SELECTED_CLASS);
this.removeClass(SELECTED_CLASS);
// Remove any excess item nodes.
while (nodes.length > items.length) {
content.removeChild(nodes.pop());
}
// Add any missing item nodes.
while (nodes.length < items.length) {
let node = renderer.createItemNode();
node.classList.add(ITEM_CLASS);
nodes.push(node);
content.appendChild(node);
}
// Remove extra classes from the nodes.
nodes.forEach(item => {
item.classList.remove(SELECTED_CLASS);
item.classList.remove(RUNNING_CLASS);
item.classList.remove(CUT_CLASS);
});
// Add extra classes to item nodes based on widget state.
items.forEach((item, i) => {
let node = nodes[i];
let ft = this._manager.registry.getFileTypeForModel(item);
renderer.updateItemNode(node, item, ft);
if (this._selection[item.name]) {
node.classList.add(SELECTED_CLASS);
if (this._isCut && this._model.path === this._prevPath) {
node.classList.add(CUT_CLASS);
}
}
// add metadata to the node
node.setAttribute(
'data-isdir',
item.type === 'directory' ? 'true' : 'false'
);
});
// Handle the selectors on the widget node.
let selected = Object.keys(this._selection).length;
if (selected) {
this.addClass(SELECTED_CLASS);
if (selected > 1) {
this.addClass(MULTI_SELECTED_CLASS);
}
}
// Handle file session statuses.
let paths = items.map(item => item.path);
each(this._model.sessions(), session => {
let index = ArrayExt.firstIndexOf(paths, session.path);
let node = nodes[index];
let name = session.kernel.name;
let specs = this._model.specs;
node.classList.add(RUNNING_CLASS);
if (specs) {
const spec = specs.kernelspecs[name];
name = spec ? spec.display_name : 'unknown';
}
node.title = `${node.title}\nKernel: ${name}`;
});
this._prevPath = this._model.path;
}
onResize(msg: Widget.ResizeMessage) {
const { width } =
msg.width === -1 ? this.node.getBoundingClientRect() : msg;
this.toggleClass('jp-DirListing-narrow', width < 250);
}
/**
* Handle the `'click'` event for the widget.
*/
private _evtClick(event: MouseEvent) {
let target = event.target as HTMLElement;
let header = this.headerNode;
if (header.contains(target)) {
let state = this.renderer.handleHeaderClick(header, event);
if (state) {
this.sort(state);
}
return;
}
}
/**
* Handle the `'scroll'` event for the widget.
*/
private _evtScroll(event: MouseEvent): void {
this.headerNode.scrollLeft = this.contentNode.scrollLeft;
}
/**
* Handle the `'mousedown'` event for the widget.
*/
private _evtMousedown(event: MouseEvent): void {
// Bail if clicking within the edit node
if (event.target === this._editNode) {
return;
}
// Blur the edit node if necessary.
if (this._editNode.parentNode) {
if (this._editNode !== (event.target as HTMLElement)) {
this._editNode.focus();
this._editNode.blur();
clearTimeout(this._selectTimer);
} else {
return;
}
}
let index = Private.hitTestNodes(this._items, event.clientX, event.clientY);
if (index === -1) {
return;
}
this._handleFileSelect(event);
if (event.button !== 0) {
clearTimeout(this._selectTimer);
}
// Check for clearing a context menu.
let newContext = (IS_MAC && event.ctrlKey) || event.button === 2;
if (newContext) {
return;
}
// Left mouse press for drag start.
if (event.button === 0) {
this._dragData = {
pressX: event.clientX,
pressY: event.clientY,
index: index
};
document.addEventListener('mouseup', this, true);
document.addEventListener('mousemove', this, true);
}
}
/**
* Handle the `'mouseup'` event for the widget.
*/
private _evtMouseup(event: MouseEvent): void {
// Handle any soft selection from the previous mouse down.
if (this._softSelection) {
let altered = event.metaKey || event.shiftKey || event.ctrlKey;
// See if we need to clear the other selection.
if (!altered && event.button === 0) {
this.clearSelectedItems();
this._selection[this._softSelection] = true;
this.update();
}
this._softSelection = '';
}
// Remove the drag listeners if necessary.
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 || !this._dragData) {
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(data.index, event.clientX, event.clientY);
}
/**
* Handle the opening of an item.
*/
private _handleOpen(item: Contents.IModel): void {
this._onItemOpened.emit(item);
if (item.type === 'directory') {
const localPath = this._manager.services.contents.localPath(item.path);
this._model
.cd(`/${localPath}`)
.catch(error => showErrorMessage('Open directory', error));
} else {
let path = item.path;
this._manager.openOrReveal(path);
}
}
/**
* Handle the `'keydown'` event for the widget.
*/
private _evtKeydown(event: KeyboardEvent): void {
switch (event.keyCode) {
case 13: // Enter
// Do nothing if any modifier keys are pressed.
if (event.ctrlKey || event.shiftKey || event.altKey || event.metaKey) {
return;
}
event.preventDefault();
event.stopPropagation();
let selected = Object.keys(this._selection);
let name = selected[0];
let items = this._sortedItems;
let i = ArrayExt.findFirstIndex(items, value => value.name === name);
if (i === -1) {
return;
}
let item = this._sortedItems[i];
this._handleOpen(item);
break;
case 38: // Up arrow
this.selectPrevious(event.shiftKey);
event.stopPropagation();
event.preventDefault();
break;
case 40: // Down arrow
this.selectNext(event.shiftKey);
event.stopPropagation();
event.preventDefault();
break;
default:
break;
}
// Detects printable characters typed by the user.
// Not all browsers support .key, but it discharges us from reconstructing
// characters from key codes.
if (!this._inRename && event.key !== undefined && event.key.length === 1) {
this._searchPrefix += event.key;
clearTimeout(this._searchPrefixTimer);
this._searchPrefixTimer = window.setTimeout(() => {
this._searchPrefix = '';
}, PREFIX_APPEND_DURATION);
this.selectByPrefix();
event.stopPropagation();
event.preventDefault();
}
}
/**
* Handle the `'dblclick'` event for the widget.
*/
private _evtDblClick(event: MouseEvent): void {