Skip to content
This repository was archived by the owner on May 26, 2025. It is now read-only.

Commit a72880d

Browse files
Code cleanup (#120)
* Lots of code cleanup * Cleanup parse-options tests - Rename objects for clarity and inline unneeded objects - Remove unneeded tests - Use Object.keys().length instead of a custom function - Use typeof === "object" instead of a custom function that checks the prototype tree as well, since we don't expect anything but an object literal. * Remove old switchFallback code * Remove polyfill for Function.prototype.bind * Inline small functions * Add more documentation and tests for options.currentUrlFullReload Closes #17 * Update package.json
1 parent 57aed82 commit a72880d

26 files changed

+275
-358
lines changed

README.md

Lines changed: 133 additions & 107 deletions
Large diffs are not rendered by default.

index.js

Lines changed: 26 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,14 @@
11
var clone = require("./lib/clone.js")
22
var executeScripts = require("./lib/execute-scripts.js")
3-
43
var forEachEls = require("./lib/foreach-els.js")
5-
4+
var switches = require("./lib/switches")
65
var newUid = require("./lib/uniqueid.js")
76

8-
var noop = require("./lib/util/noop")
9-
var contains = require("./lib/util/contains.js")
10-
117
var on = require("./lib/events/on.js")
12-
// var off = require("./lib/events/on.js")
138
var trigger = require("./lib/events/trigger.js")
149

15-
var defaultSwitches = require("./lib/switches")
16-
10+
var contains = require("./lib/util/contains.js")
11+
var noop = require("./lib/util/noop")
1712

1813
var Pjax = function(options) {
1914
this.state = {
@@ -22,8 +17,8 @@ var Pjax = function(options) {
2217
options: null
2318
}
2419

25-
var parseOptions = require("./lib/proto/parse-options.js");
26-
parseOptions.apply(this,[options])
20+
var parseOptions = require("./lib/proto/parse-options.js")
21+
parseOptions.call(this,options)
2722
this.log("Pjax options", this.options)
2823

2924
if (this.options.scrollRestoration && "scrollRestoration" in history) {
@@ -40,7 +35,7 @@ var Pjax = function(options) {
4035
opt.url = st.state.url
4136
opt.title = st.state.title
4237
opt.history = false
43-
opt.requestOptions = {};
38+
opt.requestOptions = {}
4439
opt.scrollPos = st.state.scrollPos
4540
if (st.state.uid < this.lastUid) {
4641
opt.backward = true
@@ -56,18 +51,27 @@ var Pjax = function(options) {
5651
}.bind(this))
5752
}
5853

59-
Pjax.switches = defaultSwitches
54+
Pjax.switches = switches
6055

6156
Pjax.prototype = {
6257
log: require("./lib/proto/log.js"),
6358

64-
getElements: require("./lib/proto/get-elements.js"),
59+
getElements: function(el) {
60+
return el.querySelectorAll(this.options.elements)
61+
},
6562

66-
parseDOM: require("./lib/proto/parse-dom.js"),
63+
parseDOM: function(el) {
64+
var parseElement = require("./lib/proto/parse-element")
65+
forEachEls(this.getElements(el), parseElement, this)
66+
},
6767

68-
refresh: require("./lib/proto/refresh.js"),
68+
refresh: function(el) {
69+
this.parseDOM(el || document)
70+
},
6971

70-
reload: require("./lib/reload.js"),
72+
reload: function() {
73+
window.location.reload()
74+
},
7175

7276
attachLink: require("./lib/proto/attach-link.js"),
7377

@@ -81,15 +85,6 @@ Pjax.prototype = {
8185
return require("./lib/switches-selectors.js").bind(this)(this.options.switches, this.options.switchesOptions, selectors, fromEl, toEl, options)
8286
},
8387

84-
// too much problem with the code below
85-
// + it’s too dangerous
86-
// switchFallback: function(fromEl, toEl) {
87-
// this.switchSelectors(["head", "body"], fromEl, toEl)
88-
// // execute script when DOM is like it should be
89-
// Pjax.executeScripts(document.querySelector("head"))
90-
// Pjax.executeScripts(document.querySelector("body"))
91-
// }
92-
9388
latestChance: function(href) {
9489
window.location = href
9590
},
@@ -139,16 +134,7 @@ Pjax.prototype = {
139134
} catch (e) { }
140135
}
141136

142-
// try {
143137
this.switchSelectors(this.options.selectors, tmpEl, document, options)
144-
145-
// }
146-
// catch(e) {
147-
// if (this.options.debug) {
148-
// this.log("Pjax switch fail: ", e)
149-
// }
150-
// this.switchFallback(tmpEl, document)
151-
// }
152138
},
153139

154140
abortRequest: require("./lib/abort-request.js"),
@@ -161,14 +147,14 @@ Pjax.prototype = {
161147
// Abort any previous request
162148
this.abortRequest(this.request)
163149

164-
trigger(document, "pjax:send", options);
150+
trigger(document, "pjax:send", options)
165151

166152
// Do the request
167153
options.requestOptions.timeout = this.options.timeout
168154
this.request = this.doRequest(href, options.requestOptions, function(html, request) {
169155
// Fail if unable to load HTML via AJAX
170156
if (html === false) {
171-
trigger(document,"pjax:complete pjax:error", options)
157+
trigger(document, "pjax:complete pjax:error", options)
172158

173159
return
174160
}
@@ -218,8 +204,7 @@ Pjax.prototype = {
218204
if (console && console.error) {
219205
console.error("Pjax switch fail: ", e)
220206
}
221-
this.latestChance(href)
222-
return
207+
return this.latestChance(href)
223208
}
224209
else {
225210
throw e
@@ -236,7 +221,7 @@ Pjax.prototype = {
236221
// http://www.w3.org/html/wg/drafts/html/master/forms.html
237222
var autofocusEl = Array.prototype.slice.call(document.querySelectorAll("[autofocus]")).pop()
238223
if (autofocusEl && document.activeElement !== autofocusEl) {
239-
autofocusEl.focus();
224+
autofocusEl.focus()
240225
}
241226

242227
// execute scripts when DOM have been completely updated
@@ -304,7 +289,7 @@ Pjax.prototype = {
304289
} while (target)
305290
}
306291
}
307-
window.scrollTo(0, curtop);
292+
window.scrollTo(0, curtop)
308293
}
309294
else if (state.options.scrollTo !== false) {
310295
// Scroll page to top on new page load
@@ -328,7 +313,7 @@ Pjax.prototype = {
328313
}
329314
}
330315

331-
Pjax.isSupported = require("./lib/is-supported.js");
316+
Pjax.isSupported = require("./lib/is-supported.js")
332317

333318
// arguably could do `if( require("./lib/is-supported.js")()) {` but that might be a little to simple
334319
if (Pjax.isSupported()) {

lib/clone.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
module.exports = function(obj) {
2-
if (null === obj || "object" != typeof obj) {
2+
if (null === obj || "object" !== typeof obj) {
33
return obj
44
}
55
var copy = obj.constructor()

lib/eval-script.js

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
module.exports = function(el) {
22
var code = (el.text || el.textContent || el.innerHTML || "")
3-
var src = (el.src || "");
3+
var src = (el.src || "")
44
var parent = el.parentNode || document.querySelector("head") || document.documentElement
55
var script = document.createElement("script")
66

@@ -13,12 +13,12 @@ module.exports = function(el) {
1313

1414
script.type = "text/javascript"
1515

16-
if (src != "") {
17-
script.src = src;
18-
script.async = false; // force synchronous loading of peripheral js
16+
if (src !== "") {
17+
script.src = src
18+
script.async = false // force synchronous loading of peripheral JS
1919
}
2020

21-
if (code != "") {
21+
if (code !== "") {
2222
try {
2323
script.appendChild(document.createTextNode(code))
2424
}
@@ -29,11 +29,11 @@ module.exports = function(el) {
2929
}
3030

3131
// execute
32-
parent.appendChild(script);
32+
parent.appendChild(script)
3333
// avoid pollution only in head or body tags
34-
if (["head","body"].indexOf(parent.tagName.toLowerCase()) > 0) {
34+
if (["head", "body"].indexOf(parent.tagName.toLowerCase()) > 0) {
3535
parent.removeChild(script)
3636
}
3737

38-
return true;
38+
return true
3939
}

lib/events/trigger.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ module.exports = function(els, events, opts) {
44
events = (typeof events === "string" ? events.split(" ") : events)
55

66
events.forEach(function(e) {
7-
var event // = new CustomEvent(e) // doesn't everywhere yet
7+
var event
88
event = document.createEvent("HTMLEvents")
99
event.initEvent(e, true, true)
1010
event.eventName = e
@@ -17,8 +17,8 @@ module.exports = function(els, events, opts) {
1717
forEachEls(els, function(el) {
1818
var domFix = false
1919
if (!el.parentNode && el !== document && el !== window) {
20-
// THANKS YOU IE (9/10//11 concerned)
21-
// dispatchEvent doesn't work if element is not in the dom
20+
// THANK YOU IE (9/10/11)
21+
// dispatchEvent doesn't work if the element is not in the DOM
2222
domFix = true
2323
document.body.appendChild(el)
2424
}

lib/execute-scripts.js

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,16 @@ var evalScript = require("./eval-script")
33
// Finds and executes scripts (used for newly added elements)
44
// Needed since innerHTML does not run scripts
55
module.exports = function(el) {
6-
// console.log("going to execute scripts for ", el)
7-
86
if (el.tagName.toLowerCase() === "script") {
9-
evalScript(el);
7+
evalScript(el)
108
}
119

1210
forEachEls(el.querySelectorAll("script"), function(script) {
1311
if (!script.type || script.type.toLowerCase() === "text/javascript") {
1412
if (script.parentNode) {
1513
script.parentNode.removeChild(script)
1614
}
17-
evalScript(script);
15+
evalScript(script)
1816
}
19-
});
17+
})
2018
}

lib/foreach-els.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,6 @@ module.exports = function(els, fn, context) {
44
if (els instanceof HTMLCollection || els instanceof NodeList || els instanceof Array) {
55
return Array.prototype.forEach.call(els, fn, context)
66
}
7-
// assume simple dom element
7+
// assume simple DOM element
88
return fn.call(context, els)
99
}

lib/polyfills/Function.prototype.bind.js

Lines changed: 0 additions & 20 deletions
This file was deleted.

lib/proto/attach-form.js

Lines changed: 21 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
require("../polyfills/Function.prototype.bind")
2-
31
var on = require("../events/on")
42
var clone = require("../clone")
53

@@ -8,27 +6,27 @@ var attrClick = "data-pjax-click-state"
86
var formAction = function(el, event) {
97
// Since loadUrl modifies options and we may add our own modifications below,
108
// clone it so the changes don't persist
11-
var options = clone(this.options);
9+
var options = clone(this.options)
1210

1311
// Initialize requestOptions
1412
options.requestOptions = {
1513
requestUrl: el.getAttribute("action") || window.location.href,
16-
requestMethod: el.getAttribute("method") || "GET",
14+
requestMethod: el.getAttribute("method") || "GET"
1715
}
1816

1917
// create a testable virtual link of the form action
20-
var virtLinkElement = document.createElement("a");
21-
virtLinkElement.setAttribute("href", options.requestOptions.requestUrl);
18+
var virtLinkElement = document.createElement("a")
19+
virtLinkElement.setAttribute("href", options.requestOptions.requestUrl)
2220

2321
// Ignore external links.
2422
if (virtLinkElement.protocol !== window.location.protocol || virtLinkElement.host !== window.location.host) {
25-
el.setAttribute(attrClick, "external");
23+
el.setAttribute(attrClick, "external")
2624
return
2725
}
2826

2927
// Ignore click if we are on an anchor on the same page
3028
if (virtLinkElement.pathname === window.location.pathname && virtLinkElement.hash.length > 0) {
31-
el.setAttribute(attrClick, "anchor-present");
29+
el.setAttribute(attrClick, "anchor-present")
3230
return
3331
}
3432

@@ -40,41 +38,39 @@ var formAction = function(el, event) {
4038

4139
// if declared as a full reload, just normally submit the form
4240
if (options.currentUrlFullReload) {
43-
el.setAttribute(attrClick, "reload");
44-
return;
41+
el.setAttribute(attrClick, "reload")
42+
return
4543
}
4644

4745
event.preventDefault()
4846

49-
var paramObject = [];
47+
var paramObject = []
5048
for (var elementKey in el.elements) {
51-
var element = el.elements[elementKey];
49+
var element = el.elements[elementKey]
5250
// jscs:disable disallowImplicitTypeConversion
5351
if (!!element.name && element.attributes !== undefined && element.tagName.toLowerCase() !== "button") {
5452
// jscs:enable disallowImplicitTypeConversion
5553
if ((element.attributes.type !== "checkbox" && element.attributes.type !== "radio") || element.checked) {
56-
paramObject.push({name: encodeURIComponent(element.name), value: encodeURIComponent(element.value)});
54+
paramObject.push({name: encodeURIComponent(element.name), value: encodeURIComponent(element.value)})
5755
}
5856
}
5957
}
6058

6159
// Creating a getString
62-
var paramsString = (paramObject.map(function(value) {return value.name + "=" + value.value;})).join("&");
60+
var paramsString = (paramObject.map(function(value) {return value.name + "=" + value.value})).join("&")
6361

64-
options.requestOptions.requestPayload = paramObject;
65-
options.requestOptions.requestPayloadString = paramsString;
62+
options.requestOptions.requestPayload = paramObject
63+
options.requestOptions.requestPayloadString = paramsString
6664

67-
el.setAttribute(attrClick, "submit");
65+
el.setAttribute(attrClick, "submit")
6866

69-
70-
options.triggerElement = el;
71-
this.loadUrl(virtLinkElement.href, options);
72-
};
67+
options.triggerElement = el
68+
this.loadUrl(virtLinkElement.href, options)
69+
}
7370

7471
var isDefaultPrevented = function(event) {
75-
return event.defaultPrevented || event.returnValue === false;
76-
};
77-
72+
return event.defaultPrevented || event.returnValue === false
73+
}
7874

7975
module.exports = function(el) {
8076
var that = this
@@ -92,8 +88,7 @@ module.exports = function(el) {
9288
return
9389
}
9490

95-
96-
if (event.keyCode == 13) {
91+
if (event.keyCode === 13) {
9792
formAction.call(that, el, event)
9893
}
9994
}.bind(this))

0 commit comments

Comments
 (0)