Browse Source

Clean up reusing an existing widget

Steven Silvester 8 years ago
parent
commit
5534312a84
3 changed files with 29 additions and 27 deletions
  1. 23 23
      src/docmanager/index.ts
  2. 2 1
      src/filebrowser/browser.ts
  3. 4 3
      src/filebrowser/listing.ts

+ 23 - 23
src/docmanager/index.ts

@@ -548,12 +548,6 @@ class DocumentManager implements IDisposable {
     let model: IDocumentModel;
     let id = this._contextManager.findContext(path, mFactoryEx.name);
     if (id) {
-      // See if we already have a widget open for this path and widgetName.
-      for (let widget of this._widgets[id]) {
-        if (Private.factoryProperty.get(widget) === widgetName) {
-          return widget;
-        }
-      }
       model = this._contextManager.getModel(id);
     } else {
       model = mFactoryEx.factory.createNew(lang);
@@ -614,9 +608,9 @@ class DocumentManager implements IDisposable {
   }
 
   /**
-   * Find the path given a widget.
+   * Get the path given a widget.
    */
-  findPath(widget: Widget): string {
+  getPath(widget: Widget): string {
     let id = Private.contextProperty.get(widget);
     return this._contextManager.getPath(id);
   }
@@ -683,6 +677,27 @@ class DocumentManager implements IDisposable {
     }
   }
 
+  /**
+   * See if a widget already exists for the given path and widget name.
+   *
+   * #### Notes
+   * This can be used to use an existing widget instead of opening
+   * a new widget.
+   */
+  findWidget(path: string, widgetName='default'): Widget {
+    let ids = this._contextManager.getIdsForPath(path);
+    if (widgetName === 'default') {
+      widgetName = this._defaultWidgetFactory;
+    }
+    for (let id of ids) {
+      for (let widget of this._widgets[id]) {
+        if (Private.factoryProperty.get(widget) === widgetName) {
+          return widget;
+        }
+      }
+    }
+  }
+
   /**
    * Save the document contents to disk.
    *
@@ -712,21 +727,6 @@ class DocumentManager implements IDisposable {
     });
   }
 
-  /**
-   * Create a new widget based on an existing widget.
-   *
-   * #### Notes
-   * The new widget will have the same model and context as the
-   * existing widget.
-   */
-  clone(widget: Widget): Widget {
-    let parent = new Widget();
-    let id = Private.contextProperty.get(widget);
-    let factoryName = Private.factoryProperty.get(widget);
-    this._createWidget(id, factoryName, parent);
-    return parent;
-  }
-
   /**
    * Revert the document contents to disk contents.
    *

+ 2 - 1
src/filebrowser/browser.ts

@@ -170,7 +170,8 @@ class FileBrowserWidget extends Widget {
           showErrorMessage(this, 'Open directory', error)
         );
       } else {
-        let widget = this._manager.open(item.path);
+        let path = item.path;
+        let widget = this._manager.findWidget(path) || this._manager.open(item.path);
         this._opener.open(widget);
       }
     }

+ 4 - 3
src/filebrowser/listing.ts

@@ -885,7 +885,8 @@ class DirListing extends Widget {
         showErrorMessage(this, 'Open directory', error)
       );
     } else {
-      let widget = this._manager.open(item.path);
+      let path = item.path;
+      let widget = this._manager.findWidget(path) || this._manager.open(item.path);
       this._opener.open(widget);
     }
   }
@@ -1039,13 +1040,13 @@ class DirListing extends Widget {
     this._drag.mimeData.setData(utils.CONTENTS_MIME, null);
     if (item && item.type !== 'directory') {
       this._drag.mimeData.setData(FACTORY_MIME, () => {
-        return this._manager.open(item.path);
+        let path = item.path;
+        return this._manager.findWidget(path) || this._manager.open(item.path);
       });
     }
 
     // Start the drag and remove the mousemove listener.
     this._drag.start(clientX, clientY).then(action => {
-      console.log('action', action);
       this._drag = null;
     });
     document.removeEventListener('mousemove', this, true);