forked from shower/shower
-
Notifications
You must be signed in to change notification settings - Fork 0
/
shower.min.js
6 lines (6 loc) · 9.2 KB
/
shower.min.js
1
2
3
4
5
6
/**
* Shower HTML presentation engine: github.com/shower/shower
* @copyright 2010–2013 Vadim Makeev, pepelsbey.net
* @license MIT license: github.com/shower/shower/wiki/MIT-License
*/
window.shower=window.shower||function(e,i,t){var r={},n=e.location,s=i.body,l=[],o=[],d,a=!!(e.history&&e.history.pushState);function u(e){for(var i in e){if(e.hasOwnProperty(i)){this[i]=e[i]}}}u.prototype={getSlideNumber:function(){return this.number},isLast:function(){return r.slideList.length===this.number+1},isFinished:function(){return this.innerComplete>=this.innerLength},process:function(e){if(this.timing){this.initTimer(e);return this}this.next(e);return this},initTimer:function(e){var i=this;if(!i.timing){return false}i.stopTimer();if(i.isFinished()){d=setInterval(function(){i.stopTimer();e.next()},i.timing*(i.innerLength||1))}else{d=setInterval(function(){if(i.isFinished()){i.stopTimer();e.next()}else{i.next(e)}},i.timing)}return this},stopTimer:function(){if(d){clearInterval(d);d=false}return this},prev:function(e){var t,r=this;if(!r.hasInnerNavigation||r.isFinished()||r.innerComplete===0){e.prev();return false}t=i.getElementById(r.id).querySelectorAll(".next.active");if(!t||t.length<1){return false}if(r.innerComplete>0){r.innerComplete--;t[t.length-1].classList.remove("active")}else{e.prev()}return this},next:function(e){var t,r=this;if(!r.hasInnerNavigation||r.isFinished()){e.next();return false}if(!r.isFinished()){t=i.getElementById(r.id).querySelectorAll(".next:not(.active)");t[0].classList.add("active");r.innerComplete++}return this}};r._getData=function(e,i){return e.dataset?e.dataset[i]:e.getAttribute("data-"+i)};r.slideList=[];r.init=function(e,t){var n;e=e||".slide";t=t||"div.progress div";l=i.querySelectorAll(e);o=i.querySelector(t);for(var s=0;s<l.length;s++){if(!l[s].id){l[s].id=s+1}n=r._getData(l[s],"timing");if(n&&/^(\d{1,2}:)?\d{1,3}$/.test(n)){if(n.indexOf(":")!==-1){n=n.split(":");n=(parseInt(n[0],10)*60+parseInt(n[1],10))*1e3}else{n=parseInt(n,10)*1e3}if(n===0){n=false}}else{n=false}r.slideList.push(new u({id:l[s].id,number:s,hasInnerNavigation:null!==l[s].querySelector(".next"),timing:n,innerLength:l[s].querySelectorAll(".next").length,innerComplete:0}))}return r};r._getTransform=function(){var i=Math.max(s.clientWidth/e.innerWidth,s.clientHeight/e.innerHeight);return"scale("+1/i+")"};r._applyTransform=function(e){["WebkitTransform","MozTransform","msTransform","OTransform","transform"].forEach(function(i){s.style[i]=e});return true};r._isNumber=function(e){return!isNaN(parseFloat(e))&&isFinite(e)};r._normalizeSlideNumber=function(e){if(!r._isNumber(e)){throw new Error("Gimme slide number as Number, baby!")}if(e<0){e=0}if(e>=r.slideList.length){e=r.slideList.length-1}return e};r._getSlideIdByEl=function(e){while("BODY"!==e.nodeName&&"HTML"!==e.nodeName){if(e.classList.contains("slide")){return e.id}else{e=e.parentNode}}return""};r._checkInteractiveElement=function(e){return"A"===e.target.nodeName};r.getSlideNumber=function(e){var i=r.slideList.length-1,t;if(e===""){t=0}for(;i>=0;--i){if(e===r.slideList[i].id){t=i;break}}return t};r.go=function(e,i){var t;if(!r._isNumber(e)){throw new Error("Gimme slide number as Number, baby!")}if(!r.slideList[e]){return false}n.hash=r.getSlideHash(e);r.updateProgress(e);r.updateActiveAndVisitedSlides(e);if(r.isSlideMode()){r.showPresenterNotes(e);t=r.slideList[e];if(t.timing){t.initTimer(r)}}if(typeof i==="function"){i()}return e};r.next=function(e){var i=r.getCurrentSlideNumber(),t=r.slideList[i+1];if(!t){return false}r.go(i+1);if(typeof e==="function"){e()}return this};r._turnNextSlide=function(e){var i=r.getCurrentSlideNumber(),t=r.slideList[i];if(r.isSlideMode()){t.stopTimer();t.next(r)}else{r.go(i+1)}if(typeof e==="function"){e()}return};r.prev=r.previous=function(e){var i=r.getCurrentSlideNumber();if(i<1){return false}r.go(i-1);if(typeof e==="function"){e()}return true};r._turnPreviousSlide=function(e){var i=r.getCurrentSlideNumber(),t=r.slideList[i];t.stopTimer();if(r.isSlideMode()){t.prev(r)}else{r.go(i-1)}if(typeof e==="function"){e()}return true};r.first=function(e){var i=r.slideList[r.getCurrentSlideNumber()];i.timing&&i.stopTimer();r.go(0);if(typeof e==="function"){e()}};r.last=function(e){var i=r.slideList[r.getCurrentSlideNumber()];i.timing&&i.stopTimer();r.go(r.slideList.length-1);if(typeof e==="function"){e()}};r.enterSlideMode=function(e){var i=r.getCurrentSlideNumber();s.classList.remove("list");s.classList.add("full");if(r.isListMode()&&a){history.pushState(null,null,n.pathname+"?full"+r.getSlideHash(i))}r._applyTransform(r._getTransform());if(typeof e==="function"){e()}return true};r.enterListMode=function(e){var i;s.classList.remove("full");s.classList.add("list");r.clearPresenterNotes();if(r.isListMode()){return false}i=r.getCurrentSlideNumber();r.slideList[i].stopTimer();if(r.isSlideMode()&&a){history.pushState(null,null,n.pathname+r.getSlideHash(i))}r.scrollToSlide(i);r._applyTransform("none");if(typeof e==="function"){e()}return true};r.toggleMode=function(e){if(r.isListMode()){r.enterSlideMode()}else{r.enterListMode()}if(typeof e==="function"){e()}return true};r.getCurrentSlideNumber=function(){var e=r.slideList.length-1,i=n.hash.substr(1);for(;e>=0;--e){if(i===r.slideList[e].id){return e}}return-1};r.scrollToSlide=function(t){var n,s=false;if(!r._isNumber(t)){throw new Error("Gimme slide number as Number, baby!")}if(r.isSlideMode()){throw new Error("You can't scroll to because you in slide mode. Please, switch to list mode.")}if(-1===t){return s}if(r.slideList[t]){n=i.getElementById(r.slideList[t].id);e.scrollTo(0,n.offsetTop);s=true}else{throw new Error("There is no slide with number "+t)}return s};r.isListMode=function(){return a?!/^full.*/.test(n.search.substr(1)):s.classList.contains("list")};r.isSlideMode=function(){return a?/^full.*/.test(n.search.substr(1)):s.classList.contains("full")};r.updateProgress=function(e){if(null===o){return false}if(!r._isNumber(e)){throw new Error("Gimme slide number as Number, baby!")}o.style.width=(100/(r.slideList.length-1)*r._normalizeSlideNumber(e)).toFixed(2)+"%";return true};r.updateActiveAndVisitedSlides=function(e){var t,n,s=r.slideList.length;e=r._normalizeSlideNumber(e);if(!r._isNumber(e)){throw new Error("Gimme slide number as Number, baby!")}for(t=0;t<s;++t){n=i.getElementById(r.slideList[t].id);if(t<e){n.classList.remove("active");n.classList.add("visited")}else if(t>e){n.classList.remove("visited");n.classList.remove("active")}else{n.classList.remove("visited");n.classList.add("active")}}return true};r.clearPresenterNotes=function(){if(e.console&&e.console.clear){console.clear()}};r.showPresenterNotes=function(t){r.clearPresenterNotes();if(e.console){t=r._normalizeSlideNumber(t);var n=r.slideList[t].id,s=r.slideList[t+1]?r.slideList[t+1].id:null,l=i.getElementById(n).querySelector("footer");if(l&&l.innerHTML){console.info(l.innerHTML.replace(/\n\s+/g,"\n"))}if(s){var o=i.getElementById(s).querySelector("h2");if(o){o=o.innerHTML.replace(/^\s+|<[^>]+>/g,"");console.info("NEXT: "+o)}}}};r.getSlideHash=function(e){if(!r._isNumber(e)){throw new Error("Gimme slide number as Number, baby!")}e=r._normalizeSlideNumber(e);return"#"+r.slideList[e].id};e.addEventListener("DOMContentLoaded",function(){if(s.classList.contains("full")||r.isSlideMode()){r.go(r.getCurrentSlideNumber());r.enterSlideMode()}},false);e.addEventListener("popstate",function(){if(r.isListMode()){r.enterListMode()}else{r.enterSlideMode()}},false);e.addEventListener("resize",function(){if(r.isSlideMode()){r._applyTransform(r._getTransform())}},false);i.addEventListener("keydown",function(e){var i,t;if(e.altKey||e.ctrlKey||e.metaKey){return}i=r.getCurrentSlideNumber();t=r.slideList[i];switch(e.which){case 116:e.preventDefault();if(r.isListMode()){var n=e.shiftKey?i:0,t;r.go(n);r.enterSlideMode();r.showPresenterNotes(n);t=r.slideList[i];t.timing&&t.initTimer(r)}else{r.enterListMode()}break;case 13:if(r.isListMode()&&-1!==i){e.preventDefault();r.enterSlideMode();r.showPresenterNotes(i);if(t.timing){t.initTimer(r)}}break;case 27:if(r.isSlideMode()){e.preventDefault();r.enterListMode()}break;case 33:case 38:case 37:case 72:case 75:if(r.isSlideMode()){e.preventDefault();r._turnPreviousSlide()}break;case 34:case 40:case 39:case 76:case 74:if(r.isSlideMode()){e.preventDefault();r._turnNextSlide()}break;case 36:if(r.isSlideMode()){e.preventDefault();r.first()}break;case 35:if(r.isSlideMode()){e.preventDefault();r.last()}break;case 9:case 32:if(r.isSlideMode()){e.preventDefault();r[e.shiftKey?"_turnPreviousSlide":"_turnNextSlide"]()}break;default:}},false);r.init();i.addEventListener("click",function(e){var i=r.getSlideNumber(r._getSlideIdByEl(e.target)),t;if(r.isListMode()&&r._getSlideIdByEl(e.target)){r.go(i);r.enterSlideMode();r.showPresenterNotes(i);t=r.slideList[i];if(t.timing){t.initTimer(r)}}},false);i.addEventListener("touchstart",function(i){var t=r.getSlideNumber(r._getSlideIdByEl(i.target)),n,s;if(r._getSlideIdByEl(i.target)){if(r.isSlideMode()&&!r._checkInteractiveElement(i)){s=i.touches[0].pageX;if(s>e.innerWidth/2){r._turnNextSlide()}else{r._turnPreviousSlide()}}if(r.isListMode()){r.go(r.getSlideNumber(r._getSlideIdByEl(i.target)));r.enterSlideMode();r.showPresenterNotes(t);n=r.slideList[t];if(n.timing){n.initTimer(r)}}}},false);i.addEventListener("touchmove",function(e){if(r.isSlideMode()){e.preventDefault()}},false);return r}(this,this.document);