| 1 |
// Copyright (c) Jupyter Development Team. |
| 2 |
// Distributed under the terms of the Modified BSD License. |
| 3 |
import { CoreDOMWidgetModel } from './widget_core'; |
| 4 |
import { DOMWidgetView } from '@jupyter-widgets/base'; |
| 5 |
export class FileUploadModel extends CoreDOMWidgetModel { |
| 6 |
defaults() { |
| 7 |
return Object.assign(Object.assign({}, super.defaults()), { _model_name: 'FileUploadModel', _view_name: 'FileUploadView', accept: '', description: 'Upload', disabled: false, icon: 'upload', button_style: '', multiple: false, value: [], error: '', style: null }); |
| 8 |
} |
| 9 |
} |
| 10 |
FileUploadModel.serializers = Object.assign(Object.assign({}, CoreDOMWidgetModel.serializers), { |
| 11 |
// use a dummy serializer for value to circumvent the default serializer. |
| 12 |
value: { serialize: (x) => x } }); |
| 13 |
export class FileUploadView extends DOMWidgetView { |
| 14 |
preinitialize() { |
| 15 |
// Must set this before the initialize method creates the element |
| 16 |
this.tagName = 'button'; |
| 17 |
} |
| 18 |
render() { |
| 19 |
super.render(); |
| 20 |
this.el.classList.add('jupyter-widgets'); |
| 21 |
this.el.classList.add('widget-upload'); |
| 22 |
this.el.classList.add('jupyter-button'); |
| 23 |
this.fileInput = document.createElement('input'); |
| 24 |
this.fileInput.type = 'file'; |
| 25 |
this.fileInput.style.display = 'none'; |
| 26 |
this.el.addEventListener('click', () => { |
| 27 |
this.fileInput.click(); |
| 28 |
}); |
| 29 |
this.fileInput.addEventListener('click', () => { |
| 30 |
this.fileInput.value = ''; |
| 31 |
}); |
| 32 |
this.fileInput.addEventListener('change', () => { |
| 33 |
var _a; |
| 34 |
const promisesFile = []; |
| 35 |
Array.from((_a = this.fileInput.files) !== null && _a !== void 0 ? _a : []).forEach((file) => { |
| 36 |
promisesFile.push(new Promise((resolve, reject) => { |
| 37 |
const fileReader = new FileReader(); |
| 38 |
fileReader.onload = () => { |
| 39 |
// We know we can read the result as an array buffer since |
| 40 |
// we use the `.readAsArrayBuffer` method |
| 41 |
const content = fileReader.result; |
| 42 |
resolve({ |
| 43 |
content, |
| 44 |
name: file.name, |
| 45 |
type: file.type, |
| 46 |
size: file.size, |
| 47 |
last_modified: file.lastModified, |
| 48 |
}); |
| 49 |
}; |
| 50 |
fileReader.onerror = () => { |
| 51 |
reject(); |
| 52 |
}; |
| 53 |
fileReader.onabort = fileReader.onerror; |
| 54 |
fileReader.readAsArrayBuffer(file); |
| 55 |
})); |
| 56 |
}); |
| 57 |
Promise.all(promisesFile) |
| 58 |
.then((files) => { |
| 59 |
this.model.set({ |
| 60 |
value: files, |
| 61 |
error: '', |
| 62 |
}); |
| 63 |
this.touch(); |
| 64 |
}) |
| 65 |
.catch((err) => { |
| 66 |
console.error('error in file upload: %o', err); |
| 67 |
this.model.set({ |
| 68 |
error: err, |
| 69 |
}); |
| 70 |
this.touch(); |
| 71 |
}); |
| 72 |
}); |
| 73 |
this.listenTo(this.model, 'change:button_style', this.update_button_style); |
| 74 |
this.set_button_style(); |
| 75 |
this.update(); // Set defaults. |
| 76 |
} |
| 77 |
update() { |
| 78 |
this.el.disabled = this.model.get('disabled'); |
| 79 |
this.el.setAttribute('title', this.model.get('tooltip')); |
| 80 |
const value = this.model.get('value'); |
| 81 |
const description = `${this.model.get('description')} (${value.length})`; |
| 82 |
const icon = this.model.get('icon'); |
| 83 |
if (description.length || icon.length) { |
| 84 |
this.el.textContent = ''; |
| 85 |
if (icon.length) { |
| 86 |
const i = document.createElement('i'); |
| 87 |
i.classList.add('fa'); |
| 88 |
i.classList.add('fa-' + icon); |
| 89 |
if (description.length === 0) { |
| 90 |
i.classList.add('center'); |
| 91 |
} |
| 92 |
this.el.appendChild(i); |
| 93 |
} |
| 94 |
this.el.appendChild(document.createTextNode(description)); |
| 95 |
} |
| 96 |
this.fileInput.accept = this.model.get('accept'); |
| 97 |
this.fileInput.multiple = this.model.get('multiple'); |
| 98 |
return super.update(); |
| 99 |
} |
| 100 |
update_button_style() { |
| 101 |
this.update_mapped_classes(FileUploadView.class_map, 'button_style', this.el); |
| 102 |
} |
| 103 |
set_button_style() { |
| 104 |
this.set_mapped_classes(FileUploadView.class_map, 'button_style', this.el); |
| 105 |
} |
| 106 |
} |
| 107 |
FileUploadView.class_map = { |
| 108 |
primary: ['mod-primary'], |
| 109 |
success: ['mod-success'], |
| 110 |
info: ['mod-info'], |
| 111 |
warning: ['mod-warning'], |
| 112 |
danger: ['mod-danger'], |
| 113 |
}; |
| 114 |
//# sourceMappingURL=widget_upload.js.map |