« get me outta code hell

tui-lib - Pure Node.js library for making visual command-line programs (ala vim, ncdu)
about summary refs log tree commit diff
path: root/ui/dialogs/Dialog.js
diff options
context:
space:
mode:
Diffstat (limited to 'ui/dialogs/Dialog.js')
-rw-r--r--ui/dialogs/Dialog.js55
1 files changed, 55 insertions, 0 deletions
diff --git a/ui/dialogs/Dialog.js b/ui/dialogs/Dialog.js
new file mode 100644
index 0000000..19565f5
--- /dev/null
+++ b/ui/dialogs/Dialog.js
@@ -0,0 +1,55 @@
+import {Pane} from 'tui-lib/ui/presentation'
+import {FocusElement} from 'tui-lib/ui/primitives'
+
+import telc from 'tui-lib/util/telchars'
+
+export default class Dialog extends FocusElement {
+  // A simple base dialog.
+  //
+  // Emits the 'cancelled' event when the cancel key (escape) is pressed,
+  // which should (probably) be handled by the dialog's creator.
+  //
+  // Doesn't do anything when focused by default - this should be overridden
+  // in subclasses.
+  //
+  // Automatically adjusts to fill its parent. Has a pane child (this.pane),
+  // but the pane isn't adjusted at all (you should change its size and
+  // likely center it in your subclass).
+
+  constructor() {
+    super()
+
+    this.pane = new Pane()
+    this.addChild(this.pane)
+  }
+
+  fixLayout() {
+    this.w = this.parent.contentW
+    this.h = this.parent.contentH
+  }
+
+  open() {
+    this.oldSelectedElement = this.root.selectedElement
+    this.opened()
+    this.visible = true
+    this.root.select(this)
+    this.fixLayout()
+  }
+
+  close() {
+    this.closed()
+    this.visible = false
+    this.root.select(this.oldSelectedElement)
+  }
+
+  opened() {}
+
+  closed() {}
+
+  keyPressed(keyBuf) {
+    if (telc.isCancel(keyBuf)) {
+      this.emit('cancelled')
+      return false
+    }
+  }
+}