Compare commits

...

107 Commits
0.2.0 ... 0.2.5

Author SHA1 Message Date
BehindTheMath
2d210c3305 0.2.5 (#121)
* Bump version to 0.2.5

* Update CHANGELOG
2018-02-02 11:12:27 -05:00
BehindTheMath
a72880d205 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
2018-02-02 09:52:44 -05:00
BehindTheMath
57aed828ac Merge pull request #118 from MoOx/fix/external-scripts-events
Fix external scripts events
2018-01-31 16:46:28 -05:00
Robin North
63bc2af226 Remove redundant comment 2018-01-29 20:06:33 +00:00
Robin North
20e987929e Merge pull request #116 from MoOx/fix/blur
Fix element blurring (removing focus). Closes #4.
2018-01-29 20:03:24 +00:00
Behind The Math
90d26d641c Remove focus from form elements as well. 2018-01-29 13:51:00 -05:00
BehindTheMath
93c0a5bb9b Merge pull request #119 from MoOx/feature/scroll-restoration
Opt in to manual scroll restoration in browsers that support it
2018-01-29 09:27:50 -05:00
Robin North
8718da35c6 Opt in to manual scroll restoration in browsers that support it 2018-01-28 14:35:25 +00:00
Robin North
31e1e7e405 Enable test for document.write in eval'd scripts 2018-01-26 15:54:07 +00:00
Robin North
05163d9fb5 Stop dispatching extraneous pjax:complete events, correct typo 2018-01-26 15:53:07 +00:00
Robin North
a15c94c55a Merge pull request #113 from MoOx/cleanup/analytics
Cleanup default analytics function
2018-01-25 07:50:59 +00:00
Behind The Math
2d4df39f72 Remove a redundant call to .blur() 2018-01-24 20:19:02 -05:00
Behind The Math
fa05e94f07 Only blur element if it's contained by one of the selectors
Previously, Pjax would blur (remove focus) from the active element
regardless of where it was on the page. This restricts that to
happen only if the active element is contained by one of the
elements represented by options.selectors, because only those are
affected by Pjax.

Fixes #4
2018-01-24 20:18:41 -05:00
BehindTheMath
e3df2b1c3d Merge pull request #114 from MoOx/fix/abort-pending-xhr
Abort previous pending XHR when navigating
2018-01-24 18:54:33 -05:00
Robin North
01fb72ceeb Simplify options assignment 2018-01-24 23:40:05 +00:00
Robin North
f642eec047 Preserve ability to disable analytics behavior, explicitly document this option 2018-01-24 23:38:34 +00:00
Robin North
cc384b9b16 Improve check for analytics function option 2018-01-24 23:35:45 +00:00
Robin North
f559ca2914 Cleanup default analytics function 2018-01-24 23:35:45 +00:00
Robin North
137322543c Explicitly flag if aborting a request fails due to request completing 2018-01-24 23:20:36 +00:00
Robin North
f7f68b2e50 Fix module filename 2018-01-24 23:15:40 +00:00
Robin North
ff62289683 Make use of new noop module 2018-01-24 14:51:16 +00:00
BehindTheMath
3205596f3e Merge pull request #115 from MoOx/feature/minify
Add a minified version of Pjax to NPM
2018-01-24 08:48:23 -05:00
Behind The Math
3c8cd087c5 Use double quotes in CLI arguments
Single quotes don't work in all environments.
2018-01-23 19:40:13 -05:00
Behind The Math
12f3be21fb Exclude pjax.min.js fom JSCS 2018-01-23 19:35:18 -05:00
Behind The Math
b74fbc4178 Add a minified version of Pjax to NPM
Fixes #108.
2018-01-23 17:53:10 -05:00
BehindTheMath
8fcef04c9e Merge pull request #112 from timtrinidad/master
Use a local clone of options when initiating requests, so requestOptions don't persist
2018-01-23 15:44:26 -05:00
Tim Trinidad
feb85382f2 Revert back to using '**' glob for tests, wrap in quotes to force node to parse the args 2018-01-23 15:05:31 -05:00
Tim Trinidad
f196604d73 Add tests to ensure options are not accidentally modified 2018-01-23 13:22:48 -05:00
Tim Trinidad
526a0883a2 Clone options in attach-link to prevent requestOptions changes from persisting 2018-01-23 13:22:31 -05:00
Robin North
486ef0e0ba Abort previous pending XHR when navigating 2018-01-23 15:25:56 +00:00
Behind The Math
6000ad5620 Restore the trigger for the resize and scroll events to onSwitch 2018-01-22 20:42:57 -05:00
BehindTheMath
9845244948 Merge pull request #111 from MoOx/fix/scroll
Enhance scrolling behavior

- Save scroll position with history
- Scroll to element position when URL contains a hash
- Add scrollRestoration option
2018-01-22 20:32:57 -05:00
Behind The Math
e7935d9c74 Add scrollRestoration option 2018-01-22 18:56:22 -05:00
Tim Trinidad
e4b3952589 Clone options before modifying it for form submissions 2018-01-22 18:32:28 -05:00
Behind The Math
546b9abba3 Small bug fix 2018-01-22 17:32:59 -05:00
Behind The Math
bc2432b18c Scroll to element position when URL contains a hash
When the URL contains a hash, try to find the corresponding
element, and if found, scroll to its position.

Based on darylteo/pjax@4893a2a657

Fixes #22.
2018-01-22 11:45:42 -05:00
Behind The Math
37d303ed66 Save scroll position with history
Save scroll position when navigating away from a page, and restore
it when navigating back to that page.

Fixes #30.
2018-01-22 11:27:13 -05:00
BehindTheMath
b5c2120d08 Fix async switches (#110)
If any switches are async, the subsequent code will execute before the switches are finished. This PR moves all that code to a new function, and debounces the calls to onSwitch() so it only executes
once, after all the switches finish.

Fizes #72.
2018-01-22 10:55:29 -05:00
Robin North
ca61c4a840 Fix incorrect main field in npm package (#105)
Point npm package `main` field at module source
2018-01-18 20:55:27 -05:00
Robin North
cd09cc88d1 Replace instances of ES6 const keyword with var 2018-01-18 16:00:24 -05:00
Robin North
b98e3ef914 Fix DOMException in switchElementsAlt()
Clone attribute nodes before setting on `oldEl` to prevent the following error:

```
DOMException: Failed to execute 'setNamedItem' on 'NamedNodeMap': The node provided is an attribute node that is already an attribute of another Element; attribute nodes must be explicitly cloned.
```
2018-01-17 17:07:02 -05:00
Behind The Math
36ed7079b1 Fix tooling for the example
Apparently we do need opn-cli.
2018-01-15 16:36:29 -05:00
BehindTheMath
6fa51e58f8 Add checks for XHR redirects (#101)
Fixes #7

This checks for redirects by looking for the following, in sequence:
- XMLHttpRequest.responseURL
- the X-PJAX-URL header (like jquery-pjax)
- the X-XHR-Redirected-To header (like Turbolinks)
2018-01-10 15:45:55 -05:00
Maxime Thirouin
cb9c37fcb3 Use npmpub like it should since testling is gone 2018-01-09 16:02:09 -05:00
BehindTheMath
92d5e09494 Update tooling (#99)
- Update browserify, serve, and tape to the latest versions
- update the example script to reflect serve's new CLI options
- Remove the uneeded dependency on opn-cli
- Force Travis to use NPM 5.x
- Remove bower.json
2018-01-09 15:59:15 -05:00
Behind The Math
3bd101bb1d Update JSCS to the latest version 2018-01-09 14:09:40 -05:00
Behind The Math
c0d64e41b8 Fix linting errors 2018-01-09 14:09:40 -05:00
Behind The Math
e586440964 Update JSCS includes and excludes 2018-01-09 14:09:40 -05:00
BehindTheMath
a2e6cfc0af Fix tests (#96)
Closes #63

- Switch from testling to jsdom for browser APIs
- Switch from coverify to nyc for coverage reports
- Clean up related dead code and tooling
- Update Travis to use Node v6 and v8, since we need ES6 features for jsdom.
2018-01-08 17:21:18 -05:00
BehindTheMath
6491e32437 Add an option to set a timeout for XHR requests (#95)
Closes #24.
2018-01-07 23:56:11 -05:00
Behind The Math
d3d5ef7a11 Pass the element that triggered Pjax to the pjax:send event
Closes #62.
2017-12-21 13:06:49 -05:00
BehindTheMath
0916c74171 Merge pull request #93 from BehindTheMath/bugfix/add-switchElementsAlt
Add switchElementsAlt() to the default switches

Fixes #83.
2017-12-20 15:58:29 -05:00
Behind The Math
0781f820ee Add switchElementsAlt() to the default switches
Fixes #83.
2017-12-19 15:56:48 -05:00
Maxime Thirouin
a7b584c469 Add direct download link
Ref #57
2017-12-19 14:11:55 +01:00
Oskar
be5d58d550 Asynchronous switch functions (Make sure the DOM is parsed after switching) (#79)
* Make sure the DOM is parsed after switching

* Fix reload-link in the example
2017-12-19 13:58:22 +01:00
Bradley B Smith
1e40a0d70b Send the X-PJAX header with request (#80)
Make it more compatible with the jQuery PJAX and lets the server choose to optimize the response. Sending the list of selectors would be nice, too.
2017-12-19 13:56:30 +01:00
BehindTheMath
75eddfcab6 Add default switches to Pjax.switches (#92)
Fixes #68 and reverts #74
2017-12-19 13:49:26 +01:00
Maxime Thirouin
352e7114b6 Bye codesponsor 😢 2017-12-04 14:53:01 +01:00
markusfluer
af57adaafb Fixed #77 Fails on Internet Explorer 2017-11-02 13:02:17 +01:00
markusfluer
09f14fc86c Added evaluation of remote script tags 2017-11-02 12:51:36 +01:00
markusfluer
b17457f5a2 Fixed tests for request.js 2017-09-18 14:23:44 +02:00
markusfluer
86e5a2281a Added support do do a push-state ajax request with forms 2017-09-18 14:13:45 +02:00
Maxime Thirouin
109e78347f Update README.md 2017-09-14 21:45:39 +02:00
Markus Flür
cb3b6b8a5d Merge pull request #81 from CPTechnikVX/patch-1
Fix bug on IE11 preventing from ajax page refresh
2017-07-20 15:34:50 +02:00
CPTechnikVX
6dffeba21a Fix bug on IE11 preventing from ajax page refresh
It used to be fixed a long time ago, but was perhaps merged away...
2016-11-28 15:39:14 +01:00
Dale
acdd87ef3d README: Modified switches sidebySide example so it doesn't result in a undefined Type Error. (#74) 2016-09-27 07:07:28 +02:00
Maxime Thirouin
917c6f6bcb 0.2.4 2016-07-11 07:11:39 +02:00
Bart Nagel
b201b96a37 Polyfill XHR.responseURL so tests pass in old Safari (#73)
This property is only used in the tests. It doesn't exist in Safari 8
and before, so where it's not available this patch polyfills it,
allowing the test to continue.
2016-07-11 06:55:47 +02:00
Maxime Thirouin
eff7cfd452 Merge pull request #67 from compressed/refresh
Update refresh and handle attributes without values
2016-06-28 07:17:43 +02:00
Bart Nagel
7976f06043 Added: cacheBust option (#71)
* Refactor request test to allow further tests to be added

* Make cache-busting optional

Closes #70
2016-06-28 06:46:14 +02:00
Maxime Thirouin
ee530f4c0a 0.2.3 (bower) 2016-05-03 09:23:50 +02:00
Christopher Brickley
bf71894395 fix(attributes): for attributes without a value, add only the attribute
Some attributes, such as `itemscope` have no corresponding value. This
change allows them to still be set.
2016-04-19 12:15:39 -04:00
Christopher Brickley
beaa21fb3a fix(refresh): use this.parseDOM for refresh
Fix for issue mentioned in #36
2016-04-19 12:13:14 -04:00
Maxime Thirouin
b96b0f41a7 Merge pull request #66 from christophwolff/patch-1
Fix bracket in switches example
2016-04-05 17:20:49 +02:00
Christoph Wolff
6e113b2d06 Fix bracket in switches example 2016-04-05 16:40:52 +02:00
Maxime Thirouin
46912e6797 More logical npm publish (testling don't like to be spawn or something) 2016-03-24 13:34:11 +01:00
Maxime Thirouin
4877bac2ae 0.2.3 2016-03-24 12:39:07 +01:00
Maxime Thirouin
afe0ddb6b9 Fixed: `this.reload` is now a Function
Closes #65
2016-03-24 12:38:15 +01:00
Maxime Thirouin
a5d36d28f8 Update README.md 2016-03-15 11:36:53 +01:00
Maxime Thirouin
7a4056fd77 Update README.md 2016-03-15 11:35:13 +01:00
Maxime Thirouin
ba6ef126c0 Internal: add "npm run release" command
—skip-test is used because of tesling (it does not like to be spawned
or something like that)…
2016-03-12 07:52:52 +01:00
Maxime Thirouin
01536bfbf5 0.2.2
Closes #55
2016-03-12 07:42:37 +01:00
Maxime Thirouin
722ddf2a30 Internal: simple repository url 2016-03-12 07:27:47 +01:00
Maxime Thirouin
34fc00c89d Add standalone file back, no big deal
Closes #57
2016-03-12 07:27:06 +01:00
Maxime Thirouin
c02193c61b Internal: update CHANGELOG #59 2016-03-12 07:26:26 +01:00
Maxime Thirouin
824b229158 Merge pull request #59 from coolhihi/master
Fixed: error when using pjax with google analytics
2016-03-12 07:09:55 +01:00
COoL
e3d0f8cc1b fixed an error when using pjax with google analytics 2016-01-15 17:32:07 +08:00
Maxime Thirouin
8ea8ffad07 Update CHANGELOG 2016-01-05 07:42:00 +01:00
Maxime Thirouin
c12e4cdedd Merge pull request #52 from darylteo/patch-51
Patch 51
2016-01-05 07:15:11 +01:00
Maxime Thirouin
546b7e309a Merge pull request #50 from darylteo/patch-5
Patch 5
2016-01-05 07:14:29 +01:00
Maxime Thirouin
2f4bd760a5 Merge pull request #49 from darylteo/patch-39
[FIX] #39 - events on top level
2016-01-05 07:14:17 +01:00
darylteo
477d967804 Add clone and executeScripts as well 2016-01-05 15:02:39 +11:00
darylteo
e882b8639a Use required forEachElse 2016-01-05 15:02:00 +11:00
darylteo
3d25bee131 Bind required functions to pjax 2016-01-05 14:22:03 +11:00
darylteo
47059bdb04 Add test for switch selectors 2016-01-05 14:12:52 +11:00
darylteo
791400ed20 Tests for #5 2016-01-04 23:45:25 +11:00
darylteo
97c8b2d749 [NEW] #5 Ignore default prevented clicks 2016-01-04 23:19:36 +11:00
darylteo
b156a4f389 [TEST] Test for #39 2016-01-04 22:52:30 +11:00
darylteo
1d292a1a6e [FIX] #39 - events on top level
Events triggered on top level elements (such as window or document)
lead to a HierarchyRequestError due to a fix in IE browsers
where dispatchEvent does not fire if an element is not in the DOM.

The current test is simply to check for the existence of parentNode
However, this means top level elements get added to itself, causing
the error.

The proposed fix simply tests for top level elements in the test.
2016-01-04 22:30:00 +11:00
Maxime Thirouin
aaa2631eb7 Merge pull request #42 from MohamedBoualleg/patch-1
Update README.md
2015-12-18 08:25:55 +01:00
Maxime Thirouin
460dea8a9e Merge pull request #44 from rstacruz/fix/stuff
Add examples, remove failing test
2015-11-14 12:28:26 +01:00
Rico Sta. Cruz
b908621842 Fix lint errors 2015-11-13 23:59:22 +11:00
Rico Sta. Cruz
b20ee2261e Add example 2015-11-13 10:40:59 +11:00
Rico Sta. Cruz
ad6292fffb Add prepublish hook 2015-11-13 10:28:16 +11:00
Mohamed Bouallegue
4ed4577539 Update README.md 2015-10-11 04:06:04 +01:00
Maxime Thirouin
6eafcf8dc6 Add a standalone build to ensure it "should" work 2015-02-19 07:51:42 +01:00
Maxime Thirouin
b244a8cac4 Add files into npm release 2015-02-04 08:41:15 +01:00
47 changed files with 1222 additions and 13061 deletions

3
.gitignore vendored
View File

@@ -1,3 +1,6 @@
.DS_Store .DS_Store
node_modules/ node_modules/
tests/scripts/index.html tests/scripts/index.html
pjax.js
.nyc_output/
pjax.min.js

View File

@@ -1,6 +1,8 @@
{ {
"excludeFiles": [ "excludeFiles": [
"node_modules/**" "node_modules/**",
"pjax.js",
"pjax.min.js"
], ],
"fileExtensions": [ "fileExtensions": [
".js" ".js"
@@ -121,7 +123,7 @@
"requireCapitalizedConstructors": true, "requireCapitalizedConstructors": true,
"safeContextKeyword": "that", "safeContextKeyword": "that",
"requireDotNotation": true, "requireDotNotation": true,
"validateJSDoc": { "jsDoc": {
"checkParamNames": true, "checkParamNames": true,
"checkRedundantParams": true, "checkRedundantParams": true,
"requireParamTypes": true "requireParamTypes": true

View File

@@ -1,7 +1,7 @@
language: "node_js" language: "node_js"
before_script: node_js:
# testling use headless browser - "6"
# on travis-ci, firefox is the default one - "8"
# & it needs a display to works # Force Travis to use npm v5
- export DISPLAY=:99.0 # https://github.com/travis-ci/travis-ci/issues/4653#issuecomment-194051953
- sh -e /etc/init.d/xvfb start before_install: if [[ `npm -v` != 5* ]]; then npm i -g npm@5; fi

View File

@@ -1,6 +1,75 @@
# unreleased # 0.2.5 - 2018-02-02
- - Fixed: Async switch functions now work correctly, because the DOM is now parsed after all the switches finish.
([#79](https://github.com/MoOx/pjax/pull/79), [#110](https://github.com/MoOx/pjax/pull/110) - @oskarrough, @BehindTheMath, @robinnorth)
- Fixed: Bug on IE11 preventing AJAX page refresh.
([#81](https://github.com/MoOx/pjax/pull/81) - @CPTechnikVX)
- Fixed: Default switches are now available as `Pjax.switches`.
([#92](https://github.com/MoOx/pjax/pull/92) - @BehindTheMath)
- Fixed: An error that was caused by a missing `switchElementsAlt`.
([#93](https://github.com/MoOx/pjax/pull/93), [#104](https://github.com/MoOx/pjax/pull/104) - @BehindTheMath, @robinnorth)
- Fixed: Incorrect `main` field in npm package
([#105](https://github.com/MoOx/pjax/pull/105) - @robinnorth)
- Fixed: A pending XHR is now aborted if the user navigates somewhere else before the request is finished.
([#114](https://github.com/MoOx/pjax/pull/114) - @robinnorth)
- Fixed: When rendering new content, focus will now be removed only from elements within one of the containers manipulated by Pjax.
([#116](https://github.com/MoOx/pjax/pull/116) - @BehindTheMath)
- Fixed: Stop dispatching extraneous `pjax:complete` events when external scripts load
([#118](https://github.com/MoOx/pjax/pull/118) - @robinnorth)
- Added: Send the `X-PJAX` header with XHR requests.
([#80](https://github.com/MoOx/pjax/pull/80) - @bram1028)
- Added: Direct download link for script tags. (@MoOx)
- Added: Pass the element that triggered Pjax to the `pjax:send` event.
([#94](https://github.com/MoOx/pjax/pull/94) - @BehindTheMath)
- Added: An option to set a timeout for XHR requests.
([#95](https://github.com/MoOx/pjax/pull/95) - @BehindTheMath)
- Added: Checks for XHR redirects
([#101](https://github.com/MoOx/pjax/pull/101) - @BehindTheMath)
- Added: Save scroll position with history, and restore when navigating backwards or forwards.
([#110](https://github.com/MoOx/pjax/pull/110), [#119](https://github.com/MoOx/pjax/pull/119) - @BehindTheMath, @robinnorth)
- Added: Scroll to element position when URL contains a hash
([#110](https://github.com/MoOx/pjax/pull/110) - @BehindTheMath)
- Added: Minified version of the Pjax bundle.
([#115](https://github.com/MoOx/pjax/pull/115) - @BehindTheMath)
- Changed: Miscellaneous code and tests cleanup.
([#96](https://github.com/MoOx/pjax/pull/96), [#98](https://github.com/MoOx/pjax/pull/98), [#99](https://github.com/MoOx/pjax/pull/99), [#100](https://github.com/MoOx/pjax/pull/1070), [#107](https://github.com/MoOx/pjax/pull/107), [#113](https://github.com/MoOx/pjax/pull/113), [#120](https://github.com/MoOx/pjax/pull/120) - @BehindTheMath, @MoOx, @robinnorth)
# 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
- Fixed: it's better when a release have actual files.
# 0.2.0 - 2015-02-04 # 0.2.0 - 2015-02-04

322
README.md
View File

@@ -1,66 +1,83 @@
# Pjax [![Build Status](http://img.shields.io/travis/MoOx/pjax.svg)](https://travis-ci.org/MoOx/pjax) # Pjax
[![Build Status](http://img.shields.io/travis/MoOx/pjax.svg)](https://travis-ci.org/MoOx/pjax).
[![browser support](https://ci.testling.com/MoOx/pjax.png)](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)
Pjax is ~~a jQuery plugin~~ **a standalone JavaScript module** that uses Pjax is ~~a jQuery plugin~~ **a standalone JavaScript module** that uses
ajax (XmlHttpRequest) and AJAX (XmlHttpRequest) and
[pushState()](https://developer.mozilla.org/en-US/docs/Web/Guide/API/DOM/Manipulating_the_browser_history) [pushState()](https://developer.mozilla.org/en-US/docs/Web/Guide/API/DOM/Manipulating_the_browser_history)
to deliver a fast browsing experience. to deliver a fast browsing experience.
_It allow you to completely transform user experience of standard websites _It allows you to completely transform the user experience of standard websites
(server side generated or static ones) to make them feel they browse an app. (server-side generated or static ones) to make them feel like they are browsing an app,
Especially for user that have low bandwidth connection._ especially for users with low bandwidth connection._
**No more full page reload. No more lots of HTTP request.** **No more full page reloads. No more multiple HTTP requests.**
# 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
[You can see this running on my website](http://moox.io), with sexy CSS animations when switching pages. [You can see this running on my website](http://moox.io), with sexy CSS animations when switching pages.
## Installation
- You can install Pjax from **npm**:
```shell
npm install pjax
```
- You can also link directly to the [bundle](https://cdn.jsdelivr.net/npm/pjax/pjax.js):
```html
<script src="https://cdn.jsdelivr.net/npm/pjax@VERSION/pjax.js"></script>
```
Or the [minified bundle](https://cdn.jsdelivr.net/npm/pjax/pjax.min.js):
```html
<script src="https://cdn.jsdelivr.net/npm/pjax@VERSION/pjax.min.js"></script>
```
## No dependencies
_Pjax does not rely on other libraries, like jQuery or similar. It is written entirely in vanilla JS._
## How Pjax works ## How Pjax works
Pjax loads page using ajax & updates the browser's current url using pushState without reloading your page's layout or any resources (js, css), giving a fast page load. Pjax loads pages using AJAX and updates the browser's current URL using `pushState()` without reloading your page's layout or any resources (JS, CSS), giving a fast page load.
_But under the hood, it's just ONE http request with a pushState() call._
Obviously, for [browsers that don't support pushState()](http://caniuse.com/#search=pushstate) Pjax fully degrades (yeah, it doesn't do anything at all).
It simply works with all permalinks & can update all parts of the page you _But under the hood, it's just ONE HTTP request with a `pushState()` call._
want (including html metas, title, navigation state).
- It's not limited to one container, like jQuery-Pjax is, Obviously, for [browsers that don't support `history.pushState()`](http://caniuse.com/#search=pushstate) Pjax gracefully degrades and does not do anything at all.
- It fully support browser history (back & forward buttons),
- It **will** support keyboard browsing (@todo), It simply works with all permalinks and can update all parts of the page you
- Automatically fallback to classic navigation for externals pages (thanks to Capitain Obvious help), want (including HTML metas, title, and navigation state).
- Automatically fallback to classic navigation for internals pages that will not have the appropriated DOM tree,
- It's not limited to one container, like jQuery-Pjax is.
- It fully supports browser history (back and forward buttons).
- It supports keyboard browsing.
- Automatically falls back to standard navigation for external pages (thanks to Captain Obvious's help).
- Automatically falls back to standard navigation for internal pages that do not have an appropriate DOM tree.
- You can add pretty cool CSS transitions (animations) very easily. - You can add pretty cool CSS transitions (animations) very easily.
- It's around 3kb (minified & gzipped). - It's around 4kb (minified and gzipped).
### Under the hood ### Under the hood
- It listen to every clicks on links _you want_ (by default all of them), - It listens to every click 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 renders the page's DOM tree (without loading any resources - images, CSS, JS...).
- It check if all defined parts can be replaced: - It checks that all defined parts can be replaced:
- if page doesn't suit requirement, classic navigation used, - If the page doesn't meet the requirements, standard navigation is used.
- if page suits requirement, Pjax does all defined DOM replacements - If the page meets the requirements, Pjax does all defined DOM replacements.
- Then, it updates the browser's current url using pushState - Then it updates the browser's current URL using `pushState()`.
## Overview ## Overview
Pjax is fully automatic. You won't need to setup anything on the existing HTML. Pjax is fully automatic. You don't need to setup anything in the existing HTML.
You just need to designate some elements on your page that will be replaced when You just need to designate some elements on your page that will be replaced when
you navigate your site. you navigate your site.
Consider the following page. Consider the following page.
```html ```html
<!doctype html> <!DOCTYPE html>
<html> <html>
<head> <head>
<!-- metas, title, styles, ... --> <!-- metas, title, styles, ... -->
@@ -78,7 +95,7 @@ Consider the following page.
</html> </html>
``` ```
We want Pjax to grab the url `/blah` then replace `.my-Content` with whatever it gets back. We want Pjax to intercept the URL `/blah`, and replace `.my-Content` with the results of the request.
Oh and the `<nav>` (that contains a status marker somewhere) can be updated too (or stay the same, as you wish). Oh and the `<nav>` (that contains a status marker somewhere) can be updated too (or stay the same, as you wish).
And also the `<aside>` please. And also the `<aside>` please.
So we want to update `[".my-Header", ".my-Content", ".my-Sidebar"]`, **without reloading styles nor scripts**. So we want to update `[".my-Header", ".my-Content", ".my-Sidebar"]`, **without reloading styles nor scripts**.
@@ -89,43 +106,23 @@ We do this by telling Pjax to listen on `a` tags and use CSS selectors defined a
new Pjax({ selectors: ["title", ".my-Header", ".my-Content", ".my-Sidebar"] }) new Pjax({ selectors: ["title", ".my-Header", ".my-Content", ".my-Sidebar"] })
``` ```
Now when someone in a Pjax-compatible browser clicks "blah" the content of all selectors will be replaced with the one found in the "blah" content. Now, when someone in a Pjax-compatible browser clicks "blah", the content of all selectors will be replaced with the one found in the "blah" content.
_Magic! For real!_ **There is completely no need to do anything on server side!** _Magic! For real!_ **There is no need to do anything server-side!**
## Differences with [jQuery-pjax](https://github.com/defunkt/jquery-pjax) ## Differences with [jQuery-pjax](https://github.com/defunkt/jquery-pjax)
- No jQuery dependency, - No jQuery dependency
- Not limited to a container, - Not limited to a container
- No server side requirements, - No server-side requirements
- Works for CommonJS environment (browserify), AMD (RequireJS) or even globally, - Works for CommonJS environment (Webpack/Browserify), AMD (RequireJS) or even globally
- Allow page transition with CSS animations, - Allow page transition with CSS animations
- Can be easily hacked since every method is public (so overridable) - Can be easily tweaked, since every method is public (and as a result, overridable)
## Installation
You can install pjax from **npm**
```shell
$ npm install pjax
```
Or using **bower**
```shell
$ bower install pjax
```
Pjax can obviously be downloaded directly.
## No dependencies
_There is nothing you need. No jQuery or something._
## Compatibility ## Compatibility
Pjax only works with [browsers that support the `history.pushState` API](http://caniuse.com/#search=pushstate). Pjax only works with [browsers that support the `history.pushState()` API](http://caniuse.com/#search=pushstate).
When the API isn't supported Pjax goes into fallback mode (it just does nothing). When the API isn't supported, Pjax goes into fallback mode (and it just does nothing).
To see if Pjax is actually supported by your browser, use `Pjax.isSupported()`. To see if Pjax is actually supported by your browser, use `Pjax.isSupported()`.
@@ -142,13 +139,13 @@ new Pjax({
}) })
``` ```
This will enable Pjax on all links and designate the part to replace using CSS selectors `"title", ".my-Header", ".my-Content", ".my-Sidebar"`. This will enable Pjax on all links, and designate the part to replace using CSS selectors `"title", ".my-Header", ".my-Content", ".my-Sidebar"`.
For some reason, you might want to just target some elements to apply Pjax behavior. For some reason, you might want to just target some elements to apply Pjax behavior.
In that case, you can 2 differents things: In that case, you can do two different things:
- use a custom selector like "a.js-Pjax" or ".js-Pjax a" depending on what you want. - Use a custom selector like "a.js-Pjax" or ".js-Pjax a" depending on what you want.
- override `Pjax.prototype.getElements` that just basically `querSelectorAll` the `elements` option. In this function you just need to return a `NodeList`. - Override `Pjax.prototype.getElements` that just basically `querySelectorAll` the `elements` option. In this function you just need to return a `NodeList`.
```js ```js
// use case 1 // use case 1
@@ -163,17 +160,17 @@ Pjax.prototype.getElements = function() {
new Pjax({}) new Pjax({})
``` ```
When instanciating a `Pjax` object, you need to pass all options as an object: When instantiating a `Pjax` object, you need to pass all options as an object:
#### Options #### Options
##### `elements` (String, default "a[href], form[action]") ##### `elements` (String, default: `"a[href], form[action]"`)
CSS Selector to use to retrieve links to apply Pjax CSS selector to use to retrieve links to apply Pjax to.
##### `selectors` (Array, default ["title", ".js-Pjax"]) ##### `selectors` (Array, default: `["title", ".js-Pjax"]`)
CSS Selectors to replace. If a query returns multiples items, it will just keep the index. CSS selectors to replace. If a query returns multiples items, it will just keep the index.
Example of what you can do: Example of what you can do:
@@ -193,13 +190,13 @@ Example of what you can do:
``` ```
This example is correct and should work "as expected". This example is correct and should work "as expected".
_If there is not the same amount of DOM element from current page and new page, _If the current page and new page do not have the same amount of DOM elements,
the Pjax behavior will fallback to normal page load._ Pjax will fall back to normal page load._
##### `switches` (Object, default {}) ##### `switches` (Object, default: `{}`)
Objects containing callbacks that can be used to switch old element with new element. Objects containing callbacks that can be used to switch old elements with new elements.
Keys should be one of the defined selector. Keys should be one of the defined selectors.
Examples: Examples:
@@ -219,22 +216,22 @@ new Pjax({
}) })
``` ```
Callbacks are binded to Pjax instance itself to allow you to reuse it (ex: `this.onSwitch()`) Callbacks are bound to Pjax instance itself to allow you to reuse it (ex: `this.onSwitch()`)
###### Existing switches callback ###### Existing switches callback
- `Pjax.switches.outerHTML`: default behavior, replace elements using outerHTML - `Pjax.switches.outerHTML`: default behavior, replace elements using outerHTML
- `Pjax.switches.innerHTML`: replace elements using innerHTML & copy className too - `Pjax.switches.innerHTML`: replace elements using innerHTML and copy className too
- `Pjax.switches.sideBySide`: smart replacement that allow you to have both elements in the same parent when you want to use CSS animations. Old elements are removed when all childs have been fully animated ([animationEnd](http://www.w3.org/TR/css3-animations/#animationend) event triggered) - `Pjax.switches.sideBySide`: smart replacement that allows you to have both elements in the same parent when you want to use CSS animations. Old elements are removed when all children have been fully animated ([animationEnd](http://www.w3.org/TR/css3-animations/#animationend) event triggered)
###### Create a switch callback ###### Create a switch callback
Your function can do whatever you want, but you need to: Your function can do whatever you want, but you need to:
- replace oldEl content by newEl content in some fashion - replace `oldEl`'s content with `newEl`'s content in some fashion
- call `this.onSwitch()` to trigger attached callback. - call `this.onSwitch()` to trigger the attached callback.
Here is the default behavior as example Here is the default behavior as an example:
```js ```js
function(oldEl, newEl, pjaxRequestOptions, switchesClasses) { function(oldEl, newEl, pjaxRequestOptions, switchesClasses) {
@@ -243,10 +240,10 @@ function(oldEl, newEl, pjaxRequestOptions, switchesClasses) {
} }
``` ```
##### `switchesOptions` (Object, default {}) ##### `switchesOptions` (Object, default: `{}`)
This are options that can be used during switch by switchers ( for now, only `Pjax.switches.sideBySide` use it). These are options that can be used during switch by switchers (for now, only `Pjax.switches.sideBySide` uses it).
Very convenient when you use something like [Animate.css](https://github.com/daneden/animate.css) This is very convenient when you use something like [Animate.css](https://github.com/daneden/animate.css)
with or without [WOW.js](https://github.com/matthieua/WOW). with or without [WOW.js](https://github.com/matthieua/WOW).
```js ```js
@@ -277,12 +274,13 @@ 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
to create duplicate transition for (slideIn + reverse => slideOut)._ to create duplicate transition for (slideIn + reverse => slideOut)._
The following CSS will be required to make something nice The following CSS will be required to make something nice:
```css ```css
/* /*
@@ -340,8 +338,7 @@ The following CSS will be required to make something nice
} }
``` ```
To get understand this CSS, here is a HTML snippet To give context to this CSS, here is an HTML snippet:
```html ```html
<!doctype html> <!doctype html>
@@ -369,78 +366,99 @@ To get understand this CSS, here is a HTML snippet
</html> </html>
``` ```
##### `history` (Boolean, default true) ##### `history` (Boolean, default: `true`)
Enable pushState. Only disable if you are crazy. Enable the use of `pushState()`. Disabling this will prevent Pjax from updating browser history.
Internaly, this option is used when `popstate` is used (to not pushState again). However, there is almost no use case where you would want to do that.
You should forget that option.
##### `analytics` (Function, default to a function that push `_gaq` `trackPageview` or send `ga` `pageview` Internally, this option is used when a `popstate` event triggers Pjax (to not `pushState()` again).
Function that allow you to add behavior for analytics. By default it try to track ##### `analytics` (Function|Boolean, default: a function that pushes `_gaq` `_trackPageview` or sends `ga` `pageview`
a pageview with Google Analytics.
It's called every time a page is switched, even for history buttons.
##### `scrollTo` (Integer, default to 0) Function that allows you to add behavior for analytics. By default it tries to track
a pageview with Google Analytics (if it exists on the page).
It's called every time a page is switched, even for history navigation.
Value (in px) to scrollTo when a page is switched. Set to `false` to disable this behavior.
##### `debug` (Boolean, default to false) ##### `scrollTo` (Integer, default: `0`)
Enable verbose mode & doesn't use fallback when there is an error. Value (in px from the top of the page) to scroll to when a page is switched.
Useful to debug page layout differences.
##### `currentUrlFullReload` (Boolean, default to false) ##### `scrollRestoration` (Boolean, default: `true`)
When set to true, clicking on a link that point the current url trigger a full page reload. When set to true, attempt to restore the scroll position when navigating backwards or forwards.
#### Extend Pjax ##### `cacheBust` (Boolean, default: `true`)
Pjax prototype & utilities methods can be used & changed so you can patch or hack When set to true, append a timestamp query string segment to the requested URLs
Pjax behavior, as you wish. in order to skip browser cache.
Here is a summary of functions: ##### `debug` (Boolean, default: `false`)
- `Pjax.prototype.log` (`function()`): console.log function that is enable/disabled by `debug` option Enables verbose mode. Useful to debug page layout differences.
- `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` ##### `currentUrlFullReload` (Boolean, default: `false`)
- `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 When set to true, clicking on a link that points to the current URL will trigger a full page reload.
- `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`) The default is `false`, so clicking on such a link will do nothing.
- `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) If you want to add some custom behavior, add a click listener to the link,
- `Pjax.prototype.loadContent` (`function(html, options)`): switch elements for each selectors and set `preventDefault` to true, to prevent Pjax from receiving the event.
- `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. Here is some sample code:
```js
var links = document.querySelectorAll(".js-Pjax");
for (var i = 0; i < links.length; i++) {
var el = links[i]
el.addEventListener("click", function(e) {
if (el.href === window.location.href.split("#")[0]) {
e.preventDefault();
console.log("Link to current page clicked");
// Custom code goes here.
}
})
}
```
(Note that if `cacheBust` is set to true, the code that checks if the href
is the same as the current page's URL will not work, due to the query string
appended to force a cache bust).
##### `timeout` (Integer, default: `0`)
The timeout in milliseconds for the XHR requests. Set to 0 to disable the timeout.
### Events ### Events
Pjax fires a number of events regardless of how its invoked. Pjax fires a number of events regardless of how it's invoked.
All events are fired from the _document_, not the link was clicked. All events are fired from the _document_, not the link that was clicked.
#### Ajax related events
* `pjax:send` - Fired after the Pjax request begins. * `pjax:send` - Fired after the Pjax request begins.
* `pjax:complete` - Fired after the Pjax request finishes. * `pjax:complete` - Fired after the Pjax request finishes.
* `pjax:success` - Fired after the Pjax request succeeds. * `pjax:success` - Fired after the Pjax request succeeds.
* `pjax:error` - Fired after the Pjax request fails. Returning false will prevent the the fallback redirect. * `pjax:error` - Fired after the Pjax request fails.
`send` and `complete` are a good pair of events to use if you are implementing a loading indicator (eg: [topbar](http://buunguyen.github.io/topbar/)) `send` and `complete` are a good pair of events to use if you are implementing a loading indicator (eg: [topbar](http://buunguyen.github.io/topbar/))
```js ```js
$(document).on('pjax:send', topbar.show) document.addEventListener('pjax:send', topbar.show)
$(document).on('pjax:complete', topbar.hide) document.addEventListener('pjax:complete', topbar.hide)
``` ```
#### Note about DOM ready state #### Note about DOM ready state
Most of the time, you have code attached & related to the current DOM, that you only execute when page/dom is ready. Most of the time, you will have code related to the current DOM that you only execute when the DOM is ready.
Since Pjax doesn't magically rexecute you previous code each time you load a page, you need to make a simple thing to rexecute appropriate code:
Since Pjax doesn't magically re-execute your previous code each time you load a page, you need to add some simple code to achieve this:
```js ```js
function whenDOMReady() { function whenDOMReady() {
// do you stuff // do your stuff
} }
whenDOMReady() whenDOMReady()
@@ -450,17 +468,18 @@ new Pjax()
document.addEventListener("pjax:success", whenDOMReady) document.addEventListener("pjax:success", whenDOMReady)
``` ```
_Note: Don't create the Pjax in the `whenDOMReady` function._ _Note: Don't create the Pjax instance in the `whenDOMReady` function._
For my concern & usage, I `js-Pjax`ify all body children, including stuff like navigation & footer (to get navigation state easily updated). For my concern and usage, I `js-Pjax`-ify all body children, including stuff like navigation and footer (to get navigation state easily updated).
So attached behavior are rexecuted each time a page is loaded, like in the snippet above.
The attached behavior is re-executed each time a page is loaded, like in the snippet above.
If you want to just update a specific part (it's totally a good idea), you can just If you want to just update a specific part (it's totally a good idea), you can just
add the DOM related code in a function & rexecute this function when "pjax:success" event is done. add the DOM-related code in a function and re-execute this function when "pjax:success" event is fired.
```js ```js
// do your global stuff // do your global stuff
//... dom ready blah blah //... DOM ready blah blah
function whenContainerReady() { function whenContainerReady() {
// do your container related stuff // do your container related stuff
@@ -479,12 +498,12 @@ document.addEventListener("pjax:success", whenContainerReady)
### Q: Disqus doesn't work anymore, how can I fix that ? ### Q: Disqus doesn't work anymore, how can I fix that ?
A: There is a few things you need to do: A: There is a few things you need to do:
- wrap your disqus snippet into a dom element that you will add to the `selector` - wrap your Disqus snippet into a DOM element that you will add to the `selector`
arra (or just wrap it with class="js-Pjax") & be sure to have a least the empty property (or just wrap it with `class="js-Pjax"`) and be sure to have at least the empty
wrapper on each page (to avoid differences of DOM between pages) wrapper on each page (to avoid differences of DOM between pages)
- edit your disqus snippet like the one below - edit your Disqus snippet like the one below
#### Disqus snippet before pjax integration #### Disqus snippet before Pjax integration
```html ```html
<script> <script>
@@ -502,8 +521,8 @@ wrapper on each page (to avoid differences of DOM between pages)
#### Disqus snippet after Pjax integration #### Disqus snippet after Pjax integration
```html ```html
<div class="js-Pjax"><!-- need to be here on every pjaxified page, even if empty --> <div class="js-Pjax"><!-- needs to be here on every Pjax-ified page, even if empty -->
<!-- if (blah blah) { // eventual server side test to know wheter or not you include this script --> <!-- if (blah blah) { // eventual server-side test to know whether or not you include this script -->
<script> <script>
var disqus_shortname = 'YOURSHORTNAME' var disqus_shortname = 'YOURSHORTNAME'
var disqus_identifier = 'PAGEID' var disqus_identifier = 'PAGEID'
@@ -533,19 +552,22 @@ wrapper on each page (to avoid differences of DOM between pages)
</div> </div>
``` ```
**Note: The thing you need to understand is that Pjax handle inline `<script>` only for container you are reloading.** **Note: Pjax only handles inline `<script>` blocks for the container you are switching.**
--- ---
## 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`, which will open the example app in your browser.
$ git clone https://github.com/MoOx/pjax.git ---
$ git checkout -b patch-1
$ npm install
$ npm test
## [Changelog](CHANGELOG.md) ## CONTRIBUTING
## [License](LICENSE) * ⇄ 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)

View File

@@ -1,29 +0,0 @@
{
"name": "pjax",
"version": "0.1.4",
"description": "Easily enable fast Ajax navigation on any website (using pushState + xhr)",
"keywords": [
"pjax",
"push",
"state",
"ajax",
"navigation",
"transition",
"animation"
],
"main": "src/pjax.js",
"homepage": "https://github.com/MoOx/pjax",
"authors": [
"Maxime Thirouin"
],
"license": "MIT",
"moduleType": [
"node"
],
"ignore": [
"**/.*",
"node_modules",
"test",
"tests"
]
}

27
example/example.js Normal file
View File

@@ -0,0 +1,27 @@
/* 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({
elements: [".js-Pjax"],
selectors: [".body"]
// currentUrlFullReload: true,
})
console.log("Pjax initialized.", pjax)
})

17
example/index.html Normal file
View 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' class="js-Pjax">Page 2</a> or <a href='page3.html' class="js-Pjax">Page 3</a> and view your console to see Pjax events.
Clicking on <a href='index.html'>this page</a> will just reload the page entirely.
</div>
</body>
</html>

15
example/page2.html Normal file
View 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' class="js-Pjax">Index</a>.
</div>
</body>
</html>

15
example/page3.html Normal file
View 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 3</h1>
Hello. Go to <a href='index.html' class="js-Pjax">Index</a>.
</div>
</body>
</html>

286
index.js
View File

@@ -1,28 +1,42 @@
var clone = require("./lib/clone.js")
var executeScripts = require("./lib/execute-scripts.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 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 Pjax = function(options) { var contains = require("./lib/util/contains.js")
this.firstrun = true var noop = require("./lib/util/noop")
var parseOptions = require("./lib/proto/parse-options.js"); var Pjax = function(options) {
parseOptions.apply(this,[options]) this.state = {
numPendingSwitches: 0,
href: null,
options: null
}
var parseOptions = require("./lib/proto/parse-options.js")
parseOptions.call(this,options)
this.log("Pjax options", this.options) this.log("Pjax options", this.options)
if (this.options.scrollRestoration && "scrollRestoration" in history) {
history.scrollRestoration = "manual"
}
this.maxUid = this.lastUid = newUid() this.maxUid = this.lastUid = newUid()
this.parseDOM(document) this.parseDOM(document)
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
opt.requestOptions = {}
opt.scrollPos = st.state.scrollPos
if (st.state.uid < this.lastUid) { if (st.state.uid < this.lastUid) {
opt.backward = true opt.backward = true
} }
@@ -37,44 +51,57 @@ var Pjax = function(options) {
}.bind(this)) }.bind(this))
} }
Pjax.switches = switches
Pjax.prototype = { Pjax.prototype = {
log: require("./lib/proto/log.js"), log: require("./lib/proto/log.js"),
getElements: require("./lib/proto/get-elements.js"), getElements: function(el) {
return el.querySelectorAll(this.options.elements)
},
parseDOM: require("./lib/proto/parse-dom.js"), parseDOM: function(el) {
var parseElement = require("./lib/proto/parse-element")
forEachEls(this.getElements(el), parseElement, this)
},
refresh: require("./lib/proto/refresh.js"), refresh: function(el) {
this.parseDOM(el || document)
},
reload: function() {
window.location.reload()
},
attachLink: require("./lib/proto/attach-link.js"), attachLink: require("./lib/proto/attach-link.js"),
attachForm: require("./lib/proto/attach-form.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
// + its too dangerous
// switchFallback: function(fromEl, toEl) {
// this.switchSelectors(["head", "body"], fromEl, toEl)
// // execute script when DOM is like it should be
// Pjax.executeScripts(document.querySelector("head"))
// Pjax.executeScripts(document.querySelector("body"))
// }
latestChance: function(href) { latestChance: function(href) {
window.location = href window.location = href
}, },
onSwitch: function() { onSwitch: function() {
trigger(window, "resize scroll") trigger(window, "resize scroll")
this.state.numPendingSwitches--
// debounce calls, so we only run this once after all switches are finished.
if (this.state.numPendingSwitches === 0) {
this.afterAllSwitches()
}
}, },
loadContent: function(html, options) { loadContent: function(html, options) {
var tmpEl = document.implementation.createHTMLDocument() var tmpEl = document.implementation.createHTMLDocument("pjax")
// parse HTML attributes to copy them // parse HTML attributes to copy them
// since we are forced to use documentElement.innerHTML (outerHTML can't be used for <html>) // since we are forced to use documentElement.innerHTML (outerHTML can't be used for <html>)
@@ -87,7 +114,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))
}
}) })
} }
} }
@@ -96,59 +128,73 @@ Pjax.prototype = {
this.log("load content", tmpEl.documentElement.attributes, tmpEl.documentElement.innerHTML.length) this.log("load content", tmpEl.documentElement.attributes, tmpEl.documentElement.innerHTML.length)
// Clear out any focused controls before inserting new page contents. // Clear out any focused controls before inserting new page contents.
// we clear focus on non form elements if (document.activeElement && contains(this.options.selectors, document.activeElement)) {
if (document.activeElement && !document.activeElement.value) {
try { try {
document.activeElement.blur() document.activeElement.blur()
} catch (e) { } } catch (e) { }
} }
// try {
this.switchSelectors(this.options.selectors, tmpEl, document, options) this.switchSelectors(this.options.selectors, tmpEl, document, options)
// FF bug: Wont autofocus fields that are inserted via JS.
// This behavior is incorrect. So if theres no current focus, autofocus
// the last field.
//
// http://www.w3.org/html/wg/drafts/html/master/forms.html
var autofocusEl = Array.prototype.slice.call(document.querySelectorAll("[autofocus]")).pop()
if (autofocusEl && document.activeElement !== autofocusEl) {
autofocusEl.focus();
}
// execute scripts when DOM have been completely updated
this.options.selectors.forEach(function(selector) {
Pjax.forEachEls(document.querySelectorAll(selector), function(el) {
Pjax.executeScripts(el)
})
})
// }
// catch(e) {
// if (this.options.debug) {
// this.log("Pjax switch fail: ", e)
// }
// this.switchFallback(tmpEl, document)
// }
}, },
doRequest: require("./lib/request.js"), abortRequest: require("./lib/abort-request.js"),
doRequest: require("./lib/send-request.js"),
loadUrl: function(href, options) { loadUrl: function(href, options) {
this.log("load href", href, options) this.log("load href", href, options)
trigger(document, "pjax:send", options); // Abort any previous request
this.abortRequest(this.request)
trigger(document, "pjax:send", options)
// Do the request // Do the request
this.doRequest(href, function(html) { options.requestOptions.timeout = this.options.timeout
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
} }
// Clear out any focused controls before inserting new page contents. // push scroll position to history
document.activeElement.blur() var currentState = window.history.state || {}
window.history.replaceState({
url: currentState.url || window.location.href,
title: currentState.title || document.title,
uid: currentState.uid || newUid(),
scrollPos: [document.documentElement.scrollLeft || document.body.scrollLeft,
document.documentElement.scrollTop || document.body.scrollTop]
},
document.title, window.location)
var oldHref = href
if (request.responseURL) {
if (href !== request.responseURL) {
href = request.responseURL
}
}
else if (request.getResponseHeader("X-PJAX-URL")) {
href = request.getResponseHeader("X-PJAX-URL")
}
else if (request.getResponseHeader("X-XHR-Redirected-To")) {
href = request.getResponseHeader("X-XHR-Redirected-To")
}
// Add back the hash if it was removed
var a = document.createElement("a")
a.href = oldHref
var oldHash = a.hash
a.href = href
if (oldHash && !a.hash) {
a.hash = oldHash
href = a.href
}
this.state.href = href
this.state.options = clone(options)
try { try {
this.loadContent(html, options) this.loadContent(html, options)
@@ -158,71 +204,127 @@ 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
} }
} }
}.bind(this))
},
if (options.history) { afterAllSwitches: function() {
if (this.firstrun) { // FF bug: Wont autofocus fields that are inserted via JS.
this.lastUid = this.maxUid = newUid() // This behavior is incorrect. So if theres no current focus, autofocus
this.firstrun = false // the last field.
window.history.replaceState({ //
// http://www.w3.org/html/wg/drafts/html/master/forms.html
var autofocusEl = Array.prototype.slice.call(document.querySelectorAll("[autofocus]")).pop()
if (autofocusEl && document.activeElement !== autofocusEl) {
autofocusEl.focus()
}
// execute scripts when DOM have been completely updated
this.options.selectors.forEach(function(selector) {
forEachEls(document.querySelectorAll(selector), function(el) {
executeScripts(el)
})
})
var state = this.state
if (state.options.history) {
if (!window.history.state) {
this.lastUid = this.maxUid = newUid()
window.history.replaceState({
url: window.location.href, url: window.location.href,
title: document.title, title: document.title,
uid: this.maxUid uid: this.maxUid,
scrollPos: [0, 0]
}, },
document.title) document.title)
}
// Update browser history
this.lastUid = this.maxUid = newUid()
window.history.pushState({
url: href,
title: options.title,
uid: this.maxUid
},
options.title,
href)
} }
this.forEachSelectors(function(el) { // Update browser history
this.parseDOM(el) this.lastUid = this.maxUid = newUid()
}, this)
// Fire Events window.history.pushState({
trigger(document,"pjax:complete pjax:success", options) url: state.href,
title: state.options.title,
uid: this.maxUid,
scrollPos: [0, 0]
},
state.options.title,
state.href)
}
options.analytics() this.forEachSelectors(function(el) {
this.parseDOM(el)
}, this)
// Scroll page to top on new page load // Fire Events
if (options.scrollTo !== false) { trigger(document,"pjax:complete pjax:success", state.options)
if (options.scrollTo.length > 1) {
window.scrollTo(options.scrollTo[0], options.scrollTo[1]) if (typeof state.options.analytics === "function") {
state.options.analytics()
}
if (state.options.history) {
// First parse url and check for hash to override scroll
var a = document.createElement("a")
a.href = this.state.href
if (a.hash) {
var name = a.hash.slice(1)
name = decodeURIComponent(name)
var curtop = 0
var target = document.getElementById(name) || document.getElementsByName(name)[0]
if (target) {
// http://stackoverflow.com/questions/8111094/cross-browser-javascript-function-to-find-actual-position-of-an-element-in-page
if (target.offsetParent) {
do {
curtop += target.offsetTop
target = target.offsetParent
} while (target)
}
}
window.scrollTo(0, curtop)
}
else if (state.options.scrollTo !== false) {
// Scroll page to top on new page load
if (state.options.scrollTo.length > 1) {
window.scrollTo(state.options.scrollTo[0], state.options.scrollTo[1])
} }
else { else {
window.scrollTo(0, options.scrollTo) window.scrollTo(0, state.options.scrollTo)
} }
} }
}.bind(this)) }
else if (state.options.scrollRestoration && state.options.scrollPos) {
window.scrollTo(state.options.scrollPos[0], state.options.scrollPos[1])
}
this.state = {
numPendingSwitches: 0,
href: null,
options: null
}
} }
} }
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()) {
module.exports = Pjax module.exports = Pjax
} }
// if there isnt required browser functions, returning stupid api // if there isnt required browser functions, returning stupid api
else { else {
var stupidPjax = function() {} var stupidPjax = noop
for (var key in Pjax.prototype) { for (var key in Pjax.prototype) {
if (Pjax.prototype.hasOwnProperty(key) && typeof Pjax.prototype[key] === "function") { if (Pjax.prototype.hasOwnProperty(key) && typeof Pjax.prototype[key] === "function") {
stupidPjax[key] = stupidPjax stupidPjax[key] = noop
} }
} }

8
lib/abort-request.js Normal file
View File

@@ -0,0 +1,8 @@
var noop = require("./util/noop")
module.exports = function(request) {
if (request && request.readyState < 4) {
request.onreadystatechange = noop
request.abort()
}
}

View File

@@ -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()

View File

@@ -1,8 +1,7 @@
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 head = document.querySelector("head") || document.documentElement var src = (el.src || "")
var parent = el.parentNode || document.querySelector("head") || document.documentElement
var script = document.createElement("script") var script = document.createElement("script")
if (code.match("document.write")) { if (code.match("document.write")) {
@@ -13,17 +12,28 @@ module.exports = function(el) {
} }
script.type = "text/javascript" script.type = "text/javascript"
try {
script.appendChild(document.createTextNode(code)) if (src !== "") {
script.src = src
script.async = false // force synchronous loading of peripheral JS
} }
catch (e) {
// old IEs have funky script nodes if (code !== "") {
script.text = code try {
script.appendChild(document.createTextNode(code))
}
catch (e) {
// old IEs have funky script nodes
script.text = code
}
} }
// execute // execute
head.insertBefore(script, head.firstChild) parent.appendChild(script)
head.removeChild(script) // avoid pollution // avoid pollution only in head or body tags
if (["head", "body"].indexOf(parent.tagName.toLowerCase()) > 0) {
parent.removeChild(script)
}
return true return true
} }

View File

@@ -4,7 +4,7 @@ module.exports = function(els, events, opts) {
events = (typeof events === "string" ? events.split(" ") : events) events = (typeof events === "string" ? events.split(" ") : events)
events.forEach(function(e) { events.forEach(function(e) {
var event // = new CustomEvent(e) // doesn't everywhere yet var event
event = document.createEvent("HTMLEvents") event = document.createEvent("HTMLEvents")
event.initEvent(e, true, true) event.initEvent(e, true, true)
event.eventName = e event.eventName = e
@@ -16,9 +16,9 @@ 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) // THANK YOU IE (9/10/11)
// dispatchEvent doesn't work if element is not in the dom // dispatchEvent doesn't work if the element is not in the DOM
domFix = true domFix = true
document.body.appendChild(el) document.body.appendChild(el)
} }

View File

@@ -3,7 +3,10 @@ var evalScript = require("./eval-script")
// Finds and executes scripts (used for newly added elements) // Finds and executes scripts (used for newly added elements)
// Needed since innerHTML does not run scripts // Needed since innerHTML does not run scripts
module.exports = function(el) { module.exports = function(el) {
// console.log("going to execute scripts for ", el) if (el.tagName.toLowerCase() === "script") {
evalScript(el)
}
forEachEls(el.querySelectorAll("script"), function(script) { forEachEls(el.querySelectorAll("script"), function(script) {
if (!script.type || script.type.toLowerCase() === "text/javascript") { if (!script.type || script.type.toLowerCase() === "text/javascript") {
if (script.parentNode) { if (script.parentNode) {

View File

@@ -4,6 +4,6 @@ module.exports = function(els, fn, context) {
if (els instanceof HTMLCollection || els instanceof NodeList || els instanceof Array) { if (els instanceof HTMLCollection || els instanceof NodeList || els instanceof Array) {
return Array.prototype.forEach.call(els, fn, context) return Array.prototype.forEach.call(els, fn, context)
} }
// assume simple dom element // assume simple DOM element
return fn.call(context, els) return fn.call(context, els)
} }

View File

@@ -1,20 +0,0 @@
if (!Function.prototype.bind) {
Function.prototype.bind = function(oThis) {
if (typeof this !== "function") {
// closest thing possible to the ECMAScript 5 internal IsCallable function
throw new TypeError("Function.prototype.bind - what is trying to be bound is not callable")
}
var aArgs = Array.prototype.slice.call(arguments, 1)
var that = this
var Fnoop = function() {}
var fBound = function() {
return that.apply(this instanceof Fnoop && oThis ? this : oThis, aArgs.concat(Array.prototype.slice.call(arguments)))
}
Fnoop.prototype = this.prototype
fBound.prototype = new Fnoop()
return fBound
}
}

95
lib/proto/attach-form.js Normal file
View File

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

View File

@@ -1,5 +1,3 @@
require("../polyfills/Function.prototype.bind")
var on = require("../events/on") var on = require("../events/on")
var clone = require("../clone") var clone = require("../clone")
@@ -7,6 +5,13 @@ var attrClick = "data-pjax-click-state"
var attrKey = "data-pjax-keyup-state" 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,
// clone it so the changes don't persist
var options = clone(this.options)
// Initialize requestOptions since loadUrl expects it to be an object
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) {
el.setAttribute(attrClick, "modifier") el.setAttribute(attrClick, "modifier")
@@ -43,31 +48,48 @@ var linkAction = function(el, event) {
event.preventDefault() event.preventDefault()
// dont do "nothing" if user try to reload the page by clicking the same link twice // dont 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
} }
el.setAttribute(attrClick, "load") el.setAttribute(attrClick, "load")
this.loadUrl(el.href, clone(this.options))
options.triggerElement = el
this.loadUrl(el.href, 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
}
// 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) {
el.setAttribute(attrKey, "modifier") el.setAttribute(attrKey, "modifier")
return return
} }
if (event.keyCode == 13) { if (event.keyCode === 13) {
linkAction.call(that, el, event) linkAction.call(that, el, event)
} }
}.bind(this)) }.bind(this))

View File

@@ -1,3 +0,0 @@
module.exports = function(el) {
return el.querySelectorAll(this.options.elements)
}

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

@@ -1,7 +0,0 @@
var forEachEls = require("../foreach-els")
var parseElement = require("./parse-element")
module.exports = function(el) {
forEachEls(this.getElements(el), parseElement, this)
}

View File

@@ -1,17 +1,20 @@
module.exports = function(el) { module.exports = function(el) {
switch (el.tagName.toLowerCase()) { switch (el.tagName.toLowerCase()) {
case "a": case "a":
// only attach link if el does not already have link attached // only attach link if el does not already have link attached
if (!el.hasAttribute('data-pjax-click-state')) { if (!el.hasAttribute("data-pjax-click-state")) {
this.attachLink(el) this.attachLink(el)
} }
break break
case "form": case "form":
throw "Pjax doesnt support <form> yet." // only attach link if el does not already have link attached
break if (!el.hasAttribute("data-pjax-click-state")) {
this.attachForm(el)
}
break
default: default:
throw "Pjax can only be applied on <a> or <form> submit" throw "Pjax can only be applied on <a> or <form> submit"
} }
} }

View File

@@ -1,37 +1,40 @@
/* global _gaq: true, ga: true */ /* global _gaq: true, ga: true */
module.exports = function(options){ var defaultSwitches = require("../switches")
this.options = options
this.options.elements = this.options.elements || "a[href], form[action]"
this.options.selectors = this.options.selectors || ["title", ".js-Pjax"]
this.options.switches = this.options.switches || {}
this.options.switchesOptions = this.options.switchesOptions || {}
this.options.history = this.options.history || true
this.options.analytics = this.options.analytics || function(options) {
// options.backward or options.foward can be true or undefined
// by default, we do track back/foward hit
// https://productforums.google.com/forum/#!topic/analytics/WVwMDjLhXYk
if (window._gaq) {
_gaq.push(["_trackPageview"])
}
if (window.ga) {
ga("send", "pageview", {page: options.url, title: options.title})
}
}
this.options.scrollTo = (typeof this.options.scrollTo === 'undefined') ? 0 : this.options.scrollTo;
this.options.debug = this.options.debug || false
// we cant replace body.outerHTML or head.outerHTML module.exports = function(options) {
// it create a bug where new body or new head are created in the dom options = options || {}
// if you set head.outerHTML, a new body tag is appended, so the dom get 2 body options.elements = options.elements || "a[href], form[action]"
// & it break the switchFallback which replace head & body options.selectors = options.selectors || ["title", ".js-Pjax"]
if (!this.options.switches.head) { options.switches = options.switches || {}
this.options.switches.head = this.switchElementsAlt options.switchesOptions = options.switchesOptions || {}
options.history = options.history || true
options.analytics = (typeof options.analytics === "function" || options.analytics === false) ?
options.analytics :
function() {
if (window._gaq) {
_gaq.push(["_trackPageview"])
}
if (window.ga) {
ga("send", "pageview", {page: location.pathname, title: document.title})
}
}
options.scrollTo = (typeof options.scrollTo === "undefined") ? 0 : options.scrollTo
options.scrollRestoration = (typeof options.scrollRestoration !== "undefined") ? options.scrollRestoration : true
options.cacheBust = (typeof options.cacheBust === "undefined") ? true : options.cacheBust
options.debug = options.debug || false
options.timeout = options.timeout || 0
options.currentUrlFullReload = (typeof options.currentUrlFullReload === "undefined") ? false : options.currentUrlFullReload
// We cant replace body.outerHTML or head.outerHTML.
// It creates a bug where a new body or head are created in the DOM.
// If you set head.outerHTML, a new body tag is appended, so the DOM has 2 body nodes, and vice versa
if (!options.switches.head) {
options.switches.head = defaultSwitches.switchElementsAlt
} }
if (!this.options.switches.body) { if (!options.switches.body) {
this.options.switches.body = this.switchElementsAlt options.switches.body = defaultSwitches.switchElementsAlt
} }
if (typeof options.analytics !== "function") {
options.analytics = function() {} this.options = options
} }
}

View File

@@ -1,6 +0,0 @@
var parseDom = require("./parse-dom")
module.exports = function(el) {
parseDom(el || document)
}

View File

@@ -1,3 +0,0 @@
module.exports = function() {
window.location.reload()
}

View File

@@ -1,19 +0,0 @@
module.exports = function(location, callback) {
var request = new XMLHttpRequest()
request.onreadystatechange = function() {
if (request.readyState === 4) {
if (request.status === 200) {
callback(request.responseText, request)
}
else {
callback(null, request)
}
}
}
request.open("GET", location + (!/[?&]/.test(location) ? "?" : "&") + (new Date().getTime()), true)
request.setRequestHeader("X-Requested-With", "XMLHttpRequest")
request.send(null)
return request
}

46
lib/send-request.js Normal file
View File

@@ -0,0 +1,46 @@
module.exports = function(location, options, callback) {
options = options || {}
var requestMethod = options.requestMethod || "GET"
var requestPayload = options.requestPayloadString || null
var request = new XMLHttpRequest()
request.onreadystatechange = function() {
if (request.readyState === 4) {
if (request.status === 200) {
callback(request.responseText, request)
}
else {
callback(null, request)
}
}
}
request.onerror = function(e) {
console.log(e)
callback(null, request)
}
request.ontimeout = function() {
callback(null, request)
}
// 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(requestMethod.toUpperCase(), location, true)
request.timeout = options.timeout
request.setRequestHeader("X-Requested-With", "XMLHttpRequest")
request.setRequestHeader("X-PJAX", "true")
// Add the request payload if available
if (options.requestPayloadString !== undefined && options.requestPayloadString !== "") {
// Send the proper header information along with the request
request.setRequestHeader("Content-type", "application/x-www-form-urlencoded")
}
request.send(requestPayload)
return request
}

View File

@@ -3,6 +3,8 @@ 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 = []
selectors.forEach(function(selector) { selectors.forEach(function(selector) {
var newEls = fromEl.querySelectorAll(selector) var newEls = fromEl.querySelectorAll(selector)
var oldEls = toEl.querySelectorAll(selector) var oldEls = toEl.querySelectorAll(selector)
@@ -10,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
} }
@@ -24,12 +20,18 @@ module.exports = function(switches, switchesOptions, selectors, fromEl, toEl, op
if (this.log) { if (this.log) {
this.log("newEl", newEl, "oldEl", oldEl) this.log("newEl", newEl, "oldEl", oldEl)
} }
if (switches[selector]) {
switches[selector].bind(this)(oldEl, newEl, options, switchesOptions[selector]) var callback = (switches[selector]) ?
} switches[selector].bind(this, oldEl, newEl, options, switchesOptions[selector]) :
else { defaultSwitches.outerHTML.bind(this, oldEl, newEl, options)
defaultSwitches.outerHTML.bind(this)(oldEl, newEl, options)
} switchesQueue.push(callback)
}, this) }, this)
}, this) }, this)
this.state.numPendingSwitches = switchesQueue.length
switchesQueue.forEach(function(queuedSwitch) {
queuedSwitch()
})
} }

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) {
@@ -15,18 +12,29 @@ module.exports = {
this.onSwitch() this.onSwitch()
}, },
switchElementsAlt: function(oldEl, newEl) {
oldEl.innerHTML = newEl.innerHTML
// Copy attributes from the new element to the old one
if (newEl.hasAttributes()) {
var attrs = newEl.attributes
for (var i = 0; i < attrs.length; i++) {
oldEl.attributes.setNamedItem(attrs[i].cloneNode())
}
}
this.onSwitch()
},
sideBySide: function(oldEl, newEl, options, switchOptions) { sideBySide: function(oldEl, newEl, options, switchOptions) {
var forEach = Array.prototype.forEach var forEach = Array.prototype.forEach
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) {
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
} }
@@ -44,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) {
@@ -109,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"
} }
} }

12
lib/util/contains.js Normal file
View File

@@ -0,0 +1,12 @@
module.exports = function contains(doc, selectors, el) {
for (var i = 0; i < selectors.length; i++) {
var selectedEls = doc.querySelectorAll(selectors[i])
for (var j = 0; j < selectedEls.length; j++) {
if (selectedEls[j].contains(el)) {
return true
}
}
}
return false
}

1
lib/util/noop.js Normal file
View File

@@ -0,0 +1 @@
module.exports = function() {}

View File

@@ -1,58 +1,55 @@
{ {
"name": "pjax", "name": "pjax",
"version": "0.2.0", "version": "0.2.5",
"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",
"push", "pushstate",
"state",
"ajax", "ajax",
"navigation", "navigation",
"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",
"contributors": [
"BehindTheMath",
"Robin North (http://robinnorth.co.uk)"
],
"license": "MIT", "license": "MIT",
"main": "src/pjax.js", "main": "index.js",
"files": [ "files": [
"CHANGELOG.md", "index.js",
"LICENSE", "lib",
"src" "pjax.js",
"pjax.min.js"
], ],
"devDependencies": { "devDependencies": {
"browserify": "^3.46.0", "browserify": "^15.0.0",
"coverify": "^1.0.6", "jscs": "^3.0.7",
"jscs": "^1.6.2", "jsdom": "^11.5.1",
"jsdom-global": "^3.0.2",
"jshint": "^2.5.6", "jshint": "^2.5.6",
"tape": "^3.0.0", "npmpub": "^3.1.0",
"testling": "^1.6.1" "nyc": "^11.4.1",
"opn-cli": "^3.1.0",
"serve": "^6.4.4",
"tap-nyc": "^1.0.3",
"tap-spec": "^4.1.1",
"tape": "^4.8.0",
"uglify-js": "^3.3.8"
}, },
"scripts": { "scripts": {
"lint": "jscs **/*.js && jshint . --exclude-path .gitignore", "lint": "jscs . && jshint . --exclude-path .gitignore",
"test": "npm run lint && testling", "standalone": "browserify index.js --standalone Pjax > pjax.js",
"test--html": "testling --html > tests/scripts/index.html", "build": "npm run standalone && uglifyjs pjax.js -o pjax.min.js",
"coverage": "browserify -t coverify tests/**/*.js | testling | coverify" "build-debug": "browserify index.js --debug --standalone Pjax > pjax.js",
}, "tests": "tape -r ./tests/setup.js \"./tests/**/*.js\"",
"testling": { "test": "npm run lint && npm run tests | tap-spec",
"files": "tests/**/*.js", "coverage-tests": "npm run tests | tap-nyc",
"browsers": [ "coverage": "nyc -x \"tests/**\" npm run coverage-tests",
"ie/10..latest", "example": "opn http://localhost:3000/example/ && serve -p 3000 .",
"firefox/4.0", "firefox/latest", "firefox/nightly", "prepublish": "npm run build",
"chrome/10", "chrome/latest", "chrome/canary", "release": "npmpub"
"opera/12..latest",
"opera/next",
"safari/5.1..latest",
"ipad/6.0..latest",
"iphone/6.0..latest",
"android-browser/4.2..latest"
]
} }
} }

File diff suppressed because one or more lines are too long

View File

@@ -0,0 +1,56 @@
var tape = require("tape")
var abortRequest = require("../../lib/abort-request.js")
var sendRequest = require("../../lib/send-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 aborting xhr request", function(t) {
var requestCacheBust = sendRequest.bind({
options: {
cacheBust: true
}
})
t.test("- pending request is aborted", function(t) {
var r = requestCacheBust("https://httpbin.org/delay/10", {}, function() {
t.fail("xhr was not aborted")
})
t.equal(r.readyState, 1, "xhr readyState is '1' (SENT)")
abortRequest(r)
t.equal(r.readyState, 0, "xhr readyState is '0' (ABORTED)")
t.equal(r.status, 0, "xhr HTTP status is '0' (ABORTED)")
t.equal(r.responseText, "", "xhr response is empty")
t.end()
})
t.test("- request is not aborted if it has already completed", function(t) {
var r = requestCacheBust("https://httpbin.org/get", {}, function() {
abortRequest(r)
t.equal(r.readyState, 4, "xhr readyState is '4' (DONE)")
t.equal(r.status, 200, "xhr HTTP status is '200' (OK)")
t.end()
})
})
t.test("- request is not aborted if it is undefined", function(t) {
var r
try {
abortRequest(r)
}
catch (e) {
t.fail("aborting an undefined request threw an error")
}
t.equal(typeof r, "undefined", "undefined xhr was ignored")
t.end()
})
t.end()
})

View File

@@ -13,10 +13,11 @@ tape("test evalScript method", function(t) {
evalScript(script) evalScript(script)
t.equal(document.body.className, "executed", "script has been properly executed") t.equal(document.body.className, "executed", "script has been properly executed")
// script.innerHTML = "document.write('failure')" script.innerHTML = "document.write('failure')"
// var bodyText = document.body.text document.body.text = "document.write hasn't been executed"
// evalScript(script) var bodyText = document.body.text
// t.equal(document.body.text, bodyText, "document.write hasn't been executed") evalScript(script)
t.equal(document.body.text, bodyText, "document.write hasn't been executed")
t.end() t.end()
}) })

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"
} }
@@ -90,3 +89,22 @@ 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 = ""
// With jsdom, the default this is global, not window, so we need to explicitly bind to window.
on(el, "click", classCb.bind(window))
trigger(el, "click")
t.equal(el.className, "on", "attached callback has been fired properly on window")
t.end()
})

View File

@@ -0,0 +1,95 @@
var tape = require("tape")
var on = require("../../../lib/events/on")
var trigger = require("../../../lib/events/trigger")
var attachForm = require("../../../lib/proto/attach-form")
var form = document.createElement("form")
var attr = "data-pjax-click-state"
var preventDefault = function(e) { e.preventDefault() }
tape("test attach form prototype method", function(t) {
t.plan(7)
attachForm.call({
options: {},
reload: function() {
t.equal(form.getAttribute(attr), "reload", "triggering a simple reload will just submit the form")
},
loadUrl: function() {
t.equal(form.getAttribute(attr), "submit", "triggering a post to the next page")
}
}, form)
var internalUri = window.location.protocol + "//" + window.location.host + window.location.pathname + window.location.search
form.action = "http://external.com/"
trigger(form, "submit")
t.equal(form.getAttribute(attr), "external", "external url stop behavior")
form.action = internalUri + "#anchor"
trigger(form, "submit")
t.equal(form.getAttribute(attr), "anchor-present", "internal anchor stop behavior")
window.location.hash = "#anchor"
form.action = internalUri + "#another-anchor"
trigger(form, "submit")
t.notEqual(form.getAttribute(attr), "anchor", "differents anchors stop behavior")
window.location.hash = ""
form.action = internalUri + "#"
trigger(form, "submit")
t.equal(form.getAttribute(attr), "anchor-empty", "empty anchor stop behavior")
form.action = internalUri
trigger(form, "submit")
// see reload defined above
form.action = window.location.protocol + "//" + window.location.host + "/internal"
form.method = "POST"
trigger(form, "submit")
// see post defined above
form.action = window.location.protocol + "//" + window.location.host + "/internal"
form.method = "GET"
trigger(form, "submit")
// see post defined above
t.end()
})
tape("test attach form preventDefaulted events", function(t) {
var callbacked = false
var form = document.createElement("form")
attachForm.call({
options: {},
loadUrl: function() {
callbacked = true
}
}, form)
form.action = "#"
on(form, "submit", preventDefault)
trigger(form, "submit")
t.equal(callbacked, false, "events that are preventDefaulted should not fire callback")
t.end()
})
tape("test options are not modified by attachForm", function(t) {
var form = document.createElement("form")
var options = {foo: "bar"}
var loadUrl = function() {}
attachForm.call({options: options, loadUrl: loadUrl}, form)
form.action = window.location.protocol + "//" + window.location.host + window.location.pathname + window.location.search
form.method = "GET"
trigger(form, "submit")
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.end()
})

View File

@@ -56,3 +56,39 @@ 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()
})
tape("test options are not modified by attachLink", function(t) {
var a = document.createElement("a")
var options = {foo: "bar"}
var loadUrl = function() {}
attachLink.call({options: options, loadUrl: loadUrl}, a)
a.href = window.location.protocol + "//" + window.location.host + window.location.pathname + window.location.search
trigger(a, "click")
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.end()
})

View File

@@ -1,17 +1,21 @@
var tape = require("tape") var tape = require("tape")
var parseElement = require("../../../lib/proto/parse-element") var parseElement = require("../../../lib/proto/parse-element")
var protoMock = {attachLink: function() { return true}} var protoMock = {
attachLink: function() { return true },
attachForm: function() { return true }
}
tape("test parse element prototype method", function(t) { tape("test parse element prototype method", function(t) {
t.doesNotThrow(function() { t.doesNotThrow(function() {
var a = document.createElement("a") var a = document.createElement("a")
parseElement.call(protoMock, a) parseElement.call(protoMock, a)
}, "<a> element can be parsed") }, "<a> element can be parsed")
t.throws(function() { t.doesNotThrow(function() {
var form = document.createElement("form") var form = document.createElement("form")
parseElement.call(protoMock, form) parseElement.call(protoMock, form)
}, "<form> cannot be used (for now)") }, "<form> element can be parsed")
t.end() t.end()
}) })

View File

@@ -2,71 +2,48 @@ 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) {
// via http://stackoverflow.com/questions/1173549/how-to-determine-if-an-object-is-an-object-literal-in-javascript t.test("- default options", function(t) {
function isObjLiteral(_obj) { var pjax = {}
var _test = _obj; parseOptions.call(pjax, {})
return ( typeof _obj !== 'object' || _obj === null ?
false :
(
(function () {
while (!false) {
if ( Object.getPrototypeOf( _test = Object.getPrototypeOf(_test) ) === null) {
break;
}
}
return Object.getPrototypeOf(_obj) === _test;
})()
)
);
}
function enumerableKeys(_obj) {
var c = 0;
for(var n in _obj){ n = n; c++; }
return c;
}
t.test("- default options", function(t){
var body_1 = {};
var options_1 = {};
parseOptions.apply(body_1,[options_1]);
t.deepEqual(body_1.options.elements,"a[href], form[action]"); t.equal(pjax.options.elements, "a[href], form[action]")
t.deepEqual(body_1.options.selectors.length,2,"selectors length"); t.equal(pjax.options.selectors.length, 2, "selectors length")
t.deepEqual(body_1.options.selectors[0],"title"); t.equal(pjax.options.selectors[0], "title")
t.deepEqual(body_1.options.selectors[1],".js-Pjax"); t.equal(pjax.options.selectors[1], ".js-Pjax")
t.deepEqual(isObjLiteral(body_1.options.switches),true);
t.deepEqual(enumerableKeys(body_1.options.switches),2);//head and body
t.deepEqual(isObjLiteral(body_1.options.switchesOptions),true); t.equal(typeof pjax.options.switches, "object")
t.deepEqual(enumerableKeys(body_1.options.switchesOptions),0); t.equal(Object.keys(pjax.options.switches).length, 2)// head and body
t.deepEqual(body_1.options.history,true); t.equal(typeof pjax.options.switchesOptions, "object")
t.equal(Object.keys(pjax.options.switchesOptions).length, 0)
//TODO analytics is a little weird right now t.equal(pjax.options.history, true)
t.deepEqual(typeof body_1.options.analytics,"function"); t.equal(typeof pjax.options.analytics, "function")
t.equal(pjax.options.scrollTo, 0)
t.equal(pjax.options.scrollRestoration, true)
t.equal(pjax.options.cacheBust, true)
t.equal(pjax.options.debug, false)
t.equal(pjax.options.currentUrlFullReload, false)
t.end()
})
t.deepEqual(body_1.options.scrollTo,0); // verify analytics always ends up as a function even when passed not a function
t.deepEqual(body_1.options.debug,false); t.test("- analytics is a function", function(t) {
t.end(); var pjax = {}
}); parseOptions.call(pjax, {analytics: "some string"})
//verify analytics always ends up as a function even when passed not a function t.deepEqual(typeof pjax.options.analytics, "function")
t.test("- analytics is a function", function(t){ t.end()
var body_2 = {}; })
var options_2 = {analytics:"some string"};
parseOptions.apply(body_2,[options_2]);
t.deepEqual(typeof body_2.options.analytics,"function"); // verify that the value false for scrollTo is not squashed
t.end(); t.test("- scrollTo remains false", function(t) {
}); var pjax = {}
//verify that the value false for scrollTo is not squashed parseOptions.call(pjax, {scrollTo: false})
t.test("- scrollTo remains false", function(t){
var body_3 = {}; t.deepEqual(pjax.options.scrollTo, false)
var options_3 = {scrollTo:false}; t.end()
parseOptions.apply(body_3,[options_3]); })
t.deepEqual( body_3.options.scrollTo,false);
t.end();
});
t.end() t.end()
}) })

View File

@@ -1,16 +0,0 @@
var tape = require("tape")
var request = require("../../lib/request.js")
tape("test xhr request", function(t) {
request("https://api.github.com/", function(result) {
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()
})
})

50
tests/lib/send-request.js Normal file
View File

@@ -0,0 +1,50 @@
var tape = require("tape")
var sendRequest = require("../../lib/send-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) {
var url = "https://httpbin.org/get"
t.test("- request is made, gets a result, and is cache-busted", function(t) {
var requestCacheBust = sendRequest.bind({
options: {
cacheBust: true
}
})
var r = requestCacheBust(url, {}, function(result) {
t.equal(r.responseURL.indexOf("?"), url.length, "XHR URL is cache-busted when configured to be")
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 = sendRequest.bind({
options: {
cacheBust: false
}
})
var r = requestNoCacheBust(url, {}, function() {
t.equal(r.responseURL, url, "XHR URL is left untouched")
t.end()
})
})
t.end()
})

View File

@@ -1,9 +1,42 @@
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")
},
state: {}
}
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()
}) })

View File

@@ -0,0 +1,16 @@
var tape = require("tape")
var contains = require("../../../lib/util/contains.js")
tape("test contains function", function(t) {
var tempDoc = document.implementation.createHTMLDocument()
tempDoc.body.innerHTML = "<div><p id='el' class='js-Pjax'></p></div><span></span>"
var selectors = ["div"]
var el = tempDoc.body.querySelector("#el")
t.equal(contains(tempDoc, selectors, el), true, "contains() returns true when a selector contains the element")
selectors = ["span"]
t.equal(contains(tempDoc, selectors, el), false, "contains() returns false when the selectors do not contain the element")
t.end()
})

6
tests/setup.js Normal file
View File

@@ -0,0 +1,6 @@
var jsdomOptions = {
url: "https://example.org/",
runScripts: "dangerously"
}
require("jsdom-global")("", jsdomOptions)