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.3.6 app/javascript/js/controllers/fields/reload_belongs_to_field_controller.js
avo-3.3.5 app/javascript/js/controllers/fields/reload_belongs_to_field_controller.js
avo-3.3.4 app/javascript/js/controllers/fields/reload_belongs_to_field_controller.js
avo-3.3.3 app/javascript/js/controllers/fields/reload_belongs_to_field_controller.js
avo-3.3.2 app/javascript/js/controllers/fields/reload_belongs_to_field_controller.js
avo-3.3.1 app/javascript/js/controllers/fields/reload_belongs_to_field_controller.js
avo-3.3.0 app/javascript/js/controllers/fields/reload_belongs_to_field_controller.js
avo-3.2.3 app/javascript/js/controllers/fields/reload_belongs_to_field_controller.js
avo-3.2.2 app/javascript/js/controllers/fields/reload_belongs_to_field_controller.js
avo-3.2.1 app/javascript/js/controllers/fields/reload_belongs_to_field_controller.js
avo-3.2.0 app/javascript/js/controllers/fields/reload_belongs_to_field_controller.js
avo-3.1.7 app/javascript/js/controllers/fields/reload_belongs_to_field_controller.js
avo-3.1.6 app/javascript/js/controllers/fields/reload_belongs_to_field_controller.js
avo-3.1.5 app/javascript/js/controllers/fields/reload_belongs_to_field_controller.js
avo-3.1.4 app/javascript/js/controllers/fields/reload_belongs_to_field_controller.js
avo-3.1.3 app/javascript/js/controllers/fields/reload_belongs_to_field_controller.js
avo-3.1.2 app/javascript/js/controllers/fields/reload_belongs_to_field_controller.js
avo-3.1.1 app/javascript/js/controllers/fields/reload_belongs_to_field_controller.js
avo-3.1.0 app/javascript/js/controllers/fields/reload_belongs_to_field_controller.js
avo-3.0.8 app/javascript/js/controllers/fields/reload_belongs_to_field_controller.js