UNPKG

jupyterlab-emrys

Version:

A computational environment for Jupyter. Powered by Emrys

436 lines (435 loc) 13.4 kB
// 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 browser_1 = require('./browser'); var model_1 = require('./model'); var docmanager_1 = require('../docmanager'); var docregistry_1 = require('../docregistry'); var phosphor_menus_1 = require('phosphor-menus'); var phosphor_signaling_1 = require('phosphor-signaling'); var widgettracker_1 = require('../widgettracker'); var plugin_1 = require('../mainmenu/plugin'); /** * A class that tracks the current path of the file browser. */ var PathTracker = (function () { function PathTracker() { } Object.defineProperty(PathTracker.prototype, "pathChanged", { /** * A signal emitted when the current path changes. */ get: function () { return Private.pathChangedSignal.bind(this); }, enumerable: true, configurable: true }); Object.defineProperty(PathTracker.prototype, "path", { /** * The current path of the filebrowser. * * #### Notes * This is a read-only property. */ get: function () { return Private.fbWidget ? Private.fbWidget.model.path : ''; }, enumerable: true, configurable: true }); return PathTracker; }()); exports.PathTracker = PathTracker; /** * The default file browser provider. */ exports.fileBrowserProvider = { id: 'jupyter.services.fileBrowser', provides: PathTracker, resolve: function () { return Private.pathTracker; } }; /** * The default file browser extension. */ exports.fileBrowserExtension = { id: 'jupyter.extensions.fileBrowser', requires: [jupyter_js_services_1.ServiceManager, docregistry_1.DocumentRegistry, plugin_1.MainMenu], activate: activateFileBrowser }; /** * The class name for all main area portrait tab icons. */ var PORTRAIT_ICON_CLASS = 'jp-MainAreaPortraitIcon'; /** * The class name for the notebook icon from the default theme. */ var NOTEBOOK_ICON_CLASS = 'jp-ImageNotebook'; /** * The class name for the text editor icon from the default theme. */ var TEXTEDITOR_ICON_CLASS = 'jp-ImageTextEditor'; /** * Activate the file browser. */ function activateFileBrowser(app, manager, registry, mainMenu) { var id = 0; var tracker = new widgettracker_1.WidgetTracker(); var activeWidget; tracker.activeWidgetChanged.connect(function (sender, widget) { activeWidget = widget; }); var docManager; var opener = { open: function (widget) { if (!widget.id) { widget.id = "document-manager-" + ++id; } if (!widget.isAttached) { app.shell.addToMainArea(widget); tracker.addWidget(widget); } } }; docManager = new docmanager_1.DocumentManager({ registry: registry, manager: manager, opener: opener }); var fbModel = new model_1.FileBrowserModel({ manager: manager }); var fbWidget = Private.fbWidget = new browser_1.FileBrowserWidget({ model: fbModel, manager: docManager, opener: opener }); fbModel.pathChanged.connect(function (sender, args) { Private.pathTracker.pathChanged.emit(args); }); // Add a context menu to the dir listing. var node = fbWidget.node.getElementsByClassName('jp-DirListing-content')[0]; node.addEventListener('contextmenu', function (event) { event.preventDefault(); var x = event.clientX; var y = event.clientY; var path = fbWidget.pathForClick(event); var ext = '.' + path.split('.').pop(); var widgetNames = registry.listWidgetFactories(ext); var items = []; if (widgetNames.length > 1) { var _loop_1 = function(widgetName) { items.push(new phosphor_menus_1.MenuItem({ text: widgetName, handler: function () { fbWidget.openPath(path, widgetName); } })); }; for (var _i = 0, widgetNames_1 = widgetNames; _i < widgetNames_1.length; _i++) { var widgetName = widgetNames_1[_i]; _loop_1(widgetName); } } var menu = createMenu(fbWidget, items); menu.popup(x, y); }); // Add the command for a new items. var newTextFileId = 'file-operations:new-text-file'; app.commands.add([ { id: newTextFileId, handler: function () { var icon = PORTRAIT_ICON_CLASS + " " + TEXTEDITOR_ICON_CLASS; fbWidget.createNew({ type: 'file' }).then(function (widget) { return widget.title.icon = icon; }); } } ]); var newNotebookId = 'file-operations:new-notebook'; app.commands.add([ { id: newNotebookId, handler: function () { var icon = PORTRAIT_ICON_CLASS + " " + NOTEBOOK_ICON_CLASS; fbWidget.createNew({ type: 'notebook' }).then(function (widget) { widget.title.icon = icon; }); } } ]); // Add the command for saving a document. var saveDocumentId = 'file-operations:save'; app.commands.add([ { id: saveDocumentId, handler: function () { if (activeWidget) { var context = docManager.contextForWidget(activeWidget); context.save(); } } } ]); app.palette.add([ { command: saveDocumentId, category: 'File Operations', text: 'Save Document', caption: 'Save the current document' } ]); // Add the command for reverting a document. var revertDocumentId = 'file-operations:revert'; app.commands.add([ { id: revertDocumentId, handler: function () { if (activeWidget) { var context = docManager.contextForWidget(activeWidget); context.revert(); } } } ]); app.palette.add([ { command: revertDocumentId, category: 'File Operations', text: 'Revert Document', caption: 'Revert the current document' } ]); // Add the command for saving a document with a new name. var saveDocumentAsId = 'file-operations:saveas'; app.commands.add([ { id: saveDocumentAsId, handler: function () { if (activeWidget) { var context = docManager.contextForWidget(activeWidget); context.saveAs().then(function () { fbModel.refresh(); }); } } } ]); app.palette.add([ { command: saveDocumentAsId, category: 'File Operations', text: 'Save As...', caption: 'Save the current document as...' } ]); // Add the command for closing a document. var closeDocumentId = 'file-operations:close'; app.commands.add([ { id: closeDocumentId, handler: function () { if (activeWidget) { activeWidget.close(); } } } ]); app.palette.add([ { command: closeDocumentId, category: 'File Operations', text: 'Close Document', caption: 'Close the current document' } ]); // Add the command for closing all documents. var closeAllId = 'file-operations:close-all'; app.commands.add([ { id: closeAllId, handler: function () { docManager.closeAll(); } } ]); app.palette.add([ { command: closeAllId, category: 'File Operations', text: 'Close All', caption: 'Close all open documents' } ]); app.palette.add([ { command: newTextFileId, category: 'File Operations', text: 'New Text File', caption: 'Create a new text file' }, { command: newNotebookId, category: 'File Operations', text: 'New Notebook', caption: 'Create a new notebook' } ]); app.commands.add([ { id: 'file-browser:activate', handler: showBrowser }, { id: 'file-browser:hide', handler: hideBrowser }, { id: 'file-browser:toggle', handler: toggleBrowser } ]); fbWidget.title.text = 'Files'; fbWidget.id = 'file-browser'; app.shell.addToLeftArea(fbWidget, { rank: 40 }); showBrowser(); // Adding Top Menu var newSubMenu = new phosphor_menus_1.Menu([ new phosphor_menus_1.MenuItem({ text: 'Notebook', handler: function () { app.commands.execute(newNotebookId); } }), new phosphor_menus_1.MenuItem({ text: 'Text File', handler: function () { app.commands.execute(newTextFileId); } }) ]); var menu = new phosphor_menus_1.Menu([ new phosphor_menus_1.MenuItem({ text: 'New', submenu: newSubMenu }), new phosphor_menus_1.MenuItem({ text: 'Save Document', handler: function () { app.commands.execute(saveDocumentId); } }), new phosphor_menus_1.MenuItem({ text: 'Save Document As...', handler: function () { app.commands.execute(saveDocumentAsId); } }), new phosphor_menus_1.MenuItem({ text: 'Revert Document', handler: function () { app.commands.execute(revertDocumentId); } }), new phosphor_menus_1.MenuItem({ text: 'Close Current', handler: function () { app.commands.execute(closeDocumentId); } }), new phosphor_menus_1.MenuItem({ text: 'Close All', handler: function () { app.commands.execute(closeAllId); } }), ]); var fileMenu = new phosphor_menus_1.MenuItem({ text: 'File', submenu: menu }); mainMenu.addItem(fileMenu, { rank: 1 }); function showBrowser() { app.shell.activateLeft(fbWidget.id); } function hideBrowser() { if (!fbWidget.isHidden) { app.shell.collapseLeft(); } } function toggleBrowser() { if (fbWidget.isHidden) { showBrowser(); } else { hideBrowser(); } } } /** * Create a context menu for the file browser listing. */ function createMenu(fbWidget, openWith) { var items = [ new phosphor_menus_1.MenuItem({ text: '&Open', icon: 'fa fa-folder-open-o', shortcut: 'Ctrl+O', handler: function () { fbWidget.open(); } }) ]; if (openWith.length) { items.push(new phosphor_menus_1.MenuItem({ text: 'Open With...', submenu: new phosphor_menus_1.Menu(openWith) })); } items.push(new phosphor_menus_1.MenuItem({ text: '&Rename', icon: 'fa fa-edit', shortcut: 'Ctrl+R', handler: function () { fbWidget.rename(); } }), new phosphor_menus_1.MenuItem({ text: '&Delete', icon: 'fa fa-remove', shortcut: 'Ctrl+D', handler: function () { fbWidget.delete(); } }), new phosphor_menus_1.MenuItem({ text: 'Duplicate', icon: 'fa fa-copy', handler: function () { fbWidget.duplicate(); } }), new phosphor_menus_1.MenuItem({ text: 'Cut', icon: 'fa fa-cut', shortcut: 'Ctrl+X', handler: function () { fbWidget.cut(); } }), new phosphor_menus_1.MenuItem({ text: '&Copy', icon: 'fa fa-copy', shortcut: 'Ctrl+C', handler: function () { fbWidget.copy(); } }), new phosphor_menus_1.MenuItem({ text: '&Paste', icon: 'fa fa-paste', shortcut: 'Ctrl+V', handler: function () { fbWidget.paste(); } }), new phosphor_menus_1.MenuItem({ text: 'Download', icon: 'fa fa-download', handler: function () { fbWidget.download(); } }), new phosphor_menus_1.MenuItem({ text: 'Shutdown Kernel', icon: 'fa fa-stop-circle-o', handler: function () { fbWidget.shutdownKernels(); } })); return new phosphor_menus_1.Menu(items); } /** * A namespace for private data. */ var Private; (function (Private) { Private.pathTracker = new PathTracker(); /** * A signal emitted when the current working directory changes. */ Private.pathChangedSignal = new phosphor_signaling_1.Signal(); })(Private || (Private = {}));