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