Browse Source

Do some type consolidation, handle defaults using spread operator, name
status items to be the same as their plugin id.

Ian Rose 6 years ago
parent
commit
debe735e01

+ 12 - 8
packages/codemirror-extension/src/index.ts

@@ -83,14 +83,18 @@ export const editorSyntaxStatus: JupyterLabPlugin<void> = {
         >).content.editor;
       }
     });
-    statusBar.registerStatusItem('editor-syntax-item', item, {
-      align: 'left',
-      rank: 0,
-      isActive: () =>
-        app.shell.currentWidget &&
-        tracker.currentWidget &&
-        app.shell.currentWidget === tracker.currentWidget
-    });
+    statusBar.registerStatusItem(
+      '@jupyterlab/codemirror-extension:editor-syntax-status',
+      {
+        item,
+        align: 'left',
+        rank: 0,
+        isActive: () =>
+          app.shell.currentWidget &&
+          tracker.currentWidget &&
+          app.shell.currentWidget === tracker.currentWidget
+      }
+    );
   }
 };
 

+ 21 - 13
packages/docmanager-extension/src/index.ts

@@ -179,13 +179,17 @@ export const savingStatusPlugin: JupyterLabPlugin<void> = {
       () => (item.model!.widget = app.shell.currentWidget)
     );
 
-    statusBar.registerStatusItem('saving-status', item, {
-      align: 'middle',
-      isActive: () => {
-        return true;
-      },
-      stateChanged: item.model!.stateChanged
-    });
+    statusBar.registerStatusItem(
+      '@jupyterlab.docmanager-extension:saving-status',
+      {
+        item,
+        align: 'middle',
+        isActive: () => {
+          return true;
+        },
+        activeStateChanged: item.model!.stateChanged
+      }
+    );
   }
 };
 
@@ -209,13 +213,17 @@ export const pathStatusPlugin: JupyterLabPlugin<void> = {
       item.model!.widget = app.shell.currentWidget;
     });
 
-    statusBar.registerStatusItem('path-status', item, {
-      align: 'right',
-      rank: 0,
-      isActive: () => {
-        return true;
+    statusBar.registerStatusItem(
+      '@jupyterlab/docmanager-extension:path-status',
+      {
+        item,
+        align: 'right',
+        rank: 0,
+        isActive: () => {
+          return true;
+        }
       }
-    });
+    );
   }
 };
 

+ 12 - 8
packages/filebrowser-extension/src/uploadstatus.tsx

@@ -243,7 +243,7 @@ interface IFileUploadItem {
  * A plugin providing file upload status.
  */
 export const fileUploadStatus: JupyterLabPlugin<void> = {
-  id: '@jupyterlab/filebrowser-extension:file-upload-item',
+  id: '@jupyterlab/filebrowser-extension:file-upload-status',
   autoStart: true,
   requires: [IStatusBar, IFileBrowserFactory],
   activate: (
@@ -255,12 +255,16 @@ export const fileUploadStatus: JupyterLabPlugin<void> = {
       tracker: browser.tracker
     });
 
-    statusBar.registerStatusItem('file-upload-item', item, {
-      align: 'middle',
-      isActive: () => {
-        return !!item.model && item.model.items.length > 0;
-      },
-      stateChanged: item.model.stateChanged
-    });
+    statusBar.registerStatusItem(
+      '@jupyterlab/filebrowser-extension:file-upload-status',
+      {
+        item,
+        align: 'middle',
+        isActive: () => {
+          return !!item.model && item.model.items.length > 0;
+        },
+        activeStateChanged: item.model.stateChanged
+      }
+    );
   }
 };

+ 14 - 9
packages/fileeditor-extension/src/index.ts

@@ -111,7 +111,7 @@ const plugin: JupyterLabPlugin<IEditorTracker> = {
  * switch tabs vs spaces and tab widths for text editors.
  */
 export const tabSpaceStatus: JupyterLabPlugin<void> = {
-  id: '@jupyterlab/fileeditor-extension:tab-space-item',
+  id: '@jupyterlab/fileeditor-extension:tab-space-status',
   autoStart: true,
   requires: [IStatusBar, IEditorTracker, ISettingRegistry],
   activate: (
@@ -161,15 +161,20 @@ export const tabSpaceStatus: JupyterLabPlugin<void> = {
     });
 
     // Add the status item.
-    statusBar.registerStatusItem('tab-space-item', item, {
-      align: 'right',
-      rank: 1,
-      isActive: () => {
-        return (
-          app.shell.currentWidget && editorTracker.has(app.shell.currentWidget)
-        );
+    statusBar.registerStatusItem(
+      '@jupyterlab/fileeditor-extension:tab-space-status',
+      {
+        item,
+        align: 'right',
+        rank: 1,
+        isActive: () => {
+          return (
+            app.shell.currentWidget &&
+            editorTracker.has(app.shell.currentWidget)
+          );
+        }
       }
-    });
+    );
   }
 };
 

+ 14 - 9
packages/notebook-extension/src/index.ts

@@ -305,7 +305,8 @@ export const commandEditItem: JupyterLabPlugin<void> = {
       item.model.notebook = current && current.content;
     });
 
