Commit 74be697b authored by Andrei Sobolev's avatar Andrei Sobolev
Browse files

Grouped fields added (for output polarization)

parent 7e6271e6
Loading
Loading
Loading
Loading
+48 −3
Original line number Diff line number Diff line
@@ -271,18 +271,32 @@ export class InputField extends FormElement{
              explicitInclusion=false, units='', explanation='', extras={}) {
    super(wrapper, name, text, required, explicitInclusion, explanation)
    // Check if the field is multiple (num > 1), it is composed by several input field
    if (datatype instanceof Array) this.datatypes = datatype; else this.datatypes = [datatype]
    this.extras = extras
    this.HTMLFieldNames = []
    this.type = type
    this.numRows = 1
    this.numCols = undefined

    if (type.includes(':')){
      let a = type.split(':')
      this.type = a[0]
      if (a[1].includes('x')) {
        const [rows, cols] = a[1].split('x').map(Number)
        this.numRows = rows
        this.numCols = cols
        this.num = rows * cols
      } else {
        this.num = parseInt(a[1])
      }
      if (a.length === 3) this.HTMLFieldNames = a[2].split(',')
    }
    if (this.HTMLFieldNames.length === 0) {

    if (datatype instanceof Array) this.datatypes = datatype
    else this.datatypes = Array(this.num).fill(datatype)

    if (this.numCols) {
      this.#createGroupedFields(units)
    } else if (this.HTMLFieldNames.length === 0) {
      this.#createUnnamedFields(units)
    } else {
      this.#createNamedFields(units)
@@ -349,6 +363,37 @@ export class InputField extends FormElement{
    }
  }

  /**
   * A helper function creating NxM fields grouped into rows, each row with a subtitle
   * @param units {Array<string>} units per field
   */
  #createGroupedFields(units) {
    const valueInputBox = this.wrapper.querySelector('.value-box')
    const unitsBox = this.wrapper.querySelector('.units-box')
    for (let row = 0; row < this.numRows; row++) {
      let rowDiv = document.createElement('div')
      rowDiv.className = 'named-subfield'
      let subtitle = document.createElement('span')
      subtitle.className = 'named-subfield-subtitle'
      subtitle.innerHTML = this.HTMLFieldNames[row] ?? ''
      rowDiv.appendChild(subtitle)
      let fieldsDiv = document.createElement('div')
      fieldsDiv.className = 'named-subfield-fields'
      for (let col = 0; col < this.numCols; col++) {
        const idx = row * this.numCols + col
        let field = document.createElement('input')
        field.type = 'text'
        field.style.width = (100 / this.numCols) + '%'
        fieldsDiv.appendChild(field)
        this.HTMLFields.push(field)
        let unitSpan = document.createElement('span')
        unitSpan.innerHTML = units?.[idx] ?? ''
      }
      rowDiv.appendChild(fieldsDiv)
      valueInputBox.appendChild(rowDiv)
    }
  }

  /**
   * Returns value/values of the field
   * @returns {*[]|*}
@@ -365,7 +410,7 @@ export class InputField extends FormElement{
    if (!(value instanceof Array)) value = [value]
    if (value.length !== this.num) throw(`Cannot set value to ${this.name}`)
    for (let i = 0; i < this.num; i++)
      this.HTMLFields[i].value = value[i]
      if (value[i] !== undefined) this.HTMLFields[i].value = value[i]
  }
  // clearValue() {
  //   for (let i = 0; i < this.num; i++)