#= require_tree ../utils
{ stopEverything } = Rails
# Handles "data-method" on links such as:
# Delete
Rails.handleMethod = (e) ->
link = this
method = link.getAttribute('data-method')
return unless method
href = Rails.href(link)
csrfToken = Rails.csrfToken()
csrfParam = Rails.csrfParam()
form = document.createElement('form')
formContent = ""
if csrfParam? and csrfToken? and not Rails.isCrossDomain(href)
formContent += ""
# Must trigger submit by click on a button, else "submit" event handler won't work!
# https://developer.mozilla.org/en-US/docs/Web/API/HTMLFormElement/submit
formContent += ''
form.method = 'post'
form.action = href
form.target = link.target
form.innerHTML = formContent
form.style.display = 'none'
document.body.appendChild(form)
form.querySelector('[type="submit"]').click()
stopEverything(e)