« 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/primitives/FocusElement.js
diff options
context:
space:
mode:
Diffstat (limited to 'ui/primitives/FocusElement.js')
-rw-r--r--ui/primitives/FocusElement.js45
1 files changed, 45 insertions, 0 deletions
diff --git a/ui/primitives/FocusElement.js b/ui/primitives/FocusElement.js
new file mode 100644
index 0000000..2c23b1e
--- /dev/null
+++ b/ui/primitives/FocusElement.js
@@ -0,0 +1,45 @@
+import DisplayElement from './DisplayElement.js'
+
+export default class FocusElement extends DisplayElement {
+  // A basic element that can receive cursor focus.
+
+  constructor() {
+    super()
+
+    this.cursorVisible = false
+    this.cursorX = 0
+    this.cursorY = 0
+  }
+
+  selected() {
+    // Should be overridden in subclasses.
+  }
+
+  unselected() {
+    // Should be overridden in subclasses.
+  }
+
+  get selectable() {
+    // Should be overridden if you want to make the element unselectable
+    // (according to particular conditions).
+
+    return true
+  }
+
+  keyPressed(keyBuf) {
+    // Do something with a buffer containing the key pressed (that is,
+    // telnet data sent). Should be overridden in subclasses.
+    //
+    // Arrow keys are sent as a buffer in the form of
+    // ESC[# where # is A, B, C or D. See more here:
+    // http://stackoverflow.com/a/11432632/4633828
+  }
+
+  get isSelected() {
+    const selected = this.root.selectedElement
+    return !!(selected && [selected, ...selected.directAncestors].includes(this))
+  }
+
+  get absCursorX() { return this.absX + this.cursorX }
+  get absCursorY() { return this.absY + this.cursorY }
+}