forked from jupyterlab/jupyterlab
/
uploadstatus.tsx
237 lines (207 loc) · 5.64 KB
/
uploadstatus.tsx
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
// Copyright (c) Jupyter Development Team.
// Distributed under the terms of the Modified BSD License.
//
import { VDomRenderer, VDomModel, WidgetTracker } from '@jupyterlab/apputils';
import { IChangedArgs } from '@jupyterlab/coreutils';
import { GroupItem, ProgressBar, TextItem } from '@jupyterlab/statusbar';
import { ArrayExt } from '@lumino/algorithm';
import { IUploadModel, FileBrowserModel, FileBrowser } from '.';
import React from 'react';
/**
* Half-spacing between items in the overall status item.
*/
const HALF_SPACING = 4;
/**
* A pure function component for a FileUpload status item.
*
* @param props: the props for the component.
*
* @returns a tsx component for the file upload status.
*/
function FileUploadComponent(
props: FileUploadComponent.IProps
): React.ReactElement<FileUploadComponent.IProps> {
return (
<GroupItem spacing={HALF_SPACING}>
<TextItem source={'Uploading…'} />
<ProgressBar percentage={props.upload} />
</GroupItem>
);
}
/**
* A namespace for FileUploadComponent statics.
*/
namespace FileUploadComponent {
/**
* The props for the FileUploadComponent.
*/
export interface IProps {
/**
* The current upload percentage, from 0 to 100.
*/
upload: number;
}
}
/**
* The time for which to show the "Complete!" message after uploading.
*/
const UPLOAD_COMPLETE_MESSAGE_MILLIS: number = 2000;
/**
* Status bar item to display file upload progress.
*/
export class FileUploadStatus extends VDomRenderer<FileUploadStatus.Model> {
/**
* Construct a new FileUpload status item.
*/
constructor(opts: FileUploadStatus.IOptions) {
super(
new FileUploadStatus.Model(
opts.tracker.currentWidget && opts.tracker.currentWidget.model
)
);
this._tracker = opts.tracker;
this._tracker.currentChanged.connect(this._onBrowserChange);
}
/**
* Render the FileUpload status.
*/
render() {
const uploadPaths = this.model!.items;
if (uploadPaths.length > 0) {
const item = this.model!.items[0];
if (item.complete) {
return <TextItem source="Complete!" />;
} else {
return <FileUploadComponent upload={this.model!.items[0].progress} />;
}
} else {
return <FileUploadComponent upload={100} />;
}
}
dispose() {
super.dispose();
this._tracker.currentChanged.disconnect(this._onBrowserChange);
}
private _onBrowserChange = (
tracker: WidgetTracker<FileBrowser>,
browser: FileBrowser | null
) => {
if (browser === null) {
this.model!.browserModel = null;
} else {
this.model!.browserModel = browser.model;
}
};
private _tracker: WidgetTracker<FileBrowser>;
}
/**
* A namespace for FileUpload class statics.
*/
export namespace FileUploadStatus {
/**
* The VDomModel for the FileUpload renderer.
*/
export class Model extends VDomModel {
/**
* Construct a new model.
*/
constructor(browserModel: FileBrowserModel | null) {
super();
this.browserModel = browserModel;
}
/**
* The currently uploading items.
*/
get items() {
return this._items;
}
/**
* The current file browser model.
*/
get browserModel(): FileBrowserModel | null {
return this._browserModel;
}
set browserModel(browserModel: FileBrowserModel | null) {
const oldBrowserModel = this._browserModel;
if (oldBrowserModel) {
oldBrowserModel.uploadChanged.disconnect(this._uploadChanged);
}
this._browserModel = browserModel;
this._items = [];
if (this._browserModel !== null) {
this._browserModel.uploadChanged.connect(this._uploadChanged);
}
this.stateChanged.emit(void 0);
}
/**
* Handle an uploadChanged event in the filebrowser model.
*/
private _uploadChanged = (
browse: FileBrowserModel,
uploads: IChangedArgs<IUploadModel>
) => {
if (uploads.name === 'start') {
this._items.push({
path: uploads.newValue.path,
progress: uploads.newValue.progress * 100,
complete: false
});
} else if (uploads.name === 'update') {
const idx = ArrayExt.findFirstIndex(
this._items,
val => val.path === uploads.oldValue.path
);
if (idx !== -1) {
this._items[idx].progress = uploads.newValue.progress * 100;
}
} else if (uploads.name === 'finish') {
const idx = ArrayExt.findFirstIndex(
this._items,
val => val.path === uploads.oldValue.path
);
if (idx !== -1) {
this._items[idx].complete = true;
setTimeout(() => {
ArrayExt.removeAt(this._items, idx);
this.stateChanged.emit(void 0);
}, UPLOAD_COMPLETE_MESSAGE_MILLIS);
}
} else if (uploads.name === 'failure') {
ArrayExt.removeFirstWhere(
this._items,
val => val.path === uploads.newValue.path
);
}
this.stateChanged.emit(void 0);
};
private _items: Array<IFileUploadItem> = [];
private _browserModel: FileBrowserModel | null = null;
}
/**
* Options for creating the upload status item.
*/
export interface IOptions {
/**
* The application file browser tracker.
*/
readonly tracker: WidgetTracker<FileBrowser>;
}
}
/**
* The interface for an item that is being uploaded to
* the file system.
*/
interface IFileUploadItem {
/**
* The path on the filesystem that is being uploaded to.
*/
path: string;
/**
* The upload progress fraction.
*/
progress: number;
/**
* Whether the upload is complete.
*/
complete: boolean;
}