reveal.js 71 KB

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