tokens.ts 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. // Copyright (c) Jupyter Development Team.
  2. // Distributed under the terms of the Modified BSD License.
  3. import { ServerConnection, ServiceManager } from '@jupyterlab/services';
  4. import { ITranslator } from '@jupyterlab/translation';
  5. import { CommandRegistry } from '@lumino/commands';
  6. import { ReadonlyPartialJSONObject, Token } from '@lumino/coreutils';
  7. import { IDisposable } from '@lumino/disposable';
  8. import { ISignal } from '@lumino/signaling';
  9. /**
  10. * A token for which a plugin can provide to respond to connection failures
  11. * to the application server.
  12. */
  13. export const IConnectionLost = new Token<IConnectionLost>(
  14. '@jupyterlab/apputils:IConnectionLost'
  15. );
  16. /**
  17. * A function that handles a connection to the server being lost.
  18. *
  19. * #### Notes
  20. * The default implementation shows a simple dialog upon connection
  21. * failures, but it may be overridden by extensions to perform other
  22. * actions.
  23. */
  24. export type IConnectionLost = (
  25. manager: ServiceManager.IManager,
  26. err: ServerConnection.NetworkError,
  27. translator?: ITranslator
  28. ) => Promise<void>;
  29. /**
  30. * The URL Router token.
  31. */
  32. export const IRouter = new Token<IRouter>('@jupyterlab/application:IRouter');
  33. /**
  34. * A static class that routes URLs within the application.
  35. */
  36. export interface IRouter {
  37. /**
  38. * The base URL for the router.
  39. */
  40. readonly base: string;
  41. /**
  42. * The command registry used by the router.
  43. */
  44. readonly commands: CommandRegistry;
  45. /**
  46. * The parsed current URL of the application.
  47. */
  48. readonly current: IRouter.ILocation;
  49. /**
  50. * A signal emitted when the router routes a route.
  51. */
  52. readonly routed: ISignal<IRouter, IRouter.ILocation>;
  53. /**
  54. * If a matching rule's command resolves with the `stop` token during routing,
  55. * no further matches will execute.
  56. */
  57. readonly stop: Token<void>;
  58. /**
  59. * Navigate to a new path within the application.
  60. *
  61. * @param path - The new path or empty string if redirecting to root.
  62. *
  63. * @param options - The navigation options.
  64. */
  65. navigate(path: string, options?: IRouter.INavOptions): void;
  66. /**
  67. * Register a rule that maps a path pattern to a command.
  68. *
  69. * @param options - The route registration options.
  70. *
  71. * @returns A disposable that removes the registered rule from the router.
  72. */
  73. register(options: IRouter.IRegisterOptions): IDisposable;
  74. /**
  75. * Cause a hard reload of the document.
  76. */
  77. reload(): void;
  78. /**
  79. * Route a specific path to an action.
  80. *
  81. * @param url - The URL string that will be routed.
  82. *
  83. * #### Notes
  84. * If a pattern is matched, its command will be invoked with arguments that
  85. * match the `IRouter.ILocation` interface.
  86. */
  87. route(url: string): void;
  88. }
  89. /**
  90. * A namespace for the `IRouter` specification.
  91. */
  92. export namespace IRouter {
  93. /**
  94. * The parsed location currently being routed.
  95. */
  96. export interface ILocation extends ReadonlyPartialJSONObject {
  97. /**
  98. * The location hash.
  99. */
  100. hash: string;
  101. /**
  102. * The path that matched a routing pattern.
  103. */
  104. path: string;
  105. /**
  106. * The request being routed with the router `base` omitted.
  107. *
  108. * #### Notes
  109. * This field includes the query string and hash, if they exist.
  110. */
  111. request: string;
  112. /**
  113. * The search element, including leading question mark (`'?'`), if any,
  114. * of the path.
  115. */
  116. search?: string;
  117. }
  118. /**
  119. * The options passed into a navigation request.
  120. */
  121. export interface INavOptions {
  122. /**
  123. * Whether the navigation should be hard URL change instead of an HTML
  124. * history API change.
  125. */
  126. hard?: boolean;
  127. /**
  128. * Should the routing stage be skipped when navigating? This will simply rewrite the URL
  129. * and push the new state to the history API, no routing commands will be triggered.
  130. */
  131. skipRouting?: boolean;
  132. }
  133. /**
  134. * The specification for registering a route with the router.
  135. */
  136. export interface IRegisterOptions {
  137. /**
  138. * The command string that will be invoked upon matching.
  139. */
  140. command: string;
  141. /**
  142. * The regular expression that will be matched against URLs.
  143. */
  144. pattern: RegExp;
  145. /**
  146. * The rank order of the registered rule. A lower rank denotes a higher
  147. * priority. The default rank is `100`.
  148. */
  149. rank?: number;
  150. }
  151. }