|
| 1 | +"use strict"; |
| 2 | +Object.defineProperty(exports, "__esModule", { value: true }); |
| 3 | +const application_1 = require("@jupyterlab/application"); |
| 4 | +const coreutils_1 = require("@jupyterlab/coreutils"); |
| 5 | +const notebook_1 = require("@jupyterlab/notebook"); |
| 6 | +const console_1 = require("@jupyterlab/console"); |
| 7 | +const widgets_1 = require("@lumino/widgets"); |
| 8 | +require("../style/index.css"); |
| 9 | +class DashIFrameWidget extends widgets_1.Widget { |
| 10 | + /** |
| 11 | + * Construct a new DashIFrameWidget. |
| 12 | + */ |
| 13 | + constructor(port, url) { |
| 14 | + super(); |
| 15 | + this.id = port; |
| 16 | + this.title.label = `Dash (port: ${port})`; |
| 17 | + this.title.closable = true; |
| 18 | + this.addClass('jp-dashWidget'); |
| 19 | + // Add jp-IFrame class to keep drag events from being lost to the iframe |
| 20 | + // See https://github.com/phosphorjs/phosphor/issues/305 |
| 21 | + // See https://github.com/jupyterlab/jupyterlab/blob/master/packages/apputils/style/iframe.css#L17-L35 |
| 22 | + this.addClass('jp-IFrame'); |
| 23 | + const serviceUrl = url; |
| 24 | + const iframeElement = document.createElement('iframe'); |
| 25 | + iframeElement.setAttribute('baseURI', serviceUrl); |
| 26 | + this.iframe = iframeElement; |
| 27 | + this.iframe.src = serviceUrl; |
| 28 | + this.iframe.id = 'iframe-' + this.id; |
| 29 | + this.node.appendChild(this.iframe); |
| 30 | + } |
| 31 | + /** |
| 32 | + * Handle update requests for the widget. |
| 33 | + */ |
| 34 | + onUpdateRequest(msg) { |
| 35 | + this.iframe.src += ''; |
| 36 | + } |
| 37 | +} |
| 38 | +function activate(app, restorer, notebooks, consoles) { |
| 39 | + // Declare a widget variable |
| 40 | + let widgets = new Map(); |
| 41 | + // Watch notebook creation |
| 42 | + notebooks.widgetAdded.connect((sender, nbPanel) => { |
| 43 | + // const session = nbPanel.session; |
| 44 | + const sessionContext = nbPanel.sessionContext; |
| 45 | + sessionContext.ready.then(() => { |
| 46 | + const session = sessionContext.session; |
| 47 | + let kernel = session.kernel; |
| 48 | + registerCommTarget(kernel, widgets, app); |
| 49 | + }); |
| 50 | + }); |
| 51 | + // Watch console creation |
| 52 | + consoles.widgetAdded.connect((sender, consolePanel) => { |
| 53 | + const sessionContext = consolePanel.sessionContext; |
| 54 | + sessionContext.ready.then(() => { |
| 55 | + const session = sessionContext.session; |
| 56 | + let kernel = session.kernel; |
| 57 | + registerCommTarget(kernel, widgets, app); |
| 58 | + }); |
| 59 | + }); |
| 60 | +} |
| 61 | +function registerCommTarget(kernel, widgets, app) { |
| 62 | + kernel.registerCommTarget('dash', (comm, msg) => { |
| 63 | + comm.onMsg = (msg) => { |
| 64 | + let msgData = msg.content.data; |
| 65 | + if (msgData.type === 'show') { |
| 66 | + let widget; |
| 67 | + if (!widgets.has(msgData.port)) { |
| 68 | + // Create a new widget |
| 69 | + widget = new DashIFrameWidget(msgData.port, msgData.url); |
| 70 | + widget.update(); |
| 71 | + widgets.set(msgData.port, widget); |
| 72 | + // Add instance tracker stuff |
| 73 | + } |
| 74 | + else { |
| 75 | + widget = widgets.get(msgData.port); |
| 76 | + } |
| 77 | + if (!widget.isAttached) { |
| 78 | + // Attach the widget to the main work area |
| 79 | + // if it's not there |
| 80 | + app.shell.add(widget, 'main'); |
| 81 | + widget.update(); |
| 82 | + } |
| 83 | + else { |
| 84 | + // Refresh the widget |
| 85 | + widget.update(); |
| 86 | + } |
| 87 | + // Activate the widget |
| 88 | + app.shell.activateById(widget.id); |
| 89 | + } |
| 90 | + else if (msgData.type === 'base_url_request') { |
| 91 | + // Build server url and base subpath. |
| 92 | + const baseUrl = coreutils_1.PageConfig.getBaseUrl(); |
| 93 | + const baseSubpath = coreutils_1.PageConfig.getOption('baseUrl'); |
| 94 | + const n = baseUrl.lastIndexOf(baseSubpath); |
| 95 | + const serverUrl = baseUrl.slice(0, n); |
| 96 | + comm.send({ |
| 97 | + type: 'base_url_response', |
| 98 | + server_url: serverUrl, |
| 99 | + base_subpath: baseSubpath, |
| 100 | + frontend: "jupyterlab", |
| 101 | + }); |
| 102 | + } |
| 103 | + }; |
| 104 | + }); |
| 105 | +} |
| 106 | +/** |
| 107 | + * Initialization data for the jupyterlab-dash extension. |
| 108 | + */ |
| 109 | +const extension = { |
| 110 | + id: 'jupyterlab_dash', |
| 111 | + autoStart: true, |
| 112 | + requires: [application_1.ILayoutRestorer, notebook_1.INotebookTracker, console_1.IConsoleTracker], |
| 113 | + activate: activate |
| 114 | +}; |
| 115 | +exports.default = extension; |
0 commit comments