highway.js
Version:
Highway is an light-weight JS Page Loader
1 lines • 6.41 kB
JavaScript
!function(t,e){"object"==typeof exports&&"object"==typeof module?module.exports=e():"function"==typeof define&&define.amd?define("Highway",[],e):"object"==typeof exports?exports.Highway=e():t.Highway=e()}(this,function(){return function(t){function e(i){if(n[i])return n[i].exports;var r=n[i]={i:i,l:!1,exports:{}};return t[i].call(r.exports,r,r.exports,e),r.l=!0,r.exports}var n={};return e.m=t,e.c=n,e.i=function(t){return t},e.d=function(t,n,i){e.o(t,n)||Object.defineProperty(t,n,{configurable:!1,enumerable:!0,get:i})},e.n=function(t){var n=t&&t.__esModule?function(){return t.default}:function(){return t};return e.d(n,"a",n),n},e.o=function(t,e){return Object.prototype.hasOwnProperty.call(t,e)},e.p="",e(e.s=2)}([function(t,e,n){"use strict";function i(t,e){if(!(t instanceof e))throw new TypeError("Cannot call a class as a function")}Object.defineProperty(e,"__esModule",{value:!0});var r=function(){function t(t,e){for(var n=0;n<e.length;n++){var i=e[n];i.enumerable=i.enumerable||!1,i.configurable=!0,"value"in i&&(i.writable=!0),Object.defineProperty(t,i.key,i)}}return function(e,n,i){return n&&t(e.prototype,n),i&&t(e,i),e}}(),a=function(){function t(e){i(this,t),this.views=e.views,this.mode=e.mode||"out-in",this.onPopstate=this.popstate.bind(this),this.onPathChange=this.change.bind(this),window.addEventListener("popstate",this.onPopstate),this.bind(),this.init()}return r(t,[{key:"init",value:function(){var t=document.querySelector("title").innerHTML,e=document.querySelector("[loader-view]"),n=e.getAttribute("loader-view");this.path=window.location.pathname,this.stack={},this.stack[this.path]={title:t,view:e,name:n},this.current=new this.views[n](this.stack[this.path]),this.current.init();var i=!0,r=!1,a=void 0;try{for(var o,u=this.links[Symbol.iterator]();!(i=(o=u.next()).done);i=!0){var s=o.value;s.pathname===this.path&&this.active(s)}}catch(t){r=!0,a=t}finally{try{!i&&u.return&&u.return()}finally{if(r)throw a}}}},{key:"bind",value:function(){this.links=document.querySelectorAll("a:not([loader-disabled])");var t=!0,e=!1,n=void 0;try{for(var i,r=this.links[Symbol.iterator]();!(t=(i=r.next()).done);t=!0){var a=i.value;a.addEventListener("click",this.onPathChange),a.addEventListener("touchstart",this.onPathChange)}}catch(t){e=!0,n=t}finally{try{!t&&r.return&&r.return()}finally{if(e)throw n}}}},{key:"unbind",value:function(){var t=!0,e=!1,n=void 0;try{for(var i,r=this.links[Symbol.iterator]();!(t=(i=r.next()).done);t=!0){var a=i.value;a.removeEventListener("click",this.onPathChange),a.removeEventListener("touchstart",this.onPathChange)}}catch(t){e=!0,n=t}finally{try{!t&&r.return&&r.return()}finally{if(e)throw n}}}},{key:"popstate",value:function(){this.path=window.location.pathname;var t=!0,e=!1,n=void 0;try{for(var i,r=this.links[Symbol.iterator]();!(t=(i=r.next()).done);t=!0){var a=i.value;a.pathname===this.path&&(this.temp=a)}}catch(t){e=!0,n=t}finally{try{!t&&r.return&&r.return()}finally{if(e)throw n}}this.fetchCall()}},{key:"active",value:function(t){var e=t.getAttribute("loader-active");if(e){if(this.HTMLActive){var n=this.HTMLActive.getAttribute("loader-active");this.HTMLActive.classList.remove(n)}this.HTMLActive=t,this.HTMLActive.classList.add(e)}}},{key:"change",value:function(t){t=t||window.event,t.preventDefault&&t.preventDefault(),t.returnValue=!1;var e=t.currentTarget,n=e.getAttribute("href");if("_blank"===e.getAttribute("target"))return window.open(n,"_blank");this.temp=e,this.update(n)}},{key:"update",value:function(t){t!==this.path&&(this.path=t,window.history.pushState({path:this.path},"",this.path),this.fetchCall())}},{key:"fetchCall",value:function(){var t=this;if(!this.stack[this.path])return void fetch(this.path).then(function(t){return t.text()}).then(function(e){return t.fetchSuccess(e)}).catch(function(e){return t.fetchError(e)});this.redirect()}},{key:"fetchSuccess",value:function(t){var e=document.createRange().createContextualFragment(t),n=e.querySelector("title").innerHTML,i=e.querySelector("[loader-view]"),r=i.getAttribute("loader-view");this.stack[this.path]={title:n,view:i,name:r},this.redirect()}},{key:"fetchError",value:function(t){throw Error(t)}},{key:"redirect",value:function(){var t=this,e=this.stack[this.path].name;switch(this.previous=this.current,this.current=new this.views[e](this.stack[this.path]),this.active(this.temp),this.unbind(),this.mode){case"both":this.current.load(),this.previous.unload().then(function(){return t.bind()}).catch(function(t){throw Error(t)});break;case"in-out":this.current.load().then(function(){return t.previous.unload()}).then(function(){return t.bind()}).catch(function(t){throw Error(t)});break;default:this.previous.unload().then(function(){return t.current.load()}).then(function(){return t.bind()}).catch(function(t){throw Error(t)})}}}]),t}();e.default=a},function(t,e,n){"use strict";function i(t,e){if(!(t instanceof e))throw new TypeError("Cannot call a class as a function")}Object.defineProperty(e,"__esModule",{value:!0});var r=function(){function t(t,e){for(var n=0;n<e.length;n++){var i=e[n];i.enumerable=i.enumerable||!1,i.configurable=!0,"value"in i&&(i.writable=!0),Object.defineProperty(t,i.key,i)}}return function(e,n,i){return n&&t(e.prototype,n),i&&t(e,i),e}}(),a=function(){function t(e){i(this,t),this.wrapper=document.querySelector("[loader-wrapper]"),this.title=e.title,this.view=e.view}return r(t,[{key:"init",value:function(){this.onEnterCompleted&&this.onEnterCompleted()}},{key:"append",value:function(){document.title=this.title,this.wrapper.appendChild(this.view)}},{key:"remove",value:function(){this.wrapper.removeChild(this.view)}},{key:"load",value:function(){var t=this;return this.onEnter&&this.onEnter(),this.append(),new Promise(function(e,n){t.transition.in(t.view,function(){e(),t.analytics(),t.onEnterCompleted&&t.onEnterCompleted()})})}},{key:"unload",value:function(){var t=this;return this.onLeave&&this.onLeave(),new Promise(function(e,n){t.transition.out(t.view,function(){e(),t.remove(),t.onLeaveCompleted&&t.onLeaveCompleted()})})}},{key:"analytics",value:function(){"undefined"!=typeof ga&&(ga("set",{page:window.location.pathname,title:document.title}),ga("send","pageview"))}}]),t}();e.default=a},function(t,e,n){"use strict";function i(t){return t&&t.__esModule?t:{default:t}}Object.defineProperty(e,"__esModule",{value:!0});var r=n(1),a=i(r),o=n(0),u=i(o),s={View:a.default,Loader:u.default};e.default=s}])});