aboutsummaryrefslogtreecommitdiffstats
path: root/actionview/app/assets/javascripts/rails-ujs/utils/form.coffee
blob: 736cab08dbc00b0c75373ffb41ce9712dda93d20 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#= require ./dom

{ matches } = Rails

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

Rails.serializeElement = (element, additionalParam) ->
  inputs = [element]
  inputs = toArray(element.elements) if matches(element, 'form')
  params = []

  inputs.forEach (input) ->
    return if !input.name || input.disabled
    if matches(input, 'select')
      toArray(input.options).forEach (option) ->
        params.push(name: input.name, value: option.value) if option.selected
    else if input.checked or ['radio', 'checkbox', 'submit'].indexOf(input.type) == -1
      params.push(name: input.name, value: input.value)

  params.push(additionalParam) if additionalParam

  params.map (param) ->
    if param.name?
      "#{encodeURIComponent(param.name)}=#{encodeURIComponent(param.value)}"
    else
      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
Rails.formElements = (form, selector) ->
  if matches(form, 'form')
    toArray(form.elements).filter (el) -> matches(el, selector)
  else
    toArray(form.querySelectorAll(selector))