Sha256: 3360d43e134f83816af4b427dfd899de7253cecd36b92c83ba95c356c29b813a

Contents?: true

Size: 1.64 KB

Versions: 5

Compression:

Stored size: 1.64 KB

Contents

// This code was heavily inspired by the rails-ujs project.
// Copyright (c) 2007-2021 Rails Core team.
import { matches } from "./dom"

const toArray = (e) => Array.prototype.slice.call(e)

export const serializeElement = (element, additionalParam) => {
  let inputs = [element]
  if (matches(element, "form")) {
    inputs = toArray(element.elements)
  }
  const params = []

  inputs.forEach(function (input) {
    if (!input.name || input.disabled) {
      return
    }

    if (matches(input, "fieldset[disabled] *")) {
      return
    }

    if (matches(input, "select")) {
      return toArray(input.options).forEach(function (option) {
        if (option.selected) {
          return params.push({ name: input.name, value: option.value })
        }
      })
    } else if (input.checked || ["radio", "checkbox", "submit"].indexOf(input.type) === -1) {
      return params.push({ name: input.name, value: input.value })
    }
  })

  if (additionalParam) {
    params.push(additionalParam)
  }

  return params
    .map(function (param) {
      if (param.name != null) {
        return `${encodeURIComponent(param.name)}=${encodeURIComponent(param.value)}`
      } else {
        return param
      }
    })
    .join("&")
}

// Helper function that returns form elements that match the specified CSS selector
// If form is actually a "form" element this will return associated elements outside the from that have
// the html form attribute set
export const formElements = (form, selector) => {
  if (matches(form, "form")) {
    return toArray(form.elements).filter((el) => matches(el, selector))
  } else {
    return toArray(form.querySelectorAll(selector))
  }
}

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
shoelace-rails-0.4.1 src/turbolinks/utils/form.ts
shoelace-rails-0.4.0 src/turbolinks/utils/form.ts
shoelace-rails-0.3.0 src/turbolinks/utils/form.ts
shoelace-rails-0.2.0 src/turbolinks/utils/form.ts
shoelace-rails-0.1.0 src/turbolinks/utils/form.ts