plugin.ts 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. // Copyright (c) Jupyter Development Team.
  2. // Distributed under the terms of the Modified BSD License.
  3. 'use strict';
  4. import {
  5. Application
  6. } from 'phosphide/lib/core/application';
  7. import {
  8. Widget
  9. } from 'phosphor-widget';
  10. import {
  11. IFrame
  12. } from './iframe';
  13. /**
  14. * The class name added to the help widget.
  15. */
  16. const HELP_CLASS = 'jp-Help';
  17. /**
  18. * A list of commands to add to the help widget.
  19. */
  20. const COMMANDS = [
  21. {
  22. text: 'Scipy Lecture Notes',
  23. id: 'help-doc:scipy-lecture-notes',
  24. url: 'http://www.scipy-lectures.org/'
  25. },
  26. {
  27. text: 'Numpy Reference',
  28. id: 'help-doc:numpy-reference',
  29. url: 'http://docs.scipy.org/doc/numpy/reference/'
  30. },
  31. {
  32. text: 'Scipy Reference',
  33. id: 'help-doc:scipy-reference',
  34. url: 'http://docs.scipy.org/doc/scipy/reference/'
  35. },
  36. {
  37. text: 'Notebook Tutorial',
  38. id: 'help-doc:notebook-tutorial',
  39. url: 'http://nbviewer.jupyter.org/github/jupyter/notebook/' +
  40. 'blob/master/docs/source/examples/Notebook/Notebook Basics.ipynb'
  41. }
  42. ];
  43. /**
  44. * The help handler extension.
  45. */
  46. export
  47. const helpHandlerExtension = {
  48. id: 'jupyter.extensions.helpHandler',
  49. activate: activateHelpHandler
  50. };
  51. /**
  52. * Activate the help handler extension.
  53. *
  54. * @param app - The phosphide application object.
  55. *
  56. * returns A promise that resolves when the extension is activated.
  57. */
  58. function activateHelpHandler(app: Application): Promise<void> {
  59. let widget = new IFrame();
  60. widget.addClass(HELP_CLASS);
  61. widget.title.text = 'Help';
  62. widget.id = 'help-doc';
  63. let helpCommandItems = COMMANDS.map(command => {
  64. return {
  65. id: command.id,
  66. handler: () => {
  67. attachHelp();
  68. showHelp();
  69. widget.loadURL(command.url);
  70. }
  71. };
  72. });
  73. app.commands.add(helpCommandItems);
  74. app.commands.add([
  75. {
  76. id: 'help-doc:activate',
  77. handler: showHelp
  78. },
  79. {
  80. id: 'help-doc:hide',
  81. handler: hideHelp
  82. },
  83. {
  84. id: 'help-doc:toggle',
  85. handler: toggleHelp
  86. },
  87. ]);
  88. let helpPaletteItems = COMMANDS.map(command => {
  89. return {
  90. command: command.id,
  91. text: command.text,
  92. caption: `Open ${command.text}`,
  93. category: 'Help'
  94. };
  95. });
  96. app.palette.add(helpPaletteItems);
  97. return Promise.resolve(void 0);
  98. function attachHelp(): void {
  99. if (!widget.isAttached) app.shell.addToRightArea(widget);
  100. }
  101. function showHelp(): void {
  102. app.shell.activateRight(widget.id);
  103. }
  104. function hideHelp(): void {
  105. if (!widget.isHidden) app.shell.collapseRight();
  106. }
  107. function toggleHelp(): void {
  108. if (widget.isHidden) {
  109. showHelp();
  110. } else {
  111. hideHelp();
  112. }
  113. }
  114. }