Ver código fonte

update docmanager for TS 2.4

S. Chris Colbert 7 anos atrás
pai
commit
e498ddb619

+ 37 - 37
packages/docmanager/src/dialogs.ts

@@ -61,11 +61,13 @@ interface IFileContainer extends JSONObject {
  * Create a file using a file creator.
  */
 export
-function createFromDialog(container: IFileContainer, manager: IDocumentManager, creatorName: string): Promise<Widget> {
+function createFromDialog(container: IFileContainer, manager: IDocumentManager, creatorName: string): Promise<Widget | null> {
   let handler = new CreateFromHandler(container, manager, creatorName);
-  return manager.services.ready
-    .then(() => handler.populate())
-    .then(() => handler.showDialog());
+  return manager.services.ready.then(() => {
+    return handler.populate();
+  }).then(() => {
+    return handler.showDialog();
+  });
 }
 
 
@@ -73,9 +75,8 @@ function createFromDialog(container: IFileContainer, manager: IDocumentManager,
  * Rename a file with an optional dialog.
  */
 export
-function renameDialog(manager: IDocumentManager, oldPath: string): Promise<Contents.IModel> {
-  let handler = new RenameHandler(manager, oldPath);
-  return handler.showDialog();
+function renameDialog(manager: IDocumentManager, oldPath: string): Promise<Contents.IModel | null> {
+  return (new RenameHandler(manager, oldPath)).showDialog();
 }
 
 
@@ -83,26 +84,25 @@ function renameDialog(manager: IDocumentManager, oldPath: string): Promise<Conte
  * Rename a file with optional dialog.
  */
 export
-function renameFile(manager: IDocumentManager, oldPath: string, newPath: string): Promise<Contents.IModel> {
+function renameFile(manager: IDocumentManager, oldPath: string, newPath: string): Promise<Contents.IModel | null> {
   return manager.rename(oldPath, newPath).catch(error => {
     if (error.xhr) {
       error.message = `${error.xhr.statusText} ${error.xhr.status}`;
     }
-    let overwriteBtn = Dialog.warnButton({ label: 'OVERWRITE' });
-    if (error.message.indexOf('409') !== -1) {
-      let options = {
-        title: 'Overwrite file?',
-        body: `"${newPath}" already exists, overwrite?`,
-        buttons: [Dialog.cancelButton(), overwriteBtn]
-      };
-      return showDialog(options).then(button => {
-        if (button.accept) {
-          return manager.overwrite(oldPath, newPath);
-        }
-      });
-    } else {
+    if (error.message.indexOf('409') === -1) {
       throw error;
     }
+    let options = {
+      title: 'Overwrite file?',
+      body: `"${newPath}" already exists, overwrite?`,
+      buttons: [Dialog.cancelButton(), Dialog.warnButton({ label: 'OVERWRITE' })]
+    };
+    return showDialog(options).then(button => {
+      if (!button.accept) {
+        return null;
+      }
+      return manager.overwrite(oldPath, newPath);
+    });
   });
 }
 
@@ -159,18 +159,19 @@ class RenameHandler extends Widget {
   /**
    * Show the rename dialog.
    */
-  showDialog(): Promise<Widget> {
+  showDialog(): Promise<Contents.IModel | null> {
     return showDialog({
       title: 'Rename File',
       body: this.node,
       primaryElement: this.inputNode,
       buttons: [Dialog.cancelButton(), Dialog.okButton({ label: 'RENAME' })]
     }).then(result => {
-      if (result.accept) {
-        let basePath = PathExt.dirname(this._oldPath);
-        let newPath = PathExt.join(basePath, this.inputNode.value);
-        return renameFile(this._manager, this._oldPath, newPath);
+      if (!result.accept) {
+        return null;
       }
+      let basePath = PathExt.dirname(this._oldPath);
+      let newPath = PathExt.join(basePath, this.inputNode.value);
+      return renameFile(this._manager, this._oldPath, newPath);
     });
   }
 
@@ -237,24 +238,23 @@ class CreateFromHandler extends Widget {
   /**
    * Show the createNew dialog.
    */
-  showDialog(): Promise<Widget> {
+  showDialog(): Promise<Widget | null> {
     return showDialog({
       title: `Create New ${this._creatorName}`,
       body: this.node,
       primaryElement: this.inputNode,
       buttons: [Dialog.cancelButton(), Dialog.okButton({ label: 'CREATE' })]
     }).then(result => {
-      if (result.accept) {
-        return this._open().then(widget => {
-          if (!widget) {
-            return this.showDialog();
-          }
-          return widget;
-        });
+      if (!result.accept) {
+        this._manager.deleteFile('/' + this._orig.path);
+        return null;
       }
-
-      this._manager.deleteFile('/' + this._orig.path);
-      return null;
+      return this._open().then(widget => {
+        if (!widget) {
+          return this.showDialog();
+        }
+        return widget;
+      });
     });
   }
 

+ 4 - 13
packages/docmanager/src/widgetmanager.ts

@@ -124,9 +124,7 @@ class DocumentWidgetManager implements IDisposable {
   adoptWidget(context: DocumentRegistry.Context, widget: Widget): void {
     let widgets = Private.widgetsProperty.get(context);
     widgets.push(widget);
-    MessageLoop.installMessageHook(widget, (handler: IMessageHandler, msg: Message) => {
-      return this.filterMessage(handler, msg);
-    });
+    MessageLoop.installMessageHook(widget, this);
     widget.addClass(DOCUMENT_CLASS);
     widget.title.closable = true;
     widget.disposed.connect(this._widgetDisposed, this);
@@ -209,12 +207,9 @@ class DocumentWidgetManager implements IDisposable {
    * @returns `false` if the message should be filtered, of `true`
    *   if the message should be dispatched to the handler as normal.
    */
-  protected filterMessage(handler: IMessageHandler, msg: Message): boolean {
+  messageHook(handler: IMessageHandler, msg: Message): boolean {
     switch (msg.type) {
     case 'close-request':
-      if (this._closeGuard) {
-        return true;
-      }
       this.onClose(handler as Widget);
       return false;
     case 'activate-request':
@@ -268,15 +263,12 @@ class DocumentWidgetManager implements IDisposable {
         return true;
       }
       if (result) {
-        this._closeGuard = true;
-        widget.close();
-        this._closeGuard = false;
-        // Dispose of document widgets when they are closed.
         widget.dispose();
       }
       return result;
-    }).catch(() => {
+    }).catch(error => {
       widget.dispose();
+      throw error;
     });
   }
 
@@ -348,7 +340,6 @@ class DocumentWidgetManager implements IDisposable {
     each(widgets, widget => { this.setCaption(widget); });
   }
 
-  private _closeGuard = false;
   private _registry: DocumentRegistry = null;
   private _activateRequested = new Signal<this, string>(this);
 }