jupyterlab-emrys
Version:
A computational environment for Jupyter. Powered by Emrys
146 lines (145 loc) • 4.72 kB
JavaScript
// Copyright (c) Jupyter Development Team.
// Distributed under the terms of the Modified BSD License.
"use strict";
/**
* A composite renderer.
*
* #### Notes
* When rendering a mimebundle, a mimetype is selected from the mimetypes by
* searching through the `this.order` list. The first mimetype found in the bundle
* determines the renderer that will be used.
*
* You can add a renderer by adding it to the `renderers` object and registering
* the mimetype in the `order` array.
*/
var RenderMime = (function () {
/**
* Construct a renderer.
*/
function RenderMime(options) {
this._renderers = Object.create(null);
this._sanitizer = null;
for (var mime in options.renderers) {
this._renderers[mime] = options.renderers[mime];
}
this._order = options.order.slice();
this._sanitizer = options.sanitizer;
this._resolver = options.resolver || null;
}
Object.defineProperty(RenderMime.prototype, "resolver", {
/**
* The object used to resolve relative urls for the rendermime instance.
*/
get: function () {
return this._resolver;
},
set: function (value) {
this._resolver = value;
},
enumerable: true,
configurable: true
});
/**
* Render a mimebundle.
*
* @param bundle - the mimebundle to render.
*
* @param trusted - whether the bundle is trusted.
*/
RenderMime.prototype.render = function (bundle, trusted) {
if (trusted === void 0) { trusted = false; }
var mimetype = this.preferredMimetype(bundle, trusted);
if (!mimetype) {
return void 0;
}
var options = {
mimetype: mimetype,
source: bundle[mimetype],
resolver: this._resolver,
sanitizer: trusted ? null : this._sanitizer
};
return this._renderers[mimetype].render(options);
};
/**
* Find the preferred mimetype in a mimebundle.
*
* @param bundle - the mimebundle giving available mimetype content.
*
* @param trusted - whether the bundle is trusted.
*
* #### Notes
* If the bundle is not trusted, the highest preference
* mimetype that is sanitizable or safe will be chosen.
*/
RenderMime.prototype.preferredMimetype = function (bundle, trusted) {
if (trusted === void 0) { trusted = false; }
for (var _i = 0, _a = this.order; _i < _a.length; _i++) {
var m = _a[_i];
if (m in bundle) {
var renderer = this._renderers[m];
if (trusted || renderer.isSafe(m) || renderer.sanitizable(m)) {
return m;
}
}
}
};
/**
* Clone the rendermime instance with shallow copies of data.
*/
RenderMime.prototype.clone = function () {
return new RenderMime({
renderers: this._renderers,
order: this.order,
sanitizer: this._sanitizer
});
};
/**
* Add a renderer by mimetype.
*
* @param mimetype - The mimetype of the renderer.
* @param renderer - The renderer instance.
* @param index - The optional order index.
*
* ####Notes
* Negative indices count from the end, so -1 refers to the penultimate index.
* Use the index of `.order.length` to add to the end of the render precedence list,
* which would make the new renderer the last choice.
*/
RenderMime.prototype.addRenderer = function (mimetype, renderer, index) {
if (index === void 0) { index = 0; }
this._renderers[mimetype] = renderer;
this._order.splice(index, 0, mimetype);
};
/**
* Remove a renderer by mimetype.
*/
RenderMime.prototype.removeRenderer = function (mimetype) {
delete this._renderers[mimetype];
var index = this._order.indexOf(mimetype);
if (index !== -1) {
this._order.splice(index, 1);
}
};
Object.defineProperty(RenderMime.prototype, "order", {
/**
* Get the ordered list of mimetypes.
*
* #### Notes
* These mimetypes are searched from beginning to end, and the first matching
* mimetype is used.
*/
get: function () {
return this._order.slice();
},
/**
* Set the ordered list of mimetypes.
*/
set: function (value) {
this._order = value.slice();
},
enumerable: true,
configurable: true
});
return RenderMime;
}());
exports.RenderMime = RenderMime;