| 1 |
// Copyright (c) Jupyter Development Team. |
| 2 |
// Distributed under the terms of the Modified BSD License. |
| 3 |
import * as libembed from './libembed'; |
| 4 |
/** |
| 5 |
* Load a package using requirejs and return a promise |
| 6 |
* |
| 7 |
* @param pkg Package name or names to load |
| 8 |
*/ |
| 9 |
const requirePromise = function (pkg) { |
| 10 |
return new Promise((resolve, reject) => { |
| 11 |
const require = window.requirejs; |
| 12 |
if (require === undefined) { |
| 13 |
reject('Requirejs is needed, please ensure it is loaded on the page.'); |
| 14 |
} |
| 15 |
else { |
| 16 |
require(pkg, resolve, reject); |
| 17 |
} |
| 18 |
}); |
| 19 |
}; |
| 20 |
let cdn = 'https://cdn.jsdelivr.net/npm/'; |
| 21 |
let onlyCDN = false; |
| 22 |
// find the data-cdn for any script tag, assuming it is only used for embed-amd.js |
| 23 |
const scripts = document.getElementsByTagName('script'); |
| 24 |
Array.prototype.forEach.call(scripts, (script) => { |
| 25 |
cdn = script.getAttribute('data-jupyter-widgets-cdn') || cdn; |
| 26 |
onlyCDN = onlyCDN || script.hasAttribute('data-jupyter-widgets-cdn-only'); |
| 27 |
}); |
| 28 |
function moduleNameToCDNUrl(moduleName, moduleVersion) { |
| 29 |
let packageName = moduleName; |
| 30 |
let fileName = 'index'; // default filename |
| 31 |
// if a '/' is present, like 'foo/bar', packageName is changed to 'foo', and path to 'bar' |
| 32 |
// We first find the first '/' |
| 33 |
let index = moduleName.indexOf('/'); |
| 34 |
if (index != -1 && moduleName[0] == '@') { |
| 35 |
// if we have a namespace, it's a different story |
| 36 |
// @foo/bar/baz should translate to @foo/bar and baz |
| 37 |
// so we find the 2nd '/' |
| 38 |
index = moduleName.indexOf('/', index + 1); |
| 39 |
} |
| 40 |
if (index != -1) { |
| 41 |
fileName = moduleName.substr(index + 1); |
| 42 |
packageName = moduleName.substr(0, index); |
| 43 |
} |
| 44 |
return `${cdn}${packageName}@${moduleVersion}/dist/${fileName}`; |
| 45 |
} |
| 46 |
/** |
| 47 |
* Load an amd module locally and fall back to specified CDN if unavailable. |
| 48 |
* |
| 49 |
* @param moduleName The name of the module to load.. |
| 50 |
* @param version The semver range for the module, if loaded from a CDN. |
| 51 |
* |
| 52 |
* By default, the CDN service used is jsDelivr. However, this default can be |
| 53 |
* overridden by specifying another URL via the HTML attribute |
| 54 |
* "data-jupyter-widgets-cdn" on a script tag of the page. |
| 55 |
* |
| 56 |
* The semver range is only used with the CDN. |
| 57 |
*/ |
| 58 |
export function requireLoader(moduleName, moduleVersion) { |
| 59 |
const require = window.requirejs; |
| 60 |
if (require === undefined) { |
| 61 |
throw new Error('Requirejs is needed, please ensure it is loaded on the page.'); |
| 62 |
} |
| 63 |
function loadFromCDN() { |
| 64 |
const conf = { paths: {} }; |
| 65 |
conf.paths[moduleName] = moduleNameToCDNUrl(moduleName, moduleVersion); |
| 66 |
require.config(conf); |
| 67 |
return requirePromise([`${moduleName}`]); |
| 68 |
} |
| 69 |
if (onlyCDN) { |
| 70 |
console.log(`Loading from ${cdn} for ${moduleName}@${moduleVersion}`); |
| 71 |
return loadFromCDN(); |
| 72 |
} |
| 73 |
return requirePromise([`${moduleName}`]).catch((err) => { |
| 74 |
const failedId = err.requireModules && err.requireModules[0]; |
| 75 |
if (failedId) { |
| 76 |
require.undef(failedId); |
| 77 |
console.log(`Falling back to ${cdn} for ${moduleName}@${moduleVersion}`); |
| 78 |
return loadFromCDN(); |
| 79 |
} |
| 80 |
}); |
| 81 |
} |
| 82 |
/** |
| 83 |
* Render widgets in a given element. |
| 84 |
* |
| 85 |
* @param element (default document.documentElement) The element containing widget state and views. |
| 86 |
* @param loader (default requireLoader) The function used to look up the modules containing |
| 87 |
* the widgets' models and views classes. (The default loader looks them up on jsDelivr) |
| 88 |
*/ |
| 89 |
export function renderWidgets(element = document.documentElement, loader = requireLoader) { |
| 90 |
requirePromise(['@jupyter-widgets/html-manager']).then((htmlmanager) => { |
| 91 |
const managerFactory = () => { |
| 92 |
return new htmlmanager.HTMLManager({ loader: loader }); |
| 93 |
}; |
| 94 |
libembed.renderWidgets(managerFactory, element); |
| 95 |
}); |
| 96 |
} |
| 97 |
//# sourceMappingURL=libembed-amd.js.map |