Sha256: d3f8ef8b597b59d73d5d4606437e4d43f91a023474a6737b99b8dd307c6b9585

Contents?: true

Size: 1.52 KB

Versions: 10

Compression:

Stored size: 1.52 KB

Contents

import { Controller } from '@hotwired/stimulus'

export default class extends Controller {
  static values = {
    url: String
  }

  connect () {
    this.button = this.element
    this.button.addEventListener('click', (event) => {
      event.preventDefault()
      this.requestPreview()
    })
  }

  requestPreview () {
    const form = this.buildFakeForm()

    // Insert in DOM
    document.body.appendChild(form)

    // Submit form
    form.submit()

    // Remove from DOM
    document.body.removeChild(form)
  }

  buildFakeForm () {
    const form = this.form().cloneNode(true)

    // Empty [id] fields
    const idInputs = form.querySelectorAll('input[name$="[id]"], select[name$="[id]"], textarea[name$="[id]"], button[name$="[id]"]')
    idInputs.forEach((input) => {
      input.value = ''
    })

    // Set preview action
    form.setAttribute('action', this.urlValue)

    // Set target to blank
    form.setAttribute('target', '_blank')

    // Refresh authenticity token
    const authenticityTokenInput = form.querySelector('input[name="authenticity_token"]')
    authenticityTokenInput.value = this.getAuthenticityToken()

    // Remove method input if present (to force POST)
    form.querySelector('input[name="_method"]')?.remove()

    // Ensure POST method
    form.setAttribute('method', 'POST')

    return form
  }

  getAuthenticityToken () {
    const tokenTag = document.querySelector('meta[name="csrf-token"]')
    return tokenTag.getAttribute('content')
  }

  form () {
    return this.button.closest('form')
  }
}

Version data entries

10 entries across 10 versions & 1 rubygems

Version Path
formstrap-0.4.5 app/assets/javascripts/formstrap/controllers/preview_controller.js
formstrap-0.4.4 app/assets/javascripts/formstrap/controllers/preview_controller.js
formstrap-0.4.3 app/assets/javascripts/formstrap/controllers/preview_controller.js
formstrap-0.4.2 app/assets/javascripts/formstrap/controllers/preview_controller.js
formstrap-0.3.5 app/assets/javascripts/formstrap/controllers/preview_controller.js
formstrap-0.3.4 app/assets/javascripts/formstrap/controllers/preview_controller.js
formstrap-0.3.3 app/assets/javascripts/formstrap/controllers/preview_controller.js
formstrap-0.3.2 app/assets/javascripts/formstrap/controllers/preview_controller.js
formstrap-0.3.1 app/assets/javascripts/formstrap/controllers/preview_controller.js
formstrap-0.3.0 app/assets/javascripts/formstrap/controllers/preview_controller.js