reveal.js 76 KB

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