-
Notifications
You must be signed in to change notification settings - Fork 0
/
sitepage.min.js
6 lines (6 loc) · 9.58 KB
/
sitepage.min.js
1
2
3
4
5
6
/*!
sitepage - v2.0.0
* https://github.com/pixelbyaj/SitePage
* @author Abhishek Joshi
* @license MIT*/
var __assign=this&&this.__assign||function(){return(__assign=Object.assign||function(e){for(var t,n=1,a=arguments.length;n<a;n++)for(var i in t=arguments[n])Object.prototype.hasOwnProperty.call(t,i)&&(e[i]=t[i]);return e}).apply(this,arguments)},__spreadArrays=this&&this.__spreadArrays||function(){for(var e=0,t=0,n=arguments.length;t<n;t++)e+=arguments[t].length;var a=Array(e),i=0;for(t=0;t<n;t++)for(var r=arguments[t],o=0,l=r.length;o<l;o++,i++)a[i]=r[o];return a},SitePage=function(l,e){if(!l)throw"Page element not found";var c=document,d=c.getElementById(l);if(!d)throw"Page element not found";var u={brandName:"sitePage",backgroundColor:"#fc6c7c",menuId:"sp-menu",anchors:!0,verticalAlignMiddle:!0,sections:[],navigation:"vertical",autoScrolling:!0,keyboardNavigation:!0,scrollbar:!1,transitionSpeed:1e3,easing:"ease",sameurl:!0,pageTransitionStart:function(e,t){},pageTransitionEnd:function(e){}};e&&(u=__assign(__assign({},u),e));var n,o,r,v=2,s=[],h=0,g=[],a=0,p=!0,f={setInitialStyle:function(){d.style.transform="translate3d(0px, 0px, 0px)",d.classList.add("sp-wrapper"),c.querySelector("body").style.backgroundColor=u.backgroundColor},setSectionClass:function(e){e.classList.add("sp-section")},setSectionHeight:function(e){e.style.height=window.innerHeight+"px"},setSectionHorizontal:function(e){e.style.width=100*g.length+"%",e.classList.add("sp-floatLeft"),e.querySelectorAll(".section").forEach(function(e){e.classList.add("sp-floatLeft"),e.style.width=100/g.length+"%"})},getCellElement:function(e,t){var n,a=c.createElement("div");return a.setAttribute("class","sp-cell"),u.verticalAlignMiddle&&(void 0!==t&&!t||e.push.apply(e,["align-middle","text-center"])),e&&(n=a.classList).add.apply(n,e),f.setSectionHeight(a),a},setBackgroundColor:function(e,t){e.style.backgroundColor=t},getBrandName:function(e,t){var n,a=c.createElement("span");(n=a.classList).add.apply(n,e);var i=c.createTextNode(t);return a.appendChild(i),a},getNavigationLink:function(e,t,n){var a,i=c.createElement("li");i.classList.add("nav-item");var r=c.createElement("a");(a=r.classList).add.apply(a,e),r.removeEventListener("click",m.navigationClick),u.sameurl?(r.setAttribute("href","javascript:void(0)"),r.setAttribute("data-href",n),r.addEventListener("click",m.navigationClick)):r.setAttribute("href","#"+n);var o=c.createTextNode(t);return r.appendChild(o),i.appendChild(r),i},setNavigationMenu:function(){var e,t,n,a=c.createElement("nav");(e=a.classList).add.apply(e,["navbar","fixed-top","navbar-expand","navbar-dark","flex-column","flex-md-row","bd-navbar"]);var i=f.getBrandName(["navbar-brand","mb-0","h1"],u.brandName);a.appendChild(i);var r=c.createElement("div");r.setAttribute("id","navbarNav"),r.classList.add("navbar-nav-scroll");var o=c.createElement("ul");return o.setAttribute("id",u.menuId),(t=o.classList).add.apply(t,["navbar-nav","bd-navbar-nav","flex-row"]),r.appendChild(o),a.appendChild(r),null!==(n=c.querySelector("body"))&&void 0!==n&&n.insertBefore(a,c.querySelector("#"+l)),o},setSection:function(e,t){var n=c.createElement("div");if(n.setAttribute("id","section-"+t),n.classList.add("section"),e.active&&n.classList.add("active"),e.templateUrl){var a='<sp-include url="'+e.templateUrl+'"/>';n.innerHTML=a}else e.template&&(n.innerHTML=e.template);return f.setSectionClass(n),f.setSectionHeight(n),n},fetchView:function(){var e=o.querySelector("sp-include");if(e){var t=e.getAttribute("url");fetch(t).then(function(e){return e.text()}).then(function(e){o.querySelector(".sp-cell").innerHTML=e})}}},w={scrollPageUp:function(){var e="";0<n?e=g[--n]:u.autoScrolling&&(n=g.length-1,e=g[n]),""!==e?w.scrollToSection(e,2):p=!0},scrollPageRight:function(){var e="";0<n?e=g[--n]:u.autoScrolling&&(n=g.length-1,e=g[n]),""!==e?w.scrollToSection(e,1):p=!0},scrollPageDown:function(){var e="";n<g.length-1?e=g[++n]:u.autoScrolling&&(e=g[n=0]),""!==e?w.scrollToSection(e,2):p=!0},scrollPageLeft:function(){var e="";n<g.length-1?e=g[++n]:u.autoScrolling&&(e=g[n=0]),""!==e?w.scrollToSection(e,1):p=!0},scrollToSection:function(e,t){if(o=c.querySelector("[data-anchor='"+e+"']"),n=g.indexOf(e),o){switch(f.fetchView(),d.style.transition="all "+u.transitionSpeed+"ms "+u.easing+" 0s",t){case 1:a=n*window.innerWidth,d.style.transform="translate3d(-"+a+"px, 0px, 0px)";break;case 2:a=n*window.innerHeight,0<o.offsetTop&&(a=a>o.offsetTop?a:o.offsetTop),d.style.transform="translate3d(0px, -"+a+"px, 0px)"}u.sameurl||(location.hash=e)}}},m={keyDown:function(e){switch(e.which){case 37:p&&"horizontal"===u.navigation&&(p=!1,w.scrollPageRight());break;case 38:p&&"vertical"===u.navigation&&(p=!1,w.scrollPageUp());break;case 39:p&&"horizontal"===u.navigation&&(p=!1,w.scrollPageLeft());break;case 40:p&&"vertical"===u.navigation&&(p=!1,w.scrollPageDown())}},mouseWheel:function(e){s.push(h);var t=(e=e||window.event).wheelDelta||-e.deltaY||-e.detail,n=Math.max(-1,Math.min(1,t)),a=void 0!==e.wheelDeltaX||void 0!==e.deltaX,i=Math.abs(e.wheelDeltaX)<Math.abs(e.wheelDelta)||Math.abs(e.deltaX)<Math.abs(e.deltaY)||!a;return u.scrollbar&&e.preventDefault(),clearTimeout(r),r=setTimeout(function(){if(p&&h===s.length){p=!1,s=[],h=0,clearInterval(r);var e=b.getAverage(s,10);b.getAverage(s,70)<=e&&i&&(n<0?"vertical"===u.navigation?w.scrollPageDown():w.scrollPageLeft():"vertical"===u.navigation?w.scrollPageUp():w.scrollPageRight())}},100),h=s.length,!1},windowSize:function(){var t;document.querySelectorAll(".section").forEach(function(e){f.setSectionHeight(e),f.setSectionHeight(e.querySelector(".sp-cell")),e.classList.contains("active")&&(t=e.getAttribute("data-anchor"))}),w.scrollToSection(t,v)},hashChange:function(){var e;if(!u.sameurl){var t=null===(e=location.hash)||void 0===e?void 0:e.replace("#","");w.scrollToSection(t,v)}},transitionStart:function(e){var t,n,a=c.querySelector(".section.active");null!=a&&a.classList.remove("active"),u.pageTransitionStart instanceof Function&&u.pageTransitionStart(a,o);var i=null==a?void 0:a.getAttribute("data-anchor"),r=null==o?void 0:o.getAttribute("data-anchor");null!==(t=c.querySelector(".nav-link[href='#"+i+"']"))&&void 0!==t&&t.classList.remove("active"),null!==(n=c.querySelector(".nav-link[href='#"+r+"']"))&&void 0!==n&&n.classList.add("active")},transitionEnd:function(e){null!=o&&o.classList.add("active"),p=!0,u.pageTransitionEnd instanceof Function&&u.pageTransitionEnd(o)},swipeUp:function(){p&&(p=!1,w.scrollPageDown())},swipeDown:function(){p&&(p=!1,w.scrollPageUp())},swipeLeft:function(){p&&(p=!1,w.scrollPageLeft())},swipeRight:function(){p&&(p=!1,w.scrollPageRight())},navigationClick:function(e){var t=e.target.getAttribute("data-href");w.scrollToSection(t,v)}},b={initSections:function(){var e,t;f.setInitialStyle();var s=f.setNavigationMenu();u.sections.forEach(function(e,t){var n="page"+(t+1),a=f.setSection(e,t+1);a.setAttribute("data-anchor",n);var i=e.sectionClass||[],r=f.getCellElement(i,e.verticalAlignMiddle);if(r.innerHTML=a.innerHTML,a.innerHTML="",a.appendChild(r),d.appendChild(a),g.push(n),u.anchors){var o=["nav-link","text-nowrap"];e.anchorClass&&(o=__spreadArrays(o,e.anchorClass));var l=f.getNavigationLink(o,e.anchor,n);s.appendChild(l)}e.backgroundColor&&f.setBackgroundColor(a,e.backgroundColor)}),"horizontal"===u.navigation.toLowerCase()&&(f.setSectionHorizontal(d),v=1);var n=g[0];if(u.sameurl){var a=document.querySelector(".section.active");null!==a&&(n=a.getAttribute("data-anchor"))}else{var i=null===(e=location.hash)||void 0===e?void 0:e.replace("#","");i&&(n=i)}w.scrollToSection(n,v),null==o||o.getAttribute("data-anchor"),null!==(t=c.querySelector(".nav-link[href='#"+n+"']"))&&void 0!==t&&t.classList.add("active"),b.addEventListeners(d)},addEventListeners:function(e){u.keyboardNavigation&&(document.removeEventListener("keydown",m.keyDown),document.addEventListener("keydown",m.keyDown)),document.removeEventListener("wheel",m.mouseWheel),document.addEventListener("wheel",m.mouseWheel),window.removeEventListener("resize",m.windowSize),window.addEventListener("resize",m.windowSize),e.removeEventListener("transitionstart",m.transitionStart),e.addEventListener("transitionstart",m.transitionStart),e.removeEventListener("transitionend",m.transitionEnd),e.addEventListener("transitionend",m.transitionEnd),1==v?(document.addEventListener("swiped-left",m.swipeLeft),document.addEventListener("swiped-right",m.swipeRight)):(document.addEventListener("swiped-up",m.swipeUp),document.addEventListener("swiped-down",m.swipeDown)),u.sameurl||window.addEventListener("hashchange",m.hashChange)},getAverage:function(e,t){for(var n=0,a=e.slice(Math.max(e.length-t,1)),i=0;i<a.length;i++)n+=a[i];return Math.ceil(n/t)}};b.initSections()};!function(e,a){"use strict";"function"!=typeof e.CustomEvent&&(e.CustomEvent=function(e,t){t=t||{bubbles:!1,cancelable:!1,detail:void 0};var n=a.createEvent("CustomEvent");return n.initCustomEvent(e,t.bubbles,t.cancelable,t.detail),n},e.CustomEvent.prototype=e.Event.prototype),a.addEventListener("touchstart",function(e){"true"!==e.target.getAttribute("data-swipe-ignore")&&(d=e.target,c=Date.now(),r=e.touches[0].clientX,o=e.touches[0].clientY,s=l=0)},!1),a.addEventListener("touchmove",function(e){if(r&&o){var t=e.touches[0].clientX,n=e.touches[0].clientY;l=r-t,s=o-n}},!1),a.addEventListener("touchend",function(e){if(d===e.target){var t=parseInt(d.getAttribute("data-swipe-threshold")||"20",10),n=parseInt(d.getAttribute("data-swipe-timeout")||"500",10),a=Date.now()-c,i="";Math.abs(l)>Math.abs(s)?Math.abs(l)>t&&a<n&&(i=0<l?"swiped-left":"swiped-right"):Math.abs(s)>t&&a<n&&(i=0<s?"swiped-up":"swiped-down"),""!==i&&d.dispatchEvent(new CustomEvent(i,{bubbles:!0,cancelable:!0})),c=o=r=null}},!1);var r=null,o=null,l=null,s=null,c=null,d=null}(window,document);