reveal.js 117 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380
  1. /*!
  2. * reveal.js
  3. * http://lab.hakim.se/reveal-js
  4. * MIT licensed
  5. *
  6. * Copyright (C) 2015 Hakim El Hattab, http://hakim.se
  7. */
  8. (function( root, factory ) {
  9. if( typeof define === 'function' && define.amd ) {
  10. // AMD. Register as an anonymous module.
  11. define( function() {
  12. root.Reveal = factory();
  13. return root.Reveal;
  14. } );
  15. } else if( typeof exports === 'object' ) {
  16. // Node. Does not work with strict CommonJS.
  17. module.exports = factory();
  18. } else {
  19. // Browser globals.
  20. root.Reveal = factory();
  21. }
  22. }( this, function() {
  23. 'use strict';
  24. var Reveal;
  25. var SLIDES_SELECTOR = '.slides section',
  26. HORIZONTAL_SLIDES_SELECTOR = '.slides>section',
  27. VERTICAL_SLIDES_SELECTOR = '.slides>section.present>section',
  28. HOME_SLIDE_SELECTOR = '.slides>section:first-of-type',
  29. // Configurations defaults, can be overridden at initialization time
  30. config = {
  31. // The "normal" size of the presentation, aspect ratio will be preserved
  32. // when the presentation is scaled to fit different resolutions
  33. width: 960,
  34. height: 700,
  35. // Factor of the display size that should remain empty around the content
  36. margin: 0.1,
  37. // Bounds for smallest/largest possible scale to apply to content
  38. minScale: 0.2,
  39. maxScale: 1.5,
  40. // Display controls in the bottom right corner
  41. controls: true,
  42. // Display a presentation progress bar
  43. progress: true,
  44. // Display the page number of the current slide
  45. slideNumber: false,
  46. // Push each slide change to the browser history
  47. history: false,
  48. // Enable keyboard shortcuts for navigation
  49. keyboard: true,
  50. // Optional function that blocks keyboard events when retuning false
  51. keyboardCondition: null,
  52. // Enable the slide overview mode
  53. overview: true,
  54. // Vertical centering of slides
  55. center: true,
  56. // Enables touch navigation on devices with touch input
  57. touch: true,
  58. // Loop the presentation
  59. loop: false,
  60. // Change the presentation direction to be RTL
  61. rtl: false,
  62. // Turns fragments on and off globally
  63. fragments: true,
  64. // Flags if the presentation is running in an embedded mode,
  65. // i.e. contained within a limited portion of the screen
  66. embedded: false,
  67. // Flags if we should show a help overlay when the questionmark
  68. // key is pressed
  69. help: true,
  70. // Flags if it should be possible to pause the presentation (blackout)
  71. pause: true,
  72. // Number of milliseconds between automatically proceeding to the
  73. // next slide, disabled when set to 0, this value can be overwritten
  74. // by using a data-autoslide attribute on your slides
  75. autoSlide: 0,
  76. // Stop auto-sliding after user input
  77. autoSlideStoppable: true,
  78. // Enable slide navigation via mouse wheel
  79. mouseWheel: false,
  80. // Apply a 3D roll to links on hover
  81. rollingLinks: false,
  82. // Hides the address bar on mobile devices
  83. hideAddressBar: true,
  84. // Opens links in an iframe preview overlay
  85. previewLinks: false,
  86. // Exposes the reveal.js API through window.postMessage
  87. postMessage: true,
  88. // Dispatches all reveal.js events to the parent window through postMessage
  89. postMessageEvents: false,
  90. // Focuses body when page changes visiblity to ensure keyboard shortcuts work
  91. focusBodyOnPageVisibilityChange: true,
  92. // Transition style
  93. transition: 'slide', // none/fade/slide/convex/concave/zoom
  94. // Transition speed
  95. transitionSpeed: 'default', // default/fast/slow
  96. // Transition style for full page slide backgrounds
  97. backgroundTransition: 'fade', // none/fade/slide/convex/concave/zoom
  98. // Parallax background image
  99. parallaxBackgroundImage: '', // CSS syntax, e.g. "a.jpg"
  100. // Parallax background size
  101. parallaxBackgroundSize: '', // CSS syntax, e.g. "3000px 2000px"
  102. // Number of slides away from the current that are visible
  103. viewDistance: 3,
  104. // Script dependencies to load
  105. dependencies: []
  106. },
  107. // Flags if reveal.js is loaded (has dispatched the 'ready' event)
  108. loaded = false,
  109. // Flags if the overview mode is currently active
  110. overview = false,
  111. // The horizontal and vertical index of the currently active slide
  112. indexh,
  113. indexv,
  114. // The previous and current slide HTML elements
  115. previousSlide,
  116. currentSlide,
  117. previousBackground,
  118. // Slides may hold a data-state attribute which we pick up and apply
  119. // as a class to the body. This list contains the combined state of
  120. // all current slides.
  121. state = [],
  122. // The current scale of the presentation (see width/height config)
  123. scale = 1,
  124. // CSS transform that is currently applied to the slides container,
  125. // split into two groups
  126. slidesTransform = { layout: '', overview: '' },
  127. // Cached references to DOM elements
  128. dom = {},
  129. // Features supported by the browser, see #checkCapabilities()
  130. features = {},
  131. // Client is a mobile device, see #checkCapabilities()
  132. isMobileDevice,
  133. // Throttles mouse wheel navigation
  134. lastMouseWheelStep = 0,
  135. // Delays updates to the URL due to a Chrome thumbnailer bug
  136. writeURLTimeout = 0,
  137. // Flags if the interaction event listeners are bound
  138. eventsAreBound = false,
  139. // The current auto-slide duration
  140. autoSlide = 0,
  141. // Auto slide properties
  142. autoSlidePlayer,
  143. autoSlideTimeout = 0,
  144. autoSlideStartTime = -1,
  145. autoSlidePaused = false,
  146. // Holds information about the currently ongoing touch input
  147. touch = {
  148. startX: 0,
  149. startY: 0,
  150. startSpan: 0,
  151. startCount: 0,
  152. captured: false,
  153. threshold: 40
  154. },
  155. // Holds information about the keyboard shortcuts
  156. keyboardShortcuts = {
  157. 'N , SPACE': 'Next slide',
  158. 'P': 'Previous slide',
  159. '← , H': 'Navigate left',
  160. '→ , L': 'Navigate right',
  161. '↑ , K': 'Navigate up',
  162. '↓ , J': 'Navigate down',
  163. 'Home': 'First slide',
  164. 'End': 'Last slide',
  165. 'B , .': 'Pause',
  166. 'F': 'Fullscreen',
  167. 'ESC, O': 'Slide overview'
  168. };
  169. /**
  170. * Starts up the presentation if the client is capable.
  171. */
  172. function initialize( options ) {
  173. checkCapabilities();
  174. if( !features.transforms2d && !features.transforms3d ) {
  175. document.body.setAttribute( 'class', 'no-transforms' );
  176. // Since JS won't be running any further, we need to load all
  177. // images that were intended to lazy load now
  178. var images = document.getElementsByTagName( 'img' );
  179. for( var i = 0, len = images.length; i < len; i++ ) {
  180. var image = images[i];
  181. if( image.getAttribute( 'data-src' ) ) {
  182. image.setAttribute( 'src', image.getAttribute( 'data-src' ) );
  183. image.removeAttribute( 'data-src' );
  184. }
  185. }
  186. // If the browser doesn't support core features we won't be
  187. // using JavaScript to control the presentation
  188. return;
  189. }
  190. // Cache references to key DOM elements
  191. dom.wrapper = document.querySelector( '.reveal' );
  192. dom.slides = document.querySelector( '.reveal .slides' );
  193. // Force a layout when the whole page, incl fonts, has loaded
  194. window.addEventListener( 'load', layout, false );
  195. var query = Reveal.getQueryHash();
  196. // Do not accept new dependencies via query config to avoid
  197. // the potential of malicious script injection
  198. if( typeof query['dependencies'] !== 'undefined' ) delete query['dependencies'];
  199. // Copy options over to our config object
  200. extend( config, options );
  201. extend( config, query );
  202. // Hide the address bar in mobile browsers
  203. hideAddressBar();
  204. // Loads the dependencies and continues to #start() once done
  205. load();
  206. }
  207. /**
  208. * Inspect the client to see what it's capable of, this
  209. * should only happens once per runtime.
  210. */
  211. function checkCapabilities() {
  212. features.transforms3d = 'WebkitPerspective' in document.body.style ||
  213. 'MozPerspective' in document.body.style ||
  214. 'msPerspective' in document.body.style ||
  215. 'OPerspective' in document.body.style ||
  216. 'perspective' in document.body.style;
  217. features.transforms2d = 'WebkitTransform' in document.body.style ||
  218. 'MozTransform' in document.body.style ||
  219. 'msTransform' in document.body.style ||
  220. 'OTransform' in document.body.style ||
  221. 'transform' in document.body.style;
  222. features.requestAnimationFrameMethod = window.requestAnimationFrame || window.webkitRequestAnimationFrame || window.mozRequestAnimationFrame;
  223. features.requestAnimationFrame = typeof features.requestAnimationFrameMethod === 'function';
  224. features.canvas = !!document.createElement( 'canvas' ).getContext;
  225. features.touch = !!( 'ontouchstart' in window );
  226. // Transitions in the overview are disabled in desktop and
  227. // mobile Safari due to lag
  228. features.overviewTransitions = !/Version\/[\d\.]+.*Safari/.test( navigator.userAgent );
  229. isMobileDevice = /(iphone|ipod|ipad|android)/gi.test( navigator.userAgent );
  230. }
  231. /**
  232. * Loads the dependencies of reveal.js. Dependencies are
  233. * defined via the configuration option 'dependencies'
  234. * and will be loaded prior to starting/binding reveal.js.
  235. * Some dependencies may have an 'async' flag, if so they
  236. * will load after reveal.js has been started up.
  237. */
  238. function load() {
  239. var scripts = [],
  240. scriptsAsync = [],
  241. scriptsToPreload = 0;
  242. // Called once synchronous scripts finish loading
  243. function proceed() {
  244. if( scriptsAsync.length ) {
  245. // Load asynchronous scripts
  246. head.js.apply( null, scriptsAsync );
  247. }
  248. start();
  249. }
  250. function loadScript( s ) {
  251. head.ready( s.src.match( /([\w\d_\-]*)\.?js$|[^\\\/]*$/i )[0], function() {
  252. // Extension may contain callback functions
  253. if( typeof s.callback === 'function' ) {
  254. s.callback.apply( this );
  255. }
  256. if( --scriptsToPreload === 0 ) {
  257. proceed();
  258. }
  259. });
  260. }
  261. for( var i = 0, len = config.dependencies.length; i < len; i++ ) {
  262. var s = config.dependencies[i];
  263. // Load if there's no condition or the condition is truthy
  264. if( !s.condition || s.condition() ) {
  265. if( s.async ) {
  266. scriptsAsync.push( s.src );
  267. }
  268. else {
  269. scripts.push( s.src );
  270. }
  271. loadScript( s );
  272. }
  273. }
  274. if( scripts.length ) {
  275. scriptsToPreload = scripts.length;
  276. // Load synchronous scripts
  277. head.js.apply( null, scripts );
  278. }
  279. else {
  280. proceed();
  281. }
  282. }
  283. /**
  284. * Starts up reveal.js by binding input events and navigating
  285. * to the current URL deeplink if there is one.
  286. */
  287. function start() {
  288. // Make sure we've got all the DOM elements we need
  289. setupDOM();
  290. // Listen to messages posted to this window
  291. setupPostMessage();
  292. // Resets all vertical slides so that only the first is visible
  293. resetVerticalSlides();
  294. // Updates the presentation to match the current configuration values
  295. configure();
  296. // Read the initial hash
  297. readURL();
  298. // Update all backgrounds
  299. updateBackground( true );
  300. // Notify listeners that the presentation is ready but use a 1ms
  301. // timeout to ensure it's not fired synchronously after #initialize()
  302. setTimeout( function() {
  303. // Enable transitions now that we're loaded
  304. dom.slides.classList.remove( 'no-transition' );
  305. loaded = true;
  306. dispatchEvent( 'ready', {
  307. 'indexh': indexh,
  308. 'indexv': indexv,
  309. 'currentSlide': currentSlide
  310. } );
  311. }, 1 );
  312. // Special setup and config is required when printing to PDF
  313. if( isPrintingPDF() ) {
  314. removeEventListeners();
  315. // The document needs to have loaded for the PDF layout
  316. // measurements to be accurate
  317. if( document.readyState === 'complete' ) {
  318. setupPDF();
  319. }
  320. else {
  321. window.addEventListener( 'load', setupPDF );
  322. }
  323. }
  324. }
  325. /**
  326. * Finds and stores references to DOM elements which are
  327. * required by the presentation. If a required element is
  328. * not found, it is created.
  329. */
  330. function setupDOM() {
  331. // Prevent transitions while we're loading
  332. dom.slides.classList.add( 'no-transition' );
  333. // Background element
  334. dom.background = createSingletonNode( dom.wrapper, 'div', 'backgrounds', null );
  335. // Progress bar
  336. dom.progress = createSingletonNode( dom.wrapper, 'div', 'progress', '<span></span>' );
  337. dom.progressbar = dom.progress.querySelector( 'span' );
  338. // Arrow controls
  339. createSingletonNode( dom.wrapper, 'aside', 'controls',
  340. '<div class="navigate-left"></div>' +
  341. '<div class="navigate-right"></div>' +
  342. '<div class="navigate-up"></div>' +
  343. '<div class="navigate-down"></div>' );
  344. // Slide number
  345. dom.slideNumber = createSingletonNode( dom.wrapper, 'div', 'slide-number', '' );
  346. // Overlay graphic which is displayed during the paused mode
  347. createSingletonNode( dom.wrapper, 'div', 'pause-overlay', null );
  348. // Cache references to elements
  349. dom.controls = document.querySelector( '.reveal .controls' );
  350. dom.theme = document.querySelector( '#theme' );
  351. dom.wrapper.setAttribute( 'role', 'application' );
  352. // There can be multiple instances of controls throughout the page
  353. dom.controlsLeft = toArray( document.querySelectorAll( '.navigate-left' ) );
  354. dom.controlsRight = toArray( document.querySelectorAll( '.navigate-right' ) );
  355. dom.controlsUp = toArray( document.querySelectorAll( '.navigate-up' ) );
  356. dom.controlsDown = toArray( document.querySelectorAll( '.navigate-down' ) );
  357. dom.controlsPrev = toArray( document.querySelectorAll( '.navigate-prev' ) );
  358. dom.controlsNext = toArray( document.querySelectorAll( '.navigate-next' ) );
  359. dom.statusDiv = createStatusDiv();
  360. }
  361. /**
  362. * Creates a hidden div with role aria-live to announce the
  363. * current slide content. Hide the div off-screen to make it
  364. * available only to Assistive Technologies.
  365. */
  366. function createStatusDiv() {
  367. var statusDiv = document.getElementById( 'aria-status-div' );
  368. if( !statusDiv ) {
  369. statusDiv = document.createElement( 'div' );
  370. statusDiv.style.position = 'absolute';
  371. statusDiv.style.height = '1px';
  372. statusDiv.style.width = '1px';
  373. statusDiv.style.overflow ='hidden';
  374. statusDiv.style.clip = 'rect( 1px, 1px, 1px, 1px )';
  375. statusDiv.setAttribute( 'id', 'aria-status-div' );
  376. statusDiv.setAttribute( 'aria-live', 'polite' );
  377. statusDiv.setAttribute( 'aria-atomic','true' );
  378. dom.wrapper.appendChild( statusDiv );
  379. }
  380. return statusDiv;
  381. }
  382. /**
  383. * Configures the presentation for printing to a static
  384. * PDF.
  385. */
  386. function setupPDF() {
  387. var slideSize = getComputedSlideSize( window.innerWidth, window.innerHeight );
  388. // Dimensions of the PDF pages
  389. var pageWidth = Math.floor( slideSize.width * ( 1 + config.margin ) ),
  390. pageHeight = Math.floor( slideSize.height * ( 1 + config.margin ) );
  391. // Dimensions of slides within the pages
  392. var slideWidth = slideSize.width,
  393. slideHeight = slideSize.height;
  394. // Let the browser know what page size we want to print
  395. injectStyleSheet( '@page{size:'+ pageWidth +'px '+ pageHeight +'px; margin: 0;}' );
  396. // Limit the size of certain elements to the dimensions of the slide
  397. injectStyleSheet( '.reveal section>img, .reveal section>video, .reveal section>iframe{max-width: '+ slideWidth +'px; max-height:'+ slideHeight +'px}' );
  398. document.body.classList.add( 'print-pdf' );
  399. document.body.style.width = pageWidth + 'px';
  400. document.body.style.height = pageHeight + 'px';
  401. // Slide and slide background layout
  402. toArray( dom.wrapper.querySelectorAll( SLIDES_SELECTOR ) ).forEach( function( slide ) {
  403. // Vertical stacks are not centred since their section
  404. // children will be
  405. if( slide.classList.contains( 'stack' ) === false ) {
  406. // Center the slide inside of the page, giving the slide some margin
  407. var left = ( pageWidth - slideWidth ) / 2,
  408. top = ( pageHeight - slideHeight ) / 2;
  409. var contentHeight = getAbsoluteHeight( slide );
  410. var numberOfPages = Math.max( Math.ceil( contentHeight / pageHeight ), 1 );
  411. // Center slides vertically
  412. if( numberOfPages === 1 && config.center || slide.classList.contains( 'center' ) ) {
  413. top = Math.max( ( pageHeight - contentHeight ) / 2, 0 );
  414. }
  415. // Position the slide inside of the page
  416. slide.style.left = left + 'px';
  417. slide.style.top = top + 'px';
  418. slide.style.width = slideWidth + 'px';
  419. // TODO Backgrounds need to be multiplied when the slide
  420. // stretches over multiple pages
  421. var background = slide.querySelector( '.slide-background' );
  422. if( background ) {
  423. background.style.width = pageWidth + 'px';
  424. background.style.height = ( pageHeight * numberOfPages ) + 'px';
  425. background.style.top = -top + 'px';
  426. background.style.left = -left + 'px';
  427. }
  428. }
  429. } );
  430. // Show all fragments
  431. toArray( dom.wrapper.querySelectorAll( SLIDES_SELECTOR + ' .fragment' ) ).forEach( function( fragment ) {
  432. fragment.classList.add( 'visible' );
  433. } );
  434. }
  435. /**
  436. * Creates an HTML element and returns a reference to it.
  437. * If the element already exists the existing instance will
  438. * be returned.
  439. */
  440. function createSingletonNode( container, tagname, classname, innerHTML ) {
  441. // Find all nodes matching the description
  442. var nodes = container.querySelectorAll( '.' + classname );
  443. // Check all matches to find one which is a direct child of
  444. // the specified container
  445. for( var i = 0; i < nodes.length; i++ ) {
  446. var testNode = nodes[i];
  447. if( testNode.parentNode === container ) {
  448. return testNode;
  449. }
  450. }
  451. // If no node was found, create it now
  452. var node = document.createElement( tagname );
  453. node.classList.add( classname );
  454. if( typeof innerHTML === 'string' ) {
  455. node.innerHTML = innerHTML;
  456. }
  457. container.appendChild( node );
  458. return node;
  459. }
  460. /**
  461. * Creates the slide background elements and appends them
  462. * to the background container. One element is created per
  463. * slide no matter if the given slide has visible background.
  464. */
  465. function createBackgrounds() {
  466. var printMode = isPrintingPDF();
  467. // Clear prior backgrounds
  468. dom.background.innerHTML = '';
  469. dom.background.classList.add( 'no-transition' );
  470. // Iterate over all horizontal slides
  471. toArray( dom.wrapper.querySelectorAll( HORIZONTAL_SLIDES_SELECTOR ) ).forEach( function( slideh ) {
  472. var backgroundStack;
  473. if( printMode ) {
  474. backgroundStack = createBackground( slideh, slideh );
  475. }
  476. else {
  477. backgroundStack = createBackground( slideh, dom.background );
  478. }
  479. // Iterate over all vertical slides
  480. toArray( slideh.querySelectorAll( 'section' ) ).forEach( function( slidev ) {
  481. if( printMode ) {
  482. createBackground( slidev, slidev );
  483. }
  484. else {
  485. createBackground( slidev, backgroundStack );
  486. }
  487. backgroundStack.classList.add( 'stack' );
  488. } );
  489. } );
  490. // Add parallax background if specified
  491. if( config.parallaxBackgroundImage ) {
  492. dom.background.style.backgroundImage = 'url("' + config.parallaxBackgroundImage + '")';
  493. dom.background.style.backgroundSize = config.parallaxBackgroundSize;
  494. // Make sure the below properties are set on the element - these properties are
  495. // needed for proper transitions to be set on the element via CSS. To remove
  496. // annoying background slide-in effect when the presentation starts, apply
  497. // these properties after short time delay
  498. setTimeout( function() {
  499. dom.wrapper.classList.add( 'has-parallax-background' );
  500. }, 1 );
  501. }
  502. else {
  503. dom.background.style.backgroundImage = '';
  504. dom.wrapper.classList.remove( 'has-parallax-background' );
  505. }
  506. }
  507. /**
  508. * Creates a background for the given slide.
  509. *
  510. * @param {HTMLElement} slide
  511. * @param {HTMLElement} container The element that the background
  512. * should be appended to
  513. */
  514. function createBackground( slide, container ) {
  515. var data = {
  516. background: slide.getAttribute( 'data-background' ),
  517. backgroundSize: slide.getAttribute( 'data-background-size' ),
  518. backgroundImage: slide.getAttribute( 'data-background-image' ),
  519. backgroundVideo: slide.getAttribute( 'data-background-video' ),
  520. backgroundIframe: slide.getAttribute( 'data-background-iframe' ),
  521. backgroundColor: slide.getAttribute( 'data-background-color' ),
  522. backgroundRepeat: slide.getAttribute( 'data-background-repeat' ),
  523. backgroundPosition: slide.getAttribute( 'data-background-position' ),
  524. backgroundTransition: slide.getAttribute( 'data-background-transition' )
  525. };
  526. var element = document.createElement( 'div' );
  527. // Carry over custom classes from the slide to the background
  528. element.className = 'slide-background ' + slide.className.replace( /present|past|future/, '' );
  529. if( data.background ) {
  530. // Auto-wrap image urls in url(...)
  531. if( /^(http|file|\/\/)/gi.test( data.background ) || /\.(svg|png|jpg|jpeg|gif|bmp)$/gi.test( data.background ) ) {
  532. slide.setAttribute( 'data-background-image', data.background );
  533. }
  534. else {
  535. element.style.background = data.background;
  536. }
  537. }
  538. // Create a hash for this combination of background settings.
  539. // This is used to determine when two slide backgrounds are
  540. // the same.
  541. if( data.background || data.backgroundColor || data.backgroundImage || data.backgroundVideo || data.backgroundIframe ) {
  542. element.setAttribute( 'data-background-hash', data.background +
  543. data.backgroundSize +
  544. data.backgroundImage +
  545. data.backgroundVideo +
  546. data.backgroundIframe +
  547. data.backgroundColor +
  548. data.backgroundRepeat +
  549. data.backgroundPosition +
  550. data.backgroundTransition );
  551. }
  552. // Additional and optional background properties
  553. if( data.backgroundSize ) element.style.backgroundSize = data.backgroundSize;
  554. if( data.backgroundColor ) element.style.backgroundColor = data.backgroundColor;
  555. if( data.backgroundRepeat ) element.style.backgroundRepeat = data.backgroundRepeat;
  556. if( data.backgroundPosition ) element.style.backgroundPosition = data.backgroundPosition;
  557. if( data.backgroundTransition ) element.setAttribute( 'data-background-transition', data.backgroundTransition );
  558. container.appendChild( element );
  559. // If backgrounds are being recreated, clear old classes
  560. slide.classList.remove( 'has-dark-background' );
  561. slide.classList.remove( 'has-light-background' );
  562. // If this slide has a background color, add a class that
  563. // signals if it is light or dark. If the slide has no background
  564. // color, no class will be set
  565. var computedBackgroundColor = window.getComputedStyle( element ).backgroundColor;
  566. if( computedBackgroundColor ) {
  567. var rgb = colorToRgb( computedBackgroundColor );
  568. // Ignore fully transparent backgrounds. Some browsers return
  569. // rgba(0,0,0,0) when reading the computed background color of
  570. // an element with no background
  571. if( rgb && rgb.a !== 0 ) {
  572. if( colorBrightness( computedBackgroundColor ) < 128 ) {
  573. slide.classList.add( 'has-dark-background' );
  574. }
  575. else {
  576. slide.classList.add( 'has-light-background' );
  577. }
  578. }
  579. }
  580. return element;
  581. }
  582. /**
  583. * Registers a listener to postMessage events, this makes it
  584. * possible to call all reveal.js API methods from another
  585. * window. For example:
  586. *
  587. * revealWindow.postMessage( JSON.stringify({
  588. * method: 'slide',
  589. * args: [ 2 ]
  590. * }), '*' );
  591. */
  592. function setupPostMessage() {
  593. if( config.postMessage ) {
  594. window.addEventListener( 'message', function ( event ) {
  595. var data = event.data;
  596. // Make sure we're dealing with JSON
  597. if( data.charAt( 0 ) === '{' && data.charAt( data.length - 1 ) === '}' ) {
  598. data = JSON.parse( data );
  599. // Check if the requested method can be found
  600. if( data.method && typeof Reveal[data.method] === 'function' ) {
  601. Reveal[data.method].apply( Reveal, data.args );
  602. }
  603. }
  604. }, false );
  605. }
  606. }
  607. /**
  608. * Applies the configuration settings from the config
  609. * object. May be called multiple times.
  610. */
  611. function configure( options ) {
  612. var numberOfSlides = dom.wrapper.querySelectorAll( SLIDES_SELECTOR ).length;
  613. dom.wrapper.classList.remove( config.transition );
  614. // New config options may be passed when this method
  615. // is invoked through the API after initialization
  616. if( typeof options === 'object' ) extend( config, options );
  617. // Force linear transition based on browser capabilities
  618. if( features.transforms3d === false ) config.transition = 'linear';
  619. dom.wrapper.classList.add( config.transition );
  620. dom.wrapper.setAttribute( 'data-transition-speed', config.transitionSpeed );
  621. dom.wrapper.setAttribute( 'data-background-transition', config.backgroundTransition );
  622. dom.controls.style.display = config.controls ? 'block' : 'none';
  623. dom.progress.style.display = config.progress ? 'block' : 'none';
  624. if( config.rtl ) {
  625. dom.wrapper.classList.add( 'rtl' );
  626. }
  627. else {
  628. dom.wrapper.classList.remove( 'rtl' );
  629. }
  630. if( config.center ) {
  631. dom.wrapper.classList.add( 'center' );
  632. }
  633. else {
  634. dom.wrapper.classList.remove( 'center' );
  635. }
  636. // Exit the paused mode if it was configured off
  637. if( config.pause === false ) {
  638. resume();
  639. }
  640. if( config.mouseWheel ) {
  641. document.addEventListener( 'DOMMouseScroll', onDocumentMouseScroll, false ); // FF
  642. document.addEventListener( 'mousewheel', onDocumentMouseScroll, false );
  643. }
  644. else {
  645. document.removeEventListener( 'DOMMouseScroll', onDocumentMouseScroll, false ); // FF
  646. document.removeEventListener( 'mousewheel', onDocumentMouseScroll, false );
  647. }
  648. // Rolling 3D links
  649. if( config.rollingLinks ) {
  650. enableRollingLinks();
  651. }
  652. else {
  653. disableRollingLinks();
  654. }
  655. // Iframe link previews
  656. if( config.previewLinks ) {
  657. enablePreviewLinks();
  658. }
  659. else {
  660. disablePreviewLinks();
  661. enablePreviewLinks( '[data-preview-link]' );
  662. }
  663. // Remove existing auto-slide controls
  664. if( autoSlidePlayer ) {
  665. autoSlidePlayer.destroy();
  666. autoSlidePlayer = null;
  667. }
  668. // Generate auto-slide controls if needed
  669. if( numberOfSlides > 1 && config.autoSlide && config.autoSlideStoppable && features.canvas && features.requestAnimationFrame ) {
  670. autoSlidePlayer = new Playback( dom.wrapper, function() {
  671. return Math.min( Math.max( ( Date.now() - autoSlideStartTime ) / autoSlide, 0 ), 1 );
  672. } );
  673. autoSlidePlayer.on( 'click', onAutoSlidePlayerClick );
  674. autoSlidePaused = false;
  675. }
  676. // When fragments are turned off they should be visible
  677. if( config.fragments === false ) {
  678. toArray( dom.slides.querySelectorAll( '.fragment' ) ).forEach( function( element ) {
  679. element.classList.add( 'visible' );
  680. element.classList.remove( 'current-fragment' );
  681. } );
  682. }
  683. sync();
  684. }
  685. /**
  686. * Binds all event listeners.
  687. */
  688. function addEventListeners() {
  689. eventsAreBound = true;
  690. window.addEventListener( 'hashchange', onWindowHashChange, false );
  691. window.addEventListener( 'resize', onWindowResize, false );
  692. if( config.touch ) {
  693. dom.wrapper.addEventListener( 'touchstart', onTouchStart, false );
  694. dom.wrapper.addEventListener( 'touchmove', onTouchMove, false );
  695. dom.wrapper.addEventListener( 'touchend', onTouchEnd, false );
  696. // Support pointer-style touch interaction as well
  697. if( window.navigator.pointerEnabled ) {
  698. // IE 11 uses un-prefixed version of pointer events
  699. dom.wrapper.addEventListener( 'pointerdown', onPointerDown, false );
  700. dom.wrapper.addEventListener( 'pointermove', onPointerMove, false );
  701. dom.wrapper.addEventListener( 'pointerup', onPointerUp, false );
  702. }
  703. else if( window.navigator.msPointerEnabled ) {
  704. // IE 10 uses prefixed version of pointer events
  705. dom.wrapper.addEventListener( 'MSPointerDown', onPointerDown, false );
  706. dom.wrapper.addEventListener( 'MSPointerMove', onPointerMove, false );
  707. dom.wrapper.addEventListener( 'MSPointerUp', onPointerUp, false );
  708. }
  709. }
  710. if( config.keyboard ) {
  711. document.addEventListener( 'keydown', onDocumentKeyDown, false );
  712. document.addEventListener( 'keypress', onDocumentKeyPress, false );
  713. }
  714. if( config.progress && dom.progress ) {
  715. dom.progress.addEventListener( 'click', onProgressClicked, false );
  716. }
  717. if( config.focusBodyOnPageVisibilityChange ) {
  718. var visibilityChange;
  719. if( 'hidden' in document ) {
  720. visibilityChange = 'visibilitychange';
  721. }
  722. else if( 'msHidden' in document ) {
  723. visibilityChange = 'msvisibilitychange';
  724. }
  725. else if( 'webkitHidden' in document ) {
  726. visibilityChange = 'webkitvisibilitychange';
  727. }
  728. if( visibilityChange ) {
  729. document.addEventListener( visibilityChange, onPageVisibilityChange, false );
  730. }
  731. }
  732. // Listen to both touch and click events, in case the device
  733. // supports both
  734. var pointerEvents = [ 'touchstart', 'click' ];
  735. // Only support touch for Android, fixes double navigations in
  736. // stock browser
  737. if( navigator.userAgent.match( /android/gi ) ) {
  738. pointerEvents = [ 'touchstart' ];
  739. }
  740. pointerEvents.forEach( function( eventName ) {
  741. dom.controlsLeft.forEach( function( el ) { el.addEventListener( eventName, onNavigateLeftClicked, false ); } );
  742. dom.controlsRight.forEach( function( el ) { el.addEventListener( eventName, onNavigateRightClicked, false ); } );
  743. dom.controlsUp.forEach( function( el ) { el.addEventListener( eventName, onNavigateUpClicked, false ); } );
  744. dom.controlsDown.forEach( function( el ) { el.addEventListener( eventName, onNavigateDownClicked, false ); } );
  745. dom.controlsPrev.forEach( function( el ) { el.addEventListener( eventName, onNavigatePrevClicked, false ); } );
  746. dom.controlsNext.forEach( function( el ) { el.addEventListener( eventName, onNavigateNextClicked, false ); } );
  747. } );
  748. }
  749. /**
  750. * Unbinds all event listeners.
  751. */
  752. function removeEventListeners() {
  753. eventsAreBound = false;
  754. document.removeEventListener( 'keydown', onDocumentKeyDown, false );
  755. document.removeEventListener( 'keypress', onDocumentKeyPress, false );
  756. window.removeEventListener( 'hashchange', onWindowHashChange, false );
  757. window.removeEventListener( 'resize', onWindowResize, false );
  758. dom.wrapper.removeEventListener( 'touchstart', onTouchStart, false );
  759. dom.wrapper.removeEventListener( 'touchmove', onTouchMove, false );
  760. dom.wrapper.removeEventListener( 'touchend', onTouchEnd, false );
  761. // IE11
  762. if( window.navigator.pointerEnabled ) {
  763. dom.wrapper.removeEventListener( 'pointerdown', onPointerDown, false );
  764. dom.wrapper.removeEventListener( 'pointermove', onPointerMove, false );
  765. dom.wrapper.removeEventListener( 'pointerup', onPointerUp, false );
  766. }
  767. // IE10
  768. else if( window.navigator.msPointerEnabled ) {
  769. dom.wrapper.removeEventListener( 'MSPointerDown', onPointerDown, false );
  770. dom.wrapper.removeEventListener( 'MSPointerMove', onPointerMove, false );
  771. dom.wrapper.removeEventListener( 'MSPointerUp', onPointerUp, false );
  772. }
  773. if ( config.progress && dom.progress ) {
  774. dom.progress.removeEventListener( 'click', onProgressClicked, false );
  775. }
  776. [ 'touchstart', 'click' ].forEach( function( eventName ) {
  777. dom.controlsLeft.forEach( function( el ) { el.removeEventListener( eventName, onNavigateLeftClicked, false ); } );
  778. dom.controlsRight.forEach( function( el ) { el.removeEventListener( eventName, onNavigateRightClicked, false ); } );
  779. dom.controlsUp.forEach( function( el ) { el.removeEventListener( eventName, onNavigateUpClicked, false ); } );
  780. dom.controlsDown.forEach( function( el ) { el.removeEventListener( eventName, onNavigateDownClicked, false ); } );
  781. dom.controlsPrev.forEach( function( el ) { el.removeEventListener( eventName, onNavigatePrevClicked, false ); } );
  782. dom.controlsNext.forEach( function( el ) { el.removeEventListener( eventName, onNavigateNextClicked, false ); } );
  783. } );
  784. }
  785. /**
  786. * Extend object a with the properties of object b.
  787. * If there's a conflict, object b takes precedence.
  788. */
  789. function extend( a, b ) {
  790. for( var i in b ) {
  791. a[ i ] = b[ i ];
  792. }
  793. }
  794. /**
  795. * Converts the target object to an array.
  796. */
  797. function toArray( o ) {
  798. return Array.prototype.slice.call( o );
  799. }
  800. /**
  801. * Utility for deserializing a value.
  802. */
  803. function deserialize( value ) {
  804. if( typeof value === 'string' ) {
  805. if( value === 'null' ) return null;
  806. else if( value === 'true' ) return true;
  807. else if( value === 'false' ) return false;
  808. else if( value.match( /^\d+$/ ) ) return parseFloat( value );
  809. }
  810. return value;
  811. }
  812. /**
  813. * Measures the distance in pixels between point a
  814. * and point b.
  815. *
  816. * @param {Object} a point with x/y properties
  817. * @param {Object} b point with x/y properties
  818. */
  819. function distanceBetween( a, b ) {
  820. var dx = a.x - b.x,
  821. dy = a.y - b.y;
  822. return Math.sqrt( dx*dx + dy*dy );
  823. }
  824. /**
  825. * Applies a CSS transform to the target element.
  826. */
  827. function transformElement( element, transform ) {
  828. element.style.WebkitTransform = transform;
  829. element.style.MozTransform = transform;
  830. element.style.msTransform = transform;
  831. element.style.transform = transform;
  832. }
  833. /**
  834. * Applies CSS transforms to the slides container. The container
  835. * is transformed from two separate sources: layout and the overview
  836. * mode.
  837. */
  838. function transformSlides( transforms ) {
  839. // Pick up new transforms from arguments
  840. if( typeof transforms.layout === 'string' ) slidesTransform.layout = transforms.layout;
  841. if( typeof transforms.overview === 'string' ) slidesTransform.overview = transforms.overview;
  842. // Apply the transforms to the slides container
  843. if( slidesTransform.layout ) {
  844. transformElement( dom.slides, slidesTransform.layout + ' ' + slidesTransform.overview );
  845. }
  846. else {
  847. transformElement( dom.slides, slidesTransform.overview );
  848. }
  849. }
  850. /**
  851. * Injects the given CSS styles into the DOM.
  852. */
  853. function injectStyleSheet( value ) {
  854. var tag = document.createElement( 'style' );
  855. tag.type = 'text/css';
  856. if( tag.styleSheet ) {
  857. tag.styleSheet.cssText = value;
  858. }
  859. else {
  860. tag.appendChild( document.createTextNode( value ) );
  861. }
  862. document.getElementsByTagName( 'head' )[0].appendChild( tag );
  863. }
  864. /**
  865. * Measures the distance in pixels between point a and point b.
  866. *
  867. * @param {String} color The string representation of a color,
  868. * the following formats are supported:
  869. * - #000
  870. * - #000000
  871. * - rgb(0,0,0)
  872. */
  873. function colorToRgb( color ) {
  874. var hex3 = color.match( /^#([0-9a-f]{3})$/i );
  875. if( hex3 && hex3[1] ) {
  876. hex3 = hex3[1];
  877. return {
  878. r: parseInt( hex3.charAt( 0 ), 16 ) * 0x11,
  879. g: parseInt( hex3.charAt( 1 ), 16 ) * 0x11,
  880. b: parseInt( hex3.charAt( 2 ), 16 ) * 0x11
  881. };
  882. }
  883. var hex6 = color.match( /^#([0-9a-f]{6})$/i );
  884. if( hex6 && hex6[1] ) {
  885. hex6 = hex6[1];
  886. return {
  887. r: parseInt( hex6.substr( 0, 2 ), 16 ),
  888. g: parseInt( hex6.substr( 2, 2 ), 16 ),
  889. b: parseInt( hex6.substr( 4, 2 ), 16 )
  890. };
  891. }
  892. var rgb = color.match( /^rgb\s*\(\s*(\d+)\s*,\s*(\d+)\s*,\s*(\d+)\s*\)$/i );
  893. if( rgb ) {
  894. return {
  895. r: parseInt( rgb[1], 10 ),
  896. g: parseInt( rgb[2], 10 ),
  897. b: parseInt( rgb[3], 10 )
  898. };
  899. }
  900. var rgba = color.match( /^rgba\s*\(\s*(\d+)\s*,\s*(\d+)\s*,\s*(\d+)\s*\,\s*([\d]+|[\d]*.[\d]+)\s*\)$/i );
  901. if( rgba ) {
  902. return {
  903. r: parseInt( rgba[1], 10 ),
  904. g: parseInt( rgba[2], 10 ),
  905. b: parseInt( rgba[3], 10 ),
  906. a: parseFloat( rgba[4] )
  907. };
  908. }
  909. return null;
  910. }
  911. /**
  912. * Calculates brightness on a scale of 0-255.
  913. *
  914. * @param color See colorStringToRgb for supported formats.
  915. */
  916. function colorBrightness( color ) {
  917. if( typeof color === 'string' ) color = colorToRgb( color );
  918. if( color ) {
  919. return ( color.r * 299 + color.g * 587 + color.b * 114 ) / 1000;
  920. }
  921. return null;
  922. }
  923. /**
  924. * Retrieves the height of the given element by looking
  925. * at the position and height of its immediate children.
  926. */
  927. function getAbsoluteHeight( element ) {
  928. var height = 0;
  929. if( element ) {
  930. var absoluteChildren = 0;
  931. toArray( element.childNodes ).forEach( function( child ) {
  932. if( typeof child.offsetTop === 'number' && child.style ) {
  933. // Count # of abs children
  934. if( window.getComputedStyle( child ).position === 'absolute' ) {
  935. absoluteChildren += 1;
  936. }
  937. height = Math.max( height, child.offsetTop + child.offsetHeight );
  938. }
  939. } );
  940. // If there are no absolute children, use offsetHeight
  941. if( absoluteChildren === 0 ) {
  942. height = element.offsetHeight;
  943. }
  944. }
  945. return height;
  946. }
  947. /**
  948. * Returns the remaining height within the parent of the
  949. * target element.
  950. *
  951. * remaining height = [ configured parent height ] - [ current parent height ]
  952. */
  953. function getRemainingHeight( element, height ) {
  954. height = height || 0;
  955. if( element ) {
  956. var newHeight, oldHeight = element.style.height;
  957. // Change the .stretch element height to 0 in order find the height of all
  958. // the other elements
  959. element.style.height = '0px';
  960. newHeight = height - element.parentNode.offsetHeight;
  961. // Restore the old height, just in case
  962. element.style.height = oldHeight + 'px';
  963. return newHeight;
  964. }
  965. return height;
  966. }
  967. /**
  968. * Checks if this instance is being used to print a PDF.
  969. */
  970. function isPrintingPDF() {
  971. return ( /print-pdf/gi ).test( window.location.search );
  972. }
  973. /**
  974. * Hides the address bar if we're on a mobile device.
  975. */
  976. function hideAddressBar() {
  977. if( config.hideAddressBar && isMobileDevice ) {
  978. // Events that should trigger the address bar to hide
  979. window.addEventListener( 'load', removeAddressBar, false );
  980. window.addEventListener( 'orientationchange', removeAddressBar, false );
  981. }
  982. }
  983. /**
  984. * Causes the address bar to hide on mobile devices,
  985. * more vertical space ftw.
  986. */
  987. function removeAddressBar() {
  988. setTimeout( function() {
  989. window.scrollTo( 0, 1 );
  990. }, 10 );
  991. }
  992. /**
  993. * Dispatches an event of the specified type from the
  994. * reveal DOM element.
  995. */
  996. function dispatchEvent( type, args ) {
  997. var event = document.createEvent( 'HTMLEvents', 1, 2 );
  998. event.initEvent( type, true, true );
  999. extend( event, args );
  1000. dom.wrapper.dispatchEvent( event );
  1001. // If we're in an iframe, post each reveal.js event to the
  1002. // parent window. Used by the notes plugin
  1003. if( config.postMessageEvents && window.parent !== window.self ) {
  1004. window.parent.postMessage( JSON.stringify({ namespace: 'reveal', eventName: type, state: getState() }), '*' );
  1005. }
  1006. }
  1007. /**
  1008. * Wrap all links in 3D goodness.
  1009. */
  1010. function enableRollingLinks() {
  1011. if( features.transforms3d && !( 'msPerspective' in document.body.style ) ) {
  1012. var anchors = dom.wrapper.querySelectorAll( SLIDES_SELECTOR + ' a' );
  1013. for( var i = 0, len = anchors.length; i < len; i++ ) {
  1014. var anchor = anchors[i];
  1015. if( anchor.textContent && !anchor.querySelector( '*' ) && ( !anchor.className || !anchor.classList.contains( anchor, 'roll' ) ) ) {
  1016. var span = document.createElement('span');
  1017. span.setAttribute('data-title', anchor.text);
  1018. span.innerHTML = anchor.innerHTML;
  1019. anchor.classList.add( 'roll' );
  1020. anchor.innerHTML = '';
  1021. anchor.appendChild(span);
  1022. }
  1023. }
  1024. }
  1025. }
  1026. /**
  1027. * Unwrap all 3D links.
  1028. */
  1029. function disableRollingLinks() {
  1030. var anchors = dom.wrapper.querySelectorAll( SLIDES_SELECTOR + ' a.roll' );
  1031. for( var i = 0, len = anchors.length; i < len; i++ ) {
  1032. var anchor = anchors[i];
  1033. var span = anchor.querySelector( 'span' );
  1034. if( span ) {
  1035. anchor.classList.remove( 'roll' );
  1036. anchor.innerHTML = span.innerHTML;
  1037. }
  1038. }
  1039. }
  1040. /**
  1041. * Bind preview frame links.
  1042. */
  1043. function enablePreviewLinks( selector ) {
  1044. var anchors = toArray( document.querySelectorAll( selector ? selector : 'a' ) );
  1045. anchors.forEach( function( element ) {
  1046. if( /^(http|www)/gi.test( element.getAttribute( 'href' ) ) ) {
  1047. element.addEventListener( 'click', onPreviewLinkClicked, false );
  1048. }
  1049. } );
  1050. }
  1051. /**
  1052. * Unbind preview frame links.
  1053. */
  1054. function disablePreviewLinks() {
  1055. var anchors = toArray( document.querySelectorAll( 'a' ) );
  1056. anchors.forEach( function( element ) {
  1057. if( /^(http|www)/gi.test( element.getAttribute( 'href' ) ) ) {
  1058. element.removeEventListener( 'click', onPreviewLinkClicked, false );
  1059. }
  1060. } );
  1061. }
  1062. /**
  1063. * Opens a preview window for the target URL.
  1064. */
  1065. function showPreview( url ) {
  1066. closeOverlay();
  1067. dom.overlay = document.createElement( 'div' );
  1068. dom.overlay.classList.add( 'overlay' );
  1069. dom.overlay.classList.add( 'overlay-preview' );
  1070. dom.wrapper.appendChild( dom.overlay );
  1071. dom.overlay.innerHTML = [
  1072. '<header>',
  1073. '<a class="close" href="#"><span class="icon"></span></a>',
  1074. '<a class="external" href="'+ url +'" target="_blank"><span class="icon"></span></a>',
  1075. '</header>',
  1076. '<div class="spinner"></div>',
  1077. '<div class="viewport">',
  1078. '<iframe src="'+ url +'"></iframe>',
  1079. '</div>'
  1080. ].join('');
  1081. dom.overlay.querySelector( 'iframe' ).addEventListener( 'load', function( event ) {
  1082. dom.overlay.classList.add( 'loaded' );
  1083. }, false );
  1084. dom.overlay.querySelector( '.close' ).addEventListener( 'click', function( event ) {
  1085. closeOverlay();
  1086. event.preventDefault();
  1087. }, false );
  1088. dom.overlay.querySelector( '.external' ).addEventListener( 'click', function( event ) {
  1089. closeOverlay();
  1090. }, false );
  1091. setTimeout( function() {
  1092. dom.overlay.classList.add( 'visible' );
  1093. }, 1 );
  1094. }
  1095. /**
  1096. * Opens a overlay window with help material.
  1097. */
  1098. function showHelp() {
  1099. if( config.help ) {
  1100. closeOverlay();
  1101. dom.overlay = document.createElement( 'div' );
  1102. dom.overlay.classList.add( 'overlay' );
  1103. dom.overlay.classList.add( 'overlay-help' );
  1104. dom.wrapper.appendChild( dom.overlay );
  1105. var html = '<p class="title">Keyboard Shortcuts</p><br/>';
  1106. html += '<table><th>KEY</th><th>ACTION</th>';
  1107. for( var key in keyboardShortcuts ) {
  1108. html += '<tr><td>' + key + '</td><td>' + keyboardShortcuts[ key ] + '</td></tr>';
  1109. }
  1110. html += '</table>';
  1111. dom.overlay.innerHTML = [
  1112. '<header>',
  1113. '<a class="close" href="#"><span class="icon"></span></a>',
  1114. '</header>',
  1115. '<div class="viewport">',
  1116. '<div class="viewport-inner">'+ html +'</div>',
  1117. '</div>'
  1118. ].join('');
  1119. dom.overlay.querySelector( '.close' ).addEventListener( 'click', function( event ) {
  1120. closeOverlay();
  1121. event.preventDefault();
  1122. }, false );
  1123. setTimeout( function() {
  1124. dom.overlay.classList.add( 'visible' );
  1125. }, 1 );
  1126. }
  1127. }
  1128. /**
  1129. * Closes any currently open overlay.
  1130. */
  1131. function closeOverlay() {
  1132. if( dom.overlay ) {
  1133. dom.overlay.parentNode.removeChild( dom.overlay );
  1134. dom.overlay = null;
  1135. }
  1136. }
  1137. /**
  1138. * Applies JavaScript-controlled layout rules to the
  1139. * presentation.
  1140. */
  1141. function layout() {
  1142. if( dom.wrapper && !isPrintingPDF() ) {
  1143. var size = getComputedSlideSize();
  1144. var slidePadding = 20; // TODO Dig this out of DOM
  1145. // Layout the contents of the slides
  1146. layoutSlideContents( config.width, config.height, slidePadding );
  1147. dom.slides.style.width = size.width + 'px';
  1148. dom.slides.style.height = size.height + 'px';
  1149. // Determine scale of content to fit within available space
  1150. scale = Math.min( size.presentationWidth / size.width, size.presentationHeight / size.height );
  1151. // Respect max/min scale settings
  1152. scale = Math.max( scale, config.minScale );
  1153. scale = Math.min( scale, config.maxScale );
  1154. // Don't apply any scaling styles if scale is 1
  1155. if( scale === 1 ) {
  1156. dom.slides.style.zoom = '';
  1157. dom.slides.style.left = '';
  1158. dom.slides.style.top = '';
  1159. dom.slides.style.bottom = '';
  1160. dom.slides.style.right = '';
  1161. transformSlides( { layout: '' } );
  1162. }
  1163. else {
  1164. // Prefer zooming in desktop Chrome so that content remains crisp
  1165. if( !isMobileDevice && /chrome/i.test( navigator.userAgent ) && typeof dom.slides.style.zoom !== 'undefined' ) {
  1166. dom.slides.style.zoom = scale;
  1167. transformSlides( { layout: '' } );
  1168. }
  1169. // Apply scale transform as a fallback
  1170. else {
  1171. dom.slides.style.left = '50%';
  1172. dom.slides.style.top = '50%';
  1173. dom.slides.style.bottom = 'auto';
  1174. dom.slides.style.right = 'auto';
  1175. transformSlides( { layout: 'translate(-50%, -50%) scale('+ scale +')' } );
  1176. }
  1177. }
  1178. // Select all slides, vertical and horizontal
  1179. var slides = toArray( dom.wrapper.querySelectorAll( SLIDES_SELECTOR ) );
  1180. for( var i = 0, len = slides.length; i < len; i++ ) {
  1181. var slide = slides[ i ];
  1182. // Don't bother updating invisible slides
  1183. if( slide.style.display === 'none' ) {
  1184. continue;
  1185. }
  1186. if( config.center || slide.classList.contains( 'center' ) ) {
  1187. // Vertical stacks are not centred since their section
  1188. // children will be
  1189. if( slide.classList.contains( 'stack' ) ) {
  1190. slide.style.top = 0;
  1191. }
  1192. else {
  1193. slide.style.top = Math.max( ( ( size.height - getAbsoluteHeight( slide ) ) / 2 ) - slidePadding, 0 ) + 'px';
  1194. }
  1195. }
  1196. else {
  1197. slide.style.top = '';
  1198. }
  1199. }
  1200. updateProgress();
  1201. updateParallax();
  1202. }
  1203. }
  1204. /**
  1205. * Applies layout logic to the contents of all slides in
  1206. * the presentation.
  1207. */
  1208. function layoutSlideContents( width, height, padding ) {
  1209. // Handle sizing of elements with the 'stretch' class
  1210. toArray( dom.slides.querySelectorAll( 'section > .stretch' ) ).forEach( function( element ) {
  1211. // Determine how much vertical space we can use
  1212. var remainingHeight = getRemainingHeight( element, height );
  1213. // Consider the aspect ratio of media elements
  1214. if( /(img|video)/gi.test( element.nodeName ) ) {
  1215. var nw = element.naturalWidth || element.videoWidth,
  1216. nh = element.naturalHeight || element.videoHeight;
  1217. var es = Math.min( width / nw, remainingHeight / nh );
  1218. element.style.width = ( nw * es ) + 'px';
  1219. element.style.height = ( nh * es ) + 'px';
  1220. }
  1221. else {
  1222. element.style.width = width + 'px';
  1223. element.style.height = remainingHeight + 'px';
  1224. }
  1225. } );
  1226. }
  1227. /**
  1228. * Calculates the computed pixel size of our slides. These
  1229. * values are based on the width and height configuration
  1230. * options.
  1231. */
  1232. function getComputedSlideSize( presentationWidth, presentationHeight ) {
  1233. var size = {
  1234. // Slide size
  1235. width: config.width,
  1236. height: config.height,
  1237. // Presentation size
  1238. presentationWidth: presentationWidth || dom.wrapper.offsetWidth,
  1239. presentationHeight: presentationHeight || dom.wrapper.offsetHeight
  1240. };
  1241. // Reduce available space by margin
  1242. size.presentationWidth -= ( size.presentationHeight * config.margin );
  1243. size.presentationHeight -= ( size.presentationHeight * config.margin );
  1244. // Slide width may be a percentage of available width
  1245. if( typeof size.width === 'string' && /%$/.test( size.width ) ) {
  1246. size.width = parseInt( size.width, 10 ) / 100 * size.presentationWidth;
  1247. }
  1248. // Slide height may be a percentage of available height
  1249. if( typeof size.height === 'string' && /%$/.test( size.height ) ) {
  1250. size.height = parseInt( size.height, 10 ) / 100 * size.presentationHeight;
  1251. }
  1252. return size;
  1253. }
  1254. /**
  1255. * Stores the vertical index of a stack so that the same
  1256. * vertical slide can be selected when navigating to and
  1257. * from the stack.
  1258. *
  1259. * @param {HTMLElement} stack The vertical stack element
  1260. * @param {int} v Index to memorize
  1261. */
  1262. function setPreviousVerticalIndex( stack, v ) {
  1263. if( typeof stack === 'object' && typeof stack.setAttribute === 'function' ) {
  1264. stack.setAttribute( 'data-previous-indexv', v || 0 );
  1265. }
  1266. }
  1267. /**
  1268. * Retrieves the vertical index which was stored using
  1269. * #setPreviousVerticalIndex() or 0 if no previous index
  1270. * exists.
  1271. *
  1272. * @param {HTMLElement} stack The vertical stack element
  1273. */
  1274. function getPreviousVerticalIndex( stack ) {
  1275. if( typeof stack === 'object' && typeof stack.setAttribute === 'function' && stack.classList.contains( 'stack' ) ) {
  1276. // Prefer manually defined start-indexv
  1277. var attributeName = stack.hasAttribute( 'data-start-indexv' ) ? 'data-start-indexv' : 'data-previous-indexv';
  1278. return parseInt( stack.getAttribute( attributeName ) || 0, 10 );
  1279. }
  1280. return 0;
  1281. }
  1282. /**
  1283. * Displays the overview of slides (quick nav) by scaling
  1284. * down and arranging all slide elements.
  1285. */
  1286. function activateOverview() {
  1287. // Only proceed if enabled in config
  1288. if( config.overview && !isOverview() ) {
  1289. overview = true;
  1290. dom.wrapper.classList.add( 'overview' );
  1291. dom.wrapper.classList.remove( 'overview-deactivating' );
  1292. if( features.overviewTransitions ) {
  1293. setTimeout( function() {
  1294. dom.wrapper.classList.add( 'overview-animated' );
  1295. }, 1 );
  1296. }
  1297. // Don't auto-slide while in overview mode
  1298. cancelAutoSlide();
  1299. // Move the backgrounds element into the slide container to
  1300. // that the same scaling is applied
  1301. dom.slides.appendChild( dom.background );
  1302. // Clicking on an overview slide navigates to it
  1303. toArray( dom.wrapper.querySelectorAll( SLIDES_SELECTOR ) ).forEach( function( slide ) {
  1304. if( !slide.classList.contains( 'stack' ) ) {
  1305. slide.addEventListener( 'click', onOverviewSlideClicked, true );
  1306. }
  1307. } );
  1308. updateSlidesVisibility();
  1309. layoutOverview();
  1310. updateOverview();
  1311. layout();
  1312. // Notify observers of the overview showing
  1313. dispatchEvent( 'overviewshown', {
  1314. 'indexh': indexh,
  1315. 'indexv': indexv,
  1316. 'currentSlide': currentSlide
  1317. } );
  1318. }
  1319. }
  1320. /**
  1321. * Uses CSS transforms to position all slides in a grid for
  1322. * display inside of the overview mode.
  1323. */
  1324. function layoutOverview() {
  1325. var margin = 70;
  1326. var slideWidth = config.width + margin,
  1327. slideHeight = config.height + margin;
  1328. // Reverse in RTL mode
  1329. if( config.rtl ) {
  1330. slideWidth = -slideWidth;
  1331. }
  1332. // Layout slides
  1333. toArray( dom.wrapper.querySelectorAll( HORIZONTAL_SLIDES_SELECTOR ) ).forEach( function( hslide, h ) {
  1334. hslide.setAttribute( 'data-index-h', h );
  1335. transformElement( hslide, 'translate3d(' + ( h * slideWidth ) + 'px, 0, 0)' );
  1336. if( hslide.classList.contains( 'stack' ) ) {
  1337. toArray( hslide.querySelectorAll( 'section' ) ).forEach( function( vslide, v ) {
  1338. vslide.setAttribute( 'data-index-h', h );
  1339. vslide.setAttribute( 'data-index-v', v );
  1340. transformElement( vslide, 'translate3d(0, ' + ( v * slideHeight ) + 'px, 0)' );
  1341. } );
  1342. }
  1343. } );
  1344. // Layout slide backgrounds
  1345. toArray( dom.background.childNodes ).forEach( function( hbackground, h ) {
  1346. transformElement( hbackground, 'translate3d(' + ( h * slideWidth ) + 'px, 0, 0)' );
  1347. toArray( hbackground.querySelectorAll( '.slide-background' ) ).forEach( function( vbackground, v ) {
  1348. transformElement( vbackground, 'translate3d(0, ' + ( v * slideHeight ) + 'px, 0)' );
  1349. } );
  1350. } );
  1351. }
  1352. /**
  1353. * Moves the overview viewport to the current slides.
  1354. * Called each time the current slide changes.
  1355. */
  1356. function updateOverview() {
  1357. var margin = 70;
  1358. var slideWidth = config.width + margin,
  1359. slideHeight = config.height + margin;
  1360. // Reverse in RTL mode
  1361. if( config.rtl ) {
  1362. slideWidth = -slideWidth;
  1363. }
  1364. transformSlides( {
  1365. overview: [
  1366. 'translateX('+ ( -indexh * slideWidth ) +'px)',
  1367. 'translateY('+ ( -indexv * slideHeight ) +'px)',
  1368. 'translateZ('+ ( window.innerWidth < 400 ? -1000 : -2500 ) +'px)'
  1369. ].join( ' ' )
  1370. } );
  1371. }
  1372. /**
  1373. * Exits the slide overview and enters the currently
  1374. * active slide.
  1375. */
  1376. function deactivateOverview() {
  1377. // Only proceed if enabled in config
  1378. if( config.overview ) {
  1379. overview = false;
  1380. dom.wrapper.classList.remove( 'overview' );
  1381. dom.wrapper.classList.remove( 'overview-animated' );
  1382. // Temporarily add a class so that transitions can do different things
  1383. // depending on whether they are exiting/entering overview, or just
  1384. // moving from slide to slide
  1385. dom.wrapper.classList.add( 'overview-deactivating' );
  1386. setTimeout( function () {
  1387. dom.wrapper.classList.remove( 'overview-deactivating' );
  1388. }, 1 );
  1389. // Move the background element back out
  1390. dom.wrapper.appendChild( dom.background );
  1391. // Clean up changes made to slides
  1392. toArray( dom.wrapper.querySelectorAll( SLIDES_SELECTOR ) ).forEach( function( slide ) {
  1393. transformElement( slide, '' );
  1394. slide.removeEventListener( 'click', onOverviewSlideClicked, true );
  1395. } );
  1396. // Clean up changes made to backgrounds
  1397. toArray( dom.background.querySelectorAll( '.slide-background' ) ).forEach( function( background ) {
  1398. transformElement( background, '' );
  1399. } );
  1400. transformSlides( { overview: '' } );
  1401. slide( indexh, indexv );
  1402. layout();
  1403. cueAutoSlide();
  1404. // Notify observers of the overview hiding
  1405. dispatchEvent( 'overviewhidden', {
  1406. 'indexh': indexh,
  1407. 'indexv': indexv,
  1408. 'currentSlide': currentSlide
  1409. } );
  1410. }
  1411. }
  1412. /**
  1413. * Toggles the slide overview mode on and off.
  1414. *
  1415. * @param {Boolean} override Optional flag which overrides the
  1416. * toggle logic and forcibly sets the desired state. True means
  1417. * overview is open, false means it's closed.
  1418. */
  1419. function toggleOverview( override ) {
  1420. if( typeof override === 'boolean' ) {
  1421. override ? activateOverview() : deactivateOverview();
  1422. }
  1423. else {
  1424. isOverview() ? deactivateOverview() : activateOverview();
  1425. }
  1426. }
  1427. /**
  1428. * Checks if the overview is currently active.
  1429. *
  1430. * @return {Boolean} true if the overview is active,
  1431. * false otherwise
  1432. */
  1433. function isOverview() {
  1434. return overview;
  1435. }
  1436. /**
  1437. * Checks if the current or specified slide is vertical
  1438. * (nested within another slide).
  1439. *
  1440. * @param {HTMLElement} slide [optional] The slide to check
  1441. * orientation of
  1442. */
  1443. function isVerticalSlide( slide ) {
  1444. // Prefer slide argument, otherwise use current slide
  1445. slide = slide ? slide : currentSlide;
  1446. return slide && slide.parentNode && !!slide.parentNode.nodeName.match( /section/i );
  1447. }
  1448. /**
  1449. * Handling the fullscreen functionality via the fullscreen API
  1450. *
  1451. * @see http://fullscreen.spec.whatwg.org/
  1452. * @see https://developer.mozilla.org/en-US/docs/DOM/Using_fullscreen_mode
  1453. */
  1454. function enterFullscreen() {
  1455. var element = document.body;
  1456. // Check which implementation is available
  1457. var requestMethod = element.requestFullScreen ||
  1458. element.webkitRequestFullscreen ||
  1459. element.webkitRequestFullScreen ||
  1460. element.mozRequestFullScreen ||
  1461. element.msRequestFullscreen;
  1462. if( requestMethod ) {
  1463. requestMethod.apply( element );
  1464. }
  1465. }
  1466. /**
  1467. * Enters the paused mode which fades everything on screen to
  1468. * black.
  1469. */
  1470. function pause() {
  1471. if( config.pause ) {
  1472. var wasPaused = dom.wrapper.classList.contains( 'paused' );
  1473. cancelAutoSlide();
  1474. dom.wrapper.classList.add( 'paused' );
  1475. if( wasPaused === false ) {
  1476. dispatchEvent( 'paused' );
  1477. }
  1478. }
  1479. }
  1480. /**
  1481. * Exits from the paused mode.
  1482. */
  1483. function resume() {
  1484. var wasPaused = dom.wrapper.classList.contains( 'paused' );
  1485. dom.wrapper.classList.remove( 'paused' );
  1486. cueAutoSlide();
  1487. if( wasPaused ) {
  1488. dispatchEvent( 'resumed' );
  1489. }
  1490. }
  1491. /**
  1492. * Toggles the paused mode on and off.
  1493. */
  1494. function togglePause( override ) {
  1495. if( typeof override === 'boolean' ) {
  1496. override ? pause() : resume();
  1497. }
  1498. else {
  1499. isPaused() ? resume() : pause();
  1500. }
  1501. }
  1502. /**
  1503. * Checks if we are currently in the paused mode.
  1504. */
  1505. function isPaused() {
  1506. return dom.wrapper.classList.contains( 'paused' );
  1507. }
  1508. /**
  1509. * Toggles the auto slide mode on and off.
  1510. *
  1511. * @param {Boolean} override Optional flag which sets the desired state.
  1512. * True means autoplay starts, false means it stops.
  1513. */
  1514. function toggleAutoSlide( override ) {
  1515. if( typeof override === 'boolean' ) {
  1516. override ? resumeAutoSlide() : pauseAutoSlide();
  1517. }
  1518. else {
  1519. autoSlidePaused ? resumeAutoSlide() : pauseAutoSlide();
  1520. }
  1521. }
  1522. /**
  1523. * Checks if the auto slide mode is currently on.
  1524. */
  1525. function isAutoSliding() {
  1526. return !!( autoSlide && !autoSlidePaused );
  1527. }
  1528. /**
  1529. * Steps from the current point in the presentation to the
  1530. * slide which matches the specified horizontal and vertical
  1531. * indices.
  1532. *
  1533. * @param {int} h Horizontal index of the target slide
  1534. * @param {int} v Vertical index of the target slide
  1535. * @param {int} f Optional index of a fragment within the
  1536. * target slide to activate
  1537. * @param {int} o Optional origin for use in multimaster environments
  1538. */
  1539. function slide( h, v, f, o ) {
  1540. // Remember where we were at before
  1541. previousSlide = currentSlide;
  1542. // Query all horizontal slides in the deck
  1543. var horizontalSlides = dom.wrapper.querySelectorAll( HORIZONTAL_SLIDES_SELECTOR );
  1544. // If no vertical index is specified and the upcoming slide is a
  1545. // stack, resume at its previous vertical index
  1546. if( v === undefined && !isOverview() ) {
  1547. v = getPreviousVerticalIndex( horizontalSlides[ h ] );
  1548. }
  1549. // If we were on a vertical stack, remember what vertical index
  1550. // it was on so we can resume at the same position when returning
  1551. if( previousSlide && previousSlide.parentNode && previousSlide.parentNode.classList.contains( 'stack' ) ) {
  1552. setPreviousVerticalIndex( previousSlide.parentNode, indexv );
  1553. }
  1554. // Remember the state before this slide
  1555. var stateBefore = state.concat();
  1556. // Reset the state array
  1557. state.length = 0;
  1558. var indexhBefore = indexh || 0,
  1559. indexvBefore = indexv || 0;
  1560. // Activate and transition to the new slide
  1561. indexh = updateSlides( HORIZONTAL_SLIDES_SELECTOR, h === undefined ? indexh : h );
  1562. indexv = updateSlides( VERTICAL_SLIDES_SELECTOR, v === undefined ? indexv : v );
  1563. // Update the visibility of slides now that the indices have changed
  1564. updateSlidesVisibility();
  1565. layout();
  1566. // Apply the new state
  1567. stateLoop: for( var i = 0, len = state.length; i < len; i++ ) {
  1568. // Check if this state existed on the previous slide. If it
  1569. // did, we will avoid adding it repeatedly
  1570. for( var j = 0; j < stateBefore.length; j++ ) {
  1571. if( stateBefore[j] === state[i] ) {
  1572. stateBefore.splice( j, 1 );
  1573. continue stateLoop;
  1574. }
  1575. }
  1576. document.documentElement.classList.add( state[i] );
  1577. // Dispatch custom event matching the state's name
  1578. dispatchEvent( state[i] );
  1579. }
  1580. // Clean up the remains of the previous state
  1581. while( stateBefore.length ) {
  1582. document.documentElement.classList.remove( stateBefore.pop() );
  1583. }
  1584. // Update the overview if it's currently active
  1585. if( isOverview() ) {
  1586. updateOverview();
  1587. }
  1588. // Find the current horizontal slide and any possible vertical slides
  1589. // within it
  1590. var currentHorizontalSlide = horizontalSlides[ indexh ],
  1591. currentVerticalSlides = currentHorizontalSlide.querySelectorAll( 'section' );
  1592. // Store references to the previous and current slides
  1593. currentSlide = currentVerticalSlides[ indexv ] || currentHorizontalSlide;
  1594. // Show fragment, if specified
  1595. if( typeof f !== 'undefined' ) {
  1596. navigateFragment( f );
  1597. }
  1598. // Dispatch an event if the slide changed
  1599. var slideChanged = ( indexh !== indexhBefore || indexv !== indexvBefore );
  1600. if( slideChanged ) {
  1601. dispatchEvent( 'slidechanged', {
  1602. 'indexh': indexh,
  1603. 'indexv': indexv,
  1604. 'previousSlide': previousSlide,
  1605. 'currentSlide': currentSlide,
  1606. 'origin': o
  1607. } );
  1608. }
  1609. else {
  1610. // Ensure that the previous slide is never the same as the current
  1611. previousSlide = null;
  1612. }
  1613. // Solves an edge case where the previous slide maintains the
  1614. // 'present' class when navigating between adjacent vertical
  1615. // stacks
  1616. if( previousSlide ) {
  1617. previousSlide.classList.remove( 'present' );
  1618. previousSlide.setAttribute( 'aria-hidden', 'true' );
  1619. // Reset all slides upon navigate to home
  1620. // Issue: #285
  1621. if ( dom.wrapper.querySelector( HOME_SLIDE_SELECTOR ).classList.contains( 'present' ) ) {
  1622. // Launch async task
  1623. setTimeout( function () {
  1624. var slides = toArray( dom.wrapper.querySelectorAll( HORIZONTAL_SLIDES_SELECTOR + '.stack') ), i;
  1625. for( i in slides ) {
  1626. if( slides[i] ) {
  1627. // Reset stack
  1628. setPreviousVerticalIndex( slides[i], 0 );
  1629. }
  1630. }
  1631. }, 0 );
  1632. }
  1633. }
  1634. // Handle embedded content
  1635. if( slideChanged || !previousSlide ) {
  1636. stopEmbeddedContent( previousSlide );
  1637. startEmbeddedContent( currentSlide );
  1638. }
  1639. // Announce the current slide contents, for screen readers
  1640. dom.statusDiv.textContent = currentSlide.textContent;
  1641. updateControls();
  1642. updateProgress();
  1643. updateBackground();
  1644. updateParallax();
  1645. updateSlideNumber();
  1646. // Update the URL hash
  1647. writeURL();
  1648. cueAutoSlide();
  1649. }
  1650. /**
  1651. * Syncs the presentation with the current DOM. Useful
  1652. * when new slides or control elements are added or when
  1653. * the configuration has changed.
  1654. */
  1655. function sync() {
  1656. // Subscribe to input
  1657. removeEventListeners();
  1658. addEventListeners();
  1659. // Force a layout to make sure the current config is accounted for
  1660. layout();
  1661. // Reflect the current autoSlide value
  1662. autoSlide = config.autoSlide;
  1663. // Start auto-sliding if it's enabled
  1664. cueAutoSlide();
  1665. // Re-create the slide backgrounds
  1666. createBackgrounds();
  1667. // Write the current hash to the URL
  1668. writeURL();
  1669. sortAllFragments();
  1670. updateControls();
  1671. updateProgress();
  1672. updateBackground( true );
  1673. updateSlideNumber();
  1674. updateSlidesVisibility();
  1675. formatEmbeddedContent();
  1676. if( isOverview() ) {
  1677. layoutOverview();
  1678. }
  1679. }
  1680. /**
  1681. * Resets all vertical slides so that only the first
  1682. * is visible.
  1683. */
  1684. function resetVerticalSlides() {
  1685. var horizontalSlides = toArray( dom.wrapper.querySelectorAll( HORIZONTAL_SLIDES_SELECTOR ) );
  1686. horizontalSlides.forEach( function( horizontalSlide ) {
  1687. var verticalSlides = toArray( horizontalSlide.querySelectorAll( 'section' ) );
  1688. verticalSlides.forEach( function( verticalSlide, y ) {
  1689. if( y > 0 ) {
  1690. verticalSlide.classList.remove( 'present' );
  1691. verticalSlide.classList.remove( 'past' );
  1692. verticalSlide.classList.add( 'future' );
  1693. verticalSlide.setAttribute( 'aria-hidden', 'true' );
  1694. }
  1695. } );
  1696. } );
  1697. }
  1698. /**
  1699. * Sorts and formats all of fragments in the
  1700. * presentation.
  1701. */
  1702. function sortAllFragments() {
  1703. var horizontalSlides = toArray( dom.wrapper.querySelectorAll( HORIZONTAL_SLIDES_SELECTOR ) );
  1704. horizontalSlides.forEach( function( horizontalSlide ) {
  1705. var verticalSlides = toArray( horizontalSlide.querySelectorAll( 'section' ) );
  1706. verticalSlides.forEach( function( verticalSlide, y ) {
  1707. sortFragments( verticalSlide.querySelectorAll( '.fragment' ) );
  1708. } );
  1709. if( verticalSlides.length === 0 ) sortFragments( horizontalSlide.querySelectorAll( '.fragment' ) );
  1710. } );
  1711. }
  1712. /**
  1713. * Updates one dimension of slides by showing the slide
  1714. * with the specified index.
  1715. *
  1716. * @param {String} selector A CSS selector that will fetch
  1717. * the group of slides we are working with
  1718. * @param {Number} index The index of the slide that should be
  1719. * shown
  1720. *
  1721. * @return {Number} The index of the slide that is now shown,
  1722. * might differ from the passed in index if it was out of
  1723. * bounds.
  1724. */
  1725. function updateSlides( selector, index ) {
  1726. // Select all slides and convert the NodeList result to
  1727. // an array
  1728. var slides = toArray( dom.wrapper.querySelectorAll( selector ) ),
  1729. slidesLength = slides.length;
  1730. var printMode = isPrintingPDF();
  1731. if( slidesLength ) {
  1732. // Should the index loop?
  1733. if( config.loop ) {
  1734. index %= slidesLength;
  1735. if( index < 0 ) {
  1736. index = slidesLength + index;
  1737. }
  1738. }
  1739. // Enforce max and minimum index bounds
  1740. index = Math.max( Math.min( index, slidesLength - 1 ), 0 );
  1741. for( var i = 0; i < slidesLength; i++ ) {
  1742. var element = slides[i];
  1743. var reverse = config.rtl && !isVerticalSlide( element );
  1744. element.classList.remove( 'past' );
  1745. element.classList.remove( 'present' );
  1746. element.classList.remove( 'future' );
  1747. // http://www.w3.org/html/wg/drafts/html/master/editing.html#the-hidden-attribute
  1748. element.setAttribute( 'hidden', '' );
  1749. element.setAttribute( 'aria-hidden', 'true' );
  1750. // If this element contains vertical slides
  1751. if( element.querySelector( 'section' ) ) {
  1752. element.classList.add( 'stack' );
  1753. }
  1754. // If we're printing static slides, all slides are "present"
  1755. if( printMode ) {
  1756. element.classList.add( 'present' );
  1757. continue;
  1758. }
  1759. if( i < index ) {
  1760. // Any element previous to index is given the 'past' class
  1761. element.classList.add( reverse ? 'future' : 'past' );
  1762. if( config.fragments ) {
  1763. var pastFragments = toArray( element.querySelectorAll( '.fragment' ) );
  1764. // Show all fragments on prior slides
  1765. while( pastFragments.length ) {
  1766. var pastFragment = pastFragments.pop();
  1767. pastFragment.classList.add( 'visible' );
  1768. pastFragment.classList.remove( 'current-fragment' );
  1769. }
  1770. }
  1771. }
  1772. else if( i > index ) {
  1773. // Any element subsequent to index is given the 'future' class
  1774. element.classList.add( reverse ? 'past' : 'future' );
  1775. if( config.fragments ) {
  1776. var futureFragments = toArray( element.querySelectorAll( '.fragment.visible' ) );
  1777. // No fragments in future slides should be visible ahead of time
  1778. while( futureFragments.length ) {
  1779. var futureFragment = futureFragments.pop();
  1780. futureFragment.classList.remove( 'visible' );
  1781. futureFragment.classList.remove( 'current-fragment' );
  1782. }
  1783. }
  1784. }
  1785. }
  1786. // Mark the current slide as present
  1787. slides[index].classList.add( 'present' );
  1788. slides[index].removeAttribute( 'hidden' );
  1789. slides[index].removeAttribute( 'aria-hidden' );
  1790. // If this slide has a state associated with it, add it
  1791. // onto the current state of the deck
  1792. var slideState = slides[index].getAttribute( 'data-state' );
  1793. if( slideState ) {
  1794. state = state.concat( slideState.split( ' ' ) );
  1795. }
  1796. }
  1797. else {
  1798. // Since there are no slides we can't be anywhere beyond the
  1799. // zeroth index
  1800. index = 0;
  1801. }
  1802. return index;
  1803. }
  1804. /**
  1805. * Optimization method; hide all slides that are far away
  1806. * from the present slide.
  1807. */
  1808. function updateSlidesVisibility() {
  1809. // Select all slides and convert the NodeList result to
  1810. // an array
  1811. var horizontalSlides = toArray( dom.wrapper.querySelectorAll( HORIZONTAL_SLIDES_SELECTOR ) ),
  1812. horizontalSlidesLength = horizontalSlides.length,
  1813. distanceX,
  1814. distanceY;
  1815. if( horizontalSlidesLength && typeof indexh !== 'undefined' ) {
  1816. // The number of steps away from the present slide that will
  1817. // be visible
  1818. var viewDistance = isOverview() ? 10 : config.viewDistance;
  1819. // Limit view distance on weaker devices
  1820. if( isMobileDevice ) {
  1821. viewDistance = isOverview() ? 6 : 2;
  1822. }
  1823. // Limit view distance on weaker devices
  1824. if( isPrintingPDF() ) {
  1825. viewDistance = Number.MAX_VALUE;
  1826. }
  1827. for( var x = 0; x < horizontalSlidesLength; x++ ) {
  1828. var horizontalSlide = horizontalSlides[x];
  1829. var verticalSlides = toArray( horizontalSlide.querySelectorAll( 'section' ) ),
  1830. verticalSlidesLength = verticalSlides.length;
  1831. // Loops so that it measures 1 between the first and last slides
  1832. distanceX = Math.abs( ( ( indexh || 0 ) - x ) % ( horizontalSlidesLength - viewDistance ) ) || 0;
  1833. // Show the horizontal slide if it's within the view distance
  1834. if( distanceX < viewDistance ) {
  1835. showSlide( horizontalSlide );
  1836. }
  1837. else {
  1838. hideSlide( horizontalSlide );
  1839. }
  1840. if( verticalSlidesLength ) {
  1841. var oy = getPreviousVerticalIndex( horizontalSlide );
  1842. for( var y = 0; y < verticalSlidesLength; y++ ) {
  1843. var verticalSlide = verticalSlides[y];
  1844. distanceY = x === ( indexh || 0 ) ? Math.abs( ( indexv || 0 ) - y ) : Math.abs( y - oy );
  1845. if( distanceX + distanceY < viewDistance ) {
  1846. showSlide( verticalSlide );
  1847. }
  1848. else {
  1849. hideSlide( verticalSlide );
  1850. }
  1851. }
  1852. }
  1853. }
  1854. }
  1855. }
  1856. /**
  1857. * Updates the progress bar to reflect the current slide.
  1858. */
  1859. function updateProgress() {
  1860. // Update progress if enabled
  1861. if( config.progress && dom.progressbar ) {
  1862. dom.progressbar.style.width = getProgress() * dom.wrapper.offsetWidth + 'px';
  1863. }
  1864. }
  1865. /**
  1866. * Updates the slide number div to reflect the current slide.
  1867. *
  1868. * Slide number format can be defined as a string using the
  1869. * following variables:
  1870. * h: current slide's horizontal index
  1871. * v: current slide's vertical index
  1872. * c: current slide index (flattened)
  1873. * t: total number of slides (flattened)
  1874. */
  1875. function updateSlideNumber() {
  1876. // Update slide number if enabled
  1877. if( config.slideNumber && dom.slideNumber) {
  1878. // Default to only showing the current slide number
  1879. var format = 'c';
  1880. // Check if a custom slide number format is available
  1881. if( typeof config.slideNumber === 'string' ) {
  1882. format = config.slideNumber;
  1883. }
  1884. var totalSlides = getTotalSlides();
  1885. dom.slideNumber.innerHTML = format.replace( /h/g, indexh )
  1886. .replace( /v/g, indexv )
  1887. .replace( /c/g, Math.round( getProgress() * totalSlides ) + 1 )
  1888. .replace( /t/g, totalSlides + 1 );
  1889. }
  1890. }
  1891. /**
  1892. * Updates the state of all control/navigation arrows.
  1893. */
  1894. function updateControls() {
  1895. var routes = availableRoutes();
  1896. var fragments = availableFragments();
  1897. // Remove the 'enabled' class from all directions
  1898. dom.controlsLeft.concat( dom.controlsRight )
  1899. .concat( dom.controlsUp )
  1900. .concat( dom.controlsDown )
  1901. .concat( dom.controlsPrev )
  1902. .concat( dom.controlsNext ).forEach( function( node ) {
  1903. node.classList.remove( 'enabled' );
  1904. node.classList.remove( 'fragmented' );
  1905. } );
  1906. // Add the 'enabled' class to the available routes
  1907. if( routes.left ) dom.controlsLeft.forEach( function( el ) { el.classList.add( 'enabled' ); } );
  1908. if( routes.right ) dom.controlsRight.forEach( function( el ) { el.classList.add( 'enabled' ); } );
  1909. if( routes.up ) dom.controlsUp.forEach( function( el ) { el.classList.add( 'enabled' ); } );
  1910. if( routes.down ) dom.controlsDown.forEach( function( el ) { el.classList.add( 'enabled' ); } );
  1911. // Prev/next buttons
  1912. if( routes.left || routes.up ) dom.controlsPrev.forEach( function( el ) { el.classList.add( 'enabled' ); } );
  1913. if( routes.right || routes.down ) dom.controlsNext.forEach( function( el ) { el.classList.add( 'enabled' ); } );
  1914. // Highlight fragment directions
  1915. if( currentSlide ) {
  1916. // Always apply fragment decorator to prev/next buttons
  1917. if( fragments.prev ) dom.controlsPrev.forEach( function( el ) { el.classList.add( 'fragmented', 'enabled' ); } );
  1918. if( fragments.next ) dom.controlsNext.forEach( function( el ) { el.classList.add( 'fragmented', 'enabled' ); } );
  1919. // Apply fragment decorators to directional buttons based on
  1920. // what slide axis they are in
  1921. if( isVerticalSlide( currentSlide ) ) {
  1922. if( fragments.prev ) dom.controlsUp.forEach( function( el ) { el.classList.add( 'fragmented', 'enabled' ); } );
  1923. if( fragments.next ) dom.controlsDown.forEach( function( el ) { el.classList.add( 'fragmented', 'enabled' ); } );
  1924. }
  1925. else {
  1926. if( fragments.prev ) dom.controlsLeft.forEach( function( el ) { el.classList.add( 'fragmented', 'enabled' ); } );
  1927. if( fragments.next ) dom.controlsRight.forEach( function( el ) { el.classList.add( 'fragmented', 'enabled' ); } );
  1928. }
  1929. }
  1930. }
  1931. /**
  1932. * Updates the background elements to reflect the current
  1933. * slide.
  1934. *
  1935. * @param {Boolean} includeAll If true, the backgrounds of
  1936. * all vertical slides (not just the present) will be updated.
  1937. */
  1938. function updateBackground( includeAll ) {
  1939. var currentBackground = null;
  1940. // Reverse past/future classes when in RTL mode
  1941. var horizontalPast = config.rtl ? 'future' : 'past',
  1942. horizontalFuture = config.rtl ? 'past' : 'future';
  1943. // Update the classes of all backgrounds to match the
  1944. // states of their slides (past/present/future)
  1945. toArray( dom.background.childNodes ).forEach( function( backgroundh, h ) {
  1946. backgroundh.classList.remove( 'past' );
  1947. backgroundh.classList.remove( 'present' );
  1948. backgroundh.classList.remove( 'future' );
  1949. if( h < indexh ) {
  1950. backgroundh.classList.add( horizontalPast );
  1951. }
  1952. else if ( h > indexh ) {
  1953. backgroundh.classList.add( horizontalFuture );
  1954. }
  1955. else {
  1956. backgroundh.classList.add( 'present' );
  1957. // Store a reference to the current background element
  1958. currentBackground = backgroundh;
  1959. }
  1960. if( includeAll || h === indexh ) {
  1961. toArray( backgroundh.querySelectorAll( '.slide-background' ) ).forEach( function( backgroundv, v ) {
  1962. backgroundv.classList.remove( 'past' );
  1963. backgroundv.classList.remove( 'present' );
  1964. backgroundv.classList.remove( 'future' );
  1965. if( v < indexv ) {
  1966. backgroundv.classList.add( 'past' );
  1967. }
  1968. else if ( v > indexv ) {
  1969. backgroundv.classList.add( 'future' );
  1970. }
  1971. else {
  1972. backgroundv.classList.add( 'present' );
  1973. // Only if this is the present horizontal and vertical slide
  1974. if( h === indexh ) currentBackground = backgroundv;
  1975. }
  1976. } );
  1977. }
  1978. } );
  1979. // Stop any currently playing video background
  1980. if( previousBackground ) {
  1981. var previousVideo = previousBackground.querySelector( 'video' );
  1982. if( previousVideo ) previousVideo.pause();
  1983. }
  1984. if( currentBackground ) {
  1985. // Start video playback
  1986. var currentVideo = currentBackground.querySelector( 'video' );
  1987. if( currentVideo ) {
  1988. currentVideo.currentTime = 0;
  1989. currentVideo.play();
  1990. }
  1991. // Don't transition between identical backgrounds. This
  1992. // prevents unwanted flicker.
  1993. var previousBackgroundHash = previousBackground ? previousBackground.getAttribute( 'data-background-hash' ) : null;
  1994. var currentBackgroundHash = currentBackground.getAttribute( 'data-background-hash' );
  1995. if( currentBackgroundHash && currentBackgroundHash === previousBackgroundHash && currentBackground !== previousBackground ) {
  1996. dom.background.classList.add( 'no-transition' );
  1997. }
  1998. previousBackground = currentBackground;
  1999. }
  2000. // If there's a background brightness flag for this slide,
  2001. // bubble it to the .reveal container
  2002. if( currentSlide ) {
  2003. [ 'has-light-background', 'has-dark-background' ].forEach( function( classToBubble ) {
  2004. if( currentSlide.classList.contains( classToBubble ) ) {
  2005. dom.wrapper.classList.add( classToBubble );
  2006. }
  2007. else {
  2008. dom.wrapper.classList.remove( classToBubble );
  2009. }
  2010. } );
  2011. }
  2012. // Allow the first background to apply without transition
  2013. setTimeout( function() {
  2014. dom.background.classList.remove( 'no-transition' );
  2015. }, 1 );
  2016. }
  2017. /**
  2018. * Updates the position of the parallax background based
  2019. * on the current slide index.
  2020. */
  2021. function updateParallax() {
  2022. if( config.parallaxBackgroundImage ) {
  2023. var horizontalSlides = dom.wrapper.querySelectorAll( HORIZONTAL_SLIDES_SELECTOR ),
  2024. verticalSlides = dom.wrapper.querySelectorAll( VERTICAL_SLIDES_SELECTOR );
  2025. var backgroundSize = dom.background.style.backgroundSize.split( ' ' ),
  2026. backgroundWidth, backgroundHeight;
  2027. if( backgroundSize.length === 1 ) {
  2028. backgroundWidth = backgroundHeight = parseInt( backgroundSize[0], 10 );
  2029. }
  2030. else {
  2031. backgroundWidth = parseInt( backgroundSize[0], 10 );
  2032. backgroundHeight = parseInt( backgroundSize[1], 10 );
  2033. }
  2034. var slideWidth = dom.background.offsetWidth;
  2035. var horizontalSlideCount = horizontalSlides.length;
  2036. var horizontalOffset = -( backgroundWidth - slideWidth ) / ( horizontalSlideCount-1 ) * indexh;
  2037. var slideHeight = dom.background.offsetHeight;
  2038. var verticalSlideCount = verticalSlides.length;
  2039. var verticalOffset = verticalSlideCount > 1 ? -( backgroundHeight - slideHeight ) / ( verticalSlideCount-1 ) * indexv : 0;
  2040. dom.background.style.backgroundPosition = horizontalOffset + 'px ' + verticalOffset + 'px';
  2041. }
  2042. }
  2043. /**
  2044. * Called when the given slide is within the configured view
  2045. * distance. Shows the slide element and loads any content
  2046. * that is set to load lazily (data-src).
  2047. */
  2048. function showSlide( slide ) {
  2049. // Show the slide element
  2050. slide.style.display = 'block';
  2051. // Media elements with data-src attributes
  2052. toArray( slide.querySelectorAll( 'img[data-src], video[data-src], audio[data-src], iframe[data-src]' ) ).forEach( function( element ) {
  2053. element.setAttribute( 'src', element.getAttribute( 'data-src' ) );
  2054. element.removeAttribute( 'data-src' );
  2055. } );
  2056. // Media elements with <source> children
  2057. toArray( slide.querySelectorAll( 'video, audio' ) ).forEach( function( media ) {
  2058. var sources = 0;
  2059. toArray( media.querySelectorAll( 'source[data-src]' ) ).forEach( function( source ) {
  2060. source.setAttribute( 'src', source.getAttribute( 'data-src' ) );
  2061. source.removeAttribute( 'data-src' );
  2062. sources += 1;
  2063. } );
  2064. // If we rewrote sources for this video/audio element, we need
  2065. // to manually tell it to load from its new origin
  2066. if( sources > 0 ) {
  2067. media.load();
  2068. }
  2069. } );
  2070. // Show the corresponding background element
  2071. var indices = getIndices( slide );
  2072. var background = getSlideBackground( indices.h, indices.v );
  2073. if( background ) {
  2074. background.style.display = 'block';
  2075. // If the background contains media, load it
  2076. if( background.hasAttribute( 'data-loaded' ) === false ) {
  2077. background.setAttribute( 'data-loaded', 'true' );
  2078. var backgroundImage = slide.getAttribute( 'data-background-image' ),
  2079. backgroundVideo = slide.getAttribute( 'data-background-video' ),
  2080. backgroundIframe = slide.getAttribute( 'data-background-iframe' );
  2081. // Images
  2082. if( backgroundImage ) {
  2083. background.style.backgroundImage = 'url('+ backgroundImage +')';
  2084. }
  2085. // Videos
  2086. else if ( backgroundVideo && !isSpeakerNotes() ) {
  2087. var video = document.createElement( 'video' );
  2088. // Support comma separated lists of video sources
  2089. backgroundVideo.split( ',' ).forEach( function( source ) {
  2090. video.innerHTML += '<source src="'+ source +'">';
  2091. } );
  2092. background.appendChild( video );
  2093. }
  2094. // Iframes
  2095. else if ( backgroundIframe ) {
  2096. var iframe = document.createElement( 'iframe' );
  2097. iframe.setAttribute( 'src', backgroundIframe );
  2098. iframe.style.width = '100%';
  2099. iframe.style.height = '100%';
  2100. iframe.style.maxHeight = '100%';
  2101. iframe.style.maxWidth = '100%';
  2102. background.appendChild( iframe );
  2103. }
  2104. }
  2105. }
  2106. }
  2107. /**
  2108. * Called when the given slide is moved outside of the
  2109. * configured view distance.
  2110. */
  2111. function hideSlide( slide ) {
  2112. // Hide the slide element
  2113. slide.style.display = 'none';
  2114. // Hide the corresponding background element
  2115. var indices = getIndices( slide );
  2116. var background = getSlideBackground( indices.h, indices.v );
  2117. if( background ) {
  2118. background.style.display = 'none';
  2119. }
  2120. }
  2121. /**
  2122. * Determine what available routes there are for navigation.
  2123. *
  2124. * @return {Object} containing four booleans: left/right/up/down
  2125. */
  2126. function availableRoutes() {
  2127. var horizontalSlides = dom.wrapper.querySelectorAll( HORIZONTAL_SLIDES_SELECTOR ),
  2128. verticalSlides = dom.wrapper.querySelectorAll( VERTICAL_SLIDES_SELECTOR );
  2129. var routes = {
  2130. left: indexh > 0 || config.loop,
  2131. right: indexh < horizontalSlides.length - 1 || config.loop,
  2132. up: indexv > 0,
  2133. down: indexv < verticalSlides.length - 1
  2134. };
  2135. // reverse horizontal controls for rtl
  2136. if( config.rtl ) {
  2137. var left = routes.left;
  2138. routes.left = routes.right;
  2139. routes.right = left;
  2140. }
  2141. return routes;
  2142. }
  2143. /**
  2144. * Returns an object describing the available fragment
  2145. * directions.
  2146. *
  2147. * @return {Object} two boolean properties: prev/next
  2148. */
  2149. function availableFragments() {
  2150. if( currentSlide && config.fragments ) {
  2151. var fragments = currentSlide.querySelectorAll( '.fragment' );
  2152. var hiddenFragments = currentSlide.querySelectorAll( '.fragment:not(.visible)' );
  2153. return {
  2154. prev: fragments.length - hiddenFragments.length > 0,
  2155. next: !!hiddenFragments.length
  2156. };
  2157. }
  2158. else {
  2159. return { prev: false, next: false };
  2160. }
  2161. }
  2162. /**
  2163. * Enforces origin-specific format rules for embedded media.
  2164. */
  2165. function formatEmbeddedContent() {
  2166. // YouTube frames must include "?enablejsapi=1"
  2167. toArray( dom.slides.querySelectorAll( 'iframe[src*="youtube.com/embed/"]' ) ).forEach( function( el ) {
  2168. var src = el.getAttribute( 'src' );
  2169. if( !/enablejsapi\=1/gi.test( src ) ) {
  2170. el.setAttribute( 'src', src + ( !/\?/.test( src ) ? '?' : '&' ) + 'enablejsapi=1' );
  2171. }
  2172. });
  2173. // Vimeo frames must include "?api=1"
  2174. toArray( dom.slides.querySelectorAll( 'iframe[src*="player.vimeo.com/"]' ) ).forEach( function( el ) {
  2175. var src = el.getAttribute( 'src' );
  2176. if( !/api\=1/gi.test( src ) ) {
  2177. el.setAttribute( 'src', src + ( !/\?/.test( src ) ? '?' : '&' ) + 'api=1' );
  2178. }
  2179. });
  2180. }
  2181. /**
  2182. * Start playback of any embedded content inside of
  2183. * the targeted slide.
  2184. */
  2185. function startEmbeddedContent( slide ) {
  2186. if( slide && !isSpeakerNotes() ) {
  2187. // HTML5 media elements
  2188. toArray( slide.querySelectorAll( 'video, audio' ) ).forEach( function( el ) {
  2189. if( el.hasAttribute( 'data-autoplay' ) ) {
  2190. el.play();
  2191. }
  2192. } );
  2193. // iframe embeds
  2194. toArray( slide.querySelectorAll( 'iframe' ) ).forEach( function( el ) {
  2195. el.contentWindow.postMessage( 'slide:start', '*' );
  2196. });
  2197. // YouTube embeds
  2198. toArray( slide.querySelectorAll( 'iframe[src*="youtube.com/embed/"]' ) ).forEach( function( el ) {
  2199. if( el.hasAttribute( 'data-autoplay' ) ) {
  2200. el.contentWindow.postMessage( '{"event":"command","func":"playVideo","args":""}', '*' );
  2201. }
  2202. });
  2203. // Vimeo embeds
  2204. toArray( slide.querySelectorAll( 'iframe[src*="player.vimeo.com/"]' ) ).forEach( function( el ) {
  2205. if( el.hasAttribute( 'data-autoplay' ) ) {
  2206. el.contentWindow.postMessage( '{"method":"play"}', '*' );
  2207. }
  2208. });
  2209. }
  2210. }
  2211. /**
  2212. * Stop playback of any embedded content inside of
  2213. * the targeted slide.
  2214. */
  2215. function stopEmbeddedContent( slide ) {
  2216. if( slide && slide.parentNode ) {
  2217. // HTML5 media elements
  2218. toArray( slide.querySelectorAll( 'video, audio' ) ).forEach( function( el ) {
  2219. if( !el.hasAttribute( 'data-ignore' ) ) {
  2220. el.pause();
  2221. }
  2222. } );
  2223. // iframe embeds
  2224. toArray( slide.querySelectorAll( 'iframe' ) ).forEach( function( el ) {
  2225. el.contentWindow.postMessage( 'slide:stop', '*' );
  2226. });
  2227. // YouTube embeds
  2228. toArray( slide.querySelectorAll( 'iframe[src*="youtube.com/embed/"]' ) ).forEach( function( el ) {
  2229. if( !el.hasAttribute( 'data-ignore' ) && typeof el.contentWindow.postMessage === 'function' ) {
  2230. el.contentWindow.postMessage( '{"event":"command","func":"pauseVideo","args":""}', '*' );
  2231. }
  2232. });
  2233. // Vimeo embeds
  2234. toArray( slide.querySelectorAll( 'iframe[src*="player.vimeo.com/"]' ) ).forEach( function( el ) {
  2235. if( !el.hasAttribute( 'data-ignore' ) && typeof el.contentWindow.postMessage === 'function' ) {
  2236. el.contentWindow.postMessage( '{"method":"pause"}', '*' );
  2237. }
  2238. });
  2239. }
  2240. }
  2241. /**
  2242. * Returns a value ranging from 0-1 that represents
  2243. * how far into the presentation we have navigated.
  2244. */
  2245. function getProgress() {
  2246. var horizontalSlides = toArray( dom.wrapper.querySelectorAll( HORIZONTAL_SLIDES_SELECTOR ) );
  2247. // The number of past and total slides
  2248. var totalCount = getTotalSlides();
  2249. var pastCount = 0;
  2250. // Step through all slides and count the past ones
  2251. mainLoop: for( var i = 0; i < horizontalSlides.length; i++ ) {
  2252. var horizontalSlide = horizontalSlides[i];
  2253. var verticalSlides = toArray( horizontalSlide.querySelectorAll( 'section' ) );
  2254. for( var j = 0; j < verticalSlides.length; j++ ) {
  2255. // Stop as soon as we arrive at the present
  2256. if( verticalSlides[j].classList.contains( 'present' ) ) {
  2257. break mainLoop;
  2258. }
  2259. pastCount++;
  2260. }
  2261. // Stop as soon as we arrive at the present
  2262. if( horizontalSlide.classList.contains( 'present' ) ) {
  2263. break;
  2264. }
  2265. // Don't count the wrapping section for vertical slides
  2266. if( horizontalSlide.classList.contains( 'stack' ) === false ) {
  2267. pastCount++;
  2268. }
  2269. }
  2270. if( currentSlide ) {
  2271. var allFragments = currentSlide.querySelectorAll( '.fragment' );
  2272. // If there are fragments in the current slide those should be
  2273. // accounted for in the progress.
  2274. if( allFragments.length > 0 ) {
  2275. var visibleFragments = currentSlide.querySelectorAll( '.fragment.visible' );
  2276. // This value represents how big a portion of the slide progress
  2277. // that is made up by its fragments (0-1)
  2278. var fragmentWeight = 0.9;
  2279. // Add fragment progress to the past slide count
  2280. pastCount += ( visibleFragments.length / allFragments.length ) * fragmentWeight;
  2281. }
  2282. }
  2283. return pastCount / ( totalCount - 1 );
  2284. }
  2285. /**
  2286. * Checks if this presentation is running inside of the
  2287. * speaker notes window.
  2288. */
  2289. function isSpeakerNotes() {
  2290. return !!window.location.search.match( /receiver/gi );
  2291. }
  2292. /**
  2293. * Reads the current URL (hash) and navigates accordingly.
  2294. */
  2295. function readURL() {
  2296. var hash = window.location.hash;
  2297. // Attempt to parse the hash as either an index or name
  2298. var bits = hash.slice( 2 ).split( '/' ),
  2299. name = hash.replace( /#|\//gi, '' );
  2300. // If the first bit is invalid and there is a name we can
  2301. // assume that this is a named link
  2302. if( isNaN( parseInt( bits[0], 10 ) ) && name.length ) {
  2303. var element;
  2304. // Ensure the named link is a valid HTML ID attribute
  2305. if( /^[a-zA-Z][\w:.-]*$/.test( name ) ) {
  2306. // Find the slide with the specified ID
  2307. element = document.getElementById( name );
  2308. }
  2309. if( element ) {
  2310. // Find the position of the named slide and navigate to it
  2311. var indices = Reveal.getIndices( element );
  2312. slide( indices.h, indices.v );
  2313. }
  2314. // If the slide doesn't exist, navigate to the current slide
  2315. else {
  2316. slide( indexh || 0, indexv || 0 );
  2317. }
  2318. }
  2319. else {
  2320. // Read the index components of the hash
  2321. var h = parseInt( bits[0], 10 ) || 0,
  2322. v = parseInt( bits[1], 10 ) || 0;
  2323. if( h !== indexh || v !== indexv ) {
  2324. slide( h, v );
  2325. }
  2326. }
  2327. }
  2328. /**
  2329. * Updates the page URL (hash) to reflect the current
  2330. * state.
  2331. *
  2332. * @param {Number} delay The time in ms to wait before
  2333. * writing the hash
  2334. */
  2335. function writeURL( delay ) {
  2336. if( config.history ) {
  2337. // Make sure there's never more than one timeout running
  2338. clearTimeout( writeURLTimeout );
  2339. // If a delay is specified, timeout this call
  2340. if( typeof delay === 'number' ) {
  2341. writeURLTimeout = setTimeout( writeURL, delay );
  2342. }
  2343. else if( currentSlide ) {
  2344. var url = '/';
  2345. // Attempt to create a named link based on the slide's ID
  2346. var id = currentSlide.getAttribute( 'id' );
  2347. if( id ) {
  2348. id = id.toLowerCase();
  2349. id = id.replace( /[^a-zA-Z0-9\-\_\:\.]/g, '' );
  2350. }
  2351. // If the current slide has an ID, use that as a named link
  2352. if( typeof id === 'string' && id.length ) {
  2353. url = '/' + id;
  2354. }
  2355. // Otherwise use the /h/v index
  2356. else {
  2357. if( indexh > 0 || indexv > 0 ) url += indexh;
  2358. if( indexv > 0 ) url += '/' + indexv;
  2359. }
  2360. window.location.hash = url;
  2361. }
  2362. }
  2363. }
  2364. /**
  2365. * Retrieves the h/v location of the current, or specified,
  2366. * slide.
  2367. *
  2368. * @param {HTMLElement} slide If specified, the returned
  2369. * index will be for this slide rather than the currently
  2370. * active one
  2371. *
  2372. * @return {Object} { h: <int>, v: <int>, f: <int> }
  2373. */
  2374. function getIndices( slide ) {
  2375. // By default, return the current indices
  2376. var h = indexh,
  2377. v = indexv,
  2378. f;
  2379. // If a slide is specified, return the indices of that slide
  2380. if( slide ) {
  2381. var isVertical = isVerticalSlide( slide );
  2382. var slideh = isVertical ? slide.parentNode : slide;
  2383. // Select all horizontal slides
  2384. var horizontalSlides = toArray( dom.wrapper.querySelectorAll( HORIZONTAL_SLIDES_SELECTOR ) );
  2385. // Now that we know which the horizontal slide is, get its index
  2386. h = Math.max( horizontalSlides.indexOf( slideh ), 0 );
  2387. // Assume we're not vertical
  2388. v = undefined;
  2389. // If this is a vertical slide, grab the vertical index
  2390. if( isVertical ) {
  2391. v = Math.max( toArray( slide.parentNode.querySelectorAll( 'section' ) ).indexOf( slide ), 0 );
  2392. }
  2393. }
  2394. if( !slide && currentSlide ) {
  2395. var hasFragments = currentSlide.querySelectorAll( '.fragment' ).length > 0;
  2396. if( hasFragments ) {
  2397. var currentFragment = currentSlide.querySelector( '.current-fragment' );
  2398. if( currentFragment && currentFragment.hasAttribute( 'data-fragment-index' ) ) {
  2399. f = parseInt( currentFragment.getAttribute( 'data-fragment-index' ), 10 );
  2400. }
  2401. else {
  2402. f = currentSlide.querySelectorAll( '.fragment.visible' ).length - 1;
  2403. }
  2404. }
  2405. }
  2406. return { h: h, v: v, f: f };
  2407. }
  2408. /**
  2409. * Retrieves the total number of slides in this presentation.
  2410. */
  2411. function getTotalSlides() {
  2412. return dom.wrapper.querySelectorAll( SLIDES_SELECTOR + ':not(.stack)' ).length;
  2413. }
  2414. /**
  2415. * Returns the slide element matching the specified index.
  2416. */
  2417. function getSlide( x, y ) {
  2418. var horizontalSlide = dom.wrapper.querySelectorAll( HORIZONTAL_SLIDES_SELECTOR )[ x ];
  2419. var verticalSlides = horizontalSlide && horizontalSlide.querySelectorAll( 'section' );
  2420. if( verticalSlides && verticalSlides.length && typeof y === 'number' ) {
  2421. return verticalSlides ? verticalSlides[ y ] : undefined;
  2422. }
  2423. return horizontalSlide;
  2424. }
  2425. /**
  2426. * Returns the background element for the given slide.
  2427. * All slides, even the ones with no background properties
  2428. * defined, have a background element so as long as the
  2429. * index is valid an element will be returned.
  2430. */
  2431. function getSlideBackground( x, y ) {
  2432. // When printing to PDF the slide backgrounds are nested
  2433. // inside of the slides
  2434. if( isPrintingPDF() ) {
  2435. var slide = getSlide( x, y );
  2436. if( slide ) {
  2437. var background = slide.querySelector( '.slide-background' );
  2438. if( background && background.parentNode === slide ) {
  2439. return background;
  2440. }
  2441. }
  2442. return undefined;
  2443. }
  2444. var horizontalBackground = dom.wrapper.querySelectorAll( '.backgrounds>.slide-background' )[ x ];
  2445. var verticalBackgrounds = horizontalBackground && horizontalBackground.querySelectorAll( '.slide-background' );
  2446. if( verticalBackgrounds && verticalBackgrounds.length && typeof y === 'number' ) {
  2447. return verticalBackgrounds ? verticalBackgrounds[ y ] : undefined;
  2448. }
  2449. return horizontalBackground;
  2450. }
  2451. /**
  2452. * Retrieves the current state of the presentation as
  2453. * an object. This state can then be restored at any
  2454. * time.
  2455. */
  2456. function getState() {
  2457. var indices = getIndices();
  2458. return {
  2459. indexh: indices.h,
  2460. indexv: indices.v,
  2461. indexf: indices.f,
  2462. paused: isPaused(),
  2463. overview: isOverview()
  2464. };
  2465. }
  2466. /**
  2467. * Restores the presentation to the given state.
  2468. *
  2469. * @param {Object} state As generated by getState()
  2470. */
  2471. function setState( state ) {
  2472. if( typeof state === 'object' ) {
  2473. slide( deserialize( state.indexh ), deserialize( state.indexv ), deserialize( state.indexf ) );
  2474. var pausedFlag = deserialize( state.paused ),
  2475. overviewFlag = deserialize( state.overview );
  2476. if( typeof pausedFlag === 'boolean' && pausedFlag !== isPaused() ) {
  2477. togglePause( pausedFlag );
  2478. }
  2479. if( typeof overviewFlag === 'boolean' && overviewFlag !== isOverview() ) {
  2480. toggleOverview( overviewFlag );
  2481. }
  2482. }
  2483. }
  2484. /**
  2485. * Return a sorted fragments list, ordered by an increasing
  2486. * "data-fragment-index" attribute.
  2487. *
  2488. * Fragments will be revealed in the order that they are returned by
  2489. * this function, so you can use the index attributes to control the
  2490. * order of fragment appearance.
  2491. *
  2492. * To maintain a sensible default fragment order, fragments are presumed
  2493. * to be passed in document order. This function adds a "fragment-index"
  2494. * attribute to each node if such an attribute is not already present,
  2495. * and sets that attribute to an integer value which is the position of
  2496. * the fragment within the fragments list.
  2497. */
  2498. function sortFragments( fragments ) {
  2499. fragments = toArray( fragments );
  2500. var ordered = [],
  2501. unordered = [],
  2502. sorted = [];
  2503. // Group ordered and unordered elements
  2504. fragments.forEach( function( fragment, i ) {
  2505. if( fragment.hasAttribute( 'data-fragment-index' ) ) {
  2506. var index = parseInt( fragment.getAttribute( 'data-fragment-index' ), 10 );
  2507. if( !ordered[index] ) {
  2508. ordered[index] = [];
  2509. }
  2510. ordered[index].push( fragment );
  2511. }
  2512. else {
  2513. unordered.push( [ fragment ] );
  2514. }
  2515. } );
  2516. // Append fragments without explicit indices in their
  2517. // DOM order
  2518. ordered = ordered.concat( unordered );
  2519. // Manually count the index up per group to ensure there
  2520. // are no gaps
  2521. var index = 0;
  2522. // Push all fragments in their sorted order to an array,
  2523. // this flattens the groups
  2524. ordered.forEach( function( group ) {
  2525. group.forEach( function( fragment ) {
  2526. sorted.push( fragment );
  2527. fragment.setAttribute( 'data-fragment-index', index );
  2528. } );
  2529. index ++;
  2530. } );
  2531. return sorted;
  2532. }
  2533. /**
  2534. * Navigate to the specified slide fragment.
  2535. *
  2536. * @param {Number} index The index of the fragment that
  2537. * should be shown, -1 means all are invisible
  2538. * @param {Number} offset Integer offset to apply to the
  2539. * fragment index
  2540. *
  2541. * @return {Boolean} true if a change was made in any
  2542. * fragments visibility as part of this call
  2543. */
  2544. function navigateFragment( index, offset ) {
  2545. if( currentSlide && config.fragments ) {
  2546. var fragments = sortFragments( currentSlide.querySelectorAll( '.fragment' ) );
  2547. if( fragments.length ) {
  2548. // If no index is specified, find the current
  2549. if( typeof index !== 'number' ) {
  2550. var lastVisibleFragment = sortFragments( currentSlide.querySelectorAll( '.fragment.visible' ) ).pop();
  2551. if( lastVisibleFragment ) {
  2552. index = parseInt( lastVisibleFragment.getAttribute( 'data-fragment-index' ) || 0, 10 );
  2553. }
  2554. else {
  2555. index = -1;
  2556. }
  2557. }
  2558. // If an offset is specified, apply it to the index
  2559. if( typeof offset === 'number' ) {
  2560. index += offset;
  2561. }
  2562. var fragmentsShown = [],
  2563. fragmentsHidden = [];
  2564. toArray( fragments ).forEach( function( element, i ) {
  2565. if( element.hasAttribute( 'data-fragment-index' ) ) {
  2566. i = parseInt( element.getAttribute( 'data-fragment-index' ), 10 );
  2567. }
  2568. // Visible fragments
  2569. if( i <= index ) {
  2570. if( !element.classList.contains( 'visible' ) ) fragmentsShown.push( element );
  2571. element.classList.add( 'visible' );
  2572. element.classList.remove( 'current-fragment' );
  2573. // Announce the fragments one by one to the Screen Reader
  2574. dom.statusDiv.textContent = element.textContent;
  2575. if( i === index ) {
  2576. element.classList.add( 'current-fragment' );
  2577. }
  2578. }
  2579. // Hidden fragments
  2580. else {
  2581. if( element.classList.contains( 'visible' ) ) fragmentsHidden.push( element );
  2582. element.classList.remove( 'visible' );
  2583. element.classList.remove( 'current-fragment' );
  2584. }
  2585. } );
  2586. if( fragmentsHidden.length ) {
  2587. dispatchEvent( 'fragmenthidden', { fragment: fragmentsHidden[0], fragments: fragmentsHidden } );
  2588. }
  2589. if( fragmentsShown.length ) {
  2590. dispatchEvent( 'fragmentshown', { fragment: fragmentsShown[0], fragments: fragmentsShown } );
  2591. }
  2592. updateControls();
  2593. updateProgress();
  2594. return !!( fragmentsShown.length || fragmentsHidden.length );
  2595. }
  2596. }
  2597. return false;
  2598. }
  2599. /**
  2600. * Navigate to the next slide fragment.
  2601. *
  2602. * @return {Boolean} true if there was a next fragment,
  2603. * false otherwise
  2604. */
  2605. function nextFragment() {
  2606. return navigateFragment( null, 1 );
  2607. }
  2608. /**
  2609. * Navigate to the previous slide fragment.
  2610. *
  2611. * @return {Boolean} true if there was a previous fragment,
  2612. * false otherwise
  2613. */
  2614. function previousFragment() {
  2615. return navigateFragment( null, -1 );
  2616. }
  2617. /**
  2618. * Cues a new automated slide if enabled in the config.
  2619. */
  2620. function cueAutoSlide() {
  2621. cancelAutoSlide();
  2622. if( currentSlide ) {
  2623. var currentFragment = currentSlide.querySelector( '.current-fragment' );
  2624. var fragmentAutoSlide = currentFragment ? currentFragment.getAttribute( 'data-autoslide' ) : null;
  2625. var parentAutoSlide = currentSlide.parentNode ? currentSlide.parentNode.getAttribute( 'data-autoslide' ) : null;
  2626. var slideAutoSlide = currentSlide.getAttribute( 'data-autoslide' );
  2627. // Pick value in the following priority order:
  2628. // 1. Current fragment's data-autoslide
  2629. // 2. Current slide's data-autoslide
  2630. // 3. Parent slide's data-autoslide
  2631. // 4. Global autoSlide setting
  2632. if( fragmentAutoSlide ) {
  2633. autoSlide = parseInt( fragmentAutoSlide, 10 );
  2634. }
  2635. else if( slideAutoSlide ) {
  2636. autoSlide = parseInt( slideAutoSlide, 10 );
  2637. }
  2638. else if( parentAutoSlide ) {
  2639. autoSlide = parseInt( parentAutoSlide, 10 );
  2640. }
  2641. else {
  2642. autoSlide = config.autoSlide;
  2643. }
  2644. // If there are media elements with data-autoplay,
  2645. // automatically set the autoSlide duration to the
  2646. // length of that media
  2647. toArray( currentSlide.querySelectorAll( 'video, audio' ) ).forEach( function( el ) {
  2648. if( el.hasAttribute( 'data-autoplay' ) ) {
  2649. if( autoSlide && el.duration * 1000 > autoSlide ) {
  2650. autoSlide = ( el.duration * 1000 ) + 1000;
  2651. }
  2652. }
  2653. } );
  2654. // Cue the next auto-slide if:
  2655. // - There is an autoSlide value
  2656. // - Auto-sliding isn't paused by the user
  2657. // - The presentation isn't paused
  2658. // - The overview isn't active
  2659. // - The presentation isn't over
  2660. if( autoSlide && !autoSlidePaused && !isPaused() && !isOverview() && ( !Reveal.isLastSlide() || availableFragments().next || config.loop === true ) ) {
  2661. autoSlideTimeout = setTimeout( navigateNext, autoSlide );
  2662. autoSlideStartTime = Date.now();
  2663. }
  2664. if( autoSlidePlayer ) {
  2665. autoSlidePlayer.setPlaying( autoSlideTimeout !== -1 );
  2666. }
  2667. }
  2668. }
  2669. /**
  2670. * Cancels any ongoing request to auto-slide.
  2671. */
  2672. function cancelAutoSlide() {
  2673. clearTimeout( autoSlideTimeout );
  2674. autoSlideTimeout = -1;
  2675. }
  2676. function pauseAutoSlide() {
  2677. if( autoSlide && !autoSlidePaused ) {
  2678. autoSlidePaused = true;
  2679. dispatchEvent( 'autoslidepaused' );
  2680. clearTimeout( autoSlideTimeout );
  2681. if( autoSlidePlayer ) {
  2682. autoSlidePlayer.setPlaying( false );
  2683. }
  2684. }
  2685. }
  2686. function resumeAutoSlide() {
  2687. if( autoSlide && autoSlidePaused ) {
  2688. autoSlidePaused = false;
  2689. dispatchEvent( 'autoslideresumed' );
  2690. cueAutoSlide();
  2691. }
  2692. }
  2693. function navigateLeft() {
  2694. // Reverse for RTL
  2695. if( config.rtl ) {
  2696. if( ( isOverview() || nextFragment() === false ) && availableRoutes().left ) {
  2697. slide( indexh + 1 );
  2698. }
  2699. }
  2700. // Normal navigation
  2701. else if( ( isOverview() || previousFragment() === false ) && availableRoutes().left ) {
  2702. slide( indexh - 1 );
  2703. }
  2704. }
  2705. function navigateRight() {
  2706. // Reverse for RTL
  2707. if( config.rtl ) {
  2708. if( ( isOverview() || previousFragment() === false ) && availableRoutes().right ) {
  2709. slide( indexh - 1 );
  2710. }
  2711. }
  2712. // Normal navigation
  2713. else if( ( isOverview() || nextFragment() === false ) && availableRoutes().right ) {
  2714. slide( indexh + 1 );
  2715. }
  2716. }
  2717. function navigateUp() {
  2718. // Prioritize hiding fragments
  2719. if( ( isOverview() || previousFragment() === false ) && availableRoutes().up ) {
  2720. slide( indexh, indexv - 1 );
  2721. }
  2722. }
  2723. function navigateDown() {
  2724. // Prioritize revealing fragments
  2725. if( ( isOverview() || nextFragment() === false ) && availableRoutes().down ) {
  2726. slide( indexh, indexv + 1 );
  2727. }
  2728. }
  2729. /**
  2730. * Navigates backwards, prioritized in the following order:
  2731. * 1) Previous fragment
  2732. * 2) Previous vertical slide
  2733. * 3) Previous horizontal slide
  2734. */
  2735. function navigatePrev() {
  2736. // Prioritize revealing fragments
  2737. if( previousFragment() === false ) {
  2738. if( availableRoutes().up ) {
  2739. navigateUp();
  2740. }
  2741. else {
  2742. // Fetch the previous horizontal slide, if there is one
  2743. var previousSlide;
  2744. if( config.rtl ) {
  2745. previousSlide = toArray( dom.wrapper.querySelectorAll( HORIZONTAL_SLIDES_SELECTOR + '.future' ) ).pop();
  2746. }
  2747. else {
  2748. previousSlide = toArray( dom.wrapper.querySelectorAll( HORIZONTAL_SLIDES_SELECTOR + '.past' ) ).pop();
  2749. }
  2750. if( previousSlide ) {
  2751. var v = ( previousSlide.querySelectorAll( 'section' ).length - 1 ) || undefined;
  2752. var h = indexh - 1;
  2753. slide( h, v );
  2754. }
  2755. }
  2756. }
  2757. }
  2758. /**
  2759. * The reverse of #navigatePrev().
  2760. */
  2761. function navigateNext() {
  2762. // Prioritize revealing fragments
  2763. if( nextFragment() === false ) {
  2764. if( availableRoutes().down ) {
  2765. navigateDown();
  2766. }
  2767. else if( config.rtl ) {
  2768. navigateLeft();
  2769. }
  2770. else {
  2771. navigateRight();
  2772. }
  2773. }
  2774. // If auto-sliding is enabled we need to cue up
  2775. // another timeout
  2776. cueAutoSlide();
  2777. }
  2778. // --------------------------------------------------------------------//
  2779. // ----------------------------- EVENTS -------------------------------//
  2780. // --------------------------------------------------------------------//
  2781. /**
  2782. * Called by all event handlers that are based on user
  2783. * input.
  2784. */
  2785. function onUserInput( event ) {
  2786. if( config.autoSlideStoppable ) {
  2787. pauseAutoSlide();
  2788. }
  2789. }
  2790. /**
  2791. * Handler for the document level 'keypress' event.
  2792. */
  2793. function onDocumentKeyPress( event ) {
  2794. // Check if the pressed key is question mark
  2795. if( event.shiftKey && event.charCode === 63 ) {
  2796. if( dom.overlay ) {
  2797. closeOverlay();
  2798. }
  2799. else {
  2800. showHelp( true );
  2801. }
  2802. }
  2803. }
  2804. /**
  2805. * Handler for the document level 'keydown' event.
  2806. */
  2807. function onDocumentKeyDown( event ) {
  2808. // If there's a condition specified and it returns false,
  2809. // ignore this event
  2810. if( typeof config.keyboardCondition === 'function' && config.keyboardCondition() === false ) {
  2811. return true;
  2812. }
  2813. // Remember if auto-sliding was paused so we can toggle it
  2814. var autoSlideWasPaused = autoSlidePaused;
  2815. onUserInput( event );
  2816. // Check if there's a focused element that could be using
  2817. // the keyboard
  2818. var activeElementIsCE = document.activeElement && document.activeElement.contentEditable !== 'inherit';
  2819. var activeElementIsInput = document.activeElement && document.activeElement.tagName && /input|textarea/i.test( document.activeElement.tagName );
  2820. // Disregard the event if there's a focused element or a
  2821. // keyboard modifier key is present
  2822. if( activeElementIsCE || activeElementIsInput || (event.shiftKey && event.keyCode !== 32) || event.altKey || event.ctrlKey || event.metaKey ) return;
  2823. // While paused only allow "unpausing" keyboard events (b and .)
  2824. if( isPaused() && [66,190,191].indexOf( event.keyCode ) === -1 ) {
  2825. return false;
  2826. }
  2827. var triggered = false;
  2828. // 1. User defined key bindings
  2829. if( typeof config.keyboard === 'object' ) {
  2830. for( var key in config.keyboard ) {
  2831. // Check if this binding matches the pressed key
  2832. if( parseInt( key, 10 ) === event.keyCode ) {
  2833. var value = config.keyboard[ key ];
  2834. // Callback function
  2835. if( typeof value === 'function' ) {
  2836. value.apply( null, [ event ] );
  2837. }
  2838. // String shortcuts to reveal.js API
  2839. else if( typeof value === 'string' && typeof Reveal[ value ] === 'function' ) {
  2840. Reveal[ value ].call();
  2841. }
  2842. triggered = true;
  2843. }
  2844. }
  2845. }
  2846. // 2. System defined key bindings
  2847. if( triggered === false ) {
  2848. // Assume true and try to prove false
  2849. triggered = true;
  2850. switch( event.keyCode ) {
  2851. // p, page up
  2852. case 80: case 33: navigatePrev(); break;
  2853. // n, page down
  2854. case 78: case 34: navigateNext(); break;
  2855. // h, left
  2856. case 72: case 37: navigateLeft(); break;
  2857. // l, right
  2858. case 76: case 39: navigateRight(); break;
  2859. // k, up
  2860. case 75: case 38: navigateUp(); break;
  2861. // j, down
  2862. case 74: case 40: navigateDown(); break;
  2863. // home
  2864. case 36: slide( 0 ); break;
  2865. // end
  2866. case 35: slide( Number.MAX_VALUE ); break;
  2867. // space
  2868. case 32: isOverview() ? deactivateOverview() : event.shiftKey ? navigatePrev() : navigateNext(); break;
  2869. // return
  2870. case 13: isOverview() ? deactivateOverview() : triggered = false; break;
  2871. // two-spot, semicolon, b, period, Logitech presenter tools "black screen" button
  2872. case 58: case 59: case 66: case 190: case 191: togglePause(); break;
  2873. // f
  2874. case 70: enterFullscreen(); break;
  2875. // a
  2876. case 65: if ( config.autoSlideStoppable ) toggleAutoSlide( autoSlideWasPaused ); break;
  2877. default:
  2878. triggered = false;
  2879. }
  2880. }
  2881. // If the input resulted in a triggered action we should prevent
  2882. // the browsers default behavior
  2883. if( triggered ) {
  2884. event.preventDefault && event.preventDefault();
  2885. }
  2886. // ESC or O key
  2887. else if ( ( event.keyCode === 27 || event.keyCode === 79 ) && features.transforms3d ) {
  2888. if( dom.overlay ) {
  2889. closeOverlay();
  2890. }
  2891. else {
  2892. toggleOverview();
  2893. }
  2894. event.preventDefault && event.preventDefault();
  2895. }
  2896. // If auto-sliding is enabled we need to cue up
  2897. // another timeout
  2898. cueAutoSlide();
  2899. }
  2900. /**
  2901. * Handler for the 'touchstart' event, enables support for
  2902. * swipe and pinch gestures.
  2903. */
  2904. function onTouchStart( event ) {
  2905. touch.startX = event.touches[0].clientX;
  2906. touch.startY = event.touches[0].clientY;
  2907. touch.startCount = event.touches.length;
  2908. // If there's two touches we need to memorize the distance
  2909. // between those two points to detect pinching
  2910. if( event.touches.length === 2 && config.overview ) {
  2911. touch.startSpan = distanceBetween( {
  2912. x: event.touches[1].clientX,
  2913. y: event.touches[1].clientY
  2914. }, {
  2915. x: touch.startX,
  2916. y: touch.startY
  2917. } );
  2918. }
  2919. }
  2920. /**
  2921. * Handler for the 'touchmove' event.
  2922. */
  2923. function onTouchMove( event ) {
  2924. // Each touch should only trigger one action
  2925. if( !touch.captured ) {
  2926. onUserInput( event );
  2927. var currentX = event.touches[0].clientX;
  2928. var currentY = event.touches[0].clientY;
  2929. // If the touch started with two points and still has
  2930. // two active touches; test for the pinch gesture
  2931. if( event.touches.length === 2 && touch.startCount === 2 && config.overview ) {
  2932. // The current distance in pixels between the two touch points
  2933. var currentSpan = distanceBetween( {
  2934. x: event.touches[1].clientX,
  2935. y: event.touches[1].clientY
  2936. }, {
  2937. x: touch.startX,
  2938. y: touch.startY
  2939. } );
  2940. // If the span is larger than the desire amount we've got
  2941. // ourselves a pinch
  2942. if( Math.abs( touch.startSpan - currentSpan ) > touch.threshold ) {
  2943. touch.captured = true;
  2944. if( currentSpan < touch.startSpan ) {
  2945. activateOverview();
  2946. }
  2947. else {
  2948. deactivateOverview();
  2949. }
  2950. }
  2951. event.preventDefault();
  2952. }
  2953. // There was only one touch point, look for a swipe
  2954. else if( event.touches.length === 1 && touch.startCount !== 2 ) {
  2955. var deltaX = currentX - touch.startX,
  2956. deltaY = currentY - touch.startY;
  2957. if( deltaX > touch.threshold && Math.abs( deltaX ) > Math.abs( deltaY ) ) {
  2958. touch.captured = true;
  2959. navigateLeft();
  2960. }
  2961. else if( deltaX < -touch.threshold && Math.abs( deltaX ) > Math.abs( deltaY ) ) {
  2962. touch.captured = true;
  2963. navigateRight();
  2964. }
  2965. else if( deltaY > touch.threshold ) {
  2966. touch.captured = true;
  2967. navigateUp();
  2968. }
  2969. else if( deltaY < -touch.threshold ) {
  2970. touch.captured = true;
  2971. navigateDown();
  2972. }
  2973. // If we're embedded, only block touch events if they have
  2974. // triggered an action
  2975. if( config.embedded ) {
  2976. if( touch.captured || isVerticalSlide( currentSlide ) ) {
  2977. event.preventDefault();
  2978. }
  2979. }
  2980. // Not embedded? Block them all to avoid needless tossing
  2981. // around of the viewport in iOS
  2982. else {
  2983. event.preventDefault();
  2984. }
  2985. }
  2986. }
  2987. // There's a bug with swiping on some Android devices unless
  2988. // the default action is always prevented
  2989. else if( navigator.userAgent.match( /android/gi ) ) {
  2990. event.preventDefault();
  2991. }
  2992. }
  2993. /**
  2994. * Handler for the 'touchend' event.
  2995. */
  2996. function onTouchEnd( event ) {
  2997. touch.captured = false;
  2998. }
  2999. /**
  3000. * Convert pointer down to touch start.
  3001. */
  3002. function onPointerDown( event ) {
  3003. if( event.pointerType === event.MSPOINTER_TYPE_TOUCH || event.pointerType === "touch" ) {
  3004. event.touches = [{ clientX: event.clientX, clientY: event.clientY }];
  3005. onTouchStart( event );
  3006. }
  3007. }
  3008. /**
  3009. * Convert pointer move to touch move.
  3010. */
  3011. function onPointerMove( event ) {
  3012. if( event.pointerType === event.MSPOINTER_TYPE_TOUCH || event.pointerType === "touch" ) {
  3013. event.touches = [{ clientX: event.clientX, clientY: event.clientY }];
  3014. onTouchMove( event );
  3015. }
  3016. }
  3017. /**
  3018. * Convert pointer up to touch end.
  3019. */
  3020. function onPointerUp( event ) {
  3021. if( event.pointerType === event.MSPOINTER_TYPE_TOUCH || event.pointerType === "touch" ) {
  3022. event.touches = [{ clientX: event.clientX, clientY: event.clientY }];
  3023. onTouchEnd( event );
  3024. }
  3025. }
  3026. /**
  3027. * Handles mouse wheel scrolling, throttled to avoid skipping
  3028. * multiple slides.
  3029. */
  3030. function onDocumentMouseScroll( event ) {
  3031. if( Date.now() - lastMouseWheelStep > 600 ) {
  3032. lastMouseWheelStep = Date.now();
  3033. var delta = event.detail || -event.wheelDelta;
  3034. if( delta > 0 ) {
  3035. navigateNext();
  3036. }
  3037. else {
  3038. navigatePrev();
  3039. }
  3040. }
  3041. }
  3042. /**
  3043. * Clicking on the progress bar results in a navigation to the
  3044. * closest approximate horizontal slide using this equation:
  3045. *
  3046. * ( clickX / presentationWidth ) * numberOfSlides
  3047. */
  3048. function onProgressClicked( event ) {
  3049. onUserInput( event );
  3050. event.preventDefault();
  3051. var slidesTotal = toArray( dom.wrapper.querySelectorAll( HORIZONTAL_SLIDES_SELECTOR ) ).length;
  3052. var slideIndex = Math.floor( ( event.clientX / dom.wrapper.offsetWidth ) * slidesTotal );
  3053. slide( slideIndex );
  3054. }
  3055. /**
  3056. * Event handler for navigation control buttons.
  3057. */
  3058. function onNavigateLeftClicked( event ) { event.preventDefault(); onUserInput(); navigateLeft(); }
  3059. function onNavigateRightClicked( event ) { event.preventDefault(); onUserInput(); navigateRight(); }
  3060. function onNavigateUpClicked( event ) { event.preventDefault(); onUserInput(); navigateUp(); }
  3061. function onNavigateDownClicked( event ) { event.preventDefault(); onUserInput(); navigateDown(); }
  3062. function onNavigatePrevClicked( event ) { event.preventDefault(); onUserInput(); navigatePrev(); }
  3063. function onNavigateNextClicked( event ) { event.preventDefault(); onUserInput(); navigateNext(); }
  3064. /**
  3065. * Handler for the window level 'hashchange' event.
  3066. */
  3067. function onWindowHashChange( event ) {
  3068. readURL();
  3069. }
  3070. /**
  3071. * Handler for the window level 'resize' event.
  3072. */
  3073. function onWindowResize( event ) {
  3074. layout();
  3075. }
  3076. /**
  3077. * Handle for the window level 'visibilitychange' event.
  3078. */
  3079. function onPageVisibilityChange( event ) {
  3080. var isHidden = document.webkitHidden ||
  3081. document.msHidden ||
  3082. document.hidden;
  3083. // If, after clicking a link or similar and we're coming back,
  3084. // focus the document.body to ensure we can use keyboard shortcuts
  3085. if( isHidden === false && document.activeElement !== document.body ) {
  3086. document.activeElement.blur();
  3087. document.body.focus();
  3088. }
  3089. }
  3090. /**
  3091. * Invoked when a slide is and we're in the overview.
  3092. */
  3093. function onOverviewSlideClicked( event ) {
  3094. // TODO There's a bug here where the event listeners are not
  3095. // removed after deactivating the overview.
  3096. if( eventsAreBound && isOverview() ) {
  3097. event.preventDefault();
  3098. var element = event.target;
  3099. while( element && !element.nodeName.match( /section/gi ) ) {
  3100. element = element.parentNode;
  3101. }
  3102. if( element && !element.classList.contains( 'disabled' ) ) {
  3103. deactivateOverview();
  3104. if( element.nodeName.match( /section/gi ) ) {
  3105. var h = parseInt( element.getAttribute( 'data-index-h' ), 10 ),
  3106. v = parseInt( element.getAttribute( 'data-index-v' ), 10 );
  3107. slide( h, v );
  3108. }
  3109. }
  3110. }
  3111. }
  3112. /**
  3113. * Handles clicks on links that are set to preview in the
  3114. * iframe overlay.
  3115. */
  3116. function onPreviewLinkClicked( event ) {
  3117. if( event.currentTarget && event.currentTarget.hasAttribute( 'href' ) ) {
  3118. var url = event.currentTarget.getAttribute( 'href' );
  3119. if( url ) {
  3120. showPreview( url );
  3121. event.preventDefault();
  3122. }
  3123. }
  3124. }
  3125. /**
  3126. * Handles click on the auto-sliding controls element.
  3127. */
  3128. function onAutoSlidePlayerClick( event ) {
  3129. // Replay
  3130. if( Reveal.isLastSlide() && config.loop === false ) {
  3131. slide( 0, 0 );
  3132. resumeAutoSlide();
  3133. }
  3134. // Resume
  3135. else if( autoSlidePaused ) {
  3136. resumeAutoSlide();
  3137. }
  3138. // Pause
  3139. else {
  3140. pauseAutoSlide();
  3141. }
  3142. }
  3143. // --------------------------------------------------------------------//
  3144. // ------------------------ PLAYBACK COMPONENT ------------------------//
  3145. // --------------------------------------------------------------------//
  3146. /**
  3147. * Constructor for the playback component, which displays
  3148. * play/pause/progress controls.
  3149. *
  3150. * @param {HTMLElement} container The component will append
  3151. * itself to this
  3152. * @param {Function} progressCheck A method which will be
  3153. * called frequently to get the current progress on a range
  3154. * of 0-1
  3155. */
  3156. function Playback( container, progressCheck ) {
  3157. // Cosmetics
  3158. this.diameter = 50;
  3159. this.thickness = 3;
  3160. // Flags if we are currently playing
  3161. this.playing = false;
  3162. // Current progress on a 0-1 range
  3163. this.progress = 0;
  3164. // Used to loop the animation smoothly
  3165. this.progressOffset = 1;
  3166. this.container = container;
  3167. this.progressCheck = progressCheck;
  3168. this.canvas = document.createElement( 'canvas' );
  3169. this.canvas.className = 'playback';
  3170. this.canvas.width = this.diameter;
  3171. this.canvas.height = this.diameter;
  3172. this.context = this.canvas.getContext( '2d' );
  3173. this.container.appendChild( this.canvas );
  3174. this.render();
  3175. }
  3176. Playback.prototype.setPlaying = function( value ) {
  3177. var wasPlaying = this.playing;
  3178. this.playing = value;
  3179. // Start repainting if we weren't already
  3180. if( !wasPlaying && this.playing ) {
  3181. this.animate();
  3182. }
  3183. else {
  3184. this.render();
  3185. }
  3186. };
  3187. Playback.prototype.animate = function() {
  3188. var progressBefore = this.progress;
  3189. this.progress = this.progressCheck();
  3190. // When we loop, offset the progress so that it eases
  3191. // smoothly rather than immediately resetting
  3192. if( progressBefore > 0.8 && this.progress < 0.2 ) {
  3193. this.progressOffset = this.progress;
  3194. }
  3195. this.render();
  3196. if( this.playing ) {
  3197. features.requestAnimationFrameMethod.call( window, this.animate.bind( this ) );
  3198. }
  3199. };
  3200. /**
  3201. * Renders the current progress and playback state.
  3202. */
  3203. Playback.prototype.render = function() {
  3204. var progress = this.playing ? this.progress : 0,
  3205. radius = ( this.diameter / 2 ) - this.thickness,
  3206. x = this.diameter / 2,
  3207. y = this.diameter / 2,
  3208. iconSize = 14;
  3209. // Ease towards 1
  3210. this.progressOffset += ( 1 - this.progressOffset ) * 0.1;
  3211. var endAngle = ( - Math.PI / 2 ) + ( progress * ( Math.PI * 2 ) );
  3212. var startAngle = ( - Math.PI / 2 ) + ( this.progressOffset * ( Math.PI * 2 ) );
  3213. this.context.save();
  3214. this.context.clearRect( 0, 0, this.diameter, this.diameter );
  3215. // Solid background color
  3216. this.context.beginPath();
  3217. this.context.arc( x, y, radius + 2, 0, Math.PI * 2, false );
  3218. this.context.fillStyle = 'rgba( 0, 0, 0, 0.4 )';
  3219. this.context.fill();
  3220. // Draw progress track
  3221. this.context.beginPath();
  3222. this.context.arc( x, y, radius, 0, Math.PI * 2, false );
  3223. this.context.lineWidth = this.thickness;
  3224. this.context.strokeStyle = '#666';
  3225. this.context.stroke();
  3226. if( this.playing ) {
  3227. // Draw progress on top of track
  3228. this.context.beginPath();
  3229. this.context.arc( x, y, radius, startAngle, endAngle, false );
  3230. this.context.lineWidth = this.thickness;
  3231. this.context.strokeStyle = '#fff';
  3232. this.context.stroke();
  3233. }
  3234. this.context.translate( x - ( iconSize / 2 ), y - ( iconSize / 2 ) );
  3235. // Draw play/pause icons
  3236. if( this.playing ) {
  3237. this.context.fillStyle = '#fff';
  3238. this.context.fillRect( 0, 0, iconSize / 2 - 2, iconSize );
  3239. this.context.fillRect( iconSize / 2 + 2, 0, iconSize / 2 - 2, iconSize );
  3240. }
  3241. else {
  3242. this.context.beginPath();
  3243. this.context.translate( 2, 0 );
  3244. this.context.moveTo( 0, 0 );
  3245. this.context.lineTo( iconSize - 2, iconSize / 2 );
  3246. this.context.lineTo( 0, iconSize );
  3247. this.context.fillStyle = '#fff';
  3248. this.context.fill();
  3249. }
  3250. this.context.restore();
  3251. };
  3252. Playback.prototype.on = function( type, listener ) {
  3253. this.canvas.addEventListener( type, listener, false );
  3254. };
  3255. Playback.prototype.off = function( type, listener ) {
  3256. this.canvas.removeEventListener( type, listener, false );
  3257. };
  3258. Playback.prototype.destroy = function() {
  3259. this.playing = false;
  3260. if( this.canvas.parentNode ) {
  3261. this.container.removeChild( this.canvas );
  3262. }
  3263. };
  3264. // --------------------------------------------------------------------//
  3265. // ------------------------------- API --------------------------------//
  3266. // --------------------------------------------------------------------//
  3267. Reveal = {
  3268. initialize: initialize,
  3269. configure: configure,
  3270. sync: sync,
  3271. // Navigation methods
  3272. slide: slide,
  3273. left: navigateLeft,
  3274. right: navigateRight,
  3275. up: navigateUp,
  3276. down: navigateDown,
  3277. prev: navigatePrev,
  3278. next: navigateNext,
  3279. // Fragment methods
  3280. navigateFragment: navigateFragment,
  3281. prevFragment: previousFragment,
  3282. nextFragment: nextFragment,
  3283. // Deprecated aliases
  3284. navigateTo: slide,
  3285. navigateLeft: navigateLeft,
  3286. navigateRight: navigateRight,
  3287. navigateUp: navigateUp,
  3288. navigateDown: navigateDown,
  3289. navigatePrev: navigatePrev,
  3290. navigateNext: navigateNext,
  3291. // Forces an update in slide layout
  3292. layout: layout,
  3293. // Returns an object with the available routes as booleans (left/right/top/bottom)
  3294. availableRoutes: availableRoutes,
  3295. // Returns an object with the available fragments as booleans (prev/next)
  3296. availableFragments: availableFragments,
  3297. // Toggles the overview mode on/off
  3298. toggleOverview: toggleOverview,
  3299. // Toggles the "black screen" mode on/off
  3300. togglePause: togglePause,
  3301. // Toggles the auto slide mode on/off
  3302. toggleAutoSlide: toggleAutoSlide,
  3303. // State checks
  3304. isOverview: isOverview,
  3305. isPaused: isPaused,
  3306. isAutoSliding: isAutoSliding,
  3307. // Adds or removes all internal event listeners (such as keyboard)
  3308. addEventListeners: addEventListeners,
  3309. removeEventListeners: removeEventListeners,
  3310. // Facility for persisting and restoring the presentation state
  3311. getState: getState,
  3312. setState: setState,
  3313. // Presentation progress on range of 0-1
  3314. getProgress: getProgress,
  3315. // Returns the indices of the current, or specified, slide
  3316. getIndices: getIndices,
  3317. getTotalSlides: getTotalSlides,
  3318. // Returns the slide element at the specified index
  3319. getSlide: getSlide,
  3320. // Returns the slide background element at the specified index
  3321. getSlideBackground: getSlideBackground,
  3322. // Returns the previous slide element, may be null
  3323. getPreviousSlide: function() {
  3324. return previousSlide;
  3325. },
  3326. // Returns the current slide element
  3327. getCurrentSlide: function() {
  3328. return currentSlide;
  3329. },
  3330. // Returns the current scale of the presentation content
  3331. getScale: function() {
  3332. return scale;
  3333. },
  3334. // Returns the current configuration object
  3335. getConfig: function() {
  3336. return config;
  3337. },
  3338. // Helper method, retrieves query string as a key/value hash
  3339. getQueryHash: function() {
  3340. var query = {};
  3341. location.search.replace( /[A-Z0-9]+?=([\w\.%-]*)/gi, function(a) {
  3342. query[ a.split( '=' ).shift() ] = a.split( '=' ).pop();
  3343. } );
  3344. // Basic deserialization
  3345. for( var i in query ) {
  3346. var value = query[ i ];
  3347. query[ i ] = deserialize( unescape( value ) );
  3348. }
  3349. return query;
  3350. },
  3351. // Returns true if we're currently on the first slide
  3352. isFirstSlide: function() {
  3353. return ( indexh === 0 && indexv === 0 );
  3354. },
  3355. // Returns true if we're currently on the last slide
  3356. isLastSlide: function() {
  3357. if( currentSlide ) {
  3358. // Does this slide has next a sibling?
  3359. if( currentSlide.nextElementSibling ) return false;
  3360. // If it's vertical, does its parent have a next sibling?
  3361. if( isVerticalSlide( currentSlide ) && currentSlide.parentNode.nextElementSibling ) return false;
  3362. return true;
  3363. }
  3364. return false;
  3365. },
  3366. // Checks if reveal.js has been loaded and is ready for use
  3367. isReady: function() {
  3368. return loaded;
  3369. },
  3370. // Forward event binding to the reveal DOM element
  3371. addEventListener: function( type, listener, useCapture ) {
  3372. if( 'addEventListener' in window ) {
  3373. ( dom.wrapper || document.querySelector( '.reveal' ) ).addEventListener( type, listener, useCapture );
  3374. }
  3375. },
  3376. removeEventListener: function( type, listener, useCapture ) {
  3377. if( 'addEventListener' in window ) {
  3378. ( dom.wrapper || document.querySelector( '.reveal' ) ).removeEventListener( type, listener, useCapture );
  3379. }
  3380. },
  3381. // Programatically triggers a keyboard event
  3382. triggerKey: function( keyCode ) {
  3383. onDocumentKeyDown( { keyCode: keyCode } );
  3384. }
  3385. };
  3386. return Reveal;
  3387. }));