Browse Source

Finish refactoring to use constructor options

Steven Silvester 9 years ago
parent
commit
d9ce99cc1f

+ 14 - 0
src/notebook/cells/model.ts

@@ -108,6 +108,20 @@ interface ICodeCellModel extends ICellModel {
 }
 
 
+/**
+ * The definition of a markdown cell.
+ */
+export
+interface IMarkdownCellModel extends ICellModel { }
+
+
+/**
+ * The definition of a raw cell.
+ */
+export
+interface IRawCellModel extends ICellModel { }
+
+
 /**
  * An implementation of the cell model.
  */

+ 15 - 15
src/notebook/notebook/actions.ts

@@ -143,7 +143,7 @@ namespace NotebookActions {
       }
     }
     if (!model.cells.length) {
-      let cell = model.createCodeCell();
+      let cell = model.cellFactory.newCodeCell();
       model.cells.add(cell);
     }
     model.cells.endCompoundOperation();
@@ -155,7 +155,7 @@ namespace NotebookActions {
    */
   export
   function insertAbove(widget: Notebook): void {
-    let cell = widget.model.createCodeCell();
+    let cell = widget.model.cellFactory.newCodeCell();
     widget.model.cells.insert(widget.activeCellIndex, cell);
     Private.deselectCells(widget);
   }
@@ -165,7 +165,7 @@ namespace NotebookActions {
    */
   export
   function insertBelow(widget: Notebook): void {
-    let cell = widget.model.createCodeCell();
+    let cell = widget.model.cellFactory.newCodeCell();
     widget.model.cells.insert(widget.activeCellIndex + 1, cell);
     Private.deselectCells(widget);
   }
@@ -188,13 +188,13 @@ namespace NotebookActions {
       let newCell: ICellModel;
       switch (value) {
       case 'code':
-        newCell = model.createCodeCell(child.model.toJSON());
+        newCell = model.cellFactory.newCodeCell(child.model.toJSON());
         break;
       case 'markdown':
-        newCell = model.createMarkdownCell(child.model.toJSON());
+        newCell = model.cellFactory.newMarkdownCell(child.model.toJSON());
         break;
       default:
-        newCell = model.createRawCell(child.model.toJSON());
+        newCell = model.cellFactory.newRawCell(child.model.toJSON());
       }
       model.cells.replace(i, 1, [newCell]);
       if (value === 'markdown') {
@@ -242,7 +242,7 @@ namespace NotebookActions {
     run(widget, kernel);
     let model = widget.model;
     if (widget.activeCellIndex === widget.childCount() - 1) {
-      let cell = model.createCodeCell();
+      let cell = model.cellFactory.newCodeCell();
       model.cells.add(cell);
       widget.mode = 'edit';
     } else {
@@ -259,7 +259,7 @@ namespace NotebookActions {
   function runAndInsert(widget: Notebook, kernel?: IKernel): void {
     run(widget, kernel);
     let model = widget.model;
-    let cell = model.createCodeCell();
+    let cell = model.cellFactory.newCodeCell();
     model.cells.insert(widget.activeCellIndex + 1, cell);
     widget.activeCellIndex++;
     widget.mode = 'edit';
@@ -396,7 +396,7 @@ namespace NotebookActions {
       }
     }
     if (!model.cells.length) {
-      let cell = model.createCodeCell();
+      let cell = model.cellFactory.newCodeCell();
       model.cells.add(cell);
     }
     model.cells.endCompoundOperation();
@@ -418,13 +418,13 @@ namespace NotebookActions {
     for (let value of values) {
       switch (value.cell_type) {
       case 'code':
-        cells.push(model.createCodeCell(value));
+        cells.push(model.cellFactory.newCodeCell(value));
         break;
       case 'markdown':
-        cells.push(model.createMarkdownCell(value));
+        cells.push(model.cellFactory.newMarkdownCell(value));
         break;
       default:
-        cells.push(model.createRawCell(value));
+        cells.push(model.cellFactory.newRawCell(value));
         break;
       }
     }
@@ -538,11 +538,11 @@ namespace Private {
   function cloneCell(model: INotebookModel, cell: ICellModel): ICellModel {
     switch (cell.type) {
     case 'code':
-      return model.createCodeCell(cell.toJSON());
+      return model.cellFactory.newCodeCell(cell.toJSON());
     case 'markdown':
-      return model.createMarkdownCell(cell.toJSON());
+      return model.cellFactory.newMarkdownCell(cell.toJSON());
     default:
-      return model.createRawCell(cell.toJSON());
+      return model.cellFactory.newRawCell(cell.toJSON());
     }
   }
 

+ 52 - 92
src/notebook/notebook/model.ts

@@ -21,7 +21,8 @@ import {
 } from '../../docregistry';
 
 import {
-  ICellModel, CodeCellModel, RawCellModel, MarkdownCellModel
+  ICellModel, ICodeCellModel, IRawCellModel, IMarkdownCellModel,
+  CodeCellModel, RawCellModel, MarkdownCellModel
 } from '../cells/model';
 
 import {
@@ -64,6 +65,11 @@ interface INotebookModel extends IDocumentModel {
    */
   cells: ObservableUndoableList<ICellModel>;
 
+  /**
+   * The factory for creating new cell models.
+   */
+  cellFactory: ICellModelFactory;
+
   /**
    * The major version number of the nbformat.
    *
@@ -96,36 +102,43 @@ interface INotebookModel extends IDocumentModel {
    * Metadata associated with the nbformat are not included.
    */
   listMetadata(): string[];
+}
 
+
+/**
+ * A factory for creating cell models.
+ */
+export
+interface ICellModelFactory {
   /**
-   * A factory for creating a new code cell.
+   * Create a new code cell.
    *
    * @param source - The data to use for the original source data.
    *
    * @returns A new code cell. If a source cell is provided, the
    *   new cell will be intialized with the data from the source.
    */
-  createCodeCell(source?: nbformat.IBaseCell): CodeCellModel;
+  newCodeCell(source?: nbformat.IBaseCell): ICodeCellModel;
 
   /**
-   * A factory for creating a new Markdown cell.
+   * Create a new markdown cell.
    *
    * @param source - The data to use for the original source data.
    *
    * @returns A new markdown cell. If a source cell is provided, the
    *   new cell will be intialized with the data from the source.
    */
-  createMarkdownCell(source?: nbformat.IBaseCell): MarkdownCellModel;
+  newMarkdownCell(source?: nbformat.IBaseCell): IMarkdownCellModel;
 
   /**
-   * A factory for creating a new raw cell.
+   * Create a new raw cell.
    *
    * @param source - The data to use for the original source data.
    *
    * @returns A new raw cell. If a source cell is provided, the
    *   new cell will be intialized with the data from the source.
    */
-  createRawCell(source?: nbformat.IBaseCell): RawCellModel;
+  newRawCell(source?: nbformat.IBaseCell): IRawCellModel;
 }
 
 
@@ -139,25 +152,19 @@ class NotebookModel extends DocumentModel implements INotebookModel {
    */
   constructor(options: NotebookModel.IOptions = {}) {
     super(options.languagePreference);
-    this._codeFactory = options.codeCellFactory || Private.createCodeCell;
-    this._mdFactory = options.markdownCellFactory || Private.createMarkdownCell;
-    this._rawFactory = options.rawCellFactory || Private.createRawCell;
-    let codeFactory = this._codeFactory;
+    this._factory = options.cellModelFactory || Private.defaultFactory;
     this._cells = new ObservableUndoableList<ICellModel>((data: nbformat.IBaseCell) => {
       switch (data.cell_type) {
         case 'code':
-          return codeFactory(data);
+          return this._factory.newCodeCell(data);
         case 'markdown':
-          let markdownFactory = this._mdFactory;
-          return markdownFactory(data);
+          return this._factory.newMarkdownCell(data);
         default:
-          let rawFactory = this._rawFactory;
-          return rawFactory(data);
+          return this._factory.newRawCell(data);
       }
     });
     // Add an initial code cell by default.
-    let cell = codeFactory();
-    this._cells.add(cell);
+    this._cells.add(this._factory.newCodeCell());
     this._cells.changed.connect(this._onCellsChanged, this);
     if (options.languagePreference) {
       this._metadata['language_info'] = { name: options.languagePreference };
@@ -223,6 +230,16 @@ class NotebookModel extends DocumentModel implements INotebookModel {
     return info ? info.name : '';
   }
 
+  /**
+   * Get the factory used to create cell models.
+   *
+   * #### Notes
+   * This is a read-only property.
+   */
+  get cellFactory(): ICellModelFactory {
+    return this._factory;
+  }
+
   /**
    * Dispose of the resources held by the model.
    */
@@ -381,45 +398,6 @@ class NotebookModel extends DocumentModel implements INotebookModel {
     return Object.keys(this._metadata);
   }
 
-  /**
-   * A factory for creating a new code cell.
-   *
-   * @param source - The data to use for the original source data.
-   *
-   * @returns A new code cell. If a source cell is provided, the
-   *   new cell will be intialized with the data from the source.
-   */
-  createCodeCell(source?: nbformat.IBaseCell): CodeCellModel {
-    let factory = this._codeFactory;
-    return factory(source);
-  }
-
-  /**
-   * A factory for creating a new Markdown cell.
-   *
-   * @param source - The data to use for the original source data.
-   *
-   * @returns A new markdown cell. If a source cell is provided, the
-   *   new cell will be intialized with the data from the source.
-   */
-  createMarkdownCell(source?: nbformat.IBaseCell): MarkdownCellModel {
-    let factory = this._mdFactory;
-    return factory(source);
-  }
-
-  /**
-   * A factory for creating a new raw cell.
-   *
-   * @param source - The data to use for the original source data.
-   *
-   * @returns A new raw cell. If a source cell is provided, the
-   *   new cell will be intialized with the data from the source.
-   */
-  createRawCell(source?: nbformat.IBaseCell): RawCellModel {
-    let factory = this._rawFactory;
-    return factory(source);
-  }
-
   /**
    * Set the cursor data for a given field.
    */
@@ -480,13 +458,11 @@ class NotebookModel extends DocumentModel implements INotebookModel {
   }
 
   private _cells: ObservableUndoableList<ICellModel> = null;
+  private _factory: ICellModelFactory = null;
   private _metadata: { [key: string]: any } = Private.createMetadata();
   private _cursors: { [key: string]: MetadataCursor } = Object.create(null);
   private _nbformat = nbformat.MAJOR_VERSION;
   private _nbformatMinor = nbformat.MINOR_VERSION;
-  private _codeFactory: (source?: nbformat.IBaseCell) => CodeCellModel = null;
-  private _rawFactory: (source?: nbformat.IBaseCell) => RawCellModel = null;
-  private _mdFactory: (source?: nbformat.IBaseCell) => MarkdownCellModel = null;
 }
 
 
@@ -506,19 +482,11 @@ namespace NotebookModel {
     languagePreference?: string;
 
     /**
-     * A factory for creating code cell models.
-     */
-    codeCellFactory?: (source?: nbformat.IBaseCell) => CodeCellModel;
-
-    /**
-     * A factory for creating markdown cell models.
+     * A factory for creating cell models.
+     *
+     * The default is a shared factory instance.
      */
-    markdownCellFactory?: (source?: nbformat.IBaseCell) => MarkdownCellModel;
-
-    /**
-     * A factory for creating raw cell models.
-     */
-    rawCellFactory?: (source?: nbformat.IBaseCell) => RawCellModel;
+    cellModelFactory?: ICellModelFactory;
   }
 }
 
@@ -534,28 +502,20 @@ namespace Private {
   const metadataChangedSignal = new Signal<IDocumentModel, IChangedArgs<any>>();
 
   /**
-   * A factory for creating a new code cell.
-   */
-  export
-  function createCodeCell(source?: nbformat.IBaseCell): CodeCellModel {
-    return new CodeCellModel(source);
-  }
-
-  /**
-   * A factory for creating a new Markdown cell.
+   * The default `ICellModelFactory` instance.
    */
   export
-  function createMarkdownCell(source?: nbformat.IBaseCell): MarkdownCellModel {
-    return new MarkdownCellModel(source);
-  }
-
-  /**
-   * A factory for creating a new raw cell.
-   */
-  export
-  function createRawCell(source?: nbformat.IBaseCell): RawCellModel {
-    return new RawCellModel(source);
-  }
+  const defaultFactory: ICellModelFactory = {
+    newCodeCell: (source?: nbformat.IBaseCell) => {
+      return new CodeCellModel(source);
+    },
+    newMarkdownCell: (source?: nbformat.IBaseCell) => {
+      return new MarkdownCellModel(source);
+    },
+    newRawCell: (source?: nbformat.IBaseCell) => {
+      return new RawCellModel(source);
+    }
+  };
 
   /**
    * Create the default metadata for the notebook.

+ 1 - 4
src/notebook/notebook/modelfactory.ts

@@ -61,10 +61,7 @@ class NotebookModelFactory implements IModelFactory {
    * @returns A new document model.
    */
   createNew(languagePreference?: string): INotebookModel {
-    let model = new NotebookModel(languagePreference);
-    let cell = model.createCodeCell();
-    model.cells.add(cell);
-    return model;
+    return new NotebookModel(languagePreference);
   }
 
   /**

+ 1 - 1
src/notebook/notebook/panel.ts

@@ -83,7 +83,7 @@ class NotebookPanel extends Widget {
    * Create a new content area for the notebook.
    */
   static createContent(model: INotebookModel, rendermime: RenderMime<Widget>): Notebook {
-    let widget = new Notebook(rendermime);
+    let widget = new Notebook({ rendermime });
     widget.model = model;
     return widget;
   }

+ 43 - 38
src/notebook/notebook/widget.ts

@@ -106,15 +106,13 @@ class StaticNotebook extends Widget {
   /**
    * Construct a notebook widget.
    */
-  constructor(rendermime: RenderMime<Widget>, options: StaticNotebook.IOptions = {}) {
+  constructor(options: StaticNotebook.IOptions) {
     super();
     this.node.tabIndex = -1;  // Allow the widget to take focus.
     this.addClass(NB_CLASS);
-    this._rendermime = rendermime;
+    this._rendermime = options.rendermime;
     this.layout = new PanelLayout();
-    this._codeFactory = options.codeCellFactory || Private.createCodeCell;
-    this._mdFactory = options.markdownCellFactory || Private.createMarkdownCell;
-    this._rawFactory = options.rawCellFactory || Private.createRawCell;
+    this._factory = options.cellWidgetFactory || Private.defaultFactory;
   }
 
   /**
@@ -225,13 +223,13 @@ class StaticNotebook extends Widget {
   private _createWidget(model: ICellModel): BaseCellWidget {
     switch (model.type) {
     case 'code':
-      let codeFactory = this._codeFactory;
+      let codeFactory = this._factory.newCodeCell;
       return codeFactory(model as CodeCellModel, this._rendermime);
     case 'markdown':
-      let mdFactory = this._mdFactory;
+      let mdFactory = this._factory.newMarkdownCell;
       return mdFactory(model as MarkdownCellModel, this._rendermime);
     default:
-      let rawFactory = this._rawFactory;
+      let rawFactory = this._factory.newRawCell;
       return rawFactory(model as RawCellModel);
     }
   }
@@ -305,13 +303,10 @@ class StaticNotebook extends Widget {
 
   private _model: INotebookModel = null;
   private _rendermime: RenderMime<Widget> = null;
-  private _codeFactory: (model: CodeCellModel, rendermime: RenderMime<Widget>) => CodeCellWidget = null;
-  private _mdFactory: (model: MarkdownCellModel, rendermime: RenderMime<Widget>) => MarkdownCellWidget = null;
-  private _rawFactory: (model: RawCellModel) => RawCellWidget = null;
+  private _factory: StaticNotebook.ICellWidgetFactory = null;
 }
 
 
-
 /**
  * The namespace for the `StaticNotebook` class statics.
  */
@@ -322,25 +317,43 @@ namespace StaticNotebook {
    */
   export
   interface IOptions {
+    /**
+     * The rendermime instance used by the widget.
+     */
+    rendermime: RenderMime<Widget>;
+
     /**
      * The language preference for the model.
      */
     languagePreference?: string;
 
     /**
-     * A factory for creating code cell models.
+     * A factory for creating code cell widgets.
+     *
+     * The default is a shared factory instance.
+     */
+    cellWidgetFactory?: ICellWidgetFactory;
+  }
+
+  /**
+   * A factory for creating code cell widgets.
+   */
+  export
+  interface ICellWidgetFactory {
+    /**
+     * Create a new code cell widget.
      */
-    codeCellFactory?: (model: CodeCellModel, rendermime: RenderMime<Widget>) => CodeCellWidget;
+    newCodeCell(model: CodeCellModel, rendermime: RenderMime<Widget>): CodeCellWidget;
 
     /**
-     * A factory for creating markdown cell models.
+     * Create a new markdown cell widget.
      */
-    markdownCellFactory?: (model: MarkdownCellModel, rendermime: RenderMime<Widget>) => MarkdownCellWidget;
+    newMarkdownCell(model: MarkdownCellModel, rendermime: RenderMime<Widget>): MarkdownCellWidget;
 
     /**
-     * A factory for creating raw cell models.
+     * Create a new raw cell widget.
      */
-    rawCellFactory?: (model: RawCellModel) => RawCellWidget;
+    newRawCell(model: RawCellModel): RawCellWidget;
   }
 }
 
@@ -652,28 +665,20 @@ namespace Private {
   const stateChangedSignal = new Signal<Notebook, IChangedArgs<any>>();
 
   /**
-   * A factory for creating a new code cell widget.
+   * The default `ICellWidgetFactory` instance.
    */
   export
-  function createCodeCell(model: CodeCellModel, rendermime: RenderMime<Widget>): CodeCellWidget {
-    return new CodeCellWidget(model, rendermime);
-  }
-
-  /**
-   * A factory for creating a new markdown cell widget.
-   */
-  export
-  function createMarkdownCell(model: MarkdownCellModel, rendermime: RenderMime<Widget>): MarkdownCellWidget {
-    return new MarkdownCellWidget(model, rendermime);
-  }
-
-  /**
-   * A factory for creating a new raw cell widget.
-   */
-  export
-  function createRawCell(model: RawCellModel): RawCellWidget {
-    return new RawCellWidget(model);
-  }
+  const defaultFactory: StaticNotebook.ICellWidgetFactory = {
+    newCodeCell: (model: CodeCellModel, rendermime: RenderMime<Widget>) => {
+      return new CodeCellWidget(model, rendermime);
+    },
+    newMarkdownCell: (model: MarkdownCellModel, rendermime: RenderMime<Widget>) => {
+      return new MarkdownCellWidget(model, rendermime);
+    },
+    newRawCell: (model: RawCellModel) => {
+      return new RawCellWidget(model);
+    }
+  };
 
  /**
   * Scroll an element into view if needed.