-    statusBar.registerStatusItem('command-edit-item', item, {
+    statusBar.registerStatusItem('@jupyterlab/notebook-extension:mode-status', {
+      item,
       align: 'right',
       rank: 4,
       isActive: () =>
@@ -336,14 +337,18 @@ export const notebookTrustItem: JupyterLabPlugin<void> = {
       item.model.notebook = current && current.content;
     });
 
-    statusBar.registerStatusItem('notebook-trust-item', item, {
-      align: 'right',
-      rank: 3,
-      isActive: () =>
-        app.shell.currentWidget &&
-        tracker.currentWidget &&
-        app.shell.currentWidget === tracker.currentWidget
-    });
+    statusBar.registerStatusItem(
+      '@jupyterlab/notebook-extension:trust-status',
+      {
+        item,
+        align: 'right',
+        rank: 3,
+        isActive: () =>
+          app.shell.currentWidget &&
+          tracker.currentWidget &&
+          app.shell.currentWidget === tracker.currentWidget
+      }
+    );
   }
 };
 

+ 52 - 36
packages/statusbar-extension/src/index.ts

@@ -63,7 +63,7 @@ const statusBar: JupyterLabPlugin<IStatusBar> = {
  * A plugin that provides a kernel status item to the status bar.
  */
 export const kernelStatus: JupyterLabPlugin<void> = {
-  id: '@jupyterlab/statusbar:kernel-status',
+  id: '@jupyterlab/statusbar-extension:kernel-status',
   autoStart: true,
   requires: [IStatusBar, INotebookTracker, IConsoleTracker],
   activate: (
@@ -117,17 +117,21 @@ export const kernelStatus: JupyterLabPlugin<void> = {
       item.model!.session = currentSession;
     });
 
-    statusBar.registerStatusItem('kernel-status-item', item, {
-      align: 'left',
-      rank: 1,
-      isActive: () => {
-        const current = app.shell.currentWidget;
-        return (
-          current &&
-          (notebookTracker.has(current) || consoleTracker.has(current))
-        );
+    statusBar.registerStatusItem(
+      '@jupyterlab/statusbar-extension:kernel-status',
+      {
+        item,
+        align: 'left',
+        rank: 1,
+        isActive: () => {
+          const current = app.shell.currentWidget;
+          return (
+            current &&
+            (notebookTracker.has(current) || consoleTracker.has(current))
+          );
+        }
       }
-    });
+    );
   }
 };
 
@@ -135,7 +139,7 @@ export const kernelStatus: JupyterLabPlugin<void> = {
  * A plugin providing a line/column status item to the application.
  */
 export const lineColItem: JupyterLabPlugin<void> = {
-  id: '@jupyterlab/statusbar:line-col-item',
+  id: '@jupyterlab/statusbar-extension:line-col-status',
   autoStart: true,
   requires: [IStatusBar, INotebookTracker, IEditorTracker, IConsoleTracker],
   activate: (
@@ -193,19 +197,23 @@ export const lineColItem: JupyterLabPlugin<void> = {
     });
 
     // Add the status item to the status bar.
-    statusBar.registerStatusItem('line-col-item', item, {
-      align: 'right',
-      rank: 2,
-      isActive: () => {
-        const current = app.shell.currentWidget;
-        return (
-          current &&
-          (notebookTracker.has(current) ||
-            editorTracker.has(current) ||
-            consoleTracker.has(current))
-        );
+    statusBar.registerStatusItem(
+      '@jupyterlab/statusbar-extension:line-col-status',
+      {
+        item,
+        align: 'right',
+        rank: 2,
+        isActive: () => {
+          const current = app.shell.currentWidget;
+          return (
+            current &&
+            (notebookTracker.has(current) ||
+              editorTracker.has(current) ||
+              consoleTracker.has(current))
+          );
+        }
       }
-    });
+    );
   }
 };
 
@@ -217,18 +225,22 @@ export const lineColItem: JupyterLabPlugin<void> = {
  * is installed.
  */
 export const memoryUsageItem: JupyterLabPlugin<void> = {
-  id: '@jupyterlab/statusbar:memory-usage-item',
+  id: '@jupyterlab/statusbar-extension:memory-usage-status',
   autoStart: true,
   requires: [IStatusBar],
   activate: (app: JupyterLab, statusBar: IStatusBar) => {
     let item = new MemoryUsage();
 
-    statusBar.registerStatusItem('memory-usage-item', item, {
-      align: 'left',
-      rank: 2,
-      isActive: () => item.model!.metricsAvailable,
-      stateChanged: item.model!.stateChanged
-    });
+    statusBar.registerStatusItem(
+      '@jupyterlab/statusbar-extension:memory-usage-status',
+      {
+        item,
+        align: 'left',
+        rank: 2,
+        isActive: () => item.model!.metricsAvailable,
+        activeStateChanged: item.model!.stateChanged
+      }
+    );
   }
 };
 
@@ -237,7 +249,7 @@ export const memoryUsageItem: JupyterLabPlugin<void> = {
  * to the status bar.
  */
 export const runningSessionsItem: JupyterLabPlugin<void> = {
-  id: '@jupyterlab/statusbar:running-sessions-item',
+  id: '@jupyterlab/statusbar-extension:running-sessions-status',
   autoStart: true,
   requires: [IStatusBar],
   activate: (app: JupyterLab, statusBar: IStatusBar) => {
@@ -246,10 +258,14 @@ export const runningSessionsItem: JupyterLabPlugin<void> = {
       serviceManager: app.serviceManager
     });
 
-    statusBar.registerStatusItem('running-sessions-item', item, {
-      align: 'left',
-      rank: 0
-    });
+    statusBar.registerStatusItem(
+      '@jupyterlab/statusbar-extension:running-sessions-status',
+      {
+        item,
+        align: 'left',
+        rank: 0
+      }
+    );
   }
 };
 

+ 50 - 68
packages/statusbar/src/statusbar.ts

@@ -39,17 +39,11 @@ export interface IStatusBar {
    *
    * @param id - a unique id for the status item.
    *
-   * @param widget - The item to add to the status bar.
-   *
    * @param options - The options for how to add the status item.
    *
    * @returns an `IDisposable` that can be disposed to remove the item.
    */
-  registerStatusItem(
-    id: string,
-    widget: Widget,
-    options: IStatusBar.IItemOptions
-  ): IDisposable;
+  registerStatusItem(id: string, statusItem: IStatusBar.IItem): IDisposable;
 }
 
 /**
@@ -61,11 +55,16 @@ export namespace IStatusBar {
   /**
    * Options for status bar items.
    */
-  export interface IItemOptions {
+  export interface IItem {
+    /**
+     * The item to add to the status bar.
+     */
+    item: Widget;
+
     /**
-     * Which side to place widget.
-     * Permanent widgets are intended for the right and left side,
-     * with more transient widgets in the middle.
+     * Which side to place item.
+     * Permanent items are intended for the right and left side,
+     * with more transient items in the middle.
      */
     align?: Alignment;
 
@@ -75,19 +74,19 @@ export namespace IStatusBar {
     rank?: number;
 
     /**
-     * Whether the widget is shown or hidden.
+     * Whether the item is shown or hidden.
      */
     isActive?: () => boolean;
 
     /**
-     * A signal that is fired when the widget active state changes.
+     * A signal that is fired when the item active state changes.
      */
     activeStateChanged?: ISignal<any, void>;
   }
 }
 
 /**
- * Main status bar object which contains all widgets.
+ * Main status bar object which contains all items.
  */
 export class StatusBar extends Widget implements IStatusBar {
   constructor() {
@@ -118,75 +117,60 @@ export class StatusBar extends Widget implements IStatusBar {
    *
    * @param id - a unique id for the status item.
    *
-   * @param widget - The item to add to the status bar.
-   *
-   * @param options - The options for how to add the status item.
+   * @param statusItem - The item to add to the status bar.
    */
-  registerStatusItem(
-    id: string,
-    widget: Widget,
-    options: IStatusBar.IItemOptions = {}
-  ): IDisposable {
+  registerStatusItem(id: string, statusItem: IStatusBar.IItem): IDisposable {
     if (id in this._statusItems) {
       throw new Error(`Status item ${id} already registered.`);
     }
 
-    const align = options.align || 'left';
-    const rank = options.rank || 0;
-    const isActive = options.isActive || (() => true);
-    const activeStateChanged = options.activeStateChanged || null;
+    // Populate defaults for the optional properties of the status item.
+    statusItem = { ...Private.statusItemDefaults, ...statusItem };
+    const { align, item, rank } = statusItem;
 
+    // Connect the activeStateChanged signal to refreshing the status item,
+    // if the signal was provided.
     const onActiveStateChanged = () => {
       this._refreshItem(id);
     };
-    if (activeStateChanged) {
-      activeStateChanged.connect(onActiveStateChanged);
+    if (statusItem.activeStateChanged) {
+      statusItem.activeStateChanged.connect(onActiveStateChanged);
     }
 
-    let wrapper = {
-      widget,
-      align,
-      rank,
-      isActive,
-      activeStateChanged
-    };
-
-    let rankItem = {
-      id,
-      rank
-    };
-
-    widget.addClass(itemStyle);
+    let rankItem = { id, rank };
 
-    this._statusItems[id] = wrapper;
+    statusItem.item.addClass(itemStyle);
+    this._statusItems[id] = statusItem;
 
     if (align === 'left') {
       let insertIndex = this._findInsertIndex(this._leftRankItems, rankItem);
       if (insertIndex === -1) {
-        this._leftSide.addWidget(widget);
+        this._leftSide.addWidget(item);
         this._leftRankItems.push(rankItem);
       } else {
         ArrayExt.insert(this._leftRankItems, insertIndex, rankItem);
-        this._leftSide.insertWidget(insertIndex, widget);
+        this._leftSide.insertWidget(insertIndex, item);
       }
     } else if (align === 'right') {
       let insertIndex = this._findInsertIndex(this._rightRankItems, rankItem);
       if (insertIndex === -1) {
-        this._rightSide.addWidget(widget);
+        this._rightSide.addWidget(item);
         this._rightRankItems.push(rankItem);
       } else {
         ArrayExt.insert(this._rightRankItems, insertIndex, rankItem);
-        this._rightSide.insertWidget(insertIndex, widget);
+        this._rightSide.insertWidget(insertIndex, item);
       }
     } else {
-      this._middlePanel.addWidget(widget);
+      this._middlePanel.addWidget(item);
     }
 
     const disposable = new DisposableDelegate(() => {
       delete this._statusItems[id];
-      activeStateChanged.disconnect(onActiveStateChanged);
-      widget.parent = null;
-      widget.dispose();
+      if (statusItem.activeStateChanged) {
+        statusItem.activeStateChanged.disconnect(onActiveStateChanged);
+      }
+      item.parent = null;
+      item.dispose();
     });
     this._disposables.add(disposable);
     return disposable;
@@ -220,10 +204,10 @@ export class StatusBar extends Widget implements IStatusBar {
   private _refreshItem(id: string) {
     const statusItem = this._statusItems[id];
     if (statusItem.isActive()) {
-      statusItem.widget.show();
-      statusItem.widget.update();
+      statusItem.item.show();
+      statusItem.item.update();
     } else {
-      statusItem.widget.hide();
+      statusItem.item.hide();
     }
   }
 
@@ -235,30 +219,28 @@ export class StatusBar extends Widget implements IStatusBar {
 
   private _leftRankItems: Private.IRankItem[] = [];
   private _rightRankItems: Private.IRankItem[] = [];
-  private _statusItems: { [id: string]: StatusBar.IItem } = {};
+  private _statusItems: { [id: string]: IStatusBar.IItem } = {};
   private _disposables = new DisposableSet();
   private _leftSide: Panel;
   private _middlePanel: Panel;
   private _rightSide: Panel;
 }
 
-export namespace StatusBar {
-  /**
-   * The interface for a status bar item.
-   */
-  export interface IItem {
-    align: IStatusBar.Alignment;
-    rank: number;
-    widget: Widget;
-    isActive: () => boolean;
-    activeStateChanged: ISignal<any, void> | null;
-  }
-}
-
 /**
  * A namespace for private functionality.
  */
 namespace Private {
+  type Omit<T, K extends keyof T> = Pick<T, Exclude<keyof T, K>>;
+  /**
+   * Default options for a status item, less the item itself.
+   */
+  export const statusItemDefaults: Omit<IStatusBar.IItem, 'item'> = {
+    align: 'left',
+    rank: 0,
+    isActive: () => true,
+    activeStateChanged: undefined
+  };
+
   /**
    * An interface for storing the rank of a status item.
    */