123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990 |
- // Copyright (c) Jupyter Development Team.
- // Distributed under the terms of the Modified BSD License.
- import {
- IKernel
- } from 'jupyter-js-services';
- import {
- Message
- } from 'phosphor-messaging';
- import {
- Widget
- } from 'phosphor-widget';
- import {
- ABCWidgetFactory, IDocumentModel, IWidgetFactory, IDocumentContext
- } from '../docregistry';
- /**
- * A widget for images.
- */
- export
- class ImageWidget extends Widget {
- /**
- * Create the node for the image widget.
- */
- static createNode(): HTMLElement {
- return document.createElement('img');
- }
- /**
- * Construct a new image widget.
- */
- constructor(context: IDocumentContext<IDocumentModel>) {
- super();
- this._context = context;
- this.node.tabIndex = -1;
- this.node.style.overflowX = 'auto';
- this.node.style.overflowY = 'auto';
- if (context.model.toString()) {
- this.update();
- }
- context.pathChanged.connect(() => {
- this.update();
- });
- context.model.contentChanged.connect(() => {
- this.update();
- });
- }
- /**
- * Dispose of the resources used by the widget.
- */
- dispose(): void {
- if (this.isDisposed) {
- return;
- }
- this._context = null;
- super.dispose();
- }
- /**
- * Handle `update-request` messages for the widget.
- */
- protected onUpdateRequest(msg: Message): void {
- this.title.text = this._context.path.split('/').pop();
- let node = this.node as HTMLImageElement;
- let content = this._context.model.toString();
- let cm = this._context.contentsModel;
- node.src = `data:${cm.mimetype};${cm.format},${content}`;
- }
- private _context: IDocumentContext<IDocumentModel>;
- }
- /**
- * A widget factory for images.
- */
- export
- class ImageWidgetFactory extends ABCWidgetFactory implements IWidgetFactory<ImageWidget, IDocumentModel> {
- /**
- * Create a new widget given a context.
- */
- createNew(context: IDocumentContext<IDocumentModel>, kernel?: IKernel.IModel): ImageWidget {
- return new ImageWidget(context);
- }
- }
|