2017-09-18 14:13:39 +02:00
|
|
|
var on = require("../events/on")
|
2018-03-06 10:06:38 +00:00
|
|
|
var clone = require("../util/clone")
|
2017-09-18 14:13:39 +02:00
|
|
|
|
2018-04-09 23:36:32 -04:00
|
|
|
var attrState = "data-pjax-state"
|
2017-09-18 14:13:39 +02:00
|
|
|
|
2018-01-09 00:44:20 -05:00
|
|
|
var formAction = function(el, event) {
|
2018-04-09 23:36:32 -04:00
|
|
|
if (isDefaultPrevented(event)) {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2018-01-23 13:22:31 -05:00
|
|
|
// Since loadUrl modifies options and we may add our own modifications below,
|
|
|
|
|
// clone it so the changes don't persist
|
2018-02-02 09:52:44 -05:00
|
|
|
var options = clone(this.options)
|
2018-01-22 13:31:11 -05:00
|
|
|
|
2018-01-23 13:22:31 -05:00
|
|
|
// Initialize requestOptions
|
2018-01-22 13:31:11 -05:00
|
|
|
options.requestOptions = {
|
2018-01-09 00:44:20 -05:00
|
|
|
requestUrl: el.getAttribute("action") || window.location.href,
|
2018-04-29 15:05:22 -04:00
|
|
|
requestMethod: el.getAttribute("method") || "GET"
|
2017-09-18 14:13:39 +02:00
|
|
|
}
|
|
|
|
|
|
2018-01-09 00:44:20 -05:00
|
|
|
// create a testable virtual link of the form action
|
2018-02-02 09:52:44 -05:00
|
|
|
var virtLinkElement = document.createElement("a")
|
|
|
|
|
virtLinkElement.setAttribute("href", options.requestOptions.requestUrl)
|
2017-09-18 14:13:39 +02:00
|
|
|
|
2018-04-09 23:36:32 -04:00
|
|
|
var attrValue = checkIfShouldAbort(virtLinkElement, options)
|
|
|
|
|
if (attrValue) {
|
|
|
|
|
el.setAttribute(attrState, attrValue)
|
2018-02-02 09:52:44 -05:00
|
|
|
return
|
2017-09-18 14:13:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
event.preventDefault()
|
|
|
|
|
|
2018-04-29 15:05:22 -04:00
|
|
|
if (el.enctype === "multipart/form-data") {
|
|
|
|
|
options.requestOptions.formData = new FormData(el)
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
options.requestOptions.requestParams = parseFormElements(el)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
el.setAttribute(attrState, "submit")
|
|
|
|
|
|
|
|
|
|
options.triggerElement = el
|
|
|
|
|
this.loadUrl(virtLinkElement.href, options)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function parseFormElements(el) {
|
|
|
|
|
var requestParams = []
|
|
|
|
|
|
2018-01-09 00:44:20 -05:00
|
|
|
for (var elementKey in el.elements) {
|
2018-04-12 11:08:59 +01:00
|
|
|
if (Number.isNaN(Number(elementKey))) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
2018-02-02 09:52:44 -05:00
|
|
|
var element = el.elements[elementKey]
|
2018-04-12 11:08:59 +01:00
|
|
|
var tagName = element.tagName.toLowerCase()
|
2018-01-09 00:44:20 -05:00
|
|
|
// jscs:disable disallowImplicitTypeConversion
|
2018-04-12 11:08:59 +01:00
|
|
|
if (!!element.name && element.attributes !== undefined && tagName !== "button") {
|
2018-01-09 00:44:20 -05:00
|
|
|
// jscs:enable disallowImplicitTypeConversion
|
2018-04-26 09:27:50 -04:00
|
|
|
var type = element.attributes.type
|
|
|
|
|
|
2018-04-12 11:08:59 +01:00
|
|
|
if ((!type || type.value !== "checkbox" && type.value !== "radio") || element.checked) {
|
|
|
|
|
// Build array of values to submit
|
|
|
|
|
var values = []
|
|
|
|
|
|
|
|
|
|
if (tagName === "select") {
|
|
|
|
|
var opt
|
|
|
|
|
|
|
|
|
|
for (var i = 0; i < element.options.length; i++) {
|
|
|
|
|
opt = element.options[i]
|
|
|
|
|
if (opt.selected) {
|
|
|
|
|
values.push(opt.value || opt.text)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
values.push(element.value)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for (var j = 0; j < values.length; j++) {
|
2018-04-29 15:05:22 -04:00
|
|
|
requestParams.push({
|
2018-04-12 11:08:59 +01:00
|
|
|
name: encodeURIComponent(element.name),
|
|
|
|
|
value: encodeURIComponent(values[j])
|
|
|
|
|
})
|
|
|
|
|
}
|
2017-09-18 14:13:39 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-04-29 15:05:22 -04:00
|
|
|
return requestParams
|
2018-02-02 09:52:44 -05:00
|
|
|
}
|
2017-09-18 14:13:39 +02:00
|
|
|
|
2018-04-09 23:36:32 -04:00
|
|
|
function checkIfShouldAbort(virtLinkElement, options) {
|
|
|
|
|
// Ignore external links.
|
|
|
|
|
if (virtLinkElement.protocol !== window.location.protocol || virtLinkElement.host !== window.location.host) {
|
|
|
|
|
return "external"
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Ignore click if we are on an anchor on the same page
|
|
|
|
|
if (virtLinkElement.hash && virtLinkElement.href.replace(virtLinkElement.hash, "") === window.location.href.replace(location.hash, "")) {
|
|
|
|
|
return "anchor"
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Ignore empty anchor "foo.html#"
|
|
|
|
|
if (virtLinkElement.href === window.location.href.split("#")[0] + "#") {
|
|
|
|
|
return "anchor-empty"
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// if declared as a full reload, just normally submit the form
|
|
|
|
|
if (options.currentUrlFullReload && virtLinkElement.href === window.location.href.split("#")[0]) {
|
|
|
|
|
return "reload"
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-09-18 14:13:39 +02:00
|
|
|
var isDefaultPrevented = function(event) {
|
2018-02-02 09:52:44 -05:00
|
|
|
return event.defaultPrevented || event.returnValue === false
|
|
|
|
|
}
|
2017-09-18 14:13:39 +02:00
|
|
|
|
|
|
|
|
module.exports = function(el) {
|
|
|
|
|
var that = this
|
|
|
|
|
|
2018-04-09 23:36:32 -04:00
|
|
|
el.setAttribute(attrState, "")
|
2017-09-18 14:13:39 +02:00
|
|
|
|
2018-04-09 23:36:32 -04:00
|
|
|
on(el, "submit", function(event) {
|
2017-09-18 14:13:39 +02:00
|
|
|
formAction.call(that, el, event)
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
on(el, "keyup", function(event) {
|
2018-02-02 09:52:44 -05:00
|
|
|
if (event.keyCode === 13) {
|
2017-09-18 14:13:39 +02:00
|
|
|
formAction.call(that, el, event)
|
|
|
|
|
}
|
|
|
|
|
}.bind(this))
|
|
|
|
|
}
|