2017-09-18 14:13:39 +02:00
|
|
|
require("../polyfills/Function.prototype.bind")
|
|
|
|
|
|
|
|
|
|
var on = require("../events/on")
|
|
|
|
|
var clone = require("../clone")
|
|
|
|
|
|
|
|
|
|
var attrClick = "data-pjax-click-state"
|
|
|
|
|
|
2018-01-09 00:44:20 -05:00
|
|
|
var formAction = function(el, event) {
|
2018-01-22 13:31:11 -05:00
|
|
|
// Since we'll be modifying request options, clone the existing options
|
|
|
|
|
// so these changes don't persist
|
|
|
|
|
var options = clone(this.options);
|
|
|
|
|
|
|
|
|
|
options.requestOptions = {
|
2018-01-09 00:44:20 -05:00
|
|
|
requestUrl: el.getAttribute("action") || window.location.href,
|
|
|
|
|
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
|
|
|
|
|
var virtLinkElement = document.createElement("a");
|
2018-01-22 13:31:11 -05:00
|
|
|
virtLinkElement.setAttribute("href", options.requestOptions.requestUrl);
|
2017-09-18 14:13:39 +02:00
|
|
|
|
|
|
|
|
// Ignore external links.
|
|
|
|
|
if (virtLinkElement.protocol !== window.location.protocol || virtLinkElement.host !== window.location.host) {
|
|
|
|
|
el.setAttribute(attrClick, "external");
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Ignore click if we are on an anchor on the same page
|
|
|
|
|
if (virtLinkElement.pathname === window.location.pathname && virtLinkElement.hash.length > 0) {
|
|
|
|
|
el.setAttribute(attrClick, "anchor-present");
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Ignore empty anchor "foo.html#"
|
|
|
|
|
if (virtLinkElement.href === window.location.href.split("#")[0] + "#") {
|
|
|
|
|
el.setAttribute(attrClick, "anchor-empty")
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// if declared as a full reload, just normally submit the form
|
2018-01-22 13:31:11 -05:00
|
|
|
if (options.currentUrlFullReload) {
|
2017-09-18 14:13:39 +02:00
|
|
|
el.setAttribute(attrClick, "reload");
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
event.preventDefault()
|
|
|
|
|
|
|
|
|
|
var paramObject = [];
|
2018-01-09 00:44:20 -05:00
|
|
|
for (var elementKey in el.elements) {
|
2017-09-18 14:13:39 +02:00
|
|
|
var element = el.elements[elementKey];
|
2018-01-09 00:44:20 -05:00
|
|
|
// jscs:disable disallowImplicitTypeConversion
|
|
|
|
|
if (!!element.name && element.attributes !== undefined && element.tagName.toLowerCase() !== "button") {
|
|
|
|
|
// jscs:enable disallowImplicitTypeConversion
|
|
|
|
|
if ((element.attributes.type !== "checkbox" && element.attributes.type !== "radio") || element.checked) {
|
|
|
|
|
paramObject.push({name: encodeURIComponent(element.name), value: encodeURIComponent(element.value)});
|
2017-09-18 14:13:39 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-01-09 00:44:20 -05:00
|
|
|
// Creating a getString
|
|
|
|
|
var paramsString = (paramObject.map(function(value) {return value.name + "=" + value.value;})).join("&");
|
2017-09-18 14:13:39 +02:00
|
|
|
|
2018-01-22 13:31:11 -05:00
|
|
|
options.requestOptions.requestPayload = paramObject;
|
|
|
|
|
options.requestOptions.requestPayloadString = paramsString;
|
2017-09-18 14:13:39 +02:00
|
|
|
|
|
|
|
|
el.setAttribute(attrClick, "submit");
|
|
|
|
|
|
2018-01-22 13:31:11 -05:00
|
|
|
|
2017-12-21 00:12:09 -05:00
|
|
|
options.triggerElement = el;
|
|
|
|
|
this.loadUrl(virtLinkElement.href, options);
|
2017-09-18 14:13:39 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
var isDefaultPrevented = function(event) {
|
|
|
|
|
return event.defaultPrevented || event.returnValue === false;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
module.exports = function(el) {
|
|
|
|
|
var that = this
|
|
|
|
|
|
|
|
|
|
on(el, "submit", function(event) {
|
|
|
|
|
if (isDefaultPrevented(event)) {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
formAction.call(that, el, event)
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
on(el, "keyup", function(event) {
|
|
|
|
|
if (isDefaultPrevented(event)) {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (event.keyCode == 13) {
|
|
|
|
|
formAction.call(that, el, event)
|
|
|
|
|
}
|
|
|
|
|
}.bind(this))
|
|
|
|
|
}
|