-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpage-swapper.min.js
1 lines (1 loc) · 9.17 KB
/
page-swapper.min.js
1
var PageSwapper=function(e){var a=window,t=a.document,r=this,n={disableCache:!1,debug:!1,sliderConfig:{},sliderType:"owl"},s=null,i=a.location.protocol+"//"+a.location.host,o=a.location.href,l="",p={};pswXhr=null;r.setArgs=function(a){e=$.extend(e,a)},r.linkClick=function(e){if(e&&e.target){var a=$(e.target);if(a.is("a")||(a=a.closest("a")),a.prop("href")&&!a.hasClass("no-ajax")){var t=a.prop("href");if(-1!==t.indexOf(i)&&-1===t.indexOf("mailto:")){var n=t.split(".");n=n[n.length-1],-1===$.inArray(n,["zip","exe","rar","pdf","doc","gif","png","jpg","jpeg","bmp","mp4","mp3"])&&(e.preventDefault(),r.open(t,e))}}}},r.open=function(a,t){var n=a.split("#");(l="",a=n[0],void 0!==n[1]&&(l=n[1]),o!==a)?(s.trigger("psw-beforeopen",{container:s,url:a,hash:l,currentUrl:o,clickEvent:t}),b("psw beforeOpen",r,s,e,a,l,o),w(a)||($("body").removeClass("psw-finish-loading").addClass("psw-loading"),pswXhr&&(pswXhr.abort(),pswXhr=null),pswXhr=$.ajax({dataType:"text",type:"GET",url:a,data:{pswLoad:1},success:function(e,t,r){d(e,t,a,r)},error:function(){$("body").removeClass("psw-loading"),$("body").addClass("psw-loaderror"),errorTimeout=setTimeout(function(){$("body").removeClass("psw-loaderror")},1e3)}}),s.trigger("psw-loadstart",{container:s,url:a,hash:l,currentUrl:o,clickEvent:t}),b("psw loadstart",r,s,e,a,l,o))):u()};var d=function(a,t,n,i){$("body").removeClass("psw-loading").addClass("psw-finish-loading");var d,w,u,f=$('<div class="tab psw-tab" />'),v=r.getCurrent();return!!i&&(-1!==i.getResponseHeader("Content-Type").indexOf("text/html")&&(b("psw loadComplete",r,s,e,n,l,o,i),w=C(a),d=m(a),content=d.content,u=d.bodyClass,PageSwapper.sliders[e.sliderType].add(p,f),g(v),f.data("originalhtml",content.html()),y(f,content),h(n,w),f.attr("data-url",n).data("title",w),f.data("bodyclass",u),f.parent().addClass("psw-item"),void c(f.parent(),{container:s,oldTab:v,newTab:f.parent(),url:n,currentUrl:o,fullHtml:d.fullHtml})))};r.openFromCache=function(a,t){var n=r.getCurrent();g(n),f(a.parent());var i=a.data("originalhtml");i&&i.length?a.empty().html(i):v(a),h(t,a.data("title")),b("psw openFromCache",t,a,i);var o=a.data("bodyclass");"body"===e.selector&&(o+=" page-swapper "),$("body").removeClass($("body").prop("class")).addClass(o),c(a.parent(),{container:s,oldTab:r.getCurrent(),newTab:a.parent(),url:t})};var c=function(a,t){if(s.trigger("psw-loadcomplete",t),r.jumpTo(a.index()),b("psw finish",a,a.index()),u(),"undefined"!=typeof _gaq&&null!==_gaq&&_gaq.push(["_trackPageview",e.url]),"undefined"!=typeof ga&&null!==ga&&ga("send","pageview",location.pathname),"undefined"!=typeof Piwik&&null!==Piwik){var n=Piwik.getTracker();n.setCustomUrl(window.location.href),n.setDocumentTitle(document.title),n.trackPageView()}};r.jumpTo=function(a){PageSwapper.sliders[e.sliderType].to(p,a)};var w=function(e){var a=s.find('.psw-tab[data-url="'+e+'"]');return a.length||(a=s.find('.psw-tab[data-url="'+e+'/"]')),a.length>0&&(r.openFromCache(a,e),!0)},u=function(){b("psw checkHash",r,s,e,l,o),!e.disableHash&&l&&$("#"+l).length>0&&($("html,body").animate({scrollTop:$("#"+l).offset().top},600),l="")},g=function(e){e.find("*").each(function(e,a){a.id.length>0&&(a.id="psw-rm-"+a.id)})},f=function(e){e.find("*").each(function(e,a){a.id.length>0&&(a.id=a.id.replace("psw-rm-",""))})};r.setClasses=function(){s.addClass("psw-container"),PageSwapper.sliders[e.sliderType].setClasses(p)},r.getCurrent=function(){return PageSwapper.sliders[e.sliderType].getCurrent(p)};var h=function(e,r){o=e,a.history&&void 0!==a.history.pushState&&(a.history.pushState({},r,e),t.title=jQuery("<textarea />").html(r).text())},m=function(a){a=a.replace("<body",'<body><div id="psw-body"').replace("</body>","</div></body");var t,r=$.parseHTML(a,!0);if((r=$(r)).filter("#psw-body").length>0){t=r.filter("#psw-body").prop("class").replace("no-js","");var n=$("body").prop("class");"body"===e.selector&&(t+=" page-swapper "),$("body").removeClass(n).addClass(t)}var s=r.find(e.selector);return 0===s.length&&(s=r.filter("#psw-body")),{content:s,bodyClass:t,fullHtml:r}},v=function(e){b("psw getOrgHtml",e,e.data("url")),pswXhr=$.ajax({dataType:"text",type:"GET",url:e.data("url"),data:{pswLoad:1},success:function(a,t,r){var n=m(a).content;e.data("originalhtml",n.html()),y(e,n),b("psw getOrgHtml finish",e,n.html())}})},y=function(e,a){try{e.empty().html(a.html())}catch(t){a.find("script").remove(),e.empty().html(a.html())}},C=function(e){var a=e.match(/<title>(.*?)<\/title>/);return a=a&&a[1]?a[1]:""};r.changeCurrentUrl=function(e){o=e};var b=function(){e.debug&&console.info(arguments)};r._debug=function(a){e.debug=a},function(){a.$||(a.$=jQuery),(e=$.extend(n,e)).id="page-swapper",s=$($(e.container)[0]),p={args:e,container:s,instance:r},s.is("body")&&(s.wrapInner('<div class="psw-body"></div>'),s=s.find("> .psw-body")),s.wrapInner('<div class="tab psw-starttab psw-tab"></div>'),$("body").on("click",'a:not(a[href*=".jpg"], a[href*=".jpeg"], a[href*=".png"], a[href*=".gif"], a[href*=".JPG"], a[href*=".GIF"], a[href*=".PNG"], a[href*=".JPEG"])',r.linkClick),$(a).on("popstate",function(){r.open(t.location.href)}),s.addClass("psw-container"),PageSwapper.sliders[e.sliderType].init(p);var i=s.find(".psw-starttab");i.attr("data-url",a.location.href),i.data("title",t.title),i.data("bodyclass",$("body").prop("class").replace("no-js","")),i.data("originalhtml",""),b("psw init",r,s,e),r.setClasses()}()};jQuery.fn.pageSwapper=function(e){void 0===e&&(e={}),"undefined"===(e.selector,!1)&&(e.selector=this.selector),void 0===e.selector&&console.info("PageSwapper: No selector defined"),this.each(function(a,t){void 0===document.pageSwapperInstance&&(e.container=jQuery(t),document.pageSwapperInstance=new PageSwapper(e))})},PageSwapper.sliders={},PageSwapper.sliders.owl={owlDefaultArgs:{margin:20,mouseDrag:!1,touchDrag:!1,disableHash:!1},init:function(e){var a=$.extend(PageSwapper.sliders.owl.owlDefaultArgs,e.args.owlConfig,e.args.sliderConfig);a=$.extend(a,{items:1,singleItem:!0,autoHeight:!0});try{e.container.owlCarousel(a)}catch(t){return initFailed=!0,void console.info("psw-owl-init-error",t,e.container,a)}e.container.data("owl.carousel")&&e.container.data("owl.carousel")._plugins&&e.container.data("owl.carousel")._plugins.autoHeight&&setInterval(function(){e.container.data("owl.carousel")._plugins.autoHeight.update()},300),$(document).on("translated.owl.carousel",".psw-container",function(e){e.stopPropagation(),$(e.target).is(".psw-container")&&$(".psw-item:not(.active) .psw-tab").empty()})},to:function(e,a){1==e.args.owlVersion?e.container.data("owlCarousel").goTo(a):e.container.trigger("to.owl.carousel",a)},add:function(e,a){1==e.args.owlVersion?(e.container.data("owlCarousel").addItem(a),e.instance.setClasses()):(e.container.trigger("add.owl.carousel",a),e.container.trigger("refresh.owl.carousel"))},remove:function(e,a){1==e.args.owlVersion?(e.container.data("owlCarousel").removeItem(a.index()),e.instance.setClasses()):(e.container.trigger("remove.owl.carousel",a.index()),e.container.trigger("refresh.owl.carousel"))},setClasses:function(e){e.container.addClass("owl-carousel"),e.container.find("> .owl-stage-outer, > .owl-wrapper-outer").addClass("psw-stage-outer"),e.container.find("> .owl-stage-outer > .owl-stage, > .owl-wrapper-outer > .owl-wrapper").addClass("psw-stage"),e.container.find(".psw-stage > .owl-item").addClass("psw-item")},getCurrent:function(e){return 1==e.args.owlVersion?e.container.find(".psw-item:nth-child("+container.data("owlCarousel").currentItem+")"):e.container.find(".psw-item.active")}},PageSwapper.sliders.swiper={defaultArgs:{slidesPerView:1,autoHeight:1,loop:!1,spaceBetween:20,allowTouchMove:!1,calculateHeight:!0,observer:!1},swiperInstance:null,container:null,$container:null,init:function(e){let a=$.extend(PageSwapper.sliders.swiper.defaultArgs,e.args.sliderConfig),t="string"==typeof e.container?e.container:e.container[0],r=this;PageSwapper.sliders.swiper.container=t,PageSwapper.sliders.swiper.$container=$(t),$(t).wrapInner('<div class="swiper-slide psw-item active" />'),$(t).wrapInner('<div class="swiper-wrapper psw-stage" />'),$(t).before('<div style="height:0;"><span style="display:inline-block;" /></div>'),PageSwapper.sliders.swiper.swiperInstance=new Swiper(t,a),PageSwapper.sliders.swiper.swiperInstance.on("slideChange",function(){PageSwapper.sliders.swiper.$container.find(".psw-item").removeClass("active"),r.getCurrent().addClass("active")}).on("slideChangeTransitionEnd",function(){PageSwapper.sliders.swiper.swiperInstance.updateAutoHeight(200)}),this.lastPswHeight=0,setInterval(function(){let e=$(".psw-item.active .psw-tab").height();this.lastPswHeight!==e&&(PageSwapper.sliders.swiper.swiperInstance.updateAutoHeight(150),this.lastPswHeight=e)},200)},to:function(e,a){PageSwapper.sliders.swiper.swiperInstance.slideTo(a)},add:function(e,a){let t=$('<div class="swiper-slide psw-item" />');t.append(a),PageSwapper.sliders.swiper.swiperInstance.appendSlide(t[0])},remove:function(e,a){PageSwapper.sliders.swiper.swiperInstance.removeSlide(a.index())},setClasses:function(e){let a=PageSwapper.sliders.swiper.$container;a.addClass("psw-container"),a.find("> .swiper-wrapper").addClass("psw-stage"),a.find(".psw-stage > .swiper-slide").addClass("psw-item")},getCurrent:function(e){return $(PageSwapper.sliders.swiper.$container.find(".psw-item").get(PageSwapper.sliders.swiper.swiperInstance.activeIndex))}};