« get me outta code hell

Un-selectable inputs - 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/form
diff options
context:
space:
mode:
authorFlorrie <towerofnix@gmail.com>2018-05-30 23:55:45 -0300
committerFlorrie <towerofnix@gmail.com>2018-05-30 23:55:45 -0300
commit553bbf486e043f206bb8f2c92c943fd688f8fedc (patch)
tree08366f0c8f9b0624f6b19d4e932fd286a1cab0f1 /ui/form
parentf782f7395cd287d4767c1f3ffd20a72a51007595 (diff)
Un-selectable inputs
Diffstat (limited to 'ui/form')
-rw-r--r--ui/form/FocusElement.js9
-rw-r--r--ui/form/Form.js26
2 files changed, 29 insertions, 6 deletions
diff --git a/ui/form/FocusElement.js b/ui/form/FocusElement.js
index 18f13bf..79a8afc 100644
--- a/ui/form/FocusElement.js
+++ b/ui/form/FocusElement.js
@@ -25,11 +25,18 @@ module.exports = class FocusElement extends DisplayElement {
     this.isFocused = false
   }
 
+  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.
     //
-    // Keyboard characters are sent as a buffer in the form of
+    // 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
   }
diff --git a/ui/form/Form.js b/ui/form/Form.js
index ce064aa..e5d756a 100644
--- a/ui/form/Form.js
+++ b/ui/form/Form.js
@@ -59,6 +59,10 @@ module.exports = class Form extends FocusElement {
     }
   }
 
+  get selectable() {
+    return this.inputs.some(inp => inp.selectable)
+  }
+
   updateSelectedElement() {
     if (this.root.select) {
       this.root.select(this.inputs[this.curIndex])
@@ -66,16 +70,23 @@ module.exports = class Form extends FocusElement {
   }
 
   previousInput() {
-    this.curIndex = (this.curIndex - 1)
-    if (this.curIndex < 0) {
-      this.curIndex = (this.inputs.length - 1)
-    }
+    // TODO: Forms currently assume there is at least one selectable input,
+    // but this isn't necessarily always the case.
+    do {
+      this.curIndex = (this.curIndex - 1)
+      if (this.curIndex < 0) {
+        this.curIndex = (this.inputs.length - 1)
+      }
+    } while (!this.inputs[this.curIndex].selectable)
 
     this.updateSelectedElement()
   }
 
   nextInput() {
-    this.curIndex = (this.curIndex + 1) % this.inputs.length
+    // TODO: See previousInput
+    do {
+      this.curIndex = (this.curIndex + 1) % this.inputs.length
+    } while (!this.inputs[this.curIndex].selectable)
 
     this.updateSelectedElement()
   }
@@ -83,6 +94,11 @@ module.exports = class Form extends FocusElement {
   firstInput(selectForm = true) {
     this.curIndex = 0
 
+    // TODO: See previousInput
+    if (!this.inputs[this.curIndex].selectable) {
+      this.nextInput()
+    }
+
     if (selectForm || (
       this.root.isChildOrSelfSelected && this.root.isChildOrSelfSelected(this)
     )) {