Code style cleanup, remove redundant comments

This commit is contained in:
Robin North
2018-01-31 22:17:06 +00:00
parent 9971c4c4f0
commit 1eb43f73fe
15 changed files with 87 additions and 120 deletions

View File

@@ -1,19 +1,14 @@
var clone = require("./lib/clone.js") var clone = require("./lib/clone.js")
var executeScripts = require("./lib/execute-scripts.js") var executeScripts = require("./lib/execute-scripts.js")
var forEachEls = require("./lib/foreach-els.js") var forEachEls = require("./lib/foreach-els.js")
var switches = require("./lib/switches")
var newUid = require("./lib/uniqueid.js") var newUid = require("./lib/uniqueid.js")
var noop = require("./lib/util/noop")
var contains = require("./lib/util/contains.js")
var on = require("./lib/events/on.js") var on = require("./lib/events/on.js")
// var off = require("./lib/events/on.js")
var trigger = require("./lib/events/trigger.js") var trigger = require("./lib/events/trigger.js")
var defaultSwitches = require("./lib/switches") var contains = require("./lib/util/contains.js")
var noop = require("./lib/util/noop")
var Pjax = function(options) { var Pjax = function(options) {
this.state = { this.state = {
@@ -22,7 +17,7 @@ var Pjax = function(options) {
options: null options: null
} }
var parseOptions = require("./lib/proto/parse-options.js"); var parseOptions = require("./lib/proto/parse-options.js")
parseOptions.call(this,options) parseOptions.call(this,options)
this.log("Pjax options", this.options) this.log("Pjax options", this.options)
@@ -40,7 +35,7 @@ var Pjax = function(options) {
opt.url = st.state.url opt.url = st.state.url
opt.title = st.state.title opt.title = st.state.title
opt.history = false opt.history = false
opt.requestOptions = {}; opt.requestOptions = {}
opt.scrollPos = st.state.scrollPos opt.scrollPos = st.state.scrollPos
if (st.state.uid < this.lastUid) { if (st.state.uid < this.lastUid) {
opt.backward = true opt.backward = true
@@ -56,7 +51,7 @@ var Pjax = function(options) {
}.bind(this)) }.bind(this))
} }
Pjax.switches = defaultSwitches Pjax.switches = switches
Pjax.prototype = { Pjax.prototype = {
log: require("./lib/proto/log.js"), log: require("./lib/proto/log.js"),
@@ -143,7 +138,7 @@ Pjax.prototype = {
// Abort any previous request // Abort any previous request
this.abortRequest(this.request) this.abortRequest(this.request)
trigger(document, "pjax:send", options); trigger(document, "pjax:send", options)
// Do the request // Do the request
options.requestOptions.timeout = this.options.timeout options.requestOptions.timeout = this.options.timeout
@@ -217,7 +212,7 @@ Pjax.prototype = {
// http://www.w3.org/html/wg/drafts/html/master/forms.html // http://www.w3.org/html/wg/drafts/html/master/forms.html
var autofocusEl = Array.prototype.slice.call(document.querySelectorAll("[autofocus]")).pop() var autofocusEl = Array.prototype.slice.call(document.querySelectorAll("[autofocus]")).pop()
if (autofocusEl && document.activeElement !== autofocusEl) { if (autofocusEl && document.activeElement !== autofocusEl) {
autofocusEl.focus(); autofocusEl.focus()
} }
// execute scripts when DOM have been completely updated // execute scripts when DOM have been completely updated
@@ -285,7 +280,7 @@ Pjax.prototype = {
} while (target) } while (target)
} }
} }
window.scrollTo(0, curtop); window.scrollTo(0, curtop)
} }
else if (state.options.scrollTo !== false) { else if (state.options.scrollTo !== false) {
// Scroll page to top on new page load // Scroll page to top on new page load
@@ -309,7 +304,7 @@ Pjax.prototype = {
} }
} }
Pjax.isSupported = require("./lib/is-supported.js"); Pjax.isSupported = require("./lib/is-supported.js")
// arguably could do `if( require("./lib/is-supported.js")()) {` but that might be a little to simple // arguably could do `if( require("./lib/is-supported.js")()) {` but that might be a little to simple
if (Pjax.isSupported()) { if (Pjax.isSupported()) {

View File

@@ -1,6 +1,8 @@
module.exports = function(el) { module.exports = function(el) {
// console.log("going to execute script", el)
var code = (el.text || el.textContent || el.innerHTML || "") var code = (el.text || el.textContent || el.innerHTML || "")
var src = (el.src || ""); var src = (el.src || "")
var parent = el.parentNode || document.querySelector("head") || document.documentElement var parent = el.parentNode || document.querySelector("head") || document.documentElement
var script = document.createElement("script") var script = document.createElement("script")
@@ -14,8 +16,8 @@ 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 asynchronous loading of peripheral js
} }
if (code !== "") { if (code !== "") {
@@ -29,11 +31,11 @@ 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)
} }
return true; return true
} }

