Debian Codegraph @

bqplot/js/src/test/dummy-manager.ts

raw
1 // Copyright (c) Jupyter Development Team.
2 // Distributed under the terms of the Modified BSD License.
3
4 //import * as widgets from '../../lib';
5 import * as services from '@jupyterlab/services';
6 import * as Backbone from 'backbone';
7 import * as widgets from '@jupyter-widgets/controls';
8 import * as base from '@jupyter-widgets/base';
9 import * as baseManager from '@jupyter-widgets/base-manager';
10
11 let numComms = 0;
12
13 export class MockComm {
14 constructor() {
15 this.comm_id = `mock-comm-id-${numComms}`;
16 numComms += 1;
17 }
18 on_open(fn) {
19 this._on_open = fn;
20 }
21 on_close(fn) {
22 this._on_close = fn;
23 }
24 on_msg(fn) {
25 this._on_msg = fn;
26 }
27 _process_msg(msg) {
28 if (this._on_msg) {
29 return this._on_msg(msg);
30 } else {
31 return Promise.resolve();
32 }
33 }
34 open() {
35 if (this._on_open) {
36 this._on_open();
37 }
38 return '';
39 }
40 close() {
41 if (this._on_close) {
42 this._on_close();
43 }
44 return '';
45 }
46 send() {
47 return '';
48 }
49 comm_id: string;
50 target_name: string;
51 _on_msg: Function = null;
52 _on_open: Function = null;
53 _on_close: Function = null;
54 }
55
56 export class DummyManager extends baseManager.ManagerBase {
57 constructor(library: any) {
58 super();
59 this.el = window.document.createElement('div');
60 window.document.body.appendChild(this.el);
61 this.library = library;
62 }
63
64 // @ts-ignore
65 display_view(
66 msg: services.KernelMessage.IMessage,
67 view: Backbone.View<Backbone.Model>,
68 options: any
69 ) {
70 return Promise.resolve(view).then((view) => {
71 this.el.appendChild(view.el);
72 view.on('remove', () => console.log('view removed', view));
73 (<any>window).last_view = view;
74 view.trigger('displayed');
75 return view.el;
76 });
77 }
78
79 protected loadClass(
80 className: string,
81 moduleName: string,
82 moduleVersion: string
83 ): Promise<any> {
84 if (moduleName === '@jupyter-widgets/base') {
85 if (base[className]) {
86 return Promise.resolve(base[className]);
87 } else {
88 return Promise.reject(`Cannot find class ${className}`);
89 }
90 } else if (moduleName === '@jupyter-widgets/controls') {
91 if (widgets[className]) {
92 return Promise.resolve(widgets[className]);
93 } else {
94 return Promise.reject(`Cannot find class ${className}`);
95 }
96 } else if (moduleName in this.library) {
97 return Promise.resolve(this.library[moduleName][className]);
98 } else {
99 return Promise.reject(`Cannot find module ${moduleName}`);
100 }
101 }
102
103 _get_comm_info() {
104 return Promise.resolve({});
105 }
106
107 _create_comm() {
108 return Promise.resolve(new MockComm());
109 }
110
111 el: HTMLElement;
112 library: any;
113 }