This repository has been archived by the owner on Feb 12, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtilda-router.min.js
1 lines (1 loc) · 6.67 KB
/
tilda-router.min.js
1
function _classCallCheck(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}var _createClass=function(){function e(e,t){for(var n=0;n<t.length;n++){var r=t[n];r.enumerable=r.enumerable||!1,r.configurable=!0,"value"in r&&(r.writable=!0),Object.defineProperty(e,r.key,r)}}return function(t,n,r){return n&&e(t.prototype,n),r&&e(t,r),t}}();(function(e,t){function r(e){if(n){var t;for(var r=arguments.length,i=Array(r>1?r-1:0),s=1;s<r;s++)i[s-1]=arguments[s];(t=console).log.apply(t,[e].concat(i))}}function i(){return t.location.pathname+t.location.hash+t.location.search}var n=!1,s=function(){function n(s,o){_classCallCheck(this,n);var u=s.hash,a=s.search;this.config=o||{attractors:[]},this.stateObject={hashName:"",queryStr:a,stateName:"init",timestamp:Date.now(),scrollPos:0},this.stateObject.prevState=this.stateObject,this.lastHashBeforePopup="";if(t.history){"scrollRestoration"in t.history&&(t.history.scrollRestoration="manual");var f=t.location.pathname+this.stateObject.queryStr;r("inception: %s —> %s",i(),f),t.history.replaceState(this.stateObject,null,f);var l=this;e(t).on("popstate",function(e){var n=e.originalEvent.state;n?l.processNavigation(n,{history:{}}):l.processNavigation({hashName:t.location.hash,queryStr:t.location.search})}),e(document).ready(function(){e("a[href*=#]:not([href=#],[href^=#popup],[href^=#prodpopup])").off("click").on("click",function(){return l.processNavigation({hashName:this.hash,queryStr:""}),!1}),u!==""&&l.processNavigation({hashName:u,queryStr:""},{isInitCall:!0})})}else u!==""&&(t.location.hash=""+this.stateObject.queryStr,setTimeout(function(){t.location.hash=u},500))}return _createClass(n,[{key:"switchNavState",value:function(s,o){var u=this.stateObject,a=u.queryStr||"";this.doSaveScrollPos(s,o)&&t.history&&(u.scrollPos=t.scrollY-n.getScrollPos(u.hashName),r("saveScrollPos: %s = %s",u.hashName,u.scrollPos),t.history.replaceState(u,null,t.location.pathname+u.hashName+a));if(!s.timestamp){s.timestamp=Date.now(),s.prevState=Object.assign({},u);var f=t.location.pathname+s.hashName+a;r("switchNavState: %s –> %s",i(),f),t.history?t.history.pushState(s,null,f):t.location.assign(f)}this.stateObject=s}},{key:"goToPrevNavState",value:function(n){var i=this.stateObject;r("goToPrevNavState: %s –> %s",i.hashName,i.prevState.hashName),n=this.prepareNavigationMeta(n);if(t.history)t.history.back();else{var s=i.prevState;t.location.hash=s.hashName+s.queryStr}}},{key:"prepareNavigationMeta",value:function(t){t=Object.assign({},t);var n=t.sourceBlock;return n&&this.config.attractors.indexOf(n)>-1&&(t.attractToSourceBlock=!0),t}},{key:"doSaveScrollPos",value:function(t,r){return r=r||{},["forward","move"].includes(this.stateObject.stateName)&&t.isPopup&&!t.isModalPopup&&!r.attractToSourceBlock&&!n.isManualNav(r)}},{key:"processNavigation",value:function(t,i){var s=t||{hashName:"",queryStr:""};s.hashName.indexOf("#")>0&&(s.hashName="#"+s.hashName.split("#")[1]),typeof s.queryStr=="undefined"&&(s.queryStr=""),r("processNavigation: %s",s.hashName),n.retrievePopupIndicators(s),i=this.prepareNavigationMeta(i),this.addParamSpecificMeta(s,i);if(s.hashName===this.stateObject.hashName){n.doNeedScrollToHash(s,this.stateObject,i)&&n.scrollTo(n.getScrollPos(s.hashName));return}this.stateObject.isPopup&&n.closeAllOpenPopups(),s.isPopup?(n.retrieveParentBlockInfo(s),s.stateName="popup",this.navToPopup(s,i)):(s.stateName=s.stateName||"move",n.addBlockMeta(this.stateObject,s,i),this.navToBlock(s,i))}},{key:"addParamSpecificMeta",value:function(t,n){!n.history||(n.history.backwards=!!this.stateObject.prevState&&!!t.timestamp&&this.stateObject.prevState.timestamp>=t.timestamp)}},{key:"doPersistForwardState",value:function(t,r){return t.isPopup&&!t.isModalPopup&&(!r.attractToSourceBlock&&t.parentBlock!==this.lastHashBeforePopup||r.attractToSourceBlock&&r.sourceBlock!==this.lastHashBeforePopup)&&!n.isManualNav(r)}},{key:"navToBlock",value:function(t,i){r("navToBlock: %s",t.hashName),this.switchNavState(t,i),this.lastHashBeforePopup=t.hashName;if(!!i.block.doScrollToHash){var s=i.block.prevStateScrollPos||0,o=n.getScrollPos(t.hashName),u=n.doImmediateScroll(t,i);n.scrollTo(o+s,u)}}},{key:"navToPopup",value:function(t,i){r("navToPopup: %s",t.hashName);if(this.doPersistForwardState(t,i)){var s=Object.assign({},i),o={stateName:"forward",hashName:i.sourceBlock||t.parentBlock};n.addBlockMeta(t,o,s),this.navToBlock(o,s)}this.switchNavState(t,i),this.triggerPopupOpening(t,i)}},{key:"triggerPopupOpening",value:function(n,i){var s=e();n.isProductPopup&&(s=e("#rec"+n.product.recid).find('[data-product-gen-uid="'+n.product.uid+'"] [href^="#prodpopup"]')),n.isModalPopup&&(s=e('a[href^="'+n.hashName+'"]').first()),i.skipAnalytics&&s.attr("data-skip-analytics","true"),r("triggerPopupOpening: %s",s.attr("href")),i.isInitCall?setTimeout(function(){s.trigger("click")},2e3):s.trigger("click")}},{key:"navigate",value:function(t,n){this.processNavigation({hashName:t,queryStr:""},n)}},{key:"navigateBack",value:function(t){this.goToPrevNavState(t)}}],[{key:"getScrollPos",value:function(n){var r=e(n);return r.length===0&&(r=e('a[name="'+n.substr(1)+'"]')),r.length===0?0:r.offset().top+3}},{key:"scrollTo",value:function(r,i){i?t.scrollTo(0,r):e("html, body").animate({scrollTop:r},300)}},{key:"isBackwards",value:function(t){return!!t.history&&t.history.backwards}},{key:"isForwards",value:function(t){return!!t.history&&!t.history.backwards}},{key:"isManualNav",value:function(t){return n.isBackwards(t)||n.isForwards(t)}},{key:"retrievePopupIndicators",value:function(t){t.isProductPopup=t.hashName.substring(0,3)==="#!/",t.isModalPopup=t.hashName.substring(0,7)==="#popup:",t.isPopup=t.isProductPopup||t.isModalPopup}},{key:"retrieveParentBlockInfo",value:function(n){if(n.isProductPopup){var r=n.hashName.split(/[\/-]+/);n.product={recid:r[2],uid:r[3]};var i=e("#rec"+n.product.recid).find('[data-product-gen-uid="'+n.product.uid+'"]').parents(".t-rec").prevAll('[data-record-type="215"]').first();i.length>0&&(n.parentBlock="#"+i.find("a").attr("name"))}n.parentBlock||(n.parentBlock="")}},{key:"addBlockMeta",value:function(t,r,i){return i.block={doScrollToHash:n.doNeedScrollToHash(t,r,i)},n.isBackwards(i)&&(i.block.prevStateScrollPos=t.prevState.scrollPos),i}},{key:"doNeedScrollToHash",value:function(t,r,i){return(!t.isModalPopup||n.isForwards(i))&&["forward","move","init"].includes(r.stateName)||!t.isModalPopup&&i.isInitCall}},{key:"doImmediateScroll",value:function(t,n){return n.isInitCall||t.stateName==="forward"}},{key:"closeAllOpenPopups",value:function(){r("closeAllOpenPopups"),e(".t-store .t-popup_show").length>0?t_store_closePopup(!1,null,null,!0):e(".t786 .t-popup_show").length>0&&t786_closePopup(!0),e(".t390 .t-popup_show").length>0&&t390_closePopup(!0)}}]),n}();t.TildaRouter=s})(jQuery,window)