« get me outta code hell

ListScrollForm.js « form « ui - 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/ListScrollForm.js
blob: dcdb56275937b87062bd6755e011114f3a5cab0d (plain)
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
const telc = require('../../util/telchars')

const Form = require('./Form')

module.exports = class ListScrollForm extends Form {
  // A form that lets the user scroll through a list of items. It
  // automatically adjusts to always allow the selected item to be visible.

  constructor(layoutType = 'vertical') {
    super()

    this.layoutType = layoutType

    this.scrollItems = 0
  }

  fixLayout() {
    // The scrollItems property represents the item to the very left of where
    // we've scrolled, so we know right away that none of those will be
    // visible and we won't bother iterating over them.
    const itemsPastScroll = this.inputs.slice(this.scrollItems)

    // We do need to hide them, though.
    const itemsBeforeScroll = this.inputs.slice(0, this.scrollItems)
    for (const item of itemsBeforeScroll) {
      item.visible = false
    }

    // This variable stores how far along the respective axis (as defined by
    // posProp) the next element should be.
    let nextPos = 0

    for (const item of itemsPastScroll) {
      item[this.posProp] = nextPos
      nextPos += item[this.sizeProp]

      // By default, the item should be visible..
      item.visible = true

      // ..but the item's far edge is past the form's far edge, it isn't
      // fully visible and should be hidden.
      if (item[this.posProp] + item[this.sizeProp] > this.formEdge) {
        item.visible = false
      }

      // Same deal goes for the close edge. We can check it against 0 since
      // the close edge of the form's content is going to be 0, of course!
      if (item[this.posProp] < 0) {
        item.visible = false
      }
    }
  }

  keyPressed(keyBuf) {
    handleKeyPress: {
      if (this.layoutType === 'horizontal') {
        if (telc.isLeft(keyBuf)) {
          this.previousInput()
          break handleKeyPress
        } else if (telc.isRight(keyBuf)) {
          this.nextInput()
          break handleKeyPress
        }
      } else if (this.layoutType === 'vertical') {
        if (telc.isUp(keyBuf)) {
          this.previousInput()
          break handleKeyPress
        } else if (telc.isDown(keyBuf)) {
          this.nextInput()
          break handleKeyPress
        }
      }

      if (telc.isPageUp(keyBuf)) {
        this.previousPage()
        break handleKeyPress
      } else if (telc.isPageDown(keyBuf)) {
        this.nextPage()
        break handleKeyPress
      }

      super.keyPressed(keyBuf)
    }

    this.scrollSelectedElementIntoView()
  }

  scrollSelectedElementIntoView() {
    const sel = this.inputs[this.curIndex]

    // If the item is ahead of our view (either to the right of or below),
    // 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
    }

    // Adjusting the number of scroll items is much simpler to deal with if
    // the item is behind our view. Since the item's behind, we need to move
    // the scroll to be immediately behind it, which is simple since we
    // already have its index.
    if (this.getItemPos(sel) <= this.scrollSize) {
      this.scrollItems = this.curIndex
    }

    this.fixLayout()
  }

  firstInput(...args) {
    this.scrollItems = 0

    super.firstInput(...args)
  }

  previousPage() {
    this.curIndex -= this.h
    this.scrollItems -= this.h
    if (this.curIndex < 0) {
      this.curIndex = 0
      this.scrollItems = 0
    }
    this.updateSelectedElement()
  }

  nextPage() {
    this.curIndex += this.h
    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.updateSelectedElement()
  }

  getItemPos(item) {
    // Gets the position of the item in an unscrolled view.

    return this.inputs.slice(0, this.inputs.indexOf(item) + 1)
      .reduce((a, b) => a + b[this.sizeProp], 0)
  }

  get sizeProp() {
    // The property used to measure the size of an item. If the layoutType
    // isn't valid (that is, 'horizontal' or 'vertical'), it'll return null.

    return (
      this.layoutType === 'horizontal' ? 'w' :
      this.layoutType === 'vertical' ? 'h' :
      null
    )
  }

  get posProp() {
    // The property used to position an item. Like sizeProp, returns null if
    // the layoutType isn't valid.

    return (
      this.layoutType === 'horizontal' ? 'x' :
      this.layoutType === 'vertical' ? 'y' :
      null)
  }

  get edgeProp() {
    // The property used to get the far edge of the property. As with
    // sizeProp, if the layoutType doesn't have an expected value, it'll
    // return null.

    return (
      this.layoutType === 'horizontal' ? 'right' :
      this.layoutType === 'vertical' ? 'bottom' :
      null)
  }

  get formEdge() {
    // Returns the value of the far edge of this form. Items farther in the
    // list (up to the edge) will be closer to this edge.

    return (
      this.layoutType === 'horizontal' ? this.contentW :
      this.layoutType === 'vertical' ? this.contentH :
      null)
  }

  get scrollSize() {
    // Gets the actual length made up by all of the items currently scrolled
    // past.

    return this.inputs.slice(0, this.scrollItems)
      .reduce((a, b) => a + b[this.sizeProp], 0)
  }
}