reveal.js 74 KB

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