浏览代码

Merge pull request #572 from afshin/559

Refactor help plugin.
Steven Silvester 8 年之前
父节点
当前提交
0b7ee61cd3
共有 1 个文件被更改,包括 73 次插入55 次删除
  1. 73 55
      src/help/plugin.ts

+ 73 - 55
src/help/plugin.ts

@@ -5,18 +5,22 @@ import {
   Application
 } from 'phosphide/lib/core/application';
 
+import {
+  MenuItem, Menu
+} from 'phosphor-menus';
+
+import {
+  Widget
+} from 'phosphor-widget';
+
 import {
   IFrame
 } from '../iframe';
 
 import {
-  MainMenu, mainMenuProvider
+  MainMenu
 } from '../mainmenu/plugin';
 
-import {
-  MenuItem, Menu, IMenuItemOptions, MenuItemType
-} from 'phosphor-menus';
-
 
 
 /**
@@ -38,28 +42,28 @@ const COMMANDS = [
   {
     text: 'Numpy Reference',
     id: 'help-doc:numpy-reference',
-    url: 'http://docs.scipy.org/doc/numpy/reference/'
+    url: '//docs.scipy.org/doc/numpy/reference/'
   },
   {
     text: 'Scipy Reference',
     id: 'help-doc:scipy-reference',
-    url: 'http://docs.scipy.org/doc/scipy/reference/'
+    url: '//docs.scipy.org/doc/scipy/reference/'
   },
   {
     text: 'Notebook Tutorial',
     id: 'help-doc:notebook-tutorial',
-    url: 'http://nbviewer.jupyter.org/github/jupyter/notebook/' +
+    url: '//nbviewer.jupyter.org/github/jupyter/notebook/' +
       'blob/master/docs/source/examples/Notebook/Notebook Basics.ipynb'
   },
   {
     text: 'Python Reference',
     id: 'help-doc:python-reference',
-    url: 'https://docs.python.org/3.5/'
+    url: '//docs.python.org/3.5/'
   },
   {
     text: 'IPython Reference',
     id: 'help-doc:ipython-reference',
-    url: 'http://ipython.org/documentation.html?v=20160707164940'
+    url: '//ipython.org/documentation.html?v=20160707164940'
   },
   {
     text: 'Matplotlib Reference',
@@ -79,7 +83,7 @@ const COMMANDS = [
   {
     text: 'Markdown Reference',
     id: 'help-doc:markdown-reference',
-    url: 'https://help.github.com/articles/getting-started-with-writing-and-formatting-on-github/'
+    url: '//help.github.com/articles/getting-started-with-writing-and-formatting-on-github/'
   }
 ];
 
@@ -89,7 +93,7 @@ const COMMANDS = [
  */
 export
 const helpHandlerExtension = {
-  id: 'jupyter.extensions.helpHandler',
+  id: 'jupyter.extensions.help-handler',
   requires: [MainMenu],
   activate: activateHelpHandler
 };
