8 lines
13 KiB
JavaScript
8 lines
13 KiB
JavaScript
/*!
|
|
* reveal.js 1.3
|
|
* http://lab.hakim.se/reveal-js
|
|
* MIT licensed
|
|
*
|
|
* Copyright (C) 2012 Hakim El Hattab, http://hakim.se
|
|
*/
|
|
var Reveal=(function(){var HORIZONTAL_SLIDES_SELECTOR='#reveal .slides>section',VERTICAL_SLIDES_SELECTOR='#reveal .slides>section.present>section',IS_TOUCH_DEVICE=!!('ontouchstart'in window),indexh=0,indexv=0,config={controls:false,progress:false,history:false,loop:false,mouseWheel:true,rollingLinks:true,transition:'default',theme:'default',swipeDist:30},state=[],dom={},supports3DTransforms=document.body.style['perspectiveProperty']!==undefined||document.body.style['WebkitPerspective']!==undefined||document.body.style['MozPerspective']!==undefined||document.body.style['msPerspective']!==undefined||document.body.style['OPerspective']!==undefined,supports2DTransforms=document.body.style['transformProperty']!==undefined||document.body.style['WebkitTransform']!==undefined||document.body.style['MozTransform']!==undefined||document.body.style['msTransform']!==undefined||document.body.style['OTransform']!==undefined,mouseWheelTimeout=0,writeURLTimeout=0;function initialize(options){if(!supports2DTransforms&&!supports3DTransforms){document.body.setAttribute('class','no-transforms');return;}dom.wrapper=document.querySelector('#reveal');dom.progress=document.querySelector('#reveal .progress');dom.progressbar=document.querySelector('#reveal .progress span');dom.controls=document.querySelector('#reveal .controls');dom.controlsLeft=document.querySelector('#reveal .controls .left');dom.controlsRight=document.querySelector('#reveal .controls .right');dom.controlsUp=document.querySelector('#reveal .controls .up');dom.controlsDown=document.querySelector('#reveal .controls .down');document.addEventListener('keydown',onDocumentKeyDown,false);document.addEventListener('touchstart',onDocumentTouchStart,false);document.addEventListener('touchmove',onDocumentTouchMove,false);document.addEventListener('touchend',onDocumentTouchEnd,false);window.addEventListener('hashchange',onWindowHashChange,false);dom.controlsLeft.addEventListener('click',preventAndForward(navigateLeft),false);dom.controlsRight.addEventListener('click',preventAndForward(navigateRight),false);dom.controlsUp.addEventListener('click',preventAndForward(navigateUp),false);dom.controlsDown.addEventListener('click',preventAndForward(navigateDown),false);extend(config,options);if(supports3DTransforms===false){config.transition='linear';}if(config.controls){dom.controls.style.display='block';}if(config.progress){dom.progress.style.display='block';}if(config.transition!=='default'){dom.wrapper.classList.add(config.transition);}if(config.theme!=='default'){dom.wrapper.classList.add(config.theme);}if(config.mouseWheel){document.addEventListener('DOMMouseScroll',onDocumentMouseScroll,false);document.addEventListener('mousewheel',onDocumentMouseScroll,false);}if(config.rollingLinks){linkify();}readURL();if(navigator.userAgent.match(/(iphone|ipod|android)/i)){document.documentElement.style.overflow='scroll';document.body.style.height='120%';window.addEventListener('load',removeAddressBar,false);window.addEventListener('orientationchange',removeAddressBar,false);}}function extend(a,b){for(var i in b){a[i]=b[i];}}function preventAndForward(delegate){return function(event){event.preventDefault();delegate.call();}}function removeAddressBar(){setTimeout(function(){window.scrollTo(0,1);},0);}function onDocumentKeyDown(event){if(event.target.contentEditable!='inherit'||event.shiftKey||event.altKey||event.ctrlKey||event.metaKey)return;var triggered=false;switch(event.keyCode){case 80:case 33:navigatePrev();triggered=true;break;case 78:case 34:navigateNext();triggered=true;break;case 72:case 37:navigateLeft();triggered=true;break;case 76:case 39:navigateRight();triggered=true;break;case 75:case 38:navigateUp();triggered=true;break;case 74:case 40:navigateDown();triggered=true;break;case 36:navigateTo(0);triggered=true;break;case 35:navigateTo(Number.MAX_VALUE);triggered=true;break;case 32:overviewIsActive()?deactivateOverview():navigateNext();triggered=true;break;case 13:if(overviewIsActive()){deactivateOverview();triggered=true;}break;}if(triggered){event.preventDefault();}else if(event.keyCode===27&&supports3DTransforms){if(overviewIsActive()){deactivateOverview();}else{activateOverview();}event.preventDefault();}}var touchStart={}var gesture=false;function onDocumentTouchStart(event){touchStart={x:event.touches[0].clientX,y:event.touches[0].clientY};if(event.target.tagName.toLowerCase()==='a'||event.target.tagName.toLowerCase()==='img'){}else{event.preventDefault();}}function onDocumentTouchMove(event){event.preventDefault();if(!gesture){var touch={x:event.touches[0].clientX,y:event.touches[0].clientY};if((touch.x-touchStart.x)>config.swipeDist){gesture=true;navigateLeft();}else if((touch.x-touchStart.x)<-config.swipeDist){gesture=true;navigateRight();}else if((touch.y-touchStart.y)>config.swipeDist){gesture=true;navigateUp();}else if((touch.y-touchStart.y)<-config.swipeDist){gesture=true;navigateDown();}}}function onDocumentTouchEnd(event){if(!gesture){if(event.target.tagName.toLowerCase()==='a'||event.target.tagName.toLowerCase()==='img'){return;}var wt=window.innerWidth*0.3;var ht=window.innerHeight*0.3;if(touchStart.x<wt){navigateLeft();}else if(touchStart.x>window.innerWidth-wt){navigateRight();}else if(touchStart.y<ht){navigateUp();}else if(touchStart.y>window.innerHeight-ht){navigateDown();}}gesture=false;event.preventDefault();}function onDocumentMouseScroll(event){clearTimeout(mouseWheelTimeout);mouseWheelTimeout=setTimeout(function(){var delta=event.detail||-event.wheelDelta;if(delta>0){navigateNext();}else{navigatePrev();}},100);}function onWindowHashChange(event){readURL();}function linkify(){if(supports3DTransforms){var nodes=document.querySelectorAll('#reveal .slides section a:not(.image)');for(var i=0,len=nodes.length;i<len;i++){var node=nodes[i];if(node.textContent&&!node.querySelector('img')&&(!node.className||!node.classList.contains(node,'roll'))){node.classList.add('roll');node.innerHTML='<span data-title="'+node.text+'">'+node.innerHTML+'</span>';}};}}function activateOverview(){dom.wrapper.classList.add('overview');var horizontalSlides=Array.prototype.slice.call(document.querySelectorAll(HORIZONTAL_SLIDES_SELECTOR));for(var i=0,len1=horizontalSlides.length;i<len1;i++){var hslide=horizontalSlides[i],htransform='translateZ(-2500px) translate('+((i-indexh)*105)+'%, 0%)';hslide.setAttribute('data-index-h',i);hslide.style.display='block';hslide.style.WebkitTransform=htransform;hslide.style.MozTransform=htransform;hslide.style.msTransform=htransform;hslide.style.OTransform=htransform;hslide.style.transform=htransform;if(!hslide.classList.contains('stack')){hslide.addEventListener('click',onOverviewSlideClicked,true);}var verticalSlides=Array.prototype.slice.call(hslide.querySelectorAll('section'));for(var j=0,len2=verticalSlides.length;j<len2;j++){var vslide=verticalSlides[j],vtransform='translate(0%, '+((j-indexv)*105)+'%)';vslide.setAttribute('data-index-h',i);vslide.setAttribute('data-index-v',j);vslide.style.display='block';vslide.style.WebkitTransform=vtransform;vslide.style.MozTransform=vtransform;vslide.style.msTransform=vtransform;vslide.style.OTransform=vtransform;vslide.style.transform=vtransform;vslide.addEventListener('click',onOverviewSlideClicked,true);}}}function deactivateOverview(){dom.wrapper.classList.remove('overview');var slides=Array.prototype.slice.call(document.querySelectorAll('#reveal .slides section'));for(var i=0,len=slides.length;i<len;i++){var element=slides[i];element.style.WebkitTransform='';element.style.MozTransform='';element.style.msTransform='';element.style.OTransform='';element.style.transform='';element.removeEventListener('click',onOverviewSlideClicked);}slide();}function overviewIsActive(){return dom.wrapper.classList.contains('overview');}function onOverviewSlideClicked(event){if(overviewIsActive()){event.preventDefault();deactivateOverview();indexh=this.getAttribute('data-index-h');indexv=this.getAttribute('data-index-v');slide();}}function updateSlides(selector,index){var slides=Array.prototype.slice.call(document.querySelectorAll(selector)),slidesLength=slides.length;if(slidesLength){if(config.loop){index%=slidesLength;if(index<0){index=slidesLength+index;}}index=Math.max(Math.min(index,slidesLength-1),0);for(var i=0;i<slidesLength;i++){var slide=slides[i];if(overviewIsActive()===false){var distance=Math.abs((index-i)%(slidesLength-3))||0;slide.style.display=distance>3?'none':'block';}slides[i].classList.remove('past');slides[i].classList.remove('present');slides[i].classList.remove('future');if(i<index){slides[i].classList.add('past');}else if(i>index){slides[i].classList.add('future');}if(slide.querySelector('section')){slides[i].classList.add('stack');}}slides[index].classList.add('present');var slideState=slides[index].getAttribute('data-state');if(slideState){state=state.concat(slideState.split(' '));}}else{index=0;}return index;}function slide(){var stateBefore=state.concat();state.length=0;indexh=updateSlides(HORIZONTAL_SLIDES_SELECTOR,indexh);indexv=updateSlides(VERTICAL_SLIDES_SELECTOR,indexv);stateLoop:for(var i=0,len=state.length;i<len;i++){for(var j=0;j<stateBefore.length;j++){if(stateBefore[j]===state[i]){stateBefore.splice(j,1);continue stateLoop;}}document.documentElement.classList.add(state[i]);dispatchEvent(state[i]);}while(stateBefore.length){document.documentElement.classList.remove(stateBefore.pop());}if(config.progress){dom.progressbar.style.width=(indexh/(document.querySelectorAll(HORIZONTAL_SLIDES_SELECTOR).length-1))*window.innerWidth+'px';}if(overviewIsActive()){activateOverview();}updateControls();clearTimeout(writeURLTimeout);writeURLTimeout=setTimeout(writeURL,1500);dispatchEvent('slidechanged',{'indexh':indexh,'indexv':indexv});}function updateControls(){var routes=availableRoutes();[dom.controlsLeft,dom.controlsRight,dom.controlsUp,dom.controlsDown].forEach(function(node){node.classList.remove('enabled');})if(routes.left)dom.controlsLeft.classList.add('enabled');if(routes.right)dom.controlsRight.classList.add('enabled');if(routes.up)dom.controlsUp.classList.add('enabled');if(routes.down)dom.controlsDown.classList.add('enabled');}function availableRoutes(){var horizontalSlides=document.querySelectorAll(HORIZONTAL_SLIDES_SELECTOR);var verticalSlides=document.querySelectorAll(VERTICAL_SLIDES_SELECTOR);return{left:indexh>0,right:indexh<horizontalSlides.length-1,up:indexv>0,down:indexv<verticalSlides.length-1};}function readURL(){var bits=window.location.hash.slice(2).split('/');indexh=parseInt(bits[0])||0;indexv=parseInt(bits[1])||0;navigateTo(indexh,indexv);}function writeURL(){if(config.history){var url='/';if(indexh>0||indexv>0)url+=indexh;if(indexv>0)url+='/'+indexv;window.location.hash=url;}}function dispatchEvent(type,properties){var event=document.createEvent("HTMLEvents",1,2);event.initEvent(type,true,true);extend(event,properties);dom.wrapper.dispatchEvent(event);}function nextFragment(){if(document.querySelector(VERTICAL_SLIDES_SELECTOR+'.present')){var verticalFragments=document.querySelectorAll(VERTICAL_SLIDES_SELECTOR+'.present .fragment:not(.visible)');if(verticalFragments.length){verticalFragments[0].classList.add('visible');return true;}}else{var horizontalFragments=document.querySelectorAll(HORIZONTAL_SLIDES_SELECTOR+'.present .fragment:not(.visible)');if(horizontalFragments.length){horizontalFragments[0].classList.add('visible');return true;}}return false;}function previousFragment(){if(document.querySelector(VERTICAL_SLIDES_SELECTOR+'.present')){var verticalFragments=document.querySelectorAll(VERTICAL_SLIDES_SELECTOR+'.present .fragment.visible');if(verticalFragments.length){verticalFragments[verticalFragments.length-1].classList.remove('visible');return true;}}else{var horizontalFragments=document.querySelectorAll(HORIZONTAL_SLIDES_SELECTOR+'.present .fragment.visible');if(horizontalFragments.length){horizontalFragments[horizontalFragments.length-1].classList.remove('visible');return true;}}return false;}function navigateTo(h,v){indexh=h===undefined?indexh:h;indexv=v===undefined?indexv:v;slide();}function navigateLeft(){if(overviewIsActive()||previousFragment()===false){indexh--;indexv=0;slide();}}function navigateRight(){if(overviewIsActive()||nextFragment()===false){indexh++;indexv=0;slide();}}function navigateUp(){if(overviewIsActive()||previousFragment()===false){indexv--;slide();}}function navigateDown(){if(overviewIsActive()||nextFragment()===false){indexv++;slide();}}function navigatePrev(){if(previousFragment()===false){if(availableRoutes().up){navigateUp();}else{var previousSlide=document.querySelector('#reveal .slides>section.past:nth-child('+indexh+')');if(previousSlide){indexv=(previousSlide.querySelectorAll('section').length+1)||0;indexh--;slide();}}}}function navigateNext(){if(nextFragment()===false){availableRoutes().down?navigateDown():navigateRight();}}return{initialize:initialize,navigateTo:navigateTo,navigateLeft:navigateLeft,navigateRight:navigateRight,navigateUp:navigateUp,navigateDown:navigateDown,addEventListener:function(type,listener,useCapture){(dom.wrapper||document.querySelector('#reveal')).addEventListener(type,listener,useCapture);},removeEventListener:function(type,listener,useCapture){(dom.wrapper||document.querySelector('#reveal')).removeEventListener(type,listener,useCapture);}};})(); |