123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483 |
- /* ========================================================================
- * Ratchet: push.js v2.0.2
- * http://goratchet.com/components#push
- * ========================================================================
- * inspired by @defunkt's jquery.pjax.js
- * Copyright 2014 Connor Sears
- * Licensed under MIT (https://github.com/twbs/ratchet/blob/master/LICENSE)
- * ======================================================================== */
- /* global _gaq: true */
- !(function () {
- 'use strict';
- var noop = function () {};
- // Pushstate caching
- // ==================
- var isScrolling;
- var maxCacheLength = 20;
- var cacheMapping = sessionStorage;
- var domCache = {};
- var transitionMap = {
- slideIn : 'slide-out',
- slideOut : 'slide-in',
- fade : 'fade'
- };
- var bars = {
- bartab : '.bar-tab',
- barnav : '.bar-nav',
- barfooter : '.bar-footer',
- barheadersecondary : '.bar-header-secondary'
- };
- var cacheReplace = function (data, updates) {
- PUSH.id = data.id;
- if (updates) {
- data = getCached(data.id);
- }
- cacheMapping[data.id] = JSON.stringify(data);
- window.history.replaceState(data.id, data.title, data.url);
- domCache[data.id] = document.body.cloneNode(true);
- };
- var cachePush = function () {
- var id = PUSH.id;
- var cacheForwardStack = JSON.parse(cacheMapping.cacheForwardStack || '[]');
- var cacheBackStack = JSON.parse(cacheMapping.cacheBackStack || '[]');
- cacheBackStack.push(id);
- while (cacheForwardStack.length) {
- delete cacheMapping[cacheForwardStack.shift()];
- }
- while (cacheBackStack.length > maxCacheLength) {
- delete cacheMapping[cacheBackStack.shift()];
- }
- window.history.pushState(null, '', cacheMapping[PUSH.id].url);
- cacheMapping.cacheForwardStack = JSON.stringify(cacheForwardStack);
- cacheMapping.cacheBackStack = JSON.stringify(cacheBackStack);
- };
- var cachePop = function (id, direction) {
- var forward = direction === 'forward';
- var cacheForwardStack = JSON.parse(cacheMapping.cacheForwardStack || '[]');
- var cacheBackStack = JSON.parse(cacheMapping.cacheBackStack || '[]');
- var pushStack = forward ? cacheBackStack : cacheForwardStack;
- var popStack = forward ? cacheForwardStack : cacheBackStack;
- if (PUSH.id) {
- pushStack.push(PUSH.id);
- }
- popStack.pop();
- cacheMapping.cacheForwardStack = JSON.stringify(cacheForwardStack);
- cacheMapping.cacheBackStack = JSON.stringify(cacheBackStack);
- };
- var getCached = function (id) {
- return JSON.parse(cacheMapping[id] || null) || {};
- };
- var getTarget = function (e) {
- var target = findTarget(e.target);
- if (!target ||
- e.which > 1 ||
- e.metaKey ||
- e.ctrlKey ||
- isScrolling ||
- location.protocol !== target.protocol ||
- location.host !== target.host ||
- !target.hash && /#/.test(target.href) ||
- target.hash && target.href.replace(target.hash, '') === location.href.replace(location.hash, '') ||
- target.getAttribute('data-ignore') === 'push') { return; }
- return target;
- };
- // Main event handlers (touchend, popstate)
- // ==========================================
- var touchend = function (e) {
- var target = getTarget(e);
- if (!target) {
- return;
- }
- e.preventDefault();
- PUSH({
- url : target.href,
- hash : target.hash,
- timeout : target.getAttribute('data-timeout'),
- transition : target.getAttribute('data-transition')
- });
- };
- var popstate = function (e) {
- var key;
- var barElement;
- var activeObj;
- var activeDom;
- var direction;
- var transition;
- var transitionFrom;
- var transitionFromObj;
- var id = e.state;
- if (!id || !cacheMapping[id]) {
- return;
- }
- direction = PUSH.id < id ? 'forward' : 'back';
- cachePop(id, direction);
- activeObj = getCached(id);
- activeDom = domCache[id];
- if (activeObj.title) {
- document.title = activeObj.title;
- }
- if (direction === 'back') {
- transitionFrom = JSON.parse(direction === 'back' ? cacheMapping.cacheForwardStack : cacheMapping.cacheBackStack);
- transitionFromObj = getCached(transitionFrom[transitionFrom.length - 1]);
- } else {
- transitionFromObj = activeObj;
- }
- if (direction === 'back' && !transitionFromObj.id) {
- return (PUSH.id = id);
- }
- transition = direction === 'back' ? transitionMap[transitionFromObj.transition] : transitionFromObj.transition;
- if (!activeDom) {
- return PUSH({
- id : activeObj.id,
- url : activeObj.url,
- title : activeObj.title,
- timeout : activeObj.timeout,
- transition : transition,
- ignorePush : true
- });
- }
- if (transitionFromObj.transition) {
- activeObj = extendWithDom(activeObj, '.content', activeDom.cloneNode(true));
- for (key in bars) {
- if (bars.hasOwnProperty(key)) {
- barElement = document.querySelector(bars[key]);
- if (activeObj[key]) {
- swapContent(activeObj[key], barElement);
- } else if (barElement) {
- barElement.parentNode.removeChild(barElement);
- }
- }
- }
- }
- swapContent(
- (activeObj.contents || activeDom).cloneNode(true),
- document.querySelector('.content'),
- transition
- );
- PUSH.id = id;
- document.body.offsetHeight; // force reflow to prevent scroll
- };
- // Core PUSH functionality
- // =======================
- var PUSH = function (options) {
- var key;
- var xhr = PUSH.xhr;
- options.container = options.container || options.transition ? document.querySelector('.content') : document.body;
- for (key in bars) {
- if (bars.hasOwnProperty(key)) {
- options[key] = options[key] || document.querySelector(bars[key]);
- }
- }
- if (xhr && xhr.readyState < 4) {
- xhr.onreadystatechange = noop;
- xhr.abort();
- }
- xhr = new XMLHttpRequest();
- xhr.open('GET', options.url, true);
- xhr.setRequestHeader('X-PUSH', 'true');
- xhr.onreadystatechange = function () {
- if (options._timeout) {
- clearTimeout(options._timeout);
- }
- if (xhr.readyState === 4) {
- xhr.status === 200 ? success(xhr, options) : failure(options.url);
- }
- };
- if (!PUSH.id) {
- cacheReplace({
- id : +new Date(),
- url : window.location.href,
- title : document.title,
- timeout : options.timeout,
- transition : null
- });
- }
- if (options.timeout) {
- options._timeout = setTimeout(function () { xhr.abort('timeout'); }, options.timeout);
- }
- xhr.send();
- if (xhr.readyState && !options.ignorePush) {
- cachePush();
- }
- };
- // Main XHR handlers
- // =================
- var success = function (xhr, options) {
- var key;
- var barElement;
- var data = parseXHR(xhr, options);
- if (!data.contents) {
- return locationReplace(options.url);
- }
- if (data.title) {
- document.title = data.title;
- }
- if (options.transition) {
- for (key in bars) {
- if (bars.hasOwnProperty(key)) {
- barElement = document.querySelector(bars[key]);
- if (data[key]) {
- swapContent(data[key], barElement);
- } else if (barElement) {
- barElement.parentNode.removeChild(barElement);
- }
- }
- }
- }
- swapContent(data.contents, options.container, options.transition, function () {
- cacheReplace({
- id : options.id || +new Date(),
- url : data.url,
- title : data.title,
- timeout : options.timeout,
- transition : options.transition
- }, options.id);
- triggerStateChange();
- });
- if (!options.ignorePush && window._gaq) {
- _gaq.push(['_trackPageview']); // google analytics
- }
- if (!options.hash) {
- return;
- }
- };
- var failure = function (url) {
- throw new Error('Could not get: ' + url);
- };
- // PUSH helpers
- // ============
- var swapContent = function (swap, container, transition, complete) {
- var enter;
- var containerDirection;
- var swapDirection;
- if (!transition) {
- if (container) {
- container.innerHTML = swap.innerHTML;
- } else if (swap.classList.contains('content')) {
- document.body.appendChild(swap);
- } else {
- document.body.insertBefore(swap, document.querySelector('.content'));
- }
- } else {
- enter = /in$/.test(transition);
- if (transition === 'fade') {
- container.classList.add('in');
- container.classList.add('fade');
- swap.classList.add('fade');
- }
- if (/slide/.test(transition)) {
- swap.classList.add('sliding-in', enter ? 'right' : 'left');
- swap.classList.add('sliding');
- container.classList.add('sliding');
- }
- container.parentNode.insertBefore(swap, container);
- }
- if (!transition) {
- complete && complete();
- }
- if (transition === 'fade') {
- container.offsetWidth; // force reflow
- container.classList.remove('in');
- var fadeContainerEnd = function () {
- container.removeEventListener('webkitTransitionEnd', fadeContainerEnd);
- swap.classList.add('in');
- swap.addEventListener('webkitTransitionEnd', fadeSwapEnd);
- };
- var fadeSwapEnd = function () {
- swap.removeEventListener('webkitTransitionEnd', fadeSwapEnd);
- container.parentNode.removeChild(container);
- swap.classList.remove('fade');
- swap.classList.remove('in');
- complete && complete();
- };
- container.addEventListener('webkitTransitionEnd', fadeContainerEnd);
- }
- if (/slide/.test(transition)) {
- var slideEnd = function () {
- swap.removeEventListener('webkitTransitionEnd', slideEnd);
- swap.classList.remove('sliding', 'sliding-in');
- swap.classList.remove(swapDirection);
- container.parentNode.removeChild(container);
- complete && complete();
- };
- container.offsetWidth; // force reflow
- swapDirection = enter ? 'right' : 'left';
- containerDirection = enter ? 'left' : 'right';
- container.classList.add(containerDirection);
- swap.classList.remove(swapDirection);
- swap.addEventListener('webkitTransitionEnd', slideEnd);
- }
- };
- var triggerStateChange = function () {
- var e = new CustomEvent('push', {
- detail: { state: getCached(PUSH.id) },
- bubbles: true,
- cancelable: true
- });
- window.dispatchEvent(e);
- };
- var findTarget = function (target) {
- var i;
- var toggles = document.querySelectorAll('a');
- for (; target && target !== document; target = target.parentNode) {
- for (i = toggles.length; i--;) {
- if (toggles[i] === target) {
- return target;
- }
- }
- }
- };
- var locationReplace = function (url) {
- window.history.replaceState(null, '', '#');
- window.location.replace(url);
- };
- var extendWithDom = function (obj, fragment, dom) {
- var i;
- var result = {};
- for (i in obj) {
- if (obj.hasOwnProperty(i)) {
- result[i] = obj[i];
- }
- }
- Object.keys(bars).forEach(function (key) {
- var el = dom.querySelector(bars[key]);
- if (el) {
- el.parentNode.removeChild(el);
- }
- result[key] = el;
- });
- result.contents = dom.querySelector(fragment);
- return result;
- };
- var parseXHR = function (xhr, options) {
- var head;
- var body;
- var data = {};
- var responseText = xhr.responseText;
- data.url = options.url;
- if (!responseText) {
- return data;
- }
- if (/<html/i.test(responseText)) {
- head = document.createElement('div');
- body = document.createElement('div');
- head.innerHTML = responseText.match(/<head[^>]*>([\s\S.]*)<\/head>/i)[0];
- body.innerHTML = responseText.match(/<body[^>]*>([\s\S.]*)<\/body>/i)[0];
- } else {
- head = body = document.createElement('div');
- head.innerHTML = responseText;
- }
- data.title = head.querySelector('title');
- var text = 'innerText' in data.title ? 'innerText' : 'textContent';
- data.title = data.title && data.title[text].trim();
- if (options.transition) {
- data = extendWithDom(data, '.content', body);
- } else {
- data.contents = body;
- }
- return data;
- };
- // Attach PUSH event handlers
- // ==========================
- window.addEventListener('touchstart', function () { isScrolling = false; });
- window.addEventListener('touchmove', function () { isScrolling = true; });
- window.addEventListener('touchend', touchend);
- window.addEventListener('click', function (e) { if (getTarget(e)) {e.preventDefault();} });
- window.addEventListener('popstate', popstate);
- window.PUSH = PUSH;
- }());
|