Prechádzať zdrojové kódy

Clean up handling of file overwrites

Steven Silvester 8 rokov pred
rodič
commit
98f7028877

+ 0 - 4
src/filebrowser/buttons.ts

@@ -33,10 +33,6 @@ import {
   DocumentManager
 } from '../docmanager';
 
-import {
-  IFileType
-} from '../docregistry';
-
 import {
   IWidgetOpener
 } from './browser';

+ 53 - 11
src/filebrowser/dialogs.ts

@@ -44,15 +44,7 @@ export
 function createFromDialog(model: FileBrowserModel, manager: DocumentManager, creatorName: string): Promise<Widget> {
   let handler = new CreateFromHandler(model, manager, creatorName);
   return handler.populate().then(() => {
-    return showDialog({
-      title: `Create New ${creatorName}`,
-      body: handler.node,
-      okText: 'CREATE'
-    }).then(result => {
-      if (result.text === 'CREATE') {
-        return handler.open();
-      }
-    });
+    return handler.show();
   });
 }
 
@@ -100,6 +92,33 @@ function createNewDialog(model: FileBrowserModel, manager: DocumentManager, host
 }
 
 
+/**
+ * Rename a file with optional dialog.
+ */
+export
+function renameFile(model: FileBrowserModel, oldPath: string, newPath: string): Promise<IContents.IModel> {
+  return model.rename(oldPath, newPath).catch(error => {
+    if (error.xhr) {
+      error.message = `${error.xhr.statusText} ${error.xhr.status}`;
+    }
+    if (error.message.indexOf('409') !== -1) {
+      let options = {
+        title: 'Overwrite file?',
+        body: `"${newPath}" already exists, overwrite?`,
+        okText: 'OVERWRITE'
+      };
+      return showDialog(options).then(button => {
+        if (button.text === 'OVERWRITE') {
+          return model.deleteFile(newPath).then(() => {
+            return model.rename(oldPath, newPath);
+          });
+        }
+      });
+    }
+  });
+}
+
+
 /**
  * A widget used to open files with a specific widget/kernel.
  */
@@ -249,6 +268,26 @@ class CreateFromHandler extends Widget {
     return this.node.getElementsByTagName('select')[0] as HTMLSelectElement;
   }
 
+  /**
+   * Show the createNew dialog.
+   */
+  show(): Promise<Widget> {
+    return showDialog({
+      title: `Create New ${this._creatorName}`,
+      body: this.node,
+      okText: 'CREATE'
+    }).then(result => {
+      if (result.text === 'CREATE') {
+        return this._open().then(widget => {
+          if (!widget) {
+            return this.show();
+          }
+          return widget;
+        });
+      }
+    });
+  }
+
   /**
    * Populate the create from widget.
    */
@@ -288,7 +327,7 @@ class CreateFromHandler extends Widget {
   /**
    * Open the file and return the document widget.
    */
-  open(): Promise<Widget> {
+  private _open(): Promise<Widget> {
     let path = this.input.value;
     let widgetName = this._widgetName;
     let kernelValue = this.kernelDropdown ? this.kernelDropdown.value : 'null';
@@ -297,7 +336,10 @@ class CreateFromHandler extends Widget {
       kernelId = JSON.parse(kernelValue) as IKernel.IModel;
     }
     if (path !== this._orig) {
-      return this._model.rename(this._orig, path).then(() => {
+      return renameFile(this._model, this._orig, path).then(value => {
+        if (!value) {
+          return;
+        }
         return this._manager.createNew(path, widgetName, kernelId);
       });
     }

+ 6 - 41
src/filebrowser/listing.ts

@@ -45,6 +45,10 @@ import {
   IWidgetOpener
 } from './browser';
 
+import {
+  renameFile
+} from './dialogs';
+
 import {
   FileBrowserModel
 } from './model';
@@ -943,25 +947,7 @@ class DirListing extends Widget {
     let names = event.mimeData.getData(utils.CONTENTS_MIME) as string[];
     for (let name of names) {
       let newPath = path + name;
-      promises.push(this._model.rename(name, newPath).catch(error => {
-        if (error.xhr) {
-          error.message = `${error.xhr.statusText} ${error.xhr.status}`;
-        }
-        if (error.message.indexOf('409') !== -1) {
-          let options = {
-            title: 'Overwrite file?',
-            body: `"${newPath}" already exists, overwrite?`,
-            okText: 'OVERWRITE'
-          };
-          return showDialog(options).then(button => {
-            if (button.text === 'OVERWRITE') {
-              return this._model.deleteFile(newPath).then(() => {
-                return this._model.rename(name, newPath);
-              });
-            }
-          });
-        }
-      }));
+      promises.push(renameFile(this._model, name, newPath));
     }
     Promise.all(promises).then(
       () => this._model.refresh(),
@@ -1168,28 +1154,7 @@ class DirListing extends Widget {
       if (newName === original) {
         return;
       }
-      return this._model.rename(original, newName).catch(error => {
-        if (error.xhr) {
-          error.message = `${error.xhr.status}: error.statusText`;
-        }
-        if (error.message.indexOf('409') !== -1 ||
-            error.message.indexOf('already exists') !== -1) {
-          let options = {
-            title: 'Overwrite file?',
-            body: `"${newName}" already exists, overwrite?`,
-            okText: 'OVERWRITE'
-          };
-          return showDialog(options).then(button => {
-            if (button.text === 'OVERWRITE') {
-              return this._model.deleteFile(newName).then(() => {
-                return this._model.rename(original, newName).then(() => {
-                  this._model.refresh();
-                });
-              });
-            }
-          });
-        }
-      }).catch(error => {
+      return renameFile(this._model, original, newName).catch(error => {
         utils.showErrorMessage(this, 'Rename Error', error);
         return original;
       }).then(() => {

+ 1 - 0
src/filebrowser/plugin.ts

@@ -173,6 +173,7 @@ function activateFileBrowser(app: JupyterLab, manager: IServiceManager, registry
   app.shell.addToLeftArea(fbWidget, { rank: 40 });
   app.commands.execute(cmdIds.showBrowser, void 0);
 
+  // Handle fileCreator items as they are added.
   registry.changed.connect((sender, args) => {
     if (args.type === 'fileCreator' && args.change === 'added') {
       menu.dispose();