|
| 1 | +// Copyright (c) Jupyter Development Team. |
| 2 | +// Distributed under the terms of the Modified BSD License. |
| 3 | + |
| 4 | +import { |
| 5 | + DOMWidgetView |
| 6 | +} from '@jupyter-widgets/base'; |
| 7 | + |
| 8 | +import { |
| 9 | + CoreDOMWidgetModel |
| 10 | +} from './widget_core'; |
| 11 | + |
| 12 | +import * as _ from 'underscore'; |
| 13 | + |
| 14 | +export |
| 15 | +class UploadModel extends CoreDOMWidgetModel { |
| 16 | + defaults() { |
| 17 | + return _.extend(super.defaults(), { |
| 18 | + _model_name: 'UploadModel', |
| 19 | + _view_name: 'UploadView', |
| 20 | + values: [], |
| 21 | + _values_base64: [], |
| 22 | + accept: '', |
| 23 | + multiple: false |
| 24 | + }); |
| 25 | + } |
| 26 | +} |
| 27 | + |
| 28 | +export |
| 29 | +class UploadView extends DOMWidgetView { |
| 30 | + render() { |
| 31 | + /** |
| 32 | + * Called when view is rendered. |
| 33 | + */ |
| 34 | + super.render(); |
| 35 | + this.pWidget.addClass('jupyter-widgets'); |
| 36 | + this.el.classList.add('widget-inline-hbox'); |
| 37 | + this.pWidget.addClass('widget-upload'); |
| 38 | + |
| 39 | + this.upload_container = document.createElement('input'); |
| 40 | + this.upload_container.setAttribute('type', 'file'); |
| 41 | + this.handleUploadChanged = this.handleUploadChanged.bind(this); |
| 42 | + this.upload_container.onchange = this.handleUploadChanged; |
| 43 | + |
| 44 | + this.el.appendChild(this.upload_container); |
| 45 | + |
| 46 | + |
| 47 | + this.listenTo(this.model, 'change:values', this._update_values); |
| 48 | + this._update_values(); |
| 49 | + this.update(); // Set defaults. |
| 50 | + } |
| 51 | + |
| 52 | + _update_values() { |
| 53 | + // Only allow for value to clear. This is a rule enforced by browsers |
| 54 | + const values = this.model.get('values'); |
| 55 | + if (values.length === 0) { |
| 56 | + this.upload_container.value = ''; |
| 57 | + } |
| 58 | + } |
| 59 | + |
| 60 | + update(options?) { |
| 61 | + /** |
| 62 | + * Update the contents of this view |
| 63 | + * |
| 64 | + * Called when the model is changed. The model may have been |
| 65 | + * changed by another view or by a state update from the back-end. |
| 66 | + */ |
| 67 | + if (options === undefined || options.updated_view !== this) { |
| 68 | + this.upload_container.disabled = this.model.get('disabled'); |
| 69 | + this.upload_container.setAttribute('accept', this.model.get('accept')); |
| 70 | + if (this.model.get('multiple')) { |
| 71 | + this.upload_container.setAttribute('multiple', 'true'); |
| 72 | + } else { |
| 73 | + this.upload_container.removeAttribute('multiple'); |
| 74 | + } |
| 75 | + } |
| 76 | + return super.update(options); |
| 77 | + } |
| 78 | + |
| 79 | + handleUploadChanged(event) { |
| 80 | + const that = this; |
| 81 | + const {files} = event.target; |
| 82 | + if (!files || files.length === 0) { |
| 83 | + that.model.set('_values_base64', []); |
| 84 | + that.touch(); |
| 85 | + return; |
| 86 | + } |
| 87 | + |
| 88 | + const fileContentsPromises = []; |
| 89 | + for (let file of files) { // files it NOT an array |
| 90 | + fileContentsPromises.push(new Promise((resolve, reject) => { |
| 91 | + const fileReader = new FileReader(); |
| 92 | + fileReader.onload = function fileReaderOnload() { |
| 93 | + resolve({ |
| 94 | + name: file.name, |
| 95 | + contents: fileReader.result, |
| 96 | + type: file.type, |
| 97 | + lastModified: file.lastModified |
| 98 | + }); |
| 99 | + }; |
| 100 | + fileReader.readAsDataURL(file); |
| 101 | + })); |
| 102 | + } |
| 103 | + Promise.all(fileContentsPromises) |
| 104 | + .then((contents) => { |
| 105 | + that.model.set('_values_base64', contents); |
| 106 | + that.touch(); |
| 107 | + }) |
| 108 | + .catch((err) => { |
| 109 | + console.error('FileUploadView Error loading files: %o', err); |
| 110 | + // FIXME: Set state? |
| 111 | + }); |
| 112 | + // FIXME: Add an icon that shows it loading? |
| 113 | + } |
| 114 | + |
| 115 | + upload_container: HTMLInputElement; |
| 116 | + model: UploadModel; |
| 117 | +} |
0 commit comments