Sha256: 9722dde605f90787392fee0a37743ff1a034273cfadb79e8d5ca8cf16886445b

Contents?: true

Size: 1.33 KB

Versions: 45

Compression:

Stored size: 1.33 KB

Contents

import { Controller } from '@hotwired/stimulus'

// Used as a custom Stream Action <turbo-stream action="update-belongs-to" />
export default class extends Controller {
  static values = {
    polymorphic: Boolean,
    searchable: Boolean,
    targetName: String,
    relationName: String,
  }

  beforeStreamRender(event) {
    const { relationName } = event.target.dataset
    if (event.target.action !== 'update-belongs-to' || this.relationNameValue !== relationName) {
      return false
    }

    event.detail.render = (stream) => {
      if (this.searchableValue) {
        this.updateSearchable(stream)
      } else {
        this.updateNonSearchable(stream)
      }
    }
  }

  updateSearchable(stream) {
    // Update the id component
    document.querySelector(`input[name="${this.targetNameValue}"][type="hidden"]`).value = stream.dataset.targetRecordId
    // Update the label
    document.querySelector(`input[name="${this.targetNameValue}"][type="text"]`).value = stream.dataset.targetResourceLabel
  }

  updateNonSearchable(stream) {
    const select = document.querySelector(`select[name="${this.targetNameValue}"]`)
    const option = document.createElement('option')
    option.value = stream.dataset.targetRecordId
    option.text = stream.dataset.targetResourceLabel
    option.selected = 'selected'

    select.appendChild(option)
  }
}

Version data entries

45 entries across 45 versions & 1 rubygems

Version Path
avo-3.15.1 app/javascript/js/controllers/fields/reload_belongs_to_field_controller.js
avo-3.15.0 app/javascript/js/controllers/fields/reload_belongs_to_field_controller.js
avo-3.14.5 app/javascript/js/controllers/fields/reload_belongs_to_field_controller.js
avo-3.14.4 app/javascript/js/controllers/fields/reload_belongs_to_field_controller.js
avo-3.14.3 app/javascript/js/controllers/fields/reload_belongs_to_field_controller.js
avo-3.14.2 app/javascript/js/controllers/fields/reload_belongs_to_field_controller.js
avo-3.14.1 app/javascript/js/controllers/fields/reload_belongs_to_field_controller.js
avo-3.14.0 app/javascript/js/controllers/fields/reload_belongs_to_field_controller.js
avo-3.13.7 app/javascript/js/controllers/fields/reload_belongs_to_field_controller.js
avo-3.13.6 app/javascript/js/controllers/fields/reload_belongs_to_field_controller.js
avo-3.13.5 app/javascript/js/controllers/fields/reload_belongs_to_field_controller.js
avo-3.13.4 app/javascript/js/controllers/fields/reload_belongs_to_field_controller.js
avo-3.13.3 app/javascript/js/controllers/fields/reload_belongs_to_field_controller.js
avo-3.13.2 app/javascript/js/controllers/fields/reload_belongs_to_field_controller.js
avo-3.13.1 app/javascript/js/controllers/fields/reload_belongs_to_field_controller.js
avo-3.13.0 app/javascript/js/controllers/fields/reload_belongs_to_field_controller.js
avo-3.12.0 app/javascript/js/controllers/fields/reload_belongs_to_field_controller.js
avo-3.11.10 app/javascript/js/controllers/fields/reload_belongs_to_field_controller.js
avo-3.11.9 app/javascript/js/controllers/fields/reload_belongs_to_field_controller.js
avo-3.11.8 app/javascript/js/controllers/fields/reload_belongs_to_field_controller.js