Browse Source

Add getUrl to PageConfig to generate master URLs given mode and treePath.

Brian E. Granger 4 years ago
parent
commit
1e8b8ceeed
1 changed files with 43 additions and 0 deletions
  1. 43 0
      packages/coreutils/src/pageconfig.ts

+ 43 - 0
packages/coreutils/src/pageconfig.ts

@@ -132,6 +132,49 @@ export namespace PageConfig {
     return URLExt.normalize(URLExt.join(getShareUrl(), getOption('treeUrl')));
     return URLExt.normalize(URLExt.join(getShareUrl(), getOption('treeUrl')));
   }
   }
 
 
+  /**
+   * Create a new URL given an optional mode and tree path.
+   * 
+   * This is used to create URLS when the mode or tree path change as the user
+   * changes mode or the current document in the main area. If fields in
+   * options are omitted, the value in PageConfig will be used.
+   * 
+   * @param options - IGetUrlOptions for the new path.
+   */
+  export function getUrl(options: IGetUrlOptions): string {
+    let path = getOption('baseUrl') || '/';
+    const mode = options.mode ?? getOption('mode');
+    const labOrDoc = mode === 'multiple-document' ? 'lab' : 'doc';
+    path = URLExt.join(path, labOrDoc);
+    if (getOption('workspace') !== 'default') {
+      path = URLExt.join(path, 'workspaces', getOption('workspace'));
+    }
+    const treePath = options.treePath ?? getOption('treePath');
+    if (treePath) {
+      path = URLExt.join(path, 'tree', treePath)
+    }
+    return path;
+  }
+
+  /**
+   * Options for getUrl
+   */
+  export interface IGetUrlOptions {
+
+    /**
+     * The optional mode as a string 'single-document' or 'multiple-document'. If
+     * the mode argument is missing, it will be provided from the PageConfig.
+     */
+    mode?: string;
+
+    /**
+     * The optional tree path as as string. If treePath is not provided it will be 
+     * provided from the PageConfig. If an empty string, the resulting path will not
+     * contain a tree portion.
+     */
+    treePath?: string;
+  }
+
   /**
   /**
    * Get the base websocket url for a Jupyter application, or an empty string.
    * Get the base websocket url for a Jupyter application, or an empty string.
    */
    */