| 1 |
// Copyright (c) Jupyter Development Team. |
| 2 |
// Distributed under the terms of the Modified BSD License. |
| 3 |
import { DOMWidgetView } from '@jupyter-widgets/base'; |
| 4 |
import { CoreDOMWidgetModel } from './widget_core'; |
| 5 |
export class ImageModel extends CoreDOMWidgetModel { |
| 6 |
defaults() { |
| 7 |
return Object.assign(Object.assign({}, super.defaults()), { _model_name: 'ImageModel', _view_name: 'ImageView', format: 'png', width: '', height: '', value: new DataView(new ArrayBuffer(0)) }); |
| 8 |
} |
| 9 |
} |
| 10 |
ImageModel.serializers = Object.assign(Object.assign({}, CoreDOMWidgetModel.serializers), { value: { |
| 11 |
serialize: (value) => { |
| 12 |
return new DataView(value.buffer.slice(0)); |
| 13 |
}, |
| 14 |
} }); |
| 15 |
export class ImageView extends DOMWidgetView { |
| 16 |
render() { |
| 17 |
/** |
| 18 |
* Called when view is rendered. |
| 19 |
*/ |
| 20 |
super.render(); |
| 21 |
this.luminoWidget.addClass('jupyter-widgets'); |
| 22 |
this.luminoWidget.addClass('widget-image'); |
| 23 |
this.update(); // Set defaults. |
| 24 |
} |
| 25 |
update() { |
| 26 |
/** |
| 27 |
* Update the contents of this view |
| 28 |
* |
| 29 |
* Called when the model is changed. The model may have been |
| 30 |
* changed by another view or by a state update from the back-end. |
| 31 |
*/ |
| 32 |
let url; |
| 33 |
const format = this.model.get('format'); |
| 34 |
const value = this.model.get('value'); |
| 35 |
if (format !== 'url') { |
| 36 |
const blob = new Blob([value], { |
| 37 |
type: `image/${this.model.get('format')}`, |
| 38 |
}); |
| 39 |
url = URL.createObjectURL(blob); |
| 40 |
} |
| 41 |
else { |
| 42 |
url = new TextDecoder('utf-8').decode(value.buffer); |
| 43 |
} |
| 44 |
// Clean up the old objectURL |
| 45 |
const oldurl = this.el.src; |
| 46 |
this.el.src = url; |
| 47 |
if (oldurl) { |
| 48 |
URL.revokeObjectURL(oldurl); |
| 49 |
} |
| 50 |
const width = this.model.get('width'); |
| 51 |
if (width !== undefined && width.length > 0) { |
| 52 |
this.el.setAttribute('width', width); |
| 53 |
} |
| 54 |
else { |
| 55 |
this.el.removeAttribute('width'); |
| 56 |
} |
| 57 |
const height = this.model.get('height'); |
| 58 |
if (height !== undefined && height.length > 0) { |
| 59 |
this.el.setAttribute('height', height); |
| 60 |
} |
| 61 |
else { |
| 62 |
this.el.removeAttribute('height'); |
| 63 |
} |
| 64 |
return super.update(); |
| 65 |
} |
| 66 |
remove() { |
| 67 |
if (this.el.src) { |
| 68 |
URL.revokeObjectURL(this.el.src); |
| 69 |
} |
| 70 |
super.remove(); |
| 71 |
} |
| 72 |
preinitialize() { |
| 73 |
// Must set this before the initialize method creates the element |
| 74 |
this.tagName = 'img'; |
| 75 |
} |
| 76 |
} |
| 77 |
//# sourceMappingURL=widget_image.js.map |