From 70918d33eefd9c636c37e81856c1770c0b6db31c Mon Sep 17 00:00:00 2001 From: Jason Grout Date: Wed, 26 Apr 2017 17:15:50 -0400 Subject: [PATCH] Make the widget manager totally responsible for loading a class. --- .../examples/web3/src/manager.ts | 12 ++++ .../examples/web5/src/manager.ts | 12 ++++ jupyter-js-widgets/src-embed/embed-manager.ts | 29 +++++---- jupyter-js-widgets/src-embed/embed-webpack.ts | 55 ++++++++--------- jupyter-js-widgets/src/manager-base.ts | 53 +++++++--------- jupyter-js-widgets/src/utils.ts | 60 ------------------- jupyter-js-widgets/test/src/dummy-manager.ts | 20 +++++-- jupyterlab_widgets/src/index.ts | 4 +- widgetsnbextension/src/manager.js | 12 +++- 9 files changed, 118 insertions(+), 139 deletions(-) diff --git a/jupyter-js-widgets/examples/web3/src/manager.ts b/jupyter-js-widgets/examples/web3/src/manager.ts index 381f5e1c23..eb2cbac235 100644 --- a/jupyter-js-widgets/examples/web3/src/manager.ts +++ b/jupyter-js-widgets/examples/web3/src/manager.ts @@ -28,6 +28,18 @@ class WidgetManager extends widgets.ManagerBase { }); } + protected loadClass(className: string, moduleName: string, moduleVersion: string): Promise { + if (moduleName === 'jupyter-js-widgets') { + if (widgets[className]) { + return Promise.resolve(widgets[className]); + } else { + return Promise.reject(`Cannot find class ${className}`) + } + } else { + return Promise.reject(`Cannot find module ${moduleName}`); + } + } + _create_comm(targetName, id, metadata) { return this.commManager.new_comm(targetName, metadata, id); } diff --git a/jupyter-js-widgets/examples/web5/src/manager.ts b/jupyter-js-widgets/examples/web5/src/manager.ts index ad16b327d0..13f99d264c 100644 --- a/jupyter-js-widgets/examples/web5/src/manager.ts +++ b/jupyter-js-widgets/examples/web5/src/manager.ts @@ -27,6 +27,18 @@ class WidgetManager extends widgets.ManagerBase { }); } + protected loadClass(className: string, moduleName: string, moduleVersion: string): Promise { + if (moduleName === 'jupyter-js-widgets') { + if (widgets[className]) { + return Promise.resolve(widgets[className]); + } else { + return Promise.reject(`Cannot find class ${className}`) + } + } else { + return Promise.reject(`Cannot find module ${moduleName}`); + } + } + _create_comm(targetName, id, metadata) { return this.commManager.new_comm(targetName, metadata, id); } diff --git a/jupyter-js-widgets/src-embed/embed-manager.ts b/jupyter-js-widgets/src-embed/embed-manager.ts index 386c9f056c..bbe7a4c52c 100644 --- a/jupyter-js-widgets/src-embed/embed-manager.ts +++ b/jupyter-js-widgets/src-embed/embed-manager.ts @@ -7,6 +7,8 @@ import { import * as PhosphorWidget from '@phosphor/widgets'; +import * as widgets from '../../jupyter-js-widgets/lib/index'; + export class EmbedManager extends ManagerBase { @@ -43,18 +45,25 @@ class EmbedManager extends ManagerBase { }; /** - * Takes a requirejs success handler and returns a requirejs error handler - * that attempts loading the module from unpkg. + * Load a class and return a promise to the loaded object. */ - require_error(success_callback, failure_callback, version : string) { - return function(err) : any { - var failedId = err.requireModules && err.requireModules[0]; - if (failedId) { - // TODO: Get typing to work for requirejs - (window as any).require(['https://unpkg.com/' + failedId + '@' + version + '/dist/index.js'], success_callback); + protected loadClass(className: string, moduleName: string, moduleVersion: string) { + return new Promise(function(resolve, reject) { + if (moduleName === 'jupyter-js-widgets') { + // Shortcut resolving the standard widgets so we don't load two + // copies on the page. If we ever separate the embed manager + // from the main widget package, we should get rid of this special + // case. + resolve(widgets); + } else { + (window as any).require([`https://unpkg.com/${moduleName}@${moduleVersion}/dist/index.js`], resolve, reject); + } + }).then(function(module) { + if (module[className]) { + return module[className]; } else { - failure_callback(err); + return Promise.reject(`Class ${className} not found in module ${moduleName}@${moduleVersion}`); } - }; + }); } }; diff --git a/jupyter-js-widgets/src-embed/embed-webpack.ts b/jupyter-js-widgets/src-embed/embed-webpack.ts index 2351920cb3..06dce09df8 100644 --- a/jupyter-js-widgets/src-embed/embed-webpack.ts +++ b/jupyter-js-widgets/src-embed/embed-webpack.ts @@ -6,7 +6,7 @@ // Element.prototype.matches polyfill if (Element && !Element.prototype.matches) { - var proto = Element.prototype as any; + let proto = Element.prototype as any; proto.matches = proto.matchesSelector || proto.mozMatchesSelector || proto.msMatchesSelector || proto.oMatchesSelector || proto.webkitMatchesSelector; @@ -31,22 +31,19 @@ import * as _ from 'underscore'; // All it does is inserting a