diff options
author | Javan Makhmali <javan@javan.us> | 2017-03-30 14:41:17 -0400 |
---|---|---|
committer | Javan Makhmali <javan@javan.us> | 2017-03-30 14:41:17 -0400 |
commit | 11341fdb3a1664ba58edf729ed46e04cd0e20ed6 (patch) | |
tree | 561eaca65797dee2ffd5122c02344901abd976a8 /actionview/app/assets/javascripts/rails-ujs/utils/form.coffee | |
parent | e0ac3498c669fad7aab18436fc84f9e52b72bc8a (diff) | |
download | rails-11341fdb3a1664ba58edf729ed46e04cd0e20ed6.tar.gz rails-11341fdb3a1664ba58edf729ed46e04cd0e20ed6.tar.bz2 rails-11341fdb3a1664ba58edf729ed46e04cd0e20ed6.zip |
Reorganize rails-ujs files
Diffstat (limited to 'actionview/app/assets/javascripts/rails-ujs/utils/form.coffee')
-rw-r--r-- | actionview/app/assets/javascripts/rails-ujs/utils/form.coffee | 36 |
1 files changed, 36 insertions, 0 deletions
diff --git a/actionview/app/assets/javascripts/rails-ujs/utils/form.coffee b/actionview/app/assets/javascripts/rails-ujs/utils/form.coffee new file mode 100644 index 0000000000..5fa337b518 --- /dev/null +++ b/actionview/app/assets/javascripts/rails-ujs/utils/form.coffee @@ -0,0 +1,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 unless input.name + 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)) |