@@ -103,18 +107,18 @@ const helpHandlerExtension = {
  * returns A promise that resolves when the extension is activated.
  */
 function activateHelpHandler(app: Application, mainMenu: MainMenu): Promise<void> {
-  let widget = new IFrame();
-  widget.addClass(HELP_CLASS);
-  widget.title.text = 'Help';
-  widget.id = 'help-doc';
+  let iframe = new IFrame();
+  iframe.addClass(HELP_CLASS);
+  iframe.title.text = 'Help';
+  iframe.id = 'help-doc';
 
   let helpCommandItems = COMMANDS.map(command => {
     return {
       id: command.id,
       handler: () => {
-        attachHelp();
-        showHelp();
-        widget.loadURL(command.url);
+        Private.attachHelp(app, iframe);
+        Private.showHelp(app, iframe);
+        iframe.loadURL(command.url);
       }
     };
   });
@@ -124,15 +128,15 @@ function activateHelpHandler(app: Application, mainMenu: MainMenu): Promise<void
   app.commands.add([
     {
       id: 'help-doc:activate',
-      handler: showHelp
+      handler: () => { Private.showHelp(app, iframe); }
     },
     {
       id: 'help-doc:hide',
-      handler: hideHelp
+      handler: () => { Private.hideHelp(app, iframe); }
     },
     {
       id: 'help-doc:toggle',
-      handler: toggleHelp
+      handler: () => { Private.toggleHelp(app, iframe); }
     }
   ]);
 
@@ -147,8 +151,6 @@ function activateHelpHandler(app: Application, mainMenu: MainMenu): Promise<void
 
   app.palette.add(helpPaletteItems);
 
-
-
   let menu = new Menu([
     new MenuItem({
       text: 'About JupyterLab',
@@ -165,8 +167,7 @@ function activateHelpHandler(app: Application, mainMenu: MainMenu): Promise<void
     new MenuItem({
       text: 'Notebook Tutorial',
       handler: () => {
-        handleMenu('http://nbviewer.jupyter.org/github/jupyter/notebook/' +
-        'blob/master/docs/source/examples/Notebook/Notebook Basics.ipynb');
+        app.commands.execute('help-doc:notebook-tutorial');
       }
     }),
     new MenuItem({
@@ -175,92 +176,109 @@ function activateHelpHandler(app: Application, mainMenu: MainMenu): Promise<void
     new MenuItem({
       text: 'IPython Reference',
       handler: () => {
-        handleMenu('http://ipython.org/documentation.html?v=20160707164940');
+        app.commands.execute('help-doc:ipython-reference');
       }
     }),
     new MenuItem({
       text: 'Markdown Reference',
       handler: () => {
-        handleMenu('http://help.github.com/articles/getting-started-with-writing-and-formatting-on-github/');
+        app.commands.execute('help-doc:markdown-reference');
       }
     }),
     new MenuItem({
       text: 'Matplotlib Reference',
       handler: () => {
-        handleMenu('http://matplotlib.org/contents.html?v=20160707164940');
+        app.commands.execute('help-doc:mathplotlib-reference');
       }
     }),
     new MenuItem({
       text: 'Numpy Reference',
       handler: () => {
-        handleMenu('http://docs.scipy.org/doc/numpy/reference/');
+        app.commands.execute('help-doc:numpy-reference');
       }
     }),
     new MenuItem({
       text: 'Pandas Reference',
       handler: () => {
-        handleMenu('http://pandas.pydata.org/pandas-docs/stable/?v=20160707164940');
+        app.commands.execute('help-doc:pandas-reference');
       }
     }),
     new MenuItem({
       text: 'Python Reference',
       handler: () => {
-        handleMenu('https://docs.python.org/3.5/');
+        app.commands.execute('help-doc:python-reference');
       }
     }),
     new MenuItem({
       text: 'Scipy Lecture Notes',
       handler: () => {
-        handleMenu('http://www.scipy-lectures.org/');
+        app.commands.execute('help-doc:scipy-lecture-notes');
       }
     }),
     new MenuItem({
       text: 'Scipy Reference',
       handler: () => {
-        handleMenu('http://docs.scipy.org/doc/scipy/reference/');
+        app.commands.execute('help-doc:scipy-reference');
       }
     }),
     new MenuItem({
       text: 'SymPy Reference',
       handler: () => {
-        handleMenu('http://docs.sympy.org/latest/index.html?v=20160707164940');
+        app.commands.execute('help-doc:sympy-reference');
       }
     })
   ]);
 
 
-  let helpMenu = new MenuItem ({
-    text: 'Help',
-    submenu: menu
-  });
+  let helpMenu = new MenuItem({ text: 'Help', submenu: menu });
 
   mainMenu.addItem(helpMenu);
 
   return Promise.resolve(void 0);
+}
 
-  function attachHelp(): void {
-    if (!widget.isAttached) app.shell.addToRightArea(widget);
-  }
 
-  function showHelp(): void {
-    app.shell.activateRight(widget.id);
+/**
+ * A namespace for help plugin private functions.
+ */
+namespace Private {
+  /**
+   * Attach the help iframe widget to the application shell.
+   */
+  export
+  function attachHelp(app: Application, iframe: Widget): void {
+    if (!iframe.isAttached) {
+      app.shell.addToRightArea(iframe);
+    }
   }
 
-  function hideHelp(): void {
-    if (!widget.isHidden) app.shell.collapseRight();
+  /**
+   * Show the help widget.
+   */
+  export
+  function showHelp(app: Application, iframe: Widget): void {
+    app.shell.activateRight(iframe.id);
   }
 
-  function toggleHelp(): void {
-    if (widget.isHidden) {
-      showHelp();
-    } else {
-      hideHelp();
+  /**
+   * Hide the help widget.
+   */
+  export
+  function hideHelp(app: Application, iframe: Widget): void {
+    if (!iframe.isHidden) {
+      app.shell.collapseRight();
     }
   }
 
-  function handleMenu(url: string): void {
-    attachHelp();
-    showHelp();
-    widget.loadURL(url);
+  /**
+   * Toggle whether the help widget is shown or hidden.
+   */
+  export
+  function toggleHelp(app: Application, iframe: Widget): void {
+    if (iframe.isHidden) {
+      showHelp(app, iframe);
+    } else {
+      hideHelp(app, iframe);
+    }
   }
 }