reveal.js 82 KB

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