UNPKG

jupyterlab-emrys

Version:

A computational environment for Jupyter. Powered by Emrys

273 lines (272 loc) 8.96 kB
// Copyright (c) Jupyter Development Team. // Distributed under the terms of the Modified BSD License. "use strict"; var __extends = (this && this.__extends) || function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; function __() { this.constructor = d; } d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); }; var phosphor_panel_1 = require('phosphor-panel'); var phosphor_widget_1 = require('phosphor-widget'); var buttons_1 = require('./buttons'); var crumbs_1 = require('./crumbs'); var listing_1 = require('./listing'); var utils_1 = require('./utils'); /** * The class name added to the filebrowser crumbs node. */ var CRUMBS_CLASS = 'jp-FileBrowser-crumbs'; /** * The class name added to the filebrowser buttons node. */ var BUTTON_CLASS = 'jp-FileBrowser-buttons'; /** * The class name added to the filebrowser listing node. */ var LISTING_CLASS = 'jp-FileBrowser-listing'; /** * The duration of auto-refresh in ms. */ var REFRESH_DURATION = 10000; /** * A widget which hosts a file browser. * * The widget uses the Jupyter Contents API to retreive contents, * and presents itself as a flat list of files and directories with * breadcrumbs. */ var FileBrowserWidget = (function (_super) { __extends(FileBrowserWidget, _super); /** * Construct a new file browser. * * @param model - The file browser view model. */ function FileBrowserWidget(options) { _super.call(this); this._model = null; this._crumbs = null; this._buttons = null; this._listing = null; this._timeoutId = -1; this._manager = null; this._opener = null; this.addClass(utils_1.FILE_BROWSER_CLASS); var model = this._model = options.model; var manager = this._manager = options.manager; var opener = this._opener = options.opener; model.refreshed.connect(this._handleRefresh, this); this._crumbs = new crumbs_1.BreadCrumbs({ model: model }); this._buttons = new buttons_1.FileButtons({ model: model, manager: manager, opener: opener }); this._listing = new listing_1.DirListing({ model: model, manager: manager, opener: opener, renderer: options.renderer }); model.fileChanged.connect(function (fbModel, args) { if (args.newValue) { manager.handleRename(args.oldValue, args.newValue); } else { manager.handleDelete(args.oldValue); } }); this._crumbs.addClass(CRUMBS_CLASS); this._buttons.addClass(BUTTON_CLASS); this._listing.addClass(LISTING_CLASS); var layout = new phosphor_panel_1.PanelLayout(); layout.addChild(this._buttons); layout.addChild(this._crumbs); layout.addChild(this._listing); this.layout = layout; } Object.defineProperty(FileBrowserWidget.prototype, "model", { /** * Get the model used by the file browser. * * #### Notes * This is a read-only property. */ get: function () { return this._model; }, enumerable: true, configurable: true }); /** * Dispose of the resources held by the file browser. */ FileBrowserWidget.prototype.dispose = function () { this._model = null; this._crumbs = null; this._buttons = null; this._listing = null; this._manager = null; this._opener = null; _super.prototype.dispose.call(this); }; /** * Change directory. */ FileBrowserWidget.prototype.cd = function (path) { return this._model.cd(path); }; /** * Open the currently selected item(s). * * Changes to the first directory encountered. */ FileBrowserWidget.prototype.open = function () { var _this = this; var foundDir = false; var items = this._model.items; for (var _i = 0, items_1 = items; _i < items_1.length; _i++) { var item = items_1[_i]; if (!this._listing.isSelected(item.name)) { continue; } if (item.type === 'directory') { if (!foundDir) { foundDir = true; this._model.cd(item.name).catch(function (error) { return utils_1.showErrorMessage(_this, 'Open directory', error); }); } } else { this.openPath(item.path); } } }; /** * Open a file by path. */ FileBrowserWidget.prototype.openPath = function (path, widgetName) { if (widgetName === void 0) { widgetName = 'default'; } var model = this.model; var widget = this._manager.findWidget(path, widgetName); if (!widget) { widget = this._manager.open(path, widgetName); var context = this._manager.contextForWidget(widget); context.populated.connect(function () { return model.refresh(); }); context.kernelChanged.connect(function () { return model.refresh(); }); } this._opener.open(widget); return widget; }; /** * Create a new untitled file in the current directory. */ FileBrowserWidget.prototype.createNew = function (options) { var _this = this; var model = this.model; return model.newUntitled(options).then(function (contents) { var widget = _this._manager.createNew(contents.path); var context = _this._manager.contextForWidget(widget); context.populated.connect(function () { return model.refresh(); }); context.kernelChanged.connect(function () { return model.refresh(); }); _this._opener.open(widget); return widget; }); }; /** * Rename the first currently selected item. */ FileBrowserWidget.prototype.rename = function () { return this._listing.rename(); }; /** * Cut the selected items. */ FileBrowserWidget.prototype.cut = function () { this._listing.cut(); }; /** * Copy the selected items. */ FileBrowserWidget.prototype.copy = function () { this._listing.copy(); }; /** * Paste the items from the clipboard. */ FileBrowserWidget.prototype.paste = function () { return this._listing.paste(); }; /** * Delete the currently selected item(s). */ FileBrowserWidget.prototype.delete = function () { return this._listing.delete(); }; /** * Duplicate the currently selected item(s). */ FileBrowserWidget.prototype.duplicate = function () { return this._listing.duplicate(); }; /** * Download the currently selected item(s). */ FileBrowserWidget.prototype.download = function () { return this._listing.download().then(function () { return void 0; }); }; /** * Shut down kernels on the applicable currently selected items. */ FileBrowserWidget.prototype.shutdownKernels = function () { return this._listing.shutdownKernels(); }; /** * Refresh the current directory. */ FileBrowserWidget.prototype.refresh = function () { var _this = this; return this._model.refresh().catch(function (error) { utils_1.showErrorMessage(_this, 'Server Connection Error', error); }); }; /** * Select next item. */ FileBrowserWidget.prototype.selectNext = function () { this._listing.selectNext(); }; /** * Select previous item. */ FileBrowserWidget.prototype.selectPrevious = function () { this._listing.selectPrevious(); }; /** * Find a path given a click. */ FileBrowserWidget.prototype.pathForClick = function (event) { return this._listing.pathForClick(event); }; /** * A message handler invoked on an `'after-attach'` message. */ FileBrowserWidget.prototype.onAfterAttach = function (msg) { _super.prototype.onAfterAttach.call(this, msg); this.refresh(); }; /** * A message handler invoked on an `'after-show'` message. */ FileBrowserWidget.prototype.onAfterShow = function (msg) { _super.prototype.onAfterShow.call(this, msg); this.refresh(); }; /** * Handle a model refresh. */ FileBrowserWidget.prototype._handleRefresh = function () { var _this = this; clearTimeout(this._timeoutId); this._timeoutId = setTimeout(function () { return _this.refresh(); }, REFRESH_DURATION); }; return FileBrowserWidget; }(phosphor_widget_1.Widget)); exports.FileBrowserWidget = FileBrowserWidget;