View File

@@ -6,7 +6,7 @@ module.exports = function(el) {
// console.log("going to execute scripts for ", el) // console.log("going to execute scripts for ", el)
if (el.tagName.toLowerCase() === "script") { if (el.tagName.toLowerCase() === "script") {
evalScript(el); evalScript(el)
} }
forEachEls(el.querySelectorAll("script"), function(script) { forEachEls(el.querySelectorAll("script"), function(script) {
@@ -14,7 +14,7 @@ module.exports = function(el) {
if (script.parentNode) { if (script.parentNode) {
script.parentNode.removeChild(script) script.parentNode.removeChild(script)
} }
evalScript(script); evalScript(script)
} }
}); })
} }

View File

@@ -8,7 +8,7 @@ var attrClick = "data-pjax-click-state"
var formAction = function(el, event) { var formAction = function(el, event) {
// Since loadUrl modifies options and we may add our own modifications below, // Since loadUrl modifies options and we may add our own modifications below,
// clone it so the changes don't persist // clone it so the changes don't persist
var options = clone(this.options); var options = clone(this.options)
// Initialize requestOptions // Initialize requestOptions
options.requestOptions = { options.requestOptions = {
@@ -17,18 +17,18 @@ var formAction = function(el, event) {
} }
// create a testable virtual link of the form action // create a testable virtual link of the form action
var virtLinkElement = document.createElement("a"); var virtLinkElement = document.createElement("a")
virtLinkElement.setAttribute("href", options.requestOptions.requestUrl); virtLinkElement.setAttribute("href", options.requestOptions.requestUrl)
// Ignore external links. // Ignore external links.
if (virtLinkElement.protocol !== window.location.protocol || virtLinkElement.host !== window.location.host) { if (virtLinkElement.protocol !== window.location.protocol || virtLinkElement.host !== window.location.host) {
el.setAttribute(attrClick, "external"); el.setAttribute(attrClick, "external")
return return
} }
// Ignore click if we are on an anchor on the same page // Ignore click if we are on an anchor on the same page
if (virtLinkElement.pathname === window.location.pathname && virtLinkElement.hash.length > 0) { if (virtLinkElement.pathname === window.location.pathname && virtLinkElement.hash.length > 0) {
el.setAttribute(attrClick, "anchor-present"); el.setAttribute(attrClick, "anchor-present")
return return
} }
@@ -40,41 +40,39 @@ var formAction = function(el, event) {
// if declared as a full reload, just normally submit the form // if declared as a full reload, just normally submit the form
if (options.currentUrlFullReload) { if (options.currentUrlFullReload) {
el.setAttribute(attrClick, "reload"); el.setAttribute(attrClick, "reload")
return; return
} }
event.preventDefault() event.preventDefault()
var paramObject = []; var paramObject = []
for (var elementKey in el.elements) { for (var elementKey in el.elements) {
var element = el.elements[elementKey]; var element = el.elements[elementKey]
// jscs:disable disallowImplicitTypeConversion // jscs:disable disallowImplicitTypeConversion
if (!!element.name && element.attributes !== undefined && element.tagName.toLowerCase() !== "button") { if (!!element.name && element.attributes !== undefined && element.tagName.toLowerCase() !== "button") {
// jscs:enable disallowImplicitTypeConversion // jscs:enable disallowImplicitTypeConversion
if ((element.attributes.type !== "checkbox" && element.attributes.type !== "radio") || element.checked) { if ((element.attributes.type !== "checkbox" && element.attributes.type !== "radio") || element.checked) {
paramObject.push({name: encodeURIComponent(element.name), value: encodeURIComponent(element.value)}); paramObject.push({name: encodeURIComponent(element.name), value: encodeURIComponent(element.value)})
} }
} }
} }
// Creating a getString // Creating a getString
var paramsString = (paramObject.map(function(value) {return value.name + "=" + value.value;})).join("&"); var paramsString = (paramObject.map(function(value) {return value.name + "=" + value.value})).join("&")
options.requestOptions.requestPayload = paramObject; options.requestOptions.requestPayload = paramObject
options.requestOptions.requestPayloadString = paramsString; options.requestOptions.requestPayloadString = paramsString
el.setAttribute(attrClick, "submit"); el.setAttribute(attrClick, "submit")
options.triggerElement = el
options.triggerElement = el; this.loadUrl(virtLinkElement.href, options)
this.loadUrl(virtLinkElement.href, options); }
};
var isDefaultPrevented = function(event) { var isDefaultPrevented = function(event) {
return event.defaultPrevented || event.returnValue === false; return event.defaultPrevented || event.returnValue === false
}; }
module.exports = function(el) { module.exports = function(el) {
var that = this var that = this
@@ -92,7 +90,6 @@ module.exports = function(el) {
return return
} }
if (event.keyCode === 13) { if (event.keyCode === 13) {
formAction.call(that, el, event) formAction.call(that, el, event)
} }

View File

@@ -9,10 +9,10 @@ var attrKey = "data-pjax-keyup-state"
var linkAction = function(el, event) { var linkAction = function(el, event) {
// Since loadUrl modifies options and we may add our own modifications below, // Since loadUrl modifies options and we may add our own modifications below,
// clone it so the changes don't persist // clone it so the changes don't persist
var options = clone(this.options); var options = clone(this.options)
// Initialize requestOptions since loadUrl expects it to be an object // Initialize requestOptions since loadUrl expects it to be an object
options.requestOptions = {}; options.requestOptions = {}
// Dont break browser special behavior on links (like page in new window) // Dont break browser special behavior on links (like page in new window)
if (event.which > 1 || event.metaKey || event.ctrlKey || event.shiftKey || event.altKey) { if (event.which > 1 || event.metaKey || event.ctrlKey || event.shiftKey || event.altKey) {
@@ -66,7 +66,7 @@ var linkAction = function(el, event) {
} }
var isDefaultPrevented = function(event) { var isDefaultPrevented = function(event) {
return event.defaultPrevented || event.returnValue === false; return event.defaultPrevented || event.returnValue === false
} }
module.exports = function(el) { module.exports = function(el) {

View File

@@ -1,11 +1,11 @@
module.exports = function() { module.exports = function() {
if (this.options.debug && console) { if (this.options.debug && console) {
if (typeof console.log === "function") { if (typeof console.log === "function") {
console.log.apply(console, arguments); console.log.apply(console, arguments)
} }
// ie is weird // ie is weird
else if (console.log) { else if (console.log) {
console.log(arguments); console.log(arguments)
} }
} }
} }

View File

@@ -19,7 +19,7 @@ module.exports = function(options) {
ga("send", "pageview", {page: location.pathname, title: document.title}) ga("send", "pageview", {page: location.pathname, title: document.title})
} }
} }
options.scrollTo = (typeof options.scrollTo === "undefined") ? 0 : options.scrollTo; options.scrollTo = (typeof options.scrollTo === "undefined") ? 0 : options.scrollTo
options.scrollRestoration = (typeof options.scrollRestoration !== "undefined") ? options.scrollRestoration : true options.scrollRestoration = (typeof options.scrollRestoration !== "undefined") ? options.scrollRestoration : true
options.cacheBust = (typeof options.cacheBust === "undefined") ? true : options.cacheBust options.cacheBust = (typeof options.cacheBust === "undefined") ? true : options.cacheBust
options.debug = options.debug || false options.debug = options.debug || false

View File

@@ -1,7 +1,7 @@
module.exports = function(location, options, callback) { module.exports = function(location, options, callback) {
options = options || {}; options = options || {}
var requestMethod = options.requestMethod || "GET"; var requestMethod = options.requestMethod || "GET"
var requestPayload = options.requestPayloadString || null; var requestPayload = options.requestPayloadString || null
var request = new XMLHttpRequest() var request = new XMLHttpRequest()
request.onreadystatechange = function() { request.onreadystatechange = function() {
@@ -37,7 +37,7 @@ module.exports = function(location, options, callback) {
// 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")
} }
request.send(requestPayload) request.send(requestPayload)

View File

@@ -3,7 +3,7 @@ var forEachEls = require("./foreach-els")
var defaultSwitches = require("./switches") var defaultSwitches = require("./switches")
module.exports = function(switches, switchesOptions, selectors, fromEl, toEl, options) { module.exports = function(switches, switchesOptions, selectors, fromEl, toEl, options) {
var switchesQueue = []; var switchesQueue = []
selectors.forEach(function(selector) { selectors.forEach(function(selector) {
var newEls = fromEl.querySelectorAll(selector) var newEls = fromEl.querySelectorAll(selector)
@@ -12,12 +12,6 @@ module.exports = function(switches, switchesOptions, selectors, fromEl, toEl, op
this.log("Pjax switch", selector, newEls, oldEls) this.log("Pjax switch", selector, newEls, oldEls)
} }
if (newEls.length !== oldEls.length) { if (newEls.length !== oldEls.length) {
// forEachEls(newEls, function(el) {
// this.log("newEl", el, el.outerHTML)
// }, this)
// forEachEls(oldEls, function(el) {
// this.log("oldEl", el, el.outerHTML)
// }, this)
throw "DOM doesnt look the same on new loaded page: " + selector + " - new " + newEls.length + ", old " + oldEls.length throw "DOM doesnt look the same on new loaded page: " + selector + " - new " + newEls.length + ", old " + oldEls.length
} }

View File

@@ -1,7 +1,4 @@
var on = require("./events/on.js") var on = require("./events/on.js")
// var off = require("./lib/events/on.js")
// var trigger = require("./lib/events/trigger.js")
module.exports = { module.exports = {
outerHTML: function(oldEl, newEl) { outerHTML: function(oldEl, newEl) {
@@ -20,7 +17,7 @@ module.exports = {
// Copy attributes from the new element to the old one // Copy attributes from the new element to the old one
if (newEl.hasAttributes()) { if (newEl.hasAttributes()) {
var attrs = newEl.attributes; var attrs = newEl.attributes
for (var i = 0; i < attrs.length; i++) { for (var i = 0; i < attrs.length; i++) {
oldEl.attributes.setNamedItem(attrs[i].cloneNode()) oldEl.attributes.setNamedItem(attrs[i].cloneNode())
} }
@@ -34,9 +31,6 @@ module.exports = {
var elsToRemove = [] var elsToRemove = []
var elsToAdd = [] var elsToAdd = []
var fragToAppend = document.createDocumentFragment() var fragToAppend = document.createDocumentFragment()
// height transition are shitty on safari
// so commented for now (until I found something ?)
// var relevantHeight = 0
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) {
@@ -58,28 +52,16 @@ module.exports = {
elsToAdd.forEach(function(el) { elsToAdd.forEach(function(el) {
el.className = el.className.replace(el.getAttribute("data-pjax-classes"), "") el.className = el.className.replace(el.getAttribute("data-pjax-classes"), "")
el.removeAttribute("data-pjax-classes") el.removeAttribute("data-pjax-classes")
// Pjax.off(el, animationEventNames, sexyAnimationEnd, true)
}) })
elsToAdd = null // free memory elsToAdd = null // free memory
elsToRemove = null // free memory elsToRemove = null // free memory
// assume the height is now useless (avoid bug since there is overflow hidden on the parent)
// oldEl.style.height = "auto"
// this is to trigger some repaint (example: picturefill) // this is to trigger some repaint (example: picturefill)
this.onSwitch() this.onSwitch()
// Pjax.trigger(window, "scroll")
} }
}.bind(this) }.bind(this)
// Force height to be able to trigger css animation
// here we get the relevant height
// oldEl.parentNode.appendChild(newEl)
// relevantHeight = newEl.getBoundingClientRect().height
// oldEl.parentNode.removeChild(newEl)
// oldEl.style.height = oldEl.getBoundingClientRect().height + "px"
switchOptions = switchOptions || {} switchOptions = switchOptions || {}
forEach.call(oldEl.childNodes, function(el) { forEach.call(oldEl.childNodes, function(el) {
@@ -123,7 +105,5 @@ module.exports = {
// pass all className of the parent // pass all className of the parent
oldEl.className = newEl.className oldEl.className = newEl.className
oldEl.appendChild(fragToAppend) oldEl.appendChild(fragToAppend)
// oldEl.style.height = relevantHeight + "px"
} }
} }

View File

@@ -7,8 +7,7 @@ var trigger = require("../../lib/events/trigger")
var el = document.createElement("div") var el = document.createElement("div")
var el2 = document.createElement("span") var el2 = document.createElement("span")
var els = [el, el2] var els = [el, el2]
// var eventType2 = "resize"
// var eventsType = "click resize"
var classCb = function() { var classCb = function() {
this.className += "on" this.className += "on"
} }
@@ -92,14 +91,14 @@ tape("test events on/off/trigger for multiple elements, multiple events", functi
}) })
tape("test events on top level elements", function(t) { tape("test events on top level elements", function(t) {
var el = document; var el = document
el.className = "" el.className = ""
on(el, "click", classCb) on(el, "click", classCb)
trigger(el, "click") trigger(el, "click")
t.equal(el.className, "on", "attached callback has been fired properly on document") t.equal(el.className, "on", "attached callback has been fired properly on document")
el = window; el = window
el.className = "" el.className = ""
// With jsdom, the default this is global, not window, so we need to explicitly bind to window. // With jsdom, the default this is global, not window, so we need to explicitly bind to window.

View File

@@ -91,5 +91,5 @@ tape("test options are not modified by attachForm", function(t) {
t.equal(1, Object.keys(options).length, "options object that is passed in should not be modified") t.equal(1, Object.keys(options).length, "options object that is passed in should not be modified")
t.equal("bar", options.foo, "options object that is passed in should not be modified") t.equal("bar", options.foo, "options object that is passed in should not be modified")
t.end(); t.end()
}) })

View File

@@ -79,7 +79,7 @@ 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 = function() {}; var loadUrl = function() {}
attachLink.call({options: options, loadUrl: loadUrl}, a) attachLink.call({options: options, loadUrl: loadUrl}, a)
@@ -90,5 +90,5 @@ tape("test options are not modified by attachLink", function(t) {
t.equal(1, Object.keys(options).length, "options object that is passed in should not be modified") t.equal(1, Object.keys(options).length, "options object that is passed in should not be modified")
t.equal("bar", options.foo, "options object that is passed in should not be modified") t.equal("bar", options.foo, "options object that is passed in should not be modified")
t.end(); t.end()
}) })

View File

@@ -3,46 +3,46 @@ var tape = require("tape")
var parseOptions = require("../../../lib/proto/parse-options.js") var parseOptions = require("../../../lib/proto/parse-options.js")
tape("test parse initalization options function", function(t) { tape("test parse initalization options function", function(t) {
t.test("- default options", function(t) { t.test("- default options", function(t) {
var pjax = {}; var pjax = {}
parseOptions.call(pjax, {}); parseOptions.call(pjax, {})
t.equal(pjax.options.elements, "a[href], form[action]"); t.equal(pjax.options.elements, "a[href], form[action]")
t.equal(pjax.options.selectors.length, 2, "selectors length"); t.equal(pjax.options.selectors.length, 2, "selectors length")
t.equal(pjax.options.selectors[0], "title"); t.equal(pjax.options.selectors[0], "title")
t.equal(pjax.options.selectors[1], ".js-Pjax"); t.equal(pjax.options.selectors[1], ".js-Pjax")
t.equal(typeof pjax.options.switches, "object"); t.equal(typeof pjax.options.switches, "object")
t.equal(Object.keys(pjax.options.switches).length, 2);// head and body t.equal(Object.keys(pjax.options.switches).length, 2)// head and body
t.equal(typeof pjax.options.switchesOptions, "object"); t.equal(typeof pjax.options.switchesOptions, "object")
t.equal(Object.keys(pjax.options.switchesOptions).length, 0); t.equal(Object.keys(pjax.options.switchesOptions).length, 0)
t.equal(pjax.options.history, true); t.equal(pjax.options.history, true)
t.equal(typeof pjax.options.analytics, "function"); t.equal(typeof pjax.options.analytics, "function")
t.equal(pjax.options.scrollTo, 0); t.equal(pjax.options.scrollTo, 0)
t.equal(pjax.options.scrollRestoration, true); t.equal(pjax.options.scrollRestoration, true)
t.equal(pjax.options.cacheBust, true); t.equal(pjax.options.cacheBust, true)
t.equal(pjax.options.debug, false); t.equal(pjax.options.debug, false)
t.end(); t.end()
}); })
// verify analytics always ends up as a function even when passed not a function // verify analytics always ends up as a function even when passed not a function
t.test("- analytics is a function", function(t) { t.test("- analytics is a function", function(t) {
var pjax = {}; var pjax = {}
parseOptions.call(pjax, {analytics: "some string"}); parseOptions.call(pjax, {analytics: "some string"})
t.deepEqual(typeof pjax.options.analytics, "function"); t.deepEqual(typeof pjax.options.analytics, "function")
t.end(); t.end()
}); })
// verify that the value false for scrollTo is not squashed // verify that the value false for scrollTo is not squashed
t.test("- scrollTo remains false", function(t) { t.test("- scrollTo remains false", function(t) {
var pjax = {}; var pjax = {}
parseOptions.call(pjax, {scrollTo: false}); parseOptions.call(pjax, {scrollTo: false})
t.deepEqual(pjax.options.scrollTo, false); t.deepEqual(pjax.options.scrollTo, false)
t.end(); t.end()
}); })
t.end() t.end()
}) })

View File

@@ -22,7 +22,7 @@ tape("test xhr request", function(t) {
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")
try { try {
@@ -40,7 +40,7 @@ tape("test xhr request", function(t) {
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")
t.end() t.end()