Sha256: 1d8e6f41d83c893564df836715f17f56437ac000cbac28810959b6aba103acdf

Contents?: true

Size: 1.27 KB

Versions: 2

Compression:

Stored size: 1.27 KB

Contents

class removeFields {
  // This executes when the function is instantiated.
  constructor() {
    this.iterateLinks()
  }

  iterateLinks() {
    document.querySelectorAll('.remove_fields').forEach(link => {
      link.addEventListener('click', e => {
        this.handleClick(link, e)
      })
    })
  }

  handleClick(link, e) {
    // Stop the function from executing if a link or event were not passed into the function.
    if (!link || !e) return
    // Prevent the browser from following the URL.
    e.preventDefault()
    // Find the parent wrapper for the set of nested fields.
    let fieldParent = link.closest('.nested-fields')
    // If there is a parent wrapper, find the hidden delete field.
    let deleteField = fieldParent
      ? fieldParent.querySelector('input[type="hidden"]')
      : null
    // If there is a delete field, update the value to `1` and hide the corresponding nested fields.
    if (deleteField) {
      deleteField.value = 1
      fieldParent.style.display = 'none'
    }
  }
}

// Wait for turbolinks to load, otherwise `document.querySelectorAll()` won't work
window.addEventListener('load', () => new removeFields())
window.addEventListener('works_cited:load', () => new removeFields())
window.addEventListener('works_cited:add_fields', () => new removeFields())

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
works_cited-0.1.16 app/assets/javascripts/works_cited/removeFields.js
works_cited-0.1.15 app/assets/javascripts/works_cited/removeFields.js