Selaa lähdekoodia

Add the contents model to the context

Steven Silvester 8 vuotta sitten
vanhempi
commit
56cb1a1ffe
2 muutettua tiedostoa jossa 60 lisäystä ja 13 poistoa
  1. 42 3
      src/docmanager/context.ts
  2. 18 10
      src/docmanager/index.ts

+ 42 - 3
src/docmanager/context.ts

@@ -5,7 +5,7 @@
 import {
   IKernelId, IKernel, IKernelSpecIds, IContentsManager,
   INotebookSessionManager, INotebookSession, ISessionId,
-  IContentsOpts, ISessionOptions
+  IContentsOpts, ISessionOptions, IContentsModel
 } from 'jupyter-js-services';
 
 import * as utils
@@ -99,6 +99,17 @@ class Context implements IDocumentContext {
     return this._manager.getPath(this._id);
   }
 
+  /**
+   * The current contents model associated with the document
+   *
+   * #### Notes
+   * This is a read-only property.  The model will have an
+   * empty `contents` field.
+   */
+  get contentsModel(): IContentsModel {
+    return this._manager.getContentsModel(this._id);
+  }
+
   /**
    * Get the kernel spec information.
    *
@@ -253,7 +264,8 @@ class ContextManager implements IDisposable {
   /**
    * Create a new context.
    */
-  createNew(path: string, model: IDocumentModel, options: IModelFactoryOptions): string {
+  createNew(path: string, model: IDocumentModel, options: IModelFactoryOptions,
+    contents: IContentsModel): string {
     let context = new Context(this);
     this._contexts[context.id] = {
       context,
@@ -261,6 +273,7 @@ class ContextManager implements IDisposable {
       model,
       modelName: options.name,
       opts: options.contentsOptions,
+      contentsModel: this._copyContentsModel(contents),
       session: null
     };
     return context.id;
@@ -331,6 +344,13 @@ class ContextManager implements IDisposable {
     return this._contexts[id].path;
   }
 
+  /**
+   * Get the current contents model associated with a document.
+   */
+  getContentsModel(id: string): IContentsModel {
+    return this._contexts[id].contentsModel;
+  }
+
   /**
    * Change the current kernel associated with the document.
    */
@@ -390,7 +410,8 @@ class ContextManager implements IDisposable {
     } else {
       opts.content = model.toString();
     }
-    return this._contentsManager.save(path, opts).then(() => {
+    return this._contentsManager.save(path, opts).then(contents => {
+      contextEx.contentsModel = this._copyContentsModel(contents);
       model.dirty = false;
     });
   }
@@ -409,6 +430,7 @@ class ContextManager implements IDisposable {
       } else {
         model.fromString(contents.content);
       }
+      contextEx.contentsModel = this._copyContentsModel(contents);
       model.dirty = false;
     });
   }
@@ -448,6 +470,22 @@ class ContextManager implements IDisposable {
     });
   }
 
+  /**
+   * Copy the contents of a contents model, without the content.
+   */
+  private _copyContentsModel(model: IContentsModel): IContentsModel {
+    return {
+      path: model.path,
+      name: model.name,
+      type: model.type,
+      writable: model.writable,
+      created: model.created,
+      last_modified: model.last_modified,
+      mimetype: model.mimetype,
+      format: model.format
+    };
+  }
+
   private _contentsManager: IContentsManager = null;
   private _sessionManager: INotebookSessionManager = null;
   private _kernelspecids: IKernelSpecIds = null;
@@ -467,6 +505,7 @@ namespace Private {
     session: INotebookSession;
     opts: IContentsOpts;
     path: string;
+    contentsModel: IContentsModel;
     modelName: string;
   }
 }

+ 18 - 10
src/docmanager/index.ts

@@ -146,6 +146,15 @@ export interface IDocumentContext extends IDisposable {
    */
   path: string;
 
+  /**
+   * The current contents model associated with the document
+   *
+   * #### Notes
+   * This is a read-only property.  The model will have an
+   * empty `contents` field.
+   */
+  contentsModel: IContentsModel;
+
   /**
    * Get the kernel spec information.
    *
@@ -560,11 +569,10 @@ class DocumentManager implements IDisposable {
         model.fromString(contents.content);
       }
       model.dirty = false;
-      id = this._createContext(path, model, widgetName);
+      id = this._createContext(path, model, widgetName, contents);
       this._createWidget(id, widgetName, widget, kernel);
     });
     installMessageFilter(widget, this);
-    Private.factoryProperty.set(widget, widgetName);
     return widget;
   }
 
@@ -598,12 +606,11 @@ class DocumentManager implements IDisposable {
     } else {
       opts.content = model.toString();
     }
-    manager.save(path, opts).then(content => {
-      let id = this._createContext(path, model, widgetName);
+    manager.save(path, opts).then(contents => {
+      let id = this._createContext(path, model, widgetName, contents);
       this._createWidget(id, widgetName, widget, kernel);
     });
     installMessageFilter(widget, this);
-    Private.factoryProperty.set(widget, widgetName);
     return widget;
   }
 
@@ -766,22 +773,21 @@ class DocumentManager implements IDisposable {
   /**
    * Create a context or reuse an existing one.
    */
-  private _createContext(path: string, model: IDocumentModel, widgetName: string): string {
+  private _createContext(path: string, model: IDocumentModel, widgetName: string, contents: IContentsModel): string {
     let mFactoryEx = this._getModelFactoryEx(widgetName);
     let id = this._contextManager.findContext(path, mFactoryEx.name);
     if (id) {
       return id;
     } else {
-      return this._contextManager.createNew(path, model, mFactoryEx);
+      return this._contextManager.createNew(path, model, mFactoryEx, contents);
     }
   }
 
   /**
-   * Create a or reuse a context and create a widget.
+   * Create a widget from a context and attach it to the parent.
    */
-  private _createWidget(contextId: string, widgetName: string, parent: Widget, kernel?:IKernelId): void {
+  private _createWidget(contextId: string, widgetName: string, parent: Widget, kernel?: IKernelId): void {
     let wFactoryEx = this._getWidgetFactoryEx(widgetName);
-    Private.contextProperty.set(parent, contextId);
     if (!(contextId in this._widgets)) {
       this._widgets[contextId] = [];
     }
@@ -791,6 +797,8 @@ class DocumentManager implements IDisposable {
     // Create the child widget using the factory.
     let child = wFactoryEx.factory.createNew(model, context, kernel);
     this._attachChild(parent, child);
+    Private.factoryProperty.set(parent, widgetName);
+    Private.contextProperty.set(parent, contextId);
   }
 
   /**