forked from jupyterlab/jupyterlab
-
Notifications
You must be signed in to change notification settings - Fork 0
/
upload.ts
127 lines (109 loc) · 2.83 KB
/
upload.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
// Copyright (c) Jupyter Development Team.
// Distributed under the terms of the Modified BSD License.
import {
ToolbarButton, showErrorMessage
} from '@jupyterlab/apputils';
import {
FileBrowserModel
} from './model';
/**
* The class name added to a button content node.
*/
const CONTENT_CLASS = 'jp-FileButtons-buttonContent';
/**
* The class name added to a button icon node.
*/
const ICON_CLASS = 'jp-FileButtons-buttonIcon';
/**
* The class name added to the upload button.
*/
const MATERIAL_UPLOAD = 'jp-UploadIcon';
/**
* The class name added to a material icon button.
*/
const MATERIAL_CLASS = 'jp-MaterialIcon';
/**
* The class name added to the upload button.
*/
const UPLOAD_CLASS = 'jp-id-upload';
/**
* A widget which provides an upload button.
*/
export
class Uploader extends ToolbarButton {
/**
* Construct a new file browser buttons widget.
*/
constructor(options: Uploader.IOptions) {
super({
className: UPLOAD_CLASS,
onClick: () => {
this._input.click();
},
tooltip: 'Upload Files'
});
let uploadContent = document.createElement('span');
let uploadIcon = document.createElement('span');
uploadContent.className = CONTENT_CLASS;
uploadIcon.className = ICON_CLASS + ' ' + MATERIAL_CLASS + ' ' + MATERIAL_UPLOAD;
uploadContent.appendChild(uploadIcon);
this.node.appendChild(uploadContent);
this.model = options.model;
this._input.onclick = this._onInputClicked;
this._input.onchange = this._onInputChanged;
}
/**
* The underlying file browser model for the widget.
*/
readonly model: FileBrowserModel;
/**
* The 'change' handler for the input field.
*/
private _onInputChanged = () => {
let files = Array.prototype.slice.call(this._input.files) as File[];
let pending = files.map(file => this.model.upload(file));
Promise.all(pending).catch(error => {
showErrorMessage('Upload Error', error);
});
}
/**
* The 'click' handler for the input field.
*/
private _onInputClicked = () => {
// In order to allow repeated uploads of the same file (with delete in between),
// we need to clear the input value to trigger a change event.
this._input.value = '';
}
private _input = Private.createUploadInput();
}
/**
* The namespace for Uploader class statics.
*/
export
namespace Uploader {
/**
* The options used to create an uploader.
*/
export
interface IOptions {
/**
* A file browser model instance.
*/
model: FileBrowserModel;
}
}
/**
* The namespace for module private data.
*/
namespace Private {
/**
* Create the upload input node for a file buttons widget.
*/
export
function createUploadInput(): HTMLInputElement {
let input = document.createElement('input');
input.type = 'file';
input.multiple = true;
return input;
}
}