Sha256: ed60ff51282d9db9d67175c41ef4f81a360b86e1a42c12a7496be6480533bf53

Contents?: true

Size: 1.05 KB

Versions: 24

Compression:

Stored size: 1.05 KB

Contents

import {Controller} from '@hotwired/stimulus'

export default class AccumulatorController extends Controller {
  static targets = ['sum', 'accumulator']
  static values = {
    syncAttrs: {
      type: Array,
      default: ['aria-valuenow'],
    },
    sumAttr: {
      type: 'string',
      default: 'data-value',
    },
  }

  declare sumTargets: Array<HTMLElement>
  declare accumulatorTarget?: HTMLElement
  declare syncAttrsValue: Array<string>
  declare sumAttrValue: string

  connect(): void {
    this.accumulate()
  }

  accumulate() {
    let sum = 0
    for (const i in this.sumTargets) {
      const target = this.sumTargets[i]
      const value = Number(target.getAttribute(this.sumAttrValue))
      if (!isNaN(value)) {
        sum += value
      }
    }

    this.setAttributesTo(sum)
  }

  setAttributesTo(sum: number) {
    for (const i in this.syncAttrsValue) {
      const attr = this.syncAttrsValue[i]
      this.accumulator.setAttribute(attr, sum.toString())
    }
  }

  get accumulator() {
    return this.accumulatorTarget ?? this.element
  }
}

Version data entries

24 entries across 24 versions & 1 rubygems

Version Path
ariadne_view_components-0.0.59-x64-mingw-ucrt app/components/ariadne/accumulator_controller/accumulator_controller.ts
ariadne_view_components-0.0.59-aarch64-linux app/components/ariadne/accumulator_controller/accumulator_controller.ts
ariadne_view_components-0.0.59-x86_64-darwin app/components/ariadne/accumulator_controller/accumulator_controller.ts
ariadne_view_components-0.0.59-x86_64-linux app/components/ariadne/accumulator_controller/accumulator_controller.ts
ariadne_view_components-0.0.59-arm64-darwin app/components/ariadne/accumulator_controller/accumulator_controller.ts
ariadne_view_components-0.0.59 app/components/ariadne/accumulator_controller/accumulator_controller.ts
ariadne_view_components-0.0.58-x86_64-darwin app/components/ariadne/accumulator_controller/accumulator_controller.ts
ariadne_view_components-0.0.58-aarch64-linux app/components/ariadne/accumulator_controller/accumulator_controller.ts
ariadne_view_components-0.0.58-arm64-darwin app/components/ariadne/accumulator_controller/accumulator_controller.ts
ariadne_view_components-0.0.58-x64-mingw-ucrt app/components/ariadne/accumulator_controller/accumulator_controller.ts
ariadne_view_components-0.0.58 app/components/ariadne/accumulator_controller/accumulator_controller.ts
ariadne_view_components-0.0.58-x86_64-linux app/components/ariadne/accumulator_controller/accumulator_controller.ts
ariadne_view_components-0.0.57-arm64-darwin app/components/ariadne/accumulator_controller/accumulator_controller.ts
ariadne_view_components-0.0.57-aarch64-linux app/components/ariadne/accumulator_controller/accumulator_controller.ts
ariadne_view_components-0.0.57-x86_64-darwin app/components/ariadne/accumulator_controller/accumulator_controller.ts
ariadne_view_components-0.0.57-x86_64-linux app/components/ariadne/accumulator_controller/accumulator_controller.ts
ariadne_view_components-0.0.57-x64-mingw-ucrt app/components/ariadne/accumulator_controller/accumulator_controller.ts
ariadne_view_components-0.0.57 app/components/ariadne/accumulator_controller/accumulator_controller.ts
ariadne_view_components-0.0.55-x86_64-linux app/components/ariadne/accumulator_controller/accumulator_controller.ts
ariadne_view_components-0.0.55-arm64-darwin app/components/ariadne/accumulator_controller/accumulator_controller.ts