Compare commits
40 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
917c6f6bcb | ||
|
|
b201b96a37 | ||
|
|
eff7cfd452 | ||
|
|
7976f06043 | ||
|
|
ee530f4c0a | ||
|
|
bf71894395 | ||
|
|
beaa21fb3a | ||
|
|
b96b0f41a7 | ||
|
|
6e113b2d06 | ||
|
|
46912e6797 | ||
|
|
4877bac2ae | ||
|
|
afe0ddb6b9 | ||
|
|
a5d36d28f8 | ||
|
|
7a4056fd77 | ||
|
|
ba6ef126c0 | ||
|
|
01536bfbf5 | ||
|
|
722ddf2a30 | ||
|
|
34fc00c89d | ||
|
|
c02193c61b | ||
|
|
824b229158 | ||
|
|
e3d0f8cc1b | ||
|
|
8ea8ffad07 | ||
|
|
c12e4cdedd | ||
|
|
546b7e309a | ||
|
|
2f4bd760a5 | ||
|
|
477d967804 | ||
|
|
e882b8639a | ||
|
|
3d25bee131 | ||
|
|
47059bdb04 | ||
|
|
791400ed20 | ||
|
|
97c8b2d749 | ||
|
|
b156a4f389 | ||
|
|
1d292a1a6e | ||
|
|
aaa2631eb7 | ||
|
|
460dea8a9e | ||
|
|
b908621842 | ||
|
|
b20ee2261e | ||
|
|
ad6292fffb | ||
|
|
4ed4577539 | ||
|
|
6eafcf8dc6 |
1
.gitignore
vendored
1
.gitignore
vendored
@@ -1,3 +1,4 @@
|
|||||||
.DS_Store
|
.DS_Store
|
||||||
node_modules/
|
node_modules/
|
||||||
tests/scripts/index.html
|
tests/scripts/index.html
|
||||||
|
pjax.js
|
||||||
|
|||||||
33
CHANGELOG.md
33
CHANGELOG.md
@@ -1,6 +1,35 @@
|
|||||||
# unreleased
|
# 0.2.4 - 2016-06-28
|
||||||
|
|
||||||
-
|
- Fixed: ``refresh`` should now work (use `this.parseDOM` for refresh)
|
||||||
|
([#67](https://github.com/MoOx/pjax/pull/67) - @compressed)
|
||||||
|
- Fixed: Some attributes, such as `itemscope` have no corresponding value.
|
||||||
|
This change allows them to still be set.
|
||||||
|
([#67](https://github.com/MoOx/pjax/pull/67) - @compressed)
|
||||||
|
- Added: ``cacheBust`` option
|
||||||
|
([#71](https://github.com/MoOx/pjax/pull/71) - @tremby)
|
||||||
|
|
||||||
|
# 0.2.3 - 2016-03-24
|
||||||
|
|
||||||
|
- Fixed: ``currentUrlFullReload`` option now works
|
||||||
|
- Fixed: ``this.reload`` is now a Function
|
||||||
|
([#65](https://github.com/MoOx/pjax/issues/65))
|
||||||
|
|
||||||
|
# 0.2.2 - 2016-03-12
|
||||||
|
|
||||||
|
- Fixed: added back standalone version in `./pjax.js`
|
||||||
|
([#57](https://github.com/MoOx/pjax/issues/57)
|
||||||
|
- Fixed: error when using pjax with google analytics (``options`` was undefined)
|
||||||
|
([#59](https://github.com/MoOx/pjax/pull/59))
|
||||||
|
- Fixed: HierarchyRequestError error
|
||||||
|
([#49](https://github.com/MoOx/pjax/pull/49))
|
||||||
|
- Fixed: ``TypeError: Pjax.forEachEls is not a function``
|
||||||
|
([#52](https://github.com/MoOx/pjax/pull/52))
|
||||||
|
- Fixed: ``TypeError: Pjax.executeScripts is not a function``
|
||||||
|
([#52](https://github.com/MoOx/pjax/pull/52))
|
||||||
|
- Fixed: ``TypeError: Pjax.clone is not a function``
|
||||||
|
([#52](https://github.com/MoOx/pjax/pull/52))
|
||||||
|
- Added: Ignore events with prevented defaults
|
||||||
|
([#50](https://github.com/MoOx/pjax/pull/50))
|
||||||
|
|
||||||
# 0.2.1 - 2015-02-04
|
# 0.2.1 - 2015-02-04
|
||||||
|
|
||||||
|
|||||||
56
README.md
56
README.md
@@ -1,7 +1,6 @@
|
|||||||
# Pjax [](https://travis-ci.org/MoOx/pjax)
|
# Pjax
|
||||||
|
|
||||||
|
[](https://travis-ci.org/MoOx/pjax) [@todo fix CI](https://github.com/MoOx/pjax/issues/63).
|
||||||
[](https://ci.testling.com/MoOx/pjax)
|
|
||||||
|
|
||||||
> Easily enable fast Ajax navigation on any website (using pushState + xhr)
|
> Easily enable fast Ajax navigation on any website (using pushState + xhr)
|
||||||
|
|
||||||
@@ -16,9 +15,6 @@ Especially for user that have low bandwidth connection._
|
|||||||
|
|
||||||
**No more full page reload. No more lots of HTTP request.**
|
**No more full page reload. No more lots of HTTP request.**
|
||||||
|
|
||||||
# Note: current master is WIP.
|
|
||||||
|
|
||||||
Checkout [v0.1.4](https://github.com/MoOx/pjax/tree/a17a6b90bebefd8f5209e6a6f7d8c5d59296232a) for latest "stable" (no tests so kind of funny to call that stable).
|
|
||||||
|
|
||||||
## Demo
|
## Demo
|
||||||
|
|
||||||
@@ -44,7 +40,7 @@ want (including html metas, title, navigation state).
|
|||||||
### Under the hood
|
### Under the hood
|
||||||
|
|
||||||
- It listen to every clicks on links _you want_ (by default all of them),
|
- It listen to every clicks on links _you want_ (by default all of them),
|
||||||
- When a internal link hitted, Pjax grabs HTML from your server via ajax,
|
- When an internal link is clicked, Pjax grabs HTML from your server via ajax,
|
||||||
- Pjax render pages DOM tree (without loading any resources - images, css, js...)
|
- Pjax render pages DOM tree (without loading any resources - images, css, js...)
|
||||||
- It check if all defined parts can be replaced:
|
- It check if all defined parts can be replaced:
|
||||||
- if page doesn't suit requirement, classic navigation used,
|
- if page doesn't suit requirement, classic navigation used,
|
||||||
@@ -277,6 +273,7 @@ new Pjax({
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
})
|
})
|
||||||
```
|
```
|
||||||
_Note that remove class include `Animated--reverse` which is a simple way to not have
|
_Note that remove class include `Animated--reverse` which is a simple way to not have
|
||||||
@@ -385,6 +382,12 @@ It's called every time a page is switched, even for history buttons.
|
|||||||
|
|
||||||
Value (in px) to scrollTo when a page is switched.
|
Value (in px) to scrollTo when a page is switched.
|
||||||
|
|
||||||
|
##### `cacheBust` (Boolean, default true)
|
||||||
|
|
||||||
|
When set to true,
|
||||||
|
append a timestamp query string segment to the requested URLs
|
||||||
|
in order to skip browser cache.
|
||||||
|
|
||||||
##### `debug` (Boolean, default to false)
|
##### `debug` (Boolean, default to false)
|
||||||
|
|
||||||
Enable verbose mode & doesn't use fallback when there is an error.
|
Enable verbose mode & doesn't use fallback when there is an error.
|
||||||
@@ -394,25 +397,6 @@ Useful to debug page layout differences.
|
|||||||
|
|
||||||
When set to true, clicking on a link that point the current url trigger a full page reload.
|
When set to true, clicking on a link that point the current url trigger a full page reload.
|
||||||
|
|
||||||
#### Extend Pjax
|
|
||||||
|
|
||||||
Pjax prototype & utilities methods can be used & changed so you can patch or hack
|
|
||||||
Pjax behavior, as you wish.
|
|
||||||
|
|
||||||
Here is a summary of functions:
|
|
||||||
|
|
||||||
- `Pjax.prototype.log` (`function()`): console.log function that is enable/disabled by `debug` option
|
|
||||||
- `Pjax.prototype.getElements` (`function(el)`): retrieve elements to attach Pjax behavior
|
|
||||||
- `Pjax.prototype.parseDOM` (`function(el)`): parse DOM to attach behavior using `Pjax.prototype.getElements` & `Pjax.prototype.attachLink`
|
|
||||||
- `Pjax.prototype.attachLink` (`function(el)`): attach Pjax behavior to a link
|
|
||||||
- `Pjax.prototype.forEachSelectors` (`function(cb, context, DOMcontext)`): call a function for each selectors defined
|
|
||||||
- `Pjax.prototype.switchSelectors` (`function(selectors, fromEl, toEl, options)`): loop on selectors to switch elements
|
|
||||||
- `Pjax.prototype.latestChance` (`function(href)`): when everything is fucked up, it's our only hope (just call `window.location = href`)
|
|
||||||
- `Pjax.prototype.onSwitch` (`function()`): callback triggered when elements are switched, for now it's just trigger a window resize event (lots of lib are listening to this event to draw stuff)
|
|
||||||
- `Pjax.prototype.loadContent` (`function(html, options)`): switch elements for each selectors
|
|
||||||
- `Pjax.prototype.doRequest` (`function(location, callback)`): make the ajax request to grab page from the server
|
|
||||||
- `Pjax.prototype.loadUrl` (`function(href, options)`): do the ajax request, handle html results & eventually handle browser history, analytics & scroll.
|
|
||||||
|
|
||||||
### Events
|
### Events
|
||||||
|
|
||||||
Pjax fires a number of events regardless of how its invoked.
|
Pjax fires a number of events regardless of how its invoked.
|
||||||
@@ -537,15 +521,19 @@ wrapper on each page (to avoid differences of DOM between pages)
|
|||||||
|
|
||||||
---
|
---
|
||||||
|
|
||||||
## Contributing
|
## Examples
|
||||||
|
|
||||||
Work on a branch, install dev-dependencies, respect coding style & run tests before submitting a bug fix or a feature.
|
Clone this repository and run `npm run example`, then open `http://localhost:3000/example` in your browser.
|
||||||
|
|
||||||
$ git clone https://github.com/MoOx/pjax.git
|
---
|
||||||
$ git checkout -b patch-1
|
|
||||||
$ npm install
|
|
||||||
$ npm test
|
|
||||||
|
|
||||||
## [Changelog](CHANGELOG.md)
|
## CONTRIBUTING
|
||||||
|
|
||||||
|
* ⇄ Pull requests and ★ Stars are always welcome.
|
||||||
|
* For bugs and feature requests, please create an issue.
|
||||||
|
* Pull requests must be accompanied by passing automated tests (`$ npm test`).
|
||||||
|
|
||||||
|
## [CHANGELOG](CHANGELOG.md)
|
||||||
|
|
||||||
|
## [LICENSE](LICENSE)
|
||||||
|
|
||||||
## [License](LICENSE)
|
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "pjax",
|
"name": "pjax",
|
||||||
"version": "0.1.4",
|
"version": "0.2.4",
|
||||||
"description": "Easily enable fast Ajax navigation on any website (using pushState + xhr)",
|
"description": "Easily enable fast Ajax navigation on any website (using pushState + xhr)",
|
||||||
"keywords": [
|
"keywords": [
|
||||||
"pjax",
|
"pjax",
|
||||||
@@ -11,7 +11,7 @@
|
|||||||
"transition",
|
"transition",
|
||||||
"animation"
|
"animation"
|
||||||
],
|
],
|
||||||
"main": "src/pjax.js",
|
"main": "pjax.js",
|
||||||
"homepage": "https://github.com/MoOx/pjax",
|
"homepage": "https://github.com/MoOx/pjax",
|
||||||
"authors": [
|
"authors": [
|
||||||
"Maxime Thirouin"
|
"Maxime Thirouin"
|
||||||
|
|||||||
26
example/example.js
Normal file
26
example/example.js
Normal file
@@ -0,0 +1,26 @@
|
|||||||
|
/* global Pjax */
|
||||||
|
console.log("Document initialized:", window.location.href)
|
||||||
|
|
||||||
|
document.addEventListener("pjax:send", function() {
|
||||||
|
console.log("Event: pjax:send", arguments)
|
||||||
|
})
|
||||||
|
|
||||||
|
document.addEventListener("pjax:complete", function() {
|
||||||
|
console.log("Event: pjax:complete", arguments)
|
||||||
|
})
|
||||||
|
|
||||||
|
document.addEventListener("pjax:error", function() {
|
||||||
|
console.log("Event: pjax:error", arguments)
|
||||||
|
})
|
||||||
|
|
||||||
|
document.addEventListener("pjax:success", function() {
|
||||||
|
console.log("Event: pjax:success", arguments)
|
||||||
|
})
|
||||||
|
|
||||||
|
document.addEventListener("DOMContentLoaded", function() {
|
||||||
|
var pjax = new Pjax({
|
||||||
|
selectors: [".body"],
|
||||||
|
// currentUrlFullReload: true,
|
||||||
|
})
|
||||||
|
console.log("Pjax initialized.", pjax)
|
||||||
|
})
|
||||||
17
example/index.html
Normal file
17
example/index.html
Normal file
@@ -0,0 +1,17 @@
|
|||||||
|
<!doctype html>
|
||||||
|
<html>
|
||||||
|
<head>
|
||||||
|
<meta charset='utf-8'>
|
||||||
|
<title>Hello</title>
|
||||||
|
<script src='../pjax.js'></script>
|
||||||
|
<script src='example.js'></script>
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
<div class='body'>
|
||||||
|
<h1>Index</h1>
|
||||||
|
Hello.
|
||||||
|
Go to <a href='page2.html'>Page 2</a> and view your console to see Pjax events.
|
||||||
|
Clicking on <a href='index.html'>this page</a> will just reload the page entierly.
|
||||||
|
</div>
|
||||||
|
</body>
|
||||||
|
</html>
|
||||||
15
example/page2.html
Normal file
15
example/page2.html
Normal file
@@ -0,0 +1,15 @@
|
|||||||
|
<!doctype html>
|
||||||
|
<html>
|
||||||
|
<head>
|
||||||
|
<meta charset='utf-8'>
|
||||||
|
<title>Hello</title>
|
||||||
|
<script src='../pjax.js'></script>
|
||||||
|
<script src='example.js'></script>
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
<div class='body'>
|
||||||
|
<h1>Page 2</h1>
|
||||||
|
Hello. Go to <a href='index.html'>Index</a>.
|
||||||
|
</div>
|
||||||
|
</body>
|
||||||
|
</html>
|
||||||
24
index.js
24
index.js
@@ -1,3 +1,7 @@
|
|||||||
|
var clone = require('./lib/clone.js')
|
||||||
|
var executeScripts = require('./lib/execute-scripts.js')
|
||||||
|
|
||||||
|
var forEachEls = require("./lib/foreach-els.js")
|
||||||
|
|
||||||
var newUid = require("./lib/uniqueid.js")
|
var newUid = require("./lib/uniqueid.js")
|
||||||
|
|
||||||
@@ -5,6 +9,7 @@ var on = require("./lib/events/on.js")
|
|||||||
// var off = 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 Pjax = function(options) {
|
var Pjax = function(options) {
|
||||||
this.firstrun = true
|
this.firstrun = true
|
||||||
|
|
||||||
@@ -18,7 +23,7 @@ var Pjax = function(options) {
|
|||||||
|
|
||||||
on(window, "popstate", function(st) {
|
on(window, "popstate", function(st) {
|
||||||
if (st.state) {
|
if (st.state) {
|
||||||
var opt = Pjax.clone(this.options)
|
var opt = clone(this.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
|
||||||
@@ -46,14 +51,16 @@ Pjax.prototype = {
|
|||||||
|
|
||||||
refresh: require("./lib/proto/refresh.js"),
|
refresh: require("./lib/proto/refresh.js"),
|
||||||
|
|
||||||
|
reload: require("./lib/reload.js"),
|
||||||
|
|
||||||
attachLink: require("./lib/proto/attach-link.js"),
|
attachLink: require("./lib/proto/attach-link.js"),
|
||||||
|
|
||||||
forEachSelectors: function(cb, context, DOMcontext) {
|
forEachSelectors: function(cb, context, DOMcontext) {
|
||||||
return require("./lib/foreach-selectors.js")(this.options.selectors, cb, context, DOMcontext)
|
return require("./lib/foreach-selectors.js").bind(this)(this.options.selectors, cb, context, DOMcontext)
|
||||||
},
|
},
|
||||||
|
|
||||||
switchSelectors: function(selectors, fromEl, toEl, options) {
|
switchSelectors: function(selectors, fromEl, toEl, options) {
|
||||||
return require("./lib/switches-selectors.js")(this.options.switches, this.options.switchesOptions, selectors, fromEl, toEl, options)
|
return require("./lib/switches-selectors.js").bind(this)(this.options.switches, this.options.switchesOptions, selectors, fromEl, toEl, options)
|
||||||
},
|
},
|
||||||
|
|
||||||
// too much problem with the code below
|
// too much problem with the code below
|
||||||
@@ -87,7 +94,12 @@ Pjax.prototype = {
|
|||||||
matches.shift()
|
matches.shift()
|
||||||
matches.forEach(function(htmlAttrib) {
|
matches.forEach(function(htmlAttrib) {
|
||||||
var attr = htmlAttrib.trim().split("=")
|
var attr = htmlAttrib.trim().split("=")
|
||||||
tmpEl.documentElement.setAttribute(attr[0], attr[1].slice(1, -1))
|
if (attr.length === 1) {
|
||||||
|
tmpEl.documentElement.setAttribute(attr[0], true)
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
tmpEl.documentElement.setAttribute(attr[0], attr[1].slice(1, -1))
|
||||||
|
}
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -118,8 +130,8 @@ Pjax.prototype = {
|
|||||||
|
|
||||||
// execute scripts when DOM have been completely updated
|
// execute scripts when DOM have been completely updated
|
||||||
this.options.selectors.forEach(function(selector) {
|
this.options.selectors.forEach(function(selector) {
|
||||||
Pjax.forEachEls(document.querySelectorAll(selector), function(el) {
|
forEachEls(document.querySelectorAll(selector), function(el) {
|
||||||
Pjax.executeScripts(el)
|
executeScripts(el)
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
// }
|
// }
|
||||||
|
|||||||
@@ -16,7 +16,7 @@ module.exports = function(els, events, opts) {
|
|||||||
|
|
||||||
forEachEls(els, function(el) {
|
forEachEls(els, function(el) {
|
||||||
var domFix = false
|
var domFix = false
|
||||||
if (!el.parentNode) {
|
if (!el.parentNode && el !== document && el !== window) {
|
||||||
// THANKS YOU IE (9/10//11 concerned)
|
// THANKS YOU IE (9/10//11 concerned)
|
||||||
// dispatchEvent doesn't work if element is not in the dom
|
// dispatchEvent doesn't work if element is not in the dom
|
||||||
domFix = true
|
domFix = true
|
||||||
|
|||||||
@@ -43,7 +43,10 @@ var linkAction = function(el, event) {
|
|||||||
event.preventDefault()
|
event.preventDefault()
|
||||||
|
|
||||||
// don’t do "nothing" if user try to reload the page by clicking the same link twice
|
// don’t do "nothing" if user try to reload the page by clicking the same link twice
|
||||||
if (el.href === window.location.href.split("#")[0]) {
|
if (
|
||||||
|
this.options.currentUrlFullReload &&
|
||||||
|
el.href === window.location.href.split("#")[0]
|
||||||
|
) {
|
||||||
el.setAttribute(attrClick, "reload")
|
el.setAttribute(attrClick, "reload")
|
||||||
this.reload()
|
this.reload()
|
||||||
return
|
return
|
||||||
@@ -53,14 +56,26 @@ var linkAction = function(el, event) {
|
|||||||
this.loadUrl(el.href, clone(this.options))
|
this.loadUrl(el.href, clone(this.options))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var isDefaultPrevented = function(event) {
|
||||||
|
return event.defaultPrevented || event.returnValue === false;
|
||||||
|
}
|
||||||
|
|
||||||
module.exports = function(el) {
|
module.exports = function(el) {
|
||||||
var that = this
|
var that = this
|
||||||
|
|
||||||
on(el, "click", function(event) {
|
on(el, "click", function(event) {
|
||||||
|
if (isDefaultPrevented(event)) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
linkAction.call(that, el, event)
|
linkAction.call(that, el, event)
|
||||||
})
|
})
|
||||||
|
|
||||||
on(el, "keyup", function(event) {
|
on(el, "keyup", function(event) {
|
||||||
|
if (isDefaultPrevented(event)) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
// Don’t break browser special behavior on links (like page in new window)
|
// Don’t 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) {
|
||||||
el.setAttribute(attrKey, "modifier")
|
el.setAttribute(attrKey, "modifier")
|
||||||
|
|||||||
@@ -7,7 +7,7 @@ module.exports = function(options){
|
|||||||
this.options.switches = this.options.switches || {}
|
this.options.switches = this.options.switches || {}
|
||||||
this.options.switchesOptions = this.options.switchesOptions || {}
|
this.options.switchesOptions = this.options.switchesOptions || {}
|
||||||
this.options.history = this.options.history || true
|
this.options.history = this.options.history || true
|
||||||
this.options.analytics = this.options.analytics || function(options) {
|
this.options.analytics = this.options.analytics || function() {
|
||||||
// options.backward or options.foward can be true or undefined
|
// options.backward or options.foward can be true or undefined
|
||||||
// by default, we do track back/foward hit
|
// by default, we do track back/foward hit
|
||||||
// https://productforums.google.com/forum/#!topic/analytics/WVwMDjLhXYk
|
// https://productforums.google.com/forum/#!topic/analytics/WVwMDjLhXYk
|
||||||
@@ -15,10 +15,11 @@ module.exports = function(options){
|
|||||||
_gaq.push(["_trackPageview"])
|
_gaq.push(["_trackPageview"])
|
||||||
}
|
}
|
||||||
if (window.ga) {
|
if (window.ga) {
|
||||||
ga("send", "pageview", {page: options.url, title: options.title})
|
ga("send", "pageview", {page: location.pathname, title: document.title})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
this.options.scrollTo = (typeof this.options.scrollTo === 'undefined') ? 0 : this.options.scrollTo;
|
this.options.scrollTo = (typeof this.options.scrollTo === 'undefined') ? 0 : this.options.scrollTo;
|
||||||
|
this.options.cacheBust = (typeof this.options.cacheBust === 'undefined') ? true : this.options.cacheBust
|
||||||
this.options.debug = this.options.debug || false
|
this.options.debug = this.options.debug || false
|
||||||
|
|
||||||
// we can’t replace body.outerHTML or head.outerHTML
|
// we can’t replace body.outerHTML or head.outerHTML
|
||||||
|
|||||||
@@ -1,6 +1,3 @@
|
|||||||
|
|
||||||
var parseDom = require("./parse-dom")
|
|
||||||
|
|
||||||
module.exports = function(el) {
|
module.exports = function(el) {
|
||||||
parseDom(el || document)
|
this.parseDOM(el || document)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -12,7 +12,12 @@ module.exports = function(location, callback) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
request.open("GET", location + (!/[?&]/.test(location) ? "?" : "&") + (new Date().getTime()), true)
|
// Add a timestamp as part of the query string if cache busting is enabled
|
||||||
|
if (this.options.cacheBust) {
|
||||||
|
location += (!/[?&]/.test(location) ? "?" : "&") + new Date().getTime()
|
||||||
|
}
|
||||||
|
|
||||||
|
request.open("GET", location, true)
|
||||||
request.setRequestHeader("X-Requested-With", "XMLHttpRequest")
|
request.setRequestHeader("X-Requested-With", "XMLHttpRequest")
|
||||||
request.send(null)
|
request.send(null)
|
||||||
return request
|
return request
|
||||||
|
|||||||
37
package.json
37
package.json
@@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "pjax",
|
"name": "pjax",
|
||||||
"version": "0.2.1",
|
"version": "0.2.4",
|
||||||
"description": "Easily enable fast Ajax navigation on any website (using pushState + xhr)",
|
"description": "Easily enable fast Ajax navigation on any website (using pushState + xhr)",
|
||||||
"keywords": [
|
"keywords": [
|
||||||
"pjax",
|
"pjax",
|
||||||
@@ -11,43 +11,48 @@
|
|||||||
"transition",
|
"transition",
|
||||||
"animation"
|
"animation"
|
||||||
],
|
],
|
||||||
"repository": {
|
"repository": "https://github.com/MoOx/pjax.git",
|
||||||
"type": "git",
|
|
||||||
"url": "https://github.com/MoOx/pjax.git"
|
|
||||||
},
|
|
||||||
"homepage": "https://github.com/MoOx/pjax",
|
|
||||||
"bugs": {
|
|
||||||
"url": "https://github.com/MoOx/pjax/issues"
|
|
||||||
},
|
|
||||||
"author": "Maxime Thirouin",
|
"author": "Maxime Thirouin",
|
||||||
"license": "MIT",
|
"license": "MIT",
|
||||||
"main": "src/pjax.js",
|
"main": "src/pjax.js",
|
||||||
"files": [
|
"files": [
|
||||||
"CHANGELOG.md",
|
|
||||||
"LICENSE",
|
|
||||||
"index.js",
|
"index.js",
|
||||||
"lib"
|
"lib",
|
||||||
|
"pjax.js"
|
||||||
],
|
],
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"browserify": "^3.46.0",
|
"browserify": "^3.46.0",
|
||||||
"coverify": "^1.0.6",
|
"coverify": "^1.0.6",
|
||||||
"jscs": "^1.6.2",
|
"jscs": "^1.6.2",
|
||||||
"jshint": "^2.5.6",
|
"jshint": "^2.5.6",
|
||||||
|
"npmpub": "^3.1.0",
|
||||||
|
"opn-cli": "^3.1.0",
|
||||||
|
"serve": "1.4.0",
|
||||||
"tape": "^3.0.0",
|
"tape": "^3.0.0",
|
||||||
"testling": "^1.6.1"
|
"testling": "^1.6.1"
|
||||||
},
|
},
|
||||||
"scripts": {
|
"scripts": {
|
||||||
"lint": "jscs **/*.js && jshint . --exclude-path .gitignore",
|
"lint": "jscs **/*.js && jshint . --exclude-path .gitignore",
|
||||||
"test": "npm run lint && testling",
|
"standalone": "browserify index.js --standalone Pjax > pjax.js",
|
||||||
|
"tests": "testling",
|
||||||
|
"test": "npm run lint && npm run standalone && npm run tests",
|
||||||
"test--html": "testling --html > tests/scripts/index.html",
|
"test--html": "testling --html > tests/scripts/index.html",
|
||||||
"coverage": "browserify -t coverify tests/**/*.js | testling | coverify"
|
"coverage": "browserify -t coverify tests/**/*.js | testling | coverify",
|
||||||
|
"example": "opn http://localhost:3000/example/; serve .",
|
||||||
|
"prepublish": "npm run standalone",
|
||||||
|
"#release": "testling does not work in a process launch by npm... :facepalm:",
|
||||||
|
"release": "echo \"npmpub --skip-test --dry && npm test && npmpub --skip-test --skip-cleanup\""
|
||||||
},
|
},
|
||||||
"testling": {
|
"testling": {
|
||||||
"files": "tests/**/*.js",
|
"files": "tests/**/*.js",
|
||||||
"browsers": [
|
"browsers": [
|
||||||
"ie/10..latest",
|
"ie/10..latest",
|
||||||
"firefox/4.0", "firefox/latest", "firefox/nightly",
|
"firefox/4.0",
|
||||||
"chrome/10", "chrome/latest", "chrome/canary",
|
"firefox/latest",
|
||||||
|
"firefox/nightly",
|
||||||
|
"chrome/10",
|
||||||
|
"chrome/latest",
|
||||||
|
"chrome/canary",
|
||||||
"opera/12..latest",
|
"opera/12..latest",
|
||||||
"opera/next",
|
"opera/next",
|
||||||
"safari/5.1..latest",
|
"safari/5.1..latest",
|
||||||
|
|||||||
@@ -90,3 +90,21 @@ tape("test events on/off/trigger for multiple elements, multiple events", functi
|
|||||||
|
|
||||||
t.end()
|
t.end()
|
||||||
})
|
})
|
||||||
|
|
||||||
|
tape("test events on top level elements", function(t) {
|
||||||
|
var el = document;
|
||||||
|
|
||||||
|
el.className = ""
|
||||||
|
on(el, "click", classCb)
|
||||||
|
trigger(el, "click")
|
||||||
|
t.equal(el.className, "on", "attached callback has been fired properly on document")
|
||||||
|
|
||||||
|
el = window;
|
||||||
|
|
||||||
|
el.className = ""
|
||||||
|
on(el, "click", classCb)
|
||||||
|
trigger(el, "click")
|
||||||
|
t.equal(el.className, "on", "attached callback has been fired properly on window")
|
||||||
|
|
||||||
|
t.end()
|
||||||
|
})
|
||||||
|
|||||||
@@ -56,3 +56,22 @@ tape("test attach link prototype method", function(t) {
|
|||||||
|
|
||||||
t.end()
|
t.end()
|
||||||
})
|
})
|
||||||
|
|
||||||
|
tape("test attach link preventDefaulted events", function(t) {
|
||||||
|
var callbacked = false
|
||||||
|
var a = document.createElement("a")
|
||||||
|
|
||||||
|
attachLink.call({
|
||||||
|
options: {},
|
||||||
|
loadUrl: function() {
|
||||||
|
callbacked = true
|
||||||
|
}
|
||||||
|
}, a)
|
||||||
|
|
||||||
|
a.href = "#"
|
||||||
|
on(a, "click", preventDefault)
|
||||||
|
trigger(a, "click")
|
||||||
|
t.equal(callbacked, false, "events that are preventDefaulted should not fire callback")
|
||||||
|
|
||||||
|
t.end()
|
||||||
|
})
|
||||||
|
|||||||
@@ -46,6 +46,7 @@ tape("test parse initalization options function", function(t) {
|
|||||||
t.deepEqual(typeof body_1.options.analytics,"function");
|
t.deepEqual(typeof body_1.options.analytics,"function");
|
||||||
|
|
||||||
t.deepEqual(body_1.options.scrollTo,0);
|
t.deepEqual(body_1.options.scrollTo,0);
|
||||||
|
t.deepEqual(body_1.options.cacheBust,true);
|
||||||
t.deepEqual(body_1.options.debug,false);
|
t.deepEqual(body_1.options.debug,false);
|
||||||
t.end();
|
t.end();
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -2,15 +2,47 @@ var tape = require("tape")
|
|||||||
|
|
||||||
var request = require("../../lib/request.js")
|
var request = require("../../lib/request.js")
|
||||||
|
|
||||||
|
// Polyfill responseURL property into XMLHttpRequest if it doesn't exist,
|
||||||
|
// just for the purposes of this test
|
||||||
|
// This polyfill is not complete; it won't show the updated location if a
|
||||||
|
// redirection occurred, but it's fine for our purposes.
|
||||||
|
if (!('responseURL' in XMLHttpRequest.prototype)) {
|
||||||
|
var nativeOpen = XMLHttpRequest.prototype.open
|
||||||
|
XMLHttpRequest.prototype.open = function (method, url) {
|
||||||
|
this.responseURL = url
|
||||||
|
return nativeOpen.apply(this, arguments)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
tape("test xhr request", function(t) {
|
tape("test xhr request", function(t) {
|
||||||
request("https://api.github.com/", function(result) {
|
t.test("- request is made, gets a result, and is cache-busted", function(t) {
|
||||||
try {
|
var requestCacheBust = request.bind({
|
||||||
result = JSON.parse(result)
|
options: {
|
||||||
}
|
cacheBust: true,
|
||||||
catch (e) {
|
},
|
||||||
t.fail("xhr doesn't get a JSON response")
|
});
|
||||||
}
|
var r = requestCacheBust("https://api.github.com/", function(result) {
|
||||||
t.same(typeof result, "object", "xhr request get a result")
|
t.equal(r.responseURL.indexOf("?"), 23, "XHR URL is cache-busted when configured to be")
|
||||||
t.end()
|
try {
|
||||||
|
result = JSON.parse(result)
|
||||||
|
}
|
||||||
|
catch (e) {
|
||||||
|
t.fail("xhr doesn't get a JSON response")
|
||||||
|
}
|
||||||
|
t.same(typeof result, "object", "xhr request get a result")
|
||||||
|
t.end()
|
||||||
|
})
|
||||||
})
|
})
|
||||||
|
t.test("- request is not cache-busted when configured not to be", function(t) {
|
||||||
|
var requestNoCacheBust = request.bind({
|
||||||
|
options: {
|
||||||
|
cacheBust: false,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
var r = requestNoCacheBust("https://api.github.com/", function() {
|
||||||
|
t.equal(r.responseURL, "https://api.github.com/", "XHR URL is left untouched")
|
||||||
|
t.end()
|
||||||
|
})
|
||||||
|
})
|
||||||
|
t.end()
|
||||||
})
|
})
|
||||||
|
|||||||
@@ -1,9 +1,41 @@
|
|||||||
var tape = require("tape")
|
var tape = require("tape")
|
||||||
|
|
||||||
// var switchesSelectors = require("../../lib/switches-selectors.js")
|
var switchesSelectors = require("../../lib/switches-selectors.js")
|
||||||
|
|
||||||
|
// @author darylteo
|
||||||
tape("test switchesSelectors", function(t) {
|
tape("test switchesSelectors", function(t) {
|
||||||
t.fail()
|
// switchesSelectors relies on a higher level function callback
|
||||||
|
// should really be passed in instead so I'll leave it here as a TODO:
|
||||||
|
var pjax = {
|
||||||
|
onSwitch: function() {
|
||||||
|
console.log('Switched')
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
var tmpEl = document.implementation.createHTMLDocument()
|
||||||
|
|
||||||
|
// a div container is used because swapping the containers
|
||||||
|
// will generate a new element, so things get weird
|
||||||
|
// using "body" generates a lot of testling cruft that I don't
|
||||||
|
// want so let's avoid that
|
||||||
|
var container = document.createElement("div")
|
||||||
|
container.innerHTML = "<p>Original Text</p><span>No Change</span>"
|
||||||
|
document.body.appendChild(container)
|
||||||
|
|
||||||
|
var container2 = tmpEl.createElement("div")
|
||||||
|
container2.innerHTML = "<p>New Text</p><span>New Span</span>"
|
||||||
|
tmpEl.body.appendChild(container2)
|
||||||
|
|
||||||
|
switchesSelectors.bind(pjax)(
|
||||||
|
{}, // switches
|
||||||
|
{}, // switchesOptions
|
||||||
|
['p'], //selectors,
|
||||||
|
tmpEl, // fromEl
|
||||||
|
document, // toEl,
|
||||||
|
{} // options
|
||||||
|
)
|
||||||
|
|
||||||
|
t.equals(container.innerHTML, '<p>New Text</p><span>No Change</span>', 'Elements correctly switched')
|
||||||
|
|
||||||
t.end()
|
t.end()
|
||||||
})
|
})
|
||||||
|
|||||||
Reference in New Issue
Block a user