Files
pjax/lib/proto/attach-form.js
2018-04-12 18:03:28 -04:00

120 lines
3.3 KiB
JavaScript

var on = require("../events/on")
var clone = require("../util/clone")
var attrState = "data-pjax-state"
var formAction = function(el, event) {
if (isDefaultPrevented(event)) {
return
}
// Since loadUrl modifies options and we may add our own modifications below,
// clone it so the changes don't persist
var options = clone(this.options)
// Initialize requestOptions
options.requestOptions = {
requestUrl: el.getAttribute("action") || window.location.href,
requestMethod: el.getAttribute("method") || "GET",
requestParams: []
}
// create a testable virtual link of the form action
var virtLinkElement = document.createElement("a")
virtLinkElement.setAttribute("href", options.requestOptions.requestUrl)
var attrValue = checkIfShouldAbort(virtLinkElement, options)
if (attrValue) {
el.setAttribute(attrState, attrValue)
return
}
event.preventDefault()
for (var elementKey in el.elements) {
if (Number.isNaN(Number(elementKey))) {
continue;
}
var element = el.elements[elementKey]
var tagName = element.tagName.toLowerCase()
// jscs:disable disallowImplicitTypeConversion
if (!!element.name && element.attributes !== undefined && tagName !== "button") {
var type = element.attributes.type
// jscs:enable disallowImplicitTypeConversion
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++) {
options.requestOptions.requestParams.push({
name: encodeURIComponent(element.name),
value: encodeURIComponent(values[j])
})
}
}
}
}
el.setAttribute(attrState, "submit")
options.triggerElement = el
this.loadUrl(virtLinkElement.href, options)
}
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"
}
}
var isDefaultPrevented = function(event) {
return event.defaultPrevented || event.returnValue === false
}
module.exports = function(el) {
var that = this
el.setAttribute(attrState, "")
on(el, "submit", function(event) {
formAction.call(that, el, event)
})
on(el, "keyup", function(event) {
if (event.keyCode === 13) {
formAction.call(that, el, event)
}
}.bind(this))
}