Bladeren bron

Update api to allow overrides

Steven Silvester 8 jaren geleden
bovenliggende
commit
1f7ab02bb1

+ 5 - 1
src/csvwidget/plugin.ts

@@ -30,5 +30,9 @@ const csvHandlerExtension: JupyterLabPlugin<void> = {
  * Activate the table widget extension.
  */
 function activateCSVWidget(app: JupyterLab, registry: IDocumentRegistry): void {
-  registry.addWidgetFactory(new CSVWidgetFactory());
+  registry.addWidgetFactory(new CSVWidgetFactory({
+    name: 'Table',
+    fileExtensions: ['.csv'],
+    defaultFor: ['.csv']
+  }));
 }

+ 29 - 9
src/docregistry/default.ts

@@ -278,6 +278,18 @@ class Base64ModelFactory extends TextModelFactory {
  */
 export
 abstract class ABCWidgetFactory<T extends Widget, U extends DocumentRegistry.IModel> implements DocumentRegistry.IWidgetFactory<T, U> {
+  /**
+   * Construct a new `ABCWidgetFactory`.
+   */
+  constructor(options: DocumentRegistry.IWidgetFactoryOptions) {
+    this._name = options.name;
+    this._defaultFor = options.defaultFor ? options.defaultFor.slice() : [];
+    this._fileExtensions = options.fileExtensions.slice();
+    this._modelName = options.modelName || 'text';
+    this._preferKernel = !!options.preferKernel;
+    this._canStartKernel = !!options.canStartKernel;
+  }
+
   /**
    * A signal emitted when a widget is created.
    */
@@ -298,43 +310,45 @@ abstract class ABCWidgetFactory<T extends Widget, U extends DocumentRegistry.IMo
   }
 
   /**
-   * The file extensions the widget can view.
+   * The name of the widget to display in dialogs.
    */
-  get fileExtensions(): string[] {
-    return [];
+  get name(): string {
+    return this._name;
   }
 
   /**
-   * The name of the widget to display in dialogs.
+   * The file extensions the widget can view.
    */
-  abstract get name(): string;
+  get fileExtensions(): string[] {
+    return this._fileExtensions.slice();
+  }
 
   /**
    * The registered name of the model type used to create the widgets.
    */
   get modelName(): string {
-    return 'text';
+    return this._modelName;
   }
 
   /**
    * The file extensions for which the factory should be the default.
    */
   get defaultFor(): string[] {
-    return [];
+    return this._defaultFor.slice();
   }
 
   /**
    * Whether the widgets prefer having a kernel started.
    */
   get preferKernel(): boolean {
-    return false;
+    return this._preferKernel;
   }
 
   /**
    * Whether the widgets can start a kernel when opened.
    */
   get canStartKernel(): boolean {
-    return false;
+    return this._canStartKernel;
   }
 
   /**
@@ -346,6 +360,12 @@ abstract class ABCWidgetFactory<T extends Widget, U extends DocumentRegistry.IMo
   abstract createNew(context: DocumentRegistry.IContext<U>, kernel?: Kernel.IModel): T;
 
   private _isDisposed = false;
+  private _name: string;
+  private _canStartKernel: boolean;
+  private _preferKernel: boolean;
+  private _modelName: string;
+  private _fileExtensions: string[];
+  private _defaultFor: string[];
 }
 
 

+ 33 - 32
src/docregistry/registry.ts

@@ -6,7 +6,7 @@ import {
 } from '@jupyterlab/services';
 
 import {
-  EmptyIterator, IIterator, each, iter
+  EmptyIterator, IIterator, each, map
 } from 'phosphor/lib/algorithm/iteration';
 
 import {
@@ -294,11 +294,11 @@ class DocumentRegistry {
   }
 
   /**
-   * Get an iterator over the preferred widget factories.
+   * Get a list of the preferred widget factories.
    *
    * @param ext - An optional file extension to filter the results.
    *
-   * @returns A new iterator of widget factories.
+   * @returns A new array of widget factories.
    *
    * #### Notes
    * Only the widget factories whose associated model factory have
@@ -310,7 +310,7 @@ class DocumentRegistry {
    * - all other extension-specific factories
    * - all other global factories
    */
-  preferredWidgetFactories(ext: string = '*'): IIterator<DocumentRegistry.IWidgetFactory<Widget, DocumentRegistry.IModel>> {
+  preferredWidgetFactories(ext: string = '*'): DocumentRegistry.IWidgetFactory<Widget, DocumentRegistry.IModel>[] {
     let factories = new Set<string>();
     ext = Private.normalizeExtension(ext);
 
@@ -351,7 +351,7 @@ class DocumentRegistry {
       }
     });
 
-    return iter(factoryList);
+    return factoryList;
   }
 
   /**
@@ -365,7 +365,7 @@ class DocumentRegistry {
    * This is equivalent to the first value in [[preferredWidgetFactories]].
    */
   defaultWidgetFactory(ext: string = '*'): DocumentRegistry.IWidgetFactory<Widget, DocumentRegistry.IModel> {
-    return this.preferredWidgetFactories(ext).next();
+    return this.preferredWidgetFactories(ext)[0];
   }
 
   /**
@@ -374,11 +374,9 @@ class DocumentRegistry {
    * @returns A new iterator of widget factories.
    */
   getWidgetFactories(): IIterator<DocumentRegistry.IWidgetFactory<Widget, DocumentRegistry.IModel>> {
-    let factories: DocumentRegistry.IWidgetFactory<Widget, DocumentRegistry.IModel>[] = [];
-    for (let name in this._widgetFactories) {
-      factories.push(this._widgetFactories[name]);
-    }
-    return iter(factories);
+    return map(Object.keys(this._widgetFactories), name => {
+      return this._widgetFactories[name];
+    });
   }
 
   /**
@@ -387,11 +385,9 @@ class DocumentRegistry {
    * @returns A new iterator of model factories.
    */
   getModelFactories(): IIterator<DocumentRegistry.IModelFactory<DocumentRegistry.IModel>> {
-    let factories: DocumentRegistry.IModelFactory<DocumentRegistry.IModel>[] = [];
-    for (let name in this._modelFactories) {
-      factories.push(this._modelFactories[name]);
-    }
-    return iter(factories);
+    return map(Object.keys(this._modelFactories), name => {
+      return this._modelFactories[name];
+    });
   }
 
   /**
@@ -726,15 +722,10 @@ namespace DocumentRegistry {
   }
 
   /**
-   * The interface for a widget factory.
+   * The options used to initialize a widget factory.
    */
   export
-  interface IWidgetFactory<T extends Widget, U extends IModel> extends IDisposable {
-    /**
-     * A signal emitted when a widget is created.
-     */
-    widgetCreated: ISignal<IWidgetFactory<T, U>, T>;
-
+  interface IWidgetFactoryOptions {
     /**
      * The file extensions the widget can view.
      *
@@ -749,11 +740,6 @@ namespace DocumentRegistry {
      */
     readonly name: string;
 
-    /**
-     * The registered name of the model type used to create the widgets.
-     */
-    readonly modelName: string;
-
     /**
      * The file extensions for which the factory should be the default.
      *
@@ -765,17 +751,33 @@ namespace DocumentRegistry {
      *
      * **See also:** [[fileExtensions]].
      */
-    readonly defaultFor: string[];
+    readonly defaultFor?: string[];
+
+    /**
+     * The registered name of the model type used to create the widgets.
+     */
+    readonly modelName?: string;
 
     /**
      * Whether the widgets prefer having a kernel started.
      */
-    readonly preferKernel: boolean;
+    readonly preferKernel?: boolean;
 
     /**
      * Whether the widgets can start a kernel when opened.
      */
-    readonly canStartKernel: boolean;
+    readonly canStartKernel?: boolean;
+  }
+
+  /**
+   * The interface for a widget factory.
+   */
+  export
+  interface IWidgetFactory<T extends Widget, U extends IModel> extends IDisposable, IWidgetFactoryOptions {
+    /**
+     * A signal emitted when a widget is created.
+     */
+    widgetCreated: ISignal<IWidgetFactory<T, U>, T>;
 
     /**
      * Create a new widget.
@@ -786,7 +788,6 @@ namespace DocumentRegistry {
     createNew(context: IContext<U>, kernel?: Kernel.IModel): T;
   }
 
-
   /**
    * An interface for a widget extension.
    */

+ 5 - 1
src/editorwidget/plugin.ts

@@ -94,7 +94,11 @@ const editorHandlerProvider: JupyterLabPlugin<IEditorTracker> = {
  * Sets up the editor widget
  */
 function activateEditorHandler(app: JupyterLab, registry: IDocumentRegistry, mainMenu: IMainMenu, palette: ICommandPalette): IEditorTracker {
-  let widgetFactory = new EditorWidgetFactory();
+  let widgetFactory = new EditorWidgetFactory({
+    name: 'Editor',
+    fileExtensions: ['*'],
+    defaultFor: ['*']
+  });
 
   // Sync tracker with currently focused widget.
   app.shell.currentChanged.connect((sender, args) => {

+ 0 - 21
src/editorwidget/widget.ts

@@ -128,27 +128,6 @@ class EditorWidget extends CodeMirrorWidget {
  */
 export
 class EditorWidgetFactory extends ABCWidgetFactory<EditorWidget, DocumentRegistry.IModel> {
-  /**
-   * The name of the widget to display in dialogs.
-   */
-  get name(): string {
-    return 'Editor';
-  }
-
-  /**
-   * The file extensions the widget can view.
-   */
-  get fileExtensions(): string[] {
-    return ['*'];
-  }
-
-  /**
-   * The file extensions for which the factory should be the default.
-   */
-  get defaultFor(): string[] {
-    return ['*'];
-  }
-
   /**
    * Create a new widget given a context.
    */

+ 12 - 1
src/imagewidget/plugin.ts

@@ -27,6 +27,12 @@ import {
  */
 const tracker = new InstanceTracker<ImageWidget>();
 
+/**
+ * The list of file extensions for images.
+ */
+const EXTENSIONS = ['.png', '.gif', '.jpeg', '.jpg', '.svg', '.bmp', '.ico',
+  '.xbm', '.tiff', '.tif'];
+
 
 /**
  * The image file handler extension.
@@ -47,7 +53,12 @@ function activateImageWidget(app: JupyterLab, registry: IDocumentRegistry, palet
   let zoomInImage = 'image-widget:zoom-in';
   let zoomOutImage = 'image-widget:zoom-out';
   let resetZoomImage = 'image-widget:reset-zoom';
-  let image = new ImageWidgetFactory();
+  let image = new ImageWidgetFactory({
+    name: 'Image',
+    modelName: 'base64',
+    fileExtensions: EXTENSIONS,
+    defaultFor: EXTENSIONS
+  });
 
   // Sync tracker with currently focused widget.
   app.shell.currentChanged.connect((sender, args) => {

+ 0 - 34
src/imagewidget/widget.ts

@@ -22,12 +22,6 @@ import {
  */
 const IMAGE_CLASS = 'jp-ImageWidget';
 
-/**
- * The list of file extensions for images.
- */
-const EXTENSIONS = ['.png', '.gif', '.jpeg', '.jpg', '.svg', '.bmp', '.ico',
-  '.xbm', '.tiff', '.tif'];
-
 
 /**
  * A widget for images.
@@ -111,34 +105,6 @@ class ImageWidget extends Widget {
  */
 export
 class ImageWidgetFactory extends ABCWidgetFactory<ImageWidget, DocumentRegistry.IModel> {
-  /**
-   * The name of the widget to display in dialogs.
-   */
-  get name(): string {
-    return 'Image';
-  }
-
-  /**
-   * The registered name of the model type used to create the widgets.
-   */
-  get modelName(): string {
-    return 'base64';
-  }
-
-  /**
-   * The file extensions the widget can view.
-   */
-  get fileExtensions(): string[] {
-    return EXTENSIONS;
-  }
-
-  /**
-   * The file extensions for which the factory should be the default.
-   */
-  get defaultFor(): string[] {
-    return EXTENSIONS;
-  }
-
   /**
    * Create a new widget given a context.
    */

+ 5 - 2
src/markdownwidget/plugin.ts

@@ -6,7 +6,7 @@ import {
 } from '../application';
 
 import {
-  DocumentRegistry, IDocumentRegistry
+  IDocumentRegistry
 } from '../docregistry';
 
 import {
@@ -37,7 +37,10 @@ const markdownHandlerExtension: JupyterLabPlugin<void> = {
   id: 'jupyter.extensions.rendered-markdown',
   requires: [IDocumentRegistry, IRenderMime],
   activate: (app: JupyterLab, registry: IDocumentRegistry, rendermime: IRenderMime) => {
-    let factory = new MarkdownWidgetFactory(rendermime);
+    let factory = new MarkdownWidgetFactory({
+      name: 'Rendered Markdown',
+      fileExtensions: ['.md']
+    }, rendermime);
     let icon = `${PORTRAIT_ICON_CLASS} ${TEXTEDITOR_ICON_CLASS}`;
     factory.widgetCreated.connect((sender, widget) => {
       widget.title.icon = icon;

+ 2 - 16
src/markdownwidget/widget.ts

@@ -114,25 +114,11 @@ class MarkdownWidgetFactory extends ABCWidgetFactory<MarkdownWidget, DocumentReg
   /**
    * Construct a new markdown widget factory.
    */
-  constructor(rendermime: RenderMime) {
-    super();
+  constructor(options: DocumentRegistry.IWidgetFactoryOptions, rendermime: RenderMime) {
+    super(options);
     this._rendermime = rendermime;
   }
 
-  /**
-   * The name of the widget to display in dialogs.
-   */
-  get name(): string {
-    return 'Rendered Markdown';
-  }
-
-  /**
-   * The file extensions the widget can view.
-   */
-  get fileExtensions(): string[] {
-    return ['.md'];
-  }
-
   /**
    * Create a new widget given a context.
    */

+ 34 - 52
src/notebook/notebook/widgetfactory.ts

@@ -38,59 +38,13 @@ class NotebookWidgetFactory extends ABCWidgetFactory<NotebookPanel, INotebookMod
   /**
    * Construct a new notebook widget factory.
    *
-   * @param rendermime - The rendermime instance.
-   *
-   * @param clipboard - The application clipboard.
-   *
-   * @param renderer - The notebook panel renderer.
-   */
-  constructor(rendermime: RenderMime, clipboard: IClipboard, renderer: NotebookPanel.IRenderer) {
-    super();
-    this._rendermime = rendermime;
-    this._clipboard = clipboard;
-    this._renderer = renderer;
-  }
-
-  /**
-   * The name of the widget to display in dialogs.
-   */
-  get name(): string {
-    return 'Notebook';
-  }
-
-  /**
-   * The registered name of the model type used to create the widgets.
-   */
-  get modelName(): string {
-    return 'notebook';
-  }
-
-  /**
-   * The file extensions the widget can view.
-   */
-  get fileExtensions(): string[] {
-    return ['.ipynb'];
-  }
-
-  /**
-   * The file extensions for which the factory should be the default.
+   * @param options - The options used to construct the factory.
    */
-  get defaultFor(): string[] {
-    return ['.ipynb'];
-  }
-
-  /**
-   * Whether the widgets prefer having a kernel started.
-   */
-  get preferKernel(): boolean {
-    return true;
-  }
-
-  /**
-   * Whether the widgets can start a kernel when opened.
-   */
-  get canStartKernel(): boolean {
-    return true;
+  constructor(options: NotebookWidgetFactory.IOptions) {
+    super(options);
+    this._rendermime = options.rendermime;
+    this._clipboard = options.clipboard;
+    this._renderer = options.renderer;
   }
 
   /**
@@ -134,3 +88,31 @@ class NotebookWidgetFactory extends ABCWidgetFactory<NotebookPanel, INotebookMod
   private _clipboard: IClipboard = null;
   private _renderer: NotebookPanel.IRenderer = null;
 }
+
+
+/**
+ * The namespace for `NotebookWidgetFactory` statics.
+ */
+export
+namespace NotebookWidgetFactory {
+  /**
+   * The options used to construct a `NotebookWidgetFactory`.
+   */
+  export
+  interface IOptions extends DocumentRegistry.IWidgetFactoryOptions {
+     /*
+      * A rendermime instance.
+      */
+    rendermime: RenderMime;
+
+    /**
+     * A clipboard instance.
+     */
+    clipboard: IClipboard;
+
+    /**
+     * A notebook panel renderer.
+     */
+    renderer: NotebookPanel.IRenderer;
+  }
+}

+ 12 - 2
src/notebook/plugin.ts

@@ -22,7 +22,7 @@ import {
 } from '../mainmenu';
 
 import {
-  IDocumentRegistry, DocumentRegistry,
+  IDocumentRegistry,
   restartKernel, selectKernelForContext
 } from '../docregistry';
 
@@ -132,7 +132,17 @@ const notebookTrackerProvider: JupyterLabPlugin<INotebookTracker> = {
  * Activate the notebook handler extension.
  */
 function activateNotebookHandler(app: JupyterLab, registry: IDocumentRegistry, services: IServiceManager, rendermime: IRenderMime, clipboard: IClipboard, mainMenu: IMainMenu, palette: ICommandPalette, inspector: IInspector, renderer: NotebookPanel.IRenderer): INotebookTracker {
-  let widgetFactory = new NotebookWidgetFactory(rendermime, clipboard, renderer);
+  let widgetFactory = new NotebookWidgetFactory({
+    name: 'Notebook',
+    fileExtensions: ['.ipynb'],
+    modelName: 'notebook',
+    defaultFor: ['.ipynb'],
+    preferKernel: true,
+    canStartKernel: true,
+    rendermime,
+    clipboard,
+    renderer
+  });
 
   // Sync tracker and set the source of the code inspector.
   app.shell.currentChanged.connect((sender, args) => {

+ 1 - 0
src/tsconfig.json

@@ -8,6 +8,7 @@
     "module": "commonjs",
     "moduleResolution": "node",
     "target": "ES5",
+    "types": [],
     "outDir": "../lib",
     "sourceMap": true
   },