123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368 |
- /*-----------------------------------------------------------------------------
- | Copyright (c) Jupyter Development Team.
- | Distributed under the terms of the Modified BSD License.
- |----------------------------------------------------------------------------*/
- import {
- ILayoutRestorer, IRouter, JupyterLab, JupyterLabPlugin
- } from '@jupyterlab/application';
- import {
- ICommandPalette, IThemeManager, ThemeManager, ISplashScreen
- } from '@jupyterlab/apputils';
- import {
- DataConnector, ISettingRegistry, IStateDB, PageConfig, SettingRegistry,
- StateDB, URLExt
- } from '@jupyterlab/coreutils';
- import {
- IMainMenu
- } from '@jupyterlab/mainmenu';
- import {
- ServiceManager
- } from '@jupyterlab/services';
- import {
- each
- } from '@phosphor/algorithm';
- import {
- PromiseDelegate
- } from '@phosphor/coreutils';
- import {
- DisposableDelegate, DisposableSet, IDisposable
- } from '@phosphor/disposable';
- import {
- Menu
- } from '@phosphor/widgets';
- import {
- activatePalette
- } from './palette';
- import '../style/index.css';
- /**
- * The command IDs used by the apputils plugin.
- */
- namespace CommandIDs {
- export
- const changeTheme = 'apputils:change-theme';
- export
- const clearStateDB = 'apputils:clear-statedb';
- export
- const loadState = 'apputils:load-statedb';
- }
- /**
- * A data connector to access plugin settings.
- */
- class SettingsConnector extends DataConnector<ISettingRegistry.IPlugin, string> {
- /**
- * Create a new settings connector.
- */
- constructor(manager: ServiceManager) {
- super();
- this._manager = manager;
- }
- /**
- * Retrieve a saved bundle from the data connector.
- */
- fetch(id: string): Promise<ISettingRegistry.IPlugin> {
- return this._manager.settings.fetch(id).then(data => {
- // Replace the server ID with the original unmodified version.
- data.id = id;
- return data;
- });
- }
- /**
- * Save the user setting data in the data connector.
- */
- save(id: string, raw: string): Promise<void> {
- return this._manager.settings.save(id, raw);
- }
- private _manager: ServiceManager;
- }
- /**
- * The default commmand palette extension.
- */
- const palette: JupyterLabPlugin<ICommandPalette> = {
- activate: activatePalette,
- id: '@jupyterlab/apputils-extension:palette',
- provides: ICommandPalette,
- requires: [ILayoutRestorer],
- autoStart: true
- };
- /**
- * The default setting registry provider.
- */
- const settings: JupyterLabPlugin<ISettingRegistry> = {
- id: '@jupyterlab/apputils-extension:settings',
- activate: (app: JupyterLab): ISettingRegistry => {
- const connector = new SettingsConnector(app.serviceManager);
- return new SettingRegistry({ connector });
- },
- autoStart: true,
- provides: ISettingRegistry
- };
- /**
- * The default theme manager provider.
- */
- const themes: JupyterLabPlugin<IThemeManager> = {
- id: '@jupyterlab/apputils-extension:themes',
- requires: [ISettingRegistry, ISplashScreen],
- optional: [ICommandPalette, IMainMenu],
- activate: (app: JupyterLab, settingRegistry: ISettingRegistry, splash: ISplashScreen, palette: ICommandPalette | null, mainMenu: IMainMenu | null): IThemeManager => {
- const host = app.shell;
- const when = app.started;
- const commands = app.commands;
- const manager = new ThemeManager({
- key: themes.id,
- host, settingRegistry,
- url: app.info.urls.themes,
- splash,
- when
- });
- commands.addCommand(CommandIDs.changeTheme, {
- label: args => {
- const theme = args['theme'] as string;
- return args['isPalette'] ? `Use ${theme} Theme` : theme;
- },
- isToggled: args => args['theme'] === manager.theme,
- execute: args => {
- if (args['theme'] === manager.theme) {
- return;
- }
- manager.setTheme(args['theme'] as string);
- }
- });
- // If we have a main menu, add the theme manager
- // to the settings menu.
- if (mainMenu) {
- const themeMenu = new Menu({ commands });
- themeMenu.title.label = 'JupyterLab Theme';
- manager.ready.then(() => {
- each(manager.themes, theme => {
- themeMenu.addItem({
- command: CommandIDs.changeTheme,
- args: { isPalette: false, theme: theme }
- });
- });
- });
- mainMenu.settingsMenu.addGroup([{
- type: 'submenu' as Menu.ItemType, submenu: themeMenu
- }], 0);
- }
- // If we have a command palette, add theme
- // switching options to it.
- if (palette) {
- const category = 'Settings';
- manager.ready.then(() => {
- each(manager.themes, theme => {
- palette.addItem({
- command: CommandIDs.changeTheme,
- args: { isPalette: true, theme: theme },
- category
- });
- });
- });
- }
- return manager;
- },
- autoStart: true,
- provides: IThemeManager
- };
- /**
- * The default splash screen provider.
- */
- const splash: JupyterLabPlugin<ISplashScreen> = {
- id: '@jupyterlab/apputils-extension:splash',
- autoStart: true,
- provides: ISplashScreen,
- activate: () => ({ show: () => Private.showSplash() })
- };
- /**
- * The default state database for storing application state.
- */
- const state: JupyterLabPlugin<IStateDB> = {
- id: '@jupyterlab/apputils-extension:state',
- autoStart: true,
- provides: IStateDB,
- requires: [IRouter],
- activate: (app: JupyterLab, router: IRouter) => {
- let command: string;
- let resolved = false;
- const { commands, info } = app;
- const transform = new PromiseDelegate<StateDB.DataTransform>();
- const state = new StateDB({
- namespace: info.namespace,
- load: transform.promise
- });
- const disposables = new DisposableSet();
- const pattern = /^\/workspaces\/(.+)/;
- const unload = () => {
- disposables.dispose();
- router.routed.disconnect(unload, state);
- // If the request that was routed did not contain a workspace,
- // leave the database intact.
- if (!resolved) {
- console.log('No workspace requested. Leaving state database intact.');
- transform.resolve({ type: 'cancel', contents: null });
- }
- };
- command = CommandIDs.clearStateDB;
- commands.addCommand(command, {
- label: 'Clear Application Restore State',
- execute: () => state.clear()
- });
- command = CommandIDs.loadState;
- disposables.add(commands.addCommand(command, {
- execute: (args: IRouter.ICommandArgs) => {
- const workspace = (args.path || '').match(pattern)[1];
- const base = URLExt.join(
- PageConfig.getBaseUrl(),
- PageConfig.getOption('pageUrl')
- );
- // Change the URL back to the base application URL.
- window.history.replaceState({ }, '', base);
- // If there is no workspace, leave the state database intact.
- if (!workspace) {
- console.log('No workspace found. Leaving state database intact.');
- resolved = true;
- transform.resolve({ type: 'cancel', contents: null });
- return;
- }
- // Fetch the workspace and overwrite the state database.
- console.log('Fetch the workspace:', workspace);
- resolved = true;
- transform.resolve({ type: 'merge', contents: { } });
- }
- }));
- disposables.add(router.register({ command, pattern }));
- // After the first route in the application lifecycle has been routed,
- // stop listening to routing events.
- router.routed.connect(unload, state);
- return state;
- }
- };
- /**
- * Export the plugins as default.
- */
- const plugins: JupyterLabPlugin<any>[] = [
- palette, settings, state, splash, themes
- ];
- export default plugins;
- /**
- * The namespace for module private data.
- */
- namespace Private {
- /**
- * The splash element.
- */
- let splash: HTMLElement | null;
- /**
- * The splash screen counter.
- */
- let splashCount = 0;
- /**
- * Show the splash element.
- */
- export
- function showSplash(): IDisposable {
- if (!splash) {
- splash = document.createElement('div');
- splash.id = 'jupyterlab-splash';
- let galaxy = document.createElement('div');
- galaxy.id = 'galaxy';
- splash.appendChild(galaxy);
- let mainLogo = document.createElement('div');
- mainLogo.id = 'main-logo';
- let planet = document.createElement('div');
- let planet2 = document.createElement('div');
- let planet3 = document.createElement('div');
- planet.className = 'planet';
- planet2.className = 'planet';
- planet3.className = 'planet';
- let moon1 = document.createElement('div');
- moon1.id = 'moon1';
- moon1.className = 'moon orbit';
- moon1.appendChild(planet);
- let moon2 = document.createElement('div');
- moon2.id = 'moon2';
- moon2.className = 'moon orbit';
- moon2.appendChild(planet2);
- let moon3 = document.createElement('div');
- moon3.id = 'moon3';
- moon3.className = 'moon orbit';
- moon3.appendChild(planet3);
- galaxy.appendChild(mainLogo);
- galaxy.appendChild(moon1);
- galaxy.appendChild(moon2);
- galaxy.appendChild(moon3);
- }
- splash.classList.remove('splash-fade');
- document.body.appendChild(splash);
- splashCount++;
- return new DisposableDelegate(() => {
- splashCount = Math.max(splashCount - 1, 0);
- if (splashCount === 0 && splash) {
- splash.classList.add('splash-fade');
- setTimeout(() => {
- document.body.removeChild(splash);
- }, 500);
- }
- });
- }
- }
|