This repository has been archived by the owner on Sep 1, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
model.ts
653 lines (594 loc) · 16.7 KB
/
model.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
// Copyright (c) Jupyter Development Team.
// Distributed under the terms of the Modified BSD License.
'use strict';
import {
IContentsManager, IContentsModel, IContentsOpts, INotebookSessionManager,
INotebookSession, ISessionId, KernelStatus, getKernelSpecs, IKernelSpecId
} from 'jupyter-js-services';
import * as arrays
from 'phosphor-arrays';
import {
IDisposable
} from 'phosphor-disposable';
import {
IChangedArgs
} from 'phosphor-properties';
import {
ISignal, Signal, clearSignalData
} from 'phosphor-signaling';
import * as utils
from './utils';
/**
* An implementation of a file browser view model.
*
* #### Notes
* All paths parameters without a leading `'/'` are interpreted as relative to
* the current directory. Supports `'../'` syntax.
*/
export
class FileBrowserModel implements IDisposable {
/**
* Construct a new file browser view model.
*/
constructor(contentsManager: IContentsManager, sessionManager: INotebookSessionManager) {
this._contentsManager = contentsManager;
this._sessionManager = sessionManager;
this._model = { path: '', name: '/', type: 'directory', content: [] };
this._getKernelSpecs();
this.cd();
}
/**
* Get the refreshed signal.
*/
get refreshed(): ISignal<FileBrowserModel, void> {
return Private.refreshedSignal.bind(this);
}
/**
* Get the file open requested signal.
*/
get openRequested(): ISignal<FileBrowserModel, IContentsModel> {
return Private.openRequestedSignal.bind(this);
}
/**
* Get the file path changed signal.
*/
get fileChanged(): ISignal<FileBrowserModel, IChangedArgs<string>> {
return Private.fileChangedSignal.bind(this);
}
/**
* Get the current path.
*
* #### Notes
* This is a read-only property.
*/
get path(): string {
return this._model.path;
}
/**
* Get the selection changed signal.
*/
get selectionChanged(): ISignal<FileBrowserModel, void> {
return Private.selectionChangedSignal.bind(this);
}
/**
* Get whether the view model is disposed.
*/
get isDisposed(): boolean {
return this._model === null;
}
/**
* Get the session ids for active notebooks.
*
* #### Notes
* This is a read-only property.
*/
get sessionIds(): ISessionId[] {
return this._sessionIds.slice();
}
/**
* Get a the list of available kernel specs.
*
* #### Notes
* This is a read-only property.
*/
get kernelSpecs(): IKernelSpecId[] {
return this._kernelSpecs.slice();
}
/**
* Get whether the items are sorted in ascending order.
*/
get sortAscending(): boolean {
return this._ascending;
}
/**
* Set whether the items are sorted in ascending order.
*/
set sortAscending(value: boolean) {
this._ascending = value;
this._sort();
}
/**
* Get which key the items are sorted on.
*/
get sortKey(): string {
return this._sortKey;
}
/**
* Set which key the items are sorted on.
*/
set sortKey(value: string) {
this._sortKey = value;
this._sort();
}
/**
* Get the sorted list of items.
*
* #### Notes
* This is a read-only property and should be treated as immutable.
*/
get sortedItems(): IContentsModel[] {
return this._model.content;
}
/**
* Select an item by name.
*
* #### Notes
* This is a no-op if the name is not valid or already selected.
*/
select(name: string): void {
if (!this._selection[name]) {
this._selection[name] = true;
this.selectionChanged.emit(void 0);
}
}
/**
* De-select an item by name.
*
* #### Notes
* This is a no-op if the name is not valid or not selected.
*/
deselect(name: string): void {
if (this._selection[name]) {
delete this._selection[name];
this.selectionChanged.emit(void 0);
}
}
/**
* Check whether an item is selected.
*
* #### Notes
* Returns `true` for a valid name that is selected, `false` otherwise.
*/
isSelected(name: string): boolean {
return !!this._selection[name];
}
/**
* Get the list of selected names.
*/
getSelected(): string[] {
return Object.keys(this._selection);
}
/**
* Clear the selected items.
*/
clearSelected(): void {
this._selection = Object.create(null);
this.selectionChanged.emit(void 0);
}
/**
* Dispose of the resources held by the view model.
*/
dispose(): void {
this._model = null;
this._contentsManager = null;
this._selection = null;
clearSignalData(this);
}
/**
* Change directory.
*
* @param path - The path to the file or directory.
*
* @returns A promise with the contents of the directory.
*/
cd(path = ''): Promise<void> {
if (path !== '') {
path = Private.normalizePath(this._model.path, path);
}
let changed = false;
let previous = this._selection;
if (path !== this.path) {
previous = Object.create(null);
}
let selection = Object.create(null);
return this._contentsManager.get(path, {}).then(contents => {
this._model = contents;
let content = contents.content as IContentsModel[];
let names = content.map((value, index) => value.name);
for (let name of names) {
if (previous[name]) {
selection[name] = true;
}
}
this._unsortedNames = content.map((value, index) => value.name);
if (this._sortKey !== 'name' || !this._ascending) this._sort();
return this._findSessions();
}).then(() => {
this.selectionChanged.emit(void 0);
this.refreshed.emit(void 0);
});
}
/**
* Refresh the current directory.
*/
refresh(): Promise<void> {
// Refresh the list of kernelspecs and our directory listing.
this._getKernelSpecs();
return this.cd('.');
}
/**
* Copy a file.
*
* @param fromFile - The path of the original file.
*
* @param toDir - The path to the target directory.
*
* @returns A promise which resolves to the contents of the file.
*/
copy(fromFile: string, toDir: string): Promise<IContentsModel> {
let normalizePath = Private.normalizePath;
fromFile = normalizePath(this._model.path, fromFile);
toDir = normalizePath(this._model.path, toDir);
return this._contentsManager.copy(fromFile, toDir);
}
/**
* Delete a file.
*
* @param: path - The path to the file to be deleted.
*
* @returns A promise which resolves when the file is deleted.
*/
delete(path: string): Promise<void> {
let normalizePath = Private.normalizePath;
path = normalizePath(this._model.path, path);
return this._contentsManager.delete(path).then(() => {
this.fileChanged.emit({
name: 'file',
oldValue: path,
newValue: void 0
});
});
}
/**
* Download a file.
*
* @param - path - The path of the file to be downloaded.
*
* @returns - A promise which resolves to the file contents.
*/
download(path: string): Promise<IContentsModel> {
let normalizePath = Private.normalizePath;
path = normalizePath(this._model.path, path);
return this._contentsManager.get(path, {}).then(contents => {
let element = document.createElement('a');
element.setAttribute('href', 'data:text/text;charset=utf-8,' + encodeURI(contents.content));
element.setAttribute('download', contents.name);
element.click();
return contents;
});
}
/**
* Create a new untitled file or directory in the current directory.
*
* @param type - The type of file object to create. One of
* `['file', 'notebook', 'directory']`.
*
* @param ext - Optional extension for `'file'` types (defaults to `'.txt'`).
*
* @returns A promise containing the new file contents model.
*/
newUntitled(type: string, ext?: string): Promise<IContentsModel> {
if (type === 'file') {
ext = ext || '.txt';
} else {
ext = '';
}
return this._contentsManager.newUntitled(this._model.path,
{ type: type, ext: ext }
);
}
/**
* Open a file in the current by name.
*/
open(name: string): Promise<void> {
let item = arrays.find(this.sortedItems, value => value.name === name);
if (!item) {
return Promise.reject(new Error(`No file named: '${name}'`));
}
if (item.type === 'directory') {
return this.cd(name);
} else {
this.openRequested.emit(item);
return Promise.resolve(void 0);
}
}
/**
* Rename a file or directory.
*
* @param path - The path to the original file.
*
* @param newPath - The path to the new file.
*
* @returns A promise containing the new file contents model.
*/
rename(path: string, newPath: string): Promise<IContentsModel> {
// Handle relative paths.
let normalizePath = Private.normalizePath;
path = normalizePath(this._model.path, path);
newPath = normalizePath(this._model.path, newPath);
return this._contentsManager.rename(path, newPath).then(contents => {
this.fileChanged.emit({
name: 'file',
oldValue: path,
newValue: newPath
});
return contents;
})
}
/**
* Upload a `File` object.
*
* @param file - The `File` object to upload.
*
* @param overwrite - Whether to overwrite an existing file.
*
* @returns A promise containing the new file contents model.
*
* #### Notes
* This will fail to upload files that are too big to be sent in one
* request to the server.
*/
upload(file: File, overwrite?: boolean): Promise<IContentsModel> {
// Skip large files with a warning.
if (file.size > this._max_upload_size_mb * 1024 * 1024) {
let msg = `Cannot upload file (>${this._max_upload_size_mb} MB) `;
msg += `"${file.name}"`
console.warn(msg);
return Promise.reject(new Error(msg));
}
if (overwrite) {
return this._upload(file);
}
return this._contentsManager.get(file.name, {}).then(() => {
return Private.typedThrow<IContentsModel>(`"${file.name}" already exists`);
}, () => {
return this._upload(file);
});
}
/**
* Shut down a notebook session by session id.
*/
shutdown(sessionId: ISessionId): Promise<void> {
return this._sessionManager.connectTo(sessionId.id).then(session => {
return session.shutdown();
});
}
/**
* Start a new session on a notebook.
*/
startSession(path: string, kernel: string): Promise<INotebookSession> {
return this._sessionManager.startNew({
notebookPath: path,
kernelName: kernel
});
}
/**
* Sort the model items.
*/
private _sort(): void {
if (!this._unsortedNames) {
return;
}
let items = this._model.content.slice() as IContentsModel[];
if (this._sortKey === 'name') {
items.sort((a, b) => {
let indexA = this._unsortedNames.indexOf(a.name);
let indexB = this._unsortedNames.indexOf(b.name);
return indexA - indexB;
});
} else if (this._sortKey === 'last_modified') {
items.sort((a, b) => {
let valA = new Date(a.last_modified).getTime();
let valB = new Date(b.last_modified).getTime();
return valB - valA;
});
}
// Reverse the order if descending.
if (!this._ascending) {
items.reverse();
}
this._model.content = items;
}
/**
* Perform the actual upload.
*/
private _upload(file: File): Promise<IContentsModel> {
// Gather the file model parameters.
let path = this._model.path
path = path ? path + '/' + file.name : file.name;
let name = file.name;
let isNotebook = file.name.indexOf('.ipynb') !== -1;
let type = isNotebook ? 'notebook' : 'file';
let format = isNotebook ? 'json' : 'base64';
// Get the file content.
let reader = new FileReader();
if (isNotebook) {
reader.readAsText(file);
} else {
reader.readAsArrayBuffer(file);
}
return new Promise<IContentsModel>((resolve, reject) => {
reader.onload = (event: Event) => {
let model: IContentsOpts = {
type: type,
format: format,
name: name,
content: Private.getContent(reader)
}
resolve(this._contentsManager.save(path, model));
}
reader.onerror = (event: Event) => {
reject(Error(`Failed to upload "${file.name}":` + event));
}
});
}
/**
* Get the notebook sessions for the current directory.
*/
private _findSessions(): Promise<void> {
this._sessionIds = [];
let notebooks = this._model.content.filter((content: IContentsModel) => { return content.type === 'notebook'; });
if (!notebooks.length) {
return Promise.resolve(void 0);
}
return this._sessionManager.listRunning().then(sessionIds => {
if (!sessionIds.length) {
return;
}
let promises: Promise<void>[] = [];
let paths = notebooks.map((notebook: IContentsModel) => {
return notebook.path;
});
for (var sessionId of sessionIds) {
let index = paths.indexOf(sessionId.notebook.path);
if (index !== -1) {
promises.push(this._sessionManager.connectTo(sessionId.id).then(session => {
if (session.status === KernelStatus.Idle || session.status === KernelStatus.Idle) {
this._sessionIds.push(sessionId);
return void 0;
}
}));
}
}
return Promise.all(promises).then(() => {});
});
}
/**
* Load the list of kernel specs.
*/
private _getKernelSpecs(): void {
this._sessionManager.getSpecs().then(specs => {
let kernelSpecs: IKernelSpecId[] = [];
for (let key in specs.kernelspecs) {
kernelSpecs.push(specs.kernelspecs[key]);
}
kernelSpecs.sort((a, b) => {
return a.spec.display_name.localeCompare(b.spec.display_name);
});
this._kernelSpecs = kernelSpecs;
}, error => console.error(error));
}
private _max_upload_size_mb = 15;
private _contentsManager: IContentsManager = null;
private _sessionIds: ISessionId[] = [];
private _sessionManager: INotebookSessionManager = null;
private _model: IContentsModel;
private _kernelSpecs: IKernelSpecId[] = [];
private _selection: { [key: string]: boolean; } = Object.create(null);
private _sortKey = 'name';
private _ascending = true;
private _unsortedNames: string[] = [];
}
/**
* The namespace for the file browser model private data.
*/
namespace Private {
/**
* A signal emitted when a model refresh occurs.
*/
export
const refreshedSignal = new Signal<FileBrowserModel, void>();
/**
* A signal emitted when a file open is requested.
*/
export
const openRequestedSignal = new Signal<FileBrowserModel, IContentsModel>();
/**
* A signal emitted when the a file changes path.
*/
export
const fileChangedSignal = new Signal<FileBrowserModel, IChangedArgs<string>>();
/**
* A signal emitted when the selection changes.
*/
export
const selectionChangedSignal = new Signal<FileBrowserModel, void>();
/**
* Parse the content of a `FileReader`.
*
* If the result is an `ArrayBuffer`, return a Base64-encoded string.
* Otherwise, return the JSON parsed result.
*/
export
function getContent(reader: FileReader): any {
if (reader.result instanceof ArrayBuffer) {
// Base64-encode binary file data.
let bytes = '';
let buf = new Uint8Array(reader.result);
let nbytes = buf.byteLength;
for (let i = 0; i < nbytes; i++) {
bytes += String.fromCharCode(buf[i]);
}
return btoa(bytes);
} else {
return JSON.parse(reader.result);
}
}
/**
* Normalize a path based on a root directory, accounting for relative paths.
*/
export
function normalizePath(root: string, path: string): string {
// Current directory
if (path === '.') {
return root;
}
// Root path.
if (path.indexOf('/') === 0) {
path = path.slice(1, path.length);
root = ''
// Current directory.
} else if (path.indexOf('./') === 0) {
path = path.slice(2, path.length);
// Grandparent directory.
} else if (path.indexOf('../../') === 0) {
let parts = root.split('/');
root = parts.splice(0, parts.length - 2).join('/');
path = path.slice(6, path.length);
// Parent directory.
} else if (path.indexOf('../') === 0) {
let parts = root.split('/');
root = parts.splice(0, parts.length - 1).join('/');
path = path.slice(3, path.length);
} else {
// Current directory.
}
if (path[path.length - 1] === '/') {
path = path.slice(0, path.length - 1);
}
// Combine the root and the path if necessary.
if (root && path) {
path = root + '/' + path;
} else if (root) {
path = root;
}
return path;
}
/**
* Work around TS 1.8 type inferencing in promises which only throw.
*/
export
function typedThrow<T>(msg: string): T {
throw new Error(msg);
}
}