reveal.js 69 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586
  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/none
  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. var fragments = toArray( element.querySelectorAll( '.fragment.visible' ) );
  1166. // No fragments in future slides should be visible ahead of time
  1167. while( fragments.length ) {
  1168. fragments.pop().classList.remove( 'visible' );
  1169. }
  1170. }
  1171. // If this element contains vertical slides
  1172. if( element.querySelector( 'section' ) ) {
  1173. element.classList.add( 'stack' );
  1174. }
  1175. }
  1176. // Mark the current slide as present
  1177. slides[index].classList.add( 'present' );
  1178. slides[index].removeAttribute( 'hidden' );
  1179. // If this slide has a state associated with it, add it
  1180. // onto the current state of the deck
  1181. var slideState = slides[index].getAttribute( 'data-state' );
  1182. if( slideState ) {
  1183. state = state.concat( slideState.split( ' ' ) );
  1184. }
  1185. // If this slide has a data-autoslide attribute associated use this as
  1186. // autoSlide value otherwise use the global configured time
  1187. var slideAutoSlide = slides[index].getAttribute( 'data-autoslide' );
  1188. if( slideAutoSlide ) {
  1189. autoSlide = parseInt( slideAutoSlide, 10 );
  1190. }
  1191. else {
  1192. autoSlide = config.autoSlide;
  1193. }
  1194. cueAutoSlide();
  1195. }
  1196. else {
  1197. // Since there are no slides we can't be anywhere beyond the
  1198. // zeroth index
  1199. index = 0;
  1200. }
  1201. return index;
  1202. }
  1203. /**
  1204. * Updates the progress bar to reflect the current slide.
  1205. */
  1206. function updateProgress() {
  1207. // Update progress if enabled
  1208. if( config.progress && dom.progress ) {
  1209. var horizontalSlides = toArray( document.querySelectorAll( HORIZONTAL_SLIDES_SELECTOR ) );
  1210. // The number of past and total slides
  1211. var totalCount = document.querySelectorAll( SLIDES_SELECTOR + ':not(.stack)' ).length;
  1212. var pastCount = 0;
  1213. // Step through all slides and count the past ones
  1214. mainLoop: for( var i = 0; i < horizontalSlides.length; i++ ) {
  1215. var horizontalSlide = horizontalSlides[i];
  1216. var verticalSlides = toArray( horizontalSlide.querySelectorAll( 'section' ) );
  1217. for( var j = 0; j < verticalSlides.length; j++ ) {
  1218. // Stop as soon as we arrive at the present
  1219. if( verticalSlides[j].classList.contains( 'present' ) ) {
  1220. break mainLoop;
  1221. }
  1222. pastCount++;
  1223. }
  1224. // Stop as soon as we arrive at the present
  1225. if( horizontalSlide.classList.contains( 'present' ) ) {
  1226. break;
  1227. }
  1228. // Don't count the wrapping section for vertical slides
  1229. if( horizontalSlide.classList.contains( 'stack' ) === false ) {
  1230. pastCount++;
  1231. }
  1232. }
  1233. dom.progressbar.style.width = ( pastCount / ( totalCount - 1 ) ) * window.innerWidth + 'px';
  1234. }
  1235. }
  1236. /**
  1237. * Updates the state of all control/navigation arrows.
  1238. */
  1239. function updateControls() {
  1240. if ( config.controls && dom.controls ) {
  1241. var routes = availableRoutes();
  1242. var fragments = availableFragments();
  1243. // Remove the 'enabled' class from all directions
  1244. dom.controlsLeft.concat( dom.controlsRight )
  1245. .concat( dom.controlsUp )
  1246. .concat( dom.controlsDown )
  1247. .concat( dom.controlsPrev )
  1248. .concat( dom.controlsNext ).forEach( function( node ) {
  1249. node.classList.remove( 'enabled' );
  1250. node.classList.remove( 'fragmented' );
  1251. } );
  1252. // Add the 'enabled' class to the available routes
  1253. if( routes.left ) dom.controlsLeft.forEach( function( el ) { el.classList.add( 'enabled' ); } );
  1254. if( routes.right ) dom.controlsRight.forEach( function( el ) { el.classList.add( 'enabled' ); } );
  1255. if( routes.up ) dom.controlsUp.forEach( function( el ) { el.classList.add( 'enabled' ); } );
  1256. if( routes.down ) dom.controlsDown.forEach( function( el ) { el.classList.add( 'enabled' ); } );
  1257. // Prev/next buttons
  1258. if( routes.left || routes.up ) dom.controlsPrev.forEach( function( el ) { el.classList.add( 'enabled' ); } );
  1259. if( routes.right || routes.down ) dom.controlsNext.forEach( function( el ) { el.classList.add( 'enabled' ); } );
  1260. // Highlight fragment directions
  1261. if( currentSlide ) {
  1262. // Always apply fragment decorator to prev/next buttons
  1263. if( fragments.prev ) dom.controlsPrev.forEach( function( el ) { el.classList.add( 'fragmented', 'enabled' ); } );
  1264. if( fragments.next ) dom.controlsNext.forEach( function( el ) { el.classList.add( 'fragmented', 'enabled' ); } );
  1265. // Apply fragment decorators to directional buttons based on
  1266. // what slide axis they are in
  1267. if( isVerticalSlide( currentSlide ) ) {
  1268. if( fragments.prev ) dom.controlsUp.forEach( function( el ) { el.classList.add( 'fragmented', 'enabled' ); } );
  1269. if( fragments.next ) dom.controlsDown.forEach( function( el ) { el.classList.add( 'fragmented', 'enabled' ); } );
  1270. }
  1271. else {
  1272. if( fragments.prev ) dom.controlsLeft.forEach( function( el ) { el.classList.add( 'fragmented', 'enabled' ); } );
  1273. if( fragments.next ) dom.controlsRight.forEach( function( el ) { el.classList.add( 'fragmented', 'enabled' ); } );
  1274. }
  1275. }
  1276. }
  1277. }
  1278. /**
  1279. * Updates the background elements to reflect the current
  1280. * slide.
  1281. */
  1282. function updateBackground() {
  1283. // Update the classes of all backgrounds to match the
  1284. // states of their slides (past/present/future)
  1285. toArray( dom.background.childNodes ).forEach( function( backgroundh, h ) {
  1286. // Reverse past/future classes when in RTL mode
  1287. var horizontalPast = config.rtl ? 'future' : 'past',
  1288. horizontalFuture = config.rtl ? 'past' : 'future';
  1289. backgroundh.className = 'slide-background ' + ( h < indexh ? horizontalPast : h > indexh ? horizontalFuture : 'present' );
  1290. toArray( backgroundh.childNodes ).forEach( function( backgroundv, v ) {
  1291. backgroundv.className = 'slide-background ' + ( v < indexv ? 'past' : v > indexv ? 'future' : 'present' );
  1292. } );
  1293. } );
  1294. // Allow the first background to apply without transition
  1295. setTimeout( function() {
  1296. dom.background.classList.remove( 'no-transition' );
  1297. }, 1 );
  1298. }
  1299. /**
  1300. * Determine what available routes there are for navigation.
  1301. *
  1302. * @return {Object} containing four booleans: left/right/up/down
  1303. */
  1304. function availableRoutes() {
  1305. var horizontalSlides = document.querySelectorAll( HORIZONTAL_SLIDES_SELECTOR ),
  1306. verticalSlides = document.querySelectorAll( VERTICAL_SLIDES_SELECTOR );
  1307. var routes = {
  1308. left: indexh > 0 || config.loop,
  1309. right: indexh < horizontalSlides.length - 1 || config.loop,
  1310. up: indexv > 0,
  1311. down: indexv < verticalSlides.length - 1
  1312. };
  1313. // reverse horizontal controls for rtl
  1314. if( config.rtl ) {
  1315. var left = routes.left;
  1316. routes.left = routes.right;
  1317. routes.right = left;
  1318. }
  1319. return routes;
  1320. }
  1321. /**
  1322. * Returns an object describing the available fragment
  1323. * directions.
  1324. *
  1325. * @return {Object} two boolean properties: prev/next
  1326. */
  1327. function availableFragments() {
  1328. if( currentSlide && config.fragments ) {
  1329. var fragments = currentSlide.querySelectorAll( '.fragment' );
  1330. var hiddenFragments = currentSlide.querySelectorAll( '.fragment:not(.visible)' );
  1331. return {
  1332. prev: fragments.length - hiddenFragments.length > 0,
  1333. next: !!hiddenFragments.length
  1334. };
  1335. }
  1336. else {
  1337. return { prev: false, next: false };
  1338. }
  1339. }
  1340. /**
  1341. * Start playback of any embedded content inside of
  1342. * the targeted slide.
  1343. */
  1344. function startEmbeddedContent( slide ) {
  1345. if( slide ) {
  1346. // HTML5 media elements
  1347. toArray( slide.querySelectorAll( 'video, audio' ) ).forEach( function( el ) {
  1348. if( el.hasAttribute( 'data-autoplay' ) ) {
  1349. el.play();
  1350. }
  1351. } );
  1352. // YouTube embeds
  1353. toArray( slide.querySelectorAll( 'iframe[src*="youtube.com/embed/"]' ) ).forEach( function( el ) {
  1354. if( el.hasAttribute( 'data-autoplay' ) ) {
  1355. el.contentWindow.postMessage('{"event":"command","func":"playVideo","args":""}', '*');
  1356. }
  1357. });
  1358. }
  1359. }
  1360. /**
  1361. * Stop playback of any embedded content inside of
  1362. * the targeted slide.
  1363. */
  1364. function stopEmbeddedContent( slide ) {
  1365. if( slide ) {
  1366. // HTML5 media elements
  1367. toArray( slide.querySelectorAll( 'video, audio' ) ).forEach( function( el ) {
  1368. if( !el.hasAttribute( 'data-ignore' ) ) {
  1369. el.pause();
  1370. }
  1371. } );
  1372. // YouTube embeds
  1373. toArray( slide.querySelectorAll( 'iframe[src*="youtube.com/embed/"]' ) ).forEach( function( el ) {
  1374. if( !el.hasAttribute( 'data-ignore' ) && typeof el.contentWindow.postMessage === 'function' ) {
  1375. el.contentWindow.postMessage('{"event":"command","func":"pauseVideo","args":""}', '*');
  1376. }
  1377. });
  1378. }
  1379. }
  1380. /**
  1381. * Reads the current URL (hash) and navigates accordingly.
  1382. */
  1383. function readURL() {
  1384. var hash = window.location.hash;
  1385. // Attempt to parse the hash as either an index or name
  1386. var bits = hash.slice( 2 ).split( '/' ),
  1387. name = hash.replace( /#|\//gi, '' );
  1388. // If the first bit is invalid and there is a name we can
  1389. // assume that this is a named link
  1390. if( isNaN( parseInt( bits[0], 10 ) ) && name.length ) {
  1391. // Find the slide with the specified name
  1392. var element = document.querySelector( '#' + name );
  1393. if( element ) {
  1394. // Find the position of the named slide and navigate to it
  1395. var indices = Reveal.getIndices( element );
  1396. slide( indices.h, indices.v );
  1397. }
  1398. // If the slide doesn't exist, navigate to the current slide
  1399. else {
  1400. slide( indexh, indexv );
  1401. }
  1402. }
  1403. else {
  1404. // Read the index components of the hash
  1405. var h = parseInt( bits[0], 10 ) || 0,
  1406. v = parseInt( bits[1], 10 ) || 0;
  1407. slide( h, v );
  1408. }
  1409. }
  1410. /**
  1411. * Updates the page URL (hash) to reflect the current
  1412. * state.
  1413. *
  1414. * @param {Number} delay The time in ms to wait before
  1415. * writing the hash
  1416. */
  1417. function writeURL( delay ) {
  1418. if( config.history ) {
  1419. // Make sure there's never more than one timeout running
  1420. clearTimeout( writeURLTimeout );
  1421. // If a delay is specified, timeout this call
  1422. if( typeof delay === 'number' ) {
  1423. writeURLTimeout = setTimeout( writeURL, delay );
  1424. }
  1425. else {
  1426. var url = '/';
  1427. // If the current slide has an ID, use that as a named link
  1428. if( currentSlide && typeof currentSlide.getAttribute( 'id' ) === 'string' ) {
  1429. url = '/' + currentSlide.getAttribute( 'id' );
  1430. }
  1431. // Otherwise use the /h/v index
  1432. else {
  1433. if( indexh > 0 || indexv > 0 ) url += indexh;
  1434. if( indexv > 0 ) url += '/' + indexv;
  1435. }
  1436. window.location.hash = url;
  1437. }
  1438. }
  1439. }
  1440. /**
  1441. * Retrieves the h/v location of the current, or specified,
  1442. * slide.
  1443. *
  1444. * @param {HTMLElement} slide If specified, the returned
  1445. * index will be for this slide rather than the currently
  1446. * active one
  1447. *
  1448. * @return {Object} { h: <int>, v: <int>, f: <int> }
  1449. */
  1450. function getIndices( slide ) {
  1451. // By default, return the current indices
  1452. var h = indexh,
  1453. v = indexv,
  1454. f;
  1455. // If a slide is specified, return the indices of that slide
  1456. if( slide ) {
  1457. var isVertical = isVerticalSlide( slide );
  1458. var slideh = isVertical ? slide.parentNode : slide;
  1459. // Select all horizontal slides
  1460. var horizontalSlides = toArray( document.querySelectorAll( HORIZONTAL_SLIDES_SELECTOR ) );
  1461. // Now that we know which the horizontal slide is, get its index
  1462. h = Math.max( horizontalSlides.indexOf( slideh ), 0 );
  1463. // If this is a vertical slide, grab the vertical index
  1464. if( isVertical ) {
  1465. v = Math.max( toArray( slide.parentNode.querySelectorAll( 'section' ) ).indexOf( slide ), 0 );
  1466. }
  1467. }
  1468. if( !slide && currentSlide ) {
  1469. var visibleFragments = currentSlide.querySelectorAll( '.fragment.visible' );
  1470. if( visibleFragments.length ) {
  1471. f = visibleFragments.length;
  1472. }
  1473. }
  1474. return { h: h, v: v, f: f };
  1475. }
  1476. /**
  1477. * Navigate to the next slide fragment.
  1478. *
  1479. * @return {Boolean} true if there was a next fragment,
  1480. * false otherwise
  1481. */
  1482. function nextFragment() {
  1483. if( currentSlide && config.fragments ) {
  1484. var fragments = sortFragments( currentSlide.querySelectorAll( '.fragment:not(.visible)' ) );
  1485. if( fragments.length ) {
  1486. // Find the index of the next fragment
  1487. var index = fragments[0].getAttribute( 'data-fragment-index' );
  1488. // Find all fragments with the same index
  1489. fragments = currentSlide.querySelectorAll( '.fragment[data-fragment-index="'+ index +'"]' );
  1490. toArray( fragments ).forEach( function( element ) {
  1491. element.classList.add( 'visible' );
  1492. } );
  1493. // Notify subscribers of the change
  1494. dispatchEvent( 'fragmentshown', { fragment: fragments[0], fragments: fragments } );
  1495. updateControls();
  1496. return true;
  1497. }
  1498. }
  1499. return false;
  1500. }
  1501. /**
  1502. * Navigate to the previous slide fragment.
  1503. *
  1504. * @return {Boolean} true if there was a previous fragment,
  1505. * false otherwise
  1506. */
  1507. function previousFragment() {
  1508. if( currentSlide && config.fragments ) {
  1509. var fragments = sortFragments( currentSlide.querySelectorAll( '.fragment.visible' ) );
  1510. if( fragments.length ) {
  1511. // Find the index of the previous fragment
  1512. var index = fragments[ fragments.length - 1 ].getAttribute( 'data-fragment-index' );
  1513. // Find all fragments with the same index
  1514. fragments = currentSlide.querySelectorAll( '.fragment[data-fragment-index="'+ index +'"]' );
  1515. toArray( fragments ).forEach( function( f ) {
  1516. f.classList.remove( 'visible' );
  1517. } );
  1518. // Notify subscribers of the change
  1519. dispatchEvent( 'fragmenthidden', { fragment: fragments[0], fragments: fragments } );
  1520. updateControls();
  1521. return true;
  1522. }
  1523. }
  1524. return false;
  1525. }
  1526. /**
  1527. * Cues a new automated slide if enabled in the config.
  1528. */
  1529. function cueAutoSlide() {
  1530. clearTimeout( autoSlideTimeout );
  1531. // Cue the next auto-slide if enabled
  1532. if( autoSlide && !isPaused() && !isOverview() ) {
  1533. autoSlideTimeout = setTimeout( navigateNext, autoSlide );
  1534. }
  1535. }
  1536. /**
  1537. * Cancels any ongoing request to auto-slide.
  1538. */
  1539. function cancelAutoSlide() {
  1540. clearTimeout( autoSlideTimeout );
  1541. }
  1542. function navigateLeft() {
  1543. // Reverse for RTL
  1544. if( config.rtl ) {
  1545. if( ( isOverview() || nextFragment() === false ) && availableRoutes().left ) {
  1546. slide( indexh + 1 );
  1547. }
  1548. }
  1549. // Normal navigation
  1550. else if( ( isOverview() || previousFragment() === false ) && availableRoutes().left ) {
  1551. slide( indexh - 1 );
  1552. }
  1553. }
  1554. function navigateRight() {
  1555. // Reverse for RTL
  1556. if( config.rtl ) {
  1557. if( ( isOverview() || previousFragment() === false ) && availableRoutes().right ) {
  1558. slide( indexh - 1 );
  1559. }
  1560. }
  1561. // Normal navigation
  1562. else if( ( isOverview() || nextFragment() === false ) && availableRoutes().right ) {
  1563. slide( indexh + 1 );
  1564. }
  1565. }
  1566. function navigateUp() {
  1567. // Prioritize hiding fragments
  1568. if( ( isOverview() || previousFragment() === false ) && availableRoutes().up ) {
  1569. slide( indexh, indexv - 1 );
  1570. }
  1571. }
  1572. function navigateDown() {
  1573. // Prioritize revealing fragments
  1574. if( ( isOverview() || nextFragment() === false ) && availableRoutes().down ) {
  1575. slide( indexh, indexv + 1 );
  1576. }
  1577. }
  1578. /**
  1579. * Navigates backwards, prioritized in the following order:
  1580. * 1) Previous fragment
  1581. * 2) Previous vertical slide
  1582. * 3) Previous horizontal slide
  1583. */
  1584. function navigatePrev() {
  1585. // Prioritize revealing fragments
  1586. if( previousFragment() === false ) {
  1587. if( availableRoutes().up ) {
  1588. navigateUp();
  1589. }
  1590. else {
  1591. // Fetch the previous horizontal slide, if there is one
  1592. var previousSlide = document.querySelector( HORIZONTAL_SLIDES_SELECTOR + '.past:nth-child(' + indexh + ')' );
  1593. if( previousSlide ) {
  1594. var v = ( previousSlide.querySelectorAll( 'section' ).length - 1 ) || undefined;
  1595. var h = indexh - 1;
  1596. slide( h, v );
  1597. }
  1598. }
  1599. }
  1600. }
  1601. /**
  1602. * Same as #navigatePrev() but navigates forwards.
  1603. */
  1604. function navigateNext() {
  1605. // Prioritize revealing fragments
  1606. if( nextFragment() === false ) {
  1607. availableRoutes().down ? navigateDown() : navigateRight();
  1608. }
  1609. // If auto-sliding is enabled we need to cue up
  1610. // another timeout
  1611. cueAutoSlide();
  1612. }
  1613. // --------------------------------------------------------------------//
  1614. // ----------------------------- EVENTS -------------------------------//
  1615. // --------------------------------------------------------------------//
  1616. /**
  1617. * Handler for the document level 'keydown' event.
  1618. *
  1619. * @param {Object} event
  1620. */
  1621. function onDocumentKeyDown( event ) {
  1622. // Check if there's a focused element that could be using
  1623. // the keyboard
  1624. var activeElement = document.activeElement;
  1625. var hasFocus = !!( document.activeElement && ( document.activeElement.type || document.activeElement.href || document.activeElement.contentEditable !== 'inherit' ) );
  1626. // Disregard the event if there's a focused element or a
  1627. // keyboard modifier key is present
  1628. if( hasFocus || (event.shiftKey && event.keyCode !== 32) || event.altKey || event.ctrlKey || event.metaKey ) return;
  1629. // While paused only allow "unpausing" keyboard events (b and .)
  1630. if( isPaused() && [66,190,191].indexOf( event.keyCode ) === -1 ) {
  1631. return false;
  1632. }
  1633. var triggered = false;
  1634. // 1. User defined key bindings
  1635. if( typeof config.keyboard === 'object' ) {
  1636. for( var key in config.keyboard ) {
  1637. // Check if this binding matches the pressed key
  1638. if( parseInt( key, 10 ) === event.keyCode ) {
  1639. var value = config.keyboard[ key ];
  1640. // Callback function
  1641. if( typeof value === 'function' ) {
  1642. value.apply( null, [ event ] );
  1643. }
  1644. // String shortcuts to reveal.js API
  1645. else if( typeof value === 'string' && typeof Reveal[ value ] === 'function' ) {
  1646. Reveal[ value ].call();
  1647. }
  1648. triggered = true;
  1649. }
  1650. }
  1651. }
  1652. // 2. System defined key bindings
  1653. if( triggered === false ) {
  1654. // Assume true and try to prove false
  1655. triggered = true;
  1656. switch( event.keyCode ) {
  1657. // p, page up
  1658. case 80: case 33: navigatePrev(); break;
  1659. // n, page down
  1660. case 78: case 34: navigateNext(); break;
  1661. // h, left
  1662. case 72: case 37: navigateLeft(); break;
  1663. // l, right
  1664. case 76: case 39: navigateRight(); break;
  1665. // k, up
  1666. case 75: case 38: navigateUp(); break;
  1667. // j, down
  1668. case 74: case 40: navigateDown(); break;
  1669. // home
  1670. case 36: slide( 0 ); break;
  1671. // end
  1672. case 35: slide( Number.MAX_VALUE ); break;
  1673. // space
  1674. case 32: isOverview() ? deactivateOverview() : event.shiftKey ? navigatePrev() : navigateNext(); break;
  1675. // return
  1676. case 13: isOverview() ? deactivateOverview() : triggered = false; break;
  1677. // b, period, Logitech presenter tools "black screen" button
  1678. case 66: case 190: case 191: togglePause(); break;
  1679. // f
  1680. case 70: enterFullscreen(); break;
  1681. default:
  1682. triggered = false;
  1683. }
  1684. }
  1685. // If the input resulted in a triggered action we should prevent
  1686. // the browsers default behavior
  1687. if( triggered ) {
  1688. event.preventDefault();
  1689. }
  1690. else if ( event.keyCode === 27 && supports3DTransforms ) {
  1691. toggleOverview();
  1692. event.preventDefault();
  1693. }
  1694. // If auto-sliding is enabled we need to cue up
  1695. // another timeout
  1696. cueAutoSlide();
  1697. }
  1698. /**
  1699. * Handler for the 'touchstart' event, enables support for
  1700. * swipe and pinch gestures.
  1701. */
  1702. function onTouchStart( event ) {
  1703. touch.startX = event.touches[0].clientX;
  1704. touch.startY = event.touches[0].clientY;
  1705. touch.startCount = event.touches.length;
  1706. // If there's two touches we need to memorize the distance
  1707. // between those two points to detect pinching
  1708. if( event.touches.length === 2 && config.overview ) {
  1709. touch.startSpan = distanceBetween( {
  1710. x: event.touches[1].clientX,
  1711. y: event.touches[1].clientY
  1712. }, {
  1713. x: touch.startX,
  1714. y: touch.startY
  1715. } );
  1716. }
  1717. }
  1718. /**
  1719. * Handler for the 'touchmove' event.
  1720. */
  1721. function onTouchMove( event ) {
  1722. // Each touch should only trigger one action
  1723. if( !touch.handled ) {
  1724. var currentX = event.touches[0].clientX;
  1725. var currentY = event.touches[0].clientY;
  1726. // If the touch started off with two points and still has
  1727. // two active touches; test for the pinch gesture
  1728. if( event.touches.length === 2 && touch.startCount === 2 && config.overview ) {
  1729. // The current distance in pixels between the two touch points
  1730. var currentSpan = distanceBetween( {
  1731. x: event.touches[1].clientX,
  1732. y: event.touches[1].clientY
  1733. }, {
  1734. x: touch.startX,
  1735. y: touch.startY
  1736. } );
  1737. // If the span is larger than the desire amount we've got
  1738. // ourselves a pinch
  1739. if( Math.abs( touch.startSpan - currentSpan ) > touch.threshold ) {
  1740. touch.handled = true;
  1741. if( currentSpan < touch.startSpan ) {
  1742. activateOverview();
  1743. }
  1744. else {
  1745. deactivateOverview();
  1746. }
  1747. }
  1748. event.preventDefault();
  1749. }
  1750. // There was only one touch point, look for a swipe
  1751. else if( event.touches.length === 1 && touch.startCount !== 2 ) {
  1752. var deltaX = currentX - touch.startX,
  1753. deltaY = currentY - touch.startY;
  1754. if( deltaX > touch.threshold && Math.abs( deltaX ) > Math.abs( deltaY ) ) {
  1755. touch.handled = true;
  1756. navigateLeft();
  1757. }
  1758. else if( deltaX < -touch.threshold && Math.abs( deltaX ) > Math.abs( deltaY ) ) {
  1759. touch.handled = true;
  1760. navigateRight();
  1761. }
  1762. else if( deltaY > touch.threshold ) {
  1763. touch.handled = true;
  1764. navigateUp();
  1765. }
  1766. else if( deltaY < -touch.threshold ) {
  1767. touch.handled = true;
  1768. navigateDown();
  1769. }
  1770. event.preventDefault();
  1771. }
  1772. }
  1773. // There's a bug with swiping on some Android devices unless
  1774. // the default action is always prevented
  1775. else if( navigator.userAgent.match( /android/gi ) ) {
  1776. event.preventDefault();
  1777. }
  1778. }
  1779. /**
  1780. * Handler for the 'touchend' event.
  1781. */
  1782. function onTouchEnd( event ) {
  1783. touch.handled = false;
  1784. }
  1785. /**
  1786. * Convert pointer down to touch start.
  1787. */
  1788. function onPointerDown( event ) {
  1789. if( event.pointerType === event.MSPOINTER_TYPE_TOUCH ) {
  1790. event.touches = [{ clientX: event.clientX, clientY: event.clientY }];
  1791. onTouchStart( event );
  1792. }
  1793. }
  1794. /**
  1795. * Convert pointer move to touch move.
  1796. */
  1797. function onPointerMove( event ) {
  1798. if( event.pointerType === event.MSPOINTER_TYPE_TOUCH ) {
  1799. event.touches = [{ clientX: event.clientX, clientY: event.clientY }];
  1800. onTouchMove( event );
  1801. }
  1802. }
  1803. /**
  1804. * Convert pointer up to touch end.
  1805. */
  1806. function onPointerUp( event ) {
  1807. if( event.pointerType === event.MSPOINTER_TYPE_TOUCH ) {
  1808. event.touches = [{ clientX: event.clientX, clientY: event.clientY }];
  1809. onTouchEnd( event );
  1810. }
  1811. }
  1812. /**
  1813. * Handles mouse wheel scrolling, throttled to avoid skipping
  1814. * multiple slides.
  1815. */
  1816. function onDocumentMouseScroll( event ) {
  1817. if( Date.now() - lastMouseWheelStep > 600 ) {
  1818. lastMouseWheelStep = Date.now();
  1819. var delta = event.detail || -event.wheelDelta;
  1820. if( delta > 0 ) {
  1821. navigateNext();
  1822. }
  1823. else {
  1824. navigatePrev();
  1825. }
  1826. }
  1827. }
  1828. /**
  1829. * Clicking on the progress bar results in a navigation to the
  1830. * closest approximate horizontal slide using this equation:
  1831. *
  1832. * ( clickX / presentationWidth ) * numberOfSlides
  1833. */
  1834. function onProgressClicked( event ) {
  1835. event.preventDefault();
  1836. var slidesTotal = toArray( document.querySelectorAll( HORIZONTAL_SLIDES_SELECTOR ) ).length;
  1837. var slideIndex = Math.floor( ( event.clientX / dom.wrapper.offsetWidth ) * slidesTotal );
  1838. slide( slideIndex );
  1839. }
  1840. /**
  1841. * Event handler for navigation control buttons.
  1842. */
  1843. function onNavigateLeftClicked( event ) { event.preventDefault(); navigateLeft(); }
  1844. function onNavigateRightClicked( event ) { event.preventDefault(); navigateRight(); }
  1845. function onNavigateUpClicked( event ) { event.preventDefault(); navigateUp(); }
  1846. function onNavigateDownClicked( event ) { event.preventDefault(); navigateDown(); }
  1847. function onNavigatePrevClicked( event ) { event.preventDefault(); navigatePrev(); }
  1848. function onNavigateNextClicked( event ) { event.preventDefault(); navigateNext(); }
  1849. /**
  1850. * Handler for the window level 'hashchange' event.
  1851. */
  1852. function onWindowHashChange( event ) {
  1853. readURL();
  1854. }
  1855. /**
  1856. * Handler for the window level 'resize' event.
  1857. */
  1858. function onWindowResize( event ) {
  1859. layout();
  1860. }
  1861. /**
  1862. * Invoked when a slide is and we're in the overview.
  1863. */
  1864. function onOverviewSlideClicked( event ) {
  1865. // TODO There's a bug here where the event listeners are not
  1866. // removed after deactivating the overview.
  1867. if( eventsAreBound && isOverview() ) {
  1868. event.preventDefault();
  1869. var element = event.target;
  1870. while( element && !element.nodeName.match( /section/gi ) ) {
  1871. element = element.parentNode;
  1872. }
  1873. if( element && !element.classList.contains( 'disabled' ) ) {
  1874. deactivateOverview();
  1875. if( element.nodeName.match( /section/gi ) ) {
  1876. var h = parseInt( element.getAttribute( 'data-index-h' ), 10 ),
  1877. v = parseInt( element.getAttribute( 'data-index-v' ), 10 );
  1878. slide( h, v );
  1879. }
  1880. }
  1881. }
  1882. }
  1883. /**
  1884. * Handles clicks on links that are set to preview in the
  1885. * iframe overlay.
  1886. */
  1887. function onPreviewLinkClicked( event ) {
  1888. var url = event.target.getAttribute( 'href' );
  1889. if( url ) {
  1890. openPreview( url );
  1891. event.preventDefault();
  1892. }
  1893. }
  1894. // --------------------------------------------------------------------//
  1895. // ------------------------------- API --------------------------------//
  1896. // --------------------------------------------------------------------//
  1897. return {
  1898. initialize: initialize,
  1899. configure: configure,
  1900. sync: sync,
  1901. // Navigation methods
  1902. slide: slide,
  1903. left: navigateLeft,
  1904. right: navigateRight,
  1905. up: navigateUp,
  1906. down: navigateDown,
  1907. prev: navigatePrev,
  1908. next: navigateNext,
  1909. prevFragment: previousFragment,
  1910. nextFragment: nextFragment,
  1911. // Deprecated aliases
  1912. navigateTo: slide,
  1913. navigateLeft: navigateLeft,
  1914. navigateRight: navigateRight,
  1915. navigateUp: navigateUp,
  1916. navigateDown: navigateDown,
  1917. navigatePrev: navigatePrev,
  1918. navigateNext: navigateNext,
  1919. // Forces an update in slide layout
  1920. layout: layout,
  1921. // Returns an object with the available routes as booleans (left/right/top/bottom)
  1922. availableRoutes: availableRoutes,
  1923. // Returns an object with the available fragments as booleans (prev/next)
  1924. availableFragments: availableFragments,
  1925. // Toggles the overview mode on/off
  1926. toggleOverview: toggleOverview,
  1927. // Toggles the "black screen" mode on/off
  1928. togglePause: togglePause,
  1929. // State checks
  1930. isOverview: isOverview,
  1931. isPaused: isPaused,
  1932. // Adds or removes all internal event listeners (such as keyboard)
  1933. addEventListeners: addEventListeners,
  1934. removeEventListeners: removeEventListeners,
  1935. // Returns the indices of the current, or specified, slide
  1936. getIndices: getIndices,
  1937. // Returns the slide at the specified index, y is optional
  1938. getSlide: function( x, y ) {
  1939. var horizontalSlide = document.querySelectorAll( HORIZONTAL_SLIDES_SELECTOR )[ x ];
  1940. var verticalSlides = horizontalSlide && horizontalSlide.querySelectorAll( 'section' );
  1941. if( typeof y !== 'undefined' ) {
  1942. return verticalSlides ? verticalSlides[ y ] : undefined;
  1943. }
  1944. return horizontalSlide;
  1945. },
  1946. // Returns the previous slide element, may be null
  1947. getPreviousSlide: function() {
  1948. return previousSlide;
  1949. },
  1950. // Returns the current slide element
  1951. getCurrentSlide: function() {
  1952. return currentSlide;
  1953. },
  1954. // Returns the current scale of the presentation content
  1955. getScale: function() {
  1956. return scale;
  1957. },
  1958. // Returns the current configuration object
  1959. getConfig: function() {
  1960. return config;
  1961. },
  1962. // Helper method, retrieves query string as a key/value hash
  1963. getQueryHash: function() {
  1964. var query = {};
  1965. location.search.replace( /[A-Z0-9]+?=(\w*)/gi, function(a) {
  1966. query[ a.split( '=' ).shift() ] = a.split( '=' ).pop();
  1967. } );
  1968. return query;
  1969. },
  1970. // Returns true if we're currently on the first slide
  1971. isFirstSlide: function() {
  1972. return document.querySelector( SLIDES_SELECTOR + '.past' ) == null ? true : false;
  1973. },
  1974. // Returns true if we're currently on the last slide
  1975. isLastSlide: function() {
  1976. if( currentSlide && currentSlide.classList.contains( '.stack' ) ) {
  1977. return currentSlide.querySelector( SLIDES_SELECTOR + '.future' ) == null ? true : false;
  1978. }
  1979. else {
  1980. return document.querySelector( SLIDES_SELECTOR + '.future' ) == null ? true : false;
  1981. }
  1982. },
  1983. // Forward event binding to the reveal DOM element
  1984. addEventListener: function( type, listener, useCapture ) {
  1985. if( 'addEventListener' in window ) {
  1986. ( dom.wrapper || document.querySelector( '.reveal' ) ).addEventListener( type, listener, useCapture );
  1987. }
  1988. },
  1989. removeEventListener: function( type, listener, useCapture ) {
  1990. if( 'addEventListener' in window ) {
  1991. ( dom.wrapper || document.querySelector( '.reveal' ) ).removeEventListener( type, listener, useCapture );
  1992. }
  1993. }
  1994. };
  1995. })();