From 11341fdb3a1664ba58edf729ed46e04cd0e20ed6 Mon Sep 17 00:00:00 2001 From: Javan Makhmali Date: Thu, 30 Mar 2017 14:41:17 -0400 Subject: Reorganize rails-ujs files --- .../javascripts/rails-ujs/features/method.coffee | 34 ++++++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 actionview/app/assets/javascripts/rails-ujs/features/method.coffee (limited to 'actionview/app/assets/javascripts/rails-ujs/features/method.coffee') diff --git a/actionview/app/assets/javascripts/rails-ujs/features/method.coffee b/actionview/app/assets/javascripts/rails-ujs/features/method.coffee new file mode 100644 index 0000000000..d04d9414dd --- /dev/null +++ b/actionview/app/assets/javascripts/rails-ujs/features/method.coffee @@ -0,0 +1,34 @@ +#= 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) -- cgit v1.2.3