|
@@ -6,15 +6,11 @@ import {
|
|
} from '@jupyterlab/apputils';
|
|
} from '@jupyterlab/apputils';
|
|
|
|
|
|
import {
|
|
import {
|
|
- PathExt, uuid
|
|
|
|
-} from '@jupyterlab/coreutils';
|
|
|
|
-
|
|
|
|
-import {
|
|
|
|
- Contents, IServiceManager, Kernel, Session
|
|
|
|
|
|
+ Contents, Kernel
|
|
} from '@jupyterlab/services';
|
|
} from '@jupyterlab/services';
|
|
|
|
|
|
import {
|
|
import {
|
|
- each, IIterator
|
|
|
|
|
|
+ each
|
|
} from '@phosphor/algorithm';
|
|
} from '@phosphor/algorithm';
|
|
|
|
|
|
import {
|
|
import {
|
|
@@ -22,7 +18,7 @@ import {
|
|
} from '@phosphor/widgets';
|
|
} from '@phosphor/widgets';
|
|
|
|
|
|
import {
|
|
import {
|
|
- DocumentManager
|
|
|
|
|
|
+ Actions, DocumentManager, IFileContainer
|
|
} from './';
|
|
} from './';
|
|
|
|
|
|
|
|
|
|
@@ -37,23 +33,6 @@ const FILE_DIALOG_CLASS = 'jp-FileDialog';
|
|
const FILE_CONFLICT_CLASS = 'jp-mod-conflict';
|
|
const FILE_CONFLICT_CLASS = 'jp-mod-conflict';
|
|
|
|
|
|
|
|
|
|
-/**
|
|
|
|
- * A stripped-down interface for a file container.
|
|
|
|
- */
|
|
|
|
-export
|
|
|
|
-interface IFileContainer {
|
|
|
|
- /**
|
|
|
|
- * Returns an iterator over the container's items.
|
|
|
|
- */
|
|
|
|
- items(): IIterator<Contents.IModel>;
|
|
|
|
-
|
|
|
|
- /**
|
|
|
|
- * The current working directory of the file container.
|
|
|
|
- */
|
|
|
|
- path: string;
|
|
|
|
-}
|
|
|
|
-
|
|
|
|
-
|
|
|
|
/**
|
|
/**
|
|
* Create a file using a file creator.
|
|
* Create a file using a file creator.
|
|
*/
|
|
*/
|
|
@@ -87,8 +66,7 @@ function newUntitled(manager: DocumentManager, options: Contents.ICreateOptions)
|
|
export
|
|
export
|
|
function renameFile(manager: DocumentManager, oldPath: string, newPath: string, basePath = ''): Promise<Contents.IModel> {
|
|
function renameFile(manager: DocumentManager, oldPath: string, newPath: string, basePath = ''): Promise<Contents.IModel> {
|
|
let { services } = manager;
|
|
let { services } = manager;
|
|
- let rename = Private.rename(services, oldPath, newPath, basePath);
|
|
|
|
- return rename.catch(error => {
|
|
|
|
|
|
+ return Actions.rename(services, oldPath, newPath, basePath).catch(error => {
|
|
if (error.xhr) {
|
|
if (error.xhr) {
|
|
error.message = `${error.xhr.statusText} ${error.xhr.status}`;
|
|
error.message = `${error.xhr.statusText} ${error.xhr.status}`;
|
|
}
|
|
}
|
|
@@ -101,7 +79,7 @@ function renameFile(manager: DocumentManager, oldPath: string, newPath: string,
|
|
};
|
|
};
|
|
return showDialog(options).then(button => {
|
|
return showDialog(options).then(button => {
|
|
if (button.accept) {
|
|
if (button.accept) {
|
|
- return Private.overwrite(services, oldPath, newPath, basePath);
|
|
|
|
|
|
+ return Actions.overwrite(services, oldPath, newPath, basePath);
|
|
}
|
|
}
|
|
});
|
|
});
|
|
} else {
|
|
} else {
|
|
@@ -197,7 +175,10 @@ class CreateFromHandler extends Widget {
|
|
return widget;
|
|
return widget;
|
|
});
|
|
});
|
|
}
|
|
}
|
|
- // this._model.deleteFile('/' + this._orig.path);
|
|
|
|
|
|
+
|
|
|
|
+ const basePath = this._container.path;
|
|
|
|
+ const services = this._manager.services;
|
|
|
|
+ Actions.deleteFile(services, '/' + this._orig.path, basePath);
|
|
return null;
|
|
return null;
|
|
});
|
|
});
|
|
}
|
|
}
|
|
@@ -306,90 +287,4 @@ namespace Private {
|
|
body.appendChild(kernelDropdownNode);
|
|
body.appendChild(kernelDropdownNode);
|
|
return body;
|
|
return body;
|
|
}
|
|
}
|
|
-
|
|
|
|
- /**
|
|
|
|
- * Delete a file.
|
|
|
|
- *
|
|
|
|
- * @param manager - The service manager used to delete.
|
|
|
|
- *
|
|
|
|
- * @param: path - The path to the file to be deleted.
|
|
|
|
- *
|
|
|
|
- * @param basePath - The base path to resolve against, defaults to ''.
|
|
|
|
- *
|
|
|
|
- * @returns A promise which resolves when the file is deleted.
|
|
|
|
- *
|
|
|
|
- * #### Notes
|
|
|
|
- * If there is a running session associated with the file and no other
|
|
|
|
- * sessions are using the kernel, the session will be shut down.
|
|
|
|
- */
|
|
|
|
- export
|
|
|
|
- function deleteFile(manager: IServiceManager, path: string, basePath = ''): Promise<void> {
|
|
|
|
- path = PathExt.resolve(basePath, path);
|
|
|
|
- return stopIfNeeded(manager, path).then(() => {
|
|
|
|
- return manager.contents.delete(path);
|
|
|
|
- });
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- /**
|
|
|
|
- * Overwrite a file.
|
|
|
|
- *
|
|
|
|
- * @param manager - The service manager used to overwrite.
|
|
|
|
- *
|
|
|
|
- * @param oldPath - The path to the original file.
|
|
|
|
- *
|
|
|
|
- * @param newPath - The path to the new file.
|
|
|
|
- *
|
|
|
|
- * @param basePath - The base path to resolve against, defaults to ''.
|
|
|
|
- *
|
|
|
|
- * @returns A promise containing the new file contents model.
|
|
|
|
- */
|
|
|
|
- export
|
|
|
|
- function overwrite(manager: IServiceManager, oldPath: string, newPath: string, basePath = ''): Promise<Contents.IModel> {
|
|
|
|
- // Cleanly overwrite the file by moving it, making sure the original
|
|
|
|
- // does not exist, and then renaming to the new path.
|
|
|
|
- const tempPath = `${newPath}.${uuid()}`;
|
|
|
|
- const cb = () => rename(manager, tempPath, newPath, basePath);
|
|
|
|
- return rename(manager, oldPath, tempPath, basePath).then(() => {
|
|
|
|
- return deleteFile(manager, newPath);
|
|
|
|
- }).then(cb, cb);
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- /**
|
|
|
|
- * Rename a file or directory.
|
|
|
|
- *
|
|
|
|
- * @param manager - The service manager used to rename.
|
|
|
|
- *
|
|
|
|
- * @param oldPath - The path to the original file.
|
|
|
|
- *
|
|
|
|
- * @param newPath - The path to the new file.
|
|
|
|
- *
|
|
|
|
- * @param basePath - The base path to resolve against, defaults to ''.
|
|
|
|
- *
|
|
|
|
- * @returns A promise containing the new file contents model. The promise
|
|
|
|
- * will reject if the newPath already exists. Use [[overwrite]] to
|
|
|
|
- * overwrite a file.
|
|
|
|
- */
|
|
|
|
- export
|
|
|
|
- function rename(manager: IServiceManager, oldPath: string, newPath: string, basePath = ''): Promise<Contents.IModel> {
|
|
|
|
- // Normalize paths.
|
|
|
|
- oldPath = PathExt.resolve(basePath, oldPath);
|
|
|
|
- newPath = PathExt.resolve(basePath, newPath);
|
|
|
|
-
|
|
|
|
- return manager.contents.rename(oldPath, newPath);
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- /**
|
|
|
|
- * Find a session associated with a path and stop it is the only
|
|
|
|
- * session using that kernel.
|
|
|
|
- */
|
|
|
|
- export
|
|
|
|
- function stopIfNeeded(manager: IServiceManager, path: string): Promise<void> {
|
|
|
|
- return Session.listRunning().then(sessions => {
|
|
|
|
- const matches = sessions.filter(value => value.notebook.path === path);
|
|
|
|
- if (matches.length === 1) {
|
|
|
|
- const id = matches[0].id;
|
|
|
|
- return manager.sessions.shutdown(id).catch(() => { /* no-op */ });
|
|
|
|
- }
|
|
|
|
- }).catch(() => Promise.resolve(void 0)); // Always succeed.
|
|
|
|
- }
|
|
|
|
}
|
|
}
|