| 1 |
/* InstantClick 3.1.0 | (C) 2014 Alexandre Dieulot | http://instantclick.io/license */ |
| 2 |
|
| 3 |
var InstantClick = function(document, location) { |
| 4 |
// Internal variables |
| 5 |
var $ua = navigator.userAgent, |
| 6 |
$isChromeForIOS = $ua.indexOf(' CriOS/') > -1, |
| 7 |
$hasTouch = 'createTouch' in document, |
| 8 |
$currentLocationWithoutHash, |
| 9 |
$urlToPreload, |
| 10 |
$preloadTimer, |
| 11 |
$lastTouchTimestamp, |
| 12 |
|
| 13 |
// Preloading-related variables |
| 14 |
$history = {}, |
| 15 |
$xhr, |
| 16 |
$url = false, |
| 17 |
$title = false, |
| 18 |
$mustRedirect = false, |
| 19 |
$body = false, |
| 20 |
$timing = {}, |
| 21 |
$isPreloading = false, |
| 22 |
$isWaitingForCompletion = false, |
| 23 |
$trackedAssets = [], |
| 24 |
|
| 25 |
// Variables defined by public functions |
| 26 |
$useWhitelist, |
| 27 |
$preloadOnMousedown, |
| 28 |
$delayBeforePreload, |
| 29 |
$eventsCallbacks = { |
| 30 |
fetch: [], |
| 31 |
receive: [], |
| 32 |
wait: [], |
| 33 |
change: [] |
| 34 |
} |
| 35 |
|
| 36 |
|
| 37 |
////////// HELPERS ////////// |
| 38 |
|
| 39 |
|
| 40 |
function removeHash(url) { |
| 41 |
var index = url.indexOf('#') |
| 42 |
if (index < 0) { |
| 43 |
return url |
| 44 |
} |
| 45 |
return url.substr(0, index) |
| 46 |
} |
| 47 |
|
| 48 |
function getLinkTarget(target) { |
| 49 |
while (target && target.nodeName != 'A') { |
| 50 |
target = target.parentNode |
| 51 |
} |
| 52 |
return target |
| 53 |
} |
| 54 |
|
| 55 |
function isBlacklisted(elem) { |
| 56 |
do { |
| 57 |
if (!elem.hasAttribute) { // Parent of <html> |
| 58 |
break |
| 59 |
} |
| 60 |
if (elem.hasAttribute('data-instant')) { |
| 61 |
return false |
| 62 |
} |
| 63 |
if (elem.hasAttribute('data-no-instant')) { |
| 64 |
return true |
| 65 |
} |
| 66 |
} |
| 67 |
while (elem = elem.parentNode); |
| 68 |
return false |
| 69 |
} |
| 70 |
|
| 71 |
function isWhitelisted(elem) { |
| 72 |
do { |
| 73 |
if (!elem.hasAttribute) { // Parent of <html> |
| 74 |
break |
| 75 |
} |
| 76 |
if (elem.hasAttribute('data-no-instant')) { |
| 77 |
return false |
| 78 |
} |
| 79 |
if (elem.hasAttribute('data-instant')) { |
| 80 |
return true |
| 81 |
} |
| 82 |
} |
| 83 |
while (elem = elem.parentNode); |
| 84 |
return false |
| 85 |
} |
| 86 |
|
| 87 |
function isPreloadable(a) { |
| 88 |
var domain = location.protocol + '//' + location.host |
| 89 |
|
| 90 |
if (a.target // target="_blank" etc. |
| 91 |
|| a.hasAttribute('download') |
| 92 |
|| a.href.indexOf(domain + '/') != 0 // Another domain, or no href attribute |
| 93 |
|| (a.href.indexOf('#') > -1 |
| 94 |
&& removeHash(a.href) == $currentLocationWithoutHash) // Anchor |
| 95 |
|| ($useWhitelist |
| 96 |
? !isWhitelisted(a) |
| 97 |
: isBlacklisted(a)) |
| 98 |
) { |
| 99 |
return false |
| 100 |
} |
| 101 |
return true |
| 102 |
} |
| 103 |
|
| 104 |
function triggerPageEvent(eventType, arg1, arg2, arg3) { |
| 105 |
var returnValue = false |
| 106 |
for (var i = 0; i < $eventsCallbacks[eventType].length; i++) { |
| 107 |
if (eventType == 'receive') { |
| 108 |
var altered = $eventsCallbacks[eventType][i](arg1, arg2, arg3) |
| 109 |
if (altered) { |
| 110 |
/* Update args for the next iteration of the loop. */ |
| 111 |
if ('body' in altered) { |
| 112 |
arg2 = altered.body |
| 113 |
} |
| 114 |
if ('title' in altered) { |
| 115 |
arg3 = altered.title |
| 116 |
} |
| 117 |
|
| 118 |
returnValue = altered |
| 119 |
} |
| 120 |
} |
| 121 |
else { |
| 122 |
$eventsCallbacks[eventType][i](arg1, arg2, arg3) |
| 123 |
} |
| 124 |
} |
| 125 |
return returnValue |
| 126 |
} |
| 127 |
|
| 128 |
function changePage(title, body, newUrl, scrollY) { |
| 129 |
document.documentElement.replaceChild(body, document.body) |
| 130 |
/* We cannot just use `document.body = doc.body`, it causes Safari (tested |
| 131 |
5.1, 6.0 and Mobile 7.0) to execute script tags directly. |
| 132 |
*/ |
| 133 |
|
| 134 |
if (newUrl) { |
| 135 |
history.pushState(null, null, newUrl) |
| 136 |
|
| 137 |
var hashIndex = newUrl.indexOf('#'), |
| 138 |
hashElem = hashIndex > -1 |
| 139 |
&& document.getElementById(newUrl.substr(hashIndex + 1)), |
| 140 |
offset = 0 |
| 141 |
|
| 142 |
if (hashElem) { |
| 143 |
while (hashElem.offsetParent) { |
| 144 |
offset += hashElem.offsetTop |
| 145 |
|
| 146 |
hashElem = hashElem.offsetParent |
| 147 |
} |
| 148 |
} |
| 149 |
scrollTo(0, offset) |
| 150 |
|
| 151 |
$currentLocationWithoutHash = removeHash(newUrl) |
| 152 |
} |
| 153 |
else { |
| 154 |
scrollTo(0, scrollY) |
| 155 |
} |
| 156 |
|
| 157 |
if ($isChromeForIOS && document.title == title) { |
| 158 |
/* Chrome for iOS: |
| 159 |
* |
| 160 |
* 1. Removes title on pushState, so the title needs to be set after. |
| 161 |
* |
| 162 |
* 2. Will not set the title if it’s identical when trimmed, so |
| 163 |
* appending a space won't do, but a non-breaking space works. |
| 164 |
*/ |
| 165 |
document.title = title + String.fromCharCode(160) |
| 166 |
} |
| 167 |
else { |
| 168 |
document.title = title |
| 169 |
} |
| 170 |
|
| 171 |
instantanize() |
| 172 |
bar.done() |
| 173 |
triggerPageEvent('change', false) |
| 174 |
|
| 175 |
// Real event, useful for combining userscripts, but only for that so it’s undocumented. |
| 176 |
var userscriptEvent = document.createEvent('HTMLEvents') |
| 177 |
userscriptEvent.initEvent('instantclick:newpage', true, true) |
| 178 |
dispatchEvent(userscriptEvent) |
| 179 |
} |
| 180 |
|
| 181 |
function setPreloadingAsHalted() { |
| 182 |
$isPreloading = false |
| 183 |
$isWaitingForCompletion = false |
| 184 |
} |
| 185 |
|
| 186 |
function removeNoscriptTags(html) { |
| 187 |
/* Must be done on text, not on a node's innerHTML, otherwise strange |
| 188 |
* things happen with implicitly closed elements (see the Noscript test). |
| 189 |
*/ |
| 190 |
return html.replace(/<noscript[\s\S]+<\/noscript>/gi, '') |
| 191 |
} |
| 192 |
|
| 193 |
|
| 194 |
////////// EVENT HANDLERS ////////// |
| 195 |
|
| 196 |
|
| 197 |
function mousedown(e) { |
| 198 |
if ($lastTouchTimestamp > (+new Date - 500)) { |
| 199 |
return // Otherwise, click doesn’t fire |
| 200 |
} |
| 201 |
|
| 202 |
var a = getLinkTarget(e.target) |
| 203 |
|
| 204 |
if (!a || !isPreloadable(a)) { |
| 205 |
return |
| 206 |
} |
| 207 |
|
| 208 |
preload(a.href) |
| 209 |
} |
| 210 |
|
| 211 |
function mouseover(e) { |
| 212 |
if ($lastTouchTimestamp > (+new Date - 500)) { |
| 213 |
return // Otherwise, click doesn’t fire |
| 214 |
} |
| 215 |
|
| 216 |
var a = getLinkTarget(e.target) |
| 217 |
|
| 218 |
if (!a || !isPreloadable(a)) { |
| 219 |
return |
| 220 |
} |
| 221 |
|
| 222 |
a.addEventListener('mouseout', mouseout) |
| 223 |
|
| 224 |
if (!$delayBeforePreload) { |
| 225 |
preload(a.href) |
| 226 |
} |
| 227 |
else { |
| 228 |
$urlToPreload = a.href |
| 229 |
$preloadTimer = setTimeout(preload, $delayBeforePreload) |
| 230 |
} |
| 231 |
} |
| 232 |
|
| 233 |
function touchstart(e) { |
| 234 |
$lastTouchTimestamp = +new Date |
| 235 |
|
| 236 |
var a = getLinkTarget(e.target) |
| 237 |
|
| 238 |
if (!a || !isPreloadable(a)) { |
| 239 |
return |
| 240 |
} |
| 241 |
|
| 242 |
if ($preloadOnMousedown) { |
| 243 |
a.removeEventListener('mousedown', mousedown) |
| 244 |
} |
| 245 |
else { |
| 246 |
a.removeEventListener('mouseover', mouseover) |
| 247 |
} |
| 248 |
preload(a.href) |
| 249 |
} |
| 250 |
|
| 251 |
function click(e) { |
| 252 |
var a = getLinkTarget(e.target) |
| 253 |
|
| 254 |
if (!a || !isPreloadable(a)) { |
| 255 |
return |
| 256 |
} |
| 257 |
|
| 258 |
if (e.which > 1 || e.metaKey || e.ctrlKey) { // Opening in new tab |
| 259 |
return |
| 260 |
} |
| 261 |
e.preventDefault() |
| 262 |
display(a.href) |
| 263 |
} |
| 264 |
|
| 265 |
function mouseout() { |
| 266 |
if ($preloadTimer) { |
| 267 |
clearTimeout($preloadTimer) |
| 268 |
$preloadTimer = false |
| 269 |
return |
| 270 |
} |
| 271 |
|
| 272 |
if (!$isPreloading || $isWaitingForCompletion) { |
| 273 |
return |
| 274 |
} |
| 275 |
$xhr.abort() |
| 276 |
setPreloadingAsHalted() |
| 277 |
} |
| 278 |
|
| 279 |
function readystatechange() { |
| 280 |
if ($xhr.readyState < 4) { |
| 281 |
return |
| 282 |
} |
| 283 |
if ($xhr.status == 0) { |
| 284 |
/* Request aborted */ |
| 285 |
return |
| 286 |
} |
| 287 |
|
| 288 |
$timing.ready = +new Date - $timing.start |
| 289 |
|
| 290 |
if ($xhr.getResponseHeader('Content-Type').match(/\/(x|ht|xht)ml/)) { |
| 291 |
var doc = document.implementation.createHTMLDocument('') |
| 292 |
doc.documentElement.innerHTML = removeNoscriptTags($xhr.responseText) |
| 293 |
$title = doc.title |
| 294 |
$body = doc.body |
| 295 |
|
| 296 |
var alteredOnReceive = triggerPageEvent('receive', $url, $body, $title) |
| 297 |
if (alteredOnReceive) { |
| 298 |
if ('body' in alteredOnReceive) { |
| 299 |
$body = alteredOnReceive.body |
| 300 |
} |
| 301 |
if ('title' in alteredOnReceive) { |
| 302 |
$title = alteredOnReceive.title |
| 303 |
} |
| 304 |
} |
| 305 |
|
| 306 |
var urlWithoutHash = removeHash($url) |
| 307 |
$history[urlWithoutHash] = { |
| 308 |
body: $body, |
| 309 |
title: $title, |
| 310 |
scrollY: urlWithoutHash in $history ? $history[urlWithoutHash].scrollY : 0 |
| 311 |
} |
| 312 |
|
| 313 |
var elems = doc.head.children, |
| 314 |
found = 0, |
| 315 |
elem, |
| 316 |
data |
| 317 |
|
| 318 |
for (var i = elems.length - 1; i >= 0; i--) { |
| 319 |
elem = elems[i] |
| 320 |
if (elem.hasAttribute('data-instant-track')) { |
| 321 |
data = elem.getAttribute('href') || elem.getAttribute('src') || elem.innerHTML |
| 322 |
for (var j = $trackedAssets.length - 1; j >= 0; j--) { |
| 323 |
if ($trackedAssets[j] == data) { |
| 324 |
found++ |
| 325 |
} |
| 326 |
} |
| 327 |
} |
| 328 |
} |
| 329 |
if (found != $trackedAssets.length) { |
| 330 |
$mustRedirect = true // Assets have changed |
| 331 |
} |
| 332 |
} |
| 333 |
else { |
| 334 |
$mustRedirect = true // Not an HTML document |
| 335 |
} |
| 336 |
|
| 337 |
if ($isWaitingForCompletion) { |
| 338 |
$isWaitingForCompletion = false |
| 339 |
display($url) |
| 340 |
} |
| 341 |
} |
| 342 |
|
| 343 |
|
| 344 |
////////// MAIN FUNCTIONS ////////// |
| 345 |
|
| 346 |
|
| 347 |
function instantanize(isInitializing) { |
| 348 |
document.body.addEventListener('touchstart', touchstart, true) |
| 349 |
if ($preloadOnMousedown) { |
| 350 |
document.body.addEventListener('mousedown', mousedown, true) |
| 351 |
} |
| 352 |
else { |
| 353 |
document.body.addEventListener('mouseover', mouseover, true) |
| 354 |
} |
| 355 |
document.body.addEventListener('click', click, true) |
| 356 |
|
| 357 |
if (!isInitializing) { |
| 358 |
var scripts = document.body.getElementsByTagName('script'), |
| 359 |
script, |
| 360 |
copy, |
| 361 |
parentNode, |
| 362 |
nextSibling |
| 363 |
|
| 364 |
for (i = 0, j = scripts.length; i < j; i++) { |
| 365 |
script = scripts[i] |
| 366 |
if (script.hasAttribute('data-no-instant')) { |
| 367 |
continue |
| 368 |
} |
| 369 |
copy = document.createElement('script') |
| 370 |
if (script.src) { |
| 371 |
copy.src = script.src |
| 372 |
} |
| 373 |
if (script.innerHTML) { |
| 374 |
copy.innerHTML = script.innerHTML |
| 375 |
} |
| 376 |
parentNode = script.parentNode |
| 377 |
nextSibling = script.nextSibling |
| 378 |
parentNode.removeChild(script) |
| 379 |
parentNode.insertBefore(copy, nextSibling) |
| 380 |
} |
| 381 |
} |
| 382 |
} |
| 383 |
|
| 384 |
function preload(url) { |
| 385 |
if (!$preloadOnMousedown |
| 386 |
&& 'display' in $timing |
| 387 |
&& +new Date - ($timing.start + $timing.display) < 100) { |
| 388 |
/* After a page is displayed, if the user's cursor happens to be above |
| 389 |
a link a mouseover event will be in most browsers triggered |
| 390 |
automatically, and in other browsers it will be triggered when the |
| 391 |
user moves his mouse by 1px. |
| 392 |
|
| 393 |
Here are the behavior I noticed, all on Windows: |
| 394 |
- Safari 5.1: auto-triggers after 0 ms |
| 395 |
- IE 11: auto-triggers after 30-80 ms (depends on page's size?) |
| 396 |
- Firefox: auto-triggers after 10 ms |
| 397 |
- Opera 18: auto-triggers after 10 ms |
| 398 |
|
| 399 |
- Chrome: triggers when cursor moved |
| 400 |
- Opera 12.16: triggers when cursor moved |
| 401 |
|
| 402 |
To remedy to this, we do not start preloading if last display |
| 403 |
occurred less than 100 ms ago. If they happen to click on the link, |
| 404 |
they will be redirected. |
| 405 |
*/ |
| 406 |
|
| 407 |
return |
| 408 |
} |
| 409 |
if ($preloadTimer) { |
| 410 |
clearTimeout($preloadTimer) |
| 411 |
$preloadTimer = false |
| 412 |
} |
| 413 |
|
| 414 |
if (!url) { |
| 415 |
url = $urlToPreload |
| 416 |
} |
| 417 |
|
| 418 |
if ($isPreloading && (url == $url || $isWaitingForCompletion)) { |
| 419 |
return |
| 420 |
} |
| 421 |
$isPreloading = true |
| 422 |
$isWaitingForCompletion = false |
| 423 |
|
| 424 |
$url = url |
| 425 |
$body = false |
| 426 |
$mustRedirect = false |
| 427 |
$timing = { |
| 428 |
start: +new Date |
| 429 |
} |
| 430 |
triggerPageEvent('fetch') |
| 431 |
$xhr.open('GET', url) |
| 432 |
$xhr.send() |
| 433 |
} |
| 434 |
|
| 435 |
function display(url) { |
| 436 |
if (!('display' in $timing)) { |
| 437 |
$timing.display = +new Date - $timing.start |
| 438 |
} |
| 439 |
if ($preloadTimer || !$isPreloading) { |
| 440 |
/* $preloadTimer: |
| 441 |
Happens when there’s a delay before preloading and that delay |
| 442 |
hasn't expired (preloading didn't kick in). |
| 443 |
|
| 444 |
!$isPreloading: |
| 445 |
A link has been clicked, and preloading hasn’t been initiated. |
| 446 |
It happens with touch devices when a user taps *near* the link, |
| 447 |
Safari/Chrome will trigger mousedown, mouseover, click (and others), |
| 448 |
but when that happens we ignore mousedown/mouseover (otherwise click |
| 449 |
doesn’t fire). Maybe there’s a way to make the click event fire, but |
| 450 |
that’s not worth it as mousedown/over happen just 1ms before click |
| 451 |
in this situation. |
| 452 |
|
| 453 |
It also happens when a user uses his keyboard to navigate (with Tab |
| 454 |
and Return), and possibly in other non-mainstream ways to navigate |
| 455 |
a website. |
| 456 |
*/ |
| 457 |
|
| 458 |
if ($preloadTimer && $url && $url != url) { |
| 459 |
/* Happens when the user clicks on a link before preloading |
| 460 |
kicks in while another link is already preloading. |
| 461 |
*/ |
| 462 |
|
| 463 |
location.href = url |
| 464 |
return |
| 465 |
} |
| 466 |
|
| 467 |
preload(url) |
| 468 |
bar.start(0, true) |
| 469 |
triggerPageEvent('wait') |
| 470 |
$isWaitingForCompletion = true // Must be set *after* calling `preload` |
| 471 |
return |
| 472 |
} |
| 473 |
if ($isWaitingForCompletion) { |
| 474 |
/* The user clicked on a link while a page was preloading. Either on |
| 475 |
the same link or on another link. If it's the same link something |
| 476 |
might have gone wrong (or he could have double clicked, we don’t |
| 477 |
handle that case), so we send him to the page without pjax. |
| 478 |
If it's another link, it hasn't been preloaded, so we redirect the |
| 479 |
user to it. |
| 480 |
*/ |
| 481 |
location.href = url |
| 482 |
return |
| 483 |
} |
| 484 |
if ($mustRedirect) { |
| 485 |
location.href = $url |
| 486 |
return |
| 487 |
} |
| 488 |
if (!$body) { |
| 489 |
bar.start(0, true) |
| 490 |
triggerPageEvent('wait') |
| 491 |
$isWaitingForCompletion = true |
| 492 |
return |
| 493 |
} |
| 494 |
$history[$currentLocationWithoutHash].scrollY = pageYOffset |
| 495 |
setPreloadingAsHalted() |
| 496 |
changePage($title, $body, $url) |
| 497 |
} |
| 498 |
|
| 499 |
|
| 500 |
////////// PROGRESS BAR FUNCTIONS ////////// |
| 501 |
|
| 502 |
|
| 503 |
var bar = function() { |
| 504 |
var $barContainer, |
| 505 |
$barElement, |
| 506 |
$barTransformProperty, |
| 507 |
$barProgress, |
| 508 |
$barTimer |
| 509 |
|
| 510 |
function init() { |
| 511 |
$barContainer = document.createElement('div') |
| 512 |
$barContainer.id = 'instantclick' |
| 513 |
$barElement = document.createElement('div') |
| 514 |
$barElement.id = 'instantclick-bar' |
| 515 |
$barElement.className = 'instantclick-bar' |
| 516 |
$barContainer.appendChild($barElement) |
| 517 |
|
| 518 |
var vendors = ['Webkit', 'Moz', 'O'] |
| 519 |
|
| 520 |
$barTransformProperty = 'transform' |
| 521 |
if (!($barTransformProperty in $barElement.style)) { |
| 522 |
for (var i = 0; i < 3; i++) { |
| 523 |
if (vendors[i] + 'Transform' in $barElement.style) { |
| 524 |
$barTransformProperty = vendors[i] + 'Transform' |
| 525 |
} |
| 526 |
} |
| 527 |
} |
| 528 |
|
| 529 |
var transitionProperty = 'transition' |
| 530 |
if (!(transitionProperty in $barElement.style)) { |
| 531 |
for (var i = 0; i < 3; i++) { |
| 532 |
if (vendors[i] + 'Transition' in $barElement.style) { |
| 533 |
transitionProperty = '-' + vendors[i].toLowerCase() + '-' + transitionProperty |
| 534 |
} |
| 535 |
} |
| 536 |
} |
| 537 |
|
| 538 |
var style = document.createElement('style') |
| 539 |
style.innerHTML = '#instantclick{position:' + ($hasTouch ? 'absolute' : 'fixed') + ';top:0;left:0;width:100%;pointer-events:none;z-index:2147483647;' + transitionProperty + ':opacity .25s .1s}' |
| 540 |
+ '.instantclick-bar{background:#29d;width:100%;margin-left:-100%;height:2px;' + transitionProperty + ':all .25s}' |
| 541 |
/* We set the bar's background in `.instantclick-bar` so that it can be |
| 542 |
overriden in CSS with `#instantclick-bar`, as IDs have higher priority. |
| 543 |
*/ |
| 544 |
document.head.appendChild(style) |
| 545 |
|
| 546 |
if ($hasTouch) { |
| 547 |
updatePositionAndScale() |
| 548 |
addEventListener('resize', updatePositionAndScale) |
| 549 |
addEventListener('scroll', updatePositionAndScale) |
| 550 |
} |
| 551 |
|
| 552 |
} |
| 553 |
|
| 554 |
function start(at, jump) { |
| 555 |
$barProgress = at |
| 556 |
if (document.getElementById($barContainer.id)) { |
| 557 |
document.body.removeChild($barContainer) |
| 558 |
} |
| 559 |
$barContainer.style.opacity = '1' |
| 560 |
if (document.getElementById($barContainer.id)) { |
| 561 |
document.body.removeChild($barContainer) |
| 562 |
/* So there's no CSS animation if already done once and it goes from 1 to 0 */ |
| 563 |
} |
| 564 |
update() |
| 565 |
if (jump) { |
| 566 |
setTimeout(jumpStart, 0) |
| 567 |
/* Must be done in a timer, otherwise the CSS animation doesn't happen. */ |
| 568 |
} |
| 569 |
clearTimeout($barTimer) |
| 570 |
$barTimer = setTimeout(inc, 500) |
| 571 |
} |
| 572 |
|
| 573 |
function jumpStart() { |
| 574 |
$barProgress = 10 |
| 575 |
update() |
| 576 |
} |
| 577 |
|
| 578 |
function inc() { |
| 579 |
$barProgress += 1 + (Math.random() * 2) |
| 580 |
if ($barProgress >= 98) { |
| 581 |
$barProgress = 98 |
| 582 |
} |
| 583 |
else { |
| 584 |
$barTimer = setTimeout(inc, 500) |
| 585 |
} |
| 586 |
update() |
| 587 |
} |
| 588 |
|
| 589 |
function update() { |
| 590 |
$barElement.style[$barTransformProperty] = 'translate(' + $barProgress + '%)' |
| 591 |
if (!document.getElementById($barContainer.id)) { |
| 592 |
document.body.appendChild($barContainer) |
| 593 |
} |
| 594 |
} |
| 595 |
|
| 596 |
function done() { |
| 597 |
if (document.getElementById($barContainer.id)) { |
| 598 |
clearTimeout($barTimer) |
| 599 |
$barProgress = 100 |
| 600 |
update() |
| 601 |
$barContainer.style.opacity = '0' |
| 602 |
/* If you're debugging, setting this to 0.5 is handy. */ |
| 603 |
return |
| 604 |
} |
| 605 |
|
| 606 |
/* The bar container hasn't been appended: It's a new page. */ |
| 607 |
start($barProgress == 100 ? 0 : $barProgress) |
| 608 |
/* $barProgress is 100 on popstate, usually. */ |
| 609 |
setTimeout(done, 0) |
| 610 |
/* Must be done in a timer, otherwise the CSS animation doesn't happen. */ |
| 611 |
} |
| 612 |
|
| 613 |
function updatePositionAndScale() { |
| 614 |
/* Adapted from code by Sam Stephenson and Mislav Marohnić |
| 615 |
http://signalvnoise.com/posts/2407 |
| 616 |
*/ |
| 617 |
|
| 618 |
$barContainer.style.left = pageXOffset + 'px' |
| 619 |
$barContainer.style.width = innerWidth + 'px' |
| 620 |
$barContainer.style.top = pageYOffset + 'px' |
| 621 |
|
| 622 |
var landscape = 'orientation' in window && Math.abs(orientation) == 90, |
| 623 |
scaleY = innerWidth / screen[landscape ? 'height' : 'width'] * 2 |
| 624 |
/* We multiply the size by 2 because the progress bar is harder |
| 625 |
to notice on a mobile device. |
| 626 |
*/ |
| 627 |
$barContainer.style[$barTransformProperty] = 'scaleY(' + scaleY + ')' |
| 628 |
} |
| 629 |
|
| 630 |
return { |
| 631 |
init: init, |
| 632 |
start: start, |
| 633 |
done: done |
| 634 |
} |
| 635 |
}() |
| 636 |
|
| 637 |
|
| 638 |
////////// PUBLIC VARIABLE AND FUNCTIONS ////////// |
| 639 |
|
| 640 |
var supported = 'pushState' in history |
| 641 |
&& (!$ua.match('Android') || $ua.match('Chrome/')) |
| 642 |
&& location.protocol != "file:" |
| 643 |
|
| 644 |
/* The state of Android's AOSP browsers: |
| 645 |
|
| 646 |
2.3.7: pushState appears to work correctly, but |
| 647 |
`doc.documentElement.innerHTML = body` is buggy. |
| 648 |
See details here: http://stackoverflow.com/q/21918564 |
| 649 |
Not an issue anymore, but it may fail where 3.0 do, this needs |
| 650 |
testing again. |
| 651 |
|
| 652 |
3.0: pushState appears to work correctly (though the URL bar is only |
| 653 |
updated on focus), but |
| 654 |
`document.documentElement.replaceChild(doc.body, document.body)` |
| 655 |
throws DOMException: WRONG_DOCUMENT_ERR. |
| 656 |
|
| 657 |
4.0.2: Doesn't support pushState. |
| 658 |
|
| 659 |
4.0.4, |
| 660 |
4.1.1, |
| 661 |
4.2, |
| 662 |
4.3: pushState is here, but it doesn't update the URL bar. |
| 663 |
(Great logic there.) |
| 664 |
|
| 665 |
4.4: Works correctly. Claims to be 'Chrome/30.0.0.0'. |
| 666 |
|
| 667 |
All androids tested with Android SDK's Emulator. |
| 668 |
Version numbers are from the browser's user agent. |
| 669 |
|
| 670 |
Because of this mess, the only whitelisted browser on Android is Chrome. |
| 671 |
*/ |
| 672 |
|
| 673 |
function init() { |
| 674 |
if ($currentLocationWithoutHash) { |
| 675 |
/* Already initialized */ |
| 676 |
return |
| 677 |
} |
| 678 |
if (!supported) { |
| 679 |
triggerPageEvent('change', true) |
| 680 |
return |
| 681 |
} |
| 682 |
for (var i = arguments.length - 1; i >= 0; i--) { |
| 683 |
var arg = arguments[i] |
| 684 |
if (arg === true) { |
| 685 |
$useWhitelist = true |
| 686 |
} |
| 687 |
else if (arg == 'mousedown') { |
| 688 |
$preloadOnMousedown = true |
| 689 |
} |
| 690 |
else if (typeof arg == 'number') { |
| 691 |
$delayBeforePreload = arg |
| 692 |
} |
| 693 |
} |
| 694 |
$currentLocationWithoutHash = removeHash(location.href) |
| 695 |
$history[$currentLocationWithoutHash] = { |
| 696 |
body: document.body, |
| 697 |
title: document.title, |
| 698 |
scrollY: pageYOffset |
| 699 |
} |
| 700 |
|
| 701 |
var elems = document.head.children, |
| 702 |
elem, |
| 703 |
data |
| 704 |
for (var i = elems.length - 1; i >= 0; i--) { |
| 705 |
elem = elems[i] |
| 706 |
if (elem.hasAttribute('data-instant-track')) { |
| 707 |
data = elem.getAttribute('href') || elem.getAttribute('src') || elem.innerHTML |
| 708 |
/* We can't use just `elem.href` and `elem.src` because we can't |
| 709 |
retrieve `href`s and `src`s from the Ajax response. |
| 710 |
*/ |
| 711 |
$trackedAssets.push(data) |
| 712 |
} |
| 713 |
} |
| 714 |
|
| 715 |
$xhr = new XMLHttpRequest() |
| 716 |
$xhr.addEventListener('readystatechange', readystatechange) |
| 717 |
|
| 718 |
instantanize(true) |
| 719 |
|
| 720 |
bar.init() |
| 721 |
|
| 722 |
triggerPageEvent('change', true) |
| 723 |
|
| 724 |
addEventListener('popstate', function() { |
| 725 |
var loc = removeHash(location.href) |
| 726 |
if (loc == $currentLocationWithoutHash) { |
| 727 |
return |
| 728 |
} |
| 729 |
|
| 730 |
if (!(loc in $history)) { |
| 731 |
location.href = location.href |
| 732 |
/* Reloads the page while using cache for scripts, styles and images, |
| 733 |
unlike `location.reload()` */ |
| 734 |
return |
| 735 |
} |
| 736 |
|
| 737 |
$history[$currentLocationWithoutHash].scrollY = pageYOffset |
| 738 |
$currentLocationWithoutHash = loc |
| 739 |
changePage($history[loc].title, $history[loc].body, false, $history[loc].scrollY) |
| 740 |
}) |
| 741 |
} |
| 742 |
|
| 743 |
function on(eventType, callback) { |
| 744 |
$eventsCallbacks[eventType].push(callback) |
| 745 |
} |
| 746 |
|
| 747 |
|
| 748 |
//////////////////// |
| 749 |
|
| 750 |
|
| 751 |
return { |
| 752 |
supported: supported, |
| 753 |
init: init, |
| 754 |
on: on |
| 755 |
} |
| 756 |
|
| 757 |
}(document, location); |