Debian Codegraph @

jupyter-widgets-controls/lib/widget_video.js

raw
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 VideoModel extends CoreDOMWidgetModel {
6 defaults() {
7 return Object.assign(Object.assign({}, super.defaults()), { _model_name: 'VideoModel', _view_name: 'VideoView', format: 'mp4', width: '', height: '', autoplay: true, loop: true, controls: true, value: new DataView(new ArrayBuffer(0)) });
8 }
9 }
10 VideoModel.serializers = Object.assign(Object.assign({}, CoreDOMWidgetModel.serializers), { value: {
11 serialize: (value) => {
12 return new DataView(value.buffer.slice(0));
13 },
14 } });
15 export class VideoView 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: `video/${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 // Height and width
51 const width = this.model.get('width');
52 if (width !== undefined && width.length > 0) {
53 this.el.setAttribute('width', width);
54 }
55 else {
56 this.el.removeAttribute('width');
57 }
58 const height = this.model.get('height');
59 if (height !== undefined && height.length > 0) {
60 this.el.setAttribute('height', height);
61 }
62 else {
63 this.el.removeAttribute('height');
64 }
65 // Video attributes
66 this.el.loop = this.model.get('loop');
67 this.el.autoplay = this.model.get('autoplay');
68 this.el.controls = this.model.get('controls');
69 return super.update();
70 }
71 remove() {
72 if (this.el.src) {
73 URL.revokeObjectURL(this.el.src);
74 }
75 super.remove();
76 }
77 preinitialize() {
78 // Must set this before the initialize method creates the element
79 this.tagName = 'video';
80 }
81 }
82 //# sourceMappingURL=widget_video.js.map