|
| 1 | +// Copyright (c) Jupyter Development Team. |
| 2 | +// Distributed under the terms of the Modified BSD License. |
| 3 | + |
| 4 | +import * as services from '@jupyterlab/services'; |
| 5 | +import * as Backbone from 'backbone'; |
| 6 | +import * as widgets from '@jupyter-widgets/controls'; |
| 7 | +import * as base from '@jupyter-widgets/base'; |
| 8 | +import * as bqscales from 'bqscales'; |
| 9 | +import * as bqplot from 'bqplot'; |
| 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 base.ManagerBase<HTMLElement> { |
| 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 === 'bqscales') { |
| 97 | + if (bqscales[className]) { |
| 98 | + return Promise.resolve(bqscales[className]); |
| 99 | + } else { |
| 100 | + return Promise.reject(`Cannot find class ${className}`); |
| 101 | + } |
| 102 | + } else if (moduleName === 'bqplot') { |
| 103 | + if (bqplot[className]) { |
| 104 | + return Promise.resolve(bqplot[className]); |
| 105 | + } else { |
| 106 | + return Promise.reject(`Cannot find class ${className}`); |
| 107 | + } |
| 108 | + } else if (moduleName in this.library) { |
| 109 | + return Promise.resolve(this.library[moduleName][className]); |
| 110 | + } else { |
| 111 | + return Promise.reject(`Cannot find module ${moduleName}`); |
| 112 | + } |
| 113 | + } |
| 114 | + |
| 115 | + _get_comm_info() { |
| 116 | + return Promise.resolve({}); |
| 117 | + } |
| 118 | + |
| 119 | + _create_comm() { |
| 120 | + return Promise.resolve(new MockComm()); |
| 121 | + } |
| 122 | + |
| 123 | + el: HTMLElement; |
| 124 | + library: any; |
| 125 | +} |
0 commit comments