Comparing version 1.6.1 to 1.7.0
137
dist/swup.js
@@ -250,15 +250,23 @@ (function webpackUniversalModuleDefinition(root, factory) { | ||
var _updateTransition = __webpack_require__(18); | ||
var _on = __webpack_require__(18); | ||
var _on2 = _interopRequireDefault(_on); | ||
var _off = __webpack_require__(19); | ||
var _off2 = _interopRequireDefault(_off); | ||
var _updateTransition = __webpack_require__(20); | ||
var _updateTransition2 = _interopRequireDefault(_updateTransition); | ||
var _preloadPages = __webpack_require__(19); | ||
var _preloadPages = __webpack_require__(21); | ||
var _preloadPages2 = _interopRequireDefault(_preloadPages); | ||
var _usePlugin = __webpack_require__(20); | ||
var _usePlugin = __webpack_require__(22); | ||
var _usePlugin2 = _interopRequireDefault(_usePlugin); | ||
var _log = __webpack_require__(21); | ||
var _log = __webpack_require__(23); | ||
@@ -314,2 +322,27 @@ var _log2 = _interopRequireDefault(_log); | ||
*/ | ||
this._handlers = { | ||
willReplaceContent: [], | ||
contentReplaced: [], | ||
pageView: [], | ||
hoverLink: [], | ||
clickLink: [], | ||
samePageWithHash: [], | ||
animationOutStart: [], | ||
animationOutDone: [], | ||
animationSkipped: [], | ||
pagePreloaded: [], | ||
pageLoaded: [], | ||
scrollStart: [], | ||
scrollDone: [], | ||
animationInStart: [], | ||
animationInDone: [], | ||
pageRetrievedFromCache: [], | ||
submitForm: [], | ||
enabled: [], | ||
disabled: [] | ||
}; | ||
/** | ||
* helper variables | ||
*/ | ||
// id of element to scroll to after render | ||
@@ -341,2 +374,4 @@ this.scrollToElement = null; | ||
this.markSwupElements = _markSwupElements2.default; | ||
this.on = _on2.default; | ||
this.off = _off2.default; | ||
this.updateTransition = _updateTransition2.default; | ||
@@ -480,2 +515,5 @@ this.preloadPages = _preloadPages2.default; | ||
// remove handlers | ||
this.off(); | ||
this.triggerEvent('disabled'); | ||
@@ -489,3 +527,3 @@ document.documentElement.classList.remove('swup-enabled'); | ||
if (!event.metaKey) { | ||
this.triggerEvent('clickLink'); | ||
this.triggerEvent('clickLink', event); | ||
var link = new _Link2.default(); | ||
@@ -499,3 +537,3 @@ event.preventDefault(); | ||
// link to the same URL with hash | ||
this.triggerEvent('samePageWithHash'); | ||
this.triggerEvent('samePageWithHash', event); | ||
var element = document.querySelector(link.getHash()); | ||
@@ -519,3 +557,3 @@ if (element != null) { | ||
// link to the same URL without hash | ||
this.triggerEvent('samePage'); | ||
this.triggerEvent('samePage', event); | ||
if (this.options.scroll) { | ||
@@ -539,3 +577,3 @@ this.scrollTo(document.body, 0, 1); | ||
// open in new tab (do nothing) | ||
this.triggerEvent('openPageInNewTab'); | ||
this.triggerEvent('openPageInNewTab', event); | ||
} | ||
@@ -548,3 +586,3 @@ } | ||
this.triggerEvent('hoverLink'); | ||
this.triggerEvent('hoverLink', event); | ||
if (this.options.preload) { | ||
@@ -557,3 +595,3 @@ var link = new _Link2.default(); | ||
if (request.status === 500) { | ||
_this2.triggerEvent('serverError'); | ||
_this2.triggerEvent('serverError', event); | ||
reject(link.getAddress()); | ||
@@ -567,3 +605,3 @@ return; | ||
_this2.cache.cacheUrl(page, _this2.options.debugMode); | ||
_this2.triggerEvent('pagePreloaded'); | ||
_this2.triggerEvent('pagePreloaded', event); | ||
} else { | ||
@@ -587,3 +625,3 @@ reject(link.getAddress()); | ||
if (!event.metaKey) { | ||
this.triggerEvent('submitForm'); | ||
this.triggerEvent('submitForm', event); | ||
event.preventDefault(); | ||
@@ -643,3 +681,3 @@ var form = event.target; | ||
} else { | ||
this.triggerEvent('openFormSubmitInNewTab'); | ||
this.triggerEvent('openFormSubmitInNewTab', event); | ||
} | ||
@@ -658,3 +696,3 @@ } | ||
} | ||
this.triggerEvent('popState'); | ||
this.triggerEvent('popState', event); | ||
this.loadPage({ url: link.getAddress() }, event); | ||
@@ -1231,6 +1269,17 @@ } | ||
module.exports = function (eventName) { | ||
if (this.options.debugMode) { | ||
module.exports = function (eventName, originalEvent) { | ||
if (this.options.debugMode && originalEvent) { | ||
console.groupCollapsed('%cswup:' + '%c' + eventName, 'color: #343434', 'color: #009ACD'); | ||
console.log(originalEvent); | ||
console.groupEnd(); | ||
} else if (this.options.debugMode) { | ||
console.log('%cswup:' + '%c' + eventName, 'color: #343434', 'color: #009ACD'); | ||
} | ||
// call saved handlers with "on" method and pass originalEvent object if available | ||
this._handlers[eventName].forEach(function (handler) { | ||
return handler(originalEvent); | ||
}); | ||
// trigger event on document with prefix "swup:" | ||
var event = new CustomEvent('swup:' + eventName, { detail: eventName }); | ||
@@ -1415,2 +1464,52 @@ document.dispatchEvent(event); | ||
module.exports = function on(event, handler) { | ||
if (this._handlers[event]) { | ||
this._handlers[event].push(handler); | ||
} else { | ||
console.warn("Unsupported event " + event + "."); | ||
} | ||
}; | ||
/***/ }), | ||
/* 19 */ | ||
/***/ (function(module, exports, __webpack_require__) { | ||
"use strict"; | ||
module.exports = function off(event, handler) { | ||
var _this = this; | ||
if (event != null) { | ||
if (handler != null) { | ||
if (this._handlers[event] && this._handlers[event].filter(function (savedHandler) { | ||
return savedHandler === handler; | ||
}).length) { | ||
var toRemove = this._handlers[event].filter(function (savedHandler) { | ||
return savedHandler === handler; | ||
})[0]; | ||
var index = this._handlers[event].indexOf(toRemove); | ||
if (index > -1) { | ||
this._handlers[event].splice(index, 1); | ||
} | ||
} else { | ||
console.warn("Handler for event '" + event + "' no found."); | ||
} | ||
} else { | ||
this._handlers[event] = []; | ||
} | ||
} else { | ||
Object.keys(this._handlers).forEach(function (keys) { | ||
_this._handlers[keys] = []; | ||
}); | ||
} | ||
}; | ||
/***/ }), | ||
/* 20 */ | ||
/***/ (function(module, exports, __webpack_require__) { | ||
"use strict"; | ||
module.exports = function (from, to, custom) { | ||
@@ -1438,3 +1537,3 @@ | ||
/***/ }), | ||
/* 19 */ | ||
/* 21 */ | ||
/***/ (function(module, exports, __webpack_require__) { | ||
@@ -1483,3 +1582,3 @@ | ||
/***/ }), | ||
/* 20 */ | ||
/* 22 */ | ||
/***/ (function(module, exports, __webpack_require__) { | ||
@@ -1510,3 +1609,3 @@ | ||
/***/ }), | ||
/* 21 */ | ||
/* 23 */ | ||
/***/ (function(module, exports, __webpack_require__) { | ||
@@ -1513,0 +1612,0 @@ |
@@ -1,1 +0,1 @@ | ||
(function e(t,n){if(typeof exports==="object"&&typeof module==="object")module.exports=n();else if(typeof define==="function"&&define.amd)define([],n);else if(typeof exports==="object")exports["Swup"]=n();else t["Swup"]=n()})(window,function(){return function(e){var t={};function n(o){if(t[o]){return t[o].exports}var r=t[o]={i:o,l:false,exports:{}};e[o].call(r.exports,r,r.exports,n);r.l=true;return r.exports}n.m=e;n.c=t;n.d=function(e,t,o){if(!n.o(e,t)){Object.defineProperty(e,t,{enumerable:true,get:o})}};n.r=function(e){if(typeof Symbol!=="undefined"&&Symbol.toStringTag){Object.defineProperty(e,Symbol.toStringTag,{value:"Module"})}Object.defineProperty(e,"__esModule",{value:true})};n.t=function(e,t){if(t&1)e=n(e);if(t&8)return e;if(t&4&&typeof e==="object"&&e&&e.__esModule)return e;var o=Object.create(null);n.r(o);Object.defineProperty(o,"default",{enumerable:true,value:e});if(t&2&&typeof e!="string")for(var r in e)n.d(o,r,function(t){return e[t]}.bind(null,r));return o};n.n=function(e){var t=e&&e.__esModule?function t(){return e["default"]}:function t(){return e};n.d(t,"a",t);return t};n.o=function(e,t){return Object.prototype.hasOwnProperty.call(e,t)};n.p="";return n(n.s=1)}([function(e,t,n){"use strict";Object.defineProperty(t,"__esModule",{value:true});var o=function(){function e(e,t){for(var n=0;n<t.length;n++){var o=t[n];o.enumerable=o.enumerable||false;o.configurable=true;if("value"in o)o.writable=true;Object.defineProperty(e,o.key,o)}}return function(t,n,o){if(n)e(t.prototype,n);if(o)e(t,o);return t}}();function r(e,t){if(!(e instanceof t)){throw new TypeError("Cannot call a class as a function")}}var i=function(){function e(){r(this,e);this.link=document.createElement("a")}o(e,[{key:"setPath",value:function e(t){this.link.href=t}},{key:"getPath",value:function e(){var t=this.link.pathname;if(t[0]!="/"){t="/"+t}return t}},{key:"getAddress",value:function e(){var t=this.link.pathname+this.link.search;if(t[0]!="/"){t="/"+t}return t}},{key:"getHash",value:function e(){return this.link.hash}}]);return e}();t.default=i},function(e,t,n){"use strict";var o=n(2);var r=i(o);function i(e){return e&&e.__esModule?e:{default:e}}e.exports=r.default},function(e,t,n){"use strict";Object.defineProperty(t,"__esModule",{value:true});var o=Object.assign||function(e){for(var t=1;t<arguments.length;t++){var n=arguments[t];for(var o in n){if(Object.prototype.hasOwnProperty.call(n,o)){e[o]=n[o]}}}return e};var r=function(){function e(e,t){for(var n=0;n<t.length;n++){var o=t[n];o.enumerable=o.enumerable||false;o.configurable=true;if("value"in o)o.writable=true;Object.defineProperty(e,o.key,o)}}return function(t,n,o){if(n)e(t.prototype,n);if(o)e(t,o);return t}}();var i=n(3);var s=K(i);var a=n(5);var l=K(a);var u=n(0);var c=K(u);var d=n(6);var f=K(d);var h=n(7);var p=K(h);var g=n(8);var m=K(g);var v=n(9);var w=K(v);var y=n(10);var E=K(y);var b=n(11);var P=K(b);var S=n(12);var T=K(S);var k=n(13);var L=K(k);var x=n(14);var A=K(x);var H=n(15);var M=K(H);var C=n(16);var O=K(C);var R=n(17);var _=K(R);var j=n(18);var q=K(j);var U=n(19);var D=K(U);var F=n(20);var B=K(F);var I=n(21);var N=K(I);function K(e){return e&&e.__esModule?e:{default:e}}function W(e,t){if(!(e instanceof t)){throw new TypeError("Cannot call a class as a function")}}var z=function(){function e(t){W(this,e);var n={cache:true,animationSelector:'[class*="transition-"]',elements:["#swup"],pageClassPrefix:"",debugMode:false,scroll:true,doScrollingRightAway:false,animateScroll:true,scrollFriction:.3,scrollAcceleration:.04,preload:true,support:true,plugins:[],skipPopStateHandling:function e(t){if(t.state&&t.state.source=="swup"){return false}return true},animateHistoryBrowsing:false,LINK_SELECTOR:'a[href^="'+window.location.origin+'"]:not([data-no-swup]), a[href^="/"]:not([data-no-swup]), a[href^="#"]:not([data-no-swup])',FORM_SELECTOR:"form[data-swup-form]"};this.transition={};var r=o({},n,t);this.scrollToElement=null;this.preloadPromise=null;this.options=r;this.plugins=[];this.getUrl=L.default;this.cache=new l.default;this.link=new c.default;this.transitionEndEvent=(0,f.default)();this.getDataFromHtml=m.default;this.getPage=p.default;this.scrollTo=A.default;this.loadPage=w.default;this.renderPage=E.default;this.createState=P.default;this.triggerEvent=T.default;this.classify=M.default;this.doScrolling=O.default;this.markSwupElements=_.default;this.updateTransition=q.default;this.preloadPages=D.default;this.usePlugin=B.default;this.log=N.default;this.enable=this.enable;this.destroy=this.destroy;if(this.options.debugMode){window.swup=this}this.getUrl();this.enable()}r(e,[{key:"enable",value:function e(){var t=this;if(this.options.support){if(!("pushState"in window.history)){console.warn("pushState is not supported");return}if((0,f.default)()){this.transitionEndEvent=(0,f.default)()}else{console.warn("transitionEnd detection is not supported");return}if(typeof Promise==="undefined"||Promise.toString().indexOf("[native code]")===-1){console.warn("Promise is not supported");return}}this.delegatedListeners={};this.delegatedListeners.click=(0,s.default)(document,this.options.LINK_SELECTOR,"click",this.linkClickHandler.bind(this));this.delegatedListeners.mouseover=(0,s.default)(document.body,this.options.LINK_SELECTOR,"mouseover",this.linkMouseoverHandler.bind(this));this.delegatedListeners.formSubmit=(0,s.default)(document,this.options.FORM_SELECTOR,"submit",this.formSubmitHandler.bind(this));window.addEventListener("popstate",this.popStateHandler.bind(this));var n=this.getDataFromHtml(document.documentElement.outerHTML);n.url=this.currentUrl;if(this.options.cache){this.cache.cacheUrl(n,this.options.debugMode)}this.markSwupElements(document.documentElement);this.options.plugins.forEach(function(e){return t.usePlugin(e)});window.history.replaceState(Object.assign({},window.history.state,{url:window.location.href,random:Math.random(),source:"swup"}),document.title,window.location.href);if(this.options.animateHistoryBrowsing){window.history.scrollRestoration="manual"}this.triggerEvent("enabled");document.documentElement.classList.add("swup-enabled");this.triggerEvent("pageView");this.preloadPages()}},{key:"destroy",value:function e(){this.delegatedListeners.click.destroy();this.delegatedListeners.mouseover.destroy();window.removeEventListener("popstate",this.popStateHandler.bind(this));this.cache.empty();document.querySelectorAll("[data-swup]").forEach(function(e){delete e.dataset.swup});this.triggerEvent("disabled");document.documentElement.classList.remove("swup-enabled")}},{key:"linkClickHandler",value:function e(t){if(!t.metaKey){this.triggerEvent("clickLink");var n=new c.default;t.preventDefault();n.setPath(t.delegateTarget.href);if(n.getAddress()==this.currentUrl||n.getAddress()==""){if(n.getHash()!=""){this.triggerEvent("samePageWithHash");var o=document.querySelector(n.getHash());if(o!=null){if(this.options.scroll){var r=o.getBoundingClientRect().top+window.pageYOffset;this.scrollTo(document.body,r)}history.replaceState({url:n.getAddress()+n.getHash(),random:Math.random(),source:"swup"},document.title,n.getAddress()+n.getHash())}else{console.warn("Element for offset not found ("+n.getHash()+")")}}else{this.triggerEvent("samePage");if(this.options.scroll){this.scrollTo(document.body,0,1)}}}else{if(n.getHash()!=""){this.scrollToElement=n.getHash()}var i=t.delegateTarget.dataset.swupTransition;this.loadPage({url:n.getAddress(),customTransition:i},false)}}else{this.triggerEvent("openPageInNewTab")}}},{key:"linkMouseoverHandler",value:function e(t){var n=this;this.triggerEvent("hoverLink");if(this.options.preload){var o=new c.default;o.setPath(t.delegateTarget.href);if(o.getAddress()!=this.currentUrl&&!this.cache.exists(o.getAddress())&&this.preloadPromise==null){this.preloadPromise=new Promise(function(e,t){n.getPage({url:o.getAddress()},function(r,i){if(i.status===500){n.triggerEvent("serverError");t(o.getAddress());return}else{var s=n.getDataFromHtml(r,i);if(s!=null){s.url=o.getAddress();n.cache.cacheUrl(s,n.options.debugMode);n.triggerEvent("pagePreloaded")}else{t(o.getAddress());return}}e();n.preloadPromise=null})});this.preloadPromise.route=o.getAddress()}}}},{key:"formSubmitHandler",value:function e(t){if(!t.metaKey){this.triggerEvent("submitForm");t.preventDefault();var n=t.target;var o=new FormData(n);var r=new c.default;r.setPath(n.action);if(r.getHash()!=""){this.scrollToElement=r.getHash()}if(n.method.toLowerCase()!="get"){this.cache.remove(r.getAddress());this.loadPage({url:r.getAddress(),method:n.method,data:o})}else{var i=r.getAddress()||window.location.href;var s=n.querySelectorAll("input");if(i.indexOf("?")==-1){i+="?"}else{i+="&"}s.forEach(function(e){if(e.type=="checkbox"||e.type=="radio"){if(e.checked){i+=encodeURIComponent(e.name)+"="+encodeURIComponent(e.value)+"&"}}else{i+=encodeURIComponent(e.name)+"="+encodeURIComponent(e.value)+"&"}});i=i.slice(0,-1);this.cache.remove(i);this.loadPage({url:i})}}else{this.triggerEvent("openFormSubmitInNewTab")}}},{key:"popStateHandler",value:function e(t){var n=new c.default;if(this.options.skipPopStateHandling(t))return;n.setPath(t.state?t.state.url:window.location.pathname);if(n.getHash()!=""){this.scrollToElement=n.getHash()}else{t.preventDefault()}this.triggerEvent("popState");this.loadPage({url:n.getAddress()},t)}}]);return e}();t.default=z},function(e,t,n){var o=n(4);function r(e,t,n,o,r){var i=s.apply(this,arguments);e.addEventListener(n,i,r);return{destroy:function(){e.removeEventListener(n,i,r)}}}function i(e,t,n,o,i){if(typeof e.addEventListener==="function"){return r.apply(null,arguments)}if(typeof n==="function"){return r.bind(null,document).apply(null,arguments)}if(typeof e==="string"){e=document.querySelectorAll(e)}return Array.prototype.map.call(e,function(e){return r(e,t,n,o,i)})}function s(e,t,n,r){return function(n){n.delegateTarget=o(n.target,t);if(n.delegateTarget){r.call(e,n)}}}e.exports=i},function(e,t){var n=9;if(typeof Element!=="undefined"&&!Element.prototype.matches){var o=Element.prototype;o.matches=o.matchesSelector||o.mozMatchesSelector||o.msMatchesSelector||o.oMatchesSelector||o.webkitMatchesSelector}function r(e,t){while(e&&e.nodeType!==n){if(typeof e.matches==="function"&&e.matches(t)){return e}e=e.parentNode}}e.exports=r},function(e,t,n){"use strict";Object.defineProperty(t,"__esModule",{value:true});var o=function(){function e(e,t){for(var n=0;n<t.length;n++){var o=t[n];o.enumerable=o.enumerable||false;o.configurable=true;if("value"in o)o.writable=true;Object.defineProperty(e,o.key,o)}}return function(t,n,o){if(n)e(t.prototype,n);if(o)e(t,o);return t}}();function r(e,t){if(!(e instanceof t)){throw new TypeError("Cannot call a class as a function")}}var i=function(){function e(){r(this,e);this.pages={};this.count=0;this.last=null}o(e,[{key:"cacheUrl",value:function e(t,n){this.count++;if(t.url in this.pages===false){this.pages[t.url]=t}this.last=this.pages[t.url];if(n){this.displayCache()}}},{key:"getPage",value:function e(t){return this.pages[t]}},{key:"displayCache",value:function e(){console.groupCollapsed("Cache ("+Object.keys(this.pages).length+")");for(var t in this.pages){console.log(this.pages[t])}console.groupEnd()}},{key:"exists",value:function e(t){if(t in this.pages)return true;return false}},{key:"empty",value:function e(t){this.pages={};this.count=0;this.last=null;if(t){console.log("Cache cleared")}}},{key:"remove",value:function e(t){delete this.pages[t]}}]);return e}();t.default=i},function(e,t,n){"use strict";e.exports=function e(){var t=document.createElement("div");var n={WebkitTransition:"webkitTransitionEnd",MozTransition:"transitionend",OTransition:"oTransitionEnd otransitionend",transition:"transitionend"};for(var o in n){if(t.style[o]!==undefined){return n[o]}}return false}},function(e,t,n){"use strict";var o=Object.assign||function(e){for(var t=1;t<arguments.length;t++){var n=arguments[t];for(var o in n){if(Object.prototype.hasOwnProperty.call(n,o)){e[o]=n[o]}}}return e};e.exports=function(e){var t=arguments.length>1&&arguments[1]!==undefined?arguments[1]:false;var n={url:window.location.pathname+window.location.search,method:"GET",data:null};var r=o({},n,e);var i=new XMLHttpRequest;i.onreadystatechange=function(){if(i.readyState===4){if(i.status!==500){t(i.responseText,i)}else{t(null,i)}}};i.open(r.method,r.url,true);i.setRequestHeader("X-Requested-With","swup");i.send(r.data);return i}},function(e,t,n){"use strict";e.exports=function(e,t){var n=this;var o=e.replace("<body",'<div id="swupBody"').replace("</body>","</div>");var r=document.createElement("div");r.innerHTML=o;var i=[];for(var s=0;s<this.options.elements.length;s++){if(r.querySelector(this.options.elements[s])==null){console.warn("Element "+this.options.elements[s]+" is not found in cached page.");return null}else{[].forEach.call(document.body.querySelectorAll(this.options.elements[s]),function(e,t){r.querySelectorAll(n.options.elements[s])[t].dataset.swup=i.length;i.push(r.querySelectorAll(n.options.elements[s])[t].outerHTML)})}}var a={title:r.querySelector("title").innerText,pageClass:r.querySelector("#swupBody").className,originalContent:e,blocks:i,responseURL:t!=null?t.responseURL:window.location.href};return a}},function(e,t,n){"use strict";var o=Array.prototype.forEach;e.exports=function(e,t){var n=this;if(this.options.doScrollingRightAway&&!this.scrollToElement){this.doScrolling(t)}var r=[];if(e.customTransition!=null){this.updateTransition(window.location.pathname,e.url,e.customTransition);document.documentElement.classList.add("to-"+this.classify(e.customTransition))}else{this.updateTransition(window.location.pathname,e.url)}if(!t||this.options.animateHistoryBrowsing){this.triggerEvent("animationOutStart");document.documentElement.classList.add("is-changing");document.documentElement.classList.add("is-leaving");document.documentElement.classList.add("is-animating");if(t){document.documentElement.classList.add("is-popstate")}document.documentElement.classList.add("to-"+this.classify(e.url));var i=document.querySelectorAll(this.options.animationSelector);o.call(i,function(e){var t=new Promise(function(t){e.addEventListener(n.transitionEndEvent,function(n){if(e==n.target){t()}})});r.push(t)});Promise.all(r).then(function(){n.triggerEvent("animationOutDone")});if(this.scrollToElement!=null){var s=e.url+this.scrollToElement}else{var s=e.url}if(!t)this.createState(s)}else{this.triggerEvent("animationSkipped")}if(this.cache.exists(e.url)){var a=new Promise(function(e){e()});this.triggerEvent("pageRetrievedFromCache")}else{if(!this.preloadPromise||this.preloadPromise.route!=e.url){var a=new Promise(function(t,o){n.getPage(e,function(r,i){if(i.status===500){n.triggerEvent("serverError");o(e.url);return}else{var s=n.getDataFromHtml(r,i);if(s!=null){s.url=e.url}else{o(e.url);return}n.cache.cacheUrl(s,n.options.debugMode);n.triggerEvent("pageLoaded")}t()})})}else{var a=this.preloadPromise}}Promise.all(r.concat([a])).then(function(){n.renderPage(n.cache.getPage(e.url),t);n.preloadPromise=null}).catch(function(e){n.options.skipPopStateHandling=function(){window.location=e;return true};window.history.go(-1)})}},function(e,t,n){"use strict";var o=n(0);var r=i(o);function i(e){return e&&e.__esModule?e:{default:e}}var s=Array.prototype.forEach;e.exports=function(e,t){var n=this;document.documentElement.classList.remove("is-leaving");var o=new r.default;o.setPath(e.responseURL);if(window.location.pathname!==o.getPath()){window.history.replaceState({url:o.getPath(),random:Math.random(),source:"swup"},document.title,o.getPath())}if(!t||this.options.animateHistoryBrowsing){document.documentElement.classList.add("is-rendering")}this.triggerEvent("willReplaceContent");for(var i=0;i<e.blocks.length;i++){document.body.querySelector('[data-swup="'+i+'"]').outerHTML=e.blocks[i]}document.title=e.title;if(this.options.pageClassPrefix!==false){document.body.className.split(" ").forEach(function(e){if(e!=""&&e.includes(n.options.pageClassPrefix)){document.body.classList.remove(e)}})}if(e.pageClass!=""){e.pageClass.split(" ").forEach(function(e){if(e!=""&&e.includes(n.options.pageClassPrefix)){document.body.classList.add(e)}})}this.triggerEvent("contentReplaced");this.triggerEvent("pageView");if(!this.options.cache){this.cache.empty(this.options.debugMode)}setTimeout(function(){if(!t||n.options.animateHistoryBrowsing){n.triggerEvent("animationInStart");document.documentElement.classList.remove("is-animating")}},10);if(!this.options.doScrollingRightAway||this.scrollToElement){this.doScrolling(t)}var a=document.querySelectorAll(this.options.animationSelector);var l=[];s.call(a,function(e){var t=new Promise(function(t){e.addEventListener(n.transitionEndEvent,function(n){if(e==n.target){t()}})});l.push(t)});this.preloadPages();if(!t||this.options.animateHistoryBrowsing){Promise.all(l).then(function(){n.triggerEvent("animationInDone");document.documentElement.className.split(" ").forEach(function(e){if(new RegExp("^to-").test(e)||e==="is-changing"||e==="is-rendering"||e==="is-popstate"){document.documentElement.classList.remove(e)}})})}this.getUrl();this.scrollToElement=null}},function(e,t,n){"use strict";e.exports=function(e){window.history.pushState({url:e||window.location.href.split(window.location.hostname)[1],random:Math.random(),source:"swup"},document.getElementsByTagName("title")[0].innerText,e||window.location.href.split(window.location.hostname)[1])}},function(e,t,n){"use strict";e.exports=function(e){if(this.options.debugMode){console.log("%cswup:"+"%c"+e,"color: #343434","color: #009ACD")}var t=new CustomEvent("swup:"+e,{detail:e});document.dispatchEvent(t)}},function(e,t,n){"use strict";e.exports=function(){this.currentUrl=window.location.pathname+window.location.search}},function(e,t,n){"use strict";e.exports=function(e,t){var n=this;var o=arguments.length>2&&arguments[2]!==undefined?arguments[2]:this.options.animateScroll;var r=1-this.options.scrollFriction;var i=this.options.scrollAcceleration;var s=0;var a=0;var l=0;var u=0;var c=0;var d=null;function f(){return document.body.scrollTop||document.documentElement.scrollTop}var h=function e(){var t=p();m();if(c===1&&l>s||c===-1&&l<s){d=requestAnimationFrame(e)}else{window.scrollTo(0,l);n.triggerEvent("scrollDone")}};function p(){var e=u-s;var t=e*i;g(t);a*=r;s+=a;return e}var g=function e(t){a+=t};var m=function e(){window.scrollTo(0,s)};window.addEventListener("mousewheel",function(e){if(d){cancelAnimationFrame(d);d=null}},{passive:true});var v=function e(t,o){s=f();c=s>t?-1:1;u=t+c;l=t;a=0;if(s!=l){h()}else{n.triggerEvent("scrollDone")}};this.triggerEvent("scrollStart");if(o==0){window.scrollTo(0,t);this.triggerEvent("scrollDone")}else{v(t)}}},function(e,t,n){"use strict";e.exports=function(e){var t=e.toString().toLowerCase().replace(/\s+/g,"-").replace(/\//g,"-").replace(/[^\w\-]+/g,"").replace(/\-\-+/g,"-").replace(/^-+/,"").replace(/-+$/,"");if(t[0]=="/")t=t.splice(1);if(t=="")t="homepage";return t}},function(e,t,n){"use strict";e.exports=function(e){if(this.options.scroll&&(!e||this.options.animateHistoryBrowsing)){if(this.scrollToElement!=null){var t=document.querySelector(this.scrollToElement);if(t!=null){var n=t.getBoundingClientRect().top+window.pageYOffset;this.scrollTo(document.body,n)}else{console.warn("Element for offset not found ("+this.scrollToElement+")")}this.scrollToElement=null}else{this.scrollTo(document.body,0)}}}},function(e,t,n){"use strict";e.exports=function(e){var t=this;var n=0;for(var o=0;o<this.options.elements.length;o++){if(e.querySelector(this.options.elements[o])==null){console.warn("Element "+this.options.elements[o]+" is not in current page.")}else{[].forEach.call(document.body.querySelectorAll(this.options.elements[o]),function(r,i){e.querySelectorAll(t.options.elements[o])[i].dataset.swup=n;n++})}}}},function(e,t,n){"use strict";e.exports=function(e,t,n){if(e=="/"){e="/homepage"}if(t=="/"){t="/homepage"}this.transition={from:e.replace("/",""),to:t.replace("/","")};if(n){this.transition.custom=n}}},function(e,t,n){"use strict";var o=n(0);var r=i(o);function i(e){return e&&e.__esModule?e:{default:e}}e.exports=function(){var e=this;if(this.options.preload){var t=function t(n){var o=new r.default;o.setPath(n);if(o.getAddress()!=e.currentUrl&&!e.cache.exists(o.getAddress())&&e.preloadPromise==null){e.getPage({url:o.getAddress()},function(t,n){if(n.status===500){e.triggerEvent("serverError");return}else{var r=e.getDataFromHtml(t,n);if(r!=null){r.url=o.getAddress();e.cache.cacheUrl(r,e.options.debugMode);e.triggerEvent("pagePreloaded")}}})}};document.querySelectorAll("[data-swup-preload]").forEach(function(e){t(e.href)})}}},function(e,t,n){"use strict";e.exports=function(e,t){var n=this;t=Object.assign({},e.options,t);e.options=t;var o=function e(){var t=n.cache.getPage(window.location.pathname+window.location.search);var o=document.createElement("html");o.innerHTML=t.originalContent;return o};this.plugins.push(e);e.exec(t,this,o);return this.plugins}},function(e,t,n){"use strict";e.exports=function(e){if(this.options.debugMode){console.log(e+"%c","color: #009ACD")}}}])}); | ||
(function e(t,n){if(typeof exports==="object"&&typeof module==="object")module.exports=n();else if(typeof define==="function"&&define.amd)define([],n);else if(typeof exports==="object")exports["Swup"]=n();else t["Swup"]=n()})(window,function(){return function(e){var t={};function n(o){if(t[o]){return t[o].exports}var r=t[o]={i:o,l:false,exports:{}};e[o].call(r.exports,r,r.exports,n);r.l=true;return r.exports}n.m=e;n.c=t;n.d=function(e,t,o){if(!n.o(e,t)){Object.defineProperty(e,t,{enumerable:true,get:o})}};n.r=function(e){if(typeof Symbol!=="undefined"&&Symbol.toStringTag){Object.defineProperty(e,Symbol.toStringTag,{value:"Module"})}Object.defineProperty(e,"__esModule",{value:true})};n.t=function(e,t){if(t&1)e=n(e);if(t&8)return e;if(t&4&&typeof e==="object"&&e&&e.__esModule)return e;var o=Object.create(null);n.r(o);Object.defineProperty(o,"default",{enumerable:true,value:e});if(t&2&&typeof e!="string")for(var r in e)n.d(o,r,function(t){return e[t]}.bind(null,r));return o};n.n=function(e){var t=e&&e.__esModule?function t(){return e["default"]}:function t(){return e};n.d(t,"a",t);return t};n.o=function(e,t){return Object.prototype.hasOwnProperty.call(e,t)};n.p="";return n(n.s=1)}([function(e,t,n){"use strict";Object.defineProperty(t,"__esModule",{value:true});var o=function(){function e(e,t){for(var n=0;n<t.length;n++){var o=t[n];o.enumerable=o.enumerable||false;o.configurable=true;if("value"in o)o.writable=true;Object.defineProperty(e,o.key,o)}}return function(t,n,o){if(n)e(t.prototype,n);if(o)e(t,o);return t}}();function r(e,t){if(!(e instanceof t)){throw new TypeError("Cannot call a class as a function")}}var i=function(){function e(){r(this,e);this.link=document.createElement("a")}o(e,[{key:"setPath",value:function e(t){this.link.href=t}},{key:"getPath",value:function e(){var t=this.link.pathname;if(t[0]!="/"){t="/"+t}return t}},{key:"getAddress",value:function e(){var t=this.link.pathname+this.link.search;if(t[0]!="/"){t="/"+t}return t}},{key:"getHash",value:function e(){return this.link.hash}}]);return e}();t.default=i},function(e,t,n){"use strict";var o=n(2);var r=i(o);function i(e){return e&&e.__esModule?e:{default:e}}e.exports=r.default},function(e,t,n){"use strict";Object.defineProperty(t,"__esModule",{value:true});var o=Object.assign||function(e){for(var t=1;t<arguments.length;t++){var n=arguments[t];for(var o in n){if(Object.prototype.hasOwnProperty.call(n,o)){e[o]=n[o]}}}return e};var r=function(){function e(e,t){for(var n=0;n<t.length;n++){var o=t[n];o.enumerable=o.enumerable||false;o.configurable=true;if("value"in o)o.writable=true;Object.defineProperty(e,o.key,o)}}return function(t,n,o){if(n)e(t.prototype,n);if(o)e(t,o);return t}}();var i=n(3);var s=X(i);var a=n(5);var l=X(a);var u=n(0);var c=X(u);var d=n(6);var f=X(d);var h=n(7);var p=X(h);var g=n(8);var m=X(g);var v=n(9);var w=X(v);var y=n(10);var E=X(y);var b=n(11);var P=X(b);var S=n(12);var T=X(S);var k=n(13);var L=X(k);var x=n(14);var H=X(x);var A=n(15);var C=X(A);var M=n(16);var O=X(M);var _=n(17);var R=X(_);var j=n(18);var q=X(j);var U=n(19);var D=X(U);var F=n(20);var I=X(F);var B=n(21);var N=X(B);var K=n(22);var W=X(K);var V=n(23);var z=X(V);function X(e){return e&&e.__esModule?e:{default:e}}function Y(e,t){if(!(e instanceof t)){throw new TypeError("Cannot call a class as a function")}}var G=function(){function e(t){Y(this,e);var n={cache:true,animationSelector:'[class*="transition-"]',elements:["#swup"],pageClassPrefix:"",debugMode:false,scroll:true,doScrollingRightAway:false,animateScroll:true,scrollFriction:.3,scrollAcceleration:.04,preload:true,support:true,plugins:[],skipPopStateHandling:function e(t){if(t.state&&t.state.source=="swup"){return false}return true},animateHistoryBrowsing:false,LINK_SELECTOR:'a[href^="'+window.location.origin+'"]:not([data-no-swup]), a[href^="/"]:not([data-no-swup]), a[href^="#"]:not([data-no-swup])',FORM_SELECTOR:"form[data-swup-form]"};this.transition={};var r=o({},n,t);this._handlers={willReplaceContent:[],contentReplaced:[],pageView:[],hoverLink:[],clickLink:[],samePageWithHash:[],animationOutStart:[],animationOutDone:[],animationSkipped:[],pagePreloaded:[],pageLoaded:[],scrollStart:[],scrollDone:[],animationInStart:[],animationInDone:[],pageRetrievedFromCache:[],submitForm:[],enabled:[],disabled:[]};this.scrollToElement=null;this.preloadPromise=null;this.options=r;this.plugins=[];this.getUrl=L.default;this.cache=new l.default;this.link=new c.default;this.transitionEndEvent=(0,f.default)();this.getDataFromHtml=m.default;this.getPage=p.default;this.scrollTo=H.default;this.loadPage=w.default;this.renderPage=E.default;this.createState=P.default;this.triggerEvent=T.default;this.classify=C.default;this.doScrolling=O.default;this.markSwupElements=R.default;this.on=q.default;this.off=D.default;this.updateTransition=I.default;this.preloadPages=N.default;this.usePlugin=W.default;this.log=z.default;this.enable=this.enable;this.destroy=this.destroy;if(this.options.debugMode){window.swup=this}this.getUrl();this.enable()}r(e,[{key:"enable",value:function e(){var t=this;if(this.options.support){if(!("pushState"in window.history)){console.warn("pushState is not supported");return}if((0,f.default)()){this.transitionEndEvent=(0,f.default)()}else{console.warn("transitionEnd detection is not supported");return}if(typeof Promise==="undefined"||Promise.toString().indexOf("[native code]")===-1){console.warn("Promise is not supported");return}}this.delegatedListeners={};this.delegatedListeners.click=(0,s.default)(document,this.options.LINK_SELECTOR,"click",this.linkClickHandler.bind(this));this.delegatedListeners.mouseover=(0,s.default)(document.body,this.options.LINK_SELECTOR,"mouseover",this.linkMouseoverHandler.bind(this));this.delegatedListeners.formSubmit=(0,s.default)(document,this.options.FORM_SELECTOR,"submit",this.formSubmitHandler.bind(this));window.addEventListener("popstate",this.popStateHandler.bind(this));var n=this.getDataFromHtml(document.documentElement.outerHTML);n.url=this.currentUrl;if(this.options.cache){this.cache.cacheUrl(n,this.options.debugMode)}this.markSwupElements(document.documentElement);this.options.plugins.forEach(function(e){return t.usePlugin(e)});window.history.replaceState(Object.assign({},window.history.state,{url:window.location.href,random:Math.random(),source:"swup"}),document.title,window.location.href);if(this.options.animateHistoryBrowsing){window.history.scrollRestoration="manual"}this.triggerEvent("enabled");document.documentElement.classList.add("swup-enabled");this.triggerEvent("pageView");this.preloadPages()}},{key:"destroy",value:function e(){this.delegatedListeners.click.destroy();this.delegatedListeners.mouseover.destroy();window.removeEventListener("popstate",this.popStateHandler.bind(this));this.cache.empty();document.querySelectorAll("[data-swup]").forEach(function(e){delete e.dataset.swup});this.off();this.triggerEvent("disabled");document.documentElement.classList.remove("swup-enabled")}},{key:"linkClickHandler",value:function e(t){if(!t.metaKey){this.triggerEvent("clickLink",t);var n=new c.default;t.preventDefault();n.setPath(t.delegateTarget.href);if(n.getAddress()==this.currentUrl||n.getAddress()==""){if(n.getHash()!=""){this.triggerEvent("samePageWithHash",t);var o=document.querySelector(n.getHash());if(o!=null){if(this.options.scroll){var r=o.getBoundingClientRect().top+window.pageYOffset;this.scrollTo(document.body,r)}history.replaceState({url:n.getAddress()+n.getHash(),random:Math.random(),source:"swup"},document.title,n.getAddress()+n.getHash())}else{console.warn("Element for offset not found ("+n.getHash()+")")}}else{this.triggerEvent("samePage",t);if(this.options.scroll){this.scrollTo(document.body,0,1)}}}else{if(n.getHash()!=""){this.scrollToElement=n.getHash()}var i=t.delegateTarget.dataset.swupTransition;this.loadPage({url:n.getAddress(),customTransition:i},false)}}else{this.triggerEvent("openPageInNewTab",t)}}},{key:"linkMouseoverHandler",value:function e(t){var n=this;this.triggerEvent("hoverLink",t);if(this.options.preload){var o=new c.default;o.setPath(t.delegateTarget.href);if(o.getAddress()!=this.currentUrl&&!this.cache.exists(o.getAddress())&&this.preloadPromise==null){this.preloadPromise=new Promise(function(e,r){n.getPage({url:o.getAddress()},function(i,s){if(s.status===500){n.triggerEvent("serverError",t);r(o.getAddress());return}else{var a=n.getDataFromHtml(i,s);if(a!=null){a.url=o.getAddress();n.cache.cacheUrl(a,n.options.debugMode);n.triggerEvent("pagePreloaded",t)}else{r(o.getAddress());return}}e();n.preloadPromise=null})});this.preloadPromise.route=o.getAddress()}}}},{key:"formSubmitHandler",value:function e(t){if(!t.metaKey){this.triggerEvent("submitForm",t);t.preventDefault();var n=t.target;var o=new FormData(n);var r=new c.default;r.setPath(n.action);if(r.getHash()!=""){this.scrollToElement=r.getHash()}if(n.method.toLowerCase()!="get"){this.cache.remove(r.getAddress());this.loadPage({url:r.getAddress(),method:n.method,data:o})}else{var i=r.getAddress()||window.location.href;var s=n.querySelectorAll("input");if(i.indexOf("?")==-1){i+="?"}else{i+="&"}s.forEach(function(e){if(e.type=="checkbox"||e.type=="radio"){if(e.checked){i+=encodeURIComponent(e.name)+"="+encodeURIComponent(e.value)+"&"}}else{i+=encodeURIComponent(e.name)+"="+encodeURIComponent(e.value)+"&"}});i=i.slice(0,-1);this.cache.remove(i);this.loadPage({url:i})}}else{this.triggerEvent("openFormSubmitInNewTab",t)}}},{key:"popStateHandler",value:function e(t){var n=new c.default;if(this.options.skipPopStateHandling(t))return;n.setPath(t.state?t.state.url:window.location.pathname);if(n.getHash()!=""){this.scrollToElement=n.getHash()}else{t.preventDefault()}this.triggerEvent("popState",t);this.loadPage({url:n.getAddress()},t)}}]);return e}();t.default=G},function(e,t,n){var o=n(4);function r(e,t,n,o,r){var i=s.apply(this,arguments);e.addEventListener(n,i,r);return{destroy:function(){e.removeEventListener(n,i,r)}}}function i(e,t,n,o,i){if(typeof e.addEventListener==="function"){return r.apply(null,arguments)}if(typeof n==="function"){return r.bind(null,document).apply(null,arguments)}if(typeof e==="string"){e=document.querySelectorAll(e)}return Array.prototype.map.call(e,function(e){return r(e,t,n,o,i)})}function s(e,t,n,r){return function(n){n.delegateTarget=o(n.target,t);if(n.delegateTarget){r.call(e,n)}}}e.exports=i},function(e,t){var n=9;if(typeof Element!=="undefined"&&!Element.prototype.matches){var o=Element.prototype;o.matches=o.matchesSelector||o.mozMatchesSelector||o.msMatchesSelector||o.oMatchesSelector||o.webkitMatchesSelector}function r(e,t){while(e&&e.nodeType!==n){if(typeof e.matches==="function"&&e.matches(t)){return e}e=e.parentNode}}e.exports=r},function(e,t,n){"use strict";Object.defineProperty(t,"__esModule",{value:true});var o=function(){function e(e,t){for(var n=0;n<t.length;n++){var o=t[n];o.enumerable=o.enumerable||false;o.configurable=true;if("value"in o)o.writable=true;Object.defineProperty(e,o.key,o)}}return function(t,n,o){if(n)e(t.prototype,n);if(o)e(t,o);return t}}();function r(e,t){if(!(e instanceof t)){throw new TypeError("Cannot call a class as a function")}}var i=function(){function e(){r(this,e);this.pages={};this.count=0;this.last=null}o(e,[{key:"cacheUrl",value:function e(t,n){this.count++;if(t.url in this.pages===false){this.pages[t.url]=t}this.last=this.pages[t.url];if(n){this.displayCache()}}},{key:"getPage",value:function e(t){return this.pages[t]}},{key:"displayCache",value:function e(){console.groupCollapsed("Cache ("+Object.keys(this.pages).length+")");for(var t in this.pages){console.log(this.pages[t])}console.groupEnd()}},{key:"exists",value:function e(t){if(t in this.pages)return true;return false}},{key:"empty",value:function e(t){this.pages={};this.count=0;this.last=null;if(t){console.log("Cache cleared")}}},{key:"remove",value:function e(t){delete this.pages[t]}}]);return e}();t.default=i},function(e,t,n){"use strict";e.exports=function e(){var t=document.createElement("div");var n={WebkitTransition:"webkitTransitionEnd",MozTransition:"transitionend",OTransition:"oTransitionEnd otransitionend",transition:"transitionend"};for(var o in n){if(t.style[o]!==undefined){return n[o]}}return false}},function(e,t,n){"use strict";var o=Object.assign||function(e){for(var t=1;t<arguments.length;t++){var n=arguments[t];for(var o in n){if(Object.prototype.hasOwnProperty.call(n,o)){e[o]=n[o]}}}return e};e.exports=function(e){var t=arguments.length>1&&arguments[1]!==undefined?arguments[1]:false;var n={url:window.location.pathname+window.location.search,method:"GET",data:null};var r=o({},n,e);var i=new XMLHttpRequest;i.onreadystatechange=function(){if(i.readyState===4){if(i.status!==500){t(i.responseText,i)}else{t(null,i)}}};i.open(r.method,r.url,true);i.setRequestHeader("X-Requested-With","swup");i.send(r.data);return i}},function(e,t,n){"use strict";e.exports=function(e,t){var n=this;var o=e.replace("<body",'<div id="swupBody"').replace("</body>","</div>");var r=document.createElement("div");r.innerHTML=o;var i=[];for(var s=0;s<this.options.elements.length;s++){if(r.querySelector(this.options.elements[s])==null){console.warn("Element "+this.options.elements[s]+" is not found in cached page.");return null}else{[].forEach.call(document.body.querySelectorAll(this.options.elements[s]),function(e,t){r.querySelectorAll(n.options.elements[s])[t].dataset.swup=i.length;i.push(r.querySelectorAll(n.options.elements[s])[t].outerHTML)})}}var a={title:r.querySelector("title").innerText,pageClass:r.querySelector("#swupBody").className,originalContent:e,blocks:i,responseURL:t!=null?t.responseURL:window.location.href};return a}},function(e,t,n){"use strict";var o=Array.prototype.forEach;e.exports=function(e,t){var n=this;if(this.options.doScrollingRightAway&&!this.scrollToElement){this.doScrolling(t)}var r=[];if(e.customTransition!=null){this.updateTransition(window.location.pathname,e.url,e.customTransition);document.documentElement.classList.add("to-"+this.classify(e.customTransition))}else{this.updateTransition(window.location.pathname,e.url)}if(!t||this.options.animateHistoryBrowsing){this.triggerEvent("animationOutStart");document.documentElement.classList.add("is-changing");document.documentElement.classList.add("is-leaving");document.documentElement.classList.add("is-animating");if(t){document.documentElement.classList.add("is-popstate")}document.documentElement.classList.add("to-"+this.classify(e.url));var i=document.querySelectorAll(this.options.animationSelector);o.call(i,function(e){var t=new Promise(function(t){e.addEventListener(n.transitionEndEvent,function(n){if(e==n.target){t()}})});r.push(t)});Promise.all(r).then(function(){n.triggerEvent("animationOutDone")});if(this.scrollToElement!=null){var s=e.url+this.scrollToElement}else{var s=e.url}if(!t)this.createState(s)}else{this.triggerEvent("animationSkipped")}if(this.cache.exists(e.url)){var a=new Promise(function(e){e()});this.triggerEvent("pageRetrievedFromCache")}else{if(!this.preloadPromise||this.preloadPromise.route!=e.url){var a=new Promise(function(t,o){n.getPage(e,function(r,i){if(i.status===500){n.triggerEvent("serverError");o(e.url);return}else{var s=n.getDataFromHtml(r,i);if(s!=null){s.url=e.url}else{o(e.url);return}n.cache.cacheUrl(s,n.options.debugMode);n.triggerEvent("pageLoaded")}t()})})}else{var a=this.preloadPromise}}Promise.all(r.concat([a])).then(function(){n.renderPage(n.cache.getPage(e.url),t);n.preloadPromise=null}).catch(function(e){n.options.skipPopStateHandling=function(){window.location=e;return true};window.history.go(-1)})}},function(e,t,n){"use strict";var o=n(0);var r=i(o);function i(e){return e&&e.__esModule?e:{default:e}}var s=Array.prototype.forEach;e.exports=function(e,t){var n=this;document.documentElement.classList.remove("is-leaving");var o=new r.default;o.setPath(e.responseURL);if(window.location.pathname!==o.getPath()){window.history.replaceState({url:o.getPath(),random:Math.random(),source:"swup"},document.title,o.getPath())}if(!t||this.options.animateHistoryBrowsing){document.documentElement.classList.add("is-rendering")}this.triggerEvent("willReplaceContent");for(var i=0;i<e.blocks.length;i++){document.body.querySelector('[data-swup="'+i+'"]').outerHTML=e.blocks[i]}document.title=e.title;if(this.options.pageClassPrefix!==false){document.body.className.split(" ").forEach(function(e){if(e!=""&&e.includes(n.options.pageClassPrefix)){document.body.classList.remove(e)}})}if(e.pageClass!=""){e.pageClass.split(" ").forEach(function(e){if(e!=""&&e.includes(n.options.pageClassPrefix)){document.body.classList.add(e)}})}this.triggerEvent("contentReplaced");this.triggerEvent("pageView");if(!this.options.cache){this.cache.empty(this.options.debugMode)}setTimeout(function(){if(!t||n.options.animateHistoryBrowsing){n.triggerEvent("animationInStart");document.documentElement.classList.remove("is-animating")}},10);if(!this.options.doScrollingRightAway||this.scrollToElement){this.doScrolling(t)}var a=document.querySelectorAll(this.options.animationSelector);var l=[];s.call(a,function(e){var t=new Promise(function(t){e.addEventListener(n.transitionEndEvent,function(n){if(e==n.target){t()}})});l.push(t)});this.preloadPages();if(!t||this.options.animateHistoryBrowsing){Promise.all(l).then(function(){n.triggerEvent("animationInDone");document.documentElement.className.split(" ").forEach(function(e){if(new RegExp("^to-").test(e)||e==="is-changing"||e==="is-rendering"||e==="is-popstate"){document.documentElement.classList.remove(e)}})})}this.getUrl();this.scrollToElement=null}},function(e,t,n){"use strict";e.exports=function(e){window.history.pushState({url:e||window.location.href.split(window.location.hostname)[1],random:Math.random(),source:"swup"},document.getElementsByTagName("title")[0].innerText,e||window.location.href.split(window.location.hostname)[1])}},function(e,t,n){"use strict";e.exports=function(e,t){if(this.options.debugMode&&t){console.groupCollapsed("%cswup:"+"%c"+e,"color: #343434","color: #009ACD");console.log(t);console.groupEnd()}else if(this.options.debugMode){console.log("%cswup:"+"%c"+e,"color: #343434","color: #009ACD")}this._handlers[e].forEach(function(e){return e(t)});var n=new CustomEvent("swup:"+e,{detail:e});document.dispatchEvent(n)}},function(e,t,n){"use strict";e.exports=function(){this.currentUrl=window.location.pathname+window.location.search}},function(e,t,n){"use strict";e.exports=function(e,t){var n=this;var o=arguments.length>2&&arguments[2]!==undefined?arguments[2]:this.options.animateScroll;var r=1-this.options.scrollFriction;var i=this.options.scrollAcceleration;var s=0;var a=0;var l=0;var u=0;var c=0;var d=null;function f(){return document.body.scrollTop||document.documentElement.scrollTop}var h=function e(){var t=p();m();if(c===1&&l>s||c===-1&&l<s){d=requestAnimationFrame(e)}else{window.scrollTo(0,l);n.triggerEvent("scrollDone")}};function p(){var e=u-s;var t=e*i;g(t);a*=r;s+=a;return e}var g=function e(t){a+=t};var m=function e(){window.scrollTo(0,s)};window.addEventListener("mousewheel",function(e){if(d){cancelAnimationFrame(d);d=null}},{passive:true});var v=function e(t,o){s=f();c=s>t?-1:1;u=t+c;l=t;a=0;if(s!=l){h()}else{n.triggerEvent("scrollDone")}};this.triggerEvent("scrollStart");if(o==0){window.scrollTo(0,t);this.triggerEvent("scrollDone")}else{v(t)}}},function(e,t,n){"use strict";e.exports=function(e){var t=e.toString().toLowerCase().replace(/\s+/g,"-").replace(/\//g,"-").replace(/[^\w\-]+/g,"").replace(/\-\-+/g,"-").replace(/^-+/,"").replace(/-+$/,"");if(t[0]=="/")t=t.splice(1);if(t=="")t="homepage";return t}},function(e,t,n){"use strict";e.exports=function(e){if(this.options.scroll&&(!e||this.options.animateHistoryBrowsing)){if(this.scrollToElement!=null){var t=document.querySelector(this.scrollToElement);if(t!=null){var n=t.getBoundingClientRect().top+window.pageYOffset;this.scrollTo(document.body,n)}else{console.warn("Element for offset not found ("+this.scrollToElement+")")}this.scrollToElement=null}else{this.scrollTo(document.body,0)}}}},function(e,t,n){"use strict";e.exports=function(e){var t=this;var n=0;for(var o=0;o<this.options.elements.length;o++){if(e.querySelector(this.options.elements[o])==null){console.warn("Element "+this.options.elements[o]+" is not in current page.")}else{[].forEach.call(document.body.querySelectorAll(this.options.elements[o]),function(r,i){e.querySelectorAll(t.options.elements[o])[i].dataset.swup=n;n++})}}}},function(e,t,n){"use strict";e.exports=function e(t,n){if(this._handlers[t]){this._handlers[t].push(n)}else{console.warn("Unsupported event "+t+".")}}},function(e,t,n){"use strict";e.exports=function e(t,n){var o=this;if(t!=null){if(n!=null){if(this._handlers[t]&&this._handlers[t].filter(function(e){return e===n}).length){var r=this._handlers[t].filter(function(e){return e===n})[0];var i=this._handlers[t].indexOf(r);if(i>-1){this._handlers[t].splice(i,1)}}else{console.warn("Handler for event '"+t+"' no found.")}}else{this._handlers[t]=[]}}else{Object.keys(this._handlers).forEach(function(e){o._handlers[e]=[]})}}},function(e,t,n){"use strict";e.exports=function(e,t,n){if(e=="/"){e="/homepage"}if(t=="/"){t="/homepage"}this.transition={from:e.replace("/",""),to:t.replace("/","")};if(n){this.transition.custom=n}}},function(e,t,n){"use strict";var o=n(0);var r=i(o);function i(e){return e&&e.__esModule?e:{default:e}}e.exports=function(){var e=this;if(this.options.preload){var t=function t(n){var o=new r.default;o.setPath(n);if(o.getAddress()!=e.currentUrl&&!e.cache.exists(o.getAddress())&&e.preloadPromise==null){e.getPage({url:o.getAddress()},function(t,n){if(n.status===500){e.triggerEvent("serverError");return}else{var r=e.getDataFromHtml(t,n);if(r!=null){r.url=o.getAddress();e.cache.cacheUrl(r,e.options.debugMode);e.triggerEvent("pagePreloaded")}}})}};document.querySelectorAll("[data-swup-preload]").forEach(function(e){t(e.href)})}}},function(e,t,n){"use strict";e.exports=function(e,t){var n=this;t=Object.assign({},e.options,t);e.options=t;var o=function e(){var t=n.cache.getPage(window.location.pathname+window.location.search);var o=document.createElement("html");o.innerHTML=t.originalContent;return o};this.plugins.push(e);e.exec(t,this,o);return this.plugins}},function(e,t,n){"use strict";e.exports=function(e){if(this.options.debugMode){console.log(e+"%c","color: #009ACD")}}}])}); |
@@ -77,2 +77,10 @@ 'use strict'; | ||
var _on = require('./modules/on'); | ||
var _on2 = _interopRequireDefault(_on); | ||
var _off = require('./modules/off'); | ||
var _off2 = _interopRequireDefault(_off); | ||
var _updateTransition = require('./modules/updateTransition'); | ||
@@ -141,2 +149,27 @@ | ||
*/ | ||
this._handlers = { | ||
willReplaceContent: [], | ||
contentReplaced: [], | ||
pageView: [], | ||
hoverLink: [], | ||
clickLink: [], | ||
samePageWithHash: [], | ||
animationOutStart: [], | ||
animationOutDone: [], | ||
animationSkipped: [], | ||
pagePreloaded: [], | ||
pageLoaded: [], | ||
scrollStart: [], | ||
scrollDone: [], | ||
animationInStart: [], | ||
animationInDone: [], | ||
pageRetrievedFromCache: [], | ||
submitForm: [], | ||
enabled: [], | ||
disabled: [] | ||
}; | ||
/** | ||
* helper variables | ||
*/ | ||
// id of element to scroll to after render | ||
@@ -168,2 +201,4 @@ this.scrollToElement = null; | ||
this.markSwupElements = _markSwupElements2.default; | ||
this.on = _on2.default; | ||
this.off = _off2.default; | ||
this.updateTransition = _updateTransition2.default; | ||
@@ -307,2 +342,5 @@ this.preloadPages = _preloadPages2.default; | ||
// remove handlers | ||
this.off(); | ||
this.triggerEvent('disabled'); | ||
@@ -316,3 +354,3 @@ document.documentElement.classList.remove('swup-enabled'); | ||
if (!event.metaKey) { | ||
this.triggerEvent('clickLink'); | ||
this.triggerEvent('clickLink', event); | ||
var link = new _Link2.default(); | ||
@@ -326,3 +364,3 @@ event.preventDefault(); | ||
// link to the same URL with hash | ||
this.triggerEvent('samePageWithHash'); | ||
this.triggerEvent('samePageWithHash', event); | ||
var element = document.querySelector(link.getHash()); | ||
@@ -346,3 +384,3 @@ if (element != null) { | ||
// link to the same URL without hash | ||
this.triggerEvent('samePage'); | ||
this.triggerEvent('samePage', event); | ||
if (this.options.scroll) { | ||
@@ -366,3 +404,3 @@ this.scrollTo(document.body, 0, 1); | ||
// open in new tab (do nothing) | ||
this.triggerEvent('openPageInNewTab'); | ||
this.triggerEvent('openPageInNewTab', event); | ||
} | ||
@@ -375,3 +413,3 @@ } | ||
this.triggerEvent('hoverLink'); | ||
this.triggerEvent('hoverLink', event); | ||
if (this.options.preload) { | ||
@@ -384,3 +422,3 @@ var link = new _Link2.default(); | ||
if (request.status === 500) { | ||
_this2.triggerEvent('serverError'); | ||
_this2.triggerEvent('serverError', event); | ||
reject(link.getAddress()); | ||
@@ -394,3 +432,3 @@ return; | ||
_this2.cache.cacheUrl(page, _this2.options.debugMode); | ||
_this2.triggerEvent('pagePreloaded'); | ||
_this2.triggerEvent('pagePreloaded', event); | ||
} else { | ||
@@ -414,3 +452,3 @@ reject(link.getAddress()); | ||
if (!event.metaKey) { | ||
this.triggerEvent('submitForm'); | ||
this.triggerEvent('submitForm', event); | ||
event.preventDefault(); | ||
@@ -470,3 +508,3 @@ var form = event.target; | ||
} else { | ||
this.triggerEvent('openFormSubmitInNewTab'); | ||
this.triggerEvent('openFormSubmitInNewTab', event); | ||
} | ||
@@ -485,3 +523,3 @@ } | ||
} | ||
this.triggerEvent('popState'); | ||
this.triggerEvent('popState', event); | ||
this.loadPage({ url: link.getAddress() }, event); | ||
@@ -488,0 +526,0 @@ } |
'use strict'; | ||
module.exports = function (eventName) { | ||
if (this.options.debugMode) { | ||
module.exports = function (eventName, originalEvent) { | ||
if (this.options.debugMode && originalEvent) { | ||
console.groupCollapsed('%cswup:' + '%c' + eventName, 'color: #343434', 'color: #009ACD'); | ||
console.log(originalEvent); | ||
console.groupEnd(); | ||
} else if (this.options.debugMode) { | ||
console.log('%cswup:' + '%c' + eventName, 'color: #343434', 'color: #009ACD'); | ||
} | ||
// call saved handlers with "on" method and pass originalEvent object if available | ||
this._handlers[eventName].forEach(function (handler) { | ||
return handler(originalEvent); | ||
}); | ||
// trigger event on document with prefix "swup:" | ||
var event = new CustomEvent('swup:' + eventName, { detail: eventName }); | ||
document.dispatchEvent(event); | ||
}; |
{ | ||
"name": "swup", | ||
"version": "1.6.1", | ||
"version": "1.7.0", | ||
"description": "Animated page transitions with css.", | ||
@@ -5,0 +5,0 @@ "main": "lib/index.js", |
@@ -285,8 +285,14 @@ # swup | ||
## Events | ||
As we are replacing the native functionality of the browser, there is a need for a lifecycle that would replace the standard browser page lifecycle (*load page* and *leave page*). | ||
For this purpose, swup emits bunch of events triggered on the `document` while working. | ||
We can use those events to enable our JavaScript, trigger some analytics, and much more. | ||
As we are replacing the native functionality of the browser, | ||
there is a need for a lifecycle that would replace the standard browser page lifecycle (*load page* and *leave page*). | ||
Swup emits bunch of events, that we can use to enable JavaScript, trigger analytics, and much more. | ||
Handlers are registered and unregistered with swups `on` and `off` methods. | ||
When possible, swup also passes original event into the handler (for events such as clickLink, hoverLink, etc.), | ||
where `delegateTarget` property is the actual target of the delegated event. | ||
This event is also displayed in console in debug mode. | ||
```javascript | ||
// trigger page view for GTM | ||
document.addEventListener('swup:pageView', event => { | ||
swup.on('pageView', function () { | ||
dataLayer.push({ | ||
@@ -299,4 +305,3 @@ 'event': 'VirtualPageview', | ||
// load scripts for replaced elements | ||
document.addEventListener('swup:contentReplaced', event => { | ||
swup.on('contentReplaced', function () { | ||
swup.options.elements.forEach((selector) => { | ||
@@ -308,23 +313,39 @@ // load scripts for all elements with 'selector' | ||
```javascript | ||
swup.off('pageView', handler); // removes single handler of 'pageView' event | ||
swup.off('pageView'); // removes all handlers for 'pageView' event | ||
swup.off(); // removes all handlers for all events | ||
``` | ||
**Note:** example with enabling scripts above assumes using component based approach, like the one used by [Gia framework](https://github.com/giantcz/gia). | ||
### List of all events | ||
* **swup:willReplaceContent** - triggers right before the content of page is replaced | ||
* **swup:contentReplaced** - triggers right after the content of page is replaced | ||
* **swup:pageView** - similar as previous, except it is once triggered on load | ||
* **swup:hoverLink** - triggers when link is hovered | ||
* **swup:clickLink** - triggers when link is clicked | ||
* **swup:animationOutStart** - triggers when animation *OUT* starts (class `is-animating` is added to html tag) | ||
* **swup:animationOutDone** - triggers when transition of all animated elements is done (after click of link and before content is replaced) | ||
* **swup:pagePreloaded** - triggers when the preload of some page is done | ||
* **swup:pageLoaded** - triggers when loading of some page is done (differs from previous only by the source of event - hover/click) | ||
* **swup:scrollStart** - triggers when built in scroll is started | ||
* **swup:scrollDone** - triggers when built in scroll is done | ||
* **swup:animationInStart** - triggers when animation *IN* starts (class `is-animating` is removed from html tag) | ||
* **swup:animationInDone** - triggers when transition of all animated elements is done (after content is replaced) | ||
* **swup:pageRetrievedFromCache** - triggers when page is retrieved from cache and no request is necessary | ||
* **swup:submitForm** - triggers when form is submitted trough swup (right before submission) | ||
* **swup:enabled** - triggers when swup instance is created or re-enabled after call of `destroy()` | ||
* **swup:disabled** - triggers on `destroy()` | ||
* **willReplaceContent** - triggers right before the content of page is replaced | ||
* **contentReplaced** - triggers right after the content of page is replaced | ||
* **pageView** - similar as previous, except it is once triggered on load | ||
* **hoverLink** - triggers when link is hovered | ||
* **clickLink** - triggers when link is clicked | ||
* **samePageWithHash** - triggers when link leading to the same page with `#someElement` in the href attribute is clicked | ||
* **animationOutStart** - triggers when animation *OUT* starts (class `is-animating` is added to html tag) | ||
* **animationOutDone** - triggers when transition of all animated elements is done (after click of link and before content is replaced) | ||
* **animationSkipped** - triggers when transition is skipped (on back/forward buttons) | ||
* **pagePreloaded** - triggers when the preload of some page is done | ||
* **pageLoaded** - triggers when loading of some page is done (differs from previous only by the source of event - hover/click) | ||
* **scrollStart** - triggers when built in scroll is started | ||
* **scrollDone** - triggers when built in scroll is done | ||
* **animationInStart** - triggers when animation *IN* starts (class `is-animating` is removed from html tag) | ||
* **animationInDone** - triggers when transition of all animated elements is done (after content is replaced) | ||
* **pageRetrievedFromCache** - triggers when page is retrieved from cache and no request is necessary | ||
* **submitForm** - triggers when form is submitted trough swup (right before submission) | ||
* **enabled** - triggers when swup instance is created or re-enabled after call of `destroy()` | ||
* **disabled** - triggers on `destroy()` | ||
For backward compatibility, all events are also triggered on the `document` with **swup:** prefix. | ||
```javascript | ||
document.addEventListener('swup:contentReplaced', event => { | ||
// do something when content is replaced | ||
}); | ||
``` | ||
## Plugins | ||
@@ -435,2 +456,9 @@ Some functionality is only necessary in certain projects. | ||
```javascript | ||
// register event handler | ||
swup.on("clickLink", function(event) { | ||
console.log(event); | ||
}); | ||
``` | ||
```javascript | ||
// disable swup | ||
@@ -437,0 +465,0 @@ swup.destroy(); |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
175176
41
2994
483