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
|
const telc = require('../../util/telchars')
const FocusElement = require('./FocusElement')
module.exports = class Form extends FocusElement {
constructor() {
super()
this.inputs = []
this.curIndex = 0
this.captureTab = true
}
addInput(input, asChild = true, opts = {}) {
// Adds the given input as a child element and pushes it to the input
// list. If the optional argument asChild is false, it won't add the
// input element as a child of the form.
this.inputs.push(input)
if (asChild) {
this.addChild(input, this.children.length, opts)
}
}
removeInput(input, asChild = true, opts = {}) {
// Removes the given input from the form's input list. If the optional
// argument asChild is false, it won't try to removeChild the input.
if (this.inputs.includes(input)) {
this.inputs.splice(this.inputs.indexOf(input), 1)
if (asChild) {
this.removeChild(input, opts)
}
}
}
selectInput(input) {
if (this.inputs.includes(input)) {
this.curIndex = this.inputs.indexOf(input)
this.updateSelectedElement()
}
}
keyPressed(keyBuf) {
// Don't do anything if captureTab is set to false. This is handy for
// nested forms.
if (!this.captureTab) {
return
}
if (telc.isTab(keyBuf) || telc.isBackTab(keyBuf)) {
// No inputs to tab through, so do nothing.
if (this.inputs.length < 2) {
return
}
if (telc.isTab(keyBuf)) {
this.nextInput()
} else {
this.previousInput()
}
return false
}
}
get selectable() {
return this.inputs.some(inp => inp.selectable)
}
updateSelectedElement() {
if (this.root.select && this.inputs.length) {
if (this.curIndex > this.inputs.length - 1) {
this.curIndex = this.inputs.length - 1
}
this.root.select(this.inputs[this.curIndex], {fromForm: true})
}
}
previousInput() {
if (this.inputs.length === 0) {
return
}
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() {
if (this.inputs.length === 0) {
return
}
do {
this.curIndex = (this.curIndex + 1) % this.inputs.length
} while (!this.inputs[this.curIndex].selectable)
this.updateSelectedElement()
}
firstInput(selectForm = true) {
if (this.inputs.length === 0) {
return
}
this.curIndex = 0
if (!this.inputs[this.curIndex].selectable) {
this.nextInput()
}
if (selectForm || (
this.root.isChildOrSelfSelected && this.root.isChildOrSelfSelected(this)
)) {
this.updateSelectedElement()
}
}
lastInput(selectForm = true) {
if (this.inputs.length === 0) {
return
}
this.curIndex = this.inputs.length - 1
if (!this.inputs[this.curIndex].selectable) {
this.previousInput()
}
if (selectForm || (
this.root.isChildOrSelfSelected && this.root.isChildOrSelfSelected(this)
)) {
this.updateSelectedElement()
}
}
selected() {
if (this.root.selectedElement === this) {
this.updateSelectedElement()
}
}
get curIndex() { return this.getDep('curIndex') }
set curIndex(v) { return this.setDep('curIndex', v) }
}
|