* Fix bug when checking if elements were parsed already
parse-element.js checks if the element was already parsed by
checking for the `data-pjax-click-state` attribute. However, this
attribute was not added until the link is clicked.
Originally, there was a separate attribute, `data-pjax-enabled`,
which tracked if the element was parsed already, but that was
changed in 9a86044.
This commit merges the attributes for mouse clicks and key presses
into one and adds that attribute when the element is initially
parsed.
* More bug fixes
* Fix documentation for currentUrlFullReload
* Ignore lines from coverage if they can't be tested
* Refactor attach-link and attach-form
* Fix and refactors tests
* Add tests
* Add TS definitions for options.requestOptions
* Code cleanup
75 lines
2.2 KiB
JavaScript
75 lines
2.2 KiB
JavaScript
var updateQueryString = require("./util/update-query-string");
|
|
|
|
module.exports = function(location, options, callback) {
|
|
options = options || {}
|
|
var queryString
|
|
var requestOptions = options.requestOptions || {}
|
|
var requestMethod = (requestOptions.requestMethod || "GET").toUpperCase()
|
|
var requestParams = requestOptions.requestParams || null
|
|
var requestPayload = null
|
|
var request = new XMLHttpRequest()
|
|
var timeout = options.timeout || 0
|
|
|
|
request.onreadystatechange = function() {
|
|
if (request.readyState === 4) {
|
|
if (request.status === 200) {
|
|
callback(request.responseText, request, location)
|
|
}
|
|
else if (request.status !== 0) {
|
|
callback(null, request, location)
|
|
}
|
|
}
|
|
}
|
|
|
|
request.onerror = function(e) {
|
|
console.log(e)
|
|
callback(null, request, location)
|
|
}
|
|
|
|
request.ontimeout = function() {
|
|
callback(null, request, location)
|
|
}
|
|
|
|
// Prepare the request payload for forms, if available
|
|
if (requestParams && requestParams.length) {
|
|
// Build query string
|
|
queryString = (requestParams.map(function(param) {return param.name + "=" + param.value})).join("&")
|
|
|
|
switch (requestMethod) {
|
|
case "GET":
|
|
// Reset query string to avoid an issue with repeat submissions where checkboxes that were
|
|
// previously checked are incorrectly preserved
|
|
location = location.split("?")[0]
|
|
|
|
// Append new query string
|
|
location += "?" + queryString
|
|
break
|
|
|
|
case "POST":
|
|
// Send query string as request payload
|
|
requestPayload = queryString
|
|
break
|
|
}
|
|
}
|
|
|
|
// Add a timestamp as part of the query string if cache busting is enabled
|
|
if (options.cacheBust) {
|
|
location = updateQueryString(location, "t", Date.now())
|
|
}
|
|
|
|
request.open(requestMethod, location, true)
|
|
request.timeout = timeout
|
|
request.setRequestHeader("X-Requested-With", "XMLHttpRequest")
|
|
request.setRequestHeader("X-PJAX", "true")
|
|
request.setRequestHeader("X-PJAX-Selectors", JSON.stringify(options.selectors))
|
|
|
|
// Send the proper header information for POST forms
|
|
if (requestPayload && requestMethod === "POST") {
|
|
request.setRequestHeader("Content-Type", "application/x-www-form-urlencoded")
|
|
}
|
|
|
|
request.send(requestPayload)
|
|
|
|
return request
|
|
}
|