Pārlūkot izejas kodu

Add type aliases for common doc registry interfaces

Steven Silvester 8 gadi atpakaļ
vecāks
revīzija
b6cd19b02a

+ 4 - 4
src/csvwidget/widget.ts

@@ -91,7 +91,7 @@ class CSVWidget extends Widget {
   /**
    * The CSV widget's context.
    */
-  get context(): DocumentRegistry.IContext<DocumentRegistry.IModel> {
+  get context(): DocumentRegistry.Context {
     return this._context;
   }
 
@@ -134,7 +134,7 @@ class CSVWidget extends Widget {
     this.node.focus();
   }
 
-  private _context: DocumentRegistry.IContext<DocumentRegistry.IModel> = null;
+  private _context: DocumentRegistry.Context = null;
   private _model: CSVModel = null;
   private _table: CSVTable = null;
   private _toolbar: CSVToolbar = null;
@@ -155,7 +155,7 @@ namespace CSVWidget {
     /**
      * The document context for the CSV being rendered by the widget.
      */
-    context: DocumentRegistry.IContext<DocumentRegistry.IModel>;
+    context: DocumentRegistry.Context;
   }
 }
 
@@ -189,7 +189,7 @@ class CSVWidgetFactory extends ABCWidgetFactory<CSVWidget, DocumentRegistry.IMod
   /**
    * Create a new widget given a context.
    */
-  protected createNewWidget(context: DocumentRegistry.IContext<DocumentRegistry.IModel>): CSVWidget {
+  protected createNewWidget(context: DocumentRegistry.Context): CSVWidget {
     return new CSVWidget({ context });
   }
 }

+ 14 - 8
src/docmanager/manager.ts

@@ -218,7 +218,7 @@ class DocumentManager implements IDisposable {
    *
    * @returns The context associated with the widget, or `undefined`.
    */
-  contextForWidget(widget: Widget): DocumentRegistry.IContext<DocumentRegistry.IModel> {
+  contextForWidget(widget: Widget): DocumentRegistry.Context {
     return this._widgetManager.contextForWidget(widget);
   }
 
@@ -261,7 +261,7 @@ class DocumentManager implements IDisposable {
   /**
    * Find a context for a given path and factory name.
    */
-  private _findContext(path: string, factoryName: string): Context<DocumentRegistry.IModel> {
+  private _findContext(path: string, factoryName: string): Private.IContext {
     return find(this._contexts, context => {
       return (context.factoryName === factoryName &&
               context.path === path);
@@ -271,7 +271,7 @@ class DocumentManager implements IDisposable {
   /**
    * Get a context for a given path.
    */
-  private _contextForPath(path: string): Context<DocumentRegistry.IModel> {
+  private _contextForPath(path: string): Private.IContext {
     return find(this._contexts, context => {
       return context.path === path;
     });
@@ -280,7 +280,7 @@ class DocumentManager implements IDisposable {
   /**
    * Create a context from a path and a model factory.
    */
-  private _createContext(path: string, factory: DocumentRegistry.IModelFactory<DocumentRegistry.IModel>): Context<DocumentRegistry.IModel> {
+  private _createContext(path: string, factory: DocumentRegistry.ModelFactory): Private.IContext {
     let adopter = (widget: Widget) => {
       this._widgetManager.adoptWidget(context, widget);
       this._opener.open(widget);
@@ -309,7 +309,7 @@ class DocumentManager implements IDisposable {
   /**
    * Get the model factory for a given widget name.
    */
-  private _widgetFactoryFor(path: string, widgetName: string): DocumentRegistry.IWidgetFactory<Widget, DocumentRegistry.IModel> {
+  private _widgetFactoryFor(path: string, widgetName: string): DocumentRegistry.WidgetFactory {
     let registry = this._registry;
     if (widgetName === 'default') {
       let factory = registry.defaultWidgetFactory(ContentsManager.extname(path));
@@ -339,7 +339,7 @@ class DocumentManager implements IDisposable {
       return;
     }
 
-    let context: Context<DocumentRegistry.IModel> = null;
+    let context: Private.IContext = null;
 
     // Handle the load-from-disk case
     if (which === 'open') {
@@ -375,7 +375,7 @@ class DocumentManager implements IDisposable {
   private _serviceManager: ServiceManager.IManager = null;
   private _widgetManager: DocumentWidgetManager = null;
   private _registry: DocumentRegistry = null;
-  private _contexts: Vector<Context<DocumentRegistry.IModel>> = new Vector<Context<DocumentRegistry.IModel>>();
+  private _contexts: Vector<Private.IContext> = new Vector<Private.IContext>();
   private _opener: DocumentManager.IWidgetOpener = null;
 }
 
@@ -427,7 +427,13 @@ namespace Private {
    * An attached property for a context save handler.
    */
   export
-  const saveHandlerProperty = new AttachedProperty<DocumentRegistry.IContext<DocumentRegistry.IModel>, SaveHandler>({
+  const saveHandlerProperty = new AttachedProperty<DocumentRegistry.Context, SaveHandler>({
     name: 'saveHandler'
   });
+
+  /**
+   * A type alias for a standard context.
+   */
+  export
+  interface IContext extends Context<DocumentRegistry.IModel> {};
 }

+ 2 - 2
src/docmanager/savehandler.ts

@@ -189,7 +189,7 @@ class SaveHandler implements IDisposable {
   private _autosaveTimer = -1;
   private _minInterval = -1;
   private _interval = -1;
-  private _context: DocumentRegistry.IContext<DocumentRegistry.IModel> = null;
+  private _context: DocumentRegistry.Context = null;
   private _manager: ServiceManager.IManager = null;
   private _isActive = false;
   private _inDialog = false;
@@ -209,7 +209,7 @@ namespace SaveHandler {
     /**
      * The context asssociated with the file.
      */
-    context: DocumentRegistry.IContext<DocumentRegistry.IModel>;
+    context: DocumentRegistry.Context;
 
     /**
      * The service manager to use for checking last saved.

+ 7 - 7
src/docmanager/widgetmanager.ts

@@ -93,7 +93,7 @@ class DocumentWidgetManager implements IDisposable {
    *
    * @throws If the factory is not registered.
    */
-  createWidget(name: string, context: DocumentRegistry.IContext<DocumentRegistry.IModel>): Widget {
+  createWidget(name: string, context: DocumentRegistry.Context): Widget {
     let factory = this._registry.getWidgetFactory(name);
     if (!factory) {
       throw new Error(`Factory is not registered: ${name}`);
@@ -127,7 +127,7 @@ class DocumentWidgetManager implements IDisposable {
    *
    * @param widget - The widget to adopt.
    */
-  adoptWidget(context: DocumentRegistry.IContext<DocumentRegistry.IModel>, widget: Widget): void {
+  adoptWidget(context: DocumentRegistry.Context, widget: Widget): void {
     let widgets = Private.widgetsProperty.get(context);
     widgets.pushBack(widget);
     installMessageHook(widget, (handler: IMessageHandler, msg: Message) => {
@@ -157,7 +157,7 @@ class DocumentWidgetManager implements IDisposable {
    * This can be used to use an existing widget instead of opening
    * a new widget.
    */
-  findWidget(context: DocumentRegistry.IContext<DocumentRegistry.IModel>, widgetName: string): Widget {
+  findWidget(context: DocumentRegistry.Context, widgetName: string): Widget {
     let widgets = Private.widgetsProperty.get(context);
     return find(widgets, widget => {
       let name = Private.nameProperty.get(widget);
@@ -174,7 +174,7 @@ class DocumentWidgetManager implements IDisposable {
    *
    * @returns The context associated with the widget, or `undefined`.
    */
-  contextForWidget(widget: Widget): DocumentRegistry.IContext<DocumentRegistry.IModel> {
+  contextForWidget(widget: Widget): DocumentRegistry.Context {
     return Private.contextProperty.get(widget);
   }
 
@@ -205,7 +205,7 @@ class DocumentWidgetManager implements IDisposable {
    *
    * @param context - The document context object.
    */
-  closeWidgets(context: DocumentRegistry.IContext<DocumentRegistry.IModel>): void {
+  closeWidgets(context: DocumentRegistry.Context): void {
     let widgets = Private.widgetsProperty.get(context);
     each(widgets, widget => {
       widget.close();
@@ -335,7 +335,7 @@ namespace Private {
    * A private attached property for a widget context.
    */
   export
-  const contextProperty = new AttachedProperty<Widget, DocumentRegistry.IContext<DocumentRegistry.IModel>>({
+  const contextProperty = new AttachedProperty<Widget, DocumentRegistry.Context>({
     name: 'context'
   });
 
@@ -351,7 +351,7 @@ namespace Private {
    * A private attached property for the widgets associated with a context.
    */
   export
-  const widgetsProperty = new AttachedProperty<DocumentRegistry.IContext<DocumentRegistry.IModel>, Vector<Widget>>({
+  const widgetsProperty = new AttachedProperty<DocumentRegistry.Context, Vector<Widget>>({
     name: 'widgets',
     create: () => {
       return new Vector<Widget>();

+ 1 - 1
src/docregistry/default.ts

@@ -168,7 +168,7 @@ defineSignal(DocumentModel.prototype, 'stateChanged');
  * An implementation of a model factory for text files.
  */
 export
-class TextModelFactory implements DocumentRegistry.IModelFactory<DocumentRegistry.IModel> {
+class TextModelFactory implements DocumentRegistry.ModelFactory {
   /**
    * The name of the model type.
    *

+ 1 - 1
src/docregistry/kernelselector.ts

@@ -128,7 +128,7 @@ function selectKernel(options: IKernelSelection): Promise<Kernel.IModel> {
  * Change the kernel on a context.
  */
 export
-function selectKernelForContext(context: DocumentRegistry.IContext<DocumentRegistry.IModel>, manager: Session.IManager, host?: HTMLElement): Promise<void> {
+function selectKernelForContext(context: DocumentRegistry.Context, manager: Session.IManager, host?: HTMLElement): Promise<void> {
   return manager.ready.then(() => {
     let options: IKernelSelection = {
       name: context.path.split('/').pop(),

+ 39 - 16
src/docregistry/registry.ts

@@ -108,7 +108,7 @@ class DocumentRegistry {
    * If an extension or global default is already registered, this factory
    * will override the existing default.
    */
-  addWidgetFactory(factory: DocumentRegistry.IWidgetFactory<Widget, DocumentRegistry.IModel>): IDisposable {
+  addWidgetFactory(factory: DocumentRegistry.WidgetFactory): IDisposable {
     let name = factory.name.toLowerCase();
     if (this._widgetFactories[name]) {
       console.warn(`Duplicate registered factory ${name}`);
@@ -173,7 +173,7 @@ class DocumentRegistry {
    * the given factory is already registered, a warning will be logged
    * and this will be a no-op.
    */
-  addModelFactory(factory: DocumentRegistry.IModelFactory<DocumentRegistry.IModel>): IDisposable {
+  addModelFactory(factory: DocumentRegistry.ModelFactory): IDisposable {
     let name = factory.name.toLowerCase();
     if (this._modelFactories[name]) {
       console.warn(`Duplicate registered factory ${name}`);
@@ -208,10 +208,10 @@ class DocumentRegistry {
    * If the extension is already registered for the given
    * widget name, a warning will be logged and this will be a no-op.
    */
-  addWidgetExtension(widgetName: string, extension: DocumentRegistry.IWidgetExtension<Widget, DocumentRegistry.IModel>): IDisposable {
+  addWidgetExtension(widgetName: string, extension: DocumentRegistry.WidgetExtension): IDisposable {
     widgetName = widgetName.toLowerCase();
     if (!(widgetName in this._extenders)) {
-      this._extenders[widgetName] = new Vector<DocumentRegistry.IWidgetExtension<Widget, DocumentRegistry.IModel>>();
+      this._extenders[widgetName] = new Vector<DocumentRegistry.WidgetExtension>();
     }
     let extenders = this._extenders[widgetName];
     let index = indexOf(extenders, extension);
@@ -310,7 +310,7 @@ class DocumentRegistry {
    * - all other extension-specific factories
    * - all other global factories
    */
-  preferredWidgetFactories(ext: string = '*'): DocumentRegistry.IWidgetFactory<Widget, DocumentRegistry.IModel>[] {
+  preferredWidgetFactories(ext: string = '*'): DocumentRegistry.WidgetFactory[] {
     let factories = new Set<string>();
     ext = Private.normalizeExtension(ext);
 
@@ -344,7 +344,7 @@ class DocumentRegistry {
 
     // Construct the return list, checking to make sure the corresponding
     // model factories are registered.
-    let factoryList: DocumentRegistry.IWidgetFactory<Widget, DocumentRegistry.IModel>[] = [];
+    let factoryList: DocumentRegistry.WidgetFactory[] = [];
     factories.forEach(name => {
       if (this._widgetFactories[name].modelName in this._modelFactories) {
         factoryList.push(this._widgetFactories[name]);
@@ -364,7 +364,7 @@ class DocumentRegistry {
    * #### Notes
    * This is equivalent to the first value in [[preferredWidgetFactories]].
    */
-  defaultWidgetFactory(ext: string = '*'): DocumentRegistry.IWidgetFactory<Widget, DocumentRegistry.IModel> {
+  defaultWidgetFactory(ext: string = '*'): DocumentRegistry.WidgetFactory {
     return this.preferredWidgetFactories(ext)[0];
   }
 
@@ -373,7 +373,7 @@ class DocumentRegistry {
    *
    * @returns A new iterator of widget factories.
    */
-  widgetFactories(): IIterator<DocumentRegistry.IWidgetFactory<Widget, DocumentRegistry.IModel>> {
+  widgetFactories(): IIterator<DocumentRegistry.WidgetFactory> {
     return map(Object.keys(this._widgetFactories), name => {
       return this._widgetFactories[name];
     });
@@ -384,7 +384,7 @@ class DocumentRegistry {
    *
    * @returns A new iterator of model factories.
    */
-  modelFactories(): IIterator<DocumentRegistry.IModelFactory<DocumentRegistry.IModel>> {
+  modelFactories(): IIterator<DocumentRegistry.ModelFactory> {
     return map(Object.keys(this._modelFactories), name => {
       return this._modelFactories[name];
     });
@@ -397,10 +397,10 @@ class DocumentRegistry {
    *
    * @returns A new iterator over the widget extensions.
    */
-  widgetExtensions(widgetName: string): IIterator<DocumentRegistry.IWidgetExtension<Widget, DocumentRegistry.IModel>> {
+  widgetExtensions(widgetName: string): IIterator<DocumentRegistry.WidgetExtension> {
     widgetName = widgetName.toLowerCase();
     if (!(widgetName in this._extenders)) {
-      return empty<DocumentRegistry.IWidgetExtension<Widget, DocumentRegistry.IModel>>();
+      return empty<DocumentRegistry.WidgetExtension>();
     }
     return this._extenders[widgetName].iter();
   }
@@ -430,7 +430,7 @@ class DocumentRegistry {
    *
    * @returns A widget factory instance.
    */
-  getWidgetFactory(widgetName: string): DocumentRegistry.IWidgetFactory<Widget, DocumentRegistry.IModel> {
+  getWidgetFactory(widgetName: string): DocumentRegistry.WidgetFactory {
     return this._widgetFactories[widgetName.toLowerCase()];
   }
 
@@ -441,7 +441,7 @@ class DocumentRegistry {
    *
    * @returns A model factory instance.
    */
-  getModelFactory(name: string): DocumentRegistry.IModelFactory<DocumentRegistry.IModel> {
+  getModelFactory(name: string): DocumentRegistry.ModelFactory {
     return this._modelFactories[name.toLowerCase()];
   }
 
@@ -493,14 +493,14 @@ class DocumentRegistry {
     };
   }
 
-  private _modelFactories: { [key: string]: DocumentRegistry.IModelFactory<DocumentRegistry.IModel> } = Object.create(null);
-  private _widgetFactories: { [key: string]: DocumentRegistry.IWidgetFactory<Widget, DocumentRegistry.IModel> } = Object.create(null);
+  private _modelFactories: { [key: string]: DocumentRegistry.ModelFactory } = Object.create(null);
+  private _widgetFactories: { [key: string]: DocumentRegistry.WidgetFactory } = Object.create(null);
   private _defaultWidgetFactory = '';
   private _defaultWidgetFactories: { [key: string]: string } = Object.create(null);
   private _widgetFactoryExtensions: {[key: string]: Vector<string> } = Object.create(null);
   private _fileTypes = new Vector<DocumentRegistry.IFileType>();
   private _creators = new Vector<DocumentRegistry.IFileCreator>();
-  private _extenders: { [key: string] : Vector<DocumentRegistry.IWidgetExtension<Widget, DocumentRegistry.IModel>> } = Object.create(null);
+  private _extenders: { [key: string] : Vector<DocumentRegistry.WidgetExtension> } = Object.create(null);
 }
 
 
@@ -718,6 +718,12 @@ namespace DocumentRegistry {
     addSibling(widget: Widget): IDisposable;
   }
 
+  /**
+   * A type alias for a context.
+   */
+  export
+  type Context = IContext<IModel>;
+
   /**
    * The options used to initialize a widget factory.
    */
@@ -785,6 +791,12 @@ namespace DocumentRegistry {
     createNew(context: IContext<U>): T;
   }
 
+  /**
+   * A type alias for a standard widget factory.
+   */
+  export
+  type WidgetFactory = IWidgetFactory<Widget, IModel>;
+
   /**
    * An interface for a widget extension.
    */
@@ -796,6 +808,11 @@ namespace DocumentRegistry {
     createNew(widget: T, context: IContext<U>): IDisposable;
   }
 
+  /**
+   * A type alias for a standard widget extension.
+   */
+  export
+  type WidgetExtension = IWidgetExtension<Widget, IModel>;
 
   /**
    * The interface for a model factory.
@@ -832,6 +849,12 @@ namespace DocumentRegistry {
     preferredLanguage(ext: string): string;
   }
 
+  /**
+   * A type alias for a standard model factory.
+   */
+  export
+  type ModelFactory = IModelFactory<IModel>;
+
   /**
    * A kernel preference for a given file path and widget.
    */

+ 4 - 4
src/editorwidget/widget.ts

@@ -66,7 +66,7 @@ class EditorWidget extends CodeMirrorWidget {
   /**
    * Construct a new editor widget.
    */
-  constructor(context: DocumentRegistry.IContext<DocumentRegistry.IModel>) {
+  constructor(context: DocumentRegistry.Context) {
     super({
       extraKeys: {
         'Tab': 'indentMore',
@@ -120,11 +120,11 @@ class EditorWidget extends CodeMirrorWidget {
   /**
    * Get the context for the editor widget.
    */
-  get context(): DocumentRegistry.IContext<DocumentRegistry.IModel> {
+  get context(): DocumentRegistry.Context {
     return this._context;
   }
 
-  private _context: DocumentRegistry.IContext<DocumentRegistry.IModel>;
+  private _context: DocumentRegistry.Context;
 }
 
 
@@ -136,7 +136,7 @@ class EditorWidgetFactory extends ABCWidgetFactory<EditorWidget, DocumentRegistr
   /**
    * Create a new widget given a context.
    */
-  protected createNewWidget(context: DocumentRegistry.IContext<DocumentRegistry.IModel>): EditorWidget {
+  protected createNewWidget(context: DocumentRegistry.Context): EditorWidget {
     return new EditorWidget(context);
   }
 }

+ 3 - 3
src/imagewidget/widget.ts

@@ -31,7 +31,7 @@ class ImageWidget extends Widget {
   /**
    * Construct a new image widget.
    */
-  constructor(context: DocumentRegistry.IContext<DocumentRegistry.IModel>) {
+  constructor(context: DocumentRegistry.Context) {
     super({ node: Private.createNode() });
     this._context = context;
     this.node.tabIndex = -1;
@@ -48,7 +48,7 @@ class ImageWidget extends Widget {
   /**
    * The image widget's context.
    */
-  get context(): DocumentRegistry.IContext<DocumentRegistry.IModel> {
+  get context(): DocumentRegistry.Context {
     return this._context;
   }
 
@@ -102,7 +102,7 @@ class ImageWidget extends Widget {
     this.node.focus();
   }
 
-  private _context: DocumentRegistry.IContext<DocumentRegistry.IModel>;
+  private _context: DocumentRegistry.Context;
   private _scale = 1;
 }
 

+ 4 - 4
src/markdownwidget/widget.ts

@@ -45,7 +45,7 @@ class MarkdownWidget extends Widget {
   /**
    * Construct a new markdown widget.
    */
-  constructor(context: DocumentRegistry.IContext<DocumentRegistry.IModel>, rendermime: RenderMime) {
+  constructor(context: DocumentRegistry.Context, rendermime: RenderMime) {
     super();
     this.addClass(MD_CLASS);
     this.layout = new PanelLayout();
@@ -69,7 +69,7 @@ class MarkdownWidget extends Widget {
   /**
    * The markdown widget's context.
    */
-  get context(): DocumentRegistry.IContext<DocumentRegistry.IModel> {
+  get context(): DocumentRegistry.Context {
     return this._context;
   }
 
@@ -111,7 +111,7 @@ class MarkdownWidget extends Widget {
     layout.addWidget(widget);
   }
 
-  private _context: DocumentRegistry.IContext<DocumentRegistry.IModel> = null;
+  private _context: DocumentRegistry.Context = null;
   private _monitor: ActivityMonitor<any, any> = null;
   private _rendermime: RenderMime = null;
 }
@@ -133,7 +133,7 @@ class MarkdownWidgetFactory extends ABCWidgetFactory<MarkdownWidget, DocumentReg
   /**
    * Create a new widget given a context.
    */
-  protected createNewWidget(context: DocumentRegistry.IContext<DocumentRegistry.IModel>): MarkdownWidget {
+  protected createNewWidget(context: DocumentRegistry.Context): MarkdownWidget {
     return new MarkdownWidget(context, this._rendermime.clone());
   }