Sha256: 919662ed6a582beab0986eab00cd1cba6f74a5efd88aee96913b2e95a43ef328

Contents?: true

Size: 1.08 KB

Versions: 135

Compression:

Stored size: 1.08 KB

Contents

import { Controller } from "@hotwired/stimulus"

export default class extends Controller {
  static targets = [ "field" ]
  static values = {
    original: String,
    dirty: Boolean
  }
  
  connect() {
    this.recordOriginal()
  }
  
  recordOriginal() {
    if (!this.hasOriginalValue) {
      this.originalValue = this.value
    }
    this.dispatch('original-recorded', {
      detail: { originalValue: this.originalValue },
      target: this.field
    })
  }
  
  revert(event) {
    if (this.hasOriginalValue) {
      this.value = this.originalValue
    }
    this.dispatch('reverted')
  }
  
  get field() {
    if (this.hasFieldTarget) {
      return this.fieldTarget
    }
    return this.element
  }
  
  get value() {
    switch (this.field.type) {
      case "checkbox":
        return this.field.checked
    }
    
    return this.field.value
  }
  
  set value(newValue) {
    switch (this.field.type) {
      case "checkbox":
        this.field.checked = (newValue === "true" || newValue === true)
        break
      default:
        this.field.value = newValue
        break
    }
  }
}

Version data entries

135 entries across 135 versions & 1 rubygems

Version Path
bullet_train-fields-1.10.0 app/javascript/controllers/fields/field_controller.js
bullet_train-fields-1.9.0 app/javascript/controllers/fields/field_controller.js
bullet_train-fields-1.8.5 app/javascript/controllers/fields/field_controller.js
bullet_train-fields-1.8.4 app/javascript/controllers/fields/field_controller.js
bullet_train-fields-1.8.3 app/javascript/controllers/fields/field_controller.js
bullet_train-fields-1.8.2 app/javascript/controllers/fields/field_controller.js
bullet_train-fields-1.8.1 app/javascript/controllers/fields/field_controller.js
bullet_train-fields-1.8.0 app/javascript/controllers/fields/field_controller.js
bullet_train-fields-1.7.23 app/javascript/controllers/fields/field_controller.js
bullet_train-fields-1.7.22 app/javascript/controllers/fields/field_controller.js
bullet_train-fields-1.7.21 app/javascript/controllers/fields/field_controller.js
bullet_train-fields-1.7.20 app/javascript/controllers/fields/field_controller.js
bullet_train-fields-1.7.19 app/javascript/controllers/fields/field_controller.js
bullet_train-fields-1.7.18 app/javascript/controllers/fields/field_controller.js
bullet_train-fields-1.7.17 app/javascript/controllers/fields/field_controller.js
bullet_train-fields-1.7.16 app/javascript/controllers/fields/field_controller.js
bullet_train-fields-1.7.15 app/javascript/controllers/fields/field_controller.js
bullet_train-fields-1.7.14 app/javascript/controllers/fields/field_controller.js
bullet_train-fields-1.7.13 app/javascript/controllers/fields/field_controller.js
bullet_train-fields-1.7.12 app/javascript/controllers/fields/field_controller.js