Browse Source

More cleanup of plugin creation

Steven Silvester 9 years ago
parent
commit
3ae8a47054

+ 2 - 2
examples/lab/src/plugin.ts

@@ -40,8 +40,8 @@ import {
  * This is called automatically when the plugin is loaded.
  */
 export
-function resolve(container: Container): void {
-  container.resolve({
+function resolve(container: Container): Promise<void> {
+  return container.resolve({
     requires: [IAppShell, ICommandPalette, IFileBrowserWidget],
     create: (shell, palette, browser) => {
       palette.title.text = 'Commands';

+ 3 - 2
src/filehandler/plugin.ts

@@ -28,12 +28,13 @@ import {
  * This is called automatically when the plugin is loaded.
  */
 export
-function resolve(container: Container): void {
-  container.resolve({
+function resolve(container: Container): Promise<IFileHandler> {
+  return container.resolve({
     requires: [IServicesProvider, IFileOpener],
     create: (services, opener) => {
       let handler = new FileHandler(services.contentsManager);
       opener.registerDefault(handler);
+      return handler;
     }
   });
 }

+ 55 - 84
src/fileopener/plugin.ts

@@ -6,6 +6,10 @@ import {
   FileBrowserWidget, FileHandler
 } from 'jupyter-js-filebrowser';
 
+import {
+  IContentsModel
+} from 'jupyter-js-services';
+
 import {
   IAppShell, ICommandPalette, ICommandRegistry
 } from 'phosphide';
@@ -44,103 +48,70 @@ import {
  */
 export
 function resolve(container: Container): Promise<void> {
-  return container.resolve(FileOpenerProvider).then(provider => provider.run());
-}
+  return container.resolve({
+    requires: [IAppShell, IFileOpener, IFileBrowserWidget, ICommandPalette, ICommandRegistry],
+    create: (appShell, opener, browser, palette, registry) => {
+      let newFileCommandItem = {
+        id: 'jupyter-plugins:new-text-file',
+        command: new DelegateCommand(() => {
+           browser.newUntitled('file', '.txt').then((contents: IContentsModel) => {
+              opener.open(contents.path);
+              browser.refresh();
+            });
+          })
+      }
+      let newNotebookCommandItem = {
+        id: 'jupyter-plugins:new-notebook',
+        command: new DelegateCommand(() => {
+          browser.newUntitled('notebook').then((contents: IContentsModel) => {
+            opener.open(contents.path);
+            browser.refresh();
+          });
+        })
+      }
+      registry.add([newFileCommandItem, newNotebookCommandItem]);
+      let paletteItems = [{
+        id: 'jupyter-plugins:new-text-file',
+        title: 'Text File',
+        caption: ''
+      }, {
+        id: 'jupyter-plugins:new-notebook',
+        title: 'Notebook',
+        caption: ''
+      }];
+      let section = {
+        text: 'New...',
+        items: paletteItems
+      }
+      palette.add([section]);
 
-export
-function register(container: Container): void {
-  container.register(IFileOpener, FileOpener);
+      FileBrowserWidget.widgetFactory = () => {
+        let model = browser.model;
+        let item = model.items[model.selected[0]];
+        return opener.open(item.path);
+      }
+    }
+  });
 }
 
 
-class FileOpenerProvider {
-  /**
-   * The dependencies required by the file opener.
-   */
-  static requires: Token<any>[] = [IAppShell, IFileOpener, IFileBrowserWidget, ICommandPalette, ICommandRegistry];
-
-  static create(appShell: IAppShell, opener: IFileOpener, browserProvider: IFileBrowserWidget, palette: ICommandPalette, registry: ICommandRegistry): FileOpenerProvider {
-    return new FileOpenerProvider(appShell, opener, browserProvider, palette, registry);
-  }
-
-  /**
-   * Construct a new file opener.
-   */
-  constructor(appShell: IAppShell, opener: IFileOpener, browser: IFileBrowserWidget, palette: ICommandPalette, registry: ICommandRegistry) {
-    this._browser = browser;
-    this._registry = registry;
-    this._palette = palette;
-    this._appShell = appShell;
-    this._opener = opener;
-  }
-
-
-  run() {
-    let newFileCommandItem = {
-      id: 'jupyter-plugins:new-text-file',
-      command: new DelegateCommand(() => {
-        this._browser.newUntitled('file', '.txt').then(
-          contents => this._opener.open(contents.path)
-        );
-      })
-    }
-    let newNotebookCommandItem = {
-      id: 'jupyter-plugins:new-notebook',
-      command: new DelegateCommand(() => {
-        this._browser.newUntitled('notebook').then(
-          contents => this._opener.open(contents.path)
-        );
-      })
-    }
-    this._registry.add([newFileCommandItem, newNotebookCommandItem]);
-    let paletteItems = [{
-      id: 'jupyter-plugins:new-text-file',
-      title: 'Text File',
-      caption: ''
-    }, {
-      id: 'jupyter-plugins:new-notebook',
-      title: 'Notebook',
-      caption: ''
-    }];
-    let section = {
-      text: 'New...',
-      items: paletteItems
-    }
-    this._palette.add([section]);
-
-    FileBrowserWidget.widgetFactory = () => {
-      let model = this._browser.model;
-      let item = model.items[model.selected[0]];
-      return this._opener.open(item.path);
+export
+function register(container: Container): void {
+  container.register(IFileOpener, {
+    requires: [IAppShell, IFileBrowserWidget],
+    create: (shell, browser) => {
+      return new FileOpener(shell, browser);
     }
-  }
-
-  private _appShell: IAppShell = null;
-  private _defaultHandler: IFileHandler = null;
-  private _browser: FileBrowserWidget = null;
-  private _registry: ICommandRegistry = null;
-  private _palette: ICommandPalette = null;
-  private _opener: IFileOpener = null;
+  });
 }
 
 
+
 /**
  * An implementation on an IFileOpener.
  */
 class FileOpener implements IFileOpener {
 
-  /**
-   * The dependencies required by the file opener.
-   */
-  static requires: Token<any>[] = [IAppShell, IFileBrowserWidget];
-
-  /**
-   * Create a new file opener instance.
-   */
-  static create(appShell: IAppShell, browserProvider: IFileBrowserWidget): IFileOpener {
-    return new FileOpener(appShell, browserProvider);
-  }
-
   /**
    * Construct a new file opener.
    */

+ 2 - 1
src/imagehandler/plugin.ts

@@ -32,12 +32,13 @@ import {
  * This is called automatically when the plugin is loaded.
  */
 export
-function resolve(container: Container): Promise<void> {
+function resolve(container: Container): Promise<IFileHandler> {
   return container.resolve({
     requires: [IServicesProvider, IFileOpener],
     create: (services, opener) => {
       let handler = new ImageHandler(services.contentsManager);
       opener.register(handler);
+      return handler;
     }
   });
 }

+ 4 - 3
src/notebook/plugin.ts

@@ -15,7 +15,7 @@ import {
 } from 'jupyter-js-services';
 
 import {
-  IServicesProvider, IFileOpener
+  IServicesProvider, IFileOpener, IFileHandler
 } from '../index';
 
 import {
@@ -37,12 +37,13 @@ import './plugin.css';
  * This is called automatically when the plugin is loaded.
  */
 export
-function resolve(container: Container): void {
-  container.resolve({
+function resolve(container: Container): Promise<IFileHandler> {
+  return container.resolve({
     requires: [IServicesProvider, IFileOpener],
     create: (services, opener) => {
       let handler = new NotebookFileHandler(services.contentsManager);
       opener.register(handler);
+      return handler;
     }
   });
 }

+ 4 - 13
src/services/plugin.ts

@@ -30,7 +30,10 @@ import {
  */
 export
 function register(container: Container): void {
-  container.register(IServicesProvider, ServicesProvider);
+  container.register(IServicesProvider, {
+    requires: [],
+    create: () => { return new ServicesProvider() }
+  });
 }
 
 
@@ -39,18 +42,6 @@ function register(container: Container): void {
  */
 class ServicesProvider implements IServicesProvider {
 
-  /**
-   * The dependencies required by the services provider.
-   */
-  static requires: Token<any>[] = [];
-
-  /**
-   * Create a new services provider instance.
-   */
-  static create(): IServicesProvider {
-    return new ServicesProvider();
-  }
-
   /**
    * Construct a new services provider.
    */

+ 2 - 2
src/terminal/plugin.ts

@@ -22,8 +22,8 @@ import './plugin.css';
 
 
 export
-function resolve(container: Container): void {
-  container.resolve({
+function resolve(container: Container): Promise<void> {
+  return container.resolve({
     requires: [IAppShell, ICommandPalette, ICommandRegistry],
     create: (shell, palette, registry) => {
       let termCommandItem = {