Sha256: be68f127d431c9f9f9fb853d16d9f7472d7dba2a81c4d223a134fb4c99a29d6b

Contents?: true

Size: 1.5 KB

Versions: 6

Compression:

Stored size: 1.5 KB

Contents

import {Controller} from '@hotwired/stimulus'

export default class OptionsController extends Controller {
  static targets = ['option']

  static values = {
    activeIndex: {
      type: Number,
      default: 0,
    },
    syncedAttrs: Array,
    antiAttrs: Array,
  }

  declare readonly optionTargets: Array<Element>
  declare activeIndexValue: number

  declare readonly syncedAttrsValue: string[]
  declare readonly hasSyncedAttrsValue: boolean
  declare readonly antiAttrsValue: string[]
  declare readonly hasAntiAttrsValue: boolean

  connect(): void {
    this.select(new Event('Init'), this.activeIndexValue)
  }

  select(e: Event, newIndex?: number) {
    for (let index in this.optionTargets) {
      const target = this.optionTargets[index]
      const isActive = newIndex === undefined ? target === e.currentTarget : Number(index) === Number(newIndex)

      if (isActive) {
        this.activeIndexValue = Number(index)
      }

      this.#updateAllAttrsForElement(target, isActive)
    }
  }

  #updateAllAttrsForElement(element: Element, value: boolean) {
    if (this.hasSyncedAttrsValue) {
      this.#syncAttrsForTarget(element, this.syncedAttrsValue, value)
    }

    if (this.hasAntiAttrsValue) {
      this.#syncAttrsForTarget(element, this.antiAttrsValue, !value)
    }
  }

  #syncAttrsForTarget(element: Element, attrs: string[], value: boolean) {
    const attrState = String(value)
    for (let index in attrs) {
      const attr = attrs[index]
      element.setAttribute(attr, attrState)
    }
  }
}

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
ariadne_view_components-0.0.48-x64-mingw-ucrt app/components/ariadne/options_controller/options_controller.ts
ariadne_view_components-0.0.48-aarch64-linux app/components/ariadne/options_controller/options_controller.ts
ariadne_view_components-0.0.48-x86_64-darwin app/components/ariadne/options_controller/options_controller.ts
ariadne_view_components-0.0.48-arm64-darwin app/components/ariadne/options_controller/options_controller.ts
ariadne_view_components-0.0.48 app/components/ariadne/options_controller/options_controller.ts
ariadne_view_components-0.0.48-x86_64-linux app/components/ariadne/options_controller/options_controller.ts