reveal.js 83 KB

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