All files / js dialogSystem.tsx

43.75% Statements 7/16
0% Branches 0/20
20% Functions 1/5
43.75% Lines 7/16

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 601x 1x                                     1x 1x 1x                                                                   1x   1x  
import hub from "./hub";
import { DialogChangedEvent } from "./events";
import FindFiles from "./dialogs/findFiles";
import ErrorDialog from "./dialogs/errorDialog";
import Publish from "./dialogs/publish";
import Refresh from "./dialogs/Refresh";
 
export type Dialog =
  | typeof FindFiles
  | typeof ErrorDialog
  | typeof Publish
  | typeof Refresh;
 
export type DialogInstance = InstanceType<Dialog> & {
  preventNavigation?: () => boolean;
};
 
class DialogSystem {
  private instance: DialogInstance | null;
 
  constructor() {
    this.instance = null;
    this.dismissDialog = this.dismissDialog.bind(this);
  }
 
  // invoked by the application once the dialog has been created.
  notifyDialogInstance(dialog: DialogInstance | null) {
    this.instance = dialog;
  }
 
  // given a dialog class this will instruct the application to bring up
  // the dialog and display it.
  showDialog(dialog: Dialog, options?: unknown, showIfOpen = true) {
    if (!showIfOpen && this.instance !== null) {
      return;
    }
    // if the current dialog prevents navigation, then we just silently
    // will not show the dialog.
    if (!this.preventNavigation()) {
      hub.emit(new DialogChangedEvent(dialog, options || {}));
    }
  }
 
  // tells the application to dismiss the current dialog.
  dismissDialog() {
    if (!this.preventNavigation()) {
      hub.emit(new DialogChangedEvent(null));
    }
  }
 
  // returns true if the current dialog prevents navigation.
  private preventNavigation() {
    return this.instance?.preventNavigation?.();
  }
}
 
const dialogSystem = new DialogSystem();
 
export default dialogSystem;