UNPKG

2.6 kBJavaScriptView Raw
1// Copyright (c) Jupyter Development Team.
2// Distributed under the terms of the Modified BSD License.
3import { showErrorMessage, ToolbarButton } from '@jupyterlab/apputils';
4import { nullTranslator } from '@jupyterlab/translation';
5import { fileUploadIcon } from '@jupyterlab/ui-components';
6/**
7 * A widget which provides an upload button.
8 */
9export class Uploader extends ToolbarButton {
10 /**
11 * Construct a new file browser buttons widget.
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 * The 'change' handler for the input field.
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 * The 'click' handler for the input field.
33 */
34 this._onInputClicked = () => {
35 // In order to allow repeated uploads of the same file (with delete in between),
36 // we need to clear the input value to trigger a change event.
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 * The namespace for module private data.
50 */
51var Private;
52(function (Private) {
53 /**
54 * Create the upload input node for a file buttons widget.
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 * Translate upload tooltip.
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//# sourceMappingURL=upload.js.map
\No newline at end of file