reveal.js 69 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577
  1. /*!
  2. * reveal.js
  3. * http://lab.hakim.se/reveal-js
  4. * MIT licensed
  5. *
  6. * Copyright (C) 2013 Hakim El Hattab, http://hakim.se
  7. */
  8. var Reveal = (function(){
  9. 'use strict';
  10. var SLIDES_SELECTOR = '.reveal .slides section',
  11. HORIZONTAL_SLIDES_SELECTOR = '.reveal .slides>section',
  12. VERTICAL_SLIDES_SELECTOR = '.reveal .slides>section.present>section',
  13. HOME_SLIDE_SELECTOR = '.reveal .slides>section:first-child',
  14. // Configurations defaults, can be overridden at initialization time
  15. config = {
  16. // The "normal" size of the presentation, aspect ratio will be preserved
  17. // when the presentation is scaled to fit different resolutions
  18. width: 960,
  19. height: 700,
  20. // Factor of the display size that should remain empty around the content
  21. margin: 0.1,
  22. // Bounds for smallest/largest possible scale to apply to content
  23. minScale: 0.2,
  24. maxScale: 1.0,
  25. // Display controls in the bottom right corner
  26. controls: true,
  27. // Display a presentation progress bar
  28. progress: true,
  29. // Push each slide change to the browser history
  30. history: false,
  31. // Enable keyboard shortcuts for navigation
  32. keyboard: true,
  33. // Enable the slide overview mode
  34. overview: true,
  35. // Vertical centring of slides
  36. center: true,
  37. // Enables touch navigation on devices with touch input
  38. touch: true,
  39. // Loop the presentation
  40. loop: false,
  41. // Change the presentation direction to be RTL
  42. rtl: false,
  43. // Turns fragments on and off globally
  44. fragments: true,
  45. // Number of milliseconds between automatically proceeding to the
  46. // next slide, disabled when set to 0, this value can be overwritten
  47. // by using a data-autoslide attribute on your slides
  48. autoSlide: 0,
  49. // Enable slide navigation via mouse wheel
  50. mouseWheel: false,
  51. // Apply a 3D roll to links on hover
  52. rollingLinks: true,
  53. // Opens links in an iframe preview overlay
  54. previewLinks: false,
  55. // Theme (see /css/theme)
  56. theme: null,
  57. // Transition style
  58. transition: 'default', // default/cube/page/concave/zoom/linear/fade/none
  59. // Transition speed
  60. transitionSpeed: 'default', // default/fast/slow
  61. // Transition style for full page slide backgrounds
  62. backgroundTransition: 'default', // default/linear
  63. // Script dependencies to load
  64. dependencies: []
  65. },
  66. // The current auto-slide duration
  67. autoSlide = 0,
  68. // The horizontal and vertical index of the currently active slide
  69. indexh = 0,
  70. indexv = 0,
  71. // The previous and current slide HTML elements
  72. previousSlide,
  73. currentSlide,
  74. // Slides may hold a data-state attribute which we pick up and apply
  75. // as a class to the body. This list contains the combined state of
  76. // all current slides.
  77. state = [],
  78. // The current scale of the presentation (see width/height config)
  79. scale = 1,
  80. // Cached references to DOM elements
  81. dom = {},
  82. // Detect support for CSS 3D transforms
  83. supports3DTransforms = 'WebkitPerspective' in document.body.style ||
  84. 'MozPerspective' in document.body.style ||
  85. 'msPerspective' in document.body.style ||
  86. 'OPerspective' in document.body.style ||
  87. 'perspective' in document.body.style,
  88. // Detect support for CSS 2D transforms
  89. supports2DTransforms = 'WebkitTransform' in document.body.style ||
  90. 'MozTransform' in document.body.style ||
  91. 'msTransform' in document.body.style ||
  92. 'OTransform' in document.body.style ||
  93. 'transform' in document.body.style,
  94. // Throttles mouse wheel navigation
  95. lastMouseWheelStep = 0,
  96. // An interval used to automatically move on to the next slide
  97. autoSlideTimeout = 0,
  98. // Delays updates to the URL due to a Chrome thumbnailer bug
  99. writeURLTimeout = 0,
  100. // A delay used to activate the overview mode
  101. activateOverviewTimeout = 0,
  102. // A delay used to deactivate the overview mode
  103. deactivateOverviewTimeout = 0,
  104. // Flags if the interaction event listeners are bound
  105. eventsAreBound = false,
  106. // Holds information about the currently ongoing touch input
  107. touch = {
  108. startX: 0,
  109. startY: 0,
  110. startSpan: 0,
  111. startCount: 0,
  112. handled: false,
  113. threshold: 80
  114. };
  115. /**
  116. * Starts up the presentation if the client is capable.
  117. */
  118. function initialize( options ) {
  119. if( !supports2DTransforms && !supports3DTransforms ) {
  120. document.body.setAttribute( 'class', 'no-transforms' );
  121. // If the browser doesn't support core features we won't be
  122. // using JavaScript to control the presentation
  123. return;
  124. }
  125. // Force a layout when the whole page, incl fonts, has loaded
  126. window.addEventListener( 'load', layout, false );
  127. // Copy options over to our config object
  128. extend( config, options );
  129. // Hide the address bar in mobile browsers
  130. hideAddressBar();
  131. // Loads the dependencies and continues to #start() once done
  132. load();
  133. }
  134. /**
  135. * Finds and stores references to DOM elements which are
  136. * required by the presentation. If a required element is
  137. * not found, it is created.
  138. */
  139. function setupDOM() {
  140. // Cache references to key DOM elements
  141. dom.theme = document.querySelector( '#theme' );
  142. dom.wrapper = document.querySelector( '.reveal' );
  143. dom.slides = document.querySelector( '.reveal .slides' );
  144. // Background element
  145. if( !document.querySelector( '.reveal .backgrounds' ) ) {
  146. dom.background = document.createElement( 'div' );
  147. dom.background.classList.add( 'backgrounds' );
  148. dom.wrapper.appendChild( dom.background );
  149. }
  150. // Progress bar
  151. if( !dom.wrapper.querySelector( '.progress' ) ) {
  152. var progressElement = document.createElement( 'div' );
  153. progressElement.classList.add( 'progress' );
  154. progressElement.innerHTML = '<span></span>';
  155. dom.wrapper.appendChild( progressElement );
  156. }
  157. // Arrow controls
  158. if( !dom.wrapper.querySelector( '.controls' ) ) {
  159. var controlsElement = document.createElement( 'aside' );
  160. controlsElement.classList.add( 'controls' );
  161. controlsElement.innerHTML = '<div class="navigate-left"></div>' +
  162. '<div class="navigate-right"></div>' +
  163. '<div class="navigate-up"></div>' +
  164. '<div class="navigate-down"></div>';
  165. dom.wrapper.appendChild( controlsElement );
  166. }
  167. // State background element [DEPRECATED]
  168. if( !dom.wrapper.querySelector( '.state-background' ) ) {
  169. var stateBackgroundElement = document.createElement( 'div' );
  170. stateBackgroundElement.classList.add( 'state-background' );
  171. dom.wrapper.appendChild( stateBackgroundElement );
  172. }
  173. // Overlay graphic which is displayed during the paused mode
  174. if( !dom.wrapper.querySelector( '.pause-overlay' ) ) {
  175. var pausedElement = document.createElement( 'div' );
  176. pausedElement.classList.add( 'pause-overlay' );
  177. dom.wrapper.appendChild( pausedElement );
  178. }
  179. // Cache references to elements
  180. dom.progress = document.querySelector( '.reveal .progress' );
  181. dom.progressbar = document.querySelector( '.reveal .progress span' );
  182. if ( config.controls ) {
  183. dom.controls = document.querySelector( '.reveal .controls' );
  184. // There can be multiple instances of controls throughout the page
  185. dom.controlsLeft = toArray( document.querySelectorAll( '.navigate-left' ) );
  186. dom.controlsRight = toArray( document.querySelectorAll( '.navigate-right' ) );
  187. dom.controlsUp = toArray( document.querySelectorAll( '.navigate-up' ) );
  188. dom.controlsDown = toArray( document.querySelectorAll( '.navigate-down' ) );
  189. dom.controlsPrev = toArray( document.querySelectorAll( '.navigate-prev' ) );
  190. dom.controlsNext = toArray( document.querySelectorAll( '.navigate-next' ) );
  191. }
  192. }
  193. /**
  194. * Creates the slide background elements and appends them
  195. * to the background container. One element is created per
  196. * slide no matter if the given slide has visible background.
  197. */
  198. function createBackgrounds() {
  199. if( isPrintingPDF() ) {
  200. document.body.classList.add( 'print-pdf' );
  201. }
  202. // Clear prior backgrounds
  203. dom.background.innerHTML = '';
  204. dom.background.classList.add( 'no-transition' );
  205. // Helper method for creating a background element for the
  206. // given slide
  207. function _createBackground( slide, container ) {
  208. var data = {
  209. background: slide.getAttribute( 'data-background' ),
  210. backgroundSize: slide.getAttribute( 'data-background-size' ),
  211. backgroundImage: slide.getAttribute( 'data-background-image' ),
  212. backgroundColor: slide.getAttribute( 'data-background-color' ),
  213. backgroundRepeat: slide.getAttribute( 'data-background-repeat' ),
  214. backgroundPosition: slide.getAttribute( 'data-background-position' ),
  215. backgroundTransition: slide.getAttribute( 'data-background-transition' )
  216. };
  217. var element = document.createElement( 'div' );
  218. element.className = 'slide-background';
  219. if( data.background ) {
  220. // Auto-wrap image urls in url(...)
  221. if( /^(http|file|\/\/)/gi.test( data.background ) || /\.(png|jpg|jpeg|gif|bmp)$/gi.test( data.background ) ) {
  222. element.style.backgroundImage = 'url('+ data.background +')';
  223. }
  224. else {
  225. element.style.background = data.background;
  226. }
  227. }
  228. // Additional and optional background properties
  229. if( data.backgroundSize ) element.style.backgroundSize = data.backgroundSize;
  230. if( data.backgroundImage ) element.style.backgroundImage = 'url("' + data.backgroundImage + '")';
  231. if( data.backgroundColor ) element.style.backgroundColor = data.backgroundColor;
  232. if( data.backgroundRepeat ) element.style.backgroundRepeat = data.backgroundRepeat;
  233. if( data.backgroundPosition ) element.style.backgroundPosition = data.backgroundPosition;
  234. if( data.backgroundTransition ) element.setAttribute( 'data-background-transition', data.backgroundTransition );
  235. container.appendChild( element );
  236. return element;
  237. }
  238. // Iterate over all horizontal slides
  239. toArray( document.querySelectorAll( HORIZONTAL_SLIDES_SELECTOR ) ).forEach( function( slideh ) {
  240. var backgroundStack;
  241. if( isPrintingPDF() ) {
  242. backgroundStack = _createBackground( slideh, slideh );
  243. }
  244. else {
  245. backgroundStack = _createBackground( slideh, dom.background );
  246. }
  247. // Iterate over all vertical slides
  248. toArray( slideh.querySelectorAll( 'section' ) ).forEach( function( slidev ) {
  249. if( isPrintingPDF() ) {
  250. _createBackground( slidev, slidev );
  251. }
  252. else {
  253. _createBackground( slidev, backgroundStack );
  254. }
  255. } );
  256. } );
  257. }
  258. /**
  259. * Hides the address bar if we're on a mobile device.
  260. */
  261. function hideAddressBar() {
  262. if( /iphone|ipod|android/gi.test( navigator.userAgent ) && !/crios/gi.test( navigator.userAgent ) ) {
  263. // Events that should trigger the address bar to hide
  264. window.addEventListener( 'load', removeAddressBar, false );
  265. window.addEventListener( 'orientationchange', removeAddressBar, false );
  266. }
  267. }
  268. /**
  269. * Loads the dependencies of reveal.js. Dependencies are
  270. * defined via the configuration option 'dependencies'
  271. * and will be loaded prior to starting/binding reveal.js.
  272. * Some dependencies may have an 'async' flag, if so they
  273. * will load after reveal.js has been started up.
  274. */
  275. function load() {
  276. var scripts = [],
  277. scriptsAsync = [];
  278. for( var i = 0, len = config.dependencies.length; i < len; i++ ) {
  279. var s = config.dependencies[i];
  280. // Load if there's no condition or the condition is truthy
  281. if( !s.condition || s.condition() ) {
  282. if( s.async ) {
  283. scriptsAsync.push( s.src );
  284. }
  285. else {
  286. scripts.push( s.src );
  287. }
  288. // Extension may contain callback functions
  289. if( typeof s.callback === 'function' ) {
  290. head.ready( s.src.match( /([\w\d_\-]*)\.?js$|[^\\\/]*$/i )[0], s.callback );
  291. }
  292. }
  293. }
  294. // Called once synchronous scripts finish loading
  295. function proceed() {
  296. if( scriptsAsync.length ) {
  297. // Load asynchronous scripts
  298. head.js.apply( null, scriptsAsync );
  299. }
  300. start();
  301. }
  302. if( scripts.length ) {
  303. head.ready( proceed );
  304. // Load synchronous scripts
  305. head.js.apply( null, scripts );
  306. }
  307. else {
  308. proceed();
  309. }
  310. }
  311. /**
  312. * Starts up reveal.js by binding input events and navigating
  313. * to the current URL deeplink if there is one.
  314. */
  315. function start() {
  316. // Make sure we've got all the DOM elements we need
  317. setupDOM();
  318. // Updates the presentation to match the current configuration values
  319. configure();
  320. // Read the initial hash
  321. readURL();
  322. // Notify listeners that the presentation is ready but use a 1ms
  323. // timeout to ensure it's not fired synchronously after #initialize()
  324. setTimeout( function() {
  325. dispatchEvent( 'ready', {
  326. 'indexh': indexh,
  327. 'indexv': indexv,
  328. 'currentSlide': currentSlide
  329. } );
  330. }, 1 );
  331. }
  332. /**
  333. * Applies the configuration settings from the config
  334. * object. May be called multiple times.
  335. */
  336. function configure( options ) {
  337. dom.wrapper.classList.remove( config.transition );
  338. // New config options may be passed when this method
  339. // is invoked through the API after initialization
  340. if( typeof options === 'object' ) extend( config, options );
  341. // Force linear transition based on browser capabilities
  342. if( supports3DTransforms === false ) config.transition = 'linear';
  343. dom.wrapper.classList.add( config.transition );
  344. dom.wrapper.setAttribute( 'data-transition-speed', config.transitionSpeed );
  345. dom.wrapper.setAttribute( 'data-background-transition', config.backgroundTransition );
  346. if( dom.controls ) {
  347. dom.controls.style.display = ( config.controls && dom.controls ) ? 'block' : 'none';
  348. }
  349. if( dom.progress ) {
  350. dom.progress.style.display = ( config.progress && dom.progress ) ? 'block' : 'none';
  351. }
  352. if( config.rtl ) {
  353. dom.wrapper.classList.add( 'rtl' );
  354. }
  355. else {
  356. dom.wrapper.classList.remove( 'rtl' );
  357. }
  358. if( config.center ) {
  359. dom.wrapper.classList.add( 'center' );
  360. }
  361. else {
  362. dom.wrapper.classList.remove( 'center' );
  363. }
  364. if( config.mouseWheel ) {
  365. document.addEventListener( 'DOMMouseScroll', onDocumentMouseScroll, false ); // FF
  366. document.addEventListener( 'mousewheel', onDocumentMouseScroll, false );
  367. }
  368. else {
  369. document.removeEventListener( 'DOMMouseScroll', onDocumentMouseScroll, false ); // FF
  370. document.removeEventListener( 'mousewheel', onDocumentMouseScroll, false );
  371. }
  372. // Rolling 3D links
  373. if( config.rollingLinks ) {
  374. enableRollingLinks();
  375. }
  376. else {
  377. disableRollingLinks();
  378. }
  379. // Iframe link previews
  380. if( config.previewLinks ) {
  381. enablePreviewLinks();
  382. }
  383. else {
  384. disablePreviewLinks();
  385. enablePreviewLinks( '[data-preview-link]' );
  386. }
  387. // Load the theme in the config, if it's not already loaded
  388. if( config.theme && dom.theme ) {
  389. var themeURL = dom.theme.getAttribute( 'href' );
  390. var themeFinder = /[^\/]*?(?=\.css)/;
  391. var themeName = themeURL.match(themeFinder)[0];
  392. if( config.theme !== themeName ) {
  393. themeURL = themeURL.replace(themeFinder, config.theme);
  394. dom.theme.setAttribute( 'href', themeURL );
  395. }
  396. }
  397. sync();
  398. }
  399. /**
  400. * Binds all event listeners.
  401. */
  402. function addEventListeners() {
  403. eventsAreBound = true;
  404. window.addEventListener( 'hashchange', onWindowHashChange, false );
  405. window.addEventListener( 'resize', onWindowResize, false );
  406. if( config.touch ) {
  407. dom.wrapper.addEventListener( 'touchstart', onTouchStart, false );
  408. dom.wrapper.addEventListener( 'touchmove', onTouchMove, false );
  409. dom.wrapper.addEventListener( 'touchend', onTouchEnd, false );
  410. // Support pointer-style touch interaction as well
  411. if( window.navigator.msPointerEnabled ) {
  412. dom.wrapper.addEventListener( 'MSPointerDown', onPointerDown, false );
  413. dom.wrapper.addEventListener( 'MSPointerMove', onPointerMove, false );
  414. dom.wrapper.addEventListener( 'MSPointerUp', onPointerUp, false );
  415. }
  416. }
  417. if( config.keyboard ) {
  418. document.addEventListener( 'keydown', onDocumentKeyDown, false );
  419. }
  420. if ( config.progress && dom.progress ) {
  421. dom.progress.addEventListener( 'click', onProgressClicked, false );
  422. }
  423. if ( config.controls && dom.controls ) {
  424. [ 'touchstart', 'click' ].forEach( function( eventName ) {
  425. dom.controlsLeft.forEach( function( el ) { el.addEventListener( eventName, onNavigateLeftClicked, false ); } );
  426. dom.controlsRight.forEach( function( el ) { el.addEventListener( eventName, onNavigateRightClicked, false ); } );
  427. dom.controlsUp.forEach( function( el ) { el.addEventListener( eventName, onNavigateUpClicked, false ); } );
  428. dom.controlsDown.forEach( function( el ) { el.addEventListener( eventName, onNavigateDownClicked, false ); } );
  429. dom.controlsPrev.forEach( function( el ) { el.addEventListener( eventName, onNavigatePrevClicked, false ); } );
  430. dom.controlsNext.forEach( function( el ) { el.addEventListener( eventName, onNavigateNextClicked, false ); } );
  431. } );
  432. }
  433. }
  434. /**
  435. * Unbinds all event listeners.
  436. */
  437. function removeEventListeners() {
  438. eventsAreBound = false;
  439. document.removeEventListener( 'keydown', onDocumentKeyDown, false );
  440. window.removeEventListener( 'hashchange', onWindowHashChange, false );
  441. window.removeEventListener( 'resize', onWindowResize, false );
  442. dom.wrapper.removeEventListener( 'touchstart', onTouchStart, false );
  443. dom.wrapper.removeEventListener( 'touchmove', onTouchMove, false );
  444. dom.wrapper.removeEventListener( 'touchend', onTouchEnd, false );
  445. if( window.navigator.msPointerEnabled ) {
  446. dom.wrapper.removeEventListener( 'MSPointerDown', onPointerDown, false );
  447. dom.wrapper.removeEventListener( 'MSPointerMove', onPointerMove, false );
  448. dom.wrapper.removeEventListener( 'MSPointerUp', onPointerUp, false );
  449. }
  450. if ( config.progress && dom.progress ) {
  451. dom.progress.removeEventListener( 'click', onProgressClicked, false );
  452. }
  453. if ( config.controls && dom.controls ) {
  454. [ 'touchstart', 'click' ].forEach( function( eventName ) {
  455. dom.controlsLeft.forEach( function( el ) { el.removeEventListener( eventName, onNavigateLeftClicked, false ); } );
  456. dom.controlsRight.forEach( function( el ) { el.removeEventListener( eventName, onNavigateRightClicked, false ); } );
  457. dom.controlsUp.forEach( function( el ) { el.removeEventListener( eventName, onNavigateUpClicked, false ); } );
  458. dom.controlsDown.forEach( function( el ) { el.removeEventListener( eventName, onNavigateDownClicked, false ); } );
  459. dom.controlsPrev.forEach( function( el ) { el.removeEventListener( eventName, onNavigatePrevClicked, false ); } );
  460. dom.controlsNext.forEach( function( el ) { el.removeEventListener( eventName, onNavigateNextClicked, false ); } );
  461. } );
  462. }
  463. }
  464. /**
  465. * Extend object a with the properties of object b.
  466. * If there's a conflict, object b takes precedence.
  467. */
  468. function extend( a, b ) {
  469. for( var i in b ) {
  470. a[ i ] = b[ i ];
  471. }
  472. }
  473. /**
  474. * Converts the target object to an array.
  475. */
  476. function toArray( o ) {
  477. return Array.prototype.slice.call( o );
  478. }
  479. /**
  480. * Measures the distance in pixels between point a
  481. * and point b.
  482. *
  483. * @param {Object} a point with x/y properties
  484. * @param {Object} b point with x/y properties
  485. */
  486. function distanceBetween( a, b ) {
  487. var dx = a.x - b.x,
  488. dy = a.y - b.y;
  489. return Math.sqrt( dx*dx + dy*dy );
  490. }
  491. /**
  492. * Retrieves the height of the given element by looking
  493. * at the position and height of its immediate children.
  494. */
  495. function getAbsoluteHeight( element ) {
  496. var height = 0;
  497. if( element ) {
  498. var absoluteChildren = 0;
  499. toArray( element.childNodes ).forEach( function( child ) {
  500. if( typeof child.offsetTop === 'number' && child.style ) {
  501. // Count # of abs children
  502. if( child.style.position === 'absolute' ) {
  503. absoluteChildren += 1;
  504. }
  505. height = Math.max( height, child.offsetTop + child.offsetHeight );
  506. }
  507. } );
  508. // If there are no absolute children, use offsetHeight
  509. if( absoluteChildren === 0 ) {
  510. height = element.offsetHeight;
  511. }
  512. }
  513. return height;
  514. }
  515. /**
  516. * Checks if this instance is being used to print a PDF.
  517. */
  518. function isPrintingPDF() {
  519. return ( /print-pdf/gi ).test( window.location.search );
  520. }
  521. /**
  522. * Causes the address bar to hide on mobile devices,
  523. * more vertical space ftw.
  524. */
  525. function removeAddressBar() {
  526. if( window.orientation === 0 ) {
  527. document.documentElement.style.overflow = 'scroll';
  528. document.body.style.height = '120%';
  529. }
  530. else {
  531. document.documentElement.style.overflow = '';
  532. document.body.style.height = '100%';
  533. }
  534. setTimeout( function() {
  535. window.scrollTo( 0, 1 );
  536. }, 10 );
  537. }
  538. /**
  539. * Dispatches an event of the specified type from the
  540. * reveal DOM element.
  541. */
  542. function dispatchEvent( type, properties ) {
  543. var event = document.createEvent( "HTMLEvents", 1, 2 );
  544. event.initEvent( type, true, true );
  545. extend( event, properties );
  546. dom.wrapper.dispatchEvent( event );
  547. }
  548. /**
  549. * Wrap all links in 3D goodness.
  550. */
  551. function enableRollingLinks() {
  552. if( supports3DTransforms && !( 'msPerspective' in document.body.style ) ) {
  553. var anchors = document.querySelectorAll( SLIDES_SELECTOR + ' a:not(.image)' );
  554. for( var i = 0, len = anchors.length; i < len; i++ ) {
  555. var anchor = anchors[i];
  556. if( anchor.textContent && !anchor.querySelector( '*' ) && ( !anchor.className || !anchor.classList.contains( anchor, 'roll' ) ) ) {
  557. var span = document.createElement('span');
  558. span.setAttribute('data-title', anchor.text);
  559. span.innerHTML = anchor.innerHTML;
  560. anchor.classList.add( 'roll' );
  561. anchor.innerHTML = '';
  562. anchor.appendChild(span);
  563. }
  564. }
  565. }
  566. }
  567. /**
  568. * Unwrap all 3D links.
  569. */
  570. function disableRollingLinks() {
  571. var anchors = document.querySelectorAll( SLIDES_SELECTOR + ' a.roll' );
  572. for( var i = 0, len = anchors.length; i < len; i++ ) {
  573. var anchor = anchors[i];
  574. var span = anchor.querySelector( 'span' );
  575. if( span ) {
  576. anchor.classList.remove( 'roll' );
  577. anchor.innerHTML = span.innerHTML;
  578. }
  579. }
  580. }
  581. /**
  582. * Bind preview frame links.
  583. */
  584. function enablePreviewLinks( selector ) {
  585. var anchors = toArray( document.querySelectorAll( selector ? selector : 'a' ) );
  586. anchors.forEach( function( element ) {
  587. if( /^(http|www)/gi.test( element.getAttribute( 'href' ) ) ) {
  588. element.addEventListener( 'click', onPreviewLinkClicked, false );
  589. }
  590. } );
  591. }
  592. /**
  593. * Unbind preview frame links.
  594. */
  595. function disablePreviewLinks() {
  596. var anchors = toArray( document.querySelectorAll( 'a' ) );
  597. anchors.forEach( function( element ) {
  598. if( /^(http|www)/gi.test( element.getAttribute( 'href' ) ) ) {
  599. element.removeEventListener( 'click', onPreviewLinkClicked, false );
  600. }
  601. } );
  602. }
  603. /**
  604. * Opens a preview window for the target URL.
  605. */
  606. function openPreview( url ) {
  607. closePreview();
  608. dom.preview = document.createElement( 'div' );
  609. dom.preview.classList.add( 'preview-link-overlay' );
  610. dom.wrapper.appendChild( dom.preview );
  611. dom.preview.innerHTML = [
  612. '<header>',
  613. '<a class="close" href="#"><span class="icon"></span></a>',
  614. '<a class="external" href="'+ url +'" target="_blank"><span class="icon"></span></a>',
  615. '</header>',
  616. '<div class="spinner"></div>',
  617. '<div class="viewport">',
  618. '<iframe src="'+ url +'"></iframe>',
  619. '</div>'
  620. ].join('');
  621. dom.preview.querySelector( 'iframe' ).addEventListener( 'load', function( event ) {
  622. dom.preview.classList.add( 'loaded' );
  623. }, false );
  624. dom.preview.querySelector( '.close' ).addEventListener( 'click', function( event ) {
  625. closePreview();
  626. event.preventDefault();
  627. }, false );
  628. dom.preview.querySelector( '.external' ).addEventListener( 'click', function( event ) {
  629. closePreview();
  630. }, false );
  631. setTimeout( function() {
  632. dom.preview.classList.add( 'visible' );
  633. }, 1 );
  634. }
  635. /**
  636. * Closes the iframe preview window.
  637. */
  638. function closePreview() {
  639. if( dom.preview ) {
  640. dom.preview.setAttribute( 'src', '' );
  641. dom.preview.parentNode.removeChild( dom.preview );
  642. dom.preview = null;
  643. }
  644. }
  645. /**
  646. * Return a sorted fragments list, ordered by an increasing
  647. * "data-fragment-index" attribute.
  648. *
  649. * Fragments will be revealed in the order that they are returned by
  650. * this function, so you can use the index attributes to control the
  651. * order of fragment appearance.
  652. *
  653. * To maintain a sensible default fragment order, fragments are presumed
  654. * to be passed in document order. This function adds a "fragment-index"
  655. * attribute to each node if such an attribute is not already present,
  656. * and sets that attribute to an integer value which is the position of
  657. * the fragment within the fragments list.
  658. */
  659. function sortFragments( fragments ) {
  660. var a = toArray( fragments );
  661. a.forEach( function( el, idx ) {
  662. if( !el.hasAttribute( 'data-fragment-index' ) ) {
  663. el.setAttribute( 'data-fragment-index', idx );
  664. }
  665. } );
  666. a.sort( function( l, r ) {
  667. return l.getAttribute( 'data-fragment-index' ) - r.getAttribute( 'data-fragment-index');
  668. } );
  669. return a;
  670. }
  671. /**
  672. * Applies JavaScript-controlled layout rules to the
  673. * presentation.
  674. */
  675. function layout() {
  676. if( dom.wrapper && !isPrintingPDF() ) {
  677. // Available space to scale within
  678. var availableWidth = dom.wrapper.offsetWidth,
  679. availableHeight = dom.wrapper.offsetHeight;
  680. // Reduce available space by margin
  681. availableWidth -= ( availableHeight * config.margin );
  682. availableHeight -= ( availableHeight * config.margin );
  683. // Dimensions of the content
  684. var slideWidth = config.width,
  685. slideHeight = config.height;
  686. // Slide width may be a percentage of available width
  687. if( typeof slideWidth === 'string' && /%$/.test( slideWidth ) ) {
  688. slideWidth = parseInt( slideWidth, 10 ) / 100 * availableWidth;
  689. }
  690. // Slide height may be a percentage of available height
  691. if( typeof slideHeight === 'string' && /%$/.test( slideHeight ) ) {
  692. slideHeight = parseInt( slideHeight, 10 ) / 100 * availableHeight;
  693. }
  694. dom.slides.style.width = slideWidth + 'px';
  695. dom.slides.style.height = slideHeight + 'px';
  696. // Determine scale of content to fit within available space
  697. scale = Math.min( availableWidth / slideWidth, availableHeight / slideHeight );
  698. // Respect max/min scale settings
  699. scale = Math.max( scale, config.minScale );
  700. scale = Math.min( scale, config.maxScale );
  701. // Prefer applying scale via zoom since Chrome blurs scaled content
  702. // with nested transforms
  703. if( typeof dom.slides.style.zoom !== 'undefined' && !navigator.userAgent.match( /(iphone|ipod|ipad|android)/gi ) ) {
  704. dom.slides.style.zoom = scale;
  705. }
  706. // Apply scale transform as a fallback
  707. else {
  708. var transform = 'translate(-50%, -50%) scale('+ scale +') translate(50%, 50%)';
  709. dom.slides.style.WebkitTransform = transform;
  710. dom.slides.style.MozTransform = transform;
  711. dom.slides.style.msTransform = transform;
  712. dom.slides.style.OTransform = transform;
  713. dom.slides.style.transform = transform;
  714. }
  715. // Select all slides, vertical and horizontal
  716. var slides = toArray( document.querySelectorAll( SLIDES_SELECTOR ) );
  717. for( var i = 0, len = slides.length; i < len; i++ ) {
  718. var slide = slides[ i ];
  719. // Don't bother updating invisible slides
  720. if( slide.style.display === 'none' ) {
  721. continue;
  722. }
  723. if( config.center ) {
  724. // Vertical stacks are not centred since their section
  725. // children will be
  726. if( slide.classList.contains( 'stack' ) ) {
  727. slide.style.top = 0;
  728. }
  729. else {
  730. slide.style.top = Math.max( - ( getAbsoluteHeight( slide ) / 2 ) - 20, -slideHeight / 2 ) + 'px';
  731. }
  732. }
  733. else {
  734. slide.style.top = '';
  735. }
  736. }
  737. updateProgress();
  738. }
  739. }
  740. /**
  741. * Stores the vertical index of a stack so that the same
  742. * vertical slide can be selected when navigating to and
  743. * from the stack.
  744. *
  745. * @param {HTMLElement} stack The vertical stack element
  746. * @param {int} v Index to memorize
  747. */
  748. function setPreviousVerticalIndex( stack, v ) {
  749. if( typeof stack === 'object' && typeof stack.setAttribute === 'function' ) {
  750. stack.setAttribute( 'data-previous-indexv', v || 0 );
  751. }
  752. }
  753. /**
  754. * Retrieves the vertical index which was stored using
  755. * #setPreviousVerticalIndex() or 0 if no previous index
  756. * exists.
  757. *
  758. * @param {HTMLElement} stack The vertical stack element
  759. */
  760. function getPreviousVerticalIndex( stack ) {
  761. if( typeof stack === 'object' && typeof stack.setAttribute === 'function' && stack.classList.contains( 'stack' ) ) {
  762. // Prefer manually defined start-indexv
  763. var attributeName = stack.hasAttribute( 'data-start-indexv' ) ? 'data-start-indexv' : 'data-previous-indexv';
  764. return parseInt( stack.getAttribute( attributeName ) || 0, 10 );
  765. }
  766. return 0;
  767. }
  768. /**
  769. * Displays the overview of slides (quick nav) by
  770. * scaling down and arranging all slide elements.
  771. *
  772. * Experimental feature, might be dropped if perf
  773. * can't be improved.
  774. */
  775. function activateOverview() {
  776. // Only proceed if enabled in config
  777. if( config.overview ) {
  778. // Don't auto-slide while in overview mode
  779. cancelAutoSlide();
  780. var wasActive = dom.wrapper.classList.contains( 'overview' );
  781. dom.wrapper.classList.add( 'overview' );
  782. dom.wrapper.classList.remove( 'exit-overview' );
  783. clearTimeout( activateOverviewTimeout );
  784. clearTimeout( deactivateOverviewTimeout );
  785. // Not the pretties solution, but need to let the overview
  786. // class apply first so that slides are measured accurately
  787. // before we can position them
  788. activateOverviewTimeout = setTimeout( function(){
  789. var horizontalSlides = document.querySelectorAll( HORIZONTAL_SLIDES_SELECTOR );
  790. for( var i = 0, len1 = horizontalSlides.length; i < len1; i++ ) {
  791. var hslide = horizontalSlides[i],
  792. hoffset = config.rtl ? -105 : 105,
  793. htransform = 'translateZ(-2500px) translate(' + ( ( i - indexh ) * hoffset ) + '%, 0%)';
  794. hslide.setAttribute( 'data-index-h', i );
  795. hslide.style.display = 'block';
  796. hslide.style.WebkitTransform = htransform;
  797. hslide.style.MozTransform = htransform;
  798. hslide.style.msTransform = htransform;
  799. hslide.style.OTransform = htransform;
  800. hslide.style.transform = htransform;
  801. if( hslide.classList.contains( 'stack' ) ) {
  802. var verticalSlides = hslide.querySelectorAll( 'section' );
  803. for( var j = 0, len2 = verticalSlides.length; j < len2; j++ ) {
  804. var verticalIndex = i === indexh ? indexv : getPreviousVerticalIndex( hslide );
  805. var vslide = verticalSlides[j],
  806. vtransform = 'translate(0%, ' + ( ( j - verticalIndex ) * 105 ) + '%)';
  807. vslide.setAttribute( 'data-index-h', i );
  808. vslide.setAttribute( 'data-index-v', j );
  809. vslide.style.display = 'block';
  810. vslide.style.WebkitTransform = vtransform;
  811. vslide.style.MozTransform = vtransform;
  812. vslide.style.msTransform = vtransform;
  813. vslide.style.OTransform = vtransform;
  814. vslide.style.transform = vtransform;
  815. // Navigate to this slide on click
  816. vslide.addEventListener( 'click', onOverviewSlideClicked, true );
  817. }
  818. }
  819. else {
  820. // Navigate to this slide on click
  821. hslide.addEventListener( 'click', onOverviewSlideClicked, true );
  822. }
  823. }
  824. layout();
  825. if( !wasActive ) {
  826. // Notify observers of the overview showing
  827. dispatchEvent( 'overviewshown', {
  828. 'indexh': indexh,
  829. 'indexv': indexv,
  830. 'currentSlide': currentSlide
  831. } );
  832. }
  833. }, 10 );
  834. }
  835. }
  836. /**
  837. * Exits the slide overview and enters the currently
  838. * active slide.
  839. */
  840. function deactivateOverview() {
  841. // Only proceed if enabled in config
  842. if( config.overview ) {
  843. clearTimeout( activateOverviewTimeout );
  844. clearTimeout( deactivateOverviewTimeout );
  845. dom.wrapper.classList.remove( 'overview' );
  846. // Temporarily add a class so that transitions can do different things
  847. // depending on whether they are exiting/entering overview, or just
  848. // moving from slide to slide
  849. dom.wrapper.classList.add( 'exit-overview' );
  850. deactivateOverviewTimeout = setTimeout( function () {
  851. dom.wrapper.classList.remove( 'exit-overview' );
  852. }, 10);
  853. // Select all slides
  854. var slides = toArray( document.querySelectorAll( SLIDES_SELECTOR ) );
  855. for( var i = 0, len = slides.length; i < len; i++ ) {
  856. var element = slides[i];
  857. element.style.display = '';
  858. // Resets all transforms to use the external styles
  859. element.style.WebkitTransform = '';
  860. element.style.MozTransform = '';
  861. element.style.msTransform = '';
  862. element.style.OTransform = '';
  863. element.style.transform = '';
  864. element.removeEventListener( 'click', onOverviewSlideClicked, true );
  865. }
  866. slide( indexh, indexv );
  867. cueAutoSlide();
  868. // Notify observers of the overview hiding
  869. dispatchEvent( 'overviewhidden', {
  870. 'indexh': indexh,
  871. 'indexv': indexv,
  872. 'currentSlide': currentSlide
  873. } );
  874. }
  875. }
  876. /**
  877. * Toggles the slide overview mode on and off.
  878. *
  879. * @param {Boolean} override Optional flag which overrides the
  880. * toggle logic and forcibly sets the desired state. True means
  881. * overview is open, false means it's closed.
  882. */
  883. function toggleOverview( override ) {
  884. if( typeof override === 'boolean' ) {
  885. override ? activateOverview() : deactivateOverview();
  886. }
  887. else {
  888. isOverview() ? deactivateOverview() : activateOverview();
  889. }
  890. }
  891. /**
  892. * Checks if the overview is currently active.
  893. *
  894. * @return {Boolean} true if the overview is active,
  895. * false otherwise
  896. */
  897. function isOverview() {
  898. return dom.wrapper.classList.contains( 'overview' );
  899. }
  900. /**
  901. * Checks if the current or specified slide is vertical
  902. * (nested within another slide).
  903. *
  904. * @param {HTMLElement} slide [optional] The slide to check
  905. * orientation of
  906. */
  907. function isVerticalSlide( slide ) {
  908. // Prefer slide argument, otherwise use current slide
  909. slide = slide ? slide : currentSlide;
  910. return slide && !!slide.parentNode.nodeName.match( /section/i );
  911. }
  912. /**
  913. * Handling the fullscreen functionality via the fullscreen API
  914. *
  915. * @see http://fullscreen.spec.whatwg.org/
  916. * @see https://developer.mozilla.org/en-US/docs/DOM/Using_fullscreen_mode
  917. */
  918. function enterFullscreen() {
  919. var element = document.body;
  920. // Check which implementation is available
  921. var requestMethod = element.requestFullScreen ||
  922. element.webkitRequestFullscreen ||
  923. element.webkitRequestFullScreen ||
  924. element.mozRequestFullScreen ||
  925. element.msRequestFullScreen;
  926. if( requestMethod ) {
  927. requestMethod.apply( element );
  928. }
  929. }
  930. /**
  931. * Enters the paused mode which fades everything on screen to
  932. * black.
  933. */
  934. function pause() {
  935. var wasPaused = dom.wrapper.classList.contains( 'paused' );
  936. cancelAutoSlide();
  937. dom.wrapper.classList.add( 'paused' );
  938. if( wasPaused === false ) {
  939. dispatchEvent( 'paused' );
  940. }
  941. }
  942. /**
  943. * Exits from the paused mode.
  944. */
  945. function resume() {
  946. var wasPaused = dom.wrapper.classList.contains( 'paused' );
  947. dom.wrapper.classList.remove( 'paused' );
  948. cueAutoSlide();
  949. if( wasPaused ) {
  950. dispatchEvent( 'resumed' );
  951. }
  952. }
  953. /**
  954. * Toggles the paused mode on and off.
  955. */
  956. function togglePause() {
  957. if( isPaused() ) {
  958. resume();
  959. }
  960. else {
  961. pause();
  962. }
  963. }
  964. /**
  965. * Checks if we are currently in the paused mode.
  966. */
  967. function isPaused() {
  968. return dom.wrapper.classList.contains( 'paused' );
  969. }
  970. /**
  971. * Steps from the current point in the presentation to the
  972. * slide which matches the specified horizontal and vertical
  973. * indices.
  974. *
  975. * @param {int} h Horizontal index of the target slide
  976. * @param {int} v Vertical index of the target slide
  977. * @param {int} f Optional index of a fragment within the
  978. * target slide to activate
  979. * @param {int} o Optional origin for use in multimaster environments
  980. */
  981. function slide( h, v, f, o ) {
  982. // Remember where we were at before
  983. previousSlide = currentSlide;
  984. // Query all horizontal slides in the deck
  985. var horizontalSlides = document.querySelectorAll( HORIZONTAL_SLIDES_SELECTOR );
  986. // If no vertical index is specified and the upcoming slide is a
  987. // stack, resume at its previous vertical index
  988. if( v === undefined ) {
  989. v = getPreviousVerticalIndex( horizontalSlides[ h ] );
  990. }
  991. // If we were on a vertical stack, remember what vertical index
  992. // it was on so we can resume at the same position when returning
  993. if( previousSlide && previousSlide.parentNode && previousSlide.parentNode.classList.contains( 'stack' ) ) {
  994. setPreviousVerticalIndex( previousSlide.parentNode, indexv );
  995. }
  996. // Remember the state before this slide
  997. var stateBefore = state.concat();
  998. // Reset the state array
  999. state.length = 0;
  1000. var indexhBefore = indexh,
  1001. indexvBefore = indexv;
  1002. // Activate and transition to the new slide
  1003. indexh = updateSlides( HORIZONTAL_SLIDES_SELECTOR, h === undefined ? indexh : h );
  1004. indexv = updateSlides( VERTICAL_SLIDES_SELECTOR, v === undefined ? indexv : v );
  1005. layout();
  1006. // Apply the new state
  1007. stateLoop: for( var i = 0, len = state.length; i < len; i++ ) {
  1008. // Check if this state existed on the previous slide. If it
  1009. // did, we will avoid adding it repeatedly
  1010. for( var j = 0; j < stateBefore.length; j++ ) {
  1011. if( stateBefore[j] === state[i] ) {
  1012. stateBefore.splice( j, 1 );
  1013. continue stateLoop;
  1014. }
  1015. }
  1016. document.documentElement.classList.add( state[i] );
  1017. // Dispatch custom event matching the state's name
  1018. dispatchEvent( state[i] );
  1019. }
  1020. // Clean up the remains of the previous state
  1021. while( stateBefore.length ) {
  1022. document.documentElement.classList.remove( stateBefore.pop() );
  1023. }
  1024. // If the overview is active, re-activate it to update positions
  1025. if( isOverview() ) {
  1026. activateOverview();
  1027. }
  1028. // Update the URL hash after a delay since updating it mid-transition
  1029. // is likely to cause visual lag
  1030. writeURL( 1500 );
  1031. // Find the current horizontal slide and any possible vertical slides
  1032. // within it
  1033. var currentHorizontalSlide = horizontalSlides[ indexh ],
  1034. currentVerticalSlides = currentHorizontalSlide.querySelectorAll( 'section' );
  1035. // Store references to the previous and current slides
  1036. currentSlide = currentVerticalSlides[ indexv ] || currentHorizontalSlide;
  1037. // Show fragment, if specified
  1038. if( typeof f !== 'undefined' ) {
  1039. var fragments = sortFragments( currentSlide.querySelectorAll( '.fragment' ) );
  1040. toArray( fragments ).forEach( function( fragment, indexf ) {
  1041. if( indexf < f ) {
  1042. fragment.classList.add( 'visible' );
  1043. }
  1044. else {
  1045. fragment.classList.remove( 'visible' );
  1046. }
  1047. } );
  1048. }
  1049. // Dispatch an event if the slide changed
  1050. var slideChanged = ( indexh !== indexhBefore || indexv !== indexvBefore );
  1051. if( slideChanged ) {
  1052. dispatchEvent( 'slidechanged', {
  1053. 'indexh': indexh,
  1054. 'indexv': indexv,
  1055. 'previousSlide': previousSlide,
  1056. 'currentSlide': currentSlide,
  1057. 'origin': o
  1058. } );
  1059. }
  1060. else {
  1061. // Ensure that the previous slide is never the same as the current
  1062. previousSlide = null;
  1063. }
  1064. // Solves an edge case where the previous slide maintains the
  1065. // 'present' class when navigating between adjacent vertical
  1066. // stacks
  1067. if( previousSlide ) {
  1068. previousSlide.classList.remove( 'present' );
  1069. // Reset all slides upon navigate to home
  1070. // Issue: #285
  1071. if ( document.querySelector( HOME_SLIDE_SELECTOR ).classList.contains( 'present' ) ) {
  1072. // Launch async task
  1073. setTimeout( function () {
  1074. var slides = toArray( document.querySelectorAll( HORIZONTAL_SLIDES_SELECTOR + '.stack') ), i;
  1075. for( i in slides ) {
  1076. if( slides[i] ) {
  1077. // Reset stack
  1078. setPreviousVerticalIndex( slides[i], 0 );
  1079. }
  1080. }
  1081. }, 0 );
  1082. }
  1083. }
  1084. // Handle embedded content
  1085. if( slideChanged ) {
  1086. stopEmbeddedContent( previousSlide );
  1087. startEmbeddedContent( currentSlide );
  1088. }
  1089. updateControls();
  1090. updateProgress();
  1091. updateBackground();
  1092. }
  1093. /**
  1094. * Syncs the presentation with the current DOM. Useful
  1095. * when new slides or control elements are added or when
  1096. * the configuration has changed.
  1097. */
  1098. function sync() {
  1099. // Subscribe to input
  1100. removeEventListeners();
  1101. addEventListeners();
  1102. // Force a layout to make sure the current config is accounted for
  1103. layout();
  1104. // Reflect the current autoSlide value
  1105. autoSlide = config.autoSlide;
  1106. // Start auto-sliding if it's enabled
  1107. cueAutoSlide();
  1108. // Re-create the slide backgrounds
  1109. createBackgrounds();
  1110. updateControls();
  1111. updateProgress();
  1112. updateBackground();
  1113. }
  1114. /**
  1115. * Updates one dimension of slides by showing the slide
  1116. * with the specified index.
  1117. *
  1118. * @param {String} selector A CSS selector that will fetch
  1119. * the group of slides we are working with
  1120. * @param {Number} index The index of the slide that should be
  1121. * shown
  1122. *
  1123. * @return {Number} The index of the slide that is now shown,
  1124. * might differ from the passed in index if it was out of
  1125. * bounds.
  1126. */
  1127. function updateSlides( selector, index ) {
  1128. // Select all slides and convert the NodeList result to
  1129. // an array
  1130. var slides = toArray( document.querySelectorAll( selector ) ),
  1131. slidesLength = slides.length;
  1132. if( slidesLength ) {
  1133. // Should the index loop?
  1134. if( config.loop ) {
  1135. index %= slidesLength;
  1136. if( index < 0 ) {
  1137. index = slidesLength + index;
  1138. }
  1139. }
  1140. // Enforce max and minimum index bounds
  1141. index = Math.max( Math.min( index, slidesLength - 1 ), 0 );
  1142. for( var i = 0; i < slidesLength; i++ ) {
  1143. var element = slides[i];
  1144. // Optimization; hide all slides that are three or more steps
  1145. // away from the present slide
  1146. if( isOverview() === false ) {
  1147. // The distance loops so that it measures 1 between the first
  1148. // and last slides
  1149. var distance = Math.abs( ( index - i ) % ( slidesLength - 3 ) ) || 0;
  1150. element.style.display = distance > 3 ? 'none' : 'block';
  1151. }
  1152. var reverse = config.rtl && !isVerticalSlide( element );
  1153. element.classList.remove( 'past' );
  1154. element.classList.remove( 'present' );
  1155. element.classList.remove( 'future' );
  1156. // http://www.w3.org/html/wg/drafts/html/master/editing.html#the-hidden-attribute
  1157. element.setAttribute( 'hidden', '' );
  1158. if( i < index ) {
  1159. // Any element previous to index is given the 'past' class
  1160. element.classList.add( reverse ? 'future' : 'past' );
  1161. }
  1162. else if( i > index ) {
  1163. // Any element subsequent to index is given the 'future' class
  1164. element.classList.add( reverse ? 'past' : 'future' );
  1165. }
  1166. // If this element contains vertical slides
  1167. if( element.querySelector( 'section' ) ) {
  1168. element.classList.add( 'stack' );
  1169. }
  1170. }
  1171. // Mark the current slide as present
  1172. slides[index].classList.add( 'present' );
  1173. slides[index].removeAttribute( 'hidden' );
  1174. // If this slide has a state associated with it, add it
  1175. // onto the current state of the deck
  1176. var slideState = slides[index].getAttribute( 'data-state' );
  1177. if( slideState ) {
  1178. state = state.concat( slideState.split( ' ' ) );
  1179. }
  1180. // If this slide has a data-autoslide attribute associated use this as
  1181. // autoSlide value otherwise use the global configured time
  1182. var slideAutoSlide = slides[index].getAttribute( 'data-autoslide' );
  1183. if( slideAutoSlide ) {
  1184. autoSlide = parseInt( slideAutoSlide, 10 );
  1185. }
  1186. else {
  1187. autoSlide = config.autoSlide;
  1188. }
  1189. }
  1190. else {
  1191. // Since there are no slides we can't be anywhere beyond the
  1192. // zeroth index
  1193. index = 0;
  1194. }
  1195. return index;
  1196. }
  1197. /**
  1198. * Updates the progress bar to reflect the current slide.
  1199. */
  1200. function updateProgress() {
  1201. // Update progress if enabled
  1202. if( config.progress && dom.progress ) {
  1203. var horizontalSlides = toArray( document.querySelectorAll( HORIZONTAL_SLIDES_SELECTOR ) );
  1204. // The number of past and total slides
  1205. var totalCount = document.querySelectorAll( SLIDES_SELECTOR + ':not(.stack)' ).length;
  1206. var pastCount = 0;
  1207. // Step through all slides and count the past ones
  1208. mainLoop: for( var i = 0; i < horizontalSlides.length; i++ ) {
  1209. var horizontalSlide = horizontalSlides[i];
  1210. var verticalSlides = toArray( horizontalSlide.querySelectorAll( 'section' ) );
  1211. for( var j = 0; j < verticalSlides.length; j++ ) {
  1212. // Stop as soon as we arrive at the present
  1213. if( verticalSlides[j].classList.contains( 'present' ) ) {
  1214. break mainLoop;
  1215. }
  1216. pastCount++;
  1217. }
  1218. // Stop as soon as we arrive at the present
  1219. if( horizontalSlide.classList.contains( 'present' ) ) {
  1220. break;
  1221. }
  1222. // Don't count the wrapping section for vertical slides
  1223. if( horizontalSlide.classList.contains( 'stack' ) === false ) {
  1224. pastCount++;
  1225. }
  1226. }
  1227. dom.progressbar.style.width = ( pastCount / ( totalCount - 1 ) ) * window.innerWidth + 'px';
  1228. }
  1229. }
  1230. /**
  1231. * Updates the state of all control/navigation arrows.
  1232. */
  1233. function updateControls() {
  1234. if ( config.controls && dom.controls ) {
  1235. var routes = availableRoutes();
  1236. var fragments = availableFragments();
  1237. // Remove the 'enabled' class from all directions
  1238. dom.controlsLeft.concat( dom.controlsRight )
  1239. .concat( dom.controlsUp )
  1240. .concat( dom.controlsDown )
  1241. .concat( dom.controlsPrev )
  1242. .concat( dom.controlsNext ).forEach( function( node ) {
  1243. node.classList.remove( 'enabled' );
  1244. node.classList.remove( 'fragmented' );
  1245. } );
  1246. // Add the 'enabled' class to the available routes
  1247. if( routes.left ) dom.controlsLeft.forEach( function( el ) { el.classList.add( 'enabled' ); } );
  1248. if( routes.right ) dom.controlsRight.forEach( function( el ) { el.classList.add( 'enabled' ); } );
  1249. if( routes.up ) dom.controlsUp.forEach( function( el ) { el.classList.add( 'enabled' ); } );
  1250. if( routes.down ) dom.controlsDown.forEach( function( el ) { el.classList.add( 'enabled' ); } );
  1251. // Prev/next buttons
  1252. if( routes.left || routes.up ) dom.controlsPrev.forEach( function( el ) { el.classList.add( 'enabled' ); } );
  1253. if( routes.right || routes.down ) dom.controlsNext.forEach( function( el ) { el.classList.add( 'enabled' ); } );
  1254. // Highlight fragment directions
  1255. if( currentSlide ) {
  1256. // Always apply fragment decorator to prev/next buttons
  1257. if( fragments.prev ) dom.controlsPrev.forEach( function( el ) { el.classList.add( 'fragmented', 'enabled' ); } );
  1258. if( fragments.next ) dom.controlsNext.forEach( function( el ) { el.classList.add( 'fragmented', 'enabled' ); } );
  1259. // Apply fragment decorators to directional buttons based on
  1260. // what slide axis they are in
  1261. if( isVerticalSlide( currentSlide ) ) {
  1262. if( fragments.prev ) dom.controlsUp.forEach( function( el ) { el.classList.add( 'fragmented', 'enabled' ); } );
  1263. if( fragments.next ) dom.controlsDown.forEach( function( el ) { el.classList.add( 'fragmented', 'enabled' ); } );
  1264. }
  1265. else {
  1266. if( fragments.prev ) dom.controlsLeft.forEach( function( el ) { el.classList.add( 'fragmented', 'enabled' ); } );
  1267. if( fragments.next ) dom.controlsRight.forEach( function( el ) { el.classList.add( 'fragmented', 'enabled' ); } );
  1268. }
  1269. }
  1270. }
  1271. }
  1272. /**
  1273. * Updates the background elements to reflect the current
  1274. * slide.
  1275. */
  1276. function updateBackground() {
  1277. // Update the classes of all backgrounds to match the
  1278. // states of their slides (past/present/future)
  1279. toArray( dom.background.childNodes ).forEach( function( backgroundh, h ) {
  1280. // Reverse past/future classes when in RTL mode
  1281. var horizontalPast = config.rtl ? 'future' : 'past',
  1282. horizontalFuture = config.rtl ? 'past' : 'future';
  1283. backgroundh.className = 'slide-background ' + ( h < indexh ? horizontalPast : h > indexh ? horizontalFuture : 'present' );
  1284. toArray( backgroundh.childNodes ).forEach( function( backgroundv, v ) {
  1285. backgroundv.className = 'slide-background ' + ( v < indexv ? 'past' : v > indexv ? 'future' : 'present' );
  1286. } );
  1287. } );
  1288. // Allow the first background to apply without transition
  1289. setTimeout( function() {
  1290. dom.background.classList.remove( 'no-transition' );
  1291. }, 1 );
  1292. }
  1293. /**
  1294. * Determine what available routes there are for navigation.
  1295. *
  1296. * @return {Object} containing four booleans: left/right/up/down
  1297. */
  1298. function availableRoutes() {
  1299. var horizontalSlides = document.querySelectorAll( HORIZONTAL_SLIDES_SELECTOR ),
  1300. verticalSlides = document.querySelectorAll( VERTICAL_SLIDES_SELECTOR );
  1301. var routes = {
  1302. left: indexh > 0 || config.loop,
  1303. right: indexh < horizontalSlides.length - 1 || config.loop,
  1304. up: indexv > 0,
  1305. down: indexv < verticalSlides.length - 1
  1306. };
  1307. // reverse horizontal controls for rtl
  1308. if( config.rtl ) {
  1309. var left = routes.left;
  1310. routes.left = routes.right;
  1311. routes.right = left;
  1312. }
  1313. return routes;
  1314. }
  1315. /**
  1316. * Returns an object describing the available fragment
  1317. * directions.
  1318. *
  1319. * @return {Object} two boolean properties: prev/next
  1320. */
  1321. function availableFragments() {
  1322. if( currentSlide && config.fragments ) {
  1323. var fragments = currentSlide.querySelectorAll( '.fragment' );
  1324. var hiddenFragments = currentSlide.querySelectorAll( '.fragment:not(.visible)' );
  1325. return {
  1326. prev: fragments.length - hiddenFragments.length > 0,
  1327. next: !!hiddenFragments.length
  1328. };
  1329. }
  1330. else {
  1331. return { prev: false, next: false };
  1332. }
  1333. }
  1334. /**
  1335. * Start playback of any embedded content inside of
  1336. * the targeted slide.
  1337. */
  1338. function startEmbeddedContent( slide ) {
  1339. if( slide ) {
  1340. // HTML5 media elements
  1341. toArray( slide.querySelectorAll( 'video, audio' ) ).forEach( function( el ) {
  1342. if( el.hasAttribute( 'data-autoplay' ) ) {
  1343. el.play();
  1344. }
  1345. } );
  1346. // YouTube embeds
  1347. toArray( slide.querySelectorAll( 'iframe[src*="youtube.com/embed/"]' ) ).forEach( function( el ) {
  1348. if( el.hasAttribute( 'data-autoplay' ) ) {
  1349. el.contentWindow.postMessage('{"event":"command","func":"playVideo","args":""}', '*');
  1350. }
  1351. });
  1352. }
  1353. }
  1354. /**
  1355. * Stop playback of any embedded content inside of
  1356. * the targeted slide.
  1357. */
  1358. function stopEmbeddedContent( slide ) {
  1359. if( slide ) {
  1360. // HTML5 media elements
  1361. toArray( slide.querySelectorAll( 'video, audio' ) ).forEach( function( el ) {
  1362. if( !el.hasAttribute( 'data-ignore' ) ) {
  1363. el.pause();
  1364. }
  1365. } );
  1366. // YouTube embeds
  1367. toArray( slide.querySelectorAll( 'iframe[src*="youtube.com/embed/"]' ) ).forEach( function( el ) {
  1368. if( !el.hasAttribute( 'data-ignore' ) && typeof el.contentWindow.postMessage === 'function' ) {
  1369. el.contentWindow.postMessage('{"event":"command","func":"pauseVideo","args":""}', '*');
  1370. }
  1371. });
  1372. }
  1373. }
  1374. /**
  1375. * Reads the current URL (hash) and navigates accordingly.
  1376. */
  1377. function readURL() {
  1378. var hash = window.location.hash;
  1379. // Attempt to parse the hash as either an index or name
  1380. var bits = hash.slice( 2 ).split( '/' ),
  1381. name = hash.replace( /#|\//gi, '' );
  1382. // If the first bit is invalid and there is a name we can
  1383. // assume that this is a named link
  1384. if( isNaN( parseInt( bits[0], 10 ) ) && name.length ) {
  1385. // Find the slide with the specified name
  1386. var element = document.querySelector( '#' + name );
  1387. if( element ) {
  1388. // Find the position of the named slide and navigate to it
  1389. var indices = Reveal.getIndices( element );
  1390. slide( indices.h, indices.v );
  1391. }
  1392. // If the slide doesn't exist, navigate to the current slide
  1393. else {
  1394. slide( indexh, indexv );
  1395. }
  1396. }
  1397. else {
  1398. // Read the index components of the hash
  1399. var h = parseInt( bits[0], 10 ) || 0,
  1400. v = parseInt( bits[1], 10 ) || 0;
  1401. slide( h, v );
  1402. }
  1403. }
  1404. /**
  1405. * Updates the page URL (hash) to reflect the current
  1406. * state.
  1407. *
  1408. * @param {Number} delay The time in ms to wait before
  1409. * writing the hash
  1410. */
  1411. function writeURL( delay ) {
  1412. if( config.history ) {
  1413. // Make sure there's never more than one timeout running
  1414. clearTimeout( writeURLTimeout );
  1415. // If a delay is specified, timeout this call
  1416. if( typeof delay === 'number' ) {
  1417. writeURLTimeout = setTimeout( writeURL, delay );
  1418. }
  1419. else {
  1420. var url = '/';
  1421. // If the current slide has an ID, use that as a named link
  1422. if( currentSlide && typeof currentSlide.getAttribute( 'id' ) === 'string' ) {
  1423. url = '/' + currentSlide.getAttribute( 'id' );
  1424. }
  1425. // Otherwise use the /h/v index
  1426. else {
  1427. if( indexh > 0 || indexv > 0 ) url += indexh;
  1428. if( indexv > 0 ) url += '/' + indexv;
  1429. }
  1430. window.location.hash = url;
  1431. }
  1432. }
  1433. }
  1434. /**
  1435. * Retrieves the h/v location of the current, or specified,
  1436. * slide.
  1437. *
  1438. * @param {HTMLElement} slide If specified, the returned
  1439. * index will be for this slide rather than the currently
  1440. * active one
  1441. *
  1442. * @return {Object} { h: <int>, v: <int>, f: <int> }
  1443. */
  1444. function getIndices( slide ) {
  1445. // By default, return the current indices
  1446. var h = indexh,
  1447. v = indexv,
  1448. f;
  1449. // If a slide is specified, return the indices of that slide
  1450. if( slide ) {
  1451. var isVertical = isVerticalSlide( slide );
  1452. var slideh = isVertical ? slide.parentNode : slide;
  1453. // Select all horizontal slides
  1454. var horizontalSlides = toArray( document.querySelectorAll( HORIZONTAL_SLIDES_SELECTOR ) );
  1455. // Now that we know which the horizontal slide is, get its index
  1456. h = Math.max( horizontalSlides.indexOf( slideh ), 0 );
  1457. // If this is a vertical slide, grab the vertical index
  1458. if( isVertical ) {
  1459. v = Math.max( toArray( slide.parentNode.querySelectorAll( 'section' ) ).indexOf( slide ), 0 );
  1460. }
  1461. }
  1462. if( !slide && currentSlide ) {
  1463. var visibleFragments = currentSlide.querySelectorAll( '.fragment.visible' );
  1464. if( visibleFragments.length ) {
  1465. f = visibleFragments.length;
  1466. }
  1467. }
  1468. return { h: h, v: v, f: f };
  1469. }
  1470. /**
  1471. * Navigate to the next slide fragment.
  1472. *
  1473. * @return {Boolean} true if there was a next fragment,
  1474. * false otherwise
  1475. */
  1476. function nextFragment() {
  1477. if( currentSlide && config.fragments ) {
  1478. var fragments = sortFragments( currentSlide.querySelectorAll( '.fragment:not(.visible)' ) );
  1479. if( fragments.length ) {
  1480. // Find the index of the next fragment
  1481. var index = fragments[0].getAttribute( 'data-fragment-index' );
  1482. // Find all fragments with the same index
  1483. fragments = currentSlide.querySelectorAll( '.fragment[data-fragment-index="'+ index +'"]' );
  1484. toArray( fragments ).forEach( function( element ) {
  1485. element.classList.add( 'visible' );
  1486. } );
  1487. // Notify subscribers of the change
  1488. dispatchEvent( 'fragmentshown', { fragment: fragments[0], fragments: fragments } );
  1489. updateControls();
  1490. return true;
  1491. }
  1492. }
  1493. return false;
  1494. }
  1495. /**
  1496. * Navigate to the previous slide fragment.
  1497. *
  1498. * @return {Boolean} true if there was a previous fragment,
  1499. * false otherwise
  1500. */
  1501. function previousFragment() {
  1502. if( currentSlide && config.fragments ) {
  1503. var fragments = sortFragments( currentSlide.querySelectorAll( '.fragment.visible' ) );
  1504. if( fragments.length ) {
  1505. // Find the index of the previous fragment
  1506. var index = fragments[ fragments.length - 1 ].getAttribute( 'data-fragment-index' );
  1507. // Find all fragments with the same index
  1508. fragments = currentSlide.querySelectorAll( '.fragment[data-fragment-index="'+ index +'"]' );
  1509. toArray( fragments ).forEach( function( f ) {
  1510. f.classList.remove( 'visible' );
  1511. } );
  1512. // Notify subscribers of the change
  1513. dispatchEvent( 'fragmenthidden', { fragment: fragments[0], fragments: fragments } );
  1514. updateControls();
  1515. return true;
  1516. }
  1517. }
  1518. return false;
  1519. }
  1520. /**
  1521. * Cues a new automated slide if enabled in the config.
  1522. */
  1523. function cueAutoSlide() {
  1524. clearTimeout( autoSlideTimeout );
  1525. // Cue the next auto-slide if enabled
  1526. if( autoSlide && !isPaused() && !isOverview() ) {
  1527. autoSlideTimeout = setTimeout( navigateNext, autoSlide );
  1528. }
  1529. }
  1530. /**
  1531. * Cancels any ongoing request to auto-slide.
  1532. */
  1533. function cancelAutoSlide() {
  1534. clearTimeout( autoSlideTimeout );
  1535. }
  1536. function navigateLeft() {
  1537. // Reverse for RTL
  1538. if( config.rtl ) {
  1539. if( ( isOverview() || nextFragment() === false ) && availableRoutes().left ) {
  1540. slide( indexh + 1 );
  1541. }
  1542. }
  1543. // Normal navigation
  1544. else if( ( isOverview() || previousFragment() === false ) && availableRoutes().left ) {
  1545. slide( indexh - 1 );
  1546. }
  1547. }
  1548. function navigateRight() {
  1549. // Reverse for RTL
  1550. if( config.rtl ) {
  1551. if( ( isOverview() || previousFragment() === false ) && availableRoutes().right ) {
  1552. slide( indexh - 1 );
  1553. }
  1554. }
  1555. // Normal navigation
  1556. else if( ( isOverview() || nextFragment() === false ) && availableRoutes().right ) {
  1557. slide( indexh + 1 );
  1558. }
  1559. }
  1560. function navigateUp() {
  1561. // Prioritize hiding fragments
  1562. if( ( isOverview() || previousFragment() === false ) && availableRoutes().up ) {
  1563. slide( indexh, indexv - 1 );
  1564. }
  1565. }
  1566. function navigateDown() {
  1567. // Prioritize revealing fragments
  1568. if( ( isOverview() || nextFragment() === false ) && availableRoutes().down ) {
  1569. slide( indexh, indexv + 1 );
  1570. }
  1571. }
  1572. /**
  1573. * Navigates backwards, prioritized in the following order:
  1574. * 1) Previous fragment
  1575. * 2) Previous vertical slide
  1576. * 3) Previous horizontal slide
  1577. */
  1578. function navigatePrev() {
  1579. // Prioritize revealing fragments
  1580. if( previousFragment() === false ) {
  1581. if( availableRoutes().up ) {
  1582. navigateUp();
  1583. }
  1584. else {
  1585. // Fetch the previous horizontal slide, if there is one
  1586. var previousSlide = document.querySelector( HORIZONTAL_SLIDES_SELECTOR + '.past:nth-child(' + indexh + ')' );
  1587. if( previousSlide ) {
  1588. var v = ( previousSlide.querySelectorAll( 'section' ).length - 1 ) || undefined;
  1589. var h = indexh - 1;
  1590. slide( h, v );
  1591. }
  1592. }
  1593. }
  1594. }
  1595. /**
  1596. * Same as #navigatePrev() but navigates forwards.
  1597. */
  1598. function navigateNext() {
  1599. // Prioritize revealing fragments
  1600. if( nextFragment() === false ) {
  1601. availableRoutes().down ? navigateDown() : navigateRight();
  1602. }
  1603. // If auto-sliding is enabled we need to cue up
  1604. // another timeout
  1605. cueAutoSlide();
  1606. }
  1607. // --------------------------------------------------------------------//
  1608. // ----------------------------- EVENTS -------------------------------//
  1609. // --------------------------------------------------------------------//
  1610. /**
  1611. * Handler for the document level 'keydown' event.
  1612. *
  1613. * @param {Object} event
  1614. */
  1615. function onDocumentKeyDown( event ) {
  1616. // Check if there's a focused element that could be using
  1617. // the keyboard
  1618. var activeElement = document.activeElement;
  1619. var hasFocus = !!( document.activeElement && ( document.activeElement.type || document.activeElement.href || document.activeElement.contentEditable !== 'inherit' ) );
  1620. // Disregard the event if there's a focused element or a
  1621. // keyboard modifier key is present
  1622. if( hasFocus || (event.shiftKey && event.keyCode !== 32) || event.altKey || event.ctrlKey || event.metaKey ) return;
  1623. // While paused only allow "unpausing" keyboard events (b and .)
  1624. if( isPaused() && [66,190,191].indexOf( event.keyCode ) === -1 ) {
  1625. return false;
  1626. }
  1627. var triggered = false;
  1628. // 1. User defined key bindings
  1629. if( typeof config.keyboard === 'object' ) {
  1630. for( var key in config.keyboard ) {
  1631. // Check if this binding matches the pressed key
  1632. if( parseInt( key, 10 ) === event.keyCode ) {
  1633. var value = config.keyboard[ key ];
  1634. // Callback function
  1635. if( typeof value === 'function' ) {
  1636. value.apply( null, [ event ] );
  1637. }
  1638. // String shortcuts to reveal.js API
  1639. else if( typeof value === 'string' && typeof Reveal[ value ] === 'function' ) {
  1640. Reveal[ value ].call();
  1641. }
  1642. triggered = true;
  1643. }
  1644. }
  1645. }
  1646. // 2. System defined key bindings
  1647. if( triggered === false ) {
  1648. // Assume true and try to prove false
  1649. triggered = true;
  1650. switch( event.keyCode ) {
  1651. // p, page up
  1652. case 80: case 33: navigatePrev(); break;
  1653. // n, page down
  1654. case 78: case 34: navigateNext(); break;
  1655. // h, left
  1656. case 72: case 37: navigateLeft(); break;
  1657. // l, right
  1658. case 76: case 39: navigateRight(); break;
  1659. // k, up
  1660. case 75: case 38: navigateUp(); break;
  1661. // j, down
  1662. case 74: case 40: navigateDown(); break;
  1663. // home
  1664. case 36: slide( 0 ); break;
  1665. // end
  1666. case 35: slide( Number.MAX_VALUE ); break;
  1667. // space
  1668. case 32: isOverview() ? deactivateOverview() : event.shiftKey ? navigatePrev() : navigateNext(); break;
  1669. // return
  1670. case 13: isOverview() ? deactivateOverview() : triggered = false; break;
  1671. // b, period, Logitech presenter tools "black screen" button
  1672. case 66: case 190: case 191: togglePause(); break;
  1673. // f
  1674. case 70: enterFullscreen(); break;
  1675. default:
  1676. triggered = false;
  1677. }
  1678. }
  1679. // If the input resulted in a triggered action we should prevent
  1680. // the browsers default behavior
  1681. if( triggered ) {
  1682. event.preventDefault();
  1683. }
  1684. else if ( event.keyCode === 27 && supports3DTransforms ) {
  1685. toggleOverview();
  1686. event.preventDefault();
  1687. }
  1688. // If auto-sliding is enabled we need to cue up
  1689. // another timeout
  1690. cueAutoSlide();
  1691. }
  1692. /**
  1693. * Handler for the 'touchstart' event, enables support for
  1694. * swipe and pinch gestures.
  1695. */
  1696. function onTouchStart( event ) {
  1697. touch.startX = event.touches[0].clientX;
  1698. touch.startY = event.touches[0].clientY;
  1699. touch.startCount = event.touches.length;
  1700. // If there's two touches we need to memorize the distance
  1701. // between those two points to detect pinching
  1702. if( event.touches.length === 2 && config.overview ) {
  1703. touch.startSpan = distanceBetween( {
  1704. x: event.touches[1].clientX,
  1705. y: event.touches[1].clientY
  1706. }, {
  1707. x: touch.startX,
  1708. y: touch.startY
  1709. } );
  1710. }
  1711. }
  1712. /**
  1713. * Handler for the 'touchmove' event.
  1714. */
  1715. function onTouchMove( event ) {
  1716. // Each touch should only trigger one action
  1717. if( !touch.handled ) {
  1718. var currentX = event.touches[0].clientX;
  1719. var currentY = event.touches[0].clientY;
  1720. // If the touch started off with two points and still has
  1721. // two active touches; test for the pinch gesture
  1722. if( event.touches.length === 2 && touch.startCount === 2 && config.overview ) {
  1723. // The current distance in pixels between the two touch points
  1724. var currentSpan = distanceBetween( {
  1725. x: event.touches[1].clientX,
  1726. y: event.touches[1].clientY
  1727. }, {
  1728. x: touch.startX,
  1729. y: touch.startY
  1730. } );
  1731. // If the span is larger than the desire amount we've got
  1732. // ourselves a pinch
  1733. if( Math.abs( touch.startSpan - currentSpan ) > touch.threshold ) {
  1734. touch.handled = true;
  1735. if( currentSpan < touch.startSpan ) {
  1736. activateOverview();
  1737. }
  1738. else {
  1739. deactivateOverview();
  1740. }
  1741. }
  1742. event.preventDefault();
  1743. }
  1744. // There was only one touch point, look for a swipe
  1745. else if( event.touches.length === 1 && touch.startCount !== 2 ) {
  1746. var deltaX = currentX - touch.startX,
  1747. deltaY = currentY - touch.startY;
  1748. if( deltaX > touch.threshold && Math.abs( deltaX ) > Math.abs( deltaY ) ) {
  1749. touch.handled = true;
  1750. navigateLeft();
  1751. }
  1752. else if( deltaX < -touch.threshold && Math.abs( deltaX ) > Math.abs( deltaY ) ) {
  1753. touch.handled = true;
  1754. navigateRight();
  1755. }
  1756. else if( deltaY > touch.threshold ) {
  1757. touch.handled = true;
  1758. navigateUp();
  1759. }
  1760. else if( deltaY < -touch.threshold ) {
  1761. touch.handled = true;
  1762. navigateDown();
  1763. }
  1764. event.preventDefault();
  1765. }
  1766. }
  1767. // There's a bug with swiping on some Android devices unless
  1768. // the default action is always prevented
  1769. else if( navigator.userAgent.match( /android/gi ) ) {
  1770. event.preventDefault();
  1771. }
  1772. }
  1773. /**
  1774. * Handler for the 'touchend' event.
  1775. */
  1776. function onTouchEnd( event ) {
  1777. touch.handled = false;
  1778. }
  1779. /**
  1780. * Convert pointer down to touch start.
  1781. */
  1782. function onPointerDown( event ) {
  1783. if( event.pointerType === event.MSPOINTER_TYPE_TOUCH ) {
  1784. event.touches = [{ clientX: event.clientX, clientY: event.clientY }];
  1785. onTouchStart( event );
  1786. }
  1787. }
  1788. /**
  1789. * Convert pointer move to touch move.
  1790. */
  1791. function onPointerMove( event ) {
  1792. if( event.pointerType === event.MSPOINTER_TYPE_TOUCH ) {
  1793. event.touches = [{ clientX: event.clientX, clientY: event.clientY }];
  1794. onTouchMove( event );
  1795. }
  1796. }
  1797. /**
  1798. * Convert pointer up to touch end.
  1799. */
  1800. function onPointerUp( event ) {
  1801. if( event.pointerType === event.MSPOINTER_TYPE_TOUCH ) {
  1802. event.touches = [{ clientX: event.clientX, clientY: event.clientY }];
  1803. onTouchEnd( event );
  1804. }
  1805. }
  1806. /**
  1807. * Handles mouse wheel scrolling, throttled to avoid skipping
  1808. * multiple slides.
  1809. */
  1810. function onDocumentMouseScroll( event ) {
  1811. if( Date.now() - lastMouseWheelStep > 600 ) {
  1812. lastMouseWheelStep = Date.now();
  1813. var delta = event.detail || -event.wheelDelta;
  1814. if( delta > 0 ) {
  1815. navigateNext();
  1816. }
  1817. else {
  1818. navigatePrev();
  1819. }
  1820. }
  1821. }
  1822. /**
  1823. * Clicking on the progress bar results in a navigation to the
  1824. * closest approximate horizontal slide using this equation:
  1825. *
  1826. * ( clickX / presentationWidth ) * numberOfSlides
  1827. */
  1828. function onProgressClicked( event ) {
  1829. event.preventDefault();
  1830. var slidesTotal = toArray( document.querySelectorAll( HORIZONTAL_SLIDES_SELECTOR ) ).length;
  1831. var slideIndex = Math.floor( ( event.clientX / dom.wrapper.offsetWidth ) * slidesTotal );
  1832. slide( slideIndex );
  1833. }
  1834. /**
  1835. * Event handler for navigation control buttons.
  1836. */
  1837. function onNavigateLeftClicked( event ) { event.preventDefault(); navigateLeft(); }
  1838. function onNavigateRightClicked( event ) { event.preventDefault(); navigateRight(); }
  1839. function onNavigateUpClicked( event ) { event.preventDefault(); navigateUp(); }
  1840. function onNavigateDownClicked( event ) { event.preventDefault(); navigateDown(); }
  1841. function onNavigatePrevClicked( event ) { event.preventDefault(); navigatePrev(); }
  1842. function onNavigateNextClicked( event ) { event.preventDefault(); navigateNext(); }
  1843. /**
  1844. * Handler for the window level 'hashchange' event.
  1845. */
  1846. function onWindowHashChange( event ) {
  1847. readURL();
  1848. }
  1849. /**
  1850. * Handler for the window level 'resize' event.
  1851. */
  1852. function onWindowResize( event ) {
  1853. layout();
  1854. }
  1855. /**
  1856. * Invoked when a slide is and we're in the overview.
  1857. */
  1858. function onOverviewSlideClicked( event ) {
  1859. // TODO There's a bug here where the event listeners are not
  1860. // removed after deactivating the overview.
  1861. if( eventsAreBound && isOverview() ) {
  1862. event.preventDefault();
  1863. var element = event.target;
  1864. while( element && !element.nodeName.match( /section/gi ) ) {
  1865. element = element.parentNode;
  1866. }
  1867. if( element && !element.classList.contains( 'disabled' ) ) {
  1868. deactivateOverview();
  1869. if( element.nodeName.match( /section/gi ) ) {
  1870. var h = parseInt( element.getAttribute( 'data-index-h' ), 10 ),
  1871. v = parseInt( element.getAttribute( 'data-index-v' ), 10 );
  1872. slide( h, v );
  1873. }
  1874. }
  1875. }
  1876. }
  1877. /**
  1878. * Handles clicks on links that are set to preview in the
  1879. * iframe overlay.
  1880. */
  1881. function onPreviewLinkClicked( event ) {
  1882. var url = event.target.getAttribute( 'href' );
  1883. if( url ) {
  1884. openPreview( url );
  1885. event.preventDefault();
  1886. }
  1887. }
  1888. // --------------------------------------------------------------------//
  1889. // ------------------------------- API --------------------------------//
  1890. // --------------------------------------------------------------------//
  1891. return {
  1892. initialize: initialize,
  1893. configure: configure,
  1894. sync: sync,
  1895. // Navigation methods
  1896. slide: slide,
  1897. left: navigateLeft,
  1898. right: navigateRight,
  1899. up: navigateUp,
  1900. down: navigateDown,
  1901. prev: navigatePrev,
  1902. next: navigateNext,
  1903. prevFragment: previousFragment,
  1904. nextFragment: nextFragment,
  1905. // Deprecated aliases
  1906. navigateTo: slide,
  1907. navigateLeft: navigateLeft,
  1908. navigateRight: navigateRight,
  1909. navigateUp: navigateUp,
  1910. navigateDown: navigateDown,
  1911. navigatePrev: navigatePrev,
  1912. navigateNext: navigateNext,
  1913. // Forces an update in slide layout
  1914. layout: layout,
  1915. // Returns an object with the available routes as booleans (left/right/top/bottom)
  1916. availableRoutes: availableRoutes,
  1917. // Returns an object with the available fragments as booleans (prev/next)
  1918. availableFragments: availableFragments,
  1919. // Toggles the overview mode on/off
  1920. toggleOverview: toggleOverview,
  1921. // Toggles the "black screen" mode on/off
  1922. togglePause: togglePause,
  1923. // State checks
  1924. isOverview: isOverview,
  1925. isPaused: isPaused,
  1926. // Adds or removes all internal event listeners (such as keyboard)
  1927. addEventListeners: addEventListeners,
  1928. removeEventListeners: removeEventListeners,
  1929. // Returns the indices of the current, or specified, slide
  1930. getIndices: getIndices,
  1931. // Returns the slide at the specified index, y is optional
  1932. getSlide: function( x, y ) {
  1933. var horizontalSlide = document.querySelectorAll( HORIZONTAL_SLIDES_SELECTOR )[ x ];
  1934. var verticalSlides = horizontalSlide && horizontalSlide.querySelectorAll( 'section' );
  1935. if( typeof y !== 'undefined' ) {
  1936. return verticalSlides ? verticalSlides[ y ] : undefined;
  1937. }
  1938. return horizontalSlide;
  1939. },
  1940. // Returns the previous slide element, may be null
  1941. getPreviousSlide: function() {
  1942. return previousSlide;
  1943. },
  1944. // Returns the current slide element
  1945. getCurrentSlide: function() {
  1946. return currentSlide;
  1947. },
  1948. // Returns the current scale of the presentation content
  1949. getScale: function() {
  1950. return scale;
  1951. },
  1952. // Returns the current configuration object
  1953. getConfig: function() {
  1954. return config;
  1955. },
  1956. // Helper method, retrieves query string as a key/value hash
  1957. getQueryHash: function() {
  1958. var query = {};
  1959. location.search.replace( /[A-Z0-9]+?=(\w*)/gi, function(a) {
  1960. query[ a.split( '=' ).shift() ] = a.split( '=' ).pop();
  1961. } );
  1962. return query;
  1963. },
  1964. // Returns true if we're currently on the first slide
  1965. isFirstSlide: function() {
  1966. return document.querySelector( SLIDES_SELECTOR + '.past' ) == null ? true : false;
  1967. },
  1968. // Returns true if we're currently on the last slide
  1969. isLastSlide: function() {
  1970. if( currentSlide && currentSlide.classList.contains( '.stack' ) ) {
  1971. return currentSlide.querySelector( SLIDES_SELECTOR + '.future' ) == null ? true : false;
  1972. }
  1973. else {
  1974. return document.querySelector( SLIDES_SELECTOR + '.future' ) == null ? true : false;
  1975. }
  1976. },
  1977. // Forward event binding to the reveal DOM element
  1978. addEventListener: function( type, listener, useCapture ) {
  1979. if( 'addEventListener' in window ) {
  1980. ( dom.wrapper || document.querySelector( '.reveal' ) ).addEventListener( type, listener, useCapture );
  1981. }
  1982. },
  1983. removeEventListener: function( type, listener, useCapture ) {
  1984. if( 'addEventListener' in window ) {
  1985. ( dom.wrapper || document.querySelector( '.reveal' ) ).removeEventListener( type, listener, useCapture );
  1986. }
  1987. }
  1988. };
  1989. })();