123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155 |
- import telc from 'tui-lib/util/telchars'
- import {FocusElement} from 'tui-lib/ui/primitives'
- export default 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) }
- }
|