Sha256: e79e10e8db74d5c2d015e474f356a79ea0b0a6fa7bfee032dfe6e0bc6e87d8e4

Contents?: true

Size: 1.59 KB

Versions: 46

Compression:

Stored size: 1.59 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 = this.selectorContext(stream).querySelector(`select[name="${this.targetNameValue}"]`)
    const option = document.createElement('option')
    option.value = stream.dataset.targetRecordId
    option.text = stream.dataset.targetResourceLabel
    option.selected = true
    select.appendChild(option)
  }

  selectorContext(stream) {
    // if polymorphic, search for the select in the correct sub-container
    if (this.polymorphicValue) {
      return document.querySelector(`[data-type="${stream.dataset.targetResourceClass}"]`)
    }

    return document
  }
}

Version data entries

46 entries across 46 versions & 1 rubygems

Version Path
avo-3.6.4 app/javascript/js/controllers/fields/reload_belongs_to_field_controller.js
avo-3.6.3 app/javascript/js/controllers/fields/reload_belongs_to_field_controller.js
avo-3.6.2 app/javascript/js/controllers/fields/reload_belongs_to_field_controller.js
avo-3.6.1 app/javascript/js/controllers/fields/reload_belongs_to_field_controller.js
avo-3.6.0 app/javascript/js/controllers/fields/reload_belongs_to_field_controller.js
avo-3.5.8 app/javascript/js/controllers/fields/reload_belongs_to_field_controller.js
avo-3.5.7 app/javascript/js/controllers/fields/reload_belongs_to_field_controller.js
avo-3.5.6 app/javascript/js/controllers/fields/reload_belongs_to_field_controller.js
avo-3.5.6.beta1 app/javascript/js/controllers/fields/reload_belongs_to_field_controller.js
avo-3.5.5 app/javascript/js/controllers/fields/reload_belongs_to_field_controller.js
avo-3.5.4 app/javascript/js/controllers/fields/reload_belongs_to_field_controller.js
avo-3.5.3 app/javascript/js/controllers/fields/reload_belongs_to_field_controller.js
avo-3.5.2 app/javascript/js/controllers/fields/reload_belongs_to_field_controller.js
avo-3.5.1 app/javascript/js/controllers/fields/reload_belongs_to_field_controller.js
avo-3.5.0 app/javascript/js/controllers/fields/reload_belongs_to_field_controller.js
avo-3.4.4 app/javascript/js/controllers/fields/reload_belongs_to_field_controller.js
avo-3.4.3 app/javascript/js/controllers/fields/reload_belongs_to_field_controller.js
avo-3.4.2 app/javascript/js/controllers/fields/reload_belongs_to_field_controller.js
avo-3.4.1 app/javascript/js/controllers/fields/reload_belongs_to_field_controller.js
avo-3.4.0 app/javascript/js/controllers/fields/reload_belongs_to_field_controller.js