Sha256: 9c0f9ef73d89459e2b872d6b1a919f1138b7b229144d4bd0716fcdba3f6c5b91

Contents?: true

Size: 1.87 KB

Versions: 167

Compression:

Stored size: 1.87 KB

Contents

import { Controller } from '@hotwired/stimulus'

export default class extends Controller {
  connect () {
    this.showRemove()
  }

  addItem () {
    // Save a unique timestamp to ensure the key of the associated array is unique.
    const time = new Date().getTime()
    // Save the data id attribute into a variable. This corresponds to `new_object.object_id`.
    const linkId = this.element.dataset.id
    // Create a new regular expression needed to find any instance of the `new_object.object_id` used in the fields data attribute if there's a value in `linkId`.
    const regexp = linkId ? new RegExp(linkId, 'g') : null
    // Replace all instances of the `new_object.object_id` with `time`, and save markup into a variable if there's a value in `regexp`.
    const newFields = regexp ? this.element.dataset.fields.replace(regexp, time) : null
    // Add the new markup to the form if there are fields to add.
    if (newFields) {
      this.element.insertAdjacentHTML('beforebegin', newFields)
    }
    this.element.closest('form').dispatchEvent(new Event('nestedField:added'))
    this.showRemove()
  }

  quitar () {
    const parent = this.element.closest('.nested-fields')
    parent.style.display = 'none'
    parent.classList.add('removed')
    parent.querySelector('[name*=destroy]').value = 'true'
    this.element.closest('form').dispatchEvent(new Event('nestedField:removed'))
    this.showRemove()
  }

  showRemove () {
    const container = this.element.closest('.nested-container')
    if (container.dataset.required === 'true') {
      if (container.querySelectorAll('.nested-fields:not(.removed)').length === 1) {
        container.querySelectorAll('.link-to-remove').forEach((e) => {
          e.style.visibility = 'hidden'
        })
      } else {
        container.querySelectorAll('.link-to-remove').forEach((e) => {
          e.style.visibility = ''
        })
      }
    }
  }
}

Version data entries

167 entries across 167 versions & 1 rubygems

Version Path
pg_rails-7.6.27 pg_layout/app/javascript/controllers/nested_controller.js
pg_rails-7.6.26 pg_layout/app/javascript/controllers/nested_controller.js
pg_rails-7.6.25 pg_layout/app/javascript/controllers/nested_controller.js
pg_rails-7.6.24 pg_layout/app/javascript/controllers/nested_controller.js
pg_rails-7.6.24.pre.5 pg_layout/app/javascript/controllers/nested_controller.js
pg_rails-7.6.24.pre.4 pg_layout/app/javascript/controllers/nested_controller.js
pg_rails-7.6.24.pre.3 pg_layout/app/javascript/controllers/nested_controller.js
pg_rails-7.6.23 pg_layout/app/javascript/controllers/nested_controller.js
pg_rails-7.6.22 pg_layout/app/javascript/controllers/nested_controller.js
pg_rails-7.6.22.pre.3 pg_layout/app/javascript/controllers/nested_controller.js
pg_rails-7.6.22.pre.2 pg_layout/app/javascript/controllers/nested_controller.js
pg_rails-7.6.22.pre.1 pg_layout/app/javascript/controllers/nested_controller.js
pg_rails-7.6.21 pg_layout/app/javascript/controllers/nested_controller.js
pg_rails-7.6.21.pre.11 pg_layout/app/javascript/controllers/nested_controller.js
pg_rails-7.6.21.pre.10 pg_layout/app/javascript/controllers/nested_controller.js
pg_rails-7.6.21.pre.9 pg_layout/app/javascript/controllers/nested_controller.js
pg_rails-7.6.21.pre.8 pg_layout/app/javascript/controllers/nested_controller.js
pg_rails-7.6.21.pre.7 pg_layout/app/javascript/controllers/nested_controller.js
pg_rails-7.6.21.pre.6 pg_layout/app/javascript/controllers/nested_controller.js
pg_rails-7.6.21.pre.5 pg_layout/app/javascript/controllers/nested_controller.js