Code cleanup
This commit is contained in:
committed by
Robin North
parent
57aed828ac
commit
6fb509a021
7
index.js
7
index.js
@@ -23,7 +23,7 @@ var Pjax = function(options) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
var parseOptions = require("./lib/proto/parse-options.js");
|
var parseOptions = require("./lib/proto/parse-options.js");
|
||||||
parseOptions.apply(this,[options])
|
parseOptions.call(this,options)
|
||||||
this.log("Pjax options", this.options)
|
this.log("Pjax options", this.options)
|
||||||
|
|
||||||
if (this.options.scrollRestoration && "scrollRestoration" in history) {
|
if (this.options.scrollRestoration && "scrollRestoration" in history) {
|
||||||
@@ -168,7 +168,7 @@ Pjax.prototype = {
|
|||||||
this.request = this.doRequest(href, options.requestOptions, function(html, request) {
|
this.request = this.doRequest(href, options.requestOptions, function(html, request) {
|
||||||
// Fail if unable to load HTML via AJAX
|
// Fail if unable to load HTML via AJAX
|
||||||
if (html === false) {
|
if (html === false) {
|
||||||
trigger(document,"pjax:complete pjax:error", options)
|
trigger(document, "pjax:complete pjax:error", options)
|
||||||
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@@ -218,8 +218,7 @@ Pjax.prototype = {
|
|||||||
if (console && console.error) {
|
if (console && console.error) {
|
||||||
console.error("Pjax switch fail: ", e)
|
console.error("Pjax switch fail: ", e)
|
||||||
}
|
}
|
||||||
this.latestChance(href)
|
return this.latestChance(href)
|
||||||
return
|
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
throw e
|
throw e
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
module.exports = function(obj) {
|
module.exports = function(obj) {
|
||||||
if (null === obj || "object" != typeof obj) {
|
if (null === obj || "object" !== typeof obj) {
|
||||||
return obj
|
return obj
|
||||||
}
|
}
|
||||||
var copy = obj.constructor()
|
var copy = obj.constructor()
|
||||||
|
|||||||
@@ -13,12 +13,12 @@ module.exports = function(el) {
|
|||||||
|
|
||||||
script.type = "text/javascript"
|
script.type = "text/javascript"
|
||||||
|
|
||||||
if (src != "") {
|
if (src !== "") {
|
||||||
script.src = src;
|
script.src = src;
|
||||||
script.async = false; // force synchronous loading of peripheral js
|
script.async = false; // force synchronous loading of peripheral js
|
||||||
}
|
}
|
||||||
|
|
||||||
if (code != "") {
|
if (code !== "") {
|
||||||
try {
|
try {
|
||||||
script.appendChild(document.createTextNode(code))
|
script.appendChild(document.createTextNode(code))
|
||||||
}
|
}
|
||||||
@@ -31,7 +31,7 @@ module.exports = function(el) {
|
|||||||
// execute
|
// execute
|
||||||
parent.appendChild(script);
|
parent.appendChild(script);
|
||||||
// avoid pollution only in head or body tags
|
// avoid pollution only in head or body tags
|
||||||
if (["head","body"].indexOf(parent.tagName.toLowerCase()) > 0) {
|
if (["head", "body"].indexOf(parent.tagName.toLowerCase()) > 0) {
|
||||||
parent.removeChild(script)
|
parent.removeChild(script)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -13,7 +13,7 @@ var formAction = function(el, event) {
|
|||||||
// Initialize requestOptions
|
// Initialize requestOptions
|
||||||
options.requestOptions = {
|
options.requestOptions = {
|
||||||
requestUrl: el.getAttribute("action") || window.location.href,
|
requestUrl: el.getAttribute("action") || window.location.href,
|
||||||
requestMethod: el.getAttribute("method") || "GET",
|
requestMethod: el.getAttribute("method") || "GET"
|
||||||
}
|
}
|
||||||
|
|
||||||
// create a testable virtual link of the form action
|
// create a testable virtual link of the form action
|
||||||
@@ -93,7 +93,7 @@ module.exports = function(el) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
if (event.keyCode == 13) {
|
if (event.keyCode === 13) {
|
||||||
formAction.call(that, el, event)
|
formAction.call(that, el, event)
|
||||||
}
|
}
|
||||||
}.bind(this))
|
}.bind(this))
|
||||||
|
|||||||
@@ -91,7 +91,7 @@ module.exports = function(el) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
if (event.keyCode == 13) {
|
if (event.keyCode === 13) {
|
||||||
linkAction.call(that, el, event)
|
linkAction.call(that, el, event)
|
||||||
}
|
}
|
||||||
}.bind(this))
|
}.bind(this))
|
||||||
|
|||||||
@@ -3,6 +3,7 @@
|
|||||||
var defaultSwitches = require("../switches")
|
var defaultSwitches = require("../switches")
|
||||||
|
|
||||||
module.exports = function(options) {
|
module.exports = function(options) {
|
||||||
|
options = options || {}
|
||||||
options.elements = options.elements || "a[href], form[action]"
|
options.elements = options.elements || "a[href], form[action]"
|
||||||
options.selectors = options.selectors || ["title", ".js-Pjax"]
|
options.selectors = options.selectors || ["title", ".js-Pjax"]
|
||||||
options.switches = options.switches || {}
|
options.switches = options.switches || {}
|
||||||
|
|||||||
@@ -35,7 +35,7 @@ module.exports = function(location, options, callback) {
|
|||||||
request.setRequestHeader("X-PJAX", "true")
|
request.setRequestHeader("X-PJAX", "true")
|
||||||
|
|
||||||
// Add the request payload if available
|
// Add the request payload if available
|
||||||
if (options.requestPayloadString != undefined && options.requestPayloadString != "") {
|
if (options.requestPayloadString !== undefined && options.requestPayloadString !== "") {
|
||||||
// Send the proper header information along with the request
|
// Send the proper header information along with the request
|
||||||
request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
|
request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -40,7 +40,7 @@ module.exports = {
|
|||||||
var animationEventNames = "animationend webkitAnimationEnd MSAnimationEnd oanimationend"
|
var animationEventNames = "animationend webkitAnimationEnd MSAnimationEnd oanimationend"
|
||||||
var animatedElsNumber = 0
|
var animatedElsNumber = 0
|
||||||
var sexyAnimationEnd = function(e) {
|
var sexyAnimationEnd = function(e) {
|
||||||
if (e.target != e.currentTarget) {
|
if (e.target !== e.currentTarget) {
|
||||||
// end triggered by an animation on a child
|
// end triggered by an animation on a child
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -18,8 +18,8 @@ if (!("responseURL" in XMLHttpRequest.prototype)) {
|
|||||||
tape("test aborting xhr request", function(t) {
|
tape("test aborting xhr request", function(t) {
|
||||||
var requestCacheBust = sendRequest.bind({
|
var requestCacheBust = sendRequest.bind({
|
||||||
options: {
|
options: {
|
||||||
cacheBust: true,
|
cacheBust: true
|
||||||
},
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
t.test("- pending request is aborted", function(t) {
|
t.test("- pending request is aborted", function(t) {
|
||||||
|
|||||||
@@ -80,12 +80,11 @@ tape("test attach form preventDefaulted events", function(t) {
|
|||||||
tape("test options are not modified by attachForm", function(t) {
|
tape("test options are not modified by attachForm", function(t) {
|
||||||
var form = document.createElement("form")
|
var form = document.createElement("form")
|
||||||
var options = {foo: "bar"}
|
var options = {foo: "bar"}
|
||||||
var loadUrl = () => {}
|
var loadUrl = function() {}
|
||||||
|
|
||||||
attachForm.call({options, loadUrl}, form)
|
attachForm.call({options: options, loadUrl: loadUrl}, form)
|
||||||
|
|
||||||
var internalUri = window.location.protocol + "//" + window.location.host + window.location.pathname + window.location.search
|
form.action = window.location.protocol + "//" + window.location.host + window.location.pathname + window.location.search
|
||||||
form.action = internalUri
|
|
||||||
form.method = "GET"
|
form.method = "GET"
|
||||||
trigger(form, "submit")
|
trigger(form, "submit")
|
||||||
|
|
||||||
|
|||||||
@@ -79,12 +79,11 @@ tape("test attach link preventDefaulted events", function(t) {
|
|||||||
tape("test options are not modified by attachLink", function(t) {
|
tape("test options are not modified by attachLink", function(t) {
|
||||||
var a = document.createElement("a")
|
var a = document.createElement("a")
|
||||||
var options = {foo: "bar"}
|
var options = {foo: "bar"}
|
||||||
var loadUrl = () => {};
|
var loadUrl = function() {};
|
||||||
|
|
||||||
attachLink.call({options, loadUrl}, a)
|
attachLink.call({options: options, loadUrl: loadUrl}, a)
|
||||||
|
|
||||||
var internalUri = window.location.protocol + "//" + window.location.host + window.location.pathname + window.location.search
|
a.href = window.location.protocol + "//" + window.location.host + window.location.pathname + window.location.search
|
||||||
a.href = internalUri
|
|
||||||
|
|
||||||
trigger(a, "click")
|
trigger(a, "click")
|
||||||
|
|
||||||
|
|||||||
@@ -20,8 +20,8 @@ tape("test xhr request", function(t) {
|
|||||||
t.test("- request is made, gets a result, and is cache-busted", function(t) {
|
t.test("- request is made, gets a result, and is cache-busted", function(t) {
|
||||||
var requestCacheBust = sendRequest.bind({
|
var requestCacheBust = sendRequest.bind({
|
||||||
options: {
|
options: {
|
||||||
cacheBust: true,
|
cacheBust: true
|
||||||
},
|
}
|
||||||
});
|
});
|
||||||
var r = requestCacheBust(url, {}, function(result) {
|
var r = requestCacheBust(url, {}, function(result) {
|
||||||
t.equal(r.responseURL.indexOf("?"), url.length, "XHR URL is cache-busted when configured to be")
|
t.equal(r.responseURL.indexOf("?"), url.length, "XHR URL is cache-busted when configured to be")
|
||||||
@@ -38,8 +38,8 @@ tape("test xhr request", function(t) {
|
|||||||
t.test("- request is not cache-busted when configured not to be", function(t) {
|
t.test("- request is not cache-busted when configured not to be", function(t) {
|
||||||
var requestNoCacheBust = sendRequest.bind({
|
var requestNoCacheBust = sendRequest.bind({
|
||||||
options: {
|
options: {
|
||||||
cacheBust: false,
|
cacheBust: false
|
||||||
},
|
}
|
||||||
});
|
});
|
||||||
var r = requestNoCacheBust(url, {}, function() {
|
var r = requestNoCacheBust(url, {}, function() {
|
||||||
t.equal(r.responseURL, url, "XHR URL is left untouched")
|
t.equal(r.responseURL, url, "XHR URL is left untouched")
|
||||||
|
|||||||
Reference in New Issue
Block a user