jupyterlab-emrys
Version:
A computational environment for Jupyter. Powered by Emrys
230 lines (229 loc) • 8.16 kB
JavaScript
// Copyright (c) Jupyter Development Team.
// Distributed under the terms of the Modified BSD License.
"use strict";
var jupyter_js_services_1 = require('jupyter-js-services');
var phosphor_disposable_1 = require('phosphor-disposable');
var context_1 = require('./context');
var widgetmanager_1 = require('./widgetmanager');
/**
* The document manager.
*
* #### Notes
* The document manager is used to register model and widget creators,
* and the file browser uses the document manager to create widgets. The
* document manager maintains a context for each path and model type that is
* open, and a list of widgets for each context. The document manager is in
* control of the proper closing and disposal of the widgets and contexts.
*/
var DocumentManager = (function () {
/**
* Construct a new document manager.
*/
function DocumentManager(options) {
var _this = this;
this._serviceManager = null;
this._contextManager = null;
this._widgetManager = null;
this._registry = null;
this._registry = options.registry;
this._serviceManager = options.manager;
var opener = options.opener;
this._contextManager = new context_1.ContextManager({
manager: this._serviceManager,
opener: function (id, widget) {
_this._widgetManager.adoptWidget(id, widget);
opener.open(widget);
return new phosphor_disposable_1.DisposableDelegate(function () {
widget.close();
});
}
});
this._widgetManager = new widgetmanager_1.DocumentWidgetManager({
contextManager: this._contextManager,
registry: this._registry
});
}
Object.defineProperty(DocumentManager.prototype, "kernelspecs", {
/**
* Get the kernel spec models for the manager.
*
* #### Notes
* This is a read-only property.
*/
get: function () {
return this._serviceManager.kernelspecs;
},
enumerable: true,
configurable: true
});
Object.defineProperty(DocumentManager.prototype, "registry", {
/**
* Get the registry used by the manager.
*
* #### Notes
* This is a read-only property.
*/
get: function () {
return this._registry;
},
enumerable: true,
configurable: true
});
Object.defineProperty(DocumentManager.prototype, "isDisposed", {
/**
* Get whether the document manager has been disposed.
*/
get: function () {
return this._serviceManager === null;
},
enumerable: true,
configurable: true
});
/**
* Dispose of the resources held by the document manager.
*/
DocumentManager.prototype.dispose = function () {
if (this.isDisposed) {
return;
}
this._serviceManager = null;
this._contextManager.dispose();
this._contextManager = null;
this._widgetManager.dispose();
this._widgetManager = null;
};
/**
* Open a file and return the widget used to display the contents.
*
* @param path - The file path to open.
*
* @param widgetName - The name of the widget factory to use. 'default' will use the default widget.
*
* @param kernel - An optional kernel name/id to override the default.
*/
DocumentManager.prototype.open = function (path, widgetName, kernel) {
var _this = this;
if (widgetName === void 0) { widgetName = 'default'; }
var registry = this._registry;
if (widgetName === 'default') {
widgetName = registry.defaultWidgetFactory(jupyter_js_services_1.ContentsManager.extname(path));
}
var mFactory = registry.getModelFactoryFor(widgetName);
if (!mFactory) {
return;
}
// Use an existing context if available.
var id = this._contextManager.findContext(path, mFactory.name);
if (id) {
return this._widgetManager.createWidget(widgetName, id, kernel);
}
var lang = mFactory.preferredLanguage(path);
var model = mFactory.createNew(lang);
id = this._contextManager.createNew(path, model, mFactory);
// Load the contents from disk.
this._contextManager.revert(id).then(function () {
model.dirty = false;
_this._contextManager.finalize(id);
});
return this._widgetManager.createWidget(widgetName, id, kernel);
};
/**
* Create a new file of the given name.
*
* @param path - The file path to use.
*
* @param widgetName - The name of the widget factory to use. 'default' will use the default widget.
*
* @param kernel - An optional kernel name/id to override the default.
*/
DocumentManager.prototype.createNew = function (path, widgetName, kernel) {
var _this = this;
if (widgetName === void 0) { widgetName = 'default'; }
var registry = this._registry;
if (widgetName === 'default') {
widgetName = registry.defaultWidgetFactory(jupyter_js_services_1.ContentsManager.extname(path));
}
var mFactory = registry.getModelFactoryFor(widgetName);
if (!mFactory) {
return;
}
var lang = mFactory.preferredLanguage(path);
var model = mFactory.createNew(lang);
var id = this._contextManager.createNew(path, model, mFactory);
this._contextManager.save(id).then(function () {
model.dirty = false;
_this._contextManager.finalize(id);
});
return this._widgetManager.createWidget(widgetName, id, kernel);
};
/**
* List the running notebook sessions.
*/
DocumentManager.prototype.listSessions = function () {
return this._serviceManager.sessions.listRunning();
};
/**
* Handle the renaming of an open document.
*
* @param oldPath - The previous path.
*
* @param newPath - The new path.
*/
DocumentManager.prototype.handleRename = function (oldPath, newPath) {
this._contextManager.handleRename(oldPath, newPath);
};
/**
* Handle a file deletion.
*/
DocumentManager.prototype.handleDelete = function (path) {
// TODO: Leave all of the widgets open and flag them as orphaned?
};
/**
* See if a widget already exists for the given path and widget name.
*
* @param path - The file path to use.
*
* @param widgetName - The name of the widget factory to use. 'default' will use the default widget.
*
* #### Notes
* This can be used to use an existing widget instead of opening
* a new widget.
*/
DocumentManager.prototype.findWidget = function (path, widgetName) {
if (widgetName === void 0) { widgetName = 'default'; }
if (widgetName === 'default') {
widgetName = this._registry.defaultWidgetFactory(jupyter_js_services_1.ContentsManager.extname(path));
}
return this._widgetManager.findWidget(path, widgetName);
};
/**
* Get the document context for a widget.
*/
DocumentManager.prototype.contextForWidget = function (widget) {
return this._widgetManager.contextForWidget(widget);
};
/**
* Clone a widget.
*
* #### Notes
* This will create a new widget with the same model and context
* as this widget.
*/
DocumentManager.prototype.clone = function (widget) {
return this._widgetManager.clone(widget);
};
/**
* Close the widgets associated with a given path.
*/
DocumentManager.prototype.closeFile = function (path) {
this._widgetManager.closeFile(path);
};
/**
* Close all of the open documents.
*/
DocumentManager.prototype.closeAll = function () {
this._widgetManager.closeAll();
};
return DocumentManager;
}());
exports.DocumentManager = DocumentManager;