1 |
|
2 |
|
3 | import { showErrorMessage, ToolbarButton } from '@jupyterlab/apputils';
|
4 | import { nullTranslator } from '@jupyterlab/translation';
|
5 | import { fileUploadIcon } from '@jupyterlab/ui-components';
|
6 |
|
7 |
|
8 |
|
9 | export class Uploader extends ToolbarButton {
|
10 | |
11 |
|
12 |
|
13 | constructor(options) {
|
14 | super({
|
15 | icon: fileUploadIcon,
|
16 | onClick: () => {
|
17 | this._input.click();
|
18 | },
|
19 | tooltip: Private.translateToolTip(options.translator)
|
20 | });
|
21 | |
22 |
|
23 |
|
24 | this._onInputChanged = () => {
|
25 | const files = Array.prototype.slice.call(this._input.files);
|
26 | const pending = files.map(file => this.fileBrowserModel.upload(file));
|
27 | void Promise.all(pending).catch(error => {
|
28 | void showErrorMessage(this._trans._p('showErrorMessage', 'Upload Error'), error);
|
29 | });
|
30 | };
|
31 | |
32 |
|
33 |
|
34 | this._onInputClicked = () => {
|
35 |
|
36 |
|
37 | this._input.value = '';
|
38 | };
|
39 | this._input = Private.createUploadInput();
|
40 | this.fileBrowserModel = options.model;
|
41 | this.translator = options.translator || nullTranslator;
|
42 | this._trans = this.translator.load('jupyterlab');
|
43 | this._input.onclick = this._onInputClicked;
|
44 | this._input.onchange = this._onInputChanged;
|
45 | this.addClass('jp-id-upload');
|
46 | }
|
47 | }
|
48 |
|
49 |
|
50 |
|
51 | var Private;
|
52 | (function (Private) {
|
53 | |
54 |
|
55 |
|
56 | function createUploadInput() {
|
57 | const input = document.createElement('input');
|
58 | input.type = 'file';
|
59 | input.multiple = true;
|
60 | return input;
|
61 | }
|
62 | Private.createUploadInput = createUploadInput;
|
63 | |
64 |
|
65 |
|
66 | function translateToolTip(translator) {
|
67 | translator = translator || nullTranslator;
|
68 | const trans = translator.load('jupyterlab');
|
69 | return trans.__('Upload Files');
|
70 | }
|
71 | Private.translateToolTip = translateToolTip;
|
72 | })(Private || (Private = {}));
|
73 |
|
\ | No newline at end of file |