« get me outta code hell

ListScrollForm.scrollToEnd function - 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
diff options
context:
space:
mode:
authorFlorrie <towerofnix@gmail.com>2018-06-04 09:57:49 -0300
committerFlorrie <towerofnix@gmail.com>2018-06-04 09:57:49 -0300
commit6e0fa96243902c4354764c2edc79edf1190280fc (patch)
treede520a1815b73513a97a20d00f2f8c03831e8d46 /ui
parent01707d9e6376ff04a7b39381eb6b131717a721fe (diff)
ListScrollForm.scrollToEnd function
Diffstat (limited to 'ui')
-rw-r--r--ui/form/ListScrollForm.js35
1 files changed, 21 insertions, 14 deletions
diff --git a/ui/form/ListScrollForm.js b/ui/form/ListScrollForm.js
index 581c82f..fe7ef0e 100644
--- a/ui/form/ListScrollForm.js
+++ b/ui/form/ListScrollForm.js
@@ -96,19 +96,7 @@ module.exports = class ListScrollForm extends Form {
     // we should move the view so that the item is the farthest right (of all
     // the visible items).
     if (this.getItemPos(sel) > this.formEdge + this.scrollSize) {
-      // We can decide how many items to scroll past by moving forward until
-      // our item's far edge is visible.
-
-      let i
-      let edge = this.formEdge
-
-      for (i = 0; i < this.inputs.length; i++) {
-        if (this.getItemPos(sel) <= edge) break
-        edge += this.inputs[i][this.sizeProp]
-      }
-
-      // Now that we have the right index to scroll to, apply it!
-      this.scrollItems = i
+      this.scrollElementIntoEndOfView(sel)
     }
 
     // Adjusting the number of scroll items is much simpler to deal with if
@@ -143,11 +131,30 @@ module.exports = class ListScrollForm extends Form {
     this.scrollItems += this.h
     if (this.curIndex >= this.inputs.length) {
       this.curIndex = this.inputs.length - 1
-      this.scrollItems = Math.max(0, this.inputs.length - this.h)
+      this.scrollToEnd()
     }
     this.updateSelectedElement()
   }
 
+  scrollElementIntoEndOfView(element) {
+    // We can decide how many items to scroll past by moving forward until
+    // the item's far edge is visible.
+    let i
+    let edge = this.formEdge
+    for (i = 0; i < this.inputs.length; i++) {
+      if (this.getItemPos(element) <= edge) break
+      edge += this.inputs[i][this.sizeProp]
+    }
+
+    // Now that we have the right index to scroll to, apply it!
+    this.scrollItems = i
+  }
+
+  scrollToEnd() {
+    this.scrollElementIntoEndOfView(this.inputs[this.inputs.length - 1])
+    this.fixLayout()
+  }
+
   getItemPos(item) {
     // Gets the position of the item in an unscrolled view.