Browse Source

Work in progress: help refactor + state saving.

Afshin Darian 8 years ago
parent
commit
f47dc76ae5
1 changed files with 61 additions and 59 deletions
  1. 61 59
      src/help/plugin.ts

+ 61 - 59
src/help/plugin.ts

@@ -29,6 +29,10 @@ import {
   IMainMenu
 } from '../mainmenu';
 
+import {
+  IStateDB
+} from '../statedb';
+
 
 /**
  * The class name added to the help widget.
@@ -101,7 +105,7 @@ const COMMANDS = [
 export
 const helpHandlerExtension: JupyterLabPlugin<void> = {
   id: 'jupyter.extensions.help-handler',
-  requires: [IMainMenu, ICommandPalette],
+  requires: [IMainMenu, ICommandPalette, IStateDB],
   activate: activateHelpHandler,
   autoStart: true
 };
@@ -114,60 +118,18 @@ const helpHandlerExtension: JupyterLabPlugin<void> = {
  *
  * returns A promise that resolves when the extension is activated.
  */
-function activateHelpHandler(app: JupyterLab, mainMenu: IMainMenu, palette: ICommandPalette): Promise<void> {
-  let iframe = new IFrame();
+function activateHelpHandler(app: JupyterLab, mainMenu: IMainMenu, palette: ICommandPalette, state: IStateDB): void {
+  const category = 'Help';
+  const namespace = 'help-doc';
+  const key = `${namespace}:show`;
+  const iframe = new IFrame();
   iframe.addClass(HELP_CLASS);
   iframe.title.label = 'Help';
-  iframe.id = 'help-doc';
-
-  COMMANDS.forEach(command => app.commands.addCommand(command.id, {
-    label: command.text,
-    execute: () => {
-      Private.attachHelp(app, iframe);
-      Private.showHelp(app, iframe);
-      iframe.loadURL(command.url);
-    }
-  }));
-
-  app.commands.addCommand('help-doc:activate', {
-    execute: () => { Private.showHelp(app, iframe); }
-  });
-  app.commands.addCommand('help-doc:hide', {
-    execute: () => { Private.hideHelp(app, iframe); }
-  });
-  app.commands.addCommand('help-doc:toggle', {
-    execute: () => { Private.toggleHelp(app, iframe); }
-  });
-
-  COMMANDS.forEach(item => palette.addItem({
-    command: item.id,
-    category: 'Help'
-  }));
-
-  let openClassicNotebookId = 'classic-notebook:open';
-  app.commands.addCommand(openClassicNotebookId, {
-    label: 'Open Classic Notebook',
-    execute: () => {
-      window.open(utils.getBaseUrl() + 'tree');
-    }
-  });
-  palette.addItem({ command: openClassicNotebookId, category: 'Help'});
-
-  let menu = Private.createMenu(app);
-  mainMenu.addMenu(menu, {});
-
-  return Promise.resolve(void 0);
-}
-
+  iframe.id = namespace;
 
-/**
- * A namespace for help plugin private functions.
- */
-namespace Private {
   /**
    * Creates a menu for the help plugin.
    */
-  export
   function createMenu(app: JupyterLab): Menu {
     let { commands, keymap } = app;
     let menu = new Menu({ commands, keymap });
@@ -187,8 +149,7 @@ namespace Private {
   /**
    * Attach the help iframe widget to the application shell.
    */
-  export
-  function attachHelp(app: JupyterLab, iframe: Widget): void {
+  function attachHelp(): void {
     if (!iframe.isAttached) {
       app.shell.addToRightArea(iframe);
     }
@@ -197,30 +158,71 @@ namespace Private {
   /**
    * Show the help widget.
    */
-  export
-  function showHelp(app: JupyterLab, iframe: Widget): void {
+  function showHelp(): void {
     app.shell.activateRight(iframe.id);
+    state.save(key, { url: iframe.url });
   }
 
   /**
    * Hide the help widget.
    */
-  export
-  function hideHelp(app: JupyterLab, iframe: Widget): void {
+  function hideHelp(): void {
     if (!iframe.isHidden) {
       app.shell.collapseRight();
+      state.remove(key);
     }
   }
 
   /**
    * Toggle whether the help widget is shown or hidden.
    */
-  export
-  function toggleHelp(app: JupyterLab, iframe: Widget): void {
+  function toggleHelp(): void {
     if (iframe.isHidden) {
-      showHelp(app, iframe);
+      showHelp();
     } else {
-      hideHelp(app, iframe);
+      hideHelp();
     }
   }
+
+  COMMANDS.forEach(command => app.commands.addCommand(command.id, {
+    label: command.text,
+    execute: () => {
+      attachHelp();
+      iframe.url = command.url;
+      showHelp();
+    }
+  }));
+
+  app.commands.addCommand('help-doc:activate', {
+    execute: () => { showHelp(); }
+  });
+  app.commands.addCommand('help-doc:hide', {
+    execute: () => { hideHelp(); }
+  });
+  app.commands.addCommand('help-doc:toggle', {
+    execute: () => { toggleHelp(); }
+  });
+
+  COMMANDS.forEach(item => palette.addItem({ command: item.id, category }));
+
+  let openClassicNotebookId = 'classic-notebook:open';
+  app.commands.addCommand(openClassicNotebookId, {
+    label: 'Open Classic Notebook',
+    execute: () => {
+      window.open(utils.getBaseUrl() + 'tree');
+    }
+  });
+  palette.addItem({ command: openClassicNotebookId, category });
+
+  let menu = createMenu(app);
+  mainMenu.addMenu(menu, {});
+
+  state.fetch(key).then(args => {
+    let url = args['url'] as string;
+    let filtered = COMMANDS.filter(command => command.url === url);
+    if (filtered.length) {
+      let command = filtered[0];
+      app.commands.execute(command.id, void 0);
+    }
+  });
 }