Skip to content

Commit

Permalink
Merge pull request #282 from SylvainCorlay/drop-html-loader
Browse files Browse the repository at this point in the history
Drop HTML manager
  • Loading branch information
SylvainCorlay authored Jul 3, 2019
2 parents e4217a8 + d523014 commit 977d209
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 44 deletions.
41 changes: 0 additions & 41 deletions js/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 1 addition & 2 deletions js/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,8 @@
"main": "lib/index.js",
"browserslist": ">0.8%, not ie 11, not op_mini all, not dead",
"dependencies": {
"@jupyter-widgets/base": "^1.2.5 || ^2.0.1",
"@jupyter-widgets/base": "^2.0.1",
"@jupyter-widgets/controls": "^1.5.0",
"@jupyter-widgets/html-manager": "^0.18.1",
"@jupyter-widgets/jupyterlab-manager": "^1.0.0",
"@jupyter-widgets/output": "^2.0.0",
"@jupyterlab/coreutils": "^3.0.0",
Expand Down
68 changes: 68 additions & 0 deletions js/src/loader.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
let cdn = 'https://unpkg.com/';

/**
* Load a package using requirejs and return a promise
*
* @param pkg Package name or names to load
*/
let requirePromise = function(pkg) {
return new Promise((resolve, reject) => {
let require =window.requirejs;
if (require === undefined) {
reject("Requirejs is needed, please ensure it is loaded on the page.");
} else {
require(pkg, resolve, reject);
}
});
}

function moduleNameToCDNUrl(moduleName, moduleVersion) {
let packageName = moduleName;
let fileName = 'index'; // default filename
// if a '/' is present, like 'foo/bar', packageName is changed to 'foo', and path to 'bar'
// We first find the first '/'
let index = moduleName.indexOf('/');
if ((index != -1) && (moduleName[0] == '@')) {
// if we have a namespace, it's a different story
// @foo/bar/baz should translate to @foo/bar and baz
// so we find the 2nd '/'
index = moduleName.indexOf('/', index+1);
}
if (index != -1) {
fileName = moduleName.substr(index+1);
packageName = moduleName.substr(0, index);
}
return `${cdn}${packageName}@${moduleVersion}/dist/${fileName}`;
}

/**
* Load an amd module locally and fall back to specified CDN if unavailable.
*
* @param moduleName The name of the module to load..
* @param version The semver range for the module, if loaded from a CDN.
*
* By default, the CDN service used is unpkg.com. However, this default can be
* overriden by specifying another URL via the HTML attribute
* "data-jupyter-widgets-cdn" on a script tag of the page.
*
* The semver range is only used with the CDN.
*/
export
function requireLoader(moduleName, moduleVersion) {
return requirePromise([`${moduleName}`]).catch((err) => {
let failedId = err.requireModules && err.requireModules[0];
if (failedId) {
console.log(`Falling back to ${cdn} for ${moduleName}@${moduleVersion}`);
let require = window.requirejs;
if (require === undefined) {
throw new Error("Requirejs is needed, please ensure it is loaded on the page.");
}
const conf = {paths: {}};
conf.paths[moduleName] = moduleNameToCDNUrl(moduleName, moduleVersion);
require.undef(failedId);
require.config(conf);

return requirePromise([`${moduleName}`]);
}
});
}
3 changes: 2 additions & 1 deletion js/src/manager.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,15 @@
****************************************************************************/

import { RenderMimeRegistry, standardRendererFactories } from '@jupyterlab/rendermime';
import { requireLoader } from '@jupyter-widgets/html-manager';
import { WidgetManager as JupyterLabManager } from '@jupyter-widgets/jupyterlab-manager';
import { WidgetRenderer } from '@jupyter-widgets/jupyterlab-manager';
import { output } from '@jupyter-widgets/jupyterlab-manager';
import * as base from '@jupyter-widgets/base';
import * as controls from '@jupyter-widgets/controls';
import * as PhosphorWidget from '@phosphor/widgets';

import { requireLoader } from './loader';

if (typeof window !== "undefined" && typeof window.define !== "undefined") {
window.define("@jupyter-widgets/base", base);
window.define("@jupyter-widgets/controls", controls);
Expand Down

0 comments on commit 977d209

Please sign in to comment.