Browse Source

Use new docmanager

Steven Silvester 9 years ago
parent
commit
9e571122d1

+ 2 - 1
package.json

@@ -8,7 +8,8 @@
     "codemirror": "^5.10.0",
     "jquery": "^2.2.0",
     "jquery-ui": "^1.10.5",
-    "jupyter-js-filebrowser": "^0.7.2",
+    "jupyter-js-docmanager": "0.0.1",
+    "jupyter-js-filebrowser": "^0.8.0",
     "jupyter-js-notebook": "^0.5.0",
     "jupyter-js-services": "^0.5.0",
     "jupyter-js-terminal": "^0.1.15",

+ 3 - 39
src/documentmanager/index.ts

@@ -3,8 +3,8 @@
 'use strict';
 
 import {
-  AbstractFileHandler
-} from 'jupyter-js-filebrowser';
+  DocumentManager
+} from 'jupyter-js-docmanager';
 
 import {
   IContentsModel
@@ -27,43 +27,7 @@ import {
  * An interface for a document manager.
  */
 export
-interface IDocumentManager {
-  /**
-   * Open the file and add the widget to the application shell.
-   */
-  open(model: IContentsModel): Widget;
-
-  /**
-   * Save the current document.
-   */
-  save(): void;
-
-  /**
-   * Revert the current document.
-   */
-  revert(): void;
-
-  /**
-   * Close the current document.
-   */
-  close(): void;
-
-  /**
-   * Close all documents.
-   */
-  closeAll(): void;
-
-  /**
-   * Register a file handler.
-   *
-   * @param handler - The file handler to register.
-   */
-  register(handler: AbstractFileHandler): void;
-
-  /**
-   * Register a default file handler.
-   */
-  registerDefault(handler: AbstractFileHandler): void;
+interface IDocumentManager extends DocumentManager {
 }
 
 

+ 24 - 172
src/documentmanager/plugin.ts

@@ -3,7 +3,11 @@
 'use strict';
 
 import {
-  FileBrowserWidget, AbstractFileHandler
+  AbstractFileHandler, DocumentManager
+} from 'jupyter-js-docmanager';
+
+import {
+  FileBrowserWidget
 } from 'jupyter-js-filebrowser';
 
 import {
@@ -277,177 +281,25 @@ export
 function register(container: Container): void {
   container.register(IDocumentManager, {
     requires: [IAppShell, IFileBrowserWidget],
-    create: (appShell, browserProvider): IDocumentManager => {
-      return new DocumentManager(appShell, browserProvider);
+    create: (appShell: IAppShell, browser: IFileBrowserWidget): IDocumentManager => {
+      let manager = new DocumentManager();
+      browser.openRequested.connect((browser, model) => {
+        manager.open(model);
+      });
+      manager.openRequested.connect((manager, widget) => {
+        if (!widget.isAttached) {
+          appShell.addToMainArea(widget);
+        }
+        let stack = widget.parent;
+        if (!stack) {
+          return;
+        }
+        let tabs = stack.parent;
+        if (tabs instanceof TabPanel) {
+          tabs.currentWidget = widget;
+        }
+      });
+      return manager;
     }
   });
 }
-
-
-/**
- * An implementation on an IDocumentManager.
- */
-class DocumentManager implements IDocumentManager {
-
-  /**
-   * Construct a new document manager.
-   */
-  constructor(appShell: IAppShell, browser: IFileBrowserWidget) {
-    this._appShell = appShell;
-    browser.openRequested.connect(this._openRequested,
-      this);
-    document.addEventListener('focus', this._onFocus.bind(this), true);
-  }
-
-  /**
-   * Get the most recently focused widget.
-   *
-   * #### Notes
-   * This is a read-only property.
-   */
-  get currentWidget(): Widget {
-    return this._currentWidget;
-  }
-
-  /**
-   * Register a file handler.
-   */
-  register(handler: AbstractFileHandler): void {
-    this._handlers.push(handler);
-  }
-
-  /**
-   * Register a default file handler.
-   */
-  registerDefault(handler: AbstractFileHandler): void {
-    if (this._defaultHandler !== null) {
-      throw Error('Default handler already registered');
-    }
-    this._handlers.push(handler);
-    this._defaultHandler = handler;
-  }
-
-  /**
-   * Open a file and add it to the application shell.
-   */
-  open(model: IContentsModel): Widget {
-    if (this._handlers.length === 0) {
-      return;
-    }
-    let path = model.path;
-    let ext = '.' + path.split('.').pop();
-    let handlers: AbstractFileHandler[] = [];
-    // Look for matching file extensions.
-    for (let h of this._handlers) {
-      if (h.fileExtensions.indexOf(ext) !== -1) handlers.push(h);
-    }
-    let widget: Widget;
-    // If there was only one match, use it.
-    if (handlers.length === 1) {
-      widget = this._open(handlers[0], model);
-
-    // If there were no matches, use default handler.
-    } else if (handlers.length === 0) {
-      if (this._defaultHandler !== null) {
-        widget = this._open(this._defaultHandler, model);
-      } else {
-        throw new Error(`Could not open file '${path}'`);
-      }
-
-    // There are more than one possible handlers.
-    } else {
-      // TODO: Ask the user to choose one.
-      widget = this._open(handlers[0], model);
-    }
-    widget.addClass(DOCUMENT_CLASS);
-    return widget;
-  }
-
-  /**
-   * Save the current document.
-   */
-  save(): void {
-    if (this._currentHandler) this._currentHandler.save(this._currentWidget);
-  }
-
-  /**
-   * Revert the current document.
-   */
-  revert(): void {
-    if (this._currentHandler) this._currentHandler.revert(this._currentWidget);
-  }
-
-  /**
-   * Close the current document.
-   */
-  close(): void {
-    if (this._currentHandler) this._currentHandler.close(this._currentWidget);
-    this._currentWidget = null;
-    this._currentHandler = null;
-  }
-
-  /**
-   * Close all documents.
-   */
-  closeAll(): void {
-    for (let h of this._handlers) {
-      for (let w of h.widgets) {
-        w.close();
-      }
-    }
-    this._currentWidget = null;
-    this._currentHandler = null;
-  }
-
-  /**
-   * Handle an `openRequested` signal by invoking the appropriate handler.
-   */
-  private _openRequested(browser: FileBrowserWidget, model: IContentsModel): void {
-    this.open(model);
-  }
-
-  /**
-   * Open a file and add it to the application shell and give it focus.
-   */
-  private _open(handler: AbstractFileHandler, model: IContentsModel): Widget {
-    let widget = handler.open(model);
-    if (!widget.isAttached) {
-      this._appShell.addToMainArea(widget);
-    }
-    let stack = widget.parent;
-    if (!stack) {
-      return;
-    }
-    let tabs = stack.parent;
-    if (tabs instanceof TabPanel) {
-      tabs.currentWidget = widget;
-    }
-    return widget;
-  }
-
-  /**
-   * Handle a focus event on the document.
-   */
-  private _onFocus(event: Event) {
-    for (let h of this._handlers) {
-      // If the widget belongs to the handler, update the focused widget.
-      let widget = arrays.find(h.widgets,
-        w => { return w.isVisible && w.node.contains(event.target as HTMLElement); });
-      if (widget === this._currentWidget) {
-        return;
-      } else if (widget) {
-        if (this._currentWidget) this._currentWidget.removeClass(FOCUS_CLASS);
-        this._currentWidget = widget;
-        this._currentHandler = h;
-        widget.addClass(FOCUS_CLASS);
-        return;
-      }
-    }
-  }
-
-  private _handlers: AbstractFileHandler[] = [];
-  private _appShell: IAppShell = null;
-  private _defaultHandler: AbstractFileHandler = null;
-  private _currentWidget: Widget = null;
-  private _currentHandler: AbstractFileHandler = null;
-}

+ 2 - 2
src/filehandler/plugin.ts

@@ -3,8 +3,8 @@
 'use strict';
 
 import {
-  FileBrowserWidget, FileHandler, AbstractFileHandler
-} from 'jupyter-js-filebrowser';
+  FileHandler, AbstractFileHandler
+} from 'jupyter-js-docmanager';
 
 import {
   Container, Token

+ 2 - 2
src/imagehandler/plugin.ts

@@ -3,8 +3,8 @@
 'use strict';
 
 import {
-  AbstractFileHandler, FileBrowserWidget, FileHandler
-} from 'jupyter-js-filebrowser';
+  AbstractFileHandler
+} from 'jupyter-js-docmanager';
 
 import {
   IContentsModel

+ 1 - 1
src/notebook/plugin.ts

@@ -4,7 +4,7 @@
 
 import {
   AbstractFileHandler
-} from 'jupyter-js-filebrowser';
+} from 'jupyter-js-docmanager';
 
 import {
   NotebookWidget, NotebookModel, NBData, populateNotebookModel, buildOutputModel, Output