Debian Codegraph @

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